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/ttycom.h>
61 #include <sys/syslog.h>
62 #include <sys/unistd.h>
64 #include <security/audit/audit.h>
65 #include <security/mac/mac_framework.h>
68 #include <vm/vm_object.h>
70 static fo_rdwr_t vn_read;
71 static fo_rdwr_t vn_write;
72 static fo_truncate_t vn_truncate;
73 static fo_ioctl_t vn_ioctl;
74 static fo_poll_t vn_poll;
75 static fo_kqfilter_t vn_kqfilter;
76 static fo_stat_t vn_statfile;
77 static fo_close_t vn_closefile;
79 struct fileops vnops = {
82 .fo_truncate = vn_truncate,
85 .fo_kqfilter = vn_kqfilter,
86 .fo_stat = vn_statfile,
87 .fo_close = vn_closefile,
90 .fo_flags = DFLAG_PASSABLE | DFLAG_SEEKABLE
94 vn_open(ndp, flagp, cmode, fp)
95 struct nameidata *ndp;
99 struct thread *td = ndp->ni_cnd.cn_thread;
101 return (vn_open_cred(ndp, flagp, cmode, 0, td->td_ucred, fp));
105 * Common code for vnode open operations.
106 * Check permissions, and call the VOP_OPEN or VOP_CREATE routine.
108 * Note that this does NOT free nameidata for the successful case,
109 * due to the NDINIT being done elsewhere.
112 vn_open_cred(struct nameidata *ndp, int *flagp, int cmode, u_int vn_open_flags,
113 struct ucred *cred, struct file *fp)
117 struct thread *td = ndp->ni_cnd.cn_thread;
119 struct vattr *vap = &vat;
122 int vfslocked, mpsafe;
124 mpsafe = ndp->ni_cnd.cn_flags & MPSAFE;
128 if (fmode & O_CREAT) {
129 ndp->ni_cnd.cn_nameiop = CREATE;
130 ndp->ni_cnd.cn_flags = ISOPEN | LOCKPARENT | LOCKLEAF |
132 if ((fmode & O_EXCL) == 0 && (fmode & O_NOFOLLOW) == 0)
133 ndp->ni_cnd.cn_flags |= FOLLOW;
134 if (!(vn_open_flags & VN_OPEN_NOAUDIT))
135 ndp->ni_cnd.cn_flags |= AUDITVNODE1;
137 if ((error = namei(ndp)) != 0)
139 vfslocked = NDHASGIANT(ndp);
141 ndp->ni_cnd.cn_flags &= ~MPSAFE;
142 if (ndp->ni_vp == NULL) {
145 vap->va_mode = cmode;
147 vap->va_vaflags |= VA_EXCLUSIVE;
148 if (vn_start_write(ndp->ni_dvp, &mp, V_NOWAIT) != 0) {
149 NDFREE(ndp, NDF_ONLY_PNBUF);
151 VFS_UNLOCK_GIANT(vfslocked);
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 VFS_UNLOCK_GIANT(vfslocked);
168 NDFREE(ndp, NDF_ONLY_PNBUF);
174 if (ndp->ni_dvp == ndp->ni_vp)
180 if (fmode & O_EXCL) {
187 ndp->ni_cnd.cn_nameiop = LOOKUP;
188 ndp->ni_cnd.cn_flags = ISOPEN |
189 ((fmode & O_NOFOLLOW) ? NOFOLLOW : FOLLOW) |
191 if (!(fmode & FWRITE))
192 ndp->ni_cnd.cn_flags |= LOCKSHARED;
193 if (!(vn_open_flags & VN_OPEN_NOAUDIT))
194 ndp->ni_cnd.cn_flags |= AUDITVNODE1;
195 if ((error = namei(ndp)) != 0)
198 ndp->ni_cnd.cn_flags &= ~MPSAFE;
199 vfslocked = NDHASGIANT(ndp);
202 if (vp->v_type == VLNK) {
206 if (vp->v_type == VSOCK) {
210 if (vp->v_type != VDIR && fmode & O_DIRECTORY) {
215 if (fmode & (FWRITE | O_TRUNC)) {
216 if (vp->v_type == VDIR) {
226 if ((fmode & O_APPEND) && (fmode & FWRITE))
229 error = mac_vnode_check_open(cred, vp, accmode);
233 if ((fmode & O_CREAT) == 0) {
234 if (accmode & VWRITE) {
235 error = vn_writechk(vp);
240 error = VOP_ACCESS(vp, accmode, cred, td);
245 if ((error = VOP_OPEN(vp, fmode, cred, td, fp)) != 0)
251 ASSERT_VOP_LOCKED(vp, "vn_open_cred");
253 VFS_UNLOCK_GIANT(vfslocked);
256 NDFREE(ndp, NDF_ONLY_PNBUF);
258 VFS_UNLOCK_GIANT(vfslocked);
265 * Check for write permissions on the specified vnode.
266 * Prototype text segments cannot be written.
270 register struct vnode *vp;
273 ASSERT_VOP_LOCKED(vp, "vn_writechk");
275 * If there's shared text associated with
276 * the vnode, try to free it up once. If
277 * we fail, we can't allow writing.
279 if (vp->v_vflag & VV_TEXT)
289 vn_close(vp, flags, file_cred, td)
290 register struct vnode *vp;
292 struct ucred *file_cred;
296 int error, lock_flags;
298 if (!(flags & FWRITE) && vp->v_mount != NULL &&
299 vp->v_mount->mnt_kern_flag & MNTK_EXTENDED_SHARED)
300 lock_flags = LK_SHARED;
302 lock_flags = LK_EXCLUSIVE;
304 VFS_ASSERT_GIANT(vp->v_mount);
306 vn_start_write(vp, &mp, V_WAIT);
307 vn_lock(vp, lock_flags | LK_RETRY);
308 if (flags & FWRITE) {
309 VNASSERT(vp->v_writecount > 0, vp,
310 ("vn_close: negative writecount"));
313 error = VOP_CLOSE(vp, flags, file_cred, td);
315 vn_finished_write(mp);
320 * Heuristic to detect sequential operation.
323 sequential_heuristic(struct uio *uio, struct file *fp)
326 if (atomic_load_acq_int(&(fp->f_flag)) & FRDAHEAD)
327 return (fp->f_seqcount << IO_SEQSHIFT);
330 * Offset 0 is handled specially. open() sets f_seqcount to 1 so
331 * that the first I/O is normally considered to be slightly
332 * sequential. Seeking to offset 0 doesn't change sequentiality
333 * unless previous seeks have reduced f_seqcount to 0, in which
334 * case offset 0 is not special.
336 if ((uio->uio_offset == 0 && fp->f_seqcount > 0) ||
337 uio->uio_offset == fp->f_nextoff) {
339 * f_seqcount is in units of fixed-size blocks so that it
340 * depends mainly on the amount of sequential I/O and not
341 * much on the number of sequential I/O's. The fixed size
342 * of 16384 is hard-coded here since it is (not quite) just
343 * a magic size that works well here. This size is more
344 * closely related to the best I/O size for real disks than
345 * to any block size used by software.
347 fp->f_seqcount += howmany(uio->uio_resid, 16384);
348 if (fp->f_seqcount > IO_SEQMAX)
349 fp->f_seqcount = IO_SEQMAX;
350 return (fp->f_seqcount << IO_SEQSHIFT);
353 /* Not sequential. Quickly draw-down sequentiality. */
354 if (fp->f_seqcount > 1)
362 * Package up an I/O request on a vnode into a uio and do it.
365 vn_rdwr(rw, vp, base, len, offset, segflg, ioflg, active_cred, file_cred,
374 struct ucred *active_cred;
375 struct ucred *file_cred;
383 int error, lock_flags;
385 VFS_ASSERT_GIANT(vp->v_mount);
387 if ((ioflg & IO_NODELOCKED) == 0) {
389 if (rw == UIO_WRITE) {
390 if (vp->v_type != VCHR &&
391 (error = vn_start_write(vp, &mp, V_WAIT | PCATCH))
394 if (MNT_SHARED_WRITES(mp) ||
395 ((mp == NULL) && MNT_SHARED_WRITES(vp->v_mount))) {
396 lock_flags = LK_SHARED;
398 lock_flags = LK_EXCLUSIVE;
400 vn_lock(vp, lock_flags | LK_RETRY);
402 vn_lock(vp, LK_SHARED | LK_RETRY);
405 ASSERT_VOP_LOCKED(vp, "IO_NODELOCKED with no vp lock held");
406 auio.uio_iov = &aiov;
408 aiov.iov_base = base;
410 auio.uio_resid = len;
411 auio.uio_offset = offset;
412 auio.uio_segflg = segflg;
417 if ((ioflg & IO_NOMACCHECK) == 0) {
419 error = mac_vnode_check_read(active_cred, file_cred,
422 error = mac_vnode_check_write(active_cred, file_cred,
432 error = VOP_READ(vp, &auio, ioflg, cred);
434 error = VOP_WRITE(vp, &auio, ioflg, cred);
437 *aresid = auio.uio_resid;
439 if (auio.uio_resid && error == 0)
441 if ((ioflg & IO_NODELOCKED) == 0) {
442 if (rw == UIO_WRITE && vp->v_type != VCHR)
443 vn_finished_write(mp);
450 * Package up an I/O request on a vnode into a uio and do it. The I/O
451 * request is split up into smaller chunks and we try to avoid saturating
452 * the buffer cache while potentially holding a vnode locked, so we
453 * check bwillwrite() before calling vn_rdwr(). We also call kern_yield()
454 * to give other processes a chance to lock the vnode (either other processes
455 * core'ing the same binary, or unrelated processes scanning the directory).
458 vn_rdwr_inchunks(rw, vp, base, len, offset, segflg, ioflg, active_cred,
459 file_cred, aresid, td)
467 struct ucred *active_cred;
468 struct ucred *file_cred;
475 VFS_ASSERT_GIANT(vp->v_mount);
481 * Force `offset' to a multiple of MAXBSIZE except possibly
482 * for the first chunk, so that filesystems only need to
483 * write full blocks except possibly for the first and last
486 chunk = MAXBSIZE - (uoff_t)offset % MAXBSIZE;
490 if (rw != UIO_READ && vp->v_type == VREG)
493 error = vn_rdwr(rw, vp, base, chunk, offset, segflg,
494 ioflg, active_cred, file_cred, &iaresid, td);
495 len -= chunk; /* aresid calc already includes length */
499 base = (char *)base + chunk;
500 kern_yield(PRI_USER);
503 *aresid = len + iaresid;
508 * File table vnode read routine.
511 vn_read(fp, uio, active_cred, flags, td)
514 struct ucred *active_cred;
523 KASSERT(uio->uio_td == td, ("uio_td %p is not td %p",
528 if (fp->f_flag & FNONBLOCK)
530 if (fp->f_flag & O_DIRECT)
532 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
534 * According to McKusick the vn lock was protecting f_offset here.
535 * It is now protected by the FOFFSET_LOCKED flag.
537 if ((flags & FOF_OFFSET) == 0) {
538 mtxp = mtx_pool_find(mtxpool_sleep, fp);
540 while(fp->f_vnread_flags & FOFFSET_LOCKED) {
541 fp->f_vnread_flags |= FOFFSET_LOCK_WAITING;
542 msleep(&fp->f_vnread_flags, mtxp, PUSER -1,
543 "vnread offlock", 0);
545 fp->f_vnread_flags |= FOFFSET_LOCKED;
547 vn_lock(vp, LK_SHARED | LK_RETRY);
548 uio->uio_offset = fp->f_offset;
550 vn_lock(vp, LK_SHARED | LK_RETRY);
552 ioflag |= sequential_heuristic(uio, fp);
555 error = mac_vnode_check_read(active_cred, fp->f_cred, vp);
558 error = VOP_READ(vp, uio, ioflag, fp->f_cred);
559 if ((flags & FOF_OFFSET) == 0) {
560 fp->f_offset = uio->uio_offset;
562 if (fp->f_vnread_flags & FOFFSET_LOCK_WAITING)
563 wakeup(&fp->f_vnread_flags);
564 fp->f_vnread_flags = 0;
567 fp->f_nextoff = uio->uio_offset;
569 VFS_UNLOCK_GIANT(vfslocked);
574 * File table vnode write routine.
577 vn_write(fp, uio, active_cred, flags, td)
580 struct ucred *active_cred;
586 int error, ioflag, lock_flags;
589 KASSERT(uio->uio_td == td, ("uio_td %p is not td %p",
592 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
593 if (vp->v_type == VREG)
596 if (vp->v_type == VREG && (fp->f_flag & O_APPEND))
598 if (fp->f_flag & FNONBLOCK)
600 if (fp->f_flag & O_DIRECT)
602 if ((fp->f_flag & O_FSYNC) ||
603 (vp->v_mount && (vp->v_mount->mnt_flag & MNT_SYNCHRONOUS)))
606 if (vp->v_type != VCHR &&
607 (error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
610 if ((MNT_SHARED_WRITES(mp) ||
611 ((mp == NULL) && MNT_SHARED_WRITES(vp->v_mount))) &&
612 (flags & FOF_OFFSET) != 0) {
613 lock_flags = LK_SHARED;
615 lock_flags = LK_EXCLUSIVE;
618 vn_lock(vp, lock_flags | LK_RETRY);
619 if ((flags & FOF_OFFSET) == 0)
620 uio->uio_offset = fp->f_offset;
621 ioflag |= sequential_heuristic(uio, fp);
623 error = mac_vnode_check_write(active_cred, fp->f_cred, vp);
626 error = VOP_WRITE(vp, uio, ioflag, fp->f_cred);
627 if ((flags & FOF_OFFSET) == 0)
628 fp->f_offset = uio->uio_offset;
629 fp->f_nextoff = uio->uio_offset;
631 if (vp->v_type != VCHR)
632 vn_finished_write(mp);
634 VFS_UNLOCK_GIANT(vfslocked);
639 * File table truncate routine.
642 vn_truncate(fp, length, active_cred, td)
645 struct ucred *active_cred;
655 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
656 error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
658 VFS_UNLOCK_GIANT(vfslocked);
661 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
662 if (vp->v_type == VDIR) {
667 error = mac_vnode_check_write(active_cred, fp->f_cred, vp);
671 error = vn_writechk(vp);
674 vattr.va_size = length;
675 error = VOP_SETATTR(vp, &vattr, fp->f_cred);
679 vn_finished_write(mp);
680 VFS_UNLOCK_GIANT(vfslocked);
685 * File table vnode stat routine.
688 vn_statfile(fp, sb, active_cred, td)
691 struct ucred *active_cred;
694 struct vnode *vp = fp->f_vnode;
698 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
699 vn_lock(vp, LK_SHARED | LK_RETRY);
700 error = vn_stat(vp, sb, active_cred, fp->f_cred, td);
702 VFS_UNLOCK_GIANT(vfslocked);
708 * Stat a vnode; implementation for the stat syscall
711 vn_stat(vp, sb, active_cred, file_cred, td)
713 register struct stat *sb;
714 struct ucred *active_cred;
715 struct ucred *file_cred;
719 register struct vattr *vap;
724 error = mac_vnode_check_stat(active_cred, file_cred, vp);
732 * Initialize defaults for new and unusual fields, so that file
733 * systems which don't support these fields don't need to know
736 vap->va_birthtime.tv_sec = -1;
737 vap->va_birthtime.tv_nsec = 0;
738 vap->va_fsid = VNOVAL;
739 vap->va_rdev = NODEV;
741 error = VOP_GETATTR(vp, vap, active_cred);
746 * Zero the spare stat fields
748 bzero(sb, sizeof *sb);
751 * Copy from vattr table
753 if (vap->va_fsid != VNOVAL)
754 sb->st_dev = vap->va_fsid;
756 sb->st_dev = vp->v_mount->mnt_stat.f_fsid.val[0];
757 sb->st_ino = vap->va_fileid;
759 switch (vap->va_type) {
785 sb->st_nlink = vap->va_nlink;
786 sb->st_uid = vap->va_uid;
787 sb->st_gid = vap->va_gid;
788 sb->st_rdev = vap->va_rdev;
789 if (vap->va_size > OFF_MAX)
791 sb->st_size = vap->va_size;
792 sb->st_atim = vap->va_atime;
793 sb->st_mtim = vap->va_mtime;
794 sb->st_ctim = vap->va_ctime;
795 sb->st_birthtim = vap->va_birthtime;
798 * According to www.opengroup.org, the meaning of st_blksize is
799 * "a filesystem-specific preferred I/O block size for this
800 * object. In some filesystem types, this may vary from file
802 * Use miminum/default of PAGE_SIZE (e.g. for VCHR).
805 sb->st_blksize = max(PAGE_SIZE, vap->va_blocksize);
807 sb->st_flags = vap->va_flags;
808 if (priv_check(td, PRIV_VFS_GENERATION))
811 sb->st_gen = vap->va_gen;
813 sb->st_blocks = vap->va_bytes / S_BLKSIZE;
818 * File table vnode ioctl routine.
821 vn_ioctl(fp, com, data, active_cred, td)
825 struct ucred *active_cred;
828 struct vnode *vp = fp->f_vnode;
833 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
835 switch (vp->v_type) {
838 if (com == FIONREAD) {
839 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
840 error = VOP_GETATTR(vp, &vattr, active_cred);
843 *(int *)data = vattr.va_size - fp->f_offset;
845 if (com == FIONBIO || com == FIOASYNC) /* XXX */
848 error = VOP_IOCTL(vp, com, data, fp->f_flag,
855 VFS_UNLOCK_GIANT(vfslocked);
860 * File table vnode poll routine.
863 vn_poll(fp, events, active_cred, td)
866 struct ucred *active_cred;
874 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
876 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
877 error = mac_vnode_check_poll(active_cred, fp->f_cred, vp);
882 error = VOP_POLL(vp, events, fp->f_cred, td);
883 VFS_UNLOCK_GIANT(vfslocked);
888 * Acquire the requested lock and then check for validity. LK_RETRY
889 * permits vn_lock to return doomed vnodes.
892 _vn_lock(struct vnode *vp, int flags, char *file, int line)
896 VNASSERT((flags & LK_TYPE_MASK) != 0, vp,
897 ("vn_lock called with no locktype."));
899 #ifdef DEBUG_VFS_LOCKS
900 KASSERT(vp->v_holdcnt != 0,
901 ("vn_lock %p: zero hold count", vp));
903 error = VOP_LOCK1(vp, flags, file, line);
904 flags &= ~LK_INTERLOCK; /* Interlock is always dropped. */
905 KASSERT((flags & LK_RETRY) == 0 || error == 0,
906 ("LK_RETRY set with incompatible flags (0x%x) or an error occured (%d)",
909 * Callers specify LK_RETRY if they wish to get dead vnodes.
910 * If RETRY is not set, we return ENOENT instead.
912 if (error == 0 && vp->v_iflag & VI_DOOMED &&
913 (flags & LK_RETRY) == 0) {
918 } while (flags & LK_RETRY && error != 0);
923 * File table vnode close routine.
937 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
938 if (fp->f_type == DTYPE_VNODE && fp->f_flag & FHASLOCK) {
939 lf.l_whence = SEEK_SET;
943 (void) VOP_ADVLOCK(vp, fp, F_UNLCK, &lf, F_FLOCK);
946 fp->f_ops = &badfileops;
948 error = vn_close(vp, fp->f_flag, fp->f_cred, td);
949 VFS_UNLOCK_GIANT(vfslocked);
954 * Preparing to start a filesystem write operation. If the operation is
955 * permitted, then we bump the count of operations in progress and
956 * proceed. If a suspend request is in progress, we wait until the
957 * suspension is over, and then proceed.
960 vn_start_write(vp, mpp, flags)
970 * If a vnode is provided, get and return the mount point that
971 * to which it will write.
974 if ((error = VOP_GETWRITEMOUNT(vp, mpp)) != 0) {
976 if (error != EOPNOTSUPP)
981 if ((mp = *mpp) == NULL)
985 * VOP_GETWRITEMOUNT() returns with the mp refcount held through
987 * As long as a vnode is not provided we need to acquire a
988 * refcount for the provided mountpoint too, in order to
989 * emulate a vfs_ref().
996 * Check on status of suspension.
998 if ((curthread->td_pflags & TDP_IGNSUSP) == 0 ||
999 mp->mnt_susp_owner != curthread) {
1000 while ((mp->mnt_kern_flag & MNTK_SUSPEND) != 0) {
1001 if (flags & V_NOWAIT) {
1002 error = EWOULDBLOCK;
1005 error = msleep(&mp->mnt_flag, MNT_MTX(mp),
1006 (PUSER - 1) | (flags & PCATCH), "suspfs", 0);
1011 if (flags & V_XSLEEP)
1013 mp->mnt_writeopcount++;
1015 if (error != 0 || (flags & V_XSLEEP) != 0)
1022 * Secondary suspension. Used by operations such as vop_inactive
1023 * routines that are needed by the higher level functions. These
1024 * are allowed to proceed until all the higher level functions have
1025 * completed (indicated by mnt_writeopcount dropping to zero). At that
1026 * time, these operations are halted until the suspension is over.
1029 vn_start_secondary_write(vp, mpp, flags)
1039 if ((error = VOP_GETWRITEMOUNT(vp, mpp)) != 0) {
1041 if (error != EOPNOTSUPP)
1047 * If we are not suspended or have not yet reached suspended
1048 * mode, then let the operation proceed.
1050 if ((mp = *mpp) == NULL)
1054 * VOP_GETWRITEMOUNT() returns with the mp refcount held through
1056 * As long as a vnode is not provided we need to acquire a
1057 * refcount for the provided mountpoint too, in order to
1058 * emulate a vfs_ref().
1063 if ((mp->mnt_kern_flag & (MNTK_SUSPENDED | MNTK_SUSPEND2)) == 0) {
1064 mp->mnt_secondary_writes++;
1065 mp->mnt_secondary_accwrites++;
1069 if (flags & V_NOWAIT) {
1072 return (EWOULDBLOCK);
1075 * Wait for the suspension to finish.
1077 error = msleep(&mp->mnt_flag, MNT_MTX(mp),
1078 (PUSER - 1) | (flags & PCATCH) | PDROP, "suspfs", 0);
1086 * Filesystem write operation has completed. If we are suspending and this
1087 * operation is the last one, notify the suspender that the suspension is
1091 vn_finished_write(mp)
1098 mp->mnt_writeopcount--;
1099 if (mp->mnt_writeopcount < 0)
1100 panic("vn_finished_write: neg cnt");
1101 if ((mp->mnt_kern_flag & MNTK_SUSPEND) != 0 &&
1102 mp->mnt_writeopcount <= 0)
1103 wakeup(&mp->mnt_writeopcount);
1109 * Filesystem secondary write operation has completed. If we are
1110 * suspending and this operation is the last one, notify the suspender
1111 * that the suspension is now in effect.
1114 vn_finished_secondary_write(mp)
1121 mp->mnt_secondary_writes--;
1122 if (mp->mnt_secondary_writes < 0)
1123 panic("vn_finished_secondary_write: neg cnt");
1124 if ((mp->mnt_kern_flag & MNTK_SUSPEND) != 0 &&
1125 mp->mnt_secondary_writes <= 0)
1126 wakeup(&mp->mnt_secondary_writes);
1133 * Request a filesystem to suspend write operations.
1136 vfs_write_suspend(mp)
1142 if (mp->mnt_susp_owner == curthread) {
1146 while (mp->mnt_kern_flag & MNTK_SUSPEND)
1147 msleep(&mp->mnt_flag, MNT_MTX(mp), PUSER - 1, "wsuspfs", 0);
1148 mp->mnt_kern_flag |= MNTK_SUSPEND;
1149 mp->mnt_susp_owner = curthread;
1150 if (mp->mnt_writeopcount > 0)
1151 (void) msleep(&mp->mnt_writeopcount,
1152 MNT_MTX(mp), (PUSER - 1)|PDROP, "suspwt", 0);
1155 if ((error = VFS_SYNC(mp, MNT_SUSPEND)) != 0)
1156 vfs_write_resume(mp);
1161 * Request a filesystem to resume write operations.
1164 vfs_write_resume(mp)
1169 if ((mp->mnt_kern_flag & MNTK_SUSPEND) != 0) {
1170 KASSERT(mp->mnt_susp_owner == curthread, ("mnt_susp_owner"));
1171 mp->mnt_kern_flag &= ~(MNTK_SUSPEND | MNTK_SUSPEND2 |
1173 mp->mnt_susp_owner = NULL;
1174 wakeup(&mp->mnt_writeopcount);
1175 wakeup(&mp->mnt_flag);
1176 curthread->td_pflags &= ~TDP_IGNSUSP;
1184 * Implement kqueues for files by translating it to vnode operation.
1187 vn_kqfilter(struct file *fp, struct knote *kn)
1192 vfslocked = VFS_LOCK_GIANT(fp->f_vnode->v_mount);
1193 error = VOP_KQFILTER(fp->f_vnode, kn);
1194 VFS_UNLOCK_GIANT(vfslocked);
1200 * Simplified in-kernel wrapper calls for extended attribute access.
1201 * Both calls pass in a NULL credential, authorizing as "kernel" access.
1202 * Set IO_NODELOCKED in ioflg if the vnode is already locked.
1205 vn_extattr_get(struct vnode *vp, int ioflg, int attrnamespace,
1206 const char *attrname, int *buflen, char *buf, struct thread *td)
1212 iov.iov_len = *buflen;
1215 auio.uio_iov = &iov;
1216 auio.uio_iovcnt = 1;
1217 auio.uio_rw = UIO_READ;
1218 auio.uio_segflg = UIO_SYSSPACE;
1220 auio.uio_offset = 0;
1221 auio.uio_resid = *buflen;
1223 if ((ioflg & IO_NODELOCKED) == 0)
1224 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1226 ASSERT_VOP_LOCKED(vp, "IO_NODELOCKED with no vp lock held");
1228 /* authorize attribute retrieval as kernel */
1229 error = VOP_GETEXTATTR(vp, attrnamespace, attrname, &auio, NULL, NULL,
1232 if ((ioflg & IO_NODELOCKED) == 0)
1236 *buflen = *buflen - auio.uio_resid;
1243 * XXX failure mode if partially written?
1246 vn_extattr_set(struct vnode *vp, int ioflg, int attrnamespace,
1247 const char *attrname, int buflen, char *buf, struct thread *td)
1254 iov.iov_len = buflen;
1257 auio.uio_iov = &iov;
1258 auio.uio_iovcnt = 1;
1259 auio.uio_rw = UIO_WRITE;
1260 auio.uio_segflg = UIO_SYSSPACE;
1262 auio.uio_offset = 0;
1263 auio.uio_resid = buflen;
1265 if ((ioflg & IO_NODELOCKED) == 0) {
1266 if ((error = vn_start_write(vp, &mp, V_WAIT)) != 0)
1268 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1271 ASSERT_VOP_LOCKED(vp, "IO_NODELOCKED with no vp lock held");
1273 /* authorize attribute setting as kernel */
1274 error = VOP_SETEXTATTR(vp, attrnamespace, attrname, &auio, NULL, td);
1276 if ((ioflg & IO_NODELOCKED) == 0) {
1277 vn_finished_write(mp);
1285 vn_extattr_rm(struct vnode *vp, int ioflg, int attrnamespace,
1286 const char *attrname, struct thread *td)
1291 if ((ioflg & IO_NODELOCKED) == 0) {
1292 if ((error = vn_start_write(vp, &mp, V_WAIT)) != 0)
1294 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1297 ASSERT_VOP_LOCKED(vp, "IO_NODELOCKED with no vp lock held");
1299 /* authorize attribute removal as kernel */
1300 error = VOP_DELETEEXTATTR(vp, attrnamespace, attrname, NULL, td);
1301 if (error == EOPNOTSUPP)
1302 error = VOP_SETEXTATTR(vp, attrnamespace, attrname, NULL,
1305 if ((ioflg & IO_NODELOCKED) == 0) {
1306 vn_finished_write(mp);
1314 vn_vget_ino(struct vnode *vp, ino_t ino, int lkflags, struct vnode **rvp)
1320 ltype = VOP_ISLOCKED(vp);
1321 KASSERT(ltype == LK_EXCLUSIVE || ltype == LK_SHARED,
1322 ("vn_vget_ino: vp not locked"));
1323 error = vfs_busy(mp, MBF_NOWAIT);
1327 error = vfs_busy(mp, 0);
1328 vn_lock(vp, ltype | LK_RETRY);
1332 if (vp->v_iflag & VI_DOOMED) {
1338 error = VFS_VGET(mp, ino, lkflags, rvp);
1340 vn_lock(vp, ltype | LK_RETRY);
1341 if (vp->v_iflag & VI_DOOMED) {
1350 vn_rlimit_fsize(const struct vnode *vp, const struct uio *uio,
1351 const struct thread *td)
1354 if (vp->v_type != VREG || td == NULL)
1356 PROC_LOCK(td->td_proc);
1357 if ((uoff_t)uio->uio_offset + uio->uio_resid >
1358 lim_cur(td->td_proc, RLIMIT_FSIZE)) {
1359 kern_psignal(td->td_proc, SIGXFSZ);
1360 PROC_UNLOCK(td->td_proc);
1363 PROC_UNLOCK(td->td_proc);
1368 vn_chmod(struct file *fp, mode_t mode, struct ucred *active_cred,
1372 int error, vfslocked;
1375 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
1377 vn_lock(vp, LK_SHARED | LK_RETRY);
1378 AUDIT_ARG_VNODE1(vp);
1381 error = setfmode(td, active_cred, vp, mode);
1382 VFS_UNLOCK_GIANT(vfslocked);
1387 vn_chown(struct file *fp, uid_t uid, gid_t gid, struct ucred *active_cred,
1391 int error, vfslocked;
1394 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
1396 vn_lock(vp, LK_SHARED | LK_RETRY);
1397 AUDIT_ARG_VNODE1(vp);
1400 error = setfown(td, active_cred, vp, uid, gid);
1401 VFS_UNLOCK_GIANT(vfslocked);
1406 vn_pages_remove(struct vnode *vp, vm_pindex_t start, vm_pindex_t end)
1410 if ((object = vp->v_object) == NULL)
1412 VM_OBJECT_LOCK(object);
1413 vm_object_page_remove(object, start, end, 0);
1414 VM_OBJECT_UNLOCK(object);