dlmdomain.c 45 KB

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