dlmrecovery.c 77 KB

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