dlmdomain.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527
  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. spinlock_t dlm_domain_lock = SPIN_LOCK_UNLOCKED;
  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. mlog(ML_NOTICE, "Nodes in my domain (\"%s\"):\n", dlm->name);
  330. while ((node = find_next_bit(dlm->domain_map, O2NM_MAX_NODES,
  331. node + 1)) < O2NM_MAX_NODES) {
  332. mlog(ML_NOTICE, " node %d\n", node);
  333. }
  334. }
  335. static int dlm_exit_domain_handler(struct o2net_msg *msg, u32 len, void *data)
  336. {
  337. struct dlm_ctxt *dlm = data;
  338. unsigned int node;
  339. struct dlm_exit_domain *exit_msg = (struct dlm_exit_domain *) msg->buf;
  340. mlog_entry("%p %u %p", msg, len, data);
  341. if (!dlm_grab(dlm))
  342. return 0;
  343. node = exit_msg->node_idx;
  344. mlog(0, "Node %u leaves domain %s\n", node, dlm->name);
  345. spin_lock(&dlm->spinlock);
  346. clear_bit(node, dlm->domain_map);
  347. __dlm_print_nodes(dlm);
  348. /* notify anything attached to the heartbeat events */
  349. dlm_hb_event_notify_attached(dlm, node, 0);
  350. spin_unlock(&dlm->spinlock);
  351. dlm_put(dlm);
  352. return 0;
  353. }
  354. static int dlm_send_one_domain_exit(struct dlm_ctxt *dlm,
  355. unsigned int node)
  356. {
  357. int status;
  358. struct dlm_exit_domain leave_msg;
  359. mlog(0, "Asking node %u if we can leave the domain %s me = %u\n",
  360. node, dlm->name, dlm->node_num);
  361. memset(&leave_msg, 0, sizeof(leave_msg));
  362. leave_msg.node_idx = dlm->node_num;
  363. status = o2net_send_message(DLM_EXIT_DOMAIN_MSG, dlm->key,
  364. &leave_msg, sizeof(leave_msg), node,
  365. NULL);
  366. mlog(0, "status return %d from o2net_send_message\n", status);
  367. return status;
  368. }
  369. static void dlm_leave_domain(struct dlm_ctxt *dlm)
  370. {
  371. int node, clear_node, status;
  372. /* At this point we've migrated away all our locks and won't
  373. * accept mastership of new ones. The dlm is responsible for
  374. * almost nothing now. We make sure not to confuse any joining
  375. * nodes and then commence shutdown procedure. */
  376. spin_lock(&dlm->spinlock);
  377. /* Clear ourselves from the domain map */
  378. clear_bit(dlm->node_num, dlm->domain_map);
  379. while ((node = find_next_bit(dlm->domain_map, O2NM_MAX_NODES,
  380. 0)) < O2NM_MAX_NODES) {
  381. /* Drop the dlm spinlock. This is safe wrt the domain_map.
  382. * -nodes cannot be added now as the
  383. * query_join_handlers knows to respond with OK_NO_MAP
  384. * -we catch the right network errors if a node is
  385. * removed from the map while we're sending him the
  386. * exit message. */
  387. spin_unlock(&dlm->spinlock);
  388. clear_node = 1;
  389. status = dlm_send_one_domain_exit(dlm, node);
  390. if (status < 0 &&
  391. status != -ENOPROTOOPT &&
  392. status != -ENOTCONN) {
  393. mlog(ML_NOTICE, "Error %d sending domain exit message "
  394. "to node %d\n", status, node);
  395. /* Not sure what to do here but lets sleep for
  396. * a bit in case this was a transient
  397. * error... */
  398. msleep(DLM_DOMAIN_BACKOFF_MS);
  399. clear_node = 0;
  400. }
  401. spin_lock(&dlm->spinlock);
  402. /* If we're not clearing the node bit then we intend
  403. * to loop back around to try again. */
  404. if (clear_node)
  405. clear_bit(node, dlm->domain_map);
  406. }
  407. spin_unlock(&dlm->spinlock);
  408. }
  409. int dlm_joined(struct dlm_ctxt *dlm)
  410. {
  411. int ret = 0;
  412. spin_lock(&dlm_domain_lock);
  413. if (dlm->dlm_state == DLM_CTXT_JOINED)
  414. ret = 1;
  415. spin_unlock(&dlm_domain_lock);
  416. return ret;
  417. }
  418. int dlm_shutting_down(struct dlm_ctxt *dlm)
  419. {
  420. int ret = 0;
  421. spin_lock(&dlm_domain_lock);
  422. if (dlm->dlm_state == DLM_CTXT_IN_SHUTDOWN)
  423. ret = 1;
  424. spin_unlock(&dlm_domain_lock);
  425. return ret;
  426. }
  427. void dlm_unregister_domain(struct dlm_ctxt *dlm)
  428. {
  429. int leave = 0;
  430. spin_lock(&dlm_domain_lock);
  431. BUG_ON(dlm->dlm_state != DLM_CTXT_JOINED);
  432. BUG_ON(!dlm->num_joins);
  433. dlm->num_joins--;
  434. if (!dlm->num_joins) {
  435. /* We mark it "in shutdown" now so new register
  436. * requests wait until we've completely left the
  437. * domain. Don't use DLM_CTXT_LEAVING yet as we still
  438. * want new domain joins to communicate with us at
  439. * least until we've completed migration of our
  440. * resources. */
  441. dlm->dlm_state = DLM_CTXT_IN_SHUTDOWN;
  442. leave = 1;
  443. }
  444. spin_unlock(&dlm_domain_lock);
  445. if (leave) {
  446. mlog(0, "shutting down domain %s\n", dlm->name);
  447. /* We changed dlm state, notify the thread */
  448. dlm_kick_thread(dlm, NULL);
  449. dlm_migrate_all_locks(dlm);
  450. dlm_mark_domain_leaving(dlm);
  451. dlm_leave_domain(dlm);
  452. dlm_complete_dlm_shutdown(dlm);
  453. }
  454. dlm_put(dlm);
  455. }
  456. EXPORT_SYMBOL_GPL(dlm_unregister_domain);
  457. static int dlm_query_join_handler(struct o2net_msg *msg, u32 len, void *data)
  458. {
  459. struct dlm_query_join_request *query;
  460. enum dlm_query_join_response response;
  461. struct dlm_ctxt *dlm = NULL;
  462. query = (struct dlm_query_join_request *) msg->buf;
  463. mlog(0, "node %u wants to join domain %s\n", query->node_idx,
  464. query->domain);
  465. /*
  466. * If heartbeat doesn't consider the node live, tell it
  467. * to back off and try again. This gives heartbeat a chance
  468. * to catch up.
  469. */
  470. if (!o2hb_check_node_heartbeating(query->node_idx)) {
  471. mlog(0, "node %u is not in our live map yet\n",
  472. query->node_idx);
  473. response = JOIN_DISALLOW;
  474. goto respond;
  475. }
  476. response = JOIN_OK_NO_MAP;
  477. spin_lock(&dlm_domain_lock);
  478. dlm = __dlm_lookup_domain_full(query->domain, query->name_len);
  479. /* Once the dlm ctxt is marked as leaving then we don't want
  480. * to be put in someone's domain map.
  481. * Also, explicitly disallow joining at certain troublesome
  482. * times (ie. during recovery). */
  483. if (dlm && dlm->dlm_state != DLM_CTXT_LEAVING) {
  484. int bit = query->node_idx;
  485. spin_lock(&dlm->spinlock);
  486. if (dlm->dlm_state == DLM_CTXT_NEW &&
  487. dlm->joining_node == DLM_LOCK_RES_OWNER_UNKNOWN) {
  488. /*If this is a brand new context and we
  489. * haven't started our join process yet, then
  490. * the other node won the race. */
  491. response = JOIN_OK_NO_MAP;
  492. } else if (dlm->joining_node != DLM_LOCK_RES_OWNER_UNKNOWN) {
  493. /* Disallow parallel joins. */
  494. response = JOIN_DISALLOW;
  495. } else if (dlm->reco.state & DLM_RECO_STATE_ACTIVE) {
  496. mlog(ML_NOTICE, "node %u trying to join, but recovery "
  497. "is ongoing.\n", bit);
  498. response = JOIN_DISALLOW;
  499. } else if (test_bit(bit, dlm->recovery_map)) {
  500. mlog(ML_NOTICE, "node %u trying to join, but it "
  501. "still needs recovery.\n", bit);
  502. response = JOIN_DISALLOW;
  503. } else if (test_bit(bit, dlm->domain_map)) {
  504. mlog(ML_NOTICE, "node %u trying to join, but it "
  505. "is still in the domain! needs recovery?\n",
  506. bit);
  507. response = JOIN_DISALLOW;
  508. } else {
  509. /* Alright we're fully a part of this domain
  510. * so we keep some state as to who's joining
  511. * and indicate to him that needs to be fixed
  512. * up. */
  513. response = JOIN_OK;
  514. __dlm_set_joining_node(dlm, query->node_idx);
  515. }
  516. spin_unlock(&dlm->spinlock);
  517. }
  518. spin_unlock(&dlm_domain_lock);
  519. respond:
  520. mlog(0, "We respond with %u\n", response);
  521. return response;
  522. }
  523. static int dlm_assert_joined_handler(struct o2net_msg *msg, u32 len, void *data)
  524. {
  525. struct dlm_assert_joined *assert;
  526. struct dlm_ctxt *dlm = NULL;
  527. assert = (struct dlm_assert_joined *) msg->buf;
  528. mlog(0, "node %u asserts join on domain %s\n", assert->node_idx,
  529. assert->domain);
  530. spin_lock(&dlm_domain_lock);
  531. dlm = __dlm_lookup_domain_full(assert->domain, assert->name_len);
  532. /* XXX should we consider no dlm ctxt an error? */
  533. if (dlm) {
  534. spin_lock(&dlm->spinlock);
  535. /* Alright, this node has officially joined our
  536. * domain. Set him in the map and clean up our
  537. * leftover join state. */
  538. BUG_ON(dlm->joining_node != assert->node_idx);
  539. set_bit(assert->node_idx, dlm->domain_map);
  540. __dlm_set_joining_node(dlm, DLM_LOCK_RES_OWNER_UNKNOWN);
  541. __dlm_print_nodes(dlm);
  542. /* notify anything attached to the heartbeat events */
  543. dlm_hb_event_notify_attached(dlm, assert->node_idx, 1);
  544. spin_unlock(&dlm->spinlock);
  545. }
  546. spin_unlock(&dlm_domain_lock);
  547. return 0;
  548. }
  549. static int dlm_cancel_join_handler(struct o2net_msg *msg, u32 len, void *data)
  550. {
  551. struct dlm_cancel_join *cancel;
  552. struct dlm_ctxt *dlm = NULL;
  553. cancel = (struct dlm_cancel_join *) msg->buf;
  554. mlog(0, "node %u cancels join on domain %s\n", cancel->node_idx,
  555. cancel->domain);
  556. spin_lock(&dlm_domain_lock);
  557. dlm = __dlm_lookup_domain_full(cancel->domain, cancel->name_len);
  558. if (dlm) {
  559. spin_lock(&dlm->spinlock);
  560. /* Yikes, this guy wants to cancel his join. No
  561. * problem, we simply cleanup our join state. */
  562. BUG_ON(dlm->joining_node != cancel->node_idx);
  563. __dlm_set_joining_node(dlm, DLM_LOCK_RES_OWNER_UNKNOWN);
  564. spin_unlock(&dlm->spinlock);
  565. }
  566. spin_unlock(&dlm_domain_lock);
  567. return 0;
  568. }
  569. static int dlm_send_one_join_cancel(struct dlm_ctxt *dlm,
  570. unsigned int node)
  571. {
  572. int status;
  573. struct dlm_cancel_join cancel_msg;
  574. memset(&cancel_msg, 0, sizeof(cancel_msg));
  575. cancel_msg.node_idx = dlm->node_num;
  576. cancel_msg.name_len = strlen(dlm->name);
  577. memcpy(cancel_msg.domain, dlm->name, cancel_msg.name_len);
  578. status = o2net_send_message(DLM_CANCEL_JOIN_MSG, DLM_MOD_KEY,
  579. &cancel_msg, sizeof(cancel_msg), node,
  580. NULL);
  581. if (status < 0) {
  582. mlog_errno(status);
  583. goto bail;
  584. }
  585. bail:
  586. return status;
  587. }
  588. /* map_size should be in bytes. */
  589. static int dlm_send_join_cancels(struct dlm_ctxt *dlm,
  590. unsigned long *node_map,
  591. unsigned int map_size)
  592. {
  593. int status, tmpstat;
  594. unsigned int node;
  595. if (map_size != (BITS_TO_LONGS(O2NM_MAX_NODES) *
  596. sizeof(unsigned long))) {
  597. mlog(ML_ERROR,
  598. "map_size %u != BITS_TO_LONGS(O2NM_MAX_NODES) %u\n",
  599. map_size, BITS_TO_LONGS(O2NM_MAX_NODES));
  600. return -EINVAL;
  601. }
  602. status = 0;
  603. node = -1;
  604. while ((node = find_next_bit(node_map, O2NM_MAX_NODES,
  605. node + 1)) < O2NM_MAX_NODES) {
  606. if (node == dlm->node_num)
  607. continue;
  608. tmpstat = dlm_send_one_join_cancel(dlm, node);
  609. if (tmpstat) {
  610. mlog(ML_ERROR, "Error return %d cancelling join on "
  611. "node %d\n", tmpstat, node);
  612. if (!status)
  613. status = tmpstat;
  614. }
  615. }
  616. if (status)
  617. mlog_errno(status);
  618. return status;
  619. }
  620. static int dlm_request_join(struct dlm_ctxt *dlm,
  621. int node,
  622. enum dlm_query_join_response *response)
  623. {
  624. int status, retval;
  625. struct dlm_query_join_request join_msg;
  626. mlog(0, "querying node %d\n", node);
  627. memset(&join_msg, 0, sizeof(join_msg));
  628. join_msg.node_idx = dlm->node_num;
  629. join_msg.name_len = strlen(dlm->name);
  630. memcpy(join_msg.domain, dlm->name, join_msg.name_len);
  631. status = o2net_send_message(DLM_QUERY_JOIN_MSG, DLM_MOD_KEY, &join_msg,
  632. sizeof(join_msg), node, &retval);
  633. if (status < 0 && status != -ENOPROTOOPT) {
  634. mlog_errno(status);
  635. goto bail;
  636. }
  637. /* -ENOPROTOOPT from the net code means the other side isn't
  638. listening for our message type -- that's fine, it means
  639. his dlm isn't up, so we can consider him a 'yes' but not
  640. joined into the domain. */
  641. if (status == -ENOPROTOOPT) {
  642. status = 0;
  643. *response = JOIN_OK_NO_MAP;
  644. } else if (retval == JOIN_DISALLOW ||
  645. retval == JOIN_OK ||
  646. retval == JOIN_OK_NO_MAP) {
  647. *response = retval;
  648. } else {
  649. status = -EINVAL;
  650. mlog(ML_ERROR, "invalid response %d from node %u\n", retval,
  651. node);
  652. }
  653. mlog(0, "status %d, node %d response is %d\n", status, node,
  654. *response);
  655. bail:
  656. return status;
  657. }
  658. static int dlm_send_one_join_assert(struct dlm_ctxt *dlm,
  659. unsigned int node)
  660. {
  661. int status;
  662. struct dlm_assert_joined assert_msg;
  663. mlog(0, "Sending join assert to node %u\n", node);
  664. memset(&assert_msg, 0, sizeof(assert_msg));
  665. assert_msg.node_idx = dlm->node_num;
  666. assert_msg.name_len = strlen(dlm->name);
  667. memcpy(assert_msg.domain, dlm->name, assert_msg.name_len);
  668. status = o2net_send_message(DLM_ASSERT_JOINED_MSG, DLM_MOD_KEY,
  669. &assert_msg, sizeof(assert_msg), node,
  670. NULL);
  671. if (status < 0)
  672. mlog_errno(status);
  673. return status;
  674. }
  675. static void dlm_send_join_asserts(struct dlm_ctxt *dlm,
  676. unsigned long *node_map)
  677. {
  678. int status, node, live;
  679. status = 0;
  680. node = -1;
  681. while ((node = find_next_bit(node_map, O2NM_MAX_NODES,
  682. node + 1)) < O2NM_MAX_NODES) {
  683. if (node == dlm->node_num)
  684. continue;
  685. do {
  686. /* It is very important that this message be
  687. * received so we spin until either the node
  688. * has died or it gets the message. */
  689. status = dlm_send_one_join_assert(dlm, node);
  690. spin_lock(&dlm->spinlock);
  691. live = test_bit(node, dlm->live_nodes_map);
  692. spin_unlock(&dlm->spinlock);
  693. if (status) {
  694. mlog(ML_ERROR, "Error return %d asserting "
  695. "join on node %d\n", status, node);
  696. /* give us some time between errors... */
  697. if (live)
  698. msleep(DLM_DOMAIN_BACKOFF_MS);
  699. }
  700. } while (status && live);
  701. }
  702. }
  703. struct domain_join_ctxt {
  704. unsigned long live_map[BITS_TO_LONGS(O2NM_MAX_NODES)];
  705. unsigned long yes_resp_map[BITS_TO_LONGS(O2NM_MAX_NODES)];
  706. };
  707. static int dlm_should_restart_join(struct dlm_ctxt *dlm,
  708. struct domain_join_ctxt *ctxt,
  709. enum dlm_query_join_response response)
  710. {
  711. int ret;
  712. if (response == JOIN_DISALLOW) {
  713. mlog(0, "Latest response of disallow -- should restart\n");
  714. return 1;
  715. }
  716. spin_lock(&dlm->spinlock);
  717. /* For now, we restart the process if the node maps have
  718. * changed at all */
  719. ret = memcmp(ctxt->live_map, dlm->live_nodes_map,
  720. sizeof(dlm->live_nodes_map));
  721. spin_unlock(&dlm->spinlock);
  722. if (ret)
  723. mlog(0, "Node maps changed -- should restart\n");
  724. return ret;
  725. }
  726. static int dlm_try_to_join_domain(struct dlm_ctxt *dlm)
  727. {
  728. int status = 0, tmpstat, node;
  729. struct domain_join_ctxt *ctxt;
  730. enum dlm_query_join_response response;
  731. mlog_entry("%p", dlm);
  732. ctxt = kcalloc(1, sizeof(*ctxt), GFP_KERNEL);
  733. if (!ctxt) {
  734. status = -ENOMEM;
  735. mlog_errno(status);
  736. goto bail;
  737. }
  738. /* group sem locking should work for us here -- we're already
  739. * registered for heartbeat events so filling this should be
  740. * atomic wrt getting those handlers called. */
  741. o2hb_fill_node_map(dlm->live_nodes_map, sizeof(dlm->live_nodes_map));
  742. spin_lock(&dlm->spinlock);
  743. memcpy(ctxt->live_map, dlm->live_nodes_map, sizeof(ctxt->live_map));
  744. __dlm_set_joining_node(dlm, dlm->node_num);
  745. spin_unlock(&dlm->spinlock);
  746. node = -1;
  747. while ((node = find_next_bit(ctxt->live_map, O2NM_MAX_NODES,
  748. node + 1)) < O2NM_MAX_NODES) {
  749. if (node == dlm->node_num)
  750. continue;
  751. status = dlm_request_join(dlm, node, &response);
  752. if (status < 0) {
  753. mlog_errno(status);
  754. goto bail;
  755. }
  756. /* Ok, either we got a response or the node doesn't have a
  757. * dlm up. */
  758. if (response == JOIN_OK)
  759. set_bit(node, ctxt->yes_resp_map);
  760. if (dlm_should_restart_join(dlm, ctxt, response)) {
  761. status = -EAGAIN;
  762. goto bail;
  763. }
  764. }
  765. mlog(0, "Yay, done querying nodes!\n");
  766. /* Yay, everyone agree's we can join the domain. My domain is
  767. * comprised of all nodes who were put in the
  768. * yes_resp_map. Copy that into our domain map and send a join
  769. * assert message to clean up everyone elses state. */
  770. spin_lock(&dlm->spinlock);
  771. memcpy(dlm->domain_map, ctxt->yes_resp_map,
  772. sizeof(ctxt->yes_resp_map));
  773. set_bit(dlm->node_num, dlm->domain_map);
  774. spin_unlock(&dlm->spinlock);
  775. dlm_send_join_asserts(dlm, ctxt->yes_resp_map);
  776. /* Joined state *must* be set before the joining node
  777. * information, otherwise the query_join handler may read no
  778. * current joiner but a state of NEW and tell joining nodes
  779. * we're not in the domain. */
  780. spin_lock(&dlm_domain_lock);
  781. dlm->dlm_state = DLM_CTXT_JOINED;
  782. dlm->num_joins++;
  783. spin_unlock(&dlm_domain_lock);
  784. bail:
  785. spin_lock(&dlm->spinlock);
  786. __dlm_set_joining_node(dlm, DLM_LOCK_RES_OWNER_UNKNOWN);
  787. if (!status)
  788. __dlm_print_nodes(dlm);
  789. spin_unlock(&dlm->spinlock);
  790. if (ctxt) {
  791. /* Do we need to send a cancel message to any nodes? */
  792. if (status < 0) {
  793. tmpstat = dlm_send_join_cancels(dlm,
  794. ctxt->yes_resp_map,
  795. sizeof(ctxt->yes_resp_map));
  796. if (tmpstat < 0)
  797. mlog_errno(tmpstat);
  798. }
  799. kfree(ctxt);
  800. }
  801. mlog(0, "returning %d\n", status);
  802. return status;
  803. }
  804. static void dlm_unregister_domain_handlers(struct dlm_ctxt *dlm)
  805. {
  806. o2hb_unregister_callback(&dlm->dlm_hb_up);
  807. o2hb_unregister_callback(&dlm->dlm_hb_down);
  808. o2net_unregister_handler_list(&dlm->dlm_domain_handlers);
  809. }
  810. static int dlm_register_domain_handlers(struct dlm_ctxt *dlm)
  811. {
  812. int status;
  813. mlog(0, "registering handlers.\n");
  814. o2hb_setup_callback(&dlm->dlm_hb_down, O2HB_NODE_DOWN_CB,
  815. dlm_hb_node_down_cb, dlm, DLM_HB_NODE_DOWN_PRI);
  816. status = o2hb_register_callback(&dlm->dlm_hb_down);
  817. if (status)
  818. goto bail;
  819. o2hb_setup_callback(&dlm->dlm_hb_up, O2HB_NODE_UP_CB,
  820. dlm_hb_node_up_cb, dlm, DLM_HB_NODE_UP_PRI);
  821. status = o2hb_register_callback(&dlm->dlm_hb_up);
  822. if (status)
  823. goto bail;
  824. status = o2net_register_handler(DLM_MASTER_REQUEST_MSG, dlm->key,
  825. sizeof(struct dlm_master_request),
  826. dlm_master_request_handler,
  827. dlm, &dlm->dlm_domain_handlers);
  828. if (status)
  829. goto bail;
  830. status = o2net_register_handler(DLM_ASSERT_MASTER_MSG, dlm->key,
  831. sizeof(struct dlm_assert_master),
  832. dlm_assert_master_handler,
  833. dlm, &dlm->dlm_domain_handlers);
  834. if (status)
  835. goto bail;
  836. status = o2net_register_handler(DLM_CREATE_LOCK_MSG, dlm->key,
  837. sizeof(struct dlm_create_lock),
  838. dlm_create_lock_handler,
  839. dlm, &dlm->dlm_domain_handlers);
  840. if (status)
  841. goto bail;
  842. status = o2net_register_handler(DLM_CONVERT_LOCK_MSG, dlm->key,
  843. DLM_CONVERT_LOCK_MAX_LEN,
  844. dlm_convert_lock_handler,
  845. dlm, &dlm->dlm_domain_handlers);
  846. if (status)
  847. goto bail;
  848. status = o2net_register_handler(DLM_UNLOCK_LOCK_MSG, dlm->key,
  849. DLM_UNLOCK_LOCK_MAX_LEN,
  850. dlm_unlock_lock_handler,
  851. dlm, &dlm->dlm_domain_handlers);
  852. if (status)
  853. goto bail;
  854. status = o2net_register_handler(DLM_PROXY_AST_MSG, dlm->key,
  855. DLM_PROXY_AST_MAX_LEN,
  856. dlm_proxy_ast_handler,
  857. dlm, &dlm->dlm_domain_handlers);
  858. if (status)
  859. goto bail;
  860. status = o2net_register_handler(DLM_EXIT_DOMAIN_MSG, dlm->key,
  861. sizeof(struct dlm_exit_domain),
  862. dlm_exit_domain_handler,
  863. dlm, &dlm->dlm_domain_handlers);
  864. if (status)
  865. goto bail;
  866. status = o2net_register_handler(DLM_MIGRATE_REQUEST_MSG, dlm->key,
  867. sizeof(struct dlm_migrate_request),
  868. dlm_migrate_request_handler,
  869. dlm, &dlm->dlm_domain_handlers);
  870. if (status)
  871. goto bail;
  872. status = o2net_register_handler(DLM_MIG_LOCKRES_MSG, dlm->key,
  873. DLM_MIG_LOCKRES_MAX_LEN,
  874. dlm_mig_lockres_handler,
  875. dlm, &dlm->dlm_domain_handlers);
  876. if (status)
  877. goto bail;
  878. status = o2net_register_handler(DLM_MASTER_REQUERY_MSG, dlm->key,
  879. sizeof(struct dlm_master_requery),
  880. dlm_master_requery_handler,
  881. dlm, &dlm->dlm_domain_handlers);
  882. if (status)
  883. goto bail;
  884. status = o2net_register_handler(DLM_LOCK_REQUEST_MSG, dlm->key,
  885. sizeof(struct dlm_lock_request),
  886. dlm_request_all_locks_handler,
  887. dlm, &dlm->dlm_domain_handlers);
  888. if (status)
  889. goto bail;
  890. status = o2net_register_handler(DLM_RECO_DATA_DONE_MSG, dlm->key,
  891. sizeof(struct dlm_reco_data_done),
  892. dlm_reco_data_done_handler,
  893. dlm, &dlm->dlm_domain_handlers);
  894. if (status)
  895. goto bail;
  896. status = o2net_register_handler(DLM_BEGIN_RECO_MSG, dlm->key,
  897. sizeof(struct dlm_begin_reco),
  898. dlm_begin_reco_handler,
  899. dlm, &dlm->dlm_domain_handlers);
  900. if (status)
  901. goto bail;
  902. status = o2net_register_handler(DLM_FINALIZE_RECO_MSG, dlm->key,
  903. sizeof(struct dlm_finalize_reco),
  904. dlm_finalize_reco_handler,
  905. dlm, &dlm->dlm_domain_handlers);
  906. if (status)
  907. goto bail;
  908. bail:
  909. if (status)
  910. dlm_unregister_domain_handlers(dlm);
  911. return status;
  912. }
  913. static int dlm_join_domain(struct dlm_ctxt *dlm)
  914. {
  915. int status;
  916. BUG_ON(!dlm);
  917. mlog(0, "Join domain %s\n", dlm->name);
  918. status = dlm_register_domain_handlers(dlm);
  919. if (status) {
  920. mlog_errno(status);
  921. goto bail;
  922. }
  923. status = dlm_launch_thread(dlm);
  924. if (status < 0) {
  925. mlog_errno(status);
  926. goto bail;
  927. }
  928. status = dlm_launch_recovery_thread(dlm);
  929. if (status < 0) {
  930. mlog_errno(status);
  931. goto bail;
  932. }
  933. dlm->dlm_worker = create_singlethread_workqueue("dlm_wq");
  934. if (!dlm->dlm_worker) {
  935. status = -ENOMEM;
  936. mlog_errno(status);
  937. goto bail;
  938. }
  939. do {
  940. unsigned int backoff;
  941. status = dlm_try_to_join_domain(dlm);
  942. /* If we're racing another node to the join, then we
  943. * need to back off temporarily and let them
  944. * complete. */
  945. if (status == -EAGAIN) {
  946. if (signal_pending(current)) {
  947. status = -ERESTARTSYS;
  948. goto bail;
  949. }
  950. /*
  951. * <chip> After you!
  952. * <dale> No, after you!
  953. * <chip> I insist!
  954. * <dale> But you first!
  955. * ...
  956. */
  957. backoff = (unsigned int)(jiffies & 0x3);
  958. backoff *= DLM_DOMAIN_BACKOFF_MS;
  959. mlog(0, "backoff %d\n", backoff);
  960. msleep(backoff);
  961. }
  962. } while (status == -EAGAIN);
  963. if (status < 0) {
  964. mlog_errno(status);
  965. goto bail;
  966. }
  967. status = 0;
  968. bail:
  969. wake_up(&dlm_domain_events);
  970. if (status) {
  971. dlm_unregister_domain_handlers(dlm);
  972. dlm_complete_thread(dlm);
  973. dlm_complete_recovery_thread(dlm);
  974. dlm_destroy_dlm_worker(dlm);
  975. }
  976. return status;
  977. }
  978. static struct dlm_ctxt *dlm_alloc_ctxt(const char *domain,
  979. u32 key)
  980. {
  981. int i;
  982. struct dlm_ctxt *dlm = NULL;
  983. dlm = kcalloc(1, sizeof(*dlm), GFP_KERNEL);
  984. if (!dlm) {
  985. mlog_errno(-ENOMEM);
  986. goto leave;
  987. }
  988. dlm->name = kmalloc(strlen(domain) + 1, GFP_KERNEL);
  989. if (dlm->name == NULL) {
  990. mlog_errno(-ENOMEM);
  991. kfree(dlm);
  992. dlm = NULL;
  993. goto leave;
  994. }
  995. dlm->lockres_hash = (struct hlist_head **)dlm_alloc_pagevec(DLM_HASH_PAGES);
  996. if (!dlm->lockres_hash) {
  997. mlog_errno(-ENOMEM);
  998. kfree(dlm->name);
  999. kfree(dlm);
  1000. dlm = NULL;
  1001. goto leave;
  1002. }
  1003. for (i = 0; i < DLM_HASH_BUCKETS; i++)
  1004. INIT_HLIST_HEAD(dlm_lockres_hash(dlm, i));
  1005. strcpy(dlm->name, domain);
  1006. dlm->key = key;
  1007. dlm->node_num = o2nm_this_node();
  1008. spin_lock_init(&dlm->spinlock);
  1009. spin_lock_init(&dlm->master_lock);
  1010. spin_lock_init(&dlm->ast_lock);
  1011. INIT_LIST_HEAD(&dlm->list);
  1012. INIT_LIST_HEAD(&dlm->dirty_list);
  1013. INIT_LIST_HEAD(&dlm->reco.resources);
  1014. INIT_LIST_HEAD(&dlm->reco.received);
  1015. INIT_LIST_HEAD(&dlm->reco.node_data);
  1016. INIT_LIST_HEAD(&dlm->purge_list);
  1017. INIT_LIST_HEAD(&dlm->dlm_domain_handlers);
  1018. dlm->reco.state = 0;
  1019. INIT_LIST_HEAD(&dlm->pending_asts);
  1020. INIT_LIST_HEAD(&dlm->pending_basts);
  1021. mlog(0, "dlm->recovery_map=%p, &(dlm->recovery_map[0])=%p\n",
  1022. dlm->recovery_map, &(dlm->recovery_map[0]));
  1023. memset(dlm->recovery_map, 0, sizeof(dlm->recovery_map));
  1024. memset(dlm->live_nodes_map, 0, sizeof(dlm->live_nodes_map));
  1025. memset(dlm->domain_map, 0, sizeof(dlm->domain_map));
  1026. dlm->dlm_thread_task = NULL;
  1027. dlm->dlm_reco_thread_task = NULL;
  1028. dlm->dlm_worker = NULL;
  1029. init_waitqueue_head(&dlm->dlm_thread_wq);
  1030. init_waitqueue_head(&dlm->dlm_reco_thread_wq);
  1031. init_waitqueue_head(&dlm->reco.event);
  1032. init_waitqueue_head(&dlm->ast_wq);
  1033. init_waitqueue_head(&dlm->migration_wq);
  1034. INIT_LIST_HEAD(&dlm->master_list);
  1035. INIT_LIST_HEAD(&dlm->mle_hb_events);
  1036. dlm->joining_node = DLM_LOCK_RES_OWNER_UNKNOWN;
  1037. init_waitqueue_head(&dlm->dlm_join_events);
  1038. dlm->reco.new_master = O2NM_INVALID_NODE_NUM;
  1039. dlm->reco.dead_node = O2NM_INVALID_NODE_NUM;
  1040. atomic_set(&dlm->local_resources, 0);
  1041. atomic_set(&dlm->remote_resources, 0);
  1042. atomic_set(&dlm->unknown_resources, 0);
  1043. spin_lock_init(&dlm->work_lock);
  1044. INIT_LIST_HEAD(&dlm->work_list);
  1045. INIT_WORK(&dlm->dispatched_work, dlm_dispatch_work, dlm);
  1046. kref_init(&dlm->dlm_refs);
  1047. dlm->dlm_state = DLM_CTXT_NEW;
  1048. INIT_LIST_HEAD(&dlm->dlm_eviction_callbacks);
  1049. mlog(0, "context init: refcount %u\n",
  1050. atomic_read(&dlm->dlm_refs.refcount));
  1051. leave:
  1052. return dlm;
  1053. }
  1054. /*
  1055. * dlm_register_domain: one-time setup per "domain"
  1056. */
  1057. struct dlm_ctxt * dlm_register_domain(const char *domain,
  1058. u32 key)
  1059. {
  1060. int ret;
  1061. struct dlm_ctxt *dlm = NULL;
  1062. struct dlm_ctxt *new_ctxt = NULL;
  1063. if (strlen(domain) > O2NM_MAX_NAME_LEN) {
  1064. ret = -ENAMETOOLONG;
  1065. mlog(ML_ERROR, "domain name length too long\n");
  1066. goto leave;
  1067. }
  1068. if (!o2hb_check_local_node_heartbeating()) {
  1069. mlog(ML_ERROR, "the local node has not been configured, or is "
  1070. "not heartbeating\n");
  1071. ret = -EPROTO;
  1072. goto leave;
  1073. }
  1074. mlog(0, "register called for domain \"%s\"\n", domain);
  1075. retry:
  1076. dlm = NULL;
  1077. if (signal_pending(current)) {
  1078. ret = -ERESTARTSYS;
  1079. mlog_errno(ret);
  1080. goto leave;
  1081. }
  1082. spin_lock(&dlm_domain_lock);
  1083. dlm = __dlm_lookup_domain(domain);
  1084. if (dlm) {
  1085. if (dlm->dlm_state != DLM_CTXT_JOINED) {
  1086. spin_unlock(&dlm_domain_lock);
  1087. mlog(0, "This ctxt is not joined yet!\n");
  1088. wait_event_interruptible(dlm_domain_events,
  1089. dlm_wait_on_domain_helper(
  1090. domain));
  1091. goto retry;
  1092. }
  1093. __dlm_get(dlm);
  1094. dlm->num_joins++;
  1095. spin_unlock(&dlm_domain_lock);
  1096. ret = 0;
  1097. goto leave;
  1098. }
  1099. /* doesn't exist */
  1100. if (!new_ctxt) {
  1101. spin_unlock(&dlm_domain_lock);
  1102. new_ctxt = dlm_alloc_ctxt(domain, key);
  1103. if (new_ctxt)
  1104. goto retry;
  1105. ret = -ENOMEM;
  1106. mlog_errno(ret);
  1107. goto leave;
  1108. }
  1109. /* a little variable switch-a-roo here... */
  1110. dlm = new_ctxt;
  1111. new_ctxt = NULL;
  1112. /* add the new domain */
  1113. list_add_tail(&dlm->list, &dlm_domains);
  1114. spin_unlock(&dlm_domain_lock);
  1115. ret = dlm_join_domain(dlm);
  1116. if (ret) {
  1117. mlog_errno(ret);
  1118. dlm_put(dlm);
  1119. goto leave;
  1120. }
  1121. ret = 0;
  1122. leave:
  1123. if (new_ctxt)
  1124. dlm_free_ctxt_mem(new_ctxt);
  1125. if (ret < 0)
  1126. dlm = ERR_PTR(ret);
  1127. return dlm;
  1128. }
  1129. EXPORT_SYMBOL_GPL(dlm_register_domain);
  1130. static LIST_HEAD(dlm_join_handlers);
  1131. static void dlm_unregister_net_handlers(void)
  1132. {
  1133. o2net_unregister_handler_list(&dlm_join_handlers);
  1134. }
  1135. static int dlm_register_net_handlers(void)
  1136. {
  1137. int status = 0;
  1138. status = o2net_register_handler(DLM_QUERY_JOIN_MSG, DLM_MOD_KEY,
  1139. sizeof(struct dlm_query_join_request),
  1140. dlm_query_join_handler,
  1141. NULL, &dlm_join_handlers);
  1142. if (status)
  1143. goto bail;
  1144. status = o2net_register_handler(DLM_ASSERT_JOINED_MSG, DLM_MOD_KEY,
  1145. sizeof(struct dlm_assert_joined),
  1146. dlm_assert_joined_handler,
  1147. NULL, &dlm_join_handlers);
  1148. if (status)
  1149. goto bail;
  1150. status = o2net_register_handler(DLM_CANCEL_JOIN_MSG, DLM_MOD_KEY,
  1151. sizeof(struct dlm_cancel_join),
  1152. dlm_cancel_join_handler,
  1153. NULL, &dlm_join_handlers);
  1154. bail:
  1155. if (status < 0)
  1156. dlm_unregister_net_handlers();
  1157. return status;
  1158. }
  1159. /* Domain eviction callback handling.
  1160. *
  1161. * The file system requires notification of node death *before* the
  1162. * dlm completes it's recovery work, otherwise it may be able to
  1163. * acquire locks on resources requiring recovery. Since the dlm can
  1164. * evict a node from it's domain *before* heartbeat fires, a similar
  1165. * mechanism is required. */
  1166. /* Eviction is not expected to happen often, so a per-domain lock is
  1167. * not necessary. Eviction callbacks are allowed to sleep for short
  1168. * periods of time. */
  1169. static DECLARE_RWSEM(dlm_callback_sem);
  1170. void dlm_fire_domain_eviction_callbacks(struct dlm_ctxt *dlm,
  1171. int node_num)
  1172. {
  1173. struct list_head *iter;
  1174. struct dlm_eviction_cb *cb;
  1175. down_read(&dlm_callback_sem);
  1176. list_for_each(iter, &dlm->dlm_eviction_callbacks) {
  1177. cb = list_entry(iter, struct dlm_eviction_cb, ec_item);
  1178. cb->ec_func(node_num, cb->ec_data);
  1179. }
  1180. up_read(&dlm_callback_sem);
  1181. }
  1182. void dlm_setup_eviction_cb(struct dlm_eviction_cb *cb,
  1183. dlm_eviction_func *f,
  1184. void *data)
  1185. {
  1186. INIT_LIST_HEAD(&cb->ec_item);
  1187. cb->ec_func = f;
  1188. cb->ec_data = data;
  1189. }
  1190. EXPORT_SYMBOL_GPL(dlm_setup_eviction_cb);
  1191. void dlm_register_eviction_cb(struct dlm_ctxt *dlm,
  1192. struct dlm_eviction_cb *cb)
  1193. {
  1194. down_write(&dlm_callback_sem);
  1195. list_add_tail(&cb->ec_item, &dlm->dlm_eviction_callbacks);
  1196. up_write(&dlm_callback_sem);
  1197. }
  1198. EXPORT_SYMBOL_GPL(dlm_register_eviction_cb);
  1199. void dlm_unregister_eviction_cb(struct dlm_eviction_cb *cb)
  1200. {
  1201. down_write(&dlm_callback_sem);
  1202. list_del_init(&cb->ec_item);
  1203. up_write(&dlm_callback_sem);
  1204. }
  1205. EXPORT_SYMBOL_GPL(dlm_unregister_eviction_cb);
  1206. static int __init dlm_init(void)
  1207. {
  1208. int status;
  1209. dlm_print_version();
  1210. status = dlm_init_mle_cache();
  1211. if (status)
  1212. return -1;
  1213. status = dlm_register_net_handlers();
  1214. if (status) {
  1215. dlm_destroy_mle_cache();
  1216. return -1;
  1217. }
  1218. return 0;
  1219. }
  1220. static void __exit dlm_exit (void)
  1221. {
  1222. dlm_unregister_net_handlers();
  1223. dlm_destroy_mle_cache();
  1224. }
  1225. MODULE_AUTHOR("Oracle");
  1226. MODULE_LICENSE("GPL");
  1227. module_init(dlm_init);
  1228. module_exit(dlm_exit);