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_getfhat(struct thread *td, int flags, int fd,
109 const char *path, enum uio_seg pathseg, fhandle_t *fhp);
110 static int kern_readlink_vp(struct vnode *vp, char *buf, enum uio_seg bufseg,
111 size_t count, struct thread *td);
112 static int kern_linkat_vp(struct thread *td, struct vnode *vp, int fd,
113 const char *path, enum uio_seg segflag);
116 kern_sync(struct thread *td)
118 struct mount *mp, *nmp;
121 mtx_lock(&mountlist_mtx);
122 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
123 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK)) {
124 nmp = TAILQ_NEXT(mp, mnt_list);
127 if ((mp->mnt_flag & MNT_RDONLY) == 0 &&
128 vn_start_write(NULL, &mp, V_NOWAIT) == 0) {
129 save = curthread_pflags_set(TDP_SYNCIO);
130 vfs_periodic(mp, MNT_NOWAIT);
131 VFS_SYNC(mp, MNT_NOWAIT);
132 curthread_pflags_restore(save);
133 vn_finished_write(mp);
135 mtx_lock(&mountlist_mtx);
136 nmp = TAILQ_NEXT(mp, mnt_list);
139 mtx_unlock(&mountlist_mtx);
144 * Sync each mounted filesystem.
146 #ifndef _SYS_SYSPROTO_H_
153 sys_sync(struct thread *td, struct sync_args *uap)
156 return (kern_sync(td));
160 * Change filesystem quotas.
162 #ifndef _SYS_SYSPROTO_H_
163 struct quotactl_args {
171 sys_quotactl(struct thread *td, struct quotactl_args *uap)
177 AUDIT_ARG_CMD(uap->cmd);
178 AUDIT_ARG_UID(uap->uid);
179 if (!prison_allow(td->td_ucred, PR_ALLOW_QUOTAS))
181 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
183 if ((error = namei(&nd)) != 0)
185 NDFREE(&nd, NDF_ONLY_PNBUF);
186 mp = nd.ni_vp->v_mount;
189 error = vfs_busy(mp, 0);
194 error = VFS_QUOTACTL(mp, uap->cmd, uap->uid, uap->arg);
197 * Since quota on operation typically needs to open quota
198 * file, the Q_QUOTAON handler needs to unbusy the mount point
199 * before calling into namei. Otherwise, unmount might be
200 * started between two vfs_busy() invocations (first is our,
201 * second is from mount point cross-walk code in lookup()),
204 * Require that Q_QUOTAON handles the vfs_busy() reference on
205 * its own, always returning with ubusied mount point.
207 if ((uap->cmd >> SUBCMDSHIFT) != Q_QUOTAON &&
208 (uap->cmd >> SUBCMDSHIFT) != Q_QUOTAOFF)
215 * Used by statfs conversion routines to scale the block size up if
216 * necessary so that all of the block counts are <= 'max_size'. Note
217 * that 'max_size' should be a bitmask, i.e. 2^n - 1 for some non-zero
221 statfs_scale_blocks(struct statfs *sf, long max_size)
226 KASSERT(powerof2(max_size + 1), ("%s: invalid max_size", __func__));
229 * Attempt to scale the block counts to give a more accurate
230 * overview to userland of the ratio of free space to used
231 * space. To do this, find the largest block count and compute
232 * a divisor that lets it fit into a signed integer <= max_size.
234 if (sf->f_bavail < 0)
235 count = -sf->f_bavail;
237 count = sf->f_bavail;
238 count = MAX(sf->f_blocks, MAX(sf->f_bfree, count));
239 if (count <= max_size)
242 count >>= flsl(max_size);
249 sf->f_bsize <<= shift;
250 sf->f_blocks >>= shift;
251 sf->f_bfree >>= shift;
252 sf->f_bavail >>= shift;
256 kern_do_statfs(struct thread *td, struct mount *mp, struct statfs *buf)
262 error = vfs_busy(mp, 0);
267 error = mac_mount_check_stat(td->td_ucred, mp);
271 error = VFS_STATFS(mp, buf);
274 if (priv_check_cred_vfs_generation(td->td_ucred)) {
275 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
276 prison_enforce_statfs(td->td_ucred, mp, buf);
284 * Get filesystem statistics.
286 #ifndef _SYS_SYSPROTO_H_
293 sys_statfs(struct thread *td, struct statfs_args *uap)
298 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
299 error = kern_statfs(td, uap->path, UIO_USERSPACE, sfp);
301 error = copyout(sfp, uap->buf, sizeof(struct statfs));
307 kern_statfs(struct thread *td, const char *path, enum uio_seg pathseg,
314 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
319 mp = nd.ni_vp->v_mount;
321 NDFREE(&nd, NDF_ONLY_PNBUF);
323 return (kern_do_statfs(td, mp, buf));
327 * Get filesystem statistics.
329 #ifndef _SYS_SYSPROTO_H_
330 struct fstatfs_args {
336 sys_fstatfs(struct thread *td, struct fstatfs_args *uap)
341 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
342 error = kern_fstatfs(td, uap->fd, sfp);
344 error = copyout(sfp, uap->buf, sizeof(struct statfs));
350 kern_fstatfs(struct thread *td, int fd, struct statfs *buf)
358 error = getvnode(td, fd, &cap_fstatfs_rights, &fp);
362 vn_lock(vp, LK_SHARED | LK_RETRY);
364 AUDIT_ARG_VNODE1(vp);
371 return (kern_do_statfs(td, mp, buf));
375 * Get statistics on all filesystems.
377 #ifndef _SYS_SYSPROTO_H_
378 struct getfsstat_args {
385 sys_getfsstat(struct thread *td, struct getfsstat_args *uap)
390 if (uap->bufsize < 0 || uap->bufsize > SIZE_MAX)
392 error = kern_getfsstat(td, &uap->buf, uap->bufsize, &count,
393 UIO_USERSPACE, uap->mode);
395 td->td_retval[0] = count;
400 * If (bufsize > 0 && bufseg == UIO_SYSSPACE)
401 * The caller is responsible for freeing memory which will be allocated
405 kern_getfsstat(struct thread *td, struct statfs **buf, size_t bufsize,
406 size_t *countp, enum uio_seg bufseg, int mode)
408 struct mount *mp, *nmp;
409 struct statfs *sfsp, *sp, *sptmp, *tofree;
410 size_t count, maxcount;
418 if (bufseg == UIO_SYSSPACE)
423 maxcount = bufsize / sizeof(struct statfs);
427 } else if (bufseg == UIO_USERSPACE) {
430 } else /* if (bufseg == UIO_SYSSPACE) */ {
432 mtx_lock(&mountlist_mtx);
433 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
436 mtx_unlock(&mountlist_mtx);
437 if (maxcount > count)
439 tofree = sfsp = *buf = malloc(maxcount * sizeof(struct statfs),
446 * If there is no target buffer they only want the count.
448 * This could be TAILQ_FOREACH but it is open-coded to match the original
452 mtx_lock(&mountlist_mtx);
453 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
454 if (prison_canseemount(td->td_ucred, mp) != 0) {
455 nmp = TAILQ_NEXT(mp, mnt_list);
459 if (mac_mount_check_stat(td->td_ucred, mp) != 0) {
460 nmp = TAILQ_NEXT(mp, mnt_list);
465 nmp = TAILQ_NEXT(mp, mnt_list);
467 mtx_unlock(&mountlist_mtx);
473 * They want the entire thing.
475 * Short-circuit the corner case of no room for anything, avoids
482 mtx_lock(&mountlist_mtx);
483 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
484 if (prison_canseemount(td->td_ucred, mp) != 0) {
485 nmp = TAILQ_NEXT(mp, mnt_list);
489 if (mac_mount_check_stat(td->td_ucred, mp) != 0) {
490 nmp = TAILQ_NEXT(mp, mnt_list);
494 if (mode == MNT_WAIT) {
495 if (vfs_busy(mp, MBF_MNTLSTLOCK) != 0) {
497 * If vfs_busy() failed, and MBF_NOWAIT
498 * wasn't passed, then the mp is gone.
499 * Furthermore, because of MBF_MNTLSTLOCK,
500 * the mountlist_mtx was dropped. We have
501 * no other choice than to start over.
503 mtx_unlock(&mountlist_mtx);
504 free(tofree, M_STATFS);
508 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK) != 0) {
509 nmp = TAILQ_NEXT(mp, mnt_list);
515 * If MNT_NOWAIT is specified, do not refresh
518 if (mode != MNT_NOWAIT) {
519 error = VFS_STATFS(mp, sp);
521 mtx_lock(&mountlist_mtx);
522 nmp = TAILQ_NEXT(mp, mnt_list);
527 if (priv_check_cred_vfs_generation(td->td_ucred)) {
528 sptmp = malloc(sizeof(struct statfs), M_STATFS,
531 sptmp->f_fsid.val[0] = sptmp->f_fsid.val[1] = 0;
532 prison_enforce_statfs(td->td_ucred, mp, sptmp);
536 if (bufseg == UIO_SYSSPACE) {
537 bcopy(sp, sfsp, sizeof(*sp));
538 free(sptmp, M_STATFS);
539 } else /* if (bufseg == UIO_USERSPACE) */ {
540 error = copyout(sp, sfsp, sizeof(*sp));
541 free(sptmp, M_STATFS);
550 if (count == maxcount) {
555 mtx_lock(&mountlist_mtx);
556 nmp = TAILQ_NEXT(mp, mnt_list);
559 mtx_unlock(&mountlist_mtx);
565 #ifdef COMPAT_FREEBSD4
567 * Get old format filesystem statistics.
569 static void freebsd4_cvtstatfs(struct statfs *, struct ostatfs *);
571 #ifndef _SYS_SYSPROTO_H_
572 struct freebsd4_statfs_args {
578 freebsd4_statfs(struct thread *td, struct freebsd4_statfs_args *uap)
584 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
585 error = kern_statfs(td, uap->path, UIO_USERSPACE, sfp);
587 freebsd4_cvtstatfs(sfp, &osb);
588 error = copyout(&osb, uap->buf, sizeof(osb));
595 * Get filesystem statistics.
597 #ifndef _SYS_SYSPROTO_H_
598 struct freebsd4_fstatfs_args {
604 freebsd4_fstatfs(struct thread *td, struct freebsd4_fstatfs_args *uap)
610 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
611 error = kern_fstatfs(td, uap->fd, sfp);
613 freebsd4_cvtstatfs(sfp, &osb);
614 error = copyout(&osb, uap->buf, sizeof(osb));
621 * Get statistics on all filesystems.
623 #ifndef _SYS_SYSPROTO_H_
624 struct freebsd4_getfsstat_args {
631 freebsd4_getfsstat(struct thread *td, struct freebsd4_getfsstat_args *uap)
633 struct statfs *buf, *sp;
638 if (uap->bufsize < 0)
640 count = uap->bufsize / sizeof(struct ostatfs);
641 if (count > SIZE_MAX / sizeof(struct statfs))
643 size = count * sizeof(struct statfs);
644 error = kern_getfsstat(td, &buf, size, &count, UIO_SYSSPACE,
647 td->td_retval[0] = count;
650 while (count != 0 && error == 0) {
651 freebsd4_cvtstatfs(sp, &osb);
652 error = copyout(&osb, uap->buf, sizeof(osb));
663 * Implement fstatfs() for (NFS) file handles.
665 #ifndef _SYS_SYSPROTO_H_
666 struct freebsd4_fhstatfs_args {
667 struct fhandle *u_fhp;
672 freebsd4_fhstatfs(struct thread *td, struct freebsd4_fhstatfs_args *uap)
679 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
682 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
683 error = kern_fhstatfs(td, fh, sfp);
685 freebsd4_cvtstatfs(sfp, &osb);
686 error = copyout(&osb, uap->buf, sizeof(osb));
693 * Convert a new format statfs structure to an old format statfs structure.
696 freebsd4_cvtstatfs(struct statfs *nsp, struct ostatfs *osp)
699 statfs_scale_blocks(nsp, LONG_MAX);
700 bzero(osp, sizeof(*osp));
701 osp->f_bsize = nsp->f_bsize;
702 osp->f_iosize = MIN(nsp->f_iosize, LONG_MAX);
703 osp->f_blocks = nsp->f_blocks;
704 osp->f_bfree = nsp->f_bfree;
705 osp->f_bavail = nsp->f_bavail;
706 osp->f_files = MIN(nsp->f_files, LONG_MAX);
707 osp->f_ffree = MIN(nsp->f_ffree, LONG_MAX);
708 osp->f_owner = nsp->f_owner;
709 osp->f_type = nsp->f_type;
710 osp->f_flags = nsp->f_flags;
711 osp->f_syncwrites = MIN(nsp->f_syncwrites, LONG_MAX);
712 osp->f_asyncwrites = MIN(nsp->f_asyncwrites, LONG_MAX);
713 osp->f_syncreads = MIN(nsp->f_syncreads, LONG_MAX);
714 osp->f_asyncreads = MIN(nsp->f_asyncreads, LONG_MAX);
715 strlcpy(osp->f_fstypename, nsp->f_fstypename,
716 MIN(MFSNAMELEN, OMFSNAMELEN));
717 strlcpy(osp->f_mntonname, nsp->f_mntonname,
718 MIN(MNAMELEN, OMNAMELEN));
719 strlcpy(osp->f_mntfromname, nsp->f_mntfromname,
720 MIN(MNAMELEN, OMNAMELEN));
721 osp->f_fsid = nsp->f_fsid;
723 #endif /* COMPAT_FREEBSD4 */
725 #if defined(COMPAT_FREEBSD11)
727 * Get old format filesystem statistics.
729 static void freebsd11_cvtstatfs(struct statfs *, struct freebsd11_statfs *);
732 freebsd11_statfs(struct thread *td, struct freebsd11_statfs_args *uap)
734 struct freebsd11_statfs osb;
738 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
739 error = kern_statfs(td, uap->path, UIO_USERSPACE, sfp);
741 freebsd11_cvtstatfs(sfp, &osb);
742 error = copyout(&osb, uap->buf, sizeof(osb));
749 * Get filesystem statistics.
752 freebsd11_fstatfs(struct thread *td, struct freebsd11_fstatfs_args *uap)
754 struct freebsd11_statfs osb;
758 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
759 error = kern_fstatfs(td, uap->fd, sfp);
761 freebsd11_cvtstatfs(sfp, &osb);
762 error = copyout(&osb, uap->buf, sizeof(osb));
769 * Get statistics on all filesystems.
772 freebsd11_getfsstat(struct thread *td, struct freebsd11_getfsstat_args *uap)
774 struct freebsd11_statfs osb;
775 struct statfs *buf, *sp;
779 count = uap->bufsize / sizeof(struct ostatfs);
780 size = count * sizeof(struct statfs);
781 error = kern_getfsstat(td, &buf, size, &count, UIO_SYSSPACE,
784 td->td_retval[0] = count;
787 while (count > 0 && error == 0) {
788 freebsd11_cvtstatfs(sp, &osb);
789 error = copyout(&osb, uap->buf, sizeof(osb));
800 * Implement fstatfs() for (NFS) file handles.
803 freebsd11_fhstatfs(struct thread *td, struct freebsd11_fhstatfs_args *uap)
805 struct freebsd11_statfs osb;
810 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
813 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
814 error = kern_fhstatfs(td, fh, sfp);
816 freebsd11_cvtstatfs(sfp, &osb);
817 error = copyout(&osb, uap->buf, sizeof(osb));
824 * Convert a new format statfs structure to an old format statfs structure.
827 freebsd11_cvtstatfs(struct statfs *nsp, struct freebsd11_statfs *osp)
830 bzero(osp, sizeof(*osp));
831 osp->f_version = FREEBSD11_STATFS_VERSION;
832 osp->f_type = nsp->f_type;
833 osp->f_flags = nsp->f_flags;
834 osp->f_bsize = nsp->f_bsize;
835 osp->f_iosize = nsp->f_iosize;
836 osp->f_blocks = nsp->f_blocks;
837 osp->f_bfree = nsp->f_bfree;
838 osp->f_bavail = nsp->f_bavail;
839 osp->f_files = nsp->f_files;
840 osp->f_ffree = nsp->f_ffree;
841 osp->f_syncwrites = nsp->f_syncwrites;
842 osp->f_asyncwrites = nsp->f_asyncwrites;
843 osp->f_syncreads = nsp->f_syncreads;
844 osp->f_asyncreads = nsp->f_asyncreads;
845 osp->f_namemax = nsp->f_namemax;
846 osp->f_owner = nsp->f_owner;
847 osp->f_fsid = nsp->f_fsid;
848 strlcpy(osp->f_fstypename, nsp->f_fstypename,
849 MIN(MFSNAMELEN, sizeof(osp->f_fstypename)));
850 strlcpy(osp->f_mntonname, nsp->f_mntonname,
851 MIN(MNAMELEN, sizeof(osp->f_mntonname)));
852 strlcpy(osp->f_mntfromname, nsp->f_mntfromname,
853 MIN(MNAMELEN, sizeof(osp->f_mntfromname)));
855 #endif /* COMPAT_FREEBSD11 */
858 * Change current working directory to a given file descriptor.
860 #ifndef _SYS_SYSPROTO_H_
866 sys_fchdir(struct thread *td, struct fchdir_args *uap)
868 struct vnode *vp, *tdp;
873 AUDIT_ARG_FD(uap->fd);
874 error = getvnode(td, uap->fd, &cap_fchdir_rights,
881 vn_lock(vp, LK_SHARED | LK_RETRY);
882 AUDIT_ARG_VNODE1(vp);
883 error = change_dir(vp, td);
884 while (!error && (mp = vp->v_mountedhere) != NULL) {
887 error = VFS_ROOT(mp, LK_SHARED, &tdp);
904 * Change current working directory (``.'').
906 #ifndef _SYS_SYSPROTO_H_
912 sys_chdir(struct thread *td, struct chdir_args *uap)
915 return (kern_chdir(td, uap->path, UIO_USERSPACE));
919 kern_chdir(struct thread *td, const char *path, enum uio_seg pathseg)
924 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
926 if ((error = namei(&nd)) != 0)
928 if ((error = change_dir(nd.ni_vp, td)) != 0) {
930 NDFREE(&nd, NDF_ONLY_PNBUF);
933 VOP_UNLOCK(nd.ni_vp);
934 NDFREE(&nd, NDF_ONLY_PNBUF);
935 pwd_chdir(td, nd.ni_vp);
940 * Change notion of root (``/'') directory.
942 #ifndef _SYS_SYSPROTO_H_
948 sys_chroot(struct thread *td, struct chroot_args *uap)
953 error = priv_check(td, PRIV_VFS_CHROOT);
956 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
957 UIO_USERSPACE, uap->path, td);
961 error = change_dir(nd.ni_vp, td);
965 error = mac_vnode_check_chroot(td->td_ucred, nd.ni_vp);
969 VOP_UNLOCK(nd.ni_vp);
970 error = pwd_chroot(td, nd.ni_vp);
972 NDFREE(&nd, NDF_ONLY_PNBUF);
977 NDFREE(&nd, NDF_ONLY_PNBUF);
982 * Common routine for chroot and chdir. Callers must provide a locked vnode
986 change_dir(struct vnode *vp, struct thread *td)
992 ASSERT_VOP_LOCKED(vp, "change_dir(): vp not locked");
993 if (vp->v_type != VDIR)
996 error = mac_vnode_check_chdir(td->td_ucred, vp);
1000 return (VOP_ACCESS(vp, VEXEC, td->td_ucred, td));
1003 static __inline void
1004 flags_to_rights(int flags, cap_rights_t *rightsp)
1007 if (flags & O_EXEC) {
1008 cap_rights_set_one(rightsp, CAP_FEXECVE);
1010 switch ((flags & O_ACCMODE)) {
1012 cap_rights_set_one(rightsp, CAP_READ);
1015 cap_rights_set_one(rightsp, CAP_READ);
1018 cap_rights_set_one(rightsp, CAP_WRITE);
1019 if (!(flags & (O_APPEND | O_TRUNC)))
1020 cap_rights_set_one(rightsp, CAP_SEEK);
1025 if (flags & O_CREAT)
1026 cap_rights_set_one(rightsp, CAP_CREATE);
1028 if (flags & O_TRUNC)
1029 cap_rights_set_one(rightsp, CAP_FTRUNCATE);
1031 if (flags & (O_SYNC | O_FSYNC))
1032 cap_rights_set_one(rightsp, CAP_FSYNC);
1034 if (flags & (O_EXLOCK | O_SHLOCK))
1035 cap_rights_set_one(rightsp, CAP_FLOCK);
1039 * Check permissions, allocate an open file structure, and call the device
1040 * open routine if any.
1042 #ifndef _SYS_SYSPROTO_H_
1050 sys_open(struct thread *td, struct open_args *uap)
1053 return (kern_openat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1054 uap->flags, uap->mode));
1057 #ifndef _SYS_SYSPROTO_H_
1058 struct openat_args {
1066 sys_openat(struct thread *td, struct openat_args *uap)
1069 AUDIT_ARG_FD(uap->fd);
1070 return (kern_openat(td, uap->fd, uap->path, UIO_USERSPACE, uap->flag,
1075 kern_openat(struct thread *td, int fd, const char *path, enum uio_seg pathseg,
1076 int flags, int mode)
1078 struct proc *p = td->td_proc;
1079 struct filedesc *fdp = p->p_fd;
1082 struct nameidata nd;
1083 cap_rights_t rights;
1084 int cmode, error, indx;
1088 AUDIT_ARG_FFLAGS(flags);
1089 AUDIT_ARG_MODE(mode);
1090 cap_rights_init_one(&rights, CAP_LOOKUP);
1091 flags_to_rights(flags, &rights);
1093 * Only one of the O_EXEC, O_RDONLY, O_WRONLY and O_RDWR flags
1096 if (flags & O_EXEC) {
1097 if (flags & O_ACCMODE)
1099 } else if ((flags & O_ACCMODE) == O_ACCMODE) {
1102 flags = FFLAGS(flags);
1106 * Allocate a file structure. The descriptor to reference it
1107 * is allocated and set by finstall() below.
1109 error = falloc_noinstall(td, &fp);
1113 * An extra reference on `fp' has been held for us by
1114 * falloc_noinstall().
1116 /* Set the flags early so the finit in devfs can pick them up. */
1117 fp->f_flag = flags & FMASK;
1118 cmode = ((mode & ~fdp->fd_cmask) & ALLPERMS) & ~S_ISTXT;
1119 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, fd,
1121 td->td_dupfd = -1; /* XXX check for fdopen */
1122 error = vn_open(&nd, &flags, cmode, fp);
1125 * If the vn_open replaced the method vector, something
1126 * wonderous happened deep below and we just pass it up
1127 * pretending we know what we do.
1129 if (error == ENXIO && fp->f_ops != &badfileops)
1133 * Handle special fdopen() case. bleh.
1135 * Don't do this for relative (capability) lookups; we don't
1136 * understand exactly what would happen, and we don't think
1137 * that it ever should.
1139 if ((nd.ni_resflags & NIRES_STRICTREL) == 0 &&
1140 (error == ENODEV || error == ENXIO) &&
1141 td->td_dupfd >= 0) {
1142 error = dupfdopen(td, fdp, td->td_dupfd, flags, error,
1151 NDFREE(&nd, NDF_ONLY_PNBUF);
1155 * Store the vnode, for any f_type. Typically, the vnode use
1156 * count is decremented by direct call to vn_closefile() for
1157 * files that switched type in the cdevsw fdopen() method.
1161 * If the file wasn't claimed by devfs bind it to the normal
1162 * vnode operations here.
1164 if (fp->f_ops == &badfileops) {
1165 KASSERT(vp->v_type != VFIFO, ("Unexpected fifo."));
1166 finit_vnode(fp, flags, NULL, &vnops);
1170 if (flags & O_TRUNC) {
1171 error = fo_truncate(fp, 0, td->td_ucred, td);
1177 * If we haven't already installed the FD (for dupfdopen), do so now.
1180 struct filecaps *fcaps;
1183 if ((nd.ni_resflags & NIRES_STRICTREL) != 0)
1184 fcaps = &nd.ni_filecaps;
1188 error = finstall(td, fp, &indx, flags, fcaps);
1189 /* On success finstall() consumes fcaps. */
1191 filecaps_free(&nd.ni_filecaps);
1195 filecaps_free(&nd.ni_filecaps);
1199 * Release our private reference, leaving the one associated with
1200 * the descriptor table intact.
1203 td->td_retval[0] = indx;
1206 KASSERT(indx == -1, ("indx=%d, should be -1", indx));
1215 #ifndef _SYS_SYSPROTO_H_
1216 struct ocreat_args {
1222 ocreat(struct thread *td, struct ocreat_args *uap)
1225 return (kern_openat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1226 O_WRONLY | O_CREAT | O_TRUNC, uap->mode));
1228 #endif /* COMPAT_43 */
1231 * Create a special file.
1233 #ifndef _SYS_SYSPROTO_H_
1234 struct mknodat_args {
1242 sys_mknodat(struct thread *td, struct mknodat_args *uap)
1245 return (kern_mknodat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode,
1249 #if defined(COMPAT_FREEBSD11)
1251 freebsd11_mknod(struct thread *td,
1252 struct freebsd11_mknod_args *uap)
1255 return (kern_mknodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1256 uap->mode, uap->dev));
1260 freebsd11_mknodat(struct thread *td,
1261 struct freebsd11_mknodat_args *uap)
1264 return (kern_mknodat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode,
1267 #endif /* COMPAT_FREEBSD11 */
1270 kern_mknodat(struct thread *td, int fd, const char *path, enum uio_seg pathseg,
1271 int mode, dev_t dev)
1276 struct nameidata nd;
1277 int error, whiteout = 0;
1279 AUDIT_ARG_MODE(mode);
1281 switch (mode & S_IFMT) {
1284 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
1285 if (error == 0 && dev == VNOVAL)
1289 error = priv_check(td, PRIV_VFS_MKNOD_WHT);
1293 return (kern_mkfifoat(td, fd, path, pathseg, mode));
1303 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
1304 NOCACHE, pathseg, path, fd, &cap_mknodat_rights,
1306 if ((error = namei(&nd)) != 0)
1310 NDFREE(&nd, NDF_ONLY_PNBUF);
1311 if (vp == nd.ni_dvp)
1319 vattr.va_mode = (mode & ALLPERMS) &
1320 ~td->td_proc->p_fd->fd_cmask;
1321 vattr.va_rdev = dev;
1324 switch (mode & S_IFMT) {
1326 vattr.va_type = VCHR;
1329 vattr.va_type = VBLK;
1335 panic("kern_mknod: invalid mode");
1338 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1339 NDFREE(&nd, NDF_ONLY_PNBUF);
1341 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1346 if (error == 0 && !whiteout)
1347 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp,
1348 &nd.ni_cnd, &vattr);
1352 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, CREATE);
1354 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp,
1355 &nd.ni_cnd, &vattr);
1360 NDFREE(&nd, NDF_ONLY_PNBUF);
1362 vn_finished_write(mp);
1367 * Create a named pipe.
1369 #ifndef _SYS_SYSPROTO_H_
1370 struct mkfifo_args {
1376 sys_mkfifo(struct thread *td, struct mkfifo_args *uap)
1379 return (kern_mkfifoat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1383 #ifndef _SYS_SYSPROTO_H_
1384 struct mkfifoat_args {
1391 sys_mkfifoat(struct thread *td, struct mkfifoat_args *uap)
1394 return (kern_mkfifoat(td, uap->fd, uap->path, UIO_USERSPACE,
1399 kern_mkfifoat(struct thread *td, int fd, const char *path,
1400 enum uio_seg pathseg, int mode)
1404 struct nameidata nd;
1407 AUDIT_ARG_MODE(mode);
1410 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
1411 NOCACHE, pathseg, path, fd, &cap_mkfifoat_rights,
1413 if ((error = namei(&nd)) != 0)
1415 if (nd.ni_vp != NULL) {
1416 NDFREE(&nd, NDF_ONLY_PNBUF);
1417 if (nd.ni_vp == nd.ni_dvp)
1424 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1425 NDFREE(&nd, NDF_ONLY_PNBUF);
1427 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1432 vattr.va_type = VFIFO;
1433 vattr.va_mode = (mode & ALLPERMS) & ~td->td_proc->p_fd->fd_cmask;
1435 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1440 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
1447 vn_finished_write(mp);
1448 NDFREE(&nd, NDF_ONLY_PNBUF);
1453 * Make a hard file link.
1455 #ifndef _SYS_SYSPROTO_H_
1462 sys_link(struct thread *td, struct link_args *uap)
1465 return (kern_linkat(td, AT_FDCWD, AT_FDCWD, uap->path, uap->link,
1466 UIO_USERSPACE, FOLLOW));
1469 #ifndef _SYS_SYSPROTO_H_
1470 struct linkat_args {
1479 sys_linkat(struct thread *td, struct linkat_args *uap)
1484 if ((flag & ~(AT_SYMLINK_FOLLOW | AT_BENEATH)) != 0)
1487 return (kern_linkat(td, uap->fd1, uap->fd2, uap->path1, uap->path2,
1488 UIO_USERSPACE, ((flag & AT_SYMLINK_FOLLOW) != 0 ? FOLLOW :
1489 NOFOLLOW) | ((flag & AT_BENEATH) != 0 ? BENEATH : 0)));
1492 int hardlink_check_uid = 0;
1493 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
1494 &hardlink_check_uid, 0,
1495 "Unprivileged processes cannot create hard links to files owned by other "
1497 static int hardlink_check_gid = 0;
1498 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
1499 &hardlink_check_gid, 0,
1500 "Unprivileged processes cannot create hard links to files owned by other "
1504 can_hardlink(struct vnode *vp, struct ucred *cred)
1509 if (!hardlink_check_uid && !hardlink_check_gid)
1512 error = VOP_GETATTR(vp, &va, cred);
1516 if (hardlink_check_uid && cred->cr_uid != va.va_uid) {
1517 error = priv_check_cred(cred, PRIV_VFS_LINK);
1522 if (hardlink_check_gid && !groupmember(va.va_gid, cred)) {
1523 error = priv_check_cred(cred, PRIV_VFS_LINK);
1532 kern_linkat(struct thread *td, int fd1, int fd2, const char *path1,
1533 const char *path2, enum uio_seg segflag, int follow)
1535 struct nameidata nd;
1540 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, segflag,
1541 path1, fd1, &cap_linkat_source_rights, td);
1542 if ((error = namei(&nd)) != 0)
1544 NDFREE(&nd, NDF_ONLY_PNBUF);
1545 error = kern_linkat_vp(td, nd.ni_vp, fd2, path2, segflag);
1546 } while (error == EAGAIN);
1551 kern_linkat_vp(struct thread *td, struct vnode *vp, int fd, const char *path,
1552 enum uio_seg segflag)
1554 struct nameidata nd;
1558 if (vp->v_type == VDIR) {
1560 return (EPERM); /* POSIX */
1562 NDINIT_ATRIGHTS(&nd, CREATE,
1563 LOCKPARENT | SAVENAME | AUDITVNODE2 | NOCACHE, segflag, path, fd,
1564 &cap_linkat_target_rights, td);
1565 if ((error = namei(&nd)) == 0) {
1566 if (nd.ni_vp != NULL) {
1567 NDFREE(&nd, NDF_ONLY_PNBUF);
1568 if (nd.ni_dvp == nd.ni_vp)
1575 } else if (nd.ni_dvp->v_mount != vp->v_mount) {
1577 * Cross-device link. No need to recheck
1578 * vp->v_type, since it cannot change, except
1581 NDFREE(&nd, NDF_ONLY_PNBUF);
1585 } else if ((error = vn_lock(vp, LK_EXCLUSIVE)) == 0) {
1586 error = can_hardlink(vp, td->td_ucred);
1589 error = mac_vnode_check_link(td->td_ucred,
1590 nd.ni_dvp, vp, &nd.ni_cnd);
1595 NDFREE(&nd, NDF_ONLY_PNBUF);
1598 error = vn_start_write(vp, &mp, V_NOWAIT);
1602 NDFREE(&nd, NDF_ONLY_PNBUF);
1603 error = vn_start_write(NULL, &mp,
1609 error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
1612 vn_finished_write(mp);
1613 NDFREE(&nd, NDF_ONLY_PNBUF);
1616 NDFREE(&nd, NDF_ONLY_PNBUF);
1626 * Make a symbolic link.
1628 #ifndef _SYS_SYSPROTO_H_
1629 struct symlink_args {
1635 sys_symlink(struct thread *td, struct symlink_args *uap)
1638 return (kern_symlinkat(td, uap->path, AT_FDCWD, uap->link,
1642 #ifndef _SYS_SYSPROTO_H_
1643 struct symlinkat_args {
1650 sys_symlinkat(struct thread *td, struct symlinkat_args *uap)
1653 return (kern_symlinkat(td, uap->path1, uap->fd, uap->path2,
1658 kern_symlinkat(struct thread *td, const char *path1, int fd, const char *path2,
1659 enum uio_seg segflg)
1663 const char *syspath;
1665 struct nameidata nd;
1668 if (segflg == UIO_SYSSPACE) {
1671 tmppath = uma_zalloc(namei_zone, M_WAITOK);
1672 if ((error = copyinstr(path1, tmppath, MAXPATHLEN, NULL)) != 0)
1676 AUDIT_ARG_TEXT(syspath);
1679 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
1680 NOCACHE, segflg, path2, fd, &cap_symlinkat_rights,
1682 if ((error = namei(&nd)) != 0)
1685 NDFREE(&nd, NDF_ONLY_PNBUF);
1686 if (nd.ni_vp == nd.ni_dvp)
1694 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1695 NDFREE(&nd, NDF_ONLY_PNBUF);
1697 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1702 vattr.va_mode = ACCESSPERMS &~ td->td_proc->p_fd->fd_cmask;
1704 vattr.va_type = VLNK;
1705 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1710 error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr, syspath);
1716 NDFREE(&nd, NDF_ONLY_PNBUF);
1718 vn_finished_write(mp);
1720 if (segflg != UIO_SYSSPACE)
1721 uma_zfree(namei_zone, tmppath);
1726 * Delete a whiteout from the filesystem.
1728 #ifndef _SYS_SYSPROTO_H_
1729 struct undelete_args {
1734 sys_undelete(struct thread *td, struct undelete_args *uap)
1737 struct nameidata nd;
1742 NDINIT(&nd, DELETE, LOCKPARENT | DOWHITEOUT | AUDITVNODE1,
1743 UIO_USERSPACE, uap->path, td);
1748 if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & ISWHITEOUT)) {
1749 NDFREE(&nd, NDF_ONLY_PNBUF);
1750 if (nd.ni_vp == nd.ni_dvp)
1758 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1759 NDFREE(&nd, NDF_ONLY_PNBUF);
1761 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1765 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, DELETE);
1766 NDFREE(&nd, NDF_ONLY_PNBUF);
1768 vn_finished_write(mp);
1773 * Delete a name from the filesystem.
1775 #ifndef _SYS_SYSPROTO_H_
1776 struct unlink_args {
1781 sys_unlink(struct thread *td, struct unlink_args *uap)
1784 return (kern_funlinkat(td, AT_FDCWD, uap->path, FD_NONE, UIO_USERSPACE,
1789 kern_funlinkat_ex(struct thread *td, int dfd, const char *path, int fd,
1790 int flag, enum uio_seg pathseg, ino_t oldinum)
1793 if ((flag & ~AT_REMOVEDIR) != 0)
1796 if ((flag & AT_REMOVEDIR) != 0)
1797 return (kern_frmdirat(td, dfd, path, fd, UIO_USERSPACE, 0));
1799 return (kern_funlinkat(td, dfd, path, fd, UIO_USERSPACE, 0, 0));
1802 #ifndef _SYS_SYSPROTO_H_
1803 struct unlinkat_args {
1810 sys_unlinkat(struct thread *td, struct unlinkat_args *uap)
1813 return (kern_funlinkat_ex(td, uap->fd, uap->path, FD_NONE, uap->flag,
1817 #ifndef _SYS_SYSPROTO_H_
1818 struct funlinkat_args {
1826 sys_funlinkat(struct thread *td, struct funlinkat_args *uap)
1829 return (kern_funlinkat_ex(td, uap->dfd, uap->path, uap->fd, uap->flag,
1834 kern_funlinkat(struct thread *td, int dfd, const char *path, int fd,
1835 enum uio_seg pathseg, int flag, ino_t oldinum)
1840 struct nameidata nd;
1845 if (fd != FD_NONE) {
1846 error = getvnode(td, fd, &cap_no_rights, &fp);
1853 NDINIT_ATRIGHTS(&nd, DELETE, LOCKPARENT | LOCKLEAF | AUDITVNODE1 |
1854 ((flag & AT_BENEATH) != 0 ? BENEATH : 0),
1855 pathseg, path, dfd, &cap_unlinkat_rights, td);
1856 if ((error = namei(&nd)) != 0) {
1857 if (error == EINVAL)
1862 if (vp->v_type == VDIR && oldinum == 0) {
1863 error = EPERM; /* POSIX */
1864 } else if (oldinum != 0 &&
1865 ((error = VOP_STAT(vp, &sb, td->td_ucred, NOCRED, td)) == 0) &&
1866 sb.st_ino != oldinum) {
1867 error = EIDRM; /* Identifier removed */
1868 } else if (fp != NULL && fp->f_vnode != vp) {
1869 if (VN_IS_DOOMED(fp->f_vnode))
1875 * The root of a mounted filesystem cannot be deleted.
1877 * XXX: can this only be a VDIR case?
1879 if (vp->v_vflag & VV_ROOT)
1883 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1884 NDFREE(&nd, NDF_ONLY_PNBUF);
1886 if (vp == nd.ni_dvp)
1890 if ((error = vn_start_write(NULL, &mp,
1891 V_XSLEEP | PCATCH)) != 0) {
1897 error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp,
1902 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_UNLINK);
1903 error = VOP_REMOVE(nd.ni_dvp, vp, &nd.ni_cnd);
1907 vn_finished_write(mp);
1909 NDFREE(&nd, NDF_ONLY_PNBUF);
1911 if (vp == nd.ni_dvp)
1922 * Reposition read/write file offset.
1924 #ifndef _SYS_SYSPROTO_H_
1933 sys_lseek(struct thread *td, struct lseek_args *uap)
1936 return (kern_lseek(td, uap->fd, uap->offset, uap->whence));
1940 kern_lseek(struct thread *td, int fd, off_t offset, int whence)
1946 error = fget(td, fd, &cap_seek_rights, &fp);
1949 error = (fp->f_ops->fo_flags & DFLAG_SEEKABLE) != 0 ?
1950 fo_seek(fp, offset, whence, td) : ESPIPE;
1955 #if defined(COMPAT_43)
1957 * Reposition read/write file offset.
1959 #ifndef _SYS_SYSPROTO_H_
1960 struct olseek_args {
1967 olseek(struct thread *td, struct olseek_args *uap)
1970 return (kern_lseek(td, uap->fd, uap->offset, uap->whence));
1972 #endif /* COMPAT_43 */
1974 #if defined(COMPAT_FREEBSD6)
1975 /* Version with the 'pad' argument */
1977 freebsd6_lseek(struct thread *td, struct freebsd6_lseek_args *uap)
1980 return (kern_lseek(td, uap->fd, uap->offset, uap->whence));
1985 * Check access permissions using passed credentials.
1988 vn_access(struct vnode *vp, int user_flags, struct ucred *cred,
1994 /* Flags == 0 means only check for existence. */
1995 if (user_flags == 0)
1999 if (user_flags & R_OK)
2001 if (user_flags & W_OK)
2003 if (user_flags & X_OK)
2006 error = mac_vnode_check_access(cred, vp, accmode);
2010 if ((accmode & VWRITE) == 0 || (error = vn_writechk(vp)) == 0)
2011 error = VOP_ACCESS(vp, accmode, cred, td);
2016 * Check access permissions using "real" credentials.
2018 #ifndef _SYS_SYSPROTO_H_
2019 struct access_args {
2025 sys_access(struct thread *td, struct access_args *uap)
2028 return (kern_accessat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2032 #ifndef _SYS_SYSPROTO_H_
2033 struct faccessat_args {
2041 sys_faccessat(struct thread *td, struct faccessat_args *uap)
2044 return (kern_accessat(td, uap->fd, uap->path, UIO_USERSPACE, uap->flag,
2049 kern_accessat(struct thread *td, int fd, const char *path,
2050 enum uio_seg pathseg, int flag, int amode)
2052 struct ucred *cred, *usecred;
2054 struct nameidata nd;
2057 if ((flag & ~(AT_EACCESS | AT_BENEATH)) != 0)
2059 if (amode != F_OK && (amode & ~(R_OK | W_OK | X_OK)) != 0)
2063 * Create and modify a temporary credential instead of one that
2064 * is potentially shared (if we need one).
2066 cred = td->td_ucred;
2067 if ((flag & AT_EACCESS) == 0 &&
2068 ((cred->cr_uid != cred->cr_ruid ||
2069 cred->cr_rgid != cred->cr_groups[0]))) {
2070 usecred = crdup(cred);
2071 usecred->cr_uid = cred->cr_ruid;
2072 usecred->cr_groups[0] = cred->cr_rgid;
2073 td->td_ucred = usecred;
2076 AUDIT_ARG_VALUE(amode);
2077 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF |
2078 AUDITVNODE1 | ((flag & AT_BENEATH) != 0 ? BENEATH : 0),
2079 pathseg, path, fd, &cap_fstat_rights, td);
2080 if ((error = namei(&nd)) != 0)
2084 error = vn_access(vp, amode, usecred, td);
2085 NDFREE(&nd, NDF_ONLY_PNBUF);
2088 if (usecred != cred) {
2089 td->td_ucred = cred;
2096 * Check access permissions using "effective" credentials.
2098 #ifndef _SYS_SYSPROTO_H_
2099 struct eaccess_args {
2105 sys_eaccess(struct thread *td, struct eaccess_args *uap)
2108 return (kern_accessat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2109 AT_EACCESS, uap->amode));
2112 #if defined(COMPAT_43)
2114 * Get file status; this version follows links.
2116 #ifndef _SYS_SYSPROTO_H_
2123 ostat(struct thread *td, struct ostat_args *uap)
2129 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
2134 return (copyout(&osb, uap->ub, sizeof (osb)));
2138 * Get file status; this version does not follow links.
2140 #ifndef _SYS_SYSPROTO_H_
2141 struct olstat_args {
2147 olstat(struct thread *td, struct olstat_args *uap)
2153 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2154 UIO_USERSPACE, &sb, NULL);
2158 return (copyout(&osb, uap->ub, sizeof (osb)));
2162 * Convert from an old to a new stat structure.
2163 * XXX: many values are blindly truncated.
2166 cvtstat(struct stat *st, struct ostat *ost)
2169 bzero(ost, sizeof(*ost));
2170 ost->st_dev = st->st_dev;
2171 ost->st_ino = st->st_ino;
2172 ost->st_mode = st->st_mode;
2173 ost->st_nlink = st->st_nlink;
2174 ost->st_uid = st->st_uid;
2175 ost->st_gid = st->st_gid;
2176 ost->st_rdev = st->st_rdev;
2177 ost->st_size = MIN(st->st_size, INT32_MAX);
2178 ost->st_atim = st->st_atim;
2179 ost->st_mtim = st->st_mtim;
2180 ost->st_ctim = st->st_ctim;
2181 ost->st_blksize = st->st_blksize;
2182 ost->st_blocks = st->st_blocks;
2183 ost->st_flags = st->st_flags;
2184 ost->st_gen = st->st_gen;
2186 #endif /* COMPAT_43 */
2188 #if defined(COMPAT_43) || defined(COMPAT_FREEBSD11)
2189 int ino64_trunc_error;
2190 SYSCTL_INT(_vfs, OID_AUTO, ino64_trunc_error, CTLFLAG_RW,
2191 &ino64_trunc_error, 0,
2192 "Error on truncation of device, file or inode number, or link count");
2195 freebsd11_cvtstat(struct stat *st, struct freebsd11_stat *ost)
2198 ost->st_dev = st->st_dev;
2199 if (ost->st_dev != st->st_dev) {
2200 switch (ino64_trunc_error) {
2203 * Since dev_t is almost raw, don't clamp to the
2204 * maximum for case 2, but ignore the error.
2211 ost->st_ino = st->st_ino;
2212 if (ost->st_ino != st->st_ino) {
2213 switch (ino64_trunc_error) {
2220 ost->st_ino = UINT32_MAX;
2224 ost->st_mode = st->st_mode;
2225 ost->st_nlink = st->st_nlink;
2226 if (ost->st_nlink != st->st_nlink) {
2227 switch (ino64_trunc_error) {
2234 ost->st_nlink = UINT16_MAX;
2238 ost->st_uid = st->st_uid;
2239 ost->st_gid = st->st_gid;
2240 ost->st_rdev = st->st_rdev;
2241 if (ost->st_rdev != st->st_rdev) {
2242 switch (ino64_trunc_error) {
2249 ost->st_atim = st->st_atim;
2250 ost->st_mtim = st->st_mtim;
2251 ost->st_ctim = st->st_ctim;
2252 ost->st_size = st->st_size;
2253 ost->st_blocks = st->st_blocks;
2254 ost->st_blksize = st->st_blksize;
2255 ost->st_flags = st->st_flags;
2256 ost->st_gen = st->st_gen;
2258 ost->st_birthtim = st->st_birthtim;
2259 bzero((char *)&ost->st_birthtim + sizeof(ost->st_birthtim),
2260 sizeof(*ost) - offsetof(struct freebsd11_stat,
2261 st_birthtim) - sizeof(ost->st_birthtim));
2266 freebsd11_stat(struct thread *td, struct freebsd11_stat_args* uap)
2269 struct freebsd11_stat osb;
2272 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
2276 error = freebsd11_cvtstat(&sb, &osb);
2278 error = copyout(&osb, uap->ub, sizeof(osb));
2283 freebsd11_lstat(struct thread *td, struct freebsd11_lstat_args* uap)
2286 struct freebsd11_stat osb;
2289 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2290 UIO_USERSPACE, &sb, NULL);
2293 error = freebsd11_cvtstat(&sb, &osb);
2295 error = copyout(&osb, uap->ub, sizeof(osb));
2300 freebsd11_fhstat(struct thread *td, struct freebsd11_fhstat_args* uap)
2304 struct freebsd11_stat osb;
2307 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
2310 error = kern_fhstat(td, fh, &sb);
2313 error = freebsd11_cvtstat(&sb, &osb);
2315 error = copyout(&osb, uap->sb, sizeof(osb));
2320 freebsd11_fstatat(struct thread *td, struct freebsd11_fstatat_args* uap)
2323 struct freebsd11_stat osb;
2326 error = kern_statat(td, uap->flag, uap->fd, uap->path,
2327 UIO_USERSPACE, &sb, NULL);
2330 error = freebsd11_cvtstat(&sb, &osb);
2332 error = copyout(&osb, uap->buf, sizeof(osb));
2335 #endif /* COMPAT_FREEBSD11 */
2340 #ifndef _SYS_SYSPROTO_H_
2341 struct fstatat_args {
2349 sys_fstatat(struct thread *td, struct fstatat_args *uap)
2354 error = kern_statat(td, uap->flag, uap->fd, uap->path,
2355 UIO_USERSPACE, &sb, NULL);
2357 error = copyout(&sb, uap->buf, sizeof (sb));
2362 kern_statat(struct thread *td, int flag, int fd, const char *path,
2363 enum uio_seg pathseg, struct stat *sbp,
2364 void (*hook)(struct vnode *vp, struct stat *sbp))
2366 struct nameidata nd;
2369 if ((flag & ~(AT_SYMLINK_NOFOLLOW | AT_BENEATH)) != 0)
2372 NDINIT_ATRIGHTS(&nd, LOOKUP, ((flag & AT_SYMLINK_NOFOLLOW) != 0 ?
2373 NOFOLLOW : FOLLOW) | ((flag & AT_BENEATH) != 0 ? BENEATH : 0) |
2374 LOCKSHARED | LOCKLEAF | AUDITVNODE1, pathseg, path, fd,
2375 &cap_fstat_rights, td);
2377 if ((error = namei(&nd)) != 0)
2379 error = VOP_STAT(nd.ni_vp, sbp, td->td_ucred, NOCRED, td);
2381 if (__predict_false(hook != NULL))
2382 hook(nd.ni_vp, sbp);
2384 NDFREE(&nd, NDF_ONLY_PNBUF);
2386 #ifdef __STAT_TIME_T_EXT
2387 sbp->st_atim_ext = 0;
2388 sbp->st_mtim_ext = 0;
2389 sbp->st_ctim_ext = 0;
2390 sbp->st_btim_ext = 0;
2393 if (KTRPOINT(td, KTR_STRUCT))
2394 ktrstat_error(sbp, error);
2399 #if defined(COMPAT_FREEBSD11)
2401 * Implementation of the NetBSD [l]stat() functions.
2404 freebsd11_cvtnstat(struct stat *sb, struct nstat *nsb)
2407 bzero(nsb, sizeof(*nsb));
2408 nsb->st_dev = sb->st_dev;
2409 nsb->st_ino = sb->st_ino;
2410 nsb->st_mode = sb->st_mode;
2411 nsb->st_nlink = sb->st_nlink;
2412 nsb->st_uid = sb->st_uid;
2413 nsb->st_gid = sb->st_gid;
2414 nsb->st_rdev = sb->st_rdev;
2415 nsb->st_atim = sb->st_atim;
2416 nsb->st_mtim = sb->st_mtim;
2417 nsb->st_ctim = sb->st_ctim;
2418 nsb->st_size = sb->st_size;
2419 nsb->st_blocks = sb->st_blocks;
2420 nsb->st_blksize = sb->st_blksize;
2421 nsb->st_flags = sb->st_flags;
2422 nsb->st_gen = sb->st_gen;
2423 nsb->st_birthtim = sb->st_birthtim;
2426 #ifndef _SYS_SYSPROTO_H_
2427 struct freebsd11_nstat_args {
2433 freebsd11_nstat(struct thread *td, struct freebsd11_nstat_args *uap)
2439 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
2443 freebsd11_cvtnstat(&sb, &nsb);
2444 return (copyout(&nsb, uap->ub, sizeof (nsb)));
2448 * NetBSD lstat. Get file status; this version does not follow links.
2450 #ifndef _SYS_SYSPROTO_H_
2451 struct freebsd11_nlstat_args {
2457 freebsd11_nlstat(struct thread *td, struct freebsd11_nlstat_args *uap)
2463 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2464 UIO_USERSPACE, &sb, NULL);
2467 freebsd11_cvtnstat(&sb, &nsb);
2468 return (copyout(&nsb, uap->ub, sizeof (nsb)));
2470 #endif /* COMPAT_FREEBSD11 */
2473 * Get configurable pathname variables.
2475 #ifndef _SYS_SYSPROTO_H_
2476 struct pathconf_args {
2482 sys_pathconf(struct thread *td, struct pathconf_args *uap)
2487 error = kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name, FOLLOW,
2490 td->td_retval[0] = value;
2494 #ifndef _SYS_SYSPROTO_H_
2495 struct lpathconf_args {
2501 sys_lpathconf(struct thread *td, struct lpathconf_args *uap)
2506 error = kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name,
2509 td->td_retval[0] = value;
2514 kern_pathconf(struct thread *td, const char *path, enum uio_seg pathseg,
2515 int name, u_long flags, long *valuep)
2517 struct nameidata nd;
2520 NDINIT(&nd, LOOKUP, LOCKSHARED | LOCKLEAF | AUDITVNODE1 | flags,
2522 if ((error = namei(&nd)) != 0)
2524 NDFREE(&nd, NDF_ONLY_PNBUF);
2526 error = VOP_PATHCONF(nd.ni_vp, name, valuep);
2532 * Return target name of a symbolic link.
2534 #ifndef _SYS_SYSPROTO_H_
2535 struct readlink_args {
2542 sys_readlink(struct thread *td, struct readlink_args *uap)
2545 return (kern_readlinkat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2546 uap->buf, UIO_USERSPACE, uap->count));
2548 #ifndef _SYS_SYSPROTO_H_
2549 struct readlinkat_args {
2557 sys_readlinkat(struct thread *td, struct readlinkat_args *uap)
2560 return (kern_readlinkat(td, uap->fd, uap->path, UIO_USERSPACE,
2561 uap->buf, UIO_USERSPACE, uap->bufsize));
2565 kern_readlinkat(struct thread *td, int fd, const char *path,
2566 enum uio_seg pathseg, char *buf, enum uio_seg bufseg, size_t count)
2569 struct nameidata nd;
2572 if (count > IOSIZE_MAX)
2575 NDINIT_AT(&nd, LOOKUP, NOFOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
2576 pathseg, path, fd, td);
2578 if ((error = namei(&nd)) != 0)
2580 NDFREE(&nd, NDF_ONLY_PNBUF);
2583 error = kern_readlink_vp(vp, buf, bufseg, count, td);
2590 * Helper function to readlink from a vnode
2593 kern_readlink_vp(struct vnode *vp, char *buf, enum uio_seg bufseg, size_t count,
2600 ASSERT_VOP_LOCKED(vp, "kern_readlink_vp(): vp not locked");
2602 error = mac_vnode_check_readlink(td->td_ucred, vp);
2606 if (vp->v_type != VLNK && (vp->v_vflag & VV_READLINK) == 0)
2609 aiov.iov_base = buf;
2610 aiov.iov_len = count;
2611 auio.uio_iov = &aiov;
2612 auio.uio_iovcnt = 1;
2613 auio.uio_offset = 0;
2614 auio.uio_rw = UIO_READ;
2615 auio.uio_segflg = bufseg;
2617 auio.uio_resid = count;
2618 error = VOP_READLINK(vp, &auio, td->td_ucred);
2619 td->td_retval[0] = count - auio.uio_resid;
2624 * Common implementation code for chflags() and fchflags().
2627 setfflags(struct thread *td, struct vnode *vp, u_long flags)
2633 /* We can't support the value matching VNOVAL. */
2634 if (flags == VNOVAL)
2635 return (EOPNOTSUPP);
2638 * Prevent non-root users from setting flags on devices. When
2639 * a device is reused, users can retain ownership of the device
2640 * if they are allowed to set flags and programs assume that
2641 * chown can't fail when done as root.
2643 if (vp->v_type == VCHR || vp->v_type == VBLK) {
2644 error = priv_check(td, PRIV_VFS_CHFLAGS_DEV);
2649 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2652 vattr.va_flags = flags;
2653 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2655 error = mac_vnode_check_setflags(td->td_ucred, vp, vattr.va_flags);
2658 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
2660 vn_finished_write(mp);
2665 * Change flags of a file given a path name.
2667 #ifndef _SYS_SYSPROTO_H_
2668 struct chflags_args {
2674 sys_chflags(struct thread *td, struct chflags_args *uap)
2677 return (kern_chflagsat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2681 #ifndef _SYS_SYSPROTO_H_
2682 struct chflagsat_args {
2690 sys_chflagsat(struct thread *td, struct chflagsat_args *uap)
2693 if ((uap->atflag & ~(AT_SYMLINK_NOFOLLOW | AT_BENEATH)) != 0)
2696 return (kern_chflagsat(td, uap->fd, uap->path, UIO_USERSPACE,
2697 uap->flags, uap->atflag));
2701 * Same as chflags() but doesn't follow symlinks.
2703 #ifndef _SYS_SYSPROTO_H_
2704 struct lchflags_args {
2710 sys_lchflags(struct thread *td, struct lchflags_args *uap)
2713 return (kern_chflagsat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2714 uap->flags, AT_SYMLINK_NOFOLLOW));
2718 kern_chflagsat(struct thread *td, int fd, const char *path,
2719 enum uio_seg pathseg, u_long flags, int atflag)
2721 struct nameidata nd;
2724 AUDIT_ARG_FFLAGS(flags);
2725 follow = (atflag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
2726 follow |= (atflag & AT_BENEATH) != 0 ? BENEATH : 0;
2727 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, pathseg, path, fd,
2728 &cap_fchflags_rights, td);
2729 if ((error = namei(&nd)) != 0)
2731 NDFREE(&nd, NDF_ONLY_PNBUF);
2732 error = setfflags(td, nd.ni_vp, flags);
2738 * Change flags of a file given a file descriptor.
2740 #ifndef _SYS_SYSPROTO_H_
2741 struct fchflags_args {
2747 sys_fchflags(struct thread *td, struct fchflags_args *uap)
2752 AUDIT_ARG_FD(uap->fd);
2753 AUDIT_ARG_FFLAGS(uap->flags);
2754 error = getvnode(td, uap->fd, &cap_fchflags_rights,
2759 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
2760 AUDIT_ARG_VNODE1(fp->f_vnode);
2761 VOP_UNLOCK(fp->f_vnode);
2763 error = setfflags(td, fp->f_vnode, uap->flags);
2769 * Common implementation code for chmod(), lchmod() and fchmod().
2772 setfmode(struct thread *td, struct ucred *cred, struct vnode *vp, int mode)
2778 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2780 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2782 vattr.va_mode = mode & ALLPERMS;
2784 error = mac_vnode_check_setmode(cred, vp, vattr.va_mode);
2787 error = VOP_SETATTR(vp, &vattr, cred);
2789 vn_finished_write(mp);
2794 * Change mode of a file given path name.
2796 #ifndef _SYS_SYSPROTO_H_
2803 sys_chmod(struct thread *td, struct chmod_args *uap)
2806 return (kern_fchmodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2810 #ifndef _SYS_SYSPROTO_H_
2811 struct fchmodat_args {
2819 sys_fchmodat(struct thread *td, struct fchmodat_args *uap)
2822 if ((uap->flag & ~(AT_SYMLINK_NOFOLLOW | AT_BENEATH)) != 0)
2825 return (kern_fchmodat(td, uap->fd, uap->path, UIO_USERSPACE,
2826 uap->mode, uap->flag));
2830 * Change mode of a file given path name (don't follow links.)
2832 #ifndef _SYS_SYSPROTO_H_
2833 struct lchmod_args {
2839 sys_lchmod(struct thread *td, struct lchmod_args *uap)
2842 return (kern_fchmodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2843 uap->mode, AT_SYMLINK_NOFOLLOW));
2847 kern_fchmodat(struct thread *td, int fd, const char *path,
2848 enum uio_seg pathseg, mode_t mode, int flag)
2850 struct nameidata nd;
2853 AUDIT_ARG_MODE(mode);
2854 follow = (flag & AT_SYMLINK_NOFOLLOW) != 0 ? NOFOLLOW : FOLLOW;
2855 follow |= (flag & AT_BENEATH) != 0 ? BENEATH : 0;
2856 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, pathseg, path, fd,
2857 &cap_fchmod_rights, td);
2858 if ((error = namei(&nd)) != 0)
2860 NDFREE(&nd, NDF_ONLY_PNBUF);
2861 error = setfmode(td, td->td_ucred, nd.ni_vp, mode);
2867 * Change mode of a file given a file descriptor.
2869 #ifndef _SYS_SYSPROTO_H_
2870 struct fchmod_args {
2876 sys_fchmod(struct thread *td, struct fchmod_args *uap)
2881 AUDIT_ARG_FD(uap->fd);
2882 AUDIT_ARG_MODE(uap->mode);
2884 error = fget(td, uap->fd, &cap_fchmod_rights, &fp);
2887 error = fo_chmod(fp, uap->mode, td->td_ucred, td);
2893 * Common implementation for chown(), lchown(), and fchown()
2896 setfown(struct thread *td, struct ucred *cred, struct vnode *vp, uid_t uid,
2903 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2905 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2910 error = mac_vnode_check_setowner(cred, vp, vattr.va_uid,
2914 error = VOP_SETATTR(vp, &vattr, cred);
2916 vn_finished_write(mp);
2921 * Set ownership given a path name.
2923 #ifndef _SYS_SYSPROTO_H_
2931 sys_chown(struct thread *td, struct chown_args *uap)
2934 return (kern_fchownat(td, AT_FDCWD, uap->path, UIO_USERSPACE, uap->uid,
2938 #ifndef _SYS_SYSPROTO_H_
2939 struct fchownat_args {
2948 sys_fchownat(struct thread *td, struct fchownat_args *uap)
2951 if ((uap->flag & ~(AT_SYMLINK_NOFOLLOW | AT_BENEATH)) != 0)
2954 return (kern_fchownat(td, uap->fd, uap->path, UIO_USERSPACE, uap->uid,
2955 uap->gid, uap->flag));
2959 kern_fchownat(struct thread *td, int fd, const char *path,
2960 enum uio_seg pathseg, int uid, int gid, int flag)
2962 struct nameidata nd;
2965 AUDIT_ARG_OWNER(uid, gid);
2966 follow = (flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
2967 follow |= (flag & AT_BENEATH) != 0 ? BENEATH : 0;
2968 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, pathseg, path, fd,
2969 &cap_fchown_rights, td);
2971 if ((error = namei(&nd)) != 0)
2973 NDFREE(&nd, NDF_ONLY_PNBUF);
2974 error = setfown(td, td->td_ucred, nd.ni_vp, uid, gid);
2980 * Set ownership given a path name, do not cross symlinks.
2982 #ifndef _SYS_SYSPROTO_H_
2983 struct lchown_args {
2990 sys_lchown(struct thread *td, struct lchown_args *uap)
2993 return (kern_fchownat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2994 uap->uid, uap->gid, AT_SYMLINK_NOFOLLOW));
2998 * Set ownership given a file descriptor.
3000 #ifndef _SYS_SYSPROTO_H_
3001 struct fchown_args {
3008 sys_fchown(struct thread *td, struct fchown_args *uap)
3013 AUDIT_ARG_FD(uap->fd);
3014 AUDIT_ARG_OWNER(uap->uid, uap->gid);
3015 error = fget(td, uap->fd, &cap_fchown_rights, &fp);
3018 error = fo_chown(fp, uap->uid, uap->gid, td->td_ucred, td);
3024 * Common implementation code for utimes(), lutimes(), and futimes().
3027 getutimes(const struct timeval *usrtvp, enum uio_seg tvpseg,
3028 struct timespec *tsp)
3030 struct timeval tv[2];
3031 const struct timeval *tvp;
3034 if (usrtvp == NULL) {
3035 vfs_timestamp(&tsp[0]);
3038 if (tvpseg == UIO_SYSSPACE) {
3041 if ((error = copyin(usrtvp, tv, sizeof(tv))) != 0)
3046 if (tvp[0].tv_usec < 0 || tvp[0].tv_usec >= 1000000 ||
3047 tvp[1].tv_usec < 0 || tvp[1].tv_usec >= 1000000)
3049 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
3050 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
3056 * Common implementation code for futimens(), utimensat().
3058 #define UTIMENS_NULL 0x1
3059 #define UTIMENS_EXIT 0x2
3061 getutimens(const struct timespec *usrtsp, enum uio_seg tspseg,
3062 struct timespec *tsp, int *retflags)
3064 struct timespec tsnow;
3067 vfs_timestamp(&tsnow);
3069 if (usrtsp == NULL) {
3072 *retflags |= UTIMENS_NULL;
3075 if (tspseg == UIO_SYSSPACE) {
3078 } else if ((error = copyin(usrtsp, tsp, sizeof(*tsp) * 2)) != 0)
3080 if (tsp[0].tv_nsec == UTIME_OMIT && tsp[1].tv_nsec == UTIME_OMIT)
3081 *retflags |= UTIMENS_EXIT;
3082 if (tsp[0].tv_nsec == UTIME_NOW && tsp[1].tv_nsec == UTIME_NOW)
3083 *retflags |= UTIMENS_NULL;
3084 if (tsp[0].tv_nsec == UTIME_OMIT)
3085 tsp[0].tv_sec = VNOVAL;
3086 else if (tsp[0].tv_nsec == UTIME_NOW)
3088 else if (tsp[0].tv_nsec < 0 || tsp[0].tv_nsec >= 1000000000L)
3090 if (tsp[1].tv_nsec == UTIME_OMIT)
3091 tsp[1].tv_sec = VNOVAL;
3092 else if (tsp[1].tv_nsec == UTIME_NOW)
3094 else if (tsp[1].tv_nsec < 0 || tsp[1].tv_nsec >= 1000000000L)
3101 * Common implementation code for utimes(), lutimes(), futimes(), futimens(),
3105 setutimes(struct thread *td, struct vnode *vp, const struct timespec *ts,
3106 int numtimes, int nullflag)
3110 int error, setbirthtime;
3112 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
3114 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3116 if (numtimes < 3 && !VOP_GETATTR(vp, &vattr, td->td_ucred) &&
3117 timespeccmp(&ts[1], &vattr.va_birthtime, < ))
3120 vattr.va_atime = ts[0];
3121 vattr.va_mtime = ts[1];
3123 vattr.va_birthtime = ts[1];
3125 vattr.va_birthtime = ts[2];
3127 vattr.va_vaflags |= VA_UTIMES_NULL;
3129 error = mac_vnode_check_setutimes(td->td_ucred, vp, vattr.va_atime,
3133 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3135 vn_finished_write(mp);
3140 * Set the access and modification times of a file.
3142 #ifndef _SYS_SYSPROTO_H_
3143 struct utimes_args {
3145 struct timeval *tptr;
3149 sys_utimes(struct thread *td, struct utimes_args *uap)
3152 return (kern_utimesat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
3153 uap->tptr, UIO_USERSPACE));
3156 #ifndef _SYS_SYSPROTO_H_
3157 struct futimesat_args {
3160 const struct timeval * times;
3164 sys_futimesat(struct thread *td, struct futimesat_args *uap)
3167 return (kern_utimesat(td, uap->fd, uap->path, UIO_USERSPACE,
3168 uap->times, UIO_USERSPACE));
3172 kern_utimesat(struct thread *td, int fd, const char *path,
3173 enum uio_seg pathseg, struct timeval *tptr, enum uio_seg tptrseg)
3175 struct nameidata nd;
3176 struct timespec ts[2];
3179 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3181 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, fd,
3182 &cap_futimes_rights, td);
3184 if ((error = namei(&nd)) != 0)
3186 NDFREE(&nd, NDF_ONLY_PNBUF);
3187 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3193 * Set the access and modification times of a file.
3195 #ifndef _SYS_SYSPROTO_H_
3196 struct lutimes_args {
3198 struct timeval *tptr;
3202 sys_lutimes(struct thread *td, struct lutimes_args *uap)
3205 return (kern_lutimes(td, uap->path, UIO_USERSPACE, uap->tptr,
3210 kern_lutimes(struct thread *td, const char *path, enum uio_seg pathseg,
3211 struct timeval *tptr, enum uio_seg tptrseg)
3213 struct timespec ts[2];
3214 struct nameidata nd;
3217 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3219 NDINIT(&nd, LOOKUP, NOFOLLOW | AUDITVNODE1, pathseg, path, 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 futimes_args {
3234 struct timeval *tptr;
3238 sys_futimes(struct thread *td, struct futimes_args *uap)
3241 return (kern_futimes(td, uap->fd, uap->tptr, UIO_USERSPACE));
3245 kern_futimes(struct thread *td, int fd, struct timeval *tptr,
3246 enum uio_seg tptrseg)
3248 struct timespec ts[2];
3253 error = getutimes(tptr, tptrseg, ts);
3256 error = getvnode(td, fd, &cap_futimes_rights, &fp);
3260 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
3261 AUDIT_ARG_VNODE1(fp->f_vnode);
3262 VOP_UNLOCK(fp->f_vnode);
3264 error = setutimes(td, fp->f_vnode, ts, 2, tptr == NULL);
3270 sys_futimens(struct thread *td, struct futimens_args *uap)
3273 return (kern_futimens(td, uap->fd, uap->times, UIO_USERSPACE));
3277 kern_futimens(struct thread *td, int fd, struct timespec *tptr,
3278 enum uio_seg tptrseg)
3280 struct timespec ts[2];
3285 error = getutimens(tptr, tptrseg, ts, &flags);
3288 if (flags & UTIMENS_EXIT)
3290 error = getvnode(td, fd, &cap_futimes_rights, &fp);
3294 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
3295 AUDIT_ARG_VNODE1(fp->f_vnode);
3296 VOP_UNLOCK(fp->f_vnode);
3298 error = setutimes(td, fp->f_vnode, ts, 2, flags & UTIMENS_NULL);
3304 sys_utimensat(struct thread *td, struct utimensat_args *uap)
3307 return (kern_utimensat(td, uap->fd, uap->path, UIO_USERSPACE,
3308 uap->times, UIO_USERSPACE, uap->flag));
3312 kern_utimensat(struct thread *td, int fd, const char *path,
3313 enum uio_seg pathseg, struct timespec *tptr, enum uio_seg tptrseg,
3316 struct nameidata nd;
3317 struct timespec ts[2];
3320 if ((flag & ~(AT_SYMLINK_NOFOLLOW | AT_BENEATH)) != 0)
3323 if ((error = getutimens(tptr, tptrseg, ts, &flags)) != 0)
3325 NDINIT_ATRIGHTS(&nd, LOOKUP, ((flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW :
3326 FOLLOW) | ((flag & AT_BENEATH) != 0 ? BENEATH : 0) | AUDITVNODE1,
3327 pathseg, path, fd, &cap_futimes_rights, td);
3328 if ((error = namei(&nd)) != 0)
3331 * We are allowed to call namei() regardless of 2xUTIME_OMIT.
3333 * "If both tv_nsec fields are UTIME_OMIT... EACCESS may be detected."
3334 * "Search permission is denied by a component of the path prefix."
3336 NDFREE(&nd, NDF_ONLY_PNBUF);
3337 if ((flags & UTIMENS_EXIT) == 0)
3338 error = setutimes(td, nd.ni_vp, ts, 2, flags & UTIMENS_NULL);
3344 * Truncate a file given its path name.
3346 #ifndef _SYS_SYSPROTO_H_
3347 struct truncate_args {
3354 sys_truncate(struct thread *td, struct truncate_args *uap)
3357 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3361 kern_truncate(struct thread *td, const char *path, enum uio_seg pathseg,
3368 struct nameidata nd;
3373 NDINIT(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, td);
3374 if ((error = namei(&nd)) != 0)
3377 rl_cookie = vn_rangelock_wlock(vp, 0, OFF_MAX);
3378 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0) {
3379 vn_rangelock_unlock(vp, rl_cookie);
3383 NDFREE(&nd, NDF_ONLY_PNBUF);
3384 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3385 if (vp->v_type == VDIR)
3388 else if ((error = mac_vnode_check_write(td->td_ucred, NOCRED, vp))) {
3391 else if ((error = vn_writechk(vp)) == 0 &&
3392 (error = VOP_ACCESS(vp, VWRITE, td->td_ucred, td)) == 0) {
3394 vattr.va_size = length;
3395 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3398 vn_finished_write(mp);
3399 vn_rangelock_unlock(vp, rl_cookie);
3404 #if defined(COMPAT_43)
3406 * Truncate a file given its path name.
3408 #ifndef _SYS_SYSPROTO_H_
3409 struct otruncate_args {
3415 otruncate(struct thread *td, struct otruncate_args *uap)
3418 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3420 #endif /* COMPAT_43 */
3422 #if defined(COMPAT_FREEBSD6)
3423 /* Versions with the pad argument */
3425 freebsd6_truncate(struct thread *td, struct freebsd6_truncate_args *uap)
3428 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3432 freebsd6_ftruncate(struct thread *td, struct freebsd6_ftruncate_args *uap)
3435 return (kern_ftruncate(td, uap->fd, uap->length));
3440 kern_fsync(struct thread *td, int fd, bool fullsync)
3445 int error, lock_flags;
3448 error = getvnode(td, fd, &cap_fsync_rights, &fp);
3454 /* XXXKIB: compete outstanding aio writes */;
3456 error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
3459 if (MNT_SHARED_WRITES(mp) ||
3460 ((mp == NULL) && MNT_SHARED_WRITES(vp->v_mount))) {
3461 lock_flags = LK_SHARED;
3463 lock_flags = LK_EXCLUSIVE;
3465 vn_lock(vp, lock_flags | LK_RETRY);
3466 AUDIT_ARG_VNODE1(vp);
3467 if (vp->v_object != NULL) {
3468 VM_OBJECT_WLOCK(vp->v_object);
3469 vm_object_page_clean(vp->v_object, 0, 0, 0);
3470 VM_OBJECT_WUNLOCK(vp->v_object);
3472 error = fullsync ? VOP_FSYNC(vp, MNT_WAIT, td) : VOP_FDATASYNC(vp, td);
3474 vn_finished_write(mp);
3481 * Sync an open file.
3483 #ifndef _SYS_SYSPROTO_H_
3489 sys_fsync(struct thread *td, struct fsync_args *uap)
3492 return (kern_fsync(td, uap->fd, true));
3496 sys_fdatasync(struct thread *td, struct fdatasync_args *uap)
3499 return (kern_fsync(td, uap->fd, false));
3503 * Rename files. Source and destination must either both be directories, or
3504 * both not be directories. If target is a directory, it must be empty.
3506 #ifndef _SYS_SYSPROTO_H_
3507 struct rename_args {
3513 sys_rename(struct thread *td, struct rename_args *uap)
3516 return (kern_renameat(td, AT_FDCWD, uap->from, AT_FDCWD,
3517 uap->to, UIO_USERSPACE));
3520 #ifndef _SYS_SYSPROTO_H_
3521 struct renameat_args {
3529 sys_renameat(struct thread *td, struct renameat_args *uap)
3532 return (kern_renameat(td, uap->oldfd, uap->old, uap->newfd, uap->new,
3538 kern_renameat_mac(struct thread *td, int oldfd, const char *old, int newfd,
3539 const char *new, enum uio_seg pathseg, struct nameidata *fromnd)
3543 NDINIT_ATRIGHTS(fromnd, DELETE, LOCKPARENT | LOCKLEAF | SAVESTART |
3544 AUDITVNODE1, pathseg, old, oldfd, &cap_renameat_source_rights, td);
3545 if ((error = namei(fromnd)) != 0)
3547 error = mac_vnode_check_rename_from(td->td_ucred, fromnd->ni_dvp,
3548 fromnd->ni_vp, &fromnd->ni_cnd);
3549 VOP_UNLOCK(fromnd->ni_dvp);
3550 if (fromnd->ni_dvp != fromnd->ni_vp)
3551 VOP_UNLOCK(fromnd->ni_vp);
3553 NDFREE(fromnd, NDF_ONLY_PNBUF);
3554 vrele(fromnd->ni_dvp);
3555 vrele(fromnd->ni_vp);
3556 if (fromnd->ni_startdir)
3557 vrele(fromnd->ni_startdir);
3564 kern_renameat(struct thread *td, int oldfd, const char *old, int newfd,
3565 const char *new, enum uio_seg pathseg)
3567 struct mount *mp = NULL;
3568 struct vnode *tvp, *fvp, *tdvp;
3569 struct nameidata fromnd, tond;
3575 if (mac_vnode_check_rename_from_enabled()) {
3576 error = kern_renameat_mac(td, oldfd, old, newfd, new, pathseg,
3582 NDINIT_ATRIGHTS(&fromnd, DELETE, WANTPARENT | SAVESTART | AUDITVNODE1,
3583 pathseg, old, oldfd, &cap_renameat_source_rights, td);
3584 if ((error = namei(&fromnd)) != 0)
3590 NDINIT_ATRIGHTS(&tond, RENAME, LOCKPARENT | LOCKLEAF | NOCACHE |
3591 SAVESTART | AUDITVNODE2, pathseg, new, newfd,
3592 &cap_renameat_target_rights, td);
3593 if (fromnd.ni_vp->v_type == VDIR)
3594 tond.ni_cnd.cn_flags |= WILLBEDIR;
3595 if ((error = namei(&tond)) != 0) {
3596 /* Translate error code for rename("dir1", "dir2/."). */
3597 if (error == EISDIR && fvp->v_type == VDIR)
3599 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3600 vrele(fromnd.ni_dvp);
3606 error = vn_start_write(fvp, &mp, V_NOWAIT);
3608 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3609 NDFREE(&tond, NDF_ONLY_PNBUF);
3616 vrele(fromnd.ni_dvp);
3618 vrele(tond.ni_startdir);
3619 if (fromnd.ni_startdir != NULL)
3620 vrele(fromnd.ni_startdir);
3621 error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH);
3627 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
3630 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
3635 if (newfd != AT_FDCWD && (tond.ni_resflags & NIRES_ABS) == 0) {
3637 * If the target already exists we require CAP_UNLINKAT
3638 * from 'newfd', when newfd was used for the lookup.
3640 error = cap_check(&tond.ni_filecaps.fc_rights,
3641 &cap_unlinkat_rights);
3652 * If the source is the same as the destination (that is, if they
3653 * are links to the same vnode), then there is nothing to do.
3659 error = mac_vnode_check_rename_to(td->td_ucred, tdvp,
3660 tond.ni_vp, fromnd.ni_dvp == tdvp, &tond.ni_cnd);
3664 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
3665 tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
3666 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3667 NDFREE(&tond, NDF_ONLY_PNBUF);
3669 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3670 NDFREE(&tond, NDF_ONLY_PNBUF);
3677 vrele(fromnd.ni_dvp);
3680 vrele(tond.ni_startdir);
3681 vn_finished_write(mp);
3683 if (fromnd.ni_startdir)
3684 vrele(fromnd.ni_startdir);
3691 * Make a directory file.
3693 #ifndef _SYS_SYSPROTO_H_
3700 sys_mkdir(struct thread *td, struct mkdir_args *uap)
3703 return (kern_mkdirat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
3707 #ifndef _SYS_SYSPROTO_H_
3708 struct mkdirat_args {
3715 sys_mkdirat(struct thread *td, struct mkdirat_args *uap)
3718 return (kern_mkdirat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode));
3722 kern_mkdirat(struct thread *td, int fd, const char *path, enum uio_seg segflg,
3728 struct nameidata nd;
3731 AUDIT_ARG_MODE(mode);
3734 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
3735 NOCACHE, segflg, path, fd, &cap_mkdirat_rights,
3737 nd.ni_cnd.cn_flags |= WILLBEDIR;
3738 if ((error = namei(&nd)) != 0)
3742 NDFREE(&nd, NDF_ONLY_PNBUF);
3744 * XXX namei called with LOCKPARENT but not LOCKLEAF has
3745 * the strange behaviour of leaving the vnode unlocked
3746 * if the target is the same vnode as the parent.
3748 if (vp == nd.ni_dvp)
3755 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3756 NDFREE(&nd, NDF_ONLY_PNBUF);
3758 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3763 vattr.va_type = VDIR;
3764 vattr.va_mode = (mode & ACCESSPERMS) &~ td->td_proc->p_fd->fd_cmask;
3766 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
3771 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
3775 NDFREE(&nd, NDF_ONLY_PNBUF);
3779 vn_finished_write(mp);
3784 * Remove a directory file.
3786 #ifndef _SYS_SYSPROTO_H_
3792 sys_rmdir(struct thread *td, struct rmdir_args *uap)
3795 return (kern_frmdirat(td, AT_FDCWD, uap->path, FD_NONE, UIO_USERSPACE,
3800 kern_frmdirat(struct thread *td, int dfd, const char *path, int fd,
3801 enum uio_seg pathseg, int flag)
3806 struct nameidata nd;
3807 cap_rights_t rights;
3811 if (fd != FD_NONE) {
3812 error = getvnode(td, fd, cap_rights_init_one(&rights, CAP_LOOKUP),
3820 NDINIT_ATRIGHTS(&nd, DELETE, LOCKPARENT | LOCKLEAF | AUDITVNODE1 |
3821 ((flag & AT_BENEATH) != 0 ? BENEATH : 0),
3822 pathseg, path, dfd, &cap_unlinkat_rights, td);
3823 if ((error = namei(&nd)) != 0)
3826 if (vp->v_type != VDIR) {
3831 * No rmdir "." please.
3833 if (nd.ni_dvp == vp) {
3838 * The root of a mounted filesystem cannot be deleted.
3840 if (vp->v_vflag & VV_ROOT) {
3845 if (fp != NULL && fp->f_vnode != vp) {
3846 if (VN_IS_DOOMED(fp->f_vnode))
3854 error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp,
3859 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3860 NDFREE(&nd, NDF_ONLY_PNBUF);
3862 if (nd.ni_dvp == vp)
3866 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3870 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_UNLINK);
3871 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
3872 vn_finished_write(mp);
3874 NDFREE(&nd, NDF_ONLY_PNBUF);
3876 if (nd.ni_dvp == vp)
3886 #if defined(COMPAT_43) || defined(COMPAT_FREEBSD11)
3888 freebsd11_kern_getdirentries(struct thread *td, int fd, char *ubuf, u_int count,
3889 long *basep, void (*func)(struct freebsd11_dirent *))
3891 struct freebsd11_dirent dstdp;
3892 struct dirent *dp, *edp;
3895 ssize_t resid, ucount;
3898 /* XXX arbitrary sanity limit on `count'. */
3899 count = min(count, 64 * 1024);
3901 dirbuf = malloc(count, M_TEMP, M_WAITOK);
3903 error = kern_getdirentries(td, fd, dirbuf, count, &base, &resid,
3911 for (dp = (struct dirent *)dirbuf,
3912 edp = (struct dirent *)&dirbuf[count - resid];
3913 ucount < count && dp < edp; ) {
3914 if (dp->d_reclen == 0)
3916 MPASS(dp->d_reclen >= _GENERIC_DIRLEN(0));
3917 if (dp->d_namlen >= sizeof(dstdp.d_name))
3919 dstdp.d_type = dp->d_type;
3920 dstdp.d_namlen = dp->d_namlen;
3921 dstdp.d_fileno = dp->d_fileno; /* truncate */
3922 if (dstdp.d_fileno != dp->d_fileno) {
3923 switch (ino64_trunc_error) {
3931 dstdp.d_fileno = UINT32_MAX;
3935 dstdp.d_reclen = sizeof(dstdp) - sizeof(dstdp.d_name) +
3936 ((dp->d_namlen + 1 + 3) &~ 3);
3937 bcopy(dp->d_name, dstdp.d_name, dstdp.d_namlen);
3938 bzero(dstdp.d_name + dstdp.d_namlen,
3939 dstdp.d_reclen - offsetof(struct freebsd11_dirent, d_name) -
3941 MPASS(dstdp.d_reclen <= dp->d_reclen);
3942 MPASS(ucount + dstdp.d_reclen <= count);
3945 error = copyout(&dstdp, ubuf + ucount, dstdp.d_reclen);
3948 dp = (struct dirent *)((char *)dp + dp->d_reclen);
3949 ucount += dstdp.d_reclen;
3953 free(dirbuf, M_TEMP);
3955 td->td_retval[0] = ucount;
3962 ogetdirentries_cvt(struct freebsd11_dirent *dp)
3964 #if (BYTE_ORDER == LITTLE_ENDIAN)
3966 * The expected low byte of dp->d_namlen is our dp->d_type.
3967 * The high MBZ byte of dp->d_namlen is our dp->d_namlen.
3969 dp->d_type = dp->d_namlen;
3973 * The dp->d_type is the high byte of the expected dp->d_namlen,
3974 * so must be zero'ed.
3981 * Read a block of directory entries in a filesystem independent format.
3983 #ifndef _SYS_SYSPROTO_H_
3984 struct ogetdirentries_args {
3992 ogetdirentries(struct thread *td, struct ogetdirentries_args *uap)
3997 error = kern_ogetdirentries(td, uap, &loff);
3999 error = copyout(&loff, uap->basep, sizeof(long));
4004 kern_ogetdirentries(struct thread *td, struct ogetdirentries_args *uap,
4010 /* XXX arbitrary sanity limit on `count'. */
4011 if (uap->count > 64 * 1024)
4014 error = freebsd11_kern_getdirentries(td, uap->fd, uap->buf, uap->count,
4015 &base, ogetdirentries_cvt);
4017 if (error == 0 && uap->basep != NULL)
4018 error = copyout(&base, uap->basep, sizeof(long));
4022 #endif /* COMPAT_43 */
4024 #if defined(COMPAT_FREEBSD11)
4025 #ifndef _SYS_SYSPROTO_H_
4026 struct freebsd11_getdirentries_args {
4034 freebsd11_getdirentries(struct thread *td,
4035 struct freebsd11_getdirentries_args *uap)
4040 error = freebsd11_kern_getdirentries(td, uap->fd, uap->buf, uap->count,
4043 if (error == 0 && uap->basep != NULL)
4044 error = copyout(&base, uap->basep, sizeof(long));
4049 freebsd11_getdents(struct thread *td, struct freebsd11_getdents_args *uap)
4051 struct freebsd11_getdirentries_args ap;
4055 ap.count = uap->count;
4057 return (freebsd11_getdirentries(td, &ap));
4059 #endif /* COMPAT_FREEBSD11 */
4062 * Read a block of directory entries in a filesystem independent format.
4065 sys_getdirentries(struct thread *td, struct getdirentries_args *uap)
4070 error = kern_getdirentries(td, uap->fd, uap->buf, uap->count, &base,
4071 NULL, UIO_USERSPACE);
4074 if (uap->basep != NULL)
4075 error = copyout(&base, uap->basep, sizeof(off_t));
4080 kern_getdirentries(struct thread *td, int fd, char *buf, size_t count,
4081 off_t *basep, ssize_t *residp, enum uio_seg bufseg)
4092 if (count > IOSIZE_MAX)
4094 auio.uio_resid = count;
4095 error = getvnode(td, fd, &cap_read_rights, &fp);
4098 if ((fp->f_flag & FREAD) == 0) {
4103 foffset = foffset_lock(fp, 0);
4105 if (vp->v_type != VDIR) {
4109 aiov.iov_base = buf;
4110 aiov.iov_len = count;
4111 auio.uio_iov = &aiov;
4112 auio.uio_iovcnt = 1;
4113 auio.uio_rw = UIO_READ;
4114 auio.uio_segflg = bufseg;
4116 vn_lock(vp, LK_SHARED | LK_RETRY);
4117 AUDIT_ARG_VNODE1(vp);
4118 loff = auio.uio_offset = foffset;
4120 error = mac_vnode_check_readdir(td->td_ucred, vp);
4123 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL,
4125 foffset = auio.uio_offset;
4130 if (count == auio.uio_resid &&
4131 (vp->v_vflag & VV_ROOT) &&
4132 (vp->v_mount->mnt_flag & MNT_UNION)) {
4133 struct vnode *tvp = vp;
4135 vp = vp->v_mount->mnt_vnodecovered;
4145 *residp = auio.uio_resid;
4146 td->td_retval[0] = count - auio.uio_resid;
4148 foffset_unlock(fp, foffset, 0);
4154 * Set the mode mask for creation of filesystem nodes.
4156 #ifndef _SYS_SYSPROTO_H_
4162 sys_umask(struct thread *td, struct umask_args *uap)
4164 struct filedesc *fdp;
4166 fdp = td->td_proc->p_fd;
4167 FILEDESC_XLOCK(fdp);
4168 td->td_retval[0] = fdp->fd_cmask;
4169 fdp->fd_cmask = uap->newmask & ALLPERMS;
4170 FILEDESC_XUNLOCK(fdp);
4175 * Void all references to file by ripping underlying filesystem away from
4178 #ifndef _SYS_SYSPROTO_H_
4179 struct revoke_args {
4184 sys_revoke(struct thread *td, struct revoke_args *uap)
4188 struct nameidata nd;
4191 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
4193 if ((error = namei(&nd)) != 0)
4196 NDFREE(&nd, NDF_ONLY_PNBUF);
4197 if (vp->v_type != VCHR || vp->v_rdev == NULL) {
4202 error = mac_vnode_check_revoke(td->td_ucred, vp);
4206 error = VOP_GETATTR(vp, &vattr, td->td_ucred);
4209 if (td->td_ucred->cr_uid != vattr.va_uid) {
4210 error = priv_check(td, PRIV_VFS_ADMIN);
4214 if (devfs_usecount(vp) > 0)
4215 VOP_REVOKE(vp, REVOKEALL);
4222 * Convert a user file descriptor to a kernel file entry and check that, if it
4223 * is a capability, the correct rights are present. A reference on the file
4224 * entry is held upon returning.
4227 getvnode(struct thread *td, int fd, cap_rights_t *rightsp, struct file **fpp)
4232 error = fget_unlocked(td->td_proc->p_fd, fd, rightsp, &fp);
4237 * The file could be not of the vnode type, or it may be not
4238 * yet fully initialized, in which case the f_vnode pointer
4239 * may be set, but f_ops is still badfileops. E.g.,
4240 * devfs_open() transiently create such situation to
4241 * facilitate csw d_fdopen().
4243 * Dupfdopen() handling in kern_openat() installs the
4244 * half-baked file into the process descriptor table, allowing
4245 * other thread to dereference it. Guard against the race by
4248 if (fp->f_vnode == NULL || fp->f_ops == &badfileops) {
4257 * Get an (NFS) file handle.
4259 #ifndef _SYS_SYSPROTO_H_
4260 struct lgetfh_args {
4266 sys_lgetfh(struct thread *td, struct lgetfh_args *uap)
4269 return (kern_getfhat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->fname,
4270 UIO_USERSPACE, uap->fhp));
4273 #ifndef _SYS_SYSPROTO_H_
4280 sys_getfh(struct thread *td, struct getfh_args *uap)
4283 return (kern_getfhat(td, 0, AT_FDCWD, uap->fname, UIO_USERSPACE,
4288 * syscall for the rpc.lockd to use to translate an open descriptor into
4289 * a NFS file handle.
4291 * warning: do not remove the priv_check() call or this becomes one giant
4294 #ifndef _SYS_SYSPROTO_H_
4295 struct getfhat_args {
4303 sys_getfhat(struct thread *td, struct getfhat_args *uap)
4306 if ((uap->flags & ~(AT_SYMLINK_NOFOLLOW | AT_BENEATH)) != 0)
4308 return (kern_getfhat(td, uap->flags, uap->fd, uap->path, UIO_USERSPACE,
4313 kern_getfhat(struct thread *td, int flags, int fd, const char *path,
4314 enum uio_seg pathseg, fhandle_t *fhp)
4316 struct nameidata nd;
4321 error = priv_check(td, PRIV_VFS_GETFH);
4324 NDINIT_AT(&nd, LOOKUP, ((flags & AT_SYMLINK_NOFOLLOW) != 0 ? NOFOLLOW :
4325 FOLLOW) | ((flags & AT_BENEATH) != 0 ? BENEATH : 0) | LOCKLEAF |
4326 AUDITVNODE1, pathseg, path, fd, td);
4330 NDFREE(&nd, NDF_ONLY_PNBUF);
4332 bzero(&fh, sizeof(fh));
4333 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
4334 error = VOP_VPTOFH(vp, &fh.fh_fid);
4337 error = copyout(&fh, fhp, sizeof (fh));
4341 #ifndef _SYS_SYSPROTO_H_
4342 struct fhlink_args {
4348 sys_fhlink(struct thread *td, struct fhlink_args *uap)
4351 return (kern_fhlinkat(td, AT_FDCWD, uap->to, UIO_USERSPACE, uap->fhp));
4354 #ifndef _SYS_SYSPROTO_H_
4355 struct fhlinkat_args {
4362 sys_fhlinkat(struct thread *td, struct fhlinkat_args *uap)
4365 return (kern_fhlinkat(td, uap->tofd, uap->to, UIO_USERSPACE, uap->fhp));
4369 kern_fhlinkat(struct thread *td, int fd, const char *path,
4370 enum uio_seg pathseg, fhandle_t *fhp)
4377 error = priv_check(td, PRIV_VFS_GETFH);
4380 error = copyin(fhp, &fh, sizeof(fh));
4385 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4387 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_SHARED, &vp);
4392 } while ((error = kern_linkat_vp(td, vp, fd, path, pathseg)) == EAGAIN);
4396 #ifndef _SYS_SYSPROTO_H_
4397 struct fhreadlink_args {
4404 sys_fhreadlink(struct thread *td, struct fhreadlink_args *uap)
4411 error = priv_check(td, PRIV_VFS_GETFH);
4414 if (uap->bufsize > IOSIZE_MAX)
4416 error = copyin(uap->fhp, &fh, sizeof(fh));
4419 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4421 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_SHARED, &vp);
4425 error = kern_readlink_vp(vp, uap->buf, UIO_USERSPACE, uap->bufsize, td);
4431 * syscall for the rpc.lockd to use to translate a NFS file handle into an
4434 * warning: do not remove the priv_check() call or this becomes one giant
4437 #ifndef _SYS_SYSPROTO_H_
4438 struct fhopen_args {
4439 const struct fhandle *u_fhp;
4444 sys_fhopen(struct thread *td, struct fhopen_args *uap)
4453 error = priv_check(td, PRIV_VFS_FHOPEN);
4457 fmode = FFLAGS(uap->flags);
4458 /* why not allow a non-read/write open for our lockd? */
4459 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
4461 error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
4464 /* find the mount point */
4465 mp = vfs_busyfs(&fhp.fh_fsid);
4468 /* now give me my vnode, it gets returned to me locked */
4469 error = VFS_FHTOVP(mp, &fhp.fh_fid, LK_EXCLUSIVE, &vp);
4474 error = falloc_noinstall(td, &fp);
4480 * An extra reference on `fp' has been held for us by
4481 * falloc_noinstall().
4487 error = vn_open_vnode(vp, fmode, td->td_ucred, td, fp);
4489 KASSERT(fp->f_ops == &badfileops,
4490 ("VOP_OPEN in fhopen() set f_ops"));
4491 KASSERT(td->td_dupfd < 0,
4492 ("fhopen() encountered fdopen()"));
4501 finit_vnode(fp, fmode, NULL, &vnops);
4503 if ((fmode & O_TRUNC) != 0) {
4504 error = fo_truncate(fp, 0, td->td_ucred, td);
4509 error = finstall(td, fp, &indx, fmode, NULL);
4512 td->td_retval[0] = indx;
4517 * Stat an (NFS) file handle.
4519 #ifndef _SYS_SYSPROTO_H_
4520 struct fhstat_args {
4521 struct fhandle *u_fhp;
4526 sys_fhstat(struct thread *td, struct fhstat_args *uap)
4532 error = copyin(uap->u_fhp, &fh, sizeof(fh));
4535 error = kern_fhstat(td, fh, &sb);
4537 error = copyout(&sb, uap->sb, sizeof(sb));
4542 kern_fhstat(struct thread *td, struct fhandle fh, struct stat *sb)
4548 error = priv_check(td, PRIV_VFS_FHSTAT);
4551 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4553 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_EXCLUSIVE, &vp);
4557 error = VOP_STAT(vp, sb, td->td_ucred, NOCRED, td);
4563 * Implement fstatfs() for (NFS) file handles.
4565 #ifndef _SYS_SYSPROTO_H_
4566 struct fhstatfs_args {
4567 struct fhandle *u_fhp;
4572 sys_fhstatfs(struct thread *td, struct fhstatfs_args *uap)
4578 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4581 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
4582 error = kern_fhstatfs(td, fh, sfp);
4584 error = copyout(sfp, uap->buf, sizeof(*sfp));
4585 free(sfp, M_STATFS);
4590 kern_fhstatfs(struct thread *td, fhandle_t fh, struct statfs *buf)
4596 error = priv_check(td, PRIV_VFS_FHSTATFS);
4599 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4601 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_EXCLUSIVE, &vp);
4607 error = prison_canseemount(td->td_ucred, mp);
4611 error = mac_mount_check_stat(td->td_ucred, mp);
4615 error = VFS_STATFS(mp, buf);
4622 * Unlike madvise(2), we do not make a best effort to remember every
4623 * possible caching hint. Instead, we remember the last setting with
4624 * the exception that we will allow POSIX_FADV_NORMAL to adjust the
4625 * region of any current setting.
4628 kern_posix_fadvise(struct thread *td, int fd, off_t offset, off_t len,
4631 struct fadvise_info *fa, *new;
4637 if (offset < 0 || len < 0 || offset > OFF_MAX - len)
4639 AUDIT_ARG_VALUE(advice);
4641 case POSIX_FADV_SEQUENTIAL:
4642 case POSIX_FADV_RANDOM:
4643 case POSIX_FADV_NOREUSE:
4644 new = malloc(sizeof(*fa), M_FADVISE, M_WAITOK);
4646 case POSIX_FADV_NORMAL:
4647 case POSIX_FADV_WILLNEED:
4648 case POSIX_FADV_DONTNEED:
4654 /* XXX: CAP_POSIX_FADVISE? */
4656 error = fget(td, fd, &cap_no_rights, &fp);
4659 AUDIT_ARG_FILE(td->td_proc, fp);
4660 if ((fp->f_ops->fo_flags & DFLAG_SEEKABLE) == 0) {
4664 if (fp->f_type != DTYPE_VNODE) {
4669 if (vp->v_type != VREG) {
4676 end = offset + len - 1;
4678 case POSIX_FADV_SEQUENTIAL:
4679 case POSIX_FADV_RANDOM:
4680 case POSIX_FADV_NOREUSE:
4682 * Try to merge any existing non-standard region with
4683 * this new region if possible, otherwise create a new
4684 * non-standard region for this request.
4686 mtx_pool_lock(mtxpool_sleep, fp);
4688 if (fa != NULL && fa->fa_advice == advice &&
4689 ((fa->fa_start <= end && fa->fa_end >= offset) ||
4690 (end != OFF_MAX && fa->fa_start == end + 1) ||
4691 (fa->fa_end != OFF_MAX && fa->fa_end + 1 == offset))) {
4692 if (offset < fa->fa_start)
4693 fa->fa_start = offset;
4694 if (end > fa->fa_end)
4697 new->fa_advice = advice;
4698 new->fa_start = offset;
4703 mtx_pool_unlock(mtxpool_sleep, fp);
4705 case POSIX_FADV_NORMAL:
4707 * If a the "normal" region overlaps with an existing
4708 * non-standard region, trim or remove the
4709 * non-standard region.
4711 mtx_pool_lock(mtxpool_sleep, fp);
4714 if (offset <= fa->fa_start && end >= fa->fa_end) {
4716 fp->f_advice = NULL;
4717 } else if (offset <= fa->fa_start &&
4718 end >= fa->fa_start)
4719 fa->fa_start = end + 1;
4720 else if (offset <= fa->fa_end && end >= fa->fa_end)
4721 fa->fa_end = offset - 1;
4722 else if (offset >= fa->fa_start && end <= fa->fa_end) {
4724 * If the "normal" region is a middle
4725 * portion of the existing
4726 * non-standard region, just remove
4727 * the whole thing rather than picking
4728 * one side or the other to
4732 fp->f_advice = NULL;
4735 mtx_pool_unlock(mtxpool_sleep, fp);
4737 case POSIX_FADV_WILLNEED:
4738 case POSIX_FADV_DONTNEED:
4739 error = VOP_ADVISE(vp, offset, end, advice);
4745 free(new, M_FADVISE);
4750 sys_posix_fadvise(struct thread *td, struct posix_fadvise_args *uap)
4754 error = kern_posix_fadvise(td, uap->fd, uap->offset, uap->len,
4756 return (kern_posix_error(td, error));
4760 kern_copy_file_range(struct thread *td, int infd, off_t *inoffp, int outfd,
4761 off_t *outoffp, size_t len, unsigned int flags)
4763 struct file *infp, *outfp;
4764 struct vnode *invp, *outvp;
4767 void *rl_rcookie, *rl_wcookie;
4768 off_t savinoff, savoutoff;
4770 infp = outfp = NULL;
4771 rl_rcookie = rl_wcookie = NULL;
4780 if (len > SSIZE_MAX)
4782 * Although the len argument is size_t, the return argument
4783 * is ssize_t (which is signed). Therefore a size that won't
4784 * fit in ssize_t can't be returned.
4788 /* Get the file structures for the file descriptors. */
4789 error = fget_read(td, infd, &cap_read_rights, &infp);
4792 if (infp->f_ops == &badfileops) {
4796 if (infp->f_vnode == NULL) {
4800 error = fget_write(td, outfd, &cap_write_rights, &outfp);
4803 if (outfp->f_ops == &badfileops) {
4807 if (outfp->f_vnode == NULL) {
4812 /* Set the offset pointers to the correct place. */
4814 inoffp = &infp->f_offset;
4815 if (outoffp == NULL)
4816 outoffp = &outfp->f_offset;
4818 savoutoff = *outoffp;
4820 invp = infp->f_vnode;
4821 outvp = outfp->f_vnode;
4822 /* Sanity check the f_flag bits. */
4823 if ((outfp->f_flag & (FWRITE | FAPPEND)) != FWRITE ||
4824 (infp->f_flag & FREAD) == 0) {
4829 /* If len == 0, just return 0. */
4834 * If infp and outfp refer to the same file, the byte ranges cannot
4837 if (invp == outvp && ((savinoff <= savoutoff && savinoff + len >
4838 savoutoff) || (savinoff > savoutoff && savoutoff + len >
4844 /* Range lock the byte ranges for both invp and outvp. */
4846 rl_wcookie = vn_rangelock_wlock(outvp, *outoffp, *outoffp +
4848 rl_rcookie = vn_rangelock_tryrlock(invp, *inoffp, *inoffp +
4850 if (rl_rcookie != NULL)
4852 vn_rangelock_unlock(outvp, rl_wcookie);
4853 rl_rcookie = vn_rangelock_rlock(invp, *inoffp, *inoffp + len);
4854 vn_rangelock_unlock(invp, rl_rcookie);
4858 error = vn_copy_file_range(invp, inoffp, outvp, outoffp, &retlen,
4859 flags, infp->f_cred, outfp->f_cred, td);
4861 if (rl_rcookie != NULL)
4862 vn_rangelock_unlock(invp, rl_rcookie);
4863 if (rl_wcookie != NULL)
4864 vn_rangelock_unlock(outvp, rl_wcookie);
4865 if (savinoff != -1 && (error == EINTR || error == ERESTART)) {
4867 *outoffp = savoutoff;
4873 td->td_retval[0] = retlen;
4878 sys_copy_file_range(struct thread *td, struct copy_file_range_args *uap)
4880 off_t inoff, outoff, *inoffp, *outoffp;
4883 inoffp = outoffp = NULL;
4884 if (uap->inoffp != NULL) {
4885 error = copyin(uap->inoffp, &inoff, sizeof(off_t));
4890 if (uap->outoffp != NULL) {
4891 error = copyin(uap->outoffp, &outoff, sizeof(off_t));
4896 error = kern_copy_file_range(td, uap->infd, inoffp, uap->outfd,
4897 outoffp, uap->len, uap->flags);
4898 if (error == 0 && uap->inoffp != NULL)
4899 error = copyout(inoffp, uap->inoffp, sizeof(off_t));
4900 if (error == 0 && uap->outoffp != NULL)
4901 error = copyout(outoffp, uap->outoffp, sizeof(off_t));