dlmdomain.c 41 KB

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