dlmmaster.c 94 KB

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