dlmdomain.c 54 KB

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