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);
960 * Change notion of root (``/'') directory.
962 #ifndef _SYS_SYSPROTO_H_
968 sys_chroot(struct thread *td, struct chroot_args *uap)
973 error = priv_check(td, PRIV_VFS_CHROOT);
976 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
977 UIO_USERSPACE, uap->path, td);
981 error = change_dir(nd.ni_vp, td);
985 error = mac_vnode_check_chroot(td->td_ucred, nd.ni_vp);
989 VOP_UNLOCK(nd.ni_vp);
990 error = pwd_chroot(td, nd.ni_vp);
1002 * Common routine for chroot and chdir. Callers must provide a locked vnode
1006 change_dir(struct vnode *vp, struct thread *td)
1012 ASSERT_VOP_LOCKED(vp, "change_dir(): vp not locked");
1013 if (vp->v_type != VDIR)
1016 error = mac_vnode_check_chdir(td->td_ucred, vp);
1020 return (VOP_ACCESS(vp, VEXEC, td->td_ucred, td));
1023 static __inline void
1024 flags_to_rights(int flags, cap_rights_t *rightsp)
1026 if (flags & O_EXEC) {
1027 cap_rights_set_one(rightsp, CAP_FEXECVE);
1031 switch ((flags & O_ACCMODE)) {
1033 cap_rights_set_one(rightsp, CAP_READ);
1036 cap_rights_set_one(rightsp, CAP_READ);
1039 cap_rights_set_one(rightsp, CAP_WRITE);
1040 if (!(flags & (O_APPEND | O_TRUNC)))
1041 cap_rights_set_one(rightsp, CAP_SEEK);
1046 if (flags & O_CREAT)
1047 cap_rights_set_one(rightsp, CAP_CREATE);
1049 if (flags & O_TRUNC)
1050 cap_rights_set_one(rightsp, CAP_FTRUNCATE);
1052 if (flags & (O_SYNC | O_FSYNC))
1053 cap_rights_set_one(rightsp, CAP_FSYNC);
1055 if (flags & (O_EXLOCK | O_SHLOCK))
1056 cap_rights_set_one(rightsp, CAP_FLOCK);
1060 * Check permissions, allocate an open file structure, and call the device
1061 * open routine if any.
1063 #ifndef _SYS_SYSPROTO_H_
1071 sys_open(struct thread *td, struct open_args *uap)
1074 return (kern_openat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1075 uap->flags, uap->mode));
1078 #ifndef _SYS_SYSPROTO_H_
1079 struct openat_args {
1087 sys_openat(struct thread *td, struct openat_args *uap)
1090 AUDIT_ARG_FD(uap->fd);
1091 return (kern_openat(td, uap->fd, uap->path, UIO_USERSPACE, uap->flag,
1096 kern_openat(struct thread *td, int fd, const char *path, enum uio_seg pathseg,
1097 int flags, int mode)
1099 struct proc *p = td->td_proc;
1100 struct filedesc *fdp;
1101 struct pwddesc *pdp;
1104 struct nameidata nd;
1105 cap_rights_t rights;
1106 int cmode, error, indx;
1112 AUDIT_ARG_FFLAGS(flags);
1113 AUDIT_ARG_MODE(mode);
1114 cap_rights_init_one(&rights, CAP_LOOKUP);
1115 flags_to_rights(flags, &rights);
1118 * Only one of the O_EXEC, O_RDONLY, O_WRONLY and O_RDWR flags
1119 * may be specified. On the other hand, for O_PATH any mode
1120 * except O_EXEC is ignored.
1122 if ((flags & O_PATH) != 0) {
1123 flags &= ~(O_CREAT | O_ACCMODE);
1124 } else if ((flags & O_EXEC) != 0) {
1125 if (flags & O_ACCMODE)
1127 } else if ((flags & O_ACCMODE) == O_ACCMODE) {
1130 flags = FFLAGS(flags);
1134 * Allocate a file structure. The descriptor to reference it
1135 * is allocated and used by finstall_refed() below.
1137 error = falloc_noinstall(td, &fp);
1140 /* Set the flags early so the finit in devfs can pick them up. */
1141 fp->f_flag = flags & FMASK;
1142 cmode = ((mode & ~pdp->pd_cmask) & ALLPERMS) & ~S_ISTXT;
1143 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, fd,
1145 td->td_dupfd = -1; /* XXX check for fdopen */
1146 error = vn_open(&nd, &flags, cmode, fp);
1149 * If the vn_open replaced the method vector, something
1150 * wonderous happened deep below and we just pass it up
1151 * pretending we know what we do.
1153 if (error == ENXIO && fp->f_ops != &badfileops) {
1154 MPASS((flags & O_PATH) == 0);
1159 * Handle special fdopen() case. bleh.
1161 * Don't do this for relative (capability) lookups; we don't
1162 * understand exactly what would happen, and we don't think
1163 * that it ever should.
1165 if ((nd.ni_resflags & NIRES_STRICTREL) == 0 &&
1166 (error == ENODEV || error == ENXIO) &&
1167 td->td_dupfd >= 0) {
1168 error = dupfdopen(td, fdp, td->td_dupfd, flags, error,
1177 NDFREE(&nd, NDF_ONLY_PNBUF);
1181 * Store the vnode, for any f_type. Typically, the vnode use
1182 * count is decremented by direct call to vn_closefile() for
1183 * files that switched type in the cdevsw fdopen() method.
1188 * If the file wasn't claimed by devfs bind it to the normal
1189 * vnode operations here.
1191 if (fp->f_ops == &badfileops) {
1192 KASSERT(vp->v_type != VFIFO || (flags & O_PATH) != 0,
1193 ("Unexpected fifo fp %p vp %p", fp, vp));
1194 if ((flags & O_PATH) != 0) {
1195 finit(fp, (flags & FMASK) | (fp->f_flag & FKQALLOWED),
1196 DTYPE_VNODE, NULL, &path_fileops);
1200 finit_vnode(fp, flags, NULL, &vnops);
1205 if (flags & O_TRUNC) {
1206 error = fo_truncate(fp, 0, td->td_ucred, td);
1212 * If we haven't already installed the FD (for dupfdopen), do so now.
1215 struct filecaps *fcaps;
1218 if ((nd.ni_resflags & NIRES_STRICTREL) != 0)
1219 fcaps = &nd.ni_filecaps;
1223 error = finstall_refed(td, fp, &indx, flags, fcaps);
1224 /* On success finstall_refed() consumes fcaps. */
1226 filecaps_free(&nd.ni_filecaps);
1230 filecaps_free(&nd.ni_filecaps);
1231 falloc_abort(td, fp);
1234 td->td_retval[0] = indx;
1237 KASSERT(indx == -1, ("indx=%d, should be -1", indx));
1238 falloc_abort(td, fp);
1246 #ifndef _SYS_SYSPROTO_H_
1247 struct ocreat_args {
1253 ocreat(struct thread *td, struct ocreat_args *uap)
1256 return (kern_openat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1257 O_WRONLY | O_CREAT | O_TRUNC, uap->mode));
1259 #endif /* COMPAT_43 */
1262 * Create a special file.
1264 #ifndef _SYS_SYSPROTO_H_
1265 struct mknodat_args {
1273 sys_mknodat(struct thread *td, struct mknodat_args *uap)
1276 return (kern_mknodat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode,
1280 #if defined(COMPAT_FREEBSD11)
1282 freebsd11_mknod(struct thread *td,
1283 struct freebsd11_mknod_args *uap)
1286 return (kern_mknodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1287 uap->mode, uap->dev));
1291 freebsd11_mknodat(struct thread *td,
1292 struct freebsd11_mknodat_args *uap)
1295 return (kern_mknodat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode,
1298 #endif /* COMPAT_FREEBSD11 */
1301 kern_mknodat(struct thread *td, int fd, const char *path, enum uio_seg pathseg,
1302 int mode, dev_t dev)
1307 struct nameidata nd;
1308 int error, whiteout = 0;
1310 AUDIT_ARG_MODE(mode);
1312 switch (mode & S_IFMT) {
1315 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
1316 if (error == 0 && dev == VNOVAL)
1320 error = priv_check(td, PRIV_VFS_MKNOD_WHT);
1324 return (kern_mkfifoat(td, fd, path, pathseg, mode));
1335 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
1336 NOCACHE, pathseg, path, fd, &cap_mknodat_rights,
1338 if ((error = namei(&nd)) != 0)
1342 NDFREE(&nd, NDF_ONLY_PNBUF);
1343 if (vp == nd.ni_dvp)
1351 vattr.va_mode = (mode & ALLPERMS) &
1352 ~td->td_proc->p_pd->pd_cmask;
1353 vattr.va_rdev = dev;
1356 switch (mode & S_IFMT) {
1358 vattr.va_type = VCHR;
1361 vattr.va_type = VBLK;
1367 panic("kern_mknod: invalid mode");
1370 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1371 NDFREE(&nd, NDF_ONLY_PNBUF);
1373 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1378 if (error == 0 && !whiteout)
1379 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp,
1380 &nd.ni_cnd, &vattr);
1384 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, CREATE);
1386 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp,
1387 &nd.ni_cnd, &vattr);
1390 VOP_VPUT_PAIR(nd.ni_dvp, error == 0 && !whiteout ? &nd.ni_vp : NULL,
1392 vn_finished_write(mp);
1393 NDFREE(&nd, NDF_ONLY_PNBUF);
1394 if (error == ERELOOKUP)
1400 * Create a named pipe.
1402 #ifndef _SYS_SYSPROTO_H_
1403 struct mkfifo_args {
1409 sys_mkfifo(struct thread *td, struct mkfifo_args *uap)
1412 return (kern_mkfifoat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1416 #ifndef _SYS_SYSPROTO_H_
1417 struct mkfifoat_args {
1424 sys_mkfifoat(struct thread *td, struct mkfifoat_args *uap)
1427 return (kern_mkfifoat(td, uap->fd, uap->path, UIO_USERSPACE,
1432 kern_mkfifoat(struct thread *td, int fd, const char *path,
1433 enum uio_seg pathseg, int mode)
1437 struct nameidata nd;
1440 AUDIT_ARG_MODE(mode);
1444 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
1445 NOCACHE, pathseg, path, fd, &cap_mkfifoat_rights,
1447 if ((error = namei(&nd)) != 0)
1449 if (nd.ni_vp != NULL) {
1450 NDFREE(&nd, NDF_ONLY_PNBUF);
1451 if (nd.ni_vp == nd.ni_dvp)
1458 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1459 NDFREE(&nd, NDF_ONLY_PNBUF);
1461 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1466 vattr.va_type = VFIFO;
1467 vattr.va_mode = (mode & ALLPERMS) & ~td->td_proc->p_pd->pd_cmask;
1469 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1474 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
1478 VOP_VPUT_PAIR(nd.ni_dvp, error == 0 ? &nd.ni_vp : NULL, true);
1479 vn_finished_write(mp);
1480 NDFREE(&nd, NDF_ONLY_PNBUF);
1481 if (error == ERELOOKUP)
1487 * Make a hard file link.
1489 #ifndef _SYS_SYSPROTO_H_
1496 sys_link(struct thread *td, struct link_args *uap)
1499 return (kern_linkat(td, AT_FDCWD, AT_FDCWD, uap->path, uap->link,
1500 UIO_USERSPACE, FOLLOW));
1503 #ifndef _SYS_SYSPROTO_H_
1504 struct linkat_args {
1513 sys_linkat(struct thread *td, struct linkat_args *uap)
1518 if ((flag & ~(AT_SYMLINK_FOLLOW | AT_RESOLVE_BENEATH |
1519 AT_EMPTY_PATH)) != 0)
1522 return (kern_linkat(td, uap->fd1, uap->fd2, uap->path1, uap->path2,
1523 UIO_USERSPACE, at2cnpflags(flag, AT_SYMLINK_FOLLOW |
1524 AT_RESOLVE_BENEATH | AT_EMPTY_PATH)));
1527 int hardlink_check_uid = 0;
1528 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
1529 &hardlink_check_uid, 0,
1530 "Unprivileged processes cannot create hard links to files owned by other "
1532 static int hardlink_check_gid = 0;
1533 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
1534 &hardlink_check_gid, 0,
1535 "Unprivileged processes cannot create hard links to files owned by other "
1539 can_hardlink(struct vnode *vp, struct ucred *cred)
1544 if (!hardlink_check_uid && !hardlink_check_gid)
1547 error = VOP_GETATTR(vp, &va, cred);
1551 if (hardlink_check_uid && cred->cr_uid != va.va_uid) {
1552 error = priv_check_cred(cred, PRIV_VFS_LINK);
1557 if (hardlink_check_gid && !groupmember(va.va_gid, cred)) {
1558 error = priv_check_cred(cred, PRIV_VFS_LINK);
1567 kern_linkat(struct thread *td, int fd1, int fd2, const char *path1,
1568 const char *path2, enum uio_seg segflag, int follow)
1570 struct nameidata nd;
1576 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, segflag,
1577 path1, fd1, &cap_linkat_source_rights, td);
1578 if ((error = namei(&nd)) != 0)
1580 NDFREE(&nd, NDF_ONLY_PNBUF);
1581 if ((nd.ni_resflags & NIRES_EMPTYPATH) != 0) {
1582 error = priv_check(td, PRIV_VFS_FHOPEN);
1588 error = kern_linkat_vp(td, nd.ni_vp, fd2, path2, segflag);
1589 } while (error == EAGAIN || error == ERELOOKUP);
1594 kern_linkat_vp(struct thread *td, struct vnode *vp, int fd, const char *path,
1595 enum uio_seg segflag)
1597 struct nameidata nd;
1601 if (vp->v_type == VDIR) {
1603 return (EPERM); /* POSIX */
1605 NDINIT_ATRIGHTS(&nd, CREATE,
1606 LOCKPARENT | SAVENAME | AUDITVNODE2 | NOCACHE, segflag, path, fd,
1607 &cap_linkat_target_rights, td);
1608 if ((error = namei(&nd)) == 0) {
1609 if (nd.ni_vp != NULL) {
1610 NDFREE(&nd, NDF_ONLY_PNBUF);
1611 if (nd.ni_dvp == nd.ni_vp)
1618 } else if (nd.ni_dvp->v_mount != vp->v_mount) {
1620 * Cross-device link. No need to recheck
1621 * vp->v_type, since it cannot change, except
1624 NDFREE(&nd, NDF_ONLY_PNBUF);
1628 } else if ((error = vn_lock(vp, LK_EXCLUSIVE)) == 0) {
1629 error = can_hardlink(vp, td->td_ucred);
1632 error = mac_vnode_check_link(td->td_ucred,
1633 nd.ni_dvp, vp, &nd.ni_cnd);
1638 NDFREE(&nd, NDF_ONLY_PNBUF);
1641 error = vn_start_write(vp, &mp, V_NOWAIT);
1645 NDFREE(&nd, NDF_ONLY_PNBUF);
1646 error = vn_start_write(NULL, &mp,
1652 error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
1653 VOP_VPUT_PAIR(nd.ni_dvp, &vp, true);
1654 vn_finished_write(mp);
1655 NDFREE(&nd, NDF_ONLY_PNBUF);
1659 NDFREE(&nd, NDF_ONLY_PNBUF);
1670 * Make a symbolic link.
1672 #ifndef _SYS_SYSPROTO_H_
1673 struct symlink_args {
1679 sys_symlink(struct thread *td, struct symlink_args *uap)
1682 return (kern_symlinkat(td, uap->path, AT_FDCWD, uap->link,
1686 #ifndef _SYS_SYSPROTO_H_
1687 struct symlinkat_args {
1694 sys_symlinkat(struct thread *td, struct symlinkat_args *uap)
1697 return (kern_symlinkat(td, uap->path1, uap->fd, uap->path2,
1702 kern_symlinkat(struct thread *td, const char *path1, int fd, const char *path2,
1703 enum uio_seg segflg)
1707 const char *syspath;
1709 struct nameidata nd;
1712 if (segflg == UIO_SYSSPACE) {
1715 tmppath = uma_zalloc(namei_zone, M_WAITOK);
1716 if ((error = copyinstr(path1, tmppath, MAXPATHLEN, NULL)) != 0)
1720 AUDIT_ARG_TEXT(syspath);
1724 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
1725 NOCACHE, segflg, path2, fd, &cap_symlinkat_rights,
1727 if ((error = namei(&nd)) != 0)
1730 NDFREE(&nd, NDF_ONLY_PNBUF);
1731 if (nd.ni_vp == nd.ni_dvp)
1740 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1741 NDFREE(&nd, NDF_ONLY_PNBUF);
1743 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1748 vattr.va_mode = ACCESSPERMS &~ td->td_proc->p_pd->pd_cmask;
1750 vattr.va_type = VLNK;
1751 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1756 error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr, syspath);
1760 VOP_VPUT_PAIR(nd.ni_dvp, error == 0 ? &nd.ni_vp : NULL, true);
1761 vn_finished_write(mp);
1762 NDFREE(&nd, NDF_ONLY_PNBUF);
1763 if (error == ERELOOKUP)
1766 if (segflg != UIO_SYSSPACE)
1767 uma_zfree(namei_zone, tmppath);
1772 * Delete a whiteout from the filesystem.
1774 #ifndef _SYS_SYSPROTO_H_
1775 struct undelete_args {
1780 sys_undelete(struct thread *td, struct undelete_args *uap)
1783 struct nameidata nd;
1789 NDINIT(&nd, DELETE, LOCKPARENT | DOWHITEOUT | AUDITVNODE1,
1790 UIO_USERSPACE, uap->path, td);
1795 if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & ISWHITEOUT)) {
1796 NDFREE(&nd, NDF_ONLY_PNBUF);
1797 if (nd.ni_vp == nd.ni_dvp)
1805 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1806 NDFREE(&nd, NDF_ONLY_PNBUF);
1808 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1812 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, DELETE);
1813 NDFREE(&nd, NDF_ONLY_PNBUF);
1815 vn_finished_write(mp);
1816 if (error == ERELOOKUP)
1822 * Delete a name from the filesystem.
1824 #ifndef _SYS_SYSPROTO_H_
1825 struct unlink_args {
1830 sys_unlink(struct thread *td, struct unlink_args *uap)
1833 return (kern_funlinkat(td, AT_FDCWD, uap->path, FD_NONE, UIO_USERSPACE,
1838 kern_funlinkat_ex(struct thread *td, int dfd, const char *path, int fd,
1839 int flag, enum uio_seg pathseg, ino_t oldinum)
1842 if ((flag & ~(AT_REMOVEDIR | AT_RESOLVE_BENEATH)) != 0)
1845 if ((flag & AT_REMOVEDIR) != 0)
1846 return (kern_frmdirat(td, dfd, path, fd, UIO_USERSPACE, 0));
1848 return (kern_funlinkat(td, dfd, path, fd, UIO_USERSPACE, 0, 0));
1851 #ifndef _SYS_SYSPROTO_H_
1852 struct unlinkat_args {
1859 sys_unlinkat(struct thread *td, struct unlinkat_args *uap)
1862 return (kern_funlinkat_ex(td, uap->fd, uap->path, FD_NONE, uap->flag,
1866 #ifndef _SYS_SYSPROTO_H_
1867 struct funlinkat_args {
1875 sys_funlinkat(struct thread *td, struct funlinkat_args *uap)
1878 return (kern_funlinkat_ex(td, uap->dfd, uap->path, uap->fd, uap->flag,
1883 kern_funlinkat(struct thread *td, int dfd, const char *path, int fd,
1884 enum uio_seg pathseg, int flag, ino_t oldinum)
1889 struct nameidata nd;
1894 if (fd != FD_NONE) {
1895 error = getvnode_path(td, fd, &cap_no_rights, &fp);
1903 NDINIT_ATRIGHTS(&nd, DELETE, LOCKPARENT | LOCKLEAF | AUDITVNODE1 |
1904 at2cnpflags(flag, AT_RESOLVE_BENEATH),
1905 pathseg, path, dfd, &cap_unlinkat_rights, td);
1906 if ((error = namei(&nd)) != 0) {
1907 if (error == EINVAL)
1912 if (vp->v_type == VDIR && oldinum == 0) {
1913 error = EPERM; /* POSIX */
1914 } else if (oldinum != 0 &&
1915 ((error = VOP_STAT(vp, &sb, td->td_ucred, NOCRED, td)) == 0) &&
1916 sb.st_ino != oldinum) {
1917 error = EIDRM; /* Identifier removed */
1918 } else if (fp != NULL && fp->f_vnode != vp) {
1919 if (VN_IS_DOOMED(fp->f_vnode))
1925 * The root of a mounted filesystem cannot be deleted.
1927 * XXX: can this only be a VDIR case?
1929 if (vp->v_vflag & VV_ROOT)
1933 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1934 NDFREE(&nd, NDF_ONLY_PNBUF);
1936 if (vp == nd.ni_dvp)
1940 if ((error = vn_start_write(NULL, &mp,
1941 V_XSLEEP | PCATCH)) != 0) {
1947 error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp,
1952 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_UNLINK);
1953 error = VOP_REMOVE(nd.ni_dvp, vp, &nd.ni_cnd);
1957 vn_finished_write(mp);
1959 NDFREE(&nd, NDF_ONLY_PNBUF);
1961 if (vp == nd.ni_dvp)
1965 if (error == ERELOOKUP)
1974 * Reposition read/write file offset.
1976 #ifndef _SYS_SYSPROTO_H_
1985 sys_lseek(struct thread *td, struct lseek_args *uap)
1988 return (kern_lseek(td, uap->fd, uap->offset, uap->whence));
1992 kern_lseek(struct thread *td, int fd, off_t offset, int whence)
1998 error = fget(td, fd, &cap_seek_rights, &fp);
2001 error = (fp->f_ops->fo_flags & DFLAG_SEEKABLE) != 0 ?
2002 fo_seek(fp, offset, whence, td) : ESPIPE;
2007 #if defined(COMPAT_43)
2009 * Reposition read/write file offset.
2011 #ifndef _SYS_SYSPROTO_H_
2012 struct olseek_args {
2019 olseek(struct thread *td, struct olseek_args *uap)
2022 return (kern_lseek(td, uap->fd, uap->offset, uap->whence));
2024 #endif /* COMPAT_43 */
2026 #if defined(COMPAT_FREEBSD6)
2027 /* Version with the 'pad' argument */
2029 freebsd6_lseek(struct thread *td, struct freebsd6_lseek_args *uap)
2032 return (kern_lseek(td, uap->fd, uap->offset, uap->whence));
2037 * Check access permissions using passed credentials.
2040 vn_access(struct vnode *vp, int user_flags, struct ucred *cred,
2046 /* Flags == 0 means only check for existence. */
2047 if (user_flags == 0)
2051 if (user_flags & R_OK)
2053 if (user_flags & W_OK)
2055 if (user_flags & X_OK)
2058 error = mac_vnode_check_access(cred, vp, accmode);
2062 if ((accmode & VWRITE) == 0 || (error = vn_writechk(vp)) == 0)
2063 error = VOP_ACCESS(vp, accmode, cred, td);
2068 * Check access permissions using "real" credentials.
2070 #ifndef _SYS_SYSPROTO_H_
2071 struct access_args {
2077 sys_access(struct thread *td, struct access_args *uap)
2080 return (kern_accessat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2084 #ifndef _SYS_SYSPROTO_H_
2085 struct faccessat_args {
2093 sys_faccessat(struct thread *td, struct faccessat_args *uap)
2096 return (kern_accessat(td, uap->fd, uap->path, UIO_USERSPACE, uap->flag,
2101 kern_accessat(struct thread *td, int fd, const char *path,
2102 enum uio_seg pathseg, int flag, int amode)
2104 struct ucred *cred, *usecred;
2106 struct nameidata nd;
2109 if ((flag & ~(AT_EACCESS | AT_RESOLVE_BENEATH | AT_EMPTY_PATH)) != 0)
2111 if (amode != F_OK && (amode & ~(R_OK | W_OK | X_OK)) != 0)
2115 * Create and modify a temporary credential instead of one that
2116 * is potentially shared (if we need one).
2118 cred = td->td_ucred;
2119 if ((flag & AT_EACCESS) == 0 &&
2120 ((cred->cr_uid != cred->cr_ruid ||
2121 cred->cr_rgid != cred->cr_groups[0]))) {
2122 usecred = crdup(cred);
2123 usecred->cr_uid = cred->cr_ruid;
2124 usecred->cr_groups[0] = cred->cr_rgid;
2125 td->td_ucred = usecred;
2128 AUDIT_ARG_VALUE(amode);
2129 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF |
2130 AUDITVNODE1 | at2cnpflags(flag, AT_RESOLVE_BENEATH |
2131 AT_EMPTY_PATH), pathseg, path, fd, &cap_fstat_rights, td);
2132 if ((error = namei(&nd)) != 0)
2136 error = vn_access(vp, amode, usecred, td);
2137 NDFREE_NOTHING(&nd);
2140 if (usecred != cred) {
2141 td->td_ucred = cred;
2148 * Check access permissions using "effective" credentials.
2150 #ifndef _SYS_SYSPROTO_H_
2151 struct eaccess_args {
2157 sys_eaccess(struct thread *td, struct eaccess_args *uap)
2160 return (kern_accessat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2161 AT_EACCESS, uap->amode));
2164 #if defined(COMPAT_43)
2166 * Get file status; this version follows links.
2168 #ifndef _SYS_SYSPROTO_H_
2175 ostat(struct thread *td, struct ostat_args *uap)
2181 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
2186 return (copyout(&osb, uap->ub, sizeof (osb)));
2190 * Get file status; this version does not follow links.
2192 #ifndef _SYS_SYSPROTO_H_
2193 struct olstat_args {
2199 olstat(struct thread *td, struct olstat_args *uap)
2205 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2206 UIO_USERSPACE, &sb, NULL);
2210 return (copyout(&osb, uap->ub, sizeof (osb)));
2214 * Convert from an old to a new stat structure.
2215 * XXX: many values are blindly truncated.
2218 cvtstat(struct stat *st, struct ostat *ost)
2221 bzero(ost, sizeof(*ost));
2222 ost->st_dev = st->st_dev;
2223 ost->st_ino = st->st_ino;
2224 ost->st_mode = st->st_mode;
2225 ost->st_nlink = st->st_nlink;
2226 ost->st_uid = st->st_uid;
2227 ost->st_gid = st->st_gid;
2228 ost->st_rdev = st->st_rdev;
2229 ost->st_size = MIN(st->st_size, INT32_MAX);
2230 ost->st_atim = st->st_atim;
2231 ost->st_mtim = st->st_mtim;
2232 ost->st_ctim = st->st_ctim;
2233 ost->st_blksize = st->st_blksize;
2234 ost->st_blocks = st->st_blocks;
2235 ost->st_flags = st->st_flags;
2236 ost->st_gen = st->st_gen;
2238 #endif /* COMPAT_43 */
2240 #if defined(COMPAT_43) || defined(COMPAT_FREEBSD11)
2241 int ino64_trunc_error;
2242 SYSCTL_INT(_vfs, OID_AUTO, ino64_trunc_error, CTLFLAG_RW,
2243 &ino64_trunc_error, 0,
2244 "Error on truncation of device, file or inode number, or link count");
2247 freebsd11_cvtstat(struct stat *st, struct freebsd11_stat *ost)
2250 ost->st_dev = st->st_dev;
2251 if (ost->st_dev != st->st_dev) {
2252 switch (ino64_trunc_error) {
2255 * Since dev_t is almost raw, don't clamp to the
2256 * maximum for case 2, but ignore the error.
2263 ost->st_ino = st->st_ino;
2264 if (ost->st_ino != st->st_ino) {
2265 switch (ino64_trunc_error) {
2272 ost->st_ino = UINT32_MAX;
2276 ost->st_mode = st->st_mode;
2277 ost->st_nlink = st->st_nlink;
2278 if (ost->st_nlink != st->st_nlink) {
2279 switch (ino64_trunc_error) {
2286 ost->st_nlink = UINT16_MAX;
2290 ost->st_uid = st->st_uid;
2291 ost->st_gid = st->st_gid;
2292 ost->st_rdev = st->st_rdev;
2293 if (ost->st_rdev != st->st_rdev) {
2294 switch (ino64_trunc_error) {
2301 ost->st_atim = st->st_atim;
2302 ost->st_mtim = st->st_mtim;
2303 ost->st_ctim = st->st_ctim;
2304 ost->st_size = st->st_size;
2305 ost->st_blocks = st->st_blocks;
2306 ost->st_blksize = st->st_blksize;
2307 ost->st_flags = st->st_flags;
2308 ost->st_gen = st->st_gen;
2310 ost->st_birthtim = st->st_birthtim;
2311 bzero((char *)&ost->st_birthtim + sizeof(ost->st_birthtim),
2312 sizeof(*ost) - offsetof(struct freebsd11_stat,
2313 st_birthtim) - sizeof(ost->st_birthtim));
2318 freebsd11_stat(struct thread *td, struct freebsd11_stat_args* uap)
2321 struct freebsd11_stat osb;
2324 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
2328 error = freebsd11_cvtstat(&sb, &osb);
2330 error = copyout(&osb, uap->ub, sizeof(osb));
2335 freebsd11_lstat(struct thread *td, struct freebsd11_lstat_args* uap)
2338 struct freebsd11_stat osb;
2341 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2342 UIO_USERSPACE, &sb, NULL);
2345 error = freebsd11_cvtstat(&sb, &osb);
2347 error = copyout(&osb, uap->ub, sizeof(osb));
2352 freebsd11_fhstat(struct thread *td, struct freebsd11_fhstat_args* uap)
2356 struct freebsd11_stat osb;
2359 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
2362 error = kern_fhstat(td, fh, &sb);
2365 error = freebsd11_cvtstat(&sb, &osb);
2367 error = copyout(&osb, uap->sb, sizeof(osb));
2372 freebsd11_fstatat(struct thread *td, struct freebsd11_fstatat_args* uap)
2375 struct freebsd11_stat osb;
2378 error = kern_statat(td, uap->flag, uap->fd, uap->path,
2379 UIO_USERSPACE, &sb, NULL);
2382 error = freebsd11_cvtstat(&sb, &osb);
2384 error = copyout(&osb, uap->buf, sizeof(osb));
2387 #endif /* COMPAT_FREEBSD11 */
2392 #ifndef _SYS_SYSPROTO_H_
2393 struct fstatat_args {
2401 sys_fstatat(struct thread *td, struct fstatat_args *uap)
2406 error = kern_statat(td, uap->flag, uap->fd, uap->path,
2407 UIO_USERSPACE, &sb, NULL);
2409 error = copyout(&sb, uap->buf, sizeof (sb));
2414 kern_statat(struct thread *td, int flag, int fd, const char *path,
2415 enum uio_seg pathseg, struct stat *sbp,
2416 void (*hook)(struct vnode *vp, struct stat *sbp))
2418 struct nameidata nd;
2421 if ((flag & ~(AT_SYMLINK_NOFOLLOW | AT_RESOLVE_BENEATH |
2422 AT_EMPTY_PATH)) != 0)
2425 NDINIT_ATRIGHTS(&nd, LOOKUP, at2cnpflags(flag, AT_RESOLVE_BENEATH |
2426 AT_SYMLINK_NOFOLLOW | AT_EMPTY_PATH) | LOCKSHARED | LOCKLEAF |
2427 AUDITVNODE1, pathseg, path, fd, &cap_fstat_rights, td);
2429 if ((error = namei(&nd)) != 0) {
2430 if (error == ENOTDIR &&
2431 (nd.ni_resflags & NIRES_EMPTYPATH) != 0)
2432 error = kern_fstat(td, fd, sbp);
2435 error = VOP_STAT(nd.ni_vp, sbp, td->td_ucred, NOCRED, td);
2437 if (__predict_false(hook != NULL))
2438 hook(nd.ni_vp, sbp);
2440 NDFREE_NOTHING(&nd);
2442 #ifdef __STAT_TIME_T_EXT
2443 sbp->st_atim_ext = 0;
2444 sbp->st_mtim_ext = 0;
2445 sbp->st_ctim_ext = 0;
2446 sbp->st_btim_ext = 0;
2449 if (KTRPOINT(td, KTR_STRUCT))
2450 ktrstat_error(sbp, error);
2455 #if defined(COMPAT_FREEBSD11)
2457 * Implementation of the NetBSD [l]stat() functions.
2460 freebsd11_cvtnstat(struct stat *sb, struct nstat *nsb)
2463 bzero(nsb, sizeof(*nsb));
2464 nsb->st_dev = sb->st_dev;
2465 nsb->st_ino = sb->st_ino;
2466 nsb->st_mode = sb->st_mode;
2467 nsb->st_nlink = sb->st_nlink;
2468 nsb->st_uid = sb->st_uid;
2469 nsb->st_gid = sb->st_gid;
2470 nsb->st_rdev = sb->st_rdev;
2471 nsb->st_atim = sb->st_atim;
2472 nsb->st_mtim = sb->st_mtim;
2473 nsb->st_ctim = sb->st_ctim;
2474 nsb->st_size = sb->st_size;
2475 nsb->st_blocks = sb->st_blocks;
2476 nsb->st_blksize = sb->st_blksize;
2477 nsb->st_flags = sb->st_flags;
2478 nsb->st_gen = sb->st_gen;
2479 nsb->st_birthtim = sb->st_birthtim;
2482 #ifndef _SYS_SYSPROTO_H_
2483 struct freebsd11_nstat_args {
2489 freebsd11_nstat(struct thread *td, struct freebsd11_nstat_args *uap)
2495 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
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, NULL);
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 if ((uap->atflag & ~(AT_SYMLINK_NOFOLLOW | AT_RESOLVE_BENEATH |
2750 AT_EMPTY_PATH)) != 0)
2753 return (kern_chflagsat(td, uap->fd, uap->path, UIO_USERSPACE,
2754 uap->flags, uap->atflag));
2758 * Same as chflags() but doesn't follow symlinks.
2760 #ifndef _SYS_SYSPROTO_H_
2761 struct lchflags_args {
2767 sys_lchflags(struct thread *td, struct lchflags_args *uap)
2770 return (kern_chflagsat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2771 uap->flags, AT_SYMLINK_NOFOLLOW));
2775 kern_chflagsat(struct thread *td, int fd, const char *path,
2776 enum uio_seg pathseg, u_long flags, int atflag)
2778 struct nameidata nd;
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 if ((uap->flag & ~(AT_SYMLINK_NOFOLLOW | AT_RESOLVE_BENEATH |
2881 AT_EMPTY_PATH)) != 0)
2884 return (kern_fchmodat(td, uap->fd, uap->path, UIO_USERSPACE,
2885 uap->mode, uap->flag));
2889 * Change mode of a file given path name (don't follow links.)
2891 #ifndef _SYS_SYSPROTO_H_
2892 struct lchmod_args {
2898 sys_lchmod(struct thread *td, struct lchmod_args *uap)
2901 return (kern_fchmodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2902 uap->mode, AT_SYMLINK_NOFOLLOW));
2906 kern_fchmodat(struct thread *td, int fd, const char *path,
2907 enum uio_seg pathseg, mode_t mode, int flag)
2909 struct nameidata nd;
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 if ((uap->flag & ~(AT_SYMLINK_NOFOLLOW | AT_RESOLVE_BENEATH |
3010 AT_EMPTY_PATH)) != 0)
3013 return (kern_fchownat(td, uap->fd, uap->path, UIO_USERSPACE, uap->uid,
3014 uap->gid, uap->flag));
3018 kern_fchownat(struct thread *td, int fd, const char *path,
3019 enum uio_seg pathseg, int uid, int gid, int flag)
3021 struct nameidata nd;
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 if (vp->v_object != NULL) {
3534 VM_OBJECT_WLOCK(vp->v_object);
3535 vm_object_page_clean(vp->v_object, 0, 0, 0);
3536 VM_OBJECT_WUNLOCK(vp->v_object);
3538 error = fullsync ? VOP_FSYNC(vp, MNT_WAIT, td) : VOP_FDATASYNC(vp, td);
3540 vn_finished_write(mp);
3541 if (error == ERELOOKUP)
3549 * Sync an open file.
3551 #ifndef _SYS_SYSPROTO_H_
3557 sys_fsync(struct thread *td, struct fsync_args *uap)
3560 return (kern_fsync(td, uap->fd, true));
3564 sys_fdatasync(struct thread *td, struct fdatasync_args *uap)
3567 return (kern_fsync(td, uap->fd, false));
3571 * Rename files. Source and destination must either both be directories, or
3572 * both not be directories. If target is a directory, it must be empty.
3574 #ifndef _SYS_SYSPROTO_H_
3575 struct rename_args {
3581 sys_rename(struct thread *td, struct rename_args *uap)
3584 return (kern_renameat(td, AT_FDCWD, uap->from, AT_FDCWD,
3585 uap->to, UIO_USERSPACE));
3588 #ifndef _SYS_SYSPROTO_H_
3589 struct renameat_args {
3597 sys_renameat(struct thread *td, struct renameat_args *uap)
3600 return (kern_renameat(td, uap->oldfd, uap->old, uap->newfd, uap->new,
3606 kern_renameat_mac(struct thread *td, int oldfd, const char *old, int newfd,
3607 const char *new, enum uio_seg pathseg, struct nameidata *fromnd)
3611 NDINIT_ATRIGHTS(fromnd, DELETE, LOCKPARENT | LOCKLEAF | SAVESTART |
3612 AUDITVNODE1, pathseg, old, oldfd, &cap_renameat_source_rights, td);
3613 if ((error = namei(fromnd)) != 0)
3615 error = mac_vnode_check_rename_from(td->td_ucred, fromnd->ni_dvp,
3616 fromnd->ni_vp, &fromnd->ni_cnd);
3617 VOP_UNLOCK(fromnd->ni_dvp);
3618 if (fromnd->ni_dvp != fromnd->ni_vp)
3619 VOP_UNLOCK(fromnd->ni_vp);
3621 NDFREE(fromnd, NDF_ONLY_PNBUF);
3622 vrele(fromnd->ni_dvp);
3623 vrele(fromnd->ni_vp);
3624 if (fromnd->ni_startdir)
3625 vrele(fromnd->ni_startdir);
3632 kern_renameat(struct thread *td, int oldfd, const char *old, int newfd,
3633 const char *new, enum uio_seg pathseg)
3635 struct mount *mp = NULL;
3636 struct vnode *tvp, *fvp, *tdvp;
3637 struct nameidata fromnd, tond;
3638 u_int64_t tondflags;
3644 if (mac_vnode_check_rename_from_enabled()) {
3645 error = kern_renameat_mac(td, oldfd, old, newfd, new, pathseg,
3651 NDINIT_ATRIGHTS(&fromnd, DELETE, WANTPARENT | SAVESTART | AUDITVNODE1,
3652 pathseg, old, oldfd, &cap_renameat_source_rights, td);
3653 if ((error = namei(&fromnd)) != 0)
3659 tondflags = LOCKPARENT | LOCKLEAF | NOCACHE | SAVESTART | AUDITVNODE2;
3660 if (fromnd.ni_vp->v_type == VDIR)
3661 tondflags |= WILLBEDIR;
3662 NDINIT_ATRIGHTS(&tond, RENAME, tondflags, pathseg, new, newfd,
3663 &cap_renameat_target_rights, td);
3664 if ((error = namei(&tond)) != 0) {
3665 /* Translate error code for rename("dir1", "dir2/."). */
3666 if (error == EISDIR && fvp->v_type == VDIR)
3668 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3669 vrele(fromnd.ni_dvp);
3675 error = vn_start_write(fvp, &mp, V_NOWAIT);
3677 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3678 NDFREE(&tond, NDF_ONLY_PNBUF);
3685 vrele(fromnd.ni_dvp);
3687 vrele(tond.ni_startdir);
3688 if (fromnd.ni_startdir != NULL)
3689 vrele(fromnd.ni_startdir);
3690 error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH);
3696 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
3699 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
3704 if (newfd != AT_FDCWD && (tond.ni_resflags & NIRES_ABS) == 0) {
3706 * If the target already exists we require CAP_UNLINKAT
3707 * from 'newfd', when newfd was used for the lookup.
3709 error = cap_check(&tond.ni_filecaps.fc_rights,
3710 &cap_unlinkat_rights);
3721 * If the source is the same as the destination (that is, if they
3722 * are links to the same vnode), then there is nothing to do.
3728 error = mac_vnode_check_rename_to(td->td_ucred, tdvp,
3729 tond.ni_vp, fromnd.ni_dvp == tdvp, &tond.ni_cnd);
3733 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
3734 tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
3735 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3736 NDFREE(&tond, NDF_ONLY_PNBUF);
3738 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3739 NDFREE(&tond, NDF_ONLY_PNBUF);
3746 vrele(fromnd.ni_dvp);
3749 vrele(tond.ni_startdir);
3750 vn_finished_write(mp);
3752 if (fromnd.ni_startdir)
3753 vrele(fromnd.ni_startdir);
3754 if (error == ERESTART)
3756 if (error == ERELOOKUP)
3762 * Make a directory file.
3764 #ifndef _SYS_SYSPROTO_H_
3771 sys_mkdir(struct thread *td, struct mkdir_args *uap)
3774 return (kern_mkdirat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
3778 #ifndef _SYS_SYSPROTO_H_
3779 struct mkdirat_args {
3786 sys_mkdirat(struct thread *td, struct mkdirat_args *uap)
3789 return (kern_mkdirat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode));
3793 kern_mkdirat(struct thread *td, int fd, const char *path, enum uio_seg segflg,
3798 struct nameidata nd;
3801 AUDIT_ARG_MODE(mode);
3805 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
3806 NC_NOMAKEENTRY | NC_KEEPPOSENTRY | FAILIFEXISTS | WILLBEDIR,
3807 segflg, path, fd, &cap_mkdirat_rights, td);
3808 if ((error = namei(&nd)) != 0)
3810 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3811 NDFREE(&nd, NDF_ONLY_PNBUF);
3813 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3818 vattr.va_type = VDIR;
3819 vattr.va_mode = (mode & ACCESSPERMS) &~ td->td_proc->p_pd->pd_cmask;
3821 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
3826 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
3830 NDFREE(&nd, NDF_ONLY_PNBUF);
3831 VOP_VPUT_PAIR(nd.ni_dvp, error == 0 ? &nd.ni_vp : NULL, true);
3832 vn_finished_write(mp);
3833 if (error == ERELOOKUP)
3839 * Remove a directory file.
3841 #ifndef _SYS_SYSPROTO_H_
3847 sys_rmdir(struct thread *td, struct rmdir_args *uap)
3850 return (kern_frmdirat(td, AT_FDCWD, uap->path, FD_NONE, UIO_USERSPACE,
3855 kern_frmdirat(struct thread *td, int dfd, const char *path, int fd,
3856 enum uio_seg pathseg, int flag)
3861 struct nameidata nd;
3862 cap_rights_t rights;
3866 if (fd != FD_NONE) {
3867 error = getvnode(td, fd, cap_rights_init_one(&rights,
3876 NDINIT_ATRIGHTS(&nd, DELETE, LOCKPARENT | LOCKLEAF | AUDITVNODE1 |
3877 at2cnpflags(flag, AT_RESOLVE_BENEATH),
3878 pathseg, path, dfd, &cap_unlinkat_rights, td);
3879 if ((error = namei(&nd)) != 0)
3882 if (vp->v_type != VDIR) {
3887 * No rmdir "." please.
3889 if (nd.ni_dvp == vp) {
3894 * The root of a mounted filesystem cannot be deleted.
3896 if (vp->v_vflag & VV_ROOT) {
3901 if (fp != NULL && fp->f_vnode != vp) {
3902 if (VN_IS_DOOMED(fp->f_vnode))
3910 error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp,
3915 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3916 NDFREE(&nd, NDF_ONLY_PNBUF);
3918 if (nd.ni_dvp == vp)
3922 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3926 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_UNLINK);
3927 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
3928 vn_finished_write(mp);
3930 NDFREE(&nd, NDF_ONLY_PNBUF);
3932 if (nd.ni_dvp == vp)
3936 if (error == ERELOOKUP)
3944 #if defined(COMPAT_43) || defined(COMPAT_FREEBSD11)
3946 freebsd11_kern_getdirentries(struct thread *td, int fd, char *ubuf, u_int count,
3947 long *basep, void (*func)(struct freebsd11_dirent *))
3949 struct freebsd11_dirent dstdp;
3950 struct dirent *dp, *edp;
3953 ssize_t resid, ucount;
3956 /* XXX arbitrary sanity limit on `count'. */
3957 count = min(count, 64 * 1024);
3959 dirbuf = malloc(count, M_TEMP, M_WAITOK);
3961 error = kern_getdirentries(td, fd, dirbuf, count, &base, &resid,
3969 for (dp = (struct dirent *)dirbuf,
3970 edp = (struct dirent *)&dirbuf[count - resid];
3971 ucount < count && dp < edp; ) {
3972 if (dp->d_reclen == 0)
3974 MPASS(dp->d_reclen >= _GENERIC_DIRLEN(0));
3975 if (dp->d_namlen >= sizeof(dstdp.d_name))
3977 dstdp.d_type = dp->d_type;
3978 dstdp.d_namlen = dp->d_namlen;
3979 dstdp.d_fileno = dp->d_fileno; /* truncate */
3980 if (dstdp.d_fileno != dp->d_fileno) {
3981 switch (ino64_trunc_error) {
3989 dstdp.d_fileno = UINT32_MAX;
3993 dstdp.d_reclen = sizeof(dstdp) - sizeof(dstdp.d_name) +
3994 ((dp->d_namlen + 1 + 3) &~ 3);
3995 bcopy(dp->d_name, dstdp.d_name, dstdp.d_namlen);
3996 bzero(dstdp.d_name + dstdp.d_namlen,
3997 dstdp.d_reclen - offsetof(struct freebsd11_dirent, d_name) -
3999 MPASS(dstdp.d_reclen <= dp->d_reclen);
4000 MPASS(ucount + dstdp.d_reclen <= count);
4003 error = copyout(&dstdp, ubuf + ucount, dstdp.d_reclen);
4006 dp = (struct dirent *)((char *)dp + dp->d_reclen);
4007 ucount += dstdp.d_reclen;
4011 free(dirbuf, M_TEMP);
4013 td->td_retval[0] = ucount;
4020 ogetdirentries_cvt(struct freebsd11_dirent *dp)
4022 #if (BYTE_ORDER == LITTLE_ENDIAN)
4024 * The expected low byte of dp->d_namlen is our dp->d_type.
4025 * The high MBZ byte of dp->d_namlen is our dp->d_namlen.
4027 dp->d_type = dp->d_namlen;
4031 * The dp->d_type is the high byte of the expected dp->d_namlen,
4032 * so must be zero'ed.
4039 * Read a block of directory entries in a filesystem independent format.
4041 #ifndef _SYS_SYSPROTO_H_
4042 struct ogetdirentries_args {
4050 ogetdirentries(struct thread *td, struct ogetdirentries_args *uap)
4055 error = kern_ogetdirentries(td, uap, &loff);
4057 error = copyout(&loff, uap->basep, sizeof(long));
4062 kern_ogetdirentries(struct thread *td, struct ogetdirentries_args *uap,
4068 /* XXX arbitrary sanity limit on `count'. */
4069 if (uap->count > 64 * 1024)
4072 error = freebsd11_kern_getdirentries(td, uap->fd, uap->buf, uap->count,
4073 &base, ogetdirentries_cvt);
4075 if (error == 0 && uap->basep != NULL)
4076 error = copyout(&base, uap->basep, sizeof(long));
4080 #endif /* COMPAT_43 */
4082 #if defined(COMPAT_FREEBSD11)
4083 #ifndef _SYS_SYSPROTO_H_
4084 struct freebsd11_getdirentries_args {
4092 freebsd11_getdirentries(struct thread *td,
4093 struct freebsd11_getdirentries_args *uap)
4098 error = freebsd11_kern_getdirentries(td, uap->fd, uap->buf, uap->count,
4101 if (error == 0 && uap->basep != NULL)
4102 error = copyout(&base, uap->basep, sizeof(long));
4107 freebsd11_getdents(struct thread *td, struct freebsd11_getdents_args *uap)
4109 struct freebsd11_getdirentries_args ap;
4113 ap.count = uap->count;
4115 return (freebsd11_getdirentries(td, &ap));
4117 #endif /* COMPAT_FREEBSD11 */
4120 * Read a block of directory entries in a filesystem independent format.
4123 sys_getdirentries(struct thread *td, struct getdirentries_args *uap)
4128 error = kern_getdirentries(td, uap->fd, uap->buf, uap->count, &base,
4129 NULL, UIO_USERSPACE);
4132 if (uap->basep != NULL)
4133 error = copyout(&base, uap->basep, sizeof(off_t));
4138 kern_getdirentries(struct thread *td, int fd, char *buf, size_t count,
4139 off_t *basep, ssize_t *residp, enum uio_seg bufseg)
4150 if (count > IOSIZE_MAX)
4152 auio.uio_resid = count;
4153 error = getvnode(td, fd, &cap_read_rights, &fp);
4156 if ((fp->f_flag & FREAD) == 0) {
4161 foffset = foffset_lock(fp, 0);
4163 if (vp->v_type != VDIR) {
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) {
4317 * Convert a user file descriptor to a kernel file entry and check
4318 * that, if it is a capability, the correct rights are present.
4319 * A reference on the file entry is held upon returning.
4322 getvnode(struct thread *td, int fd, cap_rights_t *rightsp, struct file **fpp)
4326 error = getvnode_path(td, fd, rightsp, fpp);
4329 * Filter out O_PATH file descriptors, most getvnode() callers
4330 * do not call fo_ methods.
4332 if (error == 0 && (*fpp)->f_ops == &path_fileops) {
4341 * Get an (NFS) file handle.
4343 #ifndef _SYS_SYSPROTO_H_
4344 struct lgetfh_args {
4350 sys_lgetfh(struct thread *td, struct lgetfh_args *uap)
4353 return (kern_getfhat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->fname,
4354 UIO_USERSPACE, uap->fhp, UIO_USERSPACE));
4357 #ifndef _SYS_SYSPROTO_H_
4364 sys_getfh(struct thread *td, struct getfh_args *uap)
4367 return (kern_getfhat(td, 0, AT_FDCWD, uap->fname, UIO_USERSPACE,
4368 uap->fhp, UIO_USERSPACE));
4372 * syscall for the rpc.lockd to use to translate an open descriptor into
4373 * a NFS file handle.
4375 * warning: do not remove the priv_check() call or this becomes one giant
4378 #ifndef _SYS_SYSPROTO_H_
4379 struct getfhat_args {
4387 sys_getfhat(struct thread *td, struct getfhat_args *uap)
4390 if ((uap->flags & ~(AT_SYMLINK_NOFOLLOW | AT_RESOLVE_BENEATH)) != 0)
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 error = priv_check(td, PRIV_VFS_GETFH);
4408 NDINIT_AT(&nd, LOOKUP, at2cnpflags(flags, AT_SYMLINK_NOFOLLOW |
4409 AT_RESOLVE_BENEATH) | LOCKLEAF | AUDITVNODE1, pathseg, path,
4414 NDFREE_NOTHING(&nd);
4416 bzero(&fh, sizeof(fh));
4417 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
4418 error = VOP_VPTOFH(vp, &fh.fh_fid);
4421 if (fhseg == UIO_USERSPACE)
4422 error = copyout(&fh, fhp, sizeof (fh));
4424 memcpy(fhp, &fh, sizeof(fh));
4429 #ifndef _SYS_SYSPROTO_H_
4430 struct fhlink_args {
4436 sys_fhlink(struct thread *td, struct fhlink_args *uap)
4439 return (kern_fhlinkat(td, AT_FDCWD, uap->to, UIO_USERSPACE, uap->fhp));
4442 #ifndef _SYS_SYSPROTO_H_
4443 struct fhlinkat_args {
4450 sys_fhlinkat(struct thread *td, struct fhlinkat_args *uap)
4453 return (kern_fhlinkat(td, uap->tofd, uap->to, UIO_USERSPACE, uap->fhp));
4457 kern_fhlinkat(struct thread *td, int fd, const char *path,
4458 enum uio_seg pathseg, fhandle_t *fhp)
4465 error = priv_check(td, PRIV_VFS_GETFH);
4468 error = copyin(fhp, &fh, sizeof(fh));
4473 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4475 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_SHARED, &vp);
4480 error = kern_linkat_vp(td, vp, fd, path, pathseg);
4481 } while (error == EAGAIN || error == ERELOOKUP);
4485 #ifndef _SYS_SYSPROTO_H_
4486 struct fhreadlink_args {
4493 sys_fhreadlink(struct thread *td, struct fhreadlink_args *uap)
4500 error = priv_check(td, PRIV_VFS_GETFH);
4503 if (uap->bufsize > IOSIZE_MAX)
4505 error = copyin(uap->fhp, &fh, sizeof(fh));
4508 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4510 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_SHARED, &vp);
4514 error = kern_readlink_vp(vp, uap->buf, UIO_USERSPACE, uap->bufsize, td);
4520 * syscall for the rpc.lockd to use to translate a NFS file handle into an
4523 * warning: do not remove the priv_check() call or this becomes one giant
4526 #ifndef _SYS_SYSPROTO_H_
4527 struct fhopen_args {
4528 const struct fhandle *u_fhp;
4533 sys_fhopen(struct thread *td, struct fhopen_args *uap)
4535 return (kern_fhopen(td, uap->u_fhp, uap->flags));
4539 kern_fhopen(struct thread *td, const struct fhandle *u_fhp, int flags)
4548 error = priv_check(td, PRIV_VFS_FHOPEN);
4552 fmode = FFLAGS(flags);
4553 /* why not allow a non-read/write open for our lockd? */
4554 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
4556 error = copyin(u_fhp, &fhp, sizeof(fhp));
4559 /* find the mount point */
4560 mp = vfs_busyfs(&fhp.fh_fsid);
4563 /* now give me my vnode, it gets returned to me locked */
4564 error = VFS_FHTOVP(mp, &fhp.fh_fid, LK_EXCLUSIVE, &vp);
4569 error = falloc_noinstall(td, &fp);
4575 * An extra reference on `fp' has been held for us by
4576 * falloc_noinstall().
4582 error = vn_open_vnode(vp, fmode, td->td_ucred, td, fp);
4584 KASSERT(fp->f_ops == &badfileops,
4585 ("VOP_OPEN in fhopen() set f_ops"));
4586 KASSERT(td->td_dupfd < 0,
4587 ("fhopen() encountered fdopen()"));
4596 finit_vnode(fp, fmode, NULL, &vnops);
4598 if ((fmode & O_TRUNC) != 0) {
4599 error = fo_truncate(fp, 0, td->td_ucred, td);
4604 error = finstall(td, fp, &indx, fmode, NULL);
4607 td->td_retval[0] = indx;
4612 * Stat an (NFS) file handle.
4614 #ifndef _SYS_SYSPROTO_H_
4615 struct fhstat_args {
4616 struct fhandle *u_fhp;
4621 sys_fhstat(struct thread *td, struct fhstat_args *uap)
4627 error = copyin(uap->u_fhp, &fh, sizeof(fh));
4630 error = kern_fhstat(td, fh, &sb);
4632 error = copyout(&sb, uap->sb, sizeof(sb));
4637 kern_fhstat(struct thread *td, struct fhandle fh, struct stat *sb)
4643 error = priv_check(td, PRIV_VFS_FHSTAT);
4646 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4648 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_EXCLUSIVE, &vp);
4652 error = VOP_STAT(vp, sb, td->td_ucred, NOCRED, td);
4658 * Implement fstatfs() for (NFS) file handles.
4660 #ifndef _SYS_SYSPROTO_H_
4661 struct fhstatfs_args {
4662 struct fhandle *u_fhp;
4667 sys_fhstatfs(struct thread *td, struct fhstatfs_args *uap)
4673 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4676 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
4677 error = kern_fhstatfs(td, fh, sfp);
4679 error = copyout(sfp, uap->buf, sizeof(*sfp));
4680 free(sfp, M_STATFS);
4685 kern_fhstatfs(struct thread *td, fhandle_t fh, struct statfs *buf)
4691 error = priv_check(td, PRIV_VFS_FHSTATFS);
4694 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4696 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_EXCLUSIVE, &vp);
4702 error = prison_canseemount(td->td_ucred, mp);
4706 error = mac_mount_check_stat(td->td_ucred, mp);
4710 error = VFS_STATFS(mp, buf);
4717 * Unlike madvise(2), we do not make a best effort to remember every
4718 * possible caching hint. Instead, we remember the last setting with
4719 * the exception that we will allow POSIX_FADV_NORMAL to adjust the
4720 * region of any current setting.
4723 kern_posix_fadvise(struct thread *td, int fd, off_t offset, off_t len,
4726 struct fadvise_info *fa, *new;
4732 if (offset < 0 || len < 0 || offset > OFF_MAX - len)
4734 AUDIT_ARG_VALUE(advice);
4736 case POSIX_FADV_SEQUENTIAL:
4737 case POSIX_FADV_RANDOM:
4738 case POSIX_FADV_NOREUSE:
4739 new = malloc(sizeof(*fa), M_FADVISE, M_WAITOK);
4741 case POSIX_FADV_NORMAL:
4742 case POSIX_FADV_WILLNEED:
4743 case POSIX_FADV_DONTNEED:
4749 /* XXX: CAP_POSIX_FADVISE? */
4751 error = fget(td, fd, &cap_no_rights, &fp);
4754 AUDIT_ARG_FILE(td->td_proc, fp);
4755 if ((fp->f_ops->fo_flags & DFLAG_SEEKABLE) == 0) {
4759 if (fp->f_type != DTYPE_VNODE) {
4764 if (vp->v_type != VREG) {
4771 end = offset + len - 1;
4773 case POSIX_FADV_SEQUENTIAL:
4774 case POSIX_FADV_RANDOM:
4775 case POSIX_FADV_NOREUSE:
4777 * Try to merge any existing non-standard region with
4778 * this new region if possible, otherwise create a new
4779 * non-standard region for this request.
4781 mtx_pool_lock(mtxpool_sleep, fp);
4783 if (fa != NULL && fa->fa_advice == advice &&
4784 ((fa->fa_start <= end && fa->fa_end >= offset) ||
4785 (end != OFF_MAX && fa->fa_start == end + 1) ||
4786 (fa->fa_end != OFF_MAX && fa->fa_end + 1 == offset))) {
4787 if (offset < fa->fa_start)
4788 fa->fa_start = offset;
4789 if (end > fa->fa_end)
4792 new->fa_advice = advice;
4793 new->fa_start = offset;
4798 mtx_pool_unlock(mtxpool_sleep, fp);
4800 case POSIX_FADV_NORMAL:
4802 * If a the "normal" region overlaps with an existing
4803 * non-standard region, trim or remove the
4804 * non-standard region.
4806 mtx_pool_lock(mtxpool_sleep, fp);
4809 if (offset <= fa->fa_start && end >= fa->fa_end) {
4811 fp->f_advice = NULL;
4812 } else if (offset <= fa->fa_start &&
4813 end >= fa->fa_start)
4814 fa->fa_start = end + 1;
4815 else if (offset <= fa->fa_end && end >= fa->fa_end)
4816 fa->fa_end = offset - 1;
4817 else if (offset >= fa->fa_start && end <= fa->fa_end) {
4819 * If the "normal" region is a middle
4820 * portion of the existing
4821 * non-standard region, just remove
4822 * the whole thing rather than picking
4823 * one side or the other to
4827 fp->f_advice = NULL;
4830 mtx_pool_unlock(mtxpool_sleep, fp);
4832 case POSIX_FADV_WILLNEED:
4833 case POSIX_FADV_DONTNEED:
4834 error = VOP_ADVISE(vp, offset, end, advice);
4840 free(new, M_FADVISE);
4845 sys_posix_fadvise(struct thread *td, struct posix_fadvise_args *uap)
4849 error = kern_posix_fadvise(td, uap->fd, uap->offset, uap->len,
4851 return (kern_posix_error(td, error));
4855 kern_copy_file_range(struct thread *td, int infd, off_t *inoffp, int outfd,
4856 off_t *outoffp, size_t len, unsigned int flags)
4858 struct file *infp, *outfp;
4859 struct vnode *invp, *outvp;
4862 void *rl_rcookie, *rl_wcookie;
4863 off_t savinoff, savoutoff;
4865 infp = outfp = NULL;
4866 rl_rcookie = rl_wcookie = NULL;
4875 if (len > SSIZE_MAX)
4877 * Although the len argument is size_t, the return argument
4878 * is ssize_t (which is signed). Therefore a size that won't
4879 * fit in ssize_t can't be returned.
4883 /* Get the file structures for the file descriptors. */
4884 error = fget_read(td, infd, &cap_read_rights, &infp);
4887 if (infp->f_ops == &badfileops) {
4891 if (infp->f_vnode == NULL) {
4895 error = fget_write(td, outfd, &cap_write_rights, &outfp);
4898 if (outfp->f_ops == &badfileops) {
4902 if (outfp->f_vnode == NULL) {
4907 /* Set the offset pointers to the correct place. */
4909 inoffp = &infp->f_offset;
4910 if (outoffp == NULL)
4911 outoffp = &outfp->f_offset;
4913 savoutoff = *outoffp;
4915 invp = infp->f_vnode;
4916 outvp = outfp->f_vnode;
4917 /* Sanity check the f_flag bits. */
4918 if ((outfp->f_flag & (FWRITE | FAPPEND)) != FWRITE ||
4919 (infp->f_flag & FREAD) == 0) {
4924 /* If len == 0, just return 0. */
4929 * If infp and outfp refer to the same file, the byte ranges cannot
4932 if (invp == outvp && ((savinoff <= savoutoff && savinoff + len >
4933 savoutoff) || (savinoff > savoutoff && savoutoff + len >
4939 /* Range lock the byte ranges for both invp and outvp. */
4941 rl_wcookie = vn_rangelock_wlock(outvp, *outoffp, *outoffp +
4943 rl_rcookie = vn_rangelock_tryrlock(invp, *inoffp, *inoffp +
4945 if (rl_rcookie != NULL)
4947 vn_rangelock_unlock(outvp, rl_wcookie);
4948 rl_rcookie = vn_rangelock_rlock(invp, *inoffp, *inoffp + len);
4949 vn_rangelock_unlock(invp, rl_rcookie);
4953 error = vn_copy_file_range(invp, inoffp, outvp, outoffp, &retlen,
4954 flags, infp->f_cred, outfp->f_cred, td);
4956 if (rl_rcookie != NULL)
4957 vn_rangelock_unlock(invp, rl_rcookie);
4958 if (rl_wcookie != NULL)
4959 vn_rangelock_unlock(outvp, rl_wcookie);
4960 if (savinoff != -1 && (error == EINTR || error == ERESTART)) {
4962 *outoffp = savoutoff;
4968 td->td_retval[0] = retlen;
4973 sys_copy_file_range(struct thread *td, struct copy_file_range_args *uap)
4975 off_t inoff, outoff, *inoffp, *outoffp;
4978 inoffp = outoffp = NULL;
4979 if (uap->inoffp != NULL) {
4980 error = copyin(uap->inoffp, &inoff, sizeof(off_t));
4985 if (uap->outoffp != NULL) {
4986 error = copyin(uap->outoffp, &outoff, sizeof(off_t));
4991 error = kern_copy_file_range(td, uap->infd, inoffp, uap->outfd,
4992 outoffp, uap->len, uap->flags);
4993 if (error == 0 && uap->inoffp != NULL)
4994 error = copyout(inoffp, uap->inoffp, sizeof(off_t));
4995 if (error == 0 && uap->outoffp != NULL)
4996 error = copyout(outoffp, uap->outoffp, sizeof(off_t));