dlmmaster.c 97 KB

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