dlmmaster.c 76 KB

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