xref: /original-bsd/sys/sys/vnode.h (revision ba762ddc)
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.34 (Berkeley) 04/19/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 	long		v_usecount;		/* reference count of users */
43 	long		v_holdcnt;		/* page & buffer references */
44 	off_t		v_lastr;		/* last read (read-ahead) */
45 	u_long		v_id;			/* capability identifier */
46 	struct mount	*v_mount;		/* ptr to vfs we are in */
47 	struct vnodeops	*v_op;			/* vnode operations */
48 	struct vnode	*v_freef;		/* vnode freelist forward */
49 	struct vnode	**v_freeb;		/* vnode freelist back */
50 	struct vnode	*v_mountf;		/* vnode mountlist forward */
51 	struct vnode	**v_mountb;		/* vnode mountlist back */
52 	struct buf	*v_cleanblkhd;		/* clean blocklist head */
53 	struct buf	*v_dirtyblkhd;		/* dirty blocklist head */
54 	long		v_numoutput;		/* num of writes in progress */
55 	enum vtype	v_type;			/* vnode type */
56 	union {
57 		struct mount	*vu_mountedhere;/* ptr to mounted vfs (VDIR) */
58 		struct socket	*vu_socket;	/* unix ipc (VSOCK) */
59 		caddr_t		vu_vmdata;	/* private data for vm (VREG) */
60 		struct specinfo	*vu_specinfo;	/* device (VCHR, VBLK) */
61 		struct fifoinfo	*vu_fifoinfo;	/* fifo (VFIFO) */
62 	} v_un;
63 	enum vtagtype	v_tag;			/* type of underlying data */
64 	char v_data[VN_MAXPRIVATE];		/* private data for fs */
65 };
66 #define v_mountedhere v_un.vu_mountedhere
67 #define v_socket v_un.vu_socket
68 #define v_vmdata v_un.vu_vmdata
69 #define v_specinfo v_un.vu_specinfo
70 #define v_fifoinfo v_un.vu_fifoinfo
71 
72 /*
73  * vnode flags.
74  */
75 #define	VROOT		0x0001	/* root of its file system */
76 #define	VTEXT		0x0002	/* vnode is a pure text prototype */
77 #define	VSYSTEM		0x0004	/* vnode being used by kernel */
78 #define	VXLOCK		0x0100	/* vnode is locked to change underlying type */
79 #define	VXWANT		0x0200	/* process is waiting for vnode */
80 #define	VBWAIT		0x0400	/* waiting for output to complete */
81 #define	VALIASED	0x0800	/* vnode has an alias */
82 
83 /*
84  * Vnode attributes.  A field value of VNOVAL
85  * represents a field whose value is unavailable
86  * (getattr) or which is not to be changed (setattr).
87  */
88 struct vattr {
89 	enum vtype	va_type;	/* vnode type (for create) */
90 	u_short		va_mode;	/* files access mode and type */
91 	short		va_nlink;	/* number of references to file */
92 	uid_t		va_uid;		/* owner user id */
93 	gid_t		va_gid;		/* owner group id */
94 	long		va_fsid;	/* file system id (dev for now) */
95 	long		va_fileid;	/* file id */
96 	u_quad		va_qsize;	/* file size in bytes */
97 	long		va_blocksize;	/* blocksize preferred for i/o */
98 	struct timeval	va_atime;	/* time of last access */
99 	struct timeval	va_mtime;	/* time of last modification */
100 	struct timeval	va_ctime;	/* time file changed */
101 	u_long		va_gen;		/* generation number of file */
102 	u_long		va_flags;	/* flags defined for file */
103 	dev_t		va_rdev;	/* device the special file represents */
104 	u_quad		va_qbytes;	/* bytes of disk space held by file */
105 };
106 #if BYTE_ORDER == LITTLE_ENDIAN
107 #define	va_size		va_qsize.val[0]
108 #define	va_size_rsv	va_qsize.val[1]
109 #define	va_bytes	va_qbytes.val[0]
110 #define	va_bytes_rsv	va_qbytes.val[1]
111 #else
112 #define	va_size		va_qsize.val[1]
113 #define	va_size_rsv	va_qsize.val[0]
114 #define	va_bytes	va_qbytes.val[1]
115 #define	va_bytes_rsv	va_qbytes.val[0]
116 #endif
117 
118 /*
119  * Operations on vnodes.
120  */
121 struct file;
122 struct flock;
123 struct nameidata;
124 struct vnodeops {
125 	int	(*vn_lookup)__P((
126 			struct vnode *vp,
127 			struct nameidata *ndp,
128 			struct proc *p));
129 	int	(*vn_create)__P((
130 			struct nameidata *ndp,
131 			struct vattr *vap,
132 			struct proc *p));
133 	int	(*vn_mknod)__P((
134 			struct nameidata *ndp,
135 			struct vattr *vap,
136 			struct ucred *cred,
137 			struct proc *p));
138 	int	(*vn_open)__P((
139 			struct vnode *vp,
140 			int mode,
141 			struct ucred *cred,
142 			struct proc *p));
143 	int	(*vn_close)__P((
144 			struct vnode *vp,
145 			int fflag,
146 			struct ucred *cred,
147 			struct proc *p));
148 	int	(*vn_access)__P((
149 			struct vnode *vp,
150 			int mode,
151 			struct ucred *cred,
152 			struct proc *p));
153 	int	(*vn_getattr)__P((
154 			struct vnode *vp,
155 			struct vattr *vap,
156 			struct ucred *cred,
157 			struct proc *p));
158 	int	(*vn_setattr)__P((
159 			struct vnode *vp,
160 			struct vattr *vap,
161 			struct ucred *cred,
162 			struct proc *p));
163 	int	(*vn_read)__P((
164 			struct vnode *vp,
165 			struct uio *uio,
166 			int ioflag,
167 			struct ucred *cred));
168 	int	(*vn_write)__P((
169 			struct vnode *vp,
170 			struct uio *uio,
171 			int ioflag,
172 			struct ucred *cred));
173 	int	(*vn_ioctl)__P((
174 			struct vnode *vp,
175 			int command,
176 			caddr_t data,
177 			int fflag,
178 			struct ucred *cred,
179 			struct proc *p));
180 	int	(*vn_select)__P((
181 			struct vnode *vp,
182 			int which,
183 			int fflags,
184 			struct ucred *cred,
185 			struct proc *p));
186 	int	(*vn_mmap)__P((
187 			struct vnode *vp,
188 			int fflags,
189 			struct ucred *cred,
190 			struct proc *p));
191 	int	(*vn_fsync)__P((
192 			struct vnode *vp,
193 			int fflags,
194 			struct ucred *cred,
195 			int waitfor,
196 			struct proc *p));
197 	int	(*vn_seek)__P((
198 			struct vnode *vp,
199 			off_t oldoff,
200 			off_t newoff,
201 			struct ucred *cred));
202 	int	(*vn_remove)__P((
203 			struct nameidata *ndp,
204 			struct proc *p));
205 	int	(*vn_link)__P((
206 			struct vnode *vp,
207 			struct nameidata *ndp,
208 			struct proc *p));
209 	int	(*vn_rename)__P((
210 			struct nameidata *fndp,
211 			struct nameidata *tdnp,
212 			struct proc *p));
213 	int	(*vn_mkdir)__P((
214 			struct nameidata *ndp,
215 			struct vattr *vap,
216 			struct proc *p));
217 	int	(*vn_rmdir)__P((
218 			struct nameidata *ndp,
219 			struct proc *p));
220 	int	(*vn_symlink)__P((
221 			struct nameidata *ndp,
222 			struct vattr *vap,
223 			char *target,
224 			struct proc *p));
225 	int	(*vn_readdir)__P((
226 			struct vnode *vp,
227 			struct uio *uio,
228 			struct ucred *cred,
229 			int *eofflagp));
230 	int	(*vn_readlink)__P((
231 			struct vnode *vp,
232 			struct uio *uio,
233 			struct ucred *cred));
234 	int	(*vn_abortop)__P((
235 			struct nameidata *ndp));
236 	int	(*vn_inactive)__P((
237 			struct vnode *vp,
238 			struct proc *p));
239 	int	(*vn_reclaim)__P((
240 			struct vnode *vp));
241 	int	(*vn_lock)__P((
242 			struct vnode *vp));
243 	int	(*vn_unlock)__P((
244 			struct vnode *vp));
245 	int	(*vn_bmap)__P((
246 			struct vnode *vp,
247 			daddr_t bn,
248 			struct vnode **vpp,
249 			daddr_t *bnp));
250 	int	(*vn_strategy)__P((
251 			struct buf *bp));
252 	int	(*vn_print)__P((
253 			struct vnode *vp));
254 	int	(*vn_islocked)__P((
255 			struct vnode *vp));
256 	int	(*vn_advlock)__P((
257 			struct vnode *vp,
258 			caddr_t id,
259 			int op,
260 			struct flock *fl,
261 			int flags));
262 };
263 
264 /* Macros to call the vnode ops */
265 #define	VOP_LOOKUP(v,n,p)	(*((v)->v_op->vn_lookup))(v,n,p)
266 #define	VOP_CREATE(n,a,p)	(*((n)->ni_dvp->v_op->vn_create))(n,a,p)
267 #define	VOP_MKNOD(n,a,c,p)	(*((n)->ni_dvp->v_op->vn_mknod))(n,a,c,p)
268 #define	VOP_OPEN(v,f,c,p)	(*((v)->v_op->vn_open))(v,f,c,p)
269 #define	VOP_CLOSE(v,f,c,p)	(*((v)->v_op->vn_close))(v,f,c,p)
270 #define	VOP_ACCESS(v,f,c,p)	(*((v)->v_op->vn_access))(v,f,c,p)
271 #define	VOP_GETATTR(v,a,c,p)	(*((v)->v_op->vn_getattr))(v,a,c,p)
272 #define	VOP_SETATTR(v,a,c,p)	(*((v)->v_op->vn_setattr))(v,a,c,p)
273 #define	VOP_READ(v,u,i,c)	(*((v)->v_op->vn_read))(v,u,i,c)
274 #define	VOP_WRITE(v,u,i,c)	(*((v)->v_op->vn_write))(v,u,i,c)
275 #define	VOP_IOCTL(v,o,d,f,c,p)	(*((v)->v_op->vn_ioctl))(v,o,d,f,c,p)
276 #define	VOP_SELECT(v,w,f,c,p)	(*((v)->v_op->vn_select))(v,w,f,c,p)
277 #define	VOP_MMAP(v,c,p)		(*((v)->v_op->vn_mmap))(v,c,p)
278 #define	VOP_FSYNC(v,f,c,w,p)	(*((v)->v_op->vn_fsync))(v,f,c,w,p)
279 #define	VOP_SEEK(v,p,o,w)	(*((v)->v_op->vn_seek))(v,p,o,w)
280 #define	VOP_REMOVE(n,p)		(*((n)->ni_dvp->v_op->vn_remove))(n,p)
281 #define	VOP_LINK(v,n,p)		(*((n)->ni_dvp->v_op->vn_link))(v,n,p)
282 #define	VOP_RENAME(s,t,p)	(*((s)->ni_dvp->v_op->vn_rename))(s,t,p)
283 #define	VOP_MKDIR(n,a,p)	(*((n)->ni_dvp->v_op->vn_mkdir))(n,a,p)
284 #define	VOP_RMDIR(n,p)		(*((n)->ni_dvp->v_op->vn_rmdir))(n,p)
285 #define	VOP_SYMLINK(n,a,m,p)	(*((n)->ni_dvp->v_op->vn_symlink))(n,a,m,p)
286 #define	VOP_READDIR(v,u,c,e)	(*((v)->v_op->vn_readdir))(v,u,c,e)
287 #define	VOP_READLINK(v,u,c)	(*((v)->v_op->vn_readlink))(v,u,c)
288 #define	VOP_ABORTOP(n)		(*((n)->ni_dvp->v_op->vn_abortop))(n)
289 #define	VOP_INACTIVE(v,p)	(*((v)->v_op->vn_inactive))(v,p)
290 #define	VOP_RECLAIM(v)		(*((v)->v_op->vn_reclaim))(v)
291 #define	VOP_LOCK(v)		(*((v)->v_op->vn_lock))(v)
292 #define	VOP_UNLOCK(v)		(*((v)->v_op->vn_unlock))(v)
293 #define	VOP_BMAP(v,s,p,n)	(*((v)->v_op->vn_bmap))(v,s,p,n)
294 #define	VOP_STRATEGY(b)		(*((b)->b_vp->v_op->vn_strategy))(b)
295 #define	VOP_PRINT(v)		(*((v)->v_op->vn_print))(v)
296 #define	VOP_ISLOCKED(v)		(((v)->v_flag & VXLOCK) || \
297 				(*((v)->v_op->vn_islocked))(v))
298 #define	VOP_ADVLOCK(v,p,o,l,f)	(*((v)->v_op->vn_advlock))(v,p,o,l,f)
299 
300 /*
301  * flags for ioflag
302  */
303 #define IO_UNIT		0x01		/* do I/O as atomic unit */
304 #define IO_APPEND	0x02		/* append write to end */
305 #define IO_SYNC		0x04		/* do I/O synchronously */
306 #define	IO_NODELOCKED	0x08		/* underlying node already locked */
307 #define	IO_NDELAY	0x10		/* FNDELAY flag set in file table */
308 
309 /*
310  *  Modes. Some values same as Ixxx entries from inode.h for now
311  */
312 #define	VSUID	04000		/* set user id on execution */
313 #define	VSGID	02000		/* set group id on execution */
314 #define	VSVTX	01000		/* save swapped text even after use */
315 #define	VREAD	0400		/* read, write, execute permissions */
316 #define	VWRITE	0200
317 #define	VEXEC	0100
318 
319 /*
320  * Token indicating no attribute value yet assigned
321  */
322 #define VNOVAL	((unsigned)0xffffffff)
323 
324 #ifdef KERNEL
325 /*
326  * public vnode manipulation functions
327  */
328 int 	vn_open __P((			/* open vnode */
329 		struct nameidata *ndp,
330 		struct proc *p,
331 		int fmode,
332 		int cmode));
333 int 	vn_rdwr __P((			/* read or write vnode */
334 		enum uio_rw rw,
335 		struct vnode *vp,
336 		caddr_t base,
337 		int len,
338 		off_t offset,
339 		enum uio_seg segflg,
340 		int ioflg,
341 		struct ucred *cred,
342 		int *aresid,
343 		struct proc *p));
344 int	vn_read __P((			/* read a vnode into a uio structure */
345 		struct file *fp,
346 		struct uio *uio,
347 		struct ucred *cred));
348 int	vn_write __P((			/* write a vnode from a uio structure */
349 		struct file *fp,
350 		struct uio *uio,
351 		struct ucred *cred));
352 int	vn_ioctl __P((			/* do an ioctl operation on a vnode */
353 		struct file *fp,
354 		int com,
355 		caddr_t data,
356 		struct proc *p));
357 int	vn_select __P((			/* do a select operation on a vnode */
358 		struct file *fp,
359 		int which,
360 		struct proc *p));
361 int 	vn_close __P((			/* close vnode */
362 		struct file *fp,
363 		struct proc *p));
364 int 	getnewvnode __P((		/* allocate a new vnode */
365 		enum vtagtype tag,
366 		struct mount *mp,
367 		struct vnodeops *vops,
368 		struct vnode **vpp));
369 int 	bdevvp __P((			/* allocate a new special dev vnode */
370 		int dev,		/* XXX should be type dev_t, not int */
371 		struct vnode **vpp));
372 struct 	vnode *checkalias __P((		/* check for special device aliases */
373 		struct vnode *vp,
374 		int nvp_rdev,		/* XXX should be type dev_t, not int */
375 		struct mount *mp));
376 void 	vattr_null __P((		/* set attributes to null */
377 		struct vattr *vap));
378 int 	vcount __P((struct vnode *vp));	/* total references to a device */
379 int 	vget __P((struct vnode *vp));	/* get first reference to a vnode */
380 void 	vref __P((struct vnode *vp));	/* increase reference to a vnode */
381 void 	vput __P((struct vnode *vp));	/* unlock and release vnode */
382 void 	vrele __P((struct vnode *vp));	/* release vnode */
383 void 	vgone __P((struct vnode *vp));	/* completely recycle vnode */
384 void 	vgoneall __P((struct vnode *vp));/* recycle vnode and all its aliases */
385 
386 /*
387  * Flags to various vnode functions.
388  */
389 #define	SKIPSYSTEM	0x0001		/* vflush: skip vnodes marked VSYSTEM */
390 #define	FORCECLOSE	0x0002		/* vflush: force file closeure */
391 #define	DOCLOSE		0x0004		/* vclean: close active files */
392 
393 #ifndef DIAGNOSTIC
394 #define VREF(vp)    (vp)->v_usecount++	/* increase reference to a vnode */
395 #define VHOLD(vp)   (vp)->v_holdcnt++	/* increase buf or page ref to vnode */
396 #define HOLDRELE(vp) (vp)->v_holdcnt--	/* decrease buf or page ref to vnode */
397 #define	VATTR_NULL(vap) *(vap) = va_null /* initialize a vattr stucture */
398 #else /* DIAGNOSTIC */
399 #define VREF(vp)    vref(vp)
400 #define VHOLD(vp)   vhold(vp)
401 #define HOLDRELE(vp) holdrele(vp)
402 #define	VATTR_NULL(vap) vattr_null(vap)
403 #endif
404 
405 #define	NULLVP	((struct vnode *)0)
406 
407 /*
408  * Global vnode data.
409  */
410 extern	struct vnode *rootdir;		/* root (i.e. "/") vnode */
411 extern	int desiredvnodes;		/* number of vnodes desired */
412 extern	struct vattr va_null;		/* predefined null vattr structure */
413 #endif
414