📄 dlmlock.c
字号:
/* -*- mode: c; c-basic-offset: 8; -*- * vim: noexpandtab sw=8 ts=8 sts=0: * * dlmlock.c * * underlying calls for lock creation * * Copyright (C) 2004 Oracle. All rights reserved. * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public * License along with this program; if not, write to the * Free Software Foundation, Inc., 59 Temple Place - Suite 330, * Boston, MA 021110-1307, USA. * */#include <linux/module.h>#include <linux/fs.h>#include <linux/types.h>#include <linux/slab.h>#include <linux/highmem.h>#include <linux/utsname.h>#include <linux/init.h>#include <linux/sysctl.h>#include <linux/random.h>#include <linux/blkdev.h>#include <linux/socket.h>#include <linux/inet.h>#include <linux/spinlock.h>#include <linux/delay.h>#include "cluster/heartbeat.h"#include "cluster/nodemanager.h"#include "cluster/tcp.h"#include "dlmapi.h"#include "dlmcommon.h"#include "dlmconvert.h"#define MLOG_MASK_PREFIX ML_DLM#include "cluster/masklog.h"static spinlock_t dlm_cookie_lock = SPIN_LOCK_UNLOCKED;static u64 dlm_next_cookie = 1;static enum dlm_status dlm_send_remote_lock_request(struct dlm_ctxt *dlm, struct dlm_lock_resource *res, struct dlm_lock *lock, int flags);static void dlm_init_lock(struct dlm_lock *newlock, int type, u8 node, u64 cookie);static void dlm_lock_release(struct kref *kref);static void dlm_lock_detach_lockres(struct dlm_lock *lock);/* Tell us whether we can grant a new lock request. * locking: * caller needs: res->spinlock * taken: none * held on exit: none * returns: 1 if the lock can be granted, 0 otherwise. */static int dlm_can_grant_new_lock(struct dlm_lock_resource *res, struct dlm_lock *lock){ struct list_head *iter; struct dlm_lock *tmplock; list_for_each(iter, &res->granted) { tmplock = list_entry(iter, struct dlm_lock, list); if (!dlm_lock_compatible(tmplock->ml.type, lock->ml.type)) return 0; } list_for_each(iter, &res->converting) { tmplock = list_entry(iter, struct dlm_lock, list); if (!dlm_lock_compatible(tmplock->ml.type, lock->ml.type)) return 0; } return 1;}/* performs lock creation at the lockres master site * locking: * caller needs: none * taken: takes and drops res->spinlock * held on exit: none * returns: DLM_NORMAL, DLM_NOTQUEUED */static enum dlm_status dlmlock_master(struct dlm_ctxt *dlm, struct dlm_lock_resource *res, struct dlm_lock *lock, int flags){ int call_ast = 0, kick_thread = 0; enum dlm_status status = DLM_NORMAL; mlog_entry("type=%d\n", lock->ml.type); spin_lock(&res->spinlock); /* if called from dlm_create_lock_handler, need to * ensure it will not sleep in dlm_wait_on_lockres */ status = __dlm_lockres_state_to_status(res); if (status != DLM_NORMAL && lock->ml.node != dlm->node_num) { /* erf. state changed after lock was dropped. */ spin_unlock(&res->spinlock); dlm_error(status); return status; } __dlm_wait_on_lockres(res); __dlm_lockres_reserve_ast(res); if (dlm_can_grant_new_lock(res, lock)) { mlog(0, "I can grant this lock right away\n"); /* got it right away */ lock->lksb->status = DLM_NORMAL; status = DLM_NORMAL; dlm_lock_get(lock); list_add_tail(&lock->list, &res->granted); /* for the recovery lock, we can't allow the ast * to be queued since the dlmthread is already * frozen. but the recovery lock is always locked * with LKM_NOQUEUE so we do not need the ast in * this special case */ if (!dlm_is_recovery_lock(res->lockname.name, res->lockname.len)) { kick_thread = 1; call_ast = 1; } } else { /* for NOQUEUE request, unless we get the * lock right away, return DLM_NOTQUEUED */ if (flags & LKM_NOQUEUE) status = DLM_NOTQUEUED; else { dlm_lock_get(lock); list_add_tail(&lock->list, &res->blocked); kick_thread = 1; } } spin_unlock(&res->spinlock); wake_up(&res->wq); /* either queue the ast or release it */ if (call_ast) dlm_queue_ast(dlm, lock); else dlm_lockres_release_ast(dlm, res); dlm_lockres_calc_usage(dlm, res); if (kick_thread) dlm_kick_thread(dlm, res); return status;}void dlm_revert_pending_lock(struct dlm_lock_resource *res, struct dlm_lock *lock){ /* remove from local queue if it failed */ list_del_init(&lock->list); lock->lksb->flags &= ~DLM_LKSB_GET_LVB;}/* * locking: * caller needs: none * taken: takes and drops res->spinlock * held on exit: none * returns: DLM_DENIED, DLM_RECOVERING, or net status */static enum dlm_status dlmlock_remote(struct dlm_ctxt *dlm, struct dlm_lock_resource *res, struct dlm_lock *lock, int flags){ enum dlm_status status = DLM_DENIED; mlog_entry("type=%d\n", lock->ml.type); mlog(0, "lockres %.*s, flags = 0x%x\n", res->lockname.len, res->lockname.name, flags); spin_lock(&res->spinlock); /* will exit this call with spinlock held */ __dlm_wait_on_lockres(res); res->state |= DLM_LOCK_RES_IN_PROGRESS; /* add lock to local (secondary) queue */ dlm_lock_get(lock); list_add_tail(&lock->list, &res->blocked); lock->lock_pending = 1; spin_unlock(&res->spinlock); /* spec seems to say that you will get DLM_NORMAL when the lock * has been queued, meaning we need to wait for a reply here. */ status = dlm_send_remote_lock_request(dlm, res, lock, flags); spin_lock(&res->spinlock); res->state &= ~DLM_LOCK_RES_IN_PROGRESS; lock->lock_pending = 0; if (status != DLM_NORMAL) { if (status != DLM_NOTQUEUED) dlm_error(status); dlm_revert_pending_lock(res, lock); dlm_lock_put(lock); } else if (dlm_is_recovery_lock(res->lockname.name, res->lockname.len)) { /* special case for the $RECOVERY lock. * there will never be an AST delivered to put * this lock on the proper secondary queue * (granted), so do it manually. */ mlog(0, "%s: $RECOVERY lock for this node (%u) is " "mastered by %u; got lock, manually granting (no ast)\n", dlm->name, dlm->node_num, res->owner); list_del_init(&lock->list); list_add_tail(&lock->list, &res->granted); } spin_unlock(&res->spinlock); dlm_lockres_calc_usage(dlm, res); wake_up(&res->wq); return status;}/* for remote lock creation. * locking: * caller needs: none, but need res->state & DLM_LOCK_RES_IN_PROGRESS * taken: none * held on exit: none * returns: DLM_NOLOCKMGR, or net status */static enum dlm_status dlm_send_remote_lock_request(struct dlm_ctxt *dlm, struct dlm_lock_resource *res, struct dlm_lock *lock, int flags){ struct dlm_create_lock create; int tmpret, status = 0; enum dlm_status ret; mlog_entry_void(); memset(&create, 0, sizeof(create)); create.node_idx = dlm->node_num; create.requested_type = lock->ml.type; create.cookie = lock->ml.cookie; create.namelen = res->lockname.len; create.flags = cpu_to_be32(flags); memcpy(create.name, res->lockname.name, create.namelen); tmpret = o2net_send_message(DLM_CREATE_LOCK_MSG, dlm->key, &create, sizeof(create), res->owner, &status); if (tmpret >= 0) { // successfully sent and received ret = status; // this is already a dlm_status } else { mlog_errno(tmpret); if (dlm_is_host_down(tmpret)) { ret = DLM_RECOVERING; mlog(0, "node %u died so returning DLM_RECOVERING " "from lock message!\n", res->owner); } else { ret = dlm_err_to_dlm_status(tmpret); } } return ret;}void dlm_lock_get(struct dlm_lock *lock){ kref_get(&lock->lock_refs);}void dlm_lock_put(struct dlm_lock *lock){ kref_put(&lock->lock_refs, dlm_lock_release);}static void dlm_lock_release(struct kref *kref){ struct dlm_lock *lock; lock = container_of(kref, struct dlm_lock, lock_refs); BUG_ON(!list_empty(&lock->list)); BUG_ON(!list_empty(&lock->ast_list)); BUG_ON(!list_empty(&lock->bast_list)); BUG_ON(lock->ast_pending); BUG_ON(lock->bast_pending); dlm_lock_detach_lockres(lock); if (lock->lksb_kernel_allocated) { mlog(0, "freeing kernel-allocated lksb\n"); kfree(lock->lksb); } kfree(lock);}/* associate a lock with it's lockres, getting a ref on the lockres */void dlm_lock_attach_lockres(struct dlm_lock *lock, struct dlm_lock_resource *res){ dlm_lockres_get(res); lock->lockres = res;}/* drop ref on lockres, if there is still one associated with lock */static void dlm_lock_detach_lockres(struct dlm_lock *lock){ struct dlm_lock_resource *res; res = lock->lockres; if (res) { lock->lockres = NULL; mlog(0, "removing lock's lockres reference\n"); dlm_lockres_put(res); }}static void dlm_init_lock(struct dlm_lock *newlock, int type, u8 node, u64 cookie){ INIT_LIST_HEAD(&newlock->list); INIT_LIST_HEAD(&newlock->ast_list); INIT_LIST_HEAD(&newlock->bast_list); spin_lock_init(&newlock->spinlock); newlock->ml.type = type; newlock->ml.convert_type = LKM_IVMODE; newlock->ml.highest_blocked = LKM_IVMODE; newlock->ml.node = node;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -