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