blob: 88ce65ff021ed35848b30ae129e9334930413cc3 [file] [log] [blame]
David Teiglande7fd4172006-01-18 09:30:29 +00001/******************************************************************************
2*******************************************************************************
3**
4** Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
David Teigland60f98d12011-11-02 14:30:58 -05005** Copyright (C) 2004-2011 Red Hat, Inc. All rights reserved.
David Teiglande7fd4172006-01-18 09:30:29 +00006**
7** This copyrighted material is made available to anyone wishing to use,
8** modify, copy, or redistribute it subject to the terms and conditions
9** of the GNU General Public License v.2.
10**
11*******************************************************************************
12******************************************************************************/
13
14#include "dlm_internal.h"
15#include "lockspace.h"
16#include "member.h"
17#include "dir.h"
18#include "ast.h"
19#include "recover.h"
20#include "lowcomms.h"
21#include "lock.h"
22#include "requestqueue.h"
23#include "recoverd.h"
24
25
26/* If the start for which we're re-enabling locking (seq) has been superseded
David Teiglandc36258b2007-09-27 15:53:38 -050027 by a newer stop (ls_recover_seq), we need to leave locking disabled.
28
29 We suspend dlm_recv threads here to avoid the race where dlm_recv a) sees
30 locking stopped and b) adds a message to the requestqueue, but dlm_recoverd
31 enables locking and clears the requestqueue between a and b. */
David Teiglande7fd4172006-01-18 09:30:29 +000032
33static int enable_locking(struct dlm_ls *ls, uint64_t seq)
34{
35 int error = -EINTR;
36
David Teiglandc36258b2007-09-27 15:53:38 -050037 down_write(&ls->ls_recv_active);
38
David Teiglande7fd4172006-01-18 09:30:29 +000039 spin_lock(&ls->ls_recover_lock);
40 if (ls->ls_recover_seq == seq) {
41 set_bit(LSFL_RUNNING, &ls->ls_flags);
David Teiglandc36258b2007-09-27 15:53:38 -050042 /* unblocks processes waiting to enter the dlm */
David Teiglande7fd4172006-01-18 09:30:29 +000043 up_write(&ls->ls_in_recovery);
44 error = 0;
45 }
46 spin_unlock(&ls->ls_recover_lock);
David Teiglandc36258b2007-09-27 15:53:38 -050047
48 up_write(&ls->ls_recv_active);
David Teiglande7fd4172006-01-18 09:30:29 +000049 return error;
50}
51
52static int ls_recover(struct dlm_ls *ls, struct dlm_recover *rv)
53{
54 unsigned long start;
55 int error, neg = 0;
56
David Teigland6d40c4a2012-04-23 16:36:01 -050057 log_debug(ls, "dlm_recover %llu", (unsigned long long)rv->seq);
David Teiglande7fd4172006-01-18 09:30:29 +000058
David Teigland90135922006-01-20 08:47:07 +000059 mutex_lock(&ls->ls_recoverd_active);
David Teiglande7fd4172006-01-18 09:30:29 +000060
David Teigland23e8e1a2011-04-05 13:16:24 -050061 dlm_callback_suspend(ls);
David Teiglande7fd4172006-01-18 09:30:29 +000062
David Teiglandc04fecb2012-05-10 10:18:07 -050063 dlm_clear_toss(ls);
David Teigland85f03792008-01-16 13:02:31 -060064
65 /*
David Teiglande7fd4172006-01-18 09:30:29 +000066 * This list of root rsb's will be the basis of most of the recovery
67 * routines.
68 */
69
70 dlm_create_root_list(ls);
71
72 /*
David Teiglande7fd4172006-01-18 09:30:29 +000073 * Add or remove nodes from the lockspace's ls_nodes list.
David Teiglande7fd4172006-01-18 09:30:29 +000074 */
75
76 error = dlm_recover_members(ls, rv, &neg);
77 if (error) {
David Teiglandf95a34c2011-10-14 12:34:58 -050078 log_debug(ls, "dlm_recover_members error %d", error);
David Teiglande7fd4172006-01-18 09:30:29 +000079 goto fail;
80 }
David Teiglandf95a34c2011-10-14 12:34:58 -050081
David Teiglandc04fecb2012-05-10 10:18:07 -050082 dlm_recover_dir_nodeid(ls);
83
84 ls->ls_recover_dir_sent_res = 0;
85 ls->ls_recover_dir_sent_msg = 0;
David Teigland48756472012-04-26 15:54:29 -050086 ls->ls_recover_locks_in = 0;
87
David Teiglandf95a34c2011-10-14 12:34:58 -050088 dlm_set_recover_status(ls, DLM_RS_NODES);
89
90 error = dlm_recover_members_wait(ls);
91 if (error) {
92 log_debug(ls, "dlm_recover_members_wait error %d", error);
93 goto fail;
94 }
95
David Teiglande7fd4172006-01-18 09:30:29 +000096 start = jiffies;
97
98 /*
99 * Rebuild our own share of the directory by collecting from all other
100 * nodes their master rsb names that hash to us.
101 */
102
103 error = dlm_recover_directory(ls);
104 if (error) {
David Teiglandf95a34c2011-10-14 12:34:58 -0500105 log_debug(ls, "dlm_recover_directory error %d", error);
David Teiglande7fd4172006-01-18 09:30:29 +0000106 goto fail;
107 }
108
David Teiglandf95a34c2011-10-14 12:34:58 -0500109 dlm_set_recover_status(ls, DLM_RS_DIR);
David Teiglande7fd4172006-01-18 09:30:29 +0000110
111 error = dlm_recover_directory_wait(ls);
112 if (error) {
David Teiglandf95a34c2011-10-14 12:34:58 -0500113 log_debug(ls, "dlm_recover_directory_wait error %d", error);
David Teiglande7fd4172006-01-18 09:30:29 +0000114 goto fail;
115 }
116
David Teiglandc04fecb2012-05-10 10:18:07 -0500117 log_debug(ls, "dlm_recover_directory %u out %u messages",
118 ls->ls_recover_dir_sent_res, ls->ls_recover_dir_sent_msg);
119
David Teiglande7fd4172006-01-18 09:30:29 +0000120 /*
121 * We may have outstanding operations that are waiting for a reply from
122 * a failed node. Mark these to be resent after recovery. Unlock and
123 * cancel ops can just be completed.
124 */
125
126 dlm_recover_waiters_pre(ls);
127
128 error = dlm_recovery_stopped(ls);
129 if (error)
130 goto fail;
131
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 Teiglandf95a34c2011-10-14 12:34:58 -0500146 log_debug(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 Teiglandf95a34c2011-10-14 12:34:58 -0500156 log_debug(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 Teiglandf95a34c2011-10-14 12:34:58 -0500164 log_debug(ls, "dlm_recover_locks_wait error %d", error);
David Teiglande7fd4172006-01-18 09:30:29 +0000165 goto fail;
166 }
167
David Teigland48756472012-04-26 15:54:29 -0500168 log_debug(ls, "dlm_recover_locks %u in",
169 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 Teiglandf95a34c2011-10-14 12:34:58 -0500188 log_debug(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 Teiglandf95a34c2011-10-14 12:34:58 -0500207 log_debug(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 Teiglandf95a34c2011-10-14 12:34:58 -0500219 log_debug(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 Teiglandf95a34c2011-10-14 12:34:58 -0500225 log_debug(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 Teiglandf95a34c2011-10-14 12:34:58 -0500231 log_debug(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 Teigland6d40c4a2012-04-23 16:36:01 -0500237 log_debug(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 Teigland6d40c4a2012-04-23 16:36:01 -0500247 log_debug(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)
265 clear_bit(LSFL_RECOVERY_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
285 while (!kthread_should_stop()) {
286 set_current_state(TASK_INTERRUPTIBLE);
287 if (!test_bit(LSFL_WORK, &ls->ls_flags))
288 schedule();
289 set_current_state(TASK_RUNNING);
290
291 if (test_and_clear_bit(LSFL_WORK, &ls->ls_flags))
292 do_ls_recovery(ls);
293 }
294
295 dlm_put_lockspace(ls);
296 return 0;
297}
298
299void dlm_recoverd_kick(struct dlm_ls *ls)
300{
301 set_bit(LSFL_WORK, &ls->ls_flags);
302 wake_up_process(ls->ls_recoverd_task);
303}
304
305int dlm_recoverd_start(struct dlm_ls *ls)
306{
307 struct task_struct *p;
308 int error = 0;
309
310 p = kthread_run(dlm_recoverd, ls, "dlm_recoverd");
311 if (IS_ERR(p))
312 error = PTR_ERR(p);
313 else
314 ls->ls_recoverd_task = p;
315 return error;
316}
317
318void dlm_recoverd_stop(struct dlm_ls *ls)
319{
320 kthread_stop(ls->ls_recoverd_task);
321}
322
323void dlm_recoverd_suspend(struct dlm_ls *ls)
324{
David Teiglandf6db1b82006-08-08 17:06:07 -0500325 wake_up(&ls->ls_wait_general);
David Teigland90135922006-01-20 08:47:07 +0000326 mutex_lock(&ls->ls_recoverd_active);
David Teiglande7fd4172006-01-18 09:30:29 +0000327}
328
329void dlm_recoverd_resume(struct dlm_ls *ls)
330{
David Teigland90135922006-01-20 08:47:07 +0000331 mutex_unlock(&ls->ls_recoverd_active);
David Teiglande7fd4172006-01-18 09:30:29 +0000332}
333