dlmdomain.c 60 KB

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