dlmrecovery.c 83 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825
  1. /* -*- mode: c; c-basic-offset: 8; -*-
  2. * vim: noexpandtab sw=8 ts=8 sts=0:
  3. *
  4. * dlmrecovery.c
  5. *
  6. * recovery stuff
  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/timer.h>
  39. #include <linux/kthread.h>
  40. #include <linux/delay.h>
  41. #include "cluster/heartbeat.h"
  42. #include "cluster/nodemanager.h"
  43. #include "cluster/tcp.h"
  44. #include "dlmapi.h"
  45. #include "dlmcommon.h"
  46. #include "dlmdomain.h"
  47. #define MLOG_MASK_PREFIX (ML_DLM|ML_DLM_RECOVERY)
  48. #include "cluster/masklog.h"
  49. static void dlm_do_local_recovery_cleanup(struct dlm_ctxt *dlm, u8 dead_node);
  50. static int dlm_recovery_thread(void *data);
  51. void dlm_complete_recovery_thread(struct dlm_ctxt *dlm);
  52. int dlm_launch_recovery_thread(struct dlm_ctxt *dlm);
  53. void dlm_kick_recovery_thread(struct dlm_ctxt *dlm);
  54. static int dlm_do_recovery(struct dlm_ctxt *dlm);
  55. static int dlm_pick_recovery_master(struct dlm_ctxt *dlm);
  56. static int dlm_remaster_locks(struct dlm_ctxt *dlm, u8 dead_node);
  57. static int dlm_init_recovery_area(struct dlm_ctxt *dlm, u8 dead_node);
  58. static int dlm_request_all_locks(struct dlm_ctxt *dlm,
  59. u8 request_from, u8 dead_node);
  60. static void dlm_destroy_recovery_area(struct dlm_ctxt *dlm, u8 dead_node);
  61. static inline int dlm_num_locks_in_lockres(struct dlm_lock_resource *res);
  62. static void dlm_init_migratable_lockres(struct dlm_migratable_lockres *mres,
  63. const char *lockname, int namelen,
  64. int total_locks, u64 cookie,
  65. u8 flags, u8 master);
  66. static int dlm_send_mig_lockres_msg(struct dlm_ctxt *dlm,
  67. struct dlm_migratable_lockres *mres,
  68. u8 send_to,
  69. struct dlm_lock_resource *res,
  70. int total_locks);
  71. static int dlm_process_recovery_data(struct dlm_ctxt *dlm,
  72. struct dlm_lock_resource *res,
  73. struct dlm_migratable_lockres *mres);
  74. static int dlm_send_finalize_reco_message(struct dlm_ctxt *dlm);
  75. static int dlm_send_all_done_msg(struct dlm_ctxt *dlm,
  76. u8 dead_node, u8 send_to);
  77. static int dlm_send_begin_reco_message(struct dlm_ctxt *dlm, u8 dead_node);
  78. static void dlm_move_reco_locks_to_list(struct dlm_ctxt *dlm,
  79. struct list_head *list, u8 dead_node);
  80. static void dlm_finish_local_lockres_recovery(struct dlm_ctxt *dlm,
  81. u8 dead_node, u8 new_master);
  82. static void dlm_reco_ast(void *astdata);
  83. static void dlm_reco_bast(void *astdata, int blocked_type);
  84. static void dlm_reco_unlock_ast(void *astdata, enum dlm_status st);
  85. static void dlm_request_all_locks_worker(struct dlm_work_item *item,
  86. void *data);
  87. static void dlm_mig_lockres_worker(struct dlm_work_item *item, void *data);
  88. static int dlm_lockres_master_requery(struct dlm_ctxt *dlm,
  89. struct dlm_lock_resource *res,
  90. u8 *real_master);
  91. static u64 dlm_get_next_mig_cookie(void);
  92. static DEFINE_SPINLOCK(dlm_reco_state_lock);
  93. static DEFINE_SPINLOCK(dlm_mig_cookie_lock);
  94. static u64 dlm_mig_cookie = 1;
  95. static u64 dlm_get_next_mig_cookie(void)
  96. {
  97. u64 c;
  98. spin_lock(&dlm_mig_cookie_lock);
  99. c = dlm_mig_cookie;
  100. if (dlm_mig_cookie == (~0ULL))
  101. dlm_mig_cookie = 1;
  102. else
  103. dlm_mig_cookie++;
  104. spin_unlock(&dlm_mig_cookie_lock);
  105. return c;
  106. }
  107. static inline void dlm_set_reco_dead_node(struct dlm_ctxt *dlm,
  108. u8 dead_node)
  109. {
  110. assert_spin_locked(&dlm->spinlock);
  111. if (dlm->reco.dead_node != dead_node)
  112. mlog(0, "%s: changing dead_node from %u to %u\n",
  113. dlm->name, dlm->reco.dead_node, dead_node);
  114. dlm->reco.dead_node = dead_node;
  115. }
  116. static inline void dlm_set_reco_master(struct dlm_ctxt *dlm,
  117. u8 master)
  118. {
  119. assert_spin_locked(&dlm->spinlock);
  120. mlog(0, "%s: changing new_master from %u to %u\n",
  121. dlm->name, dlm->reco.new_master, master);
  122. dlm->reco.new_master = master;
  123. }
  124. static inline void __dlm_reset_recovery(struct dlm_ctxt *dlm)
  125. {
  126. assert_spin_locked(&dlm->spinlock);
  127. clear_bit(dlm->reco.dead_node, dlm->recovery_map);
  128. dlm_set_reco_dead_node(dlm, O2NM_INVALID_NODE_NUM);
  129. dlm_set_reco_master(dlm, O2NM_INVALID_NODE_NUM);
  130. }
  131. static inline void dlm_reset_recovery(struct dlm_ctxt *dlm)
  132. {
  133. spin_lock(&dlm->spinlock);
  134. __dlm_reset_recovery(dlm);
  135. spin_unlock(&dlm->spinlock);
  136. }
  137. /* Worker function used during recovery. */
  138. void dlm_dispatch_work(struct work_struct *work)
  139. {
  140. struct dlm_ctxt *dlm =
  141. container_of(work, struct dlm_ctxt, dispatched_work);
  142. LIST_HEAD(tmp_list);
  143. struct dlm_work_item *item, *next;
  144. dlm_workfunc_t *workfunc;
  145. int tot=0;
  146. spin_lock(&dlm->work_lock);
  147. list_splice_init(&dlm->work_list, &tmp_list);
  148. spin_unlock(&dlm->work_lock);
  149. list_for_each_entry(item, &tmp_list, list) {
  150. tot++;
  151. }
  152. mlog(0, "%s: work thread has %d work items\n", dlm->name, tot);
  153. list_for_each_entry_safe(item, next, &tmp_list, list) {
  154. workfunc = item->func;
  155. list_del_init(&item->list);
  156. /* already have ref on dlm to avoid having
  157. * it disappear. just double-check. */
  158. BUG_ON(item->dlm != dlm);
  159. /* this is allowed to sleep and
  160. * call network stuff */
  161. workfunc(item, item->data);
  162. dlm_put(dlm);
  163. kfree(item);
  164. }
  165. }
  166. /*
  167. * RECOVERY THREAD
  168. */
  169. void dlm_kick_recovery_thread(struct dlm_ctxt *dlm)
  170. {
  171. /* wake the recovery thread
  172. * this will wake the reco thread in one of three places
  173. * 1) sleeping with no recovery happening
  174. * 2) sleeping with recovery mastered elsewhere
  175. * 3) recovery mastered here, waiting on reco data */
  176. wake_up(&dlm->dlm_reco_thread_wq);
  177. }
  178. /* Launch the recovery thread */
  179. int dlm_launch_recovery_thread(struct dlm_ctxt *dlm)
  180. {
  181. mlog(0, "starting dlm recovery thread...\n");
  182. dlm->dlm_reco_thread_task = kthread_run(dlm_recovery_thread, dlm,
  183. "dlm_reco_thread");
  184. if (IS_ERR(dlm->dlm_reco_thread_task)) {
  185. mlog_errno(PTR_ERR(dlm->dlm_reco_thread_task));
  186. dlm->dlm_reco_thread_task = NULL;
  187. return -EINVAL;
  188. }
  189. return 0;
  190. }
  191. void dlm_complete_recovery_thread(struct dlm_ctxt *dlm)
  192. {
  193. if (dlm->dlm_reco_thread_task) {
  194. mlog(0, "waiting for dlm recovery thread to exit\n");
  195. kthread_stop(dlm->dlm_reco_thread_task);
  196. dlm->dlm_reco_thread_task = NULL;
  197. }
  198. }
  199. /*
  200. * this is lame, but here's how recovery works...
  201. * 1) all recovery threads cluster wide will work on recovering
  202. * ONE node at a time
  203. * 2) negotiate who will take over all the locks for the dead node.
  204. * thats right... ALL the locks.
  205. * 3) once a new master is chosen, everyone scans all locks
  206. * and moves aside those mastered by the dead guy
  207. * 4) each of these locks should be locked until recovery is done
  208. * 5) the new master collects up all of secondary lock queue info
  209. * one lock at a time, forcing each node to communicate back
  210. * before continuing
  211. * 6) each secondary lock queue responds with the full known lock info
  212. * 7) once the new master has run all its locks, it sends a ALLDONE!
  213. * message to everyone
  214. * 8) upon receiving this message, the secondary queue node unlocks
  215. * and responds to the ALLDONE
  216. * 9) once the new master gets responses from everyone, he unlocks
  217. * everything and recovery for this dead node is done
  218. *10) go back to 2) while there are still dead nodes
  219. *
  220. */
  221. static void dlm_print_reco_node_status(struct dlm_ctxt *dlm)
  222. {
  223. struct dlm_reco_node_data *ndata;
  224. struct dlm_lock_resource *res;
  225. mlog(ML_NOTICE, "%s(%d): recovery info, state=%s, dead=%u, master=%u\n",
  226. dlm->name, task_pid_nr(dlm->dlm_reco_thread_task),
  227. dlm->reco.state & DLM_RECO_STATE_ACTIVE ? "ACTIVE" : "inactive",
  228. dlm->reco.dead_node, dlm->reco.new_master);
  229. list_for_each_entry(ndata, &dlm->reco.node_data, list) {
  230. char *st = "unknown";
  231. switch (ndata->state) {
  232. case DLM_RECO_NODE_DATA_INIT:
  233. st = "init";
  234. break;
  235. case DLM_RECO_NODE_DATA_REQUESTING:
  236. st = "requesting";
  237. break;
  238. case DLM_RECO_NODE_DATA_DEAD:
  239. st = "dead";
  240. break;
  241. case DLM_RECO_NODE_DATA_RECEIVING:
  242. st = "receiving";
  243. break;
  244. case DLM_RECO_NODE_DATA_REQUESTED:
  245. st = "requested";
  246. break;
  247. case DLM_RECO_NODE_DATA_DONE:
  248. st = "done";
  249. break;
  250. case DLM_RECO_NODE_DATA_FINALIZE_SENT:
  251. st = "finalize-sent";
  252. break;
  253. default:
  254. st = "bad";
  255. break;
  256. }
  257. mlog(ML_NOTICE, "%s: reco state, node %u, state=%s\n",
  258. dlm->name, ndata->node_num, st);
  259. }
  260. list_for_each_entry(res, &dlm->reco.resources, recovering) {
  261. mlog(ML_NOTICE, "%s: lockres %.*s on recovering list\n",
  262. dlm->name, res->lockname.len, res->lockname.name);
  263. }
  264. }
  265. #define DLM_RECO_THREAD_TIMEOUT_MS (5 * 1000)
  266. static int dlm_recovery_thread(void *data)
  267. {
  268. int status;
  269. struct dlm_ctxt *dlm = data;
  270. unsigned long timeout = msecs_to_jiffies(DLM_RECO_THREAD_TIMEOUT_MS);
  271. mlog(0, "dlm thread running for %s...\n", dlm->name);
  272. while (!kthread_should_stop()) {
  273. if (dlm_joined(dlm)) {
  274. status = dlm_do_recovery(dlm);
  275. if (status == -EAGAIN) {
  276. /* do not sleep, recheck immediately. */
  277. continue;
  278. }
  279. if (status < 0)
  280. mlog_errno(status);
  281. }
  282. wait_event_interruptible_timeout(dlm->dlm_reco_thread_wq,
  283. kthread_should_stop(),
  284. timeout);
  285. }
  286. mlog(0, "quitting DLM recovery thread\n");
  287. return 0;
  288. }
  289. /* returns true when the recovery master has contacted us */
  290. static int dlm_reco_master_ready(struct dlm_ctxt *dlm)
  291. {
  292. int ready;
  293. spin_lock(&dlm->spinlock);
  294. ready = (dlm->reco.new_master != O2NM_INVALID_NODE_NUM);
  295. spin_unlock(&dlm->spinlock);
  296. return ready;
  297. }
  298. /* returns true if node is no longer in the domain
  299. * could be dead or just not joined */
  300. int dlm_is_node_dead(struct dlm_ctxt *dlm, u8 node)
  301. {
  302. int dead;
  303. spin_lock(&dlm->spinlock);
  304. dead = !test_bit(node, dlm->domain_map);
  305. spin_unlock(&dlm->spinlock);
  306. return dead;
  307. }
  308. /* returns true if node is no longer in the domain
  309. * could be dead or just not joined */
  310. static int dlm_is_node_recovered(struct dlm_ctxt *dlm, u8 node)
  311. {
  312. int recovered;
  313. spin_lock(&dlm->spinlock);
  314. recovered = !test_bit(node, dlm->recovery_map);
  315. spin_unlock(&dlm->spinlock);
  316. return recovered;
  317. }
  318. int dlm_wait_for_node_death(struct dlm_ctxt *dlm, u8 node, int timeout)
  319. {
  320. if (timeout) {
  321. mlog(ML_NOTICE, "%s: waiting %dms for notification of "
  322. "death of node %u\n", dlm->name, timeout, node);
  323. wait_event_timeout(dlm->dlm_reco_thread_wq,
  324. dlm_is_node_dead(dlm, node),
  325. msecs_to_jiffies(timeout));
  326. } else {
  327. mlog(ML_NOTICE, "%s: waiting indefinitely for notification "
  328. "of death of node %u\n", dlm->name, node);
  329. wait_event(dlm->dlm_reco_thread_wq,
  330. dlm_is_node_dead(dlm, node));
  331. }
  332. /* for now, return 0 */
  333. return 0;
  334. }
  335. int dlm_wait_for_node_recovery(struct dlm_ctxt *dlm, u8 node, int timeout)
  336. {
  337. if (timeout) {
  338. mlog(0, "%s: waiting %dms for notification of "
  339. "recovery of node %u\n", dlm->name, timeout, node);
  340. wait_event_timeout(dlm->dlm_reco_thread_wq,
  341. dlm_is_node_recovered(dlm, node),
  342. msecs_to_jiffies(timeout));
  343. } else {
  344. mlog(0, "%s: waiting indefinitely for notification "
  345. "of recovery of node %u\n", dlm->name, node);
  346. wait_event(dlm->dlm_reco_thread_wq,
  347. dlm_is_node_recovered(dlm, node));
  348. }
  349. /* for now, return 0 */
  350. return 0;
  351. }
  352. /* callers of the top-level api calls (dlmlock/dlmunlock) should
  353. * block on the dlm->reco.event when recovery is in progress.
  354. * the dlm recovery thread will set this state when it begins
  355. * recovering a dead node (as the new master or not) and clear
  356. * the state and wake as soon as all affected lock resources have
  357. * been marked with the RECOVERY flag */
  358. static int dlm_in_recovery(struct dlm_ctxt *dlm)
  359. {
  360. int in_recovery;
  361. spin_lock(&dlm->spinlock);
  362. in_recovery = !!(dlm->reco.state & DLM_RECO_STATE_ACTIVE);
  363. spin_unlock(&dlm->spinlock);
  364. return in_recovery;
  365. }
  366. void dlm_wait_for_recovery(struct dlm_ctxt *dlm)
  367. {
  368. if (dlm_in_recovery(dlm)) {
  369. mlog(0, "%s: reco thread %d in recovery: "
  370. "state=%d, master=%u, dead=%u\n",
  371. dlm->name, task_pid_nr(dlm->dlm_reco_thread_task),
  372. dlm->reco.state, dlm->reco.new_master,
  373. dlm->reco.dead_node);
  374. }
  375. wait_event(dlm->reco.event, !dlm_in_recovery(dlm));
  376. }
  377. static void dlm_begin_recovery(struct dlm_ctxt *dlm)
  378. {
  379. spin_lock(&dlm->spinlock);
  380. BUG_ON(dlm->reco.state & DLM_RECO_STATE_ACTIVE);
  381. dlm->reco.state |= DLM_RECO_STATE_ACTIVE;
  382. spin_unlock(&dlm->spinlock);
  383. }
  384. static void dlm_end_recovery(struct dlm_ctxt *dlm)
  385. {
  386. spin_lock(&dlm->spinlock);
  387. BUG_ON(!(dlm->reco.state & DLM_RECO_STATE_ACTIVE));
  388. dlm->reco.state &= ~DLM_RECO_STATE_ACTIVE;
  389. spin_unlock(&dlm->spinlock);
  390. wake_up(&dlm->reco.event);
  391. }
  392. static int dlm_do_recovery(struct dlm_ctxt *dlm)
  393. {
  394. int status = 0;
  395. int ret;
  396. spin_lock(&dlm->spinlock);
  397. /* check to see if the new master has died */
  398. if (dlm->reco.new_master != O2NM_INVALID_NODE_NUM &&
  399. test_bit(dlm->reco.new_master, dlm->recovery_map)) {
  400. mlog(0, "new master %u died while recovering %u!\n",
  401. dlm->reco.new_master, dlm->reco.dead_node);
  402. /* unset the new_master, leave dead_node */
  403. dlm_set_reco_master(dlm, O2NM_INVALID_NODE_NUM);
  404. }
  405. /* select a target to recover */
  406. if (dlm->reco.dead_node == O2NM_INVALID_NODE_NUM) {
  407. int bit;
  408. bit = find_next_bit (dlm->recovery_map, O2NM_MAX_NODES+1, 0);
  409. if (bit >= O2NM_MAX_NODES || bit < 0)
  410. dlm_set_reco_dead_node(dlm, O2NM_INVALID_NODE_NUM);
  411. else
  412. dlm_set_reco_dead_node(dlm, bit);
  413. } else if (!test_bit(dlm->reco.dead_node, dlm->recovery_map)) {
  414. /* BUG? */
  415. mlog(ML_ERROR, "dead_node %u no longer in recovery map!\n",
  416. dlm->reco.dead_node);
  417. dlm_set_reco_dead_node(dlm, O2NM_INVALID_NODE_NUM);
  418. }
  419. if (dlm->reco.dead_node == O2NM_INVALID_NODE_NUM) {
  420. // mlog(0, "nothing to recover! sleeping now!\n");
  421. spin_unlock(&dlm->spinlock);
  422. /* return to main thread loop and sleep. */
  423. return 0;
  424. }
  425. mlog(0, "%s(%d):recovery thread found node %u in the recovery map!\n",
  426. dlm->name, task_pid_nr(dlm->dlm_reco_thread_task),
  427. dlm->reco.dead_node);
  428. spin_unlock(&dlm->spinlock);
  429. /* take write barrier */
  430. /* (stops the list reshuffling thread, proxy ast handling) */
  431. dlm_begin_recovery(dlm);
  432. if (dlm->reco.new_master == dlm->node_num)
  433. goto master_here;
  434. if (dlm->reco.new_master == O2NM_INVALID_NODE_NUM) {
  435. /* choose a new master, returns 0 if this node
  436. * is the master, -EEXIST if it's another node.
  437. * this does not return until a new master is chosen
  438. * or recovery completes entirely. */
  439. ret = dlm_pick_recovery_master(dlm);
  440. if (!ret) {
  441. /* already notified everyone. go. */
  442. goto master_here;
  443. }
  444. mlog(0, "another node will master this recovery session.\n");
  445. }
  446. mlog(0, "dlm=%s (%d), new_master=%u, this node=%u, dead_node=%u\n",
  447. dlm->name, task_pid_nr(dlm->dlm_reco_thread_task), dlm->reco.new_master,
  448. dlm->node_num, dlm->reco.dead_node);
  449. /* it is safe to start everything back up here
  450. * because all of the dead node's lock resources
  451. * have been marked as in-recovery */
  452. dlm_end_recovery(dlm);
  453. /* sleep out in main dlm_recovery_thread loop. */
  454. return 0;
  455. master_here:
  456. mlog(ML_NOTICE, "(%d) Node %u is the Recovery Master for the Dead Node "
  457. "%u for Domain %s\n", task_pid_nr(dlm->dlm_reco_thread_task),
  458. dlm->node_num, dlm->reco.dead_node, dlm->name);
  459. status = dlm_remaster_locks(dlm, dlm->reco.dead_node);
  460. if (status < 0) {
  461. /* we should never hit this anymore */
  462. mlog(ML_ERROR, "error %d remastering locks for node %u, "
  463. "retrying.\n", status, dlm->reco.dead_node);
  464. /* yield a bit to allow any final network messages
  465. * to get handled on remaining nodes */
  466. msleep(100);
  467. } else {
  468. /* success! see if any other nodes need recovery */
  469. mlog(0, "DONE mastering recovery of %s:%u here(this=%u)!\n",
  470. dlm->name, dlm->reco.dead_node, dlm->node_num);
  471. dlm_reset_recovery(dlm);
  472. }
  473. dlm_end_recovery(dlm);
  474. /* continue and look for another dead node */
  475. return -EAGAIN;
  476. }
  477. static int dlm_remaster_locks(struct dlm_ctxt *dlm, u8 dead_node)
  478. {
  479. int status = 0;
  480. struct dlm_reco_node_data *ndata;
  481. int all_nodes_done;
  482. int destroy = 0;
  483. int pass = 0;
  484. do {
  485. /* we have become recovery master. there is no escaping
  486. * this, so just keep trying until we get it. */
  487. status = dlm_init_recovery_area(dlm, dead_node);
  488. if (status < 0) {
  489. mlog(ML_ERROR, "%s: failed to alloc recovery area, "
  490. "retrying\n", dlm->name);
  491. msleep(1000);
  492. }
  493. } while (status != 0);
  494. /* safe to access the node data list without a lock, since this
  495. * process is the only one to change the list */
  496. list_for_each_entry(ndata, &dlm->reco.node_data, list) {
  497. BUG_ON(ndata->state != DLM_RECO_NODE_DATA_INIT);
  498. ndata->state = DLM_RECO_NODE_DATA_REQUESTING;
  499. mlog(0, "requesting lock info from node %u\n",
  500. ndata->node_num);
  501. if (ndata->node_num == dlm->node_num) {
  502. ndata->state = DLM_RECO_NODE_DATA_DONE;
  503. continue;
  504. }
  505. do {
  506. status = dlm_request_all_locks(dlm, ndata->node_num,
  507. dead_node);
  508. if (status < 0) {
  509. mlog_errno(status);
  510. if (dlm_is_host_down(status)) {
  511. /* node died, ignore it for recovery */
  512. status = 0;
  513. ndata->state = DLM_RECO_NODE_DATA_DEAD;
  514. /* wait for the domain map to catch up
  515. * with the network state. */
  516. wait_event_timeout(dlm->dlm_reco_thread_wq,
  517. dlm_is_node_dead(dlm,
  518. ndata->node_num),
  519. msecs_to_jiffies(1000));
  520. mlog(0, "waited 1 sec for %u, "
  521. "dead? %s\n", ndata->node_num,
  522. dlm_is_node_dead(dlm, ndata->node_num) ?
  523. "yes" : "no");
  524. } else {
  525. /* -ENOMEM on the other node */
  526. mlog(0, "%s: node %u returned "
  527. "%d during recovery, retrying "
  528. "after a short wait\n",
  529. dlm->name, ndata->node_num,
  530. status);
  531. msleep(100);
  532. }
  533. }
  534. } while (status != 0);
  535. spin_lock(&dlm_reco_state_lock);
  536. switch (ndata->state) {
  537. case DLM_RECO_NODE_DATA_INIT:
  538. case DLM_RECO_NODE_DATA_FINALIZE_SENT:
  539. case DLM_RECO_NODE_DATA_REQUESTED:
  540. BUG();
  541. break;
  542. case DLM_RECO_NODE_DATA_DEAD:
  543. mlog(0, "node %u died after requesting "
  544. "recovery info for node %u\n",
  545. ndata->node_num, dead_node);
  546. /* fine. don't need this node's info.
  547. * continue without it. */
  548. break;
  549. case DLM_RECO_NODE_DATA_REQUESTING:
  550. ndata->state = DLM_RECO_NODE_DATA_REQUESTED;
  551. mlog(0, "now receiving recovery data from "
  552. "node %u for dead node %u\n",
  553. ndata->node_num, dead_node);
  554. break;
  555. case DLM_RECO_NODE_DATA_RECEIVING:
  556. mlog(0, "already receiving recovery data from "
  557. "node %u for dead node %u\n",
  558. ndata->node_num, dead_node);
  559. break;
  560. case DLM_RECO_NODE_DATA_DONE:
  561. mlog(0, "already DONE receiving recovery data "
  562. "from node %u for dead node %u\n",
  563. ndata->node_num, dead_node);
  564. break;
  565. }
  566. spin_unlock(&dlm_reco_state_lock);
  567. }
  568. mlog(0, "done requesting all lock info\n");
  569. /* nodes should be sending reco data now
  570. * just need to wait */
  571. while (1) {
  572. /* check all the nodes now to see if we are
  573. * done, or if anyone died */
  574. all_nodes_done = 1;
  575. spin_lock(&dlm_reco_state_lock);
  576. list_for_each_entry(ndata, &dlm->reco.node_data, list) {
  577. mlog(0, "checking recovery state of node %u\n",
  578. ndata->node_num);
  579. switch (ndata->state) {
  580. case DLM_RECO_NODE_DATA_INIT:
  581. case DLM_RECO_NODE_DATA_REQUESTING:
  582. mlog(ML_ERROR, "bad ndata state for "
  583. "node %u: state=%d\n",
  584. ndata->node_num, ndata->state);
  585. BUG();
  586. break;
  587. case DLM_RECO_NODE_DATA_DEAD:
  588. mlog(0, "node %u died after "
  589. "requesting recovery info for "
  590. "node %u\n", ndata->node_num,
  591. dead_node);
  592. break;
  593. case DLM_RECO_NODE_DATA_RECEIVING:
  594. case DLM_RECO_NODE_DATA_REQUESTED:
  595. mlog(0, "%s: node %u still in state %s\n",
  596. dlm->name, ndata->node_num,
  597. ndata->state==DLM_RECO_NODE_DATA_RECEIVING ?
  598. "receiving" : "requested");
  599. all_nodes_done = 0;
  600. break;
  601. case DLM_RECO_NODE_DATA_DONE:
  602. mlog(0, "%s: node %u state is done\n",
  603. dlm->name, ndata->node_num);
  604. break;
  605. case DLM_RECO_NODE_DATA_FINALIZE_SENT:
  606. mlog(0, "%s: node %u state is finalize\n",
  607. dlm->name, ndata->node_num);
  608. break;
  609. }
  610. }
  611. spin_unlock(&dlm_reco_state_lock);
  612. mlog(0, "pass #%d, all_nodes_done?: %s\n", ++pass,
  613. all_nodes_done?"yes":"no");
  614. if (all_nodes_done) {
  615. int ret;
  616. /* all nodes are now in DLM_RECO_NODE_DATA_DONE state
  617. * just send a finalize message to everyone and
  618. * clean up */
  619. mlog(0, "all nodes are done! send finalize\n");
  620. ret = dlm_send_finalize_reco_message(dlm);
  621. if (ret < 0)
  622. mlog_errno(ret);
  623. spin_lock(&dlm->spinlock);
  624. dlm_finish_local_lockres_recovery(dlm, dead_node,
  625. dlm->node_num);
  626. spin_unlock(&dlm->spinlock);
  627. mlog(0, "should be done with recovery!\n");
  628. mlog(0, "finishing recovery of %s at %lu, "
  629. "dead=%u, this=%u, new=%u\n", dlm->name,
  630. jiffies, dlm->reco.dead_node,
  631. dlm->node_num, dlm->reco.new_master);
  632. destroy = 1;
  633. status = 0;
  634. /* rescan everything marked dirty along the way */
  635. dlm_kick_thread(dlm, NULL);
  636. break;
  637. }
  638. /* wait to be signalled, with periodic timeout
  639. * to check for node death */
  640. wait_event_interruptible_timeout(dlm->dlm_reco_thread_wq,
  641. kthread_should_stop(),
  642. msecs_to_jiffies(DLM_RECO_THREAD_TIMEOUT_MS));
  643. }
  644. if (destroy)
  645. dlm_destroy_recovery_area(dlm, dead_node);
  646. mlog_exit(status);
  647. return status;
  648. }
  649. static int dlm_init_recovery_area(struct dlm_ctxt *dlm, u8 dead_node)
  650. {
  651. int num=0;
  652. struct dlm_reco_node_data *ndata;
  653. spin_lock(&dlm->spinlock);
  654. memcpy(dlm->reco.node_map, dlm->domain_map, sizeof(dlm->domain_map));
  655. /* nodes can only be removed (by dying) after dropping
  656. * this lock, and death will be trapped later, so this should do */
  657. spin_unlock(&dlm->spinlock);
  658. while (1) {
  659. num = find_next_bit (dlm->reco.node_map, O2NM_MAX_NODES, num);
  660. if (num >= O2NM_MAX_NODES) {
  661. break;
  662. }
  663. BUG_ON(num == dead_node);
  664. ndata = kzalloc(sizeof(*ndata), GFP_NOFS);
  665. if (!ndata) {
  666. dlm_destroy_recovery_area(dlm, dead_node);
  667. return -ENOMEM;
  668. }
  669. ndata->node_num = num;
  670. ndata->state = DLM_RECO_NODE_DATA_INIT;
  671. spin_lock(&dlm_reco_state_lock);
  672. list_add_tail(&ndata->list, &dlm->reco.node_data);
  673. spin_unlock(&dlm_reco_state_lock);
  674. num++;
  675. }
  676. return 0;
  677. }
  678. static void dlm_destroy_recovery_area(struct dlm_ctxt *dlm, u8 dead_node)
  679. {
  680. struct dlm_reco_node_data *ndata, *next;
  681. LIST_HEAD(tmplist);
  682. spin_lock(&dlm_reco_state_lock);
  683. list_splice_init(&dlm->reco.node_data, &tmplist);
  684. spin_unlock(&dlm_reco_state_lock);
  685. list_for_each_entry_safe(ndata, next, &tmplist, list) {
  686. list_del_init(&ndata->list);
  687. kfree(ndata);
  688. }
  689. }
  690. static int dlm_request_all_locks(struct dlm_ctxt *dlm, u8 request_from,
  691. u8 dead_node)
  692. {
  693. struct dlm_lock_request lr;
  694. enum dlm_status ret;
  695. mlog(0, "\n");
  696. mlog(0, "dlm_request_all_locks: dead node is %u, sending request "
  697. "to %u\n", dead_node, request_from);
  698. memset(&lr, 0, sizeof(lr));
  699. lr.node_idx = dlm->node_num;
  700. lr.dead_node = dead_node;
  701. // send message
  702. ret = DLM_NOLOCKMGR;
  703. ret = o2net_send_message(DLM_LOCK_REQUEST_MSG, dlm->key,
  704. &lr, sizeof(lr), request_from, NULL);
  705. /* negative status is handled by caller */
  706. if (ret < 0)
  707. mlog_errno(ret);
  708. // return from here, then
  709. // sleep until all received or error
  710. return ret;
  711. }
  712. int dlm_request_all_locks_handler(struct o2net_msg *msg, u32 len, void *data,
  713. void **ret_data)
  714. {
  715. struct dlm_ctxt *dlm = data;
  716. struct dlm_lock_request *lr = (struct dlm_lock_request *)msg->buf;
  717. char *buf = NULL;
  718. struct dlm_work_item *item = NULL;
  719. if (!dlm_grab(dlm))
  720. return -EINVAL;
  721. if (lr->dead_node != dlm->reco.dead_node) {
  722. mlog(ML_ERROR, "%s: node %u sent dead_node=%u, but local "
  723. "dead_node is %u\n", dlm->name, lr->node_idx,
  724. lr->dead_node, dlm->reco.dead_node);
  725. dlm_print_reco_node_status(dlm);
  726. /* this is a hack */
  727. dlm_put(dlm);
  728. return -ENOMEM;
  729. }
  730. BUG_ON(lr->dead_node != dlm->reco.dead_node);
  731. item = kzalloc(sizeof(*item), GFP_NOFS);
  732. if (!item) {
  733. dlm_put(dlm);
  734. return -ENOMEM;
  735. }
  736. /* this will get freed by dlm_request_all_locks_worker */
  737. buf = (char *) __get_free_page(GFP_NOFS);
  738. if (!buf) {
  739. kfree(item);
  740. dlm_put(dlm);
  741. return -ENOMEM;
  742. }
  743. /* queue up work for dlm_request_all_locks_worker */
  744. dlm_grab(dlm); /* get an extra ref for the work item */
  745. dlm_init_work_item(dlm, item, dlm_request_all_locks_worker, buf);
  746. item->u.ral.reco_master = lr->node_idx;
  747. item->u.ral.dead_node = lr->dead_node;
  748. spin_lock(&dlm->work_lock);
  749. list_add_tail(&item->list, &dlm->work_list);
  750. spin_unlock(&dlm->work_lock);
  751. queue_work(dlm->dlm_worker, &dlm->dispatched_work);
  752. dlm_put(dlm);
  753. return 0;
  754. }
  755. static void dlm_request_all_locks_worker(struct dlm_work_item *item, void *data)
  756. {
  757. struct dlm_migratable_lockres *mres;
  758. struct dlm_lock_resource *res;
  759. struct dlm_ctxt *dlm;
  760. LIST_HEAD(resources);
  761. int ret;
  762. u8 dead_node, reco_master;
  763. int skip_all_done = 0;
  764. dlm = item->dlm;
  765. dead_node = item->u.ral.dead_node;
  766. reco_master = item->u.ral.reco_master;
  767. mres = (struct dlm_migratable_lockres *)data;
  768. mlog(0, "%s: recovery worker started, dead=%u, master=%u\n",
  769. dlm->name, dead_node, reco_master);
  770. if (dead_node != dlm->reco.dead_node ||
  771. reco_master != dlm->reco.new_master) {
  772. /* worker could have been created before the recovery master
  773. * died. if so, do not continue, but do not error. */
  774. if (dlm->reco.new_master == O2NM_INVALID_NODE_NUM) {
  775. mlog(ML_NOTICE, "%s: will not send recovery state, "
  776. "recovery master %u died, thread=(dead=%u,mas=%u)"
  777. " current=(dead=%u,mas=%u)\n", dlm->name,
  778. reco_master, dead_node, reco_master,
  779. dlm->reco.dead_node, dlm->reco.new_master);
  780. } else {
  781. mlog(ML_NOTICE, "%s: reco state invalid: reco(dead=%u, "
  782. "master=%u), request(dead=%u, master=%u)\n",
  783. dlm->name, dlm->reco.dead_node,
  784. dlm->reco.new_master, dead_node, reco_master);
  785. }
  786. goto leave;
  787. }
  788. /* lock resources should have already been moved to the
  789. * dlm->reco.resources list. now move items from that list
  790. * to a temp list if the dead owner matches. note that the
  791. * whole cluster recovers only one node at a time, so we
  792. * can safely move UNKNOWN lock resources for each recovery
  793. * session. */
  794. dlm_move_reco_locks_to_list(dlm, &resources, dead_node);
  795. /* now we can begin blasting lockreses without the dlm lock */
  796. /* any errors returned will be due to the new_master dying,
  797. * the dlm_reco_thread should detect this */
  798. list_for_each_entry(res, &resources, recovering) {
  799. ret = dlm_send_one_lockres(dlm, res, mres, reco_master,
  800. DLM_MRES_RECOVERY);
  801. if (ret < 0) {
  802. mlog(ML_ERROR, "%s: node %u went down while sending "
  803. "recovery state for dead node %u, ret=%d\n", dlm->name,
  804. reco_master, dead_node, ret);
  805. skip_all_done = 1;
  806. break;
  807. }
  808. }
  809. /* move the resources back to the list */
  810. spin_lock(&dlm->spinlock);
  811. list_splice_init(&resources, &dlm->reco.resources);
  812. spin_unlock(&dlm->spinlock);
  813. if (!skip_all_done) {
  814. ret = dlm_send_all_done_msg(dlm, dead_node, reco_master);
  815. if (ret < 0) {
  816. mlog(ML_ERROR, "%s: node %u went down while sending "
  817. "recovery all-done for dead node %u, ret=%d\n",
  818. dlm->name, reco_master, dead_node, ret);
  819. }
  820. }
  821. leave:
  822. free_page((unsigned long)data);
  823. }
  824. static int dlm_send_all_done_msg(struct dlm_ctxt *dlm, u8 dead_node, u8 send_to)
  825. {
  826. int ret, tmpret;
  827. struct dlm_reco_data_done done_msg;
  828. memset(&done_msg, 0, sizeof(done_msg));
  829. done_msg.node_idx = dlm->node_num;
  830. done_msg.dead_node = dead_node;
  831. mlog(0, "sending DATA DONE message to %u, "
  832. "my node=%u, dead node=%u\n", send_to, done_msg.node_idx,
  833. done_msg.dead_node);
  834. ret = o2net_send_message(DLM_RECO_DATA_DONE_MSG, dlm->key, &done_msg,
  835. sizeof(done_msg), send_to, &tmpret);
  836. if (ret < 0) {
  837. if (!dlm_is_host_down(ret)) {
  838. mlog_errno(ret);
  839. mlog(ML_ERROR, "%s: unknown error sending data-done "
  840. "to %u\n", dlm->name, send_to);
  841. BUG();
  842. }
  843. } else
  844. ret = tmpret;
  845. return ret;
  846. }
  847. int dlm_reco_data_done_handler(struct o2net_msg *msg, u32 len, void *data,
  848. void **ret_data)
  849. {
  850. struct dlm_ctxt *dlm = data;
  851. struct dlm_reco_data_done *done = (struct dlm_reco_data_done *)msg->buf;
  852. struct dlm_reco_node_data *ndata = NULL;
  853. int ret = -EINVAL;
  854. if (!dlm_grab(dlm))
  855. return -EINVAL;
  856. mlog(0, "got DATA DONE: dead_node=%u, reco.dead_node=%u, "
  857. "node_idx=%u, this node=%u\n", done->dead_node,
  858. dlm->reco.dead_node, done->node_idx, dlm->node_num);
  859. mlog_bug_on_msg((done->dead_node != dlm->reco.dead_node),
  860. "Got DATA DONE: dead_node=%u, reco.dead_node=%u, "
  861. "node_idx=%u, this node=%u\n", done->dead_node,
  862. dlm->reco.dead_node, done->node_idx, dlm->node_num);
  863. spin_lock(&dlm_reco_state_lock);
  864. list_for_each_entry(ndata, &dlm->reco.node_data, list) {
  865. if (ndata->node_num != done->node_idx)
  866. continue;
  867. switch (ndata->state) {
  868. /* should have moved beyond INIT but not to FINALIZE yet */
  869. case DLM_RECO_NODE_DATA_INIT:
  870. case DLM_RECO_NODE_DATA_DEAD:
  871. case DLM_RECO_NODE_DATA_FINALIZE_SENT:
  872. mlog(ML_ERROR, "bad ndata state for node %u:"
  873. " state=%d\n", ndata->node_num,
  874. ndata->state);
  875. BUG();
  876. break;
  877. /* these states are possible at this point, anywhere along
  878. * the line of recovery */
  879. case DLM_RECO_NODE_DATA_DONE:
  880. case DLM_RECO_NODE_DATA_RECEIVING:
  881. case DLM_RECO_NODE_DATA_REQUESTED:
  882. case DLM_RECO_NODE_DATA_REQUESTING:
  883. mlog(0, "node %u is DONE sending "
  884. "recovery data!\n",
  885. ndata->node_num);
  886. ndata->state = DLM_RECO_NODE_DATA_DONE;
  887. ret = 0;
  888. break;
  889. }
  890. }
  891. spin_unlock(&dlm_reco_state_lock);
  892. /* wake the recovery thread, some node is done */
  893. if (!ret)
  894. dlm_kick_recovery_thread(dlm);
  895. if (ret < 0)
  896. mlog(ML_ERROR, "failed to find recovery node data for node "
  897. "%u\n", done->node_idx);
  898. dlm_put(dlm);
  899. mlog(0, "leaving reco data done handler, ret=%d\n", ret);
  900. return ret;
  901. }
  902. static void dlm_move_reco_locks_to_list(struct dlm_ctxt *dlm,
  903. struct list_head *list,
  904. u8 dead_node)
  905. {
  906. struct dlm_lock_resource *res, *next;
  907. struct dlm_lock *lock;
  908. spin_lock(&dlm->spinlock);
  909. list_for_each_entry_safe(res, next, &dlm->reco.resources, recovering) {
  910. /* always prune any $RECOVERY entries for dead nodes,
  911. * otherwise hangs can occur during later recovery */
  912. if (dlm_is_recovery_lock(res->lockname.name,
  913. res->lockname.len)) {
  914. spin_lock(&res->spinlock);
  915. list_for_each_entry(lock, &res->granted, list) {
  916. if (lock->ml.node == dead_node) {
  917. mlog(0, "AHA! there was "
  918. "a $RECOVERY lock for dead "
  919. "node %u (%s)!\n",
  920. dead_node, dlm->name);
  921. list_del_init(&lock->list);
  922. dlm_lock_put(lock);
  923. break;
  924. }
  925. }
  926. spin_unlock(&res->spinlock);
  927. continue;
  928. }
  929. if (res->owner == dead_node) {
  930. mlog(0, "found lockres owned by dead node while "
  931. "doing recovery for node %u. sending it.\n",
  932. dead_node);
  933. list_move_tail(&res->recovering, list);
  934. } else if (res->owner == DLM_LOCK_RES_OWNER_UNKNOWN) {
  935. mlog(0, "found UNKNOWN owner while doing recovery "
  936. "for node %u. sending it.\n", dead_node);
  937. list_move_tail(&res->recovering, list);
  938. }
  939. }
  940. spin_unlock(&dlm->spinlock);
  941. }
  942. static inline int dlm_num_locks_in_lockres(struct dlm_lock_resource *res)
  943. {
  944. int total_locks = 0;
  945. struct list_head *iter, *queue = &res->granted;
  946. int i;
  947. for (i=0; i<3; i++) {
  948. list_for_each(iter, queue)
  949. total_locks++;
  950. queue++;
  951. }
  952. return total_locks;
  953. }
  954. static int dlm_send_mig_lockres_msg(struct dlm_ctxt *dlm,
  955. struct dlm_migratable_lockres *mres,
  956. u8 send_to,
  957. struct dlm_lock_resource *res,
  958. int total_locks)
  959. {
  960. u64 mig_cookie = be64_to_cpu(mres->mig_cookie);
  961. int mres_total_locks = be32_to_cpu(mres->total_locks);
  962. int sz, ret = 0, status = 0;
  963. u8 orig_flags = mres->flags,
  964. orig_master = mres->master;
  965. BUG_ON(mres->num_locks > DLM_MAX_MIGRATABLE_LOCKS);
  966. if (!mres->num_locks)
  967. return 0;
  968. sz = sizeof(struct dlm_migratable_lockres) +
  969. (mres->num_locks * sizeof(struct dlm_migratable_lock));
  970. /* add an all-done flag if we reached the last lock */
  971. orig_flags = mres->flags;
  972. BUG_ON(total_locks > mres_total_locks);
  973. if (total_locks == mres_total_locks)
  974. mres->flags |= DLM_MRES_ALL_DONE;
  975. mlog(0, "%s:%.*s: sending mig lockres (%s) to %u\n",
  976. dlm->name, res->lockname.len, res->lockname.name,
  977. orig_flags & DLM_MRES_MIGRATION ? "migrate" : "recovery",
  978. send_to);
  979. /* send it */
  980. ret = o2net_send_message(DLM_MIG_LOCKRES_MSG, dlm->key, mres,
  981. sz, send_to, &status);
  982. if (ret < 0) {
  983. /* XXX: negative status is not handled.
  984. * this will end up killing this node. */
  985. mlog_errno(ret);
  986. } else {
  987. /* might get an -ENOMEM back here */
  988. ret = status;
  989. if (ret < 0) {
  990. mlog_errno(ret);
  991. if (ret == -EFAULT) {
  992. mlog(ML_ERROR, "node %u told me to kill "
  993. "myself!\n", send_to);
  994. BUG();
  995. }
  996. }
  997. }
  998. /* zero and reinit the message buffer */
  999. dlm_init_migratable_lockres(mres, res->lockname.name,
  1000. res->lockname.len, mres_total_locks,
  1001. mig_cookie, orig_flags, orig_master);
  1002. return ret;
  1003. }
  1004. static void dlm_init_migratable_lockres(struct dlm_migratable_lockres *mres,
  1005. const char *lockname, int namelen,
  1006. int total_locks, u64 cookie,
  1007. u8 flags, u8 master)
  1008. {
  1009. /* mres here is one full page */
  1010. clear_page(mres);
  1011. mres->lockname_len = namelen;
  1012. memcpy(mres->lockname, lockname, namelen);
  1013. mres->num_locks = 0;
  1014. mres->total_locks = cpu_to_be32(total_locks);
  1015. mres->mig_cookie = cpu_to_be64(cookie);
  1016. mres->flags = flags;
  1017. mres->master = master;
  1018. }
  1019. /* returns 1 if this lock fills the network structure,
  1020. * 0 otherwise */
  1021. static int dlm_add_lock_to_array(struct dlm_lock *lock,
  1022. struct dlm_migratable_lockres *mres, int queue)
  1023. {
  1024. struct dlm_migratable_lock *ml;
  1025. int lock_num = mres->num_locks;
  1026. ml = &(mres->ml[lock_num]);
  1027. ml->cookie = lock->ml.cookie;
  1028. ml->type = lock->ml.type;
  1029. ml->convert_type = lock->ml.convert_type;
  1030. ml->highest_blocked = lock->ml.highest_blocked;
  1031. ml->list = queue;
  1032. if (lock->lksb) {
  1033. ml->flags = lock->lksb->flags;
  1034. /* send our current lvb */
  1035. if (ml->type == LKM_EXMODE ||
  1036. ml->type == LKM_PRMODE) {
  1037. /* if it is already set, this had better be a PR
  1038. * and it has to match */
  1039. if (!dlm_lvb_is_empty(mres->lvb) &&
  1040. (ml->type == LKM_EXMODE ||
  1041. memcmp(mres->lvb, lock->lksb->lvb, DLM_LVB_LEN))) {
  1042. mlog(ML_ERROR, "mismatched lvbs!\n");
  1043. dlm_print_one_lock_resource(lock->lockres);
  1044. BUG();
  1045. }
  1046. memcpy(mres->lvb, lock->lksb->lvb, DLM_LVB_LEN);
  1047. }
  1048. }
  1049. ml->node = lock->ml.node;
  1050. mres->num_locks++;
  1051. /* we reached the max, send this network message */
  1052. if (mres->num_locks == DLM_MAX_MIGRATABLE_LOCKS)
  1053. return 1;
  1054. return 0;
  1055. }
  1056. static void dlm_add_dummy_lock(struct dlm_ctxt *dlm,
  1057. struct dlm_migratable_lockres *mres)
  1058. {
  1059. struct dlm_lock dummy;
  1060. memset(&dummy, 0, sizeof(dummy));
  1061. dummy.ml.cookie = 0;
  1062. dummy.ml.type = LKM_IVMODE;
  1063. dummy.ml.convert_type = LKM_IVMODE;
  1064. dummy.ml.highest_blocked = LKM_IVMODE;
  1065. dummy.lksb = NULL;
  1066. dummy.ml.node = dlm->node_num;
  1067. dlm_add_lock_to_array(&dummy, mres, DLM_BLOCKED_LIST);
  1068. }
  1069. static inline int dlm_is_dummy_lock(struct dlm_ctxt *dlm,
  1070. struct dlm_migratable_lock *ml,
  1071. u8 *nodenum)
  1072. {
  1073. if (unlikely(ml->cookie == 0 &&
  1074. ml->type == LKM_IVMODE &&
  1075. ml->convert_type == LKM_IVMODE &&
  1076. ml->highest_blocked == LKM_IVMODE &&
  1077. ml->list == DLM_BLOCKED_LIST)) {
  1078. *nodenum = ml->node;
  1079. return 1;
  1080. }
  1081. return 0;
  1082. }
  1083. int dlm_send_one_lockres(struct dlm_ctxt *dlm, struct dlm_lock_resource *res,
  1084. struct dlm_migratable_lockres *mres,
  1085. u8 send_to, u8 flags)
  1086. {
  1087. struct list_head *queue;
  1088. int total_locks, i;
  1089. u64 mig_cookie = 0;
  1090. struct dlm_lock *lock;
  1091. int ret = 0;
  1092. BUG_ON(!(flags & (DLM_MRES_RECOVERY|DLM_MRES_MIGRATION)));
  1093. mlog(0, "sending to %u\n", send_to);
  1094. total_locks = dlm_num_locks_in_lockres(res);
  1095. if (total_locks > DLM_MAX_MIGRATABLE_LOCKS) {
  1096. /* rare, but possible */
  1097. mlog(0, "argh. lockres has %d locks. this will "
  1098. "require more than one network packet to "
  1099. "migrate\n", total_locks);
  1100. mig_cookie = dlm_get_next_mig_cookie();
  1101. }
  1102. dlm_init_migratable_lockres(mres, res->lockname.name,
  1103. res->lockname.len, total_locks,
  1104. mig_cookie, flags, res->owner);
  1105. total_locks = 0;
  1106. for (i=DLM_GRANTED_LIST; i<=DLM_BLOCKED_LIST; i++) {
  1107. queue = dlm_list_idx_to_ptr(res, i);
  1108. list_for_each_entry(lock, queue, list) {
  1109. /* add another lock. */
  1110. total_locks++;
  1111. if (!dlm_add_lock_to_array(lock, mres, i))
  1112. continue;
  1113. /* this filled the lock message,
  1114. * we must send it immediately. */
  1115. ret = dlm_send_mig_lockres_msg(dlm, mres, send_to,
  1116. res, total_locks);
  1117. if (ret < 0)
  1118. goto error;
  1119. }
  1120. }
  1121. if (total_locks == 0) {
  1122. /* send a dummy lock to indicate a mastery reference only */
  1123. mlog(0, "%s:%.*s: sending dummy lock to %u, %s\n",
  1124. dlm->name, res->lockname.len, res->lockname.name,
  1125. send_to, flags & DLM_MRES_RECOVERY ? "recovery" :
  1126. "migration");
  1127. dlm_add_dummy_lock(dlm, mres);
  1128. }
  1129. /* flush any remaining locks */
  1130. ret = dlm_send_mig_lockres_msg(dlm, mres, send_to, res, total_locks);
  1131. if (ret < 0)
  1132. goto error;
  1133. return ret;
  1134. error:
  1135. mlog(ML_ERROR, "%s: dlm_send_mig_lockres_msg returned %d\n",
  1136. dlm->name, ret);
  1137. if (!dlm_is_host_down(ret))
  1138. BUG();
  1139. mlog(0, "%s: node %u went down while sending %s "
  1140. "lockres %.*s\n", dlm->name, send_to,
  1141. flags & DLM_MRES_RECOVERY ? "recovery" : "migration",
  1142. res->lockname.len, res->lockname.name);
  1143. return ret;
  1144. }
  1145. /*
  1146. * this message will contain no more than one page worth of
  1147. * recovery data, and it will work on only one lockres.
  1148. * there may be many locks in this page, and we may need to wait
  1149. * for additional packets to complete all the locks (rare, but
  1150. * possible).
  1151. */
  1152. /*
  1153. * NOTE: the allocation error cases here are scary
  1154. * we really cannot afford to fail an alloc in recovery
  1155. * do we spin? returning an error only delays the problem really
  1156. */
  1157. int dlm_mig_lockres_handler(struct o2net_msg *msg, u32 len, void *data,
  1158. void **ret_data)
  1159. {
  1160. struct dlm_ctxt *dlm = data;
  1161. struct dlm_migratable_lockres *mres =
  1162. (struct dlm_migratable_lockres *)msg->buf;
  1163. int ret = 0;
  1164. u8 real_master;
  1165. u8 extra_refs = 0;
  1166. char *buf = NULL;
  1167. struct dlm_work_item *item = NULL;
  1168. struct dlm_lock_resource *res = NULL;
  1169. if (!dlm_grab(dlm))
  1170. return -EINVAL;
  1171. BUG_ON(!(mres->flags & (DLM_MRES_RECOVERY|DLM_MRES_MIGRATION)));
  1172. real_master = mres->master;
  1173. if (real_master == DLM_LOCK_RES_OWNER_UNKNOWN) {
  1174. /* cannot migrate a lockres with no master */
  1175. BUG_ON(!(mres->flags & DLM_MRES_RECOVERY));
  1176. }
  1177. mlog(0, "%s message received from node %u\n",
  1178. (mres->flags & DLM_MRES_RECOVERY) ?
  1179. "recovery" : "migration", mres->master);
  1180. if (mres->flags & DLM_MRES_ALL_DONE)
  1181. mlog(0, "all done flag. all lockres data received!\n");
  1182. ret = -ENOMEM;
  1183. buf = kmalloc(be16_to_cpu(msg->data_len), GFP_NOFS);
  1184. item = kzalloc(sizeof(*item), GFP_NOFS);
  1185. if (!buf || !item)
  1186. goto leave;
  1187. /* lookup the lock to see if we have a secondary queue for this
  1188. * already... just add the locks in and this will have its owner
  1189. * and RECOVERY flag changed when it completes. */
  1190. res = dlm_lookup_lockres(dlm, mres->lockname, mres->lockname_len);
  1191. if (res) {
  1192. /* this will get a ref on res */
  1193. /* mark it as recovering/migrating and hash it */
  1194. spin_lock(&res->spinlock);
  1195. if (mres->flags & DLM_MRES_RECOVERY) {
  1196. res->state |= DLM_LOCK_RES_RECOVERING;
  1197. } else {
  1198. if (res->state & DLM_LOCK_RES_MIGRATING) {
  1199. /* this is at least the second
  1200. * lockres message */
  1201. mlog(0, "lock %.*s is already migrating\n",
  1202. mres->lockname_len,
  1203. mres->lockname);
  1204. } else if (res->state & DLM_LOCK_RES_RECOVERING) {
  1205. /* caller should BUG */
  1206. mlog(ML_ERROR, "node is attempting to migrate "
  1207. "lock %.*s, but marked as recovering!\n",
  1208. mres->lockname_len, mres->lockname);
  1209. ret = -EFAULT;
  1210. spin_unlock(&res->spinlock);
  1211. goto leave;
  1212. }
  1213. res->state |= DLM_LOCK_RES_MIGRATING;
  1214. }
  1215. spin_unlock(&res->spinlock);
  1216. } else {
  1217. /* need to allocate, just like if it was
  1218. * mastered here normally */
  1219. res = dlm_new_lockres(dlm, mres->lockname, mres->lockname_len);
  1220. if (!res)
  1221. goto leave;
  1222. /* to match the ref that we would have gotten if
  1223. * dlm_lookup_lockres had succeeded */
  1224. dlm_lockres_get(res);
  1225. /* mark it as recovering/migrating and hash it */
  1226. if (mres->flags & DLM_MRES_RECOVERY)
  1227. res->state |= DLM_LOCK_RES_RECOVERING;
  1228. else
  1229. res->state |= DLM_LOCK_RES_MIGRATING;
  1230. spin_lock(&dlm->spinlock);
  1231. __dlm_insert_lockres(dlm, res);
  1232. spin_unlock(&dlm->spinlock);
  1233. /* Add an extra ref for this lock-less lockres lest the
  1234. * dlm_thread purges it before we get the chance to add
  1235. * locks to it */
  1236. dlm_lockres_get(res);
  1237. /* There are three refs that need to be put.
  1238. * 1. Taken above.
  1239. * 2. kref_init in dlm_new_lockres()->dlm_init_lockres().
  1240. * 3. dlm_lookup_lockres()
  1241. * The first one is handled at the end of this function. The
  1242. * other two are handled in the worker thread after locks have
  1243. * been attached. Yes, we don't wait for purge time to match
  1244. * kref_init. The lockres will still have atleast one ref
  1245. * added because it is in the hash __dlm_insert_lockres() */
  1246. extra_refs++;
  1247. /* now that the new lockres is inserted,
  1248. * make it usable by other processes */
  1249. spin_lock(&res->spinlock);
  1250. res->state &= ~DLM_LOCK_RES_IN_PROGRESS;
  1251. spin_unlock(&res->spinlock);
  1252. wake_up(&res->wq);
  1253. }
  1254. /* at this point we have allocated everything we need,
  1255. * and we have a hashed lockres with an extra ref and
  1256. * the proper res->state flags. */
  1257. ret = 0;
  1258. spin_lock(&res->spinlock);
  1259. /* drop this either when master requery finds a different master
  1260. * or when a lock is added by the recovery worker */
  1261. dlm_lockres_grab_inflight_ref(dlm, res);
  1262. if (mres->master == DLM_LOCK_RES_OWNER_UNKNOWN) {
  1263. /* migration cannot have an unknown master */
  1264. BUG_ON(!(mres->flags & DLM_MRES_RECOVERY));
  1265. mlog(0, "recovery has passed me a lockres with an "
  1266. "unknown owner.. will need to requery: "
  1267. "%.*s\n", mres->lockname_len, mres->lockname);
  1268. } else {
  1269. /* take a reference now to pin the lockres, drop it
  1270. * when locks are added in the worker */
  1271. dlm_change_lockres_owner(dlm, res, dlm->node_num);
  1272. }
  1273. spin_unlock(&res->spinlock);
  1274. /* queue up work for dlm_mig_lockres_worker */
  1275. dlm_grab(dlm); /* get an extra ref for the work item */
  1276. memcpy(buf, msg->buf, be16_to_cpu(msg->data_len)); /* copy the whole message */
  1277. dlm_init_work_item(dlm, item, dlm_mig_lockres_worker, buf);
  1278. item->u.ml.lockres = res; /* already have a ref */
  1279. item->u.ml.real_master = real_master;
  1280. item->u.ml.extra_ref = extra_refs;
  1281. spin_lock(&dlm->work_lock);
  1282. list_add_tail(&item->list, &dlm->work_list);
  1283. spin_unlock(&dlm->work_lock);
  1284. queue_work(dlm->dlm_worker, &dlm->dispatched_work);
  1285. leave:
  1286. /* One extra ref taken needs to be put here */
  1287. if (extra_refs)
  1288. dlm_lockres_put(res);
  1289. dlm_put(dlm);
  1290. if (ret < 0) {
  1291. if (buf)
  1292. kfree(buf);
  1293. if (item)
  1294. kfree(item);
  1295. }
  1296. mlog_exit(ret);
  1297. return ret;
  1298. }
  1299. static void dlm_mig_lockres_worker(struct dlm_work_item *item, void *data)
  1300. {
  1301. struct dlm_ctxt *dlm;
  1302. struct dlm_migratable_lockres *mres;
  1303. int ret = 0;
  1304. struct dlm_lock_resource *res;
  1305. u8 real_master;
  1306. u8 extra_ref;
  1307. dlm = item->dlm;
  1308. mres = (struct dlm_migratable_lockres *)data;
  1309. res = item->u.ml.lockres;
  1310. real_master = item->u.ml.real_master;
  1311. extra_ref = item->u.ml.extra_ref;
  1312. if (real_master == DLM_LOCK_RES_OWNER_UNKNOWN) {
  1313. /* this case is super-rare. only occurs if
  1314. * node death happens during migration. */
  1315. again:
  1316. ret = dlm_lockres_master_requery(dlm, res, &real_master);
  1317. if (ret < 0) {
  1318. mlog(0, "dlm_lockres_master_requery ret=%d\n",
  1319. ret);
  1320. goto again;
  1321. }
  1322. if (real_master == DLM_LOCK_RES_OWNER_UNKNOWN) {
  1323. mlog(0, "lockres %.*s not claimed. "
  1324. "this node will take it.\n",
  1325. res->lockname.len, res->lockname.name);
  1326. } else {
  1327. spin_lock(&res->spinlock);
  1328. dlm_lockres_drop_inflight_ref(dlm, res);
  1329. spin_unlock(&res->spinlock);
  1330. mlog(0, "master needs to respond to sender "
  1331. "that node %u still owns %.*s\n",
  1332. real_master, res->lockname.len,
  1333. res->lockname.name);
  1334. /* cannot touch this lockres */
  1335. goto leave;
  1336. }
  1337. }
  1338. ret = dlm_process_recovery_data(dlm, res, mres);
  1339. if (ret < 0)
  1340. mlog(0, "dlm_process_recovery_data returned %d\n", ret);
  1341. else
  1342. mlog(0, "dlm_process_recovery_data succeeded\n");
  1343. if ((mres->flags & (DLM_MRES_MIGRATION|DLM_MRES_ALL_DONE)) ==
  1344. (DLM_MRES_MIGRATION|DLM_MRES_ALL_DONE)) {
  1345. ret = dlm_finish_migration(dlm, res, mres->master);
  1346. if (ret < 0)
  1347. mlog_errno(ret);
  1348. }
  1349. leave:
  1350. /* See comment in dlm_mig_lockres_handler() */
  1351. if (res) {
  1352. if (extra_ref)
  1353. dlm_lockres_put(res);
  1354. dlm_lockres_put(res);
  1355. }
  1356. kfree(data);
  1357. mlog_exit(ret);
  1358. }
  1359. static int dlm_lockres_master_requery(struct dlm_ctxt *dlm,
  1360. struct dlm_lock_resource *res,
  1361. u8 *real_master)
  1362. {
  1363. struct dlm_node_iter iter;
  1364. int nodenum;
  1365. int ret = 0;
  1366. *real_master = DLM_LOCK_RES_OWNER_UNKNOWN;
  1367. /* we only reach here if one of the two nodes in a
  1368. * migration died while the migration was in progress.
  1369. * at this point we need to requery the master. we
  1370. * know that the new_master got as far as creating
  1371. * an mle on at least one node, but we do not know
  1372. * if any nodes had actually cleared the mle and set
  1373. * the master to the new_master. the old master
  1374. * is supposed to set the owner to UNKNOWN in the
  1375. * event of a new_master death, so the only possible
  1376. * responses that we can get from nodes here are
  1377. * that the master is new_master, or that the master
  1378. * is UNKNOWN.
  1379. * if all nodes come back with UNKNOWN then we know
  1380. * the lock needs remastering here.
  1381. * if any node comes back with a valid master, check
  1382. * to see if that master is the one that we are
  1383. * recovering. if so, then the new_master died and
  1384. * we need to remaster this lock. if not, then the
  1385. * new_master survived and that node will respond to
  1386. * other nodes about the owner.
  1387. * if there is an owner, this node needs to dump this
  1388. * lockres and alert the sender that this lockres
  1389. * was rejected. */
  1390. spin_lock(&dlm->spinlock);
  1391. dlm_node_iter_init(dlm->domain_map, &iter);
  1392. spin_unlock(&dlm->spinlock);
  1393. while ((nodenum = dlm_node_iter_next(&iter)) >= 0) {
  1394. /* do not send to self */
  1395. if (nodenum == dlm->node_num)
  1396. continue;
  1397. ret = dlm_do_master_requery(dlm, res, nodenum, real_master);
  1398. if (ret < 0) {
  1399. mlog_errno(ret);
  1400. if (!dlm_is_host_down(ret))
  1401. BUG();
  1402. /* host is down, so answer for that node would be
  1403. * DLM_LOCK_RES_OWNER_UNKNOWN. continue. */
  1404. }
  1405. if (*real_master != DLM_LOCK_RES_OWNER_UNKNOWN) {
  1406. mlog(0, "lock master is %u\n", *real_master);
  1407. break;
  1408. }
  1409. }
  1410. return ret;
  1411. }
  1412. int dlm_do_master_requery(struct dlm_ctxt *dlm, struct dlm_lock_resource *res,
  1413. u8 nodenum, u8 *real_master)
  1414. {
  1415. int ret = -EINVAL;
  1416. struct dlm_master_requery req;
  1417. int status = DLM_LOCK_RES_OWNER_UNKNOWN;
  1418. memset(&req, 0, sizeof(req));
  1419. req.node_idx = dlm->node_num;
  1420. req.namelen = res->lockname.len;
  1421. memcpy(req.name, res->lockname.name, res->lockname.len);
  1422. ret = o2net_send_message(DLM_MASTER_REQUERY_MSG, dlm->key,
  1423. &req, sizeof(req), nodenum, &status);
  1424. /* XXX: negative status not handled properly here. */
  1425. if (ret < 0)
  1426. mlog_errno(ret);
  1427. else {
  1428. BUG_ON(status < 0);
  1429. BUG_ON(status > DLM_LOCK_RES_OWNER_UNKNOWN);
  1430. *real_master = (u8) (status & 0xff);
  1431. mlog(0, "node %u responded to master requery with %u\n",
  1432. nodenum, *real_master);
  1433. ret = 0;
  1434. }
  1435. return ret;
  1436. }
  1437. /* this function cannot error, so unless the sending
  1438. * or receiving of the message failed, the owner can
  1439. * be trusted */
  1440. int dlm_master_requery_handler(struct o2net_msg *msg, u32 len, void *data,
  1441. void **ret_data)
  1442. {
  1443. struct dlm_ctxt *dlm = data;
  1444. struct dlm_master_requery *req = (struct dlm_master_requery *)msg->buf;
  1445. struct dlm_lock_resource *res = NULL;
  1446. unsigned int hash;
  1447. int master = DLM_LOCK_RES_OWNER_UNKNOWN;
  1448. u32 flags = DLM_ASSERT_MASTER_REQUERY;
  1449. if (!dlm_grab(dlm)) {
  1450. /* since the domain has gone away on this
  1451. * node, the proper response is UNKNOWN */
  1452. return master;
  1453. }
  1454. hash = dlm_lockid_hash(req->name, req->namelen);
  1455. spin_lock(&dlm->spinlock);
  1456. res = __dlm_lookup_lockres(dlm, req->name, req->namelen, hash);
  1457. if (res) {
  1458. spin_lock(&res->spinlock);
  1459. master = res->owner;
  1460. if (master == dlm->node_num) {
  1461. int ret = dlm_dispatch_assert_master(dlm, res,
  1462. 0, 0, flags);
  1463. if (ret < 0) {
  1464. mlog_errno(-ENOMEM);
  1465. /* retry!? */
  1466. BUG();
  1467. }
  1468. } else /* put.. incase we are not the master */
  1469. dlm_lockres_put(res);
  1470. spin_unlock(&res->spinlock);
  1471. }
  1472. spin_unlock(&dlm->spinlock);
  1473. dlm_put(dlm);
  1474. return master;
  1475. }
  1476. static inline struct list_head *
  1477. dlm_list_num_to_pointer(struct dlm_lock_resource *res, int list_num)
  1478. {
  1479. struct list_head *ret;
  1480. BUG_ON(list_num < 0);
  1481. BUG_ON(list_num > 2);
  1482. ret = &(res->granted);
  1483. ret += list_num;
  1484. return ret;
  1485. }
  1486. /* TODO: do ast flush business
  1487. * TODO: do MIGRATING and RECOVERING spinning
  1488. */
  1489. /*
  1490. * NOTE about in-flight requests during migration:
  1491. *
  1492. * Before attempting the migrate, the master has marked the lockres as
  1493. * MIGRATING and then flushed all of its pending ASTS. So any in-flight
  1494. * requests either got queued before the MIGRATING flag got set, in which
  1495. * case the lock data will reflect the change and a return message is on
  1496. * the way, or the request failed to get in before MIGRATING got set. In
  1497. * this case, the caller will be told to spin and wait for the MIGRATING
  1498. * flag to be dropped, then recheck the master.
  1499. * This holds true for the convert, cancel and unlock cases, and since lvb
  1500. * updates are tied to these same messages, it applies to lvb updates as
  1501. * well. For the lock case, there is no way a lock can be on the master
  1502. * queue and not be on the secondary queue since the lock is always added
  1503. * locally first. This means that the new target node will never be sent
  1504. * a lock that he doesn't already have on the list.
  1505. * In total, this means that the local lock is correct and should not be
  1506. * updated to match the one sent by the master. Any messages sent back
  1507. * from the master before the MIGRATING flag will bring the lock properly
  1508. * up-to-date, and the change will be ordered properly for the waiter.
  1509. * We will *not* attempt to modify the lock underneath the waiter.
  1510. */
  1511. static int dlm_process_recovery_data(struct dlm_ctxt *dlm,
  1512. struct dlm_lock_resource *res,
  1513. struct dlm_migratable_lockres *mres)
  1514. {
  1515. struct dlm_migratable_lock *ml;
  1516. struct list_head *queue;
  1517. struct list_head *tmpq = NULL;
  1518. struct dlm_lock *newlock = NULL;
  1519. struct dlm_lockstatus *lksb = NULL;
  1520. int ret = 0;
  1521. int i, j, bad;
  1522. struct dlm_lock *lock = NULL;
  1523. u8 from = O2NM_MAX_NODES;
  1524. unsigned int added = 0;
  1525. mlog(0, "running %d locks for this lockres\n", mres->num_locks);
  1526. for (i=0; i<mres->num_locks; i++) {
  1527. ml = &(mres->ml[i]);
  1528. if (dlm_is_dummy_lock(dlm, ml, &from)) {
  1529. /* placeholder, just need to set the refmap bit */
  1530. BUG_ON(mres->num_locks != 1);
  1531. mlog(0, "%s:%.*s: dummy lock for %u\n",
  1532. dlm->name, mres->lockname_len, mres->lockname,
  1533. from);
  1534. spin_lock(&res->spinlock);
  1535. dlm_lockres_set_refmap_bit(from, res);
  1536. spin_unlock(&res->spinlock);
  1537. added++;
  1538. break;
  1539. }
  1540. BUG_ON(ml->highest_blocked != LKM_IVMODE);
  1541. newlock = NULL;
  1542. lksb = NULL;
  1543. queue = dlm_list_num_to_pointer(res, ml->list);
  1544. tmpq = NULL;
  1545. /* if the lock is for the local node it needs to
  1546. * be moved to the proper location within the queue.
  1547. * do not allocate a new lock structure. */
  1548. if (ml->node == dlm->node_num) {
  1549. /* MIGRATION ONLY! */
  1550. BUG_ON(!(mres->flags & DLM_MRES_MIGRATION));
  1551. spin_lock(&res->spinlock);
  1552. for (j = DLM_GRANTED_LIST; j <= DLM_BLOCKED_LIST; j++) {
  1553. tmpq = dlm_list_idx_to_ptr(res, j);
  1554. list_for_each_entry(lock, tmpq, list) {
  1555. if (lock->ml.cookie != ml->cookie)
  1556. lock = NULL;
  1557. else
  1558. break;
  1559. }
  1560. if (lock)
  1561. break;
  1562. }
  1563. /* lock is always created locally first, and
  1564. * destroyed locally last. it must be on the list */
  1565. if (!lock) {
  1566. __be64 c = ml->cookie;
  1567. mlog(ML_ERROR, "could not find local lock "
  1568. "with cookie %u:%llu!\n",
  1569. dlm_get_lock_cookie_node(be64_to_cpu(c)),
  1570. dlm_get_lock_cookie_seq(be64_to_cpu(c)));
  1571. __dlm_print_one_lock_resource(res);
  1572. BUG();
  1573. }
  1574. BUG_ON(lock->ml.node != ml->node);
  1575. if (tmpq != queue) {
  1576. mlog(0, "lock was on %u instead of %u for %.*s\n",
  1577. j, ml->list, res->lockname.len, res->lockname.name);
  1578. spin_unlock(&res->spinlock);
  1579. continue;
  1580. }
  1581. /* see NOTE above about why we do not update
  1582. * to match the master here */
  1583. /* move the lock to its proper place */
  1584. /* do not alter lock refcount. switching lists. */
  1585. list_move_tail(&lock->list, queue);
  1586. spin_unlock(&res->spinlock);
  1587. added++;
  1588. mlog(0, "just reordered a local lock!\n");
  1589. continue;
  1590. }
  1591. /* lock is for another node. */
  1592. newlock = dlm_new_lock(ml->type, ml->node,
  1593. be64_to_cpu(ml->cookie), NULL);
  1594. if (!newlock) {
  1595. ret = -ENOMEM;
  1596. goto leave;
  1597. }
  1598. lksb = newlock->lksb;
  1599. dlm_lock_attach_lockres(newlock, res);
  1600. if (ml->convert_type != LKM_IVMODE) {
  1601. BUG_ON(queue != &res->converting);
  1602. newlock->ml.convert_type = ml->convert_type;
  1603. }
  1604. lksb->flags |= (ml->flags &
  1605. (DLM_LKSB_PUT_LVB|DLM_LKSB_GET_LVB));
  1606. if (ml->type == LKM_NLMODE)
  1607. goto skip_lvb;
  1608. if (!dlm_lvb_is_empty(mres->lvb)) {
  1609. if (lksb->flags & DLM_LKSB_PUT_LVB) {
  1610. /* other node was trying to update
  1611. * lvb when node died. recreate the
  1612. * lksb with the updated lvb. */
  1613. memcpy(lksb->lvb, mres->lvb, DLM_LVB_LEN);
  1614. /* the lock resource lvb update must happen
  1615. * NOW, before the spinlock is dropped.
  1616. * we no longer wait for the AST to update
  1617. * the lvb. */
  1618. memcpy(res->lvb, mres->lvb, DLM_LVB_LEN);
  1619. } else {
  1620. /* otherwise, the node is sending its
  1621. * most recent valid lvb info */
  1622. BUG_ON(ml->type != LKM_EXMODE &&
  1623. ml->type != LKM_PRMODE);
  1624. if (!dlm_lvb_is_empty(res->lvb) &&
  1625. (ml->type == LKM_EXMODE ||
  1626. memcmp(res->lvb, mres->lvb, DLM_LVB_LEN))) {
  1627. int i;
  1628. mlog(ML_ERROR, "%s:%.*s: received bad "
  1629. "lvb! type=%d\n", dlm->name,
  1630. res->lockname.len,
  1631. res->lockname.name, ml->type);
  1632. printk("lockres lvb=[");
  1633. for (i=0; i<DLM_LVB_LEN; i++)
  1634. printk("%02x", res->lvb[i]);
  1635. printk("]\nmigrated lvb=[");
  1636. for (i=0; i<DLM_LVB_LEN; i++)
  1637. printk("%02x", mres->lvb[i]);
  1638. printk("]\n");
  1639. dlm_print_one_lock_resource(res);
  1640. BUG();
  1641. }
  1642. memcpy(res->lvb, mres->lvb, DLM_LVB_LEN);
  1643. }
  1644. }
  1645. skip_lvb:
  1646. /* NOTE:
  1647. * wrt lock queue ordering and recovery:
  1648. * 1. order of locks on granted queue is
  1649. * meaningless.
  1650. * 2. order of locks on converting queue is
  1651. * LOST with the node death. sorry charlie.
  1652. * 3. order of locks on the blocked queue is
  1653. * also LOST.
  1654. * order of locks does not affect integrity, it
  1655. * just means that a lock request may get pushed
  1656. * back in line as a result of the node death.
  1657. * also note that for a given node the lock order
  1658. * for its secondary queue locks is preserved
  1659. * relative to each other, but clearly *not*
  1660. * preserved relative to locks from other nodes.
  1661. */
  1662. bad = 0;
  1663. spin_lock(&res->spinlock);
  1664. list_for_each_entry(lock, queue, list) {
  1665. if (lock->ml.cookie == ml->cookie) {
  1666. __be64 c = lock->ml.cookie;
  1667. mlog(ML_ERROR, "%s:%.*s: %u:%llu: lock already "
  1668. "exists on this lockres!\n", dlm->name,
  1669. res->lockname.len, res->lockname.name,
  1670. dlm_get_lock_cookie_node(be64_to_cpu(c)),
  1671. dlm_get_lock_cookie_seq(be64_to_cpu(c)));
  1672. mlog(ML_NOTICE, "sent lock: type=%d, conv=%d, "
  1673. "node=%u, cookie=%u:%llu, queue=%d\n",
  1674. ml->type, ml->convert_type, ml->node,
  1675. dlm_get_lock_cookie_node(be64_to_cpu(ml->cookie)),
  1676. dlm_get_lock_cookie_seq(be64_to_cpu(ml->cookie)),
  1677. ml->list);
  1678. __dlm_print_one_lock_resource(res);
  1679. bad = 1;
  1680. break;
  1681. }
  1682. }
  1683. if (!bad) {
  1684. dlm_lock_get(newlock);
  1685. list_add_tail(&newlock->list, queue);
  1686. mlog(0, "%s:%.*s: added lock for node %u, "
  1687. "setting refmap bit\n", dlm->name,
  1688. res->lockname.len, res->lockname.name, ml->node);
  1689. dlm_lockres_set_refmap_bit(ml->node, res);
  1690. added++;
  1691. }
  1692. spin_unlock(&res->spinlock);
  1693. }
  1694. mlog(0, "done running all the locks\n");
  1695. leave:
  1696. /* balance the ref taken when the work was queued */
  1697. spin_lock(&res->spinlock);
  1698. dlm_lockres_drop_inflight_ref(dlm, res);
  1699. spin_unlock(&res->spinlock);
  1700. if (ret < 0) {
  1701. mlog_errno(ret);
  1702. if (newlock)
  1703. dlm_lock_put(newlock);
  1704. }
  1705. mlog_exit(ret);
  1706. return ret;
  1707. }
  1708. void dlm_move_lockres_to_recovery_list(struct dlm_ctxt *dlm,
  1709. struct dlm_lock_resource *res)
  1710. {
  1711. int i;
  1712. struct list_head *queue;
  1713. struct dlm_lock *lock, *next;
  1714. res->state |= DLM_LOCK_RES_RECOVERING;
  1715. if (!list_empty(&res->recovering)) {
  1716. mlog(0,
  1717. "Recovering res %s:%.*s, is already on recovery list!\n",
  1718. dlm->name, res->lockname.len, res->lockname.name);
  1719. list_del_init(&res->recovering);
  1720. dlm_lockres_put(res);
  1721. }
  1722. /* We need to hold a reference while on the recovery list */
  1723. dlm_lockres_get(res);
  1724. list_add_tail(&res->recovering, &dlm->reco.resources);
  1725. /* find any pending locks and put them back on proper list */
  1726. for (i=DLM_BLOCKED_LIST; i>=DLM_GRANTED_LIST; i--) {
  1727. queue = dlm_list_idx_to_ptr(res, i);
  1728. list_for_each_entry_safe(lock, next, queue, list) {
  1729. dlm_lock_get(lock);
  1730. if (lock->convert_pending) {
  1731. /* move converting lock back to granted */
  1732. BUG_ON(i != DLM_CONVERTING_LIST);
  1733. mlog(0, "node died with convert pending "
  1734. "on %.*s. move back to granted list.\n",
  1735. res->lockname.len, res->lockname.name);
  1736. dlm_revert_pending_convert(res, lock);
  1737. lock->convert_pending = 0;
  1738. } else if (lock->lock_pending) {
  1739. /* remove pending lock requests completely */
  1740. BUG_ON(i != DLM_BLOCKED_LIST);
  1741. mlog(0, "node died with lock pending "
  1742. "on %.*s. remove from blocked list and skip.\n",
  1743. res->lockname.len, res->lockname.name);
  1744. /* lock will be floating until ref in
  1745. * dlmlock_remote is freed after the network
  1746. * call returns. ok for it to not be on any
  1747. * list since no ast can be called
  1748. * (the master is dead). */
  1749. dlm_revert_pending_lock(res, lock);
  1750. lock->lock_pending = 0;
  1751. } else if (lock->unlock_pending) {
  1752. /* if an unlock was in progress, treat as
  1753. * if this had completed successfully
  1754. * before sending this lock state to the
  1755. * new master. note that the dlm_unlock
  1756. * call is still responsible for calling
  1757. * the unlockast. that will happen after
  1758. * the network call times out. for now,
  1759. * just move lists to prepare the new
  1760. * recovery master. */
  1761. BUG_ON(i != DLM_GRANTED_LIST);
  1762. mlog(0, "node died with unlock pending "
  1763. "on %.*s. remove from blocked list and skip.\n",
  1764. res->lockname.len, res->lockname.name);
  1765. dlm_commit_pending_unlock(res, lock);
  1766. lock->unlock_pending = 0;
  1767. } else if (lock->cancel_pending) {
  1768. /* if a cancel was in progress, treat as
  1769. * if this had completed successfully
  1770. * before sending this lock state to the
  1771. * new master */
  1772. BUG_ON(i != DLM_CONVERTING_LIST);
  1773. mlog(0, "node died with cancel pending "
  1774. "on %.*s. move back to granted list.\n",
  1775. res->lockname.len, res->lockname.name);
  1776. dlm_commit_pending_cancel(res, lock);
  1777. lock->cancel_pending = 0;
  1778. }
  1779. dlm_lock_put(lock);
  1780. }
  1781. }
  1782. }
  1783. /* removes all recovered locks from the recovery list.
  1784. * sets the res->owner to the new master.
  1785. * unsets the RECOVERY flag and wakes waiters. */
  1786. static void dlm_finish_local_lockres_recovery(struct dlm_ctxt *dlm,
  1787. u8 dead_node, u8 new_master)
  1788. {
  1789. int i;
  1790. struct hlist_node *hash_iter;
  1791. struct hlist_head *bucket;
  1792. struct dlm_lock_resource *res, *next;
  1793. mlog_entry_void();
  1794. assert_spin_locked(&dlm->spinlock);
  1795. list_for_each_entry_safe(res, next, &dlm->reco.resources, recovering) {
  1796. if (res->owner == dead_node) {
  1797. list_del_init(&res->recovering);
  1798. spin_lock(&res->spinlock);
  1799. /* new_master has our reference from
  1800. * the lock state sent during recovery */
  1801. dlm_change_lockres_owner(dlm, res, new_master);
  1802. res->state &= ~DLM_LOCK_RES_RECOVERING;
  1803. if (__dlm_lockres_has_locks(res))
  1804. __dlm_dirty_lockres(dlm, res);
  1805. spin_unlock(&res->spinlock);
  1806. wake_up(&res->wq);
  1807. dlm_lockres_put(res);
  1808. }
  1809. }
  1810. /* this will become unnecessary eventually, but
  1811. * for now we need to run the whole hash, clear
  1812. * the RECOVERING state and set the owner
  1813. * if necessary */
  1814. for (i = 0; i < DLM_HASH_BUCKETS; i++) {
  1815. bucket = dlm_lockres_hash(dlm, i);
  1816. hlist_for_each_entry(res, hash_iter, bucket, hash_node) {
  1817. if (res->state & DLM_LOCK_RES_RECOVERING) {
  1818. if (res->owner == dead_node) {
  1819. mlog(0, "(this=%u) res %.*s owner=%u "
  1820. "was not on recovering list, but "
  1821. "clearing state anyway\n",
  1822. dlm->node_num, res->lockname.len,
  1823. res->lockname.name, new_master);
  1824. } else if (res->owner == dlm->node_num) {
  1825. mlog(0, "(this=%u) res %.*s owner=%u "
  1826. "was not on recovering list, "
  1827. "owner is THIS node, clearing\n",
  1828. dlm->node_num, res->lockname.len,
  1829. res->lockname.name, new_master);
  1830. } else
  1831. continue;
  1832. if (!list_empty(&res->recovering)) {
  1833. mlog(0, "%s:%.*s: lockres was "
  1834. "marked RECOVERING, owner=%u\n",
  1835. dlm->name, res->lockname.len,
  1836. res->lockname.name, res->owner);
  1837. list_del_init(&res->recovering);
  1838. dlm_lockres_put(res);
  1839. }
  1840. spin_lock(&res->spinlock);
  1841. /* new_master has our reference from
  1842. * the lock state sent during recovery */
  1843. dlm_change_lockres_owner(dlm, res, new_master);
  1844. res->state &= ~DLM_LOCK_RES_RECOVERING;
  1845. if (__dlm_lockres_has_locks(res))
  1846. __dlm_dirty_lockres(dlm, res);
  1847. spin_unlock(&res->spinlock);
  1848. wake_up(&res->wq);
  1849. }
  1850. }
  1851. }
  1852. }
  1853. static inline int dlm_lvb_needs_invalidation(struct dlm_lock *lock, int local)
  1854. {
  1855. if (local) {
  1856. if (lock->ml.type != LKM_EXMODE &&
  1857. lock->ml.type != LKM_PRMODE)
  1858. return 1;
  1859. } else if (lock->ml.type == LKM_EXMODE)
  1860. return 1;
  1861. return 0;
  1862. }
  1863. static void dlm_revalidate_lvb(struct dlm_ctxt *dlm,
  1864. struct dlm_lock_resource *res, u8 dead_node)
  1865. {
  1866. struct list_head *queue;
  1867. struct dlm_lock *lock;
  1868. int blank_lvb = 0, local = 0;
  1869. int i;
  1870. u8 search_node;
  1871. assert_spin_locked(&dlm->spinlock);
  1872. assert_spin_locked(&res->spinlock);
  1873. if (res->owner == dlm->node_num)
  1874. /* if this node owned the lockres, and if the dead node
  1875. * had an EX when he died, blank out the lvb */
  1876. search_node = dead_node;
  1877. else {
  1878. /* if this is a secondary lockres, and we had no EX or PR
  1879. * locks granted, we can no longer trust the lvb */
  1880. search_node = dlm->node_num;
  1881. local = 1; /* check local state for valid lvb */
  1882. }
  1883. for (i=DLM_GRANTED_LIST; i<=DLM_CONVERTING_LIST; i++) {
  1884. queue = dlm_list_idx_to_ptr(res, i);
  1885. list_for_each_entry(lock, queue, list) {
  1886. if (lock->ml.node == search_node) {
  1887. if (dlm_lvb_needs_invalidation(lock, local)) {
  1888. /* zero the lksb lvb and lockres lvb */
  1889. blank_lvb = 1;
  1890. memset(lock->lksb->lvb, 0, DLM_LVB_LEN);
  1891. }
  1892. }
  1893. }
  1894. }
  1895. if (blank_lvb) {
  1896. mlog(0, "clearing %.*s lvb, dead node %u had EX\n",
  1897. res->lockname.len, res->lockname.name, dead_node);
  1898. memset(res->lvb, 0, DLM_LVB_LEN);
  1899. }
  1900. }
  1901. static void dlm_free_dead_locks(struct dlm_ctxt *dlm,
  1902. struct dlm_lock_resource *res, u8 dead_node)
  1903. {
  1904. struct dlm_lock *lock, *next;
  1905. unsigned int freed = 0;
  1906. /* this node is the lockres master:
  1907. * 1) remove any stale locks for the dead node
  1908. * 2) if the dead node had an EX when he died, blank out the lvb
  1909. */
  1910. assert_spin_locked(&dlm->spinlock);
  1911. assert_spin_locked(&res->spinlock);
  1912. /* We do two dlm_lock_put(). One for removing from list and the other is
  1913. * to force the DLM_UNLOCK_FREE_LOCK action so as to free the locks */
  1914. /* TODO: check pending_asts, pending_basts here */
  1915. list_for_each_entry_safe(lock, next, &res->granted, list) {
  1916. if (lock->ml.node == dead_node) {
  1917. list_del_init(&lock->list);
  1918. dlm_lock_put(lock);
  1919. /* Can't schedule DLM_UNLOCK_FREE_LOCK - do manually */
  1920. dlm_lock_put(lock);
  1921. freed++;
  1922. }
  1923. }
  1924. list_for_each_entry_safe(lock, next, &res->converting, list) {
  1925. if (lock->ml.node == dead_node) {
  1926. list_del_init(&lock->list);
  1927. dlm_lock_put(lock);
  1928. /* Can't schedule DLM_UNLOCK_FREE_LOCK - do manually */
  1929. dlm_lock_put(lock);
  1930. freed++;
  1931. }
  1932. }
  1933. list_for_each_entry_safe(lock, next, &res->blocked, list) {
  1934. if (lock->ml.node == dead_node) {
  1935. list_del_init(&lock->list);
  1936. dlm_lock_put(lock);
  1937. /* Can't schedule DLM_UNLOCK_FREE_LOCK - do manually */
  1938. dlm_lock_put(lock);
  1939. freed++;
  1940. }
  1941. }
  1942. if (freed) {
  1943. mlog(0, "%s:%.*s: freed %u locks for dead node %u, "
  1944. "dropping ref from lockres\n", dlm->name,
  1945. res->lockname.len, res->lockname.name, freed, dead_node);
  1946. BUG_ON(!test_bit(dead_node, res->refmap));
  1947. dlm_lockres_clear_refmap_bit(dead_node, res);
  1948. } else if (test_bit(dead_node, res->refmap)) {
  1949. mlog(0, "%s:%.*s: dead node %u had a ref, but had "
  1950. "no locks and had not purged before dying\n", dlm->name,
  1951. res->lockname.len, res->lockname.name, dead_node);
  1952. dlm_lockres_clear_refmap_bit(dead_node, res);
  1953. }
  1954. /* do not kick thread yet */
  1955. __dlm_dirty_lockres(dlm, res);
  1956. }
  1957. /* if this node is the recovery master, and there are no
  1958. * locks for a given lockres owned by this node that are in
  1959. * either PR or EX mode, zero out the lvb before requesting.
  1960. *
  1961. */
  1962. static void dlm_do_local_recovery_cleanup(struct dlm_ctxt *dlm, u8 dead_node)
  1963. {
  1964. struct hlist_node *iter;
  1965. struct dlm_lock_resource *res;
  1966. int i;
  1967. struct hlist_head *bucket;
  1968. struct dlm_lock *lock;
  1969. /* purge any stale mles */
  1970. dlm_clean_master_list(dlm, dead_node);
  1971. /*
  1972. * now clean up all lock resources. there are two rules:
  1973. *
  1974. * 1) if the dead node was the master, move the lockres
  1975. * to the recovering list. set the RECOVERING flag.
  1976. * this lockres needs to be cleaned up before it can
  1977. * be used further.
  1978. *
  1979. * 2) if this node was the master, remove all locks from
  1980. * each of the lockres queues that were owned by the
  1981. * dead node. once recovery finishes, the dlm thread
  1982. * can be kicked again to see if any ASTs or BASTs
  1983. * need to be fired as a result.
  1984. */
  1985. for (i = 0; i < DLM_HASH_BUCKETS; i++) {
  1986. bucket = dlm_lockres_hash(dlm, i);
  1987. hlist_for_each_entry(res, iter, bucket, hash_node) {
  1988. /* always prune any $RECOVERY entries for dead nodes,
  1989. * otherwise hangs can occur during later recovery */
  1990. if (dlm_is_recovery_lock(res->lockname.name,
  1991. res->lockname.len)) {
  1992. spin_lock(&res->spinlock);
  1993. list_for_each_entry(lock, &res->granted, list) {
  1994. if (lock->ml.node == dead_node) {
  1995. mlog(0, "AHA! there was "
  1996. "a $RECOVERY lock for dead "
  1997. "node %u (%s)!\n",
  1998. dead_node, dlm->name);
  1999. list_del_init(&lock->list);
  2000. dlm_lock_put(lock);
  2001. break;
  2002. }
  2003. }
  2004. spin_unlock(&res->spinlock);
  2005. continue;
  2006. }
  2007. spin_lock(&res->spinlock);
  2008. /* zero the lvb if necessary */
  2009. dlm_revalidate_lvb(dlm, res, dead_node);
  2010. if (res->owner == dead_node) {
  2011. if (res->state & DLM_LOCK_RES_DROPPING_REF)
  2012. mlog(0, "%s:%.*s: owned by "
  2013. "dead node %u, this node was "
  2014. "dropping its ref when it died. "
  2015. "continue, dropping the flag.\n",
  2016. dlm->name, res->lockname.len,
  2017. res->lockname.name, dead_node);
  2018. /* the wake_up for this will happen when the
  2019. * RECOVERING flag is dropped later */
  2020. res->state &= ~DLM_LOCK_RES_DROPPING_REF;
  2021. dlm_move_lockres_to_recovery_list(dlm, res);
  2022. } else if (res->owner == dlm->node_num) {
  2023. dlm_free_dead_locks(dlm, res, dead_node);
  2024. __dlm_lockres_calc_usage(dlm, res);
  2025. }
  2026. spin_unlock(&res->spinlock);
  2027. }
  2028. }
  2029. }
  2030. static void __dlm_hb_node_down(struct dlm_ctxt *dlm, int idx)
  2031. {
  2032. assert_spin_locked(&dlm->spinlock);
  2033. if (dlm->reco.new_master == idx) {
  2034. mlog(0, "%s: recovery master %d just died\n",
  2035. dlm->name, idx);
  2036. if (dlm->reco.state & DLM_RECO_STATE_FINALIZE) {
  2037. /* finalize1 was reached, so it is safe to clear
  2038. * the new_master and dead_node. that recovery
  2039. * is complete. */
  2040. mlog(0, "%s: dead master %d had reached "
  2041. "finalize1 state, clearing\n", dlm->name, idx);
  2042. dlm->reco.state &= ~DLM_RECO_STATE_FINALIZE;
  2043. __dlm_reset_recovery(dlm);
  2044. }
  2045. }
  2046. /* Clean up join state on node death. */
  2047. if (dlm->joining_node == idx) {
  2048. mlog(0, "Clearing join state for node %u\n", idx);
  2049. __dlm_set_joining_node(dlm, DLM_LOCK_RES_OWNER_UNKNOWN);
  2050. }
  2051. /* check to see if the node is already considered dead */
  2052. if (!test_bit(idx, dlm->live_nodes_map)) {
  2053. mlog(0, "for domain %s, node %d is already dead. "
  2054. "another node likely did recovery already.\n",
  2055. dlm->name, idx);
  2056. return;
  2057. }
  2058. /* check to see if we do not care about this node */
  2059. if (!test_bit(idx, dlm->domain_map)) {
  2060. /* This also catches the case that we get a node down
  2061. * but haven't joined the domain yet. */
  2062. mlog(0, "node %u already removed from domain!\n", idx);
  2063. return;
  2064. }
  2065. clear_bit(idx, dlm->live_nodes_map);
  2066. /* make sure local cleanup occurs before the heartbeat events */
  2067. if (!test_bit(idx, dlm->recovery_map))
  2068. dlm_do_local_recovery_cleanup(dlm, idx);
  2069. /* notify anything attached to the heartbeat events */
  2070. dlm_hb_event_notify_attached(dlm, idx, 0);
  2071. mlog(0, "node %u being removed from domain map!\n", idx);
  2072. clear_bit(idx, dlm->domain_map);
  2073. /* wake up migration waiters if a node goes down.
  2074. * perhaps later we can genericize this for other waiters. */
  2075. wake_up(&dlm->migration_wq);
  2076. if (test_bit(idx, dlm->recovery_map))
  2077. mlog(0, "domain %s, node %u already added "
  2078. "to recovery map!\n", dlm->name, idx);
  2079. else
  2080. set_bit(idx, dlm->recovery_map);
  2081. }
  2082. void dlm_hb_node_down_cb(struct o2nm_node *node, int idx, void *data)
  2083. {
  2084. struct dlm_ctxt *dlm = data;
  2085. if (!dlm_grab(dlm))
  2086. return;
  2087. /*
  2088. * This will notify any dlm users that a node in our domain
  2089. * went away without notifying us first.
  2090. */
  2091. if (test_bit(idx, dlm->domain_map))
  2092. dlm_fire_domain_eviction_callbacks(dlm, idx);
  2093. spin_lock(&dlm->spinlock);
  2094. __dlm_hb_node_down(dlm, idx);
  2095. spin_unlock(&dlm->spinlock);
  2096. dlm_put(dlm);
  2097. }
  2098. void dlm_hb_node_up_cb(struct o2nm_node *node, int idx, void *data)
  2099. {
  2100. struct dlm_ctxt *dlm = data;
  2101. if (!dlm_grab(dlm))
  2102. return;
  2103. spin_lock(&dlm->spinlock);
  2104. set_bit(idx, dlm->live_nodes_map);
  2105. /* do NOT notify mle attached to the heartbeat events.
  2106. * new nodes are not interesting in mastery until joined. */
  2107. spin_unlock(&dlm->spinlock);
  2108. dlm_put(dlm);
  2109. }
  2110. static void dlm_reco_ast(void *astdata)
  2111. {
  2112. struct dlm_ctxt *dlm = astdata;
  2113. mlog(0, "ast for recovery lock fired!, this=%u, dlm=%s\n",
  2114. dlm->node_num, dlm->name);
  2115. }
  2116. static void dlm_reco_bast(void *astdata, int blocked_type)
  2117. {
  2118. struct dlm_ctxt *dlm = astdata;
  2119. mlog(0, "bast for recovery lock fired!, this=%u, dlm=%s\n",
  2120. dlm->node_num, dlm->name);
  2121. }
  2122. static void dlm_reco_unlock_ast(void *astdata, enum dlm_status st)
  2123. {
  2124. mlog(0, "unlockast for recovery lock fired!\n");
  2125. }
  2126. /*
  2127. * dlm_pick_recovery_master will continually attempt to use
  2128. * dlmlock() on the special "$RECOVERY" lockres with the
  2129. * LKM_NOQUEUE flag to get an EX. every thread that enters
  2130. * this function on each node racing to become the recovery
  2131. * master will not stop attempting this until either:
  2132. * a) this node gets the EX (and becomes the recovery master),
  2133. * or b) dlm->reco.new_master gets set to some nodenum
  2134. * != O2NM_INVALID_NODE_NUM (another node will do the reco).
  2135. * so each time a recovery master is needed, the entire cluster
  2136. * will sync at this point. if the new master dies, that will
  2137. * be detected in dlm_do_recovery */
  2138. static int dlm_pick_recovery_master(struct dlm_ctxt *dlm)
  2139. {
  2140. enum dlm_status ret;
  2141. struct dlm_lockstatus lksb;
  2142. int status = -EINVAL;
  2143. mlog(0, "starting recovery of %s at %lu, dead=%u, this=%u\n",
  2144. dlm->name, jiffies, dlm->reco.dead_node, dlm->node_num);
  2145. again:
  2146. memset(&lksb, 0, sizeof(lksb));
  2147. ret = dlmlock(dlm, LKM_EXMODE, &lksb, LKM_NOQUEUE|LKM_RECOVERY,
  2148. DLM_RECOVERY_LOCK_NAME, DLM_RECOVERY_LOCK_NAME_LEN,
  2149. dlm_reco_ast, dlm, dlm_reco_bast);
  2150. mlog(0, "%s: dlmlock($RECOVERY) returned %d, lksb=%d\n",
  2151. dlm->name, ret, lksb.status);
  2152. if (ret == DLM_NORMAL) {
  2153. mlog(0, "dlm=%s dlmlock says I got it (this=%u)\n",
  2154. dlm->name, dlm->node_num);
  2155. /* got the EX lock. check to see if another node
  2156. * just became the reco master */
  2157. if (dlm_reco_master_ready(dlm)) {
  2158. mlog(0, "%s: got reco EX lock, but %u will "
  2159. "do the recovery\n", dlm->name,
  2160. dlm->reco.new_master);
  2161. status = -EEXIST;
  2162. } else {
  2163. status = 0;
  2164. /* see if recovery was already finished elsewhere */
  2165. spin_lock(&dlm->spinlock);
  2166. if (dlm->reco.dead_node == O2NM_INVALID_NODE_NUM) {
  2167. status = -EINVAL;
  2168. mlog(0, "%s: got reco EX lock, but "
  2169. "node got recovered already\n", dlm->name);
  2170. if (dlm->reco.new_master != O2NM_INVALID_NODE_NUM) {
  2171. mlog(ML_ERROR, "%s: new master is %u "
  2172. "but no dead node!\n",
  2173. dlm->name, dlm->reco.new_master);
  2174. BUG();
  2175. }
  2176. }
  2177. spin_unlock(&dlm->spinlock);
  2178. }
  2179. /* if this node has actually become the recovery master,
  2180. * set the master and send the messages to begin recovery */
  2181. if (!status) {
  2182. mlog(0, "%s: dead=%u, this=%u, sending "
  2183. "begin_reco now\n", dlm->name,
  2184. dlm->reco.dead_node, dlm->node_num);
  2185. status = dlm_send_begin_reco_message(dlm,
  2186. dlm->reco.dead_node);
  2187. /* this always succeeds */
  2188. BUG_ON(status);
  2189. /* set the new_master to this node */
  2190. spin_lock(&dlm->spinlock);
  2191. dlm_set_reco_master(dlm, dlm->node_num);
  2192. spin_unlock(&dlm->spinlock);
  2193. }
  2194. /* recovery lock is a special case. ast will not get fired,
  2195. * so just go ahead and unlock it. */
  2196. ret = dlmunlock(dlm, &lksb, 0, dlm_reco_unlock_ast, dlm);
  2197. if (ret == DLM_DENIED) {
  2198. mlog(0, "got DLM_DENIED, trying LKM_CANCEL\n");
  2199. ret = dlmunlock(dlm, &lksb, LKM_CANCEL, dlm_reco_unlock_ast, dlm);
  2200. }
  2201. if (ret != DLM_NORMAL) {
  2202. /* this would really suck. this could only happen
  2203. * if there was a network error during the unlock
  2204. * because of node death. this means the unlock
  2205. * is actually "done" and the lock structure is
  2206. * even freed. we can continue, but only
  2207. * because this specific lock name is special. */
  2208. mlog(ML_ERROR, "dlmunlock returned %d\n", ret);
  2209. }
  2210. } else if (ret == DLM_NOTQUEUED) {
  2211. mlog(0, "dlm=%s dlmlock says another node got it (this=%u)\n",
  2212. dlm->name, dlm->node_num);
  2213. /* another node is master. wait on
  2214. * reco.new_master != O2NM_INVALID_NODE_NUM
  2215. * for at most one second */
  2216. wait_event_timeout(dlm->dlm_reco_thread_wq,
  2217. dlm_reco_master_ready(dlm),
  2218. msecs_to_jiffies(1000));
  2219. if (!dlm_reco_master_ready(dlm)) {
  2220. mlog(0, "%s: reco master taking awhile\n",
  2221. dlm->name);
  2222. goto again;
  2223. }
  2224. /* another node has informed this one that it is reco master */
  2225. mlog(0, "%s: reco master %u is ready to recover %u\n",
  2226. dlm->name, dlm->reco.new_master, dlm->reco.dead_node);
  2227. status = -EEXIST;
  2228. } else if (ret == DLM_RECOVERING) {
  2229. mlog(0, "dlm=%s dlmlock says master node died (this=%u)\n",
  2230. dlm->name, dlm->node_num);
  2231. goto again;
  2232. } else {
  2233. struct dlm_lock_resource *res;
  2234. /* dlmlock returned something other than NOTQUEUED or NORMAL */
  2235. mlog(ML_ERROR, "%s: got %s from dlmlock($RECOVERY), "
  2236. "lksb.status=%s\n", dlm->name, dlm_errname(ret),
  2237. dlm_errname(lksb.status));
  2238. res = dlm_lookup_lockres(dlm, DLM_RECOVERY_LOCK_NAME,
  2239. DLM_RECOVERY_LOCK_NAME_LEN);
  2240. if (res) {
  2241. dlm_print_one_lock_resource(res);
  2242. dlm_lockres_put(res);
  2243. } else {
  2244. mlog(ML_ERROR, "recovery lock not found\n");
  2245. }
  2246. BUG();
  2247. }
  2248. return status;
  2249. }
  2250. static int dlm_send_begin_reco_message(struct dlm_ctxt *dlm, u8 dead_node)
  2251. {
  2252. struct dlm_begin_reco br;
  2253. int ret = 0;
  2254. struct dlm_node_iter iter;
  2255. int nodenum;
  2256. int status;
  2257. mlog_entry("%u\n", dead_node);
  2258. mlog(0, "%s: dead node is %u\n", dlm->name, dead_node);
  2259. spin_lock(&dlm->spinlock);
  2260. dlm_node_iter_init(dlm->domain_map, &iter);
  2261. spin_unlock(&dlm->spinlock);
  2262. clear_bit(dead_node, iter.node_map);
  2263. memset(&br, 0, sizeof(br));
  2264. br.node_idx = dlm->node_num;
  2265. br.dead_node = dead_node;
  2266. while ((nodenum = dlm_node_iter_next(&iter)) >= 0) {
  2267. ret = 0;
  2268. if (nodenum == dead_node) {
  2269. mlog(0, "not sending begin reco to dead node "
  2270. "%u\n", dead_node);
  2271. continue;
  2272. }
  2273. if (nodenum == dlm->node_num) {
  2274. mlog(0, "not sending begin reco to self\n");
  2275. continue;
  2276. }
  2277. retry:
  2278. ret = -EINVAL;
  2279. mlog(0, "attempting to send begin reco msg to %d\n",
  2280. nodenum);
  2281. ret = o2net_send_message(DLM_BEGIN_RECO_MSG, dlm->key,
  2282. &br, sizeof(br), nodenum, &status);
  2283. /* negative status is handled ok by caller here */
  2284. if (ret >= 0)
  2285. ret = status;
  2286. if (dlm_is_host_down(ret)) {
  2287. /* node is down. not involved in recovery
  2288. * so just keep going */
  2289. mlog(0, "%s: node %u was down when sending "
  2290. "begin reco msg (%d)\n", dlm->name, nodenum, ret);
  2291. ret = 0;
  2292. }
  2293. if (ret < 0) {
  2294. struct dlm_lock_resource *res;
  2295. /* this is now a serious problem, possibly ENOMEM
  2296. * in the network stack. must retry */
  2297. mlog_errno(ret);
  2298. mlog(ML_ERROR, "begin reco of dlm %s to node %u "
  2299. " returned %d\n", dlm->name, nodenum, ret);
  2300. res = dlm_lookup_lockres(dlm, DLM_RECOVERY_LOCK_NAME,
  2301. DLM_RECOVERY_LOCK_NAME_LEN);
  2302. if (res) {
  2303. dlm_print_one_lock_resource(res);
  2304. dlm_lockres_put(res);
  2305. } else {
  2306. mlog(ML_ERROR, "recovery lock not found\n");
  2307. }
  2308. /* sleep for a bit in hopes that we can avoid
  2309. * another ENOMEM */
  2310. msleep(100);
  2311. goto retry;
  2312. } else if (ret == EAGAIN) {
  2313. mlog(0, "%s: trying to start recovery of node "
  2314. "%u, but node %u is waiting for last recovery "
  2315. "to complete, backoff for a bit\n", dlm->name,
  2316. dead_node, nodenum);
  2317. /* TODO Look into replacing msleep with cond_resched() */
  2318. msleep(100);
  2319. goto retry;
  2320. }
  2321. }
  2322. return ret;
  2323. }
  2324. int dlm_begin_reco_handler(struct o2net_msg *msg, u32 len, void *data,
  2325. void **ret_data)
  2326. {
  2327. struct dlm_ctxt *dlm = data;
  2328. struct dlm_begin_reco *br = (struct dlm_begin_reco *)msg->buf;
  2329. /* ok to return 0, domain has gone away */
  2330. if (!dlm_grab(dlm))
  2331. return 0;
  2332. spin_lock(&dlm->spinlock);
  2333. if (dlm->reco.state & DLM_RECO_STATE_FINALIZE) {
  2334. mlog(0, "%s: node %u wants to recover node %u (%u:%u) "
  2335. "but this node is in finalize state, waiting on finalize2\n",
  2336. dlm->name, br->node_idx, br->dead_node,
  2337. dlm->reco.dead_node, dlm->reco.new_master);
  2338. spin_unlock(&dlm->spinlock);
  2339. return EAGAIN;
  2340. }
  2341. spin_unlock(&dlm->spinlock);
  2342. mlog(0, "%s: node %u wants to recover node %u (%u:%u)\n",
  2343. dlm->name, br->node_idx, br->dead_node,
  2344. dlm->reco.dead_node, dlm->reco.new_master);
  2345. dlm_fire_domain_eviction_callbacks(dlm, br->dead_node);
  2346. spin_lock(&dlm->spinlock);
  2347. if (dlm->reco.new_master != O2NM_INVALID_NODE_NUM) {
  2348. if (test_bit(dlm->reco.new_master, dlm->recovery_map)) {
  2349. mlog(0, "%s: new_master %u died, changing "
  2350. "to %u\n", dlm->name, dlm->reco.new_master,
  2351. br->node_idx);
  2352. } else {
  2353. mlog(0, "%s: new_master %u NOT DEAD, changing "
  2354. "to %u\n", dlm->name, dlm->reco.new_master,
  2355. br->node_idx);
  2356. /* may not have seen the new master as dead yet */
  2357. }
  2358. }
  2359. if (dlm->reco.dead_node != O2NM_INVALID_NODE_NUM) {
  2360. mlog(ML_NOTICE, "%s: dead_node previously set to %u, "
  2361. "node %u changing it to %u\n", dlm->name,
  2362. dlm->reco.dead_node, br->node_idx, br->dead_node);
  2363. }
  2364. dlm_set_reco_master(dlm, br->node_idx);
  2365. dlm_set_reco_dead_node(dlm, br->dead_node);
  2366. if (!test_bit(br->dead_node, dlm->recovery_map)) {
  2367. mlog(0, "recovery master %u sees %u as dead, but this "
  2368. "node has not yet. marking %u as dead\n",
  2369. br->node_idx, br->dead_node, br->dead_node);
  2370. if (!test_bit(br->dead_node, dlm->domain_map) ||
  2371. !test_bit(br->dead_node, dlm->live_nodes_map))
  2372. mlog(0, "%u not in domain/live_nodes map "
  2373. "so setting it in reco map manually\n",
  2374. br->dead_node);
  2375. /* force the recovery cleanup in __dlm_hb_node_down
  2376. * both of these will be cleared in a moment */
  2377. set_bit(br->dead_node, dlm->domain_map);
  2378. set_bit(br->dead_node, dlm->live_nodes_map);
  2379. __dlm_hb_node_down(dlm, br->dead_node);
  2380. }
  2381. spin_unlock(&dlm->spinlock);
  2382. dlm_kick_recovery_thread(dlm);
  2383. mlog(0, "%s: recovery started by node %u, for %u (%u:%u)\n",
  2384. dlm->name, br->node_idx, br->dead_node,
  2385. dlm->reco.dead_node, dlm->reco.new_master);
  2386. dlm_put(dlm);
  2387. return 0;
  2388. }
  2389. #define DLM_FINALIZE_STAGE2 0x01
  2390. static int dlm_send_finalize_reco_message(struct dlm_ctxt *dlm)
  2391. {
  2392. int ret = 0;
  2393. struct dlm_finalize_reco fr;
  2394. struct dlm_node_iter iter;
  2395. int nodenum;
  2396. int status;
  2397. int stage = 1;
  2398. mlog(0, "finishing recovery for node %s:%u, "
  2399. "stage %d\n", dlm->name, dlm->reco.dead_node, stage);
  2400. spin_lock(&dlm->spinlock);
  2401. dlm_node_iter_init(dlm->domain_map, &iter);
  2402. spin_unlock(&dlm->spinlock);
  2403. stage2:
  2404. memset(&fr, 0, sizeof(fr));
  2405. fr.node_idx = dlm->node_num;
  2406. fr.dead_node = dlm->reco.dead_node;
  2407. if (stage == 2)
  2408. fr.flags |= DLM_FINALIZE_STAGE2;
  2409. while ((nodenum = dlm_node_iter_next(&iter)) >= 0) {
  2410. if (nodenum == dlm->node_num)
  2411. continue;
  2412. ret = o2net_send_message(DLM_FINALIZE_RECO_MSG, dlm->key,
  2413. &fr, sizeof(fr), nodenum, &status);
  2414. if (ret >= 0)
  2415. ret = status;
  2416. if (ret < 0) {
  2417. mlog_errno(ret);
  2418. if (dlm_is_host_down(ret)) {
  2419. /* this has no effect on this recovery
  2420. * session, so set the status to zero to
  2421. * finish out the last recovery */
  2422. mlog(ML_ERROR, "node %u went down after this "
  2423. "node finished recovery.\n", nodenum);
  2424. ret = 0;
  2425. continue;
  2426. }
  2427. break;
  2428. }
  2429. }
  2430. if (stage == 1) {
  2431. /* reset the node_iter back to the top and send finalize2 */
  2432. iter.curnode = -1;
  2433. stage = 2;
  2434. goto stage2;
  2435. }
  2436. return ret;
  2437. }
  2438. int dlm_finalize_reco_handler(struct o2net_msg *msg, u32 len, void *data,
  2439. void **ret_data)
  2440. {
  2441. struct dlm_ctxt *dlm = data;
  2442. struct dlm_finalize_reco *fr = (struct dlm_finalize_reco *)msg->buf;
  2443. int stage = 1;
  2444. /* ok to return 0, domain has gone away */
  2445. if (!dlm_grab(dlm))
  2446. return 0;
  2447. if (fr->flags & DLM_FINALIZE_STAGE2)
  2448. stage = 2;
  2449. mlog(0, "%s: node %u finalizing recovery stage%d of "
  2450. "node %u (%u:%u)\n", dlm->name, fr->node_idx, stage,
  2451. fr->dead_node, dlm->reco.dead_node, dlm->reco.new_master);
  2452. spin_lock(&dlm->spinlock);
  2453. if (dlm->reco.new_master != fr->node_idx) {
  2454. mlog(ML_ERROR, "node %u sent recovery finalize msg, but node "
  2455. "%u is supposed to be the new master, dead=%u\n",
  2456. fr->node_idx, dlm->reco.new_master, fr->dead_node);
  2457. BUG();
  2458. }
  2459. if (dlm->reco.dead_node != fr->dead_node) {
  2460. mlog(ML_ERROR, "node %u sent recovery finalize msg for dead "
  2461. "node %u, but node %u is supposed to be dead\n",
  2462. fr->node_idx, fr->dead_node, dlm->reco.dead_node);
  2463. BUG();
  2464. }
  2465. switch (stage) {
  2466. case 1:
  2467. dlm_finish_local_lockres_recovery(dlm, fr->dead_node, fr->node_idx);
  2468. if (dlm->reco.state & DLM_RECO_STATE_FINALIZE) {
  2469. mlog(ML_ERROR, "%s: received finalize1 from "
  2470. "new master %u for dead node %u, but "
  2471. "this node has already received it!\n",
  2472. dlm->name, fr->node_idx, fr->dead_node);
  2473. dlm_print_reco_node_status(dlm);
  2474. BUG();
  2475. }
  2476. dlm->reco.state |= DLM_RECO_STATE_FINALIZE;
  2477. spin_unlock(&dlm->spinlock);
  2478. break;
  2479. case 2:
  2480. if (!(dlm->reco.state & DLM_RECO_STATE_FINALIZE)) {
  2481. mlog(ML_ERROR, "%s: received finalize2 from "
  2482. "new master %u for dead node %u, but "
  2483. "this node did not have finalize1!\n",
  2484. dlm->name, fr->node_idx, fr->dead_node);
  2485. dlm_print_reco_node_status(dlm);
  2486. BUG();
  2487. }
  2488. dlm->reco.state &= ~DLM_RECO_STATE_FINALIZE;
  2489. spin_unlock(&dlm->spinlock);
  2490. dlm_reset_recovery(dlm);
  2491. dlm_kick_recovery_thread(dlm);
  2492. break;
  2493. default:
  2494. BUG();
  2495. }
  2496. mlog(0, "%s: recovery done, reco master was %u, dead now %u, master now %u\n",
  2497. dlm->name, fr->node_idx, dlm->reco.dead_node, dlm->reco.new_master);
  2498. dlm_put(dlm);
  2499. return 0;
  2500. }