dlmmaster.c 94 KB

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