dlmdomain.c 37 KB

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