1 /******************************************************************************
2 *******************************************************************************
4 ** Copyright (C) 2005-2011 Red Hat, Inc. All rights reserved.
6 ** This copyrighted material is made available to anyone wishing to use,
7 ** modify, copy, or redistribute it subject to the terms and conditions
8 ** of the GNU General Public License v.2.
10 *******************************************************************************
11 ******************************************************************************/
13 #include "dlm_internal.h"
14 #include "lockspace.h"
22 int dlm_slots_version(struct dlm_header
*h
)
24 if ((h
->h_version
& 0x0000FFFF) < DLM_HEADER_SLOTS
)
29 void dlm_slot_save(struct dlm_ls
*ls
, struct dlm_rcom
*rc
,
30 struct dlm_member
*memb
)
32 struct rcom_config
*rf
= (struct rcom_config
*)rc
->rc_buf
;
34 if (!dlm_slots_version(&rc
->rc_header
))
37 memb
->slot
= le16_to_cpu(rf
->rf_our_slot
);
38 memb
->generation
= le32_to_cpu(rf
->rf_generation
);
41 void dlm_slots_copy_out(struct dlm_ls
*ls
, struct dlm_rcom
*rc
)
43 struct dlm_slot
*slot
;
47 ro
= (struct rcom_slot
*)(rc
->rc_buf
+ sizeof(struct rcom_config
));
49 /* ls_slots array is sparse, but not rcom_slots */
51 for (i
= 0; i
< ls
->ls_slots_size
; i
++) {
52 slot
= &ls
->ls_slots
[i
];
55 ro
->ro_nodeid
= cpu_to_le32(slot
->nodeid
);
56 ro
->ro_slot
= cpu_to_le16(slot
->slot
);
61 #define SLOT_DEBUG_LINE 128
63 static void log_debug_slots(struct dlm_ls
*ls
, uint32_t gen
, int num_slots
,
64 struct rcom_slot
*ro0
, struct dlm_slot
*array
,
67 char line
[SLOT_DEBUG_LINE
];
68 int len
= SLOT_DEBUG_LINE
- 1;
72 if (!dlm_config
.ci_log_debug
)
75 memset(line
, 0, sizeof(line
));
78 for (i
= 0; i
< array_size
; i
++) {
82 ret
= snprintf(line
+ pos
, len
- pos
, " %d:%d",
83 array
[i
].slot
, array
[i
].nodeid
);
89 for (i
= 0; i
< num_slots
; i
++) {
90 ret
= snprintf(line
+ pos
, len
- pos
, " %d:%d",
91 ro0
[i
].ro_slot
, ro0
[i
].ro_nodeid
);
98 log_debug(ls
, "generation %u slots %d%s", gen
, num_slots
, line
);
101 int dlm_slots_copy_in(struct dlm_ls
*ls
)
103 struct dlm_member
*memb
;
104 struct dlm_rcom
*rc
= ls
->ls_recover_buf
;
105 struct rcom_config
*rf
= (struct rcom_config
*)rc
->rc_buf
;
106 struct rcom_slot
*ro0
, *ro
;
107 int our_nodeid
= dlm_our_nodeid();
111 if (!dlm_slots_version(&rc
->rc_header
))
114 gen
= le32_to_cpu(rf
->rf_generation
);
115 if (gen
<= ls
->ls_generation
) {
116 log_error(ls
, "dlm_slots_copy_in gen %u old %u",
117 gen
, ls
->ls_generation
);
119 ls
->ls_generation
= gen
;
121 num_slots
= le16_to_cpu(rf
->rf_num_slots
);
125 ro0
= (struct rcom_slot
*)(rc
->rc_buf
+ sizeof(struct rcom_config
));
127 for (i
= 0, ro
= ro0
; i
< num_slots
; i
++, ro
++) {
128 ro
->ro_nodeid
= le32_to_cpu(ro
->ro_nodeid
);
129 ro
->ro_slot
= le16_to_cpu(ro
->ro_slot
);
132 log_debug_slots(ls
, gen
, num_slots
, ro0
, NULL
, 0);
134 list_for_each_entry(memb
, &ls
->ls_nodes
, list
) {
135 for (i
= 0, ro
= ro0
; i
< num_slots
; i
++, ro
++) {
136 if (ro
->ro_nodeid
!= memb
->nodeid
)
138 memb
->slot
= ro
->ro_slot
;
139 memb
->slot_prev
= memb
->slot
;
143 if (memb
->nodeid
== our_nodeid
) {
144 if (ls
->ls_slot
&& ls
->ls_slot
!= memb
->slot
) {
145 log_error(ls
, "dlm_slots_copy_in our slot "
146 "changed %d %d", ls
->ls_slot
,
152 ls
->ls_slot
= memb
->slot
;
156 log_error(ls
, "dlm_slots_copy_in nodeid %d no slot",
165 /* for any nodes that do not support slots, we will not have set memb->slot
166 in wait_status_all(), so memb->slot will remain -1, and we will not
167 assign slots or set ls_num_slots here */
169 int dlm_slots_assign(struct dlm_ls
*ls
, int *num_slots
, int *slots_size
,
170 struct dlm_slot
**slots_out
, uint32_t *gen_out
)
172 struct dlm_member
*memb
;
173 struct dlm_slot
*array
;
174 int our_nodeid
= dlm_our_nodeid();
175 int array_size
, max_slots
, i
;
181 /* our own memb struct will have slot -1 gen 0 */
183 list_for_each_entry(memb
, &ls
->ls_nodes
, list
) {
184 if (memb
->nodeid
== our_nodeid
) {
185 memb
->slot
= ls
->ls_slot
;
186 memb
->generation
= ls
->ls_generation
;
191 list_for_each_entry(memb
, &ls
->ls_nodes
, list
) {
192 if (memb
->generation
> gen
)
193 gen
= memb
->generation
;
195 /* node doesn't support slots */
197 if (memb
->slot
== -1)
200 /* node needs a slot assigned */
205 /* node has a slot assigned */
209 if (!max
|| max
< memb
->slot
)
212 /* sanity check, once slot is assigned it shouldn't change */
214 if (memb
->slot_prev
&& memb
->slot
&& memb
->slot_prev
!= memb
->slot
) {
215 log_error(ls
, "nodeid %d slot changed %d %d",
216 memb
->nodeid
, memb
->slot_prev
, memb
->slot
);
219 memb
->slot_prev
= memb
->slot
;
222 array_size
= max
+ need
;
224 array
= kzalloc(array_size
* sizeof(struct dlm_slot
), GFP_NOFS
);
230 /* fill in slots (offsets) that are used */
232 list_for_each_entry(memb
, &ls
->ls_nodes
, list
) {
236 if (memb
->slot
> array_size
) {
237 log_error(ls
, "invalid slot number %d", memb
->slot
);
242 array
[memb
->slot
- 1].nodeid
= memb
->nodeid
;
243 array
[memb
->slot
- 1].slot
= memb
->slot
;
247 /* assign new slots from unused offsets */
249 list_for_each_entry(memb
, &ls
->ls_nodes
, list
) {
253 for (i
= 0; i
< array_size
; i
++) {
258 memb
->slot_prev
= memb
->slot
;
259 array
[i
].nodeid
= memb
->nodeid
;
260 array
[i
].slot
= memb
->slot
;
263 if (!ls
->ls_slot
&& memb
->nodeid
== our_nodeid
)
264 ls
->ls_slot
= memb
->slot
;
269 log_error(ls
, "no free slot found");
277 log_debug_slots(ls
, gen
, num
, NULL
, array
, array_size
);
279 max_slots
= (dlm_config
.ci_buffer_size
- sizeof(struct dlm_rcom
) -
280 sizeof(struct rcom_config
)) / sizeof(struct rcom_slot
);
282 if (num
> max_slots
) {
283 log_error(ls
, "num_slots %d exceeds max_slots %d",
291 *slots_size
= array_size
;
296 static void add_ordered_member(struct dlm_ls
*ls
, struct dlm_member
*new)
298 struct dlm_member
*memb
= NULL
;
299 struct list_head
*tmp
;
300 struct list_head
*newlist
= &new->list
;
301 struct list_head
*head
= &ls
->ls_nodes
;
303 list_for_each(tmp
, head
) {
304 memb
= list_entry(tmp
, struct dlm_member
, list
);
305 if (new->nodeid
< memb
->nodeid
)
310 list_add_tail(newlist
, head
);
312 /* FIXME: can use list macro here */
313 newlist
->prev
= tmp
->prev
;
315 tmp
->prev
->next
= newlist
;
320 static int dlm_add_member(struct dlm_ls
*ls
, struct dlm_config_node
*node
)
322 struct dlm_member
*memb
;
325 memb
= kzalloc(sizeof(struct dlm_member
), GFP_NOFS
);
329 error
= dlm_lowcomms_connect_node(node
->nodeid
);
335 memb
->nodeid
= node
->nodeid
;
336 memb
->weight
= node
->weight
;
337 memb
->comm_seq
= node
->comm_seq
;
338 add_ordered_member(ls
, memb
);
343 static struct dlm_member
*find_memb(struct list_head
*head
, int nodeid
)
345 struct dlm_member
*memb
;
347 list_for_each_entry(memb
, head
, list
) {
348 if (memb
->nodeid
== nodeid
)
354 int dlm_is_member(struct dlm_ls
*ls
, int nodeid
)
356 if (find_memb(&ls
->ls_nodes
, nodeid
))
361 int dlm_is_removed(struct dlm_ls
*ls
, int nodeid
)
363 if (find_memb(&ls
->ls_nodes_gone
, nodeid
))
368 static void clear_memb_list(struct list_head
*head
)
370 struct dlm_member
*memb
;
372 while (!list_empty(head
)) {
373 memb
= list_entry(head
->next
, struct dlm_member
, list
);
374 list_del(&memb
->list
);
379 void dlm_clear_members(struct dlm_ls
*ls
)
381 clear_memb_list(&ls
->ls_nodes
);
382 ls
->ls_num_nodes
= 0;
385 void dlm_clear_members_gone(struct dlm_ls
*ls
)
387 clear_memb_list(&ls
->ls_nodes_gone
);
390 static void make_member_array(struct dlm_ls
*ls
)
392 struct dlm_member
*memb
;
393 int i
, w
, x
= 0, total
= 0, all_zero
= 0, *array
;
395 kfree(ls
->ls_node_array
);
396 ls
->ls_node_array
= NULL
;
398 list_for_each_entry(memb
, &ls
->ls_nodes
, list
) {
400 total
+= memb
->weight
;
403 /* all nodes revert to weight of 1 if all have weight 0 */
406 total
= ls
->ls_num_nodes
;
410 ls
->ls_total_weight
= total
;
412 array
= kmalloc(sizeof(int) * total
, GFP_NOFS
);
416 list_for_each_entry(memb
, &ls
->ls_nodes
, list
) {
417 if (!all_zero
&& !memb
->weight
)
425 DLM_ASSERT(x
< total
, printk("total %d x %d\n", total
, x
););
427 for (i
= 0; i
< w
; i
++)
428 array
[x
++] = memb
->nodeid
;
431 ls
->ls_node_array
= array
;
434 /* send a status request to all members just to establish comms connections */
436 static int ping_members(struct dlm_ls
*ls
)
438 struct dlm_member
*memb
;
441 list_for_each_entry(memb
, &ls
->ls_nodes
, list
) {
442 error
= dlm_recovery_stopped(ls
);
445 error
= dlm_rcom_status(ls
, memb
->nodeid
, 0);
450 log_debug(ls
, "ping_members aborted %d last nodeid %d",
451 error
, ls
->ls_recover_nodeid
);
455 static void dlm_lsop_recover_prep(struct dlm_ls
*ls
)
457 if (!ls
->ls_ops
|| !ls
->ls_ops
->recover_prep
)
459 ls
->ls_ops
->recover_prep(ls
->ls_ops_arg
);
462 static void dlm_lsop_recover_slot(struct dlm_ls
*ls
, struct dlm_member
*memb
)
464 struct dlm_slot slot
;
468 if (!ls
->ls_ops
|| !ls
->ls_ops
->recover_slot
)
471 /* if there is no comms connection with this node
472 or the present comms connection is newer
473 than the one when this member was added, then
474 we consider the node to have failed (versus
475 being removed due to dlm_release_lockspace) */
477 error
= dlm_comm_seq(memb
->nodeid
, &seq
);
479 if (!error
&& seq
== memb
->comm_seq
)
482 slot
.nodeid
= memb
->nodeid
;
483 slot
.slot
= memb
->slot
;
485 ls
->ls_ops
->recover_slot(ls
->ls_ops_arg
, &slot
);
488 void dlm_lsop_recover_done(struct dlm_ls
*ls
)
490 struct dlm_member
*memb
;
491 struct dlm_slot
*slots
;
494 if (!ls
->ls_ops
|| !ls
->ls_ops
->recover_done
)
497 num
= ls
->ls_num_nodes
;
499 slots
= kzalloc(num
* sizeof(struct dlm_slot
), GFP_KERNEL
);
504 list_for_each_entry(memb
, &ls
->ls_nodes
, list
) {
506 log_error(ls
, "dlm_lsop_recover_done bad num %d", num
);
509 slots
[i
].nodeid
= memb
->nodeid
;
510 slots
[i
].slot
= memb
->slot
;
514 ls
->ls_ops
->recover_done(ls
->ls_ops_arg
, slots
, num
,
515 ls
->ls_slot
, ls
->ls_generation
);
520 static struct dlm_config_node
*find_config_node(struct dlm_recover
*rv
,
525 for (i
= 0; i
< rv
->nodes_count
; i
++) {
526 if (rv
->nodes
[i
].nodeid
== nodeid
)
527 return &rv
->nodes
[i
];
532 int dlm_recover_members(struct dlm_ls
*ls
, struct dlm_recover
*rv
, int *neg_out
)
534 struct dlm_member
*memb
, *safe
;
535 struct dlm_config_node
*node
;
536 int i
, error
, neg
= 0, low
= -1;
538 /* previously removed members that we've not finished removing need to
539 count as a negative change so the "neg" recovery steps will happen */
541 list_for_each_entry(memb
, &ls
->ls_nodes_gone
, list
) {
542 log_debug(ls
, "prev removed member %d", memb
->nodeid
);
546 /* move departed members from ls_nodes to ls_nodes_gone */
548 list_for_each_entry_safe(memb
, safe
, &ls
->ls_nodes
, list
) {
549 node
= find_config_node(rv
, memb
->nodeid
);
550 if (node
&& !node
->new)
554 log_debug(ls
, "remove member %d", memb
->nodeid
);
556 /* removed and re-added */
557 log_debug(ls
, "remove member %d comm_seq %u %u",
558 memb
->nodeid
, memb
->comm_seq
, node
->comm_seq
);
562 list_move(&memb
->list
, &ls
->ls_nodes_gone
);
564 dlm_lsop_recover_slot(ls
, memb
);
567 /* add new members to ls_nodes */
569 for (i
= 0; i
< rv
->nodes_count
; i
++) {
570 node
= &rv
->nodes
[i
];
571 if (dlm_is_member(ls
, node
->nodeid
))
573 dlm_add_member(ls
, node
);
574 log_debug(ls
, "add member %d", node
->nodeid
);
577 list_for_each_entry(memb
, &ls
->ls_nodes
, list
) {
578 if (low
== -1 || memb
->nodeid
< low
)
581 ls
->ls_low_nodeid
= low
;
583 make_member_array(ls
);
586 error
= ping_members(ls
);
587 if (!error
|| error
== -EPROTO
) {
588 /* new_lockspace() may be waiting to know if the config
590 ls
->ls_members_result
= error
;
591 complete(&ls
->ls_members_done
);
594 log_debug(ls
, "dlm_recover_members %d nodes", ls
->ls_num_nodes
);
598 /* Userspace guarantees that dlm_ls_stop() has completed on all nodes before
599 dlm_ls_start() is called on any of them to start the new recovery. */
601 int dlm_ls_stop(struct dlm_ls
*ls
)
606 * Prevent dlm_recv from being in the middle of something when we do
607 * the stop. This includes ensuring dlm_recv isn't processing a
608 * recovery message (rcom), while dlm_recoverd is aborting and
609 * resetting things from an in-progress recovery. i.e. we want
610 * dlm_recoverd to abort its recovery without worrying about dlm_recv
611 * processing an rcom at the same time. Stopping dlm_recv also makes
612 * it easy for dlm_receive_message() to check locking stopped and add a
613 * message to the requestqueue without races.
616 down_write(&ls
->ls_recv_active
);
619 * Abort any recovery that's in progress (see RECOVERY_STOP,
620 * dlm_recovery_stopped()) and tell any other threads running in the
621 * dlm to quit any processing (see RUNNING, dlm_locking_stopped()).
624 spin_lock(&ls
->ls_recover_lock
);
625 set_bit(LSFL_RECOVERY_STOP
, &ls
->ls_flags
);
626 new = test_and_clear_bit(LSFL_RUNNING
, &ls
->ls_flags
);
627 ls
->ls_recover_seq
++;
628 spin_unlock(&ls
->ls_recover_lock
);
631 * Let dlm_recv run again, now any normal messages will be saved on the
632 * requestqueue for later.
635 up_write(&ls
->ls_recv_active
);
638 * This in_recovery lock does two things:
639 * 1) Keeps this function from returning until all threads are out
640 * of locking routines and locking is truly stopped.
641 * 2) Keeps any new requests from being processed until it's unlocked
642 * when recovery is complete.
646 down_write(&ls
->ls_in_recovery
);
649 * The recoverd suspend/resume makes sure that dlm_recoverd (if
650 * running) has noticed RECOVERY_STOP above and quit processing the
654 dlm_recoverd_suspend(ls
);
656 spin_lock(&ls
->ls_recover_lock
);
659 ls
->ls_num_slots
= 0;
660 ls
->ls_slots_size
= 0;
661 ls
->ls_recover_status
= 0;
662 spin_unlock(&ls
->ls_recover_lock
);
664 dlm_recoverd_resume(ls
);
666 if (!ls
->ls_recover_begin
)
667 ls
->ls_recover_begin
= jiffies
;
669 dlm_lsop_recover_prep(ls
);
673 int dlm_ls_start(struct dlm_ls
*ls
)
675 struct dlm_recover
*rv
= NULL
, *rv_old
;
676 struct dlm_config_node
*nodes
;
679 rv
= kzalloc(sizeof(struct dlm_recover
), GFP_NOFS
);
683 error
= dlm_config_nodes(ls
->ls_name
, &nodes
, &count
);
687 spin_lock(&ls
->ls_recover_lock
);
689 /* the lockspace needs to be stopped before it can be started */
691 if (!dlm_locking_stopped(ls
)) {
692 spin_unlock(&ls
->ls_recover_lock
);
693 log_error(ls
, "start ignored: lockspace running");
699 rv
->nodes_count
= count
;
700 rv
->seq
= ++ls
->ls_recover_seq
;
701 rv_old
= ls
->ls_recover_args
;
702 ls
->ls_recover_args
= rv
;
703 spin_unlock(&ls
->ls_recover_lock
);
706 log_error(ls
, "unused recovery %llx %d",
707 (unsigned long long)rv_old
->seq
, rv_old
->nodes_count
);
708 kfree(rv_old
->nodes
);
712 dlm_recoverd_kick(ls
);