dlmmaster.c 95 KB

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