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 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * @(#)vfs_vnops.c 8.2 (Berkeley) 1/21/94
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/fcntl.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>
64 #include <machine/limits.h>
66 static int vn_closefile(struct file *fp, struct thread *td);
67 static int vn_ioctl(struct file *fp, u_long com, void *data,
68 struct ucred *active_cred, struct thread *td);
69 static int vn_read(struct file *fp, struct uio *uio,
70 struct ucred *active_cred, int flags, struct thread *td);
71 static int vn_poll(struct file *fp, int events, struct ucred *active_cred,
73 static int vn_kqfilter(struct file *fp, struct knote *kn);
74 static int vn_statfile(struct file *fp, struct stat *sb,
75 struct ucred *active_cred, struct thread *td);
76 static int vn_write(struct file *fp, struct uio *uio,
77 struct ucred *active_cred, int flags, struct thread *td);
79 struct fileops vnops = {
80 vn_read, vn_write, vn_ioctl, vn_poll, vn_kqfilter,
81 vn_statfile, vn_closefile
85 vn_open(ndp, flagp, cmode)
86 register struct nameidata *ndp;
89 struct thread *td = ndp->ni_cnd.cn_thread;
91 return (vn_open_cred(ndp, flagp, cmode, td->td_ucred));
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)
103 register struct nameidata *ndp;
109 struct thread *td = ndp->ni_cnd.cn_thread;
111 struct vattr *vap = &vat;
112 int mode, fmode, error;
114 int exclusive; /* The current intended lock state */
121 if (fmode & O_CREAT) {
122 ndp->ni_cnd.cn_nameiop = CREATE;
123 ndp->ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF;
124 if ((fmode & O_EXCL) == 0 && (fmode & O_NOFOLLOW) == 0)
125 ndp->ni_cnd.cn_flags |= FOLLOW;
127 if ((error = namei(ndp)) != 0)
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 if ((error = vn_start_write(NULL, &mp,
139 V_XSLEEP | PCATCH)) != 0)
144 error = mac_check_vnode_create(cred, ndp->ni_dvp,
148 VOP_LEASE(ndp->ni_dvp, td, cred, LEASE_WRITE);
149 error = VOP_CREATE(ndp->ni_dvp, &ndp->ni_vp,
155 vn_finished_write(mp);
157 NDFREE(ndp, NDF_ONLY_PNBUF);
160 ASSERT_VOP_UNLOCKED(ndp->ni_dvp, "create");
161 ASSERT_VOP_LOCKED(ndp->ni_vp, "create");
168 if (ndp->ni_dvp == ndp->ni_vp)
174 if (fmode & O_EXCL) {
181 ndp->ni_cnd.cn_nameiop = LOOKUP;
183 ndp->ni_cnd.cn_flags =
184 ((fmode & O_NOFOLLOW) ? NOFOLLOW : FOLLOW) |
185 LOCKSHARED | LOCKLEAF;
187 ndp->ni_cnd.cn_flags =
188 ((fmode & O_NOFOLLOW) ? NOFOLLOW : FOLLOW) | LOCKLEAF;
190 if ((error = namei(ndp)) != 0)
194 if (vp->v_type == VLNK) {
198 if (vp->v_type == VSOCK) {
203 if (fmode & (FWRITE | O_TRUNC)) {
204 if (vp->v_type == VDIR) {
212 if (fmode & O_APPEND)
215 error = mac_check_vnode_open(cred, vp, mode);
219 if ((fmode & O_CREAT) == 0) {
221 error = vn_writechk(vp);
226 error = VOP_ACCESS(vp, mode, cred, td);
231 if ((error = VOP_GETATTR(vp, vap, cred, td)) == 0) {
232 vp->v_cachedfs = vap->va_fsid;
233 vp->v_cachedid = vap->va_fileid;
235 if ((error = VOP_OPEN(vp, fmode, cred, td)) != 0)
238 * Make sure that a VM object is created for VMIO support.
240 if (vn_canvmio(vp) == TRUE) {
244 if (!exclusive && VOP_GETVOBJECT(vp, NULL) != 0)
245 VOP_LOCK(vp, LK_UPGRADE, td);
247 * In cases where the object is marked as dead object_create
248 * will unlock and relock exclusive. It is safe to call in
249 * here with a shared lock because we only examine fields that
250 * the shared lock guarantees will be stable. In the UPGRADE
251 * case it is not likely that anyone has used this vnode yet
252 * so there will be no contention. The logic after this call
253 * restores the requested locking state.
256 if ((error = vfs_object_create(vp, td, cred)) != 0) {
257 VOP_UNLOCK(vp, 0, td);
258 VOP_CLOSE(vp, fmode, cred, td);
259 NDFREE(ndp, NDF_ONLY_PNBUF);
265 flock = VOP_ISLOCKED(vp, td);
266 if (!exclusive && flock == LK_EXCLUSIVE)
267 VOP_LOCK(vp, LK_DOWNGRADE, td);
276 NDFREE(ndp, NDF_ONLY_PNBUF);
283 * Check for write permissions on the specified vnode.
284 * Prototype text segments cannot be written.
288 register struct vnode *vp;
291 ASSERT_VOP_LOCKED(vp, "vn_writechk");
293 * If there's shared text associated with
294 * the vnode, try to free it up once. If
295 * we fail, we can't allow writing.
297 if (vp->v_vflag & VV_TEXT)
307 vn_close(vp, flags, file_cred, td)
308 register struct vnode *vp;
310 struct ucred *file_cred;
317 error = VOP_CLOSE(vp, flags, file_cred, td);
319 * XXX - In certain instances VOP_CLOSE has to do the vrele
320 * itself. If the vrele has been done, it will return EAGAIN
321 * to indicate that the vrele should not be done again. When
322 * this happens, we just return success. The correct thing to
323 * do would be to have all VOP_CLOSE instances do the vrele.
332 * Sequential heuristic - detect sequential operation
336 sequential_heuristic(struct uio *uio, struct file *fp)
339 if ((uio->uio_offset == 0 && fp->f_seqcount > 0) ||
340 uio->uio_offset == fp->f_nextoff) {
342 * XXX we assume that the filesystem block size is
343 * the default. Not true, but still gives us a pretty
344 * good indicator of how sequential the read operations
347 fp->f_seqcount += (uio->uio_resid + BKVASIZE - 1) / BKVASIZE;
348 if (fp->f_seqcount >= 127)
349 fp->f_seqcount = 127;
350 return(fp->f_seqcount << 16);
354 * Not sequential, quick draw-down of seqcount
356 if (fp->f_seqcount > 1)
364 * Package up an I/O request on a vnode into a uio and do it.
367 vn_rdwr(rw, vp, base, len, offset, segflg, ioflg, active_cred, file_cred,
376 struct ucred *active_cred;
377 struct ucred *file_cred;
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 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
397 * XXX This should be LK_SHARED but I don't trust VFS
398 * enough to leave it like that until it has been
401 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
405 auio.uio_iov = &aiov;
407 aiov.iov_base = base;
409 auio.uio_resid = len;
410 auio.uio_offset = offset;
411 auio.uio_segflg = segflg;
416 if ((ioflg & IO_NOMACCHECK) == 0) {
418 error = mac_check_vnode_read(active_cred, file_cred,
421 error = mac_check_vnode_write(active_cred, file_cred,
431 error = VOP_READ(vp, &auio, ioflg, cred);
433 error = VOP_WRITE(vp, &auio, ioflg, cred);
436 *aresid = auio.uio_resid;
438 if (auio.uio_resid && error == 0)
440 if ((ioflg & IO_NODELOCKED) == 0) {
442 vn_finished_write(mp);
443 VOP_UNLOCK(vp, 0, td);
449 * Package up an I/O request on a vnode into a uio and do it. The I/O
450 * request is split up into smaller chunks and we try to avoid saturating
451 * the buffer cache while potentially holding a vnode locked, so we
452 * check bwillwrite() before calling vn_rdwr(). We also call uio_yield()
453 * to give other processes a chance to lock the vnode (either other processes
454 * core'ing the same binary, or unrelated processes scanning the directory).
457 vn_rdwr_inchunks(rw, vp, base, len, offset, segflg, ioflg, active_cred,
458 file_cred, aresid, td)
466 struct ucred *active_cred;
467 struct ucred *file_cred;
474 int chunk = (len > MAXBSIZE) ? MAXBSIZE : len;
476 if (rw != UIO_READ && vp->v_type == VREG)
478 error = vn_rdwr(rw, vp, base, chunk, offset, segflg,
479 ioflg, active_cred, file_cred, aresid, td);
480 len -= chunk; /* aresid calc already includes length */
493 * File table vnode read routine.
496 vn_read(fp, uio, active_cred, flags, td)
499 struct ucred *active_cred;
507 KASSERT(uio->uio_td == td, ("uio_td %p is not td %p",
509 vp = (struct vnode *)fp->f_data;
511 if (fp->f_flag & FNONBLOCK)
513 if (fp->f_flag & O_DIRECT)
515 VOP_LEASE(vp, td, fp->f_cred, LEASE_READ);
517 * According to McKusick the vn lock is protecting f_offset here.
518 * Once this field has it's own lock we can acquire this shared.
520 vn_lock(vp, LK_EXCLUSIVE | LK_NOPAUSE | LK_RETRY, td);
521 if ((flags & FOF_OFFSET) == 0)
522 uio->uio_offset = fp->f_offset;
524 ioflag |= sequential_heuristic(uio, fp);
527 error = mac_check_vnode_read(active_cred, fp->f_cred, vp);
530 error = VOP_READ(vp, uio, ioflag, fp->f_cred);
531 if ((flags & FOF_OFFSET) == 0)
532 fp->f_offset = uio->uio_offset;
533 fp->f_nextoff = uio->uio_offset;
534 VOP_UNLOCK(vp, 0, td);
540 * File table vnode write routine.
543 vn_write(fp, uio, active_cred, flags, td)
546 struct ucred *active_cred;
555 KASSERT(uio->uio_td == td, ("uio_td %p is not td %p",
557 vp = (struct vnode *)fp->f_data;
558 if (vp->v_type == VREG)
561 if (vp->v_type == VREG && (fp->f_flag & O_APPEND))
563 if (fp->f_flag & FNONBLOCK)
565 if (fp->f_flag & O_DIRECT)
567 if ((fp->f_flag & O_FSYNC) ||
568 (vp->v_mount && (vp->v_mount->mnt_flag & MNT_SYNCHRONOUS)))
571 if (vp->v_type != VCHR &&
572 (error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0) {
576 VOP_LEASE(vp, td, fp->f_cred, LEASE_WRITE);
577 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
578 if ((flags & FOF_OFFSET) == 0)
579 uio->uio_offset = fp->f_offset;
580 ioflag |= sequential_heuristic(uio, fp);
582 error = mac_check_vnode_write(active_cred, fp->f_cred, vp);
585 error = VOP_WRITE(vp, uio, ioflag, fp->f_cred);
586 if ((flags & FOF_OFFSET) == 0)
587 fp->f_offset = uio->uio_offset;
588 fp->f_nextoff = uio->uio_offset;
589 VOP_UNLOCK(vp, 0, td);
590 vn_finished_write(mp);
596 * File table vnode stat routine.
599 vn_statfile(fp, sb, active_cred, td)
602 struct ucred *active_cred;
605 struct vnode *vp = (struct vnode *)fp->f_data;
608 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
609 error = vn_stat(vp, sb, active_cred, fp->f_cred, td);
610 VOP_UNLOCK(vp, 0, td);
616 * Stat a vnode; implementation for the stat syscall
619 vn_stat(vp, sb, active_cred, file_cred, td)
621 register struct stat *sb;
622 struct ucred *active_cred;
623 struct ucred *file_cred;
627 register struct vattr *vap;
632 error = mac_check_vnode_stat(active_cred, file_cred, vp);
638 error = VOP_GETATTR(vp, vap, active_cred, td);
642 vp->v_cachedfs = vap->va_fsid;
643 vp->v_cachedid = vap->va_fileid;
646 * Zero the spare stat fields
648 bzero(sb, sizeof *sb);
651 * Copy from vattr table
653 if (vap->va_fsid != VNOVAL)
654 sb->st_dev = vap->va_fsid;
656 sb->st_dev = vp->v_mount->mnt_stat.f_fsid.val[0];
657 sb->st_ino = vap->va_fileid;
659 switch (vap->va_type) {
674 /* This is a cosmetic change, symlinks do not have a mode. */
675 if (vp->v_mount->mnt_flag & MNT_NOSYMFOLLOW)
676 sb->st_mode &= ~ACCESSPERMS; /* 0000 */
678 sb->st_mode |= ACCESSPERMS; /* 0777 */
690 sb->st_nlink = vap->va_nlink;
691 sb->st_uid = vap->va_uid;
692 sb->st_gid = vap->va_gid;
693 sb->st_rdev = vap->va_rdev;
694 if (vap->va_size > OFF_MAX)
696 sb->st_size = vap->va_size;
697 sb->st_atimespec = vap->va_atime;
698 sb->st_mtimespec = vap->va_mtime;
699 sb->st_ctimespec = vap->va_ctime;
700 sb->st_birthtimespec = vap->va_birthtime;
703 * According to www.opengroup.org, the meaning of st_blksize is
704 * "a filesystem-specific preferred I/O block size for this
705 * object. In some filesystem types, this may vary from file
707 * Default to PAGE_SIZE after much discussion.
710 if (vap->va_type == VREG) {
711 sb->st_blksize = vap->va_blocksize;
712 } else if (vn_isdisk(vp, NULL)) {
713 sb->st_blksize = vp->v_rdev->si_bsize_best;
714 if (sb->st_blksize < vp->v_rdev->si_bsize_phys)
715 sb->st_blksize = vp->v_rdev->si_bsize_phys;
716 if (sb->st_blksize < BLKDEV_IOSIZE)
717 sb->st_blksize = BLKDEV_IOSIZE;
719 sb->st_blksize = PAGE_SIZE;
722 sb->st_flags = vap->va_flags;
726 sb->st_gen = vap->va_gen;
728 #if (S_BLKSIZE == 512)
729 /* Optimize this case */
730 sb->st_blocks = vap->va_bytes >> 9;
732 sb->st_blocks = vap->va_bytes / S_BLKSIZE;
738 * File table vnode ioctl routine.
741 vn_ioctl(fp, com, data, active_cred, td)
745 struct ucred *active_cred;
748 register struct vnode *vp = ((struct vnode *)fp->f_data);
753 switch (vp->v_type) {
757 if (com == FIONREAD) {
758 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
759 error = VOP_GETATTR(vp, &vattr, active_cred, td);
760 VOP_UNLOCK(vp, 0, td);
763 *(int *)data = vattr.va_size - fp->f_offset;
766 if (com == FIONBIO || com == FIOASYNC) /* XXX */
767 return (0); /* XXX */
777 if (com == FIODTYPE) {
778 if (vp->v_type != VCHR && vp->v_type != VBLK)
780 *(int *)data = devsw(vp->v_rdev)->d_flags & D_TYPEMASK;
783 error = VOP_IOCTL(vp, com, data, fp->f_flag, active_cred, td);
784 if (error == ENOIOCTL) {
786 Debugger("ENOIOCTL leaked through");
790 if (error == 0 && com == TIOCSCTTY) {
792 /* Do nothing if reassigning same control tty */
793 sx_slock(&proctree_lock);
794 if (td->td_proc->p_session->s_ttyvp == vp) {
795 sx_sunlock(&proctree_lock);
799 vpold = td->td_proc->p_session->s_ttyvp;
801 SESS_LOCK(td->td_proc->p_session);
802 td->td_proc->p_session->s_ttyvp = vp;
803 SESS_UNLOCK(td->td_proc->p_session);
805 sx_sunlock(&proctree_lock);
807 /* Get rid of reference to old control tty */
816 * File table vnode poll routine.
819 vn_poll(fp, events, active_cred, td)
822 struct ucred *active_cred;
830 vp = (struct vnode *)fp->f_data;
832 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
833 error = mac_check_vnode_poll(active_cred, fp->f_cred, vp);
834 VOP_UNLOCK(vp, 0, td);
839 return (VOP_POLL(vp, events, fp->f_cred, td));
843 * Check that the vnode is still valid, and if so
844 * acquire requested lock.
848 vn_lock(vp, flags, td)
850 debug_vn_lock(vp, flags, td, filename, line)
856 const char *filename;
863 if ((flags & LK_INTERLOCK) == 0)
865 if ((vp->v_iflag & VI_XLOCK) && vp->v_vxproc != curthread) {
866 vp->v_iflag |= VI_XWANT;
867 msleep(vp, VI_MTX(vp), PINOD, "vn_lock", 0);
869 if ((flags & LK_RETRY) == 0) {
875 vp->filename = filename;
879 * lockmgr drops interlock before it will return for
880 * any reason. So force the code above to relock it.
882 error = VOP_LOCK(vp, flags | LK_NOPAUSE | LK_INTERLOCK, td);
883 flags &= ~LK_INTERLOCK;
884 } while (flags & LK_RETRY && error != 0);
889 * File table vnode close routine.
897 fp->f_ops = &badfileops;
898 return (vn_close(((struct vnode *)fp->f_data), fp->f_flag,
903 * Preparing to start a filesystem write operation. If the operation is
904 * permitted, then we bump the count of operations in progress and
905 * proceed. If a suspend request is in progress, we wait until the
906 * suspension is over, and then proceed.
909 vn_start_write(vp, mpp, flags)
918 * If a vnode is provided, get and return the mount point that
919 * to which it will write.
922 if ((error = VOP_GETWRITEMOUNT(vp, mpp)) != 0) {
924 if (error != EOPNOTSUPP)
929 if ((mp = *mpp) == NULL)
932 * Check on status of suspension.
934 while ((mp->mnt_kern_flag & MNTK_SUSPEND) != 0) {
935 if (flags & V_NOWAIT)
936 return (EWOULDBLOCK);
937 error = tsleep(&mp->mnt_flag, (PUSER - 1) | (flags & PCATCH),
942 if (flags & V_XSLEEP)
944 mp->mnt_writeopcount++;
949 * Secondary suspension. Used by operations such as vop_inactive
950 * routines that are needed by the higher level functions. These
951 * are allowed to proceed until all the higher level functions have
952 * completed (indicated by mnt_writeopcount dropping to zero). At that
953 * time, these operations are halted until the suspension is over.
956 vn_write_suspend_wait(vp, mp, flags)
964 if ((error = VOP_GETWRITEMOUNT(vp, &mp)) != 0) {
965 if (error != EOPNOTSUPP)
971 * If we are not suspended or have not yet reached suspended
972 * mode, then let the operation proceed.
974 if (mp == NULL || (mp->mnt_kern_flag & MNTK_SUSPENDED) == 0)
976 if (flags & V_NOWAIT)
977 return (EWOULDBLOCK);
979 * Wait for the suspension to finish.
981 return (tsleep(&mp->mnt_flag, (PUSER - 1) | (flags & PCATCH),
986 * Filesystem write operation has completed. If we are suspending and this
987 * operation is the last one, notify the suspender that the suspension is
991 vn_finished_write(mp)
997 mp->mnt_writeopcount--;
998 if (mp->mnt_writeopcount < 0)
999 panic("vn_finished_write: neg cnt");
1000 if ((mp->mnt_kern_flag & MNTK_SUSPEND) != 0 &&
1001 mp->mnt_writeopcount <= 0)
1002 wakeup(&mp->mnt_writeopcount);
1006 * Request a filesystem to suspend write operations.
1009 vfs_write_suspend(mp)
1012 struct thread *td = curthread;
1015 if (mp->mnt_kern_flag & MNTK_SUSPEND)
1017 mp->mnt_kern_flag |= MNTK_SUSPEND;
1018 if (mp->mnt_writeopcount > 0)
1019 (void) tsleep(&mp->mnt_writeopcount, PUSER - 1, "suspwt", 0);
1020 if ((error = VFS_SYNC(mp, MNT_WAIT, td->td_ucred, td)) != 0) {
1021 vfs_write_resume(mp);
1024 mp->mnt_kern_flag |= MNTK_SUSPENDED;
1029 * Request a filesystem to resume write operations.
1032 vfs_write_resume(mp)
1036 if ((mp->mnt_kern_flag & MNTK_SUSPEND) == 0)
1038 mp->mnt_kern_flag &= ~(MNTK_SUSPEND | MNTK_SUSPENDED);
1039 wakeup(&mp->mnt_writeopcount);
1040 wakeup(&mp->mnt_flag);
1044 * Implement kqueues for files by translating it to vnode operation.
1047 vn_kqfilter(struct file *fp, struct knote *kn)
1050 return (VOP_KQFILTER(((struct vnode *)fp->f_data), kn));
1054 * Simplified in-kernel wrapper calls for extended attribute access.
1055 * Both calls pass in a NULL credential, authorizing as "kernel" access.
1056 * Set IO_NODELOCKED in ioflg if the vnode is already locked.
1059 vn_extattr_get(struct vnode *vp, int ioflg, int attrnamespace,
1060 const char *attrname, int *buflen, char *buf, struct thread *td)
1066 iov.iov_len = *buflen;
1069 auio.uio_iov = &iov;
1070 auio.uio_iovcnt = 1;
1071 auio.uio_rw = UIO_READ;
1072 auio.uio_segflg = UIO_SYSSPACE;
1074 auio.uio_offset = 0;
1075 auio.uio_resid = *buflen;
1077 if ((ioflg & IO_NODELOCKED) == 0)
1078 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
1080 /* authorize attribute retrieval as kernel */
1081 error = VOP_GETEXTATTR(vp, attrnamespace, attrname, &auio, NULL, NULL,
1084 if ((ioflg & IO_NODELOCKED) == 0)
1085 VOP_UNLOCK(vp, 0, td);
1088 *buflen = *buflen - auio.uio_resid;
1095 * XXX failure mode if partially written?
1098 vn_extattr_set(struct vnode *vp, int ioflg, int attrnamespace,
1099 const char *attrname, int buflen, char *buf, struct thread *td)
1106 iov.iov_len = buflen;
1109 auio.uio_iov = &iov;
1110 auio.uio_iovcnt = 1;
1111 auio.uio_rw = UIO_WRITE;
1112 auio.uio_segflg = UIO_SYSSPACE;
1114 auio.uio_offset = 0;
1115 auio.uio_resid = buflen;
1117 if ((ioflg & IO_NODELOCKED) == 0) {
1118 if ((error = vn_start_write(vp, &mp, V_WAIT)) != 0)
1120 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
1123 /* authorize attribute setting as kernel */
1124 error = VOP_SETEXTATTR(vp, attrnamespace, attrname, &auio, NULL, td);
1126 if ((ioflg & IO_NODELOCKED) == 0) {
1127 vn_finished_write(mp);
1128 VOP_UNLOCK(vp, 0, td);
1135 vn_extattr_rm(struct vnode *vp, int ioflg, int attrnamespace,
1136 const char *attrname, struct thread *td)
1141 if ((ioflg & IO_NODELOCKED) == 0) {
1142 if ((error = vn_start_write(vp, &mp, V_WAIT)) != 0)
1144 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
1147 /* authorize attribute removal as kernel */
1148 error = VOP_SETEXTATTR(vp, attrnamespace, attrname, NULL, NULL, td);
1150 if ((ioflg & IO_NODELOCKED) == 0) {
1151 vn_finished_write(mp);
1152 VOP_UNLOCK(vp, 0, td);