dlmdomain.c 60 KB

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