2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
6 * (c) UNIX System Laboratories, Inc.
7 * All or some portions of this file are derived from material licensed
8 * to the University of California by American Telephone and Telegraph
9 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10 * the permission of UNIX System Laboratories, Inc.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * @(#)vfs_syscalls.c 8.13 (Berkeley) 4/15/94
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
42 #include "opt_capsicum.h"
43 #include "opt_ktrace.h"
45 #include <sys/param.h>
46 #include <sys/systm.h>
49 #include <sys/capsicum.h>
51 #include <sys/sysent.h>
52 #include <sys/malloc.h>
53 #include <sys/mount.h>
54 #include <sys/mutex.h>
55 #include <sys/sysproto.h>
56 #include <sys/namei.h>
57 #include <sys/filedesc.h>
58 #include <sys/kernel.h>
59 #include <sys/fcntl.h>
61 #include <sys/filio.h>
62 #include <sys/limits.h>
63 #include <sys/linker.h>
64 #include <sys/rwlock.h>
68 #include <sys/unistd.h>
69 #include <sys/vnode.h>
72 #include <sys/dirent.h>
74 #include <sys/syscallsubr.h>
75 #include <sys/sysctl.h>
77 #include <sys/ktrace.h>
80 #include <machine/stdarg.h>
82 #include <security/audit/audit.h>
83 #include <security/mac/mac_framework.h>
86 #include <vm/vm_object.h>
87 #include <vm/vm_page.h>
90 #include <fs/devfs/devfs.h>
92 #include <ufs/ufs/quota.h>
94 MALLOC_DEFINE(M_FADVISE, "fadvise", "posix_fadvise(2) information");
96 static int kern_chflagsat(struct thread *td, int fd, const char *path,
97 enum uio_seg pathseg, u_long flags, int atflag);
98 static int setfflags(struct thread *td, struct vnode *, u_long);
99 static int getutimes(const struct timeval *, enum uio_seg, struct timespec *);
100 static int getutimens(const struct timespec *, enum uio_seg,
101 struct timespec *, int *);
102 static int setutimes(struct thread *td, struct vnode *,
103 const struct timespec *, int, int);
104 static int vn_access(struct vnode *vp, int user_flags, struct ucred *cred,
106 static int kern_fhlinkat(struct thread *td, int fd, const char *path,
107 enum uio_seg pathseg, fhandle_t *fhp);
108 static int kern_readlink_vp(struct vnode *vp, char *buf, enum uio_seg bufseg,
109 size_t count, struct thread *td);
110 static int kern_linkat_vp(struct thread *td, struct vnode *vp, int fd,
111 const char *path, enum uio_seg segflag);
114 at2cnpflags(u_int at_flags, u_int mask)
118 MPASS((at_flags & (AT_SYMLINK_FOLLOW | AT_SYMLINK_NOFOLLOW)) !=
119 (AT_SYMLINK_FOLLOW | AT_SYMLINK_NOFOLLOW));
123 if ((at_flags & AT_RESOLVE_BENEATH) != 0)
125 if ((at_flags & AT_SYMLINK_FOLLOW) != 0)
127 /* NOFOLLOW is pseudo flag */
128 if ((mask & AT_SYMLINK_NOFOLLOW) != 0) {
129 res |= (at_flags & AT_SYMLINK_NOFOLLOW) != 0 ? NOFOLLOW :
132 if ((mask & AT_EMPTY_PATH) != 0 && (at_flags & AT_EMPTY_PATH) != 0)
138 kern_sync(struct thread *td)
140 struct mount *mp, *nmp;
143 mtx_lock(&mountlist_mtx);
144 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
145 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK)) {
146 nmp = TAILQ_NEXT(mp, mnt_list);
149 if ((mp->mnt_flag & MNT_RDONLY) == 0 &&
150 vn_start_write(NULL, &mp, V_NOWAIT) == 0) {
151 save = curthread_pflags_set(TDP_SYNCIO);
152 vfs_periodic(mp, MNT_NOWAIT);
153 VFS_SYNC(mp, MNT_NOWAIT);
154 curthread_pflags_restore(save);
155 vn_finished_write(mp);
157 mtx_lock(&mountlist_mtx);
158 nmp = TAILQ_NEXT(mp, mnt_list);
161 mtx_unlock(&mountlist_mtx);
166 * Sync each mounted filesystem.
168 #ifndef _SYS_SYSPROTO_H_
175 sys_sync(struct thread *td, struct sync_args *uap)
178 return (kern_sync(td));
182 * Change filesystem quotas.
184 #ifndef _SYS_SYSPROTO_H_
185 struct quotactl_args {
193 sys_quotactl(struct thread *td, struct quotactl_args *uap)
199 AUDIT_ARG_CMD(uap->cmd);
200 AUDIT_ARG_UID(uap->uid);
201 if (!prison_allow(td->td_ucred, PR_ALLOW_QUOTAS))
203 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
205 if ((error = namei(&nd)) != 0)
207 NDFREE(&nd, NDF_ONLY_PNBUF);
208 mp = nd.ni_vp->v_mount;
211 error = vfs_busy(mp, 0);
216 error = VFS_QUOTACTL(mp, uap->cmd, uap->uid, uap->arg);
219 * Since quota on operation typically needs to open quota
220 * file, the Q_QUOTAON handler needs to unbusy the mount point
221 * before calling into namei. Otherwise, unmount might be
222 * started between two vfs_busy() invocations (first is our,
223 * second is from mount point cross-walk code in lookup()),
226 * Require that Q_QUOTAON handles the vfs_busy() reference on
227 * its own, always returning with ubusied mount point.
229 if ((uap->cmd >> SUBCMDSHIFT) != Q_QUOTAON &&
230 (uap->cmd >> SUBCMDSHIFT) != Q_QUOTAOFF)
237 * Used by statfs conversion routines to scale the block size up if
238 * necessary so that all of the block counts are <= 'max_size'. Note
239 * that 'max_size' should be a bitmask, i.e. 2^n - 1 for some non-zero
243 statfs_scale_blocks(struct statfs *sf, long max_size)
248 KASSERT(powerof2(max_size + 1), ("%s: invalid max_size", __func__));
251 * Attempt to scale the block counts to give a more accurate
252 * overview to userland of the ratio of free space to used
253 * space. To do this, find the largest block count and compute
254 * a divisor that lets it fit into a signed integer <= max_size.
256 if (sf->f_bavail < 0)
257 count = -sf->f_bavail;
259 count = sf->f_bavail;
260 count = MAX(sf->f_blocks, MAX(sf->f_bfree, count));
261 if (count <= max_size)
264 count >>= flsl(max_size);
271 sf->f_bsize <<= shift;
272 sf->f_blocks >>= shift;
273 sf->f_bfree >>= shift;
274 sf->f_bavail >>= shift;
278 kern_do_statfs(struct thread *td, struct mount *mp, struct statfs *buf)
284 error = vfs_busy(mp, 0);
289 error = mac_mount_check_stat(td->td_ucred, mp);
293 error = VFS_STATFS(mp, buf);
296 if (priv_check_cred_vfs_generation(td->td_ucred)) {
297 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
298 prison_enforce_statfs(td->td_ucred, mp, buf);
306 * Get filesystem statistics.
308 #ifndef _SYS_SYSPROTO_H_
315 sys_statfs(struct thread *td, struct statfs_args *uap)
320 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
321 error = kern_statfs(td, uap->path, UIO_USERSPACE, sfp);
323 error = copyout(sfp, uap->buf, sizeof(struct statfs));
329 kern_statfs(struct thread *td, const char *path, enum uio_seg pathseg,
336 NDINIT(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, td);
340 mp = vfs_ref_from_vp(nd.ni_vp);
343 return (kern_do_statfs(td, mp, buf));
347 * Get filesystem statistics.
349 #ifndef _SYS_SYSPROTO_H_
350 struct fstatfs_args {
356 sys_fstatfs(struct thread *td, struct fstatfs_args *uap)
361 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
362 error = kern_fstatfs(td, uap->fd, sfp);
364 error = copyout(sfp, uap->buf, sizeof(struct statfs));
370 kern_fstatfs(struct thread *td, int fd, struct statfs *buf)
378 error = getvnode_path(td, fd, &cap_fstatfs_rights, &fp);
383 if (AUDITING_TD(td)) {
384 vn_lock(vp, LK_SHARED | LK_RETRY);
385 AUDIT_ARG_VNODE1(vp);
389 mp = vfs_ref_from_vp(vp);
391 return (kern_do_statfs(td, mp, buf));
395 * Get statistics on all filesystems.
397 #ifndef _SYS_SYSPROTO_H_
398 struct getfsstat_args {
405 sys_getfsstat(struct thread *td, struct getfsstat_args *uap)
410 if (uap->bufsize < 0 || uap->bufsize > SIZE_MAX)
412 error = kern_getfsstat(td, &uap->buf, uap->bufsize, &count,
413 UIO_USERSPACE, uap->mode);
415 td->td_retval[0] = count;
420 * If (bufsize > 0 && bufseg == UIO_SYSSPACE)
421 * The caller is responsible for freeing memory which will be allocated
425 kern_getfsstat(struct thread *td, struct statfs **buf, size_t bufsize,
426 size_t *countp, enum uio_seg bufseg, int mode)
428 struct mount *mp, *nmp;
429 struct statfs *sfsp, *sp, *sptmp, *tofree;
430 size_t count, maxcount;
438 if (bufseg == UIO_SYSSPACE)
443 maxcount = bufsize / sizeof(struct statfs);
447 } else if (bufseg == UIO_USERSPACE) {
450 } else /* if (bufseg == UIO_SYSSPACE) */ {
452 mtx_lock(&mountlist_mtx);
453 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
456 mtx_unlock(&mountlist_mtx);
457 if (maxcount > count)
459 tofree = sfsp = *buf = malloc(maxcount * sizeof(struct statfs),
466 * If there is no target buffer they only want the count.
468 * This could be TAILQ_FOREACH but it is open-coded to match the original
472 mtx_lock(&mountlist_mtx);
473 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
474 if (prison_canseemount(td->td_ucred, mp) != 0) {
475 nmp = TAILQ_NEXT(mp, mnt_list);
479 if (mac_mount_check_stat(td->td_ucred, mp) != 0) {
480 nmp = TAILQ_NEXT(mp, mnt_list);
485 nmp = TAILQ_NEXT(mp, mnt_list);
487 mtx_unlock(&mountlist_mtx);
493 * They want the entire thing.
495 * Short-circuit the corner case of no room for anything, avoids
502 mtx_lock(&mountlist_mtx);
503 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
504 if (prison_canseemount(td->td_ucred, mp) != 0) {
505 nmp = TAILQ_NEXT(mp, mnt_list);
509 if (mac_mount_check_stat(td->td_ucred, mp) != 0) {
510 nmp = TAILQ_NEXT(mp, mnt_list);
514 if (mode == MNT_WAIT) {
515 if (vfs_busy(mp, MBF_MNTLSTLOCK) != 0) {
517 * If vfs_busy() failed, and MBF_NOWAIT
518 * wasn't passed, then the mp is gone.
519 * Furthermore, because of MBF_MNTLSTLOCK,
520 * the mountlist_mtx was dropped. We have
521 * no other choice than to start over.
523 mtx_unlock(&mountlist_mtx);
524 free(tofree, M_STATFS);
528 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK) != 0) {
529 nmp = TAILQ_NEXT(mp, mnt_list);
535 * If MNT_NOWAIT is specified, do not refresh
538 if (mode != MNT_NOWAIT) {
539 error = VFS_STATFS(mp, sp);
541 mtx_lock(&mountlist_mtx);
542 nmp = TAILQ_NEXT(mp, mnt_list);
547 if (priv_check_cred_vfs_generation(td->td_ucred)) {
548 sptmp = malloc(sizeof(struct statfs), M_STATFS,
551 sptmp->f_fsid.val[0] = sptmp->f_fsid.val[1] = 0;
552 prison_enforce_statfs(td->td_ucred, mp, sptmp);
556 if (bufseg == UIO_SYSSPACE) {
557 bcopy(sp, sfsp, sizeof(*sp));
558 free(sptmp, M_STATFS);
559 } else /* if (bufseg == UIO_USERSPACE) */ {
560 error = copyout(sp, sfsp, sizeof(*sp));
561 free(sptmp, M_STATFS);
570 if (count == maxcount) {
575 mtx_lock(&mountlist_mtx);
576 nmp = TAILQ_NEXT(mp, mnt_list);
579 mtx_unlock(&mountlist_mtx);
585 #ifdef COMPAT_FREEBSD4
587 * Get old format filesystem statistics.
589 static void freebsd4_cvtstatfs(struct statfs *, struct ostatfs *);
591 #ifndef _SYS_SYSPROTO_H_
592 struct freebsd4_statfs_args {
598 freebsd4_statfs(struct thread *td, struct freebsd4_statfs_args *uap)
604 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
605 error = kern_statfs(td, uap->path, UIO_USERSPACE, sfp);
607 freebsd4_cvtstatfs(sfp, &osb);
608 error = copyout(&osb, uap->buf, sizeof(osb));
615 * Get filesystem statistics.
617 #ifndef _SYS_SYSPROTO_H_
618 struct freebsd4_fstatfs_args {
624 freebsd4_fstatfs(struct thread *td, struct freebsd4_fstatfs_args *uap)
630 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
631 error = kern_fstatfs(td, uap->fd, sfp);
633 freebsd4_cvtstatfs(sfp, &osb);
634 error = copyout(&osb, uap->buf, sizeof(osb));
641 * Get statistics on all filesystems.
643 #ifndef _SYS_SYSPROTO_H_
644 struct freebsd4_getfsstat_args {
651 freebsd4_getfsstat(struct thread *td, struct freebsd4_getfsstat_args *uap)
653 struct statfs *buf, *sp;
658 if (uap->bufsize < 0)
660 count = uap->bufsize / sizeof(struct ostatfs);
661 if (count > SIZE_MAX / sizeof(struct statfs))
663 size = count * sizeof(struct statfs);
664 error = kern_getfsstat(td, &buf, size, &count, UIO_SYSSPACE,
667 td->td_retval[0] = count;
670 while (count != 0 && error == 0) {
671 freebsd4_cvtstatfs(sp, &osb);
672 error = copyout(&osb, uap->buf, sizeof(osb));
683 * Implement fstatfs() for (NFS) file handles.
685 #ifndef _SYS_SYSPROTO_H_
686 struct freebsd4_fhstatfs_args {
687 struct fhandle *u_fhp;
692 freebsd4_fhstatfs(struct thread *td, struct freebsd4_fhstatfs_args *uap)
699 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
702 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
703 error = kern_fhstatfs(td, fh, sfp);
705 freebsd4_cvtstatfs(sfp, &osb);
706 error = copyout(&osb, uap->buf, sizeof(osb));
713 * Convert a new format statfs structure to an old format statfs structure.
716 freebsd4_cvtstatfs(struct statfs *nsp, struct ostatfs *osp)
719 statfs_scale_blocks(nsp, LONG_MAX);
720 bzero(osp, sizeof(*osp));
721 osp->f_bsize = nsp->f_bsize;
722 osp->f_iosize = MIN(nsp->f_iosize, LONG_MAX);
723 osp->f_blocks = nsp->f_blocks;
724 osp->f_bfree = nsp->f_bfree;
725 osp->f_bavail = nsp->f_bavail;
726 osp->f_files = MIN(nsp->f_files, LONG_MAX);
727 osp->f_ffree = MIN(nsp->f_ffree, LONG_MAX);
728 osp->f_owner = nsp->f_owner;
729 osp->f_type = nsp->f_type;
730 osp->f_flags = nsp->f_flags;
731 osp->f_syncwrites = MIN(nsp->f_syncwrites, LONG_MAX);
732 osp->f_asyncwrites = MIN(nsp->f_asyncwrites, LONG_MAX);
733 osp->f_syncreads = MIN(nsp->f_syncreads, LONG_MAX);
734 osp->f_asyncreads = MIN(nsp->f_asyncreads, LONG_MAX);
735 strlcpy(osp->f_fstypename, nsp->f_fstypename,
736 MIN(MFSNAMELEN, OMFSNAMELEN));
737 strlcpy(osp->f_mntonname, nsp->f_mntonname,
738 MIN(MNAMELEN, OMNAMELEN));
739 strlcpy(osp->f_mntfromname, nsp->f_mntfromname,
740 MIN(MNAMELEN, OMNAMELEN));
741 osp->f_fsid = nsp->f_fsid;
743 #endif /* COMPAT_FREEBSD4 */
745 #if defined(COMPAT_FREEBSD11)
747 * Get old format filesystem statistics.
749 static void freebsd11_cvtstatfs(struct statfs *, struct freebsd11_statfs *);
752 freebsd11_statfs(struct thread *td, struct freebsd11_statfs_args *uap)
754 struct freebsd11_statfs osb;
758 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
759 error = kern_statfs(td, uap->path, UIO_USERSPACE, sfp);
761 freebsd11_cvtstatfs(sfp, &osb);
762 error = copyout(&osb, uap->buf, sizeof(osb));
769 * Get filesystem statistics.
772 freebsd11_fstatfs(struct thread *td, struct freebsd11_fstatfs_args *uap)
774 struct freebsd11_statfs osb;
778 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
779 error = kern_fstatfs(td, uap->fd, sfp);
781 freebsd11_cvtstatfs(sfp, &osb);
782 error = copyout(&osb, uap->buf, sizeof(osb));
789 * Get statistics on all filesystems.
792 freebsd11_getfsstat(struct thread *td, struct freebsd11_getfsstat_args *uap)
794 struct freebsd11_statfs osb;
795 struct statfs *buf, *sp;
799 count = uap->bufsize / sizeof(struct ostatfs);
800 size = count * sizeof(struct statfs);
801 error = kern_getfsstat(td, &buf, size, &count, UIO_SYSSPACE,
804 td->td_retval[0] = count;
807 while (count > 0 && error == 0) {
808 freebsd11_cvtstatfs(sp, &osb);
809 error = copyout(&osb, uap->buf, sizeof(osb));
820 * Implement fstatfs() for (NFS) file handles.
823 freebsd11_fhstatfs(struct thread *td, struct freebsd11_fhstatfs_args *uap)
825 struct freebsd11_statfs osb;
830 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
833 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
834 error = kern_fhstatfs(td, fh, sfp);
836 freebsd11_cvtstatfs(sfp, &osb);
837 error = copyout(&osb, uap->buf, sizeof(osb));
844 * Convert a new format statfs structure to an old format statfs structure.
847 freebsd11_cvtstatfs(struct statfs *nsp, struct freebsd11_statfs *osp)
850 bzero(osp, sizeof(*osp));
851 osp->f_version = FREEBSD11_STATFS_VERSION;
852 osp->f_type = nsp->f_type;
853 osp->f_flags = nsp->f_flags;
854 osp->f_bsize = nsp->f_bsize;
855 osp->f_iosize = nsp->f_iosize;
856 osp->f_blocks = nsp->f_blocks;
857 osp->f_bfree = nsp->f_bfree;
858 osp->f_bavail = nsp->f_bavail;
859 osp->f_files = nsp->f_files;
860 osp->f_ffree = nsp->f_ffree;
861 osp->f_syncwrites = nsp->f_syncwrites;
862 osp->f_asyncwrites = nsp->f_asyncwrites;
863 osp->f_syncreads = nsp->f_syncreads;
864 osp->f_asyncreads = nsp->f_asyncreads;
865 osp->f_namemax = nsp->f_namemax;
866 osp->f_owner = nsp->f_owner;
867 osp->f_fsid = nsp->f_fsid;
868 strlcpy(osp->f_fstypename, nsp->f_fstypename,
869 MIN(MFSNAMELEN, sizeof(osp->f_fstypename)));
870 strlcpy(osp->f_mntonname, nsp->f_mntonname,
871 MIN(MNAMELEN, sizeof(osp->f_mntonname)));
872 strlcpy(osp->f_mntfromname, nsp->f_mntfromname,
873 MIN(MNAMELEN, sizeof(osp->f_mntfromname)));
875 #endif /* COMPAT_FREEBSD11 */
878 * Change current working directory to a given file descriptor.
880 #ifndef _SYS_SYSPROTO_H_
886 sys_fchdir(struct thread *td, struct fchdir_args *uap)
888 struct vnode *vp, *tdp;
893 AUDIT_ARG_FD(uap->fd);
894 error = getvnode_path(td, uap->fd, &cap_fchdir_rights,
901 vn_lock(vp, LK_SHARED | LK_RETRY);
902 AUDIT_ARG_VNODE1(vp);
903 error = change_dir(vp, td);
904 while (!error && (mp = vp->v_mountedhere) != NULL) {
907 error = VFS_ROOT(mp, LK_SHARED, &tdp);
924 * Change current working directory (``.'').
926 #ifndef _SYS_SYSPROTO_H_
932 sys_chdir(struct thread *td, struct chdir_args *uap)
935 return (kern_chdir(td, uap->path, UIO_USERSPACE));
939 kern_chdir(struct thread *td, const char *path, enum uio_seg pathseg)
944 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
946 if ((error = namei(&nd)) != 0)
948 if ((error = change_dir(nd.ni_vp, td)) != 0) {
953 VOP_UNLOCK(nd.ni_vp);
955 pwd_chdir(td, nd.ni_vp);
959 static int unprivileged_chroot = 0;
960 SYSCTL_INT(_security_bsd, OID_AUTO, unprivileged_chroot, CTLFLAG_RW,
961 &unprivileged_chroot, 0,
962 "Unprivileged processes can use chroot(2)");
964 * Change notion of root (``/'') directory.
966 #ifndef _SYS_SYSPROTO_H_
972 sys_chroot(struct thread *td, struct chroot_args *uap)
978 error = priv_check(td, PRIV_VFS_CHROOT);
982 if (unprivileged_chroot == 0 ||
983 (p->p_flag2 & P2_NO_NEW_PRIVS) == 0) {
989 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
990 UIO_USERSPACE, uap->path, td);
994 error = change_dir(nd.ni_vp, td);
998 error = mac_vnode_check_chroot(td->td_ucred, nd.ni_vp);
1002 VOP_UNLOCK(nd.ni_vp);
1003 error = pwd_chroot(td, nd.ni_vp);
1005 NDFREE_NOTHING(&nd);
1010 NDFREE_NOTHING(&nd);
1015 * Common routine for chroot and chdir. Callers must provide a locked vnode
1019 change_dir(struct vnode *vp, struct thread *td)
1025 ASSERT_VOP_LOCKED(vp, "change_dir(): vp not locked");
1026 if (vp->v_type != VDIR)
1029 error = mac_vnode_check_chdir(td->td_ucred, vp);
1033 return (VOP_ACCESS(vp, VEXEC, td->td_ucred, td));
1036 static __inline void
1037 flags_to_rights(int flags, cap_rights_t *rightsp)
1039 if (flags & O_EXEC) {
1040 cap_rights_set_one(rightsp, CAP_FEXECVE);
1044 switch ((flags & O_ACCMODE)) {
1046 cap_rights_set_one(rightsp, CAP_READ);
1049 cap_rights_set_one(rightsp, CAP_READ);
1052 cap_rights_set_one(rightsp, CAP_WRITE);
1053 if (!(flags & (O_APPEND | O_TRUNC)))
1054 cap_rights_set_one(rightsp, CAP_SEEK);
1059 if (flags & O_CREAT)
1060 cap_rights_set_one(rightsp, CAP_CREATE);
1062 if (flags & O_TRUNC)
1063 cap_rights_set_one(rightsp, CAP_FTRUNCATE);
1065 if (flags & (O_SYNC | O_FSYNC))
1066 cap_rights_set_one(rightsp, CAP_FSYNC);
1068 if (flags & (O_EXLOCK | O_SHLOCK))
1069 cap_rights_set_one(rightsp, CAP_FLOCK);
1073 * Check permissions, allocate an open file structure, and call the device
1074 * open routine if any.
1076 #ifndef _SYS_SYSPROTO_H_
1084 sys_open(struct thread *td, struct open_args *uap)
1087 return (kern_openat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1088 uap->flags, uap->mode));
1091 #ifndef _SYS_SYSPROTO_H_
1092 struct openat_args {
1100 sys_openat(struct thread *td, struct openat_args *uap)
1103 AUDIT_ARG_FD(uap->fd);
1104 return (kern_openat(td, uap->fd, uap->path, UIO_USERSPACE, uap->flag,
1109 kern_openat(struct thread *td, int fd, const char *path, enum uio_seg pathseg,
1110 int flags, int mode)
1112 struct proc *p = td->td_proc;
1113 struct filedesc *fdp;
1114 struct pwddesc *pdp;
1117 struct nameidata nd;
1118 cap_rights_t rights;
1119 int cmode, error, indx;
1125 AUDIT_ARG_FFLAGS(flags);
1126 AUDIT_ARG_MODE(mode);
1127 cap_rights_init_one(&rights, CAP_LOOKUP);
1128 flags_to_rights(flags, &rights);
1131 * Only one of the O_EXEC, O_RDONLY, O_WRONLY and O_RDWR flags
1132 * may be specified. On the other hand, for O_PATH any mode
1133 * except O_EXEC is ignored.
1135 if ((flags & O_PATH) != 0) {
1136 flags &= ~(O_CREAT | O_ACCMODE);
1137 } else if ((flags & O_EXEC) != 0) {
1138 if (flags & O_ACCMODE)
1140 } else if ((flags & O_ACCMODE) == O_ACCMODE) {
1143 flags = FFLAGS(flags);
1147 * Allocate a file structure. The descriptor to reference it
1148 * is allocated and used by finstall_refed() below.
1150 error = falloc_noinstall(td, &fp);
1153 /* Set the flags early so the finit in devfs can pick them up. */
1154 fp->f_flag = flags & FMASK;
1155 cmode = ((mode & ~pdp->pd_cmask) & ALLPERMS) & ~S_ISTXT;
1156 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, fd,
1158 td->td_dupfd = -1; /* XXX check for fdopen */
1159 error = vn_open(&nd, &flags, cmode, fp);
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);
1213 finit_vnode(fp, flags, NULL, &vnops);
1218 if (flags & O_TRUNC) {
1219 error = fo_truncate(fp, 0, td->td_ucred, td);
1225 * If we haven't already installed the FD (for dupfdopen), do so now.
1228 struct filecaps *fcaps;
1231 if ((nd.ni_resflags & NIRES_STRICTREL) != 0)
1232 fcaps = &nd.ni_filecaps;
1236 error = finstall_refed(td, fp, &indx, flags, fcaps);
1237 /* On success finstall_refed() consumes fcaps. */
1239 filecaps_free(&nd.ni_filecaps);
1243 filecaps_free(&nd.ni_filecaps);
1244 falloc_abort(td, fp);
1247 td->td_retval[0] = indx;
1250 KASSERT(indx == -1, ("indx=%d, should be -1", indx));
1251 falloc_abort(td, fp);
1259 #ifndef _SYS_SYSPROTO_H_
1260 struct ocreat_args {
1266 ocreat(struct thread *td, struct ocreat_args *uap)
1269 return (kern_openat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1270 O_WRONLY | O_CREAT | O_TRUNC, uap->mode));
1272 #endif /* COMPAT_43 */
1275 * Create a special file.
1277 #ifndef _SYS_SYSPROTO_H_
1278 struct mknodat_args {
1286 sys_mknodat(struct thread *td, struct mknodat_args *uap)
1289 return (kern_mknodat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode,
1293 #if defined(COMPAT_FREEBSD11)
1295 freebsd11_mknod(struct thread *td,
1296 struct freebsd11_mknod_args *uap)
1299 return (kern_mknodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1300 uap->mode, uap->dev));
1304 freebsd11_mknodat(struct thread *td,
1305 struct freebsd11_mknodat_args *uap)
1308 return (kern_mknodat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode,
1311 #endif /* COMPAT_FREEBSD11 */
1314 kern_mknodat(struct thread *td, int fd, const char *path, enum uio_seg pathseg,
1315 int mode, dev_t dev)
1320 struct nameidata nd;
1321 int error, whiteout = 0;
1323 AUDIT_ARG_MODE(mode);
1325 switch (mode & S_IFMT) {
1328 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
1329 if (error == 0 && dev == VNOVAL)
1333 error = priv_check(td, PRIV_VFS_MKNOD_WHT);
1337 return (kern_mkfifoat(td, fd, path, pathseg, mode));
1348 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
1349 NOCACHE, pathseg, path, fd, &cap_mknodat_rights,
1351 if ((error = namei(&nd)) != 0)
1355 NDFREE(&nd, NDF_ONLY_PNBUF);
1356 if (vp == nd.ni_dvp)
1364 vattr.va_mode = (mode & ALLPERMS) &
1365 ~td->td_proc->p_pd->pd_cmask;
1366 vattr.va_rdev = dev;
1369 switch (mode & S_IFMT) {
1371 vattr.va_type = VCHR;
1374 vattr.va_type = VBLK;
1380 panic("kern_mknod: invalid mode");
1383 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1384 NDFREE(&nd, NDF_ONLY_PNBUF);
1386 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1391 if (error == 0 && !whiteout)
1392 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp,
1393 &nd.ni_cnd, &vattr);
1397 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, CREATE);
1399 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp,
1400 &nd.ni_cnd, &vattr);
1403 VOP_VPUT_PAIR(nd.ni_dvp, error == 0 && !whiteout ? &nd.ni_vp : NULL,
1405 vn_finished_write(mp);
1406 NDFREE(&nd, NDF_ONLY_PNBUF);
1407 if (error == ERELOOKUP)
1413 * Create a named pipe.
1415 #ifndef _SYS_SYSPROTO_H_
1416 struct mkfifo_args {
1422 sys_mkfifo(struct thread *td, struct mkfifo_args *uap)
1425 return (kern_mkfifoat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1429 #ifndef _SYS_SYSPROTO_H_
1430 struct mkfifoat_args {
1437 sys_mkfifoat(struct thread *td, struct mkfifoat_args *uap)
1440 return (kern_mkfifoat(td, uap->fd, uap->path, UIO_USERSPACE,
1445 kern_mkfifoat(struct thread *td, int fd, const char *path,
1446 enum uio_seg pathseg, int mode)
1450 struct nameidata nd;
1453 AUDIT_ARG_MODE(mode);
1457 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
1458 NOCACHE, pathseg, path, fd, &cap_mkfifoat_rights,
1460 if ((error = namei(&nd)) != 0)
1462 if (nd.ni_vp != NULL) {
1463 NDFREE(&nd, NDF_ONLY_PNBUF);
1464 if (nd.ni_vp == nd.ni_dvp)
1471 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1472 NDFREE(&nd, NDF_ONLY_PNBUF);
1474 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1479 vattr.va_type = VFIFO;
1480 vattr.va_mode = (mode & ALLPERMS) & ~td->td_proc->p_pd->pd_cmask;
1482 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1487 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
1491 VOP_VPUT_PAIR(nd.ni_dvp, error == 0 ? &nd.ni_vp : NULL, true);
1492 vn_finished_write(mp);
1493 NDFREE(&nd, NDF_ONLY_PNBUF);
1494 if (error == ERELOOKUP)
1500 * Make a hard file link.
1502 #ifndef _SYS_SYSPROTO_H_
1509 sys_link(struct thread *td, struct link_args *uap)
1512 return (kern_linkat(td, AT_FDCWD, AT_FDCWD, uap->path, uap->link,
1513 UIO_USERSPACE, FOLLOW));
1516 #ifndef _SYS_SYSPROTO_H_
1517 struct linkat_args {
1526 sys_linkat(struct thread *td, struct linkat_args *uap)
1531 if ((flag & ~(AT_SYMLINK_FOLLOW | AT_RESOLVE_BENEATH |
1532 AT_EMPTY_PATH)) != 0)
1535 return (kern_linkat(td, uap->fd1, uap->fd2, uap->path1, uap->path2,
1536 UIO_USERSPACE, at2cnpflags(flag, AT_SYMLINK_FOLLOW |
1537 AT_RESOLVE_BENEATH | AT_EMPTY_PATH)));
1540 int hardlink_check_uid = 0;
1541 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
1542 &hardlink_check_uid, 0,
1543 "Unprivileged processes cannot create hard links to files owned by other "
1545 static int hardlink_check_gid = 0;
1546 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
1547 &hardlink_check_gid, 0,
1548 "Unprivileged processes cannot create hard links to files owned by other "
1552 can_hardlink(struct vnode *vp, struct ucred *cred)
1557 if (!hardlink_check_uid && !hardlink_check_gid)
1560 error = VOP_GETATTR(vp, &va, cred);
1564 if (hardlink_check_uid && cred->cr_uid != va.va_uid) {
1565 error = priv_check_cred(cred, PRIV_VFS_LINK);
1570 if (hardlink_check_gid && !groupmember(va.va_gid, cred)) {
1571 error = priv_check_cred(cred, PRIV_VFS_LINK);
1580 kern_linkat(struct thread *td, int fd1, int fd2, const char *path1,
1581 const char *path2, enum uio_seg segflag, int follow)
1583 struct nameidata nd;
1589 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, segflag,
1590 path1, fd1, &cap_linkat_source_rights, td);
1591 if ((error = namei(&nd)) != 0)
1593 NDFREE(&nd, NDF_ONLY_PNBUF);
1594 if ((nd.ni_resflags & NIRES_EMPTYPATH) != 0) {
1595 error = priv_check(td, PRIV_VFS_FHOPEN);
1601 error = kern_linkat_vp(td, nd.ni_vp, fd2, path2, segflag);
1602 } while (error == EAGAIN || error == ERELOOKUP);
1607 kern_linkat_vp(struct thread *td, struct vnode *vp, int fd, const char *path,
1608 enum uio_seg segflag)
1610 struct nameidata nd;
1614 if (vp->v_type == VDIR) {
1616 return (EPERM); /* POSIX */
1618 NDINIT_ATRIGHTS(&nd, CREATE,
1619 LOCKPARENT | SAVENAME | AUDITVNODE2 | NOCACHE, segflag, path, fd,
1620 &cap_linkat_target_rights, td);
1621 if ((error = namei(&nd)) == 0) {
1622 if (nd.ni_vp != NULL) {
1623 NDFREE(&nd, NDF_ONLY_PNBUF);
1624 if (nd.ni_dvp == nd.ni_vp)
1631 } else if (nd.ni_dvp->v_mount != vp->v_mount) {
1633 * Cross-device link. No need to recheck
1634 * vp->v_type, since it cannot change, except
1637 NDFREE(&nd, NDF_ONLY_PNBUF);
1641 } else if ((error = vn_lock(vp, LK_EXCLUSIVE)) == 0) {
1642 error = can_hardlink(vp, td->td_ucred);
1645 error = mac_vnode_check_link(td->td_ucred,
1646 nd.ni_dvp, vp, &nd.ni_cnd);
1651 NDFREE(&nd, NDF_ONLY_PNBUF);
1654 error = vn_start_write(vp, &mp, V_NOWAIT);
1658 NDFREE(&nd, NDF_ONLY_PNBUF);
1659 error = vn_start_write(NULL, &mp,
1665 error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
1666 VOP_VPUT_PAIR(nd.ni_dvp, &vp, true);
1667 vn_finished_write(mp);
1668 NDFREE(&nd, NDF_ONLY_PNBUF);
1672 NDFREE(&nd, NDF_ONLY_PNBUF);
1683 * Make a symbolic link.
1685 #ifndef _SYS_SYSPROTO_H_
1686 struct symlink_args {
1692 sys_symlink(struct thread *td, struct symlink_args *uap)
1695 return (kern_symlinkat(td, uap->path, AT_FDCWD, uap->link,
1699 #ifndef _SYS_SYSPROTO_H_
1700 struct symlinkat_args {
1707 sys_symlinkat(struct thread *td, struct symlinkat_args *uap)
1710 return (kern_symlinkat(td, uap->path1, uap->fd, uap->path2,
1715 kern_symlinkat(struct thread *td, const char *path1, int fd, const char *path2,
1716 enum uio_seg segflg)
1720 const char *syspath;
1722 struct nameidata nd;
1725 if (segflg == UIO_SYSSPACE) {
1728 tmppath = uma_zalloc(namei_zone, M_WAITOK);
1729 if ((error = copyinstr(path1, tmppath, MAXPATHLEN, NULL)) != 0)
1733 AUDIT_ARG_TEXT(syspath);
1737 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
1738 NOCACHE, segflg, path2, fd, &cap_symlinkat_rights,
1740 if ((error = namei(&nd)) != 0)
1743 NDFREE(&nd, NDF_ONLY_PNBUF);
1744 if (nd.ni_vp == nd.ni_dvp)
1753 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1754 NDFREE(&nd, NDF_ONLY_PNBUF);
1756 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1761 vattr.va_mode = ACCESSPERMS &~ td->td_proc->p_pd->pd_cmask;
1763 vattr.va_type = VLNK;
1764 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1769 error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr, syspath);
1773 VOP_VPUT_PAIR(nd.ni_dvp, error == 0 ? &nd.ni_vp : NULL, true);
1774 vn_finished_write(mp);
1775 NDFREE(&nd, NDF_ONLY_PNBUF);
1776 if (error == ERELOOKUP)
1779 if (segflg != UIO_SYSSPACE)
1780 uma_zfree(namei_zone, tmppath);
1785 * Delete a whiteout from the filesystem.
1787 #ifndef _SYS_SYSPROTO_H_
1788 struct undelete_args {
1793 sys_undelete(struct thread *td, struct undelete_args *uap)
1796 struct nameidata nd;
1802 NDINIT(&nd, DELETE, LOCKPARENT | DOWHITEOUT | AUDITVNODE1,
1803 UIO_USERSPACE, uap->path, td);
1808 if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & ISWHITEOUT)) {
1809 NDFREE(&nd, NDF_ONLY_PNBUF);
1810 if (nd.ni_vp == nd.ni_dvp)
1818 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1819 NDFREE(&nd, NDF_ONLY_PNBUF);
1821 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1825 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, DELETE);
1826 NDFREE(&nd, NDF_ONLY_PNBUF);
1828 vn_finished_write(mp);
1829 if (error == ERELOOKUP)
1835 * Delete a name from the filesystem.
1837 #ifndef _SYS_SYSPROTO_H_
1838 struct unlink_args {
1843 sys_unlink(struct thread *td, struct unlink_args *uap)
1846 return (kern_funlinkat(td, AT_FDCWD, uap->path, FD_NONE, UIO_USERSPACE,
1851 kern_funlinkat_ex(struct thread *td, int dfd, const char *path, int fd,
1852 int flag, enum uio_seg pathseg, ino_t oldinum)
1855 if ((flag & ~(AT_REMOVEDIR | AT_RESOLVE_BENEATH)) != 0)
1858 if ((flag & AT_REMOVEDIR) != 0)
1859 return (kern_frmdirat(td, dfd, path, fd, UIO_USERSPACE, 0));
1861 return (kern_funlinkat(td, dfd, path, fd, UIO_USERSPACE, 0, 0));
1864 #ifndef _SYS_SYSPROTO_H_
1865 struct unlinkat_args {
1872 sys_unlinkat(struct thread *td, struct unlinkat_args *uap)
1875 return (kern_funlinkat_ex(td, uap->fd, uap->path, FD_NONE, uap->flag,
1879 #ifndef _SYS_SYSPROTO_H_
1880 struct funlinkat_args {
1888 sys_funlinkat(struct thread *td, struct funlinkat_args *uap)
1891 return (kern_funlinkat_ex(td, uap->dfd, uap->path, uap->fd, uap->flag,
1896 kern_funlinkat(struct thread *td, int dfd, const char *path, int fd,
1897 enum uio_seg pathseg, int flag, ino_t oldinum)
1902 struct nameidata nd;
1907 if (fd != FD_NONE) {
1908 error = getvnode_path(td, fd, &cap_no_rights, &fp);
1916 NDINIT_ATRIGHTS(&nd, DELETE, LOCKPARENT | LOCKLEAF | AUDITVNODE1 |
1917 at2cnpflags(flag, AT_RESOLVE_BENEATH),
1918 pathseg, path, dfd, &cap_unlinkat_rights, td);
1919 if ((error = namei(&nd)) != 0) {
1920 if (error == EINVAL)
1925 if (vp->v_type == VDIR && oldinum == 0) {
1926 error = EPERM; /* POSIX */
1927 } else if (oldinum != 0 &&
1928 ((error = VOP_STAT(vp, &sb, td->td_ucred, NOCRED, td)) == 0) &&
1929 sb.st_ino != oldinum) {
1930 error = EIDRM; /* Identifier removed */
1931 } else if (fp != NULL && fp->f_vnode != vp) {
1932 if (VN_IS_DOOMED(fp->f_vnode))
1938 * The root of a mounted filesystem cannot be deleted.
1940 * XXX: can this only be a VDIR case?
1942 if (vp->v_vflag & VV_ROOT)
1946 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1947 NDFREE(&nd, NDF_ONLY_PNBUF);
1949 if (vp == nd.ni_dvp)
1953 if ((error = vn_start_write(NULL, &mp,
1954 V_XSLEEP | PCATCH)) != 0) {
1960 error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp,
1965 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_UNLINK);
1966 error = VOP_REMOVE(nd.ni_dvp, vp, &nd.ni_cnd);
1970 vn_finished_write(mp);
1972 NDFREE(&nd, NDF_ONLY_PNBUF);
1974 if (vp == nd.ni_dvp)
1978 if (error == ERELOOKUP)
1987 * Reposition read/write file offset.
1989 #ifndef _SYS_SYSPROTO_H_
1998 sys_lseek(struct thread *td, struct lseek_args *uap)
2001 return (kern_lseek(td, uap->fd, uap->offset, uap->whence));
2005 kern_lseek(struct thread *td, int fd, off_t offset, int whence)
2011 error = fget(td, fd, &cap_seek_rights, &fp);
2014 error = (fp->f_ops->fo_flags & DFLAG_SEEKABLE) != 0 ?
2015 fo_seek(fp, offset, whence, td) : ESPIPE;
2020 #if defined(COMPAT_43)
2022 * Reposition read/write file offset.
2024 #ifndef _SYS_SYSPROTO_H_
2025 struct olseek_args {
2032 olseek(struct thread *td, struct olseek_args *uap)
2035 return (kern_lseek(td, uap->fd, uap->offset, uap->whence));
2037 #endif /* COMPAT_43 */
2039 #if defined(COMPAT_FREEBSD6)
2040 /* Version with the 'pad' argument */
2042 freebsd6_lseek(struct thread *td, struct freebsd6_lseek_args *uap)
2045 return (kern_lseek(td, uap->fd, uap->offset, uap->whence));
2050 * Check access permissions using passed credentials.
2053 vn_access(struct vnode *vp, int user_flags, struct ucred *cred,
2059 /* Flags == 0 means only check for existence. */
2060 if (user_flags == 0)
2064 if (user_flags & R_OK)
2066 if (user_flags & W_OK)
2068 if (user_flags & X_OK)
2071 error = mac_vnode_check_access(cred, vp, accmode);
2075 if ((accmode & VWRITE) == 0 || (error = vn_writechk(vp)) == 0)
2076 error = VOP_ACCESS(vp, accmode, cred, td);
2081 * Check access permissions using "real" credentials.
2083 #ifndef _SYS_SYSPROTO_H_
2084 struct access_args {
2090 sys_access(struct thread *td, struct access_args *uap)
2093 return (kern_accessat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2097 #ifndef _SYS_SYSPROTO_H_
2098 struct faccessat_args {
2106 sys_faccessat(struct thread *td, struct faccessat_args *uap)
2109 return (kern_accessat(td, uap->fd, uap->path, UIO_USERSPACE, uap->flag,
2114 kern_accessat(struct thread *td, int fd, const char *path,
2115 enum uio_seg pathseg, int flag, int amode)
2117 struct ucred *cred, *usecred;
2119 struct nameidata nd;
2122 if ((flag & ~(AT_EACCESS | AT_RESOLVE_BENEATH | AT_EMPTY_PATH)) != 0)
2124 if (amode != F_OK && (amode & ~(R_OK | W_OK | X_OK)) != 0)
2128 * Create and modify a temporary credential instead of one that
2129 * is potentially shared (if we need one).
2131 cred = td->td_ucred;
2132 if ((flag & AT_EACCESS) == 0 &&
2133 ((cred->cr_uid != cred->cr_ruid ||
2134 cred->cr_rgid != cred->cr_groups[0]))) {
2135 usecred = crdup(cred);
2136 usecred->cr_uid = cred->cr_ruid;
2137 usecred->cr_groups[0] = cred->cr_rgid;
2138 td->td_ucred = usecred;
2141 AUDIT_ARG_VALUE(amode);
2142 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF |
2143 AUDITVNODE1 | at2cnpflags(flag, AT_RESOLVE_BENEATH |
2144 AT_EMPTY_PATH), pathseg, path, fd, &cap_fstat_rights, td);
2145 if ((error = namei(&nd)) != 0)
2149 error = vn_access(vp, amode, usecred, td);
2150 NDFREE_NOTHING(&nd);
2153 if (usecred != cred) {
2154 td->td_ucred = cred;
2161 * Check access permissions using "effective" credentials.
2163 #ifndef _SYS_SYSPROTO_H_
2164 struct eaccess_args {
2170 sys_eaccess(struct thread *td, struct eaccess_args *uap)
2173 return (kern_accessat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2174 AT_EACCESS, uap->amode));
2177 #if defined(COMPAT_43)
2179 * Get file status; this version follows links.
2181 #ifndef _SYS_SYSPROTO_H_
2188 ostat(struct thread *td, struct ostat_args *uap)
2194 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
2199 return (copyout(&osb, uap->ub, sizeof (osb)));
2203 * Get file status; this version does not follow links.
2205 #ifndef _SYS_SYSPROTO_H_
2206 struct olstat_args {
2212 olstat(struct thread *td, struct olstat_args *uap)
2218 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2219 UIO_USERSPACE, &sb, NULL);
2223 return (copyout(&osb, uap->ub, sizeof (osb)));
2227 * Convert from an old to a new stat structure.
2228 * XXX: many values are blindly truncated.
2231 cvtstat(struct stat *st, struct ostat *ost)
2234 bzero(ost, sizeof(*ost));
2235 ost->st_dev = st->st_dev;
2236 ost->st_ino = st->st_ino;
2237 ost->st_mode = st->st_mode;
2238 ost->st_nlink = st->st_nlink;
2239 ost->st_uid = st->st_uid;
2240 ost->st_gid = st->st_gid;
2241 ost->st_rdev = st->st_rdev;
2242 ost->st_size = MIN(st->st_size, INT32_MAX);
2243 ost->st_atim = st->st_atim;
2244 ost->st_mtim = st->st_mtim;
2245 ost->st_ctim = st->st_ctim;
2246 ost->st_blksize = st->st_blksize;
2247 ost->st_blocks = st->st_blocks;
2248 ost->st_flags = st->st_flags;
2249 ost->st_gen = st->st_gen;
2251 #endif /* COMPAT_43 */
2253 #if defined(COMPAT_43) || defined(COMPAT_FREEBSD11)
2254 int ino64_trunc_error;
2255 SYSCTL_INT(_vfs, OID_AUTO, ino64_trunc_error, CTLFLAG_RW,
2256 &ino64_trunc_error, 0,
2257 "Error on truncation of device, file or inode number, or link count");
2260 freebsd11_cvtstat(struct stat *st, struct freebsd11_stat *ost)
2263 ost->st_dev = st->st_dev;
2264 if (ost->st_dev != st->st_dev) {
2265 switch (ino64_trunc_error) {
2268 * Since dev_t is almost raw, don't clamp to the
2269 * maximum for case 2, but ignore the error.
2276 ost->st_ino = st->st_ino;
2277 if (ost->st_ino != st->st_ino) {
2278 switch (ino64_trunc_error) {
2285 ost->st_ino = UINT32_MAX;
2289 ost->st_mode = st->st_mode;
2290 ost->st_nlink = st->st_nlink;
2291 if (ost->st_nlink != st->st_nlink) {
2292 switch (ino64_trunc_error) {
2299 ost->st_nlink = UINT16_MAX;
2303 ost->st_uid = st->st_uid;
2304 ost->st_gid = st->st_gid;
2305 ost->st_rdev = st->st_rdev;
2306 if (ost->st_rdev != st->st_rdev) {
2307 switch (ino64_trunc_error) {
2314 ost->st_atim = st->st_atim;
2315 ost->st_mtim = st->st_mtim;
2316 ost->st_ctim = st->st_ctim;
2317 ost->st_size = st->st_size;
2318 ost->st_blocks = st->st_blocks;
2319 ost->st_blksize = st->st_blksize;
2320 ost->st_flags = st->st_flags;
2321 ost->st_gen = st->st_gen;
2323 ost->st_birthtim = st->st_birthtim;
2324 bzero((char *)&ost->st_birthtim + sizeof(ost->st_birthtim),
2325 sizeof(*ost) - offsetof(struct freebsd11_stat,
2326 st_birthtim) - sizeof(ost->st_birthtim));
2331 freebsd11_stat(struct thread *td, struct freebsd11_stat_args* uap)
2334 struct freebsd11_stat osb;
2337 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
2341 error = freebsd11_cvtstat(&sb, &osb);
2343 error = copyout(&osb, uap->ub, sizeof(osb));
2348 freebsd11_lstat(struct thread *td, struct freebsd11_lstat_args* uap)
2351 struct freebsd11_stat osb;
2354 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2355 UIO_USERSPACE, &sb, NULL);
2358 error = freebsd11_cvtstat(&sb, &osb);
2360 error = copyout(&osb, uap->ub, sizeof(osb));
2365 freebsd11_fhstat(struct thread *td, struct freebsd11_fhstat_args* uap)
2369 struct freebsd11_stat osb;
2372 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
2375 error = kern_fhstat(td, fh, &sb);
2378 error = freebsd11_cvtstat(&sb, &osb);
2380 error = copyout(&osb, uap->sb, sizeof(osb));
2385 freebsd11_fstatat(struct thread *td, struct freebsd11_fstatat_args* uap)
2388 struct freebsd11_stat osb;
2391 error = kern_statat(td, uap->flag, uap->fd, uap->path,
2392 UIO_USERSPACE, &sb, NULL);
2395 error = freebsd11_cvtstat(&sb, &osb);
2397 error = copyout(&osb, uap->buf, sizeof(osb));
2400 #endif /* COMPAT_FREEBSD11 */
2405 #ifndef _SYS_SYSPROTO_H_
2406 struct fstatat_args {
2414 sys_fstatat(struct thread *td, struct fstatat_args *uap)
2419 error = kern_statat(td, uap->flag, uap->fd, uap->path,
2420 UIO_USERSPACE, &sb, NULL);
2422 error = copyout(&sb, uap->buf, sizeof (sb));
2427 kern_statat(struct thread *td, int flag, int fd, const char *path,
2428 enum uio_seg pathseg, struct stat *sbp,
2429 void (*hook)(struct vnode *vp, struct stat *sbp))
2431 struct nameidata nd;
2434 if ((flag & ~(AT_SYMLINK_NOFOLLOW | AT_RESOLVE_BENEATH |
2435 AT_EMPTY_PATH)) != 0)
2438 NDINIT_ATRIGHTS(&nd, LOOKUP, at2cnpflags(flag, AT_RESOLVE_BENEATH |
2439 AT_SYMLINK_NOFOLLOW | AT_EMPTY_PATH) | LOCKSHARED | LOCKLEAF |
2440 AUDITVNODE1, pathseg, path, fd, &cap_fstat_rights, td);
2442 if ((error = namei(&nd)) != 0) {
2443 if (error == ENOTDIR &&
2444 (nd.ni_resflags & NIRES_EMPTYPATH) != 0)
2445 error = kern_fstat(td, fd, sbp);
2448 error = VOP_STAT(nd.ni_vp, sbp, td->td_ucred, NOCRED, td);
2450 if (__predict_false(hook != NULL))
2451 hook(nd.ni_vp, sbp);
2453 NDFREE_NOTHING(&nd);
2455 #ifdef __STAT_TIME_T_EXT
2456 sbp->st_atim_ext = 0;
2457 sbp->st_mtim_ext = 0;
2458 sbp->st_ctim_ext = 0;
2459 sbp->st_btim_ext = 0;
2462 if (KTRPOINT(td, KTR_STRUCT))
2463 ktrstat_error(sbp, error);
2468 #if defined(COMPAT_FREEBSD11)
2470 * Implementation of the NetBSD [l]stat() functions.
2473 freebsd11_cvtnstat(struct stat *sb, struct nstat *nsb)
2476 bzero(nsb, sizeof(*nsb));
2477 nsb->st_dev = sb->st_dev;
2478 nsb->st_ino = sb->st_ino;
2479 nsb->st_mode = sb->st_mode;
2480 nsb->st_nlink = sb->st_nlink;
2481 nsb->st_uid = sb->st_uid;
2482 nsb->st_gid = sb->st_gid;
2483 nsb->st_rdev = sb->st_rdev;
2484 nsb->st_atim = sb->st_atim;
2485 nsb->st_mtim = sb->st_mtim;
2486 nsb->st_ctim = sb->st_ctim;
2487 nsb->st_size = sb->st_size;
2488 nsb->st_blocks = sb->st_blocks;
2489 nsb->st_blksize = sb->st_blksize;
2490 nsb->st_flags = sb->st_flags;
2491 nsb->st_gen = sb->st_gen;
2492 nsb->st_birthtim = sb->st_birthtim;
2495 #ifndef _SYS_SYSPROTO_H_
2496 struct freebsd11_nstat_args {
2502 freebsd11_nstat(struct thread *td, struct freebsd11_nstat_args *uap)
2508 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
2512 freebsd11_cvtnstat(&sb, &nsb);
2513 return (copyout(&nsb, uap->ub, sizeof (nsb)));
2517 * NetBSD lstat. Get file status; this version does not follow links.
2519 #ifndef _SYS_SYSPROTO_H_
2520 struct freebsd11_nlstat_args {
2526 freebsd11_nlstat(struct thread *td, struct freebsd11_nlstat_args *uap)
2532 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2533 UIO_USERSPACE, &sb, NULL);
2536 freebsd11_cvtnstat(&sb, &nsb);
2537 return (copyout(&nsb, uap->ub, sizeof (nsb)));
2539 #endif /* COMPAT_FREEBSD11 */
2542 * Get configurable pathname variables.
2544 #ifndef _SYS_SYSPROTO_H_
2545 struct pathconf_args {
2551 sys_pathconf(struct thread *td, struct pathconf_args *uap)
2556 error = kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name, FOLLOW,
2559 td->td_retval[0] = value;
2563 #ifndef _SYS_SYSPROTO_H_
2564 struct lpathconf_args {
2570 sys_lpathconf(struct thread *td, struct lpathconf_args *uap)
2575 error = kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name,
2578 td->td_retval[0] = value;
2583 kern_pathconf(struct thread *td, const char *path, enum uio_seg pathseg,
2584 int name, u_long flags, long *valuep)
2586 struct nameidata nd;
2589 NDINIT(&nd, LOOKUP, LOCKSHARED | LOCKLEAF | AUDITVNODE1 | flags,
2591 if ((error = namei(&nd)) != 0)
2593 NDFREE_NOTHING(&nd);
2595 error = VOP_PATHCONF(nd.ni_vp, name, valuep);
2601 * Return target name of a symbolic link.
2603 #ifndef _SYS_SYSPROTO_H_
2604 struct readlink_args {
2611 sys_readlink(struct thread *td, struct readlink_args *uap)
2614 return (kern_readlinkat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2615 uap->buf, UIO_USERSPACE, uap->count));
2617 #ifndef _SYS_SYSPROTO_H_
2618 struct readlinkat_args {
2626 sys_readlinkat(struct thread *td, struct readlinkat_args *uap)
2629 return (kern_readlinkat(td, uap->fd, uap->path, UIO_USERSPACE,
2630 uap->buf, UIO_USERSPACE, uap->bufsize));
2634 kern_readlinkat(struct thread *td, int fd, const char *path,
2635 enum uio_seg pathseg, char *buf, enum uio_seg bufseg, size_t count)
2638 struct nameidata nd;
2641 if (count > IOSIZE_MAX)
2644 NDINIT_AT(&nd, LOOKUP, NOFOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1 |
2645 EMPTYPATH, pathseg, path, fd, td);
2647 if ((error = namei(&nd)) != 0)
2649 NDFREE_NOTHING(&nd);
2652 error = kern_readlink_vp(vp, buf, bufseg, count, td);
2659 * Helper function to readlink from a vnode
2662 kern_readlink_vp(struct vnode *vp, char *buf, enum uio_seg bufseg, size_t count,
2669 ASSERT_VOP_LOCKED(vp, "kern_readlink_vp(): vp not locked");
2671 error = mac_vnode_check_readlink(td->td_ucred, vp);
2675 if (vp->v_type != VLNK && (vp->v_vflag & VV_READLINK) == 0)
2678 aiov.iov_base = buf;
2679 aiov.iov_len = count;
2680 auio.uio_iov = &aiov;
2681 auio.uio_iovcnt = 1;
2682 auio.uio_offset = 0;
2683 auio.uio_rw = UIO_READ;
2684 auio.uio_segflg = bufseg;
2686 auio.uio_resid = count;
2687 error = VOP_READLINK(vp, &auio, td->td_ucred);
2688 td->td_retval[0] = count - auio.uio_resid;
2693 * Common implementation code for chflags() and fchflags().
2696 setfflags(struct thread *td, struct vnode *vp, u_long flags)
2702 /* We can't support the value matching VNOVAL. */
2703 if (flags == VNOVAL)
2704 return (EOPNOTSUPP);
2707 * Prevent non-root users from setting flags on devices. When
2708 * a device is reused, users can retain ownership of the device
2709 * if they are allowed to set flags and programs assume that
2710 * chown can't fail when done as root.
2712 if (vp->v_type == VCHR || vp->v_type == VBLK) {
2713 error = priv_check(td, PRIV_VFS_CHFLAGS_DEV);
2718 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2721 vattr.va_flags = flags;
2722 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2724 error = mac_vnode_check_setflags(td->td_ucred, vp, vattr.va_flags);
2727 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
2729 vn_finished_write(mp);
2734 * Change flags of a file given a path name.
2736 #ifndef _SYS_SYSPROTO_H_
2737 struct chflags_args {
2743 sys_chflags(struct thread *td, struct chflags_args *uap)
2746 return (kern_chflagsat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2750 #ifndef _SYS_SYSPROTO_H_
2751 struct chflagsat_args {
2759 sys_chflagsat(struct thread *td, struct chflagsat_args *uap)
2762 if ((uap->atflag & ~(AT_SYMLINK_NOFOLLOW | AT_RESOLVE_BENEATH |
2763 AT_EMPTY_PATH)) != 0)
2766 return (kern_chflagsat(td, uap->fd, uap->path, UIO_USERSPACE,
2767 uap->flags, uap->atflag));
2771 * Same as chflags() but doesn't follow symlinks.
2773 #ifndef _SYS_SYSPROTO_H_
2774 struct lchflags_args {
2780 sys_lchflags(struct thread *td, struct lchflags_args *uap)
2783 return (kern_chflagsat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2784 uap->flags, AT_SYMLINK_NOFOLLOW));
2788 kern_chflagsat(struct thread *td, int fd, const char *path,
2789 enum uio_seg pathseg, u_long flags, int atflag)
2791 struct nameidata nd;
2794 AUDIT_ARG_FFLAGS(flags);
2795 NDINIT_ATRIGHTS(&nd, LOOKUP, at2cnpflags(atflag, AT_SYMLINK_NOFOLLOW |
2796 AT_RESOLVE_BENEATH | AT_EMPTY_PATH) | AUDITVNODE1, pathseg, path,
2797 fd, &cap_fchflags_rights, td);
2798 if ((error = namei(&nd)) != 0)
2800 NDFREE_NOTHING(&nd);
2801 error = setfflags(td, nd.ni_vp, flags);
2807 * Change flags of a file given a file descriptor.
2809 #ifndef _SYS_SYSPROTO_H_
2810 struct fchflags_args {
2816 sys_fchflags(struct thread *td, struct fchflags_args *uap)
2821 AUDIT_ARG_FD(uap->fd);
2822 AUDIT_ARG_FFLAGS(uap->flags);
2823 error = getvnode(td, uap->fd, &cap_fchflags_rights,
2828 if (AUDITING_TD(td)) {
2829 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
2830 AUDIT_ARG_VNODE1(fp->f_vnode);
2831 VOP_UNLOCK(fp->f_vnode);
2834 error = setfflags(td, fp->f_vnode, uap->flags);
2840 * Common implementation code for chmod(), lchmod() and fchmod().
2843 setfmode(struct thread *td, struct ucred *cred, struct vnode *vp, int mode)
2849 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2851 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2853 vattr.va_mode = mode & ALLPERMS;
2855 error = mac_vnode_check_setmode(cred, vp, vattr.va_mode);
2858 error = VOP_SETATTR(vp, &vattr, cred);
2860 vn_finished_write(mp);
2865 * Change mode of a file given path name.
2867 #ifndef _SYS_SYSPROTO_H_
2874 sys_chmod(struct thread *td, struct chmod_args *uap)
2877 return (kern_fchmodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2881 #ifndef _SYS_SYSPROTO_H_
2882 struct fchmodat_args {
2890 sys_fchmodat(struct thread *td, struct fchmodat_args *uap)
2893 if ((uap->flag & ~(AT_SYMLINK_NOFOLLOW | AT_RESOLVE_BENEATH |
2894 AT_EMPTY_PATH)) != 0)
2897 return (kern_fchmodat(td, uap->fd, uap->path, UIO_USERSPACE,
2898 uap->mode, uap->flag));
2902 * Change mode of a file given path name (don't follow links.)
2904 #ifndef _SYS_SYSPROTO_H_
2905 struct lchmod_args {
2911 sys_lchmod(struct thread *td, struct lchmod_args *uap)
2914 return (kern_fchmodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2915 uap->mode, AT_SYMLINK_NOFOLLOW));
2919 kern_fchmodat(struct thread *td, int fd, const char *path,
2920 enum uio_seg pathseg, mode_t mode, int flag)
2922 struct nameidata nd;
2925 AUDIT_ARG_MODE(mode);
2926 NDINIT_ATRIGHTS(&nd, LOOKUP, at2cnpflags(flag, AT_SYMLINK_NOFOLLOW |
2927 AT_RESOLVE_BENEATH | AT_EMPTY_PATH) | AUDITVNODE1, pathseg, path,
2928 fd, &cap_fchmod_rights, td);
2929 if ((error = namei(&nd)) != 0)
2931 NDFREE_NOTHING(&nd);
2932 error = setfmode(td, td->td_ucred, nd.ni_vp, mode);
2938 * Change mode of a file given a file descriptor.
2940 #ifndef _SYS_SYSPROTO_H_
2941 struct fchmod_args {
2947 sys_fchmod(struct thread *td, struct fchmod_args *uap)
2952 AUDIT_ARG_FD(uap->fd);
2953 AUDIT_ARG_MODE(uap->mode);
2955 error = fget(td, uap->fd, &cap_fchmod_rights, &fp);
2958 error = fo_chmod(fp, uap->mode, td->td_ucred, td);
2964 * Common implementation for chown(), lchown(), and fchown()
2967 setfown(struct thread *td, struct ucred *cred, struct vnode *vp, uid_t uid,
2974 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2976 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2981 error = mac_vnode_check_setowner(cred, vp, vattr.va_uid,
2985 error = VOP_SETATTR(vp, &vattr, cred);
2987 vn_finished_write(mp);
2992 * Set ownership given a path name.
2994 #ifndef _SYS_SYSPROTO_H_
3002 sys_chown(struct thread *td, struct chown_args *uap)
3005 return (kern_fchownat(td, AT_FDCWD, uap->path, UIO_USERSPACE, uap->uid,
3009 #ifndef _SYS_SYSPROTO_H_
3010 struct fchownat_args {
3019 sys_fchownat(struct thread *td, struct fchownat_args *uap)
3022 if ((uap->flag & ~(AT_SYMLINK_NOFOLLOW | AT_RESOLVE_BENEATH |
3023 AT_EMPTY_PATH)) != 0)
3026 return (kern_fchownat(td, uap->fd, uap->path, UIO_USERSPACE, uap->uid,
3027 uap->gid, uap->flag));
3031 kern_fchownat(struct thread *td, int fd, const char *path,
3032 enum uio_seg pathseg, int uid, int gid, int flag)
3034 struct nameidata nd;
3037 AUDIT_ARG_OWNER(uid, gid);
3038 NDINIT_ATRIGHTS(&nd, LOOKUP, at2cnpflags(flag, AT_SYMLINK_NOFOLLOW |
3039 AT_RESOLVE_BENEATH | AT_EMPTY_PATH) | AUDITVNODE1, pathseg, path,
3040 fd, &cap_fchown_rights, td);
3042 if ((error = namei(&nd)) != 0)
3044 NDFREE_NOTHING(&nd);
3045 error = setfown(td, td->td_ucred, nd.ni_vp, uid, gid);
3051 * Set ownership given a path name, do not cross symlinks.
3053 #ifndef _SYS_SYSPROTO_H_
3054 struct lchown_args {
3061 sys_lchown(struct thread *td, struct lchown_args *uap)
3064 return (kern_fchownat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
3065 uap->uid, uap->gid, AT_SYMLINK_NOFOLLOW));
3069 * Set ownership given a file descriptor.
3071 #ifndef _SYS_SYSPROTO_H_
3072 struct fchown_args {
3079 sys_fchown(struct thread *td, struct fchown_args *uap)
3084 AUDIT_ARG_FD(uap->fd);
3085 AUDIT_ARG_OWNER(uap->uid, uap->gid);
3086 error = fget(td, uap->fd, &cap_fchown_rights, &fp);
3089 error = fo_chown(fp, uap->uid, uap->gid, td->td_ucred, td);
3095 * Common implementation code for utimes(), lutimes(), and futimes().
3098 getutimes(const struct timeval *usrtvp, enum uio_seg tvpseg,
3099 struct timespec *tsp)
3101 struct timeval tv[2];
3102 const struct timeval *tvp;
3105 if (usrtvp == NULL) {
3106 vfs_timestamp(&tsp[0]);
3109 if (tvpseg == UIO_SYSSPACE) {
3112 if ((error = copyin(usrtvp, tv, sizeof(tv))) != 0)
3117 if (tvp[0].tv_usec < 0 || tvp[0].tv_usec >= 1000000 ||
3118 tvp[1].tv_usec < 0 || tvp[1].tv_usec >= 1000000)
3120 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
3121 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
3127 * Common implementation code for futimens(), utimensat().
3129 #define UTIMENS_NULL 0x1
3130 #define UTIMENS_EXIT 0x2
3132 getutimens(const struct timespec *usrtsp, enum uio_seg tspseg,
3133 struct timespec *tsp, int *retflags)
3135 struct timespec tsnow;
3138 vfs_timestamp(&tsnow);
3140 if (usrtsp == NULL) {
3143 *retflags |= UTIMENS_NULL;
3146 if (tspseg == UIO_SYSSPACE) {
3149 } else if ((error = copyin(usrtsp, tsp, sizeof(*tsp) * 2)) != 0)
3151 if (tsp[0].tv_nsec == UTIME_OMIT && tsp[1].tv_nsec == UTIME_OMIT)
3152 *retflags |= UTIMENS_EXIT;
3153 if (tsp[0].tv_nsec == UTIME_NOW && tsp[1].tv_nsec == UTIME_NOW)
3154 *retflags |= UTIMENS_NULL;
3155 if (tsp[0].tv_nsec == UTIME_OMIT)
3156 tsp[0].tv_sec = VNOVAL;
3157 else if (tsp[0].tv_nsec == UTIME_NOW)
3159 else if (tsp[0].tv_nsec < 0 || tsp[0].tv_nsec >= 1000000000L)
3161 if (tsp[1].tv_nsec == UTIME_OMIT)
3162 tsp[1].tv_sec = VNOVAL;
3163 else if (tsp[1].tv_nsec == UTIME_NOW)
3165 else if (tsp[1].tv_nsec < 0 || tsp[1].tv_nsec >= 1000000000L)
3172 * Common implementation code for utimes(), lutimes(), futimes(), futimens(),
3176 setutimes(struct thread *td, struct vnode *vp, const struct timespec *ts,
3177 int numtimes, int nullflag)
3184 setbirthtime = false;
3185 vattr.va_birthtime.tv_sec = VNOVAL;
3186 vattr.va_birthtime.tv_nsec = 0;
3188 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
3190 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3191 if (numtimes < 3 && VOP_GETATTR(vp, &vattr, td->td_ucred) == 0 &&
3192 timespeccmp(&ts[1], &vattr.va_birthtime, < ))
3193 setbirthtime = true;
3195 vattr.va_atime = ts[0];
3196 vattr.va_mtime = ts[1];
3198 vattr.va_birthtime = ts[1];
3200 vattr.va_birthtime = ts[2];
3202 vattr.va_vaflags |= VA_UTIMES_NULL;
3204 error = mac_vnode_check_setutimes(td->td_ucred, vp, vattr.va_atime,
3208 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3210 vn_finished_write(mp);
3215 * Set the access and modification times of a file.
3217 #ifndef _SYS_SYSPROTO_H_
3218 struct utimes_args {
3220 struct timeval *tptr;
3224 sys_utimes(struct thread *td, struct utimes_args *uap)
3227 return (kern_utimesat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
3228 uap->tptr, UIO_USERSPACE));
3231 #ifndef _SYS_SYSPROTO_H_
3232 struct futimesat_args {
3235 const struct timeval * times;
3239 sys_futimesat(struct thread *td, struct futimesat_args *uap)
3242 return (kern_utimesat(td, uap->fd, uap->path, UIO_USERSPACE,
3243 uap->times, UIO_USERSPACE));
3247 kern_utimesat(struct thread *td, int fd, const char *path,
3248 enum uio_seg pathseg, struct timeval *tptr, enum uio_seg tptrseg)
3250 struct nameidata nd;
3251 struct timespec ts[2];
3254 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3256 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, fd,
3257 &cap_futimes_rights, td);
3259 if ((error = namei(&nd)) != 0)
3261 NDFREE_NOTHING(&nd);
3262 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3268 * Set the access and modification times of a file.
3270 #ifndef _SYS_SYSPROTO_H_
3271 struct lutimes_args {
3273 struct timeval *tptr;
3277 sys_lutimes(struct thread *td, struct lutimes_args *uap)
3280 return (kern_lutimes(td, uap->path, UIO_USERSPACE, uap->tptr,
3285 kern_lutimes(struct thread *td, const char *path, enum uio_seg pathseg,
3286 struct timeval *tptr, enum uio_seg tptrseg)
3288 struct timespec ts[2];
3289 struct nameidata nd;
3292 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3294 NDINIT(&nd, LOOKUP, NOFOLLOW | AUDITVNODE1, pathseg, path, td);
3295 if ((error = namei(&nd)) != 0)
3297 NDFREE_NOTHING(&nd);
3298 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3304 * Set the access and modification times of a file.
3306 #ifndef _SYS_SYSPROTO_H_
3307 struct futimes_args {
3309 struct timeval *tptr;
3313 sys_futimes(struct thread *td, struct futimes_args *uap)
3316 return (kern_futimes(td, uap->fd, uap->tptr, UIO_USERSPACE));
3320 kern_futimes(struct thread *td, int fd, struct timeval *tptr,
3321 enum uio_seg tptrseg)
3323 struct timespec ts[2];
3328 error = getutimes(tptr, tptrseg, ts);
3331 error = getvnode(td, fd, &cap_futimes_rights, &fp);
3335 if (AUDITING_TD(td)) {
3336 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
3337 AUDIT_ARG_VNODE1(fp->f_vnode);
3338 VOP_UNLOCK(fp->f_vnode);
3341 error = setutimes(td, fp->f_vnode, ts, 2, tptr == NULL);
3347 sys_futimens(struct thread *td, struct futimens_args *uap)
3350 return (kern_futimens(td, uap->fd, uap->times, UIO_USERSPACE));
3354 kern_futimens(struct thread *td, int fd, struct timespec *tptr,
3355 enum uio_seg tptrseg)
3357 struct timespec ts[2];
3362 error = getutimens(tptr, tptrseg, ts, &flags);
3365 if (flags & UTIMENS_EXIT)
3367 error = getvnode(td, fd, &cap_futimes_rights, &fp);
3371 if (AUDITING_TD(td)) {
3372 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
3373 AUDIT_ARG_VNODE1(fp->f_vnode);
3374 VOP_UNLOCK(fp->f_vnode);
3377 error = setutimes(td, fp->f_vnode, ts, 2, flags & UTIMENS_NULL);
3383 sys_utimensat(struct thread *td, struct utimensat_args *uap)
3386 return (kern_utimensat(td, uap->fd, uap->path, UIO_USERSPACE,
3387 uap->times, UIO_USERSPACE, uap->flag));
3391 kern_utimensat(struct thread *td, int fd, const char *path,
3392 enum uio_seg pathseg, struct timespec *tptr, enum uio_seg tptrseg,
3395 struct nameidata nd;
3396 struct timespec ts[2];
3399 if ((flag & ~(AT_SYMLINK_NOFOLLOW | AT_RESOLVE_BENEATH |
3400 AT_EMPTY_PATH)) != 0)
3403 if ((error = getutimens(tptr, tptrseg, ts, &flags)) != 0)
3405 NDINIT_ATRIGHTS(&nd, LOOKUP, at2cnpflags(flag, AT_SYMLINK_NOFOLLOW |
3406 AT_RESOLVE_BENEATH | AT_EMPTY_PATH) | AUDITVNODE1,
3407 pathseg, path, fd, &cap_futimes_rights, td);
3408 if ((error = namei(&nd)) != 0)
3411 * We are allowed to call namei() regardless of 2xUTIME_OMIT.
3413 * "If both tv_nsec fields are UTIME_OMIT... EACCESS may be detected."
3414 * "Search permission is denied by a component of the path prefix."
3416 NDFREE_NOTHING(&nd);
3417 if ((flags & UTIMENS_EXIT) == 0)
3418 error = setutimes(td, nd.ni_vp, ts, 2, flags & UTIMENS_NULL);
3424 * Truncate a file given its path name.
3426 #ifndef _SYS_SYSPROTO_H_
3427 struct truncate_args {
3434 sys_truncate(struct thread *td, struct truncate_args *uap)
3437 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3441 kern_truncate(struct thread *td, const char *path, enum uio_seg pathseg,
3448 struct nameidata nd;
3455 NDINIT(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, td);
3456 if ((error = namei(&nd)) != 0)
3459 rl_cookie = vn_rangelock_wlock(vp, 0, OFF_MAX);
3460 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0) {
3461 vn_rangelock_unlock(vp, rl_cookie);
3465 NDFREE(&nd, NDF_ONLY_PNBUF);
3466 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3467 if (vp->v_type == VDIR)
3470 else if ((error = mac_vnode_check_write(td->td_ucred, NOCRED, vp))) {
3473 else if ((error = vn_writechk(vp)) == 0 &&
3474 (error = VOP_ACCESS(vp, VWRITE, td->td_ucred, td)) == 0) {
3476 vattr.va_size = length;
3477 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3480 vn_finished_write(mp);
3481 vn_rangelock_unlock(vp, rl_cookie);
3483 if (error == ERELOOKUP)
3488 #if defined(COMPAT_43)
3490 * Truncate a file given its path name.
3492 #ifndef _SYS_SYSPROTO_H_
3493 struct otruncate_args {
3499 otruncate(struct thread *td, struct otruncate_args *uap)
3502 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3504 #endif /* COMPAT_43 */
3506 #if defined(COMPAT_FREEBSD6)
3507 /* Versions with the pad argument */
3509 freebsd6_truncate(struct thread *td, struct freebsd6_truncate_args *uap)
3512 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3516 freebsd6_ftruncate(struct thread *td, struct freebsd6_ftruncate_args *uap)
3519 return (kern_ftruncate(td, uap->fd, uap->length));
3524 kern_fsync(struct thread *td, int fd, bool fullsync)
3532 error = getvnode(td, fd, &cap_fsync_rights, &fp);
3538 /* XXXKIB: compete outstanding aio writes */;
3541 error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
3544 vn_lock(vp, vn_lktype_write(mp, vp) | LK_RETRY);
3545 AUDIT_ARG_VNODE1(vp);
3546 if (vp->v_object != NULL) {
3547 VM_OBJECT_WLOCK(vp->v_object);
3548 vm_object_page_clean(vp->v_object, 0, 0, 0);
3549 VM_OBJECT_WUNLOCK(vp->v_object);
3551 error = fullsync ? VOP_FSYNC(vp, MNT_WAIT, td) : VOP_FDATASYNC(vp, td);
3553 vn_finished_write(mp);
3554 if (error == ERELOOKUP)
3562 * Sync an open file.
3564 #ifndef _SYS_SYSPROTO_H_
3570 sys_fsync(struct thread *td, struct fsync_args *uap)
3573 return (kern_fsync(td, uap->fd, true));
3577 sys_fdatasync(struct thread *td, struct fdatasync_args *uap)
3580 return (kern_fsync(td, uap->fd, false));
3584 * Rename files. Source and destination must either both be directories, or
3585 * both not be directories. If target is a directory, it must be empty.
3587 #ifndef _SYS_SYSPROTO_H_
3588 struct rename_args {
3594 sys_rename(struct thread *td, struct rename_args *uap)
3597 return (kern_renameat(td, AT_FDCWD, uap->from, AT_FDCWD,
3598 uap->to, UIO_USERSPACE));
3601 #ifndef _SYS_SYSPROTO_H_
3602 struct renameat_args {
3610 sys_renameat(struct thread *td, struct renameat_args *uap)
3613 return (kern_renameat(td, uap->oldfd, uap->old, uap->newfd, uap->new,
3619 kern_renameat_mac(struct thread *td, int oldfd, const char *old, int newfd,
3620 const char *new, enum uio_seg pathseg, struct nameidata *fromnd)
3624 NDINIT_ATRIGHTS(fromnd, DELETE, LOCKPARENT | LOCKLEAF | SAVESTART |
3625 AUDITVNODE1, pathseg, old, oldfd, &cap_renameat_source_rights, td);
3626 if ((error = namei(fromnd)) != 0)
3628 error = mac_vnode_check_rename_from(td->td_ucred, fromnd->ni_dvp,
3629 fromnd->ni_vp, &fromnd->ni_cnd);
3630 VOP_UNLOCK(fromnd->ni_dvp);
3631 if (fromnd->ni_dvp != fromnd->ni_vp)
3632 VOP_UNLOCK(fromnd->ni_vp);
3634 NDFREE(fromnd, NDF_ONLY_PNBUF);
3635 vrele(fromnd->ni_dvp);
3636 vrele(fromnd->ni_vp);
3637 if (fromnd->ni_startdir)
3638 vrele(fromnd->ni_startdir);
3645 kern_renameat(struct thread *td, int oldfd, const char *old, int newfd,
3646 const char *new, enum uio_seg pathseg)
3648 struct mount *mp = NULL;
3649 struct vnode *tvp, *fvp, *tdvp;
3650 struct nameidata fromnd, tond;
3651 u_int64_t tondflags;
3657 if (mac_vnode_check_rename_from_enabled()) {
3658 error = kern_renameat_mac(td, oldfd, old, newfd, new, pathseg,
3664 NDINIT_ATRIGHTS(&fromnd, DELETE, WANTPARENT | SAVESTART | AUDITVNODE1,
3665 pathseg, old, oldfd, &cap_renameat_source_rights, td);
3666 if ((error = namei(&fromnd)) != 0)
3672 tondflags = LOCKPARENT | LOCKLEAF | NOCACHE | SAVESTART | AUDITVNODE2;
3673 if (fromnd.ni_vp->v_type == VDIR)
3674 tondflags |= WILLBEDIR;
3675 NDINIT_ATRIGHTS(&tond, RENAME, tondflags, pathseg, new, newfd,
3676 &cap_renameat_target_rights, td);
3677 if ((error = namei(&tond)) != 0) {
3678 /* Translate error code for rename("dir1", "dir2/."). */
3679 if (error == EISDIR && fvp->v_type == VDIR)
3681 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3682 vrele(fromnd.ni_dvp);
3688 error = vn_start_write(fvp, &mp, V_NOWAIT);
3690 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3691 NDFREE(&tond, NDF_ONLY_PNBUF);
3698 vrele(fromnd.ni_dvp);
3700 vrele(tond.ni_startdir);
3701 if (fromnd.ni_startdir != NULL)
3702 vrele(fromnd.ni_startdir);
3703 error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH);
3709 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
3712 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
3717 if (newfd != AT_FDCWD && (tond.ni_resflags & NIRES_ABS) == 0) {
3719 * If the target already exists we require CAP_UNLINKAT
3720 * from 'newfd', when newfd was used for the lookup.
3722 error = cap_check(&tond.ni_filecaps.fc_rights,
3723 &cap_unlinkat_rights);
3734 * If the source is the same as the destination (that is, if they
3735 * are links to the same vnode), then there is nothing to do.
3741 error = mac_vnode_check_rename_to(td->td_ucred, tdvp,
3742 tond.ni_vp, fromnd.ni_dvp == tdvp, &tond.ni_cnd);
3746 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
3747 tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
3748 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3749 NDFREE(&tond, NDF_ONLY_PNBUF);
3751 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3752 NDFREE(&tond, NDF_ONLY_PNBUF);
3759 vrele(fromnd.ni_dvp);
3762 vrele(tond.ni_startdir);
3763 vn_finished_write(mp);
3765 if (fromnd.ni_startdir)
3766 vrele(fromnd.ni_startdir);
3767 if (error == ERESTART)
3769 if (error == ERELOOKUP)
3775 * Make a directory file.
3777 #ifndef _SYS_SYSPROTO_H_
3784 sys_mkdir(struct thread *td, struct mkdir_args *uap)
3787 return (kern_mkdirat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
3791 #ifndef _SYS_SYSPROTO_H_
3792 struct mkdirat_args {
3799 sys_mkdirat(struct thread *td, struct mkdirat_args *uap)
3802 return (kern_mkdirat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode));
3806 kern_mkdirat(struct thread *td, int fd, const char *path, enum uio_seg segflg,
3811 struct nameidata nd;
3814 AUDIT_ARG_MODE(mode);
3818 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
3819 NC_NOMAKEENTRY | NC_KEEPPOSENTRY | FAILIFEXISTS | WILLBEDIR,
3820 segflg, path, fd, &cap_mkdirat_rights, td);
3821 if ((error = namei(&nd)) != 0)
3823 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3824 NDFREE(&nd, NDF_ONLY_PNBUF);
3826 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3831 vattr.va_type = VDIR;
3832 vattr.va_mode = (mode & ACCESSPERMS) &~ td->td_proc->p_pd->pd_cmask;
3834 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
3839 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
3843 NDFREE(&nd, NDF_ONLY_PNBUF);
3844 VOP_VPUT_PAIR(nd.ni_dvp, error == 0 ? &nd.ni_vp : NULL, true);
3845 vn_finished_write(mp);
3846 if (error == ERELOOKUP)
3852 * Remove a directory file.
3854 #ifndef _SYS_SYSPROTO_H_
3860 sys_rmdir(struct thread *td, struct rmdir_args *uap)
3863 return (kern_frmdirat(td, AT_FDCWD, uap->path, FD_NONE, UIO_USERSPACE,
3868 kern_frmdirat(struct thread *td, int dfd, const char *path, int fd,
3869 enum uio_seg pathseg, int flag)
3874 struct nameidata nd;
3875 cap_rights_t rights;
3879 if (fd != FD_NONE) {
3880 error = getvnode(td, fd, cap_rights_init_one(&rights,
3889 NDINIT_ATRIGHTS(&nd, DELETE, LOCKPARENT | LOCKLEAF | AUDITVNODE1 |
3890 at2cnpflags(flag, AT_RESOLVE_BENEATH),
3891 pathseg, path, dfd, &cap_unlinkat_rights, td);
3892 if ((error = namei(&nd)) != 0)
3895 if (vp->v_type != VDIR) {
3900 * No rmdir "." please.
3902 if (nd.ni_dvp == vp) {
3907 * The root of a mounted filesystem cannot be deleted.
3909 if (vp->v_vflag & VV_ROOT) {
3914 if (fp != NULL && fp->f_vnode != vp) {
3915 if (VN_IS_DOOMED(fp->f_vnode))
3923 error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp,
3928 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3929 NDFREE(&nd, NDF_ONLY_PNBUF);
3931 if (nd.ni_dvp == vp)
3935 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3939 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_UNLINK);
3940 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
3941 vn_finished_write(mp);
3943 NDFREE(&nd, NDF_ONLY_PNBUF);
3945 if (nd.ni_dvp == vp)
3949 if (error == ERELOOKUP)
3957 #if defined(COMPAT_43) || defined(COMPAT_FREEBSD11)
3959 freebsd11_kern_getdirentries(struct thread *td, int fd, char *ubuf, u_int count,
3960 long *basep, void (*func)(struct freebsd11_dirent *))
3962 struct freebsd11_dirent dstdp;
3963 struct dirent *dp, *edp;
3966 ssize_t resid, ucount;
3969 /* XXX arbitrary sanity limit on `count'. */
3970 count = min(count, 64 * 1024);
3972 dirbuf = malloc(count, M_TEMP, M_WAITOK);
3974 error = kern_getdirentries(td, fd, dirbuf, count, &base, &resid,
3982 for (dp = (struct dirent *)dirbuf,
3983 edp = (struct dirent *)&dirbuf[count - resid];
3984 ucount < count && dp < edp; ) {
3985 if (dp->d_reclen == 0)
3987 MPASS(dp->d_reclen >= _GENERIC_DIRLEN(0));
3988 if (dp->d_namlen >= sizeof(dstdp.d_name))
3990 dstdp.d_type = dp->d_type;
3991 dstdp.d_namlen = dp->d_namlen;
3992 dstdp.d_fileno = dp->d_fileno; /* truncate */
3993 if (dstdp.d_fileno != dp->d_fileno) {
3994 switch (ino64_trunc_error) {
4002 dstdp.d_fileno = UINT32_MAX;
4006 dstdp.d_reclen = sizeof(dstdp) - sizeof(dstdp.d_name) +
4007 ((dp->d_namlen + 1 + 3) &~ 3);
4008 bcopy(dp->d_name, dstdp.d_name, dstdp.d_namlen);
4009 bzero(dstdp.d_name + dstdp.d_namlen,
4010 dstdp.d_reclen - offsetof(struct freebsd11_dirent, d_name) -
4012 MPASS(dstdp.d_reclen <= dp->d_reclen);
4013 MPASS(ucount + dstdp.d_reclen <= count);
4016 error = copyout(&dstdp, ubuf + ucount, dstdp.d_reclen);
4019 dp = (struct dirent *)((char *)dp + dp->d_reclen);
4020 ucount += dstdp.d_reclen;
4024 free(dirbuf, M_TEMP);
4026 td->td_retval[0] = ucount;
4033 ogetdirentries_cvt(struct freebsd11_dirent *dp)
4035 #if (BYTE_ORDER == LITTLE_ENDIAN)
4037 * The expected low byte of dp->d_namlen is our dp->d_type.
4038 * The high MBZ byte of dp->d_namlen is our dp->d_namlen.
4040 dp->d_type = dp->d_namlen;
4044 * The dp->d_type is the high byte of the expected dp->d_namlen,
4045 * so must be zero'ed.
4052 * Read a block of directory entries in a filesystem independent format.
4054 #ifndef _SYS_SYSPROTO_H_
4055 struct ogetdirentries_args {
4063 ogetdirentries(struct thread *td, struct ogetdirentries_args *uap)
4068 error = kern_ogetdirentries(td, uap, &loff);
4070 error = copyout(&loff, uap->basep, sizeof(long));
4075 kern_ogetdirentries(struct thread *td, struct ogetdirentries_args *uap,
4081 /* XXX arbitrary sanity limit on `count'. */
4082 if (uap->count > 64 * 1024)
4085 error = freebsd11_kern_getdirentries(td, uap->fd, uap->buf, uap->count,
4086 &base, ogetdirentries_cvt);
4088 if (error == 0 && uap->basep != NULL)
4089 error = copyout(&base, uap->basep, sizeof(long));
4093 #endif /* COMPAT_43 */
4095 #if defined(COMPAT_FREEBSD11)
4096 #ifndef _SYS_SYSPROTO_H_
4097 struct freebsd11_getdirentries_args {
4105 freebsd11_getdirentries(struct thread *td,
4106 struct freebsd11_getdirentries_args *uap)
4111 error = freebsd11_kern_getdirentries(td, uap->fd, uap->buf, uap->count,
4114 if (error == 0 && uap->basep != NULL)
4115 error = copyout(&base, uap->basep, sizeof(long));
4120 freebsd11_getdents(struct thread *td, struct freebsd11_getdents_args *uap)
4122 struct freebsd11_getdirentries_args ap;
4126 ap.count = uap->count;
4128 return (freebsd11_getdirentries(td, &ap));
4130 #endif /* COMPAT_FREEBSD11 */
4133 * Read a block of directory entries in a filesystem independent format.
4136 sys_getdirentries(struct thread *td, struct getdirentries_args *uap)
4141 error = kern_getdirentries(td, uap->fd, uap->buf, uap->count, &base,
4142 NULL, UIO_USERSPACE);
4145 if (uap->basep != NULL)
4146 error = copyout(&base, uap->basep, sizeof(off_t));
4151 kern_getdirentries(struct thread *td, int fd, char *buf, size_t count,
4152 off_t *basep, ssize_t *residp, enum uio_seg bufseg)
4163 if (count > IOSIZE_MAX)
4165 auio.uio_resid = count;
4166 error = getvnode(td, fd, &cap_read_rights, &fp);
4169 if ((fp->f_flag & FREAD) == 0) {
4174 foffset = foffset_lock(fp, 0);
4176 if (vp->v_type != VDIR) {
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 if ((uap->flags & ~(AT_SYMLINK_NOFOLLOW | AT_RESOLVE_BENEATH)) != 0)
4407 return (kern_getfhat(td, uap->flags, uap->fd, uap->path, UIO_USERSPACE,
4408 uap->fhp, UIO_USERSPACE));
4412 kern_getfhat(struct thread *td, int flags, int fd, const char *path,
4413 enum uio_seg pathseg, fhandle_t *fhp, enum uio_seg fhseg)
4415 struct nameidata nd;
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));