xref: /freebsd/sys/kern/vfs_vnops.c (revision f856af0466c076beef4ea9b15d088e1119a945b8)
1 /*-
2  * Copyright (c) 1982, 1986, 1989, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  * (c) UNIX System Laboratories, Inc.
5  * All or some portions of this file are derived from material licensed
6  * to the University of California by American Telephone and Telegraph
7  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8  * the permission of UNIX System Laboratories, Inc.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 4. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  *	@(#)vfs_vnops.c	8.2 (Berkeley) 1/21/94
35  */
36 
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
39 
40 #include "opt_mac.h"
41 
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/fcntl.h>
45 #include <sys/file.h>
46 #include <sys/kdb.h>
47 #include <sys/stat.h>
48 #include <sys/priv.h>
49 #include <sys/proc.h>
50 #include <sys/limits.h>
51 #include <sys/lock.h>
52 #include <sys/mount.h>
53 #include <sys/mutex.h>
54 #include <sys/namei.h>
55 #include <sys/vnode.h>
56 #include <sys/bio.h>
57 #include <sys/buf.h>
58 #include <sys/filio.h>
59 #include <sys/sx.h>
60 #include <sys/ttycom.h>
61 #include <sys/conf.h>
62 #include <sys/syslog.h>
63 #include <sys/unistd.h>
64 
65 #include <security/mac/mac_framework.h>
66 
67 static fo_rdwr_t	vn_read;
68 static fo_rdwr_t	vn_write;
69 static fo_ioctl_t	vn_ioctl;
70 static fo_poll_t	vn_poll;
71 static fo_kqfilter_t	vn_kqfilter;
72 static fo_stat_t	vn_statfile;
73 static fo_close_t	vn_closefile;
74 
75 struct 	fileops vnops = {
76 	.fo_read = vn_read,
77 	.fo_write = vn_write,
78 	.fo_ioctl = vn_ioctl,
79 	.fo_poll = vn_poll,
80 	.fo_kqfilter = vn_kqfilter,
81 	.fo_stat = vn_statfile,
82 	.fo_close = vn_closefile,
83 	.fo_flags = DFLAG_PASSABLE | DFLAG_SEEKABLE
84 };
85 
86 int
87 vn_open(ndp, flagp, cmode, fdidx)
88 	struct nameidata *ndp;
89 	int *flagp, cmode, fdidx;
90 {
91 	struct thread *td = ndp->ni_cnd.cn_thread;
92 
93 	return (vn_open_cred(ndp, flagp, cmode, td->td_ucred, fdidx));
94 }
95 
96 /*
97  * Common code for vnode open operations.
98  * Check permissions, and call the VOP_OPEN or VOP_CREATE routine.
99  *
100  * Note that this does NOT free nameidata for the successful case,
101  * due to the NDINIT being done elsewhere.
102  */
103 int
104 vn_open_cred(ndp, flagp, cmode, cred, fdidx)
105 	struct nameidata *ndp;
106 	int *flagp, cmode;
107 	struct ucred *cred;
108 	int fdidx;
109 {
110 	struct vnode *vp;
111 	struct mount *mp;
112 	struct thread *td = ndp->ni_cnd.cn_thread;
113 	struct vattr vat;
114 	struct vattr *vap = &vat;
115 	int mode, fmode, error;
116 	int vfslocked, mpsafe;
117 
118 	mpsafe = ndp->ni_cnd.cn_flags & MPSAFE;
119 restart:
120 	vfslocked = 0;
121 	fmode = *flagp;
122 	if (fmode & O_CREAT) {
123 		ndp->ni_cnd.cn_nameiop = CREATE;
124 		ndp->ni_cnd.cn_flags = ISOPEN | LOCKPARENT | LOCKLEAF |
125 		    MPSAFE | AUDITVNODE1;
126 		if ((fmode & O_EXCL) == 0 && (fmode & O_NOFOLLOW) == 0)
127 			ndp->ni_cnd.cn_flags |= FOLLOW;
128 		bwillwrite();
129 		if ((error = namei(ndp)) != 0)
130 			return (error);
131 		vfslocked = NDHASGIANT(ndp);
132 		if (!mpsafe)
133 			ndp->ni_cnd.cn_flags &= ~MPSAFE;
134 		if (ndp->ni_vp == NULL) {
135 			VATTR_NULL(vap);
136 			vap->va_type = VREG;
137 			vap->va_mode = cmode;
138 			if (fmode & O_EXCL)
139 				vap->va_vaflags |= VA_EXCLUSIVE;
140 			if (vn_start_write(ndp->ni_dvp, &mp, V_NOWAIT) != 0) {
141 				NDFREE(ndp, NDF_ONLY_PNBUF);
142 				vput(ndp->ni_dvp);
143 				VFS_UNLOCK_GIANT(vfslocked);
144 				if ((error = vn_start_write(NULL, &mp,
145 				    V_XSLEEP | PCATCH)) != 0)
146 					return (error);
147 				goto restart;
148 			}
149 #ifdef MAC
150 			error = mac_check_vnode_create(cred, ndp->ni_dvp,
151 			    &ndp->ni_cnd, vap);
152 			if (error == 0) {
153 #endif
154 				VOP_LEASE(ndp->ni_dvp, td, cred, LEASE_WRITE);
155 				error = VOP_CREATE(ndp->ni_dvp, &ndp->ni_vp,
156 						   &ndp->ni_cnd, vap);
157 #ifdef MAC
158 			}
159 #endif
160 			vput(ndp->ni_dvp);
161 			vn_finished_write(mp);
162 			if (error) {
163 				VFS_UNLOCK_GIANT(vfslocked);
164 				NDFREE(ndp, NDF_ONLY_PNBUF);
165 				return (error);
166 			}
167 			fmode &= ~O_TRUNC;
168 			vp = ndp->ni_vp;
169 		} else {
170 			if (ndp->ni_dvp == ndp->ni_vp)
171 				vrele(ndp->ni_dvp);
172 			else
173 				vput(ndp->ni_dvp);
174 			ndp->ni_dvp = NULL;
175 			vp = ndp->ni_vp;
176 			if (fmode & O_EXCL) {
177 				error = EEXIST;
178 				goto bad;
179 			}
180 			fmode &= ~O_CREAT;
181 		}
182 	} else {
183 		ndp->ni_cnd.cn_nameiop = LOOKUP;
184 		ndp->ni_cnd.cn_flags = ISOPEN |
185 		    ((fmode & O_NOFOLLOW) ? NOFOLLOW : FOLLOW) |
186 		    LOCKSHARED | LOCKLEAF | MPSAFE | AUDITVNODE1;
187 		if ((error = namei(ndp)) != 0)
188 			return (error);
189 		if (!mpsafe)
190 			ndp->ni_cnd.cn_flags &= ~MPSAFE;
191 		vfslocked = NDHASGIANT(ndp);
192 		vp = ndp->ni_vp;
193 	}
194 	if (vp->v_type == VLNK) {
195 		error = EMLINK;
196 		goto bad;
197 	}
198 	if (vp->v_type == VSOCK) {
199 		error = EOPNOTSUPP;
200 		goto bad;
201 	}
202 	mode = 0;
203 	if (fmode & (FWRITE | O_TRUNC)) {
204 		if (vp->v_type == VDIR) {
205 			error = EISDIR;
206 			goto bad;
207 		}
208 		mode |= VWRITE;
209 	}
210 	if (fmode & FREAD)
211 		mode |= VREAD;
212 	if (fmode & O_APPEND)
213 		mode |= VAPPEND;
214 #ifdef MAC
215 	error = mac_check_vnode_open(cred, vp, mode);
216 	if (error)
217 		goto bad;
218 #endif
219 	if ((fmode & O_CREAT) == 0) {
220 		if (mode & VWRITE) {
221 			error = vn_writechk(vp);
222 			if (error)
223 				goto bad;
224 		}
225 		if (mode) {
226 		        error = VOP_ACCESS(vp, mode, cred, td);
227 			if (error)
228 				goto bad;
229 		}
230 	}
231 	if ((error = VOP_OPEN(vp, fmode, cred, td, fdidx)) != 0)
232 		goto bad;
233 
234 	if (fmode & FWRITE)
235 		vp->v_writecount++;
236 	*flagp = fmode;
237 	ASSERT_VOP_LOCKED(vp, "vn_open_cred");
238 	if (!mpsafe)
239 		VFS_UNLOCK_GIANT(vfslocked);
240 	return (0);
241 bad:
242 	NDFREE(ndp, NDF_ONLY_PNBUF);
243 	vput(vp);
244 	VFS_UNLOCK_GIANT(vfslocked);
245 	*flagp = fmode;
246 	ndp->ni_vp = NULL;
247 	return (error);
248 }
249 
250 /*
251  * Check for write permissions on the specified vnode.
252  * Prototype text segments cannot be written.
253  */
254 int
255 vn_writechk(vp)
256 	register struct vnode *vp;
257 {
258 
259 	ASSERT_VOP_LOCKED(vp, "vn_writechk");
260 	/*
261 	 * If there's shared text associated with
262 	 * the vnode, try to free it up once.  If
263 	 * we fail, we can't allow writing.
264 	 */
265 	if (vp->v_vflag & VV_TEXT)
266 		return (ETXTBSY);
267 
268 	return (0);
269 }
270 
271 /*
272  * Vnode close call
273  */
274 int
275 vn_close(vp, flags, file_cred, td)
276 	register struct vnode *vp;
277 	int flags;
278 	struct ucred *file_cred;
279 	struct thread *td;
280 {
281 	struct mount *mp;
282 	int error;
283 
284 	VFS_ASSERT_GIANT(vp->v_mount);
285 
286 	vn_start_write(vp, &mp, V_WAIT);
287 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
288 	if (flags & FWRITE)
289 		vp->v_writecount--;
290 	error = VOP_CLOSE(vp, flags, file_cred, td);
291 	vput(vp);
292 	vn_finished_write(mp);
293 	return (error);
294 }
295 
296 /*
297  * Sequential heuristic - detect sequential operation
298  */
299 static __inline
300 int
301 sequential_heuristic(struct uio *uio, struct file *fp)
302 {
303 
304 	if ((uio->uio_offset == 0 && fp->f_seqcount > 0) ||
305 	    uio->uio_offset == fp->f_nextoff) {
306 		/*
307 		 * XXX we assume that the filesystem block size is
308 		 * the default.  Not true, but still gives us a pretty
309 		 * good indicator of how sequential the read operations
310 		 * are.
311 		 */
312 		fp->f_seqcount += (uio->uio_resid + BKVASIZE - 1) / BKVASIZE;
313 		if (fp->f_seqcount > IO_SEQMAX)
314 			fp->f_seqcount = IO_SEQMAX;
315 		return(fp->f_seqcount << IO_SEQSHIFT);
316 	}
317 
318 	/*
319 	 * Not sequential, quick draw-down of seqcount
320 	 */
321 	if (fp->f_seqcount > 1)
322 		fp->f_seqcount = 1;
323 	else
324 		fp->f_seqcount = 0;
325 	return(0);
326 }
327 
328 /*
329  * Package up an I/O request on a vnode into a uio and do it.
330  */
331 int
332 vn_rdwr(rw, vp, base, len, offset, segflg, ioflg, active_cred, file_cred,
333     aresid, td)
334 	enum uio_rw rw;
335 	struct vnode *vp;
336 	void *base;
337 	int len;
338 	off_t offset;
339 	enum uio_seg segflg;
340 	int ioflg;
341 	struct ucred *active_cred;
342 	struct ucred *file_cred;
343 	int *aresid;
344 	struct thread *td;
345 {
346 	struct uio auio;
347 	struct iovec aiov;
348 	struct mount *mp;
349 	struct ucred *cred;
350 	int error;
351 
352 	VFS_ASSERT_GIANT(vp->v_mount);
353 
354 	if ((ioflg & IO_NODELOCKED) == 0) {
355 		mp = NULL;
356 		if (rw == UIO_WRITE) {
357 			if (vp->v_type != VCHR &&
358 			    (error = vn_start_write(vp, &mp, V_WAIT | PCATCH))
359 			    != 0)
360 				return (error);
361 			vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
362 		} else {
363 			/*
364 			 * XXX This should be LK_SHARED but I don't trust VFS
365 			 * enough to leave it like that until it has been
366 			 * reviewed further.
367 			 */
368 			vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
369 		}
370 
371 	}
372 	ASSERT_VOP_LOCKED(vp, "IO_NODELOCKED with no vp lock held");
373 	auio.uio_iov = &aiov;
374 	auio.uio_iovcnt = 1;
375 	aiov.iov_base = base;
376 	aiov.iov_len = len;
377 	auio.uio_resid = len;
378 	auio.uio_offset = offset;
379 	auio.uio_segflg = segflg;
380 	auio.uio_rw = rw;
381 	auio.uio_td = td;
382 	error = 0;
383 #ifdef MAC
384 	if ((ioflg & IO_NOMACCHECK) == 0) {
385 		if (rw == UIO_READ)
386 			error = mac_check_vnode_read(active_cred, file_cred,
387 			    vp);
388 		else
389 			error = mac_check_vnode_write(active_cred, file_cred,
390 			    vp);
391 	}
392 #endif
393 	if (error == 0) {
394 		if (file_cred)
395 			cred = file_cred;
396 		else
397 			cred = active_cred;
398 		if (rw == UIO_READ)
399 			error = VOP_READ(vp, &auio, ioflg, cred);
400 		else
401 			error = VOP_WRITE(vp, &auio, ioflg, cred);
402 	}
403 	if (aresid)
404 		*aresid = auio.uio_resid;
405 	else
406 		if (auio.uio_resid && error == 0)
407 			error = EIO;
408 	if ((ioflg & IO_NODELOCKED) == 0) {
409 		if (rw == UIO_WRITE && vp->v_type != VCHR)
410 			vn_finished_write(mp);
411 		VOP_UNLOCK(vp, 0, td);
412 	}
413 	return (error);
414 }
415 
416 /*
417  * Package up an I/O request on a vnode into a uio and do it.  The I/O
418  * request is split up into smaller chunks and we try to avoid saturating
419  * the buffer cache while potentially holding a vnode locked, so we
420  * check bwillwrite() before calling vn_rdwr().  We also call uio_yield()
421  * to give other processes a chance to lock the vnode (either other processes
422  * core'ing the same binary, or unrelated processes scanning the directory).
423  */
424 int
425 vn_rdwr_inchunks(rw, vp, base, len, offset, segflg, ioflg, active_cred,
426     file_cred, aresid, td)
427 	enum uio_rw rw;
428 	struct vnode *vp;
429 	void *base;
430 	size_t len;
431 	off_t offset;
432 	enum uio_seg segflg;
433 	int ioflg;
434 	struct ucred *active_cred;
435 	struct ucred *file_cred;
436 	size_t *aresid;
437 	struct thread *td;
438 {
439 	int error = 0;
440 	int iaresid;
441 
442 	VFS_ASSERT_GIANT(vp->v_mount);
443 
444 	do {
445 		int chunk;
446 
447 		/*
448 		 * Force `offset' to a multiple of MAXBSIZE except possibly
449 		 * for the first chunk, so that filesystems only need to
450 		 * write full blocks except possibly for the first and last
451 		 * chunks.
452 		 */
453 		chunk = MAXBSIZE - (uoff_t)offset % MAXBSIZE;
454 
455 		if (chunk > len)
456 			chunk = len;
457 		if (rw != UIO_READ && vp->v_type == VREG)
458 			bwillwrite();
459 		iaresid = 0;
460 		error = vn_rdwr(rw, vp, base, chunk, offset, segflg,
461 		    ioflg, active_cred, file_cred, &iaresid, td);
462 		len -= chunk;	/* aresid calc already includes length */
463 		if (error)
464 			break;
465 		offset += chunk;
466 		base = (char *)base + chunk;
467 		uio_yield();
468 	} while (len);
469 	if (aresid)
470 		*aresid = len + iaresid;
471 	return (error);
472 }
473 
474 /*
475  * File table vnode read routine.
476  */
477 static int
478 vn_read(fp, uio, active_cred, flags, td)
479 	struct file *fp;
480 	struct uio *uio;
481 	struct ucred *active_cred;
482 	struct thread *td;
483 	int flags;
484 {
485 	struct vnode *vp;
486 	int error, ioflag;
487 	int vfslocked;
488 
489 	KASSERT(uio->uio_td == td, ("uio_td %p is not td %p",
490 	    uio->uio_td, td));
491 	vp = fp->f_vnode;
492 	ioflag = 0;
493 	if (fp->f_flag & FNONBLOCK)
494 		ioflag |= IO_NDELAY;
495 	if (fp->f_flag & O_DIRECT)
496 		ioflag |= IO_DIRECT;
497 	vfslocked = VFS_LOCK_GIANT(vp->v_mount);
498 	VOP_LEASE(vp, td, fp->f_cred, LEASE_READ);
499 	/*
500 	 * According to McKusick the vn lock was protecting f_offset here.
501 	 * It is now protected by the FOFFSET_LOCKED flag.
502 	 */
503 	if ((flags & FOF_OFFSET) == 0) {
504 		FILE_LOCK(fp);
505 		while(fp->f_vnread_flags & FOFFSET_LOCKED) {
506 			fp->f_vnread_flags |= FOFFSET_LOCK_WAITING;
507 			msleep(&fp->f_vnread_flags,fp->f_mtxp,PUSER -1,"vnread offlock",0);
508 		}
509 		fp->f_vnread_flags |= FOFFSET_LOCKED;
510 		FILE_UNLOCK(fp);
511 		vn_lock(vp, LK_SHARED | LK_RETRY, td);
512 		uio->uio_offset = fp->f_offset;
513 	} else
514 		vn_lock(vp, LK_SHARED | LK_RETRY, td);
515 
516 	ioflag |= sequential_heuristic(uio, fp);
517 
518 #ifdef MAC
519 	error = mac_check_vnode_read(active_cred, fp->f_cred, vp);
520 	if (error == 0)
521 #endif
522 		error = VOP_READ(vp, uio, ioflag, fp->f_cred);
523 	if ((flags & FOF_OFFSET) == 0) {
524 		fp->f_offset = uio->uio_offset;
525 		FILE_LOCK(fp);
526 		if (fp->f_vnread_flags & FOFFSET_LOCK_WAITING)
527 			wakeup(&fp->f_vnread_flags);
528 		fp->f_vnread_flags = 0;
529 		FILE_UNLOCK(fp);
530 	}
531 	fp->f_nextoff = uio->uio_offset;
532 	VOP_UNLOCK(vp, 0, td);
533 	VFS_UNLOCK_GIANT(vfslocked);
534 	return (error);
535 }
536 
537 /*
538  * File table vnode write routine.
539  */
540 static int
541 vn_write(fp, uio, active_cred, flags, td)
542 	struct file *fp;
543 	struct uio *uio;
544 	struct ucred *active_cred;
545 	struct thread *td;
546 	int flags;
547 {
548 	struct vnode *vp;
549 	struct mount *mp;
550 	int error, ioflag;
551 	int vfslocked;
552 
553 	KASSERT(uio->uio_td == td, ("uio_td %p is not td %p",
554 	    uio->uio_td, td));
555 	vp = fp->f_vnode;
556 	vfslocked = VFS_LOCK_GIANT(vp->v_mount);
557 	if (vp->v_type == VREG)
558 		bwillwrite();
559 	ioflag = IO_UNIT;
560 	if (vp->v_type == VREG && (fp->f_flag & O_APPEND))
561 		ioflag |= IO_APPEND;
562 	if (fp->f_flag & FNONBLOCK)
563 		ioflag |= IO_NDELAY;
564 	if (fp->f_flag & O_DIRECT)
565 		ioflag |= IO_DIRECT;
566 	if ((fp->f_flag & O_FSYNC) ||
567 	    (vp->v_mount && (vp->v_mount->mnt_flag & MNT_SYNCHRONOUS)))
568 		ioflag |= IO_SYNC;
569 	mp = NULL;
570 	if (vp->v_type != VCHR &&
571 	    (error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
572 		goto unlock;
573 	VOP_LEASE(vp, td, fp->f_cred, LEASE_WRITE);
574 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
575 	if ((flags & FOF_OFFSET) == 0)
576 		uio->uio_offset = fp->f_offset;
577 	ioflag |= sequential_heuristic(uio, fp);
578 #ifdef MAC
579 	error = mac_check_vnode_write(active_cred, fp->f_cred, vp);
580 	if (error == 0)
581 #endif
582 		error = VOP_WRITE(vp, uio, ioflag, fp->f_cred);
583 	if ((flags & FOF_OFFSET) == 0)
584 		fp->f_offset = uio->uio_offset;
585 	fp->f_nextoff = uio->uio_offset;
586 	VOP_UNLOCK(vp, 0, td);
587 	if (vp->v_type != VCHR)
588 		vn_finished_write(mp);
589 unlock:
590 	VFS_UNLOCK_GIANT(vfslocked);
591 	return (error);
592 }
593 
594 /*
595  * File table vnode stat routine.
596  */
597 static int
598 vn_statfile(fp, sb, active_cred, td)
599 	struct file *fp;
600 	struct stat *sb;
601 	struct ucred *active_cred;
602 	struct thread *td;
603 {
604 	struct vnode *vp = fp->f_vnode;
605 	int vfslocked;
606 	int error;
607 
608 	vfslocked = VFS_LOCK_GIANT(vp->v_mount);
609 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
610 	error = vn_stat(vp, sb, active_cred, fp->f_cred, td);
611 	VOP_UNLOCK(vp, 0, td);
612 	VFS_UNLOCK_GIANT(vfslocked);
613 
614 	return (error);
615 }
616 
617 /*
618  * Stat a vnode; implementation for the stat syscall
619  */
620 int
621 vn_stat(vp, sb, active_cred, file_cred, td)
622 	struct vnode *vp;
623 	register struct stat *sb;
624 	struct ucred *active_cred;
625 	struct ucred *file_cred;
626 	struct thread *td;
627 {
628 	struct vattr vattr;
629 	register struct vattr *vap;
630 	int error;
631 	u_short mode;
632 
633 #ifdef MAC
634 	error = mac_check_vnode_stat(active_cred, file_cred, vp);
635 	if (error)
636 		return (error);
637 #endif
638 
639 	vap = &vattr;
640 	error = VOP_GETATTR(vp, vap, active_cred, td);
641 	if (error)
642 		return (error);
643 
644 	/*
645 	 * Zero the spare stat fields
646 	 */
647 	bzero(sb, sizeof *sb);
648 
649 	/*
650 	 * Copy from vattr table
651 	 */
652 	if (vap->va_fsid != VNOVAL)
653 		sb->st_dev = vap->va_fsid;
654 	else
655 		sb->st_dev = vp->v_mount->mnt_stat.f_fsid.val[0];
656 	sb->st_ino = vap->va_fileid;
657 	mode = vap->va_mode;
658 	switch (vap->va_type) {
659 	case VREG:
660 		mode |= S_IFREG;
661 		break;
662 	case VDIR:
663 		mode |= S_IFDIR;
664 		break;
665 	case VBLK:
666 		mode |= S_IFBLK;
667 		break;
668 	case VCHR:
669 		mode |= S_IFCHR;
670 		break;
671 	case VLNK:
672 		mode |= S_IFLNK;
673 		/* This is a cosmetic change, symlinks do not have a mode. */
674 		if (vp->v_mount->mnt_flag & MNT_NOSYMFOLLOW)
675 			sb->st_mode &= ~ACCESSPERMS;	/* 0000 */
676 		else
677 			sb->st_mode |= ACCESSPERMS;	/* 0777 */
678 		break;
679 	case VSOCK:
680 		mode |= S_IFSOCK;
681 		break;
682 	case VFIFO:
683 		mode |= S_IFIFO;
684 		break;
685 	default:
686 		return (EBADF);
687 	};
688 	sb->st_mode = mode;
689 	sb->st_nlink = vap->va_nlink;
690 	sb->st_uid = vap->va_uid;
691 	sb->st_gid = vap->va_gid;
692 	sb->st_rdev = vap->va_rdev;
693 	if (vap->va_size > OFF_MAX)
694 		return (EOVERFLOW);
695 	sb->st_size = vap->va_size;
696 	sb->st_atimespec = vap->va_atime;
697 	sb->st_mtimespec = vap->va_mtime;
698 	sb->st_ctimespec = vap->va_ctime;
699 	sb->st_birthtimespec = vap->va_birthtime;
700 
701         /*
702 	 * According to www.opengroup.org, the meaning of st_blksize is
703 	 *   "a filesystem-specific preferred I/O block size for this
704 	 *    object.  In some filesystem types, this may vary from file
705 	 *    to file"
706 	 * Default to PAGE_SIZE after much discussion.
707 	 * XXX: min(PAGE_SIZE, vp->v_bufobj.bo_bsize) may be more correct.
708 	 */
709 
710 	sb->st_blksize = PAGE_SIZE;
711 
712 	sb->st_flags = vap->va_flags;
713 	if (priv_check(td, PRIV_VFS_GENERATION))
714 		sb->st_gen = 0;
715 	else
716 		sb->st_gen = vap->va_gen;
717 
718 #if (S_BLKSIZE == 512)
719 	/* Optimize this case */
720 	sb->st_blocks = vap->va_bytes >> 9;
721 #else
722 	sb->st_blocks = vap->va_bytes / S_BLKSIZE;
723 #endif
724 	return (0);
725 }
726 
727 /*
728  * File table vnode ioctl routine.
729  */
730 static int
731 vn_ioctl(fp, com, data, active_cred, td)
732 	struct file *fp;
733 	u_long com;
734 	void *data;
735 	struct ucred *active_cred;
736 	struct thread *td;
737 {
738 	struct vnode *vp = fp->f_vnode;
739 	struct vattr vattr;
740 	int vfslocked;
741 	int error;
742 
743 	vfslocked = VFS_LOCK_GIANT(vp->v_mount);
744 	error = ENOTTY;
745 	switch (vp->v_type) {
746 	case VREG:
747 	case VDIR:
748 		if (com == FIONREAD) {
749 			vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
750 			error = VOP_GETATTR(vp, &vattr, active_cred, td);
751 			VOP_UNLOCK(vp, 0, td);
752 			if (!error)
753 				*(int *)data = vattr.va_size - fp->f_offset;
754 		}
755 		if (com == FIONBIO || com == FIOASYNC)	/* XXX */
756 			error = 0;
757 		else
758 			error = VOP_IOCTL(vp, com, data, fp->f_flag,
759 			    active_cred, td);
760 		break;
761 
762 	default:
763 		break;
764 	}
765 	VFS_UNLOCK_GIANT(vfslocked);
766 	return (error);
767 }
768 
769 /*
770  * File table vnode poll routine.
771  */
772 static int
773 vn_poll(fp, events, active_cred, td)
774 	struct file *fp;
775 	int events;
776 	struct ucred *active_cred;
777 	struct thread *td;
778 {
779 	struct vnode *vp;
780 	int vfslocked;
781 	int error;
782 
783 	vp = fp->f_vnode;
784 	vfslocked = VFS_LOCK_GIANT(vp->v_mount);
785 #ifdef MAC
786 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
787 	error = mac_check_vnode_poll(active_cred, fp->f_cred, vp);
788 	VOP_UNLOCK(vp, 0, td);
789 	if (!error)
790 #endif
791 
792 	error = VOP_POLL(vp, events, fp->f_cred, td);
793 	VFS_UNLOCK_GIANT(vfslocked);
794 	return (error);
795 }
796 
797 /*
798  * Check that the vnode is still valid, and if so
799  * acquire requested lock.
800  */
801 int
802 _vn_lock(struct vnode *vp, int flags, struct thread *td, char *file, int line)
803 {
804 	int error;
805 
806 	do {
807 		if ((flags & LK_INTERLOCK) == 0)
808 			VI_LOCK(vp);
809 		if ((flags & LK_NOWAIT || (flags & LK_TYPE_MASK) == 0) &&
810 		    vp->v_iflag & VI_DOOMED) {
811 			VI_UNLOCK(vp);
812 			return (ENOENT);
813 		}
814 		/*
815 		 * Just polling to check validity.
816 		 */
817 		if ((flags & LK_TYPE_MASK) == 0) {
818 			VI_UNLOCK(vp);
819 			return (0);
820 		}
821 		/*
822 		 * lockmgr drops interlock before it will return for
823 		 * any reason.  So force the code above to relock it.
824 		 */
825 		error = _VOP_LOCK(vp, flags | LK_INTERLOCK, td, file, line);
826 		flags &= ~LK_INTERLOCK;
827 		KASSERT((flags & LK_RETRY) == 0 || error == 0,
828 		    ("LK_RETRY set with incompatible flags %d\n", flags));
829 		/*
830 		 * Callers specify LK_RETRY if they wish to get dead vnodes.
831 		 * If RETRY is not set, we return ENOENT instead.
832 		 */
833 		if (error == 0 && vp->v_iflag & VI_DOOMED &&
834 		    (flags & LK_RETRY) == 0) {
835 			VOP_UNLOCK(vp, 0, td);
836 			error = ENOENT;
837 			break;
838 		}
839 	} while (flags & LK_RETRY && error != 0);
840 	return (error);
841 }
842 
843 /*
844  * File table vnode close routine.
845  */
846 static int
847 vn_closefile(fp, td)
848 	struct file *fp;
849 	struct thread *td;
850 {
851 	struct vnode *vp;
852 	struct flock lf;
853 	int vfslocked;
854 	int error;
855 
856 	vp = fp->f_vnode;
857 
858 	vfslocked = VFS_LOCK_GIANT(vp->v_mount);
859 	if (fp->f_type == DTYPE_VNODE && fp->f_flag & FHASLOCK) {
860 		lf.l_whence = SEEK_SET;
861 		lf.l_start = 0;
862 		lf.l_len = 0;
863 		lf.l_type = F_UNLCK;
864 		(void) VOP_ADVLOCK(vp, fp, F_UNLCK, &lf, F_FLOCK);
865 	}
866 
867 	fp->f_ops = &badfileops;
868 
869 	error = vn_close(vp, fp->f_flag, fp->f_cred, td);
870 	VFS_UNLOCK_GIANT(vfslocked);
871 	return (error);
872 }
873 
874 /*
875  * Preparing to start a filesystem write operation. If the operation is
876  * permitted, then we bump the count of operations in progress and
877  * proceed. If a suspend request is in progress, we wait until the
878  * suspension is over, and then proceed.
879  */
880 int
881 vn_start_write(vp, mpp, flags)
882 	struct vnode *vp;
883 	struct mount **mpp;
884 	int flags;
885 {
886 	struct mount *mp;
887 	int error;
888 
889 	error = 0;
890 	/*
891 	 * If a vnode is provided, get and return the mount point that
892 	 * to which it will write.
893 	 */
894 	if (vp != NULL) {
895 		if ((error = VOP_GETWRITEMOUNT(vp, mpp)) != 0) {
896 			*mpp = NULL;
897 			if (error != EOPNOTSUPP)
898 				return (error);
899 			return (0);
900 		}
901 	}
902 	if ((mp = *mpp) == NULL)
903 		return (0);
904 	MNT_ILOCK(mp);
905 	if (vp == NULL)
906 		MNT_REF(mp);
907 	/*
908 	 * Check on status of suspension.
909 	 */
910 	while ((mp->mnt_kern_flag & MNTK_SUSPEND) != 0) {
911 		if (flags & V_NOWAIT) {
912 			error = EWOULDBLOCK;
913 			goto unlock;
914 		}
915 		error = msleep(&mp->mnt_flag, MNT_MTX(mp),
916 		    (PUSER - 1) | (flags & PCATCH), "suspfs", 0);
917 		if (error)
918 			goto unlock;
919 	}
920 	if (flags & V_XSLEEP)
921 		goto unlock;
922 	mp->mnt_writeopcount++;
923 unlock:
924 	MNT_REL(mp);
925 	MNT_IUNLOCK(mp);
926 	return (error);
927 }
928 
929 /*
930  * Secondary suspension. Used by operations such as vop_inactive
931  * routines that are needed by the higher level functions. These
932  * are allowed to proceed until all the higher level functions have
933  * completed (indicated by mnt_writeopcount dropping to zero). At that
934  * time, these operations are halted until the suspension is over.
935  */
936 int
937 vn_write_suspend_wait(vp, mp, flags)
938 	struct vnode *vp;
939 	struct mount *mp;
940 	int flags;
941 {
942 	int error;
943 
944 	if (vp != NULL) {
945 		if ((error = VOP_GETWRITEMOUNT(vp, &mp)) != 0) {
946 			if (error != EOPNOTSUPP)
947 				return (error);
948 			return (0);
949 		}
950 	}
951 	/*
952 	 * If we are not suspended or have not yet reached suspended
953 	 * mode, then let the operation proceed.
954 	 */
955 	if (mp == NULL)
956 		return (0);
957 	MNT_ILOCK(mp);
958 	if (vp == NULL)
959 		MNT_REF(mp);
960 	if ((mp->mnt_kern_flag & MNTK_SUSPENDED) == 0) {
961 		MNT_REL(mp);
962 		MNT_IUNLOCK(mp);
963 		return (0);
964 	}
965 	if (flags & V_NOWAIT) {
966 		MNT_REL(mp);
967 		MNT_IUNLOCK(mp);
968 		return (EWOULDBLOCK);
969 	}
970 	/*
971 	 * Wait for the suspension to finish.
972 	 */
973 	error = msleep(&mp->mnt_flag, MNT_MTX(mp),
974 	    (PUSER - 1) | (flags & PCATCH) | PDROP, "suspfs", 0);
975 	vfs_rel(mp);
976 	return (error);
977 }
978 
979 /*
980  * Secondary suspension. Used by operations such as vop_inactive
981  * routines that are needed by the higher level functions. These
982  * are allowed to proceed until all the higher level functions have
983  * completed (indicated by mnt_writeopcount dropping to zero). At that
984  * time, these operations are halted until the suspension is over.
985  */
986 int
987 vn_start_secondary_write(vp, mpp, flags)
988 	struct vnode *vp;
989 	struct mount **mpp;
990 	int flags;
991 {
992 	struct mount *mp;
993 	int error;
994 
995  retry:
996 	if (vp != NULL) {
997 		if ((error = VOP_GETWRITEMOUNT(vp, mpp)) != 0) {
998 			*mpp = NULL;
999 			if (error != EOPNOTSUPP)
1000 				return (error);
1001 			return (0);
1002 		}
1003 	}
1004 	/*
1005 	 * If we are not suspended or have not yet reached suspended
1006 	 * mode, then let the operation proceed.
1007 	 */
1008 	if ((mp = *mpp) == NULL)
1009 		return (0);
1010 	MNT_ILOCK(mp);
1011 	if (vp == NULL)
1012 		MNT_REF(mp);
1013 	if ((mp->mnt_kern_flag & (MNTK_SUSPENDED | MNTK_SUSPEND2)) == 0) {
1014 		mp->mnt_secondary_writes++;
1015 		mp->mnt_secondary_accwrites++;
1016 		MNT_REL(mp);
1017 		MNT_IUNLOCK(mp);
1018 		return (0);
1019 	}
1020 	if (flags & V_NOWAIT) {
1021 		MNT_REL(mp);
1022 		MNT_IUNLOCK(mp);
1023 		return (EWOULDBLOCK);
1024 	}
1025 	/*
1026 	 * Wait for the suspension to finish.
1027 	 */
1028 	error = msleep(&mp->mnt_flag, MNT_MTX(mp),
1029 		       (PUSER - 1) | (flags & PCATCH) | PDROP, "suspfs", 0);
1030 	vfs_rel(mp);
1031 	if (error == 0)
1032 		goto retry;
1033 	return (error);
1034 }
1035 
1036 /*
1037  * Filesystem write operation has completed. If we are suspending and this
1038  * operation is the last one, notify the suspender that the suspension is
1039  * now in effect.
1040  */
1041 void
1042 vn_finished_write(mp)
1043 	struct mount *mp;
1044 {
1045 	if (mp == NULL)
1046 		return;
1047 	MNT_ILOCK(mp);
1048 	mp->mnt_writeopcount--;
1049 	if (mp->mnt_writeopcount < 0)
1050 		panic("vn_finished_write: neg cnt");
1051 	if ((mp->mnt_kern_flag & MNTK_SUSPEND) != 0 &&
1052 	    mp->mnt_writeopcount <= 0)
1053 		wakeup(&mp->mnt_writeopcount);
1054 	MNT_IUNLOCK(mp);
1055 }
1056 
1057 
1058 /*
1059  * Filesystem secondary write operation has completed. If we are
1060  * suspending and this operation is the last one, notify the suspender
1061  * that the suspension is now in effect.
1062  */
1063 void
1064 vn_finished_secondary_write(mp)
1065 	struct mount *mp;
1066 {
1067 	if (mp == NULL)
1068 		return;
1069 	MNT_ILOCK(mp);
1070 	mp->mnt_secondary_writes--;
1071 	if (mp->mnt_secondary_writes < 0)
1072 		panic("vn_finished_secondary_write: neg cnt");
1073 	if ((mp->mnt_kern_flag & MNTK_SUSPEND) != 0 &&
1074 	    mp->mnt_secondary_writes <= 0)
1075 		wakeup(&mp->mnt_secondary_writes);
1076 	MNT_IUNLOCK(mp);
1077 }
1078 
1079 
1080 
1081 /*
1082  * Request a filesystem to suspend write operations.
1083  */
1084 int
1085 vfs_write_suspend(mp)
1086 	struct mount *mp;
1087 {
1088 	struct thread *td = curthread;
1089 	int error;
1090 
1091 	MNT_ILOCK(mp);
1092 	if (mp->mnt_kern_flag & MNTK_SUSPEND) {
1093 		MNT_IUNLOCK(mp);
1094 		return (0);
1095 	}
1096 	mp->mnt_kern_flag |= MNTK_SUSPEND;
1097 	if (mp->mnt_writeopcount > 0)
1098 		(void) msleep(&mp->mnt_writeopcount,
1099 		    MNT_MTX(mp), (PUSER - 1)|PDROP, "suspwt", 0);
1100 	else
1101 		MNT_IUNLOCK(mp);
1102 	if ((error = VFS_SYNC(mp, MNT_SUSPEND, td)) != 0)
1103 		vfs_write_resume(mp);
1104 	return (error);
1105 }
1106 
1107 /*
1108  * Request a filesystem to resume write operations.
1109  */
1110 void
1111 vfs_write_resume(mp)
1112 	struct mount *mp;
1113 {
1114 
1115 	MNT_ILOCK(mp);
1116 	if ((mp->mnt_kern_flag & MNTK_SUSPEND) != 0) {
1117 		mp->mnt_kern_flag &= ~(MNTK_SUSPEND | MNTK_SUSPEND2 |
1118 				       MNTK_SUSPENDED);
1119 		wakeup(&mp->mnt_writeopcount);
1120 		wakeup(&mp->mnt_flag);
1121 	}
1122 	MNT_IUNLOCK(mp);
1123 }
1124 
1125 /*
1126  * Implement kqueues for files by translating it to vnode operation.
1127  */
1128 static int
1129 vn_kqfilter(struct file *fp, struct knote *kn)
1130 {
1131 	int vfslocked;
1132 	int error;
1133 
1134 	vfslocked = VFS_LOCK_GIANT(fp->f_vnode->v_mount);
1135 	error = VOP_KQFILTER(fp->f_vnode, kn);
1136 	VFS_UNLOCK_GIANT(vfslocked);
1137 
1138 	return error;
1139 }
1140 
1141 /*
1142  * Simplified in-kernel wrapper calls for extended attribute access.
1143  * Both calls pass in a NULL credential, authorizing as "kernel" access.
1144  * Set IO_NODELOCKED in ioflg if the vnode is already locked.
1145  */
1146 int
1147 vn_extattr_get(struct vnode *vp, int ioflg, int attrnamespace,
1148     const char *attrname, int *buflen, char *buf, struct thread *td)
1149 {
1150 	struct uio	auio;
1151 	struct iovec	iov;
1152 	int	error;
1153 
1154 	iov.iov_len = *buflen;
1155 	iov.iov_base = buf;
1156 
1157 	auio.uio_iov = &iov;
1158 	auio.uio_iovcnt = 1;
1159 	auio.uio_rw = UIO_READ;
1160 	auio.uio_segflg = UIO_SYSSPACE;
1161 	auio.uio_td = td;
1162 	auio.uio_offset = 0;
1163 	auio.uio_resid = *buflen;
1164 
1165 	if ((ioflg & IO_NODELOCKED) == 0)
1166 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
1167 
1168 	ASSERT_VOP_LOCKED(vp, "IO_NODELOCKED with no vp lock held");
1169 
1170 	/* authorize attribute retrieval as kernel */
1171 	error = VOP_GETEXTATTR(vp, attrnamespace, attrname, &auio, NULL, NULL,
1172 	    td);
1173 
1174 	if ((ioflg & IO_NODELOCKED) == 0)
1175 		VOP_UNLOCK(vp, 0, td);
1176 
1177 	if (error == 0) {
1178 		*buflen = *buflen - auio.uio_resid;
1179 	}
1180 
1181 	return (error);
1182 }
1183 
1184 /*
1185  * XXX failure mode if partially written?
1186  */
1187 int
1188 vn_extattr_set(struct vnode *vp, int ioflg, int attrnamespace,
1189     const char *attrname, int buflen, char *buf, struct thread *td)
1190 {
1191 	struct uio	auio;
1192 	struct iovec	iov;
1193 	struct mount	*mp;
1194 	int	error;
1195 
1196 	iov.iov_len = buflen;
1197 	iov.iov_base = buf;
1198 
1199 	auio.uio_iov = &iov;
1200 	auio.uio_iovcnt = 1;
1201 	auio.uio_rw = UIO_WRITE;
1202 	auio.uio_segflg = UIO_SYSSPACE;
1203 	auio.uio_td = td;
1204 	auio.uio_offset = 0;
1205 	auio.uio_resid = buflen;
1206 
1207 	if ((ioflg & IO_NODELOCKED) == 0) {
1208 		if ((error = vn_start_write(vp, &mp, V_WAIT)) != 0)
1209 			return (error);
1210 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
1211 	}
1212 
1213 	ASSERT_VOP_LOCKED(vp, "IO_NODELOCKED with no vp lock held");
1214 
1215 	/* authorize attribute setting as kernel */
1216 	error = VOP_SETEXTATTR(vp, attrnamespace, attrname, &auio, NULL, td);
1217 
1218 	if ((ioflg & IO_NODELOCKED) == 0) {
1219 		vn_finished_write(mp);
1220 		VOP_UNLOCK(vp, 0, td);
1221 	}
1222 
1223 	return (error);
1224 }
1225 
1226 int
1227 vn_extattr_rm(struct vnode *vp, int ioflg, int attrnamespace,
1228     const char *attrname, struct thread *td)
1229 {
1230 	struct mount	*mp;
1231 	int	error;
1232 
1233 	if ((ioflg & IO_NODELOCKED) == 0) {
1234 		if ((error = vn_start_write(vp, &mp, V_WAIT)) != 0)
1235 			return (error);
1236 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
1237 	}
1238 
1239 	ASSERT_VOP_LOCKED(vp, "IO_NODELOCKED with no vp lock held");
1240 
1241 	/* authorize attribute removal as kernel */
1242 	error = VOP_DELETEEXTATTR(vp, attrnamespace, attrname, NULL, td);
1243 	if (error == EOPNOTSUPP)
1244 		error = VOP_SETEXTATTR(vp, attrnamespace, attrname, NULL,
1245 		    NULL, td);
1246 
1247 	if ((ioflg & IO_NODELOCKED) == 0) {
1248 		vn_finished_write(mp);
1249 		VOP_UNLOCK(vp, 0, td);
1250 	}
1251 
1252 	return (error);
1253 }
1254