2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
6 * (c) UNIX System Laboratories, Inc.
7 * All or some portions of this file are derived from material licensed
8 * to the University of California by American Telephone and Telegraph
9 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10 * the permission of UNIX System Laboratories, Inc.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * @(#)vfs_syscalls.c 8.13 (Berkeley) 4/15/94
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
42 #include "opt_capsicum.h"
43 #include "opt_ktrace.h"
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #ifdef COMPAT_FREEBSD11
48 #include <sys/abi_compat.h>
52 #include <sys/capsicum.h>
54 #include <sys/sysent.h>
55 #include <sys/malloc.h>
56 #include <sys/mount.h>
57 #include <sys/mutex.h>
58 #include <sys/sysproto.h>
59 #include <sys/namei.h>
60 #include <sys/filedesc.h>
61 #include <sys/kernel.h>
62 #include <sys/fcntl.h>
64 #include <sys/filio.h>
65 #include <sys/limits.h>
66 #include <sys/linker.h>
67 #include <sys/rwlock.h>
71 #include <sys/unistd.h>
72 #include <sys/vnode.h>
75 #include <sys/dirent.h>
77 #include <sys/syscallsubr.h>
78 #include <sys/sysctl.h>
80 #include <sys/ktrace.h>
83 #include <machine/stdarg.h>
85 #include <security/audit/audit.h>
86 #include <security/mac/mac_framework.h>
89 #include <vm/vm_object.h>
90 #include <vm/vm_page.h>
93 #include <fs/devfs/devfs.h>
95 MALLOC_DEFINE(M_FADVISE, "fadvise", "posix_fadvise(2) information");
97 static int kern_chflagsat(struct thread *td, int fd, const char *path,
98 enum uio_seg pathseg, u_long flags, int atflag);
99 static int setfflags(struct thread *td, struct vnode *, u_long);
100 static int getutimes(const struct timeval *, enum uio_seg, struct timespec *);
101 static int getutimens(const struct timespec *, enum uio_seg,
102 struct timespec *, int *);
103 static int setutimes(struct thread *td, struct vnode *,
104 const struct timespec *, int, int);
105 static int vn_access(struct vnode *vp, int user_flags, struct ucred *cred,
107 static int kern_fhlinkat(struct thread *td, int fd, const char *path,
108 enum uio_seg pathseg, fhandle_t *fhp);
109 static int kern_readlink_vp(struct vnode *vp, char *buf, enum uio_seg bufseg,
110 size_t count, struct thread *td);
111 static int kern_linkat_vp(struct thread *td, struct vnode *vp, int fd,
112 const char *path, enum uio_seg segflag);
115 at2cnpflags(u_int at_flags, u_int mask)
119 MPASS((at_flags & (AT_SYMLINK_FOLLOW | AT_SYMLINK_NOFOLLOW)) !=
120 (AT_SYMLINK_FOLLOW | AT_SYMLINK_NOFOLLOW));
124 if ((at_flags & AT_RESOLVE_BENEATH) != 0)
126 if ((at_flags & AT_SYMLINK_FOLLOW) != 0)
128 /* NOFOLLOW is pseudo flag */
129 if ((mask & AT_SYMLINK_NOFOLLOW) != 0) {
130 res |= (at_flags & AT_SYMLINK_NOFOLLOW) != 0 ? NOFOLLOW :
133 if ((mask & AT_EMPTY_PATH) != 0 && (at_flags & AT_EMPTY_PATH) != 0)
139 kern_sync(struct thread *td)
141 struct mount *mp, *nmp;
144 mtx_lock(&mountlist_mtx);
145 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
146 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK)) {
147 nmp = TAILQ_NEXT(mp, mnt_list);
150 if ((mp->mnt_flag & MNT_RDONLY) == 0 &&
151 vn_start_write(NULL, &mp, V_NOWAIT) == 0) {
152 save = curthread_pflags_set(TDP_SYNCIO);
153 vfs_periodic(mp, MNT_NOWAIT);
154 VFS_SYNC(mp, MNT_NOWAIT);
155 curthread_pflags_restore(save);
156 vn_finished_write(mp);
158 mtx_lock(&mountlist_mtx);
159 nmp = TAILQ_NEXT(mp, mnt_list);
162 mtx_unlock(&mountlist_mtx);
167 * Sync each mounted filesystem.
169 #ifndef _SYS_SYSPROTO_H_
176 sys_sync(struct thread *td, struct sync_args *uap)
179 return (kern_sync(td));
183 * Change filesystem quotas.
185 #ifndef _SYS_SYSPROTO_H_
186 struct quotactl_args {
194 sys_quotactl(struct thread *td, struct quotactl_args *uap)
201 AUDIT_ARG_CMD(uap->cmd);
202 AUDIT_ARG_UID(uap->uid);
203 if (!prison_allow(td->td_ucred, PR_ALLOW_QUOTAS))
205 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
207 if ((error = namei(&nd)) != 0)
209 NDFREE(&nd, NDF_ONLY_PNBUF);
210 mp = nd.ni_vp->v_mount;
213 error = vfs_busy(mp, 0);
219 error = VFS_QUOTACTL(mp, uap->cmd, uap->uid, uap->arg, &mp_busy);
222 * Since quota on/off operations typically need to open quota
223 * files, the implementation may need to unbusy the mount point
224 * before calling into namei. Otherwise, unmount might be
225 * started between two vfs_busy() invocations (first is ours,
226 * second is from mount point cross-walk code in lookup()),
229 * Avoid unbusying mp if the implementation indicates it has
239 * Used by statfs conversion routines to scale the block size up if
240 * necessary so that all of the block counts are <= 'max_size'. Note
241 * that 'max_size' should be a bitmask, i.e. 2^n - 1 for some non-zero
245 statfs_scale_blocks(struct statfs *sf, long max_size)
250 KASSERT(powerof2(max_size + 1), ("%s: invalid max_size", __func__));
253 * Attempt to scale the block counts to give a more accurate
254 * overview to userland of the ratio of free space to used
255 * space. To do this, find the largest block count and compute
256 * a divisor that lets it fit into a signed integer <= max_size.
258 if (sf->f_bavail < 0)
259 count = -sf->f_bavail;
261 count = sf->f_bavail;
262 count = MAX(sf->f_blocks, MAX(sf->f_bfree, count));
263 if (count <= max_size)
266 count >>= flsl(max_size);
273 sf->f_bsize <<= shift;
274 sf->f_blocks >>= shift;
275 sf->f_bfree >>= shift;
276 sf->f_bavail >>= shift;
280 kern_do_statfs(struct thread *td, struct mount *mp, struct statfs *buf)
286 error = vfs_busy(mp, 0);
291 error = mac_mount_check_stat(td->td_ucred, mp);
295 error = VFS_STATFS(mp, buf);
298 if (priv_check_cred_vfs_generation(td->td_ucred)) {
299 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
300 prison_enforce_statfs(td->td_ucred, mp, buf);
308 * Get filesystem statistics.
310 #ifndef _SYS_SYSPROTO_H_
317 sys_statfs(struct thread *td, struct statfs_args *uap)
322 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
323 error = kern_statfs(td, uap->path, UIO_USERSPACE, sfp);
325 error = copyout(sfp, uap->buf, sizeof(struct statfs));
331 kern_statfs(struct thread *td, const char *path, enum uio_seg pathseg,
338 NDINIT(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path);
342 mp = vfs_ref_from_vp(nd.ni_vp);
345 return (kern_do_statfs(td, mp, buf));
349 * Get filesystem statistics.
351 #ifndef _SYS_SYSPROTO_H_
352 struct fstatfs_args {
358 sys_fstatfs(struct thread *td, struct fstatfs_args *uap)
363 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
364 error = kern_fstatfs(td, uap->fd, sfp);
366 error = copyout(sfp, uap->buf, sizeof(struct statfs));
372 kern_fstatfs(struct thread *td, int fd, struct statfs *buf)
380 error = getvnode_path(td, fd, &cap_fstatfs_rights, &fp);
385 if (AUDITING_TD(td)) {
386 vn_lock(vp, LK_SHARED | LK_RETRY);
387 AUDIT_ARG_VNODE1(vp);
391 mp = vfs_ref_from_vp(vp);
393 return (kern_do_statfs(td, mp, buf));
397 * Get statistics on all filesystems.
399 #ifndef _SYS_SYSPROTO_H_
400 struct getfsstat_args {
407 sys_getfsstat(struct thread *td, struct getfsstat_args *uap)
412 if (uap->bufsize < 0 || uap->bufsize > SIZE_MAX)
414 error = kern_getfsstat(td, &uap->buf, uap->bufsize, &count,
415 UIO_USERSPACE, uap->mode);
417 td->td_retval[0] = count;
422 * If (bufsize > 0 && bufseg == UIO_SYSSPACE)
423 * The caller is responsible for freeing memory which will be allocated
427 kern_getfsstat(struct thread *td, struct statfs **buf, size_t bufsize,
428 size_t *countp, enum uio_seg bufseg, int mode)
430 struct mount *mp, *nmp;
431 struct statfs *sfsp, *sp, *sptmp, *tofree;
432 size_t count, maxcount;
440 if (bufseg == UIO_SYSSPACE)
445 maxcount = bufsize / sizeof(struct statfs);
449 } else if (bufseg == UIO_USERSPACE) {
452 } else /* if (bufseg == UIO_SYSSPACE) */ {
454 mtx_lock(&mountlist_mtx);
455 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
458 mtx_unlock(&mountlist_mtx);
459 if (maxcount > count)
461 tofree = sfsp = *buf = malloc(maxcount * sizeof(struct statfs),
468 * If there is no target buffer they only want the count.
470 * This could be TAILQ_FOREACH but it is open-coded to match the original
474 mtx_lock(&mountlist_mtx);
475 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
476 if (prison_canseemount(td->td_ucred, mp) != 0) {
477 nmp = TAILQ_NEXT(mp, mnt_list);
481 if (mac_mount_check_stat(td->td_ucred, mp) != 0) {
482 nmp = TAILQ_NEXT(mp, mnt_list);
487 nmp = TAILQ_NEXT(mp, mnt_list);
489 mtx_unlock(&mountlist_mtx);
495 * They want the entire thing.
497 * Short-circuit the corner case of no room for anything, avoids
504 mtx_lock(&mountlist_mtx);
505 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
506 if (prison_canseemount(td->td_ucred, mp) != 0) {
507 nmp = TAILQ_NEXT(mp, mnt_list);
511 if (mac_mount_check_stat(td->td_ucred, mp) != 0) {
512 nmp = TAILQ_NEXT(mp, mnt_list);
516 if (mode == MNT_WAIT) {
517 if (vfs_busy(mp, MBF_MNTLSTLOCK) != 0) {
519 * If vfs_busy() failed, and MBF_NOWAIT
520 * wasn't passed, then the mp is gone.
521 * Furthermore, because of MBF_MNTLSTLOCK,
522 * the mountlist_mtx was dropped. We have
523 * no other choice than to start over.
525 mtx_unlock(&mountlist_mtx);
526 free(tofree, M_STATFS);
530 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK) != 0) {
531 nmp = TAILQ_NEXT(mp, mnt_list);
537 * If MNT_NOWAIT is specified, do not refresh
540 if (mode != MNT_NOWAIT) {
541 error = VFS_STATFS(mp, sp);
543 mtx_lock(&mountlist_mtx);
544 nmp = TAILQ_NEXT(mp, mnt_list);
549 if (priv_check_cred_vfs_generation(td->td_ucred)) {
550 sptmp = malloc(sizeof(struct statfs), M_STATFS,
553 sptmp->f_fsid.val[0] = sptmp->f_fsid.val[1] = 0;
554 prison_enforce_statfs(td->td_ucred, mp, sptmp);
558 if (bufseg == UIO_SYSSPACE) {
559 bcopy(sp, sfsp, sizeof(*sp));
560 free(sptmp, M_STATFS);
561 } else /* if (bufseg == UIO_USERSPACE) */ {
562 error = copyout(sp, sfsp, sizeof(*sp));
563 free(sptmp, M_STATFS);
572 if (count == maxcount) {
577 mtx_lock(&mountlist_mtx);
578 nmp = TAILQ_NEXT(mp, mnt_list);
581 mtx_unlock(&mountlist_mtx);
587 #ifdef COMPAT_FREEBSD4
589 * Get old format filesystem statistics.
591 static void freebsd4_cvtstatfs(struct statfs *, struct ostatfs *);
593 #ifndef _SYS_SYSPROTO_H_
594 struct freebsd4_statfs_args {
600 freebsd4_statfs(struct thread *td, struct freebsd4_statfs_args *uap)
606 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
607 error = kern_statfs(td, uap->path, UIO_USERSPACE, sfp);
609 freebsd4_cvtstatfs(sfp, &osb);
610 error = copyout(&osb, uap->buf, sizeof(osb));
617 * Get filesystem statistics.
619 #ifndef _SYS_SYSPROTO_H_
620 struct freebsd4_fstatfs_args {
626 freebsd4_fstatfs(struct thread *td, struct freebsd4_fstatfs_args *uap)
632 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
633 error = kern_fstatfs(td, uap->fd, sfp);
635 freebsd4_cvtstatfs(sfp, &osb);
636 error = copyout(&osb, uap->buf, sizeof(osb));
643 * Get statistics on all filesystems.
645 #ifndef _SYS_SYSPROTO_H_
646 struct freebsd4_getfsstat_args {
653 freebsd4_getfsstat(struct thread *td, struct freebsd4_getfsstat_args *uap)
655 struct statfs *buf, *sp;
660 if (uap->bufsize < 0)
662 count = uap->bufsize / sizeof(struct ostatfs);
663 if (count > SIZE_MAX / sizeof(struct statfs))
665 size = count * sizeof(struct statfs);
666 error = kern_getfsstat(td, &buf, size, &count, UIO_SYSSPACE,
669 td->td_retval[0] = count;
672 while (count != 0 && error == 0) {
673 freebsd4_cvtstatfs(sp, &osb);
674 error = copyout(&osb, uap->buf, sizeof(osb));
685 * Implement fstatfs() for (NFS) file handles.
687 #ifndef _SYS_SYSPROTO_H_
688 struct freebsd4_fhstatfs_args {
689 struct fhandle *u_fhp;
694 freebsd4_fhstatfs(struct thread *td, struct freebsd4_fhstatfs_args *uap)
701 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
704 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
705 error = kern_fhstatfs(td, fh, sfp);
707 freebsd4_cvtstatfs(sfp, &osb);
708 error = copyout(&osb, uap->buf, sizeof(osb));
715 * Convert a new format statfs structure to an old format statfs structure.
718 freebsd4_cvtstatfs(struct statfs *nsp, struct ostatfs *osp)
721 statfs_scale_blocks(nsp, LONG_MAX);
722 bzero(osp, sizeof(*osp));
723 osp->f_bsize = nsp->f_bsize;
724 osp->f_iosize = MIN(nsp->f_iosize, LONG_MAX);
725 osp->f_blocks = nsp->f_blocks;
726 osp->f_bfree = nsp->f_bfree;
727 osp->f_bavail = nsp->f_bavail;
728 osp->f_files = MIN(nsp->f_files, LONG_MAX);
729 osp->f_ffree = MIN(nsp->f_ffree, LONG_MAX);
730 osp->f_owner = nsp->f_owner;
731 osp->f_type = nsp->f_type;
732 osp->f_flags = nsp->f_flags;
733 osp->f_syncwrites = MIN(nsp->f_syncwrites, LONG_MAX);
734 osp->f_asyncwrites = MIN(nsp->f_asyncwrites, LONG_MAX);
735 osp->f_syncreads = MIN(nsp->f_syncreads, LONG_MAX);
736 osp->f_asyncreads = MIN(nsp->f_asyncreads, LONG_MAX);
737 strlcpy(osp->f_fstypename, nsp->f_fstypename,
738 MIN(MFSNAMELEN, OMFSNAMELEN));
739 strlcpy(osp->f_mntonname, nsp->f_mntonname,
740 MIN(MNAMELEN, OMNAMELEN));
741 strlcpy(osp->f_mntfromname, nsp->f_mntfromname,
742 MIN(MNAMELEN, OMNAMELEN));
743 osp->f_fsid = nsp->f_fsid;
745 #endif /* COMPAT_FREEBSD4 */
747 #if defined(COMPAT_FREEBSD11)
749 * Get old format filesystem statistics.
751 static void freebsd11_cvtstatfs(struct statfs *, struct freebsd11_statfs *);
754 freebsd11_statfs(struct thread *td, struct freebsd11_statfs_args *uap)
756 struct freebsd11_statfs osb;
760 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
761 error = kern_statfs(td, uap->path, UIO_USERSPACE, sfp);
763 freebsd11_cvtstatfs(sfp, &osb);
764 error = copyout(&osb, uap->buf, sizeof(osb));
771 * Get filesystem statistics.
774 freebsd11_fstatfs(struct thread *td, struct freebsd11_fstatfs_args *uap)
776 struct freebsd11_statfs osb;
780 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
781 error = kern_fstatfs(td, uap->fd, sfp);
783 freebsd11_cvtstatfs(sfp, &osb);
784 error = copyout(&osb, uap->buf, sizeof(osb));
791 * Get statistics on all filesystems.
794 freebsd11_getfsstat(struct thread *td, struct freebsd11_getfsstat_args *uap)
796 return (kern_freebsd11_getfsstat(td, uap->buf, uap->bufsize, uap->mode));
800 kern_freebsd11_getfsstat(struct thread *td, struct freebsd11_statfs * ubuf,
801 long bufsize, int mode)
803 struct freebsd11_statfs osb;
804 struct statfs *buf, *sp;
811 count = bufsize / sizeof(struct ostatfs);
812 size = count * sizeof(struct statfs);
813 error = kern_getfsstat(td, &buf, size, &count, UIO_SYSSPACE, mode);
815 td->td_retval[0] = count;
818 while (count > 0 && error == 0) {
819 freebsd11_cvtstatfs(sp, &osb);
820 error = copyout(&osb, ubuf, sizeof(osb));
831 * Implement fstatfs() for (NFS) file handles.
834 freebsd11_fhstatfs(struct thread *td, struct freebsd11_fhstatfs_args *uap)
836 struct freebsd11_statfs osb;
841 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
844 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
845 error = kern_fhstatfs(td, fh, sfp);
847 freebsd11_cvtstatfs(sfp, &osb);
848 error = copyout(&osb, uap->buf, sizeof(osb));
855 * Convert a new format statfs structure to an old format statfs structure.
858 freebsd11_cvtstatfs(struct statfs *nsp, struct freebsd11_statfs *osp)
861 bzero(osp, sizeof(*osp));
862 osp->f_version = FREEBSD11_STATFS_VERSION;
863 osp->f_type = nsp->f_type;
864 osp->f_flags = nsp->f_flags;
865 osp->f_bsize = nsp->f_bsize;
866 osp->f_iosize = nsp->f_iosize;
867 osp->f_blocks = nsp->f_blocks;
868 osp->f_bfree = nsp->f_bfree;
869 osp->f_bavail = nsp->f_bavail;
870 osp->f_files = nsp->f_files;
871 osp->f_ffree = nsp->f_ffree;
872 osp->f_syncwrites = nsp->f_syncwrites;
873 osp->f_asyncwrites = nsp->f_asyncwrites;
874 osp->f_syncreads = nsp->f_syncreads;
875 osp->f_asyncreads = nsp->f_asyncreads;
876 osp->f_namemax = nsp->f_namemax;
877 osp->f_owner = nsp->f_owner;
878 osp->f_fsid = nsp->f_fsid;
879 strlcpy(osp->f_fstypename, nsp->f_fstypename,
880 MIN(MFSNAMELEN, sizeof(osp->f_fstypename)));
881 strlcpy(osp->f_mntonname, nsp->f_mntonname,
882 MIN(MNAMELEN, sizeof(osp->f_mntonname)));
883 strlcpy(osp->f_mntfromname, nsp->f_mntfromname,
884 MIN(MNAMELEN, sizeof(osp->f_mntfromname)));
886 #endif /* COMPAT_FREEBSD11 */
889 * Change current working directory to a given file descriptor.
891 #ifndef _SYS_SYSPROTO_H_
897 sys_fchdir(struct thread *td, struct fchdir_args *uap)
899 struct vnode *vp, *tdp;
904 AUDIT_ARG_FD(uap->fd);
905 error = getvnode_path(td, uap->fd, &cap_fchdir_rights,
912 vn_lock(vp, LK_SHARED | LK_RETRY);
913 AUDIT_ARG_VNODE1(vp);
914 error = change_dir(vp, td);
915 while (!error && (mp = vp->v_mountedhere) != NULL) {
918 error = VFS_ROOT(mp, LK_SHARED, &tdp);
935 * Change current working directory (``.'').
937 #ifndef _SYS_SYSPROTO_H_
943 sys_chdir(struct thread *td, struct chdir_args *uap)
946 return (kern_chdir(td, uap->path, UIO_USERSPACE));
950 kern_chdir(struct thread *td, const char *path, enum uio_seg pathseg)
955 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
957 if ((error = namei(&nd)) != 0)
959 if ((error = change_dir(nd.ni_vp, td)) != 0) {
964 VOP_UNLOCK(nd.ni_vp);
966 pwd_chdir(td, nd.ni_vp);
970 static int unprivileged_chroot = 0;
971 SYSCTL_INT(_security_bsd, OID_AUTO, unprivileged_chroot, CTLFLAG_RW,
972 &unprivileged_chroot, 0,
973 "Unprivileged processes can use chroot(2)");
975 * Change notion of root (``/'') directory.
977 #ifndef _SYS_SYSPROTO_H_
983 sys_chroot(struct thread *td, struct chroot_args *uap)
989 error = priv_check(td, PRIV_VFS_CHROOT);
993 if (unprivileged_chroot == 0 ||
994 (p->p_flag2 & P2_NO_NEW_PRIVS) == 0) {
1000 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
1001 UIO_USERSPACE, uap->path);
1005 error = change_dir(nd.ni_vp, td);
1009 error = mac_vnode_check_chroot(td->td_ucred, nd.ni_vp);
1013 VOP_UNLOCK(nd.ni_vp);
1014 error = pwd_chroot(td, nd.ni_vp);
1016 NDFREE_NOTHING(&nd);
1021 NDFREE_NOTHING(&nd);
1026 * Common routine for chroot and chdir. Callers must provide a locked vnode
1030 change_dir(struct vnode *vp, struct thread *td)
1036 ASSERT_VOP_LOCKED(vp, "change_dir(): vp not locked");
1037 if (vp->v_type != VDIR)
1040 error = mac_vnode_check_chdir(td->td_ucred, vp);
1044 return (VOP_ACCESS(vp, VEXEC, td->td_ucred, td));
1047 static __inline void
1048 flags_to_rights(int flags, cap_rights_t *rightsp)
1050 if (flags & O_EXEC) {
1051 cap_rights_set_one(rightsp, CAP_FEXECVE);
1055 switch ((flags & O_ACCMODE)) {
1057 cap_rights_set_one(rightsp, CAP_READ);
1060 cap_rights_set_one(rightsp, CAP_READ);
1063 cap_rights_set_one(rightsp, CAP_WRITE);
1064 if (!(flags & (O_APPEND | O_TRUNC)))
1065 cap_rights_set_one(rightsp, CAP_SEEK);
1070 if (flags & O_CREAT)
1071 cap_rights_set_one(rightsp, CAP_CREATE);
1073 if (flags & O_TRUNC)
1074 cap_rights_set_one(rightsp, CAP_FTRUNCATE);
1076 if (flags & (O_SYNC | O_FSYNC))
1077 cap_rights_set_one(rightsp, CAP_FSYNC);
1079 if (flags & (O_EXLOCK | O_SHLOCK))
1080 cap_rights_set_one(rightsp, CAP_FLOCK);
1084 * Check permissions, allocate an open file structure, and call the device
1085 * open routine if any.
1087 #ifndef _SYS_SYSPROTO_H_
1095 sys_open(struct thread *td, struct open_args *uap)
1098 return (kern_openat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1099 uap->flags, uap->mode));
1102 #ifndef _SYS_SYSPROTO_H_
1103 struct openat_args {
1111 sys_openat(struct thread *td, struct openat_args *uap)
1114 AUDIT_ARG_FD(uap->fd);
1115 return (kern_openat(td, uap->fd, uap->path, UIO_USERSPACE, uap->flag,
1120 kern_openat(struct thread *td, int fd, const char *path, enum uio_seg pathseg,
1121 int flags, int mode)
1123 struct proc *p = td->td_proc;
1124 struct filedesc *fdp;
1125 struct pwddesc *pdp;
1128 struct nameidata nd;
1129 cap_rights_t rights;
1130 int cmode, error, indx;
1136 AUDIT_ARG_FFLAGS(flags);
1137 AUDIT_ARG_MODE(mode);
1138 cap_rights_init_one(&rights, CAP_LOOKUP);
1139 flags_to_rights(flags, &rights);
1142 * Only one of the O_EXEC, O_RDONLY, O_WRONLY and O_RDWR flags
1143 * may be specified. On the other hand, for O_PATH any mode
1144 * except O_EXEC is ignored.
1146 if ((flags & O_PATH) != 0) {
1147 flags &= ~(O_CREAT | O_ACCMODE);
1148 } else if ((flags & O_EXEC) != 0) {
1149 if (flags & O_ACCMODE)
1151 } else if ((flags & O_ACCMODE) == O_ACCMODE) {
1154 flags = FFLAGS(flags);
1158 * Allocate a file structure. The descriptor to reference it
1159 * is allocated and used by finstall_refed() below.
1161 error = falloc_noinstall(td, &fp);
1164 /* Set the flags early so the finit in devfs can pick them up. */
1165 fp->f_flag = flags & FMASK;
1166 cmode = ((mode & ~pdp->pd_cmask) & ALLPERMS) & ~S_ISTXT;
1167 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, fd,
1169 td->td_dupfd = -1; /* XXX check for fdopen */
1170 error = vn_open(&nd, &flags, cmode, fp);
1173 * If the vn_open replaced the method vector, something
1174 * wonderous happened deep below and we just pass it up
1175 * pretending we know what we do.
1177 if (error == ENXIO && fp->f_ops != &badfileops) {
1178 MPASS((flags & O_PATH) == 0);
1183 * Handle special fdopen() case. bleh.
1185 * Don't do this for relative (capability) lookups; we don't
1186 * understand exactly what would happen, and we don't think
1187 * that it ever should.
1189 if ((nd.ni_resflags & NIRES_STRICTREL) == 0 &&
1190 (error == ENODEV || error == ENXIO) &&
1191 td->td_dupfd >= 0) {
1192 error = dupfdopen(td, fdp, td->td_dupfd, flags, error,
1201 NDFREE(&nd, NDF_ONLY_PNBUF);
1205 * Store the vnode, for any f_type. Typically, the vnode use
1206 * count is decremented by direct call to vn_closefile() for
1207 * files that switched type in the cdevsw fdopen() method.
1212 * If the file wasn't claimed by devfs bind it to the normal
1213 * vnode operations here.
1215 if (fp->f_ops == &badfileops) {
1216 KASSERT(vp->v_type != VFIFO || (flags & O_PATH) != 0,
1217 ("Unexpected fifo fp %p vp %p", fp, vp));
1218 if ((flags & O_PATH) != 0) {
1219 finit(fp, (flags & FMASK) | (fp->f_flag & FKQALLOWED),
1220 DTYPE_VNODE, NULL, &path_fileops);
1224 finit_vnode(fp, flags, NULL, &vnops);
1229 if (flags & O_TRUNC) {
1230 error = fo_truncate(fp, 0, td->td_ucred, td);
1236 * If we haven't already installed the FD (for dupfdopen), do so now.
1239 struct filecaps *fcaps;
1242 if ((nd.ni_resflags & NIRES_STRICTREL) != 0)
1243 fcaps = &nd.ni_filecaps;
1247 error = finstall_refed(td, fp, &indx, flags, fcaps);
1248 /* On success finstall_refed() consumes fcaps. */
1250 filecaps_free(&nd.ni_filecaps);
1254 filecaps_free(&nd.ni_filecaps);
1255 falloc_abort(td, fp);
1258 td->td_retval[0] = indx;
1261 KASSERT(indx == -1, ("indx=%d, should be -1", indx));
1262 falloc_abort(td, fp);
1270 #ifndef _SYS_SYSPROTO_H_
1271 struct ocreat_args {
1277 ocreat(struct thread *td, struct ocreat_args *uap)
1280 return (kern_openat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1281 O_WRONLY | O_CREAT | O_TRUNC, uap->mode));
1283 #endif /* COMPAT_43 */
1286 * Create a special file.
1288 #ifndef _SYS_SYSPROTO_H_
1289 struct mknodat_args {
1297 sys_mknodat(struct thread *td, struct mknodat_args *uap)
1300 return (kern_mknodat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode,
1304 #if defined(COMPAT_FREEBSD11)
1306 freebsd11_mknod(struct thread *td,
1307 struct freebsd11_mknod_args *uap)
1310 return (kern_mknodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1311 uap->mode, uap->dev));
1315 freebsd11_mknodat(struct thread *td,
1316 struct freebsd11_mknodat_args *uap)
1319 return (kern_mknodat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode,
1322 #endif /* COMPAT_FREEBSD11 */
1325 kern_mknodat(struct thread *td, int fd, const char *path, enum uio_seg pathseg,
1326 int mode, dev_t dev)
1331 struct nameidata nd;
1332 int error, whiteout = 0;
1334 AUDIT_ARG_MODE(mode);
1336 switch (mode & S_IFMT) {
1339 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
1340 if (error == 0 && dev == VNOVAL)
1344 error = priv_check(td, PRIV_VFS_MKNOD_WHT);
1348 return (kern_mkfifoat(td, fd, path, pathseg, mode));
1359 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
1360 NOCACHE, pathseg, path, fd, &cap_mknodat_rights);
1361 if ((error = namei(&nd)) != 0)
1365 NDFREE(&nd, NDF_ONLY_PNBUF);
1366 if (vp == nd.ni_dvp)
1374 vattr.va_mode = (mode & ALLPERMS) &
1375 ~td->td_proc->p_pd->pd_cmask;
1376 vattr.va_rdev = dev;
1379 switch (mode & S_IFMT) {
1381 vattr.va_type = VCHR;
1384 vattr.va_type = VBLK;
1390 panic("kern_mknod: invalid mode");
1393 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1394 NDFREE(&nd, NDF_ONLY_PNBUF);
1396 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1401 if (error == 0 && !whiteout)
1402 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp,
1403 &nd.ni_cnd, &vattr);
1407 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, CREATE);
1409 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp,
1410 &nd.ni_cnd, &vattr);
1413 VOP_VPUT_PAIR(nd.ni_dvp, error == 0 && !whiteout ? &nd.ni_vp : NULL,
1415 vn_finished_write(mp);
1416 NDFREE(&nd, NDF_ONLY_PNBUF);
1417 if (error == ERELOOKUP)
1423 * Create a named pipe.
1425 #ifndef _SYS_SYSPROTO_H_
1426 struct mkfifo_args {
1432 sys_mkfifo(struct thread *td, struct mkfifo_args *uap)
1435 return (kern_mkfifoat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1439 #ifndef _SYS_SYSPROTO_H_
1440 struct mkfifoat_args {
1447 sys_mkfifoat(struct thread *td, struct mkfifoat_args *uap)
1450 return (kern_mkfifoat(td, uap->fd, uap->path, UIO_USERSPACE,
1455 kern_mkfifoat(struct thread *td, int fd, const char *path,
1456 enum uio_seg pathseg, int mode)
1460 struct nameidata nd;
1463 AUDIT_ARG_MODE(mode);
1467 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
1468 NOCACHE, pathseg, path, fd, &cap_mkfifoat_rights);
1469 if ((error = namei(&nd)) != 0)
1471 if (nd.ni_vp != NULL) {
1472 NDFREE(&nd, NDF_ONLY_PNBUF);
1473 if (nd.ni_vp == nd.ni_dvp)
1480 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1481 NDFREE(&nd, NDF_ONLY_PNBUF);
1483 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1488 vattr.va_type = VFIFO;
1489 vattr.va_mode = (mode & ALLPERMS) & ~td->td_proc->p_pd->pd_cmask;
1491 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1496 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
1500 VOP_VPUT_PAIR(nd.ni_dvp, error == 0 ? &nd.ni_vp : NULL, true);
1501 vn_finished_write(mp);
1502 NDFREE(&nd, NDF_ONLY_PNBUF);
1503 if (error == ERELOOKUP)
1509 * Make a hard file link.
1511 #ifndef _SYS_SYSPROTO_H_
1518 sys_link(struct thread *td, struct link_args *uap)
1521 return (kern_linkat(td, AT_FDCWD, AT_FDCWD, uap->path, uap->link,
1522 UIO_USERSPACE, AT_SYMLINK_FOLLOW));
1525 #ifndef _SYS_SYSPROTO_H_
1526 struct linkat_args {
1535 sys_linkat(struct thread *td, struct linkat_args *uap)
1538 return (kern_linkat(td, uap->fd1, uap->fd2, uap->path1, uap->path2,
1539 UIO_USERSPACE, uap->flag));
1542 int hardlink_check_uid = 0;
1543 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
1544 &hardlink_check_uid, 0,
1545 "Unprivileged processes cannot create hard links to files owned by other "
1547 static int hardlink_check_gid = 0;
1548 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
1549 &hardlink_check_gid, 0,
1550 "Unprivileged processes cannot create hard links to files owned by other "
1554 can_hardlink(struct vnode *vp, struct ucred *cred)
1559 if (!hardlink_check_uid && !hardlink_check_gid)
1562 error = VOP_GETATTR(vp, &va, cred);
1566 if (hardlink_check_uid && cred->cr_uid != va.va_uid) {
1567 error = priv_check_cred(cred, PRIV_VFS_LINK);
1572 if (hardlink_check_gid && !groupmember(va.va_gid, cred)) {
1573 error = priv_check_cred(cred, PRIV_VFS_LINK);
1582 kern_linkat(struct thread *td, int fd1, int fd2, const char *path1,
1583 const char *path2, enum uio_seg segflag, int flag)
1585 struct nameidata nd;
1588 if ((flag & ~(AT_SYMLINK_FOLLOW | AT_RESOLVE_BENEATH |
1589 AT_EMPTY_PATH)) != 0)
1595 NDINIT_ATRIGHTS(&nd, LOOKUP, AUDITVNODE1 | at2cnpflags(flag,
1596 AT_SYMLINK_FOLLOW | AT_RESOLVE_BENEATH | AT_EMPTY_PATH),
1597 segflag, path1, fd1, &cap_linkat_source_rights);
1598 if ((error = namei(&nd)) != 0)
1600 NDFREE(&nd, NDF_ONLY_PNBUF);
1601 if ((nd.ni_resflags & NIRES_EMPTYPATH) != 0) {
1602 error = priv_check(td, PRIV_VFS_FHOPEN);
1608 error = kern_linkat_vp(td, nd.ni_vp, fd2, path2, segflag);
1609 } while (error == EAGAIN || error == ERELOOKUP);
1614 kern_linkat_vp(struct thread *td, struct vnode *vp, int fd, const char *path,
1615 enum uio_seg segflag)
1617 struct nameidata nd;
1621 if (vp->v_type == VDIR) {
1623 return (EPERM); /* POSIX */
1625 NDINIT_ATRIGHTS(&nd, CREATE,
1626 LOCKPARENT | SAVENAME | AUDITVNODE2 | NOCACHE, segflag, path, fd,
1627 &cap_linkat_target_rights);
1628 if ((error = namei(&nd)) == 0) {
1629 if (nd.ni_vp != NULL) {
1630 NDFREE(&nd, NDF_ONLY_PNBUF);
1631 if (nd.ni_dvp == nd.ni_vp)
1638 } else if (nd.ni_dvp->v_mount != vp->v_mount) {
1640 * Cross-device link. No need to recheck
1641 * vp->v_type, since it cannot change, except
1644 NDFREE(&nd, NDF_ONLY_PNBUF);
1648 } else if ((error = vn_lock(vp, LK_EXCLUSIVE)) == 0) {
1649 error = can_hardlink(vp, td->td_ucred);
1652 error = mac_vnode_check_link(td->td_ucred,
1653 nd.ni_dvp, vp, &nd.ni_cnd);
1658 NDFREE(&nd, NDF_ONLY_PNBUF);
1661 error = vn_start_write(vp, &mp, V_NOWAIT);
1665 NDFREE(&nd, NDF_ONLY_PNBUF);
1666 error = vn_start_write(NULL, &mp,
1672 error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
1673 VOP_VPUT_PAIR(nd.ni_dvp, &vp, true);
1674 vn_finished_write(mp);
1675 NDFREE(&nd, NDF_ONLY_PNBUF);
1679 NDFREE(&nd, NDF_ONLY_PNBUF);
1690 * Make a symbolic link.
1692 #ifndef _SYS_SYSPROTO_H_
1693 struct symlink_args {
1699 sys_symlink(struct thread *td, struct symlink_args *uap)
1702 return (kern_symlinkat(td, uap->path, AT_FDCWD, uap->link,
1706 #ifndef _SYS_SYSPROTO_H_
1707 struct symlinkat_args {
1714 sys_symlinkat(struct thread *td, struct symlinkat_args *uap)
1717 return (kern_symlinkat(td, uap->path1, uap->fd, uap->path2,
1722 kern_symlinkat(struct thread *td, const char *path1, int fd, const char *path2,
1723 enum uio_seg segflg)
1727 const char *syspath;
1729 struct nameidata nd;
1732 if (segflg == UIO_SYSSPACE) {
1735 tmppath = uma_zalloc(namei_zone, M_WAITOK);
1736 if ((error = copyinstr(path1, tmppath, MAXPATHLEN, NULL)) != 0)
1740 AUDIT_ARG_TEXT(syspath);
1744 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
1745 NOCACHE, segflg, path2, fd, &cap_symlinkat_rights);
1746 if ((error = namei(&nd)) != 0)
1749 NDFREE(&nd, NDF_ONLY_PNBUF);
1750 if (nd.ni_vp == nd.ni_dvp)
1759 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1760 NDFREE(&nd, NDF_ONLY_PNBUF);
1762 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1767 vattr.va_mode = ACCESSPERMS &~ td->td_proc->p_pd->pd_cmask;
1769 vattr.va_type = VLNK;
1770 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1775 error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr, syspath);
1779 VOP_VPUT_PAIR(nd.ni_dvp, error == 0 ? &nd.ni_vp : NULL, true);
1780 vn_finished_write(mp);
1781 NDFREE(&nd, NDF_ONLY_PNBUF);
1782 if (error == ERELOOKUP)
1785 if (segflg != UIO_SYSSPACE)
1786 uma_zfree(namei_zone, tmppath);
1791 * Delete a whiteout from the filesystem.
1793 #ifndef _SYS_SYSPROTO_H_
1794 struct undelete_args {
1799 sys_undelete(struct thread *td, struct undelete_args *uap)
1802 struct nameidata nd;
1808 NDINIT(&nd, DELETE, LOCKPARENT | DOWHITEOUT | AUDITVNODE1,
1809 UIO_USERSPACE, uap->path);
1814 if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & ISWHITEOUT)) {
1815 NDFREE(&nd, NDF_ONLY_PNBUF);
1816 if (nd.ni_vp == nd.ni_dvp)
1824 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1825 NDFREE(&nd, NDF_ONLY_PNBUF);
1827 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1831 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, DELETE);
1832 NDFREE(&nd, NDF_ONLY_PNBUF);
1834 vn_finished_write(mp);
1835 if (error == ERELOOKUP)
1841 * Delete a name from the filesystem.
1843 #ifndef _SYS_SYSPROTO_H_
1844 struct unlink_args {
1849 sys_unlink(struct thread *td, struct unlink_args *uap)
1852 return (kern_funlinkat(td, AT_FDCWD, uap->path, FD_NONE, UIO_USERSPACE,
1857 kern_funlinkat_ex(struct thread *td, int dfd, const char *path, int fd,
1858 int flag, enum uio_seg pathseg, ino_t oldinum)
1861 if ((flag & ~(AT_REMOVEDIR | AT_RESOLVE_BENEATH)) != 0)
1864 if ((flag & AT_REMOVEDIR) != 0)
1865 return (kern_frmdirat(td, dfd, path, fd, UIO_USERSPACE, 0));
1867 return (kern_funlinkat(td, dfd, path, fd, UIO_USERSPACE, 0, 0));
1870 #ifndef _SYS_SYSPROTO_H_
1871 struct unlinkat_args {
1878 sys_unlinkat(struct thread *td, struct unlinkat_args *uap)
1881 return (kern_funlinkat_ex(td, uap->fd, uap->path, FD_NONE, uap->flag,
1885 #ifndef _SYS_SYSPROTO_H_
1886 struct funlinkat_args {
1894 sys_funlinkat(struct thread *td, struct funlinkat_args *uap)
1897 return (kern_funlinkat_ex(td, uap->dfd, uap->path, uap->fd, uap->flag,
1902 kern_funlinkat(struct thread *td, int dfd, const char *path, int fd,
1903 enum uio_seg pathseg, int flag, ino_t oldinum)
1908 struct nameidata nd;
1913 if (fd != FD_NONE) {
1914 error = getvnode_path(td, fd, &cap_no_rights, &fp);
1922 NDINIT_ATRIGHTS(&nd, DELETE, LOCKPARENT | LOCKLEAF | AUDITVNODE1 |
1923 at2cnpflags(flag, AT_RESOLVE_BENEATH),
1924 pathseg, path, dfd, &cap_unlinkat_rights);
1925 if ((error = namei(&nd)) != 0) {
1926 if (error == EINVAL)
1931 if (vp->v_type == VDIR && oldinum == 0) {
1932 error = EPERM; /* POSIX */
1933 } else if (oldinum != 0 &&
1934 ((error = VOP_STAT(vp, &sb, td->td_ucred, NOCRED)) == 0) &&
1935 sb.st_ino != oldinum) {
1936 error = EIDRM; /* Identifier removed */
1937 } else if (fp != NULL && fp->f_vnode != vp) {
1938 if (VN_IS_DOOMED(fp->f_vnode))
1944 * The root of a mounted filesystem cannot be deleted.
1946 * XXX: can this only be a VDIR case?
1948 if (vp->v_vflag & VV_ROOT)
1952 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1953 NDFREE(&nd, NDF_ONLY_PNBUF);
1955 if (vp == nd.ni_dvp)
1959 if ((error = vn_start_write(NULL, &mp,
1960 V_XSLEEP | PCATCH)) != 0) {
1966 error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp,
1971 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_UNLINK);
1972 error = VOP_REMOVE(nd.ni_dvp, vp, &nd.ni_cnd);
1976 vn_finished_write(mp);
1978 NDFREE(&nd, NDF_ONLY_PNBUF);
1980 if (vp == nd.ni_dvp)
1984 if (error == ERELOOKUP)
1993 * Reposition read/write file offset.
1995 #ifndef _SYS_SYSPROTO_H_
2004 sys_lseek(struct thread *td, struct lseek_args *uap)
2007 return (kern_lseek(td, uap->fd, uap->offset, uap->whence));
2011 kern_lseek(struct thread *td, int fd, off_t offset, int whence)
2017 error = fget(td, fd, &cap_seek_rights, &fp);
2020 error = (fp->f_ops->fo_flags & DFLAG_SEEKABLE) != 0 ?
2021 fo_seek(fp, offset, whence, td) : ESPIPE;
2026 #if defined(COMPAT_43)
2028 * Reposition read/write file offset.
2030 #ifndef _SYS_SYSPROTO_H_
2031 struct olseek_args {
2038 olseek(struct thread *td, struct olseek_args *uap)
2041 return (kern_lseek(td, uap->fd, uap->offset, uap->whence));
2043 #endif /* COMPAT_43 */
2045 #if defined(COMPAT_FREEBSD6)
2046 /* Version with the 'pad' argument */
2048 freebsd6_lseek(struct thread *td, struct freebsd6_lseek_args *uap)
2051 return (kern_lseek(td, uap->fd, uap->offset, uap->whence));
2056 * Check access permissions using passed credentials.
2059 vn_access(struct vnode *vp, int user_flags, struct ucred *cred,
2065 /* Flags == 0 means only check for existence. */
2066 if (user_flags == 0)
2070 if (user_flags & R_OK)
2072 if (user_flags & W_OK)
2074 if (user_flags & X_OK)
2077 error = mac_vnode_check_access(cred, vp, accmode);
2081 if ((accmode & VWRITE) == 0 || (error = vn_writechk(vp)) == 0)
2082 error = VOP_ACCESS(vp, accmode, cred, td);
2087 * Check access permissions using "real" credentials.
2089 #ifndef _SYS_SYSPROTO_H_
2090 struct access_args {
2096 sys_access(struct thread *td, struct access_args *uap)
2099 return (kern_accessat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2103 #ifndef _SYS_SYSPROTO_H_
2104 struct faccessat_args {
2112 sys_faccessat(struct thread *td, struct faccessat_args *uap)
2115 return (kern_accessat(td, uap->fd, uap->path, UIO_USERSPACE, uap->flag,
2120 kern_accessat(struct thread *td, int fd, const char *path,
2121 enum uio_seg pathseg, int flag, int amode)
2123 struct ucred *cred, *usecred;
2125 struct nameidata nd;
2128 if ((flag & ~(AT_EACCESS | AT_RESOLVE_BENEATH | AT_EMPTY_PATH)) != 0)
2130 if (amode != F_OK && (amode & ~(R_OK | W_OK | X_OK)) != 0)
2134 * Create and modify a temporary credential instead of one that
2135 * is potentially shared (if we need one).
2137 cred = td->td_ucred;
2138 if ((flag & AT_EACCESS) == 0 &&
2139 ((cred->cr_uid != cred->cr_ruid ||
2140 cred->cr_rgid != cred->cr_groups[0]))) {
2141 usecred = crdup(cred);
2142 usecred->cr_uid = cred->cr_ruid;
2143 usecred->cr_groups[0] = cred->cr_rgid;
2144 td->td_ucred = usecred;
2147 AUDIT_ARG_VALUE(amode);
2148 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF |
2149 AUDITVNODE1 | at2cnpflags(flag, AT_RESOLVE_BENEATH |
2150 AT_EMPTY_PATH), pathseg, path, fd, &cap_fstat_rights);
2151 if ((error = namei(&nd)) != 0)
2155 error = vn_access(vp, amode, usecred, td);
2156 NDFREE_NOTHING(&nd);
2159 if (usecred != cred) {
2160 td->td_ucred = cred;
2167 * Check access permissions using "effective" credentials.
2169 #ifndef _SYS_SYSPROTO_H_
2170 struct eaccess_args {
2176 sys_eaccess(struct thread *td, struct eaccess_args *uap)
2179 return (kern_accessat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2180 AT_EACCESS, uap->amode));
2183 #if defined(COMPAT_43)
2185 * Get file status; this version follows links.
2187 #ifndef _SYS_SYSPROTO_H_
2194 ostat(struct thread *td, struct ostat_args *uap)
2200 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
2205 return (copyout(&osb, uap->ub, sizeof (osb)));
2209 * Get file status; this version does not follow links.
2211 #ifndef _SYS_SYSPROTO_H_
2212 struct olstat_args {
2218 olstat(struct thread *td, struct olstat_args *uap)
2224 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2225 UIO_USERSPACE, &sb, NULL);
2229 return (copyout(&osb, uap->ub, sizeof (osb)));
2233 * Convert from an old to a new stat structure.
2234 * XXX: many values are blindly truncated.
2237 cvtstat(struct stat *st, struct ostat *ost)
2240 bzero(ost, sizeof(*ost));
2241 ost->st_dev = st->st_dev;
2242 ost->st_ino = st->st_ino;
2243 ost->st_mode = st->st_mode;
2244 ost->st_nlink = st->st_nlink;
2245 ost->st_uid = st->st_uid;
2246 ost->st_gid = st->st_gid;
2247 ost->st_rdev = st->st_rdev;
2248 ost->st_size = MIN(st->st_size, INT32_MAX);
2249 ost->st_atim = st->st_atim;
2250 ost->st_mtim = st->st_mtim;
2251 ost->st_ctim = st->st_ctim;
2252 ost->st_blksize = st->st_blksize;
2253 ost->st_blocks = st->st_blocks;
2254 ost->st_flags = st->st_flags;
2255 ost->st_gen = st->st_gen;
2257 #endif /* COMPAT_43 */
2259 #if defined(COMPAT_43) || defined(COMPAT_FREEBSD11)
2260 int ino64_trunc_error;
2261 SYSCTL_INT(_vfs, OID_AUTO, ino64_trunc_error, CTLFLAG_RW,
2262 &ino64_trunc_error, 0,
2263 "Error on truncation of device, file or inode number, or link count");
2266 freebsd11_cvtstat(struct stat *st, struct freebsd11_stat *ost)
2269 ost->st_dev = st->st_dev;
2270 if (ost->st_dev != st->st_dev) {
2271 switch (ino64_trunc_error) {
2274 * Since dev_t is almost raw, don't clamp to the
2275 * maximum for case 2, but ignore the error.
2282 ost->st_ino = st->st_ino;
2283 if (ost->st_ino != st->st_ino) {
2284 switch (ino64_trunc_error) {
2291 ost->st_ino = UINT32_MAX;
2295 ost->st_mode = st->st_mode;
2296 ost->st_nlink = st->st_nlink;
2297 if (ost->st_nlink != st->st_nlink) {
2298 switch (ino64_trunc_error) {
2305 ost->st_nlink = UINT16_MAX;
2309 ost->st_uid = st->st_uid;
2310 ost->st_gid = st->st_gid;
2311 ost->st_rdev = st->st_rdev;
2312 if (ost->st_rdev != st->st_rdev) {
2313 switch (ino64_trunc_error) {
2320 ost->st_atim = st->st_atim;
2321 ost->st_mtim = st->st_mtim;
2322 ost->st_ctim = st->st_ctim;
2323 ost->st_size = st->st_size;
2324 ost->st_blocks = st->st_blocks;
2325 ost->st_blksize = st->st_blksize;
2326 ost->st_flags = st->st_flags;
2327 ost->st_gen = st->st_gen;
2329 ost->st_birthtim = st->st_birthtim;
2330 bzero((char *)&ost->st_birthtim + sizeof(ost->st_birthtim),
2331 sizeof(*ost) - offsetof(struct freebsd11_stat,
2332 st_birthtim) - sizeof(ost->st_birthtim));
2337 freebsd11_stat(struct thread *td, struct freebsd11_stat_args* uap)
2340 struct freebsd11_stat osb;
2343 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
2347 error = freebsd11_cvtstat(&sb, &osb);
2349 error = copyout(&osb, uap->ub, sizeof(osb));
2354 freebsd11_lstat(struct thread *td, struct freebsd11_lstat_args* uap)
2357 struct freebsd11_stat osb;
2360 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2361 UIO_USERSPACE, &sb, NULL);
2364 error = freebsd11_cvtstat(&sb, &osb);
2366 error = copyout(&osb, uap->ub, sizeof(osb));
2371 freebsd11_fhstat(struct thread *td, struct freebsd11_fhstat_args* uap)
2375 struct freebsd11_stat osb;
2378 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
2381 error = kern_fhstat(td, fh, &sb);
2384 error = freebsd11_cvtstat(&sb, &osb);
2386 error = copyout(&osb, uap->sb, sizeof(osb));
2391 freebsd11_fstatat(struct thread *td, struct freebsd11_fstatat_args* uap)
2394 struct freebsd11_stat osb;
2397 error = kern_statat(td, uap->flag, uap->fd, uap->path,
2398 UIO_USERSPACE, &sb, NULL);
2401 error = freebsd11_cvtstat(&sb, &osb);
2403 error = copyout(&osb, uap->buf, sizeof(osb));
2406 #endif /* COMPAT_FREEBSD11 */
2411 #ifndef _SYS_SYSPROTO_H_
2412 struct fstatat_args {
2420 sys_fstatat(struct thread *td, struct fstatat_args *uap)
2425 error = kern_statat(td, uap->flag, uap->fd, uap->path,
2426 UIO_USERSPACE, &sb, NULL);
2428 error = copyout(&sb, uap->buf, sizeof (sb));
2433 kern_statat(struct thread *td, int flag, int fd, const char *path,
2434 enum uio_seg pathseg, struct stat *sbp,
2435 void (*hook)(struct vnode *vp, struct stat *sbp))
2437 struct nameidata nd;
2440 if ((flag & ~(AT_SYMLINK_NOFOLLOW | AT_RESOLVE_BENEATH |
2441 AT_EMPTY_PATH)) != 0)
2444 NDINIT_ATRIGHTS(&nd, LOOKUP, at2cnpflags(flag, AT_RESOLVE_BENEATH |
2445 AT_SYMLINK_NOFOLLOW | AT_EMPTY_PATH) | LOCKSHARED | LOCKLEAF |
2446 AUDITVNODE1, pathseg, path, fd, &cap_fstat_rights);
2448 if ((error = namei(&nd)) != 0) {
2449 if (error == ENOTDIR &&
2450 (nd.ni_resflags & NIRES_EMPTYPATH) != 0)
2451 error = kern_fstat(td, fd, sbp);
2454 error = VOP_STAT(nd.ni_vp, sbp, td->td_ucred, NOCRED);
2456 if (__predict_false(hook != NULL))
2457 hook(nd.ni_vp, sbp);
2459 NDFREE_NOTHING(&nd);
2461 #ifdef __STAT_TIME_T_EXT
2462 sbp->st_atim_ext = 0;
2463 sbp->st_mtim_ext = 0;
2464 sbp->st_ctim_ext = 0;
2465 sbp->st_btim_ext = 0;
2468 if (KTRPOINT(td, KTR_STRUCT))
2469 ktrstat_error(sbp, error);
2474 #if defined(COMPAT_FREEBSD11)
2476 * Implementation of the NetBSD [l]stat() functions.
2479 freebsd11_cvtnstat(struct stat *sb, struct nstat *nsb)
2481 struct freebsd11_stat sb11;
2484 error = freebsd11_cvtstat(sb, &sb11);
2488 bzero(nsb, sizeof(*nsb));
2489 CP(sb11, *nsb, st_dev);
2490 CP(sb11, *nsb, st_ino);
2491 CP(sb11, *nsb, st_mode);
2492 CP(sb11, *nsb, st_nlink);
2493 CP(sb11, *nsb, st_uid);
2494 CP(sb11, *nsb, st_gid);
2495 CP(sb11, *nsb, st_rdev);
2496 CP(sb11, *nsb, st_atim);
2497 CP(sb11, *nsb, st_mtim);
2498 CP(sb11, *nsb, st_ctim);
2499 CP(sb11, *nsb, st_size);
2500 CP(sb11, *nsb, st_blocks);
2501 CP(sb11, *nsb, st_blksize);
2502 CP(sb11, *nsb, st_flags);
2503 CP(sb11, *nsb, st_gen);
2504 CP(sb11, *nsb, st_birthtim);
2508 #ifndef _SYS_SYSPROTO_H_
2509 struct freebsd11_nstat_args {
2515 freebsd11_nstat(struct thread *td, struct freebsd11_nstat_args *uap)
2521 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
2525 error = freebsd11_cvtnstat(&sb, &nsb);
2527 error = copyout(&nsb, uap->ub, sizeof (nsb));
2532 * NetBSD lstat. Get file status; this version does not follow links.
2534 #ifndef _SYS_SYSPROTO_H_
2535 struct freebsd11_nlstat_args {
2541 freebsd11_nlstat(struct thread *td, struct freebsd11_nlstat_args *uap)
2547 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2548 UIO_USERSPACE, &sb, NULL);
2551 error = freebsd11_cvtnstat(&sb, &nsb);
2553 error = copyout(&nsb, uap->ub, sizeof (nsb));
2556 #endif /* COMPAT_FREEBSD11 */
2559 * Get configurable pathname variables.
2561 #ifndef _SYS_SYSPROTO_H_
2562 struct pathconf_args {
2568 sys_pathconf(struct thread *td, struct pathconf_args *uap)
2573 error = kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name, FOLLOW,
2576 td->td_retval[0] = value;
2580 #ifndef _SYS_SYSPROTO_H_
2581 struct lpathconf_args {
2587 sys_lpathconf(struct thread *td, struct lpathconf_args *uap)
2592 error = kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name,
2595 td->td_retval[0] = value;
2600 kern_pathconf(struct thread *td, const char *path, enum uio_seg pathseg,
2601 int name, u_long flags, long *valuep)
2603 struct nameidata nd;
2606 NDINIT(&nd, LOOKUP, LOCKSHARED | LOCKLEAF | AUDITVNODE1 | flags,
2608 if ((error = namei(&nd)) != 0)
2610 NDFREE_NOTHING(&nd);
2612 error = VOP_PATHCONF(nd.ni_vp, name, valuep);
2618 * Return target name of a symbolic link.
2620 #ifndef _SYS_SYSPROTO_H_
2621 struct readlink_args {
2628 sys_readlink(struct thread *td, struct readlink_args *uap)
2631 return (kern_readlinkat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2632 uap->buf, UIO_USERSPACE, uap->count));
2634 #ifndef _SYS_SYSPROTO_H_
2635 struct readlinkat_args {
2643 sys_readlinkat(struct thread *td, struct readlinkat_args *uap)
2646 return (kern_readlinkat(td, uap->fd, uap->path, UIO_USERSPACE,
2647 uap->buf, UIO_USERSPACE, uap->bufsize));
2651 kern_readlinkat(struct thread *td, int fd, const char *path,
2652 enum uio_seg pathseg, char *buf, enum uio_seg bufseg, size_t count)
2655 struct nameidata nd;
2658 if (count > IOSIZE_MAX)
2661 NDINIT_AT(&nd, LOOKUP, NOFOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1 |
2662 EMPTYPATH, pathseg, path, fd);
2664 if ((error = namei(&nd)) != 0)
2666 NDFREE_NOTHING(&nd);
2669 error = kern_readlink_vp(vp, buf, bufseg, count, td);
2676 * Helper function to readlink from a vnode
2679 kern_readlink_vp(struct vnode *vp, char *buf, enum uio_seg bufseg, size_t count,
2686 ASSERT_VOP_LOCKED(vp, "kern_readlink_vp(): vp not locked");
2688 error = mac_vnode_check_readlink(td->td_ucred, vp);
2692 if (vp->v_type != VLNK && (vp->v_vflag & VV_READLINK) == 0)
2695 aiov.iov_base = buf;
2696 aiov.iov_len = count;
2697 auio.uio_iov = &aiov;
2698 auio.uio_iovcnt = 1;
2699 auio.uio_offset = 0;
2700 auio.uio_rw = UIO_READ;
2701 auio.uio_segflg = bufseg;
2703 auio.uio_resid = count;
2704 error = VOP_READLINK(vp, &auio, td->td_ucred);
2705 td->td_retval[0] = count - auio.uio_resid;
2710 * Common implementation code for chflags() and fchflags().
2713 setfflags(struct thread *td, struct vnode *vp, u_long flags)
2719 /* We can't support the value matching VNOVAL. */
2720 if (flags == VNOVAL)
2721 return (EOPNOTSUPP);
2724 * Prevent non-root users from setting flags on devices. When
2725 * a device is reused, users can retain ownership of the device
2726 * if they are allowed to set flags and programs assume that
2727 * chown can't fail when done as root.
2729 if (vp->v_type == VCHR || vp->v_type == VBLK) {
2730 error = priv_check(td, PRIV_VFS_CHFLAGS_DEV);
2735 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2738 vattr.va_flags = flags;
2739 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2741 error = mac_vnode_check_setflags(td->td_ucred, vp, vattr.va_flags);
2744 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
2746 vn_finished_write(mp);
2751 * Change flags of a file given a path name.
2753 #ifndef _SYS_SYSPROTO_H_
2754 struct chflags_args {
2760 sys_chflags(struct thread *td, struct chflags_args *uap)
2763 return (kern_chflagsat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2767 #ifndef _SYS_SYSPROTO_H_
2768 struct chflagsat_args {
2776 sys_chflagsat(struct thread *td, struct chflagsat_args *uap)
2779 return (kern_chflagsat(td, uap->fd, uap->path, UIO_USERSPACE,
2780 uap->flags, uap->atflag));
2784 * Same as chflags() but doesn't follow symlinks.
2786 #ifndef _SYS_SYSPROTO_H_
2787 struct lchflags_args {
2793 sys_lchflags(struct thread *td, struct lchflags_args *uap)
2796 return (kern_chflagsat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2797 uap->flags, AT_SYMLINK_NOFOLLOW));
2801 kern_chflagsat(struct thread *td, int fd, const char *path,
2802 enum uio_seg pathseg, u_long flags, int atflag)
2804 struct nameidata nd;
2807 if ((atflag & ~(AT_SYMLINK_NOFOLLOW | AT_RESOLVE_BENEATH |
2808 AT_EMPTY_PATH)) != 0)
2811 AUDIT_ARG_FFLAGS(flags);
2812 NDINIT_ATRIGHTS(&nd, LOOKUP, at2cnpflags(atflag, AT_SYMLINK_NOFOLLOW |
2813 AT_RESOLVE_BENEATH | AT_EMPTY_PATH) | AUDITVNODE1, pathseg, path,
2814 fd, &cap_fchflags_rights);
2815 if ((error = namei(&nd)) != 0)
2817 NDFREE_NOTHING(&nd);
2818 error = setfflags(td, nd.ni_vp, flags);
2824 * Change flags of a file given a file descriptor.
2826 #ifndef _SYS_SYSPROTO_H_
2827 struct fchflags_args {
2833 sys_fchflags(struct thread *td, struct fchflags_args *uap)
2838 AUDIT_ARG_FD(uap->fd);
2839 AUDIT_ARG_FFLAGS(uap->flags);
2840 error = getvnode(td, uap->fd, &cap_fchflags_rights,
2845 if (AUDITING_TD(td)) {
2846 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
2847 AUDIT_ARG_VNODE1(fp->f_vnode);
2848 VOP_UNLOCK(fp->f_vnode);
2851 error = setfflags(td, fp->f_vnode, uap->flags);
2857 * Common implementation code for chmod(), lchmod() and fchmod().
2860 setfmode(struct thread *td, struct ucred *cred, struct vnode *vp, int mode)
2866 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2868 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2870 vattr.va_mode = mode & ALLPERMS;
2872 error = mac_vnode_check_setmode(cred, vp, vattr.va_mode);
2875 error = VOP_SETATTR(vp, &vattr, cred);
2877 vn_finished_write(mp);
2882 * Change mode of a file given path name.
2884 #ifndef _SYS_SYSPROTO_H_
2891 sys_chmod(struct thread *td, struct chmod_args *uap)
2894 return (kern_fchmodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2898 #ifndef _SYS_SYSPROTO_H_
2899 struct fchmodat_args {
2907 sys_fchmodat(struct thread *td, struct fchmodat_args *uap)
2910 return (kern_fchmodat(td, uap->fd, uap->path, UIO_USERSPACE,
2911 uap->mode, uap->flag));
2915 * Change mode of a file given path name (don't follow links.)
2917 #ifndef _SYS_SYSPROTO_H_
2918 struct lchmod_args {
2924 sys_lchmod(struct thread *td, struct lchmod_args *uap)
2927 return (kern_fchmodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2928 uap->mode, AT_SYMLINK_NOFOLLOW));
2932 kern_fchmodat(struct thread *td, int fd, const char *path,
2933 enum uio_seg pathseg, mode_t mode, int flag)
2935 struct nameidata nd;
2938 if ((flag & ~(AT_SYMLINK_NOFOLLOW | AT_RESOLVE_BENEATH |
2939 AT_EMPTY_PATH)) != 0)
2942 AUDIT_ARG_MODE(mode);
2943 NDINIT_ATRIGHTS(&nd, LOOKUP, at2cnpflags(flag, AT_SYMLINK_NOFOLLOW |
2944 AT_RESOLVE_BENEATH | AT_EMPTY_PATH) | AUDITVNODE1, pathseg, path,
2945 fd, &cap_fchmod_rights);
2946 if ((error = namei(&nd)) != 0)
2948 NDFREE_NOTHING(&nd);
2949 error = setfmode(td, td->td_ucred, nd.ni_vp, mode);
2955 * Change mode of a file given a file descriptor.
2957 #ifndef _SYS_SYSPROTO_H_
2958 struct fchmod_args {
2964 sys_fchmod(struct thread *td, struct fchmod_args *uap)
2969 AUDIT_ARG_FD(uap->fd);
2970 AUDIT_ARG_MODE(uap->mode);
2972 error = fget(td, uap->fd, &cap_fchmod_rights, &fp);
2975 error = fo_chmod(fp, uap->mode, td->td_ucred, td);
2981 * Common implementation for chown(), lchown(), and fchown()
2984 setfown(struct thread *td, struct ucred *cred, struct vnode *vp, uid_t uid,
2991 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2993 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2998 error = mac_vnode_check_setowner(cred, vp, vattr.va_uid,
3002 error = VOP_SETATTR(vp, &vattr, cred);
3004 vn_finished_write(mp);
3009 * Set ownership given a path name.
3011 #ifndef _SYS_SYSPROTO_H_
3019 sys_chown(struct thread *td, struct chown_args *uap)
3022 return (kern_fchownat(td, AT_FDCWD, uap->path, UIO_USERSPACE, uap->uid,
3026 #ifndef _SYS_SYSPROTO_H_
3027 struct fchownat_args {
3036 sys_fchownat(struct thread *td, struct fchownat_args *uap)
3039 return (kern_fchownat(td, uap->fd, uap->path, UIO_USERSPACE, uap->uid,
3040 uap->gid, uap->flag));
3044 kern_fchownat(struct thread *td, int fd, const char *path,
3045 enum uio_seg pathseg, int uid, int gid, int flag)
3047 struct nameidata nd;
3050 if ((flag & ~(AT_SYMLINK_NOFOLLOW | AT_RESOLVE_BENEATH |
3051 AT_EMPTY_PATH)) != 0)
3054 AUDIT_ARG_OWNER(uid, gid);
3055 NDINIT_ATRIGHTS(&nd, LOOKUP, at2cnpflags(flag, AT_SYMLINK_NOFOLLOW |
3056 AT_RESOLVE_BENEATH | AT_EMPTY_PATH) | AUDITVNODE1, pathseg, path,
3057 fd, &cap_fchown_rights);
3059 if ((error = namei(&nd)) != 0)
3061 NDFREE_NOTHING(&nd);
3062 error = setfown(td, td->td_ucred, nd.ni_vp, uid, gid);
3068 * Set ownership given a path name, do not cross symlinks.
3070 #ifndef _SYS_SYSPROTO_H_
3071 struct lchown_args {
3078 sys_lchown(struct thread *td, struct lchown_args *uap)
3081 return (kern_fchownat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
3082 uap->uid, uap->gid, AT_SYMLINK_NOFOLLOW));
3086 * Set ownership given a file descriptor.
3088 #ifndef _SYS_SYSPROTO_H_
3089 struct fchown_args {
3096 sys_fchown(struct thread *td, struct fchown_args *uap)
3101 AUDIT_ARG_FD(uap->fd);
3102 AUDIT_ARG_OWNER(uap->uid, uap->gid);
3103 error = fget(td, uap->fd, &cap_fchown_rights, &fp);
3106 error = fo_chown(fp, uap->uid, uap->gid, td->td_ucred, td);
3112 * Common implementation code for utimes(), lutimes(), and futimes().
3115 getutimes(const struct timeval *usrtvp, enum uio_seg tvpseg,
3116 struct timespec *tsp)
3118 struct timeval tv[2];
3119 const struct timeval *tvp;
3122 if (usrtvp == NULL) {
3123 vfs_timestamp(&tsp[0]);
3126 if (tvpseg == UIO_SYSSPACE) {
3129 if ((error = copyin(usrtvp, tv, sizeof(tv))) != 0)
3134 if (tvp[0].tv_usec < 0 || tvp[0].tv_usec >= 1000000 ||
3135 tvp[1].tv_usec < 0 || tvp[1].tv_usec >= 1000000)
3137 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
3138 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
3144 * Common implementation code for futimens(), utimensat().
3146 #define UTIMENS_NULL 0x1
3147 #define UTIMENS_EXIT 0x2
3149 getutimens(const struct timespec *usrtsp, enum uio_seg tspseg,
3150 struct timespec *tsp, int *retflags)
3152 struct timespec tsnow;
3155 vfs_timestamp(&tsnow);
3157 if (usrtsp == NULL) {
3160 *retflags |= UTIMENS_NULL;
3163 if (tspseg == UIO_SYSSPACE) {
3166 } else if ((error = copyin(usrtsp, tsp, sizeof(*tsp) * 2)) != 0)
3168 if (tsp[0].tv_nsec == UTIME_OMIT && tsp[1].tv_nsec == UTIME_OMIT)
3169 *retflags |= UTIMENS_EXIT;
3170 if (tsp[0].tv_nsec == UTIME_NOW && tsp[1].tv_nsec == UTIME_NOW)
3171 *retflags |= UTIMENS_NULL;
3172 if (tsp[0].tv_nsec == UTIME_OMIT)
3173 tsp[0].tv_sec = VNOVAL;
3174 else if (tsp[0].tv_nsec == UTIME_NOW)
3176 else if (tsp[0].tv_nsec < 0 || tsp[0].tv_nsec >= 1000000000L)
3178 if (tsp[1].tv_nsec == UTIME_OMIT)
3179 tsp[1].tv_sec = VNOVAL;
3180 else if (tsp[1].tv_nsec == UTIME_NOW)
3182 else if (tsp[1].tv_nsec < 0 || tsp[1].tv_nsec >= 1000000000L)
3189 * Common implementation code for utimes(), lutimes(), futimes(), futimens(),
3193 setutimes(struct thread *td, struct vnode *vp, const struct timespec *ts,
3194 int numtimes, int nullflag)
3201 setbirthtime = false;
3202 vattr.va_birthtime.tv_sec = VNOVAL;
3203 vattr.va_birthtime.tv_nsec = 0;
3205 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
3207 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3208 if (numtimes < 3 && VOP_GETATTR(vp, &vattr, td->td_ucred) == 0 &&
3209 timespeccmp(&ts[1], &vattr.va_birthtime, < ))
3210 setbirthtime = true;
3212 vattr.va_atime = ts[0];
3213 vattr.va_mtime = ts[1];
3215 vattr.va_birthtime = ts[1];
3217 vattr.va_birthtime = ts[2];
3219 vattr.va_vaflags |= VA_UTIMES_NULL;
3221 error = mac_vnode_check_setutimes(td->td_ucred, vp, vattr.va_atime,
3225 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3227 vn_finished_write(mp);
3232 * Set the access and modification times of a file.
3234 #ifndef _SYS_SYSPROTO_H_
3235 struct utimes_args {
3237 struct timeval *tptr;
3241 sys_utimes(struct thread *td, struct utimes_args *uap)
3244 return (kern_utimesat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
3245 uap->tptr, UIO_USERSPACE));
3248 #ifndef _SYS_SYSPROTO_H_
3249 struct futimesat_args {
3252 const struct timeval * times;
3256 sys_futimesat(struct thread *td, struct futimesat_args *uap)
3259 return (kern_utimesat(td, uap->fd, uap->path, UIO_USERSPACE,
3260 uap->times, UIO_USERSPACE));
3264 kern_utimesat(struct thread *td, int fd, const char *path,
3265 enum uio_seg pathseg, const struct timeval *tptr, enum uio_seg tptrseg)
3267 struct nameidata nd;
3268 struct timespec ts[2];
3271 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3273 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, fd,
3274 &cap_futimes_rights);
3276 if ((error = namei(&nd)) != 0)
3278 NDFREE_NOTHING(&nd);
3279 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3285 * Set the access and modification times of a file.
3287 #ifndef _SYS_SYSPROTO_H_
3288 struct lutimes_args {
3290 struct timeval *tptr;
3294 sys_lutimes(struct thread *td, struct lutimes_args *uap)
3297 return (kern_lutimes(td, uap->path, UIO_USERSPACE, uap->tptr,
3302 kern_lutimes(struct thread *td, const char *path, enum uio_seg pathseg,
3303 const struct timeval *tptr, enum uio_seg tptrseg)
3305 struct timespec ts[2];
3306 struct nameidata nd;
3309 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3311 NDINIT(&nd, LOOKUP, NOFOLLOW | AUDITVNODE1, pathseg, path);
3312 if ((error = namei(&nd)) != 0)
3314 NDFREE_NOTHING(&nd);
3315 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3321 * Set the access and modification times of a file.
3323 #ifndef _SYS_SYSPROTO_H_
3324 struct futimes_args {
3326 struct timeval *tptr;
3330 sys_futimes(struct thread *td, struct futimes_args *uap)
3333 return (kern_futimes(td, uap->fd, uap->tptr, UIO_USERSPACE));
3337 kern_futimes(struct thread *td, int fd, const struct timeval *tptr,
3338 enum uio_seg tptrseg)
3340 struct timespec ts[2];
3345 error = getutimes(tptr, tptrseg, ts);
3348 error = getvnode(td, fd, &cap_futimes_rights, &fp);
3352 if (AUDITING_TD(td)) {
3353 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
3354 AUDIT_ARG_VNODE1(fp->f_vnode);
3355 VOP_UNLOCK(fp->f_vnode);
3358 error = setutimes(td, fp->f_vnode, ts, 2, tptr == NULL);
3364 sys_futimens(struct thread *td, struct futimens_args *uap)
3367 return (kern_futimens(td, uap->fd, uap->times, UIO_USERSPACE));
3371 kern_futimens(struct thread *td, int fd, const struct timespec *tptr,
3372 enum uio_seg tptrseg)
3374 struct timespec ts[2];
3379 error = getutimens(tptr, tptrseg, ts, &flags);
3382 if (flags & UTIMENS_EXIT)
3384 error = getvnode(td, fd, &cap_futimes_rights, &fp);
3388 if (AUDITING_TD(td)) {
3389 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
3390 AUDIT_ARG_VNODE1(fp->f_vnode);
3391 VOP_UNLOCK(fp->f_vnode);
3394 error = setutimes(td, fp->f_vnode, ts, 2, flags & UTIMENS_NULL);
3400 sys_utimensat(struct thread *td, struct utimensat_args *uap)
3403 return (kern_utimensat(td, uap->fd, uap->path, UIO_USERSPACE,
3404 uap->times, UIO_USERSPACE, uap->flag));
3408 kern_utimensat(struct thread *td, int fd, const char *path,
3409 enum uio_seg pathseg, const struct timespec *tptr, enum uio_seg tptrseg,
3412 struct nameidata nd;
3413 struct timespec ts[2];
3416 if ((flag & ~(AT_SYMLINK_NOFOLLOW | AT_RESOLVE_BENEATH |
3417 AT_EMPTY_PATH)) != 0)
3420 if ((error = getutimens(tptr, tptrseg, ts, &flags)) != 0)
3422 NDINIT_ATRIGHTS(&nd, LOOKUP, at2cnpflags(flag, AT_SYMLINK_NOFOLLOW |
3423 AT_RESOLVE_BENEATH | AT_EMPTY_PATH) | AUDITVNODE1,
3424 pathseg, path, fd, &cap_futimes_rights);
3425 if ((error = namei(&nd)) != 0)
3428 * We are allowed to call namei() regardless of 2xUTIME_OMIT.
3430 * "If both tv_nsec fields are UTIME_OMIT... EACCESS may be detected."
3431 * "Search permission is denied by a component of the path prefix."
3433 NDFREE_NOTHING(&nd);
3434 if ((flags & UTIMENS_EXIT) == 0)
3435 error = setutimes(td, nd.ni_vp, ts, 2, flags & UTIMENS_NULL);
3441 * Truncate a file given its path name.
3443 #ifndef _SYS_SYSPROTO_H_
3444 struct truncate_args {
3451 sys_truncate(struct thread *td, struct truncate_args *uap)
3454 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3458 kern_truncate(struct thread *td, const char *path, enum uio_seg pathseg,
3465 struct nameidata nd;
3472 NDINIT(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path);
3473 if ((error = namei(&nd)) != 0)
3476 rl_cookie = vn_rangelock_wlock(vp, 0, OFF_MAX);
3477 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0) {
3478 vn_rangelock_unlock(vp, rl_cookie);
3482 NDFREE(&nd, NDF_ONLY_PNBUF);
3483 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3484 if (vp->v_type == VDIR)
3487 else if ((error = mac_vnode_check_write(td->td_ucred, NOCRED, vp))) {
3490 else if ((error = vn_writechk(vp)) == 0 &&
3491 (error = VOP_ACCESS(vp, VWRITE, td->td_ucred, td)) == 0) {
3493 vattr.va_size = length;
3494 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3497 vn_finished_write(mp);
3498 vn_rangelock_unlock(vp, rl_cookie);
3500 if (error == ERELOOKUP)
3505 #if defined(COMPAT_43)
3507 * Truncate a file given its path name.
3509 #ifndef _SYS_SYSPROTO_H_
3510 struct otruncate_args {
3516 otruncate(struct thread *td, struct otruncate_args *uap)
3519 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3521 #endif /* COMPAT_43 */
3523 #if defined(COMPAT_FREEBSD6)
3524 /* Versions with the pad argument */
3526 freebsd6_truncate(struct thread *td, struct freebsd6_truncate_args *uap)
3529 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3533 freebsd6_ftruncate(struct thread *td, struct freebsd6_ftruncate_args *uap)
3536 return (kern_ftruncate(td, uap->fd, uap->length));
3541 kern_fsync(struct thread *td, int fd, bool fullsync)
3549 error = getvnode(td, fd, &cap_fsync_rights, &fp);
3555 /* XXXKIB: compete outstanding aio writes */;
3558 error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
3561 vn_lock(vp, vn_lktype_write(mp, vp) | LK_RETRY);
3562 AUDIT_ARG_VNODE1(vp);
3563 if (vp->v_object != NULL) {
3564 VM_OBJECT_WLOCK(vp->v_object);
3565 vm_object_page_clean(vp->v_object, 0, 0, 0);
3566 VM_OBJECT_WUNLOCK(vp->v_object);
3568 error = fullsync ? VOP_FSYNC(vp, MNT_WAIT, td) : VOP_FDATASYNC(vp, td);
3570 vn_finished_write(mp);
3571 if (error == ERELOOKUP)
3579 * Sync an open file.
3581 #ifndef _SYS_SYSPROTO_H_
3587 sys_fsync(struct thread *td, struct fsync_args *uap)
3590 return (kern_fsync(td, uap->fd, true));
3594 sys_fdatasync(struct thread *td, struct fdatasync_args *uap)
3597 return (kern_fsync(td, uap->fd, false));
3601 * Rename files. Source and destination must either both be directories, or
3602 * both not be directories. If target is a directory, it must be empty.
3604 #ifndef _SYS_SYSPROTO_H_
3605 struct rename_args {
3611 sys_rename(struct thread *td, struct rename_args *uap)
3614 return (kern_renameat(td, AT_FDCWD, uap->from, AT_FDCWD,
3615 uap->to, UIO_USERSPACE));
3618 #ifndef _SYS_SYSPROTO_H_
3619 struct renameat_args {
3627 sys_renameat(struct thread *td, struct renameat_args *uap)
3630 return (kern_renameat(td, uap->oldfd, uap->old, uap->newfd, uap->new,
3636 kern_renameat_mac(struct thread *td, int oldfd, const char *old, int newfd,
3637 const char *new, enum uio_seg pathseg, struct nameidata *fromnd)
3641 NDINIT_ATRIGHTS(fromnd, DELETE, LOCKPARENT | LOCKLEAF | SAVESTART |
3642 AUDITVNODE1, pathseg, old, oldfd, &cap_renameat_source_rights);
3643 if ((error = namei(fromnd)) != 0)
3645 error = mac_vnode_check_rename_from(td->td_ucred, fromnd->ni_dvp,
3646 fromnd->ni_vp, &fromnd->ni_cnd);
3647 VOP_UNLOCK(fromnd->ni_dvp);
3648 if (fromnd->ni_dvp != fromnd->ni_vp)
3649 VOP_UNLOCK(fromnd->ni_vp);
3651 NDFREE(fromnd, NDF_ONLY_PNBUF);
3652 vrele(fromnd->ni_dvp);
3653 vrele(fromnd->ni_vp);
3654 if (fromnd->ni_startdir)
3655 vrele(fromnd->ni_startdir);
3662 kern_renameat(struct thread *td, int oldfd, const char *old, int newfd,
3663 const char *new, enum uio_seg pathseg)
3665 struct mount *mp = NULL;
3666 struct vnode *tvp, *fvp, *tdvp;
3667 struct nameidata fromnd, tond;
3674 if (mac_vnode_check_rename_from_enabled()) {
3675 error = kern_renameat_mac(td, oldfd, old, newfd, new, pathseg,
3681 NDINIT_ATRIGHTS(&fromnd, DELETE, WANTPARENT | SAVESTART | AUDITVNODE1,
3682 pathseg, old, oldfd, &cap_renameat_source_rights);
3683 if ((error = namei(&fromnd)) != 0)
3689 tondflags = LOCKPARENT | LOCKLEAF | NOCACHE | SAVESTART | AUDITVNODE2;
3690 if (fromnd.ni_vp->v_type == VDIR)
3691 tondflags |= WILLBEDIR;
3692 NDINIT_ATRIGHTS(&tond, RENAME, tondflags, pathseg, new, newfd,
3693 &cap_renameat_target_rights);
3694 if ((error = namei(&tond)) != 0) {
3695 /* Translate error code for rename("dir1", "dir2/."). */
3696 if (error == EISDIR && fvp->v_type == VDIR)
3698 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3699 vrele(fromnd.ni_dvp);
3705 error = vn_start_write(fvp, &mp, V_NOWAIT);
3707 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3708 NDFREE(&tond, NDF_ONLY_PNBUF);
3715 vrele(fromnd.ni_dvp);
3717 vrele(tond.ni_startdir);
3718 if (fromnd.ni_startdir != NULL)
3719 vrele(fromnd.ni_startdir);
3720 error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH);
3726 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
3729 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
3734 if (newfd != AT_FDCWD && (tond.ni_resflags & NIRES_ABS) == 0) {
3736 * If the target already exists we require CAP_UNLINKAT
3737 * from 'newfd', when newfd was used for the lookup.
3739 error = cap_check(&tond.ni_filecaps.fc_rights,
3740 &cap_unlinkat_rights);
3751 * If the source is the same as the destination (that is, if they
3752 * are links to the same vnode), then there is nothing to do.
3758 error = mac_vnode_check_rename_to(td->td_ucred, tdvp,
3759 tond.ni_vp, fromnd.ni_dvp == tdvp, &tond.ni_cnd);
3763 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
3764 tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
3765 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3766 NDFREE(&tond, NDF_ONLY_PNBUF);
3768 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3769 NDFREE(&tond, NDF_ONLY_PNBUF);
3776 vrele(fromnd.ni_dvp);
3779 vrele(tond.ni_startdir);
3780 vn_finished_write(mp);
3782 if (fromnd.ni_startdir)
3783 vrele(fromnd.ni_startdir);
3784 if (error == ERESTART)
3786 if (error == ERELOOKUP)
3792 * Make a directory file.
3794 #ifndef _SYS_SYSPROTO_H_
3801 sys_mkdir(struct thread *td, struct mkdir_args *uap)
3804 return (kern_mkdirat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
3808 #ifndef _SYS_SYSPROTO_H_
3809 struct mkdirat_args {
3816 sys_mkdirat(struct thread *td, struct mkdirat_args *uap)
3819 return (kern_mkdirat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode));
3823 kern_mkdirat(struct thread *td, int fd, const char *path, enum uio_seg segflg,
3828 struct nameidata nd;
3831 AUDIT_ARG_MODE(mode);
3835 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
3836 NC_NOMAKEENTRY | NC_KEEPPOSENTRY | FAILIFEXISTS | WILLBEDIR,
3837 segflg, path, fd, &cap_mkdirat_rights);
3838 if ((error = namei(&nd)) != 0)
3840 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3841 NDFREE(&nd, NDF_ONLY_PNBUF);
3843 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3848 vattr.va_type = VDIR;
3849 vattr.va_mode = (mode & ACCESSPERMS) &~ td->td_proc->p_pd->pd_cmask;
3851 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
3856 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
3860 NDFREE(&nd, NDF_ONLY_PNBUF);
3861 VOP_VPUT_PAIR(nd.ni_dvp, error == 0 ? &nd.ni_vp : NULL, true);
3862 vn_finished_write(mp);
3863 if (error == ERELOOKUP)
3869 * Remove a directory file.
3871 #ifndef _SYS_SYSPROTO_H_
3877 sys_rmdir(struct thread *td, struct rmdir_args *uap)
3880 return (kern_frmdirat(td, AT_FDCWD, uap->path, FD_NONE, UIO_USERSPACE,
3885 kern_frmdirat(struct thread *td, int dfd, const char *path, int fd,
3886 enum uio_seg pathseg, int flag)
3891 struct nameidata nd;
3892 cap_rights_t rights;
3896 if (fd != FD_NONE) {
3897 error = getvnode(td, fd, cap_rights_init_one(&rights,
3906 NDINIT_ATRIGHTS(&nd, DELETE, LOCKPARENT | LOCKLEAF | AUDITVNODE1 |
3907 at2cnpflags(flag, AT_RESOLVE_BENEATH),
3908 pathseg, path, dfd, &cap_unlinkat_rights);
3909 if ((error = namei(&nd)) != 0)
3912 if (vp->v_type != VDIR) {
3917 * No rmdir "." please.
3919 if (nd.ni_dvp == vp) {
3924 * The root of a mounted filesystem cannot be deleted.
3926 if (vp->v_vflag & VV_ROOT) {
3931 if (fp != NULL && fp->f_vnode != vp) {
3932 if (VN_IS_DOOMED(fp->f_vnode))
3940 error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp,
3945 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3946 NDFREE(&nd, NDF_ONLY_PNBUF);
3948 if (nd.ni_dvp == vp)
3952 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3956 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_UNLINK);
3957 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
3958 vn_finished_write(mp);
3960 NDFREE(&nd, NDF_ONLY_PNBUF);
3962 if (nd.ni_dvp == vp)
3966 if (error == ERELOOKUP)
3974 #if defined(COMPAT_43) || defined(COMPAT_FREEBSD11)
3976 freebsd11_kern_getdirentries(struct thread *td, int fd, char *ubuf, u_int count,
3977 long *basep, void (*func)(struct freebsd11_dirent *))
3979 struct freebsd11_dirent dstdp;
3980 struct dirent *dp, *edp;
3983 ssize_t resid, ucount;
3986 /* XXX arbitrary sanity limit on `count'. */
3987 count = min(count, 64 * 1024);
3989 dirbuf = malloc(count, M_TEMP, M_WAITOK);
3991 error = kern_getdirentries(td, fd, dirbuf, count, &base, &resid,
3999 for (dp = (struct dirent *)dirbuf,
4000 edp = (struct dirent *)&dirbuf[count - resid];
4001 ucount < count && dp < edp; ) {
4002 if (dp->d_reclen == 0)
4004 MPASS(dp->d_reclen >= _GENERIC_DIRLEN(0));
4005 if (dp->d_namlen >= sizeof(dstdp.d_name))
4007 dstdp.d_type = dp->d_type;
4008 dstdp.d_namlen = dp->d_namlen;
4009 dstdp.d_fileno = dp->d_fileno; /* truncate */
4010 if (dstdp.d_fileno != dp->d_fileno) {
4011 switch (ino64_trunc_error) {
4019 dstdp.d_fileno = UINT32_MAX;
4023 dstdp.d_reclen = sizeof(dstdp) - sizeof(dstdp.d_name) +
4024 ((dp->d_namlen + 1 + 3) &~ 3);
4025 bcopy(dp->d_name, dstdp.d_name, dstdp.d_namlen);
4026 bzero(dstdp.d_name + dstdp.d_namlen,
4027 dstdp.d_reclen - offsetof(struct freebsd11_dirent, d_name) -
4029 MPASS(dstdp.d_reclen <= dp->d_reclen);
4030 MPASS(ucount + dstdp.d_reclen <= count);
4033 error = copyout(&dstdp, ubuf + ucount, dstdp.d_reclen);
4036 dp = (struct dirent *)((char *)dp + dp->d_reclen);
4037 ucount += dstdp.d_reclen;
4041 free(dirbuf, M_TEMP);
4043 td->td_retval[0] = ucount;
4050 ogetdirentries_cvt(struct freebsd11_dirent *dp)
4052 #if (BYTE_ORDER == LITTLE_ENDIAN)
4054 * The expected low byte of dp->d_namlen is our dp->d_type.
4055 * The high MBZ byte of dp->d_namlen is our dp->d_namlen.
4057 dp->d_type = dp->d_namlen;
4061 * The dp->d_type is the high byte of the expected dp->d_namlen,
4062 * so must be zero'ed.
4069 * Read a block of directory entries in a filesystem independent format.
4071 #ifndef _SYS_SYSPROTO_H_
4072 struct ogetdirentries_args {
4080 ogetdirentries(struct thread *td, struct ogetdirentries_args *uap)
4085 error = kern_ogetdirentries(td, uap, &loff);
4087 error = copyout(&loff, uap->basep, sizeof(long));
4092 kern_ogetdirentries(struct thread *td, struct ogetdirentries_args *uap,
4098 /* XXX arbitrary sanity limit on `count'. */
4099 if (uap->count > 64 * 1024)
4102 error = freebsd11_kern_getdirentries(td, uap->fd, uap->buf, uap->count,
4103 &base, ogetdirentries_cvt);
4105 if (error == 0 && uap->basep != NULL)
4106 error = copyout(&base, uap->basep, sizeof(long));
4110 #endif /* COMPAT_43 */
4112 #if defined(COMPAT_FREEBSD11)
4113 #ifndef _SYS_SYSPROTO_H_
4114 struct freebsd11_getdirentries_args {
4122 freebsd11_getdirentries(struct thread *td,
4123 struct freebsd11_getdirentries_args *uap)
4128 error = freebsd11_kern_getdirentries(td, uap->fd, uap->buf, uap->count,
4131 if (error == 0 && uap->basep != NULL)
4132 error = copyout(&base, uap->basep, sizeof(long));
4137 freebsd11_getdents(struct thread *td, struct freebsd11_getdents_args *uap)
4139 struct freebsd11_getdirentries_args ap;
4143 ap.count = uap->count;
4145 return (freebsd11_getdirentries(td, &ap));
4147 #endif /* COMPAT_FREEBSD11 */
4150 * Read a block of directory entries in a filesystem independent format.
4153 sys_getdirentries(struct thread *td, struct getdirentries_args *uap)
4158 error = kern_getdirentries(td, uap->fd, uap->buf, uap->count, &base,
4159 NULL, UIO_USERSPACE);
4162 if (uap->basep != NULL)
4163 error = copyout(&base, uap->basep, sizeof(off_t));
4168 kern_getdirentries(struct thread *td, int fd, char *buf, size_t count,
4169 off_t *basep, ssize_t *residp, enum uio_seg bufseg)
4180 if (count > IOSIZE_MAX)
4182 auio.uio_resid = count;
4183 error = getvnode(td, fd, &cap_read_rights, &fp);
4186 if ((fp->f_flag & FREAD) == 0) {
4191 foffset = foffset_lock(fp, 0);
4193 if (vp->v_type != VDIR) {
4197 aiov.iov_base = buf;
4198 aiov.iov_len = count;
4199 auio.uio_iov = &aiov;
4200 auio.uio_iovcnt = 1;
4201 auio.uio_rw = UIO_READ;
4202 auio.uio_segflg = bufseg;
4204 vn_lock(vp, LK_SHARED | LK_RETRY);
4205 AUDIT_ARG_VNODE1(vp);
4206 loff = auio.uio_offset = foffset;
4208 error = mac_vnode_check_readdir(td->td_ucred, vp);
4211 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL,
4213 foffset = auio.uio_offset;
4218 if (count == auio.uio_resid &&
4219 (vp->v_vflag & VV_ROOT) &&
4220 (vp->v_mount->mnt_flag & MNT_UNION)) {
4221 struct vnode *tvp = vp;
4223 vp = vp->v_mount->mnt_vnodecovered;
4233 *residp = auio.uio_resid;
4234 td->td_retval[0] = count - auio.uio_resid;
4236 foffset_unlock(fp, foffset, 0);
4242 * Set the mode mask for creation of filesystem nodes.
4244 #ifndef _SYS_SYSPROTO_H_
4250 sys_umask(struct thread *td, struct umask_args *uap)
4252 struct pwddesc *pdp;
4254 pdp = td->td_proc->p_pd;
4256 td->td_retval[0] = pdp->pd_cmask;
4257 pdp->pd_cmask = uap->newmask & ALLPERMS;
4258 PWDDESC_XUNLOCK(pdp);
4263 * Void all references to file by ripping underlying filesystem away from
4266 #ifndef _SYS_SYSPROTO_H_
4267 struct revoke_args {
4272 sys_revoke(struct thread *td, struct revoke_args *uap)
4276 struct nameidata nd;
4279 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
4281 if ((error = namei(&nd)) != 0)
4284 NDFREE_NOTHING(&nd);
4285 if (vp->v_type != VCHR || vp->v_rdev == NULL) {
4290 error = mac_vnode_check_revoke(td->td_ucred, vp);
4294 error = VOP_GETATTR(vp, &vattr, td->td_ucred);
4297 if (td->td_ucred->cr_uid != vattr.va_uid) {
4298 error = priv_check(td, PRIV_VFS_ADMIN);
4302 if (devfs_usecount(vp) > 0)
4303 VOP_REVOKE(vp, REVOKEALL);
4310 * This variant of getvnode() allows O_PATH files. Caller should
4311 * ensure that returned file and vnode are only used for compatible
4315 getvnode_path(struct thread *td, int fd, cap_rights_t *rightsp,
4321 error = fget_unlocked(td->td_proc->p_fd, fd, rightsp, &fp);
4326 * The file could be not of the vnode type, or it may be not
4327 * yet fully initialized, in which case the f_vnode pointer
4328 * may be set, but f_ops is still badfileops. E.g.,
4329 * devfs_open() transiently create such situation to
4330 * facilitate csw d_fdopen().
4332 * Dupfdopen() handling in kern_openat() installs the
4333 * half-baked file into the process descriptor table, allowing
4334 * other thread to dereference it. Guard against the race by
4337 if (__predict_false(fp->f_vnode == NULL || fp->f_ops == &badfileops)) {
4347 * Convert a user file descriptor to a kernel file entry and check
4348 * that, if it is a capability, the correct rights are present.
4349 * A reference on the file entry is held upon returning.
4352 getvnode(struct thread *td, int fd, cap_rights_t *rightsp, struct file **fpp)
4356 error = getvnode_path(td, fd, rightsp, fpp);
4357 if (__predict_false(error != 0))
4361 * Filter out O_PATH file descriptors, most getvnode() callers
4362 * do not call fo_ methods.
4364 if (__predict_false((*fpp)->f_ops == &path_fileops)) {
4373 * Get an (NFS) file handle.
4375 #ifndef _SYS_SYSPROTO_H_
4376 struct lgetfh_args {
4382 sys_lgetfh(struct thread *td, struct lgetfh_args *uap)
4385 return (kern_getfhat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->fname,
4386 UIO_USERSPACE, uap->fhp, UIO_USERSPACE));
4389 #ifndef _SYS_SYSPROTO_H_
4396 sys_getfh(struct thread *td, struct getfh_args *uap)
4399 return (kern_getfhat(td, 0, AT_FDCWD, uap->fname, UIO_USERSPACE,
4400 uap->fhp, UIO_USERSPACE));
4404 * syscall for the rpc.lockd to use to translate an open descriptor into
4405 * a NFS file handle.
4407 * warning: do not remove the priv_check() call or this becomes one giant
4410 #ifndef _SYS_SYSPROTO_H_
4411 struct getfhat_args {
4419 sys_getfhat(struct thread *td, struct getfhat_args *uap)
4422 return (kern_getfhat(td, uap->flags, uap->fd, uap->path, UIO_USERSPACE,
4423 uap->fhp, UIO_USERSPACE));
4427 kern_getfhat(struct thread *td, int flags, int fd, const char *path,
4428 enum uio_seg pathseg, fhandle_t *fhp, enum uio_seg fhseg)
4430 struct nameidata nd;
4435 if ((flags & ~(AT_SYMLINK_NOFOLLOW | AT_RESOLVE_BENEATH)) != 0)
4437 error = priv_check(td, PRIV_VFS_GETFH);
4440 NDINIT_AT(&nd, LOOKUP, at2cnpflags(flags, AT_SYMLINK_NOFOLLOW |
4441 AT_RESOLVE_BENEATH) | LOCKLEAF | AUDITVNODE1, pathseg, path,
4446 NDFREE_NOTHING(&nd);
4448 bzero(&fh, sizeof(fh));
4449 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
4450 error = VOP_VPTOFH(vp, &fh.fh_fid);
4453 if (fhseg == UIO_USERSPACE)
4454 error = copyout(&fh, fhp, sizeof (fh));
4456 memcpy(fhp, &fh, sizeof(fh));
4461 #ifndef _SYS_SYSPROTO_H_
4462 struct fhlink_args {
4468 sys_fhlink(struct thread *td, struct fhlink_args *uap)
4471 return (kern_fhlinkat(td, AT_FDCWD, uap->to, UIO_USERSPACE, uap->fhp));
4474 #ifndef _SYS_SYSPROTO_H_
4475 struct fhlinkat_args {
4482 sys_fhlinkat(struct thread *td, struct fhlinkat_args *uap)
4485 return (kern_fhlinkat(td, uap->tofd, uap->to, UIO_USERSPACE, uap->fhp));
4489 kern_fhlinkat(struct thread *td, int fd, const char *path,
4490 enum uio_seg pathseg, fhandle_t *fhp)
4497 error = priv_check(td, PRIV_VFS_GETFH);
4500 error = copyin(fhp, &fh, sizeof(fh));
4505 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4507 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_SHARED, &vp);
4512 error = kern_linkat_vp(td, vp, fd, path, pathseg);
4513 } while (error == EAGAIN || error == ERELOOKUP);
4517 #ifndef _SYS_SYSPROTO_H_
4518 struct fhreadlink_args {
4525 sys_fhreadlink(struct thread *td, struct fhreadlink_args *uap)
4532 error = priv_check(td, PRIV_VFS_GETFH);
4535 if (uap->bufsize > IOSIZE_MAX)
4537 error = copyin(uap->fhp, &fh, sizeof(fh));
4540 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4542 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_SHARED, &vp);
4546 error = kern_readlink_vp(vp, uap->buf, UIO_USERSPACE, uap->bufsize, td);
4552 * syscall for the rpc.lockd to use to translate a NFS file handle into an
4555 * warning: do not remove the priv_check() call or this becomes one giant
4558 #ifndef _SYS_SYSPROTO_H_
4559 struct fhopen_args {
4560 const struct fhandle *u_fhp;
4565 sys_fhopen(struct thread *td, struct fhopen_args *uap)
4567 return (kern_fhopen(td, uap->u_fhp, uap->flags));
4571 kern_fhopen(struct thread *td, const struct fhandle *u_fhp, int flags)
4580 error = priv_check(td, PRIV_VFS_FHOPEN);
4584 fmode = FFLAGS(flags);
4585 /* why not allow a non-read/write open for our lockd? */
4586 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
4588 error = copyin(u_fhp, &fhp, sizeof(fhp));
4591 /* find the mount point */
4592 mp = vfs_busyfs(&fhp.fh_fsid);
4595 /* now give me my vnode, it gets returned to me locked */
4596 error = VFS_FHTOVP(mp, &fhp.fh_fid, LK_EXCLUSIVE, &vp);
4601 error = falloc_noinstall(td, &fp);
4607 * An extra reference on `fp' has been held for us by
4608 * falloc_noinstall().
4614 error = vn_open_vnode(vp, fmode, td->td_ucred, td, fp);
4616 KASSERT(fp->f_ops == &badfileops,
4617 ("VOP_OPEN in fhopen() set f_ops"));
4618 KASSERT(td->td_dupfd < 0,
4619 ("fhopen() encountered fdopen()"));
4628 finit_vnode(fp, fmode, NULL, &vnops);
4630 if ((fmode & O_TRUNC) != 0) {
4631 error = fo_truncate(fp, 0, td->td_ucred, td);
4636 error = finstall(td, fp, &indx, fmode, NULL);
4639 td->td_retval[0] = indx;
4644 * Stat an (NFS) file handle.
4646 #ifndef _SYS_SYSPROTO_H_
4647 struct fhstat_args {
4648 struct fhandle *u_fhp;
4653 sys_fhstat(struct thread *td, struct fhstat_args *uap)
4659 error = copyin(uap->u_fhp, &fh, sizeof(fh));
4662 error = kern_fhstat(td, fh, &sb);
4664 error = copyout(&sb, uap->sb, sizeof(sb));
4669 kern_fhstat(struct thread *td, struct fhandle fh, struct stat *sb)
4675 error = priv_check(td, PRIV_VFS_FHSTAT);
4678 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4680 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_EXCLUSIVE, &vp);
4684 error = VOP_STAT(vp, sb, td->td_ucred, NOCRED);
4690 * Implement fstatfs() for (NFS) file handles.
4692 #ifndef _SYS_SYSPROTO_H_
4693 struct fhstatfs_args {
4694 struct fhandle *u_fhp;
4699 sys_fhstatfs(struct thread *td, struct fhstatfs_args *uap)
4705 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4708 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
4709 error = kern_fhstatfs(td, fh, sfp);
4711 error = copyout(sfp, uap->buf, sizeof(*sfp));
4712 free(sfp, M_STATFS);
4717 kern_fhstatfs(struct thread *td, fhandle_t fh, struct statfs *buf)
4723 error = priv_check(td, PRIV_VFS_FHSTATFS);
4726 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4728 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_EXCLUSIVE, &vp);
4734 error = prison_canseemount(td->td_ucred, mp);
4738 error = mac_mount_check_stat(td->td_ucred, mp);
4742 error = VFS_STATFS(mp, buf);
4749 * Unlike madvise(2), we do not make a best effort to remember every
4750 * possible caching hint. Instead, we remember the last setting with
4751 * the exception that we will allow POSIX_FADV_NORMAL to adjust the
4752 * region of any current setting.
4755 kern_posix_fadvise(struct thread *td, int fd, off_t offset, off_t len,
4758 struct fadvise_info *fa, *new;
4764 if (offset < 0 || len < 0 || offset > OFF_MAX - len)
4766 AUDIT_ARG_VALUE(advice);
4768 case POSIX_FADV_SEQUENTIAL:
4769 case POSIX_FADV_RANDOM:
4770 case POSIX_FADV_NOREUSE:
4771 new = malloc(sizeof(*fa), M_FADVISE, M_WAITOK);
4773 case POSIX_FADV_NORMAL:
4774 case POSIX_FADV_WILLNEED:
4775 case POSIX_FADV_DONTNEED:
4781 /* XXX: CAP_POSIX_FADVISE? */
4783 error = fget(td, fd, &cap_no_rights, &fp);
4786 AUDIT_ARG_FILE(td->td_proc, fp);
4787 if ((fp->f_ops->fo_flags & DFLAG_SEEKABLE) == 0) {
4791 if (fp->f_type != DTYPE_VNODE) {
4796 if (vp->v_type != VREG) {
4803 end = offset + len - 1;
4805 case POSIX_FADV_SEQUENTIAL:
4806 case POSIX_FADV_RANDOM:
4807 case POSIX_FADV_NOREUSE:
4809 * Try to merge any existing non-standard region with
4810 * this new region if possible, otherwise create a new
4811 * non-standard region for this request.
4813 mtx_pool_lock(mtxpool_sleep, fp);
4815 if (fa != NULL && fa->fa_advice == advice &&
4816 ((fa->fa_start <= end && fa->fa_end >= offset) ||
4817 (end != OFF_MAX && fa->fa_start == end + 1) ||
4818 (fa->fa_end != OFF_MAX && fa->fa_end + 1 == offset))) {
4819 if (offset < fa->fa_start)
4820 fa->fa_start = offset;
4821 if (end > fa->fa_end)
4824 new->fa_advice = advice;
4825 new->fa_start = offset;
4830 mtx_pool_unlock(mtxpool_sleep, fp);
4832 case POSIX_FADV_NORMAL:
4834 * If a the "normal" region overlaps with an existing
4835 * non-standard region, trim or remove the
4836 * non-standard region.
4838 mtx_pool_lock(mtxpool_sleep, fp);
4841 if (offset <= fa->fa_start && end >= fa->fa_end) {
4843 fp->f_advice = NULL;
4844 } else if (offset <= fa->fa_start &&
4845 end >= fa->fa_start)
4846 fa->fa_start = end + 1;
4847 else if (offset <= fa->fa_end && end >= fa->fa_end)
4848 fa->fa_end = offset - 1;
4849 else if (offset >= fa->fa_start && end <= fa->fa_end) {
4851 * If the "normal" region is a middle
4852 * portion of the existing
4853 * non-standard region, just remove
4854 * the whole thing rather than picking
4855 * one side or the other to
4859 fp->f_advice = NULL;
4862 mtx_pool_unlock(mtxpool_sleep, fp);
4864 case POSIX_FADV_WILLNEED:
4865 case POSIX_FADV_DONTNEED:
4866 error = VOP_ADVISE(vp, offset, end, advice);
4872 free(new, M_FADVISE);
4877 sys_posix_fadvise(struct thread *td, struct posix_fadvise_args *uap)
4881 error = kern_posix_fadvise(td, uap->fd, uap->offset, uap->len,
4883 return (kern_posix_error(td, error));
4887 kern_copy_file_range(struct thread *td, int infd, off_t *inoffp, int outfd,
4888 off_t *outoffp, size_t len, unsigned int flags)
4890 struct file *infp, *outfp;
4891 struct vnode *invp, *outvp;
4894 void *rl_rcookie, *rl_wcookie;
4895 off_t savinoff, savoutoff;
4897 infp = outfp = NULL;
4898 rl_rcookie = rl_wcookie = NULL;
4907 if (len > SSIZE_MAX)
4909 * Although the len argument is size_t, the return argument
4910 * is ssize_t (which is signed). Therefore a size that won't
4911 * fit in ssize_t can't be returned.
4915 /* Get the file structures for the file descriptors. */
4916 error = fget_read(td, infd, &cap_read_rights, &infp);
4919 if (infp->f_ops == &badfileops) {
4923 if (infp->f_vnode == NULL) {
4927 error = fget_write(td, outfd, &cap_write_rights, &outfp);
4930 if (outfp->f_ops == &badfileops) {
4934 if (outfp->f_vnode == NULL) {
4939 /* Set the offset pointers to the correct place. */
4941 inoffp = &infp->f_offset;
4942 if (outoffp == NULL)
4943 outoffp = &outfp->f_offset;
4945 savoutoff = *outoffp;
4947 invp = infp->f_vnode;
4948 outvp = outfp->f_vnode;
4949 /* Sanity check the f_flag bits. */
4950 if ((outfp->f_flag & (FWRITE | FAPPEND)) != FWRITE ||
4951 (infp->f_flag & FREAD) == 0) {
4956 /* If len == 0, just return 0. */
4961 * If infp and outfp refer to the same file, the byte ranges cannot
4964 if (invp == outvp && ((savinoff <= savoutoff && savinoff + len >
4965 savoutoff) || (savinoff > savoutoff && savoutoff + len >
4971 /* Range lock the byte ranges for both invp and outvp. */
4973 rl_wcookie = vn_rangelock_wlock(outvp, *outoffp, *outoffp +
4975 rl_rcookie = vn_rangelock_tryrlock(invp, *inoffp, *inoffp +
4977 if (rl_rcookie != NULL)
4979 vn_rangelock_unlock(outvp, rl_wcookie);
4980 rl_rcookie = vn_rangelock_rlock(invp, *inoffp, *inoffp + len);
4981 vn_rangelock_unlock(invp, rl_rcookie);
4985 error = vn_copy_file_range(invp, inoffp, outvp, outoffp, &retlen,
4986 flags, infp->f_cred, outfp->f_cred, td);
4988 if (rl_rcookie != NULL)
4989 vn_rangelock_unlock(invp, rl_rcookie);
4990 if (rl_wcookie != NULL)
4991 vn_rangelock_unlock(outvp, rl_wcookie);
4992 if (savinoff != -1 && (error == EINTR || error == ERESTART)) {
4994 *outoffp = savoutoff;
5000 td->td_retval[0] = retlen;
5005 sys_copy_file_range(struct thread *td, struct copy_file_range_args *uap)
5007 off_t inoff, outoff, *inoffp, *outoffp;
5010 inoffp = outoffp = NULL;
5011 if (uap->inoffp != NULL) {
5012 error = copyin(uap->inoffp, &inoff, sizeof(off_t));
5017 if (uap->outoffp != NULL) {
5018 error = copyin(uap->outoffp, &outoff, sizeof(off_t));
5023 error = kern_copy_file_range(td, uap->infd, inoffp, uap->outfd,
5024 outoffp, uap->len, uap->flags);
5025 if (error == 0 && uap->inoffp != NULL)
5026 error = copyout(inoffp, uap->inoffp, sizeof(off_t));
5027 if (error == 0 && uap->outoffp != NULL)
5028 error = copyout(outoffp, uap->outoffp, sizeof(off_t));