blob: fb3e2b0817f17cc647c532ae492b30af57af36b9 [file] [log] [blame]
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001/* -*- 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
27
28#include <linux/module.h>
29#include <linux/fs.h>
30#include <linux/types.h>
31#include <linux/slab.h>
32#include <linux/highmem.h>
33#include <linux/utsname.h>
34#include <linux/init.h>
35#include <linux/sysctl.h>
36#include <linux/random.h>
37#include <linux/blkdev.h>
38#include <linux/socket.h>
39#include <linux/inet.h>
40#include <linux/timer.h>
41#include <linux/kthread.h>
Adrian Bunkb4c7f532006-01-14 20:55:10 +010042#include <linux/delay.h>
Kurt Hackel6714d8e2005-12-15 14:31:23 -080043
44
45#include "cluster/heartbeat.h"
46#include "cluster/nodemanager.h"
47#include "cluster/tcp.h"
48
49#include "dlmapi.h"
50#include "dlmcommon.h"
51#include "dlmdomain.h"
52
53#define MLOG_MASK_PREFIX (ML_DLM|ML_DLM_RECOVERY)
54#include "cluster/masklog.h"
55
56static void dlm_do_local_recovery_cleanup(struct dlm_ctxt *dlm, u8 dead_node);
57
58static int dlm_recovery_thread(void *data);
59void dlm_complete_recovery_thread(struct dlm_ctxt *dlm);
60int dlm_launch_recovery_thread(struct dlm_ctxt *dlm);
Kurt Hackelc03872f2006-03-06 14:08:49 -080061void dlm_kick_recovery_thread(struct dlm_ctxt *dlm);
Kurt Hackel6714d8e2005-12-15 14:31:23 -080062static int dlm_do_recovery(struct dlm_ctxt *dlm);
63
64static int dlm_pick_recovery_master(struct dlm_ctxt *dlm);
65static int dlm_remaster_locks(struct dlm_ctxt *dlm, u8 dead_node);
66static int dlm_init_recovery_area(struct dlm_ctxt *dlm, u8 dead_node);
67static int dlm_request_all_locks(struct dlm_ctxt *dlm,
68 u8 request_from, u8 dead_node);
69static void dlm_destroy_recovery_area(struct dlm_ctxt *dlm, u8 dead_node);
70
71static inline int dlm_num_locks_in_lockres(struct dlm_lock_resource *res);
72static void dlm_init_migratable_lockres(struct dlm_migratable_lockres *mres,
73 const char *lockname, int namelen,
74 int total_locks, u64 cookie,
75 u8 flags, u8 master);
76static int dlm_send_mig_lockres_msg(struct dlm_ctxt *dlm,
77 struct dlm_migratable_lockres *mres,
78 u8 send_to,
79 struct dlm_lock_resource *res,
80 int total_locks);
Kurt Hackel6714d8e2005-12-15 14:31:23 -080081static int dlm_process_recovery_data(struct dlm_ctxt *dlm,
82 struct dlm_lock_resource *res,
83 struct dlm_migratable_lockres *mres);
Kurt Hackel6714d8e2005-12-15 14:31:23 -080084static int dlm_send_finalize_reco_message(struct dlm_ctxt *dlm);
85static int dlm_send_all_done_msg(struct dlm_ctxt *dlm,
86 u8 dead_node, u8 send_to);
87static int dlm_send_begin_reco_message(struct dlm_ctxt *dlm, u8 dead_node);
88static void dlm_move_reco_locks_to_list(struct dlm_ctxt *dlm,
89 struct list_head *list, u8 dead_node);
90static void dlm_finish_local_lockres_recovery(struct dlm_ctxt *dlm,
91 u8 dead_node, u8 new_master);
92static void dlm_reco_ast(void *astdata);
93static void dlm_reco_bast(void *astdata, int blocked_type);
94static void dlm_reco_unlock_ast(void *astdata, enum dlm_status st);
95static void dlm_request_all_locks_worker(struct dlm_work_item *item,
96 void *data);
97static void dlm_mig_lockres_worker(struct dlm_work_item *item, void *data);
Adrian Bunk8169cae2006-03-31 16:53:55 +020098static int dlm_lockres_master_requery(struct dlm_ctxt *dlm,
99 struct dlm_lock_resource *res,
100 u8 *real_master);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800101
102static u64 dlm_get_next_mig_cookie(void);
103
Ingo Molnar34af9462006-06-27 02:53:55 -0700104static DEFINE_SPINLOCK(dlm_reco_state_lock);
105static DEFINE_SPINLOCK(dlm_mig_cookie_lock);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800106static u64 dlm_mig_cookie = 1;
107
108static u64 dlm_get_next_mig_cookie(void)
109{
110 u64 c;
111 spin_lock(&dlm_mig_cookie_lock);
112 c = dlm_mig_cookie;
113 if (dlm_mig_cookie == (~0ULL))
114 dlm_mig_cookie = 1;
115 else
116 dlm_mig_cookie++;
117 spin_unlock(&dlm_mig_cookie_lock);
118 return c;
119}
120
Kurt Hackelab27eb62006-04-27 18:03:49 -0700121static inline void dlm_set_reco_dead_node(struct dlm_ctxt *dlm,
122 u8 dead_node)
123{
124 assert_spin_locked(&dlm->spinlock);
125 if (dlm->reco.dead_node != dead_node)
126 mlog(0, "%s: changing dead_node from %u to %u\n",
127 dlm->name, dlm->reco.dead_node, dead_node);
128 dlm->reco.dead_node = dead_node;
129}
130
131static inline void dlm_set_reco_master(struct dlm_ctxt *dlm,
132 u8 master)
133{
134 assert_spin_locked(&dlm->spinlock);
135 mlog(0, "%s: changing new_master from %u to %u\n",
136 dlm->name, dlm->reco.new_master, master);
137 dlm->reco.new_master = master;
138}
139
Kurt Hackel466d1a42006-05-01 11:11:13 -0700140static inline void __dlm_reset_recovery(struct dlm_ctxt *dlm)
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800141{
Kurt Hackel466d1a42006-05-01 11:11:13 -0700142 assert_spin_locked(&dlm->spinlock);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800143 clear_bit(dlm->reco.dead_node, dlm->recovery_map);
Kurt Hackelab27eb62006-04-27 18:03:49 -0700144 dlm_set_reco_dead_node(dlm, O2NM_INVALID_NODE_NUM);
145 dlm_set_reco_master(dlm, O2NM_INVALID_NODE_NUM);
Kurt Hackel466d1a42006-05-01 11:11:13 -0700146}
147
148static inline void dlm_reset_recovery(struct dlm_ctxt *dlm)
149{
150 spin_lock(&dlm->spinlock);
151 __dlm_reset_recovery(dlm);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800152 spin_unlock(&dlm->spinlock);
153}
154
155/* Worker function used during recovery. */
David Howellsc4028952006-11-22 14:57:56 +0000156void dlm_dispatch_work(struct work_struct *work)
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800157{
David Howellsc4028952006-11-22 14:57:56 +0000158 struct dlm_ctxt *dlm =
159 container_of(work, struct dlm_ctxt, dispatched_work);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800160 LIST_HEAD(tmp_list);
161 struct list_head *iter, *iter2;
162 struct dlm_work_item *item;
163 dlm_workfunc_t *workfunc;
Kurt Hackel3156d262006-05-01 14:39:29 -0700164 int tot=0;
165
166 if (!dlm_joined(dlm))
167 return;
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800168
169 spin_lock(&dlm->work_lock);
170 list_splice_init(&dlm->work_list, &tmp_list);
171 spin_unlock(&dlm->work_lock);
172
173 list_for_each_safe(iter, iter2, &tmp_list) {
Kurt Hackel3156d262006-05-01 14:39:29 -0700174 tot++;
175 }
176 mlog(0, "%s: work thread has %d work items\n", dlm->name, tot);
177
178 list_for_each_safe(iter, iter2, &tmp_list) {
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800179 item = list_entry(iter, struct dlm_work_item, list);
180 workfunc = item->func;
181 list_del_init(&item->list);
182
183 /* already have ref on dlm to avoid having
184 * it disappear. just double-check. */
185 BUG_ON(item->dlm != dlm);
186
187 /* this is allowed to sleep and
188 * call network stuff */
189 workfunc(item, item->data);
190
191 dlm_put(dlm);
192 kfree(item);
193 }
194}
195
196/*
197 * RECOVERY THREAD
198 */
199
Kurt Hackelc03872f2006-03-06 14:08:49 -0800200void dlm_kick_recovery_thread(struct dlm_ctxt *dlm)
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800201{
202 /* wake the recovery thread
203 * this will wake the reco thread in one of three places
204 * 1) sleeping with no recovery happening
205 * 2) sleeping with recovery mastered elsewhere
206 * 3) recovery mastered here, waiting on reco data */
207
208 wake_up(&dlm->dlm_reco_thread_wq);
209}
210
211/* Launch the recovery thread */
212int dlm_launch_recovery_thread(struct dlm_ctxt *dlm)
213{
214 mlog(0, "starting dlm recovery thread...\n");
215
216 dlm->dlm_reco_thread_task = kthread_run(dlm_recovery_thread, dlm,
217 "dlm_reco_thread");
218 if (IS_ERR(dlm->dlm_reco_thread_task)) {
219 mlog_errno(PTR_ERR(dlm->dlm_reco_thread_task));
220 dlm->dlm_reco_thread_task = NULL;
221 return -EINVAL;
222 }
223
224 return 0;
225}
226
227void dlm_complete_recovery_thread(struct dlm_ctxt *dlm)
228{
229 if (dlm->dlm_reco_thread_task) {
230 mlog(0, "waiting for dlm recovery thread to exit\n");
231 kthread_stop(dlm->dlm_reco_thread_task);
232 dlm->dlm_reco_thread_task = NULL;
233 }
234}
235
236
237
238/*
239 * this is lame, but here's how recovery works...
240 * 1) all recovery threads cluster wide will work on recovering
241 * ONE node at a time
242 * 2) negotiate who will take over all the locks for the dead node.
243 * thats right... ALL the locks.
244 * 3) once a new master is chosen, everyone scans all locks
245 * and moves aside those mastered by the dead guy
246 * 4) each of these locks should be locked until recovery is done
247 * 5) the new master collects up all of secondary lock queue info
248 * one lock at a time, forcing each node to communicate back
249 * before continuing
250 * 6) each secondary lock queue responds with the full known lock info
251 * 7) once the new master has run all its locks, it sends a ALLDONE!
252 * message to everyone
253 * 8) upon receiving this message, the secondary queue node unlocks
254 * and responds to the ALLDONE
255 * 9) once the new master gets responses from everyone, he unlocks
256 * everything and recovery for this dead node is done
257 *10) go back to 2) while there are still dead nodes
258 *
259 */
260
Kurt Hackeld6dea6e2006-04-27 18:08:51 -0700261static void dlm_print_reco_node_status(struct dlm_ctxt *dlm)
262{
263 struct dlm_reco_node_data *ndata;
264 struct dlm_lock_resource *res;
265
266 mlog(ML_NOTICE, "%s(%d): recovery info, state=%s, dead=%u, master=%u\n",
267 dlm->name, dlm->dlm_reco_thread_task->pid,
268 dlm->reco.state & DLM_RECO_STATE_ACTIVE ? "ACTIVE" : "inactive",
269 dlm->reco.dead_node, dlm->reco.new_master);
270
271 list_for_each_entry(ndata, &dlm->reco.node_data, list) {
272 char *st = "unknown";
273 switch (ndata->state) {
274 case DLM_RECO_NODE_DATA_INIT:
275 st = "init";
276 break;
277 case DLM_RECO_NODE_DATA_REQUESTING:
278 st = "requesting";
279 break;
280 case DLM_RECO_NODE_DATA_DEAD:
281 st = "dead";
282 break;
283 case DLM_RECO_NODE_DATA_RECEIVING:
284 st = "receiving";
285 break;
286 case DLM_RECO_NODE_DATA_REQUESTED:
287 st = "requested";
288 break;
289 case DLM_RECO_NODE_DATA_DONE:
290 st = "done";
291 break;
292 case DLM_RECO_NODE_DATA_FINALIZE_SENT:
293 st = "finalize-sent";
294 break;
295 default:
296 st = "bad";
297 break;
298 }
299 mlog(ML_NOTICE, "%s: reco state, node %u, state=%s\n",
300 dlm->name, ndata->node_num, st);
301 }
302 list_for_each_entry(res, &dlm->reco.resources, recovering) {
303 mlog(ML_NOTICE, "%s: lockres %.*s on recovering list\n",
304 dlm->name, res->lockname.len, res->lockname.name);
305 }
306}
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800307
308#define DLM_RECO_THREAD_TIMEOUT_MS (5 * 1000)
309
310static int dlm_recovery_thread(void *data)
311{
312 int status;
313 struct dlm_ctxt *dlm = data;
314 unsigned long timeout = msecs_to_jiffies(DLM_RECO_THREAD_TIMEOUT_MS);
315
316 mlog(0, "dlm thread running for %s...\n", dlm->name);
317
318 while (!kthread_should_stop()) {
319 if (dlm_joined(dlm)) {
320 status = dlm_do_recovery(dlm);
321 if (status == -EAGAIN) {
322 /* do not sleep, recheck immediately. */
323 continue;
324 }
325 if (status < 0)
326 mlog_errno(status);
327 }
328
329 wait_event_interruptible_timeout(dlm->dlm_reco_thread_wq,
330 kthread_should_stop(),
331 timeout);
332 }
333
334 mlog(0, "quitting DLM recovery thread\n");
335 return 0;
336}
337
Kurt Hackele2faea42006-01-12 14:24:55 -0800338/* returns true when the recovery master has contacted us */
339static int dlm_reco_master_ready(struct dlm_ctxt *dlm)
340{
341 int ready;
342 spin_lock(&dlm->spinlock);
343 ready = (dlm->reco.new_master != O2NM_INVALID_NODE_NUM);
344 spin_unlock(&dlm->spinlock);
345 return ready;
346}
347
348/* returns true if node is no longer in the domain
349 * could be dead or just not joined */
350int dlm_is_node_dead(struct dlm_ctxt *dlm, u8 node)
351{
352 int dead;
353 spin_lock(&dlm->spinlock);
Kurt Hackelaba9aac2006-04-27 18:00:21 -0700354 dead = !test_bit(node, dlm->domain_map);
Kurt Hackele2faea42006-01-12 14:24:55 -0800355 spin_unlock(&dlm->spinlock);
356 return dead;
357}
358
Kurt Hackelb7084ab2006-05-01 13:54:07 -0700359/* returns true if node is no longer in the domain
360 * could be dead or just not joined */
Adrian Bunk3fb5a982006-05-16 17:26:41 +0200361static int dlm_is_node_recovered(struct dlm_ctxt *dlm, u8 node)
Kurt Hackelb7084ab2006-05-01 13:54:07 -0700362{
363 int recovered;
364 spin_lock(&dlm->spinlock);
365 recovered = !test_bit(node, dlm->recovery_map);
366 spin_unlock(&dlm->spinlock);
367 return recovered;
368}
369
370
Kurt Hackel44465a72006-01-18 17:05:38 -0800371int dlm_wait_for_node_death(struct dlm_ctxt *dlm, u8 node, int timeout)
372{
373 if (timeout) {
374 mlog(ML_NOTICE, "%s: waiting %dms for notification of "
375 "death of node %u\n", dlm->name, timeout, node);
376 wait_event_timeout(dlm->dlm_reco_thread_wq,
377 dlm_is_node_dead(dlm, node),
378 msecs_to_jiffies(timeout));
379 } else {
380 mlog(ML_NOTICE, "%s: waiting indefinitely for notification "
381 "of death of node %u\n", dlm->name, node);
382 wait_event(dlm->dlm_reco_thread_wq,
383 dlm_is_node_dead(dlm, node));
384 }
385 /* for now, return 0 */
386 return 0;
387}
388
Kurt Hackelb7084ab2006-05-01 13:54:07 -0700389int dlm_wait_for_node_recovery(struct dlm_ctxt *dlm, u8 node, int timeout)
390{
391 if (timeout) {
392 mlog(0, "%s: waiting %dms for notification of "
393 "recovery of node %u\n", dlm->name, timeout, node);
394 wait_event_timeout(dlm->dlm_reco_thread_wq,
395 dlm_is_node_recovered(dlm, node),
396 msecs_to_jiffies(timeout));
397 } else {
398 mlog(0, "%s: waiting indefinitely for notification "
399 "of recovery of node %u\n", dlm->name, node);
400 wait_event(dlm->dlm_reco_thread_wq,
401 dlm_is_node_recovered(dlm, node));
402 }
403 /* for now, return 0 */
404 return 0;
405}
406
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800407/* callers of the top-level api calls (dlmlock/dlmunlock) should
408 * block on the dlm->reco.event when recovery is in progress.
409 * the dlm recovery thread will set this state when it begins
410 * recovering a dead node (as the new master or not) and clear
411 * the state and wake as soon as all affected lock resources have
412 * been marked with the RECOVERY flag */
413static int dlm_in_recovery(struct dlm_ctxt *dlm)
414{
415 int in_recovery;
416 spin_lock(&dlm->spinlock);
417 in_recovery = !!(dlm->reco.state & DLM_RECO_STATE_ACTIVE);
418 spin_unlock(&dlm->spinlock);
419 return in_recovery;
420}
421
422
423void dlm_wait_for_recovery(struct dlm_ctxt *dlm)
424{
Kurt Hackel56a7c102006-05-01 14:30:39 -0700425 if (dlm_in_recovery(dlm)) {
Kurt Hackel3b3b84a2006-05-01 14:31:37 -0700426 mlog(0, "%s: reco thread %d in recovery: "
Kurt Hackel56a7c102006-05-01 14:30:39 -0700427 "state=%d, master=%u, dead=%u\n",
428 dlm->name, dlm->dlm_reco_thread_task->pid,
429 dlm->reco.state, dlm->reco.new_master,
430 dlm->reco.dead_node);
431 }
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800432 wait_event(dlm->reco.event, !dlm_in_recovery(dlm));
433}
434
435static void dlm_begin_recovery(struct dlm_ctxt *dlm)
436{
437 spin_lock(&dlm->spinlock);
438 BUG_ON(dlm->reco.state & DLM_RECO_STATE_ACTIVE);
439 dlm->reco.state |= DLM_RECO_STATE_ACTIVE;
440 spin_unlock(&dlm->spinlock);
441}
442
443static void dlm_end_recovery(struct dlm_ctxt *dlm)
444{
445 spin_lock(&dlm->spinlock);
446 BUG_ON(!(dlm->reco.state & DLM_RECO_STATE_ACTIVE));
447 dlm->reco.state &= ~DLM_RECO_STATE_ACTIVE;
448 spin_unlock(&dlm->spinlock);
449 wake_up(&dlm->reco.event);
450}
451
452static int dlm_do_recovery(struct dlm_ctxt *dlm)
453{
454 int status = 0;
Kurt Hackele2faea42006-01-12 14:24:55 -0800455 int ret;
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800456
457 spin_lock(&dlm->spinlock);
458
459 /* check to see if the new master has died */
460 if (dlm->reco.new_master != O2NM_INVALID_NODE_NUM &&
461 test_bit(dlm->reco.new_master, dlm->recovery_map)) {
462 mlog(0, "new master %u died while recovering %u!\n",
463 dlm->reco.new_master, dlm->reco.dead_node);
464 /* unset the new_master, leave dead_node */
Kurt Hackelab27eb62006-04-27 18:03:49 -0700465 dlm_set_reco_master(dlm, O2NM_INVALID_NODE_NUM);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800466 }
467
468 /* select a target to recover */
469 if (dlm->reco.dead_node == O2NM_INVALID_NODE_NUM) {
470 int bit;
471
472 bit = find_next_bit (dlm->recovery_map, O2NM_MAX_NODES+1, 0);
473 if (bit >= O2NM_MAX_NODES || bit < 0)
Kurt Hackelab27eb62006-04-27 18:03:49 -0700474 dlm_set_reco_dead_node(dlm, O2NM_INVALID_NODE_NUM);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800475 else
Kurt Hackelab27eb62006-04-27 18:03:49 -0700476 dlm_set_reco_dead_node(dlm, bit);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800477 } else if (!test_bit(dlm->reco.dead_node, dlm->recovery_map)) {
478 /* BUG? */
479 mlog(ML_ERROR, "dead_node %u no longer in recovery map!\n",
480 dlm->reco.dead_node);
Kurt Hackelab27eb62006-04-27 18:03:49 -0700481 dlm_set_reco_dead_node(dlm, O2NM_INVALID_NODE_NUM);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800482 }
483
484 if (dlm->reco.dead_node == O2NM_INVALID_NODE_NUM) {
485 // mlog(0, "nothing to recover! sleeping now!\n");
486 spin_unlock(&dlm->spinlock);
487 /* return to main thread loop and sleep. */
488 return 0;
489 }
Kurt Hackeld6dea6e2006-04-27 18:08:51 -0700490 mlog(0, "%s(%d):recovery thread found node %u in the recovery map!\n",
491 dlm->name, dlm->dlm_reco_thread_task->pid,
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800492 dlm->reco.dead_node);
493 spin_unlock(&dlm->spinlock);
494
495 /* take write barrier */
496 /* (stops the list reshuffling thread, proxy ast handling) */
497 dlm_begin_recovery(dlm);
498
499 if (dlm->reco.new_master == dlm->node_num)
500 goto master_here;
501
502 if (dlm->reco.new_master == O2NM_INVALID_NODE_NUM) {
Kurt Hackele2faea42006-01-12 14:24:55 -0800503 /* choose a new master, returns 0 if this node
504 * is the master, -EEXIST if it's another node.
505 * this does not return until a new master is chosen
506 * or recovery completes entirely. */
507 ret = dlm_pick_recovery_master(dlm);
508 if (!ret) {
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800509 /* already notified everyone. go. */
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800510 goto master_here;
511 }
512 mlog(0, "another node will master this recovery session.\n");
513 }
Kurt Hackeld6dea6e2006-04-27 18:08:51 -0700514 mlog(0, "dlm=%s (%d), new_master=%u, this node=%u, dead_node=%u\n",
515 dlm->name, dlm->dlm_reco_thread_task->pid, dlm->reco.new_master,
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800516 dlm->node_num, dlm->reco.dead_node);
517
518 /* it is safe to start everything back up here
519 * because all of the dead node's lock resources
520 * have been marked as in-recovery */
521 dlm_end_recovery(dlm);
522
523 /* sleep out in main dlm_recovery_thread loop. */
524 return 0;
525
526master_here:
Kurt Hackeld6dea6e2006-04-27 18:08:51 -0700527 mlog(0, "(%d) mastering recovery of %s:%u here(this=%u)!\n",
528 dlm->dlm_reco_thread_task->pid,
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800529 dlm->name, dlm->reco.dead_node, dlm->node_num);
530
531 status = dlm_remaster_locks(dlm, dlm->reco.dead_node);
532 if (status < 0) {
Kurt Hackel6a413212006-05-01 13:49:20 -0700533 /* we should never hit this anymore */
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800534 mlog(ML_ERROR, "error %d remastering locks for node %u, "
535 "retrying.\n", status, dlm->reco.dead_node);
Kurt Hackele2faea42006-01-12 14:24:55 -0800536 /* yield a bit to allow any final network messages
537 * to get handled on remaining nodes */
538 msleep(100);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800539 } else {
540 /* success! see if any other nodes need recovery */
Kurt Hackele2faea42006-01-12 14:24:55 -0800541 mlog(0, "DONE mastering recovery of %s:%u here(this=%u)!\n",
542 dlm->name, dlm->reco.dead_node, dlm->node_num);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800543 dlm_reset_recovery(dlm);
544 }
545 dlm_end_recovery(dlm);
546
547 /* continue and look for another dead node */
548 return -EAGAIN;
549}
550
551static int dlm_remaster_locks(struct dlm_ctxt *dlm, u8 dead_node)
552{
553 int status = 0;
554 struct dlm_reco_node_data *ndata;
555 struct list_head *iter;
556 int all_nodes_done;
557 int destroy = 0;
558 int pass = 0;
559
Kurt Hackel6a413212006-05-01 13:49:20 -0700560 do {
561 /* we have become recovery master. there is no escaping
562 * this, so just keep trying until we get it. */
563 status = dlm_init_recovery_area(dlm, dead_node);
564 if (status < 0) {
565 mlog(ML_ERROR, "%s: failed to alloc recovery area, "
566 "retrying\n", dlm->name);
567 msleep(1000);
568 }
569 } while (status != 0);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800570
571 /* safe to access the node data list without a lock, since this
572 * process is the only one to change the list */
573 list_for_each(iter, &dlm->reco.node_data) {
574 ndata = list_entry (iter, struct dlm_reco_node_data, list);
575 BUG_ON(ndata->state != DLM_RECO_NODE_DATA_INIT);
576 ndata->state = DLM_RECO_NODE_DATA_REQUESTING;
577
578 mlog(0, "requesting lock info from node %u\n",
579 ndata->node_num);
580
581 if (ndata->node_num == dlm->node_num) {
582 ndata->state = DLM_RECO_NODE_DATA_DONE;
583 continue;
584 }
585
Kurt Hackel6a413212006-05-01 13:49:20 -0700586 do {
587 status = dlm_request_all_locks(dlm, ndata->node_num,
588 dead_node);
589 if (status < 0) {
590 mlog_errno(status);
591 if (dlm_is_host_down(status)) {
592 /* node died, ignore it for recovery */
593 status = 0;
594 ndata->state = DLM_RECO_NODE_DATA_DEAD;
595 /* wait for the domain map to catch up
596 * with the network state. */
597 wait_event_timeout(dlm->dlm_reco_thread_wq,
598 dlm_is_node_dead(dlm,
599 ndata->node_num),
600 msecs_to_jiffies(1000));
601 mlog(0, "waited 1 sec for %u, "
602 "dead? %s\n", ndata->node_num,
603 dlm_is_node_dead(dlm, ndata->node_num) ?
604 "yes" : "no");
605 } else {
606 /* -ENOMEM on the other node */
607 mlog(0, "%s: node %u returned "
608 "%d during recovery, retrying "
609 "after a short wait\n",
610 dlm->name, ndata->node_num,
611 status);
612 msleep(100);
613 }
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800614 }
Kurt Hackel6a413212006-05-01 13:49:20 -0700615 } while (status != 0);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800616
617 switch (ndata->state) {
618 case DLM_RECO_NODE_DATA_INIT:
619 case DLM_RECO_NODE_DATA_FINALIZE_SENT:
620 case DLM_RECO_NODE_DATA_REQUESTED:
621 BUG();
622 break;
623 case DLM_RECO_NODE_DATA_DEAD:
624 mlog(0, "node %u died after requesting "
625 "recovery info for node %u\n",
626 ndata->node_num, dead_node);
Kurt Hackel6a413212006-05-01 13:49:20 -0700627 /* fine. don't need this node's info.
628 * continue without it. */
629 break;
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800630 case DLM_RECO_NODE_DATA_REQUESTING:
631 ndata->state = DLM_RECO_NODE_DATA_REQUESTED;
632 mlog(0, "now receiving recovery data from "
633 "node %u for dead node %u\n",
634 ndata->node_num, dead_node);
635 break;
636 case DLM_RECO_NODE_DATA_RECEIVING:
637 mlog(0, "already receiving recovery data from "
638 "node %u for dead node %u\n",
639 ndata->node_num, dead_node);
640 break;
641 case DLM_RECO_NODE_DATA_DONE:
642 mlog(0, "already DONE receiving recovery data "
643 "from node %u for dead node %u\n",
644 ndata->node_num, dead_node);
645 break;
646 }
647 }
648
649 mlog(0, "done requesting all lock info\n");
650
651 /* nodes should be sending reco data now
652 * just need to wait */
653
654 while (1) {
655 /* check all the nodes now to see if we are
656 * done, or if anyone died */
657 all_nodes_done = 1;
658 spin_lock(&dlm_reco_state_lock);
659 list_for_each(iter, &dlm->reco.node_data) {
660 ndata = list_entry (iter, struct dlm_reco_node_data, list);
661
662 mlog(0, "checking recovery state of node %u\n",
663 ndata->node_num);
664 switch (ndata->state) {
665 case DLM_RECO_NODE_DATA_INIT:
666 case DLM_RECO_NODE_DATA_REQUESTING:
667 mlog(ML_ERROR, "bad ndata state for "
668 "node %u: state=%d\n",
669 ndata->node_num, ndata->state);
670 BUG();
671 break;
672 case DLM_RECO_NODE_DATA_DEAD:
Kurt Hackel6a413212006-05-01 13:49:20 -0700673 mlog(0, "node %u died after "
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800674 "requesting recovery info for "
675 "node %u\n", ndata->node_num,
676 dead_node);
Kurt Hackel6a413212006-05-01 13:49:20 -0700677 break;
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800678 case DLM_RECO_NODE_DATA_RECEIVING:
679 case DLM_RECO_NODE_DATA_REQUESTED:
Kurt Hackeld6dea6e2006-04-27 18:08:51 -0700680 mlog(0, "%s: node %u still in state %s\n",
681 dlm->name, ndata->node_num,
682 ndata->state==DLM_RECO_NODE_DATA_RECEIVING ?
683 "receiving" : "requested");
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800684 all_nodes_done = 0;
685 break;
686 case DLM_RECO_NODE_DATA_DONE:
Kurt Hackeld6dea6e2006-04-27 18:08:51 -0700687 mlog(0, "%s: node %u state is done\n",
688 dlm->name, ndata->node_num);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800689 break;
690 case DLM_RECO_NODE_DATA_FINALIZE_SENT:
Kurt Hackeld6dea6e2006-04-27 18:08:51 -0700691 mlog(0, "%s: node %u state is finalize\n",
692 dlm->name, ndata->node_num);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800693 break;
694 }
695 }
696 spin_unlock(&dlm_reco_state_lock);
697
698 mlog(0, "pass #%d, all_nodes_done?: %s\n", ++pass,
699 all_nodes_done?"yes":"no");
700 if (all_nodes_done) {
701 int ret;
702
703 /* all nodes are now in DLM_RECO_NODE_DATA_DONE state
704 * just send a finalize message to everyone and
705 * clean up */
706 mlog(0, "all nodes are done! send finalize\n");
707 ret = dlm_send_finalize_reco_message(dlm);
708 if (ret < 0)
709 mlog_errno(ret);
710
711 spin_lock(&dlm->spinlock);
712 dlm_finish_local_lockres_recovery(dlm, dead_node,
713 dlm->node_num);
714 spin_unlock(&dlm->spinlock);
715 mlog(0, "should be done with recovery!\n");
716
717 mlog(0, "finishing recovery of %s at %lu, "
718 "dead=%u, this=%u, new=%u\n", dlm->name,
719 jiffies, dlm->reco.dead_node,
720 dlm->node_num, dlm->reco.new_master);
721 destroy = 1;
Kurt Hackel6a413212006-05-01 13:49:20 -0700722 status = 0;
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800723 /* rescan everything marked dirty along the way */
724 dlm_kick_thread(dlm, NULL);
725 break;
726 }
727 /* wait to be signalled, with periodic timeout
728 * to check for node death */
729 wait_event_interruptible_timeout(dlm->dlm_reco_thread_wq,
730 kthread_should_stop(),
731 msecs_to_jiffies(DLM_RECO_THREAD_TIMEOUT_MS));
732
733 }
734
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800735 if (destroy)
736 dlm_destroy_recovery_area(dlm, dead_node);
737
738 mlog_exit(status);
739 return status;
740}
741
742static int dlm_init_recovery_area(struct dlm_ctxt *dlm, u8 dead_node)
743{
744 int num=0;
745 struct dlm_reco_node_data *ndata;
746
747 spin_lock(&dlm->spinlock);
748 memcpy(dlm->reco.node_map, dlm->domain_map, sizeof(dlm->domain_map));
749 /* nodes can only be removed (by dying) after dropping
750 * this lock, and death will be trapped later, so this should do */
751 spin_unlock(&dlm->spinlock);
752
753 while (1) {
754 num = find_next_bit (dlm->reco.node_map, O2NM_MAX_NODES, num);
755 if (num >= O2NM_MAX_NODES) {
756 break;
757 }
758 BUG_ON(num == dead_node);
759
Kurt Hackelad8100e2006-05-01 14:25:21 -0700760 ndata = kcalloc(1, sizeof(*ndata), GFP_NOFS);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800761 if (!ndata) {
762 dlm_destroy_recovery_area(dlm, dead_node);
763 return -ENOMEM;
764 }
765 ndata->node_num = num;
766 ndata->state = DLM_RECO_NODE_DATA_INIT;
767 spin_lock(&dlm_reco_state_lock);
768 list_add_tail(&ndata->list, &dlm->reco.node_data);
769 spin_unlock(&dlm_reco_state_lock);
770 num++;
771 }
772
773 return 0;
774}
775
776static void dlm_destroy_recovery_area(struct dlm_ctxt *dlm, u8 dead_node)
777{
778 struct list_head *iter, *iter2;
779 struct dlm_reco_node_data *ndata;
780 LIST_HEAD(tmplist);
781
782 spin_lock(&dlm_reco_state_lock);
783 list_splice_init(&dlm->reco.node_data, &tmplist);
784 spin_unlock(&dlm_reco_state_lock);
785
786 list_for_each_safe(iter, iter2, &tmplist) {
787 ndata = list_entry (iter, struct dlm_reco_node_data, list);
788 list_del_init(&ndata->list);
789 kfree(ndata);
790 }
791}
792
793static int dlm_request_all_locks(struct dlm_ctxt *dlm, u8 request_from,
794 u8 dead_node)
795{
796 struct dlm_lock_request lr;
797 enum dlm_status ret;
798
799 mlog(0, "\n");
800
801
802 mlog(0, "dlm_request_all_locks: dead node is %u, sending request "
803 "to %u\n", dead_node, request_from);
804
805 memset(&lr, 0, sizeof(lr));
806 lr.node_idx = dlm->node_num;
807 lr.dead_node = dead_node;
808
809 // send message
810 ret = DLM_NOLOCKMGR;
811 ret = o2net_send_message(DLM_LOCK_REQUEST_MSG, dlm->key,
812 &lr, sizeof(lr), request_from, NULL);
813
814 /* negative status is handled by caller */
815 if (ret < 0)
816 mlog_errno(ret);
817
818 // return from here, then
819 // sleep until all received or error
820 return ret;
821
822}
823
824int dlm_request_all_locks_handler(struct o2net_msg *msg, u32 len, void *data)
825{
826 struct dlm_ctxt *dlm = data;
827 struct dlm_lock_request *lr = (struct dlm_lock_request *)msg->buf;
828 char *buf = NULL;
829 struct dlm_work_item *item = NULL;
830
831 if (!dlm_grab(dlm))
832 return -EINVAL;
833
Kurt Hackelc3187ce2006-04-27 18:05:41 -0700834 if (lr->dead_node != dlm->reco.dead_node) {
835 mlog(ML_ERROR, "%s: node %u sent dead_node=%u, but local "
836 "dead_node is %u\n", dlm->name, lr->node_idx,
837 lr->dead_node, dlm->reco.dead_node);
Kurt Hackeld6dea6e2006-04-27 18:08:51 -0700838 dlm_print_reco_node_status(dlm);
Kurt Hackelc3187ce2006-04-27 18:05:41 -0700839 /* this is a hack */
840 dlm_put(dlm);
841 return -ENOMEM;
842 }
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800843 BUG_ON(lr->dead_node != dlm->reco.dead_node);
844
Kurt Hackelad8100e2006-05-01 14:25:21 -0700845 item = kcalloc(1, sizeof(*item), GFP_NOFS);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800846 if (!item) {
847 dlm_put(dlm);
848 return -ENOMEM;
849 }
850
851 /* this will get freed by dlm_request_all_locks_worker */
Kurt Hackelad8100e2006-05-01 14:25:21 -0700852 buf = (char *) __get_free_page(GFP_NOFS);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800853 if (!buf) {
854 kfree(item);
855 dlm_put(dlm);
856 return -ENOMEM;
857 }
858
859 /* queue up work for dlm_request_all_locks_worker */
860 dlm_grab(dlm); /* get an extra ref for the work item */
861 dlm_init_work_item(dlm, item, dlm_request_all_locks_worker, buf);
862 item->u.ral.reco_master = lr->node_idx;
863 item->u.ral.dead_node = lr->dead_node;
864 spin_lock(&dlm->work_lock);
865 list_add_tail(&item->list, &dlm->work_list);
866 spin_unlock(&dlm->work_lock);
Kurt Hackel3156d262006-05-01 14:39:29 -0700867 queue_work(dlm->dlm_worker, &dlm->dispatched_work);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800868
869 dlm_put(dlm);
870 return 0;
871}
872
873static void dlm_request_all_locks_worker(struct dlm_work_item *item, void *data)
874{
875 struct dlm_migratable_lockres *mres;
876 struct dlm_lock_resource *res;
877 struct dlm_ctxt *dlm;
878 LIST_HEAD(resources);
879 struct list_head *iter;
880 int ret;
881 u8 dead_node, reco_master;
Kurt Hackel29c0fa02006-04-27 18:06:58 -0700882 int skip_all_done = 0;
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800883
884 dlm = item->dlm;
885 dead_node = item->u.ral.dead_node;
886 reco_master = item->u.ral.reco_master;
Kurt Hackele2faea42006-01-12 14:24:55 -0800887 mres = (struct dlm_migratable_lockres *)data;
888
Kurt Hackeld6dea6e2006-04-27 18:08:51 -0700889 mlog(0, "%s: recovery worker started, dead=%u, master=%u\n",
890 dlm->name, dead_node, reco_master);
891
Kurt Hackele2faea42006-01-12 14:24:55 -0800892 if (dead_node != dlm->reco.dead_node ||
893 reco_master != dlm->reco.new_master) {
Kurt Hackel6a413212006-05-01 13:49:20 -0700894 /* worker could have been created before the recovery master
895 * died. if so, do not continue, but do not error. */
896 if (dlm->reco.new_master == O2NM_INVALID_NODE_NUM) {
897 mlog(ML_NOTICE, "%s: will not send recovery state, "
898 "recovery master %u died, thread=(dead=%u,mas=%u)"
899 " current=(dead=%u,mas=%u)\n", dlm->name,
900 reco_master, dead_node, reco_master,
901 dlm->reco.dead_node, dlm->reco.new_master);
902 } else {
903 mlog(ML_NOTICE, "%s: reco state invalid: reco(dead=%u, "
904 "master=%u), request(dead=%u, master=%u)\n",
905 dlm->name, dlm->reco.dead_node,
906 dlm->reco.new_master, dead_node, reco_master);
907 }
908 goto leave;
Kurt Hackele2faea42006-01-12 14:24:55 -0800909 }
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800910
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800911 /* lock resources should have already been moved to the
912 * dlm->reco.resources list. now move items from that list
913 * to a temp list if the dead owner matches. note that the
914 * whole cluster recovers only one node at a time, so we
915 * can safely move UNKNOWN lock resources for each recovery
916 * session. */
917 dlm_move_reco_locks_to_list(dlm, &resources, dead_node);
918
919 /* now we can begin blasting lockreses without the dlm lock */
Kurt Hackel29c0fa02006-04-27 18:06:58 -0700920
921 /* any errors returned will be due to the new_master dying,
922 * the dlm_reco_thread should detect this */
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800923 list_for_each(iter, &resources) {
924 res = list_entry (iter, struct dlm_lock_resource, recovering);
925 ret = dlm_send_one_lockres(dlm, res, mres, reco_master,
926 DLM_MRES_RECOVERY);
Kurt Hackel29c0fa02006-04-27 18:06:58 -0700927 if (ret < 0) {
Kurt Hackeld6dea6e2006-04-27 18:08:51 -0700928 mlog(ML_ERROR, "%s: node %u went down while sending "
929 "recovery state for dead node %u, ret=%d\n", dlm->name,
930 reco_master, dead_node, ret);
Kurt Hackel29c0fa02006-04-27 18:06:58 -0700931 skip_all_done = 1;
932 break;
933 }
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800934 }
935
936 /* move the resources back to the list */
937 spin_lock(&dlm->spinlock);
938 list_splice_init(&resources, &dlm->reco.resources);
939 spin_unlock(&dlm->spinlock);
940
Kurt Hackel29c0fa02006-04-27 18:06:58 -0700941 if (!skip_all_done) {
942 ret = dlm_send_all_done_msg(dlm, dead_node, reco_master);
943 if (ret < 0) {
Kurt Hackeld6dea6e2006-04-27 18:08:51 -0700944 mlog(ML_ERROR, "%s: node %u went down while sending "
945 "recovery all-done for dead node %u, ret=%d\n",
946 dlm->name, reco_master, dead_node, ret);
Kurt Hackel29c0fa02006-04-27 18:06:58 -0700947 }
948 }
Kurt Hackel6a413212006-05-01 13:49:20 -0700949leave:
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800950 free_page((unsigned long)data);
951}
952
953
954static int dlm_send_all_done_msg(struct dlm_ctxt *dlm, u8 dead_node, u8 send_to)
955{
956 int ret, tmpret;
957 struct dlm_reco_data_done done_msg;
958
959 memset(&done_msg, 0, sizeof(done_msg));
960 done_msg.node_idx = dlm->node_num;
961 done_msg.dead_node = dead_node;
962 mlog(0, "sending DATA DONE message to %u, "
963 "my node=%u, dead node=%u\n", send_to, done_msg.node_idx,
964 done_msg.dead_node);
965
966 ret = o2net_send_message(DLM_RECO_DATA_DONE_MSG, dlm->key, &done_msg,
967 sizeof(done_msg), send_to, &tmpret);
Kurt Hackel29c0fa02006-04-27 18:06:58 -0700968 if (ret < 0) {
969 if (!dlm_is_host_down(ret)) {
970 mlog_errno(ret);
971 mlog(ML_ERROR, "%s: unknown error sending data-done "
972 "to %u\n", dlm->name, send_to);
973 BUG();
974 }
975 } else
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800976 ret = tmpret;
977 return ret;
978}
979
980
981int dlm_reco_data_done_handler(struct o2net_msg *msg, u32 len, void *data)
982{
983 struct dlm_ctxt *dlm = data;
984 struct dlm_reco_data_done *done = (struct dlm_reco_data_done *)msg->buf;
985 struct list_head *iter;
986 struct dlm_reco_node_data *ndata = NULL;
987 int ret = -EINVAL;
988
989 if (!dlm_grab(dlm))
990 return -EINVAL;
991
992 mlog(0, "got DATA DONE: dead_node=%u, reco.dead_node=%u, "
993 "node_idx=%u, this node=%u\n", done->dead_node,
994 dlm->reco.dead_node, done->node_idx, dlm->node_num);
Kurt Hackeld6dea6e2006-04-27 18:08:51 -0700995
996 mlog_bug_on_msg((done->dead_node != dlm->reco.dead_node),
997 "Got DATA DONE: dead_node=%u, reco.dead_node=%u, "
998 "node_idx=%u, this node=%u\n", done->dead_node,
999 dlm->reco.dead_node, done->node_idx, dlm->node_num);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001000
1001 spin_lock(&dlm_reco_state_lock);
1002 list_for_each(iter, &dlm->reco.node_data) {
1003 ndata = list_entry (iter, struct dlm_reco_node_data, list);
1004 if (ndata->node_num != done->node_idx)
1005 continue;
1006
1007 switch (ndata->state) {
Kurt Hackele2faea42006-01-12 14:24:55 -08001008 /* should have moved beyond INIT but not to FINALIZE yet */
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001009 case DLM_RECO_NODE_DATA_INIT:
1010 case DLM_RECO_NODE_DATA_DEAD:
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001011 case DLM_RECO_NODE_DATA_FINALIZE_SENT:
1012 mlog(ML_ERROR, "bad ndata state for node %u:"
1013 " state=%d\n", ndata->node_num,
1014 ndata->state);
1015 BUG();
1016 break;
Kurt Hackele2faea42006-01-12 14:24:55 -08001017 /* these states are possible at this point, anywhere along
1018 * the line of recovery */
1019 case DLM_RECO_NODE_DATA_DONE:
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001020 case DLM_RECO_NODE_DATA_RECEIVING:
1021 case DLM_RECO_NODE_DATA_REQUESTED:
1022 case DLM_RECO_NODE_DATA_REQUESTING:
1023 mlog(0, "node %u is DONE sending "
1024 "recovery data!\n",
1025 ndata->node_num);
1026
1027 ndata->state = DLM_RECO_NODE_DATA_DONE;
1028 ret = 0;
1029 break;
1030 }
1031 }
1032 spin_unlock(&dlm_reco_state_lock);
1033
1034 /* wake the recovery thread, some node is done */
1035 if (!ret)
1036 dlm_kick_recovery_thread(dlm);
1037
1038 if (ret < 0)
1039 mlog(ML_ERROR, "failed to find recovery node data for node "
1040 "%u\n", done->node_idx);
1041 dlm_put(dlm);
1042
1043 mlog(0, "leaving reco data done handler, ret=%d\n", ret);
1044 return ret;
1045}
1046
1047static void dlm_move_reco_locks_to_list(struct dlm_ctxt *dlm,
1048 struct list_head *list,
1049 u8 dead_node)
1050{
1051 struct dlm_lock_resource *res;
1052 struct list_head *iter, *iter2;
Kurt Hackele2faea42006-01-12 14:24:55 -08001053 struct dlm_lock *lock;
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001054
1055 spin_lock(&dlm->spinlock);
1056 list_for_each_safe(iter, iter2, &dlm->reco.resources) {
1057 res = list_entry (iter, struct dlm_lock_resource, recovering);
Kurt Hackele2faea42006-01-12 14:24:55 -08001058 /* always prune any $RECOVERY entries for dead nodes,
1059 * otherwise hangs can occur during later recovery */
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001060 if (dlm_is_recovery_lock(res->lockname.name,
Kurt Hackele2faea42006-01-12 14:24:55 -08001061 res->lockname.len)) {
1062 spin_lock(&res->spinlock);
1063 list_for_each_entry(lock, &res->granted, list) {
1064 if (lock->ml.node == dead_node) {
1065 mlog(0, "AHA! there was "
1066 "a $RECOVERY lock for dead "
1067 "node %u (%s)!\n",
1068 dead_node, dlm->name);
1069 list_del_init(&lock->list);
1070 dlm_lock_put(lock);
1071 break;
1072 }
1073 }
1074 spin_unlock(&res->spinlock);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001075 continue;
Kurt Hackele2faea42006-01-12 14:24:55 -08001076 }
1077
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001078 if (res->owner == dead_node) {
1079 mlog(0, "found lockres owned by dead node while "
1080 "doing recovery for node %u. sending it.\n",
1081 dead_node);
Akinobu Mitaf1166292006-06-26 00:24:46 -07001082 list_move_tail(&res->recovering, list);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001083 } else if (res->owner == DLM_LOCK_RES_OWNER_UNKNOWN) {
1084 mlog(0, "found UNKNOWN owner while doing recovery "
1085 "for node %u. sending it.\n", dead_node);
Akinobu Mitaf1166292006-06-26 00:24:46 -07001086 list_move_tail(&res->recovering, list);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001087 }
1088 }
1089 spin_unlock(&dlm->spinlock);
1090}
1091
1092static inline int dlm_num_locks_in_lockres(struct dlm_lock_resource *res)
1093{
1094 int total_locks = 0;
1095 struct list_head *iter, *queue = &res->granted;
1096 int i;
1097
1098 for (i=0; i<3; i++) {
1099 list_for_each(iter, queue)
1100 total_locks++;
1101 queue++;
1102 }
1103 return total_locks;
1104}
1105
1106
1107static int dlm_send_mig_lockres_msg(struct dlm_ctxt *dlm,
1108 struct dlm_migratable_lockres *mres,
1109 u8 send_to,
1110 struct dlm_lock_resource *res,
1111 int total_locks)
1112{
1113 u64 mig_cookie = be64_to_cpu(mres->mig_cookie);
1114 int mres_total_locks = be32_to_cpu(mres->total_locks);
1115 int sz, ret = 0, status = 0;
1116 u8 orig_flags = mres->flags,
1117 orig_master = mres->master;
1118
1119 BUG_ON(mres->num_locks > DLM_MAX_MIGRATABLE_LOCKS);
1120 if (!mres->num_locks)
1121 return 0;
1122
1123 sz = sizeof(struct dlm_migratable_lockres) +
1124 (mres->num_locks * sizeof(struct dlm_migratable_lock));
1125
1126 /* add an all-done flag if we reached the last lock */
1127 orig_flags = mres->flags;
1128 BUG_ON(total_locks > mres_total_locks);
1129 if (total_locks == mres_total_locks)
1130 mres->flags |= DLM_MRES_ALL_DONE;
1131
1132 /* send it */
1133 ret = o2net_send_message(DLM_MIG_LOCKRES_MSG, dlm->key, mres,
1134 sz, send_to, &status);
1135 if (ret < 0) {
1136 /* XXX: negative status is not handled.
1137 * this will end up killing this node. */
1138 mlog_errno(ret);
1139 } else {
1140 /* might get an -ENOMEM back here */
1141 ret = status;
1142 if (ret < 0) {
1143 mlog_errno(ret);
1144
1145 if (ret == -EFAULT) {
1146 mlog(ML_ERROR, "node %u told me to kill "
1147 "myself!\n", send_to);
1148 BUG();
1149 }
1150 }
1151 }
1152
1153 /* zero and reinit the message buffer */
1154 dlm_init_migratable_lockres(mres, res->lockname.name,
1155 res->lockname.len, mres_total_locks,
1156 mig_cookie, orig_flags, orig_master);
1157 return ret;
1158}
1159
1160static void dlm_init_migratable_lockres(struct dlm_migratable_lockres *mres,
1161 const char *lockname, int namelen,
1162 int total_locks, u64 cookie,
1163 u8 flags, u8 master)
1164{
1165 /* mres here is one full page */
1166 memset(mres, 0, PAGE_SIZE);
1167 mres->lockname_len = namelen;
1168 memcpy(mres->lockname, lockname, namelen);
1169 mres->num_locks = 0;
1170 mres->total_locks = cpu_to_be32(total_locks);
1171 mres->mig_cookie = cpu_to_be64(cookie);
1172 mres->flags = flags;
1173 mres->master = master;
1174}
1175
1176
1177/* returns 1 if this lock fills the network structure,
1178 * 0 otherwise */
1179static int dlm_add_lock_to_array(struct dlm_lock *lock,
1180 struct dlm_migratable_lockres *mres, int queue)
1181{
1182 struct dlm_migratable_lock *ml;
1183 int lock_num = mres->num_locks;
1184
1185 ml = &(mres->ml[lock_num]);
1186 ml->cookie = lock->ml.cookie;
1187 ml->type = lock->ml.type;
1188 ml->convert_type = lock->ml.convert_type;
1189 ml->highest_blocked = lock->ml.highest_blocked;
1190 ml->list = queue;
1191 if (lock->lksb) {
1192 ml->flags = lock->lksb->flags;
1193 /* send our current lvb */
1194 if (ml->type == LKM_EXMODE ||
1195 ml->type == LKM_PRMODE) {
1196 /* if it is already set, this had better be a PR
1197 * and it has to match */
Kurt Hackel8bc674c2006-04-27 18:02:10 -07001198 if (!dlm_lvb_is_empty(mres->lvb) &&
1199 (ml->type == LKM_EXMODE ||
1200 memcmp(mres->lvb, lock->lksb->lvb, DLM_LVB_LEN))) {
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001201 mlog(ML_ERROR, "mismatched lvbs!\n");
1202 __dlm_print_one_lock_resource(lock->lockres);
1203 BUG();
1204 }
1205 memcpy(mres->lvb, lock->lksb->lvb, DLM_LVB_LEN);
1206 }
1207 }
1208 ml->node = lock->ml.node;
1209 mres->num_locks++;
1210 /* we reached the max, send this network message */
1211 if (mres->num_locks == DLM_MAX_MIGRATABLE_LOCKS)
1212 return 1;
1213 return 0;
1214}
1215
1216
1217int dlm_send_one_lockres(struct dlm_ctxt *dlm, struct dlm_lock_resource *res,
1218 struct dlm_migratable_lockres *mres,
1219 u8 send_to, u8 flags)
1220{
1221 struct list_head *queue, *iter;
1222 int total_locks, i;
1223 u64 mig_cookie = 0;
1224 struct dlm_lock *lock;
1225 int ret = 0;
1226
1227 BUG_ON(!(flags & (DLM_MRES_RECOVERY|DLM_MRES_MIGRATION)));
1228
1229 mlog(0, "sending to %u\n", send_to);
1230
1231 total_locks = dlm_num_locks_in_lockres(res);
1232 if (total_locks > DLM_MAX_MIGRATABLE_LOCKS) {
1233 /* rare, but possible */
1234 mlog(0, "argh. lockres has %d locks. this will "
1235 "require more than one network packet to "
1236 "migrate\n", total_locks);
1237 mig_cookie = dlm_get_next_mig_cookie();
1238 }
1239
1240 dlm_init_migratable_lockres(mres, res->lockname.name,
1241 res->lockname.len, total_locks,
1242 mig_cookie, flags, res->owner);
1243
1244 total_locks = 0;
1245 for (i=DLM_GRANTED_LIST; i<=DLM_BLOCKED_LIST; i++) {
1246 queue = dlm_list_idx_to_ptr(res, i);
1247 list_for_each(iter, queue) {
1248 lock = list_entry (iter, struct dlm_lock, list);
1249
1250 /* add another lock. */
1251 total_locks++;
1252 if (!dlm_add_lock_to_array(lock, mres, i))
1253 continue;
1254
1255 /* this filled the lock message,
1256 * we must send it immediately. */
1257 ret = dlm_send_mig_lockres_msg(dlm, mres, send_to,
1258 res, total_locks);
Kurt Hackel29c0fa02006-04-27 18:06:58 -07001259 if (ret < 0)
1260 goto error;
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001261 }
1262 }
1263 /* flush any remaining locks */
1264 ret = dlm_send_mig_lockres_msg(dlm, mres, send_to, res, total_locks);
Kurt Hackel29c0fa02006-04-27 18:06:58 -07001265 if (ret < 0)
1266 goto error;
1267 return ret;
1268
1269error:
1270 mlog(ML_ERROR, "%s: dlm_send_mig_lockres_msg returned %d\n",
1271 dlm->name, ret);
1272 if (!dlm_is_host_down(ret))
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001273 BUG();
Kurt Hackel29c0fa02006-04-27 18:06:58 -07001274 mlog(0, "%s: node %u went down while sending %s "
1275 "lockres %.*s\n", dlm->name, send_to,
1276 flags & DLM_MRES_RECOVERY ? "recovery" : "migration",
1277 res->lockname.len, res->lockname.name);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001278 return ret;
1279}
1280
1281
1282
1283/*
1284 * this message will contain no more than one page worth of
1285 * recovery data, and it will work on only one lockres.
1286 * there may be many locks in this page, and we may need to wait
1287 * for additional packets to complete all the locks (rare, but
1288 * possible).
1289 */
1290/*
1291 * NOTE: the allocation error cases here are scary
1292 * we really cannot afford to fail an alloc in recovery
1293 * do we spin? returning an error only delays the problem really
1294 */
1295
1296int dlm_mig_lockres_handler(struct o2net_msg *msg, u32 len, void *data)
1297{
1298 struct dlm_ctxt *dlm = data;
1299 struct dlm_migratable_lockres *mres =
1300 (struct dlm_migratable_lockres *)msg->buf;
1301 int ret = 0;
1302 u8 real_master;
1303 char *buf = NULL;
1304 struct dlm_work_item *item = NULL;
1305 struct dlm_lock_resource *res = NULL;
1306
1307 if (!dlm_grab(dlm))
1308 return -EINVAL;
1309
1310 BUG_ON(!(mres->flags & (DLM_MRES_RECOVERY|DLM_MRES_MIGRATION)));
1311
1312 real_master = mres->master;
1313 if (real_master == DLM_LOCK_RES_OWNER_UNKNOWN) {
1314 /* cannot migrate a lockres with no master */
1315 BUG_ON(!(mres->flags & DLM_MRES_RECOVERY));
1316 }
1317
1318 mlog(0, "%s message received from node %u\n",
1319 (mres->flags & DLM_MRES_RECOVERY) ?
1320 "recovery" : "migration", mres->master);
1321 if (mres->flags & DLM_MRES_ALL_DONE)
1322 mlog(0, "all done flag. all lockres data received!\n");
1323
1324 ret = -ENOMEM;
Kurt Hackelad8100e2006-05-01 14:25:21 -07001325 buf = kmalloc(be16_to_cpu(msg->data_len), GFP_NOFS);
1326 item = kcalloc(1, sizeof(*item), GFP_NOFS);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001327 if (!buf || !item)
1328 goto leave;
1329
1330 /* lookup the lock to see if we have a secondary queue for this
1331 * already... just add the locks in and this will have its owner
1332 * and RECOVERY flag changed when it completes. */
1333 res = dlm_lookup_lockres(dlm, mres->lockname, mres->lockname_len);
1334 if (res) {
1335 /* this will get a ref on res */
1336 /* mark it as recovering/migrating and hash it */
1337 spin_lock(&res->spinlock);
1338 if (mres->flags & DLM_MRES_RECOVERY) {
1339 res->state |= DLM_LOCK_RES_RECOVERING;
1340 } else {
1341 if (res->state & DLM_LOCK_RES_MIGRATING) {
1342 /* this is at least the second
1343 * lockres message */
1344 mlog(0, "lock %.*s is already migrating\n",
1345 mres->lockname_len,
1346 mres->lockname);
1347 } else if (res->state & DLM_LOCK_RES_RECOVERING) {
1348 /* caller should BUG */
1349 mlog(ML_ERROR, "node is attempting to migrate "
1350 "lock %.*s, but marked as recovering!\n",
1351 mres->lockname_len, mres->lockname);
1352 ret = -EFAULT;
1353 spin_unlock(&res->spinlock);
1354 goto leave;
1355 }
1356 res->state |= DLM_LOCK_RES_MIGRATING;
1357 }
1358 spin_unlock(&res->spinlock);
1359 } else {
1360 /* need to allocate, just like if it was
1361 * mastered here normally */
1362 res = dlm_new_lockres(dlm, mres->lockname, mres->lockname_len);
1363 if (!res)
1364 goto leave;
1365
1366 /* to match the ref that we would have gotten if
1367 * dlm_lookup_lockres had succeeded */
1368 dlm_lockres_get(res);
1369
1370 /* mark it as recovering/migrating and hash it */
1371 if (mres->flags & DLM_MRES_RECOVERY)
1372 res->state |= DLM_LOCK_RES_RECOVERING;
1373 else
1374 res->state |= DLM_LOCK_RES_MIGRATING;
1375
1376 spin_lock(&dlm->spinlock);
1377 __dlm_insert_lockres(dlm, res);
1378 spin_unlock(&dlm->spinlock);
1379
1380 /* now that the new lockres is inserted,
1381 * make it usable by other processes */
1382 spin_lock(&res->spinlock);
1383 res->state &= ~DLM_LOCK_RES_IN_PROGRESS;
1384 spin_unlock(&res->spinlock);
1385
1386 /* add an extra ref for just-allocated lockres
1387 * otherwise the lockres will be purged immediately */
1388 dlm_lockres_get(res);
1389
1390 }
1391
1392 /* at this point we have allocated everything we need,
1393 * and we have a hashed lockres with an extra ref and
1394 * the proper res->state flags. */
1395 ret = 0;
1396 if (mres->master == DLM_LOCK_RES_OWNER_UNKNOWN) {
1397 /* migration cannot have an unknown master */
1398 BUG_ON(!(mres->flags & DLM_MRES_RECOVERY));
1399 mlog(0, "recovery has passed me a lockres with an "
1400 "unknown owner.. will need to requery: "
1401 "%.*s\n", mres->lockname_len, mres->lockname);
1402 } else {
1403 spin_lock(&res->spinlock);
1404 dlm_change_lockres_owner(dlm, res, dlm->node_num);
1405 spin_unlock(&res->spinlock);
1406 }
1407
1408 /* queue up work for dlm_mig_lockres_worker */
1409 dlm_grab(dlm); /* get an extra ref for the work item */
1410 memcpy(buf, msg->buf, be16_to_cpu(msg->data_len)); /* copy the whole message */
1411 dlm_init_work_item(dlm, item, dlm_mig_lockres_worker, buf);
1412 item->u.ml.lockres = res; /* already have a ref */
1413 item->u.ml.real_master = real_master;
1414 spin_lock(&dlm->work_lock);
1415 list_add_tail(&item->list, &dlm->work_list);
1416 spin_unlock(&dlm->work_lock);
Kurt Hackel3156d262006-05-01 14:39:29 -07001417 queue_work(dlm->dlm_worker, &dlm->dispatched_work);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001418
1419leave:
1420 dlm_put(dlm);
1421 if (ret < 0) {
1422 if (buf)
1423 kfree(buf);
1424 if (item)
1425 kfree(item);
1426 }
1427
1428 mlog_exit(ret);
1429 return ret;
1430}
1431
1432
1433static void dlm_mig_lockres_worker(struct dlm_work_item *item, void *data)
1434{
1435 struct dlm_ctxt *dlm = data;
1436 struct dlm_migratable_lockres *mres;
1437 int ret = 0;
1438 struct dlm_lock_resource *res;
1439 u8 real_master;
1440
1441 dlm = item->dlm;
1442 mres = (struct dlm_migratable_lockres *)data;
1443
1444 res = item->u.ml.lockres;
1445 real_master = item->u.ml.real_master;
1446
1447 if (real_master == DLM_LOCK_RES_OWNER_UNKNOWN) {
1448 /* this case is super-rare. only occurs if
1449 * node death happens during migration. */
1450again:
1451 ret = dlm_lockres_master_requery(dlm, res, &real_master);
1452 if (ret < 0) {
Kurt Hackele2faea42006-01-12 14:24:55 -08001453 mlog(0, "dlm_lockres_master_requery ret=%d\n",
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001454 ret);
1455 goto again;
1456 }
1457 if (real_master == DLM_LOCK_RES_OWNER_UNKNOWN) {
1458 mlog(0, "lockres %.*s not claimed. "
1459 "this node will take it.\n",
1460 res->lockname.len, res->lockname.name);
1461 } else {
1462 mlog(0, "master needs to respond to sender "
1463 "that node %u still owns %.*s\n",
1464 real_master, res->lockname.len,
1465 res->lockname.name);
1466 /* cannot touch this lockres */
1467 goto leave;
1468 }
1469 }
1470
1471 ret = dlm_process_recovery_data(dlm, res, mres);
1472 if (ret < 0)
1473 mlog(0, "dlm_process_recovery_data returned %d\n", ret);
1474 else
1475 mlog(0, "dlm_process_recovery_data succeeded\n");
1476
1477 if ((mres->flags & (DLM_MRES_MIGRATION|DLM_MRES_ALL_DONE)) ==
1478 (DLM_MRES_MIGRATION|DLM_MRES_ALL_DONE)) {
1479 ret = dlm_finish_migration(dlm, res, mres->master);
1480 if (ret < 0)
1481 mlog_errno(ret);
1482 }
1483
1484leave:
1485 kfree(data);
1486 mlog_exit(ret);
1487}
1488
1489
1490
Adrian Bunk8169cae2006-03-31 16:53:55 +02001491static int dlm_lockres_master_requery(struct dlm_ctxt *dlm,
1492 struct dlm_lock_resource *res,
1493 u8 *real_master)
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001494{
1495 struct dlm_node_iter iter;
1496 int nodenum;
1497 int ret = 0;
1498
1499 *real_master = DLM_LOCK_RES_OWNER_UNKNOWN;
1500
1501 /* we only reach here if one of the two nodes in a
1502 * migration died while the migration was in progress.
1503 * at this point we need to requery the master. we
1504 * know that the new_master got as far as creating
1505 * an mle on at least one node, but we do not know
1506 * if any nodes had actually cleared the mle and set
1507 * the master to the new_master. the old master
1508 * is supposed to set the owner to UNKNOWN in the
1509 * event of a new_master death, so the only possible
1510 * responses that we can get from nodes here are
1511 * that the master is new_master, or that the master
1512 * is UNKNOWN.
1513 * if all nodes come back with UNKNOWN then we know
1514 * the lock needs remastering here.
1515 * if any node comes back with a valid master, check
1516 * to see if that master is the one that we are
1517 * recovering. if so, then the new_master died and
1518 * we need to remaster this lock. if not, then the
1519 * new_master survived and that node will respond to
1520 * other nodes about the owner.
1521 * if there is an owner, this node needs to dump this
1522 * lockres and alert the sender that this lockres
1523 * was rejected. */
1524 spin_lock(&dlm->spinlock);
1525 dlm_node_iter_init(dlm->domain_map, &iter);
1526 spin_unlock(&dlm->spinlock);
1527
1528 while ((nodenum = dlm_node_iter_next(&iter)) >= 0) {
1529 /* do not send to self */
1530 if (nodenum == dlm->node_num)
1531 continue;
1532 ret = dlm_do_master_requery(dlm, res, nodenum, real_master);
1533 if (ret < 0) {
1534 mlog_errno(ret);
Kurt Hackelc03872f2006-03-06 14:08:49 -08001535 if (!dlm_is_host_down(ret))
1536 BUG();
1537 /* host is down, so answer for that node would be
1538 * DLM_LOCK_RES_OWNER_UNKNOWN. continue. */
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001539 }
1540 if (*real_master != DLM_LOCK_RES_OWNER_UNKNOWN) {
1541 mlog(0, "lock master is %u\n", *real_master);
1542 break;
1543 }
1544 }
1545 return ret;
1546}
1547
1548
Kurt Hackelc03872f2006-03-06 14:08:49 -08001549int dlm_do_master_requery(struct dlm_ctxt *dlm, struct dlm_lock_resource *res,
1550 u8 nodenum, u8 *real_master)
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001551{
1552 int ret = -EINVAL;
1553 struct dlm_master_requery req;
1554 int status = DLM_LOCK_RES_OWNER_UNKNOWN;
1555
1556 memset(&req, 0, sizeof(req));
1557 req.node_idx = dlm->node_num;
1558 req.namelen = res->lockname.len;
1559 memcpy(req.name, res->lockname.name, res->lockname.len);
1560
1561 ret = o2net_send_message(DLM_MASTER_REQUERY_MSG, dlm->key,
1562 &req, sizeof(req), nodenum, &status);
1563 /* XXX: negative status not handled properly here. */
1564 if (ret < 0)
1565 mlog_errno(ret);
1566 else {
1567 BUG_ON(status < 0);
1568 BUG_ON(status > DLM_LOCK_RES_OWNER_UNKNOWN);
1569 *real_master = (u8) (status & 0xff);
1570 mlog(0, "node %u responded to master requery with %u\n",
1571 nodenum, *real_master);
1572 ret = 0;
1573 }
1574 return ret;
1575}
1576
1577
1578/* this function cannot error, so unless the sending
1579 * or receiving of the message failed, the owner can
1580 * be trusted */
1581int dlm_master_requery_handler(struct o2net_msg *msg, u32 len, void *data)
1582{
1583 struct dlm_ctxt *dlm = data;
1584 struct dlm_master_requery *req = (struct dlm_master_requery *)msg->buf;
1585 struct dlm_lock_resource *res = NULL;
Mark Fasheha3d33292006-03-09 17:55:56 -08001586 unsigned int hash;
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001587 int master = DLM_LOCK_RES_OWNER_UNKNOWN;
1588 u32 flags = DLM_ASSERT_MASTER_REQUERY;
1589
1590 if (!dlm_grab(dlm)) {
1591 /* since the domain has gone away on this
1592 * node, the proper response is UNKNOWN */
1593 return master;
1594 }
1595
Mark Fasheha3d33292006-03-09 17:55:56 -08001596 hash = dlm_lockid_hash(req->name, req->namelen);
1597
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001598 spin_lock(&dlm->spinlock);
Mark Fasheha3d33292006-03-09 17:55:56 -08001599 res = __dlm_lookup_lockres(dlm, req->name, req->namelen, hash);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001600 if (res) {
1601 spin_lock(&res->spinlock);
1602 master = res->owner;
1603 if (master == dlm->node_num) {
1604 int ret = dlm_dispatch_assert_master(dlm, res,
1605 0, 0, flags);
1606 if (ret < 0) {
1607 mlog_errno(-ENOMEM);
1608 /* retry!? */
1609 BUG();
1610 }
1611 }
1612 spin_unlock(&res->spinlock);
1613 }
1614 spin_unlock(&dlm->spinlock);
1615
1616 dlm_put(dlm);
1617 return master;
1618}
1619
1620static inline struct list_head *
1621dlm_list_num_to_pointer(struct dlm_lock_resource *res, int list_num)
1622{
1623 struct list_head *ret;
1624 BUG_ON(list_num < 0);
1625 BUG_ON(list_num > 2);
1626 ret = &(res->granted);
1627 ret += list_num;
1628 return ret;
1629}
1630/* TODO: do ast flush business
1631 * TODO: do MIGRATING and RECOVERING spinning
1632 */
1633
1634/*
1635* NOTE about in-flight requests during migration:
1636*
1637* Before attempting the migrate, the master has marked the lockres as
1638* MIGRATING and then flushed all of its pending ASTS. So any in-flight
1639* requests either got queued before the MIGRATING flag got set, in which
1640* case the lock data will reflect the change and a return message is on
1641* the way, or the request failed to get in before MIGRATING got set. In
1642* this case, the caller will be told to spin and wait for the MIGRATING
1643* flag to be dropped, then recheck the master.
1644* This holds true for the convert, cancel and unlock cases, and since lvb
1645* updates are tied to these same messages, it applies to lvb updates as
1646* well. For the lock case, there is no way a lock can be on the master
1647* queue and not be on the secondary queue since the lock is always added
1648* locally first. This means that the new target node will never be sent
1649* a lock that he doesn't already have on the list.
1650* In total, this means that the local lock is correct and should not be
1651* updated to match the one sent by the master. Any messages sent back
1652* from the master before the MIGRATING flag will bring the lock properly
1653* up-to-date, and the change will be ordered properly for the waiter.
1654* We will *not* attempt to modify the lock underneath the waiter.
1655*/
1656
1657static int dlm_process_recovery_data(struct dlm_ctxt *dlm,
1658 struct dlm_lock_resource *res,
1659 struct dlm_migratable_lockres *mres)
1660{
1661 struct dlm_migratable_lock *ml;
1662 struct list_head *queue;
1663 struct dlm_lock *newlock = NULL;
1664 struct dlm_lockstatus *lksb = NULL;
1665 int ret = 0;
Kurt Hackelc3187ce2006-04-27 18:05:41 -07001666 int i, bad;
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001667 struct list_head *iter;
1668 struct dlm_lock *lock = NULL;
1669
1670 mlog(0, "running %d locks for this lockres\n", mres->num_locks);
1671 for (i=0; i<mres->num_locks; i++) {
1672 ml = &(mres->ml[i]);
1673 BUG_ON(ml->highest_blocked != LKM_IVMODE);
1674 newlock = NULL;
1675 lksb = NULL;
1676
1677 queue = dlm_list_num_to_pointer(res, ml->list);
1678
1679 /* if the lock is for the local node it needs to
1680 * be moved to the proper location within the queue.
1681 * do not allocate a new lock structure. */
1682 if (ml->node == dlm->node_num) {
1683 /* MIGRATION ONLY! */
1684 BUG_ON(!(mres->flags & DLM_MRES_MIGRATION));
1685
1686 spin_lock(&res->spinlock);
1687 list_for_each(iter, queue) {
1688 lock = list_entry (iter, struct dlm_lock, list);
1689 if (lock->ml.cookie != ml->cookie)
1690 lock = NULL;
1691 else
1692 break;
1693 }
1694
1695 /* lock is always created locally first, and
1696 * destroyed locally last. it must be on the list */
1697 if (!lock) {
Kurt Hackel29004852006-03-02 16:43:36 -08001698 u64 c = ml->cookie;
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001699 mlog(ML_ERROR, "could not find local lock "
Kurt Hackel29004852006-03-02 16:43:36 -08001700 "with cookie %u:%llu!\n",
1701 dlm_get_lock_cookie_node(c),
1702 dlm_get_lock_cookie_seq(c));
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001703 BUG();
1704 }
1705 BUG_ON(lock->ml.node != ml->node);
1706
1707 /* see NOTE above about why we do not update
1708 * to match the master here */
1709
1710 /* move the lock to its proper place */
1711 /* do not alter lock refcount. switching lists. */
Akinobu Mitaf1166292006-06-26 00:24:46 -07001712 list_move_tail(&lock->list, queue);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001713 spin_unlock(&res->spinlock);
1714
1715 mlog(0, "just reordered a local lock!\n");
1716 continue;
1717 }
1718
1719 /* lock is for another node. */
1720 newlock = dlm_new_lock(ml->type, ml->node,
1721 be64_to_cpu(ml->cookie), NULL);
1722 if (!newlock) {
1723 ret = -ENOMEM;
1724 goto leave;
1725 }
1726 lksb = newlock->lksb;
1727 dlm_lock_attach_lockres(newlock, res);
1728
1729 if (ml->convert_type != LKM_IVMODE) {
1730 BUG_ON(queue != &res->converting);
1731 newlock->ml.convert_type = ml->convert_type;
1732 }
1733 lksb->flags |= (ml->flags &
1734 (DLM_LKSB_PUT_LVB|DLM_LKSB_GET_LVB));
Kurt Hackelccd8b1f2006-05-01 11:32:14 -07001735
1736 if (ml->type == LKM_NLMODE)
1737 goto skip_lvb;
1738
Kurt Hackel8bc674c2006-04-27 18:02:10 -07001739 if (!dlm_lvb_is_empty(mres->lvb)) {
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001740 if (lksb->flags & DLM_LKSB_PUT_LVB) {
1741 /* other node was trying to update
1742 * lvb when node died. recreate the
1743 * lksb with the updated lvb. */
1744 memcpy(lksb->lvb, mres->lvb, DLM_LVB_LEN);
Kurt Hackelccd8b1f2006-05-01 11:32:14 -07001745 /* the lock resource lvb update must happen
1746 * NOW, before the spinlock is dropped.
1747 * we no longer wait for the AST to update
1748 * the lvb. */
1749 memcpy(res->lvb, mres->lvb, DLM_LVB_LEN);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001750 } else {
1751 /* otherwise, the node is sending its
1752 * most recent valid lvb info */
1753 BUG_ON(ml->type != LKM_EXMODE &&
1754 ml->type != LKM_PRMODE);
Kurt Hackel8bc674c2006-04-27 18:02:10 -07001755 if (!dlm_lvb_is_empty(res->lvb) &&
Kurt Hackelccd8b1f2006-05-01 11:32:14 -07001756 (ml->type == LKM_EXMODE ||
1757 memcmp(res->lvb, mres->lvb, DLM_LVB_LEN))) {
1758 int i;
1759 mlog(ML_ERROR, "%s:%.*s: received bad "
1760 "lvb! type=%d\n", dlm->name,
1761 res->lockname.len,
1762 res->lockname.name, ml->type);
1763 printk("lockres lvb=[");
1764 for (i=0; i<DLM_LVB_LEN; i++)
1765 printk("%02x", res->lvb[i]);
1766 printk("]\nmigrated lvb=[");
1767 for (i=0; i<DLM_LVB_LEN; i++)
1768 printk("%02x", mres->lvb[i]);
1769 printk("]\n");
1770 dlm_print_one_lock_resource(res);
1771 BUG();
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001772 }
1773 memcpy(res->lvb, mres->lvb, DLM_LVB_LEN);
1774 }
1775 }
Kurt Hackelccd8b1f2006-05-01 11:32:14 -07001776skip_lvb:
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001777
1778 /* NOTE:
1779 * wrt lock queue ordering and recovery:
1780 * 1. order of locks on granted queue is
1781 * meaningless.
1782 * 2. order of locks on converting queue is
1783 * LOST with the node death. sorry charlie.
1784 * 3. order of locks on the blocked queue is
1785 * also LOST.
1786 * order of locks does not affect integrity, it
1787 * just means that a lock request may get pushed
1788 * back in line as a result of the node death.
1789 * also note that for a given node the lock order
1790 * for its secondary queue locks is preserved
1791 * relative to each other, but clearly *not*
1792 * preserved relative to locks from other nodes.
1793 */
Kurt Hackelc3187ce2006-04-27 18:05:41 -07001794 bad = 0;
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001795 spin_lock(&res->spinlock);
Kurt Hackelc3187ce2006-04-27 18:05:41 -07001796 list_for_each_entry(lock, queue, list) {
1797 if (lock->ml.cookie == ml->cookie) {
1798 u64 c = lock->ml.cookie;
1799 mlog(ML_ERROR, "%s:%.*s: %u:%llu: lock already "
1800 "exists on this lockres!\n", dlm->name,
1801 res->lockname.len, res->lockname.name,
1802 dlm_get_lock_cookie_node(c),
1803 dlm_get_lock_cookie_seq(c));
1804
1805 mlog(ML_NOTICE, "sent lock: type=%d, conv=%d, "
1806 "node=%u, cookie=%u:%llu, queue=%d\n",
1807 ml->type, ml->convert_type, ml->node,
1808 dlm_get_lock_cookie_node(ml->cookie),
1809 dlm_get_lock_cookie_seq(ml->cookie),
1810 ml->list);
1811
1812 __dlm_print_one_lock_resource(res);
1813 bad = 1;
1814 break;
1815 }
1816 }
1817 if (!bad) {
1818 dlm_lock_get(newlock);
1819 list_add_tail(&newlock->list, queue);
1820 }
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001821 spin_unlock(&res->spinlock);
1822 }
1823 mlog(0, "done running all the locks\n");
1824
1825leave:
1826 if (ret < 0) {
1827 mlog_errno(ret);
1828 if (newlock)
1829 dlm_lock_put(newlock);
1830 }
1831
1832 mlog_exit(ret);
1833 return ret;
1834}
1835
1836void dlm_move_lockres_to_recovery_list(struct dlm_ctxt *dlm,
1837 struct dlm_lock_resource *res)
1838{
1839 int i;
1840 struct list_head *queue, *iter, *iter2;
1841 struct dlm_lock *lock;
1842
1843 res->state |= DLM_LOCK_RES_RECOVERING;
Kurt Hackel69d72b02006-05-01 10:57:51 -07001844 if (!list_empty(&res->recovering)) {
1845 mlog(0,
1846 "Recovering res %s:%.*s, is already on recovery list!\n",
1847 dlm->name, res->lockname.len, res->lockname.name);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001848 list_del_init(&res->recovering);
Kurt Hackel69d72b02006-05-01 10:57:51 -07001849 }
1850 /* We need to hold a reference while on the recovery list */
1851 dlm_lockres_get(res);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001852 list_add_tail(&res->recovering, &dlm->reco.resources);
1853
1854 /* find any pending locks and put them back on proper list */
1855 for (i=DLM_BLOCKED_LIST; i>=DLM_GRANTED_LIST; i--) {
1856 queue = dlm_list_idx_to_ptr(res, i);
1857 list_for_each_safe(iter, iter2, queue) {
1858 lock = list_entry (iter, struct dlm_lock, list);
1859 dlm_lock_get(lock);
1860 if (lock->convert_pending) {
1861 /* move converting lock back to granted */
1862 BUG_ON(i != DLM_CONVERTING_LIST);
1863 mlog(0, "node died with convert pending "
1864 "on %.*s. move back to granted list.\n",
1865 res->lockname.len, res->lockname.name);
1866 dlm_revert_pending_convert(res, lock);
1867 lock->convert_pending = 0;
1868 } else if (lock->lock_pending) {
1869 /* remove pending lock requests completely */
1870 BUG_ON(i != DLM_BLOCKED_LIST);
1871 mlog(0, "node died with lock pending "
1872 "on %.*s. remove from blocked list and skip.\n",
1873 res->lockname.len, res->lockname.name);
1874 /* lock will be floating until ref in
1875 * dlmlock_remote is freed after the network
1876 * call returns. ok for it to not be on any
1877 * list since no ast can be called
1878 * (the master is dead). */
1879 dlm_revert_pending_lock(res, lock);
1880 lock->lock_pending = 0;
1881 } else if (lock->unlock_pending) {
1882 /* if an unlock was in progress, treat as
1883 * if this had completed successfully
1884 * before sending this lock state to the
1885 * new master. note that the dlm_unlock
1886 * call is still responsible for calling
1887 * the unlockast. that will happen after
1888 * the network call times out. for now,
1889 * just move lists to prepare the new
1890 * recovery master. */
1891 BUG_ON(i != DLM_GRANTED_LIST);
1892 mlog(0, "node died with unlock pending "
1893 "on %.*s. remove from blocked list and skip.\n",
1894 res->lockname.len, res->lockname.name);
1895 dlm_commit_pending_unlock(res, lock);
1896 lock->unlock_pending = 0;
1897 } else if (lock->cancel_pending) {
1898 /* if a cancel was in progress, treat as
1899 * if this had completed successfully
1900 * before sending this lock state to the
1901 * new master */
1902 BUG_ON(i != DLM_CONVERTING_LIST);
1903 mlog(0, "node died with cancel pending "
1904 "on %.*s. move back to granted list.\n",
1905 res->lockname.len, res->lockname.name);
1906 dlm_commit_pending_cancel(res, lock);
1907 lock->cancel_pending = 0;
1908 }
1909 dlm_lock_put(lock);
1910 }
1911 }
1912}
1913
1914
1915
1916/* removes all recovered locks from the recovery list.
1917 * sets the res->owner to the new master.
1918 * unsets the RECOVERY flag and wakes waiters. */
1919static void dlm_finish_local_lockres_recovery(struct dlm_ctxt *dlm,
1920 u8 dead_node, u8 new_master)
1921{
1922 int i;
Mark Fasheh81f20942006-02-28 17:31:22 -08001923 struct list_head *iter, *iter2;
1924 struct hlist_node *hash_iter;
1925 struct hlist_head *bucket;
1926
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001927 struct dlm_lock_resource *res;
1928
1929 mlog_entry_void();
1930
1931 assert_spin_locked(&dlm->spinlock);
1932
1933 list_for_each_safe(iter, iter2, &dlm->reco.resources) {
1934 res = list_entry (iter, struct dlm_lock_resource, recovering);
1935 if (res->owner == dead_node) {
1936 list_del_init(&res->recovering);
1937 spin_lock(&res->spinlock);
1938 dlm_change_lockres_owner(dlm, res, new_master);
1939 res->state &= ~DLM_LOCK_RES_RECOVERING;
Kurt Hackel69d72b02006-05-01 10:57:51 -07001940 if (!__dlm_lockres_unused(res))
1941 __dlm_dirty_lockres(dlm, res);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001942 spin_unlock(&res->spinlock);
1943 wake_up(&res->wq);
Kurt Hackel69d72b02006-05-01 10:57:51 -07001944 dlm_lockres_put(res);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001945 }
1946 }
1947
1948 /* this will become unnecessary eventually, but
1949 * for now we need to run the whole hash, clear
1950 * the RECOVERING state and set the owner
1951 * if necessary */
Mark Fasheh81f20942006-02-28 17:31:22 -08001952 for (i = 0; i < DLM_HASH_BUCKETS; i++) {
Daniel Phillips03d864c2006-03-10 18:08:16 -08001953 bucket = dlm_lockres_hash(dlm, i);
Mark Fasheh81f20942006-02-28 17:31:22 -08001954 hlist_for_each_entry(res, hash_iter, bucket, hash_node) {
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001955 if (res->state & DLM_LOCK_RES_RECOVERING) {
1956 if (res->owner == dead_node) {
1957 mlog(0, "(this=%u) res %.*s owner=%u "
1958 "was not on recovering list, but "
1959 "clearing state anyway\n",
1960 dlm->node_num, res->lockname.len,
1961 res->lockname.name, new_master);
1962 } else if (res->owner == dlm->node_num) {
1963 mlog(0, "(this=%u) res %.*s owner=%u "
1964 "was not on recovering list, "
1965 "owner is THIS node, clearing\n",
1966 dlm->node_num, res->lockname.len,
1967 res->lockname.name, new_master);
1968 } else
1969 continue;
1970
Kurt Hackelc03872f2006-03-06 14:08:49 -08001971 if (!list_empty(&res->recovering)) {
1972 mlog(0, "%s:%.*s: lockres was "
1973 "marked RECOVERING, owner=%u\n",
1974 dlm->name, res->lockname.len,
1975 res->lockname.name, res->owner);
1976 list_del_init(&res->recovering);
Kurt Hackel69d72b02006-05-01 10:57:51 -07001977 dlm_lockres_put(res);
Kurt Hackelc03872f2006-03-06 14:08:49 -08001978 }
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001979 spin_lock(&res->spinlock);
1980 dlm_change_lockres_owner(dlm, res, new_master);
1981 res->state &= ~DLM_LOCK_RES_RECOVERING;
Kurt Hackel69d72b02006-05-01 10:57:51 -07001982 if (!__dlm_lockres_unused(res))
1983 __dlm_dirty_lockres(dlm, res);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001984 spin_unlock(&res->spinlock);
1985 wake_up(&res->wq);
1986 }
1987 }
1988 }
1989}
1990
1991static inline int dlm_lvb_needs_invalidation(struct dlm_lock *lock, int local)
1992{
1993 if (local) {
1994 if (lock->ml.type != LKM_EXMODE &&
1995 lock->ml.type != LKM_PRMODE)
1996 return 1;
1997 } else if (lock->ml.type == LKM_EXMODE)
1998 return 1;
1999 return 0;
2000}
2001
2002static void dlm_revalidate_lvb(struct dlm_ctxt *dlm,
2003 struct dlm_lock_resource *res, u8 dead_node)
2004{
2005 struct list_head *iter, *queue;
2006 struct dlm_lock *lock;
2007 int blank_lvb = 0, local = 0;
2008 int i;
2009 u8 search_node;
2010
2011 assert_spin_locked(&dlm->spinlock);
2012 assert_spin_locked(&res->spinlock);
2013
2014 if (res->owner == dlm->node_num)
2015 /* if this node owned the lockres, and if the dead node
2016 * had an EX when he died, blank out the lvb */
2017 search_node = dead_node;
2018 else {
2019 /* if this is a secondary lockres, and we had no EX or PR
2020 * locks granted, we can no longer trust the lvb */
2021 search_node = dlm->node_num;
2022 local = 1; /* check local state for valid lvb */
2023 }
2024
2025 for (i=DLM_GRANTED_LIST; i<=DLM_CONVERTING_LIST; i++) {
2026 queue = dlm_list_idx_to_ptr(res, i);
2027 list_for_each(iter, queue) {
2028 lock = list_entry (iter, struct dlm_lock, list);
2029 if (lock->ml.node == search_node) {
2030 if (dlm_lvb_needs_invalidation(lock, local)) {
2031 /* zero the lksb lvb and lockres lvb */
2032 blank_lvb = 1;
2033 memset(lock->lksb->lvb, 0, DLM_LVB_LEN);
2034 }
2035 }
2036 }
2037 }
2038
2039 if (blank_lvb) {
2040 mlog(0, "clearing %.*s lvb, dead node %u had EX\n",
2041 res->lockname.len, res->lockname.name, dead_node);
2042 memset(res->lvb, 0, DLM_LVB_LEN);
2043 }
2044}
2045
2046static void dlm_free_dead_locks(struct dlm_ctxt *dlm,
2047 struct dlm_lock_resource *res, u8 dead_node)
2048{
2049 struct list_head *iter, *tmpiter;
2050 struct dlm_lock *lock;
2051
2052 /* this node is the lockres master:
2053 * 1) remove any stale locks for the dead node
2054 * 2) if the dead node had an EX when he died, blank out the lvb
2055 */
2056 assert_spin_locked(&dlm->spinlock);
2057 assert_spin_locked(&res->spinlock);
2058
2059 /* TODO: check pending_asts, pending_basts here */
2060 list_for_each_safe(iter, tmpiter, &res->granted) {
2061 lock = list_entry (iter, struct dlm_lock, list);
2062 if (lock->ml.node == dead_node) {
2063 list_del_init(&lock->list);
2064 dlm_lock_put(lock);
2065 }
2066 }
2067 list_for_each_safe(iter, tmpiter, &res->converting) {
2068 lock = list_entry (iter, struct dlm_lock, list);
2069 if (lock->ml.node == dead_node) {
2070 list_del_init(&lock->list);
2071 dlm_lock_put(lock);
2072 }
2073 }
2074 list_for_each_safe(iter, tmpiter, &res->blocked) {
2075 lock = list_entry (iter, struct dlm_lock, list);
2076 if (lock->ml.node == dead_node) {
2077 list_del_init(&lock->list);
2078 dlm_lock_put(lock);
2079 }
2080 }
2081
2082 /* do not kick thread yet */
2083 __dlm_dirty_lockres(dlm, res);
2084}
2085
2086/* if this node is the recovery master, and there are no
2087 * locks for a given lockres owned by this node that are in
2088 * either PR or EX mode, zero out the lvb before requesting.
2089 *
2090 */
2091
2092
2093static void dlm_do_local_recovery_cleanup(struct dlm_ctxt *dlm, u8 dead_node)
2094{
Mark Fasheh81f20942006-02-28 17:31:22 -08002095 struct hlist_node *iter;
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002096 struct dlm_lock_resource *res;
2097 int i;
Mark Fasheh81f20942006-02-28 17:31:22 -08002098 struct hlist_head *bucket;
Kurt Hackele2faea42006-01-12 14:24:55 -08002099 struct dlm_lock *lock;
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002100
2101
2102 /* purge any stale mles */
2103 dlm_clean_master_list(dlm, dead_node);
2104
2105 /*
2106 * now clean up all lock resources. there are two rules:
2107 *
2108 * 1) if the dead node was the master, move the lockres
2109 * to the recovering list. set the RECOVERING flag.
2110 * this lockres needs to be cleaned up before it can
2111 * be used further.
2112 *
2113 * 2) if this node was the master, remove all locks from
2114 * each of the lockres queues that were owned by the
2115 * dead node. once recovery finishes, the dlm thread
2116 * can be kicked again to see if any ASTs or BASTs
2117 * need to be fired as a result.
2118 */
Mark Fasheh81f20942006-02-28 17:31:22 -08002119 for (i = 0; i < DLM_HASH_BUCKETS; i++) {
Daniel Phillips03d864c2006-03-10 18:08:16 -08002120 bucket = dlm_lockres_hash(dlm, i);
Mark Fasheh81f20942006-02-28 17:31:22 -08002121 hlist_for_each_entry(res, iter, bucket, hash_node) {
Kurt Hackele2faea42006-01-12 14:24:55 -08002122 /* always prune any $RECOVERY entries for dead nodes,
2123 * otherwise hangs can occur during later recovery */
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002124 if (dlm_is_recovery_lock(res->lockname.name,
Kurt Hackele2faea42006-01-12 14:24:55 -08002125 res->lockname.len)) {
2126 spin_lock(&res->spinlock);
2127 list_for_each_entry(lock, &res->granted, list) {
2128 if (lock->ml.node == dead_node) {
2129 mlog(0, "AHA! there was "
2130 "a $RECOVERY lock for dead "
2131 "node %u (%s)!\n",
2132 dead_node, dlm->name);
2133 list_del_init(&lock->list);
2134 dlm_lock_put(lock);
2135 break;
2136 }
2137 }
2138 spin_unlock(&res->spinlock);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002139 continue;
Kurt Hackele2faea42006-01-12 14:24:55 -08002140 }
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002141 spin_lock(&res->spinlock);
2142 /* zero the lvb if necessary */
2143 dlm_revalidate_lvb(dlm, res, dead_node);
2144 if (res->owner == dead_node)
2145 dlm_move_lockres_to_recovery_list(dlm, res);
2146 else if (res->owner == dlm->node_num) {
2147 dlm_free_dead_locks(dlm, res, dead_node);
2148 __dlm_lockres_calc_usage(dlm, res);
2149 }
2150 spin_unlock(&res->spinlock);
2151 }
2152 }
2153
2154}
2155
2156static void __dlm_hb_node_down(struct dlm_ctxt *dlm, int idx)
2157{
2158 assert_spin_locked(&dlm->spinlock);
2159
Kurt Hackel466d1a42006-05-01 11:11:13 -07002160 if (dlm->reco.new_master == idx) {
2161 mlog(0, "%s: recovery master %d just died\n",
2162 dlm->name, idx);
2163 if (dlm->reco.state & DLM_RECO_STATE_FINALIZE) {
2164 /* finalize1 was reached, so it is safe to clear
2165 * the new_master and dead_node. that recovery
2166 * is complete. */
2167 mlog(0, "%s: dead master %d had reached "
2168 "finalize1 state, clearing\n", dlm->name, idx);
2169 dlm->reco.state &= ~DLM_RECO_STATE_FINALIZE;
2170 __dlm_reset_recovery(dlm);
2171 }
2172 }
2173
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002174 /* check to see if the node is already considered dead */
2175 if (!test_bit(idx, dlm->live_nodes_map)) {
2176 mlog(0, "for domain %s, node %d is already dead. "
2177 "another node likely did recovery already.\n",
2178 dlm->name, idx);
2179 return;
2180 }
2181
2182 /* check to see if we do not care about this node */
2183 if (!test_bit(idx, dlm->domain_map)) {
2184 /* This also catches the case that we get a node down
2185 * but haven't joined the domain yet. */
2186 mlog(0, "node %u already removed from domain!\n", idx);
2187 return;
2188 }
2189
2190 clear_bit(idx, dlm->live_nodes_map);
2191
2192 /* Clean up join state on node death. */
2193 if (dlm->joining_node == idx) {
2194 mlog(0, "Clearing join state for node %u\n", idx);
2195 __dlm_set_joining_node(dlm, DLM_LOCK_RES_OWNER_UNKNOWN);
2196 }
2197
2198 /* make sure local cleanup occurs before the heartbeat events */
2199 if (!test_bit(idx, dlm->recovery_map))
2200 dlm_do_local_recovery_cleanup(dlm, idx);
2201
2202 /* notify anything attached to the heartbeat events */
2203 dlm_hb_event_notify_attached(dlm, idx, 0);
2204
2205 mlog(0, "node %u being removed from domain map!\n", idx);
2206 clear_bit(idx, dlm->domain_map);
2207 /* wake up migration waiters if a node goes down.
2208 * perhaps later we can genericize this for other waiters. */
2209 wake_up(&dlm->migration_wq);
2210
2211 if (test_bit(idx, dlm->recovery_map))
2212 mlog(0, "domain %s, node %u already added "
2213 "to recovery map!\n", dlm->name, idx);
2214 else
2215 set_bit(idx, dlm->recovery_map);
2216}
2217
2218void dlm_hb_node_down_cb(struct o2nm_node *node, int idx, void *data)
2219{
2220 struct dlm_ctxt *dlm = data;
2221
2222 if (!dlm_grab(dlm))
2223 return;
2224
2225 spin_lock(&dlm->spinlock);
2226 __dlm_hb_node_down(dlm, idx);
2227 spin_unlock(&dlm->spinlock);
2228
2229 dlm_put(dlm);
2230}
2231
2232void dlm_hb_node_up_cb(struct o2nm_node *node, int idx, void *data)
2233{
2234 struct dlm_ctxt *dlm = data;
2235
2236 if (!dlm_grab(dlm))
2237 return;
2238
2239 spin_lock(&dlm->spinlock);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002240 set_bit(idx, dlm->live_nodes_map);
Kurt Hackele2faea42006-01-12 14:24:55 -08002241 /* do NOT notify mle attached to the heartbeat events.
2242 * new nodes are not interesting in mastery until joined. */
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002243 spin_unlock(&dlm->spinlock);
2244
2245 dlm_put(dlm);
2246}
2247
2248static void dlm_reco_ast(void *astdata)
2249{
2250 struct dlm_ctxt *dlm = astdata;
2251 mlog(0, "ast for recovery lock fired!, this=%u, dlm=%s\n",
2252 dlm->node_num, dlm->name);
2253}
2254static void dlm_reco_bast(void *astdata, int blocked_type)
2255{
2256 struct dlm_ctxt *dlm = astdata;
2257 mlog(0, "bast for recovery lock fired!, this=%u, dlm=%s\n",
2258 dlm->node_num, dlm->name);
2259}
2260static void dlm_reco_unlock_ast(void *astdata, enum dlm_status st)
2261{
2262 mlog(0, "unlockast for recovery lock fired!\n");
2263}
2264
Kurt Hackele2faea42006-01-12 14:24:55 -08002265/*
2266 * dlm_pick_recovery_master will continually attempt to use
2267 * dlmlock() on the special "$RECOVERY" lockres with the
2268 * LKM_NOQUEUE flag to get an EX. every thread that enters
2269 * this function on each node racing to become the recovery
2270 * master will not stop attempting this until either:
2271 * a) this node gets the EX (and becomes the recovery master),
2272 * or b) dlm->reco.new_master gets set to some nodenum
2273 * != O2NM_INVALID_NODE_NUM (another node will do the reco).
2274 * so each time a recovery master is needed, the entire cluster
2275 * will sync at this point. if the new master dies, that will
2276 * be detected in dlm_do_recovery */
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002277static int dlm_pick_recovery_master(struct dlm_ctxt *dlm)
2278{
2279 enum dlm_status ret;
2280 struct dlm_lockstatus lksb;
2281 int status = -EINVAL;
2282
2283 mlog(0, "starting recovery of %s at %lu, dead=%u, this=%u\n",
2284 dlm->name, jiffies, dlm->reco.dead_node, dlm->node_num);
Kurt Hackele2faea42006-01-12 14:24:55 -08002285again:
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002286 memset(&lksb, 0, sizeof(lksb));
2287
2288 ret = dlmlock(dlm, LKM_EXMODE, &lksb, LKM_NOQUEUE|LKM_RECOVERY,
Mark Fasheh3384f3d2006-09-08 11:38:29 -07002289 DLM_RECOVERY_LOCK_NAME, DLM_RECOVERY_LOCK_NAME_LEN,
2290 dlm_reco_ast, dlm, dlm_reco_bast);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002291
Kurt Hackele2faea42006-01-12 14:24:55 -08002292 mlog(0, "%s: dlmlock($RECOVERY) returned %d, lksb=%d\n",
2293 dlm->name, ret, lksb.status);
2294
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002295 if (ret == DLM_NORMAL) {
2296 mlog(0, "dlm=%s dlmlock says I got it (this=%u)\n",
2297 dlm->name, dlm->node_num);
Kurt Hackele2faea42006-01-12 14:24:55 -08002298
2299 /* got the EX lock. check to see if another node
2300 * just became the reco master */
2301 if (dlm_reco_master_ready(dlm)) {
2302 mlog(0, "%s: got reco EX lock, but %u will "
2303 "do the recovery\n", dlm->name,
2304 dlm->reco.new_master);
2305 status = -EEXIST;
2306 } else {
Kurt Hackel898effa2006-01-18 17:01:25 -08002307 status = 0;
2308
2309 /* see if recovery was already finished elsewhere */
2310 spin_lock(&dlm->spinlock);
2311 if (dlm->reco.dead_node == O2NM_INVALID_NODE_NUM) {
2312 status = -EINVAL;
2313 mlog(0, "%s: got reco EX lock, but "
2314 "node got recovered already\n", dlm->name);
2315 if (dlm->reco.new_master != O2NM_INVALID_NODE_NUM) {
2316 mlog(ML_ERROR, "%s: new master is %u "
2317 "but no dead node!\n",
2318 dlm->name, dlm->reco.new_master);
2319 BUG();
2320 }
2321 }
2322 spin_unlock(&dlm->spinlock);
2323 }
2324
2325 /* if this node has actually become the recovery master,
2326 * set the master and send the messages to begin recovery */
2327 if (!status) {
2328 mlog(0, "%s: dead=%u, this=%u, sending "
2329 "begin_reco now\n", dlm->name,
2330 dlm->reco.dead_node, dlm->node_num);
Kurt Hackele2faea42006-01-12 14:24:55 -08002331 status = dlm_send_begin_reco_message(dlm,
2332 dlm->reco.dead_node);
2333 /* this always succeeds */
2334 BUG_ON(status);
2335
2336 /* set the new_master to this node */
2337 spin_lock(&dlm->spinlock);
Kurt Hackelab27eb62006-04-27 18:03:49 -07002338 dlm_set_reco_master(dlm, dlm->node_num);
Kurt Hackele2faea42006-01-12 14:24:55 -08002339 spin_unlock(&dlm->spinlock);
2340 }
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002341
2342 /* recovery lock is a special case. ast will not get fired,
2343 * so just go ahead and unlock it. */
2344 ret = dlmunlock(dlm, &lksb, 0, dlm_reco_unlock_ast, dlm);
Kurt Hackele2faea42006-01-12 14:24:55 -08002345 if (ret == DLM_DENIED) {
2346 mlog(0, "got DLM_DENIED, trying LKM_CANCEL\n");
2347 ret = dlmunlock(dlm, &lksb, LKM_CANCEL, dlm_reco_unlock_ast, dlm);
2348 }
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002349 if (ret != DLM_NORMAL) {
2350 /* this would really suck. this could only happen
2351 * if there was a network error during the unlock
2352 * because of node death. this means the unlock
2353 * is actually "done" and the lock structure is
2354 * even freed. we can continue, but only
2355 * because this specific lock name is special. */
Kurt Hackele2faea42006-01-12 14:24:55 -08002356 mlog(ML_ERROR, "dlmunlock returned %d\n", ret);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002357 }
2358 } else if (ret == DLM_NOTQUEUED) {
2359 mlog(0, "dlm=%s dlmlock says another node got it (this=%u)\n",
2360 dlm->name, dlm->node_num);
2361 /* another node is master. wait on
Kurt Hackele2faea42006-01-12 14:24:55 -08002362 * reco.new_master != O2NM_INVALID_NODE_NUM
2363 * for at most one second */
2364 wait_event_timeout(dlm->dlm_reco_thread_wq,
2365 dlm_reco_master_ready(dlm),
2366 msecs_to_jiffies(1000));
2367 if (!dlm_reco_master_ready(dlm)) {
2368 mlog(0, "%s: reco master taking awhile\n",
2369 dlm->name);
2370 goto again;
2371 }
2372 /* another node has informed this one that it is reco master */
2373 mlog(0, "%s: reco master %u is ready to recover %u\n",
2374 dlm->name, dlm->reco.new_master, dlm->reco.dead_node);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002375 status = -EEXIST;
Kurt Hackelc8df4122006-05-01 13:47:50 -07002376 } else if (ret == DLM_RECOVERING) {
2377 mlog(0, "dlm=%s dlmlock says master node died (this=%u)\n",
2378 dlm->name, dlm->node_num);
2379 goto again;
Kurt Hackele2faea42006-01-12 14:24:55 -08002380 } else {
2381 struct dlm_lock_resource *res;
2382
2383 /* dlmlock returned something other than NOTQUEUED or NORMAL */
2384 mlog(ML_ERROR, "%s: got %s from dlmlock($RECOVERY), "
2385 "lksb.status=%s\n", dlm->name, dlm_errname(ret),
2386 dlm_errname(lksb.status));
2387 res = dlm_lookup_lockres(dlm, DLM_RECOVERY_LOCK_NAME,
2388 DLM_RECOVERY_LOCK_NAME_LEN);
2389 if (res) {
2390 dlm_print_one_lock_resource(res);
2391 dlm_lockres_put(res);
2392 } else {
2393 mlog(ML_ERROR, "recovery lock not found\n");
2394 }
2395 BUG();
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002396 }
2397
2398 return status;
2399}
2400
2401static int dlm_send_begin_reco_message(struct dlm_ctxt *dlm, u8 dead_node)
2402{
2403 struct dlm_begin_reco br;
2404 int ret = 0;
2405 struct dlm_node_iter iter;
2406 int nodenum;
2407 int status;
2408
2409 mlog_entry("%u\n", dead_node);
2410
Kurt Hackeld6dea6e2006-04-27 18:08:51 -07002411 mlog(0, "%s: dead node is %u\n", dlm->name, dead_node);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002412
2413 spin_lock(&dlm->spinlock);
2414 dlm_node_iter_init(dlm->domain_map, &iter);
2415 spin_unlock(&dlm->spinlock);
2416
2417 clear_bit(dead_node, iter.node_map);
2418
2419 memset(&br, 0, sizeof(br));
2420 br.node_idx = dlm->node_num;
2421 br.dead_node = dead_node;
2422
2423 while ((nodenum = dlm_node_iter_next(&iter)) >= 0) {
2424 ret = 0;
2425 if (nodenum == dead_node) {
2426 mlog(0, "not sending begin reco to dead node "
2427 "%u\n", dead_node);
2428 continue;
2429 }
2430 if (nodenum == dlm->node_num) {
2431 mlog(0, "not sending begin reco to self\n");
2432 continue;
2433 }
Kurt Hackele2faea42006-01-12 14:24:55 -08002434retry:
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002435 ret = -EINVAL;
2436 mlog(0, "attempting to send begin reco msg to %d\n",
2437 nodenum);
2438 ret = o2net_send_message(DLM_BEGIN_RECO_MSG, dlm->key,
2439 &br, sizeof(br), nodenum, &status);
2440 /* negative status is handled ok by caller here */
2441 if (ret >= 0)
2442 ret = status;
Kurt Hackele2faea42006-01-12 14:24:55 -08002443 if (dlm_is_host_down(ret)) {
2444 /* node is down. not involved in recovery
2445 * so just keep going */
2446 mlog(0, "%s: node %u was down when sending "
2447 "begin reco msg (%d)\n", dlm->name, nodenum, ret);
2448 ret = 0;
2449 }
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002450 if (ret < 0) {
2451 struct dlm_lock_resource *res;
Kurt Hackele2faea42006-01-12 14:24:55 -08002452 /* this is now a serious problem, possibly ENOMEM
2453 * in the network stack. must retry */
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002454 mlog_errno(ret);
2455 mlog(ML_ERROR, "begin reco of dlm %s to node %u "
2456 " returned %d\n", dlm->name, nodenum, ret);
2457 res = dlm_lookup_lockres(dlm, DLM_RECOVERY_LOCK_NAME,
2458 DLM_RECOVERY_LOCK_NAME_LEN);
2459 if (res) {
2460 dlm_print_one_lock_resource(res);
2461 dlm_lockres_put(res);
2462 } else {
2463 mlog(ML_ERROR, "recovery lock not found\n");
2464 }
Kurt Hackele2faea42006-01-12 14:24:55 -08002465 /* sleep for a bit in hopes that we can avoid
2466 * another ENOMEM */
2467 msleep(100);
2468 goto retry;
Kurt Hackel466d1a42006-05-01 11:11:13 -07002469 } else if (ret == EAGAIN) {
2470 mlog(0, "%s: trying to start recovery of node "
2471 "%u, but node %u is waiting for last recovery "
2472 "to complete, backoff for a bit\n", dlm->name,
2473 dead_node, nodenum);
2474 /* TODO Look into replacing msleep with cond_resched() */
2475 msleep(100);
2476 goto retry;
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002477 }
2478 }
2479
2480 return ret;
2481}
2482
2483int dlm_begin_reco_handler(struct o2net_msg *msg, u32 len, void *data)
2484{
2485 struct dlm_ctxt *dlm = data;
2486 struct dlm_begin_reco *br = (struct dlm_begin_reco *)msg->buf;
2487
2488 /* ok to return 0, domain has gone away */
2489 if (!dlm_grab(dlm))
2490 return 0;
2491
Kurt Hackel466d1a42006-05-01 11:11:13 -07002492 spin_lock(&dlm->spinlock);
2493 if (dlm->reco.state & DLM_RECO_STATE_FINALIZE) {
2494 mlog(0, "%s: node %u wants to recover node %u (%u:%u) "
2495 "but this node is in finalize state, waiting on finalize2\n",
2496 dlm->name, br->node_idx, br->dead_node,
2497 dlm->reco.dead_node, dlm->reco.new_master);
2498 spin_unlock(&dlm->spinlock);
2499 return EAGAIN;
2500 }
2501 spin_unlock(&dlm->spinlock);
2502
Kurt Hackeld6dea6e2006-04-27 18:08:51 -07002503 mlog(0, "%s: node %u wants to recover node %u (%u:%u)\n",
2504 dlm->name, br->node_idx, br->dead_node,
2505 dlm->reco.dead_node, dlm->reco.new_master);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002506
2507 dlm_fire_domain_eviction_callbacks(dlm, br->dead_node);
2508
2509 spin_lock(&dlm->spinlock);
2510 if (dlm->reco.new_master != O2NM_INVALID_NODE_NUM) {
Kurt Hackele2faea42006-01-12 14:24:55 -08002511 if (test_bit(dlm->reco.new_master, dlm->recovery_map)) {
2512 mlog(0, "%s: new_master %u died, changing "
2513 "to %u\n", dlm->name, dlm->reco.new_master,
2514 br->node_idx);
2515 } else {
2516 mlog(0, "%s: new_master %u NOT DEAD, changing "
2517 "to %u\n", dlm->name, dlm->reco.new_master,
2518 br->node_idx);
2519 /* may not have seen the new master as dead yet */
2520 }
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002521 }
2522 if (dlm->reco.dead_node != O2NM_INVALID_NODE_NUM) {
Kurt Hackele2faea42006-01-12 14:24:55 -08002523 mlog(ML_NOTICE, "%s: dead_node previously set to %u, "
2524 "node %u changing it to %u\n", dlm->name,
2525 dlm->reco.dead_node, br->node_idx, br->dead_node);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002526 }
Kurt Hackelab27eb62006-04-27 18:03:49 -07002527 dlm_set_reco_master(dlm, br->node_idx);
2528 dlm_set_reco_dead_node(dlm, br->dead_node);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002529 if (!test_bit(br->dead_node, dlm->recovery_map)) {
Kurt Hackele2faea42006-01-12 14:24:55 -08002530 mlog(0, "recovery master %u sees %u as dead, but this "
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002531 "node has not yet. marking %u as dead\n",
2532 br->node_idx, br->dead_node, br->dead_node);
Kurt Hackele2faea42006-01-12 14:24:55 -08002533 if (!test_bit(br->dead_node, dlm->domain_map) ||
2534 !test_bit(br->dead_node, dlm->live_nodes_map))
2535 mlog(0, "%u not in domain/live_nodes map "
2536 "so setting it in reco map manually\n",
2537 br->dead_node);
Kurt Hackelc03872f2006-03-06 14:08:49 -08002538 /* force the recovery cleanup in __dlm_hb_node_down
2539 * both of these will be cleared in a moment */
2540 set_bit(br->dead_node, dlm->domain_map);
2541 set_bit(br->dead_node, dlm->live_nodes_map);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002542 __dlm_hb_node_down(dlm, br->dead_node);
2543 }
2544 spin_unlock(&dlm->spinlock);
2545
2546 dlm_kick_recovery_thread(dlm);
Kurt Hackeld6dea6e2006-04-27 18:08:51 -07002547
2548 mlog(0, "%s: recovery started by node %u, for %u (%u:%u)\n",
2549 dlm->name, br->node_idx, br->dead_node,
2550 dlm->reco.dead_node, dlm->reco.new_master);
2551
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002552 dlm_put(dlm);
2553 return 0;
2554}
2555
Kurt Hackel466d1a42006-05-01 11:11:13 -07002556#define DLM_FINALIZE_STAGE2 0x01
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002557static int dlm_send_finalize_reco_message(struct dlm_ctxt *dlm)
2558{
2559 int ret = 0;
2560 struct dlm_finalize_reco fr;
2561 struct dlm_node_iter iter;
2562 int nodenum;
2563 int status;
Kurt Hackel466d1a42006-05-01 11:11:13 -07002564 int stage = 1;
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002565
Kurt Hackel466d1a42006-05-01 11:11:13 -07002566 mlog(0, "finishing recovery for node %s:%u, "
2567 "stage %d\n", dlm->name, dlm->reco.dead_node, stage);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002568
2569 spin_lock(&dlm->spinlock);
2570 dlm_node_iter_init(dlm->domain_map, &iter);
2571 spin_unlock(&dlm->spinlock);
2572
Kurt Hackel466d1a42006-05-01 11:11:13 -07002573stage2:
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002574 memset(&fr, 0, sizeof(fr));
2575 fr.node_idx = dlm->node_num;
2576 fr.dead_node = dlm->reco.dead_node;
Kurt Hackel466d1a42006-05-01 11:11:13 -07002577 if (stage == 2)
2578 fr.flags |= DLM_FINALIZE_STAGE2;
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002579
2580 while ((nodenum = dlm_node_iter_next(&iter)) >= 0) {
2581 if (nodenum == dlm->node_num)
2582 continue;
2583 ret = o2net_send_message(DLM_FINALIZE_RECO_MSG, dlm->key,
2584 &fr, sizeof(fr), nodenum, &status);
Kurt Hackel466d1a42006-05-01 11:11:13 -07002585 if (ret >= 0)
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002586 ret = status;
Kurt Hackel466d1a42006-05-01 11:11:13 -07002587 if (ret < 0) {
2588 mlog_errno(ret);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002589 if (dlm_is_host_down(ret)) {
2590 /* this has no effect on this recovery
2591 * session, so set the status to zero to
2592 * finish out the last recovery */
2593 mlog(ML_ERROR, "node %u went down after this "
2594 "node finished recovery.\n", nodenum);
2595 ret = 0;
Kurt Hackelc27069e2006-05-01 13:51:49 -07002596 continue;
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002597 }
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002598 break;
2599 }
2600 }
Kurt Hackel466d1a42006-05-01 11:11:13 -07002601 if (stage == 1) {
2602 /* reset the node_iter back to the top and send finalize2 */
2603 iter.curnode = -1;
2604 stage = 2;
2605 goto stage2;
2606 }
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002607
2608 return ret;
2609}
2610
2611int dlm_finalize_reco_handler(struct o2net_msg *msg, u32 len, void *data)
2612{
2613 struct dlm_ctxt *dlm = data;
2614 struct dlm_finalize_reco *fr = (struct dlm_finalize_reco *)msg->buf;
Kurt Hackel466d1a42006-05-01 11:11:13 -07002615 int stage = 1;
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002616
2617 /* ok to return 0, domain has gone away */
2618 if (!dlm_grab(dlm))
2619 return 0;
2620
Kurt Hackel466d1a42006-05-01 11:11:13 -07002621 if (fr->flags & DLM_FINALIZE_STAGE2)
2622 stage = 2;
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002623
Kurt Hackel466d1a42006-05-01 11:11:13 -07002624 mlog(0, "%s: node %u finalizing recovery stage%d of "
2625 "node %u (%u:%u)\n", dlm->name, fr->node_idx, stage,
2626 fr->dead_node, dlm->reco.dead_node, dlm->reco.new_master);
2627
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002628 spin_lock(&dlm->spinlock);
2629
2630 if (dlm->reco.new_master != fr->node_idx) {
2631 mlog(ML_ERROR, "node %u sent recovery finalize msg, but node "
2632 "%u is supposed to be the new master, dead=%u\n",
2633 fr->node_idx, dlm->reco.new_master, fr->dead_node);
2634 BUG();
2635 }
2636 if (dlm->reco.dead_node != fr->dead_node) {
2637 mlog(ML_ERROR, "node %u sent recovery finalize msg for dead "
2638 "node %u, but node %u is supposed to be dead\n",
2639 fr->node_idx, fr->dead_node, dlm->reco.dead_node);
2640 BUG();
2641 }
2642
Kurt Hackel466d1a42006-05-01 11:11:13 -07002643 switch (stage) {
2644 case 1:
2645 dlm_finish_local_lockres_recovery(dlm, fr->dead_node, fr->node_idx);
2646 if (dlm->reco.state & DLM_RECO_STATE_FINALIZE) {
2647 mlog(ML_ERROR, "%s: received finalize1 from "
2648 "new master %u for dead node %u, but "
2649 "this node has already received it!\n",
2650 dlm->name, fr->node_idx, fr->dead_node);
2651 dlm_print_reco_node_status(dlm);
2652 BUG();
2653 }
2654 dlm->reco.state |= DLM_RECO_STATE_FINALIZE;
2655 spin_unlock(&dlm->spinlock);
2656 break;
2657 case 2:
2658 if (!(dlm->reco.state & DLM_RECO_STATE_FINALIZE)) {
2659 mlog(ML_ERROR, "%s: received finalize2 from "
2660 "new master %u for dead node %u, but "
2661 "this node did not have finalize1!\n",
2662 dlm->name, fr->node_idx, fr->dead_node);
2663 dlm_print_reco_node_status(dlm);
2664 BUG();
2665 }
2666 dlm->reco.state &= ~DLM_RECO_STATE_FINALIZE;
2667 spin_unlock(&dlm->spinlock);
2668 dlm_reset_recovery(dlm);
2669 dlm_kick_recovery_thread(dlm);
2670 break;
2671 default:
2672 BUG();
2673 }
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002674
Kurt Hackeld6dea6e2006-04-27 18:08:51 -07002675 mlog(0, "%s: recovery done, reco master was %u, dead now %u, master now %u\n",
2676 dlm->name, fr->node_idx, dlm->reco.dead_node, dlm->reco.new_master);
2677
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002678 dlm_put(dlm);
2679 return 0;
2680}