1 /* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
6 * underlying calls for lock creation
8 * Copyright (C) 2004 Oracle. All rights reserved.
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public
12 * License as published by the Free Software Foundation; either
13 * version 2 of the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
20 * You should have received a copy of the GNU General Public
21 * License along with this program; if not, write to the
22 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 * Boston, MA 021110-1307, USA.
28 #include <linux/module.h>
30 #include <linux/types.h>
31 #include <linux/slab.h>
32 #include <linux/highmem.h>
33 #include <linux/init.h>
34 #include <linux/sysctl.h>
35 #include <linux/random.h>
36 #include <linux/blkdev.h>
37 #include <linux/socket.h>
38 #include <linux/inet.h>
39 #include <linux/spinlock.h>
40 #include <linux/delay.h>
43 #include "cluster/heartbeat.h"
44 #include "cluster/nodemanager.h"
45 #include "cluster/tcp.h"
48 #include "dlmcommon.h"
50 #include "dlmconvert.h"
52 #define MLOG_MASK_PREFIX ML_DLM
53 #include "cluster/masklog.h"
55 static struct kmem_cache *dlm_lock_cache = NULL;
57 static DEFINE_SPINLOCK(dlm_cookie_lock);
58 static u64 dlm_next_cookie = 1;
60 static enum dlm_status dlm_send_remote_lock_request(struct dlm_ctxt *dlm,
61 struct dlm_lock_resource *res,
62 struct dlm_lock *lock, int flags);
63 static void dlm_init_lock(struct dlm_lock *newlock, int type,
65 static void dlm_lock_release(struct kref *kref);
66 static void dlm_lock_detach_lockres(struct dlm_lock *lock);
68 int dlm_init_lock_cache(void)
70 dlm_lock_cache = kmem_cache_create("o2dlm_lock",
71 sizeof(struct dlm_lock),
72 0, SLAB_HWCACHE_ALIGN, NULL);
73 if (dlm_lock_cache == NULL)
78 void dlm_destroy_lock_cache(void)
81 kmem_cache_destroy(dlm_lock_cache);
84 /* Tell us whether we can grant a new lock request.
86 * caller needs: res->spinlock
89 * returns: 1 if the lock can be granted, 0 otherwise.
91 static int dlm_can_grant_new_lock(struct dlm_lock_resource *res,
92 struct dlm_lock *lock)
94 struct list_head *iter;
95 struct dlm_lock *tmplock;
97 list_for_each(iter, &res->granted) {
98 tmplock = list_entry(iter, struct dlm_lock, list);
100 if (!dlm_lock_compatible(tmplock->ml.type, lock->ml.type))
104 list_for_each(iter, &res->converting) {
105 tmplock = list_entry(iter, struct dlm_lock, list);
107 if (!dlm_lock_compatible(tmplock->ml.type, lock->ml.type))
114 /* performs lock creation at the lockres master site
117 * taken: takes and drops res->spinlock
119 * returns: DLM_NORMAL, DLM_NOTQUEUED
121 static enum dlm_status dlmlock_master(struct dlm_ctxt *dlm,
122 struct dlm_lock_resource *res,
123 struct dlm_lock *lock, int flags)
125 int call_ast = 0, kick_thread = 0;
126 enum dlm_status status = DLM_NORMAL;
128 mlog_entry("type=%d\n", lock->ml.type);
130 spin_lock(&res->spinlock);
131 /* if called from dlm_create_lock_handler, need to
132 * ensure it will not sleep in dlm_wait_on_lockres */
133 status = __dlm_lockres_state_to_status(res);
134 if (status != DLM_NORMAL &&
135 lock->ml.node != dlm->node_num) {
136 /* erf. state changed after lock was dropped. */
137 spin_unlock(&res->spinlock);
141 __dlm_wait_on_lockres(res);
142 __dlm_lockres_reserve_ast(res);
144 if (dlm_can_grant_new_lock(res, lock)) {
145 mlog(0, "I can grant this lock right away\n");
146 /* got it right away */
147 lock->lksb->status = DLM_NORMAL;
150 list_add_tail(&lock->list, &res->granted);
152 /* for the recovery lock, we can't allow the ast
153 * to be queued since the dlmthread is already
154 * frozen. but the recovery lock is always locked
155 * with LKM_NOQUEUE so we do not need the ast in
156 * this special case */
157 if (!dlm_is_recovery_lock(res->lockname.name,
158 res->lockname.len)) {
162 mlog(0, "%s: returning DLM_NORMAL to "
163 "node %u for reco lock\n", dlm->name,
167 /* for NOQUEUE request, unless we get the
168 * lock right away, return DLM_NOTQUEUED */
169 if (flags & LKM_NOQUEUE) {
170 status = DLM_NOTQUEUED;
171 if (dlm_is_recovery_lock(res->lockname.name,
172 res->lockname.len)) {
173 mlog(0, "%s: returning NOTQUEUED to "
174 "node %u for reco lock\n", dlm->name,
179 list_add_tail(&lock->list, &res->blocked);
183 /* reduce the inflight count, this may result in the lockres
184 * being purged below during calc_usage */
185 if (lock->ml.node == dlm->node_num)
186 dlm_lockres_drop_inflight_ref(dlm, res);
188 spin_unlock(&res->spinlock);
191 /* either queue the ast or release it */
193 dlm_queue_ast(dlm, lock);
195 dlm_lockres_release_ast(dlm, res);
197 dlm_lockres_calc_usage(dlm, res);
199 dlm_kick_thread(dlm, res);
204 void dlm_revert_pending_lock(struct dlm_lock_resource *res,
205 struct dlm_lock *lock)
207 /* remove from local queue if it failed */
208 list_del_init(&lock->list);
209 lock->lksb->flags &= ~DLM_LKSB_GET_LVB;
216 * taken: takes and drops res->spinlock
218 * returns: DLM_DENIED, DLM_RECOVERING, or net status
220 static enum dlm_status dlmlock_remote(struct dlm_ctxt *dlm,
221 struct dlm_lock_resource *res,
222 struct dlm_lock *lock, int flags)
224 enum dlm_status status = DLM_DENIED;
225 int lockres_changed = 1;
227 mlog_entry("type=%d\n", lock->ml.type);
228 mlog(0, "lockres %.*s, flags = 0x%x\n", res->lockname.len,
229 res->lockname.name, flags);
231 spin_lock(&res->spinlock);
233 /* will exit this call with spinlock held */
234 __dlm_wait_on_lockres(res);
235 res->state |= DLM_LOCK_RES_IN_PROGRESS;
237 /* add lock to local (secondary) queue */
239 list_add_tail(&lock->list, &res->blocked);
240 lock->lock_pending = 1;
241 spin_unlock(&res->spinlock);
243 /* spec seems to say that you will get DLM_NORMAL when the lock
244 * has been queued, meaning we need to wait for a reply here. */
245 status = dlm_send_remote_lock_request(dlm, res, lock, flags);
247 spin_lock(&res->spinlock);
248 res->state &= ~DLM_LOCK_RES_IN_PROGRESS;
249 lock->lock_pending = 0;
250 if (status != DLM_NORMAL) {
251 if (status == DLM_RECOVERING &&
252 dlm_is_recovery_lock(res->lockname.name,
253 res->lockname.len)) {
254 /* recovery lock was mastered by dead node.
255 * we need to have calc_usage shoot down this
256 * lockres and completely remaster it. */
257 mlog(0, "%s: recovery lock was owned by "
258 "dead node %u, remaster it now.\n",
259 dlm->name, res->owner);
260 } else if (status != DLM_NOTQUEUED) {
262 * DO NOT call calc_usage, as this would unhash
263 * the remote lockres before we ever get to use
264 * it. treat as if we never made any change to
270 dlm_revert_pending_lock(res, lock);
272 } else if (dlm_is_recovery_lock(res->lockname.name,
273 res->lockname.len)) {
274 /* special case for the $RECOVERY lock.
275 * there will never be an AST delivered to put
276 * this lock on the proper secondary queue
277 * (granted), so do it manually. */
278 mlog(0, "%s: $RECOVERY lock for this node (%u) is "
279 "mastered by %u; got lock, manually granting (no ast)\n",
280 dlm->name, dlm->node_num, res->owner);
281 list_move_tail(&lock->list, &res->granted);
283 spin_unlock(&res->spinlock);
286 dlm_lockres_calc_usage(dlm, res);
293 /* for remote lock creation.
295 * caller needs: none, but need res->state & DLM_LOCK_RES_IN_PROGRESS
298 * returns: DLM_NOLOCKMGR, or net status
300 static enum dlm_status dlm_send_remote_lock_request(struct dlm_ctxt *dlm,
301 struct dlm_lock_resource *res,
302 struct dlm_lock *lock, int flags)
304 struct dlm_create_lock create;
305 int tmpret, status = 0;
310 memset(&create, 0, sizeof(create));
311 create.node_idx = dlm->node_num;
312 create.requested_type = lock->ml.type;
313 create.cookie = lock->ml.cookie;
314 create.namelen = res->lockname.len;
315 create.flags = cpu_to_be32(flags);
316 memcpy(create.name, res->lockname.name, create.namelen);
318 tmpret = o2net_send_message(DLM_CREATE_LOCK_MSG, dlm->key, &create,
319 sizeof(create), res->owner, &status);
321 // successfully sent and received
322 ret = status; // this is already a dlm_status
323 if (ret == DLM_REJECTED) {
324 mlog(ML_ERROR, "%s:%.*s: BUG. this is a stale lockres "
325 "no longer owned by %u. that node is coming back "
326 "up currently.\n", dlm->name, create.namelen,
327 create.name, res->owner);
328 dlm_print_one_lock_resource(res);
332 mlog(ML_ERROR, "Error %d when sending message %u (key 0x%x) to "
333 "node %u\n", tmpret, DLM_CREATE_LOCK_MSG, dlm->key,
335 if (dlm_is_host_down(tmpret)) {
336 ret = DLM_RECOVERING;
337 mlog(0, "node %u died so returning DLM_RECOVERING "
338 "from lock message!\n", res->owner);
340 ret = dlm_err_to_dlm_status(tmpret);
347 void dlm_lock_get(struct dlm_lock *lock)
349 kref_get(&lock->lock_refs);
352 void dlm_lock_put(struct dlm_lock *lock)
354 kref_put(&lock->lock_refs, dlm_lock_release);
357 static void dlm_lock_release(struct kref *kref)
359 struct dlm_lock *lock;
361 lock = container_of(kref, struct dlm_lock, lock_refs);
363 BUG_ON(!list_empty(&lock->list));
364 BUG_ON(!list_empty(&lock->ast_list));
365 BUG_ON(!list_empty(&lock->bast_list));
366 BUG_ON(lock->ast_pending);
367 BUG_ON(lock->bast_pending);
369 dlm_lock_detach_lockres(lock);
371 if (lock->lksb_kernel_allocated) {
372 mlog(0, "freeing kernel-allocated lksb\n");
375 kmem_cache_free(dlm_lock_cache, lock);
378 /* associate a lock with it's lockres, getting a ref on the lockres */
379 void dlm_lock_attach_lockres(struct dlm_lock *lock,
380 struct dlm_lock_resource *res)
382 dlm_lockres_get(res);
386 /* drop ref on lockres, if there is still one associated with lock */
387 static void dlm_lock_detach_lockres(struct dlm_lock *lock)
389 struct dlm_lock_resource *res;
393 lock->lockres = NULL;
394 mlog(0, "removing lock's lockres reference\n");
395 dlm_lockres_put(res);
399 static void dlm_init_lock(struct dlm_lock *newlock, int type,
402 INIT_LIST_HEAD(&newlock->list);
403 INIT_LIST_HEAD(&newlock->ast_list);
404 INIT_LIST_HEAD(&newlock->bast_list);
405 spin_lock_init(&newlock->spinlock);
406 newlock->ml.type = type;
407 newlock->ml.convert_type = LKM_IVMODE;
408 newlock->ml.highest_blocked = LKM_IVMODE;
409 newlock->ml.node = node;
410 newlock->ml.pad1 = 0;
411 newlock->ml.list = 0;
412 newlock->ml.flags = 0;
414 newlock->bast = NULL;
415 newlock->astdata = NULL;
416 newlock->ml.cookie = cpu_to_be64(cookie);
417 newlock->ast_pending = 0;
418 newlock->bast_pending = 0;
419 newlock->convert_pending = 0;
420 newlock->lock_pending = 0;
421 newlock->unlock_pending = 0;
422 newlock->cancel_pending = 0;
423 newlock->lksb_kernel_allocated = 0;
425 kref_init(&newlock->lock_refs);
428 struct dlm_lock * dlm_new_lock(int type, u8 node, u64 cookie,
429 struct dlm_lockstatus *lksb)
431 struct dlm_lock *lock;
432 int kernel_allocated = 0;
434 lock = kmem_cache_zalloc(dlm_lock_cache, GFP_NOFS);
439 /* zero memory only if kernel-allocated */
440 lksb = kzalloc(sizeof(*lksb), GFP_NOFS);
445 kernel_allocated = 1;
448 dlm_init_lock(lock, type, node, cookie);
449 if (kernel_allocated)
450 lock->lksb_kernel_allocated = 1;
456 /* handler for lock creation net message
459 * taken: takes and drops res->spinlock
461 * returns: DLM_NORMAL, DLM_SYSERR, DLM_IVLOCKID, DLM_NOTQUEUED
463 int dlm_create_lock_handler(struct o2net_msg *msg, u32 len, void *data,
466 struct dlm_ctxt *dlm = data;
467 struct dlm_create_lock *create = (struct dlm_create_lock *)msg->buf;
468 struct dlm_lock_resource *res = NULL;
469 struct dlm_lock *newlock = NULL;
470 struct dlm_lockstatus *lksb = NULL;
471 enum dlm_status status = DLM_NORMAL;
473 unsigned int namelen;
483 namelen = create->namelen;
484 status = DLM_REJECTED;
485 if (!dlm_domain_fully_joined(dlm)) {
486 mlog(ML_ERROR, "Domain %s not fully joined, but node %u is "
487 "sending a create_lock message for lock %.*s!\n",
488 dlm->name, create->node_idx, namelen, name);
493 status = DLM_IVBUFLEN;
494 if (namelen > DLM_LOCKID_NAME_MAX) {
500 newlock = dlm_new_lock(create->requested_type,
502 be64_to_cpu(create->cookie), NULL);
508 lksb = newlock->lksb;
510 if (be32_to_cpu(create->flags) & LKM_GET_LVB) {
511 lksb->flags |= DLM_LKSB_GET_LVB;
512 mlog(0, "set DLM_LKSB_GET_LVB flag\n");
515 status = DLM_IVLOCKID;
516 res = dlm_lookup_lockres(dlm, name, namelen);
522 spin_lock(&res->spinlock);
523 status = __dlm_lockres_state_to_status(res);
524 spin_unlock(&res->spinlock);
526 if (status != DLM_NORMAL) {
527 mlog(0, "lockres recovering/migrating/in-progress\n");
531 dlm_lock_attach_lockres(newlock, res);
533 status = dlmlock_master(dlm, res, newlock, be32_to_cpu(create->flags));
535 if (status != DLM_NORMAL)
537 dlm_lock_put(newlock);
540 dlm_lockres_put(res);
548 /* fetch next node-local (u8 nodenum + u56 cookie) into u64 */
549 static inline void dlm_get_next_cookie(u8 node_num, u64 *cookie)
551 u64 tmpnode = node_num;
553 /* shift single byte of node num into top 8 bits */
556 spin_lock(&dlm_cookie_lock);
557 *cookie = (dlm_next_cookie | tmpnode);
558 if (++dlm_next_cookie & 0xff00000000000000ull) {
559 mlog(0, "This node's cookie will now wrap!\n");
562 spin_unlock(&dlm_cookie_lock);
565 enum dlm_status dlmlock(struct dlm_ctxt *dlm, int mode,
566 struct dlm_lockstatus *lksb, int flags,
567 const char *name, int namelen, dlm_astlockfunc_t *ast,
568 void *data, dlm_bastlockfunc_t *bast)
570 enum dlm_status status;
571 struct dlm_lock_resource *res = NULL;
572 struct dlm_lock *lock = NULL;
573 int convert = 0, recovery = 0;
575 /* yes this function is a mess.
576 * TODO: clean this up. lots of common code in the
577 * lock and convert paths, especially in the retry blocks */
579 dlm_error(DLM_BADARGS);
583 status = DLM_BADPARAM;
584 if (mode != LKM_EXMODE && mode != LKM_PRMODE && mode != LKM_NLMODE) {
589 if (flags & ~LKM_VALID_FLAGS) {
594 convert = (flags & LKM_CONVERT);
595 recovery = (flags & LKM_RECOVERY);
598 (!dlm_is_recovery_lock(name, namelen) || convert) ) {
602 if (convert && (flags & LKM_LOCAL)) {
603 mlog(ML_ERROR, "strange LOCAL convert request!\n");
608 /* CONVERT request */
610 /* if converting, must pass in a valid dlm_lock */
613 mlog(ML_ERROR, "NULL lock pointer in convert "
620 mlog(ML_ERROR, "NULL lockres pointer in convert "
624 dlm_lockres_get(res);
626 /* XXX: for ocfs2 purposes, the ast/bast/astdata/lksb are
627 * static after the original lock call. convert requests will
628 * ensure that everything is the same, or return DLM_BADARGS.
629 * this means that DLM_DENIED_NOASTS will never be returned.
631 if (lock->lksb != lksb || lock->ast != ast ||
632 lock->bast != bast || lock->astdata != data) {
633 status = DLM_BADARGS;
634 mlog(ML_ERROR, "new args: lksb=%p, ast=%p, bast=%p, "
635 "astdata=%p\n", lksb, ast, bast, data);
636 mlog(ML_ERROR, "orig args: lksb=%p, ast=%p, bast=%p, "
637 "astdata=%p\n", lock->lksb, lock->ast,
638 lock->bast, lock->astdata);
642 dlm_wait_for_recovery(dlm);
644 if (res->owner == dlm->node_num)
645 status = dlmconvert_master(dlm, res, lock, flags, mode);
647 status = dlmconvert_remote(dlm, res, lock, flags, mode);
648 if (status == DLM_RECOVERING || status == DLM_MIGRATING ||
649 status == DLM_FORWARD) {
650 /* for now, see how this works without sleeping
651 * and just retry right away. I suspect the reco
652 * or migration will complete fast enough that
653 * no waiting will be necessary */
654 mlog(0, "retrying convert with migration/recovery/"
663 status = DLM_BADARGS;
669 status = DLM_IVBUFLEN;
670 if (namelen > DLM_LOCKID_NAME_MAX || namelen < 1) {
675 dlm_get_next_cookie(dlm->node_num, &tmpcookie);
676 lock = dlm_new_lock(mode, dlm->node_num, tmpcookie, lksb);
683 dlm_wait_for_recovery(dlm);
685 /* find or create the lock resource */
686 res = dlm_get_lock_resource(dlm, name, namelen, flags);
688 status = DLM_IVLOCKID;
693 mlog(0, "type=%d, flags = 0x%x\n", mode, flags);
694 mlog(0, "creating lock: lock=%p res=%p\n", lock, res);
696 dlm_lock_attach_lockres(lock, res);
699 lock->astdata = data;
702 if (flags & LKM_VALBLK) {
703 mlog(0, "LKM_VALBLK passed by caller\n");
705 /* LVB requests for non PR, PW or EX locks are
707 if (mode < LKM_PRMODE)
708 flags &= ~LKM_VALBLK;
710 flags |= LKM_GET_LVB;
711 lock->lksb->flags |= DLM_LKSB_GET_LVB;
715 if (res->owner == dlm->node_num)
716 status = dlmlock_master(dlm, res, lock, flags);
718 status = dlmlock_remote(dlm, res, lock, flags);
720 if (status == DLM_RECOVERING || status == DLM_MIGRATING ||
721 status == DLM_FORWARD) {
722 mlog(0, "retrying lock with migration/"
723 "recovery/in progress\n");
725 /* no waiting for dlm_reco_thread */
727 if (status != DLM_RECOVERING)
730 mlog(0, "%s: got RECOVERING "
731 "for $RECOVERY lock, master "
732 "was %u\n", dlm->name,
734 /* wait to see the node go down, then
735 * drop down and allow the lockres to
736 * get cleaned up. need to remaster. */
737 dlm_wait_for_node_death(dlm, res->owner,
738 DLM_NODE_DEATH_WAIT_MAX);
740 dlm_wait_for_recovery(dlm);
745 if (status != DLM_NORMAL) {
746 lock->lksb->flags &= ~DLM_LKSB_GET_LVB;
747 if (status != DLM_NOTQUEUED)
754 if (status != DLM_NORMAL) {
755 if (lock && !convert)
757 // this is kind of unnecessary
758 lksb->status = status;
761 /* put lockres ref from the convert path
762 * or from dlm_get_lock_resource */
764 dlm_lockres_put(res);
768 EXPORT_SYMBOL_GPL(dlmlock);