xref: /freebsd/sys/kern/vfs_vnops.c (revision 190cef3d)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1982, 1986, 1989, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  * (c) UNIX System Laboratories, Inc.
7  * All or some portions of this file are derived from material licensed
8  * to the University of California by American Telephone and Telegraph
9  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10  * the permission of UNIX System Laboratories, Inc.
11  *
12  * Copyright (c) 2012 Konstantin Belousov <kib@FreeBSD.org>
13  * Copyright (c) 2013, 2014 The FreeBSD Foundation
14  *
15  * Portions of this software were developed by Konstantin Belousov
16  * under sponsorship from the FreeBSD Foundation.
17  *
18  * Redistribution and use in source and binary forms, with or without
19  * modification, are permitted provided that the following conditions
20  * are met:
21  * 1. Redistributions of source code must retain the above copyright
22  *    notice, this list of conditions and the following disclaimer.
23  * 2. Redistributions in binary form must reproduce the above copyright
24  *    notice, this list of conditions and the following disclaimer in the
25  *    documentation and/or other materials provided with the distribution.
26  * 3. Neither the name of the University nor the names of its contributors
27  *    may be used to endorse or promote products derived from this software
28  *    without specific prior written permission.
29  *
30  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
31  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
34  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
39  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40  * SUCH DAMAGE.
41  *
42  *	@(#)vfs_vnops.c	8.2 (Berkeley) 1/21/94
43  */
44 
45 #include <sys/cdefs.h>
46 __FBSDID("$FreeBSD$");
47 
48 #include "opt_hwpmc_hooks.h"
49 
50 #include <sys/param.h>
51 #include <sys/systm.h>
52 #include <sys/disk.h>
53 #include <sys/fail.h>
54 #include <sys/fcntl.h>
55 #include <sys/file.h>
56 #include <sys/kdb.h>
57 #include <sys/stat.h>
58 #include <sys/priv.h>
59 #include <sys/proc.h>
60 #include <sys/limits.h>
61 #include <sys/lock.h>
62 #include <sys/mman.h>
63 #include <sys/mount.h>
64 #include <sys/mutex.h>
65 #include <sys/namei.h>
66 #include <sys/vnode.h>
67 #include <sys/bio.h>
68 #include <sys/buf.h>
69 #include <sys/filio.h>
70 #include <sys/resourcevar.h>
71 #include <sys/rwlock.h>
72 #include <sys/sx.h>
73 #include <sys/sysctl.h>
74 #include <sys/ttycom.h>
75 #include <sys/conf.h>
76 #include <sys/syslog.h>
77 #include <sys/unistd.h>
78 #include <sys/user.h>
79 
80 #include <security/audit/audit.h>
81 #include <security/mac/mac_framework.h>
82 
83 #include <vm/vm.h>
84 #include <vm/vm_extern.h>
85 #include <vm/pmap.h>
86 #include <vm/vm_map.h>
87 #include <vm/vm_object.h>
88 #include <vm/vm_page.h>
89 #include <vm/vnode_pager.h>
90 
91 #ifdef HWPMC_HOOKS
92 #include <sys/pmckern.h>
93 #endif
94 
95 static fo_rdwr_t	vn_read;
96 static fo_rdwr_t	vn_write;
97 static fo_rdwr_t	vn_io_fault;
98 static fo_truncate_t	vn_truncate;
99 static fo_ioctl_t	vn_ioctl;
100 static fo_poll_t	vn_poll;
101 static fo_kqfilter_t	vn_kqfilter;
102 static fo_stat_t	vn_statfile;
103 static fo_close_t	vn_closefile;
104 static fo_mmap_t	vn_mmap;
105 
106 struct 	fileops vnops = {
107 	.fo_read = vn_io_fault,
108 	.fo_write = vn_io_fault,
109 	.fo_truncate = vn_truncate,
110 	.fo_ioctl = vn_ioctl,
111 	.fo_poll = vn_poll,
112 	.fo_kqfilter = vn_kqfilter,
113 	.fo_stat = vn_statfile,
114 	.fo_close = vn_closefile,
115 	.fo_chmod = vn_chmod,
116 	.fo_chown = vn_chown,
117 	.fo_sendfile = vn_sendfile,
118 	.fo_seek = vn_seek,
119 	.fo_fill_kinfo = vn_fill_kinfo,
120 	.fo_mmap = vn_mmap,
121 	.fo_flags = DFLAG_PASSABLE | DFLAG_SEEKABLE
122 };
123 
124 static const int io_hold_cnt = 16;
125 static int vn_io_fault_enable = 1;
126 SYSCTL_INT(_debug, OID_AUTO, vn_io_fault_enable, CTLFLAG_RW,
127     &vn_io_fault_enable, 0, "Enable vn_io_fault lock avoidance");
128 static int vn_io_fault_prefault = 0;
129 SYSCTL_INT(_debug, OID_AUTO, vn_io_fault_prefault, CTLFLAG_RW,
130     &vn_io_fault_prefault, 0, "Enable vn_io_fault prefaulting");
131 static u_long vn_io_faults_cnt;
132 SYSCTL_ULONG(_debug, OID_AUTO, vn_io_faults, CTLFLAG_RD,
133     &vn_io_faults_cnt, 0, "Count of vn_io_fault lock avoidance triggers");
134 
135 /*
136  * Returns true if vn_io_fault mode of handling the i/o request should
137  * be used.
138  */
139 static bool
140 do_vn_io_fault(struct vnode *vp, struct uio *uio)
141 {
142 	struct mount *mp;
143 
144 	return (uio->uio_segflg == UIO_USERSPACE && vp->v_type == VREG &&
145 	    (mp = vp->v_mount) != NULL &&
146 	    (mp->mnt_kern_flag & MNTK_NO_IOPF) != 0 && vn_io_fault_enable);
147 }
148 
149 /*
150  * Structure used to pass arguments to vn_io_fault1(), to do either
151  * file- or vnode-based I/O calls.
152  */
153 struct vn_io_fault_args {
154 	enum {
155 		VN_IO_FAULT_FOP,
156 		VN_IO_FAULT_VOP
157 	} kind;
158 	struct ucred *cred;
159 	int flags;
160 	union {
161 		struct fop_args_tag {
162 			struct file *fp;
163 			fo_rdwr_t *doio;
164 		} fop_args;
165 		struct vop_args_tag {
166 			struct vnode *vp;
167 		} vop_args;
168 	} args;
169 };
170 
171 static int vn_io_fault1(struct vnode *vp, struct uio *uio,
172     struct vn_io_fault_args *args, struct thread *td);
173 
174 int
175 vn_open(struct nameidata *ndp, int *flagp, int cmode, struct file *fp)
176 {
177 	struct thread *td = ndp->ni_cnd.cn_thread;
178 
179 	return (vn_open_cred(ndp, flagp, cmode, 0, td->td_ucred, fp));
180 }
181 
182 /*
183  * Common code for vnode open operations via a name lookup.
184  * Lookup the vnode and invoke VOP_CREATE if needed.
185  * Check permissions, and call the VOP_OPEN or VOP_CREATE routine.
186  *
187  * Note that this does NOT free nameidata for the successful case,
188  * due to the NDINIT being done elsewhere.
189  */
190 int
191 vn_open_cred(struct nameidata *ndp, int *flagp, int cmode, u_int vn_open_flags,
192     struct ucred *cred, struct file *fp)
193 {
194 	struct vnode *vp;
195 	struct mount *mp;
196 	struct thread *td = ndp->ni_cnd.cn_thread;
197 	struct vattr vat;
198 	struct vattr *vap = &vat;
199 	int fmode, error;
200 
201 restart:
202 	fmode = *flagp;
203 	if ((fmode & (O_CREAT | O_EXCL | O_DIRECTORY)) == (O_CREAT |
204 	    O_EXCL | O_DIRECTORY))
205 		return (EINVAL);
206 	else if ((fmode & (O_CREAT | O_DIRECTORY)) == O_CREAT) {
207 		ndp->ni_cnd.cn_nameiop = CREATE;
208 		/*
209 		 * Set NOCACHE to avoid flushing the cache when
210 		 * rolling in many files at once.
211 		*/
212 		ndp->ni_cnd.cn_flags = ISOPEN | LOCKPARENT | LOCKLEAF | NOCACHE;
213 		if ((fmode & O_EXCL) == 0 && (fmode & O_NOFOLLOW) == 0)
214 			ndp->ni_cnd.cn_flags |= FOLLOW;
215 		if (!(vn_open_flags & VN_OPEN_NOAUDIT))
216 			ndp->ni_cnd.cn_flags |= AUDITVNODE1;
217 		if (vn_open_flags & VN_OPEN_NOCAPCHECK)
218 			ndp->ni_cnd.cn_flags |= NOCAPCHECK;
219 		bwillwrite();
220 		if ((error = namei(ndp)) != 0)
221 			return (error);
222 		if (ndp->ni_vp == NULL) {
223 			VATTR_NULL(vap);
224 			vap->va_type = VREG;
225 			vap->va_mode = cmode;
226 			if (fmode & O_EXCL)
227 				vap->va_vaflags |= VA_EXCLUSIVE;
228 			if (vn_start_write(ndp->ni_dvp, &mp, V_NOWAIT) != 0) {
229 				NDFREE(ndp, NDF_ONLY_PNBUF);
230 				vput(ndp->ni_dvp);
231 				if ((error = vn_start_write(NULL, &mp,
232 				    V_XSLEEP | PCATCH)) != 0)
233 					return (error);
234 				goto restart;
235 			}
236 			if ((vn_open_flags & VN_OPEN_NAMECACHE) != 0)
237 				ndp->ni_cnd.cn_flags |= MAKEENTRY;
238 #ifdef MAC
239 			error = mac_vnode_check_create(cred, ndp->ni_dvp,
240 			    &ndp->ni_cnd, vap);
241 			if (error == 0)
242 #endif
243 				error = VOP_CREATE(ndp->ni_dvp, &ndp->ni_vp,
244 						   &ndp->ni_cnd, vap);
245 			vput(ndp->ni_dvp);
246 			vn_finished_write(mp);
247 			if (error) {
248 				NDFREE(ndp, NDF_ONLY_PNBUF);
249 				return (error);
250 			}
251 			fmode &= ~O_TRUNC;
252 			vp = ndp->ni_vp;
253 		} else {
254 			if (ndp->ni_dvp == ndp->ni_vp)
255 				vrele(ndp->ni_dvp);
256 			else
257 				vput(ndp->ni_dvp);
258 			ndp->ni_dvp = NULL;
259 			vp = ndp->ni_vp;
260 			if (fmode & O_EXCL) {
261 				error = EEXIST;
262 				goto bad;
263 			}
264 			fmode &= ~O_CREAT;
265 		}
266 	} else {
267 		ndp->ni_cnd.cn_nameiop = LOOKUP;
268 		ndp->ni_cnd.cn_flags = ISOPEN |
269 		    ((fmode & O_NOFOLLOW) ? NOFOLLOW : FOLLOW) | LOCKLEAF;
270 		if (!(fmode & FWRITE))
271 			ndp->ni_cnd.cn_flags |= LOCKSHARED;
272 		if (!(vn_open_flags & VN_OPEN_NOAUDIT))
273 			ndp->ni_cnd.cn_flags |= AUDITVNODE1;
274 		if (vn_open_flags & VN_OPEN_NOCAPCHECK)
275 			ndp->ni_cnd.cn_flags |= NOCAPCHECK;
276 		if ((error = namei(ndp)) != 0)
277 			return (error);
278 		vp = ndp->ni_vp;
279 	}
280 	error = vn_open_vnode(vp, fmode, cred, td, fp);
281 	if (error)
282 		goto bad;
283 	*flagp = fmode;
284 	return (0);
285 bad:
286 	NDFREE(ndp, NDF_ONLY_PNBUF);
287 	vput(vp);
288 	*flagp = fmode;
289 	ndp->ni_vp = NULL;
290 	return (error);
291 }
292 
293 /*
294  * Common code for vnode open operations once a vnode is located.
295  * Check permissions, and call the VOP_OPEN routine.
296  */
297 int
298 vn_open_vnode(struct vnode *vp, int fmode, struct ucred *cred,
299     struct thread *td, struct file *fp)
300 {
301 	accmode_t accmode;
302 	struct flock lf;
303 	int error, lock_flags, type;
304 
305 	if (vp->v_type == VLNK)
306 		return (EMLINK);
307 	if (vp->v_type == VSOCK)
308 		return (EOPNOTSUPP);
309 	if (vp->v_type != VDIR && fmode & O_DIRECTORY)
310 		return (ENOTDIR);
311 	accmode = 0;
312 	if (fmode & (FWRITE | O_TRUNC)) {
313 		if (vp->v_type == VDIR)
314 			return (EISDIR);
315 		accmode |= VWRITE;
316 	}
317 	if (fmode & FREAD)
318 		accmode |= VREAD;
319 	if (fmode & FEXEC)
320 		accmode |= VEXEC;
321 	if ((fmode & O_APPEND) && (fmode & FWRITE))
322 		accmode |= VAPPEND;
323 #ifdef MAC
324 	if (fmode & O_CREAT)
325 		accmode |= VCREAT;
326 	if (fmode & O_VERIFY)
327 		accmode |= VVERIFY;
328 	error = mac_vnode_check_open(cred, vp, accmode);
329 	if (error)
330 		return (error);
331 
332 	accmode &= ~(VCREAT | VVERIFY);
333 #endif
334 	if ((fmode & O_CREAT) == 0) {
335 		if (accmode & VWRITE) {
336 			error = vn_writechk(vp);
337 			if (error)
338 				return (error);
339 		}
340 		if (accmode) {
341 		        error = VOP_ACCESS(vp, accmode, cred, td);
342 			if (error)
343 				return (error);
344 		}
345 	}
346 	if (vp->v_type == VFIFO && VOP_ISLOCKED(vp) != LK_EXCLUSIVE)
347 		vn_lock(vp, LK_UPGRADE | LK_RETRY);
348 	if ((error = VOP_OPEN(vp, fmode, cred, td, fp)) != 0)
349 		return (error);
350 
351 	while ((fmode & (O_EXLOCK | O_SHLOCK)) != 0) {
352 		KASSERT(fp != NULL, ("open with flock requires fp"));
353 		if (fp->f_type != DTYPE_NONE && fp->f_type != DTYPE_VNODE) {
354 			error = EOPNOTSUPP;
355 			break;
356 		}
357 		lock_flags = VOP_ISLOCKED(vp);
358 		VOP_UNLOCK(vp, 0);
359 		lf.l_whence = SEEK_SET;
360 		lf.l_start = 0;
361 		lf.l_len = 0;
362 		if (fmode & O_EXLOCK)
363 			lf.l_type = F_WRLCK;
364 		else
365 			lf.l_type = F_RDLCK;
366 		type = F_FLOCK;
367 		if ((fmode & FNONBLOCK) == 0)
368 			type |= F_WAIT;
369 		error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type);
370 		if (error == 0)
371 			fp->f_flag |= FHASLOCK;
372 		vn_lock(vp, lock_flags | LK_RETRY);
373 		if (error != 0)
374 			break;
375 		if ((vp->v_iflag & VI_DOOMED) != 0) {
376 			error = ENOENT;
377 			break;
378 		}
379 
380 		/*
381 		 * Another thread might have used this vnode as an
382 		 * executable while the vnode lock was dropped.
383 		 * Ensure the vnode is still able to be opened for
384 		 * writing after the lock has been obtained.
385 		 */
386 		if ((accmode & VWRITE) != 0)
387 			error = vn_writechk(vp);
388 		break;
389 	}
390 
391 	if (error != 0) {
392 		fp->f_flag |= FOPENFAILED;
393 		fp->f_vnode = vp;
394 		if (fp->f_ops == &badfileops) {
395 			fp->f_type = DTYPE_VNODE;
396 			fp->f_ops = &vnops;
397 		}
398 		vref(vp);
399 	} else if  ((fmode & FWRITE) != 0) {
400 		VOP_ADD_WRITECOUNT(vp, 1);
401 		CTR3(KTR_VFS, "%s: vp %p v_writecount increased to %d",
402 		    __func__, vp, vp->v_writecount);
403 	}
404 	ASSERT_VOP_LOCKED(vp, "vn_open_vnode");
405 	return (error);
406 }
407 
408 /*
409  * Check for write permissions on the specified vnode.
410  * Prototype text segments cannot be written.
411  */
412 int
413 vn_writechk(struct vnode *vp)
414 {
415 
416 	ASSERT_VOP_LOCKED(vp, "vn_writechk");
417 	/*
418 	 * If there's shared text associated with
419 	 * the vnode, try to free it up once.  If
420 	 * we fail, we can't allow writing.
421 	 */
422 	if (VOP_IS_TEXT(vp))
423 		return (ETXTBSY);
424 
425 	return (0);
426 }
427 
428 /*
429  * Vnode close call
430  */
431 static int
432 vn_close1(struct vnode *vp, int flags, struct ucred *file_cred,
433     struct thread *td, bool keep_ref)
434 {
435 	struct mount *mp;
436 	int error, lock_flags;
437 
438 	if (vp->v_type != VFIFO && (flags & FWRITE) == 0 &&
439 	    MNT_EXTENDED_SHARED(vp->v_mount))
440 		lock_flags = LK_SHARED;
441 	else
442 		lock_flags = LK_EXCLUSIVE;
443 
444 	vn_start_write(vp, &mp, V_WAIT);
445 	vn_lock(vp, lock_flags | LK_RETRY);
446 	AUDIT_ARG_VNODE1(vp);
447 	if ((flags & (FWRITE | FOPENFAILED)) == FWRITE) {
448 		VNASSERT(vp->v_writecount > 0, vp,
449 		    ("vn_close: negative writecount"));
450 		VOP_ADD_WRITECOUNT(vp, -1);
451 		CTR3(KTR_VFS, "%s: vp %p v_writecount decreased to %d",
452 		    __func__, vp, vp->v_writecount);
453 	}
454 	error = VOP_CLOSE(vp, flags, file_cred, td);
455 	if (keep_ref)
456 		VOP_UNLOCK(vp, 0);
457 	else
458 		vput(vp);
459 	vn_finished_write(mp);
460 	return (error);
461 }
462 
463 int
464 vn_close(struct vnode *vp, int flags, struct ucred *file_cred,
465     struct thread *td)
466 {
467 
468 	return (vn_close1(vp, flags, file_cred, td, false));
469 }
470 
471 /*
472  * Heuristic to detect sequential operation.
473  */
474 static int
475 sequential_heuristic(struct uio *uio, struct file *fp)
476 {
477 
478 	ASSERT_VOP_LOCKED(fp->f_vnode, __func__);
479 	if (fp->f_flag & FRDAHEAD)
480 		return (fp->f_seqcount << IO_SEQSHIFT);
481 
482 	/*
483 	 * Offset 0 is handled specially.  open() sets f_seqcount to 1 so
484 	 * that the first I/O is normally considered to be slightly
485 	 * sequential.  Seeking to offset 0 doesn't change sequentiality
486 	 * unless previous seeks have reduced f_seqcount to 0, in which
487 	 * case offset 0 is not special.
488 	 */
489 	if ((uio->uio_offset == 0 && fp->f_seqcount > 0) ||
490 	    uio->uio_offset == fp->f_nextoff) {
491 		/*
492 		 * f_seqcount is in units of fixed-size blocks so that it
493 		 * depends mainly on the amount of sequential I/O and not
494 		 * much on the number of sequential I/O's.  The fixed size
495 		 * of 16384 is hard-coded here since it is (not quite) just
496 		 * a magic size that works well here.  This size is more
497 		 * closely related to the best I/O size for real disks than
498 		 * to any block size used by software.
499 		 */
500 		fp->f_seqcount += howmany(uio->uio_resid, 16384);
501 		if (fp->f_seqcount > IO_SEQMAX)
502 			fp->f_seqcount = IO_SEQMAX;
503 		return (fp->f_seqcount << IO_SEQSHIFT);
504 	}
505 
506 	/* Not sequential.  Quickly draw-down sequentiality. */
507 	if (fp->f_seqcount > 1)
508 		fp->f_seqcount = 1;
509 	else
510 		fp->f_seqcount = 0;
511 	return (0);
512 }
513 
514 /*
515  * Package up an I/O request on a vnode into a uio and do it.
516  */
517 int
518 vn_rdwr(enum uio_rw rw, struct vnode *vp, void *base, int len, off_t offset,
519     enum uio_seg segflg, int ioflg, struct ucred *active_cred,
520     struct ucred *file_cred, ssize_t *aresid, struct thread *td)
521 {
522 	struct uio auio;
523 	struct iovec aiov;
524 	struct mount *mp;
525 	struct ucred *cred;
526 	void *rl_cookie;
527 	struct vn_io_fault_args args;
528 	int error, lock_flags;
529 
530 	auio.uio_iov = &aiov;
531 	auio.uio_iovcnt = 1;
532 	aiov.iov_base = base;
533 	aiov.iov_len = len;
534 	auio.uio_resid = len;
535 	auio.uio_offset = offset;
536 	auio.uio_segflg = segflg;
537 	auio.uio_rw = rw;
538 	auio.uio_td = td;
539 	error = 0;
540 
541 	if ((ioflg & IO_NODELOCKED) == 0) {
542 		if ((ioflg & IO_RANGELOCKED) == 0) {
543 			if (rw == UIO_READ) {
544 				rl_cookie = vn_rangelock_rlock(vp, offset,
545 				    offset + len);
546 			} else {
547 				rl_cookie = vn_rangelock_wlock(vp, offset,
548 				    offset + len);
549 			}
550 		} else
551 			rl_cookie = NULL;
552 		mp = NULL;
553 		if (rw == UIO_WRITE) {
554 			if (vp->v_type != VCHR &&
555 			    (error = vn_start_write(vp, &mp, V_WAIT | PCATCH))
556 			    != 0)
557 				goto out;
558 			if (MNT_SHARED_WRITES(mp) ||
559 			    ((mp == NULL) && MNT_SHARED_WRITES(vp->v_mount)))
560 				lock_flags = LK_SHARED;
561 			else
562 				lock_flags = LK_EXCLUSIVE;
563 		} else
564 			lock_flags = LK_SHARED;
565 		vn_lock(vp, lock_flags | LK_RETRY);
566 	} else
567 		rl_cookie = NULL;
568 
569 	ASSERT_VOP_LOCKED(vp, "IO_NODELOCKED with no vp lock held");
570 #ifdef MAC
571 	if ((ioflg & IO_NOMACCHECK) == 0) {
572 		if (rw == UIO_READ)
573 			error = mac_vnode_check_read(active_cred, file_cred,
574 			    vp);
575 		else
576 			error = mac_vnode_check_write(active_cred, file_cred,
577 			    vp);
578 	}
579 #endif
580 	if (error == 0) {
581 		if (file_cred != NULL)
582 			cred = file_cred;
583 		else
584 			cred = active_cred;
585 		if (do_vn_io_fault(vp, &auio)) {
586 			args.kind = VN_IO_FAULT_VOP;
587 			args.cred = cred;
588 			args.flags = ioflg;
589 			args.args.vop_args.vp = vp;
590 			error = vn_io_fault1(vp, &auio, &args, td);
591 		} else if (rw == UIO_READ) {
592 			error = VOP_READ(vp, &auio, ioflg, cred);
593 		} else /* if (rw == UIO_WRITE) */ {
594 			error = VOP_WRITE(vp, &auio, ioflg, cred);
595 		}
596 	}
597 	if (aresid)
598 		*aresid = auio.uio_resid;
599 	else
600 		if (auio.uio_resid && error == 0)
601 			error = EIO;
602 	if ((ioflg & IO_NODELOCKED) == 0) {
603 		VOP_UNLOCK(vp, 0);
604 		if (mp != NULL)
605 			vn_finished_write(mp);
606 	}
607  out:
608 	if (rl_cookie != NULL)
609 		vn_rangelock_unlock(vp, rl_cookie);
610 	return (error);
611 }
612 
613 /*
614  * Package up an I/O request on a vnode into a uio and do it.  The I/O
615  * request is split up into smaller chunks and we try to avoid saturating
616  * the buffer cache while potentially holding a vnode locked, so we
617  * check bwillwrite() before calling vn_rdwr().  We also call kern_yield()
618  * to give other processes a chance to lock the vnode (either other processes
619  * core'ing the same binary, or unrelated processes scanning the directory).
620  */
621 int
622 vn_rdwr_inchunks(enum uio_rw rw, struct vnode *vp, void *base, size_t len,
623     off_t offset, enum uio_seg segflg, int ioflg, struct ucred *active_cred,
624     struct ucred *file_cred, size_t *aresid, struct thread *td)
625 {
626 	int error = 0;
627 	ssize_t iaresid;
628 
629 	do {
630 		int chunk;
631 
632 		/*
633 		 * Force `offset' to a multiple of MAXBSIZE except possibly
634 		 * for the first chunk, so that filesystems only need to
635 		 * write full blocks except possibly for the first and last
636 		 * chunks.
637 		 */
638 		chunk = MAXBSIZE - (uoff_t)offset % MAXBSIZE;
639 
640 		if (chunk > len)
641 			chunk = len;
642 		if (rw != UIO_READ && vp->v_type == VREG)
643 			bwillwrite();
644 		iaresid = 0;
645 		error = vn_rdwr(rw, vp, base, chunk, offset, segflg,
646 		    ioflg, active_cred, file_cred, &iaresid, td);
647 		len -= chunk;	/* aresid calc already includes length */
648 		if (error)
649 			break;
650 		offset += chunk;
651 		base = (char *)base + chunk;
652 		kern_yield(PRI_USER);
653 	} while (len);
654 	if (aresid)
655 		*aresid = len + iaresid;
656 	return (error);
657 }
658 
659 off_t
660 foffset_lock(struct file *fp, int flags)
661 {
662 	struct mtx *mtxp;
663 	off_t res;
664 
665 	KASSERT((flags & FOF_OFFSET) == 0, ("FOF_OFFSET passed"));
666 
667 #if OFF_MAX <= LONG_MAX
668 	/*
669 	 * Caller only wants the current f_offset value.  Assume that
670 	 * the long and shorter integer types reads are atomic.
671 	 */
672 	if ((flags & FOF_NOLOCK) != 0)
673 		return (fp->f_offset);
674 #endif
675 
676 	/*
677 	 * According to McKusick the vn lock was protecting f_offset here.
678 	 * It is now protected by the FOFFSET_LOCKED flag.
679 	 */
680 	mtxp = mtx_pool_find(mtxpool_sleep, fp);
681 	mtx_lock(mtxp);
682 	if ((flags & FOF_NOLOCK) == 0) {
683 		while (fp->f_vnread_flags & FOFFSET_LOCKED) {
684 			fp->f_vnread_flags |= FOFFSET_LOCK_WAITING;
685 			msleep(&fp->f_vnread_flags, mtxp, PUSER -1,
686 			    "vofflock", 0);
687 		}
688 		fp->f_vnread_flags |= FOFFSET_LOCKED;
689 	}
690 	res = fp->f_offset;
691 	mtx_unlock(mtxp);
692 	return (res);
693 }
694 
695 void
696 foffset_unlock(struct file *fp, off_t val, int flags)
697 {
698 	struct mtx *mtxp;
699 
700 	KASSERT((flags & FOF_OFFSET) == 0, ("FOF_OFFSET passed"));
701 
702 #if OFF_MAX <= LONG_MAX
703 	if ((flags & FOF_NOLOCK) != 0) {
704 		if ((flags & FOF_NOUPDATE) == 0)
705 			fp->f_offset = val;
706 		if ((flags & FOF_NEXTOFF) != 0)
707 			fp->f_nextoff = val;
708 		return;
709 	}
710 #endif
711 
712 	mtxp = mtx_pool_find(mtxpool_sleep, fp);
713 	mtx_lock(mtxp);
714 	if ((flags & FOF_NOUPDATE) == 0)
715 		fp->f_offset = val;
716 	if ((flags & FOF_NEXTOFF) != 0)
717 		fp->f_nextoff = val;
718 	if ((flags & FOF_NOLOCK) == 0) {
719 		KASSERT((fp->f_vnread_flags & FOFFSET_LOCKED) != 0,
720 		    ("Lost FOFFSET_LOCKED"));
721 		if (fp->f_vnread_flags & FOFFSET_LOCK_WAITING)
722 			wakeup(&fp->f_vnread_flags);
723 		fp->f_vnread_flags = 0;
724 	}
725 	mtx_unlock(mtxp);
726 }
727 
728 void
729 foffset_lock_uio(struct file *fp, struct uio *uio, int flags)
730 {
731 
732 	if ((flags & FOF_OFFSET) == 0)
733 		uio->uio_offset = foffset_lock(fp, flags);
734 }
735 
736 void
737 foffset_unlock_uio(struct file *fp, struct uio *uio, int flags)
738 {
739 
740 	if ((flags & FOF_OFFSET) == 0)
741 		foffset_unlock(fp, uio->uio_offset, flags);
742 }
743 
744 static int
745 get_advice(struct file *fp, struct uio *uio)
746 {
747 	struct mtx *mtxp;
748 	int ret;
749 
750 	ret = POSIX_FADV_NORMAL;
751 	if (fp->f_advice == NULL || fp->f_vnode->v_type != VREG)
752 		return (ret);
753 
754 	mtxp = mtx_pool_find(mtxpool_sleep, fp);
755 	mtx_lock(mtxp);
756 	if (fp->f_advice != NULL &&
757 	    uio->uio_offset >= fp->f_advice->fa_start &&
758 	    uio->uio_offset + uio->uio_resid <= fp->f_advice->fa_end)
759 		ret = fp->f_advice->fa_advice;
760 	mtx_unlock(mtxp);
761 	return (ret);
762 }
763 
764 /*
765  * File table vnode read routine.
766  */
767 static int
768 vn_read(struct file *fp, struct uio *uio, struct ucred *active_cred, int flags,
769     struct thread *td)
770 {
771 	struct vnode *vp;
772 	off_t orig_offset;
773 	int error, ioflag;
774 	int advice;
775 
776 	KASSERT(uio->uio_td == td, ("uio_td %p is not td %p",
777 	    uio->uio_td, td));
778 	KASSERT(flags & FOF_OFFSET, ("No FOF_OFFSET"));
779 	vp = fp->f_vnode;
780 	ioflag = 0;
781 	if (fp->f_flag & FNONBLOCK)
782 		ioflag |= IO_NDELAY;
783 	if (fp->f_flag & O_DIRECT)
784 		ioflag |= IO_DIRECT;
785 	advice = get_advice(fp, uio);
786 	vn_lock(vp, LK_SHARED | LK_RETRY);
787 
788 	switch (advice) {
789 	case POSIX_FADV_NORMAL:
790 	case POSIX_FADV_SEQUENTIAL:
791 	case POSIX_FADV_NOREUSE:
792 		ioflag |= sequential_heuristic(uio, fp);
793 		break;
794 	case POSIX_FADV_RANDOM:
795 		/* Disable read-ahead for random I/O. */
796 		break;
797 	}
798 	orig_offset = uio->uio_offset;
799 
800 #ifdef MAC
801 	error = mac_vnode_check_read(active_cred, fp->f_cred, vp);
802 	if (error == 0)
803 #endif
804 		error = VOP_READ(vp, uio, ioflag, fp->f_cred);
805 	fp->f_nextoff = uio->uio_offset;
806 	VOP_UNLOCK(vp, 0);
807 	if (error == 0 && advice == POSIX_FADV_NOREUSE &&
808 	    orig_offset != uio->uio_offset)
809 		/*
810 		 * Use POSIX_FADV_DONTNEED to flush pages and buffers
811 		 * for the backing file after a POSIX_FADV_NOREUSE
812 		 * read(2).
813 		 */
814 		error = VOP_ADVISE(vp, orig_offset, uio->uio_offset - 1,
815 		    POSIX_FADV_DONTNEED);
816 	return (error);
817 }
818 
819 /*
820  * File table vnode write routine.
821  */
822 static int
823 vn_write(struct file *fp, struct uio *uio, struct ucred *active_cred, int flags,
824     struct thread *td)
825 {
826 	struct vnode *vp;
827 	struct mount *mp;
828 	off_t orig_offset;
829 	int error, ioflag, lock_flags;
830 	int advice;
831 
832 	KASSERT(uio->uio_td == td, ("uio_td %p is not td %p",
833 	    uio->uio_td, td));
834 	KASSERT(flags & FOF_OFFSET, ("No FOF_OFFSET"));
835 	vp = fp->f_vnode;
836 	if (vp->v_type == VREG)
837 		bwillwrite();
838 	ioflag = IO_UNIT;
839 	if (vp->v_type == VREG && (fp->f_flag & O_APPEND))
840 		ioflag |= IO_APPEND;
841 	if (fp->f_flag & FNONBLOCK)
842 		ioflag |= IO_NDELAY;
843 	if (fp->f_flag & O_DIRECT)
844 		ioflag |= IO_DIRECT;
845 	if ((fp->f_flag & O_FSYNC) ||
846 	    (vp->v_mount && (vp->v_mount->mnt_flag & MNT_SYNCHRONOUS)))
847 		ioflag |= IO_SYNC;
848 	mp = NULL;
849 	if (vp->v_type != VCHR &&
850 	    (error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
851 		goto unlock;
852 
853 	advice = get_advice(fp, uio);
854 
855 	if (MNT_SHARED_WRITES(mp) ||
856 	    (mp == NULL && MNT_SHARED_WRITES(vp->v_mount))) {
857 		lock_flags = LK_SHARED;
858 	} else {
859 		lock_flags = LK_EXCLUSIVE;
860 	}
861 
862 	vn_lock(vp, lock_flags | LK_RETRY);
863 	switch (advice) {
864 	case POSIX_FADV_NORMAL:
865 	case POSIX_FADV_SEQUENTIAL:
866 	case POSIX_FADV_NOREUSE:
867 		ioflag |= sequential_heuristic(uio, fp);
868 		break;
869 	case POSIX_FADV_RANDOM:
870 		/* XXX: Is this correct? */
871 		break;
872 	}
873 	orig_offset = uio->uio_offset;
874 
875 #ifdef MAC
876 	error = mac_vnode_check_write(active_cred, fp->f_cred, vp);
877 	if (error == 0)
878 #endif
879 		error = VOP_WRITE(vp, uio, ioflag, fp->f_cred);
880 	fp->f_nextoff = uio->uio_offset;
881 	VOP_UNLOCK(vp, 0);
882 	if (vp->v_type != VCHR)
883 		vn_finished_write(mp);
884 	if (error == 0 && advice == POSIX_FADV_NOREUSE &&
885 	    orig_offset != uio->uio_offset)
886 		/*
887 		 * Use POSIX_FADV_DONTNEED to flush pages and buffers
888 		 * for the backing file after a POSIX_FADV_NOREUSE
889 		 * write(2).
890 		 */
891 		error = VOP_ADVISE(vp, orig_offset, uio->uio_offset - 1,
892 		    POSIX_FADV_DONTNEED);
893 unlock:
894 	return (error);
895 }
896 
897 /*
898  * The vn_io_fault() is a wrapper around vn_read() and vn_write() to
899  * prevent the following deadlock:
900  *
901  * Assume that the thread A reads from the vnode vp1 into userspace
902  * buffer buf1 backed by the pages of vnode vp2.  If a page in buf1 is
903  * currently not resident, then system ends up with the call chain
904  *   vn_read() -> VOP_READ(vp1) -> uiomove() -> [Page Fault] ->
905  *     vm_fault(buf1) -> vnode_pager_getpages(vp2) -> VOP_GETPAGES(vp2)
906  * which establishes lock order vp1->vn_lock, then vp2->vn_lock.
907  * If, at the same time, thread B reads from vnode vp2 into buffer buf2
908  * backed by the pages of vnode vp1, and some page in buf2 is not
909  * resident, we get a reversed order vp2->vn_lock, then vp1->vn_lock.
910  *
911  * To prevent the lock order reversal and deadlock, vn_io_fault() does
912  * not allow page faults to happen during VOP_READ() or VOP_WRITE().
913  * Instead, it first tries to do the whole range i/o with pagefaults
914  * disabled. If all pages in the i/o buffer are resident and mapped,
915  * VOP will succeed (ignoring the genuine filesystem errors).
916  * Otherwise, we get back EFAULT, and vn_io_fault() falls back to do
917  * i/o in chunks, with all pages in the chunk prefaulted and held
918  * using vm_fault_quick_hold_pages().
919  *
920  * Filesystems using this deadlock avoidance scheme should use the
921  * array of the held pages from uio, saved in the curthread->td_ma,
922  * instead of doing uiomove().  A helper function
923  * vn_io_fault_uiomove() converts uiomove request into
924  * uiomove_fromphys() over td_ma array.
925  *
926  * Since vnode locks do not cover the whole i/o anymore, rangelocks
927  * make the current i/o request atomic with respect to other i/os and
928  * truncations.
929  */
930 
931 /*
932  * Decode vn_io_fault_args and perform the corresponding i/o.
933  */
934 static int
935 vn_io_fault_doio(struct vn_io_fault_args *args, struct uio *uio,
936     struct thread *td)
937 {
938 	int error, save;
939 
940 	error = 0;
941 	save = vm_fault_disable_pagefaults();
942 	switch (args->kind) {
943 	case VN_IO_FAULT_FOP:
944 		error = (args->args.fop_args.doio)(args->args.fop_args.fp,
945 		    uio, args->cred, args->flags, td);
946 		break;
947 	case VN_IO_FAULT_VOP:
948 		if (uio->uio_rw == UIO_READ) {
949 			error = VOP_READ(args->args.vop_args.vp, uio,
950 			    args->flags, args->cred);
951 		} else if (uio->uio_rw == UIO_WRITE) {
952 			error = VOP_WRITE(args->args.vop_args.vp, uio,
953 			    args->flags, args->cred);
954 		}
955 		break;
956 	default:
957 		panic("vn_io_fault_doio: unknown kind of io %d %d",
958 		    args->kind, uio->uio_rw);
959 	}
960 	vm_fault_enable_pagefaults(save);
961 	return (error);
962 }
963 
964 static int
965 vn_io_fault_touch(char *base, const struct uio *uio)
966 {
967 	int r;
968 
969 	r = fubyte(base);
970 	if (r == -1 || (uio->uio_rw == UIO_READ && subyte(base, r) == -1))
971 		return (EFAULT);
972 	return (0);
973 }
974 
975 static int
976 vn_io_fault_prefault_user(const struct uio *uio)
977 {
978 	char *base;
979 	const struct iovec *iov;
980 	size_t len;
981 	ssize_t resid;
982 	int error, i;
983 
984 	KASSERT(uio->uio_segflg == UIO_USERSPACE,
985 	    ("vn_io_fault_prefault userspace"));
986 
987 	error = i = 0;
988 	iov = uio->uio_iov;
989 	resid = uio->uio_resid;
990 	base = iov->iov_base;
991 	len = iov->iov_len;
992 	while (resid > 0) {
993 		error = vn_io_fault_touch(base, uio);
994 		if (error != 0)
995 			break;
996 		if (len < PAGE_SIZE) {
997 			if (len != 0) {
998 				error = vn_io_fault_touch(base + len - 1, uio);
999 				if (error != 0)
1000 					break;
1001 				resid -= len;
1002 			}
1003 			if (++i >= uio->uio_iovcnt)
1004 				break;
1005 			iov = uio->uio_iov + i;
1006 			base = iov->iov_base;
1007 			len = iov->iov_len;
1008 		} else {
1009 			len -= PAGE_SIZE;
1010 			base += PAGE_SIZE;
1011 			resid -= PAGE_SIZE;
1012 		}
1013 	}
1014 	return (error);
1015 }
1016 
1017 /*
1018  * Common code for vn_io_fault(), agnostic to the kind of i/o request.
1019  * Uses vn_io_fault_doio() to make the call to an actual i/o function.
1020  * Used from vn_rdwr() and vn_io_fault(), which encode the i/o request
1021  * into args and call vn_io_fault1() to handle faults during the user
1022  * mode buffer accesses.
1023  */
1024 static int
1025 vn_io_fault1(struct vnode *vp, struct uio *uio, struct vn_io_fault_args *args,
1026     struct thread *td)
1027 {
1028 	vm_page_t ma[io_hold_cnt + 2];
1029 	struct uio *uio_clone, short_uio;
1030 	struct iovec short_iovec[1];
1031 	vm_page_t *prev_td_ma;
1032 	vm_prot_t prot;
1033 	vm_offset_t addr, end;
1034 	size_t len, resid;
1035 	ssize_t adv;
1036 	int error, cnt, saveheld, prev_td_ma_cnt;
1037 
1038 	if (vn_io_fault_prefault) {
1039 		error = vn_io_fault_prefault_user(uio);
1040 		if (error != 0)
1041 			return (error); /* Or ignore ? */
1042 	}
1043 
1044 	prot = uio->uio_rw == UIO_READ ? VM_PROT_WRITE : VM_PROT_READ;
1045 
1046 	/*
1047 	 * The UFS follows IO_UNIT directive and replays back both
1048 	 * uio_offset and uio_resid if an error is encountered during the
1049 	 * operation.  But, since the iovec may be already advanced,
1050 	 * uio is still in an inconsistent state.
1051 	 *
1052 	 * Cache a copy of the original uio, which is advanced to the redo
1053 	 * point using UIO_NOCOPY below.
1054 	 */
1055 	uio_clone = cloneuio(uio);
1056 	resid = uio->uio_resid;
1057 
1058 	short_uio.uio_segflg = UIO_USERSPACE;
1059 	short_uio.uio_rw = uio->uio_rw;
1060 	short_uio.uio_td = uio->uio_td;
1061 
1062 	error = vn_io_fault_doio(args, uio, td);
1063 	if (error != EFAULT)
1064 		goto out;
1065 
1066 	atomic_add_long(&vn_io_faults_cnt, 1);
1067 	uio_clone->uio_segflg = UIO_NOCOPY;
1068 	uiomove(NULL, resid - uio->uio_resid, uio_clone);
1069 	uio_clone->uio_segflg = uio->uio_segflg;
1070 
1071 	saveheld = curthread_pflags_set(TDP_UIOHELD);
1072 	prev_td_ma = td->td_ma;
1073 	prev_td_ma_cnt = td->td_ma_cnt;
1074 
1075 	while (uio_clone->uio_resid != 0) {
1076 		len = uio_clone->uio_iov->iov_len;
1077 		if (len == 0) {
1078 			KASSERT(uio_clone->uio_iovcnt >= 1,
1079 			    ("iovcnt underflow"));
1080 			uio_clone->uio_iov++;
1081 			uio_clone->uio_iovcnt--;
1082 			continue;
1083 		}
1084 		if (len > io_hold_cnt * PAGE_SIZE)
1085 			len = io_hold_cnt * PAGE_SIZE;
1086 		addr = (uintptr_t)uio_clone->uio_iov->iov_base;
1087 		end = round_page(addr + len);
1088 		if (end < addr) {
1089 			error = EFAULT;
1090 			break;
1091 		}
1092 		cnt = atop(end - trunc_page(addr));
1093 		/*
1094 		 * A perfectly misaligned address and length could cause
1095 		 * both the start and the end of the chunk to use partial
1096 		 * page.  +2 accounts for such a situation.
1097 		 */
1098 		cnt = vm_fault_quick_hold_pages(&td->td_proc->p_vmspace->vm_map,
1099 		    addr, len, prot, ma, io_hold_cnt + 2);
1100 		if (cnt == -1) {
1101 			error = EFAULT;
1102 			break;
1103 		}
1104 		short_uio.uio_iov = &short_iovec[0];
1105 		short_iovec[0].iov_base = (void *)addr;
1106 		short_uio.uio_iovcnt = 1;
1107 		short_uio.uio_resid = short_iovec[0].iov_len = len;
1108 		short_uio.uio_offset = uio_clone->uio_offset;
1109 		td->td_ma = ma;
1110 		td->td_ma_cnt = cnt;
1111 
1112 		error = vn_io_fault_doio(args, &short_uio, td);
1113 		vm_page_unhold_pages(ma, cnt);
1114 		adv = len - short_uio.uio_resid;
1115 
1116 		uio_clone->uio_iov->iov_base =
1117 		    (char *)uio_clone->uio_iov->iov_base + adv;
1118 		uio_clone->uio_iov->iov_len -= adv;
1119 		uio_clone->uio_resid -= adv;
1120 		uio_clone->uio_offset += adv;
1121 
1122 		uio->uio_resid -= adv;
1123 		uio->uio_offset += adv;
1124 
1125 		if (error != 0 || adv == 0)
1126 			break;
1127 	}
1128 	td->td_ma = prev_td_ma;
1129 	td->td_ma_cnt = prev_td_ma_cnt;
1130 	curthread_pflags_restore(saveheld);
1131 out:
1132 	free(uio_clone, M_IOV);
1133 	return (error);
1134 }
1135 
1136 static int
1137 vn_io_fault(struct file *fp, struct uio *uio, struct ucred *active_cred,
1138     int flags, struct thread *td)
1139 {
1140 	fo_rdwr_t *doio;
1141 	struct vnode *vp;
1142 	void *rl_cookie;
1143 	struct vn_io_fault_args args;
1144 	int error;
1145 
1146 	doio = uio->uio_rw == UIO_READ ? vn_read : vn_write;
1147 	vp = fp->f_vnode;
1148 	foffset_lock_uio(fp, uio, flags);
1149 	if (do_vn_io_fault(vp, uio)) {
1150 		args.kind = VN_IO_FAULT_FOP;
1151 		args.args.fop_args.fp = fp;
1152 		args.args.fop_args.doio = doio;
1153 		args.cred = active_cred;
1154 		args.flags = flags | FOF_OFFSET;
1155 		if (uio->uio_rw == UIO_READ) {
1156 			rl_cookie = vn_rangelock_rlock(vp, uio->uio_offset,
1157 			    uio->uio_offset + uio->uio_resid);
1158 		} else if ((fp->f_flag & O_APPEND) != 0 ||
1159 		    (flags & FOF_OFFSET) == 0) {
1160 			/* For appenders, punt and lock the whole range. */
1161 			rl_cookie = vn_rangelock_wlock(vp, 0, OFF_MAX);
1162 		} else {
1163 			rl_cookie = vn_rangelock_wlock(vp, uio->uio_offset,
1164 			    uio->uio_offset + uio->uio_resid);
1165 		}
1166 		error = vn_io_fault1(vp, uio, &args, td);
1167 		vn_rangelock_unlock(vp, rl_cookie);
1168 	} else {
1169 		error = doio(fp, uio, active_cred, flags | FOF_OFFSET, td);
1170 	}
1171 	foffset_unlock_uio(fp, uio, flags);
1172 	return (error);
1173 }
1174 
1175 /*
1176  * Helper function to perform the requested uiomove operation using
1177  * the held pages for io->uio_iov[0].iov_base buffer instead of
1178  * copyin/copyout.  Access to the pages with uiomove_fromphys()
1179  * instead of iov_base prevents page faults that could occur due to
1180  * pmap_collect() invalidating the mapping created by
1181  * vm_fault_quick_hold_pages(), or pageout daemon, page laundry or
1182  * object cleanup revoking the write access from page mappings.
1183  *
1184  * Filesystems specified MNTK_NO_IOPF shall use vn_io_fault_uiomove()
1185  * instead of plain uiomove().
1186  */
1187 int
1188 vn_io_fault_uiomove(char *data, int xfersize, struct uio *uio)
1189 {
1190 	struct uio transp_uio;
1191 	struct iovec transp_iov[1];
1192 	struct thread *td;
1193 	size_t adv;
1194 	int error, pgadv;
1195 
1196 	td = curthread;
1197 	if ((td->td_pflags & TDP_UIOHELD) == 0 ||
1198 	    uio->uio_segflg != UIO_USERSPACE)
1199 		return (uiomove(data, xfersize, uio));
1200 
1201 	KASSERT(uio->uio_iovcnt == 1, ("uio_iovcnt %d", uio->uio_iovcnt));
1202 	transp_iov[0].iov_base = data;
1203 	transp_uio.uio_iov = &transp_iov[0];
1204 	transp_uio.uio_iovcnt = 1;
1205 	if (xfersize > uio->uio_resid)
1206 		xfersize = uio->uio_resid;
1207 	transp_uio.uio_resid = transp_iov[0].iov_len = xfersize;
1208 	transp_uio.uio_offset = 0;
1209 	transp_uio.uio_segflg = UIO_SYSSPACE;
1210 	/*
1211 	 * Since transp_iov points to data, and td_ma page array
1212 	 * corresponds to original uio->uio_iov, we need to invert the
1213 	 * direction of the i/o operation as passed to
1214 	 * uiomove_fromphys().
1215 	 */
1216 	switch (uio->uio_rw) {
1217 	case UIO_WRITE:
1218 		transp_uio.uio_rw = UIO_READ;
1219 		break;
1220 	case UIO_READ:
1221 		transp_uio.uio_rw = UIO_WRITE;
1222 		break;
1223 	}
1224 	transp_uio.uio_td = uio->uio_td;
1225 	error = uiomove_fromphys(td->td_ma,
1226 	    ((vm_offset_t)uio->uio_iov->iov_base) & PAGE_MASK,
1227 	    xfersize, &transp_uio);
1228 	adv = xfersize - transp_uio.uio_resid;
1229 	pgadv =
1230 	    (((vm_offset_t)uio->uio_iov->iov_base + adv) >> PAGE_SHIFT) -
1231 	    (((vm_offset_t)uio->uio_iov->iov_base) >> PAGE_SHIFT);
1232 	td->td_ma += pgadv;
1233 	KASSERT(td->td_ma_cnt >= pgadv, ("consumed pages %d %d", td->td_ma_cnt,
1234 	    pgadv));
1235 	td->td_ma_cnt -= pgadv;
1236 	uio->uio_iov->iov_base = (char *)uio->uio_iov->iov_base + adv;
1237 	uio->uio_iov->iov_len -= adv;
1238 	uio->uio_resid -= adv;
1239 	uio->uio_offset += adv;
1240 	return (error);
1241 }
1242 
1243 int
1244 vn_io_fault_pgmove(vm_page_t ma[], vm_offset_t offset, int xfersize,
1245     struct uio *uio)
1246 {
1247 	struct thread *td;
1248 	vm_offset_t iov_base;
1249 	int cnt, pgadv;
1250 
1251 	td = curthread;
1252 	if ((td->td_pflags & TDP_UIOHELD) == 0 ||
1253 	    uio->uio_segflg != UIO_USERSPACE)
1254 		return (uiomove_fromphys(ma, offset, xfersize, uio));
1255 
1256 	KASSERT(uio->uio_iovcnt == 1, ("uio_iovcnt %d", uio->uio_iovcnt));
1257 	cnt = xfersize > uio->uio_resid ? uio->uio_resid : xfersize;
1258 	iov_base = (vm_offset_t)uio->uio_iov->iov_base;
1259 	switch (uio->uio_rw) {
1260 	case UIO_WRITE:
1261 		pmap_copy_pages(td->td_ma, iov_base & PAGE_MASK, ma,
1262 		    offset, cnt);
1263 		break;
1264 	case UIO_READ:
1265 		pmap_copy_pages(ma, offset, td->td_ma, iov_base & PAGE_MASK,
1266 		    cnt);
1267 		break;
1268 	}
1269 	pgadv = ((iov_base + cnt) >> PAGE_SHIFT) - (iov_base >> PAGE_SHIFT);
1270 	td->td_ma += pgadv;
1271 	KASSERT(td->td_ma_cnt >= pgadv, ("consumed pages %d %d", td->td_ma_cnt,
1272 	    pgadv));
1273 	td->td_ma_cnt -= pgadv;
1274 	uio->uio_iov->iov_base = (char *)(iov_base + cnt);
1275 	uio->uio_iov->iov_len -= cnt;
1276 	uio->uio_resid -= cnt;
1277 	uio->uio_offset += cnt;
1278 	return (0);
1279 }
1280 
1281 
1282 /*
1283  * File table truncate routine.
1284  */
1285 static int
1286 vn_truncate(struct file *fp, off_t length, struct ucred *active_cred,
1287     struct thread *td)
1288 {
1289 	struct vattr vattr;
1290 	struct mount *mp;
1291 	struct vnode *vp;
1292 	void *rl_cookie;
1293 	int error;
1294 
1295 	vp = fp->f_vnode;
1296 
1297 	/*
1298 	 * Lock the whole range for truncation.  Otherwise split i/o
1299 	 * might happen partly before and partly after the truncation.
1300 	 */
1301 	rl_cookie = vn_rangelock_wlock(vp, 0, OFF_MAX);
1302 	error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
1303 	if (error)
1304 		goto out1;
1305 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1306 	AUDIT_ARG_VNODE1(vp);
1307 	if (vp->v_type == VDIR) {
1308 		error = EISDIR;
1309 		goto out;
1310 	}
1311 #ifdef MAC
1312 	error = mac_vnode_check_write(active_cred, fp->f_cred, vp);
1313 	if (error)
1314 		goto out;
1315 #endif
1316 	error = vn_writechk(vp);
1317 	if (error == 0) {
1318 		VATTR_NULL(&vattr);
1319 		vattr.va_size = length;
1320 		if ((fp->f_flag & O_FSYNC) != 0)
1321 			vattr.va_vaflags |= VA_SYNC;
1322 		error = VOP_SETATTR(vp, &vattr, fp->f_cred);
1323 	}
1324 out:
1325 	VOP_UNLOCK(vp, 0);
1326 	vn_finished_write(mp);
1327 out1:
1328 	vn_rangelock_unlock(vp, rl_cookie);
1329 	return (error);
1330 }
1331 
1332 /*
1333  * File table vnode stat routine.
1334  */
1335 static int
1336 vn_statfile(struct file *fp, struct stat *sb, struct ucred *active_cred,
1337     struct thread *td)
1338 {
1339 	struct vnode *vp = fp->f_vnode;
1340 	int error;
1341 
1342 	vn_lock(vp, LK_SHARED | LK_RETRY);
1343 	error = vn_stat(vp, sb, active_cred, fp->f_cred, td);
1344 	VOP_UNLOCK(vp, 0);
1345 
1346 	return (error);
1347 }
1348 
1349 /*
1350  * Stat a vnode; implementation for the stat syscall
1351  */
1352 int
1353 vn_stat(struct vnode *vp, struct stat *sb, struct ucred *active_cred,
1354     struct ucred *file_cred, struct thread *td)
1355 {
1356 	struct vattr vattr;
1357 	struct vattr *vap;
1358 	int error;
1359 	u_short mode;
1360 
1361 	AUDIT_ARG_VNODE1(vp);
1362 #ifdef MAC
1363 	error = mac_vnode_check_stat(active_cred, file_cred, vp);
1364 	if (error)
1365 		return (error);
1366 #endif
1367 
1368 	vap = &vattr;
1369 
1370 	/*
1371 	 * Initialize defaults for new and unusual fields, so that file
1372 	 * systems which don't support these fields don't need to know
1373 	 * about them.
1374 	 */
1375 	vap->va_birthtime.tv_sec = -1;
1376 	vap->va_birthtime.tv_nsec = 0;
1377 	vap->va_fsid = VNOVAL;
1378 	vap->va_rdev = NODEV;
1379 
1380 	error = VOP_GETATTR(vp, vap, active_cred);
1381 	if (error)
1382 		return (error);
1383 
1384 	/*
1385 	 * Zero the spare stat fields
1386 	 */
1387 	bzero(sb, sizeof *sb);
1388 
1389 	/*
1390 	 * Copy from vattr table
1391 	 */
1392 	if (vap->va_fsid != VNOVAL)
1393 		sb->st_dev = vap->va_fsid;
1394 	else
1395 		sb->st_dev = vp->v_mount->mnt_stat.f_fsid.val[0];
1396 	sb->st_ino = vap->va_fileid;
1397 	mode = vap->va_mode;
1398 	switch (vap->va_type) {
1399 	case VREG:
1400 		mode |= S_IFREG;
1401 		break;
1402 	case VDIR:
1403 		mode |= S_IFDIR;
1404 		break;
1405 	case VBLK:
1406 		mode |= S_IFBLK;
1407 		break;
1408 	case VCHR:
1409 		mode |= S_IFCHR;
1410 		break;
1411 	case VLNK:
1412 		mode |= S_IFLNK;
1413 		break;
1414 	case VSOCK:
1415 		mode |= S_IFSOCK;
1416 		break;
1417 	case VFIFO:
1418 		mode |= S_IFIFO;
1419 		break;
1420 	default:
1421 		return (EBADF);
1422 	}
1423 	sb->st_mode = mode;
1424 	sb->st_nlink = vap->va_nlink;
1425 	sb->st_uid = vap->va_uid;
1426 	sb->st_gid = vap->va_gid;
1427 	sb->st_rdev = vap->va_rdev;
1428 	if (vap->va_size > OFF_MAX)
1429 		return (EOVERFLOW);
1430 	sb->st_size = vap->va_size;
1431 	sb->st_atim = vap->va_atime;
1432 	sb->st_mtim = vap->va_mtime;
1433 	sb->st_ctim = vap->va_ctime;
1434 	sb->st_birthtim = vap->va_birthtime;
1435 
1436         /*
1437 	 * According to www.opengroup.org, the meaning of st_blksize is
1438 	 *   "a filesystem-specific preferred I/O block size for this
1439 	 *    object.  In some filesystem types, this may vary from file
1440 	 *    to file"
1441 	 * Use miminum/default of PAGE_SIZE (e.g. for VCHR).
1442 	 */
1443 
1444 	sb->st_blksize = max(PAGE_SIZE, vap->va_blocksize);
1445 
1446 	sb->st_flags = vap->va_flags;
1447 	if (priv_check(td, PRIV_VFS_GENERATION))
1448 		sb->st_gen = 0;
1449 	else
1450 		sb->st_gen = vap->va_gen;
1451 
1452 	sb->st_blocks = vap->va_bytes / S_BLKSIZE;
1453 	return (0);
1454 }
1455 
1456 /*
1457  * File table vnode ioctl routine.
1458  */
1459 static int
1460 vn_ioctl(struct file *fp, u_long com, void *data, struct ucred *active_cred,
1461     struct thread *td)
1462 {
1463 	struct vattr vattr;
1464 	struct vnode *vp;
1465 	int error;
1466 
1467 	vp = fp->f_vnode;
1468 	switch (vp->v_type) {
1469 	case VDIR:
1470 	case VREG:
1471 		switch (com) {
1472 		case FIONREAD:
1473 			vn_lock(vp, LK_SHARED | LK_RETRY);
1474 			error = VOP_GETATTR(vp, &vattr, active_cred);
1475 			VOP_UNLOCK(vp, 0);
1476 			if (error == 0)
1477 				*(int *)data = vattr.va_size - fp->f_offset;
1478 			return (error);
1479 		case FIONBIO:
1480 		case FIOASYNC:
1481 			return (0);
1482 		default:
1483 			return (VOP_IOCTL(vp, com, data, fp->f_flag,
1484 			    active_cred, td));
1485 		}
1486 		break;
1487 	case VCHR:
1488 		return (VOP_IOCTL(vp, com, data, fp->f_flag,
1489 		    active_cred, td));
1490 	default:
1491 		return (ENOTTY);
1492 	}
1493 }
1494 
1495 /*
1496  * File table vnode poll routine.
1497  */
1498 static int
1499 vn_poll(struct file *fp, int events, struct ucred *active_cred,
1500     struct thread *td)
1501 {
1502 	struct vnode *vp;
1503 	int error;
1504 
1505 	vp = fp->f_vnode;
1506 #ifdef MAC
1507 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1508 	AUDIT_ARG_VNODE1(vp);
1509 	error = mac_vnode_check_poll(active_cred, fp->f_cred, vp);
1510 	VOP_UNLOCK(vp, 0);
1511 	if (!error)
1512 #endif
1513 
1514 	error = VOP_POLL(vp, events, fp->f_cred, td);
1515 	return (error);
1516 }
1517 
1518 /*
1519  * Acquire the requested lock and then check for validity.  LK_RETRY
1520  * permits vn_lock to return doomed vnodes.
1521  */
1522 int
1523 _vn_lock(struct vnode *vp, int flags, char *file, int line)
1524 {
1525 	int error;
1526 
1527 	VNASSERT((flags & LK_TYPE_MASK) != 0, vp,
1528 	    ("vn_lock: no locktype"));
1529 	VNASSERT(vp->v_holdcnt != 0, vp, ("vn_lock: zero hold count"));
1530 retry:
1531 	error = VOP_LOCK1(vp, flags, file, line);
1532 	flags &= ~LK_INTERLOCK;	/* Interlock is always dropped. */
1533 	KASSERT((flags & LK_RETRY) == 0 || error == 0,
1534 	    ("vn_lock: error %d incompatible with flags %#x", error, flags));
1535 
1536 	if ((flags & LK_RETRY) == 0) {
1537 		if (error == 0 && (vp->v_iflag & VI_DOOMED) != 0) {
1538 			VOP_UNLOCK(vp, 0);
1539 			error = ENOENT;
1540 		}
1541 	} else if (error != 0)
1542 		goto retry;
1543 	return (error);
1544 }
1545 
1546 /*
1547  * File table vnode close routine.
1548  */
1549 static int
1550 vn_closefile(struct file *fp, struct thread *td)
1551 {
1552 	struct vnode *vp;
1553 	struct flock lf;
1554 	int error;
1555 	bool ref;
1556 
1557 	vp = fp->f_vnode;
1558 	fp->f_ops = &badfileops;
1559 	ref= (fp->f_flag & FHASLOCK) != 0 && fp->f_type == DTYPE_VNODE;
1560 
1561 	error = vn_close1(vp, fp->f_flag, fp->f_cred, td, ref);
1562 
1563 	if (__predict_false(ref)) {
1564 		lf.l_whence = SEEK_SET;
1565 		lf.l_start = 0;
1566 		lf.l_len = 0;
1567 		lf.l_type = F_UNLCK;
1568 		(void) VOP_ADVLOCK(vp, fp, F_UNLCK, &lf, F_FLOCK);
1569 		vrele(vp);
1570 	}
1571 	return (error);
1572 }
1573 
1574 static bool
1575 vn_suspendable(struct mount *mp)
1576 {
1577 
1578 	return (mp->mnt_op->vfs_susp_clean != NULL);
1579 }
1580 
1581 /*
1582  * Preparing to start a filesystem write operation. If the operation is
1583  * permitted, then we bump the count of operations in progress and
1584  * proceed. If a suspend request is in progress, we wait until the
1585  * suspension is over, and then proceed.
1586  */
1587 static int
1588 vn_start_write_locked(struct mount *mp, int flags)
1589 {
1590 	int error, mflags;
1591 
1592 	mtx_assert(MNT_MTX(mp), MA_OWNED);
1593 	error = 0;
1594 
1595 	/*
1596 	 * Check on status of suspension.
1597 	 */
1598 	if ((curthread->td_pflags & TDP_IGNSUSP) == 0 ||
1599 	    mp->mnt_susp_owner != curthread) {
1600 		mflags = ((mp->mnt_vfc->vfc_flags & VFCF_SBDRY) != 0 ?
1601 		    (flags & PCATCH) : 0) | (PUSER - 1);
1602 		while ((mp->mnt_kern_flag & MNTK_SUSPEND) != 0) {
1603 			if (flags & V_NOWAIT) {
1604 				error = EWOULDBLOCK;
1605 				goto unlock;
1606 			}
1607 			error = msleep(&mp->mnt_flag, MNT_MTX(mp), mflags,
1608 			    "suspfs", 0);
1609 			if (error)
1610 				goto unlock;
1611 		}
1612 	}
1613 	if (flags & V_XSLEEP)
1614 		goto unlock;
1615 	mp->mnt_writeopcount++;
1616 unlock:
1617 	if (error != 0 || (flags & V_XSLEEP) != 0)
1618 		MNT_REL(mp);
1619 	MNT_IUNLOCK(mp);
1620 	return (error);
1621 }
1622 
1623 int
1624 vn_start_write(struct vnode *vp, struct mount **mpp, int flags)
1625 {
1626 	struct mount *mp;
1627 	int error;
1628 
1629 	KASSERT((flags & V_MNTREF) == 0 || (*mpp != NULL && vp == NULL),
1630 	    ("V_MNTREF requires mp"));
1631 
1632 	error = 0;
1633 	/*
1634 	 * If a vnode is provided, get and return the mount point that
1635 	 * to which it will write.
1636 	 */
1637 	if (vp != NULL) {
1638 		if ((error = VOP_GETWRITEMOUNT(vp, mpp)) != 0) {
1639 			*mpp = NULL;
1640 			if (error != EOPNOTSUPP)
1641 				return (error);
1642 			return (0);
1643 		}
1644 	}
1645 	if ((mp = *mpp) == NULL)
1646 		return (0);
1647 
1648 	if (!vn_suspendable(mp)) {
1649 		if (vp != NULL || (flags & V_MNTREF) != 0)
1650 			vfs_rel(mp);
1651 		return (0);
1652 	}
1653 
1654 	/*
1655 	 * VOP_GETWRITEMOUNT() returns with the mp refcount held through
1656 	 * a vfs_ref().
1657 	 * As long as a vnode is not provided we need to acquire a
1658 	 * refcount for the provided mountpoint too, in order to
1659 	 * emulate a vfs_ref().
1660 	 */
1661 	MNT_ILOCK(mp);
1662 	if (vp == NULL && (flags & V_MNTREF) == 0)
1663 		MNT_REF(mp);
1664 
1665 	return (vn_start_write_locked(mp, flags));
1666 }
1667 
1668 /*
1669  * Secondary suspension. Used by operations such as vop_inactive
1670  * routines that are needed by the higher level functions. These
1671  * are allowed to proceed until all the higher level functions have
1672  * completed (indicated by mnt_writeopcount dropping to zero). At that
1673  * time, these operations are halted until the suspension is over.
1674  */
1675 int
1676 vn_start_secondary_write(struct vnode *vp, struct mount **mpp, int flags)
1677 {
1678 	struct mount *mp;
1679 	int error;
1680 
1681 	KASSERT((flags & V_MNTREF) == 0 || (*mpp != NULL && vp == NULL),
1682 	    ("V_MNTREF requires mp"));
1683 
1684  retry:
1685 	if (vp != NULL) {
1686 		if ((error = VOP_GETWRITEMOUNT(vp, mpp)) != 0) {
1687 			*mpp = NULL;
1688 			if (error != EOPNOTSUPP)
1689 				return (error);
1690 			return (0);
1691 		}
1692 	}
1693 	/*
1694 	 * If we are not suspended or have not yet reached suspended
1695 	 * mode, then let the operation proceed.
1696 	 */
1697 	if ((mp = *mpp) == NULL)
1698 		return (0);
1699 
1700 	if (!vn_suspendable(mp)) {
1701 		if (vp != NULL || (flags & V_MNTREF) != 0)
1702 			vfs_rel(mp);
1703 		return (0);
1704 	}
1705 
1706 	/*
1707 	 * VOP_GETWRITEMOUNT() returns with the mp refcount held through
1708 	 * a vfs_ref().
1709 	 * As long as a vnode is not provided we need to acquire a
1710 	 * refcount for the provided mountpoint too, in order to
1711 	 * emulate a vfs_ref().
1712 	 */
1713 	MNT_ILOCK(mp);
1714 	if (vp == NULL && (flags & V_MNTREF) == 0)
1715 		MNT_REF(mp);
1716 	if ((mp->mnt_kern_flag & (MNTK_SUSPENDED | MNTK_SUSPEND2)) == 0) {
1717 		mp->mnt_secondary_writes++;
1718 		mp->mnt_secondary_accwrites++;
1719 		MNT_IUNLOCK(mp);
1720 		return (0);
1721 	}
1722 	if (flags & V_NOWAIT) {
1723 		MNT_REL(mp);
1724 		MNT_IUNLOCK(mp);
1725 		return (EWOULDBLOCK);
1726 	}
1727 	/*
1728 	 * Wait for the suspension to finish.
1729 	 */
1730 	error = msleep(&mp->mnt_flag, MNT_MTX(mp), (PUSER - 1) | PDROP |
1731 	    ((mp->mnt_vfc->vfc_flags & VFCF_SBDRY) != 0 ? (flags & PCATCH) : 0),
1732 	    "suspfs", 0);
1733 	vfs_rel(mp);
1734 	if (error == 0)
1735 		goto retry;
1736 	return (error);
1737 }
1738 
1739 /*
1740  * Filesystem write operation has completed. If we are suspending and this
1741  * operation is the last one, notify the suspender that the suspension is
1742  * now in effect.
1743  */
1744 void
1745 vn_finished_write(struct mount *mp)
1746 {
1747 	if (mp == NULL || !vn_suspendable(mp))
1748 		return;
1749 	MNT_ILOCK(mp);
1750 	MNT_REL(mp);
1751 	mp->mnt_writeopcount--;
1752 	if (mp->mnt_writeopcount < 0)
1753 		panic("vn_finished_write: neg cnt");
1754 	if ((mp->mnt_kern_flag & MNTK_SUSPEND) != 0 &&
1755 	    mp->mnt_writeopcount <= 0)
1756 		wakeup(&mp->mnt_writeopcount);
1757 	MNT_IUNLOCK(mp);
1758 }
1759 
1760 
1761 /*
1762  * Filesystem secondary write operation has completed. If we are
1763  * suspending and this operation is the last one, notify the suspender
1764  * that the suspension is now in effect.
1765  */
1766 void
1767 vn_finished_secondary_write(struct mount *mp)
1768 {
1769 	if (mp == NULL || !vn_suspendable(mp))
1770 		return;
1771 	MNT_ILOCK(mp);
1772 	MNT_REL(mp);
1773 	mp->mnt_secondary_writes--;
1774 	if (mp->mnt_secondary_writes < 0)
1775 		panic("vn_finished_secondary_write: neg cnt");
1776 	if ((mp->mnt_kern_flag & MNTK_SUSPEND) != 0 &&
1777 	    mp->mnt_secondary_writes <= 0)
1778 		wakeup(&mp->mnt_secondary_writes);
1779 	MNT_IUNLOCK(mp);
1780 }
1781 
1782 
1783 
1784 /*
1785  * Request a filesystem to suspend write operations.
1786  */
1787 int
1788 vfs_write_suspend(struct mount *mp, int flags)
1789 {
1790 	int error;
1791 
1792 	MPASS(vn_suspendable(mp));
1793 
1794 	MNT_ILOCK(mp);
1795 	if (mp->mnt_susp_owner == curthread) {
1796 		MNT_IUNLOCK(mp);
1797 		return (EALREADY);
1798 	}
1799 	while (mp->mnt_kern_flag & MNTK_SUSPEND)
1800 		msleep(&mp->mnt_flag, MNT_MTX(mp), PUSER - 1, "wsuspfs", 0);
1801 
1802 	/*
1803 	 * Unmount holds a write reference on the mount point.  If we
1804 	 * own busy reference and drain for writers, we deadlock with
1805 	 * the reference draining in the unmount path.  Callers of
1806 	 * vfs_write_suspend() must specify VS_SKIP_UNMOUNT if
1807 	 * vfs_busy() reference is owned and caller is not in the
1808 	 * unmount context.
1809 	 */
1810 	if ((flags & VS_SKIP_UNMOUNT) != 0 &&
1811 	    (mp->mnt_kern_flag & MNTK_UNMOUNT) != 0) {
1812 		MNT_IUNLOCK(mp);
1813 		return (EBUSY);
1814 	}
1815 
1816 	mp->mnt_kern_flag |= MNTK_SUSPEND;
1817 	mp->mnt_susp_owner = curthread;
1818 	if (mp->mnt_writeopcount > 0)
1819 		(void) msleep(&mp->mnt_writeopcount,
1820 		    MNT_MTX(mp), (PUSER - 1)|PDROP, "suspwt", 0);
1821 	else
1822 		MNT_IUNLOCK(mp);
1823 	if ((error = VFS_SYNC(mp, MNT_SUSPEND)) != 0)
1824 		vfs_write_resume(mp, 0);
1825 	return (error);
1826 }
1827 
1828 /*
1829  * Request a filesystem to resume write operations.
1830  */
1831 void
1832 vfs_write_resume(struct mount *mp, int flags)
1833 {
1834 
1835 	MPASS(vn_suspendable(mp));
1836 
1837 	MNT_ILOCK(mp);
1838 	if ((mp->mnt_kern_flag & MNTK_SUSPEND) != 0) {
1839 		KASSERT(mp->mnt_susp_owner == curthread, ("mnt_susp_owner"));
1840 		mp->mnt_kern_flag &= ~(MNTK_SUSPEND | MNTK_SUSPEND2 |
1841 				       MNTK_SUSPENDED);
1842 		mp->mnt_susp_owner = NULL;
1843 		wakeup(&mp->mnt_writeopcount);
1844 		wakeup(&mp->mnt_flag);
1845 		curthread->td_pflags &= ~TDP_IGNSUSP;
1846 		if ((flags & VR_START_WRITE) != 0) {
1847 			MNT_REF(mp);
1848 			mp->mnt_writeopcount++;
1849 		}
1850 		MNT_IUNLOCK(mp);
1851 		if ((flags & VR_NO_SUSPCLR) == 0)
1852 			VFS_SUSP_CLEAN(mp);
1853 	} else if ((flags & VR_START_WRITE) != 0) {
1854 		MNT_REF(mp);
1855 		vn_start_write_locked(mp, 0);
1856 	} else {
1857 		MNT_IUNLOCK(mp);
1858 	}
1859 }
1860 
1861 /*
1862  * Helper loop around vfs_write_suspend() for filesystem unmount VFS
1863  * methods.
1864  */
1865 int
1866 vfs_write_suspend_umnt(struct mount *mp)
1867 {
1868 	int error;
1869 
1870 	MPASS(vn_suspendable(mp));
1871 	KASSERT((curthread->td_pflags & TDP_IGNSUSP) == 0,
1872 	    ("vfs_write_suspend_umnt: recursed"));
1873 
1874 	/* dounmount() already called vn_start_write(). */
1875 	for (;;) {
1876 		vn_finished_write(mp);
1877 		error = vfs_write_suspend(mp, 0);
1878 		if (error != 0) {
1879 			vn_start_write(NULL, &mp, V_WAIT);
1880 			return (error);
1881 		}
1882 		MNT_ILOCK(mp);
1883 		if ((mp->mnt_kern_flag & MNTK_SUSPENDED) != 0)
1884 			break;
1885 		MNT_IUNLOCK(mp);
1886 		vn_start_write(NULL, &mp, V_WAIT);
1887 	}
1888 	mp->mnt_kern_flag &= ~(MNTK_SUSPENDED | MNTK_SUSPEND2);
1889 	wakeup(&mp->mnt_flag);
1890 	MNT_IUNLOCK(mp);
1891 	curthread->td_pflags |= TDP_IGNSUSP;
1892 	return (0);
1893 }
1894 
1895 /*
1896  * Implement kqueues for files by translating it to vnode operation.
1897  */
1898 static int
1899 vn_kqfilter(struct file *fp, struct knote *kn)
1900 {
1901 
1902 	return (VOP_KQFILTER(fp->f_vnode, kn));
1903 }
1904 
1905 /*
1906  * Simplified in-kernel wrapper calls for extended attribute access.
1907  * Both calls pass in a NULL credential, authorizing as "kernel" access.
1908  * Set IO_NODELOCKED in ioflg if the vnode is already locked.
1909  */
1910 int
1911 vn_extattr_get(struct vnode *vp, int ioflg, int attrnamespace,
1912     const char *attrname, int *buflen, char *buf, struct thread *td)
1913 {
1914 	struct uio	auio;
1915 	struct iovec	iov;
1916 	int	error;
1917 
1918 	iov.iov_len = *buflen;
1919 	iov.iov_base = buf;
1920 
1921 	auio.uio_iov = &iov;
1922 	auio.uio_iovcnt = 1;
1923 	auio.uio_rw = UIO_READ;
1924 	auio.uio_segflg = UIO_SYSSPACE;
1925 	auio.uio_td = td;
1926 	auio.uio_offset = 0;
1927 	auio.uio_resid = *buflen;
1928 
1929 	if ((ioflg & IO_NODELOCKED) == 0)
1930 		vn_lock(vp, LK_SHARED | LK_RETRY);
1931 
1932 	ASSERT_VOP_LOCKED(vp, "IO_NODELOCKED with no vp lock held");
1933 
1934 	/* authorize attribute retrieval as kernel */
1935 	error = VOP_GETEXTATTR(vp, attrnamespace, attrname, &auio, NULL, NULL,
1936 	    td);
1937 
1938 	if ((ioflg & IO_NODELOCKED) == 0)
1939 		VOP_UNLOCK(vp, 0);
1940 
1941 	if (error == 0) {
1942 		*buflen = *buflen - auio.uio_resid;
1943 	}
1944 
1945 	return (error);
1946 }
1947 
1948 /*
1949  * XXX failure mode if partially written?
1950  */
1951 int
1952 vn_extattr_set(struct vnode *vp, int ioflg, int attrnamespace,
1953     const char *attrname, int buflen, char *buf, struct thread *td)
1954 {
1955 	struct uio	auio;
1956 	struct iovec	iov;
1957 	struct mount	*mp;
1958 	int	error;
1959 
1960 	iov.iov_len = buflen;
1961 	iov.iov_base = buf;
1962 
1963 	auio.uio_iov = &iov;
1964 	auio.uio_iovcnt = 1;
1965 	auio.uio_rw = UIO_WRITE;
1966 	auio.uio_segflg = UIO_SYSSPACE;
1967 	auio.uio_td = td;
1968 	auio.uio_offset = 0;
1969 	auio.uio_resid = buflen;
1970 
1971 	if ((ioflg & IO_NODELOCKED) == 0) {
1972 		if ((error = vn_start_write(vp, &mp, V_WAIT)) != 0)
1973 			return (error);
1974 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1975 	}
1976 
1977 	ASSERT_VOP_LOCKED(vp, "IO_NODELOCKED with no vp lock held");
1978 
1979 	/* authorize attribute setting as kernel */
1980 	error = VOP_SETEXTATTR(vp, attrnamespace, attrname, &auio, NULL, td);
1981 
1982 	if ((ioflg & IO_NODELOCKED) == 0) {
1983 		vn_finished_write(mp);
1984 		VOP_UNLOCK(vp, 0);
1985 	}
1986 
1987 	return (error);
1988 }
1989 
1990 int
1991 vn_extattr_rm(struct vnode *vp, int ioflg, int attrnamespace,
1992     const char *attrname, struct thread *td)
1993 {
1994 	struct mount	*mp;
1995 	int	error;
1996 
1997 	if ((ioflg & IO_NODELOCKED) == 0) {
1998 		if ((error = vn_start_write(vp, &mp, V_WAIT)) != 0)
1999 			return (error);
2000 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2001 	}
2002 
2003 	ASSERT_VOP_LOCKED(vp, "IO_NODELOCKED with no vp lock held");
2004 
2005 	/* authorize attribute removal as kernel */
2006 	error = VOP_DELETEEXTATTR(vp, attrnamespace, attrname, NULL, td);
2007 	if (error == EOPNOTSUPP)
2008 		error = VOP_SETEXTATTR(vp, attrnamespace, attrname, NULL,
2009 		    NULL, td);
2010 
2011 	if ((ioflg & IO_NODELOCKED) == 0) {
2012 		vn_finished_write(mp);
2013 		VOP_UNLOCK(vp, 0);
2014 	}
2015 
2016 	return (error);
2017 }
2018 
2019 static int
2020 vn_get_ino_alloc_vget(struct mount *mp, void *arg, int lkflags,
2021     struct vnode **rvp)
2022 {
2023 
2024 	return (VFS_VGET(mp, *(ino_t *)arg, lkflags, rvp));
2025 }
2026 
2027 int
2028 vn_vget_ino(struct vnode *vp, ino_t ino, int lkflags, struct vnode **rvp)
2029 {
2030 
2031 	return (vn_vget_ino_gen(vp, vn_get_ino_alloc_vget, &ino,
2032 	    lkflags, rvp));
2033 }
2034 
2035 int
2036 vn_vget_ino_gen(struct vnode *vp, vn_get_ino_t alloc, void *alloc_arg,
2037     int lkflags, struct vnode **rvp)
2038 {
2039 	struct mount *mp;
2040 	int ltype, error;
2041 
2042 	ASSERT_VOP_LOCKED(vp, "vn_vget_ino_get");
2043 	mp = vp->v_mount;
2044 	ltype = VOP_ISLOCKED(vp);
2045 	KASSERT(ltype == LK_EXCLUSIVE || ltype == LK_SHARED,
2046 	    ("vn_vget_ino: vp not locked"));
2047 	error = vfs_busy(mp, MBF_NOWAIT);
2048 	if (error != 0) {
2049 		vfs_ref(mp);
2050 		VOP_UNLOCK(vp, 0);
2051 		error = vfs_busy(mp, 0);
2052 		vn_lock(vp, ltype | LK_RETRY);
2053 		vfs_rel(mp);
2054 		if (error != 0)
2055 			return (ENOENT);
2056 		if (vp->v_iflag & VI_DOOMED) {
2057 			vfs_unbusy(mp);
2058 			return (ENOENT);
2059 		}
2060 	}
2061 	VOP_UNLOCK(vp, 0);
2062 	error = alloc(mp, alloc_arg, lkflags, rvp);
2063 	vfs_unbusy(mp);
2064 	if (*rvp != vp)
2065 		vn_lock(vp, ltype | LK_RETRY);
2066 	if (vp->v_iflag & VI_DOOMED) {
2067 		if (error == 0) {
2068 			if (*rvp == vp)
2069 				vunref(vp);
2070 			else
2071 				vput(*rvp);
2072 		}
2073 		error = ENOENT;
2074 	}
2075 	return (error);
2076 }
2077 
2078 int
2079 vn_rlimit_fsize(const struct vnode *vp, const struct uio *uio,
2080     struct thread *td)
2081 {
2082 
2083 	if (vp->v_type != VREG || td == NULL)
2084 		return (0);
2085 	if ((uoff_t)uio->uio_offset + uio->uio_resid >
2086 	    lim_cur(td, RLIMIT_FSIZE)) {
2087 		PROC_LOCK(td->td_proc);
2088 		kern_psignal(td->td_proc, SIGXFSZ);
2089 		PROC_UNLOCK(td->td_proc);
2090 		return (EFBIG);
2091 	}
2092 	return (0);
2093 }
2094 
2095 int
2096 vn_chmod(struct file *fp, mode_t mode, struct ucred *active_cred,
2097     struct thread *td)
2098 {
2099 	struct vnode *vp;
2100 
2101 	vp = fp->f_vnode;
2102 #ifdef AUDIT
2103 	vn_lock(vp, LK_SHARED | LK_RETRY);
2104 	AUDIT_ARG_VNODE1(vp);
2105 	VOP_UNLOCK(vp, 0);
2106 #endif
2107 	return (setfmode(td, active_cred, vp, mode));
2108 }
2109 
2110 int
2111 vn_chown(struct file *fp, uid_t uid, gid_t gid, struct ucred *active_cred,
2112     struct thread *td)
2113 {
2114 	struct vnode *vp;
2115 
2116 	vp = fp->f_vnode;
2117 #ifdef AUDIT
2118 	vn_lock(vp, LK_SHARED | LK_RETRY);
2119 	AUDIT_ARG_VNODE1(vp);
2120 	VOP_UNLOCK(vp, 0);
2121 #endif
2122 	return (setfown(td, active_cred, vp, uid, gid));
2123 }
2124 
2125 void
2126 vn_pages_remove(struct vnode *vp, vm_pindex_t start, vm_pindex_t end)
2127 {
2128 	vm_object_t object;
2129 
2130 	if ((object = vp->v_object) == NULL)
2131 		return;
2132 	VM_OBJECT_WLOCK(object);
2133 	vm_object_page_remove(object, start, end, 0);
2134 	VM_OBJECT_WUNLOCK(object);
2135 }
2136 
2137 int
2138 vn_bmap_seekhole(struct vnode *vp, u_long cmd, off_t *off, struct ucred *cred)
2139 {
2140 	struct vattr va;
2141 	daddr_t bn, bnp;
2142 	uint64_t bsize;
2143 	off_t noff;
2144 	int error;
2145 
2146 	KASSERT(cmd == FIOSEEKHOLE || cmd == FIOSEEKDATA,
2147 	    ("Wrong command %lu", cmd));
2148 
2149 	if (vn_lock(vp, LK_SHARED) != 0)
2150 		return (EBADF);
2151 	if (vp->v_type != VREG) {
2152 		error = ENOTTY;
2153 		goto unlock;
2154 	}
2155 	error = VOP_GETATTR(vp, &va, cred);
2156 	if (error != 0)
2157 		goto unlock;
2158 	noff = *off;
2159 	if (noff >= va.va_size) {
2160 		error = ENXIO;
2161 		goto unlock;
2162 	}
2163 	bsize = vp->v_mount->mnt_stat.f_iosize;
2164 	for (bn = noff / bsize; noff < va.va_size; bn++, noff += bsize) {
2165 		error = VOP_BMAP(vp, bn, NULL, &bnp, NULL, NULL);
2166 		if (error == EOPNOTSUPP) {
2167 			error = ENOTTY;
2168 			goto unlock;
2169 		}
2170 		if ((bnp == -1 && cmd == FIOSEEKHOLE) ||
2171 		    (bnp != -1 && cmd == FIOSEEKDATA)) {
2172 			noff = bn * bsize;
2173 			if (noff < *off)
2174 				noff = *off;
2175 			goto unlock;
2176 		}
2177 	}
2178 	if (noff > va.va_size)
2179 		noff = va.va_size;
2180 	/* noff == va.va_size. There is an implicit hole at the end of file. */
2181 	if (cmd == FIOSEEKDATA)
2182 		error = ENXIO;
2183 unlock:
2184 	VOP_UNLOCK(vp, 0);
2185 	if (error == 0)
2186 		*off = noff;
2187 	return (error);
2188 }
2189 
2190 int
2191 vn_seek(struct file *fp, off_t offset, int whence, struct thread *td)
2192 {
2193 	struct ucred *cred;
2194 	struct vnode *vp;
2195 	struct vattr vattr;
2196 	off_t foffset, size;
2197 	int error, noneg;
2198 
2199 	cred = td->td_ucred;
2200 	vp = fp->f_vnode;
2201 	foffset = foffset_lock(fp, 0);
2202 	noneg = (vp->v_type != VCHR);
2203 	error = 0;
2204 	switch (whence) {
2205 	case L_INCR:
2206 		if (noneg &&
2207 		    (foffset < 0 ||
2208 		    (offset > 0 && foffset > OFF_MAX - offset))) {
2209 			error = EOVERFLOW;
2210 			break;
2211 		}
2212 		offset += foffset;
2213 		break;
2214 	case L_XTND:
2215 		vn_lock(vp, LK_SHARED | LK_RETRY);
2216 		error = VOP_GETATTR(vp, &vattr, cred);
2217 		VOP_UNLOCK(vp, 0);
2218 		if (error)
2219 			break;
2220 
2221 		/*
2222 		 * If the file references a disk device, then fetch
2223 		 * the media size and use that to determine the ending
2224 		 * offset.
2225 		 */
2226 		if (vattr.va_size == 0 && vp->v_type == VCHR &&
2227 		    fo_ioctl(fp, DIOCGMEDIASIZE, &size, cred, td) == 0)
2228 			vattr.va_size = size;
2229 		if (noneg &&
2230 		    (vattr.va_size > OFF_MAX ||
2231 		    (offset > 0 && vattr.va_size > OFF_MAX - offset))) {
2232 			error = EOVERFLOW;
2233 			break;
2234 		}
2235 		offset += vattr.va_size;
2236 		break;
2237 	case L_SET:
2238 		break;
2239 	case SEEK_DATA:
2240 		error = fo_ioctl(fp, FIOSEEKDATA, &offset, cred, td);
2241 		break;
2242 	case SEEK_HOLE:
2243 		error = fo_ioctl(fp, FIOSEEKHOLE, &offset, cred, td);
2244 		break;
2245 	default:
2246 		error = EINVAL;
2247 	}
2248 	if (error == 0 && noneg && offset < 0)
2249 		error = EINVAL;
2250 	if (error != 0)
2251 		goto drop;
2252 	VFS_KNOTE_UNLOCKED(vp, 0);
2253 	td->td_uretoff.tdu_off = offset;
2254 drop:
2255 	foffset_unlock(fp, offset, error != 0 ? FOF_NOUPDATE : 0);
2256 	return (error);
2257 }
2258 
2259 int
2260 vn_utimes_perm(struct vnode *vp, struct vattr *vap, struct ucred *cred,
2261     struct thread *td)
2262 {
2263 	int error;
2264 
2265 	/*
2266 	 * Grant permission if the caller is the owner of the file, or
2267 	 * the super-user, or has ACL_WRITE_ATTRIBUTES permission on
2268 	 * on the file.  If the time pointer is null, then write
2269 	 * permission on the file is also sufficient.
2270 	 *
2271 	 * From NFSv4.1, draft 21, 6.2.1.3.1, Discussion of Mask Attributes:
2272 	 * A user having ACL_WRITE_DATA or ACL_WRITE_ATTRIBUTES
2273 	 * will be allowed to set the times [..] to the current
2274 	 * server time.
2275 	 */
2276 	error = VOP_ACCESSX(vp, VWRITE_ATTRIBUTES, cred, td);
2277 	if (error != 0 && (vap->va_vaflags & VA_UTIMES_NULL) != 0)
2278 		error = VOP_ACCESS(vp, VWRITE, cred, td);
2279 	return (error);
2280 }
2281 
2282 int
2283 vn_fill_kinfo(struct file *fp, struct kinfo_file *kif, struct filedesc *fdp)
2284 {
2285 	struct vnode *vp;
2286 	int error;
2287 
2288 	if (fp->f_type == DTYPE_FIFO)
2289 		kif->kf_type = KF_TYPE_FIFO;
2290 	else
2291 		kif->kf_type = KF_TYPE_VNODE;
2292 	vp = fp->f_vnode;
2293 	vref(vp);
2294 	FILEDESC_SUNLOCK(fdp);
2295 	error = vn_fill_kinfo_vnode(vp, kif);
2296 	vrele(vp);
2297 	FILEDESC_SLOCK(fdp);
2298 	return (error);
2299 }
2300 
2301 static inline void
2302 vn_fill_junk(struct kinfo_file *kif)
2303 {
2304 	size_t len, olen;
2305 
2306 	/*
2307 	 * Simulate vn_fullpath returning changing values for a given
2308 	 * vp during e.g. coredump.
2309 	 */
2310 	len = (arc4random() % (sizeof(kif->kf_path) - 2)) + 1;
2311 	olen = strlen(kif->kf_path);
2312 	if (len < olen)
2313 		strcpy(&kif->kf_path[len - 1], "$");
2314 	else
2315 		for (; olen < len; olen++)
2316 			strcpy(&kif->kf_path[olen], "A");
2317 }
2318 
2319 int
2320 vn_fill_kinfo_vnode(struct vnode *vp, struct kinfo_file *kif)
2321 {
2322 	struct vattr va;
2323 	char *fullpath, *freepath;
2324 	int error;
2325 
2326 	kif->kf_un.kf_file.kf_file_type = vntype_to_kinfo(vp->v_type);
2327 	freepath = NULL;
2328 	fullpath = "-";
2329 	error = vn_fullpath(curthread, vp, &fullpath, &freepath);
2330 	if (error == 0) {
2331 		strlcpy(kif->kf_path, fullpath, sizeof(kif->kf_path));
2332 	}
2333 	if (freepath != NULL)
2334 		free(freepath, M_TEMP);
2335 
2336 	KFAIL_POINT_CODE(DEBUG_FP, fill_kinfo_vnode__random_path,
2337 		vn_fill_junk(kif);
2338 	);
2339 
2340 	/*
2341 	 * Retrieve vnode attributes.
2342 	 */
2343 	va.va_fsid = VNOVAL;
2344 	va.va_rdev = NODEV;
2345 	vn_lock(vp, LK_SHARED | LK_RETRY);
2346 	error = VOP_GETATTR(vp, &va, curthread->td_ucred);
2347 	VOP_UNLOCK(vp, 0);
2348 	if (error != 0)
2349 		return (error);
2350 	if (va.va_fsid != VNOVAL)
2351 		kif->kf_un.kf_file.kf_file_fsid = va.va_fsid;
2352 	else
2353 		kif->kf_un.kf_file.kf_file_fsid =
2354 		    vp->v_mount->mnt_stat.f_fsid.val[0];
2355 	kif->kf_un.kf_file.kf_file_fsid_freebsd11 =
2356 	    kif->kf_un.kf_file.kf_file_fsid; /* truncate */
2357 	kif->kf_un.kf_file.kf_file_fileid = va.va_fileid;
2358 	kif->kf_un.kf_file.kf_file_mode = MAKEIMODE(va.va_type, va.va_mode);
2359 	kif->kf_un.kf_file.kf_file_size = va.va_size;
2360 	kif->kf_un.kf_file.kf_file_rdev = va.va_rdev;
2361 	kif->kf_un.kf_file.kf_file_rdev_freebsd11 =
2362 	    kif->kf_un.kf_file.kf_file_rdev; /* truncate */
2363 	return (0);
2364 }
2365 
2366 int
2367 vn_mmap(struct file *fp, vm_map_t map, vm_offset_t *addr, vm_size_t size,
2368     vm_prot_t prot, vm_prot_t cap_maxprot, int flags, vm_ooffset_t foff,
2369     struct thread *td)
2370 {
2371 #ifdef HWPMC_HOOKS
2372 	struct pmckern_map_in pkm;
2373 #endif
2374 	struct mount *mp;
2375 	struct vnode *vp;
2376 	vm_object_t object;
2377 	vm_prot_t maxprot;
2378 	boolean_t writecounted;
2379 	int error;
2380 
2381 #if defined(COMPAT_FREEBSD7) || defined(COMPAT_FREEBSD6) || \
2382     defined(COMPAT_FREEBSD5) || defined(COMPAT_FREEBSD4)
2383 	/*
2384 	 * POSIX shared-memory objects are defined to have
2385 	 * kernel persistence, and are not defined to support
2386 	 * read(2)/write(2) -- or even open(2).  Thus, we can
2387 	 * use MAP_ASYNC to trade on-disk coherence for speed.
2388 	 * The shm_open(3) library routine turns on the FPOSIXSHM
2389 	 * flag to request this behavior.
2390 	 */
2391 	if ((fp->f_flag & FPOSIXSHM) != 0)
2392 		flags |= MAP_NOSYNC;
2393 #endif
2394 	vp = fp->f_vnode;
2395 
2396 	/*
2397 	 * Ensure that file and memory protections are
2398 	 * compatible.  Note that we only worry about
2399 	 * writability if mapping is shared; in this case,
2400 	 * current and max prot are dictated by the open file.
2401 	 * XXX use the vnode instead?  Problem is: what
2402 	 * credentials do we use for determination? What if
2403 	 * proc does a setuid?
2404 	 */
2405 	mp = vp->v_mount;
2406 	if (mp != NULL && (mp->mnt_flag & MNT_NOEXEC) != 0) {
2407 		maxprot = VM_PROT_NONE;
2408 		if ((prot & VM_PROT_EXECUTE) != 0)
2409 			return (EACCES);
2410 	} else
2411 		maxprot = VM_PROT_EXECUTE;
2412 	if ((fp->f_flag & FREAD) != 0)
2413 		maxprot |= VM_PROT_READ;
2414 	else if ((prot & VM_PROT_READ) != 0)
2415 		return (EACCES);
2416 
2417 	/*
2418 	 * If we are sharing potential changes via MAP_SHARED and we
2419 	 * are trying to get write permission although we opened it
2420 	 * without asking for it, bail out.
2421 	 */
2422 	if ((flags & MAP_SHARED) != 0) {
2423 		if ((fp->f_flag & FWRITE) != 0)
2424 			maxprot |= VM_PROT_WRITE;
2425 		else if ((prot & VM_PROT_WRITE) != 0)
2426 			return (EACCES);
2427 	} else {
2428 		maxprot |= VM_PROT_WRITE;
2429 		cap_maxprot |= VM_PROT_WRITE;
2430 	}
2431 	maxprot &= cap_maxprot;
2432 
2433 	/*
2434 	 * For regular files and shared memory, POSIX requires that
2435 	 * the value of foff be a legitimate offset within the data
2436 	 * object.  In particular, negative offsets are invalid.
2437 	 * Blocking negative offsets and overflows here avoids
2438 	 * possible wraparound or user-level access into reserved
2439 	 * ranges of the data object later.  In contrast, POSIX does
2440 	 * not dictate how offsets are used by device drivers, so in
2441 	 * the case of a device mapping a negative offset is passed
2442 	 * on.
2443 	 */
2444 	if (
2445 #ifdef _LP64
2446 	    size > OFF_MAX ||
2447 #endif
2448 	    foff < 0 || foff > OFF_MAX - size)
2449 		return (EINVAL);
2450 
2451 	writecounted = FALSE;
2452 	error = vm_mmap_vnode(td, size, prot, &maxprot, &flags, vp,
2453 	    &foff, &object, &writecounted);
2454 	if (error != 0)
2455 		return (error);
2456 	error = vm_mmap_object(map, addr, size, prot, maxprot, flags, object,
2457 	    foff, writecounted, td);
2458 	if (error != 0) {
2459 		/*
2460 		 * If this mapping was accounted for in the vnode's
2461 		 * writecount, then undo that now.
2462 		 */
2463 		if (writecounted)
2464 			vnode_pager_release_writecount(object, 0, size);
2465 		vm_object_deallocate(object);
2466 	}
2467 #ifdef HWPMC_HOOKS
2468 	/* Inform hwpmc(4) if an executable is being mapped. */
2469 	if (PMC_HOOK_INSTALLED(PMC_FN_MMAP)) {
2470 		if ((prot & VM_PROT_EXECUTE) != 0 && error == 0) {
2471 			pkm.pm_file = vp;
2472 			pkm.pm_address = (uintptr_t) *addr;
2473 			PMC_CALL_HOOK_UNLOCKED(td, PMC_FN_MMAP, (void *) &pkm);
2474 		}
2475 	}
2476 #endif
2477 	return (error);
2478 }
2479 
2480 void
2481 vn_fsid(struct vnode *vp, struct vattr *va)
2482 {
2483 	fsid_t *f;
2484 
2485 	f = &vp->v_mount->mnt_stat.f_fsid;
2486 	va->va_fsid = (uint32_t)f->val[1];
2487 	va->va_fsid <<= sizeof(f->val[1]) * NBBY;
2488 	va->va_fsid += (uint32_t)f->val[0];
2489 }
2490