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 <ufs/ufs/quota.h>
92 MALLOC_DEFINE(M_FADVISE, "fadvise", "posix_fadvise(2) information");
94 SDT_PROVIDER_DEFINE(vfs);
95 SDT_PROBE_DEFINE2(vfs, , stat, mode, "char *", "int");
96 SDT_PROBE_DEFINE2(vfs, , stat, reg, "char *", "int");
98 static int kern_chflagsat(struct thread *td, int fd, const char *path,
99 enum uio_seg pathseg, u_long flags, int atflag);
100 static int setfflags(struct thread *td, struct vnode *, u_long);
101 static int getutimes(const struct timeval *, enum uio_seg, struct timespec *);
102 static int getutimens(const struct timespec *, enum uio_seg,
103 struct timespec *, int *);
104 static int setutimes(struct thread *td, struct vnode *,
105 const struct timespec *, int, int);
106 static int vn_access(struct vnode *vp, int user_flags, struct ucred *cred,
108 static int kern_fhlinkat(struct thread *td, int fd, const char *path,
109 enum uio_seg pathseg, fhandle_t *fhp);
110 static int kern_getfhat(struct thread *td, int flags, int fd,
111 const char *path, enum uio_seg pathseg, fhandle_t *fhp);
112 static int kern_readlink_vp(struct vnode *vp, char *buf, enum uio_seg bufseg,
113 size_t count, struct thread *td);
114 static int kern_linkat_vp(struct thread *td, struct vnode *vp, int fd,
115 const char *path, enum uio_seg segflag);
118 kern_sync(struct thread *td)
120 struct mount *mp, *nmp;
123 mtx_lock(&mountlist_mtx);
124 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
125 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK)) {
126 nmp = TAILQ_NEXT(mp, mnt_list);
129 if ((mp->mnt_flag & MNT_RDONLY) == 0 &&
130 vn_start_write(NULL, &mp, V_NOWAIT) == 0) {
131 save = curthread_pflags_set(TDP_SYNCIO);
132 vfs_periodic(mp, MNT_NOWAIT);
133 VFS_SYNC(mp, MNT_NOWAIT);
134 curthread_pflags_restore(save);
135 vn_finished_write(mp);
137 mtx_lock(&mountlist_mtx);
138 nmp = TAILQ_NEXT(mp, mnt_list);
141 mtx_unlock(&mountlist_mtx);
146 * Sync each mounted filesystem.
148 #ifndef _SYS_SYSPROTO_H_
155 sys_sync(struct thread *td, struct sync_args *uap)
158 return (kern_sync(td));
162 * Change filesystem quotas.
164 #ifndef _SYS_SYSPROTO_H_
165 struct quotactl_args {
173 sys_quotactl(struct thread *td, struct quotactl_args *uap)
179 AUDIT_ARG_CMD(uap->cmd);
180 AUDIT_ARG_UID(uap->uid);
181 if (!prison_allow(td->td_ucred, PR_ALLOW_QUOTAS))
183 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
185 if ((error = namei(&nd)) != 0)
187 NDFREE(&nd, NDF_ONLY_PNBUF);
188 mp = nd.ni_vp->v_mount;
191 error = vfs_busy(mp, 0);
196 error = VFS_QUOTACTL(mp, uap->cmd, uap->uid, uap->arg);
199 * Since quota on operation typically needs to open quota
200 * file, the Q_QUOTAON handler needs to unbusy the mount point
201 * before calling into namei. Otherwise, unmount might be
202 * started between two vfs_busy() invocations (first is our,
203 * second is from mount point cross-walk code in lookup()),
206 * Require that Q_QUOTAON handles the vfs_busy() reference on
207 * its own, always returning with ubusied mount point.
209 if ((uap->cmd >> SUBCMDSHIFT) != Q_QUOTAON &&
210 (uap->cmd >> SUBCMDSHIFT) != Q_QUOTAOFF)
217 * Used by statfs conversion routines to scale the block size up if
218 * necessary so that all of the block counts are <= 'max_size'. Note
219 * that 'max_size' should be a bitmask, i.e. 2^n - 1 for some non-zero
223 statfs_scale_blocks(struct statfs *sf, long max_size)
228 KASSERT(powerof2(max_size + 1), ("%s: invalid max_size", __func__));
231 * Attempt to scale the block counts to give a more accurate
232 * overview to userland of the ratio of free space to used
233 * space. To do this, find the largest block count and compute
234 * a divisor that lets it fit into a signed integer <= max_size.
236 if (sf->f_bavail < 0)
237 count = -sf->f_bavail;
239 count = sf->f_bavail;
240 count = MAX(sf->f_blocks, MAX(sf->f_bfree, count));
241 if (count <= max_size)
244 count >>= flsl(max_size);
251 sf->f_bsize <<= shift;
252 sf->f_blocks >>= shift;
253 sf->f_bfree >>= shift;
254 sf->f_bavail >>= shift;
258 kern_do_statfs(struct thread *td, struct mount *mp, struct statfs *buf)
264 error = vfs_busy(mp, 0);
269 error = mac_mount_check_stat(td->td_ucred, mp);
273 error = VFS_STATFS(mp, buf);
276 if (priv_check_cred_vfs_generation(td->td_ucred)) {
277 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
278 prison_enforce_statfs(td->td_ucred, mp, buf);
286 * Get filesystem statistics.
288 #ifndef _SYS_SYSPROTO_H_
295 sys_statfs(struct thread *td, struct statfs_args *uap)
300 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
301 error = kern_statfs(td, uap->path, UIO_USERSPACE, sfp);
303 error = copyout(sfp, uap->buf, sizeof(struct statfs));
309 kern_statfs(struct thread *td, const char *path, enum uio_seg pathseg,
316 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
321 mp = nd.ni_vp->v_mount;
323 NDFREE(&nd, NDF_ONLY_PNBUF);
325 return (kern_do_statfs(td, mp, buf));
329 * Get filesystem statistics.
331 #ifndef _SYS_SYSPROTO_H_
332 struct fstatfs_args {
338 sys_fstatfs(struct thread *td, struct fstatfs_args *uap)
343 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
344 error = kern_fstatfs(td, uap->fd, sfp);
346 error = copyout(sfp, uap->buf, sizeof(struct statfs));
352 kern_fstatfs(struct thread *td, int fd, struct statfs *buf)
360 error = getvnode(td, fd, &cap_fstatfs_rights, &fp);
364 vn_lock(vp, LK_SHARED | LK_RETRY);
366 AUDIT_ARG_VNODE1(vp);
373 return (kern_do_statfs(td, mp, buf));
377 * Get statistics on all filesystems.
379 #ifndef _SYS_SYSPROTO_H_
380 struct getfsstat_args {
387 sys_getfsstat(struct thread *td, struct getfsstat_args *uap)
392 if (uap->bufsize < 0 || uap->bufsize > SIZE_MAX)
394 error = kern_getfsstat(td, &uap->buf, uap->bufsize, &count,
395 UIO_USERSPACE, uap->mode);
397 td->td_retval[0] = count;
402 * If (bufsize > 0 && bufseg == UIO_SYSSPACE)
403 * The caller is responsible for freeing memory which will be allocated
407 kern_getfsstat(struct thread *td, struct statfs **buf, size_t bufsize,
408 size_t *countp, enum uio_seg bufseg, int mode)
410 struct mount *mp, *nmp;
411 struct statfs *sfsp, *sp, *sptmp, *tofree;
412 size_t count, maxcount;
420 if (bufseg == UIO_SYSSPACE)
425 maxcount = bufsize / sizeof(struct statfs);
429 } else if (bufseg == UIO_USERSPACE) {
432 } else /* if (bufseg == UIO_SYSSPACE) */ {
434 mtx_lock(&mountlist_mtx);
435 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
438 mtx_unlock(&mountlist_mtx);
439 if (maxcount > count)
441 tofree = sfsp = *buf = malloc(maxcount * sizeof(struct statfs),
448 * If there is no target buffer they only want the count.
450 * This could be TAILQ_FOREACH but it is open-coded to match the original
454 mtx_lock(&mountlist_mtx);
455 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
456 if (prison_canseemount(td->td_ucred, mp) != 0) {
457 nmp = TAILQ_NEXT(mp, mnt_list);
461 if (mac_mount_check_stat(td->td_ucred, mp) != 0) {
462 nmp = TAILQ_NEXT(mp, mnt_list);
467 nmp = TAILQ_NEXT(mp, mnt_list);
469 mtx_unlock(&mountlist_mtx);
475 * They want the entire thing.
477 * Short-circuit the corner case of no room for anything, avoids
484 mtx_lock(&mountlist_mtx);
485 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
486 if (prison_canseemount(td->td_ucred, mp) != 0) {
487 nmp = TAILQ_NEXT(mp, mnt_list);
491 if (mac_mount_check_stat(td->td_ucred, mp) != 0) {
492 nmp = TAILQ_NEXT(mp, mnt_list);
496 if (mode == MNT_WAIT) {
497 if (vfs_busy(mp, MBF_MNTLSTLOCK) != 0) {
499 * If vfs_busy() failed, and MBF_NOWAIT
500 * wasn't passed, then the mp is gone.
501 * Furthermore, because of MBF_MNTLSTLOCK,
502 * the mountlist_mtx was dropped. We have
503 * no other choice than to start over.
505 mtx_unlock(&mountlist_mtx);
506 free(tofree, M_STATFS);
510 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK) != 0) {
511 nmp = TAILQ_NEXT(mp, mnt_list);
517 * If MNT_NOWAIT is specified, do not refresh
520 if (mode != MNT_NOWAIT) {
521 error = VFS_STATFS(mp, sp);
523 mtx_lock(&mountlist_mtx);
524 nmp = TAILQ_NEXT(mp, mnt_list);
529 if (priv_check_cred_vfs_generation(td->td_ucred)) {
530 sptmp = malloc(sizeof(struct statfs), M_STATFS,
533 sptmp->f_fsid.val[0] = sptmp->f_fsid.val[1] = 0;
534 prison_enforce_statfs(td->td_ucred, mp, sptmp);
538 if (bufseg == UIO_SYSSPACE) {
539 bcopy(sp, sfsp, sizeof(*sp));
540 free(sptmp, M_STATFS);
541 } else /* if (bufseg == UIO_USERSPACE) */ {
542 error = copyout(sp, sfsp, sizeof(*sp));
543 free(sptmp, M_STATFS);
552 if (count == maxcount) {
557 mtx_lock(&mountlist_mtx);
558 nmp = TAILQ_NEXT(mp, mnt_list);
561 mtx_unlock(&mountlist_mtx);
567 #ifdef COMPAT_FREEBSD4
569 * Get old format filesystem statistics.
571 static void freebsd4_cvtstatfs(struct statfs *, struct ostatfs *);
573 #ifndef _SYS_SYSPROTO_H_
574 struct freebsd4_statfs_args {
580 freebsd4_statfs(struct thread *td, struct freebsd4_statfs_args *uap)
586 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
587 error = kern_statfs(td, uap->path, UIO_USERSPACE, sfp);
589 freebsd4_cvtstatfs(sfp, &osb);
590 error = copyout(&osb, uap->buf, sizeof(osb));
597 * Get filesystem statistics.
599 #ifndef _SYS_SYSPROTO_H_
600 struct freebsd4_fstatfs_args {
606 freebsd4_fstatfs(struct thread *td, struct freebsd4_fstatfs_args *uap)
612 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
613 error = kern_fstatfs(td, uap->fd, sfp);
615 freebsd4_cvtstatfs(sfp, &osb);
616 error = copyout(&osb, uap->buf, sizeof(osb));
623 * Get statistics on all filesystems.
625 #ifndef _SYS_SYSPROTO_H_
626 struct freebsd4_getfsstat_args {
633 freebsd4_getfsstat(struct thread *td, struct freebsd4_getfsstat_args *uap)
635 struct statfs *buf, *sp;
640 if (uap->bufsize < 0)
642 count = uap->bufsize / sizeof(struct ostatfs);
643 if (count > SIZE_MAX / sizeof(struct statfs))
645 size = count * sizeof(struct statfs);
646 error = kern_getfsstat(td, &buf, size, &count, UIO_SYSSPACE,
649 td->td_retval[0] = count;
652 while (count != 0 && error == 0) {
653 freebsd4_cvtstatfs(sp, &osb);
654 error = copyout(&osb, uap->buf, sizeof(osb));
665 * Implement fstatfs() for (NFS) file handles.
667 #ifndef _SYS_SYSPROTO_H_
668 struct freebsd4_fhstatfs_args {
669 struct fhandle *u_fhp;
674 freebsd4_fhstatfs(struct thread *td, struct freebsd4_fhstatfs_args *uap)
681 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
684 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
685 error = kern_fhstatfs(td, fh, sfp);
687 freebsd4_cvtstatfs(sfp, &osb);
688 error = copyout(&osb, uap->buf, sizeof(osb));
695 * Convert a new format statfs structure to an old format statfs structure.
698 freebsd4_cvtstatfs(struct statfs *nsp, struct ostatfs *osp)
701 statfs_scale_blocks(nsp, LONG_MAX);
702 bzero(osp, sizeof(*osp));
703 osp->f_bsize = nsp->f_bsize;
704 osp->f_iosize = MIN(nsp->f_iosize, LONG_MAX);
705 osp->f_blocks = nsp->f_blocks;
706 osp->f_bfree = nsp->f_bfree;
707 osp->f_bavail = nsp->f_bavail;
708 osp->f_files = MIN(nsp->f_files, LONG_MAX);
709 osp->f_ffree = MIN(nsp->f_ffree, LONG_MAX);
710 osp->f_owner = nsp->f_owner;
711 osp->f_type = nsp->f_type;
712 osp->f_flags = nsp->f_flags;
713 osp->f_syncwrites = MIN(nsp->f_syncwrites, LONG_MAX);
714 osp->f_asyncwrites = MIN(nsp->f_asyncwrites, LONG_MAX);
715 osp->f_syncreads = MIN(nsp->f_syncreads, LONG_MAX);
716 osp->f_asyncreads = MIN(nsp->f_asyncreads, LONG_MAX);
717 strlcpy(osp->f_fstypename, nsp->f_fstypename,
718 MIN(MFSNAMELEN, OMFSNAMELEN));
719 strlcpy(osp->f_mntonname, nsp->f_mntonname,
720 MIN(MNAMELEN, OMNAMELEN));
721 strlcpy(osp->f_mntfromname, nsp->f_mntfromname,
722 MIN(MNAMELEN, OMNAMELEN));
723 osp->f_fsid = nsp->f_fsid;
725 #endif /* COMPAT_FREEBSD4 */
727 #if defined(COMPAT_FREEBSD11)
729 * Get old format filesystem statistics.
731 static void freebsd11_cvtstatfs(struct statfs *, struct freebsd11_statfs *);
734 freebsd11_statfs(struct thread *td, struct freebsd11_statfs_args *uap)
736 struct freebsd11_statfs osb;
740 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
741 error = kern_statfs(td, uap->path, UIO_USERSPACE, sfp);
743 freebsd11_cvtstatfs(sfp, &osb);
744 error = copyout(&osb, uap->buf, sizeof(osb));
751 * Get filesystem statistics.
754 freebsd11_fstatfs(struct thread *td, struct freebsd11_fstatfs_args *uap)
756 struct freebsd11_statfs osb;
760 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
761 error = kern_fstatfs(td, uap->fd, sfp);
763 freebsd11_cvtstatfs(sfp, &osb);
764 error = copyout(&osb, uap->buf, sizeof(osb));
771 * Get statistics on all filesystems.
774 freebsd11_getfsstat(struct thread *td, struct freebsd11_getfsstat_args *uap)
776 struct freebsd11_statfs osb;
777 struct statfs *buf, *sp;
781 count = uap->bufsize / sizeof(struct ostatfs);
782 size = count * sizeof(struct statfs);
783 error = kern_getfsstat(td, &buf, size, &count, UIO_SYSSPACE,
786 td->td_retval[0] = count;
789 while (count > 0 && error == 0) {
790 freebsd11_cvtstatfs(sp, &osb);
791 error = copyout(&osb, uap->buf, sizeof(osb));
802 * Implement fstatfs() for (NFS) file handles.
805 freebsd11_fhstatfs(struct thread *td, struct freebsd11_fhstatfs_args *uap)
807 struct freebsd11_statfs osb;
812 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
815 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
816 error = kern_fhstatfs(td, fh, sfp);
818 freebsd11_cvtstatfs(sfp, &osb);
819 error = copyout(&osb, uap->buf, sizeof(osb));
826 * Convert a new format statfs structure to an old format statfs structure.
829 freebsd11_cvtstatfs(struct statfs *nsp, struct freebsd11_statfs *osp)
832 bzero(osp, sizeof(*osp));
833 osp->f_version = FREEBSD11_STATFS_VERSION;
834 osp->f_type = nsp->f_type;
835 osp->f_flags = nsp->f_flags;
836 osp->f_bsize = nsp->f_bsize;
837 osp->f_iosize = nsp->f_iosize;
838 osp->f_blocks = nsp->f_blocks;
839 osp->f_bfree = nsp->f_bfree;
840 osp->f_bavail = nsp->f_bavail;
841 osp->f_files = nsp->f_files;
842 osp->f_ffree = nsp->f_ffree;
843 osp->f_syncwrites = nsp->f_syncwrites;
844 osp->f_asyncwrites = nsp->f_asyncwrites;
845 osp->f_syncreads = nsp->f_syncreads;
846 osp->f_asyncreads = nsp->f_asyncreads;
847 osp->f_namemax = nsp->f_namemax;
848 osp->f_owner = nsp->f_owner;
849 osp->f_fsid = nsp->f_fsid;
850 strlcpy(osp->f_fstypename, nsp->f_fstypename,
851 MIN(MFSNAMELEN, sizeof(osp->f_fstypename)));
852 strlcpy(osp->f_mntonname, nsp->f_mntonname,
853 MIN(MNAMELEN, sizeof(osp->f_mntonname)));
854 strlcpy(osp->f_mntfromname, nsp->f_mntfromname,
855 MIN(MNAMELEN, sizeof(osp->f_mntfromname)));
857 #endif /* COMPAT_FREEBSD11 */
860 * Change current working directory to a given file descriptor.
862 #ifndef _SYS_SYSPROTO_H_
868 sys_fchdir(struct thread *td, struct fchdir_args *uap)
870 struct vnode *vp, *tdp;
875 AUDIT_ARG_FD(uap->fd);
876 error = getvnode(td, uap->fd, &cap_fchdir_rights,
883 vn_lock(vp, LK_SHARED | LK_RETRY);
884 AUDIT_ARG_VNODE1(vp);
885 error = change_dir(vp, td);
886 while (!error && (mp = vp->v_mountedhere) != NULL) {
889 error = VFS_ROOT(mp, LK_SHARED, &tdp);
906 * Change current working directory (``.'').
908 #ifndef _SYS_SYSPROTO_H_
914 sys_chdir(struct thread *td, struct chdir_args *uap)
917 return (kern_chdir(td, uap->path, UIO_USERSPACE));
921 kern_chdir(struct thread *td, const char *path, enum uio_seg pathseg)
926 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
928 if ((error = namei(&nd)) != 0)
930 if ((error = change_dir(nd.ni_vp, td)) != 0) {
932 NDFREE(&nd, NDF_ONLY_PNBUF);
935 VOP_UNLOCK(nd.ni_vp);
936 NDFREE(&nd, NDF_ONLY_PNBUF);
937 pwd_chdir(td, nd.ni_vp);
942 * Change notion of root (``/'') directory.
944 #ifndef _SYS_SYSPROTO_H_
950 sys_chroot(struct thread *td, struct chroot_args *uap)
955 error = priv_check(td, PRIV_VFS_CHROOT);
958 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
959 UIO_USERSPACE, uap->path, td);
963 error = change_dir(nd.ni_vp, td);
967 error = mac_vnode_check_chroot(td->td_ucred, nd.ni_vp);
971 VOP_UNLOCK(nd.ni_vp);
972 error = pwd_chroot(td, nd.ni_vp);
974 NDFREE(&nd, NDF_ONLY_PNBUF);
979 NDFREE(&nd, NDF_ONLY_PNBUF);
984 * Common routine for chroot and chdir. Callers must provide a locked vnode
988 change_dir(struct vnode *vp, struct thread *td)
994 ASSERT_VOP_LOCKED(vp, "change_dir(): vp not locked");
995 if (vp->v_type != VDIR)
998 error = mac_vnode_check_chdir(td->td_ucred, vp);
1002 return (VOP_ACCESS(vp, VEXEC, td->td_ucred, td));
1005 static __inline void
1006 flags_to_rights(int flags, cap_rights_t *rightsp)
1009 if (flags & O_EXEC) {
1010 cap_rights_set_one(rightsp, CAP_FEXECVE);
1012 switch ((flags & O_ACCMODE)) {
1014 cap_rights_set_one(rightsp, CAP_READ);
1017 cap_rights_set_one(rightsp, CAP_READ);
1020 cap_rights_set_one(rightsp, CAP_WRITE);
1021 if (!(flags & (O_APPEND | O_TRUNC)))
1022 cap_rights_set_one(rightsp, CAP_SEEK);
1027 if (flags & O_CREAT)
1028 cap_rights_set_one(rightsp, CAP_CREATE);
1030 if (flags & O_TRUNC)
1031 cap_rights_set_one(rightsp, CAP_FTRUNCATE);
1033 if (flags & (O_SYNC | O_FSYNC))
1034 cap_rights_set_one(rightsp, CAP_FSYNC);
1036 if (flags & (O_EXLOCK | O_SHLOCK))
1037 cap_rights_set_one(rightsp, CAP_FLOCK);
1041 * Check permissions, allocate an open file structure, and call the device
1042 * open routine if any.
1044 #ifndef _SYS_SYSPROTO_H_
1052 sys_open(struct thread *td, struct open_args *uap)
1055 return (kern_openat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1056 uap->flags, uap->mode));
1059 #ifndef _SYS_SYSPROTO_H_
1060 struct openat_args {
1068 sys_openat(struct thread *td, struct openat_args *uap)
1071 AUDIT_ARG_FD(uap->fd);
1072 return (kern_openat(td, uap->fd, uap->path, UIO_USERSPACE, uap->flag,
1077 kern_openat(struct thread *td, int fd, const char *path, enum uio_seg pathseg,
1078 int flags, int mode)
1080 struct proc *p = td->td_proc;
1081 struct filedesc *fdp = p->p_fd;
1084 struct nameidata nd;
1085 cap_rights_t rights;
1086 int cmode, error, indx;
1090 AUDIT_ARG_FFLAGS(flags);
1091 AUDIT_ARG_MODE(mode);
1092 cap_rights_init_one(&rights, CAP_LOOKUP);
1093 flags_to_rights(flags, &rights);
1095 * Only one of the O_EXEC, O_RDONLY, O_WRONLY and O_RDWR flags
1098 if (flags & O_EXEC) {
1099 if (flags & O_ACCMODE)
1101 } else if ((flags & O_ACCMODE) == O_ACCMODE) {
1104 flags = FFLAGS(flags);
1108 * Allocate a file structure. The descriptor to reference it
1109 * is allocated and set by finstall() below.
1111 error = falloc_noinstall(td, &fp);
1115 * An extra reference on `fp' has been held for us by
1116 * falloc_noinstall().
1118 /* Set the flags early so the finit in devfs can pick them up. */
1119 fp->f_flag = flags & FMASK;
1120 cmode = ((mode & ~fdp->fd_cmask) & ALLPERMS) & ~S_ISTXT;
1121 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, fd,
1123 td->td_dupfd = -1; /* XXX check for fdopen */
1124 error = vn_open(&nd, &flags, cmode, fp);
1127 * If the vn_open replaced the method vector, something
1128 * wonderous happened deep below and we just pass it up
1129 * pretending we know what we do.
1131 if (error == ENXIO && fp->f_ops != &badfileops)
1135 * Handle special fdopen() case. bleh.
1137 * Don't do this for relative (capability) lookups; we don't
1138 * understand exactly what would happen, and we don't think
1139 * that it ever should.
1141 if ((nd.ni_lcf & NI_LCF_STRICTRELATIVE) == 0 &&
1142 (error == ENODEV || error == ENXIO) &&
1143 td->td_dupfd >= 0) {
1144 error = dupfdopen(td, fdp, td->td_dupfd, flags, error,
1153 NDFREE(&nd, NDF_ONLY_PNBUF);
1157 * Store the vnode, for any f_type. Typically, the vnode use
1158 * count is decremented by direct call to vn_closefile() for
1159 * files that switched type in the cdevsw fdopen() method.
1163 * If the file wasn't claimed by devfs bind it to the normal
1164 * vnode operations here.
1166 if (fp->f_ops == &badfileops) {
1167 KASSERT(vp->v_type != VFIFO, ("Unexpected fifo."));
1168 fp->f_seqcount[UIO_READ] = 1;
1169 fp->f_seqcount[UIO_WRITE] = 1;
1170 finit(fp, (flags & FMASK) | (fp->f_flag & FHASLOCK),
1171 DTYPE_VNODE, vp, &vnops);
1175 if (flags & O_TRUNC) {
1176 error = fo_truncate(fp, 0, td->td_ucred, td);
1182 * If we haven't already installed the FD (for dupfdopen), do so now.
1185 struct filecaps *fcaps;
1188 if ((nd.ni_lcf & NI_LCF_STRICTRELATIVE) != 0)
1189 fcaps = &nd.ni_filecaps;
1193 error = finstall(td, fp, &indx, flags, fcaps);
1194 /* On success finstall() consumes fcaps. */
1196 filecaps_free(&nd.ni_filecaps);
1200 filecaps_free(&nd.ni_filecaps);
1204 * Release our private reference, leaving the one associated with
1205 * the descriptor table intact.
1208 td->td_retval[0] = indx;
1211 KASSERT(indx == -1, ("indx=%d, should be -1", indx));
1220 #ifndef _SYS_SYSPROTO_H_
1221 struct ocreat_args {
1227 ocreat(struct thread *td, struct ocreat_args *uap)
1230 return (kern_openat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1231 O_WRONLY | O_CREAT | O_TRUNC, uap->mode));
1233 #endif /* COMPAT_43 */
1236 * Create a special file.
1238 #ifndef _SYS_SYSPROTO_H_
1239 struct mknodat_args {
1247 sys_mknodat(struct thread *td, struct mknodat_args *uap)
1250 return (kern_mknodat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode,
1254 #if defined(COMPAT_FREEBSD11)
1256 freebsd11_mknod(struct thread *td,
1257 struct freebsd11_mknod_args *uap)
1260 return (kern_mknodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1261 uap->mode, uap->dev));
1265 freebsd11_mknodat(struct thread *td,
1266 struct freebsd11_mknodat_args *uap)
1269 return (kern_mknodat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode,
1272 #endif /* COMPAT_FREEBSD11 */
1275 kern_mknodat(struct thread *td, int fd, const char *path, enum uio_seg pathseg,
1276 int mode, dev_t dev)
1281 struct nameidata nd;
1282 int error, whiteout = 0;
1284 AUDIT_ARG_MODE(mode);
1286 switch (mode & S_IFMT) {
1289 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
1290 if (error == 0 && dev == VNOVAL)
1294 error = priv_check(td, PRIV_VFS_MKNOD_WHT);
1298 return (kern_mkfifoat(td, fd, path, pathseg, mode));
1308 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
1309 NOCACHE, pathseg, path, fd, &cap_mknodat_rights,
1311 if ((error = namei(&nd)) != 0)
1315 NDFREE(&nd, NDF_ONLY_PNBUF);
1316 if (vp == nd.ni_dvp)
1324 vattr.va_mode = (mode & ALLPERMS) &
1325 ~td->td_proc->p_fd->fd_cmask;
1326 vattr.va_rdev = dev;
1329 switch (mode & S_IFMT) {
1331 vattr.va_type = VCHR;
1334 vattr.va_type = VBLK;
1340 panic("kern_mknod: invalid mode");
1343 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1344 NDFREE(&nd, NDF_ONLY_PNBUF);
1346 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1351 if (error == 0 && !whiteout)
1352 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp,
1353 &nd.ni_cnd, &vattr);
1357 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, CREATE);
1359 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp,
1360 &nd.ni_cnd, &vattr);
1365 NDFREE(&nd, NDF_ONLY_PNBUF);
1367 vn_finished_write(mp);
1372 * Create a named pipe.
1374 #ifndef _SYS_SYSPROTO_H_
1375 struct mkfifo_args {
1381 sys_mkfifo(struct thread *td, struct mkfifo_args *uap)
1384 return (kern_mkfifoat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1388 #ifndef _SYS_SYSPROTO_H_
1389 struct mkfifoat_args {
1396 sys_mkfifoat(struct thread *td, struct mkfifoat_args *uap)
1399 return (kern_mkfifoat(td, uap->fd, uap->path, UIO_USERSPACE,
1404 kern_mkfifoat(struct thread *td, int fd, const char *path,
1405 enum uio_seg pathseg, int mode)
1409 struct nameidata nd;
1412 AUDIT_ARG_MODE(mode);
1415 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
1416 NOCACHE, pathseg, path, fd, &cap_mkfifoat_rights,
1418 if ((error = namei(&nd)) != 0)
1420 if (nd.ni_vp != NULL) {
1421 NDFREE(&nd, NDF_ONLY_PNBUF);
1422 if (nd.ni_vp == nd.ni_dvp)
1429 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1430 NDFREE(&nd, NDF_ONLY_PNBUF);
1432 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1437 vattr.va_type = VFIFO;
1438 vattr.va_mode = (mode & ALLPERMS) & ~td->td_proc->p_fd->fd_cmask;
1440 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1445 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
1452 vn_finished_write(mp);
1453 NDFREE(&nd, NDF_ONLY_PNBUF);
1458 * Make a hard file link.
1460 #ifndef _SYS_SYSPROTO_H_
1467 sys_link(struct thread *td, struct link_args *uap)
1470 return (kern_linkat(td, AT_FDCWD, AT_FDCWD, uap->path, uap->link,
1471 UIO_USERSPACE, FOLLOW));
1474 #ifndef _SYS_SYSPROTO_H_
1475 struct linkat_args {
1484 sys_linkat(struct thread *td, struct linkat_args *uap)
1489 if ((flag & ~(AT_SYMLINK_FOLLOW | AT_BENEATH)) != 0)
1492 return (kern_linkat(td, uap->fd1, uap->fd2, uap->path1, uap->path2,
1493 UIO_USERSPACE, ((flag & AT_SYMLINK_FOLLOW) != 0 ? FOLLOW :
1494 NOFOLLOW) | ((flag & AT_BENEATH) != 0 ? BENEATH : 0)));
1497 int hardlink_check_uid = 0;
1498 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
1499 &hardlink_check_uid, 0,
1500 "Unprivileged processes cannot create hard links to files owned by other "
1502 static int hardlink_check_gid = 0;
1503 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
1504 &hardlink_check_gid, 0,
1505 "Unprivileged processes cannot create hard links to files owned by other "
1509 can_hardlink(struct vnode *vp, struct ucred *cred)
1514 if (!hardlink_check_uid && !hardlink_check_gid)
1517 error = VOP_GETATTR(vp, &va, cred);
1521 if (hardlink_check_uid && cred->cr_uid != va.va_uid) {
1522 error = priv_check_cred(cred, PRIV_VFS_LINK);
1527 if (hardlink_check_gid && !groupmember(va.va_gid, cred)) {
1528 error = priv_check_cred(cred, PRIV_VFS_LINK);
1537 kern_linkat(struct thread *td, int fd1, int fd2, const char *path1,
1538 const char *path2, enum uio_seg segflag, int follow)
1540 struct nameidata nd;
1545 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, segflag,
1546 path1, fd1, &cap_linkat_source_rights, td);
1547 if ((error = namei(&nd)) != 0)
1549 NDFREE(&nd, NDF_ONLY_PNBUF);
1550 error = kern_linkat_vp(td, nd.ni_vp, fd2, path2, segflag);
1551 } while (error == EAGAIN);
1556 kern_linkat_vp(struct thread *td, struct vnode *vp, int fd, const char *path,
1557 enum uio_seg segflag)
1559 struct nameidata nd;
1563 if (vp->v_type == VDIR) {
1565 return (EPERM); /* POSIX */
1567 NDINIT_ATRIGHTS(&nd, CREATE,
1568 LOCKPARENT | SAVENAME | AUDITVNODE2 | NOCACHE, segflag, path, fd,
1569 &cap_linkat_target_rights, td);
1570 if ((error = namei(&nd)) == 0) {
1571 if (nd.ni_vp != NULL) {
1572 NDFREE(&nd, NDF_ONLY_PNBUF);
1573 if (nd.ni_dvp == nd.ni_vp)
1580 } else if (nd.ni_dvp->v_mount != vp->v_mount) {
1582 * Cross-device link. No need to recheck
1583 * vp->v_type, since it cannot change, except
1586 NDFREE(&nd, NDF_ONLY_PNBUF);
1590 } else if ((error = vn_lock(vp, LK_EXCLUSIVE)) == 0) {
1591 error = can_hardlink(vp, td->td_ucred);
1594 error = mac_vnode_check_link(td->td_ucred,
1595 nd.ni_dvp, vp, &nd.ni_cnd);
1600 NDFREE(&nd, NDF_ONLY_PNBUF);
1603 error = vn_start_write(vp, &mp, V_NOWAIT);
1607 NDFREE(&nd, NDF_ONLY_PNBUF);
1608 error = vn_start_write(NULL, &mp,
1614 error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
1617 vn_finished_write(mp);
1618 NDFREE(&nd, NDF_ONLY_PNBUF);
1621 NDFREE(&nd, NDF_ONLY_PNBUF);
1631 * Make a symbolic link.
1633 #ifndef _SYS_SYSPROTO_H_
1634 struct symlink_args {
1640 sys_symlink(struct thread *td, struct symlink_args *uap)
1643 return (kern_symlinkat(td, uap->path, AT_FDCWD, uap->link,
1647 #ifndef _SYS_SYSPROTO_H_
1648 struct symlinkat_args {
1655 sys_symlinkat(struct thread *td, struct symlinkat_args *uap)
1658 return (kern_symlinkat(td, uap->path1, uap->fd, uap->path2,
1663 kern_symlinkat(struct thread *td, const char *path1, int fd, const char *path2,
1664 enum uio_seg segflg)
1668 const char *syspath;
1670 struct nameidata nd;
1673 if (segflg == UIO_SYSSPACE) {
1676 tmppath = uma_zalloc(namei_zone, M_WAITOK);
1677 if ((error = copyinstr(path1, tmppath, MAXPATHLEN, NULL)) != 0)
1681 AUDIT_ARG_TEXT(syspath);
1684 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
1685 NOCACHE, segflg, path2, fd, &cap_symlinkat_rights,
1687 if ((error = namei(&nd)) != 0)
1690 NDFREE(&nd, NDF_ONLY_PNBUF);
1691 if (nd.ni_vp == nd.ni_dvp)
1699 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1700 NDFREE(&nd, NDF_ONLY_PNBUF);
1702 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1707 vattr.va_mode = ACCESSPERMS &~ td->td_proc->p_fd->fd_cmask;
1709 vattr.va_type = VLNK;
1710 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1715 error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr, syspath);
1721 NDFREE(&nd, NDF_ONLY_PNBUF);
1723 vn_finished_write(mp);
1725 if (segflg != UIO_SYSSPACE)
1726 uma_zfree(namei_zone, tmppath);
1731 * Delete a whiteout from the filesystem.
1733 #ifndef _SYS_SYSPROTO_H_
1734 struct undelete_args {
1739 sys_undelete(struct thread *td, struct undelete_args *uap)
1742 struct nameidata nd;
1747 NDINIT(&nd, DELETE, LOCKPARENT | DOWHITEOUT | AUDITVNODE1,
1748 UIO_USERSPACE, uap->path, td);
1753 if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & ISWHITEOUT)) {
1754 NDFREE(&nd, NDF_ONLY_PNBUF);
1755 if (nd.ni_vp == nd.ni_dvp)
1763 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1764 NDFREE(&nd, NDF_ONLY_PNBUF);
1766 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1770 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, DELETE);
1771 NDFREE(&nd, NDF_ONLY_PNBUF);
1773 vn_finished_write(mp);
1778 * Delete a name from the filesystem.
1780 #ifndef _SYS_SYSPROTO_H_
1781 struct unlink_args {
1786 sys_unlink(struct thread *td, struct unlink_args *uap)
1789 return (kern_funlinkat(td, AT_FDCWD, uap->path, FD_NONE, UIO_USERSPACE,
1794 kern_funlinkat_ex(struct thread *td, int dfd, const char *path, int fd,
1795 int flag, enum uio_seg pathseg, ino_t oldinum)
1798 if ((flag & ~AT_REMOVEDIR) != 0)
1801 if ((flag & AT_REMOVEDIR) != 0)
1802 return (kern_frmdirat(td, dfd, path, fd, UIO_USERSPACE, 0));
1804 return (kern_funlinkat(td, dfd, path, fd, UIO_USERSPACE, 0, 0));
1807 #ifndef _SYS_SYSPROTO_H_
1808 struct unlinkat_args {
1815 sys_unlinkat(struct thread *td, struct unlinkat_args *uap)
1818 return (kern_funlinkat_ex(td, uap->fd, uap->path, FD_NONE, uap->flag,
1822 #ifndef _SYS_SYSPROTO_H_
1823 struct funlinkat_args {
1831 sys_funlinkat(struct thread *td, struct funlinkat_args *uap)
1834 return (kern_funlinkat_ex(td, uap->dfd, uap->path, uap->fd, uap->flag,
1839 kern_funlinkat(struct thread *td, int dfd, const char *path, int fd,
1840 enum uio_seg pathseg, int flag, ino_t oldinum)
1845 struct nameidata nd;
1850 if (fd != FD_NONE) {
1851 error = getvnode(td, fd, &cap_no_rights, &fp);
1858 NDINIT_ATRIGHTS(&nd, DELETE, LOCKPARENT | LOCKLEAF | AUDITVNODE1 |
1859 ((flag & AT_BENEATH) != 0 ? BENEATH : 0),
1860 pathseg, path, dfd, &cap_unlinkat_rights, td);
1861 if ((error = namei(&nd)) != 0) {
1862 if (error == EINVAL)
1867 if (vp->v_type == VDIR && oldinum == 0) {
1868 error = EPERM; /* POSIX */
1869 } else if (oldinum != 0 &&
1870 ((error = vn_stat(vp, &sb, td->td_ucred, NOCRED, td)) == 0) &&
1871 sb.st_ino != oldinum) {
1872 error = EIDRM; /* Identifier removed */
1873 } else if (fp != NULL && fp->f_vnode != vp) {
1874 if (VN_IS_DOOMED(fp->f_vnode))
1880 * The root of a mounted filesystem cannot be deleted.
1882 * XXX: can this only be a VDIR case?
1884 if (vp->v_vflag & VV_ROOT)
1888 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1889 NDFREE(&nd, NDF_ONLY_PNBUF);
1891 if (vp == nd.ni_dvp)
1895 if ((error = vn_start_write(NULL, &mp,
1896 V_XSLEEP | PCATCH)) != 0) {
1902 error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp,
1907 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_UNLINK);
1908 error = VOP_REMOVE(nd.ni_dvp, vp, &nd.ni_cnd);
1912 vn_finished_write(mp);
1914 NDFREE(&nd, NDF_ONLY_PNBUF);
1916 if (vp == nd.ni_dvp)
1927 * Reposition read/write file offset.
1929 #ifndef _SYS_SYSPROTO_H_
1938 sys_lseek(struct thread *td, struct lseek_args *uap)
1941 return (kern_lseek(td, uap->fd, uap->offset, uap->whence));
1945 kern_lseek(struct thread *td, int fd, off_t offset, int whence)
1951 error = fget(td, fd, &cap_seek_rights, &fp);
1954 error = (fp->f_ops->fo_flags & DFLAG_SEEKABLE) != 0 ?
1955 fo_seek(fp, offset, whence, td) : ESPIPE;
1960 #if defined(COMPAT_43)
1962 * Reposition read/write file offset.
1964 #ifndef _SYS_SYSPROTO_H_
1965 struct olseek_args {
1972 olseek(struct thread *td, struct olseek_args *uap)
1975 return (kern_lseek(td, uap->fd, uap->offset, uap->whence));
1977 #endif /* COMPAT_43 */
1979 #if defined(COMPAT_FREEBSD6)
1980 /* Version with the 'pad' argument */
1982 freebsd6_lseek(struct thread *td, struct freebsd6_lseek_args *uap)
1985 return (kern_lseek(td, uap->fd, uap->offset, uap->whence));
1990 * Check access permissions using passed credentials.
1993 vn_access(struct vnode *vp, int user_flags, struct ucred *cred,
1999 /* Flags == 0 means only check for existence. */
2000 if (user_flags == 0)
2004 if (user_flags & R_OK)
2006 if (user_flags & W_OK)
2008 if (user_flags & X_OK)
2011 error = mac_vnode_check_access(cred, vp, accmode);
2015 if ((accmode & VWRITE) == 0 || (error = vn_writechk(vp)) == 0)
2016 error = VOP_ACCESS(vp, accmode, cred, td);
2021 * Check access permissions using "real" credentials.
2023 #ifndef _SYS_SYSPROTO_H_
2024 struct access_args {
2030 sys_access(struct thread *td, struct access_args *uap)
2033 return (kern_accessat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2037 #ifndef _SYS_SYSPROTO_H_
2038 struct faccessat_args {
2046 sys_faccessat(struct thread *td, struct faccessat_args *uap)
2049 return (kern_accessat(td, uap->fd, uap->path, UIO_USERSPACE, uap->flag,
2054 kern_accessat(struct thread *td, int fd, const char *path,
2055 enum uio_seg pathseg, int flag, int amode)
2057 struct ucred *cred, *usecred;
2059 struct nameidata nd;
2062 if ((flag & ~(AT_EACCESS | AT_BENEATH)) != 0)
2064 if (amode != F_OK && (amode & ~(R_OK | W_OK | X_OK)) != 0)
2068 * Create and modify a temporary credential instead of one that
2069 * is potentially shared (if we need one).
2071 cred = td->td_ucred;
2072 if ((flag & AT_EACCESS) == 0 &&
2073 ((cred->cr_uid != cred->cr_ruid ||
2074 cred->cr_rgid != cred->cr_groups[0]))) {
2075 usecred = crdup(cred);
2076 usecred->cr_uid = cred->cr_ruid;
2077 usecred->cr_groups[0] = cred->cr_rgid;
2078 td->td_ucred = usecred;
2081 AUDIT_ARG_VALUE(amode);
2082 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF |
2083 AUDITVNODE1 | ((flag & AT_BENEATH) != 0 ? BENEATH : 0),
2084 pathseg, path, fd, &cap_fstat_rights, td);
2085 if ((error = namei(&nd)) != 0)
2089 error = vn_access(vp, amode, usecred, td);
2090 NDFREE(&nd, NDF_ONLY_PNBUF);
2093 if (usecred != cred) {
2094 td->td_ucred = cred;
2101 * Check access permissions using "effective" credentials.
2103 #ifndef _SYS_SYSPROTO_H_
2104 struct eaccess_args {
2110 sys_eaccess(struct thread *td, struct eaccess_args *uap)
2113 return (kern_accessat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2114 AT_EACCESS, uap->amode));
2117 #if defined(COMPAT_43)
2119 * Get file status; this version follows links.
2121 #ifndef _SYS_SYSPROTO_H_
2128 ostat(struct thread *td, struct ostat_args *uap)
2134 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
2139 return (copyout(&osb, uap->ub, sizeof (osb)));
2143 * Get file status; this version does not follow links.
2145 #ifndef _SYS_SYSPROTO_H_
2146 struct olstat_args {
2152 olstat(struct thread *td, struct olstat_args *uap)
2158 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2159 UIO_USERSPACE, &sb, NULL);
2163 return (copyout(&osb, uap->ub, sizeof (osb)));
2167 * Convert from an old to a new stat structure.
2168 * XXX: many values are blindly truncated.
2171 cvtstat(struct stat *st, struct ostat *ost)
2174 bzero(ost, sizeof(*ost));
2175 ost->st_dev = st->st_dev;
2176 ost->st_ino = st->st_ino;
2177 ost->st_mode = st->st_mode;
2178 ost->st_nlink = st->st_nlink;
2179 ost->st_uid = st->st_uid;
2180 ost->st_gid = st->st_gid;
2181 ost->st_rdev = st->st_rdev;
2182 ost->st_size = MIN(st->st_size, INT32_MAX);
2183 ost->st_atim = st->st_atim;
2184 ost->st_mtim = st->st_mtim;
2185 ost->st_ctim = st->st_ctim;
2186 ost->st_blksize = st->st_blksize;
2187 ost->st_blocks = st->st_blocks;
2188 ost->st_flags = st->st_flags;
2189 ost->st_gen = st->st_gen;
2191 #endif /* COMPAT_43 */
2193 #if defined(COMPAT_43) || defined(COMPAT_FREEBSD11)
2194 int ino64_trunc_error;
2195 SYSCTL_INT(_vfs, OID_AUTO, ino64_trunc_error, CTLFLAG_RW,
2196 &ino64_trunc_error, 0,
2197 "Error on truncation of device, file or inode number, or link count");
2200 freebsd11_cvtstat(struct stat *st, struct freebsd11_stat *ost)
2203 ost->st_dev = st->st_dev;
2204 if (ost->st_dev != st->st_dev) {
2205 switch (ino64_trunc_error) {
2208 * Since dev_t is almost raw, don't clamp to the
2209 * maximum for case 2, but ignore the error.
2216 ost->st_ino = st->st_ino;
2217 if (ost->st_ino != st->st_ino) {
2218 switch (ino64_trunc_error) {
2225 ost->st_ino = UINT32_MAX;
2229 ost->st_mode = st->st_mode;
2230 ost->st_nlink = st->st_nlink;
2231 if (ost->st_nlink != st->st_nlink) {
2232 switch (ino64_trunc_error) {
2239 ost->st_nlink = UINT16_MAX;
2243 ost->st_uid = st->st_uid;
2244 ost->st_gid = st->st_gid;
2245 ost->st_rdev = st->st_rdev;
2246 if (ost->st_rdev != st->st_rdev) {
2247 switch (ino64_trunc_error) {
2254 ost->st_atim = st->st_atim;
2255 ost->st_mtim = st->st_mtim;
2256 ost->st_ctim = st->st_ctim;
2257 ost->st_size = st->st_size;
2258 ost->st_blocks = st->st_blocks;
2259 ost->st_blksize = st->st_blksize;
2260 ost->st_flags = st->st_flags;
2261 ost->st_gen = st->st_gen;
2263 ost->st_birthtim = st->st_birthtim;
2264 bzero((char *)&ost->st_birthtim + sizeof(ost->st_birthtim),
2265 sizeof(*ost) - offsetof(struct freebsd11_stat,
2266 st_birthtim) - sizeof(ost->st_birthtim));
2271 freebsd11_stat(struct thread *td, struct freebsd11_stat_args* uap)
2274 struct freebsd11_stat osb;
2277 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
2281 error = freebsd11_cvtstat(&sb, &osb);
2283 error = copyout(&osb, uap->ub, sizeof(osb));
2288 freebsd11_lstat(struct thread *td, struct freebsd11_lstat_args* uap)
2291 struct freebsd11_stat osb;
2294 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2295 UIO_USERSPACE, &sb, NULL);
2298 error = freebsd11_cvtstat(&sb, &osb);
2300 error = copyout(&osb, uap->ub, sizeof(osb));
2305 freebsd11_fhstat(struct thread *td, struct freebsd11_fhstat_args* uap)
2309 struct freebsd11_stat osb;
2312 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
2315 error = kern_fhstat(td, fh, &sb);
2318 error = freebsd11_cvtstat(&sb, &osb);
2320 error = copyout(&osb, uap->sb, sizeof(osb));
2325 freebsd11_fstatat(struct thread *td, struct freebsd11_fstatat_args* uap)
2328 struct freebsd11_stat osb;
2331 error = kern_statat(td, uap->flag, uap->fd, uap->path,
2332 UIO_USERSPACE, &sb, NULL);
2335 error = freebsd11_cvtstat(&sb, &osb);
2337 error = copyout(&osb, uap->buf, sizeof(osb));
2340 #endif /* COMPAT_FREEBSD11 */
2345 #ifndef _SYS_SYSPROTO_H_
2346 struct fstatat_args {
2354 sys_fstatat(struct thread *td, struct fstatat_args *uap)
2359 error = kern_statat(td, uap->flag, uap->fd, uap->path,
2360 UIO_USERSPACE, &sb, NULL);
2362 error = copyout(&sb, uap->buf, sizeof (sb));
2367 kern_statat(struct thread *td, int flag, int fd, const char *path,
2368 enum uio_seg pathseg, struct stat *sbp,
2369 void (*hook)(struct vnode *vp, struct stat *sbp))
2371 struct nameidata nd;
2374 if ((flag & ~(AT_SYMLINK_NOFOLLOW | AT_BENEATH)) != 0)
2377 NDINIT_ATRIGHTS(&nd, LOOKUP, ((flag & AT_SYMLINK_NOFOLLOW) != 0 ?
2378 NOFOLLOW : FOLLOW) | ((flag & AT_BENEATH) != 0 ? BENEATH : 0) |
2379 LOCKSHARED | LOCKLEAF | AUDITVNODE1, pathseg, path, fd,
2380 &cap_fstat_rights, td);
2382 if ((error = namei(&nd)) != 0)
2384 error = vn_stat(nd.ni_vp, sbp, td->td_ucred, NOCRED, td);
2386 SDT_PROBE2(vfs, , stat, mode, path, sbp->st_mode);
2387 if (S_ISREG(sbp->st_mode))
2388 SDT_PROBE2(vfs, , stat, reg, path, pathseg);
2389 if (__predict_false(hook != NULL))
2390 hook(nd.ni_vp, sbp);
2392 NDFREE(&nd, NDF_ONLY_PNBUF);
2394 #ifdef __STAT_TIME_T_EXT
2395 sbp->st_atim_ext = 0;
2396 sbp->st_mtim_ext = 0;
2397 sbp->st_ctim_ext = 0;
2398 sbp->st_btim_ext = 0;
2401 if (KTRPOINT(td, KTR_STRUCT))
2402 ktrstat_error(sbp, error);
2407 #if defined(COMPAT_FREEBSD11)
2409 * Implementation of the NetBSD [l]stat() functions.
2412 freebsd11_cvtnstat(struct stat *sb, struct nstat *nsb)
2415 bzero(nsb, sizeof(*nsb));
2416 nsb->st_dev = sb->st_dev;
2417 nsb->st_ino = sb->st_ino;
2418 nsb->st_mode = sb->st_mode;
2419 nsb->st_nlink = sb->st_nlink;
2420 nsb->st_uid = sb->st_uid;
2421 nsb->st_gid = sb->st_gid;
2422 nsb->st_rdev = sb->st_rdev;
2423 nsb->st_atim = sb->st_atim;
2424 nsb->st_mtim = sb->st_mtim;
2425 nsb->st_ctim = sb->st_ctim;
2426 nsb->st_size = sb->st_size;
2427 nsb->st_blocks = sb->st_blocks;
2428 nsb->st_blksize = sb->st_blksize;
2429 nsb->st_flags = sb->st_flags;
2430 nsb->st_gen = sb->st_gen;
2431 nsb->st_birthtim = sb->st_birthtim;
2434 #ifndef _SYS_SYSPROTO_H_
2435 struct freebsd11_nstat_args {
2441 freebsd11_nstat(struct thread *td, struct freebsd11_nstat_args *uap)
2447 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
2451 freebsd11_cvtnstat(&sb, &nsb);
2452 return (copyout(&nsb, uap->ub, sizeof (nsb)));
2456 * NetBSD lstat. Get file status; this version does not follow links.
2458 #ifndef _SYS_SYSPROTO_H_
2459 struct freebsd11_nlstat_args {
2465 freebsd11_nlstat(struct thread *td, struct freebsd11_nlstat_args *uap)
2471 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2472 UIO_USERSPACE, &sb, NULL);
2475 freebsd11_cvtnstat(&sb, &nsb);
2476 return (copyout(&nsb, uap->ub, sizeof (nsb)));
2478 #endif /* COMPAT_FREEBSD11 */
2481 * Get configurable pathname variables.
2483 #ifndef _SYS_SYSPROTO_H_
2484 struct pathconf_args {
2490 sys_pathconf(struct thread *td, struct pathconf_args *uap)
2495 error = kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name, FOLLOW,
2498 td->td_retval[0] = value;
2502 #ifndef _SYS_SYSPROTO_H_
2503 struct lpathconf_args {
2509 sys_lpathconf(struct thread *td, struct lpathconf_args *uap)
2514 error = kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name,
2517 td->td_retval[0] = value;
2522 kern_pathconf(struct thread *td, const char *path, enum uio_seg pathseg,
2523 int name, u_long flags, long *valuep)
2525 struct nameidata nd;
2528 NDINIT(&nd, LOOKUP, LOCKSHARED | LOCKLEAF | AUDITVNODE1 | flags,
2530 if ((error = namei(&nd)) != 0)
2532 NDFREE(&nd, NDF_ONLY_PNBUF);
2534 error = VOP_PATHCONF(nd.ni_vp, name, valuep);
2540 * Return target name of a symbolic link.
2542 #ifndef _SYS_SYSPROTO_H_
2543 struct readlink_args {
2550 sys_readlink(struct thread *td, struct readlink_args *uap)
2553 return (kern_readlinkat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2554 uap->buf, UIO_USERSPACE, uap->count));
2556 #ifndef _SYS_SYSPROTO_H_
2557 struct readlinkat_args {
2565 sys_readlinkat(struct thread *td, struct readlinkat_args *uap)
2568 return (kern_readlinkat(td, uap->fd, uap->path, UIO_USERSPACE,
2569 uap->buf, UIO_USERSPACE, uap->bufsize));
2573 kern_readlinkat(struct thread *td, int fd, const char *path,
2574 enum uio_seg pathseg, char *buf, enum uio_seg bufseg, size_t count)
2577 struct nameidata nd;
2580 if (count > IOSIZE_MAX)
2583 NDINIT_AT(&nd, LOOKUP, NOFOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
2584 pathseg, path, fd, td);
2586 if ((error = namei(&nd)) != 0)
2588 NDFREE(&nd, NDF_ONLY_PNBUF);
2591 error = kern_readlink_vp(vp, buf, bufseg, count, td);
2598 * Helper function to readlink from a vnode
2601 kern_readlink_vp(struct vnode *vp, char *buf, enum uio_seg bufseg, size_t count,
2608 ASSERT_VOP_LOCKED(vp, "kern_readlink_vp(): vp not locked");
2610 error = mac_vnode_check_readlink(td->td_ucred, vp);
2614 if (vp->v_type != VLNK && (vp->v_vflag & VV_READLINK) == 0)
2617 aiov.iov_base = buf;
2618 aiov.iov_len = count;
2619 auio.uio_iov = &aiov;
2620 auio.uio_iovcnt = 1;
2621 auio.uio_offset = 0;
2622 auio.uio_rw = UIO_READ;
2623 auio.uio_segflg = bufseg;
2625 auio.uio_resid = count;
2626 error = VOP_READLINK(vp, &auio, td->td_ucred);
2627 td->td_retval[0] = count - auio.uio_resid;
2632 * Common implementation code for chflags() and fchflags().
2635 setfflags(struct thread *td, struct vnode *vp, u_long flags)
2641 /* We can't support the value matching VNOVAL. */
2642 if (flags == VNOVAL)
2643 return (EOPNOTSUPP);
2646 * Prevent non-root users from setting flags on devices. When
2647 * a device is reused, users can retain ownership of the device
2648 * if they are allowed to set flags and programs assume that
2649 * chown can't fail when done as root.
2651 if (vp->v_type == VCHR || vp->v_type == VBLK) {
2652 error = priv_check(td, PRIV_VFS_CHFLAGS_DEV);
2657 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2660 vattr.va_flags = flags;
2661 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2663 error = mac_vnode_check_setflags(td->td_ucred, vp, vattr.va_flags);
2666 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
2668 vn_finished_write(mp);
2673 * Change flags of a file given a path name.
2675 #ifndef _SYS_SYSPROTO_H_
2676 struct chflags_args {
2682 sys_chflags(struct thread *td, struct chflags_args *uap)
2685 return (kern_chflagsat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2689 #ifndef _SYS_SYSPROTO_H_
2690 struct chflagsat_args {
2698 sys_chflagsat(struct thread *td, struct chflagsat_args *uap)
2701 if ((uap->atflag & ~(AT_SYMLINK_NOFOLLOW | AT_BENEATH)) != 0)
2704 return (kern_chflagsat(td, uap->fd, uap->path, UIO_USERSPACE,
2705 uap->flags, uap->atflag));
2709 * Same as chflags() but doesn't follow symlinks.
2711 #ifndef _SYS_SYSPROTO_H_
2712 struct lchflags_args {
2718 sys_lchflags(struct thread *td, struct lchflags_args *uap)
2721 return (kern_chflagsat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2722 uap->flags, AT_SYMLINK_NOFOLLOW));
2726 kern_chflagsat(struct thread *td, int fd, const char *path,
2727 enum uio_seg pathseg, u_long flags, int atflag)
2729 struct nameidata nd;
2732 AUDIT_ARG_FFLAGS(flags);
2733 follow = (atflag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
2734 follow |= (atflag & AT_BENEATH) != 0 ? BENEATH : 0;
2735 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, pathseg, path, fd,
2736 &cap_fchflags_rights, td);
2737 if ((error = namei(&nd)) != 0)
2739 NDFREE(&nd, NDF_ONLY_PNBUF);
2740 error = setfflags(td, nd.ni_vp, flags);
2746 * Change flags of a file given a file descriptor.
2748 #ifndef _SYS_SYSPROTO_H_
2749 struct fchflags_args {
2755 sys_fchflags(struct thread *td, struct fchflags_args *uap)
2760 AUDIT_ARG_FD(uap->fd);
2761 AUDIT_ARG_FFLAGS(uap->flags);
2762 error = getvnode(td, uap->fd, &cap_fchflags_rights,
2767 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
2768 AUDIT_ARG_VNODE1(fp->f_vnode);
2769 VOP_UNLOCK(fp->f_vnode);
2771 error = setfflags(td, fp->f_vnode, uap->flags);
2777 * Common implementation code for chmod(), lchmod() and fchmod().
2780 setfmode(struct thread *td, struct ucred *cred, struct vnode *vp, int mode)
2786 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2788 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2790 vattr.va_mode = mode & ALLPERMS;
2792 error = mac_vnode_check_setmode(cred, vp, vattr.va_mode);
2795 error = VOP_SETATTR(vp, &vattr, cred);
2797 vn_finished_write(mp);
2802 * Change mode of a file given path name.
2804 #ifndef _SYS_SYSPROTO_H_
2811 sys_chmod(struct thread *td, struct chmod_args *uap)
2814 return (kern_fchmodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2818 #ifndef _SYS_SYSPROTO_H_
2819 struct fchmodat_args {
2827 sys_fchmodat(struct thread *td, struct fchmodat_args *uap)
2830 if ((uap->flag & ~(AT_SYMLINK_NOFOLLOW | AT_BENEATH)) != 0)
2833 return (kern_fchmodat(td, uap->fd, uap->path, UIO_USERSPACE,
2834 uap->mode, uap->flag));
2838 * Change mode of a file given path name (don't follow links.)
2840 #ifndef _SYS_SYSPROTO_H_
2841 struct lchmod_args {
2847 sys_lchmod(struct thread *td, struct lchmod_args *uap)
2850 return (kern_fchmodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2851 uap->mode, AT_SYMLINK_NOFOLLOW));
2855 kern_fchmodat(struct thread *td, int fd, const char *path,
2856 enum uio_seg pathseg, mode_t mode, int flag)
2858 struct nameidata nd;
2861 AUDIT_ARG_MODE(mode);
2862 follow = (flag & AT_SYMLINK_NOFOLLOW) != 0 ? NOFOLLOW : FOLLOW;
2863 follow |= (flag & AT_BENEATH) != 0 ? BENEATH : 0;
2864 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, pathseg, path, fd,
2865 &cap_fchmod_rights, td);
2866 if ((error = namei(&nd)) != 0)
2868 NDFREE(&nd, NDF_ONLY_PNBUF);
2869 error = setfmode(td, td->td_ucred, nd.ni_vp, mode);
2875 * Change mode of a file given a file descriptor.
2877 #ifndef _SYS_SYSPROTO_H_
2878 struct fchmod_args {
2884 sys_fchmod(struct thread *td, struct fchmod_args *uap)
2889 AUDIT_ARG_FD(uap->fd);
2890 AUDIT_ARG_MODE(uap->mode);
2892 error = fget(td, uap->fd, &cap_fchmod_rights, &fp);
2895 error = fo_chmod(fp, uap->mode, td->td_ucred, td);
2901 * Common implementation for chown(), lchown(), and fchown()
2904 setfown(struct thread *td, struct ucred *cred, struct vnode *vp, uid_t uid,
2911 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2913 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2918 error = mac_vnode_check_setowner(cred, vp, vattr.va_uid,
2922 error = VOP_SETATTR(vp, &vattr, cred);
2924 vn_finished_write(mp);
2929 * Set ownership given a path name.
2931 #ifndef _SYS_SYSPROTO_H_
2939 sys_chown(struct thread *td, struct chown_args *uap)
2942 return (kern_fchownat(td, AT_FDCWD, uap->path, UIO_USERSPACE, uap->uid,
2946 #ifndef _SYS_SYSPROTO_H_
2947 struct fchownat_args {
2956 sys_fchownat(struct thread *td, struct fchownat_args *uap)
2959 if ((uap->flag & ~(AT_SYMLINK_NOFOLLOW | AT_BENEATH)) != 0)
2962 return (kern_fchownat(td, uap->fd, uap->path, UIO_USERSPACE, uap->uid,
2963 uap->gid, uap->flag));
2967 kern_fchownat(struct thread *td, int fd, const char *path,
2968 enum uio_seg pathseg, int uid, int gid, int flag)
2970 struct nameidata nd;
2973 AUDIT_ARG_OWNER(uid, gid);
2974 follow = (flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
2975 follow |= (flag & AT_BENEATH) != 0 ? BENEATH : 0;
2976 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, pathseg, path, fd,
2977 &cap_fchown_rights, td);
2979 if ((error = namei(&nd)) != 0)
2981 NDFREE(&nd, NDF_ONLY_PNBUF);
2982 error = setfown(td, td->td_ucred, nd.ni_vp, uid, gid);
2988 * Set ownership given a path name, do not cross symlinks.
2990 #ifndef _SYS_SYSPROTO_H_
2991 struct lchown_args {
2998 sys_lchown(struct thread *td, struct lchown_args *uap)
3001 return (kern_fchownat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
3002 uap->uid, uap->gid, AT_SYMLINK_NOFOLLOW));
3006 * Set ownership given a file descriptor.
3008 #ifndef _SYS_SYSPROTO_H_
3009 struct fchown_args {
3016 sys_fchown(struct thread *td, struct fchown_args *uap)
3021 AUDIT_ARG_FD(uap->fd);
3022 AUDIT_ARG_OWNER(uap->uid, uap->gid);
3023 error = fget(td, uap->fd, &cap_fchown_rights, &fp);
3026 error = fo_chown(fp, uap->uid, uap->gid, td->td_ucred, td);
3032 * Common implementation code for utimes(), lutimes(), and futimes().
3035 getutimes(const struct timeval *usrtvp, enum uio_seg tvpseg,
3036 struct timespec *tsp)
3038 struct timeval tv[2];
3039 const struct timeval *tvp;
3042 if (usrtvp == NULL) {
3043 vfs_timestamp(&tsp[0]);
3046 if (tvpseg == UIO_SYSSPACE) {
3049 if ((error = copyin(usrtvp, tv, sizeof(tv))) != 0)
3054 if (tvp[0].tv_usec < 0 || tvp[0].tv_usec >= 1000000 ||
3055 tvp[1].tv_usec < 0 || tvp[1].tv_usec >= 1000000)
3057 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
3058 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
3064 * Common implementation code for futimens(), utimensat().
3066 #define UTIMENS_NULL 0x1
3067 #define UTIMENS_EXIT 0x2
3069 getutimens(const struct timespec *usrtsp, enum uio_seg tspseg,
3070 struct timespec *tsp, int *retflags)
3072 struct timespec tsnow;
3075 vfs_timestamp(&tsnow);
3077 if (usrtsp == NULL) {
3080 *retflags |= UTIMENS_NULL;
3083 if (tspseg == UIO_SYSSPACE) {
3086 } else if ((error = copyin(usrtsp, tsp, sizeof(*tsp) * 2)) != 0)
3088 if (tsp[0].tv_nsec == UTIME_OMIT && tsp[1].tv_nsec == UTIME_OMIT)
3089 *retflags |= UTIMENS_EXIT;
3090 if (tsp[0].tv_nsec == UTIME_NOW && tsp[1].tv_nsec == UTIME_NOW)
3091 *retflags |= UTIMENS_NULL;
3092 if (tsp[0].tv_nsec == UTIME_OMIT)
3093 tsp[0].tv_sec = VNOVAL;
3094 else if (tsp[0].tv_nsec == UTIME_NOW)
3096 else if (tsp[0].tv_nsec < 0 || tsp[0].tv_nsec >= 1000000000L)
3098 if (tsp[1].tv_nsec == UTIME_OMIT)
3099 tsp[1].tv_sec = VNOVAL;
3100 else if (tsp[1].tv_nsec == UTIME_NOW)
3102 else if (tsp[1].tv_nsec < 0 || tsp[1].tv_nsec >= 1000000000L)
3109 * Common implementation code for utimes(), lutimes(), futimes(), futimens(),
3113 setutimes(struct thread *td, struct vnode *vp, const struct timespec *ts,
3114 int numtimes, int nullflag)
3118 int error, setbirthtime;
3120 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
3122 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3124 if (numtimes < 3 && !VOP_GETATTR(vp, &vattr, td->td_ucred) &&
3125 timespeccmp(&ts[1], &vattr.va_birthtime, < ))
3128 vattr.va_atime = ts[0];
3129 vattr.va_mtime = ts[1];
3131 vattr.va_birthtime = ts[1];
3133 vattr.va_birthtime = ts[2];
3135 vattr.va_vaflags |= VA_UTIMES_NULL;
3137 error = mac_vnode_check_setutimes(td->td_ucred, vp, vattr.va_atime,
3141 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3143 vn_finished_write(mp);
3148 * Set the access and modification times of a file.
3150 #ifndef _SYS_SYSPROTO_H_
3151 struct utimes_args {
3153 struct timeval *tptr;
3157 sys_utimes(struct thread *td, struct utimes_args *uap)
3160 return (kern_utimesat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
3161 uap->tptr, UIO_USERSPACE));
3164 #ifndef _SYS_SYSPROTO_H_
3165 struct futimesat_args {
3168 const struct timeval * times;
3172 sys_futimesat(struct thread *td, struct futimesat_args *uap)
3175 return (kern_utimesat(td, uap->fd, uap->path, UIO_USERSPACE,
3176 uap->times, UIO_USERSPACE));
3180 kern_utimesat(struct thread *td, int fd, const char *path,
3181 enum uio_seg pathseg, struct timeval *tptr, enum uio_seg tptrseg)
3183 struct nameidata nd;
3184 struct timespec ts[2];
3187 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3189 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, fd,
3190 &cap_futimes_rights, td);
3192 if ((error = namei(&nd)) != 0)
3194 NDFREE(&nd, NDF_ONLY_PNBUF);
3195 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3201 * Set the access and modification times of a file.
3203 #ifndef _SYS_SYSPROTO_H_
3204 struct lutimes_args {
3206 struct timeval *tptr;
3210 sys_lutimes(struct thread *td, struct lutimes_args *uap)
3213 return (kern_lutimes(td, uap->path, UIO_USERSPACE, uap->tptr,
3218 kern_lutimes(struct thread *td, const char *path, enum uio_seg pathseg,
3219 struct timeval *tptr, enum uio_seg tptrseg)
3221 struct timespec ts[2];
3222 struct nameidata nd;
3225 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3227 NDINIT(&nd, LOOKUP, NOFOLLOW | AUDITVNODE1, pathseg, path, td);
3228 if ((error = namei(&nd)) != 0)
3230 NDFREE(&nd, NDF_ONLY_PNBUF);
3231 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3237 * Set the access and modification times of a file.
3239 #ifndef _SYS_SYSPROTO_H_
3240 struct futimes_args {
3242 struct timeval *tptr;
3246 sys_futimes(struct thread *td, struct futimes_args *uap)
3249 return (kern_futimes(td, uap->fd, uap->tptr, UIO_USERSPACE));
3253 kern_futimes(struct thread *td, int fd, struct timeval *tptr,
3254 enum uio_seg tptrseg)
3256 struct timespec ts[2];
3261 error = getutimes(tptr, tptrseg, ts);
3264 error = getvnode(td, fd, &cap_futimes_rights, &fp);
3268 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
3269 AUDIT_ARG_VNODE1(fp->f_vnode);
3270 VOP_UNLOCK(fp->f_vnode);
3272 error = setutimes(td, fp->f_vnode, ts, 2, tptr == NULL);
3278 sys_futimens(struct thread *td, struct futimens_args *uap)
3281 return (kern_futimens(td, uap->fd, uap->times, UIO_USERSPACE));
3285 kern_futimens(struct thread *td, int fd, struct timespec *tptr,
3286 enum uio_seg tptrseg)
3288 struct timespec ts[2];
3293 error = getutimens(tptr, tptrseg, ts, &flags);
3296 if (flags & UTIMENS_EXIT)
3298 error = getvnode(td, fd, &cap_futimes_rights, &fp);
3302 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
3303 AUDIT_ARG_VNODE1(fp->f_vnode);
3304 VOP_UNLOCK(fp->f_vnode);
3306 error = setutimes(td, fp->f_vnode, ts, 2, flags & UTIMENS_NULL);
3312 sys_utimensat(struct thread *td, struct utimensat_args *uap)
3315 return (kern_utimensat(td, uap->fd, uap->path, UIO_USERSPACE,
3316 uap->times, UIO_USERSPACE, uap->flag));
3320 kern_utimensat(struct thread *td, int fd, const char *path,
3321 enum uio_seg pathseg, struct timespec *tptr, enum uio_seg tptrseg,
3324 struct nameidata nd;
3325 struct timespec ts[2];
3328 if ((flag & ~(AT_SYMLINK_NOFOLLOW | AT_BENEATH)) != 0)
3331 if ((error = getutimens(tptr, tptrseg, ts, &flags)) != 0)
3333 NDINIT_ATRIGHTS(&nd, LOOKUP, ((flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW :
3334 FOLLOW) | ((flag & AT_BENEATH) != 0 ? BENEATH : 0) | AUDITVNODE1,
3335 pathseg, path, fd, &cap_futimes_rights, td);
3336 if ((error = namei(&nd)) != 0)
3339 * We are allowed to call namei() regardless of 2xUTIME_OMIT.
3341 * "If both tv_nsec fields are UTIME_OMIT... EACCESS may be detected."
3342 * "Search permission is denied by a component of the path prefix."
3344 NDFREE(&nd, NDF_ONLY_PNBUF);
3345 if ((flags & UTIMENS_EXIT) == 0)
3346 error = setutimes(td, nd.ni_vp, ts, 2, flags & UTIMENS_NULL);
3352 * Truncate a file given its path name.
3354 #ifndef _SYS_SYSPROTO_H_
3355 struct truncate_args {
3362 sys_truncate(struct thread *td, struct truncate_args *uap)
3365 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3369 kern_truncate(struct thread *td, const char *path, enum uio_seg pathseg,
3376 struct nameidata nd;
3381 NDINIT(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, td);
3382 if ((error = namei(&nd)) != 0)
3385 rl_cookie = vn_rangelock_wlock(vp, 0, OFF_MAX);
3386 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0) {
3387 vn_rangelock_unlock(vp, rl_cookie);
3391 NDFREE(&nd, NDF_ONLY_PNBUF);
3392 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3393 if (vp->v_type == VDIR)
3396 else if ((error = mac_vnode_check_write(td->td_ucred, NOCRED, vp))) {
3399 else if ((error = vn_writechk(vp)) == 0 &&
3400 (error = VOP_ACCESS(vp, VWRITE, td->td_ucred, td)) == 0) {
3402 vattr.va_size = length;
3403 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3406 vn_finished_write(mp);
3407 vn_rangelock_unlock(vp, rl_cookie);
3412 #if defined(COMPAT_43)
3414 * Truncate a file given its path name.
3416 #ifndef _SYS_SYSPROTO_H_
3417 struct otruncate_args {
3423 otruncate(struct thread *td, struct otruncate_args *uap)
3426 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3428 #endif /* COMPAT_43 */
3430 #if defined(COMPAT_FREEBSD6)
3431 /* Versions with the pad argument */
3433 freebsd6_truncate(struct thread *td, struct freebsd6_truncate_args *uap)
3436 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3440 freebsd6_ftruncate(struct thread *td, struct freebsd6_ftruncate_args *uap)
3443 return (kern_ftruncate(td, uap->fd, uap->length));
3448 kern_fsync(struct thread *td, int fd, bool fullsync)
3453 int error, lock_flags;
3456 error = getvnode(td, fd, &cap_fsync_rights, &fp);
3462 /* XXXKIB: compete outstanding aio writes */;
3464 error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
3467 if (MNT_SHARED_WRITES(mp) ||
3468 ((mp == NULL) && MNT_SHARED_WRITES(vp->v_mount))) {
3469 lock_flags = LK_SHARED;
3471 lock_flags = LK_EXCLUSIVE;
3473 vn_lock(vp, lock_flags | LK_RETRY);
3474 AUDIT_ARG_VNODE1(vp);
3475 if (vp->v_object != NULL) {
3476 VM_OBJECT_WLOCK(vp->v_object);
3477 vm_object_page_clean(vp->v_object, 0, 0, 0);
3478 VM_OBJECT_WUNLOCK(vp->v_object);
3480 error = fullsync ? VOP_FSYNC(vp, MNT_WAIT, td) : VOP_FDATASYNC(vp, td);
3482 vn_finished_write(mp);
3489 * Sync an open file.
3491 #ifndef _SYS_SYSPROTO_H_
3497 sys_fsync(struct thread *td, struct fsync_args *uap)
3500 return (kern_fsync(td, uap->fd, true));
3504 sys_fdatasync(struct thread *td, struct fdatasync_args *uap)
3507 return (kern_fsync(td, uap->fd, false));
3511 * Rename files. Source and destination must either both be directories, or
3512 * both not be directories. If target is a directory, it must be empty.
3514 #ifndef _SYS_SYSPROTO_H_
3515 struct rename_args {
3521 sys_rename(struct thread *td, struct rename_args *uap)
3524 return (kern_renameat(td, AT_FDCWD, uap->from, AT_FDCWD,
3525 uap->to, UIO_USERSPACE));
3528 #ifndef _SYS_SYSPROTO_H_
3529 struct renameat_args {
3537 sys_renameat(struct thread *td, struct renameat_args *uap)
3540 return (kern_renameat(td, uap->oldfd, uap->old, uap->newfd, uap->new,
3546 kern_renameat_mac(struct thread *td, int oldfd, const char *old, int newfd,
3547 const char *new, enum uio_seg pathseg, struct nameidata *fromnd)
3551 NDINIT_ATRIGHTS(fromnd, DELETE, LOCKPARENT | LOCKLEAF | SAVESTART |
3552 AUDITVNODE1, pathseg, old, oldfd, &cap_renameat_source_rights, td);
3553 if ((error = namei(fromnd)) != 0)
3555 error = mac_vnode_check_rename_from(td->td_ucred, fromnd->ni_dvp,
3556 fromnd->ni_vp, &fromnd->ni_cnd);
3557 VOP_UNLOCK(fromnd->ni_dvp);
3558 if (fromnd->ni_dvp != fromnd->ni_vp)
3559 VOP_UNLOCK(fromnd->ni_vp);
3561 NDFREE(fromnd, NDF_ONLY_PNBUF);
3562 vrele(fromnd->ni_dvp);
3563 vrele(fromnd->ni_vp);
3564 if (fromnd->ni_startdir)
3565 vrele(fromnd->ni_startdir);
3572 kern_renameat(struct thread *td, int oldfd, const char *old, int newfd,
3573 const char *new, enum uio_seg pathseg)
3575 struct mount *mp = NULL;
3576 struct vnode *tvp, *fvp, *tdvp;
3577 struct nameidata fromnd, tond;
3583 if (mac_vnode_check_rename_from_enabled()) {
3584 error = kern_renameat_mac(td, oldfd, old, newfd, new, pathseg,
3590 NDINIT_ATRIGHTS(&fromnd, DELETE, WANTPARENT | SAVESTART | AUDITVNODE1,
3591 pathseg, old, oldfd, &cap_renameat_source_rights, td);
3592 if ((error = namei(&fromnd)) != 0)
3598 NDINIT_ATRIGHTS(&tond, RENAME, LOCKPARENT | LOCKLEAF | NOCACHE |
3599 SAVESTART | AUDITVNODE2, pathseg, new, newfd,
3600 &cap_renameat_target_rights, td);
3601 if (fromnd.ni_vp->v_type == VDIR)
3602 tond.ni_cnd.cn_flags |= WILLBEDIR;
3603 if ((error = namei(&tond)) != 0) {
3604 /* Translate error code for rename("dir1", "dir2/."). */
3605 if (error == EISDIR && fvp->v_type == VDIR)
3607 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3608 vrele(fromnd.ni_dvp);
3614 error = vn_start_write(fvp, &mp, V_NOWAIT);
3616 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3617 NDFREE(&tond, NDF_ONLY_PNBUF);
3624 vrele(fromnd.ni_dvp);
3626 vrele(tond.ni_startdir);
3627 if (fromnd.ni_startdir != NULL)
3628 vrele(fromnd.ni_startdir);
3629 error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH);
3635 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
3638 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
3643 if (newfd != AT_FDCWD && (tond.ni_resflags & NIRES_ABS) == 0) {
3645 * If the target already exists we require CAP_UNLINKAT
3646 * from 'newfd', when newfd was used for the lookup.
3648 error = cap_check(&tond.ni_filecaps.fc_rights,
3649 &cap_unlinkat_rights);
3660 * If the source is the same as the destination (that is, if they
3661 * are links to the same vnode), then there is nothing to do.
3667 error = mac_vnode_check_rename_to(td->td_ucred, tdvp,
3668 tond.ni_vp, fromnd.ni_dvp == tdvp, &tond.ni_cnd);
3672 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
3673 tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
3674 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3675 NDFREE(&tond, NDF_ONLY_PNBUF);
3677 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3678 NDFREE(&tond, NDF_ONLY_PNBUF);
3685 vrele(fromnd.ni_dvp);
3688 vrele(tond.ni_startdir);
3689 vn_finished_write(mp);
3691 if (fromnd.ni_startdir)
3692 vrele(fromnd.ni_startdir);
3699 * Make a directory file.
3701 #ifndef _SYS_SYSPROTO_H_
3708 sys_mkdir(struct thread *td, struct mkdir_args *uap)
3711 return (kern_mkdirat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
3715 #ifndef _SYS_SYSPROTO_H_
3716 struct mkdirat_args {
3723 sys_mkdirat(struct thread *td, struct mkdirat_args *uap)
3726 return (kern_mkdirat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode));
3730 kern_mkdirat(struct thread *td, int fd, const char *path, enum uio_seg segflg,
3736 struct nameidata nd;
3739 AUDIT_ARG_MODE(mode);
3742 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
3743 NOCACHE, segflg, path, fd, &cap_mkdirat_rights,
3745 nd.ni_cnd.cn_flags |= WILLBEDIR;
3746 if ((error = namei(&nd)) != 0)
3750 NDFREE(&nd, NDF_ONLY_PNBUF);
3752 * XXX namei called with LOCKPARENT but not LOCKLEAF has
3753 * the strange behaviour of leaving the vnode unlocked
3754 * if the target is the same vnode as the parent.
3756 if (vp == nd.ni_dvp)
3763 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3764 NDFREE(&nd, NDF_ONLY_PNBUF);
3766 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3771 vattr.va_type = VDIR;
3772 vattr.va_mode = (mode & ACCESSPERMS) &~ td->td_proc->p_fd->fd_cmask;
3774 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
3779 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
3783 NDFREE(&nd, NDF_ONLY_PNBUF);
3787 vn_finished_write(mp);
3792 * Remove a directory file.
3794 #ifndef _SYS_SYSPROTO_H_
3800 sys_rmdir(struct thread *td, struct rmdir_args *uap)
3803 return (kern_frmdirat(td, AT_FDCWD, uap->path, FD_NONE, UIO_USERSPACE,
3808 kern_frmdirat(struct thread *td, int dfd, const char *path, int fd,
3809 enum uio_seg pathseg, int flag)
3814 struct nameidata nd;
3815 cap_rights_t rights;
3819 if (fd != FD_NONE) {
3820 error = getvnode(td, fd, cap_rights_init_one(&rights, CAP_LOOKUP),
3828 NDINIT_ATRIGHTS(&nd, DELETE, LOCKPARENT | LOCKLEAF | AUDITVNODE1 |
3829 ((flag & AT_BENEATH) != 0 ? BENEATH : 0),
3830 pathseg, path, dfd, &cap_unlinkat_rights, td);
3831 if ((error = namei(&nd)) != 0)
3834 if (vp->v_type != VDIR) {
3839 * No rmdir "." please.
3841 if (nd.ni_dvp == vp) {
3846 * The root of a mounted filesystem cannot be deleted.
3848 if (vp->v_vflag & VV_ROOT) {
3853 if (fp != NULL && fp->f_vnode != vp) {
3854 if (VN_IS_DOOMED(fp->f_vnode))
3862 error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp,
3867 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3868 NDFREE(&nd, NDF_ONLY_PNBUF);
3870 if (nd.ni_dvp == vp)
3874 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3878 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_UNLINK);
3879 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
3880 vn_finished_write(mp);
3882 NDFREE(&nd, NDF_ONLY_PNBUF);
3884 if (nd.ni_dvp == vp)
3894 #if defined(COMPAT_43) || defined(COMPAT_FREEBSD11)
3896 freebsd11_kern_getdirentries(struct thread *td, int fd, char *ubuf, u_int count,
3897 long *basep, void (*func)(struct freebsd11_dirent *))
3899 struct freebsd11_dirent dstdp;
3900 struct dirent *dp, *edp;
3903 ssize_t resid, ucount;
3906 /* XXX arbitrary sanity limit on `count'. */
3907 count = min(count, 64 * 1024);
3909 dirbuf = malloc(count, M_TEMP, M_WAITOK);
3911 error = kern_getdirentries(td, fd, dirbuf, count, &base, &resid,
3919 for (dp = (struct dirent *)dirbuf,
3920 edp = (struct dirent *)&dirbuf[count - resid];
3921 ucount < count && dp < edp; ) {
3922 if (dp->d_reclen == 0)
3924 MPASS(dp->d_reclen >= _GENERIC_DIRLEN(0));
3925 if (dp->d_namlen >= sizeof(dstdp.d_name))
3927 dstdp.d_type = dp->d_type;
3928 dstdp.d_namlen = dp->d_namlen;
3929 dstdp.d_fileno = dp->d_fileno; /* truncate */
3930 if (dstdp.d_fileno != dp->d_fileno) {
3931 switch (ino64_trunc_error) {
3939 dstdp.d_fileno = UINT32_MAX;
3943 dstdp.d_reclen = sizeof(dstdp) - sizeof(dstdp.d_name) +
3944 ((dp->d_namlen + 1 + 3) &~ 3);
3945 bcopy(dp->d_name, dstdp.d_name, dstdp.d_namlen);
3946 bzero(dstdp.d_name + dstdp.d_namlen,
3947 dstdp.d_reclen - offsetof(struct freebsd11_dirent, d_name) -
3949 MPASS(dstdp.d_reclen <= dp->d_reclen);
3950 MPASS(ucount + dstdp.d_reclen <= count);
3953 error = copyout(&dstdp, ubuf + ucount, dstdp.d_reclen);
3956 dp = (struct dirent *)((char *)dp + dp->d_reclen);
3957 ucount += dstdp.d_reclen;
3961 free(dirbuf, M_TEMP);
3963 td->td_retval[0] = ucount;
3970 ogetdirentries_cvt(struct freebsd11_dirent *dp)
3972 #if (BYTE_ORDER == LITTLE_ENDIAN)
3974 * The expected low byte of dp->d_namlen is our dp->d_type.
3975 * The high MBZ byte of dp->d_namlen is our dp->d_namlen.
3977 dp->d_type = dp->d_namlen;
3981 * The dp->d_type is the high byte of the expected dp->d_namlen,
3982 * so must be zero'ed.
3989 * Read a block of directory entries in a filesystem independent format.
3991 #ifndef _SYS_SYSPROTO_H_
3992 struct ogetdirentries_args {
4000 ogetdirentries(struct thread *td, struct ogetdirentries_args *uap)
4005 error = kern_ogetdirentries(td, uap, &loff);
4007 error = copyout(&loff, uap->basep, sizeof(long));
4012 kern_ogetdirentries(struct thread *td, struct ogetdirentries_args *uap,
4018 /* XXX arbitrary sanity limit on `count'. */
4019 if (uap->count > 64 * 1024)
4022 error = freebsd11_kern_getdirentries(td, uap->fd, uap->buf, uap->count,
4023 &base, ogetdirentries_cvt);
4025 if (error == 0 && uap->basep != NULL)
4026 error = copyout(&base, uap->basep, sizeof(long));
4030 #endif /* COMPAT_43 */
4032 #if defined(COMPAT_FREEBSD11)
4033 #ifndef _SYS_SYSPROTO_H_
4034 struct freebsd11_getdirentries_args {
4042 freebsd11_getdirentries(struct thread *td,
4043 struct freebsd11_getdirentries_args *uap)
4048 error = freebsd11_kern_getdirentries(td, uap->fd, uap->buf, uap->count,
4051 if (error == 0 && uap->basep != NULL)
4052 error = copyout(&base, uap->basep, sizeof(long));
4057 freebsd11_getdents(struct thread *td, struct freebsd11_getdents_args *uap)
4059 struct freebsd11_getdirentries_args ap;
4063 ap.count = uap->count;
4065 return (freebsd11_getdirentries(td, &ap));
4067 #endif /* COMPAT_FREEBSD11 */
4070 * Read a block of directory entries in a filesystem independent format.
4073 sys_getdirentries(struct thread *td, struct getdirentries_args *uap)
4078 error = kern_getdirentries(td, uap->fd, uap->buf, uap->count, &base,
4079 NULL, UIO_USERSPACE);
4082 if (uap->basep != NULL)
4083 error = copyout(&base, uap->basep, sizeof(off_t));
4088 kern_getdirentries(struct thread *td, int fd, char *buf, size_t count,
4089 off_t *basep, ssize_t *residp, enum uio_seg bufseg)
4100 if (count > IOSIZE_MAX)
4102 auio.uio_resid = count;
4103 error = getvnode(td, fd, &cap_read_rights, &fp);
4106 if ((fp->f_flag & FREAD) == 0) {
4111 foffset = foffset_lock(fp, 0);
4113 if (vp->v_type != VDIR) {
4117 aiov.iov_base = buf;
4118 aiov.iov_len = count;
4119 auio.uio_iov = &aiov;
4120 auio.uio_iovcnt = 1;
4121 auio.uio_rw = UIO_READ;
4122 auio.uio_segflg = bufseg;
4124 vn_lock(vp, LK_SHARED | LK_RETRY);
4125 AUDIT_ARG_VNODE1(vp);
4126 loff = auio.uio_offset = foffset;
4128 error = mac_vnode_check_readdir(td->td_ucred, vp);
4131 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL,
4133 foffset = auio.uio_offset;
4138 if (count == auio.uio_resid &&
4139 (vp->v_vflag & VV_ROOT) &&
4140 (vp->v_mount->mnt_flag & MNT_UNION)) {
4141 struct vnode *tvp = vp;
4143 vp = vp->v_mount->mnt_vnodecovered;
4154 *residp = auio.uio_resid;
4155 td->td_retval[0] = count - auio.uio_resid;
4157 foffset_unlock(fp, foffset, 0);
4163 * Set the mode mask for creation of filesystem nodes.
4165 #ifndef _SYS_SYSPROTO_H_
4171 sys_umask(struct thread *td, struct umask_args *uap)
4173 struct filedesc *fdp;
4175 fdp = td->td_proc->p_fd;
4176 FILEDESC_XLOCK(fdp);
4177 td->td_retval[0] = fdp->fd_cmask;
4178 fdp->fd_cmask = uap->newmask & ALLPERMS;
4179 FILEDESC_XUNLOCK(fdp);
4184 * Void all references to file by ripping underlying filesystem away from
4187 #ifndef _SYS_SYSPROTO_H_
4188 struct revoke_args {
4193 sys_revoke(struct thread *td, struct revoke_args *uap)
4197 struct nameidata nd;
4200 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
4202 if ((error = namei(&nd)) != 0)
4205 NDFREE(&nd, NDF_ONLY_PNBUF);
4206 if (vp->v_type != VCHR || vp->v_rdev == NULL) {
4211 error = mac_vnode_check_revoke(td->td_ucred, vp);
4215 error = VOP_GETATTR(vp, &vattr, td->td_ucred);
4218 if (td->td_ucred->cr_uid != vattr.va_uid) {
4219 error = priv_check(td, PRIV_VFS_ADMIN);
4223 if (vp->v_usecount > 1 || vcount(vp) > 1)
4224 VOP_REVOKE(vp, REVOKEALL);
4231 * Convert a user file descriptor to a kernel file entry and check that, if it
4232 * is a capability, the correct rights are present. A reference on the file
4233 * entry is held upon returning.
4236 getvnode(struct thread *td, int fd, cap_rights_t *rightsp, struct file **fpp)
4241 error = fget_unlocked(td->td_proc->p_fd, fd, rightsp, &fp);
4246 * The file could be not of the vnode type, or it may be not
4247 * yet fully initialized, in which case the f_vnode pointer
4248 * may be set, but f_ops is still badfileops. E.g.,
4249 * devfs_open() transiently create such situation to
4250 * facilitate csw d_fdopen().
4252 * Dupfdopen() handling in kern_openat() installs the
4253 * half-baked file into the process descriptor table, allowing
4254 * other thread to dereference it. Guard against the race by
4257 if (fp->f_vnode == NULL || fp->f_ops == &badfileops) {
4266 * Get an (NFS) file handle.
4268 #ifndef _SYS_SYSPROTO_H_
4269 struct lgetfh_args {
4275 sys_lgetfh(struct thread *td, struct lgetfh_args *uap)
4278 return (kern_getfhat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->fname,
4279 UIO_USERSPACE, uap->fhp));
4282 #ifndef _SYS_SYSPROTO_H_
4289 sys_getfh(struct thread *td, struct getfh_args *uap)
4292 return (kern_getfhat(td, 0, AT_FDCWD, uap->fname, UIO_USERSPACE,
4297 * syscall for the rpc.lockd to use to translate an open descriptor into
4298 * a NFS file handle.
4300 * warning: do not remove the priv_check() call or this becomes one giant
4303 #ifndef _SYS_SYSPROTO_H_
4304 struct getfhat_args {
4312 sys_getfhat(struct thread *td, struct getfhat_args *uap)
4315 if ((uap->flags & ~(AT_SYMLINK_NOFOLLOW | AT_BENEATH)) != 0)
4317 return (kern_getfhat(td, uap->flags, uap->fd, uap->path, UIO_USERSPACE,
4322 kern_getfhat(struct thread *td, int flags, int fd, const char *path,
4323 enum uio_seg pathseg, fhandle_t *fhp)
4325 struct nameidata nd;
4330 error = priv_check(td, PRIV_VFS_GETFH);
4333 NDINIT_AT(&nd, LOOKUP, ((flags & AT_SYMLINK_NOFOLLOW) != 0 ? NOFOLLOW :
4334 FOLLOW) | ((flags & AT_BENEATH) != 0 ? BENEATH : 0) | LOCKLEAF |
4335 AUDITVNODE1, pathseg, path, fd, td);
4339 NDFREE(&nd, NDF_ONLY_PNBUF);
4341 bzero(&fh, sizeof(fh));
4342 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
4343 error = VOP_VPTOFH(vp, &fh.fh_fid);
4346 error = copyout(&fh, fhp, sizeof (fh));
4350 #ifndef _SYS_SYSPROTO_H_
4351 struct fhlink_args {
4357 sys_fhlink(struct thread *td, struct fhlink_args *uap)
4360 return (kern_fhlinkat(td, AT_FDCWD, uap->to, UIO_USERSPACE, uap->fhp));
4363 #ifndef _SYS_SYSPROTO_H_
4364 struct fhlinkat_args {
4371 sys_fhlinkat(struct thread *td, struct fhlinkat_args *uap)
4374 return (kern_fhlinkat(td, uap->tofd, uap->to, UIO_USERSPACE, uap->fhp));
4378 kern_fhlinkat(struct thread *td, int fd, const char *path,
4379 enum uio_seg pathseg, fhandle_t *fhp)
4386 error = priv_check(td, PRIV_VFS_GETFH);
4389 error = copyin(fhp, &fh, sizeof(fh));
4394 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4396 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_SHARED, &vp);
4401 } while ((error = kern_linkat_vp(td, vp, fd, path, pathseg)) == EAGAIN);
4405 #ifndef _SYS_SYSPROTO_H_
4406 struct fhreadlink_args {
4413 sys_fhreadlink(struct thread *td, struct fhreadlink_args *uap)
4420 error = priv_check(td, PRIV_VFS_GETFH);
4423 if (uap->bufsize > IOSIZE_MAX)
4425 error = copyin(uap->fhp, &fh, sizeof(fh));
4428 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4430 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_SHARED, &vp);
4434 error = kern_readlink_vp(vp, uap->buf, UIO_USERSPACE, uap->bufsize, td);
4440 * syscall for the rpc.lockd to use to translate a NFS file handle into an
4443 * warning: do not remove the priv_check() call or this becomes one giant
4446 #ifndef _SYS_SYSPROTO_H_
4447 struct fhopen_args {
4448 const struct fhandle *u_fhp;
4453 sys_fhopen(struct thread *td, struct fhopen_args *uap)
4462 error = priv_check(td, PRIV_VFS_FHOPEN);
4466 fmode = FFLAGS(uap->flags);
4467 /* why not allow a non-read/write open for our lockd? */
4468 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
4470 error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
4473 /* find the mount point */
4474 mp = vfs_busyfs(&fhp.fh_fsid);
4477 /* now give me my vnode, it gets returned to me locked */
4478 error = VFS_FHTOVP(mp, &fhp.fh_fid, LK_EXCLUSIVE, &vp);
4483 error = falloc_noinstall(td, &fp);
4489 * An extra reference on `fp' has been held for us by
4490 * falloc_noinstall().
4496 error = vn_open_vnode(vp, fmode, td->td_ucred, td, fp);
4498 KASSERT(fp->f_ops == &badfileops,
4499 ("VOP_OPEN in fhopen() set f_ops"));
4500 KASSERT(td->td_dupfd < 0,
4501 ("fhopen() encountered fdopen()"));
4510 fp->f_seqcount[UIO_READ] = 1;
4511 fp->f_seqcount[UIO_WRITE] = 1;
4512 finit(fp, (fmode & FMASK) | (fp->f_flag & FHASLOCK), DTYPE_VNODE, vp,
4515 if ((fmode & O_TRUNC) != 0) {
4516 error = fo_truncate(fp, 0, td->td_ucred, td);
4521 error = finstall(td, fp, &indx, fmode, NULL);
4524 td->td_retval[0] = indx;
4529 * Stat an (NFS) file handle.
4531 #ifndef _SYS_SYSPROTO_H_
4532 struct fhstat_args {
4533 struct fhandle *u_fhp;
4538 sys_fhstat(struct thread *td, struct fhstat_args *uap)
4544 error = copyin(uap->u_fhp, &fh, sizeof(fh));
4547 error = kern_fhstat(td, fh, &sb);
4549 error = copyout(&sb, uap->sb, sizeof(sb));
4554 kern_fhstat(struct thread *td, struct fhandle fh, struct stat *sb)
4560 error = priv_check(td, PRIV_VFS_FHSTAT);
4563 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4565 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_EXCLUSIVE, &vp);
4569 error = vn_stat(vp, sb, td->td_ucred, NOCRED, td);
4575 * Implement fstatfs() for (NFS) file handles.
4577 #ifndef _SYS_SYSPROTO_H_
4578 struct fhstatfs_args {
4579 struct fhandle *u_fhp;
4584 sys_fhstatfs(struct thread *td, struct fhstatfs_args *uap)
4590 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4593 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
4594 error = kern_fhstatfs(td, fh, sfp);
4596 error = copyout(sfp, uap->buf, sizeof(*sfp));
4597 free(sfp, M_STATFS);
4602 kern_fhstatfs(struct thread *td, fhandle_t fh, struct statfs *buf)
4608 error = priv_check(td, PRIV_VFS_FHSTATFS);
4611 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4613 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_EXCLUSIVE, &vp);
4619 error = prison_canseemount(td->td_ucred, mp);
4623 error = mac_mount_check_stat(td->td_ucred, mp);
4627 error = VFS_STATFS(mp, buf);
4634 * Unlike madvise(2), we do not make a best effort to remember every
4635 * possible caching hint. Instead, we remember the last setting with
4636 * the exception that we will allow POSIX_FADV_NORMAL to adjust the
4637 * region of any current setting.
4640 kern_posix_fadvise(struct thread *td, int fd, off_t offset, off_t len,
4643 struct fadvise_info *fa, *new;
4649 if (offset < 0 || len < 0 || offset > OFF_MAX - len)
4651 AUDIT_ARG_VALUE(advice);
4653 case POSIX_FADV_SEQUENTIAL:
4654 case POSIX_FADV_RANDOM:
4655 case POSIX_FADV_NOREUSE:
4656 new = malloc(sizeof(*fa), M_FADVISE, M_WAITOK);
4658 case POSIX_FADV_NORMAL:
4659 case POSIX_FADV_WILLNEED:
4660 case POSIX_FADV_DONTNEED:
4666 /* XXX: CAP_POSIX_FADVISE? */
4668 error = fget(td, fd, &cap_no_rights, &fp);
4671 AUDIT_ARG_FILE(td->td_proc, fp);
4672 if ((fp->f_ops->fo_flags & DFLAG_SEEKABLE) == 0) {
4676 if (fp->f_type != DTYPE_VNODE) {
4681 if (vp->v_type != VREG) {
4688 end = offset + len - 1;
4690 case POSIX_FADV_SEQUENTIAL:
4691 case POSIX_FADV_RANDOM:
4692 case POSIX_FADV_NOREUSE:
4694 * Try to merge any existing non-standard region with
4695 * this new region if possible, otherwise create a new
4696 * non-standard region for this request.
4698 mtx_pool_lock(mtxpool_sleep, fp);
4700 if (fa != NULL && fa->fa_advice == advice &&
4701 ((fa->fa_start <= end && fa->fa_end >= offset) ||
4702 (end != OFF_MAX && fa->fa_start == end + 1) ||
4703 (fa->fa_end != OFF_MAX && fa->fa_end + 1 == offset))) {
4704 if (offset < fa->fa_start)
4705 fa->fa_start = offset;
4706 if (end > fa->fa_end)
4709 new->fa_advice = advice;
4710 new->fa_start = offset;
4715 mtx_pool_unlock(mtxpool_sleep, fp);
4717 case POSIX_FADV_NORMAL:
4719 * If a the "normal" region overlaps with an existing
4720 * non-standard region, trim or remove the
4721 * non-standard region.
4723 mtx_pool_lock(mtxpool_sleep, fp);
4726 if (offset <= fa->fa_start && end >= fa->fa_end) {
4728 fp->f_advice = NULL;
4729 } else if (offset <= fa->fa_start &&
4730 end >= fa->fa_start)
4731 fa->fa_start = end + 1;
4732 else if (offset <= fa->fa_end && end >= fa->fa_end)
4733 fa->fa_end = offset - 1;
4734 else if (offset >= fa->fa_start && end <= fa->fa_end) {
4736 * If the "normal" region is a middle
4737 * portion of the existing
4738 * non-standard region, just remove
4739 * the whole thing rather than picking
4740 * one side or the other to
4744 fp->f_advice = NULL;
4747 mtx_pool_unlock(mtxpool_sleep, fp);
4749 case POSIX_FADV_WILLNEED:
4750 case POSIX_FADV_DONTNEED:
4751 error = VOP_ADVISE(vp, offset, end, advice);
4757 free(new, M_FADVISE);
4762 sys_posix_fadvise(struct thread *td, struct posix_fadvise_args *uap)
4766 error = kern_posix_fadvise(td, uap->fd, uap->offset, uap->len,
4768 return (kern_posix_error(td, error));
4772 kern_copy_file_range(struct thread *td, int infd, off_t *inoffp, int outfd,
4773 off_t *outoffp, size_t len, unsigned int flags)
4775 struct file *infp, *outfp;
4776 struct vnode *invp, *outvp;
4779 void *rl_rcookie, *rl_wcookie;
4780 off_t savinoff, savoutoff;
4782 infp = outfp = NULL;
4783 rl_rcookie = rl_wcookie = NULL;
4792 if (len > SSIZE_MAX)
4794 * Although the len argument is size_t, the return argument
4795 * is ssize_t (which is signed). Therefore a size that won't
4796 * fit in ssize_t can't be returned.
4800 /* Get the file structures for the file descriptors. */
4801 error = fget_read(td, infd, &cap_read_rights, &infp);
4804 if (infp->f_ops == &badfileops) {
4808 if (infp->f_vnode == NULL) {
4812 error = fget_write(td, outfd, &cap_write_rights, &outfp);
4815 if (outfp->f_ops == &badfileops) {
4819 if (outfp->f_vnode == NULL) {
4824 /* Set the offset pointers to the correct place. */
4826 inoffp = &infp->f_offset;
4827 if (outoffp == NULL)
4828 outoffp = &outfp->f_offset;
4830 savoutoff = *outoffp;
4832 invp = infp->f_vnode;
4833 outvp = outfp->f_vnode;
4834 /* Sanity check the f_flag bits. */
4835 if ((outfp->f_flag & (FWRITE | FAPPEND)) != FWRITE ||
4836 (infp->f_flag & FREAD) == 0) {
4841 /* If len == 0, just return 0. */
4846 * If infp and outfp refer to the same file, the byte ranges cannot
4849 if (invp == outvp && ((savinoff <= savoutoff && savinoff + len >
4850 savoutoff) || (savinoff > savoutoff && savoutoff + len >
4856 /* Range lock the byte ranges for both invp and outvp. */
4858 rl_wcookie = vn_rangelock_wlock(outvp, *outoffp, *outoffp +
4860 rl_rcookie = vn_rangelock_tryrlock(invp, *inoffp, *inoffp +
4862 if (rl_rcookie != NULL)
4864 vn_rangelock_unlock(outvp, rl_wcookie);
4865 rl_rcookie = vn_rangelock_rlock(invp, *inoffp, *inoffp + len);
4866 vn_rangelock_unlock(invp, rl_rcookie);
4870 error = vn_copy_file_range(invp, inoffp, outvp, outoffp, &retlen,
4871 flags, infp->f_cred, outfp->f_cred, td);
4873 if (rl_rcookie != NULL)
4874 vn_rangelock_unlock(invp, rl_rcookie);
4875 if (rl_wcookie != NULL)
4876 vn_rangelock_unlock(outvp, rl_wcookie);
4877 if (savinoff != -1 && (error == EINTR || error == ERESTART)) {
4879 *outoffp = savoutoff;
4885 td->td_retval[0] = retlen;
4890 sys_copy_file_range(struct thread *td, struct copy_file_range_args *uap)
4892 off_t inoff, outoff, *inoffp, *outoffp;
4895 inoffp = outoffp = NULL;
4896 if (uap->inoffp != NULL) {
4897 error = copyin(uap->inoffp, &inoff, sizeof(off_t));
4902 if (uap->outoffp != NULL) {
4903 error = copyin(uap->outoffp, &outoff, sizeof(off_t));
4908 error = kern_copy_file_range(td, uap->infd, inoffp, uap->outfd,
4909 outoffp, uap->len, uap->flags);
4910 if (error == 0 && uap->inoffp != NULL)
4911 error = copyout(inoffp, uap->inoffp, sizeof(off_t));
4912 if (error == 0 && uap->outoffp != NULL)
4913 error = copyout(outoffp, uap->outoffp, sizeof(off_t));