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$");
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/fcntl.h>
50 #include <sys/limits.h>
52 #include <sys/mount.h>
53 #include <sys/mutex.h>
54 #include <sys/namei.h>
55 #include <sys/vnode.h>
58 #include <sys/filio.h>
60 #include <sys/ttycom.h>
62 #include <sys/syslog.h>
63 #include <sys/unistd.h>
65 #include <security/mac/mac_framework.h>
67 static fo_rdwr_t vn_read;
68 static fo_rdwr_t vn_write;
69 static fo_ioctl_t vn_ioctl;
70 static fo_poll_t vn_poll;
71 static fo_kqfilter_t vn_kqfilter;
72 static fo_stat_t vn_statfile;
73 static fo_close_t vn_closefile;
75 struct fileops vnops = {
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, 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(ndp, flagp, cmode, cred, fp)
106 struct nameidata *ndp;
113 struct thread *td = ndp->ni_cnd.cn_thread;
115 struct vattr *vap = &vat;
116 int mode, fmode, error;
117 int vfslocked, mpsafe;
119 mpsafe = ndp->ni_cnd.cn_flags & MPSAFE;
123 if (fmode & O_CREAT) {
124 ndp->ni_cnd.cn_nameiop = CREATE;
125 ndp->ni_cnd.cn_flags = ISOPEN | LOCKPARENT | LOCKLEAF |
126 MPSAFE | AUDITVNODE1;
127 if ((fmode & O_EXCL) == 0 && (fmode & O_NOFOLLOW) == 0)
128 ndp->ni_cnd.cn_flags |= FOLLOW;
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_check_vnode_create(cred, ndp->ni_dvp,
155 VOP_LEASE(ndp->ni_dvp, td, cred, LEASE_WRITE);
156 error = VOP_CREATE(ndp->ni_dvp, &ndp->ni_vp,
162 vn_finished_write(mp);
164 VFS_UNLOCK_GIANT(vfslocked);
165 NDFREE(ndp, NDF_ONLY_PNBUF);
171 if (ndp->ni_dvp == ndp->ni_vp)
177 if (fmode & O_EXCL) {
184 ndp->ni_cnd.cn_nameiop = LOOKUP;
185 ndp->ni_cnd.cn_flags = ISOPEN |
186 ((fmode & O_NOFOLLOW) ? NOFOLLOW : FOLLOW) |
187 LOCKLEAF | MPSAFE | 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) {
213 if (fmode & O_APPEND)
216 error = mac_check_vnode_open(cred, vp, mode);
220 if ((fmode & O_CREAT) == 0) {
222 error = vn_writechk(vp);
227 error = VOP_ACCESS(vp, mode, cred, td);
232 if ((error = VOP_OPEN(vp, fmode, cred, td, fp)) != 0)
238 ASSERT_VOP_ELOCKED(vp, "vn_open_cred");
240 VFS_UNLOCK_GIANT(vfslocked);
243 NDFREE(ndp, NDF_ONLY_PNBUF);
245 VFS_UNLOCK_GIANT(vfslocked);
252 * Check for write permissions on the specified vnode.
253 * Prototype text segments cannot be written.
257 register struct vnode *vp;
260 ASSERT_VOP_LOCKED(vp, "vn_writechk");
262 * If there's shared text associated with
263 * the vnode, try to free it up once. If
264 * we fail, we can't allow writing.
266 if (vp->v_vflag & VV_TEXT)
276 vn_close(vp, flags, file_cred, td)
277 register struct vnode *vp;
279 struct ucred *file_cred;
285 VFS_ASSERT_GIANT(vp->v_mount);
287 vn_start_write(vp, &mp, V_WAIT);
288 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
289 if (flags & FWRITE) {
290 VNASSERT(vp->v_writecount > 0, vp,
291 ("vn_close: negative writecount"));
294 error = VOP_CLOSE(vp, flags, file_cred, td);
296 vn_finished_write(mp);
301 * Sequential heuristic - detect sequential operation
305 sequential_heuristic(struct uio *uio, struct file *fp)
308 if ((uio->uio_offset == 0 && fp->f_seqcount > 0) ||
309 uio->uio_offset == fp->f_nextoff) {
311 * XXX we assume that the filesystem block size is
312 * the default. Not true, but still gives us a pretty
313 * good indicator of how sequential the read operations
316 fp->f_seqcount += (uio->uio_resid + BKVASIZE - 1) / BKVASIZE;
317 if (fp->f_seqcount > IO_SEQMAX)
318 fp->f_seqcount = IO_SEQMAX;
319 return(fp->f_seqcount << IO_SEQSHIFT);
323 * Not sequential, quick draw-down of seqcount
325 if (fp->f_seqcount > 1)
333 * Package up an I/O request on a vnode into a uio and do it.
336 vn_rdwr(rw, vp, base, len, offset, segflg, ioflg, active_cred, file_cred,
345 struct ucred *active_cred;
346 struct ucred *file_cred;
356 VFS_ASSERT_GIANT(vp->v_mount);
358 if ((ioflg & IO_NODELOCKED) == 0) {
360 if (rw == UIO_WRITE) {
361 if (vp->v_type != VCHR &&
362 (error = vn_start_write(vp, &mp, V_WAIT | PCATCH))
365 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
368 * XXX This should be LK_SHARED but I don't trust VFS
369 * enough to leave it like that until it has been
372 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
376 ASSERT_VOP_LOCKED(vp, "IO_NODELOCKED with no vp lock held");
377 auio.uio_iov = &aiov;
379 aiov.iov_base = base;
381 auio.uio_resid = len;
382 auio.uio_offset = offset;
383 auio.uio_segflg = segflg;
388 if ((ioflg & IO_NOMACCHECK) == 0) {
390 error = mac_check_vnode_read(active_cred, file_cred,
393 error = mac_check_vnode_write(active_cred, file_cred,
403 error = VOP_READ(vp, &auio, ioflg, cred);
405 error = VOP_WRITE(vp, &auio, ioflg, cred);
408 *aresid = auio.uio_resid;
410 if (auio.uio_resid && error == 0)
412 if ((ioflg & IO_NODELOCKED) == 0) {
413 if (rw == UIO_WRITE && vp->v_type != VCHR)
414 vn_finished_write(mp);
415 VOP_UNLOCK(vp, 0, td);
421 * Package up an I/O request on a vnode into a uio and do it. The I/O
422 * request is split up into smaller chunks and we try to avoid saturating
423 * the buffer cache while potentially holding a vnode locked, so we
424 * check bwillwrite() before calling vn_rdwr(). We also call uio_yield()
425 * to give other processes a chance to lock the vnode (either other processes
426 * core'ing the same binary, or unrelated processes scanning the directory).
429 vn_rdwr_inchunks(rw, vp, base, len, offset, segflg, ioflg, active_cred,
430 file_cred, aresid, td)
438 struct ucred *active_cred;
439 struct ucred *file_cred;
446 VFS_ASSERT_GIANT(vp->v_mount);
452 * Force `offset' to a multiple of MAXBSIZE except possibly
453 * for the first chunk, so that filesystems only need to
454 * write full blocks except possibly for the first and last
457 chunk = MAXBSIZE - (uoff_t)offset % MAXBSIZE;
461 if (rw != UIO_READ && vp->v_type == VREG)
464 error = vn_rdwr(rw, vp, base, chunk, offset, segflg,
465 ioflg, active_cred, file_cred, &iaresid, td);
466 len -= chunk; /* aresid calc already includes length */
470 base = (char *)base + chunk;
474 *aresid = len + iaresid;
479 * File table vnode read routine.
482 vn_read(fp, uio, active_cred, flags, td)
485 struct ucred *active_cred;
493 KASSERT(uio->uio_td == td, ("uio_td %p is not td %p",
497 if (fp->f_flag & FNONBLOCK)
499 if (fp->f_flag & O_DIRECT)
501 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
502 VOP_LEASE(vp, td, fp->f_cred, LEASE_READ);
504 * According to McKusick the vn lock was protecting f_offset here.
505 * It is now protected by the FOFFSET_LOCKED flag.
507 if ((flags & FOF_OFFSET) == 0) {
509 while(fp->f_vnread_flags & FOFFSET_LOCKED) {
510 fp->f_vnread_flags |= FOFFSET_LOCK_WAITING;
511 msleep(&fp->f_vnread_flags,fp->f_mtxp,PUSER -1,"vnread offlock",0);
513 fp->f_vnread_flags |= FOFFSET_LOCKED;
515 vn_lock(vp, LK_SHARED | LK_RETRY, td);
516 uio->uio_offset = fp->f_offset;
518 vn_lock(vp, LK_SHARED | LK_RETRY, td);
520 ioflag |= sequential_heuristic(uio, fp);
523 error = mac_check_vnode_read(active_cred, fp->f_cred, vp);
526 error = VOP_READ(vp, uio, ioflag, fp->f_cred);
527 if ((flags & FOF_OFFSET) == 0) {
528 fp->f_offset = uio->uio_offset;
530 if (fp->f_vnread_flags & FOFFSET_LOCK_WAITING)
531 wakeup(&fp->f_vnread_flags);
532 fp->f_vnread_flags = 0;
535 fp->f_nextoff = uio->uio_offset;
536 VOP_UNLOCK(vp, 0, td);
537 VFS_UNLOCK_GIANT(vfslocked);
542 * File table vnode write routine.
545 vn_write(fp, uio, active_cred, flags, td)
548 struct ucred *active_cred;
557 KASSERT(uio->uio_td == td, ("uio_td %p is not td %p",
560 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
561 if (vp->v_type == VREG)
564 if (vp->v_type == VREG && (fp->f_flag & O_APPEND))
566 if (fp->f_flag & FNONBLOCK)
568 if (fp->f_flag & O_DIRECT)
570 if ((fp->f_flag & O_FSYNC) ||
571 (vp->v_mount && (vp->v_mount->mnt_flag & MNT_SYNCHRONOUS)))
574 if (vp->v_type != VCHR &&
575 (error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
577 VOP_LEASE(vp, td, fp->f_cred, LEASE_WRITE);
578 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
579 if ((flags & FOF_OFFSET) == 0)
580 uio->uio_offset = fp->f_offset;
581 ioflag |= sequential_heuristic(uio, fp);
583 error = mac_check_vnode_write(active_cred, fp->f_cred, vp);
586 error = VOP_WRITE(vp, uio, ioflag, fp->f_cred);
587 if ((flags & FOF_OFFSET) == 0)
588 fp->f_offset = uio->uio_offset;
589 fp->f_nextoff = uio->uio_offset;
590 VOP_UNLOCK(vp, 0, td);
591 if (vp->v_type != VCHR)
592 vn_finished_write(mp);
594 VFS_UNLOCK_GIANT(vfslocked);
599 * File table vnode stat routine.
602 vn_statfile(fp, sb, active_cred, td)
605 struct ucred *active_cred;
608 struct vnode *vp = fp->f_vnode;
612 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
613 vn_lock(vp, LK_SHARED | LK_RETRY, td);
614 error = vn_stat(vp, sb, active_cred, fp->f_cred, td);
615 VOP_UNLOCK(vp, 0, td);
616 VFS_UNLOCK_GIANT(vfslocked);
622 * Stat a vnode; implementation for the stat syscall
625 vn_stat(vp, sb, active_cred, file_cred, td)
627 register struct stat *sb;
628 struct ucred *active_cred;
629 struct ucred *file_cred;
633 register struct vattr *vap;
638 error = mac_check_vnode_stat(active_cred, file_cred, vp);
646 * Initialize defaults for new and unusual fields, so that file
647 * systems which don't support these fields don't need to know
650 vap->va_birthtime.tv_sec = -1;
651 vap->va_birthtime.tv_nsec = 0;
652 vap->va_fsid = VNOVAL;
653 vap->va_rdev = NODEV;
655 error = VOP_GETATTR(vp, vap, active_cred, td);
660 * Zero the spare stat fields
662 bzero(sb, sizeof *sb);
665 * Copy from vattr table
667 if (vap->va_fsid != VNOVAL)
668 sb->st_dev = vap->va_fsid;
670 sb->st_dev = vp->v_mount->mnt_stat.f_fsid.val[0];
671 sb->st_ino = vap->va_fileid;
673 switch (vap->va_type) {
699 sb->st_nlink = vap->va_nlink;
700 sb->st_uid = vap->va_uid;
701 sb->st_gid = vap->va_gid;
702 sb->st_rdev = vap->va_rdev;
703 if (vap->va_size > OFF_MAX)
705 sb->st_size = vap->va_size;
706 sb->st_atimespec = vap->va_atime;
707 sb->st_mtimespec = vap->va_mtime;
708 sb->st_ctimespec = vap->va_ctime;
709 sb->st_birthtimespec = vap->va_birthtime;
712 * According to www.opengroup.org, the meaning of st_blksize is
713 * "a filesystem-specific preferred I/O block size for this
714 * object. In some filesystem types, this may vary from file
716 * Default to PAGE_SIZE after much discussion.
717 * XXX: min(PAGE_SIZE, vp->v_bufobj.bo_bsize) may be more correct.
720 sb->st_blksize = PAGE_SIZE;
722 sb->st_flags = vap->va_flags;
723 if (priv_check(td, PRIV_VFS_GENERATION))
726 sb->st_gen = vap->va_gen;
728 sb->st_blocks = vap->va_bytes / S_BLKSIZE;
733 * File table vnode ioctl routine.
736 vn_ioctl(fp, com, data, active_cred, td)
740 struct ucred *active_cred;
743 struct vnode *vp = fp->f_vnode;
748 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
750 switch (vp->v_type) {
753 if (com == FIONREAD) {
754 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
755 error = VOP_GETATTR(vp, &vattr, active_cred, td);
756 VOP_UNLOCK(vp, 0, td);
758 *(int *)data = vattr.va_size - fp->f_offset;
760 if (com == FIONBIO || com == FIOASYNC) /* XXX */
763 error = VOP_IOCTL(vp, com, data, fp->f_flag,
770 VFS_UNLOCK_GIANT(vfslocked);
775 * File table vnode poll routine.
778 vn_poll(fp, events, active_cred, td)
781 struct ucred *active_cred;
789 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
791 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
792 error = mac_check_vnode_poll(active_cred, fp->f_cred, vp);
793 VOP_UNLOCK(vp, 0, td);
797 error = VOP_POLL(vp, events, fp->f_cred, td);
798 VFS_UNLOCK_GIANT(vfslocked);
803 * Check that the vnode is still valid, and if so
804 * acquire requested lock.
807 _vn_lock(struct vnode *vp, int flags, struct thread *td, char *file, int line)
812 if ((flags & LK_INTERLOCK) == 0)
814 #ifdef DEBUG_VFS_LOCKS
815 KASSERT(vp->v_holdcnt != 0,
816 ("vn_lock %p: zero hold count", vp));
818 if ((flags & LK_NOWAIT || (flags & LK_TYPE_MASK) == 0) &&
819 vp->v_iflag & VI_DOOMED) {
824 * Just polling to check validity.
826 if ((flags & LK_TYPE_MASK) == 0) {
831 * lockmgr drops interlock before it will return for
832 * any reason. So force the code above to relock it.
834 error = VOP_LOCK1(vp, flags | LK_INTERLOCK, td, file, line);
835 flags &= ~LK_INTERLOCK;
836 KASSERT((flags & LK_RETRY) == 0 || error == 0,
837 ("LK_RETRY set with incompatible flags %d\n", flags));
839 * Callers specify LK_RETRY if they wish to get dead vnodes.
840 * If RETRY is not set, we return ENOENT instead.
842 if (error == 0 && vp->v_iflag & VI_DOOMED &&
843 (flags & LK_RETRY) == 0) {
844 VOP_UNLOCK(vp, 0, td);
848 } while (flags & LK_RETRY && error != 0);
853 * File table vnode close routine.
867 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
868 if (fp->f_type == DTYPE_VNODE && fp->f_flag & FHASLOCK) {
869 lf.l_whence = SEEK_SET;
873 (void) VOP_ADVLOCK(vp, fp, F_UNLCK, &lf, F_FLOCK);
876 fp->f_ops = &badfileops;
878 error = vn_close(vp, fp->f_flag, fp->f_cred, td);
879 VFS_UNLOCK_GIANT(vfslocked);
884 * Preparing to start a filesystem write operation. If the operation is
885 * permitted, then we bump the count of operations in progress and
886 * proceed. If a suspend request is in progress, we wait until the
887 * suspension is over, and then proceed.
890 vn_start_write(vp, mpp, flags)
900 * If a vnode is provided, get and return the mount point that
901 * to which it will write.
904 if ((error = VOP_GETWRITEMOUNT(vp, mpp)) != 0) {
906 if (error != EOPNOTSUPP)
911 if ((mp = *mpp) == NULL)
917 * Check on status of suspension.
919 if ((curthread->td_pflags & TDP_IGNSUSP) == 0 ||
920 mp->mnt_susp_owner != curthread) {
921 while ((mp->mnt_kern_flag & MNTK_SUSPEND) != 0) {
922 if (flags & V_NOWAIT) {
926 error = msleep(&mp->mnt_flag, MNT_MTX(mp),
927 (PUSER - 1) | (flags & PCATCH), "suspfs", 0);
932 if (flags & V_XSLEEP)
934 mp->mnt_writeopcount++;
942 * Secondary suspension. Used by operations such as vop_inactive
943 * routines that are needed by the higher level functions. These
944 * are allowed to proceed until all the higher level functions have
945 * completed (indicated by mnt_writeopcount dropping to zero). At that
946 * time, these operations are halted until the suspension is over.
949 vn_start_secondary_write(vp, mpp, flags)
959 if ((error = VOP_GETWRITEMOUNT(vp, mpp)) != 0) {
961 if (error != EOPNOTSUPP)
967 * If we are not suspended or have not yet reached suspended
968 * mode, then let the operation proceed.
970 if ((mp = *mpp) == NULL)
975 if ((mp->mnt_kern_flag & (MNTK_SUSPENDED | MNTK_SUSPEND2)) == 0) {
976 mp->mnt_secondary_writes++;
977 mp->mnt_secondary_accwrites++;
982 if (flags & V_NOWAIT) {
985 return (EWOULDBLOCK);
988 * Wait for the suspension to finish.
990 error = msleep(&mp->mnt_flag, MNT_MTX(mp),
991 (PUSER - 1) | (flags & PCATCH) | PDROP, "suspfs", 0);
999 * Filesystem write operation has completed. If we are suspending and this
1000 * operation is the last one, notify the suspender that the suspension is
1004 vn_finished_write(mp)
1010 mp->mnt_writeopcount--;
1011 if (mp->mnt_writeopcount < 0)
1012 panic("vn_finished_write: neg cnt");
1013 if ((mp->mnt_kern_flag & MNTK_SUSPEND) != 0 &&
1014 mp->mnt_writeopcount <= 0)
1015 wakeup(&mp->mnt_writeopcount);
1021 * Filesystem secondary write operation has completed. If we are
1022 * suspending and this operation is the last one, notify the suspender
1023 * that the suspension is now in effect.
1026 vn_finished_secondary_write(mp)
1032 mp->mnt_secondary_writes--;
1033 if (mp->mnt_secondary_writes < 0)
1034 panic("vn_finished_secondary_write: neg cnt");
1035 if ((mp->mnt_kern_flag & MNTK_SUSPEND) != 0 &&
1036 mp->mnt_secondary_writes <= 0)
1037 wakeup(&mp->mnt_secondary_writes);
1044 * Request a filesystem to suspend write operations.
1047 vfs_write_suspend(mp)
1050 struct thread *td = curthread;
1054 if (mp->mnt_susp_owner == curthread) {
1058 while (mp->mnt_kern_flag & MNTK_SUSPEND)
1059 msleep(&mp->mnt_flag, MNT_MTX(mp), PUSER - 1, "wsuspfs", 0);
1060 mp->mnt_kern_flag |= MNTK_SUSPEND;
1061 mp->mnt_susp_owner = curthread;
1062 if (mp->mnt_writeopcount > 0)
1063 (void) msleep(&mp->mnt_writeopcount,
1064 MNT_MTX(mp), (PUSER - 1)|PDROP, "suspwt", 0);
1067 if ((error = VFS_SYNC(mp, MNT_SUSPEND, td)) != 0)
1068 vfs_write_resume(mp);
1073 * Request a filesystem to resume write operations.
1076 vfs_write_resume(mp)
1081 if ((mp->mnt_kern_flag & MNTK_SUSPEND) != 0) {
1082 KASSERT(mp->mnt_susp_owner == curthread, ("mnt_susp_owner"));
1083 mp->mnt_kern_flag &= ~(MNTK_SUSPEND | MNTK_SUSPEND2 |
1085 mp->mnt_susp_owner = NULL;
1086 wakeup(&mp->mnt_writeopcount);
1087 wakeup(&mp->mnt_flag);
1088 curthread->td_pflags &= ~TDP_IGNSUSP;
1096 * Implement kqueues for files by translating it to vnode operation.
1099 vn_kqfilter(struct file *fp, struct knote *kn)
1104 vfslocked = VFS_LOCK_GIANT(fp->f_vnode->v_mount);
1105 error = VOP_KQFILTER(fp->f_vnode, kn);
1106 VFS_UNLOCK_GIANT(vfslocked);
1112 * Simplified in-kernel wrapper calls for extended attribute access.
1113 * Both calls pass in a NULL credential, authorizing as "kernel" access.
1114 * Set IO_NODELOCKED in ioflg if the vnode is already locked.
1117 vn_extattr_get(struct vnode *vp, int ioflg, int attrnamespace,
1118 const char *attrname, int *buflen, char *buf, struct thread *td)
1124 iov.iov_len = *buflen;
1127 auio.uio_iov = &iov;
1128 auio.uio_iovcnt = 1;
1129 auio.uio_rw = UIO_READ;
1130 auio.uio_segflg = UIO_SYSSPACE;
1132 auio.uio_offset = 0;
1133 auio.uio_resid = *buflen;
1135 if ((ioflg & IO_NODELOCKED) == 0)
1136 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
1138 ASSERT_VOP_LOCKED(vp, "IO_NODELOCKED with no vp lock held");
1140 /* authorize attribute retrieval as kernel */
1141 error = VOP_GETEXTATTR(vp, attrnamespace, attrname, &auio, NULL, NULL,
1144 if ((ioflg & IO_NODELOCKED) == 0)
1145 VOP_UNLOCK(vp, 0, td);
1148 *buflen = *buflen - auio.uio_resid;
1155 * XXX failure mode if partially written?
1158 vn_extattr_set(struct vnode *vp, int ioflg, int attrnamespace,
1159 const char *attrname, int buflen, char *buf, struct thread *td)
1166 iov.iov_len = buflen;
1169 auio.uio_iov = &iov;
1170 auio.uio_iovcnt = 1;
1171 auio.uio_rw = UIO_WRITE;
1172 auio.uio_segflg = UIO_SYSSPACE;
1174 auio.uio_offset = 0;
1175 auio.uio_resid = buflen;
1177 if ((ioflg & IO_NODELOCKED) == 0) {
1178 if ((error = vn_start_write(vp, &mp, V_WAIT)) != 0)
1180 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
1183 ASSERT_VOP_LOCKED(vp, "IO_NODELOCKED with no vp lock held");
1185 /* authorize attribute setting as kernel */
1186 error = VOP_SETEXTATTR(vp, attrnamespace, attrname, &auio, NULL, td);
1188 if ((ioflg & IO_NODELOCKED) == 0) {
1189 vn_finished_write(mp);
1190 VOP_UNLOCK(vp, 0, td);
1197 vn_extattr_rm(struct vnode *vp, int ioflg, int attrnamespace,
1198 const char *attrname, struct thread *td)
1203 if ((ioflg & IO_NODELOCKED) == 0) {
1204 if ((error = vn_start_write(vp, &mp, V_WAIT)) != 0)
1206 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
1209 ASSERT_VOP_LOCKED(vp, "IO_NODELOCKED with no vp lock held");
1211 /* authorize attribute removal as kernel */
1212 error = VOP_DELETEEXTATTR(vp, attrnamespace, attrname, NULL, td);
1213 if (error == EOPNOTSUPP)
1214 error = VOP_SETEXTATTR(vp, attrnamespace, attrname, NULL,
1217 if ((ioflg & IO_NODELOCKED) == 0) {
1218 vn_finished_write(mp);
1219 VOP_UNLOCK(vp, 0, td);
1226 vn_vget_ino(struct vnode *vp, ino_t ino, int lkflags, struct vnode **rvp)
1232 ltype = VOP_ISLOCKED(vp, curthread);
1233 KASSERT(ltype == LK_EXCLUSIVE || ltype == LK_SHARED,
1234 ("vn_vget_ino: vp not locked"));
1236 error = vfs_busy(mp, LK_NOWAIT, NULL, curthread);
1239 VOP_UNLOCK(vp, 0, curthread);
1240 pause("vn_vget", 1);
1241 vn_lock(vp, ltype | LK_RETRY, curthread);
1242 if (vp->v_iflag & VI_DOOMED)
1245 VOP_UNLOCK(vp, 0, curthread);
1246 error = VFS_VGET(mp, ino, lkflags, rvp);
1247 vfs_unbusy(mp, curthread);
1248 vn_lock(vp, ltype | LK_RETRY, curthread);
1249 if (vp->v_iflag & VI_DOOMED) {