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;
77e7fd4179SDavid Teigland spin_lock(&ls->ls_recover_lock);
78e7fd4179SDavid Teigland status = ls->ls_recover_status;
79e7fd4179SDavid Teigland spin_unlock(&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 {
90e7fd4179SDavid Teigland spin_lock(&ls->ls_recover_lock);
91757a4271SDavid Teigland _set_recover_status(ls, status);
92e7fd4179SDavid Teigland spin_unlock(&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) {
191757a4271SDavid Teigland spin_lock(&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;
197757a4271SDavid Teigland spin_unlock(&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
244e7fd4179SDavid Teigland spin_lock(&ls->ls_recover_list_lock);
245e7fd4179SDavid Teigland empty = list_empty(&ls->ls_recover_list);
246e7fd4179SDavid Teigland spin_unlock(&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
255e7fd4179SDavid Teigland spin_lock(&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 }
261e7fd4179SDavid Teigland spin_unlock(&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
268e7fd4179SDavid Teigland spin_lock(&ls->ls_recover_list_lock);
269e7fd4179SDavid Teigland list_del_init(&r->res_recover_list);
270e7fd4179SDavid Teigland ls->ls_recover_list_count--;
271e7fd4179SDavid Teigland spin_unlock(&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
280e7fd4179SDavid Teigland spin_lock(&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 }
293e7fd4179SDavid Teigland spin_unlock(&ls->ls_recover_list_lock);
294e7fd4179SDavid Teigland }
295e7fd4179SDavid Teigland
recover_idr_empty(struct dlm_ls * ls)2961d7c484eSDavid Teigland static int recover_idr_empty(struct dlm_ls *ls)
2971d7c484eSDavid Teigland {
2981d7c484eSDavid Teigland int empty = 1;
2991d7c484eSDavid Teigland
3001d7c484eSDavid Teigland spin_lock(&ls->ls_recover_idr_lock);
3011d7c484eSDavid Teigland if (ls->ls_recover_list_count)
3021d7c484eSDavid Teigland empty = 0;
3031d7c484eSDavid Teigland spin_unlock(&ls->ls_recover_idr_lock);
3041d7c484eSDavid Teigland
3051d7c484eSDavid Teigland return empty;
3061d7c484eSDavid Teigland }
3071d7c484eSDavid Teigland
recover_idr_add(struct dlm_rsb * r)3081d7c484eSDavid Teigland static int recover_idr_add(struct dlm_rsb *r)
3091d7c484eSDavid Teigland {
3101d7c484eSDavid Teigland struct dlm_ls *ls = r->res_ls;
3112a86b3e7STejun Heo int rv;
3121d7c484eSDavid Teigland
3132a86b3e7STejun Heo idr_preload(GFP_NOFS);
3141d7c484eSDavid Teigland spin_lock(&ls->ls_recover_idr_lock);
3151d7c484eSDavid Teigland if (r->res_id) {
3162a86b3e7STejun Heo rv = -1;
3172a86b3e7STejun Heo goto out_unlock;
3181d7c484eSDavid Teigland }
3192a86b3e7STejun Heo rv = idr_alloc(&ls->ls_recover_idr, r, 1, 0, GFP_NOWAIT);
3202a86b3e7STejun Heo if (rv < 0)
3212a86b3e7STejun Heo goto out_unlock;
3222a86b3e7STejun Heo
3232a86b3e7STejun Heo r->res_id = rv;
3241d7c484eSDavid Teigland ls->ls_recover_list_count++;
3251d7c484eSDavid Teigland dlm_hold_rsb(r);
3262a86b3e7STejun Heo rv = 0;
3272a86b3e7STejun Heo out_unlock:
3281d7c484eSDavid Teigland spin_unlock(&ls->ls_recover_idr_lock);
3292a86b3e7STejun Heo idr_preload_end();
3302a86b3e7STejun Heo return rv;
3311d7c484eSDavid Teigland }
3321d7c484eSDavid Teigland
recover_idr_del(struct dlm_rsb * r)3331d7c484eSDavid Teigland static void recover_idr_del(struct dlm_rsb *r)
3341d7c484eSDavid Teigland {
3351d7c484eSDavid Teigland struct dlm_ls *ls = r->res_ls;
3361d7c484eSDavid Teigland
3371d7c484eSDavid Teigland spin_lock(&ls->ls_recover_idr_lock);
3381d7c484eSDavid Teigland idr_remove(&ls->ls_recover_idr, r->res_id);
3391d7c484eSDavid Teigland r->res_id = 0;
3401d7c484eSDavid Teigland ls->ls_recover_list_count--;
3411d7c484eSDavid Teigland spin_unlock(&ls->ls_recover_idr_lock);
3421d7c484eSDavid Teigland
3431d7c484eSDavid Teigland dlm_put_rsb(r);
3441d7c484eSDavid Teigland }
3451d7c484eSDavid Teigland
recover_idr_find(struct dlm_ls * ls,uint64_t id)3461d7c484eSDavid Teigland static struct dlm_rsb *recover_idr_find(struct dlm_ls *ls, uint64_t id)
3471d7c484eSDavid Teigland {
3481d7c484eSDavid Teigland struct dlm_rsb *r;
3491d7c484eSDavid Teigland
3501d7c484eSDavid Teigland spin_lock(&ls->ls_recover_idr_lock);
3511d7c484eSDavid Teigland r = idr_find(&ls->ls_recover_idr, (int)id);
3521d7c484eSDavid Teigland spin_unlock(&ls->ls_recover_idr_lock);
3531d7c484eSDavid Teigland return r;
3541d7c484eSDavid Teigland }
3551d7c484eSDavid Teigland
recover_idr_clear(struct dlm_ls * ls)356cda95406STejun Heo static void recover_idr_clear(struct dlm_ls *ls)
3571d7c484eSDavid Teigland {
358cda95406STejun Heo struct dlm_rsb *r;
359cda95406STejun Heo int id;
3601d7c484eSDavid Teigland
361cda95406STejun Heo spin_lock(&ls->ls_recover_idr_lock);
362cda95406STejun Heo
363cda95406STejun Heo idr_for_each_entry(&ls->ls_recover_idr, r, id) {
364a67a380eSTejun Heo idr_remove(&ls->ls_recover_idr, id);
3651d7c484eSDavid Teigland r->res_id = 0;
3661d7c484eSDavid Teigland r->res_recover_locks_count = 0;
3671d7c484eSDavid Teigland ls->ls_recover_list_count--;
3681d7c484eSDavid Teigland
3691d7c484eSDavid Teigland dlm_put_rsb(r);
3701d7c484eSDavid Teigland }
3711d7c484eSDavid Teigland
3721d7c484eSDavid Teigland if (ls->ls_recover_list_count != 0) {
3731d7c484eSDavid Teigland log_error(ls, "warning: recover_list_count %d",
3741d7c484eSDavid Teigland ls->ls_recover_list_count);
3751d7c484eSDavid Teigland ls->ls_recover_list_count = 0;
3761d7c484eSDavid Teigland }
3771d7c484eSDavid Teigland spin_unlock(&ls->ls_recover_idr_lock);
3781d7c484eSDavid Teigland }
3791d7c484eSDavid Teigland
380e7fd4179SDavid Teigland
381e7fd4179SDavid Teigland /* Master recovery: find new master node for rsb's that were
382e7fd4179SDavid Teigland mastered on nodes that have been removed.
383e7fd4179SDavid Teigland
384e7fd4179SDavid Teigland dlm_recover_masters
385e7fd4179SDavid Teigland recover_master
386e7fd4179SDavid Teigland dlm_send_rcom_lookup -> receive_rcom_lookup
387e7fd4179SDavid Teigland dlm_dir_lookup
388e7fd4179SDavid Teigland receive_rcom_lookup_reply <-
389e7fd4179SDavid Teigland dlm_recover_master_reply
390e7fd4179SDavid Teigland set_new_master
391e7fd4179SDavid Teigland set_master_lkbs
392e7fd4179SDavid Teigland set_lock_master
393e7fd4179SDavid Teigland */
394e7fd4179SDavid Teigland
395e7fd4179SDavid Teigland /*
396e7fd4179SDavid Teigland * Set the lock master for all LKBs in a lock queue
397e7fd4179SDavid Teigland * If we are the new master of the rsb, we may have received new
398e7fd4179SDavid Teigland * MSTCPY locks from other nodes already which we need to ignore
399e7fd4179SDavid Teigland * when setting the new nodeid.
400e7fd4179SDavid Teigland */
401e7fd4179SDavid Teigland
set_lock_master(struct list_head * queue,int nodeid)402e7fd4179SDavid Teigland static void set_lock_master(struct list_head *queue, int nodeid)
403e7fd4179SDavid Teigland {
404e7fd4179SDavid Teigland struct dlm_lkb *lkb;
405e7fd4179SDavid Teigland
4064875647aSDavid Teigland list_for_each_entry(lkb, queue, lkb_statequeue) {
407e1af8728SAlexander Aring if (!test_bit(DLM_IFL_MSTCPY_BIT, &lkb->lkb_iflags)) {
408e7fd4179SDavid Teigland lkb->lkb_nodeid = nodeid;
4094875647aSDavid Teigland lkb->lkb_remid = 0;
4104875647aSDavid Teigland }
4114875647aSDavid Teigland }
412e7fd4179SDavid Teigland }
413e7fd4179SDavid Teigland
set_master_lkbs(struct dlm_rsb * r)414e7fd4179SDavid Teigland static void set_master_lkbs(struct dlm_rsb *r)
415e7fd4179SDavid Teigland {
416e7fd4179SDavid Teigland set_lock_master(&r->res_grantqueue, r->res_nodeid);
417e7fd4179SDavid Teigland set_lock_master(&r->res_convertqueue, r->res_nodeid);
418e7fd4179SDavid Teigland set_lock_master(&r->res_waitqueue, r->res_nodeid);
419e7fd4179SDavid Teigland }
420e7fd4179SDavid Teigland
421e7fd4179SDavid Teigland /*
42225985edcSLucas De Marchi * Propagate the new master nodeid to locks
423e7fd4179SDavid Teigland * The NEW_MASTER flag tells dlm_recover_locks() which rsb's to consider.
4244875647aSDavid Teigland * The NEW_MASTER2 flag tells recover_lvb() and recover_grant() which
425f7da790dSDavid Teigland * rsb's to consider.
426e7fd4179SDavid Teigland */
427e7fd4179SDavid Teigland
set_new_master(struct dlm_rsb * r)428c04fecb4SDavid Teigland static void set_new_master(struct dlm_rsb *r)
429e7fd4179SDavid Teigland {
430e7fd4179SDavid Teigland set_master_lkbs(r);
431e7fd4179SDavid Teigland rsb_set_flag(r, RSB_NEW_MASTER);
432e7fd4179SDavid Teigland rsb_set_flag(r, RSB_NEW_MASTER2);
433e7fd4179SDavid Teigland }
434e7fd4179SDavid Teigland
435e7fd4179SDavid Teigland /*
436e7fd4179SDavid Teigland * We do async lookups on rsb's that need new masters. The rsb's
437e7fd4179SDavid Teigland * waiting for a lookup reply are kept on the recover_list.
438c04fecb4SDavid Teigland *
439c04fecb4SDavid Teigland * Another node recovering the master may have sent us a rcom lookup,
440c04fecb4SDavid Teigland * and our dlm_master_lookup() set it as the new master, along with
441c04fecb4SDavid Teigland * NEW_MASTER so that we'll recover it here (this implies dir_nodeid
442c04fecb4SDavid Teigland * equals our_nodeid below).
443e7fd4179SDavid Teigland */
444e7fd4179SDavid Teigland
recover_master(struct dlm_rsb * r,unsigned int * count,uint64_t seq)445c4f4e135SAlexander Aring static int recover_master(struct dlm_rsb *r, unsigned int *count, uint64_t seq)
446e7fd4179SDavid Teigland {
447e7fd4179SDavid Teigland struct dlm_ls *ls = r->res_ls;
448c04fecb4SDavid Teigland int our_nodeid, dir_nodeid;
449c04fecb4SDavid Teigland int is_removed = 0;
450c04fecb4SDavid Teigland int error;
451c04fecb4SDavid Teigland
452c04fecb4SDavid Teigland if (is_master(r))
453c04fecb4SDavid Teigland return 0;
454c04fecb4SDavid Teigland
455c04fecb4SDavid Teigland is_removed = dlm_is_removed(ls, r->res_nodeid);
456c04fecb4SDavid Teigland
457c04fecb4SDavid Teigland if (!is_removed && !rsb_flag(r, RSB_NEW_MASTER))
458c04fecb4SDavid Teigland return 0;
459c04fecb4SDavid Teigland
460c04fecb4SDavid Teigland our_nodeid = dlm_our_nodeid();
461c04fecb4SDavid Teigland dir_nodeid = dlm_dir_nodeid(r);
462e7fd4179SDavid Teigland
463e7fd4179SDavid Teigland if (dir_nodeid == our_nodeid) {
464c04fecb4SDavid Teigland if (is_removed) {
465c04fecb4SDavid Teigland r->res_master_nodeid = our_nodeid;
466c04fecb4SDavid Teigland r->res_nodeid = 0;
467c04fecb4SDavid Teigland }
468e7fd4179SDavid Teigland
469c04fecb4SDavid Teigland /* set master of lkbs to ourself when is_removed, or to
470c04fecb4SDavid Teigland another new master which we set along with NEW_MASTER
471c04fecb4SDavid Teigland in dlm_master_lookup */
472c04fecb4SDavid Teigland set_new_master(r);
473c04fecb4SDavid Teigland error = 0;
474e7fd4179SDavid Teigland } else {
4751d7c484eSDavid Teigland recover_idr_add(r);
476c4f4e135SAlexander Aring error = dlm_send_rcom_lookup(r, dir_nodeid, seq);
477e7fd4179SDavid Teigland }
478e7fd4179SDavid Teigland
479c04fecb4SDavid Teigland (*count)++;
480e7fd4179SDavid Teigland return error;
481e7fd4179SDavid Teigland }
482e7fd4179SDavid Teigland
483e7fd4179SDavid Teigland /*
4844875647aSDavid Teigland * All MSTCPY locks are purged and rebuilt, even if the master stayed the same.
4854875647aSDavid Teigland * This is necessary because recovery can be started, aborted and restarted,
4864875647aSDavid Teigland * causing the master nodeid to briefly change during the aborted recovery, and
4874875647aSDavid Teigland * change back to the original value in the second recovery. The MSTCPY locks
4884875647aSDavid Teigland * may or may not have been purged during the aborted recovery. Another node
4894875647aSDavid Teigland * with an outstanding request in waiters list and a request reply saved in the
4904875647aSDavid Teigland * requestqueue, cannot know whether it should ignore the reply and resend the
4914875647aSDavid Teigland * request, or accept the reply and complete the request. It must do the
4924875647aSDavid Teigland * former if the remote node purged MSTCPY locks, and it must do the later if
4934875647aSDavid Teigland * the remote node did not. This is solved by always purging MSTCPY locks, in
4944875647aSDavid Teigland * which case, the request reply would always be ignored and the request
4954875647aSDavid Teigland * resent.
496e7fd4179SDavid Teigland */
497e7fd4179SDavid Teigland
recover_master_static(struct dlm_rsb * r,unsigned int * count)498c04fecb4SDavid Teigland static int recover_master_static(struct dlm_rsb *r, unsigned int *count)
499e7fd4179SDavid Teigland {
5004875647aSDavid Teigland int dir_nodeid = dlm_dir_nodeid(r);
5014875647aSDavid Teigland int new_master = dir_nodeid;
502e7fd4179SDavid Teigland
5034875647aSDavid Teigland if (dir_nodeid == dlm_our_nodeid())
5044875647aSDavid Teigland new_master = 0;
505e7fd4179SDavid Teigland
506e7fd4179SDavid Teigland dlm_purge_mstcpy_locks(r);
507c04fecb4SDavid Teigland r->res_master_nodeid = dir_nodeid;
508c04fecb4SDavid Teigland r->res_nodeid = new_master;
509c04fecb4SDavid Teigland set_new_master(r);
510c04fecb4SDavid Teigland (*count)++;
511c04fecb4SDavid Teigland return 0;
512e7fd4179SDavid Teigland }
513e7fd4179SDavid Teigland
514e7fd4179SDavid Teigland /*
515e7fd4179SDavid Teigland * Go through local root resources and for each rsb which has a master which
516e7fd4179SDavid Teigland * has departed, get the new master nodeid from the directory. The dir will
517e7fd4179SDavid Teigland * assign mastery to the first node to look up the new master. That means
518e7fd4179SDavid Teigland * we'll discover in this lookup if we're the new master of any rsb's.
519e7fd4179SDavid Teigland *
520e7fd4179SDavid Teigland * We fire off all the dir lookup requests individually and asynchronously to
521e7fd4179SDavid Teigland * the correct dir node.
522e7fd4179SDavid Teigland */
523e7fd4179SDavid Teigland
dlm_recover_masters(struct dlm_ls * ls,uint64_t seq)524c4f4e135SAlexander Aring int dlm_recover_masters(struct dlm_ls *ls, uint64_t seq)
525e7fd4179SDavid Teigland {
526e7fd4179SDavid Teigland struct dlm_rsb *r;
527c04fecb4SDavid Teigland unsigned int total = 0;
528c04fecb4SDavid Teigland unsigned int count = 0;
529c04fecb4SDavid Teigland int nodir = dlm_no_directory(ls);
530c04fecb4SDavid Teigland int error;
531e7fd4179SDavid Teigland
532075f0177SDavid Teigland log_rinfo(ls, "dlm_recover_masters");
533e7fd4179SDavid Teigland
534e7fd4179SDavid Teigland down_read(&ls->ls_root_sem);
535e7fd4179SDavid Teigland list_for_each_entry(r, &ls->ls_root_list, res_root_list) {
536e7fd4179SDavid Teigland if (dlm_recovery_stopped(ls)) {
537e7fd4179SDavid Teigland up_read(&ls->ls_root_sem);
538e7fd4179SDavid Teigland error = -EINTR;
539e7fd4179SDavid Teigland goto out;
540e7fd4179SDavid Teigland }
541e7fd4179SDavid Teigland
542c04fecb4SDavid Teigland lock_rsb(r);
543c04fecb4SDavid Teigland if (nodir)
544c04fecb4SDavid Teigland error = recover_master_static(r, &count);
545c04fecb4SDavid Teigland else
546c4f4e135SAlexander Aring error = recover_master(r, &count, seq);
547c04fecb4SDavid Teigland unlock_rsb(r);
548c04fecb4SDavid Teigland cond_resched();
549c04fecb4SDavid Teigland total++;
550e7fd4179SDavid Teigland
551c04fecb4SDavid Teigland if (error) {
552c04fecb4SDavid Teigland up_read(&ls->ls_root_sem);
553c04fecb4SDavid Teigland goto out;
554c04fecb4SDavid Teigland }
555e7fd4179SDavid Teigland }
556e7fd4179SDavid Teigland up_read(&ls->ls_root_sem);
557e7fd4179SDavid Teigland
558075f0177SDavid Teigland log_rinfo(ls, "dlm_recover_masters %u of %u", count, total);
559e7fd4179SDavid Teigland
5601d7c484eSDavid Teigland error = dlm_wait_function(ls, &recover_idr_empty);
561e7fd4179SDavid Teigland out:
562e7fd4179SDavid Teigland if (error)
5631d7c484eSDavid Teigland recover_idr_clear(ls);
564e7fd4179SDavid Teigland return error;
565e7fd4179SDavid Teigland }
566e7fd4179SDavid Teigland
dlm_recover_master_reply(struct dlm_ls * ls,const struct dlm_rcom * rc)567*11519351SAlexander 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
5722f9dbedaSAlexander Aring r = recover_idr_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);
5911d7c484eSDavid Teigland recover_idr_del(r);
592e7fd4179SDavid Teigland
5931d7c484eSDavid Teigland if (recover_idr_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)661c4f4e135SAlexander Aring int dlm_recover_locks(struct dlm_ls *ls, uint64_t seq)
662e7fd4179SDavid Teigland {
663e7fd4179SDavid Teigland struct dlm_rsb *r;
664e7fd4179SDavid Teigland int error, count = 0;
665e7fd4179SDavid Teigland
666e7fd4179SDavid Teigland down_read(&ls->ls_root_sem);
667e7fd4179SDavid Teigland list_for_each_entry(r, &ls->ls_root_list, res_root_list) {
668e7fd4179SDavid Teigland if (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 up_read(&ls->ls_root_sem);
679e7fd4179SDavid Teigland goto out;
680e7fd4179SDavid Teigland }
681e7fd4179SDavid Teigland
682c4f4e135SAlexander Aring error = recover_locks(r, seq);
683e7fd4179SDavid Teigland if (error) {
684e7fd4179SDavid Teigland up_read(&ls->ls_root_sem);
685e7fd4179SDavid Teigland goto out;
686e7fd4179SDavid Teigland }
687e7fd4179SDavid Teigland
688e7fd4179SDavid Teigland count += r->res_recover_locks_count;
689e7fd4179SDavid Teigland }
690e7fd4179SDavid Teigland up_read(&ls->ls_root_sem);
691e7fd4179SDavid Teigland
692075f0177SDavid Teigland log_rinfo(ls, "dlm_recover_locks %d out", count);
693e7fd4179SDavid Teigland
694e7fd4179SDavid Teigland error = dlm_wait_function(ls, &recover_list_empty);
695e7fd4179SDavid Teigland out:
696e7fd4179SDavid Teigland if (error)
697e7fd4179SDavid Teigland recover_list_clear(ls);
698e7fd4179SDavid Teigland return error;
699e7fd4179SDavid Teigland }
700e7fd4179SDavid Teigland
dlm_recovered_lock(struct dlm_rsb * r)701e7fd4179SDavid Teigland void dlm_recovered_lock(struct dlm_rsb *r)
702e7fd4179SDavid Teigland {
703a345da3eSDavid Teigland DLM_ASSERT(rsb_flag(r, RSB_NEW_MASTER), dlm_dump_rsb(r););
704e7fd4179SDavid Teigland
705e7fd4179SDavid Teigland r->res_recover_locks_count--;
706e7fd4179SDavid Teigland if (!r->res_recover_locks_count) {
707e7fd4179SDavid Teigland rsb_clear_flag(r, RSB_NEW_MASTER);
708e7fd4179SDavid Teigland recover_list_del(r);
709e7fd4179SDavid Teigland }
710e7fd4179SDavid Teigland
711e7fd4179SDavid Teigland if (recover_list_empty(r->res_ls))
712e7fd4179SDavid Teigland wake_up(&r->res_ls->ls_wait_general);
713e7fd4179SDavid Teigland }
714e7fd4179SDavid Teigland
715e7fd4179SDavid Teigland /*
716e7fd4179SDavid Teigland * The lvb needs to be recovered on all master rsb's. This includes setting
717e7fd4179SDavid Teigland * the VALNOTVALID flag if necessary, and determining the correct lvb contents
718e7fd4179SDavid Teigland * based on the lvb's of the locks held on the rsb.
719e7fd4179SDavid Teigland *
720da8c6663SDavid Teigland * RSB_VALNOTVALID is set in two cases:
721da8c6663SDavid Teigland *
722da8c6663SDavid Teigland * 1. we are master, but not new, and we purged an EX/PW lock held by a
723da8c6663SDavid Teigland * failed node (in dlm_recover_purge which set RSB_RECOVER_LVB_INVAL)
724da8c6663SDavid Teigland *
725da8c6663SDavid Teigland * 2. we are a new master, and there are only NL/CR locks left.
726da8c6663SDavid Teigland * (We could probably improve this by only invaliding in this way when
727da8c6663SDavid Teigland * the previous master left uncleanly. VMS docs mention that.)
728e7fd4179SDavid Teigland *
729e7fd4179SDavid Teigland * The LVB contents are only considered for changing when this is a new master
730e7fd4179SDavid Teigland * of the rsb (NEW_MASTER2). Then, the rsb's lvb is taken from any lkb with
731e7fd4179SDavid Teigland * mode > CR. If no lkb's exist with mode above CR, the lvb contents are taken
732e7fd4179SDavid Teigland * from the lkb with the largest lvb sequence number.
733e7fd4179SDavid Teigland */
734e7fd4179SDavid Teigland
recover_lvb(struct dlm_rsb * r)735e7fd4179SDavid Teigland static void recover_lvb(struct dlm_rsb *r)
736e7fd4179SDavid Teigland {
737dc1acd5cSJakob Koschel struct dlm_lkb *big_lkb = NULL, *iter, *high_lkb = NULL;
738e7fd4179SDavid Teigland uint32_t high_seq = 0;
73990135925SDavid Teigland int lock_lvb_exists = 0;
740e7fd4179SDavid Teigland int lvblen = r->res_ls->ls_lvblen;
741e7fd4179SDavid Teigland
742da8c6663SDavid Teigland if (!rsb_flag(r, RSB_NEW_MASTER2) &&
743da8c6663SDavid Teigland rsb_flag(r, RSB_RECOVER_LVB_INVAL)) {
744da8c6663SDavid Teigland /* case 1 above */
745da8c6663SDavid Teigland rsb_set_flag(r, RSB_VALNOTVALID);
746da8c6663SDavid Teigland return;
747da8c6663SDavid Teigland }
748da8c6663SDavid Teigland
749da8c6663SDavid Teigland if (!rsb_flag(r, RSB_NEW_MASTER2))
750da8c6663SDavid Teigland return;
751da8c6663SDavid Teigland
752da8c6663SDavid Teigland /* we are the new master, so figure out if VALNOTVALID should
753da8c6663SDavid Teigland be set, and set the rsb lvb from the best lkb available. */
754da8c6663SDavid Teigland
755dc1acd5cSJakob Koschel list_for_each_entry(iter, &r->res_grantqueue, lkb_statequeue) {
756dc1acd5cSJakob Koschel if (!(iter->lkb_exflags & DLM_LKF_VALBLK))
757e7fd4179SDavid Teigland continue;
758e7fd4179SDavid Teigland
75990135925SDavid Teigland lock_lvb_exists = 1;
760e7fd4179SDavid Teigland
761dc1acd5cSJakob Koschel if (iter->lkb_grmode > DLM_LOCK_CR) {
762dc1acd5cSJakob Koschel big_lkb = iter;
763e7fd4179SDavid Teigland goto setflag;
764e7fd4179SDavid Teigland }
765e7fd4179SDavid Teigland
766dc1acd5cSJakob Koschel if (((int)iter->lkb_lvbseq - (int)high_seq) >= 0) {
767dc1acd5cSJakob Koschel high_lkb = iter;
768dc1acd5cSJakob Koschel high_seq = iter->lkb_lvbseq;
769e7fd4179SDavid Teigland }
770e7fd4179SDavid Teigland }
771e7fd4179SDavid Teigland
772dc1acd5cSJakob Koschel list_for_each_entry(iter, &r->res_convertqueue, lkb_statequeue) {
773dc1acd5cSJakob Koschel if (!(iter->lkb_exflags & DLM_LKF_VALBLK))
774e7fd4179SDavid Teigland continue;
775e7fd4179SDavid Teigland
77690135925SDavid Teigland lock_lvb_exists = 1;
777e7fd4179SDavid Teigland
778dc1acd5cSJakob Koschel if (iter->lkb_grmode > DLM_LOCK_CR) {
779dc1acd5cSJakob Koschel big_lkb = iter;
780e7fd4179SDavid Teigland goto setflag;
781e7fd4179SDavid Teigland }
782e7fd4179SDavid Teigland
783dc1acd5cSJakob Koschel if (((int)iter->lkb_lvbseq - (int)high_seq) >= 0) {
784dc1acd5cSJakob Koschel high_lkb = iter;
785dc1acd5cSJakob Koschel high_seq = iter->lkb_lvbseq;
786e7fd4179SDavid Teigland }
787e7fd4179SDavid Teigland }
788e7fd4179SDavid Teigland
789e7fd4179SDavid Teigland setflag:
790e7fd4179SDavid Teigland if (!lock_lvb_exists)
791e7fd4179SDavid Teigland goto out;
792e7fd4179SDavid Teigland
793da8c6663SDavid Teigland /* lvb is invalidated if only NL/CR locks remain */
794dc1acd5cSJakob Koschel if (!big_lkb)
795e7fd4179SDavid Teigland rsb_set_flag(r, RSB_VALNOTVALID);
796e7fd4179SDavid Teigland
797e7fd4179SDavid Teigland if (!r->res_lvbptr) {
79852bda2b5SDavid Teigland r->res_lvbptr = dlm_allocate_lvb(r->res_ls);
799e7fd4179SDavid Teigland if (!r->res_lvbptr)
800e7fd4179SDavid Teigland goto out;
801e7fd4179SDavid Teigland }
802e7fd4179SDavid Teigland
803dc1acd5cSJakob Koschel if (big_lkb) {
804dc1acd5cSJakob Koschel r->res_lvbseq = big_lkb->lkb_lvbseq;
805dc1acd5cSJakob Koschel memcpy(r->res_lvbptr, big_lkb->lkb_lvbptr, lvblen);
806e7fd4179SDavid Teigland } else if (high_lkb) {
807e7fd4179SDavid Teigland r->res_lvbseq = high_lkb->lkb_lvbseq;
808e7fd4179SDavid Teigland memcpy(r->res_lvbptr, high_lkb->lkb_lvbptr, lvblen);
809e7fd4179SDavid Teigland } else {
810e7fd4179SDavid Teigland r->res_lvbseq = 0;
811e7fd4179SDavid Teigland memset(r->res_lvbptr, 0, lvblen);
812e7fd4179SDavid Teigland }
813e7fd4179SDavid Teigland out:
814e7fd4179SDavid Teigland return;
815e7fd4179SDavid Teigland }
816e7fd4179SDavid Teigland
817e7fd4179SDavid Teigland /* All master rsb's flagged RECOVER_CONVERT need to be looked at. The locks
818e7fd4179SDavid Teigland converting PR->CW or CW->PR need to have their lkb_grmode set. */
819e7fd4179SDavid Teigland
recover_conversion(struct dlm_rsb * r)820e7fd4179SDavid Teigland static void recover_conversion(struct dlm_rsb *r)
821e7fd4179SDavid Teigland {
822c503a621SDavid Teigland struct dlm_ls *ls = r->res_ls;
823e7fd4179SDavid Teigland struct dlm_lkb *lkb;
824e7fd4179SDavid Teigland int grmode = -1;
825e7fd4179SDavid Teigland
826e7fd4179SDavid Teigland list_for_each_entry(lkb, &r->res_grantqueue, lkb_statequeue) {
827e7fd4179SDavid Teigland if (lkb->lkb_grmode == DLM_LOCK_PR ||
828e7fd4179SDavid Teigland lkb->lkb_grmode == DLM_LOCK_CW) {
829e7fd4179SDavid Teigland grmode = lkb->lkb_grmode;
830e7fd4179SDavid Teigland break;
831e7fd4179SDavid Teigland }
832e7fd4179SDavid Teigland }
833e7fd4179SDavid Teigland
834e7fd4179SDavid Teigland list_for_each_entry(lkb, &r->res_convertqueue, lkb_statequeue) {
835e7fd4179SDavid Teigland if (lkb->lkb_grmode != DLM_LOCK_IV)
836e7fd4179SDavid Teigland continue;
837c503a621SDavid Teigland if (grmode == -1) {
838c503a621SDavid Teigland log_debug(ls, "recover_conversion %x set gr to rq %d",
839c503a621SDavid Teigland lkb->lkb_id, lkb->lkb_rqmode);
840e7fd4179SDavid Teigland lkb->lkb_grmode = lkb->lkb_rqmode;
841c503a621SDavid Teigland } else {
842c503a621SDavid Teigland log_debug(ls, "recover_conversion %x set gr %d",
843c503a621SDavid Teigland lkb->lkb_id, grmode);
844e7fd4179SDavid Teigland lkb->lkb_grmode = grmode;
845e7fd4179SDavid Teigland }
846e7fd4179SDavid Teigland }
847c503a621SDavid Teigland }
848e7fd4179SDavid Teigland
849f7da790dSDavid Teigland /* We've become the new master for this rsb and waiting/converting locks may
8504875647aSDavid Teigland need to be granted in dlm_recover_grant() due to locks that may have
851f7da790dSDavid Teigland existed from a removed node. */
852f7da790dSDavid Teigland
recover_grant(struct dlm_rsb * r)8534875647aSDavid Teigland static void recover_grant(struct dlm_rsb *r)
854f7da790dSDavid Teigland {
855f7da790dSDavid Teigland if (!list_empty(&r->res_waitqueue) || !list_empty(&r->res_convertqueue))
8564875647aSDavid Teigland rsb_set_flag(r, RSB_RECOVER_GRANT);
857f7da790dSDavid Teigland }
858f7da790dSDavid Teigland
dlm_recover_rsbs(struct dlm_ls * ls)859e7fd4179SDavid Teigland void dlm_recover_rsbs(struct dlm_ls *ls)
860e7fd4179SDavid Teigland {
861e7fd4179SDavid Teigland struct dlm_rsb *r;
8624875647aSDavid Teigland unsigned int count = 0;
863e7fd4179SDavid Teigland
864e7fd4179SDavid Teigland down_read(&ls->ls_root_sem);
865e7fd4179SDavid Teigland list_for_each_entry(r, &ls->ls_root_list, res_root_list) {
866e7fd4179SDavid Teigland lock_rsb(r);
867e7fd4179SDavid Teigland if (is_master(r)) {
868e7fd4179SDavid Teigland if (rsb_flag(r, RSB_RECOVER_CONVERT))
869e7fd4179SDavid Teigland recover_conversion(r);
870da8c6663SDavid Teigland
871da8c6663SDavid Teigland /* recover lvb before granting locks so the updated
872da8c6663SDavid Teigland lvb/VALNOTVALID is presented in the completion */
873da8c6663SDavid Teigland recover_lvb(r);
874da8c6663SDavid Teigland
875f7da790dSDavid Teigland if (rsb_flag(r, RSB_NEW_MASTER2))
8764875647aSDavid Teigland recover_grant(r);
877e7fd4179SDavid Teigland count++;
878da8c6663SDavid Teigland } else {
879da8c6663SDavid Teigland rsb_clear_flag(r, RSB_VALNOTVALID);
880e7fd4179SDavid Teigland }
881e7fd4179SDavid Teigland rsb_clear_flag(r, RSB_RECOVER_CONVERT);
882da8c6663SDavid Teigland rsb_clear_flag(r, RSB_RECOVER_LVB_INVAL);
883f7da790dSDavid Teigland rsb_clear_flag(r, RSB_NEW_MASTER2);
884e7fd4179SDavid Teigland unlock_rsb(r);
885e7fd4179SDavid Teigland }
886e7fd4179SDavid Teigland up_read(&ls->ls_root_sem);
887e7fd4179SDavid Teigland
8884875647aSDavid Teigland if (count)
889075f0177SDavid Teigland log_rinfo(ls, "dlm_recover_rsbs %d done", count);
890e7fd4179SDavid Teigland }
891e7fd4179SDavid Teigland
892e7fd4179SDavid Teigland /* Create a single list of all root rsb's to be used during recovery */
893e7fd4179SDavid Teigland
dlm_create_root_list(struct dlm_ls * ls)894e7fd4179SDavid Teigland int dlm_create_root_list(struct dlm_ls *ls)
895e7fd4179SDavid Teigland {
8969beb3bf5SBob Peterson struct rb_node *n;
897e7fd4179SDavid Teigland struct dlm_rsb *r;
898e7fd4179SDavid Teigland int i, error = 0;
899e7fd4179SDavid Teigland
900e7fd4179SDavid Teigland down_write(&ls->ls_root_sem);
901e7fd4179SDavid Teigland if (!list_empty(&ls->ls_root_list)) {
902e7fd4179SDavid Teigland log_error(ls, "root list not empty");
903e7fd4179SDavid Teigland error = -EINVAL;
904e7fd4179SDavid Teigland goto out;
905e7fd4179SDavid Teigland }
906e7fd4179SDavid Teigland
907e7fd4179SDavid Teigland for (i = 0; i < ls->ls_rsbtbl_size; i++) {
908c7be761aSDavid Teigland spin_lock(&ls->ls_rsbtbl[i].lock);
9099beb3bf5SBob Peterson for (n = rb_first(&ls->ls_rsbtbl[i].keep); n; n = rb_next(n)) {
9109beb3bf5SBob Peterson r = rb_entry(n, struct dlm_rsb, res_hashnode);
911e7fd4179SDavid Teigland list_add(&r->res_root_list, &ls->ls_root_list);
912e7fd4179SDavid Teigland dlm_hold_rsb(r);
913e7fd4179SDavid Teigland }
91485f0379aSDavid Teigland
915c04fecb4SDavid Teigland if (!RB_EMPTY_ROOT(&ls->ls_rsbtbl[i].toss))
916c04fecb4SDavid Teigland log_error(ls, "dlm_create_root_list toss not empty");
917c7be761aSDavid Teigland spin_unlock(&ls->ls_rsbtbl[i].lock);
918e7fd4179SDavid Teigland }
919e7fd4179SDavid Teigland out:
920e7fd4179SDavid Teigland up_write(&ls->ls_root_sem);
921e7fd4179SDavid Teigland return error;
922e7fd4179SDavid Teigland }
923e7fd4179SDavid Teigland
dlm_release_root_list(struct dlm_ls * ls)924e7fd4179SDavid Teigland void dlm_release_root_list(struct dlm_ls *ls)
925e7fd4179SDavid Teigland {
926e7fd4179SDavid Teigland struct dlm_rsb *r, *safe;
927e7fd4179SDavid Teigland
928e7fd4179SDavid Teigland down_write(&ls->ls_root_sem);
929e7fd4179SDavid Teigland list_for_each_entry_safe(r, safe, &ls->ls_root_list, res_root_list) {
930e7fd4179SDavid Teigland list_del_init(&r->res_root_list);
931e7fd4179SDavid Teigland dlm_put_rsb(r);
932e7fd4179SDavid Teigland }
933e7fd4179SDavid Teigland up_write(&ls->ls_root_sem);
934e7fd4179SDavid Teigland }
935e7fd4179SDavid Teigland
dlm_clear_toss(struct dlm_ls * ls)936c04fecb4SDavid Teigland void dlm_clear_toss(struct dlm_ls *ls)
937e7fd4179SDavid Teigland {
9389beb3bf5SBob Peterson struct rb_node *n, *next;
939c04fecb4SDavid Teigland struct dlm_rsb *r;
940c04fecb4SDavid Teigland unsigned int count = 0;
941e7fd4179SDavid Teigland int i;
942e7fd4179SDavid Teigland
943e7fd4179SDavid Teigland for (i = 0; i < ls->ls_rsbtbl_size; i++) {
944c7be761aSDavid Teigland spin_lock(&ls->ls_rsbtbl[i].lock);
9459beb3bf5SBob Peterson for (n = rb_first(&ls->ls_rsbtbl[i].toss); n; n = next) {
946c04fecb4SDavid Teigland next = rb_next(n);
947c04fecb4SDavid Teigland r = rb_entry(n, struct dlm_rsb, res_hashnode);
9489beb3bf5SBob Peterson rb_erase(n, &ls->ls_rsbtbl[i].toss);
949c04fecb4SDavid Teigland dlm_free_rsb(r);
950c04fecb4SDavid Teigland count++;
95185f0379aSDavid Teigland }
952c7be761aSDavid Teigland spin_unlock(&ls->ls_rsbtbl[i].lock);
953e7fd4179SDavid Teigland }
954c04fecb4SDavid Teigland
955c04fecb4SDavid Teigland if (count)
956075f0177SDavid Teigland log_rinfo(ls, "dlm_clear_toss %u done", count);
957e7fd4179SDavid Teigland }
958e7fd4179SDavid Teigland
959