1 /******************************************************************************
2 *******************************************************************************
4 ** Copyright (C) 2005-2007 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 * Following called by dlm_recoverd thread
25 static void add_ordered_member(struct dlm_ls *ls, struct dlm_member *new)
27 struct dlm_member *memb = NULL;
28 struct list_head *tmp;
29 struct list_head *newlist = &new->list;
30 struct list_head *head = &ls->ls_nodes;
32 list_for_each(tmp, head) {
33 memb = list_entry(tmp, struct dlm_member, list);
34 if (new->nodeid < memb->nodeid)
39 list_add_tail(newlist, head);
41 /* FIXME: can use list macro here */
42 newlist->prev = tmp->prev;
44 tmp->prev->next = newlist;
49 static int dlm_add_member(struct dlm_ls *ls, int nodeid)
51 struct dlm_member *memb;
54 memb = kzalloc(sizeof(struct dlm_member), GFP_KERNEL);
58 w = dlm_node_weight(ls->ls_name, nodeid);
64 memb->nodeid = nodeid;
66 add_ordered_member(ls, memb);
71 static void dlm_remove_member(struct dlm_ls *ls, struct dlm_member *memb)
73 list_move(&memb->list, &ls->ls_nodes_gone);
77 static int dlm_is_member(struct dlm_ls *ls, int nodeid)
79 struct dlm_member *memb;
81 list_for_each_entry(memb, &ls->ls_nodes, list) {
82 if (memb->nodeid == nodeid)
88 int dlm_is_removed(struct dlm_ls *ls, int nodeid)
90 struct dlm_member *memb;
92 list_for_each_entry(memb, &ls->ls_nodes_gone, list) {
93 if (memb->nodeid == nodeid)
99 static void clear_memb_list(struct list_head *head)
101 struct dlm_member *memb;
103 while (!list_empty(head)) {
104 memb = list_entry(head->next, struct dlm_member, list);
105 list_del(&memb->list);
110 void dlm_clear_members(struct dlm_ls *ls)
112 clear_memb_list(&ls->ls_nodes);
113 ls->ls_num_nodes = 0;
116 void dlm_clear_members_gone(struct dlm_ls *ls)
118 clear_memb_list(&ls->ls_nodes_gone);
121 static void make_member_array(struct dlm_ls *ls)
123 struct dlm_member *memb;
124 int i, w, x = 0, total = 0, all_zero = 0, *array;
126 kfree(ls->ls_node_array);
127 ls->ls_node_array = NULL;
129 list_for_each_entry(memb, &ls->ls_nodes, list) {
131 total += memb->weight;
134 /* all nodes revert to weight of 1 if all have weight 0 */
137 total = ls->ls_num_nodes;
141 ls->ls_total_weight = total;
143 array = kmalloc(sizeof(int) * total, GFP_KERNEL);
147 list_for_each_entry(memb, &ls->ls_nodes, list) {
148 if (!all_zero && !memb->weight)
156 DLM_ASSERT(x < total, printk("total %d x %d\n", total, x););
158 for (i = 0; i < w; i++)
159 array[x++] = memb->nodeid;
162 ls->ls_node_array = array;
165 /* send a status request to all members just to establish comms connections */
167 static int ping_members(struct dlm_ls *ls)
169 struct dlm_member *memb;
172 list_for_each_entry(memb, &ls->ls_nodes, list) {
173 error = dlm_recovery_stopped(ls);
176 error = dlm_rcom_status(ls, memb->nodeid);
181 log_debug(ls, "ping_members aborted %d last nodeid %d",
182 error, ls->ls_recover_nodeid);
186 int dlm_recover_members(struct dlm_ls *ls, struct dlm_recover *rv, int *neg_out)
188 struct dlm_member *memb, *safe;
189 int i, error, found, pos = 0, neg = 0, low = -1;
191 /* previously removed members that we've not finished removing need to
192 count as a negative change so the "neg" recovery steps will happen */
194 list_for_each_entry(memb, &ls->ls_nodes_gone, list) {
195 log_debug(ls, "prev removed member %d", memb->nodeid);
199 /* move departed members from ls_nodes to ls_nodes_gone */
201 list_for_each_entry_safe(memb, safe, &ls->ls_nodes, list) {
203 for (i = 0; i < rv->node_count; i++) {
204 if (memb->nodeid == rv->nodeids[i]) {
212 dlm_remove_member(ls, memb);
213 log_debug(ls, "remove member %d", memb->nodeid);
217 /* add new members to ls_nodes */
219 for (i = 0; i < rv->node_count; i++) {
220 if (dlm_is_member(ls, rv->nodeids[i]))
222 dlm_add_member(ls, rv->nodeids[i]);
224 log_debug(ls, "add member %d", rv->nodeids[i]);
227 list_for_each_entry(memb, &ls->ls_nodes, list) {
228 if (low == -1 || memb->nodeid < low)
231 ls->ls_low_nodeid = low;
233 make_member_array(ls);
234 dlm_set_recover_status(ls, DLM_RS_NODES);
237 error = ping_members(ls);
238 if (!error || error == -EPROTO) {
239 /* new_lockspace() may be waiting to know if the config
241 ls->ls_members_result = error;
242 complete(&ls->ls_members_done);
247 error = dlm_recover_members_wait(ls);
249 log_debug(ls, "total members %d error %d", ls->ls_num_nodes, error);
254 * Following called from lockspace.c
257 int dlm_ls_stop(struct dlm_ls *ls)
262 * A stop cancels any recovery that's in progress (see RECOVERY_STOP,
263 * dlm_recovery_stopped()) and prevents any new locks from being
264 * processed (see RUNNING, dlm_locking_stopped()).
267 spin_lock(&ls->ls_recover_lock);
268 set_bit(LSFL_RECOVERY_STOP, &ls->ls_flags);
269 new = test_and_clear_bit(LSFL_RUNNING, &ls->ls_flags);
270 ls->ls_recover_seq++;
271 spin_unlock(&ls->ls_recover_lock);
274 * This in_recovery lock does two things:
276 * 1) Keeps this function from returning until all threads are out
277 * of locking routines and locking is truely stopped.
278 * 2) Keeps any new requests from being processed until it's unlocked
279 * when recovery is complete.
283 down_write(&ls->ls_in_recovery);
286 * The recoverd suspend/resume makes sure that dlm_recoverd (if
287 * running) has noticed the clearing of RUNNING above and quit
288 * processing the previous recovery. This will be true for all nodes
289 * before any nodes start the new recovery.
292 dlm_recoverd_suspend(ls);
293 ls->ls_recover_status = 0;
294 dlm_recoverd_resume(ls);
296 if (!ls->ls_recover_begin)
297 ls->ls_recover_begin = jiffies;
301 int dlm_ls_start(struct dlm_ls *ls)
303 struct dlm_recover *rv = NULL, *rv_old;
307 rv = kzalloc(sizeof(struct dlm_recover), GFP_KERNEL);
311 error = count = dlm_nodeid_list(ls->ls_name, &ids);
315 spin_lock(&ls->ls_recover_lock);
317 /* the lockspace needs to be stopped before it can be started */
319 if (!dlm_locking_stopped(ls)) {
320 spin_unlock(&ls->ls_recover_lock);
321 log_error(ls, "start ignored: lockspace running");
327 rv->node_count = count;
328 rv->seq = ++ls->ls_recover_seq;
329 rv_old = ls->ls_recover_args;
330 ls->ls_recover_args = rv;
331 spin_unlock(&ls->ls_recover_lock);
334 kfree(rv_old->nodeids);
338 dlm_recoverd_kick(ls);