1 /* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
8 * Copyright (C) 2004 Oracle. All rights reserved.
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.
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.
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.
28 #include <linux/module.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>
42 #include <linux/delay.h>
45 #include "cluster/heartbeat.h"
46 #include "cluster/nodemanager.h"
47 #include "cluster/tcp.h"
50 #include "dlmcommon.h"
51 #include "dlmdomain.h"
53 #define MLOG_MASK_PREFIX (ML_DLM|ML_DLM_RECOVERY)
54 #include "cluster/masklog.h"
56 static void dlm_do_local_recovery_cleanup(struct dlm_ctxt *dlm, u8 dead_node);
58 static int dlm_recovery_thread(void *data);
59 void dlm_complete_recovery_thread(struct dlm_ctxt *dlm);
60 int dlm_launch_recovery_thread(struct dlm_ctxt *dlm);
61 void dlm_kick_recovery_thread(struct dlm_ctxt *dlm);
62 static int dlm_do_recovery(struct dlm_ctxt *dlm);
64 static int dlm_pick_recovery_master(struct dlm_ctxt *dlm);
65 static int dlm_remaster_locks(struct dlm_ctxt *dlm, u8 dead_node);
66 static int dlm_init_recovery_area(struct dlm_ctxt *dlm, u8 dead_node);
67 static int dlm_request_all_locks(struct dlm_ctxt *dlm,
68 u8 request_from, u8 dead_node);
69 static void dlm_destroy_recovery_area(struct dlm_ctxt *dlm, u8 dead_node);
71 static inline int dlm_num_locks_in_lockres(struct dlm_lock_resource *res);
72 static void dlm_init_migratable_lockres(struct dlm_migratable_lockres *mres,
73 const char *lockname, int namelen,
74 int total_locks, u64 cookie,
76 static int dlm_send_mig_lockres_msg(struct dlm_ctxt *dlm,
77 struct dlm_migratable_lockres *mres,
79 struct dlm_lock_resource *res,
81 static int dlm_process_recovery_data(struct dlm_ctxt *dlm,
82 struct dlm_lock_resource *res,
83 struct dlm_migratable_lockres *mres);
84 static int dlm_send_finalize_reco_message(struct dlm_ctxt *dlm);
85 static int dlm_send_all_done_msg(struct dlm_ctxt *dlm,
86 u8 dead_node, u8 send_to);
87 static int dlm_send_begin_reco_message(struct dlm_ctxt *dlm, u8 dead_node);
88 static void dlm_move_reco_locks_to_list(struct dlm_ctxt *dlm,
89 struct list_head *list, u8 dead_node);
90 static void dlm_finish_local_lockres_recovery(struct dlm_ctxt *dlm,
91 u8 dead_node, u8 new_master);
92 static void dlm_reco_ast(void *astdata);
93 static void dlm_reco_bast(void *astdata, int blocked_type);
94 static void dlm_reco_unlock_ast(void *astdata, enum dlm_status st);
95 static void dlm_request_all_locks_worker(struct dlm_work_item *item,
97 static void dlm_mig_lockres_worker(struct dlm_work_item *item, void *data);
99 static u64 dlm_get_next_mig_cookie(void);
101 static DEFINE_SPINLOCK(dlm_reco_state_lock);
102 static DEFINE_SPINLOCK(dlm_mig_cookie_lock);
103 static u64 dlm_mig_cookie = 1;
105 static u64 dlm_get_next_mig_cookie(void)
108 spin_lock(&dlm_mig_cookie_lock);
110 if (dlm_mig_cookie == (~0ULL))
114 spin_unlock(&dlm_mig_cookie_lock);
118 static inline void dlm_set_reco_dead_node(struct dlm_ctxt *dlm,
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;
128 static inline void dlm_set_reco_master(struct dlm_ctxt *dlm,
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;
137 static inline void __dlm_reset_recovery(struct dlm_ctxt *dlm)
139 assert_spin_locked(&dlm->spinlock);
140 clear_bit(dlm->reco.dead_node, dlm->recovery_map);
141 dlm_set_reco_dead_node(dlm, O2NM_INVALID_NODE_NUM);
142 dlm_set_reco_master(dlm, O2NM_INVALID_NODE_NUM);
145 static inline void dlm_reset_recovery(struct dlm_ctxt *dlm)
147 spin_lock(&dlm->spinlock);
148 __dlm_reset_recovery(dlm);
149 spin_unlock(&dlm->spinlock);
152 /* Worker function used during recovery. */
153 void dlm_dispatch_work(void *data)
155 struct dlm_ctxt *dlm = (struct dlm_ctxt *)data;
157 struct list_head *iter, *iter2;
158 struct dlm_work_item *item;
159 dlm_workfunc_t *workfunc;
162 if (!dlm_joined(dlm))
165 spin_lock(&dlm->work_lock);
166 list_splice_init(&dlm->work_list, &tmp_list);
167 spin_unlock(&dlm->work_lock);
169 list_for_each_safe(iter, iter2, &tmp_list) {
172 mlog(0, "%s: work thread has %d work items\n", dlm->name, tot);
174 list_for_each_safe(iter, iter2, &tmp_list) {
175 item = list_entry(iter, struct dlm_work_item, list);
176 workfunc = item->func;
177 list_del_init(&item->list);
179 /* already have ref on dlm to avoid having
180 * it disappear. just double-check. */
181 BUG_ON(item->dlm != dlm);
183 /* this is allowed to sleep and
184 * call network stuff */
185 workfunc(item, item->data);
196 void dlm_kick_recovery_thread(struct dlm_ctxt *dlm)
198 /* wake the recovery thread
199 * this will wake the reco thread in one of three places
200 * 1) sleeping with no recovery happening
201 * 2) sleeping with recovery mastered elsewhere
202 * 3) recovery mastered here, waiting on reco data */
204 wake_up(&dlm->dlm_reco_thread_wq);
207 /* Launch the recovery thread */
208 int dlm_launch_recovery_thread(struct dlm_ctxt *dlm)
210 mlog(0, "starting dlm recovery thread...\n");
212 dlm->dlm_reco_thread_task = kthread_run(dlm_recovery_thread, dlm,
214 if (IS_ERR(dlm->dlm_reco_thread_task)) {
215 mlog_errno(PTR_ERR(dlm->dlm_reco_thread_task));
216 dlm->dlm_reco_thread_task = NULL;
223 void dlm_complete_recovery_thread(struct dlm_ctxt *dlm)
225 if (dlm->dlm_reco_thread_task) {
226 mlog(0, "waiting for dlm recovery thread to exit\n");
227 kthread_stop(dlm->dlm_reco_thread_task);
228 dlm->dlm_reco_thread_task = NULL;
235 * this is lame, but here's how recovery works...
236 * 1) all recovery threads cluster wide will work on recovering
238 * 2) negotiate who will take over all the locks for the dead node.
239 * thats right... ALL the locks.
240 * 3) once a new master is chosen, everyone scans all locks
241 * and moves aside those mastered by the dead guy
242 * 4) each of these locks should be locked until recovery is done
243 * 5) the new master collects up all of secondary lock queue info
244 * one lock at a time, forcing each node to communicate back
246 * 6) each secondary lock queue responds with the full known lock info
247 * 7) once the new master has run all its locks, it sends a ALLDONE!
248 * message to everyone
249 * 8) upon receiving this message, the secondary queue node unlocks
250 * and responds to the ALLDONE
251 * 9) once the new master gets responses from everyone, he unlocks
252 * everything and recovery for this dead node is done
253 *10) go back to 2) while there are still dead nodes
257 static void dlm_print_reco_node_status(struct dlm_ctxt *dlm)
259 struct dlm_reco_node_data *ndata;
260 struct dlm_lock_resource *res;
262 mlog(ML_NOTICE, "%s(%d): recovery info, state=%s, dead=%u, master=%u\n",
263 dlm->name, dlm->dlm_reco_thread_task->pid,
264 dlm->reco.state & DLM_RECO_STATE_ACTIVE ? "ACTIVE" : "inactive",
265 dlm->reco.dead_node, dlm->reco.new_master);
267 list_for_each_entry(ndata, &dlm->reco.node_data, list) {
268 char *st = "unknown";
269 switch (ndata->state) {
270 case DLM_RECO_NODE_DATA_INIT:
273 case DLM_RECO_NODE_DATA_REQUESTING:
276 case DLM_RECO_NODE_DATA_DEAD:
279 case DLM_RECO_NODE_DATA_RECEIVING:
282 case DLM_RECO_NODE_DATA_REQUESTED:
285 case DLM_RECO_NODE_DATA_DONE:
288 case DLM_RECO_NODE_DATA_FINALIZE_SENT:
289 st = "finalize-sent";
295 mlog(ML_NOTICE, "%s: reco state, node %u, state=%s\n",
296 dlm->name, ndata->node_num, st);
298 list_for_each_entry(res, &dlm->reco.resources, recovering) {
299 mlog(ML_NOTICE, "%s: lockres %.*s on recovering list\n",
300 dlm->name, res->lockname.len, res->lockname.name);
304 #define DLM_RECO_THREAD_TIMEOUT_MS (5 * 1000)
306 static int dlm_recovery_thread(void *data)
309 struct dlm_ctxt *dlm = data;
310 unsigned long timeout = msecs_to_jiffies(DLM_RECO_THREAD_TIMEOUT_MS);
312 mlog(0, "dlm thread running for %s...\n", dlm->name);
314 while (!kthread_should_stop()) {
315 if (dlm_joined(dlm)) {
316 status = dlm_do_recovery(dlm);
317 if (status == -EAGAIN) {
318 /* do not sleep, recheck immediately. */
325 wait_event_interruptible_timeout(dlm->dlm_reco_thread_wq,
326 kthread_should_stop(),
330 mlog(0, "quitting DLM recovery thread\n");
334 /* returns true when the recovery master has contacted us */
335 static int dlm_reco_master_ready(struct dlm_ctxt *dlm)
338 spin_lock(&dlm->spinlock);
339 ready = (dlm->reco.new_master != O2NM_INVALID_NODE_NUM);
340 spin_unlock(&dlm->spinlock);
344 /* returns true if node is no longer in the domain
345 * could be dead or just not joined */
346 int dlm_is_node_dead(struct dlm_ctxt *dlm, u8 node)
349 spin_lock(&dlm->spinlock);
350 dead = !test_bit(node, dlm->domain_map);
351 spin_unlock(&dlm->spinlock);
355 /* returns true if node is no longer in the domain
356 * could be dead or just not joined */
357 static int dlm_is_node_recovered(struct dlm_ctxt *dlm, u8 node)
360 spin_lock(&dlm->spinlock);
361 recovered = !test_bit(node, dlm->recovery_map);
362 spin_unlock(&dlm->spinlock);
367 int dlm_wait_for_node_death(struct dlm_ctxt *dlm, u8 node, int timeout)
370 mlog(ML_NOTICE, "%s: waiting %dms for notification of "
371 "death of node %u\n", dlm->name, timeout, node);
372 wait_event_timeout(dlm->dlm_reco_thread_wq,
373 dlm_is_node_dead(dlm, node),
374 msecs_to_jiffies(timeout));
376 mlog(ML_NOTICE, "%s: waiting indefinitely for notification "
377 "of death of node %u\n", dlm->name, node);
378 wait_event(dlm->dlm_reco_thread_wq,
379 dlm_is_node_dead(dlm, node));
381 /* for now, return 0 */
385 int dlm_wait_for_node_recovery(struct dlm_ctxt *dlm, u8 node, int timeout)
388 mlog(0, "%s: waiting %dms for notification of "
389 "recovery of node %u\n", dlm->name, timeout, node);
390 wait_event_timeout(dlm->dlm_reco_thread_wq,
391 dlm_is_node_recovered(dlm, node),
392 msecs_to_jiffies(timeout));
394 mlog(0, "%s: waiting indefinitely for notification "
395 "of recovery of node %u\n", dlm->name, node);
396 wait_event(dlm->dlm_reco_thread_wq,
397 dlm_is_node_recovered(dlm, node));
399 /* for now, return 0 */
403 /* callers of the top-level api calls (dlmlock/dlmunlock) should
404 * block on the dlm->reco.event when recovery is in progress.
405 * the dlm recovery thread will set this state when it begins
406 * recovering a dead node (as the new master or not) and clear
407 * the state and wake as soon as all affected lock resources have
408 * been marked with the RECOVERY flag */
409 static int dlm_in_recovery(struct dlm_ctxt *dlm)
412 spin_lock(&dlm->spinlock);
413 in_recovery = !!(dlm->reco.state & DLM_RECO_STATE_ACTIVE);
414 spin_unlock(&dlm->spinlock);
419 void dlm_wait_for_recovery(struct dlm_ctxt *dlm)
421 if (dlm_in_recovery(dlm)) {
422 mlog(0, "%s: reco thread %d in recovery: "
423 "state=%d, master=%u, dead=%u\n",
424 dlm->name, dlm->dlm_reco_thread_task->pid,
425 dlm->reco.state, dlm->reco.new_master,
426 dlm->reco.dead_node);
428 wait_event(dlm->reco.event, !dlm_in_recovery(dlm));
431 static void dlm_begin_recovery(struct dlm_ctxt *dlm)
433 spin_lock(&dlm->spinlock);
434 BUG_ON(dlm->reco.state & DLM_RECO_STATE_ACTIVE);
435 dlm->reco.state |= DLM_RECO_STATE_ACTIVE;
436 spin_unlock(&dlm->spinlock);
439 static void dlm_end_recovery(struct dlm_ctxt *dlm)
441 spin_lock(&dlm->spinlock);
442 BUG_ON(!(dlm->reco.state & DLM_RECO_STATE_ACTIVE));
443 dlm->reco.state &= ~DLM_RECO_STATE_ACTIVE;
444 spin_unlock(&dlm->spinlock);
445 wake_up(&dlm->reco.event);
448 static int dlm_do_recovery(struct dlm_ctxt *dlm)
453 spin_lock(&dlm->spinlock);
455 /* check to see if the new master has died */
456 if (dlm->reco.new_master != O2NM_INVALID_NODE_NUM &&
457 test_bit(dlm->reco.new_master, dlm->recovery_map)) {
458 mlog(0, "new master %u died while recovering %u!\n",
459 dlm->reco.new_master, dlm->reco.dead_node);
460 /* unset the new_master, leave dead_node */
461 dlm_set_reco_master(dlm, O2NM_INVALID_NODE_NUM);
464 /* select a target to recover */
465 if (dlm->reco.dead_node == O2NM_INVALID_NODE_NUM) {
468 bit = find_next_bit (dlm->recovery_map, O2NM_MAX_NODES+1, 0);
469 if (bit >= O2NM_MAX_NODES || bit < 0)
470 dlm_set_reco_dead_node(dlm, O2NM_INVALID_NODE_NUM);
472 dlm_set_reco_dead_node(dlm, bit);
473 } else if (!test_bit(dlm->reco.dead_node, dlm->recovery_map)) {
475 mlog(ML_ERROR, "dead_node %u no longer in recovery map!\n",
476 dlm->reco.dead_node);
477 dlm_set_reco_dead_node(dlm, O2NM_INVALID_NODE_NUM);
480 if (dlm->reco.dead_node == O2NM_INVALID_NODE_NUM) {
481 // mlog(0, "nothing to recover! sleeping now!\n");
482 spin_unlock(&dlm->spinlock);
483 /* return to main thread loop and sleep. */
486 mlog(0, "%s(%d):recovery thread found node %u in the recovery map!\n",
487 dlm->name, dlm->dlm_reco_thread_task->pid,
488 dlm->reco.dead_node);
489 spin_unlock(&dlm->spinlock);
491 /* take write barrier */
492 /* (stops the list reshuffling thread, proxy ast handling) */
493 dlm_begin_recovery(dlm);
495 if (dlm->reco.new_master == dlm->node_num)
498 if (dlm->reco.new_master == O2NM_INVALID_NODE_NUM) {
499 /* choose a new master, returns 0 if this node
500 * is the master, -EEXIST if it's another node.
501 * this does not return until a new master is chosen
502 * or recovery completes entirely. */
503 ret = dlm_pick_recovery_master(dlm);
505 /* already notified everyone. go. */
508 mlog(0, "another node will master this recovery session.\n");
510 mlog(0, "dlm=%s (%d), new_master=%u, this node=%u, dead_node=%u\n",
511 dlm->name, dlm->dlm_reco_thread_task->pid, dlm->reco.new_master,
512 dlm->node_num, dlm->reco.dead_node);
514 /* it is safe to start everything back up here
515 * because all of the dead node's lock resources
516 * have been marked as in-recovery */
517 dlm_end_recovery(dlm);
519 /* sleep out in main dlm_recovery_thread loop. */
523 mlog(0, "(%d) mastering recovery of %s:%u here(this=%u)!\n",
524 dlm->dlm_reco_thread_task->pid,
525 dlm->name, dlm->reco.dead_node, dlm->node_num);
527 status = dlm_remaster_locks(dlm, dlm->reco.dead_node);
529 /* we should never hit this anymore */
530 mlog(ML_ERROR, "error %d remastering locks for node %u, "
531 "retrying.\n", status, dlm->reco.dead_node);
532 /* yield a bit to allow any final network messages
533 * to get handled on remaining nodes */
536 /* success! see if any other nodes need recovery */
537 mlog(0, "DONE mastering recovery of %s:%u here(this=%u)!\n",
538 dlm->name, dlm->reco.dead_node, dlm->node_num);
539 dlm_reset_recovery(dlm);
541 dlm_end_recovery(dlm);
543 /* continue and look for another dead node */
547 static int dlm_remaster_locks(struct dlm_ctxt *dlm, u8 dead_node)
550 struct dlm_reco_node_data *ndata;
551 struct list_head *iter;
557 /* we have become recovery master. there is no escaping
558 * this, so just keep trying until we get it. */
559 status = dlm_init_recovery_area(dlm, dead_node);
561 mlog(ML_ERROR, "%s: failed to alloc recovery area, "
562 "retrying\n", dlm->name);
565 } while (status != 0);
567 /* safe to access the node data list without a lock, since this
568 * process is the only one to change the list */
569 list_for_each(iter, &dlm->reco.node_data) {
570 ndata = list_entry (iter, struct dlm_reco_node_data, list);
571 BUG_ON(ndata->state != DLM_RECO_NODE_DATA_INIT);
572 ndata->state = DLM_RECO_NODE_DATA_REQUESTING;
574 mlog(0, "requesting lock info from node %u\n",
577 if (ndata->node_num == dlm->node_num) {
578 ndata->state = DLM_RECO_NODE_DATA_DONE;
583 status = dlm_request_all_locks(dlm, ndata->node_num,
587 if (dlm_is_host_down(status)) {
588 /* node died, ignore it for recovery */
590 ndata->state = DLM_RECO_NODE_DATA_DEAD;
591 /* wait for the domain map to catch up
592 * with the network state. */
593 wait_event_timeout(dlm->dlm_reco_thread_wq,
594 dlm_is_node_dead(dlm,
596 msecs_to_jiffies(1000));
597 mlog(0, "waited 1 sec for %u, "
598 "dead? %s\n", ndata->node_num,
599 dlm_is_node_dead(dlm, ndata->node_num) ?
602 /* -ENOMEM on the other node */
603 mlog(0, "%s: node %u returned "
604 "%d during recovery, retrying "
605 "after a short wait\n",
606 dlm->name, ndata->node_num,
611 } while (status != 0);
613 switch (ndata->state) {
614 case DLM_RECO_NODE_DATA_INIT:
615 case DLM_RECO_NODE_DATA_FINALIZE_SENT:
616 case DLM_RECO_NODE_DATA_REQUESTED:
619 case DLM_RECO_NODE_DATA_DEAD:
620 mlog(0, "node %u died after requesting "
621 "recovery info for node %u\n",
622 ndata->node_num, dead_node);
623 /* fine. don't need this node's info.
624 * continue without it. */
626 case DLM_RECO_NODE_DATA_REQUESTING:
627 ndata->state = DLM_RECO_NODE_DATA_REQUESTED;
628 mlog(0, "now receiving recovery data from "
629 "node %u for dead node %u\n",
630 ndata->node_num, dead_node);
632 case DLM_RECO_NODE_DATA_RECEIVING:
633 mlog(0, "already receiving recovery data from "
634 "node %u for dead node %u\n",
635 ndata->node_num, dead_node);
637 case DLM_RECO_NODE_DATA_DONE:
638 mlog(0, "already DONE receiving recovery data "
639 "from node %u for dead node %u\n",
640 ndata->node_num, dead_node);
645 mlog(0, "done requesting all lock info\n");
647 /* nodes should be sending reco data now
648 * just need to wait */
651 /* check all the nodes now to see if we are
652 * done, or if anyone died */
654 spin_lock(&dlm_reco_state_lock);
655 list_for_each(iter, &dlm->reco.node_data) {
656 ndata = list_entry (iter, struct dlm_reco_node_data, list);
658 mlog(0, "checking recovery state of node %u\n",
660 switch (ndata->state) {
661 case DLM_RECO_NODE_DATA_INIT:
662 case DLM_RECO_NODE_DATA_REQUESTING:
663 mlog(ML_ERROR, "bad ndata state for "
664 "node %u: state=%d\n",
665 ndata->node_num, ndata->state);
668 case DLM_RECO_NODE_DATA_DEAD:
669 mlog(0, "node %u died after "
670 "requesting recovery info for "
671 "node %u\n", ndata->node_num,
674 case DLM_RECO_NODE_DATA_RECEIVING:
675 case DLM_RECO_NODE_DATA_REQUESTED:
676 mlog(0, "%s: node %u still in state %s\n",
677 dlm->name, ndata->node_num,
678 ndata->state==DLM_RECO_NODE_DATA_RECEIVING ?
679 "receiving" : "requested");
682 case DLM_RECO_NODE_DATA_DONE:
683 mlog(0, "%s: node %u state is done\n",
684 dlm->name, ndata->node_num);
686 case DLM_RECO_NODE_DATA_FINALIZE_SENT:
687 mlog(0, "%s: node %u state is finalize\n",
688 dlm->name, ndata->node_num);
692 spin_unlock(&dlm_reco_state_lock);
694 mlog(0, "pass #%d, all_nodes_done?: %s\n", ++pass,
695 all_nodes_done?"yes":"no");
696 if (all_nodes_done) {
699 /* all nodes are now in DLM_RECO_NODE_DATA_DONE state
700 * just send a finalize message to everyone and
702 mlog(0, "all nodes are done! send finalize\n");
703 ret = dlm_send_finalize_reco_message(dlm);
707 spin_lock(&dlm->spinlock);
708 dlm_finish_local_lockres_recovery(dlm, dead_node,
710 spin_unlock(&dlm->spinlock);
711 mlog(0, "should be done with recovery!\n");
713 mlog(0, "finishing recovery of %s at %lu, "
714 "dead=%u, this=%u, new=%u\n", dlm->name,
715 jiffies, dlm->reco.dead_node,
716 dlm->node_num, dlm->reco.new_master);
719 /* rescan everything marked dirty along the way */
720 dlm_kick_thread(dlm, NULL);
723 /* wait to be signalled, with periodic timeout
724 * to check for node death */
725 wait_event_interruptible_timeout(dlm->dlm_reco_thread_wq,
726 kthread_should_stop(),
727 msecs_to_jiffies(DLM_RECO_THREAD_TIMEOUT_MS));
732 dlm_destroy_recovery_area(dlm, dead_node);
738 static int dlm_init_recovery_area(struct dlm_ctxt *dlm, u8 dead_node)
741 struct dlm_reco_node_data *ndata;
743 spin_lock(&dlm->spinlock);
744 memcpy(dlm->reco.node_map, dlm->domain_map, sizeof(dlm->domain_map));
745 /* nodes can only be removed (by dying) after dropping
746 * this lock, and death will be trapped later, so this should do */
747 spin_unlock(&dlm->spinlock);
750 num = find_next_bit (dlm->reco.node_map, O2NM_MAX_NODES, num);
751 if (num >= O2NM_MAX_NODES) {
754 BUG_ON(num == dead_node);
756 ndata = kcalloc(1, sizeof(*ndata), GFP_NOFS);
758 dlm_destroy_recovery_area(dlm, dead_node);
761 ndata->node_num = num;
762 ndata->state = DLM_RECO_NODE_DATA_INIT;
763 spin_lock(&dlm_reco_state_lock);
764 list_add_tail(&ndata->list, &dlm->reco.node_data);
765 spin_unlock(&dlm_reco_state_lock);
772 static void dlm_destroy_recovery_area(struct dlm_ctxt *dlm, u8 dead_node)
774 struct list_head *iter, *iter2;
775 struct dlm_reco_node_data *ndata;
778 spin_lock(&dlm_reco_state_lock);
779 list_splice_init(&dlm->reco.node_data, &tmplist);
780 spin_unlock(&dlm_reco_state_lock);
782 list_for_each_safe(iter, iter2, &tmplist) {
783 ndata = list_entry (iter, struct dlm_reco_node_data, list);
784 list_del_init(&ndata->list);
789 static int dlm_request_all_locks(struct dlm_ctxt *dlm, u8 request_from,
792 struct dlm_lock_request lr;
798 mlog(0, "dlm_request_all_locks: dead node is %u, sending request "
799 "to %u\n", dead_node, request_from);
801 memset(&lr, 0, sizeof(lr));
802 lr.node_idx = dlm->node_num;
803 lr.dead_node = dead_node;
807 ret = o2net_send_message(DLM_LOCK_REQUEST_MSG, dlm->key,
808 &lr, sizeof(lr), request_from, NULL);
810 /* negative status is handled by caller */
814 // return from here, then
815 // sleep until all received or error
820 int dlm_request_all_locks_handler(struct o2net_msg *msg, u32 len, void *data)
822 struct dlm_ctxt *dlm = data;
823 struct dlm_lock_request *lr = (struct dlm_lock_request *)msg->buf;
825 struct dlm_work_item *item = NULL;
830 if (lr->dead_node != dlm->reco.dead_node) {
831 mlog(ML_ERROR, "%s: node %u sent dead_node=%u, but local "
832 "dead_node is %u\n", dlm->name, lr->node_idx,
833 lr->dead_node, dlm->reco.dead_node);
834 dlm_print_reco_node_status(dlm);
839 BUG_ON(lr->dead_node != dlm->reco.dead_node);
841 item = kcalloc(1, sizeof(*item), GFP_NOFS);
847 /* this will get freed by dlm_request_all_locks_worker */
848 buf = (char *) __get_free_page(GFP_NOFS);
855 /* queue up work for dlm_request_all_locks_worker */
856 dlm_grab(dlm); /* get an extra ref for the work item */
857 dlm_init_work_item(dlm, item, dlm_request_all_locks_worker, buf);
858 item->u.ral.reco_master = lr->node_idx;
859 item->u.ral.dead_node = lr->dead_node;
860 spin_lock(&dlm->work_lock);
861 list_add_tail(&item->list, &dlm->work_list);
862 spin_unlock(&dlm->work_lock);
863 queue_work(dlm->dlm_worker, &dlm->dispatched_work);
869 static void dlm_request_all_locks_worker(struct dlm_work_item *item, void *data)
871 struct dlm_migratable_lockres *mres;
872 struct dlm_lock_resource *res;
873 struct dlm_ctxt *dlm;
874 LIST_HEAD(resources);
875 struct list_head *iter;
877 u8 dead_node, reco_master;
878 int skip_all_done = 0;
881 dead_node = item->u.ral.dead_node;
882 reco_master = item->u.ral.reco_master;
883 mres = (struct dlm_migratable_lockres *)data;
885 mlog(0, "%s: recovery worker started, dead=%u, master=%u\n",
886 dlm->name, dead_node, reco_master);
888 if (dead_node != dlm->reco.dead_node ||
889 reco_master != dlm->reco.new_master) {
890 /* worker could have been created before the recovery master
891 * died. if so, do not continue, but do not error. */
892 if (dlm->reco.new_master == O2NM_INVALID_NODE_NUM) {
893 mlog(ML_NOTICE, "%s: will not send recovery state, "
894 "recovery master %u died, thread=(dead=%u,mas=%u)"
895 " current=(dead=%u,mas=%u)\n", dlm->name,
896 reco_master, dead_node, reco_master,
897 dlm->reco.dead_node, dlm->reco.new_master);
899 mlog(ML_NOTICE, "%s: reco state invalid: reco(dead=%u, "
900 "master=%u), request(dead=%u, master=%u)\n",
901 dlm->name, dlm->reco.dead_node,
902 dlm->reco.new_master, dead_node, reco_master);
907 /* lock resources should have already been moved to the
908 * dlm->reco.resources list. now move items from that list
909 * to a temp list if the dead owner matches. note that the
910 * whole cluster recovers only one node at a time, so we
911 * can safely move UNKNOWN lock resources for each recovery
913 dlm_move_reco_locks_to_list(dlm, &resources, dead_node);
915 /* now we can begin blasting lockreses without the dlm lock */
917 /* any errors returned will be due to the new_master dying,
918 * the dlm_reco_thread should detect this */
919 list_for_each(iter, &resources) {
920 res = list_entry (iter, struct dlm_lock_resource, recovering);
921 ret = dlm_send_one_lockres(dlm, res, mres, reco_master,
924 mlog(ML_ERROR, "%s: node %u went down while sending "
925 "recovery state for dead node %u, ret=%d\n", dlm->name,
926 reco_master, dead_node, ret);
932 /* move the resources back to the list */
933 spin_lock(&dlm->spinlock);
934 list_splice_init(&resources, &dlm->reco.resources);
935 spin_unlock(&dlm->spinlock);
937 if (!skip_all_done) {
938 ret = dlm_send_all_done_msg(dlm, dead_node, reco_master);
940 mlog(ML_ERROR, "%s: node %u went down while sending "
941 "recovery all-done for dead node %u, ret=%d\n",
942 dlm->name, reco_master, dead_node, ret);
946 free_page((unsigned long)data);
950 static int dlm_send_all_done_msg(struct dlm_ctxt *dlm, u8 dead_node, u8 send_to)
953 struct dlm_reco_data_done done_msg;
955 memset(&done_msg, 0, sizeof(done_msg));
956 done_msg.node_idx = dlm->node_num;
957 done_msg.dead_node = dead_node;
958 mlog(0, "sending DATA DONE message to %u, "
959 "my node=%u, dead node=%u\n", send_to, done_msg.node_idx,
962 ret = o2net_send_message(DLM_RECO_DATA_DONE_MSG, dlm->key, &done_msg,
963 sizeof(done_msg), send_to, &tmpret);
965 if (!dlm_is_host_down(ret)) {
967 mlog(ML_ERROR, "%s: unknown error sending data-done "
968 "to %u\n", dlm->name, send_to);
977 int dlm_reco_data_done_handler(struct o2net_msg *msg, u32 len, void *data)
979 struct dlm_ctxt *dlm = data;
980 struct dlm_reco_data_done *done = (struct dlm_reco_data_done *)msg->buf;
981 struct list_head *iter;
982 struct dlm_reco_node_data *ndata = NULL;
988 mlog(0, "got DATA DONE: dead_node=%u, reco.dead_node=%u, "
989 "node_idx=%u, this node=%u\n", done->dead_node,
990 dlm->reco.dead_node, done->node_idx, dlm->node_num);
992 mlog_bug_on_msg((done->dead_node != dlm->reco.dead_node),
993 "Got DATA DONE: dead_node=%u, reco.dead_node=%u, "
994 "node_idx=%u, this node=%u\n", done->dead_node,
995 dlm->reco.dead_node, done->node_idx, dlm->node_num);
997 spin_lock(&dlm_reco_state_lock);
998 list_for_each(iter, &dlm->reco.node_data) {
999 ndata = list_entry (iter, struct dlm_reco_node_data, list);
1000 if (ndata->node_num != done->node_idx)
1003 switch (ndata->state) {
1004 /* should have moved beyond INIT but not to FINALIZE yet */
1005 case DLM_RECO_NODE_DATA_INIT:
1006 case DLM_RECO_NODE_DATA_DEAD:
1007 case DLM_RECO_NODE_DATA_FINALIZE_SENT:
1008 mlog(ML_ERROR, "bad ndata state for node %u:"
1009 " state=%d\n", ndata->node_num,
1013 /* these states are possible at this point, anywhere along
1014 * the line of recovery */
1015 case DLM_RECO_NODE_DATA_DONE:
1016 case DLM_RECO_NODE_DATA_RECEIVING:
1017 case DLM_RECO_NODE_DATA_REQUESTED:
1018 case DLM_RECO_NODE_DATA_REQUESTING:
1019 mlog(0, "node %u is DONE sending "
1023 ndata->state = DLM_RECO_NODE_DATA_DONE;
1028 spin_unlock(&dlm_reco_state_lock);
1030 /* wake the recovery thread, some node is done */
1032 dlm_kick_recovery_thread(dlm);
1035 mlog(ML_ERROR, "failed to find recovery node data for node "
1036 "%u\n", done->node_idx);
1039 mlog(0, "leaving reco data done handler, ret=%d\n", ret);
1043 static void dlm_move_reco_locks_to_list(struct dlm_ctxt *dlm,
1044 struct list_head *list,
1047 struct dlm_lock_resource *res;
1048 struct list_head *iter, *iter2;
1049 struct dlm_lock *lock;
1051 spin_lock(&dlm->spinlock);
1052 list_for_each_safe(iter, iter2, &dlm->reco.resources) {
1053 res = list_entry (iter, struct dlm_lock_resource, recovering);
1054 /* always prune any $RECOVERY entries for dead nodes,
1055 * otherwise hangs can occur during later recovery */
1056 if (dlm_is_recovery_lock(res->lockname.name,
1057 res->lockname.len)) {
1058 spin_lock(&res->spinlock);
1059 list_for_each_entry(lock, &res->granted, list) {
1060 if (lock->ml.node == dead_node) {
1061 mlog(0, "AHA! there was "
1062 "a $RECOVERY lock for dead "
1064 dead_node, dlm->name);
1065 list_del_init(&lock->list);
1070 spin_unlock(&res->spinlock);
1074 if (res->owner == dead_node) {
1075 mlog(0, "found lockres owned by dead node while "
1076 "doing recovery for node %u. sending it.\n",
1078 list_move_tail(&res->recovering, list);
1079 } else if (res->owner == DLM_LOCK_RES_OWNER_UNKNOWN) {
1080 mlog(0, "found UNKNOWN owner while doing recovery "
1081 "for node %u. sending it.\n", dead_node);
1082 list_move_tail(&res->recovering, list);
1085 spin_unlock(&dlm->spinlock);
1088 static inline int dlm_num_locks_in_lockres(struct dlm_lock_resource *res)
1090 int total_locks = 0;
1091 struct list_head *iter, *queue = &res->granted;
1094 for (i=0; i<3; i++) {
1095 list_for_each(iter, queue)
1103 static int dlm_send_mig_lockres_msg(struct dlm_ctxt *dlm,
1104 struct dlm_migratable_lockres *mres,
1106 struct dlm_lock_resource *res,
1109 u64 mig_cookie = be64_to_cpu(mres->mig_cookie);
1110 int mres_total_locks = be32_to_cpu(mres->total_locks);
1111 int sz, ret = 0, status = 0;
1112 u8 orig_flags = mres->flags,
1113 orig_master = mres->master;
1115 BUG_ON(mres->num_locks > DLM_MAX_MIGRATABLE_LOCKS);
1116 if (!mres->num_locks)
1119 sz = sizeof(struct dlm_migratable_lockres) +
1120 (mres->num_locks * sizeof(struct dlm_migratable_lock));
1122 /* add an all-done flag if we reached the last lock */
1123 orig_flags = mres->flags;
1124 BUG_ON(total_locks > mres_total_locks);
1125 if (total_locks == mres_total_locks)
1126 mres->flags |= DLM_MRES_ALL_DONE;
1129 ret = o2net_send_message(DLM_MIG_LOCKRES_MSG, dlm->key, mres,
1130 sz, send_to, &status);
1132 /* XXX: negative status is not handled.
1133 * this will end up killing this node. */
1136 /* might get an -ENOMEM back here */
1141 if (ret == -EFAULT) {
1142 mlog(ML_ERROR, "node %u told me to kill "
1143 "myself!\n", send_to);
1149 /* zero and reinit the message buffer */
1150 dlm_init_migratable_lockres(mres, res->lockname.name,
1151 res->lockname.len, mres_total_locks,
1152 mig_cookie, orig_flags, orig_master);
1156 static void dlm_init_migratable_lockres(struct dlm_migratable_lockres *mres,
1157 const char *lockname, int namelen,
1158 int total_locks, u64 cookie,
1159 u8 flags, u8 master)
1161 /* mres here is one full page */
1162 memset(mres, 0, PAGE_SIZE);
1163 mres->lockname_len = namelen;
1164 memcpy(mres->lockname, lockname, namelen);
1165 mres->num_locks = 0;
1166 mres->total_locks = cpu_to_be32(total_locks);
1167 mres->mig_cookie = cpu_to_be64(cookie);
1168 mres->flags = flags;
1169 mres->master = master;
1173 /* returns 1 if this lock fills the network structure,
1175 static int dlm_add_lock_to_array(struct dlm_lock *lock,
1176 struct dlm_migratable_lockres *mres, int queue)
1178 struct dlm_migratable_lock *ml;
1179 int lock_num = mres->num_locks;
1181 ml = &(mres->ml[lock_num]);
1182 ml->cookie = lock->ml.cookie;
1183 ml->type = lock->ml.type;
1184 ml->convert_type = lock->ml.convert_type;
1185 ml->highest_blocked = lock->ml.highest_blocked;
1188 ml->flags = lock->lksb->flags;
1189 /* send our current lvb */
1190 if (ml->type == LKM_EXMODE ||
1191 ml->type == LKM_PRMODE) {
1192 /* if it is already set, this had better be a PR
1193 * and it has to match */
1194 if (!dlm_lvb_is_empty(mres->lvb) &&
1195 (ml->type == LKM_EXMODE ||
1196 memcmp(mres->lvb, lock->lksb->lvb, DLM_LVB_LEN))) {
1197 mlog(ML_ERROR, "mismatched lvbs!\n");
1198 __dlm_print_one_lock_resource(lock->lockres);
1201 memcpy(mres->lvb, lock->lksb->lvb, DLM_LVB_LEN);
1204 ml->node = lock->ml.node;
1206 /* we reached the max, send this network message */
1207 if (mres->num_locks == DLM_MAX_MIGRATABLE_LOCKS)
1213 int dlm_send_one_lockres(struct dlm_ctxt *dlm, struct dlm_lock_resource *res,
1214 struct dlm_migratable_lockres *mres,
1215 u8 send_to, u8 flags)
1217 struct list_head *queue, *iter;
1220 struct dlm_lock *lock;
1223 BUG_ON(!(flags & (DLM_MRES_RECOVERY|DLM_MRES_MIGRATION)));
1225 mlog(0, "sending to %u\n", send_to);
1227 total_locks = dlm_num_locks_in_lockres(res);
1228 if (total_locks > DLM_MAX_MIGRATABLE_LOCKS) {
1229 /* rare, but possible */
1230 mlog(0, "argh. lockres has %d locks. this will "
1231 "require more than one network packet to "
1232 "migrate\n", total_locks);
1233 mig_cookie = dlm_get_next_mig_cookie();
1236 dlm_init_migratable_lockres(mres, res->lockname.name,
1237 res->lockname.len, total_locks,
1238 mig_cookie, flags, res->owner);
1241 for (i=DLM_GRANTED_LIST; i<=DLM_BLOCKED_LIST; i++) {
1242 queue = dlm_list_idx_to_ptr(res, i);
1243 list_for_each(iter, queue) {
1244 lock = list_entry (iter, struct dlm_lock, list);
1246 /* add another lock. */
1248 if (!dlm_add_lock_to_array(lock, mres, i))
1251 /* this filled the lock message,
1252 * we must send it immediately. */
1253 ret = dlm_send_mig_lockres_msg(dlm, mres, send_to,
1259 /* flush any remaining locks */
1260 ret = dlm_send_mig_lockres_msg(dlm, mres, send_to, res, total_locks);
1266 mlog(ML_ERROR, "%s: dlm_send_mig_lockres_msg returned %d\n",
1268 if (!dlm_is_host_down(ret))
1270 mlog(0, "%s: node %u went down while sending %s "
1271 "lockres %.*s\n", dlm->name, send_to,
1272 flags & DLM_MRES_RECOVERY ? "recovery" : "migration",
1273 res->lockname.len, res->lockname.name);
1280 * this message will contain no more than one page worth of
1281 * recovery data, and it will work on only one lockres.
1282 * there may be many locks in this page, and we may need to wait
1283 * for additional packets to complete all the locks (rare, but
1287 * NOTE: the allocation error cases here are scary
1288 * we really cannot afford to fail an alloc in recovery
1289 * do we spin? returning an error only delays the problem really
1292 int dlm_mig_lockres_handler(struct o2net_msg *msg, u32 len, void *data)
1294 struct dlm_ctxt *dlm = data;
1295 struct dlm_migratable_lockres *mres =
1296 (struct dlm_migratable_lockres *)msg->buf;
1300 struct dlm_work_item *item = NULL;
1301 struct dlm_lock_resource *res = NULL;
1306 BUG_ON(!(mres->flags & (DLM_MRES_RECOVERY|DLM_MRES_MIGRATION)));
1308 real_master = mres->master;
1309 if (real_master == DLM_LOCK_RES_OWNER_UNKNOWN) {
1310 /* cannot migrate a lockres with no master */
1311 BUG_ON(!(mres->flags & DLM_MRES_RECOVERY));
1314 mlog(0, "%s message received from node %u\n",
1315 (mres->flags & DLM_MRES_RECOVERY) ?
1316 "recovery" : "migration", mres->master);
1317 if (mres->flags & DLM_MRES_ALL_DONE)
1318 mlog(0, "all done flag. all lockres data received!\n");
1321 buf = kmalloc(be16_to_cpu(msg->data_len), GFP_NOFS);
1322 item = kcalloc(1, sizeof(*item), GFP_NOFS);
1326 /* lookup the lock to see if we have a secondary queue for this
1327 * already... just add the locks in and this will have its owner
1328 * and RECOVERY flag changed when it completes. */
1329 res = dlm_lookup_lockres(dlm, mres->lockname, mres->lockname_len);
1331 /* this will get a ref on res */
1332 /* mark it as recovering/migrating and hash it */
1333 spin_lock(&res->spinlock);
1334 if (mres->flags & DLM_MRES_RECOVERY) {
1335 res->state |= DLM_LOCK_RES_RECOVERING;
1337 if (res->state & DLM_LOCK_RES_MIGRATING) {
1338 /* this is at least the second
1339 * lockres message */
1340 mlog(0, "lock %.*s is already migrating\n",
1343 } else if (res->state & DLM_LOCK_RES_RECOVERING) {
1344 /* caller should BUG */
1345 mlog(ML_ERROR, "node is attempting to migrate "
1346 "lock %.*s, but marked as recovering!\n",
1347 mres->lockname_len, mres->lockname);
1349 spin_unlock(&res->spinlock);
1352 res->state |= DLM_LOCK_RES_MIGRATING;
1354 spin_unlock(&res->spinlock);
1356 /* need to allocate, just like if it was
1357 * mastered here normally */
1358 res = dlm_new_lockres(dlm, mres->lockname, mres->lockname_len);
1362 /* to match the ref that we would have gotten if
1363 * dlm_lookup_lockres had succeeded */
1364 dlm_lockres_get(res);
1366 /* mark it as recovering/migrating and hash it */
1367 if (mres->flags & DLM_MRES_RECOVERY)
1368 res->state |= DLM_LOCK_RES_RECOVERING;
1370 res->state |= DLM_LOCK_RES_MIGRATING;
1372 spin_lock(&dlm->spinlock);
1373 __dlm_insert_lockres(dlm, res);
1374 spin_unlock(&dlm->spinlock);
1376 /* now that the new lockres is inserted,
1377 * make it usable by other processes */
1378 spin_lock(&res->spinlock);
1379 res->state &= ~DLM_LOCK_RES_IN_PROGRESS;
1380 spin_unlock(&res->spinlock);
1382 /* add an extra ref for just-allocated lockres
1383 * otherwise the lockres will be purged immediately */
1384 dlm_lockres_get(res);
1388 /* at this point we have allocated everything we need,
1389 * and we have a hashed lockres with an extra ref and
1390 * the proper res->state flags. */
1392 if (mres->master == DLM_LOCK_RES_OWNER_UNKNOWN) {
1393 /* migration cannot have an unknown master */
1394 BUG_ON(!(mres->flags & DLM_MRES_RECOVERY));
1395 mlog(0, "recovery has passed me a lockres with an "
1396 "unknown owner.. will need to requery: "
1397 "%.*s\n", mres->lockname_len, mres->lockname);
1399 spin_lock(&res->spinlock);
1400 dlm_change_lockres_owner(dlm, res, dlm->node_num);
1401 spin_unlock(&res->spinlock);
1404 /* queue up work for dlm_mig_lockres_worker */
1405 dlm_grab(dlm); /* get an extra ref for the work item */
1406 memcpy(buf, msg->buf, be16_to_cpu(msg->data_len)); /* copy the whole message */
1407 dlm_init_work_item(dlm, item, dlm_mig_lockres_worker, buf);
1408 item->u.ml.lockres = res; /* already have a ref */
1409 item->u.ml.real_master = real_master;
1410 spin_lock(&dlm->work_lock);
1411 list_add_tail(&item->list, &dlm->work_list);
1412 spin_unlock(&dlm->work_lock);
1413 queue_work(dlm->dlm_worker, &dlm->dispatched_work);
1429 static void dlm_mig_lockres_worker(struct dlm_work_item *item, void *data)
1431 struct dlm_ctxt *dlm = data;
1432 struct dlm_migratable_lockres *mres;
1434 struct dlm_lock_resource *res;
1438 mres = (struct dlm_migratable_lockres *)data;
1440 res = item->u.ml.lockres;
1441 real_master = item->u.ml.real_master;
1443 if (real_master == DLM_LOCK_RES_OWNER_UNKNOWN) {
1444 /* this case is super-rare. only occurs if
1445 * node death happens during migration. */
1447 ret = dlm_lockres_master_requery(dlm, res, &real_master);
1449 mlog(0, "dlm_lockres_master_requery ret=%d\n",
1453 if (real_master == DLM_LOCK_RES_OWNER_UNKNOWN) {
1454 mlog(0, "lockres %.*s not claimed. "
1455 "this node will take it.\n",
1456 res->lockname.len, res->lockname.name);
1458 mlog(0, "master needs to respond to sender "
1459 "that node %u still owns %.*s\n",
1460 real_master, res->lockname.len,
1461 res->lockname.name);
1462 /* cannot touch this lockres */
1467 ret = dlm_process_recovery_data(dlm, res, mres);
1469 mlog(0, "dlm_process_recovery_data returned %d\n", ret);
1471 mlog(0, "dlm_process_recovery_data succeeded\n");
1473 if ((mres->flags & (DLM_MRES_MIGRATION|DLM_MRES_ALL_DONE)) ==
1474 (DLM_MRES_MIGRATION|DLM_MRES_ALL_DONE)) {
1475 ret = dlm_finish_migration(dlm, res, mres->master);
1487 int dlm_lockres_master_requery(struct dlm_ctxt *dlm,
1488 struct dlm_lock_resource *res, u8 *real_master)
1490 struct dlm_node_iter iter;
1494 *real_master = DLM_LOCK_RES_OWNER_UNKNOWN;
1496 /* we only reach here if one of the two nodes in a
1497 * migration died while the migration was in progress.
1498 * at this point we need to requery the master. we
1499 * know that the new_master got as far as creating
1500 * an mle on at least one node, but we do not know
1501 * if any nodes had actually cleared the mle and set
1502 * the master to the new_master. the old master
1503 * is supposed to set the owner to UNKNOWN in the
1504 * event of a new_master death, so the only possible
1505 * responses that we can get from nodes here are
1506 * that the master is new_master, or that the master
1508 * if all nodes come back with UNKNOWN then we know
1509 * the lock needs remastering here.
1510 * if any node comes back with a valid master, check
1511 * to see if that master is the one that we are
1512 * recovering. if so, then the new_master died and
1513 * we need to remaster this lock. if not, then the
1514 * new_master survived and that node will respond to
1515 * other nodes about the owner.
1516 * if there is an owner, this node needs to dump this
1517 * lockres and alert the sender that this lockres
1519 spin_lock(&dlm->spinlock);
1520 dlm_node_iter_init(dlm->domain_map, &iter);
1521 spin_unlock(&dlm->spinlock);
1523 while ((nodenum = dlm_node_iter_next(&iter)) >= 0) {
1524 /* do not send to self */
1525 if (nodenum == dlm->node_num)
1527 ret = dlm_do_master_requery(dlm, res, nodenum, real_master);
1530 if (!dlm_is_host_down(ret))
1532 /* host is down, so answer for that node would be
1533 * DLM_LOCK_RES_OWNER_UNKNOWN. continue. */
1535 if (*real_master != DLM_LOCK_RES_OWNER_UNKNOWN) {
1536 mlog(0, "lock master is %u\n", *real_master);
1544 int dlm_do_master_requery(struct dlm_ctxt *dlm, struct dlm_lock_resource *res,
1545 u8 nodenum, u8 *real_master)
1548 struct dlm_master_requery req;
1549 int status = DLM_LOCK_RES_OWNER_UNKNOWN;
1551 memset(&req, 0, sizeof(req));
1552 req.node_idx = dlm->node_num;
1553 req.namelen = res->lockname.len;
1554 memcpy(req.name, res->lockname.name, res->lockname.len);
1556 ret = o2net_send_message(DLM_MASTER_REQUERY_MSG, dlm->key,
1557 &req, sizeof(req), nodenum, &status);
1558 /* XXX: negative status not handled properly here. */
1563 BUG_ON(status > DLM_LOCK_RES_OWNER_UNKNOWN);
1564 *real_master = (u8) (status & 0xff);
1565 mlog(0, "node %u responded to master requery with %u\n",
1566 nodenum, *real_master);
1573 /* this function cannot error, so unless the sending
1574 * or receiving of the message failed, the owner can
1576 int dlm_master_requery_handler(struct o2net_msg *msg, u32 len, void *data)
1578 struct dlm_ctxt *dlm = data;
1579 struct dlm_master_requery *req = (struct dlm_master_requery *)msg->buf;
1580 struct dlm_lock_resource *res = NULL;
1582 int master = DLM_LOCK_RES_OWNER_UNKNOWN;
1583 u32 flags = DLM_ASSERT_MASTER_REQUERY;
1585 if (!dlm_grab(dlm)) {
1586 /* since the domain has gone away on this
1587 * node, the proper response is UNKNOWN */
1591 hash = dlm_lockid_hash(req->name, req->namelen);
1593 spin_lock(&dlm->spinlock);
1594 res = __dlm_lookup_lockres(dlm, req->name, req->namelen, hash);
1596 spin_lock(&res->spinlock);
1597 master = res->owner;
1598 if (master == dlm->node_num) {
1599 int ret = dlm_dispatch_assert_master(dlm, res,
1602 mlog_errno(-ENOMEM);
1607 spin_unlock(&res->spinlock);
1609 spin_unlock(&dlm->spinlock);
1615 static inline struct list_head *
1616 dlm_list_num_to_pointer(struct dlm_lock_resource *res, int list_num)
1618 struct list_head *ret;
1619 BUG_ON(list_num < 0);
1620 BUG_ON(list_num > 2);
1621 ret = &(res->granted);
1625 /* TODO: do ast flush business
1626 * TODO: do MIGRATING and RECOVERING spinning
1630 * NOTE about in-flight requests during migration:
1632 * Before attempting the migrate, the master has marked the lockres as
1633 * MIGRATING and then flushed all of its pending ASTS. So any in-flight
1634 * requests either got queued before the MIGRATING flag got set, in which
1635 * case the lock data will reflect the change and a return message is on
1636 * the way, or the request failed to get in before MIGRATING got set. In
1637 * this case, the caller will be told to spin and wait for the MIGRATING
1638 * flag to be dropped, then recheck the master.
1639 * This holds true for the convert, cancel and unlock cases, and since lvb
1640 * updates are tied to these same messages, it applies to lvb updates as
1641 * well. For the lock case, there is no way a lock can be on the master
1642 * queue and not be on the secondary queue since the lock is always added
1643 * locally first. This means that the new target node will never be sent
1644 * a lock that he doesn't already have on the list.
1645 * In total, this means that the local lock is correct and should not be
1646 * updated to match the one sent by the master. Any messages sent back
1647 * from the master before the MIGRATING flag will bring the lock properly
1648 * up-to-date, and the change will be ordered properly for the waiter.
1649 * We will *not* attempt to modify the lock underneath the waiter.
1652 static int dlm_process_recovery_data(struct dlm_ctxt *dlm,
1653 struct dlm_lock_resource *res,
1654 struct dlm_migratable_lockres *mres)
1656 struct dlm_migratable_lock *ml;
1657 struct list_head *queue;
1658 struct dlm_lock *newlock = NULL;
1659 struct dlm_lockstatus *lksb = NULL;
1662 struct list_head *iter;
1663 struct dlm_lock *lock = NULL;
1665 mlog(0, "running %d locks for this lockres\n", mres->num_locks);
1666 for (i=0; i<mres->num_locks; i++) {
1667 ml = &(mres->ml[i]);
1668 BUG_ON(ml->highest_blocked != LKM_IVMODE);
1672 queue = dlm_list_num_to_pointer(res, ml->list);
1674 /* if the lock is for the local node it needs to
1675 * be moved to the proper location within the queue.
1676 * do not allocate a new lock structure. */
1677 if (ml->node == dlm->node_num) {
1678 /* MIGRATION ONLY! */
1679 BUG_ON(!(mres->flags & DLM_MRES_MIGRATION));
1681 spin_lock(&res->spinlock);
1682 list_for_each(iter, queue) {
1683 lock = list_entry (iter, struct dlm_lock, list);
1684 if (lock->ml.cookie != ml->cookie)
1690 /* lock is always created locally first, and
1691 * destroyed locally last. it must be on the list */
1694 mlog(ML_ERROR, "could not find local lock "
1695 "with cookie %u:%llu!\n",
1696 dlm_get_lock_cookie_node(c),
1697 dlm_get_lock_cookie_seq(c));
1700 BUG_ON(lock->ml.node != ml->node);
1702 /* see NOTE above about why we do not update
1703 * to match the master here */
1705 /* move the lock to its proper place */
1706 /* do not alter lock refcount. switching lists. */
1707 list_move_tail(&lock->list, queue);
1708 spin_unlock(&res->spinlock);
1710 mlog(0, "just reordered a local lock!\n");
1714 /* lock is for another node. */
1715 newlock = dlm_new_lock(ml->type, ml->node,
1716 be64_to_cpu(ml->cookie), NULL);
1721 lksb = newlock->lksb;
1722 dlm_lock_attach_lockres(newlock, res);
1724 if (ml->convert_type != LKM_IVMODE) {
1725 BUG_ON(queue != &res->converting);
1726 newlock->ml.convert_type = ml->convert_type;
1728 lksb->flags |= (ml->flags &
1729 (DLM_LKSB_PUT_LVB|DLM_LKSB_GET_LVB));
1731 if (ml->type == LKM_NLMODE)
1734 if (!dlm_lvb_is_empty(mres->lvb)) {
1735 if (lksb->flags & DLM_LKSB_PUT_LVB) {
1736 /* other node was trying to update
1737 * lvb when node died. recreate the
1738 * lksb with the updated lvb. */
1739 memcpy(lksb->lvb, mres->lvb, DLM_LVB_LEN);
1740 /* the lock resource lvb update must happen
1741 * NOW, before the spinlock is dropped.
1742 * we no longer wait for the AST to update
1744 memcpy(res->lvb, mres->lvb, DLM_LVB_LEN);
1746 /* otherwise, the node is sending its
1747 * most recent valid lvb info */
1748 BUG_ON(ml->type != LKM_EXMODE &&
1749 ml->type != LKM_PRMODE);
1750 if (!dlm_lvb_is_empty(res->lvb) &&
1751 (ml->type == LKM_EXMODE ||
1752 memcmp(res->lvb, mres->lvb, DLM_LVB_LEN))) {
1754 mlog(ML_ERROR, "%s:%.*s: received bad "
1755 "lvb! type=%d\n", dlm->name,
1757 res->lockname.name, ml->type);
1758 printk("lockres lvb=[");
1759 for (i=0; i<DLM_LVB_LEN; i++)
1760 printk("%02x", res->lvb[i]);
1761 printk("]\nmigrated lvb=[");
1762 for (i=0; i<DLM_LVB_LEN; i++)
1763 printk("%02x", mres->lvb[i]);
1765 dlm_print_one_lock_resource(res);
1768 memcpy(res->lvb, mres->lvb, DLM_LVB_LEN);
1774 * wrt lock queue ordering and recovery:
1775 * 1. order of locks on granted queue is
1777 * 2. order of locks on converting queue is
1778 * LOST with the node death. sorry charlie.
1779 * 3. order of locks on the blocked queue is
1781 * order of locks does not affect integrity, it
1782 * just means that a lock request may get pushed
1783 * back in line as a result of the node death.
1784 * also note that for a given node the lock order
1785 * for its secondary queue locks is preserved
1786 * relative to each other, but clearly *not*
1787 * preserved relative to locks from other nodes.
1790 spin_lock(&res->spinlock);
1791 list_for_each_entry(lock, queue, list) {
1792 if (lock->ml.cookie == ml->cookie) {
1793 u64 c = lock->ml.cookie;
1794 mlog(ML_ERROR, "%s:%.*s: %u:%llu: lock already "
1795 "exists on this lockres!\n", dlm->name,
1796 res->lockname.len, res->lockname.name,
1797 dlm_get_lock_cookie_node(c),
1798 dlm_get_lock_cookie_seq(c));
1800 mlog(ML_NOTICE, "sent lock: type=%d, conv=%d, "
1801 "node=%u, cookie=%u:%llu, queue=%d\n",
1802 ml->type, ml->convert_type, ml->node,
1803 dlm_get_lock_cookie_node(ml->cookie),
1804 dlm_get_lock_cookie_seq(ml->cookie),
1807 __dlm_print_one_lock_resource(res);
1813 dlm_lock_get(newlock);
1814 list_add_tail(&newlock->list, queue);
1816 spin_unlock(&res->spinlock);
1818 mlog(0, "done running all the locks\n");
1824 dlm_lock_put(newlock);
1831 void dlm_move_lockres_to_recovery_list(struct dlm_ctxt *dlm,
1832 struct dlm_lock_resource *res)
1835 struct list_head *queue, *iter, *iter2;
1836 struct dlm_lock *lock;
1838 res->state |= DLM_LOCK_RES_RECOVERING;
1839 if (!list_empty(&res->recovering)) {
1841 "Recovering res %s:%.*s, is already on recovery list!\n",
1842 dlm->name, res->lockname.len, res->lockname.name);
1843 list_del_init(&res->recovering);
1845 /* We need to hold a reference while on the recovery list */
1846 dlm_lockres_get(res);
1847 list_add_tail(&res->recovering, &dlm->reco.resources);
1849 /* find any pending locks and put them back on proper list */
1850 for (i=DLM_BLOCKED_LIST; i>=DLM_GRANTED_LIST; i--) {
1851 queue = dlm_list_idx_to_ptr(res, i);
1852 list_for_each_safe(iter, iter2, queue) {
1853 lock = list_entry (iter, struct dlm_lock, list);
1855 if (lock->convert_pending) {
1856 /* move converting lock back to granted */
1857 BUG_ON(i != DLM_CONVERTING_LIST);
1858 mlog(0, "node died with convert pending "
1859 "on %.*s. move back to granted list.\n",
1860 res->lockname.len, res->lockname.name);
1861 dlm_revert_pending_convert(res, lock);
1862 lock->convert_pending = 0;
1863 } else if (lock->lock_pending) {
1864 /* remove pending lock requests completely */
1865 BUG_ON(i != DLM_BLOCKED_LIST);
1866 mlog(0, "node died with lock pending "
1867 "on %.*s. remove from blocked list and skip.\n",
1868 res->lockname.len, res->lockname.name);
1869 /* lock will be floating until ref in
1870 * dlmlock_remote is freed after the network
1871 * call returns. ok for it to not be on any
1872 * list since no ast can be called
1873 * (the master is dead). */
1874 dlm_revert_pending_lock(res, lock);
1875 lock->lock_pending = 0;
1876 } else if (lock->unlock_pending) {
1877 /* if an unlock was in progress, treat as
1878 * if this had completed successfully
1879 * before sending this lock state to the
1880 * new master. note that the dlm_unlock
1881 * call is still responsible for calling
1882 * the unlockast. that will happen after
1883 * the network call times out. for now,
1884 * just move lists to prepare the new
1885 * recovery master. */
1886 BUG_ON(i != DLM_GRANTED_LIST);
1887 mlog(0, "node died with unlock pending "
1888 "on %.*s. remove from blocked list and skip.\n",
1889 res->lockname.len, res->lockname.name);
1890 dlm_commit_pending_unlock(res, lock);
1891 lock->unlock_pending = 0;
1892 } else if (lock->cancel_pending) {
1893 /* if a cancel was in progress, treat as
1894 * if this had completed successfully
1895 * before sending this lock state to the
1897 BUG_ON(i != DLM_CONVERTING_LIST);
1898 mlog(0, "node died with cancel pending "
1899 "on %.*s. move back to granted list.\n",
1900 res->lockname.len, res->lockname.name);
1901 dlm_commit_pending_cancel(res, lock);
1902 lock->cancel_pending = 0;
1911 /* removes all recovered locks from the recovery list.
1912 * sets the res->owner to the new master.
1913 * unsets the RECOVERY flag and wakes waiters. */
1914 static void dlm_finish_local_lockres_recovery(struct dlm_ctxt *dlm,
1915 u8 dead_node, u8 new_master)
1918 struct list_head *iter, *iter2;
1919 struct hlist_node *hash_iter;
1920 struct hlist_head *bucket;
1922 struct dlm_lock_resource *res;
1926 assert_spin_locked(&dlm->spinlock);
1928 list_for_each_safe(iter, iter2, &dlm->reco.resources) {
1929 res = list_entry (iter, struct dlm_lock_resource, recovering);
1930 if (res->owner == dead_node) {
1931 list_del_init(&res->recovering);
1932 spin_lock(&res->spinlock);
1933 dlm_change_lockres_owner(dlm, res, new_master);
1934 res->state &= ~DLM_LOCK_RES_RECOVERING;
1935 if (!__dlm_lockres_unused(res))
1936 __dlm_dirty_lockres(dlm, res);
1937 spin_unlock(&res->spinlock);
1939 dlm_lockres_put(res);
1943 /* this will become unnecessary eventually, but
1944 * for now we need to run the whole hash, clear
1945 * the RECOVERING state and set the owner
1947 for (i = 0; i < DLM_HASH_BUCKETS; i++) {
1948 bucket = dlm_lockres_hash(dlm, i);
1949 hlist_for_each_entry(res, hash_iter, bucket, hash_node) {
1950 if (res->state & DLM_LOCK_RES_RECOVERING) {
1951 if (res->owner == dead_node) {
1952 mlog(0, "(this=%u) res %.*s owner=%u "
1953 "was not on recovering list, but "
1954 "clearing state anyway\n",
1955 dlm->node_num, res->lockname.len,
1956 res->lockname.name, new_master);
1957 } else if (res->owner == dlm->node_num) {
1958 mlog(0, "(this=%u) res %.*s owner=%u "
1959 "was not on recovering list, "
1960 "owner is THIS node, clearing\n",
1961 dlm->node_num, res->lockname.len,
1962 res->lockname.name, new_master);
1966 if (!list_empty(&res->recovering)) {
1967 mlog(0, "%s:%.*s: lockres was "
1968 "marked RECOVERING, owner=%u\n",
1969 dlm->name, res->lockname.len,
1970 res->lockname.name, res->owner);
1971 list_del_init(&res->recovering);
1972 dlm_lockres_put(res);
1974 spin_lock(&res->spinlock);
1975 dlm_change_lockres_owner(dlm, res, new_master);
1976 res->state &= ~DLM_LOCK_RES_RECOVERING;
1977 if (!__dlm_lockres_unused(res))
1978 __dlm_dirty_lockres(dlm, res);
1979 spin_unlock(&res->spinlock);
1986 static inline int dlm_lvb_needs_invalidation(struct dlm_lock *lock, int local)
1989 if (lock->ml.type != LKM_EXMODE &&
1990 lock->ml.type != LKM_PRMODE)
1992 } else if (lock->ml.type == LKM_EXMODE)
1997 static void dlm_revalidate_lvb(struct dlm_ctxt *dlm,
1998 struct dlm_lock_resource *res, u8 dead_node)
2000 struct list_head *iter, *queue;
2001 struct dlm_lock *lock;
2002 int blank_lvb = 0, local = 0;
2006 assert_spin_locked(&dlm->spinlock);
2007 assert_spin_locked(&res->spinlock);
2009 if (res->owner == dlm->node_num)
2010 /* if this node owned the lockres, and if the dead node
2011 * had an EX when he died, blank out the lvb */
2012 search_node = dead_node;
2014 /* if this is a secondary lockres, and we had no EX or PR
2015 * locks granted, we can no longer trust the lvb */
2016 search_node = dlm->node_num;
2017 local = 1; /* check local state for valid lvb */
2020 for (i=DLM_GRANTED_LIST; i<=DLM_CONVERTING_LIST; i++) {
2021 queue = dlm_list_idx_to_ptr(res, i);
2022 list_for_each(iter, queue) {
2023 lock = list_entry (iter, struct dlm_lock, list);
2024 if (lock->ml.node == search_node) {
2025 if (dlm_lvb_needs_invalidation(lock, local)) {
2026 /* zero the lksb lvb and lockres lvb */
2028 memset(lock->lksb->lvb, 0, DLM_LVB_LEN);
2035 mlog(0, "clearing %.*s lvb, dead node %u had EX\n",
2036 res->lockname.len, res->lockname.name, dead_node);
2037 memset(res->lvb, 0, DLM_LVB_LEN);
2041 static void dlm_free_dead_locks(struct dlm_ctxt *dlm,
2042 struct dlm_lock_resource *res, u8 dead_node)
2044 struct list_head *iter, *tmpiter;
2045 struct dlm_lock *lock;
2047 /* this node is the lockres master:
2048 * 1) remove any stale locks for the dead node
2049 * 2) if the dead node had an EX when he died, blank out the lvb
2051 assert_spin_locked(&dlm->spinlock);
2052 assert_spin_locked(&res->spinlock);
2054 /* TODO: check pending_asts, pending_basts here */
2055 list_for_each_safe(iter, tmpiter, &res->granted) {
2056 lock = list_entry (iter, struct dlm_lock, list);
2057 if (lock->ml.node == dead_node) {
2058 list_del_init(&lock->list);
2062 list_for_each_safe(iter, tmpiter, &res->converting) {
2063 lock = list_entry (iter, struct dlm_lock, list);
2064 if (lock->ml.node == dead_node) {
2065 list_del_init(&lock->list);
2069 list_for_each_safe(iter, tmpiter, &res->blocked) {
2070 lock = list_entry (iter, struct dlm_lock, list);
2071 if (lock->ml.node == dead_node) {
2072 list_del_init(&lock->list);
2077 /* do not kick thread yet */
2078 __dlm_dirty_lockres(dlm, res);
2081 /* if this node is the recovery master, and there are no
2082 * locks for a given lockres owned by this node that are in
2083 * either PR or EX mode, zero out the lvb before requesting.
2088 static void dlm_do_local_recovery_cleanup(struct dlm_ctxt *dlm, u8 dead_node)
2090 struct hlist_node *iter;
2091 struct dlm_lock_resource *res;
2093 struct hlist_head *bucket;
2094 struct dlm_lock *lock;
2097 /* purge any stale mles */
2098 dlm_clean_master_list(dlm, dead_node);
2101 * now clean up all lock resources. there are two rules:
2103 * 1) if the dead node was the master, move the lockres
2104 * to the recovering list. set the RECOVERING flag.
2105 * this lockres needs to be cleaned up before it can
2108 * 2) if this node was the master, remove all locks from
2109 * each of the lockres queues that were owned by the
2110 * dead node. once recovery finishes, the dlm thread
2111 * can be kicked again to see if any ASTs or BASTs
2112 * need to be fired as a result.
2114 for (i = 0; i < DLM_HASH_BUCKETS; i++) {
2115 bucket = dlm_lockres_hash(dlm, i);
2116 hlist_for_each_entry(res, iter, bucket, hash_node) {
2117 /* always prune any $RECOVERY entries for dead nodes,
2118 * otherwise hangs can occur during later recovery */
2119 if (dlm_is_recovery_lock(res->lockname.name,
2120 res->lockname.len)) {
2121 spin_lock(&res->spinlock);
2122 list_for_each_entry(lock, &res->granted, list) {
2123 if (lock->ml.node == dead_node) {
2124 mlog(0, "AHA! there was "
2125 "a $RECOVERY lock for dead "
2127 dead_node, dlm->name);
2128 list_del_init(&lock->list);
2133 spin_unlock(&res->spinlock);
2136 spin_lock(&res->spinlock);
2137 /* zero the lvb if necessary */
2138 dlm_revalidate_lvb(dlm, res, dead_node);
2139 if (res->owner == dead_node)
2140 dlm_move_lockres_to_recovery_list(dlm, res);
2141 else if (res->owner == dlm->node_num) {
2142 dlm_free_dead_locks(dlm, res, dead_node);
2143 __dlm_lockres_calc_usage(dlm, res);
2145 spin_unlock(&res->spinlock);
2151 static void __dlm_hb_node_down(struct dlm_ctxt *dlm, int idx)
2153 assert_spin_locked(&dlm->spinlock);
2155 if (dlm->reco.new_master == idx) {
2156 mlog(0, "%s: recovery master %d just died\n",
2158 if (dlm->reco.state & DLM_RECO_STATE_FINALIZE) {
2159 /* finalize1 was reached, so it is safe to clear
2160 * the new_master and dead_node. that recovery
2162 mlog(0, "%s: dead master %d had reached "
2163 "finalize1 state, clearing\n", dlm->name, idx);
2164 dlm->reco.state &= ~DLM_RECO_STATE_FINALIZE;
2165 __dlm_reset_recovery(dlm);
2169 /* check to see if the node is already considered dead */
2170 if (!test_bit(idx, dlm->live_nodes_map)) {
2171 mlog(0, "for domain %s, node %d is already dead. "
2172 "another node likely did recovery already.\n",
2177 /* check to see if we do not care about this node */
2178 if (!test_bit(idx, dlm->domain_map)) {
2179 /* This also catches the case that we get a node down
2180 * but haven't joined the domain yet. */
2181 mlog(0, "node %u already removed from domain!\n", idx);
2185 clear_bit(idx, dlm->live_nodes_map);
2187 /* Clean up join state on node death. */
2188 if (dlm->joining_node == idx) {
2189 mlog(0, "Clearing join state for node %u\n", idx);
2190 __dlm_set_joining_node(dlm, DLM_LOCK_RES_OWNER_UNKNOWN);
2193 /* make sure local cleanup occurs before the heartbeat events */
2194 if (!test_bit(idx, dlm->recovery_map))
2195 dlm_do_local_recovery_cleanup(dlm, idx);
2197 /* notify anything attached to the heartbeat events */
2198 dlm_hb_event_notify_attached(dlm, idx, 0);
2200 mlog(0, "node %u being removed from domain map!\n", idx);
2201 clear_bit(idx, dlm->domain_map);
2202 /* wake up migration waiters if a node goes down.
2203 * perhaps later we can genericize this for other waiters. */
2204 wake_up(&dlm->migration_wq);
2206 if (test_bit(idx, dlm->recovery_map))
2207 mlog(0, "domain %s, node %u already added "
2208 "to recovery map!\n", dlm->name, idx);
2210 set_bit(idx, dlm->recovery_map);
2213 void dlm_hb_node_down_cb(struct o2nm_node *node, int idx, void *data)
2215 struct dlm_ctxt *dlm = data;
2220 spin_lock(&dlm->spinlock);
2221 __dlm_hb_node_down(dlm, idx);
2222 spin_unlock(&dlm->spinlock);
2227 void dlm_hb_node_up_cb(struct o2nm_node *node, int idx, void *data)
2229 struct dlm_ctxt *dlm = data;
2234 spin_lock(&dlm->spinlock);
2235 set_bit(idx, dlm->live_nodes_map);
2236 /* do NOT notify mle attached to the heartbeat events.
2237 * new nodes are not interesting in mastery until joined. */
2238 spin_unlock(&dlm->spinlock);
2243 static void dlm_reco_ast(void *astdata)
2245 struct dlm_ctxt *dlm = astdata;
2246 mlog(0, "ast for recovery lock fired!, this=%u, dlm=%s\n",
2247 dlm->node_num, dlm->name);
2249 static void dlm_reco_bast(void *astdata, int blocked_type)
2251 struct dlm_ctxt *dlm = astdata;
2252 mlog(0, "bast for recovery lock fired!, this=%u, dlm=%s\n",
2253 dlm->node_num, dlm->name);
2255 static void dlm_reco_unlock_ast(void *astdata, enum dlm_status st)
2257 mlog(0, "unlockast for recovery lock fired!\n");
2261 * dlm_pick_recovery_master will continually attempt to use
2262 * dlmlock() on the special "$RECOVERY" lockres with the
2263 * LKM_NOQUEUE flag to get an EX. every thread that enters
2264 * this function on each node racing to become the recovery
2265 * master will not stop attempting this until either:
2266 * a) this node gets the EX (and becomes the recovery master),
2267 * or b) dlm->reco.new_master gets set to some nodenum
2268 * != O2NM_INVALID_NODE_NUM (another node will do the reco).
2269 * so each time a recovery master is needed, the entire cluster
2270 * will sync at this point. if the new master dies, that will
2271 * be detected in dlm_do_recovery */
2272 static int dlm_pick_recovery_master(struct dlm_ctxt *dlm)
2274 enum dlm_status ret;
2275 struct dlm_lockstatus lksb;
2276 int status = -EINVAL;
2278 mlog(0, "starting recovery of %s at %lu, dead=%u, this=%u\n",
2279 dlm->name, jiffies, dlm->reco.dead_node, dlm->node_num);
2281 memset(&lksb, 0, sizeof(lksb));
2283 ret = dlmlock(dlm, LKM_EXMODE, &lksb, LKM_NOQUEUE|LKM_RECOVERY,
2284 DLM_RECOVERY_LOCK_NAME, dlm_reco_ast, dlm, dlm_reco_bast);
2286 mlog(0, "%s: dlmlock($RECOVERY) returned %d, lksb=%d\n",
2287 dlm->name, ret, lksb.status);
2289 if (ret == DLM_NORMAL) {
2290 mlog(0, "dlm=%s dlmlock says I got it (this=%u)\n",
2291 dlm->name, dlm->node_num);
2293 /* got the EX lock. check to see if another node
2294 * just became the reco master */
2295 if (dlm_reco_master_ready(dlm)) {
2296 mlog(0, "%s: got reco EX lock, but %u will "
2297 "do the recovery\n", dlm->name,
2298 dlm->reco.new_master);
2303 /* see if recovery was already finished elsewhere */
2304 spin_lock(&dlm->spinlock);
2305 if (dlm->reco.dead_node == O2NM_INVALID_NODE_NUM) {
2307 mlog(0, "%s: got reco EX lock, but "
2308 "node got recovered already\n", dlm->name);
2309 if (dlm->reco.new_master != O2NM_INVALID_NODE_NUM) {
2310 mlog(ML_ERROR, "%s: new master is %u "
2311 "but no dead node!\n",
2312 dlm->name, dlm->reco.new_master);
2316 spin_unlock(&dlm->spinlock);
2319 /* if this node has actually become the recovery master,
2320 * set the master and send the messages to begin recovery */
2322 mlog(0, "%s: dead=%u, this=%u, sending "
2323 "begin_reco now\n", dlm->name,
2324 dlm->reco.dead_node, dlm->node_num);
2325 status = dlm_send_begin_reco_message(dlm,
2326 dlm->reco.dead_node);
2327 /* this always succeeds */
2330 /* set the new_master to this node */
2331 spin_lock(&dlm->spinlock);
2332 dlm_set_reco_master(dlm, dlm->node_num);
2333 spin_unlock(&dlm->spinlock);
2336 /* recovery lock is a special case. ast will not get fired,
2337 * so just go ahead and unlock it. */
2338 ret = dlmunlock(dlm, &lksb, 0, dlm_reco_unlock_ast, dlm);
2339 if (ret == DLM_DENIED) {
2340 mlog(0, "got DLM_DENIED, trying LKM_CANCEL\n");
2341 ret = dlmunlock(dlm, &lksb, LKM_CANCEL, dlm_reco_unlock_ast, dlm);
2343 if (ret != DLM_NORMAL) {
2344 /* this would really suck. this could only happen
2345 * if there was a network error during the unlock
2346 * because of node death. this means the unlock
2347 * is actually "done" and the lock structure is
2348 * even freed. we can continue, but only
2349 * because this specific lock name is special. */
2350 mlog(ML_ERROR, "dlmunlock returned %d\n", ret);
2352 } else if (ret == DLM_NOTQUEUED) {
2353 mlog(0, "dlm=%s dlmlock says another node got it (this=%u)\n",
2354 dlm->name, dlm->node_num);
2355 /* another node is master. wait on
2356 * reco.new_master != O2NM_INVALID_NODE_NUM
2357 * for at most one second */
2358 wait_event_timeout(dlm->dlm_reco_thread_wq,
2359 dlm_reco_master_ready(dlm),
2360 msecs_to_jiffies(1000));
2361 if (!dlm_reco_master_ready(dlm)) {
2362 mlog(0, "%s: reco master taking awhile\n",
2366 /* another node has informed this one that it is reco master */
2367 mlog(0, "%s: reco master %u is ready to recover %u\n",
2368 dlm->name, dlm->reco.new_master, dlm->reco.dead_node);
2370 } else if (ret == DLM_RECOVERING) {
2371 mlog(0, "dlm=%s dlmlock says master node died (this=%u)\n",
2372 dlm->name, dlm->node_num);
2375 struct dlm_lock_resource *res;
2377 /* dlmlock returned something other than NOTQUEUED or NORMAL */
2378 mlog(ML_ERROR, "%s: got %s from dlmlock($RECOVERY), "
2379 "lksb.status=%s\n", dlm->name, dlm_errname(ret),
2380 dlm_errname(lksb.status));
2381 res = dlm_lookup_lockres(dlm, DLM_RECOVERY_LOCK_NAME,
2382 DLM_RECOVERY_LOCK_NAME_LEN);
2384 dlm_print_one_lock_resource(res);
2385 dlm_lockres_put(res);
2387 mlog(ML_ERROR, "recovery lock not found\n");
2395 static int dlm_send_begin_reco_message(struct dlm_ctxt *dlm, u8 dead_node)
2397 struct dlm_begin_reco br;
2399 struct dlm_node_iter iter;
2403 mlog_entry("%u\n", dead_node);
2405 mlog(0, "%s: dead node is %u\n", dlm->name, dead_node);
2407 spin_lock(&dlm->spinlock);
2408 dlm_node_iter_init(dlm->domain_map, &iter);
2409 spin_unlock(&dlm->spinlock);
2411 clear_bit(dead_node, iter.node_map);
2413 memset(&br, 0, sizeof(br));
2414 br.node_idx = dlm->node_num;
2415 br.dead_node = dead_node;
2417 while ((nodenum = dlm_node_iter_next(&iter)) >= 0) {
2419 if (nodenum == dead_node) {
2420 mlog(0, "not sending begin reco to dead node "
2424 if (nodenum == dlm->node_num) {
2425 mlog(0, "not sending begin reco to self\n");
2430 mlog(0, "attempting to send begin reco msg to %d\n",
2432 ret = o2net_send_message(DLM_BEGIN_RECO_MSG, dlm->key,
2433 &br, sizeof(br), nodenum, &status);
2434 /* negative status is handled ok by caller here */
2437 if (dlm_is_host_down(ret)) {
2438 /* node is down. not involved in recovery
2439 * so just keep going */
2440 mlog(0, "%s: node %u was down when sending "
2441 "begin reco msg (%d)\n", dlm->name, nodenum, ret);
2445 struct dlm_lock_resource *res;
2446 /* this is now a serious problem, possibly ENOMEM
2447 * in the network stack. must retry */
2449 mlog(ML_ERROR, "begin reco of dlm %s to node %u "
2450 " returned %d\n", dlm->name, nodenum, ret);
2451 res = dlm_lookup_lockres(dlm, DLM_RECOVERY_LOCK_NAME,
2452 DLM_RECOVERY_LOCK_NAME_LEN);
2454 dlm_print_one_lock_resource(res);
2455 dlm_lockres_put(res);
2457 mlog(ML_ERROR, "recovery lock not found\n");
2459 /* sleep for a bit in hopes that we can avoid
2463 } else if (ret == EAGAIN) {
2464 mlog(0, "%s: trying to start recovery of node "
2465 "%u, but node %u is waiting for last recovery "
2466 "to complete, backoff for a bit\n", dlm->name,
2467 dead_node, nodenum);
2468 /* TODO Look into replacing msleep with cond_resched() */
2477 int dlm_begin_reco_handler(struct o2net_msg *msg, u32 len, void *data)
2479 struct dlm_ctxt *dlm = data;
2480 struct dlm_begin_reco *br = (struct dlm_begin_reco *)msg->buf;
2482 /* ok to return 0, domain has gone away */
2486 spin_lock(&dlm->spinlock);
2487 if (dlm->reco.state & DLM_RECO_STATE_FINALIZE) {
2488 mlog(0, "%s: node %u wants to recover node %u (%u:%u) "
2489 "but this node is in finalize state, waiting on finalize2\n",
2490 dlm->name, br->node_idx, br->dead_node,
2491 dlm->reco.dead_node, dlm->reco.new_master);
2492 spin_unlock(&dlm->spinlock);
2495 spin_unlock(&dlm->spinlock);
2497 mlog(0, "%s: node %u wants to recover node %u (%u:%u)\n",
2498 dlm->name, br->node_idx, br->dead_node,
2499 dlm->reco.dead_node, dlm->reco.new_master);
2501 dlm_fire_domain_eviction_callbacks(dlm, br->dead_node);
2503 spin_lock(&dlm->spinlock);
2504 if (dlm->reco.new_master != O2NM_INVALID_NODE_NUM) {
2505 if (test_bit(dlm->reco.new_master, dlm->recovery_map)) {
2506 mlog(0, "%s: new_master %u died, changing "
2507 "to %u\n", dlm->name, dlm->reco.new_master,
2510 mlog(0, "%s: new_master %u NOT DEAD, changing "
2511 "to %u\n", dlm->name, dlm->reco.new_master,
2513 /* may not have seen the new master as dead yet */
2516 if (dlm->reco.dead_node != O2NM_INVALID_NODE_NUM) {
2517 mlog(ML_NOTICE, "%s: dead_node previously set to %u, "
2518 "node %u changing it to %u\n", dlm->name,
2519 dlm->reco.dead_node, br->node_idx, br->dead_node);
2521 dlm_set_reco_master(dlm, br->node_idx);
2522 dlm_set_reco_dead_node(dlm, br->dead_node);
2523 if (!test_bit(br->dead_node, dlm->recovery_map)) {
2524 mlog(0, "recovery master %u sees %u as dead, but this "
2525 "node has not yet. marking %u as dead\n",
2526 br->node_idx, br->dead_node, br->dead_node);
2527 if (!test_bit(br->dead_node, dlm->domain_map) ||
2528 !test_bit(br->dead_node, dlm->live_nodes_map))
2529 mlog(0, "%u not in domain/live_nodes map "
2530 "so setting it in reco map manually\n",
2532 /* force the recovery cleanup in __dlm_hb_node_down
2533 * both of these will be cleared in a moment */
2534 set_bit(br->dead_node, dlm->domain_map);
2535 set_bit(br->dead_node, dlm->live_nodes_map);
2536 __dlm_hb_node_down(dlm, br->dead_node);
2538 spin_unlock(&dlm->spinlock);
2540 dlm_kick_recovery_thread(dlm);
2542 mlog(0, "%s: recovery started by node %u, for %u (%u:%u)\n",
2543 dlm->name, br->node_idx, br->dead_node,
2544 dlm->reco.dead_node, dlm->reco.new_master);
2550 #define DLM_FINALIZE_STAGE2 0x01
2551 static int dlm_send_finalize_reco_message(struct dlm_ctxt *dlm)
2554 struct dlm_finalize_reco fr;
2555 struct dlm_node_iter iter;
2560 mlog(0, "finishing recovery for node %s:%u, "
2561 "stage %d\n", dlm->name, dlm->reco.dead_node, stage);
2563 spin_lock(&dlm->spinlock);
2564 dlm_node_iter_init(dlm->domain_map, &iter);
2565 spin_unlock(&dlm->spinlock);
2568 memset(&fr, 0, sizeof(fr));
2569 fr.node_idx = dlm->node_num;
2570 fr.dead_node = dlm->reco.dead_node;
2572 fr.flags |= DLM_FINALIZE_STAGE2;
2574 while ((nodenum = dlm_node_iter_next(&iter)) >= 0) {
2575 if (nodenum == dlm->node_num)
2577 ret = o2net_send_message(DLM_FINALIZE_RECO_MSG, dlm->key,
2578 &fr, sizeof(fr), nodenum, &status);
2583 if (dlm_is_host_down(ret)) {
2584 /* this has no effect on this recovery
2585 * session, so set the status to zero to
2586 * finish out the last recovery */
2587 mlog(ML_ERROR, "node %u went down after this "
2588 "node finished recovery.\n", nodenum);
2596 /* reset the node_iter back to the top and send finalize2 */
2605 int dlm_finalize_reco_handler(struct o2net_msg *msg, u32 len, void *data)
2607 struct dlm_ctxt *dlm = data;
2608 struct dlm_finalize_reco *fr = (struct dlm_finalize_reco *)msg->buf;
2611 /* ok to return 0, domain has gone away */
2615 if (fr->flags & DLM_FINALIZE_STAGE2)
2618 mlog(0, "%s: node %u finalizing recovery stage%d of "
2619 "node %u (%u:%u)\n", dlm->name, fr->node_idx, stage,
2620 fr->dead_node, dlm->reco.dead_node, dlm->reco.new_master);
2622 spin_lock(&dlm->spinlock);
2624 if (dlm->reco.new_master != fr->node_idx) {
2625 mlog(ML_ERROR, "node %u sent recovery finalize msg, but node "
2626 "%u is supposed to be the new master, dead=%u\n",
2627 fr->node_idx, dlm->reco.new_master, fr->dead_node);
2630 if (dlm->reco.dead_node != fr->dead_node) {
2631 mlog(ML_ERROR, "node %u sent recovery finalize msg for dead "
2632 "node %u, but node %u is supposed to be dead\n",
2633 fr->node_idx, fr->dead_node, dlm->reco.dead_node);
2639 dlm_finish_local_lockres_recovery(dlm, fr->dead_node, fr->node_idx);
2640 if (dlm->reco.state & DLM_RECO_STATE_FINALIZE) {
2641 mlog(ML_ERROR, "%s: received finalize1 from "
2642 "new master %u for dead node %u, but "
2643 "this node has already received it!\n",
2644 dlm->name, fr->node_idx, fr->dead_node);
2645 dlm_print_reco_node_status(dlm);
2648 dlm->reco.state |= DLM_RECO_STATE_FINALIZE;
2649 spin_unlock(&dlm->spinlock);
2652 if (!(dlm->reco.state & DLM_RECO_STATE_FINALIZE)) {
2653 mlog(ML_ERROR, "%s: received finalize2 from "
2654 "new master %u for dead node %u, but "
2655 "this node did not have finalize1!\n",
2656 dlm->name, fr->node_idx, fr->dead_node);
2657 dlm_print_reco_node_status(dlm);
2660 dlm->reco.state &= ~DLM_RECO_STATE_FINALIZE;
2661 spin_unlock(&dlm->spinlock);
2662 dlm_reset_recovery(dlm);
2663 dlm_kick_recovery_thread(dlm);
2669 mlog(0, "%s: recovery done, reco master was %u, dead now %u, master now %u\n",
2670 dlm->name, fr->node_idx, dlm->reco.dead_node, dlm->reco.new_master);