xref: /original-bsd/sys/sys/vnode.h (revision 68d9582f)
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.54 (Berkeley) 06/19/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
16  * unique vnode allocated for each active file, each current directory,
17  * 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 by 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 	daddr_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 	int 		(**v_op)();		/* vnode operations vector */
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_size;	/* file size in bytes */
95 	long		va_blocksize;	/* blocksize preferred for i/o */
96 	struct timespec	va_atime;	/* time of last access */
97 	struct timespec	va_mtime;	/* time of last modification */
98 	struct timespec	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 	u_quad_t	va_bytes;	/* bytes of disk space held by file */
103 	u_quad_t	va_filerev;	/* file modification number */
104 };
105 
106 /*
107  * Flags for ioflag.
108  */
109 #define	IO_UNIT		0x01		/* do I/O as atomic unit */
110 #define	IO_APPEND	0x02		/* append write to end */
111 #define	IO_SYNC		0x04		/* do I/O synchronously */
112 #define	IO_NODELOCKED	0x08		/* underlying node already locked */
113 #define	IO_NDELAY	0x10		/* FNDELAY flag set in file table */
114 
115 /*
116  *  Modes.  Some values same as Ixxx entries from inode.h for now.
117  */
118 #define	VSUID	04000		/* set user id on execution */
119 #define	VSGID	02000		/* set group id on execution */
120 #define	VSVTX	01000		/* save swapped text even after use */
121 #define	VREAD	00400		/* read, write, execute permissions */
122 #define	VWRITE	00200
123 #define	VEXEC	00100
124 
125 /*
126  * Token indicating no attribute value yet assigned.
127  */
128 #define	VNOVAL	(-1)
129 
130 #ifdef KERNEL
131 /*
132  * Convert between vnode types and inode formats (since POSIX.1
133  * defines mode word of stat structure in terms of inode formats).
134  */
135 extern enum vtype	iftovt_tab[];
136 extern int		vttoif_tab[];
137 #define IFTOVT(mode)	(iftovt_tab[((mode) & IFMT) >> 12])
138 #define VTTOIF(indx)	(vttoif_tab[(int)(indx)])
139 #define MAKEIMODE(indx, mode)	(int)(VTTOIF(indx) | (mode))
140 
141 /*
142  * Flags to various vnode functions.
143  */
144 #define	SKIPSYSTEM	0x0001		/* vflush: skip vnodes marked VSYSTEM */
145 #define	FORCECLOSE	0x0002		/* vflush: force file closeure */
146 #define	DOCLOSE		0x0004		/* vclean: close active files */
147 
148 #ifdef DIAGNOSTIC
149 #define	HOLDRELE(vp)	holdrele(vp)
150 #define	VATTR_NULL(vap)	vattr_null(vap)
151 #define	VHOLD(vp)	vhold(vp)
152 #define	VREF(vp)	vref(vp)
153 
154 void	holdrele __P((struct vnode *));
155 void	vattr_null __P((struct vattr *));
156 void	vhold __P((struct vnode *));
157 void	vref __P((struct vnode *));
158 #else
159 #define	HOLDRELE(vp)	(vp)->v_holdcnt--	/* decrease buf or page ref */
160 #define	VATTR_NULL(vap)	(*(vap) = va_null)	/* initialize a vattr */
161 #define	VHOLD(vp)	(vp)->v_holdcnt++	/* increase buf or page ref */
162 #define	VREF(vp)	(vp)->v_usecount++	/* increase reference */
163 #endif
164 
165 #define	NULLVP	((struct vnode *)NULL)
166 
167 /*
168  * Global vnode data.
169  */
170 extern	struct vnode *rootdir;		/* root (i.e. "/") vnode */
171 extern	int desiredvnodes;		/* number of vnodes desired */
172 extern	struct vattr va_null;		/* predefined null vattr structure */
173 
174 /*
175  * Macro/function to check for client cache inconsistency w.r.t. leasing.
176  */
177 #define	LEASE_READ	0x1		/* Check lease for readers */
178 #define	LEASE_WRITE	0x2		/* Check lease for modifiers */
179 
180 #ifdef NFS
181 void	lease_check __P((struct vnode *vp, struct proc *p,
182 	    struct ucred *ucred, int flag));
183 void	lease_updatetime __P((int deltat));
184 #define	LEASE_CHECK(vp, p, cred, flag)	lease_check((vp), (p), (cred), (flag))
185 #define	LEASE_UPDATETIME(dt)		lease_updatetime(dt)
186 #else
187 #define	LEASE_CHECK(vp, p, cred, flag)
188 #define	LEASE_UPDATETIME(dt)
189 #endif /* NFS */
190 #endif
191 
192 
193 /*
194  * Mods for exensibility.
195  */
196 
197 /*
198  * Flags for vdesc_flags:
199  */
200 #define VDESC_MAX_VPS		16
201 /* Low order 16 flag bits are reserved for map flags for vp arguments. */
202 #define VDESC_NOMAP_VPP		0x0100
203 
204 /*
205  * VDESC_NO_OFFSET is used to identify the end of the offset list
206  * and in places where no such field exists.
207  */
208 #define VDESC_NO_OFFSET -1
209 
210 /*
211  * This structure describes the vnode operation taking place.
212  */
213 struct vnodeop_desc {
214 	int	vdesc_offset;		/* offset in vector--first for speed */
215 	char    *vdesc_name;		/* a readable name for debugging */
216 	int	vdesc_flags;		/* VDESC_* flags */
217 
218 	/*
219 	 * These ops are used by bypass routines to map and locate arguments.
220 	 * Creds and procs are not needed in bypass routines, but sometimes
221 	 * they are useful to (for example) transport layers.
222 	 */
223 	int	*vdesc_vp_offsets;	/* list ended by VDESC_NO_OFFSET */
224 	int	vdesc_vpp_offset;	/* return vpp location */
225 	int	vdesc_cred_offset;	/* cred location, if any */
226 	int	vdesc_proc_offset;	/* proc location, if any */
227 	/*
228 	 * Finally, we've got a list of private data (about each operation)
229 	 * for each transport layer.  (Support to manage this list is not
230 	 * yet part of BSD.)
231 	 */
232 	caddr_t	*vdesc_transports;
233 };
234 
235 /*
236  * A list of all the operation descs.
237  */
238 extern struct vnodeop_desc *vnodeop_descs[];
239 
240 
241 /*
242  * This macro is very helpful in defining those offsets in the vdesc struct.
243  *
244  * This is stolen from X11R4.  I ingored all the fancy stuff for
245  * Crays, so if you decide to port this to such a serious machine,
246  * you might want to consult Intrisics.h's XtOffset{,Of,To}.
247  */
248 #define VOPARG_OFFSET(p_type,field) \
249         ((int) (((char *) (&(((p_type)NULL)->field))) - ((char *) NULL)))
250 #define VOPARG_OFFSETOF(s_type,field) \
251 	VOPARG_OFFSET(s_type*,field)
252 #define VOPARG_OFFSETTO(S_TYPE,S_OFFSET,STRUCT_P) \
253 	((S_TYPE)(((char*)(STRUCT_P))+(S_OFFSET)))
254 
255 
256 /*
257  * This structure is used to configure the new vnodeops vector.
258  */
259 struct vnodeopv_entry_desc {
260 	struct vnodeop_desc *opve_op;   /* which operation this is */
261 	int (*opve_impl)();		/* code implementing this operation */
262 };
263 struct vnodeopv_desc {
264 			/* ptr to the ptr to the vector where op should go */
265 	int (***opv_desc_vector_p)();
266 	struct vnodeopv_entry_desc *opv_desc_ops;   /* null terminated list */
267 };
268 
269 /*
270  * A default routine which just returns an error.
271  */
272 int vn_default_error __P((void));
273 
274 /*
275  * A generic structure.
276  * This can be used by bypass routines to identify generic arguments.
277  */
278 struct vop_generic_args {
279 	struct vnodeop_desc *a_desc;
280 	/* other random data follows, presumably */
281 };
282 
283 /*
284  * VOCALL calls an op given an ops vector.  We break it out because BSD's
285  * vclean changes the ops vector and then wants to call ops with the old
286  * vector.
287  */
288 #define VOCALL(OPSV,OFF,AP) (( *((OPSV)[(OFF)])) (AP))
289 
290 /*
291  * This call works for vnodes in the kernel.
292  */
293 #define VCALL(VP,OFF,AP) VOCALL((VP)->v_op,(OFF),(AP))
294 #define VDESC(OP) (& __CONCAT(OP,_desc))
295 #define VOFFSET(OP) (VDESC(OP)->vdesc_offset)
296 
297 /*
298  * Finally, include the default set of vnode operations.
299  */
300 #include <sys/vnode_if.h>
301 
302 /*
303  * Public vnode manipulation functions.
304  */
305 struct file;
306 struct mount;
307 struct nameidata;
308 struct proc;
309 struct ucred;
310 struct uio;
311 struct vattr;
312 struct vnode;
313 struct vop_bwrite_args;
314 
315 int 	bdevvp __P((dev_t dev, struct vnode **vpp));
316 int 	getnewvnode __P((enum vtagtype tag,
317 	    struct mount *mp, int (**vops)(), struct vnode **vpp));
318 void 	vattr_null __P((struct vattr *vap));
319 int 	vcount __P((struct vnode *vp));
320 int 	vget __P((struct vnode *vp));
321 void 	vgone __P((struct vnode *vp));
322 void 	vgoneall __P((struct vnode *vp));
323 int	vn_bwrite __P((struct vop_bwrite_args *ap));
324 int 	vn_close __P((struct vnode *vp,
325 	    int flags, struct ucred *cred, struct proc *p));
326 int 	vn_closefile __P((struct file *fp, struct proc *p));
327 int	vn_ioctl __P((struct file *fp, int com, caddr_t data, struct proc *p));
328 int 	vn_open __P((struct nameidata *ndp, int fmode, int cmode));
329 int 	vn_rdwr __P((enum uio_rw rw, struct vnode *vp, caddr_t base,
330 	    int len, off_t offset, enum uio_seg segflg, int ioflg,
331 	    struct ucred *cred, int *aresid, struct proc *p));
332 int	vn_read __P((struct file *fp, struct uio *uio, struct ucred *cred));
333 int	vn_select __P((struct file *fp, int which, struct proc *p));
334 int	vn_write __P((struct file *fp, struct uio *uio, struct ucred *cred));
335 struct vnode *
336 	checkalias __P((struct vnode *vp, dev_t nvp_rdev, struct mount *mp));
337 void 	vput __P((struct vnode *vp));
338 void 	vref __P((struct vnode *vp));
339 void 	vrele __P((struct vnode *vp));
340