dlmmaster.c 77 KB

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