dlmdomain.c 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390
  1. /* -*- mode: c; c-basic-offset: 8; -*-
  2. * vim: noexpandtab sw=8 ts=8 sts=0:
  3. *
  4. * dlmdomain.c
  5. *
  6. * defines domain join / leave apis
  7. *
  8. * Copyright (C) 2004 Oracle. All rights reserved.
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public
  12. * License as published by the Free Software Foundation; either
  13. * version 2 of the License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public
  21. * License along with this program; if not, write to the
  22. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  23. * Boston, MA 021110-1307, USA.
  24. *
  25. */
  26. #include <linux/module.h>
  27. #include <linux/types.h>
  28. #include <linux/slab.h>
  29. #include <linux/highmem.h>
  30. #include <linux/init.h>
  31. #include <linux/spinlock.h>
  32. #include <linux/delay.h>
  33. #include <linux/err.h>
  34. #include <linux/debugfs.h>
  35. #include "cluster/heartbeat.h"
  36. #include "cluster/nodemanager.h"
  37. #include "cluster/tcp.h"
  38. #include "dlmapi.h"
  39. #include "dlmcommon.h"
  40. #include "dlmdomain.h"
  41. #include "dlmdebug.h"
  42. #define MLOG_MASK_PREFIX (ML_DLM|ML_DLM_DOMAIN)
  43. #include "cluster/masklog.h"
  44. /*
  45. * ocfs2 node maps are array of long int, which limits to send them freely
  46. * across the wire due to endianness issues. To workaround this, we convert
  47. * long ints to byte arrays. Following 3 routines are helper functions to
  48. * set/test/copy bits within those array of bytes
  49. */
  50. static inline void byte_set_bit(u8 nr, u8 map[])
  51. {
  52. map[nr >> 3] |= (1UL << (nr & 7));
  53. }
  54. static inline int byte_test_bit(u8 nr, u8 map[])
  55. {
  56. return ((1UL << (nr & 7)) & (map[nr >> 3])) != 0;
  57. }
  58. static inline void byte_copymap(u8 dmap[], unsigned long smap[],
  59. unsigned int sz)
  60. {
  61. unsigned int nn;
  62. if (!sz)
  63. return;
  64. memset(dmap, 0, ((sz + 7) >> 3));
  65. for (nn = 0 ; nn < sz; nn++)
  66. if (test_bit(nn, smap))
  67. byte_set_bit(nn, dmap);
  68. }
  69. static void dlm_free_pagevec(void **vec, int pages)
  70. {
  71. while (pages--)
  72. free_page((unsigned long)vec[pages]);
  73. kfree(vec);
  74. }
  75. static void **dlm_alloc_pagevec(int pages)
  76. {
  77. void **vec = kmalloc(pages * sizeof(void *), GFP_KERNEL);
  78. int i;
  79. if (!vec)
  80. return NULL;
  81. for (i = 0; i < pages; i++)
  82. if (!(vec[i] = (void *)__get_free_page(GFP_KERNEL)))
  83. goto out_free;
  84. mlog(0, "Allocated DLM hash pagevec; %d pages (%lu expected), %lu buckets per page\n",
  85. pages, (unsigned long)DLM_HASH_PAGES,
  86. (unsigned long)DLM_BUCKETS_PER_PAGE);
  87. return vec;
  88. out_free:
  89. dlm_free_pagevec(vec, i);
  90. return NULL;
  91. }
  92. /*
  93. *
  94. * spinlock lock ordering: if multiple locks are needed, obey this ordering:
  95. * dlm_domain_lock
  96. * struct dlm_ctxt->spinlock
  97. * struct dlm_lock_resource->spinlock
  98. * struct dlm_ctxt->master_lock
  99. * struct dlm_ctxt->ast_lock
  100. * dlm_master_list_entry->spinlock
  101. * dlm_lock->spinlock
  102. *
  103. */
  104. DEFINE_SPINLOCK(dlm_domain_lock);
  105. LIST_HEAD(dlm_domains);
  106. static DECLARE_WAIT_QUEUE_HEAD(dlm_domain_events);
  107. /*
  108. * The supported protocol version for DLM communication. Running domains
  109. * will have a negotiated version with the same major number and a minor
  110. * number equal or smaller. The dlm_ctxt->dlm_locking_proto field should
  111. * be used to determine what a running domain is actually using.
  112. *
  113. * New in version 1.1:
  114. * - Message DLM_QUERY_REGION added to support global heartbeat
  115. * - Message DLM_QUERY_NODEINFO added to allow online node removes
  116. * New in version 1.2:
  117. * - Message DLM_BEGIN_EXIT_DOMAIN_MSG added to mark start of exit domain
  118. * New in version 1.3:
  119. * - Message DLM_DEREF_LOCKRES_DONE added to inform non-master that the
  120. * refmap is cleared
  121. */
  122. static const struct dlm_protocol_version dlm_protocol = {
  123. .pv_major = 1,
  124. .pv_minor = 3,
  125. };
  126. #define DLM_DOMAIN_BACKOFF_MS 200
  127. static int dlm_query_join_handler(struct o2net_msg *msg, u32 len, void *data,
  128. void **ret_data);
  129. static int dlm_assert_joined_handler(struct o2net_msg *msg, u32 len, void *data,
  130. void **ret_data);
  131. static int dlm_cancel_join_handler(struct o2net_msg *msg, u32 len, void *data,
  132. void **ret_data);
  133. static int dlm_query_region_handler(struct o2net_msg *msg, u32 len,
  134. void *data, void **ret_data);
  135. static int dlm_exit_domain_handler(struct o2net_msg *msg, u32 len, void *data,
  136. void **ret_data);
  137. static int dlm_protocol_compare(struct dlm_protocol_version *existing,
  138. struct dlm_protocol_version *request);
  139. static void dlm_unregister_domain_handlers(struct dlm_ctxt *dlm);
  140. void __dlm_unhash_lockres(struct dlm_ctxt *dlm, struct dlm_lock_resource *res)
  141. {
  142. if (hlist_unhashed(&res->hash_node))
  143. return;
  144. mlog(0, "%s: Unhash res %.*s\n", dlm->name, res->lockname.len,
  145. res->lockname.name);
  146. hlist_del_init(&res->hash_node);
  147. dlm_lockres_put(res);
  148. }
  149. void __dlm_insert_lockres(struct dlm_ctxt *dlm, struct dlm_lock_resource *res)
  150. {
  151. struct hlist_head *bucket;
  152. assert_spin_locked(&dlm->spinlock);
  153. bucket = dlm_lockres_hash(dlm, res->lockname.hash);
  154. /* get a reference for our hashtable */
  155. dlm_lockres_get(res);
  156. hlist_add_head(&res->hash_node, bucket);
  157. mlog(0, "%s: Hash res %.*s\n", dlm->name, res->lockname.len,
  158. res->lockname.name);
  159. }
  160. struct dlm_lock_resource * __dlm_lookup_lockres_full(struct dlm_ctxt *dlm,
  161. const char *name,
  162. unsigned int len,
  163. unsigned int hash)
  164. {
  165. struct hlist_head *bucket;
  166. struct dlm_lock_resource *res;
  167. mlog(0, "%.*s\n", len, name);
  168. assert_spin_locked(&dlm->spinlock);
  169. bucket = dlm_lockres_hash(dlm, hash);
  170. hlist_for_each_entry(res, bucket, hash_node) {
  171. if (res->lockname.name[0] != name[0])
  172. continue;
  173. if (unlikely(res->lockname.len != len))
  174. continue;
  175. if (memcmp(res->lockname.name + 1, name + 1, len - 1))
  176. continue;
  177. dlm_lockres_get(res);
  178. return res;
  179. }
  180. return NULL;
  181. }
  182. /* intended to be called by functions which do not care about lock
  183. * resources which are being purged (most net _handler functions).
  184. * this will return NULL for any lock resource which is found but
  185. * currently in the process of dropping its mastery reference.
  186. * use __dlm_lookup_lockres_full when you need the lock resource
  187. * regardless (e.g. dlm_get_lock_resource) */
  188. struct dlm_lock_resource * __dlm_lookup_lockres(struct dlm_ctxt *dlm,
  189. const char *name,
  190. unsigned int len,
  191. unsigned int hash)
  192. {
  193. struct dlm_lock_resource *res = NULL;
  194. mlog(0, "%.*s\n", len, name);
  195. assert_spin_locked(&dlm->spinlock);
  196. res = __dlm_lookup_lockres_full(dlm, name, len, hash);
  197. if (res) {
  198. spin_lock(&res->spinlock);
  199. if (res->state & DLM_LOCK_RES_DROPPING_REF) {
  200. spin_unlock(&res->spinlock);
  201. dlm_lockres_put(res);
  202. return NULL;
  203. }
  204. spin_unlock(&res->spinlock);
  205. }
  206. return res;
  207. }
  208. struct dlm_lock_resource * dlm_lookup_lockres(struct dlm_ctxt *dlm,
  209. const char *name,
  210. unsigned int len)
  211. {
  212. struct dlm_lock_resource *res;
  213. unsigned int hash = dlm_lockid_hash(name, len);
  214. spin_lock(&dlm->spinlock);
  215. res = __dlm_lookup_lockres(dlm, name, len, hash);
  216. spin_unlock(&dlm->spinlock);
  217. return res;
  218. }
  219. static struct dlm_ctxt * __dlm_lookup_domain_full(const char *domain, int len)
  220. {
  221. struct dlm_ctxt *tmp;
  222. assert_spin_locked(&dlm_domain_lock);
  223. /* tmp->name here is always NULL terminated,
  224. * but domain may not be! */
  225. list_for_each_entry(tmp, &dlm_domains, list) {
  226. if (strlen(tmp->name) == len &&
  227. memcmp(tmp->name, domain, len)==0)
  228. return tmp;
  229. }
  230. return NULL;
  231. }
  232. /* For null terminated domain strings ONLY */
  233. static struct dlm_ctxt * __dlm_lookup_domain(const char *domain)
  234. {
  235. assert_spin_locked(&dlm_domain_lock);
  236. return __dlm_lookup_domain_full(domain, strlen(domain));
  237. }
  238. /* returns true on one of two conditions:
  239. * 1) the domain does not exist
  240. * 2) the domain exists and it's state is "joined" */
  241. static int dlm_wait_on_domain_helper(const char *domain)
  242. {
  243. int ret = 0;
  244. struct dlm_ctxt *tmp = NULL;
  245. spin_lock(&dlm_domain_lock);
  246. tmp = __dlm_lookup_domain(domain);
  247. if (!tmp)
  248. ret = 1;
  249. else if (tmp->dlm_state == DLM_CTXT_JOINED)
  250. ret = 1;
  251. spin_unlock(&dlm_domain_lock);
  252. return ret;
  253. }
  254. static void dlm_free_ctxt_mem(struct dlm_ctxt *dlm)
  255. {
  256. dlm_destroy_debugfs_subroot(dlm);
  257. if (dlm->lockres_hash)
  258. dlm_free_pagevec((void **)dlm->lockres_hash, DLM_HASH_PAGES);
  259. if (dlm->master_hash)
  260. dlm_free_pagevec((void **)dlm->master_hash, DLM_HASH_PAGES);
  261. kfree(dlm->name);
  262. kfree(dlm);
  263. }
  264. /* A little strange - this function will be called while holding
  265. * dlm_domain_lock and is expected to be holding it on the way out. We
  266. * will however drop and reacquire it multiple times */
  267. static void dlm_ctxt_release(struct kref *kref)
  268. {
  269. struct dlm_ctxt *dlm;
  270. dlm = container_of(kref, struct dlm_ctxt, dlm_refs);
  271. BUG_ON(dlm->num_joins);
  272. BUG_ON(dlm->dlm_state == DLM_CTXT_JOINED);
  273. /* we may still be in the list if we hit an error during join. */
  274. list_del_init(&dlm->list);
  275. spin_unlock(&dlm_domain_lock);
  276. mlog(0, "freeing memory from domain %s\n", dlm->name);
  277. wake_up(&dlm_domain_events);
  278. dlm_free_ctxt_mem(dlm);
  279. spin_lock(&dlm_domain_lock);
  280. }
  281. void dlm_put(struct dlm_ctxt *dlm)
  282. {
  283. spin_lock(&dlm_domain_lock);
  284. kref_put(&dlm->dlm_refs, dlm_ctxt_release);
  285. spin_unlock(&dlm_domain_lock);
  286. }
  287. static void __dlm_get(struct dlm_ctxt *dlm)
  288. {
  289. kref_get(&dlm->dlm_refs);
  290. }
  291. /* given a questionable reference to a dlm object, gets a reference if
  292. * it can find it in the list, otherwise returns NULL in which case
  293. * you shouldn't trust your pointer. */
  294. struct dlm_ctxt *dlm_grab(struct dlm_ctxt *dlm)
  295. {
  296. struct dlm_ctxt *target;
  297. struct dlm_ctxt *ret = NULL;
  298. spin_lock(&dlm_domain_lock);
  299. list_for_each_entry(target, &dlm_domains, list) {
  300. if (target == dlm) {
  301. __dlm_get(target);
  302. ret = target;
  303. break;
  304. }
  305. }
  306. spin_unlock(&dlm_domain_lock);
  307. return ret;
  308. }
  309. int dlm_domain_fully_joined(struct dlm_ctxt *dlm)
  310. {
  311. int ret;
  312. spin_lock(&dlm_domain_lock);
  313. ret = (dlm->dlm_state == DLM_CTXT_JOINED) ||
  314. (dlm->dlm_state == DLM_CTXT_IN_SHUTDOWN);
  315. spin_unlock(&dlm_domain_lock);
  316. return ret;
  317. }
  318. static void dlm_destroy_dlm_worker(struct dlm_ctxt *dlm)
  319. {
  320. if (dlm->dlm_worker) {
  321. flush_workqueue(dlm->dlm_worker);
  322. destroy_workqueue(dlm->dlm_worker);
  323. dlm->dlm_worker = NULL;
  324. }
  325. }
  326. static void dlm_complete_dlm_shutdown(struct dlm_ctxt *dlm)
  327. {
  328. dlm_unregister_domain_handlers(dlm);
  329. dlm_debug_shutdown(dlm);
  330. dlm_complete_thread(dlm);
  331. dlm_complete_recovery_thread(dlm);
  332. dlm_destroy_dlm_worker(dlm);
  333. /* We've left the domain. Now we can take ourselves out of the
  334. * list and allow the kref stuff to help us free the
  335. * memory. */
  336. spin_lock(&dlm_domain_lock);
  337. list_del_init(&dlm->list);
  338. spin_unlock(&dlm_domain_lock);
  339. /* Wake up anyone waiting for us to remove this domain */
  340. wake_up(&dlm_domain_events);
  341. }
  342. static int dlm_migrate_all_locks(struct dlm_ctxt *dlm)
  343. {
  344. int i, num, n, ret = 0;
  345. struct dlm_lock_resource *res;
  346. struct hlist_node *iter;
  347. struct hlist_head *bucket;
  348. int dropped;
  349. mlog(0, "Migrating locks from domain %s\n", dlm->name);
  350. num = 0;
  351. spin_lock(&dlm->spinlock);
  352. for (i = 0; i < DLM_HASH_BUCKETS; i++) {
  353. redo_bucket:
  354. n = 0;
  355. bucket = dlm_lockres_hash(dlm, i);
  356. iter = bucket->first;
  357. while (iter) {
  358. n++;
  359. res = hlist_entry(iter, struct dlm_lock_resource,
  360. hash_node);
  361. dlm_lockres_get(res);
  362. /* migrate, if necessary. this will drop the dlm
  363. * spinlock and retake it if it does migration. */
  364. dropped = dlm_empty_lockres(dlm, res);
  365. spin_lock(&res->spinlock);
  366. if (dropped)
  367. __dlm_lockres_calc_usage(dlm, res);
  368. else
  369. iter = res->hash_node.next;
  370. spin_unlock(&res->spinlock);
  371. dlm_lockres_put(res);
  372. if (dropped) {
  373. cond_resched_lock(&dlm->spinlock);
  374. goto redo_bucket;
  375. }
  376. }
  377. cond_resched_lock(&dlm->spinlock);
  378. num += n;
  379. }
  380. spin_unlock(&dlm->spinlock);
  381. wake_up(&dlm->dlm_thread_wq);
  382. /* let the dlm thread take care of purging, keep scanning until
  383. * nothing remains in the hash */
  384. if (num) {
  385. mlog(0, "%s: %d lock resources in hash last pass\n",
  386. dlm->name, num);
  387. ret = -EAGAIN;
  388. }
  389. mlog(0, "DONE Migrating locks from domain %s\n", dlm->name);
  390. return ret;
  391. }
  392. static int dlm_no_joining_node(struct dlm_ctxt *dlm)
  393. {
  394. int ret;
  395. spin_lock(&dlm->spinlock);
  396. ret = dlm->joining_node == DLM_LOCK_RES_OWNER_UNKNOWN;
  397. spin_unlock(&dlm->spinlock);
  398. return ret;
  399. }
  400. static int dlm_begin_exit_domain_handler(struct o2net_msg *msg, u32 len,
  401. void *data, void **ret_data)
  402. {
  403. struct dlm_ctxt *dlm = data;
  404. unsigned int node;
  405. struct dlm_exit_domain *exit_msg = (struct dlm_exit_domain *) msg->buf;
  406. if (!dlm_grab(dlm))
  407. return 0;
  408. node = exit_msg->node_idx;
  409. mlog(0, "%s: Node %u sent a begin exit domain message\n", dlm->name, node);
  410. spin_lock(&dlm->spinlock);
  411. set_bit(node, dlm->exit_domain_map);
  412. spin_unlock(&dlm->spinlock);
  413. dlm_put(dlm);
  414. return 0;
  415. }
  416. static void dlm_mark_domain_leaving(struct dlm_ctxt *dlm)
  417. {
  418. /* Yikes, a double spinlock! I need domain_lock for the dlm
  419. * state and the dlm spinlock for join state... Sorry! */
  420. again:
  421. spin_lock(&dlm_domain_lock);
  422. spin_lock(&dlm->spinlock);
  423. if (dlm->joining_node != DLM_LOCK_RES_OWNER_UNKNOWN) {
  424. mlog(0, "Node %d is joining, we wait on it.\n",
  425. dlm->joining_node);
  426. spin_unlock(&dlm->spinlock);
  427. spin_unlock(&dlm_domain_lock);
  428. wait_event(dlm->dlm_join_events, dlm_no_joining_node(dlm));
  429. goto again;
  430. }
  431. dlm->dlm_state = DLM_CTXT_LEAVING;
  432. spin_unlock(&dlm->spinlock);
  433. spin_unlock(&dlm_domain_lock);
  434. }
  435. static void __dlm_print_nodes(struct dlm_ctxt *dlm)
  436. {
  437. int node = -1, num = 0;
  438. assert_spin_locked(&dlm->spinlock);
  439. printk("( ");
  440. while ((node = find_next_bit(dlm->domain_map, O2NM_MAX_NODES,
  441. node + 1)) < O2NM_MAX_NODES) {
  442. printk("%d ", node);
  443. ++num;
  444. }
  445. printk(") %u nodes\n", num);
  446. }
  447. static int dlm_exit_domain_handler(struct o2net_msg *msg, u32 len, void *data,
  448. void **ret_data)
  449. {
  450. struct dlm_ctxt *dlm = data;
  451. unsigned int node;
  452. struct dlm_exit_domain *exit_msg = (struct dlm_exit_domain *) msg->buf;
  453. mlog(0, "%p %u %p", msg, len, data);
  454. if (!dlm_grab(dlm))
  455. return 0;
  456. node = exit_msg->node_idx;
  457. spin_lock(&dlm->spinlock);
  458. clear_bit(node, dlm->domain_map);
  459. clear_bit(node, dlm->exit_domain_map);
  460. printk(KERN_NOTICE "o2dlm: Node %u leaves domain %s ", node, dlm->name);
  461. __dlm_print_nodes(dlm);
  462. /* notify anything attached to the heartbeat events */
  463. dlm_hb_event_notify_attached(dlm, node, 0);
  464. spin_unlock(&dlm->spinlock);
  465. dlm_put(dlm);
  466. return 0;
  467. }
  468. static int dlm_send_one_domain_exit(struct dlm_ctxt *dlm, u32 msg_type,
  469. unsigned int node)
  470. {
  471. int status;
  472. struct dlm_exit_domain leave_msg;
  473. mlog(0, "%s: Sending domain exit message %u to node %u\n", dlm->name,
  474. msg_type, node);
  475. memset(&leave_msg, 0, sizeof(leave_msg));
  476. leave_msg.node_idx = dlm->node_num;
  477. status = o2net_send_message(msg_type, dlm->key, &leave_msg,
  478. sizeof(leave_msg), node, NULL);
  479. if (status < 0)
  480. mlog(ML_ERROR, "Error %d sending domain exit message %u "
  481. "to node %u on domain %s\n", status, msg_type, node,
  482. dlm->name);
  483. return status;
  484. }
  485. static void dlm_begin_exit_domain(struct dlm_ctxt *dlm)
  486. {
  487. int node = -1;
  488. /* Support for begin exit domain was added in 1.2 */
  489. if (dlm->dlm_locking_proto.pv_major == 1 &&
  490. dlm->dlm_locking_proto.pv_minor < 2)
  491. return;
  492. /*
  493. * Unlike DLM_EXIT_DOMAIN_MSG, DLM_BEGIN_EXIT_DOMAIN_MSG is purely
  494. * informational. Meaning if a node does not receive the message,
  495. * so be it.
  496. */
  497. spin_lock(&dlm->spinlock);
  498. while (1) {
  499. node = find_next_bit(dlm->domain_map, O2NM_MAX_NODES, node + 1);
  500. if (node >= O2NM_MAX_NODES)
  501. break;
  502. if (node == dlm->node_num)
  503. continue;
  504. spin_unlock(&dlm->spinlock);
  505. dlm_send_one_domain_exit(dlm, DLM_BEGIN_EXIT_DOMAIN_MSG, node);
  506. spin_lock(&dlm->spinlock);
  507. }
  508. spin_unlock(&dlm->spinlock);
  509. }
  510. static void dlm_leave_domain(struct dlm_ctxt *dlm)
  511. {
  512. int node, clear_node, status;
  513. /* At this point we've migrated away all our locks and won't
  514. * accept mastership of new ones. The dlm is responsible for
  515. * almost nothing now. We make sure not to confuse any joining
  516. * nodes and then commence shutdown procedure. */
  517. spin_lock(&dlm->spinlock);
  518. /* Clear ourselves from the domain map */
  519. clear_bit(dlm->node_num, dlm->domain_map);
  520. while ((node = find_next_bit(dlm->domain_map, O2NM_MAX_NODES,
  521. 0)) < O2NM_MAX_NODES) {
  522. /* Drop the dlm spinlock. This is safe wrt the domain_map.
  523. * -nodes cannot be added now as the
  524. * query_join_handlers knows to respond with OK_NO_MAP
  525. * -we catch the right network errors if a node is
  526. * removed from the map while we're sending him the
  527. * exit message. */
  528. spin_unlock(&dlm->spinlock);
  529. clear_node = 1;
  530. status = dlm_send_one_domain_exit(dlm, DLM_EXIT_DOMAIN_MSG,
  531. node);
  532. if (status < 0 &&
  533. status != -ENOPROTOOPT &&
  534. status != -ENOTCONN) {
  535. mlog(ML_NOTICE, "Error %d sending domain exit message "
  536. "to node %d\n", status, node);
  537. /* Not sure what to do here but lets sleep for
  538. * a bit in case this was a transient
  539. * error... */
  540. msleep(DLM_DOMAIN_BACKOFF_MS);
  541. clear_node = 0;
  542. }
  543. spin_lock(&dlm->spinlock);
  544. /* If we're not clearing the node bit then we intend
  545. * to loop back around to try again. */
  546. if (clear_node)
  547. clear_bit(node, dlm->domain_map);
  548. }
  549. spin_unlock(&dlm->spinlock);
  550. }
  551. int dlm_shutting_down(struct dlm_ctxt *dlm)
  552. {
  553. int ret = 0;
  554. spin_lock(&dlm_domain_lock);
  555. if (dlm->dlm_state == DLM_CTXT_IN_SHUTDOWN)
  556. ret = 1;
  557. spin_unlock(&dlm_domain_lock);
  558. return ret;
  559. }
  560. void dlm_unregister_domain(struct dlm_ctxt *dlm)
  561. {
  562. int leave = 0;
  563. struct dlm_lock_resource *res;
  564. spin_lock(&dlm_domain_lock);
  565. BUG_ON(dlm->dlm_state != DLM_CTXT_JOINED);
  566. BUG_ON(!dlm->num_joins);
  567. dlm->num_joins--;
  568. if (!dlm->num_joins) {
  569. /* We mark it "in shutdown" now so new register
  570. * requests wait until we've completely left the
  571. * domain. Don't use DLM_CTXT_LEAVING yet as we still
  572. * want new domain joins to communicate with us at
  573. * least until we've completed migration of our
  574. * resources. */
  575. dlm->dlm_state = DLM_CTXT_IN_SHUTDOWN;
  576. leave = 1;
  577. }
  578. spin_unlock(&dlm_domain_lock);
  579. if (leave) {
  580. mlog(0, "shutting down domain %s\n", dlm->name);
  581. dlm_begin_exit_domain(dlm);
  582. /* We changed dlm state, notify the thread */
  583. dlm_kick_thread(dlm, NULL);
  584. while (dlm_migrate_all_locks(dlm)) {
  585. /* Give dlm_thread time to purge the lockres' */
  586. msleep(500);
  587. mlog(0, "%s: more migration to do\n", dlm->name);
  588. }
  589. /* This list should be empty. If not, print remaining lockres */
  590. if (!list_empty(&dlm->tracking_list)) {
  591. mlog(ML_ERROR, "Following lockres' are still on the "
  592. "tracking list:\n");
  593. list_for_each_entry(res, &dlm->tracking_list, tracking)
  594. dlm_print_one_lock_resource(res);
  595. }
  596. dlm_mark_domain_leaving(dlm);
  597. dlm_leave_domain(dlm);
  598. printk(KERN_NOTICE "o2dlm: Leaving domain %s\n", dlm->name);
  599. dlm_force_free_mles(dlm);
  600. dlm_complete_dlm_shutdown(dlm);
  601. }
  602. dlm_put(dlm);
  603. }
  604. EXPORT_SYMBOL_GPL(dlm_unregister_domain);
  605. static int dlm_query_join_proto_check(char *proto_type, int node,
  606. struct dlm_protocol_version *ours,
  607. struct dlm_protocol_version *request)
  608. {
  609. int rc;
  610. struct dlm_protocol_version proto = *request;
  611. if (!dlm_protocol_compare(ours, &proto)) {
  612. mlog(0,
  613. "node %u wanted to join with %s locking protocol "
  614. "%u.%u, we respond with %u.%u\n",
  615. node, proto_type,
  616. request->pv_major,
  617. request->pv_minor,
  618. proto.pv_major, proto.pv_minor);
  619. request->pv_minor = proto.pv_minor;
  620. rc = 0;
  621. } else {
  622. mlog(ML_NOTICE,
  623. "Node %u wanted to join with %s locking "
  624. "protocol %u.%u, but we have %u.%u, disallowing\n",
  625. node, proto_type,
  626. request->pv_major,
  627. request->pv_minor,
  628. ours->pv_major,
  629. ours->pv_minor);
  630. rc = 1;
  631. }
  632. return rc;
  633. }
  634. /*
  635. * struct dlm_query_join_packet is made up of four one-byte fields. They
  636. * are effectively in big-endian order already. However, little-endian
  637. * machines swap them before putting the packet on the wire (because
  638. * query_join's response is a status, and that status is treated as a u32
  639. * on the wire). Thus, a big-endian and little-endian machines will treat
  640. * this structure differently.
  641. *
  642. * The solution is to have little-endian machines swap the structure when
  643. * converting from the structure to the u32 representation. This will
  644. * result in the structure having the correct format on the wire no matter
  645. * the host endian format.
  646. */
  647. static void dlm_query_join_packet_to_wire(struct dlm_query_join_packet *packet,
  648. u32 *wire)
  649. {
  650. union dlm_query_join_response response;
  651. response.packet = *packet;
  652. *wire = be32_to_cpu(response.intval);
  653. }
  654. static void dlm_query_join_wire_to_packet(u32 wire,
  655. struct dlm_query_join_packet *packet)
  656. {
  657. union dlm_query_join_response response;
  658. response.intval = cpu_to_be32(wire);
  659. *packet = response.packet;
  660. }
  661. static int dlm_query_join_handler(struct o2net_msg *msg, u32 len, void *data,
  662. void **ret_data)
  663. {
  664. struct dlm_query_join_request *query;
  665. struct dlm_query_join_packet packet = {
  666. .code = JOIN_DISALLOW,
  667. };
  668. struct dlm_ctxt *dlm = NULL;
  669. u32 response;
  670. u8 nodenum;
  671. query = (struct dlm_query_join_request *) msg->buf;
  672. mlog(0, "node %u wants to join domain %s\n", query->node_idx,
  673. query->domain);
  674. /*
  675. * If heartbeat doesn't consider the node live, tell it
  676. * to back off and try again. This gives heartbeat a chance
  677. * to catch up.
  678. */
  679. if (!o2hb_check_node_heartbeating_no_sem(query->node_idx)) {
  680. mlog(0, "node %u is not in our live map yet\n",
  681. query->node_idx);
  682. packet.code = JOIN_DISALLOW;
  683. goto respond;
  684. }
  685. packet.code = JOIN_OK_NO_MAP;
  686. spin_lock(&dlm_domain_lock);
  687. dlm = __dlm_lookup_domain_full(query->domain, query->name_len);
  688. if (!dlm)
  689. goto unlock_respond;
  690. /*
  691. * There is a small window where the joining node may not see the
  692. * node(s) that just left but still part of the cluster. DISALLOW
  693. * join request if joining node has different node map.
  694. */
  695. nodenum=0;
  696. while (nodenum < O2NM_MAX_NODES) {
  697. if (test_bit(nodenum, dlm->domain_map)) {
  698. if (!byte_test_bit(nodenum, query->node_map)) {
  699. mlog(0, "disallow join as node %u does not "
  700. "have node %u in its nodemap\n",
  701. query->node_idx, nodenum);
  702. packet.code = JOIN_DISALLOW;
  703. goto unlock_respond;
  704. }
  705. }
  706. nodenum++;
  707. }
  708. /* Once the dlm ctxt is marked as leaving then we don't want
  709. * to be put in someone's domain map.
  710. * Also, explicitly disallow joining at certain troublesome
  711. * times (ie. during recovery). */
  712. if (dlm->dlm_state != DLM_CTXT_LEAVING) {
  713. int bit = query->node_idx;
  714. spin_lock(&dlm->spinlock);
  715. if (dlm->dlm_state == DLM_CTXT_NEW &&
  716. dlm->joining_node == DLM_LOCK_RES_OWNER_UNKNOWN) {
  717. /*If this is a brand new context and we
  718. * haven't started our join process yet, then
  719. * the other node won the race. */
  720. packet.code = JOIN_OK_NO_MAP;
  721. } else if (dlm->joining_node != DLM_LOCK_RES_OWNER_UNKNOWN) {
  722. /* Disallow parallel joins. */
  723. packet.code = JOIN_DISALLOW;
  724. } else if (dlm->reco.state & DLM_RECO_STATE_ACTIVE) {
  725. mlog(0, "node %u trying to join, but recovery "
  726. "is ongoing.\n", bit);
  727. packet.code = JOIN_DISALLOW;
  728. } else if (test_bit(bit, dlm->recovery_map)) {
  729. mlog(0, "node %u trying to join, but it "
  730. "still needs recovery.\n", bit);
  731. packet.code = JOIN_DISALLOW;
  732. } else if (test_bit(bit, dlm->domain_map)) {
  733. mlog(0, "node %u trying to join, but it "
  734. "is still in the domain! needs recovery?\n",
  735. bit);
  736. packet.code = JOIN_DISALLOW;
  737. } else {
  738. /* Alright we're fully a part of this domain
  739. * so we keep some state as to who's joining
  740. * and indicate to him that needs to be fixed
  741. * up. */
  742. /* Make sure we speak compatible locking protocols. */
  743. if (dlm_query_join_proto_check("DLM", bit,
  744. &dlm->dlm_locking_proto,
  745. &query->dlm_proto)) {
  746. packet.code = JOIN_PROTOCOL_MISMATCH;
  747. } else if (dlm_query_join_proto_check("fs", bit,
  748. &dlm->fs_locking_proto,
  749. &query->fs_proto)) {
  750. packet.code = JOIN_PROTOCOL_MISMATCH;
  751. } else {
  752. packet.dlm_minor = query->dlm_proto.pv_minor;
  753. packet.fs_minor = query->fs_proto.pv_minor;
  754. packet.code = JOIN_OK;
  755. __dlm_set_joining_node(dlm, query->node_idx);
  756. }
  757. }
  758. spin_unlock(&dlm->spinlock);
  759. }
  760. unlock_respond:
  761. spin_unlock(&dlm_domain_lock);
  762. respond:
  763. mlog(0, "We respond with %u\n", packet.code);
  764. dlm_query_join_packet_to_wire(&packet, &response);
  765. return response;
  766. }
  767. static int dlm_assert_joined_handler(struct o2net_msg *msg, u32 len, void *data,
  768. void **ret_data)
  769. {
  770. struct dlm_assert_joined *assert;
  771. struct dlm_ctxt *dlm = NULL;
  772. assert = (struct dlm_assert_joined *) msg->buf;
  773. mlog(0, "node %u asserts join on domain %s\n", assert->node_idx,
  774. assert->domain);
  775. spin_lock(&dlm_domain_lock);
  776. dlm = __dlm_lookup_domain_full(assert->domain, assert->name_len);
  777. /* XXX should we consider no dlm ctxt an error? */
  778. if (dlm) {
  779. spin_lock(&dlm->spinlock);
  780. /* Alright, this node has officially joined our
  781. * domain. Set him in the map and clean up our
  782. * leftover join state. */
  783. BUG_ON(dlm->joining_node != assert->node_idx);
  784. if (dlm->reco.state & DLM_RECO_STATE_ACTIVE) {
  785. mlog(0, "dlm recovery is ongoing, disallow join\n");
  786. spin_unlock(&dlm->spinlock);
  787. spin_unlock(&dlm_domain_lock);
  788. return -EAGAIN;
  789. }
  790. set_bit(assert->node_idx, dlm->domain_map);
  791. clear_bit(assert->node_idx, dlm->exit_domain_map);
  792. __dlm_set_joining_node(dlm, DLM_LOCK_RES_OWNER_UNKNOWN);
  793. printk(KERN_NOTICE "o2dlm: Node %u joins domain %s ",
  794. assert->node_idx, dlm->name);
  795. __dlm_print_nodes(dlm);
  796. /* notify anything attached to the heartbeat events */
  797. dlm_hb_event_notify_attached(dlm, assert->node_idx, 1);
  798. spin_unlock(&dlm->spinlock);
  799. }
  800. spin_unlock(&dlm_domain_lock);
  801. return 0;
  802. }
  803. static int dlm_match_regions(struct dlm_ctxt *dlm,
  804. struct dlm_query_region *qr,
  805. char *local, int locallen)
  806. {
  807. char *remote = qr->qr_regions;
  808. char *l, *r;
  809. int localnr, i, j, foundit;
  810. int status = 0;
  811. if (!o2hb_global_heartbeat_active()) {
  812. if (qr->qr_numregions) {
  813. mlog(ML_ERROR, "Domain %s: Joining node %d has global "
  814. "heartbeat enabled but local node %d does not\n",
  815. qr->qr_domain, qr->qr_node, dlm->node_num);
  816. status = -EINVAL;
  817. }
  818. goto bail;
  819. }
  820. if (o2hb_global_heartbeat_active() && !qr->qr_numregions) {
  821. mlog(ML_ERROR, "Domain %s: Local node %d has global "
  822. "heartbeat enabled but joining node %d does not\n",
  823. qr->qr_domain, dlm->node_num, qr->qr_node);
  824. status = -EINVAL;
  825. goto bail;
  826. }
  827. r = remote;
  828. for (i = 0; i < qr->qr_numregions; ++i) {
  829. mlog(0, "Region %.*s\n", O2HB_MAX_REGION_NAME_LEN, r);
  830. r += O2HB_MAX_REGION_NAME_LEN;
  831. }
  832. localnr = min(O2NM_MAX_REGIONS, locallen/O2HB_MAX_REGION_NAME_LEN);
  833. localnr = o2hb_get_all_regions(local, (u8)localnr);
  834. /* compare local regions with remote */
  835. l = local;
  836. for (i = 0; i < localnr; ++i) {
  837. foundit = 0;
  838. r = remote;
  839. for (j = 0; j <= qr->qr_numregions; ++j) {
  840. if (!memcmp(l, r, O2HB_MAX_REGION_NAME_LEN)) {
  841. foundit = 1;
  842. break;
  843. }
  844. r += O2HB_MAX_REGION_NAME_LEN;
  845. }
  846. if (!foundit) {
  847. status = -EINVAL;
  848. mlog(ML_ERROR, "Domain %s: Region '%.*s' registered "
  849. "in local node %d but not in joining node %d\n",
  850. qr->qr_domain, O2HB_MAX_REGION_NAME_LEN, l,
  851. dlm->node_num, qr->qr_node);
  852. goto bail;
  853. }
  854. l += O2HB_MAX_REGION_NAME_LEN;
  855. }
  856. /* compare remote with local regions */
  857. r = remote;
  858. for (i = 0; i < qr->qr_numregions; ++i) {
  859. foundit = 0;
  860. l = local;
  861. for (j = 0; j < localnr; ++j) {
  862. if (!memcmp(r, l, O2HB_MAX_REGION_NAME_LEN)) {
  863. foundit = 1;
  864. break;
  865. }
  866. l += O2HB_MAX_REGION_NAME_LEN;
  867. }
  868. if (!foundit) {
  869. status = -EINVAL;
  870. mlog(ML_ERROR, "Domain %s: Region '%.*s' registered "
  871. "in joining node %d but not in local node %d\n",
  872. qr->qr_domain, O2HB_MAX_REGION_NAME_LEN, r,
  873. qr->qr_node, dlm->node_num);
  874. goto bail;
  875. }
  876. r += O2HB_MAX_REGION_NAME_LEN;
  877. }
  878. bail:
  879. return status;
  880. }
  881. static int dlm_send_regions(struct dlm_ctxt *dlm, unsigned long *node_map)
  882. {
  883. struct dlm_query_region *qr = NULL;
  884. int status, ret = 0, i;
  885. char *p;
  886. if (find_next_bit(node_map, O2NM_MAX_NODES, 0) >= O2NM_MAX_NODES)
  887. goto bail;
  888. qr = kzalloc(sizeof(struct dlm_query_region), GFP_KERNEL);
  889. if (!qr) {
  890. ret = -ENOMEM;
  891. mlog_errno(ret);
  892. goto bail;
  893. }
  894. qr->qr_node = dlm->node_num;
  895. qr->qr_namelen = strlen(dlm->name);
  896. memcpy(qr->qr_domain, dlm->name, qr->qr_namelen);
  897. /* if local hb, the numregions will be zero */
  898. if (o2hb_global_heartbeat_active())
  899. qr->qr_numregions = o2hb_get_all_regions(qr->qr_regions,
  900. O2NM_MAX_REGIONS);
  901. p = qr->qr_regions;
  902. for (i = 0; i < qr->qr_numregions; ++i, p += O2HB_MAX_REGION_NAME_LEN)
  903. mlog(0, "Region %.*s\n", O2HB_MAX_REGION_NAME_LEN, p);
  904. i = -1;
  905. while ((i = find_next_bit(node_map, O2NM_MAX_NODES,
  906. i + 1)) < O2NM_MAX_NODES) {
  907. if (i == dlm->node_num)
  908. continue;
  909. mlog(0, "Sending regions to node %d\n", i);
  910. ret = o2net_send_message(DLM_QUERY_REGION, DLM_MOD_KEY, qr,
  911. sizeof(struct dlm_query_region),
  912. i, &status);
  913. if (ret >= 0)
  914. ret = status;
  915. if (ret) {
  916. mlog(ML_ERROR, "Region mismatch %d, node %d\n",
  917. ret, i);
  918. break;
  919. }
  920. }
  921. bail:
  922. kfree(qr);
  923. return ret;
  924. }
  925. static int dlm_query_region_handler(struct o2net_msg *msg, u32 len,
  926. void *data, void **ret_data)
  927. {
  928. struct dlm_query_region *qr;
  929. struct dlm_ctxt *dlm = NULL;
  930. char *local = NULL;
  931. int status = 0;
  932. qr = (struct dlm_query_region *) msg->buf;
  933. mlog(0, "Node %u queries hb regions on domain %s\n", qr->qr_node,
  934. qr->qr_domain);
  935. /* buffer used in dlm_mast_regions() */
  936. local = kmalloc(sizeof(qr->qr_regions), GFP_KERNEL);
  937. if (!local)
  938. return -ENOMEM;
  939. status = -EINVAL;
  940. spin_lock(&dlm_domain_lock);
  941. dlm = __dlm_lookup_domain_full(qr->qr_domain, qr->qr_namelen);
  942. if (!dlm) {
  943. mlog(ML_ERROR, "Node %d queried hb regions on domain %s "
  944. "before join domain\n", qr->qr_node, qr->qr_domain);
  945. goto out_domain_lock;
  946. }
  947. spin_lock(&dlm->spinlock);
  948. if (dlm->joining_node != qr->qr_node) {
  949. mlog(ML_ERROR, "Node %d queried hb regions on domain %s "
  950. "but joining node is %d\n", qr->qr_node, qr->qr_domain,
  951. dlm->joining_node);
  952. goto out_dlm_lock;
  953. }
  954. /* Support for global heartbeat was added in 1.1 */
  955. if (dlm->dlm_locking_proto.pv_major == 1 &&
  956. dlm->dlm_locking_proto.pv_minor == 0) {
  957. mlog(ML_ERROR, "Node %d queried hb regions on domain %s "
  958. "but active dlm protocol is %d.%d\n", qr->qr_node,
  959. qr->qr_domain, dlm->dlm_locking_proto.pv_major,
  960. dlm->dlm_locking_proto.pv_minor);
  961. goto out_dlm_lock;
  962. }
  963. status = dlm_match_regions(dlm, qr, local, sizeof(qr->qr_regions));
  964. out_dlm_lock:
  965. spin_unlock(&dlm->spinlock);
  966. out_domain_lock:
  967. spin_unlock(&dlm_domain_lock);
  968. kfree(local);
  969. return status;
  970. }
  971. static int dlm_match_nodes(struct dlm_ctxt *dlm, struct dlm_query_nodeinfo *qn)
  972. {
  973. struct o2nm_node *local;
  974. struct dlm_node_info *remote;
  975. int i, j;
  976. int status = 0;
  977. for (j = 0; j < qn->qn_numnodes; ++j)
  978. mlog(0, "Node %3d, %pI4:%u\n", qn->qn_nodes[j].ni_nodenum,
  979. &(qn->qn_nodes[j].ni_ipv4_address),
  980. ntohs(qn->qn_nodes[j].ni_ipv4_port));
  981. for (i = 0; i < O2NM_MAX_NODES && !status; ++i) {
  982. local = o2nm_get_node_by_num(i);
  983. remote = NULL;
  984. for (j = 0; j < qn->qn_numnodes; ++j) {
  985. if (qn->qn_nodes[j].ni_nodenum == i) {
  986. remote = &(qn->qn_nodes[j]);
  987. break;
  988. }
  989. }
  990. if (!local && !remote)
  991. continue;
  992. if ((local && !remote) || (!local && remote))
  993. status = -EINVAL;
  994. if (!status &&
  995. ((remote->ni_nodenum != local->nd_num) ||
  996. (remote->ni_ipv4_port != local->nd_ipv4_port) ||
  997. (remote->ni_ipv4_address != local->nd_ipv4_address)))
  998. status = -EINVAL;
  999. if (status) {
  1000. if (remote && !local)
  1001. mlog(ML_ERROR, "Domain %s: Node %d (%pI4:%u) "
  1002. "registered in joining node %d but not in "
  1003. "local node %d\n", qn->qn_domain,
  1004. remote->ni_nodenum,
  1005. &(remote->ni_ipv4_address),
  1006. ntohs(remote->ni_ipv4_port),
  1007. qn->qn_nodenum, dlm->node_num);
  1008. if (local && !remote)
  1009. mlog(ML_ERROR, "Domain %s: Node %d (%pI4:%u) "
  1010. "registered in local node %d but not in "
  1011. "joining node %d\n", qn->qn_domain,
  1012. local->nd_num, &(local->nd_ipv4_address),
  1013. ntohs(local->nd_ipv4_port),
  1014. dlm->node_num, qn->qn_nodenum);
  1015. BUG_ON((!local && !remote));
  1016. }
  1017. if (local)
  1018. o2nm_node_put(local);
  1019. }
  1020. return status;
  1021. }
  1022. static int dlm_send_nodeinfo(struct dlm_ctxt *dlm, unsigned long *node_map)
  1023. {
  1024. struct dlm_query_nodeinfo *qn = NULL;
  1025. struct o2nm_node *node;
  1026. int ret = 0, status, count, i;
  1027. if (find_next_bit(node_map, O2NM_MAX_NODES, 0) >= O2NM_MAX_NODES)
  1028. goto bail;
  1029. qn = kzalloc(sizeof(struct dlm_query_nodeinfo), GFP_KERNEL);
  1030. if (!qn) {
  1031. ret = -ENOMEM;
  1032. mlog_errno(ret);
  1033. goto bail;
  1034. }
  1035. for (i = 0, count = 0; i < O2NM_MAX_NODES; ++i) {
  1036. node = o2nm_get_node_by_num(i);
  1037. if (!node)
  1038. continue;
  1039. qn->qn_nodes[count].ni_nodenum = node->nd_num;
  1040. qn->qn_nodes[count].ni_ipv4_port = node->nd_ipv4_port;
  1041. qn->qn_nodes[count].ni_ipv4_address = node->nd_ipv4_address;
  1042. mlog(0, "Node %3d, %pI4:%u\n", node->nd_num,
  1043. &(node->nd_ipv4_address), ntohs(node->nd_ipv4_port));
  1044. ++count;
  1045. o2nm_node_put(node);
  1046. }
  1047. qn->qn_nodenum = dlm->node_num;
  1048. qn->qn_numnodes = count;
  1049. qn->qn_namelen = strlen(dlm->name);
  1050. memcpy(qn->qn_domain, dlm->name, qn->qn_namelen);
  1051. i = -1;
  1052. while ((i = find_next_bit(node_map, O2NM_MAX_NODES,
  1053. i + 1)) < O2NM_MAX_NODES) {
  1054. if (i == dlm->node_num)
  1055. continue;
  1056. mlog(0, "Sending nodeinfo to node %d\n", i);
  1057. ret = o2net_send_message(DLM_QUERY_NODEINFO, DLM_MOD_KEY,
  1058. qn, sizeof(struct dlm_query_nodeinfo),
  1059. i, &status);
  1060. if (ret >= 0)
  1061. ret = status;
  1062. if (ret) {
  1063. mlog(ML_ERROR, "node mismatch %d, node %d\n", ret, i);
  1064. break;
  1065. }
  1066. }
  1067. bail:
  1068. kfree(qn);
  1069. return ret;
  1070. }
  1071. static int dlm_query_nodeinfo_handler(struct o2net_msg *msg, u32 len,
  1072. void *data, void **ret_data)
  1073. {
  1074. struct dlm_query_nodeinfo *qn;
  1075. struct dlm_ctxt *dlm = NULL;
  1076. int locked = 0, status = -EINVAL;
  1077. qn = (struct dlm_query_nodeinfo *) msg->buf;
  1078. mlog(0, "Node %u queries nodes on domain %s\n", qn->qn_nodenum,
  1079. qn->qn_domain);
  1080. spin_lock(&dlm_domain_lock);
  1081. dlm = __dlm_lookup_domain_full(qn->qn_domain, qn->qn_namelen);
  1082. if (!dlm) {
  1083. mlog(ML_ERROR, "Node %d queried nodes on domain %s before "
  1084. "join domain\n", qn->qn_nodenum, qn->qn_domain);
  1085. goto bail;
  1086. }
  1087. spin_lock(&dlm->spinlock);
  1088. locked = 1;
  1089. if (dlm->joining_node != qn->qn_nodenum) {
  1090. mlog(ML_ERROR, "Node %d queried nodes on domain %s but "
  1091. "joining node is %d\n", qn->qn_nodenum, qn->qn_domain,
  1092. dlm->joining_node);
  1093. goto bail;
  1094. }
  1095. /* Support for node query was added in 1.1 */
  1096. if (dlm->dlm_locking_proto.pv_major == 1 &&
  1097. dlm->dlm_locking_proto.pv_minor == 0) {
  1098. mlog(ML_ERROR, "Node %d queried nodes on domain %s "
  1099. "but active dlm protocol is %d.%d\n", qn->qn_nodenum,
  1100. qn->qn_domain, dlm->dlm_locking_proto.pv_major,
  1101. dlm->dlm_locking_proto.pv_minor);
  1102. goto bail;
  1103. }
  1104. status = dlm_match_nodes(dlm, qn);
  1105. bail:
  1106. if (locked)
  1107. spin_unlock(&dlm->spinlock);
  1108. spin_unlock(&dlm_domain_lock);
  1109. return status;
  1110. }
  1111. static int dlm_cancel_join_handler(struct o2net_msg *msg, u32 len, void *data,
  1112. void **ret_data)
  1113. {
  1114. struct dlm_cancel_join *cancel;
  1115. struct dlm_ctxt *dlm = NULL;
  1116. cancel = (struct dlm_cancel_join *) msg->buf;
  1117. mlog(0, "node %u cancels join on domain %s\n", cancel->node_idx,
  1118. cancel->domain);
  1119. spin_lock(&dlm_domain_lock);
  1120. dlm = __dlm_lookup_domain_full(cancel->domain, cancel->name_len);
  1121. if (dlm) {
  1122. spin_lock(&dlm->spinlock);
  1123. /* Yikes, this guy wants to cancel his join. No
  1124. * problem, we simply cleanup our join state. */
  1125. BUG_ON(dlm->joining_node != cancel->node_idx);
  1126. __dlm_set_joining_node(dlm, DLM_LOCK_RES_OWNER_UNKNOWN);
  1127. spin_unlock(&dlm->spinlock);
  1128. }
  1129. spin_unlock(&dlm_domain_lock);
  1130. return 0;
  1131. }
  1132. static int dlm_send_one_join_cancel(struct dlm_ctxt *dlm,
  1133. unsigned int node)
  1134. {
  1135. int status;
  1136. struct dlm_cancel_join cancel_msg;
  1137. memset(&cancel_msg, 0, sizeof(cancel_msg));
  1138. cancel_msg.node_idx = dlm->node_num;
  1139. cancel_msg.name_len = strlen(dlm->name);
  1140. memcpy(cancel_msg.domain, dlm->name, cancel_msg.name_len);
  1141. status = o2net_send_message(DLM_CANCEL_JOIN_MSG, DLM_MOD_KEY,
  1142. &cancel_msg, sizeof(cancel_msg), node,
  1143. NULL);
  1144. if (status < 0) {
  1145. mlog(ML_ERROR, "Error %d when sending message %u (key 0x%x) to "
  1146. "node %u\n", status, DLM_CANCEL_JOIN_MSG, DLM_MOD_KEY,
  1147. node);
  1148. goto bail;
  1149. }
  1150. bail:
  1151. return status;
  1152. }
  1153. /* map_size should be in bytes. */
  1154. static int dlm_send_join_cancels(struct dlm_ctxt *dlm,
  1155. unsigned long *node_map,
  1156. unsigned int map_size)
  1157. {
  1158. int status, tmpstat;
  1159. int node;
  1160. if (map_size != (BITS_TO_LONGS(O2NM_MAX_NODES) *
  1161. sizeof(unsigned long))) {
  1162. mlog(ML_ERROR,
  1163. "map_size %u != BITS_TO_LONGS(O2NM_MAX_NODES) %u\n",
  1164. map_size, (unsigned)BITS_TO_LONGS(O2NM_MAX_NODES));
  1165. return -EINVAL;
  1166. }
  1167. status = 0;
  1168. node = -1;
  1169. while ((node = find_next_bit(node_map, O2NM_MAX_NODES,
  1170. node + 1)) < O2NM_MAX_NODES) {
  1171. if (node == dlm->node_num)
  1172. continue;
  1173. tmpstat = dlm_send_one_join_cancel(dlm, node);
  1174. if (tmpstat) {
  1175. mlog(ML_ERROR, "Error return %d cancelling join on "
  1176. "node %d\n", tmpstat, node);
  1177. if (!status)
  1178. status = tmpstat;
  1179. }
  1180. }
  1181. if (status)
  1182. mlog_errno(status);
  1183. return status;
  1184. }
  1185. static int dlm_request_join(struct dlm_ctxt *dlm,
  1186. int node,
  1187. enum dlm_query_join_response_code *response)
  1188. {
  1189. int status;
  1190. struct dlm_query_join_request join_msg;
  1191. struct dlm_query_join_packet packet;
  1192. u32 join_resp;
  1193. mlog(0, "querying node %d\n", node);
  1194. memset(&join_msg, 0, sizeof(join_msg));
  1195. join_msg.node_idx = dlm->node_num;
  1196. join_msg.name_len = strlen(dlm->name);
  1197. memcpy(join_msg.domain, dlm->name, join_msg.name_len);
  1198. join_msg.dlm_proto = dlm->dlm_locking_proto;
  1199. join_msg.fs_proto = dlm->fs_locking_proto;
  1200. /* copy live node map to join message */
  1201. byte_copymap(join_msg.node_map, dlm->live_nodes_map, O2NM_MAX_NODES);
  1202. status = o2net_send_message(DLM_QUERY_JOIN_MSG, DLM_MOD_KEY, &join_msg,
  1203. sizeof(join_msg), node, &join_resp);
  1204. if (status < 0 && status != -ENOPROTOOPT) {
  1205. mlog(ML_ERROR, "Error %d when sending message %u (key 0x%x) to "
  1206. "node %u\n", status, DLM_QUERY_JOIN_MSG, DLM_MOD_KEY,
  1207. node);
  1208. goto bail;
  1209. }
  1210. dlm_query_join_wire_to_packet(join_resp, &packet);
  1211. /* -ENOPROTOOPT from the net code means the other side isn't
  1212. listening for our message type -- that's fine, it means
  1213. his dlm isn't up, so we can consider him a 'yes' but not
  1214. joined into the domain. */
  1215. if (status == -ENOPROTOOPT) {
  1216. status = 0;
  1217. *response = JOIN_OK_NO_MAP;
  1218. } else {
  1219. *response = packet.code;
  1220. switch (packet.code) {
  1221. case JOIN_DISALLOW:
  1222. case JOIN_OK_NO_MAP:
  1223. break;
  1224. case JOIN_PROTOCOL_MISMATCH:
  1225. mlog(ML_NOTICE,
  1226. "This node requested DLM locking protocol %u.%u and "
  1227. "filesystem locking protocol %u.%u. At least one of "
  1228. "the protocol versions on node %d is not compatible, "
  1229. "disconnecting\n",
  1230. dlm->dlm_locking_proto.pv_major,
  1231. dlm->dlm_locking_proto.pv_minor,
  1232. dlm->fs_locking_proto.pv_major,
  1233. dlm->fs_locking_proto.pv_minor,
  1234. node);
  1235. status = -EPROTO;
  1236. break;
  1237. case JOIN_OK:
  1238. /* Use the same locking protocol as the remote node */
  1239. dlm->dlm_locking_proto.pv_minor = packet.dlm_minor;
  1240. dlm->fs_locking_proto.pv_minor = packet.fs_minor;
  1241. mlog(0,
  1242. "Node %d responds JOIN_OK with DLM locking protocol "
  1243. "%u.%u and fs locking protocol %u.%u\n",
  1244. node,
  1245. dlm->dlm_locking_proto.pv_major,
  1246. dlm->dlm_locking_proto.pv_minor,
  1247. dlm->fs_locking_proto.pv_major,
  1248. dlm->fs_locking_proto.pv_minor);
  1249. break;
  1250. default:
  1251. status = -EINVAL;
  1252. mlog(ML_ERROR, "invalid response %d from node %u\n",
  1253. packet.code, node);
  1254. /* Reset response to JOIN_DISALLOW */
  1255. *response = JOIN_DISALLOW;
  1256. break;
  1257. }
  1258. }
  1259. mlog(0, "status %d, node %d response is %d\n", status, node,
  1260. *response);
  1261. bail:
  1262. return status;
  1263. }
  1264. static int dlm_send_one_join_assert(struct dlm_ctxt *dlm,
  1265. unsigned int node)
  1266. {
  1267. int status;
  1268. int ret;
  1269. struct dlm_assert_joined assert_msg;
  1270. mlog(0, "Sending join assert to node %u\n", node);
  1271. memset(&assert_msg, 0, sizeof(assert_msg));
  1272. assert_msg.node_idx = dlm->node_num;
  1273. assert_msg.name_len = strlen(dlm->name);
  1274. memcpy(assert_msg.domain, dlm->name, assert_msg.name_len);
  1275. status = o2net_send_message(DLM_ASSERT_JOINED_MSG, DLM_MOD_KEY,
  1276. &assert_msg, sizeof(assert_msg), node,
  1277. &ret);
  1278. if (status < 0)
  1279. mlog(ML_ERROR, "Error %d when sending message %u (key 0x%x) to "
  1280. "node %u\n", status, DLM_ASSERT_JOINED_MSG, DLM_MOD_KEY,
  1281. node);
  1282. else
  1283. status = ret;
  1284. return status;
  1285. }
  1286. static void dlm_send_join_asserts(struct dlm_ctxt *dlm,
  1287. unsigned long *node_map)
  1288. {
  1289. int status, node, live;
  1290. status = 0;
  1291. node = -1;
  1292. while ((node = find_next_bit(node_map, O2NM_MAX_NODES,
  1293. node + 1)) < O2NM_MAX_NODES) {
  1294. if (node == dlm->node_num)
  1295. continue;
  1296. do {
  1297. /* It is very important that this message be
  1298. * received so we spin until either the node
  1299. * has died or it gets the message. */
  1300. status = dlm_send_one_join_assert(dlm, node);
  1301. spin_lock(&dlm->spinlock);
  1302. live = test_bit(node, dlm->live_nodes_map);
  1303. spin_unlock(&dlm->spinlock);
  1304. if (status) {
  1305. mlog(ML_ERROR, "Error return %d asserting "
  1306. "join on node %d\n", status, node);
  1307. /* give us some time between errors... */
  1308. if (live)
  1309. msleep(DLM_DOMAIN_BACKOFF_MS);
  1310. }
  1311. } while (status && live);
  1312. }
  1313. }
  1314. struct domain_join_ctxt {
  1315. unsigned long live_map[BITS_TO_LONGS(O2NM_MAX_NODES)];
  1316. unsigned long yes_resp_map[BITS_TO_LONGS(O2NM_MAX_NODES)];
  1317. };
  1318. static int dlm_should_restart_join(struct dlm_ctxt *dlm,
  1319. struct domain_join_ctxt *ctxt,
  1320. enum dlm_query_join_response_code response)
  1321. {
  1322. int ret;
  1323. if (response == JOIN_DISALLOW) {
  1324. mlog(0, "Latest response of disallow -- should restart\n");
  1325. return 1;
  1326. }
  1327. spin_lock(&dlm->spinlock);
  1328. /* For now, we restart the process if the node maps have
  1329. * changed at all */
  1330. ret = memcmp(ctxt->live_map, dlm->live_nodes_map,
  1331. sizeof(dlm->live_nodes_map));
  1332. spin_unlock(&dlm->spinlock);
  1333. if (ret)
  1334. mlog(0, "Node maps changed -- should restart\n");
  1335. return ret;
  1336. }
  1337. static int dlm_try_to_join_domain(struct dlm_ctxt *dlm)
  1338. {
  1339. int status = 0, tmpstat, node;
  1340. struct domain_join_ctxt *ctxt;
  1341. enum dlm_query_join_response_code response = JOIN_DISALLOW;
  1342. mlog(0, "%p", dlm);
  1343. ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
  1344. if (!ctxt) {
  1345. status = -ENOMEM;
  1346. mlog_errno(status);
  1347. goto bail;
  1348. }
  1349. /* group sem locking should work for us here -- we're already
  1350. * registered for heartbeat events so filling this should be
  1351. * atomic wrt getting those handlers called. */
  1352. o2hb_fill_node_map(dlm->live_nodes_map, sizeof(dlm->live_nodes_map));
  1353. spin_lock(&dlm->spinlock);
  1354. memcpy(ctxt->live_map, dlm->live_nodes_map, sizeof(ctxt->live_map));
  1355. __dlm_set_joining_node(dlm, dlm->node_num);
  1356. spin_unlock(&dlm->spinlock);
  1357. node = -1;
  1358. while ((node = find_next_bit(ctxt->live_map, O2NM_MAX_NODES,
  1359. node + 1)) < O2NM_MAX_NODES) {
  1360. if (node == dlm->node_num)
  1361. continue;
  1362. status = dlm_request_join(dlm, node, &response);
  1363. if (status < 0) {
  1364. mlog_errno(status);
  1365. goto bail;
  1366. }
  1367. /* Ok, either we got a response or the node doesn't have a
  1368. * dlm up. */
  1369. if (response == JOIN_OK)
  1370. set_bit(node, ctxt->yes_resp_map);
  1371. if (dlm_should_restart_join(dlm, ctxt, response)) {
  1372. status = -EAGAIN;
  1373. goto bail;
  1374. }
  1375. }
  1376. mlog(0, "Yay, done querying nodes!\n");
  1377. /* Yay, everyone agree's we can join the domain. My domain is
  1378. * comprised of all nodes who were put in the
  1379. * yes_resp_map. Copy that into our domain map and send a join
  1380. * assert message to clean up everyone elses state. */
  1381. spin_lock(&dlm->spinlock);
  1382. memcpy(dlm->domain_map, ctxt->yes_resp_map,
  1383. sizeof(ctxt->yes_resp_map));
  1384. set_bit(dlm->node_num, dlm->domain_map);
  1385. spin_unlock(&dlm->spinlock);
  1386. /* Support for global heartbeat and node info was added in 1.1 */
  1387. if (dlm->dlm_locking_proto.pv_major > 1 ||
  1388. dlm->dlm_locking_proto.pv_minor > 0) {
  1389. status = dlm_send_nodeinfo(dlm, ctxt->yes_resp_map);
  1390. if (status) {
  1391. mlog_errno(status);
  1392. goto bail;
  1393. }
  1394. status = dlm_send_regions(dlm, ctxt->yes_resp_map);
  1395. if (status) {
  1396. mlog_errno(status);
  1397. goto bail;
  1398. }
  1399. }
  1400. dlm_send_join_asserts(dlm, ctxt->yes_resp_map);
  1401. /* Joined state *must* be set before the joining node
  1402. * information, otherwise the query_join handler may read no
  1403. * current joiner but a state of NEW and tell joining nodes
  1404. * we're not in the domain. */
  1405. spin_lock(&dlm_domain_lock);
  1406. dlm->dlm_state = DLM_CTXT_JOINED;
  1407. dlm->num_joins++;
  1408. spin_unlock(&dlm_domain_lock);
  1409. bail:
  1410. spin_lock(&dlm->spinlock);
  1411. __dlm_set_joining_node(dlm, DLM_LOCK_RES_OWNER_UNKNOWN);
  1412. if (!status) {
  1413. printk(KERN_NOTICE "o2dlm: Joining domain %s ", dlm->name);
  1414. __dlm_print_nodes(dlm);
  1415. }
  1416. spin_unlock(&dlm->spinlock);
  1417. if (ctxt) {
  1418. /* Do we need to send a cancel message to any nodes? */
  1419. if (status < 0) {
  1420. tmpstat = dlm_send_join_cancels(dlm,
  1421. ctxt->yes_resp_map,
  1422. sizeof(ctxt->yes_resp_map));
  1423. if (tmpstat < 0)
  1424. mlog_errno(tmpstat);
  1425. }
  1426. kfree(ctxt);
  1427. }
  1428. mlog(0, "returning %d\n", status);
  1429. return status;
  1430. }
  1431. static void dlm_unregister_domain_handlers(struct dlm_ctxt *dlm)
  1432. {
  1433. o2hb_unregister_callback(dlm->name, &dlm->dlm_hb_up);
  1434. o2hb_unregister_callback(dlm->name, &dlm->dlm_hb_down);
  1435. o2net_unregister_handler_list(&dlm->dlm_domain_handlers);
  1436. }
  1437. static int dlm_register_domain_handlers(struct dlm_ctxt *dlm)
  1438. {
  1439. int status;
  1440. mlog(0, "registering handlers.\n");
  1441. o2hb_setup_callback(&dlm->dlm_hb_down, O2HB_NODE_DOWN_CB,
  1442. dlm_hb_node_down_cb, dlm, DLM_HB_NODE_DOWN_PRI);
  1443. o2hb_setup_callback(&dlm->dlm_hb_up, O2HB_NODE_UP_CB,
  1444. dlm_hb_node_up_cb, dlm, DLM_HB_NODE_UP_PRI);
  1445. status = o2hb_register_callback(dlm->name, &dlm->dlm_hb_down);
  1446. if (status)
  1447. goto bail;
  1448. status = o2hb_register_callback(dlm->name, &dlm->dlm_hb_up);
  1449. if (status)
  1450. goto bail;
  1451. status = o2net_register_handler(DLM_MASTER_REQUEST_MSG, dlm->key,
  1452. sizeof(struct dlm_master_request),
  1453. dlm_master_request_handler,
  1454. dlm, NULL, &dlm->dlm_domain_handlers);
  1455. if (status)
  1456. goto bail;
  1457. status = o2net_register_handler(DLM_ASSERT_MASTER_MSG, dlm->key,
  1458. sizeof(struct dlm_assert_master),
  1459. dlm_assert_master_handler,
  1460. dlm, dlm_assert_master_post_handler,
  1461. &dlm->dlm_domain_handlers);
  1462. if (status)
  1463. goto bail;
  1464. status = o2net_register_handler(DLM_CREATE_LOCK_MSG, dlm->key,
  1465. sizeof(struct dlm_create_lock),
  1466. dlm_create_lock_handler,
  1467. dlm, NULL, &dlm->dlm_domain_handlers);
  1468. if (status)
  1469. goto bail;
  1470. status = o2net_register_handler(DLM_CONVERT_LOCK_MSG, dlm->key,
  1471. DLM_CONVERT_LOCK_MAX_LEN,
  1472. dlm_convert_lock_handler,
  1473. dlm, NULL, &dlm->dlm_domain_handlers);
  1474. if (status)
  1475. goto bail;
  1476. status = o2net_register_handler(DLM_UNLOCK_LOCK_MSG, dlm->key,
  1477. DLM_UNLOCK_LOCK_MAX_LEN,
  1478. dlm_unlock_lock_handler,
  1479. dlm, NULL, &dlm->dlm_domain_handlers);
  1480. if (status)
  1481. goto bail;
  1482. status = o2net_register_handler(DLM_PROXY_AST_MSG, dlm->key,
  1483. DLM_PROXY_AST_MAX_LEN,
  1484. dlm_proxy_ast_handler,
  1485. dlm, NULL, &dlm->dlm_domain_handlers);
  1486. if (status)
  1487. goto bail;
  1488. status = o2net_register_handler(DLM_EXIT_DOMAIN_MSG, dlm->key,
  1489. sizeof(struct dlm_exit_domain),
  1490. dlm_exit_domain_handler,
  1491. dlm, NULL, &dlm->dlm_domain_handlers);
  1492. if (status)
  1493. goto bail;
  1494. status = o2net_register_handler(DLM_DEREF_LOCKRES_MSG, dlm->key,
  1495. sizeof(struct dlm_deref_lockres),
  1496. dlm_deref_lockres_handler,
  1497. dlm, NULL, &dlm->dlm_domain_handlers);
  1498. if (status)
  1499. goto bail;
  1500. status = o2net_register_handler(DLM_MIGRATE_REQUEST_MSG, dlm->key,
  1501. sizeof(struct dlm_migrate_request),
  1502. dlm_migrate_request_handler,
  1503. dlm, NULL, &dlm->dlm_domain_handlers);
  1504. if (status)
  1505. goto bail;
  1506. status = o2net_register_handler(DLM_MIG_LOCKRES_MSG, dlm->key,
  1507. DLM_MIG_LOCKRES_MAX_LEN,
  1508. dlm_mig_lockres_handler,
  1509. dlm, NULL, &dlm->dlm_domain_handlers);
  1510. if (status)
  1511. goto bail;
  1512. status = o2net_register_handler(DLM_MASTER_REQUERY_MSG, dlm->key,
  1513. sizeof(struct dlm_master_requery),
  1514. dlm_master_requery_handler,
  1515. dlm, NULL, &dlm->dlm_domain_handlers);
  1516. if (status)
  1517. goto bail;
  1518. status = o2net_register_handler(DLM_LOCK_REQUEST_MSG, dlm->key,
  1519. sizeof(struct dlm_lock_request),
  1520. dlm_request_all_locks_handler,
  1521. dlm, NULL, &dlm->dlm_domain_handlers);
  1522. if (status)
  1523. goto bail;
  1524. status = o2net_register_handler(DLM_RECO_DATA_DONE_MSG, dlm->key,
  1525. sizeof(struct dlm_reco_data_done),
  1526. dlm_reco_data_done_handler,
  1527. dlm, NULL, &dlm->dlm_domain_handlers);
  1528. if (status)
  1529. goto bail;
  1530. status = o2net_register_handler(DLM_BEGIN_RECO_MSG, dlm->key,
  1531. sizeof(struct dlm_begin_reco),
  1532. dlm_begin_reco_handler,
  1533. dlm, NULL, &dlm->dlm_domain_handlers);
  1534. if (status)
  1535. goto bail;
  1536. status = o2net_register_handler(DLM_FINALIZE_RECO_MSG, dlm->key,
  1537. sizeof(struct dlm_finalize_reco),
  1538. dlm_finalize_reco_handler,
  1539. dlm, NULL, &dlm->dlm_domain_handlers);
  1540. if (status)
  1541. goto bail;
  1542. status = o2net_register_handler(DLM_BEGIN_EXIT_DOMAIN_MSG, dlm->key,
  1543. sizeof(struct dlm_exit_domain),
  1544. dlm_begin_exit_domain_handler,
  1545. dlm, NULL, &dlm->dlm_domain_handlers);
  1546. if (status)
  1547. goto bail;
  1548. status = o2net_register_handler(DLM_DEREF_LOCKRES_DONE, dlm->key,
  1549. sizeof(struct dlm_deref_lockres_done),
  1550. dlm_deref_lockres_done_handler,
  1551. dlm, NULL, &dlm->dlm_domain_handlers);
  1552. bail:
  1553. if (status)
  1554. dlm_unregister_domain_handlers(dlm);
  1555. return status;
  1556. }
  1557. static int dlm_join_domain(struct dlm_ctxt *dlm)
  1558. {
  1559. int status;
  1560. unsigned int backoff;
  1561. unsigned int total_backoff = 0;
  1562. char wq_name[O2NM_MAX_NAME_LEN];
  1563. BUG_ON(!dlm);
  1564. mlog(0, "Join domain %s\n", dlm->name);
  1565. status = dlm_register_domain_handlers(dlm);
  1566. if (status) {
  1567. mlog_errno(status);
  1568. goto bail;
  1569. }
  1570. status = dlm_launch_thread(dlm);
  1571. if (status < 0) {
  1572. mlog_errno(status);
  1573. goto bail;
  1574. }
  1575. status = dlm_launch_recovery_thread(dlm);
  1576. if (status < 0) {
  1577. mlog_errno(status);
  1578. goto bail;
  1579. }
  1580. status = dlm_debug_init(dlm);
  1581. if (status < 0) {
  1582. mlog_errno(status);
  1583. goto bail;
  1584. }
  1585. snprintf(wq_name, O2NM_MAX_NAME_LEN, "dlm_wq-%s", dlm->name);
  1586. dlm->dlm_worker = alloc_workqueue(wq_name, WQ_MEM_RECLAIM, 0);
  1587. if (!dlm->dlm_worker) {
  1588. status = -ENOMEM;
  1589. mlog_errno(status);
  1590. goto bail;
  1591. }
  1592. do {
  1593. status = dlm_try_to_join_domain(dlm);
  1594. /* If we're racing another node to the join, then we
  1595. * need to back off temporarily and let them
  1596. * complete. */
  1597. #define DLM_JOIN_TIMEOUT_MSECS 90000
  1598. if (status == -EAGAIN) {
  1599. if (signal_pending(current)) {
  1600. status = -ERESTARTSYS;
  1601. goto bail;
  1602. }
  1603. if (total_backoff > DLM_JOIN_TIMEOUT_MSECS) {
  1604. status = -ERESTARTSYS;
  1605. mlog(ML_NOTICE, "Timed out joining dlm domain "
  1606. "%s after %u msecs\n", dlm->name,
  1607. total_backoff);
  1608. goto bail;
  1609. }
  1610. /*
  1611. * <chip> After you!
  1612. * <dale> No, after you!
  1613. * <chip> I insist!
  1614. * <dale> But you first!
  1615. * ...
  1616. */
  1617. backoff = (unsigned int)(jiffies & 0x3);
  1618. backoff *= DLM_DOMAIN_BACKOFF_MS;
  1619. total_backoff += backoff;
  1620. mlog(0, "backoff %d\n", backoff);
  1621. msleep(backoff);
  1622. }
  1623. } while (status == -EAGAIN);
  1624. if (status < 0) {
  1625. mlog_errno(status);
  1626. goto bail;
  1627. }
  1628. status = 0;
  1629. bail:
  1630. wake_up(&dlm_domain_events);
  1631. if (status) {
  1632. dlm_unregister_domain_handlers(dlm);
  1633. dlm_debug_shutdown(dlm);
  1634. dlm_complete_thread(dlm);
  1635. dlm_complete_recovery_thread(dlm);
  1636. dlm_destroy_dlm_worker(dlm);
  1637. }
  1638. return status;
  1639. }
  1640. static struct dlm_ctxt *dlm_alloc_ctxt(const char *domain,
  1641. u32 key)
  1642. {
  1643. int i;
  1644. int ret;
  1645. struct dlm_ctxt *dlm = NULL;
  1646. dlm = kzalloc(sizeof(*dlm), GFP_KERNEL);
  1647. if (!dlm) {
  1648. ret = -ENOMEM;
  1649. mlog_errno(ret);
  1650. goto leave;
  1651. }
  1652. dlm->name = kstrdup(domain, GFP_KERNEL);
  1653. if (dlm->name == NULL) {
  1654. ret = -ENOMEM;
  1655. mlog_errno(ret);
  1656. goto leave;
  1657. }
  1658. dlm->lockres_hash = (struct hlist_head **)dlm_alloc_pagevec(DLM_HASH_PAGES);
  1659. if (!dlm->lockres_hash) {
  1660. ret = -ENOMEM;
  1661. mlog_errno(ret);
  1662. goto leave;
  1663. }
  1664. for (i = 0; i < DLM_HASH_BUCKETS; i++)
  1665. INIT_HLIST_HEAD(dlm_lockres_hash(dlm, i));
  1666. dlm->master_hash = (struct hlist_head **)
  1667. dlm_alloc_pagevec(DLM_HASH_PAGES);
  1668. if (!dlm->master_hash) {
  1669. ret = -ENOMEM;
  1670. mlog_errno(ret);
  1671. goto leave;
  1672. }
  1673. for (i = 0; i < DLM_HASH_BUCKETS; i++)
  1674. INIT_HLIST_HEAD(dlm_master_hash(dlm, i));
  1675. dlm->key = key;
  1676. dlm->node_num = o2nm_this_node();
  1677. ret = dlm_create_debugfs_subroot(dlm);
  1678. if (ret < 0)
  1679. goto leave;
  1680. spin_lock_init(&dlm->spinlock);
  1681. spin_lock_init(&dlm->master_lock);
  1682. spin_lock_init(&dlm->ast_lock);
  1683. spin_lock_init(&dlm->track_lock);
  1684. INIT_LIST_HEAD(&dlm->list);
  1685. INIT_LIST_HEAD(&dlm->dirty_list);
  1686. INIT_LIST_HEAD(&dlm->reco.resources);
  1687. INIT_LIST_HEAD(&dlm->reco.node_data);
  1688. INIT_LIST_HEAD(&dlm->purge_list);
  1689. INIT_LIST_HEAD(&dlm->dlm_domain_handlers);
  1690. INIT_LIST_HEAD(&dlm->tracking_list);
  1691. dlm->reco.state = 0;
  1692. INIT_LIST_HEAD(&dlm->pending_asts);
  1693. INIT_LIST_HEAD(&dlm->pending_basts);
  1694. mlog(0, "dlm->recovery_map=%p, &(dlm->recovery_map[0])=%p\n",
  1695. dlm->recovery_map, &(dlm->recovery_map[0]));
  1696. memset(dlm->recovery_map, 0, sizeof(dlm->recovery_map));
  1697. memset(dlm->live_nodes_map, 0, sizeof(dlm->live_nodes_map));
  1698. memset(dlm->domain_map, 0, sizeof(dlm->domain_map));
  1699. dlm->dlm_thread_task = NULL;
  1700. dlm->dlm_reco_thread_task = NULL;
  1701. dlm->dlm_worker = NULL;
  1702. init_waitqueue_head(&dlm->dlm_thread_wq);
  1703. init_waitqueue_head(&dlm->dlm_reco_thread_wq);
  1704. init_waitqueue_head(&dlm->reco.event);
  1705. init_waitqueue_head(&dlm->ast_wq);
  1706. init_waitqueue_head(&dlm->migration_wq);
  1707. INIT_LIST_HEAD(&dlm->mle_hb_events);
  1708. dlm->joining_node = DLM_LOCK_RES_OWNER_UNKNOWN;
  1709. init_waitqueue_head(&dlm->dlm_join_events);
  1710. dlm->reco.new_master = O2NM_INVALID_NODE_NUM;
  1711. dlm->reco.dead_node = O2NM_INVALID_NODE_NUM;
  1712. atomic_set(&dlm->res_tot_count, 0);
  1713. atomic_set(&dlm->res_cur_count, 0);
  1714. for (i = 0; i < DLM_MLE_NUM_TYPES; ++i) {
  1715. atomic_set(&dlm->mle_tot_count[i], 0);
  1716. atomic_set(&dlm->mle_cur_count[i], 0);
  1717. }
  1718. spin_lock_init(&dlm->work_lock);
  1719. INIT_LIST_HEAD(&dlm->work_list);
  1720. INIT_WORK(&dlm->dispatched_work, dlm_dispatch_work);
  1721. kref_init(&dlm->dlm_refs);
  1722. dlm->dlm_state = DLM_CTXT_NEW;
  1723. INIT_LIST_HEAD(&dlm->dlm_eviction_callbacks);
  1724. mlog(0, "context init: refcount %u\n",
  1725. atomic_read(&dlm->dlm_refs.refcount));
  1726. leave:
  1727. if (ret < 0 && dlm) {
  1728. if (dlm->master_hash)
  1729. dlm_free_pagevec((void **)dlm->master_hash,
  1730. DLM_HASH_PAGES);
  1731. if (dlm->lockres_hash)
  1732. dlm_free_pagevec((void **)dlm->lockres_hash,
  1733. DLM_HASH_PAGES);
  1734. kfree(dlm->name);
  1735. kfree(dlm);
  1736. dlm = NULL;
  1737. }
  1738. return dlm;
  1739. }
  1740. /*
  1741. * Compare a requested locking protocol version against the current one.
  1742. *
  1743. * If the major numbers are different, they are incompatible.
  1744. * If the current minor is greater than the request, they are incompatible.
  1745. * If the current minor is less than or equal to the request, they are
  1746. * compatible, and the requester should run at the current minor version.
  1747. */
  1748. static int dlm_protocol_compare(struct dlm_protocol_version *existing,
  1749. struct dlm_protocol_version *request)
  1750. {
  1751. if (existing->pv_major != request->pv_major)
  1752. return 1;
  1753. if (existing->pv_minor > request->pv_minor)
  1754. return 1;
  1755. if (existing->pv_minor < request->pv_minor)
  1756. request->pv_minor = existing->pv_minor;
  1757. return 0;
  1758. }
  1759. /*
  1760. * dlm_register_domain: one-time setup per "domain".
  1761. *
  1762. * The filesystem passes in the requested locking version via proto.
  1763. * If registration was successful, proto will contain the negotiated
  1764. * locking protocol.
  1765. */
  1766. struct dlm_ctxt * dlm_register_domain(const char *domain,
  1767. u32 key,
  1768. struct dlm_protocol_version *fs_proto)
  1769. {
  1770. int ret;
  1771. struct dlm_ctxt *dlm = NULL;
  1772. struct dlm_ctxt *new_ctxt = NULL;
  1773. if (strlen(domain) >= O2NM_MAX_NAME_LEN) {
  1774. ret = -ENAMETOOLONG;
  1775. mlog(ML_ERROR, "domain name length too long\n");
  1776. goto leave;
  1777. }
  1778. mlog(0, "register called for domain \"%s\"\n", domain);
  1779. retry:
  1780. dlm = NULL;
  1781. if (signal_pending(current)) {
  1782. ret = -ERESTARTSYS;
  1783. mlog_errno(ret);
  1784. goto leave;
  1785. }
  1786. spin_lock(&dlm_domain_lock);
  1787. dlm = __dlm_lookup_domain(domain);
  1788. if (dlm) {
  1789. if (dlm->dlm_state != DLM_CTXT_JOINED) {
  1790. spin_unlock(&dlm_domain_lock);
  1791. mlog(0, "This ctxt is not joined yet!\n");
  1792. wait_event_interruptible(dlm_domain_events,
  1793. dlm_wait_on_domain_helper(
  1794. domain));
  1795. goto retry;
  1796. }
  1797. if (dlm_protocol_compare(&dlm->fs_locking_proto, fs_proto)) {
  1798. spin_unlock(&dlm_domain_lock);
  1799. mlog(ML_ERROR,
  1800. "Requested locking protocol version is not "
  1801. "compatible with already registered domain "
  1802. "\"%s\"\n", domain);
  1803. ret = -EPROTO;
  1804. goto leave;
  1805. }
  1806. __dlm_get(dlm);
  1807. dlm->num_joins++;
  1808. spin_unlock(&dlm_domain_lock);
  1809. ret = 0;
  1810. goto leave;
  1811. }
  1812. /* doesn't exist */
  1813. if (!new_ctxt) {
  1814. spin_unlock(&dlm_domain_lock);
  1815. new_ctxt = dlm_alloc_ctxt(domain, key);
  1816. if (new_ctxt)
  1817. goto retry;
  1818. ret = -ENOMEM;
  1819. mlog_errno(ret);
  1820. goto leave;
  1821. }
  1822. /* a little variable switch-a-roo here... */
  1823. dlm = new_ctxt;
  1824. new_ctxt = NULL;
  1825. /* add the new domain */
  1826. list_add_tail(&dlm->list, &dlm_domains);
  1827. spin_unlock(&dlm_domain_lock);
  1828. /*
  1829. * Pass the locking protocol version into the join. If the join
  1830. * succeeds, it will have the negotiated protocol set.
  1831. */
  1832. dlm->dlm_locking_proto = dlm_protocol;
  1833. dlm->fs_locking_proto = *fs_proto;
  1834. ret = dlm_join_domain(dlm);
  1835. if (ret) {
  1836. mlog_errno(ret);
  1837. dlm_put(dlm);
  1838. goto leave;
  1839. }
  1840. /* Tell the caller what locking protocol we negotiated */
  1841. *fs_proto = dlm->fs_locking_proto;
  1842. ret = 0;
  1843. leave:
  1844. if (new_ctxt)
  1845. dlm_free_ctxt_mem(new_ctxt);
  1846. if (ret < 0)
  1847. dlm = ERR_PTR(ret);
  1848. return dlm;
  1849. }
  1850. EXPORT_SYMBOL_GPL(dlm_register_domain);
  1851. static LIST_HEAD(dlm_join_handlers);
  1852. static void dlm_unregister_net_handlers(void)
  1853. {
  1854. o2net_unregister_handler_list(&dlm_join_handlers);
  1855. }
  1856. static int dlm_register_net_handlers(void)
  1857. {
  1858. int status = 0;
  1859. status = o2net_register_handler(DLM_QUERY_JOIN_MSG, DLM_MOD_KEY,
  1860. sizeof(struct dlm_query_join_request),
  1861. dlm_query_join_handler,
  1862. NULL, NULL, &dlm_join_handlers);
  1863. if (status)
  1864. goto bail;
  1865. status = o2net_register_handler(DLM_ASSERT_JOINED_MSG, DLM_MOD_KEY,
  1866. sizeof(struct dlm_assert_joined),
  1867. dlm_assert_joined_handler,
  1868. NULL, NULL, &dlm_join_handlers);
  1869. if (status)
  1870. goto bail;
  1871. status = o2net_register_handler(DLM_CANCEL_JOIN_MSG, DLM_MOD_KEY,
  1872. sizeof(struct dlm_cancel_join),
  1873. dlm_cancel_join_handler,
  1874. NULL, NULL, &dlm_join_handlers);
  1875. if (status)
  1876. goto bail;
  1877. status = o2net_register_handler(DLM_QUERY_REGION, DLM_MOD_KEY,
  1878. sizeof(struct dlm_query_region),
  1879. dlm_query_region_handler,
  1880. NULL, NULL, &dlm_join_handlers);
  1881. if (status)
  1882. goto bail;
  1883. status = o2net_register_handler(DLM_QUERY_NODEINFO, DLM_MOD_KEY,
  1884. sizeof(struct dlm_query_nodeinfo),
  1885. dlm_query_nodeinfo_handler,
  1886. NULL, NULL, &dlm_join_handlers);
  1887. bail:
  1888. if (status < 0)
  1889. dlm_unregister_net_handlers();
  1890. return status;
  1891. }
  1892. /* Domain eviction callback handling.
  1893. *
  1894. * The file system requires notification of node death *before* the
  1895. * dlm completes it's recovery work, otherwise it may be able to
  1896. * acquire locks on resources requiring recovery. Since the dlm can
  1897. * evict a node from it's domain *before* heartbeat fires, a similar
  1898. * mechanism is required. */
  1899. /* Eviction is not expected to happen often, so a per-domain lock is
  1900. * not necessary. Eviction callbacks are allowed to sleep for short
  1901. * periods of time. */
  1902. static DECLARE_RWSEM(dlm_callback_sem);
  1903. void dlm_fire_domain_eviction_callbacks(struct dlm_ctxt *dlm,
  1904. int node_num)
  1905. {
  1906. struct dlm_eviction_cb *cb;
  1907. down_read(&dlm_callback_sem);
  1908. list_for_each_entry(cb, &dlm->dlm_eviction_callbacks, ec_item) {
  1909. cb->ec_func(node_num, cb->ec_data);
  1910. }
  1911. up_read(&dlm_callback_sem);
  1912. }
  1913. void dlm_setup_eviction_cb(struct dlm_eviction_cb *cb,
  1914. dlm_eviction_func *f,
  1915. void *data)
  1916. {
  1917. INIT_LIST_HEAD(&cb->ec_item);
  1918. cb->ec_func = f;
  1919. cb->ec_data = data;
  1920. }
  1921. EXPORT_SYMBOL_GPL(dlm_setup_eviction_cb);
  1922. void dlm_register_eviction_cb(struct dlm_ctxt *dlm,
  1923. struct dlm_eviction_cb *cb)
  1924. {
  1925. down_write(&dlm_callback_sem);
  1926. list_add_tail(&cb->ec_item, &dlm->dlm_eviction_callbacks);
  1927. up_write(&dlm_callback_sem);
  1928. }
  1929. EXPORT_SYMBOL_GPL(dlm_register_eviction_cb);
  1930. void dlm_unregister_eviction_cb(struct dlm_eviction_cb *cb)
  1931. {
  1932. down_write(&dlm_callback_sem);
  1933. list_del_init(&cb->ec_item);
  1934. up_write(&dlm_callback_sem);
  1935. }
  1936. EXPORT_SYMBOL_GPL(dlm_unregister_eviction_cb);
  1937. static int __init dlm_init(void)
  1938. {
  1939. int status;
  1940. status = dlm_init_mle_cache();
  1941. if (status) {
  1942. mlog(ML_ERROR, "Could not create o2dlm_mle slabcache\n");
  1943. goto error;
  1944. }
  1945. status = dlm_init_master_caches();
  1946. if (status) {
  1947. mlog(ML_ERROR, "Could not create o2dlm_lockres and "
  1948. "o2dlm_lockname slabcaches\n");
  1949. goto error;
  1950. }
  1951. status = dlm_init_lock_cache();
  1952. if (status) {
  1953. mlog(ML_ERROR, "Count not create o2dlm_lock slabcache\n");
  1954. goto error;
  1955. }
  1956. status = dlm_register_net_handlers();
  1957. if (status) {
  1958. mlog(ML_ERROR, "Unable to register network handlers\n");
  1959. goto error;
  1960. }
  1961. status = dlm_create_debugfs_root();
  1962. if (status)
  1963. goto error;
  1964. return 0;
  1965. error:
  1966. dlm_unregister_net_handlers();
  1967. dlm_destroy_lock_cache();
  1968. dlm_destroy_master_caches();
  1969. dlm_destroy_mle_cache();
  1970. return -1;
  1971. }
  1972. static void __exit dlm_exit (void)
  1973. {
  1974. dlm_destroy_debugfs_root();
  1975. dlm_unregister_net_handlers();
  1976. dlm_destroy_lock_cache();
  1977. dlm_destroy_master_caches();
  1978. dlm_destroy_mle_cache();
  1979. }
  1980. MODULE_AUTHOR("Oracle");
  1981. MODULE_LICENSE("GPL");
  1982. MODULE_DESCRIPTION("OCFS2 Distributed Lock Management");
  1983. module_init(dlm_init);
  1984. module_exit(dlm_exit);