dlmmaster.c 77 KB

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