dlmmaster.c 94 KB

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