1.\" -*- nroff -*- 2.\" 3.\" Copyright (c) 1996 Doug Rabson 4.\" 5.\" All rights reserved. 6.\" 7.\" This program is free software. 8.\" 9.\" Redistribution and use in source and binary forms, with or without 10.\" modification, are permitted provided that the following conditions 11.\" are met: 12.\" 1. Redistributions of source code must retain the above copyright 13.\" notice, this list of conditions and the following disclaimer. 14.\" 2. Redistributions in binary form must reproduce the above copyright 15.\" notice, this list of conditions and the following disclaimer in the 16.\" documentation and/or other materials provided with the distribution. 17.\" 18.\" THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY EXPRESS OR 19.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 20.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 21.\" IN NO EVENT SHALL THE DEVELOPERS BE LIABLE FOR ANY DIRECT, INDIRECT, 22.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 23.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 27.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28.\" 29.\" $FreeBSD$ 30.\" 31.Dd June 30, 1999 32.Os 33.Dt VNODE 9 34.Sh NAME 35.Nm vnode 36.Nd internal representation of a file or directory 37.Sh SYNOPSIS 38.In sys/param.h 39.In sys/vnode.h 40.Pp 41.Bd -literal 42/* 43 * Vnode types. VNON means no type. 44 */ 45enum vtype { VNON, VREG, VDIR, VBLK, VCHR, VLNK, VSOCK, VFIFO, VBAD }; 46 47/* 48 * Each underlying file system allocates its own private area and hangs 49 * it from v_data. If non-null, this area is freed in getnewvnode(). 50 */ 51TAILQ_HEAD(buflists, buf); 52 53typedef int vop_t(void *); 54struct namecache; 55 56/* 57 * Reading or writing any of these items requires holding the appropriate lock. 58 * v_freelist is locked by the global vnode_free_list simple lock. 59 * v_mntvnodes is locked by the global mntvnodes simple lock. 60 * v_flag, v_usecount, v_holdcount and v_writecount are 61 * locked by the v_interlock simple lock. 62 * v_pollinfo is locked by the lock contained inside it. 63 */ 64struct vnode { 65 u_long v_flag; /* vnode flags (see below) */ 66 int v_usecount; /* reference count of users */ 67 int v_writecount; /* reference count of writers */ 68 int v_holdcnt; /* page & buffer references */ 69 u_long v_id; /* capability identifier */ 70 struct mount *v_mount; /* ptr to vfs we are in */ 71 vop_t **v_op; /* vnode operations vector */ 72 TAILQ_ENTRY(vnode) v_freelist; /* vnode freelist */ 73 LIST_ENTRY(vnode) v_mntvnodes; /* vnodes for mount point */ 74 struct buflists v_cleanblkhd; /* clean blocklist head */ 75 struct buflists v_dirtyblkhd; /* dirty blocklist head */ 76 LIST_ENTRY(vnode) v_synclist; /* vnodes with dirty buffers */ 77 long v_numoutput; /* num of writes in progress */ 78 enum vtype v_type; /* vnode type */ 79 union { 80 struct mount *vu_mountedhere;/* ptr to mounted vfs (VDIR) */ 81 struct socket *vu_socket; /* unix ipc (VSOCK) */ 82 struct { 83 struct specinfo *vu_specinfo; /* device (VCHR, VBLK) */ 84 SLIST_ENTRY(vnode) vu_specnext; 85 } vu_spec; 86 struct fifoinfo *vu_fifoinfo; /* fifo (VFIFO) */ 87 } v_un; 88 struct nqlease *v_lease; /* Soft reference to lease */ 89 daddr_t v_lastw; /* last write (write cluster) */ 90 daddr_t v_cstart; /* start block of cluster */ 91 daddr_t v_lasta; /* last allocation */ 92 int v_clen; /* length of current cluster */ 93 struct vm_object *v_object; /* Place to store VM object */ 94 struct simplelock v_interlock; /* lock on usecount and flag */ 95 struct lock *v_vnlock; /* used for non-locking fs's */ 96 const char *v_tag; /* type of underlying data */ 97 void *v_data; /* private data for fs */ 98 LIST_HEAD(, namecache) v_cache_src; /* Cache entries from us */ 99 TAILQ_HEAD(, namecache) v_cache_dst; /* Cache entries to us */ 100 struct vnode *v_dd; /* .. vnode */ 101 u_long v_ddid; /* .. capability identifier */ 102 struct { 103 struct simplelock vpi_lock; /* lock to protect below */ 104 struct selinfo vpi_selinfo; /* identity of poller(s) */ 105 short vpi_events; /* what they are looking for */ 106 short vpi_revents; /* what has happened */ 107 } v_pollinfo; 108}; 109#define v_mountedhere v_un.vu_mountedhere 110#define v_socket v_un.vu_socket 111#define v_rdev v_un.vu_spec.vu_specinfo 112#define v_specnext v_un.vu_spec.vu_specnext 113#define v_fifoinfo v_un.vu_fifoinfo 114 115/* 116 * Vnode flags. 117 */ 118#define VROOT 0x00001 /* root of its file system */ 119#define VTEXT 0x00002 /* vnode is a pure text prototype */ 120#define VSYSTEM 0x00004 /* vnode being used by kernel */ 121#define VISTTY 0x00008 /* vnode represents a tty */ 122#define VXLOCK 0x00100 /* vnode is locked to change underlying type */ 123#define VXWANT 0x00200 /* process is waiting for vnode */ 124#define VBWAIT 0x00400 /* waiting for output to complete */ 125#define VOBJBUF 0x02000 /* Allocate buffers in VM object */ 126#define VAGE 0x08000 /* Insert vnode at head of free list */ 127#define VOLOCK 0x10000 /* vnode is locked waiting for an object */ 128#define VOWANT 0x20000 /* a process is waiting for VOLOCK */ 129#define VDOOMED 0x40000 /* This vnode is being recycled */ 130#define VFREE 0x80000 /* This vnode is on the freelist */ 131#define VTBFREE 0x100000 /* This vnode is on the to-be-freelist */ 132#define VONWORKLST 0x200000 /* On syncer work-list */ 133#define VMOUNT 0x400000 /* Mount in progress */ 134 135.Ed 136.Sh DESCRIPTION 137The vnode is the focus of all file activity in 138.Ux . 139There is a 140unique vnode allocated for each active file, each current directory, 141each mounted-on file, text file, and the root. 142.Pp 143Each vnode has three reference counts, 144.Dv v_usecount , 145.Dv v_holdcnt 146and 147.Dv v_writecount . 148The first is the number of clients within the kernel which are 149using this vnode. This count is maintained by 150.Xr vref 9 , 151.Xr vrele 9 152and 153.Xr vput 9 . 154The second is the number of clients within the kernel who veto 155the recycling of this vnode. This count is 156maintained by 157.Xr vhold 9 158and 159.Xr vdrop 9 . 160When both the 161.Dv v_usecount 162and the 163.Dv v_holdcnt 164of a vnode reaches zero then the vnode will be put on the freelist 165and may be reused for another file, possibly in another file system. 166The transition to and from the freelist is handled by 167.Xr getnewvnode 9 , 168.Xr vfree 9 169and 170.Xr vbusy 9 . 171The third is a count of the number of clients which are writing into 172the file. It is maintained by the 173.Xr open 2 174and 175.Xr close 2 176system calls. 177.Pp 178Any call which returns a vnode (e.g.\& 179.Xr VFS_GET 9 , 180.Xr VOP_LOOKUP 9 181etc.) 182will increase the 183.Dv v_usecount 184of the vnode by one. When the caller is finished with the vnode, it 185should release this reference by calling 186.Xr vrele 9 187(or 188.Xr vput 9 189if the vnode is locked). 190.Pp 191Other commonly used members of the vnode structure are 192.Dv v_id 193which is used to maintain consistency in the name cache, 194.Dv v_mount 195which points at the file system which owns the vnode, 196.Dv v_type 197which contains the type of object the vnode represents and 198.Dv v_data 199which is used by file systems to store file system specific data with 200the vnode. 201The 202.Dv v_op 203field is used by the 204.Dv VOP_* 205macros to call functions in the file system which implement the vnode's 206functionality. 207.Sh VNODE TYPES 208.Bl -tag -width VSOCK 209.It Dv VNON 210No type. 211.It Dv VREG 212A regular file; may be with or without VM object backing. If you want 213to make sure this get a backing object, call 214.Xr vfs_object_create 9 . 215.It Dv VDIR 216A directory. 217.It Dv VBLK 218A block device; may be with or without VM object backing. If you want 219to make sure this get a backing object, call 220.Xr vfs_object_create 9 . 221.It Dv VCHR 222A character device. 223.It Dv VLNK 224A symbolic link. 225.It Dv VSOCK 226A socket. Advisory locking won't work on this. 227.It Dv VFIFO 228A FIFO (named pipe). Advisory locking won't work on this. 229.It Dv VBAD 230An old style bad sector map 231.El 232.Sh IMPLEMENTATION NOTES 233VFIFO uses the "struct fileops" from 234.Pa /sys/kern/sys_pipe.c . 235VSOCK uses the "struct fileops" from 236.Pa /sys/kern/sys_socket.c . 237Everything else uses the one from 238.Pa /sys/kern/vfs_vnops.c . 239.Pp 240The VFIFO/VSOCK code, which is why "struct fileops" is used at all, is 241an artifact of an incomplete integration of the VFS code into the 242kernel. 243.Pp 244Calls to 245.Xr malloc 9 246or 247.Xr free 9 248when holding a 249.Nm 250interlock, will cause a LOR (Lock Order Reversal) due to the 251interwining of VM Objects and Vnodes. 252.Sh SEE ALSO 253.Xr malloc 9 , 254.Xr VFS 9 255.Sh AUTHORS 256This man page was written by 257.An Doug Rabson . 258