dlmrecovery.c 69 KB

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