dlmmaster.c 96 KB

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