dlmmaster.c 95 KB

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