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.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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
34 * @(#)vfs_vnops.c 8.2 (Berkeley) 1/21/94
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/fcntl.h>
48 #include <sys/limits.h>
50 #include <sys/mount.h>
51 #include <sys/mutex.h>
52 #include <sys/namei.h>
53 #include <sys/vnode.h>
56 #include <sys/filio.h>
57 #include <sys/resourcevar.h>
59 #include <sys/sysctl.h>
60 #include <sys/ttycom.h>
62 #include <sys/syslog.h>
63 #include <sys/unistd.h>
65 #include <security/audit/audit.h>
66 #include <security/mac/mac_framework.h>
69 #include <vm/vm_extern.h>
71 #include <vm/vm_map.h>
72 #include <vm/vm_object.h>
73 #include <vm/vm_page.h>
75 static fo_rdwr_t vn_read;
76 static fo_rdwr_t vn_write;
77 static fo_rdwr_t vn_io_fault;
78 static fo_truncate_t vn_truncate;
79 static fo_ioctl_t vn_ioctl;
80 static fo_poll_t vn_poll;
81 static fo_kqfilter_t vn_kqfilter;
82 static fo_stat_t vn_statfile;
83 static fo_close_t vn_closefile;
85 struct fileops vnops = {
86 .fo_read = vn_io_fault,
87 .fo_write = vn_io_fault,
88 .fo_truncate = vn_truncate,
91 .fo_kqfilter = vn_kqfilter,
92 .fo_stat = vn_statfile,
93 .fo_close = vn_closefile,
96 .fo_flags = DFLAG_PASSABLE | DFLAG_SEEKABLE
100 vn_open(ndp, flagp, cmode, fp)
101 struct nameidata *ndp;
105 struct thread *td = ndp->ni_cnd.cn_thread;
107 return (vn_open_cred(ndp, flagp, cmode, 0, td->td_ucred, fp));
111 * Common code for vnode open operations via a name lookup.
112 * Lookup the vnode and invoke VOP_CREATE if needed.
113 * Check permissions, and call the VOP_OPEN or VOP_CREATE routine.
115 * Note that this does NOT free nameidata for the successful case,
116 * due to the NDINIT being done elsewhere.
119 vn_open_cred(struct nameidata *ndp, int *flagp, int cmode, u_int vn_open_flags,
120 struct ucred *cred, struct file *fp)
124 struct thread *td = ndp->ni_cnd.cn_thread;
126 struct vattr *vap = &vat;
131 if (fmode & O_CREAT) {
132 ndp->ni_cnd.cn_nameiop = CREATE;
133 ndp->ni_cnd.cn_flags = ISOPEN | LOCKPARENT | LOCKLEAF;
134 if ((fmode & O_EXCL) == 0 && (fmode & O_NOFOLLOW) == 0)
135 ndp->ni_cnd.cn_flags |= FOLLOW;
136 if (!(vn_open_flags & VN_OPEN_NOAUDIT))
137 ndp->ni_cnd.cn_flags |= AUDITVNODE1;
138 if (vn_open_flags & VN_OPEN_NOCAPCHECK)
139 ndp->ni_cnd.cn_flags |= NOCAPCHECK;
141 if ((error = namei(ndp)) != 0)
143 if (ndp->ni_vp == NULL) {
146 vap->va_mode = cmode;
148 vap->va_vaflags |= VA_EXCLUSIVE;
149 if (vn_start_write(ndp->ni_dvp, &mp, V_NOWAIT) != 0) {
150 NDFREE(ndp, NDF_ONLY_PNBUF);
152 if ((error = vn_start_write(NULL, &mp,
153 V_XSLEEP | PCATCH)) != 0)
158 error = mac_vnode_check_create(cred, ndp->ni_dvp,
162 error = VOP_CREATE(ndp->ni_dvp, &ndp->ni_vp,
165 vn_finished_write(mp);
167 NDFREE(ndp, NDF_ONLY_PNBUF);
173 if (ndp->ni_dvp == ndp->ni_vp)
179 if (fmode & O_EXCL) {
186 ndp->ni_cnd.cn_nameiop = LOOKUP;
187 ndp->ni_cnd.cn_flags = ISOPEN |
188 ((fmode & O_NOFOLLOW) ? NOFOLLOW : FOLLOW) | LOCKLEAF;
189 if (!(fmode & FWRITE))
190 ndp->ni_cnd.cn_flags |= LOCKSHARED;
191 if (!(vn_open_flags & VN_OPEN_NOAUDIT))
192 ndp->ni_cnd.cn_flags |= AUDITVNODE1;
193 if (vn_open_flags & VN_OPEN_NOCAPCHECK)
194 ndp->ni_cnd.cn_flags |= NOCAPCHECK;
195 if ((error = namei(ndp)) != 0)
199 error = vn_open_vnode(vp, fmode, cred, td, fp);
205 NDFREE(ndp, NDF_ONLY_PNBUF);
213 * Common code for vnode open operations once a vnode is located.
214 * Check permissions, and call the VOP_OPEN routine.
217 vn_open_vnode(struct vnode *vp, int fmode, struct ucred *cred,
218 struct thread *td, struct file *fp)
223 int error, have_flock, lock_flags, type;
225 if (vp->v_type == VLNK)
227 if (vp->v_type == VSOCK)
229 if (vp->v_type != VDIR && fmode & O_DIRECTORY)
232 if (fmode & (FWRITE | O_TRUNC)) {
233 if (vp->v_type == VDIR)
241 if ((fmode & O_APPEND) && (fmode & FWRITE))
244 error = mac_vnode_check_open(cred, vp, accmode);
248 if ((fmode & O_CREAT) == 0) {
249 if (accmode & VWRITE) {
250 error = vn_writechk(vp);
255 error = VOP_ACCESS(vp, accmode, cred, td);
260 if ((error = VOP_OPEN(vp, fmode, cred, td, fp)) != 0)
263 if (fmode & (O_EXLOCK | O_SHLOCK)) {
264 KASSERT(fp != NULL, ("open with flock requires fp"));
265 lock_flags = VOP_ISLOCKED(vp);
267 lf.l_whence = SEEK_SET;
270 if (fmode & O_EXLOCK)
275 if ((fmode & FNONBLOCK) == 0)
277 error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type);
278 have_flock = (error == 0);
279 vn_lock(vp, lock_flags | LK_RETRY);
280 if (error == 0 && vp->v_iflag & VI_DOOMED)
283 * Another thread might have used this vnode as an
284 * executable while the vnode lock was dropped.
285 * Ensure the vnode is still able to be opened for
286 * writing after the lock has been obtained.
288 if (error == 0 && accmode & VWRITE)
289 error = vn_writechk(vp);
293 lf.l_whence = SEEK_SET;
297 (void) VOP_ADVLOCK(vp, fp, F_UNLCK, &lf,
300 vn_start_write(vp, &mp, V_WAIT);
301 vn_lock(vp, lock_flags | LK_RETRY);
302 (void)VOP_CLOSE(vp, fmode, cred, td);
303 vn_finished_write(mp);
306 fp->f_flag |= FHASLOCK;
308 if (fmode & FWRITE) {
309 VOP_ADD_WRITECOUNT(vp, 1);
310 CTR3(KTR_VFS, "%s: vp %p v_writecount increased to %d",
311 __func__, vp, vp->v_writecount);
313 ASSERT_VOP_LOCKED(vp, "vn_open_vnode");
318 * Check for write permissions on the specified vnode.
319 * Prototype text segments cannot be written.
323 register struct vnode *vp;
326 ASSERT_VOP_LOCKED(vp, "vn_writechk");
328 * If there's shared text associated with
329 * the vnode, try to free it up once. If
330 * we fail, we can't allow writing.
342 vn_close(vp, flags, file_cred, td)
343 register struct vnode *vp;
345 struct ucred *file_cred;
349 int error, lock_flags;
351 if (!(flags & FWRITE) && vp->v_mount != NULL &&
352 vp->v_mount->mnt_kern_flag & MNTK_EXTENDED_SHARED)
353 lock_flags = LK_SHARED;
355 lock_flags = LK_EXCLUSIVE;
357 vn_start_write(vp, &mp, V_WAIT);
358 vn_lock(vp, lock_flags | LK_RETRY);
359 if (flags & FWRITE) {
360 VNASSERT(vp->v_writecount > 0, vp,
361 ("vn_close: negative writecount"));
362 VOP_ADD_WRITECOUNT(vp, -1);
363 CTR3(KTR_VFS, "%s: vp %p v_writecount decreased to %d",
364 __func__, vp, vp->v_writecount);
366 error = VOP_CLOSE(vp, flags, file_cred, td);
368 vn_finished_write(mp);
373 * Heuristic to detect sequential operation.
376 sequential_heuristic(struct uio *uio, struct file *fp)
379 if (atomic_load_acq_int(&(fp->f_flag)) & FRDAHEAD)
380 return (fp->f_seqcount << IO_SEQSHIFT);
383 * Offset 0 is handled specially. open() sets f_seqcount to 1 so
384 * that the first I/O is normally considered to be slightly
385 * sequential. Seeking to offset 0 doesn't change sequentiality
386 * unless previous seeks have reduced f_seqcount to 0, in which
387 * case offset 0 is not special.
389 if ((uio->uio_offset == 0 && fp->f_seqcount > 0) ||
390 uio->uio_offset == fp->f_nextoff) {
392 * f_seqcount is in units of fixed-size blocks so that it
393 * depends mainly on the amount of sequential I/O and not
394 * much on the number of sequential I/O's. The fixed size
395 * of 16384 is hard-coded here since it is (not quite) just
396 * a magic size that works well here. This size is more
397 * closely related to the best I/O size for real disks than
398 * to any block size used by software.
400 fp->f_seqcount += howmany(uio->uio_resid, 16384);
401 if (fp->f_seqcount > IO_SEQMAX)
402 fp->f_seqcount = IO_SEQMAX;
403 return (fp->f_seqcount << IO_SEQSHIFT);
406 /* Not sequential. Quickly draw-down sequentiality. */
407 if (fp->f_seqcount > 1)
415 * Package up an I/O request on a vnode into a uio and do it.
418 vn_rdwr(enum uio_rw rw, struct vnode *vp, void *base, int len, off_t offset,
419 enum uio_seg segflg, int ioflg, struct ucred *active_cred,
420 struct ucred *file_cred, ssize_t *aresid, struct thread *td)
427 int error, lock_flags;
429 auio.uio_iov = &aiov;
431 aiov.iov_base = base;
433 auio.uio_resid = len;
434 auio.uio_offset = offset;
435 auio.uio_segflg = segflg;
440 if ((ioflg & IO_NODELOCKED) == 0) {
441 if (rw == UIO_READ) {
442 rl_cookie = vn_rangelock_rlock(vp, offset,
445 rl_cookie = vn_rangelock_wlock(vp, offset,
449 if (rw == UIO_WRITE) {
450 if (vp->v_type != VCHR &&
451 (error = vn_start_write(vp, &mp, V_WAIT | PCATCH))
454 if (MNT_SHARED_WRITES(mp) ||
455 ((mp == NULL) && MNT_SHARED_WRITES(vp->v_mount)))
456 lock_flags = LK_SHARED;
458 lock_flags = LK_EXCLUSIVE;
460 lock_flags = LK_SHARED;
461 vn_lock(vp, lock_flags | LK_RETRY);
465 ASSERT_VOP_LOCKED(vp, "IO_NODELOCKED with no vp lock held");
467 if ((ioflg & IO_NOMACCHECK) == 0) {
469 error = mac_vnode_check_read(active_cred, file_cred,
472 error = mac_vnode_check_write(active_cred, file_cred,
477 if (file_cred != NULL)
482 error = VOP_READ(vp, &auio, ioflg, cred);
484 error = VOP_WRITE(vp, &auio, ioflg, cred);
487 *aresid = auio.uio_resid;
489 if (auio.uio_resid && error == 0)
491 if ((ioflg & IO_NODELOCKED) == 0) {
494 vn_finished_write(mp);
497 if (rl_cookie != NULL)
498 vn_rangelock_unlock(vp, rl_cookie);
503 * Package up an I/O request on a vnode into a uio and do it. The I/O
504 * request is split up into smaller chunks and we try to avoid saturating
505 * the buffer cache while potentially holding a vnode locked, so we
506 * check bwillwrite() before calling vn_rdwr(). We also call kern_yield()
507 * to give other processes a chance to lock the vnode (either other processes
508 * core'ing the same binary, or unrelated processes scanning the directory).
511 vn_rdwr_inchunks(rw, vp, base, len, offset, segflg, ioflg, active_cred,
512 file_cred, aresid, td)
520 struct ucred *active_cred;
521 struct ucred *file_cred;
532 * Force `offset' to a multiple of MAXBSIZE except possibly
533 * for the first chunk, so that filesystems only need to
534 * write full blocks except possibly for the first and last
537 chunk = MAXBSIZE - (uoff_t)offset % MAXBSIZE;
541 if (rw != UIO_READ && vp->v_type == VREG)
544 error = vn_rdwr(rw, vp, base, chunk, offset, segflg,
545 ioflg, active_cred, file_cred, &iaresid, td);
546 len -= chunk; /* aresid calc already includes length */
550 base = (char *)base + chunk;
551 kern_yield(PRI_USER);
554 *aresid = len + iaresid;
559 foffset_lock(struct file *fp, int flags)
564 KASSERT((flags & FOF_OFFSET) == 0, ("FOF_OFFSET passed"));
566 #if OFF_MAX <= LONG_MAX
568 * Caller only wants the current f_offset value. Assume that
569 * the long and shorter integer types reads are atomic.
571 if ((flags & FOF_NOLOCK) != 0)
572 return (fp->f_offset);
576 * According to McKusick the vn lock was protecting f_offset here.
577 * It is now protected by the FOFFSET_LOCKED flag.
579 mtxp = mtx_pool_find(mtxpool_sleep, fp);
581 if ((flags & FOF_NOLOCK) == 0) {
582 while (fp->f_vnread_flags & FOFFSET_LOCKED) {
583 fp->f_vnread_flags |= FOFFSET_LOCK_WAITING;
584 msleep(&fp->f_vnread_flags, mtxp, PUSER -1,
587 fp->f_vnread_flags |= FOFFSET_LOCKED;
595 foffset_unlock(struct file *fp, off_t val, int flags)
599 KASSERT((flags & FOF_OFFSET) == 0, ("FOF_OFFSET passed"));
601 #if OFF_MAX <= LONG_MAX
602 if ((flags & FOF_NOLOCK) != 0) {
603 if ((flags & FOF_NOUPDATE) == 0)
605 if ((flags & FOF_NEXTOFF) != 0)
611 mtxp = mtx_pool_find(mtxpool_sleep, fp);
613 if ((flags & FOF_NOUPDATE) == 0)
615 if ((flags & FOF_NEXTOFF) != 0)
617 if ((flags & FOF_NOLOCK) == 0) {
618 KASSERT((fp->f_vnread_flags & FOFFSET_LOCKED) != 0,
619 ("Lost FOFFSET_LOCKED"));
620 if (fp->f_vnread_flags & FOFFSET_LOCK_WAITING)
621 wakeup(&fp->f_vnread_flags);
622 fp->f_vnread_flags = 0;
628 foffset_lock_uio(struct file *fp, struct uio *uio, int flags)
631 if ((flags & FOF_OFFSET) == 0)
632 uio->uio_offset = foffset_lock(fp, flags);
636 foffset_unlock_uio(struct file *fp, struct uio *uio, int flags)
639 if ((flags & FOF_OFFSET) == 0)
640 foffset_unlock(fp, uio->uio_offset, flags);
644 get_advice(struct file *fp, struct uio *uio)
649 ret = POSIX_FADV_NORMAL;
650 if (fp->f_advice == NULL)
653 mtxp = mtx_pool_find(mtxpool_sleep, fp);
655 if (uio->uio_offset >= fp->f_advice->fa_start &&
656 uio->uio_offset + uio->uio_resid <= fp->f_advice->fa_end)
657 ret = fp->f_advice->fa_advice;
663 * File table vnode read routine.
666 vn_read(fp, uio, active_cred, flags, td)
669 struct ucred *active_cred;
677 off_t offset, start, end;
679 KASSERT(uio->uio_td == td, ("uio_td %p is not td %p",
681 KASSERT(flags & FOF_OFFSET, ("No FOF_OFFSET"));
684 if (fp->f_flag & FNONBLOCK)
686 if (fp->f_flag & O_DIRECT)
688 advice = get_advice(fp, uio);
689 vn_lock(vp, LK_SHARED | LK_RETRY);
692 case POSIX_FADV_NORMAL:
693 case POSIX_FADV_SEQUENTIAL:
694 case POSIX_FADV_NOREUSE:
695 ioflag |= sequential_heuristic(uio, fp);
697 case POSIX_FADV_RANDOM:
698 /* Disable read-ahead for random I/O. */
701 offset = uio->uio_offset;
704 error = mac_vnode_check_read(active_cred, fp->f_cred, vp);
707 error = VOP_READ(vp, uio, ioflag, fp->f_cred);
708 fp->f_nextoff = uio->uio_offset;
710 if (error == 0 && advice == POSIX_FADV_NOREUSE &&
711 offset != uio->uio_offset) {
713 * Use POSIX_FADV_DONTNEED to flush clean pages and
714 * buffers for the backing file after a
715 * POSIX_FADV_NOREUSE read(2). To optimize the common
716 * case of using POSIX_FADV_NOREUSE with sequential
717 * access, track the previous implicit DONTNEED
718 * request and grow this request to include the
719 * current read(2) in addition to the previous
720 * DONTNEED. With purely sequential access this will
721 * cause the DONTNEED requests to continously grow to
722 * cover all of the previously read regions of the
723 * file. This allows filesystem blocks that are
724 * accessed by multiple calls to read(2) to be flushed
725 * once the last read(2) finishes.
728 end = uio->uio_offset - 1;
729 mtxp = mtx_pool_find(mtxpool_sleep, fp);
731 if (fp->f_advice != NULL &&
732 fp->f_advice->fa_advice == POSIX_FADV_NOREUSE) {
733 if (start != 0 && fp->f_advice->fa_prevend + 1 == start)
734 start = fp->f_advice->fa_prevstart;
735 else if (fp->f_advice->fa_prevstart != 0 &&
736 fp->f_advice->fa_prevstart == end + 1)
737 end = fp->f_advice->fa_prevend;
738 fp->f_advice->fa_prevstart = start;
739 fp->f_advice->fa_prevend = end;
742 error = VOP_ADVISE(vp, start, end, POSIX_FADV_DONTNEED);
748 * File table vnode write routine.
751 vn_write(fp, uio, active_cred, flags, td)
754 struct ucred *active_cred;
761 int error, ioflag, lock_flags;
763 off_t offset, start, end;
765 KASSERT(uio->uio_td == td, ("uio_td %p is not td %p",
767 KASSERT(flags & FOF_OFFSET, ("No FOF_OFFSET"));
769 if (vp->v_type == VREG)
772 if (vp->v_type == VREG && (fp->f_flag & O_APPEND))
774 if (fp->f_flag & FNONBLOCK)
776 if (fp->f_flag & O_DIRECT)
778 if ((fp->f_flag & O_FSYNC) ||
779 (vp->v_mount && (vp->v_mount->mnt_flag & MNT_SYNCHRONOUS)))
782 if (vp->v_type != VCHR &&
783 (error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
786 advice = get_advice(fp, uio);
788 if (MNT_SHARED_WRITES(mp) ||
789 (mp == NULL && MNT_SHARED_WRITES(vp->v_mount))) {
790 lock_flags = LK_SHARED;
792 lock_flags = LK_EXCLUSIVE;
795 vn_lock(vp, lock_flags | LK_RETRY);
797 case POSIX_FADV_NORMAL:
798 case POSIX_FADV_SEQUENTIAL:
799 case POSIX_FADV_NOREUSE:
800 ioflag |= sequential_heuristic(uio, fp);
802 case POSIX_FADV_RANDOM:
803 /* XXX: Is this correct? */
806 offset = uio->uio_offset;
809 error = mac_vnode_check_write(active_cred, fp->f_cred, vp);
812 error = VOP_WRITE(vp, uio, ioflag, fp->f_cred);
813 fp->f_nextoff = uio->uio_offset;
815 if (vp->v_type != VCHR)
816 vn_finished_write(mp);
817 if (error == 0 && advice == POSIX_FADV_NOREUSE &&
818 offset != uio->uio_offset) {
820 * Use POSIX_FADV_DONTNEED to flush clean pages and
821 * buffers for the backing file after a
822 * POSIX_FADV_NOREUSE write(2). To optimize the
823 * common case of using POSIX_FADV_NOREUSE with
824 * sequential access, track the previous implicit
825 * DONTNEED request and grow this request to include
826 * the current write(2) in addition to the previous
827 * DONTNEED. With purely sequential access this will
828 * cause the DONTNEED requests to continously grow to
829 * cover all of the previously written regions of the
832 * Note that the blocks just written are almost
833 * certainly still dirty, so this only works when
834 * VOP_ADVISE() calls from subsequent writes push out
835 * the data written by this write(2) once the backing
836 * buffers are clean. However, as compared to forcing
837 * IO_DIRECT, this gives much saner behavior. Write
838 * clustering is still allowed, and clean pages are
839 * merely moved to the cache page queue rather than
840 * outright thrown away. This means a subsequent
841 * read(2) can still avoid hitting the disk if the
842 * pages have not been reclaimed.
844 * This does make POSIX_FADV_NOREUSE largely useless
845 * with non-sequential access. However, sequential
846 * access is the more common use case and the flag is
850 end = uio->uio_offset - 1;
851 mtxp = mtx_pool_find(mtxpool_sleep, fp);
853 if (fp->f_advice != NULL &&
854 fp->f_advice->fa_advice == POSIX_FADV_NOREUSE) {
855 if (start != 0 && fp->f_advice->fa_prevend + 1 == start)
856 start = fp->f_advice->fa_prevstart;
857 else if (fp->f_advice->fa_prevstart != 0 &&
858 fp->f_advice->fa_prevstart == end + 1)
859 end = fp->f_advice->fa_prevend;
860 fp->f_advice->fa_prevstart = start;
861 fp->f_advice->fa_prevend = end;
864 error = VOP_ADVISE(vp, start, end, POSIX_FADV_DONTNEED);
871 static const int io_hold_cnt = 16;
872 static int vn_io_fault_enable = 1;
873 SYSCTL_INT(_debug, OID_AUTO, vn_io_fault_enable, CTLFLAG_RW,
874 &vn_io_fault_enable, 0, "Enable vn_io_fault lock avoidance");
875 static u_long vn_io_faults_cnt;
876 SYSCTL_ULONG(_debug, OID_AUTO, vn_io_faults, CTLFLAG_RD,
877 &vn_io_faults_cnt, 0, "Count of vn_io_fault lock avoidance triggers");
880 * The vn_io_fault() is a wrapper around vn_read() and vn_write() to
881 * prevent the following deadlock:
883 * Assume that the thread A reads from the vnode vp1 into userspace
884 * buffer buf1 backed by the pages of vnode vp2. If a page in buf1 is
885 * currently not resident, then system ends up with the call chain
886 * vn_read() -> VOP_READ(vp1) -> uiomove() -> [Page Fault] ->
887 * vm_fault(buf1) -> vnode_pager_getpages(vp2) -> VOP_GETPAGES(vp2)
888 * which establishes lock order vp1->vn_lock, then vp2->vn_lock.
889 * If, at the same time, thread B reads from vnode vp2 into buffer buf2
890 * backed by the pages of vnode vp1, and some page in buf2 is not
891 * resident, we get a reversed order vp2->vn_lock, then vp1->vn_lock.
893 * To prevent the lock order reversal and deadlock, vn_io_fault() does
894 * not allow page faults to happen during VOP_READ() or VOP_WRITE().
895 * Instead, it first tries to do the whole range i/o with pagefaults
896 * disabled. If all pages in the i/o buffer are resident and mapped,
897 * VOP will succeed (ignoring the genuine filesystem errors).
898 * Otherwise, we get back EFAULT, and vn_io_fault() falls back to do
899 * i/o in chunks, with all pages in the chunk prefaulted and held
900 * using vm_fault_quick_hold_pages().
902 * Filesystems using this deadlock avoidance scheme should use the
903 * array of the held pages from uio, saved in the curthread->td_ma,
904 * instead of doing uiomove(). A helper function
905 * vn_io_fault_uiomove() converts uiomove request into
906 * uiomove_fromphys() over td_ma array.
908 * Since vnode locks do not cover the whole i/o anymore, rangelocks
909 * make the current i/o request atomic with respect to other i/os and
913 vn_io_fault(struct file *fp, struct uio *uio, struct ucred *active_cred,
914 int flags, struct thread *td)
916 vm_page_t ma[io_hold_cnt + 2];
917 struct uio *uio_clone, short_uio;
918 struct iovec short_iovec[1];
923 vm_page_t *prev_td_ma;
924 int cnt, error, save, saveheld, prev_td_ma_cnt;
925 vm_offset_t addr, end;
930 if (uio->uio_rw == UIO_READ)
935 foffset_lock_uio(fp, uio, flags);
937 if (uio->uio_segflg != UIO_USERSPACE || vp->v_type != VREG ||
938 ((mp = vp->v_mount) != NULL &&
939 (mp->mnt_kern_flag & MNTK_NO_IOPF) == 0) ||
940 !vn_io_fault_enable) {
941 error = doio(fp, uio, active_cred, flags | FOF_OFFSET, td);
946 * The UFS follows IO_UNIT directive and replays back both
947 * uio_offset and uio_resid if an error is encountered during the
948 * operation. But, since the iovec may be already advanced,
949 * uio is still in an inconsistent state.
951 * Cache a copy of the original uio, which is advanced to the redo
952 * point using UIO_NOCOPY below.
954 uio_clone = cloneuio(uio);
955 resid = uio->uio_resid;
957 short_uio.uio_segflg = UIO_USERSPACE;
958 short_uio.uio_rw = uio->uio_rw;
959 short_uio.uio_td = uio->uio_td;
961 if (uio->uio_rw == UIO_READ) {
962 prot = VM_PROT_WRITE;
963 rl_cookie = vn_rangelock_rlock(vp, uio->uio_offset,
964 uio->uio_offset + uio->uio_resid);
967 if ((fp->f_flag & O_APPEND) != 0 || (flags & FOF_OFFSET) == 0)
968 /* For appenders, punt and lock the whole range. */
969 rl_cookie = vn_rangelock_wlock(vp, 0, OFF_MAX);
971 rl_cookie = vn_rangelock_wlock(vp, uio->uio_offset,
972 uio->uio_offset + uio->uio_resid);
975 save = vm_fault_disable_pagefaults();
976 error = doio(fp, uio, active_cred, flags | FOF_OFFSET, td);
980 atomic_add_long(&vn_io_faults_cnt, 1);
981 uio_clone->uio_segflg = UIO_NOCOPY;
982 uiomove(NULL, resid - uio->uio_resid, uio_clone);
983 uio_clone->uio_segflg = uio->uio_segflg;
985 saveheld = curthread_pflags_set(TDP_UIOHELD);
986 prev_td_ma = td->td_ma;
987 prev_td_ma_cnt = td->td_ma_cnt;
989 while (uio_clone->uio_resid != 0) {
990 len = uio_clone->uio_iov->iov_len;
992 KASSERT(uio_clone->uio_iovcnt >= 1,
993 ("iovcnt underflow"));
994 uio_clone->uio_iov++;
995 uio_clone->uio_iovcnt--;
999 addr = (vm_offset_t)uio_clone->uio_iov->iov_base;
1000 end = round_page(addr + len);
1001 cnt = howmany(end - trunc_page(addr), PAGE_SIZE);
1003 * A perfectly misaligned address and length could cause
1004 * both the start and the end of the chunk to use partial
1005 * page. +2 accounts for such a situation.
1007 if (cnt > io_hold_cnt + 2) {
1008 len = io_hold_cnt * PAGE_SIZE;
1009 KASSERT(howmany(round_page(addr + len) -
1010 trunc_page(addr), PAGE_SIZE) <= io_hold_cnt + 2,
1013 cnt = vm_fault_quick_hold_pages(&td->td_proc->p_vmspace->vm_map,
1014 addr, len, prot, ma, io_hold_cnt + 2);
1019 short_uio.uio_iov = &short_iovec[0];
1020 short_iovec[0].iov_base = (void *)addr;
1021 short_uio.uio_iovcnt = 1;
1022 short_uio.uio_resid = short_iovec[0].iov_len = len;
1023 short_uio.uio_offset = uio_clone->uio_offset;
1025 td->td_ma_cnt = cnt;
1027 error = doio(fp, &short_uio, active_cred, flags | FOF_OFFSET,
1029 vm_page_unhold_pages(ma, cnt);
1030 adv = len - short_uio.uio_resid;
1032 uio_clone->uio_iov->iov_base =
1033 (char *)uio_clone->uio_iov->iov_base + adv;
1034 uio_clone->uio_iov->iov_len -= adv;
1035 uio_clone->uio_resid -= adv;
1036 uio_clone->uio_offset += adv;
1038 uio->uio_resid -= adv;
1039 uio->uio_offset += adv;
1041 if (error != 0 || adv == 0)
1044 td->td_ma = prev_td_ma;
1045 td->td_ma_cnt = prev_td_ma_cnt;
1046 curthread_pflags_restore(saveheld);
1048 vm_fault_enable_pagefaults(save);
1049 vn_rangelock_unlock(vp, rl_cookie);
1050 free(uio_clone, M_IOV);
1052 foffset_unlock_uio(fp, uio, flags);
1057 * Helper function to perform the requested uiomove operation using
1058 * the held pages for io->uio_iov[0].iov_base buffer instead of
1059 * copyin/copyout. Access to the pages with uiomove_fromphys()
1060 * instead of iov_base prevents page faults that could occur due to
1061 * pmap_collect() invalidating the mapping created by
1062 * vm_fault_quick_hold_pages(), or pageout daemon, page laundry or
1063 * object cleanup revoking the write access from page mappings.
1065 * Filesystems specified MNTK_NO_IOPF shall use vn_io_fault_uiomove()
1066 * instead of plain uiomove().
1069 vn_io_fault_uiomove(char *data, int xfersize, struct uio *uio)
1071 struct uio transp_uio;
1072 struct iovec transp_iov[1];
1078 if ((td->td_pflags & TDP_UIOHELD) == 0 ||
1079 uio->uio_segflg != UIO_USERSPACE)
1080 return (uiomove(data, xfersize, uio));
1082 KASSERT(uio->uio_iovcnt == 1, ("uio_iovcnt %d", uio->uio_iovcnt));
1083 transp_iov[0].iov_base = data;
1084 transp_uio.uio_iov = &transp_iov[0];
1085 transp_uio.uio_iovcnt = 1;
1086 if (xfersize > uio->uio_resid)
1087 xfersize = uio->uio_resid;
1088 transp_uio.uio_resid = transp_iov[0].iov_len = xfersize;
1089 transp_uio.uio_offset = 0;
1090 transp_uio.uio_segflg = UIO_SYSSPACE;
1092 * Since transp_iov points to data, and td_ma page array
1093 * corresponds to original uio->uio_iov, we need to invert the
1094 * direction of the i/o operation as passed to
1095 * uiomove_fromphys().
1097 switch (uio->uio_rw) {
1099 transp_uio.uio_rw = UIO_READ;
1102 transp_uio.uio_rw = UIO_WRITE;
1105 transp_uio.uio_td = uio->uio_td;
1106 error = uiomove_fromphys(td->td_ma,
1107 ((vm_offset_t)uio->uio_iov->iov_base) & PAGE_MASK,
1108 xfersize, &transp_uio);
1109 adv = xfersize - transp_uio.uio_resid;
1111 (((vm_offset_t)uio->uio_iov->iov_base + adv) >> PAGE_SHIFT) -
1112 (((vm_offset_t)uio->uio_iov->iov_base) >> PAGE_SHIFT);
1114 KASSERT(td->td_ma_cnt >= pgadv, ("consumed pages %d %d", td->td_ma_cnt,
1116 td->td_ma_cnt -= pgadv;
1117 uio->uio_iov->iov_base = (char *)uio->uio_iov->iov_base + adv;
1118 uio->uio_iov->iov_len -= adv;
1119 uio->uio_resid -= adv;
1120 uio->uio_offset += adv;
1125 * File table truncate routine.
1128 vn_truncate(struct file *fp, off_t length, struct ucred *active_cred,
1140 * Lock the whole range for truncation. Otherwise split i/o
1141 * might happen partly before and partly after the truncation.
1143 rl_cookie = vn_rangelock_wlock(vp, 0, OFF_MAX);
1144 error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
1147 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1148 if (vp->v_type == VDIR) {
1153 error = mac_vnode_check_write(active_cred, fp->f_cred, vp);
1157 error = vn_writechk(vp);
1160 vattr.va_size = length;
1161 error = VOP_SETATTR(vp, &vattr, fp->f_cred);
1165 vn_finished_write(mp);
1167 vn_rangelock_unlock(vp, rl_cookie);
1172 * File table vnode stat routine.
1175 vn_statfile(fp, sb, active_cred, td)
1178 struct ucred *active_cred;
1181 struct vnode *vp = fp->f_vnode;
1184 vn_lock(vp, LK_SHARED | LK_RETRY);
1185 error = vn_stat(vp, sb, active_cred, fp->f_cred, td);
1192 * Stat a vnode; implementation for the stat syscall
1195 vn_stat(vp, sb, active_cred, file_cred, td)
1197 register struct stat *sb;
1198 struct ucred *active_cred;
1199 struct ucred *file_cred;
1203 register struct vattr *vap;
1208 error = mac_vnode_check_stat(active_cred, file_cred, vp);
1216 * Initialize defaults for new and unusual fields, so that file
1217 * systems which don't support these fields don't need to know
1220 vap->va_birthtime.tv_sec = -1;
1221 vap->va_birthtime.tv_nsec = 0;
1222 vap->va_fsid = VNOVAL;
1223 vap->va_rdev = NODEV;
1225 error = VOP_GETATTR(vp, vap, active_cred);
1230 * Zero the spare stat fields
1232 bzero(sb, sizeof *sb);
1235 * Copy from vattr table
1237 if (vap->va_fsid != VNOVAL)
1238 sb->st_dev = vap->va_fsid;
1240 sb->st_dev = vp->v_mount->mnt_stat.f_fsid.val[0];
1241 sb->st_ino = vap->va_fileid;
1242 mode = vap->va_mode;
1243 switch (vap->va_type) {
1269 sb->st_nlink = vap->va_nlink;
1270 sb->st_uid = vap->va_uid;
1271 sb->st_gid = vap->va_gid;
1272 sb->st_rdev = vap->va_rdev;
1273 if (vap->va_size > OFF_MAX)
1275 sb->st_size = vap->va_size;
1276 sb->st_atim = vap->va_atime;
1277 sb->st_mtim = vap->va_mtime;
1278 sb->st_ctim = vap->va_ctime;
1279 sb->st_birthtim = vap->va_birthtime;
1282 * According to www.opengroup.org, the meaning of st_blksize is
1283 * "a filesystem-specific preferred I/O block size for this
1284 * object. In some filesystem types, this may vary from file
1286 * Use miminum/default of PAGE_SIZE (e.g. for VCHR).
1289 sb->st_blksize = max(PAGE_SIZE, vap->va_blocksize);
1291 sb->st_flags = vap->va_flags;
1292 if (priv_check(td, PRIV_VFS_GENERATION))
1295 sb->st_gen = vap->va_gen;
1297 sb->st_blocks = vap->va_bytes / S_BLKSIZE;
1302 * File table vnode ioctl routine.
1305 vn_ioctl(fp, com, data, active_cred, td)
1309 struct ucred *active_cred;
1312 struct vnode *vp = fp->f_vnode;
1317 switch (vp->v_type) {
1320 if (com == FIONREAD) {
1321 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1322 error = VOP_GETATTR(vp, &vattr, active_cred);
1325 *(int *)data = vattr.va_size - fp->f_offset;
1327 if (com == FIONBIO || com == FIOASYNC) /* XXX */
1330 error = VOP_IOCTL(vp, com, data, fp->f_flag,
1341 * File table vnode poll routine.
1344 vn_poll(fp, events, active_cred, td)
1347 struct ucred *active_cred;
1355 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1356 error = mac_vnode_check_poll(active_cred, fp->f_cred, vp);
1361 error = VOP_POLL(vp, events, fp->f_cred, td);
1366 * Acquire the requested lock and then check for validity. LK_RETRY
1367 * permits vn_lock to return doomed vnodes.
1370 _vn_lock(struct vnode *vp, int flags, char *file, int line)
1374 VNASSERT((flags & LK_TYPE_MASK) != 0, vp,
1375 ("vn_lock called with no locktype."));
1377 #ifdef DEBUG_VFS_LOCKS
1378 KASSERT(vp->v_holdcnt != 0,
1379 ("vn_lock %p: zero hold count", vp));
1381 error = VOP_LOCK1(vp, flags, file, line);
1382 flags &= ~LK_INTERLOCK; /* Interlock is always dropped. */
1383 KASSERT((flags & LK_RETRY) == 0 || error == 0,
1384 ("LK_RETRY set with incompatible flags (0x%x) or an error occured (%d)",
1387 * Callers specify LK_RETRY if they wish to get dead vnodes.
1388 * If RETRY is not set, we return ENOENT instead.
1390 if (error == 0 && vp->v_iflag & VI_DOOMED &&
1391 (flags & LK_RETRY) == 0) {
1396 } while (flags & LK_RETRY && error != 0);
1401 * File table vnode close routine.
1404 vn_closefile(fp, td)
1413 fp->f_ops = &badfileops;
1415 if (fp->f_type == DTYPE_VNODE && fp->f_flag & FHASLOCK)
1418 error = vn_close(vp, fp->f_flag, fp->f_cred, td);
1420 if (fp->f_type == DTYPE_VNODE && fp->f_flag & FHASLOCK) {
1421 lf.l_whence = SEEK_SET;
1424 lf.l_type = F_UNLCK;
1425 (void) VOP_ADVLOCK(vp, fp, F_UNLCK, &lf, F_FLOCK);
1432 * Preparing to start a filesystem write operation. If the operation is
1433 * permitted, then we bump the count of operations in progress and
1434 * proceed. If a suspend request is in progress, we wait until the
1435 * suspension is over, and then proceed.
1438 vn_start_write_locked(struct mount *mp, int flags)
1442 mtx_assert(MNT_MTX(mp), MA_OWNED);
1446 * Check on status of suspension.
1448 if ((curthread->td_pflags & TDP_IGNSUSP) == 0 ||
1449 mp->mnt_susp_owner != curthread) {
1450 while ((mp->mnt_kern_flag & MNTK_SUSPEND) != 0) {
1451 if (flags & V_NOWAIT) {
1452 error = EWOULDBLOCK;
1455 error = msleep(&mp->mnt_flag, MNT_MTX(mp),
1456 (PUSER - 1) | (flags & PCATCH), "suspfs", 0);
1461 if (flags & V_XSLEEP)
1463 mp->mnt_writeopcount++;
1465 if (error != 0 || (flags & V_XSLEEP) != 0)
1472 vn_start_write(vp, mpp, flags)
1482 * If a vnode is provided, get and return the mount point that
1483 * to which it will write.
1486 if ((error = VOP_GETWRITEMOUNT(vp, mpp)) != 0) {
1488 if (error != EOPNOTSUPP)
1493 if ((mp = *mpp) == NULL)
1497 * VOP_GETWRITEMOUNT() returns with the mp refcount held through
1499 * As long as a vnode is not provided we need to acquire a
1500 * refcount for the provided mountpoint too, in order to
1501 * emulate a vfs_ref().
1507 return (vn_start_write_locked(mp, flags));
1511 * Secondary suspension. Used by operations such as vop_inactive
1512 * routines that are needed by the higher level functions. These
1513 * are allowed to proceed until all the higher level functions have
1514 * completed (indicated by mnt_writeopcount dropping to zero). At that
1515 * time, these operations are halted until the suspension is over.
1518 vn_start_secondary_write(vp, mpp, flags)
1528 if ((error = VOP_GETWRITEMOUNT(vp, mpp)) != 0) {
1530 if (error != EOPNOTSUPP)
1536 * If we are not suspended or have not yet reached suspended
1537 * mode, then let the operation proceed.
1539 if ((mp = *mpp) == NULL)
1543 * VOP_GETWRITEMOUNT() returns with the mp refcount held through
1545 * As long as a vnode is not provided we need to acquire a
1546 * refcount for the provided mountpoint too, in order to
1547 * emulate a vfs_ref().
1552 if ((mp->mnt_kern_flag & (MNTK_SUSPENDED | MNTK_SUSPEND2)) == 0) {
1553 mp->mnt_secondary_writes++;
1554 mp->mnt_secondary_accwrites++;
1558 if (flags & V_NOWAIT) {
1561 return (EWOULDBLOCK);
1564 * Wait for the suspension to finish.
1566 error = msleep(&mp->mnt_flag, MNT_MTX(mp),
1567 (PUSER - 1) | (flags & PCATCH) | PDROP, "suspfs", 0);
1575 * Filesystem write operation has completed. If we are suspending and this
1576 * operation is the last one, notify the suspender that the suspension is
1580 vn_finished_write(mp)
1587 mp->mnt_writeopcount--;
1588 if (mp->mnt_writeopcount < 0)
1589 panic("vn_finished_write: neg cnt");
1590 if ((mp->mnt_kern_flag & MNTK_SUSPEND) != 0 &&
1591 mp->mnt_writeopcount <= 0)
1592 wakeup(&mp->mnt_writeopcount);
1598 * Filesystem secondary write operation has completed. If we are
1599 * suspending and this operation is the last one, notify the suspender
1600 * that the suspension is now in effect.
1603 vn_finished_secondary_write(mp)
1610 mp->mnt_secondary_writes--;
1611 if (mp->mnt_secondary_writes < 0)
1612 panic("vn_finished_secondary_write: neg cnt");
1613 if ((mp->mnt_kern_flag & MNTK_SUSPEND) != 0 &&
1614 mp->mnt_secondary_writes <= 0)
1615 wakeup(&mp->mnt_secondary_writes);
1622 * Request a filesystem to suspend write operations.
1625 vfs_write_suspend(mp)
1631 if (mp->mnt_susp_owner == curthread) {
1635 while (mp->mnt_kern_flag & MNTK_SUSPEND)
1636 msleep(&mp->mnt_flag, MNT_MTX(mp), PUSER - 1, "wsuspfs", 0);
1637 mp->mnt_kern_flag |= MNTK_SUSPEND;
1638 mp->mnt_susp_owner = curthread;
1639 if (mp->mnt_writeopcount > 0)
1640 (void) msleep(&mp->mnt_writeopcount,
1641 MNT_MTX(mp), (PUSER - 1)|PDROP, "suspwt", 0);
1644 if ((error = VFS_SYNC(mp, MNT_SUSPEND)) != 0)
1645 vfs_write_resume(mp, 0);
1650 * Request a filesystem to resume write operations.
1653 vfs_write_resume(struct mount *mp, int flags)
1657 if ((mp->mnt_kern_flag & MNTK_SUSPEND) != 0) {
1658 KASSERT(mp->mnt_susp_owner == curthread, ("mnt_susp_owner"));
1659 mp->mnt_kern_flag &= ~(MNTK_SUSPEND | MNTK_SUSPEND2 |
1661 mp->mnt_susp_owner = NULL;
1662 wakeup(&mp->mnt_writeopcount);
1663 wakeup(&mp->mnt_flag);
1664 curthread->td_pflags &= ~TDP_IGNSUSP;
1665 if ((flags & VR_START_WRITE) != 0) {
1667 mp->mnt_writeopcount++;
1670 if ((flags & VR_NO_SUSPCLR) == 0)
1672 } else if ((flags & VR_START_WRITE) != 0) {
1674 vn_start_write_locked(mp, 0);
1681 * Implement kqueues for files by translating it to vnode operation.
1684 vn_kqfilter(struct file *fp, struct knote *kn)
1687 return (VOP_KQFILTER(fp->f_vnode, kn));
1691 * Simplified in-kernel wrapper calls for extended attribute access.
1692 * Both calls pass in a NULL credential, authorizing as "kernel" access.
1693 * Set IO_NODELOCKED in ioflg if the vnode is already locked.
1696 vn_extattr_get(struct vnode *vp, int ioflg, int attrnamespace,
1697 const char *attrname, int *buflen, char *buf, struct thread *td)
1703 iov.iov_len = *buflen;
1706 auio.uio_iov = &iov;
1707 auio.uio_iovcnt = 1;
1708 auio.uio_rw = UIO_READ;
1709 auio.uio_segflg = UIO_SYSSPACE;
1711 auio.uio_offset = 0;
1712 auio.uio_resid = *buflen;
1714 if ((ioflg & IO_NODELOCKED) == 0)
1715 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1717 ASSERT_VOP_LOCKED(vp, "IO_NODELOCKED with no vp lock held");
1719 /* authorize attribute retrieval as kernel */
1720 error = VOP_GETEXTATTR(vp, attrnamespace, attrname, &auio, NULL, NULL,
1723 if ((ioflg & IO_NODELOCKED) == 0)
1727 *buflen = *buflen - auio.uio_resid;
1734 * XXX failure mode if partially written?
1737 vn_extattr_set(struct vnode *vp, int ioflg, int attrnamespace,
1738 const char *attrname, int buflen, char *buf, struct thread *td)
1745 iov.iov_len = buflen;
1748 auio.uio_iov = &iov;
1749 auio.uio_iovcnt = 1;
1750 auio.uio_rw = UIO_WRITE;
1751 auio.uio_segflg = UIO_SYSSPACE;
1753 auio.uio_offset = 0;
1754 auio.uio_resid = buflen;
1756 if ((ioflg & IO_NODELOCKED) == 0) {
1757 if ((error = vn_start_write(vp, &mp, V_WAIT)) != 0)
1759 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1762 ASSERT_VOP_LOCKED(vp, "IO_NODELOCKED with no vp lock held");
1764 /* authorize attribute setting as kernel */
1765 error = VOP_SETEXTATTR(vp, attrnamespace, attrname, &auio, NULL, td);
1767 if ((ioflg & IO_NODELOCKED) == 0) {
1768 vn_finished_write(mp);
1776 vn_extattr_rm(struct vnode *vp, int ioflg, int attrnamespace,
1777 const char *attrname, struct thread *td)
1782 if ((ioflg & IO_NODELOCKED) == 0) {
1783 if ((error = vn_start_write(vp, &mp, V_WAIT)) != 0)
1785 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1788 ASSERT_VOP_LOCKED(vp, "IO_NODELOCKED with no vp lock held");
1790 /* authorize attribute removal as kernel */
1791 error = VOP_DELETEEXTATTR(vp, attrnamespace, attrname, NULL, td);
1792 if (error == EOPNOTSUPP)
1793 error = VOP_SETEXTATTR(vp, attrnamespace, attrname, NULL,
1796 if ((ioflg & IO_NODELOCKED) == 0) {
1797 vn_finished_write(mp);
1805 vn_vget_ino(struct vnode *vp, ino_t ino, int lkflags, struct vnode **rvp)
1811 ltype = VOP_ISLOCKED(vp);
1812 KASSERT(ltype == LK_EXCLUSIVE || ltype == LK_SHARED,
1813 ("vn_vget_ino: vp not locked"));
1814 error = vfs_busy(mp, MBF_NOWAIT);
1818 error = vfs_busy(mp, 0);
1819 vn_lock(vp, ltype | LK_RETRY);
1823 if (vp->v_iflag & VI_DOOMED) {
1829 error = VFS_VGET(mp, ino, lkflags, rvp);
1831 vn_lock(vp, ltype | LK_RETRY);
1832 if (vp->v_iflag & VI_DOOMED) {
1841 vn_rlimit_fsize(const struct vnode *vp, const struct uio *uio,
1842 const struct thread *td)
1845 if (vp->v_type != VREG || td == NULL)
1847 PROC_LOCK(td->td_proc);
1848 if ((uoff_t)uio->uio_offset + uio->uio_resid >
1849 lim_cur(td->td_proc, RLIMIT_FSIZE)) {
1850 kern_psignal(td->td_proc, SIGXFSZ);
1851 PROC_UNLOCK(td->td_proc);
1854 PROC_UNLOCK(td->td_proc);
1859 vn_chmod(struct file *fp, mode_t mode, struct ucred *active_cred,
1866 vn_lock(vp, LK_SHARED | LK_RETRY);
1867 AUDIT_ARG_VNODE1(vp);
1870 return (setfmode(td, active_cred, vp, mode));
1874 vn_chown(struct file *fp, uid_t uid, gid_t gid, struct ucred *active_cred,
1881 vn_lock(vp, LK_SHARED | LK_RETRY);
1882 AUDIT_ARG_VNODE1(vp);
1885 return (setfown(td, active_cred, vp, uid, gid));
1889 vn_pages_remove(struct vnode *vp, vm_pindex_t start, vm_pindex_t end)
1893 if ((object = vp->v_object) == NULL)
1895 VM_OBJECT_LOCK(object);
1896 vm_object_page_remove(object, start, end, 0);
1897 VM_OBJECT_UNLOCK(object);
1901 vn_bmap_seekhole(struct vnode *vp, u_long cmd, off_t *off, struct ucred *cred)
1909 KASSERT(cmd == FIOSEEKHOLE || cmd == FIOSEEKDATA,
1910 ("Wrong command %lu", cmd));
1912 if (vn_lock(vp, LK_SHARED) != 0)
1914 if (vp->v_type != VREG) {
1918 error = VOP_GETATTR(vp, &va, cred);
1922 if (noff >= va.va_size) {
1926 bsize = vp->v_mount->mnt_stat.f_iosize;
1927 for (bn = noff / bsize; noff < va.va_size; bn++, noff += bsize) {
1928 error = VOP_BMAP(vp, bn, NULL, &bnp, NULL, NULL);
1929 if (error == EOPNOTSUPP) {
1933 if ((bnp == -1 && cmd == FIOSEEKHOLE) ||
1934 (bnp != -1 && cmd == FIOSEEKDATA)) {
1941 if (noff > va.va_size)
1943 /* noff == va.va_size. There is an implicit hole at the end of file. */
1944 if (cmd == FIOSEEKDATA)