dlmdomain.c 36 KB

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