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 #include "opt_capsicum.h"
41 #include "opt_ktrace.h"
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #ifdef COMPAT_FREEBSD11
46 #include <sys/abi_compat.h>
50 #include <sys/capsicum.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)
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);
340 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 return (kern_freebsd11_getfsstat(td, uap->buf, uap->bufsize, uap->mode));
797 kern_freebsd11_getfsstat(struct thread *td, struct freebsd11_statfs * ubuf,
798 long bufsize, int mode)
800 struct freebsd11_statfs osb;
801 struct statfs *buf, *sp;
808 count = bufsize / sizeof(struct ostatfs);
809 size = count * sizeof(struct statfs);
810 error = kern_getfsstat(td, &buf, size, &count, UIO_SYSSPACE, mode);
812 td->td_retval[0] = count;
815 while (count > 0 && error == 0) {
816 freebsd11_cvtstatfs(sp, &osb);
817 error = copyout(&osb, ubuf, sizeof(osb));
828 * Implement fstatfs() for (NFS) file handles.
831 freebsd11_fhstatfs(struct thread *td, struct freebsd11_fhstatfs_args *uap)
833 struct freebsd11_statfs osb;
838 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
841 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
842 error = kern_fhstatfs(td, fh, sfp);
844 freebsd11_cvtstatfs(sfp, &osb);
845 error = copyout(&osb, uap->buf, sizeof(osb));
852 * Convert a new format statfs structure to an old format statfs structure.
855 freebsd11_cvtstatfs(struct statfs *nsp, struct freebsd11_statfs *osp)
858 bzero(osp, sizeof(*osp));
859 osp->f_version = FREEBSD11_STATFS_VERSION;
860 osp->f_type = nsp->f_type;
861 osp->f_flags = nsp->f_flags;
862 osp->f_bsize = nsp->f_bsize;
863 osp->f_iosize = nsp->f_iosize;
864 osp->f_blocks = nsp->f_blocks;
865 osp->f_bfree = nsp->f_bfree;
866 osp->f_bavail = nsp->f_bavail;
867 osp->f_files = nsp->f_files;
868 osp->f_ffree = nsp->f_ffree;
869 osp->f_syncwrites = nsp->f_syncwrites;
870 osp->f_asyncwrites = nsp->f_asyncwrites;
871 osp->f_syncreads = nsp->f_syncreads;
872 osp->f_asyncreads = nsp->f_asyncreads;
873 osp->f_namemax = nsp->f_namemax;
874 osp->f_owner = nsp->f_owner;
875 osp->f_fsid = nsp->f_fsid;
876 strlcpy(osp->f_fstypename, nsp->f_fstypename,
877 MIN(MFSNAMELEN, sizeof(osp->f_fstypename)));
878 strlcpy(osp->f_mntonname, nsp->f_mntonname,
879 MIN(MNAMELEN, sizeof(osp->f_mntonname)));
880 strlcpy(osp->f_mntfromname, nsp->f_mntfromname,
881 MIN(MNAMELEN, sizeof(osp->f_mntfromname)));
883 #endif /* COMPAT_FREEBSD11 */
886 * Change current working directory to a given file descriptor.
888 #ifndef _SYS_SYSPROTO_H_
894 sys_fchdir(struct thread *td, struct fchdir_args *uap)
896 struct vnode *vp, *tdp;
901 AUDIT_ARG_FD(uap->fd);
902 error = getvnode_path(td, uap->fd, &cap_fchdir_rights,
909 vn_lock(vp, LK_SHARED | LK_RETRY);
910 AUDIT_ARG_VNODE1(vp);
911 error = change_dir(vp, td);
912 while (!error && (mp = vp->v_mountedhere) != NULL) {
915 error = VFS_ROOT(mp, LK_SHARED, &tdp);
932 * Change current working directory (``.'').
934 #ifndef _SYS_SYSPROTO_H_
940 sys_chdir(struct thread *td, struct chdir_args *uap)
943 return (kern_chdir(td, uap->path, UIO_USERSPACE));
947 kern_chdir(struct thread *td, const char *path, enum uio_seg pathseg)
952 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
954 if ((error = namei(&nd)) != 0)
956 if ((error = change_dir(nd.ni_vp, td)) != 0) {
961 VOP_UNLOCK(nd.ni_vp);
963 pwd_chdir(td, nd.ni_vp);
967 static int unprivileged_chroot = 0;
968 SYSCTL_INT(_security_bsd, OID_AUTO, unprivileged_chroot, CTLFLAG_RW,
969 &unprivileged_chroot, 0,
970 "Unprivileged processes can use chroot(2)");
972 * Change notion of root (``/'') directory.
974 #ifndef _SYS_SYSPROTO_H_
980 sys_chroot(struct thread *td, struct chroot_args *uap)
986 error = priv_check(td, PRIV_VFS_CHROOT);
990 if (unprivileged_chroot == 0 ||
991 (p->p_flag2 & P2_NO_NEW_PRIVS) == 0) {
997 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
998 UIO_USERSPACE, uap->path);
1003 error = change_dir(nd.ni_vp, td);
1007 error = mac_vnode_check_chroot(td->td_ucred, nd.ni_vp);
1011 VOP_UNLOCK(nd.ni_vp);
1012 error = pwd_chroot(td, nd.ni_vp);
1021 * Common routine for chroot and chdir. Callers must provide a locked vnode
1025 change_dir(struct vnode *vp, struct thread *td)
1031 ASSERT_VOP_LOCKED(vp, "change_dir(): vp not locked");
1032 if (vp->v_type != VDIR)
1035 error = mac_vnode_check_chdir(td->td_ucred, vp);
1039 return (VOP_ACCESS(vp, VEXEC, td->td_ucred, td));
1042 static __inline void
1043 flags_to_rights(int flags, cap_rights_t *rightsp)
1045 if (flags & O_EXEC) {
1046 cap_rights_set_one(rightsp, CAP_FEXECVE);
1050 switch ((flags & O_ACCMODE)) {
1052 cap_rights_set_one(rightsp, CAP_READ);
1055 cap_rights_set_one(rightsp, CAP_READ);
1058 cap_rights_set_one(rightsp, CAP_WRITE);
1059 if (!(flags & (O_APPEND | O_TRUNC)))
1060 cap_rights_set_one(rightsp, CAP_SEEK);
1065 if (flags & O_CREAT)
1066 cap_rights_set_one(rightsp, CAP_CREATE);
1068 if (flags & O_TRUNC)
1069 cap_rights_set_one(rightsp, CAP_FTRUNCATE);
1071 if (flags & (O_SYNC | O_FSYNC))
1072 cap_rights_set_one(rightsp, CAP_FSYNC);
1074 if (flags & (O_EXLOCK | O_SHLOCK))
1075 cap_rights_set_one(rightsp, CAP_FLOCK);
1079 * Check permissions, allocate an open file structure, and call the device
1080 * open routine if any.
1082 #ifndef _SYS_SYSPROTO_H_
1090 sys_open(struct thread *td, struct open_args *uap)
1093 return (kern_openat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1094 uap->flags, uap->mode));
1097 #ifndef _SYS_SYSPROTO_H_
1098 struct openat_args {
1106 sys_openat(struct thread *td, struct openat_args *uap)
1109 AUDIT_ARG_FD(uap->fd);
1110 return (kern_openat(td, uap->fd, uap->path, UIO_USERSPACE, uap->flag,
1115 kern_openat(struct thread *td, int fd, const char *path, enum uio_seg pathseg,
1116 int flags, int mode)
1118 struct proc *p = td->td_proc;
1119 struct filedesc *fdp;
1120 struct pwddesc *pdp;
1123 struct nameidata nd;
1124 cap_rights_t rights;
1125 int cmode, error, indx;
1131 AUDIT_ARG_FFLAGS(flags);
1132 AUDIT_ARG_MODE(mode);
1133 cap_rights_init_one(&rights, CAP_LOOKUP);
1134 flags_to_rights(flags, &rights);
1137 * Only one of the O_EXEC, O_RDONLY, O_WRONLY and O_RDWR flags
1138 * may be specified. On the other hand, for O_PATH any mode
1139 * except O_EXEC is ignored.
1141 if ((flags & O_PATH) != 0) {
1142 flags &= ~(O_CREAT | O_ACCMODE);
1143 } else if ((flags & O_EXEC) != 0) {
1144 if (flags & O_ACCMODE)
1146 } else if ((flags & O_ACCMODE) == O_ACCMODE) {
1149 flags = FFLAGS(flags);
1153 * Allocate a file structure. The descriptor to reference it
1154 * is allocated and used by finstall_refed() below.
1156 error = falloc_noinstall(td, &fp);
1159 /* Set the flags early so the finit in devfs can pick them up. */
1160 fp->f_flag = flags & FMASK;
1161 cmode = ((mode & ~pdp->pd_cmask) & ALLPERMS) & ~S_ISTXT;
1162 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | AUDITVNODE1 | WANTIOCTLCAPS,
1163 pathseg, path, fd, &rights);
1164 td->td_dupfd = -1; /* XXX check for fdopen */
1165 error = vn_open_cred(&nd, &flags, cmode, VN_OPEN_WANTIOCTLCAPS,
1169 * If the vn_open replaced the method vector, something
1170 * wonderous happened deep below and we just pass it up
1171 * pretending we know what we do.
1173 if (error == ENXIO && fp->f_ops != &badfileops) {
1174 MPASS((flags & O_PATH) == 0);
1179 * Handle special fdopen() case. bleh.
1181 * Don't do this for relative (capability) lookups; we don't
1182 * understand exactly what would happen, and we don't think
1183 * that it ever should.
1185 if ((nd.ni_resflags & NIRES_STRICTREL) == 0 &&
1186 (error == ENODEV || error == ENXIO) &&
1187 td->td_dupfd >= 0) {
1188 error = dupfdopen(td, fdp, td->td_dupfd, flags, error,
1201 * Store the vnode, for any f_type. Typically, the vnode use
1202 * count is decremented by direct call to vn_closefile() for
1203 * files that switched type in the cdevsw fdopen() method.
1208 * If the file wasn't claimed by devfs bind it to the normal
1209 * vnode operations here.
1211 if (fp->f_ops == &badfileops) {
1212 KASSERT(vp->v_type != VFIFO || (flags & O_PATH) != 0,
1213 ("Unexpected fifo fp %p vp %p", fp, vp));
1214 if ((flags & O_PATH) != 0) {
1215 finit(fp, (flags & FMASK) | (fp->f_flag & FKQALLOWED),
1216 DTYPE_VNODE, NULL, &path_fileops);
1218 finit_vnode(fp, flags, NULL, &vnops);
1223 if (flags & O_TRUNC) {
1224 error = fo_truncate(fp, 0, td->td_ucred, td);
1230 * If we haven't already installed the FD (for dupfdopen), do so now.
1233 struct filecaps *fcaps;
1236 if ((nd.ni_resflags & NIRES_STRICTREL) != 0)
1237 fcaps = &nd.ni_filecaps;
1241 error = finstall_refed(td, fp, &indx, flags, fcaps);
1242 /* On success finstall_refed() consumes fcaps. */
1247 NDFREE_IOCTLCAPS(&nd);
1248 falloc_abort(td, fp);
1251 td->td_retval[0] = indx;
1254 KASSERT(indx == -1, ("indx=%d, should be -1", indx));
1255 NDFREE_IOCTLCAPS(&nd);
1256 falloc_abort(td, fp);
1264 #ifndef _SYS_SYSPROTO_H_
1265 struct ocreat_args {
1271 ocreat(struct thread *td, struct ocreat_args *uap)
1274 return (kern_openat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1275 O_WRONLY | O_CREAT | O_TRUNC, uap->mode));
1277 #endif /* COMPAT_43 */
1280 * Create a special file.
1282 #ifndef _SYS_SYSPROTO_H_
1283 struct mknodat_args {
1291 sys_mknodat(struct thread *td, struct mknodat_args *uap)
1294 return (kern_mknodat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode,
1298 #if defined(COMPAT_FREEBSD11)
1300 freebsd11_mknod(struct thread *td,
1301 struct freebsd11_mknod_args *uap)
1304 return (kern_mknodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1305 uap->mode, uap->dev));
1309 freebsd11_mknodat(struct thread *td,
1310 struct freebsd11_mknodat_args *uap)
1313 return (kern_mknodat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode,
1316 #endif /* COMPAT_FREEBSD11 */
1319 kern_mknodat(struct thread *td, int fd, const char *path, enum uio_seg pathseg,
1320 int mode, dev_t dev)
1325 struct nameidata nd;
1326 int error, whiteout = 0;
1328 AUDIT_ARG_MODE(mode);
1330 switch (mode & S_IFMT) {
1333 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
1334 if (error == 0 && dev == VNOVAL)
1338 error = priv_check(td, PRIV_VFS_MKNOD_WHT);
1342 return (kern_mkfifoat(td, fd, path, pathseg, mode));
1353 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | AUDITVNODE1 | NOCACHE,
1354 pathseg, path, fd, &cap_mknodat_rights);
1355 if ((error = namei(&nd)) != 0)
1360 if (vp == nd.ni_dvp)
1368 vattr.va_mode = (mode & ALLPERMS) &
1369 ~td->td_proc->p_pd->pd_cmask;
1370 vattr.va_rdev = dev;
1373 switch (mode & S_IFMT) {
1375 vattr.va_type = VCHR;
1378 vattr.va_type = VBLK;
1384 panic("kern_mknod: invalid mode");
1387 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1390 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | V_PCATCH)) != 0)
1395 if (error == 0 && !whiteout)
1396 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp,
1397 &nd.ni_cnd, &vattr);
1401 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, CREATE);
1403 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp,
1404 &nd.ni_cnd, &vattr);
1407 VOP_VPUT_PAIR(nd.ni_dvp, error == 0 && !whiteout ? &nd.ni_vp : NULL,
1409 vn_finished_write(mp);
1411 if (error == ERELOOKUP)
1417 * Create a named pipe.
1419 #ifndef _SYS_SYSPROTO_H_
1420 struct mkfifo_args {
1426 sys_mkfifo(struct thread *td, struct mkfifo_args *uap)
1429 return (kern_mkfifoat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1433 #ifndef _SYS_SYSPROTO_H_
1434 struct mkfifoat_args {
1441 sys_mkfifoat(struct thread *td, struct mkfifoat_args *uap)
1444 return (kern_mkfifoat(td, uap->fd, uap->path, UIO_USERSPACE,
1449 kern_mkfifoat(struct thread *td, int fd, const char *path,
1450 enum uio_seg pathseg, int mode)
1454 struct nameidata nd;
1457 AUDIT_ARG_MODE(mode);
1461 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | AUDITVNODE1 | NOCACHE,
1462 pathseg, path, fd, &cap_mkfifoat_rights);
1463 if ((error = namei(&nd)) != 0)
1465 if (nd.ni_vp != NULL) {
1467 if (nd.ni_vp == nd.ni_dvp)
1474 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1477 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | V_PCATCH)) != 0)
1482 vattr.va_type = VFIFO;
1483 vattr.va_mode = (mode & ALLPERMS) & ~td->td_proc->p_pd->pd_cmask;
1485 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1490 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
1494 VOP_VPUT_PAIR(nd.ni_dvp, error == 0 ? &nd.ni_vp : NULL, true);
1495 vn_finished_write(mp);
1497 if (error == ERELOOKUP)
1503 * Make a hard file link.
1505 #ifndef _SYS_SYSPROTO_H_
1512 sys_link(struct thread *td, struct link_args *uap)
1515 return (kern_linkat(td, AT_FDCWD, AT_FDCWD, uap->path, uap->link,
1516 UIO_USERSPACE, AT_SYMLINK_FOLLOW));
1519 #ifndef _SYS_SYSPROTO_H_
1520 struct linkat_args {
1529 sys_linkat(struct thread *td, struct linkat_args *uap)
1532 return (kern_linkat(td, uap->fd1, uap->fd2, uap->path1, uap->path2,
1533 UIO_USERSPACE, uap->flag));
1536 int hardlink_check_uid = 0;
1537 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
1538 &hardlink_check_uid, 0,
1539 "Unprivileged processes cannot create hard links to files owned by other "
1541 static int hardlink_check_gid = 0;
1542 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
1543 &hardlink_check_gid, 0,
1544 "Unprivileged processes cannot create hard links to files owned by other "
1548 can_hardlink(struct vnode *vp, struct ucred *cred)
1553 if (!hardlink_check_uid && !hardlink_check_gid)
1556 error = VOP_GETATTR(vp, &va, cred);
1560 if (hardlink_check_uid && cred->cr_uid != va.va_uid) {
1561 error = priv_check_cred(cred, PRIV_VFS_LINK);
1566 if (hardlink_check_gid && !groupmember(va.va_gid, cred)) {
1567 error = priv_check_cred(cred, PRIV_VFS_LINK);
1576 kern_linkat(struct thread *td, int fd1, int fd2, const char *path1,
1577 const char *path2, enum uio_seg segflag, int flag)
1579 struct nameidata nd;
1582 if ((flag & ~(AT_SYMLINK_FOLLOW | AT_RESOLVE_BENEATH |
1583 AT_EMPTY_PATH)) != 0)
1589 NDINIT_ATRIGHTS(&nd, LOOKUP, AUDITVNODE1 | at2cnpflags(flag,
1590 AT_SYMLINK_FOLLOW | AT_RESOLVE_BENEATH | AT_EMPTY_PATH),
1591 segflag, path1, fd1, &cap_linkat_source_rights);
1592 if ((error = namei(&nd)) != 0)
1595 if ((nd.ni_resflags & NIRES_EMPTYPATH) != 0) {
1596 error = priv_check(td, PRIV_VFS_FHOPEN);
1602 error = kern_linkat_vp(td, nd.ni_vp, fd2, path2, segflag);
1603 } while (error == EAGAIN || error == ERELOOKUP);
1608 kern_linkat_vp(struct thread *td, struct vnode *vp, int fd, const char *path,
1609 enum uio_seg segflag)
1611 struct nameidata nd;
1615 if (vp->v_type == VDIR) {
1617 return (EPERM); /* POSIX */
1619 NDINIT_ATRIGHTS(&nd, CREATE,
1620 LOCKPARENT | AUDITVNODE2 | NOCACHE, segflag, path, fd,
1621 &cap_linkat_target_rights);
1622 if ((error = namei(&nd)) == 0) {
1623 if (nd.ni_vp != NULL) {
1625 if (nd.ni_dvp == nd.ni_vp)
1632 } else if (nd.ni_dvp->v_mount != vp->v_mount) {
1634 * Cross-device link. No need to recheck
1635 * vp->v_type, since it cannot change, except
1642 } else if (vn_lock(vp, LK_EXCLUSIVE) == 0) {
1643 error = can_hardlink(vp, td->td_ucred);
1646 error = mac_vnode_check_link(td->td_ucred,
1647 nd.ni_dvp, vp, &nd.ni_cnd);
1655 error = vn_start_write(vp, &mp, V_NOWAIT);
1660 error = vn_start_write(NULL, &mp,
1661 V_XSLEEP | V_PCATCH);
1666 error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
1667 VOP_VPUT_PAIR(nd.ni_dvp, &vp, true);
1668 vn_finished_write(mp);
1684 * Make a symbolic link.
1686 #ifndef _SYS_SYSPROTO_H_
1687 struct symlink_args {
1693 sys_symlink(struct thread *td, struct symlink_args *uap)
1696 return (kern_symlinkat(td, uap->path, AT_FDCWD, uap->link,
1700 #ifndef _SYS_SYSPROTO_H_
1701 struct symlinkat_args {
1708 sys_symlinkat(struct thread *td, struct symlinkat_args *uap)
1711 return (kern_symlinkat(td, uap->path1, uap->fd, uap->path2,
1716 kern_symlinkat(struct thread *td, const char *path1, int fd, const char *path2,
1717 enum uio_seg segflg)
1721 const char *syspath;
1723 struct nameidata nd;
1726 if (segflg == UIO_SYSSPACE) {
1729 tmppath = uma_zalloc(namei_zone, M_WAITOK);
1730 if ((error = copyinstr(path1, tmppath, MAXPATHLEN, NULL)) != 0)
1734 AUDIT_ARG_TEXT(syspath);
1738 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | AUDITVNODE1 | NOCACHE, segflg,
1739 path2, fd, &cap_symlinkat_rights);
1740 if ((error = namei(&nd)) != 0)
1744 if (nd.ni_vp == nd.ni_dvp)
1753 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1756 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | V_PCATCH)) != 0)
1761 vattr.va_mode = ACCESSPERMS &~ td->td_proc->p_pd->pd_cmask;
1763 vattr.va_type = VLNK;
1764 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1769 error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr, syspath);
1773 VOP_VPUT_PAIR(nd.ni_dvp, error == 0 ? &nd.ni_vp : NULL, true);
1774 vn_finished_write(mp);
1776 if (error == ERELOOKUP)
1779 if (segflg != UIO_SYSSPACE)
1780 uma_zfree(namei_zone, tmppath);
1785 * Delete a whiteout from the filesystem.
1787 #ifndef _SYS_SYSPROTO_H_
1788 struct undelete_args {
1793 sys_undelete(struct thread *td, struct undelete_args *uap)
1796 struct nameidata nd;
1802 NDINIT(&nd, DELETE, LOCKPARENT | DOWHITEOUT | AUDITVNODE1,
1803 UIO_USERSPACE, uap->path);
1808 if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & ISWHITEOUT)) {
1810 if (nd.ni_vp == nd.ni_dvp)
1818 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1821 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | V_PCATCH)) != 0)
1825 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, DELETE);
1828 vn_finished_write(mp);
1829 if (error == ERELOOKUP)
1835 * Delete a name from the filesystem.
1837 #ifndef _SYS_SYSPROTO_H_
1838 struct unlink_args {
1843 sys_unlink(struct thread *td, struct unlink_args *uap)
1846 return (kern_funlinkat(td, AT_FDCWD, uap->path, FD_NONE, UIO_USERSPACE,
1851 kern_funlinkat_ex(struct thread *td, int dfd, const char *path, int fd,
1852 int flag, enum uio_seg pathseg, ino_t oldinum)
1855 if ((flag & ~(AT_REMOVEDIR | AT_RESOLVE_BENEATH)) != 0)
1858 if ((flag & AT_REMOVEDIR) != 0)
1859 return (kern_frmdirat(td, dfd, path, fd, UIO_USERSPACE, 0));
1861 return (kern_funlinkat(td, dfd, path, fd, UIO_USERSPACE, 0, 0));
1864 #ifndef _SYS_SYSPROTO_H_
1865 struct unlinkat_args {
1872 sys_unlinkat(struct thread *td, struct unlinkat_args *uap)
1875 return (kern_funlinkat_ex(td, uap->fd, uap->path, FD_NONE, uap->flag,
1879 #ifndef _SYS_SYSPROTO_H_
1880 struct funlinkat_args {
1888 sys_funlinkat(struct thread *td, struct funlinkat_args *uap)
1891 return (kern_funlinkat_ex(td, uap->dfd, uap->path, uap->fd, uap->flag,
1896 kern_funlinkat(struct thread *td, int dfd, const char *path, int fd,
1897 enum uio_seg pathseg, int flag, ino_t oldinum)
1902 struct nameidata nd;
1907 if (fd != FD_NONE) {
1908 error = getvnode_path(td, fd, &cap_no_rights, &fp);
1916 NDINIT_ATRIGHTS(&nd, DELETE, LOCKPARENT | LOCKLEAF | AUDITVNODE1 |
1917 at2cnpflags(flag, AT_RESOLVE_BENEATH),
1918 pathseg, path, dfd, &cap_unlinkat_rights);
1919 if ((error = namei(&nd)) != 0) {
1920 if (error == EINVAL)
1925 if (vp->v_type == VDIR && oldinum == 0) {
1926 error = EPERM; /* POSIX */
1927 } else if (oldinum != 0 &&
1928 ((error = VOP_STAT(vp, &sb, td->td_ucred, NOCRED)) == 0) &&
1929 sb.st_ino != oldinum) {
1930 error = EIDRM; /* Identifier removed */
1931 } else if (fp != NULL && fp->f_vnode != vp) {
1932 if (VN_IS_DOOMED(fp->f_vnode))
1938 * The root of a mounted filesystem cannot be deleted.
1940 * XXX: can this only be a VDIR case?
1942 if (vp->v_vflag & VV_ROOT)
1946 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1949 if (vp == nd.ni_dvp)
1953 if ((error = vn_start_write(NULL, &mp,
1954 V_XSLEEP | V_PCATCH)) != 0) {
1960 error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp,
1965 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_UNLINK);
1966 error = VOP_REMOVE(nd.ni_dvp, vp, &nd.ni_cnd);
1970 vn_finished_write(mp);
1974 if (vp == nd.ni_dvp)
1978 if (error == ERELOOKUP)
1987 * Reposition read/write file offset.
1989 #ifndef _SYS_SYSPROTO_H_
1998 sys_lseek(struct thread *td, struct lseek_args *uap)
2001 return (kern_lseek(td, uap->fd, uap->offset, uap->whence));
2005 kern_lseek(struct thread *td, int fd, off_t offset, int whence)
2011 error = fget(td, fd, &cap_seek_rights, &fp);
2014 error = (fp->f_ops->fo_flags & DFLAG_SEEKABLE) != 0 ?
2015 fo_seek(fp, offset, whence, td) : ESPIPE;
2020 #if defined(COMPAT_43)
2022 * Reposition read/write file offset.
2024 #ifndef _SYS_SYSPROTO_H_
2025 struct olseek_args {
2032 olseek(struct thread *td, struct olseek_args *uap)
2035 return (kern_lseek(td, uap->fd, uap->offset, uap->whence));
2037 #endif /* COMPAT_43 */
2039 #if defined(COMPAT_FREEBSD6)
2040 /* Version with the 'pad' argument */
2042 freebsd6_lseek(struct thread *td, struct freebsd6_lseek_args *uap)
2045 return (kern_lseek(td, uap->fd, uap->offset, uap->whence));
2050 * Check access permissions using passed credentials.
2053 vn_access(struct vnode *vp, int user_flags, struct ucred *cred,
2059 /* Flags == 0 means only check for existence. */
2060 if (user_flags == 0)
2064 if (user_flags & R_OK)
2066 if (user_flags & W_OK)
2068 if (user_flags & X_OK)
2071 error = mac_vnode_check_access(cred, vp, accmode);
2075 if ((accmode & VWRITE) == 0 || (error = vn_writechk(vp)) == 0)
2076 error = VOP_ACCESS(vp, accmode, cred, td);
2081 * Check access permissions using "real" credentials.
2083 #ifndef _SYS_SYSPROTO_H_
2084 struct access_args {
2090 sys_access(struct thread *td, struct access_args *uap)
2093 return (kern_accessat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2097 #ifndef _SYS_SYSPROTO_H_
2098 struct faccessat_args {
2106 sys_faccessat(struct thread *td, struct faccessat_args *uap)
2109 return (kern_accessat(td, uap->fd, uap->path, UIO_USERSPACE, uap->flag,
2114 kern_accessat(struct thread *td, int fd, const char *path,
2115 enum uio_seg pathseg, int flag, int amode)
2117 struct ucred *cred, *usecred;
2119 struct nameidata nd;
2122 if ((flag & ~(AT_EACCESS | AT_RESOLVE_BENEATH | AT_EMPTY_PATH)) != 0)
2124 if (amode != F_OK && (amode & ~(R_OK | W_OK | X_OK)) != 0)
2128 * Create and modify a temporary credential instead of one that
2129 * is potentially shared (if we need one).
2131 cred = td->td_ucred;
2132 if ((flag & AT_EACCESS) == 0 &&
2133 ((cred->cr_uid != cred->cr_ruid ||
2134 cred->cr_rgid != cred->cr_groups[0]))) {
2135 usecred = crdup(cred);
2136 usecred->cr_uid = cred->cr_ruid;
2137 usecred->cr_groups[0] = cred->cr_rgid;
2138 td->td_ucred = usecred;
2141 AUDIT_ARG_VALUE(amode);
2142 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF |
2143 AUDITVNODE1 | at2cnpflags(flag, AT_RESOLVE_BENEATH |
2144 AT_EMPTY_PATH), pathseg, path, fd, &cap_fstat_rights);
2145 if ((error = namei(&nd)) != 0)
2149 error = vn_access(vp, amode, usecred, td);
2153 if (usecred != cred) {
2154 td->td_ucred = cred;
2161 * Check access permissions using "effective" credentials.
2163 #ifndef _SYS_SYSPROTO_H_
2164 struct eaccess_args {
2170 sys_eaccess(struct thread *td, struct eaccess_args *uap)
2173 return (kern_accessat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2174 AT_EACCESS, uap->amode));
2177 #if defined(COMPAT_43)
2179 * Get file status; this version follows links.
2181 #ifndef _SYS_SYSPROTO_H_
2188 ostat(struct thread *td, struct ostat_args *uap)
2194 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE, &sb);
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);
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, &sb);
2339 error = freebsd11_cvtstat(&sb, &osb);
2341 error = copyout(&osb, uap->ub, sizeof(osb));
2346 freebsd11_lstat(struct thread *td, struct freebsd11_lstat_args* uap)
2349 struct freebsd11_stat osb;
2352 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2353 UIO_USERSPACE, &sb);
2356 error = freebsd11_cvtstat(&sb, &osb);
2358 error = copyout(&osb, uap->ub, sizeof(osb));
2363 freebsd11_fhstat(struct thread *td, struct freebsd11_fhstat_args* uap)
2367 struct freebsd11_stat osb;
2370 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
2373 error = kern_fhstat(td, fh, &sb);
2376 error = freebsd11_cvtstat(&sb, &osb);
2378 error = copyout(&osb, uap->sb, sizeof(osb));
2383 freebsd11_fstatat(struct thread *td, struct freebsd11_fstatat_args* uap)
2386 struct freebsd11_stat osb;
2389 error = kern_statat(td, uap->flag, uap->fd, uap->path,
2390 UIO_USERSPACE, &sb);
2393 error = freebsd11_cvtstat(&sb, &osb);
2395 error = copyout(&osb, uap->buf, sizeof(osb));
2398 #endif /* COMPAT_FREEBSD11 */
2403 #ifndef _SYS_SYSPROTO_H_
2404 struct fstatat_args {
2412 sys_fstatat(struct thread *td, struct fstatat_args *uap)
2417 error = kern_statat(td, uap->flag, uap->fd, uap->path,
2418 UIO_USERSPACE, &sb);
2420 error = copyout(&sb, uap->buf, sizeof (sb));
2425 kern_statat(struct thread *td, int flag, int fd, const char *path,
2426 enum uio_seg pathseg, struct stat *sbp)
2428 struct nameidata nd;
2431 if ((flag & ~(AT_SYMLINK_NOFOLLOW | AT_RESOLVE_BENEATH |
2432 AT_EMPTY_PATH)) != 0)
2435 NDINIT_ATRIGHTS(&nd, LOOKUP, at2cnpflags(flag, AT_RESOLVE_BENEATH |
2436 AT_SYMLINK_NOFOLLOW | AT_EMPTY_PATH) | LOCKSHARED | LOCKLEAF |
2437 AUDITVNODE1, pathseg, path, fd, &cap_fstat_rights);
2439 if ((error = namei(&nd)) != 0) {
2440 if (error == ENOTDIR &&
2441 (nd.ni_resflags & NIRES_EMPTYPATH) != 0)
2442 error = kern_fstat(td, fd, sbp);
2445 error = VOP_STAT(nd.ni_vp, sbp, td->td_ucred, NOCRED);
2448 #ifdef __STAT_TIME_T_EXT
2449 sbp->st_atim_ext = 0;
2450 sbp->st_mtim_ext = 0;
2451 sbp->st_ctim_ext = 0;
2452 sbp->st_btim_ext = 0;
2455 if (KTRPOINT(td, KTR_STRUCT))
2456 ktrstat_error(sbp, error);
2461 #if defined(COMPAT_FREEBSD11)
2463 * Implementation of the NetBSD [l]stat() functions.
2466 freebsd11_cvtnstat(struct stat *sb, struct nstat *nsb)
2468 struct freebsd11_stat sb11;
2471 error = freebsd11_cvtstat(sb, &sb11);
2475 bzero(nsb, sizeof(*nsb));
2476 CP(sb11, *nsb, st_dev);
2477 CP(sb11, *nsb, st_ino);
2478 CP(sb11, *nsb, st_mode);
2479 CP(sb11, *nsb, st_nlink);
2480 CP(sb11, *nsb, st_uid);
2481 CP(sb11, *nsb, st_gid);
2482 CP(sb11, *nsb, st_rdev);
2483 CP(sb11, *nsb, st_atim);
2484 CP(sb11, *nsb, st_mtim);
2485 CP(sb11, *nsb, st_ctim);
2486 CP(sb11, *nsb, st_size);
2487 CP(sb11, *nsb, st_blocks);
2488 CP(sb11, *nsb, st_blksize);
2489 CP(sb11, *nsb, st_flags);
2490 CP(sb11, *nsb, st_gen);
2491 CP(sb11, *nsb, st_birthtim);
2495 #ifndef _SYS_SYSPROTO_H_
2496 struct freebsd11_nstat_args {
2502 freebsd11_nstat(struct thread *td, struct freebsd11_nstat_args *uap)
2508 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE, &sb);
2511 error = freebsd11_cvtnstat(&sb, &nsb);
2513 error = copyout(&nsb, uap->ub, sizeof (nsb));
2518 * NetBSD lstat. Get file status; this version does not follow links.
2520 #ifndef _SYS_SYSPROTO_H_
2521 struct freebsd11_nlstat_args {
2527 freebsd11_nlstat(struct thread *td, struct freebsd11_nlstat_args *uap)
2533 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2534 UIO_USERSPACE, &sb);
2537 error = freebsd11_cvtnstat(&sb, &nsb);
2539 error = copyout(&nsb, uap->ub, sizeof (nsb));
2542 #endif /* COMPAT_FREEBSD11 */
2545 * Get configurable pathname variables.
2547 #ifndef _SYS_SYSPROTO_H_
2548 struct pathconf_args {
2554 sys_pathconf(struct thread *td, struct pathconf_args *uap)
2559 error = kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name, FOLLOW,
2562 td->td_retval[0] = value;
2566 #ifndef _SYS_SYSPROTO_H_
2567 struct lpathconf_args {
2573 sys_lpathconf(struct thread *td, struct lpathconf_args *uap)
2578 error = kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name,
2581 td->td_retval[0] = value;
2586 kern_pathconf(struct thread *td, const char *path, enum uio_seg pathseg,
2587 int name, u_long flags, long *valuep)
2589 struct nameidata nd;
2592 NDINIT(&nd, LOOKUP, LOCKSHARED | LOCKLEAF | AUDITVNODE1 | flags,
2594 if ((error = namei(&nd)) != 0)
2598 error = VOP_PATHCONF(nd.ni_vp, name, valuep);
2604 * Return target name of a symbolic link.
2606 #ifndef _SYS_SYSPROTO_H_
2607 struct readlink_args {
2614 sys_readlink(struct thread *td, struct readlink_args *uap)
2617 return (kern_readlinkat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2618 uap->buf, UIO_USERSPACE, uap->count));
2620 #ifndef _SYS_SYSPROTO_H_
2621 struct readlinkat_args {
2629 sys_readlinkat(struct thread *td, struct readlinkat_args *uap)
2632 return (kern_readlinkat(td, uap->fd, uap->path, UIO_USERSPACE,
2633 uap->buf, UIO_USERSPACE, uap->bufsize));
2637 kern_readlinkat(struct thread *td, int fd, const char *path,
2638 enum uio_seg pathseg, char *buf, enum uio_seg bufseg, size_t count)
2641 struct nameidata nd;
2644 if (count > IOSIZE_MAX)
2647 NDINIT_AT(&nd, LOOKUP, NOFOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1 |
2648 EMPTYPATH, pathseg, path, fd);
2650 if ((error = namei(&nd)) != 0)
2655 error = kern_readlink_vp(vp, buf, bufseg, count, td);
2662 * Helper function to readlink from a vnode
2665 kern_readlink_vp(struct vnode *vp, char *buf, enum uio_seg bufseg, size_t count,
2672 ASSERT_VOP_LOCKED(vp, "kern_readlink_vp(): vp not locked");
2674 error = mac_vnode_check_readlink(td->td_ucred, vp);
2678 if (vp->v_type != VLNK && (vp->v_vflag & VV_READLINK) == 0)
2681 aiov.iov_base = buf;
2682 aiov.iov_len = count;
2683 auio.uio_iov = &aiov;
2684 auio.uio_iovcnt = 1;
2685 auio.uio_offset = 0;
2686 auio.uio_rw = UIO_READ;
2687 auio.uio_segflg = bufseg;
2689 auio.uio_resid = count;
2690 error = VOP_READLINK(vp, &auio, td->td_ucred);
2691 td->td_retval[0] = count - auio.uio_resid;
2696 * Common implementation code for chflags() and fchflags().
2699 setfflags(struct thread *td, struct vnode *vp, u_long flags)
2705 /* We can't support the value matching VNOVAL. */
2706 if (flags == VNOVAL)
2707 return (EOPNOTSUPP);
2710 * Prevent non-root users from setting flags on devices. When
2711 * a device is reused, users can retain ownership of the device
2712 * if they are allowed to set flags and programs assume that
2713 * chown can't fail when done as root.
2715 if (vp->v_type == VCHR || vp->v_type == VBLK) {
2716 error = priv_check(td, PRIV_VFS_CHFLAGS_DEV);
2721 if ((error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH)) != 0)
2724 vattr.va_flags = flags;
2725 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2727 error = mac_vnode_check_setflags(td->td_ucred, vp, vattr.va_flags);
2730 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
2732 vn_finished_write(mp);
2737 * Change flags of a file given a path name.
2739 #ifndef _SYS_SYSPROTO_H_
2740 struct chflags_args {
2746 sys_chflags(struct thread *td, struct chflags_args *uap)
2749 return (kern_chflagsat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2753 #ifndef _SYS_SYSPROTO_H_
2754 struct chflagsat_args {
2762 sys_chflagsat(struct thread *td, struct chflagsat_args *uap)
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 if ((atflag & ~(AT_SYMLINK_NOFOLLOW | AT_RESOLVE_BENEATH |
2794 AT_EMPTY_PATH)) != 0)
2797 AUDIT_ARG_FFLAGS(flags);
2798 NDINIT_ATRIGHTS(&nd, LOOKUP, at2cnpflags(atflag, AT_SYMLINK_NOFOLLOW |
2799 AT_RESOLVE_BENEATH | AT_EMPTY_PATH) | AUDITVNODE1, pathseg, path,
2800 fd, &cap_fchflags_rights);
2801 if ((error = namei(&nd)) != 0)
2804 error = setfflags(td, nd.ni_vp, flags);
2810 * Change flags of a file given a file descriptor.
2812 #ifndef _SYS_SYSPROTO_H_
2813 struct fchflags_args {
2819 sys_fchflags(struct thread *td, struct fchflags_args *uap)
2824 AUDIT_ARG_FD(uap->fd);
2825 AUDIT_ARG_FFLAGS(uap->flags);
2826 error = getvnode(td, uap->fd, &cap_fchflags_rights,
2831 if (AUDITING_TD(td)) {
2832 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
2833 AUDIT_ARG_VNODE1(fp->f_vnode);
2834 VOP_UNLOCK(fp->f_vnode);
2837 error = setfflags(td, fp->f_vnode, uap->flags);
2843 * Common implementation code for chmod(), lchmod() and fchmod().
2846 setfmode(struct thread *td, struct ucred *cred, struct vnode *vp, int mode)
2852 if ((error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH)) != 0)
2854 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2856 vattr.va_mode = mode & ALLPERMS;
2858 error = mac_vnode_check_setmode(cred, vp, vattr.va_mode);
2861 error = VOP_SETATTR(vp, &vattr, cred);
2863 vn_finished_write(mp);
2868 * Change mode of a file given path name.
2870 #ifndef _SYS_SYSPROTO_H_
2877 sys_chmod(struct thread *td, struct chmod_args *uap)
2880 return (kern_fchmodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2884 #ifndef _SYS_SYSPROTO_H_
2885 struct fchmodat_args {
2893 sys_fchmodat(struct thread *td, struct fchmodat_args *uap)
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 if ((flag & ~(AT_SYMLINK_NOFOLLOW | AT_RESOLVE_BENEATH |
2925 AT_EMPTY_PATH)) != 0)
2928 AUDIT_ARG_MODE(mode);
2929 NDINIT_ATRIGHTS(&nd, LOOKUP, at2cnpflags(flag, AT_SYMLINK_NOFOLLOW |
2930 AT_RESOLVE_BENEATH | AT_EMPTY_PATH) | AUDITVNODE1, pathseg, path,
2931 fd, &cap_fchmod_rights);
2932 if ((error = namei(&nd)) != 0)
2935 error = setfmode(td, td->td_ucred, nd.ni_vp, mode);
2941 * Change mode of a file given a file descriptor.
2943 #ifndef _SYS_SYSPROTO_H_
2944 struct fchmod_args {
2950 sys_fchmod(struct thread *td, struct fchmod_args *uap)
2955 AUDIT_ARG_FD(uap->fd);
2956 AUDIT_ARG_MODE(uap->mode);
2958 error = fget(td, uap->fd, &cap_fchmod_rights, &fp);
2961 error = fo_chmod(fp, uap->mode, td->td_ucred, td);
2967 * Common implementation for chown(), lchown(), and fchown()
2970 setfown(struct thread *td, struct ucred *cred, struct vnode *vp, uid_t uid,
2977 if ((error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH)) != 0)
2979 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2984 error = mac_vnode_check_setowner(cred, vp, vattr.va_uid,
2988 error = VOP_SETATTR(vp, &vattr, cred);
2990 vn_finished_write(mp);
2995 * Set ownership given a path name.
2997 #ifndef _SYS_SYSPROTO_H_
3005 sys_chown(struct thread *td, struct chown_args *uap)
3008 return (kern_fchownat(td, AT_FDCWD, uap->path, UIO_USERSPACE, uap->uid,
3012 #ifndef _SYS_SYSPROTO_H_
3013 struct fchownat_args {
3022 sys_fchownat(struct thread *td, struct fchownat_args *uap)
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 if ((flag & ~(AT_SYMLINK_NOFOLLOW | AT_RESOLVE_BENEATH |
3037 AT_EMPTY_PATH)) != 0)
3040 AUDIT_ARG_OWNER(uid, gid);
3041 NDINIT_ATRIGHTS(&nd, LOOKUP, at2cnpflags(flag, AT_SYMLINK_NOFOLLOW |
3042 AT_RESOLVE_BENEATH | AT_EMPTY_PATH) | AUDITVNODE1, pathseg, path,
3043 fd, &cap_fchown_rights);
3045 if ((error = namei(&nd)) != 0)
3048 error = setfown(td, td->td_ucred, nd.ni_vp, uid, gid);
3054 * Set ownership given a path name, do not cross symlinks.
3056 #ifndef _SYS_SYSPROTO_H_
3057 struct lchown_args {
3064 sys_lchown(struct thread *td, struct lchown_args *uap)
3067 return (kern_fchownat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
3068 uap->uid, uap->gid, AT_SYMLINK_NOFOLLOW));
3072 * Set ownership given a file descriptor.
3074 #ifndef _SYS_SYSPROTO_H_
3075 struct fchown_args {
3082 sys_fchown(struct thread *td, struct fchown_args *uap)
3087 AUDIT_ARG_FD(uap->fd);
3088 AUDIT_ARG_OWNER(uap->uid, uap->gid);
3089 error = fget(td, uap->fd, &cap_fchown_rights, &fp);
3092 error = fo_chown(fp, uap->uid, uap->gid, td->td_ucred, td);
3098 * Common implementation code for utimes(), lutimes(), and futimes().
3101 getutimes(const struct timeval *usrtvp, enum uio_seg tvpseg,
3102 struct timespec *tsp)
3104 struct timeval tv[2];
3105 const struct timeval *tvp;
3108 if (usrtvp == NULL) {
3109 vfs_timestamp(&tsp[0]);
3112 if (tvpseg == UIO_SYSSPACE) {
3115 if ((error = copyin(usrtvp, tv, sizeof(tv))) != 0)
3120 if (tvp[0].tv_usec < 0 || tvp[0].tv_usec >= 1000000 ||
3121 tvp[1].tv_usec < 0 || tvp[1].tv_usec >= 1000000)
3123 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
3124 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
3130 * Common implementation code for futimens(), utimensat().
3132 #define UTIMENS_NULL 0x1
3133 #define UTIMENS_EXIT 0x2
3135 getutimens(const struct timespec *usrtsp, enum uio_seg tspseg,
3136 struct timespec *tsp, int *retflags)
3138 struct timespec tsnow;
3141 vfs_timestamp(&tsnow);
3143 if (usrtsp == NULL) {
3146 *retflags |= UTIMENS_NULL;
3149 if (tspseg == UIO_SYSSPACE) {
3152 } else if ((error = copyin(usrtsp, tsp, sizeof(*tsp) * 2)) != 0)
3154 if (tsp[0].tv_nsec == UTIME_OMIT && tsp[1].tv_nsec == UTIME_OMIT)
3155 *retflags |= UTIMENS_EXIT;
3156 if (tsp[0].tv_nsec == UTIME_NOW && tsp[1].tv_nsec == UTIME_NOW)
3157 *retflags |= UTIMENS_NULL;
3158 if (tsp[0].tv_nsec == UTIME_OMIT)
3159 tsp[0].tv_sec = VNOVAL;
3160 else if (tsp[0].tv_nsec == UTIME_NOW)
3162 else if (tsp[0].tv_nsec < 0 || tsp[0].tv_nsec >= 1000000000L)
3164 if (tsp[1].tv_nsec == UTIME_OMIT)
3165 tsp[1].tv_sec = VNOVAL;
3166 else if (tsp[1].tv_nsec == UTIME_NOW)
3168 else if (tsp[1].tv_nsec < 0 || tsp[1].tv_nsec >= 1000000000L)
3175 * Common implementation code for utimes(), lutimes(), futimes(), futimens(),
3179 setutimes(struct thread *td, struct vnode *vp, const struct timespec *ts,
3180 int numtimes, int nullflag)
3187 setbirthtime = false;
3188 vattr.va_birthtime.tv_sec = VNOVAL;
3189 vattr.va_birthtime.tv_nsec = 0;
3191 if ((error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH)) != 0)
3193 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3194 if (numtimes < 3 && VOP_GETATTR(vp, &vattr, td->td_ucred) == 0 &&
3195 timespeccmp(&ts[1], &vattr.va_birthtime, < ))
3196 setbirthtime = true;
3198 vattr.va_atime = ts[0];
3199 vattr.va_mtime = ts[1];
3201 vattr.va_birthtime = ts[1];
3203 vattr.va_birthtime = ts[2];
3205 vattr.va_vaflags |= VA_UTIMES_NULL;
3207 error = mac_vnode_check_setutimes(td->td_ucred, vp, vattr.va_atime,
3211 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3213 vn_finished_write(mp);
3218 * Set the access and modification times of a file.
3220 #ifndef _SYS_SYSPROTO_H_
3221 struct utimes_args {
3223 struct timeval *tptr;
3227 sys_utimes(struct thread *td, struct utimes_args *uap)
3230 return (kern_utimesat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
3231 uap->tptr, UIO_USERSPACE));
3234 #ifndef _SYS_SYSPROTO_H_
3235 struct futimesat_args {
3238 const struct timeval * times;
3242 sys_futimesat(struct thread *td, struct futimesat_args *uap)
3245 return (kern_utimesat(td, uap->fd, uap->path, UIO_USERSPACE,
3246 uap->times, UIO_USERSPACE));
3250 kern_utimesat(struct thread *td, int fd, const char *path,
3251 enum uio_seg pathseg, const struct timeval *tptr, enum uio_seg tptrseg)
3253 struct nameidata nd;
3254 struct timespec ts[2];
3257 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3259 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, fd,
3260 &cap_futimes_rights);
3262 if ((error = namei(&nd)) != 0)
3265 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3271 * Set the access and modification times of a file.
3273 #ifndef _SYS_SYSPROTO_H_
3274 struct lutimes_args {
3276 struct timeval *tptr;
3280 sys_lutimes(struct thread *td, struct lutimes_args *uap)
3283 return (kern_lutimes(td, uap->path, UIO_USERSPACE, uap->tptr,
3288 kern_lutimes(struct thread *td, const char *path, enum uio_seg pathseg,
3289 const struct timeval *tptr, enum uio_seg tptrseg)
3291 struct timespec ts[2];
3292 struct nameidata nd;
3295 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3297 NDINIT(&nd, LOOKUP, NOFOLLOW | AUDITVNODE1, pathseg, path);
3298 if ((error = namei(&nd)) != 0)
3301 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3307 * Set the access and modification times of a file.
3309 #ifndef _SYS_SYSPROTO_H_
3310 struct futimes_args {
3312 struct timeval *tptr;
3316 sys_futimes(struct thread *td, struct futimes_args *uap)
3319 return (kern_futimes(td, uap->fd, uap->tptr, UIO_USERSPACE));
3323 kern_futimes(struct thread *td, int fd, const struct timeval *tptr,
3324 enum uio_seg tptrseg)
3326 struct timespec ts[2];
3331 error = getutimes(tptr, tptrseg, ts);
3334 error = getvnode(td, fd, &cap_futimes_rights, &fp);
3338 if (AUDITING_TD(td)) {
3339 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
3340 AUDIT_ARG_VNODE1(fp->f_vnode);
3341 VOP_UNLOCK(fp->f_vnode);
3344 error = setutimes(td, fp->f_vnode, ts, 2, tptr == NULL);
3350 sys_futimens(struct thread *td, struct futimens_args *uap)
3353 return (kern_futimens(td, uap->fd, uap->times, UIO_USERSPACE));
3357 kern_futimens(struct thread *td, int fd, const struct timespec *tptr,
3358 enum uio_seg tptrseg)
3360 struct timespec ts[2];
3365 error = getutimens(tptr, tptrseg, ts, &flags);
3368 if (flags & UTIMENS_EXIT)
3370 error = getvnode(td, fd, &cap_futimes_rights, &fp);
3374 if (AUDITING_TD(td)) {
3375 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
3376 AUDIT_ARG_VNODE1(fp->f_vnode);
3377 VOP_UNLOCK(fp->f_vnode);
3380 error = setutimes(td, fp->f_vnode, ts, 2, flags & UTIMENS_NULL);
3386 sys_utimensat(struct thread *td, struct utimensat_args *uap)
3389 return (kern_utimensat(td, uap->fd, uap->path, UIO_USERSPACE,
3390 uap->times, UIO_USERSPACE, uap->flag));
3394 kern_utimensat(struct thread *td, int fd, const char *path,
3395 enum uio_seg pathseg, const struct timespec *tptr, enum uio_seg tptrseg,
3398 struct nameidata nd;
3399 struct timespec ts[2];
3402 if ((flag & ~(AT_SYMLINK_NOFOLLOW | AT_RESOLVE_BENEATH |
3403 AT_EMPTY_PATH)) != 0)
3406 if ((error = getutimens(tptr, tptrseg, ts, &flags)) != 0)
3408 NDINIT_ATRIGHTS(&nd, LOOKUP, at2cnpflags(flag, AT_SYMLINK_NOFOLLOW |
3409 AT_RESOLVE_BENEATH | AT_EMPTY_PATH) | AUDITVNODE1,
3410 pathseg, path, fd, &cap_futimes_rights);
3411 if ((error = namei(&nd)) != 0)
3414 * We are allowed to call namei() regardless of 2xUTIME_OMIT.
3416 * "If both tv_nsec fields are UTIME_OMIT... EACCESS may be detected."
3417 * "Search permission is denied by a component of the path prefix."
3420 if ((flags & UTIMENS_EXIT) == 0)
3421 error = setutimes(td, nd.ni_vp, ts, 2, flags & UTIMENS_NULL);
3427 * Truncate a file given its path name.
3429 #ifndef _SYS_SYSPROTO_H_
3430 struct truncate_args {
3437 sys_truncate(struct thread *td, struct truncate_args *uap)
3440 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3444 kern_truncate(struct thread *td, const char *path, enum uio_seg pathseg,
3450 struct nameidata nd;
3457 NDINIT(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path);
3458 if ((error = namei(&nd)) != 0)
3462 rl_cookie = vn_rangelock_wlock(vp, 0, OFF_MAX);
3463 if ((error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH)) != 0) {
3464 vn_rangelock_unlock(vp, rl_cookie);
3468 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3469 if (vp->v_type == VDIR) {
3474 error = mac_vnode_check_write(td->td_ucred, NOCRED, vp);
3478 error = VOP_ACCESS(vp, VWRITE, td->td_ucred, td);
3482 error = vn_truncate_locked(vp, length, false, td->td_ucred);
3485 vn_finished_write(mp);
3486 vn_rangelock_unlock(vp, rl_cookie);
3488 if (error == ERELOOKUP)
3493 #if defined(COMPAT_43)
3495 * Truncate a file given its path name.
3497 #ifndef _SYS_SYSPROTO_H_
3498 struct otruncate_args {
3504 otruncate(struct thread *td, struct otruncate_args *uap)
3507 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3509 #endif /* COMPAT_43 */
3511 #if defined(COMPAT_FREEBSD6)
3512 /* Versions with the pad argument */
3514 freebsd6_truncate(struct thread *td, struct freebsd6_truncate_args *uap)
3517 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3521 freebsd6_ftruncate(struct thread *td, struct freebsd6_ftruncate_args *uap)
3524 return (kern_ftruncate(td, uap->fd, uap->length));
3529 kern_fsync(struct thread *td, int fd, bool fullsync)
3537 error = getvnode(td, fd, &cap_fsync_rights, &fp);
3543 /* XXXKIB: compete outstanding aio writes */;
3546 error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH);
3549 vn_lock(vp, vn_lktype_write(mp, vp) | LK_RETRY);
3550 AUDIT_ARG_VNODE1(vp);
3551 if (vp->v_object != NULL) {
3552 VM_OBJECT_WLOCK(vp->v_object);
3553 vm_object_page_clean(vp->v_object, 0, 0, 0);
3554 VM_OBJECT_WUNLOCK(vp->v_object);
3556 error = fullsync ? VOP_FSYNC(vp, MNT_WAIT, td) : VOP_FDATASYNC(vp, td);
3558 vn_finished_write(mp);
3559 if (error == ERELOOKUP)
3567 * Sync an open file.
3569 #ifndef _SYS_SYSPROTO_H_
3575 sys_fsync(struct thread *td, struct fsync_args *uap)
3578 return (kern_fsync(td, uap->fd, true));
3582 sys_fdatasync(struct thread *td, struct fdatasync_args *uap)
3585 return (kern_fsync(td, uap->fd, false));
3589 * Rename files. Source and destination must either both be directories, or
3590 * both not be directories. If target is a directory, it must be empty.
3592 #ifndef _SYS_SYSPROTO_H_
3593 struct rename_args {
3599 sys_rename(struct thread *td, struct rename_args *uap)
3602 return (kern_renameat(td, AT_FDCWD, uap->from, AT_FDCWD,
3603 uap->to, UIO_USERSPACE));
3606 #ifndef _SYS_SYSPROTO_H_
3607 struct renameat_args {
3615 sys_renameat(struct thread *td, struct renameat_args *uap)
3618 return (kern_renameat(td, uap->oldfd, uap->old, uap->newfd, uap->new,
3624 kern_renameat_mac(struct thread *td, int oldfd, const char *old, int newfd,
3625 const char *new, enum uio_seg pathseg, struct nameidata *fromnd)
3629 NDINIT_ATRIGHTS(fromnd, DELETE, LOCKPARENT | LOCKLEAF | AUDITVNODE1,
3630 pathseg, old, oldfd, &cap_renameat_source_rights);
3631 if ((error = namei(fromnd)) != 0)
3633 error = mac_vnode_check_rename_from(td->td_ucred, fromnd->ni_dvp,
3634 fromnd->ni_vp, &fromnd->ni_cnd);
3635 VOP_UNLOCK(fromnd->ni_dvp);
3636 if (fromnd->ni_dvp != fromnd->ni_vp)
3637 VOP_UNLOCK(fromnd->ni_vp);
3639 NDFREE_PNBUF(fromnd);
3640 vrele(fromnd->ni_dvp);
3641 vrele(fromnd->ni_vp);
3648 kern_renameat(struct thread *td, int oldfd, const char *old, int newfd,
3649 const char *new, enum uio_seg pathseg)
3651 struct mount *mp = NULL;
3652 struct vnode *tvp, *fvp, *tdvp;
3653 struct nameidata fromnd, tond;
3660 if (mac_vnode_check_rename_from_enabled()) {
3661 error = kern_renameat_mac(td, oldfd, old, newfd, new, pathseg,
3667 NDINIT_ATRIGHTS(&fromnd, DELETE, WANTPARENT | AUDITVNODE1,
3668 pathseg, old, oldfd, &cap_renameat_source_rights);
3669 if ((error = namei(&fromnd)) != 0)
3675 tondflags = LOCKPARENT | LOCKLEAF | NOCACHE | AUDITVNODE2;
3676 if (fromnd.ni_vp->v_type == VDIR)
3677 tondflags |= WILLBEDIR;
3678 NDINIT_ATRIGHTS(&tond, RENAME, tondflags, pathseg, new, newfd,
3679 &cap_renameat_target_rights);
3680 if ((error = namei(&tond)) != 0) {
3681 /* Translate error code for rename("dir1", "dir2/."). */
3682 if (error == EISDIR && fvp->v_type == VDIR)
3684 NDFREE_PNBUF(&fromnd);
3685 vrele(fromnd.ni_dvp);
3691 error = vn_start_write(fvp, &mp, V_NOWAIT);
3693 NDFREE_PNBUF(&fromnd);
3694 NDFREE_PNBUF(&tond);
3701 vrele(fromnd.ni_dvp);
3703 error = vn_start_write(NULL, &mp, V_XSLEEP | V_PCATCH);
3709 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
3712 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
3717 if (newfd != AT_FDCWD && (tond.ni_resflags & NIRES_ABS) == 0) {
3719 * If the target already exists we require CAP_UNLINKAT
3720 * from 'newfd', when newfd was used for the lookup.
3722 error = cap_check(&tond.ni_filecaps.fc_rights,
3723 &cap_unlinkat_rights);
3734 * If the source is the same as the destination (that is, if they
3735 * are links to the same vnode), then there is nothing to do.
3741 error = mac_vnode_check_rename_to(td->td_ucred, tdvp,
3742 tond.ni_vp, fromnd.ni_dvp == tdvp, &tond.ni_cnd);
3746 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
3747 tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
3748 NDFREE_PNBUF(&fromnd);
3749 NDFREE_PNBUF(&tond);
3751 NDFREE_PNBUF(&fromnd);
3752 NDFREE_PNBUF(&tond);
3759 vrele(fromnd.ni_dvp);
3762 vn_finished_write(mp);
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 | AUDITVNODE1 |
3816 NC_NOMAKEENTRY | NC_KEEPPOSENTRY | FAILIFEXISTS | WILLBEDIR,
3817 segflg, path, fd, &cap_mkdirat_rights);
3818 if ((error = namei(&nd)) != 0)
3820 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3823 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | V_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);
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);
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) {
3928 if (nd.ni_dvp == vp)
3932 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | V_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);
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 if (__predict_false((vp->v_vflag & VV_UNLINKED) != 0)) {
4181 aiov.iov_base = buf;
4182 aiov.iov_len = count;
4183 auio.uio_iov = &aiov;
4184 auio.uio_iovcnt = 1;
4185 auio.uio_rw = UIO_READ;
4186 auio.uio_segflg = bufseg;
4188 vn_lock(vp, LK_SHARED | LK_RETRY);
4189 AUDIT_ARG_VNODE1(vp);
4190 loff = auio.uio_offset = foffset;
4192 error = mac_vnode_check_readdir(td->td_ucred, vp);
4195 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL,
4197 foffset = auio.uio_offset;
4202 if (count == auio.uio_resid &&
4203 (vp->v_vflag & VV_ROOT) &&
4204 (vp->v_mount->mnt_flag & MNT_UNION)) {
4205 struct vnode *tvp = vp;
4207 vp = vp->v_mount->mnt_vnodecovered;
4217 *residp = auio.uio_resid;
4218 td->td_retval[0] = count - auio.uio_resid;
4220 foffset_unlock(fp, foffset, 0);
4226 * Set the mode mask for creation of filesystem nodes.
4228 #ifndef _SYS_SYSPROTO_H_
4234 sys_umask(struct thread *td, struct umask_args *uap)
4236 struct pwddesc *pdp;
4238 pdp = td->td_proc->p_pd;
4240 td->td_retval[0] = pdp->pd_cmask;
4241 pdp->pd_cmask = uap->newmask & ALLPERMS;
4242 PWDDESC_XUNLOCK(pdp);
4247 * Void all references to file by ripping underlying filesystem away from
4250 #ifndef _SYS_SYSPROTO_H_
4251 struct revoke_args {
4256 sys_revoke(struct thread *td, struct revoke_args *uap)
4260 struct nameidata nd;
4263 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
4265 if ((error = namei(&nd)) != 0)
4269 if (vp->v_type != VCHR || vp->v_rdev == NULL) {
4274 error = mac_vnode_check_revoke(td->td_ucred, vp);
4278 error = VOP_GETATTR(vp, &vattr, td->td_ucred);
4281 if (td->td_ucred->cr_uid != vattr.va_uid) {
4282 error = priv_check(td, PRIV_VFS_ADMIN);
4286 if (devfs_usecount(vp) > 0)
4287 VOP_REVOKE(vp, REVOKEALL);
4294 * This variant of getvnode() allows O_PATH files. Caller should
4295 * ensure that returned file and vnode are only used for compatible
4299 getvnode_path(struct thread *td, int fd, cap_rights_t *rightsp,
4305 error = fget_unlocked(td, fd, rightsp, &fp);
4310 * The file could be not of the vnode type, or it may be not
4311 * yet fully initialized, in which case the f_vnode pointer
4312 * may be set, but f_ops is still badfileops. E.g.,
4313 * devfs_open() transiently create such situation to
4314 * facilitate csw d_fdopen().
4316 * Dupfdopen() handling in kern_openat() installs the
4317 * half-baked file into the process descriptor table, allowing
4318 * other thread to dereference it. Guard against the race by
4321 if (__predict_false(fp->f_vnode == NULL || fp->f_ops == &badfileops)) {
4332 * Convert a user file descriptor to a kernel file entry and check
4333 * that, if it is a capability, the correct rights are present.
4334 * A reference on the file entry is held upon returning.
4337 getvnode(struct thread *td, int fd, cap_rights_t *rightsp, struct file **fpp)
4341 error = getvnode_path(td, fd, rightsp, fpp);
4342 if (__predict_false(error != 0))
4346 * Filter out O_PATH file descriptors, most getvnode() callers
4347 * do not call fo_ methods.
4349 if (__predict_false((*fpp)->f_ops == &path_fileops)) {
4359 * Get an (NFS) file handle.
4361 #ifndef _SYS_SYSPROTO_H_
4362 struct lgetfh_args {
4368 sys_lgetfh(struct thread *td, struct lgetfh_args *uap)
4371 return (kern_getfhat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->fname,
4372 UIO_USERSPACE, uap->fhp, UIO_USERSPACE));
4375 #ifndef _SYS_SYSPROTO_H_
4382 sys_getfh(struct thread *td, struct getfh_args *uap)
4385 return (kern_getfhat(td, 0, AT_FDCWD, uap->fname, UIO_USERSPACE,
4386 uap->fhp, UIO_USERSPACE));
4390 * syscall for the rpc.lockd to use to translate an open descriptor into
4391 * a NFS file handle.
4393 * warning: do not remove the priv_check() call or this becomes one giant
4396 #ifndef _SYS_SYSPROTO_H_
4397 struct getfhat_args {
4405 sys_getfhat(struct thread *td, struct getfhat_args *uap)
4408 return (kern_getfhat(td, uap->flags, uap->fd, uap->path, UIO_USERSPACE,
4409 uap->fhp, UIO_USERSPACE));
4413 kern_getfhat(struct thread *td, int flags, int fd, const char *path,
4414 enum uio_seg pathseg, fhandle_t *fhp, enum uio_seg fhseg)
4416 struct nameidata nd;
4421 if ((flags & ~(AT_SYMLINK_NOFOLLOW | AT_RESOLVE_BENEATH)) != 0)
4423 error = priv_check(td, PRIV_VFS_GETFH);
4426 NDINIT_AT(&nd, LOOKUP, at2cnpflags(flags, AT_SYMLINK_NOFOLLOW |
4427 AT_RESOLVE_BENEATH) | LOCKLEAF | AUDITVNODE1, pathseg, path,
4434 bzero(&fh, sizeof(fh));
4435 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
4436 error = VOP_VPTOFH(vp, &fh.fh_fid);
4439 if (fhseg == UIO_USERSPACE)
4440 error = copyout(&fh, fhp, sizeof (fh));
4442 memcpy(fhp, &fh, sizeof(fh));
4447 #ifndef _SYS_SYSPROTO_H_
4448 struct fhlink_args {
4454 sys_fhlink(struct thread *td, struct fhlink_args *uap)
4457 return (kern_fhlinkat(td, AT_FDCWD, uap->to, UIO_USERSPACE, uap->fhp));
4460 #ifndef _SYS_SYSPROTO_H_
4461 struct fhlinkat_args {
4468 sys_fhlinkat(struct thread *td, struct fhlinkat_args *uap)
4471 return (kern_fhlinkat(td, uap->tofd, uap->to, UIO_USERSPACE, uap->fhp));
4475 kern_fhlinkat(struct thread *td, int fd, const char *path,
4476 enum uio_seg pathseg, fhandle_t *fhp)
4483 error = priv_check(td, PRIV_VFS_GETFH);
4486 error = copyin(fhp, &fh, sizeof(fh));
4491 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4493 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_SHARED, &vp);
4498 error = kern_linkat_vp(td, vp, fd, path, pathseg);
4499 } while (error == EAGAIN || error == ERELOOKUP);
4503 #ifndef _SYS_SYSPROTO_H_
4504 struct fhreadlink_args {
4511 sys_fhreadlink(struct thread *td, struct fhreadlink_args *uap)
4518 error = priv_check(td, PRIV_VFS_GETFH);
4521 if (uap->bufsize > IOSIZE_MAX)
4523 error = copyin(uap->fhp, &fh, sizeof(fh));
4526 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4528 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_SHARED, &vp);
4532 error = kern_readlink_vp(vp, uap->buf, UIO_USERSPACE, uap->bufsize, td);
4538 * syscall for the rpc.lockd to use to translate a NFS file handle into an
4541 * warning: do not remove the priv_check() call or this becomes one giant
4544 #ifndef _SYS_SYSPROTO_H_
4545 struct fhopen_args {
4546 const struct fhandle *u_fhp;
4551 sys_fhopen(struct thread *td, struct fhopen_args *uap)
4553 return (kern_fhopen(td, uap->u_fhp, uap->flags));
4557 kern_fhopen(struct thread *td, const struct fhandle *u_fhp, int flags)
4566 error = priv_check(td, PRIV_VFS_FHOPEN);
4570 fmode = FFLAGS(flags);
4571 /* why not allow a non-read/write open for our lockd? */
4572 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
4574 error = copyin(u_fhp, &fhp, sizeof(fhp));
4577 /* find the mount point */
4578 mp = vfs_busyfs(&fhp.fh_fsid);
4581 /* now give me my vnode, it gets returned to me locked */
4582 error = VFS_FHTOVP(mp, &fhp.fh_fid, LK_EXCLUSIVE, &vp);
4587 error = falloc_noinstall(td, &fp);
4593 * An extra reference on `fp' has been held for us by
4594 * falloc_noinstall().
4600 error = vn_open_vnode(vp, fmode, td->td_ucred, td, fp);
4602 KASSERT(fp->f_ops == &badfileops,
4603 ("VOP_OPEN in fhopen() set f_ops"));
4604 KASSERT(td->td_dupfd < 0,
4605 ("fhopen() encountered fdopen()"));
4614 finit_vnode(fp, fmode, NULL, &vnops);
4616 if ((fmode & O_TRUNC) != 0) {
4617 error = fo_truncate(fp, 0, td->td_ucred, td);
4622 error = finstall(td, fp, &indx, fmode, NULL);
4625 td->td_retval[0] = indx;
4630 * Stat an (NFS) file handle.
4632 #ifndef _SYS_SYSPROTO_H_
4633 struct fhstat_args {
4634 struct fhandle *u_fhp;
4639 sys_fhstat(struct thread *td, struct fhstat_args *uap)
4645 error = copyin(uap->u_fhp, &fh, sizeof(fh));
4648 error = kern_fhstat(td, fh, &sb);
4650 error = copyout(&sb, uap->sb, sizeof(sb));
4655 kern_fhstat(struct thread *td, struct fhandle fh, struct stat *sb)
4661 error = priv_check(td, PRIV_VFS_FHSTAT);
4664 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4666 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_EXCLUSIVE, &vp);
4670 error = VOP_STAT(vp, sb, td->td_ucred, NOCRED);
4676 * Implement fstatfs() for (NFS) file handles.
4678 #ifndef _SYS_SYSPROTO_H_
4679 struct fhstatfs_args {
4680 struct fhandle *u_fhp;
4685 sys_fhstatfs(struct thread *td, struct fhstatfs_args *uap)
4691 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4694 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
4695 error = kern_fhstatfs(td, fh, sfp);
4697 error = copyout(sfp, uap->buf, sizeof(*sfp));
4698 free(sfp, M_STATFS);
4703 kern_fhstatfs(struct thread *td, fhandle_t fh, struct statfs *buf)
4709 error = priv_check(td, PRIV_VFS_FHSTATFS);
4712 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4714 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_EXCLUSIVE, &vp);
4720 error = prison_canseemount(td->td_ucred, mp);
4724 error = mac_mount_check_stat(td->td_ucred, mp);
4728 error = VFS_STATFS(mp, buf);
4735 * Unlike madvise(2), we do not make a best effort to remember every
4736 * possible caching hint. Instead, we remember the last setting with
4737 * the exception that we will allow POSIX_FADV_NORMAL to adjust the
4738 * region of any current setting.
4741 kern_posix_fadvise(struct thread *td, int fd, off_t offset, off_t len,
4744 struct fadvise_info *fa, *new;
4750 if (offset < 0 || len < 0 || offset > OFF_MAX - len)
4752 AUDIT_ARG_VALUE(advice);
4754 case POSIX_FADV_SEQUENTIAL:
4755 case POSIX_FADV_RANDOM:
4756 case POSIX_FADV_NOREUSE:
4757 new = malloc(sizeof(*fa), M_FADVISE, M_WAITOK);
4759 case POSIX_FADV_NORMAL:
4760 case POSIX_FADV_WILLNEED:
4761 case POSIX_FADV_DONTNEED:
4767 /* XXX: CAP_POSIX_FADVISE? */
4769 error = fget(td, fd, &cap_no_rights, &fp);
4772 AUDIT_ARG_FILE(td->td_proc, fp);
4773 if ((fp->f_ops->fo_flags & DFLAG_SEEKABLE) == 0) {
4777 if (fp->f_type != DTYPE_VNODE) {
4782 if (vp->v_type != VREG) {
4789 end = offset + len - 1;
4791 case POSIX_FADV_SEQUENTIAL:
4792 case POSIX_FADV_RANDOM:
4793 case POSIX_FADV_NOREUSE:
4795 * Try to merge any existing non-standard region with
4796 * this new region if possible, otherwise create a new
4797 * non-standard region for this request.
4799 mtx_pool_lock(mtxpool_sleep, fp);
4801 if (fa != NULL && fa->fa_advice == advice &&
4802 ((fa->fa_start <= end && fa->fa_end >= offset) ||
4803 (end != OFF_MAX && fa->fa_start == end + 1) ||
4804 (fa->fa_end != OFF_MAX && fa->fa_end + 1 == offset))) {
4805 if (offset < fa->fa_start)
4806 fa->fa_start = offset;
4807 if (end > fa->fa_end)
4810 new->fa_advice = advice;
4811 new->fa_start = offset;
4816 mtx_pool_unlock(mtxpool_sleep, fp);
4818 case POSIX_FADV_NORMAL:
4820 * If a the "normal" region overlaps with an existing
4821 * non-standard region, trim or remove the
4822 * non-standard region.
4824 mtx_pool_lock(mtxpool_sleep, fp);
4827 if (offset <= fa->fa_start && end >= fa->fa_end) {
4829 fp->f_advice = NULL;
4830 } else if (offset <= fa->fa_start &&
4831 end >= fa->fa_start)
4832 fa->fa_start = end + 1;
4833 else if (offset <= fa->fa_end && end >= fa->fa_end)
4834 fa->fa_end = offset - 1;
4835 else if (offset >= fa->fa_start && end <= fa->fa_end) {
4837 * If the "normal" region is a middle
4838 * portion of the existing
4839 * non-standard region, just remove
4840 * the whole thing rather than picking
4841 * one side or the other to
4845 fp->f_advice = NULL;
4848 mtx_pool_unlock(mtxpool_sleep, fp);
4850 case POSIX_FADV_WILLNEED:
4851 case POSIX_FADV_DONTNEED:
4852 error = VOP_ADVISE(vp, offset, end, advice);
4858 free(new, M_FADVISE);
4863 sys_posix_fadvise(struct thread *td, struct posix_fadvise_args *uap)
4867 error = kern_posix_fadvise(td, uap->fd, uap->offset, uap->len,
4869 return (kern_posix_error(td, error));
4873 kern_copy_file_range(struct thread *td, int infd, off_t *inoffp, int outfd,
4874 off_t *outoffp, size_t len, unsigned int flags)
4876 struct file *infp, *outfp;
4877 struct vnode *invp, *outvp;
4880 void *rl_rcookie, *rl_wcookie;
4881 off_t savinoff, savoutoff;
4883 infp = outfp = NULL;
4884 rl_rcookie = rl_wcookie = NULL;
4893 if (len > SSIZE_MAX)
4895 * Although the len argument is size_t, the return argument
4896 * is ssize_t (which is signed). Therefore a size that won't
4897 * fit in ssize_t can't be returned.
4901 /* Get the file structures for the file descriptors. */
4902 error = fget_read(td, infd,
4903 inoffp != NULL ? &cap_pread_rights : &cap_read_rights, &infp);
4906 if (infp->f_ops == &badfileops) {
4910 if (infp->f_vnode == NULL) {
4914 error = fget_write(td, outfd,
4915 outoffp != NULL ? &cap_pwrite_rights : &cap_write_rights, &outfp);
4918 if (outfp->f_ops == &badfileops) {
4922 if (outfp->f_vnode == NULL) {
4927 /* Set the offset pointers to the correct place. */
4929 inoffp = &infp->f_offset;
4930 if (outoffp == NULL)
4931 outoffp = &outfp->f_offset;
4933 savoutoff = *outoffp;
4935 invp = infp->f_vnode;
4936 outvp = outfp->f_vnode;
4937 /* Sanity check the f_flag bits. */
4938 if ((outfp->f_flag & (FWRITE | FAPPEND)) != FWRITE ||
4939 (infp->f_flag & FREAD) == 0) {
4944 /* If len == 0, just return 0. */
4949 * If infp and outfp refer to the same file, the byte ranges cannot
4952 if (invp == outvp && ((savinoff <= savoutoff && savinoff + len >
4953 savoutoff) || (savinoff > savoutoff && savoutoff + len >
4959 /* Range lock the byte ranges for both invp and outvp. */
4961 rl_wcookie = vn_rangelock_wlock(outvp, *outoffp, *outoffp +
4963 rl_rcookie = vn_rangelock_tryrlock(invp, *inoffp, *inoffp +
4965 if (rl_rcookie != NULL)
4967 vn_rangelock_unlock(outvp, rl_wcookie);
4968 rl_rcookie = vn_rangelock_rlock(invp, *inoffp, *inoffp + len);
4969 vn_rangelock_unlock(invp, rl_rcookie);
4973 error = vn_copy_file_range(invp, inoffp, outvp, outoffp, &retlen,
4974 flags, infp->f_cred, outfp->f_cred, td);
4976 if (rl_rcookie != NULL)
4977 vn_rangelock_unlock(invp, rl_rcookie);
4978 if (rl_wcookie != NULL)
4979 vn_rangelock_unlock(outvp, rl_wcookie);
4980 if (savinoff != -1 && (error == EINTR || error == ERESTART)) {
4982 *outoffp = savoutoff;
4988 td->td_retval[0] = retlen;
4993 sys_copy_file_range(struct thread *td, struct copy_file_range_args *uap)
4995 off_t inoff, outoff, *inoffp, *outoffp;
4998 inoffp = outoffp = NULL;
4999 if (uap->inoffp != NULL) {
5000 error = copyin(uap->inoffp, &inoff, sizeof(off_t));
5005 if (uap->outoffp != NULL) {
5006 error = copyin(uap->outoffp, &outoff, sizeof(off_t));
5011 error = kern_copy_file_range(td, uap->infd, inoffp, uap->outfd,
5012 outoffp, uap->len, uap->flags);
5013 if (error == 0 && uap->inoffp != NULL)
5014 error = copyout(inoffp, uap->inoffp, sizeof(off_t));
5015 if (error == 0 && uap->outoffp != NULL)
5016 error = copyout(outoffp, uap->outoffp, sizeof(off_t));