dlmrecovery.c 82 KB

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