xref: /openbsd-src/share/man/man9/vnode.9 (revision 3a3fbb3f2e2521ab7c4a56b7ff7462ebd9095ec5)
1.Dd February 22, 2001
2.Dt vnode 9
3.Os OpenBSD 2.9
4.Sh NAME
5.Nm vnode
6.Nd an overview of vnodes
7.Sh DESCRIPTION
8A vnode is an object that speaks the UNIX file interface (open,
9read, write, close, readdir, etc.). Vnodes can represent files,
10directories, FIFOs, domain sockets, block devices, character devices.
11.Pp
12Each vnode has a set of methods which start with string 'VOP_'. These
13methods include VOP_OPEN, VOP_READ, VOP_WRITE, VOP_RENAME, VOP_CLOSE,
14VOP_MKDIR. Many of these methods correspond closely to the equivalent
15file system call--open, read, write, rename, etc. Each file system (FFS,
16NFS, etc.) provides implementations for these methods.
17.Pp
18The Virtual File System (VFS) library maintains a pool of vnodes. File systems
19cannot allocate their own vnodes; they must use the functions provided
20by the VFS to create and manage vnodes.
21.Ss Vnode state
22.Pp
23Vnodes have a reference count which corresponds to the number of kernel
24objects that hold references to the vnode. A positive reference count keeps
25the vnode off of the free list, which prevents the vnode from being recycled
26to refer to a different file.
27.Pp
28Vnodes that refer to a valid file and have a reference count of 1 or
29greater are "active". When a vnodes reference count drops to zero, it
30is "inactivated" and becomes "inactive". Inactive vnodes are placed on the
31free list, to be re-used to represent other files.
32.Pp
33Before a struct vnode can be re-used to refer to another file, it must
34be cleaned out of all information pertaining to the old file. A vnode that
35doesn't refer to any file is called a "reclaimed" vnode.
36.Pp
37The VFS may "reclaim" a vnode with a positive reference count.
38This is done when the underlying file is revoked, as happens with the
39revoke system call or through a forceable unmount. Such a vnode is given
40to the dead file system, which returns errors for most operations.
41The vnode will not be re-used for another file until its reference count
42hits zero.
43.Pp
44There are three states then for a vnode: active, inactive, and reclaimed.
45All transitions are meaningful except reclaimed to inactive.
46.Ss Vnode pool
47The
48.Xr getnewvnode 9
49system call returns a fresh active vnode from the vnode
50pool assigned to the file system specified in its arguments.
51The vnode returned has a reference count (v_usecount) of 1.
52.Pp
53The
54.Xr vref 9
55call increments the reference count on the vnode. It may only be
56on a vnode with reference count of 1 or greater. The
57.Xr vrele 9
58and
59.Xr vput 9
60calls decrement the reference count.
61In addition, the
62.Xr vput 9
63call also releases the vnode lock.
64.Pp
65The
66.Xr vget 9
67call, when used on an inactive vnode, will make the vnode "active"
68by bumping the reference count to one. When called on an active vnode,
69vget increases the reference count by one. However, if the vnode
70is being reclaimed concurrently, then vget will fail and return an error.
71.Pp
72The
73.Xr vgone 9
74and
75.Xr vgonel 9
76orchestrate the reclamation of a vnode. They can be called on both
77active and inactive vnodes.
78.Pp
79While transitioning a vnode to the "reclaimed" state, the VFS will call
80.Xr vop_reclaim 9
81method. File systems use this method to free any file-system specific data
82they attached to the vnode.
83.Ss Vnode locks
84The vnode actually has three different types of lock: the vnode lock,
85the vnode interlock, and the vnode reclamation lock (VXLOCK).
86.Ss The vnode lock
87The most general lock is the vnode lock.
88This lock is acquired by calling
89.Xr vn_lock 9
90and released by calling
91.Xr vn_unlock 9 .
92The vnode lock is used to serialize operations through the file system for
93a given file when there are multiple concurrent requests on the same file.
94Many file system functions require that you hold the vnode lock on entry.
95The vnode lock may be held when sleeping.
96.Pp
97A vnode will not be reclaimed as long as the vnode lock is held by some
98other process.
99.Pp
100The vnode lock is a multiple-reader or single-writer lock.
101An exclusive vnode lock may be acquired multiple times by the same
102process.
103.Pp
104The vnode lock is somewhat messy because it is used for many purposes.
105Some clients of the vnode interface use it to try to bundle a series
106of VOP_ method calls into an atomic group.
107Many file systems rely on it to prevent race conditions in updating file
108system specific data structures (as opposed to having their own locks).
109.Pp
110The implementation of the vnode lock is the responsibility of the individual
111file systems. Not all file system implement it.
112.Pp
113To prevent deadlocks, when acquiring locks on multiple vnodes, the lock
114of parent directory must be acquired before the lock on the child directory.
115.Ss Vnode interlock
116The vnode interlock (vp->v_interlock) is a spinlock.
117It is useful on multi-processor systems for acquiring a quick exclusive
118lock on the contents of the vnode.
119It MUST NOT be held while sleeping.
120(What fields does it cover? What about splbio/interrupt issues?)
121.Pp
122Operations on this lock are a no-op on uniprocessor systems.
123.Ss Other Vnode synchronization
124The vnode reclamation lock (VXLOCK) is used to prevent multiple
125processes from entering the vnode reclamation code.
126It is also used as a flag to indicate that reclamation is in progress.
127The VXWANT flag is set by processes that wish to woken up when reclamation
128is finished.
129.Pp
130The
131.Xr vwaitforio 9
132call is used for to wait for all outstanding write I/Os associated with a
133vnode to complete.
134.Ss Version number/capability
135The vnode capability, v_id, is a 32-bit version number on the vnode.
136Every time a vnode is reassigned to a new file, the vnode capability
137is changed.
138This is used by code that wish to keep pointers to vnodes but doesn't want
139to hold a reference (e.g., caches).
140The code keeps both a vnode * and a copy of the capability.
141The code can later compare the vnode's capability to its copy and see
142if the vnode still points to the same file.
143.Pp
144Note: for this to work, memory assigned to hold a struct vnode can
145only be used for another purpose when all pointers to it have disappeared.
146Since the vnode pool has no way of knowing when all pointers have
147disappeared, it never frees memory it has allocated for vnodes.
148.Ss Vnode fields
149Most of the fields of the vnode structure should be treated as opaque
150and only manipulated through the proper APIs.
151This section describes the fields that are manipulated directly.
152.Pp
153The v_flag attribute contains random flags related to various functions.
154They are summarized in table ...
155.Pp
156The v_tag attribute indicates what file system the vnode belongs to.
157Very little code actually uses this attribute and its use is deprecated.
158Programmers should seriously consider using more object-oriented approaches
159(e.g. function tables).
160There is no safe way of defining new v_tags for loadable file systems.
161The v_tag attribute is read-only.
162.Pp
163The v_type attribute indicates what type of file (e.g. directory,
164regular, fifo) this vnode is.
165This is used by the generic code to ensure for various checks.
166For example, the
167.Xr read 2
168system call returns an error when a read is attempted on a directory.
169.Pp
170The v_data attribute allows a file system to attach piece of file
171system specific memory to the vnode.
172This contains information about the file that is specific to
173the file system.
174.Pp
175The v_numoutput attribute indicates the number of pending synchronous
176and asynchronous writes on the vnode.
177It does not track the number of dirty buffers attached to the vnode.
178The attribute is used by code like fsync to wait for all writes
179to complete before returning to the user.
180This attribute must be manipulated at splbio().
181.Pp
182The v_writecount attribute tracks the number of write calls pending
183on the vnode.
184.Ss RULES
185The vast majority of vnode functions may not be called from interrupt
186context.
187The exceptions are bgetvp and brelvp.
188The following fields of the vnode are manipulated at interrupt level:
189v_numoutput, v_holdcnt, v_dirtyblkhd, v_cleanblkhd, v_bioflag, v_freelist,
190and v_synclist.
191Any accesses to these field should be protected by splbio,
192unless you are certain that there is no chance an interrupt handler
193will modify them.
194.Sh HISTORY
195This document first appeared in
196.Ox 2.9 .
197