dlmrecovery.c 70 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431
  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. if (lr->dead_node != dlm->reco.dead_node) {
  615. mlog(ML_ERROR, "%s: node %u sent dead_node=%u, but local "
  616. "dead_node is %u\n", dlm->name, lr->node_idx,
  617. lr->dead_node, dlm->reco.dead_node);
  618. /* this is a hack */
  619. dlm_put(dlm);
  620. return -ENOMEM;
  621. }
  622. BUG_ON(lr->dead_node != dlm->reco.dead_node);
  623. item = kcalloc(1, sizeof(*item), GFP_KERNEL);
  624. if (!item) {
  625. dlm_put(dlm);
  626. return -ENOMEM;
  627. }
  628. /* this will get freed by dlm_request_all_locks_worker */
  629. buf = (char *) __get_free_page(GFP_KERNEL);
  630. if (!buf) {
  631. kfree(item);
  632. dlm_put(dlm);
  633. return -ENOMEM;
  634. }
  635. /* queue up work for dlm_request_all_locks_worker */
  636. dlm_grab(dlm); /* get an extra ref for the work item */
  637. dlm_init_work_item(dlm, item, dlm_request_all_locks_worker, buf);
  638. item->u.ral.reco_master = lr->node_idx;
  639. item->u.ral.dead_node = lr->dead_node;
  640. spin_lock(&dlm->work_lock);
  641. list_add_tail(&item->list, &dlm->work_list);
  642. spin_unlock(&dlm->work_lock);
  643. schedule_work(&dlm->dispatched_work);
  644. dlm_put(dlm);
  645. return 0;
  646. }
  647. static void dlm_request_all_locks_worker(struct dlm_work_item *item, void *data)
  648. {
  649. struct dlm_migratable_lockres *mres;
  650. struct dlm_lock_resource *res;
  651. struct dlm_ctxt *dlm;
  652. LIST_HEAD(resources);
  653. struct list_head *iter;
  654. int ret;
  655. u8 dead_node, reco_master;
  656. int skip_all_done = 0;
  657. dlm = item->dlm;
  658. dead_node = item->u.ral.dead_node;
  659. reco_master = item->u.ral.reco_master;
  660. mres = (struct dlm_migratable_lockres *)data;
  661. if (dead_node != dlm->reco.dead_node ||
  662. reco_master != dlm->reco.new_master) {
  663. /* show extra debug info if the recovery state is messed */
  664. mlog(ML_ERROR, "%s: bad reco state: reco(dead=%u, master=%u), "
  665. "request(dead=%u, master=%u)\n",
  666. dlm->name, dlm->reco.dead_node, dlm->reco.new_master,
  667. dead_node, reco_master);
  668. mlog(ML_ERROR, "%s: name=%.*s master=%u locks=%u/%u flags=%u "
  669. "entry[0]={c=%u:%llu,l=%u,f=%u,t=%d,ct=%d,hb=%d,n=%u}\n",
  670. dlm->name, mres->lockname_len, mres->lockname, mres->master,
  671. mres->num_locks, mres->total_locks, mres->flags,
  672. dlm_get_lock_cookie_node(mres->ml[0].cookie),
  673. dlm_get_lock_cookie_seq(mres->ml[0].cookie),
  674. mres->ml[0].list, mres->ml[0].flags,
  675. mres->ml[0].type, mres->ml[0].convert_type,
  676. mres->ml[0].highest_blocked, mres->ml[0].node);
  677. BUG();
  678. }
  679. BUG_ON(dead_node != dlm->reco.dead_node);
  680. BUG_ON(reco_master != dlm->reco.new_master);
  681. /* lock resources should have already been moved to the
  682. * dlm->reco.resources list. now move items from that list
  683. * to a temp list if the dead owner matches. note that the
  684. * whole cluster recovers only one node at a time, so we
  685. * can safely move UNKNOWN lock resources for each recovery
  686. * session. */
  687. dlm_move_reco_locks_to_list(dlm, &resources, dead_node);
  688. /* now we can begin blasting lockreses without the dlm lock */
  689. /* any errors returned will be due to the new_master dying,
  690. * the dlm_reco_thread should detect this */
  691. list_for_each(iter, &resources) {
  692. res = list_entry (iter, struct dlm_lock_resource, recovering);
  693. ret = dlm_send_one_lockres(dlm, res, mres, reco_master,
  694. DLM_MRES_RECOVERY);
  695. if (ret < 0) {
  696. mlog_errno(ret);
  697. skip_all_done = 1;
  698. break;
  699. }
  700. }
  701. /* move the resources back to the list */
  702. spin_lock(&dlm->spinlock);
  703. list_splice_init(&resources, &dlm->reco.resources);
  704. spin_unlock(&dlm->spinlock);
  705. if (!skip_all_done) {
  706. ret = dlm_send_all_done_msg(dlm, dead_node, reco_master);
  707. if (ret < 0) {
  708. mlog_errno(ret);
  709. }
  710. }
  711. free_page((unsigned long)data);
  712. }
  713. static int dlm_send_all_done_msg(struct dlm_ctxt *dlm, u8 dead_node, u8 send_to)
  714. {
  715. int ret, tmpret;
  716. struct dlm_reco_data_done done_msg;
  717. memset(&done_msg, 0, sizeof(done_msg));
  718. done_msg.node_idx = dlm->node_num;
  719. done_msg.dead_node = dead_node;
  720. mlog(0, "sending DATA DONE message to %u, "
  721. "my node=%u, dead node=%u\n", send_to, done_msg.node_idx,
  722. done_msg.dead_node);
  723. ret = o2net_send_message(DLM_RECO_DATA_DONE_MSG, dlm->key, &done_msg,
  724. sizeof(done_msg), send_to, &tmpret);
  725. if (ret < 0) {
  726. if (!dlm_is_host_down(ret)) {
  727. mlog_errno(ret);
  728. mlog(ML_ERROR, "%s: unknown error sending data-done "
  729. "to %u\n", dlm->name, send_to);
  730. BUG();
  731. }
  732. } else
  733. ret = tmpret;
  734. return ret;
  735. }
  736. int dlm_reco_data_done_handler(struct o2net_msg *msg, u32 len, void *data)
  737. {
  738. struct dlm_ctxt *dlm = data;
  739. struct dlm_reco_data_done *done = (struct dlm_reco_data_done *)msg->buf;
  740. struct list_head *iter;
  741. struct dlm_reco_node_data *ndata = NULL;
  742. int ret = -EINVAL;
  743. if (!dlm_grab(dlm))
  744. return -EINVAL;
  745. mlog(0, "got DATA DONE: dead_node=%u, reco.dead_node=%u, "
  746. "node_idx=%u, this node=%u\n", done->dead_node,
  747. dlm->reco.dead_node, done->node_idx, dlm->node_num);
  748. BUG_ON(done->dead_node != dlm->reco.dead_node);
  749. spin_lock(&dlm_reco_state_lock);
  750. list_for_each(iter, &dlm->reco.node_data) {
  751. ndata = list_entry (iter, struct dlm_reco_node_data, list);
  752. if (ndata->node_num != done->node_idx)
  753. continue;
  754. switch (ndata->state) {
  755. /* should have moved beyond INIT but not to FINALIZE yet */
  756. case DLM_RECO_NODE_DATA_INIT:
  757. case DLM_RECO_NODE_DATA_DEAD:
  758. case DLM_RECO_NODE_DATA_FINALIZE_SENT:
  759. mlog(ML_ERROR, "bad ndata state for node %u:"
  760. " state=%d\n", ndata->node_num,
  761. ndata->state);
  762. BUG();
  763. break;
  764. /* these states are possible at this point, anywhere along
  765. * the line of recovery */
  766. case DLM_RECO_NODE_DATA_DONE:
  767. case DLM_RECO_NODE_DATA_RECEIVING:
  768. case DLM_RECO_NODE_DATA_REQUESTED:
  769. case DLM_RECO_NODE_DATA_REQUESTING:
  770. mlog(0, "node %u is DONE sending "
  771. "recovery data!\n",
  772. ndata->node_num);
  773. ndata->state = DLM_RECO_NODE_DATA_DONE;
  774. ret = 0;
  775. break;
  776. }
  777. }
  778. spin_unlock(&dlm_reco_state_lock);
  779. /* wake the recovery thread, some node is done */
  780. if (!ret)
  781. dlm_kick_recovery_thread(dlm);
  782. if (ret < 0)
  783. mlog(ML_ERROR, "failed to find recovery node data for node "
  784. "%u\n", done->node_idx);
  785. dlm_put(dlm);
  786. mlog(0, "leaving reco data done handler, ret=%d\n", ret);
  787. return ret;
  788. }
  789. static void dlm_move_reco_locks_to_list(struct dlm_ctxt *dlm,
  790. struct list_head *list,
  791. u8 dead_node)
  792. {
  793. struct dlm_lock_resource *res;
  794. struct list_head *iter, *iter2;
  795. struct dlm_lock *lock;
  796. spin_lock(&dlm->spinlock);
  797. list_for_each_safe(iter, iter2, &dlm->reco.resources) {
  798. res = list_entry (iter, struct dlm_lock_resource, recovering);
  799. /* always prune any $RECOVERY entries for dead nodes,
  800. * otherwise hangs can occur during later recovery */
  801. if (dlm_is_recovery_lock(res->lockname.name,
  802. res->lockname.len)) {
  803. spin_lock(&res->spinlock);
  804. list_for_each_entry(lock, &res->granted, list) {
  805. if (lock->ml.node == dead_node) {
  806. mlog(0, "AHA! there was "
  807. "a $RECOVERY lock for dead "
  808. "node %u (%s)!\n",
  809. dead_node, dlm->name);
  810. list_del_init(&lock->list);
  811. dlm_lock_put(lock);
  812. break;
  813. }
  814. }
  815. spin_unlock(&res->spinlock);
  816. continue;
  817. }
  818. if (res->owner == dead_node) {
  819. mlog(0, "found lockres owned by dead node while "
  820. "doing recovery for node %u. sending it.\n",
  821. dead_node);
  822. list_move_tail(&res->recovering, list);
  823. } else if (res->owner == DLM_LOCK_RES_OWNER_UNKNOWN) {
  824. mlog(0, "found UNKNOWN owner while doing recovery "
  825. "for node %u. sending it.\n", dead_node);
  826. list_move_tail(&res->recovering, list);
  827. }
  828. }
  829. spin_unlock(&dlm->spinlock);
  830. }
  831. static inline int dlm_num_locks_in_lockres(struct dlm_lock_resource *res)
  832. {
  833. int total_locks = 0;
  834. struct list_head *iter, *queue = &res->granted;
  835. int i;
  836. for (i=0; i<3; i++) {
  837. list_for_each(iter, queue)
  838. total_locks++;
  839. queue++;
  840. }
  841. return total_locks;
  842. }
  843. static int dlm_send_mig_lockres_msg(struct dlm_ctxt *dlm,
  844. struct dlm_migratable_lockres *mres,
  845. u8 send_to,
  846. struct dlm_lock_resource *res,
  847. int total_locks)
  848. {
  849. u64 mig_cookie = be64_to_cpu(mres->mig_cookie);
  850. int mres_total_locks = be32_to_cpu(mres->total_locks);
  851. int sz, ret = 0, status = 0;
  852. u8 orig_flags = mres->flags,
  853. orig_master = mres->master;
  854. BUG_ON(mres->num_locks > DLM_MAX_MIGRATABLE_LOCKS);
  855. if (!mres->num_locks)
  856. return 0;
  857. sz = sizeof(struct dlm_migratable_lockres) +
  858. (mres->num_locks * sizeof(struct dlm_migratable_lock));
  859. /* add an all-done flag if we reached the last lock */
  860. orig_flags = mres->flags;
  861. BUG_ON(total_locks > mres_total_locks);
  862. if (total_locks == mres_total_locks)
  863. mres->flags |= DLM_MRES_ALL_DONE;
  864. /* send it */
  865. ret = o2net_send_message(DLM_MIG_LOCKRES_MSG, dlm->key, mres,
  866. sz, send_to, &status);
  867. if (ret < 0) {
  868. /* XXX: negative status is not handled.
  869. * this will end up killing this node. */
  870. mlog_errno(ret);
  871. } else {
  872. /* might get an -ENOMEM back here */
  873. ret = status;
  874. if (ret < 0) {
  875. mlog_errno(ret);
  876. if (ret == -EFAULT) {
  877. mlog(ML_ERROR, "node %u told me to kill "
  878. "myself!\n", send_to);
  879. BUG();
  880. }
  881. }
  882. }
  883. /* zero and reinit the message buffer */
  884. dlm_init_migratable_lockres(mres, res->lockname.name,
  885. res->lockname.len, mres_total_locks,
  886. mig_cookie, orig_flags, orig_master);
  887. return ret;
  888. }
  889. static void dlm_init_migratable_lockres(struct dlm_migratable_lockres *mres,
  890. const char *lockname, int namelen,
  891. int total_locks, u64 cookie,
  892. u8 flags, u8 master)
  893. {
  894. /* mres here is one full page */
  895. memset(mres, 0, PAGE_SIZE);
  896. mres->lockname_len = namelen;
  897. memcpy(mres->lockname, lockname, namelen);
  898. mres->num_locks = 0;
  899. mres->total_locks = cpu_to_be32(total_locks);
  900. mres->mig_cookie = cpu_to_be64(cookie);
  901. mres->flags = flags;
  902. mres->master = master;
  903. }
  904. /* returns 1 if this lock fills the network structure,
  905. * 0 otherwise */
  906. static int dlm_add_lock_to_array(struct dlm_lock *lock,
  907. struct dlm_migratable_lockres *mres, int queue)
  908. {
  909. struct dlm_migratable_lock *ml;
  910. int lock_num = mres->num_locks;
  911. ml = &(mres->ml[lock_num]);
  912. ml->cookie = lock->ml.cookie;
  913. ml->type = lock->ml.type;
  914. ml->convert_type = lock->ml.convert_type;
  915. ml->highest_blocked = lock->ml.highest_blocked;
  916. ml->list = queue;
  917. if (lock->lksb) {
  918. ml->flags = lock->lksb->flags;
  919. /* send our current lvb */
  920. if (ml->type == LKM_EXMODE ||
  921. ml->type == LKM_PRMODE) {
  922. /* if it is already set, this had better be a PR
  923. * and it has to match */
  924. if (!dlm_lvb_is_empty(mres->lvb) &&
  925. (ml->type == LKM_EXMODE ||
  926. memcmp(mres->lvb, lock->lksb->lvb, DLM_LVB_LEN))) {
  927. mlog(ML_ERROR, "mismatched lvbs!\n");
  928. __dlm_print_one_lock_resource(lock->lockres);
  929. BUG();
  930. }
  931. memcpy(mres->lvb, lock->lksb->lvb, DLM_LVB_LEN);
  932. }
  933. }
  934. ml->node = lock->ml.node;
  935. mres->num_locks++;
  936. /* we reached the max, send this network message */
  937. if (mres->num_locks == DLM_MAX_MIGRATABLE_LOCKS)
  938. return 1;
  939. return 0;
  940. }
  941. int dlm_send_one_lockres(struct dlm_ctxt *dlm, struct dlm_lock_resource *res,
  942. struct dlm_migratable_lockres *mres,
  943. u8 send_to, u8 flags)
  944. {
  945. struct list_head *queue, *iter;
  946. int total_locks, i;
  947. u64 mig_cookie = 0;
  948. struct dlm_lock *lock;
  949. int ret = 0;
  950. BUG_ON(!(flags & (DLM_MRES_RECOVERY|DLM_MRES_MIGRATION)));
  951. mlog(0, "sending to %u\n", send_to);
  952. total_locks = dlm_num_locks_in_lockres(res);
  953. if (total_locks > DLM_MAX_MIGRATABLE_LOCKS) {
  954. /* rare, but possible */
  955. mlog(0, "argh. lockres has %d locks. this will "
  956. "require more than one network packet to "
  957. "migrate\n", total_locks);
  958. mig_cookie = dlm_get_next_mig_cookie();
  959. }
  960. dlm_init_migratable_lockres(mres, res->lockname.name,
  961. res->lockname.len, total_locks,
  962. mig_cookie, flags, res->owner);
  963. total_locks = 0;
  964. for (i=DLM_GRANTED_LIST; i<=DLM_BLOCKED_LIST; i++) {
  965. queue = dlm_list_idx_to_ptr(res, i);
  966. list_for_each(iter, queue) {
  967. lock = list_entry (iter, struct dlm_lock, list);
  968. /* add another lock. */
  969. total_locks++;
  970. if (!dlm_add_lock_to_array(lock, mres, i))
  971. continue;
  972. /* this filled the lock message,
  973. * we must send it immediately. */
  974. ret = dlm_send_mig_lockres_msg(dlm, mres, send_to,
  975. res, total_locks);
  976. if (ret < 0)
  977. goto error;
  978. }
  979. }
  980. /* flush any remaining locks */
  981. ret = dlm_send_mig_lockres_msg(dlm, mres, send_to, res, total_locks);
  982. if (ret < 0)
  983. goto error;
  984. return ret;
  985. error:
  986. mlog(ML_ERROR, "%s: dlm_send_mig_lockres_msg returned %d\n",
  987. dlm->name, ret);
  988. if (!dlm_is_host_down(ret))
  989. BUG();
  990. mlog(0, "%s: node %u went down while sending %s "
  991. "lockres %.*s\n", dlm->name, send_to,
  992. flags & DLM_MRES_RECOVERY ? "recovery" : "migration",
  993. res->lockname.len, res->lockname.name);
  994. return ret;
  995. }
  996. /*
  997. * this message will contain no more than one page worth of
  998. * recovery data, and it will work on only one lockres.
  999. * there may be many locks in this page, and we may need to wait
  1000. * for additional packets to complete all the locks (rare, but
  1001. * possible).
  1002. */
  1003. /*
  1004. * NOTE: the allocation error cases here are scary
  1005. * we really cannot afford to fail an alloc in recovery
  1006. * do we spin? returning an error only delays the problem really
  1007. */
  1008. int dlm_mig_lockres_handler(struct o2net_msg *msg, u32 len, void *data)
  1009. {
  1010. struct dlm_ctxt *dlm = data;
  1011. struct dlm_migratable_lockres *mres =
  1012. (struct dlm_migratable_lockres *)msg->buf;
  1013. int ret = 0;
  1014. u8 real_master;
  1015. char *buf = NULL;
  1016. struct dlm_work_item *item = NULL;
  1017. struct dlm_lock_resource *res = NULL;
  1018. if (!dlm_grab(dlm))
  1019. return -EINVAL;
  1020. BUG_ON(!(mres->flags & (DLM_MRES_RECOVERY|DLM_MRES_MIGRATION)));
  1021. real_master = mres->master;
  1022. if (real_master == DLM_LOCK_RES_OWNER_UNKNOWN) {
  1023. /* cannot migrate a lockres with no master */
  1024. BUG_ON(!(mres->flags & DLM_MRES_RECOVERY));
  1025. }
  1026. mlog(0, "%s message received from node %u\n",
  1027. (mres->flags & DLM_MRES_RECOVERY) ?
  1028. "recovery" : "migration", mres->master);
  1029. if (mres->flags & DLM_MRES_ALL_DONE)
  1030. mlog(0, "all done flag. all lockres data received!\n");
  1031. ret = -ENOMEM;
  1032. buf = kmalloc(be16_to_cpu(msg->data_len), GFP_KERNEL);
  1033. item = kcalloc(1, sizeof(*item), GFP_KERNEL);
  1034. if (!buf || !item)
  1035. goto leave;
  1036. /* lookup the lock to see if we have a secondary queue for this
  1037. * already... just add the locks in and this will have its owner
  1038. * and RECOVERY flag changed when it completes. */
  1039. res = dlm_lookup_lockres(dlm, mres->lockname, mres->lockname_len);
  1040. if (res) {
  1041. /* this will get a ref on res */
  1042. /* mark it as recovering/migrating and hash it */
  1043. spin_lock(&res->spinlock);
  1044. if (mres->flags & DLM_MRES_RECOVERY) {
  1045. res->state |= DLM_LOCK_RES_RECOVERING;
  1046. } else {
  1047. if (res->state & DLM_LOCK_RES_MIGRATING) {
  1048. /* this is at least the second
  1049. * lockres message */
  1050. mlog(0, "lock %.*s is already migrating\n",
  1051. mres->lockname_len,
  1052. mres->lockname);
  1053. } else if (res->state & DLM_LOCK_RES_RECOVERING) {
  1054. /* caller should BUG */
  1055. mlog(ML_ERROR, "node is attempting to migrate "
  1056. "lock %.*s, but marked as recovering!\n",
  1057. mres->lockname_len, mres->lockname);
  1058. ret = -EFAULT;
  1059. spin_unlock(&res->spinlock);
  1060. goto leave;
  1061. }
  1062. res->state |= DLM_LOCK_RES_MIGRATING;
  1063. }
  1064. spin_unlock(&res->spinlock);
  1065. } else {
  1066. /* need to allocate, just like if it was
  1067. * mastered here normally */
  1068. res = dlm_new_lockres(dlm, mres->lockname, mres->lockname_len);
  1069. if (!res)
  1070. goto leave;
  1071. /* to match the ref that we would have gotten if
  1072. * dlm_lookup_lockres had succeeded */
  1073. dlm_lockres_get(res);
  1074. /* mark it as recovering/migrating and hash it */
  1075. if (mres->flags & DLM_MRES_RECOVERY)
  1076. res->state |= DLM_LOCK_RES_RECOVERING;
  1077. else
  1078. res->state |= DLM_LOCK_RES_MIGRATING;
  1079. spin_lock(&dlm->spinlock);
  1080. __dlm_insert_lockres(dlm, res);
  1081. spin_unlock(&dlm->spinlock);
  1082. /* now that the new lockres is inserted,
  1083. * make it usable by other processes */
  1084. spin_lock(&res->spinlock);
  1085. res->state &= ~DLM_LOCK_RES_IN_PROGRESS;
  1086. spin_unlock(&res->spinlock);
  1087. /* add an extra ref for just-allocated lockres
  1088. * otherwise the lockres will be purged immediately */
  1089. dlm_lockres_get(res);
  1090. }
  1091. /* at this point we have allocated everything we need,
  1092. * and we have a hashed lockres with an extra ref and
  1093. * the proper res->state flags. */
  1094. ret = 0;
  1095. if (mres->master == DLM_LOCK_RES_OWNER_UNKNOWN) {
  1096. /* migration cannot have an unknown master */
  1097. BUG_ON(!(mres->flags & DLM_MRES_RECOVERY));
  1098. mlog(0, "recovery has passed me a lockres with an "
  1099. "unknown owner.. will need to requery: "
  1100. "%.*s\n", mres->lockname_len, mres->lockname);
  1101. } else {
  1102. spin_lock(&res->spinlock);
  1103. dlm_change_lockres_owner(dlm, res, dlm->node_num);
  1104. spin_unlock(&res->spinlock);
  1105. }
  1106. /* queue up work for dlm_mig_lockres_worker */
  1107. dlm_grab(dlm); /* get an extra ref for the work item */
  1108. memcpy(buf, msg->buf, be16_to_cpu(msg->data_len)); /* copy the whole message */
  1109. dlm_init_work_item(dlm, item, dlm_mig_lockres_worker, buf);
  1110. item->u.ml.lockres = res; /* already have a ref */
  1111. item->u.ml.real_master = real_master;
  1112. spin_lock(&dlm->work_lock);
  1113. list_add_tail(&item->list, &dlm->work_list);
  1114. spin_unlock(&dlm->work_lock);
  1115. schedule_work(&dlm->dispatched_work);
  1116. leave:
  1117. dlm_put(dlm);
  1118. if (ret < 0) {
  1119. if (buf)
  1120. kfree(buf);
  1121. if (item)
  1122. kfree(item);
  1123. }
  1124. mlog_exit(ret);
  1125. return ret;
  1126. }
  1127. static void dlm_mig_lockres_worker(struct dlm_work_item *item, void *data)
  1128. {
  1129. struct dlm_ctxt *dlm = data;
  1130. struct dlm_migratable_lockres *mres;
  1131. int ret = 0;
  1132. struct dlm_lock_resource *res;
  1133. u8 real_master;
  1134. dlm = item->dlm;
  1135. mres = (struct dlm_migratable_lockres *)data;
  1136. res = item->u.ml.lockres;
  1137. real_master = item->u.ml.real_master;
  1138. if (real_master == DLM_LOCK_RES_OWNER_UNKNOWN) {
  1139. /* this case is super-rare. only occurs if
  1140. * node death happens during migration. */
  1141. again:
  1142. ret = dlm_lockres_master_requery(dlm, res, &real_master);
  1143. if (ret < 0) {
  1144. mlog(0, "dlm_lockres_master_requery ret=%d\n",
  1145. ret);
  1146. goto again;
  1147. }
  1148. if (real_master == DLM_LOCK_RES_OWNER_UNKNOWN) {
  1149. mlog(0, "lockres %.*s not claimed. "
  1150. "this node will take it.\n",
  1151. res->lockname.len, res->lockname.name);
  1152. } else {
  1153. mlog(0, "master needs to respond to sender "
  1154. "that node %u still owns %.*s\n",
  1155. real_master, res->lockname.len,
  1156. res->lockname.name);
  1157. /* cannot touch this lockres */
  1158. goto leave;
  1159. }
  1160. }
  1161. ret = dlm_process_recovery_data(dlm, res, mres);
  1162. if (ret < 0)
  1163. mlog(0, "dlm_process_recovery_data returned %d\n", ret);
  1164. else
  1165. mlog(0, "dlm_process_recovery_data succeeded\n");
  1166. if ((mres->flags & (DLM_MRES_MIGRATION|DLM_MRES_ALL_DONE)) ==
  1167. (DLM_MRES_MIGRATION|DLM_MRES_ALL_DONE)) {
  1168. ret = dlm_finish_migration(dlm, res, mres->master);
  1169. if (ret < 0)
  1170. mlog_errno(ret);
  1171. }
  1172. leave:
  1173. kfree(data);
  1174. mlog_exit(ret);
  1175. }
  1176. int dlm_lockres_master_requery(struct dlm_ctxt *dlm,
  1177. struct dlm_lock_resource *res, u8 *real_master)
  1178. {
  1179. struct dlm_node_iter iter;
  1180. int nodenum;
  1181. int ret = 0;
  1182. *real_master = DLM_LOCK_RES_OWNER_UNKNOWN;
  1183. /* we only reach here if one of the two nodes in a
  1184. * migration died while the migration was in progress.
  1185. * at this point we need to requery the master. we
  1186. * know that the new_master got as far as creating
  1187. * an mle on at least one node, but we do not know
  1188. * if any nodes had actually cleared the mle and set
  1189. * the master to the new_master. the old master
  1190. * is supposed to set the owner to UNKNOWN in the
  1191. * event of a new_master death, so the only possible
  1192. * responses that we can get from nodes here are
  1193. * that the master is new_master, or that the master
  1194. * is UNKNOWN.
  1195. * if all nodes come back with UNKNOWN then we know
  1196. * the lock needs remastering here.
  1197. * if any node comes back with a valid master, check
  1198. * to see if that master is the one that we are
  1199. * recovering. if so, then the new_master died and
  1200. * we need to remaster this lock. if not, then the
  1201. * new_master survived and that node will respond to
  1202. * other nodes about the owner.
  1203. * if there is an owner, this node needs to dump this
  1204. * lockres and alert the sender that this lockres
  1205. * was rejected. */
  1206. spin_lock(&dlm->spinlock);
  1207. dlm_node_iter_init(dlm->domain_map, &iter);
  1208. spin_unlock(&dlm->spinlock);
  1209. while ((nodenum = dlm_node_iter_next(&iter)) >= 0) {
  1210. /* do not send to self */
  1211. if (nodenum == dlm->node_num)
  1212. continue;
  1213. ret = dlm_do_master_requery(dlm, res, nodenum, real_master);
  1214. if (ret < 0) {
  1215. mlog_errno(ret);
  1216. if (!dlm_is_host_down(ret))
  1217. BUG();
  1218. /* host is down, so answer for that node would be
  1219. * DLM_LOCK_RES_OWNER_UNKNOWN. continue. */
  1220. }
  1221. if (*real_master != DLM_LOCK_RES_OWNER_UNKNOWN) {
  1222. mlog(0, "lock master is %u\n", *real_master);
  1223. break;
  1224. }
  1225. }
  1226. return ret;
  1227. }
  1228. int dlm_do_master_requery(struct dlm_ctxt *dlm, struct dlm_lock_resource *res,
  1229. u8 nodenum, u8 *real_master)
  1230. {
  1231. int ret = -EINVAL;
  1232. struct dlm_master_requery req;
  1233. int status = DLM_LOCK_RES_OWNER_UNKNOWN;
  1234. memset(&req, 0, sizeof(req));
  1235. req.node_idx = dlm->node_num;
  1236. req.namelen = res->lockname.len;
  1237. memcpy(req.name, res->lockname.name, res->lockname.len);
  1238. ret = o2net_send_message(DLM_MASTER_REQUERY_MSG, dlm->key,
  1239. &req, sizeof(req), nodenum, &status);
  1240. /* XXX: negative status not handled properly here. */
  1241. if (ret < 0)
  1242. mlog_errno(ret);
  1243. else {
  1244. BUG_ON(status < 0);
  1245. BUG_ON(status > DLM_LOCK_RES_OWNER_UNKNOWN);
  1246. *real_master = (u8) (status & 0xff);
  1247. mlog(0, "node %u responded to master requery with %u\n",
  1248. nodenum, *real_master);
  1249. ret = 0;
  1250. }
  1251. return ret;
  1252. }
  1253. /* this function cannot error, so unless the sending
  1254. * or receiving of the message failed, the owner can
  1255. * be trusted */
  1256. int dlm_master_requery_handler(struct o2net_msg *msg, u32 len, void *data)
  1257. {
  1258. struct dlm_ctxt *dlm = data;
  1259. struct dlm_master_requery *req = (struct dlm_master_requery *)msg->buf;
  1260. struct dlm_lock_resource *res = NULL;
  1261. unsigned int hash;
  1262. int master = DLM_LOCK_RES_OWNER_UNKNOWN;
  1263. u32 flags = DLM_ASSERT_MASTER_REQUERY;
  1264. if (!dlm_grab(dlm)) {
  1265. /* since the domain has gone away on this
  1266. * node, the proper response is UNKNOWN */
  1267. return master;
  1268. }
  1269. hash = dlm_lockid_hash(req->name, req->namelen);
  1270. spin_lock(&dlm->spinlock);
  1271. res = __dlm_lookup_lockres(dlm, req->name, req->namelen, hash);
  1272. if (res) {
  1273. spin_lock(&res->spinlock);
  1274. master = res->owner;
  1275. if (master == dlm->node_num) {
  1276. int ret = dlm_dispatch_assert_master(dlm, res,
  1277. 0, 0, flags);
  1278. if (ret < 0) {
  1279. mlog_errno(-ENOMEM);
  1280. /* retry!? */
  1281. BUG();
  1282. }
  1283. }
  1284. spin_unlock(&res->spinlock);
  1285. }
  1286. spin_unlock(&dlm->spinlock);
  1287. dlm_put(dlm);
  1288. return master;
  1289. }
  1290. static inline struct list_head *
  1291. dlm_list_num_to_pointer(struct dlm_lock_resource *res, int list_num)
  1292. {
  1293. struct list_head *ret;
  1294. BUG_ON(list_num < 0);
  1295. BUG_ON(list_num > 2);
  1296. ret = &(res->granted);
  1297. ret += list_num;
  1298. return ret;
  1299. }
  1300. /* TODO: do ast flush business
  1301. * TODO: do MIGRATING and RECOVERING spinning
  1302. */
  1303. /*
  1304. * NOTE about in-flight requests during migration:
  1305. *
  1306. * Before attempting the migrate, the master has marked the lockres as
  1307. * MIGRATING and then flushed all of its pending ASTS. So any in-flight
  1308. * requests either got queued before the MIGRATING flag got set, in which
  1309. * case the lock data will reflect the change and a return message is on
  1310. * the way, or the request failed to get in before MIGRATING got set. In
  1311. * this case, the caller will be told to spin and wait for the MIGRATING
  1312. * flag to be dropped, then recheck the master.
  1313. * This holds true for the convert, cancel and unlock cases, and since lvb
  1314. * updates are tied to these same messages, it applies to lvb updates as
  1315. * well. For the lock case, there is no way a lock can be on the master
  1316. * queue and not be on the secondary queue since the lock is always added
  1317. * locally first. This means that the new target node will never be sent
  1318. * a lock that he doesn't already have on the list.
  1319. * In total, this means that the local lock is correct and should not be
  1320. * updated to match the one sent by the master. Any messages sent back
  1321. * from the master before the MIGRATING flag will bring the lock properly
  1322. * up-to-date, and the change will be ordered properly for the waiter.
  1323. * We will *not* attempt to modify the lock underneath the waiter.
  1324. */
  1325. static int dlm_process_recovery_data(struct dlm_ctxt *dlm,
  1326. struct dlm_lock_resource *res,
  1327. struct dlm_migratable_lockres *mres)
  1328. {
  1329. struct dlm_migratable_lock *ml;
  1330. struct list_head *queue;
  1331. struct dlm_lock *newlock = NULL;
  1332. struct dlm_lockstatus *lksb = NULL;
  1333. int ret = 0;
  1334. int i, bad;
  1335. struct list_head *iter;
  1336. struct dlm_lock *lock = NULL;
  1337. mlog(0, "running %d locks for this lockres\n", mres->num_locks);
  1338. for (i=0; i<mres->num_locks; i++) {
  1339. ml = &(mres->ml[i]);
  1340. BUG_ON(ml->highest_blocked != LKM_IVMODE);
  1341. newlock = NULL;
  1342. lksb = NULL;
  1343. queue = dlm_list_num_to_pointer(res, ml->list);
  1344. /* if the lock is for the local node it needs to
  1345. * be moved to the proper location within the queue.
  1346. * do not allocate a new lock structure. */
  1347. if (ml->node == dlm->node_num) {
  1348. /* MIGRATION ONLY! */
  1349. BUG_ON(!(mres->flags & DLM_MRES_MIGRATION));
  1350. spin_lock(&res->spinlock);
  1351. list_for_each(iter, queue) {
  1352. lock = list_entry (iter, struct dlm_lock, list);
  1353. if (lock->ml.cookie != ml->cookie)
  1354. lock = NULL;
  1355. else
  1356. break;
  1357. }
  1358. /* lock is always created locally first, and
  1359. * destroyed locally last. it must be on the list */
  1360. if (!lock) {
  1361. u64 c = ml->cookie;
  1362. mlog(ML_ERROR, "could not find local lock "
  1363. "with cookie %u:%llu!\n",
  1364. dlm_get_lock_cookie_node(c),
  1365. dlm_get_lock_cookie_seq(c));
  1366. BUG();
  1367. }
  1368. BUG_ON(lock->ml.node != ml->node);
  1369. /* see NOTE above about why we do not update
  1370. * to match the master here */
  1371. /* move the lock to its proper place */
  1372. /* do not alter lock refcount. switching lists. */
  1373. list_move_tail(&lock->list, queue);
  1374. spin_unlock(&res->spinlock);
  1375. mlog(0, "just reordered a local lock!\n");
  1376. continue;
  1377. }
  1378. /* lock is for another node. */
  1379. newlock = dlm_new_lock(ml->type, ml->node,
  1380. be64_to_cpu(ml->cookie), NULL);
  1381. if (!newlock) {
  1382. ret = -ENOMEM;
  1383. goto leave;
  1384. }
  1385. lksb = newlock->lksb;
  1386. dlm_lock_attach_lockres(newlock, res);
  1387. if (ml->convert_type != LKM_IVMODE) {
  1388. BUG_ON(queue != &res->converting);
  1389. newlock->ml.convert_type = ml->convert_type;
  1390. }
  1391. lksb->flags |= (ml->flags &
  1392. (DLM_LKSB_PUT_LVB|DLM_LKSB_GET_LVB));
  1393. if (!dlm_lvb_is_empty(mres->lvb)) {
  1394. if (lksb->flags & DLM_LKSB_PUT_LVB) {
  1395. /* other node was trying to update
  1396. * lvb when node died. recreate the
  1397. * lksb with the updated lvb. */
  1398. memcpy(lksb->lvb, mres->lvb, DLM_LVB_LEN);
  1399. } else {
  1400. /* otherwise, the node is sending its
  1401. * most recent valid lvb info */
  1402. BUG_ON(ml->type != LKM_EXMODE &&
  1403. ml->type != LKM_PRMODE);
  1404. if (!dlm_lvb_is_empty(res->lvb) &&
  1405. (ml->type == LKM_EXMODE ||
  1406. memcmp(res->lvb, mres->lvb, DLM_LVB_LEN))) {
  1407. mlog(ML_ERROR, "received bad lvb!\n");
  1408. __dlm_print_one_lock_resource(res);
  1409. BUG();
  1410. }
  1411. memcpy(res->lvb, mres->lvb, DLM_LVB_LEN);
  1412. }
  1413. }
  1414. /* NOTE:
  1415. * wrt lock queue ordering and recovery:
  1416. * 1. order of locks on granted queue is
  1417. * meaningless.
  1418. * 2. order of locks on converting queue is
  1419. * LOST with the node death. sorry charlie.
  1420. * 3. order of locks on the blocked queue is
  1421. * also LOST.
  1422. * order of locks does not affect integrity, it
  1423. * just means that a lock request may get pushed
  1424. * back in line as a result of the node death.
  1425. * also note that for a given node the lock order
  1426. * for its secondary queue locks is preserved
  1427. * relative to each other, but clearly *not*
  1428. * preserved relative to locks from other nodes.
  1429. */
  1430. bad = 0;
  1431. spin_lock(&res->spinlock);
  1432. list_for_each_entry(lock, queue, list) {
  1433. if (lock->ml.cookie == ml->cookie) {
  1434. u64 c = lock->ml.cookie;
  1435. mlog(ML_ERROR, "%s:%.*s: %u:%llu: lock already "
  1436. "exists on this lockres!\n", dlm->name,
  1437. res->lockname.len, res->lockname.name,
  1438. dlm_get_lock_cookie_node(c),
  1439. dlm_get_lock_cookie_seq(c));
  1440. mlog(ML_NOTICE, "sent lock: type=%d, conv=%d, "
  1441. "node=%u, cookie=%u:%llu, queue=%d\n",
  1442. ml->type, ml->convert_type, ml->node,
  1443. dlm_get_lock_cookie_node(ml->cookie),
  1444. dlm_get_lock_cookie_seq(ml->cookie),
  1445. ml->list);
  1446. __dlm_print_one_lock_resource(res);
  1447. bad = 1;
  1448. break;
  1449. }
  1450. }
  1451. if (!bad) {
  1452. dlm_lock_get(newlock);
  1453. list_add_tail(&newlock->list, queue);
  1454. }
  1455. spin_unlock(&res->spinlock);
  1456. }
  1457. mlog(0, "done running all the locks\n");
  1458. leave:
  1459. if (ret < 0) {
  1460. mlog_errno(ret);
  1461. if (newlock)
  1462. dlm_lock_put(newlock);
  1463. }
  1464. mlog_exit(ret);
  1465. return ret;
  1466. }
  1467. void dlm_move_lockres_to_recovery_list(struct dlm_ctxt *dlm,
  1468. struct dlm_lock_resource *res)
  1469. {
  1470. int i;
  1471. struct list_head *queue, *iter, *iter2;
  1472. struct dlm_lock *lock;
  1473. res->state |= DLM_LOCK_RES_RECOVERING;
  1474. if (!list_empty(&res->recovering))
  1475. list_del_init(&res->recovering);
  1476. list_add_tail(&res->recovering, &dlm->reco.resources);
  1477. /* find any pending locks and put them back on proper list */
  1478. for (i=DLM_BLOCKED_LIST; i>=DLM_GRANTED_LIST; i--) {
  1479. queue = dlm_list_idx_to_ptr(res, i);
  1480. list_for_each_safe(iter, iter2, queue) {
  1481. lock = list_entry (iter, struct dlm_lock, list);
  1482. dlm_lock_get(lock);
  1483. if (lock->convert_pending) {
  1484. /* move converting lock back to granted */
  1485. BUG_ON(i != DLM_CONVERTING_LIST);
  1486. mlog(0, "node died with convert pending "
  1487. "on %.*s. move back to granted list.\n",
  1488. res->lockname.len, res->lockname.name);
  1489. dlm_revert_pending_convert(res, lock);
  1490. lock->convert_pending = 0;
  1491. } else if (lock->lock_pending) {
  1492. /* remove pending lock requests completely */
  1493. BUG_ON(i != DLM_BLOCKED_LIST);
  1494. mlog(0, "node died with lock pending "
  1495. "on %.*s. remove from blocked list and skip.\n",
  1496. res->lockname.len, res->lockname.name);
  1497. /* lock will be floating until ref in
  1498. * dlmlock_remote is freed after the network
  1499. * call returns. ok for it to not be on any
  1500. * list since no ast can be called
  1501. * (the master is dead). */
  1502. dlm_revert_pending_lock(res, lock);
  1503. lock->lock_pending = 0;
  1504. } else if (lock->unlock_pending) {
  1505. /* if an unlock was in progress, treat as
  1506. * if this had completed successfully
  1507. * before sending this lock state to the
  1508. * new master. note that the dlm_unlock
  1509. * call is still responsible for calling
  1510. * the unlockast. that will happen after
  1511. * the network call times out. for now,
  1512. * just move lists to prepare the new
  1513. * recovery master. */
  1514. BUG_ON(i != DLM_GRANTED_LIST);
  1515. mlog(0, "node died with unlock pending "
  1516. "on %.*s. remove from blocked list and skip.\n",
  1517. res->lockname.len, res->lockname.name);
  1518. dlm_commit_pending_unlock(res, lock);
  1519. lock->unlock_pending = 0;
  1520. } else if (lock->cancel_pending) {
  1521. /* if a cancel was in progress, treat as
  1522. * if this had completed successfully
  1523. * before sending this lock state to the
  1524. * new master */
  1525. BUG_ON(i != DLM_CONVERTING_LIST);
  1526. mlog(0, "node died with cancel pending "
  1527. "on %.*s. move back to granted list.\n",
  1528. res->lockname.len, res->lockname.name);
  1529. dlm_commit_pending_cancel(res, lock);
  1530. lock->cancel_pending = 0;
  1531. }
  1532. dlm_lock_put(lock);
  1533. }
  1534. }
  1535. }
  1536. /* removes all recovered locks from the recovery list.
  1537. * sets the res->owner to the new master.
  1538. * unsets the RECOVERY flag and wakes waiters. */
  1539. static void dlm_finish_local_lockres_recovery(struct dlm_ctxt *dlm,
  1540. u8 dead_node, u8 new_master)
  1541. {
  1542. int i;
  1543. struct list_head *iter, *iter2;
  1544. struct hlist_node *hash_iter;
  1545. struct hlist_head *bucket;
  1546. struct dlm_lock_resource *res;
  1547. mlog_entry_void();
  1548. assert_spin_locked(&dlm->spinlock);
  1549. list_for_each_safe(iter, iter2, &dlm->reco.resources) {
  1550. res = list_entry (iter, struct dlm_lock_resource, recovering);
  1551. if (res->owner == dead_node) {
  1552. list_del_init(&res->recovering);
  1553. spin_lock(&res->spinlock);
  1554. dlm_change_lockres_owner(dlm, res, new_master);
  1555. res->state &= ~DLM_LOCK_RES_RECOVERING;
  1556. __dlm_dirty_lockres(dlm, res);
  1557. spin_unlock(&res->spinlock);
  1558. wake_up(&res->wq);
  1559. }
  1560. }
  1561. /* this will become unnecessary eventually, but
  1562. * for now we need to run the whole hash, clear
  1563. * the RECOVERING state and set the owner
  1564. * if necessary */
  1565. for (i = 0; i < DLM_HASH_BUCKETS; i++) {
  1566. bucket = dlm_lockres_hash(dlm, i);
  1567. hlist_for_each_entry(res, hash_iter, bucket, hash_node) {
  1568. if (res->state & DLM_LOCK_RES_RECOVERING) {
  1569. if (res->owner == dead_node) {
  1570. mlog(0, "(this=%u) res %.*s owner=%u "
  1571. "was not on recovering list, but "
  1572. "clearing state anyway\n",
  1573. dlm->node_num, res->lockname.len,
  1574. res->lockname.name, new_master);
  1575. } else if (res->owner == dlm->node_num) {
  1576. mlog(0, "(this=%u) res %.*s owner=%u "
  1577. "was not on recovering list, "
  1578. "owner is THIS node, clearing\n",
  1579. dlm->node_num, res->lockname.len,
  1580. res->lockname.name, new_master);
  1581. } else
  1582. continue;
  1583. if (!list_empty(&res->recovering)) {
  1584. mlog(0, "%s:%.*s: lockres was "
  1585. "marked RECOVERING, owner=%u\n",
  1586. dlm->name, res->lockname.len,
  1587. res->lockname.name, res->owner);
  1588. list_del_init(&res->recovering);
  1589. }
  1590. spin_lock(&res->spinlock);
  1591. dlm_change_lockres_owner(dlm, res, new_master);
  1592. res->state &= ~DLM_LOCK_RES_RECOVERING;
  1593. __dlm_dirty_lockres(dlm, res);
  1594. spin_unlock(&res->spinlock);
  1595. wake_up(&res->wq);
  1596. }
  1597. }
  1598. }
  1599. }
  1600. static inline int dlm_lvb_needs_invalidation(struct dlm_lock *lock, int local)
  1601. {
  1602. if (local) {
  1603. if (lock->ml.type != LKM_EXMODE &&
  1604. lock->ml.type != LKM_PRMODE)
  1605. return 1;
  1606. } else if (lock->ml.type == LKM_EXMODE)
  1607. return 1;
  1608. return 0;
  1609. }
  1610. static void dlm_revalidate_lvb(struct dlm_ctxt *dlm,
  1611. struct dlm_lock_resource *res, u8 dead_node)
  1612. {
  1613. struct list_head *iter, *queue;
  1614. struct dlm_lock *lock;
  1615. int blank_lvb = 0, local = 0;
  1616. int i;
  1617. u8 search_node;
  1618. assert_spin_locked(&dlm->spinlock);
  1619. assert_spin_locked(&res->spinlock);
  1620. if (res->owner == dlm->node_num)
  1621. /* if this node owned the lockres, and if the dead node
  1622. * had an EX when he died, blank out the lvb */
  1623. search_node = dead_node;
  1624. else {
  1625. /* if this is a secondary lockres, and we had no EX or PR
  1626. * locks granted, we can no longer trust the lvb */
  1627. search_node = dlm->node_num;
  1628. local = 1; /* check local state for valid lvb */
  1629. }
  1630. for (i=DLM_GRANTED_LIST; i<=DLM_CONVERTING_LIST; i++) {
  1631. queue = dlm_list_idx_to_ptr(res, i);
  1632. list_for_each(iter, queue) {
  1633. lock = list_entry (iter, struct dlm_lock, list);
  1634. if (lock->ml.node == search_node) {
  1635. if (dlm_lvb_needs_invalidation(lock, local)) {
  1636. /* zero the lksb lvb and lockres lvb */
  1637. blank_lvb = 1;
  1638. memset(lock->lksb->lvb, 0, DLM_LVB_LEN);
  1639. }
  1640. }
  1641. }
  1642. }
  1643. if (blank_lvb) {
  1644. mlog(0, "clearing %.*s lvb, dead node %u had EX\n",
  1645. res->lockname.len, res->lockname.name, dead_node);
  1646. memset(res->lvb, 0, DLM_LVB_LEN);
  1647. }
  1648. }
  1649. static void dlm_free_dead_locks(struct dlm_ctxt *dlm,
  1650. struct dlm_lock_resource *res, u8 dead_node)
  1651. {
  1652. struct list_head *iter, *tmpiter;
  1653. struct dlm_lock *lock;
  1654. /* this node is the lockres master:
  1655. * 1) remove any stale locks for the dead node
  1656. * 2) if the dead node had an EX when he died, blank out the lvb
  1657. */
  1658. assert_spin_locked(&dlm->spinlock);
  1659. assert_spin_locked(&res->spinlock);
  1660. /* TODO: check pending_asts, pending_basts here */
  1661. list_for_each_safe(iter, tmpiter, &res->granted) {
  1662. lock = list_entry (iter, struct dlm_lock, list);
  1663. if (lock->ml.node == dead_node) {
  1664. list_del_init(&lock->list);
  1665. dlm_lock_put(lock);
  1666. }
  1667. }
  1668. list_for_each_safe(iter, tmpiter, &res->converting) {
  1669. lock = list_entry (iter, struct dlm_lock, list);
  1670. if (lock->ml.node == dead_node) {
  1671. list_del_init(&lock->list);
  1672. dlm_lock_put(lock);
  1673. }
  1674. }
  1675. list_for_each_safe(iter, tmpiter, &res->blocked) {
  1676. lock = list_entry (iter, struct dlm_lock, list);
  1677. if (lock->ml.node == dead_node) {
  1678. list_del_init(&lock->list);
  1679. dlm_lock_put(lock);
  1680. }
  1681. }
  1682. /* do not kick thread yet */
  1683. __dlm_dirty_lockres(dlm, res);
  1684. }
  1685. /* if this node is the recovery master, and there are no
  1686. * locks for a given lockres owned by this node that are in
  1687. * either PR or EX mode, zero out the lvb before requesting.
  1688. *
  1689. */
  1690. static void dlm_do_local_recovery_cleanup(struct dlm_ctxt *dlm, u8 dead_node)
  1691. {
  1692. struct hlist_node *iter;
  1693. struct dlm_lock_resource *res;
  1694. int i;
  1695. struct hlist_head *bucket;
  1696. struct dlm_lock *lock;
  1697. /* purge any stale mles */
  1698. dlm_clean_master_list(dlm, dead_node);
  1699. /*
  1700. * now clean up all lock resources. there are two rules:
  1701. *
  1702. * 1) if the dead node was the master, move the lockres
  1703. * to the recovering list. set the RECOVERING flag.
  1704. * this lockres needs to be cleaned up before it can
  1705. * be used further.
  1706. *
  1707. * 2) if this node was the master, remove all locks from
  1708. * each of the lockres queues that were owned by the
  1709. * dead node. once recovery finishes, the dlm thread
  1710. * can be kicked again to see if any ASTs or BASTs
  1711. * need to be fired as a result.
  1712. */
  1713. for (i = 0; i < DLM_HASH_BUCKETS; i++) {
  1714. bucket = dlm_lockres_hash(dlm, i);
  1715. hlist_for_each_entry(res, iter, bucket, hash_node) {
  1716. /* always prune any $RECOVERY entries for dead nodes,
  1717. * otherwise hangs can occur during later recovery */
  1718. if (dlm_is_recovery_lock(res->lockname.name,
  1719. res->lockname.len)) {
  1720. spin_lock(&res->spinlock);
  1721. list_for_each_entry(lock, &res->granted, list) {
  1722. if (lock->ml.node == dead_node) {
  1723. mlog(0, "AHA! there was "
  1724. "a $RECOVERY lock for dead "
  1725. "node %u (%s)!\n",
  1726. dead_node, dlm->name);
  1727. list_del_init(&lock->list);
  1728. dlm_lock_put(lock);
  1729. break;
  1730. }
  1731. }
  1732. spin_unlock(&res->spinlock);
  1733. continue;
  1734. }
  1735. spin_lock(&res->spinlock);
  1736. /* zero the lvb if necessary */
  1737. dlm_revalidate_lvb(dlm, res, dead_node);
  1738. if (res->owner == dead_node)
  1739. dlm_move_lockres_to_recovery_list(dlm, res);
  1740. else if (res->owner == dlm->node_num) {
  1741. dlm_free_dead_locks(dlm, res, dead_node);
  1742. __dlm_lockres_calc_usage(dlm, res);
  1743. }
  1744. spin_unlock(&res->spinlock);
  1745. }
  1746. }
  1747. }
  1748. static void __dlm_hb_node_down(struct dlm_ctxt *dlm, int idx)
  1749. {
  1750. assert_spin_locked(&dlm->spinlock);
  1751. /* check to see if the node is already considered dead */
  1752. if (!test_bit(idx, dlm->live_nodes_map)) {
  1753. mlog(0, "for domain %s, node %d is already dead. "
  1754. "another node likely did recovery already.\n",
  1755. dlm->name, idx);
  1756. return;
  1757. }
  1758. /* check to see if we do not care about this node */
  1759. if (!test_bit(idx, dlm->domain_map)) {
  1760. /* This also catches the case that we get a node down
  1761. * but haven't joined the domain yet. */
  1762. mlog(0, "node %u already removed from domain!\n", idx);
  1763. return;
  1764. }
  1765. clear_bit(idx, dlm->live_nodes_map);
  1766. /* Clean up join state on node death. */
  1767. if (dlm->joining_node == idx) {
  1768. mlog(0, "Clearing join state for node %u\n", idx);
  1769. __dlm_set_joining_node(dlm, DLM_LOCK_RES_OWNER_UNKNOWN);
  1770. }
  1771. /* make sure local cleanup occurs before the heartbeat events */
  1772. if (!test_bit(idx, dlm->recovery_map))
  1773. dlm_do_local_recovery_cleanup(dlm, idx);
  1774. /* notify anything attached to the heartbeat events */
  1775. dlm_hb_event_notify_attached(dlm, idx, 0);
  1776. mlog(0, "node %u being removed from domain map!\n", idx);
  1777. clear_bit(idx, dlm->domain_map);
  1778. /* wake up migration waiters if a node goes down.
  1779. * perhaps later we can genericize this for other waiters. */
  1780. wake_up(&dlm->migration_wq);
  1781. if (test_bit(idx, dlm->recovery_map))
  1782. mlog(0, "domain %s, node %u already added "
  1783. "to recovery map!\n", dlm->name, idx);
  1784. else
  1785. set_bit(idx, dlm->recovery_map);
  1786. }
  1787. void dlm_hb_node_down_cb(struct o2nm_node *node, int idx, void *data)
  1788. {
  1789. struct dlm_ctxt *dlm = data;
  1790. if (!dlm_grab(dlm))
  1791. return;
  1792. spin_lock(&dlm->spinlock);
  1793. __dlm_hb_node_down(dlm, idx);
  1794. spin_unlock(&dlm->spinlock);
  1795. dlm_put(dlm);
  1796. }
  1797. void dlm_hb_node_up_cb(struct o2nm_node *node, int idx, void *data)
  1798. {
  1799. struct dlm_ctxt *dlm = data;
  1800. if (!dlm_grab(dlm))
  1801. return;
  1802. spin_lock(&dlm->spinlock);
  1803. set_bit(idx, dlm->live_nodes_map);
  1804. /* do NOT notify mle attached to the heartbeat events.
  1805. * new nodes are not interesting in mastery until joined. */
  1806. spin_unlock(&dlm->spinlock);
  1807. dlm_put(dlm);
  1808. }
  1809. static void dlm_reco_ast(void *astdata)
  1810. {
  1811. struct dlm_ctxt *dlm = astdata;
  1812. mlog(0, "ast for recovery lock fired!, this=%u, dlm=%s\n",
  1813. dlm->node_num, dlm->name);
  1814. }
  1815. static void dlm_reco_bast(void *astdata, int blocked_type)
  1816. {
  1817. struct dlm_ctxt *dlm = astdata;
  1818. mlog(0, "bast for recovery lock fired!, this=%u, dlm=%s\n",
  1819. dlm->node_num, dlm->name);
  1820. }
  1821. static void dlm_reco_unlock_ast(void *astdata, enum dlm_status st)
  1822. {
  1823. mlog(0, "unlockast for recovery lock fired!\n");
  1824. }
  1825. /*
  1826. * dlm_pick_recovery_master will continually attempt to use
  1827. * dlmlock() on the special "$RECOVERY" lockres with the
  1828. * LKM_NOQUEUE flag to get an EX. every thread that enters
  1829. * this function on each node racing to become the recovery
  1830. * master will not stop attempting this until either:
  1831. * a) this node gets the EX (and becomes the recovery master),
  1832. * or b) dlm->reco.new_master gets set to some nodenum
  1833. * != O2NM_INVALID_NODE_NUM (another node will do the reco).
  1834. * so each time a recovery master is needed, the entire cluster
  1835. * will sync at this point. if the new master dies, that will
  1836. * be detected in dlm_do_recovery */
  1837. static int dlm_pick_recovery_master(struct dlm_ctxt *dlm)
  1838. {
  1839. enum dlm_status ret;
  1840. struct dlm_lockstatus lksb;
  1841. int status = -EINVAL;
  1842. mlog(0, "starting recovery of %s at %lu, dead=%u, this=%u\n",
  1843. dlm->name, jiffies, dlm->reco.dead_node, dlm->node_num);
  1844. again:
  1845. memset(&lksb, 0, sizeof(lksb));
  1846. ret = dlmlock(dlm, LKM_EXMODE, &lksb, LKM_NOQUEUE|LKM_RECOVERY,
  1847. DLM_RECOVERY_LOCK_NAME, dlm_reco_ast, dlm, dlm_reco_bast);
  1848. mlog(0, "%s: dlmlock($RECOVERY) returned %d, lksb=%d\n",
  1849. dlm->name, ret, lksb.status);
  1850. if (ret == DLM_NORMAL) {
  1851. mlog(0, "dlm=%s dlmlock says I got it (this=%u)\n",
  1852. dlm->name, dlm->node_num);
  1853. /* got the EX lock. check to see if another node
  1854. * just became the reco master */
  1855. if (dlm_reco_master_ready(dlm)) {
  1856. mlog(0, "%s: got reco EX lock, but %u will "
  1857. "do the recovery\n", dlm->name,
  1858. dlm->reco.new_master);
  1859. status = -EEXIST;
  1860. } else {
  1861. status = 0;
  1862. /* see if recovery was already finished elsewhere */
  1863. spin_lock(&dlm->spinlock);
  1864. if (dlm->reco.dead_node == O2NM_INVALID_NODE_NUM) {
  1865. status = -EINVAL;
  1866. mlog(0, "%s: got reco EX lock, but "
  1867. "node got recovered already\n", dlm->name);
  1868. if (dlm->reco.new_master != O2NM_INVALID_NODE_NUM) {
  1869. mlog(ML_ERROR, "%s: new master is %u "
  1870. "but no dead node!\n",
  1871. dlm->name, dlm->reco.new_master);
  1872. BUG();
  1873. }
  1874. }
  1875. spin_unlock(&dlm->spinlock);
  1876. }
  1877. /* if this node has actually become the recovery master,
  1878. * set the master and send the messages to begin recovery */
  1879. if (!status) {
  1880. mlog(0, "%s: dead=%u, this=%u, sending "
  1881. "begin_reco now\n", dlm->name,
  1882. dlm->reco.dead_node, dlm->node_num);
  1883. status = dlm_send_begin_reco_message(dlm,
  1884. dlm->reco.dead_node);
  1885. /* this always succeeds */
  1886. BUG_ON(status);
  1887. /* set the new_master to this node */
  1888. spin_lock(&dlm->spinlock);
  1889. dlm_set_reco_master(dlm, dlm->node_num);
  1890. spin_unlock(&dlm->spinlock);
  1891. }
  1892. /* recovery lock is a special case. ast will not get fired,
  1893. * so just go ahead and unlock it. */
  1894. ret = dlmunlock(dlm, &lksb, 0, dlm_reco_unlock_ast, dlm);
  1895. if (ret == DLM_DENIED) {
  1896. mlog(0, "got DLM_DENIED, trying LKM_CANCEL\n");
  1897. ret = dlmunlock(dlm, &lksb, LKM_CANCEL, dlm_reco_unlock_ast, dlm);
  1898. }
  1899. if (ret != DLM_NORMAL) {
  1900. /* this would really suck. this could only happen
  1901. * if there was a network error during the unlock
  1902. * because of node death. this means the unlock
  1903. * is actually "done" and the lock structure is
  1904. * even freed. we can continue, but only
  1905. * because this specific lock name is special. */
  1906. mlog(ML_ERROR, "dlmunlock returned %d\n", ret);
  1907. }
  1908. } else if (ret == DLM_NOTQUEUED) {
  1909. mlog(0, "dlm=%s dlmlock says another node got it (this=%u)\n",
  1910. dlm->name, dlm->node_num);
  1911. /* another node is master. wait on
  1912. * reco.new_master != O2NM_INVALID_NODE_NUM
  1913. * for at most one second */
  1914. wait_event_timeout(dlm->dlm_reco_thread_wq,
  1915. dlm_reco_master_ready(dlm),
  1916. msecs_to_jiffies(1000));
  1917. if (!dlm_reco_master_ready(dlm)) {
  1918. mlog(0, "%s: reco master taking awhile\n",
  1919. dlm->name);
  1920. goto again;
  1921. }
  1922. /* another node has informed this one that it is reco master */
  1923. mlog(0, "%s: reco master %u is ready to recover %u\n",
  1924. dlm->name, dlm->reco.new_master, dlm->reco.dead_node);
  1925. status = -EEXIST;
  1926. } else {
  1927. struct dlm_lock_resource *res;
  1928. /* dlmlock returned something other than NOTQUEUED or NORMAL */
  1929. mlog(ML_ERROR, "%s: got %s from dlmlock($RECOVERY), "
  1930. "lksb.status=%s\n", dlm->name, dlm_errname(ret),
  1931. dlm_errname(lksb.status));
  1932. res = dlm_lookup_lockres(dlm, DLM_RECOVERY_LOCK_NAME,
  1933. DLM_RECOVERY_LOCK_NAME_LEN);
  1934. if (res) {
  1935. dlm_print_one_lock_resource(res);
  1936. dlm_lockres_put(res);
  1937. } else {
  1938. mlog(ML_ERROR, "recovery lock not found\n");
  1939. }
  1940. BUG();
  1941. }
  1942. return status;
  1943. }
  1944. static int dlm_send_begin_reco_message(struct dlm_ctxt *dlm, u8 dead_node)
  1945. {
  1946. struct dlm_begin_reco br;
  1947. int ret = 0;
  1948. struct dlm_node_iter iter;
  1949. int nodenum;
  1950. int status;
  1951. mlog_entry("%u\n", dead_node);
  1952. mlog(0, "dead node is %u\n", dead_node);
  1953. spin_lock(&dlm->spinlock);
  1954. dlm_node_iter_init(dlm->domain_map, &iter);
  1955. spin_unlock(&dlm->spinlock);
  1956. clear_bit(dead_node, iter.node_map);
  1957. memset(&br, 0, sizeof(br));
  1958. br.node_idx = dlm->node_num;
  1959. br.dead_node = dead_node;
  1960. while ((nodenum = dlm_node_iter_next(&iter)) >= 0) {
  1961. ret = 0;
  1962. if (nodenum == dead_node) {
  1963. mlog(0, "not sending begin reco to dead node "
  1964. "%u\n", dead_node);
  1965. continue;
  1966. }
  1967. if (nodenum == dlm->node_num) {
  1968. mlog(0, "not sending begin reco to self\n");
  1969. continue;
  1970. }
  1971. retry:
  1972. ret = -EINVAL;
  1973. mlog(0, "attempting to send begin reco msg to %d\n",
  1974. nodenum);
  1975. ret = o2net_send_message(DLM_BEGIN_RECO_MSG, dlm->key,
  1976. &br, sizeof(br), nodenum, &status);
  1977. /* negative status is handled ok by caller here */
  1978. if (ret >= 0)
  1979. ret = status;
  1980. if (dlm_is_host_down(ret)) {
  1981. /* node is down. not involved in recovery
  1982. * so just keep going */
  1983. mlog(0, "%s: node %u was down when sending "
  1984. "begin reco msg (%d)\n", dlm->name, nodenum, ret);
  1985. ret = 0;
  1986. }
  1987. if (ret < 0) {
  1988. struct dlm_lock_resource *res;
  1989. /* this is now a serious problem, possibly ENOMEM
  1990. * in the network stack. must retry */
  1991. mlog_errno(ret);
  1992. mlog(ML_ERROR, "begin reco of dlm %s to node %u "
  1993. " returned %d\n", dlm->name, nodenum, ret);
  1994. res = dlm_lookup_lockres(dlm, DLM_RECOVERY_LOCK_NAME,
  1995. DLM_RECOVERY_LOCK_NAME_LEN);
  1996. if (res) {
  1997. dlm_print_one_lock_resource(res);
  1998. dlm_lockres_put(res);
  1999. } else {
  2000. mlog(ML_ERROR, "recovery lock not found\n");
  2001. }
  2002. /* sleep for a bit in hopes that we can avoid
  2003. * another ENOMEM */
  2004. msleep(100);
  2005. goto retry;
  2006. }
  2007. }
  2008. return ret;
  2009. }
  2010. int dlm_begin_reco_handler(struct o2net_msg *msg, u32 len, void *data)
  2011. {
  2012. struct dlm_ctxt *dlm = data;
  2013. struct dlm_begin_reco *br = (struct dlm_begin_reco *)msg->buf;
  2014. /* ok to return 0, domain has gone away */
  2015. if (!dlm_grab(dlm))
  2016. return 0;
  2017. mlog(0, "node %u wants to recover node %u\n",
  2018. br->node_idx, br->dead_node);
  2019. dlm_fire_domain_eviction_callbacks(dlm, br->dead_node);
  2020. spin_lock(&dlm->spinlock);
  2021. if (dlm->reco.new_master != O2NM_INVALID_NODE_NUM) {
  2022. if (test_bit(dlm->reco.new_master, dlm->recovery_map)) {
  2023. mlog(0, "%s: new_master %u died, changing "
  2024. "to %u\n", dlm->name, dlm->reco.new_master,
  2025. br->node_idx);
  2026. } else {
  2027. mlog(0, "%s: new_master %u NOT DEAD, changing "
  2028. "to %u\n", dlm->name, dlm->reco.new_master,
  2029. br->node_idx);
  2030. /* may not have seen the new master as dead yet */
  2031. }
  2032. }
  2033. if (dlm->reco.dead_node != O2NM_INVALID_NODE_NUM) {
  2034. mlog(ML_NOTICE, "%s: dead_node previously set to %u, "
  2035. "node %u changing it to %u\n", dlm->name,
  2036. dlm->reco.dead_node, br->node_idx, br->dead_node);
  2037. }
  2038. dlm_set_reco_master(dlm, br->node_idx);
  2039. dlm_set_reco_dead_node(dlm, br->dead_node);
  2040. if (!test_bit(br->dead_node, dlm->recovery_map)) {
  2041. mlog(0, "recovery master %u sees %u as dead, but this "
  2042. "node has not yet. marking %u as dead\n",
  2043. br->node_idx, br->dead_node, br->dead_node);
  2044. if (!test_bit(br->dead_node, dlm->domain_map) ||
  2045. !test_bit(br->dead_node, dlm->live_nodes_map))
  2046. mlog(0, "%u not in domain/live_nodes map "
  2047. "so setting it in reco map manually\n",
  2048. br->dead_node);
  2049. /* force the recovery cleanup in __dlm_hb_node_down
  2050. * both of these will be cleared in a moment */
  2051. set_bit(br->dead_node, dlm->domain_map);
  2052. set_bit(br->dead_node, dlm->live_nodes_map);
  2053. __dlm_hb_node_down(dlm, br->dead_node);
  2054. }
  2055. spin_unlock(&dlm->spinlock);
  2056. dlm_kick_recovery_thread(dlm);
  2057. dlm_put(dlm);
  2058. return 0;
  2059. }
  2060. static int dlm_send_finalize_reco_message(struct dlm_ctxt *dlm)
  2061. {
  2062. int ret = 0;
  2063. struct dlm_finalize_reco fr;
  2064. struct dlm_node_iter iter;
  2065. int nodenum;
  2066. int status;
  2067. mlog(0, "finishing recovery for node %s:%u\n",
  2068. dlm->name, dlm->reco.dead_node);
  2069. spin_lock(&dlm->spinlock);
  2070. dlm_node_iter_init(dlm->domain_map, &iter);
  2071. spin_unlock(&dlm->spinlock);
  2072. memset(&fr, 0, sizeof(fr));
  2073. fr.node_idx = dlm->node_num;
  2074. fr.dead_node = dlm->reco.dead_node;
  2075. while ((nodenum = dlm_node_iter_next(&iter)) >= 0) {
  2076. if (nodenum == dlm->node_num)
  2077. continue;
  2078. ret = o2net_send_message(DLM_FINALIZE_RECO_MSG, dlm->key,
  2079. &fr, sizeof(fr), nodenum, &status);
  2080. if (ret >= 0) {
  2081. ret = status;
  2082. if (dlm_is_host_down(ret)) {
  2083. /* this has no effect on this recovery
  2084. * session, so set the status to zero to
  2085. * finish out the last recovery */
  2086. mlog(ML_ERROR, "node %u went down after this "
  2087. "node finished recovery.\n", nodenum);
  2088. ret = 0;
  2089. }
  2090. }
  2091. if (ret < 0) {
  2092. mlog_errno(ret);
  2093. break;
  2094. }
  2095. }
  2096. return ret;
  2097. }
  2098. int dlm_finalize_reco_handler(struct o2net_msg *msg, u32 len, void *data)
  2099. {
  2100. struct dlm_ctxt *dlm = data;
  2101. struct dlm_finalize_reco *fr = (struct dlm_finalize_reco *)msg->buf;
  2102. /* ok to return 0, domain has gone away */
  2103. if (!dlm_grab(dlm))
  2104. return 0;
  2105. mlog(0, "node %u finalizing recovery of node %u\n",
  2106. fr->node_idx, fr->dead_node);
  2107. spin_lock(&dlm->spinlock);
  2108. if (dlm->reco.new_master != fr->node_idx) {
  2109. mlog(ML_ERROR, "node %u sent recovery finalize msg, but node "
  2110. "%u is supposed to be the new master, dead=%u\n",
  2111. fr->node_idx, dlm->reco.new_master, fr->dead_node);
  2112. BUG();
  2113. }
  2114. if (dlm->reco.dead_node != fr->dead_node) {
  2115. mlog(ML_ERROR, "node %u sent recovery finalize msg for dead "
  2116. "node %u, but node %u is supposed to be dead\n",
  2117. fr->node_idx, fr->dead_node, dlm->reco.dead_node);
  2118. BUG();
  2119. }
  2120. dlm_finish_local_lockres_recovery(dlm, fr->dead_node, fr->node_idx);
  2121. spin_unlock(&dlm->spinlock);
  2122. dlm_reset_recovery(dlm);
  2123. dlm_kick_recovery_thread(dlm);
  2124. dlm_put(dlm);
  2125. return 0;
  2126. }