dlmrecovery.c 78 KB

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