VOP_LOCK(9) BSD Kernel Developer's Manual VOP_LOCK(9)NAME
VOP_LOCK, VOP_UNLOCK, VOP_ISLOCKED, vn_lock — serialize access to a vnode
SYNOPSIS
#include <sys/param.h>
#include <sys/lock.h>
#include <sys/vnode.h>
int
VOP_LOCK(struct vnode *vp, int flags, struct proc *p);
int
VOP_UNLOCK(struct vnode *vp, int flags, struct proc *p);
int
VOP_ISLOCKED(struct vnode *vp, struct proc *p);
int
vn_lock(struct vnode *vp, int flags, struct proc *p);
DESCRIPTION
These calls are used to serialize access to the filesystem, such as to
prevent two writes to the same file from happening at the same time.
The arguments are:
vp the vnode being locked or unlocked
flags One of the lock request types:
LK_SHARED Shared lock
LK_EXCLUSIVE Exclusive lock
LK_UPGRADE Shared-to-exclusive upgrade
LK_EXCLUPGRADE First shared-to-exclusive upgrade
LK_DOWNGRADE Exclusive-to-shared downgrade
LK_RELEASE Release any type of lock
The lock type may be or'ed with these lock flags:
LK_NOWAIT Do not sleep to wait for lock
LK_SLEEPFAIL Sleep, then return failure
LK_CANRECURSE Allow recursive exclusive lock
The lock type may be or'ed with these control flags:
LK_RETRY Retry until locked
LK_NOOBJ Don't create object
p process context to use for the locks
Kernel code should use vn_lock() to lock a vnode rather than calling
VOP_LOCK() directly.
RETURN VALUES
Zero is returned on success, otherwise an error is returned.
PSEUDOCODE
struct vopnode {
int von_flag;
/*
* Other filesystem specific data.
*/
...;
};
#define VON_LOCKED 1
#define VON_WANTED 2
#define VTOVON(vp) ((struct vopnode *) (vp)->v_data)
int
vop_lock(struct vnode *vp)
{
struct vopnode* vop;
start:
while (vp->v_flag & VXLOCK) {
vp->v_flag |= VXWANT;
tsleep((caddr_t)vp, 0, "voplk1", 0);
}
if (vp->v_tag == VT_NON)
return ENOENT;
vop = VTOVON(vp);
if (vop->von_flag & VON_LOCKED) {
vop->von_flag |= VON_WANTED;
tsleep((caddr_t) vop, 0, "voplk2", 0);
goto start;
}
vop->von_flag |= VON_LOCKED;
return 0;
}
int
vop_unlock(struct vnode *vp)
{
struct vopnode *vop = VTOVON(vp);
if ((vop->von_flag & VON_LOCKED) == 0) {
panic("vop_unlock not locked");
}
vop->von_flag &= ~VON_LOCKED;
if (vop->von_flag & VON_WANTED) {
vop->von_flag &= ~VON_WANTED;
wakeup((caddr_t) vop);
}
return 0;
}
int
vop_islocked(struct vnode *vp)
{
struct vopnode *vop = VTOVON(vp);
if (vop->von_flag & VON_LOCKED)
return 1;
else
return 0;
}
SEE ALSOvnode(9)AUTHORS
This man page was written by Doug Rabson.
BSD April 23, 2006 BSD