dlmrecovery.c 78 KB

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