xref: /original-bsd/sys/sys/vnode.h (revision 753853ba)
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.47 (Berkeley) 02/04/92
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.  There is a unique
16  * vnode allocated for each active file, each current directory, each
17  * 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, VT_LFS };
31 
32 /*
33  * Each underlying filesystem allocates its own private area and hangs
34  * it from v_data. If non-null, this area is free in getnewvnode().
35  */
36 struct vnode {
37 	u_long		v_flag;			/* vnode flags (see below) */
38 	short		v_usecount;		/* reference count of users */
39 	short		v_writecount;		/* reference count of writers */
40 	long		v_holdcnt;		/* page & buffer references */
41 	off_t		v_lastr;		/* last read (read-ahead) */
42 	u_long		v_id;			/* capability identifier */
43 	struct mount	*v_mount;		/* ptr to vfs we are in */
44 	struct vnodeops	*v_op;			/* vnode operations */
45 	struct vnode	*v_freef;		/* vnode freelist forward */
46 	struct vnode	**v_freeb;		/* vnode freelist back */
47 	struct vnode	*v_mountf;		/* vnode mountlist forward */
48 	struct vnode	**v_mountb;		/* vnode mountlist back */
49 	struct buf	*v_cleanblkhd;		/* clean blocklist head */
50 	struct buf	*v_dirtyblkhd;		/* dirty blocklist head */
51 	long		v_numoutput;		/* num of writes in progress */
52 	enum vtype	v_type;			/* vnode type */
53 	union {
54 		struct mount	*vu_mountedhere;/* ptr to mounted vfs (VDIR) */
55 		struct socket	*vu_socket;	/* unix ipc (VSOCK) */
56 		caddr_t		vu_vmdata;	/* private data for vm (VREG) */
57 		struct specinfo	*vu_specinfo;	/* device (VCHR, VBLK) */
58 		struct fifoinfo	*vu_fifoinfo;	/* fifo (VFIFO) */
59 	} v_un;
60 	struct nqlease	*v_lease;		/* Soft reference to lease */
61 	long		v_spare[13];		/* round to 128 bytes */
62 	enum vtagtype	v_tag;			/* type of underlying data */
63 	void 		*v_data;		/* private data for fs */
64 };
65 #define	v_mountedhere	v_un.vu_mountedhere
66 #define	v_socket	v_un.vu_socket
67 #define	v_vmdata	v_un.vu_vmdata
68 #define	v_specinfo	v_un.vu_specinfo
69 #define	v_fifoinfo	v_un.vu_fifoinfo
70 
71 /*
72  * vnode flags.
73  */
74 #define	VROOT		0x0001	/* root of its file system */
75 #define	VTEXT		0x0002	/* vnode is a pure text prototype */
76 #define	VSYSTEM		0x0004	/* vnode being used by kernel */
77 #define	VXLOCK		0x0100	/* vnode is locked to change underlying type */
78 #define	VXWANT		0x0200	/* process is waiting for vnode */
79 #define	VBWAIT		0x0400	/* waiting for output to complete */
80 #define	VALIASED	0x0800	/* vnode has an alias */
81 
82 /*
83  * Vnode attributes.  A field value of VNOVAL represents a field whose value
84  * is unavailable (getattr) or which is not to be changed (setattr).
85  */
86 struct vattr {
87 	enum vtype	va_type;	/* vnode type (for create) */
88 	u_short		va_mode;	/* files access mode and type */
89 	short		va_nlink;	/* number of references to file */
90 	uid_t		va_uid;		/* owner user id */
91 	gid_t		va_gid;		/* owner group id */
92 	long		va_fsid;	/* file system id (dev for now) */
93 	long		va_fileid;	/* file id */
94 	u_quad_t	va_qsize;	/* file size in bytes */
95 	long		va_blocksize;	/* blocksize preferred for i/o */
96 	struct timeval	va_atime;	/* time of last access */
97 	struct timeval	va_mtime;	/* time of last modification */
98 	struct timeval	va_ctime;	/* time file changed */
99 	u_long		va_gen;		/* generation number of file */
100 	u_long		va_flags;	/* flags defined for file */
101 	dev_t		va_rdev;	/* device the special file represents */
102 	short		va_pad;		/* pad out to long */
103 	u_quad_t	va_qbytes;	/* bytes of disk space held by file */
104 	u_quad_t	va_filerev;	/* file modification number */
105 };
106 #ifdef _NOQUAD
107 #define va_size		va_qsize.val[_QUAD_LOWWORD]
108 #define va_size_rsv	va_qsize.val[_QUAD_HIGHWORD]
109 #define va_bytes	va_qbytes.val[_QUAD_LOWWORD]
110 #define va_bytes_rsv	va_qbytes.val[_QUAD_HIGHWORD]
111 #else
112 #define va_size		va_qsize
113 #define va_bytes	va_qbytes
114 #endif
115 
116 /*
117  * Operations on vnodes.
118  */
119 #ifdef __STDC__
120 struct flock;
121 struct nameidata;
122 struct componentname;
123 #endif
124 
125 struct vnodeops {
126 #define	VOP_LOOKUP(d,v,c)	(*((d)->v_op->vop_lookup))(d,v,c)
127 	int	(*vop_lookup)	__P((struct vnode *dvp, struct vnode **vpp,
128 				     struct componentname *cnp));
129 #define	VOP_CREATE(d,v,c,a)	(*((d)->v_op->vop_create))(d,v,c,a)
130 	int	(*vop_create)	__P((struct vnode *dvp, struct vnode **vpp,
131 				     struct componentname *cnp,
132 				     struct vattr *vap));
133 #define	VOP_MKNOD(d,v,c,a)	(*((d)->v_op->vop_mknod))(d,v,c,a)
134 	int	(*vop_mknod)	__P((struct vnode *dvp, struct vnode **vpp,
135 				     struct componentname *cnp,
136 				     struct vattr *vap));
137 #define	VOP_OPEN(v,f,c,p)	(*((v)->v_op->vop_open))(v,f,c,p)
138 	int	(*vop_open)	__P((struct vnode *vp, int mode,
139 				    struct ucred *cred, struct proc *p));
140 #define	VOP_CLOSE(v,f,c,p)	(*((v)->v_op->vop_close))(v,f,c,p)
141 	int	(*vop_close)	__P((struct vnode *vp, int fflag,
142 				    struct ucred *cred, struct proc *p));
143 #define	VOP_ACCESS(v,f,c,p)	(*((v)->v_op->vop_access))(v,f,c,p)
144 	int	(*vop_access)	__P((struct vnode *vp, int mode,
145 				    struct ucred *cred, struct proc *p));
146 #define	VOP_GETATTR(v,a,c,p)	(*((v)->v_op->vop_getattr))(v,a,c,p)
147 	int	(*vop_getattr)	__P((struct vnode *vp, struct vattr *vap,
148 				    struct ucred *cred, struct proc *p));
149 #define	VOP_SETATTR(v,a,c,p)	(*((v)->v_op->vop_setattr))(v,a,c,p)
150 	int	(*vop_setattr)	__P((struct vnode *vp, struct vattr *vap,
151 				    struct ucred *cred, struct proc *p));
152 #define	VOP_READ(v,u,i,c)	(*((v)->v_op->vop_read))(v,u,i,c)
153 	int	(*vop_read)	__P((struct vnode *vp, struct uio *uio,
154 				    int ioflag, struct ucred *cred));
155 #define	VOP_WRITE(v,u,i,c)	(*((v)->v_op->vop_write))(v,u,i,c)
156 	int	(*vop_write)	__P((struct vnode *vp, struct uio *uio,
157 				    int ioflag, struct ucred *cred));
158 #define	VOP_IOCTL(v,o,d,f,c,p)	(*((v)->v_op->vop_ioctl))(v,o,d,f,c,p)
159 	int	(*vop_ioctl)	__P((struct vnode *vp, int command,
160 				    caddr_t data, int fflag,
161 				    struct ucred *cred, struct proc *p));
162 #define	VOP_SELECT(v,w,f,c,p)	(*((v)->v_op->vop_select))(v,w,f,c,p)
163 	int	(*vop_select)	__P((struct vnode *vp, int which, int fflags,
164 				    struct ucred *cred, struct proc *p));
165 #define	VOP_MMAP(v,c,p)		(*((v)->v_op->vop_mmap))(v,c,p)
166 	int	(*vop_mmap)	__P((struct vnode *vp, int fflags,
167 				    struct ucred *cred, struct proc *p));
168 #define	VOP_FSYNC(v,f,c,w,p)	(*((v)->v_op->vop_fsync))(v,f,c,w,p)
169 	int	(*vop_fsync)	__P((struct vnode *vp, int fflags,
170 				    struct ucred *cred, int waitfor,
171 				    struct proc *p));
172 #define	VOP_SEEK(v,p,o,w)	(*((v)->v_op->vop_seek))(v,p,o,w)
173 	int	(*vop_seek)	__P((struct vnode *vp, off_t oldoff,
174 				    off_t newoff, struct ucred *cred));
175 #define	VOP_REMOVE(d,v,c)	(*((d)->v_op->vop_remove))(d,v,c)
176 	int	(*vop_remove)	__P((struct vnode *dvp, struct vnode *vp,
177 				     struct componentname *cnp));
178 #define	VOP_LINK(v,d,c)		(*((v)->v_op->vop_link))(v,d,c)
179 	int	(*vop_link)	__P((struct vnode *vp, struct vnode *tdvp,
180 				     struct componentname *cnp));
181 #define	VOP_RENAME(fd,fv,fc,td,tv,tc) (*((fd)->v_op->vop_rename))(fd,fv,fc,td,tv,tc)
182 	int	(*vop_rename)	__P((struct vnode *fdvp, struct vnode *fvp,
183 				     struct componentname *fcnp,
184 				     struct vnode *tdvp, struct vnode *tvp,
185 				     struct componentname *tcnp));
186 #define	VOP_MKDIR(d,v,c,a)	(*((d)->v_op->vop_mkdir))(d,v,c,a)
187 	int	(*vop_mkdir)	__P((struct vnode *dvp, struct vnode **vpp,
188 				     struct componentname *cnp,
189 				     struct vattr *vap));
190 #define	VOP_RMDIR(d,v,c)	(*((d)->v_op->vop_rmdir))(d,v,c)
191 	int	(*vop_rmdir)	__P((struct vnode *dvp, struct vnode *vp,
192 				     struct componentname *cnp));
193 #define	VOP_SYMLINK(d,v,c,a,t)	(*((d)->v_op->vop_symlink))(d,v,c,a,t)
194 	int	(*vop_symlink)	__P((struct vnode *dvp, struct vnode **vpp,
195 				     struct componentname *cnp,
196 				     struct vattr *vap, char *target));
197 #define	VOP_READDIR(v,u,c,e)	(*((v)->v_op->vop_readdir))(v,u,c,e)
198 	int	(*vop_readdir)	__P((struct vnode *vp, struct uio *uio,
199 				    struct ucred *cred, int *eofflagp));
200 #define	VOP_READLINK(v,u,c)	(*((v)->v_op->vop_readlink))(v,u,c)
201 	int	(*vop_readlink)	__P((struct vnode *vp, struct uio *uio,
202 				    struct ucred *cred));
203 #define	VOP_ABORTOP(d,c)	(*((d)->v_op->vop_abortop))(d,c)
204 	int	(*vop_abortop)	__P((struct vnode *dvp,
205 				     struct componentname *cnp));
206 #define	VOP_INACTIVE(v,p)	(*((v)->v_op->vop_inactive))(v,p)
207 	int	(*vop_inactive)	__P((struct vnode *vp, struct proc *p));
208 #define	VOP_RECLAIM(v)		(*((v)->v_op->vop_reclaim))(v)
209 	int	(*vop_reclaim)	__P((struct vnode *vp));
210 #define	VOP_LOCK(v)		(*((v)->v_op->vop_lock))(v)
211 	int	(*vop_lock)	__P((struct vnode *vp));
212 #define	VOP_UNLOCK(v)		(*((v)->v_op->vop_unlock))(v)
213 	int	(*vop_unlock)	__P((struct vnode *vp));
214 #define	VOP_BMAP(v,s,p,n)	(*((v)->v_op->vop_bmap))(v,s,p,n)
215 	int	(*vop_bmap)	__P((struct vnode *vp, daddr_t bn,
216 				    struct vnode **vpp, daddr_t *bnp));
217 #define	VOP_STRATEGY(b)		(*((b)->b_vp->v_op->vop_strategy))(b)
218 	int	(*vop_strategy)	__P((struct buf *bp));
219 #define	VOP_PRINT(v)		(*((v)->v_op->vop_print))(v)
220 	int	(*vop_print)	__P((struct vnode *vp));
221 #define	VOP_ISLOCKED(v)		(((v)->v_flag & VXLOCK) || \
222 				(*((v)->v_op->vop_islocked))(v))
223 	int	(*vop_islocked)	__P((struct vnode *vp));
224 #define	VOP_ADVLOCK(v,p,o,l,f)	(*((v)->v_op->vop_advlock))(v,p,o,l,f)
225 	int	(*vop_advlock)	__P((struct vnode *vp, caddr_t id, int op,
226 				    struct flock *fl, int flags));
227 #define	VOP_BLKATOFF(v,o,r,b)	(*((v)->v_op->vop_blkatoff))(v,o,r,b)
228 	int	(*vop_blkatoff) __P((struct vnode *vp,
229 		    off_t offset, char **res, struct buf **bpp));
230 #define	VOP_VGET(v,i,vp)	(*((v)->v_op->vop_vget))((v)->v_mount,i,vp)
231 	int	(*vop_vget)
232 		    __P((struct mount *mp, ino_t ino, struct vnode **vpp));
233 #define	VOP_VALLOC(v,m,c,vp)	(*((v)->v_op->vop_valloc))(v,m,c,vp)
234 	int	(*vop_valloc) __P((struct vnode *pvp,
235 		    int mode, struct ucred *cred, struct vnode **vpp));
236 #define	VOP_VFREE(v,i,m)	(*((v)->v_op->vop_vfree))(v,i,m)
237 	void	(*vop_vfree) __P((struct vnode *pvp, ino_t ino, int mode));
238 #define	VOP_TRUNCATE(v,l,f)	(*((v)->v_op->vop_truncate))(v,l,f)
239 	int	(*vop_truncate)
240 		    __P((struct vnode *vp, u_long length, int flags));
241 #define	VOP_UPDATE(v,ta,tm,w)	(*((v)->v_op->vop_update))(v,ta,tm,w)
242 	int	(*vop_update) __P((struct vnode *vp,
243 		    struct timeval *ta, struct timeval *tm, int waitfor));
244 #define	VOP_BWRITE(b)		(*((b)->b_vp->v_op->vop_bwrite))(b)
245 	int	(*vop_bwrite) __P((struct buf *bp));
246 };
247 
248 /*
249  * flags for ioflag
250  */
251 #define	IO_UNIT		0x01		/* do I/O as atomic unit */
252 #define	IO_APPEND	0x02		/* append write to end */
253 #define	IO_SYNC		0x04		/* do I/O synchronously */
254 #define	IO_NODELOCKED	0x08		/* underlying node already locked */
255 #define	IO_NDELAY	0x10		/* FNDELAY flag set in file table */
256 
257 /*
258  *  Modes. Some values same as Ixxx entries from inode.h for now
259  */
260 #define	VSUID	04000		/* set user id on execution */
261 #define	VSGID	02000		/* set group id on execution */
262 #define	VSVTX	01000		/* save swapped text even after use */
263 #define	VREAD	0400		/* read, write, execute permissions */
264 #define	VWRITE	0200
265 #define	VEXEC	0100
266 
267 /*
268  * Token indicating no attribute value yet assigned
269  */
270 #define	VNOVAL	(-1)
271 
272 #ifdef KERNEL
273 /*
274  * Convert between vnode types and inode formats (since POSIX.1
275  * defines mode word of stat structure in terms of inode formats).
276  */
277 extern enum vtype	iftovt_tab[];
278 extern int		vttoif_tab[];
279 #define IFTOVT(mode)	(iftovt_tab[((mode) & IFMT) >> 12])
280 #define VTTOIF(indx)	(vttoif_tab[(int)(indx)])
281 #define MAKEIMODE(indx, mode)	(int)(VTTOIF(indx) | (mode))
282 
283 /*
284  * public vnode manipulation functions
285  */
286 int 	vn_open __P((struct nameidata *ndp, int fmode, int cmode));
287 int 	vn_close __P((struct vnode *vp, int flags, struct ucred *cred,
288 	    struct proc *p));
289 int 	vn_rdwr __P((enum uio_rw rw, struct vnode *vp, caddr_t base,
290 	    int len, off_t offset, enum uio_seg segflg, int ioflg,
291 	    struct ucred *cred, int *aresid, struct proc *p));
292 int	vn_read __P((struct file *fp, struct uio *uio, struct ucred *cred));
293 int	vn_write __P((struct file *fp, struct uio *uio, struct ucred *cred));
294 int	vn_ioctl __P((struct file *fp, int com, caddr_t data, struct proc *p));
295 int	vn_select __P((struct file *fp, int which, struct proc *p));
296 int 	vn_closefile __P((struct file *fp, struct proc *p));
297 int 	getnewvnode __P((enum vtagtype tag, struct mount *mp,
298 	    struct vnodeops *vops, struct vnode **vpp));
299 int 	bdevvp __P((int dev, struct vnode **vpp));
300 	/* check for special device aliases */
301 	/* XXX nvp_rdev should be type dev_t, not int */
302 struct 	vnode *checkalias __P((struct vnode *vp, int nvp_rdev,
303 	    struct mount *mp));
304 void 	vattr_null __P((struct vattr *vap));
305 int 	vcount __P((struct vnode *vp));	/* total references to a device */
306 int 	vget __P((struct vnode *vp));	/* get first reference to a vnode */
307 void 	vref __P((struct vnode *vp));	/* increase reference to a vnode */
308 void 	vput __P((struct vnode *vp));	/* unlock and release vnode */
309 void 	vrele __P((struct vnode *vp));	/* release vnode */
310 void 	vgone __P((struct vnode *vp));	/* completely recycle vnode */
311 void 	vgoneall __P((struct vnode *vp));/* recycle vnode and all its aliases */
312 
313 /*
314  * Flags to various vnode functions.
315  */
316 #define	SKIPSYSTEM	0x0001		/* vflush: skip vnodes marked VSYSTEM */
317 #define	FORCECLOSE	0x0002		/* vflush: force file closeure */
318 #define	DOCLOSE		0x0004		/* vclean: close active files */
319 
320 #ifndef DIAGNOSTIC
321 #define	VREF(vp)	(vp)->v_usecount++	/* increase reference */
322 #define	VHOLD(vp)	(vp)->v_holdcnt++	/* increase buf or page ref */
323 #define	HOLDRELE(vp)	(vp)->v_holdcnt--	/* decrease buf or page ref */
324 #define	VATTR_NULL(vap)	(*(vap) = va_null)	/* initialize a vattr */
325 #else
326 #define	VREF(vp)	vref(vp)
327 #define	VHOLD(vp)	vhold(vp)
328 #define	HOLDRELE(vp)	holdrele(vp)
329 #define	VATTR_NULL(vap)	vattr_null(vap)
330 #endif
331 
332 #define	NULLVP	((struct vnode *)NULL)
333 
334 /*
335  * Global vnode data.
336  */
337 extern	struct vnode *rootdir;		/* root (i.e. "/") vnode */
338 extern	int desiredvnodes;		/* number of vnodes desired */
339 extern	struct vattr va_null;		/* predefined null vattr structure */
340 
341 /*
342  * Macro/function to check for client cache inconsistency w.r.t. leasing.
343  */
344 #define	LEASE_READ	0x1		/* Check lease for readers */
345 #define	LEASE_WRITE	0x2		/* Check lease for modifiers */
346 
347 #ifdef NFS
348 void	lease_check __P((struct vnode *vp, struct proc *p,
349 		struct ucred *ucred, int flag));
350 void	lease_updatetime __P((int deltat));
351 #define	LEASE_CHECK(vp, p, cred, flag)	lease_check((vp), (p), (cred), (flag))
352 #define	LEASE_UPDATETIME(dt)		lease_updatetime(dt)
353 #else
354 #define	LEASE_CHECK(vp, p, cred, flag)
355 #define	LEASE_UPDATETIME(dt)
356 #endif /* NFS */
357 #endif
358