xref: /original-bsd/sys/sys/vnode.h (revision 333da485)
1 /*
2  * Copyright (c) 1989, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  *
7  *	@(#)vnode.h	8.6 (Berkeley) 01/12/94
8  */
9 
10 #include <sys/queue.h>
11 
12 /*
13  * The vnode is the focus of all file activity in UNIX.  There is a
14  * unique vnode allocated for each active file, each current directory,
15  * each mounted-on file, text file, and the root.
16  */
17 
18 /*
19  * Vnode types.  VNON means no type.
20  */
21 enum vtype	{ VNON, VREG, VDIR, VBLK, VCHR, VLNK, VSOCK, VFIFO, VBAD };
22 
23 /*
24  * Vnode tag types.
25  * These are for the benefit of external programs only (e.g., pstat)
26  * and should NEVER be inspected by the kernel.
27  */
28 enum vtagtype	{
29 	VT_NON, VT_UFS, VT_NFS, VT_MFS, VT_PC, VT_LFS, VT_LOFS, VT_FDESC,
30 	VT_PORTAL, VT_NULL, VT_UMAP, VT_KERNFS, VT_PROCFS, VT_AFS, VT_ISOFS
31 };
32 
33 /*
34  * Each underlying filesystem allocates its own private area and hangs
35  * it from v_data.  If non-null, this area is freed in getnewvnode().
36  */
37 LIST_HEAD(buflists, buf);
38 
39 struct vnode {
40 	u_long	v_flag;				/* vnode flags (see below) */
41 	short	v_usecount;			/* reference count of users */
42 	short	v_writecount;			/* reference count of writers */
43 	long	v_holdcnt;			/* page & buffer references */
44 	daddr_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 	int 	(**v_op)();			/* vnode operations vector */
48 	TAILQ_ENTRY(vnode) v_freelist;		/* vnode freelist */
49 	LIST_ENTRY(vnode) v_mntvnodes;		/* vnodes for mount point */
50 	struct	buflists v_cleanblkhd;		/* clean blocklist head */
51 	struct	buflists v_dirtyblkhd;		/* dirty blocklist head */
52 	long	v_numoutput;			/* num of writes in progress */
53 	enum	vtype v_type;			/* vnode type */
54 	union {
55 		struct mount	*vu_mountedhere;/* ptr to mounted vfs (VDIR) */
56 		struct socket	*vu_socket;	/* unix ipc (VSOCK) */
57 		caddr_t		vu_vmdata;	/* private data for vm (VREG) */
58 		struct specinfo	*vu_specinfo;	/* device (VCHR, VBLK) */
59 		struct fifoinfo	*vu_fifoinfo;	/* fifo (VFIFO) */
60 	} v_un;
61 	struct	nqlease *v_lease;		/* Soft reference to lease */
62 	daddr_t	v_lastw;			/* last write (write cluster) */
63 	daddr_t	v_cstart;			/* start block of cluster */
64 	daddr_t	v_lasta;			/* last allocation */
65 	int	v_clen;				/* length of current cluster */
66 	int	v_ralen;			/* Read-ahead length */
67 	daddr_t	v_maxra;			/* last readahead block */
68 	long	v_spare[7];			/* round to 128 bytes */
69 	enum	vtagtype v_tag;			/* type of underlying data */
70 	void 	*v_data;			/* private data for fs */
71 };
72 #define	v_mountedhere	v_un.vu_mountedhere
73 #define	v_socket	v_un.vu_socket
74 #define	v_vmdata	v_un.vu_vmdata
75 #define	v_specinfo	v_un.vu_specinfo
76 #define	v_fifoinfo	v_un.vu_fifoinfo
77 
78 /*
79  * Vnode flags.
80  */
81 #define	VROOT		0x0001	/* root of its file system */
82 #define	VTEXT		0x0002	/* vnode is a pure text prototype */
83 #define	VSYSTEM		0x0004	/* vnode being used by kernel */
84 #define	VXLOCK		0x0100	/* vnode is locked to change underlying type */
85 #define	VXWANT		0x0200	/* process is waiting for vnode */
86 #define	VBWAIT		0x0400	/* waiting for output to complete */
87 #define	VALIASED	0x0800	/* vnode has an alias */
88 #define	VDIROP		0x1000	/* LFS: vnode is involved in a directory op */
89 
90 /*
91  * Vnode attributes.  A field value of VNOVAL represents a field whose value
92  * is unavailable (getattr) or which is not to be changed (setattr).
93  */
94 struct vattr {
95 	enum vtype	va_type;	/* vnode type (for create) */
96 	u_short		va_mode;	/* files access mode and type */
97 	short		va_nlink;	/* number of references to file */
98 	uid_t		va_uid;		/* owner user id */
99 	gid_t		va_gid;		/* owner group id */
100 	long		va_fsid;	/* file system id (dev for now) */
101 	long		va_fileid;	/* file id */
102 	u_quad_t	va_size;	/* file size in bytes */
103 	long		va_blocksize;	/* blocksize preferred for i/o */
104 	struct timespec	va_atime;	/* time of last access */
105 	struct timespec	va_mtime;	/* time of last modification */
106 	struct timespec	va_ctime;	/* time file changed */
107 	u_long		va_gen;		/* generation number of file */
108 	u_long		va_flags;	/* flags defined for file */
109 	dev_t		va_rdev;	/* device the special file represents */
110 	u_quad_t	va_bytes;	/* bytes of disk space held by file */
111 	u_quad_t	va_filerev;	/* file modification number */
112 	u_int		va_vaflags;	/* operations flags, see below */
113 	long		va_spare;	/* remain quad aligned */
114 };
115 
116 /*
117  * Flags for va_cflags.
118  */
119 #define	VA_UTIMES_NULL	0x01		/* utimes argument was NULL */
120 
121 /*
122  * Flags for ioflag.
123  */
124 #define	IO_UNIT		0x01		/* do I/O as atomic unit */
125 #define	IO_APPEND	0x02		/* append write to end */
126 #define	IO_SYNC		0x04		/* do I/O synchronously */
127 #define	IO_NODELOCKED	0x08		/* underlying node already locked */
128 #define	IO_NDELAY	0x10		/* FNDELAY flag set in file table */
129 
130 /*
131  *  Modes.  Some values same as Ixxx entries from inode.h for now.
132  */
133 #define	VSUID	04000		/* set user id on execution */
134 #define	VSGID	02000		/* set group id on execution */
135 #define	VSVTX	01000		/* save swapped text even after use */
136 #define	VREAD	00400		/* read, write, execute permissions */
137 #define	VWRITE	00200
138 #define	VEXEC	00100
139 
140 /*
141  * Token indicating no attribute value yet assigned.
142  */
143 #define	VNOVAL	(-1)
144 
145 #ifdef KERNEL
146 /*
147  * Convert between vnode types and inode formats (since POSIX.1
148  * defines mode word of stat structure in terms of inode formats).
149  */
150 extern enum vtype	iftovt_tab[];
151 extern int		vttoif_tab[];
152 #define IFTOVT(mode)	(iftovt_tab[((mode) & S_IFMT) >> 12])
153 #define VTTOIF(indx)	(vttoif_tab[(int)(indx)])
154 #define MAKEIMODE(indx, mode)	(int)(VTTOIF(indx) | (mode))
155 
156 /*
157  * Flags to various vnode functions.
158  */
159 #define	SKIPSYSTEM	0x0001		/* vflush: skip vnodes marked VSYSTEM */
160 #define	FORCECLOSE	0x0002		/* vflush: force file closeure */
161 #define	WRITECLOSE	0x0004		/* vflush: only close writeable files */
162 #define	DOCLOSE		0x0008		/* vclean: close active files */
163 #define	V_SAVE		0x0001		/* vinvalbuf: sync file first */
164 #define	V_SAVEMETA	0x0002		/* vinvalbuf: leave indirect blocks */
165 
166 #ifdef DIAGNOSTIC
167 #define	HOLDRELE(vp)	holdrele(vp)
168 #define	VATTR_NULL(vap)	vattr_null(vap)
169 #define	VHOLD(vp)	vhold(vp)
170 #define	VREF(vp)	vref(vp)
171 
172 void	holdrele __P((struct vnode *));
173 void	vattr_null __P((struct vattr *));
174 void	vhold __P((struct vnode *));
175 void	vref __P((struct vnode *));
176 #else
177 #define	HOLDRELE(vp)	(vp)->v_holdcnt--	/* decrease buf or page ref */
178 #define	VATTR_NULL(vap)	(*(vap) = va_null)	/* initialize a vattr */
179 #define	VHOLD(vp)	(vp)->v_holdcnt++	/* increase buf or page ref */
180 #define	VREF(vp)	(vp)->v_usecount++	/* increase reference */
181 #endif
182 
183 #define	NULLVP	((struct vnode *)NULL)
184 
185 /*
186  * Global vnode data.
187  */
188 extern	struct vnode *rootvnode;	/* root (i.e. "/") vnode */
189 extern	int desiredvnodes;		/* number of vnodes desired */
190 extern	struct vattr va_null;		/* predefined null vattr structure */
191 
192 /*
193  * Macro/function to check for client cache inconsistency w.r.t. leasing.
194  */
195 #define	LEASE_READ	0x1		/* Check lease for readers */
196 #define	LEASE_WRITE	0x2		/* Check lease for modifiers */
197 
198 #ifdef NFS
199 void	lease_check __P((struct vnode *vp, struct proc *p,
200 	    struct ucred *ucred, int flag));
201 void	lease_updatetime __P((int deltat));
202 #define	LEASE_CHECK(vp, p, cred, flag)	lease_check((vp), (p), (cred), (flag))
203 #define	LEASE_UPDATETIME(dt)		lease_updatetime(dt)
204 #else
205 #define	LEASE_CHECK(vp, p, cred, flag)
206 #define	LEASE_UPDATETIME(dt)
207 #endif /* NFS */
208 #endif /* KERNEL */
209 
210 
211 /*
212  * Mods for exensibility.
213  */
214 
215 /*
216  * Flags for vdesc_flags:
217  */
218 #define VDESC_MAX_VPS		16
219 /* Low order 16 flag bits are reserved for willrele flags for vp arguments. */
220 #define VDESC_VP0_WILLRELE	0x0001
221 #define VDESC_VP1_WILLRELE	0x0002
222 #define VDESC_VP2_WILLRELE	0x0004
223 #define VDESC_VP3_WILLRELE	0x0008
224 #define VDESC_NOMAP_VPP		0x0100
225 #define VDESC_VPP_WILLRELE	0x0200
226 
227 /*
228  * VDESC_NO_OFFSET is used to identify the end of the offset list
229  * and in places where no such field exists.
230  */
231 #define VDESC_NO_OFFSET -1
232 
233 /*
234  * This structure describes the vnode operation taking place.
235  */
236 struct vnodeop_desc {
237 	int	vdesc_offset;		/* offset in vector--first for speed */
238 	char    *vdesc_name;		/* a readable name for debugging */
239 	int	vdesc_flags;		/* VDESC_* flags */
240 
241 	/*
242 	 * These ops are used by bypass routines to map and locate arguments.
243 	 * Creds and procs are not needed in bypass routines, but sometimes
244 	 * they are useful to (for example) transport layers.
245 	 * Nameidata is useful because it has a cred in it.
246 	 */
247 	int	*vdesc_vp_offsets;	/* list ended by VDESC_NO_OFFSET */
248 	int	vdesc_vpp_offset;	/* return vpp location */
249 	int	vdesc_cred_offset;	/* cred location, if any */
250 	int	vdesc_proc_offset;	/* proc location, if any */
251 	int	vdesc_componentname_offset; /* if any */
252 	/*
253 	 * Finally, we've got a list of private data (about each operation)
254 	 * for each transport layer.  (Support to manage this list is not
255 	 * yet part of BSD.)
256 	 */
257 	caddr_t	*vdesc_transports;
258 };
259 
260 #ifdef KERNEL
261 /*
262  * A list of all the operation descs.
263  */
264 extern struct vnodeop_desc *vnodeop_descs[];
265 
266 
267 /*
268  * This macro is very helpful in defining those offsets in the vdesc struct.
269  *
270  * This is stolen from X11R4.  I ingored all the fancy stuff for
271  * Crays, so if you decide to port this to such a serious machine,
272  * you might want to consult Intrisics.h's XtOffset{,Of,To}.
273  */
274 #define VOPARG_OFFSET(p_type,field) \
275         ((int) (((char *) (&(((p_type)NULL)->field))) - ((char *) NULL)))
276 #define VOPARG_OFFSETOF(s_type,field) \
277 	VOPARG_OFFSET(s_type*,field)
278 #define VOPARG_OFFSETTO(S_TYPE,S_OFFSET,STRUCT_P) \
279 	((S_TYPE)(((char*)(STRUCT_P))+(S_OFFSET)))
280 
281 
282 /*
283  * This structure is used to configure the new vnodeops vector.
284  */
285 struct vnodeopv_entry_desc {
286 	struct vnodeop_desc *opve_op;   /* which operation this is */
287 	int (*opve_impl)();		/* code implementing this operation */
288 };
289 struct vnodeopv_desc {
290 			/* ptr to the ptr to the vector where op should go */
291 	int (***opv_desc_vector_p)();
292 	struct vnodeopv_entry_desc *opv_desc_ops;   /* null terminated list */
293 };
294 
295 /*
296  * A default routine which just returns an error.
297  */
298 int vn_default_error __P((void));
299 
300 /*
301  * A generic structure.
302  * This can be used by bypass routines to identify generic arguments.
303  */
304 struct vop_generic_args {
305 	struct vnodeop_desc *a_desc;
306 	/* other random data follows, presumably */
307 };
308 
309 /*
310  * VOCALL calls an op given an ops vector.  We break it out because BSD's
311  * vclean changes the ops vector and then wants to call ops with the old
312  * vector.
313  */
314 #define VOCALL(OPSV,OFF,AP) (( *((OPSV)[(OFF)])) (AP))
315 
316 /*
317  * This call works for vnodes in the kernel.
318  */
319 #define VCALL(VP,OFF,AP) VOCALL((VP)->v_op,(OFF),(AP))
320 #define VDESC(OP) (& __CONCAT(OP,_desc))
321 #define VOFFSET(OP) (VDESC(OP)->vdesc_offset)
322 
323 /*
324  * Finally, include the default set of vnode operations.
325  */
326 #include <vnode_if.h>
327 
328 /*
329  * Public vnode manipulation functions.
330  */
331 struct file;
332 struct mount;
333 struct nameidata;
334 struct proc;
335 struct stat;
336 struct ucred;
337 struct uio;
338 struct vattr;
339 struct vnode;
340 struct vop_bwrite_args;
341 
342 int 	bdevvp __P((dev_t dev, struct vnode **vpp));
343 int 	getnewvnode __P((enum vtagtype tag,
344 	    struct mount *mp, int (**vops)(), struct vnode **vpp));
345 int	vinvalbuf __P((struct vnode *vp, int save, struct ucred *cred,
346 	    struct proc *p, int slpflag, int slptimeo));
347 void 	vattr_null __P((struct vattr *vap));
348 int 	vcount __P((struct vnode *vp));
349 int 	vget __P((struct vnode *vp, int lockflag));
350 void 	vgone __P((struct vnode *vp));
351 void 	vgoneall __P((struct vnode *vp));
352 int	vn_bwrite __P((struct vop_bwrite_args *ap));
353 int 	vn_close __P((struct vnode *vp,
354 	    int flags, struct ucred *cred, struct proc *p));
355 int 	vn_closefile __P((struct file *fp, struct proc *p));
356 int	vn_ioctl __P((struct file *fp, int com, caddr_t data, struct proc *p));
357 int 	vn_open __P((struct nameidata *ndp, int fmode, int cmode));
358 int 	vn_rdwr __P((enum uio_rw rw, struct vnode *vp, caddr_t base,
359 	    int len, off_t offset, enum uio_seg segflg, int ioflg,
360 	    struct ucred *cred, int *aresid, struct proc *p));
361 int	vn_read __P((struct file *fp, struct uio *uio, struct ucred *cred));
362 int	vn_select __P((struct file *fp, int which, struct proc *p));
363 int	vn_stat __P((struct vnode *vp, struct stat *sb, struct proc *p));
364 int	vn_write __P((struct file *fp, struct uio *uio, struct ucred *cred));
365 struct vnode *
366 	checkalias __P((struct vnode *vp, dev_t nvp_rdev, struct mount *mp));
367 void 	vput __P((struct vnode *vp));
368 void 	vref __P((struct vnode *vp));
369 void 	vrele __P((struct vnode *vp));
370 #endif /* KERNEL */
371