dlmmaster.c 95 KB

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