xref: /freebsd/sys/kern/vfs_vnops.c (revision f1f890804985a1043da42a5def13c79dc005f5e9)
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 <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/fcntl.h>
43 #include <sys/file.h>
44 #include <sys/kdb.h>
45 #include <sys/stat.h>
46 #include <sys/priv.h>
47 #include <sys/proc.h>
48 #include <sys/limits.h>
49 #include <sys/lock.h>
50 #include <sys/mount.h>
51 #include <sys/mutex.h>
52 #include <sys/namei.h>
53 #include <sys/vnode.h>
54 #include <sys/bio.h>
55 #include <sys/buf.h>
56 #include <sys/filio.h>
57 #include <sys/resourcevar.h>
58 #include <sys/rwlock.h>
59 #include <sys/sx.h>
60 #include <sys/sysctl.h>
61 #include <sys/ttycom.h>
62 #include <sys/conf.h>
63 #include <sys/syslog.h>
64 #include <sys/unistd.h>
65 
66 #include <security/audit/audit.h>
67 #include <security/mac/mac_framework.h>
68 
69 #include <vm/vm.h>
70 #include <vm/vm_extern.h>
71 #include <vm/pmap.h>
72 #include <vm/vm_map.h>
73 #include <vm/vm_object.h>
74 #include <vm/vm_page.h>
75 
76 static fo_rdwr_t	vn_read;
77 static fo_rdwr_t	vn_write;
78 static fo_rdwr_t	vn_io_fault;
79 static fo_truncate_t	vn_truncate;
80 static fo_ioctl_t	vn_ioctl;
81 static fo_poll_t	vn_poll;
82 static fo_kqfilter_t	vn_kqfilter;
83 static fo_stat_t	vn_statfile;
84 static fo_close_t	vn_closefile;
85 
86 struct 	fileops vnops = {
87 	.fo_read = vn_io_fault,
88 	.fo_write = vn_io_fault,
89 	.fo_truncate = vn_truncate,
90 	.fo_ioctl = vn_ioctl,
91 	.fo_poll = vn_poll,
92 	.fo_kqfilter = vn_kqfilter,
93 	.fo_stat = vn_statfile,
94 	.fo_close = vn_closefile,
95 	.fo_chmod = vn_chmod,
96 	.fo_chown = vn_chown,
97 	.fo_flags = DFLAG_PASSABLE | DFLAG_SEEKABLE
98 };
99 
100 int
101 vn_open(ndp, flagp, cmode, fp)
102 	struct nameidata *ndp;
103 	int *flagp, cmode;
104 	struct file *fp;
105 {
106 	struct thread *td = ndp->ni_cnd.cn_thread;
107 
108 	return (vn_open_cred(ndp, flagp, cmode, 0, td->td_ucred, fp));
109 }
110 
111 /*
112  * Common code for vnode open operations via a name lookup.
113  * Lookup the vnode and invoke VOP_CREATE if needed.
114  * Check permissions, and call the VOP_OPEN or VOP_CREATE routine.
115  *
116  * Note that this does NOT free nameidata for the successful case,
117  * due to the NDINIT being done elsewhere.
118  */
119 int
120 vn_open_cred(struct nameidata *ndp, int *flagp, int cmode, u_int vn_open_flags,
121     struct ucred *cred, struct file *fp)
122 {
123 	struct vnode *vp;
124 	struct mount *mp;
125 	struct thread *td = ndp->ni_cnd.cn_thread;
126 	struct vattr vat;
127 	struct vattr *vap = &vat;
128 	int fmode, error;
129 
130 restart:
131 	fmode = *flagp;
132 	if (fmode & O_CREAT) {
133 		ndp->ni_cnd.cn_nameiop = CREATE;
134 		ndp->ni_cnd.cn_flags = ISOPEN | LOCKPARENT | LOCKLEAF;
135 		if ((fmode & O_EXCL) == 0 && (fmode & O_NOFOLLOW) == 0)
136 			ndp->ni_cnd.cn_flags |= FOLLOW;
137 		if (!(vn_open_flags & VN_OPEN_NOAUDIT))
138 			ndp->ni_cnd.cn_flags |= AUDITVNODE1;
139 		if (vn_open_flags & VN_OPEN_NOCAPCHECK)
140 			ndp->ni_cnd.cn_flags |= NOCAPCHECK;
141 		bwillwrite();
142 		if ((error = namei(ndp)) != 0)
143 			return (error);
144 		if (ndp->ni_vp == NULL) {
145 			VATTR_NULL(vap);
146 			vap->va_type = VREG;
147 			vap->va_mode = cmode;
148 			if (fmode & O_EXCL)
149 				vap->va_vaflags |= VA_EXCLUSIVE;
150 			if (vn_start_write(ndp->ni_dvp, &mp, V_NOWAIT) != 0) {
151 				NDFREE(ndp, NDF_ONLY_PNBUF);
152 				vput(ndp->ni_dvp);
153 				if ((error = vn_start_write(NULL, &mp,
154 				    V_XSLEEP | PCATCH)) != 0)
155 					return (error);
156 				goto restart;
157 			}
158 #ifdef MAC
159 			error = mac_vnode_check_create(cred, ndp->ni_dvp,
160 			    &ndp->ni_cnd, vap);
161 			if (error == 0)
162 #endif
163 				error = VOP_CREATE(ndp->ni_dvp, &ndp->ni_vp,
164 						   &ndp->ni_cnd, vap);
165 			vput(ndp->ni_dvp);
166 			vn_finished_write(mp);
167 			if (error) {
168 				NDFREE(ndp, NDF_ONLY_PNBUF);
169 				return (error);
170 			}
171 			fmode &= ~O_TRUNC;
172 			vp = ndp->ni_vp;
173 		} else {
174 			if (ndp->ni_dvp == ndp->ni_vp)
175 				vrele(ndp->ni_dvp);
176 			else
177 				vput(ndp->ni_dvp);
178 			ndp->ni_dvp = NULL;
179 			vp = ndp->ni_vp;
180 			if (fmode & O_EXCL) {
181 				error = EEXIST;
182 				goto bad;
183 			}
184 			fmode &= ~O_CREAT;
185 		}
186 	} else {
187 		ndp->ni_cnd.cn_nameiop = LOOKUP;
188 		ndp->ni_cnd.cn_flags = ISOPEN |
189 		    ((fmode & O_NOFOLLOW) ? NOFOLLOW : FOLLOW) | LOCKLEAF;
190 		if (!(fmode & FWRITE))
191 			ndp->ni_cnd.cn_flags |= LOCKSHARED;
192 		if (!(vn_open_flags & VN_OPEN_NOAUDIT))
193 			ndp->ni_cnd.cn_flags |= AUDITVNODE1;
194 		if (vn_open_flags & VN_OPEN_NOCAPCHECK)
195 			ndp->ni_cnd.cn_flags |= NOCAPCHECK;
196 		if ((error = namei(ndp)) != 0)
197 			return (error);
198 		vp = ndp->ni_vp;
199 	}
200 	error = vn_open_vnode(vp, fmode, cred, td, fp);
201 	if (error)
202 		goto bad;
203 	*flagp = fmode;
204 	return (0);
205 bad:
206 	NDFREE(ndp, NDF_ONLY_PNBUF);
207 	vput(vp);
208 	*flagp = fmode;
209 	ndp->ni_vp = NULL;
210 	return (error);
211 }
212 
213 /*
214  * Common code for vnode open operations once a vnode is located.
215  * Check permissions, and call the VOP_OPEN routine.
216  */
217 int
218 vn_open_vnode(struct vnode *vp, int fmode, struct ucred *cred,
219     struct thread *td, struct file *fp)
220 {
221 	struct mount *mp;
222 	accmode_t accmode;
223 	struct flock lf;
224 	int error, have_flock, lock_flags, type;
225 
226 	if (vp->v_type == VLNK)
227 		return (EMLINK);
228 	if (vp->v_type == VSOCK)
229 		return (EOPNOTSUPP);
230 	if (vp->v_type != VDIR && fmode & O_DIRECTORY)
231 		return (ENOTDIR);
232 	accmode = 0;
233 	if (fmode & (FWRITE | O_TRUNC)) {
234 		if (vp->v_type == VDIR)
235 			return (EISDIR);
236 		accmode |= VWRITE;
237 	}
238 	if (fmode & FREAD)
239 		accmode |= VREAD;
240 	if (fmode & FEXEC)
241 		accmode |= VEXEC;
242 	if ((fmode & O_APPEND) && (fmode & FWRITE))
243 		accmode |= VAPPEND;
244 #ifdef MAC
245 	error = mac_vnode_check_open(cred, vp, accmode);
246 	if (error)
247 		return (error);
248 #endif
249 	if ((fmode & O_CREAT) == 0) {
250 		if (accmode & VWRITE) {
251 			error = vn_writechk(vp);
252 			if (error)
253 				return (error);
254 		}
255 		if (accmode) {
256 		        error = VOP_ACCESS(vp, accmode, cred, td);
257 			if (error)
258 				return (error);
259 		}
260 	}
261 	if ((error = VOP_OPEN(vp, fmode, cred, td, fp)) != 0)
262 		return (error);
263 
264 	if (fmode & (O_EXLOCK | O_SHLOCK)) {
265 		KASSERT(fp != NULL, ("open with flock requires fp"));
266 		lock_flags = VOP_ISLOCKED(vp);
267 		VOP_UNLOCK(vp, 0);
268 		lf.l_whence = SEEK_SET;
269 		lf.l_start = 0;
270 		lf.l_len = 0;
271 		if (fmode & O_EXLOCK)
272 			lf.l_type = F_WRLCK;
273 		else
274 			lf.l_type = F_RDLCK;
275 		type = F_FLOCK;
276 		if ((fmode & FNONBLOCK) == 0)
277 			type |= F_WAIT;
278 		error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type);
279 		have_flock = (error == 0);
280 		vn_lock(vp, lock_flags | LK_RETRY);
281 		if (error == 0 && vp->v_iflag & VI_DOOMED)
282 			error = ENOENT;
283 		/*
284 		 * Another thread might have used this vnode as an
285 		 * executable while the vnode lock was dropped.
286 		 * Ensure the vnode is still able to be opened for
287 		 * writing after the lock has been obtained.
288 		 */
289 		if (error == 0 && accmode & VWRITE)
290 			error = vn_writechk(vp);
291 		if (error) {
292 			VOP_UNLOCK(vp, 0);
293 			if (have_flock) {
294 				lf.l_whence = SEEK_SET;
295 				lf.l_start = 0;
296 				lf.l_len = 0;
297 				lf.l_type = F_UNLCK;
298 				(void) VOP_ADVLOCK(vp, fp, F_UNLCK, &lf,
299 				    F_FLOCK);
300 			}
301 			vn_start_write(vp, &mp, V_WAIT);
302 			vn_lock(vp, lock_flags | LK_RETRY);
303 			(void)VOP_CLOSE(vp, fmode, cred, td);
304 			vn_finished_write(mp);
305 			return (error);
306 		}
307 		fp->f_flag |= FHASLOCK;
308 	}
309 	if (fmode & FWRITE) {
310 		VOP_ADD_WRITECOUNT(vp, 1);
311 		CTR3(KTR_VFS, "%s: vp %p v_writecount increased to %d",
312 		    __func__, vp, vp->v_writecount);
313 	}
314 	ASSERT_VOP_LOCKED(vp, "vn_open_vnode");
315 	return (0);
316 }
317 
318 /*
319  * Check for write permissions on the specified vnode.
320  * Prototype text segments cannot be written.
321  */
322 int
323 vn_writechk(vp)
324 	register struct vnode *vp;
325 {
326 
327 	ASSERT_VOP_LOCKED(vp, "vn_writechk");
328 	/*
329 	 * If there's shared text associated with
330 	 * the vnode, try to free it up once.  If
331 	 * we fail, we can't allow writing.
332 	 */
333 	if (VOP_IS_TEXT(vp))
334 		return (ETXTBSY);
335 
336 	return (0);
337 }
338 
339 /*
340  * Vnode close call
341  */
342 int
343 vn_close(vp, flags, file_cred, td)
344 	register struct vnode *vp;
345 	int flags;
346 	struct ucred *file_cred;
347 	struct thread *td;
348 {
349 	struct mount *mp;
350 	int error, lock_flags;
351 
352 	if (!(flags & FWRITE) && vp->v_mount != NULL &&
353 	    vp->v_mount->mnt_kern_flag & MNTK_EXTENDED_SHARED)
354 		lock_flags = LK_SHARED;
355 	else
356 		lock_flags = LK_EXCLUSIVE;
357 
358 	vn_start_write(vp, &mp, V_WAIT);
359 	vn_lock(vp, lock_flags | LK_RETRY);
360 	if (flags & FWRITE) {
361 		VNASSERT(vp->v_writecount > 0, vp,
362 		    ("vn_close: negative writecount"));
363 		VOP_ADD_WRITECOUNT(vp, -1);
364 		CTR3(KTR_VFS, "%s: vp %p v_writecount decreased to %d",
365 		    __func__, vp, vp->v_writecount);
366 	}
367 	error = VOP_CLOSE(vp, flags, file_cred, td);
368 	vput(vp);
369 	vn_finished_write(mp);
370 	return (error);
371 }
372 
373 /*
374  * Heuristic to detect sequential operation.
375  */
376 static int
377 sequential_heuristic(struct uio *uio, struct file *fp)
378 {
379 
380 	if (atomic_load_acq_int(&(fp->f_flag)) & FRDAHEAD)
381 		return (fp->f_seqcount << IO_SEQSHIFT);
382 
383 	/*
384 	 * Offset 0 is handled specially.  open() sets f_seqcount to 1 so
385 	 * that the first I/O is normally considered to be slightly
386 	 * sequential.  Seeking to offset 0 doesn't change sequentiality
387 	 * unless previous seeks have reduced f_seqcount to 0, in which
388 	 * case offset 0 is not special.
389 	 */
390 	if ((uio->uio_offset == 0 && fp->f_seqcount > 0) ||
391 	    uio->uio_offset == fp->f_nextoff) {
392 		/*
393 		 * f_seqcount is in units of fixed-size blocks so that it
394 		 * depends mainly on the amount of sequential I/O and not
395 		 * much on the number of sequential I/O's.  The fixed size
396 		 * of 16384 is hard-coded here since it is (not quite) just
397 		 * a magic size that works well here.  This size is more
398 		 * closely related to the best I/O size for real disks than
399 		 * to any block size used by software.
400 		 */
401 		fp->f_seqcount += howmany(uio->uio_resid, 16384);
402 		if (fp->f_seqcount > IO_SEQMAX)
403 			fp->f_seqcount = IO_SEQMAX;
404 		return (fp->f_seqcount << IO_SEQSHIFT);
405 	}
406 
407 	/* Not sequential.  Quickly draw-down sequentiality. */
408 	if (fp->f_seqcount > 1)
409 		fp->f_seqcount = 1;
410 	else
411 		fp->f_seqcount = 0;
412 	return (0);
413 }
414 
415 /*
416  * Package up an I/O request on a vnode into a uio and do it.
417  */
418 int
419 vn_rdwr(enum uio_rw rw, struct vnode *vp, void *base, int len, off_t offset,
420     enum uio_seg segflg, int ioflg, struct ucred *active_cred,
421     struct ucred *file_cred, ssize_t *aresid, struct thread *td)
422 {
423 	struct uio auio;
424 	struct iovec aiov;
425 	struct mount *mp;
426 	struct ucred *cred;
427 	void *rl_cookie;
428 	int error, lock_flags;
429 
430 	auio.uio_iov = &aiov;
431 	auio.uio_iovcnt = 1;
432 	aiov.iov_base = base;
433 	aiov.iov_len = len;
434 	auio.uio_resid = len;
435 	auio.uio_offset = offset;
436 	auio.uio_segflg = segflg;
437 	auio.uio_rw = rw;
438 	auio.uio_td = td;
439 	error = 0;
440 
441 	if ((ioflg & IO_NODELOCKED) == 0) {
442 		if (rw == UIO_READ) {
443 			rl_cookie = vn_rangelock_rlock(vp, offset,
444 			    offset + len);
445 		} else {
446 			rl_cookie = vn_rangelock_wlock(vp, offset,
447 			    offset + len);
448 		}
449 		mp = NULL;
450 		if (rw == UIO_WRITE) {
451 			if (vp->v_type != VCHR &&
452 			    (error = vn_start_write(vp, &mp, V_WAIT | PCATCH))
453 			    != 0)
454 				goto out;
455 			if (MNT_SHARED_WRITES(mp) ||
456 			    ((mp == NULL) && MNT_SHARED_WRITES(vp->v_mount)))
457 				lock_flags = LK_SHARED;
458 			else
459 				lock_flags = LK_EXCLUSIVE;
460 		} else
461 			lock_flags = LK_SHARED;
462 		vn_lock(vp, lock_flags | LK_RETRY);
463 	} else
464 		rl_cookie = NULL;
465 
466 	ASSERT_VOP_LOCKED(vp, "IO_NODELOCKED with no vp lock held");
467 #ifdef MAC
468 	if ((ioflg & IO_NOMACCHECK) == 0) {
469 		if (rw == UIO_READ)
470 			error = mac_vnode_check_read(active_cred, file_cred,
471 			    vp);
472 		else
473 			error = mac_vnode_check_write(active_cred, file_cred,
474 			    vp);
475 	}
476 #endif
477 	if (error == 0) {
478 		if (file_cred != NULL)
479 			cred = file_cred;
480 		else
481 			cred = active_cred;
482 		if (rw == UIO_READ)
483 			error = VOP_READ(vp, &auio, ioflg, cred);
484 		else
485 			error = VOP_WRITE(vp, &auio, ioflg, cred);
486 	}
487 	if (aresid)
488 		*aresid = auio.uio_resid;
489 	else
490 		if (auio.uio_resid && error == 0)
491 			error = EIO;
492 	if ((ioflg & IO_NODELOCKED) == 0) {
493 		VOP_UNLOCK(vp, 0);
494 		if (mp != NULL)
495 			vn_finished_write(mp);
496 	}
497  out:
498 	if (rl_cookie != NULL)
499 		vn_rangelock_unlock(vp, rl_cookie);
500 	return (error);
501 }
502 
503 /*
504  * Package up an I/O request on a vnode into a uio and do it.  The I/O
505  * request is split up into smaller chunks and we try to avoid saturating
506  * the buffer cache while potentially holding a vnode locked, so we
507  * check bwillwrite() before calling vn_rdwr().  We also call kern_yield()
508  * to give other processes a chance to lock the vnode (either other processes
509  * core'ing the same binary, or unrelated processes scanning the directory).
510  */
511 int
512 vn_rdwr_inchunks(rw, vp, base, len, offset, segflg, ioflg, active_cred,
513     file_cred, aresid, td)
514 	enum uio_rw rw;
515 	struct vnode *vp;
516 	void *base;
517 	size_t len;
518 	off_t offset;
519 	enum uio_seg segflg;
520 	int ioflg;
521 	struct ucred *active_cred;
522 	struct ucred *file_cred;
523 	size_t *aresid;
524 	struct thread *td;
525 {
526 	int error = 0;
527 	ssize_t iaresid;
528 
529 	do {
530 		int chunk;
531 
532 		/*
533 		 * Force `offset' to a multiple of MAXBSIZE except possibly
534 		 * for the first chunk, so that filesystems only need to
535 		 * write full blocks except possibly for the first and last
536 		 * chunks.
537 		 */
538 		chunk = MAXBSIZE - (uoff_t)offset % MAXBSIZE;
539 
540 		if (chunk > len)
541 			chunk = len;
542 		if (rw != UIO_READ && vp->v_type == VREG)
543 			bwillwrite();
544 		iaresid = 0;
545 		error = vn_rdwr(rw, vp, base, chunk, offset, segflg,
546 		    ioflg, active_cred, file_cred, &iaresid, td);
547 		len -= chunk;	/* aresid calc already includes length */
548 		if (error)
549 			break;
550 		offset += chunk;
551 		base = (char *)base + chunk;
552 		kern_yield(PRI_USER);
553 	} while (len);
554 	if (aresid)
555 		*aresid = len + iaresid;
556 	return (error);
557 }
558 
559 off_t
560 foffset_lock(struct file *fp, int flags)
561 {
562 	struct mtx *mtxp;
563 	off_t res;
564 
565 	KASSERT((flags & FOF_OFFSET) == 0, ("FOF_OFFSET passed"));
566 
567 #if OFF_MAX <= LONG_MAX
568 	/*
569 	 * Caller only wants the current f_offset value.  Assume that
570 	 * the long and shorter integer types reads are atomic.
571 	 */
572 	if ((flags & FOF_NOLOCK) != 0)
573 		return (fp->f_offset);
574 #endif
575 
576 	/*
577 	 * According to McKusick the vn lock was protecting f_offset here.
578 	 * It is now protected by the FOFFSET_LOCKED flag.
579 	 */
580 	mtxp = mtx_pool_find(mtxpool_sleep, fp);
581 	mtx_lock(mtxp);
582 	if ((flags & FOF_NOLOCK) == 0) {
583 		while (fp->f_vnread_flags & FOFFSET_LOCKED) {
584 			fp->f_vnread_flags |= FOFFSET_LOCK_WAITING;
585 			msleep(&fp->f_vnread_flags, mtxp, PUSER -1,
586 			    "vofflock", 0);
587 		}
588 		fp->f_vnread_flags |= FOFFSET_LOCKED;
589 	}
590 	res = fp->f_offset;
591 	mtx_unlock(mtxp);
592 	return (res);
593 }
594 
595 void
596 foffset_unlock(struct file *fp, off_t val, int flags)
597 {
598 	struct mtx *mtxp;
599 
600 	KASSERT((flags & FOF_OFFSET) == 0, ("FOF_OFFSET passed"));
601 
602 #if OFF_MAX <= LONG_MAX
603 	if ((flags & FOF_NOLOCK) != 0) {
604 		if ((flags & FOF_NOUPDATE) == 0)
605 			fp->f_offset = val;
606 		if ((flags & FOF_NEXTOFF) != 0)
607 			fp->f_nextoff = val;
608 		return;
609 	}
610 #endif
611 
612 	mtxp = mtx_pool_find(mtxpool_sleep, fp);
613 	mtx_lock(mtxp);
614 	if ((flags & FOF_NOUPDATE) == 0)
615 		fp->f_offset = val;
616 	if ((flags & FOF_NEXTOFF) != 0)
617 		fp->f_nextoff = val;
618 	if ((flags & FOF_NOLOCK) == 0) {
619 		KASSERT((fp->f_vnread_flags & FOFFSET_LOCKED) != 0,
620 		    ("Lost FOFFSET_LOCKED"));
621 		if (fp->f_vnread_flags & FOFFSET_LOCK_WAITING)
622 			wakeup(&fp->f_vnread_flags);
623 		fp->f_vnread_flags = 0;
624 	}
625 	mtx_unlock(mtxp);
626 }
627 
628 void
629 foffset_lock_uio(struct file *fp, struct uio *uio, int flags)
630 {
631 
632 	if ((flags & FOF_OFFSET) == 0)
633 		uio->uio_offset = foffset_lock(fp, flags);
634 }
635 
636 void
637 foffset_unlock_uio(struct file *fp, struct uio *uio, int flags)
638 {
639 
640 	if ((flags & FOF_OFFSET) == 0)
641 		foffset_unlock(fp, uio->uio_offset, flags);
642 }
643 
644 static int
645 get_advice(struct file *fp, struct uio *uio)
646 {
647 	struct mtx *mtxp;
648 	int ret;
649 
650 	ret = POSIX_FADV_NORMAL;
651 	if (fp->f_advice == NULL)
652 		return (ret);
653 
654 	mtxp = mtx_pool_find(mtxpool_sleep, fp);
655 	mtx_lock(mtxp);
656 	if (uio->uio_offset >= fp->f_advice->fa_start &&
657 	    uio->uio_offset + uio->uio_resid <= fp->f_advice->fa_end)
658 		ret = fp->f_advice->fa_advice;
659 	mtx_unlock(mtxp);
660 	return (ret);
661 }
662 
663 /*
664  * File table vnode read routine.
665  */
666 static int
667 vn_read(fp, uio, active_cred, flags, td)
668 	struct file *fp;
669 	struct uio *uio;
670 	struct ucred *active_cred;
671 	int flags;
672 	struct thread *td;
673 {
674 	struct vnode *vp;
675 	struct mtx *mtxp;
676 	int error, ioflag;
677 	int advice;
678 	off_t offset, start, end;
679 
680 	KASSERT(uio->uio_td == td, ("uio_td %p is not td %p",
681 	    uio->uio_td, td));
682 	KASSERT(flags & FOF_OFFSET, ("No FOF_OFFSET"));
683 	vp = fp->f_vnode;
684 	ioflag = 0;
685 	if (fp->f_flag & FNONBLOCK)
686 		ioflag |= IO_NDELAY;
687 	if (fp->f_flag & O_DIRECT)
688 		ioflag |= IO_DIRECT;
689 	advice = get_advice(fp, uio);
690 	vn_lock(vp, LK_SHARED | LK_RETRY);
691 
692 	switch (advice) {
693 	case POSIX_FADV_NORMAL:
694 	case POSIX_FADV_SEQUENTIAL:
695 	case POSIX_FADV_NOREUSE:
696 		ioflag |= sequential_heuristic(uio, fp);
697 		break;
698 	case POSIX_FADV_RANDOM:
699 		/* Disable read-ahead for random I/O. */
700 		break;
701 	}
702 	offset = uio->uio_offset;
703 
704 #ifdef MAC
705 	error = mac_vnode_check_read(active_cred, fp->f_cred, vp);
706 	if (error == 0)
707 #endif
708 		error = VOP_READ(vp, uio, ioflag, fp->f_cred);
709 	fp->f_nextoff = uio->uio_offset;
710 	VOP_UNLOCK(vp, 0);
711 	if (error == 0 && advice == POSIX_FADV_NOREUSE &&
712 	    offset != uio->uio_offset) {
713 		/*
714 		 * Use POSIX_FADV_DONTNEED to flush clean pages and
715 		 * buffers for the backing file after a
716 		 * POSIX_FADV_NOREUSE read(2).  To optimize the common
717 		 * case of using POSIX_FADV_NOREUSE with sequential
718 		 * access, track the previous implicit DONTNEED
719 		 * request and grow this request to include the
720 		 * current read(2) in addition to the previous
721 		 * DONTNEED.  With purely sequential access this will
722 		 * cause the DONTNEED requests to continously grow to
723 		 * cover all of the previously read regions of the
724 		 * file.  This allows filesystem blocks that are
725 		 * accessed by multiple calls to read(2) to be flushed
726 		 * once the last read(2) finishes.
727 		 */
728 		start = offset;
729 		end = uio->uio_offset - 1;
730 		mtxp = mtx_pool_find(mtxpool_sleep, fp);
731 		mtx_lock(mtxp);
732 		if (fp->f_advice != NULL &&
733 		    fp->f_advice->fa_advice == POSIX_FADV_NOREUSE) {
734 			if (start != 0 && fp->f_advice->fa_prevend + 1 == start)
735 				start = fp->f_advice->fa_prevstart;
736 			else if (fp->f_advice->fa_prevstart != 0 &&
737 			    fp->f_advice->fa_prevstart == end + 1)
738 				end = fp->f_advice->fa_prevend;
739 			fp->f_advice->fa_prevstart = start;
740 			fp->f_advice->fa_prevend = end;
741 		}
742 		mtx_unlock(mtxp);
743 		error = VOP_ADVISE(vp, start, end, POSIX_FADV_DONTNEED);
744 	}
745 	return (error);
746 }
747 
748 /*
749  * File table vnode write routine.
750  */
751 static int
752 vn_write(fp, uio, active_cred, flags, td)
753 	struct file *fp;
754 	struct uio *uio;
755 	struct ucred *active_cred;
756 	int flags;
757 	struct thread *td;
758 {
759 	struct vnode *vp;
760 	struct mount *mp;
761 	struct mtx *mtxp;
762 	int error, ioflag, lock_flags;
763 	int advice;
764 	off_t offset, start, end;
765 
766 	KASSERT(uio->uio_td == td, ("uio_td %p is not td %p",
767 	    uio->uio_td, td));
768 	KASSERT(flags & FOF_OFFSET, ("No FOF_OFFSET"));
769 	vp = fp->f_vnode;
770 	if (vp->v_type == VREG)
771 		bwillwrite();
772 	ioflag = IO_UNIT;
773 	if (vp->v_type == VREG && (fp->f_flag & O_APPEND))
774 		ioflag |= IO_APPEND;
775 	if (fp->f_flag & FNONBLOCK)
776 		ioflag |= IO_NDELAY;
777 	if (fp->f_flag & O_DIRECT)
778 		ioflag |= IO_DIRECT;
779 	if ((fp->f_flag & O_FSYNC) ||
780 	    (vp->v_mount && (vp->v_mount->mnt_flag & MNT_SYNCHRONOUS)))
781 		ioflag |= IO_SYNC;
782 	mp = NULL;
783 	if (vp->v_type != VCHR &&
784 	    (error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
785 		goto unlock;
786 
787 	advice = get_advice(fp, uio);
788 
789 	if (MNT_SHARED_WRITES(mp) ||
790 	    (mp == NULL && MNT_SHARED_WRITES(vp->v_mount))) {
791 		lock_flags = LK_SHARED;
792 	} else {
793 		lock_flags = LK_EXCLUSIVE;
794 	}
795 
796 	vn_lock(vp, lock_flags | LK_RETRY);
797 	switch (advice) {
798 	case POSIX_FADV_NORMAL:
799 	case POSIX_FADV_SEQUENTIAL:
800 	case POSIX_FADV_NOREUSE:
801 		ioflag |= sequential_heuristic(uio, fp);
802 		break;
803 	case POSIX_FADV_RANDOM:
804 		/* XXX: Is this correct? */
805 		break;
806 	}
807 	offset = uio->uio_offset;
808 
809 #ifdef MAC
810 	error = mac_vnode_check_write(active_cred, fp->f_cred, vp);
811 	if (error == 0)
812 #endif
813 		error = VOP_WRITE(vp, uio, ioflag, fp->f_cred);
814 	fp->f_nextoff = uio->uio_offset;
815 	VOP_UNLOCK(vp, 0);
816 	if (vp->v_type != VCHR)
817 		vn_finished_write(mp);
818 	if (error == 0 && advice == POSIX_FADV_NOREUSE &&
819 	    offset != uio->uio_offset) {
820 		/*
821 		 * Use POSIX_FADV_DONTNEED to flush clean pages and
822 		 * buffers for the backing file after a
823 		 * POSIX_FADV_NOREUSE write(2).  To optimize the
824 		 * common case of using POSIX_FADV_NOREUSE with
825 		 * sequential access, track the previous implicit
826 		 * DONTNEED request and grow this request to include
827 		 * the current write(2) in addition to the previous
828 		 * DONTNEED.  With purely sequential access this will
829 		 * cause the DONTNEED requests to continously grow to
830 		 * cover all of the previously written regions of the
831 		 * file.
832 		 *
833 		 * Note that the blocks just written are almost
834 		 * certainly still dirty, so this only works when
835 		 * VOP_ADVISE() calls from subsequent writes push out
836 		 * the data written by this write(2) once the backing
837 		 * buffers are clean.  However, as compared to forcing
838 		 * IO_DIRECT, this gives much saner behavior.  Write
839 		 * clustering is still allowed, and clean pages are
840 		 * merely moved to the cache page queue rather than
841 		 * outright thrown away.  This means a subsequent
842 		 * read(2) can still avoid hitting the disk if the
843 		 * pages have not been reclaimed.
844 		 *
845 		 * This does make POSIX_FADV_NOREUSE largely useless
846 		 * with non-sequential access.  However, sequential
847 		 * access is the more common use case and the flag is
848 		 * merely advisory.
849 		 */
850 		start = offset;
851 		end = uio->uio_offset - 1;
852 		mtxp = mtx_pool_find(mtxpool_sleep, fp);
853 		mtx_lock(mtxp);
854 		if (fp->f_advice != NULL &&
855 		    fp->f_advice->fa_advice == POSIX_FADV_NOREUSE) {
856 			if (start != 0 && fp->f_advice->fa_prevend + 1 == start)
857 				start = fp->f_advice->fa_prevstart;
858 			else if (fp->f_advice->fa_prevstart != 0 &&
859 			    fp->f_advice->fa_prevstart == end + 1)
860 				end = fp->f_advice->fa_prevend;
861 			fp->f_advice->fa_prevstart = start;
862 			fp->f_advice->fa_prevend = end;
863 		}
864 		mtx_unlock(mtxp);
865 		error = VOP_ADVISE(vp, start, end, POSIX_FADV_DONTNEED);
866 	}
867 
868 unlock:
869 	return (error);
870 }
871 
872 static const int io_hold_cnt = 16;
873 static int vn_io_fault_enable = 1;
874 SYSCTL_INT(_debug, OID_AUTO, vn_io_fault_enable, CTLFLAG_RW,
875     &vn_io_fault_enable, 0, "Enable vn_io_fault lock avoidance");
876 static u_long vn_io_faults_cnt;
877 SYSCTL_ULONG(_debug, OID_AUTO, vn_io_faults, CTLFLAG_RD,
878     &vn_io_faults_cnt, 0, "Count of vn_io_fault lock avoidance triggers");
879 
880 /*
881  * The vn_io_fault() is a wrapper around vn_read() and vn_write() to
882  * prevent the following deadlock:
883  *
884  * Assume that the thread A reads from the vnode vp1 into userspace
885  * buffer buf1 backed by the pages of vnode vp2.  If a page in buf1 is
886  * currently not resident, then system ends up with the call chain
887  *   vn_read() -> VOP_READ(vp1) -> uiomove() -> [Page Fault] ->
888  *     vm_fault(buf1) -> vnode_pager_getpages(vp2) -> VOP_GETPAGES(vp2)
889  * which establishes lock order vp1->vn_lock, then vp2->vn_lock.
890  * If, at the same time, thread B reads from vnode vp2 into buffer buf2
891  * backed by the pages of vnode vp1, and some page in buf2 is not
892  * resident, we get a reversed order vp2->vn_lock, then vp1->vn_lock.
893  *
894  * To prevent the lock order reversal and deadlock, vn_io_fault() does
895  * not allow page faults to happen during VOP_READ() or VOP_WRITE().
896  * Instead, it first tries to do the whole range i/o with pagefaults
897  * disabled. If all pages in the i/o buffer are resident and mapped,
898  * VOP will succeed (ignoring the genuine filesystem errors).
899  * Otherwise, we get back EFAULT, and vn_io_fault() falls back to do
900  * i/o in chunks, with all pages in the chunk prefaulted and held
901  * using vm_fault_quick_hold_pages().
902  *
903  * Filesystems using this deadlock avoidance scheme should use the
904  * array of the held pages from uio, saved in the curthread->td_ma,
905  * instead of doing uiomove().  A helper function
906  * vn_io_fault_uiomove() converts uiomove request into
907  * uiomove_fromphys() over td_ma array.
908  *
909  * Since vnode locks do not cover the whole i/o anymore, rangelocks
910  * make the current i/o request atomic with respect to other i/os and
911  * truncations.
912  */
913 static int
914 vn_io_fault(struct file *fp, struct uio *uio, struct ucred *active_cred,
915     int flags, struct thread *td)
916 {
917 	vm_page_t ma[io_hold_cnt + 2];
918 	struct uio *uio_clone, short_uio;
919 	struct iovec short_iovec[1];
920 	fo_rdwr_t *doio;
921 	struct vnode *vp;
922 	void *rl_cookie;
923 	struct mount *mp;
924 	vm_page_t *prev_td_ma;
925 	int cnt, error, save, saveheld, prev_td_ma_cnt;
926 	vm_offset_t addr, end;
927 	vm_prot_t prot;
928 	size_t len, resid;
929 	ssize_t adv;
930 
931 	if (uio->uio_rw == UIO_READ)
932 		doio = vn_read;
933 	else
934 		doio = vn_write;
935 	vp = fp->f_vnode;
936 	foffset_lock_uio(fp, uio, flags);
937 
938 	if (uio->uio_segflg != UIO_USERSPACE || vp->v_type != VREG ||
939 	    ((mp = vp->v_mount) != NULL &&
940 	    (mp->mnt_kern_flag & MNTK_NO_IOPF) == 0) ||
941 	    !vn_io_fault_enable) {
942 		error = doio(fp, uio, active_cred, flags | FOF_OFFSET, td);
943 		goto out_last;
944 	}
945 
946 	/*
947 	 * The UFS follows IO_UNIT directive and replays back both
948 	 * uio_offset and uio_resid if an error is encountered during the
949 	 * operation.  But, since the iovec may be already advanced,
950 	 * uio is still in an inconsistent state.
951 	 *
952 	 * Cache a copy of the original uio, which is advanced to the redo
953 	 * point using UIO_NOCOPY below.
954 	 */
955 	uio_clone = cloneuio(uio);
956 	resid = uio->uio_resid;
957 
958 	short_uio.uio_segflg = UIO_USERSPACE;
959 	short_uio.uio_rw = uio->uio_rw;
960 	short_uio.uio_td = uio->uio_td;
961 
962 	if (uio->uio_rw == UIO_READ) {
963 		prot = VM_PROT_WRITE;
964 		rl_cookie = vn_rangelock_rlock(vp, uio->uio_offset,
965 		    uio->uio_offset + uio->uio_resid);
966 	} else {
967 		prot = VM_PROT_READ;
968 		if ((fp->f_flag & O_APPEND) != 0 || (flags & FOF_OFFSET) == 0)
969 			/* For appenders, punt and lock the whole range. */
970 			rl_cookie = vn_rangelock_wlock(vp, 0, OFF_MAX);
971 		else
972 			rl_cookie = vn_rangelock_wlock(vp, uio->uio_offset,
973 			    uio->uio_offset + uio->uio_resid);
974 	}
975 
976 	save = vm_fault_disable_pagefaults();
977 	error = doio(fp, uio, active_cred, flags | FOF_OFFSET, td);
978 	if (error != EFAULT)
979 		goto out;
980 
981 	atomic_add_long(&vn_io_faults_cnt, 1);
982 	uio_clone->uio_segflg = UIO_NOCOPY;
983 	uiomove(NULL, resid - uio->uio_resid, uio_clone);
984 	uio_clone->uio_segflg = uio->uio_segflg;
985 
986 	saveheld = curthread_pflags_set(TDP_UIOHELD);
987 	prev_td_ma = td->td_ma;
988 	prev_td_ma_cnt = td->td_ma_cnt;
989 
990 	while (uio_clone->uio_resid != 0) {
991 		len = uio_clone->uio_iov->iov_len;
992 		if (len == 0) {
993 			KASSERT(uio_clone->uio_iovcnt >= 1,
994 			    ("iovcnt underflow"));
995 			uio_clone->uio_iov++;
996 			uio_clone->uio_iovcnt--;
997 			continue;
998 		}
999 
1000 		addr = (vm_offset_t)uio_clone->uio_iov->iov_base;
1001 		end = round_page(addr + len);
1002 		cnt = howmany(end - trunc_page(addr), PAGE_SIZE);
1003 		/*
1004 		 * A perfectly misaligned address and length could cause
1005 		 * both the start and the end of the chunk to use partial
1006 		 * page.  +2 accounts for such a situation.
1007 		 */
1008 		if (cnt > io_hold_cnt + 2) {
1009 			len = io_hold_cnt * PAGE_SIZE;
1010 			KASSERT(howmany(round_page(addr + len) -
1011 			    trunc_page(addr), PAGE_SIZE) <= io_hold_cnt + 2,
1012 			    ("cnt overflow"));
1013 		}
1014 		cnt = vm_fault_quick_hold_pages(&td->td_proc->p_vmspace->vm_map,
1015 		    addr, len, prot, ma, io_hold_cnt + 2);
1016 		if (cnt == -1) {
1017 			error = EFAULT;
1018 			break;
1019 		}
1020 		short_uio.uio_iov = &short_iovec[0];
1021 		short_iovec[0].iov_base = (void *)addr;
1022 		short_uio.uio_iovcnt = 1;
1023 		short_uio.uio_resid = short_iovec[0].iov_len = len;
1024 		short_uio.uio_offset = uio_clone->uio_offset;
1025 		td->td_ma = ma;
1026 		td->td_ma_cnt = cnt;
1027 
1028 		error = doio(fp, &short_uio, active_cred, flags | FOF_OFFSET,
1029 		    td);
1030 		vm_page_unhold_pages(ma, cnt);
1031 		adv = len - short_uio.uio_resid;
1032 
1033 		uio_clone->uio_iov->iov_base =
1034 		    (char *)uio_clone->uio_iov->iov_base + adv;
1035 		uio_clone->uio_iov->iov_len -= adv;
1036 		uio_clone->uio_resid -= adv;
1037 		uio_clone->uio_offset += adv;
1038 
1039 		uio->uio_resid -= adv;
1040 		uio->uio_offset += adv;
1041 
1042 		if (error != 0 || adv == 0)
1043 			break;
1044 	}
1045 	td->td_ma = prev_td_ma;
1046 	td->td_ma_cnt = prev_td_ma_cnt;
1047 	curthread_pflags_restore(saveheld);
1048 out:
1049 	vm_fault_enable_pagefaults(save);
1050 	vn_rangelock_unlock(vp, rl_cookie);
1051 	free(uio_clone, M_IOV);
1052 out_last:
1053 	foffset_unlock_uio(fp, uio, flags);
1054 	return (error);
1055 }
1056 
1057 /*
1058  * Helper function to perform the requested uiomove operation using
1059  * the held pages for io->uio_iov[0].iov_base buffer instead of
1060  * copyin/copyout.  Access to the pages with uiomove_fromphys()
1061  * instead of iov_base prevents page faults that could occur due to
1062  * pmap_collect() invalidating the mapping created by
1063  * vm_fault_quick_hold_pages(), or pageout daemon, page laundry or
1064  * object cleanup revoking the write access from page mappings.
1065  *
1066  * Filesystems specified MNTK_NO_IOPF shall use vn_io_fault_uiomove()
1067  * instead of plain uiomove().
1068  */
1069 int
1070 vn_io_fault_uiomove(char *data, int xfersize, struct uio *uio)
1071 {
1072 	struct uio transp_uio;
1073 	struct iovec transp_iov[1];
1074 	struct thread *td;
1075 	size_t adv;
1076 	int error, pgadv;
1077 
1078 	td = curthread;
1079 	if ((td->td_pflags & TDP_UIOHELD) == 0 ||
1080 	    uio->uio_segflg != UIO_USERSPACE)
1081 		return (uiomove(data, xfersize, uio));
1082 
1083 	KASSERT(uio->uio_iovcnt == 1, ("uio_iovcnt %d", uio->uio_iovcnt));
1084 	transp_iov[0].iov_base = data;
1085 	transp_uio.uio_iov = &transp_iov[0];
1086 	transp_uio.uio_iovcnt = 1;
1087 	if (xfersize > uio->uio_resid)
1088 		xfersize = uio->uio_resid;
1089 	transp_uio.uio_resid = transp_iov[0].iov_len = xfersize;
1090 	transp_uio.uio_offset = 0;
1091 	transp_uio.uio_segflg = UIO_SYSSPACE;
1092 	/*
1093 	 * Since transp_iov points to data, and td_ma page array
1094 	 * corresponds to original uio->uio_iov, we need to invert the
1095 	 * direction of the i/o operation as passed to
1096 	 * uiomove_fromphys().
1097 	 */
1098 	switch (uio->uio_rw) {
1099 	case UIO_WRITE:
1100 		transp_uio.uio_rw = UIO_READ;
1101 		break;
1102 	case UIO_READ:
1103 		transp_uio.uio_rw = UIO_WRITE;
1104 		break;
1105 	}
1106 	transp_uio.uio_td = uio->uio_td;
1107 	error = uiomove_fromphys(td->td_ma,
1108 	    ((vm_offset_t)uio->uio_iov->iov_base) & PAGE_MASK,
1109 	    xfersize, &transp_uio);
1110 	adv = xfersize - transp_uio.uio_resid;
1111 	pgadv =
1112 	    (((vm_offset_t)uio->uio_iov->iov_base + adv) >> PAGE_SHIFT) -
1113 	    (((vm_offset_t)uio->uio_iov->iov_base) >> PAGE_SHIFT);
1114 	td->td_ma += pgadv;
1115 	KASSERT(td->td_ma_cnt >= pgadv, ("consumed pages %d %d", td->td_ma_cnt,
1116 	    pgadv));
1117 	td->td_ma_cnt -= pgadv;
1118 	uio->uio_iov->iov_base = (char *)uio->uio_iov->iov_base + adv;
1119 	uio->uio_iov->iov_len -= adv;
1120 	uio->uio_resid -= adv;
1121 	uio->uio_offset += adv;
1122 	return (error);
1123 }
1124 
1125 /*
1126  * File table truncate routine.
1127  */
1128 static int
1129 vn_truncate(struct file *fp, off_t length, struct ucred *active_cred,
1130     struct thread *td)
1131 {
1132 	struct vattr vattr;
1133 	struct mount *mp;
1134 	struct vnode *vp;
1135 	void *rl_cookie;
1136 	int error;
1137 
1138 	vp = fp->f_vnode;
1139 
1140 	/*
1141 	 * Lock the whole range for truncation.  Otherwise split i/o
1142 	 * might happen partly before and partly after the truncation.
1143 	 */
1144 	rl_cookie = vn_rangelock_wlock(vp, 0, OFF_MAX);
1145 	error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
1146 	if (error)
1147 		goto out1;
1148 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1149 	if (vp->v_type == VDIR) {
1150 		error = EISDIR;
1151 		goto out;
1152 	}
1153 #ifdef MAC
1154 	error = mac_vnode_check_write(active_cred, fp->f_cred, vp);
1155 	if (error)
1156 		goto out;
1157 #endif
1158 	error = vn_writechk(vp);
1159 	if (error == 0) {
1160 		VATTR_NULL(&vattr);
1161 		vattr.va_size = length;
1162 		error = VOP_SETATTR(vp, &vattr, fp->f_cred);
1163 	}
1164 out:
1165 	VOP_UNLOCK(vp, 0);
1166 	vn_finished_write(mp);
1167 out1:
1168 	vn_rangelock_unlock(vp, rl_cookie);
1169 	return (error);
1170 }
1171 
1172 /*
1173  * File table vnode stat routine.
1174  */
1175 static int
1176 vn_statfile(fp, sb, active_cred, td)
1177 	struct file *fp;
1178 	struct stat *sb;
1179 	struct ucred *active_cred;
1180 	struct thread *td;
1181 {
1182 	struct vnode *vp = fp->f_vnode;
1183 	int error;
1184 
1185 	vn_lock(vp, LK_SHARED | LK_RETRY);
1186 	error = vn_stat(vp, sb, active_cred, fp->f_cred, td);
1187 	VOP_UNLOCK(vp, 0);
1188 
1189 	return (error);
1190 }
1191 
1192 /*
1193  * Stat a vnode; implementation for the stat syscall
1194  */
1195 int
1196 vn_stat(vp, sb, active_cred, file_cred, td)
1197 	struct vnode *vp;
1198 	register struct stat *sb;
1199 	struct ucred *active_cred;
1200 	struct ucred *file_cred;
1201 	struct thread *td;
1202 {
1203 	struct vattr vattr;
1204 	register struct vattr *vap;
1205 	int error;
1206 	u_short mode;
1207 
1208 #ifdef MAC
1209 	error = mac_vnode_check_stat(active_cred, file_cred, vp);
1210 	if (error)
1211 		return (error);
1212 #endif
1213 
1214 	vap = &vattr;
1215 
1216 	/*
1217 	 * Initialize defaults for new and unusual fields, so that file
1218 	 * systems which don't support these fields don't need to know
1219 	 * about them.
1220 	 */
1221 	vap->va_birthtime.tv_sec = -1;
1222 	vap->va_birthtime.tv_nsec = 0;
1223 	vap->va_fsid = VNOVAL;
1224 	vap->va_rdev = NODEV;
1225 
1226 	error = VOP_GETATTR(vp, vap, active_cred);
1227 	if (error)
1228 		return (error);
1229 
1230 	/*
1231 	 * Zero the spare stat fields
1232 	 */
1233 	bzero(sb, sizeof *sb);
1234 
1235 	/*
1236 	 * Copy from vattr table
1237 	 */
1238 	if (vap->va_fsid != VNOVAL)
1239 		sb->st_dev = vap->va_fsid;
1240 	else
1241 		sb->st_dev = vp->v_mount->mnt_stat.f_fsid.val[0];
1242 	sb->st_ino = vap->va_fileid;
1243 	mode = vap->va_mode;
1244 	switch (vap->va_type) {
1245 	case VREG:
1246 		mode |= S_IFREG;
1247 		break;
1248 	case VDIR:
1249 		mode |= S_IFDIR;
1250 		break;
1251 	case VBLK:
1252 		mode |= S_IFBLK;
1253 		break;
1254 	case VCHR:
1255 		mode |= S_IFCHR;
1256 		break;
1257 	case VLNK:
1258 		mode |= S_IFLNK;
1259 		break;
1260 	case VSOCK:
1261 		mode |= S_IFSOCK;
1262 		break;
1263 	case VFIFO:
1264 		mode |= S_IFIFO;
1265 		break;
1266 	default:
1267 		return (EBADF);
1268 	};
1269 	sb->st_mode = mode;
1270 	sb->st_nlink = vap->va_nlink;
1271 	sb->st_uid = vap->va_uid;
1272 	sb->st_gid = vap->va_gid;
1273 	sb->st_rdev = vap->va_rdev;
1274 	if (vap->va_size > OFF_MAX)
1275 		return (EOVERFLOW);
1276 	sb->st_size = vap->va_size;
1277 	sb->st_atim = vap->va_atime;
1278 	sb->st_mtim = vap->va_mtime;
1279 	sb->st_ctim = vap->va_ctime;
1280 	sb->st_birthtim = vap->va_birthtime;
1281 
1282         /*
1283 	 * According to www.opengroup.org, the meaning of st_blksize is
1284 	 *   "a filesystem-specific preferred I/O block size for this
1285 	 *    object.  In some filesystem types, this may vary from file
1286 	 *    to file"
1287 	 * Use miminum/default of PAGE_SIZE (e.g. for VCHR).
1288 	 */
1289 
1290 	sb->st_blksize = max(PAGE_SIZE, vap->va_blocksize);
1291 
1292 	sb->st_flags = vap->va_flags;
1293 	if (priv_check(td, PRIV_VFS_GENERATION))
1294 		sb->st_gen = 0;
1295 	else
1296 		sb->st_gen = vap->va_gen;
1297 
1298 	sb->st_blocks = vap->va_bytes / S_BLKSIZE;
1299 	return (0);
1300 }
1301 
1302 /*
1303  * File table vnode ioctl routine.
1304  */
1305 static int
1306 vn_ioctl(fp, com, data, active_cred, td)
1307 	struct file *fp;
1308 	u_long com;
1309 	void *data;
1310 	struct ucred *active_cred;
1311 	struct thread *td;
1312 {
1313 	struct vnode *vp = fp->f_vnode;
1314 	struct vattr vattr;
1315 	int error;
1316 
1317 	error = ENOTTY;
1318 	switch (vp->v_type) {
1319 	case VREG:
1320 	case VDIR:
1321 		if (com == FIONREAD) {
1322 			vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1323 			error = VOP_GETATTR(vp, &vattr, active_cred);
1324 			VOP_UNLOCK(vp, 0);
1325 			if (!error)
1326 				*(int *)data = vattr.va_size - fp->f_offset;
1327 		}
1328 		if (com == FIONBIO || com == FIOASYNC)	/* XXX */
1329 			error = 0;
1330 		else
1331 			error = VOP_IOCTL(vp, com, data, fp->f_flag,
1332 			    active_cred, td);
1333 		break;
1334 
1335 	default:
1336 		break;
1337 	}
1338 	return (error);
1339 }
1340 
1341 /*
1342  * File table vnode poll routine.
1343  */
1344 static int
1345 vn_poll(fp, events, active_cred, td)
1346 	struct file *fp;
1347 	int events;
1348 	struct ucred *active_cred;
1349 	struct thread *td;
1350 {
1351 	struct vnode *vp;
1352 	int error;
1353 
1354 	vp = fp->f_vnode;
1355 #ifdef MAC
1356 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1357 	error = mac_vnode_check_poll(active_cred, fp->f_cred, vp);
1358 	VOP_UNLOCK(vp, 0);
1359 	if (!error)
1360 #endif
1361 
1362 	error = VOP_POLL(vp, events, fp->f_cred, td);
1363 	return (error);
1364 }
1365 
1366 /*
1367  * Acquire the requested lock and then check for validity.  LK_RETRY
1368  * permits vn_lock to return doomed vnodes.
1369  */
1370 int
1371 _vn_lock(struct vnode *vp, int flags, char *file, int line)
1372 {
1373 	int error;
1374 
1375 	VNASSERT((flags & LK_TYPE_MASK) != 0, vp,
1376 	    ("vn_lock called with no locktype."));
1377 	do {
1378 #ifdef DEBUG_VFS_LOCKS
1379 		KASSERT(vp->v_holdcnt != 0,
1380 		    ("vn_lock %p: zero hold count", vp));
1381 #endif
1382 		error = VOP_LOCK1(vp, flags, file, line);
1383 		flags &= ~LK_INTERLOCK;	/* Interlock is always dropped. */
1384 		KASSERT((flags & LK_RETRY) == 0 || error == 0,
1385 		    ("LK_RETRY set with incompatible flags (0x%x) or an error occured (%d)",
1386 		    flags, error));
1387 		/*
1388 		 * Callers specify LK_RETRY if they wish to get dead vnodes.
1389 		 * If RETRY is not set, we return ENOENT instead.
1390 		 */
1391 		if (error == 0 && vp->v_iflag & VI_DOOMED &&
1392 		    (flags & LK_RETRY) == 0) {
1393 			VOP_UNLOCK(vp, 0);
1394 			error = ENOENT;
1395 			break;
1396 		}
1397 	} while (flags & LK_RETRY && error != 0);
1398 	return (error);
1399 }
1400 
1401 /*
1402  * File table vnode close routine.
1403  */
1404 static int
1405 vn_closefile(fp, td)
1406 	struct file *fp;
1407 	struct thread *td;
1408 {
1409 	struct vnode *vp;
1410 	struct flock lf;
1411 	int error;
1412 
1413 	vp = fp->f_vnode;
1414 	fp->f_ops = &badfileops;
1415 
1416 	if (fp->f_type == DTYPE_VNODE && fp->f_flag & FHASLOCK)
1417 		vref(vp);
1418 
1419 	error = vn_close(vp, fp->f_flag, fp->f_cred, td);
1420 
1421 	if (fp->f_type == DTYPE_VNODE && fp->f_flag & FHASLOCK) {
1422 		lf.l_whence = SEEK_SET;
1423 		lf.l_start = 0;
1424 		lf.l_len = 0;
1425 		lf.l_type = F_UNLCK;
1426 		(void) VOP_ADVLOCK(vp, fp, F_UNLCK, &lf, F_FLOCK);
1427 		vrele(vp);
1428 	}
1429 	return (error);
1430 }
1431 
1432 /*
1433  * Preparing to start a filesystem write operation. If the operation is
1434  * permitted, then we bump the count of operations in progress and
1435  * proceed. If a suspend request is in progress, we wait until the
1436  * suspension is over, and then proceed.
1437  */
1438 static int
1439 vn_start_write_locked(struct mount *mp, int flags)
1440 {
1441 	int error;
1442 
1443 	mtx_assert(MNT_MTX(mp), MA_OWNED);
1444 	error = 0;
1445 
1446 	/*
1447 	 * Check on status of suspension.
1448 	 */
1449 	if ((curthread->td_pflags & TDP_IGNSUSP) == 0 ||
1450 	    mp->mnt_susp_owner != curthread) {
1451 		while ((mp->mnt_kern_flag & MNTK_SUSPEND) != 0) {
1452 			if (flags & V_NOWAIT) {
1453 				error = EWOULDBLOCK;
1454 				goto unlock;
1455 			}
1456 			error = msleep(&mp->mnt_flag, MNT_MTX(mp),
1457 			    (PUSER - 1) | (flags & PCATCH), "suspfs", 0);
1458 			if (error)
1459 				goto unlock;
1460 		}
1461 	}
1462 	if (flags & V_XSLEEP)
1463 		goto unlock;
1464 	mp->mnt_writeopcount++;
1465 unlock:
1466 	if (error != 0 || (flags & V_XSLEEP) != 0)
1467 		MNT_REL(mp);
1468 	MNT_IUNLOCK(mp);
1469 	return (error);
1470 }
1471 
1472 int
1473 vn_start_write(vp, mpp, flags)
1474 	struct vnode *vp;
1475 	struct mount **mpp;
1476 	int flags;
1477 {
1478 	struct mount *mp;
1479 	int error;
1480 
1481 	error = 0;
1482 	/*
1483 	 * If a vnode is provided, get and return the mount point that
1484 	 * to which it will write.
1485 	 */
1486 	if (vp != NULL) {
1487 		if ((error = VOP_GETWRITEMOUNT(vp, mpp)) != 0) {
1488 			*mpp = NULL;
1489 			if (error != EOPNOTSUPP)
1490 				return (error);
1491 			return (0);
1492 		}
1493 	}
1494 	if ((mp = *mpp) == NULL)
1495 		return (0);
1496 
1497 	/*
1498 	 * VOP_GETWRITEMOUNT() returns with the mp refcount held through
1499 	 * a vfs_ref().
1500 	 * As long as a vnode is not provided we need to acquire a
1501 	 * refcount for the provided mountpoint too, in order to
1502 	 * emulate a vfs_ref().
1503 	 */
1504 	MNT_ILOCK(mp);
1505 	if (vp == NULL)
1506 		MNT_REF(mp);
1507 
1508 	return (vn_start_write_locked(mp, flags));
1509 }
1510 
1511 /*
1512  * Secondary suspension. Used by operations such as vop_inactive
1513  * routines that are needed by the higher level functions. These
1514  * are allowed to proceed until all the higher level functions have
1515  * completed (indicated by mnt_writeopcount dropping to zero). At that
1516  * time, these operations are halted until the suspension is over.
1517  */
1518 int
1519 vn_start_secondary_write(vp, mpp, flags)
1520 	struct vnode *vp;
1521 	struct mount **mpp;
1522 	int flags;
1523 {
1524 	struct mount *mp;
1525 	int error;
1526 
1527  retry:
1528 	if (vp != NULL) {
1529 		if ((error = VOP_GETWRITEMOUNT(vp, mpp)) != 0) {
1530 			*mpp = NULL;
1531 			if (error != EOPNOTSUPP)
1532 				return (error);
1533 			return (0);
1534 		}
1535 	}
1536 	/*
1537 	 * If we are not suspended or have not yet reached suspended
1538 	 * mode, then let the operation proceed.
1539 	 */
1540 	if ((mp = *mpp) == NULL)
1541 		return (0);
1542 
1543 	/*
1544 	 * VOP_GETWRITEMOUNT() returns with the mp refcount held through
1545 	 * a vfs_ref().
1546 	 * As long as a vnode is not provided we need to acquire a
1547 	 * refcount for the provided mountpoint too, in order to
1548 	 * emulate a vfs_ref().
1549 	 */
1550 	MNT_ILOCK(mp);
1551 	if (vp == NULL)
1552 		MNT_REF(mp);
1553 	if ((mp->mnt_kern_flag & (MNTK_SUSPENDED | MNTK_SUSPEND2)) == 0) {
1554 		mp->mnt_secondary_writes++;
1555 		mp->mnt_secondary_accwrites++;
1556 		MNT_IUNLOCK(mp);
1557 		return (0);
1558 	}
1559 	if (flags & V_NOWAIT) {
1560 		MNT_REL(mp);
1561 		MNT_IUNLOCK(mp);
1562 		return (EWOULDBLOCK);
1563 	}
1564 	/*
1565 	 * Wait for the suspension to finish.
1566 	 */
1567 	error = msleep(&mp->mnt_flag, MNT_MTX(mp),
1568 		       (PUSER - 1) | (flags & PCATCH) | PDROP, "suspfs", 0);
1569 	vfs_rel(mp);
1570 	if (error == 0)
1571 		goto retry;
1572 	return (error);
1573 }
1574 
1575 /*
1576  * Filesystem write operation has completed. If we are suspending and this
1577  * operation is the last one, notify the suspender that the suspension is
1578  * now in effect.
1579  */
1580 void
1581 vn_finished_write(mp)
1582 	struct mount *mp;
1583 {
1584 	if (mp == NULL)
1585 		return;
1586 	MNT_ILOCK(mp);
1587 	MNT_REL(mp);
1588 	mp->mnt_writeopcount--;
1589 	if (mp->mnt_writeopcount < 0)
1590 		panic("vn_finished_write: neg cnt");
1591 	if ((mp->mnt_kern_flag & MNTK_SUSPEND) != 0 &&
1592 	    mp->mnt_writeopcount <= 0)
1593 		wakeup(&mp->mnt_writeopcount);
1594 	MNT_IUNLOCK(mp);
1595 }
1596 
1597 
1598 /*
1599  * Filesystem secondary write operation has completed. If we are
1600  * suspending and this operation is the last one, notify the suspender
1601  * that the suspension is now in effect.
1602  */
1603 void
1604 vn_finished_secondary_write(mp)
1605 	struct mount *mp;
1606 {
1607 	if (mp == NULL)
1608 		return;
1609 	MNT_ILOCK(mp);
1610 	MNT_REL(mp);
1611 	mp->mnt_secondary_writes--;
1612 	if (mp->mnt_secondary_writes < 0)
1613 		panic("vn_finished_secondary_write: neg cnt");
1614 	if ((mp->mnt_kern_flag & MNTK_SUSPEND) != 0 &&
1615 	    mp->mnt_secondary_writes <= 0)
1616 		wakeup(&mp->mnt_secondary_writes);
1617 	MNT_IUNLOCK(mp);
1618 }
1619 
1620 
1621 
1622 /*
1623  * Request a filesystem to suspend write operations.
1624  */
1625 int
1626 vfs_write_suspend(mp)
1627 	struct mount *mp;
1628 {
1629 	int error;
1630 
1631 	MNT_ILOCK(mp);
1632 	if (mp->mnt_susp_owner == curthread) {
1633 		MNT_IUNLOCK(mp);
1634 		return (EALREADY);
1635 	}
1636 	while (mp->mnt_kern_flag & MNTK_SUSPEND)
1637 		msleep(&mp->mnt_flag, MNT_MTX(mp), PUSER - 1, "wsuspfs", 0);
1638 	mp->mnt_kern_flag |= MNTK_SUSPEND;
1639 	mp->mnt_susp_owner = curthread;
1640 	if (mp->mnt_writeopcount > 0)
1641 		(void) msleep(&mp->mnt_writeopcount,
1642 		    MNT_MTX(mp), (PUSER - 1)|PDROP, "suspwt", 0);
1643 	else
1644 		MNT_IUNLOCK(mp);
1645 	if ((error = VFS_SYNC(mp, MNT_SUSPEND)) != 0)
1646 		vfs_write_resume(mp, 0);
1647 	return (error);
1648 }
1649 
1650 /*
1651  * Request a filesystem to resume write operations.
1652  */
1653 void
1654 vfs_write_resume(struct mount *mp, int flags)
1655 {
1656 
1657 	MNT_ILOCK(mp);
1658 	if ((mp->mnt_kern_flag & MNTK_SUSPEND) != 0) {
1659 		KASSERT(mp->mnt_susp_owner == curthread, ("mnt_susp_owner"));
1660 		mp->mnt_kern_flag &= ~(MNTK_SUSPEND | MNTK_SUSPEND2 |
1661 				       MNTK_SUSPENDED);
1662 		mp->mnt_susp_owner = NULL;
1663 		wakeup(&mp->mnt_writeopcount);
1664 		wakeup(&mp->mnt_flag);
1665 		curthread->td_pflags &= ~TDP_IGNSUSP;
1666 		if ((flags & VR_START_WRITE) != 0) {
1667 			MNT_REF(mp);
1668 			mp->mnt_writeopcount++;
1669 		}
1670 		MNT_IUNLOCK(mp);
1671 		if ((flags & VR_NO_SUSPCLR) == 0)
1672 			VFS_SUSP_CLEAN(mp);
1673 	} else if ((flags & VR_START_WRITE) != 0) {
1674 		MNT_REF(mp);
1675 		vn_start_write_locked(mp, 0);
1676 	} else {
1677 		MNT_IUNLOCK(mp);
1678 	}
1679 }
1680 
1681 /*
1682  * Implement kqueues for files by translating it to vnode operation.
1683  */
1684 static int
1685 vn_kqfilter(struct file *fp, struct knote *kn)
1686 {
1687 
1688 	return (VOP_KQFILTER(fp->f_vnode, kn));
1689 }
1690 
1691 /*
1692  * Simplified in-kernel wrapper calls for extended attribute access.
1693  * Both calls pass in a NULL credential, authorizing as "kernel" access.
1694  * Set IO_NODELOCKED in ioflg if the vnode is already locked.
1695  */
1696 int
1697 vn_extattr_get(struct vnode *vp, int ioflg, int attrnamespace,
1698     const char *attrname, int *buflen, char *buf, struct thread *td)
1699 {
1700 	struct uio	auio;
1701 	struct iovec	iov;
1702 	int	error;
1703 
1704 	iov.iov_len = *buflen;
1705 	iov.iov_base = buf;
1706 
1707 	auio.uio_iov = &iov;
1708 	auio.uio_iovcnt = 1;
1709 	auio.uio_rw = UIO_READ;
1710 	auio.uio_segflg = UIO_SYSSPACE;
1711 	auio.uio_td = td;
1712 	auio.uio_offset = 0;
1713 	auio.uio_resid = *buflen;
1714 
1715 	if ((ioflg & IO_NODELOCKED) == 0)
1716 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1717 
1718 	ASSERT_VOP_LOCKED(vp, "IO_NODELOCKED with no vp lock held");
1719 
1720 	/* authorize attribute retrieval as kernel */
1721 	error = VOP_GETEXTATTR(vp, attrnamespace, attrname, &auio, NULL, NULL,
1722 	    td);
1723 
1724 	if ((ioflg & IO_NODELOCKED) == 0)
1725 		VOP_UNLOCK(vp, 0);
1726 
1727 	if (error == 0) {
1728 		*buflen = *buflen - auio.uio_resid;
1729 	}
1730 
1731 	return (error);
1732 }
1733 
1734 /*
1735  * XXX failure mode if partially written?
1736  */
1737 int
1738 vn_extattr_set(struct vnode *vp, int ioflg, int attrnamespace,
1739     const char *attrname, int buflen, char *buf, struct thread *td)
1740 {
1741 	struct uio	auio;
1742 	struct iovec	iov;
1743 	struct mount	*mp;
1744 	int	error;
1745 
1746 	iov.iov_len = buflen;
1747 	iov.iov_base = buf;
1748 
1749 	auio.uio_iov = &iov;
1750 	auio.uio_iovcnt = 1;
1751 	auio.uio_rw = UIO_WRITE;
1752 	auio.uio_segflg = UIO_SYSSPACE;
1753 	auio.uio_td = td;
1754 	auio.uio_offset = 0;
1755 	auio.uio_resid = buflen;
1756 
1757 	if ((ioflg & IO_NODELOCKED) == 0) {
1758 		if ((error = vn_start_write(vp, &mp, V_WAIT)) != 0)
1759 			return (error);
1760 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1761 	}
1762 
1763 	ASSERT_VOP_LOCKED(vp, "IO_NODELOCKED with no vp lock held");
1764 
1765 	/* authorize attribute setting as kernel */
1766 	error = VOP_SETEXTATTR(vp, attrnamespace, attrname, &auio, NULL, td);
1767 
1768 	if ((ioflg & IO_NODELOCKED) == 0) {
1769 		vn_finished_write(mp);
1770 		VOP_UNLOCK(vp, 0);
1771 	}
1772 
1773 	return (error);
1774 }
1775 
1776 int
1777 vn_extattr_rm(struct vnode *vp, int ioflg, int attrnamespace,
1778     const char *attrname, struct thread *td)
1779 {
1780 	struct mount	*mp;
1781 	int	error;
1782 
1783 	if ((ioflg & IO_NODELOCKED) == 0) {
1784 		if ((error = vn_start_write(vp, &mp, V_WAIT)) != 0)
1785 			return (error);
1786 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1787 	}
1788 
1789 	ASSERT_VOP_LOCKED(vp, "IO_NODELOCKED with no vp lock held");
1790 
1791 	/* authorize attribute removal as kernel */
1792 	error = VOP_DELETEEXTATTR(vp, attrnamespace, attrname, NULL, td);
1793 	if (error == EOPNOTSUPP)
1794 		error = VOP_SETEXTATTR(vp, attrnamespace, attrname, NULL,
1795 		    NULL, td);
1796 
1797 	if ((ioflg & IO_NODELOCKED) == 0) {
1798 		vn_finished_write(mp);
1799 		VOP_UNLOCK(vp, 0);
1800 	}
1801 
1802 	return (error);
1803 }
1804 
1805 int
1806 vn_vget_ino(struct vnode *vp, ino_t ino, int lkflags, struct vnode **rvp)
1807 {
1808 	struct mount *mp;
1809 	int ltype, error;
1810 
1811 	mp = vp->v_mount;
1812 	ltype = VOP_ISLOCKED(vp);
1813 	KASSERT(ltype == LK_EXCLUSIVE || ltype == LK_SHARED,
1814 	    ("vn_vget_ino: vp not locked"));
1815 	error = vfs_busy(mp, MBF_NOWAIT);
1816 	if (error != 0) {
1817 		vfs_ref(mp);
1818 		VOP_UNLOCK(vp, 0);
1819 		error = vfs_busy(mp, 0);
1820 		vn_lock(vp, ltype | LK_RETRY);
1821 		vfs_rel(mp);
1822 		if (error != 0)
1823 			return (ENOENT);
1824 		if (vp->v_iflag & VI_DOOMED) {
1825 			vfs_unbusy(mp);
1826 			return (ENOENT);
1827 		}
1828 	}
1829 	VOP_UNLOCK(vp, 0);
1830 	error = VFS_VGET(mp, ino, lkflags, rvp);
1831 	vfs_unbusy(mp);
1832 	vn_lock(vp, ltype | LK_RETRY);
1833 	if (vp->v_iflag & VI_DOOMED) {
1834 		if (error == 0)
1835 			vput(*rvp);
1836 		error = ENOENT;
1837 	}
1838 	return (error);
1839 }
1840 
1841 int
1842 vn_rlimit_fsize(const struct vnode *vp, const struct uio *uio,
1843     const struct thread *td)
1844 {
1845 
1846 	if (vp->v_type != VREG || td == NULL)
1847 		return (0);
1848 	PROC_LOCK(td->td_proc);
1849 	if ((uoff_t)uio->uio_offset + uio->uio_resid >
1850 	    lim_cur(td->td_proc, RLIMIT_FSIZE)) {
1851 		kern_psignal(td->td_proc, SIGXFSZ);
1852 		PROC_UNLOCK(td->td_proc);
1853 		return (EFBIG);
1854 	}
1855 	PROC_UNLOCK(td->td_proc);
1856 	return (0);
1857 }
1858 
1859 int
1860 vn_chmod(struct file *fp, mode_t mode, struct ucred *active_cred,
1861     struct thread *td)
1862 {
1863 	struct vnode *vp;
1864 
1865 	vp = fp->f_vnode;
1866 #ifdef AUDIT
1867 	vn_lock(vp, LK_SHARED | LK_RETRY);
1868 	AUDIT_ARG_VNODE1(vp);
1869 	VOP_UNLOCK(vp, 0);
1870 #endif
1871 	return (setfmode(td, active_cred, vp, mode));
1872 }
1873 
1874 int
1875 vn_chown(struct file *fp, uid_t uid, gid_t gid, struct ucred *active_cred,
1876     struct thread *td)
1877 {
1878 	struct vnode *vp;
1879 
1880 	vp = fp->f_vnode;
1881 #ifdef AUDIT
1882 	vn_lock(vp, LK_SHARED | LK_RETRY);
1883 	AUDIT_ARG_VNODE1(vp);
1884 	VOP_UNLOCK(vp, 0);
1885 #endif
1886 	return (setfown(td, active_cred, vp, uid, gid));
1887 }
1888 
1889 void
1890 vn_pages_remove(struct vnode *vp, vm_pindex_t start, vm_pindex_t end)
1891 {
1892 	vm_object_t object;
1893 
1894 	if ((object = vp->v_object) == NULL)
1895 		return;
1896 	VM_OBJECT_WLOCK(object);
1897 	vm_object_page_remove(object, start, end, 0);
1898 	VM_OBJECT_WUNLOCK(object);
1899 }
1900 
1901 int
1902 vn_bmap_seekhole(struct vnode *vp, u_long cmd, off_t *off, struct ucred *cred)
1903 {
1904 	struct vattr va;
1905 	daddr_t bn, bnp;
1906 	uint64_t bsize;
1907 	off_t noff;
1908 	int error;
1909 
1910 	KASSERT(cmd == FIOSEEKHOLE || cmd == FIOSEEKDATA,
1911 	    ("Wrong command %lu", cmd));
1912 
1913 	if (vn_lock(vp, LK_SHARED) != 0)
1914 		return (EBADF);
1915 	if (vp->v_type != VREG) {
1916 		error = ENOTTY;
1917 		goto unlock;
1918 	}
1919 	error = VOP_GETATTR(vp, &va, cred);
1920 	if (error != 0)
1921 		goto unlock;
1922 	noff = *off;
1923 	if (noff >= va.va_size) {
1924 		error = ENXIO;
1925 		goto unlock;
1926 	}
1927 	bsize = vp->v_mount->mnt_stat.f_iosize;
1928 	for (bn = noff / bsize; noff < va.va_size; bn++, noff += bsize) {
1929 		error = VOP_BMAP(vp, bn, NULL, &bnp, NULL, NULL);
1930 		if (error == EOPNOTSUPP) {
1931 			error = ENOTTY;
1932 			goto unlock;
1933 		}
1934 		if ((bnp == -1 && cmd == FIOSEEKHOLE) ||
1935 		    (bnp != -1 && cmd == FIOSEEKDATA)) {
1936 			noff = bn * bsize;
1937 			if (noff < *off)
1938 				noff = *off;
1939 			goto unlock;
1940 		}
1941 	}
1942 	if (noff > va.va_size)
1943 		noff = va.va_size;
1944 	/* noff == va.va_size. There is an implicit hole at the end of file. */
1945 	if (cmd == FIOSEEKDATA)
1946 		error = ENXIO;
1947 unlock:
1948 	VOP_UNLOCK(vp, 0);
1949 	if (error == 0)
1950 		*off = noff;
1951 	return (error);
1952 }
1953