dlmdomain.c 39 KB

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