---
name: "VOP_LOCK"
section: "9"
platform: "linux"
package: "freebsd-manpages"
version: "6.0-5"
release: "Debian 4.0 (Etch)"
platforms: ["linux", "freebsd"]
url: "https://new.carta.tech/VOP_LOCK"
---

# VOP\_LOCK(9)

serialize access to a vnode

## 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 thread \*td*);

*int*\

**VOP\_UNLOCK**(*struct vnode \*vp*, *int flags*, *struct thread \*td*);

*int*\

**VOP\_ISLOCKED**(*struct vnode \*vp*, *struct thread \*td*);

*int*\

**vn\_lock**(*struct vnode \*vp*, *int flags*, *struct thread \*td*);

## DESCRIPTION

These calls are used to serialize access to the file system, 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.

  - LK\_DRAIN

    Wait for all lock activity to end.

    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.

    - LK\_REENABLE

      Lock is to be reenabled after drain.

    - LK\_NOPAUSE

      No spinloop.

      The lock type may be *or*'ed with these control flags:

      - LK\_INTERLOCK

        Specify when the caller already has a simple lock **(**(*VOP\_LOCK*) will unlock the simple lock after getting the lock).

      - LK\_RETRY

        Retry until locked.

      - LK\_NOOBJ

        Do not create object.

- *td*

  Thread 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 file system 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, PINOD, "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, PINOD, "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 ALSO

[vnode(9)](/vnode/9.md)

## AUTHORS

This manual page was written by Doug Rabson.
