dlmmaster.c 93 KB

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