dlmrecovery.c 85 KB

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