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 spinlock_t dlm_reco_state_lock
= SPIN_LOCK_UNLOCKED
;
102 static spinlock_t dlm_mig_cookie_lock
= SPIN_LOCK_UNLOCKED
;
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
;
161 spin_lock(&dlm
->work_lock
);
162 list_splice_init(&dlm
->work_list
, &tmp_list
);
163 spin_unlock(&dlm
->work_lock
);
165 list_for_each_safe(iter
, iter2
, &tmp_list
) {
166 item
= list_entry(iter
, struct dlm_work_item
, list
);
167 workfunc
= item
->func
;
168 list_del_init(&item
->list
);
170 /* already have ref on dlm to avoid having
171 * it disappear. just double-check. */
172 BUG_ON(item
->dlm
!= dlm
);
174 /* this is allowed to sleep and
175 * call network stuff */
176 workfunc(item
, item
->data
);
187 void dlm_kick_recovery_thread(struct dlm_ctxt
*dlm
)
189 /* wake the recovery thread
190 * this will wake the reco thread in one of three places
191 * 1) sleeping with no recovery happening
192 * 2) sleeping with recovery mastered elsewhere
193 * 3) recovery mastered here, waiting on reco data */
195 wake_up(&dlm
->dlm_reco_thread_wq
);
198 /* Launch the recovery thread */
199 int dlm_launch_recovery_thread(struct dlm_ctxt
*dlm
)
201 mlog(0, "starting dlm recovery thread...\n");
203 dlm
->dlm_reco_thread_task
= kthread_run(dlm_recovery_thread
, dlm
,
205 if (IS_ERR(dlm
->dlm_reco_thread_task
)) {
206 mlog_errno(PTR_ERR(dlm
->dlm_reco_thread_task
));
207 dlm
->dlm_reco_thread_task
= NULL
;
214 void dlm_complete_recovery_thread(struct dlm_ctxt
*dlm
)
216 if (dlm
->dlm_reco_thread_task
) {
217 mlog(0, "waiting for dlm recovery thread to exit\n");
218 kthread_stop(dlm
->dlm_reco_thread_task
);
219 dlm
->dlm_reco_thread_task
= NULL
;
226 * this is lame, but here's how recovery works...
227 * 1) all recovery threads cluster wide will work on recovering
229 * 2) negotiate who will take over all the locks for the dead node.
230 * thats right... ALL the locks.
231 * 3) once a new master is chosen, everyone scans all locks
232 * and moves aside those mastered by the dead guy
233 * 4) each of these locks should be locked until recovery is done
234 * 5) the new master collects up all of secondary lock queue info
235 * one lock at a time, forcing each node to communicate back
237 * 6) each secondary lock queue responds with the full known lock info
238 * 7) once the new master has run all its locks, it sends a ALLDONE!
239 * message to everyone
240 * 8) upon receiving this message, the secondary queue node unlocks
241 * and responds to the ALLDONE
242 * 9) once the new master gets responses from everyone, he unlocks
243 * everything and recovery for this dead node is done
244 *10) go back to 2) while there are still dead nodes
248 static void dlm_print_reco_node_status(struct dlm_ctxt
*dlm
)
250 struct dlm_reco_node_data
*ndata
;
251 struct dlm_lock_resource
*res
;
253 mlog(ML_NOTICE
, "%s(%d): recovery info, state=%s, dead=%u, master=%u\n",
254 dlm
->name
, dlm
->dlm_reco_thread_task
->pid
,
255 dlm
->reco
.state
& DLM_RECO_STATE_ACTIVE
? "ACTIVE" : "inactive",
256 dlm
->reco
.dead_node
, dlm
->reco
.new_master
);
258 list_for_each_entry(ndata
, &dlm
->reco
.node_data
, list
) {
259 char *st
= "unknown";
260 switch (ndata
->state
) {
261 case DLM_RECO_NODE_DATA_INIT
:
264 case DLM_RECO_NODE_DATA_REQUESTING
:
267 case DLM_RECO_NODE_DATA_DEAD
:
270 case DLM_RECO_NODE_DATA_RECEIVING
:
273 case DLM_RECO_NODE_DATA_REQUESTED
:
276 case DLM_RECO_NODE_DATA_DONE
:
279 case DLM_RECO_NODE_DATA_FINALIZE_SENT
:
280 st
= "finalize-sent";
286 mlog(ML_NOTICE
, "%s: reco state, node %u, state=%s\n",
287 dlm
->name
, ndata
->node_num
, st
);
289 list_for_each_entry(res
, &dlm
->reco
.resources
, recovering
) {
290 mlog(ML_NOTICE
, "%s: lockres %.*s on recovering list\n",
291 dlm
->name
, res
->lockname
.len
, res
->lockname
.name
);
295 #define DLM_RECO_THREAD_TIMEOUT_MS (5 * 1000)
297 static int dlm_recovery_thread(void *data
)
300 struct dlm_ctxt
*dlm
= data
;
301 unsigned long timeout
= msecs_to_jiffies(DLM_RECO_THREAD_TIMEOUT_MS
);
303 mlog(0, "dlm thread running for %s...\n", dlm
->name
);
305 while (!kthread_should_stop()) {
306 if (dlm_joined(dlm
)) {
307 status
= dlm_do_recovery(dlm
);
308 if (status
== -EAGAIN
) {
309 /* do not sleep, recheck immediately. */
316 wait_event_interruptible_timeout(dlm
->dlm_reco_thread_wq
,
317 kthread_should_stop(),
321 mlog(0, "quitting DLM recovery thread\n");
325 /* returns true when the recovery master has contacted us */
326 static int dlm_reco_master_ready(struct dlm_ctxt
*dlm
)
329 spin_lock(&dlm
->spinlock
);
330 ready
= (dlm
->reco
.new_master
!= O2NM_INVALID_NODE_NUM
);
331 spin_unlock(&dlm
->spinlock
);
335 /* returns true if node is no longer in the domain
336 * could be dead or just not joined */
337 int dlm_is_node_dead(struct dlm_ctxt
*dlm
, u8 node
)
340 spin_lock(&dlm
->spinlock
);
341 dead
= !test_bit(node
, dlm
->domain_map
);
342 spin_unlock(&dlm
->spinlock
);
346 /* returns true if node is no longer in the domain
347 * could be dead or just not joined */
348 int dlm_is_node_recovered(struct dlm_ctxt
*dlm
, u8 node
)
351 spin_lock(&dlm
->spinlock
);
352 recovered
= !test_bit(node
, dlm
->recovery_map
);
353 spin_unlock(&dlm
->spinlock
);
358 int dlm_wait_for_node_death(struct dlm_ctxt
*dlm
, u8 node
, int timeout
)
361 mlog(ML_NOTICE
, "%s: waiting %dms for notification of "
362 "death of node %u\n", dlm
->name
, timeout
, node
);
363 wait_event_timeout(dlm
->dlm_reco_thread_wq
,
364 dlm_is_node_dead(dlm
, node
),
365 msecs_to_jiffies(timeout
));
367 mlog(ML_NOTICE
, "%s: waiting indefinitely for notification "
368 "of death of node %u\n", dlm
->name
, node
);
369 wait_event(dlm
->dlm_reco_thread_wq
,
370 dlm_is_node_dead(dlm
, node
));
372 /* for now, return 0 */
376 int dlm_wait_for_node_recovery(struct dlm_ctxt
*dlm
, u8 node
, int timeout
)
379 mlog(0, "%s: waiting %dms for notification of "
380 "recovery of node %u\n", dlm
->name
, timeout
, node
);
381 wait_event_timeout(dlm
->dlm_reco_thread_wq
,
382 dlm_is_node_recovered(dlm
, node
),
383 msecs_to_jiffies(timeout
));
385 mlog(0, "%s: waiting indefinitely for notification "
386 "of recovery of node %u\n", dlm
->name
, node
);
387 wait_event(dlm
->dlm_reco_thread_wq
,
388 dlm_is_node_recovered(dlm
, node
));
390 /* for now, return 0 */
394 /* callers of the top-level api calls (dlmlock/dlmunlock) should
395 * block on the dlm->reco.event when recovery is in progress.
396 * the dlm recovery thread will set this state when it begins
397 * recovering a dead node (as the new master or not) and clear
398 * the state and wake as soon as all affected lock resources have
399 * been marked with the RECOVERY flag */
400 static int dlm_in_recovery(struct dlm_ctxt
*dlm
)
403 spin_lock(&dlm
->spinlock
);
404 in_recovery
= !!(dlm
->reco
.state
& DLM_RECO_STATE_ACTIVE
);
405 spin_unlock(&dlm
->spinlock
);
410 void dlm_wait_for_recovery(struct dlm_ctxt
*dlm
)
412 wait_event(dlm
->reco
.event
, !dlm_in_recovery(dlm
));
415 static void dlm_begin_recovery(struct dlm_ctxt
*dlm
)
417 spin_lock(&dlm
->spinlock
);
418 BUG_ON(dlm
->reco
.state
& DLM_RECO_STATE_ACTIVE
);
419 dlm
->reco
.state
|= DLM_RECO_STATE_ACTIVE
;
420 spin_unlock(&dlm
->spinlock
);
423 static void dlm_end_recovery(struct dlm_ctxt
*dlm
)
425 spin_lock(&dlm
->spinlock
);
426 BUG_ON(!(dlm
->reco
.state
& DLM_RECO_STATE_ACTIVE
));
427 dlm
->reco
.state
&= ~DLM_RECO_STATE_ACTIVE
;
428 spin_unlock(&dlm
->spinlock
);
429 wake_up(&dlm
->reco
.event
);
432 static int dlm_do_recovery(struct dlm_ctxt
*dlm
)
437 spin_lock(&dlm
->spinlock
);
439 /* check to see if the new master has died */
440 if (dlm
->reco
.new_master
!= O2NM_INVALID_NODE_NUM
&&
441 test_bit(dlm
->reco
.new_master
, dlm
->recovery_map
)) {
442 mlog(0, "new master %u died while recovering %u!\n",
443 dlm
->reco
.new_master
, dlm
->reco
.dead_node
);
444 /* unset the new_master, leave dead_node */
445 dlm_set_reco_master(dlm
, O2NM_INVALID_NODE_NUM
);
448 /* select a target to recover */
449 if (dlm
->reco
.dead_node
== O2NM_INVALID_NODE_NUM
) {
452 bit
= find_next_bit (dlm
->recovery_map
, O2NM_MAX_NODES
+1, 0);
453 if (bit
>= O2NM_MAX_NODES
|| bit
< 0)
454 dlm_set_reco_dead_node(dlm
, O2NM_INVALID_NODE_NUM
);
456 dlm_set_reco_dead_node(dlm
, bit
);
457 } else if (!test_bit(dlm
->reco
.dead_node
, dlm
->recovery_map
)) {
459 mlog(ML_ERROR
, "dead_node %u no longer in recovery map!\n",
460 dlm
->reco
.dead_node
);
461 dlm_set_reco_dead_node(dlm
, O2NM_INVALID_NODE_NUM
);
464 if (dlm
->reco
.dead_node
== O2NM_INVALID_NODE_NUM
) {
465 // mlog(0, "nothing to recover! sleeping now!\n");
466 spin_unlock(&dlm
->spinlock
);
467 /* return to main thread loop and sleep. */
470 mlog(0, "%s(%d):recovery thread found node %u in the recovery map!\n",
471 dlm
->name
, dlm
->dlm_reco_thread_task
->pid
,
472 dlm
->reco
.dead_node
);
473 spin_unlock(&dlm
->spinlock
);
475 /* take write barrier */
476 /* (stops the list reshuffling thread, proxy ast handling) */
477 dlm_begin_recovery(dlm
);
479 if (dlm
->reco
.new_master
== dlm
->node_num
)
482 if (dlm
->reco
.new_master
== O2NM_INVALID_NODE_NUM
) {
483 /* choose a new master, returns 0 if this node
484 * is the master, -EEXIST if it's another node.
485 * this does not return until a new master is chosen
486 * or recovery completes entirely. */
487 ret
= dlm_pick_recovery_master(dlm
);
489 /* already notified everyone. go. */
492 mlog(0, "another node will master this recovery session.\n");
494 mlog(0, "dlm=%s (%d), new_master=%u, this node=%u, dead_node=%u\n",
495 dlm
->name
, dlm
->dlm_reco_thread_task
->pid
, dlm
->reco
.new_master
,
496 dlm
->node_num
, dlm
->reco
.dead_node
);
498 /* it is safe to start everything back up here
499 * because all of the dead node's lock resources
500 * have been marked as in-recovery */
501 dlm_end_recovery(dlm
);
503 /* sleep out in main dlm_recovery_thread loop. */
507 mlog(0, "(%d) mastering recovery of %s:%u here(this=%u)!\n",
508 dlm
->dlm_reco_thread_task
->pid
,
509 dlm
->name
, dlm
->reco
.dead_node
, dlm
->node_num
);
511 status
= dlm_remaster_locks(dlm
, dlm
->reco
.dead_node
);
513 /* we should never hit this anymore */
514 mlog(ML_ERROR
, "error %d remastering locks for node %u, "
515 "retrying.\n", status
, dlm
->reco
.dead_node
);
516 /* yield a bit to allow any final network messages
517 * to get handled on remaining nodes */
520 /* success! see if any other nodes need recovery */
521 mlog(0, "DONE mastering recovery of %s:%u here(this=%u)!\n",
522 dlm
->name
, dlm
->reco
.dead_node
, dlm
->node_num
);
523 dlm_reset_recovery(dlm
);
525 dlm_end_recovery(dlm
);
527 /* continue and look for another dead node */
531 static int dlm_remaster_locks(struct dlm_ctxt
*dlm
, u8 dead_node
)
534 struct dlm_reco_node_data
*ndata
;
535 struct list_head
*iter
;
541 /* we have become recovery master. there is no escaping
542 * this, so just keep trying until we get it. */
543 status
= dlm_init_recovery_area(dlm
, dead_node
);
545 mlog(ML_ERROR
, "%s: failed to alloc recovery area, "
546 "retrying\n", dlm
->name
);
549 } while (status
!= 0);
551 /* safe to access the node data list without a lock, since this
552 * process is the only one to change the list */
553 list_for_each(iter
, &dlm
->reco
.node_data
) {
554 ndata
= list_entry (iter
, struct dlm_reco_node_data
, list
);
555 BUG_ON(ndata
->state
!= DLM_RECO_NODE_DATA_INIT
);
556 ndata
->state
= DLM_RECO_NODE_DATA_REQUESTING
;
558 mlog(0, "requesting lock info from node %u\n",
561 if (ndata
->node_num
== dlm
->node_num
) {
562 ndata
->state
= DLM_RECO_NODE_DATA_DONE
;
567 status
= dlm_request_all_locks(dlm
, ndata
->node_num
,
571 if (dlm_is_host_down(status
)) {
572 /* node died, ignore it for recovery */
574 ndata
->state
= DLM_RECO_NODE_DATA_DEAD
;
575 /* wait for the domain map to catch up
576 * with the network state. */
577 wait_event_timeout(dlm
->dlm_reco_thread_wq
,
578 dlm_is_node_dead(dlm
,
580 msecs_to_jiffies(1000));
581 mlog(0, "waited 1 sec for %u, "
582 "dead? %s\n", ndata
->node_num
,
583 dlm_is_node_dead(dlm
, ndata
->node_num
) ?
586 /* -ENOMEM on the other node */
587 mlog(0, "%s: node %u returned "
588 "%d during recovery, retrying "
589 "after a short wait\n",
590 dlm
->name
, ndata
->node_num
,
595 } while (status
!= 0);
597 switch (ndata
->state
) {
598 case DLM_RECO_NODE_DATA_INIT
:
599 case DLM_RECO_NODE_DATA_FINALIZE_SENT
:
600 case DLM_RECO_NODE_DATA_REQUESTED
:
603 case DLM_RECO_NODE_DATA_DEAD
:
604 mlog(0, "node %u died after requesting "
605 "recovery info for node %u\n",
606 ndata
->node_num
, dead_node
);
607 /* fine. don't need this node's info.
608 * continue without it. */
610 case DLM_RECO_NODE_DATA_REQUESTING
:
611 ndata
->state
= DLM_RECO_NODE_DATA_REQUESTED
;
612 mlog(0, "now receiving recovery data from "
613 "node %u for dead node %u\n",
614 ndata
->node_num
, dead_node
);
616 case DLM_RECO_NODE_DATA_RECEIVING
:
617 mlog(0, "already receiving recovery data from "
618 "node %u for dead node %u\n",
619 ndata
->node_num
, dead_node
);
621 case DLM_RECO_NODE_DATA_DONE
:
622 mlog(0, "already DONE receiving recovery data "
623 "from node %u for dead node %u\n",
624 ndata
->node_num
, dead_node
);
629 mlog(0, "done requesting all lock info\n");
631 /* nodes should be sending reco data now
632 * just need to wait */
635 /* check all the nodes now to see if we are
636 * done, or if anyone died */
638 spin_lock(&dlm_reco_state_lock
);
639 list_for_each(iter
, &dlm
->reco
.node_data
) {
640 ndata
= list_entry (iter
, struct dlm_reco_node_data
, list
);
642 mlog(0, "checking recovery state of node %u\n",
644 switch (ndata
->state
) {
645 case DLM_RECO_NODE_DATA_INIT
:
646 case DLM_RECO_NODE_DATA_REQUESTING
:
647 mlog(ML_ERROR
, "bad ndata state for "
648 "node %u: state=%d\n",
649 ndata
->node_num
, ndata
->state
);
652 case DLM_RECO_NODE_DATA_DEAD
:
653 mlog(0, "node %u died after "
654 "requesting recovery info for "
655 "node %u\n", ndata
->node_num
,
658 case DLM_RECO_NODE_DATA_RECEIVING
:
659 case DLM_RECO_NODE_DATA_REQUESTED
:
660 mlog(0, "%s: node %u still in state %s\n",
661 dlm
->name
, ndata
->node_num
,
662 ndata
->state
==DLM_RECO_NODE_DATA_RECEIVING
?
663 "receiving" : "requested");
666 case DLM_RECO_NODE_DATA_DONE
:
667 mlog(0, "%s: node %u state is done\n",
668 dlm
->name
, ndata
->node_num
);
670 case DLM_RECO_NODE_DATA_FINALIZE_SENT
:
671 mlog(0, "%s: node %u state is finalize\n",
672 dlm
->name
, ndata
->node_num
);
676 spin_unlock(&dlm_reco_state_lock
);
678 mlog(0, "pass #%d, all_nodes_done?: %s\n", ++pass
,
679 all_nodes_done
?"yes":"no");
680 if (all_nodes_done
) {
683 /* all nodes are now in DLM_RECO_NODE_DATA_DONE state
684 * just send a finalize message to everyone and
686 mlog(0, "all nodes are done! send finalize\n");
687 ret
= dlm_send_finalize_reco_message(dlm
);
691 spin_lock(&dlm
->spinlock
);
692 dlm_finish_local_lockres_recovery(dlm
, dead_node
,
694 spin_unlock(&dlm
->spinlock
);
695 mlog(0, "should be done with recovery!\n");
697 mlog(0, "finishing recovery of %s at %lu, "
698 "dead=%u, this=%u, new=%u\n", dlm
->name
,
699 jiffies
, dlm
->reco
.dead_node
,
700 dlm
->node_num
, dlm
->reco
.new_master
);
703 /* rescan everything marked dirty along the way */
704 dlm_kick_thread(dlm
, NULL
);
707 /* wait to be signalled, with periodic timeout
708 * to check for node death */
709 wait_event_interruptible_timeout(dlm
->dlm_reco_thread_wq
,
710 kthread_should_stop(),
711 msecs_to_jiffies(DLM_RECO_THREAD_TIMEOUT_MS
));
716 dlm_destroy_recovery_area(dlm
, dead_node
);
722 static int dlm_init_recovery_area(struct dlm_ctxt
*dlm
, u8 dead_node
)
725 struct dlm_reco_node_data
*ndata
;
727 spin_lock(&dlm
->spinlock
);
728 memcpy(dlm
->reco
.node_map
, dlm
->domain_map
, sizeof(dlm
->domain_map
));
729 /* nodes can only be removed (by dying) after dropping
730 * this lock, and death will be trapped later, so this should do */
731 spin_unlock(&dlm
->spinlock
);
734 num
= find_next_bit (dlm
->reco
.node_map
, O2NM_MAX_NODES
, num
);
735 if (num
>= O2NM_MAX_NODES
) {
738 BUG_ON(num
== dead_node
);
740 ndata
= kcalloc(1, sizeof(*ndata
), GFP_NOFS
);
742 dlm_destroy_recovery_area(dlm
, dead_node
);
745 ndata
->node_num
= num
;
746 ndata
->state
= DLM_RECO_NODE_DATA_INIT
;
747 spin_lock(&dlm_reco_state_lock
);
748 list_add_tail(&ndata
->list
, &dlm
->reco
.node_data
);
749 spin_unlock(&dlm_reco_state_lock
);
756 static void dlm_destroy_recovery_area(struct dlm_ctxt
*dlm
, u8 dead_node
)
758 struct list_head
*iter
, *iter2
;
759 struct dlm_reco_node_data
*ndata
;
762 spin_lock(&dlm_reco_state_lock
);
763 list_splice_init(&dlm
->reco
.node_data
, &tmplist
);
764 spin_unlock(&dlm_reco_state_lock
);
766 list_for_each_safe(iter
, iter2
, &tmplist
) {
767 ndata
= list_entry (iter
, struct dlm_reco_node_data
, list
);
768 list_del_init(&ndata
->list
);
773 static int dlm_request_all_locks(struct dlm_ctxt
*dlm
, u8 request_from
,
776 struct dlm_lock_request lr
;
782 mlog(0, "dlm_request_all_locks: dead node is %u, sending request "
783 "to %u\n", dead_node
, request_from
);
785 memset(&lr
, 0, sizeof(lr
));
786 lr
.node_idx
= dlm
->node_num
;
787 lr
.dead_node
= dead_node
;
791 ret
= o2net_send_message(DLM_LOCK_REQUEST_MSG
, dlm
->key
,
792 &lr
, sizeof(lr
), request_from
, NULL
);
794 /* negative status is handled by caller */
798 // return from here, then
799 // sleep until all received or error
804 int dlm_request_all_locks_handler(struct o2net_msg
*msg
, u32 len
, void *data
)
806 struct dlm_ctxt
*dlm
= data
;
807 struct dlm_lock_request
*lr
= (struct dlm_lock_request
*)msg
->buf
;
809 struct dlm_work_item
*item
= NULL
;
814 if (lr
->dead_node
!= dlm
->reco
.dead_node
) {
815 mlog(ML_ERROR
, "%s: node %u sent dead_node=%u, but local "
816 "dead_node is %u\n", dlm
->name
, lr
->node_idx
,
817 lr
->dead_node
, dlm
->reco
.dead_node
);
818 dlm_print_reco_node_status(dlm
);
823 BUG_ON(lr
->dead_node
!= dlm
->reco
.dead_node
);
825 item
= kcalloc(1, sizeof(*item
), GFP_NOFS
);
831 /* this will get freed by dlm_request_all_locks_worker */
832 buf
= (char *) __get_free_page(GFP_NOFS
);
839 /* queue up work for dlm_request_all_locks_worker */
840 dlm_grab(dlm
); /* get an extra ref for the work item */
841 dlm_init_work_item(dlm
, item
, dlm_request_all_locks_worker
, buf
);
842 item
->u
.ral
.reco_master
= lr
->node_idx
;
843 item
->u
.ral
.dead_node
= lr
->dead_node
;
844 spin_lock(&dlm
->work_lock
);
845 list_add_tail(&item
->list
, &dlm
->work_list
);
846 spin_unlock(&dlm
->work_lock
);
847 schedule_work(&dlm
->dispatched_work
);
853 static void dlm_request_all_locks_worker(struct dlm_work_item
*item
, void *data
)
855 struct dlm_migratable_lockres
*mres
;
856 struct dlm_lock_resource
*res
;
857 struct dlm_ctxt
*dlm
;
858 LIST_HEAD(resources
);
859 struct list_head
*iter
;
861 u8 dead_node
, reco_master
;
862 int skip_all_done
= 0;
865 dead_node
= item
->u
.ral
.dead_node
;
866 reco_master
= item
->u
.ral
.reco_master
;
867 mres
= (struct dlm_migratable_lockres
*)data
;
869 mlog(0, "%s: recovery worker started, dead=%u, master=%u\n",
870 dlm
->name
, dead_node
, reco_master
);
872 if (dead_node
!= dlm
->reco
.dead_node
||
873 reco_master
!= dlm
->reco
.new_master
) {
874 /* worker could have been created before the recovery master
875 * died. if so, do not continue, but do not error. */
876 if (dlm
->reco
.new_master
== O2NM_INVALID_NODE_NUM
) {
877 mlog(ML_NOTICE
, "%s: will not send recovery state, "
878 "recovery master %u died, thread=(dead=%u,mas=%u)"
879 " current=(dead=%u,mas=%u)\n", dlm
->name
,
880 reco_master
, dead_node
, reco_master
,
881 dlm
->reco
.dead_node
, dlm
->reco
.new_master
);
883 mlog(ML_NOTICE
, "%s: reco state invalid: reco(dead=%u, "
884 "master=%u), request(dead=%u, master=%u)\n",
885 dlm
->name
, dlm
->reco
.dead_node
,
886 dlm
->reco
.new_master
, dead_node
, reco_master
);
891 /* lock resources should have already been moved to the
892 * dlm->reco.resources list. now move items from that list
893 * to a temp list if the dead owner matches. note that the
894 * whole cluster recovers only one node at a time, so we
895 * can safely move UNKNOWN lock resources for each recovery
897 dlm_move_reco_locks_to_list(dlm
, &resources
, dead_node
);
899 /* now we can begin blasting lockreses without the dlm lock */
901 /* any errors returned will be due to the new_master dying,
902 * the dlm_reco_thread should detect this */
903 list_for_each(iter
, &resources
) {
904 res
= list_entry (iter
, struct dlm_lock_resource
, recovering
);
905 ret
= dlm_send_one_lockres(dlm
, res
, mres
, reco_master
,
908 mlog(ML_ERROR
, "%s: node %u went down while sending "
909 "recovery state for dead node %u, ret=%d\n", dlm
->name
,
910 reco_master
, dead_node
, ret
);
916 /* move the resources back to the list */
917 spin_lock(&dlm
->spinlock
);
918 list_splice_init(&resources
, &dlm
->reco
.resources
);
919 spin_unlock(&dlm
->spinlock
);
921 if (!skip_all_done
) {
922 ret
= dlm_send_all_done_msg(dlm
, dead_node
, reco_master
);
924 mlog(ML_ERROR
, "%s: node %u went down while sending "
925 "recovery all-done for dead node %u, ret=%d\n",
926 dlm
->name
, reco_master
, dead_node
, ret
);
930 free_page((unsigned long)data
);
934 static int dlm_send_all_done_msg(struct dlm_ctxt
*dlm
, u8 dead_node
, u8 send_to
)
937 struct dlm_reco_data_done done_msg
;
939 memset(&done_msg
, 0, sizeof(done_msg
));
940 done_msg
.node_idx
= dlm
->node_num
;
941 done_msg
.dead_node
= dead_node
;
942 mlog(0, "sending DATA DONE message to %u, "
943 "my node=%u, dead node=%u\n", send_to
, done_msg
.node_idx
,
946 ret
= o2net_send_message(DLM_RECO_DATA_DONE_MSG
, dlm
->key
, &done_msg
,
947 sizeof(done_msg
), send_to
, &tmpret
);
949 if (!dlm_is_host_down(ret
)) {
951 mlog(ML_ERROR
, "%s: unknown error sending data-done "
952 "to %u\n", dlm
->name
, send_to
);
961 int dlm_reco_data_done_handler(struct o2net_msg
*msg
, u32 len
, void *data
)
963 struct dlm_ctxt
*dlm
= data
;
964 struct dlm_reco_data_done
*done
= (struct dlm_reco_data_done
*)msg
->buf
;
965 struct list_head
*iter
;
966 struct dlm_reco_node_data
*ndata
= NULL
;
972 mlog(0, "got DATA DONE: dead_node=%u, reco.dead_node=%u, "
973 "node_idx=%u, this node=%u\n", done
->dead_node
,
974 dlm
->reco
.dead_node
, done
->node_idx
, dlm
->node_num
);
976 mlog_bug_on_msg((done
->dead_node
!= dlm
->reco
.dead_node
),
977 "Got DATA DONE: dead_node=%u, reco.dead_node=%u, "
978 "node_idx=%u, this node=%u\n", done
->dead_node
,
979 dlm
->reco
.dead_node
, done
->node_idx
, dlm
->node_num
);
981 spin_lock(&dlm_reco_state_lock
);
982 list_for_each(iter
, &dlm
->reco
.node_data
) {
983 ndata
= list_entry (iter
, struct dlm_reco_node_data
, list
);
984 if (ndata
->node_num
!= done
->node_idx
)
987 switch (ndata
->state
) {
988 /* should have moved beyond INIT but not to FINALIZE yet */
989 case DLM_RECO_NODE_DATA_INIT
:
990 case DLM_RECO_NODE_DATA_DEAD
:
991 case DLM_RECO_NODE_DATA_FINALIZE_SENT
:
992 mlog(ML_ERROR
, "bad ndata state for node %u:"
993 " state=%d\n", ndata
->node_num
,
997 /* these states are possible at this point, anywhere along
998 * the line of recovery */
999 case DLM_RECO_NODE_DATA_DONE
:
1000 case DLM_RECO_NODE_DATA_RECEIVING
:
1001 case DLM_RECO_NODE_DATA_REQUESTED
:
1002 case DLM_RECO_NODE_DATA_REQUESTING
:
1003 mlog(0, "node %u is DONE sending "
1007 ndata
->state
= DLM_RECO_NODE_DATA_DONE
;
1012 spin_unlock(&dlm_reco_state_lock
);
1014 /* wake the recovery thread, some node is done */
1016 dlm_kick_recovery_thread(dlm
);
1019 mlog(ML_ERROR
, "failed to find recovery node data for node "
1020 "%u\n", done
->node_idx
);
1023 mlog(0, "leaving reco data done handler, ret=%d\n", ret
);
1027 static void dlm_move_reco_locks_to_list(struct dlm_ctxt
*dlm
,
1028 struct list_head
*list
,
1031 struct dlm_lock_resource
*res
;
1032 struct list_head
*iter
, *iter2
;
1033 struct dlm_lock
*lock
;
1035 spin_lock(&dlm
->spinlock
);
1036 list_for_each_safe(iter
, iter2
, &dlm
->reco
.resources
) {
1037 res
= list_entry (iter
, struct dlm_lock_resource
, recovering
);
1038 /* always prune any $RECOVERY entries for dead nodes,
1039 * otherwise hangs can occur during later recovery */
1040 if (dlm_is_recovery_lock(res
->lockname
.name
,
1041 res
->lockname
.len
)) {
1042 spin_lock(&res
->spinlock
);
1043 list_for_each_entry(lock
, &res
->granted
, list
) {
1044 if (lock
->ml
.node
== dead_node
) {
1045 mlog(0, "AHA! there was "
1046 "a $RECOVERY lock for dead "
1048 dead_node
, dlm
->name
);
1049 list_del_init(&lock
->list
);
1054 spin_unlock(&res
->spinlock
);
1058 if (res
->owner
== dead_node
) {
1059 mlog(0, "found lockres owned by dead node while "
1060 "doing recovery for node %u. sending it.\n",
1062 list_move_tail(&res
->recovering
, list
);
1063 } else if (res
->owner
== DLM_LOCK_RES_OWNER_UNKNOWN
) {
1064 mlog(0, "found UNKNOWN owner while doing recovery "
1065 "for node %u. sending it.\n", dead_node
);
1066 list_move_tail(&res
->recovering
, list
);
1069 spin_unlock(&dlm
->spinlock
);
1072 static inline int dlm_num_locks_in_lockres(struct dlm_lock_resource
*res
)
1074 int total_locks
= 0;
1075 struct list_head
*iter
, *queue
= &res
->granted
;
1078 for (i
=0; i
<3; i
++) {
1079 list_for_each(iter
, queue
)
1087 static int dlm_send_mig_lockres_msg(struct dlm_ctxt
*dlm
,
1088 struct dlm_migratable_lockres
*mres
,
1090 struct dlm_lock_resource
*res
,
1093 u64 mig_cookie
= be64_to_cpu(mres
->mig_cookie
);
1094 int mres_total_locks
= be32_to_cpu(mres
->total_locks
);
1095 int sz
, ret
= 0, status
= 0;
1096 u8 orig_flags
= mres
->flags
,
1097 orig_master
= mres
->master
;
1099 BUG_ON(mres
->num_locks
> DLM_MAX_MIGRATABLE_LOCKS
);
1100 if (!mres
->num_locks
)
1103 sz
= sizeof(struct dlm_migratable_lockres
) +
1104 (mres
->num_locks
* sizeof(struct dlm_migratable_lock
));
1106 /* add an all-done flag if we reached the last lock */
1107 orig_flags
= mres
->flags
;
1108 BUG_ON(total_locks
> mres_total_locks
);
1109 if (total_locks
== mres_total_locks
)
1110 mres
->flags
|= DLM_MRES_ALL_DONE
;
1113 ret
= o2net_send_message(DLM_MIG_LOCKRES_MSG
, dlm
->key
, mres
,
1114 sz
, send_to
, &status
);
1116 /* XXX: negative status is not handled.
1117 * this will end up killing this node. */
1120 /* might get an -ENOMEM back here */
1125 if (ret
== -EFAULT
) {
1126 mlog(ML_ERROR
, "node %u told me to kill "
1127 "myself!\n", send_to
);
1133 /* zero and reinit the message buffer */
1134 dlm_init_migratable_lockres(mres
, res
->lockname
.name
,
1135 res
->lockname
.len
, mres_total_locks
,
1136 mig_cookie
, orig_flags
, orig_master
);
1140 static void dlm_init_migratable_lockres(struct dlm_migratable_lockres
*mres
,
1141 const char *lockname
, int namelen
,
1142 int total_locks
, u64 cookie
,
1143 u8 flags
, u8 master
)
1145 /* mres here is one full page */
1146 memset(mres
, 0, PAGE_SIZE
);
1147 mres
->lockname_len
= namelen
;
1148 memcpy(mres
->lockname
, lockname
, namelen
);
1149 mres
->num_locks
= 0;
1150 mres
->total_locks
= cpu_to_be32(total_locks
);
1151 mres
->mig_cookie
= cpu_to_be64(cookie
);
1152 mres
->flags
= flags
;
1153 mres
->master
= master
;
1157 /* returns 1 if this lock fills the network structure,
1159 static int dlm_add_lock_to_array(struct dlm_lock
*lock
,
1160 struct dlm_migratable_lockres
*mres
, int queue
)
1162 struct dlm_migratable_lock
*ml
;
1163 int lock_num
= mres
->num_locks
;
1165 ml
= &(mres
->ml
[lock_num
]);
1166 ml
->cookie
= lock
->ml
.cookie
;
1167 ml
->type
= lock
->ml
.type
;
1168 ml
->convert_type
= lock
->ml
.convert_type
;
1169 ml
->highest_blocked
= lock
->ml
.highest_blocked
;
1172 ml
->flags
= lock
->lksb
->flags
;
1173 /* send our current lvb */
1174 if (ml
->type
== LKM_EXMODE
||
1175 ml
->type
== LKM_PRMODE
) {
1176 /* if it is already set, this had better be a PR
1177 * and it has to match */
1178 if (!dlm_lvb_is_empty(mres
->lvb
) &&
1179 (ml
->type
== LKM_EXMODE
||
1180 memcmp(mres
->lvb
, lock
->lksb
->lvb
, DLM_LVB_LEN
))) {
1181 mlog(ML_ERROR
, "mismatched lvbs!\n");
1182 __dlm_print_one_lock_resource(lock
->lockres
);
1185 memcpy(mres
->lvb
, lock
->lksb
->lvb
, DLM_LVB_LEN
);
1188 ml
->node
= lock
->ml
.node
;
1190 /* we reached the max, send this network message */
1191 if (mres
->num_locks
== DLM_MAX_MIGRATABLE_LOCKS
)
1197 int dlm_send_one_lockres(struct dlm_ctxt
*dlm
, struct dlm_lock_resource
*res
,
1198 struct dlm_migratable_lockres
*mres
,
1199 u8 send_to
, u8 flags
)
1201 struct list_head
*queue
, *iter
;
1204 struct dlm_lock
*lock
;
1207 BUG_ON(!(flags
& (DLM_MRES_RECOVERY
|DLM_MRES_MIGRATION
)));
1209 mlog(0, "sending to %u\n", send_to
);
1211 total_locks
= dlm_num_locks_in_lockres(res
);
1212 if (total_locks
> DLM_MAX_MIGRATABLE_LOCKS
) {
1213 /* rare, but possible */
1214 mlog(0, "argh. lockres has %d locks. this will "
1215 "require more than one network packet to "
1216 "migrate\n", total_locks
);
1217 mig_cookie
= dlm_get_next_mig_cookie();
1220 dlm_init_migratable_lockres(mres
, res
->lockname
.name
,
1221 res
->lockname
.len
, total_locks
,
1222 mig_cookie
, flags
, res
->owner
);
1225 for (i
=DLM_GRANTED_LIST
; i
<=DLM_BLOCKED_LIST
; i
++) {
1226 queue
= dlm_list_idx_to_ptr(res
, i
);
1227 list_for_each(iter
, queue
) {
1228 lock
= list_entry (iter
, struct dlm_lock
, list
);
1230 /* add another lock. */
1232 if (!dlm_add_lock_to_array(lock
, mres
, i
))
1235 /* this filled the lock message,
1236 * we must send it immediately. */
1237 ret
= dlm_send_mig_lockres_msg(dlm
, mres
, send_to
,
1243 /* flush any remaining locks */
1244 ret
= dlm_send_mig_lockres_msg(dlm
, mres
, send_to
, res
, total_locks
);
1250 mlog(ML_ERROR
, "%s: dlm_send_mig_lockres_msg returned %d\n",
1252 if (!dlm_is_host_down(ret
))
1254 mlog(0, "%s: node %u went down while sending %s "
1255 "lockres %.*s\n", dlm
->name
, send_to
,
1256 flags
& DLM_MRES_RECOVERY
? "recovery" : "migration",
1257 res
->lockname
.len
, res
->lockname
.name
);
1264 * this message will contain no more than one page worth of
1265 * recovery data, and it will work on only one lockres.
1266 * there may be many locks in this page, and we may need to wait
1267 * for additional packets to complete all the locks (rare, but
1271 * NOTE: the allocation error cases here are scary
1272 * we really cannot afford to fail an alloc in recovery
1273 * do we spin? returning an error only delays the problem really
1276 int dlm_mig_lockres_handler(struct o2net_msg
*msg
, u32 len
, void *data
)
1278 struct dlm_ctxt
*dlm
= data
;
1279 struct dlm_migratable_lockres
*mres
=
1280 (struct dlm_migratable_lockres
*)msg
->buf
;
1284 struct dlm_work_item
*item
= NULL
;
1285 struct dlm_lock_resource
*res
= NULL
;
1290 BUG_ON(!(mres
->flags
& (DLM_MRES_RECOVERY
|DLM_MRES_MIGRATION
)));
1292 real_master
= mres
->master
;
1293 if (real_master
== DLM_LOCK_RES_OWNER_UNKNOWN
) {
1294 /* cannot migrate a lockres with no master */
1295 BUG_ON(!(mres
->flags
& DLM_MRES_RECOVERY
));
1298 mlog(0, "%s message received from node %u\n",
1299 (mres
->flags
& DLM_MRES_RECOVERY
) ?
1300 "recovery" : "migration", mres
->master
);
1301 if (mres
->flags
& DLM_MRES_ALL_DONE
)
1302 mlog(0, "all done flag. all lockres data received!\n");
1305 buf
= kmalloc(be16_to_cpu(msg
->data_len
), GFP_NOFS
);
1306 item
= kcalloc(1, sizeof(*item
), GFP_NOFS
);
1310 /* lookup the lock to see if we have a secondary queue for this
1311 * already... just add the locks in and this will have its owner
1312 * and RECOVERY flag changed when it completes. */
1313 res
= dlm_lookup_lockres(dlm
, mres
->lockname
, mres
->lockname_len
);
1315 /* this will get a ref on res */
1316 /* mark it as recovering/migrating and hash it */
1317 spin_lock(&res
->spinlock
);
1318 if (mres
->flags
& DLM_MRES_RECOVERY
) {
1319 res
->state
|= DLM_LOCK_RES_RECOVERING
;
1321 if (res
->state
& DLM_LOCK_RES_MIGRATING
) {
1322 /* this is at least the second
1323 * lockres message */
1324 mlog(0, "lock %.*s is already migrating\n",
1327 } else if (res
->state
& DLM_LOCK_RES_RECOVERING
) {
1328 /* caller should BUG */
1329 mlog(ML_ERROR
, "node is attempting to migrate "
1330 "lock %.*s, but marked as recovering!\n",
1331 mres
->lockname_len
, mres
->lockname
);
1333 spin_unlock(&res
->spinlock
);
1336 res
->state
|= DLM_LOCK_RES_MIGRATING
;
1338 spin_unlock(&res
->spinlock
);
1340 /* need to allocate, just like if it was
1341 * mastered here normally */
1342 res
= dlm_new_lockres(dlm
, mres
->lockname
, mres
->lockname_len
);
1346 /* to match the ref that we would have gotten if
1347 * dlm_lookup_lockres had succeeded */
1348 dlm_lockres_get(res
);
1350 /* mark it as recovering/migrating and hash it */
1351 if (mres
->flags
& DLM_MRES_RECOVERY
)
1352 res
->state
|= DLM_LOCK_RES_RECOVERING
;
1354 res
->state
|= DLM_LOCK_RES_MIGRATING
;
1356 spin_lock(&dlm
->spinlock
);
1357 __dlm_insert_lockres(dlm
, res
);
1358 spin_unlock(&dlm
->spinlock
);
1360 /* now that the new lockres is inserted,
1361 * make it usable by other processes */
1362 spin_lock(&res
->spinlock
);
1363 res
->state
&= ~DLM_LOCK_RES_IN_PROGRESS
;
1364 spin_unlock(&res
->spinlock
);
1366 /* add an extra ref for just-allocated lockres
1367 * otherwise the lockres will be purged immediately */
1368 dlm_lockres_get(res
);
1372 /* at this point we have allocated everything we need,
1373 * and we have a hashed lockres with an extra ref and
1374 * the proper res->state flags. */
1376 if (mres
->master
== DLM_LOCK_RES_OWNER_UNKNOWN
) {
1377 /* migration cannot have an unknown master */
1378 BUG_ON(!(mres
->flags
& DLM_MRES_RECOVERY
));
1379 mlog(0, "recovery has passed me a lockres with an "
1380 "unknown owner.. will need to requery: "
1381 "%.*s\n", mres
->lockname_len
, mres
->lockname
);
1383 spin_lock(&res
->spinlock
);
1384 dlm_change_lockres_owner(dlm
, res
, dlm
->node_num
);
1385 spin_unlock(&res
->spinlock
);
1388 /* queue up work for dlm_mig_lockres_worker */
1389 dlm_grab(dlm
); /* get an extra ref for the work item */
1390 memcpy(buf
, msg
->buf
, be16_to_cpu(msg
->data_len
)); /* copy the whole message */
1391 dlm_init_work_item(dlm
, item
, dlm_mig_lockres_worker
, buf
);
1392 item
->u
.ml
.lockres
= res
; /* already have a ref */
1393 item
->u
.ml
.real_master
= real_master
;
1394 spin_lock(&dlm
->work_lock
);
1395 list_add_tail(&item
->list
, &dlm
->work_list
);
1396 spin_unlock(&dlm
->work_lock
);
1397 schedule_work(&dlm
->dispatched_work
);
1413 static void dlm_mig_lockres_worker(struct dlm_work_item
*item
, void *data
)
1415 struct dlm_ctxt
*dlm
= data
;
1416 struct dlm_migratable_lockres
*mres
;
1418 struct dlm_lock_resource
*res
;
1422 mres
= (struct dlm_migratable_lockres
*)data
;
1424 res
= item
->u
.ml
.lockres
;
1425 real_master
= item
->u
.ml
.real_master
;
1427 if (real_master
== DLM_LOCK_RES_OWNER_UNKNOWN
) {
1428 /* this case is super-rare. only occurs if
1429 * node death happens during migration. */
1431 ret
= dlm_lockres_master_requery(dlm
, res
, &real_master
);
1433 mlog(0, "dlm_lockres_master_requery ret=%d\n",
1437 if (real_master
== DLM_LOCK_RES_OWNER_UNKNOWN
) {
1438 mlog(0, "lockres %.*s not claimed. "
1439 "this node will take it.\n",
1440 res
->lockname
.len
, res
->lockname
.name
);
1442 mlog(0, "master needs to respond to sender "
1443 "that node %u still owns %.*s\n",
1444 real_master
, res
->lockname
.len
,
1445 res
->lockname
.name
);
1446 /* cannot touch this lockres */
1451 ret
= dlm_process_recovery_data(dlm
, res
, mres
);
1453 mlog(0, "dlm_process_recovery_data returned %d\n", ret
);
1455 mlog(0, "dlm_process_recovery_data succeeded\n");
1457 if ((mres
->flags
& (DLM_MRES_MIGRATION
|DLM_MRES_ALL_DONE
)) ==
1458 (DLM_MRES_MIGRATION
|DLM_MRES_ALL_DONE
)) {
1459 ret
= dlm_finish_migration(dlm
, res
, mres
->master
);
1471 int dlm_lockres_master_requery(struct dlm_ctxt
*dlm
,
1472 struct dlm_lock_resource
*res
, u8
*real_master
)
1474 struct dlm_node_iter iter
;
1478 *real_master
= DLM_LOCK_RES_OWNER_UNKNOWN
;
1480 /* we only reach here if one of the two nodes in a
1481 * migration died while the migration was in progress.
1482 * at this point we need to requery the master. we
1483 * know that the new_master got as far as creating
1484 * an mle on at least one node, but we do not know
1485 * if any nodes had actually cleared the mle and set
1486 * the master to the new_master. the old master
1487 * is supposed to set the owner to UNKNOWN in the
1488 * event of a new_master death, so the only possible
1489 * responses that we can get from nodes here are
1490 * that the master is new_master, or that the master
1492 * if all nodes come back with UNKNOWN then we know
1493 * the lock needs remastering here.
1494 * if any node comes back with a valid master, check
1495 * to see if that master is the one that we are
1496 * recovering. if so, then the new_master died and
1497 * we need to remaster this lock. if not, then the
1498 * new_master survived and that node will respond to
1499 * other nodes about the owner.
1500 * if there is an owner, this node needs to dump this
1501 * lockres and alert the sender that this lockres
1503 spin_lock(&dlm
->spinlock
);
1504 dlm_node_iter_init(dlm
->domain_map
, &iter
);
1505 spin_unlock(&dlm
->spinlock
);
1507 while ((nodenum
= dlm_node_iter_next(&iter
)) >= 0) {
1508 /* do not send to self */
1509 if (nodenum
== dlm
->node_num
)
1511 ret
= dlm_do_master_requery(dlm
, res
, nodenum
, real_master
);
1514 if (!dlm_is_host_down(ret
))
1516 /* host is down, so answer for that node would be
1517 * DLM_LOCK_RES_OWNER_UNKNOWN. continue. */
1519 if (*real_master
!= DLM_LOCK_RES_OWNER_UNKNOWN
) {
1520 mlog(0, "lock master is %u\n", *real_master
);
1528 int dlm_do_master_requery(struct dlm_ctxt
*dlm
, struct dlm_lock_resource
*res
,
1529 u8 nodenum
, u8
*real_master
)
1532 struct dlm_master_requery req
;
1533 int status
= DLM_LOCK_RES_OWNER_UNKNOWN
;
1535 memset(&req
, 0, sizeof(req
));
1536 req
.node_idx
= dlm
->node_num
;
1537 req
.namelen
= res
->lockname
.len
;
1538 memcpy(req
.name
, res
->lockname
.name
, res
->lockname
.len
);
1540 ret
= o2net_send_message(DLM_MASTER_REQUERY_MSG
, dlm
->key
,
1541 &req
, sizeof(req
), nodenum
, &status
);
1542 /* XXX: negative status not handled properly here. */
1547 BUG_ON(status
> DLM_LOCK_RES_OWNER_UNKNOWN
);
1548 *real_master
= (u8
) (status
& 0xff);
1549 mlog(0, "node %u responded to master requery with %u\n",
1550 nodenum
, *real_master
);
1557 /* this function cannot error, so unless the sending
1558 * or receiving of the message failed, the owner can
1560 int dlm_master_requery_handler(struct o2net_msg
*msg
, u32 len
, void *data
)
1562 struct dlm_ctxt
*dlm
= data
;
1563 struct dlm_master_requery
*req
= (struct dlm_master_requery
*)msg
->buf
;
1564 struct dlm_lock_resource
*res
= NULL
;
1566 int master
= DLM_LOCK_RES_OWNER_UNKNOWN
;
1567 u32 flags
= DLM_ASSERT_MASTER_REQUERY
;
1569 if (!dlm_grab(dlm
)) {
1570 /* since the domain has gone away on this
1571 * node, the proper response is UNKNOWN */
1575 hash
= dlm_lockid_hash(req
->name
, req
->namelen
);
1577 spin_lock(&dlm
->spinlock
);
1578 res
= __dlm_lookup_lockres(dlm
, req
->name
, req
->namelen
, hash
);
1580 spin_lock(&res
->spinlock
);
1581 master
= res
->owner
;
1582 if (master
== dlm
->node_num
) {
1583 int ret
= dlm_dispatch_assert_master(dlm
, res
,
1586 mlog_errno(-ENOMEM
);
1591 spin_unlock(&res
->spinlock
);
1593 spin_unlock(&dlm
->spinlock
);
1599 static inline struct list_head
*
1600 dlm_list_num_to_pointer(struct dlm_lock_resource
*res
, int list_num
)
1602 struct list_head
*ret
;
1603 BUG_ON(list_num
< 0);
1604 BUG_ON(list_num
> 2);
1605 ret
= &(res
->granted
);
1609 /* TODO: do ast flush business
1610 * TODO: do MIGRATING and RECOVERING spinning
1614 * NOTE about in-flight requests during migration:
1616 * Before attempting the migrate, the master has marked the lockres as
1617 * MIGRATING and then flushed all of its pending ASTS. So any in-flight
1618 * requests either got queued before the MIGRATING flag got set, in which
1619 * case the lock data will reflect the change and a return message is on
1620 * the way, or the request failed to get in before MIGRATING got set. In
1621 * this case, the caller will be told to spin and wait for the MIGRATING
1622 * flag to be dropped, then recheck the master.
1623 * This holds true for the convert, cancel and unlock cases, and since lvb
1624 * updates are tied to these same messages, it applies to lvb updates as
1625 * well. For the lock case, there is no way a lock can be on the master
1626 * queue and not be on the secondary queue since the lock is always added
1627 * locally first. This means that the new target node will never be sent
1628 * a lock that he doesn't already have on the list.
1629 * In total, this means that the local lock is correct and should not be
1630 * updated to match the one sent by the master. Any messages sent back
1631 * from the master before the MIGRATING flag will bring the lock properly
1632 * up-to-date, and the change will be ordered properly for the waiter.
1633 * We will *not* attempt to modify the lock underneath the waiter.
1636 static int dlm_process_recovery_data(struct dlm_ctxt
*dlm
,
1637 struct dlm_lock_resource
*res
,
1638 struct dlm_migratable_lockres
*mres
)
1640 struct dlm_migratable_lock
*ml
;
1641 struct list_head
*queue
;
1642 struct dlm_lock
*newlock
= NULL
;
1643 struct dlm_lockstatus
*lksb
= NULL
;
1646 struct list_head
*iter
;
1647 struct dlm_lock
*lock
= NULL
;
1649 mlog(0, "running %d locks for this lockres\n", mres
->num_locks
);
1650 for (i
=0; i
<mres
->num_locks
; i
++) {
1651 ml
= &(mres
->ml
[i
]);
1652 BUG_ON(ml
->highest_blocked
!= LKM_IVMODE
);
1656 queue
= dlm_list_num_to_pointer(res
, ml
->list
);
1658 /* if the lock is for the local node it needs to
1659 * be moved to the proper location within the queue.
1660 * do not allocate a new lock structure. */
1661 if (ml
->node
== dlm
->node_num
) {
1662 /* MIGRATION ONLY! */
1663 BUG_ON(!(mres
->flags
& DLM_MRES_MIGRATION
));
1665 spin_lock(&res
->spinlock
);
1666 list_for_each(iter
, queue
) {
1667 lock
= list_entry (iter
, struct dlm_lock
, list
);
1668 if (lock
->ml
.cookie
!= ml
->cookie
)
1674 /* lock is always created locally first, and
1675 * destroyed locally last. it must be on the list */
1678 mlog(ML_ERROR
, "could not find local lock "
1679 "with cookie %u:%llu!\n",
1680 dlm_get_lock_cookie_node(c
),
1681 dlm_get_lock_cookie_seq(c
));
1684 BUG_ON(lock
->ml
.node
!= ml
->node
);
1686 /* see NOTE above about why we do not update
1687 * to match the master here */
1689 /* move the lock to its proper place */
1690 /* do not alter lock refcount. switching lists. */
1691 list_move_tail(&lock
->list
, queue
);
1692 spin_unlock(&res
->spinlock
);
1694 mlog(0, "just reordered a local lock!\n");
1698 /* lock is for another node. */
1699 newlock
= dlm_new_lock(ml
->type
, ml
->node
,
1700 be64_to_cpu(ml
->cookie
), NULL
);
1705 lksb
= newlock
->lksb
;
1706 dlm_lock_attach_lockres(newlock
, res
);
1708 if (ml
->convert_type
!= LKM_IVMODE
) {
1709 BUG_ON(queue
!= &res
->converting
);
1710 newlock
->ml
.convert_type
= ml
->convert_type
;
1712 lksb
->flags
|= (ml
->flags
&
1713 (DLM_LKSB_PUT_LVB
|DLM_LKSB_GET_LVB
));
1715 if (ml
->type
== LKM_NLMODE
)
1718 if (!dlm_lvb_is_empty(mres
->lvb
)) {
1719 if (lksb
->flags
& DLM_LKSB_PUT_LVB
) {
1720 /* other node was trying to update
1721 * lvb when node died. recreate the
1722 * lksb with the updated lvb. */
1723 memcpy(lksb
->lvb
, mres
->lvb
, DLM_LVB_LEN
);
1724 /* the lock resource lvb update must happen
1725 * NOW, before the spinlock is dropped.
1726 * we no longer wait for the AST to update
1728 memcpy(res
->lvb
, mres
->lvb
, DLM_LVB_LEN
);
1730 /* otherwise, the node is sending its
1731 * most recent valid lvb info */
1732 BUG_ON(ml
->type
!= LKM_EXMODE
&&
1733 ml
->type
!= LKM_PRMODE
);
1734 if (!dlm_lvb_is_empty(res
->lvb
) &&
1735 (ml
->type
== LKM_EXMODE
||
1736 memcmp(res
->lvb
, mres
->lvb
, DLM_LVB_LEN
))) {
1738 mlog(ML_ERROR
, "%s:%.*s: received bad "
1739 "lvb! type=%d\n", dlm
->name
,
1741 res
->lockname
.name
, ml
->type
);
1742 printk("lockres lvb=[");
1743 for (i
=0; i
<DLM_LVB_LEN
; i
++)
1744 printk("%02x", res
->lvb
[i
]);
1745 printk("]\nmigrated lvb=[");
1746 for (i
=0; i
<DLM_LVB_LEN
; i
++)
1747 printk("%02x", mres
->lvb
[i
]);
1749 dlm_print_one_lock_resource(res
);
1752 memcpy(res
->lvb
, mres
->lvb
, DLM_LVB_LEN
);
1758 * wrt lock queue ordering and recovery:
1759 * 1. order of locks on granted queue is
1761 * 2. order of locks on converting queue is
1762 * LOST with the node death. sorry charlie.
1763 * 3. order of locks on the blocked queue is
1765 * order of locks does not affect integrity, it
1766 * just means that a lock request may get pushed
1767 * back in line as a result of the node death.
1768 * also note that for a given node the lock order
1769 * for its secondary queue locks is preserved
1770 * relative to each other, but clearly *not*
1771 * preserved relative to locks from other nodes.
1774 spin_lock(&res
->spinlock
);
1775 list_for_each_entry(lock
, queue
, list
) {
1776 if (lock
->ml
.cookie
== ml
->cookie
) {
1777 u64 c
= lock
->ml
.cookie
;
1778 mlog(ML_ERROR
, "%s:%.*s: %u:%llu: lock already "
1779 "exists on this lockres!\n", dlm
->name
,
1780 res
->lockname
.len
, res
->lockname
.name
,
1781 dlm_get_lock_cookie_node(c
),
1782 dlm_get_lock_cookie_seq(c
));
1784 mlog(ML_NOTICE
, "sent lock: type=%d, conv=%d, "
1785 "node=%u, cookie=%u:%llu, queue=%d\n",
1786 ml
->type
, ml
->convert_type
, ml
->node
,
1787 dlm_get_lock_cookie_node(ml
->cookie
),
1788 dlm_get_lock_cookie_seq(ml
->cookie
),
1791 __dlm_print_one_lock_resource(res
);
1797 dlm_lock_get(newlock
);
1798 list_add_tail(&newlock
->list
, queue
);
1800 spin_unlock(&res
->spinlock
);
1802 mlog(0, "done running all the locks\n");
1808 dlm_lock_put(newlock
);
1815 void dlm_move_lockres_to_recovery_list(struct dlm_ctxt
*dlm
,
1816 struct dlm_lock_resource
*res
)
1819 struct list_head
*queue
, *iter
, *iter2
;
1820 struct dlm_lock
*lock
;
1822 res
->state
|= DLM_LOCK_RES_RECOVERING
;
1823 if (!list_empty(&res
->recovering
)) {
1825 "Recovering res %s:%.*s, is already on recovery list!\n",
1826 dlm
->name
, res
->lockname
.len
, res
->lockname
.name
);
1827 list_del_init(&res
->recovering
);
1829 /* We need to hold a reference while on the recovery list */
1830 dlm_lockres_get(res
);
1831 list_add_tail(&res
->recovering
, &dlm
->reco
.resources
);
1833 /* find any pending locks and put them back on proper list */
1834 for (i
=DLM_BLOCKED_LIST
; i
>=DLM_GRANTED_LIST
; i
--) {
1835 queue
= dlm_list_idx_to_ptr(res
, i
);
1836 list_for_each_safe(iter
, iter2
, queue
) {
1837 lock
= list_entry (iter
, struct dlm_lock
, list
);
1839 if (lock
->convert_pending
) {
1840 /* move converting lock back to granted */
1841 BUG_ON(i
!= DLM_CONVERTING_LIST
);
1842 mlog(0, "node died with convert pending "
1843 "on %.*s. move back to granted list.\n",
1844 res
->lockname
.len
, res
->lockname
.name
);
1845 dlm_revert_pending_convert(res
, lock
);
1846 lock
->convert_pending
= 0;
1847 } else if (lock
->lock_pending
) {
1848 /* remove pending lock requests completely */
1849 BUG_ON(i
!= DLM_BLOCKED_LIST
);
1850 mlog(0, "node died with lock pending "
1851 "on %.*s. remove from blocked list and skip.\n",
1852 res
->lockname
.len
, res
->lockname
.name
);
1853 /* lock will be floating until ref in
1854 * dlmlock_remote is freed after the network
1855 * call returns. ok for it to not be on any
1856 * list since no ast can be called
1857 * (the master is dead). */
1858 dlm_revert_pending_lock(res
, lock
);
1859 lock
->lock_pending
= 0;
1860 } else if (lock
->unlock_pending
) {
1861 /* if an unlock was in progress, treat as
1862 * if this had completed successfully
1863 * before sending this lock state to the
1864 * new master. note that the dlm_unlock
1865 * call is still responsible for calling
1866 * the unlockast. that will happen after
1867 * the network call times out. for now,
1868 * just move lists to prepare the new
1869 * recovery master. */
1870 BUG_ON(i
!= DLM_GRANTED_LIST
);
1871 mlog(0, "node died with unlock pending "
1872 "on %.*s. remove from blocked list and skip.\n",
1873 res
->lockname
.len
, res
->lockname
.name
);
1874 dlm_commit_pending_unlock(res
, lock
);
1875 lock
->unlock_pending
= 0;
1876 } else if (lock
->cancel_pending
) {
1877 /* if a cancel was in progress, treat as
1878 * if this had completed successfully
1879 * before sending this lock state to the
1881 BUG_ON(i
!= DLM_CONVERTING_LIST
);
1882 mlog(0, "node died with cancel pending "
1883 "on %.*s. move back to granted list.\n",
1884 res
->lockname
.len
, res
->lockname
.name
);
1885 dlm_commit_pending_cancel(res
, lock
);
1886 lock
->cancel_pending
= 0;
1895 /* removes all recovered locks from the recovery list.
1896 * sets the res->owner to the new master.
1897 * unsets the RECOVERY flag and wakes waiters. */
1898 static void dlm_finish_local_lockres_recovery(struct dlm_ctxt
*dlm
,
1899 u8 dead_node
, u8 new_master
)
1902 struct list_head
*iter
, *iter2
;
1903 struct hlist_node
*hash_iter
;
1904 struct hlist_head
*bucket
;
1906 struct dlm_lock_resource
*res
;
1910 assert_spin_locked(&dlm
->spinlock
);
1912 list_for_each_safe(iter
, iter2
, &dlm
->reco
.resources
) {
1913 res
= list_entry (iter
, struct dlm_lock_resource
, recovering
);
1914 if (res
->owner
== dead_node
) {
1915 list_del_init(&res
->recovering
);
1916 spin_lock(&res
->spinlock
);
1917 dlm_change_lockres_owner(dlm
, res
, new_master
);
1918 res
->state
&= ~DLM_LOCK_RES_RECOVERING
;
1919 if (!__dlm_lockres_unused(res
))
1920 __dlm_dirty_lockres(dlm
, res
);
1921 spin_unlock(&res
->spinlock
);
1923 dlm_lockres_put(res
);
1927 /* this will become unnecessary eventually, but
1928 * for now we need to run the whole hash, clear
1929 * the RECOVERING state and set the owner
1931 for (i
= 0; i
< DLM_HASH_BUCKETS
; i
++) {
1932 bucket
= dlm_lockres_hash(dlm
, i
);
1933 hlist_for_each_entry(res
, hash_iter
, bucket
, hash_node
) {
1934 if (res
->state
& DLM_LOCK_RES_RECOVERING
) {
1935 if (res
->owner
== dead_node
) {
1936 mlog(0, "(this=%u) res %.*s owner=%u "
1937 "was not on recovering list, but "
1938 "clearing state anyway\n",
1939 dlm
->node_num
, res
->lockname
.len
,
1940 res
->lockname
.name
, new_master
);
1941 } else if (res
->owner
== dlm
->node_num
) {
1942 mlog(0, "(this=%u) res %.*s owner=%u "
1943 "was not on recovering list, "
1944 "owner is THIS node, clearing\n",
1945 dlm
->node_num
, res
->lockname
.len
,
1946 res
->lockname
.name
, new_master
);
1950 if (!list_empty(&res
->recovering
)) {
1951 mlog(0, "%s:%.*s: lockres was "
1952 "marked RECOVERING, owner=%u\n",
1953 dlm
->name
, res
->lockname
.len
,
1954 res
->lockname
.name
, res
->owner
);
1955 list_del_init(&res
->recovering
);
1956 dlm_lockres_put(res
);
1958 spin_lock(&res
->spinlock
);
1959 dlm_change_lockres_owner(dlm
, res
, new_master
);
1960 res
->state
&= ~DLM_LOCK_RES_RECOVERING
;
1961 if (!__dlm_lockres_unused(res
))
1962 __dlm_dirty_lockres(dlm
, res
);
1963 spin_unlock(&res
->spinlock
);
1970 static inline int dlm_lvb_needs_invalidation(struct dlm_lock
*lock
, int local
)
1973 if (lock
->ml
.type
!= LKM_EXMODE
&&
1974 lock
->ml
.type
!= LKM_PRMODE
)
1976 } else if (lock
->ml
.type
== LKM_EXMODE
)
1981 static void dlm_revalidate_lvb(struct dlm_ctxt
*dlm
,
1982 struct dlm_lock_resource
*res
, u8 dead_node
)
1984 struct list_head
*iter
, *queue
;
1985 struct dlm_lock
*lock
;
1986 int blank_lvb
= 0, local
= 0;
1990 assert_spin_locked(&dlm
->spinlock
);
1991 assert_spin_locked(&res
->spinlock
);
1993 if (res
->owner
== dlm
->node_num
)
1994 /* if this node owned the lockres, and if the dead node
1995 * had an EX when he died, blank out the lvb */
1996 search_node
= dead_node
;
1998 /* if this is a secondary lockres, and we had no EX or PR
1999 * locks granted, we can no longer trust the lvb */
2000 search_node
= dlm
->node_num
;
2001 local
= 1; /* check local state for valid lvb */
2004 for (i
=DLM_GRANTED_LIST
; i
<=DLM_CONVERTING_LIST
; i
++) {
2005 queue
= dlm_list_idx_to_ptr(res
, i
);
2006 list_for_each(iter
, queue
) {
2007 lock
= list_entry (iter
, struct dlm_lock
, list
);
2008 if (lock
->ml
.node
== search_node
) {
2009 if (dlm_lvb_needs_invalidation(lock
, local
)) {
2010 /* zero the lksb lvb and lockres lvb */
2012 memset(lock
->lksb
->lvb
, 0, DLM_LVB_LEN
);
2019 mlog(0, "clearing %.*s lvb, dead node %u had EX\n",
2020 res
->lockname
.len
, res
->lockname
.name
, dead_node
);
2021 memset(res
->lvb
, 0, DLM_LVB_LEN
);
2025 static void dlm_free_dead_locks(struct dlm_ctxt
*dlm
,
2026 struct dlm_lock_resource
*res
, u8 dead_node
)
2028 struct list_head
*iter
, *tmpiter
;
2029 struct dlm_lock
*lock
;
2031 /* this node is the lockres master:
2032 * 1) remove any stale locks for the dead node
2033 * 2) if the dead node had an EX when he died, blank out the lvb
2035 assert_spin_locked(&dlm
->spinlock
);
2036 assert_spin_locked(&res
->spinlock
);
2038 /* TODO: check pending_asts, pending_basts here */
2039 list_for_each_safe(iter
, tmpiter
, &res
->granted
) {
2040 lock
= list_entry (iter
, struct dlm_lock
, list
);
2041 if (lock
->ml
.node
== dead_node
) {
2042 list_del_init(&lock
->list
);
2046 list_for_each_safe(iter
, tmpiter
, &res
->converting
) {
2047 lock
= list_entry (iter
, struct dlm_lock
, list
);
2048 if (lock
->ml
.node
== dead_node
) {
2049 list_del_init(&lock
->list
);
2053 list_for_each_safe(iter
, tmpiter
, &res
->blocked
) {
2054 lock
= list_entry (iter
, struct dlm_lock
, list
);
2055 if (lock
->ml
.node
== dead_node
) {
2056 list_del_init(&lock
->list
);
2061 /* do not kick thread yet */
2062 __dlm_dirty_lockres(dlm
, res
);
2065 /* if this node is the recovery master, and there are no
2066 * locks for a given lockres owned by this node that are in
2067 * either PR or EX mode, zero out the lvb before requesting.
2072 static void dlm_do_local_recovery_cleanup(struct dlm_ctxt
*dlm
, u8 dead_node
)
2074 struct hlist_node
*iter
;
2075 struct dlm_lock_resource
*res
;
2077 struct hlist_head
*bucket
;
2078 struct dlm_lock
*lock
;
2081 /* purge any stale mles */
2082 dlm_clean_master_list(dlm
, dead_node
);
2085 * now clean up all lock resources. there are two rules:
2087 * 1) if the dead node was the master, move the lockres
2088 * to the recovering list. set the RECOVERING flag.
2089 * this lockres needs to be cleaned up before it can
2092 * 2) if this node was the master, remove all locks from
2093 * each of the lockres queues that were owned by the
2094 * dead node. once recovery finishes, the dlm thread
2095 * can be kicked again to see if any ASTs or BASTs
2096 * need to be fired as a result.
2098 for (i
= 0; i
< DLM_HASH_BUCKETS
; i
++) {
2099 bucket
= dlm_lockres_hash(dlm
, i
);
2100 hlist_for_each_entry(res
, iter
, bucket
, hash_node
) {
2101 /* always prune any $RECOVERY entries for dead nodes,
2102 * otherwise hangs can occur during later recovery */
2103 if (dlm_is_recovery_lock(res
->lockname
.name
,
2104 res
->lockname
.len
)) {
2105 spin_lock(&res
->spinlock
);
2106 list_for_each_entry(lock
, &res
->granted
, list
) {
2107 if (lock
->ml
.node
== dead_node
) {
2108 mlog(0, "AHA! there was "
2109 "a $RECOVERY lock for dead "
2111 dead_node
, dlm
->name
);
2112 list_del_init(&lock
->list
);
2117 spin_unlock(&res
->spinlock
);
2120 spin_lock(&res
->spinlock
);
2121 /* zero the lvb if necessary */
2122 dlm_revalidate_lvb(dlm
, res
, dead_node
);
2123 if (res
->owner
== dead_node
)
2124 dlm_move_lockres_to_recovery_list(dlm
, res
);
2125 else if (res
->owner
== dlm
->node_num
) {
2126 dlm_free_dead_locks(dlm
, res
, dead_node
);
2127 __dlm_lockres_calc_usage(dlm
, res
);
2129 spin_unlock(&res
->spinlock
);
2135 static void __dlm_hb_node_down(struct dlm_ctxt
*dlm
, int idx
)
2137 assert_spin_locked(&dlm
->spinlock
);
2139 if (dlm
->reco
.new_master
== idx
) {
2140 mlog(0, "%s: recovery master %d just died\n",
2142 if (dlm
->reco
.state
& DLM_RECO_STATE_FINALIZE
) {
2143 /* finalize1 was reached, so it is safe to clear
2144 * the new_master and dead_node. that recovery
2146 mlog(0, "%s: dead master %d had reached "
2147 "finalize1 state, clearing\n", dlm
->name
, idx
);
2148 dlm
->reco
.state
&= ~DLM_RECO_STATE_FINALIZE
;
2149 __dlm_reset_recovery(dlm
);
2153 /* check to see if the node is already considered dead */
2154 if (!test_bit(idx
, dlm
->live_nodes_map
)) {
2155 mlog(0, "for domain %s, node %d is already dead. "
2156 "another node likely did recovery already.\n",
2161 /* check to see if we do not care about this node */
2162 if (!test_bit(idx
, dlm
->domain_map
)) {
2163 /* This also catches the case that we get a node down
2164 * but haven't joined the domain yet. */
2165 mlog(0, "node %u already removed from domain!\n", idx
);
2169 clear_bit(idx
, dlm
->live_nodes_map
);
2171 /* Clean up join state on node death. */
2172 if (dlm
->joining_node
== idx
) {
2173 mlog(0, "Clearing join state for node %u\n", idx
);
2174 __dlm_set_joining_node(dlm
, DLM_LOCK_RES_OWNER_UNKNOWN
);
2177 /* make sure local cleanup occurs before the heartbeat events */
2178 if (!test_bit(idx
, dlm
->recovery_map
))
2179 dlm_do_local_recovery_cleanup(dlm
, idx
);
2181 /* notify anything attached to the heartbeat events */
2182 dlm_hb_event_notify_attached(dlm
, idx
, 0);
2184 mlog(0, "node %u being removed from domain map!\n", idx
);
2185 clear_bit(idx
, dlm
->domain_map
);
2186 /* wake up migration waiters if a node goes down.
2187 * perhaps later we can genericize this for other waiters. */
2188 wake_up(&dlm
->migration_wq
);
2190 if (test_bit(idx
, dlm
->recovery_map
))
2191 mlog(0, "domain %s, node %u already added "
2192 "to recovery map!\n", dlm
->name
, idx
);
2194 set_bit(idx
, dlm
->recovery_map
);
2197 void dlm_hb_node_down_cb(struct o2nm_node
*node
, int idx
, void *data
)
2199 struct dlm_ctxt
*dlm
= data
;
2204 spin_lock(&dlm
->spinlock
);
2205 __dlm_hb_node_down(dlm
, idx
);
2206 spin_unlock(&dlm
->spinlock
);
2211 void dlm_hb_node_up_cb(struct o2nm_node
*node
, int idx
, void *data
)
2213 struct dlm_ctxt
*dlm
= data
;
2218 spin_lock(&dlm
->spinlock
);
2219 set_bit(idx
, dlm
->live_nodes_map
);
2220 /* do NOT notify mle attached to the heartbeat events.
2221 * new nodes are not interesting in mastery until joined. */
2222 spin_unlock(&dlm
->spinlock
);
2227 static void dlm_reco_ast(void *astdata
)
2229 struct dlm_ctxt
*dlm
= astdata
;
2230 mlog(0, "ast for recovery lock fired!, this=%u, dlm=%s\n",
2231 dlm
->node_num
, dlm
->name
);
2233 static void dlm_reco_bast(void *astdata
, int blocked_type
)
2235 struct dlm_ctxt
*dlm
= astdata
;
2236 mlog(0, "bast for recovery lock fired!, this=%u, dlm=%s\n",
2237 dlm
->node_num
, dlm
->name
);
2239 static void dlm_reco_unlock_ast(void *astdata
, enum dlm_status st
)
2241 mlog(0, "unlockast for recovery lock fired!\n");
2245 * dlm_pick_recovery_master will continually attempt to use
2246 * dlmlock() on the special "$RECOVERY" lockres with the
2247 * LKM_NOQUEUE flag to get an EX. every thread that enters
2248 * this function on each node racing to become the recovery
2249 * master will not stop attempting this until either:
2250 * a) this node gets the EX (and becomes the recovery master),
2251 * or b) dlm->reco.new_master gets set to some nodenum
2252 * != O2NM_INVALID_NODE_NUM (another node will do the reco).
2253 * so each time a recovery master is needed, the entire cluster
2254 * will sync at this point. if the new master dies, that will
2255 * be detected in dlm_do_recovery */
2256 static int dlm_pick_recovery_master(struct dlm_ctxt
*dlm
)
2258 enum dlm_status ret
;
2259 struct dlm_lockstatus lksb
;
2260 int status
= -EINVAL
;
2262 mlog(0, "starting recovery of %s at %lu, dead=%u, this=%u\n",
2263 dlm
->name
, jiffies
, dlm
->reco
.dead_node
, dlm
->node_num
);
2265 memset(&lksb
, 0, sizeof(lksb
));
2267 ret
= dlmlock(dlm
, LKM_EXMODE
, &lksb
, LKM_NOQUEUE
|LKM_RECOVERY
,
2268 DLM_RECOVERY_LOCK_NAME
, dlm_reco_ast
, dlm
, dlm_reco_bast
);
2270 mlog(0, "%s: dlmlock($RECOVERY) returned %d, lksb=%d\n",
2271 dlm
->name
, ret
, lksb
.status
);
2273 if (ret
== DLM_NORMAL
) {
2274 mlog(0, "dlm=%s dlmlock says I got it (this=%u)\n",
2275 dlm
->name
, dlm
->node_num
);
2277 /* got the EX lock. check to see if another node
2278 * just became the reco master */
2279 if (dlm_reco_master_ready(dlm
)) {
2280 mlog(0, "%s: got reco EX lock, but %u will "
2281 "do the recovery\n", dlm
->name
,
2282 dlm
->reco
.new_master
);
2287 /* see if recovery was already finished elsewhere */
2288 spin_lock(&dlm
->spinlock
);
2289 if (dlm
->reco
.dead_node
== O2NM_INVALID_NODE_NUM
) {
2291 mlog(0, "%s: got reco EX lock, but "
2292 "node got recovered already\n", dlm
->name
);
2293 if (dlm
->reco
.new_master
!= O2NM_INVALID_NODE_NUM
) {
2294 mlog(ML_ERROR
, "%s: new master is %u "
2295 "but no dead node!\n",
2296 dlm
->name
, dlm
->reco
.new_master
);
2300 spin_unlock(&dlm
->spinlock
);
2303 /* if this node has actually become the recovery master,
2304 * set the master and send the messages to begin recovery */
2306 mlog(0, "%s: dead=%u, this=%u, sending "
2307 "begin_reco now\n", dlm
->name
,
2308 dlm
->reco
.dead_node
, dlm
->node_num
);
2309 status
= dlm_send_begin_reco_message(dlm
,
2310 dlm
->reco
.dead_node
);
2311 /* this always succeeds */
2314 /* set the new_master to this node */
2315 spin_lock(&dlm
->spinlock
);
2316 dlm_set_reco_master(dlm
, dlm
->node_num
);
2317 spin_unlock(&dlm
->spinlock
);
2320 /* recovery lock is a special case. ast will not get fired,
2321 * so just go ahead and unlock it. */
2322 ret
= dlmunlock(dlm
, &lksb
, 0, dlm_reco_unlock_ast
, dlm
);
2323 if (ret
== DLM_DENIED
) {
2324 mlog(0, "got DLM_DENIED, trying LKM_CANCEL\n");
2325 ret
= dlmunlock(dlm
, &lksb
, LKM_CANCEL
, dlm_reco_unlock_ast
, dlm
);
2327 if (ret
!= DLM_NORMAL
) {
2328 /* this would really suck. this could only happen
2329 * if there was a network error during the unlock
2330 * because of node death. this means the unlock
2331 * is actually "done" and the lock structure is
2332 * even freed. we can continue, but only
2333 * because this specific lock name is special. */
2334 mlog(ML_ERROR
, "dlmunlock returned %d\n", ret
);
2336 } else if (ret
== DLM_NOTQUEUED
) {
2337 mlog(0, "dlm=%s dlmlock says another node got it (this=%u)\n",
2338 dlm
->name
, dlm
->node_num
);
2339 /* another node is master. wait on
2340 * reco.new_master != O2NM_INVALID_NODE_NUM
2341 * for at most one second */
2342 wait_event_timeout(dlm
->dlm_reco_thread_wq
,
2343 dlm_reco_master_ready(dlm
),
2344 msecs_to_jiffies(1000));
2345 if (!dlm_reco_master_ready(dlm
)) {
2346 mlog(0, "%s: reco master taking awhile\n",
2350 /* another node has informed this one that it is reco master */
2351 mlog(0, "%s: reco master %u is ready to recover %u\n",
2352 dlm
->name
, dlm
->reco
.new_master
, dlm
->reco
.dead_node
);
2354 } else if (ret
== DLM_RECOVERING
) {
2355 mlog(0, "dlm=%s dlmlock says master node died (this=%u)\n",
2356 dlm
->name
, dlm
->node_num
);
2359 struct dlm_lock_resource
*res
;
2361 /* dlmlock returned something other than NOTQUEUED or NORMAL */
2362 mlog(ML_ERROR
, "%s: got %s from dlmlock($RECOVERY), "
2363 "lksb.status=%s\n", dlm
->name
, dlm_errname(ret
),
2364 dlm_errname(lksb
.status
));
2365 res
= dlm_lookup_lockres(dlm
, DLM_RECOVERY_LOCK_NAME
,
2366 DLM_RECOVERY_LOCK_NAME_LEN
);
2368 dlm_print_one_lock_resource(res
);
2369 dlm_lockres_put(res
);
2371 mlog(ML_ERROR
, "recovery lock not found\n");
2379 static int dlm_send_begin_reco_message(struct dlm_ctxt
*dlm
, u8 dead_node
)
2381 struct dlm_begin_reco br
;
2383 struct dlm_node_iter iter
;
2387 mlog_entry("%u\n", dead_node
);
2389 mlog(0, "%s: dead node is %u\n", dlm
->name
, dead_node
);
2391 spin_lock(&dlm
->spinlock
);
2392 dlm_node_iter_init(dlm
->domain_map
, &iter
);
2393 spin_unlock(&dlm
->spinlock
);
2395 clear_bit(dead_node
, iter
.node_map
);
2397 memset(&br
, 0, sizeof(br
));
2398 br
.node_idx
= dlm
->node_num
;
2399 br
.dead_node
= dead_node
;
2401 while ((nodenum
= dlm_node_iter_next(&iter
)) >= 0) {
2403 if (nodenum
== dead_node
) {
2404 mlog(0, "not sending begin reco to dead node "
2408 if (nodenum
== dlm
->node_num
) {
2409 mlog(0, "not sending begin reco to self\n");
2414 mlog(0, "attempting to send begin reco msg to %d\n",
2416 ret
= o2net_send_message(DLM_BEGIN_RECO_MSG
, dlm
->key
,
2417 &br
, sizeof(br
), nodenum
, &status
);
2418 /* negative status is handled ok by caller here */
2421 if (dlm_is_host_down(ret
)) {
2422 /* node is down. not involved in recovery
2423 * so just keep going */
2424 mlog(0, "%s: node %u was down when sending "
2425 "begin reco msg (%d)\n", dlm
->name
, nodenum
, ret
);
2429 struct dlm_lock_resource
*res
;
2430 /* this is now a serious problem, possibly ENOMEM
2431 * in the network stack. must retry */
2433 mlog(ML_ERROR
, "begin reco of dlm %s to node %u "
2434 " returned %d\n", dlm
->name
, nodenum
, ret
);
2435 res
= dlm_lookup_lockres(dlm
, DLM_RECOVERY_LOCK_NAME
,
2436 DLM_RECOVERY_LOCK_NAME_LEN
);
2438 dlm_print_one_lock_resource(res
);
2439 dlm_lockres_put(res
);
2441 mlog(ML_ERROR
, "recovery lock not found\n");
2443 /* sleep for a bit in hopes that we can avoid
2447 } else if (ret
== EAGAIN
) {
2448 mlog(0, "%s: trying to start recovery of node "
2449 "%u, but node %u is waiting for last recovery "
2450 "to complete, backoff for a bit\n", dlm
->name
,
2451 dead_node
, nodenum
);
2452 /* TODO Look into replacing msleep with cond_resched() */
2461 int dlm_begin_reco_handler(struct o2net_msg
*msg
, u32 len
, void *data
)
2463 struct dlm_ctxt
*dlm
= data
;
2464 struct dlm_begin_reco
*br
= (struct dlm_begin_reco
*)msg
->buf
;
2466 /* ok to return 0, domain has gone away */
2470 spin_lock(&dlm
->spinlock
);
2471 if (dlm
->reco
.state
& DLM_RECO_STATE_FINALIZE
) {
2472 mlog(0, "%s: node %u wants to recover node %u (%u:%u) "
2473 "but this node is in finalize state, waiting on finalize2\n",
2474 dlm
->name
, br
->node_idx
, br
->dead_node
,
2475 dlm
->reco
.dead_node
, dlm
->reco
.new_master
);
2476 spin_unlock(&dlm
->spinlock
);
2479 spin_unlock(&dlm
->spinlock
);
2481 mlog(0, "%s: node %u wants to recover node %u (%u:%u)\n",
2482 dlm
->name
, br
->node_idx
, br
->dead_node
,
2483 dlm
->reco
.dead_node
, dlm
->reco
.new_master
);
2485 dlm_fire_domain_eviction_callbacks(dlm
, br
->dead_node
);
2487 spin_lock(&dlm
->spinlock
);
2488 if (dlm
->reco
.new_master
!= O2NM_INVALID_NODE_NUM
) {
2489 if (test_bit(dlm
->reco
.new_master
, dlm
->recovery_map
)) {
2490 mlog(0, "%s: new_master %u died, changing "
2491 "to %u\n", dlm
->name
, dlm
->reco
.new_master
,
2494 mlog(0, "%s: new_master %u NOT DEAD, changing "
2495 "to %u\n", dlm
->name
, dlm
->reco
.new_master
,
2497 /* may not have seen the new master as dead yet */
2500 if (dlm
->reco
.dead_node
!= O2NM_INVALID_NODE_NUM
) {
2501 mlog(ML_NOTICE
, "%s: dead_node previously set to %u, "
2502 "node %u changing it to %u\n", dlm
->name
,
2503 dlm
->reco
.dead_node
, br
->node_idx
, br
->dead_node
);
2505 dlm_set_reco_master(dlm
, br
->node_idx
);
2506 dlm_set_reco_dead_node(dlm
, br
->dead_node
);
2507 if (!test_bit(br
->dead_node
, dlm
->recovery_map
)) {
2508 mlog(0, "recovery master %u sees %u as dead, but this "
2509 "node has not yet. marking %u as dead\n",
2510 br
->node_idx
, br
->dead_node
, br
->dead_node
);
2511 if (!test_bit(br
->dead_node
, dlm
->domain_map
) ||
2512 !test_bit(br
->dead_node
, dlm
->live_nodes_map
))
2513 mlog(0, "%u not in domain/live_nodes map "
2514 "so setting it in reco map manually\n",
2516 /* force the recovery cleanup in __dlm_hb_node_down
2517 * both of these will be cleared in a moment */
2518 set_bit(br
->dead_node
, dlm
->domain_map
);
2519 set_bit(br
->dead_node
, dlm
->live_nodes_map
);
2520 __dlm_hb_node_down(dlm
, br
->dead_node
);
2522 spin_unlock(&dlm
->spinlock
);
2524 dlm_kick_recovery_thread(dlm
);
2526 mlog(0, "%s: recovery started by node %u, for %u (%u:%u)\n",
2527 dlm
->name
, br
->node_idx
, br
->dead_node
,
2528 dlm
->reco
.dead_node
, dlm
->reco
.new_master
);
2534 #define DLM_FINALIZE_STAGE2 0x01
2535 static int dlm_send_finalize_reco_message(struct dlm_ctxt
*dlm
)
2538 struct dlm_finalize_reco fr
;
2539 struct dlm_node_iter iter
;
2544 mlog(0, "finishing recovery for node %s:%u, "
2545 "stage %d\n", dlm
->name
, dlm
->reco
.dead_node
, stage
);
2547 spin_lock(&dlm
->spinlock
);
2548 dlm_node_iter_init(dlm
->domain_map
, &iter
);
2549 spin_unlock(&dlm
->spinlock
);
2552 memset(&fr
, 0, sizeof(fr
));
2553 fr
.node_idx
= dlm
->node_num
;
2554 fr
.dead_node
= dlm
->reco
.dead_node
;
2556 fr
.flags
|= DLM_FINALIZE_STAGE2
;
2558 while ((nodenum
= dlm_node_iter_next(&iter
)) >= 0) {
2559 if (nodenum
== dlm
->node_num
)
2561 ret
= o2net_send_message(DLM_FINALIZE_RECO_MSG
, dlm
->key
,
2562 &fr
, sizeof(fr
), nodenum
, &status
);
2567 if (dlm_is_host_down(ret
)) {
2568 /* this has no effect on this recovery
2569 * session, so set the status to zero to
2570 * finish out the last recovery */
2571 mlog(ML_ERROR
, "node %u went down after this "
2572 "node finished recovery.\n", nodenum
);
2580 /* reset the node_iter back to the top and send finalize2 */
2589 int dlm_finalize_reco_handler(struct o2net_msg
*msg
, u32 len
, void *data
)
2591 struct dlm_ctxt
*dlm
= data
;
2592 struct dlm_finalize_reco
*fr
= (struct dlm_finalize_reco
*)msg
->buf
;
2595 /* ok to return 0, domain has gone away */
2599 if (fr
->flags
& DLM_FINALIZE_STAGE2
)
2602 mlog(0, "%s: node %u finalizing recovery stage%d of "
2603 "node %u (%u:%u)\n", dlm
->name
, fr
->node_idx
, stage
,
2604 fr
->dead_node
, dlm
->reco
.dead_node
, dlm
->reco
.new_master
);
2606 spin_lock(&dlm
->spinlock
);
2608 if (dlm
->reco
.new_master
!= fr
->node_idx
) {
2609 mlog(ML_ERROR
, "node %u sent recovery finalize msg, but node "
2610 "%u is supposed to be the new master, dead=%u\n",
2611 fr
->node_idx
, dlm
->reco
.new_master
, fr
->dead_node
);
2614 if (dlm
->reco
.dead_node
!= fr
->dead_node
) {
2615 mlog(ML_ERROR
, "node %u sent recovery finalize msg for dead "
2616 "node %u, but node %u is supposed to be dead\n",
2617 fr
->node_idx
, fr
->dead_node
, dlm
->reco
.dead_node
);
2623 dlm_finish_local_lockres_recovery(dlm
, fr
->dead_node
, fr
->node_idx
);
2624 if (dlm
->reco
.state
& DLM_RECO_STATE_FINALIZE
) {
2625 mlog(ML_ERROR
, "%s: received finalize1 from "
2626 "new master %u for dead node %u, but "
2627 "this node has already received it!\n",
2628 dlm
->name
, fr
->node_idx
, fr
->dead_node
);
2629 dlm_print_reco_node_status(dlm
);
2632 dlm
->reco
.state
|= DLM_RECO_STATE_FINALIZE
;
2633 spin_unlock(&dlm
->spinlock
);
2636 if (!(dlm
->reco
.state
& DLM_RECO_STATE_FINALIZE
)) {
2637 mlog(ML_ERROR
, "%s: received finalize2 from "
2638 "new master %u for dead node %u, but "
2639 "this node did not have finalize1!\n",
2640 dlm
->name
, fr
->node_idx
, fr
->dead_node
);
2641 dlm_print_reco_node_status(dlm
);
2644 dlm
->reco
.state
&= ~DLM_RECO_STATE_FINALIZE
;
2645 spin_unlock(&dlm
->spinlock
);
2646 dlm_reset_recovery(dlm
);
2647 dlm_kick_recovery_thread(dlm
);
2653 mlog(0, "%s: recovery done, reco master was %u, dead now %u, master now %u\n",
2654 dlm
->name
, fr
->node_idx
, dlm
->reco
.dead_node
, dlm
->reco
.new_master
);