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_BENEATH) != 0)
125 if ((at_flags & AT_RESOLVE_BENEATH) != 0)
127 if ((at_flags & AT_SYMLINK_FOLLOW) != 0)
129 /* NOFOLLOW is pseudo flag */
130 if ((mask & AT_SYMLINK_NOFOLLOW) != 0) {
131 res |= (at_flags & AT_SYMLINK_NOFOLLOW) != 0 ? NOFOLLOW :
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 | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
341 mp = nd.ni_vp->v_mount;
343 NDFREE(&nd, NDF_ONLY_PNBUF);
345 return (kern_do_statfs(td, mp, buf));
349 * Get filesystem statistics.
351 #ifndef _SYS_SYSPROTO_H_
352 struct fstatfs_args {
358 sys_fstatfs(struct thread *td, struct fstatfs_args *uap)
363 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
364 error = kern_fstatfs(td, uap->fd, sfp);
366 error = copyout(sfp, uap->buf, sizeof(struct statfs));
372 kern_fstatfs(struct thread *td, int fd, struct statfs *buf)
380 error = getvnode(td, fd, &cap_fstatfs_rights, &fp);
384 vn_lock(vp, LK_SHARED | LK_RETRY);
386 AUDIT_ARG_VNODE1(vp);
393 return (kern_do_statfs(td, mp, buf));
397 * Get statistics on all filesystems.
399 #ifndef _SYS_SYSPROTO_H_
400 struct getfsstat_args {
407 sys_getfsstat(struct thread *td, struct getfsstat_args *uap)
412 if (uap->bufsize < 0 || uap->bufsize > SIZE_MAX)
414 error = kern_getfsstat(td, &uap->buf, uap->bufsize, &count,
415 UIO_USERSPACE, uap->mode);
417 td->td_retval[0] = count;
422 * If (bufsize > 0 && bufseg == UIO_SYSSPACE)
423 * The caller is responsible for freeing memory which will be allocated
427 kern_getfsstat(struct thread *td, struct statfs **buf, size_t bufsize,
428 size_t *countp, enum uio_seg bufseg, int mode)
430 struct mount *mp, *nmp;
431 struct statfs *sfsp, *sp, *sptmp, *tofree;
432 size_t count, maxcount;
440 if (bufseg == UIO_SYSSPACE)
445 maxcount = bufsize / sizeof(struct statfs);
449 } else if (bufseg == UIO_USERSPACE) {
452 } else /* if (bufseg == UIO_SYSSPACE) */ {
454 mtx_lock(&mountlist_mtx);
455 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
458 mtx_unlock(&mountlist_mtx);
459 if (maxcount > count)
461 tofree = sfsp = *buf = malloc(maxcount * sizeof(struct statfs),
468 * If there is no target buffer they only want the count.
470 * This could be TAILQ_FOREACH but it is open-coded to match the original
474 mtx_lock(&mountlist_mtx);
475 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
476 if (prison_canseemount(td->td_ucred, mp) != 0) {
477 nmp = TAILQ_NEXT(mp, mnt_list);
481 if (mac_mount_check_stat(td->td_ucred, mp) != 0) {
482 nmp = TAILQ_NEXT(mp, mnt_list);
487 nmp = TAILQ_NEXT(mp, mnt_list);
489 mtx_unlock(&mountlist_mtx);
495 * They want the entire thing.
497 * Short-circuit the corner case of no room for anything, avoids
504 mtx_lock(&mountlist_mtx);
505 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
506 if (prison_canseemount(td->td_ucred, mp) != 0) {
507 nmp = TAILQ_NEXT(mp, mnt_list);
511 if (mac_mount_check_stat(td->td_ucred, mp) != 0) {
512 nmp = TAILQ_NEXT(mp, mnt_list);
516 if (mode == MNT_WAIT) {
517 if (vfs_busy(mp, MBF_MNTLSTLOCK) != 0) {
519 * If vfs_busy() failed, and MBF_NOWAIT
520 * wasn't passed, then the mp is gone.
521 * Furthermore, because of MBF_MNTLSTLOCK,
522 * the mountlist_mtx was dropped. We have
523 * no other choice than to start over.
525 mtx_unlock(&mountlist_mtx);
526 free(tofree, M_STATFS);
530 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK) != 0) {
531 nmp = TAILQ_NEXT(mp, mnt_list);
537 * If MNT_NOWAIT is specified, do not refresh
540 if (mode != MNT_NOWAIT) {
541 error = VFS_STATFS(mp, sp);
543 mtx_lock(&mountlist_mtx);
544 nmp = TAILQ_NEXT(mp, mnt_list);
549 if (priv_check_cred_vfs_generation(td->td_ucred)) {
550 sptmp = malloc(sizeof(struct statfs), M_STATFS,
553 sptmp->f_fsid.val[0] = sptmp->f_fsid.val[1] = 0;
554 prison_enforce_statfs(td->td_ucred, mp, sptmp);
558 if (bufseg == UIO_SYSSPACE) {
559 bcopy(sp, sfsp, sizeof(*sp));
560 free(sptmp, M_STATFS);
561 } else /* if (bufseg == UIO_USERSPACE) */ {
562 error = copyout(sp, sfsp, sizeof(*sp));
563 free(sptmp, M_STATFS);
572 if (count == maxcount) {
577 mtx_lock(&mountlist_mtx);
578 nmp = TAILQ_NEXT(mp, mnt_list);
581 mtx_unlock(&mountlist_mtx);
587 #ifdef COMPAT_FREEBSD4
589 * Get old format filesystem statistics.
591 static void freebsd4_cvtstatfs(struct statfs *, struct ostatfs *);
593 #ifndef _SYS_SYSPROTO_H_
594 struct freebsd4_statfs_args {
600 freebsd4_statfs(struct thread *td, struct freebsd4_statfs_args *uap)
606 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
607 error = kern_statfs(td, uap->path, UIO_USERSPACE, sfp);
609 freebsd4_cvtstatfs(sfp, &osb);
610 error = copyout(&osb, uap->buf, sizeof(osb));
617 * Get filesystem statistics.
619 #ifndef _SYS_SYSPROTO_H_
620 struct freebsd4_fstatfs_args {
626 freebsd4_fstatfs(struct thread *td, struct freebsd4_fstatfs_args *uap)
632 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
633 error = kern_fstatfs(td, uap->fd, sfp);
635 freebsd4_cvtstatfs(sfp, &osb);
636 error = copyout(&osb, uap->buf, sizeof(osb));
643 * Get statistics on all filesystems.
645 #ifndef _SYS_SYSPROTO_H_
646 struct freebsd4_getfsstat_args {
653 freebsd4_getfsstat(struct thread *td, struct freebsd4_getfsstat_args *uap)
655 struct statfs *buf, *sp;
660 if (uap->bufsize < 0)
662 count = uap->bufsize / sizeof(struct ostatfs);
663 if (count > SIZE_MAX / sizeof(struct statfs))
665 size = count * sizeof(struct statfs);
666 error = kern_getfsstat(td, &buf, size, &count, UIO_SYSSPACE,
669 td->td_retval[0] = count;
672 while (count != 0 && error == 0) {
673 freebsd4_cvtstatfs(sp, &osb);
674 error = copyout(&osb, uap->buf, sizeof(osb));
685 * Implement fstatfs() for (NFS) file handles.
687 #ifndef _SYS_SYSPROTO_H_
688 struct freebsd4_fhstatfs_args {
689 struct fhandle *u_fhp;
694 freebsd4_fhstatfs(struct thread *td, struct freebsd4_fhstatfs_args *uap)
701 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
704 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
705 error = kern_fhstatfs(td, fh, sfp);
707 freebsd4_cvtstatfs(sfp, &osb);
708 error = copyout(&osb, uap->buf, sizeof(osb));
715 * Convert a new format statfs structure to an old format statfs structure.
718 freebsd4_cvtstatfs(struct statfs *nsp, struct ostatfs *osp)
721 statfs_scale_blocks(nsp, LONG_MAX);
722 bzero(osp, sizeof(*osp));
723 osp->f_bsize = nsp->f_bsize;
724 osp->f_iosize = MIN(nsp->f_iosize, LONG_MAX);
725 osp->f_blocks = nsp->f_blocks;
726 osp->f_bfree = nsp->f_bfree;
727 osp->f_bavail = nsp->f_bavail;
728 osp->f_files = MIN(nsp->f_files, LONG_MAX);
729 osp->f_ffree = MIN(nsp->f_ffree, LONG_MAX);
730 osp->f_owner = nsp->f_owner;
731 osp->f_type = nsp->f_type;
732 osp->f_flags = nsp->f_flags;
733 osp->f_syncwrites = MIN(nsp->f_syncwrites, LONG_MAX);
734 osp->f_asyncwrites = MIN(nsp->f_asyncwrites, LONG_MAX);
735 osp->f_syncreads = MIN(nsp->f_syncreads, LONG_MAX);
736 osp->f_asyncreads = MIN(nsp->f_asyncreads, LONG_MAX);
737 strlcpy(osp->f_fstypename, nsp->f_fstypename,
738 MIN(MFSNAMELEN, OMFSNAMELEN));
739 strlcpy(osp->f_mntonname, nsp->f_mntonname,
740 MIN(MNAMELEN, OMNAMELEN));
741 strlcpy(osp->f_mntfromname, nsp->f_mntfromname,
742 MIN(MNAMELEN, OMNAMELEN));
743 osp->f_fsid = nsp->f_fsid;
745 #endif /* COMPAT_FREEBSD4 */
747 #if defined(COMPAT_FREEBSD11)
749 * Get old format filesystem statistics.
751 static void freebsd11_cvtstatfs(struct statfs *, struct freebsd11_statfs *);
754 freebsd11_statfs(struct thread *td, struct freebsd11_statfs_args *uap)
756 struct freebsd11_statfs osb;
760 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
761 error = kern_statfs(td, uap->path, UIO_USERSPACE, sfp);
763 freebsd11_cvtstatfs(sfp, &osb);
764 error = copyout(&osb, uap->buf, sizeof(osb));
771 * Get filesystem statistics.
774 freebsd11_fstatfs(struct thread *td, struct freebsd11_fstatfs_args *uap)
776 struct freebsd11_statfs osb;
780 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
781 error = kern_fstatfs(td, uap->fd, sfp);
783 freebsd11_cvtstatfs(sfp, &osb);
784 error = copyout(&osb, uap->buf, sizeof(osb));
791 * Get statistics on all filesystems.
794 freebsd11_getfsstat(struct thread *td, struct freebsd11_getfsstat_args *uap)
796 struct freebsd11_statfs osb;
797 struct statfs *buf, *sp;
801 count = uap->bufsize / sizeof(struct ostatfs);
802 size = count * sizeof(struct statfs);
803 error = kern_getfsstat(td, &buf, size, &count, UIO_SYSSPACE,
806 td->td_retval[0] = count;
809 while (count > 0 && error == 0) {
810 freebsd11_cvtstatfs(sp, &osb);
811 error = copyout(&osb, uap->buf, sizeof(osb));
822 * Implement fstatfs() for (NFS) file handles.
825 freebsd11_fhstatfs(struct thread *td, struct freebsd11_fhstatfs_args *uap)
827 struct freebsd11_statfs osb;
832 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
835 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
836 error = kern_fhstatfs(td, fh, sfp);
838 freebsd11_cvtstatfs(sfp, &osb);
839 error = copyout(&osb, uap->buf, sizeof(osb));
846 * Convert a new format statfs structure to an old format statfs structure.
849 freebsd11_cvtstatfs(struct statfs *nsp, struct freebsd11_statfs *osp)
852 bzero(osp, sizeof(*osp));
853 osp->f_version = FREEBSD11_STATFS_VERSION;
854 osp->f_type = nsp->f_type;
855 osp->f_flags = nsp->f_flags;
856 osp->f_bsize = nsp->f_bsize;
857 osp->f_iosize = nsp->f_iosize;
858 osp->f_blocks = nsp->f_blocks;
859 osp->f_bfree = nsp->f_bfree;
860 osp->f_bavail = nsp->f_bavail;
861 osp->f_files = nsp->f_files;
862 osp->f_ffree = nsp->f_ffree;
863 osp->f_syncwrites = nsp->f_syncwrites;
864 osp->f_asyncwrites = nsp->f_asyncwrites;
865 osp->f_syncreads = nsp->f_syncreads;
866 osp->f_asyncreads = nsp->f_asyncreads;
867 osp->f_namemax = nsp->f_namemax;
868 osp->f_owner = nsp->f_owner;
869 osp->f_fsid = nsp->f_fsid;
870 strlcpy(osp->f_fstypename, nsp->f_fstypename,
871 MIN(MFSNAMELEN, sizeof(osp->f_fstypename)));
872 strlcpy(osp->f_mntonname, nsp->f_mntonname,
873 MIN(MNAMELEN, sizeof(osp->f_mntonname)));
874 strlcpy(osp->f_mntfromname, nsp->f_mntfromname,
875 MIN(MNAMELEN, sizeof(osp->f_mntfromname)));
877 #endif /* COMPAT_FREEBSD11 */
880 * Change current working directory to a given file descriptor.
882 #ifndef _SYS_SYSPROTO_H_
888 sys_fchdir(struct thread *td, struct fchdir_args *uap)
890 struct vnode *vp, *tdp;
895 AUDIT_ARG_FD(uap->fd);
896 error = getvnode(td, uap->fd, &cap_fchdir_rights,
903 vn_lock(vp, LK_SHARED | LK_RETRY);
904 AUDIT_ARG_VNODE1(vp);
905 error = change_dir(vp, td);
906 while (!error && (mp = vp->v_mountedhere) != NULL) {
909 error = VFS_ROOT(mp, LK_SHARED, &tdp);
926 * Change current working directory (``.'').
928 #ifndef _SYS_SYSPROTO_H_
934 sys_chdir(struct thread *td, struct chdir_args *uap)
937 return (kern_chdir(td, uap->path, UIO_USERSPACE));
941 kern_chdir(struct thread *td, const char *path, enum uio_seg pathseg)
946 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
948 if ((error = namei(&nd)) != 0)
950 if ((error = change_dir(nd.ni_vp, td)) != 0) {
952 NDFREE(&nd, NDF_ONLY_PNBUF);
955 VOP_UNLOCK(nd.ni_vp);
956 NDFREE(&nd, NDF_ONLY_PNBUF);
957 pwd_chdir(td, nd.ni_vp);
962 * Change notion of root (``/'') directory.
964 #ifndef _SYS_SYSPROTO_H_
970 sys_chroot(struct thread *td, struct chroot_args *uap)
975 error = priv_check(td, PRIV_VFS_CHROOT);
978 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
979 UIO_USERSPACE, uap->path, td);
983 error = change_dir(nd.ni_vp, td);
987 error = mac_vnode_check_chroot(td->td_ucred, nd.ni_vp);
991 VOP_UNLOCK(nd.ni_vp);
992 error = pwd_chroot(td, nd.ni_vp);
994 NDFREE(&nd, NDF_ONLY_PNBUF);
999 NDFREE(&nd, NDF_ONLY_PNBUF);
1004 * Common routine for chroot and chdir. Callers must provide a locked vnode
1008 change_dir(struct vnode *vp, struct thread *td)
1014 ASSERT_VOP_LOCKED(vp, "change_dir(): vp not locked");
1015 if (vp->v_type != VDIR)
1018 error = mac_vnode_check_chdir(td->td_ucred, vp);
1022 return (VOP_ACCESS(vp, VEXEC, td->td_ucred, td));
1025 static __inline void
1026 flags_to_rights(int flags, cap_rights_t *rightsp)
1029 if (flags & O_EXEC) {
1030 cap_rights_set_one(rightsp, CAP_FEXECVE);
1032 switch ((flags & O_ACCMODE)) {
1034 cap_rights_set_one(rightsp, CAP_READ);
1037 cap_rights_set_one(rightsp, CAP_READ);
1040 cap_rights_set_one(rightsp, CAP_WRITE);
1041 if (!(flags & (O_APPEND | O_TRUNC)))
1042 cap_rights_set_one(rightsp, CAP_SEEK);
1047 if (flags & O_CREAT)
1048 cap_rights_set_one(rightsp, CAP_CREATE);
1050 if (flags & O_TRUNC)
1051 cap_rights_set_one(rightsp, CAP_FTRUNCATE);
1053 if (flags & (O_SYNC | O_FSYNC))
1054 cap_rights_set_one(rightsp, CAP_FSYNC);
1056 if (flags & (O_EXLOCK | O_SHLOCK))
1057 cap_rights_set_one(rightsp, CAP_FLOCK);
1061 * Check permissions, allocate an open file structure, and call the device
1062 * open routine if any.
1064 #ifndef _SYS_SYSPROTO_H_
1072 sys_open(struct thread *td, struct open_args *uap)
1075 return (kern_openat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1076 uap->flags, uap->mode));
1079 #ifndef _SYS_SYSPROTO_H_
1080 struct openat_args {
1088 sys_openat(struct thread *td, struct openat_args *uap)
1091 AUDIT_ARG_FD(uap->fd);
1092 return (kern_openat(td, uap->fd, uap->path, UIO_USERSPACE, uap->flag,
1097 kern_openat(struct thread *td, int fd, const char *path, enum uio_seg pathseg,
1098 int flags, int mode)
1100 struct proc *p = td->td_proc;
1101 struct filedesc *fdp;
1102 struct pwddesc *pdp;
1105 struct nameidata nd;
1106 cap_rights_t rights;
1107 int cmode, error, indx;
1113 AUDIT_ARG_FFLAGS(flags);
1114 AUDIT_ARG_MODE(mode);
1115 cap_rights_init_one(&rights, CAP_LOOKUP);
1116 flags_to_rights(flags, &rights);
1118 * Only one of the O_EXEC, O_RDONLY, O_WRONLY and O_RDWR flags
1121 if (flags & O_EXEC) {
1122 if (flags & O_ACCMODE)
1124 } else if ((flags & O_ACCMODE) == O_ACCMODE) {
1127 flags = FFLAGS(flags);
1131 * Allocate a file structure. The descriptor to reference it
1132 * is allocated and set by finstall() below.
1134 error = falloc_noinstall(td, &fp);
1138 * An extra reference on `fp' has been held for us by
1139 * falloc_noinstall().
1141 /* Set the flags early so the finit in devfs can pick them up. */
1142 fp->f_flag = flags & FMASK;
1143 cmode = ((mode & ~pdp->pd_cmask) & ALLPERMS) & ~S_ISTXT;
1144 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, fd,
1146 td->td_dupfd = -1; /* XXX check for fdopen */
1147 error = vn_open(&nd, &flags, cmode, fp);
1150 * If the vn_open replaced the method vector, something
1151 * wonderous happened deep below and we just pass it up
1152 * pretending we know what we do.
1154 if (error == ENXIO && fp->f_ops != &badfileops)
1158 * Handle special fdopen() case. bleh.
1160 * Don't do this for relative (capability) lookups; we don't
1161 * understand exactly what would happen, and we don't think
1162 * that it ever should.
1164 if ((nd.ni_resflags & NIRES_STRICTREL) == 0 &&
1165 (error == ENODEV || error == ENXIO) &&
1166 td->td_dupfd >= 0) {
1167 error = dupfdopen(td, fdp, td->td_dupfd, flags, error,
1176 NDFREE(&nd, NDF_ONLY_PNBUF);
1180 * Store the vnode, for any f_type. Typically, the vnode use
1181 * count is decremented by direct call to vn_closefile() for
1182 * files that switched type in the cdevsw fdopen() method.
1186 * If the file wasn't claimed by devfs bind it to the normal
1187 * vnode operations here.
1189 if (fp->f_ops == &badfileops) {
1190 KASSERT(vp->v_type != VFIFO, ("Unexpected fifo."));
1191 finit_vnode(fp, flags, NULL, &vnops);
1195 if (flags & O_TRUNC) {
1196 error = fo_truncate(fp, 0, td->td_ucred, td);
1202 * If we haven't already installed the FD (for dupfdopen), do so now.
1205 struct filecaps *fcaps;
1208 if ((nd.ni_resflags & NIRES_STRICTREL) != 0)
1209 fcaps = &nd.ni_filecaps;
1213 error = finstall(td, fp, &indx, flags, fcaps);
1214 /* On success finstall() consumes fcaps. */
1216 filecaps_free(&nd.ni_filecaps);
1220 filecaps_free(&nd.ni_filecaps);
1224 * Release our private reference, leaving the one associated with
1225 * the descriptor table intact.
1228 td->td_retval[0] = indx;
1231 KASSERT(indx == -1, ("indx=%d, should be -1", indx));
1240 #ifndef _SYS_SYSPROTO_H_
1241 struct ocreat_args {
1247 ocreat(struct thread *td, struct ocreat_args *uap)
1250 return (kern_openat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1251 O_WRONLY | O_CREAT | O_TRUNC, uap->mode));
1253 #endif /* COMPAT_43 */
1256 * Create a special file.
1258 #ifndef _SYS_SYSPROTO_H_
1259 struct mknodat_args {
1267 sys_mknodat(struct thread *td, struct mknodat_args *uap)
1270 return (kern_mknodat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode,
1274 #if defined(COMPAT_FREEBSD11)
1276 freebsd11_mknod(struct thread *td,
1277 struct freebsd11_mknod_args *uap)
1280 return (kern_mknodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1281 uap->mode, uap->dev));
1285 freebsd11_mknodat(struct thread *td,
1286 struct freebsd11_mknodat_args *uap)
1289 return (kern_mknodat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode,
1292 #endif /* COMPAT_FREEBSD11 */
1295 kern_mknodat(struct thread *td, int fd, const char *path, enum uio_seg pathseg,
1296 int mode, dev_t dev)
1301 struct nameidata nd;
1302 int error, whiteout = 0;
1304 AUDIT_ARG_MODE(mode);
1306 switch (mode & S_IFMT) {
1309 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
1310 if (error == 0 && dev == VNOVAL)
1314 error = priv_check(td, PRIV_VFS_MKNOD_WHT);
1318 return (kern_mkfifoat(td, fd, path, pathseg, mode));
1328 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
1329 NOCACHE, pathseg, path, fd, &cap_mknodat_rights,
1331 if ((error = namei(&nd)) != 0)
1335 NDFREE(&nd, NDF_ONLY_PNBUF);
1336 if (vp == nd.ni_dvp)
1344 vattr.va_mode = (mode & ALLPERMS) &
1345 ~td->td_proc->p_pd->pd_cmask;
1346 vattr.va_rdev = dev;
1349 switch (mode & S_IFMT) {
1351 vattr.va_type = VCHR;
1354 vattr.va_type = VBLK;
1360 panic("kern_mknod: invalid mode");
1363 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1364 NDFREE(&nd, NDF_ONLY_PNBUF);
1366 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1371 if (error == 0 && !whiteout)
1372 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp,
1373 &nd.ni_cnd, &vattr);
1377 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, CREATE);
1379 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp,
1380 &nd.ni_cnd, &vattr);
1385 NDFREE(&nd, NDF_ONLY_PNBUF);
1387 vn_finished_write(mp);
1388 if (error == ERELOOKUP)
1394 * Create a named pipe.
1396 #ifndef _SYS_SYSPROTO_H_
1397 struct mkfifo_args {
1403 sys_mkfifo(struct thread *td, struct mkfifo_args *uap)
1406 return (kern_mkfifoat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1410 #ifndef _SYS_SYSPROTO_H_
1411 struct mkfifoat_args {
1418 sys_mkfifoat(struct thread *td, struct mkfifoat_args *uap)
1421 return (kern_mkfifoat(td, uap->fd, uap->path, UIO_USERSPACE,
1426 kern_mkfifoat(struct thread *td, int fd, const char *path,
1427 enum uio_seg pathseg, int mode)
1431 struct nameidata nd;
1434 AUDIT_ARG_MODE(mode);
1437 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
1438 NOCACHE, pathseg, path, fd, &cap_mkfifoat_rights,
1440 if ((error = namei(&nd)) != 0)
1442 if (nd.ni_vp != NULL) {
1443 NDFREE(&nd, NDF_ONLY_PNBUF);
1444 if (nd.ni_vp == nd.ni_dvp)
1451 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1452 NDFREE(&nd, NDF_ONLY_PNBUF);
1454 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1459 vattr.va_type = VFIFO;
1460 vattr.va_mode = (mode & ALLPERMS) & ~td->td_proc->p_pd->pd_cmask;
1462 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1467 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
1474 vn_finished_write(mp);
1475 NDFREE(&nd, NDF_ONLY_PNBUF);
1476 if (error == ERELOOKUP)
1482 * Make a hard file link.
1484 #ifndef _SYS_SYSPROTO_H_
1491 sys_link(struct thread *td, struct link_args *uap)
1494 return (kern_linkat(td, AT_FDCWD, AT_FDCWD, uap->path, uap->link,
1495 UIO_USERSPACE, FOLLOW));
1498 #ifndef _SYS_SYSPROTO_H_
1499 struct linkat_args {
1508 sys_linkat(struct thread *td, struct linkat_args *uap)
1513 if ((flag & ~(AT_SYMLINK_FOLLOW | AT_BENEATH |
1514 AT_RESOLVE_BENEATH)) != 0)
1517 return (kern_linkat(td, uap->fd1, uap->fd2, uap->path1, uap->path2,
1518 UIO_USERSPACE, at2cnpflags(flag, AT_SYMLINK_FOLLOW | AT_BENEATH |
1519 AT_RESOLVE_BENEATH)));
1522 int hardlink_check_uid = 0;
1523 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
1524 &hardlink_check_uid, 0,
1525 "Unprivileged processes cannot create hard links to files owned by other "
1527 static int hardlink_check_gid = 0;
1528 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
1529 &hardlink_check_gid, 0,
1530 "Unprivileged processes cannot create hard links to files owned by other "
1534 can_hardlink(struct vnode *vp, struct ucred *cred)
1539 if (!hardlink_check_uid && !hardlink_check_gid)
1542 error = VOP_GETATTR(vp, &va, cred);
1546 if (hardlink_check_uid && cred->cr_uid != va.va_uid) {
1547 error = priv_check_cred(cred, PRIV_VFS_LINK);
1552 if (hardlink_check_gid && !groupmember(va.va_gid, cred)) {
1553 error = priv_check_cred(cred, PRIV_VFS_LINK);
1562 kern_linkat(struct thread *td, int fd1, int fd2, const char *path1,
1563 const char *path2, enum uio_seg segflag, int follow)
1565 struct nameidata nd;
1570 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, segflag,
1571 path1, fd1, &cap_linkat_source_rights, td);
1572 if ((error = namei(&nd)) != 0)
1574 NDFREE(&nd, NDF_ONLY_PNBUF);
1575 error = kern_linkat_vp(td, nd.ni_vp, fd2, path2, segflag);
1576 } while (error == EAGAIN || error == ERELOOKUP);
1581 kern_linkat_vp(struct thread *td, struct vnode *vp, int fd, const char *path,
1582 enum uio_seg segflag)
1584 struct nameidata nd;
1588 if (vp->v_type == VDIR) {
1590 return (EPERM); /* POSIX */
1592 NDINIT_ATRIGHTS(&nd, CREATE,
1593 LOCKPARENT | SAVENAME | AUDITVNODE2 | NOCACHE, segflag, path, fd,
1594 &cap_linkat_target_rights, td);
1595 if ((error = namei(&nd)) == 0) {
1596 if (nd.ni_vp != NULL) {
1597 NDFREE(&nd, NDF_ONLY_PNBUF);
1598 if (nd.ni_dvp == nd.ni_vp)
1605 } else if (nd.ni_dvp->v_mount != vp->v_mount) {
1607 * Cross-device link. No need to recheck
1608 * vp->v_type, since it cannot change, except
1611 NDFREE(&nd, NDF_ONLY_PNBUF);
1615 } else if ((error = vn_lock(vp, LK_EXCLUSIVE)) == 0) {
1616 error = can_hardlink(vp, td->td_ucred);
1619 error = mac_vnode_check_link(td->td_ucred,
1620 nd.ni_dvp, vp, &nd.ni_cnd);
1625 NDFREE(&nd, NDF_ONLY_PNBUF);
1628 error = vn_start_write(vp, &mp, V_NOWAIT);
1632 NDFREE(&nd, NDF_ONLY_PNBUF);
1633 error = vn_start_write(NULL, &mp,
1639 error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
1642 vn_finished_write(mp);
1643 NDFREE(&nd, NDF_ONLY_PNBUF);
1646 NDFREE(&nd, NDF_ONLY_PNBUF);
1656 * Make a symbolic link.
1658 #ifndef _SYS_SYSPROTO_H_
1659 struct symlink_args {
1665 sys_symlink(struct thread *td, struct symlink_args *uap)
1668 return (kern_symlinkat(td, uap->path, AT_FDCWD, uap->link,
1672 #ifndef _SYS_SYSPROTO_H_
1673 struct symlinkat_args {
1680 sys_symlinkat(struct thread *td, struct symlinkat_args *uap)
1683 return (kern_symlinkat(td, uap->path1, uap->fd, uap->path2,
1688 kern_symlinkat(struct thread *td, const char *path1, int fd, const char *path2,
1689 enum uio_seg segflg)
1693 const char *syspath;
1695 struct nameidata nd;
1698 if (segflg == UIO_SYSSPACE) {
1701 tmppath = uma_zalloc(namei_zone, M_WAITOK);
1702 if ((error = copyinstr(path1, tmppath, MAXPATHLEN, NULL)) != 0)
1706 AUDIT_ARG_TEXT(syspath);
1709 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
1710 NOCACHE, segflg, path2, fd, &cap_symlinkat_rights,
1712 if ((error = namei(&nd)) != 0)
1715 NDFREE(&nd, NDF_ONLY_PNBUF);
1716 if (nd.ni_vp == nd.ni_dvp)
1724 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1725 NDFREE(&nd, NDF_ONLY_PNBUF);
1727 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1732 vattr.va_mode = ACCESSPERMS &~ td->td_proc->p_pd->pd_cmask;
1734 vattr.va_type = VLNK;
1735 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1740 error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr, syspath);
1746 NDFREE(&nd, NDF_ONLY_PNBUF);
1748 vn_finished_write(mp);
1749 if (error == ERELOOKUP)
1752 if (segflg != UIO_SYSSPACE)
1753 uma_zfree(namei_zone, tmppath);
1758 * Delete a whiteout from the filesystem.
1760 #ifndef _SYS_SYSPROTO_H_
1761 struct undelete_args {
1766 sys_undelete(struct thread *td, struct undelete_args *uap)
1769 struct nameidata nd;
1774 NDINIT(&nd, DELETE, LOCKPARENT | DOWHITEOUT | AUDITVNODE1,
1775 UIO_USERSPACE, uap->path, td);
1780 if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & ISWHITEOUT)) {
1781 NDFREE(&nd, NDF_ONLY_PNBUF);
1782 if (nd.ni_vp == nd.ni_dvp)
1790 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1791 NDFREE(&nd, NDF_ONLY_PNBUF);
1793 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1797 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, DELETE);
1798 NDFREE(&nd, NDF_ONLY_PNBUF);
1800 vn_finished_write(mp);
1801 if (error == ERELOOKUP)
1807 * Delete a name from the filesystem.
1809 #ifndef _SYS_SYSPROTO_H_
1810 struct unlink_args {
1815 sys_unlink(struct thread *td, struct unlink_args *uap)
1818 return (kern_funlinkat(td, AT_FDCWD, uap->path, FD_NONE, UIO_USERSPACE,
1823 kern_funlinkat_ex(struct thread *td, int dfd, const char *path, int fd,
1824 int flag, enum uio_seg pathseg, ino_t oldinum)
1827 if ((flag & ~AT_REMOVEDIR) != 0)
1830 if ((flag & AT_REMOVEDIR) != 0)
1831 return (kern_frmdirat(td, dfd, path, fd, UIO_USERSPACE, 0));
1833 return (kern_funlinkat(td, dfd, path, fd, UIO_USERSPACE, 0, 0));
1836 #ifndef _SYS_SYSPROTO_H_
1837 struct unlinkat_args {
1844 sys_unlinkat(struct thread *td, struct unlinkat_args *uap)
1847 return (kern_funlinkat_ex(td, uap->fd, uap->path, FD_NONE, uap->flag,
1851 #ifndef _SYS_SYSPROTO_H_
1852 struct funlinkat_args {
1860 sys_funlinkat(struct thread *td, struct funlinkat_args *uap)
1863 return (kern_funlinkat_ex(td, uap->dfd, uap->path, uap->fd, uap->flag,
1868 kern_funlinkat(struct thread *td, int dfd, const char *path, int fd,
1869 enum uio_seg pathseg, int flag, ino_t oldinum)
1874 struct nameidata nd;
1879 if (fd != FD_NONE) {
1880 error = getvnode(td, fd, &cap_no_rights, &fp);
1887 NDINIT_ATRIGHTS(&nd, DELETE, LOCKPARENT | LOCKLEAF | AUDITVNODE1 |
1888 at2cnpflags(flag, AT_BENEATH | AT_RESOLVE_BENEATH),
1889 pathseg, path, dfd, &cap_unlinkat_rights, td);
1890 if ((error = namei(&nd)) != 0) {
1891 if (error == EINVAL)
1896 if (vp->v_type == VDIR && oldinum == 0) {
1897 error = EPERM; /* POSIX */
1898 } else if (oldinum != 0 &&
1899 ((error = VOP_STAT(vp, &sb, td->td_ucred, NOCRED, td)) == 0) &&
1900 sb.st_ino != oldinum) {
1901 error = EIDRM; /* Identifier removed */
1902 } else if (fp != NULL && fp->f_vnode != vp) {
1903 if (VN_IS_DOOMED(fp->f_vnode))
1909 * The root of a mounted filesystem cannot be deleted.
1911 * XXX: can this only be a VDIR case?
1913 if (vp->v_vflag & VV_ROOT)
1917 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1918 NDFREE(&nd, NDF_ONLY_PNBUF);
1920 if (vp == nd.ni_dvp)
1924 if ((error = vn_start_write(NULL, &mp,
1925 V_XSLEEP | PCATCH)) != 0) {
1931 error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp,
1936 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_UNLINK);
1937 error = VOP_REMOVE(nd.ni_dvp, vp, &nd.ni_cnd);
1941 vn_finished_write(mp);
1943 NDFREE(&nd, NDF_ONLY_PNBUF);
1945 if (vp == nd.ni_dvp)
1949 if (error == ERELOOKUP)
1958 * Reposition read/write file offset.
1960 #ifndef _SYS_SYSPROTO_H_
1969 sys_lseek(struct thread *td, struct lseek_args *uap)
1972 return (kern_lseek(td, uap->fd, uap->offset, uap->whence));
1976 kern_lseek(struct thread *td, int fd, off_t offset, int whence)
1982 error = fget(td, fd, &cap_seek_rights, &fp);
1985 error = (fp->f_ops->fo_flags & DFLAG_SEEKABLE) != 0 ?
1986 fo_seek(fp, offset, whence, td) : ESPIPE;
1991 #if defined(COMPAT_43)
1993 * Reposition read/write file offset.
1995 #ifndef _SYS_SYSPROTO_H_
1996 struct olseek_args {
2003 olseek(struct thread *td, struct olseek_args *uap)
2006 return (kern_lseek(td, uap->fd, uap->offset, uap->whence));
2008 #endif /* COMPAT_43 */
2010 #if defined(COMPAT_FREEBSD6)
2011 /* Version with the 'pad' argument */
2013 freebsd6_lseek(struct thread *td, struct freebsd6_lseek_args *uap)
2016 return (kern_lseek(td, uap->fd, uap->offset, uap->whence));
2021 * Check access permissions using passed credentials.
2024 vn_access(struct vnode *vp, int user_flags, struct ucred *cred,
2030 /* Flags == 0 means only check for existence. */
2031 if (user_flags == 0)
2035 if (user_flags & R_OK)
2037 if (user_flags & W_OK)
2039 if (user_flags & X_OK)
2042 error = mac_vnode_check_access(cred, vp, accmode);
2046 if ((accmode & VWRITE) == 0 || (error = vn_writechk(vp)) == 0)
2047 error = VOP_ACCESS(vp, accmode, cred, td);
2052 * Check access permissions using "real" credentials.
2054 #ifndef _SYS_SYSPROTO_H_
2055 struct access_args {
2061 sys_access(struct thread *td, struct access_args *uap)
2064 return (kern_accessat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2068 #ifndef _SYS_SYSPROTO_H_
2069 struct faccessat_args {
2077 sys_faccessat(struct thread *td, struct faccessat_args *uap)
2080 return (kern_accessat(td, uap->fd, uap->path, UIO_USERSPACE, uap->flag,
2085 kern_accessat(struct thread *td, int fd, const char *path,
2086 enum uio_seg pathseg, int flag, int amode)
2088 struct ucred *cred, *usecred;
2090 struct nameidata nd;
2093 if ((flag & ~(AT_EACCESS | AT_BENEATH | AT_RESOLVE_BENEATH)) != 0)
2095 if (amode != F_OK && (amode & ~(R_OK | W_OK | X_OK)) != 0)
2099 * Create and modify a temporary credential instead of one that
2100 * is potentially shared (if we need one).
2102 cred = td->td_ucred;
2103 if ((flag & AT_EACCESS) == 0 &&
2104 ((cred->cr_uid != cred->cr_ruid ||
2105 cred->cr_rgid != cred->cr_groups[0]))) {
2106 usecred = crdup(cred);
2107 usecred->cr_uid = cred->cr_ruid;
2108 usecred->cr_groups[0] = cred->cr_rgid;
2109 td->td_ucred = usecred;
2112 AUDIT_ARG_VALUE(amode);
2113 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF |
2114 AUDITVNODE1 | at2cnpflags(flag, AT_BENEATH | AT_RESOLVE_BENEATH),
2115 pathseg, path, fd, &cap_fstat_rights, td);
2116 if ((error = namei(&nd)) != 0)
2120 error = vn_access(vp, amode, usecred, td);
2121 NDFREE(&nd, NDF_ONLY_PNBUF);
2124 if (usecred != cred) {
2125 td->td_ucred = cred;
2132 * Check access permissions using "effective" credentials.
2134 #ifndef _SYS_SYSPROTO_H_
2135 struct eaccess_args {
2141 sys_eaccess(struct thread *td, struct eaccess_args *uap)
2144 return (kern_accessat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2145 AT_EACCESS, uap->amode));
2148 #if defined(COMPAT_43)
2150 * Get file status; this version follows links.
2152 #ifndef _SYS_SYSPROTO_H_
2159 ostat(struct thread *td, struct ostat_args *uap)
2165 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
2170 return (copyout(&osb, uap->ub, sizeof (osb)));
2174 * Get file status; this version does not follow links.
2176 #ifndef _SYS_SYSPROTO_H_
2177 struct olstat_args {
2183 olstat(struct thread *td, struct olstat_args *uap)
2189 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2190 UIO_USERSPACE, &sb, NULL);
2194 return (copyout(&osb, uap->ub, sizeof (osb)));
2198 * Convert from an old to a new stat structure.
2199 * XXX: many values are blindly truncated.
2202 cvtstat(struct stat *st, struct ostat *ost)
2205 bzero(ost, sizeof(*ost));
2206 ost->st_dev = st->st_dev;
2207 ost->st_ino = st->st_ino;
2208 ost->st_mode = st->st_mode;
2209 ost->st_nlink = st->st_nlink;
2210 ost->st_uid = st->st_uid;
2211 ost->st_gid = st->st_gid;
2212 ost->st_rdev = st->st_rdev;
2213 ost->st_size = MIN(st->st_size, INT32_MAX);
2214 ost->st_atim = st->st_atim;
2215 ost->st_mtim = st->st_mtim;
2216 ost->st_ctim = st->st_ctim;
2217 ost->st_blksize = st->st_blksize;
2218 ost->st_blocks = st->st_blocks;
2219 ost->st_flags = st->st_flags;
2220 ost->st_gen = st->st_gen;
2222 #endif /* COMPAT_43 */
2224 #if defined(COMPAT_43) || defined(COMPAT_FREEBSD11)
2225 int ino64_trunc_error;
2226 SYSCTL_INT(_vfs, OID_AUTO, ino64_trunc_error, CTLFLAG_RW,
2227 &ino64_trunc_error, 0,
2228 "Error on truncation of device, file or inode number, or link count");
2231 freebsd11_cvtstat(struct stat *st, struct freebsd11_stat *ost)
2234 ost->st_dev = st->st_dev;
2235 if (ost->st_dev != st->st_dev) {
2236 switch (ino64_trunc_error) {
2239 * Since dev_t is almost raw, don't clamp to the
2240 * maximum for case 2, but ignore the error.
2247 ost->st_ino = st->st_ino;
2248 if (ost->st_ino != st->st_ino) {
2249 switch (ino64_trunc_error) {
2256 ost->st_ino = UINT32_MAX;
2260 ost->st_mode = st->st_mode;
2261 ost->st_nlink = st->st_nlink;
2262 if (ost->st_nlink != st->st_nlink) {
2263 switch (ino64_trunc_error) {
2270 ost->st_nlink = UINT16_MAX;
2274 ost->st_uid = st->st_uid;
2275 ost->st_gid = st->st_gid;
2276 ost->st_rdev = st->st_rdev;
2277 if (ost->st_rdev != st->st_rdev) {
2278 switch (ino64_trunc_error) {
2285 ost->st_atim = st->st_atim;
2286 ost->st_mtim = st->st_mtim;
2287 ost->st_ctim = st->st_ctim;
2288 ost->st_size = st->st_size;
2289 ost->st_blocks = st->st_blocks;
2290 ost->st_blksize = st->st_blksize;
2291 ost->st_flags = st->st_flags;
2292 ost->st_gen = st->st_gen;
2294 ost->st_birthtim = st->st_birthtim;
2295 bzero((char *)&ost->st_birthtim + sizeof(ost->st_birthtim),
2296 sizeof(*ost) - offsetof(struct freebsd11_stat,
2297 st_birthtim) - sizeof(ost->st_birthtim));
2302 freebsd11_stat(struct thread *td, struct freebsd11_stat_args* uap)
2305 struct freebsd11_stat osb;
2308 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
2312 error = freebsd11_cvtstat(&sb, &osb);
2314 error = copyout(&osb, uap->ub, sizeof(osb));
2319 freebsd11_lstat(struct thread *td, struct freebsd11_lstat_args* uap)
2322 struct freebsd11_stat osb;
2325 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2326 UIO_USERSPACE, &sb, NULL);
2329 error = freebsd11_cvtstat(&sb, &osb);
2331 error = copyout(&osb, uap->ub, sizeof(osb));
2336 freebsd11_fhstat(struct thread *td, struct freebsd11_fhstat_args* uap)
2340 struct freebsd11_stat osb;
2343 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
2346 error = kern_fhstat(td, fh, &sb);
2349 error = freebsd11_cvtstat(&sb, &osb);
2351 error = copyout(&osb, uap->sb, sizeof(osb));
2356 freebsd11_fstatat(struct thread *td, struct freebsd11_fstatat_args* uap)
2359 struct freebsd11_stat osb;
2362 error = kern_statat(td, uap->flag, uap->fd, uap->path,
2363 UIO_USERSPACE, &sb, NULL);
2366 error = freebsd11_cvtstat(&sb, &osb);
2368 error = copyout(&osb, uap->buf, sizeof(osb));
2371 #endif /* COMPAT_FREEBSD11 */
2376 #ifndef _SYS_SYSPROTO_H_
2377 struct fstatat_args {
2385 sys_fstatat(struct thread *td, struct fstatat_args *uap)
2390 error = kern_statat(td, uap->flag, uap->fd, uap->path,
2391 UIO_USERSPACE, &sb, NULL);
2393 error = copyout(&sb, uap->buf, sizeof (sb));
2398 kern_statat(struct thread *td, int flag, int fd, const char *path,
2399 enum uio_seg pathseg, struct stat *sbp,
2400 void (*hook)(struct vnode *vp, struct stat *sbp))
2402 struct nameidata nd;
2405 if ((flag & ~(AT_SYMLINK_NOFOLLOW | AT_BENEATH |
2406 AT_RESOLVE_BENEATH)) != 0)
2409 NDINIT_ATRIGHTS(&nd, LOOKUP, at2cnpflags(flag, AT_BENEATH |
2410 AT_RESOLVE_BENEATH | AT_SYMLINK_NOFOLLOW) | LOCKSHARED | LOCKLEAF |
2411 AUDITVNODE1, pathseg, path, fd, &cap_fstat_rights, td);
2413 if ((error = namei(&nd)) != 0)
2415 error = VOP_STAT(nd.ni_vp, sbp, td->td_ucred, NOCRED, td);
2417 if (__predict_false(hook != NULL))
2418 hook(nd.ni_vp, sbp);
2420 NDFREE(&nd, NDF_ONLY_PNBUF);
2422 #ifdef __STAT_TIME_T_EXT
2423 sbp->st_atim_ext = 0;
2424 sbp->st_mtim_ext = 0;
2425 sbp->st_ctim_ext = 0;
2426 sbp->st_btim_ext = 0;
2429 if (KTRPOINT(td, KTR_STRUCT))
2430 ktrstat_error(sbp, error);
2435 #if defined(COMPAT_FREEBSD11)
2437 * Implementation of the NetBSD [l]stat() functions.
2440 freebsd11_cvtnstat(struct stat *sb, struct nstat *nsb)
2443 bzero(nsb, sizeof(*nsb));
2444 nsb->st_dev = sb->st_dev;
2445 nsb->st_ino = sb->st_ino;
2446 nsb->st_mode = sb->st_mode;
2447 nsb->st_nlink = sb->st_nlink;
2448 nsb->st_uid = sb->st_uid;
2449 nsb->st_gid = sb->st_gid;
2450 nsb->st_rdev = sb->st_rdev;
2451 nsb->st_atim = sb->st_atim;
2452 nsb->st_mtim = sb->st_mtim;
2453 nsb->st_ctim = sb->st_ctim;
2454 nsb->st_size = sb->st_size;
2455 nsb->st_blocks = sb->st_blocks;
2456 nsb->st_blksize = sb->st_blksize;
2457 nsb->st_flags = sb->st_flags;
2458 nsb->st_gen = sb->st_gen;
2459 nsb->st_birthtim = sb->st_birthtim;
2462 #ifndef _SYS_SYSPROTO_H_
2463 struct freebsd11_nstat_args {
2469 freebsd11_nstat(struct thread *td, struct freebsd11_nstat_args *uap)
2475 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
2479 freebsd11_cvtnstat(&sb, &nsb);
2480 return (copyout(&nsb, uap->ub, sizeof (nsb)));
2484 * NetBSD lstat. Get file status; this version does not follow links.
2486 #ifndef _SYS_SYSPROTO_H_
2487 struct freebsd11_nlstat_args {
2493 freebsd11_nlstat(struct thread *td, struct freebsd11_nlstat_args *uap)
2499 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2500 UIO_USERSPACE, &sb, NULL);
2503 freebsd11_cvtnstat(&sb, &nsb);
2504 return (copyout(&nsb, uap->ub, sizeof (nsb)));
2506 #endif /* COMPAT_FREEBSD11 */
2509 * Get configurable pathname variables.
2511 #ifndef _SYS_SYSPROTO_H_
2512 struct pathconf_args {
2518 sys_pathconf(struct thread *td, struct pathconf_args *uap)
2523 error = kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name, FOLLOW,
2526 td->td_retval[0] = value;
2530 #ifndef _SYS_SYSPROTO_H_
2531 struct lpathconf_args {
2537 sys_lpathconf(struct thread *td, struct lpathconf_args *uap)
2542 error = kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name,
2545 td->td_retval[0] = value;
2550 kern_pathconf(struct thread *td, const char *path, enum uio_seg pathseg,
2551 int name, u_long flags, long *valuep)
2553 struct nameidata nd;
2556 NDINIT(&nd, LOOKUP, LOCKSHARED | LOCKLEAF | AUDITVNODE1 | flags,
2558 if ((error = namei(&nd)) != 0)
2560 NDFREE(&nd, NDF_ONLY_PNBUF);
2562 error = VOP_PATHCONF(nd.ni_vp, name, valuep);
2568 * Return target name of a symbolic link.
2570 #ifndef _SYS_SYSPROTO_H_
2571 struct readlink_args {
2578 sys_readlink(struct thread *td, struct readlink_args *uap)
2581 return (kern_readlinkat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2582 uap->buf, UIO_USERSPACE, uap->count));
2584 #ifndef _SYS_SYSPROTO_H_
2585 struct readlinkat_args {
2593 sys_readlinkat(struct thread *td, struct readlinkat_args *uap)
2596 return (kern_readlinkat(td, uap->fd, uap->path, UIO_USERSPACE,
2597 uap->buf, UIO_USERSPACE, uap->bufsize));
2601 kern_readlinkat(struct thread *td, int fd, const char *path,
2602 enum uio_seg pathseg, char *buf, enum uio_seg bufseg, size_t count)
2605 struct nameidata nd;
2608 if (count > IOSIZE_MAX)
2611 NDINIT_AT(&nd, LOOKUP, NOFOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
2612 pathseg, path, fd, td);
2614 if ((error = namei(&nd)) != 0)
2616 NDFREE(&nd, NDF_ONLY_PNBUF);
2619 error = kern_readlink_vp(vp, buf, bufseg, count, td);
2626 * Helper function to readlink from a vnode
2629 kern_readlink_vp(struct vnode *vp, char *buf, enum uio_seg bufseg, size_t count,
2636 ASSERT_VOP_LOCKED(vp, "kern_readlink_vp(): vp not locked");
2638 error = mac_vnode_check_readlink(td->td_ucred, vp);
2642 if (vp->v_type != VLNK && (vp->v_vflag & VV_READLINK) == 0)
2645 aiov.iov_base = buf;
2646 aiov.iov_len = count;
2647 auio.uio_iov = &aiov;
2648 auio.uio_iovcnt = 1;
2649 auio.uio_offset = 0;
2650 auio.uio_rw = UIO_READ;
2651 auio.uio_segflg = bufseg;
2653 auio.uio_resid = count;
2654 error = VOP_READLINK(vp, &auio, td->td_ucred);
2655 td->td_retval[0] = count - auio.uio_resid;
2660 * Common implementation code for chflags() and fchflags().
2663 setfflags(struct thread *td, struct vnode *vp, u_long flags)
2669 /* We can't support the value matching VNOVAL. */
2670 if (flags == VNOVAL)
2671 return (EOPNOTSUPP);
2674 * Prevent non-root users from setting flags on devices. When
2675 * a device is reused, users can retain ownership of the device
2676 * if they are allowed to set flags and programs assume that
2677 * chown can't fail when done as root.
2679 if (vp->v_type == VCHR || vp->v_type == VBLK) {
2680 error = priv_check(td, PRIV_VFS_CHFLAGS_DEV);
2685 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2688 vattr.va_flags = flags;
2689 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2691 error = mac_vnode_check_setflags(td->td_ucred, vp, vattr.va_flags);
2694 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
2696 vn_finished_write(mp);
2701 * Change flags of a file given a path name.
2703 #ifndef _SYS_SYSPROTO_H_
2704 struct chflags_args {
2710 sys_chflags(struct thread *td, struct chflags_args *uap)
2713 return (kern_chflagsat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2717 #ifndef _SYS_SYSPROTO_H_
2718 struct chflagsat_args {
2726 sys_chflagsat(struct thread *td, struct chflagsat_args *uap)
2729 if ((uap->atflag & ~(AT_SYMLINK_NOFOLLOW | AT_BENEATH |
2730 AT_RESOLVE_BENEATH)) != 0)
2733 return (kern_chflagsat(td, uap->fd, uap->path, UIO_USERSPACE,
2734 uap->flags, uap->atflag));
2738 * Same as chflags() but doesn't follow symlinks.
2740 #ifndef _SYS_SYSPROTO_H_
2741 struct lchflags_args {
2747 sys_lchflags(struct thread *td, struct lchflags_args *uap)
2750 return (kern_chflagsat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2751 uap->flags, AT_SYMLINK_NOFOLLOW));
2755 kern_chflagsat(struct thread *td, int fd, const char *path,
2756 enum uio_seg pathseg, u_long flags, int atflag)
2758 struct nameidata nd;
2761 AUDIT_ARG_FFLAGS(flags);
2762 NDINIT_ATRIGHTS(&nd, LOOKUP, at2cnpflags(atflag, AT_SYMLINK_NOFOLLOW |
2763 AT_BENEATH | AT_RESOLVE_BENEATH) | AUDITVNODE1, pathseg, path, fd,
2764 &cap_fchflags_rights, td);
2765 if ((error = namei(&nd)) != 0)
2767 NDFREE(&nd, NDF_ONLY_PNBUF);
2768 error = setfflags(td, nd.ni_vp, flags);
2774 * Change flags of a file given a file descriptor.
2776 #ifndef _SYS_SYSPROTO_H_
2777 struct fchflags_args {
2783 sys_fchflags(struct thread *td, struct fchflags_args *uap)
2788 AUDIT_ARG_FD(uap->fd);
2789 AUDIT_ARG_FFLAGS(uap->flags);
2790 error = getvnode(td, uap->fd, &cap_fchflags_rights,
2795 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
2796 AUDIT_ARG_VNODE1(fp->f_vnode);
2797 VOP_UNLOCK(fp->f_vnode);
2799 error = setfflags(td, fp->f_vnode, uap->flags);
2805 * Common implementation code for chmod(), lchmod() and fchmod().
2808 setfmode(struct thread *td, struct ucred *cred, struct vnode *vp, int mode)
2814 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2816 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2818 vattr.va_mode = mode & ALLPERMS;
2820 error = mac_vnode_check_setmode(cred, vp, vattr.va_mode);
2823 error = VOP_SETATTR(vp, &vattr, cred);
2825 vn_finished_write(mp);
2830 * Change mode of a file given path name.
2832 #ifndef _SYS_SYSPROTO_H_
2839 sys_chmod(struct thread *td, struct chmod_args *uap)
2842 return (kern_fchmodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2846 #ifndef _SYS_SYSPROTO_H_
2847 struct fchmodat_args {
2855 sys_fchmodat(struct thread *td, struct fchmodat_args *uap)
2858 if ((uap->flag & ~(AT_SYMLINK_NOFOLLOW | AT_BENEATH |
2859 AT_RESOLVE_BENEATH)) != 0)
2862 return (kern_fchmodat(td, uap->fd, uap->path, UIO_USERSPACE,
2863 uap->mode, uap->flag));
2867 * Change mode of a file given path name (don't follow links.)
2869 #ifndef _SYS_SYSPROTO_H_
2870 struct lchmod_args {
2876 sys_lchmod(struct thread *td, struct lchmod_args *uap)
2879 return (kern_fchmodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2880 uap->mode, AT_SYMLINK_NOFOLLOW));
2884 kern_fchmodat(struct thread *td, int fd, const char *path,
2885 enum uio_seg pathseg, mode_t mode, int flag)
2887 struct nameidata nd;
2890 AUDIT_ARG_MODE(mode);
2891 NDINIT_ATRIGHTS(&nd, LOOKUP, at2cnpflags(flag, AT_SYMLINK_NOFOLLOW |
2892 AT_BENEATH | AT_RESOLVE_BENEATH) | AUDITVNODE1, pathseg, path, fd,
2893 &cap_fchmod_rights, td);
2894 if ((error = namei(&nd)) != 0)
2896 NDFREE(&nd, NDF_ONLY_PNBUF);
2897 error = setfmode(td, td->td_ucred, nd.ni_vp, mode);
2903 * Change mode of a file given a file descriptor.
2905 #ifndef _SYS_SYSPROTO_H_
2906 struct fchmod_args {
2912 sys_fchmod(struct thread *td, struct fchmod_args *uap)
2917 AUDIT_ARG_FD(uap->fd);
2918 AUDIT_ARG_MODE(uap->mode);
2920 error = fget(td, uap->fd, &cap_fchmod_rights, &fp);
2923 error = fo_chmod(fp, uap->mode, td->td_ucred, td);
2929 * Common implementation for chown(), lchown(), and fchown()
2932 setfown(struct thread *td, struct ucred *cred, struct vnode *vp, uid_t uid,
2939 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2941 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2946 error = mac_vnode_check_setowner(cred, vp, vattr.va_uid,
2950 error = VOP_SETATTR(vp, &vattr, cred);
2952 vn_finished_write(mp);
2957 * Set ownership given a path name.
2959 #ifndef _SYS_SYSPROTO_H_
2967 sys_chown(struct thread *td, struct chown_args *uap)
2970 return (kern_fchownat(td, AT_FDCWD, uap->path, UIO_USERSPACE, uap->uid,
2974 #ifndef _SYS_SYSPROTO_H_
2975 struct fchownat_args {
2984 sys_fchownat(struct thread *td, struct fchownat_args *uap)
2987 if ((uap->flag & ~(AT_SYMLINK_NOFOLLOW | AT_BENEATH |
2988 AT_RESOLVE_BENEATH)) != 0)
2991 return (kern_fchownat(td, uap->fd, uap->path, UIO_USERSPACE, uap->uid,
2992 uap->gid, uap->flag));
2996 kern_fchownat(struct thread *td, int fd, const char *path,
2997 enum uio_seg pathseg, int uid, int gid, int flag)
2999 struct nameidata nd;
3002 AUDIT_ARG_OWNER(uid, gid);
3003 NDINIT_ATRIGHTS(&nd, LOOKUP, at2cnpflags(flag, AT_SYMLINK_NOFOLLOW |
3004 AT_BENEATH | AT_RESOLVE_BENEATH) | AUDITVNODE1, pathseg, path, fd,
3005 &cap_fchown_rights, td);
3007 if ((error = namei(&nd)) != 0)
3009 NDFREE(&nd, NDF_ONLY_PNBUF);
3010 error = setfown(td, td->td_ucred, nd.ni_vp, uid, gid);
3016 * Set ownership given a path name, do not cross symlinks.
3018 #ifndef _SYS_SYSPROTO_H_
3019 struct lchown_args {
3026 sys_lchown(struct thread *td, struct lchown_args *uap)
3029 return (kern_fchownat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
3030 uap->uid, uap->gid, AT_SYMLINK_NOFOLLOW));
3034 * Set ownership given a file descriptor.
3036 #ifndef _SYS_SYSPROTO_H_
3037 struct fchown_args {
3044 sys_fchown(struct thread *td, struct fchown_args *uap)
3049 AUDIT_ARG_FD(uap->fd);
3050 AUDIT_ARG_OWNER(uap->uid, uap->gid);
3051 error = fget(td, uap->fd, &cap_fchown_rights, &fp);
3054 error = fo_chown(fp, uap->uid, uap->gid, td->td_ucred, td);
3060 * Common implementation code for utimes(), lutimes(), and futimes().
3063 getutimes(const struct timeval *usrtvp, enum uio_seg tvpseg,
3064 struct timespec *tsp)
3066 struct timeval tv[2];
3067 const struct timeval *tvp;
3070 if (usrtvp == NULL) {
3071 vfs_timestamp(&tsp[0]);
3074 if (tvpseg == UIO_SYSSPACE) {
3077 if ((error = copyin(usrtvp, tv, sizeof(tv))) != 0)
3082 if (tvp[0].tv_usec < 0 || tvp[0].tv_usec >= 1000000 ||
3083 tvp[1].tv_usec < 0 || tvp[1].tv_usec >= 1000000)
3085 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
3086 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
3092 * Common implementation code for futimens(), utimensat().
3094 #define UTIMENS_NULL 0x1
3095 #define UTIMENS_EXIT 0x2
3097 getutimens(const struct timespec *usrtsp, enum uio_seg tspseg,
3098 struct timespec *tsp, int *retflags)
3100 struct timespec tsnow;
3103 vfs_timestamp(&tsnow);
3105 if (usrtsp == NULL) {
3108 *retflags |= UTIMENS_NULL;
3111 if (tspseg == UIO_SYSSPACE) {
3114 } else if ((error = copyin(usrtsp, tsp, sizeof(*tsp) * 2)) != 0)
3116 if (tsp[0].tv_nsec == UTIME_OMIT && tsp[1].tv_nsec == UTIME_OMIT)
3117 *retflags |= UTIMENS_EXIT;
3118 if (tsp[0].tv_nsec == UTIME_NOW && tsp[1].tv_nsec == UTIME_NOW)
3119 *retflags |= UTIMENS_NULL;
3120 if (tsp[0].tv_nsec == UTIME_OMIT)
3121 tsp[0].tv_sec = VNOVAL;
3122 else if (tsp[0].tv_nsec == UTIME_NOW)
3124 else if (tsp[0].tv_nsec < 0 || tsp[0].tv_nsec >= 1000000000L)
3126 if (tsp[1].tv_nsec == UTIME_OMIT)
3127 tsp[1].tv_sec = VNOVAL;
3128 else if (tsp[1].tv_nsec == UTIME_NOW)
3130 else if (tsp[1].tv_nsec < 0 || tsp[1].tv_nsec >= 1000000000L)
3137 * Common implementation code for utimes(), lutimes(), futimes(), futimens(),
3141 setutimes(struct thread *td, struct vnode *vp, const struct timespec *ts,
3142 int numtimes, int nullflag)
3146 int error, setbirthtime;
3148 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
3150 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3152 if (numtimes < 3 && !VOP_GETATTR(vp, &vattr, td->td_ucred) &&
3153 timespeccmp(&ts[1], &vattr.va_birthtime, < ))
3156 vattr.va_atime = ts[0];
3157 vattr.va_mtime = ts[1];
3159 vattr.va_birthtime = ts[1];
3161 vattr.va_birthtime = ts[2];
3163 vattr.va_vaflags |= VA_UTIMES_NULL;
3165 error = mac_vnode_check_setutimes(td->td_ucred, vp, vattr.va_atime,
3169 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3171 vn_finished_write(mp);
3176 * Set the access and modification times of a file.
3178 #ifndef _SYS_SYSPROTO_H_
3179 struct utimes_args {
3181 struct timeval *tptr;
3185 sys_utimes(struct thread *td, struct utimes_args *uap)
3188 return (kern_utimesat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
3189 uap->tptr, UIO_USERSPACE));
3192 #ifndef _SYS_SYSPROTO_H_
3193 struct futimesat_args {
3196 const struct timeval * times;
3200 sys_futimesat(struct thread *td, struct futimesat_args *uap)
3203 return (kern_utimesat(td, uap->fd, uap->path, UIO_USERSPACE,
3204 uap->times, UIO_USERSPACE));
3208 kern_utimesat(struct thread *td, int fd, const char *path,
3209 enum uio_seg pathseg, struct timeval *tptr, enum uio_seg tptrseg)
3211 struct nameidata nd;
3212 struct timespec ts[2];
3215 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3217 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, fd,
3218 &cap_futimes_rights, td);
3220 if ((error = namei(&nd)) != 0)
3222 NDFREE(&nd, NDF_ONLY_PNBUF);
3223 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3229 * Set the access and modification times of a file.
3231 #ifndef _SYS_SYSPROTO_H_
3232 struct lutimes_args {
3234 struct timeval *tptr;
3238 sys_lutimes(struct thread *td, struct lutimes_args *uap)
3241 return (kern_lutimes(td, uap->path, UIO_USERSPACE, uap->tptr,
3246 kern_lutimes(struct thread *td, const char *path, enum uio_seg pathseg,
3247 struct timeval *tptr, enum uio_seg tptrseg)
3249 struct timespec ts[2];
3250 struct nameidata nd;
3253 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3255 NDINIT(&nd, LOOKUP, NOFOLLOW | AUDITVNODE1, pathseg, path, td);
3256 if ((error = namei(&nd)) != 0)
3258 NDFREE(&nd, NDF_ONLY_PNBUF);
3259 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3265 * Set the access and modification times of a file.
3267 #ifndef _SYS_SYSPROTO_H_
3268 struct futimes_args {
3270 struct timeval *tptr;
3274 sys_futimes(struct thread *td, struct futimes_args *uap)
3277 return (kern_futimes(td, uap->fd, uap->tptr, UIO_USERSPACE));
3281 kern_futimes(struct thread *td, int fd, struct timeval *tptr,
3282 enum uio_seg tptrseg)
3284 struct timespec ts[2];
3289 error = getutimes(tptr, tptrseg, ts);
3292 error = getvnode(td, fd, &cap_futimes_rights, &fp);
3296 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
3297 AUDIT_ARG_VNODE1(fp->f_vnode);
3298 VOP_UNLOCK(fp->f_vnode);
3300 error = setutimes(td, fp->f_vnode, ts, 2, tptr == NULL);
3306 sys_futimens(struct thread *td, struct futimens_args *uap)
3309 return (kern_futimens(td, uap->fd, uap->times, UIO_USERSPACE));
3313 kern_futimens(struct thread *td, int fd, struct timespec *tptr,
3314 enum uio_seg tptrseg)
3316 struct timespec ts[2];
3321 error = getutimens(tptr, tptrseg, ts, &flags);
3324 if (flags & UTIMENS_EXIT)
3326 error = getvnode(td, fd, &cap_futimes_rights, &fp);
3330 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
3331 AUDIT_ARG_VNODE1(fp->f_vnode);
3332 VOP_UNLOCK(fp->f_vnode);
3334 error = setutimes(td, fp->f_vnode, ts, 2, flags & UTIMENS_NULL);
3340 sys_utimensat(struct thread *td, struct utimensat_args *uap)
3343 return (kern_utimensat(td, uap->fd, uap->path, UIO_USERSPACE,
3344 uap->times, UIO_USERSPACE, uap->flag));
3348 kern_utimensat(struct thread *td, int fd, const char *path,
3349 enum uio_seg pathseg, struct timespec *tptr, enum uio_seg tptrseg,
3352 struct nameidata nd;
3353 struct timespec ts[2];
3356 if ((flag & ~(AT_SYMLINK_NOFOLLOW | AT_BENEATH |
3357 AT_RESOLVE_BENEATH)) != 0)
3360 if ((error = getutimens(tptr, tptrseg, ts, &flags)) != 0)
3362 NDINIT_ATRIGHTS(&nd, LOOKUP, at2cnpflags(flag, AT_SYMLINK_NOFOLLOW |
3363 AT_BENEATH | AT_RESOLVE_BENEATH) | AUDITVNODE1,
3364 pathseg, path, fd, &cap_futimes_rights, td);
3365 if ((error = namei(&nd)) != 0)
3368 * We are allowed to call namei() regardless of 2xUTIME_OMIT.
3370 * "If both tv_nsec fields are UTIME_OMIT... EACCESS may be detected."
3371 * "Search permission is denied by a component of the path prefix."
3373 NDFREE(&nd, NDF_ONLY_PNBUF);
3374 if ((flags & UTIMENS_EXIT) == 0)
3375 error = setutimes(td, nd.ni_vp, ts, 2, flags & UTIMENS_NULL);
3381 * Truncate a file given its path name.
3383 #ifndef _SYS_SYSPROTO_H_
3384 struct truncate_args {
3391 sys_truncate(struct thread *td, struct truncate_args *uap)
3394 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3398 kern_truncate(struct thread *td, const char *path, enum uio_seg pathseg,
3405 struct nameidata nd;
3411 NDINIT(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, td);
3412 if ((error = namei(&nd)) != 0)
3415 rl_cookie = vn_rangelock_wlock(vp, 0, OFF_MAX);
3416 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0) {
3417 vn_rangelock_unlock(vp, rl_cookie);
3421 NDFREE(&nd, NDF_ONLY_PNBUF);
3422 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3423 if (vp->v_type == VDIR)
3426 else if ((error = mac_vnode_check_write(td->td_ucred, NOCRED, vp))) {
3429 else if ((error = vn_writechk(vp)) == 0 &&
3430 (error = VOP_ACCESS(vp, VWRITE, td->td_ucred, td)) == 0) {
3432 vattr.va_size = length;
3433 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3436 vn_finished_write(mp);
3437 vn_rangelock_unlock(vp, rl_cookie);
3439 if (error == ERELOOKUP)
3444 #if defined(COMPAT_43)
3446 * Truncate a file given its path name.
3448 #ifndef _SYS_SYSPROTO_H_
3449 struct otruncate_args {
3455 otruncate(struct thread *td, struct otruncate_args *uap)
3458 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3460 #endif /* COMPAT_43 */
3462 #if defined(COMPAT_FREEBSD6)
3463 /* Versions with the pad argument */
3465 freebsd6_truncate(struct thread *td, struct freebsd6_truncate_args *uap)
3468 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3472 freebsd6_ftruncate(struct thread *td, struct freebsd6_ftruncate_args *uap)
3475 return (kern_ftruncate(td, uap->fd, uap->length));
3480 kern_fsync(struct thread *td, int fd, bool fullsync)
3485 int error, lock_flags;
3488 error = getvnode(td, fd, &cap_fsync_rights, &fp);
3494 /* XXXKIB: compete outstanding aio writes */;
3497 error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
3500 if (MNT_SHARED_WRITES(mp) ||
3501 ((mp == NULL) && MNT_SHARED_WRITES(vp->v_mount))) {
3502 lock_flags = LK_SHARED;
3504 lock_flags = LK_EXCLUSIVE;
3506 vn_lock(vp, lock_flags | LK_RETRY);
3507 AUDIT_ARG_VNODE1(vp);
3508 if (vp->v_object != NULL) {
3509 VM_OBJECT_WLOCK(vp->v_object);
3510 vm_object_page_clean(vp->v_object, 0, 0, 0);
3511 VM_OBJECT_WUNLOCK(vp->v_object);
3513 error = fullsync ? VOP_FSYNC(vp, MNT_WAIT, td) : VOP_FDATASYNC(vp, td);
3515 vn_finished_write(mp);
3516 if (error == ERELOOKUP)
3524 * Sync an open file.
3526 #ifndef _SYS_SYSPROTO_H_
3532 sys_fsync(struct thread *td, struct fsync_args *uap)
3535 return (kern_fsync(td, uap->fd, true));
3539 sys_fdatasync(struct thread *td, struct fdatasync_args *uap)
3542 return (kern_fsync(td, uap->fd, false));
3546 * Rename files. Source and destination must either both be directories, or
3547 * both not be directories. If target is a directory, it must be empty.
3549 #ifndef _SYS_SYSPROTO_H_
3550 struct rename_args {
3556 sys_rename(struct thread *td, struct rename_args *uap)
3559 return (kern_renameat(td, AT_FDCWD, uap->from, AT_FDCWD,
3560 uap->to, UIO_USERSPACE));
3563 #ifndef _SYS_SYSPROTO_H_
3564 struct renameat_args {
3572 sys_renameat(struct thread *td, struct renameat_args *uap)
3575 return (kern_renameat(td, uap->oldfd, uap->old, uap->newfd, uap->new,
3581 kern_renameat_mac(struct thread *td, int oldfd, const char *old, int newfd,
3582 const char *new, enum uio_seg pathseg, struct nameidata *fromnd)
3586 NDINIT_ATRIGHTS(fromnd, DELETE, LOCKPARENT | LOCKLEAF | SAVESTART |
3587 AUDITVNODE1, pathseg, old, oldfd, &cap_renameat_source_rights, td);
3588 if ((error = namei(fromnd)) != 0)
3590 error = mac_vnode_check_rename_from(td->td_ucred, fromnd->ni_dvp,
3591 fromnd->ni_vp, &fromnd->ni_cnd);
3592 VOP_UNLOCK(fromnd->ni_dvp);
3593 if (fromnd->ni_dvp != fromnd->ni_vp)
3594 VOP_UNLOCK(fromnd->ni_vp);
3596 NDFREE(fromnd, NDF_ONLY_PNBUF);
3597 vrele(fromnd->ni_dvp);
3598 vrele(fromnd->ni_vp);
3599 if (fromnd->ni_startdir)
3600 vrele(fromnd->ni_startdir);
3607 kern_renameat(struct thread *td, int oldfd, const char *old, int newfd,
3608 const char *new, enum uio_seg pathseg)
3610 struct mount *mp = NULL;
3611 struct vnode *tvp, *fvp, *tdvp;
3612 struct nameidata fromnd, tond;
3618 if (mac_vnode_check_rename_from_enabled()) {
3619 error = kern_renameat_mac(td, oldfd, old, newfd, new, pathseg,
3625 NDINIT_ATRIGHTS(&fromnd, DELETE, WANTPARENT | SAVESTART | AUDITVNODE1,
3626 pathseg, old, oldfd, &cap_renameat_source_rights, td);
3627 if ((error = namei(&fromnd)) != 0)
3633 NDINIT_ATRIGHTS(&tond, RENAME, LOCKPARENT | LOCKLEAF | NOCACHE |
3634 SAVESTART | AUDITVNODE2, pathseg, new, newfd,
3635 &cap_renameat_target_rights, td);
3636 if (fromnd.ni_vp->v_type == VDIR)
3637 tond.ni_cnd.cn_flags |= WILLBEDIR;
3638 if ((error = namei(&tond)) != 0) {
3639 /* Translate error code for rename("dir1", "dir2/."). */
3640 if (error == EISDIR && fvp->v_type == VDIR)
3642 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3643 vrele(fromnd.ni_dvp);
3649 error = vn_start_write(fvp, &mp, V_NOWAIT);
3651 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3652 NDFREE(&tond, NDF_ONLY_PNBUF);
3659 vrele(fromnd.ni_dvp);
3661 vrele(tond.ni_startdir);
3662 if (fromnd.ni_startdir != NULL)
3663 vrele(fromnd.ni_startdir);
3664 error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH);
3670 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
3673 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
3678 if (newfd != AT_FDCWD && (tond.ni_resflags & NIRES_ABS) == 0) {
3680 * If the target already exists we require CAP_UNLINKAT
3681 * from 'newfd', when newfd was used for the lookup.
3683 error = cap_check(&tond.ni_filecaps.fc_rights,
3684 &cap_unlinkat_rights);
3695 * If the source is the same as the destination (that is, if they
3696 * are links to the same vnode), then there is nothing to do.
3702 error = mac_vnode_check_rename_to(td->td_ucred, tdvp,
3703 tond.ni_vp, fromnd.ni_dvp == tdvp, &tond.ni_cnd);
3707 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
3708 tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
3709 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3710 NDFREE(&tond, NDF_ONLY_PNBUF);
3712 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3713 NDFREE(&tond, NDF_ONLY_PNBUF);
3720 vrele(fromnd.ni_dvp);
3723 vrele(tond.ni_startdir);
3724 vn_finished_write(mp);
3726 if (fromnd.ni_startdir)
3727 vrele(fromnd.ni_startdir);
3728 if (error == ERESTART)
3730 if (error == ERELOOKUP)
3736 * Make a directory file.
3738 #ifndef _SYS_SYSPROTO_H_
3745 sys_mkdir(struct thread *td, struct mkdir_args *uap)
3748 return (kern_mkdirat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
3752 #ifndef _SYS_SYSPROTO_H_
3753 struct mkdirat_args {
3760 sys_mkdirat(struct thread *td, struct mkdirat_args *uap)
3763 return (kern_mkdirat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode));
3767 kern_mkdirat(struct thread *td, int fd, const char *path, enum uio_seg segflg,
3773 struct nameidata nd;
3776 AUDIT_ARG_MODE(mode);
3779 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
3780 NC_NOMAKEENTRY | NC_KEEPPOSENTRY, segflg, path, fd,
3781 &cap_mkdirat_rights, td);
3782 nd.ni_cnd.cn_flags |= WILLBEDIR;
3783 if ((error = namei(&nd)) != 0)
3787 NDFREE(&nd, NDF_ONLY_PNBUF);
3789 * XXX namei called with LOCKPARENT but not LOCKLEAF has
3790 * the strange behaviour of leaving the vnode unlocked
3791 * if the target is the same vnode as the parent.
3793 if (vp == nd.ni_dvp)
3800 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3801 NDFREE(&nd, NDF_ONLY_PNBUF);
3803 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3808 vattr.va_type = VDIR;
3809 vattr.va_mode = (mode & ACCESSPERMS) &~ td->td_proc->p_pd->pd_cmask;
3811 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
3816 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
3820 NDFREE(&nd, NDF_ONLY_PNBUF);
3824 vn_finished_write(mp);
3825 if (error == ERELOOKUP)
3831 * Remove a directory file.
3833 #ifndef _SYS_SYSPROTO_H_
3839 sys_rmdir(struct thread *td, struct rmdir_args *uap)
3842 return (kern_frmdirat(td, AT_FDCWD, uap->path, FD_NONE, UIO_USERSPACE,
3847 kern_frmdirat(struct thread *td, int dfd, const char *path, int fd,
3848 enum uio_seg pathseg, int flag)
3853 struct nameidata nd;
3854 cap_rights_t rights;
3858 if (fd != FD_NONE) {
3859 error = getvnode(td, fd, cap_rights_init_one(&rights, CAP_LOOKUP),
3867 NDINIT_ATRIGHTS(&nd, DELETE, LOCKPARENT | LOCKLEAF | AUDITVNODE1 |
3868 at2cnpflags(flag, AT_BENEATH | AT_RESOLVE_BENEATH),
3869 pathseg, path, dfd, &cap_unlinkat_rights, td);
3870 if ((error = namei(&nd)) != 0)
3873 if (vp->v_type != VDIR) {
3878 * No rmdir "." please.
3880 if (nd.ni_dvp == vp) {
3885 * The root of a mounted filesystem cannot be deleted.
3887 if (vp->v_vflag & VV_ROOT) {
3892 if (fp != NULL && fp->f_vnode != vp) {
3893 if (VN_IS_DOOMED(fp->f_vnode))
3901 error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp,
3906 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3907 NDFREE(&nd, NDF_ONLY_PNBUF);
3909 if (nd.ni_dvp == vp)
3913 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3917 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_UNLINK);
3918 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
3919 vn_finished_write(mp);
3921 NDFREE(&nd, NDF_ONLY_PNBUF);
3923 if (nd.ni_dvp == vp)
3927 if (error == ERELOOKUP)
3935 #if defined(COMPAT_43) || defined(COMPAT_FREEBSD11)
3937 freebsd11_kern_getdirentries(struct thread *td, int fd, char *ubuf, u_int count,
3938 long *basep, void (*func)(struct freebsd11_dirent *))
3940 struct freebsd11_dirent dstdp;
3941 struct dirent *dp, *edp;
3944 ssize_t resid, ucount;
3947 /* XXX arbitrary sanity limit on `count'. */
3948 count = min(count, 64 * 1024);
3950 dirbuf = malloc(count, M_TEMP, M_WAITOK);
3952 error = kern_getdirentries(td, fd, dirbuf, count, &base, &resid,
3960 for (dp = (struct dirent *)dirbuf,
3961 edp = (struct dirent *)&dirbuf[count - resid];
3962 ucount < count && dp < edp; ) {
3963 if (dp->d_reclen == 0)
3965 MPASS(dp->d_reclen >= _GENERIC_DIRLEN(0));
3966 if (dp->d_namlen >= sizeof(dstdp.d_name))
3968 dstdp.d_type = dp->d_type;
3969 dstdp.d_namlen = dp->d_namlen;
3970 dstdp.d_fileno = dp->d_fileno; /* truncate */
3971 if (dstdp.d_fileno != dp->d_fileno) {
3972 switch (ino64_trunc_error) {
3980 dstdp.d_fileno = UINT32_MAX;
3984 dstdp.d_reclen = sizeof(dstdp) - sizeof(dstdp.d_name) +
3985 ((dp->d_namlen + 1 + 3) &~ 3);
3986 bcopy(dp->d_name, dstdp.d_name, dstdp.d_namlen);
3987 bzero(dstdp.d_name + dstdp.d_namlen,
3988 dstdp.d_reclen - offsetof(struct freebsd11_dirent, d_name) -
3990 MPASS(dstdp.d_reclen <= dp->d_reclen);
3991 MPASS(ucount + dstdp.d_reclen <= count);
3994 error = copyout(&dstdp, ubuf + ucount, dstdp.d_reclen);
3997 dp = (struct dirent *)((char *)dp + dp->d_reclen);
3998 ucount += dstdp.d_reclen;
4002 free(dirbuf, M_TEMP);
4004 td->td_retval[0] = ucount;
4011 ogetdirentries_cvt(struct freebsd11_dirent *dp)
4013 #if (BYTE_ORDER == LITTLE_ENDIAN)
4015 * The expected low byte of dp->d_namlen is our dp->d_type.
4016 * The high MBZ byte of dp->d_namlen is our dp->d_namlen.
4018 dp->d_type = dp->d_namlen;
4022 * The dp->d_type is the high byte of the expected dp->d_namlen,
4023 * so must be zero'ed.
4030 * Read a block of directory entries in a filesystem independent format.
4032 #ifndef _SYS_SYSPROTO_H_
4033 struct ogetdirentries_args {
4041 ogetdirentries(struct thread *td, struct ogetdirentries_args *uap)
4046 error = kern_ogetdirentries(td, uap, &loff);
4048 error = copyout(&loff, uap->basep, sizeof(long));
4053 kern_ogetdirentries(struct thread *td, struct ogetdirentries_args *uap,
4059 /* XXX arbitrary sanity limit on `count'. */
4060 if (uap->count > 64 * 1024)
4063 error = freebsd11_kern_getdirentries(td, uap->fd, uap->buf, uap->count,
4064 &base, ogetdirentries_cvt);
4066 if (error == 0 && uap->basep != NULL)
4067 error = copyout(&base, uap->basep, sizeof(long));
4071 #endif /* COMPAT_43 */
4073 #if defined(COMPAT_FREEBSD11)
4074 #ifndef _SYS_SYSPROTO_H_
4075 struct freebsd11_getdirentries_args {
4083 freebsd11_getdirentries(struct thread *td,
4084 struct freebsd11_getdirentries_args *uap)
4089 error = freebsd11_kern_getdirentries(td, uap->fd, uap->buf, uap->count,
4092 if (error == 0 && uap->basep != NULL)
4093 error = copyout(&base, uap->basep, sizeof(long));
4098 freebsd11_getdents(struct thread *td, struct freebsd11_getdents_args *uap)
4100 struct freebsd11_getdirentries_args ap;
4104 ap.count = uap->count;
4106 return (freebsd11_getdirentries(td, &ap));
4108 #endif /* COMPAT_FREEBSD11 */
4111 * Read a block of directory entries in a filesystem independent format.
4114 sys_getdirentries(struct thread *td, struct getdirentries_args *uap)
4119 error = kern_getdirentries(td, uap->fd, uap->buf, uap->count, &base,
4120 NULL, UIO_USERSPACE);
4123 if (uap->basep != NULL)
4124 error = copyout(&base, uap->basep, sizeof(off_t));
4129 kern_getdirentries(struct thread *td, int fd, char *buf, size_t count,
4130 off_t *basep, ssize_t *residp, enum uio_seg bufseg)
4141 if (count > IOSIZE_MAX)
4143 auio.uio_resid = count;
4144 error = getvnode(td, fd, &cap_read_rights, &fp);
4147 if ((fp->f_flag & FREAD) == 0) {
4152 foffset = foffset_lock(fp, 0);
4154 if (vp->v_type != VDIR) {
4158 aiov.iov_base = buf;
4159 aiov.iov_len = count;
4160 auio.uio_iov = &aiov;
4161 auio.uio_iovcnt = 1;
4162 auio.uio_rw = UIO_READ;
4163 auio.uio_segflg = bufseg;
4165 vn_lock(vp, LK_SHARED | LK_RETRY);
4166 AUDIT_ARG_VNODE1(vp);
4167 loff = auio.uio_offset = foffset;
4169 error = mac_vnode_check_readdir(td->td_ucred, vp);
4172 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL,
4174 foffset = auio.uio_offset;
4179 if (count == auio.uio_resid &&
4180 (vp->v_vflag & VV_ROOT) &&
4181 (vp->v_mount->mnt_flag & MNT_UNION)) {
4182 struct vnode *tvp = vp;
4184 vp = vp->v_mount->mnt_vnodecovered;
4194 *residp = auio.uio_resid;
4195 td->td_retval[0] = count - auio.uio_resid;
4197 foffset_unlock(fp, foffset, 0);
4203 * Set the mode mask for creation of filesystem nodes.
4205 #ifndef _SYS_SYSPROTO_H_
4211 sys_umask(struct thread *td, struct umask_args *uap)
4213 struct pwddesc *pdp;
4215 pdp = td->td_proc->p_pd;
4217 td->td_retval[0] = pdp->pd_cmask;
4218 pdp->pd_cmask = uap->newmask & ALLPERMS;
4219 PWDDESC_XUNLOCK(pdp);
4224 * Void all references to file by ripping underlying filesystem away from
4227 #ifndef _SYS_SYSPROTO_H_
4228 struct revoke_args {
4233 sys_revoke(struct thread *td, struct revoke_args *uap)
4237 struct nameidata nd;
4240 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
4242 if ((error = namei(&nd)) != 0)
4245 NDFREE(&nd, NDF_ONLY_PNBUF);
4246 if (vp->v_type != VCHR || vp->v_rdev == NULL) {
4251 error = mac_vnode_check_revoke(td->td_ucred, vp);
4255 error = VOP_GETATTR(vp, &vattr, td->td_ucred);
4258 if (td->td_ucred->cr_uid != vattr.va_uid) {
4259 error = priv_check(td, PRIV_VFS_ADMIN);
4263 if (devfs_usecount(vp) > 0)
4264 VOP_REVOKE(vp, REVOKEALL);
4271 * Convert a user file descriptor to a kernel file entry and check that, if it
4272 * is a capability, the correct rights are present. A reference on the file
4273 * entry is held upon returning.
4276 getvnode(struct thread *td, int fd, cap_rights_t *rightsp, struct file **fpp)
4281 error = fget_unlocked(td->td_proc->p_fd, fd, rightsp, &fp);
4286 * The file could be not of the vnode type, or it may be not
4287 * yet fully initialized, in which case the f_vnode pointer
4288 * may be set, but f_ops is still badfileops. E.g.,
4289 * devfs_open() transiently create such situation to
4290 * facilitate csw d_fdopen().
4292 * Dupfdopen() handling in kern_openat() installs the
4293 * half-baked file into the process descriptor table, allowing
4294 * other thread to dereference it. Guard against the race by
4297 if (fp->f_vnode == NULL || fp->f_ops == &badfileops) {
4306 * Get an (NFS) file handle.
4308 #ifndef _SYS_SYSPROTO_H_
4309 struct lgetfh_args {
4315 sys_lgetfh(struct thread *td, struct lgetfh_args *uap)
4318 return (kern_getfhat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->fname,
4319 UIO_USERSPACE, uap->fhp, UIO_USERSPACE));
4322 #ifndef _SYS_SYSPROTO_H_
4329 sys_getfh(struct thread *td, struct getfh_args *uap)
4332 return (kern_getfhat(td, 0, AT_FDCWD, uap->fname, UIO_USERSPACE,
4333 uap->fhp, UIO_USERSPACE));
4337 * syscall for the rpc.lockd to use to translate an open descriptor into
4338 * a NFS file handle.
4340 * warning: do not remove the priv_check() call or this becomes one giant
4343 #ifndef _SYS_SYSPROTO_H_
4344 struct getfhat_args {
4352 sys_getfhat(struct thread *td, struct getfhat_args *uap)
4355 if ((uap->flags & ~(AT_SYMLINK_NOFOLLOW | AT_BENEATH |
4356 AT_RESOLVE_BENEATH)) != 0)
4358 return (kern_getfhat(td, uap->flags, uap->fd, uap->path, UIO_USERSPACE,
4359 uap->fhp, UIO_USERSPACE));
4363 kern_getfhat(struct thread *td, int flags, int fd, const char *path,
4364 enum uio_seg pathseg, fhandle_t *fhp, enum uio_seg fhseg)
4366 struct nameidata nd;
4371 error = priv_check(td, PRIV_VFS_GETFH);
4374 NDINIT_AT(&nd, LOOKUP, at2cnpflags(flags, AT_SYMLINK_NOFOLLOW |
4375 AT_BENEATH | AT_RESOLVE_BENEATH) | LOCKLEAF | AUDITVNODE1,
4376 pathseg, path, fd, td);
4380 NDFREE(&nd, NDF_ONLY_PNBUF);
4382 bzero(&fh, sizeof(fh));
4383 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
4384 error = VOP_VPTOFH(vp, &fh.fh_fid);
4387 if (fhseg == UIO_USERSPACE)
4388 error = copyout(&fh, fhp, sizeof (fh));
4390 memcpy(fhp, &fh, sizeof(fh));
4395 #ifndef _SYS_SYSPROTO_H_
4396 struct fhlink_args {
4402 sys_fhlink(struct thread *td, struct fhlink_args *uap)
4405 return (kern_fhlinkat(td, AT_FDCWD, uap->to, UIO_USERSPACE, uap->fhp));
4408 #ifndef _SYS_SYSPROTO_H_
4409 struct fhlinkat_args {
4416 sys_fhlinkat(struct thread *td, struct fhlinkat_args *uap)
4419 return (kern_fhlinkat(td, uap->tofd, uap->to, UIO_USERSPACE, uap->fhp));
4423 kern_fhlinkat(struct thread *td, int fd, const char *path,
4424 enum uio_seg pathseg, fhandle_t *fhp)
4431 error = priv_check(td, PRIV_VFS_GETFH);
4434 error = copyin(fhp, &fh, sizeof(fh));
4439 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4441 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_SHARED, &vp);
4446 error = kern_linkat_vp(td, vp, fd, path, pathseg);
4447 } while (error == EAGAIN || error == ERELOOKUP);
4451 #ifndef _SYS_SYSPROTO_H_
4452 struct fhreadlink_args {
4459 sys_fhreadlink(struct thread *td, struct fhreadlink_args *uap)
4466 error = priv_check(td, PRIV_VFS_GETFH);
4469 if (uap->bufsize > IOSIZE_MAX)
4471 error = copyin(uap->fhp, &fh, sizeof(fh));
4474 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4476 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_SHARED, &vp);
4480 error = kern_readlink_vp(vp, uap->buf, UIO_USERSPACE, uap->bufsize, td);
4486 * syscall for the rpc.lockd to use to translate a NFS file handle into an
4489 * warning: do not remove the priv_check() call or this becomes one giant
4492 #ifndef _SYS_SYSPROTO_H_
4493 struct fhopen_args {
4494 const struct fhandle *u_fhp;
4499 sys_fhopen(struct thread *td, struct fhopen_args *uap)
4501 return (kern_fhopen(td, uap->u_fhp, uap->flags));
4505 kern_fhopen(struct thread *td, const struct fhandle *u_fhp, int flags)
4514 error = priv_check(td, PRIV_VFS_FHOPEN);
4518 fmode = FFLAGS(flags);
4519 /* why not allow a non-read/write open for our lockd? */
4520 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
4522 error = copyin(u_fhp, &fhp, sizeof(fhp));
4525 /* find the mount point */
4526 mp = vfs_busyfs(&fhp.fh_fsid);
4529 /* now give me my vnode, it gets returned to me locked */
4530 error = VFS_FHTOVP(mp, &fhp.fh_fid, LK_EXCLUSIVE, &vp);
4535 error = falloc_noinstall(td, &fp);
4541 * An extra reference on `fp' has been held for us by
4542 * falloc_noinstall().
4548 error = vn_open_vnode(vp, fmode, td->td_ucred, td, fp);
4550 KASSERT(fp->f_ops == &badfileops,
4551 ("VOP_OPEN in fhopen() set f_ops"));
4552 KASSERT(td->td_dupfd < 0,
4553 ("fhopen() encountered fdopen()"));
4562 finit_vnode(fp, fmode, NULL, &vnops);
4564 if ((fmode & O_TRUNC) != 0) {
4565 error = fo_truncate(fp, 0, td->td_ucred, td);
4570 error = finstall(td, fp, &indx, fmode, NULL);
4573 td->td_retval[0] = indx;
4578 * Stat an (NFS) file handle.
4580 #ifndef _SYS_SYSPROTO_H_
4581 struct fhstat_args {
4582 struct fhandle *u_fhp;
4587 sys_fhstat(struct thread *td, struct fhstat_args *uap)
4593 error = copyin(uap->u_fhp, &fh, sizeof(fh));
4596 error = kern_fhstat(td, fh, &sb);
4598 error = copyout(&sb, uap->sb, sizeof(sb));
4603 kern_fhstat(struct thread *td, struct fhandle fh, struct stat *sb)
4609 error = priv_check(td, PRIV_VFS_FHSTAT);
4612 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4614 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_EXCLUSIVE, &vp);
4618 error = VOP_STAT(vp, sb, td->td_ucred, NOCRED, td);
4624 * Implement fstatfs() for (NFS) file handles.
4626 #ifndef _SYS_SYSPROTO_H_
4627 struct fhstatfs_args {
4628 struct fhandle *u_fhp;
4633 sys_fhstatfs(struct thread *td, struct fhstatfs_args *uap)
4639 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4642 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
4643 error = kern_fhstatfs(td, fh, sfp);
4645 error = copyout(sfp, uap->buf, sizeof(*sfp));
4646 free(sfp, M_STATFS);
4651 kern_fhstatfs(struct thread *td, fhandle_t fh, struct statfs *buf)
4657 error = priv_check(td, PRIV_VFS_FHSTATFS);
4660 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4662 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_EXCLUSIVE, &vp);
4668 error = prison_canseemount(td->td_ucred, mp);
4672 error = mac_mount_check_stat(td->td_ucred, mp);
4676 error = VFS_STATFS(mp, buf);
4683 * Unlike madvise(2), we do not make a best effort to remember every
4684 * possible caching hint. Instead, we remember the last setting with
4685 * the exception that we will allow POSIX_FADV_NORMAL to adjust the
4686 * region of any current setting.
4689 kern_posix_fadvise(struct thread *td, int fd, off_t offset, off_t len,
4692 struct fadvise_info *fa, *new;
4698 if (offset < 0 || len < 0 || offset > OFF_MAX - len)
4700 AUDIT_ARG_VALUE(advice);
4702 case POSIX_FADV_SEQUENTIAL:
4703 case POSIX_FADV_RANDOM:
4704 case POSIX_FADV_NOREUSE:
4705 new = malloc(sizeof(*fa), M_FADVISE, M_WAITOK);
4707 case POSIX_FADV_NORMAL:
4708 case POSIX_FADV_WILLNEED:
4709 case POSIX_FADV_DONTNEED:
4715 /* XXX: CAP_POSIX_FADVISE? */
4717 error = fget(td, fd, &cap_no_rights, &fp);
4720 AUDIT_ARG_FILE(td->td_proc, fp);
4721 if ((fp->f_ops->fo_flags & DFLAG_SEEKABLE) == 0) {
4725 if (fp->f_type != DTYPE_VNODE) {
4730 if (vp->v_type != VREG) {
4737 end = offset + len - 1;
4739 case POSIX_FADV_SEQUENTIAL:
4740 case POSIX_FADV_RANDOM:
4741 case POSIX_FADV_NOREUSE:
4743 * Try to merge any existing non-standard region with
4744 * this new region if possible, otherwise create a new
4745 * non-standard region for this request.
4747 mtx_pool_lock(mtxpool_sleep, fp);
4749 if (fa != NULL && fa->fa_advice == advice &&
4750 ((fa->fa_start <= end && fa->fa_end >= offset) ||
4751 (end != OFF_MAX && fa->fa_start == end + 1) ||
4752 (fa->fa_end != OFF_MAX && fa->fa_end + 1 == offset))) {
4753 if (offset < fa->fa_start)
4754 fa->fa_start = offset;
4755 if (end > fa->fa_end)
4758 new->fa_advice = advice;
4759 new->fa_start = offset;
4764 mtx_pool_unlock(mtxpool_sleep, fp);
4766 case POSIX_FADV_NORMAL:
4768 * If a the "normal" region overlaps with an existing
4769 * non-standard region, trim or remove the
4770 * non-standard region.
4772 mtx_pool_lock(mtxpool_sleep, fp);
4775 if (offset <= fa->fa_start && end >= fa->fa_end) {
4777 fp->f_advice = NULL;
4778 } else if (offset <= fa->fa_start &&
4779 end >= fa->fa_start)
4780 fa->fa_start = end + 1;
4781 else if (offset <= fa->fa_end && end >= fa->fa_end)
4782 fa->fa_end = offset - 1;
4783 else if (offset >= fa->fa_start && end <= fa->fa_end) {
4785 * If the "normal" region is a middle
4786 * portion of the existing
4787 * non-standard region, just remove
4788 * the whole thing rather than picking
4789 * one side or the other to
4793 fp->f_advice = NULL;
4796 mtx_pool_unlock(mtxpool_sleep, fp);
4798 case POSIX_FADV_WILLNEED:
4799 case POSIX_FADV_DONTNEED:
4800 error = VOP_ADVISE(vp, offset, end, advice);
4806 free(new, M_FADVISE);
4811 sys_posix_fadvise(struct thread *td, struct posix_fadvise_args *uap)
4815 error = kern_posix_fadvise(td, uap->fd, uap->offset, uap->len,
4817 return (kern_posix_error(td, error));
4821 kern_copy_file_range(struct thread *td, int infd, off_t *inoffp, int outfd,
4822 off_t *outoffp, size_t len, unsigned int flags)
4824 struct file *infp, *outfp;
4825 struct vnode *invp, *outvp;
4828 void *rl_rcookie, *rl_wcookie;
4829 off_t savinoff, savoutoff;
4831 infp = outfp = NULL;
4832 rl_rcookie = rl_wcookie = NULL;
4841 if (len > SSIZE_MAX)
4843 * Although the len argument is size_t, the return argument
4844 * is ssize_t (which is signed). Therefore a size that won't
4845 * fit in ssize_t can't be returned.
4849 /* Get the file structures for the file descriptors. */
4850 error = fget_read(td, infd, &cap_read_rights, &infp);
4853 if (infp->f_ops == &badfileops) {
4857 if (infp->f_vnode == NULL) {
4861 error = fget_write(td, outfd, &cap_write_rights, &outfp);
4864 if (outfp->f_ops == &badfileops) {
4868 if (outfp->f_vnode == NULL) {
4873 /* Set the offset pointers to the correct place. */
4875 inoffp = &infp->f_offset;
4876 if (outoffp == NULL)
4877 outoffp = &outfp->f_offset;
4879 savoutoff = *outoffp;
4881 invp = infp->f_vnode;
4882 outvp = outfp->f_vnode;
4883 /* Sanity check the f_flag bits. */
4884 if ((outfp->f_flag & (FWRITE | FAPPEND)) != FWRITE ||
4885 (infp->f_flag & FREAD) == 0) {
4890 /* If len == 0, just return 0. */
4895 * If infp and outfp refer to the same file, the byte ranges cannot
4898 if (invp == outvp && ((savinoff <= savoutoff && savinoff + len >
4899 savoutoff) || (savinoff > savoutoff && savoutoff + len >
4905 /* Range lock the byte ranges for both invp and outvp. */
4907 rl_wcookie = vn_rangelock_wlock(outvp, *outoffp, *outoffp +
4909 rl_rcookie = vn_rangelock_tryrlock(invp, *inoffp, *inoffp +
4911 if (rl_rcookie != NULL)
4913 vn_rangelock_unlock(outvp, rl_wcookie);
4914 rl_rcookie = vn_rangelock_rlock(invp, *inoffp, *inoffp + len);
4915 vn_rangelock_unlock(invp, rl_rcookie);
4919 error = vn_copy_file_range(invp, inoffp, outvp, outoffp, &retlen,
4920 flags, infp->f_cred, outfp->f_cred, td);
4922 if (rl_rcookie != NULL)
4923 vn_rangelock_unlock(invp, rl_rcookie);
4924 if (rl_wcookie != NULL)
4925 vn_rangelock_unlock(outvp, rl_wcookie);
4926 if (savinoff != -1 && (error == EINTR || error == ERESTART)) {
4928 *outoffp = savoutoff;
4934 td->td_retval[0] = retlen;
4939 sys_copy_file_range(struct thread *td, struct copy_file_range_args *uap)
4941 off_t inoff, outoff, *inoffp, *outoffp;
4944 inoffp = outoffp = NULL;
4945 if (uap->inoffp != NULL) {
4946 error = copyin(uap->inoffp, &inoff, sizeof(off_t));
4951 if (uap->outoffp != NULL) {
4952 error = copyin(uap->outoffp, &outoff, sizeof(off_t));
4957 error = kern_copy_file_range(td, uap->infd, inoffp, uap->outfd,
4958 outoffp, uap->len, uap->flags);
4959 if (error == 0 && uap->inoffp != NULL)
4960 error = copyout(inoffp, uap->inoffp, sizeof(off_t));
4961 if (error == 0 && uap->outoffp != NULL)
4962 error = copyout(outoffp, uap->outoffp, sizeof(off_t));