dlmrecovery.c 77 KB

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