Manual browser: vnode(9)

VNODE(9) Kernel Developer's Manual VNODE(9)


vnode, vref, vrele, vrele_async, vget, vput, vhold, holdrele, getnewvnode, ungetnewvnode, vrecycle, vgone, vgonel, vdead_check, vflush, vaccess, bdevvp, cdevvp, vfinddev, vdevgone, vwakeup, vflushbuf, vinvalbuf, vtruncbuf, vprintkernel representation of a file or directory


#include <sys/param.h>
#include <sys/vnode.h>

vref(struct vnode *vp);

vrele(struct vnode *vp);

vrele_async(struct vnode *vp);

vget(struct vnode *vp, int lockflag);

vput(struct vnode *vp);

vhold(struct vnode *vp);

holdrele(struct vnode *vp);

getnewvnode(enum vtagtype tag, struct mount *mp, int (**vops)(void *), kmutex_t *slock, struct vnode **vpp);

ungetnewvnode(struct vnode *vp);

vrecycle(struct vnode *vp);

vgone(struct vnode *vp);

vgonel(struct vnode *vp, struct lwp *l);

vdead_check(struct vnode *vp, int flags);

vflush(struct mount *mp, struct vnode *skipvp, int flags);

vaccess(enum vtype type, mode_t file_mode, uid_t uid, gid_t gid, mode_t acc_mode, kauth_cred_t cred);

bdevvp(dev_t dev, struct vnode **vpp);

cdevvp(dev_t dev, struct vnode **vpp);

vfinddev(dev_t dev, enum vtype, struct vnode **vpp);

vdevgone(int maj, int minl, int minh, enum vtype type);

vwakeup(struct buf *bp);

vflushbuf(struct vnode *vp, int sync);

vinvalbuf(struct vnode *vp, int flags, kauth_cred_t cred, struct lwp *l, int slpflag, int slptimeo);

vtruncbuf(struct vnode *vp, daddr_t lbn, int slpflag, int slptimeo);

vprint(const char *label, struct vnode *vp);


The vnode is the focus of all file activity in NetBSD. There is a unique vnode allocated for each active file, directory, mounted-on file, fifo, domain socket, symbolic link and device. The kernel has no concept of a file's underlying structure and so it relies on the information stored in the vnode to describe the file. Thus, the vnode associated with a file holds all the administration information pertaining to it.

When a process requests an operation on a file, the vfs(9) interface passes control to a file system type dependent function to carry out the operation. If the file system type dependent function finds that a vnode representing the file is not in main memory, it dynamically allocates a new vnode from the system main memory pool. Once allocated, the vnode is attached to the data structure pointer associated with the cause of the vnode allocation and it remains resident in the main memory until the system decides that it is no longer needed and can be recycled.

The vnode has the following structure:

struct vnode { 
	struct uvm_object v_uobj;		/* the VM object */ 
	kcondvar_t	v_cv;			/* synchronization */ 
	voff_t		v_size;			/* size of file */ 
	voff_t		v_writesize;		/* new size after write */ 
	int		v_iflag;		/* VI_* flags */ 
	int		v_vflag;		/* VV_* flags */ 
	int		v_uflag;		/* VU_* flags */ 
	int		v_numoutput;		/* # of pending writes */ 
	int		v_writecount;		/* ref count of writers */ 
	int		v_holdcnt;		/* page & buffer refs */ 
	int		v_synclist_slot;	/* synclist slot index */ 
	struct mount	*v_mount;		/* ptr to vfs we are in */ 
	int		(**v_op)(void *);	/* vnode operations vector */ 
	TAILQ_ENTRY(vnode) v_freelist;		/* vnode freelist */ 
	struct vnodelst	*v_freelisthd;		/* which freelist? */ 
	TAILQ_ENTRY(vnode) v_mntvnodes;		/* vnodes for mount point */ 
	struct buflists	v_cleanblkhd;		/* clean blocklist head */ 
	struct buflists	v_dirtyblkhd;		/* dirty blocklist head */ 
	TAILQ_ENTRY(vnode) v_synclist;		/* vnodes with dirty bufs */ 
	LIST_HEAD(, namecache) v_dnclist;	/* namecaches (children) */ 
	LIST_HEAD(, namecache) v_nclist;	/* namecaches (parent) */ 
	union { 
		struct mount	*vu_mountedhere;/* ptr to vfs (VDIR) */ 
		struct socket	*vu_socket;	/* unix ipc (VSOCK) */ 
		struct specnode	*vu_specnode;	/* device (VCHR, VBLK) */ 
		struct fifoinfo	*vu_fifoinfo;	/* fifo (VFIFO) */ 
		struct uvm_ractx *vu_ractx;	/* read-ahead ctx (VREG) */ 
	} v_un; 
	enum vtype	v_type;			/* vnode type */ 
	enum vtagtype	v_tag;			/* type of underlying data */ 
	struct vnlock	v_lock;			/* lock for this vnode */ 
	void 		*v_data;		/* private data for fs */ 
	struct klist	v_klist;		/* notes attached to vnode */ 

Most members of the vnode structure should be treated as opaque and only manipulated using the proper functions. There are some rather common exceptions detailed throughout this page.

Files and file systems are inextricably linked with the virtual memory system and v_uobj contains the data maintained by the virtual memory system. For compatibility with code written before the integration of uvm(9) into NetBSD, C-preprocessor directives are used to alias the members of v_uobj.

Vnode flags are recorded by v_iflag, v_vflag and v_uflag. Valid flags are:

This vnode is the root of its file system.
This vnode is being used by the kernel; only used to skip quota files in vflush().
This vnode represents a tty; used when reading dead vnodes.
This vnode might have user mappings.
This file system is MP safe.
This vnode's file system supports locking.
This vnode is a pure text prototype.
This vnode has executable mappings.
This vnode might have PROT_WRITE user mappings.
This vnode might have dirty pages due to VWRITEMAP.
This vnode is currently locked to change underlying type.
This vnode is on syncer work-list.
A dummy marker vnode.
This vnode is on a layered file system.
This vnode shares its v_interlock with other vnodes.
This vnode has been reclaimed and is no longer attached to a file system.
This vnode is involved in a directory operation. This flag is used exclusively by LFS.

The VI_XLOCK flag is used to prevent multiple processes from entering the vnode reclamation code. It is also used as a flag to indicate that reclamation is in progress. Before v_iflag can be modified, the v_interlock mutex must be acquired. See lock(9) for details on the kernel locking API.

Each vnode has three reference counts: v_usecount, v_writecount and v_holdcnt. The first is the number of active references within the kernel to the vnode. This count is maintained by vref(), vrele(), vrele_async(), and vput(). The second is the number of active references within the kernel to the vnode performing write access to the file. It is maintained by the open(2) and close(2) system calls. The third is the number of references within the kernel requiring the vnode to remain active and not be recycled. This count is maintained by vhold() and holdrele(). When both the v_usecount and v_holdcnt reach zero, the vnode is recycled to the freelist and may be reused for another file. The transition to and from the freelist is handled by a kernel thread and vrecycle(). Access to v_usecount, v_writecount and v_holdcnt is also protected by the v_interlock mutex.

The number of pending synchronous and asynchronous writes on the vnode are recorded in v_numoutput. It is used by fsync(2) to wait for all writes to complete before returning to the user. Its value must only be modified at splbio (see spl(9)). It does not track the number of dirty buffers attached to the vnode.

v_dnclist and v_nclist are used by namecache(9) to maintain the list of associated entries so that cache_purge(9) can purge them.

The link to the file system which owns the vnode is recorded by v_mount. See vfsops(9) for further information of file system mount status.

The v_op pointer points to its vnode operations vector. This vector describes what operations can be done to the file associated with the vnode. The system maintains one vnode operations vector for each file system type configured into the kernel. The vnode operations vector contains a pointer to a function for each operation supported by the file system. See vnodeops(9) for a description of vnode operations.

When not in use, vnodes are kept on the freelist through v_freelist. The vnodes still reference valid files but may be reused to refer to a new file at any time. When a valid vnode which is on the freelist is used again, the user must call vget() to increment the reference count and retrieve it from the freelist. When a user wants a new vnode for another file, getnewvnode() is invoked to allocate a vnode and initialize it for the new file.

The type of object the vnode represents is recorded by v_type. It is used by generic code to perform checks to ensure operations are performed on valid file system objects. Valid types are:

The vnode has no type.
The vnode represents a regular file.
The vnode represents a directory.
The vnode represents a block special device.
The vnode represents a character special device.
The vnode represents a symbolic link.
The vnode represents a socket.
The vnode represents a pipe.
The vnode represents a bad file (not currently used).

Vnode tag types are used by external programs only (e.g., pstat(8)), and should never be inspected by the kernel. Its use is deprecated since new v_tag values cannot be defined for loadable file systems. The v_tag member is read-only. Valid tag types are:

non file system
universal file system
network file system
memory file system
FAT file system
log-structured file system
loopback file system
file descriptor file system
null file system layer
uid/gid remapping file system layer
kernel interface file system
process interface file system
AFS file system
ISO 9660 file system(s)
union file system
Amiga file system
Linux's ext2 file system
Coda file system
filecore file system
Microsoft NT's file system
virtual file system
overlay file system
SMB file system
pseudo-terminal device file system
efficient memory file system
universal disk format file system
systemV boot file system

All vnode locking operations use v_lock. This lock is acquired by calling vn_lock(9) and released by calling VOP_UNLOCK(9). The reason for this asymmetry is that vn_lock(9) is a wrapper for VOP_LOCK(9) with extra checks, while the unlocking step usually does not need additional checks and thus has no wrapper.

The vnode locking operation is complicated because it is used for many purposes. Sometimes it is used to bundle a series of vnode operations (see vnodeops(9)) into an atomic group. Many file systems rely on it to prevent race conditions in updating file system type specific data structures rather than using their own private locks. The vnode lock can operate as a multiple-reader (shared-access lock) or single-writer lock (exclusive access lock), however many current file system implementations were written assuming only single-writer locking. Multiple-reader locking functions equivalently only in the presence of big-lock SMP locking or a uni-processor machine. The lock may be held while sleeping. While the v_lock is acquired, the holder is guaranteed that the vnode will not be reclaimed or invalidated. Most file system functions require that you hold the vnode lock on entry. See lock(9) for details on the kernel locking API.

Each file system underlying a vnode allocates its own private area and hangs it from v_data.

Most functions discussed in this page that operate on vnodes cannot be called from interrupt context. The members v_numoutput, v_holdcnt, v_dirtyblkhd, v_cleanblkhd, v_freelist, and v_synclist are modified in interrupt context and must be protected by splbio(9) unless it is certain that there is no chance an interrupt handler will modify them. The vnode lock must not be acquired within interrupt context.


Increment v_usecount of the vnode vp. Any kernel thread system which uses a vnode (e.g., during the operation of some algorithm or to store in a data structure) should call vref().
Decrement v_usecount of unlocked vnode vp. Any code in the system which is using a vnode should call vrele() when it is finished with the vnode. If v_usecount of the vnode reaches zero and v_holdcnt is greater than zero, the vnode is placed on the holdlist. If both v_usecount and v_holdcnt are zero, the vnode is placed on the freelist.
Will asychronously release the vnode in different context than the caller, sometime after the call.
vget(vp, lockflags)
Reclaim vnode vp from the freelist, increment its reference count and lock it. The argument lockflags specifies the rwlock(9) flags used to lock the vnode. If the VI_XLOCK flag is set in vp's v_flag, vnode vp is being recycled in vgone() and the calling thread sleeps until the transition is complete. When it is awakened, an error is returned to indicate that the vnode is no longer usable.
Unlock vnode vp and decrement its v_usecount. Depending on the reference counts, move the vnode to the holdlist or the freelist. This operation is functionally equivalent to calling VOP_UNLOCK(9) followed by vrele().
Mark the vnode vp as active by incrementing vp->v_holdcnt and moving the vnode from the freelist to the holdlist. Once on the holdlist, the vnode will not be recycled until it is released with holdrele().
Mark the vnode vp as inactive by decrementing vp->v_holdcnt and moving the vnode from the holdlist to the freelist.
getnewvnode(tag, mp, vops, slock, vpp)
Retrieve the next vnode from the freelist. getnewvnode() allocates a new vnode. The new vnode is returned in the address specified by vpp.

The argument mp is the mount point for the file system requested the new vnode. Before retrieving the new vnode, the file system is checked if it is busy (such as currently unmounting). An error is returned if the file system is unmounted.

The argument tag is the vnode tag assigned to *vpp->v_tag. The argument vops is the vnode operations vector of the file system requesting the new vnode. If a vnode is successfully retrieved zero is returned, otherwise an appropriate error code is returned. If slock is not NULL, it specifies the lock to share for v_interlock. The reference will be held on the lock and sharing noted. Reference will be released and lock unshared when the vnode gets recycled. If NULL (regular case), vnode will use its own interlock.

Undo the operation of getnewvnode(). The argument vp is the vnode to return to the freelist. This function is needed for VFS_VGET(9) which may need to push back a vnode in case of a locking race condition.
Recycle the referenced vnode vp if this is the last reference. vrecycle() is a null operation if the reference count is greater than one.
Eliminate all activity associated with the unlocked vnode vp in preparation for recycling.
vgonel(vp, p)
Eliminate all activity associated with the locked vnode vp in preparation for recycling.
vdead_check(vp, flags)
Check the vnode vp for being or becoming dead. Returns ENOENT for a dead vnode and zero otherwise. If flags is VDEAD_NOWAIT it will return EBUSY if the vnode is becoming dead and the function will not sleep.

Whenever this function returns a non-zero value all future calls for this vp will also return a non-zero value.

vflush(mp, skipvp, flags)
Remove any vnodes in the vnode table belonging to mount point mp. If skipvp is not NULL it is exempt from being flushed. The argument flags is a set of flags modifying the operation of vflush(). If FORCECLOSE is not specified, there should not be any active vnodes and the error EBUSY is returned if any are found (this is a user error, not a system error). If FORCECLOSE is specified, active vnodes that are found are detached. If WRITECLOSE is set, only flush out regular file vnodes open for writing. SKIPSYSTEM causes any vnodes marked V_SYSTEM to be skipped.
vaccess(type, file_mode, uid, gid, acc_mode, cred)
Do access checking by comparing the file's permissions to the caller's desired access type acc_mode and credentials cred.
bdevvp(dev, vpp)
Create a vnode for a block device. bdevvp() is used for root file systems, swap areas and for memory file system special devices.
cdevvp(dev, vpp)
Create a vnode for a character device. cdevvp() is used for the console and kernfs special devices.
vfinddev(dev, vtype, vpp)
Lookup a vnode by device number. The vnode is referenced and returned in the address specified by vpp.
vdevgone(int maj, int min, int minh, enum vtype type)
Reclaim all vnodes that correspond to the specified minor number range minl to minh (endpoints inclusive) of the specified major maj.
Update outstanding I/O count vp->v_numoutput for the vnode bp->b_vp and do a wakeup if requested and vp->vflag has VBWAIT set.
vflushbuf(vp, sync)
Flush all dirty buffers to disk for the file with the locked vnode vp. The argument sync specifies whether the I/O should be synchronous and vflushbuf() will sleep until vp->v_numoutput is zero and vp->v_dirtyblkhd is empty.
vinvalbuf(vp, flags, cred, l, slpflag, slptimeo)
Flush out and invalidate all buffers associated with locked vnode vp. The argument l and cred specified the calling process and its credentials. The ltsleep(9) flag and timeout are specified by the arguments slpflag and slptimeo respectively. If the operation is successful zero is returned, otherwise an appropriate error code is returned.
vtruncbuf(vp, lbn, slpflag, slptimeo)
Destroy any in-core buffers past the file truncation length for the locked vnode vp. The truncation length is specified by lbn. vtruncbuf() will sleep while the I/O is performed, The ltsleep(9) flag and timeout are specified by the arguments slpflag and slptimeo respectively. If the operation is successful zero is returned, otherwise an appropriate error code is returned.
vprint(label, vp)
This function is used by the kernel to dump vnode information during a panic. It is only used if the kernel option DIAGNOSTIC is compiled into the kernel. The argument label is a string to prefix the information dump of vnode vp.


The vnode framework is implemented within the file sys/kern/vfs_subr.c.


The locking protocol is inconsistent. Many vnode operations are passed locked vnodes on entry but release the lock before they exit. The locking protocol is used in some places to attempt to make a series of operations atomic (e.g., access check then operation). This does not work for non-local file systems that do not support locking (e.g., NFS). The vnode interface would benefit from a simpler locking protocol.
March 24, 2014 NetBSD 7.0