dlmdomain.c 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328
  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/init.h>
  31. #include <linux/spinlock.h>
  32. #include <linux/delay.h>
  33. #include <linux/err.h>
  34. #include <linux/debugfs.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 "dlmdebug.h"
  42. #include "dlmver.h"
  43. #define MLOG_MASK_PREFIX (ML_DLM|ML_DLM_DOMAIN)
  44. #include "cluster/masklog.h"
  45. /*
  46. * ocfs2 node maps are array of long int, which limits to send them freely
  47. * across the wire due to endianness issues. To workaround this, we convert
  48. * long ints to byte arrays. Following 3 routines are helper functions to
  49. * set/test/copy bits within those array of bytes
  50. */
  51. static inline void byte_set_bit(u8 nr, u8 map[])
  52. {
  53. map[nr >> 3] |= (1UL << (nr & 7));
  54. }
  55. static inline int byte_test_bit(u8 nr, u8 map[])
  56. {
  57. return ((1UL << (nr & 7)) & (map[nr >> 3])) != 0;
  58. }
  59. static inline void byte_copymap(u8 dmap[], unsigned long smap[],
  60. unsigned int sz)
  61. {
  62. unsigned int nn;
  63. if (!sz)
  64. return;
  65. memset(dmap, 0, ((sz + 7) >> 3));
  66. for (nn = 0 ; nn < sz; nn++)
  67. if (test_bit(nn, smap))
  68. byte_set_bit(nn, dmap);
  69. }
  70. static void dlm_free_pagevec(void **vec, int pages)
  71. {
  72. while (pages--)
  73. free_page((unsigned long)vec[pages]);
  74. kfree(vec);
  75. }
  76. static void **dlm_alloc_pagevec(int pages)
  77. {
  78. void **vec = kmalloc(pages * sizeof(void *), GFP_KERNEL);
  79. int i;
  80. if (!vec)
  81. return NULL;
  82. for (i = 0; i < pages; i++)
  83. if (!(vec[i] = (void *)__get_free_page(GFP_KERNEL)))
  84. goto out_free;
  85. mlog(0, "Allocated DLM hash pagevec; %d pages (%lu expected), %lu buckets per page\n",
  86. pages, (unsigned long)DLM_HASH_PAGES,
  87. (unsigned long)DLM_BUCKETS_PER_PAGE);
  88. return vec;
  89. out_free:
  90. dlm_free_pagevec(vec, i);
  91. return NULL;
  92. }
  93. /*
  94. *
  95. * spinlock lock ordering: if multiple locks are needed, obey this ordering:
  96. * dlm_domain_lock
  97. * struct dlm_ctxt->spinlock
  98. * struct dlm_lock_resource->spinlock
  99. * struct dlm_ctxt->master_lock
  100. * struct dlm_ctxt->ast_lock
  101. * dlm_master_list_entry->spinlock
  102. * dlm_lock->spinlock
  103. *
  104. */
  105. DEFINE_SPINLOCK(dlm_domain_lock);
  106. LIST_HEAD(dlm_domains);
  107. static DECLARE_WAIT_QUEUE_HEAD(dlm_domain_events);
  108. /*
  109. * The supported protocol version for DLM communication. Running domains
  110. * will have a negotiated version with the same major number and a minor
  111. * number equal or smaller. The dlm_ctxt->dlm_locking_proto field should
  112. * be used to determine what a running domain is actually using.
  113. *
  114. * New in version 1.1:
  115. * - Message DLM_QUERY_REGION added to support global heartbeat
  116. * - Message DLM_QUERY_NODEINFO added to allow online node removes
  117. */
  118. static const struct dlm_protocol_version dlm_protocol = {
  119. .pv_major = 1,
  120. .pv_minor = 1,
  121. };
  122. #define DLM_DOMAIN_BACKOFF_MS 200
  123. static int dlm_query_join_handler(struct o2net_msg *msg, u32 len, void *data,
  124. void **ret_data);
  125. static int dlm_assert_joined_handler(struct o2net_msg *msg, u32 len, void *data,
  126. void **ret_data);
  127. static int dlm_cancel_join_handler(struct o2net_msg *msg, u32 len, void *data,
  128. void **ret_data);
  129. static int dlm_query_region_handler(struct o2net_msg *msg, u32 len,
  130. void *data, void **ret_data);
  131. static int dlm_exit_domain_handler(struct o2net_msg *msg, u32 len, void *data,
  132. void **ret_data);
  133. static int dlm_protocol_compare(struct dlm_protocol_version *existing,
  134. struct dlm_protocol_version *request);
  135. static void dlm_unregister_domain_handlers(struct dlm_ctxt *dlm);
  136. void __dlm_unhash_lockres(struct dlm_lock_resource *lockres)
  137. {
  138. if (!hlist_unhashed(&lockres->hash_node)) {
  139. hlist_del_init(&lockres->hash_node);
  140. dlm_lockres_put(lockres);
  141. }
  142. }
  143. void __dlm_insert_lockres(struct dlm_ctxt *dlm,
  144. struct dlm_lock_resource *res)
  145. {
  146. struct hlist_head *bucket;
  147. struct qstr *q;
  148. assert_spin_locked(&dlm->spinlock);
  149. q = &res->lockname;
  150. bucket = dlm_lockres_hash(dlm, q->hash);
  151. /* get a reference for our hashtable */
  152. dlm_lockres_get(res);
  153. hlist_add_head(&res->hash_node, bucket);
  154. }
  155. struct dlm_lock_resource * __dlm_lookup_lockres_full(struct dlm_ctxt *dlm,
  156. const char *name,
  157. unsigned int len,
  158. unsigned int hash)
  159. {
  160. struct hlist_head *bucket;
  161. struct hlist_node *list;
  162. mlog_entry("%.*s\n", len, name);
  163. assert_spin_locked(&dlm->spinlock);
  164. bucket = dlm_lockres_hash(dlm, hash);
  165. hlist_for_each(list, bucket) {
  166. struct dlm_lock_resource *res = hlist_entry(list,
  167. struct dlm_lock_resource, hash_node);
  168. if (res->lockname.name[0] != name[0])
  169. continue;
  170. if (unlikely(res->lockname.len != len))
  171. continue;
  172. if (memcmp(res->lockname.name + 1, name + 1, len - 1))
  173. continue;
  174. dlm_lockres_get(res);
  175. return res;
  176. }
  177. return NULL;
  178. }
  179. /* intended to be called by functions which do not care about lock
  180. * resources which are being purged (most net _handler functions).
  181. * this will return NULL for any lock resource which is found but
  182. * currently in the process of dropping its mastery reference.
  183. * use __dlm_lookup_lockres_full when you need the lock resource
  184. * regardless (e.g. dlm_get_lock_resource) */
  185. struct dlm_lock_resource * __dlm_lookup_lockres(struct dlm_ctxt *dlm,
  186. const char *name,
  187. unsigned int len,
  188. unsigned int hash)
  189. {
  190. struct dlm_lock_resource *res = NULL;
  191. mlog_entry("%.*s\n", len, name);
  192. assert_spin_locked(&dlm->spinlock);
  193. res = __dlm_lookup_lockres_full(dlm, name, len, hash);
  194. if (res) {
  195. spin_lock(&res->spinlock);
  196. if (res->state & DLM_LOCK_RES_DROPPING_REF) {
  197. spin_unlock(&res->spinlock);
  198. dlm_lockres_put(res);
  199. return NULL;
  200. }
  201. spin_unlock(&res->spinlock);
  202. }
  203. return res;
  204. }
  205. struct dlm_lock_resource * dlm_lookup_lockres(struct dlm_ctxt *dlm,
  206. const char *name,
  207. unsigned int len)
  208. {
  209. struct dlm_lock_resource *res;
  210. unsigned int hash = dlm_lockid_hash(name, len);
  211. spin_lock(&dlm->spinlock);
  212. res = __dlm_lookup_lockres(dlm, name, len, hash);
  213. spin_unlock(&dlm->spinlock);
  214. return res;
  215. }
  216. static struct dlm_ctxt * __dlm_lookup_domain_full(const char *domain, int len)
  217. {
  218. struct dlm_ctxt *tmp = NULL;
  219. struct list_head *iter;
  220. assert_spin_locked(&dlm_domain_lock);
  221. /* tmp->name here is always NULL terminated,
  222. * but domain may not be! */
  223. list_for_each(iter, &dlm_domains) {
  224. tmp = list_entry (iter, struct dlm_ctxt, list);
  225. if (strlen(tmp->name) == len &&
  226. memcmp(tmp->name, domain, len)==0)
  227. break;
  228. tmp = NULL;
  229. }
  230. return tmp;
  231. }
  232. /* For null terminated domain strings ONLY */
  233. static struct dlm_ctxt * __dlm_lookup_domain(const char *domain)
  234. {
  235. assert_spin_locked(&dlm_domain_lock);
  236. return __dlm_lookup_domain_full(domain, strlen(domain));
  237. }
  238. /* returns true on one of two conditions:
  239. * 1) the domain does not exist
  240. * 2) the domain exists and it's state is "joined" */
  241. static int dlm_wait_on_domain_helper(const char *domain)
  242. {
  243. int ret = 0;
  244. struct dlm_ctxt *tmp = NULL;
  245. spin_lock(&dlm_domain_lock);
  246. tmp = __dlm_lookup_domain(domain);
  247. if (!tmp)
  248. ret = 1;
  249. else if (tmp->dlm_state == DLM_CTXT_JOINED)
  250. ret = 1;
  251. spin_unlock(&dlm_domain_lock);
  252. return ret;
  253. }
  254. static void dlm_free_ctxt_mem(struct dlm_ctxt *dlm)
  255. {
  256. dlm_destroy_debugfs_subroot(dlm);
  257. if (dlm->lockres_hash)
  258. dlm_free_pagevec((void **)dlm->lockres_hash, DLM_HASH_PAGES);
  259. if (dlm->master_hash)
  260. dlm_free_pagevec((void **)dlm->master_hash, DLM_HASH_PAGES);
  261. if (dlm->name)
  262. kfree(dlm->name);
  263. kfree(dlm);
  264. }
  265. /* A little strange - this function will be called while holding
  266. * dlm_domain_lock and is expected to be holding it on the way out. We
  267. * will however drop and reacquire it multiple times */
  268. static void dlm_ctxt_release(struct kref *kref)
  269. {
  270. struct dlm_ctxt *dlm;
  271. dlm = container_of(kref, struct dlm_ctxt, dlm_refs);
  272. BUG_ON(dlm->num_joins);
  273. BUG_ON(dlm->dlm_state == DLM_CTXT_JOINED);
  274. /* we may still be in the list if we hit an error during join. */
  275. list_del_init(&dlm->list);
  276. spin_unlock(&dlm_domain_lock);
  277. mlog(0, "freeing memory from domain %s\n", dlm->name);
  278. wake_up(&dlm_domain_events);
  279. dlm_free_ctxt_mem(dlm);
  280. spin_lock(&dlm_domain_lock);
  281. }
  282. void dlm_put(struct dlm_ctxt *dlm)
  283. {
  284. spin_lock(&dlm_domain_lock);
  285. kref_put(&dlm->dlm_refs, dlm_ctxt_release);
  286. spin_unlock(&dlm_domain_lock);
  287. }
  288. static void __dlm_get(struct dlm_ctxt *dlm)
  289. {
  290. kref_get(&dlm->dlm_refs);
  291. }
  292. /* given a questionable reference to a dlm object, gets a reference if
  293. * it can find it in the list, otherwise returns NULL in which case
  294. * you shouldn't trust your pointer. */
  295. struct dlm_ctxt *dlm_grab(struct dlm_ctxt *dlm)
  296. {
  297. struct list_head *iter;
  298. struct dlm_ctxt *target = NULL;
  299. spin_lock(&dlm_domain_lock);
  300. list_for_each(iter, &dlm_domains) {
  301. target = list_entry (iter, struct dlm_ctxt, list);
  302. if (target == dlm) {
  303. __dlm_get(target);
  304. break;
  305. }
  306. target = NULL;
  307. }
  308. spin_unlock(&dlm_domain_lock);
  309. return target;
  310. }
  311. int dlm_domain_fully_joined(struct dlm_ctxt *dlm)
  312. {
  313. int ret;
  314. spin_lock(&dlm_domain_lock);
  315. ret = (dlm->dlm_state == DLM_CTXT_JOINED) ||
  316. (dlm->dlm_state == DLM_CTXT_IN_SHUTDOWN);
  317. spin_unlock(&dlm_domain_lock);
  318. return ret;
  319. }
  320. static void dlm_destroy_dlm_worker(struct dlm_ctxt *dlm)
  321. {
  322. if (dlm->dlm_worker) {
  323. flush_workqueue(dlm->dlm_worker);
  324. destroy_workqueue(dlm->dlm_worker);
  325. dlm->dlm_worker = NULL;
  326. }
  327. }
  328. static void dlm_complete_dlm_shutdown(struct dlm_ctxt *dlm)
  329. {
  330. dlm_unregister_domain_handlers(dlm);
  331. dlm_debug_shutdown(dlm);
  332. dlm_complete_thread(dlm);
  333. dlm_complete_recovery_thread(dlm);
  334. dlm_destroy_dlm_worker(dlm);
  335. /* We've left the domain. Now we can take ourselves out of the
  336. * list and allow the kref stuff to help us free the
  337. * memory. */
  338. spin_lock(&dlm_domain_lock);
  339. list_del_init(&dlm->list);
  340. spin_unlock(&dlm_domain_lock);
  341. /* Wake up anyone waiting for us to remove this domain */
  342. wake_up(&dlm_domain_events);
  343. }
  344. static int dlm_migrate_all_locks(struct dlm_ctxt *dlm)
  345. {
  346. int i, num, n, ret = 0;
  347. struct dlm_lock_resource *res;
  348. struct hlist_node *iter;
  349. struct hlist_head *bucket;
  350. int dropped;
  351. mlog(0, "Migrating locks from domain %s\n", dlm->name);
  352. num = 0;
  353. spin_lock(&dlm->spinlock);
  354. for (i = 0; i < DLM_HASH_BUCKETS; i++) {
  355. redo_bucket:
  356. n = 0;
  357. bucket = dlm_lockres_hash(dlm, i);
  358. iter = bucket->first;
  359. while (iter) {
  360. n++;
  361. res = hlist_entry(iter, struct dlm_lock_resource,
  362. hash_node);
  363. dlm_lockres_get(res);
  364. /* migrate, if necessary. this will drop the dlm
  365. * spinlock and retake it if it does migration. */
  366. dropped = dlm_empty_lockres(dlm, res);
  367. spin_lock(&res->spinlock);
  368. __dlm_lockres_calc_usage(dlm, res);
  369. iter = res->hash_node.next;
  370. spin_unlock(&res->spinlock);
  371. dlm_lockres_put(res);
  372. if (dropped)
  373. goto redo_bucket;
  374. }
  375. cond_resched_lock(&dlm->spinlock);
  376. num += n;
  377. mlog(0, "%s: touched %d lockreses in bucket %d "
  378. "(tot=%d)\n", dlm->name, n, i, num);
  379. }
  380. spin_unlock(&dlm->spinlock);
  381. wake_up(&dlm->dlm_thread_wq);
  382. /* let the dlm thread take care of purging, keep scanning until
  383. * nothing remains in the hash */
  384. if (num) {
  385. mlog(0, "%s: %d lock resources in hash last pass\n",
  386. dlm->name, num);
  387. ret = -EAGAIN;
  388. }
  389. mlog(0, "DONE Migrating locks from domain %s\n", dlm->name);
  390. return ret;
  391. }
  392. static int dlm_no_joining_node(struct dlm_ctxt *dlm)
  393. {
  394. int ret;
  395. spin_lock(&dlm->spinlock);
  396. ret = dlm->joining_node == DLM_LOCK_RES_OWNER_UNKNOWN;
  397. spin_unlock(&dlm->spinlock);
  398. return ret;
  399. }
  400. static void dlm_mark_domain_leaving(struct dlm_ctxt *dlm)
  401. {
  402. /* Yikes, a double spinlock! I need domain_lock for the dlm
  403. * state and the dlm spinlock for join state... Sorry! */
  404. again:
  405. spin_lock(&dlm_domain_lock);
  406. spin_lock(&dlm->spinlock);
  407. if (dlm->joining_node != DLM_LOCK_RES_OWNER_UNKNOWN) {
  408. mlog(0, "Node %d is joining, we wait on it.\n",
  409. dlm->joining_node);
  410. spin_unlock(&dlm->spinlock);
  411. spin_unlock(&dlm_domain_lock);
  412. wait_event(dlm->dlm_join_events, dlm_no_joining_node(dlm));
  413. goto again;
  414. }
  415. dlm->dlm_state = DLM_CTXT_LEAVING;
  416. spin_unlock(&dlm->spinlock);
  417. spin_unlock(&dlm_domain_lock);
  418. }
  419. static void __dlm_print_nodes(struct dlm_ctxt *dlm)
  420. {
  421. int node = -1;
  422. assert_spin_locked(&dlm->spinlock);
  423. printk(KERN_NOTICE "o2dlm: Nodes in domain %s: ", dlm->name);
  424. while ((node = find_next_bit(dlm->domain_map, O2NM_MAX_NODES,
  425. node + 1)) < O2NM_MAX_NODES) {
  426. printk("%d ", node);
  427. }
  428. printk("\n");
  429. }
  430. static int dlm_exit_domain_handler(struct o2net_msg *msg, u32 len, void *data,
  431. void **ret_data)
  432. {
  433. struct dlm_ctxt *dlm = data;
  434. unsigned int node;
  435. struct dlm_exit_domain *exit_msg = (struct dlm_exit_domain *) msg->buf;
  436. mlog_entry("%p %u %p", msg, len, data);
  437. if (!dlm_grab(dlm))
  438. return 0;
  439. node = exit_msg->node_idx;
  440. printk(KERN_NOTICE "o2dlm: Node %u leaves domain %s\n", node, dlm->name);
  441. spin_lock(&dlm->spinlock);
  442. clear_bit(node, dlm->domain_map);
  443. __dlm_print_nodes(dlm);
  444. /* notify anything attached to the heartbeat events */
  445. dlm_hb_event_notify_attached(dlm, node, 0);
  446. spin_unlock(&dlm->spinlock);
  447. dlm_put(dlm);
  448. return 0;
  449. }
  450. static int dlm_send_one_domain_exit(struct dlm_ctxt *dlm,
  451. unsigned int node)
  452. {
  453. int status;
  454. struct dlm_exit_domain leave_msg;
  455. mlog(0, "Asking node %u if we can leave the domain %s me = %u\n",
  456. node, dlm->name, dlm->node_num);
  457. memset(&leave_msg, 0, sizeof(leave_msg));
  458. leave_msg.node_idx = dlm->node_num;
  459. status = o2net_send_message(DLM_EXIT_DOMAIN_MSG, dlm->key,
  460. &leave_msg, sizeof(leave_msg), node,
  461. NULL);
  462. if (status < 0)
  463. mlog(ML_ERROR, "Error %d when sending message %u (key 0x%x) to "
  464. "node %u\n", status, DLM_EXIT_DOMAIN_MSG, dlm->key, node);
  465. mlog(0, "status return %d from o2net_send_message\n", status);
  466. return status;
  467. }
  468. static void dlm_leave_domain(struct dlm_ctxt *dlm)
  469. {
  470. int node, clear_node, status;
  471. /* At this point we've migrated away all our locks and won't
  472. * accept mastership of new ones. The dlm is responsible for
  473. * almost nothing now. We make sure not to confuse any joining
  474. * nodes and then commence shutdown procedure. */
  475. spin_lock(&dlm->spinlock);
  476. /* Clear ourselves from the domain map */
  477. clear_bit(dlm->node_num, dlm->domain_map);
  478. while ((node = find_next_bit(dlm->domain_map, O2NM_MAX_NODES,
  479. 0)) < O2NM_MAX_NODES) {
  480. /* Drop the dlm spinlock. This is safe wrt the domain_map.
  481. * -nodes cannot be added now as the
  482. * query_join_handlers knows to respond with OK_NO_MAP
  483. * -we catch the right network errors if a node is
  484. * removed from the map while we're sending him the
  485. * exit message. */
  486. spin_unlock(&dlm->spinlock);
  487. clear_node = 1;
  488. status = dlm_send_one_domain_exit(dlm, node);
  489. if (status < 0 &&
  490. status != -ENOPROTOOPT &&
  491. status != -ENOTCONN) {
  492. mlog(ML_NOTICE, "Error %d sending domain exit message "
  493. "to node %d\n", status, node);
  494. /* Not sure what to do here but lets sleep for
  495. * a bit in case this was a transient
  496. * error... */
  497. msleep(DLM_DOMAIN_BACKOFF_MS);
  498. clear_node = 0;
  499. }
  500. spin_lock(&dlm->spinlock);
  501. /* If we're not clearing the node bit then we intend
  502. * to loop back around to try again. */
  503. if (clear_node)
  504. clear_bit(node, dlm->domain_map);
  505. }
  506. spin_unlock(&dlm->spinlock);
  507. }
  508. int dlm_joined(struct dlm_ctxt *dlm)
  509. {
  510. int ret = 0;
  511. spin_lock(&dlm_domain_lock);
  512. if (dlm->dlm_state == DLM_CTXT_JOINED)
  513. ret = 1;
  514. spin_unlock(&dlm_domain_lock);
  515. return ret;
  516. }
  517. int dlm_shutting_down(struct dlm_ctxt *dlm)
  518. {
  519. int ret = 0;
  520. spin_lock(&dlm_domain_lock);
  521. if (dlm->dlm_state == DLM_CTXT_IN_SHUTDOWN)
  522. ret = 1;
  523. spin_unlock(&dlm_domain_lock);
  524. return ret;
  525. }
  526. void dlm_unregister_domain(struct dlm_ctxt *dlm)
  527. {
  528. int leave = 0;
  529. struct dlm_lock_resource *res;
  530. spin_lock(&dlm_domain_lock);
  531. BUG_ON(dlm->dlm_state != DLM_CTXT_JOINED);
  532. BUG_ON(!dlm->num_joins);
  533. dlm->num_joins--;
  534. if (!dlm->num_joins) {
  535. /* We mark it "in shutdown" now so new register
  536. * requests wait until we've completely left the
  537. * domain. Don't use DLM_CTXT_LEAVING yet as we still
  538. * want new domain joins to communicate with us at
  539. * least until we've completed migration of our
  540. * resources. */
  541. dlm->dlm_state = DLM_CTXT_IN_SHUTDOWN;
  542. leave = 1;
  543. }
  544. spin_unlock(&dlm_domain_lock);
  545. if (leave) {
  546. mlog(0, "shutting down domain %s\n", dlm->name);
  547. /* We changed dlm state, notify the thread */
  548. dlm_kick_thread(dlm, NULL);
  549. while (dlm_migrate_all_locks(dlm)) {
  550. /* Give dlm_thread time to purge the lockres' */
  551. msleep(500);
  552. mlog(0, "%s: more migration to do\n", dlm->name);
  553. }
  554. /* This list should be empty. If not, print remaining lockres */
  555. if (!list_empty(&dlm->tracking_list)) {
  556. mlog(ML_ERROR, "Following lockres' are still on the "
  557. "tracking list:\n");
  558. list_for_each_entry(res, &dlm->tracking_list, tracking)
  559. dlm_print_one_lock_resource(res);
  560. }
  561. dlm_mark_domain_leaving(dlm);
  562. dlm_leave_domain(dlm);
  563. dlm_force_free_mles(dlm);
  564. dlm_complete_dlm_shutdown(dlm);
  565. }
  566. dlm_put(dlm);
  567. }
  568. EXPORT_SYMBOL_GPL(dlm_unregister_domain);
  569. static int dlm_query_join_proto_check(char *proto_type, int node,
  570. struct dlm_protocol_version *ours,
  571. struct dlm_protocol_version *request)
  572. {
  573. int rc;
  574. struct dlm_protocol_version proto = *request;
  575. if (!dlm_protocol_compare(ours, &proto)) {
  576. mlog(0,
  577. "node %u wanted to join with %s locking protocol "
  578. "%u.%u, we respond with %u.%u\n",
  579. node, proto_type,
  580. request->pv_major,
  581. request->pv_minor,
  582. proto.pv_major, proto.pv_minor);
  583. request->pv_minor = proto.pv_minor;
  584. rc = 0;
  585. } else {
  586. mlog(ML_NOTICE,
  587. "Node %u wanted to join with %s locking "
  588. "protocol %u.%u, but we have %u.%u, disallowing\n",
  589. node, proto_type,
  590. request->pv_major,
  591. request->pv_minor,
  592. ours->pv_major,
  593. ours->pv_minor);
  594. rc = 1;
  595. }
  596. return rc;
  597. }
  598. /*
  599. * struct dlm_query_join_packet is made up of four one-byte fields. They
  600. * are effectively in big-endian order already. However, little-endian
  601. * machines swap them before putting the packet on the wire (because
  602. * query_join's response is a status, and that status is treated as a u32
  603. * on the wire). Thus, a big-endian and little-endian machines will treat
  604. * this structure differently.
  605. *
  606. * The solution is to have little-endian machines swap the structure when
  607. * converting from the structure to the u32 representation. This will
  608. * result in the structure having the correct format on the wire no matter
  609. * the host endian format.
  610. */
  611. static void dlm_query_join_packet_to_wire(struct dlm_query_join_packet *packet,
  612. u32 *wire)
  613. {
  614. union dlm_query_join_response response;
  615. response.packet = *packet;
  616. *wire = cpu_to_be32(response.intval);
  617. }
  618. static void dlm_query_join_wire_to_packet(u32 wire,
  619. struct dlm_query_join_packet *packet)
  620. {
  621. union dlm_query_join_response response;
  622. response.intval = cpu_to_be32(wire);
  623. *packet = response.packet;
  624. }
  625. static int dlm_query_join_handler(struct o2net_msg *msg, u32 len, void *data,
  626. void **ret_data)
  627. {
  628. struct dlm_query_join_request *query;
  629. struct dlm_query_join_packet packet = {
  630. .code = JOIN_DISALLOW,
  631. };
  632. struct dlm_ctxt *dlm = NULL;
  633. u32 response;
  634. u8 nodenum;
  635. query = (struct dlm_query_join_request *) msg->buf;
  636. mlog(0, "node %u wants to join domain %s\n", query->node_idx,
  637. query->domain);
  638. /*
  639. * If heartbeat doesn't consider the node live, tell it
  640. * to back off and try again. This gives heartbeat a chance
  641. * to catch up.
  642. */
  643. if (!o2hb_check_node_heartbeating(query->node_idx)) {
  644. mlog(0, "node %u is not in our live map yet\n",
  645. query->node_idx);
  646. packet.code = JOIN_DISALLOW;
  647. goto respond;
  648. }
  649. packet.code = JOIN_OK_NO_MAP;
  650. spin_lock(&dlm_domain_lock);
  651. dlm = __dlm_lookup_domain_full(query->domain, query->name_len);
  652. if (!dlm)
  653. goto unlock_respond;
  654. /*
  655. * There is a small window where the joining node may not see the
  656. * node(s) that just left but still part of the cluster. DISALLOW
  657. * join request if joining node has different node map.
  658. */
  659. nodenum=0;
  660. while (nodenum < O2NM_MAX_NODES) {
  661. if (test_bit(nodenum, dlm->domain_map)) {
  662. if (!byte_test_bit(nodenum, query->node_map)) {
  663. mlog(0, "disallow join as node %u does not "
  664. "have node %u in its nodemap\n",
  665. query->node_idx, nodenum);
  666. packet.code = JOIN_DISALLOW;
  667. goto unlock_respond;
  668. }
  669. }
  670. nodenum++;
  671. }
  672. /* Once the dlm ctxt is marked as leaving then we don't want
  673. * to be put in someone's domain map.
  674. * Also, explicitly disallow joining at certain troublesome
  675. * times (ie. during recovery). */
  676. if (dlm && dlm->dlm_state != DLM_CTXT_LEAVING) {
  677. int bit = query->node_idx;
  678. spin_lock(&dlm->spinlock);
  679. if (dlm->dlm_state == DLM_CTXT_NEW &&
  680. dlm->joining_node == DLM_LOCK_RES_OWNER_UNKNOWN) {
  681. /*If this is a brand new context and we
  682. * haven't started our join process yet, then
  683. * the other node won the race. */
  684. packet.code = JOIN_OK_NO_MAP;
  685. } else if (dlm->joining_node != DLM_LOCK_RES_OWNER_UNKNOWN) {
  686. /* Disallow parallel joins. */
  687. packet.code = JOIN_DISALLOW;
  688. } else if (dlm->reco.state & DLM_RECO_STATE_ACTIVE) {
  689. mlog(0, "node %u trying to join, but recovery "
  690. "is ongoing.\n", bit);
  691. packet.code = JOIN_DISALLOW;
  692. } else if (test_bit(bit, dlm->recovery_map)) {
  693. mlog(0, "node %u trying to join, but it "
  694. "still needs recovery.\n", bit);
  695. packet.code = JOIN_DISALLOW;
  696. } else if (test_bit(bit, dlm->domain_map)) {
  697. mlog(0, "node %u trying to join, but it "
  698. "is still in the domain! needs recovery?\n",
  699. bit);
  700. packet.code = JOIN_DISALLOW;
  701. } else {
  702. /* Alright we're fully a part of this domain
  703. * so we keep some state as to who's joining
  704. * and indicate to him that needs to be fixed
  705. * up. */
  706. /* Make sure we speak compatible locking protocols. */
  707. if (dlm_query_join_proto_check("DLM", bit,
  708. &dlm->dlm_locking_proto,
  709. &query->dlm_proto)) {
  710. packet.code = JOIN_PROTOCOL_MISMATCH;
  711. } else if (dlm_query_join_proto_check("fs", bit,
  712. &dlm->fs_locking_proto,
  713. &query->fs_proto)) {
  714. packet.code = JOIN_PROTOCOL_MISMATCH;
  715. } else {
  716. packet.dlm_minor = query->dlm_proto.pv_minor;
  717. packet.fs_minor = query->fs_proto.pv_minor;
  718. packet.code = JOIN_OK;
  719. __dlm_set_joining_node(dlm, query->node_idx);
  720. }
  721. }
  722. spin_unlock(&dlm->spinlock);
  723. }
  724. unlock_respond:
  725. spin_unlock(&dlm_domain_lock);
  726. respond:
  727. mlog(0, "We respond with %u\n", packet.code);
  728. dlm_query_join_packet_to_wire(&packet, &response);
  729. return response;
  730. }
  731. static int dlm_assert_joined_handler(struct o2net_msg *msg, u32 len, void *data,
  732. void **ret_data)
  733. {
  734. struct dlm_assert_joined *assert;
  735. struct dlm_ctxt *dlm = NULL;
  736. assert = (struct dlm_assert_joined *) msg->buf;
  737. mlog(0, "node %u asserts join on domain %s\n", assert->node_idx,
  738. assert->domain);
  739. spin_lock(&dlm_domain_lock);
  740. dlm = __dlm_lookup_domain_full(assert->domain, assert->name_len);
  741. /* XXX should we consider no dlm ctxt an error? */
  742. if (dlm) {
  743. spin_lock(&dlm->spinlock);
  744. /* Alright, this node has officially joined our
  745. * domain. Set him in the map and clean up our
  746. * leftover join state. */
  747. BUG_ON(dlm->joining_node != assert->node_idx);
  748. set_bit(assert->node_idx, dlm->domain_map);
  749. __dlm_set_joining_node(dlm, DLM_LOCK_RES_OWNER_UNKNOWN);
  750. printk(KERN_NOTICE "o2dlm: Node %u joins domain %s\n",
  751. assert->node_idx, dlm->name);
  752. __dlm_print_nodes(dlm);
  753. /* notify anything attached to the heartbeat events */
  754. dlm_hb_event_notify_attached(dlm, assert->node_idx, 1);
  755. spin_unlock(&dlm->spinlock);
  756. }
  757. spin_unlock(&dlm_domain_lock);
  758. return 0;
  759. }
  760. static int dlm_match_regions(struct dlm_ctxt *dlm,
  761. struct dlm_query_region *qr)
  762. {
  763. char *local = NULL, *remote = qr->qr_regions;
  764. char *l, *r;
  765. int localnr, i, j, foundit;
  766. int status = 0;
  767. if (!o2hb_global_heartbeat_active()) {
  768. if (qr->qr_numregions) {
  769. mlog(ML_ERROR, "Domain %s: Joining node %d has global "
  770. "heartbeat enabled but local node %d does not\n",
  771. qr->qr_domain, qr->qr_node, dlm->node_num);
  772. status = -EINVAL;
  773. }
  774. goto bail;
  775. }
  776. if (o2hb_global_heartbeat_active() && !qr->qr_numregions) {
  777. mlog(ML_ERROR, "Domain %s: Local node %d has global "
  778. "heartbeat enabled but joining node %d does not\n",
  779. qr->qr_domain, dlm->node_num, qr->qr_node);
  780. status = -EINVAL;
  781. goto bail;
  782. }
  783. r = remote;
  784. for (i = 0; i < qr->qr_numregions; ++i) {
  785. mlog(0, "Region %.*s\n", O2HB_MAX_REGION_NAME_LEN, r);
  786. r += O2HB_MAX_REGION_NAME_LEN;
  787. }
  788. local = kmalloc(sizeof(qr->qr_regions), GFP_KERNEL);
  789. if (!local) {
  790. status = -ENOMEM;
  791. goto bail;
  792. }
  793. localnr = o2hb_get_all_regions(local, O2NM_MAX_REGIONS);
  794. /* compare local regions with remote */
  795. l = local;
  796. for (i = 0; i < localnr; ++i) {
  797. foundit = 0;
  798. r = remote;
  799. for (j = 0; j <= qr->qr_numregions; ++j) {
  800. if (!memcmp(l, r, O2HB_MAX_REGION_NAME_LEN)) {
  801. foundit = 1;
  802. break;
  803. }
  804. r += O2HB_MAX_REGION_NAME_LEN;
  805. }
  806. if (!foundit) {
  807. status = -EINVAL;
  808. mlog(ML_ERROR, "Domain %s: Region '%.*s' registered "
  809. "in local node %d but not in joining node %d\n",
  810. qr->qr_domain, O2HB_MAX_REGION_NAME_LEN, l,
  811. dlm->node_num, qr->qr_node);
  812. goto bail;
  813. }
  814. l += O2HB_MAX_REGION_NAME_LEN;
  815. }
  816. /* compare remote with local regions */
  817. r = remote;
  818. for (i = 0; i < qr->qr_numregions; ++i) {
  819. foundit = 0;
  820. l = local;
  821. for (j = 0; j < localnr; ++j) {
  822. if (!memcmp(r, l, O2HB_MAX_REGION_NAME_LEN)) {
  823. foundit = 1;
  824. break;
  825. }
  826. l += O2HB_MAX_REGION_NAME_LEN;
  827. }
  828. if (!foundit) {
  829. status = -EINVAL;
  830. mlog(ML_ERROR, "Domain %s: Region '%.*s' registered "
  831. "in joining node %d but not in local node %d\n",
  832. qr->qr_domain, O2HB_MAX_REGION_NAME_LEN, r,
  833. qr->qr_node, dlm->node_num);
  834. goto bail;
  835. }
  836. r += O2HB_MAX_REGION_NAME_LEN;
  837. }
  838. bail:
  839. kfree(local);
  840. return status;
  841. }
  842. static int dlm_send_regions(struct dlm_ctxt *dlm, unsigned long *node_map)
  843. {
  844. struct dlm_query_region *qr = NULL;
  845. int status, ret = 0, i;
  846. char *p;
  847. if (find_next_bit(node_map, O2NM_MAX_NODES, 0) >= O2NM_MAX_NODES)
  848. goto bail;
  849. qr = kzalloc(sizeof(struct dlm_query_region), GFP_KERNEL);
  850. if (!qr) {
  851. ret = -ENOMEM;
  852. mlog_errno(ret);
  853. goto bail;
  854. }
  855. qr->qr_node = dlm->node_num;
  856. qr->qr_namelen = strlen(dlm->name);
  857. memcpy(qr->qr_domain, dlm->name, qr->qr_namelen);
  858. /* if local hb, the numregions will be zero */
  859. if (o2hb_global_heartbeat_active())
  860. qr->qr_numregions = o2hb_get_all_regions(qr->qr_regions,
  861. O2NM_MAX_REGIONS);
  862. p = qr->qr_regions;
  863. for (i = 0; i < qr->qr_numregions; ++i, p += O2HB_MAX_REGION_NAME_LEN)
  864. mlog(0, "Region %.*s\n", O2HB_MAX_REGION_NAME_LEN, p);
  865. i = -1;
  866. while ((i = find_next_bit(node_map, O2NM_MAX_NODES,
  867. i + 1)) < O2NM_MAX_NODES) {
  868. if (i == dlm->node_num)
  869. continue;
  870. mlog(0, "Sending regions to node %d\n", i);
  871. ret = o2net_send_message(DLM_QUERY_REGION, DLM_MOD_KEY, qr,
  872. sizeof(struct dlm_query_region),
  873. i, &status);
  874. if (ret >= 0)
  875. ret = status;
  876. if (ret) {
  877. mlog(ML_ERROR, "Region mismatch %d, node %d\n",
  878. ret, i);
  879. break;
  880. }
  881. }
  882. bail:
  883. kfree(qr);
  884. return ret;
  885. }
  886. static int dlm_query_region_handler(struct o2net_msg *msg, u32 len,
  887. void *data, void **ret_data)
  888. {
  889. struct dlm_query_region *qr;
  890. struct dlm_ctxt *dlm = NULL;
  891. int status = 0;
  892. int locked = 0;
  893. qr = (struct dlm_query_region *) msg->buf;
  894. mlog(0, "Node %u queries hb regions on domain %s\n", qr->qr_node,
  895. qr->qr_domain);
  896. status = -EINVAL;
  897. spin_lock(&dlm_domain_lock);
  898. dlm = __dlm_lookup_domain_full(qr->qr_domain, qr->qr_namelen);
  899. if (!dlm) {
  900. mlog(ML_ERROR, "Node %d queried hb regions on domain %s "
  901. "before join domain\n", qr->qr_node, qr->qr_domain);
  902. goto bail;
  903. }
  904. spin_lock(&dlm->spinlock);
  905. locked = 1;
  906. if (dlm->joining_node != qr->qr_node) {
  907. mlog(ML_ERROR, "Node %d queried hb regions on domain %s "
  908. "but joining node is %d\n", qr->qr_node, qr->qr_domain,
  909. dlm->joining_node);
  910. goto bail;
  911. }
  912. /* Support for global heartbeat was added in 1.1 */
  913. if (dlm->dlm_locking_proto.pv_major == 1 &&
  914. dlm->dlm_locking_proto.pv_minor == 0) {
  915. mlog(ML_ERROR, "Node %d queried hb regions on domain %s "
  916. "but active dlm protocol is %d.%d\n", qr->qr_node,
  917. qr->qr_domain, dlm->dlm_locking_proto.pv_major,
  918. dlm->dlm_locking_proto.pv_minor);
  919. goto bail;
  920. }
  921. status = dlm_match_regions(dlm, qr);
  922. bail:
  923. if (locked)
  924. spin_unlock(&dlm->spinlock);
  925. spin_unlock(&dlm_domain_lock);
  926. return status;
  927. }
  928. static int dlm_match_nodes(struct dlm_ctxt *dlm, struct dlm_query_nodeinfo *qn)
  929. {
  930. struct o2nm_node *local;
  931. struct dlm_node_info *remote;
  932. int i, j;
  933. int status = 0;
  934. for (j = 0; j < qn->qn_numnodes; ++j)
  935. mlog(0, "Node %3d, %pI4:%u\n", qn->qn_nodes[j].ni_nodenum,
  936. &(qn->qn_nodes[j].ni_ipv4_address),
  937. ntohs(qn->qn_nodes[j].ni_ipv4_port));
  938. for (i = 0; i < O2NM_MAX_NODES && !status; ++i) {
  939. local = o2nm_get_node_by_num(i);
  940. remote = NULL;
  941. for (j = 0; j < qn->qn_numnodes; ++j) {
  942. if (qn->qn_nodes[j].ni_nodenum == i) {
  943. remote = &(qn->qn_nodes[j]);
  944. break;
  945. }
  946. }
  947. if (!local && !remote)
  948. continue;
  949. if ((local && !remote) || (!local && remote))
  950. status = -EINVAL;
  951. if (!status &&
  952. ((remote->ni_nodenum != local->nd_num) ||
  953. (remote->ni_ipv4_port != local->nd_ipv4_port) ||
  954. (remote->ni_ipv4_address != local->nd_ipv4_address)))
  955. status = -EINVAL;
  956. if (status) {
  957. if (remote && !local)
  958. mlog(ML_ERROR, "Domain %s: Node %d (%pI4:%u) "
  959. "registered in joining node %d but not in "
  960. "local node %d\n", qn->qn_domain,
  961. remote->ni_nodenum,
  962. &(remote->ni_ipv4_address),
  963. ntohs(remote->ni_ipv4_port),
  964. qn->qn_nodenum, dlm->node_num);
  965. if (local && !remote)
  966. mlog(ML_ERROR, "Domain %s: Node %d (%pI4:%u) "
  967. "registered in local node %d but not in "
  968. "joining node %d\n", qn->qn_domain,
  969. local->nd_num, &(local->nd_ipv4_address),
  970. ntohs(local->nd_ipv4_port),
  971. dlm->node_num, qn->qn_nodenum);
  972. BUG_ON((!local && !remote));
  973. }
  974. if (local)
  975. o2nm_node_put(local);
  976. }
  977. return status;
  978. }
  979. static int dlm_send_nodeinfo(struct dlm_ctxt *dlm, unsigned long *node_map)
  980. {
  981. struct dlm_query_nodeinfo *qn = NULL;
  982. struct o2nm_node *node;
  983. int ret = 0, status, count, i;
  984. if (find_next_bit(node_map, O2NM_MAX_NODES, 0) >= O2NM_MAX_NODES)
  985. goto bail;
  986. qn = kzalloc(sizeof(struct dlm_query_nodeinfo), GFP_KERNEL);
  987. if (!qn) {
  988. ret = -ENOMEM;
  989. mlog_errno(ret);
  990. goto bail;
  991. }
  992. for (i = 0, count = 0; i < O2NM_MAX_NODES; ++i) {
  993. node = o2nm_get_node_by_num(i);
  994. if (!node)
  995. continue;
  996. qn->qn_nodes[count].ni_nodenum = node->nd_num;
  997. qn->qn_nodes[count].ni_ipv4_port = node->nd_ipv4_port;
  998. qn->qn_nodes[count].ni_ipv4_address = node->nd_ipv4_address;
  999. mlog(0, "Node %3d, %pI4:%u\n", node->nd_num,
  1000. &(node->nd_ipv4_address), ntohs(node->nd_ipv4_port));
  1001. ++count;
  1002. o2nm_node_put(node);
  1003. }
  1004. qn->qn_nodenum = dlm->node_num;
  1005. qn->qn_numnodes = count;
  1006. qn->qn_namelen = strlen(dlm->name);
  1007. memcpy(qn->qn_domain, dlm->name, qn->qn_namelen);
  1008. i = -1;
  1009. while ((i = find_next_bit(node_map, O2NM_MAX_NODES,
  1010. i + 1)) < O2NM_MAX_NODES) {
  1011. if (i == dlm->node_num)
  1012. continue;
  1013. mlog(0, "Sending nodeinfo to node %d\n", i);
  1014. ret = o2net_send_message(DLM_QUERY_NODEINFO, DLM_MOD_KEY,
  1015. qn, sizeof(struct dlm_query_nodeinfo),
  1016. i, &status);
  1017. if (ret >= 0)
  1018. ret = status;
  1019. if (ret) {
  1020. mlog(ML_ERROR, "node mismatch %d, node %d\n", ret, i);
  1021. break;
  1022. }
  1023. }
  1024. bail:
  1025. kfree(qn);
  1026. return ret;
  1027. }
  1028. static int dlm_query_nodeinfo_handler(struct o2net_msg *msg, u32 len,
  1029. void *data, void **ret_data)
  1030. {
  1031. struct dlm_query_nodeinfo *qn;
  1032. struct dlm_ctxt *dlm = NULL;
  1033. int locked = 0, status = -EINVAL;
  1034. qn = (struct dlm_query_nodeinfo *) msg->buf;
  1035. mlog(0, "Node %u queries nodes on domain %s\n", qn->qn_nodenum,
  1036. qn->qn_domain);
  1037. spin_lock(&dlm_domain_lock);
  1038. dlm = __dlm_lookup_domain_full(qn->qn_domain, qn->qn_namelen);
  1039. if (!dlm) {
  1040. mlog(ML_ERROR, "Node %d queried nodes on domain %s before "
  1041. "join domain\n", qn->qn_nodenum, qn->qn_domain);
  1042. goto bail;
  1043. }
  1044. spin_lock(&dlm->spinlock);
  1045. locked = 1;
  1046. if (dlm->joining_node != qn->qn_nodenum) {
  1047. mlog(ML_ERROR, "Node %d queried nodes on domain %s but "
  1048. "joining node is %d\n", qn->qn_nodenum, qn->qn_domain,
  1049. dlm->joining_node);
  1050. goto bail;
  1051. }
  1052. /* Support for node query was added in 1.1 */
  1053. if (dlm->dlm_locking_proto.pv_major == 1 &&
  1054. dlm->dlm_locking_proto.pv_minor == 0) {
  1055. mlog(ML_ERROR, "Node %d queried nodes on domain %s "
  1056. "but active dlm protocol is %d.%d\n", qn->qn_nodenum,
  1057. qn->qn_domain, dlm->dlm_locking_proto.pv_major,
  1058. dlm->dlm_locking_proto.pv_minor);
  1059. goto bail;
  1060. }
  1061. status = dlm_match_nodes(dlm, qn);
  1062. bail:
  1063. if (locked)
  1064. spin_unlock(&dlm->spinlock);
  1065. spin_unlock(&dlm_domain_lock);
  1066. return status;
  1067. }
  1068. static int dlm_cancel_join_handler(struct o2net_msg *msg, u32 len, void *data,
  1069. void **ret_data)
  1070. {
  1071. struct dlm_cancel_join *cancel;
  1072. struct dlm_ctxt *dlm = NULL;
  1073. cancel = (struct dlm_cancel_join *) msg->buf;
  1074. mlog(0, "node %u cancels join on domain %s\n", cancel->node_idx,
  1075. cancel->domain);
  1076. spin_lock(&dlm_domain_lock);
  1077. dlm = __dlm_lookup_domain_full(cancel->domain, cancel->name_len);
  1078. if (dlm) {
  1079. spin_lock(&dlm->spinlock);
  1080. /* Yikes, this guy wants to cancel his join. No
  1081. * problem, we simply cleanup our join state. */
  1082. BUG_ON(dlm->joining_node != cancel->node_idx);
  1083. __dlm_set_joining_node(dlm, DLM_LOCK_RES_OWNER_UNKNOWN);
  1084. spin_unlock(&dlm->spinlock);
  1085. }
  1086. spin_unlock(&dlm_domain_lock);
  1087. return 0;
  1088. }
  1089. static int dlm_send_one_join_cancel(struct dlm_ctxt *dlm,
  1090. unsigned int node)
  1091. {
  1092. int status;
  1093. struct dlm_cancel_join cancel_msg;
  1094. memset(&cancel_msg, 0, sizeof(cancel_msg));
  1095. cancel_msg.node_idx = dlm->node_num;
  1096. cancel_msg.name_len = strlen(dlm->name);
  1097. memcpy(cancel_msg.domain, dlm->name, cancel_msg.name_len);
  1098. status = o2net_send_message(DLM_CANCEL_JOIN_MSG, DLM_MOD_KEY,
  1099. &cancel_msg, sizeof(cancel_msg), node,
  1100. NULL);
  1101. if (status < 0) {
  1102. mlog(ML_ERROR, "Error %d when sending message %u (key 0x%x) to "
  1103. "node %u\n", status, DLM_CANCEL_JOIN_MSG, DLM_MOD_KEY,
  1104. node);
  1105. goto bail;
  1106. }
  1107. bail:
  1108. return status;
  1109. }
  1110. /* map_size should be in bytes. */
  1111. static int dlm_send_join_cancels(struct dlm_ctxt *dlm,
  1112. unsigned long *node_map,
  1113. unsigned int map_size)
  1114. {
  1115. int status, tmpstat;
  1116. unsigned int node;
  1117. if (map_size != (BITS_TO_LONGS(O2NM_MAX_NODES) *
  1118. sizeof(unsigned long))) {
  1119. mlog(ML_ERROR,
  1120. "map_size %u != BITS_TO_LONGS(O2NM_MAX_NODES) %u\n",
  1121. map_size, (unsigned)BITS_TO_LONGS(O2NM_MAX_NODES));
  1122. return -EINVAL;
  1123. }
  1124. status = 0;
  1125. node = -1;
  1126. while ((node = find_next_bit(node_map, O2NM_MAX_NODES,
  1127. node + 1)) < O2NM_MAX_NODES) {
  1128. if (node == dlm->node_num)
  1129. continue;
  1130. tmpstat = dlm_send_one_join_cancel(dlm, node);
  1131. if (tmpstat) {
  1132. mlog(ML_ERROR, "Error return %d cancelling join on "
  1133. "node %d\n", tmpstat, node);
  1134. if (!status)
  1135. status = tmpstat;
  1136. }
  1137. }
  1138. if (status)
  1139. mlog_errno(status);
  1140. return status;
  1141. }
  1142. static int dlm_request_join(struct dlm_ctxt *dlm,
  1143. int node,
  1144. enum dlm_query_join_response_code *response)
  1145. {
  1146. int status;
  1147. struct dlm_query_join_request join_msg;
  1148. struct dlm_query_join_packet packet;
  1149. u32 join_resp;
  1150. mlog(0, "querying node %d\n", node);
  1151. memset(&join_msg, 0, sizeof(join_msg));
  1152. join_msg.node_idx = dlm->node_num;
  1153. join_msg.name_len = strlen(dlm->name);
  1154. memcpy(join_msg.domain, dlm->name, join_msg.name_len);
  1155. join_msg.dlm_proto = dlm->dlm_locking_proto;
  1156. join_msg.fs_proto = dlm->fs_locking_proto;
  1157. /* copy live node map to join message */
  1158. byte_copymap(join_msg.node_map, dlm->live_nodes_map, O2NM_MAX_NODES);
  1159. status = o2net_send_message(DLM_QUERY_JOIN_MSG, DLM_MOD_KEY, &join_msg,
  1160. sizeof(join_msg), node, &join_resp);
  1161. if (status < 0 && status != -ENOPROTOOPT) {
  1162. mlog(ML_ERROR, "Error %d when sending message %u (key 0x%x) to "
  1163. "node %u\n", status, DLM_QUERY_JOIN_MSG, DLM_MOD_KEY,
  1164. node);
  1165. goto bail;
  1166. }
  1167. dlm_query_join_wire_to_packet(join_resp, &packet);
  1168. /* -ENOPROTOOPT from the net code means the other side isn't
  1169. listening for our message type -- that's fine, it means
  1170. his dlm isn't up, so we can consider him a 'yes' but not
  1171. joined into the domain. */
  1172. if (status == -ENOPROTOOPT) {
  1173. status = 0;
  1174. *response = JOIN_OK_NO_MAP;
  1175. } else if (packet.code == JOIN_DISALLOW ||
  1176. packet.code == JOIN_OK_NO_MAP) {
  1177. *response = packet.code;
  1178. } else if (packet.code == JOIN_PROTOCOL_MISMATCH) {
  1179. mlog(ML_NOTICE,
  1180. "This node requested DLM locking protocol %u.%u and "
  1181. "filesystem locking protocol %u.%u. At least one of "
  1182. "the protocol versions on node %d is not compatible, "
  1183. "disconnecting\n",
  1184. dlm->dlm_locking_proto.pv_major,
  1185. dlm->dlm_locking_proto.pv_minor,
  1186. dlm->fs_locking_proto.pv_major,
  1187. dlm->fs_locking_proto.pv_minor,
  1188. node);
  1189. status = -EPROTO;
  1190. *response = packet.code;
  1191. } else if (packet.code == JOIN_OK) {
  1192. *response = packet.code;
  1193. /* Use the same locking protocol as the remote node */
  1194. dlm->dlm_locking_proto.pv_minor = packet.dlm_minor;
  1195. dlm->fs_locking_proto.pv_minor = packet.fs_minor;
  1196. mlog(0,
  1197. "Node %d responds JOIN_OK with DLM locking protocol "
  1198. "%u.%u and fs locking protocol %u.%u\n",
  1199. node,
  1200. dlm->dlm_locking_proto.pv_major,
  1201. dlm->dlm_locking_proto.pv_minor,
  1202. dlm->fs_locking_proto.pv_major,
  1203. dlm->fs_locking_proto.pv_minor);
  1204. } else {
  1205. status = -EINVAL;
  1206. mlog(ML_ERROR, "invalid response %d from node %u\n",
  1207. packet.code, node);
  1208. }
  1209. mlog(0, "status %d, node %d response is %d\n", status, node,
  1210. *response);
  1211. bail:
  1212. return status;
  1213. }
  1214. static int dlm_send_one_join_assert(struct dlm_ctxt *dlm,
  1215. unsigned int node)
  1216. {
  1217. int status;
  1218. struct dlm_assert_joined assert_msg;
  1219. mlog(0, "Sending join assert to node %u\n", node);
  1220. memset(&assert_msg, 0, sizeof(assert_msg));
  1221. assert_msg.node_idx = dlm->node_num;
  1222. assert_msg.name_len = strlen(dlm->name);
  1223. memcpy(assert_msg.domain, dlm->name, assert_msg.name_len);
  1224. status = o2net_send_message(DLM_ASSERT_JOINED_MSG, DLM_MOD_KEY,
  1225. &assert_msg, sizeof(assert_msg), node,
  1226. NULL);
  1227. if (status < 0)
  1228. mlog(ML_ERROR, "Error %d when sending message %u (key 0x%x) to "
  1229. "node %u\n", status, DLM_ASSERT_JOINED_MSG, DLM_MOD_KEY,
  1230. node);
  1231. return status;
  1232. }
  1233. static void dlm_send_join_asserts(struct dlm_ctxt *dlm,
  1234. unsigned long *node_map)
  1235. {
  1236. int status, node, live;
  1237. status = 0;
  1238. node = -1;
  1239. while ((node = find_next_bit(node_map, O2NM_MAX_NODES,
  1240. node + 1)) < O2NM_MAX_NODES) {
  1241. if (node == dlm->node_num)
  1242. continue;
  1243. do {
  1244. /* It is very important that this message be
  1245. * received so we spin until either the node
  1246. * has died or it gets the message. */
  1247. status = dlm_send_one_join_assert(dlm, node);
  1248. spin_lock(&dlm->spinlock);
  1249. live = test_bit(node, dlm->live_nodes_map);
  1250. spin_unlock(&dlm->spinlock);
  1251. if (status) {
  1252. mlog(ML_ERROR, "Error return %d asserting "
  1253. "join on node %d\n", status, node);
  1254. /* give us some time between errors... */
  1255. if (live)
  1256. msleep(DLM_DOMAIN_BACKOFF_MS);
  1257. }
  1258. } while (status && live);
  1259. }
  1260. }
  1261. struct domain_join_ctxt {
  1262. unsigned long live_map[BITS_TO_LONGS(O2NM_MAX_NODES)];
  1263. unsigned long yes_resp_map[BITS_TO_LONGS(O2NM_MAX_NODES)];
  1264. };
  1265. static int dlm_should_restart_join(struct dlm_ctxt *dlm,
  1266. struct domain_join_ctxt *ctxt,
  1267. enum dlm_query_join_response_code response)
  1268. {
  1269. int ret;
  1270. if (response == JOIN_DISALLOW) {
  1271. mlog(0, "Latest response of disallow -- should restart\n");
  1272. return 1;
  1273. }
  1274. spin_lock(&dlm->spinlock);
  1275. /* For now, we restart the process if the node maps have
  1276. * changed at all */
  1277. ret = memcmp(ctxt->live_map, dlm->live_nodes_map,
  1278. sizeof(dlm->live_nodes_map));
  1279. spin_unlock(&dlm->spinlock);
  1280. if (ret)
  1281. mlog(0, "Node maps changed -- should restart\n");
  1282. return ret;
  1283. }
  1284. static int dlm_try_to_join_domain(struct dlm_ctxt *dlm)
  1285. {
  1286. int status = 0, tmpstat, node;
  1287. struct domain_join_ctxt *ctxt;
  1288. enum dlm_query_join_response_code response = JOIN_DISALLOW;
  1289. mlog_entry("%p", dlm);
  1290. ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
  1291. if (!ctxt) {
  1292. status = -ENOMEM;
  1293. mlog_errno(status);
  1294. goto bail;
  1295. }
  1296. /* group sem locking should work for us here -- we're already
  1297. * registered for heartbeat events so filling this should be
  1298. * atomic wrt getting those handlers called. */
  1299. o2hb_fill_node_map(dlm->live_nodes_map, sizeof(dlm->live_nodes_map));
  1300. spin_lock(&dlm->spinlock);
  1301. memcpy(ctxt->live_map, dlm->live_nodes_map, sizeof(ctxt->live_map));
  1302. __dlm_set_joining_node(dlm, dlm->node_num);
  1303. spin_unlock(&dlm->spinlock);
  1304. node = -1;
  1305. while ((node = find_next_bit(ctxt->live_map, O2NM_MAX_NODES,
  1306. node + 1)) < O2NM_MAX_NODES) {
  1307. if (node == dlm->node_num)
  1308. continue;
  1309. status = dlm_request_join(dlm, node, &response);
  1310. if (status < 0) {
  1311. mlog_errno(status);
  1312. goto bail;
  1313. }
  1314. /* Ok, either we got a response or the node doesn't have a
  1315. * dlm up. */
  1316. if (response == JOIN_OK)
  1317. set_bit(node, ctxt->yes_resp_map);
  1318. if (dlm_should_restart_join(dlm, ctxt, response)) {
  1319. status = -EAGAIN;
  1320. goto bail;
  1321. }
  1322. }
  1323. mlog(0, "Yay, done querying nodes!\n");
  1324. /* Yay, everyone agree's we can join the domain. My domain is
  1325. * comprised of all nodes who were put in the
  1326. * yes_resp_map. Copy that into our domain map and send a join
  1327. * assert message to clean up everyone elses state. */
  1328. spin_lock(&dlm->spinlock);
  1329. memcpy(dlm->domain_map, ctxt->yes_resp_map,
  1330. sizeof(ctxt->yes_resp_map));
  1331. set_bit(dlm->node_num, dlm->domain_map);
  1332. spin_unlock(&dlm->spinlock);
  1333. /* Support for global heartbeat and node info was added in 1.1 */
  1334. if (dlm_protocol.pv_major > 1 || dlm_protocol.pv_minor > 0) {
  1335. status = dlm_send_nodeinfo(dlm, ctxt->yes_resp_map);
  1336. if (status) {
  1337. mlog_errno(status);
  1338. goto bail;
  1339. }
  1340. status = dlm_send_regions(dlm, ctxt->yes_resp_map);
  1341. if (status) {
  1342. mlog_errno(status);
  1343. goto bail;
  1344. }
  1345. }
  1346. dlm_send_join_asserts(dlm, ctxt->yes_resp_map);
  1347. /* Joined state *must* be set before the joining node
  1348. * information, otherwise the query_join handler may read no
  1349. * current joiner but a state of NEW and tell joining nodes
  1350. * we're not in the domain. */
  1351. spin_lock(&dlm_domain_lock);
  1352. dlm->dlm_state = DLM_CTXT_JOINED;
  1353. dlm->num_joins++;
  1354. spin_unlock(&dlm_domain_lock);
  1355. bail:
  1356. spin_lock(&dlm->spinlock);
  1357. __dlm_set_joining_node(dlm, DLM_LOCK_RES_OWNER_UNKNOWN);
  1358. if (!status)
  1359. __dlm_print_nodes(dlm);
  1360. spin_unlock(&dlm->spinlock);
  1361. if (ctxt) {
  1362. /* Do we need to send a cancel message to any nodes? */
  1363. if (status < 0) {
  1364. tmpstat = dlm_send_join_cancels(dlm,
  1365. ctxt->yes_resp_map,
  1366. sizeof(ctxt->yes_resp_map));
  1367. if (tmpstat < 0)
  1368. mlog_errno(tmpstat);
  1369. }
  1370. kfree(ctxt);
  1371. }
  1372. mlog(0, "returning %d\n", status);
  1373. return status;
  1374. }
  1375. static void dlm_unregister_domain_handlers(struct dlm_ctxt *dlm)
  1376. {
  1377. o2hb_unregister_callback(NULL, &dlm->dlm_hb_up);
  1378. o2hb_unregister_callback(NULL, &dlm->dlm_hb_down);
  1379. o2net_unregister_handler_list(&dlm->dlm_domain_handlers);
  1380. }
  1381. static int dlm_register_domain_handlers(struct dlm_ctxt *dlm)
  1382. {
  1383. int status;
  1384. mlog(0, "registering handlers.\n");
  1385. o2hb_setup_callback(&dlm->dlm_hb_down, O2HB_NODE_DOWN_CB,
  1386. dlm_hb_node_down_cb, dlm, DLM_HB_NODE_DOWN_PRI);
  1387. status = o2hb_register_callback(NULL, &dlm->dlm_hb_down);
  1388. if (status)
  1389. goto bail;
  1390. o2hb_setup_callback(&dlm->dlm_hb_up, O2HB_NODE_UP_CB,
  1391. dlm_hb_node_up_cb, dlm, DLM_HB_NODE_UP_PRI);
  1392. status = o2hb_register_callback(NULL, &dlm->dlm_hb_up);
  1393. if (status)
  1394. goto bail;
  1395. status = o2net_register_handler(DLM_MASTER_REQUEST_MSG, dlm->key,
  1396. sizeof(struct dlm_master_request),
  1397. dlm_master_request_handler,
  1398. dlm, NULL, &dlm->dlm_domain_handlers);
  1399. if (status)
  1400. goto bail;
  1401. status = o2net_register_handler(DLM_ASSERT_MASTER_MSG, dlm->key,
  1402. sizeof(struct dlm_assert_master),
  1403. dlm_assert_master_handler,
  1404. dlm, dlm_assert_master_post_handler,
  1405. &dlm->dlm_domain_handlers);
  1406. if (status)
  1407. goto bail;
  1408. status = o2net_register_handler(DLM_CREATE_LOCK_MSG, dlm->key,
  1409. sizeof(struct dlm_create_lock),
  1410. dlm_create_lock_handler,
  1411. dlm, NULL, &dlm->dlm_domain_handlers);
  1412. if (status)
  1413. goto bail;
  1414. status = o2net_register_handler(DLM_CONVERT_LOCK_MSG, dlm->key,
  1415. DLM_CONVERT_LOCK_MAX_LEN,
  1416. dlm_convert_lock_handler,
  1417. dlm, NULL, &dlm->dlm_domain_handlers);
  1418. if (status)
  1419. goto bail;
  1420. status = o2net_register_handler(DLM_UNLOCK_LOCK_MSG, dlm->key,
  1421. DLM_UNLOCK_LOCK_MAX_LEN,
  1422. dlm_unlock_lock_handler,
  1423. dlm, NULL, &dlm->dlm_domain_handlers);
  1424. if (status)
  1425. goto bail;
  1426. status = o2net_register_handler(DLM_PROXY_AST_MSG, dlm->key,
  1427. DLM_PROXY_AST_MAX_LEN,
  1428. dlm_proxy_ast_handler,
  1429. dlm, NULL, &dlm->dlm_domain_handlers);
  1430. if (status)
  1431. goto bail;
  1432. status = o2net_register_handler(DLM_EXIT_DOMAIN_MSG, dlm->key,
  1433. sizeof(struct dlm_exit_domain),
  1434. dlm_exit_domain_handler,
  1435. dlm, NULL, &dlm->dlm_domain_handlers);
  1436. if (status)
  1437. goto bail;
  1438. status = o2net_register_handler(DLM_DEREF_LOCKRES_MSG, dlm->key,
  1439. sizeof(struct dlm_deref_lockres),
  1440. dlm_deref_lockres_handler,
  1441. dlm, NULL, &dlm->dlm_domain_handlers);
  1442. if (status)
  1443. goto bail;
  1444. status = o2net_register_handler(DLM_MIGRATE_REQUEST_MSG, dlm->key,
  1445. sizeof(struct dlm_migrate_request),
  1446. dlm_migrate_request_handler,
  1447. dlm, NULL, &dlm->dlm_domain_handlers);
  1448. if (status)
  1449. goto bail;
  1450. status = o2net_register_handler(DLM_MIG_LOCKRES_MSG, dlm->key,
  1451. DLM_MIG_LOCKRES_MAX_LEN,
  1452. dlm_mig_lockres_handler,
  1453. dlm, NULL, &dlm->dlm_domain_handlers);
  1454. if (status)
  1455. goto bail;
  1456. status = o2net_register_handler(DLM_MASTER_REQUERY_MSG, dlm->key,
  1457. sizeof(struct dlm_master_requery),
  1458. dlm_master_requery_handler,
  1459. dlm, NULL, &dlm->dlm_domain_handlers);
  1460. if (status)
  1461. goto bail;
  1462. status = o2net_register_handler(DLM_LOCK_REQUEST_MSG, dlm->key,
  1463. sizeof(struct dlm_lock_request),
  1464. dlm_request_all_locks_handler,
  1465. dlm, NULL, &dlm->dlm_domain_handlers);
  1466. if (status)
  1467. goto bail;
  1468. status = o2net_register_handler(DLM_RECO_DATA_DONE_MSG, dlm->key,
  1469. sizeof(struct dlm_reco_data_done),
  1470. dlm_reco_data_done_handler,
  1471. dlm, NULL, &dlm->dlm_domain_handlers);
  1472. if (status)
  1473. goto bail;
  1474. status = o2net_register_handler(DLM_BEGIN_RECO_MSG, dlm->key,
  1475. sizeof(struct dlm_begin_reco),
  1476. dlm_begin_reco_handler,
  1477. dlm, NULL, &dlm->dlm_domain_handlers);
  1478. if (status)
  1479. goto bail;
  1480. status = o2net_register_handler(DLM_FINALIZE_RECO_MSG, dlm->key,
  1481. sizeof(struct dlm_finalize_reco),
  1482. dlm_finalize_reco_handler,
  1483. dlm, NULL, &dlm->dlm_domain_handlers);
  1484. if (status)
  1485. goto bail;
  1486. bail:
  1487. if (status)
  1488. dlm_unregister_domain_handlers(dlm);
  1489. return status;
  1490. }
  1491. static int dlm_join_domain(struct dlm_ctxt *dlm)
  1492. {
  1493. int status;
  1494. unsigned int backoff;
  1495. unsigned int total_backoff = 0;
  1496. BUG_ON(!dlm);
  1497. mlog(0, "Join domain %s\n", dlm->name);
  1498. status = dlm_register_domain_handlers(dlm);
  1499. if (status) {
  1500. mlog_errno(status);
  1501. goto bail;
  1502. }
  1503. status = dlm_debug_init(dlm);
  1504. if (status < 0) {
  1505. mlog_errno(status);
  1506. goto bail;
  1507. }
  1508. status = dlm_launch_thread(dlm);
  1509. if (status < 0) {
  1510. mlog_errno(status);
  1511. goto bail;
  1512. }
  1513. status = dlm_launch_recovery_thread(dlm);
  1514. if (status < 0) {
  1515. mlog_errno(status);
  1516. goto bail;
  1517. }
  1518. dlm->dlm_worker = create_singlethread_workqueue("dlm_wq");
  1519. if (!dlm->dlm_worker) {
  1520. status = -ENOMEM;
  1521. mlog_errno(status);
  1522. goto bail;
  1523. }
  1524. do {
  1525. status = dlm_try_to_join_domain(dlm);
  1526. /* If we're racing another node to the join, then we
  1527. * need to back off temporarily and let them
  1528. * complete. */
  1529. #define DLM_JOIN_TIMEOUT_MSECS 90000
  1530. if (status == -EAGAIN) {
  1531. if (signal_pending(current)) {
  1532. status = -ERESTARTSYS;
  1533. goto bail;
  1534. }
  1535. if (total_backoff >
  1536. msecs_to_jiffies(DLM_JOIN_TIMEOUT_MSECS)) {
  1537. status = -ERESTARTSYS;
  1538. mlog(ML_NOTICE, "Timed out joining dlm domain "
  1539. "%s after %u msecs\n", dlm->name,
  1540. jiffies_to_msecs(total_backoff));
  1541. goto bail;
  1542. }
  1543. /*
  1544. * <chip> After you!
  1545. * <dale> No, after you!
  1546. * <chip> I insist!
  1547. * <dale> But you first!
  1548. * ...
  1549. */
  1550. backoff = (unsigned int)(jiffies & 0x3);
  1551. backoff *= DLM_DOMAIN_BACKOFF_MS;
  1552. total_backoff += backoff;
  1553. mlog(0, "backoff %d\n", backoff);
  1554. msleep(backoff);
  1555. }
  1556. } while (status == -EAGAIN);
  1557. if (status < 0) {
  1558. mlog_errno(status);
  1559. goto bail;
  1560. }
  1561. status = 0;
  1562. bail:
  1563. wake_up(&dlm_domain_events);
  1564. if (status) {
  1565. dlm_unregister_domain_handlers(dlm);
  1566. dlm_debug_shutdown(dlm);
  1567. dlm_complete_thread(dlm);
  1568. dlm_complete_recovery_thread(dlm);
  1569. dlm_destroy_dlm_worker(dlm);
  1570. }
  1571. return status;
  1572. }
  1573. static struct dlm_ctxt *dlm_alloc_ctxt(const char *domain,
  1574. u32 key)
  1575. {
  1576. int i;
  1577. int ret;
  1578. struct dlm_ctxt *dlm = NULL;
  1579. dlm = kzalloc(sizeof(*dlm), GFP_KERNEL);
  1580. if (!dlm) {
  1581. mlog_errno(-ENOMEM);
  1582. goto leave;
  1583. }
  1584. dlm->name = kstrdup(domain, GFP_KERNEL);
  1585. if (dlm->name == NULL) {
  1586. mlog_errno(-ENOMEM);
  1587. kfree(dlm);
  1588. dlm = NULL;
  1589. goto leave;
  1590. }
  1591. dlm->lockres_hash = (struct hlist_head **)dlm_alloc_pagevec(DLM_HASH_PAGES);
  1592. if (!dlm->lockres_hash) {
  1593. mlog_errno(-ENOMEM);
  1594. kfree(dlm->name);
  1595. kfree(dlm);
  1596. dlm = NULL;
  1597. goto leave;
  1598. }
  1599. for (i = 0; i < DLM_HASH_BUCKETS; i++)
  1600. INIT_HLIST_HEAD(dlm_lockres_hash(dlm, i));
  1601. dlm->master_hash = (struct hlist_head **)
  1602. dlm_alloc_pagevec(DLM_HASH_PAGES);
  1603. if (!dlm->master_hash) {
  1604. mlog_errno(-ENOMEM);
  1605. dlm_free_pagevec((void **)dlm->lockres_hash, DLM_HASH_PAGES);
  1606. kfree(dlm->name);
  1607. kfree(dlm);
  1608. dlm = NULL;
  1609. goto leave;
  1610. }
  1611. for (i = 0; i < DLM_HASH_BUCKETS; i++)
  1612. INIT_HLIST_HEAD(dlm_master_hash(dlm, i));
  1613. dlm->key = key;
  1614. dlm->node_num = o2nm_this_node();
  1615. ret = dlm_create_debugfs_subroot(dlm);
  1616. if (ret < 0) {
  1617. dlm_free_pagevec((void **)dlm->master_hash, DLM_HASH_PAGES);
  1618. dlm_free_pagevec((void **)dlm->lockres_hash, DLM_HASH_PAGES);
  1619. kfree(dlm->name);
  1620. kfree(dlm);
  1621. dlm = NULL;
  1622. goto leave;
  1623. }
  1624. spin_lock_init(&dlm->spinlock);
  1625. spin_lock_init(&dlm->master_lock);
  1626. spin_lock_init(&dlm->ast_lock);
  1627. spin_lock_init(&dlm->track_lock);
  1628. INIT_LIST_HEAD(&dlm->list);
  1629. INIT_LIST_HEAD(&dlm->dirty_list);
  1630. INIT_LIST_HEAD(&dlm->reco.resources);
  1631. INIT_LIST_HEAD(&dlm->reco.received);
  1632. INIT_LIST_HEAD(&dlm->reco.node_data);
  1633. INIT_LIST_HEAD(&dlm->purge_list);
  1634. INIT_LIST_HEAD(&dlm->dlm_domain_handlers);
  1635. INIT_LIST_HEAD(&dlm->tracking_list);
  1636. dlm->reco.state = 0;
  1637. INIT_LIST_HEAD(&dlm->pending_asts);
  1638. INIT_LIST_HEAD(&dlm->pending_basts);
  1639. mlog(0, "dlm->recovery_map=%p, &(dlm->recovery_map[0])=%p\n",
  1640. dlm->recovery_map, &(dlm->recovery_map[0]));
  1641. memset(dlm->recovery_map, 0, sizeof(dlm->recovery_map));
  1642. memset(dlm->live_nodes_map, 0, sizeof(dlm->live_nodes_map));
  1643. memset(dlm->domain_map, 0, sizeof(dlm->domain_map));
  1644. dlm->dlm_thread_task = NULL;
  1645. dlm->dlm_reco_thread_task = NULL;
  1646. dlm->dlm_worker = NULL;
  1647. init_waitqueue_head(&dlm->dlm_thread_wq);
  1648. init_waitqueue_head(&dlm->dlm_reco_thread_wq);
  1649. init_waitqueue_head(&dlm->reco.event);
  1650. init_waitqueue_head(&dlm->ast_wq);
  1651. init_waitqueue_head(&dlm->migration_wq);
  1652. INIT_LIST_HEAD(&dlm->mle_hb_events);
  1653. dlm->joining_node = DLM_LOCK_RES_OWNER_UNKNOWN;
  1654. init_waitqueue_head(&dlm->dlm_join_events);
  1655. dlm->reco.new_master = O2NM_INVALID_NODE_NUM;
  1656. dlm->reco.dead_node = O2NM_INVALID_NODE_NUM;
  1657. atomic_set(&dlm->res_tot_count, 0);
  1658. atomic_set(&dlm->res_cur_count, 0);
  1659. for (i = 0; i < DLM_MLE_NUM_TYPES; ++i) {
  1660. atomic_set(&dlm->mle_tot_count[i], 0);
  1661. atomic_set(&dlm->mle_cur_count[i], 0);
  1662. }
  1663. spin_lock_init(&dlm->work_lock);
  1664. INIT_LIST_HEAD(&dlm->work_list);
  1665. INIT_WORK(&dlm->dispatched_work, dlm_dispatch_work);
  1666. kref_init(&dlm->dlm_refs);
  1667. dlm->dlm_state = DLM_CTXT_NEW;
  1668. INIT_LIST_HEAD(&dlm->dlm_eviction_callbacks);
  1669. mlog(0, "context init: refcount %u\n",
  1670. atomic_read(&dlm->dlm_refs.refcount));
  1671. leave:
  1672. return dlm;
  1673. }
  1674. /*
  1675. * Compare a requested locking protocol version against the current one.
  1676. *
  1677. * If the major numbers are different, they are incompatible.
  1678. * If the current minor is greater than the request, they are incompatible.
  1679. * If the current minor is less than or equal to the request, they are
  1680. * compatible, and the requester should run at the current minor version.
  1681. */
  1682. static int dlm_protocol_compare(struct dlm_protocol_version *existing,
  1683. struct dlm_protocol_version *request)
  1684. {
  1685. if (existing->pv_major != request->pv_major)
  1686. return 1;
  1687. if (existing->pv_minor > request->pv_minor)
  1688. return 1;
  1689. if (existing->pv_minor < request->pv_minor)
  1690. request->pv_minor = existing->pv_minor;
  1691. return 0;
  1692. }
  1693. /*
  1694. * dlm_register_domain: one-time setup per "domain".
  1695. *
  1696. * The filesystem passes in the requested locking version via proto.
  1697. * If registration was successful, proto will contain the negotiated
  1698. * locking protocol.
  1699. */
  1700. struct dlm_ctxt * dlm_register_domain(const char *domain,
  1701. u32 key,
  1702. struct dlm_protocol_version *fs_proto)
  1703. {
  1704. int ret;
  1705. struct dlm_ctxt *dlm = NULL;
  1706. struct dlm_ctxt *new_ctxt = NULL;
  1707. if (strlen(domain) >= O2NM_MAX_NAME_LEN) {
  1708. ret = -ENAMETOOLONG;
  1709. mlog(ML_ERROR, "domain name length too long\n");
  1710. goto leave;
  1711. }
  1712. if (!o2hb_check_local_node_heartbeating()) {
  1713. mlog(ML_ERROR, "the local node has not been configured, or is "
  1714. "not heartbeating\n");
  1715. ret = -EPROTO;
  1716. goto leave;
  1717. }
  1718. mlog(0, "register called for domain \"%s\"\n", domain);
  1719. retry:
  1720. dlm = NULL;
  1721. if (signal_pending(current)) {
  1722. ret = -ERESTARTSYS;
  1723. mlog_errno(ret);
  1724. goto leave;
  1725. }
  1726. spin_lock(&dlm_domain_lock);
  1727. dlm = __dlm_lookup_domain(domain);
  1728. if (dlm) {
  1729. if (dlm->dlm_state != DLM_CTXT_JOINED) {
  1730. spin_unlock(&dlm_domain_lock);
  1731. mlog(0, "This ctxt is not joined yet!\n");
  1732. wait_event_interruptible(dlm_domain_events,
  1733. dlm_wait_on_domain_helper(
  1734. domain));
  1735. goto retry;
  1736. }
  1737. if (dlm_protocol_compare(&dlm->fs_locking_proto, fs_proto)) {
  1738. spin_unlock(&dlm_domain_lock);
  1739. mlog(ML_ERROR,
  1740. "Requested locking protocol version is not "
  1741. "compatible with already registered domain "
  1742. "\"%s\"\n", domain);
  1743. ret = -EPROTO;
  1744. goto leave;
  1745. }
  1746. __dlm_get(dlm);
  1747. dlm->num_joins++;
  1748. spin_unlock(&dlm_domain_lock);
  1749. ret = 0;
  1750. goto leave;
  1751. }
  1752. /* doesn't exist */
  1753. if (!new_ctxt) {
  1754. spin_unlock(&dlm_domain_lock);
  1755. new_ctxt = dlm_alloc_ctxt(domain, key);
  1756. if (new_ctxt)
  1757. goto retry;
  1758. ret = -ENOMEM;
  1759. mlog_errno(ret);
  1760. goto leave;
  1761. }
  1762. /* a little variable switch-a-roo here... */
  1763. dlm = new_ctxt;
  1764. new_ctxt = NULL;
  1765. /* add the new domain */
  1766. list_add_tail(&dlm->list, &dlm_domains);
  1767. spin_unlock(&dlm_domain_lock);
  1768. /*
  1769. * Pass the locking protocol version into the join. If the join
  1770. * succeeds, it will have the negotiated protocol set.
  1771. */
  1772. dlm->dlm_locking_proto = dlm_protocol;
  1773. dlm->fs_locking_proto = *fs_proto;
  1774. ret = dlm_join_domain(dlm);
  1775. if (ret) {
  1776. mlog_errno(ret);
  1777. dlm_put(dlm);
  1778. goto leave;
  1779. }
  1780. /* Tell the caller what locking protocol we negotiated */
  1781. *fs_proto = dlm->fs_locking_proto;
  1782. ret = 0;
  1783. leave:
  1784. if (new_ctxt)
  1785. dlm_free_ctxt_mem(new_ctxt);
  1786. if (ret < 0)
  1787. dlm = ERR_PTR(ret);
  1788. return dlm;
  1789. }
  1790. EXPORT_SYMBOL_GPL(dlm_register_domain);
  1791. static LIST_HEAD(dlm_join_handlers);
  1792. static void dlm_unregister_net_handlers(void)
  1793. {
  1794. o2net_unregister_handler_list(&dlm_join_handlers);
  1795. }
  1796. static int dlm_register_net_handlers(void)
  1797. {
  1798. int status = 0;
  1799. status = o2net_register_handler(DLM_QUERY_JOIN_MSG, DLM_MOD_KEY,
  1800. sizeof(struct dlm_query_join_request),
  1801. dlm_query_join_handler,
  1802. NULL, NULL, &dlm_join_handlers);
  1803. if (status)
  1804. goto bail;
  1805. status = o2net_register_handler(DLM_ASSERT_JOINED_MSG, DLM_MOD_KEY,
  1806. sizeof(struct dlm_assert_joined),
  1807. dlm_assert_joined_handler,
  1808. NULL, NULL, &dlm_join_handlers);
  1809. if (status)
  1810. goto bail;
  1811. status = o2net_register_handler(DLM_CANCEL_JOIN_MSG, DLM_MOD_KEY,
  1812. sizeof(struct dlm_cancel_join),
  1813. dlm_cancel_join_handler,
  1814. NULL, NULL, &dlm_join_handlers);
  1815. if (status)
  1816. goto bail;
  1817. status = o2net_register_handler(DLM_QUERY_REGION, DLM_MOD_KEY,
  1818. sizeof(struct dlm_query_region),
  1819. dlm_query_region_handler,
  1820. NULL, NULL, &dlm_join_handlers);
  1821. if (status)
  1822. goto bail;
  1823. status = o2net_register_handler(DLM_QUERY_NODEINFO, DLM_MOD_KEY,
  1824. sizeof(struct dlm_query_nodeinfo),
  1825. dlm_query_nodeinfo_handler,
  1826. NULL, NULL, &dlm_join_handlers);
  1827. bail:
  1828. if (status < 0)
  1829. dlm_unregister_net_handlers();
  1830. return status;
  1831. }
  1832. /* Domain eviction callback handling.
  1833. *
  1834. * The file system requires notification of node death *before* the
  1835. * dlm completes it's recovery work, otherwise it may be able to
  1836. * acquire locks on resources requiring recovery. Since the dlm can
  1837. * evict a node from it's domain *before* heartbeat fires, a similar
  1838. * mechanism is required. */
  1839. /* Eviction is not expected to happen often, so a per-domain lock is
  1840. * not necessary. Eviction callbacks are allowed to sleep for short
  1841. * periods of time. */
  1842. static DECLARE_RWSEM(dlm_callback_sem);
  1843. void dlm_fire_domain_eviction_callbacks(struct dlm_ctxt *dlm,
  1844. int node_num)
  1845. {
  1846. struct list_head *iter;
  1847. struct dlm_eviction_cb *cb;
  1848. down_read(&dlm_callback_sem);
  1849. list_for_each(iter, &dlm->dlm_eviction_callbacks) {
  1850. cb = list_entry(iter, struct dlm_eviction_cb, ec_item);
  1851. cb->ec_func(node_num, cb->ec_data);
  1852. }
  1853. up_read(&dlm_callback_sem);
  1854. }
  1855. void dlm_setup_eviction_cb(struct dlm_eviction_cb *cb,
  1856. dlm_eviction_func *f,
  1857. void *data)
  1858. {
  1859. INIT_LIST_HEAD(&cb->ec_item);
  1860. cb->ec_func = f;
  1861. cb->ec_data = data;
  1862. }
  1863. EXPORT_SYMBOL_GPL(dlm_setup_eviction_cb);
  1864. void dlm_register_eviction_cb(struct dlm_ctxt *dlm,
  1865. struct dlm_eviction_cb *cb)
  1866. {
  1867. down_write(&dlm_callback_sem);
  1868. list_add_tail(&cb->ec_item, &dlm->dlm_eviction_callbacks);
  1869. up_write(&dlm_callback_sem);
  1870. }
  1871. EXPORT_SYMBOL_GPL(dlm_register_eviction_cb);
  1872. void dlm_unregister_eviction_cb(struct dlm_eviction_cb *cb)
  1873. {
  1874. down_write(&dlm_callback_sem);
  1875. list_del_init(&cb->ec_item);
  1876. up_write(&dlm_callback_sem);
  1877. }
  1878. EXPORT_SYMBOL_GPL(dlm_unregister_eviction_cb);
  1879. static int __init dlm_init(void)
  1880. {
  1881. int status;
  1882. dlm_print_version();
  1883. status = dlm_init_mle_cache();
  1884. if (status) {
  1885. mlog(ML_ERROR, "Could not create o2dlm_mle slabcache\n");
  1886. goto error;
  1887. }
  1888. status = dlm_init_master_caches();
  1889. if (status) {
  1890. mlog(ML_ERROR, "Could not create o2dlm_lockres and "
  1891. "o2dlm_lockname slabcaches\n");
  1892. goto error;
  1893. }
  1894. status = dlm_init_lock_cache();
  1895. if (status) {
  1896. mlog(ML_ERROR, "Count not create o2dlm_lock slabcache\n");
  1897. goto error;
  1898. }
  1899. status = dlm_register_net_handlers();
  1900. if (status) {
  1901. mlog(ML_ERROR, "Unable to register network handlers\n");
  1902. goto error;
  1903. }
  1904. status = dlm_create_debugfs_root();
  1905. if (status)
  1906. goto error;
  1907. return 0;
  1908. error:
  1909. dlm_unregister_net_handlers();
  1910. dlm_destroy_lock_cache();
  1911. dlm_destroy_master_caches();
  1912. dlm_destroy_mle_cache();
  1913. return -1;
  1914. }
  1915. static void __exit dlm_exit (void)
  1916. {
  1917. dlm_destroy_debugfs_root();
  1918. dlm_unregister_net_handlers();
  1919. dlm_destroy_lock_cache();
  1920. dlm_destroy_master_caches();
  1921. dlm_destroy_mle_cache();
  1922. }
  1923. MODULE_AUTHOR("Oracle");
  1924. MODULE_LICENSE("GPL");
  1925. module_init(dlm_init);
  1926. module_exit(dlm_exit);