dlmmaster.c 94 KB

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