dlmdomain.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863
  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. /*
  576. * struct dlm_query_join_packet is made up of four one-byte fields. They
  577. * are effectively in big-endian order already. However, little-endian
  578. * machines swap them before putting the packet on the wire (because
  579. * query_join's response is a status, and that status is treated as a u32
  580. * on the wire). Thus, a big-endian and little-endian machines will treat
  581. * this structure differently.
  582. *
  583. * The solution is to have little-endian machines swap the structure when
  584. * converting from the structure to the u32 representation. This will
  585. * result in the structure having the correct format on the wire no matter
  586. * the host endian format.
  587. */
  588. static void dlm_query_join_packet_to_wire(struct dlm_query_join_packet *packet,
  589. u32 *wire)
  590. {
  591. union dlm_query_join_response response;
  592. response.packet = *packet;
  593. *wire = cpu_to_be32(response.intval);
  594. }
  595. static void dlm_query_join_wire_to_packet(u32 wire,
  596. struct dlm_query_join_packet *packet)
  597. {
  598. union dlm_query_join_response response;
  599. response.intval = cpu_to_be32(wire);
  600. *packet = response.packet;
  601. }
  602. static int dlm_query_join_handler(struct o2net_msg *msg, u32 len, void *data,
  603. void **ret_data)
  604. {
  605. struct dlm_query_join_request *query;
  606. struct dlm_query_join_packet packet = {
  607. .code = JOIN_DISALLOW,
  608. };
  609. struct dlm_ctxt *dlm = NULL;
  610. u32 response;
  611. u8 nodenum;
  612. query = (struct dlm_query_join_request *) msg->buf;
  613. mlog(0, "node %u wants to join domain %s\n", query->node_idx,
  614. query->domain);
  615. /*
  616. * If heartbeat doesn't consider the node live, tell it
  617. * to back off and try again. This gives heartbeat a chance
  618. * to catch up.
  619. */
  620. if (!o2hb_check_node_heartbeating(query->node_idx)) {
  621. mlog(0, "node %u is not in our live map yet\n",
  622. query->node_idx);
  623. packet.code = JOIN_DISALLOW;
  624. goto respond;
  625. }
  626. packet.code = JOIN_OK_NO_MAP;
  627. spin_lock(&dlm_domain_lock);
  628. dlm = __dlm_lookup_domain_full(query->domain, query->name_len);
  629. if (!dlm)
  630. goto unlock_respond;
  631. /*
  632. * There is a small window where the joining node may not see the
  633. * node(s) that just left but still part of the cluster. DISALLOW
  634. * join request if joining node has different node map.
  635. */
  636. nodenum=0;
  637. while (nodenum < O2NM_MAX_NODES) {
  638. if (test_bit(nodenum, dlm->domain_map)) {
  639. if (!byte_test_bit(nodenum, query->node_map)) {
  640. mlog(0, "disallow join as node %u does not "
  641. "have node %u in its nodemap\n",
  642. query->node_idx, nodenum);
  643. packet.code = JOIN_DISALLOW;
  644. goto unlock_respond;
  645. }
  646. }
  647. nodenum++;
  648. }
  649. /* Once the dlm ctxt is marked as leaving then we don't want
  650. * to be put in someone's domain map.
  651. * Also, explicitly disallow joining at certain troublesome
  652. * times (ie. during recovery). */
  653. if (dlm && dlm->dlm_state != DLM_CTXT_LEAVING) {
  654. int bit = query->node_idx;
  655. spin_lock(&dlm->spinlock);
  656. if (dlm->dlm_state == DLM_CTXT_NEW &&
  657. dlm->joining_node == DLM_LOCK_RES_OWNER_UNKNOWN) {
  658. /*If this is a brand new context and we
  659. * haven't started our join process yet, then
  660. * the other node won the race. */
  661. packet.code = JOIN_OK_NO_MAP;
  662. } else if (dlm->joining_node != DLM_LOCK_RES_OWNER_UNKNOWN) {
  663. /* Disallow parallel joins. */
  664. packet.code = JOIN_DISALLOW;
  665. } else if (dlm->reco.state & DLM_RECO_STATE_ACTIVE) {
  666. mlog(0, "node %u trying to join, but recovery "
  667. "is ongoing.\n", bit);
  668. packet.code = JOIN_DISALLOW;
  669. } else if (test_bit(bit, dlm->recovery_map)) {
  670. mlog(0, "node %u trying to join, but it "
  671. "still needs recovery.\n", bit);
  672. packet.code = JOIN_DISALLOW;
  673. } else if (test_bit(bit, dlm->domain_map)) {
  674. mlog(0, "node %u trying to join, but it "
  675. "is still in the domain! needs recovery?\n",
  676. bit);
  677. packet.code = JOIN_DISALLOW;
  678. } else {
  679. /* Alright we're fully a part of this domain
  680. * so we keep some state as to who's joining
  681. * and indicate to him that needs to be fixed
  682. * up. */
  683. /* Make sure we speak compatible locking protocols. */
  684. if (dlm_query_join_proto_check("DLM", bit,
  685. &dlm->dlm_locking_proto,
  686. &query->dlm_proto)) {
  687. packet.code = JOIN_PROTOCOL_MISMATCH;
  688. } else if (dlm_query_join_proto_check("fs", bit,
  689. &dlm->fs_locking_proto,
  690. &query->fs_proto)) {
  691. packet.code = JOIN_PROTOCOL_MISMATCH;
  692. } else {
  693. packet.dlm_minor = query->dlm_proto.pv_minor;
  694. packet.fs_minor = query->fs_proto.pv_minor;
  695. packet.code = JOIN_OK;
  696. __dlm_set_joining_node(dlm, query->node_idx);
  697. }
  698. }
  699. spin_unlock(&dlm->spinlock);
  700. }
  701. unlock_respond:
  702. spin_unlock(&dlm_domain_lock);
  703. respond:
  704. mlog(0, "We respond with %u\n", packet.code);
  705. dlm_query_join_packet_to_wire(&packet, &response);
  706. return response;
  707. }
  708. static int dlm_assert_joined_handler(struct o2net_msg *msg, u32 len, void *data,
  709. void **ret_data)
  710. {
  711. struct dlm_assert_joined *assert;
  712. struct dlm_ctxt *dlm = NULL;
  713. assert = (struct dlm_assert_joined *) msg->buf;
  714. mlog(0, "node %u asserts join on domain %s\n", assert->node_idx,
  715. assert->domain);
  716. spin_lock(&dlm_domain_lock);
  717. dlm = __dlm_lookup_domain_full(assert->domain, assert->name_len);
  718. /* XXX should we consider no dlm ctxt an error? */
  719. if (dlm) {
  720. spin_lock(&dlm->spinlock);
  721. /* Alright, this node has officially joined our
  722. * domain. Set him in the map and clean up our
  723. * leftover join state. */
  724. BUG_ON(dlm->joining_node != assert->node_idx);
  725. set_bit(assert->node_idx, dlm->domain_map);
  726. __dlm_set_joining_node(dlm, DLM_LOCK_RES_OWNER_UNKNOWN);
  727. printk(KERN_INFO "ocfs2_dlm: Node %u joins domain %s\n",
  728. assert->node_idx, dlm->name);
  729. __dlm_print_nodes(dlm);
  730. /* notify anything attached to the heartbeat events */
  731. dlm_hb_event_notify_attached(dlm, assert->node_idx, 1);
  732. spin_unlock(&dlm->spinlock);
  733. }
  734. spin_unlock(&dlm_domain_lock);
  735. return 0;
  736. }
  737. static int dlm_cancel_join_handler(struct o2net_msg *msg, u32 len, void *data,
  738. void **ret_data)
  739. {
  740. struct dlm_cancel_join *cancel;
  741. struct dlm_ctxt *dlm = NULL;
  742. cancel = (struct dlm_cancel_join *) msg->buf;
  743. mlog(0, "node %u cancels join on domain %s\n", cancel->node_idx,
  744. cancel->domain);
  745. spin_lock(&dlm_domain_lock);
  746. dlm = __dlm_lookup_domain_full(cancel->domain, cancel->name_len);
  747. if (dlm) {
  748. spin_lock(&dlm->spinlock);
  749. /* Yikes, this guy wants to cancel his join. No
  750. * problem, we simply cleanup our join state. */
  751. BUG_ON(dlm->joining_node != cancel->node_idx);
  752. __dlm_set_joining_node(dlm, DLM_LOCK_RES_OWNER_UNKNOWN);
  753. spin_unlock(&dlm->spinlock);
  754. }
  755. spin_unlock(&dlm_domain_lock);
  756. return 0;
  757. }
  758. static int dlm_send_one_join_cancel(struct dlm_ctxt *dlm,
  759. unsigned int node)
  760. {
  761. int status;
  762. struct dlm_cancel_join cancel_msg;
  763. memset(&cancel_msg, 0, sizeof(cancel_msg));
  764. cancel_msg.node_idx = dlm->node_num;
  765. cancel_msg.name_len = strlen(dlm->name);
  766. memcpy(cancel_msg.domain, dlm->name, cancel_msg.name_len);
  767. status = o2net_send_message(DLM_CANCEL_JOIN_MSG, DLM_MOD_KEY,
  768. &cancel_msg, sizeof(cancel_msg), node,
  769. NULL);
  770. if (status < 0) {
  771. mlog_errno(status);
  772. goto bail;
  773. }
  774. bail:
  775. return status;
  776. }
  777. /* map_size should be in bytes. */
  778. static int dlm_send_join_cancels(struct dlm_ctxt *dlm,
  779. unsigned long *node_map,
  780. unsigned int map_size)
  781. {
  782. int status, tmpstat;
  783. unsigned int node;
  784. if (map_size != (BITS_TO_LONGS(O2NM_MAX_NODES) *
  785. sizeof(unsigned long))) {
  786. mlog(ML_ERROR,
  787. "map_size %u != BITS_TO_LONGS(O2NM_MAX_NODES) %u\n",
  788. map_size, (unsigned)BITS_TO_LONGS(O2NM_MAX_NODES));
  789. return -EINVAL;
  790. }
  791. status = 0;
  792. node = -1;
  793. while ((node = find_next_bit(node_map, O2NM_MAX_NODES,
  794. node + 1)) < O2NM_MAX_NODES) {
  795. if (node == dlm->node_num)
  796. continue;
  797. tmpstat = dlm_send_one_join_cancel(dlm, node);
  798. if (tmpstat) {
  799. mlog(ML_ERROR, "Error return %d cancelling join on "
  800. "node %d\n", tmpstat, node);
  801. if (!status)
  802. status = tmpstat;
  803. }
  804. }
  805. if (status)
  806. mlog_errno(status);
  807. return status;
  808. }
  809. static int dlm_request_join(struct dlm_ctxt *dlm,
  810. int node,
  811. enum dlm_query_join_response_code *response)
  812. {
  813. int status;
  814. struct dlm_query_join_request join_msg;
  815. struct dlm_query_join_packet packet;
  816. u32 join_resp;
  817. mlog(0, "querying node %d\n", node);
  818. memset(&join_msg, 0, sizeof(join_msg));
  819. join_msg.node_idx = dlm->node_num;
  820. join_msg.name_len = strlen(dlm->name);
  821. memcpy(join_msg.domain, dlm->name, join_msg.name_len);
  822. join_msg.dlm_proto = dlm->dlm_locking_proto;
  823. join_msg.fs_proto = dlm->fs_locking_proto;
  824. /* copy live node map to join message */
  825. byte_copymap(join_msg.node_map, dlm->live_nodes_map, O2NM_MAX_NODES);
  826. status = o2net_send_message(DLM_QUERY_JOIN_MSG, DLM_MOD_KEY, &join_msg,
  827. sizeof(join_msg), node,
  828. &join_resp);
  829. if (status < 0 && status != -ENOPROTOOPT) {
  830. mlog_errno(status);
  831. goto bail;
  832. }
  833. dlm_query_join_wire_to_packet(join_resp, &packet);
  834. /* -ENOPROTOOPT from the net code means the other side isn't
  835. listening for our message type -- that's fine, it means
  836. his dlm isn't up, so we can consider him a 'yes' but not
  837. joined into the domain. */
  838. if (status == -ENOPROTOOPT) {
  839. status = 0;
  840. *response = JOIN_OK_NO_MAP;
  841. } else if (packet.code == JOIN_DISALLOW ||
  842. packet.code == JOIN_OK_NO_MAP) {
  843. *response = packet.code;
  844. } else if (packet.code == JOIN_PROTOCOL_MISMATCH) {
  845. mlog(ML_NOTICE,
  846. "This node requested DLM locking protocol %u.%u and "
  847. "filesystem locking protocol %u.%u. At least one of "
  848. "the protocol versions on node %d is not compatible, "
  849. "disconnecting\n",
  850. dlm->dlm_locking_proto.pv_major,
  851. dlm->dlm_locking_proto.pv_minor,
  852. dlm->fs_locking_proto.pv_major,
  853. dlm->fs_locking_proto.pv_minor,
  854. node);
  855. status = -EPROTO;
  856. *response = packet.code;
  857. } else if (packet.code == JOIN_OK) {
  858. *response = packet.code;
  859. /* Use the same locking protocol as the remote node */
  860. dlm->dlm_locking_proto.pv_minor = packet.dlm_minor;
  861. dlm->fs_locking_proto.pv_minor = packet.fs_minor;
  862. mlog(0,
  863. "Node %d responds JOIN_OK with DLM locking protocol "
  864. "%u.%u and fs locking protocol %u.%u\n",
  865. node,
  866. dlm->dlm_locking_proto.pv_major,
  867. dlm->dlm_locking_proto.pv_minor,
  868. dlm->fs_locking_proto.pv_major,
  869. dlm->fs_locking_proto.pv_minor);
  870. } else {
  871. status = -EINVAL;
  872. mlog(ML_ERROR, "invalid response %d from node %u\n",
  873. packet.code, node);
  874. }
  875. mlog(0, "status %d, node %d response is %d\n", status, node,
  876. *response);
  877. bail:
  878. return status;
  879. }
  880. static int dlm_send_one_join_assert(struct dlm_ctxt *dlm,
  881. unsigned int node)
  882. {
  883. int status;
  884. struct dlm_assert_joined assert_msg;
  885. mlog(0, "Sending join assert to node %u\n", node);
  886. memset(&assert_msg, 0, sizeof(assert_msg));
  887. assert_msg.node_idx = dlm->node_num;
  888. assert_msg.name_len = strlen(dlm->name);
  889. memcpy(assert_msg.domain, dlm->name, assert_msg.name_len);
  890. status = o2net_send_message(DLM_ASSERT_JOINED_MSG, DLM_MOD_KEY,
  891. &assert_msg, sizeof(assert_msg), node,
  892. NULL);
  893. if (status < 0)
  894. mlog_errno(status);
  895. return status;
  896. }
  897. static void dlm_send_join_asserts(struct dlm_ctxt *dlm,
  898. unsigned long *node_map)
  899. {
  900. int status, node, live;
  901. status = 0;
  902. node = -1;
  903. while ((node = find_next_bit(node_map, O2NM_MAX_NODES,
  904. node + 1)) < O2NM_MAX_NODES) {
  905. if (node == dlm->node_num)
  906. continue;
  907. do {
  908. /* It is very important that this message be
  909. * received so we spin until either the node
  910. * has died or it gets the message. */
  911. status = dlm_send_one_join_assert(dlm, node);
  912. spin_lock(&dlm->spinlock);
  913. live = test_bit(node, dlm->live_nodes_map);
  914. spin_unlock(&dlm->spinlock);
  915. if (status) {
  916. mlog(ML_ERROR, "Error return %d asserting "
  917. "join on node %d\n", status, node);
  918. /* give us some time between errors... */
  919. if (live)
  920. msleep(DLM_DOMAIN_BACKOFF_MS);
  921. }
  922. } while (status && live);
  923. }
  924. }
  925. struct domain_join_ctxt {
  926. unsigned long live_map[BITS_TO_LONGS(O2NM_MAX_NODES)];
  927. unsigned long yes_resp_map[BITS_TO_LONGS(O2NM_MAX_NODES)];
  928. };
  929. static int dlm_should_restart_join(struct dlm_ctxt *dlm,
  930. struct domain_join_ctxt *ctxt,
  931. enum dlm_query_join_response_code response)
  932. {
  933. int ret;
  934. if (response == JOIN_DISALLOW) {
  935. mlog(0, "Latest response of disallow -- should restart\n");
  936. return 1;
  937. }
  938. spin_lock(&dlm->spinlock);
  939. /* For now, we restart the process if the node maps have
  940. * changed at all */
  941. ret = memcmp(ctxt->live_map, dlm->live_nodes_map,
  942. sizeof(dlm->live_nodes_map));
  943. spin_unlock(&dlm->spinlock);
  944. if (ret)
  945. mlog(0, "Node maps changed -- should restart\n");
  946. return ret;
  947. }
  948. static int dlm_try_to_join_domain(struct dlm_ctxt *dlm)
  949. {
  950. int status = 0, tmpstat, node;
  951. struct domain_join_ctxt *ctxt;
  952. enum dlm_query_join_response_code response = JOIN_DISALLOW;
  953. mlog_entry("%p", dlm);
  954. ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
  955. if (!ctxt) {
  956. status = -ENOMEM;
  957. mlog_errno(status);
  958. goto bail;
  959. }
  960. /* group sem locking should work for us here -- we're already
  961. * registered for heartbeat events so filling this should be
  962. * atomic wrt getting those handlers called. */
  963. o2hb_fill_node_map(dlm->live_nodes_map, sizeof(dlm->live_nodes_map));
  964. spin_lock(&dlm->spinlock);
  965. memcpy(ctxt->live_map, dlm->live_nodes_map, sizeof(ctxt->live_map));
  966. __dlm_set_joining_node(dlm, dlm->node_num);
  967. spin_unlock(&dlm->spinlock);
  968. node = -1;
  969. while ((node = find_next_bit(ctxt->live_map, O2NM_MAX_NODES,
  970. node + 1)) < O2NM_MAX_NODES) {
  971. if (node == dlm->node_num)
  972. continue;
  973. status = dlm_request_join(dlm, node, &response);
  974. if (status < 0) {
  975. mlog_errno(status);
  976. goto bail;
  977. }
  978. /* Ok, either we got a response or the node doesn't have a
  979. * dlm up. */
  980. if (response == JOIN_OK)
  981. set_bit(node, ctxt->yes_resp_map);
  982. if (dlm_should_restart_join(dlm, ctxt, response)) {
  983. status = -EAGAIN;
  984. goto bail;
  985. }
  986. }
  987. mlog(0, "Yay, done querying nodes!\n");
  988. /* Yay, everyone agree's we can join the domain. My domain is
  989. * comprised of all nodes who were put in the
  990. * yes_resp_map. Copy that into our domain map and send a join
  991. * assert message to clean up everyone elses state. */
  992. spin_lock(&dlm->spinlock);
  993. memcpy(dlm->domain_map, ctxt->yes_resp_map,
  994. sizeof(ctxt->yes_resp_map));
  995. set_bit(dlm->node_num, dlm->domain_map);
  996. spin_unlock(&dlm->spinlock);
  997. dlm_send_join_asserts(dlm, ctxt->yes_resp_map);
  998. /* Joined state *must* be set before the joining node
  999. * information, otherwise the query_join handler may read no
  1000. * current joiner but a state of NEW and tell joining nodes
  1001. * we're not in the domain. */
  1002. spin_lock(&dlm_domain_lock);
  1003. dlm->dlm_state = DLM_CTXT_JOINED;
  1004. dlm->num_joins++;
  1005. spin_unlock(&dlm_domain_lock);
  1006. bail:
  1007. spin_lock(&dlm->spinlock);
  1008. __dlm_set_joining_node(dlm, DLM_LOCK_RES_OWNER_UNKNOWN);
  1009. if (!status)
  1010. __dlm_print_nodes(dlm);
  1011. spin_unlock(&dlm->spinlock);
  1012. if (ctxt) {
  1013. /* Do we need to send a cancel message to any nodes? */
  1014. if (status < 0) {
  1015. tmpstat = dlm_send_join_cancels(dlm,
  1016. ctxt->yes_resp_map,
  1017. sizeof(ctxt->yes_resp_map));
  1018. if (tmpstat < 0)
  1019. mlog_errno(tmpstat);
  1020. }
  1021. kfree(ctxt);
  1022. }
  1023. mlog(0, "returning %d\n", status);
  1024. return status;
  1025. }
  1026. static void dlm_unregister_domain_handlers(struct dlm_ctxt *dlm)
  1027. {
  1028. o2hb_unregister_callback(NULL, &dlm->dlm_hb_up);
  1029. o2hb_unregister_callback(NULL, &dlm->dlm_hb_down);
  1030. o2net_unregister_handler_list(&dlm->dlm_domain_handlers);
  1031. }
  1032. static int dlm_register_domain_handlers(struct dlm_ctxt *dlm)
  1033. {
  1034. int status;
  1035. mlog(0, "registering handlers.\n");
  1036. o2hb_setup_callback(&dlm->dlm_hb_down, O2HB_NODE_DOWN_CB,
  1037. dlm_hb_node_down_cb, dlm, DLM_HB_NODE_DOWN_PRI);
  1038. status = o2hb_register_callback(NULL, &dlm->dlm_hb_down);
  1039. if (status)
  1040. goto bail;
  1041. o2hb_setup_callback(&dlm->dlm_hb_up, O2HB_NODE_UP_CB,
  1042. dlm_hb_node_up_cb, dlm, DLM_HB_NODE_UP_PRI);
  1043. status = o2hb_register_callback(NULL, &dlm->dlm_hb_up);
  1044. if (status)
  1045. goto bail;
  1046. status = o2net_register_handler(DLM_MASTER_REQUEST_MSG, dlm->key,
  1047. sizeof(struct dlm_master_request),
  1048. dlm_master_request_handler,
  1049. dlm, NULL, &dlm->dlm_domain_handlers);
  1050. if (status)
  1051. goto bail;
  1052. status = o2net_register_handler(DLM_ASSERT_MASTER_MSG, dlm->key,
  1053. sizeof(struct dlm_assert_master),
  1054. dlm_assert_master_handler,
  1055. dlm, dlm_assert_master_post_handler,
  1056. &dlm->dlm_domain_handlers);
  1057. if (status)
  1058. goto bail;
  1059. status = o2net_register_handler(DLM_CREATE_LOCK_MSG, dlm->key,
  1060. sizeof(struct dlm_create_lock),
  1061. dlm_create_lock_handler,
  1062. dlm, NULL, &dlm->dlm_domain_handlers);
  1063. if (status)
  1064. goto bail;
  1065. status = o2net_register_handler(DLM_CONVERT_LOCK_MSG, dlm->key,
  1066. DLM_CONVERT_LOCK_MAX_LEN,
  1067. dlm_convert_lock_handler,
  1068. dlm, NULL, &dlm->dlm_domain_handlers);
  1069. if (status)
  1070. goto bail;
  1071. status = o2net_register_handler(DLM_UNLOCK_LOCK_MSG, dlm->key,
  1072. DLM_UNLOCK_LOCK_MAX_LEN,
  1073. dlm_unlock_lock_handler,
  1074. dlm, NULL, &dlm->dlm_domain_handlers);
  1075. if (status)
  1076. goto bail;
  1077. status = o2net_register_handler(DLM_PROXY_AST_MSG, dlm->key,
  1078. DLM_PROXY_AST_MAX_LEN,
  1079. dlm_proxy_ast_handler,
  1080. dlm, NULL, &dlm->dlm_domain_handlers);
  1081. if (status)
  1082. goto bail;
  1083. status = o2net_register_handler(DLM_EXIT_DOMAIN_MSG, dlm->key,
  1084. sizeof(struct dlm_exit_domain),
  1085. dlm_exit_domain_handler,
  1086. dlm, NULL, &dlm->dlm_domain_handlers);
  1087. if (status)
  1088. goto bail;
  1089. status = o2net_register_handler(DLM_DEREF_LOCKRES_MSG, dlm->key,
  1090. sizeof(struct dlm_deref_lockres),
  1091. dlm_deref_lockres_handler,
  1092. dlm, NULL, &dlm->dlm_domain_handlers);
  1093. if (status)
  1094. goto bail;
  1095. status = o2net_register_handler(DLM_MIGRATE_REQUEST_MSG, dlm->key,
  1096. sizeof(struct dlm_migrate_request),
  1097. dlm_migrate_request_handler,
  1098. dlm, NULL, &dlm->dlm_domain_handlers);
  1099. if (status)
  1100. goto bail;
  1101. status = o2net_register_handler(DLM_MIG_LOCKRES_MSG, dlm->key,
  1102. DLM_MIG_LOCKRES_MAX_LEN,
  1103. dlm_mig_lockres_handler,
  1104. dlm, NULL, &dlm->dlm_domain_handlers);
  1105. if (status)
  1106. goto bail;
  1107. status = o2net_register_handler(DLM_MASTER_REQUERY_MSG, dlm->key,
  1108. sizeof(struct dlm_master_requery),
  1109. dlm_master_requery_handler,
  1110. dlm, NULL, &dlm->dlm_domain_handlers);
  1111. if (status)
  1112. goto bail;
  1113. status = o2net_register_handler(DLM_LOCK_REQUEST_MSG, dlm->key,
  1114. sizeof(struct dlm_lock_request),
  1115. dlm_request_all_locks_handler,
  1116. dlm, NULL, &dlm->dlm_domain_handlers);
  1117. if (status)
  1118. goto bail;
  1119. status = o2net_register_handler(DLM_RECO_DATA_DONE_MSG, dlm->key,
  1120. sizeof(struct dlm_reco_data_done),
  1121. dlm_reco_data_done_handler,
  1122. dlm, NULL, &dlm->dlm_domain_handlers);
  1123. if (status)
  1124. goto bail;
  1125. status = o2net_register_handler(DLM_BEGIN_RECO_MSG, dlm->key,
  1126. sizeof(struct dlm_begin_reco),
  1127. dlm_begin_reco_handler,
  1128. dlm, NULL, &dlm->dlm_domain_handlers);
  1129. if (status)
  1130. goto bail;
  1131. status = o2net_register_handler(DLM_FINALIZE_RECO_MSG, dlm->key,
  1132. sizeof(struct dlm_finalize_reco),
  1133. dlm_finalize_reco_handler,
  1134. dlm, NULL, &dlm->dlm_domain_handlers);
  1135. if (status)
  1136. goto bail;
  1137. bail:
  1138. if (status)
  1139. dlm_unregister_domain_handlers(dlm);
  1140. return status;
  1141. }
  1142. static int dlm_join_domain(struct dlm_ctxt *dlm)
  1143. {
  1144. int status;
  1145. unsigned int backoff;
  1146. unsigned int total_backoff = 0;
  1147. BUG_ON(!dlm);
  1148. mlog(0, "Join domain %s\n", dlm->name);
  1149. status = dlm_register_domain_handlers(dlm);
  1150. if (status) {
  1151. mlog_errno(status);
  1152. goto bail;
  1153. }
  1154. status = dlm_launch_thread(dlm);
  1155. if (status < 0) {
  1156. mlog_errno(status);
  1157. goto bail;
  1158. }
  1159. status = dlm_launch_recovery_thread(dlm);
  1160. if (status < 0) {
  1161. mlog_errno(status);
  1162. goto bail;
  1163. }
  1164. dlm->dlm_worker = create_singlethread_workqueue("dlm_wq");
  1165. if (!dlm->dlm_worker) {
  1166. status = -ENOMEM;
  1167. mlog_errno(status);
  1168. goto bail;
  1169. }
  1170. do {
  1171. status = dlm_try_to_join_domain(dlm);
  1172. /* If we're racing another node to the join, then we
  1173. * need to back off temporarily and let them
  1174. * complete. */
  1175. #define DLM_JOIN_TIMEOUT_MSECS 90000
  1176. if (status == -EAGAIN) {
  1177. if (signal_pending(current)) {
  1178. status = -ERESTARTSYS;
  1179. goto bail;
  1180. }
  1181. if (total_backoff >
  1182. msecs_to_jiffies(DLM_JOIN_TIMEOUT_MSECS)) {
  1183. status = -ERESTARTSYS;
  1184. mlog(ML_NOTICE, "Timed out joining dlm domain "
  1185. "%s after %u msecs\n", dlm->name,
  1186. jiffies_to_msecs(total_backoff));
  1187. goto bail;
  1188. }
  1189. /*
  1190. * <chip> After you!
  1191. * <dale> No, after you!
  1192. * <chip> I insist!
  1193. * <dale> But you first!
  1194. * ...
  1195. */
  1196. backoff = (unsigned int)(jiffies & 0x3);
  1197. backoff *= DLM_DOMAIN_BACKOFF_MS;
  1198. total_backoff += backoff;
  1199. mlog(0, "backoff %d\n", backoff);
  1200. msleep(backoff);
  1201. }
  1202. } while (status == -EAGAIN);
  1203. if (status < 0) {
  1204. mlog_errno(status);
  1205. goto bail;
  1206. }
  1207. status = 0;
  1208. bail:
  1209. wake_up(&dlm_domain_events);
  1210. if (status) {
  1211. dlm_unregister_domain_handlers(dlm);
  1212. dlm_complete_thread(dlm);
  1213. dlm_complete_recovery_thread(dlm);
  1214. dlm_destroy_dlm_worker(dlm);
  1215. }
  1216. return status;
  1217. }
  1218. static struct dlm_ctxt *dlm_alloc_ctxt(const char *domain,
  1219. u32 key)
  1220. {
  1221. int i;
  1222. struct dlm_ctxt *dlm = NULL;
  1223. dlm = kzalloc(sizeof(*dlm), GFP_KERNEL);
  1224. if (!dlm) {
  1225. mlog_errno(-ENOMEM);
  1226. goto leave;
  1227. }
  1228. dlm->name = kmalloc(strlen(domain) + 1, GFP_KERNEL);
  1229. if (dlm->name == NULL) {
  1230. mlog_errno(-ENOMEM);
  1231. kfree(dlm);
  1232. dlm = NULL;
  1233. goto leave;
  1234. }
  1235. dlm->lockres_hash = (struct hlist_head **)dlm_alloc_pagevec(DLM_HASH_PAGES);
  1236. if (!dlm->lockres_hash) {
  1237. mlog_errno(-ENOMEM);
  1238. kfree(dlm->name);
  1239. kfree(dlm);
  1240. dlm = NULL;
  1241. goto leave;
  1242. }
  1243. for (i = 0; i < DLM_HASH_BUCKETS; i++)
  1244. INIT_HLIST_HEAD(dlm_lockres_hash(dlm, i));
  1245. strcpy(dlm->name, domain);
  1246. dlm->key = key;
  1247. dlm->node_num = o2nm_this_node();
  1248. spin_lock_init(&dlm->spinlock);
  1249. spin_lock_init(&dlm->master_lock);
  1250. spin_lock_init(&dlm->ast_lock);
  1251. INIT_LIST_HEAD(&dlm->list);
  1252. INIT_LIST_HEAD(&dlm->dirty_list);
  1253. INIT_LIST_HEAD(&dlm->reco.resources);
  1254. INIT_LIST_HEAD(&dlm->reco.received);
  1255. INIT_LIST_HEAD(&dlm->reco.node_data);
  1256. INIT_LIST_HEAD(&dlm->purge_list);
  1257. INIT_LIST_HEAD(&dlm->dlm_domain_handlers);
  1258. dlm->reco.state = 0;
  1259. INIT_LIST_HEAD(&dlm->pending_asts);
  1260. INIT_LIST_HEAD(&dlm->pending_basts);
  1261. mlog(0, "dlm->recovery_map=%p, &(dlm->recovery_map[0])=%p\n",
  1262. dlm->recovery_map, &(dlm->recovery_map[0]));
  1263. memset(dlm->recovery_map, 0, sizeof(dlm->recovery_map));
  1264. memset(dlm->live_nodes_map, 0, sizeof(dlm->live_nodes_map));
  1265. memset(dlm->domain_map, 0, sizeof(dlm->domain_map));
  1266. dlm->dlm_thread_task = NULL;
  1267. dlm->dlm_reco_thread_task = NULL;
  1268. dlm->dlm_worker = NULL;
  1269. init_waitqueue_head(&dlm->dlm_thread_wq);
  1270. init_waitqueue_head(&dlm->dlm_reco_thread_wq);
  1271. init_waitqueue_head(&dlm->reco.event);
  1272. init_waitqueue_head(&dlm->ast_wq);
  1273. init_waitqueue_head(&dlm->migration_wq);
  1274. INIT_LIST_HEAD(&dlm->master_list);
  1275. INIT_LIST_HEAD(&dlm->mle_hb_events);
  1276. dlm->joining_node = DLM_LOCK_RES_OWNER_UNKNOWN;
  1277. init_waitqueue_head(&dlm->dlm_join_events);
  1278. dlm->reco.new_master = O2NM_INVALID_NODE_NUM;
  1279. dlm->reco.dead_node = O2NM_INVALID_NODE_NUM;
  1280. atomic_set(&dlm->local_resources, 0);
  1281. atomic_set(&dlm->remote_resources, 0);
  1282. atomic_set(&dlm->unknown_resources, 0);
  1283. spin_lock_init(&dlm->work_lock);
  1284. INIT_LIST_HEAD(&dlm->work_list);
  1285. INIT_WORK(&dlm->dispatched_work, dlm_dispatch_work);
  1286. kref_init(&dlm->dlm_refs);
  1287. dlm->dlm_state = DLM_CTXT_NEW;
  1288. INIT_LIST_HEAD(&dlm->dlm_eviction_callbacks);
  1289. mlog(0, "context init: refcount %u\n",
  1290. atomic_read(&dlm->dlm_refs.refcount));
  1291. leave:
  1292. return dlm;
  1293. }
  1294. /*
  1295. * Compare a requested locking protocol version against the current one.
  1296. *
  1297. * If the major numbers are different, they are incompatible.
  1298. * If the current minor is greater than the request, they are incompatible.
  1299. * If the current minor is less than or equal to the request, they are
  1300. * compatible, and the requester should run at the current minor version.
  1301. */
  1302. static int dlm_protocol_compare(struct dlm_protocol_version *existing,
  1303. struct dlm_protocol_version *request)
  1304. {
  1305. if (existing->pv_major != request->pv_major)
  1306. return 1;
  1307. if (existing->pv_minor > request->pv_minor)
  1308. return 1;
  1309. if (existing->pv_minor < request->pv_minor)
  1310. request->pv_minor = existing->pv_minor;
  1311. return 0;
  1312. }
  1313. /*
  1314. * dlm_register_domain: one-time setup per "domain".
  1315. *
  1316. * The filesystem passes in the requested locking version via proto.
  1317. * If registration was successful, proto will contain the negotiated
  1318. * locking protocol.
  1319. */
  1320. struct dlm_ctxt * dlm_register_domain(const char *domain,
  1321. u32 key,
  1322. struct dlm_protocol_version *fs_proto)
  1323. {
  1324. int ret;
  1325. struct dlm_ctxt *dlm = NULL;
  1326. struct dlm_ctxt *new_ctxt = NULL;
  1327. if (strlen(domain) > O2NM_MAX_NAME_LEN) {
  1328. ret = -ENAMETOOLONG;
  1329. mlog(ML_ERROR, "domain name length too long\n");
  1330. goto leave;
  1331. }
  1332. if (!o2hb_check_local_node_heartbeating()) {
  1333. mlog(ML_ERROR, "the local node has not been configured, or is "
  1334. "not heartbeating\n");
  1335. ret = -EPROTO;
  1336. goto leave;
  1337. }
  1338. mlog(0, "register called for domain \"%s\"\n", domain);
  1339. retry:
  1340. dlm = NULL;
  1341. if (signal_pending(current)) {
  1342. ret = -ERESTARTSYS;
  1343. mlog_errno(ret);
  1344. goto leave;
  1345. }
  1346. spin_lock(&dlm_domain_lock);
  1347. dlm = __dlm_lookup_domain(domain);
  1348. if (dlm) {
  1349. if (dlm->dlm_state != DLM_CTXT_JOINED) {
  1350. spin_unlock(&dlm_domain_lock);
  1351. mlog(0, "This ctxt is not joined yet!\n");
  1352. wait_event_interruptible(dlm_domain_events,
  1353. dlm_wait_on_domain_helper(
  1354. domain));
  1355. goto retry;
  1356. }
  1357. if (dlm_protocol_compare(&dlm->fs_locking_proto, fs_proto)) {
  1358. mlog(ML_ERROR,
  1359. "Requested locking protocol version is not "
  1360. "compatible with already registered domain "
  1361. "\"%s\"\n", domain);
  1362. ret = -EPROTO;
  1363. goto leave;
  1364. }
  1365. __dlm_get(dlm);
  1366. dlm->num_joins++;
  1367. spin_unlock(&dlm_domain_lock);
  1368. ret = 0;
  1369. goto leave;
  1370. }
  1371. /* doesn't exist */
  1372. if (!new_ctxt) {
  1373. spin_unlock(&dlm_domain_lock);
  1374. new_ctxt = dlm_alloc_ctxt(domain, key);
  1375. if (new_ctxt)
  1376. goto retry;
  1377. ret = -ENOMEM;
  1378. mlog_errno(ret);
  1379. goto leave;
  1380. }
  1381. /* a little variable switch-a-roo here... */
  1382. dlm = new_ctxt;
  1383. new_ctxt = NULL;
  1384. /* add the new domain */
  1385. list_add_tail(&dlm->list, &dlm_domains);
  1386. spin_unlock(&dlm_domain_lock);
  1387. /*
  1388. * Pass the locking protocol version into the join. If the join
  1389. * succeeds, it will have the negotiated protocol set.
  1390. */
  1391. dlm->dlm_locking_proto = dlm_protocol;
  1392. dlm->fs_locking_proto = *fs_proto;
  1393. ret = dlm_join_domain(dlm);
  1394. if (ret) {
  1395. mlog_errno(ret);
  1396. dlm_put(dlm);
  1397. goto leave;
  1398. }
  1399. /* Tell the caller what locking protocol we negotiated */
  1400. *fs_proto = dlm->fs_locking_proto;
  1401. ret = 0;
  1402. leave:
  1403. if (new_ctxt)
  1404. dlm_free_ctxt_mem(new_ctxt);
  1405. if (ret < 0)
  1406. dlm = ERR_PTR(ret);
  1407. return dlm;
  1408. }
  1409. EXPORT_SYMBOL_GPL(dlm_register_domain);
  1410. static LIST_HEAD(dlm_join_handlers);
  1411. static void dlm_unregister_net_handlers(void)
  1412. {
  1413. o2net_unregister_handler_list(&dlm_join_handlers);
  1414. }
  1415. static int dlm_register_net_handlers(void)
  1416. {
  1417. int status = 0;
  1418. status = o2net_register_handler(DLM_QUERY_JOIN_MSG, DLM_MOD_KEY,
  1419. sizeof(struct dlm_query_join_request),
  1420. dlm_query_join_handler,
  1421. NULL, NULL, &dlm_join_handlers);
  1422. if (status)
  1423. goto bail;
  1424. status = o2net_register_handler(DLM_ASSERT_JOINED_MSG, DLM_MOD_KEY,
  1425. sizeof(struct dlm_assert_joined),
  1426. dlm_assert_joined_handler,
  1427. NULL, NULL, &dlm_join_handlers);
  1428. if (status)
  1429. goto bail;
  1430. status = o2net_register_handler(DLM_CANCEL_JOIN_MSG, DLM_MOD_KEY,
  1431. sizeof(struct dlm_cancel_join),
  1432. dlm_cancel_join_handler,
  1433. NULL, NULL, &dlm_join_handlers);
  1434. bail:
  1435. if (status < 0)
  1436. dlm_unregister_net_handlers();
  1437. return status;
  1438. }
  1439. /* Domain eviction callback handling.
  1440. *
  1441. * The file system requires notification of node death *before* the
  1442. * dlm completes it's recovery work, otherwise it may be able to
  1443. * acquire locks on resources requiring recovery. Since the dlm can
  1444. * evict a node from it's domain *before* heartbeat fires, a similar
  1445. * mechanism is required. */
  1446. /* Eviction is not expected to happen often, so a per-domain lock is
  1447. * not necessary. Eviction callbacks are allowed to sleep for short
  1448. * periods of time. */
  1449. static DECLARE_RWSEM(dlm_callback_sem);
  1450. void dlm_fire_domain_eviction_callbacks(struct dlm_ctxt *dlm,
  1451. int node_num)
  1452. {
  1453. struct list_head *iter;
  1454. struct dlm_eviction_cb *cb;
  1455. down_read(&dlm_callback_sem);
  1456. list_for_each(iter, &dlm->dlm_eviction_callbacks) {
  1457. cb = list_entry(iter, struct dlm_eviction_cb, ec_item);
  1458. cb->ec_func(node_num, cb->ec_data);
  1459. }
  1460. up_read(&dlm_callback_sem);
  1461. }
  1462. void dlm_setup_eviction_cb(struct dlm_eviction_cb *cb,
  1463. dlm_eviction_func *f,
  1464. void *data)
  1465. {
  1466. INIT_LIST_HEAD(&cb->ec_item);
  1467. cb->ec_func = f;
  1468. cb->ec_data = data;
  1469. }
  1470. EXPORT_SYMBOL_GPL(dlm_setup_eviction_cb);
  1471. void dlm_register_eviction_cb(struct dlm_ctxt *dlm,
  1472. struct dlm_eviction_cb *cb)
  1473. {
  1474. down_write(&dlm_callback_sem);
  1475. list_add_tail(&cb->ec_item, &dlm->dlm_eviction_callbacks);
  1476. up_write(&dlm_callback_sem);
  1477. }
  1478. EXPORT_SYMBOL_GPL(dlm_register_eviction_cb);
  1479. void dlm_unregister_eviction_cb(struct dlm_eviction_cb *cb)
  1480. {
  1481. down_write(&dlm_callback_sem);
  1482. list_del_init(&cb->ec_item);
  1483. up_write(&dlm_callback_sem);
  1484. }
  1485. EXPORT_SYMBOL_GPL(dlm_unregister_eviction_cb);
  1486. static int __init dlm_init(void)
  1487. {
  1488. int status;
  1489. dlm_print_version();
  1490. status = dlm_init_mle_cache();
  1491. if (status) {
  1492. mlog(ML_ERROR, "Could not create o2dlm_mle slabcache\n");
  1493. goto error;
  1494. }
  1495. status = dlm_init_master_caches();
  1496. if (status) {
  1497. mlog(ML_ERROR, "Could not create o2dlm_lockres and "
  1498. "o2dlm_lockname slabcaches\n");
  1499. goto error;
  1500. }
  1501. status = dlm_init_lock_cache();
  1502. if (status) {
  1503. mlog(ML_ERROR, "Count not create o2dlm_lock slabcache\n");
  1504. goto error;
  1505. }
  1506. status = dlm_register_net_handlers();
  1507. if (status) {
  1508. mlog(ML_ERROR, "Unable to register network handlers\n");
  1509. goto error;
  1510. }
  1511. return 0;
  1512. error:
  1513. dlm_destroy_lock_cache();
  1514. dlm_destroy_master_caches();
  1515. dlm_destroy_mle_cache();
  1516. return -1;
  1517. }
  1518. static void __exit dlm_exit (void)
  1519. {
  1520. dlm_unregister_net_handlers();
  1521. dlm_destroy_lock_cache();
  1522. dlm_destroy_master_caches();
  1523. dlm_destroy_mle_cache();
  1524. }
  1525. MODULE_AUTHOR("Oracle");
  1526. MODULE_LICENSE("GPL");
  1527. module_init(dlm_init);
  1528. module_exit(dlm_exit);