blob: 85e24539271538668425051977069fe07a8f1b2c [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
127 error = dlm_recovery_stopped(ls);
128 if (error)
129 goto fail;
130
131 if (neg || dlm_no_directory(ls)) {
132 /*
133 * Clear lkb's for departed nodes.
134 */
135
David Teigland48756472012-04-26 15:54:29 -0500136 dlm_recover_purge(ls);
David Teiglande7fd4172006-01-18 09:30:29 +0000137
138 /*
139 * Get new master nodeid's for rsb's that were mastered on
140 * departed nodes.
141 */
142
143 error = dlm_recover_masters(ls);
144 if (error) {
David Teigland075f0172014-02-14 11:54:44 -0600145 log_rinfo(ls, "dlm_recover_masters error %d", error);
David Teiglande7fd4172006-01-18 09:30:29 +0000146 goto fail;
147 }
148
149 /*
150 * Send our locks on remastered rsb's to the new masters.
151 */
152
153 error = dlm_recover_locks(ls);
154 if (error) {
David Teigland075f0172014-02-14 11:54:44 -0600155 log_rinfo(ls, "dlm_recover_locks error %d", error);
David Teiglande7fd4172006-01-18 09:30:29 +0000156 goto fail;
157 }
158
David Teiglandf95a34c2011-10-14 12:34:58 -0500159 dlm_set_recover_status(ls, DLM_RS_LOCKS);
160
David Teiglande7fd4172006-01-18 09:30:29 +0000161 error = dlm_recover_locks_wait(ls);
162 if (error) {
David Teigland075f0172014-02-14 11:54:44 -0600163 log_rinfo(ls, "dlm_recover_locks_wait error %d", error);
David Teiglande7fd4172006-01-18 09:30:29 +0000164 goto fail;
165 }
166
David Teigland075f0172014-02-14 11:54:44 -0600167 log_rinfo(ls, "dlm_recover_locks %u in",
David Teigland48756472012-04-26 15:54:29 -0500168 ls->ls_recover_locks_in);
169
David Teiglande7fd4172006-01-18 09:30:29 +0000170 /*
171 * Finalize state in master rsb's now that all locks can be
172 * checked. This includes conversion resolution and lvb
173 * settings.
174 */
175
176 dlm_recover_rsbs(ls);
David Teigland91c0dc92006-10-31 11:56:01 -0600177 } else {
178 /*
179 * Other lockspace members may be going through the "neg" steps
180 * while also adding us to the lockspace, in which case they'll
David Teigland4b77f2c2006-11-01 09:31:48 -0600181 * be doing the recover_locks (RS_LOCKS) barrier.
David Teigland91c0dc92006-10-31 11:56:01 -0600182 */
183 dlm_set_recover_status(ls, DLM_RS_LOCKS);
David Teigland4b77f2c2006-11-01 09:31:48 -0600184
185 error = dlm_recover_locks_wait(ls);
186 if (error) {
David Teigland075f0172014-02-14 11:54:44 -0600187 log_rinfo(ls, "dlm_recover_locks_wait error %d", error);
David Teigland4b77f2c2006-11-01 09:31:48 -0600188 goto fail;
189 }
David Teiglande7fd4172006-01-18 09:30:29 +0000190 }
191
192 dlm_release_root_list(ls);
193
David Teigland2896ee32006-11-27 11:31:22 -0600194 /*
195 * Purge directory-related requests that are saved in requestqueue.
196 * All dir requests from before recovery are invalid now due to the dir
197 * rebuild and will be resent by the requesting nodes.
198 */
199
200 dlm_purge_requestqueue(ls);
201
David Teiglande7fd4172006-01-18 09:30:29 +0000202 dlm_set_recover_status(ls, DLM_RS_DONE);
David Teiglandf95a34c2011-10-14 12:34:58 -0500203
David Teiglande7fd4172006-01-18 09:30:29 +0000204 error = dlm_recover_done_wait(ls);
205 if (error) {
David Teigland075f0172014-02-14 11:54:44 -0600206 log_rinfo(ls, "dlm_recover_done_wait error %d", error);
David Teiglande7fd4172006-01-18 09:30:29 +0000207 goto fail;
208 }
209
210 dlm_clear_members_gone(ls);
211
David Teigland3ae1acf2007-05-18 08:59:31 -0500212 dlm_adjust_timeouts(ls);
213
David Teigland23e8e1a2011-04-05 13:16:24 -0500214 dlm_callback_resume(ls);
215
David Teiglande7fd4172006-01-18 09:30:29 +0000216 error = enable_locking(ls, rv->seq);
217 if (error) {
David Teigland075f0172014-02-14 11:54:44 -0600218 log_rinfo(ls, "enable_locking error %d", error);
David Teiglande7fd4172006-01-18 09:30:29 +0000219 goto fail;
220 }
221
222 error = dlm_process_requestqueue(ls);
223 if (error) {
David Teigland075f0172014-02-14 11:54:44 -0600224 log_rinfo(ls, "dlm_process_requestqueue error %d", error);
David Teiglande7fd4172006-01-18 09:30:29 +0000225 goto fail;
226 }
227
228 error = dlm_recover_waiters_post(ls);
229 if (error) {
David Teigland075f0172014-02-14 11:54:44 -0600230 log_rinfo(ls, "dlm_recover_waiters_post error %d", error);
David Teiglande7fd4172006-01-18 09:30:29 +0000231 goto fail;
232 }
233
David Teigland48756472012-04-26 15:54:29 -0500234 dlm_recover_grant(ls);
David Teiglande7fd4172006-01-18 09:30:29 +0000235
David Teigland075f0172014-02-14 11:54:44 -0600236 log_rinfo(ls, "dlm_recover %llu generation %u done: %u ms",
David Teigland60f98d12011-11-02 14:30:58 -0500237 (unsigned long long)rv->seq, ls->ls_generation,
David Teiglande7fd4172006-01-18 09:30:29 +0000238 jiffies_to_msecs(jiffies - start));
David Teigland90135922006-01-20 08:47:07 +0000239 mutex_unlock(&ls->ls_recoverd_active);
David Teiglande7fd4172006-01-18 09:30:29 +0000240
David Teigland60f98d12011-11-02 14:30:58 -0500241 dlm_lsop_recover_done(ls);
David Teiglande7fd4172006-01-18 09:30:29 +0000242 return 0;
243
244 fail:
245 dlm_release_root_list(ls);
David Teigland075f0172014-02-14 11:54:44 -0600246 log_rinfo(ls, "dlm_recover %llu error %d",
Ryusuke Konishi57adf7e2006-11-29 09:33:48 -0500247 (unsigned long long)rv->seq, error);
David Teigland90135922006-01-20 08:47:07 +0000248 mutex_unlock(&ls->ls_recoverd_active);
David Teiglande7fd4172006-01-18 09:30:29 +0000249 return error;
250}
251
David Teigland2cdc98a2006-10-31 11:56:08 -0600252/* The dlm_ls_start() that created the rv we take here may already have been
253 stopped via dlm_ls_stop(); in that case we need to leave the RECOVERY_STOP
254 flag set. */
255
David Teiglande7fd4172006-01-18 09:30:29 +0000256static void do_ls_recovery(struct dlm_ls *ls)
257{
258 struct dlm_recover *rv = NULL;
259
260 spin_lock(&ls->ls_recover_lock);
261 rv = ls->ls_recover_args;
262 ls->ls_recover_args = NULL;
David Teigland2cdc98a2006-10-31 11:56:08 -0600263 if (rv && ls->ls_recover_seq == rv->seq)
David Teigland475f2302012-08-02 11:08:21 -0500264 clear_bit(LSFL_RECOVER_STOP, &ls->ls_flags);
David Teiglande7fd4172006-01-18 09:30:29 +0000265 spin_unlock(&ls->ls_recover_lock);
266
267 if (rv) {
268 ls_recover(ls, rv);
David Teigland60f98d12011-11-02 14:30:58 -0500269 kfree(rv->nodes);
David Teiglande7fd4172006-01-18 09:30:29 +0000270 kfree(rv);
271 }
272}
273
274static int dlm_recoverd(void *arg)
275{
276 struct dlm_ls *ls;
277
278 ls = dlm_find_lockspace_local(arg);
David Teigland5f88f1e2006-08-24 14:47:20 -0500279 if (!ls) {
280 log_print("dlm_recoverd: no lockspace %p", arg);
281 return -1;
282 }
David Teiglande7fd4172006-01-18 09:30:29 +0000283
David Teigland475f2302012-08-02 11:08:21 -0500284 down_write(&ls->ls_in_recovery);
285 set_bit(LSFL_RECOVER_LOCK, &ls->ls_flags);
286 wake_up(&ls->ls_recover_lock_wait);
287
tsutomu.owa@toshiba.co.jpe412f922017-09-12 08:56:30 +0000288 while (1) {
289 /*
290 * We call kthread_should_stop() after set_current_state().
291 * This is because it works correctly if kthread_stop() is
292 * called just before set_current_state().
293 */
David Teiglande7fd4172006-01-18 09:30:29 +0000294 set_current_state(TASK_INTERRUPTIBLE);
tsutomu.owa@toshiba.co.jpe412f922017-09-12 08:56:30 +0000295 if (kthread_should_stop()) {
296 set_current_state(TASK_RUNNING);
297 break;
298 }
David Teigland475f2302012-08-02 11:08:21 -0500299 if (!test_bit(LSFL_RECOVER_WORK, &ls->ls_flags) &&
Guoqing Jiang9e1b0212017-09-25 15:47:50 +0800300 !test_bit(LSFL_RECOVER_DOWN, &ls->ls_flags)) {
301 if (kthread_should_stop())
302 break;
David Teiglande7fd4172006-01-18 09:30:29 +0000303 schedule();
Guoqing Jiang9e1b0212017-09-25 15:47:50 +0800304 }
David Teiglande7fd4172006-01-18 09:30:29 +0000305 set_current_state(TASK_RUNNING);
306
David Teigland475f2302012-08-02 11:08:21 -0500307 if (test_and_clear_bit(LSFL_RECOVER_DOWN, &ls->ls_flags)) {
308 down_write(&ls->ls_in_recovery);
309 set_bit(LSFL_RECOVER_LOCK, &ls->ls_flags);
310 wake_up(&ls->ls_recover_lock_wait);
311 }
312
313 if (test_and_clear_bit(LSFL_RECOVER_WORK, &ls->ls_flags))
David Teiglande7fd4172006-01-18 09:30:29 +0000314 do_ls_recovery(ls);
315 }
316
David Teigland475f2302012-08-02 11:08:21 -0500317 if (test_bit(LSFL_RECOVER_LOCK, &ls->ls_flags))
318 up_write(&ls->ls_in_recovery);
319
David Teiglande7fd4172006-01-18 09:30:29 +0000320 dlm_put_lockspace(ls);
321 return 0;
322}
323
David Teiglande7fd4172006-01-18 09:30:29 +0000324int dlm_recoverd_start(struct dlm_ls *ls)
325{
326 struct task_struct *p;
327 int error = 0;
328
329 p = kthread_run(dlm_recoverd, ls, "dlm_recoverd");
330 if (IS_ERR(p))
331 error = PTR_ERR(p);
332 else
333 ls->ls_recoverd_task = p;
334 return error;
335}
336
337void dlm_recoverd_stop(struct dlm_ls *ls)
338{
339 kthread_stop(ls->ls_recoverd_task);
340}
341
342void dlm_recoverd_suspend(struct dlm_ls *ls)
343{
David Teiglandf6db1b82006-08-08 17:06:07 -0500344 wake_up(&ls->ls_wait_general);
David Teigland90135922006-01-20 08:47:07 +0000345 mutex_lock(&ls->ls_recoverd_active);
David Teiglande7fd4172006-01-18 09:30:29 +0000346}
347
348void dlm_recoverd_resume(struct dlm_ls *ls)
349{
David Teigland90135922006-01-20 08:47:07 +0000350 mutex_unlock(&ls->ls_recoverd_active);
David Teiglande7fd4172006-01-18 09:30:29 +0000351}
352