dlmmaster.c 79 KB

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