2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
6 * (c) UNIX System Laboratories, Inc.
7 * All or some portions of this file are derived from material licensed
8 * to the University of California by American Telephone and Telegraph
9 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10 * the permission of UNIX System Laboratories, Inc.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * @(#)vfs_syscalls.c 8.13 (Berkeley) 4/15/94
39 #include <sys/cdefs.h>
40 #include "opt_capsicum.h"
41 #include "opt_ktrace.h"
43 #include <sys/param.h>
44 #include <sys/systm.h>
47 #include <sys/capsicum.h>
49 #include <sys/malloc.h>
50 #include <sys/mount.h>
51 #include <sys/mutex.h>
52 #include <sys/sysproto.h>
53 #include <sys/namei.h>
54 #include <sys/filedesc.h>
55 #include <sys/kernel.h>
56 #include <sys/fcntl.h>
58 #include <sys/filio.h>
59 #include <sys/limits.h>
60 #include <sys/linker.h>
61 #include <sys/rwlock.h>
65 #include <sys/unistd.h>
66 #include <sys/vnode.h>
69 #include <sys/dirent.h>
71 #include <sys/syscallsubr.h>
72 #include <sys/sysctl.h>
74 #include <sys/ktrace.h>
77 #include <machine/stdarg.h>
79 #include <security/audit/audit.h>
80 #include <security/mac/mac_framework.h>
83 #include <vm/vm_object.h>
84 #include <vm/vm_page.h>
85 #include <vm/vnode_pager.h>
88 #include <fs/devfs/devfs.h>
90 #include <ufs/ufs/quota.h>
92 MALLOC_DEFINE(M_FADVISE, "fadvise", "posix_fadvise(2) information");
94 static int kern_chflagsat(struct thread *td, int fd, const char *path,
95 enum uio_seg pathseg, u_long flags, int atflag);
96 static int setfflags(struct thread *td, struct vnode *, u_long);
97 static int getutimes(const struct timeval *, enum uio_seg, struct timespec *);
98 static int getutimens(const struct timespec *, enum uio_seg,
99 struct timespec *, int *);
100 static int setutimes(struct thread *td, struct vnode *,
101 const struct timespec *, int, int);
102 static int vn_access(struct vnode *vp, int user_flags, struct ucred *cred,
104 static int kern_fhlinkat(struct thread *td, int fd, const char *path,
105 enum uio_seg pathseg, fhandle_t *fhp);
106 static int kern_readlink_vp(struct vnode *vp, char *buf, enum uio_seg bufseg,
107 size_t count, struct thread *td);
108 static int kern_linkat_vp(struct thread *td, struct vnode *vp, int fd,
109 const char *path, enum uio_seg segflag);
112 at2cnpflags(u_int at_flags, u_int mask)
116 MPASS((at_flags & (AT_SYMLINK_FOLLOW | AT_SYMLINK_NOFOLLOW)) !=
117 (AT_SYMLINK_FOLLOW | AT_SYMLINK_NOFOLLOW));
121 if ((at_flags & AT_RESOLVE_BENEATH) != 0)
123 if ((at_flags & AT_SYMLINK_FOLLOW) != 0)
125 /* NOFOLLOW is pseudo flag */
126 if ((mask & AT_SYMLINK_NOFOLLOW) != 0) {
127 res |= (at_flags & AT_SYMLINK_NOFOLLOW) != 0 ? NOFOLLOW :
130 if ((mask & AT_EMPTY_PATH) != 0 && (at_flags & AT_EMPTY_PATH) != 0)
136 kern_sync(struct thread *td)
138 struct mount *mp, *nmp;
141 mtx_lock(&mountlist_mtx);
142 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
143 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK)) {
144 nmp = TAILQ_NEXT(mp, mnt_list);
147 if ((mp->mnt_flag & MNT_RDONLY) == 0 &&
148 vn_start_write(NULL, &mp, V_NOWAIT) == 0) {
149 save = curthread_pflags_set(TDP_SYNCIO);
150 vfs_periodic(mp, MNT_NOWAIT);
151 VFS_SYNC(mp, MNT_NOWAIT);
152 curthread_pflags_restore(save);
153 vn_finished_write(mp);
155 mtx_lock(&mountlist_mtx);
156 nmp = TAILQ_NEXT(mp, mnt_list);
159 mtx_unlock(&mountlist_mtx);
164 * Sync each mounted filesystem.
166 #ifndef _SYS_SYSPROTO_H_
173 sys_sync(struct thread *td, struct sync_args *uap)
176 return (kern_sync(td));
180 * Change filesystem quotas.
182 #ifndef _SYS_SYSPROTO_H_
183 struct quotactl_args {
191 sys_quotactl(struct thread *td, struct quotactl_args *uap)
197 AUDIT_ARG_CMD(uap->cmd);
198 AUDIT_ARG_UID(uap->uid);
199 if (!prison_allow(td->td_ucred, PR_ALLOW_QUOTAS))
201 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
203 if ((error = namei(&nd)) != 0)
205 NDFREE(&nd, NDF_ONLY_PNBUF);
206 mp = nd.ni_vp->v_mount;
209 error = vfs_busy(mp, 0);
214 error = VFS_QUOTACTL(mp, uap->cmd, uap->uid, uap->arg);
217 * Since quota on operation typically needs to open quota
218 * file, the Q_QUOTAON handler needs to unbusy the mount point
219 * before calling into namei. Otherwise, unmount might be
220 * started between two vfs_busy() invocations (first is our,
221 * second is from mount point cross-walk code in lookup()),
224 * Require that Q_QUOTAON handles the vfs_busy() reference on
225 * its own, always returning with ubusied mount point.
227 if ((uap->cmd >> SUBCMDSHIFT) != Q_QUOTAON &&
228 (uap->cmd >> SUBCMDSHIFT) != Q_QUOTAOFF)
235 * Used by statfs conversion routines to scale the block size up if
236 * necessary so that all of the block counts are <= 'max_size'. Note
237 * that 'max_size' should be a bitmask, i.e. 2^n - 1 for some non-zero
241 statfs_scale_blocks(struct statfs *sf, long max_size)
246 KASSERT(powerof2(max_size + 1), ("%s: invalid max_size", __func__));
249 * Attempt to scale the block counts to give a more accurate
250 * overview to userland of the ratio of free space to used
251 * space. To do this, find the largest block count and compute
252 * a divisor that lets it fit into a signed integer <= max_size.
254 if (sf->f_bavail < 0)
255 count = -sf->f_bavail;
257 count = sf->f_bavail;
258 count = MAX(sf->f_blocks, MAX(sf->f_bfree, count));
259 if (count <= max_size)
262 count >>= flsl(max_size);
269 sf->f_bsize <<= shift;
270 sf->f_blocks >>= shift;
271 sf->f_bfree >>= shift;
272 sf->f_bavail >>= shift;
276 kern_do_statfs(struct thread *td, struct mount *mp, struct statfs *buf)
282 error = vfs_busy(mp, 0);
287 error = mac_mount_check_stat(td->td_ucred, mp);
291 error = VFS_STATFS(mp, buf);
294 if (priv_check_cred_vfs_generation(td->td_ucred)) {
295 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
296 prison_enforce_statfs(td->td_ucred, mp, buf);
304 * Get filesystem statistics.
306 #ifndef _SYS_SYSPROTO_H_
313 sys_statfs(struct thread *td, struct statfs_args *uap)
318 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
319 error = kern_statfs(td, uap->path, UIO_USERSPACE, sfp);
321 error = copyout(sfp, uap->buf, sizeof(struct statfs));
327 kern_statfs(struct thread *td, const char *path, enum uio_seg pathseg,
334 NDINIT(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, td);
338 mp = vfs_ref_from_vp(nd.ni_vp);
341 return (kern_do_statfs(td, mp, buf));
345 * Get filesystem statistics.
347 #ifndef _SYS_SYSPROTO_H_
348 struct fstatfs_args {
354 sys_fstatfs(struct thread *td, struct fstatfs_args *uap)
359 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
360 error = kern_fstatfs(td, uap->fd, sfp);
362 error = copyout(sfp, uap->buf, sizeof(struct statfs));
368 kern_fstatfs(struct thread *td, int fd, struct statfs *buf)
376 error = getvnode_path(td, fd, &cap_fstatfs_rights, &fp);
381 if (AUDITING_TD(td)) {
382 vn_lock(vp, LK_SHARED | LK_RETRY);
383 AUDIT_ARG_VNODE1(vp);
387 mp = vfs_ref_from_vp(vp);
389 return (kern_do_statfs(td, mp, buf));
393 * Get statistics on all filesystems.
395 #ifndef _SYS_SYSPROTO_H_
396 struct getfsstat_args {
403 sys_getfsstat(struct thread *td, struct getfsstat_args *uap)
408 if (uap->bufsize < 0 || uap->bufsize > SIZE_MAX)
410 error = kern_getfsstat(td, &uap->buf, uap->bufsize, &count,
411 UIO_USERSPACE, uap->mode);
413 td->td_retval[0] = count;
418 * If (bufsize > 0 && bufseg == UIO_SYSSPACE)
419 * The caller is responsible for freeing memory which will be allocated
423 kern_getfsstat(struct thread *td, struct statfs **buf, size_t bufsize,
424 size_t *countp, enum uio_seg bufseg, int mode)
426 struct mount *mp, *nmp;
427 struct statfs *sfsp, *sp, *sptmp, *tofree;
428 size_t count, maxcount;
436 if (bufseg == UIO_SYSSPACE)
441 maxcount = bufsize / sizeof(struct statfs);
445 } else if (bufseg == UIO_USERSPACE) {
448 } else /* if (bufseg == UIO_SYSSPACE) */ {
450 mtx_lock(&mountlist_mtx);
451 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
454 mtx_unlock(&mountlist_mtx);
455 if (maxcount > count)
457 tofree = sfsp = *buf = malloc(maxcount * sizeof(struct statfs),
464 * If there is no target buffer they only want the count.
466 * This could be TAILQ_FOREACH but it is open-coded to match the original
470 mtx_lock(&mountlist_mtx);
471 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
472 if (prison_canseemount(td->td_ucred, mp) != 0) {
473 nmp = TAILQ_NEXT(mp, mnt_list);
477 if (mac_mount_check_stat(td->td_ucred, mp) != 0) {
478 nmp = TAILQ_NEXT(mp, mnt_list);
483 nmp = TAILQ_NEXT(mp, mnt_list);
485 mtx_unlock(&mountlist_mtx);
491 * They want the entire thing.
493 * Short-circuit the corner case of no room for anything, avoids
500 mtx_lock(&mountlist_mtx);
501 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
502 if (prison_canseemount(td->td_ucred, mp) != 0) {
503 nmp = TAILQ_NEXT(mp, mnt_list);
507 if (mac_mount_check_stat(td->td_ucred, mp) != 0) {
508 nmp = TAILQ_NEXT(mp, mnt_list);
512 if (mode == MNT_WAIT) {
513 if (vfs_busy(mp, MBF_MNTLSTLOCK) != 0) {
515 * If vfs_busy() failed, and MBF_NOWAIT
516 * wasn't passed, then the mp is gone.
517 * Furthermore, because of MBF_MNTLSTLOCK,
518 * the mountlist_mtx was dropped. We have
519 * no other choice than to start over.
521 mtx_unlock(&mountlist_mtx);
522 free(tofree, M_STATFS);
526 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK) != 0) {
527 nmp = TAILQ_NEXT(mp, mnt_list);
533 * If MNT_NOWAIT is specified, do not refresh
536 if (mode != MNT_NOWAIT) {
537 error = VFS_STATFS(mp, sp);
539 mtx_lock(&mountlist_mtx);
540 nmp = TAILQ_NEXT(mp, mnt_list);
545 if (priv_check_cred_vfs_generation(td->td_ucred)) {
546 sptmp = malloc(sizeof(struct statfs), M_STATFS,
549 sptmp->f_fsid.val[0] = sptmp->f_fsid.val[1] = 0;
550 prison_enforce_statfs(td->td_ucred, mp, sptmp);
554 if (bufseg == UIO_SYSSPACE) {
555 bcopy(sp, sfsp, sizeof(*sp));
556 free(sptmp, M_STATFS);
557 } else /* if (bufseg == UIO_USERSPACE) */ {
558 error = copyout(sp, sfsp, sizeof(*sp));
559 free(sptmp, M_STATFS);
568 if (count == maxcount) {
573 mtx_lock(&mountlist_mtx);
574 nmp = TAILQ_NEXT(mp, mnt_list);
577 mtx_unlock(&mountlist_mtx);
583 #ifdef COMPAT_FREEBSD4
585 * Get old format filesystem statistics.
587 static void freebsd4_cvtstatfs(struct statfs *, struct ostatfs *);
589 #ifndef _SYS_SYSPROTO_H_
590 struct freebsd4_statfs_args {
596 freebsd4_statfs(struct thread *td, struct freebsd4_statfs_args *uap)
602 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
603 error = kern_statfs(td, uap->path, UIO_USERSPACE, sfp);
605 freebsd4_cvtstatfs(sfp, &osb);
606 error = copyout(&osb, uap->buf, sizeof(osb));
613 * Get filesystem statistics.
615 #ifndef _SYS_SYSPROTO_H_
616 struct freebsd4_fstatfs_args {
622 freebsd4_fstatfs(struct thread *td, struct freebsd4_fstatfs_args *uap)
628 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
629 error = kern_fstatfs(td, uap->fd, sfp);
631 freebsd4_cvtstatfs(sfp, &osb);
632 error = copyout(&osb, uap->buf, sizeof(osb));
639 * Get statistics on all filesystems.
641 #ifndef _SYS_SYSPROTO_H_
642 struct freebsd4_getfsstat_args {
649 freebsd4_getfsstat(struct thread *td, struct freebsd4_getfsstat_args *uap)
651 struct statfs *buf, *sp;
656 if (uap->bufsize < 0)
658 count = uap->bufsize / sizeof(struct ostatfs);
659 if (count > SIZE_MAX / sizeof(struct statfs))
661 size = count * sizeof(struct statfs);
662 error = kern_getfsstat(td, &buf, size, &count, UIO_SYSSPACE,
665 td->td_retval[0] = count;
668 while (count != 0 && error == 0) {
669 freebsd4_cvtstatfs(sp, &osb);
670 error = copyout(&osb, uap->buf, sizeof(osb));
681 * Implement fstatfs() for (NFS) file handles.
683 #ifndef _SYS_SYSPROTO_H_
684 struct freebsd4_fhstatfs_args {
685 struct fhandle *u_fhp;
690 freebsd4_fhstatfs(struct thread *td, struct freebsd4_fhstatfs_args *uap)
697 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
700 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
701 error = kern_fhstatfs(td, fh, sfp);
703 freebsd4_cvtstatfs(sfp, &osb);
704 error = copyout(&osb, uap->buf, sizeof(osb));
711 * Convert a new format statfs structure to an old format statfs structure.
714 freebsd4_cvtstatfs(struct statfs *nsp, struct ostatfs *osp)
717 statfs_scale_blocks(nsp, LONG_MAX);
718 bzero(osp, sizeof(*osp));
719 osp->f_bsize = nsp->f_bsize;
720 osp->f_iosize = MIN(nsp->f_iosize, LONG_MAX);
721 osp->f_blocks = nsp->f_blocks;
722 osp->f_bfree = nsp->f_bfree;
723 osp->f_bavail = nsp->f_bavail;
724 osp->f_files = MIN(nsp->f_files, LONG_MAX);
725 osp->f_ffree = MIN(nsp->f_ffree, LONG_MAX);
726 osp->f_owner = nsp->f_owner;
727 osp->f_type = nsp->f_type;
728 osp->f_flags = nsp->f_flags;
729 osp->f_syncwrites = MIN(nsp->f_syncwrites, LONG_MAX);
730 osp->f_asyncwrites = MIN(nsp->f_asyncwrites, LONG_MAX);
731 osp->f_syncreads = MIN(nsp->f_syncreads, LONG_MAX);
732 osp->f_asyncreads = MIN(nsp->f_asyncreads, LONG_MAX);
733 strlcpy(osp->f_fstypename, nsp->f_fstypename,
734 MIN(MFSNAMELEN, OMFSNAMELEN));
735 strlcpy(osp->f_mntonname, nsp->f_mntonname,
736 MIN(MNAMELEN, OMNAMELEN));
737 strlcpy(osp->f_mntfromname, nsp->f_mntfromname,
738 MIN(MNAMELEN, OMNAMELEN));
739 osp->f_fsid = nsp->f_fsid;
741 #endif /* COMPAT_FREEBSD4 */
743 #if defined(COMPAT_FREEBSD11)
745 * Get old format filesystem statistics.
747 static void freebsd11_cvtstatfs(struct statfs *, struct freebsd11_statfs *);
750 freebsd11_statfs(struct thread *td, struct freebsd11_statfs_args *uap)
752 struct freebsd11_statfs osb;
756 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
757 error = kern_statfs(td, uap->path, UIO_USERSPACE, sfp);
759 freebsd11_cvtstatfs(sfp, &osb);
760 error = copyout(&osb, uap->buf, sizeof(osb));
767 * Get filesystem statistics.
770 freebsd11_fstatfs(struct thread *td, struct freebsd11_fstatfs_args *uap)
772 struct freebsd11_statfs osb;
776 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
777 error = kern_fstatfs(td, uap->fd, sfp);
779 freebsd11_cvtstatfs(sfp, &osb);
780 error = copyout(&osb, uap->buf, sizeof(osb));
787 * Get statistics on all filesystems.
790 freebsd11_getfsstat(struct thread *td, struct freebsd11_getfsstat_args *uap)
792 struct freebsd11_statfs osb;
793 struct statfs *buf, *sp;
797 count = uap->bufsize / sizeof(struct ostatfs);
798 size = count * sizeof(struct statfs);
799 error = kern_getfsstat(td, &buf, size, &count, UIO_SYSSPACE,
802 td->td_retval[0] = count;
805 while (count > 0 && error == 0) {
806 freebsd11_cvtstatfs(sp, &osb);
807 error = copyout(&osb, uap->buf, sizeof(osb));
818 * Implement fstatfs() for (NFS) file handles.
821 freebsd11_fhstatfs(struct thread *td, struct freebsd11_fhstatfs_args *uap)
823 struct freebsd11_statfs osb;
828 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
831 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
832 error = kern_fhstatfs(td, fh, sfp);
834 freebsd11_cvtstatfs(sfp, &osb);
835 error = copyout(&osb, uap->buf, sizeof(osb));
842 * Convert a new format statfs structure to an old format statfs structure.
845 freebsd11_cvtstatfs(struct statfs *nsp, struct freebsd11_statfs *osp)
848 bzero(osp, sizeof(*osp));
849 osp->f_version = FREEBSD11_STATFS_VERSION;
850 osp->f_type = nsp->f_type;
851 osp->f_flags = nsp->f_flags;
852 osp->f_bsize = nsp->f_bsize;
853 osp->f_iosize = nsp->f_iosize;
854 osp->f_blocks = nsp->f_blocks;
855 osp->f_bfree = nsp->f_bfree;
856 osp->f_bavail = nsp->f_bavail;
857 osp->f_files = nsp->f_files;
858 osp->f_ffree = nsp->f_ffree;
859 osp->f_syncwrites = nsp->f_syncwrites;
860 osp->f_asyncwrites = nsp->f_asyncwrites;
861 osp->f_syncreads = nsp->f_syncreads;
862 osp->f_asyncreads = nsp->f_asyncreads;
863 osp->f_namemax = nsp->f_namemax;
864 osp->f_owner = nsp->f_owner;
865 osp->f_fsid = nsp->f_fsid;
866 strlcpy(osp->f_fstypename, nsp->f_fstypename,
867 MIN(MFSNAMELEN, sizeof(osp->f_fstypename)));
868 strlcpy(osp->f_mntonname, nsp->f_mntonname,
869 MIN(MNAMELEN, sizeof(osp->f_mntonname)));
870 strlcpy(osp->f_mntfromname, nsp->f_mntfromname,
871 MIN(MNAMELEN, sizeof(osp->f_mntfromname)));
873 #endif /* COMPAT_FREEBSD11 */
876 * Change current working directory to a given file descriptor.
878 #ifndef _SYS_SYSPROTO_H_
884 sys_fchdir(struct thread *td, struct fchdir_args *uap)
886 struct vnode *vp, *tdp;
891 AUDIT_ARG_FD(uap->fd);
892 error = getvnode_path(td, uap->fd, &cap_fchdir_rights,
899 vn_lock(vp, LK_SHARED | LK_RETRY);
900 AUDIT_ARG_VNODE1(vp);
901 error = change_dir(vp, td);
902 while (!error && (mp = vp->v_mountedhere) != NULL) {
905 error = VFS_ROOT(mp, LK_SHARED, &tdp);
922 * Change current working directory (``.'').
924 #ifndef _SYS_SYSPROTO_H_
930 sys_chdir(struct thread *td, struct chdir_args *uap)
933 return (kern_chdir(td, uap->path, UIO_USERSPACE));
937 kern_chdir(struct thread *td, const char *path, enum uio_seg pathseg)
942 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
944 if ((error = namei(&nd)) != 0)
946 if ((error = change_dir(nd.ni_vp, td)) != 0) {
951 VOP_UNLOCK(nd.ni_vp);
953 pwd_chdir(td, nd.ni_vp);
957 static int unprivileged_chroot = 0;
958 SYSCTL_INT(_security_bsd, OID_AUTO, unprivileged_chroot, CTLFLAG_RW,
959 &unprivileged_chroot, 0,
960 "Unprivileged processes can use chroot(2)");
962 * Change notion of root (``/'') directory.
964 #ifndef _SYS_SYSPROTO_H_
970 sys_chroot(struct thread *td, struct chroot_args *uap)
976 error = priv_check(td, PRIV_VFS_CHROOT);
980 if (unprivileged_chroot == 0 ||
981 (p->p_flag2 & P2_NO_NEW_PRIVS) == 0) {
987 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
988 UIO_USERSPACE, uap->path, td);
992 error = change_dir(nd.ni_vp, td);
996 error = mac_vnode_check_chroot(td->td_ucred, nd.ni_vp);
1000 VOP_UNLOCK(nd.ni_vp);
1001 error = pwd_chroot(td, nd.ni_vp);
1003 NDFREE_NOTHING(&nd);
1008 NDFREE_NOTHING(&nd);
1013 * Common routine for chroot and chdir. Callers must provide a locked vnode
1017 change_dir(struct vnode *vp, struct thread *td)
1023 ASSERT_VOP_LOCKED(vp, "change_dir(): vp not locked");
1024 if (vp->v_type != VDIR)
1027 error = mac_vnode_check_chdir(td->td_ucred, vp);
1031 return (VOP_ACCESS(vp, VEXEC, td->td_ucred, td));
1034 static __inline void
1035 flags_to_rights(int flags, cap_rights_t *rightsp)
1037 if (flags & O_EXEC) {
1038 cap_rights_set_one(rightsp, CAP_FEXECVE);
1042 switch ((flags & O_ACCMODE)) {
1044 cap_rights_set_one(rightsp, CAP_READ);
1047 cap_rights_set_one(rightsp, CAP_READ);
1050 cap_rights_set_one(rightsp, CAP_WRITE);
1051 if (!(flags & (O_APPEND | O_TRUNC)))
1052 cap_rights_set_one(rightsp, CAP_SEEK);
1057 if (flags & O_CREAT)
1058 cap_rights_set_one(rightsp, CAP_CREATE);
1060 if (flags & O_TRUNC)
1061 cap_rights_set_one(rightsp, CAP_FTRUNCATE);
1063 if (flags & (O_SYNC | O_FSYNC))
1064 cap_rights_set_one(rightsp, CAP_FSYNC);
1066 if (flags & (O_EXLOCK | O_SHLOCK))
1067 cap_rights_set_one(rightsp, CAP_FLOCK);
1071 * Check permissions, allocate an open file structure, and call the device
1072 * open routine if any.
1074 #ifndef _SYS_SYSPROTO_H_
1082 sys_open(struct thread *td, struct open_args *uap)
1085 return (kern_openat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1086 uap->flags, uap->mode));
1089 #ifndef _SYS_SYSPROTO_H_
1090 struct openat_args {
1098 sys_openat(struct thread *td, struct openat_args *uap)
1101 AUDIT_ARG_FD(uap->fd);
1102 return (kern_openat(td, uap->fd, uap->path, UIO_USERSPACE, uap->flag,
1107 kern_openat(struct thread *td, int fd, const char *path, enum uio_seg pathseg,
1108 int flags, int mode)
1110 struct proc *p = td->td_proc;
1111 struct filedesc *fdp;
1112 struct pwddesc *pdp;
1115 struct nameidata nd;
1116 cap_rights_t rights;
1117 int cmode, error, indx;
1123 AUDIT_ARG_FFLAGS(flags);
1124 AUDIT_ARG_MODE(mode);
1125 cap_rights_init_one(&rights, CAP_LOOKUP);
1126 flags_to_rights(flags, &rights);
1129 * Only one of the O_EXEC, O_RDONLY, O_WRONLY and O_RDWR flags
1130 * may be specified. On the other hand, for O_PATH any mode
1131 * except O_EXEC is ignored.
1133 if ((flags & O_PATH) != 0) {
1134 flags &= ~(O_CREAT | O_ACCMODE);
1135 } else if ((flags & O_EXEC) != 0) {
1136 if (flags & O_ACCMODE)
1138 } else if ((flags & O_ACCMODE) == O_ACCMODE) {
1141 flags = FFLAGS(flags);
1145 * Allocate a file structure. The descriptor to reference it
1146 * is allocated and used by finstall_refed() below.
1148 error = falloc_noinstall(td, &fp);
1151 /* Set the flags early so the finit in devfs can pick them up. */
1152 fp->f_flag = flags & FMASK;
1153 cmode = ((mode & ~pdp->pd_cmask) & ALLPERMS) & ~S_ISTXT;
1154 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | AUDITVNODE1 | WANTIOCTLCAPS,
1155 pathseg, path, fd, &rights, td);
1156 td->td_dupfd = -1; /* XXX check for fdopen */
1157 error = vn_open_cred(&nd, &flags, cmode, VN_OPEN_WANTIOCTLCAPS,
1161 * If the vn_open replaced the method vector, something
1162 * wonderous happened deep below and we just pass it up
1163 * pretending we know what we do.
1165 if (error == ENXIO && fp->f_ops != &badfileops) {
1166 MPASS((flags & O_PATH) == 0);
1171 * Handle special fdopen() case. bleh.
1173 * Don't do this for relative (capability) lookups; we don't
1174 * understand exactly what would happen, and we don't think
1175 * that it ever should.
1177 if ((nd.ni_resflags & NIRES_STRICTREL) == 0 &&
1178 (error == ENODEV || error == ENXIO) &&
1179 td->td_dupfd >= 0) {
1180 error = dupfdopen(td, fdp, td->td_dupfd, flags, error,
1189 NDFREE(&nd, NDF_ONLY_PNBUF);
1193 * Store the vnode, for any f_type. Typically, the vnode use
1194 * count is decremented by direct call to vn_closefile() for
1195 * files that switched type in the cdevsw fdopen() method.
1200 * If the file wasn't claimed by devfs bind it to the normal
1201 * vnode operations here.
1203 if (fp->f_ops == &badfileops) {
1204 KASSERT(vp->v_type != VFIFO || (flags & O_PATH) != 0,
1205 ("Unexpected fifo fp %p vp %p", fp, vp));
1206 if ((flags & O_PATH) != 0) {
1207 finit(fp, (flags & FMASK) | (fp->f_flag & FKQALLOWED),
1208 DTYPE_VNODE, NULL, &path_fileops);
1210 finit_vnode(fp, flags, NULL, &vnops);
1215 if (flags & O_TRUNC) {
1216 error = fo_truncate(fp, 0, td->td_ucred, td);
1222 * If we haven't already installed the FD (for dupfdopen), do so now.
1225 struct filecaps *fcaps;
1228 if ((nd.ni_resflags & NIRES_STRICTREL) != 0)
1229 fcaps = &nd.ni_filecaps;
1233 error = finstall_refed(td, fp, &indx, flags, fcaps);
1234 /* On success finstall_refed() consumes fcaps. */
1239 NDFREE_IOCTLCAPS(&nd);
1240 falloc_abort(td, fp);
1243 td->td_retval[0] = indx;
1246 KASSERT(indx == -1, ("indx=%d, should be -1", indx));
1247 NDFREE_IOCTLCAPS(&nd);
1248 falloc_abort(td, fp);
1256 #ifndef _SYS_SYSPROTO_H_
1257 struct ocreat_args {
1263 ocreat(struct thread *td, struct ocreat_args *uap)
1266 return (kern_openat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1267 O_WRONLY | O_CREAT | O_TRUNC, uap->mode));
1269 #endif /* COMPAT_43 */
1272 * Create a special file.
1274 #ifndef _SYS_SYSPROTO_H_
1275 struct mknodat_args {
1283 sys_mknodat(struct thread *td, struct mknodat_args *uap)
1286 return (kern_mknodat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode,
1290 #if defined(COMPAT_FREEBSD11)
1292 freebsd11_mknod(struct thread *td,
1293 struct freebsd11_mknod_args *uap)
1296 return (kern_mknodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1297 uap->mode, uap->dev));
1301 freebsd11_mknodat(struct thread *td,
1302 struct freebsd11_mknodat_args *uap)
1305 return (kern_mknodat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode,
1308 #endif /* COMPAT_FREEBSD11 */
1311 kern_mknodat(struct thread *td, int fd, const char *path, enum uio_seg pathseg,
1312 int mode, dev_t dev)
1317 struct nameidata nd;
1318 int error, whiteout = 0;
1320 AUDIT_ARG_MODE(mode);
1322 switch (mode & S_IFMT) {
1325 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
1326 if (error == 0 && dev == VNOVAL)
1330 error = priv_check(td, PRIV_VFS_MKNOD_WHT);
1334 return (kern_mkfifoat(td, fd, path, pathseg, mode));
1345 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
1346 NOCACHE, pathseg, path, fd, &cap_mknodat_rights,
1348 if ((error = namei(&nd)) != 0)
1352 NDFREE(&nd, NDF_ONLY_PNBUF);
1353 if (vp == nd.ni_dvp)
1361 vattr.va_mode = (mode & ALLPERMS) &
1362 ~td->td_proc->p_pd->pd_cmask;
1363 vattr.va_rdev = dev;
1366 switch (mode & S_IFMT) {
1368 vattr.va_type = VCHR;
1371 vattr.va_type = VBLK;
1377 panic("kern_mknod: invalid mode");
1380 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1381 NDFREE(&nd, NDF_ONLY_PNBUF);
1383 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1388 if (error == 0 && !whiteout)
1389 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp,
1390 &nd.ni_cnd, &vattr);
1394 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, CREATE);
1396 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp,
1397 &nd.ni_cnd, &vattr);
1400 VOP_VPUT_PAIR(nd.ni_dvp, error == 0 && !whiteout ? &nd.ni_vp : NULL,
1402 vn_finished_write(mp);
1403 NDFREE(&nd, NDF_ONLY_PNBUF);
1404 if (error == ERELOOKUP)
1410 * Create a named pipe.
1412 #ifndef _SYS_SYSPROTO_H_
1413 struct mkfifo_args {
1419 sys_mkfifo(struct thread *td, struct mkfifo_args *uap)
1422 return (kern_mkfifoat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1426 #ifndef _SYS_SYSPROTO_H_
1427 struct mkfifoat_args {
1434 sys_mkfifoat(struct thread *td, struct mkfifoat_args *uap)
1437 return (kern_mkfifoat(td, uap->fd, uap->path, UIO_USERSPACE,
1442 kern_mkfifoat(struct thread *td, int fd, const char *path,
1443 enum uio_seg pathseg, int mode)
1447 struct nameidata nd;
1450 AUDIT_ARG_MODE(mode);
1454 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
1455 NOCACHE, pathseg, path, fd, &cap_mkfifoat_rights,
1457 if ((error = namei(&nd)) != 0)
1459 if (nd.ni_vp != NULL) {
1460 NDFREE(&nd, NDF_ONLY_PNBUF);
1461 if (nd.ni_vp == nd.ni_dvp)
1468 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1469 NDFREE(&nd, NDF_ONLY_PNBUF);
1471 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1476 vattr.va_type = VFIFO;
1477 vattr.va_mode = (mode & ALLPERMS) & ~td->td_proc->p_pd->pd_cmask;
1479 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1484 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
1488 VOP_VPUT_PAIR(nd.ni_dvp, error == 0 ? &nd.ni_vp : NULL, true);
1489 vn_finished_write(mp);
1490 NDFREE(&nd, NDF_ONLY_PNBUF);
1491 if (error == ERELOOKUP)
1497 * Make a hard file link.
1499 #ifndef _SYS_SYSPROTO_H_
1506 sys_link(struct thread *td, struct link_args *uap)
1509 return (kern_linkat(td, AT_FDCWD, AT_FDCWD, uap->path, uap->link,
1510 UIO_USERSPACE, AT_SYMLINK_FOLLOW));
1513 #ifndef _SYS_SYSPROTO_H_
1514 struct linkat_args {
1523 sys_linkat(struct thread *td, struct linkat_args *uap)
1526 return (kern_linkat(td, uap->fd1, uap->fd2, uap->path1, uap->path2,
1527 UIO_USERSPACE, uap->flag));
1530 int hardlink_check_uid = 0;
1531 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
1532 &hardlink_check_uid, 0,
1533 "Unprivileged processes cannot create hard links to files owned by other "
1535 static int hardlink_check_gid = 0;
1536 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
1537 &hardlink_check_gid, 0,
1538 "Unprivileged processes cannot create hard links to files owned by other "
1542 can_hardlink(struct vnode *vp, struct ucred *cred)
1547 if (!hardlink_check_uid && !hardlink_check_gid)
1550 error = VOP_GETATTR(vp, &va, cred);
1554 if (hardlink_check_uid && cred->cr_uid != va.va_uid) {
1555 error = priv_check_cred(cred, PRIV_VFS_LINK);
1560 if (hardlink_check_gid && !groupmember(va.va_gid, cred)) {
1561 error = priv_check_cred(cred, PRIV_VFS_LINK);
1570 kern_linkat(struct thread *td, int fd1, int fd2, const char *path1,
1571 const char *path2, enum uio_seg segflag, int flag)
1573 struct nameidata nd;
1576 if ((flag & ~(AT_SYMLINK_FOLLOW | AT_RESOLVE_BENEATH |
1577 AT_EMPTY_PATH)) != 0)
1583 NDINIT_ATRIGHTS(&nd, LOOKUP, AUDITVNODE1 | at2cnpflags(flag,
1584 AT_SYMLINK_FOLLOW | AT_RESOLVE_BENEATH | AT_EMPTY_PATH),
1585 segflag, path1, fd1, &cap_linkat_source_rights, td);
1586 if ((error = namei(&nd)) != 0)
1588 NDFREE(&nd, NDF_ONLY_PNBUF);
1589 if ((nd.ni_resflags & NIRES_EMPTYPATH) != 0) {
1590 error = priv_check(td, PRIV_VFS_FHOPEN);
1596 error = kern_linkat_vp(td, nd.ni_vp, fd2, path2, segflag);
1597 } while (error == EAGAIN || error == ERELOOKUP);
1602 kern_linkat_vp(struct thread *td, struct vnode *vp, int fd, const char *path,
1603 enum uio_seg segflag)
1605 struct nameidata nd;
1609 if (vp->v_type == VDIR) {
1611 return (EPERM); /* POSIX */
1613 NDINIT_ATRIGHTS(&nd, CREATE,
1614 LOCKPARENT | SAVENAME | AUDITVNODE2 | NOCACHE, segflag, path, fd,
1615 &cap_linkat_target_rights, td);
1616 if ((error = namei(&nd)) == 0) {
1617 if (nd.ni_vp != NULL) {
1618 NDFREE(&nd, NDF_ONLY_PNBUF);
1619 if (nd.ni_dvp == nd.ni_vp)
1626 } else if (nd.ni_dvp->v_mount != vp->v_mount) {
1628 * Cross-device link. No need to recheck
1629 * vp->v_type, since it cannot change, except
1632 NDFREE(&nd, NDF_ONLY_PNBUF);
1636 } else if ((error = vn_lock(vp, LK_EXCLUSIVE)) == 0) {
1637 error = can_hardlink(vp, td->td_ucred);
1640 error = mac_vnode_check_link(td->td_ucred,
1641 nd.ni_dvp, vp, &nd.ni_cnd);
1646 NDFREE(&nd, NDF_ONLY_PNBUF);
1649 error = vn_start_write(vp, &mp, V_NOWAIT);
1653 NDFREE(&nd, NDF_ONLY_PNBUF);
1654 error = vn_start_write(NULL, &mp,
1660 error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
1661 VOP_VPUT_PAIR(nd.ni_dvp, &vp, true);
1662 vn_finished_write(mp);
1663 NDFREE(&nd, NDF_ONLY_PNBUF);
1667 NDFREE(&nd, NDF_ONLY_PNBUF);
1678 * Make a symbolic link.
1680 #ifndef _SYS_SYSPROTO_H_
1681 struct symlink_args {
1687 sys_symlink(struct thread *td, struct symlink_args *uap)
1690 return (kern_symlinkat(td, uap->path, AT_FDCWD, uap->link,
1694 #ifndef _SYS_SYSPROTO_H_
1695 struct symlinkat_args {
1702 sys_symlinkat(struct thread *td, struct symlinkat_args *uap)
1705 return (kern_symlinkat(td, uap->path1, uap->fd, uap->path2,
1710 kern_symlinkat(struct thread *td, const char *path1, int fd, const char *path2,
1711 enum uio_seg segflg)
1715 const char *syspath;
1717 struct nameidata nd;
1720 if (segflg == UIO_SYSSPACE) {
1723 tmppath = uma_zalloc(namei_zone, M_WAITOK);
1724 if ((error = copyinstr(path1, tmppath, MAXPATHLEN, NULL)) != 0)
1728 AUDIT_ARG_TEXT(syspath);
1732 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
1733 NOCACHE, segflg, path2, fd, &cap_symlinkat_rights,
1735 if ((error = namei(&nd)) != 0)
1738 NDFREE(&nd, NDF_ONLY_PNBUF);
1739 if (nd.ni_vp == nd.ni_dvp)
1748 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1749 NDFREE(&nd, NDF_ONLY_PNBUF);
1751 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1756 vattr.va_mode = ACCESSPERMS &~ td->td_proc->p_pd->pd_cmask;
1758 vattr.va_type = VLNK;
1759 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1764 error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr, syspath);
1768 VOP_VPUT_PAIR(nd.ni_dvp, error == 0 ? &nd.ni_vp : NULL, true);
1769 vn_finished_write(mp);
1770 NDFREE(&nd, NDF_ONLY_PNBUF);
1771 if (error == ERELOOKUP)
1774 if (segflg != UIO_SYSSPACE)
1775 uma_zfree(namei_zone, tmppath);
1780 * Delete a whiteout from the filesystem.
1782 #ifndef _SYS_SYSPROTO_H_
1783 struct undelete_args {
1788 sys_undelete(struct thread *td, struct undelete_args *uap)
1791 struct nameidata nd;
1797 NDINIT(&nd, DELETE, LOCKPARENT | DOWHITEOUT | AUDITVNODE1,
1798 UIO_USERSPACE, uap->path, td);
1803 if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & ISWHITEOUT)) {
1804 NDFREE(&nd, NDF_ONLY_PNBUF);
1805 if (nd.ni_vp == nd.ni_dvp)
1813 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1814 NDFREE(&nd, NDF_ONLY_PNBUF);
1816 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1820 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, DELETE);
1821 NDFREE(&nd, NDF_ONLY_PNBUF);
1823 vn_finished_write(mp);
1824 if (error == ERELOOKUP)
1830 * Delete a name from the filesystem.
1832 #ifndef _SYS_SYSPROTO_H_
1833 struct unlink_args {
1838 sys_unlink(struct thread *td, struct unlink_args *uap)
1841 return (kern_funlinkat(td, AT_FDCWD, uap->path, FD_NONE, UIO_USERSPACE,
1846 kern_funlinkat_ex(struct thread *td, int dfd, const char *path, int fd,
1847 int flag, enum uio_seg pathseg, ino_t oldinum)
1850 if ((flag & ~(AT_REMOVEDIR | AT_RESOLVE_BENEATH)) != 0)
1853 if ((flag & AT_REMOVEDIR) != 0)
1854 return (kern_frmdirat(td, dfd, path, fd, UIO_USERSPACE, 0));
1856 return (kern_funlinkat(td, dfd, path, fd, UIO_USERSPACE, 0, 0));
1859 #ifndef _SYS_SYSPROTO_H_
1860 struct unlinkat_args {
1867 sys_unlinkat(struct thread *td, struct unlinkat_args *uap)
1870 return (kern_funlinkat_ex(td, uap->fd, uap->path, FD_NONE, uap->flag,
1874 #ifndef _SYS_SYSPROTO_H_
1875 struct funlinkat_args {
1883 sys_funlinkat(struct thread *td, struct funlinkat_args *uap)
1886 return (kern_funlinkat_ex(td, uap->dfd, uap->path, uap->fd, uap->flag,
1891 kern_funlinkat(struct thread *td, int dfd, const char *path, int fd,
1892 enum uio_seg pathseg, int flag, ino_t oldinum)
1897 struct nameidata nd;
1902 if (fd != FD_NONE) {
1903 error = getvnode_path(td, fd, &cap_no_rights, &fp);
1911 NDINIT_ATRIGHTS(&nd, DELETE, LOCKPARENT | LOCKLEAF | AUDITVNODE1 |
1912 at2cnpflags(flag, AT_RESOLVE_BENEATH),
1913 pathseg, path, dfd, &cap_unlinkat_rights, td);
1914 if ((error = namei(&nd)) != 0) {
1915 if (error == EINVAL)
1920 if (vp->v_type == VDIR && oldinum == 0) {
1921 error = EPERM; /* POSIX */
1922 } else if (oldinum != 0 &&
1923 ((error = VOP_STAT(vp, &sb, td->td_ucred, NOCRED, td)) == 0) &&
1924 sb.st_ino != oldinum) {
1925 error = EIDRM; /* Identifier removed */
1926 } else if (fp != NULL && fp->f_vnode != vp) {
1927 if (VN_IS_DOOMED(fp->f_vnode))
1933 * The root of a mounted filesystem cannot be deleted.
1935 * XXX: can this only be a VDIR case?
1937 if (vp->v_vflag & VV_ROOT)
1941 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1942 NDFREE(&nd, NDF_ONLY_PNBUF);
1944 if (vp == nd.ni_dvp)
1948 if ((error = vn_start_write(NULL, &mp,
1949 V_XSLEEP | PCATCH)) != 0) {
1955 error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp,
1960 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_UNLINK);
1961 error = VOP_REMOVE(nd.ni_dvp, vp, &nd.ni_cnd);
1965 vn_finished_write(mp);
1967 NDFREE(&nd, NDF_ONLY_PNBUF);
1969 if (vp == nd.ni_dvp)
1973 if (error == ERELOOKUP)
1982 * Reposition read/write file offset.
1984 #ifndef _SYS_SYSPROTO_H_
1993 sys_lseek(struct thread *td, struct lseek_args *uap)
1996 return (kern_lseek(td, uap->fd, uap->offset, uap->whence));
2000 kern_lseek(struct thread *td, int fd, off_t offset, int whence)
2006 error = fget(td, fd, &cap_seek_rights, &fp);
2009 error = (fp->f_ops->fo_flags & DFLAG_SEEKABLE) != 0 ?
2010 fo_seek(fp, offset, whence, td) : ESPIPE;
2015 #if defined(COMPAT_43)
2017 * Reposition read/write file offset.
2019 #ifndef _SYS_SYSPROTO_H_
2020 struct olseek_args {
2027 olseek(struct thread *td, struct olseek_args *uap)
2030 return (kern_lseek(td, uap->fd, uap->offset, uap->whence));
2032 #endif /* COMPAT_43 */
2034 #if defined(COMPAT_FREEBSD6)
2035 /* Version with the 'pad' argument */
2037 freebsd6_lseek(struct thread *td, struct freebsd6_lseek_args *uap)
2040 return (kern_lseek(td, uap->fd, uap->offset, uap->whence));
2045 * Check access permissions using passed credentials.
2048 vn_access(struct vnode *vp, int user_flags, struct ucred *cred,
2054 /* Flags == 0 means only check for existence. */
2055 if (user_flags == 0)
2059 if (user_flags & R_OK)
2061 if (user_flags & W_OK)
2063 if (user_flags & X_OK)
2066 error = mac_vnode_check_access(cred, vp, accmode);
2070 if ((accmode & VWRITE) == 0 || (error = vn_writechk(vp)) == 0)
2071 error = VOP_ACCESS(vp, accmode, cred, td);
2076 * Check access permissions using "real" credentials.
2078 #ifndef _SYS_SYSPROTO_H_
2079 struct access_args {
2085 sys_access(struct thread *td, struct access_args *uap)
2088 return (kern_accessat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2092 #ifndef _SYS_SYSPROTO_H_
2093 struct faccessat_args {
2101 sys_faccessat(struct thread *td, struct faccessat_args *uap)
2104 return (kern_accessat(td, uap->fd, uap->path, UIO_USERSPACE, uap->flag,
2109 kern_accessat(struct thread *td, int fd, const char *path,
2110 enum uio_seg pathseg, int flag, int amode)
2112 struct ucred *cred, *usecred;
2114 struct nameidata nd;
2117 if ((flag & ~(AT_EACCESS | AT_RESOLVE_BENEATH | AT_EMPTY_PATH)) != 0)
2119 if (amode != F_OK && (amode & ~(R_OK | W_OK | X_OK)) != 0)
2123 * Create and modify a temporary credential instead of one that
2124 * is potentially shared (if we need one).
2126 cred = td->td_ucred;
2127 if ((flag & AT_EACCESS) == 0 &&
2128 ((cred->cr_uid != cred->cr_ruid ||
2129 cred->cr_rgid != cred->cr_groups[0]))) {
2130 usecred = crdup(cred);
2131 usecred->cr_uid = cred->cr_ruid;
2132 usecred->cr_groups[0] = cred->cr_rgid;
2133 td->td_ucred = usecred;
2136 AUDIT_ARG_VALUE(amode);
2137 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF |
2138 AUDITVNODE1 | at2cnpflags(flag, AT_RESOLVE_BENEATH |
2139 AT_EMPTY_PATH), pathseg, path, fd, &cap_fstat_rights, td);
2140 if ((error = namei(&nd)) != 0)
2144 error = vn_access(vp, amode, usecred, td);
2145 NDFREE_NOTHING(&nd);
2148 if (usecred != cred) {
2149 td->td_ucred = cred;
2156 * Check access permissions using "effective" credentials.
2158 #ifndef _SYS_SYSPROTO_H_
2159 struct eaccess_args {
2165 sys_eaccess(struct thread *td, struct eaccess_args *uap)
2168 return (kern_accessat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2169 AT_EACCESS, uap->amode));
2172 #if defined(COMPAT_43)
2174 * Get file status; this version follows links.
2176 #ifndef _SYS_SYSPROTO_H_
2183 ostat(struct thread *td, struct ostat_args *uap)
2189 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE, &sb);
2193 return (copyout(&osb, uap->ub, sizeof (osb)));
2197 * Get file status; this version does not follow links.
2199 #ifndef _SYS_SYSPROTO_H_
2200 struct olstat_args {
2206 olstat(struct thread *td, struct olstat_args *uap)
2212 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2213 UIO_USERSPACE, &sb);
2217 return (copyout(&osb, uap->ub, sizeof (osb)));
2221 * Convert from an old to a new stat structure.
2222 * XXX: many values are blindly truncated.
2225 cvtstat(struct stat *st, struct ostat *ost)
2228 bzero(ost, sizeof(*ost));
2229 ost->st_dev = st->st_dev;
2230 ost->st_ino = st->st_ino;
2231 ost->st_mode = st->st_mode;
2232 ost->st_nlink = st->st_nlink;
2233 ost->st_uid = st->st_uid;
2234 ost->st_gid = st->st_gid;
2235 ost->st_rdev = st->st_rdev;
2236 ost->st_size = MIN(st->st_size, INT32_MAX);
2237 ost->st_atim = st->st_atim;
2238 ost->st_mtim = st->st_mtim;
2239 ost->st_ctim = st->st_ctim;
2240 ost->st_blksize = st->st_blksize;
2241 ost->st_blocks = st->st_blocks;
2242 ost->st_flags = st->st_flags;
2243 ost->st_gen = st->st_gen;
2245 #endif /* COMPAT_43 */
2247 #if defined(COMPAT_43) || defined(COMPAT_FREEBSD11)
2248 int ino64_trunc_error;
2249 SYSCTL_INT(_vfs, OID_AUTO, ino64_trunc_error, CTLFLAG_RW,
2250 &ino64_trunc_error, 0,
2251 "Error on truncation of device, file or inode number, or link count");
2254 freebsd11_cvtstat(struct stat *st, struct freebsd11_stat *ost)
2257 ost->st_dev = st->st_dev;
2258 if (ost->st_dev != st->st_dev) {
2259 switch (ino64_trunc_error) {
2262 * Since dev_t is almost raw, don't clamp to the
2263 * maximum for case 2, but ignore the error.
2270 ost->st_ino = st->st_ino;
2271 if (ost->st_ino != st->st_ino) {
2272 switch (ino64_trunc_error) {
2279 ost->st_ino = UINT32_MAX;
2283 ost->st_mode = st->st_mode;
2284 ost->st_nlink = st->st_nlink;
2285 if (ost->st_nlink != st->st_nlink) {
2286 switch (ino64_trunc_error) {
2293 ost->st_nlink = UINT16_MAX;
2297 ost->st_uid = st->st_uid;
2298 ost->st_gid = st->st_gid;
2299 ost->st_rdev = st->st_rdev;
2300 if (ost->st_rdev != st->st_rdev) {
2301 switch (ino64_trunc_error) {
2308 ost->st_atim = st->st_atim;
2309 ost->st_mtim = st->st_mtim;
2310 ost->st_ctim = st->st_ctim;
2311 ost->st_size = st->st_size;
2312 ost->st_blocks = st->st_blocks;
2313 ost->st_blksize = st->st_blksize;
2314 ost->st_flags = st->st_flags;
2315 ost->st_gen = st->st_gen;
2317 ost->st_birthtim = st->st_birthtim;
2318 bzero((char *)&ost->st_birthtim + sizeof(ost->st_birthtim),
2319 sizeof(*ost) - offsetof(struct freebsd11_stat,
2320 st_birthtim) - sizeof(ost->st_birthtim));
2325 freebsd11_stat(struct thread *td, struct freebsd11_stat_args* uap)
2328 struct freebsd11_stat osb;
2331 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE, &sb);
2334 error = freebsd11_cvtstat(&sb, &osb);
2336 error = copyout(&osb, uap->ub, sizeof(osb));
2341 freebsd11_lstat(struct thread *td, struct freebsd11_lstat_args* uap)
2344 struct freebsd11_stat osb;
2347 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2348 UIO_USERSPACE, &sb);
2351 error = freebsd11_cvtstat(&sb, &osb);
2353 error = copyout(&osb, uap->ub, sizeof(osb));
2358 freebsd11_fhstat(struct thread *td, struct freebsd11_fhstat_args* uap)
2362 struct freebsd11_stat osb;
2365 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
2368 error = kern_fhstat(td, fh, &sb);
2371 error = freebsd11_cvtstat(&sb, &osb);
2373 error = copyout(&osb, uap->sb, sizeof(osb));
2378 freebsd11_fstatat(struct thread *td, struct freebsd11_fstatat_args* uap)
2381 struct freebsd11_stat osb;
2384 error = kern_statat(td, uap->flag, uap->fd, uap->path,
2385 UIO_USERSPACE, &sb);
2388 error = freebsd11_cvtstat(&sb, &osb);
2390 error = copyout(&osb, uap->buf, sizeof(osb));
2393 #endif /* COMPAT_FREEBSD11 */
2398 #ifndef _SYS_SYSPROTO_H_
2399 struct fstatat_args {
2407 sys_fstatat(struct thread *td, struct fstatat_args *uap)
2412 error = kern_statat(td, uap->flag, uap->fd, uap->path,
2413 UIO_USERSPACE, &sb);
2415 error = copyout(&sb, uap->buf, sizeof (sb));
2420 kern_statat(struct thread *td, int flag, int fd, const char *path,
2421 enum uio_seg pathseg, struct stat *sbp)
2423 struct nameidata nd;
2426 if ((flag & ~(AT_SYMLINK_NOFOLLOW | AT_RESOLVE_BENEATH |
2427 AT_EMPTY_PATH)) != 0)
2430 NDINIT_ATRIGHTS(&nd, LOOKUP, at2cnpflags(flag, AT_RESOLVE_BENEATH |
2431 AT_SYMLINK_NOFOLLOW | AT_EMPTY_PATH) | LOCKSHARED | LOCKLEAF |
2432 AUDITVNODE1, pathseg, path, fd, &cap_fstat_rights, td);
2434 if ((error = namei(&nd)) != 0) {
2435 if (error == ENOTDIR &&
2436 (nd.ni_resflags & NIRES_EMPTYPATH) != 0)
2437 error = kern_fstat(td, fd, sbp);
2440 error = VOP_STAT(nd.ni_vp, sbp, td->td_ucred, NOCRED, td);
2441 NDFREE_NOTHING(&nd);
2443 #ifdef __STAT_TIME_T_EXT
2444 sbp->st_atim_ext = 0;
2445 sbp->st_mtim_ext = 0;
2446 sbp->st_ctim_ext = 0;
2447 sbp->st_btim_ext = 0;
2450 if (KTRPOINT(td, KTR_STRUCT))
2451 ktrstat_error(sbp, error);
2456 #if defined(COMPAT_FREEBSD11)
2458 * Implementation of the NetBSD [l]stat() functions.
2461 freebsd11_cvtnstat(struct stat *sb, struct nstat *nsb)
2464 bzero(nsb, sizeof(*nsb));
2465 nsb->st_dev = sb->st_dev;
2466 nsb->st_ino = sb->st_ino;
2467 nsb->st_mode = sb->st_mode;
2468 nsb->st_nlink = sb->st_nlink;
2469 nsb->st_uid = sb->st_uid;
2470 nsb->st_gid = sb->st_gid;
2471 nsb->st_rdev = sb->st_rdev;
2472 nsb->st_atim = sb->st_atim;
2473 nsb->st_mtim = sb->st_mtim;
2474 nsb->st_ctim = sb->st_ctim;
2475 nsb->st_size = sb->st_size;
2476 nsb->st_blocks = sb->st_blocks;
2477 nsb->st_blksize = sb->st_blksize;
2478 nsb->st_flags = sb->st_flags;
2479 nsb->st_gen = sb->st_gen;
2480 nsb->st_birthtim = sb->st_birthtim;
2483 #ifndef _SYS_SYSPROTO_H_
2484 struct freebsd11_nstat_args {
2490 freebsd11_nstat(struct thread *td, struct freebsd11_nstat_args *uap)
2496 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE, &sb);
2499 freebsd11_cvtnstat(&sb, &nsb);
2500 return (copyout(&nsb, uap->ub, sizeof (nsb)));
2504 * NetBSD lstat. Get file status; this version does not follow links.
2506 #ifndef _SYS_SYSPROTO_H_
2507 struct freebsd11_nlstat_args {
2513 freebsd11_nlstat(struct thread *td, struct freebsd11_nlstat_args *uap)
2519 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2520 UIO_USERSPACE, &sb);
2523 freebsd11_cvtnstat(&sb, &nsb);
2524 return (copyout(&nsb, uap->ub, sizeof (nsb)));
2526 #endif /* COMPAT_FREEBSD11 */
2529 * Get configurable pathname variables.
2531 #ifndef _SYS_SYSPROTO_H_
2532 struct pathconf_args {
2538 sys_pathconf(struct thread *td, struct pathconf_args *uap)
2543 error = kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name, FOLLOW,
2546 td->td_retval[0] = value;
2550 #ifndef _SYS_SYSPROTO_H_
2551 struct lpathconf_args {
2557 sys_lpathconf(struct thread *td, struct lpathconf_args *uap)
2562 error = kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name,
2565 td->td_retval[0] = value;
2570 kern_pathconf(struct thread *td, const char *path, enum uio_seg pathseg,
2571 int name, u_long flags, long *valuep)
2573 struct nameidata nd;
2576 NDINIT(&nd, LOOKUP, LOCKSHARED | LOCKLEAF | AUDITVNODE1 | flags,
2578 if ((error = namei(&nd)) != 0)
2580 NDFREE_NOTHING(&nd);
2582 error = VOP_PATHCONF(nd.ni_vp, name, valuep);
2588 * Return target name of a symbolic link.
2590 #ifndef _SYS_SYSPROTO_H_
2591 struct readlink_args {
2598 sys_readlink(struct thread *td, struct readlink_args *uap)
2601 return (kern_readlinkat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2602 uap->buf, UIO_USERSPACE, uap->count));
2604 #ifndef _SYS_SYSPROTO_H_
2605 struct readlinkat_args {
2613 sys_readlinkat(struct thread *td, struct readlinkat_args *uap)
2616 return (kern_readlinkat(td, uap->fd, uap->path, UIO_USERSPACE,
2617 uap->buf, UIO_USERSPACE, uap->bufsize));
2621 kern_readlinkat(struct thread *td, int fd, const char *path,
2622 enum uio_seg pathseg, char *buf, enum uio_seg bufseg, size_t count)
2625 struct nameidata nd;
2628 if (count > IOSIZE_MAX)
2631 NDINIT_AT(&nd, LOOKUP, NOFOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1 |
2632 EMPTYPATH, pathseg, path, fd, td);
2634 if ((error = namei(&nd)) != 0)
2636 NDFREE_NOTHING(&nd);
2639 error = kern_readlink_vp(vp, buf, bufseg, count, td);
2646 * Helper function to readlink from a vnode
2649 kern_readlink_vp(struct vnode *vp, char *buf, enum uio_seg bufseg, size_t count,
2656 ASSERT_VOP_LOCKED(vp, "kern_readlink_vp(): vp not locked");
2658 error = mac_vnode_check_readlink(td->td_ucred, vp);
2662 if (vp->v_type != VLNK && (vp->v_vflag & VV_READLINK) == 0)
2665 aiov.iov_base = buf;
2666 aiov.iov_len = count;
2667 auio.uio_iov = &aiov;
2668 auio.uio_iovcnt = 1;
2669 auio.uio_offset = 0;
2670 auio.uio_rw = UIO_READ;
2671 auio.uio_segflg = bufseg;
2673 auio.uio_resid = count;
2674 error = VOP_READLINK(vp, &auio, td->td_ucred);
2675 td->td_retval[0] = count - auio.uio_resid;
2680 * Common implementation code for chflags() and fchflags().
2683 setfflags(struct thread *td, struct vnode *vp, u_long flags)
2689 /* We can't support the value matching VNOVAL. */
2690 if (flags == VNOVAL)
2691 return (EOPNOTSUPP);
2694 * Prevent non-root users from setting flags on devices. When
2695 * a device is reused, users can retain ownership of the device
2696 * if they are allowed to set flags and programs assume that
2697 * chown can't fail when done as root.
2699 if (vp->v_type == VCHR || vp->v_type == VBLK) {
2700 error = priv_check(td, PRIV_VFS_CHFLAGS_DEV);
2705 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2708 vattr.va_flags = flags;
2709 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2711 error = mac_vnode_check_setflags(td->td_ucred, vp, vattr.va_flags);
2714 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
2716 vn_finished_write(mp);
2721 * Change flags of a file given a path name.
2723 #ifndef _SYS_SYSPROTO_H_
2724 struct chflags_args {
2730 sys_chflags(struct thread *td, struct chflags_args *uap)
2733 return (kern_chflagsat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2737 #ifndef _SYS_SYSPROTO_H_
2738 struct chflagsat_args {
2746 sys_chflagsat(struct thread *td, struct chflagsat_args *uap)
2749 return (kern_chflagsat(td, uap->fd, uap->path, UIO_USERSPACE,
2750 uap->flags, uap->atflag));
2754 * Same as chflags() but doesn't follow symlinks.
2756 #ifndef _SYS_SYSPROTO_H_
2757 struct lchflags_args {
2763 sys_lchflags(struct thread *td, struct lchflags_args *uap)
2766 return (kern_chflagsat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2767 uap->flags, AT_SYMLINK_NOFOLLOW));
2771 kern_chflagsat(struct thread *td, int fd, const char *path,
2772 enum uio_seg pathseg, u_long flags, int atflag)
2774 struct nameidata nd;
2777 if ((atflag & ~(AT_SYMLINK_NOFOLLOW | AT_RESOLVE_BENEATH |
2778 AT_EMPTY_PATH)) != 0)
2781 AUDIT_ARG_FFLAGS(flags);
2782 NDINIT_ATRIGHTS(&nd, LOOKUP, at2cnpflags(atflag, AT_SYMLINK_NOFOLLOW |
2783 AT_RESOLVE_BENEATH | AT_EMPTY_PATH) | AUDITVNODE1, pathseg, path,
2784 fd, &cap_fchflags_rights, td);
2785 if ((error = namei(&nd)) != 0)
2787 NDFREE_NOTHING(&nd);
2788 error = setfflags(td, nd.ni_vp, flags);
2794 * Change flags of a file given a file descriptor.
2796 #ifndef _SYS_SYSPROTO_H_
2797 struct fchflags_args {
2803 sys_fchflags(struct thread *td, struct fchflags_args *uap)
2808 AUDIT_ARG_FD(uap->fd);
2809 AUDIT_ARG_FFLAGS(uap->flags);
2810 error = getvnode(td, uap->fd, &cap_fchflags_rights,
2815 if (AUDITING_TD(td)) {
2816 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
2817 AUDIT_ARG_VNODE1(fp->f_vnode);
2818 VOP_UNLOCK(fp->f_vnode);
2821 error = setfflags(td, fp->f_vnode, uap->flags);
2827 * Common implementation code for chmod(), lchmod() and fchmod().
2830 setfmode(struct thread *td, struct ucred *cred, struct vnode *vp, int mode)
2836 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2838 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2840 vattr.va_mode = mode & ALLPERMS;
2842 error = mac_vnode_check_setmode(cred, vp, vattr.va_mode);
2845 error = VOP_SETATTR(vp, &vattr, cred);
2847 vn_finished_write(mp);
2852 * Change mode of a file given path name.
2854 #ifndef _SYS_SYSPROTO_H_
2861 sys_chmod(struct thread *td, struct chmod_args *uap)
2864 return (kern_fchmodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2868 #ifndef _SYS_SYSPROTO_H_
2869 struct fchmodat_args {
2877 sys_fchmodat(struct thread *td, struct fchmodat_args *uap)
2880 return (kern_fchmodat(td, uap->fd, uap->path, UIO_USERSPACE,
2881 uap->mode, uap->flag));
2885 * Change mode of a file given path name (don't follow links.)
2887 #ifndef _SYS_SYSPROTO_H_
2888 struct lchmod_args {
2894 sys_lchmod(struct thread *td, struct lchmod_args *uap)
2897 return (kern_fchmodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2898 uap->mode, AT_SYMLINK_NOFOLLOW));
2902 kern_fchmodat(struct thread *td, int fd, const char *path,
2903 enum uio_seg pathseg, mode_t mode, int flag)
2905 struct nameidata nd;
2908 if ((flag & ~(AT_SYMLINK_NOFOLLOW | AT_RESOLVE_BENEATH |
2909 AT_EMPTY_PATH)) != 0)
2912 AUDIT_ARG_MODE(mode);
2913 NDINIT_ATRIGHTS(&nd, LOOKUP, at2cnpflags(flag, AT_SYMLINK_NOFOLLOW |
2914 AT_RESOLVE_BENEATH | AT_EMPTY_PATH) | AUDITVNODE1, pathseg, path,
2915 fd, &cap_fchmod_rights, td);
2916 if ((error = namei(&nd)) != 0)
2918 NDFREE_NOTHING(&nd);
2919 error = setfmode(td, td->td_ucred, nd.ni_vp, mode);
2925 * Change mode of a file given a file descriptor.
2927 #ifndef _SYS_SYSPROTO_H_
2928 struct fchmod_args {
2934 sys_fchmod(struct thread *td, struct fchmod_args *uap)
2939 AUDIT_ARG_FD(uap->fd);
2940 AUDIT_ARG_MODE(uap->mode);
2942 error = fget(td, uap->fd, &cap_fchmod_rights, &fp);
2945 error = fo_chmod(fp, uap->mode, td->td_ucred, td);
2951 * Common implementation for chown(), lchown(), and fchown()
2954 setfown(struct thread *td, struct ucred *cred, struct vnode *vp, uid_t uid,
2961 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2963 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2968 error = mac_vnode_check_setowner(cred, vp, vattr.va_uid,
2972 error = VOP_SETATTR(vp, &vattr, cred);
2974 vn_finished_write(mp);
2979 * Set ownership given a path name.
2981 #ifndef _SYS_SYSPROTO_H_
2989 sys_chown(struct thread *td, struct chown_args *uap)
2992 return (kern_fchownat(td, AT_FDCWD, uap->path, UIO_USERSPACE, uap->uid,
2996 #ifndef _SYS_SYSPROTO_H_
2997 struct fchownat_args {
3006 sys_fchownat(struct thread *td, struct fchownat_args *uap)
3009 return (kern_fchownat(td, uap->fd, uap->path, UIO_USERSPACE, uap->uid,
3010 uap->gid, uap->flag));
3014 kern_fchownat(struct thread *td, int fd, const char *path,
3015 enum uio_seg pathseg, int uid, int gid, int flag)
3017 struct nameidata nd;
3020 if ((flag & ~(AT_SYMLINK_NOFOLLOW | AT_RESOLVE_BENEATH |
3021 AT_EMPTY_PATH)) != 0)
3024 AUDIT_ARG_OWNER(uid, gid);
3025 NDINIT_ATRIGHTS(&nd, LOOKUP, at2cnpflags(flag, AT_SYMLINK_NOFOLLOW |
3026 AT_RESOLVE_BENEATH | AT_EMPTY_PATH) | AUDITVNODE1, pathseg, path,
3027 fd, &cap_fchown_rights, td);
3029 if ((error = namei(&nd)) != 0)
3031 NDFREE_NOTHING(&nd);
3032 error = setfown(td, td->td_ucred, nd.ni_vp, uid, gid);
3038 * Set ownership given a path name, do not cross symlinks.
3040 #ifndef _SYS_SYSPROTO_H_
3041 struct lchown_args {
3048 sys_lchown(struct thread *td, struct lchown_args *uap)
3051 return (kern_fchownat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
3052 uap->uid, uap->gid, AT_SYMLINK_NOFOLLOW));
3056 * Set ownership given a file descriptor.
3058 #ifndef _SYS_SYSPROTO_H_
3059 struct fchown_args {
3066 sys_fchown(struct thread *td, struct fchown_args *uap)
3071 AUDIT_ARG_FD(uap->fd);
3072 AUDIT_ARG_OWNER(uap->uid, uap->gid);
3073 error = fget(td, uap->fd, &cap_fchown_rights, &fp);
3076 error = fo_chown(fp, uap->uid, uap->gid, td->td_ucred, td);
3082 * Common implementation code for utimes(), lutimes(), and futimes().
3085 getutimes(const struct timeval *usrtvp, enum uio_seg tvpseg,
3086 struct timespec *tsp)
3088 struct timeval tv[2];
3089 const struct timeval *tvp;
3092 if (usrtvp == NULL) {
3093 vfs_timestamp(&tsp[0]);
3096 if (tvpseg == UIO_SYSSPACE) {
3099 if ((error = copyin(usrtvp, tv, sizeof(tv))) != 0)
3104 if (tvp[0].tv_usec < 0 || tvp[0].tv_usec >= 1000000 ||
3105 tvp[1].tv_usec < 0 || tvp[1].tv_usec >= 1000000)
3107 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
3108 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
3114 * Common implementation code for futimens(), utimensat().
3116 #define UTIMENS_NULL 0x1
3117 #define UTIMENS_EXIT 0x2
3119 getutimens(const struct timespec *usrtsp, enum uio_seg tspseg,
3120 struct timespec *tsp, int *retflags)
3122 struct timespec tsnow;
3125 vfs_timestamp(&tsnow);
3127 if (usrtsp == NULL) {
3130 *retflags |= UTIMENS_NULL;
3133 if (tspseg == UIO_SYSSPACE) {
3136 } else if ((error = copyin(usrtsp, tsp, sizeof(*tsp) * 2)) != 0)
3138 if (tsp[0].tv_nsec == UTIME_OMIT && tsp[1].tv_nsec == UTIME_OMIT)
3139 *retflags |= UTIMENS_EXIT;
3140 if (tsp[0].tv_nsec == UTIME_NOW && tsp[1].tv_nsec == UTIME_NOW)
3141 *retflags |= UTIMENS_NULL;
3142 if (tsp[0].tv_nsec == UTIME_OMIT)
3143 tsp[0].tv_sec = VNOVAL;
3144 else if (tsp[0].tv_nsec == UTIME_NOW)
3146 else if (tsp[0].tv_nsec < 0 || tsp[0].tv_nsec >= 1000000000L)
3148 if (tsp[1].tv_nsec == UTIME_OMIT)
3149 tsp[1].tv_sec = VNOVAL;
3150 else if (tsp[1].tv_nsec == UTIME_NOW)
3152 else if (tsp[1].tv_nsec < 0 || tsp[1].tv_nsec >= 1000000000L)
3159 * Common implementation code for utimes(), lutimes(), futimes(), futimens(),
3163 setutimes(struct thread *td, struct vnode *vp, const struct timespec *ts,
3164 int numtimes, int nullflag)
3171 setbirthtime = false;
3172 vattr.va_birthtime.tv_sec = VNOVAL;
3173 vattr.va_birthtime.tv_nsec = 0;
3175 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
3177 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3178 if (numtimes < 3 && VOP_GETATTR(vp, &vattr, td->td_ucred) == 0 &&
3179 timespeccmp(&ts[1], &vattr.va_birthtime, < ))
3180 setbirthtime = true;
3182 vattr.va_atime = ts[0];
3183 vattr.va_mtime = ts[1];
3185 vattr.va_birthtime = ts[1];
3187 vattr.va_birthtime = ts[2];
3189 vattr.va_vaflags |= VA_UTIMES_NULL;
3191 error = mac_vnode_check_setutimes(td->td_ucred, vp, vattr.va_atime,
3195 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3197 vn_finished_write(mp);
3202 * Set the access and modification times of a file.
3204 #ifndef _SYS_SYSPROTO_H_
3205 struct utimes_args {
3207 struct timeval *tptr;
3211 sys_utimes(struct thread *td, struct utimes_args *uap)
3214 return (kern_utimesat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
3215 uap->tptr, UIO_USERSPACE));
3218 #ifndef _SYS_SYSPROTO_H_
3219 struct futimesat_args {
3222 const struct timeval * times;
3226 sys_futimesat(struct thread *td, struct futimesat_args *uap)
3229 return (kern_utimesat(td, uap->fd, uap->path, UIO_USERSPACE,
3230 uap->times, UIO_USERSPACE));
3234 kern_utimesat(struct thread *td, int fd, const char *path,
3235 enum uio_seg pathseg, struct timeval *tptr, enum uio_seg tptrseg)
3237 struct nameidata nd;
3238 struct timespec ts[2];
3241 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3243 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, fd,
3244 &cap_futimes_rights, td);
3246 if ((error = namei(&nd)) != 0)
3248 NDFREE_NOTHING(&nd);
3249 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3255 * Set the access and modification times of a file.
3257 #ifndef _SYS_SYSPROTO_H_
3258 struct lutimes_args {
3260 struct timeval *tptr;
3264 sys_lutimes(struct thread *td, struct lutimes_args *uap)
3267 return (kern_lutimes(td, uap->path, UIO_USERSPACE, uap->tptr,
3272 kern_lutimes(struct thread *td, const char *path, enum uio_seg pathseg,
3273 struct timeval *tptr, enum uio_seg tptrseg)
3275 struct timespec ts[2];
3276 struct nameidata nd;
3279 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3281 NDINIT(&nd, LOOKUP, NOFOLLOW | AUDITVNODE1, pathseg, path, td);
3282 if ((error = namei(&nd)) != 0)
3284 NDFREE_NOTHING(&nd);
3285 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3291 * Set the access and modification times of a file.
3293 #ifndef _SYS_SYSPROTO_H_
3294 struct futimes_args {
3296 struct timeval *tptr;
3300 sys_futimes(struct thread *td, struct futimes_args *uap)
3303 return (kern_futimes(td, uap->fd, uap->tptr, UIO_USERSPACE));
3307 kern_futimes(struct thread *td, int fd, struct timeval *tptr,
3308 enum uio_seg tptrseg)
3310 struct timespec ts[2];
3315 error = getutimes(tptr, tptrseg, ts);
3318 error = getvnode(td, fd, &cap_futimes_rights, &fp);
3322 if (AUDITING_TD(td)) {
3323 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
3324 AUDIT_ARG_VNODE1(fp->f_vnode);
3325 VOP_UNLOCK(fp->f_vnode);
3328 error = setutimes(td, fp->f_vnode, ts, 2, tptr == NULL);
3334 sys_futimens(struct thread *td, struct futimens_args *uap)
3337 return (kern_futimens(td, uap->fd, uap->times, UIO_USERSPACE));
3341 kern_futimens(struct thread *td, int fd, struct timespec *tptr,
3342 enum uio_seg tptrseg)
3344 struct timespec ts[2];
3349 error = getutimens(tptr, tptrseg, ts, &flags);
3352 if (flags & UTIMENS_EXIT)
3354 error = getvnode(td, fd, &cap_futimes_rights, &fp);
3358 if (AUDITING_TD(td)) {
3359 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
3360 AUDIT_ARG_VNODE1(fp->f_vnode);
3361 VOP_UNLOCK(fp->f_vnode);
3364 error = setutimes(td, fp->f_vnode, ts, 2, flags & UTIMENS_NULL);
3370 sys_utimensat(struct thread *td, struct utimensat_args *uap)
3373 return (kern_utimensat(td, uap->fd, uap->path, UIO_USERSPACE,
3374 uap->times, UIO_USERSPACE, uap->flag));
3378 kern_utimensat(struct thread *td, int fd, const char *path,
3379 enum uio_seg pathseg, struct timespec *tptr, enum uio_seg tptrseg,
3382 struct nameidata nd;
3383 struct timespec ts[2];
3386 if ((flag & ~(AT_SYMLINK_NOFOLLOW | AT_RESOLVE_BENEATH |
3387 AT_EMPTY_PATH)) != 0)
3390 if ((error = getutimens(tptr, tptrseg, ts, &flags)) != 0)
3392 NDINIT_ATRIGHTS(&nd, LOOKUP, at2cnpflags(flag, AT_SYMLINK_NOFOLLOW |
3393 AT_RESOLVE_BENEATH | AT_EMPTY_PATH) | AUDITVNODE1,
3394 pathseg, path, fd, &cap_futimes_rights, td);
3395 if ((error = namei(&nd)) != 0)
3398 * We are allowed to call namei() regardless of 2xUTIME_OMIT.
3400 * "If both tv_nsec fields are UTIME_OMIT... EACCESS may be detected."
3401 * "Search permission is denied by a component of the path prefix."
3403 NDFREE_NOTHING(&nd);
3404 if ((flags & UTIMENS_EXIT) == 0)
3405 error = setutimes(td, nd.ni_vp, ts, 2, flags & UTIMENS_NULL);
3411 * Truncate a file given its path name.
3413 #ifndef _SYS_SYSPROTO_H_
3414 struct truncate_args {
3421 sys_truncate(struct thread *td, struct truncate_args *uap)
3424 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3428 kern_truncate(struct thread *td, const char *path, enum uio_seg pathseg,
3435 struct nameidata nd;
3442 NDINIT(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, td);
3443 if ((error = namei(&nd)) != 0)
3446 rl_cookie = vn_rangelock_wlock(vp, 0, OFF_MAX);
3447 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0) {
3448 vn_rangelock_unlock(vp, rl_cookie);
3452 NDFREE(&nd, NDF_ONLY_PNBUF);
3453 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3454 if (vp->v_type == VDIR)
3457 else if ((error = mac_vnode_check_write(td->td_ucred, NOCRED, vp))) {
3460 else if ((error = vn_writechk(vp)) == 0 &&
3461 (error = VOP_ACCESS(vp, VWRITE, td->td_ucred, td)) == 0) {
3463 vattr.va_size = length;
3464 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3467 vn_finished_write(mp);
3468 vn_rangelock_unlock(vp, rl_cookie);
3470 if (error == ERELOOKUP)
3475 #if defined(COMPAT_43)
3477 * Truncate a file given its path name.
3479 #ifndef _SYS_SYSPROTO_H_
3480 struct otruncate_args {
3486 otruncate(struct thread *td, struct otruncate_args *uap)
3489 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3491 #endif /* COMPAT_43 */
3493 #if defined(COMPAT_FREEBSD6)
3494 /* Versions with the pad argument */
3496 freebsd6_truncate(struct thread *td, struct freebsd6_truncate_args *uap)
3499 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3503 freebsd6_ftruncate(struct thread *td, struct freebsd6_ftruncate_args *uap)
3506 return (kern_ftruncate(td, uap->fd, uap->length));
3511 kern_fsync(struct thread *td, int fd, bool fullsync)
3519 error = getvnode(td, fd, &cap_fsync_rights, &fp);
3525 /* XXXKIB: compete outstanding aio writes */;
3528 error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
3531 vn_lock(vp, vn_lktype_write(mp, vp) | LK_RETRY);
3532 AUDIT_ARG_VNODE1(vp);
3533 vnode_pager_clean_async(vp);
3534 error = fullsync ? VOP_FSYNC(vp, MNT_WAIT, td) : VOP_FDATASYNC(vp, td);
3536 vn_finished_write(mp);
3537 if (error == ERELOOKUP)
3545 * Sync an open file.
3547 #ifndef _SYS_SYSPROTO_H_
3553 sys_fsync(struct thread *td, struct fsync_args *uap)
3556 return (kern_fsync(td, uap->fd, true));
3560 sys_fdatasync(struct thread *td, struct fdatasync_args *uap)
3563 return (kern_fsync(td, uap->fd, false));
3567 * Rename files. Source and destination must either both be directories, or
3568 * both not be directories. If target is a directory, it must be empty.
3570 #ifndef _SYS_SYSPROTO_H_
3571 struct rename_args {
3577 sys_rename(struct thread *td, struct rename_args *uap)
3580 return (kern_renameat(td, AT_FDCWD, uap->from, AT_FDCWD,
3581 uap->to, UIO_USERSPACE));
3584 #ifndef _SYS_SYSPROTO_H_
3585 struct renameat_args {
3593 sys_renameat(struct thread *td, struct renameat_args *uap)
3596 return (kern_renameat(td, uap->oldfd, uap->old, uap->newfd, uap->new,
3602 kern_renameat_mac(struct thread *td, int oldfd, const char *old, int newfd,
3603 const char *new, enum uio_seg pathseg, struct nameidata *fromnd)
3607 NDINIT_ATRIGHTS(fromnd, DELETE, LOCKPARENT | LOCKLEAF | SAVESTART |
3608 AUDITVNODE1, pathseg, old, oldfd, &cap_renameat_source_rights, td);
3609 if ((error = namei(fromnd)) != 0)
3611 error = mac_vnode_check_rename_from(td->td_ucred, fromnd->ni_dvp,
3612 fromnd->ni_vp, &fromnd->ni_cnd);
3613 VOP_UNLOCK(fromnd->ni_dvp);
3614 if (fromnd->ni_dvp != fromnd->ni_vp)
3615 VOP_UNLOCK(fromnd->ni_vp);
3617 NDFREE(fromnd, NDF_ONLY_PNBUF);
3618 vrele(fromnd->ni_dvp);
3619 vrele(fromnd->ni_vp);
3620 if (fromnd->ni_startdir)
3621 vrele(fromnd->ni_startdir);
3628 kern_renameat(struct thread *td, int oldfd, const char *old, int newfd,
3629 const char *new, enum uio_seg pathseg)
3631 struct mount *mp = NULL;
3632 struct vnode *tvp, *fvp, *tdvp;
3633 struct nameidata fromnd, tond;
3634 u_int64_t tondflags;
3640 if (mac_vnode_check_rename_from_enabled()) {
3641 error = kern_renameat_mac(td, oldfd, old, newfd, new, pathseg,
3647 NDINIT_ATRIGHTS(&fromnd, DELETE, WANTPARENT | SAVESTART | AUDITVNODE1,
3648 pathseg, old, oldfd, &cap_renameat_source_rights, td);
3649 if ((error = namei(&fromnd)) != 0)
3655 tondflags = LOCKPARENT | LOCKLEAF | NOCACHE | SAVESTART | AUDITVNODE2;
3656 if (fromnd.ni_vp->v_type == VDIR)
3657 tondflags |= WILLBEDIR;
3658 NDINIT_ATRIGHTS(&tond, RENAME, tondflags, pathseg, new, newfd,
3659 &cap_renameat_target_rights, td);
3660 if ((error = namei(&tond)) != 0) {
3661 /* Translate error code for rename("dir1", "dir2/."). */
3662 if (error == EISDIR && fvp->v_type == VDIR)
3664 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3665 vrele(fromnd.ni_dvp);
3671 error = vn_start_write(fvp, &mp, V_NOWAIT);
3673 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3674 NDFREE(&tond, NDF_ONLY_PNBUF);
3681 vrele(fromnd.ni_dvp);
3683 vrele(tond.ni_startdir);
3684 if (fromnd.ni_startdir != NULL)
3685 vrele(fromnd.ni_startdir);
3686 error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH);
3692 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
3695 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
3700 if (newfd != AT_FDCWD && (tond.ni_resflags & NIRES_ABS) == 0) {
3702 * If the target already exists we require CAP_UNLINKAT
3703 * from 'newfd', when newfd was used for the lookup.
3705 error = cap_check(&tond.ni_filecaps.fc_rights,
3706 &cap_unlinkat_rights);
3717 * If the source is the same as the destination (that is, if they
3718 * are links to the same vnode), then there is nothing to do.
3724 error = mac_vnode_check_rename_to(td->td_ucred, tdvp,
3725 tond.ni_vp, fromnd.ni_dvp == tdvp, &tond.ni_cnd);
3729 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
3730 tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
3731 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3732 NDFREE(&tond, NDF_ONLY_PNBUF);
3734 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3735 NDFREE(&tond, NDF_ONLY_PNBUF);
3742 vrele(fromnd.ni_dvp);
3745 vrele(tond.ni_startdir);
3746 vn_finished_write(mp);
3748 if (fromnd.ni_startdir)
3749 vrele(fromnd.ni_startdir);
3750 if (error == ERESTART)
3752 if (error == ERELOOKUP)
3758 * Make a directory file.
3760 #ifndef _SYS_SYSPROTO_H_
3767 sys_mkdir(struct thread *td, struct mkdir_args *uap)
3770 return (kern_mkdirat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
3774 #ifndef _SYS_SYSPROTO_H_
3775 struct mkdirat_args {
3782 sys_mkdirat(struct thread *td, struct mkdirat_args *uap)
3785 return (kern_mkdirat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode));
3789 kern_mkdirat(struct thread *td, int fd, const char *path, enum uio_seg segflg,
3794 struct nameidata nd;
3797 AUDIT_ARG_MODE(mode);
3801 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
3802 NC_NOMAKEENTRY | NC_KEEPPOSENTRY | FAILIFEXISTS | WILLBEDIR,
3803 segflg, path, fd, &cap_mkdirat_rights, td);
3804 if ((error = namei(&nd)) != 0)
3806 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3807 NDFREE(&nd, NDF_ONLY_PNBUF);
3809 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3814 vattr.va_type = VDIR;
3815 vattr.va_mode = (mode & ACCESSPERMS) &~ td->td_proc->p_pd->pd_cmask;
3817 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
3822 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
3826 NDFREE(&nd, NDF_ONLY_PNBUF);
3827 VOP_VPUT_PAIR(nd.ni_dvp, error == 0 ? &nd.ni_vp : NULL, true);
3828 vn_finished_write(mp);
3829 if (error == ERELOOKUP)
3835 * Remove a directory file.
3837 #ifndef _SYS_SYSPROTO_H_
3843 sys_rmdir(struct thread *td, struct rmdir_args *uap)
3846 return (kern_frmdirat(td, AT_FDCWD, uap->path, FD_NONE, UIO_USERSPACE,
3851 kern_frmdirat(struct thread *td, int dfd, const char *path, int fd,
3852 enum uio_seg pathseg, int flag)
3857 struct nameidata nd;
3858 cap_rights_t rights;
3862 if (fd != FD_NONE) {
3863 error = getvnode(td, fd, cap_rights_init_one(&rights,
3872 NDINIT_ATRIGHTS(&nd, DELETE, LOCKPARENT | LOCKLEAF | AUDITVNODE1 |
3873 at2cnpflags(flag, AT_RESOLVE_BENEATH),
3874 pathseg, path, dfd, &cap_unlinkat_rights, td);
3875 if ((error = namei(&nd)) != 0)
3878 if (vp->v_type != VDIR) {
3883 * No rmdir "." please.
3885 if (nd.ni_dvp == vp) {
3890 * The root of a mounted filesystem cannot be deleted.
3892 if (vp->v_vflag & VV_ROOT) {
3897 if (fp != NULL && fp->f_vnode != vp) {
3898 if (VN_IS_DOOMED(fp->f_vnode))
3906 error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp,
3911 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3912 NDFREE(&nd, NDF_ONLY_PNBUF);
3914 if (nd.ni_dvp == vp)
3918 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3922 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_UNLINK);
3923 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
3924 vn_finished_write(mp);
3926 NDFREE(&nd, NDF_ONLY_PNBUF);
3928 if (nd.ni_dvp == vp)
3932 if (error == ERELOOKUP)
3940 #if defined(COMPAT_43) || defined(COMPAT_FREEBSD11)
3942 freebsd11_kern_getdirentries(struct thread *td, int fd, char *ubuf, u_int count,
3943 long *basep, void (*func)(struct freebsd11_dirent *))
3945 struct freebsd11_dirent dstdp;
3946 struct dirent *dp, *edp;
3949 ssize_t resid, ucount;
3952 /* XXX arbitrary sanity limit on `count'. */
3953 count = min(count, 64 * 1024);
3955 dirbuf = malloc(count, M_TEMP, M_WAITOK);
3957 error = kern_getdirentries(td, fd, dirbuf, count, &base, &resid,
3965 for (dp = (struct dirent *)dirbuf,
3966 edp = (struct dirent *)&dirbuf[count - resid];
3967 ucount < count && dp < edp; ) {
3968 if (dp->d_reclen == 0)
3970 MPASS(dp->d_reclen >= _GENERIC_DIRLEN(0));
3971 if (dp->d_namlen >= sizeof(dstdp.d_name))
3973 dstdp.d_type = dp->d_type;
3974 dstdp.d_namlen = dp->d_namlen;
3975 dstdp.d_fileno = dp->d_fileno; /* truncate */
3976 if (dstdp.d_fileno != dp->d_fileno) {
3977 switch (ino64_trunc_error) {
3985 dstdp.d_fileno = UINT32_MAX;
3989 dstdp.d_reclen = sizeof(dstdp) - sizeof(dstdp.d_name) +
3990 ((dp->d_namlen + 1 + 3) &~ 3);
3991 bcopy(dp->d_name, dstdp.d_name, dstdp.d_namlen);
3992 bzero(dstdp.d_name + dstdp.d_namlen,
3993 dstdp.d_reclen - offsetof(struct freebsd11_dirent, d_name) -
3995 MPASS(dstdp.d_reclen <= dp->d_reclen);
3996 MPASS(ucount + dstdp.d_reclen <= count);
3999 error = copyout(&dstdp, ubuf + ucount, dstdp.d_reclen);
4002 dp = (struct dirent *)((char *)dp + dp->d_reclen);
4003 ucount += dstdp.d_reclen;
4007 free(dirbuf, M_TEMP);
4009 td->td_retval[0] = ucount;
4016 ogetdirentries_cvt(struct freebsd11_dirent *dp)
4018 #if (BYTE_ORDER == LITTLE_ENDIAN)
4020 * The expected low byte of dp->d_namlen is our dp->d_type.
4021 * The high MBZ byte of dp->d_namlen is our dp->d_namlen.
4023 dp->d_type = dp->d_namlen;
4027 * The dp->d_type is the high byte of the expected dp->d_namlen,
4028 * so must be zero'ed.
4035 * Read a block of directory entries in a filesystem independent format.
4037 #ifndef _SYS_SYSPROTO_H_
4038 struct ogetdirentries_args {
4046 ogetdirentries(struct thread *td, struct ogetdirentries_args *uap)
4051 error = kern_ogetdirentries(td, uap, &loff);
4053 error = copyout(&loff, uap->basep, sizeof(long));
4058 kern_ogetdirentries(struct thread *td, struct ogetdirentries_args *uap,
4064 /* XXX arbitrary sanity limit on `count'. */
4065 if (uap->count > 64 * 1024)
4068 error = freebsd11_kern_getdirentries(td, uap->fd, uap->buf, uap->count,
4069 &base, ogetdirentries_cvt);
4071 if (error == 0 && uap->basep != NULL)
4072 error = copyout(&base, uap->basep, sizeof(long));
4076 #endif /* COMPAT_43 */
4078 #if defined(COMPAT_FREEBSD11)
4079 #ifndef _SYS_SYSPROTO_H_
4080 struct freebsd11_getdirentries_args {
4088 freebsd11_getdirentries(struct thread *td,
4089 struct freebsd11_getdirentries_args *uap)
4094 error = freebsd11_kern_getdirentries(td, uap->fd, uap->buf, uap->count,
4097 if (error == 0 && uap->basep != NULL)
4098 error = copyout(&base, uap->basep, sizeof(long));
4103 freebsd11_getdents(struct thread *td, struct freebsd11_getdents_args *uap)
4105 struct freebsd11_getdirentries_args ap;
4109 ap.count = uap->count;
4111 return (freebsd11_getdirentries(td, &ap));
4113 #endif /* COMPAT_FREEBSD11 */
4116 * Read a block of directory entries in a filesystem independent format.
4119 sys_getdirentries(struct thread *td, struct getdirentries_args *uap)
4124 error = kern_getdirentries(td, uap->fd, uap->buf, uap->count, &base,
4125 NULL, UIO_USERSPACE);
4128 if (uap->basep != NULL)
4129 error = copyout(&base, uap->basep, sizeof(off_t));
4134 kern_getdirentries(struct thread *td, int fd, char *buf, size_t count,
4135 off_t *basep, ssize_t *residp, enum uio_seg bufseg)
4146 if (count > IOSIZE_MAX)
4148 auio.uio_resid = count;
4149 error = getvnode(td, fd, &cap_read_rights, &fp);
4152 if ((fp->f_flag & FREAD) == 0) {
4157 foffset = foffset_lock(fp, 0);
4159 if (vp->v_type != VDIR) {
4163 if (__predict_false((vp->v_vflag & VV_UNLINKED) != 0)) {
4167 aiov.iov_base = buf;
4168 aiov.iov_len = count;
4169 auio.uio_iov = &aiov;
4170 auio.uio_iovcnt = 1;
4171 auio.uio_rw = UIO_READ;
4172 auio.uio_segflg = bufseg;
4174 vn_lock(vp, LK_SHARED | LK_RETRY);
4175 AUDIT_ARG_VNODE1(vp);
4176 loff = auio.uio_offset = foffset;
4178 error = mac_vnode_check_readdir(td->td_ucred, vp);
4181 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL,
4183 foffset = auio.uio_offset;
4188 if (count == auio.uio_resid &&
4189 (vp->v_vflag & VV_ROOT) &&
4190 (vp->v_mount->mnt_flag & MNT_UNION)) {
4191 struct vnode *tvp = vp;
4193 vp = vp->v_mount->mnt_vnodecovered;
4203 *residp = auio.uio_resid;
4204 td->td_retval[0] = count - auio.uio_resid;
4206 foffset_unlock(fp, foffset, 0);
4212 * Set the mode mask for creation of filesystem nodes.
4214 #ifndef _SYS_SYSPROTO_H_
4220 sys_umask(struct thread *td, struct umask_args *uap)
4222 struct pwddesc *pdp;
4224 pdp = td->td_proc->p_pd;
4226 td->td_retval[0] = pdp->pd_cmask;
4227 pdp->pd_cmask = uap->newmask & ALLPERMS;
4228 PWDDESC_XUNLOCK(pdp);
4233 * Void all references to file by ripping underlying filesystem away from
4236 #ifndef _SYS_SYSPROTO_H_
4237 struct revoke_args {
4242 sys_revoke(struct thread *td, struct revoke_args *uap)
4246 struct nameidata nd;
4249 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
4251 if ((error = namei(&nd)) != 0)
4254 NDFREE_NOTHING(&nd);
4255 if (vp->v_type != VCHR || vp->v_rdev == NULL) {
4260 error = mac_vnode_check_revoke(td->td_ucred, vp);
4264 error = VOP_GETATTR(vp, &vattr, td->td_ucred);
4267 if (td->td_ucred->cr_uid != vattr.va_uid) {
4268 error = priv_check(td, PRIV_VFS_ADMIN);
4272 if (devfs_usecount(vp) > 0)
4273 VOP_REVOKE(vp, REVOKEALL);
4280 * This variant of getvnode() allows O_PATH files. Caller should
4281 * ensure that returned file and vnode are only used for compatible
4285 getvnode_path(struct thread *td, int fd, cap_rights_t *rightsp,
4291 error = fget_unlocked(td->td_proc->p_fd, fd, rightsp, &fp);
4296 * The file could be not of the vnode type, or it may be not
4297 * yet fully initialized, in which case the f_vnode pointer
4298 * may be set, but f_ops is still badfileops. E.g.,
4299 * devfs_open() transiently create such situation to
4300 * facilitate csw d_fdopen().
4302 * Dupfdopen() handling in kern_openat() installs the
4303 * half-baked file into the process descriptor table, allowing
4304 * other thread to dereference it. Guard against the race by
4307 if (fp->f_vnode == NULL || fp->f_ops == &badfileops) {
4318 * Convert a user file descriptor to a kernel file entry and check
4319 * that, if it is a capability, the correct rights are present.
4320 * A reference on the file entry is held upon returning.
4323 getvnode(struct thread *td, int fd, cap_rights_t *rightsp, struct file **fpp)
4327 error = getvnode_path(td, fd, rightsp, fpp);
4330 * Filter out O_PATH file descriptors, most getvnode() callers
4331 * do not call fo_ methods.
4333 if (error == 0 && (*fpp)->f_ops == &path_fileops) {
4343 * Get an (NFS) file handle.
4345 #ifndef _SYS_SYSPROTO_H_
4346 struct lgetfh_args {
4352 sys_lgetfh(struct thread *td, struct lgetfh_args *uap)
4355 return (kern_getfhat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->fname,
4356 UIO_USERSPACE, uap->fhp, UIO_USERSPACE));
4359 #ifndef _SYS_SYSPROTO_H_
4366 sys_getfh(struct thread *td, struct getfh_args *uap)
4369 return (kern_getfhat(td, 0, AT_FDCWD, uap->fname, UIO_USERSPACE,
4370 uap->fhp, UIO_USERSPACE));
4374 * syscall for the rpc.lockd to use to translate an open descriptor into
4375 * a NFS file handle.
4377 * warning: do not remove the priv_check() call or this becomes one giant
4380 #ifndef _SYS_SYSPROTO_H_
4381 struct getfhat_args {
4389 sys_getfhat(struct thread *td, struct getfhat_args *uap)
4392 return (kern_getfhat(td, uap->flags, uap->fd, uap->path, UIO_USERSPACE,
4393 uap->fhp, UIO_USERSPACE));
4397 kern_getfhat(struct thread *td, int flags, int fd, const char *path,
4398 enum uio_seg pathseg, fhandle_t *fhp, enum uio_seg fhseg)
4400 struct nameidata nd;
4405 if ((flags & ~(AT_SYMLINK_NOFOLLOW | AT_RESOLVE_BENEATH)) != 0)
4407 error = priv_check(td, PRIV_VFS_GETFH);
4410 NDINIT_AT(&nd, LOOKUP, at2cnpflags(flags, AT_SYMLINK_NOFOLLOW |
4411 AT_RESOLVE_BENEATH) | LOCKLEAF | AUDITVNODE1, pathseg, path,
4416 NDFREE_NOTHING(&nd);
4418 bzero(&fh, sizeof(fh));
4419 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
4420 error = VOP_VPTOFH(vp, &fh.fh_fid);
4423 if (fhseg == UIO_USERSPACE)
4424 error = copyout(&fh, fhp, sizeof (fh));
4426 memcpy(fhp, &fh, sizeof(fh));
4431 #ifndef _SYS_SYSPROTO_H_
4432 struct fhlink_args {
4438 sys_fhlink(struct thread *td, struct fhlink_args *uap)
4441 return (kern_fhlinkat(td, AT_FDCWD, uap->to, UIO_USERSPACE, uap->fhp));
4444 #ifndef _SYS_SYSPROTO_H_
4445 struct fhlinkat_args {
4452 sys_fhlinkat(struct thread *td, struct fhlinkat_args *uap)
4455 return (kern_fhlinkat(td, uap->tofd, uap->to, UIO_USERSPACE, uap->fhp));
4459 kern_fhlinkat(struct thread *td, int fd, const char *path,
4460 enum uio_seg pathseg, fhandle_t *fhp)
4467 error = priv_check(td, PRIV_VFS_GETFH);
4470 error = copyin(fhp, &fh, sizeof(fh));
4475 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4477 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_SHARED, &vp);
4482 error = kern_linkat_vp(td, vp, fd, path, pathseg);
4483 } while (error == EAGAIN || error == ERELOOKUP);
4487 #ifndef _SYS_SYSPROTO_H_
4488 struct fhreadlink_args {
4495 sys_fhreadlink(struct thread *td, struct fhreadlink_args *uap)
4502 error = priv_check(td, PRIV_VFS_GETFH);
4505 if (uap->bufsize > IOSIZE_MAX)
4507 error = copyin(uap->fhp, &fh, sizeof(fh));
4510 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4512 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_SHARED, &vp);
4516 error = kern_readlink_vp(vp, uap->buf, UIO_USERSPACE, uap->bufsize, td);
4522 * syscall for the rpc.lockd to use to translate a NFS file handle into an
4525 * warning: do not remove the priv_check() call or this becomes one giant
4528 #ifndef _SYS_SYSPROTO_H_
4529 struct fhopen_args {
4530 const struct fhandle *u_fhp;
4535 sys_fhopen(struct thread *td, struct fhopen_args *uap)
4537 return (kern_fhopen(td, uap->u_fhp, uap->flags));
4541 kern_fhopen(struct thread *td, const struct fhandle *u_fhp, int flags)
4550 error = priv_check(td, PRIV_VFS_FHOPEN);
4554 fmode = FFLAGS(flags);
4555 /* why not allow a non-read/write open for our lockd? */
4556 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
4558 error = copyin(u_fhp, &fhp, sizeof(fhp));
4561 /* find the mount point */
4562 mp = vfs_busyfs(&fhp.fh_fsid);
4565 /* now give me my vnode, it gets returned to me locked */
4566 error = VFS_FHTOVP(mp, &fhp.fh_fid, LK_EXCLUSIVE, &vp);
4571 error = falloc_noinstall(td, &fp);
4577 * An extra reference on `fp' has been held for us by
4578 * falloc_noinstall().
4584 error = vn_open_vnode(vp, fmode, td->td_ucred, td, fp);
4586 KASSERT(fp->f_ops == &badfileops,
4587 ("VOP_OPEN in fhopen() set f_ops"));
4588 KASSERT(td->td_dupfd < 0,
4589 ("fhopen() encountered fdopen()"));
4598 finit_vnode(fp, fmode, NULL, &vnops);
4600 if ((fmode & O_TRUNC) != 0) {
4601 error = fo_truncate(fp, 0, td->td_ucred, td);
4606 error = finstall(td, fp, &indx, fmode, NULL);
4609 td->td_retval[0] = indx;
4614 * Stat an (NFS) file handle.
4616 #ifndef _SYS_SYSPROTO_H_
4617 struct fhstat_args {
4618 struct fhandle *u_fhp;
4623 sys_fhstat(struct thread *td, struct fhstat_args *uap)
4629 error = copyin(uap->u_fhp, &fh, sizeof(fh));
4632 error = kern_fhstat(td, fh, &sb);
4634 error = copyout(&sb, uap->sb, sizeof(sb));
4639 kern_fhstat(struct thread *td, struct fhandle fh, struct stat *sb)
4645 error = priv_check(td, PRIV_VFS_FHSTAT);
4648 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4650 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_EXCLUSIVE, &vp);
4654 error = VOP_STAT(vp, sb, td->td_ucred, NOCRED, td);
4660 * Implement fstatfs() for (NFS) file handles.
4662 #ifndef _SYS_SYSPROTO_H_
4663 struct fhstatfs_args {
4664 struct fhandle *u_fhp;
4669 sys_fhstatfs(struct thread *td, struct fhstatfs_args *uap)
4675 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4678 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
4679 error = kern_fhstatfs(td, fh, sfp);
4681 error = copyout(sfp, uap->buf, sizeof(*sfp));
4682 free(sfp, M_STATFS);
4687 kern_fhstatfs(struct thread *td, fhandle_t fh, struct statfs *buf)
4693 error = priv_check(td, PRIV_VFS_FHSTATFS);
4696 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4698 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_EXCLUSIVE, &vp);
4704 error = prison_canseemount(td->td_ucred, mp);
4708 error = mac_mount_check_stat(td->td_ucred, mp);
4712 error = VFS_STATFS(mp, buf);
4719 * Unlike madvise(2), we do not make a best effort to remember every
4720 * possible caching hint. Instead, we remember the last setting with
4721 * the exception that we will allow POSIX_FADV_NORMAL to adjust the
4722 * region of any current setting.
4725 kern_posix_fadvise(struct thread *td, int fd, off_t offset, off_t len,
4728 struct fadvise_info *fa, *new;
4734 if (offset < 0 || len < 0 || offset > OFF_MAX - len)
4736 AUDIT_ARG_VALUE(advice);
4738 case POSIX_FADV_SEQUENTIAL:
4739 case POSIX_FADV_RANDOM:
4740 case POSIX_FADV_NOREUSE:
4741 new = malloc(sizeof(*fa), M_FADVISE, M_WAITOK);
4743 case POSIX_FADV_NORMAL:
4744 case POSIX_FADV_WILLNEED:
4745 case POSIX_FADV_DONTNEED:
4751 /* XXX: CAP_POSIX_FADVISE? */
4753 error = fget(td, fd, &cap_no_rights, &fp);
4756 AUDIT_ARG_FILE(td->td_proc, fp);
4757 if ((fp->f_ops->fo_flags & DFLAG_SEEKABLE) == 0) {
4761 if (fp->f_type != DTYPE_VNODE) {
4766 if (vp->v_type != VREG) {
4773 end = offset + len - 1;
4775 case POSIX_FADV_SEQUENTIAL:
4776 case POSIX_FADV_RANDOM:
4777 case POSIX_FADV_NOREUSE:
4779 * Try to merge any existing non-standard region with
4780 * this new region if possible, otherwise create a new
4781 * non-standard region for this request.
4783 mtx_pool_lock(mtxpool_sleep, fp);
4785 if (fa != NULL && fa->fa_advice == advice &&
4786 ((fa->fa_start <= end && fa->fa_end >= offset) ||
4787 (end != OFF_MAX && fa->fa_start == end + 1) ||
4788 (fa->fa_end != OFF_MAX && fa->fa_end + 1 == offset))) {
4789 if (offset < fa->fa_start)
4790 fa->fa_start = offset;
4791 if (end > fa->fa_end)
4794 new->fa_advice = advice;
4795 new->fa_start = offset;
4800 mtx_pool_unlock(mtxpool_sleep, fp);
4802 case POSIX_FADV_NORMAL:
4804 * If a the "normal" region overlaps with an existing
4805 * non-standard region, trim or remove the
4806 * non-standard region.
4808 mtx_pool_lock(mtxpool_sleep, fp);
4811 if (offset <= fa->fa_start && end >= fa->fa_end) {
4813 fp->f_advice = NULL;
4814 } else if (offset <= fa->fa_start &&
4815 end >= fa->fa_start)
4816 fa->fa_start = end + 1;
4817 else if (offset <= fa->fa_end && end >= fa->fa_end)
4818 fa->fa_end = offset - 1;
4819 else if (offset >= fa->fa_start && end <= fa->fa_end) {
4821 * If the "normal" region is a middle
4822 * portion of the existing
4823 * non-standard region, just remove
4824 * the whole thing rather than picking
4825 * one side or the other to
4829 fp->f_advice = NULL;
4832 mtx_pool_unlock(mtxpool_sleep, fp);
4834 case POSIX_FADV_WILLNEED:
4835 case POSIX_FADV_DONTNEED:
4836 error = VOP_ADVISE(vp, offset, end, advice);
4842 free(new, M_FADVISE);
4847 sys_posix_fadvise(struct thread *td, struct posix_fadvise_args *uap)
4851 error = kern_posix_fadvise(td, uap->fd, uap->offset, uap->len,
4853 return (kern_posix_error(td, error));
4857 kern_copy_file_range(struct thread *td, int infd, off_t *inoffp, int outfd,
4858 off_t *outoffp, size_t len, unsigned int flags)
4860 struct file *infp, *outfp;
4861 struct vnode *invp, *outvp;
4864 void *rl_rcookie, *rl_wcookie;
4865 off_t savinoff, savoutoff;
4867 infp = outfp = NULL;
4868 rl_rcookie = rl_wcookie = NULL;
4877 if (len > SSIZE_MAX)
4879 * Although the len argument is size_t, the return argument
4880 * is ssize_t (which is signed). Therefore a size that won't
4881 * fit in ssize_t can't be returned.
4885 /* Get the file structures for the file descriptors. */
4886 error = fget_read(td, infd,
4887 inoffp != NULL ? &cap_pread_rights : &cap_read_rights, &infp);
4890 if (infp->f_ops == &badfileops) {
4894 if (infp->f_vnode == NULL) {
4898 error = fget_write(td, outfd,
4899 outoffp != NULL ? &cap_pwrite_rights : &cap_write_rights, &outfp);
4902 if (outfp->f_ops == &badfileops) {
4906 if (outfp->f_vnode == NULL) {
4911 /* Set the offset pointers to the correct place. */
4913 inoffp = &infp->f_offset;
4914 if (outoffp == NULL)
4915 outoffp = &outfp->f_offset;
4917 savoutoff = *outoffp;
4919 invp = infp->f_vnode;
4920 outvp = outfp->f_vnode;
4921 /* Sanity check the f_flag bits. */
4922 if ((outfp->f_flag & (FWRITE | FAPPEND)) != FWRITE ||
4923 (infp->f_flag & FREAD) == 0) {
4928 /* If len == 0, just return 0. */
4933 * If infp and outfp refer to the same file, the byte ranges cannot
4936 if (invp == outvp && ((savinoff <= savoutoff && savinoff + len >
4937 savoutoff) || (savinoff > savoutoff && savoutoff + len >
4943 /* Range lock the byte ranges for both invp and outvp. */
4945 rl_wcookie = vn_rangelock_wlock(outvp, *outoffp, *outoffp +
4947 rl_rcookie = vn_rangelock_tryrlock(invp, *inoffp, *inoffp +
4949 if (rl_rcookie != NULL)
4951 vn_rangelock_unlock(outvp, rl_wcookie);
4952 rl_rcookie = vn_rangelock_rlock(invp, *inoffp, *inoffp + len);
4953 vn_rangelock_unlock(invp, rl_rcookie);
4957 error = vn_copy_file_range(invp, inoffp, outvp, outoffp, &retlen,
4958 flags, infp->f_cred, outfp->f_cred, td);
4960 if (rl_rcookie != NULL)
4961 vn_rangelock_unlock(invp, rl_rcookie);
4962 if (rl_wcookie != NULL)
4963 vn_rangelock_unlock(outvp, rl_wcookie);
4964 if (savinoff != -1 && (error == EINTR || error == ERESTART)) {
4966 *outoffp = savoutoff;
4972 td->td_retval[0] = retlen;
4977 sys_copy_file_range(struct thread *td, struct copy_file_range_args *uap)
4979 off_t inoff, outoff, *inoffp, *outoffp;
4982 inoffp = outoffp = NULL;
4983 if (uap->inoffp != NULL) {
4984 error = copyin(uap->inoffp, &inoff, sizeof(off_t));
4989 if (uap->outoffp != NULL) {
4990 error = copyin(uap->outoffp, &outoff, sizeof(off_t));
4995 error = kern_copy_file_range(td, uap->infd, inoffp, uap->outfd,
4996 outoffp, uap->len, uap->flags);
4997 if (error == 0 && uap->inoffp != NULL)
4998 error = copyout(inoffp, uap->inoffp, sizeof(off_t));
4999 if (error == 0 && uap->outoffp != NULL)
5000 error = copyout(outoffp, uap->outoffp, sizeof(off_t));