dlmdomain.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668
  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/utsname.h>
  31. #include <linux/init.h>
  32. #include <linux/spinlock.h>
  33. #include <linux/delay.h>
  34. #include <linux/err.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 "dlmver.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. #define DLM_DOMAIN_BACKOFF_MS 200
  108. static int dlm_query_join_handler(struct o2net_msg *msg, u32 len, void *data,
  109. void **ret_data);
  110. static int dlm_assert_joined_handler(struct o2net_msg *msg, u32 len, void *data,
  111. void **ret_data);
  112. static int dlm_cancel_join_handler(struct o2net_msg *msg, u32 len, void *data,
  113. void **ret_data);
  114. static int dlm_exit_domain_handler(struct o2net_msg *msg, u32 len, void *data,
  115. void **ret_data);
  116. static void dlm_unregister_domain_handlers(struct dlm_ctxt *dlm);
  117. void __dlm_unhash_lockres(struct dlm_lock_resource *lockres)
  118. {
  119. if (!hlist_unhashed(&lockres->hash_node)) {
  120. hlist_del_init(&lockres->hash_node);
  121. dlm_lockres_put(lockres);
  122. }
  123. }
  124. void __dlm_insert_lockres(struct dlm_ctxt *dlm,
  125. struct dlm_lock_resource *res)
  126. {
  127. struct hlist_head *bucket;
  128. struct qstr *q;
  129. assert_spin_locked(&dlm->spinlock);
  130. q = &res->lockname;
  131. bucket = dlm_lockres_hash(dlm, q->hash);
  132. /* get a reference for our hashtable */
  133. dlm_lockres_get(res);
  134. hlist_add_head(&res->hash_node, bucket);
  135. }
  136. struct dlm_lock_resource * __dlm_lookup_lockres_full(struct dlm_ctxt *dlm,
  137. const char *name,
  138. unsigned int len,
  139. unsigned int hash)
  140. {
  141. struct hlist_head *bucket;
  142. struct hlist_node *list;
  143. mlog_entry("%.*s\n", len, name);
  144. assert_spin_locked(&dlm->spinlock);
  145. bucket = dlm_lockres_hash(dlm, hash);
  146. hlist_for_each(list, bucket) {
  147. struct dlm_lock_resource *res = hlist_entry(list,
  148. struct dlm_lock_resource, hash_node);
  149. if (res->lockname.name[0] != name[0])
  150. continue;
  151. if (unlikely(res->lockname.len != len))
  152. continue;
  153. if (memcmp(res->lockname.name + 1, name + 1, len - 1))
  154. continue;
  155. dlm_lockres_get(res);
  156. return res;
  157. }
  158. return NULL;
  159. }
  160. /* intended to be called by functions which do not care about lock
  161. * resources which are being purged (most net _handler functions).
  162. * this will return NULL for any lock resource which is found but
  163. * currently in the process of dropping its mastery reference.
  164. * use __dlm_lookup_lockres_full when you need the lock resource
  165. * regardless (e.g. dlm_get_lock_resource) */
  166. struct dlm_lock_resource * __dlm_lookup_lockres(struct dlm_ctxt *dlm,
  167. const char *name,
  168. unsigned int len,
  169. unsigned int hash)
  170. {
  171. struct dlm_lock_resource *res = NULL;
  172. mlog_entry("%.*s\n", len, name);
  173. assert_spin_locked(&dlm->spinlock);
  174. res = __dlm_lookup_lockres_full(dlm, name, len, hash);
  175. if (res) {
  176. spin_lock(&res->spinlock);
  177. if (res->state & DLM_LOCK_RES_DROPPING_REF) {
  178. spin_unlock(&res->spinlock);
  179. dlm_lockres_put(res);
  180. return NULL;
  181. }
  182. spin_unlock(&res->spinlock);
  183. }
  184. return res;
  185. }
  186. struct dlm_lock_resource * dlm_lookup_lockres(struct dlm_ctxt *dlm,
  187. const char *name,
  188. unsigned int len)
  189. {
  190. struct dlm_lock_resource *res;
  191. unsigned int hash = dlm_lockid_hash(name, len);
  192. spin_lock(&dlm->spinlock);
  193. res = __dlm_lookup_lockres(dlm, name, len, hash);
  194. spin_unlock(&dlm->spinlock);
  195. return res;
  196. }
  197. static struct dlm_ctxt * __dlm_lookup_domain_full(const char *domain, int len)
  198. {
  199. struct dlm_ctxt *tmp = NULL;
  200. struct list_head *iter;
  201. assert_spin_locked(&dlm_domain_lock);
  202. /* tmp->name here is always NULL terminated,
  203. * but domain may not be! */
  204. list_for_each(iter, &dlm_domains) {
  205. tmp = list_entry (iter, struct dlm_ctxt, list);
  206. if (strlen(tmp->name) == len &&
  207. memcmp(tmp->name, domain, len)==0)
  208. break;
  209. tmp = NULL;
  210. }
  211. return tmp;
  212. }
  213. /* For null terminated domain strings ONLY */
  214. static struct dlm_ctxt * __dlm_lookup_domain(const char *domain)
  215. {
  216. assert_spin_locked(&dlm_domain_lock);
  217. return __dlm_lookup_domain_full(domain, strlen(domain));
  218. }
  219. /* returns true on one of two conditions:
  220. * 1) the domain does not exist
  221. * 2) the domain exists and it's state is "joined" */
  222. static int dlm_wait_on_domain_helper(const char *domain)
  223. {
  224. int ret = 0;
  225. struct dlm_ctxt *tmp = NULL;
  226. spin_lock(&dlm_domain_lock);
  227. tmp = __dlm_lookup_domain(domain);
  228. if (!tmp)
  229. ret = 1;
  230. else if (tmp->dlm_state == DLM_CTXT_JOINED)
  231. ret = 1;
  232. spin_unlock(&dlm_domain_lock);
  233. return ret;
  234. }
  235. static void dlm_free_ctxt_mem(struct dlm_ctxt *dlm)
  236. {
  237. if (dlm->lockres_hash)
  238. dlm_free_pagevec((void **)dlm->lockres_hash, DLM_HASH_PAGES);
  239. if (dlm->name)
  240. kfree(dlm->name);
  241. kfree(dlm);
  242. }
  243. /* A little strange - this function will be called while holding
  244. * dlm_domain_lock and is expected to be holding it on the way out. We
  245. * will however drop and reacquire it multiple times */
  246. static void dlm_ctxt_release(struct kref *kref)
  247. {
  248. struct dlm_ctxt *dlm;
  249. dlm = container_of(kref, struct dlm_ctxt, dlm_refs);
  250. BUG_ON(dlm->num_joins);
  251. BUG_ON(dlm->dlm_state == DLM_CTXT_JOINED);
  252. /* we may still be in the list if we hit an error during join. */
  253. list_del_init(&dlm->list);
  254. spin_unlock(&dlm_domain_lock);
  255. mlog(0, "freeing memory from domain %s\n", dlm->name);
  256. wake_up(&dlm_domain_events);
  257. dlm_free_ctxt_mem(dlm);
  258. spin_lock(&dlm_domain_lock);
  259. }
  260. void dlm_put(struct dlm_ctxt *dlm)
  261. {
  262. spin_lock(&dlm_domain_lock);
  263. kref_put(&dlm->dlm_refs, dlm_ctxt_release);
  264. spin_unlock(&dlm_domain_lock);
  265. }
  266. static void __dlm_get(struct dlm_ctxt *dlm)
  267. {
  268. kref_get(&dlm->dlm_refs);
  269. }
  270. /* given a questionable reference to a dlm object, gets a reference if
  271. * it can find it in the list, otherwise returns NULL in which case
  272. * you shouldn't trust your pointer. */
  273. struct dlm_ctxt *dlm_grab(struct dlm_ctxt *dlm)
  274. {
  275. struct list_head *iter;
  276. struct dlm_ctxt *target = NULL;
  277. spin_lock(&dlm_domain_lock);
  278. list_for_each(iter, &dlm_domains) {
  279. target = list_entry (iter, struct dlm_ctxt, list);
  280. if (target == dlm) {
  281. __dlm_get(target);
  282. break;
  283. }
  284. target = NULL;
  285. }
  286. spin_unlock(&dlm_domain_lock);
  287. return target;
  288. }
  289. int dlm_domain_fully_joined(struct dlm_ctxt *dlm)
  290. {
  291. int ret;
  292. spin_lock(&dlm_domain_lock);
  293. ret = (dlm->dlm_state == DLM_CTXT_JOINED) ||
  294. (dlm->dlm_state == DLM_CTXT_IN_SHUTDOWN);
  295. spin_unlock(&dlm_domain_lock);
  296. return ret;
  297. }
  298. static void dlm_destroy_dlm_worker(struct dlm_ctxt *dlm)
  299. {
  300. if (dlm->dlm_worker) {
  301. flush_workqueue(dlm->dlm_worker);
  302. destroy_workqueue(dlm->dlm_worker);
  303. dlm->dlm_worker = NULL;
  304. }
  305. }
  306. static void dlm_complete_dlm_shutdown(struct dlm_ctxt *dlm)
  307. {
  308. dlm_unregister_domain_handlers(dlm);
  309. dlm_complete_thread(dlm);
  310. dlm_complete_recovery_thread(dlm);
  311. dlm_destroy_dlm_worker(dlm);
  312. /* We've left the domain. Now we can take ourselves out of the
  313. * list and allow the kref stuff to help us free the
  314. * memory. */
  315. spin_lock(&dlm_domain_lock);
  316. list_del_init(&dlm->list);
  317. spin_unlock(&dlm_domain_lock);
  318. /* Wake up anyone waiting for us to remove this domain */
  319. wake_up(&dlm_domain_events);
  320. }
  321. static int dlm_migrate_all_locks(struct dlm_ctxt *dlm)
  322. {
  323. int i, num, n, ret = 0;
  324. struct dlm_lock_resource *res;
  325. struct hlist_node *iter;
  326. struct hlist_head *bucket;
  327. int dropped;
  328. mlog(0, "Migrating locks from domain %s\n", dlm->name);
  329. num = 0;
  330. spin_lock(&dlm->spinlock);
  331. for (i = 0; i < DLM_HASH_BUCKETS; i++) {
  332. redo_bucket:
  333. n = 0;
  334. bucket = dlm_lockres_hash(dlm, i);
  335. iter = bucket->first;
  336. while (iter) {
  337. n++;
  338. res = hlist_entry(iter, struct dlm_lock_resource,
  339. hash_node);
  340. dlm_lockres_get(res);
  341. /* migrate, if necessary. this will drop the dlm
  342. * spinlock and retake it if it does migration. */
  343. dropped = dlm_empty_lockres(dlm, res);
  344. spin_lock(&res->spinlock);
  345. __dlm_lockres_calc_usage(dlm, res);
  346. iter = res->hash_node.next;
  347. spin_unlock(&res->spinlock);
  348. dlm_lockres_put(res);
  349. cond_resched_lock(&dlm->spinlock);
  350. if (dropped)
  351. goto redo_bucket;
  352. }
  353. num += n;
  354. mlog(0, "%s: touched %d lockreses in bucket %d "
  355. "(tot=%d)\n", dlm->name, n, i, num);
  356. }
  357. spin_unlock(&dlm->spinlock);
  358. wake_up(&dlm->dlm_thread_wq);
  359. /* let the dlm thread take care of purging, keep scanning until
  360. * nothing remains in the hash */
  361. if (num) {
  362. mlog(0, "%s: %d lock resources in hash last pass\n",
  363. dlm->name, num);
  364. ret = -EAGAIN;
  365. }
  366. mlog(0, "DONE Migrating locks from domain %s\n", dlm->name);
  367. return ret;
  368. }
  369. static int dlm_no_joining_node(struct dlm_ctxt *dlm)
  370. {
  371. int ret;
  372. spin_lock(&dlm->spinlock);
  373. ret = dlm->joining_node == DLM_LOCK_RES_OWNER_UNKNOWN;
  374. spin_unlock(&dlm->spinlock);
  375. return ret;
  376. }
  377. static void dlm_mark_domain_leaving(struct dlm_ctxt *dlm)
  378. {
  379. /* Yikes, a double spinlock! I need domain_lock for the dlm
  380. * state and the dlm spinlock for join state... Sorry! */
  381. again:
  382. spin_lock(&dlm_domain_lock);
  383. spin_lock(&dlm->spinlock);
  384. if (dlm->joining_node != DLM_LOCK_RES_OWNER_UNKNOWN) {
  385. mlog(0, "Node %d is joining, we wait on it.\n",
  386. dlm->joining_node);
  387. spin_unlock(&dlm->spinlock);
  388. spin_unlock(&dlm_domain_lock);
  389. wait_event(dlm->dlm_join_events, dlm_no_joining_node(dlm));
  390. goto again;
  391. }
  392. dlm->dlm_state = DLM_CTXT_LEAVING;
  393. spin_unlock(&dlm->spinlock);
  394. spin_unlock(&dlm_domain_lock);
  395. }
  396. static void __dlm_print_nodes(struct dlm_ctxt *dlm)
  397. {
  398. int node = -1;
  399. assert_spin_locked(&dlm->spinlock);
  400. printk(KERN_INFO "ocfs2_dlm: Nodes in domain (\"%s\"): ", dlm->name);
  401. while ((node = find_next_bit(dlm->domain_map, O2NM_MAX_NODES,
  402. node + 1)) < O2NM_MAX_NODES) {
  403. printk("%d ", node);
  404. }
  405. printk("\n");
  406. }
  407. static int dlm_exit_domain_handler(struct o2net_msg *msg, u32 len, void *data,
  408. void **ret_data)
  409. {
  410. struct dlm_ctxt *dlm = data;
  411. unsigned int node;
  412. struct dlm_exit_domain *exit_msg = (struct dlm_exit_domain *) msg->buf;
  413. mlog_entry("%p %u %p", msg, len, data);
  414. if (!dlm_grab(dlm))
  415. return 0;
  416. node = exit_msg->node_idx;
  417. printk(KERN_INFO "ocfs2_dlm: Node %u leaves domain %s\n", node, dlm->name);
  418. spin_lock(&dlm->spinlock);
  419. clear_bit(node, dlm->domain_map);
  420. __dlm_print_nodes(dlm);
  421. /* notify anything attached to the heartbeat events */
  422. dlm_hb_event_notify_attached(dlm, node, 0);
  423. spin_unlock(&dlm->spinlock);
  424. dlm_put(dlm);
  425. return 0;
  426. }
  427. static int dlm_send_one_domain_exit(struct dlm_ctxt *dlm,
  428. unsigned int node)
  429. {
  430. int status;
  431. struct dlm_exit_domain leave_msg;
  432. mlog(0, "Asking node %u if we can leave the domain %s me = %u\n",
  433. node, dlm->name, dlm->node_num);
  434. memset(&leave_msg, 0, sizeof(leave_msg));
  435. leave_msg.node_idx = dlm->node_num;
  436. status = o2net_send_message(DLM_EXIT_DOMAIN_MSG, dlm->key,
  437. &leave_msg, sizeof(leave_msg), node,
  438. NULL);
  439. mlog(0, "status return %d from o2net_send_message\n", status);
  440. return status;
  441. }
  442. static void dlm_leave_domain(struct dlm_ctxt *dlm)
  443. {
  444. int node, clear_node, status;
  445. /* At this point we've migrated away all our locks and won't
  446. * accept mastership of new ones. The dlm is responsible for
  447. * almost nothing now. We make sure not to confuse any joining
  448. * nodes and then commence shutdown procedure. */
  449. spin_lock(&dlm->spinlock);
  450. /* Clear ourselves from the domain map */
  451. clear_bit(dlm->node_num, dlm->domain_map);
  452. while ((node = find_next_bit(dlm->domain_map, O2NM_MAX_NODES,
  453. 0)) < O2NM_MAX_NODES) {
  454. /* Drop the dlm spinlock. This is safe wrt the domain_map.
  455. * -nodes cannot be added now as the
  456. * query_join_handlers knows to respond with OK_NO_MAP
  457. * -we catch the right network errors if a node is
  458. * removed from the map while we're sending him the
  459. * exit message. */
  460. spin_unlock(&dlm->spinlock);
  461. clear_node = 1;
  462. status = dlm_send_one_domain_exit(dlm, node);
  463. if (status < 0 &&
  464. status != -ENOPROTOOPT &&
  465. status != -ENOTCONN) {
  466. mlog(ML_NOTICE, "Error %d sending domain exit message "
  467. "to node %d\n", status, node);
  468. /* Not sure what to do here but lets sleep for
  469. * a bit in case this was a transient
  470. * error... */
  471. msleep(DLM_DOMAIN_BACKOFF_MS);
  472. clear_node = 0;
  473. }
  474. spin_lock(&dlm->spinlock);
  475. /* If we're not clearing the node bit then we intend
  476. * to loop back around to try again. */
  477. if (clear_node)
  478. clear_bit(node, dlm->domain_map);
  479. }
  480. spin_unlock(&dlm->spinlock);
  481. }
  482. int dlm_joined(struct dlm_ctxt *dlm)
  483. {
  484. int ret = 0;
  485. spin_lock(&dlm_domain_lock);
  486. if (dlm->dlm_state == DLM_CTXT_JOINED)
  487. ret = 1;
  488. spin_unlock(&dlm_domain_lock);
  489. return ret;
  490. }
  491. int dlm_shutting_down(struct dlm_ctxt *dlm)
  492. {
  493. int ret = 0;
  494. spin_lock(&dlm_domain_lock);
  495. if (dlm->dlm_state == DLM_CTXT_IN_SHUTDOWN)
  496. ret = 1;
  497. spin_unlock(&dlm_domain_lock);
  498. return ret;
  499. }
  500. void dlm_unregister_domain(struct dlm_ctxt *dlm)
  501. {
  502. int leave = 0;
  503. spin_lock(&dlm_domain_lock);
  504. BUG_ON(dlm->dlm_state != DLM_CTXT_JOINED);
  505. BUG_ON(!dlm->num_joins);
  506. dlm->num_joins--;
  507. if (!dlm->num_joins) {
  508. /* We mark it "in shutdown" now so new register
  509. * requests wait until we've completely left the
  510. * domain. Don't use DLM_CTXT_LEAVING yet as we still
  511. * want new domain joins to communicate with us at
  512. * least until we've completed migration of our
  513. * resources. */
  514. dlm->dlm_state = DLM_CTXT_IN_SHUTDOWN;
  515. leave = 1;
  516. }
  517. spin_unlock(&dlm_domain_lock);
  518. if (leave) {
  519. mlog(0, "shutting down domain %s\n", dlm->name);
  520. /* We changed dlm state, notify the thread */
  521. dlm_kick_thread(dlm, NULL);
  522. while (dlm_migrate_all_locks(dlm)) {
  523. mlog(0, "%s: more migration to do\n", dlm->name);
  524. }
  525. dlm_mark_domain_leaving(dlm);
  526. dlm_leave_domain(dlm);
  527. dlm_complete_dlm_shutdown(dlm);
  528. }
  529. dlm_put(dlm);
  530. }
  531. EXPORT_SYMBOL_GPL(dlm_unregister_domain);
  532. static int dlm_query_join_handler(struct o2net_msg *msg, u32 len, void *data,
  533. void **ret_data)
  534. {
  535. struct dlm_query_join_request *query;
  536. enum dlm_query_join_response response;
  537. struct dlm_ctxt *dlm = NULL;
  538. u8 nodenum;
  539. query = (struct dlm_query_join_request *) msg->buf;
  540. mlog(0, "node %u wants to join domain %s\n", query->node_idx,
  541. query->domain);
  542. /*
  543. * If heartbeat doesn't consider the node live, tell it
  544. * to back off and try again. This gives heartbeat a chance
  545. * to catch up.
  546. */
  547. if (!o2hb_check_node_heartbeating(query->node_idx)) {
  548. mlog(0, "node %u is not in our live map yet\n",
  549. query->node_idx);
  550. response = JOIN_DISALLOW;
  551. goto respond;
  552. }
  553. response = JOIN_OK_NO_MAP;
  554. spin_lock(&dlm_domain_lock);
  555. dlm = __dlm_lookup_domain_full(query->domain, query->name_len);
  556. if (!dlm)
  557. goto unlock_respond;
  558. /*
  559. * There is a small window where the joining node may not see the
  560. * node(s) that just left but still part of the cluster. DISALLOW
  561. * join request if joining node has different node map.
  562. */
  563. nodenum=0;
  564. while (nodenum < O2NM_MAX_NODES) {
  565. if (test_bit(nodenum, dlm->domain_map)) {
  566. if (!byte_test_bit(nodenum, query->node_map)) {
  567. mlog(0, "disallow join as node %u does not "
  568. "have node %u in its nodemap\n",
  569. query->node_idx, nodenum);
  570. response = JOIN_DISALLOW;
  571. goto unlock_respond;
  572. }
  573. }
  574. nodenum++;
  575. }
  576. /* Once the dlm ctxt is marked as leaving then we don't want
  577. * to be put in someone's domain map.
  578. * Also, explicitly disallow joining at certain troublesome
  579. * times (ie. during recovery). */
  580. if (dlm && dlm->dlm_state != DLM_CTXT_LEAVING) {
  581. int bit = query->node_idx;
  582. spin_lock(&dlm->spinlock);
  583. if (dlm->dlm_state == DLM_CTXT_NEW &&
  584. dlm->joining_node == DLM_LOCK_RES_OWNER_UNKNOWN) {
  585. /*If this is a brand new context and we
  586. * haven't started our join process yet, then
  587. * the other node won the race. */
  588. response = JOIN_OK_NO_MAP;
  589. } else if (dlm->joining_node != DLM_LOCK_RES_OWNER_UNKNOWN) {
  590. /* Disallow parallel joins. */
  591. response = JOIN_DISALLOW;
  592. } else if (dlm->reco.state & DLM_RECO_STATE_ACTIVE) {
  593. mlog(0, "node %u trying to join, but recovery "
  594. "is ongoing.\n", bit);
  595. response = JOIN_DISALLOW;
  596. } else if (test_bit(bit, dlm->recovery_map)) {
  597. mlog(0, "node %u trying to join, but it "
  598. "still needs recovery.\n", bit);
  599. response = JOIN_DISALLOW;
  600. } else if (test_bit(bit, dlm->domain_map)) {
  601. mlog(0, "node %u trying to join, but it "
  602. "is still in the domain! needs recovery?\n",
  603. bit);
  604. response = JOIN_DISALLOW;
  605. } else {
  606. /* Alright we're fully a part of this domain
  607. * so we keep some state as to who's joining
  608. * and indicate to him that needs to be fixed
  609. * up. */
  610. response = JOIN_OK;
  611. __dlm_set_joining_node(dlm, query->node_idx);
  612. }
  613. spin_unlock(&dlm->spinlock);
  614. }
  615. unlock_respond:
  616. spin_unlock(&dlm_domain_lock);
  617. respond:
  618. mlog(0, "We respond with %u\n", response);
  619. return response;
  620. }
  621. static int dlm_assert_joined_handler(struct o2net_msg *msg, u32 len, void *data,
  622. void **ret_data)
  623. {
  624. struct dlm_assert_joined *assert;
  625. struct dlm_ctxt *dlm = NULL;
  626. assert = (struct dlm_assert_joined *) msg->buf;
  627. mlog(0, "node %u asserts join on domain %s\n", assert->node_idx,
  628. assert->domain);
  629. spin_lock(&dlm_domain_lock);
  630. dlm = __dlm_lookup_domain_full(assert->domain, assert->name_len);
  631. /* XXX should we consider no dlm ctxt an error? */
  632. if (dlm) {
  633. spin_lock(&dlm->spinlock);
  634. /* Alright, this node has officially joined our
  635. * domain. Set him in the map and clean up our
  636. * leftover join state. */
  637. BUG_ON(dlm->joining_node != assert->node_idx);
  638. set_bit(assert->node_idx, dlm->domain_map);
  639. __dlm_set_joining_node(dlm, DLM_LOCK_RES_OWNER_UNKNOWN);
  640. printk(KERN_INFO "ocfs2_dlm: Node %u joins domain %s\n",
  641. assert->node_idx, dlm->name);
  642. __dlm_print_nodes(dlm);
  643. /* notify anything attached to the heartbeat events */
  644. dlm_hb_event_notify_attached(dlm, assert->node_idx, 1);
  645. spin_unlock(&dlm->spinlock);
  646. }
  647. spin_unlock(&dlm_domain_lock);
  648. return 0;
  649. }
  650. static int dlm_cancel_join_handler(struct o2net_msg *msg, u32 len, void *data,
  651. void **ret_data)
  652. {
  653. struct dlm_cancel_join *cancel;
  654. struct dlm_ctxt *dlm = NULL;
  655. cancel = (struct dlm_cancel_join *) msg->buf;
  656. mlog(0, "node %u cancels join on domain %s\n", cancel->node_idx,
  657. cancel->domain);
  658. spin_lock(&dlm_domain_lock);
  659. dlm = __dlm_lookup_domain_full(cancel->domain, cancel->name_len);
  660. if (dlm) {
  661. spin_lock(&dlm->spinlock);
  662. /* Yikes, this guy wants to cancel his join. No
  663. * problem, we simply cleanup our join state. */
  664. BUG_ON(dlm->joining_node != cancel->node_idx);
  665. __dlm_set_joining_node(dlm, DLM_LOCK_RES_OWNER_UNKNOWN);
  666. spin_unlock(&dlm->spinlock);
  667. }
  668. spin_unlock(&dlm_domain_lock);
  669. return 0;
  670. }
  671. static int dlm_send_one_join_cancel(struct dlm_ctxt *dlm,
  672. unsigned int node)
  673. {
  674. int status;
  675. struct dlm_cancel_join cancel_msg;
  676. memset(&cancel_msg, 0, sizeof(cancel_msg));
  677. cancel_msg.node_idx = dlm->node_num;
  678. cancel_msg.name_len = strlen(dlm->name);
  679. memcpy(cancel_msg.domain, dlm->name, cancel_msg.name_len);
  680. status = o2net_send_message(DLM_CANCEL_JOIN_MSG, DLM_MOD_KEY,
  681. &cancel_msg, sizeof(cancel_msg), node,
  682. NULL);
  683. if (status < 0) {
  684. mlog_errno(status);
  685. goto bail;
  686. }
  687. bail:
  688. return status;
  689. }
  690. /* map_size should be in bytes. */
  691. static int dlm_send_join_cancels(struct dlm_ctxt *dlm,
  692. unsigned long *node_map,
  693. unsigned int map_size)
  694. {
  695. int status, tmpstat;
  696. unsigned int node;
  697. if (map_size != (BITS_TO_LONGS(O2NM_MAX_NODES) *
  698. sizeof(unsigned long))) {
  699. mlog(ML_ERROR,
  700. "map_size %u != BITS_TO_LONGS(O2NM_MAX_NODES) %u\n",
  701. map_size, BITS_TO_LONGS(O2NM_MAX_NODES));
  702. return -EINVAL;
  703. }
  704. status = 0;
  705. node = -1;
  706. while ((node = find_next_bit(node_map, O2NM_MAX_NODES,
  707. node + 1)) < O2NM_MAX_NODES) {
  708. if (node == dlm->node_num)
  709. continue;
  710. tmpstat = dlm_send_one_join_cancel(dlm, node);
  711. if (tmpstat) {
  712. mlog(ML_ERROR, "Error return %d cancelling join on "
  713. "node %d\n", tmpstat, node);
  714. if (!status)
  715. status = tmpstat;
  716. }
  717. }
  718. if (status)
  719. mlog_errno(status);
  720. return status;
  721. }
  722. static int dlm_request_join(struct dlm_ctxt *dlm,
  723. int node,
  724. enum dlm_query_join_response *response)
  725. {
  726. int status, retval;
  727. struct dlm_query_join_request join_msg;
  728. mlog(0, "querying node %d\n", node);
  729. memset(&join_msg, 0, sizeof(join_msg));
  730. join_msg.node_idx = dlm->node_num;
  731. join_msg.name_len = strlen(dlm->name);
  732. memcpy(join_msg.domain, dlm->name, join_msg.name_len);
  733. /* copy live node map to join message */
  734. byte_copymap(join_msg.node_map, dlm->live_nodes_map, O2NM_MAX_NODES);
  735. status = o2net_send_message(DLM_QUERY_JOIN_MSG, DLM_MOD_KEY, &join_msg,
  736. sizeof(join_msg), node, &retval);
  737. if (status < 0 && status != -ENOPROTOOPT) {
  738. mlog_errno(status);
  739. goto bail;
  740. }
  741. /* -ENOPROTOOPT from the net code means the other side isn't
  742. listening for our message type -- that's fine, it means
  743. his dlm isn't up, so we can consider him a 'yes' but not
  744. joined into the domain. */
  745. if (status == -ENOPROTOOPT) {
  746. status = 0;
  747. *response = JOIN_OK_NO_MAP;
  748. } else if (retval == JOIN_DISALLOW ||
  749. retval == JOIN_OK ||
  750. retval == JOIN_OK_NO_MAP) {
  751. *response = retval;
  752. } else {
  753. status = -EINVAL;
  754. mlog(ML_ERROR, "invalid response %d from node %u\n", retval,
  755. node);
  756. }
  757. mlog(0, "status %d, node %d response is %d\n", status, node,
  758. *response);
  759. bail:
  760. return status;
  761. }
  762. static int dlm_send_one_join_assert(struct dlm_ctxt *dlm,
  763. unsigned int node)
  764. {
  765. int status;
  766. struct dlm_assert_joined assert_msg;
  767. mlog(0, "Sending join assert to node %u\n", node);
  768. memset(&assert_msg, 0, sizeof(assert_msg));
  769. assert_msg.node_idx = dlm->node_num;
  770. assert_msg.name_len = strlen(dlm->name);
  771. memcpy(assert_msg.domain, dlm->name, assert_msg.name_len);
  772. status = o2net_send_message(DLM_ASSERT_JOINED_MSG, DLM_MOD_KEY,
  773. &assert_msg, sizeof(assert_msg), node,
  774. NULL);
  775. if (status < 0)
  776. mlog_errno(status);
  777. return status;
  778. }
  779. static void dlm_send_join_asserts(struct dlm_ctxt *dlm,
  780. unsigned long *node_map)
  781. {
  782. int status, node, live;
  783. status = 0;
  784. node = -1;
  785. while ((node = find_next_bit(node_map, O2NM_MAX_NODES,
  786. node + 1)) < O2NM_MAX_NODES) {
  787. if (node == dlm->node_num)
  788. continue;
  789. do {
  790. /* It is very important that this message be
  791. * received so we spin until either the node
  792. * has died or it gets the message. */
  793. status = dlm_send_one_join_assert(dlm, node);
  794. spin_lock(&dlm->spinlock);
  795. live = test_bit(node, dlm->live_nodes_map);
  796. spin_unlock(&dlm->spinlock);
  797. if (status) {
  798. mlog(ML_ERROR, "Error return %d asserting "
  799. "join on node %d\n", status, node);
  800. /* give us some time between errors... */
  801. if (live)
  802. msleep(DLM_DOMAIN_BACKOFF_MS);
  803. }
  804. } while (status && live);
  805. }
  806. }
  807. struct domain_join_ctxt {
  808. unsigned long live_map[BITS_TO_LONGS(O2NM_MAX_NODES)];
  809. unsigned long yes_resp_map[BITS_TO_LONGS(O2NM_MAX_NODES)];
  810. };
  811. static int dlm_should_restart_join(struct dlm_ctxt *dlm,
  812. struct domain_join_ctxt *ctxt,
  813. enum dlm_query_join_response response)
  814. {
  815. int ret;
  816. if (response == JOIN_DISALLOW) {
  817. mlog(0, "Latest response of disallow -- should restart\n");
  818. return 1;
  819. }
  820. spin_lock(&dlm->spinlock);
  821. /* For now, we restart the process if the node maps have
  822. * changed at all */
  823. ret = memcmp(ctxt->live_map, dlm->live_nodes_map,
  824. sizeof(dlm->live_nodes_map));
  825. spin_unlock(&dlm->spinlock);
  826. if (ret)
  827. mlog(0, "Node maps changed -- should restart\n");
  828. return ret;
  829. }
  830. static int dlm_try_to_join_domain(struct dlm_ctxt *dlm)
  831. {
  832. int status = 0, tmpstat, node;
  833. struct domain_join_ctxt *ctxt;
  834. enum dlm_query_join_response response;
  835. mlog_entry("%p", dlm);
  836. ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
  837. if (!ctxt) {
  838. status = -ENOMEM;
  839. mlog_errno(status);
  840. goto bail;
  841. }
  842. /* group sem locking should work for us here -- we're already
  843. * registered for heartbeat events so filling this should be
  844. * atomic wrt getting those handlers called. */
  845. o2hb_fill_node_map(dlm->live_nodes_map, sizeof(dlm->live_nodes_map));
  846. spin_lock(&dlm->spinlock);
  847. memcpy(ctxt->live_map, dlm->live_nodes_map, sizeof(ctxt->live_map));
  848. __dlm_set_joining_node(dlm, dlm->node_num);
  849. spin_unlock(&dlm->spinlock);
  850. node = -1;
  851. while ((node = find_next_bit(ctxt->live_map, O2NM_MAX_NODES,
  852. node + 1)) < O2NM_MAX_NODES) {
  853. if (node == dlm->node_num)
  854. continue;
  855. status = dlm_request_join(dlm, node, &response);
  856. if (status < 0) {
  857. mlog_errno(status);
  858. goto bail;
  859. }
  860. /* Ok, either we got a response or the node doesn't have a
  861. * dlm up. */
  862. if (response == JOIN_OK)
  863. set_bit(node, ctxt->yes_resp_map);
  864. if (dlm_should_restart_join(dlm, ctxt, response)) {
  865. status = -EAGAIN;
  866. goto bail;
  867. }
  868. }
  869. mlog(0, "Yay, done querying nodes!\n");
  870. /* Yay, everyone agree's we can join the domain. My domain is
  871. * comprised of all nodes who were put in the
  872. * yes_resp_map. Copy that into our domain map and send a join
  873. * assert message to clean up everyone elses state. */
  874. spin_lock(&dlm->spinlock);
  875. memcpy(dlm->domain_map, ctxt->yes_resp_map,
  876. sizeof(ctxt->yes_resp_map));
  877. set_bit(dlm->node_num, dlm->domain_map);
  878. spin_unlock(&dlm->spinlock);
  879. dlm_send_join_asserts(dlm, ctxt->yes_resp_map);
  880. /* Joined state *must* be set before the joining node
  881. * information, otherwise the query_join handler may read no
  882. * current joiner but a state of NEW and tell joining nodes
  883. * we're not in the domain. */
  884. spin_lock(&dlm_domain_lock);
  885. dlm->dlm_state = DLM_CTXT_JOINED;
  886. dlm->num_joins++;
  887. spin_unlock(&dlm_domain_lock);
  888. bail:
  889. spin_lock(&dlm->spinlock);
  890. __dlm_set_joining_node(dlm, DLM_LOCK_RES_OWNER_UNKNOWN);
  891. if (!status)
  892. __dlm_print_nodes(dlm);
  893. spin_unlock(&dlm->spinlock);
  894. if (ctxt) {
  895. /* Do we need to send a cancel message to any nodes? */
  896. if (status < 0) {
  897. tmpstat = dlm_send_join_cancels(dlm,
  898. ctxt->yes_resp_map,
  899. sizeof(ctxt->yes_resp_map));
  900. if (tmpstat < 0)
  901. mlog_errno(tmpstat);
  902. }
  903. kfree(ctxt);
  904. }
  905. mlog(0, "returning %d\n", status);
  906. return status;
  907. }
  908. static void dlm_unregister_domain_handlers(struct dlm_ctxt *dlm)
  909. {
  910. o2hb_unregister_callback(&dlm->dlm_hb_up);
  911. o2hb_unregister_callback(&dlm->dlm_hb_down);
  912. o2net_unregister_handler_list(&dlm->dlm_domain_handlers);
  913. }
  914. static int dlm_register_domain_handlers(struct dlm_ctxt *dlm)
  915. {
  916. int status;
  917. mlog(0, "registering handlers.\n");
  918. o2hb_setup_callback(&dlm->dlm_hb_down, O2HB_NODE_DOWN_CB,
  919. dlm_hb_node_down_cb, dlm, DLM_HB_NODE_DOWN_PRI);
  920. status = o2hb_register_callback(&dlm->dlm_hb_down);
  921. if (status)
  922. goto bail;
  923. o2hb_setup_callback(&dlm->dlm_hb_up, O2HB_NODE_UP_CB,
  924. dlm_hb_node_up_cb, dlm, DLM_HB_NODE_UP_PRI);
  925. status = o2hb_register_callback(&dlm->dlm_hb_up);
  926. if (status)
  927. goto bail;
  928. status = o2net_register_handler(DLM_MASTER_REQUEST_MSG, dlm->key,
  929. sizeof(struct dlm_master_request),
  930. dlm_master_request_handler,
  931. dlm, NULL, &dlm->dlm_domain_handlers);
  932. if (status)
  933. goto bail;
  934. status = o2net_register_handler(DLM_ASSERT_MASTER_MSG, dlm->key,
  935. sizeof(struct dlm_assert_master),
  936. dlm_assert_master_handler,
  937. dlm, dlm_assert_master_post_handler,
  938. &dlm->dlm_domain_handlers);
  939. if (status)
  940. goto bail;
  941. status = o2net_register_handler(DLM_CREATE_LOCK_MSG, dlm->key,
  942. sizeof(struct dlm_create_lock),
  943. dlm_create_lock_handler,
  944. dlm, NULL, &dlm->dlm_domain_handlers);
  945. if (status)
  946. goto bail;
  947. status = o2net_register_handler(DLM_CONVERT_LOCK_MSG, dlm->key,
  948. DLM_CONVERT_LOCK_MAX_LEN,
  949. dlm_convert_lock_handler,
  950. dlm, NULL, &dlm->dlm_domain_handlers);
  951. if (status)
  952. goto bail;
  953. status = o2net_register_handler(DLM_UNLOCK_LOCK_MSG, dlm->key,
  954. DLM_UNLOCK_LOCK_MAX_LEN,
  955. dlm_unlock_lock_handler,
  956. dlm, NULL, &dlm->dlm_domain_handlers);
  957. if (status)
  958. goto bail;
  959. status = o2net_register_handler(DLM_PROXY_AST_MSG, dlm->key,
  960. DLM_PROXY_AST_MAX_LEN,
  961. dlm_proxy_ast_handler,
  962. dlm, NULL, &dlm->dlm_domain_handlers);
  963. if (status)
  964. goto bail;
  965. status = o2net_register_handler(DLM_EXIT_DOMAIN_MSG, dlm->key,
  966. sizeof(struct dlm_exit_domain),
  967. dlm_exit_domain_handler,
  968. dlm, NULL, &dlm->dlm_domain_handlers);
  969. if (status)
  970. goto bail;
  971. status = o2net_register_handler(DLM_DEREF_LOCKRES_MSG, dlm->key,
  972. sizeof(struct dlm_deref_lockres),
  973. dlm_deref_lockres_handler,
  974. dlm, NULL, &dlm->dlm_domain_handlers);
  975. if (status)
  976. goto bail;
  977. status = o2net_register_handler(DLM_MIGRATE_REQUEST_MSG, dlm->key,
  978. sizeof(struct dlm_migrate_request),
  979. dlm_migrate_request_handler,
  980. dlm, NULL, &dlm->dlm_domain_handlers);
  981. if (status)
  982. goto bail;
  983. status = o2net_register_handler(DLM_MIG_LOCKRES_MSG, dlm->key,
  984. DLM_MIG_LOCKRES_MAX_LEN,
  985. dlm_mig_lockres_handler,
  986. dlm, NULL, &dlm->dlm_domain_handlers);
  987. if (status)
  988. goto bail;
  989. status = o2net_register_handler(DLM_MASTER_REQUERY_MSG, dlm->key,
  990. sizeof(struct dlm_master_requery),
  991. dlm_master_requery_handler,
  992. dlm, NULL, &dlm->dlm_domain_handlers);
  993. if (status)
  994. goto bail;
  995. status = o2net_register_handler(DLM_LOCK_REQUEST_MSG, dlm->key,
  996. sizeof(struct dlm_lock_request),
  997. dlm_request_all_locks_handler,
  998. dlm, NULL, &dlm->dlm_domain_handlers);
  999. if (status)
  1000. goto bail;
  1001. status = o2net_register_handler(DLM_RECO_DATA_DONE_MSG, dlm->key,
  1002. sizeof(struct dlm_reco_data_done),
  1003. dlm_reco_data_done_handler,
  1004. dlm, NULL, &dlm->dlm_domain_handlers);
  1005. if (status)
  1006. goto bail;
  1007. status = o2net_register_handler(DLM_BEGIN_RECO_MSG, dlm->key,
  1008. sizeof(struct dlm_begin_reco),
  1009. dlm_begin_reco_handler,
  1010. dlm, NULL, &dlm->dlm_domain_handlers);
  1011. if (status)
  1012. goto bail;
  1013. status = o2net_register_handler(DLM_FINALIZE_RECO_MSG, dlm->key,
  1014. sizeof(struct dlm_finalize_reco),
  1015. dlm_finalize_reco_handler,
  1016. dlm, NULL, &dlm->dlm_domain_handlers);
  1017. if (status)
  1018. goto bail;
  1019. bail:
  1020. if (status)
  1021. dlm_unregister_domain_handlers(dlm);
  1022. return status;
  1023. }
  1024. static int dlm_join_domain(struct dlm_ctxt *dlm)
  1025. {
  1026. int status;
  1027. unsigned int backoff;
  1028. unsigned int total_backoff = 0;
  1029. BUG_ON(!dlm);
  1030. mlog(0, "Join domain %s\n", dlm->name);
  1031. status = dlm_register_domain_handlers(dlm);
  1032. if (status) {
  1033. mlog_errno(status);
  1034. goto bail;
  1035. }
  1036. status = dlm_launch_thread(dlm);
  1037. if (status < 0) {
  1038. mlog_errno(status);
  1039. goto bail;
  1040. }
  1041. status = dlm_launch_recovery_thread(dlm);
  1042. if (status < 0) {
  1043. mlog_errno(status);
  1044. goto bail;
  1045. }
  1046. dlm->dlm_worker = create_singlethread_workqueue("dlm_wq");
  1047. if (!dlm->dlm_worker) {
  1048. status = -ENOMEM;
  1049. mlog_errno(status);
  1050. goto bail;
  1051. }
  1052. do {
  1053. status = dlm_try_to_join_domain(dlm);
  1054. /* If we're racing another node to the join, then we
  1055. * need to back off temporarily and let them
  1056. * complete. */
  1057. #define DLM_JOIN_TIMEOUT_MSECS 90000
  1058. if (status == -EAGAIN) {
  1059. if (signal_pending(current)) {
  1060. status = -ERESTARTSYS;
  1061. goto bail;
  1062. }
  1063. if (total_backoff >
  1064. msecs_to_jiffies(DLM_JOIN_TIMEOUT_MSECS)) {
  1065. status = -ERESTARTSYS;
  1066. mlog(ML_NOTICE, "Timed out joining dlm domain "
  1067. "%s after %u msecs\n", dlm->name,
  1068. jiffies_to_msecs(total_backoff));
  1069. goto bail;
  1070. }
  1071. /*
  1072. * <chip> After you!
  1073. * <dale> No, after you!
  1074. * <chip> I insist!
  1075. * <dale> But you first!
  1076. * ...
  1077. */
  1078. backoff = (unsigned int)(jiffies & 0x3);
  1079. backoff *= DLM_DOMAIN_BACKOFF_MS;
  1080. total_backoff += backoff;
  1081. mlog(0, "backoff %d\n", backoff);
  1082. msleep(backoff);
  1083. }
  1084. } while (status == -EAGAIN);
  1085. if (status < 0) {
  1086. mlog_errno(status);
  1087. goto bail;
  1088. }
  1089. status = 0;
  1090. bail:
  1091. wake_up(&dlm_domain_events);
  1092. if (status) {
  1093. dlm_unregister_domain_handlers(dlm);
  1094. dlm_complete_thread(dlm);
  1095. dlm_complete_recovery_thread(dlm);
  1096. dlm_destroy_dlm_worker(dlm);
  1097. }
  1098. return status;
  1099. }
  1100. static struct dlm_ctxt *dlm_alloc_ctxt(const char *domain,
  1101. u32 key)
  1102. {
  1103. int i;
  1104. struct dlm_ctxt *dlm = NULL;
  1105. dlm = kzalloc(sizeof(*dlm), GFP_KERNEL);
  1106. if (!dlm) {
  1107. mlog_errno(-ENOMEM);
  1108. goto leave;
  1109. }
  1110. dlm->name = kmalloc(strlen(domain) + 1, GFP_KERNEL);
  1111. if (dlm->name == NULL) {
  1112. mlog_errno(-ENOMEM);
  1113. kfree(dlm);
  1114. dlm = NULL;
  1115. goto leave;
  1116. }
  1117. dlm->lockres_hash = (struct hlist_head **)dlm_alloc_pagevec(DLM_HASH_PAGES);
  1118. if (!dlm->lockres_hash) {
  1119. mlog_errno(-ENOMEM);
  1120. kfree(dlm->name);
  1121. kfree(dlm);
  1122. dlm = NULL;
  1123. goto leave;
  1124. }
  1125. for (i = 0; i < DLM_HASH_BUCKETS; i++)
  1126. INIT_HLIST_HEAD(dlm_lockres_hash(dlm, i));
  1127. strcpy(dlm->name, domain);
  1128. dlm->key = key;
  1129. dlm->node_num = o2nm_this_node();
  1130. spin_lock_init(&dlm->spinlock);
  1131. spin_lock_init(&dlm->master_lock);
  1132. spin_lock_init(&dlm->ast_lock);
  1133. INIT_LIST_HEAD(&dlm->list);
  1134. INIT_LIST_HEAD(&dlm->dirty_list);
  1135. INIT_LIST_HEAD(&dlm->reco.resources);
  1136. INIT_LIST_HEAD(&dlm->reco.received);
  1137. INIT_LIST_HEAD(&dlm->reco.node_data);
  1138. INIT_LIST_HEAD(&dlm->purge_list);
  1139. INIT_LIST_HEAD(&dlm->dlm_domain_handlers);
  1140. dlm->reco.state = 0;
  1141. INIT_LIST_HEAD(&dlm->pending_asts);
  1142. INIT_LIST_HEAD(&dlm->pending_basts);
  1143. mlog(0, "dlm->recovery_map=%p, &(dlm->recovery_map[0])=%p\n",
  1144. dlm->recovery_map, &(dlm->recovery_map[0]));
  1145. memset(dlm->recovery_map, 0, sizeof(dlm->recovery_map));
  1146. memset(dlm->live_nodes_map, 0, sizeof(dlm->live_nodes_map));
  1147. memset(dlm->domain_map, 0, sizeof(dlm->domain_map));
  1148. dlm->dlm_thread_task = NULL;
  1149. dlm->dlm_reco_thread_task = NULL;
  1150. dlm->dlm_worker = NULL;
  1151. init_waitqueue_head(&dlm->dlm_thread_wq);
  1152. init_waitqueue_head(&dlm->dlm_reco_thread_wq);
  1153. init_waitqueue_head(&dlm->reco.event);
  1154. init_waitqueue_head(&dlm->ast_wq);
  1155. init_waitqueue_head(&dlm->migration_wq);
  1156. INIT_LIST_HEAD(&dlm->master_list);
  1157. INIT_LIST_HEAD(&dlm->mle_hb_events);
  1158. dlm->joining_node = DLM_LOCK_RES_OWNER_UNKNOWN;
  1159. init_waitqueue_head(&dlm->dlm_join_events);
  1160. dlm->reco.new_master = O2NM_INVALID_NODE_NUM;
  1161. dlm->reco.dead_node = O2NM_INVALID_NODE_NUM;
  1162. atomic_set(&dlm->local_resources, 0);
  1163. atomic_set(&dlm->remote_resources, 0);
  1164. atomic_set(&dlm->unknown_resources, 0);
  1165. spin_lock_init(&dlm->work_lock);
  1166. INIT_LIST_HEAD(&dlm->work_list);
  1167. INIT_WORK(&dlm->dispatched_work, dlm_dispatch_work);
  1168. kref_init(&dlm->dlm_refs);
  1169. dlm->dlm_state = DLM_CTXT_NEW;
  1170. INIT_LIST_HEAD(&dlm->dlm_eviction_callbacks);
  1171. mlog(0, "context init: refcount %u\n",
  1172. atomic_read(&dlm->dlm_refs.refcount));
  1173. leave:
  1174. return dlm;
  1175. }
  1176. /*
  1177. * dlm_register_domain: one-time setup per "domain"
  1178. */
  1179. struct dlm_ctxt * dlm_register_domain(const char *domain,
  1180. u32 key)
  1181. {
  1182. int ret;
  1183. struct dlm_ctxt *dlm = NULL;
  1184. struct dlm_ctxt *new_ctxt = NULL;
  1185. if (strlen(domain) > O2NM_MAX_NAME_LEN) {
  1186. ret = -ENAMETOOLONG;
  1187. mlog(ML_ERROR, "domain name length too long\n");
  1188. goto leave;
  1189. }
  1190. if (!o2hb_check_local_node_heartbeating()) {
  1191. mlog(ML_ERROR, "the local node has not been configured, or is "
  1192. "not heartbeating\n");
  1193. ret = -EPROTO;
  1194. goto leave;
  1195. }
  1196. mlog(0, "register called for domain \"%s\"\n", domain);
  1197. retry:
  1198. dlm = NULL;
  1199. if (signal_pending(current)) {
  1200. ret = -ERESTARTSYS;
  1201. mlog_errno(ret);
  1202. goto leave;
  1203. }
  1204. spin_lock(&dlm_domain_lock);
  1205. dlm = __dlm_lookup_domain(domain);
  1206. if (dlm) {
  1207. if (dlm->dlm_state != DLM_CTXT_JOINED) {
  1208. spin_unlock(&dlm_domain_lock);
  1209. mlog(0, "This ctxt is not joined yet!\n");
  1210. wait_event_interruptible(dlm_domain_events,
  1211. dlm_wait_on_domain_helper(
  1212. domain));
  1213. goto retry;
  1214. }
  1215. __dlm_get(dlm);
  1216. dlm->num_joins++;
  1217. spin_unlock(&dlm_domain_lock);
  1218. ret = 0;
  1219. goto leave;
  1220. }
  1221. /* doesn't exist */
  1222. if (!new_ctxt) {
  1223. spin_unlock(&dlm_domain_lock);
  1224. new_ctxt = dlm_alloc_ctxt(domain, key);
  1225. if (new_ctxt)
  1226. goto retry;
  1227. ret = -ENOMEM;
  1228. mlog_errno(ret);
  1229. goto leave;
  1230. }
  1231. /* a little variable switch-a-roo here... */
  1232. dlm = new_ctxt;
  1233. new_ctxt = NULL;
  1234. /* add the new domain */
  1235. list_add_tail(&dlm->list, &dlm_domains);
  1236. spin_unlock(&dlm_domain_lock);
  1237. ret = dlm_join_domain(dlm);
  1238. if (ret) {
  1239. mlog_errno(ret);
  1240. dlm_put(dlm);
  1241. goto leave;
  1242. }
  1243. ret = 0;
  1244. leave:
  1245. if (new_ctxt)
  1246. dlm_free_ctxt_mem(new_ctxt);
  1247. if (ret < 0)
  1248. dlm = ERR_PTR(ret);
  1249. return dlm;
  1250. }
  1251. EXPORT_SYMBOL_GPL(dlm_register_domain);
  1252. static LIST_HEAD(dlm_join_handlers);
  1253. static void dlm_unregister_net_handlers(void)
  1254. {
  1255. o2net_unregister_handler_list(&dlm_join_handlers);
  1256. }
  1257. static int dlm_register_net_handlers(void)
  1258. {
  1259. int status = 0;
  1260. status = o2net_register_handler(DLM_QUERY_JOIN_MSG, DLM_MOD_KEY,
  1261. sizeof(struct dlm_query_join_request),
  1262. dlm_query_join_handler,
  1263. NULL, NULL, &dlm_join_handlers);
  1264. if (status)
  1265. goto bail;
  1266. status = o2net_register_handler(DLM_ASSERT_JOINED_MSG, DLM_MOD_KEY,
  1267. sizeof(struct dlm_assert_joined),
  1268. dlm_assert_joined_handler,
  1269. NULL, NULL, &dlm_join_handlers);
  1270. if (status)
  1271. goto bail;
  1272. status = o2net_register_handler(DLM_CANCEL_JOIN_MSG, DLM_MOD_KEY,
  1273. sizeof(struct dlm_cancel_join),
  1274. dlm_cancel_join_handler,
  1275. NULL, NULL, &dlm_join_handlers);
  1276. bail:
  1277. if (status < 0)
  1278. dlm_unregister_net_handlers();
  1279. return status;
  1280. }
  1281. /* Domain eviction callback handling.
  1282. *
  1283. * The file system requires notification of node death *before* the
  1284. * dlm completes it's recovery work, otherwise it may be able to
  1285. * acquire locks on resources requiring recovery. Since the dlm can
  1286. * evict a node from it's domain *before* heartbeat fires, a similar
  1287. * mechanism is required. */
  1288. /* Eviction is not expected to happen often, so a per-domain lock is
  1289. * not necessary. Eviction callbacks are allowed to sleep for short
  1290. * periods of time. */
  1291. static DECLARE_RWSEM(dlm_callback_sem);
  1292. void dlm_fire_domain_eviction_callbacks(struct dlm_ctxt *dlm,
  1293. int node_num)
  1294. {
  1295. struct list_head *iter;
  1296. struct dlm_eviction_cb *cb;
  1297. down_read(&dlm_callback_sem);
  1298. list_for_each(iter, &dlm->dlm_eviction_callbacks) {
  1299. cb = list_entry(iter, struct dlm_eviction_cb, ec_item);
  1300. cb->ec_func(node_num, cb->ec_data);
  1301. }
  1302. up_read(&dlm_callback_sem);
  1303. }
  1304. void dlm_setup_eviction_cb(struct dlm_eviction_cb *cb,
  1305. dlm_eviction_func *f,
  1306. void *data)
  1307. {
  1308. INIT_LIST_HEAD(&cb->ec_item);
  1309. cb->ec_func = f;
  1310. cb->ec_data = data;
  1311. }
  1312. EXPORT_SYMBOL_GPL(dlm_setup_eviction_cb);
  1313. void dlm_register_eviction_cb(struct dlm_ctxt *dlm,
  1314. struct dlm_eviction_cb *cb)
  1315. {
  1316. down_write(&dlm_callback_sem);
  1317. list_add_tail(&cb->ec_item, &dlm->dlm_eviction_callbacks);
  1318. up_write(&dlm_callback_sem);
  1319. }
  1320. EXPORT_SYMBOL_GPL(dlm_register_eviction_cb);
  1321. void dlm_unregister_eviction_cb(struct dlm_eviction_cb *cb)
  1322. {
  1323. down_write(&dlm_callback_sem);
  1324. list_del_init(&cb->ec_item);
  1325. up_write(&dlm_callback_sem);
  1326. }
  1327. EXPORT_SYMBOL_GPL(dlm_unregister_eviction_cb);
  1328. static int __init dlm_init(void)
  1329. {
  1330. int status;
  1331. dlm_print_version();
  1332. status = dlm_init_mle_cache();
  1333. if (status)
  1334. return -1;
  1335. status = dlm_register_net_handlers();
  1336. if (status) {
  1337. dlm_destroy_mle_cache();
  1338. return -1;
  1339. }
  1340. return 0;
  1341. }
  1342. static void __exit dlm_exit (void)
  1343. {
  1344. dlm_unregister_net_handlers();
  1345. dlm_destroy_mle_cache();
  1346. }
  1347. MODULE_AUTHOR("Oracle");
  1348. MODULE_LICENSE("GPL");
  1349. module_init(dlm_init);
  1350. module_exit(dlm_exit);