xref: /original-bsd/sys/sys/vnode.h (revision 499a7081)
1 /*
2  * Copyright (c) 1989 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  *
7  *	@(#)vnode.h	7.39 (Berkeley) 06/27/91
8  */
9 
10 #ifndef KERNEL
11 #include <machine/endian.h>
12 #endif
13 
14 /*
15  * The vnode is the focus of all file activity in UNIX.
16  * There is a unique vnode allocated for each active file,
17  * each current directory, each mounted-on file, text file, and the root.
18  */
19 
20 /*
21  * vnode types. VNON means no type.
22  */
23 enum vtype 	{ VNON, VREG, VDIR, VBLK, VCHR, VLNK, VSOCK, VFIFO, VBAD };
24 
25 /*
26  * Vnode tag types.
27  * These are for the benefit of external programs only (e.g., pstat)
28  * and should NEVER be inspected inside the kernel.
29  */
30 enum vtagtype	{ VT_NON, VT_UFS, VT_NFS, VT_MFS };
31 
32 /*
33  * This defines the maximum size of the private data area
34  * permitted for any file system type. A defined constant
35  * is used rather than a union structure to cut down on the
36  * number of header files that must be included.
37  */
38 #define	VN_MAXPRIVATE	188
39 
40 struct vnode {
41 	u_long		v_flag;			/* vnode flags (see below) */
42 	short		v_usecount;		/* reference count of users */
43 	short		v_writecount;		/* reference count of writers */
44 	long		v_holdcnt;		/* page & buffer references */
45 	off_t		v_lastr;		/* last read (read-ahead) */
46 	u_long		v_id;			/* capability identifier */
47 	struct mount	*v_mount;		/* ptr to vfs we are in */
48 	struct vnodeops	*v_op;			/* vnode operations */
49 	struct vnode	*v_freef;		/* vnode freelist forward */
50 	struct vnode	**v_freeb;		/* vnode freelist back */
51 	struct vnode	*v_mountf;		/* vnode mountlist forward */
52 	struct vnode	**v_mountb;		/* vnode mountlist back */
53 	struct buf	*v_cleanblkhd;		/* clean blocklist head */
54 	struct buf	*v_dirtyblkhd;		/* dirty blocklist head */
55 	long		v_numoutput;		/* num of writes in progress */
56 	enum vtype	v_type;			/* vnode type */
57 	union {
58 		struct mount	*vu_mountedhere;/* ptr to mounted vfs (VDIR) */
59 		struct socket	*vu_socket;	/* unix ipc (VSOCK) */
60 		caddr_t		vu_vmdata;	/* private data for vm (VREG) */
61 		struct specinfo	*vu_specinfo;	/* device (VCHR, VBLK) */
62 		struct fifoinfo	*vu_fifoinfo;	/* fifo (VFIFO) */
63 	} v_un;
64 	enum vtagtype	v_tag;			/* type of underlying data */
65 	char v_data[VN_MAXPRIVATE];		/* private data for fs */
66 };
67 #define	v_mountedhere	v_un.vu_mountedhere
68 #define	v_socket	v_un.vu_socket
69 #define	v_vmdata	v_un.vu_vmdata
70 #define	v_specinfo	v_un.vu_specinfo
71 #define	v_fifoinfo	v_un.vu_fifoinfo
72 
73 /*
74  * vnode flags.
75  */
76 #define	VROOT		0x0001	/* root of its file system */
77 #define	VTEXT		0x0002	/* vnode is a pure text prototype */
78 #define	VSYSTEM		0x0004	/* vnode being used by kernel */
79 #define	VXLOCK		0x0100	/* vnode is locked to change underlying type */
80 #define	VXWANT		0x0200	/* process is waiting for vnode */
81 #define	VBWAIT		0x0400	/* waiting for output to complete */
82 #define	VALIASED	0x0800	/* vnode has an alias */
83 
84 /*
85  * Vnode attributes.  A field value of VNOVAL
86  * represents a field whose value is unavailable
87  * (getattr) or which is not to be changed (setattr).
88  */
89 struct vattr {
90 	enum vtype	va_type;	/* vnode type (for create) */
91 	u_short		va_mode;	/* files access mode and type */
92 	short		va_nlink;	/* number of references to file */
93 	uid_t		va_uid;		/* owner user id */
94 	gid_t		va_gid;		/* owner group id */
95 	long		va_fsid;	/* file system id (dev for now) */
96 	long		va_fileid;	/* file id */
97 	u_quad		va_qsize;	/* file size in bytes */
98 	long		va_blocksize;	/* blocksize preferred for i/o */
99 	struct timeval	va_atime;	/* time of last access */
100 	struct timeval	va_mtime;	/* time of last modification */
101 	struct timeval	va_ctime;	/* time file changed */
102 	u_long		va_gen;		/* generation number of file */
103 	u_long		va_flags;	/* flags defined for file */
104 	dev_t		va_rdev;	/* device the special file represents */
105 	u_quad		va_qbytes;	/* bytes of disk space held by file */
106 };
107 #if BYTE_ORDER == LITTLE_ENDIAN
108 #define	va_size		va_qsize.val[0]
109 #define	va_size_rsv	va_qsize.val[1]
110 #define	va_bytes	va_qbytes.val[0]
111 #define	va_bytes_rsv	va_qbytes.val[1]
112 #else
113 #define	va_size		va_qsize.val[1]
114 #define	va_size_rsv	va_qsize.val[0]
115 #define	va_bytes	va_qbytes.val[1]
116 #define	va_bytes_rsv	va_qbytes.val[0]
117 #endif
118 
119 /*
120  * Operations on vnodes.
121  */
122 #ifdef __STDC__
123 struct flock;
124 struct nameidata;
125 #endif
126 
127 struct vnodeops {
128 	int	(*vop_lookup)	__P((struct vnode *vp, struct nameidata *ndp,
129 				    struct proc *p));
130 	int	(*vop_create)	__P((struct nameidata *ndp, struct vattr *vap,
131 				    struct proc *p));
132 	int	(*vop_mknod)	__P((struct nameidata *ndp, struct vattr *vap,
133 				    struct ucred *cred, struct proc *p));
134 	int	(*vop_open)	__P((struct vnode *vp, int mode,
135 				    struct ucred *cred, struct proc *p));
136 	int	(*vop_close)	__P((struct vnode *vp, int fflag,
137 				    struct ucred *cred, struct proc *p));
138 	int	(*vop_access)	__P((struct vnode *vp, int mode,
139 				    struct ucred *cred, struct proc *p));
140 	int	(*vop_getattr)	__P((struct vnode *vp, struct vattr *vap,
141 				    struct ucred *cred, struct proc *p));
142 	int	(*vop_setattr)	__P((struct vnode *vp, struct vattr *vap,
143 				    struct ucred *cred, struct proc *p));
144 	int	(*vop_read)	__P((struct vnode *vp, struct uio *uio,
145 				    int ioflag, struct ucred *cred));
146 	int	(*vop_write)	__P((struct vnode *vp, struct uio *uio,
147 				    int ioflag, struct ucred *cred));
148 	int	(*vop_ioctl)	__P((struct vnode *vp, int command,
149 				    caddr_t data, int fflag,
150 				    struct ucred *cred, struct proc *p));
151 	int	(*vop_select)	__P((struct vnode *vp, int which, int fflags,
152 				    struct ucred *cred, struct proc *p));
153 	int	(*vop_mmap)	__P((struct vnode *vp, int fflags,
154 				    struct ucred *cred, struct proc *p));
155 	int	(*vop_fsync)	__P((struct vnode *vp, int fflags,
156 				    struct ucred *cred, int waitfor,
157 				    struct proc *p));
158 	int	(*vop_seek)	__P((struct vnode *vp, off_t oldoff,
159 				    off_t newoff, struct ucred *cred));
160 	int	(*vop_remove)	__P((struct nameidata *ndp, struct proc *p));
161 	int	(*vop_link)	__P((struct vnode *vp, struct nameidata *ndp,
162 				    struct proc *p));
163 	int	(*vop_rename)	__P((struct nameidata *fndp,
164 				    struct nameidata *tdnp, struct proc *p));
165 	int	(*vop_mkdir)	__P((struct nameidata *ndp, struct vattr *vap,
166 				    struct proc *p));
167 	int	(*vop_rmdir)	__P((struct nameidata *ndp, struct proc *p));
168 	int	(*vop_symlink)	__P((struct nameidata *ndp, struct vattr *vap,
169 				    char *target, struct proc *p));
170 	int	(*vop_readdir)	__P((struct vnode *vp, struct uio *uio,
171 				    struct ucred *cred, int *eofflagp));
172 	int	(*vop_readlink)	__P((struct vnode *vp, struct uio *uio,
173 				    struct ucred *cred));
174 	int	(*vop_abortop)	__P((struct nameidata *ndp));
175 	int	(*vop_inactive)	__P((struct vnode *vp, struct proc *p));
176 	int	(*vop_reclaim)	__P((struct vnode *vp));
177 	int	(*vop_lock)	__P((struct vnode *vp));
178 	int	(*vop_unlock)	__P((struct vnode *vp));
179 	int	(*vop_bmap)	__P((struct vnode *vp, daddr_t bn,
180 				    struct vnode **vpp, daddr_t *bnp));
181 	int	(*vop_strategy)	__P((struct buf *bp));
182 	int	(*vop_print)	__P((struct vnode *vp));
183 	int	(*vop_islocked)	__P((struct vnode *vp));
184 	int	(*vop_advlock)	__P((struct vnode *vp, caddr_t id, int op,
185 				    struct flock *fl, int flags));
186 };
187 
188 /* Macros to call the vnode ops */
189 #define	VOP_LOOKUP(v,n,p)	(*((v)->v_op->vop_lookup))(v,n,p)
190 #define	VOP_CREATE(n,a,p)	(*((n)->ni_dvp->v_op->vop_create))(n,a,p)
191 #define	VOP_MKNOD(n,a,c,p)	(*((n)->ni_dvp->v_op->vop_mknod))(n,a,c,p)
192 #define	VOP_OPEN(v,f,c,p)	(*((v)->v_op->vop_open))(v,f,c,p)
193 #define	VOP_CLOSE(v,f,c,p)	(*((v)->v_op->vop_close))(v,f,c,p)
194 #define	VOP_ACCESS(v,f,c,p)	(*((v)->v_op->vop_access))(v,f,c,p)
195 #define	VOP_GETATTR(v,a,c,p)	(*((v)->v_op->vop_getattr))(v,a,c,p)
196 #define	VOP_SETATTR(v,a,c,p)	(*((v)->v_op->vop_setattr))(v,a,c,p)
197 #define	VOP_READ(v,u,i,c)	(*((v)->v_op->vop_read))(v,u,i,c)
198 #define	VOP_WRITE(v,u,i,c)	(*((v)->v_op->vop_write))(v,u,i,c)
199 #define	VOP_IOCTL(v,o,d,f,c,p)	(*((v)->v_op->vop_ioctl))(v,o,d,f,c,p)
200 #define	VOP_SELECT(v,w,f,c,p)	(*((v)->v_op->vop_select))(v,w,f,c,p)
201 #define	VOP_MMAP(v,c,p)		(*((v)->v_op->vop_mmap))(v,c,p)
202 #define	VOP_FSYNC(v,f,c,w,p)	(*((v)->v_op->vop_fsync))(v,f,c,w,p)
203 #define	VOP_SEEK(v,p,o,w)	(*((v)->v_op->vop_seek))(v,p,o,w)
204 #define	VOP_REMOVE(n,p)		(*((n)->ni_dvp->v_op->vop_remove))(n,p)
205 #define	VOP_LINK(v,n,p)		(*((n)->ni_dvp->v_op->vop_link))(v,n,p)
206 #define	VOP_RENAME(s,t,p)	(*((s)->ni_dvp->v_op->vop_rename))(s,t,p)
207 #define	VOP_MKDIR(n,a,p)	(*((n)->ni_dvp->v_op->vop_mkdir))(n,a,p)
208 #define	VOP_RMDIR(n,p)		(*((n)->ni_dvp->v_op->vop_rmdir))(n,p)
209 #define	VOP_SYMLINK(n,a,m,p)	(*((n)->ni_dvp->v_op->vop_symlink))(n,a,m,p)
210 #define	VOP_READDIR(v,u,c,e)	(*((v)->v_op->vop_readdir))(v,u,c,e)
211 #define	VOP_READLINK(v,u,c)	(*((v)->v_op->vop_readlink))(v,u,c)
212 #define	VOP_ABORTOP(n)		(*((n)->ni_dvp->v_op->vop_abortop))(n)
213 #define	VOP_INACTIVE(v,p)	(*((v)->v_op->vop_inactive))(v,p)
214 #define	VOP_RECLAIM(v)		(*((v)->v_op->vop_reclaim))(v)
215 #define	VOP_LOCK(v)		(*((v)->v_op->vop_lock))(v)
216 #define	VOP_UNLOCK(v)		(*((v)->v_op->vop_unlock))(v)
217 #define	VOP_BMAP(v,s,p,n)	(*((v)->v_op->vop_bmap))(v,s,p,n)
218 #define	VOP_STRATEGY(b)		(*((b)->b_vp->v_op->vop_strategy))(b)
219 #define	VOP_PRINT(v)		(*((v)->v_op->vop_print))(v)
220 #define	VOP_ISLOCKED(v)		(((v)->v_flag & VXLOCK) || \
221 				(*((v)->v_op->vop_islocked))(v))
222 #define	VOP_ADVLOCK(v,p,o,l,f)	(*((v)->v_op->vop_advlock))(v,p,o,l,f)
223 
224 /*
225  * flags for ioflag
226  */
227 #define	IO_UNIT		0x01		/* do I/O as atomic unit */
228 #define	IO_APPEND	0x02		/* append write to end */
229 #define	IO_SYNC		0x04		/* do I/O synchronously */
230 #define	IO_NODELOCKED	0x08		/* underlying node already locked */
231 #define	IO_NDELAY	0x10		/* FNDELAY flag set in file table */
232 
233 /*
234  *  Modes. Some values same as Ixxx entries from inode.h for now
235  */
236 #define	VSUID	04000		/* set user id on execution */
237 #define	VSGID	02000		/* set group id on execution */
238 #define	VSVTX	01000		/* save swapped text even after use */
239 #define	VREAD	0400		/* read, write, execute permissions */
240 #define	VWRITE	0200
241 #define	VEXEC	0100
242 
243 /*
244  * Token indicating no attribute value yet assigned
245  */
246 #define	VNOVAL	((unsigned)0xffffffff)
247 
248 #ifdef KERNEL
249 /*
250  * public vnode manipulation functions
251  */
252 int 	vn_open __P((struct nameidata *ndp, struct proc *p, int fmode,
253 	    int cmode));
254 int 	vn_close __P((struct vnode *vp, int flags, struct ucred *cred,
255 	    struct proc *p));
256 int 	vn_rdwr __P((enum uio_rw rw, struct vnode *vp, caddr_t base,
257 	    int len, off_t offset, enum uio_seg segflg, int ioflg,
258 	    struct ucred *cred, int *aresid, struct proc *p));
259 int	vn_read __P((struct file *fp, struct uio *uio, struct ucred *cred));
260 int	vn_write __P((struct file *fp, struct uio *uio, struct ucred *cred));
261 int	vn_ioctl __P((struct file *fp, int com, caddr_t data, struct proc *p));
262 int	vn_select __P((struct file *fp, int which, struct proc *p));
263 int 	vn_closefile __P((struct file *fp, struct proc *p));
264 int 	getnewvnode __P((enum vtagtype tag, struct mount *mp,
265 	    struct vnodeops *vops, struct vnode **vpp));
266 int 	bdevvp __P((int dev, struct vnode **vpp));
267 	/* check for special device aliases */
268 	/* XXX nvp_rdev should be type dev_t, not int */
269 struct 	vnode *checkalias __P((struct vnode *vp, int nvp_rdev,
270 	    struct mount *mp));
271 void 	vattr_null __P((struct vattr *vap));
272 int 	vcount __P((struct vnode *vp));	/* total references to a device */
273 int 	vget __P((struct vnode *vp));	/* get first reference to a vnode */
274 void 	vref __P((struct vnode *vp));	/* increase reference to a vnode */
275 void 	vput __P((struct vnode *vp));	/* unlock and release vnode */
276 void 	vrele __P((struct vnode *vp));	/* release vnode */
277 void 	vgone __P((struct vnode *vp));	/* completely recycle vnode */
278 void 	vgoneall __P((struct vnode *vp));/* recycle vnode and all its aliases */
279 
280 /*
281  * Flags to various vnode functions.
282  */
283 #define	SKIPSYSTEM	0x0001		/* vflush: skip vnodes marked VSYSTEM */
284 #define	FORCECLOSE	0x0002		/* vflush: force file closeure */
285 #define	DOCLOSE		0x0004		/* vclean: close active files */
286 
287 #ifndef DIAGNOSTIC
288 #define	VREF(vp)	(vp)->v_usecount++	/* increase reference */
289 #define	VHOLD(vp)	(vp)->v_holdcnt++	/* increase buf or page ref */
290 #define	HOLDRELE(vp)	(vp)->v_holdcnt--	/* decrease buf or page ref */
291 #define	VATTR_NULL(vap)	(*(vap) = va_null)	/* initialize a vattr */
292 #else /* DIAGNOSTIC */
293 #define	VREF(vp)	vref(vp)
294 #define	VHOLD(vp)	vhold(vp)
295 #define	HOLDRELE(vp)	holdrele(vp)
296 #define	VATTR_NULL(vap)	vattr_null(vap)
297 #endif
298 
299 #define	NULLVP	((struct vnode *)NULL)
300 
301 /*
302  * Global vnode data.
303  */
304 extern	struct vnode *rootdir;		/* root (i.e. "/") vnode */
305 extern	int desiredvnodes;		/* number of vnodes desired */
306 extern	struct vattr va_null;		/* predefined null vattr structure */
307 #endif
308