dlmmaster.c 94 KB

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