dlmmaster.c 71 KB

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