xref: /linux/fs/dlm/recover.c (revision 3a39d672e7f48b8d6b91a09afa4b55352773b4b5)
12522fe45SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2e7fd4179SDavid Teigland /******************************************************************************
3e7fd4179SDavid Teigland *******************************************************************************
4e7fd4179SDavid Teigland **
5e7fd4179SDavid Teigland **  Copyright (C) Sistina Software, Inc.  1997-2003  All rights reserved.
6e7fd4179SDavid Teigland **  Copyright (C) 2004-2005 Red Hat, Inc.  All rights reserved.
7e7fd4179SDavid Teigland **
8e7fd4179SDavid Teigland **
9e7fd4179SDavid Teigland *******************************************************************************
10e7fd4179SDavid Teigland ******************************************************************************/
11e7fd4179SDavid Teigland 
12e7fd4179SDavid Teigland #include "dlm_internal.h"
13e7fd4179SDavid Teigland #include "lockspace.h"
14e7fd4179SDavid Teigland #include "dir.h"
15e7fd4179SDavid Teigland #include "config.h"
16e7fd4179SDavid Teigland #include "ast.h"
17e7fd4179SDavid Teigland #include "memory.h"
18e7fd4179SDavid Teigland #include "rcom.h"
19e7fd4179SDavid Teigland #include "lock.h"
20e7fd4179SDavid Teigland #include "lowcomms.h"
21e7fd4179SDavid Teigland #include "member.h"
22e7fd4179SDavid Teigland #include "recover.h"
23e7fd4179SDavid Teigland 
24e7fd4179SDavid Teigland 
25e7fd4179SDavid Teigland /*
26e7fd4179SDavid Teigland  * Recovery waiting routines: these functions wait for a particular reply from
27e7fd4179SDavid Teigland  * a remote node, or for the remote node to report a certain status.  They need
28e7fd4179SDavid Teigland  * to abort if the lockspace is stopped indicating a node has failed (perhaps
29e7fd4179SDavid Teigland  * the one being waited for).
30e7fd4179SDavid Teigland  */
31e7fd4179SDavid Teigland 
32e7fd4179SDavid Teigland /*
33e7fd4179SDavid Teigland  * Wait until given function returns non-zero or lockspace is stopped
34e7fd4179SDavid Teigland  * (LS_RECOVERY_STOP set due to failure of a node in ls_nodes).  When another
35e7fd4179SDavid Teigland  * function thinks it could have completed the waited-on task, they should wake
36e7fd4179SDavid Teigland  * up ls_wait_general to get an immediate response rather than waiting for the
376d768177SDavid Teigland  * timeout.  This uses a timeout so it can check periodically if the wait
386d768177SDavid Teigland  * should abort due to node failure (which doesn't cause a wake_up).
396d768177SDavid Teigland  * This should only be called by the dlm_recoverd thread.
40e7fd4179SDavid Teigland  */
41e7fd4179SDavid Teigland 
dlm_wait_function(struct dlm_ls * ls,int (* testfn)(struct dlm_ls * ls))42e7fd4179SDavid Teigland int dlm_wait_function(struct dlm_ls *ls, int (*testfn) (struct dlm_ls *ls))
43e7fd4179SDavid Teigland {
44e7fd4179SDavid Teigland 	int error = 0;
456d768177SDavid Teigland 	int rv;
46e7fd4179SDavid Teigland 
476d768177SDavid Teigland 	while (1) {
486d768177SDavid Teigland 		rv = wait_event_timeout(ls->ls_wait_general,
496d768177SDavid Teigland 					testfn(ls) || dlm_recovery_stopped(ls),
506d768177SDavid Teigland 					dlm_config.ci_recover_timer * HZ);
516d768177SDavid Teigland 		if (rv)
526d768177SDavid Teigland 			break;
5359661212Stsutomu.owa@toshiba.co.jp 		if (test_bit(LSFL_RCOM_WAIT, &ls->ls_flags)) {
5459661212Stsutomu.owa@toshiba.co.jp 			log_debug(ls, "dlm_wait_function timed out");
5559661212Stsutomu.owa@toshiba.co.jp 			return -ETIMEDOUT;
5659661212Stsutomu.owa@toshiba.co.jp 		}
576d768177SDavid Teigland 	}
58e7fd4179SDavid Teigland 
59e7fd4179SDavid Teigland 	if (dlm_recovery_stopped(ls)) {
60e7fd4179SDavid Teigland 		log_debug(ls, "dlm_wait_function aborted");
61e7fd4179SDavid Teigland 		error = -EINTR;
62e7fd4179SDavid Teigland 	}
63e7fd4179SDavid Teigland 	return error;
64e7fd4179SDavid Teigland }
65e7fd4179SDavid Teigland 
66e7fd4179SDavid Teigland /*
67e7fd4179SDavid Teigland  * An efficient way for all nodes to wait for all others to have a certain
68e7fd4179SDavid Teigland  * status.  The node with the lowest nodeid polls all the others for their
69e7fd4179SDavid Teigland  * status (wait_status_all) and all the others poll the node with the low id
70e7fd4179SDavid Teigland  * for its accumulated result (wait_status_low).  When all nodes have set
71e7fd4179SDavid Teigland  * status flag X, then status flag X_ALL will be set on the low nodeid.
72e7fd4179SDavid Teigland  */
73e7fd4179SDavid Teigland 
dlm_recover_status(struct dlm_ls * ls)74e7fd4179SDavid Teigland uint32_t dlm_recover_status(struct dlm_ls *ls)
75e7fd4179SDavid Teigland {
76e7fd4179SDavid Teigland 	uint32_t status;
77578acf9aSAlexander Aring 	spin_lock_bh(&ls->ls_recover_lock);
78e7fd4179SDavid Teigland 	status = ls->ls_recover_status;
79578acf9aSAlexander Aring 	spin_unlock_bh(&ls->ls_recover_lock);
80e7fd4179SDavid Teigland 	return status;
81e7fd4179SDavid Teigland }
82e7fd4179SDavid Teigland 
_set_recover_status(struct dlm_ls * ls,uint32_t status)83757a4271SDavid Teigland static void _set_recover_status(struct dlm_ls *ls, uint32_t status)
84757a4271SDavid Teigland {
85757a4271SDavid Teigland 	ls->ls_recover_status |= status;
86757a4271SDavid Teigland }
87757a4271SDavid Teigland 
dlm_set_recover_status(struct dlm_ls * ls,uint32_t status)88e7fd4179SDavid Teigland void dlm_set_recover_status(struct dlm_ls *ls, uint32_t status)
89e7fd4179SDavid Teigland {
90578acf9aSAlexander Aring 	spin_lock_bh(&ls->ls_recover_lock);
91757a4271SDavid Teigland 	_set_recover_status(ls, status);
92578acf9aSAlexander Aring 	spin_unlock_bh(&ls->ls_recover_lock);
93e7fd4179SDavid Teigland }
94e7fd4179SDavid Teigland 
wait_status_all(struct dlm_ls * ls,uint32_t wait_status,int save_slots,uint64_t seq)95757a4271SDavid Teigland static int wait_status_all(struct dlm_ls *ls, uint32_t wait_status,
96c4f4e135SAlexander Aring 			   int save_slots, uint64_t seq)
97e7fd4179SDavid Teigland {
984007685cSAl Viro 	struct dlm_rcom *rc = ls->ls_recover_buf;
99e7fd4179SDavid Teigland 	struct dlm_member *memb;
100e7fd4179SDavid Teigland 	int error = 0, delay;
101e7fd4179SDavid Teigland 
102e7fd4179SDavid Teigland 	list_for_each_entry(memb, &ls->ls_nodes, list) {
103e7fd4179SDavid Teigland 		delay = 0;
104e7fd4179SDavid Teigland 		for (;;) {
105e7fd4179SDavid Teigland 			if (dlm_recovery_stopped(ls)) {
106e7fd4179SDavid Teigland 				error = -EINTR;
107e7fd4179SDavid Teigland 				goto out;
108e7fd4179SDavid Teigland 			}
109e7fd4179SDavid Teigland 
110c4f4e135SAlexander Aring 			error = dlm_rcom_status(ls, memb->nodeid, 0, seq);
111e7fd4179SDavid Teigland 			if (error)
112e7fd4179SDavid Teigland 				goto out;
113e7fd4179SDavid Teigland 
114757a4271SDavid Teigland 			if (save_slots)
115757a4271SDavid Teigland 				dlm_slot_save(ls, rc, memb);
116757a4271SDavid Teigland 
1172f9dbedaSAlexander Aring 			if (le32_to_cpu(rc->rc_result) & wait_status)
118e7fd4179SDavid Teigland 				break;
119e7fd4179SDavid Teigland 			if (delay < 1000)
120e7fd4179SDavid Teigland 				delay += 20;
121e7fd4179SDavid Teigland 			msleep(delay);
122e7fd4179SDavid Teigland 		}
123e7fd4179SDavid Teigland 	}
124e7fd4179SDavid Teigland  out:
125e7fd4179SDavid Teigland 	return error;
126e7fd4179SDavid Teigland }
127e7fd4179SDavid Teigland 
wait_status_low(struct dlm_ls * ls,uint32_t wait_status,uint32_t status_flags,uint64_t seq)128757a4271SDavid Teigland static int wait_status_low(struct dlm_ls *ls, uint32_t wait_status,
129c4f4e135SAlexander Aring 			   uint32_t status_flags, uint64_t seq)
130e7fd4179SDavid Teigland {
1314007685cSAl Viro 	struct dlm_rcom *rc = ls->ls_recover_buf;
132e7fd4179SDavid Teigland 	int error = 0, delay = 0, nodeid = ls->ls_low_nodeid;
133e7fd4179SDavid Teigland 
134e7fd4179SDavid Teigland 	for (;;) {
135e7fd4179SDavid Teigland 		if (dlm_recovery_stopped(ls)) {
136e7fd4179SDavid Teigland 			error = -EINTR;
137e7fd4179SDavid Teigland 			goto out;
138e7fd4179SDavid Teigland 		}
139e7fd4179SDavid Teigland 
140c4f4e135SAlexander Aring 		error = dlm_rcom_status(ls, nodeid, status_flags, seq);
141e7fd4179SDavid Teigland 		if (error)
142e7fd4179SDavid Teigland 			break;
143e7fd4179SDavid Teigland 
1442f9dbedaSAlexander Aring 		if (le32_to_cpu(rc->rc_result) & wait_status)
145e7fd4179SDavid Teigland 			break;
146e7fd4179SDavid Teigland 		if (delay < 1000)
147e7fd4179SDavid Teigland 			delay += 20;
148e7fd4179SDavid Teigland 		msleep(delay);
149e7fd4179SDavid Teigland 	}
150e7fd4179SDavid Teigland  out:
151e7fd4179SDavid Teigland 	return error;
152e7fd4179SDavid Teigland }
153e7fd4179SDavid Teigland 
wait_status(struct dlm_ls * ls,uint32_t status,uint64_t seq)154c4f4e135SAlexander Aring static int wait_status(struct dlm_ls *ls, uint32_t status, uint64_t seq)
155e7fd4179SDavid Teigland {
156e7fd4179SDavid Teigland 	uint32_t status_all = status << 1;
157e7fd4179SDavid Teigland 	int error;
158e7fd4179SDavid Teigland 
159e7fd4179SDavid Teigland 	if (ls->ls_low_nodeid == dlm_our_nodeid()) {
160c4f4e135SAlexander Aring 		error = wait_status_all(ls, status, 0, seq);
161e7fd4179SDavid Teigland 		if (!error)
162e7fd4179SDavid Teigland 			dlm_set_recover_status(ls, status_all);
163e7fd4179SDavid Teigland 	} else
164c4f4e135SAlexander Aring 		error = wait_status_low(ls, status_all, 0, seq);
165e7fd4179SDavid Teigland 
166e7fd4179SDavid Teigland 	return error;
167e7fd4179SDavid Teigland }
168e7fd4179SDavid Teigland 
dlm_recover_members_wait(struct dlm_ls * ls,uint64_t seq)169c4f4e135SAlexander Aring int dlm_recover_members_wait(struct dlm_ls *ls, uint64_t seq)
170e7fd4179SDavid Teigland {
171757a4271SDavid Teigland 	struct dlm_member *memb;
172757a4271SDavid Teigland 	struct dlm_slot *slots;
173757a4271SDavid Teigland 	int num_slots, slots_size;
174757a4271SDavid Teigland 	int error, rv;
175757a4271SDavid Teigland 	uint32_t gen;
176757a4271SDavid Teigland 
177757a4271SDavid Teigland 	list_for_each_entry(memb, &ls->ls_nodes, list) {
178757a4271SDavid Teigland 		memb->slot = -1;
179757a4271SDavid Teigland 		memb->generation = 0;
180757a4271SDavid Teigland 	}
181757a4271SDavid Teigland 
182757a4271SDavid Teigland 	if (ls->ls_low_nodeid == dlm_our_nodeid()) {
183c4f4e135SAlexander Aring 		error = wait_status_all(ls, DLM_RS_NODES, 1, seq);
184757a4271SDavid Teigland 		if (error)
185757a4271SDavid Teigland 			goto out;
186757a4271SDavid Teigland 
187757a4271SDavid Teigland 		/* slots array is sparse, slots_size may be > num_slots */
188757a4271SDavid Teigland 
189757a4271SDavid Teigland 		rv = dlm_slots_assign(ls, &num_slots, &slots_size, &slots, &gen);
190757a4271SDavid Teigland 		if (!rv) {
191578acf9aSAlexander Aring 			spin_lock_bh(&ls->ls_recover_lock);
192757a4271SDavid Teigland 			_set_recover_status(ls, DLM_RS_NODES_ALL);
193757a4271SDavid Teigland 			ls->ls_num_slots = num_slots;
194757a4271SDavid Teigland 			ls->ls_slots_size = slots_size;
195757a4271SDavid Teigland 			ls->ls_slots = slots;
196757a4271SDavid Teigland 			ls->ls_generation = gen;
197578acf9aSAlexander Aring 			spin_unlock_bh(&ls->ls_recover_lock);
198757a4271SDavid Teigland 		} else {
199757a4271SDavid Teigland 			dlm_set_recover_status(ls, DLM_RS_NODES_ALL);
200757a4271SDavid Teigland 		}
201757a4271SDavid Teigland 	} else {
202c4f4e135SAlexander Aring 		error = wait_status_low(ls, DLM_RS_NODES_ALL,
203c4f4e135SAlexander Aring 					DLM_RSF_NEED_SLOTS, seq);
204757a4271SDavid Teigland 		if (error)
205757a4271SDavid Teigland 			goto out;
206757a4271SDavid Teigland 
207757a4271SDavid Teigland 		dlm_slots_copy_in(ls);
208757a4271SDavid Teigland 	}
209757a4271SDavid Teigland  out:
210757a4271SDavid Teigland 	return error;
211e7fd4179SDavid Teigland }
212e7fd4179SDavid Teigland 
dlm_recover_directory_wait(struct dlm_ls * ls,uint64_t seq)213c4f4e135SAlexander Aring int dlm_recover_directory_wait(struct dlm_ls *ls, uint64_t seq)
214e7fd4179SDavid Teigland {
215c4f4e135SAlexander Aring 	return wait_status(ls, DLM_RS_DIR, seq);
216e7fd4179SDavid Teigland }
217e7fd4179SDavid Teigland 
dlm_recover_locks_wait(struct dlm_ls * ls,uint64_t seq)218c4f4e135SAlexander Aring int dlm_recover_locks_wait(struct dlm_ls *ls, uint64_t seq)
219e7fd4179SDavid Teigland {
220c4f4e135SAlexander Aring 	return wait_status(ls, DLM_RS_LOCKS, seq);
221e7fd4179SDavid Teigland }
222e7fd4179SDavid Teigland 
dlm_recover_done_wait(struct dlm_ls * ls,uint64_t seq)223c4f4e135SAlexander Aring int dlm_recover_done_wait(struct dlm_ls *ls, uint64_t seq)
224e7fd4179SDavid Teigland {
225c4f4e135SAlexander Aring 	return wait_status(ls, DLM_RS_DONE, seq);
226e7fd4179SDavid Teigland }
227e7fd4179SDavid Teigland 
228e7fd4179SDavid Teigland /*
229e7fd4179SDavid Teigland  * The recover_list contains all the rsb's for which we've requested the new
230e7fd4179SDavid Teigland  * master nodeid.  As replies are returned from the resource directories the
231e7fd4179SDavid Teigland  * rsb's are removed from the list.  When the list is empty we're done.
232e7fd4179SDavid Teigland  *
233e7fd4179SDavid Teigland  * The recover_list is later similarly used for all rsb's for which we've sent
234e7fd4179SDavid Teigland  * new lkb's and need to receive new corresponding lkid's.
235e7fd4179SDavid Teigland  *
236e7fd4179SDavid Teigland  * We use the address of the rsb struct as a simple local identifier for the
237e7fd4179SDavid Teigland  * rsb so we can match an rcom reply with the rsb it was sent for.
238e7fd4179SDavid Teigland  */
239e7fd4179SDavid Teigland 
recover_list_empty(struct dlm_ls * ls)240e7fd4179SDavid Teigland static int recover_list_empty(struct dlm_ls *ls)
241e7fd4179SDavid Teigland {
242e7fd4179SDavid Teigland 	int empty;
243e7fd4179SDavid Teigland 
244578acf9aSAlexander Aring 	spin_lock_bh(&ls->ls_recover_list_lock);
245e7fd4179SDavid Teigland 	empty = list_empty(&ls->ls_recover_list);
246578acf9aSAlexander Aring 	spin_unlock_bh(&ls->ls_recover_list_lock);
247e7fd4179SDavid Teigland 
248e7fd4179SDavid Teigland 	return empty;
249e7fd4179SDavid Teigland }
250e7fd4179SDavid Teigland 
recover_list_add(struct dlm_rsb * r)251e7fd4179SDavid Teigland static void recover_list_add(struct dlm_rsb *r)
252e7fd4179SDavid Teigland {
253e7fd4179SDavid Teigland 	struct dlm_ls *ls = r->res_ls;
254e7fd4179SDavid Teigland 
255578acf9aSAlexander Aring 	spin_lock_bh(&ls->ls_recover_list_lock);
256e7fd4179SDavid Teigland 	if (list_empty(&r->res_recover_list)) {
257e7fd4179SDavid Teigland 		list_add_tail(&r->res_recover_list, &ls->ls_recover_list);
258e7fd4179SDavid Teigland 		ls->ls_recover_list_count++;
259e7fd4179SDavid Teigland 		dlm_hold_rsb(r);
260e7fd4179SDavid Teigland 	}
261578acf9aSAlexander Aring 	spin_unlock_bh(&ls->ls_recover_list_lock);
262e7fd4179SDavid Teigland }
263e7fd4179SDavid Teigland 
recover_list_del(struct dlm_rsb * r)264e7fd4179SDavid Teigland static void recover_list_del(struct dlm_rsb *r)
265e7fd4179SDavid Teigland {
266e7fd4179SDavid Teigland 	struct dlm_ls *ls = r->res_ls;
267e7fd4179SDavid Teigland 
268578acf9aSAlexander Aring 	spin_lock_bh(&ls->ls_recover_list_lock);
269e7fd4179SDavid Teigland 	list_del_init(&r->res_recover_list);
270e7fd4179SDavid Teigland 	ls->ls_recover_list_count--;
271578acf9aSAlexander Aring 	spin_unlock_bh(&ls->ls_recover_list_lock);
272e7fd4179SDavid Teigland 
273e7fd4179SDavid Teigland 	dlm_put_rsb(r);
274e7fd4179SDavid Teigland }
275e7fd4179SDavid Teigland 
recover_list_clear(struct dlm_ls * ls)276e7fd4179SDavid Teigland static void recover_list_clear(struct dlm_ls *ls)
277e7fd4179SDavid Teigland {
278e7fd4179SDavid Teigland 	struct dlm_rsb *r, *s;
279e7fd4179SDavid Teigland 
280578acf9aSAlexander Aring 	spin_lock_bh(&ls->ls_recover_list_lock);
281e7fd4179SDavid Teigland 	list_for_each_entry_safe(r, s, &ls->ls_recover_list, res_recover_list) {
282e7fd4179SDavid Teigland 		list_del_init(&r->res_recover_list);
28352069809SDavid Teigland 		r->res_recover_locks_count = 0;
284e7fd4179SDavid Teigland 		dlm_put_rsb(r);
285e7fd4179SDavid Teigland 		ls->ls_recover_list_count--;
286e7fd4179SDavid Teigland 	}
287e7fd4179SDavid Teigland 
288e7fd4179SDavid Teigland 	if (ls->ls_recover_list_count != 0) {
289e7fd4179SDavid Teigland 		log_error(ls, "warning: recover_list_count %d",
290e7fd4179SDavid Teigland 			  ls->ls_recover_list_count);
291e7fd4179SDavid Teigland 		ls->ls_recover_list_count = 0;
292e7fd4179SDavid Teigland 	}
293578acf9aSAlexander Aring 	spin_unlock_bh(&ls->ls_recover_list_lock);
294e7fd4179SDavid Teigland }
295e7fd4179SDavid Teigland 
recover_xa_empty(struct dlm_ls * ls)296fa0b54f1SAlexander Aring static int recover_xa_empty(struct dlm_ls *ls)
2971d7c484eSDavid Teigland {
2981d7c484eSDavid Teigland 	int empty = 1;
2991d7c484eSDavid Teigland 
300fa0b54f1SAlexander Aring 	spin_lock_bh(&ls->ls_recover_xa_lock);
3011d7c484eSDavid Teigland 	if (ls->ls_recover_list_count)
3021d7c484eSDavid Teigland 		empty = 0;
303fa0b54f1SAlexander Aring 	spin_unlock_bh(&ls->ls_recover_xa_lock);
3041d7c484eSDavid Teigland 
3051d7c484eSDavid Teigland 	return empty;
3061d7c484eSDavid Teigland }
3071d7c484eSDavid Teigland 
recover_xa_add(struct dlm_rsb * r)308fa0b54f1SAlexander Aring static int recover_xa_add(struct dlm_rsb *r)
3091d7c484eSDavid Teigland {
3101d7c484eSDavid Teigland 	struct dlm_ls *ls = r->res_ls;
311fa0b54f1SAlexander Aring 	struct xa_limit limit = {
312fa0b54f1SAlexander Aring 		.min = 1,
313fa0b54f1SAlexander Aring 		.max = UINT_MAX,
314fa0b54f1SAlexander Aring 	};
315fa0b54f1SAlexander Aring 	uint32_t id;
3162a86b3e7STejun Heo 	int rv;
3171d7c484eSDavid Teigland 
318fa0b54f1SAlexander Aring 	spin_lock_bh(&ls->ls_recover_xa_lock);
3191d7c484eSDavid Teigland 	if (r->res_id) {
3202a86b3e7STejun Heo 		rv = -1;
3212a86b3e7STejun Heo 		goto out_unlock;
3221d7c484eSDavid Teigland 	}
323fa0b54f1SAlexander Aring 	rv = xa_alloc(&ls->ls_recover_xa, &id, r, limit, GFP_ATOMIC);
3242a86b3e7STejun Heo 	if (rv < 0)
3252a86b3e7STejun Heo 		goto out_unlock;
3262a86b3e7STejun Heo 
327fa0b54f1SAlexander Aring 	r->res_id = id;
3281d7c484eSDavid Teigland 	ls->ls_recover_list_count++;
3291d7c484eSDavid Teigland 	dlm_hold_rsb(r);
3302a86b3e7STejun Heo 	rv = 0;
3312a86b3e7STejun Heo out_unlock:
332fa0b54f1SAlexander Aring 	spin_unlock_bh(&ls->ls_recover_xa_lock);
3332a86b3e7STejun Heo 	return rv;
3341d7c484eSDavid Teigland }
3351d7c484eSDavid Teigland 
recover_xa_del(struct dlm_rsb * r)336fa0b54f1SAlexander Aring static void recover_xa_del(struct dlm_rsb *r)
3371d7c484eSDavid Teigland {
3381d7c484eSDavid Teigland 	struct dlm_ls *ls = r->res_ls;
3391d7c484eSDavid Teigland 
340fa0b54f1SAlexander Aring 	spin_lock_bh(&ls->ls_recover_xa_lock);
341fa0b54f1SAlexander Aring 	xa_erase_bh(&ls->ls_recover_xa, r->res_id);
3421d7c484eSDavid Teigland 	r->res_id = 0;
3431d7c484eSDavid Teigland 	ls->ls_recover_list_count--;
344fa0b54f1SAlexander Aring 	spin_unlock_bh(&ls->ls_recover_xa_lock);
3451d7c484eSDavid Teigland 
3461d7c484eSDavid Teigland 	dlm_put_rsb(r);
3471d7c484eSDavid Teigland }
3481d7c484eSDavid Teigland 
recover_xa_find(struct dlm_ls * ls,uint64_t id)349fa0b54f1SAlexander Aring static struct dlm_rsb *recover_xa_find(struct dlm_ls *ls, uint64_t id)
3501d7c484eSDavid Teigland {
3511d7c484eSDavid Teigland 	struct dlm_rsb *r;
3521d7c484eSDavid Teigland 
353fa0b54f1SAlexander Aring 	spin_lock_bh(&ls->ls_recover_xa_lock);
354fa0b54f1SAlexander Aring 	r = xa_load(&ls->ls_recover_xa, (int)id);
355fa0b54f1SAlexander Aring 	spin_unlock_bh(&ls->ls_recover_xa_lock);
3561d7c484eSDavid Teigland 	return r;
3571d7c484eSDavid Teigland }
3581d7c484eSDavid Teigland 
recover_xa_clear(struct dlm_ls * ls)359fa0b54f1SAlexander Aring static void recover_xa_clear(struct dlm_ls *ls)
3601d7c484eSDavid Teigland {
361cda95406STejun Heo 	struct dlm_rsb *r;
362fa0b54f1SAlexander Aring 	unsigned long id;
3631d7c484eSDavid Teigland 
364fa0b54f1SAlexander Aring 	spin_lock_bh(&ls->ls_recover_xa_lock);
365cda95406STejun Heo 
366fa0b54f1SAlexander Aring 	xa_for_each(&ls->ls_recover_xa, id, r) {
367fa0b54f1SAlexander Aring 		xa_erase_bh(&ls->ls_recover_xa, id);
3681d7c484eSDavid Teigland 		r->res_id = 0;
3691d7c484eSDavid Teigland 		r->res_recover_locks_count = 0;
3701d7c484eSDavid Teigland 		ls->ls_recover_list_count--;
3711d7c484eSDavid Teigland 
3721d7c484eSDavid Teigland 		dlm_put_rsb(r);
3731d7c484eSDavid Teigland 	}
3741d7c484eSDavid Teigland 
3751d7c484eSDavid Teigland 	if (ls->ls_recover_list_count != 0) {
3761d7c484eSDavid Teigland 		log_error(ls, "warning: recover_list_count %d",
3771d7c484eSDavid Teigland 			  ls->ls_recover_list_count);
3781d7c484eSDavid Teigland 		ls->ls_recover_list_count = 0;
3791d7c484eSDavid Teigland 	}
380fa0b54f1SAlexander Aring 	spin_unlock_bh(&ls->ls_recover_xa_lock);
3811d7c484eSDavid Teigland }
3821d7c484eSDavid Teigland 
383e7fd4179SDavid Teigland 
384e7fd4179SDavid Teigland /* Master recovery: find new master node for rsb's that were
385e7fd4179SDavid Teigland    mastered on nodes that have been removed.
386e7fd4179SDavid Teigland 
387e7fd4179SDavid Teigland    dlm_recover_masters
388e7fd4179SDavid Teigland    recover_master
389e7fd4179SDavid Teigland    dlm_send_rcom_lookup            ->  receive_rcom_lookup
390e7fd4179SDavid Teigland                                        dlm_dir_lookup
391e7fd4179SDavid Teigland    receive_rcom_lookup_reply       <-
392e7fd4179SDavid Teigland    dlm_recover_master_reply
393e7fd4179SDavid Teigland    set_new_master
394e7fd4179SDavid Teigland    set_master_lkbs
395e7fd4179SDavid Teigland    set_lock_master
396e7fd4179SDavid Teigland */
397e7fd4179SDavid Teigland 
398e7fd4179SDavid Teigland /*
399e7fd4179SDavid Teigland  * Set the lock master for all LKBs in a lock queue
400e7fd4179SDavid Teigland  * If we are the new master of the rsb, we may have received new
401e7fd4179SDavid Teigland  * MSTCPY locks from other nodes already which we need to ignore
402e7fd4179SDavid Teigland  * when setting the new nodeid.
403e7fd4179SDavid Teigland  */
404e7fd4179SDavid Teigland 
set_lock_master(struct list_head * queue,int nodeid)405e7fd4179SDavid Teigland static void set_lock_master(struct list_head *queue, int nodeid)
406e7fd4179SDavid Teigland {
407e7fd4179SDavid Teigland 	struct dlm_lkb *lkb;
408e7fd4179SDavid Teigland 
4094875647aSDavid Teigland 	list_for_each_entry(lkb, queue, lkb_statequeue) {
410e1af8728SAlexander Aring 		if (!test_bit(DLM_IFL_MSTCPY_BIT, &lkb->lkb_iflags)) {
411e7fd4179SDavid Teigland 			lkb->lkb_nodeid = nodeid;
4124875647aSDavid Teigland 			lkb->lkb_remid = 0;
4134875647aSDavid Teigland 		}
4144875647aSDavid Teigland 	}
415e7fd4179SDavid Teigland }
416e7fd4179SDavid Teigland 
set_master_lkbs(struct dlm_rsb * r)417e7fd4179SDavid Teigland static void set_master_lkbs(struct dlm_rsb *r)
418e7fd4179SDavid Teigland {
419e7fd4179SDavid Teigland 	set_lock_master(&r->res_grantqueue, r->res_nodeid);
420e7fd4179SDavid Teigland 	set_lock_master(&r->res_convertqueue, r->res_nodeid);
421e7fd4179SDavid Teigland 	set_lock_master(&r->res_waitqueue, r->res_nodeid);
422e7fd4179SDavid Teigland }
423e7fd4179SDavid Teigland 
424e7fd4179SDavid Teigland /*
42525985edcSLucas De Marchi  * Propagate the new master nodeid to locks
426e7fd4179SDavid Teigland  * The NEW_MASTER flag tells dlm_recover_locks() which rsb's to consider.
4274875647aSDavid Teigland  * The NEW_MASTER2 flag tells recover_lvb() and recover_grant() which
428f7da790dSDavid Teigland  * rsb's to consider.
429e7fd4179SDavid Teigland  */
430e7fd4179SDavid Teigland 
set_new_master(struct dlm_rsb * r)431c04fecb4SDavid Teigland static void set_new_master(struct dlm_rsb *r)
432e7fd4179SDavid Teigland {
433e7fd4179SDavid Teigland 	set_master_lkbs(r);
434e7fd4179SDavid Teigland 	rsb_set_flag(r, RSB_NEW_MASTER);
435e7fd4179SDavid Teigland 	rsb_set_flag(r, RSB_NEW_MASTER2);
436e7fd4179SDavid Teigland }
437e7fd4179SDavid Teigland 
438e7fd4179SDavid Teigland /*
439e7fd4179SDavid Teigland  * We do async lookups on rsb's that need new masters.  The rsb's
440e7fd4179SDavid Teigland  * waiting for a lookup reply are kept on the recover_list.
441c04fecb4SDavid Teigland  *
442c04fecb4SDavid Teigland  * Another node recovering the master may have sent us a rcom lookup,
443c04fecb4SDavid Teigland  * and our dlm_master_lookup() set it as the new master, along with
444c04fecb4SDavid Teigland  * NEW_MASTER so that we'll recover it here (this implies dir_nodeid
445c04fecb4SDavid Teigland  * equals our_nodeid below).
446e7fd4179SDavid Teigland  */
447e7fd4179SDavid Teigland 
recover_master(struct dlm_rsb * r,unsigned int * count,uint64_t seq)448c4f4e135SAlexander Aring static int recover_master(struct dlm_rsb *r, unsigned int *count, uint64_t seq)
449e7fd4179SDavid Teigland {
450e7fd4179SDavid Teigland 	struct dlm_ls *ls = r->res_ls;
451c04fecb4SDavid Teigland 	int our_nodeid, dir_nodeid;
452c04fecb4SDavid Teigland 	int is_removed = 0;
453c04fecb4SDavid Teigland 	int error;
454c04fecb4SDavid Teigland 
455*d47b8229SAlexander Aring 	if (r->res_nodeid != -1 && is_master(r))
456c04fecb4SDavid Teigland 		return 0;
457c04fecb4SDavid Teigland 
458*d47b8229SAlexander Aring 	if (r->res_nodeid != -1)
459c04fecb4SDavid Teigland 		is_removed = dlm_is_removed(ls, r->res_nodeid);
460c04fecb4SDavid Teigland 
461c04fecb4SDavid Teigland 	if (!is_removed && !rsb_flag(r, RSB_NEW_MASTER))
462c04fecb4SDavid Teigland 		return 0;
463c04fecb4SDavid Teigland 
464c04fecb4SDavid Teigland 	our_nodeid = dlm_our_nodeid();
465c04fecb4SDavid Teigland 	dir_nodeid = dlm_dir_nodeid(r);
466e7fd4179SDavid Teigland 
467e7fd4179SDavid Teigland 	if (dir_nodeid == our_nodeid) {
468c04fecb4SDavid Teigland 		if (is_removed) {
469c04fecb4SDavid Teigland 			r->res_master_nodeid = our_nodeid;
470c04fecb4SDavid Teigland 			r->res_nodeid = 0;
471c04fecb4SDavid Teigland 		}
472e7fd4179SDavid Teigland 
473c04fecb4SDavid Teigland 		/* set master of lkbs to ourself when is_removed, or to
474c04fecb4SDavid Teigland 		   another new master which we set along with NEW_MASTER
475c04fecb4SDavid Teigland 		   in dlm_master_lookup */
476c04fecb4SDavid Teigland 		set_new_master(r);
477c04fecb4SDavid Teigland 		error = 0;
478e7fd4179SDavid Teigland 	} else {
479fa0b54f1SAlexander Aring 		recover_xa_add(r);
480c4f4e135SAlexander Aring 		error = dlm_send_rcom_lookup(r, dir_nodeid, seq);
481e7fd4179SDavid Teigland 	}
482e7fd4179SDavid Teigland 
483c04fecb4SDavid Teigland 	(*count)++;
484e7fd4179SDavid Teigland 	return error;
485e7fd4179SDavid Teigland }
486e7fd4179SDavid Teigland 
487e7fd4179SDavid Teigland /*
4884875647aSDavid Teigland  * All MSTCPY locks are purged and rebuilt, even if the master stayed the same.
4894875647aSDavid Teigland  * This is necessary because recovery can be started, aborted and restarted,
4904875647aSDavid Teigland  * causing the master nodeid to briefly change during the aborted recovery, and
4914875647aSDavid Teigland  * change back to the original value in the second recovery.  The MSTCPY locks
4924875647aSDavid Teigland  * may or may not have been purged during the aborted recovery.  Another node
4934875647aSDavid Teigland  * with an outstanding request in waiters list and a request reply saved in the
4944875647aSDavid Teigland  * requestqueue, cannot know whether it should ignore the reply and resend the
4954875647aSDavid Teigland  * request, or accept the reply and complete the request.  It must do the
4964875647aSDavid Teigland  * former if the remote node purged MSTCPY locks, and it must do the later if
4974875647aSDavid Teigland  * the remote node did not.  This is solved by always purging MSTCPY locks, in
4984875647aSDavid Teigland  * which case, the request reply would always be ignored and the request
4994875647aSDavid Teigland  * resent.
500e7fd4179SDavid Teigland  */
501e7fd4179SDavid Teigland 
recover_master_static(struct dlm_rsb * r,unsigned int * count)502c04fecb4SDavid Teigland static int recover_master_static(struct dlm_rsb *r, unsigned int *count)
503e7fd4179SDavid Teigland {
5044875647aSDavid Teigland 	int dir_nodeid = dlm_dir_nodeid(r);
5054875647aSDavid Teigland 	int new_master = dir_nodeid;
506e7fd4179SDavid Teigland 
5074875647aSDavid Teigland 	if (dir_nodeid == dlm_our_nodeid())
5084875647aSDavid Teigland 		new_master = 0;
509e7fd4179SDavid Teigland 
510e7fd4179SDavid Teigland 	dlm_purge_mstcpy_locks(r);
511c04fecb4SDavid Teigland 	r->res_master_nodeid = dir_nodeid;
512c04fecb4SDavid Teigland 	r->res_nodeid = new_master;
513c04fecb4SDavid Teigland 	set_new_master(r);
514c04fecb4SDavid Teigland 	(*count)++;
515c04fecb4SDavid Teigland 	return 0;
516e7fd4179SDavid Teigland }
517e7fd4179SDavid Teigland 
518e7fd4179SDavid Teigland /*
519e7fd4179SDavid Teigland  * Go through local root resources and for each rsb which has a master which
520e7fd4179SDavid Teigland  * has departed, get the new master nodeid from the directory.  The dir will
521e7fd4179SDavid Teigland  * assign mastery to the first node to look up the new master.  That means
522e7fd4179SDavid Teigland  * we'll discover in this lookup if we're the new master of any rsb's.
523e7fd4179SDavid Teigland  *
524e7fd4179SDavid Teigland  * We fire off all the dir lookup requests individually and asynchronously to
525e7fd4179SDavid Teigland  * the correct dir node.
526e7fd4179SDavid Teigland  */
527e7fd4179SDavid Teigland 
dlm_recover_masters(struct dlm_ls * ls,uint64_t seq,const struct list_head * root_list)5283a747f4aSAlexander Aring int dlm_recover_masters(struct dlm_ls *ls, uint64_t seq,
5293a747f4aSAlexander Aring 			const struct list_head *root_list)
530e7fd4179SDavid Teigland {
531e7fd4179SDavid Teigland 	struct dlm_rsb *r;
532c04fecb4SDavid Teigland 	unsigned int total = 0;
533c04fecb4SDavid Teigland 	unsigned int count = 0;
534c04fecb4SDavid Teigland 	int nodir = dlm_no_directory(ls);
535c04fecb4SDavid Teigland 	int error;
536e7fd4179SDavid Teigland 
537075f0177SDavid Teigland 	log_rinfo(ls, "dlm_recover_masters");
538e7fd4179SDavid Teigland 
5393a747f4aSAlexander Aring 	list_for_each_entry(r, root_list, res_root_list) {
540e7fd4179SDavid Teigland 		if (dlm_recovery_stopped(ls)) {
541e7fd4179SDavid Teigland 			error = -EINTR;
542e7fd4179SDavid Teigland 			goto out;
543e7fd4179SDavid Teigland 		}
544e7fd4179SDavid Teigland 
545c04fecb4SDavid Teigland 		lock_rsb(r);
546c04fecb4SDavid Teigland 		if (nodir)
547c04fecb4SDavid Teigland 			error = recover_master_static(r, &count);
548c04fecb4SDavid Teigland 		else
549c4f4e135SAlexander Aring 			error = recover_master(r, &count, seq);
550c04fecb4SDavid Teigland 		unlock_rsb(r);
551c04fecb4SDavid Teigland 		cond_resched();
552c04fecb4SDavid Teigland 		total++;
553e7fd4179SDavid Teigland 
5543a747f4aSAlexander Aring 		if (error)
555c04fecb4SDavid Teigland 			goto out;
556c04fecb4SDavid Teigland 	}
557e7fd4179SDavid Teigland 
558075f0177SDavid Teigland 	log_rinfo(ls, "dlm_recover_masters %u of %u", count, total);
559e7fd4179SDavid Teigland 
560fa0b54f1SAlexander Aring 	error = dlm_wait_function(ls, &recover_xa_empty);
561e7fd4179SDavid Teigland  out:
562e7fd4179SDavid Teigland 	if (error)
563fa0b54f1SAlexander Aring 		recover_xa_clear(ls);
564e7fd4179SDavid Teigland 	return error;
565e7fd4179SDavid Teigland }
566e7fd4179SDavid Teigland 
dlm_recover_master_reply(struct dlm_ls * ls,const struct dlm_rcom * rc)56711519351SAlexander Aring int dlm_recover_master_reply(struct dlm_ls *ls, const struct dlm_rcom *rc)
568e7fd4179SDavid Teigland {
569e7fd4179SDavid Teigland 	struct dlm_rsb *r;
570c04fecb4SDavid Teigland 	int ret_nodeid, new_master;
571e7fd4179SDavid Teigland 
572fa0b54f1SAlexander Aring 	r = recover_xa_find(ls, le64_to_cpu(rc->rc_id));
573e7fd4179SDavid Teigland 	if (!r) {
57490135925SDavid Teigland 		log_error(ls, "dlm_recover_master_reply no id %llx",
5752f9dbedaSAlexander Aring 			  (unsigned long long)le64_to_cpu(rc->rc_id));
576e7fd4179SDavid Teigland 		goto out;
577e7fd4179SDavid Teigland 	}
578e7fd4179SDavid Teigland 
5792f9dbedaSAlexander Aring 	ret_nodeid = le32_to_cpu(rc->rc_result);
580c04fecb4SDavid Teigland 
581c04fecb4SDavid Teigland 	if (ret_nodeid == dlm_our_nodeid())
582c04fecb4SDavid Teigland 		new_master = 0;
583c04fecb4SDavid Teigland 	else
584c04fecb4SDavid Teigland 		new_master = ret_nodeid;
585e7fd4179SDavid Teigland 
5864875647aSDavid Teigland 	lock_rsb(r);
587c04fecb4SDavid Teigland 	r->res_master_nodeid = ret_nodeid;
588c04fecb4SDavid Teigland 	r->res_nodeid = new_master;
589c04fecb4SDavid Teigland 	set_new_master(r);
5904875647aSDavid Teigland 	unlock_rsb(r);
591fa0b54f1SAlexander Aring 	recover_xa_del(r);
592e7fd4179SDavid Teigland 
593fa0b54f1SAlexander Aring 	if (recover_xa_empty(ls))
594e7fd4179SDavid Teigland 		wake_up(&ls->ls_wait_general);
595e7fd4179SDavid Teigland  out:
596e7fd4179SDavid Teigland 	return 0;
597e7fd4179SDavid Teigland }
598e7fd4179SDavid Teigland 
599e7fd4179SDavid Teigland 
600e7fd4179SDavid Teigland /* Lock recovery: rebuild the process-copy locks we hold on a
601e7fd4179SDavid Teigland    remastered rsb on the new rsb master.
602e7fd4179SDavid Teigland 
603e7fd4179SDavid Teigland    dlm_recover_locks
604e7fd4179SDavid Teigland    recover_locks
605e7fd4179SDavid Teigland    recover_locks_queue
606e7fd4179SDavid Teigland    dlm_send_rcom_lock              ->  receive_rcom_lock
607e7fd4179SDavid Teigland                                        dlm_recover_master_copy
608e7fd4179SDavid Teigland    receive_rcom_lock_reply         <-
609e7fd4179SDavid Teigland    dlm_recover_process_copy
610e7fd4179SDavid Teigland */
611e7fd4179SDavid Teigland 
612e7fd4179SDavid Teigland 
613e7fd4179SDavid Teigland /*
614e7fd4179SDavid Teigland  * keep a count of the number of lkb's we send to the new master; when we get
615e7fd4179SDavid Teigland  * an equal number of replies then recovery for the rsb is done
616e7fd4179SDavid Teigland  */
617e7fd4179SDavid Teigland 
recover_locks_queue(struct dlm_rsb * r,struct list_head * head,uint64_t seq)618c4f4e135SAlexander Aring static int recover_locks_queue(struct dlm_rsb *r, struct list_head *head,
619c4f4e135SAlexander Aring 			       uint64_t seq)
620e7fd4179SDavid Teigland {
621e7fd4179SDavid Teigland 	struct dlm_lkb *lkb;
622e7fd4179SDavid Teigland 	int error = 0;
623e7fd4179SDavid Teigland 
624e7fd4179SDavid Teigland 	list_for_each_entry(lkb, head, lkb_statequeue) {
625c4f4e135SAlexander Aring 		error = dlm_send_rcom_lock(r, lkb, seq);
626e7fd4179SDavid Teigland 		if (error)
627e7fd4179SDavid Teigland 			break;
628e7fd4179SDavid Teigland 		r->res_recover_locks_count++;
629e7fd4179SDavid Teigland 	}
630e7fd4179SDavid Teigland 
631e7fd4179SDavid Teigland 	return error;
632e7fd4179SDavid Teigland }
633e7fd4179SDavid Teigland 
recover_locks(struct dlm_rsb * r,uint64_t seq)634c4f4e135SAlexander Aring static int recover_locks(struct dlm_rsb *r, uint64_t seq)
635e7fd4179SDavid Teigland {
636e7fd4179SDavid Teigland 	int error = 0;
637e7fd4179SDavid Teigland 
638e7fd4179SDavid Teigland 	lock_rsb(r);
639e7fd4179SDavid Teigland 
640a345da3eSDavid Teigland 	DLM_ASSERT(!r->res_recover_locks_count, dlm_dump_rsb(r););
641e7fd4179SDavid Teigland 
642c4f4e135SAlexander Aring 	error = recover_locks_queue(r, &r->res_grantqueue, seq);
643e7fd4179SDavid Teigland 	if (error)
644e7fd4179SDavid Teigland 		goto out;
645c4f4e135SAlexander Aring 	error = recover_locks_queue(r, &r->res_convertqueue, seq);
646e7fd4179SDavid Teigland 	if (error)
647e7fd4179SDavid Teigland 		goto out;
648c4f4e135SAlexander Aring 	error = recover_locks_queue(r, &r->res_waitqueue, seq);
649e7fd4179SDavid Teigland 	if (error)
650e7fd4179SDavid Teigland 		goto out;
651e7fd4179SDavid Teigland 
652e7fd4179SDavid Teigland 	if (r->res_recover_locks_count)
653e7fd4179SDavid Teigland 		recover_list_add(r);
654e7fd4179SDavid Teigland 	else
655e7fd4179SDavid Teigland 		rsb_clear_flag(r, RSB_NEW_MASTER);
656e7fd4179SDavid Teigland  out:
657e7fd4179SDavid Teigland 	unlock_rsb(r);
658e7fd4179SDavid Teigland 	return error;
659e7fd4179SDavid Teigland }
660e7fd4179SDavid Teigland 
dlm_recover_locks(struct dlm_ls * ls,uint64_t seq,const struct list_head * root_list)6613a747f4aSAlexander Aring int dlm_recover_locks(struct dlm_ls *ls, uint64_t seq,
6623a747f4aSAlexander Aring 		      const struct list_head *root_list)
663e7fd4179SDavid Teigland {
664e7fd4179SDavid Teigland 	struct dlm_rsb *r;
665e7fd4179SDavid Teigland 	int error, count = 0;
666e7fd4179SDavid Teigland 
6673a747f4aSAlexander Aring 	list_for_each_entry(r, root_list, res_root_list) {
668*d47b8229SAlexander Aring 		if (r->res_nodeid != -1 && is_master(r)) {
669e7fd4179SDavid Teigland 			rsb_clear_flag(r, RSB_NEW_MASTER);
670e7fd4179SDavid Teigland 			continue;
671e7fd4179SDavid Teigland 		}
672e7fd4179SDavid Teigland 
673e7fd4179SDavid Teigland 		if (!rsb_flag(r, RSB_NEW_MASTER))
674e7fd4179SDavid Teigland 			continue;
675e7fd4179SDavid Teigland 
676e7fd4179SDavid Teigland 		if (dlm_recovery_stopped(ls)) {
677e7fd4179SDavid Teigland 			error = -EINTR;
678e7fd4179SDavid Teigland 			goto out;
679e7fd4179SDavid Teigland 		}
680e7fd4179SDavid Teigland 
681c4f4e135SAlexander Aring 		error = recover_locks(r, seq);
6823a747f4aSAlexander Aring 		if (error)
683e7fd4179SDavid Teigland 			goto out;
684e7fd4179SDavid Teigland 
685e7fd4179SDavid Teigland 		count += r->res_recover_locks_count;
686e7fd4179SDavid Teigland 	}
687e7fd4179SDavid Teigland 
688075f0177SDavid Teigland 	log_rinfo(ls, "dlm_recover_locks %d out", count);
689e7fd4179SDavid Teigland 
690e7fd4179SDavid Teigland 	error = dlm_wait_function(ls, &recover_list_empty);
691e7fd4179SDavid Teigland  out:
692e7fd4179SDavid Teigland 	if (error)
693e7fd4179SDavid Teigland 		recover_list_clear(ls);
694e7fd4179SDavid Teigland 	return error;
695e7fd4179SDavid Teigland }
696e7fd4179SDavid Teigland 
dlm_recovered_lock(struct dlm_rsb * r)697e7fd4179SDavid Teigland void dlm_recovered_lock(struct dlm_rsb *r)
698e7fd4179SDavid Teigland {
699a345da3eSDavid Teigland 	DLM_ASSERT(rsb_flag(r, RSB_NEW_MASTER), dlm_dump_rsb(r););
700e7fd4179SDavid Teigland 
701e7fd4179SDavid Teigland 	r->res_recover_locks_count--;
702e7fd4179SDavid Teigland 	if (!r->res_recover_locks_count) {
703e7fd4179SDavid Teigland 		rsb_clear_flag(r, RSB_NEW_MASTER);
704e7fd4179SDavid Teigland 		recover_list_del(r);
705e7fd4179SDavid Teigland 	}
706e7fd4179SDavid Teigland 
707e7fd4179SDavid Teigland 	if (recover_list_empty(r->res_ls))
708e7fd4179SDavid Teigland 		wake_up(&r->res_ls->ls_wait_general);
709e7fd4179SDavid Teigland }
710e7fd4179SDavid Teigland 
711e7fd4179SDavid Teigland /*
712e7fd4179SDavid Teigland  * The lvb needs to be recovered on all master rsb's.  This includes setting
713e7fd4179SDavid Teigland  * the VALNOTVALID flag if necessary, and determining the correct lvb contents
714e7fd4179SDavid Teigland  * based on the lvb's of the locks held on the rsb.
715e7fd4179SDavid Teigland  *
716da8c6663SDavid Teigland  * RSB_VALNOTVALID is set in two cases:
717da8c6663SDavid Teigland  *
718da8c6663SDavid Teigland  * 1. we are master, but not new, and we purged an EX/PW lock held by a
719da8c6663SDavid Teigland  * failed node (in dlm_recover_purge which set RSB_RECOVER_LVB_INVAL)
720da8c6663SDavid Teigland  *
721da8c6663SDavid Teigland  * 2. we are a new master, and there are only NL/CR locks left.
722da8c6663SDavid Teigland  * (We could probably improve this by only invaliding in this way when
723da8c6663SDavid Teigland  * the previous master left uncleanly.  VMS docs mention that.)
724e7fd4179SDavid Teigland  *
725e7fd4179SDavid Teigland  * The LVB contents are only considered for changing when this is a new master
726e7fd4179SDavid Teigland  * of the rsb (NEW_MASTER2).  Then, the rsb's lvb is taken from any lkb with
727e7fd4179SDavid Teigland  * mode > CR.  If no lkb's exist with mode above CR, the lvb contents are taken
728e7fd4179SDavid Teigland  * from the lkb with the largest lvb sequence number.
729e7fd4179SDavid Teigland  */
730e7fd4179SDavid Teigland 
recover_lvb(struct dlm_rsb * r)731e7fd4179SDavid Teigland static void recover_lvb(struct dlm_rsb *r)
732e7fd4179SDavid Teigland {
733dc1acd5cSJakob Koschel 	struct dlm_lkb *big_lkb = NULL, *iter, *high_lkb = NULL;
734e7fd4179SDavid Teigland 	uint32_t high_seq = 0;
73590135925SDavid Teigland 	int lock_lvb_exists = 0;
736e7fd4179SDavid Teigland 	int lvblen = r->res_ls->ls_lvblen;
737e7fd4179SDavid Teigland 
738da8c6663SDavid Teigland 	if (!rsb_flag(r, RSB_NEW_MASTER2) &&
739da8c6663SDavid Teigland 	    rsb_flag(r, RSB_RECOVER_LVB_INVAL)) {
740da8c6663SDavid Teigland 		/* case 1 above */
741da8c6663SDavid Teigland 		rsb_set_flag(r, RSB_VALNOTVALID);
742da8c6663SDavid Teigland 		return;
743da8c6663SDavid Teigland 	}
744da8c6663SDavid Teigland 
745da8c6663SDavid Teigland 	if (!rsb_flag(r, RSB_NEW_MASTER2))
746da8c6663SDavid Teigland 		return;
747da8c6663SDavid Teigland 
748da8c6663SDavid Teigland 	/* we are the new master, so figure out if VALNOTVALID should
749da8c6663SDavid Teigland 	   be set, and set the rsb lvb from the best lkb available. */
750da8c6663SDavid Teigland 
751dc1acd5cSJakob Koschel 	list_for_each_entry(iter, &r->res_grantqueue, lkb_statequeue) {
752dc1acd5cSJakob Koschel 		if (!(iter->lkb_exflags & DLM_LKF_VALBLK))
753e7fd4179SDavid Teigland 			continue;
754e7fd4179SDavid Teigland 
75590135925SDavid Teigland 		lock_lvb_exists = 1;
756e7fd4179SDavid Teigland 
757dc1acd5cSJakob Koschel 		if (iter->lkb_grmode > DLM_LOCK_CR) {
758dc1acd5cSJakob Koschel 			big_lkb = iter;
759e7fd4179SDavid Teigland 			goto setflag;
760e7fd4179SDavid Teigland 		}
761e7fd4179SDavid Teigland 
762dc1acd5cSJakob Koschel 		if (((int)iter->lkb_lvbseq - (int)high_seq) >= 0) {
763dc1acd5cSJakob Koschel 			high_lkb = iter;
764dc1acd5cSJakob Koschel 			high_seq = iter->lkb_lvbseq;
765e7fd4179SDavid Teigland 		}
766e7fd4179SDavid Teigland 	}
767e7fd4179SDavid Teigland 
768dc1acd5cSJakob Koschel 	list_for_each_entry(iter, &r->res_convertqueue, lkb_statequeue) {
769dc1acd5cSJakob Koschel 		if (!(iter->lkb_exflags & DLM_LKF_VALBLK))
770e7fd4179SDavid Teigland 			continue;
771e7fd4179SDavid Teigland 
77290135925SDavid Teigland 		lock_lvb_exists = 1;
773e7fd4179SDavid Teigland 
774dc1acd5cSJakob Koschel 		if (iter->lkb_grmode > DLM_LOCK_CR) {
775dc1acd5cSJakob Koschel 			big_lkb = iter;
776e7fd4179SDavid Teigland 			goto setflag;
777e7fd4179SDavid Teigland 		}
778e7fd4179SDavid Teigland 
779dc1acd5cSJakob Koschel 		if (((int)iter->lkb_lvbseq - (int)high_seq) >= 0) {
780dc1acd5cSJakob Koschel 			high_lkb = iter;
781dc1acd5cSJakob Koschel 			high_seq = iter->lkb_lvbseq;
782e7fd4179SDavid Teigland 		}
783e7fd4179SDavid Teigland 	}
784e7fd4179SDavid Teigland 
785e7fd4179SDavid Teigland  setflag:
786e7fd4179SDavid Teigland 	if (!lock_lvb_exists)
787e7fd4179SDavid Teigland 		goto out;
788e7fd4179SDavid Teigland 
789da8c6663SDavid Teigland 	/* lvb is invalidated if only NL/CR locks remain */
790dc1acd5cSJakob Koschel 	if (!big_lkb)
791e7fd4179SDavid Teigland 		rsb_set_flag(r, RSB_VALNOTVALID);
792e7fd4179SDavid Teigland 
793e7fd4179SDavid Teigland 	if (!r->res_lvbptr) {
79452bda2b5SDavid Teigland 		r->res_lvbptr = dlm_allocate_lvb(r->res_ls);
795e7fd4179SDavid Teigland 		if (!r->res_lvbptr)
796e7fd4179SDavid Teigland 			goto out;
797e7fd4179SDavid Teigland 	}
798e7fd4179SDavid Teigland 
799dc1acd5cSJakob Koschel 	if (big_lkb) {
800dc1acd5cSJakob Koschel 		r->res_lvbseq = big_lkb->lkb_lvbseq;
801dc1acd5cSJakob Koschel 		memcpy(r->res_lvbptr, big_lkb->lkb_lvbptr, lvblen);
802e7fd4179SDavid Teigland 	} else if (high_lkb) {
803e7fd4179SDavid Teigland 		r->res_lvbseq = high_lkb->lkb_lvbseq;
804e7fd4179SDavid Teigland 		memcpy(r->res_lvbptr, high_lkb->lkb_lvbptr, lvblen);
805e7fd4179SDavid Teigland 	} else {
806e7fd4179SDavid Teigland 		r->res_lvbseq = 0;
807e7fd4179SDavid Teigland 		memset(r->res_lvbptr, 0, lvblen);
808e7fd4179SDavid Teigland 	}
809e7fd4179SDavid Teigland  out:
810e7fd4179SDavid Teigland 	return;
811e7fd4179SDavid Teigland }
812e7fd4179SDavid Teigland 
813e7fd4179SDavid Teigland /* All master rsb's flagged RECOVER_CONVERT need to be looked at.  The locks
814e7fd4179SDavid Teigland    converting PR->CW or CW->PR need to have their lkb_grmode set. */
815e7fd4179SDavid Teigland 
recover_conversion(struct dlm_rsb * r)816e7fd4179SDavid Teigland static void recover_conversion(struct dlm_rsb *r)
817e7fd4179SDavid Teigland {
818c503a621SDavid Teigland 	struct dlm_ls *ls = r->res_ls;
819e7fd4179SDavid Teigland 	struct dlm_lkb *lkb;
820e7fd4179SDavid Teigland 	int grmode = -1;
821e7fd4179SDavid Teigland 
822e7fd4179SDavid Teigland 	list_for_each_entry(lkb, &r->res_grantqueue, lkb_statequeue) {
823e7fd4179SDavid Teigland 		if (lkb->lkb_grmode == DLM_LOCK_PR ||
824e7fd4179SDavid Teigland 		    lkb->lkb_grmode == DLM_LOCK_CW) {
825e7fd4179SDavid Teigland 			grmode = lkb->lkb_grmode;
826e7fd4179SDavid Teigland 			break;
827e7fd4179SDavid Teigland 		}
828e7fd4179SDavid Teigland 	}
829e7fd4179SDavid Teigland 
830e7fd4179SDavid Teigland 	list_for_each_entry(lkb, &r->res_convertqueue, lkb_statequeue) {
831e7fd4179SDavid Teigland 		if (lkb->lkb_grmode != DLM_LOCK_IV)
832e7fd4179SDavid Teigland 			continue;
833c503a621SDavid Teigland 		if (grmode == -1) {
834c503a621SDavid Teigland 			log_debug(ls, "recover_conversion %x set gr to rq %d",
835c503a621SDavid Teigland 				  lkb->lkb_id, lkb->lkb_rqmode);
836e7fd4179SDavid Teigland 			lkb->lkb_grmode = lkb->lkb_rqmode;
837c503a621SDavid Teigland 		} else {
838c503a621SDavid Teigland 			log_debug(ls, "recover_conversion %x set gr %d",
839c503a621SDavid Teigland 				  lkb->lkb_id, grmode);
840e7fd4179SDavid Teigland 			lkb->lkb_grmode = grmode;
841e7fd4179SDavid Teigland 		}
842e7fd4179SDavid Teigland 	}
843c503a621SDavid Teigland }
844e7fd4179SDavid Teigland 
845f7da790dSDavid Teigland /* We've become the new master for this rsb and waiting/converting locks may
8464875647aSDavid Teigland    need to be granted in dlm_recover_grant() due to locks that may have
847f7da790dSDavid Teigland    existed from a removed node. */
848f7da790dSDavid Teigland 
recover_grant(struct dlm_rsb * r)8494875647aSDavid Teigland static void recover_grant(struct dlm_rsb *r)
850f7da790dSDavid Teigland {
851f7da790dSDavid Teigland 	if (!list_empty(&r->res_waitqueue) || !list_empty(&r->res_convertqueue))
8524875647aSDavid Teigland 		rsb_set_flag(r, RSB_RECOVER_GRANT);
853f7da790dSDavid Teigland }
854f7da790dSDavid Teigland 
dlm_recover_rsbs(struct dlm_ls * ls,const struct list_head * root_list)8553a747f4aSAlexander Aring void dlm_recover_rsbs(struct dlm_ls *ls, const struct list_head *root_list)
856e7fd4179SDavid Teigland {
857e7fd4179SDavid Teigland 	struct dlm_rsb *r;
8584875647aSDavid Teigland 	unsigned int count = 0;
859e7fd4179SDavid Teigland 
8603a747f4aSAlexander Aring 	list_for_each_entry(r, root_list, res_root_list) {
861e7fd4179SDavid Teigland 		lock_rsb(r);
862*d47b8229SAlexander Aring 		if (r->res_nodeid != -1 && is_master(r)) {
863e7fd4179SDavid Teigland 			if (rsb_flag(r, RSB_RECOVER_CONVERT))
864e7fd4179SDavid Teigland 				recover_conversion(r);
865da8c6663SDavid Teigland 
866da8c6663SDavid Teigland 			/* recover lvb before granting locks so the updated
867da8c6663SDavid Teigland 			   lvb/VALNOTVALID is presented in the completion */
868da8c6663SDavid Teigland 			recover_lvb(r);
869da8c6663SDavid Teigland 
870f7da790dSDavid Teigland 			if (rsb_flag(r, RSB_NEW_MASTER2))
8714875647aSDavid Teigland 				recover_grant(r);
872e7fd4179SDavid Teigland 			count++;
873da8c6663SDavid Teigland 		} else {
874da8c6663SDavid Teigland 			rsb_clear_flag(r, RSB_VALNOTVALID);
875e7fd4179SDavid Teigland 		}
876e7fd4179SDavid Teigland 		rsb_clear_flag(r, RSB_RECOVER_CONVERT);
877da8c6663SDavid Teigland 		rsb_clear_flag(r, RSB_RECOVER_LVB_INVAL);
878f7da790dSDavid Teigland 		rsb_clear_flag(r, RSB_NEW_MASTER2);
879e7fd4179SDavid Teigland 		unlock_rsb(r);
880e7fd4179SDavid Teigland 	}
881e7fd4179SDavid Teigland 
8824875647aSDavid Teigland 	if (count)
883075f0177SDavid Teigland 		log_rinfo(ls, "dlm_recover_rsbs %d done", count);
884e7fd4179SDavid Teigland }
885e7fd4179SDavid Teigland 
dlm_clear_inactive(struct dlm_ls * ls)8864f5957a9SDavid Teigland void dlm_clear_inactive(struct dlm_ls *ls)
887e7fd4179SDavid Teigland {
88893a693d1SAlexander Aring 	struct dlm_rsb *r, *safe;
889c04fecb4SDavid Teigland 	unsigned int count = 0;
890e7fd4179SDavid Teigland 
891e9131359SAlexander Aring 	write_lock_bh(&ls->ls_rsbtbl_lock);
8924f5957a9SDavid Teigland 	list_for_each_entry_safe(r, safe, &ls->ls_slow_inactive, res_slow_list) {
8934f5957a9SDavid Teigland 		list_del(&r->res_slow_list);
8946c648035SAlexander Aring 		rhashtable_remove_fast(&ls->ls_rsbtbl, &r->res_node,
8956c648035SAlexander Aring 				       dlm_rhash_rsb_params);
896b1f2381cSAlexander Aring 
8974f5957a9SDavid Teigland 		if (!list_empty(&r->res_scan_list))
8984f5957a9SDavid Teigland 			list_del_init(&r->res_scan_list);
899b1f2381cSAlexander Aring 
9004f5957a9SDavid Teigland 		free_inactive_rsb(r);
901c04fecb4SDavid Teigland 		count++;
90285f0379aSDavid Teigland 	}
903e9131359SAlexander Aring 	write_unlock_bh(&ls->ls_rsbtbl_lock);
904c04fecb4SDavid Teigland 
905c04fecb4SDavid Teigland 	if (count)
9064f5957a9SDavid Teigland 		log_rinfo(ls, "dlm_clear_inactive %u done", count);
907e7fd4179SDavid Teigland }
908e7fd4179SDavid Teigland 
909