dlmmaster.c 92 KB

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