dlmmaster.c 96 KB

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