1 // SPDX-License-Identifier: GPL-2.0-only
2 /******************************************************************************
3 *******************************************************************************
5 ** Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
6 ** Copyright (C) 2004-2011 Red Hat, Inc. All rights reserved.
9 *******************************************************************************
10 ******************************************************************************/
12 #include "dlm_internal.h"
13 #include "lockspace.h"
20 #include "requestqueue.h"
23 static int dlm_create_masters_list(struct dlm_ls
*ls
)
28 write_lock_bh(&ls
->ls_masters_lock
);
29 if (!list_empty(&ls
->ls_masters_list
)) {
30 log_error(ls
, "root list not empty");
35 read_lock_bh(&ls
->ls_rsbtbl_lock
);
36 list_for_each_entry(r
, &ls
->ls_slow_active
, res_slow_list
) {
40 list_add(&r
->res_masters_list
, &ls
->ls_masters_list
);
43 read_unlock_bh(&ls
->ls_rsbtbl_lock
);
45 write_unlock_bh(&ls
->ls_masters_lock
);
49 static void dlm_release_masters_list(struct dlm_ls
*ls
)
51 struct dlm_rsb
*r
, *safe
;
53 write_lock_bh(&ls
->ls_masters_lock
);
54 list_for_each_entry_safe(r
, safe
, &ls
->ls_masters_list
, res_masters_list
) {
55 list_del_init(&r
->res_masters_list
);
58 write_unlock_bh(&ls
->ls_masters_lock
);
61 static void dlm_create_root_list(struct dlm_ls
*ls
, struct list_head
*root_list
)
65 read_lock_bh(&ls
->ls_rsbtbl_lock
);
66 list_for_each_entry(r
, &ls
->ls_slow_active
, res_slow_list
) {
67 list_add(&r
->res_root_list
, root_list
);
71 WARN_ON_ONCE(!list_empty(&ls
->ls_slow_inactive
));
72 read_unlock_bh(&ls
->ls_rsbtbl_lock
);
75 static void dlm_release_root_list(struct list_head
*root_list
)
77 struct dlm_rsb
*r
, *safe
;
79 list_for_each_entry_safe(r
, safe
, root_list
, res_root_list
) {
80 list_del_init(&r
->res_root_list
);
85 /* If the start for which we're re-enabling locking (seq) has been superseded
86 by a newer stop (ls_recover_seq), we need to leave locking disabled.
88 We suspend dlm_recv threads here to avoid the race where dlm_recv a) sees
89 locking stopped and b) adds a message to the requestqueue, but dlm_recoverd
90 enables locking and clears the requestqueue between a and b. */
92 static int enable_locking(struct dlm_ls
*ls
, uint64_t seq
)
96 write_lock_bh(&ls
->ls_recv_active
);
98 spin_lock_bh(&ls
->ls_recover_lock
);
99 if (ls
->ls_recover_seq
== seq
) {
100 set_bit(LSFL_RUNNING
, &ls
->ls_flags
);
101 /* Schedule next timer if recovery put something on inactive.
103 * The rsbs that was queued while recovery on toss hasn't
104 * started yet because LSFL_RUNNING was set everything
105 * else recovery hasn't started as well because ls_in_recovery
106 * is still hold. So we should not run into the case that
107 * resume_scan_timer() queues a timer that can occur in
110 resume_scan_timer(ls
);
111 /* unblocks processes waiting to enter the dlm */
112 up_write(&ls
->ls_in_recovery
);
113 clear_bit(LSFL_RECOVER_LOCK
, &ls
->ls_flags
);
116 spin_unlock_bh(&ls
->ls_recover_lock
);
118 write_unlock_bh(&ls
->ls_recv_active
);
122 static int ls_recover(struct dlm_ls
*ls
, struct dlm_recover
*rv
)
124 LIST_HEAD(root_list
);
128 log_rinfo(ls
, "dlm_recover %llu", (unsigned long long)rv
->seq
);
130 mutex_lock(&ls
->ls_recoverd_active
);
132 dlm_callback_suspend(ls
);
134 dlm_clear_inactive(ls
);
137 * This list of root rsb's will be the basis of most of the recovery
141 dlm_create_root_list(ls
, &root_list
);
144 * Add or remove nodes from the lockspace's ls_nodes list.
146 * Due to the fact that we must report all membership changes to lsops
147 * or midcomms layer, it is not permitted to abort ls_recover() until
151 error
= dlm_recover_members(ls
, rv
, &neg
);
153 log_rinfo(ls
, "dlm_recover_members error %d", error
);
157 dlm_recover_dir_nodeid(ls
, &root_list
);
159 /* Create a snapshot of all active rsbs were we are the master of.
160 * During the barrier between dlm_recover_members_wait() and
161 * dlm_recover_directory() other nodes can dump their necessary
162 * directory dlm_rsb (r->res_dir_nodeid == nodeid) in rcom
163 * communication dlm_copy_master_names() handling.
165 * TODO We should create a per lockspace list that contains rsbs
166 * that we are the master of. Instead of creating this list while
167 * recovery we keep track of those rsbs while locking handling and
168 * recovery can use it when necessary.
170 error
= dlm_create_masters_list(ls
);
172 log_rinfo(ls
, "dlm_create_masters_list error %d", error
);
176 ls
->ls_recover_locks_in
= 0;
178 dlm_set_recover_status(ls
, DLM_RS_NODES
);
180 error
= dlm_recover_members_wait(ls
, rv
->seq
);
182 log_rinfo(ls
, "dlm_recover_members_wait error %d", error
);
183 dlm_release_masters_list(ls
);
190 * Rebuild our own share of the directory by collecting from all other
191 * nodes their master rsb names that hash to us.
194 error
= dlm_recover_directory(ls
, rv
->seq
);
196 log_rinfo(ls
, "dlm_recover_directory error %d", error
);
197 dlm_release_masters_list(ls
);
201 dlm_set_recover_status(ls
, DLM_RS_DIR
);
203 error
= dlm_recover_directory_wait(ls
, rv
->seq
);
205 log_rinfo(ls
, "dlm_recover_directory_wait error %d", error
);
206 dlm_release_masters_list(ls
);
210 dlm_release_masters_list(ls
);
213 * We may have outstanding operations that are waiting for a reply from
214 * a failed node. Mark these to be resent after recovery. Unlock and
215 * cancel ops can just be completed.
218 dlm_recover_waiters_pre(ls
);
220 if (dlm_recovery_stopped(ls
)) {
225 if (neg
|| dlm_no_directory(ls
)) {
227 * Clear lkb's for departed nodes.
230 dlm_recover_purge(ls
, &root_list
);
233 * Get new master nodeid's for rsb's that were mastered on
237 error
= dlm_recover_masters(ls
, rv
->seq
, &root_list
);
239 log_rinfo(ls
, "dlm_recover_masters error %d", error
);
244 * Send our locks on remastered rsb's to the new masters.
247 error
= dlm_recover_locks(ls
, rv
->seq
, &root_list
);
249 log_rinfo(ls
, "dlm_recover_locks error %d", error
);
253 dlm_set_recover_status(ls
, DLM_RS_LOCKS
);
255 error
= dlm_recover_locks_wait(ls
, rv
->seq
);
257 log_rinfo(ls
, "dlm_recover_locks_wait error %d", error
);
261 log_rinfo(ls
, "dlm_recover_locks %u in",
262 ls
->ls_recover_locks_in
);
265 * Finalize state in master rsb's now that all locks can be
266 * checked. This includes conversion resolution and lvb
270 dlm_recover_rsbs(ls
, &root_list
);
273 * Other lockspace members may be going through the "neg" steps
274 * while also adding us to the lockspace, in which case they'll
275 * be doing the recover_locks (RS_LOCKS) barrier.
277 dlm_set_recover_status(ls
, DLM_RS_LOCKS
);
279 error
= dlm_recover_locks_wait(ls
, rv
->seq
);
281 log_rinfo(ls
, "dlm_recover_locks_wait error %d", error
);
286 dlm_release_root_list(&root_list
);
289 * Purge directory-related requests that are saved in requestqueue.
290 * All dir requests from before recovery are invalid now due to the dir
291 * rebuild and will be resent by the requesting nodes.
294 dlm_purge_requestqueue(ls
);
296 dlm_set_recover_status(ls
, DLM_RS_DONE
);
298 error
= dlm_recover_done_wait(ls
, rv
->seq
);
300 log_rinfo(ls
, "dlm_recover_done_wait error %d", error
);
304 dlm_clear_members_gone(ls
);
306 dlm_callback_resume(ls
);
308 error
= enable_locking(ls
, rv
->seq
);
310 log_rinfo(ls
, "enable_locking error %d", error
);
314 error
= dlm_process_requestqueue(ls
);
316 log_rinfo(ls
, "dlm_process_requestqueue error %d", error
);
320 error
= dlm_recover_waiters_post(ls
);
322 log_rinfo(ls
, "dlm_recover_waiters_post error %d", error
);
326 dlm_recover_grant(ls
);
328 log_rinfo(ls
, "dlm_recover %llu generation %u done: %u ms",
329 (unsigned long long)rv
->seq
, ls
->ls_generation
,
330 jiffies_to_msecs(jiffies
- start
));
331 mutex_unlock(&ls
->ls_recoverd_active
);
336 dlm_release_root_list(&root_list
);
338 mutex_unlock(&ls
->ls_recoverd_active
);
343 /* The dlm_ls_start() that created the rv we take here may already have been
344 stopped via dlm_ls_stop(); in that case we need to leave the RECOVERY_STOP
347 static void do_ls_recovery(struct dlm_ls
*ls
)
349 struct dlm_recover
*rv
= NULL
;
352 spin_lock_bh(&ls
->ls_recover_lock
);
353 rv
= ls
->ls_recover_args
;
354 ls
->ls_recover_args
= NULL
;
355 if (rv
&& ls
->ls_recover_seq
== rv
->seq
)
356 clear_bit(LSFL_RECOVER_STOP
, &ls
->ls_flags
);
357 spin_unlock_bh(&ls
->ls_recover_lock
);
360 error
= ls_recover(ls
, rv
);
363 ls
->ls_recovery_result
= 0;
364 complete(&ls
->ls_recovery_done
);
366 dlm_lsop_recover_done(ls
);
369 /* if recovery was interrupted -EINTR we wait for the next
370 * ls_recover() iteration until it hopefully succeeds.
372 log_rinfo(ls
, "%s %llu interrupted and should be queued to run again",
373 __func__
, (unsigned long long)rv
->seq
);
376 log_rinfo(ls
, "%s %llu error %d", __func__
,
377 (unsigned long long)rv
->seq
, error
);
379 /* let new_lockspace() get aware of critical error */
380 ls
->ls_recovery_result
= error
;
381 complete(&ls
->ls_recovery_done
);
390 static int dlm_recoverd(void *arg
)
394 ls
= dlm_find_lockspace_local(arg
);
396 log_print("dlm_recoverd: no lockspace %p", arg
);
400 down_write(&ls
->ls_in_recovery
);
401 set_bit(LSFL_RECOVER_LOCK
, &ls
->ls_flags
);
402 wake_up(&ls
->ls_recover_lock_wait
);
406 * We call kthread_should_stop() after set_current_state().
407 * This is because it works correctly if kthread_stop() is
408 * called just before set_current_state().
410 set_current_state(TASK_INTERRUPTIBLE
);
411 if (kthread_should_stop()) {
412 set_current_state(TASK_RUNNING
);
415 if (!test_bit(LSFL_RECOVER_WORK
, &ls
->ls_flags
) &&
416 !test_bit(LSFL_RECOVER_DOWN
, &ls
->ls_flags
)) {
417 if (kthread_should_stop())
421 set_current_state(TASK_RUNNING
);
423 if (test_and_clear_bit(LSFL_RECOVER_DOWN
, &ls
->ls_flags
)) {
424 down_write(&ls
->ls_in_recovery
);
425 set_bit(LSFL_RECOVER_LOCK
, &ls
->ls_flags
);
426 wake_up(&ls
->ls_recover_lock_wait
);
429 if (test_and_clear_bit(LSFL_RECOVER_WORK
, &ls
->ls_flags
))
433 if (test_bit(LSFL_RECOVER_LOCK
, &ls
->ls_flags
))
434 up_write(&ls
->ls_in_recovery
);
436 dlm_put_lockspace(ls
);
440 int dlm_recoverd_start(struct dlm_ls
*ls
)
442 struct task_struct
*p
;
445 p
= kthread_run(dlm_recoverd
, ls
, "dlm_recoverd");
449 ls
->ls_recoverd_task
= p
;
453 void dlm_recoverd_stop(struct dlm_ls
*ls
)
455 kthread_stop(ls
->ls_recoverd_task
);
458 void dlm_recoverd_suspend(struct dlm_ls
*ls
)
460 wake_up(&ls
->ls_wait_general
);
461 mutex_lock(&ls
->ls_recoverd_active
);
464 void dlm_recoverd_resume(struct dlm_ls
*ls
)
466 mutex_unlock(&ls
->ls_recoverd_active
);