dlmrecovery.c 86 KB

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