dlmmaster.c 91 KB

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