dlmrecovery.c 88 KB

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