dlmrecovery.c 68 KB

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