dlmmaster.c 95 KB

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