dlmrecovery.c 68 KB

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