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/malloc.h>
52 #include <sys/mount.h>
53 #include <sys/mutex.h>
54 #include <sys/sysproto.h>
55 #include <sys/namei.h>
56 #include <sys/filedesc.h>
57 #include <sys/kernel.h>
58 #include <sys/fcntl.h>
60 #include <sys/filio.h>
61 #include <sys/limits.h>
62 #include <sys/linker.h>
63 #include <sys/rwlock.h>
67 #include <sys/unistd.h>
68 #include <sys/vnode.h>
71 #include <sys/dirent.h>
73 #include <sys/syscallsubr.h>
74 #include <sys/sysctl.h>
76 #include <sys/ktrace.h>
79 #include <machine/stdarg.h>
81 #include <security/audit/audit.h>
82 #include <security/mac/mac_framework.h>
85 #include <vm/vm_object.h>
86 #include <vm/vm_page.h>
89 #include <fs/devfs/devfs.h>
91 #include <ufs/ufs/quota.h>
93 MALLOC_DEFINE(M_FADVISE, "fadvise", "posix_fadvise(2) information");
95 static int kern_chflagsat(struct thread *td, int fd, const char *path,
96 enum uio_seg pathseg, u_long flags, int atflag);
97 static int setfflags(struct thread *td, struct vnode *, u_long);
98 static int getutimes(const struct timeval *, enum uio_seg, struct timespec *);
99 static int getutimens(const struct timespec *, enum uio_seg,
100 struct timespec *, int *);
101 static int setutimes(struct thread *td, struct vnode *,
102 const struct timespec *, int, int);
103 static int vn_access(struct vnode *vp, int user_flags, struct ucred *cred,
105 static int kern_fhlinkat(struct thread *td, int fd, const char *path,
106 enum uio_seg pathseg, fhandle_t *fhp);
107 static int kern_readlink_vp(struct vnode *vp, char *buf, enum uio_seg bufseg,
108 size_t count, struct thread *td);
109 static int kern_linkat_vp(struct thread *td, struct vnode *vp, int fd,
110 const char *path, enum uio_seg segflag);
113 at2cnpflags(u_int at_flags, u_int mask)
117 MPASS((at_flags & (AT_SYMLINK_FOLLOW | AT_SYMLINK_NOFOLLOW)) !=
118 (AT_SYMLINK_FOLLOW | AT_SYMLINK_NOFOLLOW));
122 if ((at_flags & AT_RESOLVE_BENEATH) != 0)
124 if ((at_flags & AT_SYMLINK_FOLLOW) != 0)
126 /* NOFOLLOW is pseudo flag */
127 if ((mask & AT_SYMLINK_NOFOLLOW) != 0) {
128 res |= (at_flags & AT_SYMLINK_NOFOLLOW) != 0 ? NOFOLLOW :
131 if ((mask & AT_EMPTY_PATH) != 0 && (at_flags & AT_EMPTY_PATH) != 0)
137 kern_sync(struct thread *td)
139 struct mount *mp, *nmp;
142 mtx_lock(&mountlist_mtx);
143 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
144 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK)) {
145 nmp = TAILQ_NEXT(mp, mnt_list);
148 if ((mp->mnt_flag & MNT_RDONLY) == 0 &&
149 vn_start_write(NULL, &mp, V_NOWAIT) == 0) {
150 save = curthread_pflags_set(TDP_SYNCIO);
151 vfs_periodic(mp, MNT_NOWAIT);
152 VFS_SYNC(mp, MNT_NOWAIT);
153 curthread_pflags_restore(save);
154 vn_finished_write(mp);
156 mtx_lock(&mountlist_mtx);
157 nmp = TAILQ_NEXT(mp, mnt_list);
160 mtx_unlock(&mountlist_mtx);
165 * Sync each mounted filesystem.
167 #ifndef _SYS_SYSPROTO_H_
174 sys_sync(struct thread *td, struct sync_args *uap)
177 return (kern_sync(td));
181 * Change filesystem quotas.
183 #ifndef _SYS_SYSPROTO_H_
184 struct quotactl_args {
192 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);
215 error = VFS_QUOTACTL(mp, uap->cmd, uap->uid, uap->arg);
218 * Since quota on operation typically needs to open quota
219 * file, the Q_QUOTAON handler needs to unbusy the mount point
220 * before calling into namei. Otherwise, unmount might be
221 * started between two vfs_busy() invocations (first is our,
222 * second is from mount point cross-walk code in lookup()),
225 * Require that Q_QUOTAON handles the vfs_busy() reference on
226 * its own, always returning with ubusied mount point.
228 if ((uap->cmd >> SUBCMDSHIFT) != Q_QUOTAON &&
229 (uap->cmd >> SUBCMDSHIFT) != Q_QUOTAOFF)
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 | WANTIOCTLCAPS,
1156 pathseg, path, fd, &rights, td);
1157 td->td_dupfd = -1; /* XXX check for fdopen */
1158 error = vn_open_cred(&nd, &flags, cmode, VN_OPEN_WANTIOCTLCAPS,
1162 * If the vn_open replaced the method vector, something
1163 * wonderous happened deep below and we just pass it up
1164 * pretending we know what we do.
1166 if (error == ENXIO && fp->f_ops != &badfileops) {
1167 MPASS((flags & O_PATH) == 0);
1172 * Handle special fdopen() case. bleh.
1174 * Don't do this for relative (capability) lookups; we don't
1175 * understand exactly what would happen, and we don't think
1176 * that it ever should.
1178 if ((nd.ni_resflags & NIRES_STRICTREL) == 0 &&
1179 (error == ENODEV || error == ENXIO) &&
1180 td->td_dupfd >= 0) {
1181 error = dupfdopen(td, fdp, td->td_dupfd, flags, error,
1190 NDFREE(&nd, NDF_ONLY_PNBUF);
1194 * Store the vnode, for any f_type. Typically, the vnode use
1195 * count is decremented by direct call to vn_closefile() for
1196 * files that switched type in the cdevsw fdopen() method.
1201 * If the file wasn't claimed by devfs bind it to the normal
1202 * vnode operations here.
1204 if (fp->f_ops == &badfileops) {
1205 KASSERT(vp->v_type != VFIFO || (flags & O_PATH) != 0,
1206 ("Unexpected fifo fp %p vp %p", fp, vp));
1207 if ((flags & O_PATH) != 0) {
1208 finit(fp, (flags & FMASK) | (fp->f_flag & FKQALLOWED),
1209 DTYPE_VNODE, NULL, &path_fileops);
1211 finit_vnode(fp, flags, NULL, &vnops);
1216 if (flags & O_TRUNC) {
1217 error = fo_truncate(fp, 0, td->td_ucred, td);
1223 * If we haven't already installed the FD (for dupfdopen), do so now.
1226 struct filecaps *fcaps;
1229 if ((nd.ni_resflags & NIRES_STRICTREL) != 0)
1230 fcaps = &nd.ni_filecaps;
1234 error = finstall_refed(td, fp, &indx, flags, fcaps);
1235 /* On success finstall_refed() consumes fcaps. */
1240 NDFREE_IOCTLCAPS(&nd);
1241 falloc_abort(td, fp);
1244 td->td_retval[0] = indx;
1247 KASSERT(indx == -1, ("indx=%d, should be -1", indx));
1248 NDFREE_IOCTLCAPS(&nd);
1249 falloc_abort(td, fp);
1257 #ifndef _SYS_SYSPROTO_H_
1258 struct ocreat_args {
1264 ocreat(struct thread *td, struct ocreat_args *uap)
1267 return (kern_openat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1268 O_WRONLY | O_CREAT | O_TRUNC, uap->mode));
1270 #endif /* COMPAT_43 */
1273 * Create a special file.
1275 #ifndef _SYS_SYSPROTO_H_
1276 struct mknodat_args {
1284 sys_mknodat(struct thread *td, struct mknodat_args *uap)
1287 return (kern_mknodat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode,
1291 #if defined(COMPAT_FREEBSD11)
1293 freebsd11_mknod(struct thread *td,
1294 struct freebsd11_mknod_args *uap)
1297 return (kern_mknodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1298 uap->mode, uap->dev));
1302 freebsd11_mknodat(struct thread *td,
1303 struct freebsd11_mknodat_args *uap)
1306 return (kern_mknodat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode,
1309 #endif /* COMPAT_FREEBSD11 */
1312 kern_mknodat(struct thread *td, int fd, const char *path, enum uio_seg pathseg,
1313 int mode, dev_t dev)
1318 struct nameidata nd;
1319 int error, whiteout = 0;
1321 AUDIT_ARG_MODE(mode);
1323 switch (mode & S_IFMT) {
1326 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
1327 if (error == 0 && dev == VNOVAL)
1331 error = priv_check(td, PRIV_VFS_MKNOD_WHT);
1335 return (kern_mkfifoat(td, fd, path, pathseg, mode));
1346 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
1347 NOCACHE, pathseg, path, fd, &cap_mknodat_rights,
1349 if ((error = namei(&nd)) != 0)
1353 NDFREE(&nd, NDF_ONLY_PNBUF);
1354 if (vp == nd.ni_dvp)
1362 vattr.va_mode = (mode & ALLPERMS) &
1363 ~td->td_proc->p_pd->pd_cmask;
1364 vattr.va_rdev = dev;
1367 switch (mode & S_IFMT) {
1369 vattr.va_type = VCHR;
1372 vattr.va_type = VBLK;
1378 panic("kern_mknod: invalid mode");
1381 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1382 NDFREE(&nd, NDF_ONLY_PNBUF);
1384 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1389 if (error == 0 && !whiteout)
1390 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp,
1391 &nd.ni_cnd, &vattr);
1395 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, CREATE);
1397 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp,
1398 &nd.ni_cnd, &vattr);
1401 VOP_VPUT_PAIR(nd.ni_dvp, error == 0 && !whiteout ? &nd.ni_vp : NULL,
1403 vn_finished_write(mp);
1404 NDFREE(&nd, NDF_ONLY_PNBUF);
1405 if (error == ERELOOKUP)
1411 * Create a named pipe.
1413 #ifndef _SYS_SYSPROTO_H_
1414 struct mkfifo_args {
1420 sys_mkfifo(struct thread *td, struct mkfifo_args *uap)
1423 return (kern_mkfifoat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1427 #ifndef _SYS_SYSPROTO_H_
1428 struct mkfifoat_args {
1435 sys_mkfifoat(struct thread *td, struct mkfifoat_args *uap)
1438 return (kern_mkfifoat(td, uap->fd, uap->path, UIO_USERSPACE,
1443 kern_mkfifoat(struct thread *td, int fd, const char *path,
1444 enum uio_seg pathseg, int mode)
1448 struct nameidata nd;
1451 AUDIT_ARG_MODE(mode);
1455 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
1456 NOCACHE, pathseg, path, fd, &cap_mkfifoat_rights,
1458 if ((error = namei(&nd)) != 0)
1460 if (nd.ni_vp != NULL) {
1461 NDFREE(&nd, NDF_ONLY_PNBUF);
1462 if (nd.ni_vp == nd.ni_dvp)
1469 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1470 NDFREE(&nd, NDF_ONLY_PNBUF);
1472 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1477 vattr.va_type = VFIFO;
1478 vattr.va_mode = (mode & ALLPERMS) & ~td->td_proc->p_pd->pd_cmask;
1480 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1485 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
1489 VOP_VPUT_PAIR(nd.ni_dvp, error == 0 ? &nd.ni_vp : NULL, true);
1490 vn_finished_write(mp);
1491 NDFREE(&nd, NDF_ONLY_PNBUF);
1492 if (error == ERELOOKUP)
1498 * Make a hard file link.
1500 #ifndef _SYS_SYSPROTO_H_
1507 sys_link(struct thread *td, struct link_args *uap)
1510 return (kern_linkat(td, AT_FDCWD, AT_FDCWD, uap->path, uap->link,
1511 UIO_USERSPACE, AT_SYMLINK_FOLLOW));
1514 #ifndef _SYS_SYSPROTO_H_
1515 struct linkat_args {
1524 sys_linkat(struct thread *td, struct linkat_args *uap)
1527 return (kern_linkat(td, uap->fd1, uap->fd2, uap->path1, uap->path2,
1528 UIO_USERSPACE, uap->flag));
1531 int hardlink_check_uid = 0;
1532 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
1533 &hardlink_check_uid, 0,
1534 "Unprivileged processes cannot create hard links to files owned by other "
1536 static int hardlink_check_gid = 0;
1537 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
1538 &hardlink_check_gid, 0,
1539 "Unprivileged processes cannot create hard links to files owned by other "
1543 can_hardlink(struct vnode *vp, struct ucred *cred)
1548 if (!hardlink_check_uid && !hardlink_check_gid)
1551 error = VOP_GETATTR(vp, &va, cred);
1555 if (hardlink_check_uid && cred->cr_uid != va.va_uid) {
1556 error = priv_check_cred(cred, PRIV_VFS_LINK);
1561 if (hardlink_check_gid && !groupmember(va.va_gid, cred)) {
1562 error = priv_check_cred(cred, PRIV_VFS_LINK);
1571 kern_linkat(struct thread *td, int fd1, int fd2, const char *path1,
1572 const char *path2, enum uio_seg segflag, int flag)
1574 struct nameidata nd;
1577 if ((flag & ~(AT_SYMLINK_FOLLOW | AT_RESOLVE_BENEATH |
1578 AT_EMPTY_PATH)) != 0)
1584 NDINIT_ATRIGHTS(&nd, LOOKUP, AUDITVNODE1 | at2cnpflags(flag,
1585 AT_SYMLINK_FOLLOW | AT_RESOLVE_BENEATH | AT_EMPTY_PATH),
1586 segflag, path1, fd1, &cap_linkat_source_rights, td);
1587 if ((error = namei(&nd)) != 0)
1589 NDFREE(&nd, NDF_ONLY_PNBUF);
1590 if ((nd.ni_resflags & NIRES_EMPTYPATH) != 0) {
1591 error = priv_check(td, PRIV_VFS_FHOPEN);
1597 error = kern_linkat_vp(td, nd.ni_vp, fd2, path2, segflag);
1598 } while (error == EAGAIN || error == ERELOOKUP);
1603 kern_linkat_vp(struct thread *td, struct vnode *vp, int fd, const char *path,
1604 enum uio_seg segflag)
1606 struct nameidata nd;
1610 if (vp->v_type == VDIR) {
1612 return (EPERM); /* POSIX */
1614 NDINIT_ATRIGHTS(&nd, CREATE,
1615 LOCKPARENT | SAVENAME | AUDITVNODE2 | NOCACHE, segflag, path, fd,
1616 &cap_linkat_target_rights, td);
1617 if ((error = namei(&nd)) == 0) {
1618 if (nd.ni_vp != NULL) {
1619 NDFREE(&nd, NDF_ONLY_PNBUF);
1620 if (nd.ni_dvp == nd.ni_vp)
1627 } else if (nd.ni_dvp->v_mount != vp->v_mount) {
1629 * Cross-device link. No need to recheck
1630 * vp->v_type, since it cannot change, except
1633 NDFREE(&nd, NDF_ONLY_PNBUF);
1637 } else if ((error = vn_lock(vp, LK_EXCLUSIVE)) == 0) {
1638 error = can_hardlink(vp, td->td_ucred);
1641 error = mac_vnode_check_link(td->td_ucred,
1642 nd.ni_dvp, vp, &nd.ni_cnd);
1647 NDFREE(&nd, NDF_ONLY_PNBUF);
1650 error = vn_start_write(vp, &mp, V_NOWAIT);
1654 NDFREE(&nd, NDF_ONLY_PNBUF);
1655 error = vn_start_write(NULL, &mp,
1661 error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
1662 VOP_VPUT_PAIR(nd.ni_dvp, &vp, true);
1663 vn_finished_write(mp);
1664 NDFREE(&nd, NDF_ONLY_PNBUF);
1668 NDFREE(&nd, NDF_ONLY_PNBUF);
1679 * Make a symbolic link.
1681 #ifndef _SYS_SYSPROTO_H_
1682 struct symlink_args {
1688 sys_symlink(struct thread *td, struct symlink_args *uap)
1691 return (kern_symlinkat(td, uap->path, AT_FDCWD, uap->link,
1695 #ifndef _SYS_SYSPROTO_H_
1696 struct symlinkat_args {
1703 sys_symlinkat(struct thread *td, struct symlinkat_args *uap)
1706 return (kern_symlinkat(td, uap->path1, uap->fd, uap->path2,
1711 kern_symlinkat(struct thread *td, const char *path1, int fd, const char *path2,
1712 enum uio_seg segflg)
1716 const char *syspath;
1718 struct nameidata nd;
1721 if (segflg == UIO_SYSSPACE) {
1724 tmppath = uma_zalloc(namei_zone, M_WAITOK);
1725 if ((error = copyinstr(path1, tmppath, MAXPATHLEN, NULL)) != 0)
1729 AUDIT_ARG_TEXT(syspath);
1733 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
1734 NOCACHE, segflg, path2, fd, &cap_symlinkat_rights,
1736 if ((error = namei(&nd)) != 0)
1739 NDFREE(&nd, NDF_ONLY_PNBUF);
1740 if (nd.ni_vp == nd.ni_dvp)
1749 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1750 NDFREE(&nd, NDF_ONLY_PNBUF);
1752 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1757 vattr.va_mode = ACCESSPERMS &~ td->td_proc->p_pd->pd_cmask;
1759 vattr.va_type = VLNK;
1760 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1765 error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr, syspath);
1769 VOP_VPUT_PAIR(nd.ni_dvp, error == 0 ? &nd.ni_vp : NULL, true);
1770 vn_finished_write(mp);
1771 NDFREE(&nd, NDF_ONLY_PNBUF);
1772 if (error == ERELOOKUP)
1775 if (segflg != UIO_SYSSPACE)
1776 uma_zfree(namei_zone, tmppath);
1781 * Delete a whiteout from the filesystem.
1783 #ifndef _SYS_SYSPROTO_H_
1784 struct undelete_args {
1789 sys_undelete(struct thread *td, struct undelete_args *uap)
1792 struct nameidata nd;
1798 NDINIT(&nd, DELETE, LOCKPARENT | DOWHITEOUT | AUDITVNODE1,
1799 UIO_USERSPACE, uap->path, td);
1804 if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & ISWHITEOUT)) {
1805 NDFREE(&nd, NDF_ONLY_PNBUF);
1806 if (nd.ni_vp == nd.ni_dvp)
1814 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1815 NDFREE(&nd, NDF_ONLY_PNBUF);
1817 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1821 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, DELETE);
1822 NDFREE(&nd, NDF_ONLY_PNBUF);
1824 vn_finished_write(mp);
1825 if (error == ERELOOKUP)
1831 * Delete a name from the filesystem.
1833 #ifndef _SYS_SYSPROTO_H_
1834 struct unlink_args {
1839 sys_unlink(struct thread *td, struct unlink_args *uap)
1842 return (kern_funlinkat(td, AT_FDCWD, uap->path, FD_NONE, UIO_USERSPACE,
1847 kern_funlinkat_ex(struct thread *td, int dfd, const char *path, int fd,
1848 int flag, enum uio_seg pathseg, ino_t oldinum)
1851 if ((flag & ~(AT_REMOVEDIR | AT_RESOLVE_BENEATH)) != 0)
1854 if ((flag & AT_REMOVEDIR) != 0)
1855 return (kern_frmdirat(td, dfd, path, fd, UIO_USERSPACE, 0));
1857 return (kern_funlinkat(td, dfd, path, fd, UIO_USERSPACE, 0, 0));
1860 #ifndef _SYS_SYSPROTO_H_
1861 struct unlinkat_args {
1868 sys_unlinkat(struct thread *td, struct unlinkat_args *uap)
1871 return (kern_funlinkat_ex(td, uap->fd, uap->path, FD_NONE, uap->flag,
1875 #ifndef _SYS_SYSPROTO_H_
1876 struct funlinkat_args {
1884 sys_funlinkat(struct thread *td, struct funlinkat_args *uap)
1887 return (kern_funlinkat_ex(td, uap->dfd, uap->path, uap->fd, uap->flag,
1892 kern_funlinkat(struct thread *td, int dfd, const char *path, int fd,
1893 enum uio_seg pathseg, int flag, ino_t oldinum)
1898 struct nameidata nd;
1903 if (fd != FD_NONE) {
1904 error = getvnode_path(td, fd, &cap_no_rights, &fp);
1912 NDINIT_ATRIGHTS(&nd, DELETE, LOCKPARENT | LOCKLEAF | AUDITVNODE1 |
1913 at2cnpflags(flag, AT_RESOLVE_BENEATH),
1914 pathseg, path, dfd, &cap_unlinkat_rights, td);
1915 if ((error = namei(&nd)) != 0) {
1916 if (error == EINVAL)
1921 if (vp->v_type == VDIR && oldinum == 0) {
1922 error = EPERM; /* POSIX */
1923 } else if (oldinum != 0 &&
1924 ((error = VOP_STAT(vp, &sb, td->td_ucred, NOCRED, td)) == 0) &&
1925 sb.st_ino != oldinum) {
1926 error = EIDRM; /* Identifier removed */
1927 } else if (fp != NULL && fp->f_vnode != vp) {
1928 if (VN_IS_DOOMED(fp->f_vnode))
1934 * The root of a mounted filesystem cannot be deleted.
1936 * XXX: can this only be a VDIR case?
1938 if (vp->v_vflag & VV_ROOT)
1942 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1943 NDFREE(&nd, NDF_ONLY_PNBUF);
1945 if (vp == nd.ni_dvp)
1949 if ((error = vn_start_write(NULL, &mp,
1950 V_XSLEEP | PCATCH)) != 0) {
1956 error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp,
1961 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_UNLINK);
1962 error = VOP_REMOVE(nd.ni_dvp, vp, &nd.ni_cnd);
1966 vn_finished_write(mp);
1968 NDFREE(&nd, NDF_ONLY_PNBUF);
1970 if (vp == nd.ni_dvp)
1974 if (error == ERELOOKUP)
1983 * Reposition read/write file offset.
1985 #ifndef _SYS_SYSPROTO_H_
1994 sys_lseek(struct thread *td, struct lseek_args *uap)
1997 return (kern_lseek(td, uap->fd, uap->offset, uap->whence));
2001 kern_lseek(struct thread *td, int fd, off_t offset, int whence)
2007 error = fget(td, fd, &cap_seek_rights, &fp);
2010 error = (fp->f_ops->fo_flags & DFLAG_SEEKABLE) != 0 ?
2011 fo_seek(fp, offset, whence, td) : ESPIPE;
2016 #if defined(COMPAT_43)
2018 * Reposition read/write file offset.
2020 #ifndef _SYS_SYSPROTO_H_
2021 struct olseek_args {
2028 olseek(struct thread *td, struct olseek_args *uap)
2031 return (kern_lseek(td, uap->fd, uap->offset, uap->whence));
2033 #endif /* COMPAT_43 */
2035 #if defined(COMPAT_FREEBSD6)
2036 /* Version with the 'pad' argument */
2038 freebsd6_lseek(struct thread *td, struct freebsd6_lseek_args *uap)
2041 return (kern_lseek(td, uap->fd, uap->offset, uap->whence));
2046 * Check access permissions using passed credentials.
2049 vn_access(struct vnode *vp, int user_flags, struct ucred *cred,
2055 /* Flags == 0 means only check for existence. */
2056 if (user_flags == 0)
2060 if (user_flags & R_OK)
2062 if (user_flags & W_OK)
2064 if (user_flags & X_OK)
2067 error = mac_vnode_check_access(cred, vp, accmode);
2071 if ((accmode & VWRITE) == 0 || (error = vn_writechk(vp)) == 0)
2072 error = VOP_ACCESS(vp, accmode, cred, td);
2077 * Check access permissions using "real" credentials.
2079 #ifndef _SYS_SYSPROTO_H_
2080 struct access_args {
2086 sys_access(struct thread *td, struct access_args *uap)
2089 return (kern_accessat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2093 #ifndef _SYS_SYSPROTO_H_
2094 struct faccessat_args {
2102 sys_faccessat(struct thread *td, struct faccessat_args *uap)
2105 return (kern_accessat(td, uap->fd, uap->path, UIO_USERSPACE, uap->flag,
2110 kern_accessat(struct thread *td, int fd, const char *path,
2111 enum uio_seg pathseg, int flag, int amode)
2113 struct ucred *cred, *usecred;
2115 struct nameidata nd;
2118 if ((flag & ~(AT_EACCESS | AT_RESOLVE_BENEATH | AT_EMPTY_PATH)) != 0)
2120 if (amode != F_OK && (amode & ~(R_OK | W_OK | X_OK)) != 0)
2124 * Create and modify a temporary credential instead of one that
2125 * is potentially shared (if we need one).
2127 cred = td->td_ucred;
2128 if ((flag & AT_EACCESS) == 0 &&
2129 ((cred->cr_uid != cred->cr_ruid ||
2130 cred->cr_rgid != cred->cr_groups[0]))) {
2131 usecred = crdup(cred);
2132 usecred->cr_uid = cred->cr_ruid;
2133 usecred->cr_groups[0] = cred->cr_rgid;
2134 td->td_ucred = usecred;
2137 AUDIT_ARG_VALUE(amode);
2138 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF |
2139 AUDITVNODE1 | at2cnpflags(flag, AT_RESOLVE_BENEATH |
2140 AT_EMPTY_PATH), pathseg, path, fd, &cap_fstat_rights, td);
2141 if ((error = namei(&nd)) != 0)
2145 error = vn_access(vp, amode, usecred, td);
2146 NDFREE_NOTHING(&nd);
2149 if (usecred != cred) {
2150 td->td_ucred = cred;
2157 * Check access permissions using "effective" credentials.
2159 #ifndef _SYS_SYSPROTO_H_
2160 struct eaccess_args {
2166 sys_eaccess(struct thread *td, struct eaccess_args *uap)
2169 return (kern_accessat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2170 AT_EACCESS, uap->amode));
2173 #if defined(COMPAT_43)
2175 * Get file status; this version follows links.
2177 #ifndef _SYS_SYSPROTO_H_
2184 ostat(struct thread *td, struct ostat_args *uap)
2190 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
2195 return (copyout(&osb, uap->ub, sizeof (osb)));
2199 * Get file status; this version does not follow links.
2201 #ifndef _SYS_SYSPROTO_H_
2202 struct olstat_args {
2208 olstat(struct thread *td, struct olstat_args *uap)
2214 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2215 UIO_USERSPACE, &sb, NULL);
2219 return (copyout(&osb, uap->ub, sizeof (osb)));
2223 * Convert from an old to a new stat structure.
2224 * XXX: many values are blindly truncated.
2227 cvtstat(struct stat *st, struct ostat *ost)
2230 bzero(ost, sizeof(*ost));
2231 ost->st_dev = st->st_dev;
2232 ost->st_ino = st->st_ino;
2233 ost->st_mode = st->st_mode;
2234 ost->st_nlink = st->st_nlink;
2235 ost->st_uid = st->st_uid;
2236 ost->st_gid = st->st_gid;
2237 ost->st_rdev = st->st_rdev;
2238 ost->st_size = MIN(st->st_size, INT32_MAX);
2239 ost->st_atim = st->st_atim;
2240 ost->st_mtim = st->st_mtim;
2241 ost->st_ctim = st->st_ctim;
2242 ost->st_blksize = st->st_blksize;
2243 ost->st_blocks = st->st_blocks;
2244 ost->st_flags = st->st_flags;
2245 ost->st_gen = st->st_gen;
2247 #endif /* COMPAT_43 */
2249 #if defined(COMPAT_43) || defined(COMPAT_FREEBSD11)
2250 int ino64_trunc_error;
2251 SYSCTL_INT(_vfs, OID_AUTO, ino64_trunc_error, CTLFLAG_RW,
2252 &ino64_trunc_error, 0,
2253 "Error on truncation of device, file or inode number, or link count");
2256 freebsd11_cvtstat(struct stat *st, struct freebsd11_stat *ost)
2259 ost->st_dev = st->st_dev;
2260 if (ost->st_dev != st->st_dev) {
2261 switch (ino64_trunc_error) {
2264 * Since dev_t is almost raw, don't clamp to the
2265 * maximum for case 2, but ignore the error.
2272 ost->st_ino = st->st_ino;
2273 if (ost->st_ino != st->st_ino) {
2274 switch (ino64_trunc_error) {
2281 ost->st_ino = UINT32_MAX;
2285 ost->st_mode = st->st_mode;
2286 ost->st_nlink = st->st_nlink;
2287 if (ost->st_nlink != st->st_nlink) {
2288 switch (ino64_trunc_error) {
2295 ost->st_nlink = UINT16_MAX;
2299 ost->st_uid = st->st_uid;
2300 ost->st_gid = st->st_gid;
2301 ost->st_rdev = st->st_rdev;
2302 if (ost->st_rdev != st->st_rdev) {
2303 switch (ino64_trunc_error) {
2310 ost->st_atim = st->st_atim;
2311 ost->st_mtim = st->st_mtim;
2312 ost->st_ctim = st->st_ctim;
2313 ost->st_size = st->st_size;
2314 ost->st_blocks = st->st_blocks;
2315 ost->st_blksize = st->st_blksize;
2316 ost->st_flags = st->st_flags;
2317 ost->st_gen = st->st_gen;
2319 ost->st_birthtim = st->st_birthtim;
2320 bzero((char *)&ost->st_birthtim + sizeof(ost->st_birthtim),
2321 sizeof(*ost) - offsetof(struct freebsd11_stat,
2322 st_birthtim) - sizeof(ost->st_birthtim));
2327 freebsd11_stat(struct thread *td, struct freebsd11_stat_args* uap)
2330 struct freebsd11_stat osb;
2333 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
2337 error = freebsd11_cvtstat(&sb, &osb);
2339 error = copyout(&osb, uap->ub, sizeof(osb));
2344 freebsd11_lstat(struct thread *td, struct freebsd11_lstat_args* uap)
2347 struct freebsd11_stat osb;
2350 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2351 UIO_USERSPACE, &sb, NULL);
2354 error = freebsd11_cvtstat(&sb, &osb);
2356 error = copyout(&osb, uap->ub, sizeof(osb));
2361 freebsd11_fhstat(struct thread *td, struct freebsd11_fhstat_args* uap)
2365 struct freebsd11_stat osb;
2368 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
2371 error = kern_fhstat(td, fh, &sb);
2374 error = freebsd11_cvtstat(&sb, &osb);
2376 error = copyout(&osb, uap->sb, sizeof(osb));
2381 freebsd11_fstatat(struct thread *td, struct freebsd11_fstatat_args* uap)
2384 struct freebsd11_stat osb;
2387 error = kern_statat(td, uap->flag, uap->fd, uap->path,
2388 UIO_USERSPACE, &sb, NULL);
2391 error = freebsd11_cvtstat(&sb, &osb);
2393 error = copyout(&osb, uap->buf, sizeof(osb));
2396 #endif /* COMPAT_FREEBSD11 */
2401 #ifndef _SYS_SYSPROTO_H_
2402 struct fstatat_args {
2410 sys_fstatat(struct thread *td, struct fstatat_args *uap)
2415 error = kern_statat(td, uap->flag, uap->fd, uap->path,
2416 UIO_USERSPACE, &sb, NULL);
2418 error = copyout(&sb, uap->buf, sizeof (sb));
2423 kern_statat(struct thread *td, int flag, int fd, const char *path,
2424 enum uio_seg pathseg, struct stat *sbp,
2425 void (*hook)(struct vnode *vp, struct stat *sbp))
2427 struct nameidata nd;
2430 if ((flag & ~(AT_SYMLINK_NOFOLLOW | AT_RESOLVE_BENEATH |
2431 AT_EMPTY_PATH)) != 0)
2434 NDINIT_ATRIGHTS(&nd, LOOKUP, at2cnpflags(flag, AT_RESOLVE_BENEATH |
2435 AT_SYMLINK_NOFOLLOW | AT_EMPTY_PATH) | LOCKSHARED | LOCKLEAF |
2436 AUDITVNODE1, pathseg, path, fd, &cap_fstat_rights, td);
2438 if ((error = namei(&nd)) != 0) {
2439 if (error == ENOTDIR &&
2440 (nd.ni_resflags & NIRES_EMPTYPATH) != 0)
2441 error = kern_fstat(td, fd, sbp);
2444 error = VOP_STAT(nd.ni_vp, sbp, td->td_ucred, NOCRED, td);
2446 if (__predict_false(hook != NULL))
2447 hook(nd.ni_vp, sbp);
2449 NDFREE_NOTHING(&nd);
2451 #ifdef __STAT_TIME_T_EXT
2452 sbp->st_atim_ext = 0;
2453 sbp->st_mtim_ext = 0;
2454 sbp->st_ctim_ext = 0;
2455 sbp->st_btim_ext = 0;
2458 if (KTRPOINT(td, KTR_STRUCT))
2459 ktrstat_error(sbp, error);
2464 #if defined(COMPAT_FREEBSD11)
2466 * Implementation of the NetBSD [l]stat() functions.
2469 freebsd11_cvtnstat(struct stat *sb, struct nstat *nsb)
2472 bzero(nsb, sizeof(*nsb));
2473 nsb->st_dev = sb->st_dev;
2474 nsb->st_ino = sb->st_ino;
2475 nsb->st_mode = sb->st_mode;
2476 nsb->st_nlink = sb->st_nlink;
2477 nsb->st_uid = sb->st_uid;
2478 nsb->st_gid = sb->st_gid;
2479 nsb->st_rdev = sb->st_rdev;
2480 nsb->st_atim = sb->st_atim;
2481 nsb->st_mtim = sb->st_mtim;
2482 nsb->st_ctim = sb->st_ctim;
2483 nsb->st_size = sb->st_size;
2484 nsb->st_blocks = sb->st_blocks;
2485 nsb->st_blksize = sb->st_blksize;
2486 nsb->st_flags = sb->st_flags;
2487 nsb->st_gen = sb->st_gen;
2488 nsb->st_birthtim = sb->st_birthtim;
2491 #ifndef _SYS_SYSPROTO_H_
2492 struct freebsd11_nstat_args {
2498 freebsd11_nstat(struct thread *td, struct freebsd11_nstat_args *uap)
2504 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
2508 freebsd11_cvtnstat(&sb, &nsb);
2509 return (copyout(&nsb, uap->ub, sizeof (nsb)));
2513 * NetBSD lstat. Get file status; this version does not follow links.
2515 #ifndef _SYS_SYSPROTO_H_
2516 struct freebsd11_nlstat_args {
2522 freebsd11_nlstat(struct thread *td, struct freebsd11_nlstat_args *uap)
2528 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2529 UIO_USERSPACE, &sb, NULL);
2532 freebsd11_cvtnstat(&sb, &nsb);
2533 return (copyout(&nsb, uap->ub, sizeof (nsb)));
2535 #endif /* COMPAT_FREEBSD11 */
2538 * Get configurable pathname variables.
2540 #ifndef _SYS_SYSPROTO_H_
2541 struct pathconf_args {
2547 sys_pathconf(struct thread *td, struct pathconf_args *uap)
2552 error = kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name, FOLLOW,
2555 td->td_retval[0] = value;
2559 #ifndef _SYS_SYSPROTO_H_
2560 struct lpathconf_args {
2566 sys_lpathconf(struct thread *td, struct lpathconf_args *uap)
2571 error = kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name,
2574 td->td_retval[0] = value;
2579 kern_pathconf(struct thread *td, const char *path, enum uio_seg pathseg,
2580 int name, u_long flags, long *valuep)
2582 struct nameidata nd;
2585 NDINIT(&nd, LOOKUP, LOCKSHARED | LOCKLEAF | AUDITVNODE1 | flags,
2587 if ((error = namei(&nd)) != 0)
2589 NDFREE_NOTHING(&nd);
2591 error = VOP_PATHCONF(nd.ni_vp, name, valuep);
2597 * Return target name of a symbolic link.
2599 #ifndef _SYS_SYSPROTO_H_
2600 struct readlink_args {
2607 sys_readlink(struct thread *td, struct readlink_args *uap)
2610 return (kern_readlinkat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2611 uap->buf, UIO_USERSPACE, uap->count));
2613 #ifndef _SYS_SYSPROTO_H_
2614 struct readlinkat_args {
2622 sys_readlinkat(struct thread *td, struct readlinkat_args *uap)
2625 return (kern_readlinkat(td, uap->fd, uap->path, UIO_USERSPACE,
2626 uap->buf, UIO_USERSPACE, uap->bufsize));
2630 kern_readlinkat(struct thread *td, int fd, const char *path,
2631 enum uio_seg pathseg, char *buf, enum uio_seg bufseg, size_t count)
2634 struct nameidata nd;
2637 if (count > IOSIZE_MAX)
2640 NDINIT_AT(&nd, LOOKUP, NOFOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1 |
2641 EMPTYPATH, pathseg, path, fd, td);
2643 if ((error = namei(&nd)) != 0)
2645 NDFREE_NOTHING(&nd);
2648 error = kern_readlink_vp(vp, buf, bufseg, count, td);
2655 * Helper function to readlink from a vnode
2658 kern_readlink_vp(struct vnode *vp, char *buf, enum uio_seg bufseg, size_t count,
2665 ASSERT_VOP_LOCKED(vp, "kern_readlink_vp(): vp not locked");
2667 error = mac_vnode_check_readlink(td->td_ucred, vp);
2671 if (vp->v_type != VLNK && (vp->v_vflag & VV_READLINK) == 0)
2674 aiov.iov_base = buf;
2675 aiov.iov_len = count;
2676 auio.uio_iov = &aiov;
2677 auio.uio_iovcnt = 1;
2678 auio.uio_offset = 0;
2679 auio.uio_rw = UIO_READ;
2680 auio.uio_segflg = bufseg;
2682 auio.uio_resid = count;
2683 error = VOP_READLINK(vp, &auio, td->td_ucred);
2684 td->td_retval[0] = count - auio.uio_resid;
2689 * Common implementation code for chflags() and fchflags().
2692 setfflags(struct thread *td, struct vnode *vp, u_long flags)
2698 /* We can't support the value matching VNOVAL. */
2699 if (flags == VNOVAL)
2700 return (EOPNOTSUPP);
2703 * Prevent non-root users from setting flags on devices. When
2704 * a device is reused, users can retain ownership of the device
2705 * if they are allowed to set flags and programs assume that
2706 * chown can't fail when done as root.
2708 if (vp->v_type == VCHR || vp->v_type == VBLK) {
2709 error = priv_check(td, PRIV_VFS_CHFLAGS_DEV);
2714 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2717 vattr.va_flags = flags;
2718 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2720 error = mac_vnode_check_setflags(td->td_ucred, vp, vattr.va_flags);
2723 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
2725 vn_finished_write(mp);
2730 * Change flags of a file given a path name.
2732 #ifndef _SYS_SYSPROTO_H_
2733 struct chflags_args {
2739 sys_chflags(struct thread *td, struct chflags_args *uap)
2742 return (kern_chflagsat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2746 #ifndef _SYS_SYSPROTO_H_
2747 struct chflagsat_args {
2755 sys_chflagsat(struct thread *td, struct chflagsat_args *uap)
2758 return (kern_chflagsat(td, uap->fd, uap->path, UIO_USERSPACE,
2759 uap->flags, uap->atflag));
2763 * Same as chflags() but doesn't follow symlinks.
2765 #ifndef _SYS_SYSPROTO_H_
2766 struct lchflags_args {
2772 sys_lchflags(struct thread *td, struct lchflags_args *uap)
2775 return (kern_chflagsat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2776 uap->flags, AT_SYMLINK_NOFOLLOW));
2780 kern_chflagsat(struct thread *td, int fd, const char *path,
2781 enum uio_seg pathseg, u_long flags, int atflag)
2783 struct nameidata nd;
2786 if ((atflag & ~(AT_SYMLINK_NOFOLLOW | AT_RESOLVE_BENEATH |
2787 AT_EMPTY_PATH)) != 0)
2790 AUDIT_ARG_FFLAGS(flags);
2791 NDINIT_ATRIGHTS(&nd, LOOKUP, at2cnpflags(atflag, AT_SYMLINK_NOFOLLOW |
2792 AT_RESOLVE_BENEATH | AT_EMPTY_PATH) | AUDITVNODE1, pathseg, path,
2793 fd, &cap_fchflags_rights, td);
2794 if ((error = namei(&nd)) != 0)
2796 NDFREE_NOTHING(&nd);
2797 error = setfflags(td, nd.ni_vp, flags);
2803 * Change flags of a file given a file descriptor.
2805 #ifndef _SYS_SYSPROTO_H_
2806 struct fchflags_args {
2812 sys_fchflags(struct thread *td, struct fchflags_args *uap)
2817 AUDIT_ARG_FD(uap->fd);
2818 AUDIT_ARG_FFLAGS(uap->flags);
2819 error = getvnode(td, uap->fd, &cap_fchflags_rights,
2824 if (AUDITING_TD(td)) {
2825 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
2826 AUDIT_ARG_VNODE1(fp->f_vnode);
2827 VOP_UNLOCK(fp->f_vnode);
2830 error = setfflags(td, fp->f_vnode, uap->flags);
2836 * Common implementation code for chmod(), lchmod() and fchmod().
2839 setfmode(struct thread *td, struct ucred *cred, struct vnode *vp, int mode)
2845 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2847 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2849 vattr.va_mode = mode & ALLPERMS;
2851 error = mac_vnode_check_setmode(cred, vp, vattr.va_mode);
2854 error = VOP_SETATTR(vp, &vattr, cred);
2856 vn_finished_write(mp);
2861 * Change mode of a file given path name.
2863 #ifndef _SYS_SYSPROTO_H_
2870 sys_chmod(struct thread *td, struct chmod_args *uap)
2873 return (kern_fchmodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2877 #ifndef _SYS_SYSPROTO_H_
2878 struct fchmodat_args {
2886 sys_fchmodat(struct thread *td, struct fchmodat_args *uap)
2889 return (kern_fchmodat(td, uap->fd, uap->path, UIO_USERSPACE,
2890 uap->mode, uap->flag));
2894 * Change mode of a file given path name (don't follow links.)
2896 #ifndef _SYS_SYSPROTO_H_
2897 struct lchmod_args {
2903 sys_lchmod(struct thread *td, struct lchmod_args *uap)
2906 return (kern_fchmodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2907 uap->mode, AT_SYMLINK_NOFOLLOW));
2911 kern_fchmodat(struct thread *td, int fd, const char *path,
2912 enum uio_seg pathseg, mode_t mode, int flag)
2914 struct nameidata nd;
2917 if ((flag & ~(AT_SYMLINK_NOFOLLOW | AT_RESOLVE_BENEATH |
2918 AT_EMPTY_PATH)) != 0)
2921 AUDIT_ARG_MODE(mode);
2922 NDINIT_ATRIGHTS(&nd, LOOKUP, at2cnpflags(flag, AT_SYMLINK_NOFOLLOW |
2923 AT_RESOLVE_BENEATH | AT_EMPTY_PATH) | AUDITVNODE1, pathseg, path,
2924 fd, &cap_fchmod_rights, td);
2925 if ((error = namei(&nd)) != 0)
2927 NDFREE_NOTHING(&nd);
2928 error = setfmode(td, td->td_ucred, nd.ni_vp, mode);
2934 * Change mode of a file given a file descriptor.
2936 #ifndef _SYS_SYSPROTO_H_
2937 struct fchmod_args {
2943 sys_fchmod(struct thread *td, struct fchmod_args *uap)
2948 AUDIT_ARG_FD(uap->fd);
2949 AUDIT_ARG_MODE(uap->mode);
2951 error = fget(td, uap->fd, &cap_fchmod_rights, &fp);
2954 error = fo_chmod(fp, uap->mode, td->td_ucred, td);
2960 * Common implementation for chown(), lchown(), and fchown()
2963 setfown(struct thread *td, struct ucred *cred, struct vnode *vp, uid_t uid,
2970 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2972 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2977 error = mac_vnode_check_setowner(cred, vp, vattr.va_uid,
2981 error = VOP_SETATTR(vp, &vattr, cred);
2983 vn_finished_write(mp);
2988 * Set ownership given a path name.
2990 #ifndef _SYS_SYSPROTO_H_
2998 sys_chown(struct thread *td, struct chown_args *uap)
3001 return (kern_fchownat(td, AT_FDCWD, uap->path, UIO_USERSPACE, uap->uid,
3005 #ifndef _SYS_SYSPROTO_H_
3006 struct fchownat_args {
3015 sys_fchownat(struct thread *td, struct fchownat_args *uap)
3018 return (kern_fchownat(td, uap->fd, uap->path, UIO_USERSPACE, uap->uid,
3019 uap->gid, uap->flag));
3023 kern_fchownat(struct thread *td, int fd, const char *path,
3024 enum uio_seg pathseg, int uid, int gid, int flag)
3026 struct nameidata nd;
3029 if ((flag & ~(AT_SYMLINK_NOFOLLOW | AT_RESOLVE_BENEATH |
3030 AT_EMPTY_PATH)) != 0)
3033 AUDIT_ARG_OWNER(uid, gid);
3034 NDINIT_ATRIGHTS(&nd, LOOKUP, at2cnpflags(flag, AT_SYMLINK_NOFOLLOW |
3035 AT_RESOLVE_BENEATH | AT_EMPTY_PATH) | AUDITVNODE1, pathseg, path,
3036 fd, &cap_fchown_rights, td);
3038 if ((error = namei(&nd)) != 0)
3040 NDFREE_NOTHING(&nd);
3041 error = setfown(td, td->td_ucred, nd.ni_vp, uid, gid);
3047 * Set ownership given a path name, do not cross symlinks.
3049 #ifndef _SYS_SYSPROTO_H_
3050 struct lchown_args {
3057 sys_lchown(struct thread *td, struct lchown_args *uap)
3060 return (kern_fchownat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
3061 uap->uid, uap->gid, AT_SYMLINK_NOFOLLOW));
3065 * Set ownership given a file descriptor.
3067 #ifndef _SYS_SYSPROTO_H_
3068 struct fchown_args {
3075 sys_fchown(struct thread *td, struct fchown_args *uap)
3080 AUDIT_ARG_FD(uap->fd);
3081 AUDIT_ARG_OWNER(uap->uid, uap->gid);
3082 error = fget(td, uap->fd, &cap_fchown_rights, &fp);
3085 error = fo_chown(fp, uap->uid, uap->gid, td->td_ucred, td);
3091 * Common implementation code for utimes(), lutimes(), and futimes().
3094 getutimes(const struct timeval *usrtvp, enum uio_seg tvpseg,
3095 struct timespec *tsp)
3097 struct timeval tv[2];
3098 const struct timeval *tvp;
3101 if (usrtvp == NULL) {
3102 vfs_timestamp(&tsp[0]);
3105 if (tvpseg == UIO_SYSSPACE) {
3108 if ((error = copyin(usrtvp, tv, sizeof(tv))) != 0)
3113 if (tvp[0].tv_usec < 0 || tvp[0].tv_usec >= 1000000 ||
3114 tvp[1].tv_usec < 0 || tvp[1].tv_usec >= 1000000)
3116 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
3117 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
3123 * Common implementation code for futimens(), utimensat().
3125 #define UTIMENS_NULL 0x1
3126 #define UTIMENS_EXIT 0x2
3128 getutimens(const struct timespec *usrtsp, enum uio_seg tspseg,
3129 struct timespec *tsp, int *retflags)
3131 struct timespec tsnow;
3134 vfs_timestamp(&tsnow);
3136 if (usrtsp == NULL) {
3139 *retflags |= UTIMENS_NULL;
3142 if (tspseg == UIO_SYSSPACE) {
3145 } else if ((error = copyin(usrtsp, tsp, sizeof(*tsp) * 2)) != 0)
3147 if (tsp[0].tv_nsec == UTIME_OMIT && tsp[1].tv_nsec == UTIME_OMIT)
3148 *retflags |= UTIMENS_EXIT;
3149 if (tsp[0].tv_nsec == UTIME_NOW && tsp[1].tv_nsec == UTIME_NOW)
3150 *retflags |= UTIMENS_NULL;
3151 if (tsp[0].tv_nsec == UTIME_OMIT)
3152 tsp[0].tv_sec = VNOVAL;
3153 else if (tsp[0].tv_nsec == UTIME_NOW)
3155 else if (tsp[0].tv_nsec < 0 || tsp[0].tv_nsec >= 1000000000L)
3157 if (tsp[1].tv_nsec == UTIME_OMIT)
3158 tsp[1].tv_sec = VNOVAL;
3159 else if (tsp[1].tv_nsec == UTIME_NOW)
3161 else if (tsp[1].tv_nsec < 0 || tsp[1].tv_nsec >= 1000000000L)
3168 * Common implementation code for utimes(), lutimes(), futimes(), futimens(),
3172 setutimes(struct thread *td, struct vnode *vp, const struct timespec *ts,
3173 int numtimes, int nullflag)
3180 setbirthtime = false;
3181 vattr.va_birthtime.tv_sec = VNOVAL;
3182 vattr.va_birthtime.tv_nsec = 0;
3184 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
3186 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3187 if (numtimes < 3 && VOP_GETATTR(vp, &vattr, td->td_ucred) == 0 &&
3188 timespeccmp(&ts[1], &vattr.va_birthtime, < ))
3189 setbirthtime = true;
3191 vattr.va_atime = ts[0];
3192 vattr.va_mtime = ts[1];
3194 vattr.va_birthtime = ts[1];
3196 vattr.va_birthtime = ts[2];
3198 vattr.va_vaflags |= VA_UTIMES_NULL;
3200 error = mac_vnode_check_setutimes(td->td_ucred, vp, vattr.va_atime,
3204 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3206 vn_finished_write(mp);
3211 * Set the access and modification times of a file.
3213 #ifndef _SYS_SYSPROTO_H_
3214 struct utimes_args {
3216 struct timeval *tptr;
3220 sys_utimes(struct thread *td, struct utimes_args *uap)
3223 return (kern_utimesat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
3224 uap->tptr, UIO_USERSPACE));
3227 #ifndef _SYS_SYSPROTO_H_
3228 struct futimesat_args {
3231 const struct timeval * times;
3235 sys_futimesat(struct thread *td, struct futimesat_args *uap)
3238 return (kern_utimesat(td, uap->fd, uap->path, UIO_USERSPACE,
3239 uap->times, UIO_USERSPACE));
3243 kern_utimesat(struct thread *td, int fd, const char *path,
3244 enum uio_seg pathseg, struct timeval *tptr, enum uio_seg tptrseg)
3246 struct nameidata nd;
3247 struct timespec ts[2];
3250 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3252 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, fd,
3253 &cap_futimes_rights, td);
3255 if ((error = namei(&nd)) != 0)
3257 NDFREE_NOTHING(&nd);
3258 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3264 * Set the access and modification times of a file.
3266 #ifndef _SYS_SYSPROTO_H_
3267 struct lutimes_args {
3269 struct timeval *tptr;
3273 sys_lutimes(struct thread *td, struct lutimes_args *uap)
3276 return (kern_lutimes(td, uap->path, UIO_USERSPACE, uap->tptr,
3281 kern_lutimes(struct thread *td, const char *path, enum uio_seg pathseg,
3282 struct timeval *tptr, enum uio_seg tptrseg)
3284 struct timespec ts[2];
3285 struct nameidata nd;
3288 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3290 NDINIT(&nd, LOOKUP, NOFOLLOW | AUDITVNODE1, pathseg, path, td);
3291 if ((error = namei(&nd)) != 0)
3293 NDFREE_NOTHING(&nd);
3294 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3300 * Set the access and modification times of a file.
3302 #ifndef _SYS_SYSPROTO_H_
3303 struct futimes_args {
3305 struct timeval *tptr;
3309 sys_futimes(struct thread *td, struct futimes_args *uap)
3312 return (kern_futimes(td, uap->fd, uap->tptr, UIO_USERSPACE));
3316 kern_futimes(struct thread *td, int fd, struct timeval *tptr,
3317 enum uio_seg tptrseg)
3319 struct timespec ts[2];
3324 error = getutimes(tptr, tptrseg, ts);
3327 error = getvnode(td, fd, &cap_futimes_rights, &fp);
3331 if (AUDITING_TD(td)) {
3332 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
3333 AUDIT_ARG_VNODE1(fp->f_vnode);
3334 VOP_UNLOCK(fp->f_vnode);
3337 error = setutimes(td, fp->f_vnode, ts, 2, tptr == NULL);
3343 sys_futimens(struct thread *td, struct futimens_args *uap)
3346 return (kern_futimens(td, uap->fd, uap->times, UIO_USERSPACE));
3350 kern_futimens(struct thread *td, int fd, struct timespec *tptr,
3351 enum uio_seg tptrseg)
3353 struct timespec ts[2];
3358 error = getutimens(tptr, tptrseg, ts, &flags);
3361 if (flags & UTIMENS_EXIT)
3363 error = getvnode(td, fd, &cap_futimes_rights, &fp);
3367 if (AUDITING_TD(td)) {
3368 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
3369 AUDIT_ARG_VNODE1(fp->f_vnode);
3370 VOP_UNLOCK(fp->f_vnode);
3373 error = setutimes(td, fp->f_vnode, ts, 2, flags & UTIMENS_NULL);
3379 sys_utimensat(struct thread *td, struct utimensat_args *uap)
3382 return (kern_utimensat(td, uap->fd, uap->path, UIO_USERSPACE,
3383 uap->times, UIO_USERSPACE, uap->flag));
3387 kern_utimensat(struct thread *td, int fd, const char *path,
3388 enum uio_seg pathseg, struct timespec *tptr, enum uio_seg tptrseg,
3391 struct nameidata nd;
3392 struct timespec ts[2];
3395 if ((flag & ~(AT_SYMLINK_NOFOLLOW | AT_RESOLVE_BENEATH |
3396 AT_EMPTY_PATH)) != 0)
3399 if ((error = getutimens(tptr, tptrseg, ts, &flags)) != 0)
3401 NDINIT_ATRIGHTS(&nd, LOOKUP, at2cnpflags(flag, AT_SYMLINK_NOFOLLOW |
3402 AT_RESOLVE_BENEATH | AT_EMPTY_PATH) | AUDITVNODE1,
3403 pathseg, path, fd, &cap_futimes_rights, td);
3404 if ((error = namei(&nd)) != 0)
3407 * We are allowed to call namei() regardless of 2xUTIME_OMIT.
3409 * "If both tv_nsec fields are UTIME_OMIT... EACCESS may be detected."
3410 * "Search permission is denied by a component of the path prefix."
3412 NDFREE_NOTHING(&nd);
3413 if ((flags & UTIMENS_EXIT) == 0)
3414 error = setutimes(td, nd.ni_vp, ts, 2, flags & UTIMENS_NULL);
3420 * Truncate a file given its path name.
3422 #ifndef _SYS_SYSPROTO_H_
3423 struct truncate_args {
3430 sys_truncate(struct thread *td, struct truncate_args *uap)
3433 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3437 kern_truncate(struct thread *td, const char *path, enum uio_seg pathseg,
3444 struct nameidata nd;
3451 NDINIT(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, td);
3452 if ((error = namei(&nd)) != 0)
3455 rl_cookie = vn_rangelock_wlock(vp, 0, OFF_MAX);
3456 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0) {
3457 vn_rangelock_unlock(vp, rl_cookie);
3461 NDFREE(&nd, NDF_ONLY_PNBUF);
3462 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3463 if (vp->v_type == VDIR)
3466 else if ((error = mac_vnode_check_write(td->td_ucred, NOCRED, vp))) {
3469 else if ((error = vn_writechk(vp)) == 0 &&
3470 (error = VOP_ACCESS(vp, VWRITE, td->td_ucred, td)) == 0) {
3472 vattr.va_size = length;
3473 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3476 vn_finished_write(mp);
3477 vn_rangelock_unlock(vp, rl_cookie);
3479 if (error == ERELOOKUP)
3484 #if defined(COMPAT_43)
3486 * Truncate a file given its path name.
3488 #ifndef _SYS_SYSPROTO_H_
3489 struct otruncate_args {
3495 otruncate(struct thread *td, struct otruncate_args *uap)
3498 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3500 #endif /* COMPAT_43 */
3502 #if defined(COMPAT_FREEBSD6)
3503 /* Versions with the pad argument */
3505 freebsd6_truncate(struct thread *td, struct freebsd6_truncate_args *uap)
3508 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3512 freebsd6_ftruncate(struct thread *td, struct freebsd6_ftruncate_args *uap)
3515 return (kern_ftruncate(td, uap->fd, uap->length));
3520 kern_fsync(struct thread *td, int fd, bool fullsync)
3528 error = getvnode(td, fd, &cap_fsync_rights, &fp);
3534 /* XXXKIB: compete outstanding aio writes */;
3537 error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
3540 vn_lock(vp, vn_lktype_write(mp, vp) | LK_RETRY);
3541 AUDIT_ARG_VNODE1(vp);
3542 if (vp->v_object != NULL) {
3543 VM_OBJECT_WLOCK(vp->v_object);
3544 vm_object_page_clean(vp->v_object, 0, 0, 0);
3545 VM_OBJECT_WUNLOCK(vp->v_object);
3547 error = fullsync ? VOP_FSYNC(vp, MNT_WAIT, td) : VOP_FDATASYNC(vp, td);
3549 vn_finished_write(mp);
3550 if (error == ERELOOKUP)
3558 * Sync an open file.
3560 #ifndef _SYS_SYSPROTO_H_
3566 sys_fsync(struct thread *td, struct fsync_args *uap)
3569 return (kern_fsync(td, uap->fd, true));
3573 sys_fdatasync(struct thread *td, struct fdatasync_args *uap)
3576 return (kern_fsync(td, uap->fd, false));
3580 * Rename files. Source and destination must either both be directories, or
3581 * both not be directories. If target is a directory, it must be empty.
3583 #ifndef _SYS_SYSPROTO_H_
3584 struct rename_args {
3590 sys_rename(struct thread *td, struct rename_args *uap)
3593 return (kern_renameat(td, AT_FDCWD, uap->from, AT_FDCWD,
3594 uap->to, UIO_USERSPACE));
3597 #ifndef _SYS_SYSPROTO_H_
3598 struct renameat_args {
3606 sys_renameat(struct thread *td, struct renameat_args *uap)
3609 return (kern_renameat(td, uap->oldfd, uap->old, uap->newfd, uap->new,
3615 kern_renameat_mac(struct thread *td, int oldfd, const char *old, int newfd,
3616 const char *new, enum uio_seg pathseg, struct nameidata *fromnd)
3620 NDINIT_ATRIGHTS(fromnd, DELETE, LOCKPARENT | LOCKLEAF | SAVESTART |
3621 AUDITVNODE1, pathseg, old, oldfd, &cap_renameat_source_rights, td);
3622 if ((error = namei(fromnd)) != 0)
3624 error = mac_vnode_check_rename_from(td->td_ucred, fromnd->ni_dvp,
3625 fromnd->ni_vp, &fromnd->ni_cnd);
3626 VOP_UNLOCK(fromnd->ni_dvp);
3627 if (fromnd->ni_dvp != fromnd->ni_vp)
3628 VOP_UNLOCK(fromnd->ni_vp);
3630 NDFREE(fromnd, NDF_ONLY_PNBUF);
3631 vrele(fromnd->ni_dvp);
3632 vrele(fromnd->ni_vp);
3633 if (fromnd->ni_startdir)
3634 vrele(fromnd->ni_startdir);
3641 kern_renameat(struct thread *td, int oldfd, const char *old, int newfd,
3642 const char *new, enum uio_seg pathseg)
3644 struct mount *mp = NULL;
3645 struct vnode *tvp, *fvp, *tdvp;
3646 struct nameidata fromnd, tond;
3647 u_int64_t tondflags;
3653 if (mac_vnode_check_rename_from_enabled()) {
3654 error = kern_renameat_mac(td, oldfd, old, newfd, new, pathseg,
3660 NDINIT_ATRIGHTS(&fromnd, DELETE, WANTPARENT | SAVESTART | AUDITVNODE1,
3661 pathseg, old, oldfd, &cap_renameat_source_rights, td);
3662 if ((error = namei(&fromnd)) != 0)
3668 tondflags = LOCKPARENT | LOCKLEAF | NOCACHE | SAVESTART | AUDITVNODE2;
3669 if (fromnd.ni_vp->v_type == VDIR)
3670 tondflags |= WILLBEDIR;
3671 NDINIT_ATRIGHTS(&tond, RENAME, tondflags, pathseg, new, newfd,
3672 &cap_renameat_target_rights, td);
3673 if ((error = namei(&tond)) != 0) {
3674 /* Translate error code for rename("dir1", "dir2/."). */
3675 if (error == EISDIR && fvp->v_type == VDIR)
3677 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3678 vrele(fromnd.ni_dvp);
3684 error = vn_start_write(fvp, &mp, V_NOWAIT);
3686 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3687 NDFREE(&tond, NDF_ONLY_PNBUF);
3694 vrele(fromnd.ni_dvp);
3696 vrele(tond.ni_startdir);
3697 if (fromnd.ni_startdir != NULL)
3698 vrele(fromnd.ni_startdir);
3699 error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH);
3705 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
3708 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
3713 if (newfd != AT_FDCWD && (tond.ni_resflags & NIRES_ABS) == 0) {
3715 * If the target already exists we require CAP_UNLINKAT
3716 * from 'newfd', when newfd was used for the lookup.
3718 error = cap_check(&tond.ni_filecaps.fc_rights,
3719 &cap_unlinkat_rights);
3730 * If the source is the same as the destination (that is, if they
3731 * are links to the same vnode), then there is nothing to do.
3737 error = mac_vnode_check_rename_to(td->td_ucred, tdvp,
3738 tond.ni_vp, fromnd.ni_dvp == tdvp, &tond.ni_cnd);
3742 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
3743 tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
3744 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3745 NDFREE(&tond, NDF_ONLY_PNBUF);
3747 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3748 NDFREE(&tond, NDF_ONLY_PNBUF);
3755 vrele(fromnd.ni_dvp);
3758 vrele(tond.ni_startdir);
3759 vn_finished_write(mp);
3761 if (fromnd.ni_startdir)
3762 vrele(fromnd.ni_startdir);
3763 if (error == ERESTART)
3765 if (error == ERELOOKUP)
3771 * Make a directory file.
3773 #ifndef _SYS_SYSPROTO_H_
3780 sys_mkdir(struct thread *td, struct mkdir_args *uap)
3783 return (kern_mkdirat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
3787 #ifndef _SYS_SYSPROTO_H_
3788 struct mkdirat_args {
3795 sys_mkdirat(struct thread *td, struct mkdirat_args *uap)
3798 return (kern_mkdirat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode));
3802 kern_mkdirat(struct thread *td, int fd, const char *path, enum uio_seg segflg,
3807 struct nameidata nd;
3810 AUDIT_ARG_MODE(mode);
3814 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
3815 NC_NOMAKEENTRY | NC_KEEPPOSENTRY | FAILIFEXISTS | WILLBEDIR,
3816 segflg, path, fd, &cap_mkdirat_rights, td);
3817 if ((error = namei(&nd)) != 0)
3819 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3820 NDFREE(&nd, NDF_ONLY_PNBUF);
3822 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3827 vattr.va_type = VDIR;
3828 vattr.va_mode = (mode & ACCESSPERMS) &~ td->td_proc->p_pd->pd_cmask;
3830 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
3835 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
3839 NDFREE(&nd, NDF_ONLY_PNBUF);
3840 VOP_VPUT_PAIR(nd.ni_dvp, error == 0 ? &nd.ni_vp : NULL, true);
3841 vn_finished_write(mp);
3842 if (error == ERELOOKUP)
3848 * Remove a directory file.
3850 #ifndef _SYS_SYSPROTO_H_
3856 sys_rmdir(struct thread *td, struct rmdir_args *uap)
3859 return (kern_frmdirat(td, AT_FDCWD, uap->path, FD_NONE, UIO_USERSPACE,
3864 kern_frmdirat(struct thread *td, int dfd, const char *path, int fd,
3865 enum uio_seg pathseg, int flag)
3870 struct nameidata nd;
3871 cap_rights_t rights;
3875 if (fd != FD_NONE) {
3876 error = getvnode(td, fd, cap_rights_init_one(&rights,
3885 NDINIT_ATRIGHTS(&nd, DELETE, LOCKPARENT | LOCKLEAF | AUDITVNODE1 |
3886 at2cnpflags(flag, AT_RESOLVE_BENEATH),
3887 pathseg, path, dfd, &cap_unlinkat_rights, td);
3888 if ((error = namei(&nd)) != 0)
3891 if (vp->v_type != VDIR) {
3896 * No rmdir "." please.
3898 if (nd.ni_dvp == vp) {
3903 * The root of a mounted filesystem cannot be deleted.
3905 if (vp->v_vflag & VV_ROOT) {
3910 if (fp != NULL && fp->f_vnode != vp) {
3911 if (VN_IS_DOOMED(fp->f_vnode))
3919 error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp,
3924 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3925 NDFREE(&nd, NDF_ONLY_PNBUF);
3927 if (nd.ni_dvp == vp)
3931 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3935 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_UNLINK);
3936 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
3937 vn_finished_write(mp);
3939 NDFREE(&nd, NDF_ONLY_PNBUF);
3941 if (nd.ni_dvp == vp)
3945 if (error == ERELOOKUP)
3953 #if defined(COMPAT_43) || defined(COMPAT_FREEBSD11)
3955 freebsd11_kern_getdirentries(struct thread *td, int fd, char *ubuf, u_int count,
3956 long *basep, void (*func)(struct freebsd11_dirent *))
3958 struct freebsd11_dirent dstdp;
3959 struct dirent *dp, *edp;
3962 ssize_t resid, ucount;
3965 /* XXX arbitrary sanity limit on `count'. */
3966 count = min(count, 64 * 1024);
3968 dirbuf = malloc(count, M_TEMP, M_WAITOK);
3970 error = kern_getdirentries(td, fd, dirbuf, count, &base, &resid,
3978 for (dp = (struct dirent *)dirbuf,
3979 edp = (struct dirent *)&dirbuf[count - resid];
3980 ucount < count && dp < edp; ) {
3981 if (dp->d_reclen == 0)
3983 MPASS(dp->d_reclen >= _GENERIC_DIRLEN(0));
3984 if (dp->d_namlen >= sizeof(dstdp.d_name))
3986 dstdp.d_type = dp->d_type;
3987 dstdp.d_namlen = dp->d_namlen;
3988 dstdp.d_fileno = dp->d_fileno; /* truncate */
3989 if (dstdp.d_fileno != dp->d_fileno) {
3990 switch (ino64_trunc_error) {
3998 dstdp.d_fileno = UINT32_MAX;
4002 dstdp.d_reclen = sizeof(dstdp) - sizeof(dstdp.d_name) +
4003 ((dp->d_namlen + 1 + 3) &~ 3);
4004 bcopy(dp->d_name, dstdp.d_name, dstdp.d_namlen);
4005 bzero(dstdp.d_name + dstdp.d_namlen,
4006 dstdp.d_reclen - offsetof(struct freebsd11_dirent, d_name) -
4008 MPASS(dstdp.d_reclen <= dp->d_reclen);
4009 MPASS(ucount + dstdp.d_reclen <= count);
4012 error = copyout(&dstdp, ubuf + ucount, dstdp.d_reclen);
4015 dp = (struct dirent *)((char *)dp + dp->d_reclen);
4016 ucount += dstdp.d_reclen;
4020 free(dirbuf, M_TEMP);
4022 td->td_retval[0] = ucount;
4029 ogetdirentries_cvt(struct freebsd11_dirent *dp)
4031 #if (BYTE_ORDER == LITTLE_ENDIAN)
4033 * The expected low byte of dp->d_namlen is our dp->d_type.
4034 * The high MBZ byte of dp->d_namlen is our dp->d_namlen.
4036 dp->d_type = dp->d_namlen;
4040 * The dp->d_type is the high byte of the expected dp->d_namlen,
4041 * so must be zero'ed.
4048 * Read a block of directory entries in a filesystem independent format.
4050 #ifndef _SYS_SYSPROTO_H_
4051 struct ogetdirentries_args {
4059 ogetdirentries(struct thread *td, struct ogetdirentries_args *uap)
4064 error = kern_ogetdirentries(td, uap, &loff);
4066 error = copyout(&loff, uap->basep, sizeof(long));
4071 kern_ogetdirentries(struct thread *td, struct ogetdirentries_args *uap,
4077 /* XXX arbitrary sanity limit on `count'. */
4078 if (uap->count > 64 * 1024)
4081 error = freebsd11_kern_getdirentries(td, uap->fd, uap->buf, uap->count,
4082 &base, ogetdirentries_cvt);
4084 if (error == 0 && uap->basep != NULL)
4085 error = copyout(&base, uap->basep, sizeof(long));
4089 #endif /* COMPAT_43 */
4091 #if defined(COMPAT_FREEBSD11)
4092 #ifndef _SYS_SYSPROTO_H_
4093 struct freebsd11_getdirentries_args {
4101 freebsd11_getdirentries(struct thread *td,
4102 struct freebsd11_getdirentries_args *uap)
4107 error = freebsd11_kern_getdirentries(td, uap->fd, uap->buf, uap->count,
4110 if (error == 0 && uap->basep != NULL)
4111 error = copyout(&base, uap->basep, sizeof(long));
4116 freebsd11_getdents(struct thread *td, struct freebsd11_getdents_args *uap)
4118 struct freebsd11_getdirentries_args ap;
4122 ap.count = uap->count;
4124 return (freebsd11_getdirentries(td, &ap));
4126 #endif /* COMPAT_FREEBSD11 */
4129 * Read a block of directory entries in a filesystem independent format.
4132 sys_getdirentries(struct thread *td, struct getdirentries_args *uap)
4137 error = kern_getdirentries(td, uap->fd, uap->buf, uap->count, &base,
4138 NULL, UIO_USERSPACE);
4141 if (uap->basep != NULL)
4142 error = copyout(&base, uap->basep, sizeof(off_t));
4147 kern_getdirentries(struct thread *td, int fd, char *buf, size_t count,
4148 off_t *basep, ssize_t *residp, enum uio_seg bufseg)
4159 if (count > IOSIZE_MAX)
4161 auio.uio_resid = count;
4162 error = getvnode(td, fd, &cap_read_rights, &fp);
4165 if ((fp->f_flag & FREAD) == 0) {
4170 foffset = foffset_lock(fp, 0);
4172 if (vp->v_type != VDIR) {
4176 if (__predict_false((vp->v_vflag & VV_UNLINKED) != 0)) {
4180 aiov.iov_base = buf;
4181 aiov.iov_len = count;
4182 auio.uio_iov = &aiov;
4183 auio.uio_iovcnt = 1;
4184 auio.uio_rw = UIO_READ;
4185 auio.uio_segflg = bufseg;
4187 vn_lock(vp, LK_SHARED | LK_RETRY);
4188 AUDIT_ARG_VNODE1(vp);
4189 loff = auio.uio_offset = foffset;
4191 error = mac_vnode_check_readdir(td->td_ucred, vp);
4194 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL,
4196 foffset = auio.uio_offset;
4201 if (count == auio.uio_resid &&
4202 (vp->v_vflag & VV_ROOT) &&
4203 (vp->v_mount->mnt_flag & MNT_UNION)) {
4204 struct vnode *tvp = vp;
4206 vp = vp->v_mount->mnt_vnodecovered;
4216 *residp = auio.uio_resid;
4217 td->td_retval[0] = count - auio.uio_resid;
4219 foffset_unlock(fp, foffset, 0);
4225 * Set the mode mask for creation of filesystem nodes.
4227 #ifndef _SYS_SYSPROTO_H_
4233 sys_umask(struct thread *td, struct umask_args *uap)
4235 struct pwddesc *pdp;
4237 pdp = td->td_proc->p_pd;
4239 td->td_retval[0] = pdp->pd_cmask;
4240 pdp->pd_cmask = uap->newmask & ALLPERMS;
4241 PWDDESC_XUNLOCK(pdp);
4246 * Void all references to file by ripping underlying filesystem away from
4249 #ifndef _SYS_SYSPROTO_H_
4250 struct revoke_args {
4255 sys_revoke(struct thread *td, struct revoke_args *uap)
4259 struct nameidata nd;
4262 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
4264 if ((error = namei(&nd)) != 0)
4267 NDFREE_NOTHING(&nd);
4268 if (vp->v_type != VCHR || vp->v_rdev == NULL) {
4273 error = mac_vnode_check_revoke(td->td_ucred, vp);
4277 error = VOP_GETATTR(vp, &vattr, td->td_ucred);
4280 if (td->td_ucred->cr_uid != vattr.va_uid) {
4281 error = priv_check(td, PRIV_VFS_ADMIN);
4285 if (devfs_usecount(vp) > 0)
4286 VOP_REVOKE(vp, REVOKEALL);
4293 * This variant of getvnode() allows O_PATH files. Caller should
4294 * ensure that returned file and vnode are only used for compatible
4298 getvnode_path(struct thread *td, int fd, cap_rights_t *rightsp,
4304 error = fget_unlocked(td->td_proc->p_fd, fd, rightsp, &fp);
4309 * The file could be not of the vnode type, or it may be not
4310 * yet fully initialized, in which case the f_vnode pointer
4311 * may be set, but f_ops is still badfileops. E.g.,
4312 * devfs_open() transiently create such situation to
4313 * facilitate csw d_fdopen().
4315 * Dupfdopen() handling in kern_openat() installs the
4316 * half-baked file into the process descriptor table, allowing
4317 * other thread to dereference it. Guard against the race by
4320 if (fp->f_vnode == NULL || fp->f_ops == &badfileops) {
4331 * Convert a user file descriptor to a kernel file entry and check
4332 * that, if it is a capability, the correct rights are present.
4333 * A reference on the file entry is held upon returning.
4336 getvnode(struct thread *td, int fd, cap_rights_t *rightsp, struct file **fpp)
4340 error = getvnode_path(td, fd, rightsp, fpp);
4343 * Filter out O_PATH file descriptors, most getvnode() callers
4344 * do not call fo_ methods.
4346 if (error == 0 && (*fpp)->f_ops == &path_fileops) {
4356 * Get an (NFS) file handle.
4358 #ifndef _SYS_SYSPROTO_H_
4359 struct lgetfh_args {
4365 sys_lgetfh(struct thread *td, struct lgetfh_args *uap)
4368 return (kern_getfhat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->fname,
4369 UIO_USERSPACE, uap->fhp, UIO_USERSPACE));
4372 #ifndef _SYS_SYSPROTO_H_
4379 sys_getfh(struct thread *td, struct getfh_args *uap)
4382 return (kern_getfhat(td, 0, AT_FDCWD, uap->fname, UIO_USERSPACE,
4383 uap->fhp, UIO_USERSPACE));
4387 * syscall for the rpc.lockd to use to translate an open descriptor into
4388 * a NFS file handle.
4390 * warning: do not remove the priv_check() call or this becomes one giant
4393 #ifndef _SYS_SYSPROTO_H_
4394 struct getfhat_args {
4402 sys_getfhat(struct thread *td, struct getfhat_args *uap)
4405 return (kern_getfhat(td, uap->flags, uap->fd, uap->path, UIO_USERSPACE,
4406 uap->fhp, UIO_USERSPACE));
4410 kern_getfhat(struct thread *td, int flags, int fd, const char *path,
4411 enum uio_seg pathseg, fhandle_t *fhp, enum uio_seg fhseg)
4413 struct nameidata nd;
4418 if ((flags & ~(AT_SYMLINK_NOFOLLOW | AT_RESOLVE_BENEATH)) != 0)
4420 error = priv_check(td, PRIV_VFS_GETFH);
4423 NDINIT_AT(&nd, LOOKUP, at2cnpflags(flags, AT_SYMLINK_NOFOLLOW |
4424 AT_RESOLVE_BENEATH) | LOCKLEAF | AUDITVNODE1, pathseg, path,
4429 NDFREE_NOTHING(&nd);
4431 bzero(&fh, sizeof(fh));
4432 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
4433 error = VOP_VPTOFH(vp, &fh.fh_fid);
4436 if (fhseg == UIO_USERSPACE)
4437 error = copyout(&fh, fhp, sizeof (fh));
4439 memcpy(fhp, &fh, sizeof(fh));
4444 #ifndef _SYS_SYSPROTO_H_
4445 struct fhlink_args {
4451 sys_fhlink(struct thread *td, struct fhlink_args *uap)
4454 return (kern_fhlinkat(td, AT_FDCWD, uap->to, UIO_USERSPACE, uap->fhp));
4457 #ifndef _SYS_SYSPROTO_H_
4458 struct fhlinkat_args {
4465 sys_fhlinkat(struct thread *td, struct fhlinkat_args *uap)
4468 return (kern_fhlinkat(td, uap->tofd, uap->to, UIO_USERSPACE, uap->fhp));
4472 kern_fhlinkat(struct thread *td, int fd, const char *path,
4473 enum uio_seg pathseg, fhandle_t *fhp)
4480 error = priv_check(td, PRIV_VFS_GETFH);
4483 error = copyin(fhp, &fh, sizeof(fh));
4488 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4490 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_SHARED, &vp);
4495 error = kern_linkat_vp(td, vp, fd, path, pathseg);
4496 } while (error == EAGAIN || error == ERELOOKUP);
4500 #ifndef _SYS_SYSPROTO_H_
4501 struct fhreadlink_args {
4508 sys_fhreadlink(struct thread *td, struct fhreadlink_args *uap)
4515 error = priv_check(td, PRIV_VFS_GETFH);
4518 if (uap->bufsize > IOSIZE_MAX)
4520 error = copyin(uap->fhp, &fh, sizeof(fh));
4523 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4525 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_SHARED, &vp);
4529 error = kern_readlink_vp(vp, uap->buf, UIO_USERSPACE, uap->bufsize, td);
4535 * syscall for the rpc.lockd to use to translate a NFS file handle into an
4538 * warning: do not remove the priv_check() call or this becomes one giant
4541 #ifndef _SYS_SYSPROTO_H_
4542 struct fhopen_args {
4543 const struct fhandle *u_fhp;
4548 sys_fhopen(struct thread *td, struct fhopen_args *uap)
4550 return (kern_fhopen(td, uap->u_fhp, uap->flags));
4554 kern_fhopen(struct thread *td, const struct fhandle *u_fhp, int flags)
4563 error = priv_check(td, PRIV_VFS_FHOPEN);
4567 fmode = FFLAGS(flags);
4568 /* why not allow a non-read/write open for our lockd? */
4569 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
4571 error = copyin(u_fhp, &fhp, sizeof(fhp));
4574 /* find the mount point */
4575 mp = vfs_busyfs(&fhp.fh_fsid);
4578 /* now give me my vnode, it gets returned to me locked */
4579 error = VFS_FHTOVP(mp, &fhp.fh_fid, LK_EXCLUSIVE, &vp);
4584 error = falloc_noinstall(td, &fp);
4590 * An extra reference on `fp' has been held for us by
4591 * falloc_noinstall().
4597 error = vn_open_vnode(vp, fmode, td->td_ucred, td, fp);
4599 KASSERT(fp->f_ops == &badfileops,
4600 ("VOP_OPEN in fhopen() set f_ops"));
4601 KASSERT(td->td_dupfd < 0,
4602 ("fhopen() encountered fdopen()"));
4611 finit_vnode(fp, fmode, NULL, &vnops);
4613 if ((fmode & O_TRUNC) != 0) {
4614 error = fo_truncate(fp, 0, td->td_ucred, td);
4619 error = finstall(td, fp, &indx, fmode, NULL);
4622 td->td_retval[0] = indx;
4627 * Stat an (NFS) file handle.
4629 #ifndef _SYS_SYSPROTO_H_
4630 struct fhstat_args {
4631 struct fhandle *u_fhp;
4636 sys_fhstat(struct thread *td, struct fhstat_args *uap)
4642 error = copyin(uap->u_fhp, &fh, sizeof(fh));
4645 error = kern_fhstat(td, fh, &sb);
4647 error = copyout(&sb, uap->sb, sizeof(sb));
4652 kern_fhstat(struct thread *td, struct fhandle fh, struct stat *sb)
4658 error = priv_check(td, PRIV_VFS_FHSTAT);
4661 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4663 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_EXCLUSIVE, &vp);
4667 error = VOP_STAT(vp, sb, td->td_ucred, NOCRED, td);
4673 * Implement fstatfs() for (NFS) file handles.
4675 #ifndef _SYS_SYSPROTO_H_
4676 struct fhstatfs_args {
4677 struct fhandle *u_fhp;
4682 sys_fhstatfs(struct thread *td, struct fhstatfs_args *uap)
4688 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4691 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
4692 error = kern_fhstatfs(td, fh, sfp);
4694 error = copyout(sfp, uap->buf, sizeof(*sfp));
4695 free(sfp, M_STATFS);
4700 kern_fhstatfs(struct thread *td, fhandle_t fh, struct statfs *buf)
4706 error = priv_check(td, PRIV_VFS_FHSTATFS);
4709 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4711 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_EXCLUSIVE, &vp);
4717 error = prison_canseemount(td->td_ucred, mp);
4721 error = mac_mount_check_stat(td->td_ucred, mp);
4725 error = VFS_STATFS(mp, buf);
4732 * Unlike madvise(2), we do not make a best effort to remember every
4733 * possible caching hint. Instead, we remember the last setting with
4734 * the exception that we will allow POSIX_FADV_NORMAL to adjust the
4735 * region of any current setting.
4738 kern_posix_fadvise(struct thread *td, int fd, off_t offset, off_t len,
4741 struct fadvise_info *fa, *new;
4747 if (offset < 0 || len < 0 || offset > OFF_MAX - len)
4749 AUDIT_ARG_VALUE(advice);
4751 case POSIX_FADV_SEQUENTIAL:
4752 case POSIX_FADV_RANDOM:
4753 case POSIX_FADV_NOREUSE:
4754 new = malloc(sizeof(*fa), M_FADVISE, M_WAITOK);
4756 case POSIX_FADV_NORMAL:
4757 case POSIX_FADV_WILLNEED:
4758 case POSIX_FADV_DONTNEED:
4764 /* XXX: CAP_POSIX_FADVISE? */
4766 error = fget(td, fd, &cap_no_rights, &fp);
4769 AUDIT_ARG_FILE(td->td_proc, fp);
4770 if ((fp->f_ops->fo_flags & DFLAG_SEEKABLE) == 0) {
4774 if (fp->f_type != DTYPE_VNODE) {
4779 if (vp->v_type != VREG) {
4786 end = offset + len - 1;
4788 case POSIX_FADV_SEQUENTIAL:
4789 case POSIX_FADV_RANDOM:
4790 case POSIX_FADV_NOREUSE:
4792 * Try to merge any existing non-standard region with
4793 * this new region if possible, otherwise create a new
4794 * non-standard region for this request.
4796 mtx_pool_lock(mtxpool_sleep, fp);
4798 if (fa != NULL && fa->fa_advice == advice &&
4799 ((fa->fa_start <= end && fa->fa_end >= offset) ||
4800 (end != OFF_MAX && fa->fa_start == end + 1) ||
4801 (fa->fa_end != OFF_MAX && fa->fa_end + 1 == offset))) {
4802 if (offset < fa->fa_start)
4803 fa->fa_start = offset;
4804 if (end > fa->fa_end)
4807 new->fa_advice = advice;
4808 new->fa_start = offset;
4813 mtx_pool_unlock(mtxpool_sleep, fp);
4815 case POSIX_FADV_NORMAL:
4817 * If a the "normal" region overlaps with an existing
4818 * non-standard region, trim or remove the
4819 * non-standard region.
4821 mtx_pool_lock(mtxpool_sleep, fp);
4824 if (offset <= fa->fa_start && end >= fa->fa_end) {
4826 fp->f_advice = NULL;
4827 } else if (offset <= fa->fa_start &&
4828 end >= fa->fa_start)
4829 fa->fa_start = end + 1;
4830 else if (offset <= fa->fa_end && end >= fa->fa_end)
4831 fa->fa_end = offset - 1;
4832 else if (offset >= fa->fa_start && end <= fa->fa_end) {
4834 * If the "normal" region is a middle
4835 * portion of the existing
4836 * non-standard region, just remove
4837 * the whole thing rather than picking
4838 * one side or the other to
4842 fp->f_advice = NULL;
4845 mtx_pool_unlock(mtxpool_sleep, fp);
4847 case POSIX_FADV_WILLNEED:
4848 case POSIX_FADV_DONTNEED:
4849 error = VOP_ADVISE(vp, offset, end, advice);
4855 free(new, M_FADVISE);
4860 sys_posix_fadvise(struct thread *td, struct posix_fadvise_args *uap)
4864 error = kern_posix_fadvise(td, uap->fd, uap->offset, uap->len,
4866 return (kern_posix_error(td, error));
4870 kern_copy_file_range(struct thread *td, int infd, off_t *inoffp, int outfd,
4871 off_t *outoffp, size_t len, unsigned int flags)
4873 struct file *infp, *outfp;
4874 struct vnode *invp, *outvp;
4877 void *rl_rcookie, *rl_wcookie;
4878 off_t savinoff, savoutoff;
4880 infp = outfp = NULL;
4881 rl_rcookie = rl_wcookie = NULL;
4890 if (len > SSIZE_MAX)
4892 * Although the len argument is size_t, the return argument
4893 * is ssize_t (which is signed). Therefore a size that won't
4894 * fit in ssize_t can't be returned.
4898 /* Get the file structures for the file descriptors. */
4899 error = fget_read(td, infd, &cap_read_rights, &infp);
4902 if (infp->f_ops == &badfileops) {
4906 if (infp->f_vnode == NULL) {
4910 error = fget_write(td, outfd, &cap_write_rights, &outfp);
4913 if (outfp->f_ops == &badfileops) {
4917 if (outfp->f_vnode == NULL) {
4922 /* Set the offset pointers to the correct place. */
4924 inoffp = &infp->f_offset;
4925 if (outoffp == NULL)
4926 outoffp = &outfp->f_offset;
4928 savoutoff = *outoffp;
4930 invp = infp->f_vnode;
4931 outvp = outfp->f_vnode;
4932 /* Sanity check the f_flag bits. */
4933 if ((outfp->f_flag & (FWRITE | FAPPEND)) != FWRITE ||
4934 (infp->f_flag & FREAD) == 0) {
4939 /* If len == 0, just return 0. */
4944 * If infp and outfp refer to the same file, the byte ranges cannot
4947 if (invp == outvp && ((savinoff <= savoutoff && savinoff + len >
4948 savoutoff) || (savinoff > savoutoff && savoutoff + len >
4954 /* Range lock the byte ranges for both invp and outvp. */
4956 rl_wcookie = vn_rangelock_wlock(outvp, *outoffp, *outoffp +
4958 rl_rcookie = vn_rangelock_tryrlock(invp, *inoffp, *inoffp +
4960 if (rl_rcookie != NULL)
4962 vn_rangelock_unlock(outvp, rl_wcookie);
4963 rl_rcookie = vn_rangelock_rlock(invp, *inoffp, *inoffp + len);
4964 vn_rangelock_unlock(invp, rl_rcookie);
4968 error = vn_copy_file_range(invp, inoffp, outvp, outoffp, &retlen,
4969 flags, infp->f_cred, outfp->f_cred, td);
4971 if (rl_rcookie != NULL)
4972 vn_rangelock_unlock(invp, rl_rcookie);
4973 if (rl_wcookie != NULL)
4974 vn_rangelock_unlock(outvp, rl_wcookie);
4975 if (savinoff != -1 && (error == EINTR || error == ERESTART)) {
4977 *outoffp = savoutoff;
4983 td->td_retval[0] = retlen;
4988 sys_copy_file_range(struct thread *td, struct copy_file_range_args *uap)
4990 off_t inoff, outoff, *inoffp, *outoffp;
4993 inoffp = outoffp = NULL;
4994 if (uap->inoffp != NULL) {
4995 error = copyin(uap->inoffp, &inoff, sizeof(off_t));
5000 if (uap->outoffp != NULL) {
5001 error = copyin(uap->outoffp, &outoff, sizeof(off_t));
5006 error = kern_copy_file_range(td, uap->infd, inoffp, uap->outfd,
5007 outoffp, uap->len, uap->flags);
5008 if (error == 0 && uap->inoffp != NULL)
5009 error = copyout(inoffp, uap->inoffp, sizeof(off_t));
5010 if (error == 0 && uap->outoffp != NULL)
5011 error = copyout(outoffp, uap->outoffp, sizeof(off_t));