dlmmaster.c 94 KB

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