⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dlmthread.c

📁 linux 内核源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
/* -*- mode: c; c-basic-offset: 8; -*- * vim: noexpandtab sw=8 ts=8 sts=0: * * dlmthread.c * * standalone DLM module * * 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/timer.h>#include <linux/kthread.h>#include <linux/delay.h>#include "cluster/heartbeat.h"#include "cluster/nodemanager.h"#include "cluster/tcp.h"#include "dlmapi.h"#include "dlmcommon.h"#include "dlmdomain.h"#define MLOG_MASK_PREFIX (ML_DLM|ML_DLM_THREAD)#include "cluster/masklog.h"static int dlm_thread(void *data);static void dlm_flush_asts(struct dlm_ctxt *dlm);#define dlm_lock_is_remote(dlm, lock)     ((lock)->ml.node != (dlm)->node_num)/* will exit holding res->spinlock, but may drop in function *//* waits until flags are cleared on res->state */void __dlm_wait_on_lockres_flags(struct dlm_lock_resource *res, int flags){	DECLARE_WAITQUEUE(wait, current);	assert_spin_locked(&res->spinlock);	add_wait_queue(&res->wq, &wait);repeat:	set_current_state(TASK_UNINTERRUPTIBLE);	if (res->state & flags) {		spin_unlock(&res->spinlock);		schedule();		spin_lock(&res->spinlock);		goto repeat;	}	remove_wait_queue(&res->wq, &wait);	__set_current_state(TASK_RUNNING);}int __dlm_lockres_has_locks(struct dlm_lock_resource *res){	if (list_empty(&res->granted) &&	    list_empty(&res->converting) &&	    list_empty(&res->blocked))		return 0;	return 1;}/* "unused": the lockres has no locks, is not on the dirty list, * has no inflight locks (in the gap between mastery and acquiring * the first lock), and has no bits in its refmap. * truly ready to be freed. */int __dlm_lockres_unused(struct dlm_lock_resource *res){	if (!__dlm_lockres_has_locks(res) &&	    (list_empty(&res->dirty) && !(res->state & DLM_LOCK_RES_DIRTY))) {		/* try not to scan the bitmap unless the first two		 * conditions are already true */		int bit = find_next_bit(res->refmap, O2NM_MAX_NODES, 0);		if (bit >= O2NM_MAX_NODES) {			/* since the bit for dlm->node_num is not			 * set, inflight_locks better be zero */			BUG_ON(res->inflight_locks != 0);			return 1;		}	}	return 0;}/* Call whenever you may have added or deleted something from one of * the lockres queue's. This will figure out whether it belongs on the * unused list or not and does the appropriate thing. */void __dlm_lockres_calc_usage(struct dlm_ctxt *dlm,			      struct dlm_lock_resource *res){	mlog_entry("%.*s\n", res->lockname.len, res->lockname.name);	assert_spin_locked(&dlm->spinlock);	assert_spin_locked(&res->spinlock);	if (__dlm_lockres_unused(res)){		if (list_empty(&res->purge)) {			mlog(0, "putting lockres %.*s:%p onto purge list\n",			     res->lockname.len, res->lockname.name, res);			res->last_used = jiffies;			dlm_lockres_get(res);			list_add_tail(&res->purge, &dlm->purge_list);			dlm->purge_count++;		}	} else if (!list_empty(&res->purge)) {		mlog(0, "removing lockres %.*s:%p from purge list, owner=%u\n",		     res->lockname.len, res->lockname.name, res, res->owner);		list_del_init(&res->purge);		dlm_lockres_put(res);		dlm->purge_count--;	}}void dlm_lockres_calc_usage(struct dlm_ctxt *dlm,			    struct dlm_lock_resource *res){	mlog_entry("%.*s\n", res->lockname.len, res->lockname.name);	spin_lock(&dlm->spinlock);	spin_lock(&res->spinlock);	__dlm_lockres_calc_usage(dlm, res);	spin_unlock(&res->spinlock);	spin_unlock(&dlm->spinlock);}static int dlm_purge_lockres(struct dlm_ctxt *dlm,			     struct dlm_lock_resource *res){	int master;	int ret = 0;	spin_lock(&res->spinlock);	if (!__dlm_lockres_unused(res)) {		spin_unlock(&res->spinlock);		mlog(0, "%s:%.*s: tried to purge but not unused\n",		     dlm->name, res->lockname.len, res->lockname.name);		return -ENOTEMPTY;	}	master = (res->owner == dlm->node_num);	if (!master)		res->state |= DLM_LOCK_RES_DROPPING_REF;	spin_unlock(&res->spinlock);	mlog(0, "purging lockres %.*s, master = %d\n", res->lockname.len,	     res->lockname.name, master);	if (!master) {		spin_lock(&res->spinlock);		/* This ensures that clear refmap is sent after the set */		__dlm_wait_on_lockres_flags(res, DLM_LOCK_RES_SETREF_INPROG);		spin_unlock(&res->spinlock);		/* drop spinlock to do messaging, retake below */		spin_unlock(&dlm->spinlock);		/* clear our bit from the master's refmap, ignore errors */		ret = dlm_drop_lockres_ref(dlm, res);		if (ret < 0) {			mlog_errno(ret);			if (!dlm_is_host_down(ret))				BUG();		}		mlog(0, "%s:%.*s: dlm_deref_lockres returned %d\n",		     dlm->name, res->lockname.len, res->lockname.name, ret);		spin_lock(&dlm->spinlock);	}	if (!list_empty(&res->purge)) {		mlog(0, "removing lockres %.*s:%p from purgelist, "		     "master = %d\n", res->lockname.len, res->lockname.name,		     res, master);		list_del_init(&res->purge);		dlm_lockres_put(res);		dlm->purge_count--;	}	__dlm_unhash_lockres(res);	/* lockres is not in the hash now.  drop the flag and wake up	 * any processes waiting in dlm_get_lock_resource. */	if (!master) {		spin_lock(&res->spinlock);		res->state &= ~DLM_LOCK_RES_DROPPING_REF;		spin_unlock(&res->spinlock);		wake_up(&res->wq);	}	return 0;}static void dlm_run_purge_list(struct dlm_ctxt *dlm,			       int purge_now){	unsigned int run_max, unused;	unsigned long purge_jiffies;	struct dlm_lock_resource *lockres;	spin_lock(&dlm->spinlock);	run_max = dlm->purge_count;	while(run_max && !list_empty(&dlm->purge_list)) {		run_max--;		lockres = list_entry(dlm->purge_list.next,				     struct dlm_lock_resource, purge);		/* Status of the lockres *might* change so double		 * check. If the lockres is unused, holding the dlm		 * spinlock will prevent people from getting and more		 * refs on it -- there's no need to keep the lockres		 * spinlock. */		spin_lock(&lockres->spinlock);		unused = __dlm_lockres_unused(lockres);		spin_unlock(&lockres->spinlock);		if (!unused)			continue;		purge_jiffies = lockres->last_used +			msecs_to_jiffies(DLM_PURGE_INTERVAL_MS);		/* Make sure that we want to be processing this guy at		 * this time. */		if (!purge_now && time_after(purge_jiffies, jiffies)) {			/* Since resources are added to the purge list			 * in tail order, we can stop at the first			 * unpurgable resource -- anyone added after			 * him will have a greater last_used value */			break;		}		dlm_lockres_get(lockres);		/* This may drop and reacquire the dlm spinlock if it		 * has to do migration. */		if (dlm_purge_lockres(dlm, lockres))			BUG();		dlm_lockres_put(lockres);		/* Avoid adding any scheduling latencies */		cond_resched_lock(&dlm->spinlock);	}	spin_unlock(&dlm->spinlock);}static void dlm_shuffle_lists(struct dlm_ctxt *dlm,			      struct dlm_lock_resource *res){	struct dlm_lock *lock, *target;	struct list_head *iter;	struct list_head *head;	int can_grant = 1;	//mlog(0, "res->lockname.len=%d\n", res->lockname.len);	//mlog(0, "res->lockname.name=%p\n", res->lockname.name);	//mlog(0, "shuffle res %.*s\n", res->lockname.len,	//	  res->lockname.name);	/* because this function is called with the lockres	 * spinlock, and because we know that it is not migrating/	 * recovering/in-progress, it is fine to reserve asts and	 * basts right before queueing them all throughout */	assert_spin_locked(&res->spinlock);	BUG_ON((res->state & (DLM_LOCK_RES_MIGRATING|			      DLM_LOCK_RES_RECOVERING|			      DLM_LOCK_RES_IN_PROGRESS)));converting:	if (list_empty(&res->converting))		goto blocked;	mlog(0, "res %.*s has locks on a convert queue\n", res->lockname.len,	     res->lockname.name);	target = list_entry(res->converting.next, struct dlm_lock, list);	if (target->ml.convert_type == LKM_IVMODE) {		mlog(ML_ERROR, "%.*s: converting a lock with no "		     "convert_type!\n", res->lockname.len, res->lockname.name);		BUG();	}	head = &res->granted;	list_for_each(iter, head) {		lock = list_entry(iter, struct dlm_lock, list);		if (lock==target)			continue;		if (!dlm_lock_compatible(lock->ml.type,					 target->ml.convert_type)) {			can_grant = 0;			/* queue the BAST if not already */			if (lock->ml.highest_blocked == LKM_IVMODE) {				__dlm_lockres_reserve_ast(res);				dlm_queue_bast(dlm, lock);			}			/* update the highest_blocked if needed */			if (lock->ml.highest_blocked < target->ml.convert_type)				lock->ml.highest_blocked =					target->ml.convert_type;		}	}	head = &res->converting;	list_for_each(iter, head) {		lock = list_entry(iter, struct dlm_lock, list);		if (lock==target)			continue;		if (!dlm_lock_compatible(lock->ml.type,					 target->ml.convert_type)) {			can_grant = 0;			if (lock->ml.highest_blocked == LKM_IVMODE) {				__dlm_lockres_reserve_ast(res);				dlm_queue_bast(dlm, lock);			}			if (lock->ml.highest_blocked < target->ml.convert_type)				lock->ml.highest_blocked =					target->ml.convert_type;		}	}	/* we can convert the lock */	if (can_grant) {		spin_lock(&target->spinlock);		BUG_ON(target->ml.highest_blocked != LKM_IVMODE);		mlog(0, "calling ast for converting lock: %.*s, have: %d, "		     "granting: %d, node: %u\n", res->lockname.len,		     res->lockname.name, target->ml.type,		     target->ml.convert_type, target->ml.node);		target->ml.type = target->ml.convert_type;		target->ml.convert_type = LKM_IVMODE;		list_move_tail(&target->list, &res->granted);		BUG_ON(!target->lksb);		target->lksb->status = DLM_NORMAL;		spin_unlock(&target->spinlock);		__dlm_lockres_reserve_ast(res);		dlm_queue_ast(dlm, target);		/* go back and check for more */		goto converting;	}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -