dlmdomain.c 36 KB

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