dlmmaster.c 94 KB

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