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)
2443 error = VOP_STAT(nd.ni_vp, sbp, td->td_ucred, NOCRED, td);
2445 if (__predict_false(hook != NULL))
2446 hook(nd.ni_vp, sbp);
2448 NDFREE_NOTHING(&nd);
2450 #ifdef __STAT_TIME_T_EXT
2451 sbp->st_atim_ext = 0;
2452 sbp->st_mtim_ext = 0;
2453 sbp->st_ctim_ext = 0;
2454 sbp->st_btim_ext = 0;
2457 if (KTRPOINT(td, KTR_STRUCT))
2458 ktrstat_error(sbp, error);
2463 #if defined(COMPAT_FREEBSD11)
2465 * Implementation of the NetBSD [l]stat() functions.
2468 freebsd11_cvtnstat(struct stat *sb, struct nstat *nsb)
2471 bzero(nsb, sizeof(*nsb));
2472 nsb->st_dev = sb->st_dev;
2473 nsb->st_ino = sb->st_ino;
2474 nsb->st_mode = sb->st_mode;
2475 nsb->st_nlink = sb->st_nlink;
2476 nsb->st_uid = sb->st_uid;
2477 nsb->st_gid = sb->st_gid;
2478 nsb->st_rdev = sb->st_rdev;
2479 nsb->st_atim = sb->st_atim;
2480 nsb->st_mtim = sb->st_mtim;
2481 nsb->st_ctim = sb->st_ctim;
2482 nsb->st_size = sb->st_size;
2483 nsb->st_blocks = sb->st_blocks;
2484 nsb->st_blksize = sb->st_blksize;
2485 nsb->st_flags = sb->st_flags;
2486 nsb->st_gen = sb->st_gen;
2487 nsb->st_birthtim = sb->st_birthtim;
2490 #ifndef _SYS_SYSPROTO_H_
2491 struct freebsd11_nstat_args {
2497 freebsd11_nstat(struct thread *td, struct freebsd11_nstat_args *uap)
2503 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
2507 freebsd11_cvtnstat(&sb, &nsb);
2508 return (copyout(&nsb, uap->ub, sizeof (nsb)));
2512 * NetBSD lstat. Get file status; this version does not follow links.
2514 #ifndef _SYS_SYSPROTO_H_
2515 struct freebsd11_nlstat_args {
2521 freebsd11_nlstat(struct thread *td, struct freebsd11_nlstat_args *uap)
2527 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2528 UIO_USERSPACE, &sb, NULL);
2531 freebsd11_cvtnstat(&sb, &nsb);
2532 return (copyout(&nsb, uap->ub, sizeof (nsb)));
2534 #endif /* COMPAT_FREEBSD11 */
2537 * Get configurable pathname variables.
2539 #ifndef _SYS_SYSPROTO_H_
2540 struct pathconf_args {
2546 sys_pathconf(struct thread *td, struct pathconf_args *uap)
2551 error = kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name, FOLLOW,
2554 td->td_retval[0] = value;
2558 #ifndef _SYS_SYSPROTO_H_
2559 struct lpathconf_args {
2565 sys_lpathconf(struct thread *td, struct lpathconf_args *uap)
2570 error = kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name,
2573 td->td_retval[0] = value;
2578 kern_pathconf(struct thread *td, const char *path, enum uio_seg pathseg,
2579 int name, u_long flags, long *valuep)
2581 struct nameidata nd;
2584 NDINIT(&nd, LOOKUP, LOCKSHARED | LOCKLEAF | AUDITVNODE1 | flags,
2586 if ((error = namei(&nd)) != 0)
2588 NDFREE_NOTHING(&nd);
2590 error = VOP_PATHCONF(nd.ni_vp, name, valuep);
2596 * Return target name of a symbolic link.
2598 #ifndef _SYS_SYSPROTO_H_
2599 struct readlink_args {
2606 sys_readlink(struct thread *td, struct readlink_args *uap)
2609 return (kern_readlinkat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2610 uap->buf, UIO_USERSPACE, uap->count));
2612 #ifndef _SYS_SYSPROTO_H_
2613 struct readlinkat_args {
2621 sys_readlinkat(struct thread *td, struct readlinkat_args *uap)
2624 return (kern_readlinkat(td, uap->fd, uap->path, UIO_USERSPACE,
2625 uap->buf, UIO_USERSPACE, uap->bufsize));
2629 kern_readlinkat(struct thread *td, int fd, const char *path,
2630 enum uio_seg pathseg, char *buf, enum uio_seg bufseg, size_t count)
2633 struct nameidata nd;
2636 if (count > IOSIZE_MAX)
2639 NDINIT_AT(&nd, LOOKUP, NOFOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
2640 pathseg, path, fd, td);
2642 if ((error = namei(&nd)) != 0)
2644 NDFREE_NOTHING(&nd);
2647 error = kern_readlink_vp(vp, buf, bufseg, count, td);
2654 * Helper function to readlink from a vnode
2657 kern_readlink_vp(struct vnode *vp, char *buf, enum uio_seg bufseg, size_t count,
2664 ASSERT_VOP_LOCKED(vp, "kern_readlink_vp(): vp not locked");
2666 error = mac_vnode_check_readlink(td->td_ucred, vp);
2670 if (vp->v_type != VLNK && (vp->v_vflag & VV_READLINK) == 0)
2673 aiov.iov_base = buf;
2674 aiov.iov_len = count;
2675 auio.uio_iov = &aiov;
2676 auio.uio_iovcnt = 1;
2677 auio.uio_offset = 0;
2678 auio.uio_rw = UIO_READ;
2679 auio.uio_segflg = bufseg;
2681 auio.uio_resid = count;
2682 error = VOP_READLINK(vp, &auio, td->td_ucred);
2683 td->td_retval[0] = count - auio.uio_resid;
2688 * Common implementation code for chflags() and fchflags().
2691 setfflags(struct thread *td, struct vnode *vp, u_long flags)
2697 /* We can't support the value matching VNOVAL. */
2698 if (flags == VNOVAL)
2699 return (EOPNOTSUPP);
2702 * Prevent non-root users from setting flags on devices. When
2703 * a device is reused, users can retain ownership of the device
2704 * if they are allowed to set flags and programs assume that
2705 * chown can't fail when done as root.
2707 if (vp->v_type == VCHR || vp->v_type == VBLK) {
2708 error = priv_check(td, PRIV_VFS_CHFLAGS_DEV);
2713 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2716 vattr.va_flags = flags;
2717 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2719 error = mac_vnode_check_setflags(td->td_ucred, vp, vattr.va_flags);
2722 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
2724 vn_finished_write(mp);
2729 * Change flags of a file given a path name.
2731 #ifndef _SYS_SYSPROTO_H_
2732 struct chflags_args {
2738 sys_chflags(struct thread *td, struct chflags_args *uap)
2741 return (kern_chflagsat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2745 #ifndef _SYS_SYSPROTO_H_
2746 struct chflagsat_args {
2754 sys_chflagsat(struct thread *td, struct chflagsat_args *uap)
2757 if ((uap->atflag & ~(AT_SYMLINK_NOFOLLOW | AT_RESOLVE_BENEATH |
2758 AT_EMPTY_PATH)) != 0)
2761 return (kern_chflagsat(td, uap->fd, uap->path, UIO_USERSPACE,
2762 uap->flags, uap->atflag));
2766 * Same as chflags() but doesn't follow symlinks.
2768 #ifndef _SYS_SYSPROTO_H_
2769 struct lchflags_args {
2775 sys_lchflags(struct thread *td, struct lchflags_args *uap)
2778 return (kern_chflagsat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2779 uap->flags, AT_SYMLINK_NOFOLLOW));
2783 kern_chflagsat(struct thread *td, int fd, const char *path,
2784 enum uio_seg pathseg, u_long flags, int atflag)
2786 struct nameidata nd;
2789 AUDIT_ARG_FFLAGS(flags);
2790 NDINIT_ATRIGHTS(&nd, LOOKUP, at2cnpflags(atflag, AT_SYMLINK_NOFOLLOW |
2791 AT_RESOLVE_BENEATH | AT_EMPTY_PATH) | AUDITVNODE1, pathseg, path,
2792 fd, &cap_fchflags_rights, td);
2793 if ((error = namei(&nd)) != 0)
2795 NDFREE_NOTHING(&nd);
2796 error = setfflags(td, nd.ni_vp, flags);
2802 * Change flags of a file given a file descriptor.
2804 #ifndef _SYS_SYSPROTO_H_
2805 struct fchflags_args {
2811 sys_fchflags(struct thread *td, struct fchflags_args *uap)
2816 AUDIT_ARG_FD(uap->fd);
2817 AUDIT_ARG_FFLAGS(uap->flags);
2818 error = getvnode(td, uap->fd, &cap_fchflags_rights,
2823 if (AUDITING_TD(td)) {
2824 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
2825 AUDIT_ARG_VNODE1(fp->f_vnode);
2826 VOP_UNLOCK(fp->f_vnode);
2829 error = setfflags(td, fp->f_vnode, uap->flags);
2835 * Common implementation code for chmod(), lchmod() and fchmod().
2838 setfmode(struct thread *td, struct ucred *cred, struct vnode *vp, int mode)
2844 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2846 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2848 vattr.va_mode = mode & ALLPERMS;
2850 error = mac_vnode_check_setmode(cred, vp, vattr.va_mode);
2853 error = VOP_SETATTR(vp, &vattr, cred);
2855 vn_finished_write(mp);
2860 * Change mode of a file given path name.
2862 #ifndef _SYS_SYSPROTO_H_
2869 sys_chmod(struct thread *td, struct chmod_args *uap)
2872 return (kern_fchmodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2876 #ifndef _SYS_SYSPROTO_H_
2877 struct fchmodat_args {
2885 sys_fchmodat(struct thread *td, struct fchmodat_args *uap)
2888 if ((uap->flag & ~(AT_SYMLINK_NOFOLLOW | AT_RESOLVE_BENEATH |
2889 AT_EMPTY_PATH)) != 0)
2892 return (kern_fchmodat(td, uap->fd, uap->path, UIO_USERSPACE,
2893 uap->mode, uap->flag));
2897 * Change mode of a file given path name (don't follow links.)
2899 #ifndef _SYS_SYSPROTO_H_
2900 struct lchmod_args {
2906 sys_lchmod(struct thread *td, struct lchmod_args *uap)
2909 return (kern_fchmodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2910 uap->mode, AT_SYMLINK_NOFOLLOW));
2914 kern_fchmodat(struct thread *td, int fd, const char *path,
2915 enum uio_seg pathseg, mode_t mode, int flag)
2917 struct nameidata nd;
2920 AUDIT_ARG_MODE(mode);
2921 NDINIT_ATRIGHTS(&nd, LOOKUP, at2cnpflags(flag, AT_SYMLINK_NOFOLLOW |
2922 AT_RESOLVE_BENEATH | AT_EMPTY_PATH) | AUDITVNODE1, pathseg, path,
2923 fd, &cap_fchmod_rights, td);
2924 if ((error = namei(&nd)) != 0)
2926 NDFREE_NOTHING(&nd);
2927 error = setfmode(td, td->td_ucred, nd.ni_vp, mode);
2933 * Change mode of a file given a file descriptor.
2935 #ifndef _SYS_SYSPROTO_H_
2936 struct fchmod_args {
2942 sys_fchmod(struct thread *td, struct fchmod_args *uap)
2947 AUDIT_ARG_FD(uap->fd);
2948 AUDIT_ARG_MODE(uap->mode);
2950 error = fget(td, uap->fd, &cap_fchmod_rights, &fp);
2953 error = fo_chmod(fp, uap->mode, td->td_ucred, td);
2959 * Common implementation for chown(), lchown(), and fchown()
2962 setfown(struct thread *td, struct ucred *cred, struct vnode *vp, uid_t uid,
2969 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2971 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2976 error = mac_vnode_check_setowner(cred, vp, vattr.va_uid,
2980 error = VOP_SETATTR(vp, &vattr, cred);
2982 vn_finished_write(mp);
2987 * Set ownership given a path name.
2989 #ifndef _SYS_SYSPROTO_H_
2997 sys_chown(struct thread *td, struct chown_args *uap)
3000 return (kern_fchownat(td, AT_FDCWD, uap->path, UIO_USERSPACE, uap->uid,
3004 #ifndef _SYS_SYSPROTO_H_
3005 struct fchownat_args {
3014 sys_fchownat(struct thread *td, struct fchownat_args *uap)
3017 if ((uap->flag & ~(AT_SYMLINK_NOFOLLOW | AT_RESOLVE_BENEATH |
3018 AT_EMPTY_PATH)) != 0)
3021 return (kern_fchownat(td, uap->fd, uap->path, UIO_USERSPACE, uap->uid,
3022 uap->gid, uap->flag));
3026 kern_fchownat(struct thread *td, int fd, const char *path,
3027 enum uio_seg pathseg, int uid, int gid, int flag)
3029 struct nameidata nd;
3032 AUDIT_ARG_OWNER(uid, gid);
3033 NDINIT_ATRIGHTS(&nd, LOOKUP, at2cnpflags(flag, AT_SYMLINK_NOFOLLOW |
3034 AT_RESOLVE_BENEATH | AT_EMPTY_PATH) | AUDITVNODE1, pathseg, path,
3035 fd, &cap_fchown_rights, td);
3037 if ((error = namei(&nd)) != 0)
3039 NDFREE_NOTHING(&nd);
3040 error = setfown(td, td->td_ucred, nd.ni_vp, uid, gid);
3046 * Set ownership given a path name, do not cross symlinks.
3048 #ifndef _SYS_SYSPROTO_H_
3049 struct lchown_args {
3056 sys_lchown(struct thread *td, struct lchown_args *uap)
3059 return (kern_fchownat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
3060 uap->uid, uap->gid, AT_SYMLINK_NOFOLLOW));
3064 * Set ownership given a file descriptor.
3066 #ifndef _SYS_SYSPROTO_H_
3067 struct fchown_args {
3074 sys_fchown(struct thread *td, struct fchown_args *uap)
3079 AUDIT_ARG_FD(uap->fd);
3080 AUDIT_ARG_OWNER(uap->uid, uap->gid);
3081 error = fget(td, uap->fd, &cap_fchown_rights, &fp);
3084 error = fo_chown(fp, uap->uid, uap->gid, td->td_ucred, td);
3090 * Common implementation code for utimes(), lutimes(), and futimes().
3093 getutimes(const struct timeval *usrtvp, enum uio_seg tvpseg,
3094 struct timespec *tsp)
3096 struct timeval tv[2];
3097 const struct timeval *tvp;
3100 if (usrtvp == NULL) {
3101 vfs_timestamp(&tsp[0]);
3104 if (tvpseg == UIO_SYSSPACE) {
3107 if ((error = copyin(usrtvp, tv, sizeof(tv))) != 0)
3112 if (tvp[0].tv_usec < 0 || tvp[0].tv_usec >= 1000000 ||
3113 tvp[1].tv_usec < 0 || tvp[1].tv_usec >= 1000000)
3115 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
3116 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
3122 * Common implementation code for futimens(), utimensat().
3124 #define UTIMENS_NULL 0x1
3125 #define UTIMENS_EXIT 0x2
3127 getutimens(const struct timespec *usrtsp, enum uio_seg tspseg,
3128 struct timespec *tsp, int *retflags)
3130 struct timespec tsnow;
3133 vfs_timestamp(&tsnow);
3135 if (usrtsp == NULL) {
3138 *retflags |= UTIMENS_NULL;
3141 if (tspseg == UIO_SYSSPACE) {
3144 } else if ((error = copyin(usrtsp, tsp, sizeof(*tsp) * 2)) != 0)
3146 if (tsp[0].tv_nsec == UTIME_OMIT && tsp[1].tv_nsec == UTIME_OMIT)
3147 *retflags |= UTIMENS_EXIT;
3148 if (tsp[0].tv_nsec == UTIME_NOW && tsp[1].tv_nsec == UTIME_NOW)
3149 *retflags |= UTIMENS_NULL;
3150 if (tsp[0].tv_nsec == UTIME_OMIT)
3151 tsp[0].tv_sec = VNOVAL;
3152 else if (tsp[0].tv_nsec == UTIME_NOW)
3154 else if (tsp[0].tv_nsec < 0 || tsp[0].tv_nsec >= 1000000000L)
3156 if (tsp[1].tv_nsec == UTIME_OMIT)
3157 tsp[1].tv_sec = VNOVAL;
3158 else if (tsp[1].tv_nsec == UTIME_NOW)
3160 else if (tsp[1].tv_nsec < 0 || tsp[1].tv_nsec >= 1000000000L)
3167 * Common implementation code for utimes(), lutimes(), futimes(), futimens(),
3171 setutimes(struct thread *td, struct vnode *vp, const struct timespec *ts,
3172 int numtimes, int nullflag)
3176 int error, setbirthtime;
3178 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
3180 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3182 if (numtimes < 3 && !VOP_GETATTR(vp, &vattr, td->td_ucred) &&
3183 timespeccmp(&ts[1], &vattr.va_birthtime, < ))
3186 vattr.va_atime = ts[0];
3187 vattr.va_mtime = ts[1];
3189 vattr.va_birthtime = ts[1];
3191 vattr.va_birthtime = ts[2];
3193 vattr.va_vaflags |= VA_UTIMES_NULL;
3195 error = mac_vnode_check_setutimes(td->td_ucred, vp, vattr.va_atime,
3199 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3201 vn_finished_write(mp);
3206 * Set the access and modification times of a file.
3208 #ifndef _SYS_SYSPROTO_H_
3209 struct utimes_args {
3211 struct timeval *tptr;
3215 sys_utimes(struct thread *td, struct utimes_args *uap)
3218 return (kern_utimesat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
3219 uap->tptr, UIO_USERSPACE));
3222 #ifndef _SYS_SYSPROTO_H_
3223 struct futimesat_args {
3226 const struct timeval * times;
3230 sys_futimesat(struct thread *td, struct futimesat_args *uap)
3233 return (kern_utimesat(td, uap->fd, uap->path, UIO_USERSPACE,
3234 uap->times, UIO_USERSPACE));
3238 kern_utimesat(struct thread *td, int fd, const char *path,
3239 enum uio_seg pathseg, struct timeval *tptr, enum uio_seg tptrseg)
3241 struct nameidata nd;
3242 struct timespec ts[2];
3245 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3247 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, fd,
3248 &cap_futimes_rights, td);
3250 if ((error = namei(&nd)) != 0)
3252 NDFREE_NOTHING(&nd);
3253 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3259 * Set the access and modification times of a file.
3261 #ifndef _SYS_SYSPROTO_H_
3262 struct lutimes_args {
3264 struct timeval *tptr;
3268 sys_lutimes(struct thread *td, struct lutimes_args *uap)
3271 return (kern_lutimes(td, uap->path, UIO_USERSPACE, uap->tptr,
3276 kern_lutimes(struct thread *td, const char *path, enum uio_seg pathseg,
3277 struct timeval *tptr, enum uio_seg tptrseg)
3279 struct timespec ts[2];
3280 struct nameidata nd;
3283 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3285 NDINIT(&nd, LOOKUP, NOFOLLOW | AUDITVNODE1, pathseg, path, td);
3286 if ((error = namei(&nd)) != 0)
3288 NDFREE_NOTHING(&nd);
3289 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3295 * Set the access and modification times of a file.
3297 #ifndef _SYS_SYSPROTO_H_
3298 struct futimes_args {
3300 struct timeval *tptr;
3304 sys_futimes(struct thread *td, struct futimes_args *uap)
3307 return (kern_futimes(td, uap->fd, uap->tptr, UIO_USERSPACE));
3311 kern_futimes(struct thread *td, int fd, struct timeval *tptr,
3312 enum uio_seg tptrseg)
3314 struct timespec ts[2];
3319 error = getutimes(tptr, tptrseg, ts);
3322 error = getvnode(td, fd, &cap_futimes_rights, &fp);
3326 if (AUDITING_TD(td)) {
3327 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
3328 AUDIT_ARG_VNODE1(fp->f_vnode);
3329 VOP_UNLOCK(fp->f_vnode);
3332 error = setutimes(td, fp->f_vnode, ts, 2, tptr == NULL);
3338 sys_futimens(struct thread *td, struct futimens_args *uap)
3341 return (kern_futimens(td, uap->fd, uap->times, UIO_USERSPACE));
3345 kern_futimens(struct thread *td, int fd, struct timespec *tptr,
3346 enum uio_seg tptrseg)
3348 struct timespec ts[2];
3353 error = getutimens(tptr, tptrseg, ts, &flags);
3356 if (flags & UTIMENS_EXIT)
3358 error = getvnode(td, fd, &cap_futimes_rights, &fp);
3362 if (AUDITING_TD(td)) {
3363 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
3364 AUDIT_ARG_VNODE1(fp->f_vnode);
3365 VOP_UNLOCK(fp->f_vnode);
3368 error = setutimes(td, fp->f_vnode, ts, 2, flags & UTIMENS_NULL);
3374 sys_utimensat(struct thread *td, struct utimensat_args *uap)
3377 return (kern_utimensat(td, uap->fd, uap->path, UIO_USERSPACE,
3378 uap->times, UIO_USERSPACE, uap->flag));
3382 kern_utimensat(struct thread *td, int fd, const char *path,
3383 enum uio_seg pathseg, struct timespec *tptr, enum uio_seg tptrseg,
3386 struct nameidata nd;
3387 struct timespec ts[2];
3390 if ((flag & ~(AT_SYMLINK_NOFOLLOW | AT_RESOLVE_BENEATH |
3391 AT_EMPTY_PATH)) != 0)
3394 if ((error = getutimens(tptr, tptrseg, ts, &flags)) != 0)
3396 NDINIT_ATRIGHTS(&nd, LOOKUP, at2cnpflags(flag, AT_SYMLINK_NOFOLLOW |
3397 AT_RESOLVE_BENEATH | AT_EMPTY_PATH) | AUDITVNODE1,
3398 pathseg, path, fd, &cap_futimes_rights, td);
3399 if ((error = namei(&nd)) != 0)
3402 * We are allowed to call namei() regardless of 2xUTIME_OMIT.
3404 * "If both tv_nsec fields are UTIME_OMIT... EACCESS may be detected."
3405 * "Search permission is denied by a component of the path prefix."
3407 NDFREE_NOTHING(&nd);
3408 if ((flags & UTIMENS_EXIT) == 0)
3409 error = setutimes(td, nd.ni_vp, ts, 2, flags & UTIMENS_NULL);
3415 * Truncate a file given its path name.
3417 #ifndef _SYS_SYSPROTO_H_
3418 struct truncate_args {
3425 sys_truncate(struct thread *td, struct truncate_args *uap)
3428 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3432 kern_truncate(struct thread *td, const char *path, enum uio_seg pathseg,
3439 struct nameidata nd;
3446 NDINIT(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, td);
3447 if ((error = namei(&nd)) != 0)
3450 rl_cookie = vn_rangelock_wlock(vp, 0, OFF_MAX);
3451 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0) {
3452 vn_rangelock_unlock(vp, rl_cookie);
3456 NDFREE(&nd, NDF_ONLY_PNBUF);
3457 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3458 if (vp->v_type == VDIR)
3461 else if ((error = mac_vnode_check_write(td->td_ucred, NOCRED, vp))) {
3464 else if ((error = vn_writechk(vp)) == 0 &&
3465 (error = VOP_ACCESS(vp, VWRITE, td->td_ucred, td)) == 0) {
3467 vattr.va_size = length;
3468 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3471 vn_finished_write(mp);
3472 vn_rangelock_unlock(vp, rl_cookie);
3474 if (error == ERELOOKUP)
3479 #if defined(COMPAT_43)
3481 * Truncate a file given its path name.
3483 #ifndef _SYS_SYSPROTO_H_
3484 struct otruncate_args {
3490 otruncate(struct thread *td, struct otruncate_args *uap)
3493 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3495 #endif /* COMPAT_43 */
3497 #if defined(COMPAT_FREEBSD6)
3498 /* Versions with the pad argument */
3500 freebsd6_truncate(struct thread *td, struct freebsd6_truncate_args *uap)
3503 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3507 freebsd6_ftruncate(struct thread *td, struct freebsd6_ftruncate_args *uap)
3510 return (kern_ftruncate(td, uap->fd, uap->length));
3515 kern_fsync(struct thread *td, int fd, bool fullsync)
3520 int error, lock_flags;
3523 error = getvnode(td, fd, &cap_fsync_rights, &fp);
3529 /* XXXKIB: compete outstanding aio writes */;
3532 error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
3535 if (MNT_SHARED_WRITES(mp) ||
3536 ((mp == NULL) && MNT_SHARED_WRITES(vp->v_mount))) {
3537 lock_flags = LK_SHARED;
3539 lock_flags = LK_EXCLUSIVE;
3541 vn_lock(vp, lock_flags | LK_RETRY);
3542 AUDIT_ARG_VNODE1(vp);
3543 if (vp->v_object != NULL) {
3544 VM_OBJECT_WLOCK(vp->v_object);
3545 vm_object_page_clean(vp->v_object, 0, 0, 0);
3546 VM_OBJECT_WUNLOCK(vp->v_object);
3548 error = fullsync ? VOP_FSYNC(vp, MNT_WAIT, td) : VOP_FDATASYNC(vp, td);
3550 vn_finished_write(mp);
3551 if (error == ERELOOKUP)
3559 * Sync an open file.
3561 #ifndef _SYS_SYSPROTO_H_
3567 sys_fsync(struct thread *td, struct fsync_args *uap)
3570 return (kern_fsync(td, uap->fd, true));
3574 sys_fdatasync(struct thread *td, struct fdatasync_args *uap)
3577 return (kern_fsync(td, uap->fd, false));
3581 * Rename files. Source and destination must either both be directories, or
3582 * both not be directories. If target is a directory, it must be empty.
3584 #ifndef _SYS_SYSPROTO_H_
3585 struct rename_args {
3591 sys_rename(struct thread *td, struct rename_args *uap)
3594 return (kern_renameat(td, AT_FDCWD, uap->from, AT_FDCWD,
3595 uap->to, UIO_USERSPACE));
3598 #ifndef _SYS_SYSPROTO_H_
3599 struct renameat_args {
3607 sys_renameat(struct thread *td, struct renameat_args *uap)
3610 return (kern_renameat(td, uap->oldfd, uap->old, uap->newfd, uap->new,
3616 kern_renameat_mac(struct thread *td, int oldfd, const char *old, int newfd,
3617 const char *new, enum uio_seg pathseg, struct nameidata *fromnd)
3621 NDINIT_ATRIGHTS(fromnd, DELETE, LOCKPARENT | LOCKLEAF | SAVESTART |
3622 AUDITVNODE1, pathseg, old, oldfd, &cap_renameat_source_rights, td);
3623 if ((error = namei(fromnd)) != 0)
3625 error = mac_vnode_check_rename_from(td->td_ucred, fromnd->ni_dvp,
3626 fromnd->ni_vp, &fromnd->ni_cnd);
3627 VOP_UNLOCK(fromnd->ni_dvp);
3628 if (fromnd->ni_dvp != fromnd->ni_vp)
3629 VOP_UNLOCK(fromnd->ni_vp);
3631 NDFREE(fromnd, NDF_ONLY_PNBUF);
3632 vrele(fromnd->ni_dvp);
3633 vrele(fromnd->ni_vp);
3634 if (fromnd->ni_startdir)
3635 vrele(fromnd->ni_startdir);
3642 kern_renameat(struct thread *td, int oldfd, const char *old, int newfd,
3643 const char *new, enum uio_seg pathseg)
3645 struct mount *mp = NULL;
3646 struct vnode *tvp, *fvp, *tdvp;
3647 struct nameidata fromnd, tond;
3648 u_int64_t tondflags;
3654 if (mac_vnode_check_rename_from_enabled()) {
3655 error = kern_renameat_mac(td, oldfd, old, newfd, new, pathseg,
3661 NDINIT_ATRIGHTS(&fromnd, DELETE, WANTPARENT | SAVESTART | AUDITVNODE1,
3662 pathseg, old, oldfd, &cap_renameat_source_rights, td);
3663 if ((error = namei(&fromnd)) != 0)
3669 tondflags = LOCKPARENT | LOCKLEAF | NOCACHE | SAVESTART | AUDITVNODE2;
3670 if (fromnd.ni_vp->v_type == VDIR)
3671 tondflags |= WILLBEDIR;
3672 NDINIT_ATRIGHTS(&tond, RENAME, tondflags, pathseg, new, newfd,
3673 &cap_renameat_target_rights, td);
3674 if ((error = namei(&tond)) != 0) {
3675 /* Translate error code for rename("dir1", "dir2/."). */
3676 if (error == EISDIR && fvp->v_type == VDIR)
3678 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3679 vrele(fromnd.ni_dvp);
3685 error = vn_start_write(fvp, &mp, V_NOWAIT);
3687 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3688 NDFREE(&tond, NDF_ONLY_PNBUF);
3695 vrele(fromnd.ni_dvp);
3697 vrele(tond.ni_startdir);
3698 if (fromnd.ni_startdir != NULL)
3699 vrele(fromnd.ni_startdir);
3700 error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH);
3706 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
3709 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
3714 if (newfd != AT_FDCWD && (tond.ni_resflags & NIRES_ABS) == 0) {
3716 * If the target already exists we require CAP_UNLINKAT
3717 * from 'newfd', when newfd was used for the lookup.
3719 error = cap_check(&tond.ni_filecaps.fc_rights,
3720 &cap_unlinkat_rights);
3731 * If the source is the same as the destination (that is, if they
3732 * are links to the same vnode), then there is nothing to do.
3738 error = mac_vnode_check_rename_to(td->td_ucred, tdvp,
3739 tond.ni_vp, fromnd.ni_dvp == tdvp, &tond.ni_cnd);
3743 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
3744 tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
3745 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3746 NDFREE(&tond, NDF_ONLY_PNBUF);
3748 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3749 NDFREE(&tond, NDF_ONLY_PNBUF);
3756 vrele(fromnd.ni_dvp);
3759 vrele(tond.ni_startdir);
3760 vn_finished_write(mp);
3762 if (fromnd.ni_startdir)
3763 vrele(fromnd.ni_startdir);
3764 if (error == ERESTART)
3766 if (error == ERELOOKUP)
3772 * Make a directory file.
3774 #ifndef _SYS_SYSPROTO_H_
3781 sys_mkdir(struct thread *td, struct mkdir_args *uap)
3784 return (kern_mkdirat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
3788 #ifndef _SYS_SYSPROTO_H_
3789 struct mkdirat_args {
3796 sys_mkdirat(struct thread *td, struct mkdirat_args *uap)
3799 return (kern_mkdirat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode));
3803 kern_mkdirat(struct thread *td, int fd, const char *path, enum uio_seg segflg,
3808 struct nameidata nd;
3811 AUDIT_ARG_MODE(mode);
3815 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
3816 NC_NOMAKEENTRY | NC_KEEPPOSENTRY | FAILIFEXISTS | WILLBEDIR,
3817 segflg, path, fd, &cap_mkdirat_rights, td);
3818 if ((error = namei(&nd)) != 0)
3820 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3821 NDFREE(&nd, NDF_ONLY_PNBUF);
3823 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3828 vattr.va_type = VDIR;
3829 vattr.va_mode = (mode & ACCESSPERMS) &~ td->td_proc->p_pd->pd_cmask;
3831 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
3836 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
3840 NDFREE(&nd, NDF_ONLY_PNBUF);
3841 VOP_VPUT_PAIR(nd.ni_dvp, error == 0 ? &nd.ni_vp : NULL, true);
3842 vn_finished_write(mp);
3843 if (error == ERELOOKUP)
3849 * Remove a directory file.
3851 #ifndef _SYS_SYSPROTO_H_
3857 sys_rmdir(struct thread *td, struct rmdir_args *uap)
3860 return (kern_frmdirat(td, AT_FDCWD, uap->path, FD_NONE, UIO_USERSPACE,
3865 kern_frmdirat(struct thread *td, int dfd, const char *path, int fd,
3866 enum uio_seg pathseg, int flag)
3871 struct nameidata nd;
3872 cap_rights_t rights;
3876 if (fd != FD_NONE) {
3877 error = getvnode(td, fd, cap_rights_init_one(&rights,
3886 NDINIT_ATRIGHTS(&nd, DELETE, LOCKPARENT | LOCKLEAF | AUDITVNODE1 |
3887 at2cnpflags(flag, AT_RESOLVE_BENEATH),
3888 pathseg, path, dfd, &cap_unlinkat_rights, td);
3889 if ((error = namei(&nd)) != 0)
3892 if (vp->v_type != VDIR) {
3897 * No rmdir "." please.
3899 if (nd.ni_dvp == vp) {
3904 * The root of a mounted filesystem cannot be deleted.
3906 if (vp->v_vflag & VV_ROOT) {
3911 if (fp != NULL && fp->f_vnode != vp) {
3912 if (VN_IS_DOOMED(fp->f_vnode))
3920 error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp,
3925 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3926 NDFREE(&nd, NDF_ONLY_PNBUF);
3928 if (nd.ni_dvp == vp)
3932 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3936 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_UNLINK);
3937 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
3938 vn_finished_write(mp);
3940 NDFREE(&nd, NDF_ONLY_PNBUF);
3942 if (nd.ni_dvp == vp)
3946 if (error == ERELOOKUP)
3954 #if defined(COMPAT_43) || defined(COMPAT_FREEBSD11)
3956 freebsd11_kern_getdirentries(struct thread *td, int fd, char *ubuf, u_int count,
3957 long *basep, void (*func)(struct freebsd11_dirent *))
3959 struct freebsd11_dirent dstdp;
3960 struct dirent *dp, *edp;
3963 ssize_t resid, ucount;
3966 /* XXX arbitrary sanity limit on `count'. */
3967 count = min(count, 64 * 1024);
3969 dirbuf = malloc(count, M_TEMP, M_WAITOK);
3971 error = kern_getdirentries(td, fd, dirbuf, count, &base, &resid,
3979 for (dp = (struct dirent *)dirbuf,
3980 edp = (struct dirent *)&dirbuf[count - resid];
3981 ucount < count && dp < edp; ) {
3982 if (dp->d_reclen == 0)
3984 MPASS(dp->d_reclen >= _GENERIC_DIRLEN(0));
3985 if (dp->d_namlen >= sizeof(dstdp.d_name))
3987 dstdp.d_type = dp->d_type;
3988 dstdp.d_namlen = dp->d_namlen;
3989 dstdp.d_fileno = dp->d_fileno; /* truncate */
3990 if (dstdp.d_fileno != dp->d_fileno) {
3991 switch (ino64_trunc_error) {
3999 dstdp.d_fileno = UINT32_MAX;
4003 dstdp.d_reclen = sizeof(dstdp) - sizeof(dstdp.d_name) +
4004 ((dp->d_namlen + 1 + 3) &~ 3);
4005 bcopy(dp->d_name, dstdp.d_name, dstdp.d_namlen);
4006 bzero(dstdp.d_name + dstdp.d_namlen,
4007 dstdp.d_reclen - offsetof(struct freebsd11_dirent, d_name) -
4009 MPASS(dstdp.d_reclen <= dp->d_reclen);
4010 MPASS(ucount + dstdp.d_reclen <= count);
4013 error = copyout(&dstdp, ubuf + ucount, dstdp.d_reclen);
4016 dp = (struct dirent *)((char *)dp + dp->d_reclen);
4017 ucount += dstdp.d_reclen;
4021 free(dirbuf, M_TEMP);
4023 td->td_retval[0] = ucount;
4030 ogetdirentries_cvt(struct freebsd11_dirent *dp)
4032 #if (BYTE_ORDER == LITTLE_ENDIAN)
4034 * The expected low byte of dp->d_namlen is our dp->d_type.
4035 * The high MBZ byte of dp->d_namlen is our dp->d_namlen.
4037 dp->d_type = dp->d_namlen;
4041 * The dp->d_type is the high byte of the expected dp->d_namlen,
4042 * so must be zero'ed.
4049 * Read a block of directory entries in a filesystem independent format.
4051 #ifndef _SYS_SYSPROTO_H_
4052 struct ogetdirentries_args {
4060 ogetdirentries(struct thread *td, struct ogetdirentries_args *uap)
4065 error = kern_ogetdirentries(td, uap, &loff);
4067 error = copyout(&loff, uap->basep, sizeof(long));
4072 kern_ogetdirentries(struct thread *td, struct ogetdirentries_args *uap,
4078 /* XXX arbitrary sanity limit on `count'. */
4079 if (uap->count > 64 * 1024)
4082 error = freebsd11_kern_getdirentries(td, uap->fd, uap->buf, uap->count,
4083 &base, ogetdirentries_cvt);
4085 if (error == 0 && uap->basep != NULL)
4086 error = copyout(&base, uap->basep, sizeof(long));
4090 #endif /* COMPAT_43 */
4092 #if defined(COMPAT_FREEBSD11)
4093 #ifndef _SYS_SYSPROTO_H_
4094 struct freebsd11_getdirentries_args {
4102 freebsd11_getdirentries(struct thread *td,
4103 struct freebsd11_getdirentries_args *uap)
4108 error = freebsd11_kern_getdirentries(td, uap->fd, uap->buf, uap->count,
4111 if (error == 0 && uap->basep != NULL)
4112 error = copyout(&base, uap->basep, sizeof(long));
4117 freebsd11_getdents(struct thread *td, struct freebsd11_getdents_args *uap)
4119 struct freebsd11_getdirentries_args ap;
4123 ap.count = uap->count;
4125 return (freebsd11_getdirentries(td, &ap));
4127 #endif /* COMPAT_FREEBSD11 */
4130 * Read a block of directory entries in a filesystem independent format.
4133 sys_getdirentries(struct thread *td, struct getdirentries_args *uap)
4138 error = kern_getdirentries(td, uap->fd, uap->buf, uap->count, &base,
4139 NULL, UIO_USERSPACE);
4142 if (uap->basep != NULL)
4143 error = copyout(&base, uap->basep, sizeof(off_t));
4148 kern_getdirentries(struct thread *td, int fd, char *buf, size_t count,
4149 off_t *basep, ssize_t *residp, enum uio_seg bufseg)
4160 if (count > IOSIZE_MAX)
4162 auio.uio_resid = count;
4163 error = getvnode(td, fd, &cap_read_rights, &fp);
4166 if ((fp->f_flag & FREAD) == 0) {
4171 foffset = foffset_lock(fp, 0);
4173 if (vp->v_type != VDIR) {
4177 aiov.iov_base = buf;
4178 aiov.iov_len = count;
4179 auio.uio_iov = &aiov;
4180 auio.uio_iovcnt = 1;
4181 auio.uio_rw = UIO_READ;
4182 auio.uio_segflg = bufseg;
4184 vn_lock(vp, LK_SHARED | LK_RETRY);
4185 AUDIT_ARG_VNODE1(vp);
4186 loff = auio.uio_offset = foffset;
4188 error = mac_vnode_check_readdir(td->td_ucred, vp);
4191 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL,
4193 foffset = auio.uio_offset;
4198 if (count == auio.uio_resid &&
4199 (vp->v_vflag & VV_ROOT) &&
4200 (vp->v_mount->mnt_flag & MNT_UNION)) {
4201 struct vnode *tvp = vp;
4203 vp = vp->v_mount->mnt_vnodecovered;
4213 *residp = auio.uio_resid;
4214 td->td_retval[0] = count - auio.uio_resid;
4216 foffset_unlock(fp, foffset, 0);
4222 * Set the mode mask for creation of filesystem nodes.
4224 #ifndef _SYS_SYSPROTO_H_
4230 sys_umask(struct thread *td, struct umask_args *uap)
4232 struct pwddesc *pdp;
4234 pdp = td->td_proc->p_pd;
4236 td->td_retval[0] = pdp->pd_cmask;
4237 pdp->pd_cmask = uap->newmask & ALLPERMS;
4238 PWDDESC_XUNLOCK(pdp);
4243 * Void all references to file by ripping underlying filesystem away from
4246 #ifndef _SYS_SYSPROTO_H_
4247 struct revoke_args {
4252 sys_revoke(struct thread *td, struct revoke_args *uap)
4256 struct nameidata nd;
4259 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
4261 if ((error = namei(&nd)) != 0)
4264 NDFREE_NOTHING(&nd);
4265 if (vp->v_type != VCHR || vp->v_rdev == NULL) {
4270 error = mac_vnode_check_revoke(td->td_ucred, vp);
4274 error = VOP_GETATTR(vp, &vattr, td->td_ucred);
4277 if (td->td_ucred->cr_uid != vattr.va_uid) {
4278 error = priv_check(td, PRIV_VFS_ADMIN);
4282 if (devfs_usecount(vp) > 0)
4283 VOP_REVOKE(vp, REVOKEALL);
4290 * This variant of getvnode() allows O_PATH files. Caller should
4291 * ensure that returned file and vnode are only used for compatible
4295 getvnode_path(struct thread *td, int fd, cap_rights_t *rightsp,
4301 error = fget_unlocked(td->td_proc->p_fd, fd, rightsp, &fp);
4306 * The file could be not of the vnode type, or it may be not
4307 * yet fully initialized, in which case the f_vnode pointer
4308 * may be set, but f_ops is still badfileops. E.g.,
4309 * devfs_open() transiently create such situation to
4310 * facilitate csw d_fdopen().
4312 * Dupfdopen() handling in kern_openat() installs the
4313 * half-baked file into the process descriptor table, allowing
4314 * other thread to dereference it. Guard against the race by
4317 if (fp->f_vnode == NULL || fp->f_ops == &badfileops) {
4327 * Convert a user file descriptor to a kernel file entry and check
4328 * that, if it is a capability, the correct rights are present.
4329 * A reference on the file entry is held upon returning.
4332 getvnode(struct thread *td, int fd, cap_rights_t *rightsp, struct file **fpp)
4336 error = getvnode_path(td, fd, rightsp, fpp);
4339 * Filter out O_PATH file descriptors, most getvnode() callers
4340 * do not call fo_ methods.
4342 if (error == 0 && (*fpp)->f_ops == &path_fileops) {
4351 * Get an (NFS) file handle.
4353 #ifndef _SYS_SYSPROTO_H_
4354 struct lgetfh_args {
4360 sys_lgetfh(struct thread *td, struct lgetfh_args *uap)
4363 return (kern_getfhat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->fname,
4364 UIO_USERSPACE, uap->fhp, UIO_USERSPACE));
4367 #ifndef _SYS_SYSPROTO_H_
4374 sys_getfh(struct thread *td, struct getfh_args *uap)
4377 return (kern_getfhat(td, 0, AT_FDCWD, uap->fname, UIO_USERSPACE,
4378 uap->fhp, UIO_USERSPACE));
4382 * syscall for the rpc.lockd to use to translate an open descriptor into
4383 * a NFS file handle.
4385 * warning: do not remove the priv_check() call or this becomes one giant
4388 #ifndef _SYS_SYSPROTO_H_
4389 struct getfhat_args {
4397 sys_getfhat(struct thread *td, struct getfhat_args *uap)
4400 if ((uap->flags & ~(AT_SYMLINK_NOFOLLOW | AT_RESOLVE_BENEATH)) != 0)
4402 return (kern_getfhat(td, uap->flags, uap->fd, uap->path, UIO_USERSPACE,
4403 uap->fhp, UIO_USERSPACE));
4407 kern_getfhat(struct thread *td, int flags, int fd, const char *path,
4408 enum uio_seg pathseg, fhandle_t *fhp, enum uio_seg fhseg)
4410 struct nameidata nd;
4415 error = priv_check(td, PRIV_VFS_GETFH);
4418 NDINIT_AT(&nd, LOOKUP, at2cnpflags(flags, AT_SYMLINK_NOFOLLOW |
4419 AT_RESOLVE_BENEATH) | LOCKLEAF | AUDITVNODE1, pathseg, path,
4424 NDFREE_NOTHING(&nd);
4426 bzero(&fh, sizeof(fh));
4427 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
4428 error = VOP_VPTOFH(vp, &fh.fh_fid);
4431 if (fhseg == UIO_USERSPACE)
4432 error = copyout(&fh, fhp, sizeof (fh));
4434 memcpy(fhp, &fh, sizeof(fh));
4439 #ifndef _SYS_SYSPROTO_H_
4440 struct fhlink_args {
4446 sys_fhlink(struct thread *td, struct fhlink_args *uap)
4449 return (kern_fhlinkat(td, AT_FDCWD, uap->to, UIO_USERSPACE, uap->fhp));
4452 #ifndef _SYS_SYSPROTO_H_
4453 struct fhlinkat_args {
4460 sys_fhlinkat(struct thread *td, struct fhlinkat_args *uap)
4463 return (kern_fhlinkat(td, uap->tofd, uap->to, UIO_USERSPACE, uap->fhp));
4467 kern_fhlinkat(struct thread *td, int fd, const char *path,
4468 enum uio_seg pathseg, fhandle_t *fhp)
4475 error = priv_check(td, PRIV_VFS_GETFH);
4478 error = copyin(fhp, &fh, sizeof(fh));
4483 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4485 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_SHARED, &vp);
4490 error = kern_linkat_vp(td, vp, fd, path, pathseg);
4491 } while (error == EAGAIN || error == ERELOOKUP);
4495 #ifndef _SYS_SYSPROTO_H_
4496 struct fhreadlink_args {
4503 sys_fhreadlink(struct thread *td, struct fhreadlink_args *uap)
4510 error = priv_check(td, PRIV_VFS_GETFH);
4513 if (uap->bufsize > IOSIZE_MAX)
4515 error = copyin(uap->fhp, &fh, sizeof(fh));
4518 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4520 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_SHARED, &vp);
4524 error = kern_readlink_vp(vp, uap->buf, UIO_USERSPACE, uap->bufsize, td);
4530 * syscall for the rpc.lockd to use to translate a NFS file handle into an
4533 * warning: do not remove the priv_check() call or this becomes one giant
4536 #ifndef _SYS_SYSPROTO_H_
4537 struct fhopen_args {
4538 const struct fhandle *u_fhp;
4543 sys_fhopen(struct thread *td, struct fhopen_args *uap)
4545 return (kern_fhopen(td, uap->u_fhp, uap->flags));
4549 kern_fhopen(struct thread *td, const struct fhandle *u_fhp, int flags)
4558 error = priv_check(td, PRIV_VFS_FHOPEN);
4562 fmode = FFLAGS(flags);
4563 /* why not allow a non-read/write open for our lockd? */
4564 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
4566 error = copyin(u_fhp, &fhp, sizeof(fhp));
4569 /* find the mount point */
4570 mp = vfs_busyfs(&fhp.fh_fsid);
4573 /* now give me my vnode, it gets returned to me locked */
4574 error = VFS_FHTOVP(mp, &fhp.fh_fid, LK_EXCLUSIVE, &vp);
4579 error = falloc_noinstall(td, &fp);
4585 * An extra reference on `fp' has been held for us by
4586 * falloc_noinstall().
4592 error = vn_open_vnode(vp, fmode, td->td_ucred, td, fp);
4594 KASSERT(fp->f_ops == &badfileops,
4595 ("VOP_OPEN in fhopen() set f_ops"));
4596 KASSERT(td->td_dupfd < 0,
4597 ("fhopen() encountered fdopen()"));
4606 finit_vnode(fp, fmode, NULL, &vnops);
4608 if ((fmode & O_TRUNC) != 0) {
4609 error = fo_truncate(fp, 0, td->td_ucred, td);
4614 error = finstall(td, fp, &indx, fmode, NULL);
4617 td->td_retval[0] = indx;
4622 * Stat an (NFS) file handle.
4624 #ifndef _SYS_SYSPROTO_H_
4625 struct fhstat_args {
4626 struct fhandle *u_fhp;
4631 sys_fhstat(struct thread *td, struct fhstat_args *uap)
4637 error = copyin(uap->u_fhp, &fh, sizeof(fh));
4640 error = kern_fhstat(td, fh, &sb);
4642 error = copyout(&sb, uap->sb, sizeof(sb));
4647 kern_fhstat(struct thread *td, struct fhandle fh, struct stat *sb)
4653 error = priv_check(td, PRIV_VFS_FHSTAT);
4656 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4658 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_EXCLUSIVE, &vp);
4662 error = VOP_STAT(vp, sb, td->td_ucred, NOCRED, td);
4668 * Implement fstatfs() for (NFS) file handles.
4670 #ifndef _SYS_SYSPROTO_H_
4671 struct fhstatfs_args {
4672 struct fhandle *u_fhp;
4677 sys_fhstatfs(struct thread *td, struct fhstatfs_args *uap)
4683 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4686 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
4687 error = kern_fhstatfs(td, fh, sfp);
4689 error = copyout(sfp, uap->buf, sizeof(*sfp));
4690 free(sfp, M_STATFS);
4695 kern_fhstatfs(struct thread *td, fhandle_t fh, struct statfs *buf)
4701 error = priv_check(td, PRIV_VFS_FHSTATFS);
4704 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4706 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_EXCLUSIVE, &vp);
4712 error = prison_canseemount(td->td_ucred, mp);
4716 error = mac_mount_check_stat(td->td_ucred, mp);
4720 error = VFS_STATFS(mp, buf);
4727 * Unlike madvise(2), we do not make a best effort to remember every
4728 * possible caching hint. Instead, we remember the last setting with
4729 * the exception that we will allow POSIX_FADV_NORMAL to adjust the
4730 * region of any current setting.
4733 kern_posix_fadvise(struct thread *td, int fd, off_t offset, off_t len,
4736 struct fadvise_info *fa, *new;
4742 if (offset < 0 || len < 0 || offset > OFF_MAX - len)
4744 AUDIT_ARG_VALUE(advice);
4746 case POSIX_FADV_SEQUENTIAL:
4747 case POSIX_FADV_RANDOM:
4748 case POSIX_FADV_NOREUSE:
4749 new = malloc(sizeof(*fa), M_FADVISE, M_WAITOK);
4751 case POSIX_FADV_NORMAL:
4752 case POSIX_FADV_WILLNEED:
4753 case POSIX_FADV_DONTNEED:
4759 /* XXX: CAP_POSIX_FADVISE? */
4761 error = fget(td, fd, &cap_no_rights, &fp);
4764 AUDIT_ARG_FILE(td->td_proc, fp);
4765 if ((fp->f_ops->fo_flags & DFLAG_SEEKABLE) == 0) {
4769 if (fp->f_type != DTYPE_VNODE) {
4774 if (vp->v_type != VREG) {
4781 end = offset + len - 1;
4783 case POSIX_FADV_SEQUENTIAL:
4784 case POSIX_FADV_RANDOM:
4785 case POSIX_FADV_NOREUSE:
4787 * Try to merge any existing non-standard region with
4788 * this new region if possible, otherwise create a new
4789 * non-standard region for this request.
4791 mtx_pool_lock(mtxpool_sleep, fp);
4793 if (fa != NULL && fa->fa_advice == advice &&
4794 ((fa->fa_start <= end && fa->fa_end >= offset) ||
4795 (end != OFF_MAX && fa->fa_start == end + 1) ||
4796 (fa->fa_end != OFF_MAX && fa->fa_end + 1 == offset))) {
4797 if (offset < fa->fa_start)
4798 fa->fa_start = offset;
4799 if (end > fa->fa_end)
4802 new->fa_advice = advice;
4803 new->fa_start = offset;
4808 mtx_pool_unlock(mtxpool_sleep, fp);
4810 case POSIX_FADV_NORMAL:
4812 * If a the "normal" region overlaps with an existing
4813 * non-standard region, trim or remove the
4814 * non-standard region.
4816 mtx_pool_lock(mtxpool_sleep, fp);
4819 if (offset <= fa->fa_start && end >= fa->fa_end) {
4821 fp->f_advice = NULL;
4822 } else if (offset <= fa->fa_start &&
4823 end >= fa->fa_start)
4824 fa->fa_start = end + 1;
4825 else if (offset <= fa->fa_end && end >= fa->fa_end)
4826 fa->fa_end = offset - 1;
4827 else if (offset >= fa->fa_start && end <= fa->fa_end) {
4829 * If the "normal" region is a middle
4830 * portion of the existing
4831 * non-standard region, just remove
4832 * the whole thing rather than picking
4833 * one side or the other to
4837 fp->f_advice = NULL;
4840 mtx_pool_unlock(mtxpool_sleep, fp);
4842 case POSIX_FADV_WILLNEED:
4843 case POSIX_FADV_DONTNEED:
4844 error = VOP_ADVISE(vp, offset, end, advice);
4850 free(new, M_FADVISE);
4855 sys_posix_fadvise(struct thread *td, struct posix_fadvise_args *uap)
4859 error = kern_posix_fadvise(td, uap->fd, uap->offset, uap->len,
4861 return (kern_posix_error(td, error));
4865 kern_copy_file_range(struct thread *td, int infd, off_t *inoffp, int outfd,
4866 off_t *outoffp, size_t len, unsigned int flags)
4868 struct file *infp, *outfp;
4869 struct vnode *invp, *outvp;
4872 void *rl_rcookie, *rl_wcookie;
4873 off_t savinoff, savoutoff;
4875 infp = outfp = NULL;
4876 rl_rcookie = rl_wcookie = NULL;
4885 if (len > SSIZE_MAX)
4887 * Although the len argument is size_t, the return argument
4888 * is ssize_t (which is signed). Therefore a size that won't
4889 * fit in ssize_t can't be returned.
4893 /* Get the file structures for the file descriptors. */
4894 error = fget_read(td, infd, &cap_read_rights, &infp);
4897 if (infp->f_ops == &badfileops) {
4901 if (infp->f_vnode == NULL) {
4905 error = fget_write(td, outfd, &cap_write_rights, &outfp);
4908 if (outfp->f_ops == &badfileops) {
4912 if (outfp->f_vnode == NULL) {
4917 /* Set the offset pointers to the correct place. */
4919 inoffp = &infp->f_offset;
4920 if (outoffp == NULL)
4921 outoffp = &outfp->f_offset;
4923 savoutoff = *outoffp;
4925 invp = infp->f_vnode;
4926 outvp = outfp->f_vnode;
4927 /* Sanity check the f_flag bits. */
4928 if ((outfp->f_flag & (FWRITE | FAPPEND)) != FWRITE ||
4929 (infp->f_flag & FREAD) == 0) {
4934 /* If len == 0, just return 0. */
4939 * If infp and outfp refer to the same file, the byte ranges cannot
4942 if (invp == outvp && ((savinoff <= savoutoff && savinoff + len >
4943 savoutoff) || (savinoff > savoutoff && savoutoff + len >
4949 /* Range lock the byte ranges for both invp and outvp. */
4951 rl_wcookie = vn_rangelock_wlock(outvp, *outoffp, *outoffp +
4953 rl_rcookie = vn_rangelock_tryrlock(invp, *inoffp, *inoffp +
4955 if (rl_rcookie != NULL)
4957 vn_rangelock_unlock(outvp, rl_wcookie);
4958 rl_rcookie = vn_rangelock_rlock(invp, *inoffp, *inoffp + len);
4959 vn_rangelock_unlock(invp, rl_rcookie);
4963 error = vn_copy_file_range(invp, inoffp, outvp, outoffp, &retlen,
4964 flags, infp->f_cred, outfp->f_cred, td);
4966 if (rl_rcookie != NULL)
4967 vn_rangelock_unlock(invp, rl_rcookie);
4968 if (rl_wcookie != NULL)
4969 vn_rangelock_unlock(outvp, rl_wcookie);
4970 if (savinoff != -1 && (error == EINTR || error == ERESTART)) {
4972 *outoffp = savoutoff;
4978 td->td_retval[0] = retlen;
4983 sys_copy_file_range(struct thread *td, struct copy_file_range_args *uap)
4985 off_t inoff, outoff, *inoffp, *outoffp;
4988 inoffp = outoffp = NULL;
4989 if (uap->inoffp != NULL) {
4990 error = copyin(uap->inoffp, &inoff, sizeof(off_t));
4995 if (uap->outoffp != NULL) {
4996 error = copyin(uap->outoffp, &outoff, sizeof(off_t));
5001 error = kern_copy_file_range(td, uap->infd, inoffp, uap->outfd,
5002 outoffp, uap->len, uap->flags);
5003 if (error == 0 && uap->inoffp != NULL)
5004 error = copyout(inoffp, uap->inoffp, sizeof(off_t));
5005 if (error == 0 && uap->outoffp != NULL)
5006 error = copyout(outoffp, uap->outoffp, sizeof(off_t));