dlmmaster.c 98 KB

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