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);
98 static int dlm_lockres_master_requery(struct dlm_ctxt *dlm,
99 struct dlm_lock_resource *res,
102 static u64 dlm_get_next_mig_cookie(void);
104 static DEFINE_SPINLOCK(dlm_reco_state_lock);
105 static DEFINE_SPINLOCK(dlm_mig_cookie_lock);
106 static u64 dlm_mig_cookie = 1;
108 static u64 dlm_get_next_mig_cookie(void)
111 spin_lock(&dlm_mig_cookie_lock);
113 if (dlm_mig_cookie == (~0ULL))
117 spin_unlock(&dlm_mig_cookie_lock);
121 static inline void dlm_set_reco_dead_node(struct dlm_ctxt *dlm,
124 assert_spin_locked(&dlm->spinlock);
125 if (dlm->reco.dead_node != dead_node)
126 mlog(0, "%s: changing dead_node from %u to %u\n",
127 dlm->name, dlm->reco.dead_node, dead_node);
128 dlm->reco.dead_node = dead_node;
131 static inline void dlm_set_reco_master(struct dlm_ctxt *dlm,
134 assert_spin_locked(&dlm->spinlock);
135 mlog(0, "%s: changing new_master from %u to %u\n",
136 dlm->name, dlm->reco.new_master, master);
137 dlm->reco.new_master = master;
140 static inline void __dlm_reset_recovery(struct dlm_ctxt *dlm)
142 assert_spin_locked(&dlm->spinlock);
143 clear_bit(dlm->reco.dead_node, dlm->recovery_map);
144 dlm_set_reco_dead_node(dlm, O2NM_INVALID_NODE_NUM);
145 dlm_set_reco_master(dlm, O2NM_INVALID_NODE_NUM);
148 static inline void dlm_reset_recovery(struct dlm_ctxt *dlm)
150 spin_lock(&dlm->spinlock);
151 __dlm_reset_recovery(dlm);
152 spin_unlock(&dlm->spinlock);
155 /* Worker function used during recovery. */
156 void dlm_dispatch_work(struct work_struct *work)
158 struct dlm_ctxt *dlm =
159 container_of(work, struct dlm_ctxt, dispatched_work);
161 struct dlm_work_item *item, *next;
162 dlm_workfunc_t *workfunc;
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_entry(item, &tmp_list, list) {
172 mlog(0, "%s: work thread has %d work items\n", dlm->name, tot);
174 list_for_each_entry_safe(item, next, &tmp_list, list) {
175 workfunc = item->func;
176 list_del_init(&item->list);
178 /* already have ref on dlm to avoid having
179 * it disappear. just double-check. */
180 BUG_ON(item->dlm != dlm);
182 /* this is allowed to sleep and
183 * call network stuff */
184 workfunc(item, item->data);
195 void dlm_kick_recovery_thread(struct dlm_ctxt *dlm)
197 /* wake the recovery thread
198 * this will wake the reco thread in one of three places
199 * 1) sleeping with no recovery happening
200 * 2) sleeping with recovery mastered elsewhere
201 * 3) recovery mastered here, waiting on reco data */
203 wake_up(&dlm->dlm_reco_thread_wq);
206 /* Launch the recovery thread */
207 int dlm_launch_recovery_thread(struct dlm_ctxt *dlm)
209 mlog(0, "starting dlm recovery thread...\n");
211 dlm->dlm_reco_thread_task = kthread_run(dlm_recovery_thread, dlm,
213 if (IS_ERR(dlm->dlm_reco_thread_task)) {
214 mlog_errno(PTR_ERR(dlm->dlm_reco_thread_task));
215 dlm->dlm_reco_thread_task = NULL;
222 void dlm_complete_recovery_thread(struct dlm_ctxt *dlm)
224 if (dlm->dlm_reco_thread_task) {
225 mlog(0, "waiting for dlm recovery thread to exit\n");
226 kthread_stop(dlm->dlm_reco_thread_task);
227 dlm->dlm_reco_thread_task = NULL;
234 * this is lame, but here's how recovery works...
235 * 1) all recovery threads cluster wide will work on recovering
237 * 2) negotiate who will take over all the locks for the dead node.
238 * thats right... ALL the locks.
239 * 3) once a new master is chosen, everyone scans all locks
240 * and moves aside those mastered by the dead guy
241 * 4) each of these locks should be locked until recovery is done
242 * 5) the new master collects up all of secondary lock queue info
243 * one lock at a time, forcing each node to communicate back
245 * 6) each secondary lock queue responds with the full known lock info
246 * 7) once the new master has run all its locks, it sends a ALLDONE!
247 * message to everyone
248 * 8) upon receiving this message, the secondary queue node unlocks
249 * and responds to the ALLDONE
250 * 9) once the new master gets responses from everyone, he unlocks
251 * everything and recovery for this dead node is done
252 *10) go back to 2) while there are still dead nodes
256 static void dlm_print_reco_node_status(struct dlm_ctxt *dlm)
258 struct dlm_reco_node_data *ndata;
259 struct dlm_lock_resource *res;
261 mlog(ML_NOTICE, "%s(%d): recovery info, state=%s, dead=%u, master=%u\n",
262 dlm->name, dlm->dlm_reco_thread_task->pid,
263 dlm->reco.state & DLM_RECO_STATE_ACTIVE ? "ACTIVE" : "inactive",
264 dlm->reco.dead_node, dlm->reco.new_master);
266 list_for_each_entry(ndata, &dlm->reco.node_data, list) {
267 char *st = "unknown";
268 switch (ndata->state) {
269 case DLM_RECO_NODE_DATA_INIT:
272 case DLM_RECO_NODE_DATA_REQUESTING:
275 case DLM_RECO_NODE_DATA_DEAD:
278 case DLM_RECO_NODE_DATA_RECEIVING:
281 case DLM_RECO_NODE_DATA_REQUESTED:
284 case DLM_RECO_NODE_DATA_DONE:
287 case DLM_RECO_NODE_DATA_FINALIZE_SENT:
288 st = "finalize-sent";
294 mlog(ML_NOTICE, "%s: reco state, node %u, state=%s\n",
295 dlm->name, ndata->node_num, st);
297 list_for_each_entry(res, &dlm->reco.resources, recovering) {
298 mlog(ML_NOTICE, "%s: lockres %.*s on recovering list\n",
299 dlm->name, res->lockname.len, res->lockname.name);
303 #define DLM_RECO_THREAD_TIMEOUT_MS (5 * 1000)
305 static int dlm_recovery_thread(void *data)
308 struct dlm_ctxt *dlm = data;
309 unsigned long timeout = msecs_to_jiffies(DLM_RECO_THREAD_TIMEOUT_MS);
311 mlog(0, "dlm thread running for %s...\n", dlm->name);
313 while (!kthread_should_stop()) {
314 if (dlm_joined(dlm)) {
315 status = dlm_do_recovery(dlm);
316 if (status == -EAGAIN) {
317 /* do not sleep, recheck immediately. */
324 wait_event_interruptible_timeout(dlm->dlm_reco_thread_wq,
325 kthread_should_stop(),
329 mlog(0, "quitting DLM recovery thread\n");
333 /* returns true when the recovery master has contacted us */
334 static int dlm_reco_master_ready(struct dlm_ctxt *dlm)
337 spin_lock(&dlm->spinlock);
338 ready = (dlm->reco.new_master != O2NM_INVALID_NODE_NUM);
339 spin_unlock(&dlm->spinlock);
343 /* returns true if node is no longer in the domain
344 * could be dead or just not joined */
345 int dlm_is_node_dead(struct dlm_ctxt *dlm, u8 node)
348 spin_lock(&dlm->spinlock);
349 dead = !test_bit(node, dlm->domain_map);
350 spin_unlock(&dlm->spinlock);
354 /* returns true if node is no longer in the domain
355 * could be dead or just not joined */
356 static int dlm_is_node_recovered(struct dlm_ctxt *dlm, u8 node)
359 spin_lock(&dlm->spinlock);
360 recovered = !test_bit(node, dlm->recovery_map);
361 spin_unlock(&dlm->spinlock);
366 int dlm_wait_for_node_death(struct dlm_ctxt *dlm, u8 node, int timeout)
369 mlog(ML_NOTICE, "%s: waiting %dms for notification of "
370 "death of node %u\n", dlm->name, timeout, node);
371 wait_event_timeout(dlm->dlm_reco_thread_wq,
372 dlm_is_node_dead(dlm, node),
373 msecs_to_jiffies(timeout));
375 mlog(ML_NOTICE, "%s: waiting indefinitely for notification "
376 "of death of node %u\n", dlm->name, node);
377 wait_event(dlm->dlm_reco_thread_wq,
378 dlm_is_node_dead(dlm, node));
380 /* for now, return 0 */
384 int dlm_wait_for_node_recovery(struct dlm_ctxt *dlm, u8 node, int timeout)
387 mlog(0, "%s: waiting %dms for notification of "
388 "recovery of node %u\n", dlm->name, timeout, node);
389 wait_event_timeout(dlm->dlm_reco_thread_wq,
390 dlm_is_node_recovered(dlm, node),
391 msecs_to_jiffies(timeout));
393 mlog(0, "%s: waiting indefinitely for notification "
394 "of recovery of node %u\n", dlm->name, node);
395 wait_event(dlm->dlm_reco_thread_wq,
396 dlm_is_node_recovered(dlm, node));
398 /* for now, return 0 */
402 /* callers of the top-level api calls (dlmlock/dlmunlock) should
403 * block on the dlm->reco.event when recovery is in progress.
404 * the dlm recovery thread will set this state when it begins
405 * recovering a dead node (as the new master or not) and clear
406 * the state and wake as soon as all affected lock resources have
407 * been marked with the RECOVERY flag */
408 static int dlm_in_recovery(struct dlm_ctxt *dlm)
411 spin_lock(&dlm->spinlock);
412 in_recovery = !!(dlm->reco.state & DLM_RECO_STATE_ACTIVE);
413 spin_unlock(&dlm->spinlock);
418 void dlm_wait_for_recovery(struct dlm_ctxt *dlm)
420 if (dlm_in_recovery(dlm)) {
421 mlog(0, "%s: reco thread %d in recovery: "
422 "state=%d, master=%u, dead=%u\n",
423 dlm->name, dlm->dlm_reco_thread_task->pid,
424 dlm->reco.state, dlm->reco.new_master,
425 dlm->reco.dead_node);
427 wait_event(dlm->reco.event, !dlm_in_recovery(dlm));
430 static void dlm_begin_recovery(struct dlm_ctxt *dlm)
432 spin_lock(&dlm->spinlock);
433 BUG_ON(dlm->reco.state & DLM_RECO_STATE_ACTIVE);
434 dlm->reco.state |= DLM_RECO_STATE_ACTIVE;
435 spin_unlock(&dlm->spinlock);
438 static void dlm_end_recovery(struct dlm_ctxt *dlm)
440 spin_lock(&dlm->spinlock);
441 BUG_ON(!(dlm->reco.state & DLM_RECO_STATE_ACTIVE));
442 dlm->reco.state &= ~DLM_RECO_STATE_ACTIVE;
443 spin_unlock(&dlm->spinlock);
444 wake_up(&dlm->reco.event);
447 static int dlm_do_recovery(struct dlm_ctxt *dlm)
452 spin_lock(&dlm->spinlock);
454 /* check to see if the new master has died */
455 if (dlm->reco.new_master != O2NM_INVALID_NODE_NUM &&
456 test_bit(dlm->reco.new_master, dlm->recovery_map)) {
457 mlog(0, "new master %u died while recovering %u!\n",
458 dlm->reco.new_master, dlm->reco.dead_node);
459 /* unset the new_master, leave dead_node */
460 dlm_set_reco_master(dlm, O2NM_INVALID_NODE_NUM);
463 /* select a target to recover */
464 if (dlm->reco.dead_node == O2NM_INVALID_NODE_NUM) {
467 bit = find_next_bit (dlm->recovery_map, O2NM_MAX_NODES+1, 0);
468 if (bit >= O2NM_MAX_NODES || bit < 0)
469 dlm_set_reco_dead_node(dlm, O2NM_INVALID_NODE_NUM);
471 dlm_set_reco_dead_node(dlm, bit);
472 } else if (!test_bit(dlm->reco.dead_node, dlm->recovery_map)) {
474 mlog(ML_ERROR, "dead_node %u no longer in recovery map!\n",
475 dlm->reco.dead_node);
476 dlm_set_reco_dead_node(dlm, O2NM_INVALID_NODE_NUM);
479 if (dlm->reco.dead_node == O2NM_INVALID_NODE_NUM) {
480 // mlog(0, "nothing to recover! sleeping now!\n");
481 spin_unlock(&dlm->spinlock);
482 /* return to main thread loop and sleep. */
485 mlog(0, "%s(%d):recovery thread found node %u in the recovery map!\n",
486 dlm->name, dlm->dlm_reco_thread_task->pid,
487 dlm->reco.dead_node);
488 spin_unlock(&dlm->spinlock);
490 /* take write barrier */
491 /* (stops the list reshuffling thread, proxy ast handling) */
492 dlm_begin_recovery(dlm);
494 if (dlm->reco.new_master == dlm->node_num)
497 if (dlm->reco.new_master == O2NM_INVALID_NODE_NUM) {
498 /* choose a new master, returns 0 if this node
499 * is the master, -EEXIST if it's another node.
500 * this does not return until a new master is chosen
501 * or recovery completes entirely. */
502 ret = dlm_pick_recovery_master(dlm);
504 /* already notified everyone. go. */
507 mlog(0, "another node will master this recovery session.\n");
509 mlog(0, "dlm=%s (%d), new_master=%u, this node=%u, dead_node=%u\n",
510 dlm->name, dlm->dlm_reco_thread_task->pid, dlm->reco.new_master,
511 dlm->node_num, dlm->reco.dead_node);
513 /* it is safe to start everything back up here
514 * because all of the dead node's lock resources
515 * have been marked as in-recovery */
516 dlm_end_recovery(dlm);
518 /* sleep out in main dlm_recovery_thread loop. */
522 mlog(0, "(%d) mastering recovery of %s:%u here(this=%u)!\n",
523 dlm->dlm_reco_thread_task->pid,
524 dlm->name, dlm->reco.dead_node, dlm->node_num);
526 status = dlm_remaster_locks(dlm, dlm->reco.dead_node);
528 /* we should never hit this anymore */
529 mlog(ML_ERROR, "error %d remastering locks for node %u, "
530 "retrying.\n", status, dlm->reco.dead_node);
531 /* yield a bit to allow any final network messages
532 * to get handled on remaining nodes */
535 /* success! see if any other nodes need recovery */
536 mlog(0, "DONE mastering recovery of %s:%u here(this=%u)!\n",
537 dlm->name, dlm->reco.dead_node, dlm->node_num);
538 dlm_reset_recovery(dlm);
540 dlm_end_recovery(dlm);
542 /* continue and look for another dead node */
546 static int dlm_remaster_locks(struct dlm_ctxt *dlm, u8 dead_node)
549 struct dlm_reco_node_data *ndata;
555 /* we have become recovery master. there is no escaping
556 * this, so just keep trying until we get it. */
557 status = dlm_init_recovery_area(dlm, dead_node);
559 mlog(ML_ERROR, "%s: failed to alloc recovery area, "
560 "retrying\n", dlm->name);
563 } while (status != 0);
565 /* safe to access the node data list without a lock, since this
566 * process is the only one to change the list */
567 list_for_each_entry(ndata, &dlm->reco.node_data, list) {
568 BUG_ON(ndata->state != DLM_RECO_NODE_DATA_INIT);
569 ndata->state = DLM_RECO_NODE_DATA_REQUESTING;
571 mlog(0, "requesting lock info from node %u\n",
574 if (ndata->node_num == dlm->node_num) {
575 ndata->state = DLM_RECO_NODE_DATA_DONE;
580 status = dlm_request_all_locks(dlm, ndata->node_num,
584 if (dlm_is_host_down(status)) {
585 /* node died, ignore it for recovery */
587 ndata->state = DLM_RECO_NODE_DATA_DEAD;
588 /* wait for the domain map to catch up
589 * with the network state. */
590 wait_event_timeout(dlm->dlm_reco_thread_wq,
591 dlm_is_node_dead(dlm,
593 msecs_to_jiffies(1000));
594 mlog(0, "waited 1 sec for %u, "
595 "dead? %s\n", ndata->node_num,
596 dlm_is_node_dead(dlm, ndata->node_num) ?
599 /* -ENOMEM on the other node */
600 mlog(0, "%s: node %u returned "
601 "%d during recovery, retrying "
602 "after a short wait\n",
603 dlm->name, ndata->node_num,
608 } while (status != 0);
610 spin_lock(&dlm_reco_state_lock);
611 switch (ndata->state) {
612 case DLM_RECO_NODE_DATA_INIT:
613 case DLM_RECO_NODE_DATA_FINALIZE_SENT:
614 case DLM_RECO_NODE_DATA_REQUESTED:
617 case DLM_RECO_NODE_DATA_DEAD:
618 mlog(0, "node %u died after requesting "
619 "recovery info for node %u\n",
620 ndata->node_num, dead_node);
621 /* fine. don't need this node's info.
622 * continue without it. */
624 case DLM_RECO_NODE_DATA_REQUESTING:
625 ndata->state = DLM_RECO_NODE_DATA_REQUESTED;
626 mlog(0, "now receiving recovery data from "
627 "node %u for dead node %u\n",
628 ndata->node_num, dead_node);
630 case DLM_RECO_NODE_DATA_RECEIVING:
631 mlog(0, "already receiving recovery data from "
632 "node %u for dead node %u\n",
633 ndata->node_num, dead_node);
635 case DLM_RECO_NODE_DATA_DONE:
636 mlog(0, "already DONE receiving recovery data "
637 "from node %u for dead node %u\n",
638 ndata->node_num, dead_node);
641 spin_unlock(&dlm_reco_state_lock);
644 mlog(0, "done requesting all lock info\n");
646 /* nodes should be sending reco data now
647 * just need to wait */
650 /* check all the nodes now to see if we are
651 * done, or if anyone died */
653 spin_lock(&dlm_reco_state_lock);
654 list_for_each_entry(ndata, &dlm->reco.node_data, list) {
655 mlog(0, "checking recovery state of node %u\n",
657 switch (ndata->state) {
658 case DLM_RECO_NODE_DATA_INIT:
659 case DLM_RECO_NODE_DATA_REQUESTING:
660 mlog(ML_ERROR, "bad ndata state for "
661 "node %u: state=%d\n",
662 ndata->node_num, ndata->state);
665 case DLM_RECO_NODE_DATA_DEAD:
666 mlog(0, "node %u died after "
667 "requesting recovery info for "
668 "node %u\n", ndata->node_num,
671 case DLM_RECO_NODE_DATA_RECEIVING:
672 case DLM_RECO_NODE_DATA_REQUESTED:
673 mlog(0, "%s: node %u still in state %s\n",
674 dlm->name, ndata->node_num,
675 ndata->state==DLM_RECO_NODE_DATA_RECEIVING ?
676 "receiving" : "requested");
679 case DLM_RECO_NODE_DATA_DONE:
680 mlog(0, "%s: node %u state is done\n",
681 dlm->name, ndata->node_num);
683 case DLM_RECO_NODE_DATA_FINALIZE_SENT:
684 mlog(0, "%s: node %u state is finalize\n",
685 dlm->name, ndata->node_num);
689 spin_unlock(&dlm_reco_state_lock);
691 mlog(0, "pass #%d, all_nodes_done?: %s\n", ++pass,
692 all_nodes_done?"yes":"no");
693 if (all_nodes_done) {
696 /* all nodes are now in DLM_RECO_NODE_DATA_DONE state
697 * just send a finalize message to everyone and
699 mlog(0, "all nodes are done! send finalize\n");
700 ret = dlm_send_finalize_reco_message(dlm);
704 spin_lock(&dlm->spinlock);
705 dlm_finish_local_lockres_recovery(dlm, dead_node,
707 spin_unlock(&dlm->spinlock);
708 mlog(0, "should be done with recovery!\n");
710 mlog(0, "finishing recovery of %s at %lu, "
711 "dead=%u, this=%u, new=%u\n", dlm->name,
712 jiffies, dlm->reco.dead_node,
713 dlm->node_num, dlm->reco.new_master);
716 /* rescan everything marked dirty along the way */
717 dlm_kick_thread(dlm, NULL);
720 /* wait to be signalled, with periodic timeout
721 * to check for node death */
722 wait_event_interruptible_timeout(dlm->dlm_reco_thread_wq,
723 kthread_should_stop(),
724 msecs_to_jiffies(DLM_RECO_THREAD_TIMEOUT_MS));
729 dlm_destroy_recovery_area(dlm, dead_node);
735 static int dlm_init_recovery_area(struct dlm_ctxt *dlm, u8 dead_node)
738 struct dlm_reco_node_data *ndata;
740 spin_lock(&dlm->spinlock);
741 memcpy(dlm->reco.node_map, dlm->domain_map, sizeof(dlm->domain_map));
742 /* nodes can only be removed (by dying) after dropping
743 * this lock, and death will be trapped later, so this should do */
744 spin_unlock(&dlm->spinlock);
747 num = find_next_bit (dlm->reco.node_map, O2NM_MAX_NODES, num);
748 if (num >= O2NM_MAX_NODES) {
751 BUG_ON(num == dead_node);
753 ndata = kzalloc(sizeof(*ndata), GFP_NOFS);
755 dlm_destroy_recovery_area(dlm, dead_node);
758 ndata->node_num = num;
759 ndata->state = DLM_RECO_NODE_DATA_INIT;
760 spin_lock(&dlm_reco_state_lock);
761 list_add_tail(&ndata->list, &dlm->reco.node_data);
762 spin_unlock(&dlm_reco_state_lock);
769 static void dlm_destroy_recovery_area(struct dlm_ctxt *dlm, u8 dead_node)
771 struct dlm_reco_node_data *ndata, *next;
774 spin_lock(&dlm_reco_state_lock);
775 list_splice_init(&dlm->reco.node_data, &tmplist);
776 spin_unlock(&dlm_reco_state_lock);
778 list_for_each_entry_safe(ndata, next, &tmplist, list) {
779 list_del_init(&ndata->list);
784 static int dlm_request_all_locks(struct dlm_ctxt *dlm, u8 request_from,
787 struct dlm_lock_request lr;
793 mlog(0, "dlm_request_all_locks: dead node is %u, sending request "
794 "to %u\n", dead_node, request_from);
796 memset(&lr, 0, sizeof(lr));
797 lr.node_idx = dlm->node_num;
798 lr.dead_node = dead_node;
802 ret = o2net_send_message(DLM_LOCK_REQUEST_MSG, dlm->key,
803 &lr, sizeof(lr), request_from, NULL);
805 /* negative status is handled by caller */
809 // return from here, then
810 // sleep until all received or error
815 int dlm_request_all_locks_handler(struct o2net_msg *msg, u32 len, void *data,
818 struct dlm_ctxt *dlm = data;
819 struct dlm_lock_request *lr = (struct dlm_lock_request *)msg->buf;
821 struct dlm_work_item *item = NULL;
826 if (lr->dead_node != dlm->reco.dead_node) {
827 mlog(ML_ERROR, "%s: node %u sent dead_node=%u, but local "
828 "dead_node is %u\n", dlm->name, lr->node_idx,
829 lr->dead_node, dlm->reco.dead_node);
830 dlm_print_reco_node_status(dlm);
835 BUG_ON(lr->dead_node != dlm->reco.dead_node);
837 item = kzalloc(sizeof(*item), GFP_NOFS);
843 /* this will get freed by dlm_request_all_locks_worker */
844 buf = (char *) __get_free_page(GFP_NOFS);
851 /* queue up work for dlm_request_all_locks_worker */
852 dlm_grab(dlm); /* get an extra ref for the work item */
853 dlm_init_work_item(dlm, item, dlm_request_all_locks_worker, buf);
854 item->u.ral.reco_master = lr->node_idx;
855 item->u.ral.dead_node = lr->dead_node;
856 spin_lock(&dlm->work_lock);
857 list_add_tail(&item->list, &dlm->work_list);
858 spin_unlock(&dlm->work_lock);
859 queue_work(dlm->dlm_worker, &dlm->dispatched_work);
865 static void dlm_request_all_locks_worker(struct dlm_work_item *item, void *data)
867 struct dlm_migratable_lockres *mres;
868 struct dlm_lock_resource *res;
869 struct dlm_ctxt *dlm;
870 LIST_HEAD(resources);
872 u8 dead_node, reco_master;
873 int skip_all_done = 0;
876 dead_node = item->u.ral.dead_node;
877 reco_master = item->u.ral.reco_master;
878 mres = (struct dlm_migratable_lockres *)data;
880 mlog(0, "%s: recovery worker started, dead=%u, master=%u\n",
881 dlm->name, dead_node, reco_master);
883 if (dead_node != dlm->reco.dead_node ||
884 reco_master != dlm->reco.new_master) {
885 /* worker could have been created before the recovery master
886 * died. if so, do not continue, but do not error. */
887 if (dlm->reco.new_master == O2NM_INVALID_NODE_NUM) {
888 mlog(ML_NOTICE, "%s: will not send recovery state, "
889 "recovery master %u died, thread=(dead=%u,mas=%u)"
890 " current=(dead=%u,mas=%u)\n", dlm->name,
891 reco_master, dead_node, reco_master,
892 dlm->reco.dead_node, dlm->reco.new_master);
894 mlog(ML_NOTICE, "%s: reco state invalid: reco(dead=%u, "
895 "master=%u), request(dead=%u, master=%u)\n",
896 dlm->name, dlm->reco.dead_node,
897 dlm->reco.new_master, dead_node, reco_master);
902 /* lock resources should have already been moved to the
903 * dlm->reco.resources list. now move items from that list
904 * to a temp list if the dead owner matches. note that the
905 * whole cluster recovers only one node at a time, so we
906 * can safely move UNKNOWN lock resources for each recovery
908 dlm_move_reco_locks_to_list(dlm, &resources, dead_node);
910 /* now we can begin blasting lockreses without the dlm lock */
912 /* any errors returned will be due to the new_master dying,
913 * the dlm_reco_thread should detect this */
914 list_for_each_entry(res, &resources, recovering) {
915 ret = dlm_send_one_lockres(dlm, res, mres, reco_master,
918 mlog(ML_ERROR, "%s: node %u went down while sending "
919 "recovery state for dead node %u, ret=%d\n", dlm->name,
920 reco_master, dead_node, ret);
926 /* move the resources back to the list */
927 spin_lock(&dlm->spinlock);
928 list_splice_init(&resources, &dlm->reco.resources);
929 spin_unlock(&dlm->spinlock);
931 if (!skip_all_done) {
932 ret = dlm_send_all_done_msg(dlm, dead_node, reco_master);
934 mlog(ML_ERROR, "%s: node %u went down while sending "
935 "recovery all-done for dead node %u, ret=%d\n",
936 dlm->name, reco_master, dead_node, ret);
940 free_page((unsigned long)data);
944 static int dlm_send_all_done_msg(struct dlm_ctxt *dlm, u8 dead_node, u8 send_to)
947 struct dlm_reco_data_done done_msg;
949 memset(&done_msg, 0, sizeof(done_msg));
950 done_msg.node_idx = dlm->node_num;
951 done_msg.dead_node = dead_node;
952 mlog(0, "sending DATA DONE message to %u, "
953 "my node=%u, dead node=%u\n", send_to, done_msg.node_idx,
956 ret = o2net_send_message(DLM_RECO_DATA_DONE_MSG, dlm->key, &done_msg,
957 sizeof(done_msg), send_to, &tmpret);
959 if (!dlm_is_host_down(ret)) {
961 mlog(ML_ERROR, "%s: unknown error sending data-done "
962 "to %u\n", dlm->name, send_to);
971 int dlm_reco_data_done_handler(struct o2net_msg *msg, u32 len, void *data,
974 struct dlm_ctxt *dlm = data;
975 struct dlm_reco_data_done *done = (struct dlm_reco_data_done *)msg->buf;
976 struct dlm_reco_node_data *ndata = NULL;
982 mlog(0, "got DATA DONE: dead_node=%u, reco.dead_node=%u, "
983 "node_idx=%u, this node=%u\n", done->dead_node,
984 dlm->reco.dead_node, done->node_idx, dlm->node_num);
986 mlog_bug_on_msg((done->dead_node != dlm->reco.dead_node),
987 "Got DATA DONE: dead_node=%u, reco.dead_node=%u, "
988 "node_idx=%u, this node=%u\n", done->dead_node,
989 dlm->reco.dead_node, done->node_idx, dlm->node_num);
991 spin_lock(&dlm_reco_state_lock);
992 list_for_each_entry(ndata, &dlm->reco.node_data, list) {
993 if (ndata->node_num != done->node_idx)
996 switch (ndata->state) {
997 /* should have moved beyond INIT but not to FINALIZE yet */
998 case DLM_RECO_NODE_DATA_INIT:
999 case DLM_RECO_NODE_DATA_DEAD:
1000 case DLM_RECO_NODE_DATA_FINALIZE_SENT:
1001 mlog(ML_ERROR, "bad ndata state for node %u:"
1002 " state=%d\n", ndata->node_num,
1006 /* these states are possible at this point, anywhere along
1007 * the line of recovery */
1008 case DLM_RECO_NODE_DATA_DONE:
1009 case DLM_RECO_NODE_DATA_RECEIVING:
1010 case DLM_RECO_NODE_DATA_REQUESTED:
1011 case DLM_RECO_NODE_DATA_REQUESTING:
1012 mlog(0, "node %u is DONE sending "
1016 ndata->state = DLM_RECO_NODE_DATA_DONE;
1021 spin_unlock(&dlm_reco_state_lock);
1023 /* wake the recovery thread, some node is done */
1025 dlm_kick_recovery_thread(dlm);
1028 mlog(ML_ERROR, "failed to find recovery node data for node "
1029 "%u\n", done->node_idx);
1032 mlog(0, "leaving reco data done handler, ret=%d\n", ret);
1036 static void dlm_move_reco_locks_to_list(struct dlm_ctxt *dlm,
1037 struct list_head *list,
1040 struct dlm_lock_resource *res, *next;
1041 struct dlm_lock *lock;
1043 spin_lock(&dlm->spinlock);
1044 list_for_each_entry_safe(res, next, &dlm->reco.resources, recovering) {
1045 /* always prune any $RECOVERY entries for dead nodes,
1046 * otherwise hangs can occur during later recovery */
1047 if (dlm_is_recovery_lock(res->lockname.name,
1048 res->lockname.len)) {
1049 spin_lock(&res->spinlock);
1050 list_for_each_entry(lock, &res->granted, list) {
1051 if (lock->ml.node == dead_node) {
1052 mlog(0, "AHA! there was "
1053 "a $RECOVERY lock for dead "
1055 dead_node, dlm->name);
1056 list_del_init(&lock->list);
1061 spin_unlock(&res->spinlock);
1065 if (res->owner == dead_node) {
1066 mlog(0, "found lockres owned by dead node while "
1067 "doing recovery for node %u. sending it.\n",
1069 list_move_tail(&res->recovering, list);
1070 } else if (res->owner == DLM_LOCK_RES_OWNER_UNKNOWN) {
1071 mlog(0, "found UNKNOWN owner while doing recovery "
1072 "for node %u. sending it.\n", dead_node);
1073 list_move_tail(&res->recovering, list);
1076 spin_unlock(&dlm->spinlock);
1079 static inline int dlm_num_locks_in_lockres(struct dlm_lock_resource *res)
1081 int total_locks = 0;
1082 struct list_head *iter, *queue = &res->granted;
1085 for (i=0; i<3; i++) {
1086 list_for_each(iter, queue)
1094 static int dlm_send_mig_lockres_msg(struct dlm_ctxt *dlm,
1095 struct dlm_migratable_lockres *mres,
1097 struct dlm_lock_resource *res,
1100 u64 mig_cookie = be64_to_cpu(mres->mig_cookie);
1101 int mres_total_locks = be32_to_cpu(mres->total_locks);
1102 int sz, ret = 0, status = 0;
1103 u8 orig_flags = mres->flags,
1104 orig_master = mres->master;
1106 BUG_ON(mres->num_locks > DLM_MAX_MIGRATABLE_LOCKS);
1107 if (!mres->num_locks)
1110 sz = sizeof(struct dlm_migratable_lockres) +
1111 (mres->num_locks * sizeof(struct dlm_migratable_lock));
1113 /* add an all-done flag if we reached the last lock */
1114 orig_flags = mres->flags;
1115 BUG_ON(total_locks > mres_total_locks);
1116 if (total_locks == mres_total_locks)
1117 mres->flags |= DLM_MRES_ALL_DONE;
1119 mlog(0, "%s:%.*s: sending mig lockres (%s) to %u\n",
1120 dlm->name, res->lockname.len, res->lockname.name,
1121 orig_flags & DLM_MRES_MIGRATION ? "migrate" : "recovery",
1125 ret = o2net_send_message(DLM_MIG_LOCKRES_MSG, dlm->key, mres,
1126 sz, send_to, &status);
1128 /* XXX: negative status is not handled.
1129 * this will end up killing this node. */
1132 /* might get an -ENOMEM back here */
1137 if (ret == -EFAULT) {
1138 mlog(ML_ERROR, "node %u told me to kill "
1139 "myself!\n", send_to);
1145 /* zero and reinit the message buffer */
1146 dlm_init_migratable_lockres(mres, res->lockname.name,
1147 res->lockname.len, mres_total_locks,
1148 mig_cookie, orig_flags, orig_master);
1152 static void dlm_init_migratable_lockres(struct dlm_migratable_lockres *mres,
1153 const char *lockname, int namelen,
1154 int total_locks, u64 cookie,
1155 u8 flags, u8 master)
1157 /* mres here is one full page */
1159 mres->lockname_len = namelen;
1160 memcpy(mres->lockname, lockname, namelen);
1161 mres->num_locks = 0;
1162 mres->total_locks = cpu_to_be32(total_locks);
1163 mres->mig_cookie = cpu_to_be64(cookie);
1164 mres->flags = flags;
1165 mres->master = master;
1169 /* returns 1 if this lock fills the network structure,
1171 static int dlm_add_lock_to_array(struct dlm_lock *lock,
1172 struct dlm_migratable_lockres *mres, int queue)
1174 struct dlm_migratable_lock *ml;
1175 int lock_num = mres->num_locks;
1177 ml = &(mres->ml[lock_num]);
1178 ml->cookie = lock->ml.cookie;
1179 ml->type = lock->ml.type;
1180 ml->convert_type = lock->ml.convert_type;
1181 ml->highest_blocked = lock->ml.highest_blocked;
1184 ml->flags = lock->lksb->flags;
1185 /* send our current lvb */
1186 if (ml->type == LKM_EXMODE ||
1187 ml->type == LKM_PRMODE) {
1188 /* if it is already set, this had better be a PR
1189 * and it has to match */
1190 if (!dlm_lvb_is_empty(mres->lvb) &&
1191 (ml->type == LKM_EXMODE ||
1192 memcmp(mres->lvb, lock->lksb->lvb, DLM_LVB_LEN))) {
1193 mlog(ML_ERROR, "mismatched lvbs!\n");
1194 __dlm_print_one_lock_resource(lock->lockres);
1197 memcpy(mres->lvb, lock->lksb->lvb, DLM_LVB_LEN);
1200 ml->node = lock->ml.node;
1202 /* we reached the max, send this network message */
1203 if (mres->num_locks == DLM_MAX_MIGRATABLE_LOCKS)
1208 static void dlm_add_dummy_lock(struct dlm_ctxt *dlm,
1209 struct dlm_migratable_lockres *mres)
1211 struct dlm_lock dummy;
1212 memset(&dummy, 0, sizeof(dummy));
1213 dummy.ml.cookie = 0;
1214 dummy.ml.type = LKM_IVMODE;
1215 dummy.ml.convert_type = LKM_IVMODE;
1216 dummy.ml.highest_blocked = LKM_IVMODE;
1218 dummy.ml.node = dlm->node_num;
1219 dlm_add_lock_to_array(&dummy, mres, DLM_BLOCKED_LIST);
1222 static inline int dlm_is_dummy_lock(struct dlm_ctxt *dlm,
1223 struct dlm_migratable_lock *ml,
1226 if (unlikely(ml->cookie == 0 &&
1227 ml->type == LKM_IVMODE &&
1228 ml->convert_type == LKM_IVMODE &&
1229 ml->highest_blocked == LKM_IVMODE &&
1230 ml->list == DLM_BLOCKED_LIST)) {
1231 *nodenum = ml->node;
1237 int dlm_send_one_lockres(struct dlm_ctxt *dlm, struct dlm_lock_resource *res,
1238 struct dlm_migratable_lockres *mres,
1239 u8 send_to, u8 flags)
1241 struct list_head *queue;
1244 struct dlm_lock *lock;
1247 BUG_ON(!(flags & (DLM_MRES_RECOVERY|DLM_MRES_MIGRATION)));
1249 mlog(0, "sending to %u\n", send_to);
1251 total_locks = dlm_num_locks_in_lockres(res);
1252 if (total_locks > DLM_MAX_MIGRATABLE_LOCKS) {
1253 /* rare, but possible */
1254 mlog(0, "argh. lockres has %d locks. this will "
1255 "require more than one network packet to "
1256 "migrate\n", total_locks);
1257 mig_cookie = dlm_get_next_mig_cookie();
1260 dlm_init_migratable_lockres(mres, res->lockname.name,
1261 res->lockname.len, total_locks,
1262 mig_cookie, flags, res->owner);
1265 for (i=DLM_GRANTED_LIST; i<=DLM_BLOCKED_LIST; i++) {
1266 queue = dlm_list_idx_to_ptr(res, i);
1267 list_for_each_entry(lock, queue, list) {
1268 /* add another lock. */
1270 if (!dlm_add_lock_to_array(lock, mres, i))
1273 /* this filled the lock message,
1274 * we must send it immediately. */
1275 ret = dlm_send_mig_lockres_msg(dlm, mres, send_to,
1281 if (total_locks == 0) {
1282 /* send a dummy lock to indicate a mastery reference only */
1283 mlog(0, "%s:%.*s: sending dummy lock to %u, %s\n",
1284 dlm->name, res->lockname.len, res->lockname.name,
1285 send_to, flags & DLM_MRES_RECOVERY ? "recovery" :
1287 dlm_add_dummy_lock(dlm, mres);
1289 /* flush any remaining locks */
1290 ret = dlm_send_mig_lockres_msg(dlm, mres, send_to, res, total_locks);
1296 mlog(ML_ERROR, "%s: dlm_send_mig_lockres_msg returned %d\n",
1298 if (!dlm_is_host_down(ret))
1300 mlog(0, "%s: node %u went down while sending %s "
1301 "lockres %.*s\n", dlm->name, send_to,
1302 flags & DLM_MRES_RECOVERY ? "recovery" : "migration",
1303 res->lockname.len, res->lockname.name);
1310 * this message will contain no more than one page worth of
1311 * recovery data, and it will work on only one lockres.
1312 * there may be many locks in this page, and we may need to wait
1313 * for additional packets to complete all the locks (rare, but
1317 * NOTE: the allocation error cases here are scary
1318 * we really cannot afford to fail an alloc in recovery
1319 * do we spin? returning an error only delays the problem really
1322 int dlm_mig_lockres_handler(struct o2net_msg *msg, u32 len, void *data,
1325 struct dlm_ctxt *dlm = data;
1326 struct dlm_migratable_lockres *mres =
1327 (struct dlm_migratable_lockres *)msg->buf;
1331 struct dlm_work_item *item = NULL;
1332 struct dlm_lock_resource *res = NULL;
1337 BUG_ON(!(mres->flags & (DLM_MRES_RECOVERY|DLM_MRES_MIGRATION)));
1339 real_master = mres->master;
1340 if (real_master == DLM_LOCK_RES_OWNER_UNKNOWN) {
1341 /* cannot migrate a lockres with no master */
1342 BUG_ON(!(mres->flags & DLM_MRES_RECOVERY));
1345 mlog(0, "%s message received from node %u\n",
1346 (mres->flags & DLM_MRES_RECOVERY) ?
1347 "recovery" : "migration", mres->master);
1348 if (mres->flags & DLM_MRES_ALL_DONE)
1349 mlog(0, "all done flag. all lockres data received!\n");
1352 buf = kmalloc(be16_to_cpu(msg->data_len), GFP_NOFS);
1353 item = kzalloc(sizeof(*item), GFP_NOFS);
1357 /* lookup the lock to see if we have a secondary queue for this
1358 * already... just add the locks in and this will have its owner
1359 * and RECOVERY flag changed when it completes. */
1360 res = dlm_lookup_lockres(dlm, mres->lockname, mres->lockname_len);
1362 /* this will get a ref on res */
1363 /* mark it as recovering/migrating and hash it */
1364 spin_lock(&res->spinlock);
1365 if (mres->flags & DLM_MRES_RECOVERY) {
1366 res->state |= DLM_LOCK_RES_RECOVERING;
1368 if (res->state & DLM_LOCK_RES_MIGRATING) {
1369 /* this is at least the second
1370 * lockres message */
1371 mlog(0, "lock %.*s is already migrating\n",
1374 } else if (res->state & DLM_LOCK_RES_RECOVERING) {
1375 /* caller should BUG */
1376 mlog(ML_ERROR, "node is attempting to migrate "
1377 "lock %.*s, but marked as recovering!\n",
1378 mres->lockname_len, mres->lockname);
1380 spin_unlock(&res->spinlock);
1383 res->state |= DLM_LOCK_RES_MIGRATING;
1385 spin_unlock(&res->spinlock);
1387 /* need to allocate, just like if it was
1388 * mastered here normally */
1389 res = dlm_new_lockres(dlm, mres->lockname, mres->lockname_len);
1393 /* to match the ref that we would have gotten if
1394 * dlm_lookup_lockres had succeeded */
1395 dlm_lockres_get(res);
1397 /* mark it as recovering/migrating and hash it */
1398 if (mres->flags & DLM_MRES_RECOVERY)
1399 res->state |= DLM_LOCK_RES_RECOVERING;
1401 res->state |= DLM_LOCK_RES_MIGRATING;
1403 spin_lock(&dlm->spinlock);
1404 __dlm_insert_lockres(dlm, res);
1405 spin_unlock(&dlm->spinlock);
1407 /* now that the new lockres is inserted,
1408 * make it usable by other processes */
1409 spin_lock(&res->spinlock);
1410 res->state &= ~DLM_LOCK_RES_IN_PROGRESS;
1411 spin_unlock(&res->spinlock);
1414 /* add an extra ref for just-allocated lockres
1415 * otherwise the lockres will be purged immediately */
1416 dlm_lockres_get(res);
1419 /* at this point we have allocated everything we need,
1420 * and we have a hashed lockres with an extra ref and
1421 * the proper res->state flags. */
1423 spin_lock(&res->spinlock);
1424 /* drop this either when master requery finds a different master
1425 * or when a lock is added by the recovery worker */
1426 dlm_lockres_grab_inflight_ref(dlm, res);
1427 if (mres->master == DLM_LOCK_RES_OWNER_UNKNOWN) {
1428 /* migration cannot have an unknown master */
1429 BUG_ON(!(mres->flags & DLM_MRES_RECOVERY));
1430 mlog(0, "recovery has passed me a lockres with an "
1431 "unknown owner.. will need to requery: "
1432 "%.*s\n", mres->lockname_len, mres->lockname);
1434 /* take a reference now to pin the lockres, drop it
1435 * when locks are added in the worker */
1436 dlm_change_lockres_owner(dlm, res, dlm->node_num);
1438 spin_unlock(&res->spinlock);
1440 /* queue up work for dlm_mig_lockres_worker */
1441 dlm_grab(dlm); /* get an extra ref for the work item */
1442 memcpy(buf, msg->buf, be16_to_cpu(msg->data_len)); /* copy the whole message */
1443 dlm_init_work_item(dlm, item, dlm_mig_lockres_worker, buf);
1444 item->u.ml.lockres = res; /* already have a ref */
1445 item->u.ml.real_master = real_master;
1446 spin_lock(&dlm->work_lock);
1447 list_add_tail(&item->list, &dlm->work_list);
1448 spin_unlock(&dlm->work_lock);
1449 queue_work(dlm->dlm_worker, &dlm->dispatched_work);
1465 static void dlm_mig_lockres_worker(struct dlm_work_item *item, void *data)
1467 struct dlm_ctxt *dlm = data;
1468 struct dlm_migratable_lockres *mres;
1470 struct dlm_lock_resource *res;
1474 mres = (struct dlm_migratable_lockres *)data;
1476 res = item->u.ml.lockres;
1477 real_master = item->u.ml.real_master;
1479 if (real_master == DLM_LOCK_RES_OWNER_UNKNOWN) {
1480 /* this case is super-rare. only occurs if
1481 * node death happens during migration. */
1483 ret = dlm_lockres_master_requery(dlm, res, &real_master);
1485 mlog(0, "dlm_lockres_master_requery ret=%d\n",
1489 if (real_master == DLM_LOCK_RES_OWNER_UNKNOWN) {
1490 mlog(0, "lockres %.*s not claimed. "
1491 "this node will take it.\n",
1492 res->lockname.len, res->lockname.name);
1494 spin_lock(&res->spinlock);
1495 dlm_lockres_drop_inflight_ref(dlm, res);
1496 spin_unlock(&res->spinlock);
1497 mlog(0, "master needs to respond to sender "
1498 "that node %u still owns %.*s\n",
1499 real_master, res->lockname.len,
1500 res->lockname.name);
1501 /* cannot touch this lockres */
1506 ret = dlm_process_recovery_data(dlm, res, mres);
1508 mlog(0, "dlm_process_recovery_data returned %d\n", ret);
1510 mlog(0, "dlm_process_recovery_data succeeded\n");
1512 if ((mres->flags & (DLM_MRES_MIGRATION|DLM_MRES_ALL_DONE)) ==
1513 (DLM_MRES_MIGRATION|DLM_MRES_ALL_DONE)) {
1514 ret = dlm_finish_migration(dlm, res, mres->master);
1526 static int dlm_lockres_master_requery(struct dlm_ctxt *dlm,
1527 struct dlm_lock_resource *res,
1530 struct dlm_node_iter iter;
1534 *real_master = DLM_LOCK_RES_OWNER_UNKNOWN;
1536 /* we only reach here if one of the two nodes in a
1537 * migration died while the migration was in progress.
1538 * at this point we need to requery the master. we
1539 * know that the new_master got as far as creating
1540 * an mle on at least one node, but we do not know
1541 * if any nodes had actually cleared the mle and set
1542 * the master to the new_master. the old master
1543 * is supposed to set the owner to UNKNOWN in the
1544 * event of a new_master death, so the only possible
1545 * responses that we can get from nodes here are
1546 * that the master is new_master, or that the master
1548 * if all nodes come back with UNKNOWN then we know
1549 * the lock needs remastering here.
1550 * if any node comes back with a valid master, check
1551 * to see if that master is the one that we are
1552 * recovering. if so, then the new_master died and
1553 * we need to remaster this lock. if not, then the
1554 * new_master survived and that node will respond to
1555 * other nodes about the owner.
1556 * if there is an owner, this node needs to dump this
1557 * lockres and alert the sender that this lockres
1559 spin_lock(&dlm->spinlock);
1560 dlm_node_iter_init(dlm->domain_map, &iter);
1561 spin_unlock(&dlm->spinlock);
1563 while ((nodenum = dlm_node_iter_next(&iter)) >= 0) {
1564 /* do not send to self */
1565 if (nodenum == dlm->node_num)
1567 ret = dlm_do_master_requery(dlm, res, nodenum, real_master);
1570 if (!dlm_is_host_down(ret))
1572 /* host is down, so answer for that node would be
1573 * DLM_LOCK_RES_OWNER_UNKNOWN. continue. */
1575 if (*real_master != DLM_LOCK_RES_OWNER_UNKNOWN) {
1576 mlog(0, "lock master is %u\n", *real_master);
1584 int dlm_do_master_requery(struct dlm_ctxt *dlm, struct dlm_lock_resource *res,
1585 u8 nodenum, u8 *real_master)
1588 struct dlm_master_requery req;
1589 int status = DLM_LOCK_RES_OWNER_UNKNOWN;
1591 memset(&req, 0, sizeof(req));
1592 req.node_idx = dlm->node_num;
1593 req.namelen = res->lockname.len;
1594 memcpy(req.name, res->lockname.name, res->lockname.len);
1596 ret = o2net_send_message(DLM_MASTER_REQUERY_MSG, dlm->key,
1597 &req, sizeof(req), nodenum, &status);
1598 /* XXX: negative status not handled properly here. */
1603 BUG_ON(status > DLM_LOCK_RES_OWNER_UNKNOWN);
1604 *real_master = (u8) (status & 0xff);
1605 mlog(0, "node %u responded to master requery with %u\n",
1606 nodenum, *real_master);
1613 /* this function cannot error, so unless the sending
1614 * or receiving of the message failed, the owner can
1616 int dlm_master_requery_handler(struct o2net_msg *msg, u32 len, void *data,
1619 struct dlm_ctxt *dlm = data;
1620 struct dlm_master_requery *req = (struct dlm_master_requery *)msg->buf;
1621 struct dlm_lock_resource *res = NULL;
1623 int master = DLM_LOCK_RES_OWNER_UNKNOWN;
1624 u32 flags = DLM_ASSERT_MASTER_REQUERY;
1626 if (!dlm_grab(dlm)) {
1627 /* since the domain has gone away on this
1628 * node, the proper response is UNKNOWN */
1632 hash = dlm_lockid_hash(req->name, req->namelen);
1634 spin_lock(&dlm->spinlock);
1635 res = __dlm_lookup_lockres(dlm, req->name, req->namelen, hash);
1637 spin_lock(&res->spinlock);
1638 master = res->owner;
1639 if (master == dlm->node_num) {
1640 int ret = dlm_dispatch_assert_master(dlm, res,
1643 mlog_errno(-ENOMEM);
1648 spin_unlock(&res->spinlock);
1650 spin_unlock(&dlm->spinlock);
1656 static inline struct list_head *
1657 dlm_list_num_to_pointer(struct dlm_lock_resource *res, int list_num)
1659 struct list_head *ret;
1660 BUG_ON(list_num < 0);
1661 BUG_ON(list_num > 2);
1662 ret = &(res->granted);
1666 /* TODO: do ast flush business
1667 * TODO: do MIGRATING and RECOVERING spinning
1671 * NOTE about in-flight requests during migration:
1673 * Before attempting the migrate, the master has marked the lockres as
1674 * MIGRATING and then flushed all of its pending ASTS. So any in-flight
1675 * requests either got queued before the MIGRATING flag got set, in which
1676 * case the lock data will reflect the change and a return message is on
1677 * the way, or the request failed to get in before MIGRATING got set. In
1678 * this case, the caller will be told to spin and wait for the MIGRATING
1679 * flag to be dropped, then recheck the master.
1680 * This holds true for the convert, cancel and unlock cases, and since lvb
1681 * updates are tied to these same messages, it applies to lvb updates as
1682 * well. For the lock case, there is no way a lock can be on the master
1683 * queue and not be on the secondary queue since the lock is always added
1684 * locally first. This means that the new target node will never be sent
1685 * a lock that he doesn't already have on the list.
1686 * In total, this means that the local lock is correct and should not be
1687 * updated to match the one sent by the master. Any messages sent back
1688 * from the master before the MIGRATING flag will bring the lock properly
1689 * up-to-date, and the change will be ordered properly for the waiter.
1690 * We will *not* attempt to modify the lock underneath the waiter.
1693 static int dlm_process_recovery_data(struct dlm_ctxt *dlm,
1694 struct dlm_lock_resource *res,
1695 struct dlm_migratable_lockres *mres)
1697 struct dlm_migratable_lock *ml;
1698 struct list_head *queue;
1699 struct list_head *tmpq = NULL;
1700 struct dlm_lock *newlock = NULL;
1701 struct dlm_lockstatus *lksb = NULL;
1704 struct dlm_lock *lock = NULL;
1705 u8 from = O2NM_MAX_NODES;
1706 unsigned int added = 0;
1708 mlog(0, "running %d locks for this lockres\n", mres->num_locks);
1709 for (i=0; i<mres->num_locks; i++) {
1710 ml = &(mres->ml[i]);
1712 if (dlm_is_dummy_lock(dlm, ml, &from)) {
1713 /* placeholder, just need to set the refmap bit */
1714 BUG_ON(mres->num_locks != 1);
1715 mlog(0, "%s:%.*s: dummy lock for %u\n",
1716 dlm->name, mres->lockname_len, mres->lockname,
1718 spin_lock(&res->spinlock);
1719 dlm_lockres_set_refmap_bit(from, res);
1720 spin_unlock(&res->spinlock);
1724 BUG_ON(ml->highest_blocked != LKM_IVMODE);
1728 queue = dlm_list_num_to_pointer(res, ml->list);
1731 /* if the lock is for the local node it needs to
1732 * be moved to the proper location within the queue.
1733 * do not allocate a new lock structure. */
1734 if (ml->node == dlm->node_num) {
1735 /* MIGRATION ONLY! */
1736 BUG_ON(!(mres->flags & DLM_MRES_MIGRATION));
1738 spin_lock(&res->spinlock);
1739 for (j = DLM_GRANTED_LIST; j <= DLM_BLOCKED_LIST; j++) {
1740 tmpq = dlm_list_idx_to_ptr(res, j);
1741 list_for_each_entry(lock, tmpq, list) {
1742 if (lock->ml.cookie != ml->cookie)
1751 /* lock is always created locally first, and
1752 * destroyed locally last. it must be on the list */
1754 __be64 c = ml->cookie;
1755 mlog(ML_ERROR, "could not find local lock "
1756 "with cookie %u:%llu!\n",
1757 dlm_get_lock_cookie_node(be64_to_cpu(c)),
1758 dlm_get_lock_cookie_seq(be64_to_cpu(c)));
1759 __dlm_print_one_lock_resource(res);
1762 BUG_ON(lock->ml.node != ml->node);
1764 if (tmpq != queue) {
1765 mlog(0, "lock was on %u instead of %u for %.*s\n",
1766 j, ml->list, res->lockname.len, res->lockname.name);
1767 spin_unlock(&res->spinlock);
1771 /* see NOTE above about why we do not update
1772 * to match the master here */
1774 /* move the lock to its proper place */
1775 /* do not alter lock refcount. switching lists. */
1776 list_move_tail(&lock->list, queue);
1777 spin_unlock(&res->spinlock);
1780 mlog(0, "just reordered a local lock!\n");
1784 /* lock is for another node. */
1785 newlock = dlm_new_lock(ml->type, ml->node,
1786 be64_to_cpu(ml->cookie), NULL);
1791 lksb = newlock->lksb;
1792 dlm_lock_attach_lockres(newlock, res);
1794 if (ml->convert_type != LKM_IVMODE) {
1795 BUG_ON(queue != &res->converting);
1796 newlock->ml.convert_type = ml->convert_type;
1798 lksb->flags |= (ml->flags &
1799 (DLM_LKSB_PUT_LVB|DLM_LKSB_GET_LVB));
1801 if (ml->type == LKM_NLMODE)
1804 if (!dlm_lvb_is_empty(mres->lvb)) {
1805 if (lksb->flags & DLM_LKSB_PUT_LVB) {
1806 /* other node was trying to update
1807 * lvb when node died. recreate the
1808 * lksb with the updated lvb. */
1809 memcpy(lksb->lvb, mres->lvb, DLM_LVB_LEN);
1810 /* the lock resource lvb update must happen
1811 * NOW, before the spinlock is dropped.
1812 * we no longer wait for the AST to update
1814 memcpy(res->lvb, mres->lvb, DLM_LVB_LEN);
1816 /* otherwise, the node is sending its
1817 * most recent valid lvb info */
1818 BUG_ON(ml->type != LKM_EXMODE &&
1819 ml->type != LKM_PRMODE);
1820 if (!dlm_lvb_is_empty(res->lvb) &&
1821 (ml->type == LKM_EXMODE ||
1822 memcmp(res->lvb, mres->lvb, DLM_LVB_LEN))) {
1824 mlog(ML_ERROR, "%s:%.*s: received bad "
1825 "lvb! type=%d\n", dlm->name,
1827 res->lockname.name, ml->type);
1828 printk("lockres lvb=[");
1829 for (i=0; i<DLM_LVB_LEN; i++)
1830 printk("%02x", res->lvb[i]);
1831 printk("]\nmigrated lvb=[");
1832 for (i=0; i<DLM_LVB_LEN; i++)
1833 printk("%02x", mres->lvb[i]);
1835 dlm_print_one_lock_resource(res);
1838 memcpy(res->lvb, mres->lvb, DLM_LVB_LEN);
1844 * wrt lock queue ordering and recovery:
1845 * 1. order of locks on granted queue is
1847 * 2. order of locks on converting queue is
1848 * LOST with the node death. sorry charlie.
1849 * 3. order of locks on the blocked queue is
1851 * order of locks does not affect integrity, it
1852 * just means that a lock request may get pushed
1853 * back in line as a result of the node death.
1854 * also note that for a given node the lock order
1855 * for its secondary queue locks is preserved
1856 * relative to each other, but clearly *not*
1857 * preserved relative to locks from other nodes.
1860 spin_lock(&res->spinlock);
1861 list_for_each_entry(lock, queue, list) {
1862 if (lock->ml.cookie == ml->cookie) {
1863 __be64 c = lock->ml.cookie;
1864 mlog(ML_ERROR, "%s:%.*s: %u:%llu: lock already "
1865 "exists on this lockres!\n", dlm->name,
1866 res->lockname.len, res->lockname.name,
1867 dlm_get_lock_cookie_node(be64_to_cpu(c)),
1868 dlm_get_lock_cookie_seq(be64_to_cpu(c)));
1870 mlog(ML_NOTICE, "sent lock: type=%d, conv=%d, "
1871 "node=%u, cookie=%u:%llu, queue=%d\n",
1872 ml->type, ml->convert_type, ml->node,
1873 dlm_get_lock_cookie_node(be64_to_cpu(ml->cookie)),
1874 dlm_get_lock_cookie_seq(be64_to_cpu(ml->cookie)),
1877 __dlm_print_one_lock_resource(res);
1883 dlm_lock_get(newlock);
1884 list_add_tail(&newlock->list, queue);
1885 mlog(0, "%s:%.*s: added lock for node %u, "
1886 "setting refmap bit\n", dlm->name,
1887 res->lockname.len, res->lockname.name, ml->node);
1888 dlm_lockres_set_refmap_bit(ml->node, res);
1891 spin_unlock(&res->spinlock);
1893 mlog(0, "done running all the locks\n");
1896 /* balance the ref taken when the work was queued */
1897 spin_lock(&res->spinlock);
1898 dlm_lockres_drop_inflight_ref(dlm, res);
1899 spin_unlock(&res->spinlock);
1904 dlm_lock_put(newlock);
1911 void dlm_move_lockres_to_recovery_list(struct dlm_ctxt *dlm,
1912 struct dlm_lock_resource *res)
1915 struct list_head *queue;
1916 struct dlm_lock *lock, *next;
1918 res->state |= DLM_LOCK_RES_RECOVERING;
1919 if (!list_empty(&res->recovering)) {
1921 "Recovering res %s:%.*s, is already on recovery list!\n",
1922 dlm->name, res->lockname.len, res->lockname.name);
1923 list_del_init(&res->recovering);
1925 /* We need to hold a reference while on the recovery list */
1926 dlm_lockres_get(res);
1927 list_add_tail(&res->recovering, &dlm->reco.resources);
1929 /* find any pending locks and put them back on proper list */
1930 for (i=DLM_BLOCKED_LIST; i>=DLM_GRANTED_LIST; i--) {
1931 queue = dlm_list_idx_to_ptr(res, i);
1932 list_for_each_entry_safe(lock, next, queue, list) {
1934 if (lock->convert_pending) {
1935 /* move converting lock back to granted */
1936 BUG_ON(i != DLM_CONVERTING_LIST);
1937 mlog(0, "node died with convert pending "
1938 "on %.*s. move back to granted list.\n",
1939 res->lockname.len, res->lockname.name);
1940 dlm_revert_pending_convert(res, lock);
1941 lock->convert_pending = 0;
1942 } else if (lock->lock_pending) {
1943 /* remove pending lock requests completely */
1944 BUG_ON(i != DLM_BLOCKED_LIST);
1945 mlog(0, "node died with lock pending "
1946 "on %.*s. remove from blocked list and skip.\n",
1947 res->lockname.len, res->lockname.name);
1948 /* lock will be floating until ref in
1949 * dlmlock_remote is freed after the network
1950 * call returns. ok for it to not be on any
1951 * list since no ast can be called
1952 * (the master is dead). */
1953 dlm_revert_pending_lock(res, lock);
1954 lock->lock_pending = 0;
1955 } else if (lock->unlock_pending) {
1956 /* if an unlock was in progress, treat as
1957 * if this had completed successfully
1958 * before sending this lock state to the
1959 * new master. note that the dlm_unlock
1960 * call is still responsible for calling
1961 * the unlockast. that will happen after
1962 * the network call times out. for now,
1963 * just move lists to prepare the new
1964 * recovery master. */
1965 BUG_ON(i != DLM_GRANTED_LIST);
1966 mlog(0, "node died with unlock pending "
1967 "on %.*s. remove from blocked list and skip.\n",
1968 res->lockname.len, res->lockname.name);
1969 dlm_commit_pending_unlock(res, lock);
1970 lock->unlock_pending = 0;
1971 } else if (lock->cancel_pending) {
1972 /* if a cancel was in progress, treat as
1973 * if this had completed successfully
1974 * before sending this lock state to the
1976 BUG_ON(i != DLM_CONVERTING_LIST);
1977 mlog(0, "node died with cancel pending "
1978 "on %.*s. move back to granted list.\n",
1979 res->lockname.len, res->lockname.name);
1980 dlm_commit_pending_cancel(res, lock);
1981 lock->cancel_pending = 0;
1990 /* removes all recovered locks from the recovery list.
1991 * sets the res->owner to the new master.
1992 * unsets the RECOVERY flag and wakes waiters. */
1993 static void dlm_finish_local_lockres_recovery(struct dlm_ctxt *dlm,
1994 u8 dead_node, u8 new_master)
1997 struct hlist_node *hash_iter;
1998 struct hlist_head *bucket;
1999 struct dlm_lock_resource *res, *next;
2003 assert_spin_locked(&dlm->spinlock);
2005 list_for_each_entry_safe(res, next, &dlm->reco.resources, recovering) {
2006 if (res->owner == dead_node) {
2007 list_del_init(&res->recovering);
2008 spin_lock(&res->spinlock);
2009 /* new_master has our reference from
2010 * the lock state sent during recovery */
2011 dlm_change_lockres_owner(dlm, res, new_master);
2012 res->state &= ~DLM_LOCK_RES_RECOVERING;
2013 if (__dlm_lockres_has_locks(res))
2014 __dlm_dirty_lockres(dlm, res);
2015 spin_unlock(&res->spinlock);
2017 dlm_lockres_put(res);
2021 /* this will become unnecessary eventually, but
2022 * for now we need to run the whole hash, clear
2023 * the RECOVERING state and set the owner
2025 for (i = 0; i < DLM_HASH_BUCKETS; i++) {
2026 bucket = dlm_lockres_hash(dlm, i);
2027 hlist_for_each_entry(res, hash_iter, bucket, hash_node) {
2028 if (res->state & DLM_LOCK_RES_RECOVERING) {
2029 if (res->owner == dead_node) {
2030 mlog(0, "(this=%u) res %.*s owner=%u "
2031 "was not on recovering list, but "
2032 "clearing state anyway\n",
2033 dlm->node_num, res->lockname.len,
2034 res->lockname.name, new_master);
2035 } else if (res->owner == dlm->node_num) {
2036 mlog(0, "(this=%u) res %.*s owner=%u "
2037 "was not on recovering list, "
2038 "owner is THIS node, clearing\n",
2039 dlm->node_num, res->lockname.len,
2040 res->lockname.name, new_master);
2044 if (!list_empty(&res->recovering)) {
2045 mlog(0, "%s:%.*s: lockres was "
2046 "marked RECOVERING, owner=%u\n",
2047 dlm->name, res->lockname.len,
2048 res->lockname.name, res->owner);
2049 list_del_init(&res->recovering);
2050 dlm_lockres_put(res);
2052 spin_lock(&res->spinlock);
2053 /* new_master has our reference from
2054 * the lock state sent during recovery */
2055 dlm_change_lockres_owner(dlm, res, new_master);
2056 res->state &= ~DLM_LOCK_RES_RECOVERING;
2057 if (__dlm_lockres_has_locks(res))
2058 __dlm_dirty_lockres(dlm, res);
2059 spin_unlock(&res->spinlock);
2066 static inline int dlm_lvb_needs_invalidation(struct dlm_lock *lock, int local)
2069 if (lock->ml.type != LKM_EXMODE &&
2070 lock->ml.type != LKM_PRMODE)
2072 } else if (lock->ml.type == LKM_EXMODE)
2077 static void dlm_revalidate_lvb(struct dlm_ctxt *dlm,
2078 struct dlm_lock_resource *res, u8 dead_node)
2080 struct list_head *queue;
2081 struct dlm_lock *lock;
2082 int blank_lvb = 0, local = 0;
2086 assert_spin_locked(&dlm->spinlock);
2087 assert_spin_locked(&res->spinlock);
2089 if (res->owner == dlm->node_num)
2090 /* if this node owned the lockres, and if the dead node
2091 * had an EX when he died, blank out the lvb */
2092 search_node = dead_node;
2094 /* if this is a secondary lockres, and we had no EX or PR
2095 * locks granted, we can no longer trust the lvb */
2096 search_node = dlm->node_num;
2097 local = 1; /* check local state for valid lvb */
2100 for (i=DLM_GRANTED_LIST; i<=DLM_CONVERTING_LIST; i++) {
2101 queue = dlm_list_idx_to_ptr(res, i);
2102 list_for_each_entry(lock, queue, list) {
2103 if (lock->ml.node == search_node) {
2104 if (dlm_lvb_needs_invalidation(lock, local)) {
2105 /* zero the lksb lvb and lockres lvb */
2107 memset(lock->lksb->lvb, 0, DLM_LVB_LEN);
2114 mlog(0, "clearing %.*s lvb, dead node %u had EX\n",
2115 res->lockname.len, res->lockname.name, dead_node);
2116 memset(res->lvb, 0, DLM_LVB_LEN);
2120 static void dlm_free_dead_locks(struct dlm_ctxt *dlm,
2121 struct dlm_lock_resource *res, u8 dead_node)
2123 struct dlm_lock *lock, *next;
2124 unsigned int freed = 0;
2126 /* this node is the lockres master:
2127 * 1) remove any stale locks for the dead node
2128 * 2) if the dead node had an EX when he died, blank out the lvb
2130 assert_spin_locked(&dlm->spinlock);
2131 assert_spin_locked(&res->spinlock);
2133 /* TODO: check pending_asts, pending_basts here */
2134 list_for_each_entry_safe(lock, next, &res->granted, list) {
2135 if (lock->ml.node == dead_node) {
2136 list_del_init(&lock->list);
2141 list_for_each_entry_safe(lock, next, &res->converting, list) {
2142 if (lock->ml.node == dead_node) {
2143 list_del_init(&lock->list);
2148 list_for_each_entry_safe(lock, next, &res->blocked, list) {
2149 if (lock->ml.node == dead_node) {
2150 list_del_init(&lock->list);
2157 mlog(0, "%s:%.*s: freed %u locks for dead node %u, "
2158 "dropping ref from lockres\n", dlm->name,
2159 res->lockname.len, res->lockname.name, freed, dead_node);
2160 BUG_ON(!test_bit(dead_node, res->refmap));
2161 dlm_lockres_clear_refmap_bit(dead_node, res);
2162 } else if (test_bit(dead_node, res->refmap)) {
2163 mlog(0, "%s:%.*s: dead node %u had a ref, but had "
2164 "no locks and had not purged before dying\n", dlm->name,
2165 res->lockname.len, res->lockname.name, dead_node);
2166 dlm_lockres_clear_refmap_bit(dead_node, res);
2169 /* do not kick thread yet */
2170 __dlm_dirty_lockres(dlm, res);
2173 /* if this node is the recovery master, and there are no
2174 * locks for a given lockres owned by this node that are in
2175 * either PR or EX mode, zero out the lvb before requesting.
2180 static void dlm_do_local_recovery_cleanup(struct dlm_ctxt *dlm, u8 dead_node)
2182 struct hlist_node *iter;
2183 struct dlm_lock_resource *res;
2185 struct hlist_head *bucket;
2186 struct dlm_lock *lock;
2189 /* purge any stale mles */
2190 dlm_clean_master_list(dlm, dead_node);
2193 * now clean up all lock resources. there are two rules:
2195 * 1) if the dead node was the master, move the lockres
2196 * to the recovering list. set the RECOVERING flag.
2197 * this lockres needs to be cleaned up before it can
2200 * 2) if this node was the master, remove all locks from
2201 * each of the lockres queues that were owned by the
2202 * dead node. once recovery finishes, the dlm thread
2203 * can be kicked again to see if any ASTs or BASTs
2204 * need to be fired as a result.
2206 for (i = 0; i < DLM_HASH_BUCKETS; i++) {
2207 bucket = dlm_lockres_hash(dlm, i);
2208 hlist_for_each_entry(res, iter, bucket, hash_node) {
2209 /* always prune any $RECOVERY entries for dead nodes,
2210 * otherwise hangs can occur during later recovery */
2211 if (dlm_is_recovery_lock(res->lockname.name,
2212 res->lockname.len)) {
2213 spin_lock(&res->spinlock);
2214 list_for_each_entry(lock, &res->granted, list) {
2215 if (lock->ml.node == dead_node) {
2216 mlog(0, "AHA! there was "
2217 "a $RECOVERY lock for dead "
2219 dead_node, dlm->name);
2220 list_del_init(&lock->list);
2225 spin_unlock(&res->spinlock);
2228 spin_lock(&res->spinlock);
2229 /* zero the lvb if necessary */
2230 dlm_revalidate_lvb(dlm, res, dead_node);
2231 if (res->owner == dead_node) {
2232 if (res->state & DLM_LOCK_RES_DROPPING_REF)
2233 mlog(0, "%s:%.*s: owned by "
2234 "dead node %u, this node was "
2235 "dropping its ref when it died. "
2236 "continue, dropping the flag.\n",
2237 dlm->name, res->lockname.len,
2238 res->lockname.name, dead_node);
2240 /* the wake_up for this will happen when the
2241 * RECOVERING flag is dropped later */
2242 res->state &= ~DLM_LOCK_RES_DROPPING_REF;
2244 dlm_move_lockres_to_recovery_list(dlm, res);
2245 } else if (res->owner == dlm->node_num) {
2246 dlm_free_dead_locks(dlm, res, dead_node);
2247 __dlm_lockres_calc_usage(dlm, res);
2249 spin_unlock(&res->spinlock);
2255 static void __dlm_hb_node_down(struct dlm_ctxt *dlm, int idx)
2257 assert_spin_locked(&dlm->spinlock);
2259 if (dlm->reco.new_master == idx) {
2260 mlog(0, "%s: recovery master %d just died\n",
2262 if (dlm->reco.state & DLM_RECO_STATE_FINALIZE) {
2263 /* finalize1 was reached, so it is safe to clear
2264 * the new_master and dead_node. that recovery
2266 mlog(0, "%s: dead master %d had reached "
2267 "finalize1 state, clearing\n", dlm->name, idx);
2268 dlm->reco.state &= ~DLM_RECO_STATE_FINALIZE;
2269 __dlm_reset_recovery(dlm);
2273 /* check to see if the node is already considered dead */
2274 if (!test_bit(idx, dlm->live_nodes_map)) {
2275 mlog(0, "for domain %s, node %d is already dead. "
2276 "another node likely did recovery already.\n",
2281 /* check to see if we do not care about this node */
2282 if (!test_bit(idx, dlm->domain_map)) {
2283 /* This also catches the case that we get a node down
2284 * but haven't joined the domain yet. */
2285 mlog(0, "node %u already removed from domain!\n", idx);
2289 clear_bit(idx, dlm->live_nodes_map);
2291 /* Clean up join state on node death. */
2292 if (dlm->joining_node == idx) {
2293 mlog(0, "Clearing join state for node %u\n", idx);
2294 __dlm_set_joining_node(dlm, DLM_LOCK_RES_OWNER_UNKNOWN);
2297 /* make sure local cleanup occurs before the heartbeat events */
2298 if (!test_bit(idx, dlm->recovery_map))
2299 dlm_do_local_recovery_cleanup(dlm, idx);
2301 /* notify anything attached to the heartbeat events */
2302 dlm_hb_event_notify_attached(dlm, idx, 0);
2304 mlog(0, "node %u being removed from domain map!\n", idx);
2305 clear_bit(idx, dlm->domain_map);
2306 /* wake up migration waiters if a node goes down.
2307 * perhaps later we can genericize this for other waiters. */
2308 wake_up(&dlm->migration_wq);
2310 if (test_bit(idx, dlm->recovery_map))
2311 mlog(0, "domain %s, node %u already added "
2312 "to recovery map!\n", dlm->name, idx);
2314 set_bit(idx, dlm->recovery_map);
2317 void dlm_hb_node_down_cb(struct o2nm_node *node, int idx, void *data)
2319 struct dlm_ctxt *dlm = data;
2324 spin_lock(&dlm->spinlock);
2325 __dlm_hb_node_down(dlm, idx);
2326 spin_unlock(&dlm->spinlock);
2331 void dlm_hb_node_up_cb(struct o2nm_node *node, int idx, void *data)
2333 struct dlm_ctxt *dlm = data;
2338 spin_lock(&dlm->spinlock);
2339 set_bit(idx, dlm->live_nodes_map);
2340 /* do NOT notify mle attached to the heartbeat events.
2341 * new nodes are not interesting in mastery until joined. */
2342 spin_unlock(&dlm->spinlock);
2347 static void dlm_reco_ast(void *astdata)
2349 struct dlm_ctxt *dlm = astdata;
2350 mlog(0, "ast for recovery lock fired!, this=%u, dlm=%s\n",
2351 dlm->node_num, dlm->name);
2353 static void dlm_reco_bast(void *astdata, int blocked_type)
2355 struct dlm_ctxt *dlm = astdata;
2356 mlog(0, "bast for recovery lock fired!, this=%u, dlm=%s\n",
2357 dlm->node_num, dlm->name);
2359 static void dlm_reco_unlock_ast(void *astdata, enum dlm_status st)
2361 mlog(0, "unlockast for recovery lock fired!\n");
2365 * dlm_pick_recovery_master will continually attempt to use
2366 * dlmlock() on the special "$RECOVERY" lockres with the
2367 * LKM_NOQUEUE flag to get an EX. every thread that enters
2368 * this function on each node racing to become the recovery
2369 * master will not stop attempting this until either:
2370 * a) this node gets the EX (and becomes the recovery master),
2371 * or b) dlm->reco.new_master gets set to some nodenum
2372 * != O2NM_INVALID_NODE_NUM (another node will do the reco).
2373 * so each time a recovery master is needed, the entire cluster
2374 * will sync at this point. if the new master dies, that will
2375 * be detected in dlm_do_recovery */
2376 static int dlm_pick_recovery_master(struct dlm_ctxt *dlm)
2378 enum dlm_status ret;
2379 struct dlm_lockstatus lksb;
2380 int status = -EINVAL;
2382 mlog(0, "starting recovery of %s at %lu, dead=%u, this=%u\n",
2383 dlm->name, jiffies, dlm->reco.dead_node, dlm->node_num);
2385 memset(&lksb, 0, sizeof(lksb));
2387 ret = dlmlock(dlm, LKM_EXMODE, &lksb, LKM_NOQUEUE|LKM_RECOVERY,
2388 DLM_RECOVERY_LOCK_NAME, DLM_RECOVERY_LOCK_NAME_LEN,
2389 dlm_reco_ast, dlm, dlm_reco_bast);
2391 mlog(0, "%s: dlmlock($RECOVERY) returned %d, lksb=%d\n",
2392 dlm->name, ret, lksb.status);
2394 if (ret == DLM_NORMAL) {
2395 mlog(0, "dlm=%s dlmlock says I got it (this=%u)\n",
2396 dlm->name, dlm->node_num);
2398 /* got the EX lock. check to see if another node
2399 * just became the reco master */
2400 if (dlm_reco_master_ready(dlm)) {
2401 mlog(0, "%s: got reco EX lock, but %u will "
2402 "do the recovery\n", dlm->name,
2403 dlm->reco.new_master);
2408 /* see if recovery was already finished elsewhere */
2409 spin_lock(&dlm->spinlock);
2410 if (dlm->reco.dead_node == O2NM_INVALID_NODE_NUM) {
2412 mlog(0, "%s: got reco EX lock, but "
2413 "node got recovered already\n", dlm->name);
2414 if (dlm->reco.new_master != O2NM_INVALID_NODE_NUM) {
2415 mlog(ML_ERROR, "%s: new master is %u "
2416 "but no dead node!\n",
2417 dlm->name, dlm->reco.new_master);
2421 spin_unlock(&dlm->spinlock);
2424 /* if this node has actually become the recovery master,
2425 * set the master and send the messages to begin recovery */
2427 mlog(0, "%s: dead=%u, this=%u, sending "
2428 "begin_reco now\n", dlm->name,
2429 dlm->reco.dead_node, dlm->node_num);
2430 status = dlm_send_begin_reco_message(dlm,
2431 dlm->reco.dead_node);
2432 /* this always succeeds */
2435 /* set the new_master to this node */
2436 spin_lock(&dlm->spinlock);
2437 dlm_set_reco_master(dlm, dlm->node_num);
2438 spin_unlock(&dlm->spinlock);
2441 /* recovery lock is a special case. ast will not get fired,
2442 * so just go ahead and unlock it. */
2443 ret = dlmunlock(dlm, &lksb, 0, dlm_reco_unlock_ast, dlm);
2444 if (ret == DLM_DENIED) {
2445 mlog(0, "got DLM_DENIED, trying LKM_CANCEL\n");
2446 ret = dlmunlock(dlm, &lksb, LKM_CANCEL, dlm_reco_unlock_ast, dlm);
2448 if (ret != DLM_NORMAL) {
2449 /* this would really suck. this could only happen
2450 * if there was a network error during the unlock
2451 * because of node death. this means the unlock
2452 * is actually "done" and the lock structure is
2453 * even freed. we can continue, but only
2454 * because this specific lock name is special. */
2455 mlog(ML_ERROR, "dlmunlock returned %d\n", ret);
2457 } else if (ret == DLM_NOTQUEUED) {
2458 mlog(0, "dlm=%s dlmlock says another node got it (this=%u)\n",
2459 dlm->name, dlm->node_num);
2460 /* another node is master. wait on
2461 * reco.new_master != O2NM_INVALID_NODE_NUM
2462 * for at most one second */
2463 wait_event_timeout(dlm->dlm_reco_thread_wq,
2464 dlm_reco_master_ready(dlm),
2465 msecs_to_jiffies(1000));
2466 if (!dlm_reco_master_ready(dlm)) {
2467 mlog(0, "%s: reco master taking awhile\n",
2471 /* another node has informed this one that it is reco master */
2472 mlog(0, "%s: reco master %u is ready to recover %u\n",
2473 dlm->name, dlm->reco.new_master, dlm->reco.dead_node);
2475 } else if (ret == DLM_RECOVERING) {
2476 mlog(0, "dlm=%s dlmlock says master node died (this=%u)\n",
2477 dlm->name, dlm->node_num);
2480 struct dlm_lock_resource *res;
2482 /* dlmlock returned something other than NOTQUEUED or NORMAL */
2483 mlog(ML_ERROR, "%s: got %s from dlmlock($RECOVERY), "
2484 "lksb.status=%s\n", dlm->name, dlm_errname(ret),
2485 dlm_errname(lksb.status));
2486 res = dlm_lookup_lockres(dlm, DLM_RECOVERY_LOCK_NAME,
2487 DLM_RECOVERY_LOCK_NAME_LEN);
2489 dlm_print_one_lock_resource(res);
2490 dlm_lockres_put(res);
2492 mlog(ML_ERROR, "recovery lock not found\n");
2500 static int dlm_send_begin_reco_message(struct dlm_ctxt *dlm, u8 dead_node)
2502 struct dlm_begin_reco br;
2504 struct dlm_node_iter iter;
2508 mlog_entry("%u\n", dead_node);
2510 mlog(0, "%s: dead node is %u\n", dlm->name, dead_node);
2512 spin_lock(&dlm->spinlock);
2513 dlm_node_iter_init(dlm->domain_map, &iter);
2514 spin_unlock(&dlm->spinlock);
2516 clear_bit(dead_node, iter.node_map);
2518 memset(&br, 0, sizeof(br));
2519 br.node_idx = dlm->node_num;
2520 br.dead_node = dead_node;
2522 while ((nodenum = dlm_node_iter_next(&iter)) >= 0) {
2524 if (nodenum == dead_node) {
2525 mlog(0, "not sending begin reco to dead node "
2529 if (nodenum == dlm->node_num) {
2530 mlog(0, "not sending begin reco to self\n");
2535 mlog(0, "attempting to send begin reco msg to %d\n",
2537 ret = o2net_send_message(DLM_BEGIN_RECO_MSG, dlm->key,
2538 &br, sizeof(br), nodenum, &status);
2539 /* negative status is handled ok by caller here */
2542 if (dlm_is_host_down(ret)) {
2543 /* node is down. not involved in recovery
2544 * so just keep going */
2545 mlog(0, "%s: node %u was down when sending "
2546 "begin reco msg (%d)\n", dlm->name, nodenum, ret);
2550 struct dlm_lock_resource *res;
2551 /* this is now a serious problem, possibly ENOMEM
2552 * in the network stack. must retry */
2554 mlog(ML_ERROR, "begin reco of dlm %s to node %u "
2555 " returned %d\n", dlm->name, nodenum, ret);
2556 res = dlm_lookup_lockres(dlm, DLM_RECOVERY_LOCK_NAME,
2557 DLM_RECOVERY_LOCK_NAME_LEN);
2559 dlm_print_one_lock_resource(res);
2560 dlm_lockres_put(res);
2562 mlog(ML_ERROR, "recovery lock not found\n");
2564 /* sleep for a bit in hopes that we can avoid
2568 } else if (ret == EAGAIN) {
2569 mlog(0, "%s: trying to start recovery of node "
2570 "%u, but node %u is waiting for last recovery "
2571 "to complete, backoff for a bit\n", dlm->name,
2572 dead_node, nodenum);
2573 /* TODO Look into replacing msleep with cond_resched() */
2582 int dlm_begin_reco_handler(struct o2net_msg *msg, u32 len, void *data,
2585 struct dlm_ctxt *dlm = data;
2586 struct dlm_begin_reco *br = (struct dlm_begin_reco *)msg->buf;
2588 /* ok to return 0, domain has gone away */
2592 spin_lock(&dlm->spinlock);
2593 if (dlm->reco.state & DLM_RECO_STATE_FINALIZE) {
2594 mlog(0, "%s: node %u wants to recover node %u (%u:%u) "
2595 "but this node is in finalize state, waiting on finalize2\n",
2596 dlm->name, br->node_idx, br->dead_node,
2597 dlm->reco.dead_node, dlm->reco.new_master);
2598 spin_unlock(&dlm->spinlock);
2601 spin_unlock(&dlm->spinlock);
2603 mlog(0, "%s: node %u wants to recover node %u (%u:%u)\n",
2604 dlm->name, br->node_idx, br->dead_node,
2605 dlm->reco.dead_node, dlm->reco.new_master);
2607 dlm_fire_domain_eviction_callbacks(dlm, br->dead_node);
2609 spin_lock(&dlm->spinlock);
2610 if (dlm->reco.new_master != O2NM_INVALID_NODE_NUM) {
2611 if (test_bit(dlm->reco.new_master, dlm->recovery_map)) {
2612 mlog(0, "%s: new_master %u died, changing "
2613 "to %u\n", dlm->name, dlm->reco.new_master,
2616 mlog(0, "%s: new_master %u NOT DEAD, changing "
2617 "to %u\n", dlm->name, dlm->reco.new_master,
2619 /* may not have seen the new master as dead yet */
2622 if (dlm->reco.dead_node != O2NM_INVALID_NODE_NUM) {
2623 mlog(ML_NOTICE, "%s: dead_node previously set to %u, "
2624 "node %u changing it to %u\n", dlm->name,
2625 dlm->reco.dead_node, br->node_idx, br->dead_node);
2627 dlm_set_reco_master(dlm, br->node_idx);
2628 dlm_set_reco_dead_node(dlm, br->dead_node);
2629 if (!test_bit(br->dead_node, dlm->recovery_map)) {
2630 mlog(0, "recovery master %u sees %u as dead, but this "
2631 "node has not yet. marking %u as dead\n",
2632 br->node_idx, br->dead_node, br->dead_node);
2633 if (!test_bit(br->dead_node, dlm->domain_map) ||
2634 !test_bit(br->dead_node, dlm->live_nodes_map))
2635 mlog(0, "%u not in domain/live_nodes map "
2636 "so setting it in reco map manually\n",
2638 /* force the recovery cleanup in __dlm_hb_node_down
2639 * both of these will be cleared in a moment */
2640 set_bit(br->dead_node, dlm->domain_map);
2641 set_bit(br->dead_node, dlm->live_nodes_map);
2642 __dlm_hb_node_down(dlm, br->dead_node);
2644 spin_unlock(&dlm->spinlock);
2646 dlm_kick_recovery_thread(dlm);
2648 mlog(0, "%s: recovery started by node %u, for %u (%u:%u)\n",
2649 dlm->name, br->node_idx, br->dead_node,
2650 dlm->reco.dead_node, dlm->reco.new_master);
2656 #define DLM_FINALIZE_STAGE2 0x01
2657 static int dlm_send_finalize_reco_message(struct dlm_ctxt *dlm)
2660 struct dlm_finalize_reco fr;
2661 struct dlm_node_iter iter;
2666 mlog(0, "finishing recovery for node %s:%u, "
2667 "stage %d\n", dlm->name, dlm->reco.dead_node, stage);
2669 spin_lock(&dlm->spinlock);
2670 dlm_node_iter_init(dlm->domain_map, &iter);
2671 spin_unlock(&dlm->spinlock);
2674 memset(&fr, 0, sizeof(fr));
2675 fr.node_idx = dlm->node_num;
2676 fr.dead_node = dlm->reco.dead_node;
2678 fr.flags |= DLM_FINALIZE_STAGE2;
2680 while ((nodenum = dlm_node_iter_next(&iter)) >= 0) {
2681 if (nodenum == dlm->node_num)
2683 ret = o2net_send_message(DLM_FINALIZE_RECO_MSG, dlm->key,
2684 &fr, sizeof(fr), nodenum, &status);
2689 if (dlm_is_host_down(ret)) {
2690 /* this has no effect on this recovery
2691 * session, so set the status to zero to
2692 * finish out the last recovery */
2693 mlog(ML_ERROR, "node %u went down after this "
2694 "node finished recovery.\n", nodenum);
2702 /* reset the node_iter back to the top and send finalize2 */
2711 int dlm_finalize_reco_handler(struct o2net_msg *msg, u32 len, void *data,
2714 struct dlm_ctxt *dlm = data;
2715 struct dlm_finalize_reco *fr = (struct dlm_finalize_reco *)msg->buf;
2718 /* ok to return 0, domain has gone away */
2722 if (fr->flags & DLM_FINALIZE_STAGE2)
2725 mlog(0, "%s: node %u finalizing recovery stage%d of "
2726 "node %u (%u:%u)\n", dlm->name, fr->node_idx, stage,
2727 fr->dead_node, dlm->reco.dead_node, dlm->reco.new_master);
2729 spin_lock(&dlm->spinlock);
2731 if (dlm->reco.new_master != fr->node_idx) {
2732 mlog(ML_ERROR, "node %u sent recovery finalize msg, but node "
2733 "%u is supposed to be the new master, dead=%u\n",
2734 fr->node_idx, dlm->reco.new_master, fr->dead_node);
2737 if (dlm->reco.dead_node != fr->dead_node) {
2738 mlog(ML_ERROR, "node %u sent recovery finalize msg for dead "
2739 "node %u, but node %u is supposed to be dead\n",
2740 fr->node_idx, fr->dead_node, dlm->reco.dead_node);
2746 dlm_finish_local_lockres_recovery(dlm, fr->dead_node, fr->node_idx);
2747 if (dlm->reco.state & DLM_RECO_STATE_FINALIZE) {
2748 mlog(ML_ERROR, "%s: received finalize1 from "
2749 "new master %u for dead node %u, but "
2750 "this node has already received it!\n",
2751 dlm->name, fr->node_idx, fr->dead_node);
2752 dlm_print_reco_node_status(dlm);
2755 dlm->reco.state |= DLM_RECO_STATE_FINALIZE;
2756 spin_unlock(&dlm->spinlock);
2759 if (!(dlm->reco.state & DLM_RECO_STATE_FINALIZE)) {
2760 mlog(ML_ERROR, "%s: received finalize2 from "
2761 "new master %u for dead node %u, but "
2762 "this node did not have finalize1!\n",
2763 dlm->name, fr->node_idx, fr->dead_node);
2764 dlm_print_reco_node_status(dlm);
2767 dlm->reco.state &= ~DLM_RECO_STATE_FINALIZE;
2768 spin_unlock(&dlm->spinlock);
2769 dlm_reset_recovery(dlm);
2770 dlm_kick_recovery_thread(dlm);
2776 mlog(0, "%s: recovery done, reco master was %u, dead now %u, master now %u\n",
2777 dlm->name, fr->node_idx, dlm->reco.dead_node, dlm->reco.new_master);