dlmdomain.c 61 KB

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