dlmmaster.c 81 KB

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