dlmdomain.c 60 KB

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