2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
6 * (c) UNIX System Laboratories, Inc.
7 * All or some portions of this file are derived from material licensed
8 * to the University of California by American Telephone and Telegraph
9 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10 * the permission of UNIX System Laboratories, Inc.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * @(#)vfs_syscalls.c 8.13 (Berkeley) 4/15/94
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
42 #include "opt_capsicum.h"
43 #include "opt_ktrace.h"
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #ifdef COMPAT_FREEBSD11
48 #include <sys/abi_compat.h>
52 #include <sys/capsicum.h>
54 #include <sys/sysent.h>
55 #include <sys/malloc.h>
56 #include <sys/mount.h>
57 #include <sys/mutex.h>
58 #include <sys/sysproto.h>
59 #include <sys/namei.h>
60 #include <sys/filedesc.h>
61 #include <sys/kernel.h>
62 #include <sys/fcntl.h>
64 #include <sys/filio.h>
65 #include <sys/limits.h>
66 #include <sys/linker.h>
67 #include <sys/rwlock.h>
71 #include <sys/unistd.h>
72 #include <sys/vnode.h>
75 #include <sys/dirent.h>
77 #include <sys/syscallsubr.h>
78 #include <sys/sysctl.h>
80 #include <sys/ktrace.h>
83 #include <machine/stdarg.h>
85 #include <security/audit/audit.h>
86 #include <security/mac/mac_framework.h>
89 #include <vm/vm_object.h>
90 #include <vm/vm_page.h>
93 #include <fs/devfs/devfs.h>
95 MALLOC_DEFINE(M_FADVISE, "fadvise", "posix_fadvise(2) information");
97 static int kern_chflagsat(struct thread *td, int fd, const char *path,
98 enum uio_seg pathseg, u_long flags, int atflag);
99 static int setfflags(struct thread *td, struct vnode *, u_long);
100 static int getutimes(const struct timeval *, enum uio_seg, struct timespec *);
101 static int getutimens(const struct timespec *, enum uio_seg,
102 struct timespec *, int *);
103 static int setutimes(struct thread *td, struct vnode *,
104 const struct timespec *, int, int);
105 static int vn_access(struct vnode *vp, int user_flags, struct ucred *cred,
107 static int kern_fhlinkat(struct thread *td, int fd, const char *path,
108 enum uio_seg pathseg, fhandle_t *fhp);
109 static int kern_readlink_vp(struct vnode *vp, char *buf, enum uio_seg bufseg,
110 size_t count, struct thread *td);
111 static int kern_linkat_vp(struct thread *td, struct vnode *vp, int fd,
112 const char *path, enum uio_seg segflag);
115 at2cnpflags(u_int at_flags, u_int mask)
119 MPASS((at_flags & (AT_SYMLINK_FOLLOW | AT_SYMLINK_NOFOLLOW)) !=
120 (AT_SYMLINK_FOLLOW | AT_SYMLINK_NOFOLLOW));
124 if ((at_flags & AT_RESOLVE_BENEATH) != 0)
126 if ((at_flags & AT_SYMLINK_FOLLOW) != 0)
128 /* NOFOLLOW is pseudo flag */
129 if ((mask & AT_SYMLINK_NOFOLLOW) != 0) {
130 res |= (at_flags & AT_SYMLINK_NOFOLLOW) != 0 ? NOFOLLOW :
133 if ((mask & AT_EMPTY_PATH) != 0 && (at_flags & AT_EMPTY_PATH) != 0)
139 kern_sync(struct thread *td)
141 struct mount *mp, *nmp;
144 mtx_lock(&mountlist_mtx);
145 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
146 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK)) {
147 nmp = TAILQ_NEXT(mp, mnt_list);
150 if ((mp->mnt_flag & MNT_RDONLY) == 0 &&
151 vn_start_write(NULL, &mp, V_NOWAIT) == 0) {
152 save = curthread_pflags_set(TDP_SYNCIO);
153 vfs_periodic(mp, MNT_NOWAIT);
154 VFS_SYNC(mp, MNT_NOWAIT);
155 curthread_pflags_restore(save);
156 vn_finished_write(mp);
158 mtx_lock(&mountlist_mtx);
159 nmp = TAILQ_NEXT(mp, mnt_list);
162 mtx_unlock(&mountlist_mtx);
167 * Sync each mounted filesystem.
169 #ifndef _SYS_SYSPROTO_H_
176 sys_sync(struct thread *td, struct sync_args *uap)
179 return (kern_sync(td));
183 * Change filesystem quotas.
185 #ifndef _SYS_SYSPROTO_H_
186 struct quotactl_args {
194 sys_quotactl(struct thread *td, struct quotactl_args *uap)
201 AUDIT_ARG_CMD(uap->cmd);
202 AUDIT_ARG_UID(uap->uid);
203 if (!prison_allow(td->td_ucred, PR_ALLOW_QUOTAS))
205 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
207 if ((error = namei(&nd)) != 0)
210 mp = nd.ni_vp->v_mount;
213 error = vfs_busy(mp, 0);
219 error = VFS_QUOTACTL(mp, uap->cmd, uap->uid, uap->arg, &mp_busy);
222 * Since quota on/off operations typically need to open quota
223 * files, the implementation may need to unbusy the mount point
224 * before calling into namei. Otherwise, unmount might be
225 * started between two vfs_busy() invocations (first is ours,
226 * second is from mount point cross-walk code in lookup()),
229 * Avoid unbusying mp if the implementation indicates it has
239 * Used by statfs conversion routines to scale the block size up if
240 * necessary so that all of the block counts are <= 'max_size'. Note
241 * that 'max_size' should be a bitmask, i.e. 2^n - 1 for some non-zero
245 statfs_scale_blocks(struct statfs *sf, long max_size)
250 KASSERT(powerof2(max_size + 1), ("%s: invalid max_size", __func__));
253 * Attempt to scale the block counts to give a more accurate
254 * overview to userland of the ratio of free space to used
255 * space. To do this, find the largest block count and compute
256 * a divisor that lets it fit into a signed integer <= max_size.
258 if (sf->f_bavail < 0)
259 count = -sf->f_bavail;
261 count = sf->f_bavail;
262 count = MAX(sf->f_blocks, MAX(sf->f_bfree, count));
263 if (count <= max_size)
266 count >>= flsl(max_size);
273 sf->f_bsize <<= shift;
274 sf->f_blocks >>= shift;
275 sf->f_bfree >>= shift;
276 sf->f_bavail >>= shift;
280 kern_do_statfs(struct thread *td, struct mount *mp, struct statfs *buf)
286 error = vfs_busy(mp, 0);
291 error = mac_mount_check_stat(td->td_ucred, mp);
295 error = VFS_STATFS(mp, buf);
298 if (priv_check_cred_vfs_generation(td->td_ucred)) {
299 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
300 prison_enforce_statfs(td->td_ucred, mp, buf);
308 * Get filesystem statistics.
310 #ifndef _SYS_SYSPROTO_H_
317 sys_statfs(struct thread *td, struct statfs_args *uap)
322 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
323 error = kern_statfs(td, uap->path, UIO_USERSPACE, sfp);
325 error = copyout(sfp, uap->buf, sizeof(struct statfs));
331 kern_statfs(struct thread *td, const char *path, enum uio_seg pathseg,
338 NDINIT(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path);
342 mp = vfs_ref_from_vp(nd.ni_vp);
345 return (kern_do_statfs(td, mp, buf));
349 * Get filesystem statistics.
351 #ifndef _SYS_SYSPROTO_H_
352 struct fstatfs_args {
358 sys_fstatfs(struct thread *td, struct fstatfs_args *uap)
363 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
364 error = kern_fstatfs(td, uap->fd, sfp);
366 error = copyout(sfp, uap->buf, sizeof(struct statfs));
372 kern_fstatfs(struct thread *td, int fd, struct statfs *buf)
380 error = getvnode_path(td, fd, &cap_fstatfs_rights, &fp);
385 if (AUDITING_TD(td)) {
386 vn_lock(vp, LK_SHARED | LK_RETRY);
387 AUDIT_ARG_VNODE1(vp);
391 mp = vfs_ref_from_vp(vp);
393 return (kern_do_statfs(td, mp, buf));
397 * Get statistics on all filesystems.
399 #ifndef _SYS_SYSPROTO_H_
400 struct getfsstat_args {
407 sys_getfsstat(struct thread *td, struct getfsstat_args *uap)
412 if (uap->bufsize < 0 || uap->bufsize > SIZE_MAX)
414 error = kern_getfsstat(td, &uap->buf, uap->bufsize, &count,
415 UIO_USERSPACE, uap->mode);
417 td->td_retval[0] = count;
422 * If (bufsize > 0 && bufseg == UIO_SYSSPACE)
423 * The caller is responsible for freeing memory which will be allocated
427 kern_getfsstat(struct thread *td, struct statfs **buf, size_t bufsize,
428 size_t *countp, enum uio_seg bufseg, int mode)
430 struct mount *mp, *nmp;
431 struct statfs *sfsp, *sp, *sptmp, *tofree;
432 size_t count, maxcount;
440 if (bufseg == UIO_SYSSPACE)
445 maxcount = bufsize / sizeof(struct statfs);
449 } else if (bufseg == UIO_USERSPACE) {
452 } else /* if (bufseg == UIO_SYSSPACE) */ {
454 mtx_lock(&mountlist_mtx);
455 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
458 mtx_unlock(&mountlist_mtx);
459 if (maxcount > count)
461 tofree = sfsp = *buf = malloc(maxcount * sizeof(struct statfs),
468 * If there is no target buffer they only want the count.
470 * This could be TAILQ_FOREACH but it is open-coded to match the original
474 mtx_lock(&mountlist_mtx);
475 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
476 if (prison_canseemount(td->td_ucred, mp) != 0) {
477 nmp = TAILQ_NEXT(mp, mnt_list);
481 if (mac_mount_check_stat(td->td_ucred, mp) != 0) {
482 nmp = TAILQ_NEXT(mp, mnt_list);
487 nmp = TAILQ_NEXT(mp, mnt_list);
489 mtx_unlock(&mountlist_mtx);
495 * They want the entire thing.
497 * Short-circuit the corner case of no room for anything, avoids
504 mtx_lock(&mountlist_mtx);
505 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
506 if (prison_canseemount(td->td_ucred, mp) != 0) {
507 nmp = TAILQ_NEXT(mp, mnt_list);
511 if (mac_mount_check_stat(td->td_ucred, mp) != 0) {
512 nmp = TAILQ_NEXT(mp, mnt_list);
516 if (mode == MNT_WAIT) {
517 if (vfs_busy(mp, MBF_MNTLSTLOCK) != 0) {
519 * If vfs_busy() failed, and MBF_NOWAIT
520 * wasn't passed, then the mp is gone.
521 * Furthermore, because of MBF_MNTLSTLOCK,
522 * the mountlist_mtx was dropped. We have
523 * no other choice than to start over.
525 mtx_unlock(&mountlist_mtx);
526 free(tofree, M_STATFS);
530 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK) != 0) {
531 nmp = TAILQ_NEXT(mp, mnt_list);
537 * If MNT_NOWAIT is specified, do not refresh
540 if (mode != MNT_NOWAIT) {
541 error = VFS_STATFS(mp, sp);
543 mtx_lock(&mountlist_mtx);
544 nmp = TAILQ_NEXT(mp, mnt_list);
549 if (priv_check_cred_vfs_generation(td->td_ucred)) {
550 sptmp = malloc(sizeof(struct statfs), M_STATFS,
553 sptmp->f_fsid.val[0] = sptmp->f_fsid.val[1] = 0;
554 prison_enforce_statfs(td->td_ucred, mp, sptmp);
558 if (bufseg == UIO_SYSSPACE) {
559 bcopy(sp, sfsp, sizeof(*sp));
560 free(sptmp, M_STATFS);
561 } else /* if (bufseg == UIO_USERSPACE) */ {
562 error = copyout(sp, sfsp, sizeof(*sp));
563 free(sptmp, M_STATFS);
572 if (count == maxcount) {
577 mtx_lock(&mountlist_mtx);
578 nmp = TAILQ_NEXT(mp, mnt_list);
581 mtx_unlock(&mountlist_mtx);
587 #ifdef COMPAT_FREEBSD4
589 * Get old format filesystem statistics.
591 static void freebsd4_cvtstatfs(struct statfs *, struct ostatfs *);
593 #ifndef _SYS_SYSPROTO_H_
594 struct freebsd4_statfs_args {
600 freebsd4_statfs(struct thread *td, struct freebsd4_statfs_args *uap)
606 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
607 error = kern_statfs(td, uap->path, UIO_USERSPACE, sfp);
609 freebsd4_cvtstatfs(sfp, &osb);
610 error = copyout(&osb, uap->buf, sizeof(osb));
617 * Get filesystem statistics.
619 #ifndef _SYS_SYSPROTO_H_
620 struct freebsd4_fstatfs_args {
626 freebsd4_fstatfs(struct thread *td, struct freebsd4_fstatfs_args *uap)
632 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
633 error = kern_fstatfs(td, uap->fd, sfp);
635 freebsd4_cvtstatfs(sfp, &osb);
636 error = copyout(&osb, uap->buf, sizeof(osb));
643 * Get statistics on all filesystems.
645 #ifndef _SYS_SYSPROTO_H_
646 struct freebsd4_getfsstat_args {
653 freebsd4_getfsstat(struct thread *td, struct freebsd4_getfsstat_args *uap)
655 struct statfs *buf, *sp;
660 if (uap->bufsize < 0)
662 count = uap->bufsize / sizeof(struct ostatfs);
663 if (count > SIZE_MAX / sizeof(struct statfs))
665 size = count * sizeof(struct statfs);
666 error = kern_getfsstat(td, &buf, size, &count, UIO_SYSSPACE,
669 td->td_retval[0] = count;
672 while (count != 0 && error == 0) {
673 freebsd4_cvtstatfs(sp, &osb);
674 error = copyout(&osb, uap->buf, sizeof(osb));
685 * Implement fstatfs() for (NFS) file handles.
687 #ifndef _SYS_SYSPROTO_H_
688 struct freebsd4_fhstatfs_args {
689 struct fhandle *u_fhp;
694 freebsd4_fhstatfs(struct thread *td, struct freebsd4_fhstatfs_args *uap)
701 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
704 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
705 error = kern_fhstatfs(td, fh, sfp);
707 freebsd4_cvtstatfs(sfp, &osb);
708 error = copyout(&osb, uap->buf, sizeof(osb));
715 * Convert a new format statfs structure to an old format statfs structure.
718 freebsd4_cvtstatfs(struct statfs *nsp, struct ostatfs *osp)
721 statfs_scale_blocks(nsp, LONG_MAX);
722 bzero(osp, sizeof(*osp));
723 osp->f_bsize = nsp->f_bsize;
724 osp->f_iosize = MIN(nsp->f_iosize, LONG_MAX);
725 osp->f_blocks = nsp->f_blocks;
726 osp->f_bfree = nsp->f_bfree;
727 osp->f_bavail = nsp->f_bavail;
728 osp->f_files = MIN(nsp->f_files, LONG_MAX);
729 osp->f_ffree = MIN(nsp->f_ffree, LONG_MAX);
730 osp->f_owner = nsp->f_owner;
731 osp->f_type = nsp->f_type;
732 osp->f_flags = nsp->f_flags;
733 osp->f_syncwrites = MIN(nsp->f_syncwrites, LONG_MAX);
734 osp->f_asyncwrites = MIN(nsp->f_asyncwrites, LONG_MAX);
735 osp->f_syncreads = MIN(nsp->f_syncreads, LONG_MAX);
736 osp->f_asyncreads = MIN(nsp->f_asyncreads, LONG_MAX);
737 strlcpy(osp->f_fstypename, nsp->f_fstypename,
738 MIN(MFSNAMELEN, OMFSNAMELEN));
739 strlcpy(osp->f_mntonname, nsp->f_mntonname,
740 MIN(MNAMELEN, OMNAMELEN));
741 strlcpy(osp->f_mntfromname, nsp->f_mntfromname,
742 MIN(MNAMELEN, OMNAMELEN));
743 osp->f_fsid = nsp->f_fsid;
745 #endif /* COMPAT_FREEBSD4 */
747 #if defined(COMPAT_FREEBSD11)
749 * Get old format filesystem statistics.
751 static void freebsd11_cvtstatfs(struct statfs *, struct freebsd11_statfs *);
754 freebsd11_statfs(struct thread *td, struct freebsd11_statfs_args *uap)
756 struct freebsd11_statfs osb;
760 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
761 error = kern_statfs(td, uap->path, UIO_USERSPACE, sfp);
763 freebsd11_cvtstatfs(sfp, &osb);
764 error = copyout(&osb, uap->buf, sizeof(osb));
771 * Get filesystem statistics.
774 freebsd11_fstatfs(struct thread *td, struct freebsd11_fstatfs_args *uap)
776 struct freebsd11_statfs osb;
780 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
781 error = kern_fstatfs(td, uap->fd, sfp);
783 freebsd11_cvtstatfs(sfp, &osb);
784 error = copyout(&osb, uap->buf, sizeof(osb));
791 * Get statistics on all filesystems.
794 freebsd11_getfsstat(struct thread *td, struct freebsd11_getfsstat_args *uap)
796 return (kern_freebsd11_getfsstat(td, uap->buf, uap->bufsize, uap->mode));
800 kern_freebsd11_getfsstat(struct thread *td, struct freebsd11_statfs * ubuf,
801 long bufsize, int mode)
803 struct freebsd11_statfs osb;
804 struct statfs *buf, *sp;
811 count = bufsize / sizeof(struct ostatfs);
812 size = count * sizeof(struct statfs);
813 error = kern_getfsstat(td, &buf, size, &count, UIO_SYSSPACE, mode);
815 td->td_retval[0] = count;
818 while (count > 0 && error == 0) {
819 freebsd11_cvtstatfs(sp, &osb);
820 error = copyout(&osb, ubuf, sizeof(osb));
831 * Implement fstatfs() for (NFS) file handles.
834 freebsd11_fhstatfs(struct thread *td, struct freebsd11_fhstatfs_args *uap)
836 struct freebsd11_statfs osb;
841 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
844 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
845 error = kern_fhstatfs(td, fh, sfp);
847 freebsd11_cvtstatfs(sfp, &osb);
848 error = copyout(&osb, uap->buf, sizeof(osb));
855 * Convert a new format statfs structure to an old format statfs structure.
858 freebsd11_cvtstatfs(struct statfs *nsp, struct freebsd11_statfs *osp)
861 bzero(osp, sizeof(*osp));
862 osp->f_version = FREEBSD11_STATFS_VERSION;
863 osp->f_type = nsp->f_type;
864 osp->f_flags = nsp->f_flags;
865 osp->f_bsize = nsp->f_bsize;
866 osp->f_iosize = nsp->f_iosize;
867 osp->f_blocks = nsp->f_blocks;
868 osp->f_bfree = nsp->f_bfree;
869 osp->f_bavail = nsp->f_bavail;
870 osp->f_files = nsp->f_files;
871 osp->f_ffree = nsp->f_ffree;
872 osp->f_syncwrites = nsp->f_syncwrites;
873 osp->f_asyncwrites = nsp->f_asyncwrites;
874 osp->f_syncreads = nsp->f_syncreads;
875 osp->f_asyncreads = nsp->f_asyncreads;
876 osp->f_namemax = nsp->f_namemax;
877 osp->f_owner = nsp->f_owner;
878 osp->f_fsid = nsp->f_fsid;
879 strlcpy(osp->f_fstypename, nsp->f_fstypename,
880 MIN(MFSNAMELEN, sizeof(osp->f_fstypename)));
881 strlcpy(osp->f_mntonname, nsp->f_mntonname,
882 MIN(MNAMELEN, sizeof(osp->f_mntonname)));
883 strlcpy(osp->f_mntfromname, nsp->f_mntfromname,
884 MIN(MNAMELEN, sizeof(osp->f_mntfromname)));
886 #endif /* COMPAT_FREEBSD11 */
889 * Change current working directory to a given file descriptor.
891 #ifndef _SYS_SYSPROTO_H_
897 sys_fchdir(struct thread *td, struct fchdir_args *uap)
899 struct vnode *vp, *tdp;
904 AUDIT_ARG_FD(uap->fd);
905 error = getvnode_path(td, uap->fd, &cap_fchdir_rights,
912 vn_lock(vp, LK_SHARED | LK_RETRY);
913 AUDIT_ARG_VNODE1(vp);
914 error = change_dir(vp, td);
915 while (!error && (mp = vp->v_mountedhere) != NULL) {
918 error = VFS_ROOT(mp, LK_SHARED, &tdp);
935 * Change current working directory (``.'').
937 #ifndef _SYS_SYSPROTO_H_
943 sys_chdir(struct thread *td, struct chdir_args *uap)
946 return (kern_chdir(td, uap->path, UIO_USERSPACE));
950 kern_chdir(struct thread *td, const char *path, enum uio_seg pathseg)
955 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
957 if ((error = namei(&nd)) != 0)
959 if ((error = change_dir(nd.ni_vp, td)) != 0) {
964 VOP_UNLOCK(nd.ni_vp);
966 pwd_chdir(td, nd.ni_vp);
970 static int unprivileged_chroot = 0;
971 SYSCTL_INT(_security_bsd, OID_AUTO, unprivileged_chroot, CTLFLAG_RW,
972 &unprivileged_chroot, 0,
973 "Unprivileged processes can use chroot(2)");
975 * Change notion of root (``/'') directory.
977 #ifndef _SYS_SYSPROTO_H_
983 sys_chroot(struct thread *td, struct chroot_args *uap)
989 error = priv_check(td, PRIV_VFS_CHROOT);
993 if (unprivileged_chroot == 0 ||
994 (p->p_flag2 & P2_NO_NEW_PRIVS) == 0) {
1000 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
1001 UIO_USERSPACE, uap->path);
1005 error = change_dir(nd.ni_vp, td);
1009 error = mac_vnode_check_chroot(td->td_ucred, nd.ni_vp);
1013 VOP_UNLOCK(nd.ni_vp);
1014 error = pwd_chroot(td, nd.ni_vp);
1016 NDFREE_NOTHING(&nd);
1021 NDFREE_NOTHING(&nd);
1026 * Common routine for chroot and chdir. Callers must provide a locked vnode
1030 change_dir(struct vnode *vp, struct thread *td)
1036 ASSERT_VOP_LOCKED(vp, "change_dir(): vp not locked");
1037 if (vp->v_type != VDIR)
1040 error = mac_vnode_check_chdir(td->td_ucred, vp);
1044 return (VOP_ACCESS(vp, VEXEC, td->td_ucred, td));
1047 static __inline void
1048 flags_to_rights(int flags, cap_rights_t *rightsp)
1050 if (flags & O_EXEC) {
1051 cap_rights_set_one(rightsp, CAP_FEXECVE);
1055 switch ((flags & O_ACCMODE)) {
1057 cap_rights_set_one(rightsp, CAP_READ);
1060 cap_rights_set_one(rightsp, CAP_READ);
1063 cap_rights_set_one(rightsp, CAP_WRITE);
1064 if (!(flags & (O_APPEND | O_TRUNC)))
1065 cap_rights_set_one(rightsp, CAP_SEEK);
1070 if (flags & O_CREAT)
1071 cap_rights_set_one(rightsp, CAP_CREATE);
1073 if (flags & O_TRUNC)
1074 cap_rights_set_one(rightsp, CAP_FTRUNCATE);
1076 if (flags & (O_SYNC | O_FSYNC))
1077 cap_rights_set_one(rightsp, CAP_FSYNC);
1079 if (flags & (O_EXLOCK | O_SHLOCK))
1080 cap_rights_set_one(rightsp, CAP_FLOCK);
1084 * Check permissions, allocate an open file structure, and call the device
1085 * open routine if any.
1087 #ifndef _SYS_SYSPROTO_H_
1095 sys_open(struct thread *td, struct open_args *uap)
1098 return (kern_openat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1099 uap->flags, uap->mode));
1102 #ifndef _SYS_SYSPROTO_H_
1103 struct openat_args {
1111 sys_openat(struct thread *td, struct openat_args *uap)
1114 AUDIT_ARG_FD(uap->fd);
1115 return (kern_openat(td, uap->fd, uap->path, UIO_USERSPACE, uap->flag,
1120 kern_openat(struct thread *td, int fd, const char *path, enum uio_seg pathseg,
1121 int flags, int mode)
1123 struct proc *p = td->td_proc;
1124 struct filedesc *fdp;
1125 struct pwddesc *pdp;
1128 struct nameidata nd;
1129 cap_rights_t rights;
1130 int cmode, error, indx;
1136 AUDIT_ARG_FFLAGS(flags);
1137 AUDIT_ARG_MODE(mode);
1138 cap_rights_init_one(&rights, CAP_LOOKUP);
1139 flags_to_rights(flags, &rights);
1142 * Only one of the O_EXEC, O_RDONLY, O_WRONLY and O_RDWR flags
1143 * may be specified. On the other hand, for O_PATH any mode
1144 * except O_EXEC is ignored.
1146 if ((flags & O_PATH) != 0) {
1147 flags &= ~(O_CREAT | O_ACCMODE);
1148 } else if ((flags & O_EXEC) != 0) {
1149 if (flags & O_ACCMODE)
1151 } else if ((flags & O_ACCMODE) == O_ACCMODE) {
1154 flags = FFLAGS(flags);
1158 * Allocate a file structure. The descriptor to reference it
1159 * is allocated and used by finstall_refed() below.
1161 error = falloc_noinstall(td, &fp);
1164 /* Set the flags early so the finit in devfs can pick them up. */
1165 fp->f_flag = flags & FMASK;
1166 cmode = ((mode & ~pdp->pd_cmask) & ALLPERMS) & ~S_ISTXT;
1167 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | AUDITVNODE1 | WANTIOCTLCAPS,
1168 pathseg, path, fd, &rights);
1169 td->td_dupfd = -1; /* XXX check for fdopen */
1170 error = vn_open_cred(&nd, &flags, cmode, VN_OPEN_WANTIOCTLCAPS,
1174 * If the vn_open replaced the method vector, something
1175 * wonderous happened deep below and we just pass it up
1176 * pretending we know what we do.
1178 if (error == ENXIO && fp->f_ops != &badfileops) {
1179 MPASS((flags & O_PATH) == 0);
1184 * Handle special fdopen() case. bleh.
1186 * Don't do this for relative (capability) lookups; we don't
1187 * understand exactly what would happen, and we don't think
1188 * that it ever should.
1190 if ((nd.ni_resflags & NIRES_STRICTREL) == 0 &&
1191 (error == ENODEV || error == ENXIO) &&
1192 td->td_dupfd >= 0) {
1193 error = dupfdopen(td, fdp, td->td_dupfd, flags, error,
1206 * Store the vnode, for any f_type. Typically, the vnode use
1207 * count is decremented by direct call to vn_closefile() for
1208 * files that switched type in the cdevsw fdopen() method.
1213 * If the file wasn't claimed by devfs bind it to the normal
1214 * vnode operations here.
1216 if (fp->f_ops == &badfileops) {
1217 KASSERT(vp->v_type != VFIFO || (flags & O_PATH) != 0,
1218 ("Unexpected fifo fp %p vp %p", fp, vp));
1219 if ((flags & O_PATH) != 0) {
1220 finit(fp, (flags & FMASK) | (fp->f_flag & FKQALLOWED),
1221 DTYPE_VNODE, NULL, &path_fileops);
1225 finit_vnode(fp, flags, NULL, &vnops);
1230 if (flags & O_TRUNC) {
1231 error = fo_truncate(fp, 0, td->td_ucred, td);
1237 * If we haven't already installed the FD (for dupfdopen), do so now.
1240 struct filecaps *fcaps;
1243 if ((nd.ni_resflags & NIRES_STRICTREL) != 0)
1244 fcaps = &nd.ni_filecaps;
1248 error = finstall_refed(td, fp, &indx, flags, fcaps);
1249 /* On success finstall_refed() consumes fcaps. */
1254 NDFREE_IOCTLCAPS(&nd);
1255 falloc_abort(td, fp);
1258 td->td_retval[0] = indx;
1261 KASSERT(indx == -1, ("indx=%d, should be -1", indx));
1262 NDFREE_IOCTLCAPS(&nd);
1263 falloc_abort(td, fp);
1271 #ifndef _SYS_SYSPROTO_H_
1272 struct ocreat_args {
1278 ocreat(struct thread *td, struct ocreat_args *uap)
1281 return (kern_openat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1282 O_WRONLY | O_CREAT | O_TRUNC, uap->mode));
1284 #endif /* COMPAT_43 */
1287 * Create a special file.
1289 #ifndef _SYS_SYSPROTO_H_
1290 struct mknodat_args {
1298 sys_mknodat(struct thread *td, struct mknodat_args *uap)
1301 return (kern_mknodat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode,
1305 #if defined(COMPAT_FREEBSD11)
1307 freebsd11_mknod(struct thread *td,
1308 struct freebsd11_mknod_args *uap)
1311 return (kern_mknodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1312 uap->mode, uap->dev));
1316 freebsd11_mknodat(struct thread *td,
1317 struct freebsd11_mknodat_args *uap)
1320 return (kern_mknodat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode,
1323 #endif /* COMPAT_FREEBSD11 */
1326 kern_mknodat(struct thread *td, int fd, const char *path, enum uio_seg pathseg,
1327 int mode, dev_t dev)
1332 struct nameidata nd;
1333 int error, whiteout = 0;
1335 AUDIT_ARG_MODE(mode);
1337 switch (mode & S_IFMT) {
1340 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
1341 if (error == 0 && dev == VNOVAL)
1345 error = priv_check(td, PRIV_VFS_MKNOD_WHT);
1349 return (kern_mkfifoat(td, fd, path, pathseg, mode));
1360 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
1361 NOCACHE, pathseg, path, fd, &cap_mknodat_rights);
1362 if ((error = namei(&nd)) != 0)
1367 if (vp == nd.ni_dvp)
1375 vattr.va_mode = (mode & ALLPERMS) &
1376 ~td->td_proc->p_pd->pd_cmask;
1377 vattr.va_rdev = dev;
1380 switch (mode & S_IFMT) {
1382 vattr.va_type = VCHR;
1385 vattr.va_type = VBLK;
1391 panic("kern_mknod: invalid mode");
1394 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1397 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1402 if (error == 0 && !whiteout)
1403 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp,
1404 &nd.ni_cnd, &vattr);
1408 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, CREATE);
1410 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp,
1411 &nd.ni_cnd, &vattr);
1414 VOP_VPUT_PAIR(nd.ni_dvp, error == 0 && !whiteout ? &nd.ni_vp : NULL,
1416 vn_finished_write(mp);
1418 if (error == ERELOOKUP)
1424 * Create a named pipe.
1426 #ifndef _SYS_SYSPROTO_H_
1427 struct mkfifo_args {
1433 sys_mkfifo(struct thread *td, struct mkfifo_args *uap)
1436 return (kern_mkfifoat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1440 #ifndef _SYS_SYSPROTO_H_
1441 struct mkfifoat_args {
1448 sys_mkfifoat(struct thread *td, struct mkfifoat_args *uap)
1451 return (kern_mkfifoat(td, uap->fd, uap->path, UIO_USERSPACE,
1456 kern_mkfifoat(struct thread *td, int fd, const char *path,
1457 enum uio_seg pathseg, int mode)
1461 struct nameidata nd;
1464 AUDIT_ARG_MODE(mode);
1468 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
1469 NOCACHE, pathseg, path, fd, &cap_mkfifoat_rights);
1470 if ((error = namei(&nd)) != 0)
1472 if (nd.ni_vp != NULL) {
1474 if (nd.ni_vp == nd.ni_dvp)
1481 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1484 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1489 vattr.va_type = VFIFO;
1490 vattr.va_mode = (mode & ALLPERMS) & ~td->td_proc->p_pd->pd_cmask;
1492 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1497 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
1501 VOP_VPUT_PAIR(nd.ni_dvp, error == 0 ? &nd.ni_vp : NULL, true);
1502 vn_finished_write(mp);
1504 if (error == ERELOOKUP)
1510 * Make a hard file link.
1512 #ifndef _SYS_SYSPROTO_H_
1519 sys_link(struct thread *td, struct link_args *uap)
1522 return (kern_linkat(td, AT_FDCWD, AT_FDCWD, uap->path, uap->link,
1523 UIO_USERSPACE, AT_SYMLINK_FOLLOW));
1526 #ifndef _SYS_SYSPROTO_H_
1527 struct linkat_args {
1536 sys_linkat(struct thread *td, struct linkat_args *uap)
1539 return (kern_linkat(td, uap->fd1, uap->fd2, uap->path1, uap->path2,
1540 UIO_USERSPACE, uap->flag));
1543 int hardlink_check_uid = 0;
1544 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
1545 &hardlink_check_uid, 0,
1546 "Unprivileged processes cannot create hard links to files owned by other "
1548 static int hardlink_check_gid = 0;
1549 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
1550 &hardlink_check_gid, 0,
1551 "Unprivileged processes cannot create hard links to files owned by other "
1555 can_hardlink(struct vnode *vp, struct ucred *cred)
1560 if (!hardlink_check_uid && !hardlink_check_gid)
1563 error = VOP_GETATTR(vp, &va, cred);
1567 if (hardlink_check_uid && cred->cr_uid != va.va_uid) {
1568 error = priv_check_cred(cred, PRIV_VFS_LINK);
1573 if (hardlink_check_gid && !groupmember(va.va_gid, cred)) {
1574 error = priv_check_cred(cred, PRIV_VFS_LINK);
1583 kern_linkat(struct thread *td, int fd1, int fd2, const char *path1,
1584 const char *path2, enum uio_seg segflag, int flag)
1586 struct nameidata nd;
1589 if ((flag & ~(AT_SYMLINK_FOLLOW | AT_RESOLVE_BENEATH |
1590 AT_EMPTY_PATH)) != 0)
1596 NDINIT_ATRIGHTS(&nd, LOOKUP, AUDITVNODE1 | at2cnpflags(flag,
1597 AT_SYMLINK_FOLLOW | AT_RESOLVE_BENEATH | AT_EMPTY_PATH),
1598 segflag, path1, fd1, &cap_linkat_source_rights);
1599 if ((error = namei(&nd)) != 0)
1602 if ((nd.ni_resflags & NIRES_EMPTYPATH) != 0) {
1603 error = priv_check(td, PRIV_VFS_FHOPEN);
1609 error = kern_linkat_vp(td, nd.ni_vp, fd2, path2, segflag);
1610 } while (error == EAGAIN || error == ERELOOKUP);
1615 kern_linkat_vp(struct thread *td, struct vnode *vp, int fd, const char *path,
1616 enum uio_seg segflag)
1618 struct nameidata nd;
1622 if (vp->v_type == VDIR) {
1624 return (EPERM); /* POSIX */
1626 NDINIT_ATRIGHTS(&nd, CREATE,
1627 LOCKPARENT | SAVENAME | AUDITVNODE2 | NOCACHE, segflag, path, fd,
1628 &cap_linkat_target_rights);
1629 if ((error = namei(&nd)) == 0) {
1630 if (nd.ni_vp != NULL) {
1632 if (nd.ni_dvp == nd.ni_vp)
1639 } else if (nd.ni_dvp->v_mount != vp->v_mount) {
1641 * Cross-device link. No need to recheck
1642 * vp->v_type, since it cannot change, except
1649 } else if ((error = vn_lock(vp, LK_EXCLUSIVE)) == 0) {
1650 error = can_hardlink(vp, td->td_ucred);
1653 error = mac_vnode_check_link(td->td_ucred,
1654 nd.ni_dvp, vp, &nd.ni_cnd);
1662 error = vn_start_write(vp, &mp, V_NOWAIT);
1667 error = vn_start_write(NULL, &mp,
1673 error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
1674 VOP_VPUT_PAIR(nd.ni_dvp, &vp, true);
1675 vn_finished_write(mp);
1691 * Make a symbolic link.
1693 #ifndef _SYS_SYSPROTO_H_
1694 struct symlink_args {
1700 sys_symlink(struct thread *td, struct symlink_args *uap)
1703 return (kern_symlinkat(td, uap->path, AT_FDCWD, uap->link,
1707 #ifndef _SYS_SYSPROTO_H_
1708 struct symlinkat_args {
1715 sys_symlinkat(struct thread *td, struct symlinkat_args *uap)
1718 return (kern_symlinkat(td, uap->path1, uap->fd, uap->path2,
1723 kern_symlinkat(struct thread *td, const char *path1, int fd, const char *path2,
1724 enum uio_seg segflg)
1728 const char *syspath;
1730 struct nameidata nd;
1733 if (segflg == UIO_SYSSPACE) {
1736 tmppath = uma_zalloc(namei_zone, M_WAITOK);
1737 if ((error = copyinstr(path1, tmppath, MAXPATHLEN, NULL)) != 0)
1741 AUDIT_ARG_TEXT(syspath);
1745 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
1746 NOCACHE, segflg, path2, fd, &cap_symlinkat_rights);
1747 if ((error = namei(&nd)) != 0)
1751 if (nd.ni_vp == nd.ni_dvp)
1760 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1763 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1768 vattr.va_mode = ACCESSPERMS &~ td->td_proc->p_pd->pd_cmask;
1770 vattr.va_type = VLNK;
1771 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1776 error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr, syspath);
1780 VOP_VPUT_PAIR(nd.ni_dvp, error == 0 ? &nd.ni_vp : NULL, true);
1781 vn_finished_write(mp);
1783 if (error == ERELOOKUP)
1786 if (segflg != UIO_SYSSPACE)
1787 uma_zfree(namei_zone, tmppath);
1792 * Delete a whiteout from the filesystem.
1794 #ifndef _SYS_SYSPROTO_H_
1795 struct undelete_args {
1800 sys_undelete(struct thread *td, struct undelete_args *uap)
1803 struct nameidata nd;
1809 NDINIT(&nd, DELETE, LOCKPARENT | DOWHITEOUT | AUDITVNODE1,
1810 UIO_USERSPACE, uap->path);
1815 if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & ISWHITEOUT)) {
1817 if (nd.ni_vp == nd.ni_dvp)
1825 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1828 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1832 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, DELETE);
1835 vn_finished_write(mp);
1836 if (error == ERELOOKUP)
1842 * Delete a name from the filesystem.
1844 #ifndef _SYS_SYSPROTO_H_
1845 struct unlink_args {
1850 sys_unlink(struct thread *td, struct unlink_args *uap)
1853 return (kern_funlinkat(td, AT_FDCWD, uap->path, FD_NONE, UIO_USERSPACE,
1858 kern_funlinkat_ex(struct thread *td, int dfd, const char *path, int fd,
1859 int flag, enum uio_seg pathseg, ino_t oldinum)
1862 if ((flag & ~(AT_REMOVEDIR | AT_RESOLVE_BENEATH)) != 0)
1865 if ((flag & AT_REMOVEDIR) != 0)
1866 return (kern_frmdirat(td, dfd, path, fd, UIO_USERSPACE, 0));
1868 return (kern_funlinkat(td, dfd, path, fd, UIO_USERSPACE, 0, 0));
1871 #ifndef _SYS_SYSPROTO_H_
1872 struct unlinkat_args {
1879 sys_unlinkat(struct thread *td, struct unlinkat_args *uap)
1882 return (kern_funlinkat_ex(td, uap->fd, uap->path, FD_NONE, uap->flag,
1886 #ifndef _SYS_SYSPROTO_H_
1887 struct funlinkat_args {
1895 sys_funlinkat(struct thread *td, struct funlinkat_args *uap)
1898 return (kern_funlinkat_ex(td, uap->dfd, uap->path, uap->fd, uap->flag,
1903 kern_funlinkat(struct thread *td, int dfd, const char *path, int fd,
1904 enum uio_seg pathseg, int flag, ino_t oldinum)
1909 struct nameidata nd;
1914 if (fd != FD_NONE) {
1915 error = getvnode_path(td, fd, &cap_no_rights, &fp);
1923 NDINIT_ATRIGHTS(&nd, DELETE, LOCKPARENT | LOCKLEAF | AUDITVNODE1 |
1924 at2cnpflags(flag, AT_RESOLVE_BENEATH),
1925 pathseg, path, dfd, &cap_unlinkat_rights);
1926 if ((error = namei(&nd)) != 0) {
1927 if (error == EINVAL)
1932 if (vp->v_type == VDIR && oldinum == 0) {
1933 error = EPERM; /* POSIX */
1934 } else if (oldinum != 0 &&
1935 ((error = VOP_STAT(vp, &sb, td->td_ucred, NOCRED)) == 0) &&
1936 sb.st_ino != oldinum) {
1937 error = EIDRM; /* Identifier removed */
1938 } else if (fp != NULL && fp->f_vnode != vp) {
1939 if (VN_IS_DOOMED(fp->f_vnode))
1945 * The root of a mounted filesystem cannot be deleted.
1947 * XXX: can this only be a VDIR case?
1949 if (vp->v_vflag & VV_ROOT)
1953 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1956 if (vp == nd.ni_dvp)
1960 if ((error = vn_start_write(NULL, &mp,
1961 V_XSLEEP | PCATCH)) != 0) {
1967 error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp,
1972 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_UNLINK);
1973 error = VOP_REMOVE(nd.ni_dvp, vp, &nd.ni_cnd);
1977 vn_finished_write(mp);
1981 if (vp == nd.ni_dvp)
1985 if (error == ERELOOKUP)
1994 * Reposition read/write file offset.
1996 #ifndef _SYS_SYSPROTO_H_
2005 sys_lseek(struct thread *td, struct lseek_args *uap)
2008 return (kern_lseek(td, uap->fd, uap->offset, uap->whence));
2012 kern_lseek(struct thread *td, int fd, off_t offset, int whence)
2018 error = fget(td, fd, &cap_seek_rights, &fp);
2021 error = (fp->f_ops->fo_flags & DFLAG_SEEKABLE) != 0 ?
2022 fo_seek(fp, offset, whence, td) : ESPIPE;
2027 #if defined(COMPAT_43)
2029 * Reposition read/write file offset.
2031 #ifndef _SYS_SYSPROTO_H_
2032 struct olseek_args {
2039 olseek(struct thread *td, struct olseek_args *uap)
2042 return (kern_lseek(td, uap->fd, uap->offset, uap->whence));
2044 #endif /* COMPAT_43 */
2046 #if defined(COMPAT_FREEBSD6)
2047 /* Version with the 'pad' argument */
2049 freebsd6_lseek(struct thread *td, struct freebsd6_lseek_args *uap)
2052 return (kern_lseek(td, uap->fd, uap->offset, uap->whence));
2057 * Check access permissions using passed credentials.
2060 vn_access(struct vnode *vp, int user_flags, struct ucred *cred,
2066 /* Flags == 0 means only check for existence. */
2067 if (user_flags == 0)
2071 if (user_flags & R_OK)
2073 if (user_flags & W_OK)
2075 if (user_flags & X_OK)
2078 error = mac_vnode_check_access(cred, vp, accmode);
2082 if ((accmode & VWRITE) == 0 || (error = vn_writechk(vp)) == 0)
2083 error = VOP_ACCESS(vp, accmode, cred, td);
2088 * Check access permissions using "real" credentials.
2090 #ifndef _SYS_SYSPROTO_H_
2091 struct access_args {
2097 sys_access(struct thread *td, struct access_args *uap)
2100 return (kern_accessat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2104 #ifndef _SYS_SYSPROTO_H_
2105 struct faccessat_args {
2113 sys_faccessat(struct thread *td, struct faccessat_args *uap)
2116 return (kern_accessat(td, uap->fd, uap->path, UIO_USERSPACE, uap->flag,
2121 kern_accessat(struct thread *td, int fd, const char *path,
2122 enum uio_seg pathseg, int flag, int amode)
2124 struct ucred *cred, *usecred;
2126 struct nameidata nd;
2129 if ((flag & ~(AT_EACCESS | AT_RESOLVE_BENEATH | AT_EMPTY_PATH)) != 0)
2131 if (amode != F_OK && (amode & ~(R_OK | W_OK | X_OK)) != 0)
2135 * Create and modify a temporary credential instead of one that
2136 * is potentially shared (if we need one).
2138 cred = td->td_ucred;
2139 if ((flag & AT_EACCESS) == 0 &&
2140 ((cred->cr_uid != cred->cr_ruid ||
2141 cred->cr_rgid != cred->cr_groups[0]))) {
2142 usecred = crdup(cred);
2143 usecred->cr_uid = cred->cr_ruid;
2144 usecred->cr_groups[0] = cred->cr_rgid;
2145 td->td_ucred = usecred;
2148 AUDIT_ARG_VALUE(amode);
2149 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF |
2150 AUDITVNODE1 | at2cnpflags(flag, AT_RESOLVE_BENEATH |
2151 AT_EMPTY_PATH), pathseg, path, fd, &cap_fstat_rights);
2152 if ((error = namei(&nd)) != 0)
2156 error = vn_access(vp, amode, usecred, td);
2157 NDFREE_NOTHING(&nd);
2160 if (usecred != cred) {
2161 td->td_ucred = cred;
2168 * Check access permissions using "effective" credentials.
2170 #ifndef _SYS_SYSPROTO_H_
2171 struct eaccess_args {
2177 sys_eaccess(struct thread *td, struct eaccess_args *uap)
2180 return (kern_accessat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2181 AT_EACCESS, uap->amode));
2184 #if defined(COMPAT_43)
2186 * Get file status; this version follows links.
2188 #ifndef _SYS_SYSPROTO_H_
2195 ostat(struct thread *td, struct ostat_args *uap)
2201 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
2206 return (copyout(&osb, uap->ub, sizeof (osb)));
2210 * Get file status; this version does not follow links.
2212 #ifndef _SYS_SYSPROTO_H_
2213 struct olstat_args {
2219 olstat(struct thread *td, struct olstat_args *uap)
2225 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2226 UIO_USERSPACE, &sb, NULL);
2230 return (copyout(&osb, uap->ub, sizeof (osb)));
2234 * Convert from an old to a new stat structure.
2235 * XXX: many values are blindly truncated.
2238 cvtstat(struct stat *st, struct ostat *ost)
2241 bzero(ost, sizeof(*ost));
2242 ost->st_dev = st->st_dev;
2243 ost->st_ino = st->st_ino;
2244 ost->st_mode = st->st_mode;
2245 ost->st_nlink = st->st_nlink;
2246 ost->st_uid = st->st_uid;
2247 ost->st_gid = st->st_gid;
2248 ost->st_rdev = st->st_rdev;
2249 ost->st_size = MIN(st->st_size, INT32_MAX);
2250 ost->st_atim = st->st_atim;
2251 ost->st_mtim = st->st_mtim;
2252 ost->st_ctim = st->st_ctim;
2253 ost->st_blksize = st->st_blksize;
2254 ost->st_blocks = st->st_blocks;
2255 ost->st_flags = st->st_flags;
2256 ost->st_gen = st->st_gen;
2258 #endif /* COMPAT_43 */
2260 #if defined(COMPAT_43) || defined(COMPAT_FREEBSD11)
2261 int ino64_trunc_error;
2262 SYSCTL_INT(_vfs, OID_AUTO, ino64_trunc_error, CTLFLAG_RW,
2263 &ino64_trunc_error, 0,
2264 "Error on truncation of device, file or inode number, or link count");
2267 freebsd11_cvtstat(struct stat *st, struct freebsd11_stat *ost)
2270 ost->st_dev = st->st_dev;
2271 if (ost->st_dev != st->st_dev) {
2272 switch (ino64_trunc_error) {
2275 * Since dev_t is almost raw, don't clamp to the
2276 * maximum for case 2, but ignore the error.
2283 ost->st_ino = st->st_ino;
2284 if (ost->st_ino != st->st_ino) {
2285 switch (ino64_trunc_error) {
2292 ost->st_ino = UINT32_MAX;
2296 ost->st_mode = st->st_mode;
2297 ost->st_nlink = st->st_nlink;
2298 if (ost->st_nlink != st->st_nlink) {
2299 switch (ino64_trunc_error) {
2306 ost->st_nlink = UINT16_MAX;
2310 ost->st_uid = st->st_uid;
2311 ost->st_gid = st->st_gid;
2312 ost->st_rdev = st->st_rdev;
2313 if (ost->st_rdev != st->st_rdev) {
2314 switch (ino64_trunc_error) {
2321 ost->st_atim = st->st_atim;
2322 ost->st_mtim = st->st_mtim;
2323 ost->st_ctim = st->st_ctim;
2324 ost->st_size = st->st_size;
2325 ost->st_blocks = st->st_blocks;
2326 ost->st_blksize = st->st_blksize;
2327 ost->st_flags = st->st_flags;
2328 ost->st_gen = st->st_gen;
2330 ost->st_birthtim = st->st_birthtim;
2331 bzero((char *)&ost->st_birthtim + sizeof(ost->st_birthtim),
2332 sizeof(*ost) - offsetof(struct freebsd11_stat,
2333 st_birthtim) - sizeof(ost->st_birthtim));
2338 freebsd11_stat(struct thread *td, struct freebsd11_stat_args* uap)
2341 struct freebsd11_stat osb;
2344 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
2348 error = freebsd11_cvtstat(&sb, &osb);
2350 error = copyout(&osb, uap->ub, sizeof(osb));
2355 freebsd11_lstat(struct thread *td, struct freebsd11_lstat_args* uap)
2358 struct freebsd11_stat osb;
2361 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2362 UIO_USERSPACE, &sb, NULL);
2365 error = freebsd11_cvtstat(&sb, &osb);
2367 error = copyout(&osb, uap->ub, sizeof(osb));
2372 freebsd11_fhstat(struct thread *td, struct freebsd11_fhstat_args* uap)
2376 struct freebsd11_stat osb;
2379 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
2382 error = kern_fhstat(td, fh, &sb);
2385 error = freebsd11_cvtstat(&sb, &osb);
2387 error = copyout(&osb, uap->sb, sizeof(osb));
2392 freebsd11_fstatat(struct thread *td, struct freebsd11_fstatat_args* uap)
2395 struct freebsd11_stat osb;
2398 error = kern_statat(td, uap->flag, uap->fd, uap->path,
2399 UIO_USERSPACE, &sb, NULL);
2402 error = freebsd11_cvtstat(&sb, &osb);
2404 error = copyout(&osb, uap->buf, sizeof(osb));
2407 #endif /* COMPAT_FREEBSD11 */
2412 #ifndef _SYS_SYSPROTO_H_
2413 struct fstatat_args {
2421 sys_fstatat(struct thread *td, struct fstatat_args *uap)
2426 error = kern_statat(td, uap->flag, uap->fd, uap->path,
2427 UIO_USERSPACE, &sb, NULL);
2429 error = copyout(&sb, uap->buf, sizeof (sb));
2434 kern_statat(struct thread *td, int flag, int fd, const char *path,
2435 enum uio_seg pathseg, struct stat *sbp,
2436 void (*hook)(struct vnode *vp, struct stat *sbp))
2438 struct nameidata nd;
2441 if ((flag & ~(AT_SYMLINK_NOFOLLOW | AT_RESOLVE_BENEATH |
2442 AT_EMPTY_PATH)) != 0)
2445 NDINIT_ATRIGHTS(&nd, LOOKUP, at2cnpflags(flag, AT_RESOLVE_BENEATH |
2446 AT_SYMLINK_NOFOLLOW | AT_EMPTY_PATH) | LOCKSHARED | LOCKLEAF |
2447 AUDITVNODE1, pathseg, path, fd, &cap_fstat_rights);
2449 if ((error = namei(&nd)) != 0) {
2450 if (error == ENOTDIR &&
2451 (nd.ni_resflags & NIRES_EMPTYPATH) != 0)
2452 error = kern_fstat(td, fd, sbp);
2455 error = VOP_STAT(nd.ni_vp, sbp, td->td_ucred, NOCRED);
2457 if (__predict_false(hook != NULL))
2458 hook(nd.ni_vp, sbp);
2460 NDFREE_NOTHING(&nd);
2462 #ifdef __STAT_TIME_T_EXT
2463 sbp->st_atim_ext = 0;
2464 sbp->st_mtim_ext = 0;
2465 sbp->st_ctim_ext = 0;
2466 sbp->st_btim_ext = 0;
2469 if (KTRPOINT(td, KTR_STRUCT))
2470 ktrstat_error(sbp, error);
2475 #if defined(COMPAT_FREEBSD11)
2477 * Implementation of the NetBSD [l]stat() functions.
2480 freebsd11_cvtnstat(struct stat *sb, struct nstat *nsb)
2482 struct freebsd11_stat sb11;
2485 error = freebsd11_cvtstat(sb, &sb11);
2489 bzero(nsb, sizeof(*nsb));
2490 CP(sb11, *nsb, st_dev);
2491 CP(sb11, *nsb, st_ino);
2492 CP(sb11, *nsb, st_mode);
2493 CP(sb11, *nsb, st_nlink);
2494 CP(sb11, *nsb, st_uid);
2495 CP(sb11, *nsb, st_gid);
2496 CP(sb11, *nsb, st_rdev);
2497 CP(sb11, *nsb, st_atim);
2498 CP(sb11, *nsb, st_mtim);
2499 CP(sb11, *nsb, st_ctim);
2500 CP(sb11, *nsb, st_size);
2501 CP(sb11, *nsb, st_blocks);
2502 CP(sb11, *nsb, st_blksize);
2503 CP(sb11, *nsb, st_flags);
2504 CP(sb11, *nsb, st_gen);
2505 CP(sb11, *nsb, st_birthtim);
2509 #ifndef _SYS_SYSPROTO_H_
2510 struct freebsd11_nstat_args {
2516 freebsd11_nstat(struct thread *td, struct freebsd11_nstat_args *uap)
2522 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
2526 error = freebsd11_cvtnstat(&sb, &nsb);
2528 error = copyout(&nsb, uap->ub, sizeof (nsb));
2533 * NetBSD lstat. Get file status; this version does not follow links.
2535 #ifndef _SYS_SYSPROTO_H_
2536 struct freebsd11_nlstat_args {
2542 freebsd11_nlstat(struct thread *td, struct freebsd11_nlstat_args *uap)
2548 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2549 UIO_USERSPACE, &sb, NULL);
2552 error = freebsd11_cvtnstat(&sb, &nsb);
2554 error = copyout(&nsb, uap->ub, sizeof (nsb));
2557 #endif /* COMPAT_FREEBSD11 */
2560 * Get configurable pathname variables.
2562 #ifndef _SYS_SYSPROTO_H_
2563 struct pathconf_args {
2569 sys_pathconf(struct thread *td, struct pathconf_args *uap)
2574 error = kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name, FOLLOW,
2577 td->td_retval[0] = value;
2581 #ifndef _SYS_SYSPROTO_H_
2582 struct lpathconf_args {
2588 sys_lpathconf(struct thread *td, struct lpathconf_args *uap)
2593 error = kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name,
2596 td->td_retval[0] = value;
2601 kern_pathconf(struct thread *td, const char *path, enum uio_seg pathseg,
2602 int name, u_long flags, long *valuep)
2604 struct nameidata nd;
2607 NDINIT(&nd, LOOKUP, LOCKSHARED | LOCKLEAF | AUDITVNODE1 | flags,
2609 if ((error = namei(&nd)) != 0)
2611 NDFREE_NOTHING(&nd);
2613 error = VOP_PATHCONF(nd.ni_vp, name, valuep);
2619 * Return target name of a symbolic link.
2621 #ifndef _SYS_SYSPROTO_H_
2622 struct readlink_args {
2629 sys_readlink(struct thread *td, struct readlink_args *uap)
2632 return (kern_readlinkat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2633 uap->buf, UIO_USERSPACE, uap->count));
2635 #ifndef _SYS_SYSPROTO_H_
2636 struct readlinkat_args {
2644 sys_readlinkat(struct thread *td, struct readlinkat_args *uap)
2647 return (kern_readlinkat(td, uap->fd, uap->path, UIO_USERSPACE,
2648 uap->buf, UIO_USERSPACE, uap->bufsize));
2652 kern_readlinkat(struct thread *td, int fd, const char *path,
2653 enum uio_seg pathseg, char *buf, enum uio_seg bufseg, size_t count)
2656 struct nameidata nd;
2659 if (count > IOSIZE_MAX)
2662 NDINIT_AT(&nd, LOOKUP, NOFOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1 |
2663 EMPTYPATH, pathseg, path, fd);
2665 if ((error = namei(&nd)) != 0)
2667 NDFREE_NOTHING(&nd);
2670 error = kern_readlink_vp(vp, buf, bufseg, count, td);
2677 * Helper function to readlink from a vnode
2680 kern_readlink_vp(struct vnode *vp, char *buf, enum uio_seg bufseg, size_t count,
2687 ASSERT_VOP_LOCKED(vp, "kern_readlink_vp(): vp not locked");
2689 error = mac_vnode_check_readlink(td->td_ucred, vp);
2693 if (vp->v_type != VLNK && (vp->v_vflag & VV_READLINK) == 0)
2696 aiov.iov_base = buf;
2697 aiov.iov_len = count;
2698 auio.uio_iov = &aiov;
2699 auio.uio_iovcnt = 1;
2700 auio.uio_offset = 0;
2701 auio.uio_rw = UIO_READ;
2702 auio.uio_segflg = bufseg;
2704 auio.uio_resid = count;
2705 error = VOP_READLINK(vp, &auio, td->td_ucred);
2706 td->td_retval[0] = count - auio.uio_resid;
2711 * Common implementation code for chflags() and fchflags().
2714 setfflags(struct thread *td, struct vnode *vp, u_long flags)
2720 /* We can't support the value matching VNOVAL. */
2721 if (flags == VNOVAL)
2722 return (EOPNOTSUPP);
2725 * Prevent non-root users from setting flags on devices. When
2726 * a device is reused, users can retain ownership of the device
2727 * if they are allowed to set flags and programs assume that
2728 * chown can't fail when done as root.
2730 if (vp->v_type == VCHR || vp->v_type == VBLK) {
2731 error = priv_check(td, PRIV_VFS_CHFLAGS_DEV);
2736 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2739 vattr.va_flags = flags;
2740 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2742 error = mac_vnode_check_setflags(td->td_ucred, vp, vattr.va_flags);
2745 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
2747 vn_finished_write(mp);
2752 * Change flags of a file given a path name.
2754 #ifndef _SYS_SYSPROTO_H_
2755 struct chflags_args {
2761 sys_chflags(struct thread *td, struct chflags_args *uap)
2764 return (kern_chflagsat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2768 #ifndef _SYS_SYSPROTO_H_
2769 struct chflagsat_args {
2777 sys_chflagsat(struct thread *td, struct chflagsat_args *uap)
2780 return (kern_chflagsat(td, uap->fd, uap->path, UIO_USERSPACE,
2781 uap->flags, uap->atflag));
2785 * Same as chflags() but doesn't follow symlinks.
2787 #ifndef _SYS_SYSPROTO_H_
2788 struct lchflags_args {
2794 sys_lchflags(struct thread *td, struct lchflags_args *uap)
2797 return (kern_chflagsat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2798 uap->flags, AT_SYMLINK_NOFOLLOW));
2802 kern_chflagsat(struct thread *td, int fd, const char *path,
2803 enum uio_seg pathseg, u_long flags, int atflag)
2805 struct nameidata nd;
2808 if ((atflag & ~(AT_SYMLINK_NOFOLLOW | AT_RESOLVE_BENEATH |
2809 AT_EMPTY_PATH)) != 0)
2812 AUDIT_ARG_FFLAGS(flags);
2813 NDINIT_ATRIGHTS(&nd, LOOKUP, at2cnpflags(atflag, AT_SYMLINK_NOFOLLOW |
2814 AT_RESOLVE_BENEATH | AT_EMPTY_PATH) | AUDITVNODE1, pathseg, path,
2815 fd, &cap_fchflags_rights);
2816 if ((error = namei(&nd)) != 0)
2818 NDFREE_NOTHING(&nd);
2819 error = setfflags(td, nd.ni_vp, flags);
2825 * Change flags of a file given a file descriptor.
2827 #ifndef _SYS_SYSPROTO_H_
2828 struct fchflags_args {
2834 sys_fchflags(struct thread *td, struct fchflags_args *uap)
2839 AUDIT_ARG_FD(uap->fd);
2840 AUDIT_ARG_FFLAGS(uap->flags);
2841 error = getvnode(td, uap->fd, &cap_fchflags_rights,
2846 if (AUDITING_TD(td)) {
2847 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
2848 AUDIT_ARG_VNODE1(fp->f_vnode);
2849 VOP_UNLOCK(fp->f_vnode);
2852 error = setfflags(td, fp->f_vnode, uap->flags);
2858 * Common implementation code for chmod(), lchmod() and fchmod().
2861 setfmode(struct thread *td, struct ucred *cred, struct vnode *vp, int mode)
2867 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2869 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2871 vattr.va_mode = mode & ALLPERMS;
2873 error = mac_vnode_check_setmode(cred, vp, vattr.va_mode);
2876 error = VOP_SETATTR(vp, &vattr, cred);
2878 vn_finished_write(mp);
2883 * Change mode of a file given path name.
2885 #ifndef _SYS_SYSPROTO_H_
2892 sys_chmod(struct thread *td, struct chmod_args *uap)
2895 return (kern_fchmodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2899 #ifndef _SYS_SYSPROTO_H_
2900 struct fchmodat_args {
2908 sys_fchmodat(struct thread *td, struct fchmodat_args *uap)
2911 return (kern_fchmodat(td, uap->fd, uap->path, UIO_USERSPACE,
2912 uap->mode, uap->flag));
2916 * Change mode of a file given path name (don't follow links.)
2918 #ifndef _SYS_SYSPROTO_H_
2919 struct lchmod_args {
2925 sys_lchmod(struct thread *td, struct lchmod_args *uap)
2928 return (kern_fchmodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2929 uap->mode, AT_SYMLINK_NOFOLLOW));
2933 kern_fchmodat(struct thread *td, int fd, const char *path,
2934 enum uio_seg pathseg, mode_t mode, int flag)
2936 struct nameidata nd;
2939 if ((flag & ~(AT_SYMLINK_NOFOLLOW | AT_RESOLVE_BENEATH |
2940 AT_EMPTY_PATH)) != 0)
2943 AUDIT_ARG_MODE(mode);
2944 NDINIT_ATRIGHTS(&nd, LOOKUP, at2cnpflags(flag, AT_SYMLINK_NOFOLLOW |
2945 AT_RESOLVE_BENEATH | AT_EMPTY_PATH) | AUDITVNODE1, pathseg, path,
2946 fd, &cap_fchmod_rights);
2947 if ((error = namei(&nd)) != 0)
2949 NDFREE_NOTHING(&nd);
2950 error = setfmode(td, td->td_ucred, nd.ni_vp, mode);
2956 * Change mode of a file given a file descriptor.
2958 #ifndef _SYS_SYSPROTO_H_
2959 struct fchmod_args {
2965 sys_fchmod(struct thread *td, struct fchmod_args *uap)
2970 AUDIT_ARG_FD(uap->fd);
2971 AUDIT_ARG_MODE(uap->mode);
2973 error = fget(td, uap->fd, &cap_fchmod_rights, &fp);
2976 error = fo_chmod(fp, uap->mode, td->td_ucred, td);
2982 * Common implementation for chown(), lchown(), and fchown()
2985 setfown(struct thread *td, struct ucred *cred, struct vnode *vp, uid_t uid,
2992 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2994 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2999 error = mac_vnode_check_setowner(cred, vp, vattr.va_uid,
3003 error = VOP_SETATTR(vp, &vattr, cred);
3005 vn_finished_write(mp);
3010 * Set ownership given a path name.
3012 #ifndef _SYS_SYSPROTO_H_
3020 sys_chown(struct thread *td, struct chown_args *uap)
3023 return (kern_fchownat(td, AT_FDCWD, uap->path, UIO_USERSPACE, uap->uid,
3027 #ifndef _SYS_SYSPROTO_H_
3028 struct fchownat_args {
3037 sys_fchownat(struct thread *td, struct fchownat_args *uap)
3040 return (kern_fchownat(td, uap->fd, uap->path, UIO_USERSPACE, uap->uid,
3041 uap->gid, uap->flag));
3045 kern_fchownat(struct thread *td, int fd, const char *path,
3046 enum uio_seg pathseg, int uid, int gid, int flag)
3048 struct nameidata nd;
3051 if ((flag & ~(AT_SYMLINK_NOFOLLOW | AT_RESOLVE_BENEATH |
3052 AT_EMPTY_PATH)) != 0)
3055 AUDIT_ARG_OWNER(uid, gid);
3056 NDINIT_ATRIGHTS(&nd, LOOKUP, at2cnpflags(flag, AT_SYMLINK_NOFOLLOW |
3057 AT_RESOLVE_BENEATH | AT_EMPTY_PATH) | AUDITVNODE1, pathseg, path,
3058 fd, &cap_fchown_rights);
3060 if ((error = namei(&nd)) != 0)
3062 NDFREE_NOTHING(&nd);
3063 error = setfown(td, td->td_ucred, nd.ni_vp, uid, gid);
3069 * Set ownership given a path name, do not cross symlinks.
3071 #ifndef _SYS_SYSPROTO_H_
3072 struct lchown_args {
3079 sys_lchown(struct thread *td, struct lchown_args *uap)
3082 return (kern_fchownat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
3083 uap->uid, uap->gid, AT_SYMLINK_NOFOLLOW));
3087 * Set ownership given a file descriptor.
3089 #ifndef _SYS_SYSPROTO_H_
3090 struct fchown_args {
3097 sys_fchown(struct thread *td, struct fchown_args *uap)
3102 AUDIT_ARG_FD(uap->fd);
3103 AUDIT_ARG_OWNER(uap->uid, uap->gid);
3104 error = fget(td, uap->fd, &cap_fchown_rights, &fp);
3107 error = fo_chown(fp, uap->uid, uap->gid, td->td_ucred, td);
3113 * Common implementation code for utimes(), lutimes(), and futimes().
3116 getutimes(const struct timeval *usrtvp, enum uio_seg tvpseg,
3117 struct timespec *tsp)
3119 struct timeval tv[2];
3120 const struct timeval *tvp;
3123 if (usrtvp == NULL) {
3124 vfs_timestamp(&tsp[0]);
3127 if (tvpseg == UIO_SYSSPACE) {
3130 if ((error = copyin(usrtvp, tv, sizeof(tv))) != 0)
3135 if (tvp[0].tv_usec < 0 || tvp[0].tv_usec >= 1000000 ||
3136 tvp[1].tv_usec < 0 || tvp[1].tv_usec >= 1000000)
3138 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
3139 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
3145 * Common implementation code for futimens(), utimensat().
3147 #define UTIMENS_NULL 0x1
3148 #define UTIMENS_EXIT 0x2
3150 getutimens(const struct timespec *usrtsp, enum uio_seg tspseg,
3151 struct timespec *tsp, int *retflags)
3153 struct timespec tsnow;
3156 vfs_timestamp(&tsnow);
3158 if (usrtsp == NULL) {
3161 *retflags |= UTIMENS_NULL;
3164 if (tspseg == UIO_SYSSPACE) {
3167 } else if ((error = copyin(usrtsp, tsp, sizeof(*tsp) * 2)) != 0)
3169 if (tsp[0].tv_nsec == UTIME_OMIT && tsp[1].tv_nsec == UTIME_OMIT)
3170 *retflags |= UTIMENS_EXIT;
3171 if (tsp[0].tv_nsec == UTIME_NOW && tsp[1].tv_nsec == UTIME_NOW)
3172 *retflags |= UTIMENS_NULL;
3173 if (tsp[0].tv_nsec == UTIME_OMIT)
3174 tsp[0].tv_sec = VNOVAL;
3175 else if (tsp[0].tv_nsec == UTIME_NOW)
3177 else if (tsp[0].tv_nsec < 0 || tsp[0].tv_nsec >= 1000000000L)
3179 if (tsp[1].tv_nsec == UTIME_OMIT)
3180 tsp[1].tv_sec = VNOVAL;
3181 else if (tsp[1].tv_nsec == UTIME_NOW)
3183 else if (tsp[1].tv_nsec < 0 || tsp[1].tv_nsec >= 1000000000L)
3190 * Common implementation code for utimes(), lutimes(), futimes(), futimens(),
3194 setutimes(struct thread *td, struct vnode *vp, const struct timespec *ts,
3195 int numtimes, int nullflag)
3202 setbirthtime = false;
3203 vattr.va_birthtime.tv_sec = VNOVAL;
3204 vattr.va_birthtime.tv_nsec = 0;
3206 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
3208 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3209 if (numtimes < 3 && VOP_GETATTR(vp, &vattr, td->td_ucred) == 0 &&
3210 timespeccmp(&ts[1], &vattr.va_birthtime, < ))
3211 setbirthtime = true;
3213 vattr.va_atime = ts[0];
3214 vattr.va_mtime = ts[1];
3216 vattr.va_birthtime = ts[1];
3218 vattr.va_birthtime = ts[2];
3220 vattr.va_vaflags |= VA_UTIMES_NULL;
3222 error = mac_vnode_check_setutimes(td->td_ucred, vp, vattr.va_atime,
3226 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3228 vn_finished_write(mp);
3233 * Set the access and modification times of a file.
3235 #ifndef _SYS_SYSPROTO_H_
3236 struct utimes_args {
3238 struct timeval *tptr;
3242 sys_utimes(struct thread *td, struct utimes_args *uap)
3245 return (kern_utimesat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
3246 uap->tptr, UIO_USERSPACE));
3249 #ifndef _SYS_SYSPROTO_H_
3250 struct futimesat_args {
3253 const struct timeval * times;
3257 sys_futimesat(struct thread *td, struct futimesat_args *uap)
3260 return (kern_utimesat(td, uap->fd, uap->path, UIO_USERSPACE,
3261 uap->times, UIO_USERSPACE));
3265 kern_utimesat(struct thread *td, int fd, const char *path,
3266 enum uio_seg pathseg, const struct timeval *tptr, enum uio_seg tptrseg)
3268 struct nameidata nd;
3269 struct timespec ts[2];
3272 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3274 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, fd,
3275 &cap_futimes_rights);
3277 if ((error = namei(&nd)) != 0)
3279 NDFREE_NOTHING(&nd);
3280 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3286 * Set the access and modification times of a file.
3288 #ifndef _SYS_SYSPROTO_H_
3289 struct lutimes_args {
3291 struct timeval *tptr;
3295 sys_lutimes(struct thread *td, struct lutimes_args *uap)
3298 return (kern_lutimes(td, uap->path, UIO_USERSPACE, uap->tptr,
3303 kern_lutimes(struct thread *td, const char *path, enum uio_seg pathseg,
3304 const struct timeval *tptr, enum uio_seg tptrseg)
3306 struct timespec ts[2];
3307 struct nameidata nd;
3310 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3312 NDINIT(&nd, LOOKUP, NOFOLLOW | AUDITVNODE1, pathseg, path);
3313 if ((error = namei(&nd)) != 0)
3315 NDFREE_NOTHING(&nd);
3316 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3322 * Set the access and modification times of a file.
3324 #ifndef _SYS_SYSPROTO_H_
3325 struct futimes_args {
3327 struct timeval *tptr;
3331 sys_futimes(struct thread *td, struct futimes_args *uap)
3334 return (kern_futimes(td, uap->fd, uap->tptr, UIO_USERSPACE));
3338 kern_futimes(struct thread *td, int fd, const struct timeval *tptr,
3339 enum uio_seg tptrseg)
3341 struct timespec ts[2];
3346 error = getutimes(tptr, tptrseg, ts);
3349 error = getvnode(td, fd, &cap_futimes_rights, &fp);
3353 if (AUDITING_TD(td)) {
3354 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
3355 AUDIT_ARG_VNODE1(fp->f_vnode);
3356 VOP_UNLOCK(fp->f_vnode);
3359 error = setutimes(td, fp->f_vnode, ts, 2, tptr == NULL);
3365 sys_futimens(struct thread *td, struct futimens_args *uap)
3368 return (kern_futimens(td, uap->fd, uap->times, UIO_USERSPACE));
3372 kern_futimens(struct thread *td, int fd, const struct timespec *tptr,
3373 enum uio_seg tptrseg)
3375 struct timespec ts[2];
3380 error = getutimens(tptr, tptrseg, ts, &flags);
3383 if (flags & UTIMENS_EXIT)
3385 error = getvnode(td, fd, &cap_futimes_rights, &fp);
3389 if (AUDITING_TD(td)) {
3390 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
3391 AUDIT_ARG_VNODE1(fp->f_vnode);
3392 VOP_UNLOCK(fp->f_vnode);
3395 error = setutimes(td, fp->f_vnode, ts, 2, flags & UTIMENS_NULL);
3401 sys_utimensat(struct thread *td, struct utimensat_args *uap)
3404 return (kern_utimensat(td, uap->fd, uap->path, UIO_USERSPACE,
3405 uap->times, UIO_USERSPACE, uap->flag));
3409 kern_utimensat(struct thread *td, int fd, const char *path,
3410 enum uio_seg pathseg, const struct timespec *tptr, enum uio_seg tptrseg,
3413 struct nameidata nd;
3414 struct timespec ts[2];
3417 if ((flag & ~(AT_SYMLINK_NOFOLLOW | AT_RESOLVE_BENEATH |
3418 AT_EMPTY_PATH)) != 0)
3421 if ((error = getutimens(tptr, tptrseg, ts, &flags)) != 0)
3423 NDINIT_ATRIGHTS(&nd, LOOKUP, at2cnpflags(flag, AT_SYMLINK_NOFOLLOW |
3424 AT_RESOLVE_BENEATH | AT_EMPTY_PATH) | AUDITVNODE1,
3425 pathseg, path, fd, &cap_futimes_rights);
3426 if ((error = namei(&nd)) != 0)
3429 * We are allowed to call namei() regardless of 2xUTIME_OMIT.
3431 * "If both tv_nsec fields are UTIME_OMIT... EACCESS may be detected."
3432 * "Search permission is denied by a component of the path prefix."
3434 NDFREE_NOTHING(&nd);
3435 if ((flags & UTIMENS_EXIT) == 0)
3436 error = setutimes(td, nd.ni_vp, ts, 2, flags & UTIMENS_NULL);
3442 * Truncate a file given its path name.
3444 #ifndef _SYS_SYSPROTO_H_
3445 struct truncate_args {
3452 sys_truncate(struct thread *td, struct truncate_args *uap)
3455 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3459 kern_truncate(struct thread *td, const char *path, enum uio_seg pathseg,
3466 struct nameidata nd;
3473 NDINIT(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path);
3474 if ((error = namei(&nd)) != 0)
3477 rl_cookie = vn_rangelock_wlock(vp, 0, OFF_MAX);
3478 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0) {
3479 vn_rangelock_unlock(vp, rl_cookie);
3484 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3485 if (vp->v_type == VDIR)
3488 else if ((error = mac_vnode_check_write(td->td_ucred, NOCRED, vp))) {
3491 else if ((error = vn_writechk(vp)) == 0 &&
3492 (error = VOP_ACCESS(vp, VWRITE, td->td_ucred, td)) == 0) {
3494 vattr.va_size = length;
3495 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3498 vn_finished_write(mp);
3499 vn_rangelock_unlock(vp, rl_cookie);
3501 if (error == ERELOOKUP)
3506 #if defined(COMPAT_43)
3508 * Truncate a file given its path name.
3510 #ifndef _SYS_SYSPROTO_H_
3511 struct otruncate_args {
3517 otruncate(struct thread *td, struct otruncate_args *uap)
3520 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3522 #endif /* COMPAT_43 */
3524 #if defined(COMPAT_FREEBSD6)
3525 /* Versions with the pad argument */
3527 freebsd6_truncate(struct thread *td, struct freebsd6_truncate_args *uap)
3530 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3534 freebsd6_ftruncate(struct thread *td, struct freebsd6_ftruncate_args *uap)
3537 return (kern_ftruncate(td, uap->fd, uap->length));
3542 kern_fsync(struct thread *td, int fd, bool fullsync)
3550 error = getvnode(td, fd, &cap_fsync_rights, &fp);
3556 /* XXXKIB: compete outstanding aio writes */;
3559 error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
3562 vn_lock(vp, vn_lktype_write(mp, vp) | LK_RETRY);
3563 AUDIT_ARG_VNODE1(vp);
3564 if (vp->v_object != NULL) {
3565 VM_OBJECT_WLOCK(vp->v_object);
3566 vm_object_page_clean(vp->v_object, 0, 0, 0);
3567 VM_OBJECT_WUNLOCK(vp->v_object);
3569 error = fullsync ? VOP_FSYNC(vp, MNT_WAIT, td) : VOP_FDATASYNC(vp, td);
3571 vn_finished_write(mp);
3572 if (error == ERELOOKUP)
3580 * Sync an open file.
3582 #ifndef _SYS_SYSPROTO_H_
3588 sys_fsync(struct thread *td, struct fsync_args *uap)
3591 return (kern_fsync(td, uap->fd, true));
3595 sys_fdatasync(struct thread *td, struct fdatasync_args *uap)
3598 return (kern_fsync(td, uap->fd, false));
3602 * Rename files. Source and destination must either both be directories, or
3603 * both not be directories. If target is a directory, it must be empty.
3605 #ifndef _SYS_SYSPROTO_H_
3606 struct rename_args {
3612 sys_rename(struct thread *td, struct rename_args *uap)
3615 return (kern_renameat(td, AT_FDCWD, uap->from, AT_FDCWD,
3616 uap->to, UIO_USERSPACE));
3619 #ifndef _SYS_SYSPROTO_H_
3620 struct renameat_args {
3628 sys_renameat(struct thread *td, struct renameat_args *uap)
3631 return (kern_renameat(td, uap->oldfd, uap->old, uap->newfd, uap->new,
3637 kern_renameat_mac(struct thread *td, int oldfd, const char *old, int newfd,
3638 const char *new, enum uio_seg pathseg, struct nameidata *fromnd)
3642 NDINIT_ATRIGHTS(fromnd, DELETE, LOCKPARENT | LOCKLEAF | SAVESTART |
3643 AUDITVNODE1, pathseg, old, oldfd, &cap_renameat_source_rights);
3644 if ((error = namei(fromnd)) != 0)
3646 error = mac_vnode_check_rename_from(td->td_ucred, fromnd->ni_dvp,
3647 fromnd->ni_vp, &fromnd->ni_cnd);
3648 VOP_UNLOCK(fromnd->ni_dvp);
3649 if (fromnd->ni_dvp != fromnd->ni_vp)
3650 VOP_UNLOCK(fromnd->ni_vp);
3652 NDFREE_PNBUF(fromnd);
3653 vrele(fromnd->ni_dvp);
3654 vrele(fromnd->ni_vp);
3655 if (fromnd->ni_startdir)
3656 vrele(fromnd->ni_startdir);
3663 kern_renameat(struct thread *td, int oldfd, const char *old, int newfd,
3664 const char *new, enum uio_seg pathseg)
3666 struct mount *mp = NULL;
3667 struct vnode *tvp, *fvp, *tdvp;
3668 struct nameidata fromnd, tond;
3675 if (mac_vnode_check_rename_from_enabled()) {
3676 error = kern_renameat_mac(td, oldfd, old, newfd, new, pathseg,
3682 NDINIT_ATRIGHTS(&fromnd, DELETE, WANTPARENT | SAVESTART | AUDITVNODE1,
3683 pathseg, old, oldfd, &cap_renameat_source_rights);
3684 if ((error = namei(&fromnd)) != 0)
3690 tondflags = LOCKPARENT | LOCKLEAF | NOCACHE | SAVESTART | AUDITVNODE2;
3691 if (fromnd.ni_vp->v_type == VDIR)
3692 tondflags |= WILLBEDIR;
3693 NDINIT_ATRIGHTS(&tond, RENAME, tondflags, pathseg, new, newfd,
3694 &cap_renameat_target_rights);
3695 if ((error = namei(&tond)) != 0) {
3696 /* Translate error code for rename("dir1", "dir2/."). */
3697 if (error == EISDIR && fvp->v_type == VDIR)
3699 NDFREE_PNBUF(&fromnd);
3700 vrele(fromnd.ni_dvp);
3706 error = vn_start_write(fvp, &mp, V_NOWAIT);
3708 NDFREE_PNBUF(&fromnd);
3709 NDFREE_PNBUF(&tond);
3716 vrele(fromnd.ni_dvp);
3718 vrele(tond.ni_startdir);
3719 if (fromnd.ni_startdir != NULL)
3720 vrele(fromnd.ni_startdir);
3721 error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH);
3727 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
3730 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
3735 if (newfd != AT_FDCWD && (tond.ni_resflags & NIRES_ABS) == 0) {
3737 * If the target already exists we require CAP_UNLINKAT
3738 * from 'newfd', when newfd was used for the lookup.
3740 error = cap_check(&tond.ni_filecaps.fc_rights,
3741 &cap_unlinkat_rights);
3752 * If the source is the same as the destination (that is, if they
3753 * are links to the same vnode), then there is nothing to do.
3759 error = mac_vnode_check_rename_to(td->td_ucred, tdvp,
3760 tond.ni_vp, fromnd.ni_dvp == tdvp, &tond.ni_cnd);
3764 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
3765 tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
3766 NDFREE_PNBUF(&fromnd);
3767 NDFREE_PNBUF(&tond);
3769 NDFREE_PNBUF(&fromnd);
3770 NDFREE_PNBUF(&tond);
3777 vrele(fromnd.ni_dvp);
3780 vrele(tond.ni_startdir);
3781 vn_finished_write(mp);
3783 if (fromnd.ni_startdir)
3784 vrele(fromnd.ni_startdir);
3785 if (error == ERESTART)
3787 if (error == ERELOOKUP)
3793 * Make a directory file.
3795 #ifndef _SYS_SYSPROTO_H_
3802 sys_mkdir(struct thread *td, struct mkdir_args *uap)
3805 return (kern_mkdirat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
3809 #ifndef _SYS_SYSPROTO_H_
3810 struct mkdirat_args {
3817 sys_mkdirat(struct thread *td, struct mkdirat_args *uap)
3820 return (kern_mkdirat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode));
3824 kern_mkdirat(struct thread *td, int fd, const char *path, enum uio_seg segflg,
3829 struct nameidata nd;
3832 AUDIT_ARG_MODE(mode);
3836 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
3837 NC_NOMAKEENTRY | NC_KEEPPOSENTRY | FAILIFEXISTS | WILLBEDIR,
3838 segflg, path, fd, &cap_mkdirat_rights);
3839 if ((error = namei(&nd)) != 0)
3841 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3844 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3849 vattr.va_type = VDIR;
3850 vattr.va_mode = (mode & ACCESSPERMS) &~ td->td_proc->p_pd->pd_cmask;
3852 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
3857 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
3862 VOP_VPUT_PAIR(nd.ni_dvp, error == 0 ? &nd.ni_vp : NULL, true);
3863 vn_finished_write(mp);
3864 if (error == ERELOOKUP)
3870 * Remove a directory file.
3872 #ifndef _SYS_SYSPROTO_H_
3878 sys_rmdir(struct thread *td, struct rmdir_args *uap)
3881 return (kern_frmdirat(td, AT_FDCWD, uap->path, FD_NONE, UIO_USERSPACE,
3886 kern_frmdirat(struct thread *td, int dfd, const char *path, int fd,
3887 enum uio_seg pathseg, int flag)
3892 struct nameidata nd;
3893 cap_rights_t rights;
3897 if (fd != FD_NONE) {
3898 error = getvnode(td, fd, cap_rights_init_one(&rights,
3907 NDINIT_ATRIGHTS(&nd, DELETE, LOCKPARENT | LOCKLEAF | AUDITVNODE1 |
3908 at2cnpflags(flag, AT_RESOLVE_BENEATH),
3909 pathseg, path, dfd, &cap_unlinkat_rights);
3910 if ((error = namei(&nd)) != 0)
3913 if (vp->v_type != VDIR) {
3918 * No rmdir "." please.
3920 if (nd.ni_dvp == vp) {
3925 * The root of a mounted filesystem cannot be deleted.
3927 if (vp->v_vflag & VV_ROOT) {
3932 if (fp != NULL && fp->f_vnode != vp) {
3933 if (VN_IS_DOOMED(fp->f_vnode))
3941 error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp,
3946 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3949 if (nd.ni_dvp == vp)
3953 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3957 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_UNLINK);
3958 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
3959 vn_finished_write(mp);
3963 if (nd.ni_dvp == vp)
3967 if (error == ERELOOKUP)
3975 #if defined(COMPAT_43) || defined(COMPAT_FREEBSD11)
3977 freebsd11_kern_getdirentries(struct thread *td, int fd, char *ubuf, u_int count,
3978 long *basep, void (*func)(struct freebsd11_dirent *))
3980 struct freebsd11_dirent dstdp;
3981 struct dirent *dp, *edp;
3984 ssize_t resid, ucount;
3987 /* XXX arbitrary sanity limit on `count'. */
3988 count = min(count, 64 * 1024);
3990 dirbuf = malloc(count, M_TEMP, M_WAITOK);
3992 error = kern_getdirentries(td, fd, dirbuf, count, &base, &resid,
4000 for (dp = (struct dirent *)dirbuf,
4001 edp = (struct dirent *)&dirbuf[count - resid];
4002 ucount < count && dp < edp; ) {
4003 if (dp->d_reclen == 0)
4005 MPASS(dp->d_reclen >= _GENERIC_DIRLEN(0));
4006 if (dp->d_namlen >= sizeof(dstdp.d_name))
4008 dstdp.d_type = dp->d_type;
4009 dstdp.d_namlen = dp->d_namlen;
4010 dstdp.d_fileno = dp->d_fileno; /* truncate */
4011 if (dstdp.d_fileno != dp->d_fileno) {
4012 switch (ino64_trunc_error) {
4020 dstdp.d_fileno = UINT32_MAX;
4024 dstdp.d_reclen = sizeof(dstdp) - sizeof(dstdp.d_name) +
4025 ((dp->d_namlen + 1 + 3) &~ 3);
4026 bcopy(dp->d_name, dstdp.d_name, dstdp.d_namlen);
4027 bzero(dstdp.d_name + dstdp.d_namlen,
4028 dstdp.d_reclen - offsetof(struct freebsd11_dirent, d_name) -
4030 MPASS(dstdp.d_reclen <= dp->d_reclen);
4031 MPASS(ucount + dstdp.d_reclen <= count);
4034 error = copyout(&dstdp, ubuf + ucount, dstdp.d_reclen);
4037 dp = (struct dirent *)((char *)dp + dp->d_reclen);
4038 ucount += dstdp.d_reclen;
4042 free(dirbuf, M_TEMP);
4044 td->td_retval[0] = ucount;
4051 ogetdirentries_cvt(struct freebsd11_dirent *dp)
4053 #if (BYTE_ORDER == LITTLE_ENDIAN)
4055 * The expected low byte of dp->d_namlen is our dp->d_type.
4056 * The high MBZ byte of dp->d_namlen is our dp->d_namlen.
4058 dp->d_type = dp->d_namlen;
4062 * The dp->d_type is the high byte of the expected dp->d_namlen,
4063 * so must be zero'ed.
4070 * Read a block of directory entries in a filesystem independent format.
4072 #ifndef _SYS_SYSPROTO_H_
4073 struct ogetdirentries_args {
4081 ogetdirentries(struct thread *td, struct ogetdirentries_args *uap)
4086 error = kern_ogetdirentries(td, uap, &loff);
4088 error = copyout(&loff, uap->basep, sizeof(long));
4093 kern_ogetdirentries(struct thread *td, struct ogetdirentries_args *uap,
4099 /* XXX arbitrary sanity limit on `count'. */
4100 if (uap->count > 64 * 1024)
4103 error = freebsd11_kern_getdirentries(td, uap->fd, uap->buf, uap->count,
4104 &base, ogetdirentries_cvt);
4106 if (error == 0 && uap->basep != NULL)
4107 error = copyout(&base, uap->basep, sizeof(long));
4111 #endif /* COMPAT_43 */
4113 #if defined(COMPAT_FREEBSD11)
4114 #ifndef _SYS_SYSPROTO_H_
4115 struct freebsd11_getdirentries_args {
4123 freebsd11_getdirentries(struct thread *td,
4124 struct freebsd11_getdirentries_args *uap)
4129 error = freebsd11_kern_getdirentries(td, uap->fd, uap->buf, uap->count,
4132 if (error == 0 && uap->basep != NULL)
4133 error = copyout(&base, uap->basep, sizeof(long));
4138 freebsd11_getdents(struct thread *td, struct freebsd11_getdents_args *uap)
4140 struct freebsd11_getdirentries_args ap;
4144 ap.count = uap->count;
4146 return (freebsd11_getdirentries(td, &ap));
4148 #endif /* COMPAT_FREEBSD11 */
4151 * Read a block of directory entries in a filesystem independent format.
4154 sys_getdirentries(struct thread *td, struct getdirentries_args *uap)
4159 error = kern_getdirentries(td, uap->fd, uap->buf, uap->count, &base,
4160 NULL, UIO_USERSPACE);
4163 if (uap->basep != NULL)
4164 error = copyout(&base, uap->basep, sizeof(off_t));
4169 kern_getdirentries(struct thread *td, int fd, char *buf, size_t count,
4170 off_t *basep, ssize_t *residp, enum uio_seg bufseg)
4181 if (count > IOSIZE_MAX)
4183 auio.uio_resid = count;
4184 error = getvnode(td, fd, &cap_read_rights, &fp);
4187 if ((fp->f_flag & FREAD) == 0) {
4192 foffset = foffset_lock(fp, 0);
4194 if (vp->v_type != VDIR) {
4198 aiov.iov_base = buf;
4199 aiov.iov_len = count;
4200 auio.uio_iov = &aiov;
4201 auio.uio_iovcnt = 1;
4202 auio.uio_rw = UIO_READ;
4203 auio.uio_segflg = bufseg;
4205 vn_lock(vp, LK_SHARED | LK_RETRY);
4206 AUDIT_ARG_VNODE1(vp);
4207 loff = auio.uio_offset = foffset;
4209 error = mac_vnode_check_readdir(td->td_ucred, vp);
4212 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL,
4214 foffset = auio.uio_offset;
4219 if (count == auio.uio_resid &&
4220 (vp->v_vflag & VV_ROOT) &&
4221 (vp->v_mount->mnt_flag & MNT_UNION)) {
4222 struct vnode *tvp = vp;
4224 vp = vp->v_mount->mnt_vnodecovered;
4234 *residp = auio.uio_resid;
4235 td->td_retval[0] = count - auio.uio_resid;
4237 foffset_unlock(fp, foffset, 0);
4243 * Set the mode mask for creation of filesystem nodes.
4245 #ifndef _SYS_SYSPROTO_H_
4251 sys_umask(struct thread *td, struct umask_args *uap)
4253 struct pwddesc *pdp;
4255 pdp = td->td_proc->p_pd;
4257 td->td_retval[0] = pdp->pd_cmask;
4258 pdp->pd_cmask = uap->newmask & ALLPERMS;
4259 PWDDESC_XUNLOCK(pdp);
4264 * Void all references to file by ripping underlying filesystem away from
4267 #ifndef _SYS_SYSPROTO_H_
4268 struct revoke_args {
4273 sys_revoke(struct thread *td, struct revoke_args *uap)
4277 struct nameidata nd;
4280 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
4282 if ((error = namei(&nd)) != 0)
4285 NDFREE_NOTHING(&nd);
4286 if (vp->v_type != VCHR || vp->v_rdev == NULL) {
4291 error = mac_vnode_check_revoke(td->td_ucred, vp);
4295 error = VOP_GETATTR(vp, &vattr, td->td_ucred);
4298 if (td->td_ucred->cr_uid != vattr.va_uid) {
4299 error = priv_check(td, PRIV_VFS_ADMIN);
4303 if (devfs_usecount(vp) > 0)
4304 VOP_REVOKE(vp, REVOKEALL);
4311 * This variant of getvnode() allows O_PATH files. Caller should
4312 * ensure that returned file and vnode are only used for compatible
4316 getvnode_path(struct thread *td, int fd, cap_rights_t *rightsp,
4322 error = fget_unlocked(td, fd, rightsp, &fp);
4327 * The file could be not of the vnode type, or it may be not
4328 * yet fully initialized, in which case the f_vnode pointer
4329 * may be set, but f_ops is still badfileops. E.g.,
4330 * devfs_open() transiently create such situation to
4331 * facilitate csw d_fdopen().
4333 * Dupfdopen() handling in kern_openat() installs the
4334 * half-baked file into the process descriptor table, allowing
4335 * other thread to dereference it. Guard against the race by
4338 if (__predict_false(fp->f_vnode == NULL || fp->f_ops == &badfileops)) {
4349 * Convert a user file descriptor to a kernel file entry and check
4350 * that, if it is a capability, the correct rights are present.
4351 * A reference on the file entry is held upon returning.
4354 getvnode(struct thread *td, int fd, cap_rights_t *rightsp, struct file **fpp)
4358 error = getvnode_path(td, fd, rightsp, fpp);
4359 if (__predict_false(error != 0))
4363 * Filter out O_PATH file descriptors, most getvnode() callers
4364 * do not call fo_ methods.
4366 if (__predict_false((*fpp)->f_ops == &path_fileops)) {
4376 * Get an (NFS) file handle.
4378 #ifndef _SYS_SYSPROTO_H_
4379 struct lgetfh_args {
4385 sys_lgetfh(struct thread *td, struct lgetfh_args *uap)
4388 return (kern_getfhat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->fname,
4389 UIO_USERSPACE, uap->fhp, UIO_USERSPACE));
4392 #ifndef _SYS_SYSPROTO_H_
4399 sys_getfh(struct thread *td, struct getfh_args *uap)
4402 return (kern_getfhat(td, 0, AT_FDCWD, uap->fname, UIO_USERSPACE,
4403 uap->fhp, UIO_USERSPACE));
4407 * syscall for the rpc.lockd to use to translate an open descriptor into
4408 * a NFS file handle.
4410 * warning: do not remove the priv_check() call or this becomes one giant
4413 #ifndef _SYS_SYSPROTO_H_
4414 struct getfhat_args {
4422 sys_getfhat(struct thread *td, struct getfhat_args *uap)
4425 return (kern_getfhat(td, uap->flags, uap->fd, uap->path, UIO_USERSPACE,
4426 uap->fhp, UIO_USERSPACE));
4430 kern_getfhat(struct thread *td, int flags, int fd, const char *path,
4431 enum uio_seg pathseg, fhandle_t *fhp, enum uio_seg fhseg)
4433 struct nameidata nd;
4438 if ((flags & ~(AT_SYMLINK_NOFOLLOW | AT_RESOLVE_BENEATH)) != 0)
4440 error = priv_check(td, PRIV_VFS_GETFH);
4443 NDINIT_AT(&nd, LOOKUP, at2cnpflags(flags, AT_SYMLINK_NOFOLLOW |
4444 AT_RESOLVE_BENEATH) | LOCKLEAF | AUDITVNODE1, pathseg, path,
4449 NDFREE_NOTHING(&nd);
4451 bzero(&fh, sizeof(fh));
4452 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
4453 error = VOP_VPTOFH(vp, &fh.fh_fid);
4456 if (fhseg == UIO_USERSPACE)
4457 error = copyout(&fh, fhp, sizeof (fh));
4459 memcpy(fhp, &fh, sizeof(fh));
4464 #ifndef _SYS_SYSPROTO_H_
4465 struct fhlink_args {
4471 sys_fhlink(struct thread *td, struct fhlink_args *uap)
4474 return (kern_fhlinkat(td, AT_FDCWD, uap->to, UIO_USERSPACE, uap->fhp));
4477 #ifndef _SYS_SYSPROTO_H_
4478 struct fhlinkat_args {
4485 sys_fhlinkat(struct thread *td, struct fhlinkat_args *uap)
4488 return (kern_fhlinkat(td, uap->tofd, uap->to, UIO_USERSPACE, uap->fhp));
4492 kern_fhlinkat(struct thread *td, int fd, const char *path,
4493 enum uio_seg pathseg, fhandle_t *fhp)
4500 error = priv_check(td, PRIV_VFS_GETFH);
4503 error = copyin(fhp, &fh, sizeof(fh));
4508 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4510 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_SHARED, &vp);
4515 error = kern_linkat_vp(td, vp, fd, path, pathseg);
4516 } while (error == EAGAIN || error == ERELOOKUP);
4520 #ifndef _SYS_SYSPROTO_H_
4521 struct fhreadlink_args {
4528 sys_fhreadlink(struct thread *td, struct fhreadlink_args *uap)
4535 error = priv_check(td, PRIV_VFS_GETFH);
4538 if (uap->bufsize > IOSIZE_MAX)
4540 error = copyin(uap->fhp, &fh, sizeof(fh));
4543 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4545 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_SHARED, &vp);
4549 error = kern_readlink_vp(vp, uap->buf, UIO_USERSPACE, uap->bufsize, td);
4555 * syscall for the rpc.lockd to use to translate a NFS file handle into an
4558 * warning: do not remove the priv_check() call or this becomes one giant
4561 #ifndef _SYS_SYSPROTO_H_
4562 struct fhopen_args {
4563 const struct fhandle *u_fhp;
4568 sys_fhopen(struct thread *td, struct fhopen_args *uap)
4570 return (kern_fhopen(td, uap->u_fhp, uap->flags));
4574 kern_fhopen(struct thread *td, const struct fhandle *u_fhp, int flags)
4583 error = priv_check(td, PRIV_VFS_FHOPEN);
4587 fmode = FFLAGS(flags);
4588 /* why not allow a non-read/write open for our lockd? */
4589 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
4591 error = copyin(u_fhp, &fhp, sizeof(fhp));
4594 /* find the mount point */
4595 mp = vfs_busyfs(&fhp.fh_fsid);
4598 /* now give me my vnode, it gets returned to me locked */
4599 error = VFS_FHTOVP(mp, &fhp.fh_fid, LK_EXCLUSIVE, &vp);
4604 error = falloc_noinstall(td, &fp);
4610 * An extra reference on `fp' has been held for us by
4611 * falloc_noinstall().
4617 error = vn_open_vnode(vp, fmode, td->td_ucred, td, fp);
4619 KASSERT(fp->f_ops == &badfileops,
4620 ("VOP_OPEN in fhopen() set f_ops"));
4621 KASSERT(td->td_dupfd < 0,
4622 ("fhopen() encountered fdopen()"));
4631 finit_vnode(fp, fmode, NULL, &vnops);
4633 if ((fmode & O_TRUNC) != 0) {
4634 error = fo_truncate(fp, 0, td->td_ucred, td);
4639 error = finstall(td, fp, &indx, fmode, NULL);
4642 td->td_retval[0] = indx;
4647 * Stat an (NFS) file handle.
4649 #ifndef _SYS_SYSPROTO_H_
4650 struct fhstat_args {
4651 struct fhandle *u_fhp;
4656 sys_fhstat(struct thread *td, struct fhstat_args *uap)
4662 error = copyin(uap->u_fhp, &fh, sizeof(fh));
4665 error = kern_fhstat(td, fh, &sb);
4667 error = copyout(&sb, uap->sb, sizeof(sb));
4672 kern_fhstat(struct thread *td, struct fhandle fh, struct stat *sb)
4678 error = priv_check(td, PRIV_VFS_FHSTAT);
4681 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4683 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_EXCLUSIVE, &vp);
4687 error = VOP_STAT(vp, sb, td->td_ucred, NOCRED);
4693 * Implement fstatfs() for (NFS) file handles.
4695 #ifndef _SYS_SYSPROTO_H_
4696 struct fhstatfs_args {
4697 struct fhandle *u_fhp;
4702 sys_fhstatfs(struct thread *td, struct fhstatfs_args *uap)
4708 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4711 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
4712 error = kern_fhstatfs(td, fh, sfp);
4714 error = copyout(sfp, uap->buf, sizeof(*sfp));
4715 free(sfp, M_STATFS);
4720 kern_fhstatfs(struct thread *td, fhandle_t fh, struct statfs *buf)
4726 error = priv_check(td, PRIV_VFS_FHSTATFS);
4729 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4731 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_EXCLUSIVE, &vp);
4737 error = prison_canseemount(td->td_ucred, mp);
4741 error = mac_mount_check_stat(td->td_ucred, mp);
4745 error = VFS_STATFS(mp, buf);
4752 * Unlike madvise(2), we do not make a best effort to remember every
4753 * possible caching hint. Instead, we remember the last setting with
4754 * the exception that we will allow POSIX_FADV_NORMAL to adjust the
4755 * region of any current setting.
4758 kern_posix_fadvise(struct thread *td, int fd, off_t offset, off_t len,
4761 struct fadvise_info *fa, *new;
4767 if (offset < 0 || len < 0 || offset > OFF_MAX - len)
4769 AUDIT_ARG_VALUE(advice);
4771 case POSIX_FADV_SEQUENTIAL:
4772 case POSIX_FADV_RANDOM:
4773 case POSIX_FADV_NOREUSE:
4774 new = malloc(sizeof(*fa), M_FADVISE, M_WAITOK);
4776 case POSIX_FADV_NORMAL:
4777 case POSIX_FADV_WILLNEED:
4778 case POSIX_FADV_DONTNEED:
4784 /* XXX: CAP_POSIX_FADVISE? */
4786 error = fget(td, fd, &cap_no_rights, &fp);
4789 AUDIT_ARG_FILE(td->td_proc, fp);
4790 if ((fp->f_ops->fo_flags & DFLAG_SEEKABLE) == 0) {
4794 if (fp->f_type != DTYPE_VNODE) {
4799 if (vp->v_type != VREG) {
4806 end = offset + len - 1;
4808 case POSIX_FADV_SEQUENTIAL:
4809 case POSIX_FADV_RANDOM:
4810 case POSIX_FADV_NOREUSE:
4812 * Try to merge any existing non-standard region with
4813 * this new region if possible, otherwise create a new
4814 * non-standard region for this request.
4816 mtx_pool_lock(mtxpool_sleep, fp);
4818 if (fa != NULL && fa->fa_advice == advice &&
4819 ((fa->fa_start <= end && fa->fa_end >= offset) ||
4820 (end != OFF_MAX && fa->fa_start == end + 1) ||
4821 (fa->fa_end != OFF_MAX && fa->fa_end + 1 == offset))) {
4822 if (offset < fa->fa_start)
4823 fa->fa_start = offset;
4824 if (end > fa->fa_end)
4827 new->fa_advice = advice;
4828 new->fa_start = offset;
4833 mtx_pool_unlock(mtxpool_sleep, fp);
4835 case POSIX_FADV_NORMAL:
4837 * If a the "normal" region overlaps with an existing
4838 * non-standard region, trim or remove the
4839 * non-standard region.
4841 mtx_pool_lock(mtxpool_sleep, fp);
4844 if (offset <= fa->fa_start && end >= fa->fa_end) {
4846 fp->f_advice = NULL;
4847 } else if (offset <= fa->fa_start &&
4848 end >= fa->fa_start)
4849 fa->fa_start = end + 1;
4850 else if (offset <= fa->fa_end && end >= fa->fa_end)
4851 fa->fa_end = offset - 1;
4852 else if (offset >= fa->fa_start && end <= fa->fa_end) {
4854 * If the "normal" region is a middle
4855 * portion of the existing
4856 * non-standard region, just remove
4857 * the whole thing rather than picking
4858 * one side or the other to
4862 fp->f_advice = NULL;
4865 mtx_pool_unlock(mtxpool_sleep, fp);
4867 case POSIX_FADV_WILLNEED:
4868 case POSIX_FADV_DONTNEED:
4869 error = VOP_ADVISE(vp, offset, end, advice);
4875 free(new, M_FADVISE);
4880 sys_posix_fadvise(struct thread *td, struct posix_fadvise_args *uap)
4884 error = kern_posix_fadvise(td, uap->fd, uap->offset, uap->len,
4886 return (kern_posix_error(td, error));
4890 kern_copy_file_range(struct thread *td, int infd, off_t *inoffp, int outfd,
4891 off_t *outoffp, size_t len, unsigned int flags)
4893 struct file *infp, *outfp;
4894 struct vnode *invp, *outvp;
4897 void *rl_rcookie, *rl_wcookie;
4898 off_t savinoff, savoutoff;
4900 infp = outfp = NULL;
4901 rl_rcookie = rl_wcookie = NULL;
4910 if (len > SSIZE_MAX)
4912 * Although the len argument is size_t, the return argument
4913 * is ssize_t (which is signed). Therefore a size that won't
4914 * fit in ssize_t can't be returned.
4918 /* Get the file structures for the file descriptors. */
4919 error = fget_read(td, infd, &cap_read_rights, &infp);
4922 if (infp->f_ops == &badfileops) {
4926 if (infp->f_vnode == NULL) {
4930 error = fget_write(td, outfd, &cap_write_rights, &outfp);
4933 if (outfp->f_ops == &badfileops) {
4937 if (outfp->f_vnode == NULL) {
4942 /* Set the offset pointers to the correct place. */
4944 inoffp = &infp->f_offset;
4945 if (outoffp == NULL)
4946 outoffp = &outfp->f_offset;
4948 savoutoff = *outoffp;
4950 invp = infp->f_vnode;
4951 outvp = outfp->f_vnode;
4952 /* Sanity check the f_flag bits. */
4953 if ((outfp->f_flag & (FWRITE | FAPPEND)) != FWRITE ||
4954 (infp->f_flag & FREAD) == 0) {
4959 /* If len == 0, just return 0. */
4964 * If infp and outfp refer to the same file, the byte ranges cannot
4967 if (invp == outvp && ((savinoff <= savoutoff && savinoff + len >
4968 savoutoff) || (savinoff > savoutoff && savoutoff + len >
4974 /* Range lock the byte ranges for both invp and outvp. */
4976 rl_wcookie = vn_rangelock_wlock(outvp, *outoffp, *outoffp +
4978 rl_rcookie = vn_rangelock_tryrlock(invp, *inoffp, *inoffp +
4980 if (rl_rcookie != NULL)
4982 vn_rangelock_unlock(outvp, rl_wcookie);
4983 rl_rcookie = vn_rangelock_rlock(invp, *inoffp, *inoffp + len);
4984 vn_rangelock_unlock(invp, rl_rcookie);
4988 error = vn_copy_file_range(invp, inoffp, outvp, outoffp, &retlen,
4989 flags, infp->f_cred, outfp->f_cred, td);
4991 if (rl_rcookie != NULL)
4992 vn_rangelock_unlock(invp, rl_rcookie);
4993 if (rl_wcookie != NULL)
4994 vn_rangelock_unlock(outvp, rl_wcookie);
4995 if (savinoff != -1 && (error == EINTR || error == ERESTART)) {
4997 *outoffp = savoutoff;
5003 td->td_retval[0] = retlen;
5008 sys_copy_file_range(struct thread *td, struct copy_file_range_args *uap)
5010 off_t inoff, outoff, *inoffp, *outoffp;
5013 inoffp = outoffp = NULL;
5014 if (uap->inoffp != NULL) {
5015 error = copyin(uap->inoffp, &inoff, sizeof(off_t));
5020 if (uap->outoffp != NULL) {
5021 error = copyin(uap->outoffp, &outoff, sizeof(off_t));
5026 error = kern_copy_file_range(td, uap->infd, inoffp, uap->outfd,
5027 outoffp, uap->len, uap->flags);
5028 if (error == 0 && uap->inoffp != NULL)
5029 error = copyout(inoffp, uap->inoffp, sizeof(off_t));
5030 if (error == 0 && uap->outoffp != NULL)
5031 error = copyout(outoffp, uap->outoffp, sizeof(off_t));