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.
168 #ifndef _SYS_SYSPROTO_H_
169 struct quotactl_args {
179 register struct quotactl_args /* {
186 struct mount *mp, *vmp;
190 if (jailed(td->td_ucred) && !prison_quotas)
192 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, uap->path, td);
193 if ((error = namei(&nd)) != 0)
195 NDFREE(&nd, NDF_ONLY_PNBUF);
196 error = vn_start_write(nd.ni_vp, &vmp, V_WAIT | PCATCH);
197 mp = nd.ni_vp->v_mount;
201 error = VFS_QUOTACTL(mp, uap->cmd, uap->uid, uap->arg, td);
202 vn_finished_write(vmp);
207 * Get filesystem statistics.
209 #ifndef _SYS_SYSPROTO_H_
218 register struct statfs_args /* {
226 error = kern_statfs(td, uap->path, UIO_USERSPACE, &sf);
228 error = copyout(&sf, uap->buf, sizeof(sf));
233 kern_statfs(struct thread *td, char *path, enum uio_seg pathseg,
237 struct statfs *sp, sb;
242 NDINIT(&nd, LOOKUP, FOLLOW, pathseg, path, td);
243 if ((error = namei(&nd)) != 0) {
247 mp = nd.ni_vp->v_mount;
249 NDFREE(&nd, NDF_ONLY_PNBUF);
252 error = mac_check_mount_stat(td->td_ucred, mp);
259 * Set these in case the underlying filesystem fails to do so.
261 sp->f_version = STATFS_VERSION;
262 sp->f_namemax = NAME_MAX;
263 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
264 error = VFS_STATFS(mp, sp, td);
269 bcopy(sp, &sb, sizeof(sb));
270 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
278 * Get filesystem statistics.
280 #ifndef _SYS_SYSPROTO_H_
281 struct fstatfs_args {
289 register struct fstatfs_args /* {
297 error = kern_fstatfs(td, uap->fd, &sf);
299 error = copyout(&sf, uap->buf, sizeof(sf));
304 kern_fstatfs(struct thread *td, int fd, struct statfs *buf)
308 struct statfs *sp, sb;
311 if ((error = getvnode(td->td_proc->p_fd, fd, &fp)) != 0)
314 mp = fp->f_vnode->v_mount;
321 error = mac_check_mount_stat(td->td_ucred, mp);
329 * Set these in case the underlying filesystem fails to do so.
331 sp->f_version = STATFS_VERSION;
332 sp->f_namemax = NAME_MAX;
333 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
334 error = VFS_STATFS(mp, sp, td);
339 bcopy(sp, &sb, sizeof(sb));
340 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
348 * Get statistics on all filesystems.
350 #ifndef _SYS_SYSPROTO_H_
351 struct getfsstat_args {
360 register struct getfsstat_args /* {
366 struct mount *mp, *nmp;
367 struct statfs *sp, sb;
369 long count, maxcount, error;
371 maxcount = uap->bufsize / sizeof(struct statfs);
372 sfsp = (caddr_t)uap->buf;
375 mtx_lock(&mountlist_mtx);
376 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
377 if (!prison_check_mount(td->td_ucred, mp)) {
378 nmp = TAILQ_NEXT(mp, mnt_list);
382 if (mac_check_mount_stat(td->td_ucred, mp) != 0) {
383 nmp = TAILQ_NEXT(mp, mnt_list);
387 if (vfs_busy(mp, LK_NOWAIT, &mountlist_mtx, td)) {
388 nmp = TAILQ_NEXT(mp, mnt_list);
391 if (sfsp && count < maxcount) {
394 * Set these in case the underlying filesystem
397 sp->f_version = STATFS_VERSION;
398 sp->f_namemax = NAME_MAX;
399 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
401 * If MNT_NOWAIT or MNT_LAZY is specified, do not
402 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
403 * overrides MNT_WAIT.
405 if (((uap->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
406 (uap->flags & MNT_WAIT)) &&
407 (error = VFS_STATFS(mp, sp, td))) {
408 mtx_lock(&mountlist_mtx);
409 nmp = TAILQ_NEXT(mp, mnt_list);
414 bcopy(sp, &sb, sizeof(sb));
415 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
418 error = copyout(sp, sfsp, sizeof(*sp));
427 mtx_lock(&mountlist_mtx);
428 nmp = TAILQ_NEXT(mp, mnt_list);
431 mtx_unlock(&mountlist_mtx);
433 if (sfsp && count > maxcount)
434 td->td_retval[0] = maxcount;
436 td->td_retval[0] = count;
440 #ifdef COMPAT_FREEBSD4
442 * Get old format filesystem statistics.
444 static void cvtstatfs(struct thread *, struct statfs *, struct ostatfs *);
446 #ifndef _SYS_SYSPROTO_H_
447 struct freebsd4_statfs_args {
453 freebsd4_statfs(td, uap)
455 struct freebsd4_statfs_args /* {
464 error = kern_statfs(td, uap->path, UIO_USERSPACE, &sf);
467 cvtstatfs(td, &sf, &osb);
468 return (copyout(&osb, uap->buf, sizeof(osb)));
472 * Get filesystem statistics.
474 #ifndef _SYS_SYSPROTO_H_
475 struct freebsd4_fstatfs_args {
481 freebsd4_fstatfs(td, uap)
483 struct freebsd4_fstatfs_args /* {
492 error = kern_fstatfs(td, uap->fd, &sf);
495 cvtstatfs(td, &sf, &osb);
496 return (copyout(&osb, uap->buf, sizeof(osb)));
500 * Get statistics on all filesystems.
502 #ifndef _SYS_SYSPROTO_H_
503 struct freebsd4_getfsstat_args {
510 freebsd4_getfsstat(td, uap)
512 register struct freebsd4_getfsstat_args /* {
518 struct mount *mp, *nmp;
522 long count, maxcount, error;
524 maxcount = uap->bufsize / sizeof(struct ostatfs);
525 sfsp = (caddr_t)uap->buf;
527 mtx_lock(&mountlist_mtx);
528 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
529 if (!prison_check_mount(td->td_ucred, mp)) {
530 nmp = TAILQ_NEXT(mp, mnt_list);
534 if (mac_check_mount_stat(td->td_ucred, mp) != 0) {
535 nmp = TAILQ_NEXT(mp, mnt_list);
539 if (vfs_busy(mp, LK_NOWAIT, &mountlist_mtx, td)) {
540 nmp = TAILQ_NEXT(mp, mnt_list);
543 if (sfsp && count < maxcount) {
546 * If MNT_NOWAIT or MNT_LAZY is specified, do not
547 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
548 * overrides MNT_WAIT.
550 if (((uap->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
551 (uap->flags & MNT_WAIT)) &&
552 (error = VFS_STATFS(mp, sp, td))) {
553 mtx_lock(&mountlist_mtx);
554 nmp = TAILQ_NEXT(mp, mnt_list);
558 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
559 cvtstatfs(td, sp, &osb);
560 error = copyout(&osb, sfsp, sizeof(osb));
568 mtx_lock(&mountlist_mtx);
569 nmp = TAILQ_NEXT(mp, mnt_list);
572 mtx_unlock(&mountlist_mtx);
573 if (sfsp && count > maxcount)
574 td->td_retval[0] = maxcount;
576 td->td_retval[0] = count;
581 * Implement fstatfs() for (NFS) file handles.
583 #ifndef _SYS_SYSPROTO_H_
584 struct freebsd4_fhstatfs_args {
585 struct fhandle *u_fhp;
590 freebsd4_fhstatfs(td, uap)
592 struct freebsd4_fhstatfs_args /* {
593 struct fhandle *u_fhp;
602 if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
604 error = kern_fhstatfs(td, fh, &sf);
607 cvtstatfs(td, &sf, &osb);
608 return (copyout(&osb, uap->buf, sizeof(osb)));
612 * Convert a new format statfs structure to an old format statfs structure.
615 cvtstatfs(td, nsp, osp)
621 bzero(osp, sizeof(*osp));
622 osp->f_bsize = MIN(nsp->f_bsize, LONG_MAX);
623 osp->f_iosize = MIN(nsp->f_iosize, LONG_MAX);
624 osp->f_blocks = MIN(nsp->f_blocks, LONG_MAX);
625 osp->f_bfree = MIN(nsp->f_bfree, LONG_MAX);
626 osp->f_bavail = MIN(nsp->f_bavail, LONG_MAX);
627 osp->f_files = MIN(nsp->f_files, LONG_MAX);
628 osp->f_ffree = MIN(nsp->f_ffree, LONG_MAX);
629 osp->f_owner = nsp->f_owner;
630 osp->f_type = nsp->f_type;
631 osp->f_flags = nsp->f_flags;
632 osp->f_syncwrites = MIN(nsp->f_syncwrites, LONG_MAX);
633 osp->f_asyncwrites = MIN(nsp->f_asyncwrites, LONG_MAX);
634 osp->f_syncreads = MIN(nsp->f_syncreads, LONG_MAX);
635 osp->f_asyncreads = MIN(nsp->f_asyncreads, LONG_MAX);
636 bcopy(nsp->f_fstypename, osp->f_fstypename,
637 MIN(MFSNAMELEN, OMNAMELEN));
638 bcopy(nsp->f_mntonname, osp->f_mntonname,
639 MIN(MFSNAMELEN, OMNAMELEN));
640 bcopy(nsp->f_mntfromname, osp->f_mntfromname,
641 MIN(MFSNAMELEN, OMNAMELEN));
643 osp->f_fsid.val[0] = osp->f_fsid.val[1] = 0;
645 osp->f_fsid = nsp->f_fsid;
648 #endif /* COMPAT_FREEBSD4 */
651 * Change current working directory to a given file descriptor.
653 #ifndef _SYS_SYSPROTO_H_
661 struct fchdir_args /* {
665 register struct filedesc *fdp = td->td_proc->p_fd;
666 struct vnode *vp, *tdp, *vpold;
672 if ((error = getvnode(fdp, uap->fd, &fp)) != 0)
675 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
678 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
679 if (vp->v_type != VDIR)
682 else if ((error = mac_check_vnode_chdir(td->td_ucred, vp)) != 0) {
686 error = VOP_ACCESS(vp, VEXEC, td->td_ucred, td);
687 while (!error && (mp = vp->v_mountedhere) != NULL) {
689 if (vfs_busy(mp, 0, 0, td))
691 tvfslocked = VFS_LOCK_GIANT(mp);
692 error = VFS_ROOT(mp, &tdp, td);
695 VFS_UNLOCK_GIANT(tvfslocked);
699 VFS_UNLOCK_GIANT(vfslocked);
701 vfslocked = tvfslocked;
705 VFS_UNLOCK_GIANT(vfslocked);
708 VOP_UNLOCK(vp, 0, td);
709 FILEDESC_LOCK_FAST(fdp);
710 vpold = fdp->fd_cdir;
712 FILEDESC_UNLOCK_FAST(fdp);
714 VFS_UNLOCK_GIANT(vfslocked);
719 * Change current working directory (``.'').
721 #ifndef _SYS_SYSPROTO_H_
729 struct chdir_args /* {
734 return (kern_chdir(td, uap->path, UIO_USERSPACE));
738 kern_chdir(struct thread *td, char *path, enum uio_seg pathseg)
740 register struct filedesc *fdp = td->td_proc->p_fd;
746 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | MPSAFE, pathseg, path, td);
747 if ((error = namei(&nd)) != 0)
749 vfslocked = NDHASGIANT(&nd);
750 if ((error = change_dir(nd.ni_vp, td)) != 0) {
752 VFS_UNLOCK_GIANT(vfslocked);
753 NDFREE(&nd, NDF_ONLY_PNBUF);
756 VOP_UNLOCK(nd.ni_vp, 0, td);
757 NDFREE(&nd, NDF_ONLY_PNBUF);
758 FILEDESC_LOCK_FAST(fdp);
760 fdp->fd_cdir = nd.ni_vp;
761 FILEDESC_UNLOCK_FAST(fdp);
763 VFS_UNLOCK_GIANT(vfslocked);
768 * Helper function for raised chroot(2) security function: Refuse if
769 * any filedescriptors are open directories.
772 chroot_refuse_vdir_fds(fdp)
773 struct filedesc *fdp;
779 FILEDESC_LOCK_ASSERT(fdp, MA_OWNED);
780 for (fd = 0; fd < fdp->fd_nfiles ; fd++) {
781 fp = fget_locked(fdp, fd);
784 if (fp->f_type == DTYPE_VNODE) {
786 if (vp->v_type == VDIR)
794 * This sysctl determines if we will allow a process to chroot(2) if it
795 * has a directory open:
796 * 0: disallowed for all processes.
797 * 1: allowed for processes that were not already chroot(2)'ed.
798 * 2: allowed for all processes.
801 static int chroot_allow_open_directories = 1;
803 SYSCTL_INT(_kern, OID_AUTO, chroot_allow_open_directories, CTLFLAG_RW,
804 &chroot_allow_open_directories, 0, "");
807 * Change notion of root (``/'') directory.
809 #ifndef _SYS_SYSPROTO_H_
817 struct chroot_args /* {
825 error = suser_cred(td->td_ucred, SUSER_ALLOWJAIL);
828 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | MPSAFE,
829 UIO_USERSPACE, uap->path, td);
833 vfslocked = NDHASGIANT(&nd);
834 if ((error = change_dir(nd.ni_vp, td)) != 0)
837 if ((error = mac_check_vnode_chroot(td->td_ucred, nd.ni_vp)))
840 VOP_UNLOCK(nd.ni_vp, 0, td);
841 error = change_root(nd.ni_vp, td);
843 VFS_UNLOCK_GIANT(vfslocked);
844 NDFREE(&nd, NDF_ONLY_PNBUF);
848 VFS_UNLOCK_GIANT(vfslocked);
850 NDFREE(&nd, NDF_ONLY_PNBUF);
855 * Common routine for chroot and chdir. Callers must provide a locked vnode
865 ASSERT_VOP_LOCKED(vp, "change_dir(): vp not locked");
866 if (vp->v_type != VDIR)
869 error = mac_check_vnode_chdir(td->td_ucred, vp);
873 error = VOP_ACCESS(vp, VEXEC, td->td_ucred, td);
878 * Common routine for kern_chroot() and jail_attach(). The caller is
879 * responsible for invoking suser() and mac_check_chroot() to authorize this
887 struct filedesc *fdp;
891 VFS_ASSERT_GIANT(vp->v_mount);
892 fdp = td->td_proc->p_fd;
894 if (chroot_allow_open_directories == 0 ||
895 (chroot_allow_open_directories == 1 && fdp->fd_rdir != rootvnode)) {
896 error = chroot_refuse_vdir_fds(fdp);
898 FILEDESC_UNLOCK(fdp);
902 oldvp = fdp->fd_rdir;
909 FILEDESC_UNLOCK(fdp);
915 * Check permissions, allocate an open file structure,
916 * and call the device open routine if any.
920 #ifndef _SYS_SYSPROTO_H_
930 register struct open_args /* {
938 error = kern_open(td, uap->path, UIO_USERSPACE, uap->flags, uap->mode);
939 if (mtx_owned(&Giant))
940 printf("open: %s: %d\n", uap->path, error);
945 kern_open(struct thread *td, char *path, enum uio_seg pathseg, int flags,
948 struct proc *p = td->td_proc;
949 struct filedesc *fdp = p->p_fd;
956 int type, indx, error;
961 if ((flags & O_ACCMODE) == O_ACCMODE)
963 flags = FFLAGS(flags);
964 error = falloc(td, &nfp, &indx);
967 /* An extra reference on `nfp' has been held for us by falloc(). */
969 cmode = ((mode &~ fdp->fd_cmask) & ALLPERMS) &~ S_ISTXT;
970 NDINIT(&nd, LOOKUP, FOLLOW, pathseg, path, td);
971 td->td_dupfd = -1; /* XXX check for fdopen */
972 error = vn_open(&nd, &flags, cmode, indx);
975 * If the vn_open replaced the method vector, something
976 * wonderous happened deep below and we just pass it up
977 * pretending we know what we do.
979 if (error == ENXIO && fp->f_ops != &badfileops) {
981 td->td_retval[0] = indx;
986 * release our own reference
991 * handle special fdopen() case. bleh. dupfdopen() is
992 * responsible for dropping the old contents of ofiles[indx]
995 if ((error == ENODEV || error == ENXIO) &&
996 td->td_dupfd >= 0 && /* XXX from fdopen */
998 dupfdopen(td, fdp, indx, td->td_dupfd, flags, error)) == 0) {
999 td->td_retval[0] = indx;
1003 * Clean up the descriptor, but only if another thread hadn't
1004 * replaced or closed it.
1006 fdclose(fdp, fp, indx, td);
1008 if (error == ERESTART)
1013 vfslocked = NDHASGIANT(&nd);
1014 NDFREE(&nd, NDF_ONLY_PNBUF);
1018 * There should be 2 references on the file, one from the descriptor
1019 * table, and one for us.
1021 * Handle the case where someone closed the file (via its file
1022 * descriptor) while we were blocked. The end result should look
1023 * like opening the file succeeded but it was immediately closed.
1024 * We call vn_close() manually because we haven't yet hooked up
1025 * the various 'struct file' fields.
1029 if (fp->f_count == 1) {
1031 KASSERT(fdp->fd_ofiles[indx] != fp,
1032 ("Open file descriptor lost all refs"));
1034 FILEDESC_UNLOCK(fdp);
1035 VOP_UNLOCK(vp, 0, td);
1036 vn_close(vp, flags & FMASK, fp->f_cred, td);
1037 VFS_UNLOCK_GIANT(vfslocked);
1039 td->td_retval[0] = indx;
1043 if (fp->f_data == NULL)
1045 fp->f_flag = flags & FMASK;
1046 if (fp->f_ops == &badfileops)
1049 fp->f_type = (vp->v_type == VFIFO ? DTYPE_FIFO : DTYPE_VNODE);
1051 FILEDESC_UNLOCK(fdp);
1053 VOP_UNLOCK(vp, 0, td);
1054 if (flags & (O_EXLOCK | O_SHLOCK)) {
1055 lf.l_whence = SEEK_SET;
1058 if (flags & O_EXLOCK)
1059 lf.l_type = F_WRLCK;
1061 lf.l_type = F_RDLCK;
1063 if ((flags & FNONBLOCK) == 0)
1065 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf,
1068 fp->f_flag |= FHASLOCK;
1070 if (flags & O_TRUNC) {
1071 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
1073 VOP_LEASE(vp, td, td->td_ucred, LEASE_WRITE);
1076 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
1078 error = mac_check_vnode_write(td->td_ucred, fp->f_cred, vp);
1081 error = VOP_SETATTR(vp, &vat, td->td_ucred, td);
1082 VOP_UNLOCK(vp, 0, td);
1083 vn_finished_write(mp);
1087 VFS_UNLOCK_GIANT(vfslocked);
1089 * Release our private reference, leaving the one associated with
1090 * the descriptor table intact.
1093 td->td_retval[0] = indx;
1096 VFS_UNLOCK_GIANT(vfslocked);
1097 fdclose(fdp, fp, indx, td);
1108 #ifndef _SYS_SYSPROTO_H_
1109 struct ocreat_args {
1117 register struct ocreat_args /* {
1123 return (kern_open(td, uap->path, UIO_USERSPACE,
1124 O_WRONLY | O_CREAT | O_TRUNC, uap->mode));
1126 #endif /* COMPAT_43 */
1129 * Create a special file.
1131 #ifndef _SYS_SYSPROTO_H_
1141 register struct mknod_args /* {
1148 return (kern_mknod(td, uap->path, UIO_USERSPACE, uap->mode, uap->dev));
1152 kern_mknod(struct thread *td, char *path, enum uio_seg pathseg, int mode,
1160 struct nameidata nd;
1163 switch (mode & S_IFMT) {
1169 error = suser_cred(td->td_ucred, SUSER_ALLOWJAIL);
1176 NDINIT(&nd, CREATE, LOCKPARENT | SAVENAME | MPSAFE, pathseg, path, td);
1177 if ((error = namei(&nd)) != 0)
1179 vfslocked = NDHASGIANT(&nd);
1182 NDFREE(&nd, NDF_ONLY_PNBUF);
1184 if (vp == nd.ni_dvp)
1188 VFS_UNLOCK_GIANT(vfslocked);
1192 FILEDESC_LOCK_FAST(td->td_proc->p_fd);
1193 vattr.va_mode = (mode & ALLPERMS) &
1194 ~td->td_proc->p_fd->fd_cmask;
1195 FILEDESC_UNLOCK_FAST(td->td_proc->p_fd);
1196 vattr.va_rdev = dev;
1199 switch (mode & S_IFMT) {
1200 case S_IFMT: /* used by badsect to flag bad sectors */
1201 vattr.va_type = VBAD;
1204 vattr.va_type = VCHR;
1207 vattr.va_type = VBLK;
1217 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1218 NDFREE(&nd, NDF_ONLY_PNBUF);
1220 VFS_UNLOCK_GIANT(vfslocked);
1221 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1226 if (error == 0 && !whiteout)
1227 error = mac_check_vnode_create(td->td_ucred, nd.ni_dvp,
1228 &nd.ni_cnd, &vattr);
1231 VOP_LEASE(nd.ni_dvp, td, td->td_ucred, LEASE_WRITE);
1233 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, CREATE);
1235 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp,
1236 &nd.ni_cnd, &vattr);
1241 NDFREE(&nd, NDF_ONLY_PNBUF);
1243 vn_finished_write(mp);
1244 ASSERT_VOP_UNLOCKED(nd.ni_dvp, "mknod");
1245 ASSERT_VOP_UNLOCKED(nd.ni_vp, "mknod");
1246 VFS_UNLOCK_GIANT(vfslocked);
1251 * Create a named pipe.
1253 #ifndef _SYS_SYSPROTO_H_
1254 struct mkfifo_args {
1262 register struct mkfifo_args /* {
1268 return (kern_mkfifo(td, uap->path, UIO_USERSPACE, uap->mode));
1272 kern_mkfifo(struct thread *td, char *path, enum uio_seg pathseg, int mode)
1277 struct nameidata nd;
1282 NDINIT(&nd, CREATE, LOCKPARENT | SAVENAME | MPSAFE, pathseg, path, td);
1283 if ((error = namei(&nd)) != 0)
1285 vfslocked = NDHASGIANT(&nd);
1286 if (nd.ni_vp != NULL) {
1287 NDFREE(&nd, NDF_ONLY_PNBUF);
1289 if (nd.ni_vp == nd.ni_dvp)
1293 VFS_UNLOCK_GIANT(vfslocked);
1296 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1297 NDFREE(&nd, NDF_ONLY_PNBUF);
1299 VFS_UNLOCK_GIANT(vfslocked);
1300 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1305 vattr.va_type = VFIFO;
1306 FILEDESC_LOCK_FAST(td->td_proc->p_fd);
1307 vattr.va_mode = (mode & ALLPERMS) & ~td->td_proc->p_fd->fd_cmask;
1308 FILEDESC_UNLOCK_FAST(td->td_proc->p_fd);
1310 error = mac_check_vnode_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1315 VOP_LEASE(nd.ni_dvp, td, td->td_ucred, LEASE_WRITE);
1316 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
1323 vn_finished_write(mp);
1324 VFS_UNLOCK_GIANT(vfslocked);
1325 NDFREE(&nd, NDF_ONLY_PNBUF);
1330 * Make a hard file link.
1332 #ifndef _SYS_SYSPROTO_H_
1341 register struct link_args /* {
1348 error = kern_link(td, uap->path, uap->link, UIO_USERSPACE);
1352 SYSCTL_DECL(_security_bsd);
1354 static int hardlink_check_uid = 0;
1355 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
1356 &hardlink_check_uid, 0,
1357 "Unprivileged processes cannot create hard links to files owned by other "
1359 static int hardlink_check_gid = 0;
1360 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
1361 &hardlink_check_gid, 0,
1362 "Unprivileged processes cannot create hard links to files owned by other "
1366 can_hardlink(struct vnode *vp, struct thread *td, struct ucred *cred)
1371 if (suser_cred(cred, SUSER_ALLOWJAIL) == 0)
1374 if (!hardlink_check_uid && !hardlink_check_gid)
1377 error = VOP_GETATTR(vp, &va, cred, td);
1381 if (hardlink_check_uid) {
1382 if (cred->cr_uid != va.va_uid)
1386 if (hardlink_check_gid) {
1387 if (!groupmember(va.va_gid, cred))
1395 kern_link(struct thread *td, char *path, char *link, enum uio_seg segflg)
1399 struct nameidata nd;
1405 NDINIT(&nd, LOOKUP, FOLLOW | NOOBJ | MPSAFE, segflg, path, td);
1406 if ((error = namei(&nd)) != 0)
1408 vfslocked = NDHASGIANT(&nd);
1409 NDFREE(&nd, NDF_ONLY_PNBUF);
1411 if (vp->v_type == VDIR) {
1413 VFS_UNLOCK_GIANT(vfslocked);
1414 return (EPERM); /* POSIX */
1416 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0) {
1418 VFS_UNLOCK_GIANT(vfslocked);
1421 NDINIT(&nd, CREATE, LOCKPARENT | NOOBJ | SAVENAME, segflg, link, td);
1422 if ((error = namei(&nd)) == 0) {
1423 lvfslocked = NDHASGIANT(&nd);
1424 if (nd.ni_vp != NULL) {
1426 if (nd.ni_dvp == nd.ni_vp)
1431 } else if ((error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td))
1433 VOP_LEASE(nd.ni_dvp, td, td->td_ucred, LEASE_WRITE);
1434 VOP_LEASE(vp, td, td->td_ucred, LEASE_WRITE);
1435 error = can_hardlink(vp, td, td->td_ucred);
1438 error = mac_check_vnode_link(td->td_ucred,
1439 nd.ni_dvp, vp, &nd.ni_cnd);
1442 error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
1443 VOP_UNLOCK(vp, 0, td);
1446 NDFREE(&nd, NDF_ONLY_PNBUF);
1447 VFS_UNLOCK_GIANT(lvfslocked);
1450 vn_finished_write(mp);
1451 VFS_UNLOCK_GIANT(vfslocked);
1452 ASSERT_VOP_UNLOCKED(nd.ni_dvp, "link");
1453 ASSERT_VOP_UNLOCKED(nd.ni_vp, "link");
1458 * Make a symbolic link.
1460 #ifndef _SYS_SYSPROTO_H_
1461 struct symlink_args {
1469 register struct symlink_args /* {
1475 return (kern_symlink(td, uap->path, uap->link, UIO_USERSPACE));
1479 kern_symlink(struct thread *td, char *path, char *link, enum uio_seg segflg)
1485 struct nameidata nd;
1488 if (segflg == UIO_SYSSPACE) {
1491 syspath = uma_zalloc(namei_zone, M_WAITOK);
1492 if ((error = copyinstr(path, syspath, MAXPATHLEN, NULL)) != 0)
1497 NDINIT(&nd, CREATE, LOCKPARENT | NOOBJ | SAVENAME | MPSAFE,
1499 if ((error = namei(&nd)) != 0)
1501 vfslocked = NDHASGIANT(&nd);
1503 NDFREE(&nd, NDF_ONLY_PNBUF);
1505 if (nd.ni_vp == nd.ni_dvp)
1509 VFS_UNLOCK_GIANT(vfslocked);
1513 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1514 NDFREE(&nd, NDF_ONLY_PNBUF);
1516 VFS_UNLOCK_GIANT(vfslocked);
1517 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1522 FILEDESC_LOCK_FAST(td->td_proc->p_fd);
1523 vattr.va_mode = ACCESSPERMS &~ td->td_proc->p_fd->fd_cmask;
1524 FILEDESC_UNLOCK_FAST(td->td_proc->p_fd);
1526 vattr.va_type = VLNK;
1527 error = mac_check_vnode_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1532 VOP_LEASE(nd.ni_dvp, td, td->td_ucred, LEASE_WRITE);
1533 error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr, syspath);
1539 NDFREE(&nd, NDF_ONLY_PNBUF);
1541 vn_finished_write(mp);
1542 VFS_UNLOCK_GIANT(vfslocked);
1543 ASSERT_VOP_UNLOCKED(nd.ni_dvp, "symlink");
1544 ASSERT_VOP_UNLOCKED(nd.ni_vp, "symlink");
1546 if (segflg != UIO_SYSSPACE)
1547 uma_zfree(namei_zone, syspath);
1552 * Delete a whiteout from the filesystem.
1557 register struct undelete_args /* {
1563 struct nameidata nd;
1568 NDINIT(&nd, DELETE, LOCKPARENT | DOWHITEOUT | MPSAFE, UIO_USERSPACE,
1573 vfslocked = NDHASGIANT(&nd);
1575 if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & ISWHITEOUT)) {
1576 NDFREE(&nd, NDF_ONLY_PNBUF);
1579 if (nd.ni_vp == nd.ni_dvp)
1583 VFS_UNLOCK_GIANT(vfslocked);
1586 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1587 NDFREE(&nd, NDF_ONLY_PNBUF);
1589 VFS_UNLOCK_GIANT(vfslocked);
1590 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1594 VOP_LEASE(nd.ni_dvp, td, td->td_ucred, LEASE_WRITE);
1595 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, DELETE);
1596 NDFREE(&nd, NDF_ONLY_PNBUF);
1598 vn_finished_write(mp);
1599 VFS_UNLOCK_GIANT(vfslocked);
1600 ASSERT_VOP_UNLOCKED(nd.ni_dvp, "undelete");
1601 ASSERT_VOP_UNLOCKED(nd.ni_vp, "undelete");
1606 * Delete a name from the filesystem.
1608 #ifndef _SYS_SYSPROTO_H_
1609 struct unlink_args {
1616 struct unlink_args /* {
1622 error = kern_unlink(td, uap->path, UIO_USERSPACE);
1627 kern_unlink(struct thread *td, char *path, enum uio_seg pathseg)
1632 struct nameidata nd;
1637 NDINIT(&nd, DELETE, LOCKPARENT | LOCKLEAF | MPSAFE, pathseg, path, td);
1638 if ((error = namei(&nd)) != 0)
1640 vfslocked = NDHASGIANT(&nd);
1642 if (vp->v_type == VDIR)
1643 error = EPERM; /* POSIX */
1646 * The root of a mounted filesystem cannot be deleted.
1648 * XXX: can this only be a VDIR case?
1650 if (vp->v_vflag & VV_ROOT)
1654 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1655 NDFREE(&nd, NDF_ONLY_PNBUF);
1656 if (vp == nd.ni_dvp)
1661 VFS_UNLOCK_GIANT(vfslocked);
1662 if ((error = vn_start_write(NULL, &mp,
1663 V_XSLEEP | PCATCH)) != 0)
1668 error = mac_check_vnode_delete(td->td_ucred, nd.ni_dvp, vp,
1673 VOP_LEASE(nd.ni_dvp, td, td->td_ucred, LEASE_WRITE);
1674 error = VOP_REMOVE(nd.ni_dvp, vp, &nd.ni_cnd);
1678 vn_finished_write(mp);
1680 NDFREE(&nd, NDF_ONLY_PNBUF);
1681 if (vp == nd.ni_dvp)
1686 VFS_UNLOCK_GIANT(vfslocked);
1687 ASSERT_VOP_UNLOCKED(nd.ni_dvp, "unlink");
1688 ASSERT_VOP_UNLOCKED(nd.ni_vp, "unlink");
1693 * Reposition read/write file offset.
1695 #ifndef _SYS_SYSPROTO_H_
1706 register struct lseek_args /* {
1713 struct ucred *cred = td->td_ucred;
1721 if ((error = fget(td, uap->fd, &fp)) != 0)
1723 if (!(fp->f_ops->fo_flags & DFLAG_SEEKABLE)) {
1728 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
1729 noneg = (vp->v_type != VCHR);
1730 offset = uap->offset;
1731 switch (uap->whence) {
1734 (fp->f_offset < 0 ||
1735 (offset > 0 && fp->f_offset > OFF_MAX - offset))) {
1739 offset += fp->f_offset;
1742 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
1743 error = VOP_GETATTR(vp, &vattr, cred, td);
1744 VOP_UNLOCK(vp, 0, td);
1748 (vattr.va_size > OFF_MAX ||
1749 (offset > 0 && vattr.va_size > OFF_MAX - offset))) {
1753 offset += vattr.va_size;
1760 if (error == 0 && noneg && offset < 0)
1764 fp->f_offset = offset;
1765 *(off_t *)(td->td_retval) = fp->f_offset;
1768 VFS_UNLOCK_GIANT(vfslocked);
1772 #if defined(COMPAT_43)
1774 * Reposition read/write file offset.
1776 #ifndef _SYS_SYSPROTO_H_
1777 struct olseek_args {
1786 register struct olseek_args /* {
1792 struct lseek_args /* {
1801 nuap.offset = uap->offset;
1802 nuap.whence = uap->whence;
1803 error = lseek(td, &nuap);
1806 #endif /* COMPAT_43 */
1809 * Check access permissions using passed credentials.
1812 vn_access(vp, user_flags, cred, td)
1820 /* Flags == 0 means only check for existence. */
1824 if (user_flags & R_OK)
1826 if (user_flags & W_OK)
1828 if (user_flags & X_OK)
1831 error = mac_check_vnode_access(cred, vp, flags);
1835 if ((flags & VWRITE) == 0 || (error = vn_writechk(vp)) == 0)
1836 error = VOP_ACCESS(vp, flags, cred, td);
1842 * Check access permissions using "real" credentials.
1844 #ifndef _SYS_SYSPROTO_H_
1845 struct access_args {
1853 register struct access_args /* {
1859 return (kern_access(td, uap->path, UIO_USERSPACE, uap->flags));
1863 kern_access(struct thread *td, char *path, enum uio_seg pathseg, int flags)
1865 struct ucred *cred, *tmpcred;
1866 register struct vnode *vp;
1867 struct nameidata nd;
1872 * Create and modify a temporary credential instead of one that
1873 * is potentially shared. This could also mess up socket
1874 * buffer accounting which can run in an interrupt context.
1876 cred = td->td_ucred;
1877 tmpcred = crdup(cred);
1878 tmpcred->cr_uid = cred->cr_ruid;
1879 tmpcred->cr_groups[0] = cred->cr_rgid;
1880 td->td_ucred = tmpcred;
1881 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | NOOBJ | MPSAFE, pathseg,
1883 if ((error = namei(&nd)) != 0)
1885 vfslocked = NDHASGIANT(&nd);
1888 error = vn_access(vp, flags, tmpcred, td);
1889 NDFREE(&nd, NDF_ONLY_PNBUF);
1891 VFS_UNLOCK_GIANT(vfslocked);
1893 td->td_ucred = cred;
1899 * Check access permissions using "effective" credentials.
1901 #ifndef _SYS_SYSPROTO_H_
1902 struct eaccess_args {
1910 register struct eaccess_args /* {
1915 struct nameidata nd;
1920 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | NOOBJ | MPSAFE, UIO_USERSPACE,
1922 if ((error = namei(&nd)) != 0)
1925 vfslocked = NDHASGIANT(&nd);
1926 error = vn_access(vp, uap->flags, td->td_ucred, td);
1927 NDFREE(&nd, NDF_ONLY_PNBUF);
1929 VFS_UNLOCK_GIANT(vfslocked);
1933 #if defined(COMPAT_43)
1935 * Get file status; this version follows links.
1937 #ifndef _SYS_SYSPROTO_H_
1946 register struct ostat_args /* {
1955 error = kern_stat(td, uap->path, UIO_USERSPACE, &sb);
1959 error = copyout(&osb, uap->ub, sizeof (osb));
1964 * Get file status; this version does not follow links.
1966 #ifndef _SYS_SYSPROTO_H_
1967 struct olstat_args {
1975 register struct olstat_args /* {
1984 error = kern_lstat(td, uap->path, UIO_USERSPACE, &sb);
1988 error = copyout(&osb, uap->ub, sizeof (osb));
1993 * Convert from an old to a new stat structure.
2001 ost->st_dev = st->st_dev;
2002 ost->st_ino = st->st_ino;
2003 ost->st_mode = st->st_mode;
2004 ost->st_nlink = st->st_nlink;
2005 ost->st_uid = st->st_uid;
2006 ost->st_gid = st->st_gid;
2007 ost->st_rdev = st->st_rdev;
2008 if (st->st_size < (quad_t)1 << 32)
2009 ost->st_size = st->st_size;
2012 ost->st_atime = st->st_atime;
2013 ost->st_mtime = st->st_mtime;
2014 ost->st_ctime = st->st_ctime;
2015 ost->st_blksize = st->st_blksize;
2016 ost->st_blocks = st->st_blocks;
2017 ost->st_flags = st->st_flags;
2018 ost->st_gen = st->st_gen;
2020 #endif /* COMPAT_43 */
2023 * Get file status; this version follows links.
2025 #ifndef _SYS_SYSPROTO_H_
2034 register struct stat_args /* {
2042 error = kern_stat(td, uap->path, UIO_USERSPACE, &sb);
2044 error = copyout(&sb, uap->ub, sizeof (sb));
2049 kern_stat(struct thread *td, char *path, enum uio_seg pathseg, struct stat *sbp)
2051 struct nameidata nd;
2053 int error, vfslocked;
2055 #ifdef LOOKUP_SHARED
2056 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | NOOBJ | MPSAFE,
2059 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | NOOBJ | MPSAFE, pathseg, path,
2062 if ((error = namei(&nd)) != 0)
2064 vfslocked = NDHASGIANT(&nd);
2065 error = vn_stat(nd.ni_vp, &sb, td->td_ucred, NOCRED, td);
2066 NDFREE(&nd, NDF_ONLY_PNBUF);
2068 VFS_UNLOCK_GIANT(vfslocked);
2076 * Get file status; this version does not follow links.
2078 #ifndef _SYS_SYSPROTO_H_
2087 register struct lstat_args /* {
2095 error = kern_lstat(td, uap->path, UIO_USERSPACE, &sb);
2097 error = copyout(&sb, uap->ub, sizeof (sb));
2102 kern_lstat(struct thread *td, char *path, enum uio_seg pathseg, struct stat *sbp)
2106 struct nameidata nd;
2107 int error, vfslocked;
2109 /* XXX LOOKUP_SHARED? */
2110 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | NOOBJ | MPSAFE,
2112 if ((error = namei(&nd)) != 0)
2114 vfslocked = NDHASGIANT(&nd);
2116 error = vn_stat(vp, &sb, td->td_ucred, NOCRED, td);
2117 NDFREE(&nd, NDF_ONLY_PNBUF);
2119 VFS_UNLOCK_GIANT(vfslocked);
2127 * Implementation of the NetBSD [l]stat() functions.
2134 bzero(nsb, sizeof *nsb);
2135 nsb->st_dev = sb->st_dev;
2136 nsb->st_ino = sb->st_ino;
2137 nsb->st_mode = sb->st_mode;
2138 nsb->st_nlink = sb->st_nlink;
2139 nsb->st_uid = sb->st_uid;
2140 nsb->st_gid = sb->st_gid;
2141 nsb->st_rdev = sb->st_rdev;
2142 nsb->st_atimespec = sb->st_atimespec;
2143 nsb->st_mtimespec = sb->st_mtimespec;
2144 nsb->st_ctimespec = sb->st_ctimespec;
2145 nsb->st_size = sb->st_size;
2146 nsb->st_blocks = sb->st_blocks;
2147 nsb->st_blksize = sb->st_blksize;
2148 nsb->st_flags = sb->st_flags;
2149 nsb->st_gen = sb->st_gen;
2150 nsb->st_birthtimespec = sb->st_birthtimespec;
2153 #ifndef _SYS_SYSPROTO_H_
2162 register struct nstat_args /* {
2171 error = kern_stat(td, uap->path, UIO_USERSPACE, &sb);
2174 cvtnstat(&sb, &nsb);
2175 error = copyout(&nsb, uap->ub, sizeof (nsb));
2180 * NetBSD lstat. Get file status; this version does not follow links.
2182 #ifndef _SYS_SYSPROTO_H_
2191 register struct nlstat_args /* {
2200 error = kern_lstat(td, uap->path, UIO_USERSPACE, &sb);
2203 cvtnstat(&sb, &nsb);
2204 error = copyout(&nsb, uap->ub, sizeof (nsb));
2209 * Get configurable pathname variables.
2211 #ifndef _SYS_SYSPROTO_H_
2212 struct pathconf_args {
2220 register struct pathconf_args /* {
2226 return (kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name));
2230 kern_pathconf(struct thread *td, char *path, enum uio_seg pathseg, int name)
2232 struct nameidata nd;
2233 int error, vfslocked;
2235 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | NOOBJ | MPSAFE, pathseg, path,
2237 if ((error = namei(&nd)) != 0)
2239 vfslocked = NDHASGIANT(&nd);
2240 NDFREE(&nd, NDF_ONLY_PNBUF);
2242 /* If asynchronous I/O is available, it works for all files. */
2243 if (name == _PC_ASYNC_IO)
2244 td->td_retval[0] = async_io_version;
2246 error = VOP_PATHCONF(nd.ni_vp, name, td->td_retval);
2248 VFS_UNLOCK_GIANT(vfslocked);
2253 * Return target name of a symbolic link.
2255 #ifndef _SYS_SYSPROTO_H_
2256 struct readlink_args {
2265 register struct readlink_args /* {
2272 return (kern_readlink(td, uap->path, UIO_USERSPACE, uap->buf,
2273 UIO_USERSPACE, uap->count));
2277 kern_readlink(struct thread *td, char *path, enum uio_seg pathseg, char *buf,
2278 enum uio_seg bufseg, int count)
2280 register struct vnode *vp;
2284 struct nameidata nd;
2287 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | NOOBJ | MPSAFE,
2289 if ((error = namei(&nd)) != 0)
2291 NDFREE(&nd, NDF_ONLY_PNBUF);
2292 vfslocked = NDHASGIANT(&nd);
2295 error = mac_check_vnode_readlink(td->td_ucred, vp);
2298 VFS_UNLOCK_GIANT(vfslocked);
2302 if (vp->v_type != VLNK)
2305 aiov.iov_base = buf;
2306 aiov.iov_len = count;
2307 auio.uio_iov = &aiov;
2308 auio.uio_iovcnt = 1;
2309 auio.uio_offset = 0;
2310 auio.uio_rw = UIO_READ;
2311 auio.uio_segflg = bufseg;
2313 auio.uio_resid = count;
2314 error = VOP_READLINK(vp, &auio, td->td_ucred);
2317 VFS_UNLOCK_GIANT(vfslocked);
2318 td->td_retval[0] = count - auio.uio_resid;
2323 * Common implementation code for chflags() and fchflags().
2326 setfflags(td, vp, flags)
2336 * Prevent non-root users from setting flags on devices. When
2337 * a device is reused, users can retain ownership of the device
2338 * if they are allowed to set flags and programs assume that
2339 * chown can't fail when done as root.
2341 if (vp->v_type == VCHR || vp->v_type == VBLK) {
2342 error = suser_cred(td->td_ucred, SUSER_ALLOWJAIL);
2347 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2349 VOP_LEASE(vp, td, td->td_ucred, LEASE_WRITE);
2350 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2352 vattr.va_flags = flags;
2354 error = mac_check_vnode_setflags(td->td_ucred, vp, vattr.va_flags);
2357 error = VOP_SETATTR(vp, &vattr, td->td_ucred, td);
2358 VOP_UNLOCK(vp, 0, td);
2359 vn_finished_write(mp);
2364 * Change flags of a file given a path name.
2366 #ifndef _SYS_SYSPROTO_H_
2367 struct chflags_args {
2375 register struct chflags_args /* {
2381 struct nameidata nd;
2384 NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE, UIO_USERSPACE, uap->path, td);
2385 if ((error = namei(&nd)) != 0)
2387 NDFREE(&nd, NDF_ONLY_PNBUF);
2388 vfslocked = NDHASGIANT(&nd);
2389 error = setfflags(td, nd.ni_vp, uap->flags);
2391 VFS_UNLOCK_GIANT(vfslocked);
2396 * Same as chflags() but doesn't follow symlinks.
2401 register struct lchflags_args /* {
2407 struct nameidata nd;
2410 NDINIT(&nd, LOOKUP, NOFOLLOW | MPSAFE, UIO_USERSPACE, uap->path, td);
2411 if ((error = namei(&nd)) != 0)
2413 vfslocked = NDHASGIANT(&nd);
2414 NDFREE(&nd, NDF_ONLY_PNBUF);
2415 error = setfflags(td, nd.ni_vp, uap->flags);
2417 VFS_UNLOCK_GIANT(vfslocked);
2422 * Change flags of a file given a file descriptor.
2424 #ifndef _SYS_SYSPROTO_H_
2425 struct fchflags_args {
2433 register struct fchflags_args /* {
2442 if ((error = getvnode(td->td_proc->p_fd, uap->fd, &fp)) != 0)
2444 vfslocked = VFS_LOCK_GIANT(fp->f_vnode->v_mount);
2445 error = setfflags(td, fp->f_vnode, uap->flags);
2447 VFS_UNLOCK_GIANT(vfslocked);
2452 * Common implementation code for chmod(), lchmod() and fchmod().
2455 setfmode(td, vp, mode)
2464 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2466 VOP_LEASE(vp, td, td->td_ucred, LEASE_WRITE);
2467 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2469 vattr.va_mode = mode & ALLPERMS;
2471 error = mac_check_vnode_setmode(td->td_ucred, vp, vattr.va_mode);
2474 error = VOP_SETATTR(vp, &vattr, td->td_ucred, td);
2475 VOP_UNLOCK(vp, 0, td);
2476 vn_finished_write(mp);
2481 * Change mode of a file given path name.
2483 #ifndef _SYS_SYSPROTO_H_
2492 register struct chmod_args /* {
2498 return (kern_chmod(td, uap->path, UIO_USERSPACE, uap->mode));
2502 kern_chmod(struct thread *td, char *path, enum uio_seg pathseg, int mode)
2505 struct nameidata nd;
2508 NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE, pathseg, path, td);
2509 if ((error = namei(&nd)) != 0)
2511 vfslocked = NDHASGIANT(&nd);
2512 NDFREE(&nd, NDF_ONLY_PNBUF);
2513 error = setfmode(td, nd.ni_vp, mode);
2515 VFS_UNLOCK_GIANT(vfslocked);
2520 * Change mode of a file given path name (don't follow links.)
2522 #ifndef _SYS_SYSPROTO_H_
2523 struct lchmod_args {
2531 register struct lchmod_args /* {
2537 struct nameidata nd;
2540 NDINIT(&nd, LOOKUP, NOFOLLOW | MPSAFE, UIO_USERSPACE, uap->path, td);
2541 if ((error = namei(&nd)) != 0)
2543 vfslocked = NDHASGIANT(&nd);
2544 NDFREE(&nd, NDF_ONLY_PNBUF);
2545 error = setfmode(td, nd.ni_vp, uap->mode);
2547 VFS_UNLOCK_GIANT(vfslocked);
2552 * Change mode of a file given a file descriptor.
2554 #ifndef _SYS_SYSPROTO_H_
2555 struct fchmod_args {
2563 register struct fchmod_args /* {
2572 if ((error = getvnode(td->td_proc->p_fd, uap->fd, &fp)) != 0)
2574 vfslocked = VFS_LOCK_GIANT(fp->f_vnode->v_mount);
2575 error = setfmode(td, fp->f_vnode, uap->mode);
2577 VFS_UNLOCK_GIANT(vfslocked);
2582 * Common implementation for chown(), lchown(), and fchown()
2585 setfown(td, vp, uid, gid)
2595 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2597 VOP_LEASE(vp, td, td->td_ucred, LEASE_WRITE);
2598 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2603 error = mac_check_vnode_setowner(td->td_ucred, vp, vattr.va_uid,
2607 error = VOP_SETATTR(vp, &vattr, td->td_ucred, td);
2608 VOP_UNLOCK(vp, 0, td);
2609 vn_finished_write(mp);
2614 * Set ownership given a path name.
2616 #ifndef _SYS_SYSPROTO_H_
2626 register struct chown_args /* {
2633 return (kern_chown(td, uap->path, UIO_USERSPACE, uap->uid, uap->gid));
2637 kern_chown(struct thread *td, char *path, enum uio_seg pathseg, int uid,
2641 struct nameidata nd;
2644 NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE, pathseg, path, td);
2645 if ((error = namei(&nd)) != 0)
2647 vfslocked = NDHASGIANT(&nd);
2648 NDFREE(&nd, NDF_ONLY_PNBUF);
2649 error = setfown(td, nd.ni_vp, uid, gid);
2651 VFS_UNLOCK_GIANT(vfslocked);
2656 * Set ownership given a path name, do not cross symlinks.
2658 #ifndef _SYS_SYSPROTO_H_
2659 struct lchown_args {
2668 register struct lchown_args /* {
2675 return (kern_lchown(td, uap->path, UIO_USERSPACE, uap->uid, uap->gid));
2679 kern_lchown(struct thread *td, char *path, enum uio_seg pathseg, int uid,
2683 struct nameidata nd;
2686 NDINIT(&nd, LOOKUP, NOFOLLOW | MPSAFE, pathseg, path, td);
2687 if ((error = namei(&nd)) != 0)
2689 vfslocked = NDHASGIANT(&nd);
2690 NDFREE(&nd, NDF_ONLY_PNBUF);
2691 error = setfown(td, nd.ni_vp, uid, gid);
2693 VFS_UNLOCK_GIANT(vfslocked);
2698 * Set ownership given a file descriptor.
2700 #ifndef _SYS_SYSPROTO_H_
2701 struct fchown_args {
2710 register struct fchown_args /* {
2720 if ((error = getvnode(td->td_proc->p_fd, uap->fd, &fp)) != 0)
2722 vfslocked = VFS_LOCK_GIANT(fp->f_vnode->v_mount);
2723 error = setfown(td, fp->f_vnode, uap->uid, uap->gid);
2725 VFS_UNLOCK_GIANT(vfslocked);
2730 * Common implementation code for utimes(), lutimes(), and futimes().
2733 getutimes(usrtvp, tvpseg, tsp)
2734 const struct timeval *usrtvp;
2735 enum uio_seg tvpseg;
2736 struct timespec *tsp;
2738 struct timeval tv[2];
2739 const struct timeval *tvp;
2742 if (usrtvp == NULL) {
2744 TIMEVAL_TO_TIMESPEC(&tv[0], &tsp[0]);
2747 if (tvpseg == UIO_SYSSPACE) {
2750 if ((error = copyin(usrtvp, tv, sizeof(tv))) != 0)
2755 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
2756 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
2762 * Common implementation code for utimes(), lutimes(), and futimes().
2765 setutimes(td, vp, ts, numtimes, nullflag)
2768 const struct timespec *ts;
2772 int error, setbirthtime;
2776 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2778 VOP_LEASE(vp, td, td->td_ucred, LEASE_WRITE);
2779 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2781 if (numtimes < 3 && VOP_GETATTR(vp, &vattr, td->td_ucred, td) == 0 &&
2782 timespeccmp(&ts[1], &vattr.va_birthtime, < ))
2785 vattr.va_atime = ts[0];
2786 vattr.va_mtime = ts[1];
2788 vattr.va_birthtime = ts[1];
2790 vattr.va_birthtime = ts[2];
2792 vattr.va_vaflags |= VA_UTIMES_NULL;
2794 error = mac_check_vnode_setutimes(td->td_ucred, vp, vattr.va_atime,
2798 error = VOP_SETATTR(vp, &vattr, td->td_ucred, td);
2799 VOP_UNLOCK(vp, 0, td);
2800 vn_finished_write(mp);
2805 * Set the access and modification times of a file.
2807 #ifndef _SYS_SYSPROTO_H_
2808 struct utimes_args {
2810 struct timeval *tptr;
2816 register struct utimes_args /* {
2818 struct timeval *tptr;
2822 return (kern_utimes(td, uap->path, UIO_USERSPACE, uap->tptr,
2827 kern_utimes(struct thread *td, char *path, enum uio_seg pathseg,
2828 struct timeval *tptr, enum uio_seg tptrseg)
2830 struct timespec ts[2];
2832 struct nameidata nd;
2835 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
2837 NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE, pathseg, path, td);
2838 if ((error = namei(&nd)) != 0)
2840 vfslocked = NDHASGIANT(&nd);
2841 NDFREE(&nd, NDF_ONLY_PNBUF);
2842 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
2844 VFS_UNLOCK_GIANT(vfslocked);
2849 * Set the access and modification times of a file.
2851 #ifndef _SYS_SYSPROTO_H_
2852 struct lutimes_args {
2854 struct timeval *tptr;
2860 register struct lutimes_args /* {
2862 struct timeval *tptr;
2866 return (kern_lutimes(td, uap->path, UIO_USERSPACE, uap->tptr,
2871 kern_lutimes(struct thread *td, char *path, enum uio_seg pathseg,
2872 struct timeval *tptr, enum uio_seg tptrseg)
2874 struct timespec ts[2];
2876 struct nameidata nd;
2879 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
2881 NDINIT(&nd, LOOKUP, NOFOLLOW | MPSAFE, pathseg, path, td);
2882 if ((error = namei(&nd)) != 0)
2884 vfslocked = NDHASGIANT(&nd);
2885 NDFREE(&nd, NDF_ONLY_PNBUF);
2886 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
2888 VFS_UNLOCK_GIANT(vfslocked);
2893 * Set the access and modification times of a file.
2895 #ifndef _SYS_SYSPROTO_H_
2896 struct futimes_args {
2898 struct timeval *tptr;
2904 register struct futimes_args /* {
2906 struct timeval *tptr;
2910 return (kern_futimes(td, uap->fd, uap->tptr, UIO_USERSPACE));
2914 kern_futimes(struct thread *td, int fd, struct timeval *tptr,
2915 enum uio_seg tptrseg)
2917 struct timespec ts[2];
2922 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
2924 if ((error = getvnode(td->td_proc->p_fd, fd, &fp)) != 0)
2926 vfslocked = VFS_LOCK_GIANT(fp->f_vnode->v_mount);
2927 error = setutimes(td, fp->f_vnode, ts, 2, tptr == NULL);
2929 VFS_UNLOCK_GIANT(vfslocked);
2934 * Truncate a file given its path name.
2936 #ifndef _SYS_SYSPROTO_H_
2937 struct truncate_args {
2946 register struct truncate_args /* {
2953 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
2957 kern_truncate(struct thread *td, char *path, enum uio_seg pathseg, off_t length)
2963 struct nameidata nd;
2968 NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE, pathseg, path, td);
2969 if ((error = namei(&nd)) != 0)
2971 vfslocked = NDHASGIANT(&nd);
2973 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0) {
2975 VFS_UNLOCK_GIANT(vfslocked);
2978 NDFREE(&nd, NDF_ONLY_PNBUF);
2979 VOP_LEASE(vp, td, td->td_ucred, LEASE_WRITE);
2980 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2981 if (vp->v_type == VDIR)
2984 else if ((error = mac_check_vnode_write(td->td_ucred, NOCRED, vp))) {
2987 else if ((error = vn_writechk(vp)) == 0 &&
2988 (error = VOP_ACCESS(vp, VWRITE, td->td_ucred, td)) == 0) {
2990 vattr.va_size = length;
2991 error = VOP_SETATTR(vp, &vattr, td->td_ucred, td);
2994 vn_finished_write(mp);
2995 VFS_UNLOCK_GIANT(vfslocked);
3000 * Truncate a file given a file descriptor.
3002 #ifndef _SYS_SYSPROTO_H_
3003 struct ftruncate_args {
3012 register struct ftruncate_args /* {
3025 if (uap->length < 0)
3027 if ((error = getvnode(td->td_proc->p_fd, uap->fd, &fp)) != 0)
3029 if ((fp->f_flag & FWRITE) == 0) {
3034 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
3035 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
3037 VOP_LEASE(vp, td, td->td_ucred, LEASE_WRITE);
3038 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
3039 if (vp->v_type == VDIR)
3042 else if ((error = mac_check_vnode_write(td->td_ucred, fp->f_cred,
3046 else if ((error = vn_writechk(vp)) == 0) {
3048 vattr.va_size = uap->length;
3049 error = VOP_SETATTR(vp, &vattr, fp->f_cred, td);
3051 VOP_UNLOCK(vp, 0, td);
3052 vn_finished_write(mp);
3054 VFS_UNLOCK_GIANT(vfslocked);
3059 #if defined(COMPAT_43)
3061 * Truncate a file given its path name.
3063 #ifndef _SYS_SYSPROTO_H_
3064 struct otruncate_args {
3072 register struct otruncate_args /* {
3077 struct truncate_args /* {
3083 nuap.path = uap->path;
3084 nuap.length = uap->length;
3085 return (truncate(td, &nuap));
3089 * Truncate a file given a file descriptor.
3091 #ifndef _SYS_SYSPROTO_H_
3092 struct oftruncate_args {
3100 register struct oftruncate_args /* {
3105 struct ftruncate_args /* {
3112 nuap.length = uap->length;
3113 return (ftruncate(td, &nuap));
3115 #endif /* COMPAT_43 */
3118 * Sync an open file.
3120 #ifndef _SYS_SYSPROTO_H_
3128 struct fsync_args /* {
3138 if ((error = getvnode(td->td_proc->p_fd, uap->fd, &fp)) != 0)
3141 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
3142 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
3144 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
3145 if (vp->v_object != NULL) {
3146 VM_OBJECT_LOCK(vp->v_object);
3147 vm_object_page_clean(vp->v_object, 0, 0, 0);
3148 VM_OBJECT_UNLOCK(vp->v_object);
3150 error = VOP_FSYNC(vp, MNT_WAIT, td);
3152 VOP_UNLOCK(vp, 0, td);
3153 vn_finished_write(mp);
3155 VFS_UNLOCK_GIANT(vfslocked);
3161 * Rename files. Source and destination must either both be directories,
3162 * or both not be directories. If target is a directory, it must be empty.
3164 #ifndef _SYS_SYSPROTO_H_
3165 struct rename_args {
3173 register struct rename_args /* {
3179 return (kern_rename(td, uap->from, uap->to, UIO_USERSPACE));
3183 kern_rename(struct thread *td, char *from, char *to, enum uio_seg pathseg)
3185 struct mount *mp = NULL;
3186 struct vnode *tvp, *fvp, *tdvp;
3187 struct nameidata fromnd, tond;
3194 NDINIT(&fromnd, DELETE, LOCKPARENT | LOCKLEAF | SAVESTART | MPSAFE,
3197 NDINIT(&fromnd, DELETE, WANTPARENT | SAVESTART | MPSAFE,
3200 if ((error = namei(&fromnd)) != 0)
3202 fvfslocked = NDHASGIANT(&fromnd);
3205 error = mac_check_vnode_rename_from(td->td_ucred, fromnd.ni_dvp,
3206 fromnd.ni_vp, &fromnd.ni_cnd);
3207 VOP_UNLOCK(fromnd.ni_dvp, 0, td);
3208 VOP_UNLOCK(fromnd.ni_vp, 0, td);
3212 error = vn_start_write(fvp, &mp, V_WAIT | PCATCH);
3214 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3215 vrele(fromnd.ni_dvp);
3219 NDINIT(&tond, RENAME, LOCKPARENT | LOCKLEAF | NOCACHE | SAVESTART |
3220 NOOBJ | MPSAFE, pathseg, to, td);
3221 if (fromnd.ni_vp->v_type == VDIR)
3222 tond.ni_cnd.cn_flags |= WILLBEDIR;
3223 if ((error = namei(&tond)) != 0) {
3224 /* Translate error code for rename("dir1", "dir2/."). */
3225 if (error == EISDIR && fvp->v_type == VDIR)
3227 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3228 vrele(fromnd.ni_dvp);
3232 tvfslocked = NDHASGIANT(&tond);
3236 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
3239 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
3247 * If the source is the same as the destination (that is, if they
3248 * are links to the same vnode), then there is nothing to do.
3254 error = mac_check_vnode_rename_to(td->td_ucred, tdvp,
3255 tond.ni_vp, fromnd.ni_dvp == tdvp, &tond.ni_cnd);
3259 VOP_LEASE(tdvp, td, td->td_ucred, LEASE_WRITE);
3260 if (fromnd.ni_dvp != tdvp) {
3261 VOP_LEASE(fromnd.ni_dvp, td, td->td_ucred, LEASE_WRITE);
3264 VOP_LEASE(tvp, td, td->td_ucred, LEASE_WRITE);
3266 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
3267 tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
3268 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3269 NDFREE(&tond, NDF_ONLY_PNBUF);
3271 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3272 NDFREE(&tond, NDF_ONLY_PNBUF);
3279 vrele(fromnd.ni_dvp);
3282 vrele(tond.ni_startdir);
3283 ASSERT_VOP_UNLOCKED(fromnd.ni_dvp, "rename");
3284 ASSERT_VOP_UNLOCKED(fromnd.ni_vp, "rename");
3285 ASSERT_VOP_UNLOCKED(tond.ni_dvp, "rename");
3286 ASSERT_VOP_UNLOCKED(tond.ni_vp, "rename");
3288 vn_finished_write(mp);
3289 if (fromnd.ni_startdir)
3290 vrele(fromnd.ni_startdir);
3291 VFS_UNLOCK_GIANT(fvfslocked);
3292 VFS_UNLOCK_GIANT(tvfslocked);
3299 * Make a directory file.
3301 #ifndef _SYS_SYSPROTO_H_
3310 register struct mkdir_args /* {
3316 return (kern_mkdir(td, uap->path, UIO_USERSPACE, uap->mode));
3320 kern_mkdir(struct thread *td, char *path, enum uio_seg segflg, int mode)
3326 struct nameidata nd;
3331 NDINIT(&nd, CREATE, LOCKPARENT | SAVENAME | MPSAFE, segflg, path, td);
3332 nd.ni_cnd.cn_flags |= WILLBEDIR;
3333 if ((error = namei(&nd)) != 0)
3335 vfslocked = NDHASGIANT(&nd);
3338 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)
3349 VFS_UNLOCK_GIANT(vfslocked);
3352 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3353 NDFREE(&nd, NDF_ONLY_PNBUF);
3355 VFS_UNLOCK_GIANT(vfslocked);
3356 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3361 vattr.va_type = VDIR;
3362 FILEDESC_LOCK_FAST(td->td_proc->p_fd);
3363 vattr.va_mode = (mode & ACCESSPERMS) &~ td->td_proc->p_fd->fd_cmask;
3364 FILEDESC_UNLOCK_FAST(td->td_proc->p_fd);
3366 error = mac_check_vnode_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
3371 VOP_LEASE(nd.ni_dvp, td, td->td_ucred, LEASE_WRITE);
3372 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
3376 NDFREE(&nd, NDF_ONLY_PNBUF);
3380 vn_finished_write(mp);
3381 VFS_UNLOCK_GIANT(vfslocked);
3382 ASSERT_VOP_UNLOCKED(nd.ni_dvp, "mkdir");
3383 ASSERT_VOP_UNLOCKED(nd.ni_vp, "mkdir");
3388 * Remove a directory file.
3390 #ifndef _SYS_SYSPROTO_H_
3398 struct rmdir_args /* {
3403 return (kern_rmdir(td, uap->path, UIO_USERSPACE));
3407 kern_rmdir(struct thread *td, char *path, enum uio_seg pathseg)
3412 struct nameidata nd;
3417 NDINIT(&nd, DELETE, LOCKPARENT | LOCKLEAF | MPSAFE, pathseg, path, td);
3418 if ((error = namei(&nd)) != 0)
3420 vfslocked = NDHASGIANT(&nd);
3422 if (vp->v_type != VDIR) {
3427 * No rmdir "." please.
3429 if (nd.ni_dvp == vp) {
3434 * The root of a mounted filesystem cannot be deleted.
3436 if (vp->v_vflag & VV_ROOT) {
3441 error = mac_check_vnode_delete(td->td_ucred, nd.ni_dvp, vp,
3446 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3447 NDFREE(&nd, NDF_ONLY_PNBUF);
3448 if (nd.ni_dvp == vp)
3453 VFS_UNLOCK_GIANT(vfslocked);
3454 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3458 VOP_LEASE(nd.ni_dvp, td, td->td_ucred, LEASE_WRITE);
3459 VOP_LEASE(vp, td, td->td_ucred, LEASE_WRITE);
3460 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
3461 vn_finished_write(mp);
3463 NDFREE(&nd, NDF_ONLY_PNBUF);
3464 if (nd.ni_dvp == vp)
3469 VFS_UNLOCK_GIANT(vfslocked);
3470 ASSERT_VOP_UNLOCKED(nd.ni_dvp, "rmdir");
3471 ASSERT_VOP_UNLOCKED(nd.ni_vp, "rmdir");
3477 * Read a block of directory entries in a filesystem independent format.
3479 #ifndef _SYS_SYSPROTO_H_
3480 struct ogetdirentries_args {
3488 ogetdirentries(td, uap)
3490 register struct ogetdirentries_args /* {
3499 struct uio auio, kuio;
3500 struct iovec aiov, kiov;
3501 struct dirent *dp, *edp;
3503 int error, eofflag, readcnt;
3506 /* XXX arbitrary sanity limit on `count'. */
3507 if (uap->count > 64 * 1024)
3509 if ((error = getvnode(td->td_proc->p_fd, uap->fd, &fp)) != 0)
3511 if ((fp->f_flag & FREAD) == 0) {
3517 if (vp->v_type != VDIR) {
3521 aiov.iov_base = uap->buf;
3522 aiov.iov_len = uap->count;
3523 auio.uio_iov = &aiov;
3524 auio.uio_iovcnt = 1;
3525 auio.uio_rw = UIO_READ;
3526 auio.uio_segflg = UIO_USERSPACE;
3528 auio.uio_resid = uap->count;
3529 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
3530 loff = auio.uio_offset = fp->f_offset;
3532 error = mac_check_vnode_readdir(td->td_ucred, vp);
3534 VOP_UNLOCK(vp, 0, td);
3539 # if (BYTE_ORDER != LITTLE_ENDIAN)
3540 if (vp->v_mount->mnt_maxsymlinklen <= 0) {
3541 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag,
3543 fp->f_offset = auio.uio_offset;
3548 kuio.uio_iov = &kiov;
3549 kuio.uio_segflg = UIO_SYSSPACE;
3550 kiov.iov_len = uap->count;
3551 MALLOC(dirbuf, caddr_t, uap->count, M_TEMP, M_WAITOK);
3552 kiov.iov_base = dirbuf;
3553 error = VOP_READDIR(vp, &kuio, fp->f_cred, &eofflag,
3555 fp->f_offset = kuio.uio_offset;
3557 readcnt = uap->count - kuio.uio_resid;
3558 edp = (struct dirent *)&dirbuf[readcnt];
3559 for (dp = (struct dirent *)dirbuf; dp < edp; ) {
3560 # if (BYTE_ORDER == LITTLE_ENDIAN)
3562 * The expected low byte of
3563 * dp->d_namlen is our dp->d_type.
3564 * The high MBZ byte of dp->d_namlen
3565 * is our dp->d_namlen.
3567 dp->d_type = dp->d_namlen;
3571 * The dp->d_type is the high byte
3572 * of the expected dp->d_namlen,
3573 * so must be zero'ed.
3577 if (dp->d_reclen > 0) {
3578 dp = (struct dirent *)
3579 ((char *)dp + dp->d_reclen);
3586 error = uiomove(dirbuf, readcnt, &auio);
3588 FREE(dirbuf, M_TEMP);
3590 VOP_UNLOCK(vp, 0, td);
3595 if (uap->count == auio.uio_resid) {
3596 if (union_dircheckp) {
3597 error = union_dircheckp(td, &vp, fp);
3606 * XXX We could delay dropping the lock above but
3607 * union_dircheckp complicates things.
3609 vn_lock(vp, LK_EXCLUSIVE|LK_RETRY, td);
3610 if ((vp->v_vflag & VV_ROOT) &&
3611 (vp->v_mount->mnt_flag & MNT_UNION)) {
3612 struct vnode *tvp = vp;
3613 vp = vp->v_mount->mnt_vnodecovered;
3621 VOP_UNLOCK(vp, 0, td);
3623 error = copyout(&loff, uap->basep, sizeof(long));
3625 td->td_retval[0] = uap->count - auio.uio_resid;
3628 #endif /* COMPAT_43 */
3631 * Read a block of directory entries in a filesystem independent format.
3633 #ifndef _SYS_SYSPROTO_H_
3634 struct getdirentries_args {
3642 getdirentries(td, uap)
3644 register struct getdirentries_args /* {
3659 if ((error = getvnode(td->td_proc->p_fd, uap->fd, &fp)) != 0)
3661 if ((fp->f_flag & FREAD) == 0) {
3667 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
3668 if (vp->v_type != VDIR) {
3672 aiov.iov_base = uap->buf;
3673 aiov.iov_len = uap->count;
3674 auio.uio_iov = &aiov;
3675 auio.uio_iovcnt = 1;
3676 auio.uio_rw = UIO_READ;
3677 auio.uio_segflg = UIO_USERSPACE;
3679 auio.uio_resid = uap->count;
3680 /* vn_lock(vp, LK_SHARED | LK_RETRY, td); */
3681 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
3682 loff = auio.uio_offset = fp->f_offset;
3684 error = mac_check_vnode_readdir(td->td_ucred, vp);
3687 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL,
3689 fp->f_offset = auio.uio_offset;
3690 VOP_UNLOCK(vp, 0, td);
3693 if (uap->count == auio.uio_resid) {
3694 if (union_dircheckp) {
3695 error = union_dircheckp(td, &vp, fp);
3697 VFS_UNLOCK_GIANT(vfslocked);
3704 * XXX We could delay dropping the lock above but
3705 * union_dircheckp complicates things.
3707 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
3708 if ((vp->v_vflag & VV_ROOT) &&
3709 (vp->v_mount->mnt_flag & MNT_UNION)) {
3710 struct vnode *tvp = vp;
3711 vp = vp->v_mount->mnt_vnodecovered;
3717 VFS_UNLOCK_GIANT(vfslocked);
3720 VOP_UNLOCK(vp, 0, td);
3722 if (uap->basep != NULL) {
3723 error = copyout(&loff, uap->basep, sizeof(long));
3725 td->td_retval[0] = uap->count - auio.uio_resid;
3727 VFS_UNLOCK_GIANT(vfslocked);
3731 #ifndef _SYS_SYSPROTO_H_
3732 struct getdents_args {
3741 register struct getdents_args /* {
3747 struct getdirentries_args ap;
3750 ap.count = uap->count;
3752 return (getdirentries(td, &ap));
3756 * Set the mode mask for creation of filesystem nodes.
3760 #ifndef _SYS_SYSPROTO_H_
3768 struct umask_args /* {
3772 register struct filedesc *fdp;
3774 FILEDESC_LOCK_FAST(td->td_proc->p_fd);
3775 fdp = td->td_proc->p_fd;
3776 td->td_retval[0] = fdp->fd_cmask;
3777 fdp->fd_cmask = uap->newmask & ALLPERMS;
3778 FILEDESC_UNLOCK_FAST(td->td_proc->p_fd);
3783 * Void all references to file by ripping underlying filesystem
3786 #ifndef _SYS_SYSPROTO_H_
3787 struct revoke_args {
3794 register struct revoke_args /* {
3801 struct nameidata nd;
3804 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | MPSAFE, UIO_USERSPACE,
3806 if ((error = namei(&nd)) != 0)
3808 vfslocked = NDHASGIANT(&nd);
3810 NDFREE(&nd, NDF_ONLY_PNBUF);
3811 if (vp->v_type != VCHR) {
3816 error = mac_check_vnode_revoke(td->td_ucred, vp);
3820 error = VOP_GETATTR(vp, &vattr, td->td_ucred, td);
3823 if (td->td_ucred->cr_uid != vattr.va_uid) {
3824 error = suser_cred(td->td_ucred, SUSER_ALLOWJAIL);
3829 VOP_REVOKE(vp, REVOKEALL);
3832 VFS_UNLOCK_GIANT(vfslocked);
3837 * Convert a user file descriptor to a kernel file entry.
3838 * A reference on the file entry is held upon returning.
3841 getvnode(fdp, fd, fpp)
3842 struct filedesc *fdp;
3854 if ((u_int)fd >= fdp->fd_nfiles ||
3855 (fp = fdp->fd_ofiles[fd]) == NULL)
3857 else if (fp->f_vnode == NULL) {
3864 FILEDESC_UNLOCK(fdp);
3871 * Get (NFS) file handle
3873 #ifndef _SYS_SYSPROTO_H_
3874 struct lgetfh_args {
3882 register struct lgetfh_args *uap;
3884 struct nameidata nd;
3886 register struct vnode *vp;
3893 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | MPSAFE,
3894 UIO_USERSPACE, uap->fname, td);
3898 vfslocked = NDHASGIANT(&nd);
3899 NDFREE(&nd, NDF_ONLY_PNBUF);
3901 bzero(&fh, sizeof(fh));
3902 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
3903 error = VFS_VPTOFH(vp, &fh.fh_fid);
3905 VFS_UNLOCK_GIANT(vfslocked);
3908 error = copyout(&fh, uap->fhp, sizeof (fh));
3912 #ifndef _SYS_SYSPROTO_H_
3921 register struct getfh_args *uap;
3923 struct nameidata nd;
3925 register struct vnode *vp;
3932 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | MPSAFE,
3933 UIO_USERSPACE, uap->fname, td);
3937 vfslocked = NDHASGIANT(&nd);
3938 NDFREE(&nd, NDF_ONLY_PNBUF);
3940 bzero(&fh, sizeof(fh));
3941 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
3942 error = VFS_VPTOFH(vp, &fh.fh_fid);
3944 VFS_UNLOCK_GIANT(vfslocked);
3947 error = copyout(&fh, uap->fhp, sizeof (fh));
3952 * syscall for the rpc.lockd to use to translate a NFS file handle into
3953 * an open descriptor.
3955 * warning: do not remove the suser() call or this becomes one giant
3958 #ifndef _SYS_SYSPROTO_H_
3959 struct fhopen_args {
3960 const struct fhandle *u_fhp;
3967 struct fhopen_args /* {
3968 const struct fhandle *u_fhp;
3972 struct proc *p = td->td_proc;
3977 struct vattr *vap = &vat;
3980 register struct filedesc *fdp = p->p_fd;
3981 int fmode, mode, error, type;
3988 fmode = FFLAGS(uap->flags);
3989 /* why not allow a non-read/write open for our lockd? */
3990 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
3992 error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
3995 /* find the mount point */
3996 mp = vfs_getvfs(&fhp.fh_fsid);
3999 /* now give me my vnode, it gets returned to me locked */
4000 error = VFS_FHTOVP(mp, &fhp.fh_fid, &vp);
4004 * from now on we have to make sure not
4005 * to forget about the vnode
4006 * any error that causes an abort must vput(vp)
4007 * just set error = err and 'goto bad;'.
4013 if (vp->v_type == VLNK) {
4017 if (vp->v_type == VSOCK) {
4022 if (fmode & (FWRITE | O_TRUNC)) {
4023 if (vp->v_type == VDIR) {
4027 error = vn_writechk(vp);
4034 if (fmode & O_APPEND)
4037 error = mac_check_vnode_open(td->td_ucred, vp, mode);
4042 error = VOP_ACCESS(vp, mode, td->td_ucred, td);
4046 if (fmode & O_TRUNC) {
4047 VOP_UNLOCK(vp, 0, td); /* XXX */
4048 if ((error = vn_start_write(NULL, &mp, V_WAIT | PCATCH)) != 0) {
4052 VOP_LEASE(vp, td, td->td_ucred, LEASE_WRITE);
4053 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td); /* XXX */
4056 * We don't yet have fp->f_cred, so use td->td_ucred, which
4059 error = mac_check_vnode_write(td->td_ucred, td->td_ucred, vp);
4064 error = VOP_SETATTR(vp, vap, td->td_ucred, td);
4068 vn_finished_write(mp);
4072 error = VOP_OPEN(vp, fmode, td->td_ucred, td, -1);
4080 * end of vn_open code
4083 if ((error = falloc(td, &nfp, &indx)) != 0) {
4088 /* An extra reference on `nfp' has been held for us by falloc(). */
4093 nfp->f_flag = fmode & FMASK;
4094 nfp->f_ops = &vnops;
4095 nfp->f_type = DTYPE_VNODE;
4096 if (fmode & (O_EXLOCK | O_SHLOCK)) {
4097 lf.l_whence = SEEK_SET;
4100 if (fmode & O_EXLOCK)
4101 lf.l_type = F_WRLCK;
4103 lf.l_type = F_RDLCK;
4105 if ((fmode & FNONBLOCK) == 0)
4107 VOP_UNLOCK(vp, 0, td);
4108 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf,
4111 * The lock request failed. Normally close the
4112 * descriptor but handle the case where someone might
4113 * have dup()d or close()d it when we weren't looking.
4115 fdclose(fdp, fp, indx, td);
4118 * release our private reference
4123 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
4124 fp->f_flag |= FHASLOCK;
4127 VOP_UNLOCK(vp, 0, td);
4129 td->td_retval[0] = indx;
4138 * Stat an (NFS) file handle.
4140 #ifndef _SYS_SYSPROTO_H_
4141 struct fhstat_args {
4142 struct fhandle *u_fhp;
4149 register struct fhstat_args /* {
4150 struct fhandle *u_fhp;
4163 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4166 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
4168 if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)))
4170 error = vn_stat(vp, &sb, td->td_ucred, NOCRED, td);
4174 error = copyout(&sb, uap->sb, sizeof(sb));
4179 * Implement fstatfs() for (NFS) file handles.
4181 #ifndef _SYS_SYSPROTO_H_
4182 struct fhstatfs_args {
4183 struct fhandle *u_fhp;
4190 struct fhstatfs_args /* {
4191 struct fhandle *u_fhp;
4199 if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
4201 error = kern_fhstatfs(td, fh, &sf);
4203 error = copyout(&sf, uap->buf, sizeof(sf));
4208 kern_fhstatfs(struct thread *td, fhandle_t fh, struct statfs *buf)
4218 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
4220 if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)))
4226 error = mac_check_mount_stat(td->td_ucred, mp);
4231 * Set these in case the underlying filesystem fails to do so.
4233 sp->f_version = STATFS_VERSION;
4234 sp->f_namemax = NAME_MAX;
4235 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
4236 if ((error = VFS_STATFS(mp, sp, td)) != 0)
4243 * Syscall to push extended attribute configuration information into the
4244 * VFS. Accepts a path, which it converts to a mountpoint, as well as
4245 * a command (int cmd), and attribute name and misc data. For now, the
4246 * attribute name is left in userspace for consumption by the VFS_op.
4247 * It will probably be changed to be copied into sysspace by the
4248 * syscall in the future, once issues with various consumers of the
4249 * attribute code have raised their hands.
4251 * Currently this is used only by UFS Extended Attributes.
4256 struct extattrctl_args /* {
4259 const char *filename;
4261 const char *attrname;
4264 struct vnode *filename_vp;
4265 struct nameidata nd;
4266 struct mount *mp, *mp_writable;
4267 char attrname[EXTATTR_MAXNAMELEN];
4271 * uap->attrname is not always defined. We check again later when we
4272 * invoke the VFS call so as to pass in NULL there if needed.
4274 if (uap->attrname != NULL) {
4275 error = copyinstr(uap->attrname, attrname, EXTATTR_MAXNAMELEN,
4282 * uap->filename is not always defined. If it is, grab a vnode lock,
4283 * which VFS_EXTATTRCTL() will later release.
4286 if (uap->filename != NULL) {
4287 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
4292 filename_vp = nd.ni_vp;
4293 NDFREE(&nd, NDF_NO_VP_RELE | NDF_NO_VP_UNLOCK);
4296 /* uap->path is always defined. */
4297 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, uap->path, td);
4300 if (filename_vp != NULL)
4304 mp = nd.ni_vp->v_mount;
4305 error = vn_start_write(nd.ni_vp, &mp_writable, V_WAIT | PCATCH);
4308 if (filename_vp != NULL)
4313 error = VFS_EXTATTRCTL(mp, uap->cmd, filename_vp, uap->attrnamespace,
4314 uap->attrname != NULL ? attrname : NULL, td);
4316 vn_finished_write(mp_writable);
4318 * VFS_EXTATTRCTL will have unlocked, but not de-ref'd,
4319 * filename_vp, so vrele it if it is defined.
4321 if (filename_vp != NULL)
4327 * Set a named extended attribute on a file or directory
4329 * Arguments: unlocked vnode "vp", attribute namespace "attrnamespace",
4330 * kernelspace string pointer "attrname", userspace buffer
4331 * pointer "data", buffer length "nbytes", thread "td".
4332 * Returns: 0 on success, an error number otherwise
4334 * References: vp must be a valid reference for the duration of the call
4337 extattr_set_vp(struct vnode *vp, int attrnamespace, const char *attrname,
4338 void *data, size_t nbytes, struct thread *td)
4346 error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
4349 VOP_LEASE(vp, td, td->td_ucred, LEASE_WRITE);
4350 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
4352 aiov.iov_base = data;
4353 aiov.iov_len = nbytes;
4354 auio.uio_iov = &aiov;
4355 auio.uio_iovcnt = 1;
4356 auio.uio_offset = 0;
4357 if (nbytes > INT_MAX) {
4361 auio.uio_resid = nbytes;
4362 auio.uio_rw = UIO_WRITE;
4363 auio.uio_segflg = UIO_USERSPACE;
4368 error = mac_check_vnode_setextattr(td->td_ucred, vp, attrnamespace,
4374 error = VOP_SETEXTATTR(vp, attrnamespace, attrname, &auio,
4376 cnt -= auio.uio_resid;
4377 td->td_retval[0] = cnt;
4380 VOP_UNLOCK(vp, 0, td);
4381 vn_finished_write(mp);
4386 extattr_set_fd(td, uap)
4388 struct extattr_set_fd_args /* {
4391 const char *attrname;
4397 char attrname[EXTATTR_MAXNAMELEN];
4400 error = copyinstr(uap->attrname, attrname, EXTATTR_MAXNAMELEN, NULL);
4404 error = getvnode(td->td_proc->p_fd, uap->fd, &fp);
4408 error = extattr_set_vp(fp->f_vnode, uap->attrnamespace,
4409 attrname, uap->data, uap->nbytes, td);
4416 extattr_set_file(td, uap)
4418 struct extattr_set_file_args /* {
4421 const char *attrname;
4426 struct nameidata nd;
4427 char attrname[EXTATTR_MAXNAMELEN];
4430 error = copyinstr(uap->attrname, attrname, EXTATTR_MAXNAMELEN, NULL);
4434 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, uap->path, td);
4438 NDFREE(&nd, NDF_ONLY_PNBUF);
4440 error = extattr_set_vp(nd.ni_vp, uap->attrnamespace, attrname,
4441 uap->data, uap->nbytes, td);
4448 extattr_set_link(td, uap)
4450 struct extattr_set_link_args /* {
4453 const char *attrname;
4458 struct nameidata nd;
4459 char attrname[EXTATTR_MAXNAMELEN];
4462 error = copyinstr(uap->attrname, attrname, EXTATTR_MAXNAMELEN, NULL);
4466 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, uap->path, td);
4470 NDFREE(&nd, NDF_ONLY_PNBUF);
4472 error = extattr_set_vp(nd.ni_vp, uap->attrnamespace, attrname,
4473 uap->data, uap->nbytes, td);
4480 * Get a named extended attribute on a file or directory
4482 * Arguments: unlocked vnode "vp", attribute namespace "attrnamespace",
4483 * kernelspace string pointer "attrname", userspace buffer
4484 * pointer "data", buffer length "nbytes", thread "td".
4485 * Returns: 0 on success, an error number otherwise
4487 * References: vp must be a valid reference for the duration of the call
4490 extattr_get_vp(struct vnode *vp, int attrnamespace, const char *attrname,
4491 void *data, size_t nbytes, struct thread *td)
4493 struct uio auio, *auiop;
4496 size_t size, *sizep;
4499 VOP_LEASE(vp, td, td->td_ucred, LEASE_READ);
4500 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
4503 * Slightly unusual semantics: if the user provides a NULL data
4504 * pointer, they don't want to receive the data, just the
4505 * maximum read length.
4511 aiov.iov_base = data;
4512 aiov.iov_len = nbytes;
4513 auio.uio_iov = &aiov;
4514 auio.uio_offset = 0;
4515 if (nbytes > INT_MAX) {
4519 auio.uio_resid = nbytes;
4520 auio.uio_rw = UIO_READ;
4521 auio.uio_segflg = UIO_USERSPACE;
4529 error = mac_check_vnode_getextattr(td->td_ucred, vp, attrnamespace,
4535 error = VOP_GETEXTATTR(vp, attrnamespace, attrname, auiop, sizep,
4538 if (auiop != NULL) {
4539 cnt -= auio.uio_resid;
4540 td->td_retval[0] = cnt;
4542 td->td_retval[0] = size;
4545 VOP_UNLOCK(vp, 0, td);
4550 extattr_get_fd(td, uap)
4552 struct extattr_get_fd_args /* {
4555 const char *attrname;
4561 char attrname[EXTATTR_MAXNAMELEN];
4564 error = copyinstr(uap->attrname, attrname, EXTATTR_MAXNAMELEN, NULL);
4568 error = getvnode(td->td_proc->p_fd, uap->fd, &fp);
4572 error = extattr_get_vp(fp->f_vnode, uap->attrnamespace,
4573 attrname, uap->data, uap->nbytes, td);
4580 extattr_get_file(td, uap)
4582 struct extattr_get_file_args /* {
4585 const char *attrname;
4590 struct nameidata nd;
4591 char attrname[EXTATTR_MAXNAMELEN];
4594 error = copyinstr(uap->attrname, attrname, EXTATTR_MAXNAMELEN, NULL);
4598 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, uap->path, td);
4602 NDFREE(&nd, NDF_ONLY_PNBUF);
4604 error = extattr_get_vp(nd.ni_vp, uap->attrnamespace, attrname,
4605 uap->data, uap->nbytes, td);
4612 extattr_get_link(td, uap)
4614 struct extattr_get_link_args /* {
4617 const char *attrname;
4622 struct nameidata nd;
4623 char attrname[EXTATTR_MAXNAMELEN];
4626 error = copyinstr(uap->attrname, attrname, EXTATTR_MAXNAMELEN, NULL);
4630 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, uap->path, td);
4634 NDFREE(&nd, NDF_ONLY_PNBUF);
4636 error = extattr_get_vp(nd.ni_vp, uap->attrnamespace, attrname,
4637 uap->data, uap->nbytes, td);
4644 * extattr_delete_vp(): Delete a named extended attribute on a file or
4647 * Arguments: unlocked vnode "vp", attribute namespace "attrnamespace",
4648 * kernelspace string pointer "attrname", proc "p"
4649 * Returns: 0 on success, an error number otherwise
4651 * References: vp must be a valid reference for the duration of the call
4654 extattr_delete_vp(struct vnode *vp, int attrnamespace, const char *attrname,
4660 error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
4663 VOP_LEASE(vp, td, td->td_ucred, LEASE_WRITE);
4664 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
4667 error = mac_check_vnode_deleteextattr(td->td_ucred, vp, attrnamespace,
4673 error = VOP_DELETEEXTATTR(vp, attrnamespace, attrname, td->td_ucred,
4675 if (error == EOPNOTSUPP)
4676 error = VOP_SETEXTATTR(vp, attrnamespace, attrname, NULL,
4681 VOP_UNLOCK(vp, 0, td);
4682 vn_finished_write(mp);
4687 extattr_delete_fd(td, uap)
4689 struct extattr_delete_fd_args /* {
4692 const char *attrname;
4697 char attrname[EXTATTR_MAXNAMELEN];
4700 error = copyinstr(uap->attrname, attrname, EXTATTR_MAXNAMELEN, NULL);
4704 error = getvnode(td->td_proc->p_fd, uap->fd, &fp);
4709 error = extattr_delete_vp(vp, uap->attrnamespace, attrname, td);
4715 extattr_delete_file(td, uap)
4717 struct extattr_delete_file_args /* {
4720 const char *attrname;
4723 struct nameidata nd;
4724 char attrname[EXTATTR_MAXNAMELEN];
4727 error = copyinstr(uap->attrname, attrname, EXTATTR_MAXNAMELEN, NULL);
4731 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, uap->path, td);
4735 NDFREE(&nd, NDF_ONLY_PNBUF);
4737 error = extattr_delete_vp(nd.ni_vp, uap->attrnamespace, attrname, td);
4743 extattr_delete_link(td, uap)
4745 struct extattr_delete_link_args /* {
4748 const char *attrname;
4751 struct nameidata nd;
4752 char attrname[EXTATTR_MAXNAMELEN];
4755 error = copyinstr(uap->attrname, attrname, EXTATTR_MAXNAMELEN, NULL);
4759 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, uap->path, td);
4763 NDFREE(&nd, NDF_ONLY_PNBUF);
4765 error = extattr_delete_vp(nd.ni_vp, uap->attrnamespace, attrname, td);
4771 * Retrieve a list of extended attributes on a file or directory.
4773 * Arguments: unlocked vnode "vp", attribute namespace 'attrnamespace",
4774 * userspace buffer pointer "data", buffer length "nbytes",
4776 * Returns: 0 on success, an error number otherwise
4778 * References: vp must be a valid reference for the duration of the call
4781 extattr_list_vp(struct vnode *vp, int attrnamespace, void *data,
4782 size_t nbytes, struct thread *td)
4784 struct uio auio, *auiop;
4785 size_t size, *sizep;
4790 VOP_LEASE(vp, td, td->td_ucred, LEASE_READ);
4791 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
4797 aiov.iov_base = data;
4798 aiov.iov_len = nbytes;
4799 auio.uio_iov = &aiov;
4800 auio.uio_offset = 0;
4801 if (nbytes > INT_MAX) {
4805 auio.uio_resid = nbytes;
4806 auio.uio_rw = UIO_READ;
4807 auio.uio_segflg = UIO_USERSPACE;
4815 error = mac_check_vnode_listextattr(td->td_ucred, vp, attrnamespace);
4820 error = VOP_LISTEXTATTR(vp, attrnamespace, auiop, sizep,
4823 if (auiop != NULL) {
4824 cnt -= auio.uio_resid;
4825 td->td_retval[0] = cnt;
4827 td->td_retval[0] = size;
4830 VOP_UNLOCK(vp, 0, td);
4836 extattr_list_fd(td, uap)
4838 struct extattr_list_fd_args /* {
4848 error = getvnode(td->td_proc->p_fd, uap->fd, &fp);
4852 error = extattr_list_vp(fp->f_vnode, uap->attrnamespace, uap->data,
4860 extattr_list_file(td, uap)
4862 struct extattr_list_file_args /* {
4869 struct nameidata nd;
4872 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, uap->path, td);
4876 NDFREE(&nd, NDF_ONLY_PNBUF);
4878 error = extattr_list_vp(nd.ni_vp, uap->attrnamespace, uap->data,
4886 extattr_list_link(td, uap)
4888 struct extattr_list_link_args /* {
4895 struct nameidata nd;
4898 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, uap->path, td);
4902 NDFREE(&nd, NDF_ONLY_PNBUF);
4904 error = extattr_list_vp(nd.ni_vp, uap->attrnamespace, uap->data,