dlmmaster.c 94 KB

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