dlmmaster.c 94 KB

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