dlmrecovery.c 78 KB

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