dlmdomain.c 40 KB

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