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 int dlm_wait_for_node_death(struct dlm_ctxt
*dlm
, u8 node
, int timeout
)
349 mlog(ML_NOTICE
, "%s: waiting %dms for notification of "
350 "death of node %u\n", dlm
->name
, timeout
, node
);
351 wait_event_timeout(dlm
->dlm_reco_thread_wq
,
352 dlm_is_node_dead(dlm
, node
),
353 msecs_to_jiffies(timeout
));
355 mlog(ML_NOTICE
, "%s: waiting indefinitely for notification "
356 "of death of node %u\n", dlm
->name
, node
);
357 wait_event(dlm
->dlm_reco_thread_wq
,
358 dlm_is_node_dead(dlm
, node
));
360 /* for now, return 0 */
364 /* callers of the top-level api calls (dlmlock/dlmunlock) should
365 * block on the dlm->reco.event when recovery is in progress.
366 * the dlm recovery thread will set this state when it begins
367 * recovering a dead node (as the new master or not) and clear
368 * the state and wake as soon as all affected lock resources have
369 * been marked with the RECOVERY flag */
370 static int dlm_in_recovery(struct dlm_ctxt
*dlm
)
373 spin_lock(&dlm
->spinlock
);
374 in_recovery
= !!(dlm
->reco
.state
& DLM_RECO_STATE_ACTIVE
);
375 spin_unlock(&dlm
->spinlock
);
380 void dlm_wait_for_recovery(struct dlm_ctxt
*dlm
)
382 wait_event(dlm
->reco
.event
, !dlm_in_recovery(dlm
));
385 static void dlm_begin_recovery(struct dlm_ctxt
*dlm
)
387 spin_lock(&dlm
->spinlock
);
388 BUG_ON(dlm
->reco
.state
& DLM_RECO_STATE_ACTIVE
);
389 dlm
->reco
.state
|= DLM_RECO_STATE_ACTIVE
;
390 spin_unlock(&dlm
->spinlock
);
393 static void dlm_end_recovery(struct dlm_ctxt
*dlm
)
395 spin_lock(&dlm
->spinlock
);
396 BUG_ON(!(dlm
->reco
.state
& DLM_RECO_STATE_ACTIVE
));
397 dlm
->reco
.state
&= ~DLM_RECO_STATE_ACTIVE
;
398 spin_unlock(&dlm
->spinlock
);
399 wake_up(&dlm
->reco
.event
);
402 static int dlm_do_recovery(struct dlm_ctxt
*dlm
)
407 spin_lock(&dlm
->spinlock
);
409 /* check to see if the new master has died */
410 if (dlm
->reco
.new_master
!= O2NM_INVALID_NODE_NUM
&&
411 test_bit(dlm
->reco
.new_master
, dlm
->recovery_map
)) {
412 mlog(0, "new master %u died while recovering %u!\n",
413 dlm
->reco
.new_master
, dlm
->reco
.dead_node
);
414 /* unset the new_master, leave dead_node */
415 dlm_set_reco_master(dlm
, O2NM_INVALID_NODE_NUM
);
418 /* select a target to recover */
419 if (dlm
->reco
.dead_node
== O2NM_INVALID_NODE_NUM
) {
422 bit
= find_next_bit (dlm
->recovery_map
, O2NM_MAX_NODES
+1, 0);
423 if (bit
>= O2NM_MAX_NODES
|| bit
< 0)
424 dlm_set_reco_dead_node(dlm
, O2NM_INVALID_NODE_NUM
);
426 dlm_set_reco_dead_node(dlm
, bit
);
427 } else if (!test_bit(dlm
->reco
.dead_node
, dlm
->recovery_map
)) {
429 mlog(ML_ERROR
, "dead_node %u no longer in recovery map!\n",
430 dlm
->reco
.dead_node
);
431 dlm_set_reco_dead_node(dlm
, O2NM_INVALID_NODE_NUM
);
434 if (dlm
->reco
.dead_node
== O2NM_INVALID_NODE_NUM
) {
435 // mlog(0, "nothing to recover! sleeping now!\n");
436 spin_unlock(&dlm
->spinlock
);
437 /* return to main thread loop and sleep. */
440 mlog(0, "%s(%d):recovery thread found node %u in the recovery map!\n",
441 dlm
->name
, dlm
->dlm_reco_thread_task
->pid
,
442 dlm
->reco
.dead_node
);
443 spin_unlock(&dlm
->spinlock
);
445 /* take write barrier */
446 /* (stops the list reshuffling thread, proxy ast handling) */
447 dlm_begin_recovery(dlm
);
449 if (dlm
->reco
.new_master
== dlm
->node_num
)
452 if (dlm
->reco
.new_master
== O2NM_INVALID_NODE_NUM
) {
453 /* choose a new master, returns 0 if this node
454 * is the master, -EEXIST if it's another node.
455 * this does not return until a new master is chosen
456 * or recovery completes entirely. */
457 ret
= dlm_pick_recovery_master(dlm
);
459 /* already notified everyone. go. */
462 mlog(0, "another node will master this recovery session.\n");
464 mlog(0, "dlm=%s (%d), new_master=%u, this node=%u, dead_node=%u\n",
465 dlm
->name
, dlm
->dlm_reco_thread_task
->pid
, dlm
->reco
.new_master
,
466 dlm
->node_num
, dlm
->reco
.dead_node
);
468 /* it is safe to start everything back up here
469 * because all of the dead node's lock resources
470 * have been marked as in-recovery */
471 dlm_end_recovery(dlm
);
473 /* sleep out in main dlm_recovery_thread loop. */
477 mlog(0, "(%d) mastering recovery of %s:%u here(this=%u)!\n",
478 dlm
->dlm_reco_thread_task
->pid
,
479 dlm
->name
, dlm
->reco
.dead_node
, dlm
->node_num
);
481 status
= dlm_remaster_locks(dlm
, dlm
->reco
.dead_node
);
483 /* we should never hit this anymore */
484 mlog(ML_ERROR
, "error %d remastering locks for node %u, "
485 "retrying.\n", status
, dlm
->reco
.dead_node
);
486 /* yield a bit to allow any final network messages
487 * to get handled on remaining nodes */
490 /* success! see if any other nodes need recovery */
491 mlog(0, "DONE mastering recovery of %s:%u here(this=%u)!\n",
492 dlm
->name
, dlm
->reco
.dead_node
, dlm
->node_num
);
493 dlm_reset_recovery(dlm
);
495 dlm_end_recovery(dlm
);
497 /* continue and look for another dead node */
501 static int dlm_remaster_locks(struct dlm_ctxt
*dlm
, u8 dead_node
)
504 struct dlm_reco_node_data
*ndata
;
505 struct list_head
*iter
;
511 /* we have become recovery master. there is no escaping
512 * this, so just keep trying until we get it. */
513 status
= dlm_init_recovery_area(dlm
, dead_node
);
515 mlog(ML_ERROR
, "%s: failed to alloc recovery area, "
516 "retrying\n", dlm
->name
);
519 } while (status
!= 0);
521 /* safe to access the node data list without a lock, since this
522 * process is the only one to change the list */
523 list_for_each(iter
, &dlm
->reco
.node_data
) {
524 ndata
= list_entry (iter
, struct dlm_reco_node_data
, list
);
525 BUG_ON(ndata
->state
!= DLM_RECO_NODE_DATA_INIT
);
526 ndata
->state
= DLM_RECO_NODE_DATA_REQUESTING
;
528 mlog(0, "requesting lock info from node %u\n",
531 if (ndata
->node_num
== dlm
->node_num
) {
532 ndata
->state
= DLM_RECO_NODE_DATA_DONE
;
537 status
= dlm_request_all_locks(dlm
, ndata
->node_num
,
541 if (dlm_is_host_down(status
)) {
542 /* node died, ignore it for recovery */
544 ndata
->state
= DLM_RECO_NODE_DATA_DEAD
;
545 /* wait for the domain map to catch up
546 * with the network state. */
547 wait_event_timeout(dlm
->dlm_reco_thread_wq
,
548 dlm_is_node_dead(dlm
,
550 msecs_to_jiffies(1000));
551 mlog(0, "waited 1 sec for %u, "
552 "dead? %s\n", ndata
->node_num
,
553 dlm_is_node_dead(dlm
, ndata
->node_num
) ?
556 /* -ENOMEM on the other node */
557 mlog(0, "%s: node %u returned "
558 "%d during recovery, retrying "
559 "after a short wait\n",
560 dlm
->name
, ndata
->node_num
,
565 } while (status
!= 0);
567 switch (ndata
->state
) {
568 case DLM_RECO_NODE_DATA_INIT
:
569 case DLM_RECO_NODE_DATA_FINALIZE_SENT
:
570 case DLM_RECO_NODE_DATA_REQUESTED
:
573 case DLM_RECO_NODE_DATA_DEAD
:
574 mlog(0, "node %u died after requesting "
575 "recovery info for node %u\n",
576 ndata
->node_num
, dead_node
);
577 /* fine. don't need this node's info.
578 * continue without it. */
580 case DLM_RECO_NODE_DATA_REQUESTING
:
581 ndata
->state
= DLM_RECO_NODE_DATA_REQUESTED
;
582 mlog(0, "now receiving recovery data from "
583 "node %u for dead node %u\n",
584 ndata
->node_num
, dead_node
);
586 case DLM_RECO_NODE_DATA_RECEIVING
:
587 mlog(0, "already receiving recovery data from "
588 "node %u for dead node %u\n",
589 ndata
->node_num
, dead_node
);
591 case DLM_RECO_NODE_DATA_DONE
:
592 mlog(0, "already DONE receiving recovery data "
593 "from node %u for dead node %u\n",
594 ndata
->node_num
, dead_node
);
599 mlog(0, "done requesting all lock info\n");
601 /* nodes should be sending reco data now
602 * just need to wait */
605 /* check all the nodes now to see if we are
606 * done, or if anyone died */
608 spin_lock(&dlm_reco_state_lock
);
609 list_for_each(iter
, &dlm
->reco
.node_data
) {
610 ndata
= list_entry (iter
, struct dlm_reco_node_data
, list
);
612 mlog(0, "checking recovery state of node %u\n",
614 switch (ndata
->state
) {
615 case DLM_RECO_NODE_DATA_INIT
:
616 case DLM_RECO_NODE_DATA_REQUESTING
:
617 mlog(ML_ERROR
, "bad ndata state for "
618 "node %u: state=%d\n",
619 ndata
->node_num
, ndata
->state
);
622 case DLM_RECO_NODE_DATA_DEAD
:
623 mlog(0, "node %u died after "
624 "requesting recovery info for "
625 "node %u\n", ndata
->node_num
,
628 case DLM_RECO_NODE_DATA_RECEIVING
:
629 case DLM_RECO_NODE_DATA_REQUESTED
:
630 mlog(0, "%s: node %u still in state %s\n",
631 dlm
->name
, ndata
->node_num
,
632 ndata
->state
==DLM_RECO_NODE_DATA_RECEIVING
?
633 "receiving" : "requested");
636 case DLM_RECO_NODE_DATA_DONE
:
637 mlog(0, "%s: node %u state is done\n",
638 dlm
->name
, ndata
->node_num
);
640 case DLM_RECO_NODE_DATA_FINALIZE_SENT
:
641 mlog(0, "%s: node %u state is finalize\n",
642 dlm
->name
, ndata
->node_num
);
646 spin_unlock(&dlm_reco_state_lock
);
648 mlog(0, "pass #%d, all_nodes_done?: %s\n", ++pass
,
649 all_nodes_done
?"yes":"no");
650 if (all_nodes_done
) {
653 /* all nodes are now in DLM_RECO_NODE_DATA_DONE state
654 * just send a finalize message to everyone and
656 mlog(0, "all nodes are done! send finalize\n");
657 ret
= dlm_send_finalize_reco_message(dlm
);
661 spin_lock(&dlm
->spinlock
);
662 dlm_finish_local_lockres_recovery(dlm
, dead_node
,
664 spin_unlock(&dlm
->spinlock
);
665 mlog(0, "should be done with recovery!\n");
667 mlog(0, "finishing recovery of %s at %lu, "
668 "dead=%u, this=%u, new=%u\n", dlm
->name
,
669 jiffies
, dlm
->reco
.dead_node
,
670 dlm
->node_num
, dlm
->reco
.new_master
);
673 /* rescan everything marked dirty along the way */
674 dlm_kick_thread(dlm
, NULL
);
677 /* wait to be signalled, with periodic timeout
678 * to check for node death */
679 wait_event_interruptible_timeout(dlm
->dlm_reco_thread_wq
,
680 kthread_should_stop(),
681 msecs_to_jiffies(DLM_RECO_THREAD_TIMEOUT_MS
));
686 dlm_destroy_recovery_area(dlm
, dead_node
);
692 static int dlm_init_recovery_area(struct dlm_ctxt
*dlm
, u8 dead_node
)
695 struct dlm_reco_node_data
*ndata
;
697 spin_lock(&dlm
->spinlock
);
698 memcpy(dlm
->reco
.node_map
, dlm
->domain_map
, sizeof(dlm
->domain_map
));
699 /* nodes can only be removed (by dying) after dropping
700 * this lock, and death will be trapped later, so this should do */
701 spin_unlock(&dlm
->spinlock
);
704 num
= find_next_bit (dlm
->reco
.node_map
, O2NM_MAX_NODES
, num
);
705 if (num
>= O2NM_MAX_NODES
) {
708 BUG_ON(num
== dead_node
);
710 ndata
= kcalloc(1, sizeof(*ndata
), GFP_KERNEL
);
712 dlm_destroy_recovery_area(dlm
, dead_node
);
715 ndata
->node_num
= num
;
716 ndata
->state
= DLM_RECO_NODE_DATA_INIT
;
717 spin_lock(&dlm_reco_state_lock
);
718 list_add_tail(&ndata
->list
, &dlm
->reco
.node_data
);
719 spin_unlock(&dlm_reco_state_lock
);
726 static void dlm_destroy_recovery_area(struct dlm_ctxt
*dlm
, u8 dead_node
)
728 struct list_head
*iter
, *iter2
;
729 struct dlm_reco_node_data
*ndata
;
732 spin_lock(&dlm_reco_state_lock
);
733 list_splice_init(&dlm
->reco
.node_data
, &tmplist
);
734 spin_unlock(&dlm_reco_state_lock
);
736 list_for_each_safe(iter
, iter2
, &tmplist
) {
737 ndata
= list_entry (iter
, struct dlm_reco_node_data
, list
);
738 list_del_init(&ndata
->list
);
743 static int dlm_request_all_locks(struct dlm_ctxt
*dlm
, u8 request_from
,
746 struct dlm_lock_request lr
;
752 mlog(0, "dlm_request_all_locks: dead node is %u, sending request "
753 "to %u\n", dead_node
, request_from
);
755 memset(&lr
, 0, sizeof(lr
));
756 lr
.node_idx
= dlm
->node_num
;
757 lr
.dead_node
= dead_node
;
761 ret
= o2net_send_message(DLM_LOCK_REQUEST_MSG
, dlm
->key
,
762 &lr
, sizeof(lr
), request_from
, NULL
);
764 /* negative status is handled by caller */
768 // return from here, then
769 // sleep until all received or error
774 int dlm_request_all_locks_handler(struct o2net_msg
*msg
, u32 len
, void *data
)
776 struct dlm_ctxt
*dlm
= data
;
777 struct dlm_lock_request
*lr
= (struct dlm_lock_request
*)msg
->buf
;
779 struct dlm_work_item
*item
= NULL
;
784 if (lr
->dead_node
!= dlm
->reco
.dead_node
) {
785 mlog(ML_ERROR
, "%s: node %u sent dead_node=%u, but local "
786 "dead_node is %u\n", dlm
->name
, lr
->node_idx
,
787 lr
->dead_node
, dlm
->reco
.dead_node
);
788 dlm_print_reco_node_status(dlm
);
793 BUG_ON(lr
->dead_node
!= dlm
->reco
.dead_node
);
795 item
= kcalloc(1, sizeof(*item
), GFP_KERNEL
);
801 /* this will get freed by dlm_request_all_locks_worker */
802 buf
= (char *) __get_free_page(GFP_KERNEL
);
809 /* queue up work for dlm_request_all_locks_worker */
810 dlm_grab(dlm
); /* get an extra ref for the work item */
811 dlm_init_work_item(dlm
, item
, dlm_request_all_locks_worker
, buf
);
812 item
->u
.ral
.reco_master
= lr
->node_idx
;
813 item
->u
.ral
.dead_node
= lr
->dead_node
;
814 spin_lock(&dlm
->work_lock
);
815 list_add_tail(&item
->list
, &dlm
->work_list
);
816 spin_unlock(&dlm
->work_lock
);
817 schedule_work(&dlm
->dispatched_work
);
823 static void dlm_request_all_locks_worker(struct dlm_work_item
*item
, void *data
)
825 struct dlm_migratable_lockres
*mres
;
826 struct dlm_lock_resource
*res
;
827 struct dlm_ctxt
*dlm
;
828 LIST_HEAD(resources
);
829 struct list_head
*iter
;
831 u8 dead_node
, reco_master
;
832 int skip_all_done
= 0;
835 dead_node
= item
->u
.ral
.dead_node
;
836 reco_master
= item
->u
.ral
.reco_master
;
837 mres
= (struct dlm_migratable_lockres
*)data
;
839 mlog(0, "%s: recovery worker started, dead=%u, master=%u\n",
840 dlm
->name
, dead_node
, reco_master
);
842 if (dead_node
!= dlm
->reco
.dead_node
||
843 reco_master
!= dlm
->reco
.new_master
) {
844 /* worker could have been created before the recovery master
845 * died. if so, do not continue, but do not error. */
846 if (dlm
->reco
.new_master
== O2NM_INVALID_NODE_NUM
) {
847 mlog(ML_NOTICE
, "%s: will not send recovery state, "
848 "recovery master %u died, thread=(dead=%u,mas=%u)"
849 " current=(dead=%u,mas=%u)\n", dlm
->name
,
850 reco_master
, dead_node
, reco_master
,
851 dlm
->reco
.dead_node
, dlm
->reco
.new_master
);
853 mlog(ML_NOTICE
, "%s: reco state invalid: reco(dead=%u, "
854 "master=%u), request(dead=%u, master=%u)\n",
855 dlm
->name
, dlm
->reco
.dead_node
,
856 dlm
->reco
.new_master
, dead_node
, reco_master
);
861 /* lock resources should have already been moved to the
862 * dlm->reco.resources list. now move items from that list
863 * to a temp list if the dead owner matches. note that the
864 * whole cluster recovers only one node at a time, so we
865 * can safely move UNKNOWN lock resources for each recovery
867 dlm_move_reco_locks_to_list(dlm
, &resources
, dead_node
);
869 /* now we can begin blasting lockreses without the dlm lock */
871 /* any errors returned will be due to the new_master dying,
872 * the dlm_reco_thread should detect this */
873 list_for_each(iter
, &resources
) {
874 res
= list_entry (iter
, struct dlm_lock_resource
, recovering
);
875 ret
= dlm_send_one_lockres(dlm
, res
, mres
, reco_master
,
878 mlog(ML_ERROR
, "%s: node %u went down while sending "
879 "recovery state for dead node %u, ret=%d\n", dlm
->name
,
880 reco_master
, dead_node
, ret
);
886 /* move the resources back to the list */
887 spin_lock(&dlm
->spinlock
);
888 list_splice_init(&resources
, &dlm
->reco
.resources
);
889 spin_unlock(&dlm
->spinlock
);
891 if (!skip_all_done
) {
892 ret
= dlm_send_all_done_msg(dlm
, dead_node
, reco_master
);
894 mlog(ML_ERROR
, "%s: node %u went down while sending "
895 "recovery all-done for dead node %u, ret=%d\n",
896 dlm
->name
, reco_master
, dead_node
, ret
);
900 free_page((unsigned long)data
);
904 static int dlm_send_all_done_msg(struct dlm_ctxt
*dlm
, u8 dead_node
, u8 send_to
)
907 struct dlm_reco_data_done done_msg
;
909 memset(&done_msg
, 0, sizeof(done_msg
));
910 done_msg
.node_idx
= dlm
->node_num
;
911 done_msg
.dead_node
= dead_node
;
912 mlog(0, "sending DATA DONE message to %u, "
913 "my node=%u, dead node=%u\n", send_to
, done_msg
.node_idx
,
916 ret
= o2net_send_message(DLM_RECO_DATA_DONE_MSG
, dlm
->key
, &done_msg
,
917 sizeof(done_msg
), send_to
, &tmpret
);
919 if (!dlm_is_host_down(ret
)) {
921 mlog(ML_ERROR
, "%s: unknown error sending data-done "
922 "to %u\n", dlm
->name
, send_to
);
931 int dlm_reco_data_done_handler(struct o2net_msg
*msg
, u32 len
, void *data
)
933 struct dlm_ctxt
*dlm
= data
;
934 struct dlm_reco_data_done
*done
= (struct dlm_reco_data_done
*)msg
->buf
;
935 struct list_head
*iter
;
936 struct dlm_reco_node_data
*ndata
= NULL
;
942 mlog(0, "got DATA DONE: dead_node=%u, reco.dead_node=%u, "
943 "node_idx=%u, this node=%u\n", done
->dead_node
,
944 dlm
->reco
.dead_node
, done
->node_idx
, dlm
->node_num
);
946 mlog_bug_on_msg((done
->dead_node
!= dlm
->reco
.dead_node
),
947 "Got DATA DONE: dead_node=%u, reco.dead_node=%u, "
948 "node_idx=%u, this node=%u\n", done
->dead_node
,
949 dlm
->reco
.dead_node
, done
->node_idx
, dlm
->node_num
);
951 spin_lock(&dlm_reco_state_lock
);
952 list_for_each(iter
, &dlm
->reco
.node_data
) {
953 ndata
= list_entry (iter
, struct dlm_reco_node_data
, list
);
954 if (ndata
->node_num
!= done
->node_idx
)
957 switch (ndata
->state
) {
958 /* should have moved beyond INIT but not to FINALIZE yet */
959 case DLM_RECO_NODE_DATA_INIT
:
960 case DLM_RECO_NODE_DATA_DEAD
:
961 case DLM_RECO_NODE_DATA_FINALIZE_SENT
:
962 mlog(ML_ERROR
, "bad ndata state for node %u:"
963 " state=%d\n", ndata
->node_num
,
967 /* these states are possible at this point, anywhere along
968 * the line of recovery */
969 case DLM_RECO_NODE_DATA_DONE
:
970 case DLM_RECO_NODE_DATA_RECEIVING
:
971 case DLM_RECO_NODE_DATA_REQUESTED
:
972 case DLM_RECO_NODE_DATA_REQUESTING
:
973 mlog(0, "node %u is DONE sending "
977 ndata
->state
= DLM_RECO_NODE_DATA_DONE
;
982 spin_unlock(&dlm_reco_state_lock
);
984 /* wake the recovery thread, some node is done */
986 dlm_kick_recovery_thread(dlm
);
989 mlog(ML_ERROR
, "failed to find recovery node data for node "
990 "%u\n", done
->node_idx
);
993 mlog(0, "leaving reco data done handler, ret=%d\n", ret
);
997 static void dlm_move_reco_locks_to_list(struct dlm_ctxt
*dlm
,
998 struct list_head
*list
,
1001 struct dlm_lock_resource
*res
;
1002 struct list_head
*iter
, *iter2
;
1003 struct dlm_lock
*lock
;
1005 spin_lock(&dlm
->spinlock
);
1006 list_for_each_safe(iter
, iter2
, &dlm
->reco
.resources
) {
1007 res
= list_entry (iter
, struct dlm_lock_resource
, recovering
);
1008 /* always prune any $RECOVERY entries for dead nodes,
1009 * otherwise hangs can occur during later recovery */
1010 if (dlm_is_recovery_lock(res
->lockname
.name
,
1011 res
->lockname
.len
)) {
1012 spin_lock(&res
->spinlock
);
1013 list_for_each_entry(lock
, &res
->granted
, list
) {
1014 if (lock
->ml
.node
== dead_node
) {
1015 mlog(0, "AHA! there was "
1016 "a $RECOVERY lock for dead "
1018 dead_node
, dlm
->name
);
1019 list_del_init(&lock
->list
);
1024 spin_unlock(&res
->spinlock
);
1028 if (res
->owner
== dead_node
) {
1029 mlog(0, "found lockres owned by dead node while "
1030 "doing recovery for node %u. sending it.\n",
1032 list_move_tail(&res
->recovering
, list
);
1033 } else if (res
->owner
== DLM_LOCK_RES_OWNER_UNKNOWN
) {
1034 mlog(0, "found UNKNOWN owner while doing recovery "
1035 "for node %u. sending it.\n", dead_node
);
1036 list_move_tail(&res
->recovering
, list
);
1039 spin_unlock(&dlm
->spinlock
);
1042 static inline int dlm_num_locks_in_lockres(struct dlm_lock_resource
*res
)
1044 int total_locks
= 0;
1045 struct list_head
*iter
, *queue
= &res
->granted
;
1048 for (i
=0; i
<3; i
++) {
1049 list_for_each(iter
, queue
)
1057 static int dlm_send_mig_lockres_msg(struct dlm_ctxt
*dlm
,
1058 struct dlm_migratable_lockres
*mres
,
1060 struct dlm_lock_resource
*res
,
1063 u64 mig_cookie
= be64_to_cpu(mres
->mig_cookie
);
1064 int mres_total_locks
= be32_to_cpu(mres
->total_locks
);
1065 int sz
, ret
= 0, status
= 0;
1066 u8 orig_flags
= mres
->flags
,
1067 orig_master
= mres
->master
;
1069 BUG_ON(mres
->num_locks
> DLM_MAX_MIGRATABLE_LOCKS
);
1070 if (!mres
->num_locks
)
1073 sz
= sizeof(struct dlm_migratable_lockres
) +
1074 (mres
->num_locks
* sizeof(struct dlm_migratable_lock
));
1076 /* add an all-done flag if we reached the last lock */
1077 orig_flags
= mres
->flags
;
1078 BUG_ON(total_locks
> mres_total_locks
);
1079 if (total_locks
== mres_total_locks
)
1080 mres
->flags
|= DLM_MRES_ALL_DONE
;
1083 ret
= o2net_send_message(DLM_MIG_LOCKRES_MSG
, dlm
->key
, mres
,
1084 sz
, send_to
, &status
);
1086 /* XXX: negative status is not handled.
1087 * this will end up killing this node. */
1090 /* might get an -ENOMEM back here */
1095 if (ret
== -EFAULT
) {
1096 mlog(ML_ERROR
, "node %u told me to kill "
1097 "myself!\n", send_to
);
1103 /* zero and reinit the message buffer */
1104 dlm_init_migratable_lockres(mres
, res
->lockname
.name
,
1105 res
->lockname
.len
, mres_total_locks
,
1106 mig_cookie
, orig_flags
, orig_master
);
1110 static void dlm_init_migratable_lockres(struct dlm_migratable_lockres
*mres
,
1111 const char *lockname
, int namelen
,
1112 int total_locks
, u64 cookie
,
1113 u8 flags
, u8 master
)
1115 /* mres here is one full page */
1116 memset(mres
, 0, PAGE_SIZE
);
1117 mres
->lockname_len
= namelen
;
1118 memcpy(mres
->lockname
, lockname
, namelen
);
1119 mres
->num_locks
= 0;
1120 mres
->total_locks
= cpu_to_be32(total_locks
);
1121 mres
->mig_cookie
= cpu_to_be64(cookie
);
1122 mres
->flags
= flags
;
1123 mres
->master
= master
;
1127 /* returns 1 if this lock fills the network structure,
1129 static int dlm_add_lock_to_array(struct dlm_lock
*lock
,
1130 struct dlm_migratable_lockres
*mres
, int queue
)
1132 struct dlm_migratable_lock
*ml
;
1133 int lock_num
= mres
->num_locks
;
1135 ml
= &(mres
->ml
[lock_num
]);
1136 ml
->cookie
= lock
->ml
.cookie
;
1137 ml
->type
= lock
->ml
.type
;
1138 ml
->convert_type
= lock
->ml
.convert_type
;
1139 ml
->highest_blocked
= lock
->ml
.highest_blocked
;
1142 ml
->flags
= lock
->lksb
->flags
;
1143 /* send our current lvb */
1144 if (ml
->type
== LKM_EXMODE
||
1145 ml
->type
== LKM_PRMODE
) {
1146 /* if it is already set, this had better be a PR
1147 * and it has to match */
1148 if (!dlm_lvb_is_empty(mres
->lvb
) &&
1149 (ml
->type
== LKM_EXMODE
||
1150 memcmp(mres
->lvb
, lock
->lksb
->lvb
, DLM_LVB_LEN
))) {
1151 mlog(ML_ERROR
, "mismatched lvbs!\n");
1152 __dlm_print_one_lock_resource(lock
->lockres
);
1155 memcpy(mres
->lvb
, lock
->lksb
->lvb
, DLM_LVB_LEN
);
1158 ml
->node
= lock
->ml
.node
;
1160 /* we reached the max, send this network message */
1161 if (mres
->num_locks
== DLM_MAX_MIGRATABLE_LOCKS
)
1167 int dlm_send_one_lockres(struct dlm_ctxt
*dlm
, struct dlm_lock_resource
*res
,
1168 struct dlm_migratable_lockres
*mres
,
1169 u8 send_to
, u8 flags
)
1171 struct list_head
*queue
, *iter
;
1174 struct dlm_lock
*lock
;
1177 BUG_ON(!(flags
& (DLM_MRES_RECOVERY
|DLM_MRES_MIGRATION
)));
1179 mlog(0, "sending to %u\n", send_to
);
1181 total_locks
= dlm_num_locks_in_lockres(res
);
1182 if (total_locks
> DLM_MAX_MIGRATABLE_LOCKS
) {
1183 /* rare, but possible */
1184 mlog(0, "argh. lockres has %d locks. this will "
1185 "require more than one network packet to "
1186 "migrate\n", total_locks
);
1187 mig_cookie
= dlm_get_next_mig_cookie();
1190 dlm_init_migratable_lockres(mres
, res
->lockname
.name
,
1191 res
->lockname
.len
, total_locks
,
1192 mig_cookie
, flags
, res
->owner
);
1195 for (i
=DLM_GRANTED_LIST
; i
<=DLM_BLOCKED_LIST
; i
++) {
1196 queue
= dlm_list_idx_to_ptr(res
, i
);
1197 list_for_each(iter
, queue
) {
1198 lock
= list_entry (iter
, struct dlm_lock
, list
);
1200 /* add another lock. */
1202 if (!dlm_add_lock_to_array(lock
, mres
, i
))
1205 /* this filled the lock message,
1206 * we must send it immediately. */
1207 ret
= dlm_send_mig_lockres_msg(dlm
, mres
, send_to
,
1213 /* flush any remaining locks */
1214 ret
= dlm_send_mig_lockres_msg(dlm
, mres
, send_to
, res
, total_locks
);
1220 mlog(ML_ERROR
, "%s: dlm_send_mig_lockres_msg returned %d\n",
1222 if (!dlm_is_host_down(ret
))
1224 mlog(0, "%s: node %u went down while sending %s "
1225 "lockres %.*s\n", dlm
->name
, send_to
,
1226 flags
& DLM_MRES_RECOVERY
? "recovery" : "migration",
1227 res
->lockname
.len
, res
->lockname
.name
);
1234 * this message will contain no more than one page worth of
1235 * recovery data, and it will work on only one lockres.
1236 * there may be many locks in this page, and we may need to wait
1237 * for additional packets to complete all the locks (rare, but
1241 * NOTE: the allocation error cases here are scary
1242 * we really cannot afford to fail an alloc in recovery
1243 * do we spin? returning an error only delays the problem really
1246 int dlm_mig_lockres_handler(struct o2net_msg
*msg
, u32 len
, void *data
)
1248 struct dlm_ctxt
*dlm
= data
;
1249 struct dlm_migratable_lockres
*mres
=
1250 (struct dlm_migratable_lockres
*)msg
->buf
;
1254 struct dlm_work_item
*item
= NULL
;
1255 struct dlm_lock_resource
*res
= NULL
;
1260 BUG_ON(!(mres
->flags
& (DLM_MRES_RECOVERY
|DLM_MRES_MIGRATION
)));
1262 real_master
= mres
->master
;
1263 if (real_master
== DLM_LOCK_RES_OWNER_UNKNOWN
) {
1264 /* cannot migrate a lockres with no master */
1265 BUG_ON(!(mres
->flags
& DLM_MRES_RECOVERY
));
1268 mlog(0, "%s message received from node %u\n",
1269 (mres
->flags
& DLM_MRES_RECOVERY
) ?
1270 "recovery" : "migration", mres
->master
);
1271 if (mres
->flags
& DLM_MRES_ALL_DONE
)
1272 mlog(0, "all done flag. all lockres data received!\n");
1275 buf
= kmalloc(be16_to_cpu(msg
->data_len
), GFP_KERNEL
);
1276 item
= kcalloc(1, sizeof(*item
), GFP_KERNEL
);
1280 /* lookup the lock to see if we have a secondary queue for this
1281 * already... just add the locks in and this will have its owner
1282 * and RECOVERY flag changed when it completes. */
1283 res
= dlm_lookup_lockres(dlm
, mres
->lockname
, mres
->lockname_len
);
1285 /* this will get a ref on res */
1286 /* mark it as recovering/migrating and hash it */
1287 spin_lock(&res
->spinlock
);
1288 if (mres
->flags
& DLM_MRES_RECOVERY
) {
1289 res
->state
|= DLM_LOCK_RES_RECOVERING
;
1291 if (res
->state
& DLM_LOCK_RES_MIGRATING
) {
1292 /* this is at least the second
1293 * lockres message */
1294 mlog(0, "lock %.*s is already migrating\n",
1297 } else if (res
->state
& DLM_LOCK_RES_RECOVERING
) {
1298 /* caller should BUG */
1299 mlog(ML_ERROR
, "node is attempting to migrate "
1300 "lock %.*s, but marked as recovering!\n",
1301 mres
->lockname_len
, mres
->lockname
);
1303 spin_unlock(&res
->spinlock
);
1306 res
->state
|= DLM_LOCK_RES_MIGRATING
;
1308 spin_unlock(&res
->spinlock
);
1310 /* need to allocate, just like if it was
1311 * mastered here normally */
1312 res
= dlm_new_lockres(dlm
, mres
->lockname
, mres
->lockname_len
);
1316 /* to match the ref that we would have gotten if
1317 * dlm_lookup_lockres had succeeded */
1318 dlm_lockres_get(res
);
1320 /* mark it as recovering/migrating and hash it */
1321 if (mres
->flags
& DLM_MRES_RECOVERY
)
1322 res
->state
|= DLM_LOCK_RES_RECOVERING
;
1324 res
->state
|= DLM_LOCK_RES_MIGRATING
;
1326 spin_lock(&dlm
->spinlock
);
1327 __dlm_insert_lockres(dlm
, res
);
1328 spin_unlock(&dlm
->spinlock
);
1330 /* now that the new lockres is inserted,
1331 * make it usable by other processes */
1332 spin_lock(&res
->spinlock
);
1333 res
->state
&= ~DLM_LOCK_RES_IN_PROGRESS
;
1334 spin_unlock(&res
->spinlock
);
1336 /* add an extra ref for just-allocated lockres
1337 * otherwise the lockres will be purged immediately */
1338 dlm_lockres_get(res
);
1342 /* at this point we have allocated everything we need,
1343 * and we have a hashed lockres with an extra ref and
1344 * the proper res->state flags. */
1346 if (mres
->master
== DLM_LOCK_RES_OWNER_UNKNOWN
) {
1347 /* migration cannot have an unknown master */
1348 BUG_ON(!(mres
->flags
& DLM_MRES_RECOVERY
));
1349 mlog(0, "recovery has passed me a lockres with an "
1350 "unknown owner.. will need to requery: "
1351 "%.*s\n", mres
->lockname_len
, mres
->lockname
);
1353 spin_lock(&res
->spinlock
);
1354 dlm_change_lockres_owner(dlm
, res
, dlm
->node_num
);
1355 spin_unlock(&res
->spinlock
);
1358 /* queue up work for dlm_mig_lockres_worker */
1359 dlm_grab(dlm
); /* get an extra ref for the work item */
1360 memcpy(buf
, msg
->buf
, be16_to_cpu(msg
->data_len
)); /* copy the whole message */
1361 dlm_init_work_item(dlm
, item
, dlm_mig_lockres_worker
, buf
);
1362 item
->u
.ml
.lockres
= res
; /* already have a ref */
1363 item
->u
.ml
.real_master
= real_master
;
1364 spin_lock(&dlm
->work_lock
);
1365 list_add_tail(&item
->list
, &dlm
->work_list
);
1366 spin_unlock(&dlm
->work_lock
);
1367 schedule_work(&dlm
->dispatched_work
);
1383 static void dlm_mig_lockres_worker(struct dlm_work_item
*item
, void *data
)
1385 struct dlm_ctxt
*dlm
= data
;
1386 struct dlm_migratable_lockres
*mres
;
1388 struct dlm_lock_resource
*res
;
1392 mres
= (struct dlm_migratable_lockres
*)data
;
1394 res
= item
->u
.ml
.lockres
;
1395 real_master
= item
->u
.ml
.real_master
;
1397 if (real_master
== DLM_LOCK_RES_OWNER_UNKNOWN
) {
1398 /* this case is super-rare. only occurs if
1399 * node death happens during migration. */
1401 ret
= dlm_lockres_master_requery(dlm
, res
, &real_master
);
1403 mlog(0, "dlm_lockres_master_requery ret=%d\n",
1407 if (real_master
== DLM_LOCK_RES_OWNER_UNKNOWN
) {
1408 mlog(0, "lockres %.*s not claimed. "
1409 "this node will take it.\n",
1410 res
->lockname
.len
, res
->lockname
.name
);
1412 mlog(0, "master needs to respond to sender "
1413 "that node %u still owns %.*s\n",
1414 real_master
, res
->lockname
.len
,
1415 res
->lockname
.name
);
1416 /* cannot touch this lockres */
1421 ret
= dlm_process_recovery_data(dlm
, res
, mres
);
1423 mlog(0, "dlm_process_recovery_data returned %d\n", ret
);
1425 mlog(0, "dlm_process_recovery_data succeeded\n");
1427 if ((mres
->flags
& (DLM_MRES_MIGRATION
|DLM_MRES_ALL_DONE
)) ==
1428 (DLM_MRES_MIGRATION
|DLM_MRES_ALL_DONE
)) {
1429 ret
= dlm_finish_migration(dlm
, res
, mres
->master
);
1441 int dlm_lockres_master_requery(struct dlm_ctxt
*dlm
,
1442 struct dlm_lock_resource
*res
, u8
*real_master
)
1444 struct dlm_node_iter iter
;
1448 *real_master
= DLM_LOCK_RES_OWNER_UNKNOWN
;
1450 /* we only reach here if one of the two nodes in a
1451 * migration died while the migration was in progress.
1452 * at this point we need to requery the master. we
1453 * know that the new_master got as far as creating
1454 * an mle on at least one node, but we do not know
1455 * if any nodes had actually cleared the mle and set
1456 * the master to the new_master. the old master
1457 * is supposed to set the owner to UNKNOWN in the
1458 * event of a new_master death, so the only possible
1459 * responses that we can get from nodes here are
1460 * that the master is new_master, or that the master
1462 * if all nodes come back with UNKNOWN then we know
1463 * the lock needs remastering here.
1464 * if any node comes back with a valid master, check
1465 * to see if that master is the one that we are
1466 * recovering. if so, then the new_master died and
1467 * we need to remaster this lock. if not, then the
1468 * new_master survived and that node will respond to
1469 * other nodes about the owner.
1470 * if there is an owner, this node needs to dump this
1471 * lockres and alert the sender that this lockres
1473 spin_lock(&dlm
->spinlock
);
1474 dlm_node_iter_init(dlm
->domain_map
, &iter
);
1475 spin_unlock(&dlm
->spinlock
);
1477 while ((nodenum
= dlm_node_iter_next(&iter
)) >= 0) {
1478 /* do not send to self */
1479 if (nodenum
== dlm
->node_num
)
1481 ret
= dlm_do_master_requery(dlm
, res
, nodenum
, real_master
);
1484 if (!dlm_is_host_down(ret
))
1486 /* host is down, so answer for that node would be
1487 * DLM_LOCK_RES_OWNER_UNKNOWN. continue. */
1489 if (*real_master
!= DLM_LOCK_RES_OWNER_UNKNOWN
) {
1490 mlog(0, "lock master is %u\n", *real_master
);
1498 int dlm_do_master_requery(struct dlm_ctxt
*dlm
, struct dlm_lock_resource
*res
,
1499 u8 nodenum
, u8
*real_master
)
1502 struct dlm_master_requery req
;
1503 int status
= DLM_LOCK_RES_OWNER_UNKNOWN
;
1505 memset(&req
, 0, sizeof(req
));
1506 req
.node_idx
= dlm
->node_num
;
1507 req
.namelen
= res
->lockname
.len
;
1508 memcpy(req
.name
, res
->lockname
.name
, res
->lockname
.len
);
1510 ret
= o2net_send_message(DLM_MASTER_REQUERY_MSG
, dlm
->key
,
1511 &req
, sizeof(req
), nodenum
, &status
);
1512 /* XXX: negative status not handled properly here. */
1517 BUG_ON(status
> DLM_LOCK_RES_OWNER_UNKNOWN
);
1518 *real_master
= (u8
) (status
& 0xff);
1519 mlog(0, "node %u responded to master requery with %u\n",
1520 nodenum
, *real_master
);
1527 /* this function cannot error, so unless the sending
1528 * or receiving of the message failed, the owner can
1530 int dlm_master_requery_handler(struct o2net_msg
*msg
, u32 len
, void *data
)
1532 struct dlm_ctxt
*dlm
= data
;
1533 struct dlm_master_requery
*req
= (struct dlm_master_requery
*)msg
->buf
;
1534 struct dlm_lock_resource
*res
= NULL
;
1536 int master
= DLM_LOCK_RES_OWNER_UNKNOWN
;
1537 u32 flags
= DLM_ASSERT_MASTER_REQUERY
;
1539 if (!dlm_grab(dlm
)) {
1540 /* since the domain has gone away on this
1541 * node, the proper response is UNKNOWN */
1545 hash
= dlm_lockid_hash(req
->name
, req
->namelen
);
1547 spin_lock(&dlm
->spinlock
);
1548 res
= __dlm_lookup_lockres(dlm
, req
->name
, req
->namelen
, hash
);
1550 spin_lock(&res
->spinlock
);
1551 master
= res
->owner
;
1552 if (master
== dlm
->node_num
) {
1553 int ret
= dlm_dispatch_assert_master(dlm
, res
,
1556 mlog_errno(-ENOMEM
);
1561 spin_unlock(&res
->spinlock
);
1563 spin_unlock(&dlm
->spinlock
);
1569 static inline struct list_head
*
1570 dlm_list_num_to_pointer(struct dlm_lock_resource
*res
, int list_num
)
1572 struct list_head
*ret
;
1573 BUG_ON(list_num
< 0);
1574 BUG_ON(list_num
> 2);
1575 ret
= &(res
->granted
);
1579 /* TODO: do ast flush business
1580 * TODO: do MIGRATING and RECOVERING spinning
1584 * NOTE about in-flight requests during migration:
1586 * Before attempting the migrate, the master has marked the lockres as
1587 * MIGRATING and then flushed all of its pending ASTS. So any in-flight
1588 * requests either got queued before the MIGRATING flag got set, in which
1589 * case the lock data will reflect the change and a return message is on
1590 * the way, or the request failed to get in before MIGRATING got set. In
1591 * this case, the caller will be told to spin and wait for the MIGRATING
1592 * flag to be dropped, then recheck the master.
1593 * This holds true for the convert, cancel and unlock cases, and since lvb
1594 * updates are tied to these same messages, it applies to lvb updates as
1595 * well. For the lock case, there is no way a lock can be on the master
1596 * queue and not be on the secondary queue since the lock is always added
1597 * locally first. This means that the new target node will never be sent
1598 * a lock that he doesn't already have on the list.
1599 * In total, this means that the local lock is correct and should not be
1600 * updated to match the one sent by the master. Any messages sent back
1601 * from the master before the MIGRATING flag will bring the lock properly
1602 * up-to-date, and the change will be ordered properly for the waiter.
1603 * We will *not* attempt to modify the lock underneath the waiter.
1606 static int dlm_process_recovery_data(struct dlm_ctxt
*dlm
,
1607 struct dlm_lock_resource
*res
,
1608 struct dlm_migratable_lockres
*mres
)
1610 struct dlm_migratable_lock
*ml
;
1611 struct list_head
*queue
;
1612 struct dlm_lock
*newlock
= NULL
;
1613 struct dlm_lockstatus
*lksb
= NULL
;
1616 struct list_head
*iter
;
1617 struct dlm_lock
*lock
= NULL
;
1619 mlog(0, "running %d locks for this lockres\n", mres
->num_locks
);
1620 for (i
=0; i
<mres
->num_locks
; i
++) {
1621 ml
= &(mres
->ml
[i
]);
1622 BUG_ON(ml
->highest_blocked
!= LKM_IVMODE
);
1626 queue
= dlm_list_num_to_pointer(res
, ml
->list
);
1628 /* if the lock is for the local node it needs to
1629 * be moved to the proper location within the queue.
1630 * do not allocate a new lock structure. */
1631 if (ml
->node
== dlm
->node_num
) {
1632 /* MIGRATION ONLY! */
1633 BUG_ON(!(mres
->flags
& DLM_MRES_MIGRATION
));
1635 spin_lock(&res
->spinlock
);
1636 list_for_each(iter
, queue
) {
1637 lock
= list_entry (iter
, struct dlm_lock
, list
);
1638 if (lock
->ml
.cookie
!= ml
->cookie
)
1644 /* lock is always created locally first, and
1645 * destroyed locally last. it must be on the list */
1648 mlog(ML_ERROR
, "could not find local lock "
1649 "with cookie %u:%llu!\n",
1650 dlm_get_lock_cookie_node(c
),
1651 dlm_get_lock_cookie_seq(c
));
1654 BUG_ON(lock
->ml
.node
!= ml
->node
);
1656 /* see NOTE above about why we do not update
1657 * to match the master here */
1659 /* move the lock to its proper place */
1660 /* do not alter lock refcount. switching lists. */
1661 list_move_tail(&lock
->list
, queue
);
1662 spin_unlock(&res
->spinlock
);
1664 mlog(0, "just reordered a local lock!\n");
1668 /* lock is for another node. */
1669 newlock
= dlm_new_lock(ml
->type
, ml
->node
,
1670 be64_to_cpu(ml
->cookie
), NULL
);
1675 lksb
= newlock
->lksb
;
1676 dlm_lock_attach_lockres(newlock
, res
);
1678 if (ml
->convert_type
!= LKM_IVMODE
) {
1679 BUG_ON(queue
!= &res
->converting
);
1680 newlock
->ml
.convert_type
= ml
->convert_type
;
1682 lksb
->flags
|= (ml
->flags
&
1683 (DLM_LKSB_PUT_LVB
|DLM_LKSB_GET_LVB
));
1685 if (ml
->type
== LKM_NLMODE
)
1688 if (!dlm_lvb_is_empty(mres
->lvb
)) {
1689 if (lksb
->flags
& DLM_LKSB_PUT_LVB
) {
1690 /* other node was trying to update
1691 * lvb when node died. recreate the
1692 * lksb with the updated lvb. */
1693 memcpy(lksb
->lvb
, mres
->lvb
, DLM_LVB_LEN
);
1694 /* the lock resource lvb update must happen
1695 * NOW, before the spinlock is dropped.
1696 * we no longer wait for the AST to update
1698 memcpy(res
->lvb
, mres
->lvb
, DLM_LVB_LEN
);
1700 /* otherwise, the node is sending its
1701 * most recent valid lvb info */
1702 BUG_ON(ml
->type
!= LKM_EXMODE
&&
1703 ml
->type
!= LKM_PRMODE
);
1704 if (!dlm_lvb_is_empty(res
->lvb
) &&
1705 (ml
->type
== LKM_EXMODE
||
1706 memcmp(res
->lvb
, mres
->lvb
, DLM_LVB_LEN
))) {
1708 mlog(ML_ERROR
, "%s:%.*s: received bad "
1709 "lvb! type=%d\n", dlm
->name
,
1711 res
->lockname
.name
, ml
->type
);
1712 printk("lockres lvb=[");
1713 for (i
=0; i
<DLM_LVB_LEN
; i
++)
1714 printk("%02x", res
->lvb
[i
]);
1715 printk("]\nmigrated lvb=[");
1716 for (i
=0; i
<DLM_LVB_LEN
; i
++)
1717 printk("%02x", mres
->lvb
[i
]);
1719 dlm_print_one_lock_resource(res
);
1722 memcpy(res
->lvb
, mres
->lvb
, DLM_LVB_LEN
);
1728 * wrt lock queue ordering and recovery:
1729 * 1. order of locks on granted queue is
1731 * 2. order of locks on converting queue is
1732 * LOST with the node death. sorry charlie.
1733 * 3. order of locks on the blocked queue is
1735 * order of locks does not affect integrity, it
1736 * just means that a lock request may get pushed
1737 * back in line as a result of the node death.
1738 * also note that for a given node the lock order
1739 * for its secondary queue locks is preserved
1740 * relative to each other, but clearly *not*
1741 * preserved relative to locks from other nodes.
1744 spin_lock(&res
->spinlock
);
1745 list_for_each_entry(lock
, queue
, list
) {
1746 if (lock
->ml
.cookie
== ml
->cookie
) {
1747 u64 c
= lock
->ml
.cookie
;
1748 mlog(ML_ERROR
, "%s:%.*s: %u:%llu: lock already "
1749 "exists on this lockres!\n", dlm
->name
,
1750 res
->lockname
.len
, res
->lockname
.name
,
1751 dlm_get_lock_cookie_node(c
),
1752 dlm_get_lock_cookie_seq(c
));
1754 mlog(ML_NOTICE
, "sent lock: type=%d, conv=%d, "
1755 "node=%u, cookie=%u:%llu, queue=%d\n",
1756 ml
->type
, ml
->convert_type
, ml
->node
,
1757 dlm_get_lock_cookie_node(ml
->cookie
),
1758 dlm_get_lock_cookie_seq(ml
->cookie
),
1761 __dlm_print_one_lock_resource(res
);
1767 dlm_lock_get(newlock
);
1768 list_add_tail(&newlock
->list
, queue
);
1770 spin_unlock(&res
->spinlock
);
1772 mlog(0, "done running all the locks\n");
1778 dlm_lock_put(newlock
);
1785 void dlm_move_lockres_to_recovery_list(struct dlm_ctxt
*dlm
,
1786 struct dlm_lock_resource
*res
)
1789 struct list_head
*queue
, *iter
, *iter2
;
1790 struct dlm_lock
*lock
;
1792 res
->state
|= DLM_LOCK_RES_RECOVERING
;
1793 if (!list_empty(&res
->recovering
)) {
1795 "Recovering res %s:%.*s, is already on recovery list!\n",
1796 dlm
->name
, res
->lockname
.len
, res
->lockname
.name
);
1797 list_del_init(&res
->recovering
);
1799 /* We need to hold a reference while on the recovery list */
1800 dlm_lockres_get(res
);
1801 list_add_tail(&res
->recovering
, &dlm
->reco
.resources
);
1803 /* find any pending locks and put them back on proper list */
1804 for (i
=DLM_BLOCKED_LIST
; i
>=DLM_GRANTED_LIST
; i
--) {
1805 queue
= dlm_list_idx_to_ptr(res
, i
);
1806 list_for_each_safe(iter
, iter2
, queue
) {
1807 lock
= list_entry (iter
, struct dlm_lock
, list
);
1809 if (lock
->convert_pending
) {
1810 /* move converting lock back to granted */
1811 BUG_ON(i
!= DLM_CONVERTING_LIST
);
1812 mlog(0, "node died with convert pending "
1813 "on %.*s. move back to granted list.\n",
1814 res
->lockname
.len
, res
->lockname
.name
);
1815 dlm_revert_pending_convert(res
, lock
);
1816 lock
->convert_pending
= 0;
1817 } else if (lock
->lock_pending
) {
1818 /* remove pending lock requests completely */
1819 BUG_ON(i
!= DLM_BLOCKED_LIST
);
1820 mlog(0, "node died with lock pending "
1821 "on %.*s. remove from blocked list and skip.\n",
1822 res
->lockname
.len
, res
->lockname
.name
);
1823 /* lock will be floating until ref in
1824 * dlmlock_remote is freed after the network
1825 * call returns. ok for it to not be on any
1826 * list since no ast can be called
1827 * (the master is dead). */
1828 dlm_revert_pending_lock(res
, lock
);
1829 lock
->lock_pending
= 0;
1830 } else if (lock
->unlock_pending
) {
1831 /* if an unlock was in progress, treat as
1832 * if this had completed successfully
1833 * before sending this lock state to the
1834 * new master. note that the dlm_unlock
1835 * call is still responsible for calling
1836 * the unlockast. that will happen after
1837 * the network call times out. for now,
1838 * just move lists to prepare the new
1839 * recovery master. */
1840 BUG_ON(i
!= DLM_GRANTED_LIST
);
1841 mlog(0, "node died with unlock pending "
1842 "on %.*s. remove from blocked list and skip.\n",
1843 res
->lockname
.len
, res
->lockname
.name
);
1844 dlm_commit_pending_unlock(res
, lock
);
1845 lock
->unlock_pending
= 0;
1846 } else if (lock
->cancel_pending
) {
1847 /* if a cancel was in progress, treat as
1848 * if this had completed successfully
1849 * before sending this lock state to the
1851 BUG_ON(i
!= DLM_CONVERTING_LIST
);
1852 mlog(0, "node died with cancel pending "
1853 "on %.*s. move back to granted list.\n",
1854 res
->lockname
.len
, res
->lockname
.name
);
1855 dlm_commit_pending_cancel(res
, lock
);
1856 lock
->cancel_pending
= 0;
1865 /* removes all recovered locks from the recovery list.
1866 * sets the res->owner to the new master.
1867 * unsets the RECOVERY flag and wakes waiters. */
1868 static void dlm_finish_local_lockres_recovery(struct dlm_ctxt
*dlm
,
1869 u8 dead_node
, u8 new_master
)
1872 struct list_head
*iter
, *iter2
;
1873 struct hlist_node
*hash_iter
;
1874 struct hlist_head
*bucket
;
1876 struct dlm_lock_resource
*res
;
1880 assert_spin_locked(&dlm
->spinlock
);
1882 list_for_each_safe(iter
, iter2
, &dlm
->reco
.resources
) {
1883 res
= list_entry (iter
, struct dlm_lock_resource
, recovering
);
1884 if (res
->owner
== dead_node
) {
1885 list_del_init(&res
->recovering
);
1886 spin_lock(&res
->spinlock
);
1887 dlm_change_lockres_owner(dlm
, res
, new_master
);
1888 res
->state
&= ~DLM_LOCK_RES_RECOVERING
;
1889 if (!__dlm_lockres_unused(res
))
1890 __dlm_dirty_lockres(dlm
, res
);
1891 spin_unlock(&res
->spinlock
);
1893 dlm_lockres_put(res
);
1897 /* this will become unnecessary eventually, but
1898 * for now we need to run the whole hash, clear
1899 * the RECOVERING state and set the owner
1901 for (i
= 0; i
< DLM_HASH_BUCKETS
; i
++) {
1902 bucket
= dlm_lockres_hash(dlm
, i
);
1903 hlist_for_each_entry(res
, hash_iter
, bucket
, hash_node
) {
1904 if (res
->state
& DLM_LOCK_RES_RECOVERING
) {
1905 if (res
->owner
== dead_node
) {
1906 mlog(0, "(this=%u) res %.*s owner=%u "
1907 "was not on recovering list, but "
1908 "clearing state anyway\n",
1909 dlm
->node_num
, res
->lockname
.len
,
1910 res
->lockname
.name
, new_master
);
1911 } else if (res
->owner
== dlm
->node_num
) {
1912 mlog(0, "(this=%u) res %.*s owner=%u "
1913 "was not on recovering list, "
1914 "owner is THIS node, clearing\n",
1915 dlm
->node_num
, res
->lockname
.len
,
1916 res
->lockname
.name
, new_master
);
1920 if (!list_empty(&res
->recovering
)) {
1921 mlog(0, "%s:%.*s: lockres was "
1922 "marked RECOVERING, owner=%u\n",
1923 dlm
->name
, res
->lockname
.len
,
1924 res
->lockname
.name
, res
->owner
);
1925 list_del_init(&res
->recovering
);
1926 dlm_lockres_put(res
);
1928 spin_lock(&res
->spinlock
);
1929 dlm_change_lockres_owner(dlm
, res
, new_master
);
1930 res
->state
&= ~DLM_LOCK_RES_RECOVERING
;
1931 if (!__dlm_lockres_unused(res
))
1932 __dlm_dirty_lockres(dlm
, res
);
1933 spin_unlock(&res
->spinlock
);
1940 static inline int dlm_lvb_needs_invalidation(struct dlm_lock
*lock
, int local
)
1943 if (lock
->ml
.type
!= LKM_EXMODE
&&
1944 lock
->ml
.type
!= LKM_PRMODE
)
1946 } else if (lock
->ml
.type
== LKM_EXMODE
)
1951 static void dlm_revalidate_lvb(struct dlm_ctxt
*dlm
,
1952 struct dlm_lock_resource
*res
, u8 dead_node
)
1954 struct list_head
*iter
, *queue
;
1955 struct dlm_lock
*lock
;
1956 int blank_lvb
= 0, local
= 0;
1960 assert_spin_locked(&dlm
->spinlock
);
1961 assert_spin_locked(&res
->spinlock
);
1963 if (res
->owner
== dlm
->node_num
)
1964 /* if this node owned the lockres, and if the dead node
1965 * had an EX when he died, blank out the lvb */
1966 search_node
= dead_node
;
1968 /* if this is a secondary lockres, and we had no EX or PR
1969 * locks granted, we can no longer trust the lvb */
1970 search_node
= dlm
->node_num
;
1971 local
= 1; /* check local state for valid lvb */
1974 for (i
=DLM_GRANTED_LIST
; i
<=DLM_CONVERTING_LIST
; i
++) {
1975 queue
= dlm_list_idx_to_ptr(res
, i
);
1976 list_for_each(iter
, queue
) {
1977 lock
= list_entry (iter
, struct dlm_lock
, list
);
1978 if (lock
->ml
.node
== search_node
) {
1979 if (dlm_lvb_needs_invalidation(lock
, local
)) {
1980 /* zero the lksb lvb and lockres lvb */
1982 memset(lock
->lksb
->lvb
, 0, DLM_LVB_LEN
);
1989 mlog(0, "clearing %.*s lvb, dead node %u had EX\n",
1990 res
->lockname
.len
, res
->lockname
.name
, dead_node
);
1991 memset(res
->lvb
, 0, DLM_LVB_LEN
);
1995 static void dlm_free_dead_locks(struct dlm_ctxt
*dlm
,
1996 struct dlm_lock_resource
*res
, u8 dead_node
)
1998 struct list_head
*iter
, *tmpiter
;
1999 struct dlm_lock
*lock
;
2001 /* this node is the lockres master:
2002 * 1) remove any stale locks for the dead node
2003 * 2) if the dead node had an EX when he died, blank out the lvb
2005 assert_spin_locked(&dlm
->spinlock
);
2006 assert_spin_locked(&res
->spinlock
);
2008 /* TODO: check pending_asts, pending_basts here */
2009 list_for_each_safe(iter
, tmpiter
, &res
->granted
) {
2010 lock
= list_entry (iter
, struct dlm_lock
, list
);
2011 if (lock
->ml
.node
== dead_node
) {
2012 list_del_init(&lock
->list
);
2016 list_for_each_safe(iter
, tmpiter
, &res
->converting
) {
2017 lock
= list_entry (iter
, struct dlm_lock
, list
);
2018 if (lock
->ml
.node
== dead_node
) {
2019 list_del_init(&lock
->list
);
2023 list_for_each_safe(iter
, tmpiter
, &res
->blocked
) {
2024 lock
= list_entry (iter
, struct dlm_lock
, list
);
2025 if (lock
->ml
.node
== dead_node
) {
2026 list_del_init(&lock
->list
);
2031 /* do not kick thread yet */
2032 __dlm_dirty_lockres(dlm
, res
);
2035 /* if this node is the recovery master, and there are no
2036 * locks for a given lockres owned by this node that are in
2037 * either PR or EX mode, zero out the lvb before requesting.
2042 static void dlm_do_local_recovery_cleanup(struct dlm_ctxt
*dlm
, u8 dead_node
)
2044 struct hlist_node
*iter
;
2045 struct dlm_lock_resource
*res
;
2047 struct hlist_head
*bucket
;
2048 struct dlm_lock
*lock
;
2051 /* purge any stale mles */
2052 dlm_clean_master_list(dlm
, dead_node
);
2055 * now clean up all lock resources. there are two rules:
2057 * 1) if the dead node was the master, move the lockres
2058 * to the recovering list. set the RECOVERING flag.
2059 * this lockres needs to be cleaned up before it can
2062 * 2) if this node was the master, remove all locks from
2063 * each of the lockres queues that were owned by the
2064 * dead node. once recovery finishes, the dlm thread
2065 * can be kicked again to see if any ASTs or BASTs
2066 * need to be fired as a result.
2068 for (i
= 0; i
< DLM_HASH_BUCKETS
; i
++) {
2069 bucket
= dlm_lockres_hash(dlm
, i
);
2070 hlist_for_each_entry(res
, iter
, bucket
, hash_node
) {
2071 /* always prune any $RECOVERY entries for dead nodes,
2072 * otherwise hangs can occur during later recovery */
2073 if (dlm_is_recovery_lock(res
->lockname
.name
,
2074 res
->lockname
.len
)) {
2075 spin_lock(&res
->spinlock
);
2076 list_for_each_entry(lock
, &res
->granted
, list
) {
2077 if (lock
->ml
.node
== dead_node
) {
2078 mlog(0, "AHA! there was "
2079 "a $RECOVERY lock for dead "
2081 dead_node
, dlm
->name
);
2082 list_del_init(&lock
->list
);
2087 spin_unlock(&res
->spinlock
);
2090 spin_lock(&res
->spinlock
);
2091 /* zero the lvb if necessary */
2092 dlm_revalidate_lvb(dlm
, res
, dead_node
);
2093 if (res
->owner
== dead_node
)
2094 dlm_move_lockres_to_recovery_list(dlm
, res
);
2095 else if (res
->owner
== dlm
->node_num
) {
2096 dlm_free_dead_locks(dlm
, res
, dead_node
);
2097 __dlm_lockres_calc_usage(dlm
, res
);
2099 spin_unlock(&res
->spinlock
);
2105 static void __dlm_hb_node_down(struct dlm_ctxt
*dlm
, int idx
)
2107 assert_spin_locked(&dlm
->spinlock
);
2109 if (dlm
->reco
.new_master
== idx
) {
2110 mlog(0, "%s: recovery master %d just died\n",
2112 if (dlm
->reco
.state
& DLM_RECO_STATE_FINALIZE
) {
2113 /* finalize1 was reached, so it is safe to clear
2114 * the new_master and dead_node. that recovery
2116 mlog(0, "%s: dead master %d had reached "
2117 "finalize1 state, clearing\n", dlm
->name
, idx
);
2118 dlm
->reco
.state
&= ~DLM_RECO_STATE_FINALIZE
;
2119 __dlm_reset_recovery(dlm
);
2123 /* check to see if the node is already considered dead */
2124 if (!test_bit(idx
, dlm
->live_nodes_map
)) {
2125 mlog(0, "for domain %s, node %d is already dead. "
2126 "another node likely did recovery already.\n",
2131 /* check to see if we do not care about this node */
2132 if (!test_bit(idx
, dlm
->domain_map
)) {
2133 /* This also catches the case that we get a node down
2134 * but haven't joined the domain yet. */
2135 mlog(0, "node %u already removed from domain!\n", idx
);
2139 clear_bit(idx
, dlm
->live_nodes_map
);
2141 /* Clean up join state on node death. */
2142 if (dlm
->joining_node
== idx
) {
2143 mlog(0, "Clearing join state for node %u\n", idx
);
2144 __dlm_set_joining_node(dlm
, DLM_LOCK_RES_OWNER_UNKNOWN
);
2147 /* make sure local cleanup occurs before the heartbeat events */
2148 if (!test_bit(idx
, dlm
->recovery_map
))
2149 dlm_do_local_recovery_cleanup(dlm
, idx
);
2151 /* notify anything attached to the heartbeat events */
2152 dlm_hb_event_notify_attached(dlm
, idx
, 0);
2154 mlog(0, "node %u being removed from domain map!\n", idx
);
2155 clear_bit(idx
, dlm
->domain_map
);
2156 /* wake up migration waiters if a node goes down.
2157 * perhaps later we can genericize this for other waiters. */
2158 wake_up(&dlm
->migration_wq
);
2160 if (test_bit(idx
, dlm
->recovery_map
))
2161 mlog(0, "domain %s, node %u already added "
2162 "to recovery map!\n", dlm
->name
, idx
);
2164 set_bit(idx
, dlm
->recovery_map
);
2167 void dlm_hb_node_down_cb(struct o2nm_node
*node
, int idx
, void *data
)
2169 struct dlm_ctxt
*dlm
= data
;
2174 spin_lock(&dlm
->spinlock
);
2175 __dlm_hb_node_down(dlm
, idx
);
2176 spin_unlock(&dlm
->spinlock
);
2181 void dlm_hb_node_up_cb(struct o2nm_node
*node
, int idx
, void *data
)
2183 struct dlm_ctxt
*dlm
= data
;
2188 spin_lock(&dlm
->spinlock
);
2189 set_bit(idx
, dlm
->live_nodes_map
);
2190 /* do NOT notify mle attached to the heartbeat events.
2191 * new nodes are not interesting in mastery until joined. */
2192 spin_unlock(&dlm
->spinlock
);
2197 static void dlm_reco_ast(void *astdata
)
2199 struct dlm_ctxt
*dlm
= astdata
;
2200 mlog(0, "ast for recovery lock fired!, this=%u, dlm=%s\n",
2201 dlm
->node_num
, dlm
->name
);
2203 static void dlm_reco_bast(void *astdata
, int blocked_type
)
2205 struct dlm_ctxt
*dlm
= astdata
;
2206 mlog(0, "bast for recovery lock fired!, this=%u, dlm=%s\n",
2207 dlm
->node_num
, dlm
->name
);
2209 static void dlm_reco_unlock_ast(void *astdata
, enum dlm_status st
)
2211 mlog(0, "unlockast for recovery lock fired!\n");
2215 * dlm_pick_recovery_master will continually attempt to use
2216 * dlmlock() on the special "$RECOVERY" lockres with the
2217 * LKM_NOQUEUE flag to get an EX. every thread that enters
2218 * this function on each node racing to become the recovery
2219 * master will not stop attempting this until either:
2220 * a) this node gets the EX (and becomes the recovery master),
2221 * or b) dlm->reco.new_master gets set to some nodenum
2222 * != O2NM_INVALID_NODE_NUM (another node will do the reco).
2223 * so each time a recovery master is needed, the entire cluster
2224 * will sync at this point. if the new master dies, that will
2225 * be detected in dlm_do_recovery */
2226 static int dlm_pick_recovery_master(struct dlm_ctxt
*dlm
)
2228 enum dlm_status ret
;
2229 struct dlm_lockstatus lksb
;
2230 int status
= -EINVAL
;
2232 mlog(0, "starting recovery of %s at %lu, dead=%u, this=%u\n",
2233 dlm
->name
, jiffies
, dlm
->reco
.dead_node
, dlm
->node_num
);
2235 memset(&lksb
, 0, sizeof(lksb
));
2237 ret
= dlmlock(dlm
, LKM_EXMODE
, &lksb
, LKM_NOQUEUE
|LKM_RECOVERY
,
2238 DLM_RECOVERY_LOCK_NAME
, dlm_reco_ast
, dlm
, dlm_reco_bast
);
2240 mlog(0, "%s: dlmlock($RECOVERY) returned %d, lksb=%d\n",
2241 dlm
->name
, ret
, lksb
.status
);
2243 if (ret
== DLM_NORMAL
) {
2244 mlog(0, "dlm=%s dlmlock says I got it (this=%u)\n",
2245 dlm
->name
, dlm
->node_num
);
2247 /* got the EX lock. check to see if another node
2248 * just became the reco master */
2249 if (dlm_reco_master_ready(dlm
)) {
2250 mlog(0, "%s: got reco EX lock, but %u will "
2251 "do the recovery\n", dlm
->name
,
2252 dlm
->reco
.new_master
);
2257 /* see if recovery was already finished elsewhere */
2258 spin_lock(&dlm
->spinlock
);
2259 if (dlm
->reco
.dead_node
== O2NM_INVALID_NODE_NUM
) {
2261 mlog(0, "%s: got reco EX lock, but "
2262 "node got recovered already\n", dlm
->name
);
2263 if (dlm
->reco
.new_master
!= O2NM_INVALID_NODE_NUM
) {
2264 mlog(ML_ERROR
, "%s: new master is %u "
2265 "but no dead node!\n",
2266 dlm
->name
, dlm
->reco
.new_master
);
2270 spin_unlock(&dlm
->spinlock
);
2273 /* if this node has actually become the recovery master,
2274 * set the master and send the messages to begin recovery */
2276 mlog(0, "%s: dead=%u, this=%u, sending "
2277 "begin_reco now\n", dlm
->name
,
2278 dlm
->reco
.dead_node
, dlm
->node_num
);
2279 status
= dlm_send_begin_reco_message(dlm
,
2280 dlm
->reco
.dead_node
);
2281 /* this always succeeds */
2284 /* set the new_master to this node */
2285 spin_lock(&dlm
->spinlock
);
2286 dlm_set_reco_master(dlm
, dlm
->node_num
);
2287 spin_unlock(&dlm
->spinlock
);
2290 /* recovery lock is a special case. ast will not get fired,
2291 * so just go ahead and unlock it. */
2292 ret
= dlmunlock(dlm
, &lksb
, 0, dlm_reco_unlock_ast
, dlm
);
2293 if (ret
== DLM_DENIED
) {
2294 mlog(0, "got DLM_DENIED, trying LKM_CANCEL\n");
2295 ret
= dlmunlock(dlm
, &lksb
, LKM_CANCEL
, dlm_reco_unlock_ast
, dlm
);
2297 if (ret
!= DLM_NORMAL
) {
2298 /* this would really suck. this could only happen
2299 * if there was a network error during the unlock
2300 * because of node death. this means the unlock
2301 * is actually "done" and the lock structure is
2302 * even freed. we can continue, but only
2303 * because this specific lock name is special. */
2304 mlog(ML_ERROR
, "dlmunlock returned %d\n", ret
);
2306 } else if (ret
== DLM_NOTQUEUED
) {
2307 mlog(0, "dlm=%s dlmlock says another node got it (this=%u)\n",
2308 dlm
->name
, dlm
->node_num
);
2309 /* another node is master. wait on
2310 * reco.new_master != O2NM_INVALID_NODE_NUM
2311 * for at most one second */
2312 wait_event_timeout(dlm
->dlm_reco_thread_wq
,
2313 dlm_reco_master_ready(dlm
),
2314 msecs_to_jiffies(1000));
2315 if (!dlm_reco_master_ready(dlm
)) {
2316 mlog(0, "%s: reco master taking awhile\n",
2320 /* another node has informed this one that it is reco master */
2321 mlog(0, "%s: reco master %u is ready to recover %u\n",
2322 dlm
->name
, dlm
->reco
.new_master
, dlm
->reco
.dead_node
);
2324 } else if (ret
== DLM_RECOVERING
) {
2325 mlog(0, "dlm=%s dlmlock says master node died (this=%u)\n",
2326 dlm
->name
, dlm
->node_num
);
2329 struct dlm_lock_resource
*res
;
2331 /* dlmlock returned something other than NOTQUEUED or NORMAL */
2332 mlog(ML_ERROR
, "%s: got %s from dlmlock($RECOVERY), "
2333 "lksb.status=%s\n", dlm
->name
, dlm_errname(ret
),
2334 dlm_errname(lksb
.status
));
2335 res
= dlm_lookup_lockres(dlm
, DLM_RECOVERY_LOCK_NAME
,
2336 DLM_RECOVERY_LOCK_NAME_LEN
);
2338 dlm_print_one_lock_resource(res
);
2339 dlm_lockres_put(res
);
2341 mlog(ML_ERROR
, "recovery lock not found\n");
2349 static int dlm_send_begin_reco_message(struct dlm_ctxt
*dlm
, u8 dead_node
)
2351 struct dlm_begin_reco br
;
2353 struct dlm_node_iter iter
;
2357 mlog_entry("%u\n", dead_node
);
2359 mlog(0, "%s: dead node is %u\n", dlm
->name
, dead_node
);
2361 spin_lock(&dlm
->spinlock
);
2362 dlm_node_iter_init(dlm
->domain_map
, &iter
);
2363 spin_unlock(&dlm
->spinlock
);
2365 clear_bit(dead_node
, iter
.node_map
);
2367 memset(&br
, 0, sizeof(br
));
2368 br
.node_idx
= dlm
->node_num
;
2369 br
.dead_node
= dead_node
;
2371 while ((nodenum
= dlm_node_iter_next(&iter
)) >= 0) {
2373 if (nodenum
== dead_node
) {
2374 mlog(0, "not sending begin reco to dead node "
2378 if (nodenum
== dlm
->node_num
) {
2379 mlog(0, "not sending begin reco to self\n");
2384 mlog(0, "attempting to send begin reco msg to %d\n",
2386 ret
= o2net_send_message(DLM_BEGIN_RECO_MSG
, dlm
->key
,
2387 &br
, sizeof(br
), nodenum
, &status
);
2388 /* negative status is handled ok by caller here */
2391 if (dlm_is_host_down(ret
)) {
2392 /* node is down. not involved in recovery
2393 * so just keep going */
2394 mlog(0, "%s: node %u was down when sending "
2395 "begin reco msg (%d)\n", dlm
->name
, nodenum
, ret
);
2399 struct dlm_lock_resource
*res
;
2400 /* this is now a serious problem, possibly ENOMEM
2401 * in the network stack. must retry */
2403 mlog(ML_ERROR
, "begin reco of dlm %s to node %u "
2404 " returned %d\n", dlm
->name
, nodenum
, ret
);
2405 res
= dlm_lookup_lockres(dlm
, DLM_RECOVERY_LOCK_NAME
,
2406 DLM_RECOVERY_LOCK_NAME_LEN
);
2408 dlm_print_one_lock_resource(res
);
2409 dlm_lockres_put(res
);
2411 mlog(ML_ERROR
, "recovery lock not found\n");
2413 /* sleep for a bit in hopes that we can avoid
2417 } else if (ret
== EAGAIN
) {
2418 mlog(0, "%s: trying to start recovery of node "
2419 "%u, but node %u is waiting for last recovery "
2420 "to complete, backoff for a bit\n", dlm
->name
,
2421 dead_node
, nodenum
);
2422 /* TODO Look into replacing msleep with cond_resched() */
2431 int dlm_begin_reco_handler(struct o2net_msg
*msg
, u32 len
, void *data
)
2433 struct dlm_ctxt
*dlm
= data
;
2434 struct dlm_begin_reco
*br
= (struct dlm_begin_reco
*)msg
->buf
;
2436 /* ok to return 0, domain has gone away */
2440 spin_lock(&dlm
->spinlock
);
2441 if (dlm
->reco
.state
& DLM_RECO_STATE_FINALIZE
) {
2442 mlog(0, "%s: node %u wants to recover node %u (%u:%u) "
2443 "but this node is in finalize state, waiting on finalize2\n",
2444 dlm
->name
, br
->node_idx
, br
->dead_node
,
2445 dlm
->reco
.dead_node
, dlm
->reco
.new_master
);
2446 spin_unlock(&dlm
->spinlock
);
2449 spin_unlock(&dlm
->spinlock
);
2451 mlog(0, "%s: node %u wants to recover node %u (%u:%u)\n",
2452 dlm
->name
, br
->node_idx
, br
->dead_node
,
2453 dlm
->reco
.dead_node
, dlm
->reco
.new_master
);
2455 dlm_fire_domain_eviction_callbacks(dlm
, br
->dead_node
);
2457 spin_lock(&dlm
->spinlock
);
2458 if (dlm
->reco
.new_master
!= O2NM_INVALID_NODE_NUM
) {
2459 if (test_bit(dlm
->reco
.new_master
, dlm
->recovery_map
)) {
2460 mlog(0, "%s: new_master %u died, changing "
2461 "to %u\n", dlm
->name
, dlm
->reco
.new_master
,
2464 mlog(0, "%s: new_master %u NOT DEAD, changing "
2465 "to %u\n", dlm
->name
, dlm
->reco
.new_master
,
2467 /* may not have seen the new master as dead yet */
2470 if (dlm
->reco
.dead_node
!= O2NM_INVALID_NODE_NUM
) {
2471 mlog(ML_NOTICE
, "%s: dead_node previously set to %u, "
2472 "node %u changing it to %u\n", dlm
->name
,
2473 dlm
->reco
.dead_node
, br
->node_idx
, br
->dead_node
);
2475 dlm_set_reco_master(dlm
, br
->node_idx
);
2476 dlm_set_reco_dead_node(dlm
, br
->dead_node
);
2477 if (!test_bit(br
->dead_node
, dlm
->recovery_map
)) {
2478 mlog(0, "recovery master %u sees %u as dead, but this "
2479 "node has not yet. marking %u as dead\n",
2480 br
->node_idx
, br
->dead_node
, br
->dead_node
);
2481 if (!test_bit(br
->dead_node
, dlm
->domain_map
) ||
2482 !test_bit(br
->dead_node
, dlm
->live_nodes_map
))
2483 mlog(0, "%u not in domain/live_nodes map "
2484 "so setting it in reco map manually\n",
2486 /* force the recovery cleanup in __dlm_hb_node_down
2487 * both of these will be cleared in a moment */
2488 set_bit(br
->dead_node
, dlm
->domain_map
);
2489 set_bit(br
->dead_node
, dlm
->live_nodes_map
);
2490 __dlm_hb_node_down(dlm
, br
->dead_node
);
2492 spin_unlock(&dlm
->spinlock
);
2494 dlm_kick_recovery_thread(dlm
);
2496 mlog(0, "%s: recovery started by node %u, for %u (%u:%u)\n",
2497 dlm
->name
, br
->node_idx
, br
->dead_node
,
2498 dlm
->reco
.dead_node
, dlm
->reco
.new_master
);
2504 #define DLM_FINALIZE_STAGE2 0x01
2505 static int dlm_send_finalize_reco_message(struct dlm_ctxt
*dlm
)
2508 struct dlm_finalize_reco fr
;
2509 struct dlm_node_iter iter
;
2514 mlog(0, "finishing recovery for node %s:%u, "
2515 "stage %d\n", dlm
->name
, dlm
->reco
.dead_node
, stage
);
2517 spin_lock(&dlm
->spinlock
);
2518 dlm_node_iter_init(dlm
->domain_map
, &iter
);
2519 spin_unlock(&dlm
->spinlock
);
2522 memset(&fr
, 0, sizeof(fr
));
2523 fr
.node_idx
= dlm
->node_num
;
2524 fr
.dead_node
= dlm
->reco
.dead_node
;
2526 fr
.flags
|= DLM_FINALIZE_STAGE2
;
2528 while ((nodenum
= dlm_node_iter_next(&iter
)) >= 0) {
2529 if (nodenum
== dlm
->node_num
)
2531 ret
= o2net_send_message(DLM_FINALIZE_RECO_MSG
, dlm
->key
,
2532 &fr
, sizeof(fr
), nodenum
, &status
);
2537 if (dlm_is_host_down(ret
)) {
2538 /* this has no effect on this recovery
2539 * session, so set the status to zero to
2540 * finish out the last recovery */
2541 mlog(ML_ERROR
, "node %u went down after this "
2542 "node finished recovery.\n", nodenum
);
2550 /* reset the node_iter back to the top and send finalize2 */
2559 int dlm_finalize_reco_handler(struct o2net_msg
*msg
, u32 len
, void *data
)
2561 struct dlm_ctxt
*dlm
= data
;
2562 struct dlm_finalize_reco
*fr
= (struct dlm_finalize_reco
*)msg
->buf
;
2565 /* ok to return 0, domain has gone away */
2569 if (fr
->flags
& DLM_FINALIZE_STAGE2
)
2572 mlog(0, "%s: node %u finalizing recovery stage%d of "
2573 "node %u (%u:%u)\n", dlm
->name
, fr
->node_idx
, stage
,
2574 fr
->dead_node
, dlm
->reco
.dead_node
, dlm
->reco
.new_master
);
2576 spin_lock(&dlm
->spinlock
);
2578 if (dlm
->reco
.new_master
!= fr
->node_idx
) {
2579 mlog(ML_ERROR
, "node %u sent recovery finalize msg, but node "
2580 "%u is supposed to be the new master, dead=%u\n",
2581 fr
->node_idx
, dlm
->reco
.new_master
, fr
->dead_node
);
2584 if (dlm
->reco
.dead_node
!= fr
->dead_node
) {
2585 mlog(ML_ERROR
, "node %u sent recovery finalize msg for dead "
2586 "node %u, but node %u is supposed to be dead\n",
2587 fr
->node_idx
, fr
->dead_node
, dlm
->reco
.dead_node
);
2593 dlm_finish_local_lockres_recovery(dlm
, fr
->dead_node
, fr
->node_idx
);
2594 if (dlm
->reco
.state
& DLM_RECO_STATE_FINALIZE
) {
2595 mlog(ML_ERROR
, "%s: received finalize1 from "
2596 "new master %u for dead node %u, but "
2597 "this node has already received it!\n",
2598 dlm
->name
, fr
->node_idx
, fr
->dead_node
);
2599 dlm_print_reco_node_status(dlm
);
2602 dlm
->reco
.state
|= DLM_RECO_STATE_FINALIZE
;
2603 spin_unlock(&dlm
->spinlock
);
2606 if (!(dlm
->reco
.state
& DLM_RECO_STATE_FINALIZE
)) {
2607 mlog(ML_ERROR
, "%s: received finalize2 from "
2608 "new master %u for dead node %u, but "
2609 "this node did not have finalize1!\n",
2610 dlm
->name
, fr
->node_idx
, fr
->dead_node
);
2611 dlm_print_reco_node_status(dlm
);
2614 dlm
->reco
.state
&= ~DLM_RECO_STATE_FINALIZE
;
2615 spin_unlock(&dlm
->spinlock
);
2616 dlm_reset_recovery(dlm
);
2617 dlm_kick_recovery_thread(dlm
);
2623 mlog(0, "%s: recovery done, reco master was %u, dead now %u, master now %u\n",
2624 dlm
->name
, fr
->node_idx
, dlm
->reco
.dead_node
, dlm
->reco
.new_master
);