1 /******************************************************************************
2 *******************************************************************************
4 ** Copyright (C) 2005 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"
23 * Following called by dlm_recoverd thread
26 static void add_ordered_member(struct dlm_ls *ls, struct dlm_member *new)
28 struct dlm_member *memb = NULL;
29 struct list_head *tmp;
30 struct list_head *newlist = &new->list;
31 struct list_head *head = &ls->ls_nodes;
33 list_for_each(tmp, head) {
34 memb = list_entry(tmp, struct dlm_member, list);
35 if (new->nodeid < memb->nodeid)
40 list_add_tail(newlist, head);
42 /* FIXME: can use list macro here */
43 newlist->prev = tmp->prev;
45 tmp->prev->next = newlist;
50 static int dlm_add_member(struct dlm_ls *ls, int nodeid)
52 struct dlm_member *memb;
55 memb = kzalloc(sizeof(struct dlm_member), GFP_KERNEL);
59 w = dlm_node_weight(ls->ls_name, nodeid);
63 memb->nodeid = nodeid;
65 add_ordered_member(ls, memb);
70 static void dlm_remove_member(struct dlm_ls *ls, struct dlm_member *memb)
72 list_move(&memb->list, &ls->ls_nodes_gone);
76 static int dlm_is_member(struct dlm_ls *ls, int nodeid)
78 struct dlm_member *memb;
80 list_for_each_entry(memb, &ls->ls_nodes, list) {
81 if (memb->nodeid == nodeid)
87 int dlm_is_removed(struct dlm_ls *ls, int nodeid)
89 struct dlm_member *memb;
91 list_for_each_entry(memb, &ls->ls_nodes_gone, list) {
92 if (memb->nodeid == nodeid)
98 static void clear_memb_list(struct list_head *head)
100 struct dlm_member *memb;
102 while (!list_empty(head)) {
103 memb = list_entry(head->next, struct dlm_member, list);
104 list_del(&memb->list);
109 void dlm_clear_members(struct dlm_ls *ls)
111 clear_memb_list(&ls->ls_nodes);
112 ls->ls_num_nodes = 0;
115 void dlm_clear_members_gone(struct dlm_ls *ls)
117 clear_memb_list(&ls->ls_nodes_gone);
120 static void make_member_array(struct dlm_ls *ls)
122 struct dlm_member *memb;
123 int i, w, x = 0, total = 0, all_zero = 0, *array;
125 kfree(ls->ls_node_array);
126 ls->ls_node_array = NULL;
128 list_for_each_entry(memb, &ls->ls_nodes, list) {
130 total += memb->weight;
133 /* all nodes revert to weight of 1 if all have weight 0 */
136 total = ls->ls_num_nodes;
140 ls->ls_total_weight = total;
142 array = kmalloc(sizeof(int) * total, GFP_KERNEL);
146 list_for_each_entry(memb, &ls->ls_nodes, list) {
147 if (!all_zero && !memb->weight)
155 DLM_ASSERT(x < total, printk("total %d x %d\n", total, x););
157 for (i = 0; i < w; i++)
158 array[x++] = memb->nodeid;
161 ls->ls_node_array = array;
164 /* send a status request to all members just to establish comms connections */
166 static void ping_members(struct dlm_ls *ls)
168 struct dlm_member *memb;
169 list_for_each_entry(memb, &ls->ls_nodes, list)
170 dlm_rcom_status(ls, memb->nodeid);
173 int dlm_recover_members(struct dlm_ls *ls, struct dlm_recover *rv, int *neg_out)
175 struct dlm_member *memb, *safe;
176 int i, error, found, pos = 0, neg = 0, low = -1;
178 /* move departed members from ls_nodes to ls_nodes_gone */
180 list_for_each_entry_safe(memb, safe, &ls->ls_nodes, list) {
182 for (i = 0; i < rv->node_count; i++) {
183 if (memb->nodeid == rv->nodeids[i]) {
191 dlm_remove_member(ls, memb);
192 log_debug(ls, "remove member %d", memb->nodeid);
196 /* add new members to ls_nodes */
198 for (i = 0; i < rv->node_count; i++) {
199 if (dlm_is_member(ls, rv->nodeids[i]))
201 dlm_add_member(ls, rv->nodeids[i]);
203 log_debug(ls, "add member %d", rv->nodeids[i]);
206 list_for_each_entry(memb, &ls->ls_nodes, list) {
207 if (low == -1 || memb->nodeid < low)
210 ls->ls_low_nodeid = low;
212 make_member_array(ls);
213 dlm_set_recover_status(ls, DLM_RS_NODES);
218 error = dlm_recover_members_wait(ls);
219 log_debug(ls, "total members %d", ls->ls_num_nodes);
224 * Following called from lockspace.c
227 int dlm_ls_stop(struct dlm_ls *ls)
232 * A stop cancels any recovery that's in progress (see RECOVERY_STOP,
233 * dlm_recovery_stopped()) and prevents any new locks from being
234 * processed (see RUNNING, dlm_locking_stopped()).
237 spin_lock(&ls->ls_recover_lock);
238 set_bit(LSFL_RECOVERY_STOP, &ls->ls_flags);
239 new = test_and_clear_bit(LSFL_RUNNING, &ls->ls_flags);
240 ls->ls_recover_seq++;
241 spin_unlock(&ls->ls_recover_lock);
244 * This in_recovery lock does two things:
246 * 1) Keeps this function from returning until all threads are out
247 * of locking routines and locking is truely stopped.
248 * 2) Keeps any new requests from being processed until it's unlocked
249 * when recovery is complete.
253 down_write(&ls->ls_in_recovery);
256 * The recoverd suspend/resume makes sure that dlm_recoverd (if
257 * running) has noticed the clearing of RUNNING above and quit
258 * processing the previous recovery. This will be true for all nodes
259 * before any nodes start the new recovery.
262 dlm_recoverd_suspend(ls);
263 ls->ls_recover_status = 0;
264 dlm_recoverd_resume(ls);
268 int dlm_ls_start(struct dlm_ls *ls)
270 struct dlm_recover *rv = NULL, *rv_old;
274 rv = kzalloc(sizeof(struct dlm_recover), GFP_KERNEL);
278 error = count = dlm_nodeid_list(ls->ls_name, &ids);
282 spin_lock(&ls->ls_recover_lock);
284 /* the lockspace needs to be stopped before it can be started */
286 if (!dlm_locking_stopped(ls)) {
287 spin_unlock(&ls->ls_recover_lock);
288 log_error(ls, "start ignored: lockspace running");
294 rv->node_count = count;
295 rv->seq = ++ls->ls_recover_seq;
296 rv_old = ls->ls_recover_args;
297 ls->ls_recover_args = rv;
298 spin_unlock(&ls->ls_recover_lock);
301 kfree(rv_old->nodeids);
305 dlm_recoverd_kick(ls);