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 * Copyright (c) 2012 Konstantin Belousov <kib@FreeBSD.org>
11 * Copyright (c) 2013 The FreeBSD Foundation
13 * Portions of this software were developed by Konstantin Belousov
14 * under sponsorship from the FreeBSD Foundation.
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
19 * 1. Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution.
24 * 4. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 * @(#)vfs_vnops.c 8.2 (Berkeley) 1/21/94
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
46 #include <sys/param.h>
47 #include <sys/systm.h>
49 #include <sys/fcntl.h>
55 #include <sys/limits.h>
57 #include <sys/mount.h>
58 #include <sys/mutex.h>
59 #include <sys/namei.h>
60 #include <sys/vnode.h>
63 #include <sys/filio.h>
64 #include <sys/resourcevar.h>
65 #include <sys/rwlock.h>
67 #include <sys/sysctl.h>
68 #include <sys/ttycom.h>
70 #include <sys/syslog.h>
71 #include <sys/unistd.h>
73 #include <security/audit/audit.h>
74 #include <security/mac/mac_framework.h>
77 #include <vm/vm_extern.h>
79 #include <vm/vm_map.h>
80 #include <vm/vm_object.h>
81 #include <vm/vm_page.h>
83 static fo_rdwr_t vn_read;
84 static fo_rdwr_t vn_write;
85 static fo_rdwr_t vn_io_fault;
86 static fo_truncate_t vn_truncate;
87 static fo_ioctl_t vn_ioctl;
88 static fo_poll_t vn_poll;
89 static fo_kqfilter_t vn_kqfilter;
90 static fo_stat_t vn_statfile;
91 static fo_close_t vn_closefile;
93 struct fileops vnops = {
94 .fo_read = vn_io_fault,
95 .fo_write = vn_io_fault,
96 .fo_truncate = vn_truncate,
99 .fo_kqfilter = vn_kqfilter,
100 .fo_stat = vn_statfile,
101 .fo_close = vn_closefile,
102 .fo_chmod = vn_chmod,
103 .fo_chown = vn_chown,
104 .fo_sendfile = vn_sendfile,
106 .fo_flags = DFLAG_PASSABLE | DFLAG_SEEKABLE
110 vn_open(ndp, flagp, cmode, fp)
111 struct nameidata *ndp;
115 struct thread *td = ndp->ni_cnd.cn_thread;
117 return (vn_open_cred(ndp, flagp, cmode, 0, td->td_ucred, fp));
121 * Common code for vnode open operations via a name lookup.
122 * Lookup the vnode and invoke VOP_CREATE if needed.
123 * Check permissions, and call the VOP_OPEN or VOP_CREATE routine.
125 * Note that this does NOT free nameidata for the successful case,
126 * due to the NDINIT being done elsewhere.
129 vn_open_cred(struct nameidata *ndp, int *flagp, int cmode, u_int vn_open_flags,
130 struct ucred *cred, struct file *fp)
134 struct thread *td = ndp->ni_cnd.cn_thread;
136 struct vattr *vap = &vat;
141 if (fmode & O_CREAT) {
142 ndp->ni_cnd.cn_nameiop = CREATE;
143 ndp->ni_cnd.cn_flags = ISOPEN | LOCKPARENT | LOCKLEAF;
144 if ((fmode & O_EXCL) == 0 && (fmode & O_NOFOLLOW) == 0)
145 ndp->ni_cnd.cn_flags |= FOLLOW;
146 if (!(vn_open_flags & VN_OPEN_NOAUDIT))
147 ndp->ni_cnd.cn_flags |= AUDITVNODE1;
148 if (vn_open_flags & VN_OPEN_NOCAPCHECK)
149 ndp->ni_cnd.cn_flags |= NOCAPCHECK;
151 if ((error = namei(ndp)) != 0)
153 if (ndp->ni_vp == NULL) {
156 vap->va_mode = cmode;
158 vap->va_vaflags |= VA_EXCLUSIVE;
159 if (vn_start_write(ndp->ni_dvp, &mp, V_NOWAIT) != 0) {
160 NDFREE(ndp, NDF_ONLY_PNBUF);
162 if ((error = vn_start_write(NULL, &mp,
163 V_XSLEEP | PCATCH)) != 0)
168 error = mac_vnode_check_create(cred, ndp->ni_dvp,
172 error = VOP_CREATE(ndp->ni_dvp, &ndp->ni_vp,
175 vn_finished_write(mp);
177 NDFREE(ndp, NDF_ONLY_PNBUF);
183 if (ndp->ni_dvp == ndp->ni_vp)
189 if (fmode & O_EXCL) {
196 ndp->ni_cnd.cn_nameiop = LOOKUP;
197 ndp->ni_cnd.cn_flags = ISOPEN |
198 ((fmode & O_NOFOLLOW) ? NOFOLLOW : FOLLOW) | LOCKLEAF;
199 if (!(fmode & FWRITE))
200 ndp->ni_cnd.cn_flags |= LOCKSHARED;
201 if (!(vn_open_flags & VN_OPEN_NOAUDIT))
202 ndp->ni_cnd.cn_flags |= AUDITVNODE1;
203 if (vn_open_flags & VN_OPEN_NOCAPCHECK)
204 ndp->ni_cnd.cn_flags |= NOCAPCHECK;
205 if ((error = namei(ndp)) != 0)
209 error = vn_open_vnode(vp, fmode, cred, td, fp);
215 NDFREE(ndp, NDF_ONLY_PNBUF);
223 * Common code for vnode open operations once a vnode is located.
224 * Check permissions, and call the VOP_OPEN routine.
227 vn_open_vnode(struct vnode *vp, int fmode, struct ucred *cred,
228 struct thread *td, struct file *fp)
233 int error, have_flock, lock_flags, type;
235 if (vp->v_type == VLNK)
237 if (vp->v_type == VSOCK)
239 if (vp->v_type != VDIR && fmode & O_DIRECTORY)
242 if (fmode & (FWRITE | O_TRUNC)) {
243 if (vp->v_type == VDIR)
251 if ((fmode & O_APPEND) && (fmode & FWRITE))
254 error = mac_vnode_check_open(cred, vp, accmode);
258 if ((fmode & O_CREAT) == 0) {
259 if (accmode & VWRITE) {
260 error = vn_writechk(vp);
265 error = VOP_ACCESS(vp, accmode, cred, td);
270 if ((error = VOP_OPEN(vp, fmode, cred, td, fp)) != 0)
273 if (fmode & (O_EXLOCK | O_SHLOCK)) {
274 KASSERT(fp != NULL, ("open with flock requires fp"));
275 lock_flags = VOP_ISLOCKED(vp);
277 lf.l_whence = SEEK_SET;
280 if (fmode & O_EXLOCK)
285 if ((fmode & FNONBLOCK) == 0)
287 error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type);
288 have_flock = (error == 0);
289 vn_lock(vp, lock_flags | LK_RETRY);
290 if (error == 0 && vp->v_iflag & VI_DOOMED)
293 * Another thread might have used this vnode as an
294 * executable while the vnode lock was dropped.
295 * Ensure the vnode is still able to be opened for
296 * writing after the lock has been obtained.
298 if (error == 0 && accmode & VWRITE)
299 error = vn_writechk(vp);
303 lf.l_whence = SEEK_SET;
307 (void) VOP_ADVLOCK(vp, fp, F_UNLCK, &lf,
310 vn_start_write(vp, &mp, V_WAIT);
311 vn_lock(vp, lock_flags | LK_RETRY);
312 (void)VOP_CLOSE(vp, fmode, cred, td);
313 vn_finished_write(mp);
316 fp->f_flag |= FHASLOCK;
318 if (fmode & FWRITE) {
319 VOP_ADD_WRITECOUNT(vp, 1);
320 CTR3(KTR_VFS, "%s: vp %p v_writecount increased to %d",
321 __func__, vp, vp->v_writecount);
323 ASSERT_VOP_LOCKED(vp, "vn_open_vnode");
328 * Check for write permissions on the specified vnode.
329 * Prototype text segments cannot be written.
333 register struct vnode *vp;
336 ASSERT_VOP_LOCKED(vp, "vn_writechk");
338 * If there's shared text associated with
339 * the vnode, try to free it up once. If
340 * we fail, we can't allow writing.
352 vn_close(vp, flags, file_cred, td)
353 register struct vnode *vp;
355 struct ucred *file_cred;
359 int error, lock_flags;
361 if (!(flags & FWRITE) && vp->v_mount != NULL &&
362 vp->v_mount->mnt_kern_flag & MNTK_EXTENDED_SHARED)
363 lock_flags = LK_SHARED;
365 lock_flags = LK_EXCLUSIVE;
367 vn_start_write(vp, &mp, V_WAIT);
368 vn_lock(vp, lock_flags | LK_RETRY);
369 if (flags & FWRITE) {
370 VNASSERT(vp->v_writecount > 0, vp,
371 ("vn_close: negative writecount"));
372 VOP_ADD_WRITECOUNT(vp, -1);
373 CTR3(KTR_VFS, "%s: vp %p v_writecount decreased to %d",
374 __func__, vp, vp->v_writecount);
376 error = VOP_CLOSE(vp, flags, file_cred, td);
378 vn_finished_write(mp);
383 * Heuristic to detect sequential operation.
386 sequential_heuristic(struct uio *uio, struct file *fp)
389 if (atomic_load_acq_int(&(fp->f_flag)) & FRDAHEAD)
390 return (fp->f_seqcount << IO_SEQSHIFT);
393 * Offset 0 is handled specially. open() sets f_seqcount to 1 so
394 * that the first I/O is normally considered to be slightly
395 * sequential. Seeking to offset 0 doesn't change sequentiality
396 * unless previous seeks have reduced f_seqcount to 0, in which
397 * case offset 0 is not special.
399 if ((uio->uio_offset == 0 && fp->f_seqcount > 0) ||
400 uio->uio_offset == fp->f_nextoff) {
402 * f_seqcount is in units of fixed-size blocks so that it
403 * depends mainly on the amount of sequential I/O and not
404 * much on the number of sequential I/O's. The fixed size
405 * of 16384 is hard-coded here since it is (not quite) just
406 * a magic size that works well here. This size is more
407 * closely related to the best I/O size for real disks than
408 * to any block size used by software.
410 fp->f_seqcount += howmany(uio->uio_resid, 16384);
411 if (fp->f_seqcount > IO_SEQMAX)
412 fp->f_seqcount = IO_SEQMAX;
413 return (fp->f_seqcount << IO_SEQSHIFT);
416 /* Not sequential. Quickly draw-down sequentiality. */
417 if (fp->f_seqcount > 1)
425 * Package up an I/O request on a vnode into a uio and do it.
428 vn_rdwr(enum uio_rw rw, struct vnode *vp, void *base, int len, off_t offset,
429 enum uio_seg segflg, int ioflg, struct ucred *active_cred,
430 struct ucred *file_cred, ssize_t *aresid, struct thread *td)
437 int error, lock_flags;
439 auio.uio_iov = &aiov;
441 aiov.iov_base = base;
443 auio.uio_resid = len;
444 auio.uio_offset = offset;
445 auio.uio_segflg = segflg;
450 if ((ioflg & IO_NODELOCKED) == 0) {
451 if (rw == UIO_READ) {
452 rl_cookie = vn_rangelock_rlock(vp, offset,
455 rl_cookie = vn_rangelock_wlock(vp, offset,
459 if (rw == UIO_WRITE) {
460 if (vp->v_type != VCHR &&
461 (error = vn_start_write(vp, &mp, V_WAIT | PCATCH))
464 if (MNT_SHARED_WRITES(mp) ||
465 ((mp == NULL) && MNT_SHARED_WRITES(vp->v_mount)))
466 lock_flags = LK_SHARED;
468 lock_flags = LK_EXCLUSIVE;
470 lock_flags = LK_SHARED;
471 vn_lock(vp, lock_flags | LK_RETRY);
475 ASSERT_VOP_LOCKED(vp, "IO_NODELOCKED with no vp lock held");
477 if ((ioflg & IO_NOMACCHECK) == 0) {
479 error = mac_vnode_check_read(active_cred, file_cred,
482 error = mac_vnode_check_write(active_cred, file_cred,
487 if (file_cred != NULL)
492 error = VOP_READ(vp, &auio, ioflg, cred);
494 error = VOP_WRITE(vp, &auio, ioflg, cred);
497 *aresid = auio.uio_resid;
499 if (auio.uio_resid && error == 0)
501 if ((ioflg & IO_NODELOCKED) == 0) {
504 vn_finished_write(mp);
507 if (rl_cookie != NULL)
508 vn_rangelock_unlock(vp, rl_cookie);
513 * Package up an I/O request on a vnode into a uio and do it. The I/O
514 * request is split up into smaller chunks and we try to avoid saturating
515 * the buffer cache while potentially holding a vnode locked, so we
516 * check bwillwrite() before calling vn_rdwr(). We also call kern_yield()
517 * to give other processes a chance to lock the vnode (either other processes
518 * core'ing the same binary, or unrelated processes scanning the directory).
521 vn_rdwr_inchunks(rw, vp, base, len, offset, segflg, ioflg, active_cred,
522 file_cred, aresid, td)
530 struct ucred *active_cred;
531 struct ucred *file_cred;
542 * Force `offset' to a multiple of MAXBSIZE except possibly
543 * for the first chunk, so that filesystems only need to
544 * write full blocks except possibly for the first and last
547 chunk = MAXBSIZE - (uoff_t)offset % MAXBSIZE;
551 if (rw != UIO_READ && vp->v_type == VREG)
554 error = vn_rdwr(rw, vp, base, chunk, offset, segflg,
555 ioflg, active_cred, file_cred, &iaresid, td);
556 len -= chunk; /* aresid calc already includes length */
560 base = (char *)base + chunk;
561 kern_yield(PRI_USER);
564 *aresid = len + iaresid;
569 foffset_lock(struct file *fp, int flags)
574 KASSERT((flags & FOF_OFFSET) == 0, ("FOF_OFFSET passed"));
576 #if OFF_MAX <= LONG_MAX
578 * Caller only wants the current f_offset value. Assume that
579 * the long and shorter integer types reads are atomic.
581 if ((flags & FOF_NOLOCK) != 0)
582 return (fp->f_offset);
586 * According to McKusick the vn lock was protecting f_offset here.
587 * It is now protected by the FOFFSET_LOCKED flag.
589 mtxp = mtx_pool_find(mtxpool_sleep, fp);
591 if ((flags & FOF_NOLOCK) == 0) {
592 while (fp->f_vnread_flags & FOFFSET_LOCKED) {
593 fp->f_vnread_flags |= FOFFSET_LOCK_WAITING;
594 msleep(&fp->f_vnread_flags, mtxp, PUSER -1,
597 fp->f_vnread_flags |= FOFFSET_LOCKED;
605 foffset_unlock(struct file *fp, off_t val, int flags)
609 KASSERT((flags & FOF_OFFSET) == 0, ("FOF_OFFSET passed"));
611 #if OFF_MAX <= LONG_MAX
612 if ((flags & FOF_NOLOCK) != 0) {
613 if ((flags & FOF_NOUPDATE) == 0)
615 if ((flags & FOF_NEXTOFF) != 0)
621 mtxp = mtx_pool_find(mtxpool_sleep, fp);
623 if ((flags & FOF_NOUPDATE) == 0)
625 if ((flags & FOF_NEXTOFF) != 0)
627 if ((flags & FOF_NOLOCK) == 0) {
628 KASSERT((fp->f_vnread_flags & FOFFSET_LOCKED) != 0,
629 ("Lost FOFFSET_LOCKED"));
630 if (fp->f_vnread_flags & FOFFSET_LOCK_WAITING)
631 wakeup(&fp->f_vnread_flags);
632 fp->f_vnread_flags = 0;
638 foffset_lock_uio(struct file *fp, struct uio *uio, int flags)
641 if ((flags & FOF_OFFSET) == 0)
642 uio->uio_offset = foffset_lock(fp, flags);
646 foffset_unlock_uio(struct file *fp, struct uio *uio, int flags)
649 if ((flags & FOF_OFFSET) == 0)
650 foffset_unlock(fp, uio->uio_offset, flags);
654 get_advice(struct file *fp, struct uio *uio)
659 ret = POSIX_FADV_NORMAL;
660 if (fp->f_advice == NULL)
663 mtxp = mtx_pool_find(mtxpool_sleep, fp);
665 if (uio->uio_offset >= fp->f_advice->fa_start &&
666 uio->uio_offset + uio->uio_resid <= fp->f_advice->fa_end)
667 ret = fp->f_advice->fa_advice;
673 * File table vnode read routine.
676 vn_read(fp, uio, active_cred, flags, td)
679 struct ucred *active_cred;
687 off_t offset, start, end;
689 KASSERT(uio->uio_td == td, ("uio_td %p is not td %p",
691 KASSERT(flags & FOF_OFFSET, ("No FOF_OFFSET"));
694 if (fp->f_flag & FNONBLOCK)
696 if (fp->f_flag & O_DIRECT)
698 advice = get_advice(fp, uio);
699 vn_lock(vp, LK_SHARED | LK_RETRY);
702 case POSIX_FADV_NORMAL:
703 case POSIX_FADV_SEQUENTIAL:
704 case POSIX_FADV_NOREUSE:
705 ioflag |= sequential_heuristic(uio, fp);
707 case POSIX_FADV_RANDOM:
708 /* Disable read-ahead for random I/O. */
711 offset = uio->uio_offset;
714 error = mac_vnode_check_read(active_cred, fp->f_cred, vp);
717 error = VOP_READ(vp, uio, ioflag, fp->f_cred);
718 fp->f_nextoff = uio->uio_offset;
720 if (error == 0 && advice == POSIX_FADV_NOREUSE &&
721 offset != uio->uio_offset) {
723 * Use POSIX_FADV_DONTNEED to flush clean pages and
724 * buffers for the backing file after a
725 * POSIX_FADV_NOREUSE read(2). To optimize the common
726 * case of using POSIX_FADV_NOREUSE with sequential
727 * access, track the previous implicit DONTNEED
728 * request and grow this request to include the
729 * current read(2) in addition to the previous
730 * DONTNEED. With purely sequential access this will
731 * cause the DONTNEED requests to continously grow to
732 * cover all of the previously read regions of the
733 * file. This allows filesystem blocks that are
734 * accessed by multiple calls to read(2) to be flushed
735 * once the last read(2) finishes.
738 end = uio->uio_offset - 1;
739 mtxp = mtx_pool_find(mtxpool_sleep, fp);
741 if (fp->f_advice != NULL &&
742 fp->f_advice->fa_advice == POSIX_FADV_NOREUSE) {
743 if (start != 0 && fp->f_advice->fa_prevend + 1 == start)
744 start = fp->f_advice->fa_prevstart;
745 else if (fp->f_advice->fa_prevstart != 0 &&
746 fp->f_advice->fa_prevstart == end + 1)
747 end = fp->f_advice->fa_prevend;
748 fp->f_advice->fa_prevstart = start;
749 fp->f_advice->fa_prevend = end;
752 error = VOP_ADVISE(vp, start, end, POSIX_FADV_DONTNEED);
758 * File table vnode write routine.
761 vn_write(fp, uio, active_cred, flags, td)
764 struct ucred *active_cred;
771 int error, ioflag, lock_flags;
773 off_t offset, start, end;
775 KASSERT(uio->uio_td == td, ("uio_td %p is not td %p",
777 KASSERT(flags & FOF_OFFSET, ("No FOF_OFFSET"));
779 if (vp->v_type == VREG)
782 if (vp->v_type == VREG && (fp->f_flag & O_APPEND))
784 if (fp->f_flag & FNONBLOCK)
786 if (fp->f_flag & O_DIRECT)
788 if ((fp->f_flag & O_FSYNC) ||
789 (vp->v_mount && (vp->v_mount->mnt_flag & MNT_SYNCHRONOUS)))
792 if (vp->v_type != VCHR &&
793 (error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
796 advice = get_advice(fp, uio);
798 if (MNT_SHARED_WRITES(mp) ||
799 (mp == NULL && MNT_SHARED_WRITES(vp->v_mount))) {
800 lock_flags = LK_SHARED;
802 lock_flags = LK_EXCLUSIVE;
805 vn_lock(vp, lock_flags | LK_RETRY);
807 case POSIX_FADV_NORMAL:
808 case POSIX_FADV_SEQUENTIAL:
809 case POSIX_FADV_NOREUSE:
810 ioflag |= sequential_heuristic(uio, fp);
812 case POSIX_FADV_RANDOM:
813 /* XXX: Is this correct? */
816 offset = uio->uio_offset;
819 error = mac_vnode_check_write(active_cred, fp->f_cred, vp);
822 error = VOP_WRITE(vp, uio, ioflag, fp->f_cred);
823 fp->f_nextoff = uio->uio_offset;
825 if (vp->v_type != VCHR)
826 vn_finished_write(mp);
827 if (error == 0 && advice == POSIX_FADV_NOREUSE &&
828 offset != uio->uio_offset) {
830 * Use POSIX_FADV_DONTNEED to flush clean pages and
831 * buffers for the backing file after a
832 * POSIX_FADV_NOREUSE write(2). To optimize the
833 * common case of using POSIX_FADV_NOREUSE with
834 * sequential access, track the previous implicit
835 * DONTNEED request and grow this request to include
836 * the current write(2) in addition to the previous
837 * DONTNEED. With purely sequential access this will
838 * cause the DONTNEED requests to continously grow to
839 * cover all of the previously written regions of the
842 * Note that the blocks just written are almost
843 * certainly still dirty, so this only works when
844 * VOP_ADVISE() calls from subsequent writes push out
845 * the data written by this write(2) once the backing
846 * buffers are clean. However, as compared to forcing
847 * IO_DIRECT, this gives much saner behavior. Write
848 * clustering is still allowed, and clean pages are
849 * merely moved to the cache page queue rather than
850 * outright thrown away. This means a subsequent
851 * read(2) can still avoid hitting the disk if the
852 * pages have not been reclaimed.
854 * This does make POSIX_FADV_NOREUSE largely useless
855 * with non-sequential access. However, sequential
856 * access is the more common use case and the flag is
860 end = uio->uio_offset - 1;
861 mtxp = mtx_pool_find(mtxpool_sleep, fp);
863 if (fp->f_advice != NULL &&
864 fp->f_advice->fa_advice == POSIX_FADV_NOREUSE) {
865 if (start != 0 && fp->f_advice->fa_prevend + 1 == start)
866 start = fp->f_advice->fa_prevstart;
867 else if (fp->f_advice->fa_prevstart != 0 &&
868 fp->f_advice->fa_prevstart == end + 1)
869 end = fp->f_advice->fa_prevend;
870 fp->f_advice->fa_prevstart = start;
871 fp->f_advice->fa_prevend = end;
874 error = VOP_ADVISE(vp, start, end, POSIX_FADV_DONTNEED);
881 static const int io_hold_cnt = 16;
882 static int vn_io_fault_enable = 1;
883 SYSCTL_INT(_debug, OID_AUTO, vn_io_fault_enable, CTLFLAG_RW,
884 &vn_io_fault_enable, 0, "Enable vn_io_fault lock avoidance");
885 static u_long vn_io_faults_cnt;
886 SYSCTL_ULONG(_debug, OID_AUTO, vn_io_faults, CTLFLAG_RD,
887 &vn_io_faults_cnt, 0, "Count of vn_io_fault lock avoidance triggers");
890 * The vn_io_fault() is a wrapper around vn_read() and vn_write() to
891 * prevent the following deadlock:
893 * Assume that the thread A reads from the vnode vp1 into userspace
894 * buffer buf1 backed by the pages of vnode vp2. If a page in buf1 is
895 * currently not resident, then system ends up with the call chain
896 * vn_read() -> VOP_READ(vp1) -> uiomove() -> [Page Fault] ->
897 * vm_fault(buf1) -> vnode_pager_getpages(vp2) -> VOP_GETPAGES(vp2)
898 * which establishes lock order vp1->vn_lock, then vp2->vn_lock.
899 * If, at the same time, thread B reads from vnode vp2 into buffer buf2
900 * backed by the pages of vnode vp1, and some page in buf2 is not
901 * resident, we get a reversed order vp2->vn_lock, then vp1->vn_lock.
903 * To prevent the lock order reversal and deadlock, vn_io_fault() does
904 * not allow page faults to happen during VOP_READ() or VOP_WRITE().
905 * Instead, it first tries to do the whole range i/o with pagefaults
906 * disabled. If all pages in the i/o buffer are resident and mapped,
907 * VOP will succeed (ignoring the genuine filesystem errors).
908 * Otherwise, we get back EFAULT, and vn_io_fault() falls back to do
909 * i/o in chunks, with all pages in the chunk prefaulted and held
910 * using vm_fault_quick_hold_pages().
912 * Filesystems using this deadlock avoidance scheme should use the
913 * array of the held pages from uio, saved in the curthread->td_ma,
914 * instead of doing uiomove(). A helper function
915 * vn_io_fault_uiomove() converts uiomove request into
916 * uiomove_fromphys() over td_ma array.
918 * Since vnode locks do not cover the whole i/o anymore, rangelocks
919 * make the current i/o request atomic with respect to other i/os and
923 vn_io_fault(struct file *fp, struct uio *uio, struct ucred *active_cred,
924 int flags, struct thread *td)
926 vm_page_t ma[io_hold_cnt + 2];
927 struct uio *uio_clone, short_uio;
928 struct iovec short_iovec[1];
933 vm_page_t *prev_td_ma;
934 int cnt, error, save, saveheld, prev_td_ma_cnt;
935 vm_offset_t addr, end;
940 if (uio->uio_rw == UIO_READ)
945 foffset_lock_uio(fp, uio, flags);
947 if (uio->uio_segflg != UIO_USERSPACE || vp->v_type != VREG ||
948 ((mp = vp->v_mount) != NULL &&
949 (mp->mnt_kern_flag & MNTK_NO_IOPF) == 0) ||
950 !vn_io_fault_enable) {
951 error = doio(fp, uio, active_cred, flags | FOF_OFFSET, td);
956 * The UFS follows IO_UNIT directive and replays back both
957 * uio_offset and uio_resid if an error is encountered during the
958 * operation. But, since the iovec may be already advanced,
959 * uio is still in an inconsistent state.
961 * Cache a copy of the original uio, which is advanced to the redo
962 * point using UIO_NOCOPY below.
964 uio_clone = cloneuio(uio);
965 resid = uio->uio_resid;
967 short_uio.uio_segflg = UIO_USERSPACE;
968 short_uio.uio_rw = uio->uio_rw;
969 short_uio.uio_td = uio->uio_td;
971 if (uio->uio_rw == UIO_READ) {
972 prot = VM_PROT_WRITE;
973 rl_cookie = vn_rangelock_rlock(vp, uio->uio_offset,
974 uio->uio_offset + uio->uio_resid);
977 if ((fp->f_flag & O_APPEND) != 0 || (flags & FOF_OFFSET) == 0)
978 /* For appenders, punt and lock the whole range. */
979 rl_cookie = vn_rangelock_wlock(vp, 0, OFF_MAX);
981 rl_cookie = vn_rangelock_wlock(vp, uio->uio_offset,
982 uio->uio_offset + uio->uio_resid);
985 save = vm_fault_disable_pagefaults();
986 error = doio(fp, uio, active_cred, flags | FOF_OFFSET, td);
990 atomic_add_long(&vn_io_faults_cnt, 1);
991 uio_clone->uio_segflg = UIO_NOCOPY;
992 uiomove(NULL, resid - uio->uio_resid, uio_clone);
993 uio_clone->uio_segflg = uio->uio_segflg;
995 saveheld = curthread_pflags_set(TDP_UIOHELD);
996 prev_td_ma = td->td_ma;
997 prev_td_ma_cnt = td->td_ma_cnt;
999 while (uio_clone->uio_resid != 0) {
1000 len = uio_clone->uio_iov->iov_len;
1002 KASSERT(uio_clone->uio_iovcnt >= 1,
1003 ("iovcnt underflow"));
1004 uio_clone->uio_iov++;
1005 uio_clone->uio_iovcnt--;
1009 addr = (vm_offset_t)uio_clone->uio_iov->iov_base;
1010 end = round_page(addr + len);
1011 cnt = howmany(end - trunc_page(addr), PAGE_SIZE);
1013 * A perfectly misaligned address and length could cause
1014 * both the start and the end of the chunk to use partial
1015 * page. +2 accounts for such a situation.
1017 if (cnt > io_hold_cnt + 2) {
1018 len = io_hold_cnt * PAGE_SIZE;
1019 KASSERT(howmany(round_page(addr + len) -
1020 trunc_page(addr), PAGE_SIZE) <= io_hold_cnt + 2,
1023 cnt = vm_fault_quick_hold_pages(&td->td_proc->p_vmspace->vm_map,
1024 addr, len, prot, ma, io_hold_cnt + 2);
1029 short_uio.uio_iov = &short_iovec[0];
1030 short_iovec[0].iov_base = (void *)addr;
1031 short_uio.uio_iovcnt = 1;
1032 short_uio.uio_resid = short_iovec[0].iov_len = len;
1033 short_uio.uio_offset = uio_clone->uio_offset;
1035 td->td_ma_cnt = cnt;
1037 error = doio(fp, &short_uio, active_cred, flags | FOF_OFFSET,
1039 vm_page_unhold_pages(ma, cnt);
1040 adv = len - short_uio.uio_resid;
1042 uio_clone->uio_iov->iov_base =
1043 (char *)uio_clone->uio_iov->iov_base + adv;
1044 uio_clone->uio_iov->iov_len -= adv;
1045 uio_clone->uio_resid -= adv;
1046 uio_clone->uio_offset += adv;
1048 uio->uio_resid -= adv;
1049 uio->uio_offset += adv;
1051 if (error != 0 || adv == 0)
1054 td->td_ma = prev_td_ma;
1055 td->td_ma_cnt = prev_td_ma_cnt;
1056 curthread_pflags_restore(saveheld);
1058 vm_fault_enable_pagefaults(save);
1059 vn_rangelock_unlock(vp, rl_cookie);
1060 free(uio_clone, M_IOV);
1062 foffset_unlock_uio(fp, uio, flags);
1067 * Helper function to perform the requested uiomove operation using
1068 * the held pages for io->uio_iov[0].iov_base buffer instead of
1069 * copyin/copyout. Access to the pages with uiomove_fromphys()
1070 * instead of iov_base prevents page faults that could occur due to
1071 * pmap_collect() invalidating the mapping created by
1072 * vm_fault_quick_hold_pages(), or pageout daemon, page laundry or
1073 * object cleanup revoking the write access from page mappings.
1075 * Filesystems specified MNTK_NO_IOPF shall use vn_io_fault_uiomove()
1076 * instead of plain uiomove().
1079 vn_io_fault_uiomove(char *data, int xfersize, struct uio *uio)
1081 struct uio transp_uio;
1082 struct iovec transp_iov[1];
1088 if ((td->td_pflags & TDP_UIOHELD) == 0 ||
1089 uio->uio_segflg != UIO_USERSPACE)
1090 return (uiomove(data, xfersize, uio));
1092 KASSERT(uio->uio_iovcnt == 1, ("uio_iovcnt %d", uio->uio_iovcnt));
1093 transp_iov[0].iov_base = data;
1094 transp_uio.uio_iov = &transp_iov[0];
1095 transp_uio.uio_iovcnt = 1;
1096 if (xfersize > uio->uio_resid)
1097 xfersize = uio->uio_resid;
1098 transp_uio.uio_resid = transp_iov[0].iov_len = xfersize;
1099 transp_uio.uio_offset = 0;
1100 transp_uio.uio_segflg = UIO_SYSSPACE;
1102 * Since transp_iov points to data, and td_ma page array
1103 * corresponds to original uio->uio_iov, we need to invert the
1104 * direction of the i/o operation as passed to
1105 * uiomove_fromphys().
1107 switch (uio->uio_rw) {
1109 transp_uio.uio_rw = UIO_READ;
1112 transp_uio.uio_rw = UIO_WRITE;
1115 transp_uio.uio_td = uio->uio_td;
1116 error = uiomove_fromphys(td->td_ma,
1117 ((vm_offset_t)uio->uio_iov->iov_base) & PAGE_MASK,
1118 xfersize, &transp_uio);
1119 adv = xfersize - transp_uio.uio_resid;
1121 (((vm_offset_t)uio->uio_iov->iov_base + adv) >> PAGE_SHIFT) -
1122 (((vm_offset_t)uio->uio_iov->iov_base) >> PAGE_SHIFT);
1124 KASSERT(td->td_ma_cnt >= pgadv, ("consumed pages %d %d", td->td_ma_cnt,
1126 td->td_ma_cnt -= pgadv;
1127 uio->uio_iov->iov_base = (char *)uio->uio_iov->iov_base + adv;
1128 uio->uio_iov->iov_len -= adv;
1129 uio->uio_resid -= adv;
1130 uio->uio_offset += adv;
1135 vn_io_fault_pgmove(vm_page_t ma[], vm_offset_t offset, int xfersize,
1139 vm_offset_t iov_base;
1143 if ((td->td_pflags & TDP_UIOHELD) == 0 ||
1144 uio->uio_segflg != UIO_USERSPACE)
1145 return (uiomove_fromphys(ma, offset, xfersize, uio));
1147 KASSERT(uio->uio_iovcnt == 1, ("uio_iovcnt %d", uio->uio_iovcnt));
1148 cnt = xfersize > uio->uio_resid ? uio->uio_resid : xfersize;
1149 iov_base = (vm_offset_t)uio->uio_iov->iov_base;
1150 switch (uio->uio_rw) {
1152 pmap_copy_pages(td->td_ma, iov_base & PAGE_MASK, ma,
1156 pmap_copy_pages(ma, offset, td->td_ma, iov_base & PAGE_MASK,
1160 pgadv = ((iov_base + cnt) >> PAGE_SHIFT) - (iov_base >> PAGE_SHIFT);
1162 KASSERT(td->td_ma_cnt >= pgadv, ("consumed pages %d %d", td->td_ma_cnt,
1164 td->td_ma_cnt -= pgadv;
1165 uio->uio_iov->iov_base = (char *)(iov_base + cnt);
1166 uio->uio_iov->iov_len -= cnt;
1167 uio->uio_resid -= cnt;
1168 uio->uio_offset += cnt;
1174 * File table truncate routine.
1177 vn_truncate(struct file *fp, off_t length, struct ucred *active_cred,
1189 * Lock the whole range for truncation. Otherwise split i/o
1190 * might happen partly before and partly after the truncation.
1192 rl_cookie = vn_rangelock_wlock(vp, 0, OFF_MAX);
1193 error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
1196 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1197 if (vp->v_type == VDIR) {
1202 error = mac_vnode_check_write(active_cred, fp->f_cred, vp);
1206 error = vn_writechk(vp);
1209 vattr.va_size = length;
1210 error = VOP_SETATTR(vp, &vattr, fp->f_cred);
1214 vn_finished_write(mp);
1216 vn_rangelock_unlock(vp, rl_cookie);
1221 * File table vnode stat routine.
1224 vn_statfile(fp, sb, active_cred, td)
1227 struct ucred *active_cred;
1230 struct vnode *vp = fp->f_vnode;
1233 vn_lock(vp, LK_SHARED | LK_RETRY);
1234 error = vn_stat(vp, sb, active_cred, fp->f_cred, td);
1241 * Stat a vnode; implementation for the stat syscall
1244 vn_stat(vp, sb, active_cred, file_cred, td)
1246 register struct stat *sb;
1247 struct ucred *active_cred;
1248 struct ucred *file_cred;
1252 register struct vattr *vap;
1257 error = mac_vnode_check_stat(active_cred, file_cred, vp);
1265 * Initialize defaults for new and unusual fields, so that file
1266 * systems which don't support these fields don't need to know
1269 vap->va_birthtime.tv_sec = -1;
1270 vap->va_birthtime.tv_nsec = 0;
1271 vap->va_fsid = VNOVAL;
1272 vap->va_rdev = NODEV;
1274 error = VOP_GETATTR(vp, vap, active_cred);
1279 * Zero the spare stat fields
1281 bzero(sb, sizeof *sb);
1284 * Copy from vattr table
1286 if (vap->va_fsid != VNOVAL)
1287 sb->st_dev = vap->va_fsid;
1289 sb->st_dev = vp->v_mount->mnt_stat.f_fsid.val[0];
1290 sb->st_ino = vap->va_fileid;
1291 mode = vap->va_mode;
1292 switch (vap->va_type) {
1318 sb->st_nlink = vap->va_nlink;
1319 sb->st_uid = vap->va_uid;
1320 sb->st_gid = vap->va_gid;
1321 sb->st_rdev = vap->va_rdev;
1322 if (vap->va_size > OFF_MAX)
1324 sb->st_size = vap->va_size;
1325 sb->st_atim = vap->va_atime;
1326 sb->st_mtim = vap->va_mtime;
1327 sb->st_ctim = vap->va_ctime;
1328 sb->st_birthtim = vap->va_birthtime;
1331 * According to www.opengroup.org, the meaning of st_blksize is
1332 * "a filesystem-specific preferred I/O block size for this
1333 * object. In some filesystem types, this may vary from file
1335 * Use miminum/default of PAGE_SIZE (e.g. for VCHR).
1338 sb->st_blksize = max(PAGE_SIZE, vap->va_blocksize);
1340 sb->st_flags = vap->va_flags;
1341 if (priv_check(td, PRIV_VFS_GENERATION))
1344 sb->st_gen = vap->va_gen;
1346 sb->st_blocks = vap->va_bytes / S_BLKSIZE;
1351 * File table vnode ioctl routine.
1354 vn_ioctl(fp, com, data, active_cred, td)
1358 struct ucred *active_cred;
1366 switch (vp->v_type) {
1371 vn_lock(vp, LK_SHARED | LK_RETRY);
1372 error = VOP_GETATTR(vp, &vattr, active_cred);
1375 *(int *)data = vattr.va_size - fp->f_offset;
1381 return (VOP_IOCTL(vp, com, data, fp->f_flag,
1390 * File table vnode poll routine.
1393 vn_poll(fp, events, active_cred, td)
1396 struct ucred *active_cred;
1404 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1405 error = mac_vnode_check_poll(active_cred, fp->f_cred, vp);
1410 error = VOP_POLL(vp, events, fp->f_cred, td);
1415 * Acquire the requested lock and then check for validity. LK_RETRY
1416 * permits vn_lock to return doomed vnodes.
1419 _vn_lock(struct vnode *vp, int flags, char *file, int line)
1423 VNASSERT((flags & LK_TYPE_MASK) != 0, vp,
1424 ("vn_lock called with no locktype."));
1426 #ifdef DEBUG_VFS_LOCKS
1427 KASSERT(vp->v_holdcnt != 0,
1428 ("vn_lock %p: zero hold count", vp));
1430 error = VOP_LOCK1(vp, flags, file, line);
1431 flags &= ~LK_INTERLOCK; /* Interlock is always dropped. */
1432 KASSERT((flags & LK_RETRY) == 0 || error == 0,
1433 ("LK_RETRY set with incompatible flags (0x%x) or an error occured (%d)",
1436 * Callers specify LK_RETRY if they wish to get dead vnodes.
1437 * If RETRY is not set, we return ENOENT instead.
1439 if (error == 0 && vp->v_iflag & VI_DOOMED &&
1440 (flags & LK_RETRY) == 0) {
1445 } while (flags & LK_RETRY && error != 0);
1450 * File table vnode close routine.
1453 vn_closefile(fp, td)
1462 fp->f_ops = &badfileops;
1464 if (fp->f_type == DTYPE_VNODE && fp->f_flag & FHASLOCK)
1467 error = vn_close(vp, fp->f_flag, fp->f_cred, td);
1469 if (fp->f_type == DTYPE_VNODE && fp->f_flag & FHASLOCK) {
1470 lf.l_whence = SEEK_SET;
1473 lf.l_type = F_UNLCK;
1474 (void) VOP_ADVLOCK(vp, fp, F_UNLCK, &lf, F_FLOCK);
1481 * Preparing to start a filesystem write operation. If the operation is
1482 * permitted, then we bump the count of operations in progress and
1483 * proceed. If a suspend request is in progress, we wait until the
1484 * suspension is over, and then proceed.
1487 vn_start_write_locked(struct mount *mp, int flags)
1491 mtx_assert(MNT_MTX(mp), MA_OWNED);
1495 * Check on status of suspension.
1497 if ((curthread->td_pflags & TDP_IGNSUSP) == 0 ||
1498 mp->mnt_susp_owner != curthread) {
1499 while ((mp->mnt_kern_flag & MNTK_SUSPEND) != 0) {
1500 if (flags & V_NOWAIT) {
1501 error = EWOULDBLOCK;
1504 error = msleep(&mp->mnt_flag, MNT_MTX(mp),
1505 (PUSER - 1) | (flags & PCATCH), "suspfs", 0);
1510 if (flags & V_XSLEEP)
1512 mp->mnt_writeopcount++;
1514 if (error != 0 || (flags & V_XSLEEP) != 0)
1521 vn_start_write(vp, mpp, flags)
1531 * If a vnode is provided, get and return the mount point that
1532 * to which it will write.
1535 if ((error = VOP_GETWRITEMOUNT(vp, mpp)) != 0) {
1537 if (error != EOPNOTSUPP)
1542 if ((mp = *mpp) == NULL)
1546 * VOP_GETWRITEMOUNT() returns with the mp refcount held through
1548 * As long as a vnode is not provided we need to acquire a
1549 * refcount for the provided mountpoint too, in order to
1550 * emulate a vfs_ref().
1556 return (vn_start_write_locked(mp, flags));
1560 * Secondary suspension. Used by operations such as vop_inactive
1561 * routines that are needed by the higher level functions. These
1562 * are allowed to proceed until all the higher level functions have
1563 * completed (indicated by mnt_writeopcount dropping to zero). At that
1564 * time, these operations are halted until the suspension is over.
1567 vn_start_secondary_write(vp, mpp, flags)
1577 if ((error = VOP_GETWRITEMOUNT(vp, mpp)) != 0) {
1579 if (error != EOPNOTSUPP)
1585 * If we are not suspended or have not yet reached suspended
1586 * mode, then let the operation proceed.
1588 if ((mp = *mpp) == NULL)
1592 * VOP_GETWRITEMOUNT() returns with the mp refcount held through
1594 * As long as a vnode is not provided we need to acquire a
1595 * refcount for the provided mountpoint too, in order to
1596 * emulate a vfs_ref().
1601 if ((mp->mnt_kern_flag & (MNTK_SUSPENDED | MNTK_SUSPEND2)) == 0) {
1602 mp->mnt_secondary_writes++;
1603 mp->mnt_secondary_accwrites++;
1607 if (flags & V_NOWAIT) {
1610 return (EWOULDBLOCK);
1613 * Wait for the suspension to finish.
1615 error = msleep(&mp->mnt_flag, MNT_MTX(mp),
1616 (PUSER - 1) | (flags & PCATCH) | PDROP, "suspfs", 0);
1624 * Filesystem write operation has completed. If we are suspending and this
1625 * operation is the last one, notify the suspender that the suspension is
1629 vn_finished_write(mp)
1636 mp->mnt_writeopcount--;
1637 if (mp->mnt_writeopcount < 0)
1638 panic("vn_finished_write: neg cnt");
1639 if ((mp->mnt_kern_flag & MNTK_SUSPEND) != 0 &&
1640 mp->mnt_writeopcount <= 0)
1641 wakeup(&mp->mnt_writeopcount);
1647 * Filesystem secondary write operation has completed. If we are
1648 * suspending and this operation is the last one, notify the suspender
1649 * that the suspension is now in effect.
1652 vn_finished_secondary_write(mp)
1659 mp->mnt_secondary_writes--;
1660 if (mp->mnt_secondary_writes < 0)
1661 panic("vn_finished_secondary_write: neg cnt");
1662 if ((mp->mnt_kern_flag & MNTK_SUSPEND) != 0 &&
1663 mp->mnt_secondary_writes <= 0)
1664 wakeup(&mp->mnt_secondary_writes);
1671 * Request a filesystem to suspend write operations.
1674 vfs_write_suspend(struct mount *mp, int flags)
1679 if (mp->mnt_susp_owner == curthread) {
1683 while (mp->mnt_kern_flag & MNTK_SUSPEND)
1684 msleep(&mp->mnt_flag, MNT_MTX(mp), PUSER - 1, "wsuspfs", 0);
1687 * Unmount holds a write reference on the mount point. If we
1688 * own busy reference and drain for writers, we deadlock with
1689 * the reference draining in the unmount path. Callers of
1690 * vfs_write_suspend() must specify VS_SKIP_UNMOUNT if
1691 * vfs_busy() reference is owned and caller is not in the
1694 if ((flags & VS_SKIP_UNMOUNT) != 0 &&
1695 (mp->mnt_kern_flag & MNTK_UNMOUNT) != 0) {
1700 mp->mnt_kern_flag |= MNTK_SUSPEND;
1701 mp->mnt_susp_owner = curthread;
1702 if (mp->mnt_writeopcount > 0)
1703 (void) msleep(&mp->mnt_writeopcount,
1704 MNT_MTX(mp), (PUSER - 1)|PDROP, "suspwt", 0);
1707 if ((error = VFS_SYNC(mp, MNT_SUSPEND)) != 0)
1708 vfs_write_resume(mp, 0);
1713 * Request a filesystem to resume write operations.
1716 vfs_write_resume(struct mount *mp, int flags)
1720 if ((mp->mnt_kern_flag & MNTK_SUSPEND) != 0) {
1721 KASSERT(mp->mnt_susp_owner == curthread, ("mnt_susp_owner"));
1722 mp->mnt_kern_flag &= ~(MNTK_SUSPEND | MNTK_SUSPEND2 |
1724 mp->mnt_susp_owner = NULL;
1725 wakeup(&mp->mnt_writeopcount);
1726 wakeup(&mp->mnt_flag);
1727 curthread->td_pflags &= ~TDP_IGNSUSP;
1728 if ((flags & VR_START_WRITE) != 0) {
1730 mp->mnt_writeopcount++;
1733 if ((flags & VR_NO_SUSPCLR) == 0)
1735 } else if ((flags & VR_START_WRITE) != 0) {
1737 vn_start_write_locked(mp, 0);
1744 * Implement kqueues for files by translating it to vnode operation.
1747 vn_kqfilter(struct file *fp, struct knote *kn)
1750 return (VOP_KQFILTER(fp->f_vnode, kn));
1754 * Simplified in-kernel wrapper calls for extended attribute access.
1755 * Both calls pass in a NULL credential, authorizing as "kernel" access.
1756 * Set IO_NODELOCKED in ioflg if the vnode is already locked.
1759 vn_extattr_get(struct vnode *vp, int ioflg, int attrnamespace,
1760 const char *attrname, int *buflen, char *buf, struct thread *td)
1766 iov.iov_len = *buflen;
1769 auio.uio_iov = &iov;
1770 auio.uio_iovcnt = 1;
1771 auio.uio_rw = UIO_READ;
1772 auio.uio_segflg = UIO_SYSSPACE;
1774 auio.uio_offset = 0;
1775 auio.uio_resid = *buflen;
1777 if ((ioflg & IO_NODELOCKED) == 0)
1778 vn_lock(vp, LK_SHARED | LK_RETRY);
1780 ASSERT_VOP_LOCKED(vp, "IO_NODELOCKED with no vp lock held");
1782 /* authorize attribute retrieval as kernel */
1783 error = VOP_GETEXTATTR(vp, attrnamespace, attrname, &auio, NULL, NULL,
1786 if ((ioflg & IO_NODELOCKED) == 0)
1790 *buflen = *buflen - auio.uio_resid;
1797 * XXX failure mode if partially written?
1800 vn_extattr_set(struct vnode *vp, int ioflg, int attrnamespace,
1801 const char *attrname, int buflen, char *buf, struct thread *td)
1808 iov.iov_len = buflen;
1811 auio.uio_iov = &iov;
1812 auio.uio_iovcnt = 1;
1813 auio.uio_rw = UIO_WRITE;
1814 auio.uio_segflg = UIO_SYSSPACE;
1816 auio.uio_offset = 0;
1817 auio.uio_resid = buflen;
1819 if ((ioflg & IO_NODELOCKED) == 0) {
1820 if ((error = vn_start_write(vp, &mp, V_WAIT)) != 0)
1822 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1825 ASSERT_VOP_LOCKED(vp, "IO_NODELOCKED with no vp lock held");
1827 /* authorize attribute setting as kernel */
1828 error = VOP_SETEXTATTR(vp, attrnamespace, attrname, &auio, NULL, td);
1830 if ((ioflg & IO_NODELOCKED) == 0) {
1831 vn_finished_write(mp);
1839 vn_extattr_rm(struct vnode *vp, int ioflg, int attrnamespace,
1840 const char *attrname, struct thread *td)
1845 if ((ioflg & IO_NODELOCKED) == 0) {
1846 if ((error = vn_start_write(vp, &mp, V_WAIT)) != 0)
1848 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1851 ASSERT_VOP_LOCKED(vp, "IO_NODELOCKED with no vp lock held");
1853 /* authorize attribute removal as kernel */
1854 error = VOP_DELETEEXTATTR(vp, attrnamespace, attrname, NULL, td);
1855 if (error == EOPNOTSUPP)
1856 error = VOP_SETEXTATTR(vp, attrnamespace, attrname, NULL,
1859 if ((ioflg & IO_NODELOCKED) == 0) {
1860 vn_finished_write(mp);
1868 vn_vget_ino(struct vnode *vp, ino_t ino, int lkflags, struct vnode **rvp)
1874 ltype = VOP_ISLOCKED(vp);
1875 KASSERT(ltype == LK_EXCLUSIVE || ltype == LK_SHARED,
1876 ("vn_vget_ino: vp not locked"));
1877 error = vfs_busy(mp, MBF_NOWAIT);
1881 error = vfs_busy(mp, 0);
1882 vn_lock(vp, ltype | LK_RETRY);
1886 if (vp->v_iflag & VI_DOOMED) {
1892 error = VFS_VGET(mp, ino, lkflags, rvp);
1894 vn_lock(vp, ltype | LK_RETRY);
1895 if (vp->v_iflag & VI_DOOMED) {
1904 vn_rlimit_fsize(const struct vnode *vp, const struct uio *uio,
1905 const struct thread *td)
1908 if (vp->v_type != VREG || td == NULL)
1910 PROC_LOCK(td->td_proc);
1911 if ((uoff_t)uio->uio_offset + uio->uio_resid >
1912 lim_cur(td->td_proc, RLIMIT_FSIZE)) {
1913 kern_psignal(td->td_proc, SIGXFSZ);
1914 PROC_UNLOCK(td->td_proc);
1917 PROC_UNLOCK(td->td_proc);
1922 vn_chmod(struct file *fp, mode_t mode, struct ucred *active_cred,
1929 vn_lock(vp, LK_SHARED | LK_RETRY);
1930 AUDIT_ARG_VNODE1(vp);
1933 return (setfmode(td, active_cred, vp, mode));
1937 vn_chown(struct file *fp, uid_t uid, gid_t gid, struct ucred *active_cred,
1944 vn_lock(vp, LK_SHARED | LK_RETRY);
1945 AUDIT_ARG_VNODE1(vp);
1948 return (setfown(td, active_cred, vp, uid, gid));
1952 vn_pages_remove(struct vnode *vp, vm_pindex_t start, vm_pindex_t end)
1956 if ((object = vp->v_object) == NULL)
1958 VM_OBJECT_WLOCK(object);
1959 vm_object_page_remove(object, start, end, 0);
1960 VM_OBJECT_WUNLOCK(object);
1964 vn_bmap_seekhole(struct vnode *vp, u_long cmd, off_t *off, struct ucred *cred)
1972 KASSERT(cmd == FIOSEEKHOLE || cmd == FIOSEEKDATA,
1973 ("Wrong command %lu", cmd));
1975 if (vn_lock(vp, LK_SHARED) != 0)
1977 if (vp->v_type != VREG) {
1981 error = VOP_GETATTR(vp, &va, cred);
1985 if (noff >= va.va_size) {
1989 bsize = vp->v_mount->mnt_stat.f_iosize;
1990 for (bn = noff / bsize; noff < va.va_size; bn++, noff += bsize) {
1991 error = VOP_BMAP(vp, bn, NULL, &bnp, NULL, NULL);
1992 if (error == EOPNOTSUPP) {
1996 if ((bnp == -1 && cmd == FIOSEEKHOLE) ||
1997 (bnp != -1 && cmd == FIOSEEKDATA)) {
2004 if (noff > va.va_size)
2006 /* noff == va.va_size. There is an implicit hole at the end of file. */
2007 if (cmd == FIOSEEKDATA)
2017 vn_seek(struct file *fp, off_t offset, int whence, struct thread *td)
2022 off_t foffset, size;
2025 cred = td->td_ucred;
2027 foffset = foffset_lock(fp, 0);
2028 noneg = (vp->v_type != VCHR);
2034 (offset > 0 && foffset > OFF_MAX - offset))) {
2041 vn_lock(vp, LK_SHARED | LK_RETRY);
2042 error = VOP_GETATTR(vp, &vattr, cred);
2048 * If the file references a disk device, then fetch
2049 * the media size and use that to determine the ending
2052 if (vattr.va_size == 0 && vp->v_type == VCHR &&
2053 fo_ioctl(fp, DIOCGMEDIASIZE, &size, cred, td) == 0)
2054 vattr.va_size = size;
2056 (vattr.va_size > OFF_MAX ||
2057 (offset > 0 && vattr.va_size > OFF_MAX - offset))) {
2061 offset += vattr.va_size;
2066 error = fo_ioctl(fp, FIOSEEKDATA, &offset, cred, td);
2069 error = fo_ioctl(fp, FIOSEEKHOLE, &offset, cred, td);
2074 if (error == 0 && noneg && offset < 0)
2078 VFS_KNOTE_UNLOCKED(vp, 0);
2079 *(off_t *)(td->td_retval) = offset;
2081 foffset_unlock(fp, offset, error != 0 ? FOF_NOUPDATE : 0);