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>
49 #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 static fo_rdwr_t vn_read;
66 static fo_rdwr_t vn_write;
67 static fo_ioctl_t vn_ioctl;
68 static fo_poll_t vn_poll;
69 static fo_kqfilter_t vn_kqfilter;
70 static fo_stat_t vn_statfile;
71 static fo_close_t vn_closefile;
73 struct fileops vnops = {
78 .fo_kqfilter = vn_kqfilter,
79 .fo_stat = vn_statfile,
80 .fo_close = vn_closefile,
81 .fo_flags = DFLAG_PASSABLE | DFLAG_SEEKABLE
85 vn_open(ndp, flagp, cmode, fdidx)
86 struct nameidata *ndp;
87 int *flagp, cmode, fdidx;
89 struct thread *td = ndp->ni_cnd.cn_thread;
91 return (vn_open_cred(ndp, flagp, cmode, td->td_ucred, fdidx));
95 * Common code for vnode open operations.
96 * Check permissions, and call the VOP_OPEN or VOP_CREATE routine.
98 * Note that this does NOT free nameidata for the successful case,
99 * due to the NDINIT being done elsewhere.
102 vn_open_cred(ndp, flagp, cmode, cred, fdidx)
103 struct nameidata *ndp;
110 struct thread *td = ndp->ni_cnd.cn_thread;
112 struct vattr *vap = &vat;
113 int mode, fmode, error;
119 if (fmode & O_CREAT) {
120 ndp->ni_cnd.cn_nameiop = CREATE;
121 ndp->ni_cnd.cn_flags = ISOPEN | LOCKPARENT | LOCKLEAF | MPSAFE;
122 if ((fmode & O_EXCL) == 0 && (fmode & O_NOFOLLOW) == 0)
123 ndp->ni_cnd.cn_flags |= FOLLOW;
125 if ((error = namei(ndp)) != 0)
127 vfslocked = (ndp->ni_cnd.cn_flags & GIANTHELD) != 0;
128 ndp->ni_cnd.cn_flags &= ~MPSAFE;
129 if (ndp->ni_vp == NULL) {
132 vap->va_mode = cmode;
134 vap->va_vaflags |= VA_EXCLUSIVE;
135 if (vn_start_write(ndp->ni_dvp, &mp, V_NOWAIT) != 0) {
136 NDFREE(ndp, NDF_ONLY_PNBUF);
138 VFS_UNLOCK_GIANT(vfslocked);
139 if ((error = vn_start_write(NULL, &mp,
140 V_XSLEEP | PCATCH)) != 0)
145 error = mac_check_vnode_create(cred, ndp->ni_dvp,
149 VOP_LEASE(ndp->ni_dvp, td, cred, LEASE_WRITE);
150 error = VOP_CREATE(ndp->ni_dvp, &ndp->ni_vp,
156 vn_finished_write(mp);
158 VFS_UNLOCK_GIANT(vfslocked);
159 NDFREE(ndp, NDF_ONLY_PNBUF);
165 if (ndp->ni_dvp == ndp->ni_vp)
171 if (fmode & O_EXCL) {
178 ndp->ni_cnd.cn_nameiop = LOOKUP;
179 ndp->ni_cnd.cn_flags = ISOPEN |
180 ((fmode & O_NOFOLLOW) ? NOFOLLOW : FOLLOW) |
181 LOCKSHARED | LOCKLEAF | MPSAFE;
182 if ((error = namei(ndp)) != 0)
184 ndp->ni_cnd.cn_flags &= ~MPSAFE;
185 vfslocked = (ndp->ni_cnd.cn_flags & GIANTHELD) != 0;
188 if (vp->v_type == VLNK) {
192 if (vp->v_type == VSOCK) {
197 if (fmode & (FWRITE | O_TRUNC)) {
198 if (vp->v_type == VDIR) {
206 if (fmode & O_APPEND)
209 error = mac_check_vnode_open(cred, vp, mode);
213 if ((fmode & O_CREAT) == 0) {
215 error = vn_writechk(vp);
220 error = VOP_ACCESS(vp, mode, cred, td);
225 if ((error = VOP_OPEN(vp, fmode, cred, td, fdidx)) != 0)
231 ASSERT_VOP_LOCKED(vp, "vn_open_cred");
233 VFS_UNLOCK_GIANT(vfslocked);
236 NDFREE(ndp, NDF_ONLY_PNBUF);
238 VFS_UNLOCK_GIANT(vfslocked);
245 * Check for write permissions on the specified vnode.
246 * Prototype text segments cannot be written.
250 register struct vnode *vp;
253 ASSERT_VOP_LOCKED(vp, "vn_writechk");
255 * If there's shared text associated with
256 * the vnode, try to free it up once. If
257 * we fail, we can't allow writing.
259 if (vp->v_vflag & VV_TEXT)
269 vn_close(vp, flags, file_cred, td)
270 register struct vnode *vp;
272 struct ucred *file_cred;
278 VFS_ASSERT_GIANT(vp->v_mount);
280 vn_start_write(vp, &mp, V_WAIT);
281 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
284 error = VOP_CLOSE(vp, flags, file_cred, td);
286 vn_finished_write(mp);
291 * Sequential heuristic - detect sequential operation
295 sequential_heuristic(struct uio *uio, struct file *fp)
298 if ((uio->uio_offset == 0 && fp->f_seqcount > 0) ||
299 uio->uio_offset == fp->f_nextoff) {
301 * XXX we assume that the filesystem block size is
302 * the default. Not true, but still gives us a pretty
303 * good indicator of how sequential the read operations
306 fp->f_seqcount += (uio->uio_resid + BKVASIZE - 1) / BKVASIZE;
307 if (fp->f_seqcount > IO_SEQMAX)
308 fp->f_seqcount = IO_SEQMAX;
309 return(fp->f_seqcount << IO_SEQSHIFT);
313 * Not sequential, quick draw-down of seqcount
315 if (fp->f_seqcount > 1)
323 * Package up an I/O request on a vnode into a uio and do it.
326 vn_rdwr(rw, vp, base, len, offset, segflg, ioflg, active_cred, file_cred,
335 struct ucred *active_cred;
336 struct ucred *file_cred;
346 VFS_ASSERT_GIANT(vp->v_mount);
348 if ((ioflg & IO_NODELOCKED) == 0) {
350 if (rw == UIO_WRITE) {
351 if (vp->v_type != VCHR &&
352 (error = vn_start_write(vp, &mp, V_WAIT | PCATCH))
355 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
358 * XXX This should be LK_SHARED but I don't trust VFS
359 * enough to leave it like that until it has been
362 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
366 ASSERT_VOP_LOCKED(vp, "IO_NODELOCKED with no vp lock held");
367 auio.uio_iov = &aiov;
369 aiov.iov_base = base;
371 auio.uio_resid = len;
372 auio.uio_offset = offset;
373 auio.uio_segflg = segflg;
378 if ((ioflg & IO_NOMACCHECK) == 0) {
380 error = mac_check_vnode_read(active_cred, file_cred,
383 error = mac_check_vnode_write(active_cred, file_cred,
393 error = VOP_READ(vp, &auio, ioflg, cred);
395 error = VOP_WRITE(vp, &auio, ioflg, cred);
398 *aresid = auio.uio_resid;
400 if (auio.uio_resid && error == 0)
402 if ((ioflg & IO_NODELOCKED) == 0) {
404 vn_finished_write(mp);
405 VOP_UNLOCK(vp, 0, td);
411 * Package up an I/O request on a vnode into a uio and do it. The I/O
412 * request is split up into smaller chunks and we try to avoid saturating
413 * the buffer cache while potentially holding a vnode locked, so we
414 * check bwillwrite() before calling vn_rdwr(). We also call uio_yield()
415 * to give other processes a chance to lock the vnode (either other processes
416 * core'ing the same binary, or unrelated processes scanning the directory).
419 vn_rdwr_inchunks(rw, vp, base, len, offset, segflg, ioflg, active_cred,
420 file_cred, aresid, td)
428 struct ucred *active_cred;
429 struct ucred *file_cred;
436 VFS_ASSERT_GIANT(vp->v_mount);
442 * Force `offset' to a multiple of MAXBSIZE except possibly
443 * for the first chunk, so that filesystems only need to
444 * write full blocks except possibly for the first and last
447 chunk = MAXBSIZE - (uoff_t)offset % MAXBSIZE;
451 if (rw != UIO_READ && vp->v_type == VREG)
454 error = vn_rdwr(rw, vp, base, chunk, offset, segflg,
455 ioflg, active_cred, file_cred, &iaresid, td);
456 len -= chunk; /* aresid calc already includes length */
460 base = (char *)base + chunk;
464 *aresid = len + iaresid;
469 * File table vnode read routine.
472 vn_read(fp, uio, active_cred, flags, td)
475 struct ucred *active_cred;
483 KASSERT(uio->uio_td == td, ("uio_td %p is not td %p",
487 if (fp->f_flag & FNONBLOCK)
489 if (fp->f_flag & O_DIRECT)
491 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
492 VOP_LEASE(vp, td, fp->f_cred, LEASE_READ);
494 * According to McKusick the vn lock is protecting f_offset here.
495 * Once this field has it's own lock we can acquire this shared.
497 if ((flags & FOF_OFFSET) == 0) {
498 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
499 uio->uio_offset = fp->f_offset;
501 vn_lock(vp, LK_SHARED | LK_RETRY, td);
503 ioflag |= sequential_heuristic(uio, fp);
506 error = mac_check_vnode_read(active_cred, fp->f_cred, vp);
509 error = VOP_READ(vp, uio, ioflag, fp->f_cred);
510 if ((flags & FOF_OFFSET) == 0)
511 fp->f_offset = uio->uio_offset;
512 fp->f_nextoff = uio->uio_offset;
513 VOP_UNLOCK(vp, 0, td);
514 VFS_UNLOCK_GIANT(vfslocked);
519 * File table vnode write routine.
522 vn_write(fp, uio, active_cred, flags, td)
525 struct ucred *active_cred;
534 KASSERT(uio->uio_td == td, ("uio_td %p is not td %p",
537 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
538 if (vp->v_type == VREG)
541 if (vp->v_type == VREG && (fp->f_flag & O_APPEND))
543 if (fp->f_flag & FNONBLOCK)
545 if (fp->f_flag & O_DIRECT)
547 if ((fp->f_flag & O_FSYNC) ||
548 (vp->v_mount && (vp->v_mount->mnt_flag & MNT_SYNCHRONOUS)))
551 if (vp->v_type != VCHR &&
552 (error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
554 VOP_LEASE(vp, td, fp->f_cred, LEASE_WRITE);
555 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
556 if ((flags & FOF_OFFSET) == 0)
557 uio->uio_offset = fp->f_offset;
558 ioflag |= sequential_heuristic(uio, fp);
560 error = mac_check_vnode_write(active_cred, fp->f_cred, vp);
563 error = VOP_WRITE(vp, uio, ioflag, fp->f_cred);
564 if ((flags & FOF_OFFSET) == 0)
565 fp->f_offset = uio->uio_offset;
566 fp->f_nextoff = uio->uio_offset;
567 VOP_UNLOCK(vp, 0, td);
568 vn_finished_write(mp);
570 VFS_UNLOCK_GIANT(vfslocked);
575 * File table vnode stat routine.
578 vn_statfile(fp, sb, active_cred, td)
581 struct ucred *active_cred;
584 struct vnode *vp = fp->f_vnode;
588 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
589 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
590 error = vn_stat(vp, sb, active_cred, fp->f_cred, td);
591 VOP_UNLOCK(vp, 0, td);
592 VFS_UNLOCK_GIANT(vfslocked);
598 * Stat a vnode; implementation for the stat syscall
601 vn_stat(vp, sb, active_cred, file_cred, td)
603 register struct stat *sb;
604 struct ucred *active_cred;
605 struct ucred *file_cred;
609 register struct vattr *vap;
614 error = mac_check_vnode_stat(active_cred, file_cred, vp);
620 error = VOP_GETATTR(vp, vap, active_cred, td);
625 * Zero the spare stat fields
627 bzero(sb, sizeof *sb);
630 * Copy from vattr table
632 if (vap->va_fsid != VNOVAL)
633 sb->st_dev = vap->va_fsid;
635 sb->st_dev = vp->v_mount->mnt_stat.f_fsid.val[0];
636 sb->st_ino = vap->va_fileid;
638 switch (vap->va_type) {
653 /* This is a cosmetic change, symlinks do not have a mode. */
654 if (vp->v_mount->mnt_flag & MNT_NOSYMFOLLOW)
655 sb->st_mode &= ~ACCESSPERMS; /* 0000 */
657 sb->st_mode |= ACCESSPERMS; /* 0777 */
669 sb->st_nlink = vap->va_nlink;
670 sb->st_uid = vap->va_uid;
671 sb->st_gid = vap->va_gid;
672 sb->st_rdev = vap->va_rdev;
673 if (vap->va_size > OFF_MAX)
675 sb->st_size = vap->va_size;
676 sb->st_atimespec = vap->va_atime;
677 sb->st_mtimespec = vap->va_mtime;
678 sb->st_ctimespec = vap->va_ctime;
679 sb->st_birthtimespec = vap->va_birthtime;
682 * According to www.opengroup.org, the meaning of st_blksize is
683 * "a filesystem-specific preferred I/O block size for this
684 * object. In some filesystem types, this may vary from file
686 * Default to PAGE_SIZE after much discussion.
687 * XXX: min(PAGE_SIZE, vp->v_bufobj.bo_bsize) may be more correct.
690 sb->st_blksize = PAGE_SIZE;
692 sb->st_flags = vap->va_flags;
696 sb->st_gen = vap->va_gen;
698 #if (S_BLKSIZE == 512)
699 /* Optimize this case */
700 sb->st_blocks = vap->va_bytes >> 9;
702 sb->st_blocks = vap->va_bytes / S_BLKSIZE;
708 * File table vnode ioctl routine.
711 vn_ioctl(fp, com, data, active_cred, td)
715 struct ucred *active_cred;
718 struct vnode *vp = fp->f_vnode;
723 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
725 switch (vp->v_type) {
728 if (com == FIONREAD) {
729 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
730 error = VOP_GETATTR(vp, &vattr, active_cred, td);
731 VOP_UNLOCK(vp, 0, td);
733 *(int *)data = vattr.va_size - fp->f_offset;
735 if (com == FIONBIO || com == FIOASYNC) /* XXX */
738 error = VOP_IOCTL(vp, com, data, fp->f_flag,
745 VFS_UNLOCK_GIANT(vfslocked);
750 * File table vnode poll routine.
753 vn_poll(fp, events, active_cred, td)
756 struct ucred *active_cred;
766 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
767 error = mac_check_vnode_poll(active_cred, fp->f_cred, vp);
768 VOP_UNLOCK(vp, 0, td);
772 error = VOP_POLL(vp, events, fp->f_cred, td);
778 * Check that the vnode is still valid, and if so
779 * acquire requested lock.
782 vn_lock(vp, flags, td)
790 if ((flags & LK_INTERLOCK) == 0)
792 if ((flags & LK_NOWAIT || (flags & LK_TYPE_MASK) == 0) &&
793 vp->v_iflag & VI_DOOMED) {
798 * Just polling to check validity.
800 if ((flags & LK_TYPE_MASK) == 0) {
805 * lockmgr drops interlock before it will return for
806 * any reason. So force the code above to relock it.
808 error = VOP_LOCK(vp, flags | LK_INTERLOCK, td);
809 flags &= ~LK_INTERLOCK;
810 KASSERT((flags & LK_RETRY) == 0 || error == 0,
811 ("LK_RETRY set with incompatible flags %d\n", flags));
813 * Callers specify LK_RETRY if they wish to get dead vnodes.
814 * If RETRY is not set, we return ENOENT instead.
816 if (error == 0 && vp->v_iflag & VI_DOOMED &&
817 (flags & LK_RETRY) == 0) {
818 VOP_UNLOCK(vp, 0, td);
822 } while (flags & LK_RETRY && error != 0);
827 * File table vnode close routine.
841 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
842 if (fp->f_type == DTYPE_VNODE && fp->f_flag & FHASLOCK) {
843 lf.l_whence = SEEK_SET;
847 (void) VOP_ADVLOCK(vp, fp, F_UNLCK, &lf, F_FLOCK);
850 fp->f_ops = &badfileops;
852 error = vn_close(vp, fp->f_flag, fp->f_cred, td);
853 VFS_UNLOCK_GIANT(vfslocked);
858 * Preparing to start a filesystem write operation. If the operation is
859 * permitted, then we bump the count of operations in progress and
860 * proceed. If a suspend request is in progress, we wait until the
861 * suspension is over, and then proceed.
864 vn_start_write(vp, mpp, flags)
874 * If a vnode is provided, get and return the mount point that
875 * to which it will write.
878 if ((error = VOP_GETWRITEMOUNT(vp, mpp)) != 0) {
880 if (error != EOPNOTSUPP)
885 if ((mp = *mpp) == NULL)
889 * Check on status of suspension.
891 while ((mp->mnt_kern_flag & MNTK_SUSPEND) != 0) {
892 if (flags & V_NOWAIT) {
896 error = msleep(&mp->mnt_flag, MNT_MTX(mp),
897 (PUSER - 1) | (flags & PCATCH), "suspfs", 0);
901 if (flags & V_XSLEEP)
903 mp->mnt_writeopcount++;
910 * Secondary suspension. Used by operations such as vop_inactive
911 * routines that are needed by the higher level functions. These
912 * are allowed to proceed until all the higher level functions have
913 * completed (indicated by mnt_writeopcount dropping to zero). At that
914 * time, these operations are halted until the suspension is over.
917 vn_write_suspend_wait(vp, mp, flags)
925 if ((error = VOP_GETWRITEMOUNT(vp, &mp)) != 0) {
926 if (error != EOPNOTSUPP)
932 * If we are not suspended or have not yet reached suspended
933 * mode, then let the operation proceed.
938 if ((mp->mnt_kern_flag & MNTK_SUSPENDED) == 0) {
942 if (flags & V_NOWAIT) {
944 return (EWOULDBLOCK);
947 * Wait for the suspension to finish.
949 return (msleep(&mp->mnt_flag, MNT_MTX(mp),
950 (PUSER - 1) | (flags & PCATCH) | PDROP, "suspfs", 0));
954 * Filesystem write operation has completed. If we are suspending and this
955 * operation is the last one, notify the suspender that the suspension is
959 vn_finished_write(mp)
965 mp->mnt_writeopcount--;
966 if (mp->mnt_writeopcount < 0)
967 panic("vn_finished_write: neg cnt");
968 if ((mp->mnt_kern_flag & MNTK_SUSPEND) != 0 &&
969 mp->mnt_writeopcount <= 0)
970 wakeup(&mp->mnt_writeopcount);
975 * Request a filesystem to suspend write operations.
978 vfs_write_suspend(mp)
981 struct thread *td = curthread;
986 if (mp->mnt_kern_flag & MNTK_SUSPEND)
988 mp->mnt_kern_flag |= MNTK_SUSPEND;
989 if (mp->mnt_writeopcount > 0)
990 (void) msleep(&mp->mnt_writeopcount,
991 MNT_MTX(mp), (PUSER - 1)|PDROP, "suspwt", 0);
994 if ((error = VFS_SYNC(mp, MNT_WAIT, td)) != 0) {
995 vfs_write_resume(mp);
999 mp->mnt_kern_flag |= MNTK_SUSPENDED;
1006 * Request a filesystem to resume write operations.
1009 vfs_write_resume(mp)
1014 if ((mp->mnt_kern_flag & MNTK_SUSPEND) != 0) {
1015 mp->mnt_kern_flag &= ~(MNTK_SUSPEND | MNTK_SUSPENDED);
1016 wakeup(&mp->mnt_writeopcount);
1017 wakeup(&mp->mnt_flag);
1023 * Implement kqueues for files by translating it to vnode operation.
1026 vn_kqfilter(struct file *fp, struct knote *kn)
1031 error = VOP_KQFILTER(fp->f_vnode, kn);
1038 * Simplified in-kernel wrapper calls for extended attribute access.
1039 * Both calls pass in a NULL credential, authorizing as "kernel" access.
1040 * Set IO_NODELOCKED in ioflg if the vnode is already locked.
1043 vn_extattr_get(struct vnode *vp, int ioflg, int attrnamespace,
1044 const char *attrname, int *buflen, char *buf, struct thread *td)
1050 iov.iov_len = *buflen;
1053 auio.uio_iov = &iov;
1054 auio.uio_iovcnt = 1;
1055 auio.uio_rw = UIO_READ;
1056 auio.uio_segflg = UIO_SYSSPACE;
1058 auio.uio_offset = 0;
1059 auio.uio_resid = *buflen;
1061 if ((ioflg & IO_NODELOCKED) == 0)
1062 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
1064 ASSERT_VOP_LOCKED(vp, "IO_NODELOCKED with no vp lock held");
1066 /* authorize attribute retrieval as kernel */
1067 error = VOP_GETEXTATTR(vp, attrnamespace, attrname, &auio, NULL, NULL,
1070 if ((ioflg & IO_NODELOCKED) == 0)
1071 VOP_UNLOCK(vp, 0, td);
1074 *buflen = *buflen - auio.uio_resid;
1081 * XXX failure mode if partially written?
1084 vn_extattr_set(struct vnode *vp, int ioflg, int attrnamespace,
1085 const char *attrname, int buflen, char *buf, struct thread *td)
1092 iov.iov_len = buflen;
1095 auio.uio_iov = &iov;
1096 auio.uio_iovcnt = 1;
1097 auio.uio_rw = UIO_WRITE;
1098 auio.uio_segflg = UIO_SYSSPACE;
1100 auio.uio_offset = 0;
1101 auio.uio_resid = buflen;
1103 if ((ioflg & IO_NODELOCKED) == 0) {
1104 if ((error = vn_start_write(vp, &mp, V_WAIT)) != 0)
1106 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
1109 ASSERT_VOP_LOCKED(vp, "IO_NODELOCKED with no vp lock held");
1111 /* authorize attribute setting as kernel */
1112 error = VOP_SETEXTATTR(vp, attrnamespace, attrname, &auio, NULL, td);
1114 if ((ioflg & IO_NODELOCKED) == 0) {
1115 vn_finished_write(mp);
1116 VOP_UNLOCK(vp, 0, td);
1123 vn_extattr_rm(struct vnode *vp, int ioflg, int attrnamespace,
1124 const char *attrname, struct thread *td)
1129 if ((ioflg & IO_NODELOCKED) == 0) {
1130 if ((error = vn_start_write(vp, &mp, V_WAIT)) != 0)
1132 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
1135 ASSERT_VOP_LOCKED(vp, "IO_NODELOCKED with no vp lock held");
1137 /* authorize attribute removal as kernel */
1138 error = VOP_DELETEEXTATTR(vp, attrnamespace, attrname, NULL, td);
1139 if (error == EOPNOTSUPP)
1140 error = VOP_SETEXTATTR(vp, attrnamespace, attrname, NULL,
1143 if ((ioflg & IO_NODELOCKED) == 0) {
1144 vn_finished_write(mp);
1145 VOP_UNLOCK(vp, 0, td);