dlmmaster.c 94 KB

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