vnode.h revision 105902
1142215Sglebius/*
2142215Sglebius * Copyright (c) 1989, 1993
3142215Sglebius *	The Regents of the University of California.  All rights reserved.
4142215Sglebius *
5142215Sglebius * Redistribution and use in source and binary forms, with or without
6142215Sglebius * modification, are permitted provided that the following conditions
7142215Sglebius * are met:
8142215Sglebius * 1. Redistributions of source code must retain the above copyright
9142215Sglebius *    notice, this list of conditions and the following disclaimer.
10142215Sglebius * 2. Redistributions in binary form must reproduce the above copyright
11142215Sglebius *    notice, this list of conditions and the following disclaimer in the
12142215Sglebius *    documentation and/or other materials provided with the distribution.
13142215Sglebius * 3. All advertising materials mentioning features or use of this software
14142215Sglebius *    must display the following acknowledgement:
15142215Sglebius *	This product includes software developed by the University of
16142215Sglebius *	California, Berkeley and its contributors.
17142215Sglebius * 4. Neither the name of the University nor the names of its contributors
18142215Sglebius *    may be used to endorse or promote products derived from this software
19142215Sglebius *    without specific prior written permission.
20142215Sglebius *
21142215Sglebius * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22142215Sglebius * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23142215Sglebius * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24142215Sglebius * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25142215Sglebius * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26142215Sglebius * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27172467Ssilby * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28172467Ssilby * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29172467Ssilby * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30142215Sglebius * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31142215Sglebius * SUCH DAMAGE.
32142215Sglebius *
33142215Sglebius *	@(#)vnode.h	8.7 (Berkeley) 2/4/94
34142215Sglebius * $FreeBSD: head/sys/sys/vnode.h 105902 2002-10-25 00:20:37Z mckusick $
35142215Sglebius */
36142215Sglebius
37142215Sglebius#ifndef _SYS_VNODE_H_
38142215Sglebius#define	_SYS_VNODE_H_
39142215Sglebius
40142215Sglebius/*
41142215Sglebius * XXX - compatability until lockmgr() goes away or all the #includes are
42142215Sglebius * updated.
43142215Sglebius */
44142215Sglebius#include <sys/lockmgr.h>
45164033Srwatson
46142215Sglebius#include <sys/queue.h>
47142215Sglebius#include <sys/_label.h>
48142215Sglebius#include <sys/_lock.h>
49142215Sglebius#include <sys/lock.h>
50142215Sglebius#include <sys/_mutex.h>
51142215Sglebius#include <sys/mutex.h>
52142215Sglebius#include <sys/selinfo.h>
53142215Sglebius#include <sys/uio.h>
54142215Sglebius#include <sys/acl.h>
55142215Sglebius#include <sys/ktr.h>
56142215Sglebius
57142215Sglebius/*
58142215Sglebius * The vnode is the focus of all file activity in UNIX.  There is a
59142215Sglebius * unique vnode allocated for each active file, each current directory,
60142215Sglebius * each mounted-on file, text file, and the root.
61142215Sglebius */
62142215Sglebius
63142215Sglebius/*
64152410Sru * Vnode types.  VNON means no type.
65142215Sglebius */
66142215Sglebiusenum vtype	{ VNON, VREG, VDIR, VBLK, VCHR, VLNK, VSOCK, VFIFO, VBAD };
67142215Sglebius
68142215Sglebius/*
69142215Sglebius * Each underlying filesystem allocates its own private area and hangs
70142215Sglebius * it from v_data.  If non-null, this area is freed in getnewvnode().
71142215Sglebius */
72142215SglebiusTAILQ_HEAD(buflists, buf);
73142215Sglebius
74142215Sglebiustypedef	int	vop_t(void *);
75142215Sglebiusstruct namecache;
76142215Sglebius
77142215Sglebiusstruct vpollinfo {
78142215Sglebius	struct	mtx vpi_lock;		/* lock to protect below */
79142215Sglebius	struct	selinfo vpi_selinfo;	/* identity of poller(s) */
80142215Sglebius	short	vpi_events;		/* what they are looking for */
81142215Sglebius	short	vpi_revents;		/* what has happened */
82148387Sume};
83142215Sglebius
84142215Sglebius/*
85142215Sglebius * Reading or writing any of these items requires holding the appropriate lock.
86142215Sglebius *
87142215Sglebius * Lock reference:
88142215Sglebius *	c - namecache mutex
89142215Sglebius *	f - freelist mutex
90142215Sglebius *	i - interlock
91142215Sglebius *	m - mntvnodes mutex
92142215Sglebius *	p - pollinfo lock
93142215Sglebius *	s - spechash mutex
94147256Sbrooks *	S - syncer mutex
95142901Sglebius *	u - Only a reference to the vnode is needed to read.
96142215Sglebius *	v - vnode lock
97142215Sglebius *
98142215Sglebius * XXX Not all fields are locked yet and some fields that are marked are not
99142215Sglebius * locked consistently.  This is a work in progress.
100142215Sglebius */
101142215Sglebius
102142215Sglebiusstruct vnode {
103142215Sglebius	struct	mtx v_interlock;		/* lock for "i" things */
104142215Sglebius	u_long	v_iflag;			/* i vnode flags (see below) */
105142215Sglebius	int	v_usecount;			/* i ref count of users */
106142215Sglebius	long	v_numoutput;			/* i writes in progress */
107142215Sglebius	struct thread *v_vxproc;		/* i thread owning VXLOCK */
108142215Sglebius	int	v_holdcnt;			/* i page & buffer references */
109142215Sglebius	struct	buflists v_cleanblkhd;		/* i SORTED clean blocklist */
110142215Sglebius	struct buf	*v_cleanblkroot;	/* i clean buf splay tree  */
111142215Sglebius	struct	buflists v_dirtyblkhd;		/* i SORTED dirty blocklist */
112142215Sglebius	struct buf	*v_dirtyblkroot;	/* i dirty buf splay tree */
113142215Sglebius	u_long	v_vflag;			/* v vnode flags */
114142215Sglebius	int	v_writecount;			/* v ref count of writers */
115142215Sglebius	struct vm_object *v_object;		/* v Place to store VM object */
116142215Sglebius	daddr_t	v_lastw;			/* v last write (write cluster) */
117142215Sglebius	daddr_t	v_cstart;			/* v start block of cluster */
118142215Sglebius	daddr_t	v_lasta;			/* v last allocation (cluster) */
119142215Sglebius	int	v_clen;				/* v length of current cluster */
120142215Sglebius	union {
121142215Sglebius		struct mount	*vu_mountedhere;/* v ptr to mounted vfs (VDIR) */
122142215Sglebius		struct socket	*vu_socket;	/* v unix ipc (VSOCK) */
123142215Sglebius		struct {
124142215Sglebius			struct cdev	*vu_cdev; /* v device (VCHR, VBLK) */
125142215Sglebius			SLIST_ENTRY(vnode) vu_specnext;	/* s device aliases */
126142215Sglebius		} vu_spec;
127142215Sglebius		struct fifoinfo	*vu_fifoinfo;	/* v fifo (VFIFO) */
128142215Sglebius	} v_un;
129142215Sglebius	TAILQ_ENTRY(vnode) v_freelist;		/* f vnode freelist */
130142215Sglebius	TAILQ_ENTRY(vnode) v_nmntvnodes;	/* m vnodes for mount point */
131142215Sglebius	LIST_ENTRY(vnode) v_synclist;		/* S dirty vnode list */
132142215Sglebius	enum	vtype v_type;			/* u vnode type */
133142215Sglebius	const char *v_tag;			/* u type of underlying data */
134147256Sbrooks	void	*v_data;			/* u private data for fs */
135142215Sglebius	struct	lock v_lock;			/* u used if fs don't have one */
136142215Sglebius	struct	lock *v_vnlock;			/* u pointer to vnode lock */
137142215Sglebius	vop_t	**v_op;				/* u vnode operations vector */
138142215Sglebius	struct	mount *v_mount;			/* u ptr to vfs we are in */
139142215Sglebius	LIST_HEAD(, namecache) v_cache_src;	/* c Cache entries from us */
140142215Sglebius	TAILQ_HEAD(, namecache) v_cache_dst;	/* c Cache entries to us */
141142215Sglebius	u_long	v_id;				/* c capability identifier */
142142215Sglebius	struct	vnode *v_dd;			/* c .. vnode */
143142215Sglebius	u_long	v_ddid;				/* c .. capability identifier */
144142215Sglebius	struct vpollinfo *v_pollinfo;		/* p Poll events */
145142215Sglebius	struct label v_label;			/* MAC label for vnode */
146146226Sglebius#ifdef	DEBUG_LOCKS
147146226Sglebius	const char *filename;			/* Source file doing locking */
148142215Sglebius	int line;				/* Line number doing locking */
149142215Sglebius#endif
150142215Sglebius	udev_t	v_cachedfs;			/* cached fs id */
151142215Sglebius	ino_t	v_cachedid;			/* cached file id */
152142215Sglebius};
153142215Sglebius#define	v_mountedhere	v_un.vu_mountedhere
154142215Sglebius#define	v_socket	v_un.vu_socket
155142215Sglebius#define	v_rdev		v_un.vu_spec.vu_cdev
156142215Sglebius#define	v_specnext	v_un.vu_spec.vu_specnext
157142215Sglebius#define	v_fifoinfo	v_un.vu_fifoinfo
158142215Sglebius
159142215Sglebius/*
160142215Sglebius * Userland version of struct vnode, for sysctl.
161142914Sglebius */
162142914Sglebiusstruct xvnode {
163142914Sglebius	size_t	xv_size;			/* sizeof(struct xvnode) */
164142914Sglebius	void	*xv_vnode;			/* address of real vnode */
165142215Sglebius	u_long	xv_flag;			/* vnode vflags */
166142914Sglebius	int	xv_usecount;			/* reference count of users */
167142215Sglebius	int	xv_writecount;			/* reference count of writers */
168142914Sglebius	int	xv_holdcnt;			/* page & buffer references */
169142215Sglebius	u_long	xv_id;				/* capability identifier */
170142215Sglebius	void	*xv_mount;			/* address of parent mount */
171142215Sglebius	long	xv_numoutput;			/* num of writes in progress */
172142215Sglebius	enum	vtype xv_type;			/* vnode type */
173142914Sglebius	union {
174142914Sglebius		void	*xvu_socket;		/* socket, if VSOCK */
175142914Sglebius		void	*xvu_fifo;		/* fifo, if VFIFO */
176142914Sglebius		udev_t	xvu_rdev;		/* maj/min, if VBLK/VCHR */
177142451Sglebius		struct {
178142446Sglebius			udev_t	xvu_dev;	/* device, if VDIR/VREG/VLNK */
179142446Sglebius			ino_t	xvu_ino;	/* id, if VDIR/VREG/VLNK */
180142451Sglebius		} xv_uns;
181142215Sglebius	} xv_un;
182142451Sglebius};
183142446Sglebius#define xv_socket	xv_un.xvu_socket
184142446Sglebius#define xv_fifo		xv_un.xvu_fifo
185142451Sglebius#define xv_rdev		xv_un.xvu_rdev
186142446Sglebius#define xv_dev		xv_un.xv_uns.xvu_dev
187142559Sglebius#define xv_ino		xv_un.xv_uns.xvu_ino
188142559Sglebius
189142559Sglebius#define	VN_POLLEVENT(vp, events)				\
190142559Sglebius	do {							\
191142559Sglebius		if ((vp)->v_pollinfo != NULL && 		\
192142559Sglebius		    (vp)->v_pollinfo->vpi_events & (events))	\
193142559Sglebius			vn_pollevent((vp), (events));		\
194160195Ssam	} while (0)
195142559Sglebius
196166228Sglebius#define VN_KNOTE(vp, b)						\
197142559Sglebius	do {							\
198142559Sglebius		if ((vp)->v_pollinfo != NULL)			\
199142559Sglebius			KNOTE(&vp->v_pollinfo->vpi_selinfo.si_note, (b)); \
200142559Sglebius	} while (0)
201142914Sglebius
202142559Sglebius/*
203142559Sglebius * Vnode flags.
204142914Sglebius *	VI flags are protected by interlock and live in v_iflag
205142559Sglebius *	VV flags are protected by the vnode lock and live in v_vflag
206142559Sglebius */
207142559Sglebius#define	VI_XLOCK	0x0001	/* vnode is locked to change vtype */
208142559Sglebius#define	VI_XWANT	0x0002	/* thread is waiting for vnode */
209142559Sglebius#define	VI_BWAIT	0x0004	/* waiting for output to complete */
210142559Sglebius#define	VI_OLOCK	0x0008	/* vnode is locked waiting for an object */
211142559Sglebius#define	VI_OWANT	0x0010	/* a thread is waiting for VOLOCK */
212142215Sglebius#define	VI_MOUNT	0x0020	/* Mount in progress */
213142215Sglebius#define	VI_AGE		0x0040	/* Insert vnode at head of free list */
214156947Sglebius#define	VI_DOOMED	0x0080	/* This vnode is being recycled */
215142559Sglebius#define	VI_FREE		0x0100	/* This vnode is on the freelist */
216142559Sglebius#define	VI_OBJDIRTY	0x0400	/* object might be dirty */
217142914Sglebius/*
218144329Sglebius * XXX VI_ONWORKLST could be replaced with a check for NULL list elements
219142215Sglebius * in v_synclist.
220142559Sglebius */
221142559Sglebius#define	VI_ONWORKLST	0x0200	/* On syncer work-list */
222142559Sglebius
223166423Sglebius#define	VV_ROOT		0x0001	/* root of its filesystem */
224142215Sglebius#define	VV_ISTTY	0x0002	/* vnode represents a tty */
225142215Sglebius#define	VV_NOSYNC	0x0004	/* unlinked, stop syncing */
226142215Sglebius#define	VV_OBJBUF	0x0008	/* Allocate buffers in VM object */
227142911Sglebius#define	VV_CACHEDLABEL	0x0010	/* Vnode has valid cached MAC label */
228142215Sglebius#define	VV_TEXT		0x0020	/* vnode is a pure text prototype */
229142215Sglebius#define	VV_COPYONWRITE	0x0040	/* vnode is doing copy-on-write */
230156947Sglebius#define	VV_SYSTEM	0x0080	/* vnode being used by kernel */
231156947Sglebius#define	VV_PROCDEP	0x0100	/* vnode is process dependent */
232142215Sglebius
233142215Sglebius/*
234142215Sglebius * Vnode attributes.  A field value of VNOVAL represents a field whose value
235142215Sglebius * is unavailable (getattr) or which is not to be changed (setattr).
236142215Sglebius */
237142215Sglebiusstruct vattr {
238142559Sglebius	enum vtype	va_type;	/* vnode type (for create) */
239142215Sglebius	u_short		va_mode;	/* files access mode and type */
240142215Sglebius	short		va_nlink;	/* number of references to file */
241142215Sglebius	uid_t		va_uid;		/* owner user id */
242142215Sglebius	gid_t		va_gid;		/* owner group id */
243142215Sglebius	udev_t		va_fsid;	/* filesystem id */
244179490Smlaier	long		va_fileid;	/* file id */
245179490Smlaier	u_quad_t	va_size;	/* file size in bytes */
246179490Smlaier	long		va_blocksize;	/* blocksize preferred for i/o */
247179490Smlaier	struct timespec	va_atime;	/* time of last access */
248142215Sglebius	struct timespec	va_mtime;	/* time of last modification */
249179490Smlaier	struct timespec	va_ctime;	/* time file changed */
250142215Sglebius	struct timespec	va_birthtime;	/* time file created */
251142215Sglebius	u_long		va_gen;		/* generation number of file */
252142914Sglebius	u_long		va_flags;	/* flags defined for file */
253142914Sglebius	udev_t		va_rdev;	/* device the special file represents */
254142914Sglebius	u_quad_t	va_bytes;	/* bytes of disk space held by file */
255142914Sglebius	u_quad_t	va_filerev;	/* file modification number */
256142914Sglebius	u_int		va_vaflags;	/* operations flags, see below */
257142215Sglebius	long		va_spare;	/* remain quad aligned */
258142215Sglebius};
259142215Sglebius
260142215Sglebius/*
261142215Sglebius * Flags for va_vaflags.
262142215Sglebius */
263142215Sglebius#define	VA_UTIMES_NULL	0x01		/* utimes argument was NULL */
264142215Sglebius#define	VA_EXCLUSIVE	0x02		/* exclusive create request */
265142215Sglebius
266142215Sglebius/*
267142215Sglebius * Flags for ioflag. (high 16 bits used to ask for read-ahead and
268142215Sglebius * help with write clustering)
269142215Sglebius */
270179490Smlaier#define	IO_UNIT		0x0001		/* do I/O as atomic unit */
271179490Smlaier#define	IO_APPEND	0x0002		/* append write to end */
272179490Smlaier#define	IO_SYNC		0x0004		/* do I/O synchronously */
273179490Smlaier#define	IO_NODELOCKED	0x0008		/* underlying node already locked */
274179490Smlaier#define	IO_NDELAY	0x0010		/* FNDELAY flag set in file table */
275179490Smlaier#define	IO_VMIO		0x0020		/* data already in VMIO space */
276179490Smlaier#define	IO_INVAL	0x0040		/* invalidate after I/O */
277179490Smlaier#define	IO_ASYNC	0x0080		/* bawrite rather then bdwrite */
278179490Smlaier#define	IO_DIRECT	0x0100		/* attempt to bypass buffer cache */
279179490Smlaier#define	IO_NOWDRAIN	0x0200		/* do not block on wdrain */
280179490Smlaier#define	IO_EXT		0x0400		/* operate on external attributes */
281179490Smlaier#define	IO_NORMAL	0x0800		/* operate on regular data */
282179490Smlaier#define	IO_NOMACCHECK	0x1000		/* MAC checks unnecessary */
283179490Smlaier
284179490Smlaier/*
285179490Smlaier *  Modes.  Some values same as Ixxx entries from inode.h for now.
286179490Smlaier */
287142215Sglebius#define	VEXEC	000100		/* execute/search permission */
288142215Sglebius#define	VWRITE	000200		/* write permission */
289179490Smlaier#define	VREAD	000400		/* read permission */
290179490Smlaier#define	VSVTX	001000		/* save swapped text even after use */
291179490Smlaier#define	VSGID	002000		/* set group id on execution */
292179490Smlaier#define	VSUID	004000		/* set user id on execution */
293179490Smlaier#define	VADMIN	010000		/* permission to administer */
294179490Smlaier#define	VSTAT	020000		/* permission to retrieve attrs */
295142215Sglebius#define	VAPPEND	040000		/* permission to write/append */
296179490Smlaier#define	VALLPERM	(VEXEC | VWRITE | VREAD | VADMIN | VSTAT | VAPPEND)
297179490Smlaier
298179490Smlaier/*
299179490Smlaier * Token indicating no attribute value yet assigned.
300179490Smlaier */
301179490Smlaier#define	VNOVAL	(-1)
302179490Smlaier
303179490Smlaier/*
304142215Sglebius * LK_TIMELOCK timeout for vnode locks (used mainly by the pageout daemon)
305179490Smlaier */
306179490Smlaier#define VLKTIMEOUT	(hz / 20 + 1)
307179490Smlaier
308142215Sglebius#ifdef _KERNEL
309142215Sglebius
310142215Sglebius#ifdef MALLOC_DECLARE
311142215SglebiusMALLOC_DECLARE(M_VNODE);
312142215Sglebius#endif
313142914Sglebius
314142914Sglebius/*
315142914Sglebius * Convert between vnode types and inode formats (since POSIX.1
316142215Sglebius * defines mode word of stat structure in terms of inode formats).
317142215Sglebius */
318142559Sglebiusextern enum vtype	iftovt_tab[];
319142215Sglebiusextern int		vttoif_tab[];
320142215Sglebius#define	IFTOVT(mode)	(iftovt_tab[((mode) & S_IFMT) >> 12])
321142215Sglebius#define	VTTOIF(indx)	(vttoif_tab[(int)(indx)])
322142215Sglebius#define	MAKEIMODE(indx, mode)	(int)(VTTOIF(indx) | (mode))
323142215Sglebius
324142215Sglebius/*
325142215Sglebius * Flags to various vnode functions.
326142215Sglebius */
327142215Sglebius#define	SKIPSYSTEM	0x0001	/* vflush: skip vnodes marked VSYSTEM */
328142215Sglebius#define	FORCECLOSE	0x0002	/* vflush: force file closure */
329142215Sglebius#define	WRITECLOSE	0x0004	/* vflush: only close writable files */
330142215Sglebius#define	DOCLOSE		0x0008	/* vclean: close active files */
331142215Sglebius#define	V_SAVE		0x0001	/* vinvalbuf: sync file first */
332142215Sglebius#define	V_ALT		0x0002	/* vinvalbuf: invalidate only alternate bufs */
333142215Sglebius#define	V_NORMAL	0x0004	/* vinvalbuf: invalidate only regular bufs */
334142215Sglebius#define	REVOKEALL	0x0001	/* vop_revoke: revoke all aliases */
335142215Sglebius#define	V_WAIT		0x0001	/* vn_start_write: sleep for suspend */
336142215Sglebius#define	V_NOWAIT	0x0002	/* vn_start_write: don't sleep for suspend */
337142559Sglebius#define	V_XSLEEP	0x0004	/* vn_start_write: just return after sleep */
338142215Sglebius
339142215Sglebius#define	VREF(vp)	vref(vp)
340142215Sglebius
341142215Sglebius
342142215Sglebius#ifdef DIAGNOSTIC
343142914Sglebius#define	VATTR_NULL(vap)	vattr_null(vap)
344142914Sglebius#else
345142215Sglebius#define	VATTR_NULL(vap)	(*(vap) = va_null)	/* initialize a vattr */
346142215Sglebius#endif /* DIAGNOSTIC */
347142215Sglebius
348142215Sglebius#define	NULLVP	((struct vnode *)NULL)
349142215Sglebius
350142559Sglebius#define	VNODEOP_SET(f) \
351142215Sglebius	C_SYSINIT(f##init, SI_SUB_VFS, SI_ORDER_SECOND, vfs_add_vnodeops, &f); \
352142215Sglebius	C_SYSUNINIT(f##uninit, SI_SUB_VFS, SI_ORDER_SECOND, vfs_rm_vnodeops, &f);
353142215Sglebius
354142215Sglebius/*
355142215Sglebius * Global vnode data.
356142914Sglebius */
357142914Sglebiusextern	struct vnode *rootvnode;	/* root (i.e. "/") vnode */
358142914Sglebiusextern	int desiredvnodes;		/* number of vnodes desired */
359142215Sglebiusextern	struct uma_zone *namei_zone;
360147256Sbrooksextern	int prtactive;			/* nonzero to call vprint() */
361142914Sglebiusextern	struct vattr va_null;		/* predefined null vattr structure */
362142914Sglebiusextern	int vfs_ioopt;
363142215Sglebius
364142564Sglebius/*
365142215Sglebius * Macro/function to check for client cache inconsistency w.r.t. leasing.
366142215Sglebius */
367142215Sglebius#define	LEASE_READ	0x1		/* Check lease for readers */
368142215Sglebius#define	LEASE_WRITE	0x2		/* Check lease for modifiers */
369142215Sglebius
370142215Sglebius
371142215Sglebiusextern void	(*lease_updatetime)(int deltat);
372142215Sglebius
373142215Sglebius/* Requires interlock */
374142215Sglebius#define	VSHOULDFREE(vp)	\
375142215Sglebius	(!((vp)->v_iflag & (VI_FREE|VI_DOOMED)) && \
376142215Sglebius	 !(vp)->v_holdcnt && !(vp)->v_usecount && \
377142215Sglebius	 (!(vp)->v_object || \
378142215Sglebius	  !((vp)->v_object->ref_count || (vp)->v_object->resident_page_count)))
379142215Sglebius
380142215Sglebius/* Requires interlock */
381142215Sglebius#define VMIGHTFREE(vp) \
382142215Sglebius	(!((vp)->v_iflag & (VI_FREE|VI_DOOMED|VI_XLOCK)) &&	\
383142559Sglebius	 LIST_EMPTY(&(vp)->v_cache_src) && !(vp)->v_usecount)
384160195Ssam
385142215Sglebius/* Requires interlock */
386142215Sglebius#define	VSHOULDBUSY(vp)	\
387142215Sglebius	(((vp)->v_iflag & VI_FREE) && \
388142215Sglebius	 ((vp)->v_holdcnt || (vp)->v_usecount))
389142215Sglebius
390142215Sglebius#define	VI_LOCK(vp)	mtx_lock(&(vp)->v_interlock)
391147256Sbrooks#define	VI_TRYLOCK(vp)	mtx_trylock(&(vp)->v_interlock)
392147256Sbrooks#define	VI_UNLOCK(vp)	mtx_unlock(&(vp)->v_interlock)
393147256Sbrooks#define	VI_MTX(vp)	(&(vp)->v_interlock)
394147256Sbrooks
395147256Sbrooks#endif /* _KERNEL */
396142215Sglebius
397142215Sglebius
398142215Sglebius/*
399142215Sglebius * Mods for extensibility.
400142215Sglebius */
401142215Sglebius
402142215Sglebius/*
403142215Sglebius * Flags for vdesc_flags:
404142215Sglebius */
405142215Sglebius#define	VDESC_MAX_VPS		16
406142215Sglebius/* Low order 16 flag bits are reserved for willrele flags for vp arguments. */
407160164Smlaier#define	VDESC_VP0_WILLRELE	0x0001
408160164Smlaier#define	VDESC_VP1_WILLRELE	0x0002
409160164Smlaier#define	VDESC_VP2_WILLRELE	0x0004
410170613Sbms#define	VDESC_VP3_WILLRELE	0x0008
411160164Smlaier#define	VDESC_NOMAP_VPP		0x0100
412162627Sbms#define	VDESC_VPP_WILLRELE	0x0200
413142215Sglebius
414171637Srwatson/*
415171637Srwatson * VDESC_NO_OFFSET is used to identify the end of the offset list
416171637Srwatson * and in places where no such field exists.
417142215Sglebius */
418142215Sglebius#define VDESC_NO_OFFSET -1
419142215Sglebius
420142215Sglebius/*
421151688Syar * This structure describes the vnode operation taking place.
422142215Sglebius */
423142215Sglebiusstruct vnodeop_desc {
424142215Sglebius	int	 vdesc_offset;		/* offset in vector,first for speed */
425142215Sglebius	char	*vdesc_name;		/* a readable name for debugging */
426142215Sglebius	int	 vdesc_flags;		/* VDESC_* flags */
427142215Sglebius
428142215Sglebius	/*
429147256Sbrooks	 * These ops are used by bypass routines to map and locate arguments.
430142911Sglebius	 * Creds and procs are not needed in bypass routines, but sometimes
431142215Sglebius	 * they are useful to (for example) transport layers.
432142911Sglebius	 * Nameidata is useful because it has a cred in it.
433142215Sglebius	 */
434142215Sglebius	int	*vdesc_vp_offsets;	/* list ended by VDESC_NO_OFFSET */
435142215Sglebius	int	vdesc_vpp_offset;	/* return vpp location */
436142559Sglebius	int	vdesc_cred_offset;	/* cred location, if any */
437142215Sglebius	int	vdesc_thread_offset;	/* thread location, if any */
438142215Sglebius	int	vdesc_componentname_offset; /* if any */
439142215Sglebius	/*
440156947Sglebius	 * Finally, we've got a list of private data (about each operation)
441156947Sglebius	 * for each transport layer.  (Support to manage this list is not
442156947Sglebius	 * yet part of BSD.)
443166228Sglebius	 */
444156947Sglebius	caddr_t	*vdesc_transports;
445156947Sglebius};
446156947Sglebius
447156947Sglebius#ifdef _KERNEL
448156947Sglebius/*
449156947Sglebius * A list of all the operation descs.
450156947Sglebius */
451160164Smlaierextern struct vnodeop_desc *vnodeop_descs[];
452156947Sglebius
453156947Sglebius/*
454156947Sglebius * Interlock for scanning list of vnodes attached to a mountpoint
455166423Sglebius */
456166423Sglebiusextern struct mtx mntvnode_mtx;
457166423Sglebius
458166423Sglebius/*
459166423Sglebius * This macro is very helpful in defining those offsets in the vdesc struct.
460166423Sglebius *
461166423Sglebius * This is stolen from X11R4.  I ignored all the fancy stuff for
462166423Sglebius * Crays, so if you decide to port this to such a serious machine,
463166423Sglebius * you might want to consult Intrinsic.h's XtOffset{,Of,To}.
464166423Sglebius */
465156947Sglebius#define	VOPARG_OFFSET(p_type,field) \
466166228Sglebius	((int) (((char *) (&(((p_type)NULL)->field))) - ((char *) NULL)))
467156947Sglebius#define	VOPARG_OFFSETOF(s_type,field) \
468142215Sglebius	VOPARG_OFFSET(s_type*,field)
469146226Sglebius#define	VOPARG_OFFSETTO(S_TYPE,S_OFFSET,STRUCT_P) \
470156947Sglebius	((S_TYPE)(((char*)(STRUCT_P))+(S_OFFSET)))
471156947Sglebius
472156947Sglebius
473156947Sglebius/*
474146226Sglebius * This structure is used to configure the new vnodeops vector.
475146226Sglebius */
476146226Sglebiusstruct vnodeopv_entry_desc {
477146226Sglebius	struct vnodeop_desc *opve_op;   /* which operation this is */
478156947Sglebius	vop_t *opve_impl;		/* code implementing this operation */
479156947Sglebius};
480156947Sglebiusstruct vnodeopv_desc {
481142215Sglebius			/* ptr to the ptr to the vector where op should go */
482156947Sglebius	vop_t ***opv_desc_vector_p;
483156947Sglebius	struct vnodeopv_entry_desc *opv_desc_ops;   /* null terminated list */
484156947Sglebius};
485166423Sglebius
486166423Sglebius/*
487166423Sglebius * A generic structure.
488166423Sglebius * This can be used by bypass routines to identify generic arguments.
489166423Sglebius */
490142215Sglebiusstruct vop_generic_args {
491156947Sglebius	struct vnodeop_desc *a_desc;
492156947Sglebius	/* other random data follows, presumably */
493156947Sglebius};
494142215Sglebius
495142215Sglebius/*
496156947Sglebius * Support code to aid in debugging VFS locking problems.  Not totally
497142564Sglebius * reliable since if the thread sleeps between changing the lock
498142215Sglebius * state and checking it with the assert, some other thread could
499156947Sglebius * change the state.  They are good enough for debugging a single
500166228Sglebius * filesystem using a single-threaded test.
501166228Sglebius */
502166228Sglebiusvoid assert_vi_locked(struct vnode *vp, char *str);
503142215Sglebiusvoid assert_vi_unlocked(struct vnode *vp, char *str);
504156947Sglebiusvoid assert_vop_unlocked(struct vnode *vp, char *str);
505142215Sglebiusvoid assert_vop_locked(struct vnode *vp, char *str);
506156947Sglebiusvoid assert_vop_slocked(struct vnode *vp, char *str);
507156947Sglebiusvoid assert_vop_elocked(struct vnode *vp, char *str);
508156947Sglebiusvoid assert_vop_elocked_other(struct vnode *vp, char *str);
509156947Sglebius
510156947Sglebius/* These are called from within the actuall VOPS */
511156947Sglebiusvoid vop_rename_pre(void *a);
512166226Sglebiusvoid vop_strategy_pre(void *a);
513156947Sglebiusvoid vop_lookup_pre(void *a);
514156947Sglebiusvoid vop_lookup_post(void *a, int rc);
515156947Sglebiusvoid vop_lock_pre(void *a);
516156947Sglebiusvoid vop_lock_post(void *a, int rc);
517156947Sglebiusvoid vop_unlock_pre(void *a);
518156947Sglebiusvoid vop_unlock_post(void *a, int rc);
519156947Sglebius
520156947Sglebius#ifdef DEBUG_VFS_LOCKS
521156947Sglebius
522166228Sglebius#define	ASSERT_VI_LOCKED(vp, str)	assert_vi_locked((vp), (str))
523156947Sglebius#define	ASSERT_VI_UNLOCKED(vp, str)	assert_vi_unlocked((vp), (str))
524142215Sglebius#define	ASSERT_VOP_LOCKED(vp, str)	assert_vop_locked((vp), (str))
525142215Sglebius#define	ASSERT_VOP_UNLOCKED(vp, str)	assert_vop_unlocked((vp), (str))
526142215Sglebius#define	ASSERT_VOP_ELOCKED(vp, str)	assert_vop_elocked((vp), (str))
527142215Sglebius#define	ASSERT_VOP_ELOCKED_OTHER(vp, str) assert_vop_locked_other((vp), (str))
528142215Sglebius#define	ASSERT_VOP_SLOCKED(vp, str)	assert_vop_slocked((vp), (str))
529142215Sglebius
530142215Sglebius#else
531142215Sglebius
532142215Sglebius#define	ASSERT_VOP_LOCKED(vp, str)
533142215Sglebius#define	ASSERT_VOP_UNLOCKED(vp, str)
534142215Sglebius#define	ASSERT_VOP_ELOCKED(vp, str)
535142215Sglebius#define	ASSERT_VOP_ELOCKED_OTHER(vp, str)
536142215Sglebius#define	ASSERT_VOP_SLOCKED(vp, str)
537142215Sglebius#define	ASSERT_VI_UNLOCKED(vp, str)
538142215Sglebius#define	ASSERT_VI_LOCKED(vp, str)
539142215Sglebius
540142215Sglebius#endif
541142215Sglebius
542142215Sglebius/*
543142215Sglebius * VOCALL calls an op given an ops vector.  We break it out because BSD's
544142215Sglebius * vclean changes the ops vector and then wants to call ops with the old
545142215Sglebius * vector.
546142215Sglebius */
547142215Sglebius#define VOCALL(OPSV,OFF,AP) (( *((OPSV)[(OFF)])) (AP))
548142452Sglebius
549142452Sglebius/*
550142446Sglebius * This call works for vnodes in the kernel.
551142215Sglebius */
552142215Sglebius#define VCALL(VP,OFF,AP) VOCALL((VP)->v_op,(OFF),(AP))
553142215Sglebius#define VDESC(OP) (& __CONCAT(OP,_desc))
554142215Sglebius#define VOFFSET(OP) (VDESC(OP)->vdesc_offset)
555142215Sglebius
556142215Sglebius/*
557142215Sglebius * VMIO support inline
558142452Sglebius */
559142446Sglebius
560142446Sglebiusextern int vmiodirenable;
561142215Sglebius
562142215Sglebiusstatic __inline int
563142215Sglebiusvn_canvmio(struct vnode *vp)
564142215Sglebius{
565142215Sglebius      if (vp && (vp->v_type == VREG || (vmiodirenable && vp->v_type == VDIR)))
566142215Sglebius		return(TRUE);
567142215Sglebius	return(FALSE);
568142215Sglebius}
569142446Sglebius
570142452Sglebius/*
571142446Sglebius * Finally, include the default set of vnode operations.
572142215Sglebius */
573142215Sglebius#include "vnode_if.h"
574142215Sglebius
575142215Sglebius/*
576142215Sglebius * Public vnode manipulation functions.
577142215Sglebius */
578142215Sglebiusstruct componentname;
579142452Sglebiusstruct file;
580142215Sglebiusstruct mount;
581142215Sglebiusstruct nameidata;
582142215Sglebiusstruct ostat;
583142215Sglebiusstruct thread;
584142215Sglebiusstruct proc;
585142215Sglebiusstruct stat;
586142215Sglebiusstruct nstat;
587142215Sglebiusstruct ucred;
588142215Sglebiusstruct uio;
589142215Sglebiusstruct vattr;
590142215Sglebiusstruct vnode;
591142215Sglebius
592142215Sglebiusextern int	(*lease_check_hook)(struct vop_lease_args *);
593142452Sglebiusextern int	(*softdep_fsync_hook)(struct vnode *);
594142446Sglebiusextern int	(*softdep_process_worklist_hook)(struct mount *);
595142446Sglebius
596142215Sglebiusstruct	vnode *addaliasu(struct vnode *vp, udev_t nvp_rdev);
597142215Sglebiusint	bdevvp(dev_t dev, struct vnode **vpp);
598142215Sglebius/* cache_* may belong in namei.h. */
599142215Sglebiusvoid	cache_enter(struct vnode *dvp, struct vnode *vp,
600142215Sglebius	    struct componentname *cnp);
601142215Sglebiusint	cache_lookup(struct vnode *dvp, struct vnode **vpp,
602142215Sglebius	    struct componentname *cnp);
603142215Sglebiusvoid	cache_purge(struct vnode *vp);
604142215Sglebiusvoid	cache_purgevfs(struct mount *mp);
605142215Sglebiusint	cache_leaf_test(struct vnode *vp);
606142215Sglebiusvoid	cvtstat(struct stat *st, struct ostat *ost);
607142215Sglebiusvoid	cvtnstat(struct stat *sb, struct nstat *nsb);
608142215Sglebiusint	getnewvnode(const char *tag, struct mount *mp, vop_t **vops,
609142215Sglebius	    struct vnode **vpp);
610142215Sglebiusint	lease_check(struct vop_lease_args *ap);
611142452Sglebiusint	spec_vnoperate(struct vop_generic_args *);
612142446Sglebiusint	speedup_syncer(void);
613142215Sglebius#define textvp_fullpath(p, rb, rfb) \
614142215Sglebius	vn_fullpath(FIRST_THREAD_IN_PROC(p), (p)->p_textvp, rb, rfb)
615142215Sglebiusint	vn_fullpath(struct thread *td, struct vnode *vn,
616142215Sglebius	    char **retbuf, char **freebuf);
617142215Sglebiusint	vaccess(enum vtype type, mode_t file_mode, uid_t uid, gid_t gid,
618142446Sglebius	    mode_t acc_mode, struct ucred *cred, int *privused);
619142215Sglebiusint	vaccess_acl_posix1e(enum vtype type, uid_t file_uid,
620142215Sglebius	    gid_t file_gid, struct acl *acl, mode_t acc_mode,
621142215Sglebius	    struct ucred *cred, int *privused);
622142215Sglebiusvoid	vattr_null(struct vattr *vap);
623142215Sglebiusint	vcount(struct vnode *vp);
624142215Sglebiusvoid	vdrop(struct vnode *);
625142215Sglebiusvoid	vdropl(struct vnode *);
626142215Sglebiusint	vfinddev(dev_t dev, enum vtype type, struct vnode **vpp);
627142215Sglebiusvoid	vfs_add_vnodeops(const void *);
628142215Sglebiusvoid	vfs_rm_vnodeops(const void *);
629142215Sglebiusint	vflush(struct mount *mp, int rootrefs, int flags);
630142215Sglebiusint	vget(struct vnode *vp, int lockflag, struct thread *td);
631142215Sglebiusvoid	vgone(struct vnode *vp);
632142215Sglebiusvoid	vgonel(struct vnode *vp, struct thread *td);
633142215Sglebiusvoid	vhold(struct vnode *);
634142215Sglebiusvoid	vholdl(struct vnode *);
635142215Sglebiusint	vinvalbuf(struct vnode *vp, int save, struct ucred *cred,
636142215Sglebius	    struct thread *td, int slpflag, int slptimeo);
637142215Sglebiusint	vtruncbuf(struct vnode *vp, struct ucred *cred, struct thread *td,
638142215Sglebius	    off_t length, int blksize);
639142215Sglebiusvoid	vprint(char *label, struct vnode *vp);
640142446Sglebiusint	vrecycle(struct vnode *vp, struct mtx *inter_lkp,
641142452Sglebius	    struct thread *td);
642142446Sglebiusint	vn_close(struct vnode *vp,
643142215Sglebius	    int flags, struct ucred *file_cred, struct thread *td);
644142215Sglebiusvoid	vn_finished_write(struct mount *mp);
645142215Sglebiusint	vn_isdisk(struct vnode *vp, int *errp);
646142215Sglebiusint	vn_lock(struct vnode *vp, int flags, struct thread *td);
647142215Sglebius#ifdef	DEBUG_LOCKS
648142215Sglebiusint	debug_vn_lock(struct vnode *vp, int flags, struct thread *p,
649142215Sglebius	    const char *filename, int line);
650142452Sglebius#define vn_lock(vp,flags,p) debug_vn_lock(vp,flags,p,__FILE__,__LINE__)
651142446Sglebius#endif
652142446Sglebiusint	vn_open(struct nameidata *ndp, int *flagp, int cmode);
653142215Sglebiusint	vn_open_cred(struct nameidata *ndp, int *flagp, int cmode,
654142215Sglebius	    struct ucred *cred);
655142215Sglebiusvoid	vn_pollevent(struct vnode *vp, int events);
656142215Sglebiusvoid	vn_pollgone(struct vnode *vp);
657142215Sglebiusint	vn_pollrecord(struct vnode *vp, struct thread *p, int events);
658142215Sglebiusint	vn_rdwr(enum uio_rw rw, struct vnode *vp, caddr_t base,
659142215Sglebius	    int len, off_t offset, enum uio_seg segflg, int ioflg,
660142215Sglebius	    struct ucred *active_cred, struct ucred *file_cred, int *aresid,
661142215Sglebius	    struct thread *td);
662143804Sglebiusint	vn_rdwr_inchunks(enum uio_rw rw, struct vnode *vp, caddr_t base,
663142215Sglebius	    int len, off_t offset, enum uio_seg segflg, int ioflg,
664142215Sglebius	    struct ucred *active_cred, struct ucred *file_cred, int *aresid,
665142215Sglebius	    struct thread *td);
666142215Sglebiusint	vn_stat(struct vnode *vp, struct stat *sb, struct ucred *active_cred,
667142215Sglebius	    struct ucred *file_cred, struct thread *td);
668142215Sglebiusint	vn_start_write(struct vnode *vp, struct mount **mpp, int flags);
669142215Sglebiusdev_t	vn_todev(struct vnode *vp);
670142215Sglebiusint	vn_write_suspend_wait(struct vnode *vp, struct mount *mp,
671142452Sglebius	    int flags);
672142446Sglebiusint	vn_writechk(struct vnode *vp);
673142215Sglebiusint	vn_extattr_get(struct vnode *vp, int ioflg, int attrnamespace,
674142215Sglebius	    const char *attrname, int *buflen, char *buf, struct thread *td);
675142215Sglebiusint	vn_extattr_set(struct vnode *vp, int ioflg, int attrnamespace,
676142215Sglebius	    const char *attrname, int buflen, char *buf, struct thread *td);
677142215Sglebiusint	vn_extattr_rm(struct vnode *vp, int ioflg, int attrnamespace,
678142446Sglebius	    const char *attrname, struct thread *td);
679142215Sglebiusint	vfs_cache_lookup(struct vop_lookup_args *ap);
680142215Sglebiusint	vfs_object_create(struct vnode *vp, struct thread *td,
681142215Sglebius	    struct ucred *cred);
682142215Sglebiusvoid	vfs_timestamp(struct timespec *);
683142559Sglebiusvoid	vfs_write_resume(struct mount *mp);
684142446Sglebiusint	vfs_write_suspend(struct mount *mp);
685142215Sglebiusint	vop_stdbmap(struct vop_bmap_args *);
686142215Sglebiusint	vop_stdgetwritemount(struct vop_getwritemount_args *);
687142446Sglebiusint	vop_stdgetpages(struct vop_getpages_args *);
688142215Sglebiusint	vop_stdinactive(struct vop_inactive_args *);
689142215Sglebiusint	vop_stdislocked(struct vop_islocked_args *);
690142215Sglebiusint	vop_stdlock(struct vop_lock_args *);
691142215Sglebiusint	vop_stdputpages(struct vop_putpages_args *);
692142215Sglebiusint	vop_stdunlock(struct vop_unlock_args *);
693142215Sglebiusint	vop_noislocked(struct vop_islocked_args *);
694142215Sglebiusint	vop_nolock(struct vop_lock_args *);
695142215Sglebiusint	vop_nopoll(struct vop_poll_args *);
696142914Sglebiusint	vop_nounlock(struct vop_unlock_args *);
697148887Srwatsonint	vop_stdpathconf(struct vop_pathconf_args *);
698148887Srwatsonint	vop_stdpoll(struct vop_poll_args *);
699142215Sglebiusint	vop_revoke(struct vop_revoke_args *);
700142914Sglebiusint	vop_sharedlock(struct vop_lock_args *);
701142215Sglebiusint	vop_eopnotsupp(struct vop_generic_args *ap);
702142215Sglebiusint	vop_ebadf(struct vop_generic_args *ap);
703142215Sglebiusint	vop_einval(struct vop_generic_args *ap);
704142215Sglebiusint	vop_enotty(struct vop_generic_args *ap);
705147256Sbrooksint	vop_defaultop(struct vop_generic_args *ap);
706147256Sbrooksint	vop_null(struct vop_generic_args *ap);
707147256Sbrooksint	vop_panic(struct vop_generic_args *ap);
708142215Sglebiusint	vop_stdcreatevobject(struct vop_createvobject_args *ap);
709159180Scsjpint	vop_stddestroyvobject(struct vop_destroyvobject_args *ap);
710142215Sglebiusint	vop_stdgetvobject(struct vop_getvobject_args *ap);
711142784Sglebius
712142215Sglebiusvoid	vfree(struct vnode *);
713142215Sglebiusvoid	vput(struct vnode *vp);
714142784Sglebiusvoid	vrele(struct vnode *vp);
715142784Sglebiusvoid	vref(struct vnode *vp);
716147256Sbrooksint	vrefcnt(struct vnode *vp);
717142215Sglebiusvoid	vbusy(struct vnode *vp);
718142215Sglebiusvoid 	v_addpollinfo(struct vnode *vp);
719142215Sglebius
720142215Sglebiusextern	vop_t **default_vnodeop_p;
721142215Sglebiusextern	vop_t **spec_vnodeop_p;
722147256Sbrooksextern	vop_t **dead_vnodeop_p;
723142914Sglebius
724142452Sglebius#endif /* _KERNEL */
725147256Sbrooks
726142446Sglebius#endif /* !_SYS_VNODE_H_ */
727142215Sglebius