dlmdomain.c 36 KB

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