dlmmaster.c 93 KB

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