2 * Copyright (c) 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_syscalls.c 8.13 (Berkeley) 4/15/94
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
40 #include "opt_compat.h"
43 #include <sys/param.h>
44 #include <sys/systm.h>
47 #include <sys/sysent.h>
49 #include <sys/malloc.h>
50 #include <sys/mount.h>
51 #include <sys/mutex.h>
52 #include <sys/sysproto.h>
53 #include <sys/namei.h>
54 #include <sys/filedesc.h>
55 #include <sys/kernel.h>
56 #include <sys/fcntl.h>
58 #include <sys/limits.h>
59 #include <sys/linker.h>
62 #include <sys/unistd.h>
63 #include <sys/vnode.h>
65 #include <sys/dirent.h>
66 #include <sys/extattr.h>
68 #include <sys/syscallsubr.h>
69 #include <sys/sysctl.h>
71 #include <machine/stdarg.h>
74 #include <vm/vm_object.h>
75 #include <vm/vm_page.h>
78 static int chroot_refuse_vdir_fds(struct filedesc *fdp);
79 static int getutimes(const struct timeval *, enum uio_seg, struct timespec *);
80 static int setfown(struct thread *td, struct vnode *, uid_t, gid_t);
81 static int setfmode(struct thread *td, struct vnode *, int);
82 static int setfflags(struct thread *td, struct vnode *, int);
83 static int setutimes(struct thread *td, struct vnode *,
84 const struct timespec *, int, int);
85 static int vn_access(struct vnode *vp, int user_flags, struct ucred *cred,
88 static int extattr_list_vp(struct vnode *vp, int attrnamespace, void *data,
89 size_t nbytes, struct thread *td);
91 int (*union_dircheckp)(struct thread *td, struct vnode **, struct file *);
94 * The module initialization routine for POSIX asynchronous I/O will
95 * set this to the version of AIO that it implements. (Zero means
96 * that it is not implemented.) This value is used here by pathconf()
97 * and in kern_descrip.c by fpathconf().
102 * Sync each mounted filesystem.
104 #ifndef _SYS_SYSPROTO_H_
111 static int syncprt = 0;
112 SYSCTL_INT(_debug, OID_AUTO, syncprt, CTLFLAG_RW, &syncprt, 0, "");
119 struct sync_args *uap;
121 struct mount *mp, *nmp;
125 mtx_lock(&mountlist_mtx);
126 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
127 if (vfs_busy(mp, LK_NOWAIT, &mountlist_mtx, td)) {
128 nmp = TAILQ_NEXT(mp, mnt_list);
131 if ((mp->mnt_flag & MNT_RDONLY) == 0 &&
132 vn_start_write(NULL, &mp, V_NOWAIT) == 0) {
133 asyncflag = mp->mnt_flag & MNT_ASYNC;
134 mp->mnt_flag &= ~MNT_ASYNC;
135 vfs_msync(mp, MNT_NOWAIT);
136 VFS_SYNC(mp, MNT_NOWAIT, td);
137 mp->mnt_flag |= asyncflag;
138 vn_finished_write(mp);
140 mtx_lock(&mountlist_mtx);
141 nmp = TAILQ_NEXT(mp, mnt_list);
144 mtx_unlock(&mountlist_mtx);
147 * XXX don't call vfs_bufstats() yet because that routine
148 * was not imported in the Lite2 merge.
153 #endif /* DIAGNOSTIC */
159 /* XXX PRISON: could be per prison flag */
160 static int prison_quotas;
162 SYSCTL_INT(_kern_prison, OID_AUTO, quotas, CTLFLAG_RW, &prison_quotas, 0, "");
166 * Change filesystem quotas.
170 #ifndef _SYS_SYSPROTO_H_
171 struct quotactl_args {
181 register struct quotactl_args /* {
188 struct mount *mp, *vmp;
192 if (jailed(td->td_ucred) && !prison_quotas)
195 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, uap->path, td);
196 if ((error = namei(&nd)) != 0) {
200 NDFREE(&nd, NDF_ONLY_PNBUF);
201 error = vn_start_write(nd.ni_vp, &vmp, V_WAIT | PCATCH);
202 mp = nd.ni_vp->v_mount;
208 error = VFS_QUOTACTL(mp, uap->cmd, uap->uid, uap->arg, td);
209 vn_finished_write(vmp);
215 * Get filesystem statistics.
217 #ifndef _SYS_SYSPROTO_H_
226 register struct statfs_args /* {
234 error = kern_statfs(td, uap->path, UIO_USERSPACE, &sf);
236 error = copyout(&sf, uap->buf, sizeof(sf));
241 kern_statfs(struct thread *td, char *path, enum uio_seg pathseg,
245 struct statfs *sp, sb;
250 NDINIT(&nd, LOOKUP, FOLLOW, pathseg, path, td);
256 mp = nd.ni_vp->v_mount;
258 NDFREE(&nd, NDF_ONLY_PNBUF);
261 error = mac_check_mount_stat(td->td_ucred, mp);
268 * Set these in case the underlying filesystem fails to do so.
270 sp->f_version = STATFS_VERSION;
271 sp->f_namemax = NAME_MAX;
272 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
273 error = VFS_STATFS(mp, sp, td);
279 bcopy(sp, &sb, sizeof(sb));
280 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
281 prison_enforce_statfs(td->td_ucred, mp, &sb);
290 * Get filesystem statistics.
292 #ifndef _SYS_SYSPROTO_H_
293 struct fstatfs_args {
301 register struct fstatfs_args /* {
309 error = kern_fstatfs(td, uap->fd, &sf);
311 error = copyout(&sf, uap->buf, sizeof(sf));
316 kern_fstatfs(struct thread *td, int fd, struct statfs *buf)
320 struct statfs *sp, sb;
324 error = getvnode(td->td_proc->p_fd, fd, &fp);
330 if (vp->v_iflag & VI_DOOMED)
334 error = mac_check_mount_stat(td->td_ucred, mp);
342 * Set these in case the underlying filesystem fails to do so.
344 sp->f_version = STATFS_VERSION;
345 sp->f_namemax = NAME_MAX;
346 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
347 error = VFS_STATFS(mp, sp, td);
353 bcopy(sp, &sb, sizeof(sb));
354 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
355 prison_enforce_statfs(td->td_ucred, mp, &sb);
364 * Get statistics on all filesystems.
366 #ifndef _SYS_SYSPROTO_H_
367 struct getfsstat_args {
376 register struct getfsstat_args /* {
383 return (kern_getfsstat(td, &uap->buf, uap->bufsize, UIO_USERSPACE,
388 * If (bufsize > 0 && bufseg == UIO_SYSSPACE)
389 * The caller is responsible for freeing memory which will be allocated
393 kern_getfsstat(struct thread *td, struct statfs **buf, size_t bufsize,
394 enum uio_seg bufseg, int flags)
396 struct mount *mp, *nmp;
397 struct statfs *sfsp, *sp, sb;
398 size_t count, maxcount;
401 maxcount = bufsize / sizeof(struct statfs);
404 else if (bufseg == UIO_USERSPACE)
406 else /* if (bufseg == UIO_SYSSPACE) */ {
408 mtx_lock(&mountlist_mtx);
409 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
412 mtx_unlock(&mountlist_mtx);
413 if (maxcount > count)
415 sfsp = *buf = malloc(maxcount * sizeof(struct statfs), M_TEMP,
420 mtx_lock(&mountlist_mtx);
421 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
422 if (prison_canseemount(td->td_ucred, mp) != 0) {
423 nmp = TAILQ_NEXT(mp, mnt_list);
427 if (mac_check_mount_stat(td->td_ucred, mp) != 0) {
428 nmp = TAILQ_NEXT(mp, mnt_list);
432 if (vfs_busy(mp, LK_NOWAIT, &mountlist_mtx, td)) {
433 nmp = TAILQ_NEXT(mp, mnt_list);
436 if (sfsp && count < maxcount) {
439 * Set these in case the underlying filesystem
442 sp->f_version = STATFS_VERSION;
443 sp->f_namemax = NAME_MAX;
444 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
446 * If MNT_NOWAIT or MNT_LAZY is specified, do not
447 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
448 * overrides MNT_WAIT.
450 if (((flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
451 (flags & MNT_WAIT)) &&
452 (error = VFS_STATFS(mp, sp, td))) {
453 mtx_lock(&mountlist_mtx);
454 nmp = TAILQ_NEXT(mp, mnt_list);
459 bcopy(sp, &sb, sizeof(sb));
460 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
461 prison_enforce_statfs(td->td_ucred, mp, &sb);
464 if (bufseg == UIO_SYSSPACE)
465 bcopy(sp, sfsp, sizeof(*sp));
466 else /* if (bufseg == UIO_USERSPACE) */ {
467 error = copyout(sp, sfsp, sizeof(*sp));
477 mtx_lock(&mountlist_mtx);
478 nmp = TAILQ_NEXT(mp, mnt_list);
481 mtx_unlock(&mountlist_mtx);
483 if (sfsp && count > maxcount)
484 td->td_retval[0] = maxcount;
486 td->td_retval[0] = count;
490 #ifdef COMPAT_FREEBSD4
492 * Get old format filesystem statistics.
494 static void cvtstatfs(struct statfs *, struct ostatfs *);
496 #ifndef _SYS_SYSPROTO_H_
497 struct freebsd4_statfs_args {
503 freebsd4_statfs(td, uap)
505 struct freebsd4_statfs_args /* {
514 error = kern_statfs(td, uap->path, UIO_USERSPACE, &sf);
517 cvtstatfs(&sf, &osb);
518 return (copyout(&osb, uap->buf, sizeof(osb)));
522 * Get filesystem statistics.
524 #ifndef _SYS_SYSPROTO_H_
525 struct freebsd4_fstatfs_args {
531 freebsd4_fstatfs(td, uap)
533 struct freebsd4_fstatfs_args /* {
542 error = kern_fstatfs(td, uap->fd, &sf);
545 cvtstatfs(&sf, &osb);
546 return (copyout(&osb, uap->buf, sizeof(osb)));
550 * Get statistics on all filesystems.
552 #ifndef _SYS_SYSPROTO_H_
553 struct freebsd4_getfsstat_args {
560 freebsd4_getfsstat(td, uap)
562 register struct freebsd4_getfsstat_args /* {
568 struct statfs *buf, *sp;
573 count = uap->bufsize / sizeof(struct ostatfs);
574 size = count * sizeof(struct statfs);
575 error = kern_getfsstat(td, &buf, size, UIO_SYSSPACE, uap->flags);
577 count = td->td_retval[0];
579 while (count > 0 && error == 0) {
581 error = copyout(&osb, uap->buf, sizeof(osb));
592 * Implement fstatfs() for (NFS) file handles.
594 #ifndef _SYS_SYSPROTO_H_
595 struct freebsd4_fhstatfs_args {
596 struct fhandle *u_fhp;
601 freebsd4_fhstatfs(td, uap)
603 struct freebsd4_fhstatfs_args /* {
604 struct fhandle *u_fhp;
613 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
616 error = kern_fhstatfs(td, fh, &sf);
619 cvtstatfs(&sf, &osb);
620 return (copyout(&osb, uap->buf, sizeof(osb)));
624 * Convert a new format statfs structure to an old format statfs structure.
632 bzero(osp, sizeof(*osp));
633 osp->f_bsize = MIN(nsp->f_bsize, LONG_MAX);
634 osp->f_iosize = MIN(nsp->f_iosize, LONG_MAX);
635 osp->f_blocks = MIN(nsp->f_blocks, LONG_MAX);
636 osp->f_bfree = MIN(nsp->f_bfree, LONG_MAX);
637 osp->f_bavail = MIN(nsp->f_bavail, LONG_MAX);
638 osp->f_files = MIN(nsp->f_files, LONG_MAX);
639 osp->f_ffree = MIN(nsp->f_ffree, LONG_MAX);
640 osp->f_owner = nsp->f_owner;
641 osp->f_type = nsp->f_type;
642 osp->f_flags = nsp->f_flags;
643 osp->f_syncwrites = MIN(nsp->f_syncwrites, LONG_MAX);
644 osp->f_asyncwrites = MIN(nsp->f_asyncwrites, LONG_MAX);
645 osp->f_syncreads = MIN(nsp->f_syncreads, LONG_MAX);
646 osp->f_asyncreads = MIN(nsp->f_asyncreads, LONG_MAX);
647 bcopy(nsp->f_fstypename, osp->f_fstypename,
648 MIN(MFSNAMELEN, OMNAMELEN));
649 bcopy(nsp->f_mntonname, osp->f_mntonname,
650 MIN(MFSNAMELEN, OMNAMELEN));
651 bcopy(nsp->f_mntfromname, osp->f_mntfromname,
652 MIN(MFSNAMELEN, OMNAMELEN));
653 osp->f_fsid = nsp->f_fsid;
655 #endif /* COMPAT_FREEBSD4 */
658 * Change current working directory to a given file descriptor.
660 #ifndef _SYS_SYSPROTO_H_
668 struct fchdir_args /* {
672 register struct filedesc *fdp = td->td_proc->p_fd;
673 struct vnode *vp, *tdp, *vpold;
679 if ((error = getvnode(fdp, uap->fd, &fp)) != 0)
684 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
685 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
686 if (vp->v_type != VDIR)
689 else if ((error = mac_check_vnode_chdir(td->td_ucred, vp)) != 0) {
693 error = VOP_ACCESS(vp, VEXEC, td->td_ucred, td);
694 while (!error && (mp = vp->v_mountedhere) != NULL) {
696 if (vfs_busy(mp, 0, 0, td))
698 tvfslocked = VFS_LOCK_GIANT(mp);
699 error = VFS_ROOT(mp, LK_EXCLUSIVE, &tdp, td);
702 VFS_UNLOCK_GIANT(tvfslocked);
706 VFS_UNLOCK_GIANT(vfslocked);
708 vfslocked = tvfslocked;
712 VFS_UNLOCK_GIANT(vfslocked);
715 VOP_UNLOCK(vp, 0, td);
716 VFS_UNLOCK_GIANT(vfslocked);
717 FILEDESC_LOCK_FAST(fdp);
718 vpold = fdp->fd_cdir;
720 FILEDESC_UNLOCK_FAST(fdp);
721 vfslocked = VFS_LOCK_GIANT(vpold->v_mount);
723 VFS_UNLOCK_GIANT(vfslocked);
728 * Change current working directory (``.'').
730 #ifndef _SYS_SYSPROTO_H_
738 struct chdir_args /* {
743 return (kern_chdir(td, uap->path, UIO_USERSPACE));
747 kern_chdir(struct thread *td, char *path, enum uio_seg pathseg)
749 register struct filedesc *fdp = td->td_proc->p_fd;
755 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | MPSAFE, pathseg, path, td);
756 if ((error = namei(&nd)) != 0)
758 vfslocked = NDHASGIANT(&nd);
759 if ((error = change_dir(nd.ni_vp, td)) != 0) {
761 VFS_UNLOCK_GIANT(vfslocked);
762 NDFREE(&nd, NDF_ONLY_PNBUF);
765 VOP_UNLOCK(nd.ni_vp, 0, td);
766 VFS_UNLOCK_GIANT(vfslocked);
767 NDFREE(&nd, NDF_ONLY_PNBUF);
768 FILEDESC_LOCK_FAST(fdp);
770 fdp->fd_cdir = nd.ni_vp;
771 FILEDESC_UNLOCK_FAST(fdp);
772 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
774 VFS_UNLOCK_GIANT(vfslocked);
779 * Helper function for raised chroot(2) security function: Refuse if
780 * any filedescriptors are open directories.
783 chroot_refuse_vdir_fds(fdp)
784 struct filedesc *fdp;
790 FILEDESC_LOCK_ASSERT(fdp, MA_OWNED);
791 for (fd = 0; fd < fdp->fd_nfiles ; fd++) {
792 fp = fget_locked(fdp, fd);
795 if (fp->f_type == DTYPE_VNODE) {
797 if (vp->v_type == VDIR)
805 * This sysctl determines if we will allow a process to chroot(2) if it
806 * has a directory open:
807 * 0: disallowed for all processes.
808 * 1: allowed for processes that were not already chroot(2)'ed.
809 * 2: allowed for all processes.
812 static int chroot_allow_open_directories = 1;
814 SYSCTL_INT(_kern, OID_AUTO, chroot_allow_open_directories, CTLFLAG_RW,
815 &chroot_allow_open_directories, 0, "");
818 * Change notion of root (``/'') directory.
820 #ifndef _SYS_SYSPROTO_H_
828 struct chroot_args /* {
836 error = suser_cred(td->td_ucred, SUSER_ALLOWJAIL);
839 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | MPSAFE,
840 UIO_USERSPACE, uap->path, td);
844 vfslocked = NDHASGIANT(&nd);
845 if ((error = change_dir(nd.ni_vp, td)) != 0)
848 if ((error = mac_check_vnode_chroot(td->td_ucred, nd.ni_vp)))
851 VOP_UNLOCK(nd.ni_vp, 0, td);
852 error = change_root(nd.ni_vp, td);
854 VFS_UNLOCK_GIANT(vfslocked);
855 NDFREE(&nd, NDF_ONLY_PNBUF);
859 VFS_UNLOCK_GIANT(vfslocked);
861 NDFREE(&nd, NDF_ONLY_PNBUF);
866 * Common routine for chroot and chdir. Callers must provide a locked vnode
876 ASSERT_VOP_LOCKED(vp, "change_dir(): vp not locked");
877 if (vp->v_type != VDIR)
880 error = mac_check_vnode_chdir(td->td_ucred, vp);
884 error = VOP_ACCESS(vp, VEXEC, td->td_ucred, td);
889 * Common routine for kern_chroot() and jail_attach(). The caller is
890 * responsible for invoking suser() and mac_check_chroot() to authorize this
898 struct filedesc *fdp;
903 VFS_ASSERT_GIANT(vp->v_mount);
904 fdp = td->td_proc->p_fd;
906 if (chroot_allow_open_directories == 0 ||
907 (chroot_allow_open_directories == 1 && fdp->fd_rdir != rootvnode)) {
908 error = chroot_refuse_vdir_fds(fdp);
910 FILEDESC_UNLOCK(fdp);
914 oldvp = fdp->fd_rdir;
921 FILEDESC_UNLOCK(fdp);
922 vfslocked = VFS_LOCK_GIANT(oldvp->v_mount);
924 VFS_UNLOCK_GIANT(vfslocked);
929 * Check permissions, allocate an open file structure,
930 * and call the device open routine if any.
934 #ifndef _SYS_SYSPROTO_H_
944 register struct open_args /* {
952 error = kern_open(td, uap->path, UIO_USERSPACE, uap->flags, uap->mode);
953 if (mtx_owned(&Giant))
954 printf("open: %s: %d\n", uap->path, error);
959 kern_open(struct thread *td, char *path, enum uio_seg pathseg, int flags,
962 struct proc *p = td->td_proc;
963 struct filedesc *fdp = p->p_fd;
970 int type, indx, error;
975 if ((flags & O_ACCMODE) == O_ACCMODE)
977 flags = FFLAGS(flags);
978 error = falloc(td, &nfp, &indx);
981 /* An extra reference on `nfp' has been held for us by falloc(). */
983 cmode = ((mode &~ fdp->fd_cmask) & ALLPERMS) &~ S_ISTXT;
984 NDINIT(&nd, LOOKUP, FOLLOW, pathseg, path, td);
985 td->td_dupfd = -1; /* XXX check for fdopen */
986 error = vn_open(&nd, &flags, cmode, indx);
989 * If the vn_open replaced the method vector, something
990 * wonderous happened deep below and we just pass it up
991 * pretending we know what we do.
993 if (error == ENXIO && fp->f_ops != &badfileops) {
995 td->td_retval[0] = indx;
1000 * release our own reference
1005 * handle special fdopen() case. bleh. dupfdopen() is
1006 * responsible for dropping the old contents of ofiles[indx]
1009 if ((error == ENODEV || error == ENXIO) &&
1010 td->td_dupfd >= 0 && /* XXX from fdopen */
1012 dupfdopen(td, fdp, indx, td->td_dupfd, flags, error)) == 0) {
1013 td->td_retval[0] = indx;
1017 * Clean up the descriptor, but only if another thread hadn't
1018 * replaced or closed it.
1020 fdclose(fdp, fp, indx, td);
1022 if (error == ERESTART)
1027 vfslocked = NDHASGIANT(&nd);
1028 NDFREE(&nd, NDF_ONLY_PNBUF);
1032 * There should be 2 references on the file, one from the descriptor
1033 * table, and one for us.
1035 * Handle the case where someone closed the file (via its file
1036 * descriptor) while we were blocked. The end result should look
1037 * like opening the file succeeded but it was immediately closed.
1038 * We call vn_close() manually because we haven't yet hooked up
1039 * the various 'struct file' fields.
1043 if (fp->f_count == 1) {
1045 KASSERT(fdp->fd_ofiles[indx] != fp,
1046 ("Open file descriptor lost all refs"));
1048 FILEDESC_UNLOCK(fdp);
1049 VOP_UNLOCK(vp, 0, td);
1050 vn_close(vp, flags & FMASK, fp->f_cred, td);
1051 VFS_UNLOCK_GIANT(vfslocked);
1053 td->td_retval[0] = indx;
1057 if (fp->f_data == NULL)
1059 fp->f_flag = flags & FMASK;
1060 if (fp->f_ops == &badfileops)
1063 fp->f_type = (vp->v_type == VFIFO ? DTYPE_FIFO : DTYPE_VNODE);
1065 FILEDESC_UNLOCK(fdp);
1067 VOP_UNLOCK(vp, 0, td);
1068 if (flags & (O_EXLOCK | O_SHLOCK)) {
1069 lf.l_whence = SEEK_SET;
1072 if (flags & O_EXLOCK)
1073 lf.l_type = F_WRLCK;
1075 lf.l_type = F_RDLCK;
1077 if ((flags & FNONBLOCK) == 0)
1079 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf,
1082 fp->f_flag |= FHASLOCK;
1084 if (flags & O_TRUNC) {
1085 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
1087 VOP_LEASE(vp, td, td->td_ucred, LEASE_WRITE);
1090 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
1092 error = mac_check_vnode_write(td->td_ucred, fp->f_cred, vp);
1095 error = VOP_SETATTR(vp, &vat, td->td_ucred, td);
1096 VOP_UNLOCK(vp, 0, td);
1097 vn_finished_write(mp);
1101 VFS_UNLOCK_GIANT(vfslocked);
1103 * Release our private reference, leaving the one associated with
1104 * the descriptor table intact.
1107 td->td_retval[0] = indx;
1110 VFS_UNLOCK_GIANT(vfslocked);
1111 fdclose(fdp, fp, indx, td);
1122 #ifndef _SYS_SYSPROTO_H_
1123 struct ocreat_args {
1131 register struct ocreat_args /* {
1137 return (kern_open(td, uap->path, UIO_USERSPACE,
1138 O_WRONLY | O_CREAT | O_TRUNC, uap->mode));
1140 #endif /* COMPAT_43 */
1143 * Create a special file.
1145 #ifndef _SYS_SYSPROTO_H_
1155 register struct mknod_args /* {
1162 return (kern_mknod(td, uap->path, UIO_USERSPACE, uap->mode, uap->dev));
1166 kern_mknod(struct thread *td, char *path, enum uio_seg pathseg, int mode,
1174 struct nameidata nd;
1177 switch (mode & S_IFMT) {
1183 error = suser_cred(td->td_ucred, SUSER_ALLOWJAIL);
1190 NDINIT(&nd, CREATE, LOCKPARENT | SAVENAME | MPSAFE, pathseg, path, td);
1191 if ((error = namei(&nd)) != 0)
1193 vfslocked = NDHASGIANT(&nd);
1196 NDFREE(&nd, NDF_ONLY_PNBUF);
1197 if (vp == nd.ni_dvp)
1202 VFS_UNLOCK_GIANT(vfslocked);
1206 FILEDESC_LOCK_FAST(td->td_proc->p_fd);
1207 vattr.va_mode = (mode & ALLPERMS) &
1208 ~td->td_proc->p_fd->fd_cmask;
1209 FILEDESC_UNLOCK_FAST(td->td_proc->p_fd);
1210 vattr.va_rdev = dev;
1213 switch (mode & S_IFMT) {
1214 case S_IFMT: /* used by badsect to flag bad sectors */
1215 vattr.va_type = VBAD;
1218 vattr.va_type = VCHR;
1221 vattr.va_type = VBLK;
1231 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1232 NDFREE(&nd, NDF_ONLY_PNBUF);
1234 VFS_UNLOCK_GIANT(vfslocked);
1235 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1240 if (error == 0 && !whiteout)
1241 error = mac_check_vnode_create(td->td_ucred, nd.ni_dvp,
1242 &nd.ni_cnd, &vattr);
1245 VOP_LEASE(nd.ni_dvp, td, td->td_ucred, LEASE_WRITE);
1247 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, CREATE);
1249 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp,
1250 &nd.ni_cnd, &vattr);
1255 NDFREE(&nd, NDF_ONLY_PNBUF);
1257 vn_finished_write(mp);
1258 VFS_UNLOCK_GIANT(vfslocked);
1263 * Create a named pipe.
1265 #ifndef _SYS_SYSPROTO_H_
1266 struct mkfifo_args {
1274 register struct mkfifo_args /* {
1280 return (kern_mkfifo(td, uap->path, UIO_USERSPACE, uap->mode));
1284 kern_mkfifo(struct thread *td, char *path, enum uio_seg pathseg, int mode)
1289 struct nameidata nd;
1294 NDINIT(&nd, CREATE, LOCKPARENT | SAVENAME | MPSAFE, pathseg, path, td);
1295 if ((error = namei(&nd)) != 0)
1297 vfslocked = NDHASGIANT(&nd);
1298 if (nd.ni_vp != NULL) {
1299 NDFREE(&nd, NDF_ONLY_PNBUF);
1300 if (nd.ni_vp == nd.ni_dvp)
1305 VFS_UNLOCK_GIANT(vfslocked);
1308 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1309 NDFREE(&nd, NDF_ONLY_PNBUF);
1311 VFS_UNLOCK_GIANT(vfslocked);
1312 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1317 vattr.va_type = VFIFO;
1318 FILEDESC_LOCK_FAST(td->td_proc->p_fd);
1319 vattr.va_mode = (mode & ALLPERMS) & ~td->td_proc->p_fd->fd_cmask;
1320 FILEDESC_UNLOCK_FAST(td->td_proc->p_fd);
1322 error = mac_check_vnode_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1327 VOP_LEASE(nd.ni_dvp, td, td->td_ucred, LEASE_WRITE);
1328 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
1335 vn_finished_write(mp);
1336 VFS_UNLOCK_GIANT(vfslocked);
1337 NDFREE(&nd, NDF_ONLY_PNBUF);
1342 * Make a hard file link.
1344 #ifndef _SYS_SYSPROTO_H_
1353 register struct link_args /* {
1360 error = kern_link(td, uap->path, uap->link, UIO_USERSPACE);
1364 SYSCTL_DECL(_security_bsd);
1366 static int hardlink_check_uid = 0;
1367 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
1368 &hardlink_check_uid, 0,
1369 "Unprivileged processes cannot create hard links to files owned by other "
1371 static int hardlink_check_gid = 0;
1372 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
1373 &hardlink_check_gid, 0,
1374 "Unprivileged processes cannot create hard links to files owned by other "
1378 can_hardlink(struct vnode *vp, struct thread *td, struct ucred *cred)
1383 if (suser_cred(cred, SUSER_ALLOWJAIL) == 0)
1386 if (!hardlink_check_uid && !hardlink_check_gid)
1389 error = VOP_GETATTR(vp, &va, cred, td);
1393 if (hardlink_check_uid) {
1394 if (cred->cr_uid != va.va_uid)
1398 if (hardlink_check_gid) {
1399 if (!groupmember(va.va_gid, cred))
1407 kern_link(struct thread *td, char *path, char *link, enum uio_seg segflg)
1411 struct nameidata nd;
1417 NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE, segflg, path, td);
1418 if ((error = namei(&nd)) != 0)
1420 vfslocked = NDHASGIANT(&nd);
1421 NDFREE(&nd, NDF_ONLY_PNBUF);
1423 if (vp->v_type == VDIR) {
1425 VFS_UNLOCK_GIANT(vfslocked);
1426 return (EPERM); /* POSIX */
1428 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0) {
1430 VFS_UNLOCK_GIANT(vfslocked);
1433 NDINIT(&nd, CREATE, LOCKPARENT | SAVENAME | MPSAFE, segflg, link, td);
1434 if ((error = namei(&nd)) == 0) {
1435 lvfslocked = NDHASGIANT(&nd);
1436 if (nd.ni_vp != NULL) {
1437 if (nd.ni_dvp == nd.ni_vp)
1443 } else if ((error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td))
1445 VOP_LEASE(nd.ni_dvp, td, td->td_ucred, LEASE_WRITE);
1446 VOP_LEASE(vp, td, td->td_ucred, LEASE_WRITE);
1447 error = can_hardlink(vp, td, td->td_ucred);
1450 error = mac_check_vnode_link(td->td_ucred,
1451 nd.ni_dvp, vp, &nd.ni_cnd);
1454 error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
1455 VOP_UNLOCK(vp, 0, td);
1458 NDFREE(&nd, NDF_ONLY_PNBUF);
1459 VFS_UNLOCK_GIANT(lvfslocked);
1462 vn_finished_write(mp);
1463 VFS_UNLOCK_GIANT(vfslocked);
1468 * Make a symbolic link.
1470 #ifndef _SYS_SYSPROTO_H_
1471 struct symlink_args {
1479 register struct symlink_args /* {
1485 return (kern_symlink(td, uap->path, uap->link, UIO_USERSPACE));
1489 kern_symlink(struct thread *td, char *path, char *link, enum uio_seg segflg)
1495 struct nameidata nd;
1498 if (segflg == UIO_SYSSPACE) {
1501 syspath = uma_zalloc(namei_zone, M_WAITOK);
1502 if ((error = copyinstr(path, syspath, MAXPATHLEN, NULL)) != 0)
1507 NDINIT(&nd, CREATE, LOCKPARENT | SAVENAME | MPSAFE,
1509 if ((error = namei(&nd)) != 0)
1511 vfslocked = NDHASGIANT(&nd);
1513 NDFREE(&nd, NDF_ONLY_PNBUF);
1514 if (nd.ni_vp == nd.ni_dvp)
1519 VFS_UNLOCK_GIANT(vfslocked);
1523 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1524 NDFREE(&nd, NDF_ONLY_PNBUF);
1526 VFS_UNLOCK_GIANT(vfslocked);
1527 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1532 FILEDESC_LOCK_FAST(td->td_proc->p_fd);
1533 vattr.va_mode = ACCESSPERMS &~ td->td_proc->p_fd->fd_cmask;
1534 FILEDESC_UNLOCK_FAST(td->td_proc->p_fd);
1536 vattr.va_type = VLNK;
1537 error = mac_check_vnode_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1542 VOP_LEASE(nd.ni_dvp, td, td->td_ucred, LEASE_WRITE);
1543 error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr, syspath);
1549 NDFREE(&nd, NDF_ONLY_PNBUF);
1551 vn_finished_write(mp);
1552 VFS_UNLOCK_GIANT(vfslocked);
1554 if (segflg != UIO_SYSSPACE)
1555 uma_zfree(namei_zone, syspath);
1560 * Delete a whiteout from the filesystem.
1565 register struct undelete_args /* {
1571 struct nameidata nd;
1576 NDINIT(&nd, DELETE, LOCKPARENT | DOWHITEOUT | MPSAFE, UIO_USERSPACE,
1581 vfslocked = NDHASGIANT(&nd);
1583 if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & ISWHITEOUT)) {
1584 NDFREE(&nd, NDF_ONLY_PNBUF);
1585 if (nd.ni_vp == nd.ni_dvp)
1591 VFS_UNLOCK_GIANT(vfslocked);
1594 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1595 NDFREE(&nd, NDF_ONLY_PNBUF);
1597 VFS_UNLOCK_GIANT(vfslocked);
1598 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1602 VOP_LEASE(nd.ni_dvp, td, td->td_ucred, LEASE_WRITE);
1603 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, DELETE);
1604 NDFREE(&nd, NDF_ONLY_PNBUF);
1606 vn_finished_write(mp);
1607 VFS_UNLOCK_GIANT(vfslocked);
1612 * Delete a name from the filesystem.
1614 #ifndef _SYS_SYSPROTO_H_
1615 struct unlink_args {
1622 struct unlink_args /* {
1628 error = kern_unlink(td, uap->path, UIO_USERSPACE);
1633 kern_unlink(struct thread *td, char *path, enum uio_seg pathseg)
1638 struct nameidata nd;
1643 NDINIT(&nd, DELETE, LOCKPARENT | LOCKLEAF | MPSAFE, pathseg, path, td);
1644 if ((error = namei(&nd)) != 0)
1645 return (error == EINVAL ? EPERM : error);
1646 vfslocked = NDHASGIANT(&nd);
1648 if (vp->v_type == VDIR)
1649 error = EPERM; /* POSIX */
1652 * The root of a mounted filesystem cannot be deleted.
1654 * XXX: can this only be a VDIR case?
1656 if (vp->v_vflag & VV_ROOT)
1660 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1661 NDFREE(&nd, NDF_ONLY_PNBUF);
1663 if (vp == nd.ni_dvp)
1667 VFS_UNLOCK_GIANT(vfslocked);
1668 if ((error = vn_start_write(NULL, &mp,
1669 V_XSLEEP | PCATCH)) != 0)
1674 error = mac_check_vnode_delete(td->td_ucred, nd.ni_dvp, vp,
1679 VOP_LEASE(nd.ni_dvp, td, td->td_ucred, LEASE_WRITE);
1680 error = VOP_REMOVE(nd.ni_dvp, vp, &nd.ni_cnd);
1684 vn_finished_write(mp);
1686 NDFREE(&nd, NDF_ONLY_PNBUF);
1688 if (vp == nd.ni_dvp)
1692 VFS_UNLOCK_GIANT(vfslocked);
1697 * Reposition read/write file offset.
1699 #ifndef _SYS_SYSPROTO_H_
1710 register struct lseek_args /* {
1717 struct ucred *cred = td->td_ucred;
1725 if ((error = fget(td, uap->fd, &fp)) != 0)
1727 if (!(fp->f_ops->fo_flags & DFLAG_SEEKABLE)) {
1732 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
1733 noneg = (vp->v_type != VCHR);
1734 offset = uap->offset;
1735 switch (uap->whence) {
1738 (fp->f_offset < 0 ||
1739 (offset > 0 && fp->f_offset > OFF_MAX - offset))) {
1743 offset += fp->f_offset;
1746 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
1747 error = VOP_GETATTR(vp, &vattr, cred, td);
1748 VOP_UNLOCK(vp, 0, td);
1752 (vattr.va_size > OFF_MAX ||
1753 (offset > 0 && vattr.va_size > OFF_MAX - offset))) {
1757 offset += vattr.va_size;
1764 if (error == 0 && noneg && offset < 0)
1768 fp->f_offset = offset;
1769 *(off_t *)(td->td_retval) = fp->f_offset;
1772 VFS_UNLOCK_GIANT(vfslocked);
1776 #if defined(COMPAT_43)
1778 * Reposition read/write file offset.
1780 #ifndef _SYS_SYSPROTO_H_
1781 struct olseek_args {
1790 register struct olseek_args /* {
1796 struct lseek_args /* {
1805 nuap.offset = uap->offset;
1806 nuap.whence = uap->whence;
1807 error = lseek(td, &nuap);
1810 #endif /* COMPAT_43 */
1813 * Check access permissions using passed credentials.
1816 vn_access(vp, user_flags, cred, td)
1824 /* Flags == 0 means only check for existence. */
1828 if (user_flags & R_OK)
1830 if (user_flags & W_OK)
1832 if (user_flags & X_OK)
1835 error = mac_check_vnode_access(cred, vp, flags);
1839 if ((flags & VWRITE) == 0 || (error = vn_writechk(vp)) == 0)
1840 error = VOP_ACCESS(vp, flags, cred, td);
1846 * Check access permissions using "real" credentials.
1848 #ifndef _SYS_SYSPROTO_H_
1849 struct access_args {
1857 register struct access_args /* {
1863 return (kern_access(td, uap->path, UIO_USERSPACE, uap->flags));
1867 kern_access(struct thread *td, char *path, enum uio_seg pathseg, int flags)
1869 struct ucred *cred, *tmpcred;
1870 register struct vnode *vp;
1871 struct nameidata nd;
1876 * Create and modify a temporary credential instead of one that
1877 * is potentially shared. This could also mess up socket
1878 * buffer accounting which can run in an interrupt context.
1880 cred = td->td_ucred;
1881 tmpcred = crdup(cred);
1882 tmpcred->cr_uid = cred->cr_ruid;
1883 tmpcred->cr_groups[0] = cred->cr_rgid;
1884 td->td_ucred = tmpcred;
1885 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | MPSAFE, pathseg, path, td);
1886 if ((error = namei(&nd)) != 0)
1888 vfslocked = NDHASGIANT(&nd);
1891 error = vn_access(vp, flags, tmpcred, td);
1892 NDFREE(&nd, NDF_ONLY_PNBUF);
1894 VFS_UNLOCK_GIANT(vfslocked);
1896 td->td_ucred = cred;
1902 * Check access permissions using "effective" credentials.
1904 #ifndef _SYS_SYSPROTO_H_
1905 struct eaccess_args {
1913 register struct eaccess_args /* {
1919 return (kern_eaccess(td, uap->path, UIO_USERSPACE, uap->flags));
1923 kern_eaccess(struct thread *td, char *path, enum uio_seg pathseg, int flags)
1925 struct nameidata nd;
1930 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | MPSAFE, pathseg, path, td);
1931 if ((error = namei(&nd)) != 0)
1934 vfslocked = NDHASGIANT(&nd);
1935 error = vn_access(vp, flags, td->td_ucred, td);
1936 NDFREE(&nd, NDF_ONLY_PNBUF);
1938 VFS_UNLOCK_GIANT(vfslocked);
1942 #if defined(COMPAT_43)
1944 * Get file status; this version follows links.
1946 #ifndef _SYS_SYSPROTO_H_
1955 register struct ostat_args /* {
1964 error = kern_stat(td, uap->path, UIO_USERSPACE, &sb);
1968 error = copyout(&osb, uap->ub, sizeof (osb));
1973 * Get file status; this version does not follow links.
1975 #ifndef _SYS_SYSPROTO_H_
1976 struct olstat_args {
1984 register struct olstat_args /* {
1993 error = kern_lstat(td, uap->path, UIO_USERSPACE, &sb);
1997 error = copyout(&osb, uap->ub, sizeof (osb));
2002 * Convert from an old to a new stat structure.
2010 ost->st_dev = st->st_dev;
2011 ost->st_ino = st->st_ino;
2012 ost->st_mode = st->st_mode;
2013 ost->st_nlink = st->st_nlink;
2014 ost->st_uid = st->st_uid;
2015 ost->st_gid = st->st_gid;
2016 ost->st_rdev = st->st_rdev;
2017 if (st->st_size < (quad_t)1 << 32)
2018 ost->st_size = st->st_size;
2021 ost->st_atime = st->st_atime;
2022 ost->st_mtime = st->st_mtime;
2023 ost->st_ctime = st->st_ctime;
2024 ost->st_blksize = st->st_blksize;
2025 ost->st_blocks = st->st_blocks;
2026 ost->st_flags = st->st_flags;
2027 ost->st_gen = st->st_gen;
2029 #endif /* COMPAT_43 */
2032 * Get file status; this version follows links.
2034 #ifndef _SYS_SYSPROTO_H_
2043 register struct stat_args /* {
2051 error = kern_stat(td, uap->path, UIO_USERSPACE, &sb);
2053 error = copyout(&sb, uap->ub, sizeof (sb));
2058 kern_stat(struct thread *td, char *path, enum uio_seg pathseg, struct stat *sbp)
2060 struct nameidata nd;
2062 int error, vfslocked;
2064 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | MPSAFE,
2066 if ((error = namei(&nd)) != 0)
2068 vfslocked = NDHASGIANT(&nd);
2069 error = vn_stat(nd.ni_vp, &sb, td->td_ucred, NOCRED, td);
2070 NDFREE(&nd, NDF_ONLY_PNBUF);
2072 VFS_UNLOCK_GIANT(vfslocked);
2080 * Get file status; this version does not follow links.
2082 #ifndef _SYS_SYSPROTO_H_
2091 register struct lstat_args /* {
2099 error = kern_lstat(td, uap->path, UIO_USERSPACE, &sb);
2101 error = copyout(&sb, uap->ub, sizeof (sb));
2106 kern_lstat(struct thread *td, char *path, enum uio_seg pathseg, struct stat *sbp)
2110 struct nameidata nd;
2111 int error, vfslocked;
2113 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | LOCKSHARED | MPSAFE,
2115 if ((error = namei(&nd)) != 0)
2117 vfslocked = NDHASGIANT(&nd);
2119 error = vn_stat(vp, &sb, td->td_ucred, NOCRED, td);
2120 NDFREE(&nd, NDF_ONLY_PNBUF);
2122 VFS_UNLOCK_GIANT(vfslocked);
2130 * Implementation of the NetBSD [l]stat() functions.
2137 bzero(nsb, sizeof *nsb);
2138 nsb->st_dev = sb->st_dev;
2139 nsb->st_ino = sb->st_ino;
2140 nsb->st_mode = sb->st_mode;
2141 nsb->st_nlink = sb->st_nlink;
2142 nsb->st_uid = sb->st_uid;
2143 nsb->st_gid = sb->st_gid;
2144 nsb->st_rdev = sb->st_rdev;
2145 nsb->st_atimespec = sb->st_atimespec;
2146 nsb->st_mtimespec = sb->st_mtimespec;
2147 nsb->st_ctimespec = sb->st_ctimespec;
2148 nsb->st_size = sb->st_size;
2149 nsb->st_blocks = sb->st_blocks;
2150 nsb->st_blksize = sb->st_blksize;
2151 nsb->st_flags = sb->st_flags;
2152 nsb->st_gen = sb->st_gen;
2153 nsb->st_birthtimespec = sb->st_birthtimespec;
2156 #ifndef _SYS_SYSPROTO_H_
2165 register struct nstat_args /* {
2174 error = kern_stat(td, uap->path, UIO_USERSPACE, &sb);
2177 cvtnstat(&sb, &nsb);
2178 error = copyout(&nsb, uap->ub, sizeof (nsb));
2183 * NetBSD lstat. Get file status; this version does not follow links.
2185 #ifndef _SYS_SYSPROTO_H_
2194 register struct nlstat_args /* {
2203 error = kern_lstat(td, uap->path, UIO_USERSPACE, &sb);
2206 cvtnstat(&sb, &nsb);
2207 error = copyout(&nsb, uap->ub, sizeof (nsb));
2212 * Get configurable pathname variables.
2214 #ifndef _SYS_SYSPROTO_H_
2215 struct pathconf_args {
2223 register struct pathconf_args /* {
2229 return (kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name));
2233 kern_pathconf(struct thread *td, char *path, enum uio_seg pathseg, int name)
2235 struct nameidata nd;
2236 int error, vfslocked;
2238 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | MPSAFE, pathseg, path, td);
2239 if ((error = namei(&nd)) != 0)
2241 vfslocked = NDHASGIANT(&nd);
2242 NDFREE(&nd, NDF_ONLY_PNBUF);
2244 /* If asynchronous I/O is available, it works for all files. */
2245 if (name == _PC_ASYNC_IO)
2246 td->td_retval[0] = async_io_version;
2248 error = VOP_PATHCONF(nd.ni_vp, name, td->td_retval);
2250 VFS_UNLOCK_GIANT(vfslocked);
2255 * Return target name of a symbolic link.
2257 #ifndef _SYS_SYSPROTO_H_
2258 struct readlink_args {
2267 register struct readlink_args /* {
2274 return (kern_readlink(td, uap->path, UIO_USERSPACE, uap->buf,
2275 UIO_USERSPACE, uap->count));
2279 kern_readlink(struct thread *td, char *path, enum uio_seg pathseg, char *buf,
2280 enum uio_seg bufseg, int count)
2282 register struct vnode *vp;
2286 struct nameidata nd;
2289 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | MPSAFE, pathseg, path, td);
2290 if ((error = namei(&nd)) != 0)
2292 NDFREE(&nd, NDF_ONLY_PNBUF);
2293 vfslocked = NDHASGIANT(&nd);
2296 error = mac_check_vnode_readlink(td->td_ucred, vp);
2299 VFS_UNLOCK_GIANT(vfslocked);
2303 if (vp->v_type != VLNK)
2306 aiov.iov_base = buf;
2307 aiov.iov_len = count;
2308 auio.uio_iov = &aiov;
2309 auio.uio_iovcnt = 1;
2310 auio.uio_offset = 0;
2311 auio.uio_rw = UIO_READ;
2312 auio.uio_segflg = bufseg;
2314 auio.uio_resid = count;
2315 error = VOP_READLINK(vp, &auio, td->td_ucred);
2318 VFS_UNLOCK_GIANT(vfslocked);
2319 td->td_retval[0] = count - auio.uio_resid;
2324 * Common implementation code for chflags() and fchflags().
2327 setfflags(td, vp, flags)
2337 * Prevent non-root users from setting flags on devices. When
2338 * a device is reused, users can retain ownership of the device
2339 * if they are allowed to set flags and programs assume that
2340 * chown can't fail when done as root.
2342 if (vp->v_type == VCHR || vp->v_type == VBLK) {
2343 error = suser_cred(td->td_ucred, SUSER_ALLOWJAIL);
2348 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2350 VOP_LEASE(vp, td, td->td_ucred, LEASE_WRITE);
2351 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2353 vattr.va_flags = flags;
2355 error = mac_check_vnode_setflags(td->td_ucred, vp, vattr.va_flags);
2358 error = VOP_SETATTR(vp, &vattr, td->td_ucred, td);
2359 VOP_UNLOCK(vp, 0, td);
2360 vn_finished_write(mp);
2365 * Change flags of a file given a path name.
2367 #ifndef _SYS_SYSPROTO_H_
2368 struct chflags_args {
2376 register struct chflags_args /* {
2382 struct nameidata nd;
2385 NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE, UIO_USERSPACE, uap->path, td);
2386 if ((error = namei(&nd)) != 0)
2388 NDFREE(&nd, NDF_ONLY_PNBUF);
2389 vfslocked = NDHASGIANT(&nd);
2390 error = setfflags(td, nd.ni_vp, uap->flags);
2392 VFS_UNLOCK_GIANT(vfslocked);
2397 * Same as chflags() but doesn't follow symlinks.
2402 register struct lchflags_args /* {
2408 struct nameidata nd;
2411 NDINIT(&nd, LOOKUP, NOFOLLOW | MPSAFE, UIO_USERSPACE, uap->path, td);
2412 if ((error = namei(&nd)) != 0)
2414 vfslocked = NDHASGIANT(&nd);
2415 NDFREE(&nd, NDF_ONLY_PNBUF);
2416 error = setfflags(td, nd.ni_vp, uap->flags);
2418 VFS_UNLOCK_GIANT(vfslocked);
2423 * Change flags of a file given a file descriptor.
2425 #ifndef _SYS_SYSPROTO_H_
2426 struct fchflags_args {
2434 register struct fchflags_args /* {
2443 if ((error = getvnode(td->td_proc->p_fd, uap->fd, &fp)) != 0)
2445 vfslocked = VFS_LOCK_GIANT(fp->f_vnode->v_mount);
2446 error = setfflags(td, fp->f_vnode, uap->flags);
2447 VFS_UNLOCK_GIANT(vfslocked);
2453 * Common implementation code for chmod(), lchmod() and fchmod().
2456 setfmode(td, vp, mode)
2465 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2467 VOP_LEASE(vp, td, td->td_ucred, LEASE_WRITE);
2468 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2470 vattr.va_mode = mode & ALLPERMS;
2472 error = mac_check_vnode_setmode(td->td_ucred, vp, vattr.va_mode);
2475 error = VOP_SETATTR(vp, &vattr, td->td_ucred, td);
2476 VOP_UNLOCK(vp, 0, td);
2477 vn_finished_write(mp);
2482 * Change mode of a file given path name.
2484 #ifndef _SYS_SYSPROTO_H_
2493 register struct chmod_args /* {
2499 return (kern_chmod(td, uap->path, UIO_USERSPACE, uap->mode));
2503 kern_chmod(struct thread *td, char *path, enum uio_seg pathseg, int mode)
2506 struct nameidata nd;
2509 NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE, pathseg, path, td);
2510 if ((error = namei(&nd)) != 0)
2512 vfslocked = NDHASGIANT(&nd);
2513 NDFREE(&nd, NDF_ONLY_PNBUF);
2514 error = setfmode(td, nd.ni_vp, mode);
2516 VFS_UNLOCK_GIANT(vfslocked);
2521 * Change mode of a file given path name (don't follow links.)
2523 #ifndef _SYS_SYSPROTO_H_
2524 struct lchmod_args {
2532 register struct lchmod_args /* {
2538 struct nameidata nd;
2541 NDINIT(&nd, LOOKUP, NOFOLLOW | MPSAFE, UIO_USERSPACE, uap->path, td);
2542 if ((error = namei(&nd)) != 0)
2544 vfslocked = NDHASGIANT(&nd);
2545 NDFREE(&nd, NDF_ONLY_PNBUF);
2546 error = setfmode(td, nd.ni_vp, uap->mode);
2548 VFS_UNLOCK_GIANT(vfslocked);
2553 * Change mode of a file given a file descriptor.
2555 #ifndef _SYS_SYSPROTO_H_
2556 struct fchmod_args {
2564 register struct fchmod_args /* {
2573 if ((error = getvnode(td->td_proc->p_fd, uap->fd, &fp)) != 0)
2575 vfslocked = VFS_LOCK_GIANT(fp->f_vnode->v_mount);
2576 error = setfmode(td, fp->f_vnode, uap->mode);
2577 VFS_UNLOCK_GIANT(vfslocked);
2583 * Common implementation for chown(), lchown(), and fchown()
2586 setfown(td, vp, uid, gid)
2596 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2598 VOP_LEASE(vp, td, td->td_ucred, LEASE_WRITE);
2599 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2604 error = mac_check_vnode_setowner(td->td_ucred, vp, vattr.va_uid,
2608 error = VOP_SETATTR(vp, &vattr, td->td_ucred, td);
2609 VOP_UNLOCK(vp, 0, td);
2610 vn_finished_write(mp);
2615 * Set ownership given a path name.
2617 #ifndef _SYS_SYSPROTO_H_
2627 register struct chown_args /* {
2634 return (kern_chown(td, uap->path, UIO_USERSPACE, uap->uid, uap->gid));
2638 kern_chown(struct thread *td, char *path, enum uio_seg pathseg, int uid,
2642 struct nameidata nd;
2645 NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE, pathseg, path, td);
2646 if ((error = namei(&nd)) != 0)
2648 vfslocked = NDHASGIANT(&nd);
2649 NDFREE(&nd, NDF_ONLY_PNBUF);
2650 error = setfown(td, nd.ni_vp, uid, gid);
2652 VFS_UNLOCK_GIANT(vfslocked);
2657 * Set ownership given a path name, do not cross symlinks.
2659 #ifndef _SYS_SYSPROTO_H_
2660 struct lchown_args {
2669 register struct lchown_args /* {
2676 return (kern_lchown(td, uap->path, UIO_USERSPACE, uap->uid, uap->gid));
2680 kern_lchown(struct thread *td, char *path, enum uio_seg pathseg, int uid,
2684 struct nameidata nd;
2687 NDINIT(&nd, LOOKUP, NOFOLLOW | MPSAFE, pathseg, path, td);
2688 if ((error = namei(&nd)) != 0)
2690 vfslocked = NDHASGIANT(&nd);
2691 NDFREE(&nd, NDF_ONLY_PNBUF);
2692 error = setfown(td, nd.ni_vp, uid, gid);
2694 VFS_UNLOCK_GIANT(vfslocked);
2699 * Set ownership given a file descriptor.
2701 #ifndef _SYS_SYSPROTO_H_
2702 struct fchown_args {
2711 register struct fchown_args /* {
2721 if ((error = getvnode(td->td_proc->p_fd, uap->fd, &fp)) != 0)
2723 vfslocked = VFS_LOCK_GIANT(fp->f_vnode->v_mount);
2724 error = setfown(td, fp->f_vnode, uap->uid, uap->gid);
2725 VFS_UNLOCK_GIANT(vfslocked);
2731 * Common implementation code for utimes(), lutimes(), and futimes().
2734 getutimes(usrtvp, tvpseg, tsp)
2735 const struct timeval *usrtvp;
2736 enum uio_seg tvpseg;
2737 struct timespec *tsp;
2739 struct timeval tv[2];
2740 const struct timeval *tvp;
2743 if (usrtvp == NULL) {
2745 TIMEVAL_TO_TIMESPEC(&tv[0], &tsp[0]);
2748 if (tvpseg == UIO_SYSSPACE) {
2751 if ((error = copyin(usrtvp, tv, sizeof(tv))) != 0)
2756 if (tvp[0].tv_usec < 0 || tvp[0].tv_usec >= 1000000 ||
2757 tvp[1].tv_usec < 0 || tvp[1].tv_usec >= 1000000)
2759 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
2760 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
2766 * Common implementation code for utimes(), lutimes(), and futimes().
2769 setutimes(td, vp, ts, numtimes, nullflag)
2772 const struct timespec *ts;
2776 int error, setbirthtime;
2780 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2782 VOP_LEASE(vp, td, td->td_ucred, LEASE_WRITE);
2783 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2785 if (numtimes < 3 && VOP_GETATTR(vp, &vattr, td->td_ucred, td) == 0 &&
2786 timespeccmp(&ts[1], &vattr.va_birthtime, < ))
2789 vattr.va_atime = ts[0];
2790 vattr.va_mtime = ts[1];
2792 vattr.va_birthtime = ts[1];
2794 vattr.va_birthtime = ts[2];
2796 vattr.va_vaflags |= VA_UTIMES_NULL;
2798 error = mac_check_vnode_setutimes(td->td_ucred, vp, vattr.va_atime,
2802 error = VOP_SETATTR(vp, &vattr, td->td_ucred, td);
2803 VOP_UNLOCK(vp, 0, td);
2804 vn_finished_write(mp);
2809 * Set the access and modification times of a file.
2811 #ifndef _SYS_SYSPROTO_H_
2812 struct utimes_args {
2814 struct timeval *tptr;
2820 register struct utimes_args /* {
2822 struct timeval *tptr;
2826 return (kern_utimes(td, uap->path, UIO_USERSPACE, uap->tptr,
2831 kern_utimes(struct thread *td, char *path, enum uio_seg pathseg,
2832 struct timeval *tptr, enum uio_seg tptrseg)
2834 struct timespec ts[2];
2836 struct nameidata nd;
2839 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
2841 NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE, pathseg, path, td);
2842 if ((error = namei(&nd)) != 0)
2844 vfslocked = NDHASGIANT(&nd);
2845 NDFREE(&nd, NDF_ONLY_PNBUF);
2846 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
2848 VFS_UNLOCK_GIANT(vfslocked);
2853 * Set the access and modification times of a file.
2855 #ifndef _SYS_SYSPROTO_H_
2856 struct lutimes_args {
2858 struct timeval *tptr;
2864 register struct lutimes_args /* {
2866 struct timeval *tptr;
2870 return (kern_lutimes(td, uap->path, UIO_USERSPACE, uap->tptr,
2875 kern_lutimes(struct thread *td, char *path, enum uio_seg pathseg,
2876 struct timeval *tptr, enum uio_seg tptrseg)
2878 struct timespec ts[2];
2880 struct nameidata nd;
2883 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
2885 NDINIT(&nd, LOOKUP, NOFOLLOW | MPSAFE, pathseg, path, td);
2886 if ((error = namei(&nd)) != 0)
2888 vfslocked = NDHASGIANT(&nd);
2889 NDFREE(&nd, NDF_ONLY_PNBUF);
2890 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
2892 VFS_UNLOCK_GIANT(vfslocked);
2897 * Set the access and modification times of a file.
2899 #ifndef _SYS_SYSPROTO_H_
2900 struct futimes_args {
2902 struct timeval *tptr;
2908 register struct futimes_args /* {
2910 struct timeval *tptr;
2914 return (kern_futimes(td, uap->fd, uap->tptr, UIO_USERSPACE));
2918 kern_futimes(struct thread *td, int fd, struct timeval *tptr,
2919 enum uio_seg tptrseg)
2921 struct timespec ts[2];
2926 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
2928 if ((error = getvnode(td->td_proc->p_fd, fd, &fp)) != 0)
2930 vfslocked = VFS_LOCK_GIANT(fp->f_vnode->v_mount);
2931 error = setutimes(td, fp->f_vnode, ts, 2, tptr == NULL);
2932 VFS_UNLOCK_GIANT(vfslocked);
2938 * Truncate a file given its path name.
2940 #ifndef _SYS_SYSPROTO_H_
2941 struct truncate_args {
2950 register struct truncate_args /* {
2957 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
2961 kern_truncate(struct thread *td, char *path, enum uio_seg pathseg, off_t length)
2967 struct nameidata nd;
2972 NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE, pathseg, path, td);
2973 if ((error = namei(&nd)) != 0)
2975 vfslocked = NDHASGIANT(&nd);
2977 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0) {
2979 VFS_UNLOCK_GIANT(vfslocked);
2982 NDFREE(&nd, NDF_ONLY_PNBUF);
2983 VOP_LEASE(vp, td, td->td_ucred, LEASE_WRITE);
2984 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2985 if (vp->v_type == VDIR)
2988 else if ((error = mac_check_vnode_write(td->td_ucred, NOCRED, vp))) {
2991 else if ((error = vn_writechk(vp)) == 0 &&
2992 (error = VOP_ACCESS(vp, VWRITE, td->td_ucred, td)) == 0) {
2994 vattr.va_size = length;
2995 error = VOP_SETATTR(vp, &vattr, td->td_ucred, td);
2998 vn_finished_write(mp);
2999 VFS_UNLOCK_GIANT(vfslocked);
3004 * Truncate a file given a file descriptor.
3006 #ifndef _SYS_SYSPROTO_H_
3007 struct ftruncate_args {
3016 register struct ftruncate_args /* {
3029 if (uap->length < 0)
3031 if ((error = getvnode(td->td_proc->p_fd, uap->fd, &fp)) != 0)
3033 if ((fp->f_flag & FWRITE) == 0) {
3038 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
3039 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
3041 VOP_LEASE(vp, td, td->td_ucred, LEASE_WRITE);
3042 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
3043 if (vp->v_type == VDIR)
3046 else if ((error = mac_check_vnode_write(td->td_ucred, fp->f_cred,
3050 else if ((error = vn_writechk(vp)) == 0) {
3052 vattr.va_size = uap->length;
3053 error = VOP_SETATTR(vp, &vattr, fp->f_cred, td);
3055 VOP_UNLOCK(vp, 0, td);
3056 vn_finished_write(mp);
3058 VFS_UNLOCK_GIANT(vfslocked);
3063 #if defined(COMPAT_43)
3065 * Truncate a file given its path name.
3067 #ifndef _SYS_SYSPROTO_H_
3068 struct otruncate_args {
3076 register struct otruncate_args /* {
3081 struct truncate_args /* {
3087 nuap.path = uap->path;
3088 nuap.length = uap->length;
3089 return (truncate(td, &nuap));
3093 * Truncate a file given a file descriptor.
3095 #ifndef _SYS_SYSPROTO_H_
3096 struct oftruncate_args {
3104 register struct oftruncate_args /* {
3109 struct ftruncate_args /* {
3116 nuap.length = uap->length;
3117 return (ftruncate(td, &nuap));
3119 #endif /* COMPAT_43 */
3122 * Sync an open file.
3124 #ifndef _SYS_SYSPROTO_H_
3132 struct fsync_args /* {
3142 if ((error = getvnode(td->td_proc->p_fd, uap->fd, &fp)) != 0)
3145 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
3146 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
3148 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
3149 if (vp->v_object != NULL) {
3150 VM_OBJECT_LOCK(vp->v_object);
3151 vm_object_page_clean(vp->v_object, 0, 0, 0);
3152 VM_OBJECT_UNLOCK(vp->v_object);
3154 error = VOP_FSYNC(vp, MNT_WAIT, td);
3156 VOP_UNLOCK(vp, 0, td);
3157 vn_finished_write(mp);
3159 VFS_UNLOCK_GIANT(vfslocked);
3165 * Rename files. Source and destination must either both be directories,
3166 * or both not be directories. If target is a directory, it must be empty.
3168 #ifndef _SYS_SYSPROTO_H_
3169 struct rename_args {
3177 register struct rename_args /* {
3183 return (kern_rename(td, uap->from, uap->to, UIO_USERSPACE));
3187 kern_rename(struct thread *td, char *from, char *to, enum uio_seg pathseg)
3189 struct mount *mp = NULL;
3190 struct vnode *tvp, *fvp, *tdvp;
3191 struct nameidata fromnd, tond;
3198 NDINIT(&fromnd, DELETE, LOCKPARENT | LOCKLEAF | SAVESTART | MPSAFE,
3201 NDINIT(&fromnd, DELETE, WANTPARENT | SAVESTART | MPSAFE,
3204 if ((error = namei(&fromnd)) != 0)
3206 fvfslocked = NDHASGIANT(&fromnd);
3209 error = mac_check_vnode_rename_from(td->td_ucred, fromnd.ni_dvp,
3210 fromnd.ni_vp, &fromnd.ni_cnd);
3211 VOP_UNLOCK(fromnd.ni_dvp, 0, td);
3212 VOP_UNLOCK(fromnd.ni_vp, 0, td);
3216 error = vn_start_write(fvp, &mp, V_WAIT | PCATCH);
3218 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3219 vrele(fromnd.ni_dvp);
3223 NDINIT(&tond, RENAME, LOCKPARENT | LOCKLEAF | NOCACHE | SAVESTART |
3224 MPSAFE, pathseg, to, td);
3225 if (fromnd.ni_vp->v_type == VDIR)
3226 tond.ni_cnd.cn_flags |= WILLBEDIR;
3227 if ((error = namei(&tond)) != 0) {
3228 /* Translate error code for rename("dir1", "dir2/."). */
3229 if (error == EISDIR && fvp->v_type == VDIR)
3231 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3232 vrele(fromnd.ni_dvp);
3234 vn_finished_write(mp);
3237 tvfslocked = NDHASGIANT(&tond);
3241 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
3244 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
3252 * If the source is the same as the destination (that is, if they
3253 * are links to the same vnode), then there is nothing to do.
3259 error = mac_check_vnode_rename_to(td->td_ucred, tdvp,
3260 tond.ni_vp, fromnd.ni_dvp == tdvp, &tond.ni_cnd);
3264 VOP_LEASE(tdvp, td, td->td_ucred, LEASE_WRITE);
3265 if (fromnd.ni_dvp != tdvp) {
3266 VOP_LEASE(fromnd.ni_dvp, td, td->td_ucred, LEASE_WRITE);
3269 VOP_LEASE(tvp, td, td->td_ucred, LEASE_WRITE);
3271 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
3272 tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
3273 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3274 NDFREE(&tond, NDF_ONLY_PNBUF);
3276 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3277 NDFREE(&tond, NDF_ONLY_PNBUF);
3284 vrele(fromnd.ni_dvp);
3287 vrele(tond.ni_startdir);
3288 vn_finished_write(mp);
3290 if (fromnd.ni_startdir)
3291 vrele(fromnd.ni_startdir);
3292 VFS_UNLOCK_GIANT(fvfslocked);
3293 VFS_UNLOCK_GIANT(tvfslocked);
3300 * Make a directory file.
3302 #ifndef _SYS_SYSPROTO_H_
3311 register struct mkdir_args /* {
3317 return (kern_mkdir(td, uap->path, UIO_USERSPACE, uap->mode));
3321 kern_mkdir(struct thread *td, char *path, enum uio_seg segflg, int mode)
3327 struct nameidata nd;
3332 NDINIT(&nd, CREATE, LOCKPARENT | SAVENAME | MPSAFE, segflg, path, td);
3333 nd.ni_cnd.cn_flags |= WILLBEDIR;
3334 if ((error = namei(&nd)) != 0)
3336 vfslocked = NDHASGIANT(&nd);
3339 NDFREE(&nd, NDF_ONLY_PNBUF);
3341 * XXX namei called with LOCKPARENT but not LOCKLEAF has
3342 * the strange behaviour of leaving the vnode unlocked
3343 * if the target is the same vnode as the parent.
3345 if (vp == nd.ni_dvp)
3350 VFS_UNLOCK_GIANT(vfslocked);
3353 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3354 NDFREE(&nd, NDF_ONLY_PNBUF);
3356 VFS_UNLOCK_GIANT(vfslocked);
3357 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3362 vattr.va_type = VDIR;
3363 FILEDESC_LOCK_FAST(td->td_proc->p_fd);
3364 vattr.va_mode = (mode & ACCESSPERMS) &~ td->td_proc->p_fd->fd_cmask;
3365 FILEDESC_UNLOCK_FAST(td->td_proc->p_fd);
3367 error = mac_check_vnode_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
3372 VOP_LEASE(nd.ni_dvp, td, td->td_ucred, LEASE_WRITE);
3373 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
3377 NDFREE(&nd, NDF_ONLY_PNBUF);
3381 vn_finished_write(mp);
3382 VFS_UNLOCK_GIANT(vfslocked);
3387 * Remove a directory file.
3389 #ifndef _SYS_SYSPROTO_H_
3397 struct rmdir_args /* {
3402 return (kern_rmdir(td, uap->path, UIO_USERSPACE));
3406 kern_rmdir(struct thread *td, char *path, enum uio_seg pathseg)
3411 struct nameidata nd;
3416 NDINIT(&nd, DELETE, LOCKPARENT | LOCKLEAF | MPSAFE, pathseg, path, td);
3417 if ((error = namei(&nd)) != 0)
3419 vfslocked = NDHASGIANT(&nd);
3421 if (vp->v_type != VDIR) {
3426 * No rmdir "." please.
3428 if (nd.ni_dvp == vp) {
3433 * The root of a mounted filesystem cannot be deleted.
3435 if (vp->v_vflag & VV_ROOT) {
3440 error = mac_check_vnode_delete(td->td_ucred, nd.ni_dvp, vp,
3445 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3446 NDFREE(&nd, NDF_ONLY_PNBUF);
3448 if (nd.ni_dvp == vp)
3452 VFS_UNLOCK_GIANT(vfslocked);
3453 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3457 VOP_LEASE(nd.ni_dvp, td, td->td_ucred, LEASE_WRITE);
3458 VOP_LEASE(vp, td, td->td_ucred, LEASE_WRITE);
3459 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
3460 vn_finished_write(mp);
3462 NDFREE(&nd, NDF_ONLY_PNBUF);
3464 if (nd.ni_dvp == vp)
3468 VFS_UNLOCK_GIANT(vfslocked);
3474 * Read a block of directory entries in a filesystem independent format.
3476 #ifndef _SYS_SYSPROTO_H_
3477 struct ogetdirentries_args {
3485 ogetdirentries(td, uap)
3487 register struct ogetdirentries_args /* {
3496 struct uio auio, kuio;
3497 struct iovec aiov, kiov;
3498 struct dirent *dp, *edp;
3500 int error, eofflag, readcnt;
3503 /* XXX arbitrary sanity limit on `count'. */
3504 if (uap->count > 64 * 1024)
3506 if ((error = getvnode(td->td_proc->p_fd, uap->fd, &fp)) != 0)
3508 if ((fp->f_flag & FREAD) == 0) {
3514 if (vp->v_type != VDIR) {
3518 aiov.iov_base = uap->buf;
3519 aiov.iov_len = uap->count;
3520 auio.uio_iov = &aiov;
3521 auio.uio_iovcnt = 1;
3522 auio.uio_rw = UIO_READ;
3523 auio.uio_segflg = UIO_USERSPACE;
3525 auio.uio_resid = uap->count;
3526 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
3527 loff = auio.uio_offset = fp->f_offset;
3529 error = mac_check_vnode_readdir(td->td_ucred, vp);
3531 VOP_UNLOCK(vp, 0, td);
3536 # if (BYTE_ORDER != LITTLE_ENDIAN)
3537 if (vp->v_mount->mnt_maxsymlinklen <= 0) {
3538 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag,
3540 fp->f_offset = auio.uio_offset;
3545 kuio.uio_iov = &kiov;
3546 kuio.uio_segflg = UIO_SYSSPACE;
3547 kiov.iov_len = uap->count;
3548 MALLOC(dirbuf, caddr_t, uap->count, M_TEMP, M_WAITOK);
3549 kiov.iov_base = dirbuf;
3550 error = VOP_READDIR(vp, &kuio, fp->f_cred, &eofflag,
3552 fp->f_offset = kuio.uio_offset;
3554 readcnt = uap->count - kuio.uio_resid;
3555 edp = (struct dirent *)&dirbuf[readcnt];
3556 for (dp = (struct dirent *)dirbuf; dp < edp; ) {
3557 # if (BYTE_ORDER == LITTLE_ENDIAN)
3559 * The expected low byte of
3560 * dp->d_namlen is our dp->d_type.
3561 * The high MBZ byte of dp->d_namlen
3562 * is our dp->d_namlen.
3564 dp->d_type = dp->d_namlen;
3568 * The dp->d_type is the high byte
3569 * of the expected dp->d_namlen,
3570 * so must be zero'ed.
3574 if (dp->d_reclen > 0) {
3575 dp = (struct dirent *)
3576 ((char *)dp + dp->d_reclen);
3583 error = uiomove(dirbuf, readcnt, &auio);
3585 FREE(dirbuf, M_TEMP);
3587 VOP_UNLOCK(vp, 0, td);
3592 if (uap->count == auio.uio_resid) {
3593 if (union_dircheckp) {
3594 error = union_dircheckp(td, &vp, fp);
3603 * XXX We could delay dropping the lock above but
3604 * union_dircheckp complicates things.
3606 vn_lock(vp, LK_EXCLUSIVE|LK_RETRY, td);
3607 if ((vp->v_vflag & VV_ROOT) &&
3608 (vp->v_mount->mnt_flag & MNT_UNION)) {
3609 struct vnode *tvp = vp;
3610 vp = vp->v_mount->mnt_vnodecovered;
3618 VOP_UNLOCK(vp, 0, td);
3620 error = copyout(&loff, uap->basep, sizeof(long));
3622 td->td_retval[0] = uap->count - auio.uio_resid;
3625 #endif /* COMPAT_43 */
3628 * Read a block of directory entries in a filesystem independent format.
3630 #ifndef _SYS_SYSPROTO_H_
3631 struct getdirentries_args {
3639 getdirentries(td, uap)
3641 register struct getdirentries_args /* {
3656 if ((error = getvnode(td->td_proc->p_fd, uap->fd, &fp)) != 0)
3658 if ((fp->f_flag & FREAD) == 0) {
3664 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
3665 if (vp->v_type != VDIR) {
3669 aiov.iov_base = uap->buf;
3670 aiov.iov_len = uap->count;
3671 auio.uio_iov = &aiov;
3672 auio.uio_iovcnt = 1;
3673 auio.uio_rw = UIO_READ;
3674 auio.uio_segflg = UIO_USERSPACE;
3676 auio.uio_resid = uap->count;
3677 /* vn_lock(vp, LK_SHARED | LK_RETRY, td); */
3678 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
3679 loff = auio.uio_offset = fp->f_offset;
3681 error = mac_check_vnode_readdir(td->td_ucred, vp);
3684 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL,
3686 fp->f_offset = auio.uio_offset;
3687 VOP_UNLOCK(vp, 0, td);
3690 if (uap->count == auio.uio_resid) {
3691 if (union_dircheckp) {
3692 error = union_dircheckp(td, &vp, fp);
3694 VFS_UNLOCK_GIANT(vfslocked);
3701 * XXX We could delay dropping the lock above but
3702 * union_dircheckp complicates things.
3704 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
3705 if ((vp->v_vflag & VV_ROOT) &&
3706 (vp->v_mount->mnt_flag & MNT_UNION)) {
3707 struct vnode *tvp = vp;
3708 vp = vp->v_mount->mnt_vnodecovered;
3714 VFS_UNLOCK_GIANT(vfslocked);
3717 VOP_UNLOCK(vp, 0, td);
3719 if (uap->basep != NULL) {
3720 error = copyout(&loff, uap->basep, sizeof(long));
3722 td->td_retval[0] = uap->count - auio.uio_resid;
3724 VFS_UNLOCK_GIANT(vfslocked);
3728 #ifndef _SYS_SYSPROTO_H_
3729 struct getdents_args {
3738 register struct getdents_args /* {
3744 struct getdirentries_args ap;
3747 ap.count = uap->count;
3749 return (getdirentries(td, &ap));
3753 * Set the mode mask for creation of filesystem nodes.
3757 #ifndef _SYS_SYSPROTO_H_
3765 struct umask_args /* {
3769 register struct filedesc *fdp;
3771 FILEDESC_LOCK_FAST(td->td_proc->p_fd);
3772 fdp = td->td_proc->p_fd;
3773 td->td_retval[0] = fdp->fd_cmask;
3774 fdp->fd_cmask = uap->newmask & ALLPERMS;
3775 FILEDESC_UNLOCK_FAST(td->td_proc->p_fd);
3780 * Void all references to file by ripping underlying filesystem
3783 #ifndef _SYS_SYSPROTO_H_
3784 struct revoke_args {
3791 register struct revoke_args /* {
3798 struct nameidata nd;
3801 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | MPSAFE, UIO_USERSPACE,
3803 if ((error = namei(&nd)) != 0)
3805 vfslocked = NDHASGIANT(&nd);
3807 NDFREE(&nd, NDF_ONLY_PNBUF);
3808 if (vp->v_type != VCHR) {
3813 error = mac_check_vnode_revoke(td->td_ucred, vp);
3817 error = VOP_GETATTR(vp, &vattr, td->td_ucred, td);
3820 if (td->td_ucred->cr_uid != vattr.va_uid) {
3821 error = suser_cred(td->td_ucred, SUSER_ALLOWJAIL);
3826 VOP_REVOKE(vp, REVOKEALL);
3829 VFS_UNLOCK_GIANT(vfslocked);
3834 * Convert a user file descriptor to a kernel file entry.
3835 * A reference on the file entry is held upon returning.
3838 getvnode(fdp, fd, fpp)
3839 struct filedesc *fdp;
3851 if ((u_int)fd >= fdp->fd_nfiles ||
3852 (fp = fdp->fd_ofiles[fd]) == NULL)
3854 else if (fp->f_vnode == NULL) {
3861 FILEDESC_UNLOCK(fdp);
3868 * Get (NFS) file handle
3870 #ifndef _SYS_SYSPROTO_H_
3871 struct lgetfh_args {
3879 register struct lgetfh_args *uap;
3881 struct nameidata nd;
3883 register struct vnode *vp;
3890 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | MPSAFE,
3891 UIO_USERSPACE, uap->fname, td);
3895 vfslocked = NDHASGIANT(&nd);
3896 NDFREE(&nd, NDF_ONLY_PNBUF);
3898 bzero(&fh, sizeof(fh));
3899 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
3900 error = VFS_VPTOFH(vp, &fh.fh_fid);
3902 VFS_UNLOCK_GIANT(vfslocked);
3905 error = copyout(&fh, uap->fhp, sizeof (fh));
3909 #ifndef _SYS_SYSPROTO_H_
3918 register struct getfh_args *uap;
3920 struct nameidata nd;
3922 register struct vnode *vp;
3929 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | MPSAFE,
3930 UIO_USERSPACE, uap->fname, td);
3934 vfslocked = NDHASGIANT(&nd);
3935 NDFREE(&nd, NDF_ONLY_PNBUF);
3937 bzero(&fh, sizeof(fh));
3938 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
3939 error = VFS_VPTOFH(vp, &fh.fh_fid);
3941 VFS_UNLOCK_GIANT(vfslocked);
3944 error = copyout(&fh, uap->fhp, sizeof (fh));
3949 * syscall for the rpc.lockd to use to translate a NFS file handle into
3950 * an open descriptor.
3952 * warning: do not remove the suser() call or this becomes one giant
3957 #ifndef _SYS_SYSPROTO_H_
3958 struct fhopen_args {
3959 const struct fhandle *u_fhp;
3966 struct fhopen_args /* {
3967 const struct fhandle *u_fhp;
3971 struct proc *p = td->td_proc;
3976 struct vattr *vap = &vat;
3979 register struct filedesc *fdp = p->p_fd;
3980 int fmode, mode, error, type;
3987 fmode = FFLAGS(uap->flags);
3988 /* why not allow a non-read/write open for our lockd? */
3989 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
3991 error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
3994 /* find the mount point */
3996 mp = vfs_getvfs(&fhp.fh_fsid);
4001 /* now give me my vnode, it gets returned to me locked */
4002 error = VFS_FHTOVP(mp, &fhp.fh_fid, &vp);
4006 * from now on we have to make sure not
4007 * to forget about the vnode
4008 * any error that causes an abort must vput(vp)
4009 * just set error = err and 'goto bad;'.
4015 if (vp->v_type == VLNK) {
4019 if (vp->v_type == VSOCK) {
4024 if (fmode & (FWRITE | O_TRUNC)) {
4025 if (vp->v_type == VDIR) {
4029 error = vn_writechk(vp);
4036 if (fmode & O_APPEND)
4039 error = mac_check_vnode_open(td->td_ucred, vp, mode);
4044 error = VOP_ACCESS(vp, mode, td->td_ucred, td);
4048 if (fmode & O_TRUNC) {
4049 VOP_UNLOCK(vp, 0, td); /* XXX */
4050 if ((error = vn_start_write(NULL, &mp, V_WAIT | PCATCH)) != 0) {
4054 VOP_LEASE(vp, td, td->td_ucred, LEASE_WRITE);
4055 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td); /* XXX */
4058 * We don't yet have fp->f_cred, so use td->td_ucred, which
4061 error = mac_check_vnode_write(td->td_ucred, td->td_ucred, vp);
4066 error = VOP_SETATTR(vp, vap, td->td_ucred, td);
4070 vn_finished_write(mp);
4074 error = VOP_OPEN(vp, fmode, td->td_ucred, td, -1);
4082 * end of vn_open code
4085 if ((error = falloc(td, &nfp, &indx)) != 0) {
4090 /* An extra reference on `nfp' has been held for us by falloc(). */
4095 nfp->f_flag = fmode & FMASK;
4096 nfp->f_ops = &vnops;
4097 nfp->f_type = DTYPE_VNODE;
4098 if (fmode & (O_EXLOCK | O_SHLOCK)) {
4099 lf.l_whence = SEEK_SET;
4102 if (fmode & O_EXLOCK)
4103 lf.l_type = F_WRLCK;
4105 lf.l_type = F_RDLCK;
4107 if ((fmode & FNONBLOCK) == 0)
4109 VOP_UNLOCK(vp, 0, td);
4110 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf,
4113 * The lock request failed. Normally close the
4114 * descriptor but handle the case where someone might
4115 * have dup()d or close()d it when we weren't looking.
4117 fdclose(fdp, fp, indx, td);
4120 * release our private reference
4125 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
4126 fp->f_flag |= FHASLOCK;
4129 VOP_UNLOCK(vp, 0, td);
4132 td->td_retval[0] = indx;
4143 * Stat an (NFS) file handle.
4147 #ifndef _SYS_SYSPROTO_H_
4148 struct fhstat_args {
4149 struct fhandle *u_fhp;
4156 register struct fhstat_args /* {
4157 struct fhandle *u_fhp;
4170 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4174 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL) {
4178 if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp))) {
4182 error = vn_stat(vp, &sb, td->td_ucred, NOCRED, td);
4187 error = copyout(&sb, uap->sb, sizeof(sb));
4192 * Implement fstatfs() for (NFS) file handles.
4196 #ifndef _SYS_SYSPROTO_H_
4197 struct fhstatfs_args {
4198 struct fhandle *u_fhp;
4205 struct fhstatfs_args /* {
4206 struct fhandle *u_fhp;
4214 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4217 error = kern_fhstatfs(td, fh, &sf);
4220 return (copyout(&sf, uap->buf, sizeof(sf)));
4224 kern_fhstatfs(struct thread *td, fhandle_t fh, struct statfs *buf)
4235 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL) {
4239 error = VFS_FHTOVP(mp, &fh.fh_fid, &vp);
4247 error = prison_canseemount(td->td_ucred, mp);
4251 error = mac_check_mount_stat(td->td_ucred, mp);
4258 * Set these in case the underlying filesystem fails to do so.
4260 sp->f_version = STATFS_VERSION;
4261 sp->f_namemax = NAME_MAX;
4262 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
4263 error = VFS_STATFS(mp, sp, td);
4272 * Syscall to push extended attribute configuration information into the
4273 * VFS. Accepts a path, which it converts to a mountpoint, as well as
4274 * a command (int cmd), and attribute name and misc data. For now, the
4275 * attribute name is left in userspace for consumption by the VFS_op.
4276 * It will probably be changed to be copied into sysspace by the
4277 * syscall in the future, once issues with various consumers of the
4278 * attribute code have raised their hands.
4280 * Currently this is used only by UFS Extended Attributes.
4285 struct extattrctl_args /* {
4288 const char *filename;
4290 const char *attrname;
4293 struct vnode *filename_vp;
4294 struct nameidata nd;
4295 struct mount *mp, *mp_writable;
4296 char attrname[EXTATTR_MAXNAMELEN];
4297 int vfslocked, fnvfslocked, error;
4300 * uap->attrname is not always defined. We check again later when we
4301 * invoke the VFS call so as to pass in NULL there if needed.
4303 if (uap->attrname != NULL) {
4304 error = copyinstr(uap->attrname, attrname, EXTATTR_MAXNAMELEN,
4310 vfslocked = fnvfslocked = 0;
4312 * uap->filename is not always defined. If it is, grab a vnode lock,
4313 * which VFS_EXTATTRCTL() will later release.
4316 if (uap->filename != NULL) {
4317 NDINIT(&nd, LOOKUP, MPSAFE | FOLLOW | LOCKLEAF,
4318 UIO_USERSPACE, uap->filename, td);
4322 fnvfslocked = NDHASGIANT(&nd);
4323 filename_vp = nd.ni_vp;
4324 NDFREE(&nd, NDF_NO_VP_RELE | NDF_NO_VP_UNLOCK);
4327 /* uap->path is always defined. */
4328 NDINIT(&nd, LOOKUP, MPSAFE | FOLLOW, UIO_USERSPACE, uap->path, td);
4331 if (filename_vp != NULL)
4335 vfslocked = NDHASGIANT(&nd);
4336 mp = nd.ni_vp->v_mount;
4337 error = vn_start_write(nd.ni_vp, &mp_writable, V_WAIT | PCATCH);
4340 if (filename_vp != NULL)
4345 error = VFS_EXTATTRCTL(mp, uap->cmd, filename_vp, uap->attrnamespace,
4346 uap->attrname != NULL ? attrname : NULL, td);
4348 vn_finished_write(mp_writable);
4350 * VFS_EXTATTRCTL will have unlocked, but not de-ref'd,
4351 * filename_vp, so vrele it if it is defined.
4353 if (filename_vp != NULL)
4356 VFS_UNLOCK_GIANT(fnvfslocked);
4357 VFS_UNLOCK_GIANT(vfslocked);
4362 * Set a named extended attribute on a file or directory
4364 * Arguments: unlocked vnode "vp", attribute namespace "attrnamespace",
4365 * kernelspace string pointer "attrname", userspace buffer
4366 * pointer "data", buffer length "nbytes", thread "td".
4367 * Returns: 0 on success, an error number otherwise
4369 * References: vp must be a valid reference for the duration of the call
4372 extattr_set_vp(struct vnode *vp, int attrnamespace, const char *attrname,
4373 void *data, size_t nbytes, struct thread *td)
4381 VFS_ASSERT_GIANT(vp->v_mount);
4382 error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
4385 VOP_LEASE(vp, td, td->td_ucred, LEASE_WRITE);
4386 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
4388 aiov.iov_base = data;
4389 aiov.iov_len = nbytes;
4390 auio.uio_iov = &aiov;
4391 auio.uio_iovcnt = 1;
4392 auio.uio_offset = 0;
4393 if (nbytes > INT_MAX) {
4397 auio.uio_resid = nbytes;
4398 auio.uio_rw = UIO_WRITE;
4399 auio.uio_segflg = UIO_USERSPACE;
4404 error = mac_check_vnode_setextattr(td->td_ucred, vp, attrnamespace,
4410 error = VOP_SETEXTATTR(vp, attrnamespace, attrname, &auio,
4412 cnt -= auio.uio_resid;
4413 td->td_retval[0] = cnt;
4416 VOP_UNLOCK(vp, 0, td);
4417 vn_finished_write(mp);
4422 extattr_set_fd(td, uap)
4424 struct extattr_set_fd_args /* {
4427 const char *attrname;
4433 char attrname[EXTATTR_MAXNAMELEN];
4434 int vfslocked, error;
4436 error = copyinstr(uap->attrname, attrname, EXTATTR_MAXNAMELEN, NULL);
4440 error = getvnode(td->td_proc->p_fd, uap->fd, &fp);
4444 vfslocked = VFS_LOCK_GIANT(fp->f_vnode->v_mount);
4445 error = extattr_set_vp(fp->f_vnode, uap->attrnamespace,
4446 attrname, uap->data, uap->nbytes, td);
4448 VFS_UNLOCK_GIANT(vfslocked);
4454 extattr_set_file(td, uap)
4456 struct extattr_set_file_args /* {
4459 const char *attrname;
4464 struct nameidata nd;
4465 char attrname[EXTATTR_MAXNAMELEN];
4466 int vfslocked, error;
4468 error = copyinstr(uap->attrname, attrname, EXTATTR_MAXNAMELEN, NULL);
4472 NDINIT(&nd, LOOKUP, MPSAFE | FOLLOW, UIO_USERSPACE, uap->path, td);
4476 NDFREE(&nd, NDF_ONLY_PNBUF);
4478 vfslocked = NDHASGIANT(&nd);
4479 error = extattr_set_vp(nd.ni_vp, uap->attrnamespace, attrname,
4480 uap->data, uap->nbytes, td);
4483 VFS_UNLOCK_GIANT(vfslocked);
4488 extattr_set_link(td, uap)
4490 struct extattr_set_link_args /* {
4493 const char *attrname;
4498 struct nameidata nd;
4499 char attrname[EXTATTR_MAXNAMELEN];
4500 int vfslocked, error;
4502 error = copyinstr(uap->attrname, attrname, EXTATTR_MAXNAMELEN, NULL);
4506 NDINIT(&nd, LOOKUP, MPSAFE | NOFOLLOW, UIO_USERSPACE, uap->path, td);
4510 NDFREE(&nd, NDF_ONLY_PNBUF);
4512 vfslocked = NDHASGIANT(&nd);
4513 error = extattr_set_vp(nd.ni_vp, uap->attrnamespace, attrname,
4514 uap->data, uap->nbytes, td);
4517 VFS_UNLOCK_GIANT(vfslocked);
4522 * Get a named extended attribute on a file or directory
4524 * Arguments: unlocked vnode "vp", attribute namespace "attrnamespace",
4525 * kernelspace string pointer "attrname", userspace buffer
4526 * pointer "data", buffer length "nbytes", thread "td".
4527 * Returns: 0 on success, an error number otherwise
4529 * References: vp must be a valid reference for the duration of the call
4532 extattr_get_vp(struct vnode *vp, int attrnamespace, const char *attrname,
4533 void *data, size_t nbytes, struct thread *td)
4535 struct uio auio, *auiop;
4538 size_t size, *sizep;
4541 VFS_ASSERT_GIANT(vp->v_mount);
4542 VOP_LEASE(vp, td, td->td_ucred, LEASE_READ);
4543 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
4546 * Slightly unusual semantics: if the user provides a NULL data
4547 * pointer, they don't want to receive the data, just the
4548 * maximum read length.
4554 aiov.iov_base = data;
4555 aiov.iov_len = nbytes;
4556 auio.uio_iov = &aiov;
4557 auio.uio_iovcnt = 1;
4558 auio.uio_offset = 0;
4559 if (nbytes > INT_MAX) {
4563 auio.uio_resid = nbytes;
4564 auio.uio_rw = UIO_READ;
4565 auio.uio_segflg = UIO_USERSPACE;
4573 error = mac_check_vnode_getextattr(td->td_ucred, vp, attrnamespace,
4579 error = VOP_GETEXTATTR(vp, attrnamespace, attrname, auiop, sizep,
4582 if (auiop != NULL) {
4583 cnt -= auio.uio_resid;
4584 td->td_retval[0] = cnt;
4586 td->td_retval[0] = size;
4589 VOP_UNLOCK(vp, 0, td);
4594 extattr_get_fd(td, uap)
4596 struct extattr_get_fd_args /* {
4599 const char *attrname;
4605 char attrname[EXTATTR_MAXNAMELEN];
4606 int vfslocked, error;
4608 error = copyinstr(uap->attrname, attrname, EXTATTR_MAXNAMELEN, NULL);
4612 error = getvnode(td->td_proc->p_fd, uap->fd, &fp);
4616 vfslocked = VFS_LOCK_GIANT(fp->f_vnode->v_mount);
4617 error = extattr_get_vp(fp->f_vnode, uap->attrnamespace,
4618 attrname, uap->data, uap->nbytes, td);
4621 VFS_UNLOCK_GIANT(vfslocked);
4626 extattr_get_file(td, uap)
4628 struct extattr_get_file_args /* {
4631 const char *attrname;
4636 struct nameidata nd;
4637 char attrname[EXTATTR_MAXNAMELEN];
4638 int vfslocked, error;
4640 error = copyinstr(uap->attrname, attrname, EXTATTR_MAXNAMELEN, NULL);
4644 NDINIT(&nd, LOOKUP, MPSAFE | FOLLOW, UIO_USERSPACE, uap->path, td);
4648 NDFREE(&nd, NDF_ONLY_PNBUF);
4650 vfslocked = NDHASGIANT(&nd);
4651 error = extattr_get_vp(nd.ni_vp, uap->attrnamespace, attrname,
4652 uap->data, uap->nbytes, td);
4655 VFS_UNLOCK_GIANT(vfslocked);
4660 extattr_get_link(td, uap)
4662 struct extattr_get_link_args /* {
4665 const char *attrname;
4670 struct nameidata nd;
4671 char attrname[EXTATTR_MAXNAMELEN];
4672 int vfslocked, error;
4674 error = copyinstr(uap->attrname, attrname, EXTATTR_MAXNAMELEN, NULL);
4678 NDINIT(&nd, LOOKUP, MPSAFE | NOFOLLOW, UIO_USERSPACE, uap->path, td);
4682 NDFREE(&nd, NDF_ONLY_PNBUF);
4684 vfslocked = NDHASGIANT(&nd);
4685 error = extattr_get_vp(nd.ni_vp, uap->attrnamespace, attrname,
4686 uap->data, uap->nbytes, td);
4689 VFS_UNLOCK_GIANT(vfslocked);
4694 * extattr_delete_vp(): Delete a named extended attribute on a file or
4697 * Arguments: unlocked vnode "vp", attribute namespace "attrnamespace",
4698 * kernelspace string pointer "attrname", proc "p"
4699 * Returns: 0 on success, an error number otherwise
4701 * References: vp must be a valid reference for the duration of the call
4704 extattr_delete_vp(struct vnode *vp, int attrnamespace, const char *attrname,
4710 VFS_ASSERT_GIANT(vp->v_mount);
4711 error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
4714 VOP_LEASE(vp, td, td->td_ucred, LEASE_WRITE);
4715 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
4718 error = mac_check_vnode_deleteextattr(td->td_ucred, vp, attrnamespace,
4724 error = VOP_DELETEEXTATTR(vp, attrnamespace, attrname, td->td_ucred,
4726 if (error == EOPNOTSUPP)
4727 error = VOP_SETEXTATTR(vp, attrnamespace, attrname, NULL,
4732 VOP_UNLOCK(vp, 0, td);
4733 vn_finished_write(mp);
4738 extattr_delete_fd(td, uap)
4740 struct extattr_delete_fd_args /* {
4743 const char *attrname;
4747 char attrname[EXTATTR_MAXNAMELEN];
4748 int vfslocked, error;
4750 error = copyinstr(uap->attrname, attrname, EXTATTR_MAXNAMELEN, NULL);
4754 error = getvnode(td->td_proc->p_fd, uap->fd, &fp);
4758 vfslocked = VFS_LOCK_GIANT(fp->f_vnode->v_mount);
4759 error = extattr_delete_vp(fp->f_vnode, uap->attrnamespace,
4762 VFS_UNLOCK_GIANT(vfslocked);
4767 extattr_delete_file(td, uap)
4769 struct extattr_delete_file_args /* {
4772 const char *attrname;
4775 struct nameidata nd;
4776 char attrname[EXTATTR_MAXNAMELEN];
4777 int vfslocked, error;
4779 error = copyinstr(uap->attrname, attrname, EXTATTR_MAXNAMELEN, NULL);
4783 NDINIT(&nd, LOOKUP, MPSAFE | FOLLOW, UIO_USERSPACE, uap->path, td);
4787 NDFREE(&nd, NDF_ONLY_PNBUF);
4789 vfslocked = NDHASGIANT(&nd);
4790 error = extattr_delete_vp(nd.ni_vp, uap->attrnamespace, attrname, td);
4792 VFS_UNLOCK_GIANT(vfslocked);
4797 extattr_delete_link(td, uap)
4799 struct extattr_delete_link_args /* {
4802 const char *attrname;
4805 struct nameidata nd;
4806 char attrname[EXTATTR_MAXNAMELEN];
4807 int vfslocked, error;
4809 error = copyinstr(uap->attrname, attrname, EXTATTR_MAXNAMELEN, NULL);
4813 NDINIT(&nd, LOOKUP, MPSAFE | NOFOLLOW, UIO_USERSPACE, uap->path, td);
4817 NDFREE(&nd, NDF_ONLY_PNBUF);
4819 vfslocked = NDHASGIANT(&nd);
4820 error = extattr_delete_vp(nd.ni_vp, uap->attrnamespace, attrname, td);
4822 VFS_UNLOCK_GIANT(vfslocked);
4827 * Retrieve a list of extended attributes on a file or directory.
4829 * Arguments: unlocked vnode "vp", attribute namespace 'attrnamespace",
4830 * userspace buffer pointer "data", buffer length "nbytes",
4832 * Returns: 0 on success, an error number otherwise
4834 * References: vp must be a valid reference for the duration of the call
4837 extattr_list_vp(struct vnode *vp, int attrnamespace, void *data,
4838 size_t nbytes, struct thread *td)
4840 struct uio auio, *auiop;
4841 size_t size, *sizep;
4846 VFS_ASSERT_GIANT(vp->v_mount);
4847 VOP_LEASE(vp, td, td->td_ucred, LEASE_READ);
4848 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
4854 aiov.iov_base = data;
4855 aiov.iov_len = nbytes;
4856 auio.uio_iov = &aiov;
4857 auio.uio_iovcnt = 1;
4858 auio.uio_offset = 0;
4859 if (nbytes > INT_MAX) {
4863 auio.uio_resid = nbytes;
4864 auio.uio_rw = UIO_READ;
4865 auio.uio_segflg = UIO_USERSPACE;
4873 error = mac_check_vnode_listextattr(td->td_ucred, vp, attrnamespace);
4878 error = VOP_LISTEXTATTR(vp, attrnamespace, auiop, sizep,
4881 if (auiop != NULL) {
4882 cnt -= auio.uio_resid;
4883 td->td_retval[0] = cnt;
4885 td->td_retval[0] = size;
4888 VOP_UNLOCK(vp, 0, td);
4894 extattr_list_fd(td, uap)
4896 struct extattr_list_fd_args /* {
4904 int vfslocked, error;
4906 error = getvnode(td->td_proc->p_fd, uap->fd, &fp);
4910 vfslocked = VFS_LOCK_GIANT(fp->f_vnode->v_mount);
4911 error = extattr_list_vp(fp->f_vnode, uap->attrnamespace, uap->data,
4915 VFS_UNLOCK_GIANT(vfslocked);
4920 extattr_list_file(td, uap)
4922 struct extattr_list_file_args /* {
4929 struct nameidata nd;
4930 int vfslocked, error;
4932 NDINIT(&nd, LOOKUP, MPSAFE | FOLLOW, UIO_USERSPACE, uap->path, td);
4936 NDFREE(&nd, NDF_ONLY_PNBUF);
4938 vfslocked = NDHASGIANT(&nd);
4939 error = extattr_list_vp(nd.ni_vp, uap->attrnamespace, uap->data,
4943 VFS_UNLOCK_GIANT(vfslocked);
4948 extattr_list_link(td, uap)
4950 struct extattr_list_link_args /* {
4957 struct nameidata nd;
4958 int vfslocked, error;
4960 NDINIT(&nd, LOOKUP, MPSAFE | NOFOLLOW, UIO_USERSPACE, uap->path, td);
4964 NDFREE(&nd, NDF_ONLY_PNBUF);
4966 vfslocked = NDHASGIANT(&nd);
4967 error = extattr_list_vp(nd.ni_vp, uap->attrnamespace, uap->data,
4971 VFS_UNLOCK_GIANT(vfslocked);