dlmmaster.c 96 KB

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