OpenBSD manual page server

Manual Page Search Parameters

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

vnodean overview of vnodes

A is an object in kernel memory that speaks the UNIX file interface (open, read, write, close, readdir, etc.). Vnodes can represent files, directories, FIFOs, domain sockets, block devices, character devices.

Each vnode has a set of methods which start with the string “VOP_”. These methods include (), (), (), (), (), and (). Many of these methods correspond closely to the equivalent file system call - open(2), read(2), write(2), rename(2), etc. Each file system (FFS, NFS, etc.) provides implementations for these methods.

The Virtual File System library (see vfs(9)) maintains a pool of vnodes. File systems cannot allocate their own vnodes; they must use the functions provided by the VFS to create and manage vnodes.

The definition of a vnode is as follows:

struct vnode {
	struct uvm_vnode *v_uvm;		/* uvm data */
	const struct vops *v_op;		/* vnode operations vector */
	enum	vtype v_type;			/* vnode type */
	enum	vtagtype v_tag;			/* type of underlying data */
	u_int	v_flag;				/* vnode flags (see below) */
	u_int   v_usecount;			/* reference count of users */
	u_int   v_uvcount;			/* unveil references */
	/* reference count of writers */
	u_int   v_writecount;
	/* Flags that can be read/written in interrupts */
	u_int   v_bioflag;
	u_int   v_holdcnt;			/* buffer references */
	u_int   v_id;				/* capability identifier */
	u_int	v_inflight;
	struct	mount *v_mount;			/* ptr to vfs we are in */
	TAILQ_ENTRY(vnode) v_freelist;		/* vnode freelist */
	LIST_ENTRY(vnode) v_mntvnodes;		/* vnodes for mount point */
	struct	buf_rb_bufs v_bufs_tree;	/* lookup of all bufs */
	struct	buflists v_cleanblkhd;		/* clean blocklist head */
	struct	buflists v_dirtyblkhd;		/* dirty blocklist head */
	u_int   v_numoutput;			/* num of writes in progress */
	LIST_ENTRY(vnode) v_synclist;		/* vnode with dirty buffers */
	union {
		struct mount	*vu_mountedhere;/* ptr to mounted vfs (VDIR) */
		struct socket	*vu_socket;	/* unix ipc (VSOCK) */
		struct specinfo	*vu_specinfo;	/* device (VCHR, VBLK) */
		struct fifoinfo	*vu_fifoinfo;	/* fifo (VFIFO) */
	} v_un;

	/* VFS namecache */
	struct namecache_rb_cache v_nc_tree;
	TAILQ_HEAD(, namecache) v_cache_dst;	 /* cache entries to us */

	void	*v_data;			/* private data for fs */
	struct	selinfo v_selectinfo;		/* identity of poller(s) */
};
#define	v_mountedhere	v_un.vu_mountedhere
#define	v_socket	v_un.vu_socket
#define	v_specinfo	v_un.vu_specinfo
#define	v_fifoinfo	v_un.vu_fifoinfo

When a client of the VFS requests a new vnode, the vnode allocation code can reuse an old vnode object that is no longer in use. Whether a vnode is in use is tracked by the vnode reference count (v_usecount). By convention, each open file handle holds a reference as do VM objects backed by files. A vnode with a reference count of 1 or more will not be deallocated or reused to point to a different file. So, if you want to ensure that your vnode doesn't become a different file under you, you better be sure you have a reference to it. A vnode that points to a valid file and has a reference count of 1 or more is called .

When a vnode's reference count drops to zero, it becomes , that is, a candidate for reuse. An inactive vnode still refers to a valid file and one can try to reactivate it using vget(9) (this is used a lot by caches).

Before the VFS can reuse an inactive vnode to refer to another file, it must clean all information pertaining to the old file. A cleaned out vnode is called a vnode.

To support forceable unmounts and the revoke(2) system call, the VFS may reclaim a vnode with a positive reference count. The reclaimed vnode is given to the dead file system, which returns errors for most operations. The reclaimed vnode will not be reused for another file until its reference count hits zero.

The getnewvnode(9) call allocates a vnode from the pool, possibly reusing an inactive vnode, and returns it to the caller. The vnode returned has a reference count (v_usecount) of 1.

The vref(9) call increments the reference count on the vnode. It may only be on a vnode with reference count of 1 or greater. The vrele(9) and vput(9) calls decrement the reference count. In addition, the vput(9) call also releases the vnode lock.

The vget(9) call, when used on an inactive vnode, will make the vnode active by bumping the reference count to one. When called on an active vnode, () increases the reference count by one. However, if the vnode is being reclaimed concurrently, then vget() will fail and return an error.

The vgone(9) and vgonel(9) calls orchestrate the reclamation of a vnode. They can be called on both active and inactive vnodes.

When transitioning a vnode to the reclaimed state, the VFS will call the VOP_RECLAIM(9) method. File systems use this method to free any file-system-specific data they attached to the vnode.

The vnode actually has two different types of locks: the vnode lock and the vnode reclamation lock (VXLOCK).

The vnode lock and its consistent use accomplishes the following:

  • It keeps a locked vnode from changing across certain pairs of VOP_ calls, thus preserving cached data. For example, it keeps the directory from changing between a VOP_LOOKUP(9) call and a VOP_CREATE(9). The () call makes sure the name doesn't already exist in the directory and finds free room in the directory for the new entry. The () call can then go ahead and create the file without checking if it already exists or looking for free space.
  • Some file systems rely on it to ensure that only one “thread” at a time is calling VOP_ vnode operations on a given file or directory. Otherwise, the file system's behavior is undefined.
  • On rare occasions, code will hold the vnode lock so that a series of VOP_ operations occurs as an atomic unit. (Of course, this doesn't work with network file systems like NFSv2 that don't have any notion of bundling a bunch of operations into an atomic unit.)
  • While the vnode lock is held, the vnode will not be reclaimed.

There is a discipline to using the vnode lock. Some VOP_ operations require that the vnode lock is held before being called.

The vnode lock is acquired by calling vn_lock(9) and released by calling VOP_UNLOCK(9).

A process is allowed to sleep while holding the vnode lock.

The implementation of the vnode lock is the responsibility of the individual file systems. Not all file systems implement it.

To prevent deadlocks, when acquiring locks on multiple vnodes, the lock of parent directory must be acquired before the lock on the child directory.

The vnode reclamation lock (VXLOCK) 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. The VXWANT flag is set by processes that wish to be woken up when reclamation is finished.

The vwaitforio(9) call is used to wait for all outstanding write I/Os associated with a vnode to complete.

The vnode capability, v_id, is a 32-bit version number on the vnode. Every time a vnode is reassigned to a new file, the vnode capability is changed. This is used by code that wishes to keep pointers to vnodes but doesn't want to hold a reference (e.g., caches). The code keeps both a vnode pointer and a copy of the capability. The code can later compare the vnode's capability to its copy and see if the vnode still points to the same file.

Note: for this to work, memory assigned to hold a struct vnode can only be used for another purpose when all pointers to it have disappeared. Since the vnode pool has no way of knowing when all pointers have disappeared, it never frees memory it has allocated for vnodes.

Most of the fields of the vnode structure should be treated as opaque and only manipulated through the proper APIs. This section describes the fields that are manipulated directly.

The v_flag attribute contains random flags related to various functions. They are summarized in the following table:

This vnode is the root of its file system.
This vnode is a pure text prototype.
This vnode is being used by kernel.
This vnode represents a tty(4).
This vnode is locked to change its underlying type.
A process is waiting for this vnode.
This vnode has an alias.
This vnode's underlying file system supports locking discipline.

The v_tag attribute indicates what file system the vnode belongs to. Very little code actually uses this attribute and its use is deprecated. Programmers should seriously consider using more object-oriented approaches (e.g. function tables). There is no safe way of defining new v_tag's for loadable file systems. The v_tag attribute is read-only.

The v_type attribute indicates what type of file (e.g. directory, regular, FIFO) this vnode is. This is used by the generic code for various checks. For example, the read(2) system call returns zero when a read is attempted on a directory.

Possible types are:

This vnode has no type.
This vnode represents a regular file.
This vnode represents a directory.
This vnode represents a block device.
This vnode represents a character device.
This vnode represents a symbolic link.
This vnode represents a socket.
This vnode represents a named pipe.
This vnode represents a bad or dead file.

The v_data attribute allows a file system to attach a piece of file system specific memory to the vnode. This contains information about the file that is specific to the file system (such as an inode pointer in the case of FFS).

The v_numoutput attribute indicates the number of pending synchronous and asynchronous writes on the vnode. It does not track the number of dirty buffers attached to the vnode. The attribute is used by code like fsync(2) to wait for all writes to complete before returning to the user. This attribute must be manipulated at splbio(9).

The v_writecount attribute tracks the number of write calls pending on the vnode.

The vast majority of vnode functions may not be called from interrupt context. The exceptions are () and (). The following fields of the vnode are manipulated at interrupt level: v_numoutput, v_holdcnt, v_dirtyblkhd, v_cleanblkhd, v_bioflag, v_freelist, and v_synclist. Any access to these fields should be protected by splbio(9).

uvn_attach(9), vaccess(9), vclean(9), vcount(9), vdevgone(9), vfinddev(9), vflush(9), vflushbuf(9), vfs(9), vget(9), vgone(9), vhold(9), vinvalbuf(9), vn_lock(9), VOP_LOOKUP(9), vput(9), vrecycle(9), vref(9), vrele(9), vwaitforio(9), vwakeup(9)

This document first appeared in OpenBSD 2.9.

October 20, 2021 OpenBSD-7.2