blob: 3c025fe49ad3c0846455aeb77863d990f071e74a [file] [log] [blame]
David Teiglande7fd4172006-01-18 09:30:29 +00001/******************************************************************************
2*******************************************************************************
3**
4** Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
5** Copyright (C) 2004-2005 Red Hat, Inc. All rights reserved.
6**
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 "dir.h"
17#include "config.h"
18#include "ast.h"
19#include "memory.h"
20#include "rcom.h"
21#include "lock.h"
22#include "lowcomms.h"
23#include "member.h"
24#include "recover.h"
25
26
27/*
28 * Recovery waiting routines: these functions wait for a particular reply from
29 * a remote node, or for the remote node to report a certain status. They need
30 * to abort if the lockspace is stopped indicating a node has failed (perhaps
31 * the one being waited for).
32 */
33
34/*
35 * Wait until given function returns non-zero or lockspace is stopped
36 * (LS_RECOVERY_STOP set due to failure of a node in ls_nodes). When another
37 * function thinks it could have completed the waited-on task, they should wake
38 * up ls_wait_general to get an immediate response rather than waiting for the
39 * timer to detect the result. A timer wakes us up periodically while waiting
40 * to see if we should abort due to a node failure. This should only be called
41 * by the dlm_recoverd thread.
42 */
43
44static void dlm_wait_timer_fn(unsigned long data)
45{
46 struct dlm_ls *ls = (struct dlm_ls *) data;
David Teigland68c817a2007-01-09 09:41:48 -060047 mod_timer(&ls->ls_timer, jiffies + (dlm_config.ci_recover_timer * HZ));
David Teiglande7fd4172006-01-18 09:30:29 +000048 wake_up(&ls->ls_wait_general);
49}
50
51int dlm_wait_function(struct dlm_ls *ls, int (*testfn) (struct dlm_ls *ls))
52{
53 int error = 0;
54
55 init_timer(&ls->ls_timer);
56 ls->ls_timer.function = dlm_wait_timer_fn;
57 ls->ls_timer.data = (long) ls;
David Teigland68c817a2007-01-09 09:41:48 -060058 ls->ls_timer.expires = jiffies + (dlm_config.ci_recover_timer * HZ);
David Teiglande7fd4172006-01-18 09:30:29 +000059 add_timer(&ls->ls_timer);
60
61 wait_event(ls->ls_wait_general, testfn(ls) || dlm_recovery_stopped(ls));
62 del_timer_sync(&ls->ls_timer);
63
64 if (dlm_recovery_stopped(ls)) {
65 log_debug(ls, "dlm_wait_function aborted");
66 error = -EINTR;
67 }
68 return error;
69}
70
71/*
72 * An efficient way for all nodes to wait for all others to have a certain
73 * status. The node with the lowest nodeid polls all the others for their
74 * status (wait_status_all) and all the others poll the node with the low id
75 * for its accumulated result (wait_status_low). When all nodes have set
76 * status flag X, then status flag X_ALL will be set on the low nodeid.
77 */
78
79uint32_t dlm_recover_status(struct dlm_ls *ls)
80{
81 uint32_t status;
82 spin_lock(&ls->ls_recover_lock);
83 status = ls->ls_recover_status;
84 spin_unlock(&ls->ls_recover_lock);
85 return status;
86}
87
David Teigland757a4272011-10-20 13:26:28 -050088static void _set_recover_status(struct dlm_ls *ls, uint32_t status)
89{
90 ls->ls_recover_status |= status;
91}
92
David Teiglande7fd4172006-01-18 09:30:29 +000093void dlm_set_recover_status(struct dlm_ls *ls, uint32_t status)
94{
95 spin_lock(&ls->ls_recover_lock);
David Teigland757a4272011-10-20 13:26:28 -050096 _set_recover_status(ls, status);
David Teiglande7fd4172006-01-18 09:30:29 +000097 spin_unlock(&ls->ls_recover_lock);
98}
99
David Teigland757a4272011-10-20 13:26:28 -0500100static int wait_status_all(struct dlm_ls *ls, uint32_t wait_status,
101 int save_slots)
David Teiglande7fd4172006-01-18 09:30:29 +0000102{
Al Viro40076852008-01-25 03:01:51 -0500103 struct dlm_rcom *rc = ls->ls_recover_buf;
David Teiglande7fd4172006-01-18 09:30:29 +0000104 struct dlm_member *memb;
105 int error = 0, delay;
106
107 list_for_each_entry(memb, &ls->ls_nodes, list) {
108 delay = 0;
109 for (;;) {
110 if (dlm_recovery_stopped(ls)) {
111 error = -EINTR;
112 goto out;
113 }
114
David Teigland757a4272011-10-20 13:26:28 -0500115 error = dlm_rcom_status(ls, memb->nodeid, 0);
David Teiglande7fd4172006-01-18 09:30:29 +0000116 if (error)
117 goto out;
118
David Teigland757a4272011-10-20 13:26:28 -0500119 if (save_slots)
120 dlm_slot_save(ls, rc, memb);
121
David Teiglande7fd4172006-01-18 09:30:29 +0000122 if (rc->rc_result & wait_status)
123 break;
124 if (delay < 1000)
125 delay += 20;
126 msleep(delay);
127 }
128 }
129 out:
130 return error;
131}
132
David Teigland757a4272011-10-20 13:26:28 -0500133static int wait_status_low(struct dlm_ls *ls, uint32_t wait_status,
134 uint32_t status_flags)
David Teiglande7fd4172006-01-18 09:30:29 +0000135{
Al Viro40076852008-01-25 03:01:51 -0500136 struct dlm_rcom *rc = ls->ls_recover_buf;
David Teiglande7fd4172006-01-18 09:30:29 +0000137 int error = 0, delay = 0, nodeid = ls->ls_low_nodeid;
138
139 for (;;) {
140 if (dlm_recovery_stopped(ls)) {
141 error = -EINTR;
142 goto out;
143 }
144
David Teigland757a4272011-10-20 13:26:28 -0500145 error = dlm_rcom_status(ls, nodeid, status_flags);
David Teiglande7fd4172006-01-18 09:30:29 +0000146 if (error)
147 break;
148
149 if (rc->rc_result & wait_status)
150 break;
151 if (delay < 1000)
152 delay += 20;
153 msleep(delay);
154 }
155 out:
156 return error;
157}
158
159static int wait_status(struct dlm_ls *ls, uint32_t status)
160{
161 uint32_t status_all = status << 1;
162 int error;
163
164 if (ls->ls_low_nodeid == dlm_our_nodeid()) {
David Teigland757a4272011-10-20 13:26:28 -0500165 error = wait_status_all(ls, status, 0);
David Teiglande7fd4172006-01-18 09:30:29 +0000166 if (!error)
167 dlm_set_recover_status(ls, status_all);
168 } else
David Teigland757a4272011-10-20 13:26:28 -0500169 error = wait_status_low(ls, status_all, 0);
David Teiglande7fd4172006-01-18 09:30:29 +0000170
171 return error;
172}
173
174int dlm_recover_members_wait(struct dlm_ls *ls)
175{
David Teigland757a4272011-10-20 13:26:28 -0500176 struct dlm_member *memb;
177 struct dlm_slot *slots;
178 int num_slots, slots_size;
179 int error, rv;
180 uint32_t gen;
181
182 list_for_each_entry(memb, &ls->ls_nodes, list) {
183 memb->slot = -1;
184 memb->generation = 0;
185 }
186
187 if (ls->ls_low_nodeid == dlm_our_nodeid()) {
188 error = wait_status_all(ls, DLM_RS_NODES, 1);
189 if (error)
190 goto out;
191
192 /* slots array is sparse, slots_size may be > num_slots */
193
194 rv = dlm_slots_assign(ls, &num_slots, &slots_size, &slots, &gen);
195 if (!rv) {
196 spin_lock(&ls->ls_recover_lock);
197 _set_recover_status(ls, DLM_RS_NODES_ALL);
198 ls->ls_num_slots = num_slots;
199 ls->ls_slots_size = slots_size;
200 ls->ls_slots = slots;
201 ls->ls_generation = gen;
202 spin_unlock(&ls->ls_recover_lock);
203 } else {
204 dlm_set_recover_status(ls, DLM_RS_NODES_ALL);
205 }
206 } else {
207 error = wait_status_low(ls, DLM_RS_NODES_ALL, DLM_RSF_NEED_SLOTS);
208 if (error)
209 goto out;
210
211 dlm_slots_copy_in(ls);
212 }
213 out:
214 return error;
David Teiglande7fd4172006-01-18 09:30:29 +0000215}
216
217int dlm_recover_directory_wait(struct dlm_ls *ls)
218{
219 return wait_status(ls, DLM_RS_DIR);
220}
221
222int dlm_recover_locks_wait(struct dlm_ls *ls)
223{
224 return wait_status(ls, DLM_RS_LOCKS);
225}
226
227int dlm_recover_done_wait(struct dlm_ls *ls)
228{
229 return wait_status(ls, DLM_RS_DONE);
230}
231
232/*
233 * The recover_list contains all the rsb's for which we've requested the new
234 * master nodeid. As replies are returned from the resource directories the
235 * rsb's are removed from the list. When the list is empty we're done.
236 *
237 * The recover_list is later similarly used for all rsb's for which we've sent
238 * new lkb's and need to receive new corresponding lkid's.
239 *
240 * We use the address of the rsb struct as a simple local identifier for the
241 * rsb so we can match an rcom reply with the rsb it was sent for.
242 */
243
244static int recover_list_empty(struct dlm_ls *ls)
245{
246 int empty;
247
248 spin_lock(&ls->ls_recover_list_lock);
249 empty = list_empty(&ls->ls_recover_list);
250 spin_unlock(&ls->ls_recover_list_lock);
251
252 return empty;
253}
254
255static void recover_list_add(struct dlm_rsb *r)
256{
257 struct dlm_ls *ls = r->res_ls;
258
259 spin_lock(&ls->ls_recover_list_lock);
260 if (list_empty(&r->res_recover_list)) {
261 list_add_tail(&r->res_recover_list, &ls->ls_recover_list);
262 ls->ls_recover_list_count++;
263 dlm_hold_rsb(r);
264 }
265 spin_unlock(&ls->ls_recover_list_lock);
266}
267
268static void recover_list_del(struct dlm_rsb *r)
269{
270 struct dlm_ls *ls = r->res_ls;
271
272 spin_lock(&ls->ls_recover_list_lock);
273 list_del_init(&r->res_recover_list);
274 ls->ls_recover_list_count--;
275 spin_unlock(&ls->ls_recover_list_lock);
276
277 dlm_put_rsb(r);
278}
279
280static struct dlm_rsb *recover_list_find(struct dlm_ls *ls, uint64_t id)
281{
282 struct dlm_rsb *r = NULL;
283
284 spin_lock(&ls->ls_recover_list_lock);
285
286 list_for_each_entry(r, &ls->ls_recover_list, res_recover_list) {
287 if (id == (unsigned long) r)
288 goto out;
289 }
290 r = NULL;
291 out:
292 spin_unlock(&ls->ls_recover_list_lock);
293 return r;
294}
295
296static void recover_list_clear(struct dlm_ls *ls)
297{
298 struct dlm_rsb *r, *s;
299
300 spin_lock(&ls->ls_recover_list_lock);
301 list_for_each_entry_safe(r, s, &ls->ls_recover_list, res_recover_list) {
302 list_del_init(&r->res_recover_list);
David Teigland52069802006-11-02 09:49:02 -0600303 r->res_recover_locks_count = 0;
David Teiglande7fd4172006-01-18 09:30:29 +0000304 dlm_put_rsb(r);
305 ls->ls_recover_list_count--;
306 }
307
308 if (ls->ls_recover_list_count != 0) {
309 log_error(ls, "warning: recover_list_count %d",
310 ls->ls_recover_list_count);
311 ls->ls_recover_list_count = 0;
312 }
313 spin_unlock(&ls->ls_recover_list_lock);
314}
315
316
317/* Master recovery: find new master node for rsb's that were
318 mastered on nodes that have been removed.
319
320 dlm_recover_masters
321 recover_master
322 dlm_send_rcom_lookup -> receive_rcom_lookup
323 dlm_dir_lookup
324 receive_rcom_lookup_reply <-
325 dlm_recover_master_reply
326 set_new_master
327 set_master_lkbs
328 set_lock_master
329*/
330
331/*
332 * Set the lock master for all LKBs in a lock queue
333 * If we are the new master of the rsb, we may have received new
334 * MSTCPY locks from other nodes already which we need to ignore
335 * when setting the new nodeid.
336 */
337
338static void set_lock_master(struct list_head *queue, int nodeid)
339{
340 struct dlm_lkb *lkb;
341
David Teigland48756472012-04-26 15:54:29 -0500342 list_for_each_entry(lkb, queue, lkb_statequeue) {
343 if (!(lkb->lkb_flags & DLM_IFL_MSTCPY)) {
David Teiglande7fd4172006-01-18 09:30:29 +0000344 lkb->lkb_nodeid = nodeid;
David Teigland48756472012-04-26 15:54:29 -0500345 lkb->lkb_remid = 0;
346 }
347 }
David Teiglande7fd4172006-01-18 09:30:29 +0000348}
349
350static void set_master_lkbs(struct dlm_rsb *r)
351{
352 set_lock_master(&r->res_grantqueue, r->res_nodeid);
353 set_lock_master(&r->res_convertqueue, r->res_nodeid);
354 set_lock_master(&r->res_waitqueue, r->res_nodeid);
355}
356
357/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300358 * Propagate the new master nodeid to locks
David Teiglande7fd4172006-01-18 09:30:29 +0000359 * The NEW_MASTER flag tells dlm_recover_locks() which rsb's to consider.
David Teigland48756472012-04-26 15:54:29 -0500360 * The NEW_MASTER2 flag tells recover_lvb() and recover_grant() which
David Teiglandf7da7902006-07-25 13:53:33 -0500361 * rsb's to consider.
David Teiglande7fd4172006-01-18 09:30:29 +0000362 */
363
David Teiglandc04fecb2012-05-10 10:18:07 -0500364static void set_new_master(struct dlm_rsb *r)
David Teiglande7fd4172006-01-18 09:30:29 +0000365{
David Teiglande7fd4172006-01-18 09:30:29 +0000366 set_master_lkbs(r);
367 rsb_set_flag(r, RSB_NEW_MASTER);
368 rsb_set_flag(r, RSB_NEW_MASTER2);
David Teiglande7fd4172006-01-18 09:30:29 +0000369}
370
371/*
372 * We do async lookups on rsb's that need new masters. The rsb's
373 * waiting for a lookup reply are kept on the recover_list.
David Teiglandc04fecb2012-05-10 10:18:07 -0500374 *
375 * Another node recovering the master may have sent us a rcom lookup,
376 * and our dlm_master_lookup() set it as the new master, along with
377 * NEW_MASTER so that we'll recover it here (this implies dir_nodeid
378 * equals our_nodeid below).
David Teiglande7fd4172006-01-18 09:30:29 +0000379 */
380
David Teiglandc04fecb2012-05-10 10:18:07 -0500381static int recover_master(struct dlm_rsb *r, unsigned int *count)
David Teiglande7fd4172006-01-18 09:30:29 +0000382{
383 struct dlm_ls *ls = r->res_ls;
David Teiglandc04fecb2012-05-10 10:18:07 -0500384 int our_nodeid, dir_nodeid;
385 int is_removed = 0;
386 int error;
387
388 if (is_master(r))
389 return 0;
390
391 is_removed = dlm_is_removed(ls, r->res_nodeid);
392
393 if (!is_removed && !rsb_flag(r, RSB_NEW_MASTER))
394 return 0;
395
396 our_nodeid = dlm_our_nodeid();
397 dir_nodeid = dlm_dir_nodeid(r);
David Teiglande7fd4172006-01-18 09:30:29 +0000398
399 if (dir_nodeid == our_nodeid) {
David Teiglandc04fecb2012-05-10 10:18:07 -0500400 if (is_removed) {
401 r->res_master_nodeid = our_nodeid;
402 r->res_nodeid = 0;
403 }
David Teiglande7fd4172006-01-18 09:30:29 +0000404
David Teiglandc04fecb2012-05-10 10:18:07 -0500405 /* set master of lkbs to ourself when is_removed, or to
406 another new master which we set along with NEW_MASTER
407 in dlm_master_lookup */
408 set_new_master(r);
409 error = 0;
David Teiglande7fd4172006-01-18 09:30:29 +0000410 } else {
411 recover_list_add(r);
412 error = dlm_send_rcom_lookup(r, dir_nodeid);
413 }
414
David Teiglandc04fecb2012-05-10 10:18:07 -0500415 (*count)++;
David Teiglande7fd4172006-01-18 09:30:29 +0000416 return error;
417}
418
419/*
David Teigland48756472012-04-26 15:54:29 -0500420 * All MSTCPY locks are purged and rebuilt, even if the master stayed the same.
421 * This is necessary because recovery can be started, aborted and restarted,
422 * causing the master nodeid to briefly change during the aborted recovery, and
423 * change back to the original value in the second recovery. The MSTCPY locks
424 * may or may not have been purged during the aborted recovery. Another node
425 * with an outstanding request in waiters list and a request reply saved in the
426 * requestqueue, cannot know whether it should ignore the reply and resend the
427 * request, or accept the reply and complete the request. It must do the
428 * former if the remote node purged MSTCPY locks, and it must do the later if
429 * the remote node did not. This is solved by always purging MSTCPY locks, in
430 * which case, the request reply would always be ignored and the request
431 * resent.
David Teiglande7fd4172006-01-18 09:30:29 +0000432 */
433
David Teiglandc04fecb2012-05-10 10:18:07 -0500434static int recover_master_static(struct dlm_rsb *r, unsigned int *count)
David Teiglande7fd4172006-01-18 09:30:29 +0000435{
David Teigland48756472012-04-26 15:54:29 -0500436 int dir_nodeid = dlm_dir_nodeid(r);
437 int new_master = dir_nodeid;
David Teiglande7fd4172006-01-18 09:30:29 +0000438
David Teigland48756472012-04-26 15:54:29 -0500439 if (dir_nodeid == dlm_our_nodeid())
440 new_master = 0;
David Teiglande7fd4172006-01-18 09:30:29 +0000441
David Teigland48756472012-04-26 15:54:29 -0500442 dlm_purge_mstcpy_locks(r);
David Teiglandc04fecb2012-05-10 10:18:07 -0500443 r->res_master_nodeid = dir_nodeid;
444 r->res_nodeid = new_master;
445 set_new_master(r);
446 (*count)++;
447 return 0;
David Teiglande7fd4172006-01-18 09:30:29 +0000448}
449
450/*
451 * Go through local root resources and for each rsb which has a master which
452 * has departed, get the new master nodeid from the directory. The dir will
453 * assign mastery to the first node to look up the new master. That means
454 * we'll discover in this lookup if we're the new master of any rsb's.
455 *
456 * We fire off all the dir lookup requests individually and asynchronously to
457 * the correct dir node.
458 */
459
460int dlm_recover_masters(struct dlm_ls *ls)
461{
462 struct dlm_rsb *r;
David Teiglandc04fecb2012-05-10 10:18:07 -0500463 unsigned int total = 0;
464 unsigned int count = 0;
465 int nodir = dlm_no_directory(ls);
466 int error;
David Teiglande7fd4172006-01-18 09:30:29 +0000467
468 log_debug(ls, "dlm_recover_masters");
469
470 down_read(&ls->ls_root_sem);
471 list_for_each_entry(r, &ls->ls_root_list, res_root_list) {
472 if (dlm_recovery_stopped(ls)) {
473 up_read(&ls->ls_root_sem);
474 error = -EINTR;
475 goto out;
476 }
477
David Teiglandc04fecb2012-05-10 10:18:07 -0500478 lock_rsb(r);
479 if (nodir)
480 error = recover_master_static(r, &count);
481 else
482 error = recover_master(r, &count);
483 unlock_rsb(r);
484 cond_resched();
485 total++;
David Teiglande7fd4172006-01-18 09:30:29 +0000486
David Teiglandc04fecb2012-05-10 10:18:07 -0500487 if (error) {
488 up_read(&ls->ls_root_sem);
489 goto out;
490 }
David Teiglande7fd4172006-01-18 09:30:29 +0000491 }
492 up_read(&ls->ls_root_sem);
493
David Teiglandc04fecb2012-05-10 10:18:07 -0500494 log_debug(ls, "dlm_recover_masters %u of %u", count, total);
David Teiglande7fd4172006-01-18 09:30:29 +0000495
496 error = dlm_wait_function(ls, &recover_list_empty);
497 out:
498 if (error)
499 recover_list_clear(ls);
500 return error;
501}
502
503int dlm_recover_master_reply(struct dlm_ls *ls, struct dlm_rcom *rc)
504{
505 struct dlm_rsb *r;
David Teiglandc04fecb2012-05-10 10:18:07 -0500506 int ret_nodeid, new_master;
David Teiglande7fd4172006-01-18 09:30:29 +0000507
508 r = recover_list_find(ls, rc->rc_id);
509 if (!r) {
David Teigland90135922006-01-20 08:47:07 +0000510 log_error(ls, "dlm_recover_master_reply no id %llx",
David Teigland9229f012006-05-24 09:21:30 -0400511 (unsigned long long)rc->rc_id);
David Teiglande7fd4172006-01-18 09:30:29 +0000512 goto out;
513 }
514
David Teiglandc04fecb2012-05-10 10:18:07 -0500515 ret_nodeid = rc->rc_result;
516
517 if (ret_nodeid == dlm_our_nodeid())
518 new_master = 0;
519 else
520 new_master = ret_nodeid;
David Teiglande7fd4172006-01-18 09:30:29 +0000521
David Teigland48756472012-04-26 15:54:29 -0500522 lock_rsb(r);
David Teiglandc04fecb2012-05-10 10:18:07 -0500523 r->res_master_nodeid = ret_nodeid;
524 r->res_nodeid = new_master;
525 set_new_master(r);
David Teigland48756472012-04-26 15:54:29 -0500526 unlock_rsb(r);
David Teiglande7fd4172006-01-18 09:30:29 +0000527 recover_list_del(r);
528
529 if (recover_list_empty(ls))
530 wake_up(&ls->ls_wait_general);
531 out:
532 return 0;
533}
534
535
536/* Lock recovery: rebuild the process-copy locks we hold on a
537 remastered rsb on the new rsb master.
538
539 dlm_recover_locks
540 recover_locks
541 recover_locks_queue
542 dlm_send_rcom_lock -> receive_rcom_lock
543 dlm_recover_master_copy
544 receive_rcom_lock_reply <-
545 dlm_recover_process_copy
546*/
547
548
549/*
550 * keep a count of the number of lkb's we send to the new master; when we get
551 * an equal number of replies then recovery for the rsb is done
552 */
553
554static int recover_locks_queue(struct dlm_rsb *r, struct list_head *head)
555{
556 struct dlm_lkb *lkb;
557 int error = 0;
558
559 list_for_each_entry(lkb, head, lkb_statequeue) {
560 error = dlm_send_rcom_lock(r, lkb);
561 if (error)
562 break;
563 r->res_recover_locks_count++;
564 }
565
566 return error;
567}
568
David Teiglande7fd4172006-01-18 09:30:29 +0000569static int recover_locks(struct dlm_rsb *r)
570{
571 int error = 0;
572
573 lock_rsb(r);
David Teiglande7fd4172006-01-18 09:30:29 +0000574
David Teiglanda345da32006-08-18 11:54:25 -0500575 DLM_ASSERT(!r->res_recover_locks_count, dlm_dump_rsb(r););
David Teiglande7fd4172006-01-18 09:30:29 +0000576
577 error = recover_locks_queue(r, &r->res_grantqueue);
578 if (error)
579 goto out;
580 error = recover_locks_queue(r, &r->res_convertqueue);
581 if (error)
582 goto out;
583 error = recover_locks_queue(r, &r->res_waitqueue);
584 if (error)
585 goto out;
586
587 if (r->res_recover_locks_count)
588 recover_list_add(r);
589 else
590 rsb_clear_flag(r, RSB_NEW_MASTER);
591 out:
592 unlock_rsb(r);
593 return error;
594}
595
596int dlm_recover_locks(struct dlm_ls *ls)
597{
598 struct dlm_rsb *r;
599 int error, count = 0;
600
David Teiglande7fd4172006-01-18 09:30:29 +0000601 down_read(&ls->ls_root_sem);
602 list_for_each_entry(r, &ls->ls_root_list, res_root_list) {
603 if (is_master(r)) {
604 rsb_clear_flag(r, RSB_NEW_MASTER);
605 continue;
606 }
607
608 if (!rsb_flag(r, RSB_NEW_MASTER))
609 continue;
610
611 if (dlm_recovery_stopped(ls)) {
612 error = -EINTR;
613 up_read(&ls->ls_root_sem);
614 goto out;
615 }
616
617 error = recover_locks(r);
618 if (error) {
619 up_read(&ls->ls_root_sem);
620 goto out;
621 }
622
623 count += r->res_recover_locks_count;
624 }
625 up_read(&ls->ls_root_sem);
626
David Teigland48756472012-04-26 15:54:29 -0500627 log_debug(ls, "dlm_recover_locks %d out", count);
David Teiglande7fd4172006-01-18 09:30:29 +0000628
629 error = dlm_wait_function(ls, &recover_list_empty);
630 out:
631 if (error)
632 recover_list_clear(ls);
David Teiglande7fd4172006-01-18 09:30:29 +0000633 return error;
634}
635
636void dlm_recovered_lock(struct dlm_rsb *r)
637{
David Teiglanda345da32006-08-18 11:54:25 -0500638 DLM_ASSERT(rsb_flag(r, RSB_NEW_MASTER), dlm_dump_rsb(r););
David Teiglande7fd4172006-01-18 09:30:29 +0000639
640 r->res_recover_locks_count--;
641 if (!r->res_recover_locks_count) {
642 rsb_clear_flag(r, RSB_NEW_MASTER);
643 recover_list_del(r);
644 }
645
646 if (recover_list_empty(r->res_ls))
647 wake_up(&r->res_ls->ls_wait_general);
648}
649
650/*
651 * The lvb needs to be recovered on all master rsb's. This includes setting
652 * the VALNOTVALID flag if necessary, and determining the correct lvb contents
653 * based on the lvb's of the locks held on the rsb.
654 *
655 * RSB_VALNOTVALID is set if there are only NL/CR locks on the rsb. If it
656 * was already set prior to recovery, it's not cleared, regardless of locks.
657 *
658 * The LVB contents are only considered for changing when this is a new master
659 * of the rsb (NEW_MASTER2). Then, the rsb's lvb is taken from any lkb with
660 * mode > CR. If no lkb's exist with mode above CR, the lvb contents are taken
661 * from the lkb with the largest lvb sequence number.
662 */
663
664static void recover_lvb(struct dlm_rsb *r)
665{
666 struct dlm_lkb *lkb, *high_lkb = NULL;
667 uint32_t high_seq = 0;
David Teigland90135922006-01-20 08:47:07 +0000668 int lock_lvb_exists = 0;
669 int big_lock_exists = 0;
David Teiglande7fd4172006-01-18 09:30:29 +0000670 int lvblen = r->res_ls->ls_lvblen;
671
672 list_for_each_entry(lkb, &r->res_grantqueue, lkb_statequeue) {
673 if (!(lkb->lkb_exflags & DLM_LKF_VALBLK))
674 continue;
675
David Teigland90135922006-01-20 08:47:07 +0000676 lock_lvb_exists = 1;
David Teiglande7fd4172006-01-18 09:30:29 +0000677
678 if (lkb->lkb_grmode > DLM_LOCK_CR) {
David Teigland90135922006-01-20 08:47:07 +0000679 big_lock_exists = 1;
David Teiglande7fd4172006-01-18 09:30:29 +0000680 goto setflag;
681 }
682
683 if (((int)lkb->lkb_lvbseq - (int)high_seq) >= 0) {
684 high_lkb = lkb;
685 high_seq = lkb->lkb_lvbseq;
686 }
687 }
688
689 list_for_each_entry(lkb, &r->res_convertqueue, lkb_statequeue) {
690 if (!(lkb->lkb_exflags & DLM_LKF_VALBLK))
691 continue;
692
David Teigland90135922006-01-20 08:47:07 +0000693 lock_lvb_exists = 1;
David Teiglande7fd4172006-01-18 09:30:29 +0000694
695 if (lkb->lkb_grmode > DLM_LOCK_CR) {
David Teigland90135922006-01-20 08:47:07 +0000696 big_lock_exists = 1;
David Teiglande7fd4172006-01-18 09:30:29 +0000697 goto setflag;
698 }
699
700 if (((int)lkb->lkb_lvbseq - (int)high_seq) >= 0) {
701 high_lkb = lkb;
702 high_seq = lkb->lkb_lvbseq;
703 }
704 }
705
706 setflag:
707 if (!lock_lvb_exists)
708 goto out;
709
710 if (!big_lock_exists)
711 rsb_set_flag(r, RSB_VALNOTVALID);
712
713 /* don't mess with the lvb unless we're the new master */
714 if (!rsb_flag(r, RSB_NEW_MASTER2))
715 goto out;
716
717 if (!r->res_lvbptr) {
David Teigland52bda2b2007-11-07 09:06:49 -0600718 r->res_lvbptr = dlm_allocate_lvb(r->res_ls);
David Teiglande7fd4172006-01-18 09:30:29 +0000719 if (!r->res_lvbptr)
720 goto out;
721 }
722
723 if (big_lock_exists) {
724 r->res_lvbseq = lkb->lkb_lvbseq;
725 memcpy(r->res_lvbptr, lkb->lkb_lvbptr, lvblen);
726 } else if (high_lkb) {
727 r->res_lvbseq = high_lkb->lkb_lvbseq;
728 memcpy(r->res_lvbptr, high_lkb->lkb_lvbptr, lvblen);
729 } else {
730 r->res_lvbseq = 0;
731 memset(r->res_lvbptr, 0, lvblen);
732 }
733 out:
734 return;
735}
736
737/* All master rsb's flagged RECOVER_CONVERT need to be looked at. The locks
738 converting PR->CW or CW->PR need to have their lkb_grmode set. */
739
740static void recover_conversion(struct dlm_rsb *r)
741{
742 struct dlm_lkb *lkb;
743 int grmode = -1;
744
745 list_for_each_entry(lkb, &r->res_grantqueue, lkb_statequeue) {
746 if (lkb->lkb_grmode == DLM_LOCK_PR ||
747 lkb->lkb_grmode == DLM_LOCK_CW) {
748 grmode = lkb->lkb_grmode;
749 break;
750 }
751 }
752
753 list_for_each_entry(lkb, &r->res_convertqueue, lkb_statequeue) {
754 if (lkb->lkb_grmode != DLM_LOCK_IV)
755 continue;
756 if (grmode == -1)
757 lkb->lkb_grmode = lkb->lkb_rqmode;
758 else
759 lkb->lkb_grmode = grmode;
760 }
761}
762
David Teiglandf7da7902006-07-25 13:53:33 -0500763/* We've become the new master for this rsb and waiting/converting locks may
David Teigland48756472012-04-26 15:54:29 -0500764 need to be granted in dlm_recover_grant() due to locks that may have
David Teiglandf7da7902006-07-25 13:53:33 -0500765 existed from a removed node. */
766
David Teigland48756472012-04-26 15:54:29 -0500767static void recover_grant(struct dlm_rsb *r)
David Teiglandf7da7902006-07-25 13:53:33 -0500768{
769 if (!list_empty(&r->res_waitqueue) || !list_empty(&r->res_convertqueue))
David Teigland48756472012-04-26 15:54:29 -0500770 rsb_set_flag(r, RSB_RECOVER_GRANT);
David Teiglandf7da7902006-07-25 13:53:33 -0500771}
772
David Teiglande7fd4172006-01-18 09:30:29 +0000773void dlm_recover_rsbs(struct dlm_ls *ls)
774{
775 struct dlm_rsb *r;
David Teigland48756472012-04-26 15:54:29 -0500776 unsigned int count = 0;
David Teiglande7fd4172006-01-18 09:30:29 +0000777
778 down_read(&ls->ls_root_sem);
779 list_for_each_entry(r, &ls->ls_root_list, res_root_list) {
780 lock_rsb(r);
781 if (is_master(r)) {
782 if (rsb_flag(r, RSB_RECOVER_CONVERT))
783 recover_conversion(r);
David Teiglandf7da7902006-07-25 13:53:33 -0500784 if (rsb_flag(r, RSB_NEW_MASTER2))
David Teigland48756472012-04-26 15:54:29 -0500785 recover_grant(r);
David Teiglande7fd4172006-01-18 09:30:29 +0000786 recover_lvb(r);
787 count++;
788 }
789 rsb_clear_flag(r, RSB_RECOVER_CONVERT);
David Teiglandf7da7902006-07-25 13:53:33 -0500790 rsb_clear_flag(r, RSB_NEW_MASTER2);
David Teiglande7fd4172006-01-18 09:30:29 +0000791 unlock_rsb(r);
792 }
793 up_read(&ls->ls_root_sem);
794
David Teigland48756472012-04-26 15:54:29 -0500795 if (count)
796 log_debug(ls, "dlm_recover_rsbs %d done", count);
David Teiglande7fd4172006-01-18 09:30:29 +0000797}
798
799/* Create a single list of all root rsb's to be used during recovery */
800
801int dlm_create_root_list(struct dlm_ls *ls)
802{
Bob Peterson9beb3bf2011-10-26 15:24:55 -0500803 struct rb_node *n;
David Teiglande7fd4172006-01-18 09:30:29 +0000804 struct dlm_rsb *r;
805 int i, error = 0;
806
807 down_write(&ls->ls_root_sem);
808 if (!list_empty(&ls->ls_root_list)) {
809 log_error(ls, "root list not empty");
810 error = -EINVAL;
811 goto out;
812 }
813
814 for (i = 0; i < ls->ls_rsbtbl_size; i++) {
David Teiglandc7be7612009-01-07 16:50:41 -0600815 spin_lock(&ls->ls_rsbtbl[i].lock);
Bob Peterson9beb3bf2011-10-26 15:24:55 -0500816 for (n = rb_first(&ls->ls_rsbtbl[i].keep); n; n = rb_next(n)) {
817 r = rb_entry(n, struct dlm_rsb, res_hashnode);
David Teiglande7fd4172006-01-18 09:30:29 +0000818 list_add(&r->res_root_list, &ls->ls_root_list);
819 dlm_hold_rsb(r);
820 }
David Teigland85f03792008-01-16 13:02:31 -0600821
David Teiglandc04fecb2012-05-10 10:18:07 -0500822 if (!RB_EMPTY_ROOT(&ls->ls_rsbtbl[i].toss))
823 log_error(ls, "dlm_create_root_list toss not empty");
David Teiglandc7be7612009-01-07 16:50:41 -0600824 spin_unlock(&ls->ls_rsbtbl[i].lock);
David Teiglande7fd4172006-01-18 09:30:29 +0000825 }
826 out:
827 up_write(&ls->ls_root_sem);
828 return error;
829}
830
831void dlm_release_root_list(struct dlm_ls *ls)
832{
833 struct dlm_rsb *r, *safe;
834
835 down_write(&ls->ls_root_sem);
836 list_for_each_entry_safe(r, safe, &ls->ls_root_list, res_root_list) {
837 list_del_init(&r->res_root_list);
838 dlm_put_rsb(r);
839 }
840 up_write(&ls->ls_root_sem);
841}
842
David Teiglandc04fecb2012-05-10 10:18:07 -0500843void dlm_clear_toss(struct dlm_ls *ls)
David Teiglande7fd4172006-01-18 09:30:29 +0000844{
Bob Peterson9beb3bf2011-10-26 15:24:55 -0500845 struct rb_node *n, *next;
David Teiglandc04fecb2012-05-10 10:18:07 -0500846 struct dlm_rsb *r;
847 unsigned int count = 0;
David Teiglande7fd4172006-01-18 09:30:29 +0000848 int i;
849
850 for (i = 0; i < ls->ls_rsbtbl_size; i++) {
David Teiglandc7be7612009-01-07 16:50:41 -0600851 spin_lock(&ls->ls_rsbtbl[i].lock);
Bob Peterson9beb3bf2011-10-26 15:24:55 -0500852 for (n = rb_first(&ls->ls_rsbtbl[i].toss); n; n = next) {
David Teiglandc04fecb2012-05-10 10:18:07 -0500853 next = rb_next(n);
854 r = rb_entry(n, struct dlm_rsb, res_hashnode);
855 rb_erase(n, &ls->ls_rsbtbl[i].toss);
856 dlm_free_rsb(r);
857 count++;
David Teiglande7fd4172006-01-18 09:30:29 +0000858 }
David Teiglandc7be7612009-01-07 16:50:41 -0600859 spin_unlock(&ls->ls_rsbtbl[i].lock);
David Teiglande7fd4172006-01-18 09:30:29 +0000860 }
David Teiglandc04fecb2012-05-10 10:18:07 -0500861
862 if (count)
863 log_debug(ls, "dlm_clear_toss %u done", count);
David Teiglande7fd4172006-01-18 09:30:29 +0000864}
865