0001
0002
0003
0004
0005
0006
0007
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
0039
0040
0041
0042
0043
0044
0045
0046
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
0059 return 0;
0060
0061 if (lock->ml.type == LKM_EXMODE)
0062
0063 return 0;
0064 else if (lock->ml.type == LKM_NLMODE)
0065
0066 return 1;
0067 else if (lock->ml.highest_blocked != LKM_EXMODE)
0068
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
0101 dlm_lock_get(lock);
0102 spin_lock(&lock->spinlock);
0103
0104
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
0114
0115
0116 dlm_lock_put(lock);
0117
0118
0119
0120
0121
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
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
0173 spin_lock(&res->spinlock);
0174 if (res->owner == dlm->node_num) {
0175
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
0183
0184
0185
0186
0187
0188
0189 }
0190 spin_unlock(&res->spinlock);
0191
0192
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
0233
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
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
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
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
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
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
0394 }
0395
0396 lock->lksb->status = DLM_NORMAL;
0397
0398
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
0471 }
0472 ret = 0;
0473 }
0474 return ret;
0475 }