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