Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * dlmast.c
0004  *
0005  * AST and BAST functionality for local and remote nodes
0006  *
0007  * Copyright (C) 2004 Oracle.  All rights reserved.
0008  */
0009 
0010 
0011 #include <linux/module.h>
0012 #include <linux/fs.h>
0013 #include <linux/types.h>
0014 #include <linux/highmem.h>
0015 #include <linux/init.h>
0016 #include <linux/sysctl.h>
0017 #include <linux/random.h>
0018 #include <linux/blkdev.h>
0019 #include <linux/socket.h>
0020 #include <linux/inet.h>
0021 #include <linux/spinlock.h>
0022 
0023 
0024 #include "../cluster/heartbeat.h"
0025 #include "../cluster/nodemanager.h"
0026 #include "../cluster/tcp.h"
0027 
0028 #include "dlmapi.h"
0029 #include "dlmcommon.h"
0030 
0031 #define MLOG_MASK_PREFIX ML_DLM
0032 #include "../cluster/masklog.h"
0033 
0034 static void dlm_update_lvb(struct dlm_ctxt *dlm, struct dlm_lock_resource *res,
0035                struct dlm_lock *lock);
0036 static int dlm_should_cancel_bast(struct dlm_ctxt *dlm, struct dlm_lock *lock);
0037 
0038 /* Should be called as an ast gets queued to see if the new
0039  * lock level will obsolete a pending bast.
0040  * For example, if dlm_thread queued a bast for an EX lock that
0041  * was blocking another EX, but before sending the bast the
0042  * lock owner downconverted to NL, the bast is now obsolete.
0043  * Only the ast should be sent.
0044  * This is needed because the lock and convert paths can queue
0045  * asts out-of-band (not waiting for dlm_thread) in order to
0046  * allow for LKM_NOQUEUE to get immediate responses. */
0047 static int dlm_should_cancel_bast(struct dlm_ctxt *dlm, struct dlm_lock *lock)
0048 {
0049     assert_spin_locked(&dlm->ast_lock);
0050     assert_spin_locked(&lock->spinlock);
0051 
0052     if (lock->ml.highest_blocked == LKM_IVMODE)
0053         return 0;
0054     BUG_ON(lock->ml.highest_blocked == LKM_NLMODE);
0055 
0056     if (lock->bast_pending &&
0057         list_empty(&lock->bast_list))
0058         /* old bast already sent, ok */
0059         return 0;
0060 
0061     if (lock->ml.type == LKM_EXMODE)
0062         /* EX blocks anything left, any bast still valid */
0063         return 0;
0064     else if (lock->ml.type == LKM_NLMODE)
0065         /* NL blocks nothing, no reason to send any bast, cancel it */
0066         return 1;
0067     else if (lock->ml.highest_blocked != LKM_EXMODE)
0068         /* PR only blocks EX */
0069         return 1;
0070 
0071     return 0;
0072 }
0073 
0074 void __dlm_queue_ast(struct dlm_ctxt *dlm, struct dlm_lock *lock)
0075 {
0076     struct dlm_lock_resource *res;
0077 
0078     BUG_ON(!dlm);
0079     BUG_ON(!lock);
0080 
0081     res = lock->lockres;
0082 
0083     assert_spin_locked(&dlm->ast_lock);
0084 
0085     if (!list_empty(&lock->ast_list)) {
0086         mlog(ML_ERROR, "%s: res %.*s, lock %u:%llu, "
0087              "AST list not empty, pending %d, newlevel %d\n",
0088              dlm->name, res->lockname.len, res->lockname.name,
0089              dlm_get_lock_cookie_node(be64_to_cpu(lock->ml.cookie)),
0090              dlm_get_lock_cookie_seq(be64_to_cpu(lock->ml.cookie)),
0091              lock->ast_pending, lock->ml.type);
0092         BUG();
0093     }
0094     if (lock->ast_pending)
0095         mlog(0, "%s: res %.*s, lock %u:%llu, AST getting flushed\n",
0096              dlm->name, res->lockname.len, res->lockname.name,
0097              dlm_get_lock_cookie_node(be64_to_cpu(lock->ml.cookie)),
0098              dlm_get_lock_cookie_seq(be64_to_cpu(lock->ml.cookie)));
0099 
0100     /* putting lock on list, add a ref */
0101     dlm_lock_get(lock);
0102     spin_lock(&lock->spinlock);
0103 
0104     /* check to see if this ast obsoletes the bast */
0105     if (dlm_should_cancel_bast(dlm, lock)) {
0106         mlog(0, "%s: res %.*s, lock %u:%llu, Cancelling BAST\n",
0107              dlm->name, res->lockname.len, res->lockname.name,
0108              dlm_get_lock_cookie_node(be64_to_cpu(lock->ml.cookie)),
0109              dlm_get_lock_cookie_seq(be64_to_cpu(lock->ml.cookie)));
0110         lock->bast_pending = 0;
0111         list_del_init(&lock->bast_list);
0112         lock->ml.highest_blocked = LKM_IVMODE;
0113         /* removing lock from list, remove a ref.  guaranteed
0114          * this won't be the last ref because of the get above,
0115          * so res->spinlock will not be taken here */
0116         dlm_lock_put(lock);
0117         /* free up the reserved bast that we are cancelling.
0118          * guaranteed that this will not be the last reserved
0119          * ast because *both* an ast and a bast were reserved
0120          * to get to this point.  the res->spinlock will not be
0121          * taken here */
0122         dlm_lockres_release_ast(dlm, res);
0123     }
0124     list_add_tail(&lock->ast_list, &dlm->pending_asts);
0125     lock->ast_pending = 1;
0126     spin_unlock(&lock->spinlock);
0127 }
0128 
0129 void dlm_queue_ast(struct dlm_ctxt *dlm, struct dlm_lock *lock)
0130 {
0131     BUG_ON(!dlm);
0132     BUG_ON(!lock);
0133 
0134     spin_lock(&dlm->ast_lock);
0135     __dlm_queue_ast(dlm, lock);
0136     spin_unlock(&dlm->ast_lock);
0137 }
0138 
0139 
0140 void __dlm_queue_bast(struct dlm_ctxt *dlm, struct dlm_lock *lock)
0141 {
0142     struct dlm_lock_resource *res;
0143 
0144     BUG_ON(!dlm);
0145     BUG_ON(!lock);
0146 
0147     assert_spin_locked(&dlm->ast_lock);
0148 
0149     res = lock->lockres;
0150 
0151     BUG_ON(!list_empty(&lock->bast_list));
0152     if (lock->bast_pending)
0153         mlog(0, "%s: res %.*s, lock %u:%llu, BAST getting flushed\n",
0154              dlm->name, res->lockname.len, res->lockname.name,
0155              dlm_get_lock_cookie_node(be64_to_cpu(lock->ml.cookie)),
0156              dlm_get_lock_cookie_seq(be64_to_cpu(lock->ml.cookie)));
0157 
0158     /* putting lock on list, add a ref */
0159     dlm_lock_get(lock);
0160     spin_lock(&lock->spinlock);
0161     list_add_tail(&lock->bast_list, &dlm->pending_basts);
0162     lock->bast_pending = 1;
0163     spin_unlock(&lock->spinlock);
0164 }
0165 
0166 static void dlm_update_lvb(struct dlm_ctxt *dlm, struct dlm_lock_resource *res,
0167                struct dlm_lock *lock)
0168 {
0169     struct dlm_lockstatus *lksb = lock->lksb;
0170     BUG_ON(!lksb);
0171 
0172     /* only updates if this node masters the lockres */
0173     spin_lock(&res->spinlock);
0174     if (res->owner == dlm->node_num) {
0175         /* check the lksb flags for the direction */
0176         if (lksb->flags & DLM_LKSB_GET_LVB) {
0177             mlog(0, "getting lvb from lockres for %s node\n",
0178                   lock->ml.node == dlm->node_num ? "master" :
0179                   "remote");
0180             memcpy(lksb->lvb, res->lvb, DLM_LVB_LEN);
0181         }
0182         /* Do nothing for lvb put requests - they should be done in
0183          * place when the lock is downconverted - otherwise we risk
0184          * racing gets and puts which could result in old lvb data
0185          * being propagated. We leave the put flag set and clear it
0186          * here. In the future we might want to clear it at the time
0187          * the put is actually done.
0188          */
0189     }
0190     spin_unlock(&res->spinlock);
0191 
0192     /* reset any lvb flags on the lksb */
0193     lksb->flags &= ~(DLM_LKSB_PUT_LVB|DLM_LKSB_GET_LVB);
0194 }
0195 
0196 void dlm_do_local_ast(struct dlm_ctxt *dlm, struct dlm_lock_resource *res,
0197               struct dlm_lock *lock)
0198 {
0199     dlm_astlockfunc_t *fn;
0200 
0201     mlog(0, "%s: res %.*s, lock %u:%llu, Local AST\n", dlm->name,
0202          res->lockname.len, res->lockname.name,
0203          dlm_get_lock_cookie_node(be64_to_cpu(lock->ml.cookie)),
0204          dlm_get_lock_cookie_seq(be64_to_cpu(lock->ml.cookie)));
0205 
0206     fn = lock->ast;
0207     BUG_ON(lock->ml.node != dlm->node_num);
0208 
0209     dlm_update_lvb(dlm, res, lock);
0210     (*fn)(lock->astdata);
0211 }
0212 
0213 
0214 int dlm_do_remote_ast(struct dlm_ctxt *dlm, struct dlm_lock_resource *res,
0215               struct dlm_lock *lock)
0216 {
0217     int ret;
0218     struct dlm_lockstatus *lksb;
0219     int lksbflags;
0220 
0221     mlog(0, "%s: res %.*s, lock %u:%llu, Remote AST\n", dlm->name,
0222          res->lockname.len, res->lockname.name,
0223          dlm_get_lock_cookie_node(be64_to_cpu(lock->ml.cookie)),
0224          dlm_get_lock_cookie_seq(be64_to_cpu(lock->ml.cookie)));
0225 
0226     lksb = lock->lksb;
0227     BUG_ON(lock->ml.node == dlm->node_num);
0228 
0229     lksbflags = lksb->flags;
0230     dlm_update_lvb(dlm, res, lock);
0231 
0232     /* lock request came from another node
0233      * go do the ast over there */
0234     ret = dlm_send_proxy_ast(dlm, res, lock, lksbflags);
0235     return ret;
0236 }
0237 
0238 void dlm_do_local_bast(struct dlm_ctxt *dlm, struct dlm_lock_resource *res,
0239                struct dlm_lock *lock, int blocked_type)
0240 {
0241     dlm_bastlockfunc_t *fn = lock->bast;
0242 
0243     BUG_ON(lock->ml.node != dlm->node_num);
0244 
0245     mlog(0, "%s: res %.*s, lock %u:%llu, Local BAST, blocked %d\n",
0246          dlm->name, res->lockname.len, res->lockname.name,
0247          dlm_get_lock_cookie_node(be64_to_cpu(lock->ml.cookie)),
0248          dlm_get_lock_cookie_seq(be64_to_cpu(lock->ml.cookie)),
0249          blocked_type);
0250 
0251     (*fn)(lock->astdata, blocked_type);
0252 }
0253 
0254 
0255 
0256 int dlm_proxy_ast_handler(struct o2net_msg *msg, u32 len, void *data,
0257               void **ret_data)
0258 {
0259     int ret;
0260     unsigned int locklen;
0261     struct dlm_ctxt *dlm = data;
0262     struct dlm_lock_resource *res = NULL;
0263     struct dlm_lock *lock = NULL;
0264     struct dlm_proxy_ast *past = (struct dlm_proxy_ast *) msg->buf;
0265     char *name;
0266     struct list_head *head = NULL;
0267     __be64 cookie;
0268     u32 flags;
0269     u8 node;
0270 
0271     if (!dlm_grab(dlm)) {
0272         dlm_error(DLM_REJECTED);
0273         return DLM_REJECTED;
0274     }
0275 
0276     mlog_bug_on_msg(!dlm_domain_fully_joined(dlm),
0277             "Domain %s not fully joined!\n", dlm->name);
0278 
0279     name = past->name;
0280     locklen = past->namelen;
0281     cookie = past->cookie;
0282     flags = be32_to_cpu(past->flags);
0283     node = past->node_idx;
0284 
0285     if (locklen > DLM_LOCKID_NAME_MAX) {
0286         ret = DLM_IVBUFLEN;
0287         mlog(ML_ERROR, "Invalid name length (%d) in proxy ast "
0288              "handler!\n", locklen);
0289         goto leave;
0290     }
0291 
0292     if ((flags & (LKM_PUT_LVB|LKM_GET_LVB)) ==
0293          (LKM_PUT_LVB|LKM_GET_LVB)) {
0294         mlog(ML_ERROR, "Both PUT and GET lvb specified, (0x%x)\n",
0295              flags);
0296         ret = DLM_BADARGS;
0297         goto leave;
0298     }
0299 
0300     mlog(0, "lvb: %s\n", flags & LKM_PUT_LVB ? "put lvb" :
0301           (flags & LKM_GET_LVB ? "get lvb" : "none"));
0302 
0303     mlog(0, "type=%d, blocked_type=%d\n", past->type, past->blocked_type);
0304 
0305     if (past->type != DLM_AST &&
0306         past->type != DLM_BAST) {
0307         mlog(ML_ERROR, "Unknown ast type! %d, cookie=%u:%llu"
0308              "name=%.*s, node=%u\n", past->type,
0309              dlm_get_lock_cookie_node(be64_to_cpu(cookie)),
0310              dlm_get_lock_cookie_seq(be64_to_cpu(cookie)),
0311              locklen, name, node);
0312         ret = DLM_IVLOCKID;
0313         goto leave;
0314     }
0315 
0316     res = dlm_lookup_lockres(dlm, name, locklen);
0317     if (!res) {
0318         mlog(0, "Got %sast for unknown lockres! cookie=%u:%llu, "
0319              "name=%.*s, node=%u\n", (past->type == DLM_AST ? "" : "b"),
0320              dlm_get_lock_cookie_node(be64_to_cpu(cookie)),
0321              dlm_get_lock_cookie_seq(be64_to_cpu(cookie)),
0322              locklen, name, node);
0323         ret = DLM_IVLOCKID;
0324         goto leave;
0325     }
0326 
0327     /* cannot get a proxy ast message if this node owns it */
0328     BUG_ON(res->owner == dlm->node_num);
0329 
0330     mlog(0, "%s: res %.*s\n", dlm->name, res->lockname.len,
0331          res->lockname.name);
0332 
0333     spin_lock(&res->spinlock);
0334     if (res->state & DLM_LOCK_RES_RECOVERING) {
0335         mlog(0, "Responding with DLM_RECOVERING!\n");
0336         ret = DLM_RECOVERING;
0337         goto unlock_out;
0338     }
0339     if (res->state & DLM_LOCK_RES_MIGRATING) {
0340         mlog(0, "Responding with DLM_MIGRATING!\n");
0341         ret = DLM_MIGRATING;
0342         goto unlock_out;
0343     }
0344     /* try convert queue for both ast/bast */
0345     head = &res->converting;
0346     lock = NULL;
0347     list_for_each_entry(lock, head, list) {
0348         if (lock->ml.cookie == cookie)
0349             goto do_ast;
0350     }
0351 
0352     /* if not on convert, try blocked for ast, granted for bast */
0353     if (past->type == DLM_AST)
0354         head = &res->blocked;
0355     else
0356         head = &res->granted;
0357 
0358     list_for_each_entry(lock, head, list) {
0359         /* if lock is found but unlock is pending ignore the bast */
0360         if (lock->ml.cookie == cookie) {
0361             if (lock->unlock_pending)
0362                 break;
0363             goto do_ast;
0364         }
0365     }
0366 
0367     mlog(0, "Got %sast for unknown lock! cookie=%u:%llu, name=%.*s, "
0368          "node=%u\n", past->type == DLM_AST ? "" : "b",
0369          dlm_get_lock_cookie_node(be64_to_cpu(cookie)),
0370          dlm_get_lock_cookie_seq(be64_to_cpu(cookie)),
0371          locklen, name, node);
0372 
0373     ret = DLM_NORMAL;
0374 unlock_out:
0375     spin_unlock(&res->spinlock);
0376     goto leave;
0377 
0378 do_ast:
0379     ret = DLM_NORMAL;
0380     if (past->type == DLM_AST) {
0381         /* do not alter lock refcount.  switching lists. */
0382         list_move_tail(&lock->list, &res->granted);
0383         mlog(0, "%s: res %.*s, lock %u:%llu, Granted type %d => %d\n",
0384              dlm->name, res->lockname.len, res->lockname.name,
0385              dlm_get_lock_cookie_node(be64_to_cpu(cookie)),
0386              dlm_get_lock_cookie_seq(be64_to_cpu(cookie)),
0387              lock->ml.type, lock->ml.convert_type);
0388 
0389         if (lock->ml.convert_type != LKM_IVMODE) {
0390             lock->ml.type = lock->ml.convert_type;
0391             lock->ml.convert_type = LKM_IVMODE;
0392         } else {
0393             // should already be there....
0394         }
0395 
0396         lock->lksb->status = DLM_NORMAL;
0397 
0398         /* if we requested the lvb, fetch it into our lksb now */
0399         if (flags & LKM_GET_LVB) {
0400             BUG_ON(!(lock->lksb->flags & DLM_LKSB_GET_LVB));
0401             memcpy(lock->lksb->lvb, past->lvb, DLM_LVB_LEN);
0402         }
0403     }
0404     spin_unlock(&res->spinlock);
0405 
0406     if (past->type == DLM_AST)
0407         dlm_do_local_ast(dlm, res, lock);
0408     else
0409         dlm_do_local_bast(dlm, res, lock, past->blocked_type);
0410 
0411 leave:
0412     if (res)
0413         dlm_lockres_put(res);
0414 
0415     dlm_put(dlm);
0416     return ret;
0417 }
0418 
0419 
0420 
0421 int dlm_send_proxy_ast_msg(struct dlm_ctxt *dlm, struct dlm_lock_resource *res,
0422                struct dlm_lock *lock, int msg_type,
0423                int blocked_type, int flags)
0424 {
0425     int ret = 0;
0426     struct dlm_proxy_ast past;
0427     struct kvec vec[2];
0428     size_t veclen = 1;
0429     int status;
0430 
0431     mlog(0, "%s: res %.*s, to %u, type %d, blocked_type %d\n", dlm->name,
0432          res->lockname.len, res->lockname.name, lock->ml.node, msg_type,
0433          blocked_type);
0434 
0435     memset(&past, 0, sizeof(struct dlm_proxy_ast));
0436     past.node_idx = dlm->node_num;
0437     past.type = msg_type;
0438     past.blocked_type = blocked_type;
0439     past.namelen = res->lockname.len;
0440     memcpy(past.name, res->lockname.name, past.namelen);
0441     past.cookie = lock->ml.cookie;
0442 
0443     vec[0].iov_len = sizeof(struct dlm_proxy_ast);
0444     vec[0].iov_base = &past;
0445     if (flags & DLM_LKSB_GET_LVB) {
0446         be32_add_cpu(&past.flags, LKM_GET_LVB);
0447         vec[1].iov_len = DLM_LVB_LEN;
0448         vec[1].iov_base = lock->lksb->lvb;
0449         veclen++;
0450     }
0451 
0452     ret = o2net_send_message_vec(DLM_PROXY_AST_MSG, dlm->key, vec, veclen,
0453                      lock->ml.node, &status);
0454     if (ret < 0)
0455         mlog(ML_ERROR, "%s: res %.*s, error %d send AST to node %u\n",
0456              dlm->name, res->lockname.len, res->lockname.name, ret,
0457              lock->ml.node);
0458     else {
0459         if (status == DLM_RECOVERING) {
0460             mlog(ML_ERROR, "sent AST to node %u, it thinks this "
0461                  "node is dead!\n", lock->ml.node);
0462             BUG();
0463         } else if (status == DLM_MIGRATING) {
0464             mlog(ML_ERROR, "sent AST to node %u, it returned "
0465                  "DLM_MIGRATING!\n", lock->ml.node);
0466             BUG();
0467         } else if (status != DLM_NORMAL && status != DLM_IVLOCKID) {
0468             mlog(ML_ERROR, "AST to node %u returned %d!\n",
0469                  lock->ml.node, status);
0470             /* ignore it */
0471         }
0472         ret = 0;
0473     }
0474     return ret;
0475 }