dlmmaster.c 96 KB

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