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>
58 #include <sys/ttycom.h>
60 #include <sys/syslog.h>
61 #include <sys/unistd.h>
63 #include <security/mac/mac_framework.h>
65 static fo_rdwr_t vn_read;
66 static fo_rdwr_t vn_write;
67 static fo_truncate_t vn_truncate;
68 static fo_ioctl_t vn_ioctl;
69 static fo_poll_t vn_poll;
70 static fo_kqfilter_t vn_kqfilter;
71 static fo_stat_t vn_statfile;
72 static fo_close_t vn_closefile;
74 struct fileops vnops = {
77 .fo_truncate = vn_truncate,
80 .fo_kqfilter = vn_kqfilter,
81 .fo_stat = vn_statfile,
82 .fo_close = vn_closefile,
83 .fo_flags = DFLAG_PASSABLE | DFLAG_SEEKABLE
87 vn_open(ndp, flagp, cmode, fp)
88 struct nameidata *ndp;
92 struct thread *td = ndp->ni_cnd.cn_thread;
94 return (vn_open_cred(ndp, flagp, cmode, 0, td->td_ucred, fp));
98 * Common code for vnode open operations.
99 * Check permissions, and call the VOP_OPEN or VOP_CREATE routine.
101 * Note that this does NOT free nameidata for the successful case,
102 * due to the NDINIT being done elsewhere.
105 vn_open_cred(struct nameidata *ndp, int *flagp, int cmode, u_int vn_open_flags,
106 struct ucred *cred, struct file *fp)
110 struct thread *td = ndp->ni_cnd.cn_thread;
112 struct vattr *vap = &vat;
115 int vfslocked, mpsafe;
117 mpsafe = ndp->ni_cnd.cn_flags & MPSAFE;
121 if (fmode & O_CREAT) {
122 ndp->ni_cnd.cn_nameiop = CREATE;
123 ndp->ni_cnd.cn_flags = ISOPEN | LOCKPARENT | LOCKLEAF |
125 if ((fmode & O_EXCL) == 0 && (fmode & O_NOFOLLOW) == 0)
126 ndp->ni_cnd.cn_flags |= FOLLOW;
127 if (!(vn_open_flags & VN_OPEN_NOAUDIT))
128 ndp->ni_cnd.cn_flags |= AUDITVNODE1;
130 if ((error = namei(ndp)) != 0)
132 vfslocked = NDHASGIANT(ndp);
134 ndp->ni_cnd.cn_flags &= ~MPSAFE;
135 if (ndp->ni_vp == NULL) {
138 vap->va_mode = cmode;
140 vap->va_vaflags |= VA_EXCLUSIVE;
141 if (vn_start_write(ndp->ni_dvp, &mp, V_NOWAIT) != 0) {
142 NDFREE(ndp, NDF_ONLY_PNBUF);
144 VFS_UNLOCK_GIANT(vfslocked);
145 if ((error = vn_start_write(NULL, &mp,
146 V_XSLEEP | PCATCH)) != 0)
151 error = mac_vnode_check_create(cred, ndp->ni_dvp,
155 error = VOP_CREATE(ndp->ni_dvp, &ndp->ni_vp,
158 vn_finished_write(mp);
160 VFS_UNLOCK_GIANT(vfslocked);
161 NDFREE(ndp, NDF_ONLY_PNBUF);
167 if (ndp->ni_dvp == ndp->ni_vp)
173 if (fmode & O_EXCL) {
180 ndp->ni_cnd.cn_nameiop = LOOKUP;
181 ndp->ni_cnd.cn_flags = ISOPEN |
182 ((fmode & O_NOFOLLOW) ? NOFOLLOW : FOLLOW) |
184 if (!(fmode & FWRITE))
185 ndp->ni_cnd.cn_flags |= LOCKSHARED;
186 if (!(vn_open_flags & VN_OPEN_NOAUDIT))
187 ndp->ni_cnd.cn_flags |= AUDITVNODE1;
188 if ((error = namei(ndp)) != 0)
191 ndp->ni_cnd.cn_flags &= ~MPSAFE;
192 vfslocked = NDHASGIANT(ndp);
195 if (vp->v_type == VLNK) {
199 if (vp->v_type == VSOCK) {
204 if (fmode & (FWRITE | O_TRUNC)) {
205 if (vp->v_type == VDIR) {
215 if ((fmode & O_APPEND) && (fmode & FWRITE))
218 error = mac_vnode_check_open(cred, vp, accmode);
222 if ((fmode & O_CREAT) == 0) {
223 if (accmode & VWRITE) {
224 error = vn_writechk(vp);
229 error = VOP_ACCESS(vp, accmode, cred, td);
234 if ((error = VOP_OPEN(vp, fmode, cred, td, fp)) != 0)
240 ASSERT_VOP_LOCKED(vp, "vn_open_cred");
242 VFS_UNLOCK_GIANT(vfslocked);
245 NDFREE(ndp, NDF_ONLY_PNBUF);
247 VFS_UNLOCK_GIANT(vfslocked);
254 * Check for write permissions on the specified vnode.
255 * Prototype text segments cannot be written.
259 register struct vnode *vp;
262 ASSERT_VOP_LOCKED(vp, "vn_writechk");
264 * If there's shared text associated with
265 * the vnode, try to free it up once. If
266 * we fail, we can't allow writing.
268 if (vp->v_vflag & VV_TEXT)
278 vn_close(vp, flags, file_cred, td)
279 register struct vnode *vp;
281 struct ucred *file_cred;
285 int error, lock_flags;
287 if (!(flags & FWRITE) && vp->v_mount != NULL &&
288 vp->v_mount->mnt_kern_flag & MNTK_EXTENDED_SHARED)
289 lock_flags = LK_SHARED;
291 lock_flags = LK_EXCLUSIVE;
293 VFS_ASSERT_GIANT(vp->v_mount);
295 vn_start_write(vp, &mp, V_WAIT);
296 vn_lock(vp, lock_flags | LK_RETRY);
297 if (flags & FWRITE) {
298 VNASSERT(vp->v_writecount > 0, vp,
299 ("vn_close: negative writecount"));
302 error = VOP_CLOSE(vp, flags, file_cred, td);
304 vn_finished_write(mp);
309 * Heuristic to detect sequential operation.
312 sequential_heuristic(struct uio *uio, struct file *fp)
315 if (atomic_load_acq_int(&(fp->f_flag)) & FRDAHEAD)
316 return (fp->f_seqcount << IO_SEQSHIFT);
319 * Offset 0 is handled specially. open() sets f_seqcount to 1 so
320 * that the first I/O is normally considered to be slightly
321 * sequential. Seeking to offset 0 doesn't change sequentiality
322 * unless previous seeks have reduced f_seqcount to 0, in which
323 * case offset 0 is not special.
325 if ((uio->uio_offset == 0 && fp->f_seqcount > 0) ||
326 uio->uio_offset == fp->f_nextoff) {
328 * f_seqcount is in units of fixed-size blocks so that it
329 * depends mainly on the amount of sequential I/O and not
330 * much on the number of sequential I/O's. The fixed size
331 * of 16384 is hard-coded here since it is (not quite) just
332 * a magic size that works well here. This size is more
333 * closely related to the best I/O size for real disks than
334 * to any block size used by software.
336 fp->f_seqcount += howmany(uio->uio_resid, 16384);
337 if (fp->f_seqcount > IO_SEQMAX)
338 fp->f_seqcount = IO_SEQMAX;
339 return (fp->f_seqcount << IO_SEQSHIFT);
342 /* Not sequential. Quickly draw-down sequentiality. */
343 if (fp->f_seqcount > 1)
351 * Package up an I/O request on a vnode into a uio and do it.
354 vn_rdwr(rw, vp, base, len, offset, segflg, ioflg, active_cred, file_cred,
363 struct ucred *active_cred;
364 struct ucred *file_cred;
372 int error, lock_flags;
374 VFS_ASSERT_GIANT(vp->v_mount);
376 if ((ioflg & IO_NODELOCKED) == 0) {
378 if (rw == UIO_WRITE) {
379 if (vp->v_type != VCHR &&
380 (error = vn_start_write(vp, &mp, V_WAIT | PCATCH))
383 if (MNT_SHARED_WRITES(mp) ||
384 ((mp == NULL) && MNT_SHARED_WRITES(vp->v_mount))) {
385 lock_flags = LK_SHARED;
387 lock_flags = LK_EXCLUSIVE;
389 vn_lock(vp, lock_flags | LK_RETRY);
391 vn_lock(vp, LK_SHARED | LK_RETRY);
394 ASSERT_VOP_LOCKED(vp, "IO_NODELOCKED with no vp lock held");
395 auio.uio_iov = &aiov;
397 aiov.iov_base = base;
399 auio.uio_resid = len;
400 auio.uio_offset = offset;
401 auio.uio_segflg = segflg;
406 if ((ioflg & IO_NOMACCHECK) == 0) {
408 error = mac_vnode_check_read(active_cred, file_cred,
411 error = mac_vnode_check_write(active_cred, file_cred,
421 error = VOP_READ(vp, &auio, ioflg, cred);
423 error = VOP_WRITE(vp, &auio, ioflg, cred);
426 *aresid = auio.uio_resid;
428 if (auio.uio_resid && error == 0)
430 if ((ioflg & IO_NODELOCKED) == 0) {
431 if (rw == UIO_WRITE && vp->v_type != VCHR)
432 vn_finished_write(mp);
439 * Package up an I/O request on a vnode into a uio and do it. The I/O
440 * request is split up into smaller chunks and we try to avoid saturating
441 * the buffer cache while potentially holding a vnode locked, so we
442 * check bwillwrite() before calling vn_rdwr(). We also call uio_yield()
443 * to give other processes a chance to lock the vnode (either other processes
444 * core'ing the same binary, or unrelated processes scanning the directory).
447 vn_rdwr_inchunks(rw, vp, base, len, offset, segflg, ioflg, active_cred,
448 file_cred, aresid, td)
456 struct ucred *active_cred;
457 struct ucred *file_cred;
464 VFS_ASSERT_GIANT(vp->v_mount);
470 * Force `offset' to a multiple of MAXBSIZE except possibly
471 * for the first chunk, so that filesystems only need to
472 * write full blocks except possibly for the first and last
475 chunk = MAXBSIZE - (uoff_t)offset % MAXBSIZE;
479 if (rw != UIO_READ && vp->v_type == VREG)
482 error = vn_rdwr(rw, vp, base, chunk, offset, segflg,
483 ioflg, active_cred, file_cred, &iaresid, td);
484 len -= chunk; /* aresid calc already includes length */
488 base = (char *)base + chunk;
492 *aresid = len + iaresid;
497 * File table vnode read routine.
500 vn_read(fp, uio, active_cred, flags, td)
503 struct ucred *active_cred;
512 KASSERT(uio->uio_td == td, ("uio_td %p is not td %p",
517 if (fp->f_flag & FNONBLOCK)
519 if (fp->f_flag & O_DIRECT)
521 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
523 * According to McKusick the vn lock was protecting f_offset here.
524 * It is now protected by the FOFFSET_LOCKED flag.
526 if ((flags & FOF_OFFSET) == 0) {
527 mtxp = mtx_pool_find(mtxpool_sleep, fp);
529 while(fp->f_vnread_flags & FOFFSET_LOCKED) {
530 fp->f_vnread_flags |= FOFFSET_LOCK_WAITING;
531 msleep(&fp->f_vnread_flags, mtxp, PUSER -1,
532 "vnread offlock", 0);
534 fp->f_vnread_flags |= FOFFSET_LOCKED;
536 vn_lock(vp, LK_SHARED | LK_RETRY);
537 uio->uio_offset = fp->f_offset;
539 vn_lock(vp, LK_SHARED | LK_RETRY);
541 ioflag |= sequential_heuristic(uio, fp);
544 error = mac_vnode_check_read(active_cred, fp->f_cred, vp);
547 error = VOP_READ(vp, uio, ioflag, fp->f_cred);
548 if ((flags & FOF_OFFSET) == 0) {
549 fp->f_offset = uio->uio_offset;
551 if (fp->f_vnread_flags & FOFFSET_LOCK_WAITING)
552 wakeup(&fp->f_vnread_flags);
553 fp->f_vnread_flags = 0;
556 fp->f_nextoff = uio->uio_offset;
558 VFS_UNLOCK_GIANT(vfslocked);
563 * File table vnode write routine.
566 vn_write(fp, uio, active_cred, flags, td)
569 struct ucred *active_cred;
575 int error, ioflag, lock_flags;
578 KASSERT(uio->uio_td == td, ("uio_td %p is not td %p",
581 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
582 if (vp->v_type == VREG)
585 if (vp->v_type == VREG && (fp->f_flag & O_APPEND))
587 if (fp->f_flag & FNONBLOCK)
589 if (fp->f_flag & O_DIRECT)
591 if ((fp->f_flag & O_FSYNC) ||
592 (vp->v_mount && (vp->v_mount->mnt_flag & MNT_SYNCHRONOUS)))
595 if (vp->v_type != VCHR &&
596 (error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
599 if ((MNT_SHARED_WRITES(mp) ||
600 ((mp == NULL) && MNT_SHARED_WRITES(vp->v_mount))) &&
601 (flags & FOF_OFFSET) != 0) {
602 lock_flags = LK_SHARED;
604 lock_flags = LK_EXCLUSIVE;
607 vn_lock(vp, lock_flags | LK_RETRY);
608 if ((flags & FOF_OFFSET) == 0)
609 uio->uio_offset = fp->f_offset;
610 ioflag |= sequential_heuristic(uio, fp);
612 error = mac_vnode_check_write(active_cred, fp->f_cred, vp);
615 error = VOP_WRITE(vp, uio, ioflag, fp->f_cred);
616 if ((flags & FOF_OFFSET) == 0)
617 fp->f_offset = uio->uio_offset;
618 fp->f_nextoff = uio->uio_offset;
620 if (vp->v_type != VCHR)
621 vn_finished_write(mp);
623 VFS_UNLOCK_GIANT(vfslocked);
628 * File table truncate routine.
631 vn_truncate(fp, length, active_cred, td)
634 struct ucred *active_cred;
644 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
645 error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
647 VFS_UNLOCK_GIANT(vfslocked);
650 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
651 if (vp->v_type == VDIR) {
656 error = mac_vnode_check_write(active_cred, fp->f_cred, vp);
660 error = vn_writechk(vp);
663 vattr.va_size = length;
664 error = VOP_SETATTR(vp, &vattr, fp->f_cred);
668 vn_finished_write(mp);
669 VFS_UNLOCK_GIANT(vfslocked);
674 * File table vnode stat routine.
677 vn_statfile(fp, sb, active_cred, td)
680 struct ucred *active_cred;
683 struct vnode *vp = fp->f_vnode;
687 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
688 vn_lock(vp, LK_SHARED | LK_RETRY);
689 error = vn_stat(vp, sb, active_cred, fp->f_cred, td);
691 VFS_UNLOCK_GIANT(vfslocked);
697 * Stat a vnode; implementation for the stat syscall
700 vn_stat(vp, sb, active_cred, file_cred, td)
702 register struct stat *sb;
703 struct ucred *active_cred;
704 struct ucred *file_cred;
708 register struct vattr *vap;
713 error = mac_vnode_check_stat(active_cred, file_cred, vp);
721 * Initialize defaults for new and unusual fields, so that file
722 * systems which don't support these fields don't need to know
725 vap->va_birthtime.tv_sec = -1;
726 vap->va_birthtime.tv_nsec = 0;
727 vap->va_fsid = VNOVAL;
728 vap->va_rdev = NODEV;
730 error = VOP_GETATTR(vp, vap, active_cred);
735 * Zero the spare stat fields
737 bzero(sb, sizeof *sb);
740 * Copy from vattr table
742 if (vap->va_fsid != VNOVAL)
743 sb->st_dev = vap->va_fsid;
745 sb->st_dev = vp->v_mount->mnt_stat.f_fsid.val[0];
746 sb->st_ino = vap->va_fileid;
748 switch (vap->va_type) {
774 sb->st_nlink = vap->va_nlink;
775 sb->st_uid = vap->va_uid;
776 sb->st_gid = vap->va_gid;
777 sb->st_rdev = vap->va_rdev;
778 if (vap->va_size > OFF_MAX)
780 sb->st_size = vap->va_size;
781 sb->st_atimespec = vap->va_atime;
782 sb->st_mtimespec = vap->va_mtime;
783 sb->st_ctimespec = vap->va_ctime;
784 sb->st_birthtimespec = vap->va_birthtime;
787 * According to www.opengroup.org, the meaning of st_blksize is
788 * "a filesystem-specific preferred I/O block size for this
789 * object. In some filesystem types, this may vary from file
791 * Use miminum/default of PAGE_SIZE (e.g. for VCHR).
794 sb->st_blksize = max(PAGE_SIZE, vap->va_blocksize);
796 sb->st_flags = vap->va_flags;
797 if (priv_check(td, PRIV_VFS_GENERATION))
800 sb->st_gen = vap->va_gen;
802 sb->st_blocks = vap->va_bytes / S_BLKSIZE;
807 * File table vnode ioctl routine.
810 vn_ioctl(fp, com, data, active_cred, td)
814 struct ucred *active_cred;
817 struct vnode *vp = fp->f_vnode;
822 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
824 switch (vp->v_type) {
827 if (com == FIONREAD) {
828 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
829 error = VOP_GETATTR(vp, &vattr, active_cred);
832 *(int *)data = vattr.va_size - fp->f_offset;
834 if (com == FIONBIO || com == FIOASYNC) /* XXX */
837 error = VOP_IOCTL(vp, com, data, fp->f_flag,
844 VFS_UNLOCK_GIANT(vfslocked);
849 * File table vnode poll routine.
852 vn_poll(fp, events, active_cred, td)
855 struct ucred *active_cred;
863 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
865 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
866 error = mac_vnode_check_poll(active_cred, fp->f_cred, vp);
871 error = VOP_POLL(vp, events, fp->f_cred, td);
872 VFS_UNLOCK_GIANT(vfslocked);
877 * Acquire the requested lock and then check for validity. LK_RETRY
878 * permits vn_lock to return doomed vnodes.
881 _vn_lock(struct vnode *vp, int flags, char *file, int line)
885 VNASSERT((flags & LK_TYPE_MASK) != 0, vp,
886 ("vn_lock called with no locktype."));
888 #ifdef DEBUG_VFS_LOCKS
889 KASSERT(vp->v_holdcnt != 0,
890 ("vn_lock %p: zero hold count", vp));
892 error = VOP_LOCK1(vp, flags, file, line);
893 flags &= ~LK_INTERLOCK; /* Interlock is always dropped. */
894 KASSERT((flags & LK_RETRY) == 0 || error == 0,
895 ("LK_RETRY set with incompatible flags (0x%x) or an error occured (%d)",
898 * Callers specify LK_RETRY if they wish to get dead vnodes.
899 * If RETRY is not set, we return ENOENT instead.
901 if (error == 0 && vp->v_iflag & VI_DOOMED &&
902 (flags & LK_RETRY) == 0) {
907 } while (flags & LK_RETRY && error != 0);
912 * File table vnode close routine.
926 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
927 if (fp->f_type == DTYPE_VNODE && fp->f_flag & FHASLOCK) {
928 lf.l_whence = SEEK_SET;
932 (void) VOP_ADVLOCK(vp, fp, F_UNLCK, &lf, F_FLOCK);
935 fp->f_ops = &badfileops;
937 error = vn_close(vp, fp->f_flag, fp->f_cred, td);
938 VFS_UNLOCK_GIANT(vfslocked);
943 * Preparing to start a filesystem write operation. If the operation is
944 * permitted, then we bump the count of operations in progress and
945 * proceed. If a suspend request is in progress, we wait until the
946 * suspension is over, and then proceed.
949 vn_start_write(vp, mpp, flags)
959 * If a vnode is provided, get and return the mount point that
960 * to which it will write.
963 if ((error = VOP_GETWRITEMOUNT(vp, mpp)) != 0) {
965 if (error != EOPNOTSUPP)
970 if ((mp = *mpp) == NULL)
974 * VOP_GETWRITEMOUNT() returns with the mp refcount held through
976 * As long as a vnode is not provided we need to acquire a
977 * refcount for the provided mountpoint too, in order to
978 * emulate a vfs_ref().
985 * Check on status of suspension.
987 if ((curthread->td_pflags & TDP_IGNSUSP) == 0 ||
988 mp->mnt_susp_owner != curthread) {
989 while ((mp->mnt_kern_flag & MNTK_SUSPEND) != 0) {
990 if (flags & V_NOWAIT) {
994 error = msleep(&mp->mnt_flag, MNT_MTX(mp),
995 (PUSER - 1) | (flags & PCATCH), "suspfs", 0);
1000 if (flags & V_XSLEEP)
1002 mp->mnt_writeopcount++;
1004 if (error != 0 || (flags & V_XSLEEP) != 0)
1011 * Secondary suspension. Used by operations such as vop_inactive
1012 * routines that are needed by the higher level functions. These
1013 * are allowed to proceed until all the higher level functions have
1014 * completed (indicated by mnt_writeopcount dropping to zero). At that
1015 * time, these operations are halted until the suspension is over.
1018 vn_start_secondary_write(vp, mpp, flags)
1028 if ((error = VOP_GETWRITEMOUNT(vp, mpp)) != 0) {
1030 if (error != EOPNOTSUPP)
1036 * If we are not suspended or have not yet reached suspended
1037 * mode, then let the operation proceed.
1039 if ((mp = *mpp) == NULL)
1043 * VOP_GETWRITEMOUNT() returns with the mp refcount held through
1045 * As long as a vnode is not provided we need to acquire a
1046 * refcount for the provided mountpoint too, in order to
1047 * emulate a vfs_ref().
1052 if ((mp->mnt_kern_flag & (MNTK_SUSPENDED | MNTK_SUSPEND2)) == 0) {
1053 mp->mnt_secondary_writes++;
1054 mp->mnt_secondary_accwrites++;
1058 if (flags & V_NOWAIT) {
1061 return (EWOULDBLOCK);
1064 * Wait for the suspension to finish.
1066 error = msleep(&mp->mnt_flag, MNT_MTX(mp),
1067 (PUSER - 1) | (flags & PCATCH) | PDROP, "suspfs", 0);
1075 * Filesystem write operation has completed. If we are suspending and this
1076 * operation is the last one, notify the suspender that the suspension is
1080 vn_finished_write(mp)
1087 mp->mnt_writeopcount--;
1088 if (mp->mnt_writeopcount < 0)
1089 panic("vn_finished_write: neg cnt");
1090 if ((mp->mnt_kern_flag & MNTK_SUSPEND) != 0 &&
1091 mp->mnt_writeopcount <= 0)
1092 wakeup(&mp->mnt_writeopcount);
1098 * Filesystem secondary write operation has completed. If we are
1099 * suspending and this operation is the last one, notify the suspender
1100 * that the suspension is now in effect.
1103 vn_finished_secondary_write(mp)
1110 mp->mnt_secondary_writes--;
1111 if (mp->mnt_secondary_writes < 0)
1112 panic("vn_finished_secondary_write: neg cnt");
1113 if ((mp->mnt_kern_flag & MNTK_SUSPEND) != 0 &&
1114 mp->mnt_secondary_writes <= 0)
1115 wakeup(&mp->mnt_secondary_writes);
1122 * Request a filesystem to suspend write operations.
1125 vfs_write_suspend(mp)
1131 if (mp->mnt_susp_owner == curthread) {
1135 while (mp->mnt_kern_flag & MNTK_SUSPEND)
1136 msleep(&mp->mnt_flag, MNT_MTX(mp), PUSER - 1, "wsuspfs", 0);
1137 mp->mnt_kern_flag |= MNTK_SUSPEND;
1138 mp->mnt_susp_owner = curthread;
1139 if (mp->mnt_writeopcount > 0)
1140 (void) msleep(&mp->mnt_writeopcount,
1141 MNT_MTX(mp), (PUSER - 1)|PDROP, "suspwt", 0);
1144 if ((error = VFS_SYNC(mp, MNT_SUSPEND)) != 0)
1145 vfs_write_resume(mp);
1150 * Request a filesystem to resume write operations.
1153 vfs_write_resume(mp)
1158 if ((mp->mnt_kern_flag & MNTK_SUSPEND) != 0) {
1159 KASSERT(mp->mnt_susp_owner == curthread, ("mnt_susp_owner"));
1160 mp->mnt_kern_flag &= ~(MNTK_SUSPEND | MNTK_SUSPEND2 |
1162 mp->mnt_susp_owner = NULL;
1163 wakeup(&mp->mnt_writeopcount);
1164 wakeup(&mp->mnt_flag);
1165 curthread->td_pflags &= ~TDP_IGNSUSP;
1173 * Implement kqueues for files by translating it to vnode operation.
1176 vn_kqfilter(struct file *fp, struct knote *kn)
1181 vfslocked = VFS_LOCK_GIANT(fp->f_vnode->v_mount);
1182 error = VOP_KQFILTER(fp->f_vnode, kn);
1183 VFS_UNLOCK_GIANT(vfslocked);
1189 * Simplified in-kernel wrapper calls for extended attribute access.
1190 * Both calls pass in a NULL credential, authorizing as "kernel" access.
1191 * Set IO_NODELOCKED in ioflg if the vnode is already locked.
1194 vn_extattr_get(struct vnode *vp, int ioflg, int attrnamespace,
1195 const char *attrname, int *buflen, char *buf, struct thread *td)
1201 iov.iov_len = *buflen;
1204 auio.uio_iov = &iov;
1205 auio.uio_iovcnt = 1;
1206 auio.uio_rw = UIO_READ;
1207 auio.uio_segflg = UIO_SYSSPACE;
1209 auio.uio_offset = 0;
1210 auio.uio_resid = *buflen;
1212 if ((ioflg & IO_NODELOCKED) == 0)
1213 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1215 ASSERT_VOP_LOCKED(vp, "IO_NODELOCKED with no vp lock held");
1217 /* authorize attribute retrieval as kernel */
1218 error = VOP_GETEXTATTR(vp, attrnamespace, attrname, &auio, NULL, NULL,
1221 if ((ioflg & IO_NODELOCKED) == 0)
1225 *buflen = *buflen - auio.uio_resid;
1232 * XXX failure mode if partially written?
1235 vn_extattr_set(struct vnode *vp, int ioflg, int attrnamespace,
1236 const char *attrname, int buflen, char *buf, struct thread *td)
1243 iov.iov_len = buflen;
1246 auio.uio_iov = &iov;
1247 auio.uio_iovcnt = 1;
1248 auio.uio_rw = UIO_WRITE;
1249 auio.uio_segflg = UIO_SYSSPACE;
1251 auio.uio_offset = 0;
1252 auio.uio_resid = buflen;
1254 if ((ioflg & IO_NODELOCKED) == 0) {
1255 if ((error = vn_start_write(vp, &mp, V_WAIT)) != 0)
1257 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1260 ASSERT_VOP_LOCKED(vp, "IO_NODELOCKED with no vp lock held");
1262 /* authorize attribute setting as kernel */
1263 error = VOP_SETEXTATTR(vp, attrnamespace, attrname, &auio, NULL, td);
1265 if ((ioflg & IO_NODELOCKED) == 0) {
1266 vn_finished_write(mp);
1274 vn_extattr_rm(struct vnode *vp, int ioflg, int attrnamespace,
1275 const char *attrname, struct thread *td)
1280 if ((ioflg & IO_NODELOCKED) == 0) {
1281 if ((error = vn_start_write(vp, &mp, V_WAIT)) != 0)
1283 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1286 ASSERT_VOP_LOCKED(vp, "IO_NODELOCKED with no vp lock held");
1288 /* authorize attribute removal as kernel */
1289 error = VOP_DELETEEXTATTR(vp, attrnamespace, attrname, NULL, td);
1290 if (error == EOPNOTSUPP)
1291 error = VOP_SETEXTATTR(vp, attrnamespace, attrname, NULL,
1294 if ((ioflg & IO_NODELOCKED) == 0) {
1295 vn_finished_write(mp);
1303 vn_vget_ino(struct vnode *vp, ino_t ino, int lkflags, struct vnode **rvp)
1309 ltype = VOP_ISLOCKED(vp);
1310 KASSERT(ltype == LK_EXCLUSIVE || ltype == LK_SHARED,
1311 ("vn_vget_ino: vp not locked"));
1312 error = vfs_busy(mp, MBF_NOWAIT);
1316 error = vfs_busy(mp, 0);
1317 vn_lock(vp, ltype | LK_RETRY);
1321 if (vp->v_iflag & VI_DOOMED) {
1327 error = VFS_VGET(mp, ino, lkflags, rvp);
1329 vn_lock(vp, ltype | LK_RETRY);
1330 if (vp->v_iflag & VI_DOOMED) {