dlmrecovery.c 82 KB

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