blob: a55dfce705dd264b78d0dc8df352517c993f4a14 [file] [log] [blame]
Thomas Gleixner2522fe42019-05-28 09:57:20 -07001// SPDX-License-Identifier: GPL-2.0-only
David Teiglande7fd4172006-01-18 09:30:29 +00002/******************************************************************************
3*******************************************************************************
4**
5** Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
David Teigland60f98d12011-11-02 14:30:58 -05006** Copyright (C) 2004-2011 Red Hat, Inc. All rights reserved.
David Teiglande7fd4172006-01-18 09:30:29 +00007**
David Teiglande7fd4172006-01-18 09:30:29 +00008**
9*******************************************************************************
10******************************************************************************/
11
12#include "dlm_internal.h"
13#include "lockspace.h"
14#include "member.h"
15#include "dir.h"
16#include "ast.h"
17#include "recover.h"
18#include "lowcomms.h"
19#include "lock.h"
20#include "requestqueue.h"
21#include "recoverd.h"
22
23
24/* If the start for which we're re-enabling locking (seq) has been superseded
David Teiglandc36258b2007-09-27 15:53:38 -050025 by a newer stop (ls_recover_seq), we need to leave locking disabled.
26
27 We suspend dlm_recv threads here to avoid the race where dlm_recv a) sees
28 locking stopped and b) adds a message to the requestqueue, but dlm_recoverd
29 enables locking and clears the requestqueue between a and b. */
David Teiglande7fd4172006-01-18 09:30:29 +000030
31static int enable_locking(struct dlm_ls *ls, uint64_t seq)
32{
33 int error = -EINTR;
34
David Teiglandc36258b2007-09-27 15:53:38 -050035 down_write(&ls->ls_recv_active);
36
David Teiglande7fd4172006-01-18 09:30:29 +000037 spin_lock(&ls->ls_recover_lock);
38 if (ls->ls_recover_seq == seq) {
39 set_bit(LSFL_RUNNING, &ls->ls_flags);
David Teiglandc36258b2007-09-27 15:53:38 -050040 /* unblocks processes waiting to enter the dlm */
David Teiglande7fd4172006-01-18 09:30:29 +000041 up_write(&ls->ls_in_recovery);
David Teigland475f2302012-08-02 11:08:21 -050042 clear_bit(LSFL_RECOVER_LOCK, &ls->ls_flags);
David Teiglande7fd4172006-01-18 09:30:29 +000043 error = 0;
44 }
45 spin_unlock(&ls->ls_recover_lock);
David Teiglandc36258b2007-09-27 15:53:38 -050046
47 up_write(&ls->ls_recv_active);
David Teiglande7fd4172006-01-18 09:30:29 +000048 return error;
49}
50
51static int ls_recover(struct dlm_ls *ls, struct dlm_recover *rv)
52{
53 unsigned long start;
54 int error, neg = 0;
55
David Teigland075f0172014-02-14 11:54:44 -060056 log_rinfo(ls, "dlm_recover %llu", (unsigned long long)rv->seq);
David Teiglande7fd4172006-01-18 09:30:29 +000057
David Teigland90135922006-01-20 08:47:07 +000058 mutex_lock(&ls->ls_recoverd_active);
David Teiglande7fd4172006-01-18 09:30:29 +000059
David Teigland23e8e1a2011-04-05 13:16:24 -050060 dlm_callback_suspend(ls);
David Teiglande7fd4172006-01-18 09:30:29 +000061
David Teiglandc04fecb2012-05-10 10:18:07 -050062 dlm_clear_toss(ls);
David Teigland85f03792008-01-16 13:02:31 -060063
64 /*
David Teiglande7fd4172006-01-18 09:30:29 +000065 * This list of root rsb's will be the basis of most of the recovery
66 * routines.
67 */
68
69 dlm_create_root_list(ls);
70
71 /*
David Teiglande7fd4172006-01-18 09:30:29 +000072 * Add or remove nodes from the lockspace's ls_nodes list.
David Teiglande7fd4172006-01-18 09:30:29 +000073 */
74
75 error = dlm_recover_members(ls, rv, &neg);
76 if (error) {
David Teigland075f0172014-02-14 11:54:44 -060077 log_rinfo(ls, "dlm_recover_members error %d", error);
David Teiglande7fd4172006-01-18 09:30:29 +000078 goto fail;
79 }
David Teiglandf95a34c2011-10-14 12:34:58 -050080
David Teiglandc04fecb2012-05-10 10:18:07 -050081 dlm_recover_dir_nodeid(ls);
82
83 ls->ls_recover_dir_sent_res = 0;
84 ls->ls_recover_dir_sent_msg = 0;
David Teigland48756472012-04-26 15:54:29 -050085 ls->ls_recover_locks_in = 0;
86
David Teiglandf95a34c2011-10-14 12:34:58 -050087 dlm_set_recover_status(ls, DLM_RS_NODES);
88
89 error = dlm_recover_members_wait(ls);
90 if (error) {
David Teigland075f0172014-02-14 11:54:44 -060091 log_rinfo(ls, "dlm_recover_members_wait error %d", error);
David Teiglandf95a34c2011-10-14 12:34:58 -050092 goto fail;
93 }
94
David Teiglande7fd4172006-01-18 09:30:29 +000095 start = jiffies;
96
97 /*
98 * Rebuild our own share of the directory by collecting from all other
99 * nodes their master rsb names that hash to us.
100 */
101
102 error = dlm_recover_directory(ls);
103 if (error) {
David Teigland075f0172014-02-14 11:54:44 -0600104 log_rinfo(ls, "dlm_recover_directory error %d", error);
David Teiglande7fd4172006-01-18 09:30:29 +0000105 goto fail;
106 }
107
David Teiglandf95a34c2011-10-14 12:34:58 -0500108 dlm_set_recover_status(ls, DLM_RS_DIR);
David Teiglande7fd4172006-01-18 09:30:29 +0000109
110 error = dlm_recover_directory_wait(ls);
111 if (error) {
David Teigland075f0172014-02-14 11:54:44 -0600112 log_rinfo(ls, "dlm_recover_directory_wait error %d", error);
David Teiglande7fd4172006-01-18 09:30:29 +0000113 goto fail;
114 }
115
David Teigland075f0172014-02-14 11:54:44 -0600116 log_rinfo(ls, "dlm_recover_directory %u out %u messages",
David Teiglandc04fecb2012-05-10 10:18:07 -0500117 ls->ls_recover_dir_sent_res, ls->ls_recover_dir_sent_msg);
118
David Teiglande7fd4172006-01-18 09:30:29 +0000119 /*
120 * We may have outstanding operations that are waiting for a reply from
121 * a failed node. Mark these to be resent after recovery. Unlock and
122 * cancel ops can just be completed.
123 */
124
125 dlm_recover_waiters_pre(ls);
126
Alexander Aringe10249b2021-11-02 15:17:13 -0400127 if (dlm_recovery_stopped(ls)) {
Alexander Aringaee742c2021-08-18 16:27:14 -0400128 error = -EINTR;
David Teiglande7fd4172006-01-18 09:30:29 +0000129 goto fail;
Alexander Aringaee742c2021-08-18 16:27:14 -0400130 }
David Teiglande7fd4172006-01-18 09:30:29 +0000131
132 if (neg || dlm_no_directory(ls)) {
133 /*
134 * Clear lkb's for departed nodes.
135 */
136
David Teigland48756472012-04-26 15:54:29 -0500137 dlm_recover_purge(ls);
David Teiglande7fd4172006-01-18 09:30:29 +0000138
139 /*
140 * Get new master nodeid's for rsb's that were mastered on
141 * departed nodes.
142 */
143
144 error = dlm_recover_masters(ls);
145 if (error) {
David Teigland075f0172014-02-14 11:54:44 -0600146 log_rinfo(ls, "dlm_recover_masters error %d", error);
David Teiglande7fd4172006-01-18 09:30:29 +0000147 goto fail;
148 }
149
150 /*
151 * Send our locks on remastered rsb's to the new masters.
152 */
153
154 error = dlm_recover_locks(ls);
155 if (error) {
David Teigland075f0172014-02-14 11:54:44 -0600156 log_rinfo(ls, "dlm_recover_locks error %d", error);
David Teiglande7fd4172006-01-18 09:30:29 +0000157 goto fail;
158 }
159
David Teiglandf95a34c2011-10-14 12:34:58 -0500160 dlm_set_recover_status(ls, DLM_RS_LOCKS);
161
David Teiglande7fd4172006-01-18 09:30:29 +0000162 error = dlm_recover_locks_wait(ls);
163 if (error) {
David Teigland075f0172014-02-14 11:54:44 -0600164 log_rinfo(ls, "dlm_recover_locks_wait error %d", error);
David Teiglande7fd4172006-01-18 09:30:29 +0000165 goto fail;
166 }
167
David Teigland075f0172014-02-14 11:54:44 -0600168 log_rinfo(ls, "dlm_recover_locks %u in",
David Teigland48756472012-04-26 15:54:29 -0500169 ls->ls_recover_locks_in);
170
David Teiglande7fd4172006-01-18 09:30:29 +0000171 /*
172 * Finalize state in master rsb's now that all locks can be
173 * checked. This includes conversion resolution and lvb
174 * settings.
175 */
176
177 dlm_recover_rsbs(ls);
David Teigland91c0dc92006-10-31 11:56:01 -0600178 } else {
179 /*
180 * Other lockspace members may be going through the "neg" steps
181 * while also adding us to the lockspace, in which case they'll
David Teigland4b77f2c2006-11-01 09:31:48 -0600182 * be doing the recover_locks (RS_LOCKS) barrier.
David Teigland91c0dc92006-10-31 11:56:01 -0600183 */
184 dlm_set_recover_status(ls, DLM_RS_LOCKS);
David Teigland4b77f2c2006-11-01 09:31:48 -0600185
186 error = dlm_recover_locks_wait(ls);
187 if (error) {
David Teigland075f0172014-02-14 11:54:44 -0600188 log_rinfo(ls, "dlm_recover_locks_wait error %d", error);
David Teigland4b77f2c2006-11-01 09:31:48 -0600189 goto fail;
190 }
David Teiglande7fd4172006-01-18 09:30:29 +0000191 }
192
193 dlm_release_root_list(ls);
194
David Teigland2896ee32006-11-27 11:31:22 -0600195 /*
196 * Purge directory-related requests that are saved in requestqueue.
197 * All dir requests from before recovery are invalid now due to the dir
198 * rebuild and will be resent by the requesting nodes.
199 */
200
201 dlm_purge_requestqueue(ls);
202
David Teiglande7fd4172006-01-18 09:30:29 +0000203 dlm_set_recover_status(ls, DLM_RS_DONE);
David Teiglandf95a34c2011-10-14 12:34:58 -0500204
David Teiglande7fd4172006-01-18 09:30:29 +0000205 error = dlm_recover_done_wait(ls);
206 if (error) {
David Teigland075f0172014-02-14 11:54:44 -0600207 log_rinfo(ls, "dlm_recover_done_wait error %d", error);
David Teiglande7fd4172006-01-18 09:30:29 +0000208 goto fail;
209 }
210
211 dlm_clear_members_gone(ls);
212
David Teigland3ae1acf2007-05-18 08:59:31 -0500213 dlm_adjust_timeouts(ls);
214
David Teigland23e8e1a2011-04-05 13:16:24 -0500215 dlm_callback_resume(ls);
216
David Teiglande7fd4172006-01-18 09:30:29 +0000217 error = enable_locking(ls, rv->seq);
218 if (error) {
David Teigland075f0172014-02-14 11:54:44 -0600219 log_rinfo(ls, "enable_locking error %d", error);
David Teiglande7fd4172006-01-18 09:30:29 +0000220 goto fail;
221 }
222
223 error = dlm_process_requestqueue(ls);
224 if (error) {
David Teigland075f0172014-02-14 11:54:44 -0600225 log_rinfo(ls, "dlm_process_requestqueue error %d", error);
David Teiglande7fd4172006-01-18 09:30:29 +0000226 goto fail;
227 }
228
229 error = dlm_recover_waiters_post(ls);
230 if (error) {
David Teigland075f0172014-02-14 11:54:44 -0600231 log_rinfo(ls, "dlm_recover_waiters_post error %d", error);
David Teiglande7fd4172006-01-18 09:30:29 +0000232 goto fail;
233 }
234
David Teigland48756472012-04-26 15:54:29 -0500235 dlm_recover_grant(ls);
David Teiglande7fd4172006-01-18 09:30:29 +0000236
David Teigland075f0172014-02-14 11:54:44 -0600237 log_rinfo(ls, "dlm_recover %llu generation %u done: %u ms",
David Teigland60f98d12011-11-02 14:30:58 -0500238 (unsigned long long)rv->seq, ls->ls_generation,
David Teiglande7fd4172006-01-18 09:30:29 +0000239 jiffies_to_msecs(jiffies - start));
David Teigland90135922006-01-20 08:47:07 +0000240 mutex_unlock(&ls->ls_recoverd_active);
David Teiglande7fd4172006-01-18 09:30:29 +0000241
David Teigland60f98d12011-11-02 14:30:58 -0500242 dlm_lsop_recover_done(ls);
David Teiglande7fd4172006-01-18 09:30:29 +0000243 return 0;
244
245 fail:
246 dlm_release_root_list(ls);
David Teigland075f0172014-02-14 11:54:44 -0600247 log_rinfo(ls, "dlm_recover %llu error %d",
Ryusuke Konishi57adf7e2006-11-29 09:33:48 -0500248 (unsigned long long)rv->seq, error);
David Teigland90135922006-01-20 08:47:07 +0000249 mutex_unlock(&ls->ls_recoverd_active);
David Teiglande7fd4172006-01-18 09:30:29 +0000250 return error;
251}
252
David Teigland2cdc98a2006-10-31 11:56:08 -0600253/* The dlm_ls_start() that created the rv we take here may already have been
254 stopped via dlm_ls_stop(); in that case we need to leave the RECOVERY_STOP
255 flag set. */
256
David Teiglande7fd4172006-01-18 09:30:29 +0000257static void do_ls_recovery(struct dlm_ls *ls)
258{
259 struct dlm_recover *rv = NULL;
260
261 spin_lock(&ls->ls_recover_lock);
262 rv = ls->ls_recover_args;
263 ls->ls_recover_args = NULL;
David Teigland2cdc98a2006-10-31 11:56:08 -0600264 if (rv && ls->ls_recover_seq == rv->seq)
David Teigland475f2302012-08-02 11:08:21 -0500265 clear_bit(LSFL_RECOVER_STOP, &ls->ls_flags);
David Teiglande7fd4172006-01-18 09:30:29 +0000266 spin_unlock(&ls->ls_recover_lock);
267
268 if (rv) {
269 ls_recover(ls, rv);
David Teigland60f98d12011-11-02 14:30:58 -0500270 kfree(rv->nodes);
David Teiglande7fd4172006-01-18 09:30:29 +0000271 kfree(rv);
272 }
273}
274
275static int dlm_recoverd(void *arg)
276{
277 struct dlm_ls *ls;
278
279 ls = dlm_find_lockspace_local(arg);
David Teigland5f88f1e2006-08-24 14:47:20 -0500280 if (!ls) {
281 log_print("dlm_recoverd: no lockspace %p", arg);
282 return -1;
283 }
David Teiglande7fd4172006-01-18 09:30:29 +0000284
David Teigland475f2302012-08-02 11:08:21 -0500285 down_write(&ls->ls_in_recovery);
286 set_bit(LSFL_RECOVER_LOCK, &ls->ls_flags);
287 wake_up(&ls->ls_recover_lock_wait);
288
tsutomu.owa@toshiba.co.jpe412f922017-09-12 08:56:30 +0000289 while (1) {
290 /*
291 * We call kthread_should_stop() after set_current_state().
292 * This is because it works correctly if kthread_stop() is
293 * called just before set_current_state().
294 */
David Teiglande7fd4172006-01-18 09:30:29 +0000295 set_current_state(TASK_INTERRUPTIBLE);
tsutomu.owa@toshiba.co.jpe412f922017-09-12 08:56:30 +0000296 if (kthread_should_stop()) {
297 set_current_state(TASK_RUNNING);
298 break;
299 }
David Teigland475f2302012-08-02 11:08:21 -0500300 if (!test_bit(LSFL_RECOVER_WORK, &ls->ls_flags) &&
Guoqing Jiang9e1b0212017-09-25 15:47:50 +0800301 !test_bit(LSFL_RECOVER_DOWN, &ls->ls_flags)) {
302 if (kthread_should_stop())
303 break;
David Teiglande7fd4172006-01-18 09:30:29 +0000304 schedule();
Guoqing Jiang9e1b0212017-09-25 15:47:50 +0800305 }
David Teiglande7fd4172006-01-18 09:30:29 +0000306 set_current_state(TASK_RUNNING);
307
David Teigland475f2302012-08-02 11:08:21 -0500308 if (test_and_clear_bit(LSFL_RECOVER_DOWN, &ls->ls_flags)) {
309 down_write(&ls->ls_in_recovery);
310 set_bit(LSFL_RECOVER_LOCK, &ls->ls_flags);
311 wake_up(&ls->ls_recover_lock_wait);
312 }
313
314 if (test_and_clear_bit(LSFL_RECOVER_WORK, &ls->ls_flags))
David Teiglande7fd4172006-01-18 09:30:29 +0000315 do_ls_recovery(ls);
316 }
317
David Teigland475f2302012-08-02 11:08:21 -0500318 if (test_bit(LSFL_RECOVER_LOCK, &ls->ls_flags))
319 up_write(&ls->ls_in_recovery);
320
David Teiglande7fd4172006-01-18 09:30:29 +0000321 dlm_put_lockspace(ls);
322 return 0;
323}
324
David Teiglande7fd4172006-01-18 09:30:29 +0000325int dlm_recoverd_start(struct dlm_ls *ls)
326{
327 struct task_struct *p;
328 int error = 0;
329
330 p = kthread_run(dlm_recoverd, ls, "dlm_recoverd");
331 if (IS_ERR(p))
332 error = PTR_ERR(p);
333 else
334 ls->ls_recoverd_task = p;
335 return error;
336}
337
338void dlm_recoverd_stop(struct dlm_ls *ls)
339{
340 kthread_stop(ls->ls_recoverd_task);
341}
342
343void dlm_recoverd_suspend(struct dlm_ls *ls)
344{
David Teiglandf6db1b82006-08-08 17:06:07 -0500345 wake_up(&ls->ls_wait_general);
David Teigland90135922006-01-20 08:47:07 +0000346 mutex_lock(&ls->ls_recoverd_active);
David Teiglande7fd4172006-01-18 09:30:29 +0000347}
348
349void dlm_recoverd_resume(struct dlm_ls *ls)
350{
David Teigland90135922006-01-20 08:47:07 +0000351 mutex_unlock(&ls->ls_recoverd_active);
David Teiglande7fd4172006-01-18 09:30:29 +0000352}
353