Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /******************************************************************************
0003 *******************************************************************************
0004 **
0005 **  Copyright (C) Sistina Software, Inc.  1997-2003  All rights reserved.
0006 **  Copyright (C) 2004-2005 Red Hat, Inc.  All rights reserved.
0007 **
0008 **
0009 *******************************************************************************
0010 ******************************************************************************/
0011 
0012 #include "dlm_internal.h"
0013 #include "lockspace.h"
0014 #include "dir.h"
0015 #include "config.h"
0016 #include "ast.h"
0017 #include "memory.h"
0018 #include "rcom.h"
0019 #include "lock.h"
0020 #include "lowcomms.h"
0021 #include "member.h"
0022 #include "recover.h"
0023 
0024 
0025 /*
0026  * Recovery waiting routines: these functions wait for a particular reply from
0027  * a remote node, or for the remote node to report a certain status.  They need
0028  * to abort if the lockspace is stopped indicating a node has failed (perhaps
0029  * the one being waited for).
0030  */
0031 
0032 /*
0033  * Wait until given function returns non-zero or lockspace is stopped
0034  * (LS_RECOVERY_STOP set due to failure of a node in ls_nodes).  When another
0035  * function thinks it could have completed the waited-on task, they should wake
0036  * up ls_wait_general to get an immediate response rather than waiting for the
0037  * timeout.  This uses a timeout so it can check periodically if the wait
0038  * should abort due to node failure (which doesn't cause a wake_up).
0039  * This should only be called by the dlm_recoverd thread.
0040  */
0041 
0042 int dlm_wait_function(struct dlm_ls *ls, int (*testfn) (struct dlm_ls *ls))
0043 {
0044     int error = 0;
0045     int rv;
0046 
0047     while (1) {
0048         rv = wait_event_timeout(ls->ls_wait_general,
0049                     testfn(ls) || dlm_recovery_stopped(ls),
0050                     dlm_config.ci_recover_timer * HZ);
0051         if (rv)
0052             break;
0053         if (test_bit(LSFL_RCOM_WAIT, &ls->ls_flags)) {
0054             log_debug(ls, "dlm_wait_function timed out");
0055             return -ETIMEDOUT;
0056         }
0057     }
0058 
0059     if (dlm_recovery_stopped(ls)) {
0060         log_debug(ls, "dlm_wait_function aborted");
0061         error = -EINTR;
0062     }
0063     return error;
0064 }
0065 
0066 /*
0067  * An efficient way for all nodes to wait for all others to have a certain
0068  * status.  The node with the lowest nodeid polls all the others for their
0069  * status (wait_status_all) and all the others poll the node with the low id
0070  * for its accumulated result (wait_status_low).  When all nodes have set
0071  * status flag X, then status flag X_ALL will be set on the low nodeid.
0072  */
0073 
0074 uint32_t dlm_recover_status(struct dlm_ls *ls)
0075 {
0076     uint32_t status;
0077     spin_lock(&ls->ls_recover_lock);
0078     status = ls->ls_recover_status;
0079     spin_unlock(&ls->ls_recover_lock);
0080     return status;
0081 }
0082 
0083 static void _set_recover_status(struct dlm_ls *ls, uint32_t status)
0084 {
0085     ls->ls_recover_status |= status;
0086 }
0087 
0088 void dlm_set_recover_status(struct dlm_ls *ls, uint32_t status)
0089 {
0090     spin_lock(&ls->ls_recover_lock);
0091     _set_recover_status(ls, status);
0092     spin_unlock(&ls->ls_recover_lock);
0093 }
0094 
0095 static int wait_status_all(struct dlm_ls *ls, uint32_t wait_status,
0096                int save_slots)
0097 {
0098     struct dlm_rcom *rc = ls->ls_recover_buf;
0099     struct dlm_member *memb;
0100     int error = 0, delay;
0101 
0102     list_for_each_entry(memb, &ls->ls_nodes, list) {
0103         delay = 0;
0104         for (;;) {
0105             if (dlm_recovery_stopped(ls)) {
0106                 error = -EINTR;
0107                 goto out;
0108             }
0109 
0110             error = dlm_rcom_status(ls, memb->nodeid, 0);
0111             if (error)
0112                 goto out;
0113 
0114             if (save_slots)
0115                 dlm_slot_save(ls, rc, memb);
0116 
0117             if (le32_to_cpu(rc->rc_result) & wait_status)
0118                 break;
0119             if (delay < 1000)
0120                 delay += 20;
0121             msleep(delay);
0122         }
0123     }
0124  out:
0125     return error;
0126 }
0127 
0128 static int wait_status_low(struct dlm_ls *ls, uint32_t wait_status,
0129                uint32_t status_flags)
0130 {
0131     struct dlm_rcom *rc = ls->ls_recover_buf;
0132     int error = 0, delay = 0, nodeid = ls->ls_low_nodeid;
0133 
0134     for (;;) {
0135         if (dlm_recovery_stopped(ls)) {
0136             error = -EINTR;
0137             goto out;
0138         }
0139 
0140         error = dlm_rcom_status(ls, nodeid, status_flags);
0141         if (error)
0142             break;
0143 
0144         if (le32_to_cpu(rc->rc_result) & wait_status)
0145             break;
0146         if (delay < 1000)
0147             delay += 20;
0148         msleep(delay);
0149     }
0150  out:
0151     return error;
0152 }
0153 
0154 static int wait_status(struct dlm_ls *ls, uint32_t status)
0155 {
0156     uint32_t status_all = status << 1;
0157     int error;
0158 
0159     if (ls->ls_low_nodeid == dlm_our_nodeid()) {
0160         error = wait_status_all(ls, status, 0);
0161         if (!error)
0162             dlm_set_recover_status(ls, status_all);
0163     } else
0164         error = wait_status_low(ls, status_all, 0);
0165 
0166     return error;
0167 }
0168 
0169 int dlm_recover_members_wait(struct dlm_ls *ls)
0170 {
0171     struct dlm_member *memb;
0172     struct dlm_slot *slots;
0173     int num_slots, slots_size;
0174     int error, rv;
0175     uint32_t gen;
0176 
0177     list_for_each_entry(memb, &ls->ls_nodes, list) {
0178         memb->slot = -1;
0179         memb->generation = 0;
0180     }
0181 
0182     if (ls->ls_low_nodeid == dlm_our_nodeid()) {
0183         error = wait_status_all(ls, DLM_RS_NODES, 1);
0184         if (error)
0185             goto out;
0186 
0187         /* slots array is sparse, slots_size may be > num_slots */
0188 
0189         rv = dlm_slots_assign(ls, &num_slots, &slots_size, &slots, &gen);
0190         if (!rv) {
0191             spin_lock(&ls->ls_recover_lock);
0192             _set_recover_status(ls, DLM_RS_NODES_ALL);
0193             ls->ls_num_slots = num_slots;
0194             ls->ls_slots_size = slots_size;
0195             ls->ls_slots = slots;
0196             ls->ls_generation = gen;
0197             spin_unlock(&ls->ls_recover_lock);
0198         } else {
0199             dlm_set_recover_status(ls, DLM_RS_NODES_ALL);
0200         }
0201     } else {
0202         error = wait_status_low(ls, DLM_RS_NODES_ALL, DLM_RSF_NEED_SLOTS);
0203         if (error)
0204             goto out;
0205 
0206         dlm_slots_copy_in(ls);
0207     }
0208  out:
0209     return error;
0210 }
0211 
0212 int dlm_recover_directory_wait(struct dlm_ls *ls)
0213 {
0214     return wait_status(ls, DLM_RS_DIR);
0215 }
0216 
0217 int dlm_recover_locks_wait(struct dlm_ls *ls)
0218 {
0219     return wait_status(ls, DLM_RS_LOCKS);
0220 }
0221 
0222 int dlm_recover_done_wait(struct dlm_ls *ls)
0223 {
0224     return wait_status(ls, DLM_RS_DONE);
0225 }
0226 
0227 /*
0228  * The recover_list contains all the rsb's for which we've requested the new
0229  * master nodeid.  As replies are returned from the resource directories the
0230  * rsb's are removed from the list.  When the list is empty we're done.
0231  *
0232  * The recover_list is later similarly used for all rsb's for which we've sent
0233  * new lkb's and need to receive new corresponding lkid's.
0234  *
0235  * We use the address of the rsb struct as a simple local identifier for the
0236  * rsb so we can match an rcom reply with the rsb it was sent for.
0237  */
0238 
0239 static int recover_list_empty(struct dlm_ls *ls)
0240 {
0241     int empty;
0242 
0243     spin_lock(&ls->ls_recover_list_lock);
0244     empty = list_empty(&ls->ls_recover_list);
0245     spin_unlock(&ls->ls_recover_list_lock);
0246 
0247     return empty;
0248 }
0249 
0250 static void recover_list_add(struct dlm_rsb *r)
0251 {
0252     struct dlm_ls *ls = r->res_ls;
0253 
0254     spin_lock(&ls->ls_recover_list_lock);
0255     if (list_empty(&r->res_recover_list)) {
0256         list_add_tail(&r->res_recover_list, &ls->ls_recover_list);
0257         ls->ls_recover_list_count++;
0258         dlm_hold_rsb(r);
0259     }
0260     spin_unlock(&ls->ls_recover_list_lock);
0261 }
0262 
0263 static void recover_list_del(struct dlm_rsb *r)
0264 {
0265     struct dlm_ls *ls = r->res_ls;
0266 
0267     spin_lock(&ls->ls_recover_list_lock);
0268     list_del_init(&r->res_recover_list);
0269     ls->ls_recover_list_count--;
0270     spin_unlock(&ls->ls_recover_list_lock);
0271 
0272     dlm_put_rsb(r);
0273 }
0274 
0275 static void recover_list_clear(struct dlm_ls *ls)
0276 {
0277     struct dlm_rsb *r, *s;
0278 
0279     spin_lock(&ls->ls_recover_list_lock);
0280     list_for_each_entry_safe(r, s, &ls->ls_recover_list, res_recover_list) {
0281         list_del_init(&r->res_recover_list);
0282         r->res_recover_locks_count = 0;
0283         dlm_put_rsb(r);
0284         ls->ls_recover_list_count--;
0285     }
0286 
0287     if (ls->ls_recover_list_count != 0) {
0288         log_error(ls, "warning: recover_list_count %d",
0289               ls->ls_recover_list_count);
0290         ls->ls_recover_list_count = 0;
0291     }
0292     spin_unlock(&ls->ls_recover_list_lock);
0293 }
0294 
0295 static int recover_idr_empty(struct dlm_ls *ls)
0296 {
0297     int empty = 1;
0298 
0299     spin_lock(&ls->ls_recover_idr_lock);
0300     if (ls->ls_recover_list_count)
0301         empty = 0;
0302     spin_unlock(&ls->ls_recover_idr_lock);
0303 
0304     return empty;
0305 }
0306 
0307 static int recover_idr_add(struct dlm_rsb *r)
0308 {
0309     struct dlm_ls *ls = r->res_ls;
0310     int rv;
0311 
0312     idr_preload(GFP_NOFS);
0313     spin_lock(&ls->ls_recover_idr_lock);
0314     if (r->res_id) {
0315         rv = -1;
0316         goto out_unlock;
0317     }
0318     rv = idr_alloc(&ls->ls_recover_idr, r, 1, 0, GFP_NOWAIT);
0319     if (rv < 0)
0320         goto out_unlock;
0321 
0322     r->res_id = rv;
0323     ls->ls_recover_list_count++;
0324     dlm_hold_rsb(r);
0325     rv = 0;
0326 out_unlock:
0327     spin_unlock(&ls->ls_recover_idr_lock);
0328     idr_preload_end();
0329     return rv;
0330 }
0331 
0332 static void recover_idr_del(struct dlm_rsb *r)
0333 {
0334     struct dlm_ls *ls = r->res_ls;
0335 
0336     spin_lock(&ls->ls_recover_idr_lock);
0337     idr_remove(&ls->ls_recover_idr, r->res_id);
0338     r->res_id = 0;
0339     ls->ls_recover_list_count--;
0340     spin_unlock(&ls->ls_recover_idr_lock);
0341 
0342     dlm_put_rsb(r);
0343 }
0344 
0345 static struct dlm_rsb *recover_idr_find(struct dlm_ls *ls, uint64_t id)
0346 {
0347     struct dlm_rsb *r;
0348 
0349     spin_lock(&ls->ls_recover_idr_lock);
0350     r = idr_find(&ls->ls_recover_idr, (int)id);
0351     spin_unlock(&ls->ls_recover_idr_lock);
0352     return r;
0353 }
0354 
0355 static void recover_idr_clear(struct dlm_ls *ls)
0356 {
0357     struct dlm_rsb *r;
0358     int id;
0359 
0360     spin_lock(&ls->ls_recover_idr_lock);
0361 
0362     idr_for_each_entry(&ls->ls_recover_idr, r, id) {
0363         idr_remove(&ls->ls_recover_idr, id);
0364         r->res_id = 0;
0365         r->res_recover_locks_count = 0;
0366         ls->ls_recover_list_count--;
0367 
0368         dlm_put_rsb(r);
0369     }
0370 
0371     if (ls->ls_recover_list_count != 0) {
0372         log_error(ls, "warning: recover_list_count %d",
0373               ls->ls_recover_list_count);
0374         ls->ls_recover_list_count = 0;
0375     }
0376     spin_unlock(&ls->ls_recover_idr_lock);
0377 }
0378 
0379 
0380 /* Master recovery: find new master node for rsb's that were
0381    mastered on nodes that have been removed.
0382 
0383    dlm_recover_masters
0384    recover_master
0385    dlm_send_rcom_lookup            ->  receive_rcom_lookup
0386                                        dlm_dir_lookup
0387    receive_rcom_lookup_reply       <-
0388    dlm_recover_master_reply
0389    set_new_master
0390    set_master_lkbs
0391    set_lock_master
0392 */
0393 
0394 /*
0395  * Set the lock master for all LKBs in a lock queue
0396  * If we are the new master of the rsb, we may have received new
0397  * MSTCPY locks from other nodes already which we need to ignore
0398  * when setting the new nodeid.
0399  */
0400 
0401 static void set_lock_master(struct list_head *queue, int nodeid)
0402 {
0403     struct dlm_lkb *lkb;
0404 
0405     list_for_each_entry(lkb, queue, lkb_statequeue) {
0406         if (!(lkb->lkb_flags & DLM_IFL_MSTCPY)) {
0407             lkb->lkb_nodeid = nodeid;
0408             lkb->lkb_remid = 0;
0409         }
0410     }
0411 }
0412 
0413 static void set_master_lkbs(struct dlm_rsb *r)
0414 {
0415     set_lock_master(&r->res_grantqueue, r->res_nodeid);
0416     set_lock_master(&r->res_convertqueue, r->res_nodeid);
0417     set_lock_master(&r->res_waitqueue, r->res_nodeid);
0418 }
0419 
0420 /*
0421  * Propagate the new master nodeid to locks
0422  * The NEW_MASTER flag tells dlm_recover_locks() which rsb's to consider.
0423  * The NEW_MASTER2 flag tells recover_lvb() and recover_grant() which
0424  * rsb's to consider.
0425  */
0426 
0427 static void set_new_master(struct dlm_rsb *r)
0428 {
0429     set_master_lkbs(r);
0430     rsb_set_flag(r, RSB_NEW_MASTER);
0431     rsb_set_flag(r, RSB_NEW_MASTER2);
0432 }
0433 
0434 /*
0435  * We do async lookups on rsb's that need new masters.  The rsb's
0436  * waiting for a lookup reply are kept on the recover_list.
0437  *
0438  * Another node recovering the master may have sent us a rcom lookup,
0439  * and our dlm_master_lookup() set it as the new master, along with
0440  * NEW_MASTER so that we'll recover it here (this implies dir_nodeid
0441  * equals our_nodeid below).
0442  */
0443 
0444 static int recover_master(struct dlm_rsb *r, unsigned int *count)
0445 {
0446     struct dlm_ls *ls = r->res_ls;
0447     int our_nodeid, dir_nodeid;
0448     int is_removed = 0;
0449     int error;
0450 
0451     if (is_master(r))
0452         return 0;
0453 
0454     is_removed = dlm_is_removed(ls, r->res_nodeid);
0455 
0456     if (!is_removed && !rsb_flag(r, RSB_NEW_MASTER))
0457         return 0;
0458 
0459     our_nodeid = dlm_our_nodeid();
0460     dir_nodeid = dlm_dir_nodeid(r);
0461 
0462     if (dir_nodeid == our_nodeid) {
0463         if (is_removed) {
0464             r->res_master_nodeid = our_nodeid;
0465             r->res_nodeid = 0;
0466         }
0467 
0468         /* set master of lkbs to ourself when is_removed, or to
0469            another new master which we set along with NEW_MASTER
0470            in dlm_master_lookup */
0471         set_new_master(r);
0472         error = 0;
0473     } else {
0474         recover_idr_add(r);
0475         error = dlm_send_rcom_lookup(r, dir_nodeid);
0476     }
0477 
0478     (*count)++;
0479     return error;
0480 }
0481 
0482 /*
0483  * All MSTCPY locks are purged and rebuilt, even if the master stayed the same.
0484  * This is necessary because recovery can be started, aborted and restarted,
0485  * causing the master nodeid to briefly change during the aborted recovery, and
0486  * change back to the original value in the second recovery.  The MSTCPY locks
0487  * may or may not have been purged during the aborted recovery.  Another node
0488  * with an outstanding request in waiters list and a request reply saved in the
0489  * requestqueue, cannot know whether it should ignore the reply and resend the
0490  * request, or accept the reply and complete the request.  It must do the
0491  * former if the remote node purged MSTCPY locks, and it must do the later if
0492  * the remote node did not.  This is solved by always purging MSTCPY locks, in
0493  * which case, the request reply would always be ignored and the request
0494  * resent.
0495  */
0496 
0497 static int recover_master_static(struct dlm_rsb *r, unsigned int *count)
0498 {
0499     int dir_nodeid = dlm_dir_nodeid(r);
0500     int new_master = dir_nodeid;
0501 
0502     if (dir_nodeid == dlm_our_nodeid())
0503         new_master = 0;
0504 
0505     dlm_purge_mstcpy_locks(r);
0506     r->res_master_nodeid = dir_nodeid;
0507     r->res_nodeid = new_master;
0508     set_new_master(r);
0509     (*count)++;
0510     return 0;
0511 }
0512 
0513 /*
0514  * Go through local root resources and for each rsb which has a master which
0515  * has departed, get the new master nodeid from the directory.  The dir will
0516  * assign mastery to the first node to look up the new master.  That means
0517  * we'll discover in this lookup if we're the new master of any rsb's.
0518  *
0519  * We fire off all the dir lookup requests individually and asynchronously to
0520  * the correct dir node.
0521  */
0522 
0523 int dlm_recover_masters(struct dlm_ls *ls)
0524 {
0525     struct dlm_rsb *r;
0526     unsigned int total = 0;
0527     unsigned int count = 0;
0528     int nodir = dlm_no_directory(ls);
0529     int error;
0530 
0531     log_rinfo(ls, "dlm_recover_masters");
0532 
0533     down_read(&ls->ls_root_sem);
0534     list_for_each_entry(r, &ls->ls_root_list, res_root_list) {
0535         if (dlm_recovery_stopped(ls)) {
0536             up_read(&ls->ls_root_sem);
0537             error = -EINTR;
0538             goto out;
0539         }
0540 
0541         lock_rsb(r);
0542         if (nodir)
0543             error = recover_master_static(r, &count);
0544         else
0545             error = recover_master(r, &count);
0546         unlock_rsb(r);
0547         cond_resched();
0548         total++;
0549 
0550         if (error) {
0551             up_read(&ls->ls_root_sem);
0552             goto out;
0553         }
0554     }
0555     up_read(&ls->ls_root_sem);
0556 
0557     log_rinfo(ls, "dlm_recover_masters %u of %u", count, total);
0558 
0559     error = dlm_wait_function(ls, &recover_idr_empty);
0560  out:
0561     if (error)
0562         recover_idr_clear(ls);
0563     return error;
0564 }
0565 
0566 int dlm_recover_master_reply(struct dlm_ls *ls, struct dlm_rcom *rc)
0567 {
0568     struct dlm_rsb *r;
0569     int ret_nodeid, new_master;
0570 
0571     r = recover_idr_find(ls, le64_to_cpu(rc->rc_id));
0572     if (!r) {
0573         log_error(ls, "dlm_recover_master_reply no id %llx",
0574               (unsigned long long)le64_to_cpu(rc->rc_id));
0575         goto out;
0576     }
0577 
0578     ret_nodeid = le32_to_cpu(rc->rc_result);
0579 
0580     if (ret_nodeid == dlm_our_nodeid())
0581         new_master = 0;
0582     else
0583         new_master = ret_nodeid;
0584 
0585     lock_rsb(r);
0586     r->res_master_nodeid = ret_nodeid;
0587     r->res_nodeid = new_master;
0588     set_new_master(r);
0589     unlock_rsb(r);
0590     recover_idr_del(r);
0591 
0592     if (recover_idr_empty(ls))
0593         wake_up(&ls->ls_wait_general);
0594  out:
0595     return 0;
0596 }
0597 
0598 
0599 /* Lock recovery: rebuild the process-copy locks we hold on a
0600    remastered rsb on the new rsb master.
0601 
0602    dlm_recover_locks
0603    recover_locks
0604    recover_locks_queue
0605    dlm_send_rcom_lock              ->  receive_rcom_lock
0606                                        dlm_recover_master_copy
0607    receive_rcom_lock_reply         <-
0608    dlm_recover_process_copy
0609 */
0610 
0611 
0612 /*
0613  * keep a count of the number of lkb's we send to the new master; when we get
0614  * an equal number of replies then recovery for the rsb is done
0615  */
0616 
0617 static int recover_locks_queue(struct dlm_rsb *r, struct list_head *head)
0618 {
0619     struct dlm_lkb *lkb;
0620     int error = 0;
0621 
0622     list_for_each_entry(lkb, head, lkb_statequeue) {
0623         error = dlm_send_rcom_lock(r, lkb);
0624         if (error)
0625             break;
0626         r->res_recover_locks_count++;
0627     }
0628 
0629     return error;
0630 }
0631 
0632 static int recover_locks(struct dlm_rsb *r)
0633 {
0634     int error = 0;
0635 
0636     lock_rsb(r);
0637 
0638     DLM_ASSERT(!r->res_recover_locks_count, dlm_dump_rsb(r););
0639 
0640     error = recover_locks_queue(r, &r->res_grantqueue);
0641     if (error)
0642         goto out;
0643     error = recover_locks_queue(r, &r->res_convertqueue);
0644     if (error)
0645         goto out;
0646     error = recover_locks_queue(r, &r->res_waitqueue);
0647     if (error)
0648         goto out;
0649 
0650     if (r->res_recover_locks_count)
0651         recover_list_add(r);
0652     else
0653         rsb_clear_flag(r, RSB_NEW_MASTER);
0654  out:
0655     unlock_rsb(r);
0656     return error;
0657 }
0658 
0659 int dlm_recover_locks(struct dlm_ls *ls)
0660 {
0661     struct dlm_rsb *r;
0662     int error, count = 0;
0663 
0664     down_read(&ls->ls_root_sem);
0665     list_for_each_entry(r, &ls->ls_root_list, res_root_list) {
0666         if (is_master(r)) {
0667             rsb_clear_flag(r, RSB_NEW_MASTER);
0668             continue;
0669         }
0670 
0671         if (!rsb_flag(r, RSB_NEW_MASTER))
0672             continue;
0673 
0674         if (dlm_recovery_stopped(ls)) {
0675             error = -EINTR;
0676             up_read(&ls->ls_root_sem);
0677             goto out;
0678         }
0679 
0680         error = recover_locks(r);
0681         if (error) {
0682             up_read(&ls->ls_root_sem);
0683             goto out;
0684         }
0685 
0686         count += r->res_recover_locks_count;
0687     }
0688     up_read(&ls->ls_root_sem);
0689 
0690     log_rinfo(ls, "dlm_recover_locks %d out", count);
0691 
0692     error = dlm_wait_function(ls, &recover_list_empty);
0693  out:
0694     if (error)
0695         recover_list_clear(ls);
0696     return error;
0697 }
0698 
0699 void dlm_recovered_lock(struct dlm_rsb *r)
0700 {
0701     DLM_ASSERT(rsb_flag(r, RSB_NEW_MASTER), dlm_dump_rsb(r););
0702 
0703     r->res_recover_locks_count--;
0704     if (!r->res_recover_locks_count) {
0705         rsb_clear_flag(r, RSB_NEW_MASTER);
0706         recover_list_del(r);
0707     }
0708 
0709     if (recover_list_empty(r->res_ls))
0710         wake_up(&r->res_ls->ls_wait_general);
0711 }
0712 
0713 /*
0714  * The lvb needs to be recovered on all master rsb's.  This includes setting
0715  * the VALNOTVALID flag if necessary, and determining the correct lvb contents
0716  * based on the lvb's of the locks held on the rsb.
0717  *
0718  * RSB_VALNOTVALID is set in two cases:
0719  *
0720  * 1. we are master, but not new, and we purged an EX/PW lock held by a
0721  * failed node (in dlm_recover_purge which set RSB_RECOVER_LVB_INVAL)
0722  *
0723  * 2. we are a new master, and there are only NL/CR locks left.
0724  * (We could probably improve this by only invaliding in this way when
0725  * the previous master left uncleanly.  VMS docs mention that.)
0726  *
0727  * The LVB contents are only considered for changing when this is a new master
0728  * of the rsb (NEW_MASTER2).  Then, the rsb's lvb is taken from any lkb with
0729  * mode > CR.  If no lkb's exist with mode above CR, the lvb contents are taken
0730  * from the lkb with the largest lvb sequence number.
0731  */
0732 
0733 static void recover_lvb(struct dlm_rsb *r)
0734 {
0735     struct dlm_lkb *big_lkb = NULL, *iter, *high_lkb = NULL;
0736     uint32_t high_seq = 0;
0737     int lock_lvb_exists = 0;
0738     int lvblen = r->res_ls->ls_lvblen;
0739 
0740     if (!rsb_flag(r, RSB_NEW_MASTER2) &&
0741         rsb_flag(r, RSB_RECOVER_LVB_INVAL)) {
0742         /* case 1 above */
0743         rsb_set_flag(r, RSB_VALNOTVALID);
0744         return;
0745     }
0746 
0747     if (!rsb_flag(r, RSB_NEW_MASTER2))
0748         return;
0749 
0750     /* we are the new master, so figure out if VALNOTVALID should
0751        be set, and set the rsb lvb from the best lkb available. */
0752 
0753     list_for_each_entry(iter, &r->res_grantqueue, lkb_statequeue) {
0754         if (!(iter->lkb_exflags & DLM_LKF_VALBLK))
0755             continue;
0756 
0757         lock_lvb_exists = 1;
0758 
0759         if (iter->lkb_grmode > DLM_LOCK_CR) {
0760             big_lkb = iter;
0761             goto setflag;
0762         }
0763 
0764         if (((int)iter->lkb_lvbseq - (int)high_seq) >= 0) {
0765             high_lkb = iter;
0766             high_seq = iter->lkb_lvbseq;
0767         }
0768     }
0769 
0770     list_for_each_entry(iter, &r->res_convertqueue, lkb_statequeue) {
0771         if (!(iter->lkb_exflags & DLM_LKF_VALBLK))
0772             continue;
0773 
0774         lock_lvb_exists = 1;
0775 
0776         if (iter->lkb_grmode > DLM_LOCK_CR) {
0777             big_lkb = iter;
0778             goto setflag;
0779         }
0780 
0781         if (((int)iter->lkb_lvbseq - (int)high_seq) >= 0) {
0782             high_lkb = iter;
0783             high_seq = iter->lkb_lvbseq;
0784         }
0785     }
0786 
0787  setflag:
0788     if (!lock_lvb_exists)
0789         goto out;
0790 
0791     /* lvb is invalidated if only NL/CR locks remain */
0792     if (!big_lkb)
0793         rsb_set_flag(r, RSB_VALNOTVALID);
0794 
0795     if (!r->res_lvbptr) {
0796         r->res_lvbptr = dlm_allocate_lvb(r->res_ls);
0797         if (!r->res_lvbptr)
0798             goto out;
0799     }
0800 
0801     if (big_lkb) {
0802         r->res_lvbseq = big_lkb->lkb_lvbseq;
0803         memcpy(r->res_lvbptr, big_lkb->lkb_lvbptr, lvblen);
0804     } else if (high_lkb) {
0805         r->res_lvbseq = high_lkb->lkb_lvbseq;
0806         memcpy(r->res_lvbptr, high_lkb->lkb_lvbptr, lvblen);
0807     } else {
0808         r->res_lvbseq = 0;
0809         memset(r->res_lvbptr, 0, lvblen);
0810     }
0811  out:
0812     return;
0813 }
0814 
0815 /* All master rsb's flagged RECOVER_CONVERT need to be looked at.  The locks
0816    converting PR->CW or CW->PR need to have their lkb_grmode set. */
0817 
0818 static void recover_conversion(struct dlm_rsb *r)
0819 {
0820     struct dlm_ls *ls = r->res_ls;
0821     struct dlm_lkb *lkb;
0822     int grmode = -1;
0823 
0824     list_for_each_entry(lkb, &r->res_grantqueue, lkb_statequeue) {
0825         if (lkb->lkb_grmode == DLM_LOCK_PR ||
0826             lkb->lkb_grmode == DLM_LOCK_CW) {
0827             grmode = lkb->lkb_grmode;
0828             break;
0829         }
0830     }
0831 
0832     list_for_each_entry(lkb, &r->res_convertqueue, lkb_statequeue) {
0833         if (lkb->lkb_grmode != DLM_LOCK_IV)
0834             continue;
0835         if (grmode == -1) {
0836             log_debug(ls, "recover_conversion %x set gr to rq %d",
0837                   lkb->lkb_id, lkb->lkb_rqmode);
0838             lkb->lkb_grmode = lkb->lkb_rqmode;
0839         } else {
0840             log_debug(ls, "recover_conversion %x set gr %d",
0841                   lkb->lkb_id, grmode);
0842             lkb->lkb_grmode = grmode;
0843         }
0844     }
0845 }
0846 
0847 /* We've become the new master for this rsb and waiting/converting locks may
0848    need to be granted in dlm_recover_grant() due to locks that may have
0849    existed from a removed node. */
0850 
0851 static void recover_grant(struct dlm_rsb *r)
0852 {
0853     if (!list_empty(&r->res_waitqueue) || !list_empty(&r->res_convertqueue))
0854         rsb_set_flag(r, RSB_RECOVER_GRANT);
0855 }
0856 
0857 void dlm_recover_rsbs(struct dlm_ls *ls)
0858 {
0859     struct dlm_rsb *r;
0860     unsigned int count = 0;
0861 
0862     down_read(&ls->ls_root_sem);
0863     list_for_each_entry(r, &ls->ls_root_list, res_root_list) {
0864         lock_rsb(r);
0865         if (is_master(r)) {
0866             if (rsb_flag(r, RSB_RECOVER_CONVERT))
0867                 recover_conversion(r);
0868 
0869             /* recover lvb before granting locks so the updated
0870                lvb/VALNOTVALID is presented in the completion */
0871             recover_lvb(r);
0872 
0873             if (rsb_flag(r, RSB_NEW_MASTER2))
0874                 recover_grant(r);
0875             count++;
0876         } else {
0877             rsb_clear_flag(r, RSB_VALNOTVALID);
0878         }
0879         rsb_clear_flag(r, RSB_RECOVER_CONVERT);
0880         rsb_clear_flag(r, RSB_RECOVER_LVB_INVAL);
0881         rsb_clear_flag(r, RSB_NEW_MASTER2);
0882         unlock_rsb(r);
0883     }
0884     up_read(&ls->ls_root_sem);
0885 
0886     if (count)
0887         log_rinfo(ls, "dlm_recover_rsbs %d done", count);
0888 }
0889 
0890 /* Create a single list of all root rsb's to be used during recovery */
0891 
0892 int dlm_create_root_list(struct dlm_ls *ls)
0893 {
0894     struct rb_node *n;
0895     struct dlm_rsb *r;
0896     int i, error = 0;
0897 
0898     down_write(&ls->ls_root_sem);
0899     if (!list_empty(&ls->ls_root_list)) {
0900         log_error(ls, "root list not empty");
0901         error = -EINVAL;
0902         goto out;
0903     }
0904 
0905     for (i = 0; i < ls->ls_rsbtbl_size; i++) {
0906         spin_lock(&ls->ls_rsbtbl[i].lock);
0907         for (n = rb_first(&ls->ls_rsbtbl[i].keep); n; n = rb_next(n)) {
0908             r = rb_entry(n, struct dlm_rsb, res_hashnode);
0909             list_add(&r->res_root_list, &ls->ls_root_list);
0910             dlm_hold_rsb(r);
0911         }
0912 
0913         if (!RB_EMPTY_ROOT(&ls->ls_rsbtbl[i].toss))
0914             log_error(ls, "dlm_create_root_list toss not empty");
0915         spin_unlock(&ls->ls_rsbtbl[i].lock);
0916     }
0917  out:
0918     up_write(&ls->ls_root_sem);
0919     return error;
0920 }
0921 
0922 void dlm_release_root_list(struct dlm_ls *ls)
0923 {
0924     struct dlm_rsb *r, *safe;
0925 
0926     down_write(&ls->ls_root_sem);
0927     list_for_each_entry_safe(r, safe, &ls->ls_root_list, res_root_list) {
0928         list_del_init(&r->res_root_list);
0929         dlm_put_rsb(r);
0930     }
0931     up_write(&ls->ls_root_sem);
0932 }
0933 
0934 void dlm_clear_toss(struct dlm_ls *ls)
0935 {
0936     struct rb_node *n, *next;
0937     struct dlm_rsb *r;
0938     unsigned int count = 0;
0939     int i;
0940 
0941     for (i = 0; i < ls->ls_rsbtbl_size; i++) {
0942         spin_lock(&ls->ls_rsbtbl[i].lock);
0943         for (n = rb_first(&ls->ls_rsbtbl[i].toss); n; n = next) {
0944             next = rb_next(n);
0945             r = rb_entry(n, struct dlm_rsb, res_hashnode);
0946             rb_erase(n, &ls->ls_rsbtbl[i].toss);
0947             dlm_free_rsb(r);
0948             count++;
0949         }
0950         spin_unlock(&ls->ls_rsbtbl[i].lock);
0951     }
0952 
0953     if (count)
0954         log_rinfo(ls, "dlm_clear_toss %u done", count);
0955 }
0956