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>
49 #include <sys/capsicum.h>
51 #include <sys/sysent.h>
52 #include <sys/malloc.h>
53 #include <sys/mount.h>
54 #include <sys/mutex.h>
55 #include <sys/sysproto.h>
56 #include <sys/namei.h>
57 #include <sys/filedesc.h>
58 #include <sys/kernel.h>
59 #include <sys/fcntl.h>
61 #include <sys/filio.h>
62 #include <sys/limits.h>
63 #include <sys/linker.h>
64 #include <sys/rwlock.h>
68 #include <sys/unistd.h>
69 #include <sys/vnode.h>
72 #include <sys/dirent.h>
74 #include <sys/syscallsubr.h>
75 #include <sys/sysctl.h>
77 #include <sys/ktrace.h>
80 #include <machine/stdarg.h>
82 #include <security/audit/audit.h>
83 #include <security/mac/mac_framework.h>
86 #include <vm/vm_object.h>
87 #include <vm/vm_page.h>
90 #include <fs/devfs/devfs.h>
92 MALLOC_DEFINE(M_FADVISE, "fadvise", "posix_fadvise(2) information");
94 static int kern_chflagsat(struct thread *td, int fd, const char *path,
95 enum uio_seg pathseg, u_long flags, int atflag);
96 static int setfflags(struct thread *td, struct vnode *, u_long);
97 static int getutimes(const struct timeval *, enum uio_seg, struct timespec *);
98 static int getutimens(const struct timespec *, enum uio_seg,
99 struct timespec *, int *);
100 static int setutimes(struct thread *td, struct vnode *,
101 const struct timespec *, int, int);
102 static int vn_access(struct vnode *vp, int user_flags, struct ucred *cred,
104 static int kern_fhlinkat(struct thread *td, int fd, const char *path,
105 enum uio_seg pathseg, fhandle_t *fhp);
106 static int kern_readlink_vp(struct vnode *vp, char *buf, enum uio_seg bufseg,
107 size_t count, struct thread *td);
108 static int kern_linkat_vp(struct thread *td, struct vnode *vp, int fd,
109 const char *path, enum uio_seg segflag);
112 at2cnpflags(u_int at_flags, u_int mask)
116 MPASS((at_flags & (AT_SYMLINK_FOLLOW | AT_SYMLINK_NOFOLLOW)) !=
117 (AT_SYMLINK_FOLLOW | AT_SYMLINK_NOFOLLOW));
121 if ((at_flags & AT_RESOLVE_BENEATH) != 0)
123 if ((at_flags & AT_SYMLINK_FOLLOW) != 0)
125 /* NOFOLLOW is pseudo flag */
126 if ((mask & AT_SYMLINK_NOFOLLOW) != 0) {
127 res |= (at_flags & AT_SYMLINK_NOFOLLOW) != 0 ? NOFOLLOW :
130 if ((mask & AT_EMPTY_PATH) != 0 && (at_flags & AT_EMPTY_PATH) != 0)
136 kern_sync(struct thread *td)
138 struct mount *mp, *nmp;
141 mtx_lock(&mountlist_mtx);
142 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
143 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK)) {
144 nmp = TAILQ_NEXT(mp, mnt_list);
147 if ((mp->mnt_flag & MNT_RDONLY) == 0 &&
148 vn_start_write(NULL, &mp, V_NOWAIT) == 0) {
149 save = curthread_pflags_set(TDP_SYNCIO);
150 vfs_periodic(mp, MNT_NOWAIT);
151 VFS_SYNC(mp, MNT_NOWAIT);
152 curthread_pflags_restore(save);
153 vn_finished_write(mp);
155 mtx_lock(&mountlist_mtx);
156 nmp = TAILQ_NEXT(mp, mnt_list);
159 mtx_unlock(&mountlist_mtx);
164 * Sync each mounted filesystem.
166 #ifndef _SYS_SYSPROTO_H_
173 sys_sync(struct thread *td, struct sync_args *uap)
176 return (kern_sync(td));
180 * Change filesystem quotas.
182 #ifndef _SYS_SYSPROTO_H_
183 struct quotactl_args {
191 sys_quotactl(struct thread *td, struct quotactl_args *uap)
198 AUDIT_ARG_CMD(uap->cmd);
199 AUDIT_ARG_UID(uap->uid);
200 if (!prison_allow(td->td_ucred, PR_ALLOW_QUOTAS))
202 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
204 if ((error = namei(&nd)) != 0)
206 NDFREE(&nd, NDF_ONLY_PNBUF);
207 mp = nd.ni_vp->v_mount;
210 error = vfs_busy(mp, 0);
216 error = VFS_QUOTACTL(mp, uap->cmd, uap->uid, uap->arg, &mp_busy);
219 * Since quota on/off operations typically need to open quota
220 * files, the implementation may need to unbusy the mount point
221 * before calling into namei. Otherwise, unmount might be
222 * started between two vfs_busy() invocations (first is ours,
223 * second is from mount point cross-walk code in lookup()),
226 * Avoid unbusying mp if the implementation indicates it has
236 * Used by statfs conversion routines to scale the block size up if
237 * necessary so that all of the block counts are <= 'max_size'. Note
238 * that 'max_size' should be a bitmask, i.e. 2^n - 1 for some non-zero
242 statfs_scale_blocks(struct statfs *sf, long max_size)
247 KASSERT(powerof2(max_size + 1), ("%s: invalid max_size", __func__));
250 * Attempt to scale the block counts to give a more accurate
251 * overview to userland of the ratio of free space to used
252 * space. To do this, find the largest block count and compute
253 * a divisor that lets it fit into a signed integer <= max_size.
255 if (sf->f_bavail < 0)
256 count = -sf->f_bavail;
258 count = sf->f_bavail;
259 count = MAX(sf->f_blocks, MAX(sf->f_bfree, count));
260 if (count <= max_size)
263 count >>= flsl(max_size);
270 sf->f_bsize <<= shift;
271 sf->f_blocks >>= shift;
272 sf->f_bfree >>= shift;
273 sf->f_bavail >>= shift;
277 kern_do_statfs(struct thread *td, struct mount *mp, struct statfs *buf)
283 error = vfs_busy(mp, 0);
288 error = mac_mount_check_stat(td->td_ucred, mp);
292 error = VFS_STATFS(mp, buf);
295 if (priv_check_cred_vfs_generation(td->td_ucred)) {
296 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
297 prison_enforce_statfs(td->td_ucred, mp, buf);
305 * Get filesystem statistics.
307 #ifndef _SYS_SYSPROTO_H_
314 sys_statfs(struct thread *td, struct statfs_args *uap)
319 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
320 error = kern_statfs(td, uap->path, UIO_USERSPACE, sfp);
322 error = copyout(sfp, uap->buf, sizeof(struct statfs));
328 kern_statfs(struct thread *td, const char *path, enum uio_seg pathseg,
335 NDINIT(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, td);
339 mp = vfs_ref_from_vp(nd.ni_vp);
342 return (kern_do_statfs(td, mp, buf));
346 * Get filesystem statistics.
348 #ifndef _SYS_SYSPROTO_H_
349 struct fstatfs_args {
355 sys_fstatfs(struct thread *td, struct fstatfs_args *uap)
360 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
361 error = kern_fstatfs(td, uap->fd, sfp);
363 error = copyout(sfp, uap->buf, sizeof(struct statfs));
369 kern_fstatfs(struct thread *td, int fd, struct statfs *buf)
377 error = getvnode_path(td, fd, &cap_fstatfs_rights, &fp);
382 if (AUDITING_TD(td)) {
383 vn_lock(vp, LK_SHARED | LK_RETRY);
384 AUDIT_ARG_VNODE1(vp);
388 mp = vfs_ref_from_vp(vp);
390 return (kern_do_statfs(td, mp, buf));
394 * Get statistics on all filesystems.
396 #ifndef _SYS_SYSPROTO_H_
397 struct getfsstat_args {
404 sys_getfsstat(struct thread *td, struct getfsstat_args *uap)
409 if (uap->bufsize < 0 || uap->bufsize > SIZE_MAX)
411 error = kern_getfsstat(td, &uap->buf, uap->bufsize, &count,
412 UIO_USERSPACE, uap->mode);
414 td->td_retval[0] = count;
419 * If (bufsize > 0 && bufseg == UIO_SYSSPACE)
420 * The caller is responsible for freeing memory which will be allocated
424 kern_getfsstat(struct thread *td, struct statfs **buf, size_t bufsize,
425 size_t *countp, enum uio_seg bufseg, int mode)
427 struct mount *mp, *nmp;
428 struct statfs *sfsp, *sp, *sptmp, *tofree;
429 size_t count, maxcount;
437 if (bufseg == UIO_SYSSPACE)
442 maxcount = bufsize / sizeof(struct statfs);
446 } else if (bufseg == UIO_USERSPACE) {
449 } else /* if (bufseg == UIO_SYSSPACE) */ {
451 mtx_lock(&mountlist_mtx);
452 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
455 mtx_unlock(&mountlist_mtx);
456 if (maxcount > count)
458 tofree = sfsp = *buf = malloc(maxcount * sizeof(struct statfs),
465 * If there is no target buffer they only want the count.
467 * This could be TAILQ_FOREACH but it is open-coded to match the original
471 mtx_lock(&mountlist_mtx);
472 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
473 if (prison_canseemount(td->td_ucred, mp) != 0) {
474 nmp = TAILQ_NEXT(mp, mnt_list);
478 if (mac_mount_check_stat(td->td_ucred, mp) != 0) {
479 nmp = TAILQ_NEXT(mp, mnt_list);
484 nmp = TAILQ_NEXT(mp, mnt_list);
486 mtx_unlock(&mountlist_mtx);
492 * They want the entire thing.
494 * Short-circuit the corner case of no room for anything, avoids
501 mtx_lock(&mountlist_mtx);
502 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
503 if (prison_canseemount(td->td_ucred, mp) != 0) {
504 nmp = TAILQ_NEXT(mp, mnt_list);
508 if (mac_mount_check_stat(td->td_ucred, mp) != 0) {
509 nmp = TAILQ_NEXT(mp, mnt_list);
513 if (mode == MNT_WAIT) {
514 if (vfs_busy(mp, MBF_MNTLSTLOCK) != 0) {
516 * If vfs_busy() failed, and MBF_NOWAIT
517 * wasn't passed, then the mp is gone.
518 * Furthermore, because of MBF_MNTLSTLOCK,
519 * the mountlist_mtx was dropped. We have
520 * no other choice than to start over.
522 mtx_unlock(&mountlist_mtx);
523 free(tofree, M_STATFS);
527 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK) != 0) {
528 nmp = TAILQ_NEXT(mp, mnt_list);
534 * If MNT_NOWAIT is specified, do not refresh
537 if (mode != MNT_NOWAIT) {
538 error = VFS_STATFS(mp, sp);
540 mtx_lock(&mountlist_mtx);
541 nmp = TAILQ_NEXT(mp, mnt_list);
546 if (priv_check_cred_vfs_generation(td->td_ucred)) {
547 sptmp = malloc(sizeof(struct statfs), M_STATFS,
550 sptmp->f_fsid.val[0] = sptmp->f_fsid.val[1] = 0;
551 prison_enforce_statfs(td->td_ucred, mp, sptmp);
555 if (bufseg == UIO_SYSSPACE) {
556 bcopy(sp, sfsp, sizeof(*sp));
557 free(sptmp, M_STATFS);
558 } else /* if (bufseg == UIO_USERSPACE) */ {
559 error = copyout(sp, sfsp, sizeof(*sp));
560 free(sptmp, M_STATFS);
569 if (count == maxcount) {
574 mtx_lock(&mountlist_mtx);
575 nmp = TAILQ_NEXT(mp, mnt_list);
578 mtx_unlock(&mountlist_mtx);
584 #ifdef COMPAT_FREEBSD4
586 * Get old format filesystem statistics.
588 static void freebsd4_cvtstatfs(struct statfs *, struct ostatfs *);
590 #ifndef _SYS_SYSPROTO_H_
591 struct freebsd4_statfs_args {
597 freebsd4_statfs(struct thread *td, struct freebsd4_statfs_args *uap)
603 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
604 error = kern_statfs(td, uap->path, UIO_USERSPACE, sfp);
606 freebsd4_cvtstatfs(sfp, &osb);
607 error = copyout(&osb, uap->buf, sizeof(osb));
614 * Get filesystem statistics.
616 #ifndef _SYS_SYSPROTO_H_
617 struct freebsd4_fstatfs_args {
623 freebsd4_fstatfs(struct thread *td, struct freebsd4_fstatfs_args *uap)
629 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
630 error = kern_fstatfs(td, uap->fd, sfp);
632 freebsd4_cvtstatfs(sfp, &osb);
633 error = copyout(&osb, uap->buf, sizeof(osb));
640 * Get statistics on all filesystems.
642 #ifndef _SYS_SYSPROTO_H_
643 struct freebsd4_getfsstat_args {
650 freebsd4_getfsstat(struct thread *td, struct freebsd4_getfsstat_args *uap)
652 struct statfs *buf, *sp;
657 if (uap->bufsize < 0)
659 count = uap->bufsize / sizeof(struct ostatfs);
660 if (count > SIZE_MAX / sizeof(struct statfs))
662 size = count * sizeof(struct statfs);
663 error = kern_getfsstat(td, &buf, size, &count, UIO_SYSSPACE,
666 td->td_retval[0] = count;
669 while (count != 0 && error == 0) {
670 freebsd4_cvtstatfs(sp, &osb);
671 error = copyout(&osb, uap->buf, sizeof(osb));
682 * Implement fstatfs() for (NFS) file handles.
684 #ifndef _SYS_SYSPROTO_H_
685 struct freebsd4_fhstatfs_args {
686 struct fhandle *u_fhp;
691 freebsd4_fhstatfs(struct thread *td, struct freebsd4_fhstatfs_args *uap)
698 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
701 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
702 error = kern_fhstatfs(td, fh, sfp);
704 freebsd4_cvtstatfs(sfp, &osb);
705 error = copyout(&osb, uap->buf, sizeof(osb));
712 * Convert a new format statfs structure to an old format statfs structure.
715 freebsd4_cvtstatfs(struct statfs *nsp, struct ostatfs *osp)
718 statfs_scale_blocks(nsp, LONG_MAX);
719 bzero(osp, sizeof(*osp));
720 osp->f_bsize = nsp->f_bsize;
721 osp->f_iosize = MIN(nsp->f_iosize, LONG_MAX);
722 osp->f_blocks = nsp->f_blocks;
723 osp->f_bfree = nsp->f_bfree;
724 osp->f_bavail = nsp->f_bavail;
725 osp->f_files = MIN(nsp->f_files, LONG_MAX);
726 osp->f_ffree = MIN(nsp->f_ffree, LONG_MAX);
727 osp->f_owner = nsp->f_owner;
728 osp->f_type = nsp->f_type;
729 osp->f_flags = nsp->f_flags;
730 osp->f_syncwrites = MIN(nsp->f_syncwrites, LONG_MAX);
731 osp->f_asyncwrites = MIN(nsp->f_asyncwrites, LONG_MAX);
732 osp->f_syncreads = MIN(nsp->f_syncreads, LONG_MAX);
733 osp->f_asyncreads = MIN(nsp->f_asyncreads, LONG_MAX);
734 strlcpy(osp->f_fstypename, nsp->f_fstypename,
735 MIN(MFSNAMELEN, OMFSNAMELEN));
736 strlcpy(osp->f_mntonname, nsp->f_mntonname,
737 MIN(MNAMELEN, OMNAMELEN));
738 strlcpy(osp->f_mntfromname, nsp->f_mntfromname,
739 MIN(MNAMELEN, OMNAMELEN));
740 osp->f_fsid = nsp->f_fsid;
742 #endif /* COMPAT_FREEBSD4 */
744 #if defined(COMPAT_FREEBSD11)
746 * Get old format filesystem statistics.
748 static void freebsd11_cvtstatfs(struct statfs *, struct freebsd11_statfs *);
751 freebsd11_statfs(struct thread *td, struct freebsd11_statfs_args *uap)
753 struct freebsd11_statfs osb;
757 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
758 error = kern_statfs(td, uap->path, UIO_USERSPACE, sfp);
760 freebsd11_cvtstatfs(sfp, &osb);
761 error = copyout(&osb, uap->buf, sizeof(osb));
768 * Get filesystem statistics.
771 freebsd11_fstatfs(struct thread *td, struct freebsd11_fstatfs_args *uap)
773 struct freebsd11_statfs osb;
777 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
778 error = kern_fstatfs(td, uap->fd, sfp);
780 freebsd11_cvtstatfs(sfp, &osb);
781 error = copyout(&osb, uap->buf, sizeof(osb));
788 * Get statistics on all filesystems.
791 freebsd11_getfsstat(struct thread *td, struct freebsd11_getfsstat_args *uap)
793 struct freebsd11_statfs osb;
794 struct statfs *buf, *sp;
798 count = uap->bufsize / sizeof(struct ostatfs);
799 size = count * sizeof(struct statfs);
800 error = kern_getfsstat(td, &buf, size, &count, UIO_SYSSPACE,
803 td->td_retval[0] = count;
806 while (count > 0 && error == 0) {
807 freebsd11_cvtstatfs(sp, &osb);
808 error = copyout(&osb, uap->buf, sizeof(osb));
819 * Implement fstatfs() for (NFS) file handles.
822 freebsd11_fhstatfs(struct thread *td, struct freebsd11_fhstatfs_args *uap)
824 struct freebsd11_statfs osb;
829 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
832 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
833 error = kern_fhstatfs(td, fh, sfp);
835 freebsd11_cvtstatfs(sfp, &osb);
836 error = copyout(&osb, uap->buf, sizeof(osb));
843 * Convert a new format statfs structure to an old format statfs structure.
846 freebsd11_cvtstatfs(struct statfs *nsp, struct freebsd11_statfs *osp)
849 bzero(osp, sizeof(*osp));
850 osp->f_version = FREEBSD11_STATFS_VERSION;
851 osp->f_type = nsp->f_type;
852 osp->f_flags = nsp->f_flags;
853 osp->f_bsize = nsp->f_bsize;
854 osp->f_iosize = nsp->f_iosize;
855 osp->f_blocks = nsp->f_blocks;
856 osp->f_bfree = nsp->f_bfree;
857 osp->f_bavail = nsp->f_bavail;
858 osp->f_files = nsp->f_files;
859 osp->f_ffree = nsp->f_ffree;
860 osp->f_syncwrites = nsp->f_syncwrites;
861 osp->f_asyncwrites = nsp->f_asyncwrites;
862 osp->f_syncreads = nsp->f_syncreads;
863 osp->f_asyncreads = nsp->f_asyncreads;
864 osp->f_namemax = nsp->f_namemax;
865 osp->f_owner = nsp->f_owner;
866 osp->f_fsid = nsp->f_fsid;
867 strlcpy(osp->f_fstypename, nsp->f_fstypename,
868 MIN(MFSNAMELEN, sizeof(osp->f_fstypename)));
869 strlcpy(osp->f_mntonname, nsp->f_mntonname,
870 MIN(MNAMELEN, sizeof(osp->f_mntonname)));
871 strlcpy(osp->f_mntfromname, nsp->f_mntfromname,
872 MIN(MNAMELEN, sizeof(osp->f_mntfromname)));
874 #endif /* COMPAT_FREEBSD11 */
877 * Change current working directory to a given file descriptor.
879 #ifndef _SYS_SYSPROTO_H_
885 sys_fchdir(struct thread *td, struct fchdir_args *uap)
887 struct vnode *vp, *tdp;
892 AUDIT_ARG_FD(uap->fd);
893 error = getvnode_path(td, uap->fd, &cap_fchdir_rights,
900 vn_lock(vp, LK_SHARED | LK_RETRY);
901 AUDIT_ARG_VNODE1(vp);
902 error = change_dir(vp, td);
903 while (!error && (mp = vp->v_mountedhere) != NULL) {
906 error = VFS_ROOT(mp, LK_SHARED, &tdp);
923 * Change current working directory (``.'').
925 #ifndef _SYS_SYSPROTO_H_
931 sys_chdir(struct thread *td, struct chdir_args *uap)
934 return (kern_chdir(td, uap->path, UIO_USERSPACE));
938 kern_chdir(struct thread *td, const char *path, enum uio_seg pathseg)
943 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
945 if ((error = namei(&nd)) != 0)
947 if ((error = change_dir(nd.ni_vp, td)) != 0) {
952 VOP_UNLOCK(nd.ni_vp);
954 pwd_chdir(td, nd.ni_vp);
958 static int unprivileged_chroot = 0;
959 SYSCTL_INT(_security_bsd, OID_AUTO, unprivileged_chroot, CTLFLAG_RW,
960 &unprivileged_chroot, 0,
961 "Unprivileged processes can use chroot(2)");
963 * Change notion of root (``/'') directory.
965 #ifndef _SYS_SYSPROTO_H_
971 sys_chroot(struct thread *td, struct chroot_args *uap)
977 error = priv_check(td, PRIV_VFS_CHROOT);
981 if (unprivileged_chroot == 0 ||
982 (p->p_flag2 & P2_NO_NEW_PRIVS) == 0) {
988 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
989 UIO_USERSPACE, uap->path, td);
993 error = change_dir(nd.ni_vp, td);
997 error = mac_vnode_check_chroot(td->td_ucred, nd.ni_vp);
1001 VOP_UNLOCK(nd.ni_vp);
1002 error = pwd_chroot(td, nd.ni_vp);
1004 NDFREE_NOTHING(&nd);
1009 NDFREE_NOTHING(&nd);
1014 * Common routine for chroot and chdir. Callers must provide a locked vnode
1018 change_dir(struct vnode *vp, struct thread *td)
1024 ASSERT_VOP_LOCKED(vp, "change_dir(): vp not locked");
1025 if (vp->v_type != VDIR)
1028 error = mac_vnode_check_chdir(td->td_ucred, vp);
1032 return (VOP_ACCESS(vp, VEXEC, td->td_ucred, td));
1035 static __inline void
1036 flags_to_rights(int flags, cap_rights_t *rightsp)
1038 if (flags & O_EXEC) {
1039 cap_rights_set_one(rightsp, CAP_FEXECVE);
1043 switch ((flags & O_ACCMODE)) {
1045 cap_rights_set_one(rightsp, CAP_READ);
1048 cap_rights_set_one(rightsp, CAP_READ);
1051 cap_rights_set_one(rightsp, CAP_WRITE);
1052 if (!(flags & (O_APPEND | O_TRUNC)))
1053 cap_rights_set_one(rightsp, CAP_SEEK);
1058 if (flags & O_CREAT)
1059 cap_rights_set_one(rightsp, CAP_CREATE);
1061 if (flags & O_TRUNC)
1062 cap_rights_set_one(rightsp, CAP_FTRUNCATE);
1064 if (flags & (O_SYNC | O_FSYNC))
1065 cap_rights_set_one(rightsp, CAP_FSYNC);
1067 if (flags & (O_EXLOCK | O_SHLOCK))
1068 cap_rights_set_one(rightsp, CAP_FLOCK);
1072 * Check permissions, allocate an open file structure, and call the device
1073 * open routine if any.
1075 #ifndef _SYS_SYSPROTO_H_
1083 sys_open(struct thread *td, struct open_args *uap)
1086 return (kern_openat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1087 uap->flags, uap->mode));
1090 #ifndef _SYS_SYSPROTO_H_
1091 struct openat_args {
1099 sys_openat(struct thread *td, struct openat_args *uap)
1102 AUDIT_ARG_FD(uap->fd);
1103 return (kern_openat(td, uap->fd, uap->path, UIO_USERSPACE, uap->flag,
1108 kern_openat(struct thread *td, int fd, const char *path, enum uio_seg pathseg,
1109 int flags, int mode)
1111 struct proc *p = td->td_proc;
1112 struct filedesc *fdp;
1113 struct pwddesc *pdp;
1116 struct nameidata nd;
1117 cap_rights_t rights;
1118 int cmode, error, indx;
1124 AUDIT_ARG_FFLAGS(flags);
1125 AUDIT_ARG_MODE(mode);
1126 cap_rights_init_one(&rights, CAP_LOOKUP);
1127 flags_to_rights(flags, &rights);
1130 * Only one of the O_EXEC, O_RDONLY, O_WRONLY and O_RDWR flags
1131 * may be specified. On the other hand, for O_PATH any mode
1132 * except O_EXEC is ignored.
1134 if ((flags & O_PATH) != 0) {
1135 flags &= ~(O_CREAT | O_ACCMODE);
1136 } else if ((flags & O_EXEC) != 0) {
1137 if (flags & O_ACCMODE)
1139 } else if ((flags & O_ACCMODE) == O_ACCMODE) {
1142 flags = FFLAGS(flags);
1146 * Allocate a file structure. The descriptor to reference it
1147 * is allocated and used by finstall_refed() below.
1149 error = falloc_noinstall(td, &fp);
1152 /* Set the flags early so the finit in devfs can pick them up. */
1153 fp->f_flag = flags & FMASK;
1154 cmode = ((mode & ~pdp->pd_cmask) & ALLPERMS) & ~S_ISTXT;
1155 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, fd,
1157 td->td_dupfd = -1; /* XXX check for fdopen */
1158 error = vn_open(&nd, &flags, cmode, fp);
1161 * If the vn_open replaced the method vector, something
1162 * wonderous happened deep below and we just pass it up
1163 * pretending we know what we do.
1165 if (error == ENXIO && fp->f_ops != &badfileops) {
1166 MPASS((flags & O_PATH) == 0);
1171 * Handle special fdopen() case. bleh.
1173 * Don't do this for relative (capability) lookups; we don't
1174 * understand exactly what would happen, and we don't think
1175 * that it ever should.
1177 if ((nd.ni_resflags & NIRES_STRICTREL) == 0 &&
1178 (error == ENODEV || error == ENXIO) &&
1179 td->td_dupfd >= 0) {
1180 error = dupfdopen(td, fdp, td->td_dupfd, flags, error,
1189 NDFREE(&nd, NDF_ONLY_PNBUF);
1193 * Store the vnode, for any f_type. Typically, the vnode use
1194 * count is decremented by direct call to vn_closefile() for
1195 * files that switched type in the cdevsw fdopen() method.
1200 * If the file wasn't claimed by devfs bind it to the normal
1201 * vnode operations here.
1203 if (fp->f_ops == &badfileops) {
1204 KASSERT(vp->v_type != VFIFO || (flags & O_PATH) != 0,
1205 ("Unexpected fifo fp %p vp %p", fp, vp));
1206 if ((flags & O_PATH) != 0) {
1207 finit(fp, (flags & FMASK) | (fp->f_flag & FKQALLOWED),
1208 DTYPE_VNODE, NULL, &path_fileops);
1212 finit_vnode(fp, flags, NULL, &vnops);
1217 if (flags & O_TRUNC) {
1218 error = fo_truncate(fp, 0, td->td_ucred, td);
1224 * If we haven't already installed the FD (for dupfdopen), do so now.
1227 struct filecaps *fcaps;
1230 if ((nd.ni_resflags & NIRES_STRICTREL) != 0)
1231 fcaps = &nd.ni_filecaps;
1235 error = finstall_refed(td, fp, &indx, flags, fcaps);
1236 /* On success finstall_refed() consumes fcaps. */
1238 filecaps_free(&nd.ni_filecaps);
1242 filecaps_free(&nd.ni_filecaps);
1243 falloc_abort(td, fp);
1246 td->td_retval[0] = indx;
1249 KASSERT(indx == -1, ("indx=%d, should be -1", indx));
1250 falloc_abort(td, fp);
1258 #ifndef _SYS_SYSPROTO_H_
1259 struct ocreat_args {
1265 ocreat(struct thread *td, struct ocreat_args *uap)
1268 return (kern_openat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1269 O_WRONLY | O_CREAT | O_TRUNC, uap->mode));
1271 #endif /* COMPAT_43 */
1274 * Create a special file.
1276 #ifndef _SYS_SYSPROTO_H_
1277 struct mknodat_args {
1285 sys_mknodat(struct thread *td, struct mknodat_args *uap)
1288 return (kern_mknodat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode,
1292 #if defined(COMPAT_FREEBSD11)
1294 freebsd11_mknod(struct thread *td,
1295 struct freebsd11_mknod_args *uap)
1298 return (kern_mknodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1299 uap->mode, uap->dev));
1303 freebsd11_mknodat(struct thread *td,
1304 struct freebsd11_mknodat_args *uap)
1307 return (kern_mknodat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode,
1310 #endif /* COMPAT_FREEBSD11 */
1313 kern_mknodat(struct thread *td, int fd, const char *path, enum uio_seg pathseg,
1314 int mode, dev_t dev)
1319 struct nameidata nd;
1320 int error, whiteout = 0;
1322 AUDIT_ARG_MODE(mode);
1324 switch (mode & S_IFMT) {
1327 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
1328 if (error == 0 && dev == VNOVAL)
1332 error = priv_check(td, PRIV_VFS_MKNOD_WHT);
1336 return (kern_mkfifoat(td, fd, path, pathseg, mode));
1347 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
1348 NOCACHE, pathseg, path, fd, &cap_mknodat_rights,
1350 if ((error = namei(&nd)) != 0)
1354 NDFREE(&nd, NDF_ONLY_PNBUF);
1355 if (vp == nd.ni_dvp)
1363 vattr.va_mode = (mode & ALLPERMS) &
1364 ~td->td_proc->p_pd->pd_cmask;
1365 vattr.va_rdev = dev;
1368 switch (mode & S_IFMT) {
1370 vattr.va_type = VCHR;
1373 vattr.va_type = VBLK;
1379 panic("kern_mknod: invalid mode");
1382 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1383 NDFREE(&nd, NDF_ONLY_PNBUF);
1385 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1390 if (error == 0 && !whiteout)
1391 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp,
1392 &nd.ni_cnd, &vattr);
1396 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, CREATE);
1398 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp,
1399 &nd.ni_cnd, &vattr);
1402 VOP_VPUT_PAIR(nd.ni_dvp, error == 0 && !whiteout ? &nd.ni_vp : NULL,
1404 vn_finished_write(mp);
1405 NDFREE(&nd, NDF_ONLY_PNBUF);
1406 if (error == ERELOOKUP)
1412 * Create a named pipe.
1414 #ifndef _SYS_SYSPROTO_H_
1415 struct mkfifo_args {
1421 sys_mkfifo(struct thread *td, struct mkfifo_args *uap)
1424 return (kern_mkfifoat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1428 #ifndef _SYS_SYSPROTO_H_
1429 struct mkfifoat_args {
1436 sys_mkfifoat(struct thread *td, struct mkfifoat_args *uap)
1439 return (kern_mkfifoat(td, uap->fd, uap->path, UIO_USERSPACE,
1444 kern_mkfifoat(struct thread *td, int fd, const char *path,
1445 enum uio_seg pathseg, int mode)
1449 struct nameidata nd;
1452 AUDIT_ARG_MODE(mode);
1456 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
1457 NOCACHE, pathseg, path, fd, &cap_mkfifoat_rights,
1459 if ((error = namei(&nd)) != 0)
1461 if (nd.ni_vp != NULL) {
1462 NDFREE(&nd, NDF_ONLY_PNBUF);
1463 if (nd.ni_vp == nd.ni_dvp)
1470 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1471 NDFREE(&nd, NDF_ONLY_PNBUF);
1473 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1478 vattr.va_type = VFIFO;
1479 vattr.va_mode = (mode & ALLPERMS) & ~td->td_proc->p_pd->pd_cmask;
1481 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1486 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
1490 VOP_VPUT_PAIR(nd.ni_dvp, error == 0 ? &nd.ni_vp : NULL, true);
1491 vn_finished_write(mp);
1492 NDFREE(&nd, NDF_ONLY_PNBUF);
1493 if (error == ERELOOKUP)
1499 * Make a hard file link.
1501 #ifndef _SYS_SYSPROTO_H_
1508 sys_link(struct thread *td, struct link_args *uap)
1511 return (kern_linkat(td, AT_FDCWD, AT_FDCWD, uap->path, uap->link,
1512 UIO_USERSPACE, AT_SYMLINK_FOLLOW));
1515 #ifndef _SYS_SYSPROTO_H_
1516 struct linkat_args {
1525 sys_linkat(struct thread *td, struct linkat_args *uap)
1530 if ((flag & ~(AT_SYMLINK_FOLLOW | AT_RESOLVE_BENEATH |
1531 AT_EMPTY_PATH)) != 0)
1534 return (kern_linkat(td, uap->fd1, uap->fd2, uap->path1, uap->path2,
1535 UIO_USERSPACE, flag));
1538 int hardlink_check_uid = 0;
1539 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
1540 &hardlink_check_uid, 0,
1541 "Unprivileged processes cannot create hard links to files owned by other "
1543 static int hardlink_check_gid = 0;
1544 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
1545 &hardlink_check_gid, 0,
1546 "Unprivileged processes cannot create hard links to files owned by other "
1550 can_hardlink(struct vnode *vp, struct ucred *cred)
1555 if (!hardlink_check_uid && !hardlink_check_gid)
1558 error = VOP_GETATTR(vp, &va, cred);
1562 if (hardlink_check_uid && cred->cr_uid != va.va_uid) {
1563 error = priv_check_cred(cred, PRIV_VFS_LINK);
1568 if (hardlink_check_gid && !groupmember(va.va_gid, cred)) {
1569 error = priv_check_cred(cred, PRIV_VFS_LINK);
1578 kern_linkat(struct thread *td, int fd1, int fd2, const char *path1,
1579 const char *path2, enum uio_seg segflag, int flag)
1581 struct nameidata nd;
1587 NDINIT_ATRIGHTS(&nd, LOOKUP, AUDITVNODE1 | at2cnpflags(flag,
1588 AT_SYMLINK_FOLLOW | AT_RESOLVE_BENEATH | AT_EMPTY_PATH),
1589 segflag, path1, fd1, &cap_linkat_source_rights, td);
1590 if ((error = namei(&nd)) != 0)
1592 NDFREE(&nd, NDF_ONLY_PNBUF);
1593 if ((nd.ni_resflags & NIRES_EMPTYPATH) != 0) {
1594 error = priv_check(td, PRIV_VFS_FHOPEN);
1600 error = kern_linkat_vp(td, nd.ni_vp, fd2, path2, segflag);
1601 } while (error == EAGAIN || error == ERELOOKUP);
1606 kern_linkat_vp(struct thread *td, struct vnode *vp, int fd, const char *path,
1607 enum uio_seg segflag)
1609 struct nameidata nd;
1613 if (vp->v_type == VDIR) {
1615 return (EPERM); /* POSIX */
1617 NDINIT_ATRIGHTS(&nd, CREATE,
1618 LOCKPARENT | SAVENAME | AUDITVNODE2 | NOCACHE, segflag, path, fd,
1619 &cap_linkat_target_rights, td);
1620 if ((error = namei(&nd)) == 0) {
1621 if (nd.ni_vp != NULL) {
1622 NDFREE(&nd, NDF_ONLY_PNBUF);
1623 if (nd.ni_dvp == nd.ni_vp)
1630 } else if (nd.ni_dvp->v_mount != vp->v_mount) {
1632 * Cross-device link. No need to recheck
1633 * vp->v_type, since it cannot change, except
1636 NDFREE(&nd, NDF_ONLY_PNBUF);
1640 } else if ((error = vn_lock(vp, LK_EXCLUSIVE)) == 0) {
1641 error = can_hardlink(vp, td->td_ucred);
1644 error = mac_vnode_check_link(td->td_ucred,
1645 nd.ni_dvp, vp, &nd.ni_cnd);
1650 NDFREE(&nd, NDF_ONLY_PNBUF);
1653 error = vn_start_write(vp, &mp, V_NOWAIT);
1657 NDFREE(&nd, NDF_ONLY_PNBUF);
1658 error = vn_start_write(NULL, &mp,
1664 error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
1665 VOP_VPUT_PAIR(nd.ni_dvp, &vp, true);
1666 vn_finished_write(mp);
1667 NDFREE(&nd, NDF_ONLY_PNBUF);
1671 NDFREE(&nd, NDF_ONLY_PNBUF);
1682 * Make a symbolic link.
1684 #ifndef _SYS_SYSPROTO_H_
1685 struct symlink_args {
1691 sys_symlink(struct thread *td, struct symlink_args *uap)
1694 return (kern_symlinkat(td, uap->path, AT_FDCWD, uap->link,
1698 #ifndef _SYS_SYSPROTO_H_
1699 struct symlinkat_args {
1706 sys_symlinkat(struct thread *td, struct symlinkat_args *uap)
1709 return (kern_symlinkat(td, uap->path1, uap->fd, uap->path2,
1714 kern_symlinkat(struct thread *td, const char *path1, int fd, const char *path2,
1715 enum uio_seg segflg)
1719 const char *syspath;
1721 struct nameidata nd;
1724 if (segflg == UIO_SYSSPACE) {
1727 tmppath = uma_zalloc(namei_zone, M_WAITOK);
1728 if ((error = copyinstr(path1, tmppath, MAXPATHLEN, NULL)) != 0)
1732 AUDIT_ARG_TEXT(syspath);
1736 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
1737 NOCACHE, segflg, path2, fd, &cap_symlinkat_rights,
1739 if ((error = namei(&nd)) != 0)
1742 NDFREE(&nd, NDF_ONLY_PNBUF);
1743 if (nd.ni_vp == nd.ni_dvp)
1752 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1753 NDFREE(&nd, NDF_ONLY_PNBUF);
1755 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1760 vattr.va_mode = ACCESSPERMS &~ td->td_proc->p_pd->pd_cmask;
1762 vattr.va_type = VLNK;
1763 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1768 error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr, syspath);
1772 VOP_VPUT_PAIR(nd.ni_dvp, error == 0 ? &nd.ni_vp : NULL, true);
1773 vn_finished_write(mp);
1774 NDFREE(&nd, NDF_ONLY_PNBUF);
1775 if (error == ERELOOKUP)
1778 if (segflg != UIO_SYSSPACE)
1779 uma_zfree(namei_zone, tmppath);
1784 * Delete a whiteout from the filesystem.
1786 #ifndef _SYS_SYSPROTO_H_
1787 struct undelete_args {
1792 sys_undelete(struct thread *td, struct undelete_args *uap)
1795 struct nameidata nd;
1801 NDINIT(&nd, DELETE, LOCKPARENT | DOWHITEOUT | AUDITVNODE1,
1802 UIO_USERSPACE, uap->path, td);
1807 if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & ISWHITEOUT)) {
1808 NDFREE(&nd, NDF_ONLY_PNBUF);
1809 if (nd.ni_vp == nd.ni_dvp)
1817 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1818 NDFREE(&nd, NDF_ONLY_PNBUF);
1820 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1824 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, DELETE);
1825 NDFREE(&nd, NDF_ONLY_PNBUF);
1827 vn_finished_write(mp);
1828 if (error == ERELOOKUP)
1834 * Delete a name from the filesystem.
1836 #ifndef _SYS_SYSPROTO_H_
1837 struct unlink_args {
1842 sys_unlink(struct thread *td, struct unlink_args *uap)
1845 return (kern_funlinkat(td, AT_FDCWD, uap->path, FD_NONE, UIO_USERSPACE,
1850 kern_funlinkat_ex(struct thread *td, int dfd, const char *path, int fd,
1851 int flag, enum uio_seg pathseg, ino_t oldinum)
1854 if ((flag & ~(AT_REMOVEDIR | AT_RESOLVE_BENEATH)) != 0)
1857 if ((flag & AT_REMOVEDIR) != 0)
1858 return (kern_frmdirat(td, dfd, path, fd, UIO_USERSPACE, 0));
1860 return (kern_funlinkat(td, dfd, path, fd, UIO_USERSPACE, 0, 0));
1863 #ifndef _SYS_SYSPROTO_H_
1864 struct unlinkat_args {
1871 sys_unlinkat(struct thread *td, struct unlinkat_args *uap)
1874 return (kern_funlinkat_ex(td, uap->fd, uap->path, FD_NONE, uap->flag,
1878 #ifndef _SYS_SYSPROTO_H_
1879 struct funlinkat_args {
1887 sys_funlinkat(struct thread *td, struct funlinkat_args *uap)
1890 return (kern_funlinkat_ex(td, uap->dfd, uap->path, uap->fd, uap->flag,
1895 kern_funlinkat(struct thread *td, int dfd, const char *path, int fd,
1896 enum uio_seg pathseg, int flag, ino_t oldinum)
1901 struct nameidata nd;
1906 if (fd != FD_NONE) {
1907 error = getvnode_path(td, fd, &cap_no_rights, &fp);
1915 NDINIT_ATRIGHTS(&nd, DELETE, LOCKPARENT | LOCKLEAF | AUDITVNODE1 |
1916 at2cnpflags(flag, AT_RESOLVE_BENEATH),
1917 pathseg, path, dfd, &cap_unlinkat_rights, td);
1918 if ((error = namei(&nd)) != 0) {
1919 if (error == EINVAL)
1924 if (vp->v_type == VDIR && oldinum == 0) {
1925 error = EPERM; /* POSIX */
1926 } else if (oldinum != 0 &&
1927 ((error = VOP_STAT(vp, &sb, td->td_ucred, NOCRED, td)) == 0) &&
1928 sb.st_ino != oldinum) {
1929 error = EIDRM; /* Identifier removed */
1930 } else if (fp != NULL && fp->f_vnode != vp) {
1931 if (VN_IS_DOOMED(fp->f_vnode))
1937 * The root of a mounted filesystem cannot be deleted.
1939 * XXX: can this only be a VDIR case?
1941 if (vp->v_vflag & VV_ROOT)
1945 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1946 NDFREE(&nd, NDF_ONLY_PNBUF);
1948 if (vp == nd.ni_dvp)
1952 if ((error = vn_start_write(NULL, &mp,
1953 V_XSLEEP | PCATCH)) != 0) {
1959 error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp,
1964 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_UNLINK);
1965 error = VOP_REMOVE(nd.ni_dvp, vp, &nd.ni_cnd);
1969 vn_finished_write(mp);
1971 NDFREE(&nd, NDF_ONLY_PNBUF);
1973 if (vp == nd.ni_dvp)
1977 if (error == ERELOOKUP)
1986 * Reposition read/write file offset.
1988 #ifndef _SYS_SYSPROTO_H_
1997 sys_lseek(struct thread *td, struct lseek_args *uap)
2000 return (kern_lseek(td, uap->fd, uap->offset, uap->whence));
2004 kern_lseek(struct thread *td, int fd, off_t offset, int whence)
2010 error = fget(td, fd, &cap_seek_rights, &fp);
2013 error = (fp->f_ops->fo_flags & DFLAG_SEEKABLE) != 0 ?
2014 fo_seek(fp, offset, whence, td) : ESPIPE;
2019 #if defined(COMPAT_43)
2021 * Reposition read/write file offset.
2023 #ifndef _SYS_SYSPROTO_H_
2024 struct olseek_args {
2031 olseek(struct thread *td, struct olseek_args *uap)
2034 return (kern_lseek(td, uap->fd, uap->offset, uap->whence));
2036 #endif /* COMPAT_43 */
2038 #if defined(COMPAT_FREEBSD6)
2039 /* Version with the 'pad' argument */
2041 freebsd6_lseek(struct thread *td, struct freebsd6_lseek_args *uap)
2044 return (kern_lseek(td, uap->fd, uap->offset, uap->whence));
2049 * Check access permissions using passed credentials.
2052 vn_access(struct vnode *vp, int user_flags, struct ucred *cred,
2058 /* Flags == 0 means only check for existence. */
2059 if (user_flags == 0)
2063 if (user_flags & R_OK)
2065 if (user_flags & W_OK)
2067 if (user_flags & X_OK)
2070 error = mac_vnode_check_access(cred, vp, accmode);
2074 if ((accmode & VWRITE) == 0 || (error = vn_writechk(vp)) == 0)
2075 error = VOP_ACCESS(vp, accmode, cred, td);
2080 * Check access permissions using "real" credentials.
2082 #ifndef _SYS_SYSPROTO_H_
2083 struct access_args {
2089 sys_access(struct thread *td, struct access_args *uap)
2092 return (kern_accessat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2096 #ifndef _SYS_SYSPROTO_H_
2097 struct faccessat_args {
2105 sys_faccessat(struct thread *td, struct faccessat_args *uap)
2108 return (kern_accessat(td, uap->fd, uap->path, UIO_USERSPACE, uap->flag,
2113 kern_accessat(struct thread *td, int fd, const char *path,
2114 enum uio_seg pathseg, int flag, int amode)
2116 struct ucred *cred, *usecred;
2118 struct nameidata nd;
2121 if ((flag & ~(AT_EACCESS | AT_RESOLVE_BENEATH | AT_EMPTY_PATH)) != 0)
2123 if (amode != F_OK && (amode & ~(R_OK | W_OK | X_OK)) != 0)
2127 * Create and modify a temporary credential instead of one that
2128 * is potentially shared (if we need one).
2130 cred = td->td_ucred;
2131 if ((flag & AT_EACCESS) == 0 &&
2132 ((cred->cr_uid != cred->cr_ruid ||
2133 cred->cr_rgid != cred->cr_groups[0]))) {
2134 usecred = crdup(cred);
2135 usecred->cr_uid = cred->cr_ruid;
2136 usecred->cr_groups[0] = cred->cr_rgid;
2137 td->td_ucred = usecred;
2140 AUDIT_ARG_VALUE(amode);
2141 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF |
2142 AUDITVNODE1 | at2cnpflags(flag, AT_RESOLVE_BENEATH |
2143 AT_EMPTY_PATH), pathseg, path, fd, &cap_fstat_rights, td);
2144 if ((error = namei(&nd)) != 0)
2148 error = vn_access(vp, amode, usecred, td);
2149 NDFREE_NOTHING(&nd);
2152 if (usecred != cred) {
2153 td->td_ucred = cred;
2160 * Check access permissions using "effective" credentials.
2162 #ifndef _SYS_SYSPROTO_H_
2163 struct eaccess_args {
2169 sys_eaccess(struct thread *td, struct eaccess_args *uap)
2172 return (kern_accessat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2173 AT_EACCESS, uap->amode));
2176 #if defined(COMPAT_43)
2178 * Get file status; this version follows links.
2180 #ifndef _SYS_SYSPROTO_H_
2187 ostat(struct thread *td, struct ostat_args *uap)
2193 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
2198 return (copyout(&osb, uap->ub, sizeof (osb)));
2202 * Get file status; this version does not follow links.
2204 #ifndef _SYS_SYSPROTO_H_
2205 struct olstat_args {
2211 olstat(struct thread *td, struct olstat_args *uap)
2217 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2218 UIO_USERSPACE, &sb, NULL);
2222 return (copyout(&osb, uap->ub, sizeof (osb)));
2226 * Convert from an old to a new stat structure.
2227 * XXX: many values are blindly truncated.
2230 cvtstat(struct stat *st, struct ostat *ost)
2233 bzero(ost, sizeof(*ost));
2234 ost->st_dev = st->st_dev;
2235 ost->st_ino = st->st_ino;
2236 ost->st_mode = st->st_mode;
2237 ost->st_nlink = st->st_nlink;
2238 ost->st_uid = st->st_uid;
2239 ost->st_gid = st->st_gid;
2240 ost->st_rdev = st->st_rdev;
2241 ost->st_size = MIN(st->st_size, INT32_MAX);
2242 ost->st_atim = st->st_atim;
2243 ost->st_mtim = st->st_mtim;
2244 ost->st_ctim = st->st_ctim;
2245 ost->st_blksize = st->st_blksize;
2246 ost->st_blocks = st->st_blocks;
2247 ost->st_flags = st->st_flags;
2248 ost->st_gen = st->st_gen;
2250 #endif /* COMPAT_43 */
2252 #if defined(COMPAT_43) || defined(COMPAT_FREEBSD11)
2253 int ino64_trunc_error;
2254 SYSCTL_INT(_vfs, OID_AUTO, ino64_trunc_error, CTLFLAG_RW,
2255 &ino64_trunc_error, 0,
2256 "Error on truncation of device, file or inode number, or link count");
2259 freebsd11_cvtstat(struct stat *st, struct freebsd11_stat *ost)
2262 ost->st_dev = st->st_dev;
2263 if (ost->st_dev != st->st_dev) {
2264 switch (ino64_trunc_error) {
2267 * Since dev_t is almost raw, don't clamp to the
2268 * maximum for case 2, but ignore the error.
2275 ost->st_ino = st->st_ino;
2276 if (ost->st_ino != st->st_ino) {
2277 switch (ino64_trunc_error) {
2284 ost->st_ino = UINT32_MAX;
2288 ost->st_mode = st->st_mode;
2289 ost->st_nlink = st->st_nlink;
2290 if (ost->st_nlink != st->st_nlink) {
2291 switch (ino64_trunc_error) {
2298 ost->st_nlink = UINT16_MAX;
2302 ost->st_uid = st->st_uid;
2303 ost->st_gid = st->st_gid;
2304 ost->st_rdev = st->st_rdev;
2305 if (ost->st_rdev != st->st_rdev) {
2306 switch (ino64_trunc_error) {
2313 ost->st_atim = st->st_atim;
2314 ost->st_mtim = st->st_mtim;
2315 ost->st_ctim = st->st_ctim;
2316 ost->st_size = st->st_size;
2317 ost->st_blocks = st->st_blocks;
2318 ost->st_blksize = st->st_blksize;
2319 ost->st_flags = st->st_flags;
2320 ost->st_gen = st->st_gen;
2322 ost->st_birthtim = st->st_birthtim;
2323 bzero((char *)&ost->st_birthtim + sizeof(ost->st_birthtim),
2324 sizeof(*ost) - offsetof(struct freebsd11_stat,
2325 st_birthtim) - sizeof(ost->st_birthtim));
2330 freebsd11_stat(struct thread *td, struct freebsd11_stat_args* uap)
2333 struct freebsd11_stat osb;
2336 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
2340 error = freebsd11_cvtstat(&sb, &osb);
2342 error = copyout(&osb, uap->ub, sizeof(osb));
2347 freebsd11_lstat(struct thread *td, struct freebsd11_lstat_args* uap)
2350 struct freebsd11_stat osb;
2353 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2354 UIO_USERSPACE, &sb, NULL);
2357 error = freebsd11_cvtstat(&sb, &osb);
2359 error = copyout(&osb, uap->ub, sizeof(osb));
2364 freebsd11_fhstat(struct thread *td, struct freebsd11_fhstat_args* uap)
2368 struct freebsd11_stat osb;
2371 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
2374 error = kern_fhstat(td, fh, &sb);
2377 error = freebsd11_cvtstat(&sb, &osb);
2379 error = copyout(&osb, uap->sb, sizeof(osb));
2384 freebsd11_fstatat(struct thread *td, struct freebsd11_fstatat_args* uap)
2387 struct freebsd11_stat osb;
2390 error = kern_statat(td, uap->flag, uap->fd, uap->path,
2391 UIO_USERSPACE, &sb, NULL);
2394 error = freebsd11_cvtstat(&sb, &osb);
2396 error = copyout(&osb, uap->buf, sizeof(osb));
2399 #endif /* COMPAT_FREEBSD11 */
2404 #ifndef _SYS_SYSPROTO_H_
2405 struct fstatat_args {
2413 sys_fstatat(struct thread *td, struct fstatat_args *uap)
2418 error = kern_statat(td, uap->flag, uap->fd, uap->path,
2419 UIO_USERSPACE, &sb, NULL);
2421 error = copyout(&sb, uap->buf, sizeof (sb));
2426 kern_statat(struct thread *td, int flag, int fd, const char *path,
2427 enum uio_seg pathseg, struct stat *sbp,
2428 void (*hook)(struct vnode *vp, struct stat *sbp))
2430 struct nameidata nd;
2433 if ((flag & ~(AT_SYMLINK_NOFOLLOW | AT_RESOLVE_BENEATH |
2434 AT_EMPTY_PATH)) != 0)
2437 NDINIT_ATRIGHTS(&nd, LOOKUP, at2cnpflags(flag, AT_RESOLVE_BENEATH |
2438 AT_SYMLINK_NOFOLLOW | AT_EMPTY_PATH) | LOCKSHARED | LOCKLEAF |
2439 AUDITVNODE1, pathseg, path, fd, &cap_fstat_rights, td);
2441 if ((error = namei(&nd)) != 0) {
2442 if (error == ENOTDIR &&
2443 (nd.ni_resflags & NIRES_EMPTYPATH) != 0)
2444 error = kern_fstat(td, fd, sbp);
2447 error = VOP_STAT(nd.ni_vp, sbp, td->td_ucred, NOCRED, td);
2449 if (__predict_false(hook != NULL))
2450 hook(nd.ni_vp, sbp);
2452 NDFREE_NOTHING(&nd);
2454 #ifdef __STAT_TIME_T_EXT
2455 sbp->st_atim_ext = 0;
2456 sbp->st_mtim_ext = 0;
2457 sbp->st_ctim_ext = 0;
2458 sbp->st_btim_ext = 0;
2461 if (KTRPOINT(td, KTR_STRUCT))
2462 ktrstat_error(sbp, error);
2467 #if defined(COMPAT_FREEBSD11)
2469 * Implementation of the NetBSD [l]stat() functions.
2472 freebsd11_cvtnstat(struct stat *sb, struct nstat *nsb)
2475 bzero(nsb, sizeof(*nsb));
2476 nsb->st_dev = sb->st_dev;
2477 nsb->st_ino = sb->st_ino;
2478 nsb->st_mode = sb->st_mode;
2479 nsb->st_nlink = sb->st_nlink;
2480 nsb->st_uid = sb->st_uid;
2481 nsb->st_gid = sb->st_gid;
2482 nsb->st_rdev = sb->st_rdev;
2483 nsb->st_atim = sb->st_atim;
2484 nsb->st_mtim = sb->st_mtim;
2485 nsb->st_ctim = sb->st_ctim;
2486 nsb->st_size = sb->st_size;
2487 nsb->st_blocks = sb->st_blocks;
2488 nsb->st_blksize = sb->st_blksize;
2489 nsb->st_flags = sb->st_flags;
2490 nsb->st_gen = sb->st_gen;
2491 nsb->st_birthtim = sb->st_birthtim;
2494 #ifndef _SYS_SYSPROTO_H_
2495 struct freebsd11_nstat_args {
2501 freebsd11_nstat(struct thread *td, struct freebsd11_nstat_args *uap)
2507 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
2511 freebsd11_cvtnstat(&sb, &nsb);
2512 return (copyout(&nsb, uap->ub, sizeof (nsb)));
2516 * NetBSD lstat. Get file status; this version does not follow links.
2518 #ifndef _SYS_SYSPROTO_H_
2519 struct freebsd11_nlstat_args {
2525 freebsd11_nlstat(struct thread *td, struct freebsd11_nlstat_args *uap)
2531 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2532 UIO_USERSPACE, &sb, NULL);
2535 freebsd11_cvtnstat(&sb, &nsb);
2536 return (copyout(&nsb, uap->ub, sizeof (nsb)));
2538 #endif /* COMPAT_FREEBSD11 */
2541 * Get configurable pathname variables.
2543 #ifndef _SYS_SYSPROTO_H_
2544 struct pathconf_args {
2550 sys_pathconf(struct thread *td, struct pathconf_args *uap)
2555 error = kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name, FOLLOW,
2558 td->td_retval[0] = value;
2562 #ifndef _SYS_SYSPROTO_H_
2563 struct lpathconf_args {
2569 sys_lpathconf(struct thread *td, struct lpathconf_args *uap)
2574 error = kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name,
2577 td->td_retval[0] = value;
2582 kern_pathconf(struct thread *td, const char *path, enum uio_seg pathseg,
2583 int name, u_long flags, long *valuep)
2585 struct nameidata nd;
2588 NDINIT(&nd, LOOKUP, LOCKSHARED | LOCKLEAF | AUDITVNODE1 | flags,
2590 if ((error = namei(&nd)) != 0)
2592 NDFREE_NOTHING(&nd);
2594 error = VOP_PATHCONF(nd.ni_vp, name, valuep);
2600 * Return target name of a symbolic link.
2602 #ifndef _SYS_SYSPROTO_H_
2603 struct readlink_args {
2610 sys_readlink(struct thread *td, struct readlink_args *uap)
2613 return (kern_readlinkat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2614 uap->buf, UIO_USERSPACE, uap->count));
2616 #ifndef _SYS_SYSPROTO_H_
2617 struct readlinkat_args {
2625 sys_readlinkat(struct thread *td, struct readlinkat_args *uap)
2628 return (kern_readlinkat(td, uap->fd, uap->path, UIO_USERSPACE,
2629 uap->buf, UIO_USERSPACE, uap->bufsize));
2633 kern_readlinkat(struct thread *td, int fd, const char *path,
2634 enum uio_seg pathseg, char *buf, enum uio_seg bufseg, size_t count)
2637 struct nameidata nd;
2640 if (count > IOSIZE_MAX)
2643 NDINIT_AT(&nd, LOOKUP, NOFOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
2644 pathseg, path, fd, td);
2646 if ((error = namei(&nd)) != 0)
2648 NDFREE_NOTHING(&nd);
2651 error = kern_readlink_vp(vp, buf, bufseg, count, td);
2658 * Helper function to readlink from a vnode
2661 kern_readlink_vp(struct vnode *vp, char *buf, enum uio_seg bufseg, size_t count,
2668 ASSERT_VOP_LOCKED(vp, "kern_readlink_vp(): vp not locked");
2670 error = mac_vnode_check_readlink(td->td_ucred, vp);
2674 if (vp->v_type != VLNK && (vp->v_vflag & VV_READLINK) == 0)
2677 aiov.iov_base = buf;
2678 aiov.iov_len = count;
2679 auio.uio_iov = &aiov;
2680 auio.uio_iovcnt = 1;
2681 auio.uio_offset = 0;
2682 auio.uio_rw = UIO_READ;
2683 auio.uio_segflg = bufseg;
2685 auio.uio_resid = count;
2686 error = VOP_READLINK(vp, &auio, td->td_ucred);
2687 td->td_retval[0] = count - auio.uio_resid;
2692 * Common implementation code for chflags() and fchflags().
2695 setfflags(struct thread *td, struct vnode *vp, u_long flags)
2701 /* We can't support the value matching VNOVAL. */
2702 if (flags == VNOVAL)
2703 return (EOPNOTSUPP);
2706 * Prevent non-root users from setting flags on devices. When
2707 * a device is reused, users can retain ownership of the device
2708 * if they are allowed to set flags and programs assume that
2709 * chown can't fail when done as root.
2711 if (vp->v_type == VCHR || vp->v_type == VBLK) {
2712 error = priv_check(td, PRIV_VFS_CHFLAGS_DEV);
2717 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2720 vattr.va_flags = flags;
2721 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2723 error = mac_vnode_check_setflags(td->td_ucred, vp, vattr.va_flags);
2726 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
2728 vn_finished_write(mp);
2733 * Change flags of a file given a path name.
2735 #ifndef _SYS_SYSPROTO_H_
2736 struct chflags_args {
2742 sys_chflags(struct thread *td, struct chflags_args *uap)
2745 return (kern_chflagsat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2749 #ifndef _SYS_SYSPROTO_H_
2750 struct chflagsat_args {
2758 sys_chflagsat(struct thread *td, struct chflagsat_args *uap)
2761 if ((uap->atflag & ~(AT_SYMLINK_NOFOLLOW | AT_RESOLVE_BENEATH |
2762 AT_EMPTY_PATH)) != 0)
2765 return (kern_chflagsat(td, uap->fd, uap->path, UIO_USERSPACE,
2766 uap->flags, uap->atflag));
2770 * Same as chflags() but doesn't follow symlinks.
2772 #ifndef _SYS_SYSPROTO_H_
2773 struct lchflags_args {
2779 sys_lchflags(struct thread *td, struct lchflags_args *uap)
2782 return (kern_chflagsat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2783 uap->flags, AT_SYMLINK_NOFOLLOW));
2787 kern_chflagsat(struct thread *td, int fd, const char *path,
2788 enum uio_seg pathseg, u_long flags, int atflag)
2790 struct nameidata nd;
2793 AUDIT_ARG_FFLAGS(flags);
2794 NDINIT_ATRIGHTS(&nd, LOOKUP, at2cnpflags(atflag, AT_SYMLINK_NOFOLLOW |
2795 AT_RESOLVE_BENEATH | AT_EMPTY_PATH) | AUDITVNODE1, pathseg, path,
2796 fd, &cap_fchflags_rights, td);
2797 if ((error = namei(&nd)) != 0)
2799 NDFREE_NOTHING(&nd);
2800 error = setfflags(td, nd.ni_vp, flags);
2806 * Change flags of a file given a file descriptor.
2808 #ifndef _SYS_SYSPROTO_H_
2809 struct fchflags_args {
2815 sys_fchflags(struct thread *td, struct fchflags_args *uap)
2820 AUDIT_ARG_FD(uap->fd);
2821 AUDIT_ARG_FFLAGS(uap->flags);
2822 error = getvnode(td, uap->fd, &cap_fchflags_rights,
2827 if (AUDITING_TD(td)) {
2828 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
2829 AUDIT_ARG_VNODE1(fp->f_vnode);
2830 VOP_UNLOCK(fp->f_vnode);
2833 error = setfflags(td, fp->f_vnode, uap->flags);
2839 * Common implementation code for chmod(), lchmod() and fchmod().
2842 setfmode(struct thread *td, struct ucred *cred, struct vnode *vp, int mode)
2848 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2850 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2852 vattr.va_mode = mode & ALLPERMS;
2854 error = mac_vnode_check_setmode(cred, vp, vattr.va_mode);
2857 error = VOP_SETATTR(vp, &vattr, cred);
2859 vn_finished_write(mp);
2864 * Change mode of a file given path name.
2866 #ifndef _SYS_SYSPROTO_H_
2873 sys_chmod(struct thread *td, struct chmod_args *uap)
2876 return (kern_fchmodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2880 #ifndef _SYS_SYSPROTO_H_
2881 struct fchmodat_args {
2889 sys_fchmodat(struct thread *td, struct fchmodat_args *uap)
2892 if ((uap->flag & ~(AT_SYMLINK_NOFOLLOW | AT_RESOLVE_BENEATH |
2893 AT_EMPTY_PATH)) != 0)
2896 return (kern_fchmodat(td, uap->fd, uap->path, UIO_USERSPACE,
2897 uap->mode, uap->flag));
2901 * Change mode of a file given path name (don't follow links.)
2903 #ifndef _SYS_SYSPROTO_H_
2904 struct lchmod_args {
2910 sys_lchmod(struct thread *td, struct lchmod_args *uap)
2913 return (kern_fchmodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2914 uap->mode, AT_SYMLINK_NOFOLLOW));
2918 kern_fchmodat(struct thread *td, int fd, const char *path,
2919 enum uio_seg pathseg, mode_t mode, int flag)
2921 struct nameidata nd;
2924 AUDIT_ARG_MODE(mode);
2925 NDINIT_ATRIGHTS(&nd, LOOKUP, at2cnpflags(flag, AT_SYMLINK_NOFOLLOW |
2926 AT_RESOLVE_BENEATH | AT_EMPTY_PATH) | AUDITVNODE1, pathseg, path,
2927 fd, &cap_fchmod_rights, td);
2928 if ((error = namei(&nd)) != 0)
2930 NDFREE_NOTHING(&nd);
2931 error = setfmode(td, td->td_ucred, nd.ni_vp, mode);
2937 * Change mode of a file given a file descriptor.
2939 #ifndef _SYS_SYSPROTO_H_
2940 struct fchmod_args {
2946 sys_fchmod(struct thread *td, struct fchmod_args *uap)
2951 AUDIT_ARG_FD(uap->fd);
2952 AUDIT_ARG_MODE(uap->mode);
2954 error = fget(td, uap->fd, &cap_fchmod_rights, &fp);
2957 error = fo_chmod(fp, uap->mode, td->td_ucred, td);
2963 * Common implementation for chown(), lchown(), and fchown()
2966 setfown(struct thread *td, struct ucred *cred, struct vnode *vp, uid_t uid,
2973 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2975 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2980 error = mac_vnode_check_setowner(cred, vp, vattr.va_uid,
2984 error = VOP_SETATTR(vp, &vattr, cred);
2986 vn_finished_write(mp);
2991 * Set ownership given a path name.
2993 #ifndef _SYS_SYSPROTO_H_
3001 sys_chown(struct thread *td, struct chown_args *uap)
3004 return (kern_fchownat(td, AT_FDCWD, uap->path, UIO_USERSPACE, uap->uid,
3008 #ifndef _SYS_SYSPROTO_H_
3009 struct fchownat_args {
3018 sys_fchownat(struct thread *td, struct fchownat_args *uap)
3021 if ((uap->flag & ~(AT_SYMLINK_NOFOLLOW | AT_RESOLVE_BENEATH |
3022 AT_EMPTY_PATH)) != 0)
3025 return (kern_fchownat(td, uap->fd, uap->path, UIO_USERSPACE, uap->uid,
3026 uap->gid, uap->flag));
3030 kern_fchownat(struct thread *td, int fd, const char *path,
3031 enum uio_seg pathseg, int uid, int gid, int flag)
3033 struct nameidata nd;
3036 AUDIT_ARG_OWNER(uid, gid);
3037 NDINIT_ATRIGHTS(&nd, LOOKUP, at2cnpflags(flag, AT_SYMLINK_NOFOLLOW |
3038 AT_RESOLVE_BENEATH | AT_EMPTY_PATH) | AUDITVNODE1, pathseg, path,
3039 fd, &cap_fchown_rights, td);
3041 if ((error = namei(&nd)) != 0)
3043 NDFREE_NOTHING(&nd);
3044 error = setfown(td, td->td_ucred, nd.ni_vp, uid, gid);
3050 * Set ownership given a path name, do not cross symlinks.
3052 #ifndef _SYS_SYSPROTO_H_
3053 struct lchown_args {
3060 sys_lchown(struct thread *td, struct lchown_args *uap)
3063 return (kern_fchownat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
3064 uap->uid, uap->gid, AT_SYMLINK_NOFOLLOW));
3068 * Set ownership given a file descriptor.
3070 #ifndef _SYS_SYSPROTO_H_
3071 struct fchown_args {
3078 sys_fchown(struct thread *td, struct fchown_args *uap)
3083 AUDIT_ARG_FD(uap->fd);
3084 AUDIT_ARG_OWNER(uap->uid, uap->gid);
3085 error = fget(td, uap->fd, &cap_fchown_rights, &fp);
3088 error = fo_chown(fp, uap->uid, uap->gid, td->td_ucred, td);
3094 * Common implementation code for utimes(), lutimes(), and futimes().
3097 getutimes(const struct timeval *usrtvp, enum uio_seg tvpseg,
3098 struct timespec *tsp)
3100 struct timeval tv[2];
3101 const struct timeval *tvp;
3104 if (usrtvp == NULL) {
3105 vfs_timestamp(&tsp[0]);
3108 if (tvpseg == UIO_SYSSPACE) {
3111 if ((error = copyin(usrtvp, tv, sizeof(tv))) != 0)
3116 if (tvp[0].tv_usec < 0 || tvp[0].tv_usec >= 1000000 ||
3117 tvp[1].tv_usec < 0 || tvp[1].tv_usec >= 1000000)
3119 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
3120 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
3126 * Common implementation code for futimens(), utimensat().
3128 #define UTIMENS_NULL 0x1
3129 #define UTIMENS_EXIT 0x2
3131 getutimens(const struct timespec *usrtsp, enum uio_seg tspseg,
3132 struct timespec *tsp, int *retflags)
3134 struct timespec tsnow;
3137 vfs_timestamp(&tsnow);
3139 if (usrtsp == NULL) {
3142 *retflags |= UTIMENS_NULL;
3145 if (tspseg == UIO_SYSSPACE) {
3148 } else if ((error = copyin(usrtsp, tsp, sizeof(*tsp) * 2)) != 0)
3150 if (tsp[0].tv_nsec == UTIME_OMIT && tsp[1].tv_nsec == UTIME_OMIT)
3151 *retflags |= UTIMENS_EXIT;
3152 if (tsp[0].tv_nsec == UTIME_NOW && tsp[1].tv_nsec == UTIME_NOW)
3153 *retflags |= UTIMENS_NULL;
3154 if (tsp[0].tv_nsec == UTIME_OMIT)
3155 tsp[0].tv_sec = VNOVAL;
3156 else if (tsp[0].tv_nsec == UTIME_NOW)
3158 else if (tsp[0].tv_nsec < 0 || tsp[0].tv_nsec >= 1000000000L)
3160 if (tsp[1].tv_nsec == UTIME_OMIT)
3161 tsp[1].tv_sec = VNOVAL;
3162 else if (tsp[1].tv_nsec == UTIME_NOW)
3164 else if (tsp[1].tv_nsec < 0 || tsp[1].tv_nsec >= 1000000000L)
3171 * Common implementation code for utimes(), lutimes(), futimes(), futimens(),
3175 setutimes(struct thread *td, struct vnode *vp, const struct timespec *ts,
3176 int numtimes, int nullflag)
3183 setbirthtime = false;
3184 vattr.va_birthtime.tv_sec = VNOVAL;
3185 vattr.va_birthtime.tv_nsec = 0;
3187 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
3189 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3190 if (numtimes < 3 && VOP_GETATTR(vp, &vattr, td->td_ucred) == 0 &&
3191 timespeccmp(&ts[1], &vattr.va_birthtime, < ))
3192 setbirthtime = true;
3194 vattr.va_atime = ts[0];
3195 vattr.va_mtime = ts[1];
3197 vattr.va_birthtime = ts[1];
3199 vattr.va_birthtime = ts[2];
3201 vattr.va_vaflags |= VA_UTIMES_NULL;
3203 error = mac_vnode_check_setutimes(td->td_ucred, vp, vattr.va_atime,
3207 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3209 vn_finished_write(mp);
3214 * Set the access and modification times of a file.
3216 #ifndef _SYS_SYSPROTO_H_
3217 struct utimes_args {
3219 struct timeval *tptr;
3223 sys_utimes(struct thread *td, struct utimes_args *uap)
3226 return (kern_utimesat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
3227 uap->tptr, UIO_USERSPACE));
3230 #ifndef _SYS_SYSPROTO_H_
3231 struct futimesat_args {
3234 const struct timeval * times;
3238 sys_futimesat(struct thread *td, struct futimesat_args *uap)
3241 return (kern_utimesat(td, uap->fd, uap->path, UIO_USERSPACE,
3242 uap->times, UIO_USERSPACE));
3246 kern_utimesat(struct thread *td, int fd, const char *path,
3247 enum uio_seg pathseg, struct timeval *tptr, enum uio_seg tptrseg)
3249 struct nameidata nd;
3250 struct timespec ts[2];
3253 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3255 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, fd,
3256 &cap_futimes_rights, td);
3258 if ((error = namei(&nd)) != 0)
3260 NDFREE_NOTHING(&nd);
3261 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3267 * Set the access and modification times of a file.
3269 #ifndef _SYS_SYSPROTO_H_
3270 struct lutimes_args {
3272 struct timeval *tptr;
3276 sys_lutimes(struct thread *td, struct lutimes_args *uap)
3279 return (kern_lutimes(td, uap->path, UIO_USERSPACE, uap->tptr,
3284 kern_lutimes(struct thread *td, const char *path, enum uio_seg pathseg,
3285 struct timeval *tptr, enum uio_seg tptrseg)
3287 struct timespec ts[2];
3288 struct nameidata nd;
3291 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3293 NDINIT(&nd, LOOKUP, NOFOLLOW | AUDITVNODE1, pathseg, path, td);
3294 if ((error = namei(&nd)) != 0)
3296 NDFREE_NOTHING(&nd);
3297 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3303 * Set the access and modification times of a file.
3305 #ifndef _SYS_SYSPROTO_H_
3306 struct futimes_args {
3308 struct timeval *tptr;
3312 sys_futimes(struct thread *td, struct futimes_args *uap)
3315 return (kern_futimes(td, uap->fd, uap->tptr, UIO_USERSPACE));
3319 kern_futimes(struct thread *td, int fd, struct timeval *tptr,
3320 enum uio_seg tptrseg)
3322 struct timespec ts[2];
3327 error = getutimes(tptr, tptrseg, ts);
3330 error = getvnode(td, fd, &cap_futimes_rights, &fp);
3334 if (AUDITING_TD(td)) {
3335 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
3336 AUDIT_ARG_VNODE1(fp->f_vnode);
3337 VOP_UNLOCK(fp->f_vnode);
3340 error = setutimes(td, fp->f_vnode, ts, 2, tptr == NULL);
3346 sys_futimens(struct thread *td, struct futimens_args *uap)
3349 return (kern_futimens(td, uap->fd, uap->times, UIO_USERSPACE));
3353 kern_futimens(struct thread *td, int fd, struct timespec *tptr,
3354 enum uio_seg tptrseg)
3356 struct timespec ts[2];
3361 error = getutimens(tptr, tptrseg, ts, &flags);
3364 if (flags & UTIMENS_EXIT)
3366 error = getvnode(td, fd, &cap_futimes_rights, &fp);
3370 if (AUDITING_TD(td)) {
3371 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
3372 AUDIT_ARG_VNODE1(fp->f_vnode);
3373 VOP_UNLOCK(fp->f_vnode);
3376 error = setutimes(td, fp->f_vnode, ts, 2, flags & UTIMENS_NULL);
3382 sys_utimensat(struct thread *td, struct utimensat_args *uap)
3385 return (kern_utimensat(td, uap->fd, uap->path, UIO_USERSPACE,
3386 uap->times, UIO_USERSPACE, uap->flag));
3390 kern_utimensat(struct thread *td, int fd, const char *path,
3391 enum uio_seg pathseg, struct timespec *tptr, enum uio_seg tptrseg,
3394 struct nameidata nd;
3395 struct timespec ts[2];
3398 if ((flag & ~(AT_SYMLINK_NOFOLLOW | AT_RESOLVE_BENEATH |
3399 AT_EMPTY_PATH)) != 0)
3402 if ((error = getutimens(tptr, tptrseg, ts, &flags)) != 0)
3404 NDINIT_ATRIGHTS(&nd, LOOKUP, at2cnpflags(flag, AT_SYMLINK_NOFOLLOW |
3405 AT_RESOLVE_BENEATH | AT_EMPTY_PATH) | AUDITVNODE1,
3406 pathseg, path, fd, &cap_futimes_rights, td);
3407 if ((error = namei(&nd)) != 0)
3410 * We are allowed to call namei() regardless of 2xUTIME_OMIT.
3412 * "If both tv_nsec fields are UTIME_OMIT... EACCESS may be detected."
3413 * "Search permission is denied by a component of the path prefix."
3415 NDFREE_NOTHING(&nd);
3416 if ((flags & UTIMENS_EXIT) == 0)
3417 error = setutimes(td, nd.ni_vp, ts, 2, flags & UTIMENS_NULL);
3423 * Truncate a file given its path name.
3425 #ifndef _SYS_SYSPROTO_H_
3426 struct truncate_args {
3433 sys_truncate(struct thread *td, struct truncate_args *uap)
3436 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3440 kern_truncate(struct thread *td, const char *path, enum uio_seg pathseg,
3447 struct nameidata nd;
3454 NDINIT(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, td);
3455 if ((error = namei(&nd)) != 0)
3458 rl_cookie = vn_rangelock_wlock(vp, 0, OFF_MAX);
3459 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0) {
3460 vn_rangelock_unlock(vp, rl_cookie);
3464 NDFREE(&nd, NDF_ONLY_PNBUF);
3465 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3466 if (vp->v_type == VDIR)
3469 else if ((error = mac_vnode_check_write(td->td_ucred, NOCRED, vp))) {
3472 else if ((error = vn_writechk(vp)) == 0 &&
3473 (error = VOP_ACCESS(vp, VWRITE, td->td_ucred, td)) == 0) {
3475 vattr.va_size = length;
3476 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3479 vn_finished_write(mp);
3480 vn_rangelock_unlock(vp, rl_cookie);
3482 if (error == ERELOOKUP)
3487 #if defined(COMPAT_43)
3489 * Truncate a file given its path name.
3491 #ifndef _SYS_SYSPROTO_H_
3492 struct otruncate_args {
3498 otruncate(struct thread *td, struct otruncate_args *uap)
3501 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3503 #endif /* COMPAT_43 */
3505 #if defined(COMPAT_FREEBSD6)
3506 /* Versions with the pad argument */
3508 freebsd6_truncate(struct thread *td, struct freebsd6_truncate_args *uap)
3511 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3515 freebsd6_ftruncate(struct thread *td, struct freebsd6_ftruncate_args *uap)
3518 return (kern_ftruncate(td, uap->fd, uap->length));
3523 kern_fsync(struct thread *td, int fd, bool fullsync)
3531 error = getvnode(td, fd, &cap_fsync_rights, &fp);
3537 /* XXXKIB: compete outstanding aio writes */;
3540 error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
3543 vn_lock(vp, vn_lktype_write(mp, vp) | LK_RETRY);
3544 AUDIT_ARG_VNODE1(vp);
3545 if (vp->v_object != NULL) {
3546 VM_OBJECT_WLOCK(vp->v_object);
3547 vm_object_page_clean(vp->v_object, 0, 0, 0);
3548 VM_OBJECT_WUNLOCK(vp->v_object);
3550 error = fullsync ? VOP_FSYNC(vp, MNT_WAIT, td) : VOP_FDATASYNC(vp, td);
3552 vn_finished_write(mp);
3553 if (error == ERELOOKUP)
3561 * Sync an open file.
3563 #ifndef _SYS_SYSPROTO_H_
3569 sys_fsync(struct thread *td, struct fsync_args *uap)
3572 return (kern_fsync(td, uap->fd, true));
3576 sys_fdatasync(struct thread *td, struct fdatasync_args *uap)
3579 return (kern_fsync(td, uap->fd, false));
3583 * Rename files. Source and destination must either both be directories, or
3584 * both not be directories. If target is a directory, it must be empty.
3586 #ifndef _SYS_SYSPROTO_H_
3587 struct rename_args {
3593 sys_rename(struct thread *td, struct rename_args *uap)
3596 return (kern_renameat(td, AT_FDCWD, uap->from, AT_FDCWD,
3597 uap->to, UIO_USERSPACE));
3600 #ifndef _SYS_SYSPROTO_H_
3601 struct renameat_args {
3609 sys_renameat(struct thread *td, struct renameat_args *uap)
3612 return (kern_renameat(td, uap->oldfd, uap->old, uap->newfd, uap->new,
3618 kern_renameat_mac(struct thread *td, int oldfd, const char *old, int newfd,
3619 const char *new, enum uio_seg pathseg, struct nameidata *fromnd)
3623 NDINIT_ATRIGHTS(fromnd, DELETE, LOCKPARENT | LOCKLEAF | SAVESTART |
3624 AUDITVNODE1, pathseg, old, oldfd, &cap_renameat_source_rights, td);
3625 if ((error = namei(fromnd)) != 0)
3627 error = mac_vnode_check_rename_from(td->td_ucred, fromnd->ni_dvp,
3628 fromnd->ni_vp, &fromnd->ni_cnd);
3629 VOP_UNLOCK(fromnd->ni_dvp);
3630 if (fromnd->ni_dvp != fromnd->ni_vp)
3631 VOP_UNLOCK(fromnd->ni_vp);
3633 NDFREE(fromnd, NDF_ONLY_PNBUF);
3634 vrele(fromnd->ni_dvp);
3635 vrele(fromnd->ni_vp);
3636 if (fromnd->ni_startdir)
3637 vrele(fromnd->ni_startdir);
3644 kern_renameat(struct thread *td, int oldfd, const char *old, int newfd,
3645 const char *new, enum uio_seg pathseg)
3647 struct mount *mp = NULL;
3648 struct vnode *tvp, *fvp, *tdvp;
3649 struct nameidata fromnd, tond;
3650 u_int64_t tondflags;
3656 if (mac_vnode_check_rename_from_enabled()) {
3657 error = kern_renameat_mac(td, oldfd, old, newfd, new, pathseg,
3663 NDINIT_ATRIGHTS(&fromnd, DELETE, WANTPARENT | SAVESTART | AUDITVNODE1,
3664 pathseg, old, oldfd, &cap_renameat_source_rights, td);
3665 if ((error = namei(&fromnd)) != 0)
3671 tondflags = LOCKPARENT | LOCKLEAF | NOCACHE | SAVESTART | AUDITVNODE2;
3672 if (fromnd.ni_vp->v_type == VDIR)
3673 tondflags |= WILLBEDIR;
3674 NDINIT_ATRIGHTS(&tond, RENAME, tondflags, pathseg, new, newfd,
3675 &cap_renameat_target_rights, td);
3676 if ((error = namei(&tond)) != 0) {
3677 /* Translate error code for rename("dir1", "dir2/."). */
3678 if (error == EISDIR && fvp->v_type == VDIR)
3680 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3681 vrele(fromnd.ni_dvp);
3687 error = vn_start_write(fvp, &mp, V_NOWAIT);
3689 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3690 NDFREE(&tond, NDF_ONLY_PNBUF);
3697 vrele(fromnd.ni_dvp);
3699 vrele(tond.ni_startdir);
3700 if (fromnd.ni_startdir != NULL)
3701 vrele(fromnd.ni_startdir);
3702 error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH);
3708 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
3711 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
3716 if (newfd != AT_FDCWD && (tond.ni_resflags & NIRES_ABS) == 0) {
3718 * If the target already exists we require CAP_UNLINKAT
3719 * from 'newfd', when newfd was used for the lookup.
3721 error = cap_check(&tond.ni_filecaps.fc_rights,
3722 &cap_unlinkat_rights);
3733 * If the source is the same as the destination (that is, if they
3734 * are links to the same vnode), then there is nothing to do.
3740 error = mac_vnode_check_rename_to(td->td_ucred, tdvp,
3741 tond.ni_vp, fromnd.ni_dvp == tdvp, &tond.ni_cnd);
3745 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
3746 tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
3747 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3748 NDFREE(&tond, NDF_ONLY_PNBUF);
3750 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3751 NDFREE(&tond, NDF_ONLY_PNBUF);
3758 vrele(fromnd.ni_dvp);
3761 vrele(tond.ni_startdir);
3762 vn_finished_write(mp);
3764 if (fromnd.ni_startdir)
3765 vrele(fromnd.ni_startdir);
3766 if (error == ERESTART)
3768 if (error == ERELOOKUP)
3774 * Make a directory file.
3776 #ifndef _SYS_SYSPROTO_H_
3783 sys_mkdir(struct thread *td, struct mkdir_args *uap)
3786 return (kern_mkdirat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
3790 #ifndef _SYS_SYSPROTO_H_
3791 struct mkdirat_args {
3798 sys_mkdirat(struct thread *td, struct mkdirat_args *uap)
3801 return (kern_mkdirat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode));
3805 kern_mkdirat(struct thread *td, int fd, const char *path, enum uio_seg segflg,
3810 struct nameidata nd;
3813 AUDIT_ARG_MODE(mode);
3817 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
3818 NC_NOMAKEENTRY | NC_KEEPPOSENTRY | FAILIFEXISTS | WILLBEDIR,
3819 segflg, path, fd, &cap_mkdirat_rights, td);
3820 if ((error = namei(&nd)) != 0)
3822 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3823 NDFREE(&nd, NDF_ONLY_PNBUF);
3825 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3830 vattr.va_type = VDIR;
3831 vattr.va_mode = (mode & ACCESSPERMS) &~ td->td_proc->p_pd->pd_cmask;
3833 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
3838 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
3842 NDFREE(&nd, NDF_ONLY_PNBUF);
3843 VOP_VPUT_PAIR(nd.ni_dvp, error == 0 ? &nd.ni_vp : NULL, true);
3844 vn_finished_write(mp);
3845 if (error == ERELOOKUP)
3851 * Remove a directory file.
3853 #ifndef _SYS_SYSPROTO_H_
3859 sys_rmdir(struct thread *td, struct rmdir_args *uap)
3862 return (kern_frmdirat(td, AT_FDCWD, uap->path, FD_NONE, UIO_USERSPACE,
3867 kern_frmdirat(struct thread *td, int dfd, const char *path, int fd,
3868 enum uio_seg pathseg, int flag)
3873 struct nameidata nd;
3874 cap_rights_t rights;
3878 if (fd != FD_NONE) {
3879 error = getvnode(td, fd, cap_rights_init_one(&rights,
3888 NDINIT_ATRIGHTS(&nd, DELETE, LOCKPARENT | LOCKLEAF | AUDITVNODE1 |
3889 at2cnpflags(flag, AT_RESOLVE_BENEATH),
3890 pathseg, path, dfd, &cap_unlinkat_rights, td);
3891 if ((error = namei(&nd)) != 0)
3894 if (vp->v_type != VDIR) {
3899 * No rmdir "." please.
3901 if (nd.ni_dvp == vp) {
3906 * The root of a mounted filesystem cannot be deleted.
3908 if (vp->v_vflag & VV_ROOT) {
3913 if (fp != NULL && fp->f_vnode != vp) {
3914 if (VN_IS_DOOMED(fp->f_vnode))
3922 error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp,
3927 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3928 NDFREE(&nd, NDF_ONLY_PNBUF);
3930 if (nd.ni_dvp == vp)
3934 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3938 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_UNLINK);
3939 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
3940 vn_finished_write(mp);
3942 NDFREE(&nd, NDF_ONLY_PNBUF);
3944 if (nd.ni_dvp == vp)
3948 if (error == ERELOOKUP)
3956 #if defined(COMPAT_43) || defined(COMPAT_FREEBSD11)
3958 freebsd11_kern_getdirentries(struct thread *td, int fd, char *ubuf, u_int count,
3959 long *basep, void (*func)(struct freebsd11_dirent *))
3961 struct freebsd11_dirent dstdp;
3962 struct dirent *dp, *edp;
3965 ssize_t resid, ucount;
3968 /* XXX arbitrary sanity limit on `count'. */
3969 count = min(count, 64 * 1024);
3971 dirbuf = malloc(count, M_TEMP, M_WAITOK);
3973 error = kern_getdirentries(td, fd, dirbuf, count, &base, &resid,
3981 for (dp = (struct dirent *)dirbuf,
3982 edp = (struct dirent *)&dirbuf[count - resid];
3983 ucount < count && dp < edp; ) {
3984 if (dp->d_reclen == 0)
3986 MPASS(dp->d_reclen >= _GENERIC_DIRLEN(0));
3987 if (dp->d_namlen >= sizeof(dstdp.d_name))
3989 dstdp.d_type = dp->d_type;
3990 dstdp.d_namlen = dp->d_namlen;
3991 dstdp.d_fileno = dp->d_fileno; /* truncate */
3992 if (dstdp.d_fileno != dp->d_fileno) {
3993 switch (ino64_trunc_error) {
4001 dstdp.d_fileno = UINT32_MAX;
4005 dstdp.d_reclen = sizeof(dstdp) - sizeof(dstdp.d_name) +
4006 ((dp->d_namlen + 1 + 3) &~ 3);
4007 bcopy(dp->d_name, dstdp.d_name, dstdp.d_namlen);
4008 bzero(dstdp.d_name + dstdp.d_namlen,
4009 dstdp.d_reclen - offsetof(struct freebsd11_dirent, d_name) -
4011 MPASS(dstdp.d_reclen <= dp->d_reclen);
4012 MPASS(ucount + dstdp.d_reclen <= count);
4015 error = copyout(&dstdp, ubuf + ucount, dstdp.d_reclen);
4018 dp = (struct dirent *)((char *)dp + dp->d_reclen);
4019 ucount += dstdp.d_reclen;
4023 free(dirbuf, M_TEMP);
4025 td->td_retval[0] = ucount;
4032 ogetdirentries_cvt(struct freebsd11_dirent *dp)
4034 #if (BYTE_ORDER == LITTLE_ENDIAN)
4036 * The expected low byte of dp->d_namlen is our dp->d_type.
4037 * The high MBZ byte of dp->d_namlen is our dp->d_namlen.
4039 dp->d_type = dp->d_namlen;
4043 * The dp->d_type is the high byte of the expected dp->d_namlen,
4044 * so must be zero'ed.
4051 * Read a block of directory entries in a filesystem independent format.
4053 #ifndef _SYS_SYSPROTO_H_
4054 struct ogetdirentries_args {
4062 ogetdirentries(struct thread *td, struct ogetdirentries_args *uap)
4067 error = kern_ogetdirentries(td, uap, &loff);
4069 error = copyout(&loff, uap->basep, sizeof(long));
4074 kern_ogetdirentries(struct thread *td, struct ogetdirentries_args *uap,
4080 /* XXX arbitrary sanity limit on `count'. */
4081 if (uap->count > 64 * 1024)
4084 error = freebsd11_kern_getdirentries(td, uap->fd, uap->buf, uap->count,
4085 &base, ogetdirentries_cvt);
4087 if (error == 0 && uap->basep != NULL)
4088 error = copyout(&base, uap->basep, sizeof(long));
4092 #endif /* COMPAT_43 */
4094 #if defined(COMPAT_FREEBSD11)
4095 #ifndef _SYS_SYSPROTO_H_
4096 struct freebsd11_getdirentries_args {
4104 freebsd11_getdirentries(struct thread *td,
4105 struct freebsd11_getdirentries_args *uap)
4110 error = freebsd11_kern_getdirentries(td, uap->fd, uap->buf, uap->count,
4113 if (error == 0 && uap->basep != NULL)
4114 error = copyout(&base, uap->basep, sizeof(long));
4119 freebsd11_getdents(struct thread *td, struct freebsd11_getdents_args *uap)
4121 struct freebsd11_getdirentries_args ap;
4125 ap.count = uap->count;
4127 return (freebsd11_getdirentries(td, &ap));
4129 #endif /* COMPAT_FREEBSD11 */
4132 * Read a block of directory entries in a filesystem independent format.
4135 sys_getdirentries(struct thread *td, struct getdirentries_args *uap)
4140 error = kern_getdirentries(td, uap->fd, uap->buf, uap->count, &base,
4141 NULL, UIO_USERSPACE);
4144 if (uap->basep != NULL)
4145 error = copyout(&base, uap->basep, sizeof(off_t));
4150 kern_getdirentries(struct thread *td, int fd, char *buf, size_t count,
4151 off_t *basep, ssize_t *residp, enum uio_seg bufseg)
4162 if (count > IOSIZE_MAX)
4164 auio.uio_resid = count;
4165 error = getvnode(td, fd, &cap_read_rights, &fp);
4168 if ((fp->f_flag & FREAD) == 0) {
4173 foffset = foffset_lock(fp, 0);
4175 if (vp->v_type != VDIR) {
4179 aiov.iov_base = buf;
4180 aiov.iov_len = count;
4181 auio.uio_iov = &aiov;
4182 auio.uio_iovcnt = 1;
4183 auio.uio_rw = UIO_READ;
4184 auio.uio_segflg = bufseg;
4186 vn_lock(vp, LK_SHARED | LK_RETRY);
4187 AUDIT_ARG_VNODE1(vp);
4188 loff = auio.uio_offset = foffset;
4190 error = mac_vnode_check_readdir(td->td_ucred, vp);
4193 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL,
4195 foffset = auio.uio_offset;
4200 if (count == auio.uio_resid &&
4201 (vp->v_vflag & VV_ROOT) &&
4202 (vp->v_mount->mnt_flag & MNT_UNION)) {
4203 struct vnode *tvp = vp;
4205 vp = vp->v_mount->mnt_vnodecovered;
4215 *residp = auio.uio_resid;
4216 td->td_retval[0] = count - auio.uio_resid;
4218 foffset_unlock(fp, foffset, 0);
4224 * Set the mode mask for creation of filesystem nodes.
4226 #ifndef _SYS_SYSPROTO_H_
4232 sys_umask(struct thread *td, struct umask_args *uap)
4234 struct pwddesc *pdp;
4236 pdp = td->td_proc->p_pd;
4238 td->td_retval[0] = pdp->pd_cmask;
4239 pdp->pd_cmask = uap->newmask & ALLPERMS;
4240 PWDDESC_XUNLOCK(pdp);
4245 * Void all references to file by ripping underlying filesystem away from
4248 #ifndef _SYS_SYSPROTO_H_
4249 struct revoke_args {
4254 sys_revoke(struct thread *td, struct revoke_args *uap)
4258 struct nameidata nd;
4261 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
4263 if ((error = namei(&nd)) != 0)
4266 NDFREE_NOTHING(&nd);
4267 if (vp->v_type != VCHR || vp->v_rdev == NULL) {
4272 error = mac_vnode_check_revoke(td->td_ucred, vp);
4276 error = VOP_GETATTR(vp, &vattr, td->td_ucred);
4279 if (td->td_ucred->cr_uid != vattr.va_uid) {
4280 error = priv_check(td, PRIV_VFS_ADMIN);
4284 if (devfs_usecount(vp) > 0)
4285 VOP_REVOKE(vp, REVOKEALL);
4292 * This variant of getvnode() allows O_PATH files. Caller should
4293 * ensure that returned file and vnode are only used for compatible
4297 getvnode_path(struct thread *td, int fd, cap_rights_t *rightsp,
4303 error = fget_unlocked(td->td_proc->p_fd, fd, rightsp, &fp);
4308 * The file could be not of the vnode type, or it may be not
4309 * yet fully initialized, in which case the f_vnode pointer
4310 * may be set, but f_ops is still badfileops. E.g.,
4311 * devfs_open() transiently create such situation to
4312 * facilitate csw d_fdopen().
4314 * Dupfdopen() handling in kern_openat() installs the
4315 * half-baked file into the process descriptor table, allowing
4316 * other thread to dereference it. Guard against the race by
4319 if (fp->f_vnode == NULL || fp->f_ops == &badfileops) {
4329 * Convert a user file descriptor to a kernel file entry and check
4330 * that, if it is a capability, the correct rights are present.
4331 * A reference on the file entry is held upon returning.
4334 getvnode(struct thread *td, int fd, cap_rights_t *rightsp, struct file **fpp)
4338 error = getvnode_path(td, fd, rightsp, fpp);
4341 * Filter out O_PATH file descriptors, most getvnode() callers
4342 * do not call fo_ methods.
4344 if (error == 0 && (*fpp)->f_ops == &path_fileops) {
4353 * Get an (NFS) file handle.
4355 #ifndef _SYS_SYSPROTO_H_
4356 struct lgetfh_args {
4362 sys_lgetfh(struct thread *td, struct lgetfh_args *uap)
4365 return (kern_getfhat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->fname,
4366 UIO_USERSPACE, uap->fhp, UIO_USERSPACE));
4369 #ifndef _SYS_SYSPROTO_H_
4376 sys_getfh(struct thread *td, struct getfh_args *uap)
4379 return (kern_getfhat(td, 0, AT_FDCWD, uap->fname, UIO_USERSPACE,
4380 uap->fhp, UIO_USERSPACE));
4384 * syscall for the rpc.lockd to use to translate an open descriptor into
4385 * a NFS file handle.
4387 * warning: do not remove the priv_check() call or this becomes one giant
4390 #ifndef _SYS_SYSPROTO_H_
4391 struct getfhat_args {
4399 sys_getfhat(struct thread *td, struct getfhat_args *uap)
4402 if ((uap->flags & ~(AT_SYMLINK_NOFOLLOW | AT_RESOLVE_BENEATH)) != 0)
4404 return (kern_getfhat(td, uap->flags, uap->fd, uap->path, UIO_USERSPACE,
4405 uap->fhp, UIO_USERSPACE));
4409 kern_getfhat(struct thread *td, int flags, int fd, const char *path,
4410 enum uio_seg pathseg, fhandle_t *fhp, enum uio_seg fhseg)
4412 struct nameidata nd;
4417 error = priv_check(td, PRIV_VFS_GETFH);
4420 NDINIT_AT(&nd, LOOKUP, at2cnpflags(flags, AT_SYMLINK_NOFOLLOW |
4421 AT_RESOLVE_BENEATH) | LOCKLEAF | AUDITVNODE1, pathseg, path,
4426 NDFREE_NOTHING(&nd);
4428 bzero(&fh, sizeof(fh));
4429 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
4430 error = VOP_VPTOFH(vp, &fh.fh_fid);
4433 if (fhseg == UIO_USERSPACE)
4434 error = copyout(&fh, fhp, sizeof (fh));
4436 memcpy(fhp, &fh, sizeof(fh));
4441 #ifndef _SYS_SYSPROTO_H_
4442 struct fhlink_args {
4448 sys_fhlink(struct thread *td, struct fhlink_args *uap)
4451 return (kern_fhlinkat(td, AT_FDCWD, uap->to, UIO_USERSPACE, uap->fhp));
4454 #ifndef _SYS_SYSPROTO_H_
4455 struct fhlinkat_args {
4462 sys_fhlinkat(struct thread *td, struct fhlinkat_args *uap)
4465 return (kern_fhlinkat(td, uap->tofd, uap->to, UIO_USERSPACE, uap->fhp));
4469 kern_fhlinkat(struct thread *td, int fd, const char *path,
4470 enum uio_seg pathseg, fhandle_t *fhp)
4477 error = priv_check(td, PRIV_VFS_GETFH);
4480 error = copyin(fhp, &fh, sizeof(fh));
4485 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4487 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_SHARED, &vp);
4492 error = kern_linkat_vp(td, vp, fd, path, pathseg);
4493 } while (error == EAGAIN || error == ERELOOKUP);
4497 #ifndef _SYS_SYSPROTO_H_
4498 struct fhreadlink_args {
4505 sys_fhreadlink(struct thread *td, struct fhreadlink_args *uap)
4512 error = priv_check(td, PRIV_VFS_GETFH);
4515 if (uap->bufsize > IOSIZE_MAX)
4517 error = copyin(uap->fhp, &fh, sizeof(fh));
4520 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4522 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_SHARED, &vp);
4526 error = kern_readlink_vp(vp, uap->buf, UIO_USERSPACE, uap->bufsize, td);
4532 * syscall for the rpc.lockd to use to translate a NFS file handle into an
4535 * warning: do not remove the priv_check() call or this becomes one giant
4538 #ifndef _SYS_SYSPROTO_H_
4539 struct fhopen_args {
4540 const struct fhandle *u_fhp;
4545 sys_fhopen(struct thread *td, struct fhopen_args *uap)
4547 return (kern_fhopen(td, uap->u_fhp, uap->flags));
4551 kern_fhopen(struct thread *td, const struct fhandle *u_fhp, int flags)
4560 error = priv_check(td, PRIV_VFS_FHOPEN);
4564 fmode = FFLAGS(flags);
4565 /* why not allow a non-read/write open for our lockd? */
4566 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
4568 error = copyin(u_fhp, &fhp, sizeof(fhp));
4571 /* find the mount point */
4572 mp = vfs_busyfs(&fhp.fh_fsid);
4575 /* now give me my vnode, it gets returned to me locked */
4576 error = VFS_FHTOVP(mp, &fhp.fh_fid, LK_EXCLUSIVE, &vp);
4581 error = falloc_noinstall(td, &fp);
4587 * An extra reference on `fp' has been held for us by
4588 * falloc_noinstall().
4594 error = vn_open_vnode(vp, fmode, td->td_ucred, td, fp);
4596 KASSERT(fp->f_ops == &badfileops,
4597 ("VOP_OPEN in fhopen() set f_ops"));
4598 KASSERT(td->td_dupfd < 0,
4599 ("fhopen() encountered fdopen()"));
4608 finit_vnode(fp, fmode, NULL, &vnops);
4610 if ((fmode & O_TRUNC) != 0) {
4611 error = fo_truncate(fp, 0, td->td_ucred, td);
4616 error = finstall(td, fp, &indx, fmode, NULL);
4619 td->td_retval[0] = indx;
4624 * Stat an (NFS) file handle.
4626 #ifndef _SYS_SYSPROTO_H_
4627 struct fhstat_args {
4628 struct fhandle *u_fhp;
4633 sys_fhstat(struct thread *td, struct fhstat_args *uap)
4639 error = copyin(uap->u_fhp, &fh, sizeof(fh));
4642 error = kern_fhstat(td, fh, &sb);
4644 error = copyout(&sb, uap->sb, sizeof(sb));
4649 kern_fhstat(struct thread *td, struct fhandle fh, struct stat *sb)
4655 error = priv_check(td, PRIV_VFS_FHSTAT);
4658 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4660 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_EXCLUSIVE, &vp);
4664 error = VOP_STAT(vp, sb, td->td_ucred, NOCRED, td);
4670 * Implement fstatfs() for (NFS) file handles.
4672 #ifndef _SYS_SYSPROTO_H_
4673 struct fhstatfs_args {
4674 struct fhandle *u_fhp;
4679 sys_fhstatfs(struct thread *td, struct fhstatfs_args *uap)
4685 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4688 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
4689 error = kern_fhstatfs(td, fh, sfp);
4691 error = copyout(sfp, uap->buf, sizeof(*sfp));
4692 free(sfp, M_STATFS);
4697 kern_fhstatfs(struct thread *td, fhandle_t fh, struct statfs *buf)
4703 error = priv_check(td, PRIV_VFS_FHSTATFS);
4706 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4708 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_EXCLUSIVE, &vp);
4714 error = prison_canseemount(td->td_ucred, mp);
4718 error = mac_mount_check_stat(td->td_ucred, mp);
4722 error = VFS_STATFS(mp, buf);
4729 * Unlike madvise(2), we do not make a best effort to remember every
4730 * possible caching hint. Instead, we remember the last setting with
4731 * the exception that we will allow POSIX_FADV_NORMAL to adjust the
4732 * region of any current setting.
4735 kern_posix_fadvise(struct thread *td, int fd, off_t offset, off_t len,
4738 struct fadvise_info *fa, *new;
4744 if (offset < 0 || len < 0 || offset > OFF_MAX - len)
4746 AUDIT_ARG_VALUE(advice);
4748 case POSIX_FADV_SEQUENTIAL:
4749 case POSIX_FADV_RANDOM:
4750 case POSIX_FADV_NOREUSE:
4751 new = malloc(sizeof(*fa), M_FADVISE, M_WAITOK);
4753 case POSIX_FADV_NORMAL:
4754 case POSIX_FADV_WILLNEED:
4755 case POSIX_FADV_DONTNEED:
4761 /* XXX: CAP_POSIX_FADVISE? */
4763 error = fget(td, fd, &cap_no_rights, &fp);
4766 AUDIT_ARG_FILE(td->td_proc, fp);
4767 if ((fp->f_ops->fo_flags & DFLAG_SEEKABLE) == 0) {
4771 if (fp->f_type != DTYPE_VNODE) {
4776 if (vp->v_type != VREG) {
4783 end = offset + len - 1;
4785 case POSIX_FADV_SEQUENTIAL:
4786 case POSIX_FADV_RANDOM:
4787 case POSIX_FADV_NOREUSE:
4789 * Try to merge any existing non-standard region with
4790 * this new region if possible, otherwise create a new
4791 * non-standard region for this request.
4793 mtx_pool_lock(mtxpool_sleep, fp);
4795 if (fa != NULL && fa->fa_advice == advice &&
4796 ((fa->fa_start <= end && fa->fa_end >= offset) ||
4797 (end != OFF_MAX && fa->fa_start == end + 1) ||
4798 (fa->fa_end != OFF_MAX && fa->fa_end + 1 == offset))) {
4799 if (offset < fa->fa_start)
4800 fa->fa_start = offset;
4801 if (end > fa->fa_end)
4804 new->fa_advice = advice;
4805 new->fa_start = offset;
4810 mtx_pool_unlock(mtxpool_sleep, fp);
4812 case POSIX_FADV_NORMAL:
4814 * If a the "normal" region overlaps with an existing
4815 * non-standard region, trim or remove the
4816 * non-standard region.
4818 mtx_pool_lock(mtxpool_sleep, fp);
4821 if (offset <= fa->fa_start && end >= fa->fa_end) {
4823 fp->f_advice = NULL;
4824 } else if (offset <= fa->fa_start &&
4825 end >= fa->fa_start)
4826 fa->fa_start = end + 1;
4827 else if (offset <= fa->fa_end && end >= fa->fa_end)
4828 fa->fa_end = offset - 1;
4829 else if (offset >= fa->fa_start && end <= fa->fa_end) {
4831 * If the "normal" region is a middle
4832 * portion of the existing
4833 * non-standard region, just remove
4834 * the whole thing rather than picking
4835 * one side or the other to
4839 fp->f_advice = NULL;
4842 mtx_pool_unlock(mtxpool_sleep, fp);
4844 case POSIX_FADV_WILLNEED:
4845 case POSIX_FADV_DONTNEED:
4846 error = VOP_ADVISE(vp, offset, end, advice);
4852 free(new, M_FADVISE);
4857 sys_posix_fadvise(struct thread *td, struct posix_fadvise_args *uap)
4861 error = kern_posix_fadvise(td, uap->fd, uap->offset, uap->len,
4863 return (kern_posix_error(td, error));
4867 kern_copy_file_range(struct thread *td, int infd, off_t *inoffp, int outfd,
4868 off_t *outoffp, size_t len, unsigned int flags)
4870 struct file *infp, *outfp;
4871 struct vnode *invp, *outvp;
4874 void *rl_rcookie, *rl_wcookie;
4875 off_t savinoff, savoutoff;
4877 infp = outfp = NULL;
4878 rl_rcookie = rl_wcookie = NULL;
4887 if (len > SSIZE_MAX)
4889 * Although the len argument is size_t, the return argument
4890 * is ssize_t (which is signed). Therefore a size that won't
4891 * fit in ssize_t can't be returned.
4895 /* Get the file structures for the file descriptors. */
4896 error = fget_read(td, infd, &cap_read_rights, &infp);
4899 if (infp->f_ops == &badfileops) {
4903 if (infp->f_vnode == NULL) {
4907 error = fget_write(td, outfd, &cap_write_rights, &outfp);
4910 if (outfp->f_ops == &badfileops) {
4914 if (outfp->f_vnode == NULL) {
4919 /* Set the offset pointers to the correct place. */
4921 inoffp = &infp->f_offset;
4922 if (outoffp == NULL)
4923 outoffp = &outfp->f_offset;
4925 savoutoff = *outoffp;
4927 invp = infp->f_vnode;
4928 outvp = outfp->f_vnode;
4929 /* Sanity check the f_flag bits. */
4930 if ((outfp->f_flag & (FWRITE | FAPPEND)) != FWRITE ||
4931 (infp->f_flag & FREAD) == 0) {
4936 /* If len == 0, just return 0. */
4941 * If infp and outfp refer to the same file, the byte ranges cannot
4944 if (invp == outvp && ((savinoff <= savoutoff && savinoff + len >
4945 savoutoff) || (savinoff > savoutoff && savoutoff + len >
4951 /* Range lock the byte ranges for both invp and outvp. */
4953 rl_wcookie = vn_rangelock_wlock(outvp, *outoffp, *outoffp +
4955 rl_rcookie = vn_rangelock_tryrlock(invp, *inoffp, *inoffp +
4957 if (rl_rcookie != NULL)
4959 vn_rangelock_unlock(outvp, rl_wcookie);
4960 rl_rcookie = vn_rangelock_rlock(invp, *inoffp, *inoffp + len);
4961 vn_rangelock_unlock(invp, rl_rcookie);
4965 error = vn_copy_file_range(invp, inoffp, outvp, outoffp, &retlen,
4966 flags, infp->f_cred, outfp->f_cred, td);
4968 if (rl_rcookie != NULL)
4969 vn_rangelock_unlock(invp, rl_rcookie);
4970 if (rl_wcookie != NULL)
4971 vn_rangelock_unlock(outvp, rl_wcookie);
4972 if (savinoff != -1 && (error == EINTR || error == ERESTART)) {
4974 *outoffp = savoutoff;
4980 td->td_retval[0] = retlen;
4985 sys_copy_file_range(struct thread *td, struct copy_file_range_args *uap)
4987 off_t inoff, outoff, *inoffp, *outoffp;
4990 inoffp = outoffp = NULL;
4991 if (uap->inoffp != NULL) {
4992 error = copyin(uap->inoffp, &inoff, sizeof(off_t));
4997 if (uap->outoffp != NULL) {
4998 error = copyin(uap->outoffp, &outoff, sizeof(off_t));
5003 error = kern_copy_file_range(td, uap->infd, inoffp, uap->outfd,
5004 outoffp, uap->len, uap->flags);
5005 if (error == 0 && uap->inoffp != NULL)
5006 error = copyout(inoffp, uap->inoffp, sizeof(off_t));
5007 if (error == 0 && uap->outoffp != NULL)
5008 error = copyout(outoffp, uap->outoffp, sizeof(off_t));