dlmdomain.c 37 KB

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