dlmrecovery.c 78 KB

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