dlmmaster.c 96 KB

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