dlmrecovery.c 85 KB

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