dlmmaster.c 95 KB

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