dlmmaster.c 81 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991
  1. /* -*- mode: c; c-basic-offset: 8; -*-
  2. * vim: noexpandtab sw=8 ts=8 sts=0:
  3. *
  4. * dlmmod.c
  5. *
  6. * standalone DLM module
  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/fs.h>
  28. #include <linux/types.h>
  29. #include <linux/slab.h>
  30. #include <linux/highmem.h>
  31. #include <linux/utsname.h>
  32. #include <linux/init.h>
  33. #include <linux/sysctl.h>
  34. #include <linux/random.h>
  35. #include <linux/blkdev.h>
  36. #include <linux/socket.h>
  37. #include <linux/inet.h>
  38. #include <linux/spinlock.h>
  39. #include <linux/delay.h>
  40. #include "cluster/heartbeat.h"
  41. #include "cluster/nodemanager.h"
  42. #include "cluster/tcp.h"
  43. #include "dlmapi.h"
  44. #include "dlmcommon.h"
  45. #include "dlmdebug.h"
  46. #include "dlmdomain.h"
  47. #define MLOG_MASK_PREFIX (ML_DLM|ML_DLM_MASTER)
  48. #include "cluster/masklog.h"
  49. enum dlm_mle_type {
  50. DLM_MLE_BLOCK,
  51. DLM_MLE_MASTER,
  52. DLM_MLE_MIGRATION
  53. };
  54. struct dlm_lock_name
  55. {
  56. u8 len;
  57. u8 name[DLM_LOCKID_NAME_MAX];
  58. };
  59. struct dlm_master_list_entry
  60. {
  61. struct list_head list;
  62. struct list_head hb_events;
  63. struct dlm_ctxt *dlm;
  64. spinlock_t spinlock;
  65. wait_queue_head_t wq;
  66. atomic_t woken;
  67. struct kref mle_refs;
  68. int inuse;
  69. unsigned long maybe_map[BITS_TO_LONGS(O2NM_MAX_NODES)];
  70. unsigned long vote_map[BITS_TO_LONGS(O2NM_MAX_NODES)];
  71. unsigned long response_map[BITS_TO_LONGS(O2NM_MAX_NODES)];
  72. unsigned long node_map[BITS_TO_LONGS(O2NM_MAX_NODES)];
  73. u8 master;
  74. u8 new_master;
  75. enum dlm_mle_type type;
  76. struct o2hb_callback_func mle_hb_up;
  77. struct o2hb_callback_func mle_hb_down;
  78. union {
  79. struct dlm_lock_resource *res;
  80. struct dlm_lock_name name;
  81. } u;
  82. };
  83. static void dlm_mle_node_down(struct dlm_ctxt *dlm,
  84. struct dlm_master_list_entry *mle,
  85. struct o2nm_node *node,
  86. int idx);
  87. static void dlm_mle_node_up(struct dlm_ctxt *dlm,
  88. struct dlm_master_list_entry *mle,
  89. struct o2nm_node *node,
  90. int idx);
  91. static void dlm_assert_master_worker(struct dlm_work_item *item, void *data);
  92. static int dlm_do_assert_master(struct dlm_ctxt *dlm, const char *lockname,
  93. unsigned int namelen, void *nodemap,
  94. u32 flags);
  95. static inline int dlm_mle_equal(struct dlm_ctxt *dlm,
  96. struct dlm_master_list_entry *mle,
  97. const char *name,
  98. unsigned int namelen)
  99. {
  100. struct dlm_lock_resource *res;
  101. if (dlm != mle->dlm)
  102. return 0;
  103. if (mle->type == DLM_MLE_BLOCK ||
  104. mle->type == DLM_MLE_MIGRATION) {
  105. if (namelen != mle->u.name.len ||
  106. memcmp(name, mle->u.name.name, namelen)!=0)
  107. return 0;
  108. } else {
  109. res = mle->u.res;
  110. if (namelen != res->lockname.len ||
  111. memcmp(res->lockname.name, name, namelen) != 0)
  112. return 0;
  113. }
  114. return 1;
  115. }
  116. #if 0
  117. /* Code here is included but defined out as it aids debugging */
  118. #define dlm_print_nodemap(m) _dlm_print_nodemap(m,#m)
  119. void _dlm_print_nodemap(unsigned long *map, const char *mapname)
  120. {
  121. int i;
  122. printk("%s=[ ", mapname);
  123. for (i=0; i<O2NM_MAX_NODES; i++)
  124. if (test_bit(i, map))
  125. printk("%d ", i);
  126. printk("]");
  127. }
  128. void dlm_print_one_mle(struct dlm_master_list_entry *mle)
  129. {
  130. int refs;
  131. char *type;
  132. char attached;
  133. u8 master;
  134. unsigned int namelen;
  135. const char *name;
  136. struct kref *k;
  137. unsigned long *maybe = mle->maybe_map,
  138. *vote = mle->vote_map,
  139. *resp = mle->response_map,
  140. *node = mle->node_map;
  141. k = &mle->mle_refs;
  142. if (mle->type == DLM_MLE_BLOCK)
  143. type = "BLK";
  144. else if (mle->type == DLM_MLE_MASTER)
  145. type = "MAS";
  146. else
  147. type = "MIG";
  148. refs = atomic_read(&k->refcount);
  149. master = mle->master;
  150. attached = (list_empty(&mle->hb_events) ? 'N' : 'Y');
  151. if (mle->type != DLM_MLE_MASTER) {
  152. namelen = mle->u.name.len;
  153. name = mle->u.name.name;
  154. } else {
  155. namelen = mle->u.res->lockname.len;
  156. name = mle->u.res->lockname.name;
  157. }
  158. mlog(ML_NOTICE, "%.*s: %3s refs=%3d mas=%3u new=%3u evt=%c inuse=%d ",
  159. namelen, name, type, refs, master, mle->new_master, attached,
  160. mle->inuse);
  161. dlm_print_nodemap(maybe);
  162. printk(", ");
  163. dlm_print_nodemap(vote);
  164. printk(", ");
  165. dlm_print_nodemap(resp);
  166. printk(", ");
  167. dlm_print_nodemap(node);
  168. printk(", ");
  169. printk("\n");
  170. }
  171. static void dlm_dump_mles(struct dlm_ctxt *dlm)
  172. {
  173. struct dlm_master_list_entry *mle;
  174. struct list_head *iter;
  175. mlog(ML_NOTICE, "dumping all mles for domain %s:\n", dlm->name);
  176. spin_lock(&dlm->master_lock);
  177. list_for_each(iter, &dlm->master_list) {
  178. mle = list_entry(iter, struct dlm_master_list_entry, list);
  179. dlm_print_one_mle(mle);
  180. }
  181. spin_unlock(&dlm->master_lock);
  182. }
  183. int dlm_dump_all_mles(const char __user *data, unsigned int len)
  184. {
  185. struct list_head *iter;
  186. struct dlm_ctxt *dlm;
  187. spin_lock(&dlm_domain_lock);
  188. list_for_each(iter, &dlm_domains) {
  189. dlm = list_entry (iter, struct dlm_ctxt, list);
  190. mlog(ML_NOTICE, "found dlm: %p, name=%s\n", dlm, dlm->name);
  191. dlm_dump_mles(dlm);
  192. }
  193. spin_unlock(&dlm_domain_lock);
  194. return len;
  195. }
  196. EXPORT_SYMBOL_GPL(dlm_dump_all_mles);
  197. #endif /* 0 */
  198. static kmem_cache_t *dlm_mle_cache = NULL;
  199. static void dlm_mle_release(struct kref *kref);
  200. static void dlm_init_mle(struct dlm_master_list_entry *mle,
  201. enum dlm_mle_type type,
  202. struct dlm_ctxt *dlm,
  203. struct dlm_lock_resource *res,
  204. const char *name,
  205. unsigned int namelen);
  206. static void dlm_put_mle(struct dlm_master_list_entry *mle);
  207. static void __dlm_put_mle(struct dlm_master_list_entry *mle);
  208. static int dlm_find_mle(struct dlm_ctxt *dlm,
  209. struct dlm_master_list_entry **mle,
  210. char *name, unsigned int namelen);
  211. static int dlm_do_master_request(struct dlm_master_list_entry *mle, int to);
  212. static int dlm_wait_for_lock_mastery(struct dlm_ctxt *dlm,
  213. struct dlm_lock_resource *res,
  214. struct dlm_master_list_entry *mle,
  215. int *blocked);
  216. static int dlm_restart_lock_mastery(struct dlm_ctxt *dlm,
  217. struct dlm_lock_resource *res,
  218. struct dlm_master_list_entry *mle,
  219. int blocked);
  220. static int dlm_add_migration_mle(struct dlm_ctxt *dlm,
  221. struct dlm_lock_resource *res,
  222. struct dlm_master_list_entry *mle,
  223. struct dlm_master_list_entry **oldmle,
  224. const char *name, unsigned int namelen,
  225. u8 new_master, u8 master);
  226. static u8 dlm_pick_migration_target(struct dlm_ctxt *dlm,
  227. struct dlm_lock_resource *res);
  228. static void dlm_remove_nonlocal_locks(struct dlm_ctxt *dlm,
  229. struct dlm_lock_resource *res);
  230. static int dlm_mark_lockres_migrating(struct dlm_ctxt *dlm,
  231. struct dlm_lock_resource *res,
  232. u8 target);
  233. static int dlm_pre_master_reco_lockres(struct dlm_ctxt *dlm,
  234. struct dlm_lock_resource *res);
  235. int dlm_is_host_down(int errno)
  236. {
  237. switch (errno) {
  238. case -EBADF:
  239. case -ECONNREFUSED:
  240. case -ENOTCONN:
  241. case -ECONNRESET:
  242. case -EPIPE:
  243. case -EHOSTDOWN:
  244. case -EHOSTUNREACH:
  245. case -ETIMEDOUT:
  246. case -ECONNABORTED:
  247. case -ENETDOWN:
  248. case -ENETUNREACH:
  249. case -ENETRESET:
  250. case -ESHUTDOWN:
  251. case -ENOPROTOOPT:
  252. case -EINVAL: /* if returned from our tcp code,
  253. this means there is no socket */
  254. return 1;
  255. }
  256. return 0;
  257. }
  258. /*
  259. * MASTER LIST FUNCTIONS
  260. */
  261. /*
  262. * regarding master list entries and heartbeat callbacks:
  263. *
  264. * in order to avoid sleeping and allocation that occurs in
  265. * heartbeat, master list entries are simply attached to the
  266. * dlm's established heartbeat callbacks. the mle is attached
  267. * when it is created, and since the dlm->spinlock is held at
  268. * that time, any heartbeat event will be properly discovered
  269. * by the mle. the mle needs to be detached from the
  270. * dlm->mle_hb_events list as soon as heartbeat events are no
  271. * longer useful to the mle, and before the mle is freed.
  272. *
  273. * as a general rule, heartbeat events are no longer needed by
  274. * the mle once an "answer" regarding the lock master has been
  275. * received.
  276. */
  277. static inline void __dlm_mle_attach_hb_events(struct dlm_ctxt *dlm,
  278. struct dlm_master_list_entry *mle)
  279. {
  280. assert_spin_locked(&dlm->spinlock);
  281. list_add_tail(&mle->hb_events, &dlm->mle_hb_events);
  282. }
  283. static inline void __dlm_mle_detach_hb_events(struct dlm_ctxt *dlm,
  284. struct dlm_master_list_entry *mle)
  285. {
  286. if (!list_empty(&mle->hb_events))
  287. list_del_init(&mle->hb_events);
  288. }
  289. static inline void dlm_mle_detach_hb_events(struct dlm_ctxt *dlm,
  290. struct dlm_master_list_entry *mle)
  291. {
  292. spin_lock(&dlm->spinlock);
  293. __dlm_mle_detach_hb_events(dlm, mle);
  294. spin_unlock(&dlm->spinlock);
  295. }
  296. static void dlm_get_mle_inuse(struct dlm_master_list_entry *mle)
  297. {
  298. struct dlm_ctxt *dlm;
  299. dlm = mle->dlm;
  300. assert_spin_locked(&dlm->spinlock);
  301. assert_spin_locked(&dlm->master_lock);
  302. mle->inuse++;
  303. kref_get(&mle->mle_refs);
  304. }
  305. static void dlm_put_mle_inuse(struct dlm_master_list_entry *mle)
  306. {
  307. struct dlm_ctxt *dlm;
  308. dlm = mle->dlm;
  309. spin_lock(&dlm->spinlock);
  310. spin_lock(&dlm->master_lock);
  311. mle->inuse--;
  312. __dlm_put_mle(mle);
  313. spin_unlock(&dlm->master_lock);
  314. spin_unlock(&dlm->spinlock);
  315. }
  316. /* remove from list and free */
  317. static void __dlm_put_mle(struct dlm_master_list_entry *mle)
  318. {
  319. struct dlm_ctxt *dlm;
  320. dlm = mle->dlm;
  321. assert_spin_locked(&dlm->spinlock);
  322. assert_spin_locked(&dlm->master_lock);
  323. if (!atomic_read(&mle->mle_refs.refcount)) {
  324. /* this may or may not crash, but who cares.
  325. * it's a BUG. */
  326. mlog(ML_ERROR, "bad mle: %p\n", mle);
  327. dlm_print_one_mle(mle);
  328. BUG();
  329. } else
  330. kref_put(&mle->mle_refs, dlm_mle_release);
  331. }
  332. /* must not have any spinlocks coming in */
  333. static void dlm_put_mle(struct dlm_master_list_entry *mle)
  334. {
  335. struct dlm_ctxt *dlm;
  336. dlm = mle->dlm;
  337. spin_lock(&dlm->spinlock);
  338. spin_lock(&dlm->master_lock);
  339. __dlm_put_mle(mle);
  340. spin_unlock(&dlm->master_lock);
  341. spin_unlock(&dlm->spinlock);
  342. }
  343. static inline void dlm_get_mle(struct dlm_master_list_entry *mle)
  344. {
  345. kref_get(&mle->mle_refs);
  346. }
  347. static void dlm_init_mle(struct dlm_master_list_entry *mle,
  348. enum dlm_mle_type type,
  349. struct dlm_ctxt *dlm,
  350. struct dlm_lock_resource *res,
  351. const char *name,
  352. unsigned int namelen)
  353. {
  354. assert_spin_locked(&dlm->spinlock);
  355. mle->dlm = dlm;
  356. mle->type = type;
  357. INIT_LIST_HEAD(&mle->list);
  358. INIT_LIST_HEAD(&mle->hb_events);
  359. memset(mle->maybe_map, 0, sizeof(mle->maybe_map));
  360. spin_lock_init(&mle->spinlock);
  361. init_waitqueue_head(&mle->wq);
  362. atomic_set(&mle->woken, 0);
  363. kref_init(&mle->mle_refs);
  364. memset(mle->response_map, 0, sizeof(mle->response_map));
  365. mle->master = O2NM_MAX_NODES;
  366. mle->new_master = O2NM_MAX_NODES;
  367. mle->inuse = 0;
  368. if (mle->type == DLM_MLE_MASTER) {
  369. BUG_ON(!res);
  370. mle->u.res = res;
  371. } else if (mle->type == DLM_MLE_BLOCK) {
  372. BUG_ON(!name);
  373. memcpy(mle->u.name.name, name, namelen);
  374. mle->u.name.len = namelen;
  375. } else /* DLM_MLE_MIGRATION */ {
  376. BUG_ON(!name);
  377. memcpy(mle->u.name.name, name, namelen);
  378. mle->u.name.len = namelen;
  379. }
  380. /* copy off the node_map and register hb callbacks on our copy */
  381. memcpy(mle->node_map, dlm->domain_map, sizeof(mle->node_map));
  382. memcpy(mle->vote_map, dlm->domain_map, sizeof(mle->vote_map));
  383. clear_bit(dlm->node_num, mle->vote_map);
  384. clear_bit(dlm->node_num, mle->node_map);
  385. /* attach the mle to the domain node up/down events */
  386. __dlm_mle_attach_hb_events(dlm, mle);
  387. }
  388. /* returns 1 if found, 0 if not */
  389. static int dlm_find_mle(struct dlm_ctxt *dlm,
  390. struct dlm_master_list_entry **mle,
  391. char *name, unsigned int namelen)
  392. {
  393. struct dlm_master_list_entry *tmpmle;
  394. struct list_head *iter;
  395. assert_spin_locked(&dlm->master_lock);
  396. list_for_each(iter, &dlm->master_list) {
  397. tmpmle = list_entry(iter, struct dlm_master_list_entry, list);
  398. if (!dlm_mle_equal(dlm, tmpmle, name, namelen))
  399. continue;
  400. dlm_get_mle(tmpmle);
  401. *mle = tmpmle;
  402. return 1;
  403. }
  404. return 0;
  405. }
  406. void dlm_hb_event_notify_attached(struct dlm_ctxt *dlm, int idx, int node_up)
  407. {
  408. struct dlm_master_list_entry *mle;
  409. struct list_head *iter;
  410. assert_spin_locked(&dlm->spinlock);
  411. list_for_each(iter, &dlm->mle_hb_events) {
  412. mle = list_entry(iter, struct dlm_master_list_entry,
  413. hb_events);
  414. if (node_up)
  415. dlm_mle_node_up(dlm, mle, NULL, idx);
  416. else
  417. dlm_mle_node_down(dlm, mle, NULL, idx);
  418. }
  419. }
  420. static void dlm_mle_node_down(struct dlm_ctxt *dlm,
  421. struct dlm_master_list_entry *mle,
  422. struct o2nm_node *node, int idx)
  423. {
  424. spin_lock(&mle->spinlock);
  425. if (!test_bit(idx, mle->node_map))
  426. mlog(0, "node %u already removed from nodemap!\n", idx);
  427. else
  428. clear_bit(idx, mle->node_map);
  429. spin_unlock(&mle->spinlock);
  430. }
  431. static void dlm_mle_node_up(struct dlm_ctxt *dlm,
  432. struct dlm_master_list_entry *mle,
  433. struct o2nm_node *node, int idx)
  434. {
  435. spin_lock(&mle->spinlock);
  436. if (test_bit(idx, mle->node_map))
  437. mlog(0, "node %u already in node map!\n", idx);
  438. else
  439. set_bit(idx, mle->node_map);
  440. spin_unlock(&mle->spinlock);
  441. }
  442. int dlm_init_mle_cache(void)
  443. {
  444. dlm_mle_cache = kmem_cache_create("dlm_mle_cache",
  445. sizeof(struct dlm_master_list_entry),
  446. 0, SLAB_HWCACHE_ALIGN,
  447. NULL, NULL);
  448. if (dlm_mle_cache == NULL)
  449. return -ENOMEM;
  450. return 0;
  451. }
  452. void dlm_destroy_mle_cache(void)
  453. {
  454. if (dlm_mle_cache)
  455. kmem_cache_destroy(dlm_mle_cache);
  456. }
  457. static void dlm_mle_release(struct kref *kref)
  458. {
  459. struct dlm_master_list_entry *mle;
  460. struct dlm_ctxt *dlm;
  461. mlog_entry_void();
  462. mle = container_of(kref, struct dlm_master_list_entry, mle_refs);
  463. dlm = mle->dlm;
  464. if (mle->type != DLM_MLE_MASTER) {
  465. mlog(0, "calling mle_release for %.*s, type %d\n",
  466. mle->u.name.len, mle->u.name.name, mle->type);
  467. } else {
  468. mlog(0, "calling mle_release for %.*s, type %d\n",
  469. mle->u.res->lockname.len,
  470. mle->u.res->lockname.name, mle->type);
  471. }
  472. assert_spin_locked(&dlm->spinlock);
  473. assert_spin_locked(&dlm->master_lock);
  474. /* remove from list if not already */
  475. if (!list_empty(&mle->list))
  476. list_del_init(&mle->list);
  477. /* detach the mle from the domain node up/down events */
  478. __dlm_mle_detach_hb_events(dlm, mle);
  479. /* NOTE: kfree under spinlock here.
  480. * if this is bad, we can move this to a freelist. */
  481. kmem_cache_free(dlm_mle_cache, mle);
  482. }
  483. /*
  484. * LOCK RESOURCE FUNCTIONS
  485. */
  486. static void dlm_set_lockres_owner(struct dlm_ctxt *dlm,
  487. struct dlm_lock_resource *res,
  488. u8 owner)
  489. {
  490. assert_spin_locked(&res->spinlock);
  491. mlog_entry("%.*s, %u\n", res->lockname.len, res->lockname.name, owner);
  492. if (owner == dlm->node_num)
  493. atomic_inc(&dlm->local_resources);
  494. else if (owner == DLM_LOCK_RES_OWNER_UNKNOWN)
  495. atomic_inc(&dlm->unknown_resources);
  496. else
  497. atomic_inc(&dlm->remote_resources);
  498. res->owner = owner;
  499. }
  500. void dlm_change_lockres_owner(struct dlm_ctxt *dlm,
  501. struct dlm_lock_resource *res, u8 owner)
  502. {
  503. assert_spin_locked(&res->spinlock);
  504. if (owner == res->owner)
  505. return;
  506. if (res->owner == dlm->node_num)
  507. atomic_dec(&dlm->local_resources);
  508. else if (res->owner == DLM_LOCK_RES_OWNER_UNKNOWN)
  509. atomic_dec(&dlm->unknown_resources);
  510. else
  511. atomic_dec(&dlm->remote_resources);
  512. dlm_set_lockres_owner(dlm, res, owner);
  513. }
  514. static void dlm_lockres_release(struct kref *kref)
  515. {
  516. struct dlm_lock_resource *res;
  517. res = container_of(kref, struct dlm_lock_resource, refs);
  518. /* This should not happen -- all lockres' have a name
  519. * associated with them at init time. */
  520. BUG_ON(!res->lockname.name);
  521. mlog(0, "destroying lockres %.*s\n", res->lockname.len,
  522. res->lockname.name);
  523. if (!hlist_unhashed(&res->hash_node) ||
  524. !list_empty(&res->granted) ||
  525. !list_empty(&res->converting) ||
  526. !list_empty(&res->blocked) ||
  527. !list_empty(&res->dirty) ||
  528. !list_empty(&res->recovering) ||
  529. !list_empty(&res->purge)) {
  530. mlog(ML_ERROR,
  531. "Going to BUG for resource %.*s."
  532. " We're on a list! [%c%c%c%c%c%c%c]\n",
  533. res->lockname.len, res->lockname.name,
  534. !hlist_unhashed(&res->hash_node) ? 'H' : ' ',
  535. !list_empty(&res->granted) ? 'G' : ' ',
  536. !list_empty(&res->converting) ? 'C' : ' ',
  537. !list_empty(&res->blocked) ? 'B' : ' ',
  538. !list_empty(&res->dirty) ? 'D' : ' ',
  539. !list_empty(&res->recovering) ? 'R' : ' ',
  540. !list_empty(&res->purge) ? 'P' : ' ');
  541. dlm_print_one_lock_resource(res);
  542. }
  543. /* By the time we're ready to blow this guy away, we shouldn't
  544. * be on any lists. */
  545. BUG_ON(!hlist_unhashed(&res->hash_node));
  546. BUG_ON(!list_empty(&res->granted));
  547. BUG_ON(!list_empty(&res->converting));
  548. BUG_ON(!list_empty(&res->blocked));
  549. BUG_ON(!list_empty(&res->dirty));
  550. BUG_ON(!list_empty(&res->recovering));
  551. BUG_ON(!list_empty(&res->purge));
  552. kfree(res->lockname.name);
  553. kfree(res);
  554. }
  555. void dlm_lockres_put(struct dlm_lock_resource *res)
  556. {
  557. kref_put(&res->refs, dlm_lockres_release);
  558. }
  559. static void dlm_init_lockres(struct dlm_ctxt *dlm,
  560. struct dlm_lock_resource *res,
  561. const char *name, unsigned int namelen)
  562. {
  563. char *qname;
  564. /* If we memset here, we lose our reference to the kmalloc'd
  565. * res->lockname.name, so be sure to init every field
  566. * correctly! */
  567. qname = (char *) res->lockname.name;
  568. memcpy(qname, name, namelen);
  569. res->lockname.len = namelen;
  570. res->lockname.hash = dlm_lockid_hash(name, namelen);
  571. init_waitqueue_head(&res->wq);
  572. spin_lock_init(&res->spinlock);
  573. INIT_HLIST_NODE(&res->hash_node);
  574. INIT_LIST_HEAD(&res->granted);
  575. INIT_LIST_HEAD(&res->converting);
  576. INIT_LIST_HEAD(&res->blocked);
  577. INIT_LIST_HEAD(&res->dirty);
  578. INIT_LIST_HEAD(&res->recovering);
  579. INIT_LIST_HEAD(&res->purge);
  580. atomic_set(&res->asts_reserved, 0);
  581. res->migration_pending = 0;
  582. kref_init(&res->refs);
  583. /* just for consistency */
  584. spin_lock(&res->spinlock);
  585. dlm_set_lockres_owner(dlm, res, DLM_LOCK_RES_OWNER_UNKNOWN);
  586. spin_unlock(&res->spinlock);
  587. res->state = DLM_LOCK_RES_IN_PROGRESS;
  588. res->last_used = 0;
  589. memset(res->lvb, 0, DLM_LVB_LEN);
  590. }
  591. struct dlm_lock_resource *dlm_new_lockres(struct dlm_ctxt *dlm,
  592. const char *name,
  593. unsigned int namelen)
  594. {
  595. struct dlm_lock_resource *res;
  596. res = kmalloc(sizeof(struct dlm_lock_resource), GFP_KERNEL);
  597. if (!res)
  598. return NULL;
  599. res->lockname.name = kmalloc(namelen, GFP_KERNEL);
  600. if (!res->lockname.name) {
  601. kfree(res);
  602. return NULL;
  603. }
  604. dlm_init_lockres(dlm, res, name, namelen);
  605. return res;
  606. }
  607. /*
  608. * lookup a lock resource by name.
  609. * may already exist in the hashtable.
  610. * lockid is null terminated
  611. *
  612. * if not, allocate enough for the lockres and for
  613. * the temporary structure used in doing the mastering.
  614. *
  615. * also, do a lookup in the dlm->master_list to see
  616. * if another node has begun mastering the same lock.
  617. * if so, there should be a block entry in there
  618. * for this name, and we should *not* attempt to master
  619. * the lock here. need to wait around for that node
  620. * to assert_master (or die).
  621. *
  622. */
  623. struct dlm_lock_resource * dlm_get_lock_resource(struct dlm_ctxt *dlm,
  624. const char *lockid,
  625. int flags)
  626. {
  627. struct dlm_lock_resource *tmpres=NULL, *res=NULL;
  628. struct dlm_master_list_entry *mle = NULL;
  629. struct dlm_master_list_entry *alloc_mle = NULL;
  630. int blocked = 0;
  631. int ret, nodenum;
  632. struct dlm_node_iter iter;
  633. unsigned int namelen, hash;
  634. int tries = 0;
  635. int bit, wait_on_recovery = 0;
  636. BUG_ON(!lockid);
  637. namelen = strlen(lockid);
  638. hash = dlm_lockid_hash(lockid, namelen);
  639. mlog(0, "get lockres %s (len %d)\n", lockid, namelen);
  640. lookup:
  641. spin_lock(&dlm->spinlock);
  642. tmpres = __dlm_lookup_lockres(dlm, lockid, namelen, hash);
  643. if (tmpres) {
  644. spin_unlock(&dlm->spinlock);
  645. mlog(0, "found in hash!\n");
  646. if (res)
  647. dlm_lockres_put(res);
  648. res = tmpres;
  649. goto leave;
  650. }
  651. if (!res) {
  652. spin_unlock(&dlm->spinlock);
  653. mlog(0, "allocating a new resource\n");
  654. /* nothing found and we need to allocate one. */
  655. alloc_mle = (struct dlm_master_list_entry *)
  656. kmem_cache_alloc(dlm_mle_cache, GFP_KERNEL);
  657. if (!alloc_mle)
  658. goto leave;
  659. res = dlm_new_lockres(dlm, lockid, namelen);
  660. if (!res)
  661. goto leave;
  662. goto lookup;
  663. }
  664. mlog(0, "no lockres found, allocated our own: %p\n", res);
  665. if (flags & LKM_LOCAL) {
  666. /* caller knows it's safe to assume it's not mastered elsewhere
  667. * DONE! return right away */
  668. spin_lock(&res->spinlock);
  669. dlm_change_lockres_owner(dlm, res, dlm->node_num);
  670. __dlm_insert_lockres(dlm, res);
  671. spin_unlock(&res->spinlock);
  672. spin_unlock(&dlm->spinlock);
  673. /* lockres still marked IN_PROGRESS */
  674. goto wake_waiters;
  675. }
  676. /* check master list to see if another node has started mastering it */
  677. spin_lock(&dlm->master_lock);
  678. /* if we found a block, wait for lock to be mastered by another node */
  679. blocked = dlm_find_mle(dlm, &mle, (char *)lockid, namelen);
  680. if (blocked) {
  681. if (mle->type == DLM_MLE_MASTER) {
  682. mlog(ML_ERROR, "master entry for nonexistent lock!\n");
  683. BUG();
  684. } else if (mle->type == DLM_MLE_MIGRATION) {
  685. /* migration is in progress! */
  686. /* the good news is that we now know the
  687. * "current" master (mle->master). */
  688. spin_unlock(&dlm->master_lock);
  689. assert_spin_locked(&dlm->spinlock);
  690. /* set the lockres owner and hash it */
  691. spin_lock(&res->spinlock);
  692. dlm_set_lockres_owner(dlm, res, mle->master);
  693. __dlm_insert_lockres(dlm, res);
  694. spin_unlock(&res->spinlock);
  695. spin_unlock(&dlm->spinlock);
  696. /* master is known, detach */
  697. dlm_mle_detach_hb_events(dlm, mle);
  698. dlm_put_mle(mle);
  699. mle = NULL;
  700. goto wake_waiters;
  701. }
  702. } else {
  703. /* go ahead and try to master lock on this node */
  704. mle = alloc_mle;
  705. /* make sure this does not get freed below */
  706. alloc_mle = NULL;
  707. dlm_init_mle(mle, DLM_MLE_MASTER, dlm, res, NULL, 0);
  708. set_bit(dlm->node_num, mle->maybe_map);
  709. list_add(&mle->list, &dlm->master_list);
  710. /* still holding the dlm spinlock, check the recovery map
  711. * to see if there are any nodes that still need to be
  712. * considered. these will not appear in the mle nodemap
  713. * but they might own this lockres. wait on them. */
  714. bit = find_next_bit(dlm->recovery_map, O2NM_MAX_NODES, 0);
  715. if (bit < O2NM_MAX_NODES) {
  716. mlog(ML_NOTICE, "%s:%.*s: at least one node (%d) to"
  717. "recover before lock mastery can begin\n",
  718. dlm->name, namelen, (char *)lockid, bit);
  719. wait_on_recovery = 1;
  720. }
  721. }
  722. /* at this point there is either a DLM_MLE_BLOCK or a
  723. * DLM_MLE_MASTER on the master list, so it's safe to add the
  724. * lockres to the hashtable. anyone who finds the lock will
  725. * still have to wait on the IN_PROGRESS. */
  726. /* finally add the lockres to its hash bucket */
  727. __dlm_insert_lockres(dlm, res);
  728. /* get an extra ref on the mle in case this is a BLOCK
  729. * if so, the creator of the BLOCK may try to put the last
  730. * ref at this time in the assert master handler, so we
  731. * need an extra one to keep from a bad ptr deref. */
  732. dlm_get_mle_inuse(mle);
  733. spin_unlock(&dlm->master_lock);
  734. spin_unlock(&dlm->spinlock);
  735. while (wait_on_recovery) {
  736. /* any cluster changes that occurred after dropping the
  737. * dlm spinlock would be detectable be a change on the mle,
  738. * so we only need to clear out the recovery map once. */
  739. if (dlm_is_recovery_lock(lockid, namelen)) {
  740. mlog(ML_NOTICE, "%s: recovery map is not empty, but "
  741. "must master $RECOVERY lock now\n", dlm->name);
  742. if (!dlm_pre_master_reco_lockres(dlm, res))
  743. wait_on_recovery = 0;
  744. else {
  745. mlog(0, "%s: waiting 500ms for heartbeat state "
  746. "change\n", dlm->name);
  747. msleep(500);
  748. }
  749. continue;
  750. }
  751. dlm_kick_recovery_thread(dlm);
  752. msleep(100);
  753. dlm_wait_for_recovery(dlm);
  754. spin_lock(&dlm->spinlock);
  755. bit = find_next_bit(dlm->recovery_map, O2NM_MAX_NODES, 0);
  756. if (bit < O2NM_MAX_NODES) {
  757. mlog(ML_NOTICE, "%s:%.*s: at least one node (%d) to"
  758. "recover before lock mastery can begin\n",
  759. dlm->name, namelen, (char *)lockid, bit);
  760. wait_on_recovery = 1;
  761. } else
  762. wait_on_recovery = 0;
  763. spin_unlock(&dlm->spinlock);
  764. }
  765. /* must wait for lock to be mastered elsewhere */
  766. if (blocked)
  767. goto wait;
  768. redo_request:
  769. ret = -EINVAL;
  770. dlm_node_iter_init(mle->vote_map, &iter);
  771. while ((nodenum = dlm_node_iter_next(&iter)) >= 0) {
  772. ret = dlm_do_master_request(mle, nodenum);
  773. if (ret < 0)
  774. mlog_errno(ret);
  775. if (mle->master != O2NM_MAX_NODES) {
  776. /* found a master ! */
  777. if (mle->master <= nodenum)
  778. break;
  779. /* if our master request has not reached the master
  780. * yet, keep going until it does. this is how the
  781. * master will know that asserts are needed back to
  782. * the lower nodes. */
  783. mlog(0, "%s:%.*s: requests only up to %u but master "
  784. "is %u, keep going\n", dlm->name, namelen,
  785. lockid, nodenum, mle->master);
  786. }
  787. }
  788. wait:
  789. /* keep going until the response map includes all nodes */
  790. ret = dlm_wait_for_lock_mastery(dlm, res, mle, &blocked);
  791. if (ret < 0) {
  792. mlog(0, "%s:%.*s: node map changed, redo the "
  793. "master request now, blocked=%d\n",
  794. dlm->name, res->lockname.len,
  795. res->lockname.name, blocked);
  796. if (++tries > 20) {
  797. mlog(ML_ERROR, "%s:%.*s: spinning on "
  798. "dlm_wait_for_lock_mastery, blocked=%d\n",
  799. dlm->name, res->lockname.len,
  800. res->lockname.name, blocked);
  801. dlm_print_one_lock_resource(res);
  802. /* dlm_print_one_mle(mle); */
  803. tries = 0;
  804. }
  805. goto redo_request;
  806. }
  807. mlog(0, "lockres mastered by %u\n", res->owner);
  808. /* make sure we never continue without this */
  809. BUG_ON(res->owner == O2NM_MAX_NODES);
  810. /* master is known, detach if not already detached */
  811. dlm_mle_detach_hb_events(dlm, mle);
  812. dlm_put_mle(mle);
  813. /* put the extra ref */
  814. dlm_put_mle_inuse(mle);
  815. wake_waiters:
  816. spin_lock(&res->spinlock);
  817. res->state &= ~DLM_LOCK_RES_IN_PROGRESS;
  818. spin_unlock(&res->spinlock);
  819. wake_up(&res->wq);
  820. leave:
  821. /* need to free the unused mle */
  822. if (alloc_mle)
  823. kmem_cache_free(dlm_mle_cache, alloc_mle);
  824. return res;
  825. }
  826. #define DLM_MASTERY_TIMEOUT_MS 5000
  827. static int dlm_wait_for_lock_mastery(struct dlm_ctxt *dlm,
  828. struct dlm_lock_resource *res,
  829. struct dlm_master_list_entry *mle,
  830. int *blocked)
  831. {
  832. u8 m;
  833. int ret, bit;
  834. int map_changed, voting_done;
  835. int assert, sleep;
  836. recheck:
  837. ret = 0;
  838. assert = 0;
  839. /* check if another node has already become the owner */
  840. spin_lock(&res->spinlock);
  841. if (res->owner != DLM_LOCK_RES_OWNER_UNKNOWN) {
  842. mlog(0, "%s:%.*s: owner is suddenly %u\n", dlm->name,
  843. res->lockname.len, res->lockname.name, res->owner);
  844. spin_unlock(&res->spinlock);
  845. /* this will cause the master to re-assert across
  846. * the whole cluster, freeing up mles */
  847. if (res->owner != dlm->node_num) {
  848. ret = dlm_do_master_request(mle, res->owner);
  849. if (ret < 0) {
  850. /* give recovery a chance to run */
  851. mlog(ML_ERROR, "link to %u went down?: %d\n", res->owner, ret);
  852. msleep(500);
  853. goto recheck;
  854. }
  855. }
  856. ret = 0;
  857. goto leave;
  858. }
  859. spin_unlock(&res->spinlock);
  860. spin_lock(&mle->spinlock);
  861. m = mle->master;
  862. map_changed = (memcmp(mle->vote_map, mle->node_map,
  863. sizeof(mle->vote_map)) != 0);
  864. voting_done = (memcmp(mle->vote_map, mle->response_map,
  865. sizeof(mle->vote_map)) == 0);
  866. /* restart if we hit any errors */
  867. if (map_changed) {
  868. int b;
  869. mlog(0, "%s: %.*s: node map changed, restarting\n",
  870. dlm->name, res->lockname.len, res->lockname.name);
  871. ret = dlm_restart_lock_mastery(dlm, res, mle, *blocked);
  872. b = (mle->type == DLM_MLE_BLOCK);
  873. if ((*blocked && !b) || (!*blocked && b)) {
  874. mlog(0, "%s:%.*s: status change: old=%d new=%d\n",
  875. dlm->name, res->lockname.len, res->lockname.name,
  876. *blocked, b);
  877. *blocked = b;
  878. }
  879. spin_unlock(&mle->spinlock);
  880. if (ret < 0) {
  881. mlog_errno(ret);
  882. goto leave;
  883. }
  884. mlog(0, "%s:%.*s: restart lock mastery succeeded, "
  885. "rechecking now\n", dlm->name, res->lockname.len,
  886. res->lockname.name);
  887. goto recheck;
  888. } else {
  889. if (!voting_done) {
  890. mlog(0, "map not changed and voting not done "
  891. "for %s:%.*s\n", dlm->name, res->lockname.len,
  892. res->lockname.name);
  893. }
  894. }
  895. if (m != O2NM_MAX_NODES) {
  896. /* another node has done an assert!
  897. * all done! */
  898. sleep = 0;
  899. } else {
  900. sleep = 1;
  901. /* have all nodes responded? */
  902. if (voting_done && !*blocked) {
  903. bit = find_next_bit(mle->maybe_map, O2NM_MAX_NODES, 0);
  904. if (dlm->node_num <= bit) {
  905. /* my node number is lowest.
  906. * now tell other nodes that I am
  907. * mastering this. */
  908. mle->master = dlm->node_num;
  909. assert = 1;
  910. sleep = 0;
  911. }
  912. /* if voting is done, but we have not received
  913. * an assert master yet, we must sleep */
  914. }
  915. }
  916. spin_unlock(&mle->spinlock);
  917. /* sleep if we haven't finished voting yet */
  918. if (sleep) {
  919. unsigned long timeo = msecs_to_jiffies(DLM_MASTERY_TIMEOUT_MS);
  920. /*
  921. if (atomic_read(&mle->mle_refs.refcount) < 2)
  922. mlog(ML_ERROR, "mle (%p) refs=%d, name=%.*s\n", mle,
  923. atomic_read(&mle->mle_refs.refcount),
  924. res->lockname.len, res->lockname.name);
  925. */
  926. atomic_set(&mle->woken, 0);
  927. (void)wait_event_timeout(mle->wq,
  928. (atomic_read(&mle->woken) == 1),
  929. timeo);
  930. if (res->owner == O2NM_MAX_NODES) {
  931. mlog(0, "waiting again\n");
  932. goto recheck;
  933. }
  934. mlog(0, "done waiting, master is %u\n", res->owner);
  935. ret = 0;
  936. goto leave;
  937. }
  938. ret = 0; /* done */
  939. if (assert) {
  940. m = dlm->node_num;
  941. mlog(0, "about to master %.*s here, this=%u\n",
  942. res->lockname.len, res->lockname.name, m);
  943. ret = dlm_do_assert_master(dlm, res->lockname.name,
  944. res->lockname.len, mle->vote_map, 0);
  945. if (ret) {
  946. /* This is a failure in the network path,
  947. * not in the response to the assert_master
  948. * (any nonzero response is a BUG on this node).
  949. * Most likely a socket just got disconnected
  950. * due to node death. */
  951. mlog_errno(ret);
  952. }
  953. /* no longer need to restart lock mastery.
  954. * all living nodes have been contacted. */
  955. ret = 0;
  956. }
  957. /* set the lockres owner */
  958. spin_lock(&res->spinlock);
  959. dlm_change_lockres_owner(dlm, res, m);
  960. spin_unlock(&res->spinlock);
  961. leave:
  962. return ret;
  963. }
  964. struct dlm_bitmap_diff_iter
  965. {
  966. int curnode;
  967. unsigned long *orig_bm;
  968. unsigned long *cur_bm;
  969. unsigned long diff_bm[BITS_TO_LONGS(O2NM_MAX_NODES)];
  970. };
  971. enum dlm_node_state_change
  972. {
  973. NODE_DOWN = -1,
  974. NODE_NO_CHANGE = 0,
  975. NODE_UP
  976. };
  977. static void dlm_bitmap_diff_iter_init(struct dlm_bitmap_diff_iter *iter,
  978. unsigned long *orig_bm,
  979. unsigned long *cur_bm)
  980. {
  981. unsigned long p1, p2;
  982. int i;
  983. iter->curnode = -1;
  984. iter->orig_bm = orig_bm;
  985. iter->cur_bm = cur_bm;
  986. for (i = 0; i < BITS_TO_LONGS(O2NM_MAX_NODES); i++) {
  987. p1 = *(iter->orig_bm + i);
  988. p2 = *(iter->cur_bm + i);
  989. iter->diff_bm[i] = (p1 & ~p2) | (p2 & ~p1);
  990. }
  991. }
  992. static int dlm_bitmap_diff_iter_next(struct dlm_bitmap_diff_iter *iter,
  993. enum dlm_node_state_change *state)
  994. {
  995. int bit;
  996. if (iter->curnode >= O2NM_MAX_NODES)
  997. return -ENOENT;
  998. bit = find_next_bit(iter->diff_bm, O2NM_MAX_NODES,
  999. iter->curnode+1);
  1000. if (bit >= O2NM_MAX_NODES) {
  1001. iter->curnode = O2NM_MAX_NODES;
  1002. return -ENOENT;
  1003. }
  1004. /* if it was there in the original then this node died */
  1005. if (test_bit(bit, iter->orig_bm))
  1006. *state = NODE_DOWN;
  1007. else
  1008. *state = NODE_UP;
  1009. iter->curnode = bit;
  1010. return bit;
  1011. }
  1012. static int dlm_restart_lock_mastery(struct dlm_ctxt *dlm,
  1013. struct dlm_lock_resource *res,
  1014. struct dlm_master_list_entry *mle,
  1015. int blocked)
  1016. {
  1017. struct dlm_bitmap_diff_iter bdi;
  1018. enum dlm_node_state_change sc;
  1019. int node;
  1020. int ret = 0;
  1021. mlog(0, "something happened such that the "
  1022. "master process may need to be restarted!\n");
  1023. assert_spin_locked(&mle->spinlock);
  1024. dlm_bitmap_diff_iter_init(&bdi, mle->vote_map, mle->node_map);
  1025. node = dlm_bitmap_diff_iter_next(&bdi, &sc);
  1026. while (node >= 0) {
  1027. if (sc == NODE_UP) {
  1028. /* a node came up. clear any old vote from
  1029. * the response map and set it in the vote map
  1030. * then restart the mastery. */
  1031. mlog(ML_NOTICE, "node %d up while restarting\n", node);
  1032. /* redo the master request, but only for the new node */
  1033. mlog(0, "sending request to new node\n");
  1034. clear_bit(node, mle->response_map);
  1035. set_bit(node, mle->vote_map);
  1036. } else {
  1037. mlog(ML_ERROR, "node down! %d\n", node);
  1038. /* if the node wasn't involved in mastery skip it,
  1039. * but clear it out from the maps so that it will
  1040. * not affect mastery of this lockres */
  1041. clear_bit(node, mle->response_map);
  1042. clear_bit(node, mle->vote_map);
  1043. if (!test_bit(node, mle->maybe_map))
  1044. goto next;
  1045. /* if we're already blocked on lock mastery, and the
  1046. * dead node wasn't the expected master, or there is
  1047. * another node in the maybe_map, keep waiting */
  1048. if (blocked) {
  1049. int lowest = find_next_bit(mle->maybe_map,
  1050. O2NM_MAX_NODES, 0);
  1051. /* act like it was never there */
  1052. clear_bit(node, mle->maybe_map);
  1053. if (node != lowest)
  1054. goto next;
  1055. mlog(ML_ERROR, "expected master %u died while "
  1056. "this node was blocked waiting on it!\n",
  1057. node);
  1058. lowest = find_next_bit(mle->maybe_map,
  1059. O2NM_MAX_NODES,
  1060. lowest+1);
  1061. if (lowest < O2NM_MAX_NODES) {
  1062. mlog(0, "still blocked. waiting "
  1063. "on %u now\n", lowest);
  1064. goto next;
  1065. }
  1066. /* mle is an MLE_BLOCK, but there is now
  1067. * nothing left to block on. we need to return
  1068. * all the way back out and try again with
  1069. * an MLE_MASTER. dlm_do_local_recovery_cleanup
  1070. * has already run, so the mle refcount is ok */
  1071. mlog(0, "no longer blocking. we can "
  1072. "try to master this here\n");
  1073. mle->type = DLM_MLE_MASTER;
  1074. memset(mle->maybe_map, 0,
  1075. sizeof(mle->maybe_map));
  1076. memset(mle->response_map, 0,
  1077. sizeof(mle->maybe_map));
  1078. memcpy(mle->vote_map, mle->node_map,
  1079. sizeof(mle->node_map));
  1080. mle->u.res = res;
  1081. set_bit(dlm->node_num, mle->maybe_map);
  1082. ret = -EAGAIN;
  1083. goto next;
  1084. }
  1085. clear_bit(node, mle->maybe_map);
  1086. if (node > dlm->node_num)
  1087. goto next;
  1088. mlog(0, "dead node in map!\n");
  1089. /* yuck. go back and re-contact all nodes
  1090. * in the vote_map, removing this node. */
  1091. memset(mle->response_map, 0,
  1092. sizeof(mle->response_map));
  1093. }
  1094. ret = -EAGAIN;
  1095. next:
  1096. node = dlm_bitmap_diff_iter_next(&bdi, &sc);
  1097. }
  1098. return ret;
  1099. }
  1100. /*
  1101. * DLM_MASTER_REQUEST_MSG
  1102. *
  1103. * returns: 0 on success,
  1104. * -errno on a network error
  1105. *
  1106. * on error, the caller should assume the target node is "dead"
  1107. *
  1108. */
  1109. static int dlm_do_master_request(struct dlm_master_list_entry *mle, int to)
  1110. {
  1111. struct dlm_ctxt *dlm = mle->dlm;
  1112. struct dlm_master_request request;
  1113. int ret, response=0, resend;
  1114. memset(&request, 0, sizeof(request));
  1115. request.node_idx = dlm->node_num;
  1116. BUG_ON(mle->type == DLM_MLE_MIGRATION);
  1117. if (mle->type != DLM_MLE_MASTER) {
  1118. request.namelen = mle->u.name.len;
  1119. memcpy(request.name, mle->u.name.name, request.namelen);
  1120. } else {
  1121. request.namelen = mle->u.res->lockname.len;
  1122. memcpy(request.name, mle->u.res->lockname.name,
  1123. request.namelen);
  1124. }
  1125. again:
  1126. ret = o2net_send_message(DLM_MASTER_REQUEST_MSG, dlm->key, &request,
  1127. sizeof(request), to, &response);
  1128. if (ret < 0) {
  1129. if (ret == -ESRCH) {
  1130. /* should never happen */
  1131. mlog(ML_ERROR, "TCP stack not ready!\n");
  1132. BUG();
  1133. } else if (ret == -EINVAL) {
  1134. mlog(ML_ERROR, "bad args passed to o2net!\n");
  1135. BUG();
  1136. } else if (ret == -ENOMEM) {
  1137. mlog(ML_ERROR, "out of memory while trying to send "
  1138. "network message! retrying\n");
  1139. /* this is totally crude */
  1140. msleep(50);
  1141. goto again;
  1142. } else if (!dlm_is_host_down(ret)) {
  1143. /* not a network error. bad. */
  1144. mlog_errno(ret);
  1145. mlog(ML_ERROR, "unhandled error!");
  1146. BUG();
  1147. }
  1148. /* all other errors should be network errors,
  1149. * and likely indicate node death */
  1150. mlog(ML_ERROR, "link to %d went down!\n", to);
  1151. goto out;
  1152. }
  1153. ret = 0;
  1154. resend = 0;
  1155. spin_lock(&mle->spinlock);
  1156. switch (response) {
  1157. case DLM_MASTER_RESP_YES:
  1158. set_bit(to, mle->response_map);
  1159. mlog(0, "node %u is the master, response=YES\n", to);
  1160. mle->master = to;
  1161. break;
  1162. case DLM_MASTER_RESP_NO:
  1163. mlog(0, "node %u not master, response=NO\n", to);
  1164. set_bit(to, mle->response_map);
  1165. break;
  1166. case DLM_MASTER_RESP_MAYBE:
  1167. mlog(0, "node %u not master, response=MAYBE\n", to);
  1168. set_bit(to, mle->response_map);
  1169. set_bit(to, mle->maybe_map);
  1170. break;
  1171. case DLM_MASTER_RESP_ERROR:
  1172. mlog(0, "node %u hit an error, resending\n", to);
  1173. resend = 1;
  1174. response = 0;
  1175. break;
  1176. default:
  1177. mlog(ML_ERROR, "bad response! %u\n", response);
  1178. BUG();
  1179. }
  1180. spin_unlock(&mle->spinlock);
  1181. if (resend) {
  1182. /* this is also totally crude */
  1183. msleep(50);
  1184. goto again;
  1185. }
  1186. out:
  1187. return ret;
  1188. }
  1189. /*
  1190. * locks that can be taken here:
  1191. * dlm->spinlock
  1192. * res->spinlock
  1193. * mle->spinlock
  1194. * dlm->master_list
  1195. *
  1196. * if possible, TRIM THIS DOWN!!!
  1197. */
  1198. int dlm_master_request_handler(struct o2net_msg *msg, u32 len, void *data)
  1199. {
  1200. u8 response = DLM_MASTER_RESP_MAYBE;
  1201. struct dlm_ctxt *dlm = data;
  1202. struct dlm_lock_resource *res = NULL;
  1203. struct dlm_master_request *request = (struct dlm_master_request *) msg->buf;
  1204. struct dlm_master_list_entry *mle = NULL, *tmpmle = NULL;
  1205. char *name;
  1206. unsigned int namelen, hash;
  1207. int found, ret;
  1208. int set_maybe;
  1209. int dispatch_assert = 0;
  1210. if (!dlm_grab(dlm))
  1211. return DLM_MASTER_RESP_NO;
  1212. if (!dlm_domain_fully_joined(dlm)) {
  1213. response = DLM_MASTER_RESP_NO;
  1214. goto send_response;
  1215. }
  1216. name = request->name;
  1217. namelen = request->namelen;
  1218. hash = dlm_lockid_hash(name, namelen);
  1219. if (namelen > DLM_LOCKID_NAME_MAX) {
  1220. response = DLM_IVBUFLEN;
  1221. goto send_response;
  1222. }
  1223. way_up_top:
  1224. spin_lock(&dlm->spinlock);
  1225. res = __dlm_lookup_lockres(dlm, name, namelen, hash);
  1226. if (res) {
  1227. spin_unlock(&dlm->spinlock);
  1228. /* take care of the easy cases up front */
  1229. spin_lock(&res->spinlock);
  1230. if (res->state & DLM_LOCK_RES_RECOVERING) {
  1231. spin_unlock(&res->spinlock);
  1232. mlog(0, "returning DLM_MASTER_RESP_ERROR since res is "
  1233. "being recovered\n");
  1234. response = DLM_MASTER_RESP_ERROR;
  1235. if (mle)
  1236. kmem_cache_free(dlm_mle_cache, mle);
  1237. goto send_response;
  1238. }
  1239. if (res->owner == dlm->node_num) {
  1240. spin_unlock(&res->spinlock);
  1241. // mlog(0, "this node is the master\n");
  1242. response = DLM_MASTER_RESP_YES;
  1243. if (mle)
  1244. kmem_cache_free(dlm_mle_cache, mle);
  1245. /* this node is the owner.
  1246. * there is some extra work that needs to
  1247. * happen now. the requesting node has
  1248. * caused all nodes up to this one to
  1249. * create mles. this node now needs to
  1250. * go back and clean those up. */
  1251. dispatch_assert = 1;
  1252. goto send_response;
  1253. } else if (res->owner != DLM_LOCK_RES_OWNER_UNKNOWN) {
  1254. spin_unlock(&res->spinlock);
  1255. // mlog(0, "node %u is the master\n", res->owner);
  1256. response = DLM_MASTER_RESP_NO;
  1257. if (mle)
  1258. kmem_cache_free(dlm_mle_cache, mle);
  1259. goto send_response;
  1260. }
  1261. /* ok, there is no owner. either this node is
  1262. * being blocked, or it is actively trying to
  1263. * master this lock. */
  1264. if (!(res->state & DLM_LOCK_RES_IN_PROGRESS)) {
  1265. mlog(ML_ERROR, "lock with no owner should be "
  1266. "in-progress!\n");
  1267. BUG();
  1268. }
  1269. // mlog(0, "lockres is in progress...\n");
  1270. spin_lock(&dlm->master_lock);
  1271. found = dlm_find_mle(dlm, &tmpmle, name, namelen);
  1272. if (!found) {
  1273. mlog(ML_ERROR, "no mle found for this lock!\n");
  1274. BUG();
  1275. }
  1276. set_maybe = 1;
  1277. spin_lock(&tmpmle->spinlock);
  1278. if (tmpmle->type == DLM_MLE_BLOCK) {
  1279. // mlog(0, "this node is waiting for "
  1280. // "lockres to be mastered\n");
  1281. response = DLM_MASTER_RESP_NO;
  1282. } else if (tmpmle->type == DLM_MLE_MIGRATION) {
  1283. mlog(0, "node %u is master, but trying to migrate to "
  1284. "node %u.\n", tmpmle->master, tmpmle->new_master);
  1285. if (tmpmle->master == dlm->node_num) {
  1286. response = DLM_MASTER_RESP_YES;
  1287. mlog(ML_ERROR, "no owner on lockres, but this "
  1288. "node is trying to migrate it to %u?!\n",
  1289. tmpmle->new_master);
  1290. BUG();
  1291. } else {
  1292. /* the real master can respond on its own */
  1293. response = DLM_MASTER_RESP_NO;
  1294. }
  1295. } else if (tmpmle->master != DLM_LOCK_RES_OWNER_UNKNOWN) {
  1296. set_maybe = 0;
  1297. if (tmpmle->master == dlm->node_num) {
  1298. response = DLM_MASTER_RESP_YES;
  1299. /* this node will be the owner.
  1300. * go back and clean the mles on any
  1301. * other nodes */
  1302. dispatch_assert = 1;
  1303. } else
  1304. response = DLM_MASTER_RESP_NO;
  1305. } else {
  1306. // mlog(0, "this node is attempting to "
  1307. // "master lockres\n");
  1308. response = DLM_MASTER_RESP_MAYBE;
  1309. }
  1310. if (set_maybe)
  1311. set_bit(request->node_idx, tmpmle->maybe_map);
  1312. spin_unlock(&tmpmle->spinlock);
  1313. spin_unlock(&dlm->master_lock);
  1314. spin_unlock(&res->spinlock);
  1315. /* keep the mle attached to heartbeat events */
  1316. dlm_put_mle(tmpmle);
  1317. if (mle)
  1318. kmem_cache_free(dlm_mle_cache, mle);
  1319. goto send_response;
  1320. }
  1321. /*
  1322. * lockres doesn't exist on this node
  1323. * if there is an MLE_BLOCK, return NO
  1324. * if there is an MLE_MASTER, return MAYBE
  1325. * otherwise, add an MLE_BLOCK, return NO
  1326. */
  1327. spin_lock(&dlm->master_lock);
  1328. found = dlm_find_mle(dlm, &tmpmle, name, namelen);
  1329. if (!found) {
  1330. /* this lockid has never been seen on this node yet */
  1331. // mlog(0, "no mle found\n");
  1332. if (!mle) {
  1333. spin_unlock(&dlm->master_lock);
  1334. spin_unlock(&dlm->spinlock);
  1335. mle = (struct dlm_master_list_entry *)
  1336. kmem_cache_alloc(dlm_mle_cache, GFP_KERNEL);
  1337. if (!mle) {
  1338. response = DLM_MASTER_RESP_ERROR;
  1339. mlog_errno(-ENOMEM);
  1340. goto send_response;
  1341. }
  1342. goto way_up_top;
  1343. }
  1344. // mlog(0, "this is second time thru, already allocated, "
  1345. // "add the block.\n");
  1346. dlm_init_mle(mle, DLM_MLE_BLOCK, dlm, NULL, name, namelen);
  1347. set_bit(request->node_idx, mle->maybe_map);
  1348. list_add(&mle->list, &dlm->master_list);
  1349. response = DLM_MASTER_RESP_NO;
  1350. } else {
  1351. // mlog(0, "mle was found\n");
  1352. set_maybe = 1;
  1353. spin_lock(&tmpmle->spinlock);
  1354. if (tmpmle->master == dlm->node_num) {
  1355. mlog(ML_ERROR, "no lockres, but an mle with this node as master!\n");
  1356. BUG();
  1357. }
  1358. if (tmpmle->type == DLM_MLE_BLOCK)
  1359. response = DLM_MASTER_RESP_NO;
  1360. else if (tmpmle->type == DLM_MLE_MIGRATION) {
  1361. mlog(0, "migration mle was found (%u->%u)\n",
  1362. tmpmle->master, tmpmle->new_master);
  1363. /* real master can respond on its own */
  1364. response = DLM_MASTER_RESP_NO;
  1365. } else
  1366. response = DLM_MASTER_RESP_MAYBE;
  1367. if (set_maybe)
  1368. set_bit(request->node_idx, tmpmle->maybe_map);
  1369. spin_unlock(&tmpmle->spinlock);
  1370. }
  1371. spin_unlock(&dlm->master_lock);
  1372. spin_unlock(&dlm->spinlock);
  1373. if (found) {
  1374. /* keep the mle attached to heartbeat events */
  1375. dlm_put_mle(tmpmle);
  1376. }
  1377. send_response:
  1378. if (dispatch_assert) {
  1379. if (response != DLM_MASTER_RESP_YES)
  1380. mlog(ML_ERROR, "invalid response %d\n", response);
  1381. if (!res) {
  1382. mlog(ML_ERROR, "bad lockres while trying to assert!\n");
  1383. BUG();
  1384. }
  1385. mlog(0, "%u is the owner of %.*s, cleaning everyone else\n",
  1386. dlm->node_num, res->lockname.len, res->lockname.name);
  1387. ret = dlm_dispatch_assert_master(dlm, res, 0, request->node_idx,
  1388. DLM_ASSERT_MASTER_MLE_CLEANUP);
  1389. if (ret < 0) {
  1390. mlog(ML_ERROR, "failed to dispatch assert master work\n");
  1391. response = DLM_MASTER_RESP_ERROR;
  1392. }
  1393. }
  1394. dlm_put(dlm);
  1395. return response;
  1396. }
  1397. /*
  1398. * DLM_ASSERT_MASTER_MSG
  1399. */
  1400. /*
  1401. * NOTE: this can be used for debugging
  1402. * can periodically run all locks owned by this node
  1403. * and re-assert across the cluster...
  1404. */
  1405. static int dlm_do_assert_master(struct dlm_ctxt *dlm, const char *lockname,
  1406. unsigned int namelen, void *nodemap,
  1407. u32 flags)
  1408. {
  1409. struct dlm_assert_master assert;
  1410. int to, tmpret;
  1411. struct dlm_node_iter iter;
  1412. int ret = 0;
  1413. int reassert;
  1414. BUG_ON(namelen > O2NM_MAX_NAME_LEN);
  1415. again:
  1416. reassert = 0;
  1417. /* note that if this nodemap is empty, it returns 0 */
  1418. dlm_node_iter_init(nodemap, &iter);
  1419. while ((to = dlm_node_iter_next(&iter)) >= 0) {
  1420. int r = 0;
  1421. struct dlm_master_list_entry *mle = NULL;
  1422. mlog(0, "sending assert master to %d (%.*s)\n", to,
  1423. namelen, lockname);
  1424. memset(&assert, 0, sizeof(assert));
  1425. assert.node_idx = dlm->node_num;
  1426. assert.namelen = namelen;
  1427. memcpy(assert.name, lockname, namelen);
  1428. assert.flags = cpu_to_be32(flags);
  1429. tmpret = o2net_send_message(DLM_ASSERT_MASTER_MSG, dlm->key,
  1430. &assert, sizeof(assert), to, &r);
  1431. if (tmpret < 0) {
  1432. mlog(ML_ERROR, "assert_master returned %d!\n", tmpret);
  1433. if (!dlm_is_host_down(tmpret)) {
  1434. mlog(ML_ERROR, "unhandled error!\n");
  1435. BUG();
  1436. }
  1437. /* a node died. finish out the rest of the nodes. */
  1438. mlog(ML_ERROR, "link to %d went down!\n", to);
  1439. /* any nonzero status return will do */
  1440. ret = tmpret;
  1441. } else if (r < 0) {
  1442. /* ok, something horribly messed. kill thyself. */
  1443. mlog(ML_ERROR,"during assert master of %.*s to %u, "
  1444. "got %d.\n", namelen, lockname, to, r);
  1445. spin_lock(&dlm->spinlock);
  1446. spin_lock(&dlm->master_lock);
  1447. if (dlm_find_mle(dlm, &mle, (char *)lockname,
  1448. namelen)) {
  1449. dlm_print_one_mle(mle);
  1450. __dlm_put_mle(mle);
  1451. }
  1452. spin_unlock(&dlm->master_lock);
  1453. spin_unlock(&dlm->spinlock);
  1454. BUG();
  1455. } else if (r == EAGAIN) {
  1456. mlog(0, "%.*s: node %u create mles on other "
  1457. "nodes and requests a re-assert\n",
  1458. namelen, lockname, to);
  1459. reassert = 1;
  1460. }
  1461. }
  1462. if (reassert)
  1463. goto again;
  1464. return ret;
  1465. }
  1466. /*
  1467. * locks that can be taken here:
  1468. * dlm->spinlock
  1469. * res->spinlock
  1470. * mle->spinlock
  1471. * dlm->master_list
  1472. *
  1473. * if possible, TRIM THIS DOWN!!!
  1474. */
  1475. int dlm_assert_master_handler(struct o2net_msg *msg, u32 len, void *data)
  1476. {
  1477. struct dlm_ctxt *dlm = data;
  1478. struct dlm_master_list_entry *mle = NULL;
  1479. struct dlm_assert_master *assert = (struct dlm_assert_master *)msg->buf;
  1480. struct dlm_lock_resource *res = NULL;
  1481. char *name;
  1482. unsigned int namelen, hash;
  1483. u32 flags;
  1484. int master_request = 0;
  1485. int ret = 0;
  1486. if (!dlm_grab(dlm))
  1487. return 0;
  1488. name = assert->name;
  1489. namelen = assert->namelen;
  1490. hash = dlm_lockid_hash(name, namelen);
  1491. flags = be32_to_cpu(assert->flags);
  1492. if (namelen > DLM_LOCKID_NAME_MAX) {
  1493. mlog(ML_ERROR, "Invalid name length!");
  1494. goto done;
  1495. }
  1496. spin_lock(&dlm->spinlock);
  1497. if (flags)
  1498. mlog(0, "assert_master with flags: %u\n", flags);
  1499. /* find the MLE */
  1500. spin_lock(&dlm->master_lock);
  1501. if (!dlm_find_mle(dlm, &mle, name, namelen)) {
  1502. /* not an error, could be master just re-asserting */
  1503. mlog(0, "just got an assert_master from %u, but no "
  1504. "MLE for it! (%.*s)\n", assert->node_idx,
  1505. namelen, name);
  1506. } else {
  1507. int bit = find_next_bit (mle->maybe_map, O2NM_MAX_NODES, 0);
  1508. if (bit >= O2NM_MAX_NODES) {
  1509. /* not necessarily an error, though less likely.
  1510. * could be master just re-asserting. */
  1511. mlog(0, "no bits set in the maybe_map, but %u "
  1512. "is asserting! (%.*s)\n", assert->node_idx,
  1513. namelen, name);
  1514. } else if (bit != assert->node_idx) {
  1515. if (flags & DLM_ASSERT_MASTER_MLE_CLEANUP) {
  1516. mlog(0, "master %u was found, %u should "
  1517. "back off\n", assert->node_idx, bit);
  1518. } else {
  1519. /* with the fix for bug 569, a higher node
  1520. * number winning the mastery will respond
  1521. * YES to mastery requests, but this node
  1522. * had no way of knowing. let it pass. */
  1523. mlog(0, "%u is the lowest node, "
  1524. "%u is asserting. (%.*s) %u must "
  1525. "have begun after %u won.\n", bit,
  1526. assert->node_idx, namelen, name, bit,
  1527. assert->node_idx);
  1528. }
  1529. }
  1530. if (mle->type == DLM_MLE_MIGRATION) {
  1531. if (flags & DLM_ASSERT_MASTER_MLE_CLEANUP) {
  1532. mlog(0, "%s:%.*s: got cleanup assert"
  1533. " from %u for migration\n",
  1534. dlm->name, namelen, name,
  1535. assert->node_idx);
  1536. } else if (!(flags & DLM_ASSERT_MASTER_FINISH_MIGRATION)) {
  1537. mlog(0, "%s:%.*s: got unrelated assert"
  1538. " from %u for migration, ignoring\n",
  1539. dlm->name, namelen, name,
  1540. assert->node_idx);
  1541. __dlm_put_mle(mle);
  1542. spin_unlock(&dlm->master_lock);
  1543. spin_unlock(&dlm->spinlock);
  1544. goto done;
  1545. }
  1546. }
  1547. }
  1548. spin_unlock(&dlm->master_lock);
  1549. /* ok everything checks out with the MLE
  1550. * now check to see if there is a lockres */
  1551. res = __dlm_lookup_lockres(dlm, name, namelen, hash);
  1552. if (res) {
  1553. spin_lock(&res->spinlock);
  1554. if (res->state & DLM_LOCK_RES_RECOVERING) {
  1555. mlog(ML_ERROR, "%u asserting but %.*s is "
  1556. "RECOVERING!\n", assert->node_idx, namelen, name);
  1557. goto kill;
  1558. }
  1559. if (!mle) {
  1560. if (res->owner != DLM_LOCK_RES_OWNER_UNKNOWN &&
  1561. res->owner != assert->node_idx) {
  1562. mlog(ML_ERROR, "assert_master from "
  1563. "%u, but current owner is "
  1564. "%u! (%.*s)\n",
  1565. assert->node_idx, res->owner,
  1566. namelen, name);
  1567. goto kill;
  1568. }
  1569. } else if (mle->type != DLM_MLE_MIGRATION) {
  1570. if (res->owner != DLM_LOCK_RES_OWNER_UNKNOWN) {
  1571. /* owner is just re-asserting */
  1572. if (res->owner == assert->node_idx) {
  1573. mlog(0, "owner %u re-asserting on "
  1574. "lock %.*s\n", assert->node_idx,
  1575. namelen, name);
  1576. goto ok;
  1577. }
  1578. mlog(ML_ERROR, "got assert_master from "
  1579. "node %u, but %u is the owner! "
  1580. "(%.*s)\n", assert->node_idx,
  1581. res->owner, namelen, name);
  1582. goto kill;
  1583. }
  1584. if (!(res->state & DLM_LOCK_RES_IN_PROGRESS)) {
  1585. mlog(ML_ERROR, "got assert from %u, but lock "
  1586. "with no owner should be "
  1587. "in-progress! (%.*s)\n",
  1588. assert->node_idx,
  1589. namelen, name);
  1590. goto kill;
  1591. }
  1592. } else /* mle->type == DLM_MLE_MIGRATION */ {
  1593. /* should only be getting an assert from new master */
  1594. if (assert->node_idx != mle->new_master) {
  1595. mlog(ML_ERROR, "got assert from %u, but "
  1596. "new master is %u, and old master "
  1597. "was %u (%.*s)\n",
  1598. assert->node_idx, mle->new_master,
  1599. mle->master, namelen, name);
  1600. goto kill;
  1601. }
  1602. }
  1603. ok:
  1604. spin_unlock(&res->spinlock);
  1605. }
  1606. spin_unlock(&dlm->spinlock);
  1607. // mlog(0, "woo! got an assert_master from node %u!\n",
  1608. // assert->node_idx);
  1609. if (mle) {
  1610. int extra_ref = 0;
  1611. int nn = -1;
  1612. int rr, err = 0;
  1613. spin_lock(&mle->spinlock);
  1614. if (mle->type == DLM_MLE_BLOCK || mle->type == DLM_MLE_MIGRATION)
  1615. extra_ref = 1;
  1616. else {
  1617. /* MASTER mle: if any bits set in the response map
  1618. * then the calling node needs to re-assert to clear
  1619. * up nodes that this node contacted */
  1620. while ((nn = find_next_bit (mle->response_map, O2NM_MAX_NODES,
  1621. nn+1)) < O2NM_MAX_NODES) {
  1622. if (nn != dlm->node_num && nn != assert->node_idx)
  1623. master_request = 1;
  1624. }
  1625. }
  1626. mle->master = assert->node_idx;
  1627. atomic_set(&mle->woken, 1);
  1628. wake_up(&mle->wq);
  1629. spin_unlock(&mle->spinlock);
  1630. if (res) {
  1631. spin_lock(&res->spinlock);
  1632. if (mle->type == DLM_MLE_MIGRATION) {
  1633. mlog(0, "finishing off migration of lockres %.*s, "
  1634. "from %u to %u\n",
  1635. res->lockname.len, res->lockname.name,
  1636. dlm->node_num, mle->new_master);
  1637. res->state &= ~DLM_LOCK_RES_MIGRATING;
  1638. dlm_change_lockres_owner(dlm, res, mle->new_master);
  1639. BUG_ON(res->state & DLM_LOCK_RES_DIRTY);
  1640. } else {
  1641. dlm_change_lockres_owner(dlm, res, mle->master);
  1642. }
  1643. spin_unlock(&res->spinlock);
  1644. }
  1645. /* master is known, detach if not already detached.
  1646. * ensures that only one assert_master call will happen
  1647. * on this mle. */
  1648. spin_lock(&dlm->spinlock);
  1649. spin_lock(&dlm->master_lock);
  1650. rr = atomic_read(&mle->mle_refs.refcount);
  1651. if (mle->inuse > 0) {
  1652. if (extra_ref && rr < 3)
  1653. err = 1;
  1654. else if (!extra_ref && rr < 2)
  1655. err = 1;
  1656. } else {
  1657. if (extra_ref && rr < 2)
  1658. err = 1;
  1659. else if (!extra_ref && rr < 1)
  1660. err = 1;
  1661. }
  1662. if (err) {
  1663. mlog(ML_ERROR, "%s:%.*s: got assert master from %u "
  1664. "that will mess up this node, refs=%d, extra=%d, "
  1665. "inuse=%d\n", dlm->name, namelen, name,
  1666. assert->node_idx, rr, extra_ref, mle->inuse);
  1667. dlm_print_one_mle(mle);
  1668. }
  1669. list_del_init(&mle->list);
  1670. __dlm_mle_detach_hb_events(dlm, mle);
  1671. __dlm_put_mle(mle);
  1672. if (extra_ref) {
  1673. /* the assert master message now balances the extra
  1674. * ref given by the master / migration request message.
  1675. * if this is the last put, it will be removed
  1676. * from the list. */
  1677. __dlm_put_mle(mle);
  1678. }
  1679. spin_unlock(&dlm->master_lock);
  1680. spin_unlock(&dlm->spinlock);
  1681. } else if (res) {
  1682. if (res->owner != assert->node_idx) {
  1683. mlog(0, "assert_master from %u, but current "
  1684. "owner is %u (%.*s), no mle\n", assert->node_idx,
  1685. res->owner, namelen, name);
  1686. }
  1687. }
  1688. done:
  1689. ret = 0;
  1690. if (res)
  1691. dlm_lockres_put(res);
  1692. dlm_put(dlm);
  1693. if (master_request) {
  1694. mlog(0, "need to tell master to reassert\n");
  1695. ret = EAGAIN; // positive. negative would shoot down the node.
  1696. }
  1697. return ret;
  1698. kill:
  1699. /* kill the caller! */
  1700. mlog(ML_ERROR, "Bad message received from another node. Dumping state "
  1701. "and killing the other node now! This node is OK and can continue.\n");
  1702. __dlm_print_one_lock_resource(res);
  1703. spin_unlock(&res->spinlock);
  1704. spin_unlock(&dlm->spinlock);
  1705. dlm_lockres_put(res);
  1706. dlm_put(dlm);
  1707. return -EINVAL;
  1708. }
  1709. int dlm_dispatch_assert_master(struct dlm_ctxt *dlm,
  1710. struct dlm_lock_resource *res,
  1711. int ignore_higher, u8 request_from, u32 flags)
  1712. {
  1713. struct dlm_work_item *item;
  1714. item = kcalloc(1, sizeof(*item), GFP_KERNEL);
  1715. if (!item)
  1716. return -ENOMEM;
  1717. /* queue up work for dlm_assert_master_worker */
  1718. dlm_grab(dlm); /* get an extra ref for the work item */
  1719. dlm_init_work_item(dlm, item, dlm_assert_master_worker, NULL);
  1720. item->u.am.lockres = res; /* already have a ref */
  1721. /* can optionally ignore node numbers higher than this node */
  1722. item->u.am.ignore_higher = ignore_higher;
  1723. item->u.am.request_from = request_from;
  1724. item->u.am.flags = flags;
  1725. if (ignore_higher)
  1726. mlog(0, "IGNORE HIGHER: %.*s\n", res->lockname.len,
  1727. res->lockname.name);
  1728. spin_lock(&dlm->work_lock);
  1729. list_add_tail(&item->list, &dlm->work_list);
  1730. spin_unlock(&dlm->work_lock);
  1731. schedule_work(&dlm->dispatched_work);
  1732. return 0;
  1733. }
  1734. static void dlm_assert_master_worker(struct dlm_work_item *item, void *data)
  1735. {
  1736. struct dlm_ctxt *dlm = data;
  1737. int ret = 0;
  1738. struct dlm_lock_resource *res;
  1739. unsigned long nodemap[BITS_TO_LONGS(O2NM_MAX_NODES)];
  1740. int ignore_higher;
  1741. int bit;
  1742. u8 request_from;
  1743. u32 flags;
  1744. dlm = item->dlm;
  1745. res = item->u.am.lockres;
  1746. ignore_higher = item->u.am.ignore_higher;
  1747. request_from = item->u.am.request_from;
  1748. flags = item->u.am.flags;
  1749. spin_lock(&dlm->spinlock);
  1750. memcpy(nodemap, dlm->domain_map, sizeof(nodemap));
  1751. spin_unlock(&dlm->spinlock);
  1752. clear_bit(dlm->node_num, nodemap);
  1753. if (ignore_higher) {
  1754. /* if is this just to clear up mles for nodes below
  1755. * this node, do not send the message to the original
  1756. * caller or any node number higher than this */
  1757. clear_bit(request_from, nodemap);
  1758. bit = dlm->node_num;
  1759. while (1) {
  1760. bit = find_next_bit(nodemap, O2NM_MAX_NODES,
  1761. bit+1);
  1762. if (bit >= O2NM_MAX_NODES)
  1763. break;
  1764. clear_bit(bit, nodemap);
  1765. }
  1766. }
  1767. /* this call now finishes out the nodemap
  1768. * even if one or more nodes die */
  1769. mlog(0, "worker about to master %.*s here, this=%u\n",
  1770. res->lockname.len, res->lockname.name, dlm->node_num);
  1771. ret = dlm_do_assert_master(dlm, res->lockname.name,
  1772. res->lockname.len,
  1773. nodemap, flags);
  1774. if (ret < 0) {
  1775. /* no need to restart, we are done */
  1776. mlog_errno(ret);
  1777. }
  1778. dlm_lockres_put(res);
  1779. mlog(0, "finished with dlm_assert_master_worker\n");
  1780. }
  1781. /* SPECIAL CASE for the $RECOVERY lock used by the recovery thread.
  1782. * We cannot wait for node recovery to complete to begin mastering this
  1783. * lockres because this lockres is used to kick off recovery! ;-)
  1784. * So, do a pre-check on all living nodes to see if any of those nodes
  1785. * think that $RECOVERY is currently mastered by a dead node. If so,
  1786. * we wait a short time to allow that node to get notified by its own
  1787. * heartbeat stack, then check again. All $RECOVERY lock resources
  1788. * mastered by dead nodes are purged when the hearbeat callback is
  1789. * fired, so we can know for sure that it is safe to continue once
  1790. * the node returns a live node or no node. */
  1791. static int dlm_pre_master_reco_lockres(struct dlm_ctxt *dlm,
  1792. struct dlm_lock_resource *res)
  1793. {
  1794. struct dlm_node_iter iter;
  1795. int nodenum;
  1796. int ret = 0;
  1797. u8 master = DLM_LOCK_RES_OWNER_UNKNOWN;
  1798. spin_lock(&dlm->spinlock);
  1799. dlm_node_iter_init(dlm->domain_map, &iter);
  1800. spin_unlock(&dlm->spinlock);
  1801. while ((nodenum = dlm_node_iter_next(&iter)) >= 0) {
  1802. /* do not send to self */
  1803. if (nodenum == dlm->node_num)
  1804. continue;
  1805. ret = dlm_do_master_requery(dlm, res, nodenum, &master);
  1806. if (ret < 0) {
  1807. mlog_errno(ret);
  1808. if (!dlm_is_host_down(ret))
  1809. BUG();
  1810. /* host is down, so answer for that node would be
  1811. * DLM_LOCK_RES_OWNER_UNKNOWN. continue. */
  1812. }
  1813. if (master != DLM_LOCK_RES_OWNER_UNKNOWN) {
  1814. /* check to see if this master is in the recovery map */
  1815. spin_lock(&dlm->spinlock);
  1816. if (test_bit(master, dlm->recovery_map)) {
  1817. mlog(ML_NOTICE, "%s: node %u has not seen "
  1818. "node %u go down yet, and thinks the "
  1819. "dead node is mastering the recovery "
  1820. "lock. must wait.\n", dlm->name,
  1821. nodenum, master);
  1822. ret = -EAGAIN;
  1823. }
  1824. spin_unlock(&dlm->spinlock);
  1825. mlog(0, "%s: reco lock master is %u\n", dlm->name,
  1826. master);
  1827. break;
  1828. }
  1829. }
  1830. return ret;
  1831. }
  1832. /*
  1833. * DLM_MIGRATE_LOCKRES
  1834. */
  1835. int dlm_migrate_lockres(struct dlm_ctxt *dlm, struct dlm_lock_resource *res,
  1836. u8 target)
  1837. {
  1838. struct dlm_master_list_entry *mle = NULL;
  1839. struct dlm_master_list_entry *oldmle = NULL;
  1840. struct dlm_migratable_lockres *mres = NULL;
  1841. int ret = -EINVAL;
  1842. const char *name;
  1843. unsigned int namelen;
  1844. int mle_added = 0;
  1845. struct list_head *queue, *iter;
  1846. int i;
  1847. struct dlm_lock *lock;
  1848. int empty = 1;
  1849. if (!dlm_grab(dlm))
  1850. return -EINVAL;
  1851. name = res->lockname.name;
  1852. namelen = res->lockname.len;
  1853. mlog(0, "migrating %.*s to %u\n", namelen, name, target);
  1854. /*
  1855. * ensure this lockres is a proper candidate for migration
  1856. */
  1857. spin_lock(&res->spinlock);
  1858. if (res->owner == DLM_LOCK_RES_OWNER_UNKNOWN) {
  1859. mlog(0, "cannot migrate lockres with unknown owner!\n");
  1860. spin_unlock(&res->spinlock);
  1861. goto leave;
  1862. }
  1863. if (res->owner != dlm->node_num) {
  1864. mlog(0, "cannot migrate lockres this node doesn't own!\n");
  1865. spin_unlock(&res->spinlock);
  1866. goto leave;
  1867. }
  1868. mlog(0, "checking queues...\n");
  1869. queue = &res->granted;
  1870. for (i=0; i<3; i++) {
  1871. list_for_each(iter, queue) {
  1872. lock = list_entry (iter, struct dlm_lock, list);
  1873. empty = 0;
  1874. if (lock->ml.node == dlm->node_num) {
  1875. mlog(0, "found a lock owned by this node "
  1876. "still on the %s queue! will not "
  1877. "migrate this lockres\n",
  1878. i==0 ? "granted" :
  1879. (i==1 ? "converting" : "blocked"));
  1880. spin_unlock(&res->spinlock);
  1881. ret = -ENOTEMPTY;
  1882. goto leave;
  1883. }
  1884. }
  1885. queue++;
  1886. }
  1887. mlog(0, "all locks on this lockres are nonlocal. continuing\n");
  1888. spin_unlock(&res->spinlock);
  1889. /* no work to do */
  1890. if (empty) {
  1891. mlog(0, "no locks were found on this lockres! done!\n");
  1892. ret = 0;
  1893. goto leave;
  1894. }
  1895. /*
  1896. * preallocate up front
  1897. * if this fails, abort
  1898. */
  1899. ret = -ENOMEM;
  1900. mres = (struct dlm_migratable_lockres *) __get_free_page(GFP_KERNEL);
  1901. if (!mres) {
  1902. mlog_errno(ret);
  1903. goto leave;
  1904. }
  1905. mle = (struct dlm_master_list_entry *) kmem_cache_alloc(dlm_mle_cache,
  1906. GFP_KERNEL);
  1907. if (!mle) {
  1908. mlog_errno(ret);
  1909. goto leave;
  1910. }
  1911. ret = 0;
  1912. /*
  1913. * find a node to migrate the lockres to
  1914. */
  1915. mlog(0, "picking a migration node\n");
  1916. spin_lock(&dlm->spinlock);
  1917. /* pick a new node */
  1918. if (!test_bit(target, dlm->domain_map) ||
  1919. target >= O2NM_MAX_NODES) {
  1920. target = dlm_pick_migration_target(dlm, res);
  1921. }
  1922. mlog(0, "node %u chosen for migration\n", target);
  1923. if (target >= O2NM_MAX_NODES ||
  1924. !test_bit(target, dlm->domain_map)) {
  1925. /* target chosen is not alive */
  1926. ret = -EINVAL;
  1927. }
  1928. if (ret) {
  1929. spin_unlock(&dlm->spinlock);
  1930. goto fail;
  1931. }
  1932. mlog(0, "continuing with target = %u\n", target);
  1933. /*
  1934. * clear any existing master requests and
  1935. * add the migration mle to the list
  1936. */
  1937. spin_lock(&dlm->master_lock);
  1938. ret = dlm_add_migration_mle(dlm, res, mle, &oldmle, name,
  1939. namelen, target, dlm->node_num);
  1940. spin_unlock(&dlm->master_lock);
  1941. spin_unlock(&dlm->spinlock);
  1942. if (ret == -EEXIST) {
  1943. mlog(0, "another process is already migrating it\n");
  1944. goto fail;
  1945. }
  1946. mle_added = 1;
  1947. /*
  1948. * set the MIGRATING flag and flush asts
  1949. * if we fail after this we need to re-dirty the lockres
  1950. */
  1951. if (dlm_mark_lockres_migrating(dlm, res, target) < 0) {
  1952. mlog(ML_ERROR, "tried to migrate %.*s to %u, but "
  1953. "the target went down.\n", res->lockname.len,
  1954. res->lockname.name, target);
  1955. spin_lock(&res->spinlock);
  1956. res->state &= ~DLM_LOCK_RES_MIGRATING;
  1957. spin_unlock(&res->spinlock);
  1958. ret = -EINVAL;
  1959. }
  1960. fail:
  1961. if (oldmle) {
  1962. /* master is known, detach if not already detached */
  1963. dlm_mle_detach_hb_events(dlm, oldmle);
  1964. dlm_put_mle(oldmle);
  1965. }
  1966. if (ret < 0) {
  1967. if (mle_added) {
  1968. dlm_mle_detach_hb_events(dlm, mle);
  1969. dlm_put_mle(mle);
  1970. } else if (mle) {
  1971. kmem_cache_free(dlm_mle_cache, mle);
  1972. }
  1973. goto leave;
  1974. }
  1975. /*
  1976. * at this point, we have a migration target, an mle
  1977. * in the master list, and the MIGRATING flag set on
  1978. * the lockres
  1979. */
  1980. /* get an extra reference on the mle.
  1981. * otherwise the assert_master from the new
  1982. * master will destroy this.
  1983. * also, make sure that all callers of dlm_get_mle
  1984. * take both dlm->spinlock and dlm->master_lock */
  1985. spin_lock(&dlm->spinlock);
  1986. spin_lock(&dlm->master_lock);
  1987. dlm_get_mle_inuse(mle);
  1988. spin_unlock(&dlm->master_lock);
  1989. spin_unlock(&dlm->spinlock);
  1990. /* notify new node and send all lock state */
  1991. /* call send_one_lockres with migration flag.
  1992. * this serves as notice to the target node that a
  1993. * migration is starting. */
  1994. ret = dlm_send_one_lockres(dlm, res, mres, target,
  1995. DLM_MRES_MIGRATION);
  1996. if (ret < 0) {
  1997. mlog(0, "migration to node %u failed with %d\n",
  1998. target, ret);
  1999. /* migration failed, detach and clean up mle */
  2000. dlm_mle_detach_hb_events(dlm, mle);
  2001. dlm_put_mle(mle);
  2002. dlm_put_mle_inuse(mle);
  2003. spin_lock(&res->spinlock);
  2004. res->state &= ~DLM_LOCK_RES_MIGRATING;
  2005. spin_unlock(&res->spinlock);
  2006. goto leave;
  2007. }
  2008. /* at this point, the target sends a message to all nodes,
  2009. * (using dlm_do_migrate_request). this node is skipped since
  2010. * we had to put an mle in the list to begin the process. this
  2011. * node now waits for target to do an assert master. this node
  2012. * will be the last one notified, ensuring that the migration
  2013. * is complete everywhere. if the target dies while this is
  2014. * going on, some nodes could potentially see the target as the
  2015. * master, so it is important that my recovery finds the migration
  2016. * mle and sets the master to UNKNONWN. */
  2017. /* wait for new node to assert master */
  2018. while (1) {
  2019. ret = wait_event_interruptible_timeout(mle->wq,
  2020. (atomic_read(&mle->woken) == 1),
  2021. msecs_to_jiffies(5000));
  2022. if (ret >= 0) {
  2023. if (atomic_read(&mle->woken) == 1 ||
  2024. res->owner == target)
  2025. break;
  2026. mlog(0, "timed out during migration\n");
  2027. /* avoid hang during shutdown when migrating lockres
  2028. * to a node which also goes down */
  2029. if (dlm_is_node_dead(dlm, target)) {
  2030. mlog(0, "%s:%.*s: expected migration "
  2031. "target %u is no longer up, restarting\n",
  2032. dlm->name, res->lockname.len,
  2033. res->lockname.name, target);
  2034. ret = -ERESTARTSYS;
  2035. }
  2036. }
  2037. if (ret == -ERESTARTSYS) {
  2038. /* migration failed, detach and clean up mle */
  2039. dlm_mle_detach_hb_events(dlm, mle);
  2040. dlm_put_mle(mle);
  2041. dlm_put_mle_inuse(mle);
  2042. spin_lock(&res->spinlock);
  2043. res->state &= ~DLM_LOCK_RES_MIGRATING;
  2044. spin_unlock(&res->spinlock);
  2045. goto leave;
  2046. }
  2047. /* TODO: if node died: stop, clean up, return error */
  2048. }
  2049. /* all done, set the owner, clear the flag */
  2050. spin_lock(&res->spinlock);
  2051. dlm_set_lockres_owner(dlm, res, target);
  2052. res->state &= ~DLM_LOCK_RES_MIGRATING;
  2053. dlm_remove_nonlocal_locks(dlm, res);
  2054. spin_unlock(&res->spinlock);
  2055. wake_up(&res->wq);
  2056. /* master is known, detach if not already detached */
  2057. dlm_mle_detach_hb_events(dlm, mle);
  2058. dlm_put_mle_inuse(mle);
  2059. ret = 0;
  2060. dlm_lockres_calc_usage(dlm, res);
  2061. leave:
  2062. /* re-dirty the lockres if we failed */
  2063. if (ret < 0)
  2064. dlm_kick_thread(dlm, res);
  2065. /* TODO: cleanup */
  2066. if (mres)
  2067. free_page((unsigned long)mres);
  2068. dlm_put(dlm);
  2069. mlog(0, "returning %d\n", ret);
  2070. return ret;
  2071. }
  2072. EXPORT_SYMBOL_GPL(dlm_migrate_lockres);
  2073. int dlm_lock_basts_flushed(struct dlm_ctxt *dlm, struct dlm_lock *lock)
  2074. {
  2075. int ret;
  2076. spin_lock(&dlm->ast_lock);
  2077. spin_lock(&lock->spinlock);
  2078. ret = (list_empty(&lock->bast_list) && !lock->bast_pending);
  2079. spin_unlock(&lock->spinlock);
  2080. spin_unlock(&dlm->ast_lock);
  2081. return ret;
  2082. }
  2083. static int dlm_migration_can_proceed(struct dlm_ctxt *dlm,
  2084. struct dlm_lock_resource *res,
  2085. u8 mig_target)
  2086. {
  2087. int can_proceed;
  2088. spin_lock(&res->spinlock);
  2089. can_proceed = !!(res->state & DLM_LOCK_RES_MIGRATING);
  2090. spin_unlock(&res->spinlock);
  2091. /* target has died, so make the caller break out of the
  2092. * wait_event, but caller must recheck the domain_map */
  2093. spin_lock(&dlm->spinlock);
  2094. if (!test_bit(mig_target, dlm->domain_map))
  2095. can_proceed = 1;
  2096. spin_unlock(&dlm->spinlock);
  2097. return can_proceed;
  2098. }
  2099. int dlm_lockres_is_dirty(struct dlm_ctxt *dlm, struct dlm_lock_resource *res)
  2100. {
  2101. int ret;
  2102. spin_lock(&res->spinlock);
  2103. ret = !!(res->state & DLM_LOCK_RES_DIRTY);
  2104. spin_unlock(&res->spinlock);
  2105. return ret;
  2106. }
  2107. static int dlm_mark_lockres_migrating(struct dlm_ctxt *dlm,
  2108. struct dlm_lock_resource *res,
  2109. u8 target)
  2110. {
  2111. int ret = 0;
  2112. mlog(0, "dlm_mark_lockres_migrating: %.*s, from %u to %u\n",
  2113. res->lockname.len, res->lockname.name, dlm->node_num,
  2114. target);
  2115. /* need to set MIGRATING flag on lockres. this is done by
  2116. * ensuring that all asts have been flushed for this lockres. */
  2117. spin_lock(&res->spinlock);
  2118. BUG_ON(res->migration_pending);
  2119. res->migration_pending = 1;
  2120. /* strategy is to reserve an extra ast then release
  2121. * it below, letting the release do all of the work */
  2122. __dlm_lockres_reserve_ast(res);
  2123. spin_unlock(&res->spinlock);
  2124. /* now flush all the pending asts.. hang out for a bit */
  2125. dlm_kick_thread(dlm, res);
  2126. wait_event(dlm->ast_wq, !dlm_lockres_is_dirty(dlm, res));
  2127. dlm_lockres_release_ast(dlm, res);
  2128. mlog(0, "about to wait on migration_wq, dirty=%s\n",
  2129. res->state & DLM_LOCK_RES_DIRTY ? "yes" : "no");
  2130. /* if the extra ref we just put was the final one, this
  2131. * will pass thru immediately. otherwise, we need to wait
  2132. * for the last ast to finish. */
  2133. again:
  2134. ret = wait_event_interruptible_timeout(dlm->migration_wq,
  2135. dlm_migration_can_proceed(dlm, res, target),
  2136. msecs_to_jiffies(1000));
  2137. if (ret < 0) {
  2138. mlog(0, "woken again: migrating? %s, dead? %s\n",
  2139. res->state & DLM_LOCK_RES_MIGRATING ? "yes":"no",
  2140. test_bit(target, dlm->domain_map) ? "no":"yes");
  2141. } else {
  2142. mlog(0, "all is well: migrating? %s, dead? %s\n",
  2143. res->state & DLM_LOCK_RES_MIGRATING ? "yes":"no",
  2144. test_bit(target, dlm->domain_map) ? "no":"yes");
  2145. }
  2146. if (!dlm_migration_can_proceed(dlm, res, target)) {
  2147. mlog(0, "trying again...\n");
  2148. goto again;
  2149. }
  2150. /* did the target go down or die? */
  2151. spin_lock(&dlm->spinlock);
  2152. if (!test_bit(target, dlm->domain_map)) {
  2153. mlog(ML_ERROR, "aha. migration target %u just went down\n",
  2154. target);
  2155. ret = -EHOSTDOWN;
  2156. }
  2157. spin_unlock(&dlm->spinlock);
  2158. /*
  2159. * at this point:
  2160. *
  2161. * o the DLM_LOCK_RES_MIGRATING flag is set
  2162. * o there are no pending asts on this lockres
  2163. * o all processes trying to reserve an ast on this
  2164. * lockres must wait for the MIGRATING flag to clear
  2165. */
  2166. return ret;
  2167. }
  2168. /* last step in the migration process.
  2169. * original master calls this to free all of the dlm_lock
  2170. * structures that used to be for other nodes. */
  2171. static void dlm_remove_nonlocal_locks(struct dlm_ctxt *dlm,
  2172. struct dlm_lock_resource *res)
  2173. {
  2174. struct list_head *iter, *iter2;
  2175. struct list_head *queue = &res->granted;
  2176. int i;
  2177. struct dlm_lock *lock;
  2178. assert_spin_locked(&res->spinlock);
  2179. BUG_ON(res->owner == dlm->node_num);
  2180. for (i=0; i<3; i++) {
  2181. list_for_each_safe(iter, iter2, queue) {
  2182. lock = list_entry (iter, struct dlm_lock, list);
  2183. if (lock->ml.node != dlm->node_num) {
  2184. mlog(0, "putting lock for node %u\n",
  2185. lock->ml.node);
  2186. /* be extra careful */
  2187. BUG_ON(!list_empty(&lock->ast_list));
  2188. BUG_ON(!list_empty(&lock->bast_list));
  2189. BUG_ON(lock->ast_pending);
  2190. BUG_ON(lock->bast_pending);
  2191. list_del_init(&lock->list);
  2192. dlm_lock_put(lock);
  2193. }
  2194. }
  2195. queue++;
  2196. }
  2197. }
  2198. /* for now this is not too intelligent. we will
  2199. * need stats to make this do the right thing.
  2200. * this just finds the first lock on one of the
  2201. * queues and uses that node as the target. */
  2202. static u8 dlm_pick_migration_target(struct dlm_ctxt *dlm,
  2203. struct dlm_lock_resource *res)
  2204. {
  2205. int i;
  2206. struct list_head *queue = &res->granted;
  2207. struct list_head *iter;
  2208. struct dlm_lock *lock;
  2209. int nodenum;
  2210. assert_spin_locked(&dlm->spinlock);
  2211. spin_lock(&res->spinlock);
  2212. for (i=0; i<3; i++) {
  2213. list_for_each(iter, queue) {
  2214. /* up to the caller to make sure this node
  2215. * is alive */
  2216. lock = list_entry (iter, struct dlm_lock, list);
  2217. if (lock->ml.node != dlm->node_num) {
  2218. spin_unlock(&res->spinlock);
  2219. return lock->ml.node;
  2220. }
  2221. }
  2222. queue++;
  2223. }
  2224. spin_unlock(&res->spinlock);
  2225. mlog(0, "have not found a suitable target yet! checking domain map\n");
  2226. /* ok now we're getting desperate. pick anyone alive. */
  2227. nodenum = -1;
  2228. while (1) {
  2229. nodenum = find_next_bit(dlm->domain_map,
  2230. O2NM_MAX_NODES, nodenum+1);
  2231. mlog(0, "found %d in domain map\n", nodenum);
  2232. if (nodenum >= O2NM_MAX_NODES)
  2233. break;
  2234. if (nodenum != dlm->node_num) {
  2235. mlog(0, "picking %d\n", nodenum);
  2236. return nodenum;
  2237. }
  2238. }
  2239. mlog(0, "giving up. no master to migrate to\n");
  2240. return DLM_LOCK_RES_OWNER_UNKNOWN;
  2241. }
  2242. /* this is called by the new master once all lockres
  2243. * data has been received */
  2244. static int dlm_do_migrate_request(struct dlm_ctxt *dlm,
  2245. struct dlm_lock_resource *res,
  2246. u8 master, u8 new_master,
  2247. struct dlm_node_iter *iter)
  2248. {
  2249. struct dlm_migrate_request migrate;
  2250. int ret, status = 0;
  2251. int nodenum;
  2252. memset(&migrate, 0, sizeof(migrate));
  2253. migrate.namelen = res->lockname.len;
  2254. memcpy(migrate.name, res->lockname.name, migrate.namelen);
  2255. migrate.new_master = new_master;
  2256. migrate.master = master;
  2257. ret = 0;
  2258. /* send message to all nodes, except the master and myself */
  2259. while ((nodenum = dlm_node_iter_next(iter)) >= 0) {
  2260. if (nodenum == master ||
  2261. nodenum == new_master)
  2262. continue;
  2263. ret = o2net_send_message(DLM_MIGRATE_REQUEST_MSG, dlm->key,
  2264. &migrate, sizeof(migrate), nodenum,
  2265. &status);
  2266. if (ret < 0)
  2267. mlog_errno(ret);
  2268. else if (status < 0) {
  2269. mlog(0, "migrate request (node %u) returned %d!\n",
  2270. nodenum, status);
  2271. ret = status;
  2272. }
  2273. }
  2274. if (ret < 0)
  2275. mlog_errno(ret);
  2276. mlog(0, "returning ret=%d\n", ret);
  2277. return ret;
  2278. }
  2279. /* if there is an existing mle for this lockres, we now know who the master is.
  2280. * (the one who sent us *this* message) we can clear it up right away.
  2281. * since the process that put the mle on the list still has a reference to it,
  2282. * we can unhash it now, set the master and wake the process. as a result,
  2283. * we will have no mle in the list to start with. now we can add an mle for
  2284. * the migration and this should be the only one found for those scanning the
  2285. * list. */
  2286. int dlm_migrate_request_handler(struct o2net_msg *msg, u32 len, void *data)
  2287. {
  2288. struct dlm_ctxt *dlm = data;
  2289. struct dlm_lock_resource *res = NULL;
  2290. struct dlm_migrate_request *migrate = (struct dlm_migrate_request *) msg->buf;
  2291. struct dlm_master_list_entry *mle = NULL, *oldmle = NULL;
  2292. const char *name;
  2293. unsigned int namelen, hash;
  2294. int ret = 0;
  2295. if (!dlm_grab(dlm))
  2296. return -EINVAL;
  2297. name = migrate->name;
  2298. namelen = migrate->namelen;
  2299. hash = dlm_lockid_hash(name, namelen);
  2300. /* preallocate.. if this fails, abort */
  2301. mle = (struct dlm_master_list_entry *) kmem_cache_alloc(dlm_mle_cache,
  2302. GFP_KERNEL);
  2303. if (!mle) {
  2304. ret = -ENOMEM;
  2305. goto leave;
  2306. }
  2307. /* check for pre-existing lock */
  2308. spin_lock(&dlm->spinlock);
  2309. res = __dlm_lookup_lockres(dlm, name, namelen, hash);
  2310. spin_lock(&dlm->master_lock);
  2311. if (res) {
  2312. spin_lock(&res->spinlock);
  2313. if (res->state & DLM_LOCK_RES_RECOVERING) {
  2314. /* if all is working ok, this can only mean that we got
  2315. * a migrate request from a node that we now see as
  2316. * dead. what can we do here? drop it to the floor? */
  2317. spin_unlock(&res->spinlock);
  2318. mlog(ML_ERROR, "Got a migrate request, but the "
  2319. "lockres is marked as recovering!");
  2320. kmem_cache_free(dlm_mle_cache, mle);
  2321. ret = -EINVAL; /* need a better solution */
  2322. goto unlock;
  2323. }
  2324. res->state |= DLM_LOCK_RES_MIGRATING;
  2325. spin_unlock(&res->spinlock);
  2326. }
  2327. /* ignore status. only nonzero status would BUG. */
  2328. ret = dlm_add_migration_mle(dlm, res, mle, &oldmle,
  2329. name, namelen,
  2330. migrate->new_master,
  2331. migrate->master);
  2332. unlock:
  2333. spin_unlock(&dlm->master_lock);
  2334. spin_unlock(&dlm->spinlock);
  2335. if (oldmle) {
  2336. /* master is known, detach if not already detached */
  2337. dlm_mle_detach_hb_events(dlm, oldmle);
  2338. dlm_put_mle(oldmle);
  2339. }
  2340. if (res)
  2341. dlm_lockres_put(res);
  2342. leave:
  2343. dlm_put(dlm);
  2344. return ret;
  2345. }
  2346. /* must be holding dlm->spinlock and dlm->master_lock
  2347. * when adding a migration mle, we can clear any other mles
  2348. * in the master list because we know with certainty that
  2349. * the master is "master". so we remove any old mle from
  2350. * the list after setting it's master field, and then add
  2351. * the new migration mle. this way we can hold with the rule
  2352. * of having only one mle for a given lock name at all times. */
  2353. static int dlm_add_migration_mle(struct dlm_ctxt *dlm,
  2354. struct dlm_lock_resource *res,
  2355. struct dlm_master_list_entry *mle,
  2356. struct dlm_master_list_entry **oldmle,
  2357. const char *name, unsigned int namelen,
  2358. u8 new_master, u8 master)
  2359. {
  2360. int found;
  2361. int ret = 0;
  2362. *oldmle = NULL;
  2363. mlog_entry_void();
  2364. assert_spin_locked(&dlm->spinlock);
  2365. assert_spin_locked(&dlm->master_lock);
  2366. /* caller is responsible for any ref taken here on oldmle */
  2367. found = dlm_find_mle(dlm, oldmle, (char *)name, namelen);
  2368. if (found) {
  2369. struct dlm_master_list_entry *tmp = *oldmle;
  2370. spin_lock(&tmp->spinlock);
  2371. if (tmp->type == DLM_MLE_MIGRATION) {
  2372. if (master == dlm->node_num) {
  2373. /* ah another process raced me to it */
  2374. mlog(0, "tried to migrate %.*s, but some "
  2375. "process beat me to it\n",
  2376. namelen, name);
  2377. ret = -EEXIST;
  2378. } else {
  2379. /* bad. 2 NODES are trying to migrate! */
  2380. mlog(ML_ERROR, "migration error mle: "
  2381. "master=%u new_master=%u // request: "
  2382. "master=%u new_master=%u // "
  2383. "lockres=%.*s\n",
  2384. tmp->master, tmp->new_master,
  2385. master, new_master,
  2386. namelen, name);
  2387. BUG();
  2388. }
  2389. } else {
  2390. /* this is essentially what assert_master does */
  2391. tmp->master = master;
  2392. atomic_set(&tmp->woken, 1);
  2393. wake_up(&tmp->wq);
  2394. /* remove it from the list so that only one
  2395. * mle will be found */
  2396. list_del_init(&tmp->list);
  2397. __dlm_mle_detach_hb_events(dlm, mle);
  2398. }
  2399. spin_unlock(&tmp->spinlock);
  2400. }
  2401. /* now add a migration mle to the tail of the list */
  2402. dlm_init_mle(mle, DLM_MLE_MIGRATION, dlm, res, name, namelen);
  2403. mle->new_master = new_master;
  2404. mle->master = master;
  2405. /* do this for consistency with other mle types */
  2406. set_bit(new_master, mle->maybe_map);
  2407. list_add(&mle->list, &dlm->master_list);
  2408. return ret;
  2409. }
  2410. void dlm_clean_master_list(struct dlm_ctxt *dlm, u8 dead_node)
  2411. {
  2412. struct list_head *iter, *iter2;
  2413. struct dlm_master_list_entry *mle;
  2414. struct dlm_lock_resource *res;
  2415. unsigned int hash;
  2416. mlog_entry("dlm=%s, dead node=%u\n", dlm->name, dead_node);
  2417. top:
  2418. assert_spin_locked(&dlm->spinlock);
  2419. /* clean the master list */
  2420. spin_lock(&dlm->master_lock);
  2421. list_for_each_safe(iter, iter2, &dlm->master_list) {
  2422. mle = list_entry(iter, struct dlm_master_list_entry, list);
  2423. BUG_ON(mle->type != DLM_MLE_BLOCK &&
  2424. mle->type != DLM_MLE_MASTER &&
  2425. mle->type != DLM_MLE_MIGRATION);
  2426. /* MASTER mles are initiated locally. the waiting
  2427. * process will notice the node map change
  2428. * shortly. let that happen as normal. */
  2429. if (mle->type == DLM_MLE_MASTER)
  2430. continue;
  2431. /* BLOCK mles are initiated by other nodes.
  2432. * need to clean up if the dead node would have
  2433. * been the master. */
  2434. if (mle->type == DLM_MLE_BLOCK) {
  2435. int bit;
  2436. spin_lock(&mle->spinlock);
  2437. bit = find_next_bit(mle->maybe_map, O2NM_MAX_NODES, 0);
  2438. if (bit != dead_node) {
  2439. mlog(0, "mle found, but dead node %u would "
  2440. "not have been master\n", dead_node);
  2441. spin_unlock(&mle->spinlock);
  2442. } else {
  2443. /* must drop the refcount by one since the
  2444. * assert_master will never arrive. this
  2445. * may result in the mle being unlinked and
  2446. * freed, but there may still be a process
  2447. * waiting in the dlmlock path which is fine. */
  2448. mlog(ML_ERROR, "node %u was expected master\n",
  2449. dead_node);
  2450. atomic_set(&mle->woken, 1);
  2451. spin_unlock(&mle->spinlock);
  2452. wake_up(&mle->wq);
  2453. /* do not need events any longer, so detach
  2454. * from heartbeat */
  2455. __dlm_mle_detach_hb_events(dlm, mle);
  2456. __dlm_put_mle(mle);
  2457. }
  2458. continue;
  2459. }
  2460. /* everything else is a MIGRATION mle */
  2461. /* the rule for MIGRATION mles is that the master
  2462. * becomes UNKNOWN if *either* the original or
  2463. * the new master dies. all UNKNOWN lockreses
  2464. * are sent to whichever node becomes the recovery
  2465. * master. the new master is responsible for
  2466. * determining if there is still a master for
  2467. * this lockres, or if he needs to take over
  2468. * mastery. either way, this node should expect
  2469. * another message to resolve this. */
  2470. if (mle->master != dead_node &&
  2471. mle->new_master != dead_node)
  2472. continue;
  2473. /* if we have reached this point, this mle needs to
  2474. * be removed from the list and freed. */
  2475. /* remove from the list early. NOTE: unlinking
  2476. * list_head while in list_for_each_safe */
  2477. __dlm_mle_detach_hb_events(dlm, mle);
  2478. spin_lock(&mle->spinlock);
  2479. list_del_init(&mle->list);
  2480. atomic_set(&mle->woken, 1);
  2481. spin_unlock(&mle->spinlock);
  2482. wake_up(&mle->wq);
  2483. mlog(0, "%s: node %u died during migration from "
  2484. "%u to %u!\n", dlm->name, dead_node,
  2485. mle->master, mle->new_master);
  2486. /* if there is a lockres associated with this
  2487. * mle, find it and set its owner to UNKNOWN */
  2488. hash = dlm_lockid_hash(mle->u.name.name, mle->u.name.len);
  2489. res = __dlm_lookup_lockres(dlm, mle->u.name.name,
  2490. mle->u.name.len, hash);
  2491. if (res) {
  2492. /* unfortunately if we hit this rare case, our
  2493. * lock ordering is messed. we need to drop
  2494. * the master lock so that we can take the
  2495. * lockres lock, meaning that we will have to
  2496. * restart from the head of list. */
  2497. spin_unlock(&dlm->master_lock);
  2498. /* move lockres onto recovery list */
  2499. spin_lock(&res->spinlock);
  2500. dlm_set_lockres_owner(dlm, res,
  2501. DLM_LOCK_RES_OWNER_UNKNOWN);
  2502. dlm_move_lockres_to_recovery_list(dlm, res);
  2503. spin_unlock(&res->spinlock);
  2504. dlm_lockres_put(res);
  2505. /* about to get rid of mle, detach from heartbeat */
  2506. __dlm_mle_detach_hb_events(dlm, mle);
  2507. /* dump the mle */
  2508. spin_lock(&dlm->master_lock);
  2509. __dlm_put_mle(mle);
  2510. spin_unlock(&dlm->master_lock);
  2511. /* restart */
  2512. goto top;
  2513. }
  2514. /* this may be the last reference */
  2515. __dlm_put_mle(mle);
  2516. }
  2517. spin_unlock(&dlm->master_lock);
  2518. }
  2519. int dlm_finish_migration(struct dlm_ctxt *dlm, struct dlm_lock_resource *res,
  2520. u8 old_master)
  2521. {
  2522. struct dlm_node_iter iter;
  2523. int ret = 0;
  2524. spin_lock(&dlm->spinlock);
  2525. dlm_node_iter_init(dlm->domain_map, &iter);
  2526. clear_bit(old_master, iter.node_map);
  2527. clear_bit(dlm->node_num, iter.node_map);
  2528. spin_unlock(&dlm->spinlock);
  2529. mlog(0, "now time to do a migrate request to other nodes\n");
  2530. ret = dlm_do_migrate_request(dlm, res, old_master,
  2531. dlm->node_num, &iter);
  2532. if (ret < 0) {
  2533. mlog_errno(ret);
  2534. goto leave;
  2535. }
  2536. mlog(0, "doing assert master of %.*s to all except the original node\n",
  2537. res->lockname.len, res->lockname.name);
  2538. /* this call now finishes out the nodemap
  2539. * even if one or more nodes die */
  2540. ret = dlm_do_assert_master(dlm, res->lockname.name,
  2541. res->lockname.len, iter.node_map,
  2542. DLM_ASSERT_MASTER_FINISH_MIGRATION);
  2543. if (ret < 0) {
  2544. /* no longer need to retry. all living nodes contacted. */
  2545. mlog_errno(ret);
  2546. ret = 0;
  2547. }
  2548. memset(iter.node_map, 0, sizeof(iter.node_map));
  2549. set_bit(old_master, iter.node_map);
  2550. mlog(0, "doing assert master of %.*s back to %u\n",
  2551. res->lockname.len, res->lockname.name, old_master);
  2552. ret = dlm_do_assert_master(dlm, res->lockname.name,
  2553. res->lockname.len, iter.node_map,
  2554. DLM_ASSERT_MASTER_FINISH_MIGRATION);
  2555. if (ret < 0) {
  2556. mlog(0, "assert master to original master failed "
  2557. "with %d.\n", ret);
  2558. /* the only nonzero status here would be because of
  2559. * a dead original node. we're done. */
  2560. ret = 0;
  2561. }
  2562. /* all done, set the owner, clear the flag */
  2563. spin_lock(&res->spinlock);
  2564. dlm_set_lockres_owner(dlm, res, dlm->node_num);
  2565. res->state &= ~DLM_LOCK_RES_MIGRATING;
  2566. spin_unlock(&res->spinlock);
  2567. /* re-dirty it on the new master */
  2568. dlm_kick_thread(dlm, res);
  2569. wake_up(&res->wq);
  2570. leave:
  2571. return ret;
  2572. }
  2573. /*
  2574. * LOCKRES AST REFCOUNT
  2575. * this is integral to migration
  2576. */
  2577. /* for future intent to call an ast, reserve one ahead of time.
  2578. * this should be called only after waiting on the lockres
  2579. * with dlm_wait_on_lockres, and while still holding the
  2580. * spinlock after the call. */
  2581. void __dlm_lockres_reserve_ast(struct dlm_lock_resource *res)
  2582. {
  2583. assert_spin_locked(&res->spinlock);
  2584. if (res->state & DLM_LOCK_RES_MIGRATING) {
  2585. __dlm_print_one_lock_resource(res);
  2586. }
  2587. BUG_ON(res->state & DLM_LOCK_RES_MIGRATING);
  2588. atomic_inc(&res->asts_reserved);
  2589. }
  2590. /*
  2591. * used to drop the reserved ast, either because it went unused,
  2592. * or because the ast/bast was actually called.
  2593. *
  2594. * also, if there is a pending migration on this lockres,
  2595. * and this was the last pending ast on the lockres,
  2596. * atomically set the MIGRATING flag before we drop the lock.
  2597. * this is how we ensure that migration can proceed with no
  2598. * asts in progress. note that it is ok if the state of the
  2599. * queues is such that a lock should be granted in the future
  2600. * or that a bast should be fired, because the new master will
  2601. * shuffle the lists on this lockres as soon as it is migrated.
  2602. */
  2603. void dlm_lockres_release_ast(struct dlm_ctxt *dlm,
  2604. struct dlm_lock_resource *res)
  2605. {
  2606. if (!atomic_dec_and_lock(&res->asts_reserved, &res->spinlock))
  2607. return;
  2608. if (!res->migration_pending) {
  2609. spin_unlock(&res->spinlock);
  2610. return;
  2611. }
  2612. BUG_ON(res->state & DLM_LOCK_RES_MIGRATING);
  2613. res->migration_pending = 0;
  2614. res->state |= DLM_LOCK_RES_MIGRATING;
  2615. spin_unlock(&res->spinlock);
  2616. wake_up(&res->wq);
  2617. wake_up(&dlm->migration_wq);
  2618. }