xref: /dragonfly/share/man/man9/vnode.9 (revision 9c600e7d)
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: src/share/man/man9/vnode.9,v 1.10.2.5 2001/12/17 11:30:19 ru Exp $
30.\" $DragonFly: src/share/man/man9/vnode.9,v 1.2 2003/06/17 04:37:01 dillon Exp $
31.\"
32.Dd June 30, 1999
33.Os
34.Dt VNODE 9
35.Sh NAME
36.Nm vnode
37.Nd internal representation of a file or directory
38.Sh SYNOPSIS
39.In sys/param.h
40.In sys/vnode.h
41.Pp
42.Bd -literal
43/*
44 * Vnode types.  VNON means no type.
45 */
46enum vtype	{ VNON, VREG, VDIR, VBLK, VCHR, VLNK, VSOCK, VFIFO, VBAD };
47
48/*
49 * Vnode tag types.
50 * These are for the benefit of external programs only (e.g., pstat)
51 * and should NEVER be inspected by the kernel.
52 */
53enum vtagtype	{
54	VT_NON, VT_UFS, VT_NFS, VT_MFS, VT_PC, VT_LFS, VT_LOFS, VT_FDESC,
55	VT_PORTAL, VT_NULL, VT_UMAP, VT_KERNFS, VT_PROCFS, VT_AFS, VT_ISOFS,
56	VT_UNION, VT_MSDOSFS, VT_TFS, VT_VFS, VT_CODA, VT_NTFS
57};
58
59/*
60 * Each underlying filesystem allocates its own private area and hangs
61 * it from v_data.  If non-null, this area is freed in getnewvnode().
62 */
63TAILQ_HEAD(buflists, buf);
64
65typedef	int 	vop_t __P((void *));
66struct namecache;
67
68/*
69 * Reading or writing any of these items requires holding the appropriate lock.
70 * v_freelist is locked by the global vnode_free_list simple lock.
71 * v_mntvnodes is locked by the global mntvnodes simple lock.
72 * v_flag, v_usecount, v_holdcount and v_writecount are
73 *    locked by the v_interlock simple lock.
74 * v_pollinfo is locked by the lock contained inside it.
75 */
76struct vnode {
77	u_long	v_flag;				/* vnode flags (see below) */
78	int	v_usecount;			/* reference count of users */
79	int	v_writecount;			/* reference count of writers */
80	int	v_holdcnt;			/* page & buffer references */
81	daddr_t	v_lastr;			/* last read (read-ahead) */
82	u_long	v_id;				/* capability identifier */
83	struct	mount *v_mount;			/* ptr to vfs we are in */
84	vop_t	**v_op;				/* vnode operations vector */
85	TAILQ_ENTRY(vnode) v_freelist;		/* vnode freelist */
86	LIST_ENTRY(vnode) v_mntvnodes;		/* vnodes for mount point */
87	struct	buflists v_cleanblkhd;		/* clean blocklist head */
88	struct	buflists v_dirtyblkhd;		/* dirty blocklist head */
89	LIST_ENTRY(vnode) v_synclist;		/* vnodes with dirty buffers */
90	long	v_numoutput;			/* num of writes in progress */
91	enum	vtype v_type;			/* vnode type */
92	union {
93		struct mount	*vu_mountedhere;/* ptr to mounted vfs (VDIR) */
94		struct socket	*vu_socket;	/* unix ipc (VSOCK) */
95		struct specinfo	*vu_specinfo;	/* device (VCHR, VBLK) */
96		struct fifoinfo	*vu_fifoinfo;	/* fifo (VFIFO) */
97	} v_un;
98	struct	nqlease *v_lease;		/* Soft reference to lease */
99	daddr_t	v_lastw;			/* last write (write cluster) */
100	daddr_t	v_cstart;			/* start block of cluster */
101	daddr_t	v_lasta;			/* last allocation */
102	int	v_clen;				/* length of current cluster */
103	int	v_maxio;			/* maximum I/O cluster size */
104	struct vm_object *v_object;		/* Place to store VM object */
105	struct	simplelock v_interlock;		/* lock on usecount and flag */
106	struct	lock *v_vnlock;			/* used for non-locking fs's */
107	enum	vtagtype v_tag;			/* type of underlying data */
108	void 	*v_data;			/* private data for fs */
109	LIST_HEAD(, namecache) v_cache_src;	/* Cache entries from us */
110	TAILQ_HEAD(, namecache) v_cache_dst;	/* Cache entries to us */
111	struct	vnode *v_dd;			/* .. vnode */
112	u_long	v_ddid;				/* .. capability identifier */
113	struct	{
114		struct	simplelock vpi_lock;	/* lock to protect below */
115		struct	selinfo vpi_selinfo;	/* identity of poller(s) */
116		short	vpi_events;		/* what they are looking for */
117		short	vpi_revents;		/* what has happened */
118	} v_pollinfo;
119};
120#define	v_mountedhere	v_un.vu_mountedhere
121#define	v_socket	v_un.vu_socket
122#define	v_specinfo	v_un.vu_specinfo
123#define	v_fifoinfo	v_un.vu_fifoinfo
124
125/*
126 * Vnode flags.
127 */
128#define	VROOT		0x00001	/* root of its file system */
129#define	VTEXT		0x00002	/* vnode is a pure text prototype */
130#define	VSYSTEM		0x00004	/* vnode being used by kernel */
131#define	VISTTY		0x00008	/* vnode represents a tty */
132#define	VXLOCK		0x00100	/* vnode is locked to change underlying type */
133#define	VXWANT		0x00200	/* process is waiting for vnode */
134#define	VBWAIT		0x00400	/* waiting for output to complete */
135#define	VALIASED	0x00800	/* vnode has an alias */
136#define	VDIROP		0x01000	/* LFS: vnode is involved in a directory op */
137#define	VOBJBUF		0x02000	/* Allocate buffers in VM object */
138#define	VNINACT		0x04000	/* LFS: skip ufs_inactive() in lfs_vunref */
139#define	VAGE		0x08000	/* Insert vnode at head of free list */
140#define	VOLOCK		0x10000	/* vnode is locked waiting for an object */
141#define	VOWANT		0x20000	/* a process is waiting for VOLOCK */
142#define	VDOOMED		0x40000	/* This vnode is being recycled */
143#define	VFREE		0x80000	/* This vnode is on the freelist */
144#define	VTBFREE		0x100000 /* This vnode is on the to-be-freelist */
145#define	VONWORKLST	0x200000 /* On syncer work-list */
146#define	VMOUNT		0x400000 /* Mount in progress */
147
148.Ed
149.Sh DESCRIPTION
150The vnode is the focus of all file activity in UNIX.  There is a
151unique vnode allocated for each active file, each current directory,
152each mounted-on file, text file, and the root.
153.Pp
154Each vnode has three reference counts,
155.Dv v_usecount ,
156.Dv v_holdcnt
157and
158.Dv v_writecount .
159The first is the number of clients within the kernel which are
160using this vnode.  This count is maintained by
161.Xr vref 9 ,
162.Xr vrele 9
163and
164.Xr vput 9 .
165The second is the number of clients within the kernel who veto
166the recycling of this vnode.  This count is
167maintained by
168.Xr vhold 9
169and
170.Xr vdrop 9 .
171When both the
172.Dv v_usecount
173and the
174.Dv v_holdcnt
175of a vnode reaches zero then the vnode will be put on the freelist
176and may be reused for another file, possibly in another filesystem.
177The transition to and from the freelist is handled by
178.Xr getnewvnode 9 ,
179.Xr vfree 9
180and
181.Xr vbusy 9 .
182The third is a count of the number of clients which are writing into
183the file.  It is maintained by the
184.Xr open 2
185and
186.Xr close 2
187system calls.
188.Pp
189Any call which returns a vnode (e.g.\&
190.Xr VFS_GET 9 ,
191.Xr VOP_LOOKUP 9
192etc.)
193will increase the
194.Dv v_usecount
195of the vnode by one.  When the caller is finished with the vnode, it
196should release this reference by calling
197.Xr vrele 9
198(or
199.Xr vput 9
200if the vnode is locked).
201.Pp
202Other commonly used members of the vnode structure are
203.Dv v_id
204which is used to maintain consistency in the name cache,
205.Dv v_mount
206which points at the filesystem which owns the vnode,
207.Dv v_type
208which contains the type of object the vnode represents and
209.Dv v_data
210which is used by filesystems to store filesystem specific data with
211the vnode.
212The
213.Dv v_op
214field is used by the
215.Dv VOP_*
216macros to call functions in the filesystem which implement the vnode's
217functionality.
218.Sh VNODE TYPES
219.Bl -tag -width VSOCK
220.It Dv VNON
221No type.
222.It Dv VREG
223A regular file; may be with or without VM object backing.  If you want
224to make sure this get a backing object, call
225.Xr vfs_object_create 9 .
226.It Dv VDIR
227A directory.
228.It Dv VBLK
229A block device; may be with or without VM object backing.  If you want
230to make sure this get a backing object, call
231.Xr vfs_object_create 9 .
232.It Dv VCHR
233A character device.
234.It Dv VLNK
235A symbolic link.
236.It Dv VSOCK
237A socket.  Advisory locking won't work on this.
238.It Dv VFIFO
239A FIFO (named pipe).  Advisory locking won't work on this.
240.It Dv VBAD
241An old style bad sector map
242.El
243.Sh NOTES
244VFIFO uses the "struct fileops" from
245.Pa /sys/kern/sys_pipe.c .
246VSOCK uses the "struct fileops" from
247.Pa /sys/kern/sys_socket.c .
248Everything else uses the one from
249.Pa /sys/kern/vfs_vnops.c .
250.Pp
251The VFIFO/VSOCK code, which is why "struct fileops" is used at all, is
252an artifact of an incomplete integration of the VFS code into the
253kernel.
254.Sh SEE ALSO
255.Xr VFS 9
256.Sh AUTHORS
257This man page was written by
258.An Doug Rabson .
259