dlmmaster.c 96 KB

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