dlmrecovery.c 78 KB

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