2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
6 * (c) UNIX System Laboratories, Inc.
7 * All or some portions of this file are derived from material licensed
8 * to the University of California by American Telephone and Telegraph
9 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10 * the permission of UNIX System Laboratories, Inc.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * @(#)vfs_syscalls.c 8.13 (Berkeley) 4/15/94
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
42 #include "opt_capsicum.h"
43 #include "opt_ktrace.h"
45 #include <sys/param.h>
46 #include <sys/systm.h>
49 #include <sys/capsicum.h>
51 #include <sys/sysent.h>
52 #include <sys/malloc.h>
53 #include <sys/mount.h>
54 #include <sys/mutex.h>
55 #include <sys/sysproto.h>
56 #include <sys/namei.h>
57 #include <sys/filedesc.h>
58 #include <sys/kernel.h>
59 #include <sys/fcntl.h>
61 #include <sys/filio.h>
62 #include <sys/limits.h>
63 #include <sys/linker.h>
64 #include <sys/rwlock.h>
68 #include <sys/unistd.h>
69 #include <sys/vnode.h>
72 #include <sys/dirent.h>
74 #include <sys/syscallsubr.h>
75 #include <sys/sysctl.h>
77 #include <sys/ktrace.h>
80 #include <machine/stdarg.h>
82 #include <security/audit/audit.h>
83 #include <security/mac/mac_framework.h>
86 #include <vm/vm_object.h>
87 #include <vm/vm_page.h>
90 #include <ufs/ufs/quota.h>
92 MALLOC_DEFINE(M_FADVISE, "fadvise", "posix_fadvise(2) information");
94 SDT_PROVIDER_DEFINE(vfs);
95 SDT_PROBE_DEFINE2(vfs, , stat, mode, "char *", "int");
96 SDT_PROBE_DEFINE2(vfs, , stat, reg, "char *", "int");
98 static int kern_chflagsat(struct thread *td, int fd, const char *path,
99 enum uio_seg pathseg, u_long flags, int atflag);
100 static int setfflags(struct thread *td, struct vnode *, u_long);
101 static int getutimes(const struct timeval *, enum uio_seg, struct timespec *);
102 static int getutimens(const struct timespec *, enum uio_seg,
103 struct timespec *, int *);
104 static int setutimes(struct thread *td, struct vnode *,
105 const struct timespec *, int, int);
106 static int vn_access(struct vnode *vp, int user_flags, struct ucred *cred,
108 static int kern_fhlinkat(struct thread *td, int fd, const char *path,
109 enum uio_seg pathseg, fhandle_t *fhp);
110 static int kern_getfhat(struct thread *td, int flags, int fd,
111 const char *path, enum uio_seg pathseg, fhandle_t *fhp);
112 static int kern_readlink_vp(struct vnode *vp, char *buf, enum uio_seg bufseg,
113 size_t count, struct thread *td);
114 static int kern_linkat_vp(struct thread *td, struct vnode *vp, int fd,
115 const char *path, enum uio_seg segflag);
118 kern_sync(struct thread *td)
120 struct mount *mp, *nmp;
123 mtx_lock(&mountlist_mtx);
124 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
125 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK)) {
126 nmp = TAILQ_NEXT(mp, mnt_list);
129 if ((mp->mnt_flag & MNT_RDONLY) == 0 &&
130 vn_start_write(NULL, &mp, V_NOWAIT) == 0) {
131 save = curthread_pflags_set(TDP_SYNCIO);
132 vfs_periodic(mp, MNT_NOWAIT);
133 VFS_SYNC(mp, MNT_NOWAIT);
134 curthread_pflags_restore(save);
135 vn_finished_write(mp);
137 mtx_lock(&mountlist_mtx);
138 nmp = TAILQ_NEXT(mp, mnt_list);
141 mtx_unlock(&mountlist_mtx);
146 * Sync each mounted filesystem.
148 #ifndef _SYS_SYSPROTO_H_
155 sys_sync(struct thread *td, struct sync_args *uap)
158 return (kern_sync(td));
162 * Change filesystem quotas.
164 #ifndef _SYS_SYSPROTO_H_
165 struct quotactl_args {
173 sys_quotactl(struct thread *td, struct quotactl_args *uap)
179 AUDIT_ARG_CMD(uap->cmd);
180 AUDIT_ARG_UID(uap->uid);
181 if (!prison_allow(td->td_ucred, PR_ALLOW_QUOTAS))
183 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
185 if ((error = namei(&nd)) != 0)
187 NDFREE(&nd, NDF_ONLY_PNBUF);
188 mp = nd.ni_vp->v_mount;
191 error = vfs_busy(mp, 0);
196 error = VFS_QUOTACTL(mp, uap->cmd, uap->uid, uap->arg);
199 * Since quota on operation typically needs to open quota
200 * file, the Q_QUOTAON handler needs to unbusy the mount point
201 * before calling into namei. Otherwise, unmount might be
202 * started between two vfs_busy() invocations (first is our,
203 * second is from mount point cross-walk code in lookup()),
206 * Require that Q_QUOTAON handles the vfs_busy() reference on
207 * its own, always returning with ubusied mount point.
209 if ((uap->cmd >> SUBCMDSHIFT) != Q_QUOTAON &&
210 (uap->cmd >> SUBCMDSHIFT) != Q_QUOTAOFF)
217 * Used by statfs conversion routines to scale the block size up if
218 * necessary so that all of the block counts are <= 'max_size'. Note
219 * that 'max_size' should be a bitmask, i.e. 2^n - 1 for some non-zero
223 statfs_scale_blocks(struct statfs *sf, long max_size)
228 KASSERT(powerof2(max_size + 1), ("%s: invalid max_size", __func__));
231 * Attempt to scale the block counts to give a more accurate
232 * overview to userland of the ratio of free space to used
233 * space. To do this, find the largest block count and compute
234 * a divisor that lets it fit into a signed integer <= max_size.
236 if (sf->f_bavail < 0)
237 count = -sf->f_bavail;
239 count = sf->f_bavail;
240 count = MAX(sf->f_blocks, MAX(sf->f_bfree, count));
241 if (count <= max_size)
244 count >>= flsl(max_size);
251 sf->f_bsize <<= shift;
252 sf->f_blocks >>= shift;
253 sf->f_bfree >>= shift;
254 sf->f_bavail >>= shift;
258 kern_do_statfs(struct thread *td, struct mount *mp, struct statfs *buf)
264 error = vfs_busy(mp, 0);
269 error = mac_mount_check_stat(td->td_ucred, mp);
273 error = VFS_STATFS(mp, buf);
276 if (priv_check_cred_vfs_generation(td->td_ucred)) {
277 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
278 prison_enforce_statfs(td->td_ucred, mp, buf);
286 * Get filesystem statistics.
288 #ifndef _SYS_SYSPROTO_H_
295 sys_statfs(struct thread *td, struct statfs_args *uap)
300 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
301 error = kern_statfs(td, uap->path, UIO_USERSPACE, sfp);
303 error = copyout(sfp, uap->buf, sizeof(struct statfs));
309 kern_statfs(struct thread *td, const char *path, enum uio_seg pathseg,
316 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
321 mp = nd.ni_vp->v_mount;
323 NDFREE(&nd, NDF_ONLY_PNBUF);
325 return (kern_do_statfs(td, mp, buf));
329 * Get filesystem statistics.
331 #ifndef _SYS_SYSPROTO_H_
332 struct fstatfs_args {
338 sys_fstatfs(struct thread *td, struct fstatfs_args *uap)
343 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
344 error = kern_fstatfs(td, uap->fd, sfp);
346 error = copyout(sfp, uap->buf, sizeof(struct statfs));
352 kern_fstatfs(struct thread *td, int fd, struct statfs *buf)
360 error = getvnode(td, fd, &cap_fstatfs_rights, &fp);
364 vn_lock(vp, LK_SHARED | LK_RETRY);
366 AUDIT_ARG_VNODE1(vp);
373 return (kern_do_statfs(td, mp, buf));
377 * Get statistics on all filesystems.
379 #ifndef _SYS_SYSPROTO_H_
380 struct getfsstat_args {
387 sys_getfsstat(struct thread *td, struct getfsstat_args *uap)
392 if (uap->bufsize < 0 || uap->bufsize > SIZE_MAX)
394 error = kern_getfsstat(td, &uap->buf, uap->bufsize, &count,
395 UIO_USERSPACE, uap->mode);
397 td->td_retval[0] = count;
402 * If (bufsize > 0 && bufseg == UIO_SYSSPACE)
403 * The caller is responsible for freeing memory which will be allocated
407 kern_getfsstat(struct thread *td, struct statfs **buf, size_t bufsize,
408 size_t *countp, enum uio_seg bufseg, int mode)
410 struct mount *mp, *nmp;
411 struct statfs *sfsp, *sp, *sptmp, *tofree;
412 size_t count, maxcount;
420 if (bufseg == UIO_SYSSPACE)
425 maxcount = bufsize / sizeof(struct statfs);
429 } else if (bufseg == UIO_USERSPACE) {
432 } else /* if (bufseg == UIO_SYSSPACE) */ {
434 mtx_lock(&mountlist_mtx);
435 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
438 mtx_unlock(&mountlist_mtx);
439 if (maxcount > count)
441 tofree = sfsp = *buf = malloc(maxcount * sizeof(struct statfs),
445 mtx_lock(&mountlist_mtx);
446 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
447 if (prison_canseemount(td->td_ucred, mp) != 0) {
448 nmp = TAILQ_NEXT(mp, mnt_list);
452 if (mac_mount_check_stat(td->td_ucred, mp) != 0) {
453 nmp = TAILQ_NEXT(mp, mnt_list);
457 if (mode == MNT_WAIT) {
458 if (vfs_busy(mp, MBF_MNTLSTLOCK) != 0) {
460 * If vfs_busy() failed, and MBF_NOWAIT
461 * wasn't passed, then the mp is gone.
462 * Furthermore, because of MBF_MNTLSTLOCK,
463 * the mountlist_mtx was dropped. We have
464 * no other choice than to start over.
466 mtx_unlock(&mountlist_mtx);
467 free(tofree, M_STATFS);
471 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK) != 0) {
472 nmp = TAILQ_NEXT(mp, mnt_list);
476 if (sfsp != NULL && count < maxcount) {
479 * If MNT_NOWAIT is specified, do not refresh
482 if (mode != MNT_NOWAIT) {
483 error = VFS_STATFS(mp, sp);
485 mtx_lock(&mountlist_mtx);
486 nmp = TAILQ_NEXT(mp, mnt_list);
491 if (priv_check_cred_vfs_generation(td->td_ucred)) {
492 sptmp = malloc(sizeof(struct statfs), M_STATFS,
495 sptmp->f_fsid.val[0] = sptmp->f_fsid.val[1] = 0;
496 prison_enforce_statfs(td->td_ucred, mp, sptmp);
500 if (bufseg == UIO_SYSSPACE) {
501 bcopy(sp, sfsp, sizeof(*sp));
502 free(sptmp, M_STATFS);
503 } else /* if (bufseg == UIO_USERSPACE) */ {
504 error = copyout(sp, sfsp, sizeof(*sp));
505 free(sptmp, M_STATFS);
514 mtx_lock(&mountlist_mtx);
515 nmp = TAILQ_NEXT(mp, mnt_list);
518 mtx_unlock(&mountlist_mtx);
519 if (sfsp != NULL && count > maxcount)
526 #ifdef COMPAT_FREEBSD4
528 * Get old format filesystem statistics.
530 static void freebsd4_cvtstatfs(struct statfs *, struct ostatfs *);
532 #ifndef _SYS_SYSPROTO_H_
533 struct freebsd4_statfs_args {
539 freebsd4_statfs(struct thread *td, struct freebsd4_statfs_args *uap)
545 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
546 error = kern_statfs(td, uap->path, UIO_USERSPACE, sfp);
548 freebsd4_cvtstatfs(sfp, &osb);
549 error = copyout(&osb, uap->buf, sizeof(osb));
556 * Get filesystem statistics.
558 #ifndef _SYS_SYSPROTO_H_
559 struct freebsd4_fstatfs_args {
565 freebsd4_fstatfs(struct thread *td, struct freebsd4_fstatfs_args *uap)
571 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
572 error = kern_fstatfs(td, uap->fd, sfp);
574 freebsd4_cvtstatfs(sfp, &osb);
575 error = copyout(&osb, uap->buf, sizeof(osb));
582 * Get statistics on all filesystems.
584 #ifndef _SYS_SYSPROTO_H_
585 struct freebsd4_getfsstat_args {
592 freebsd4_getfsstat(struct thread *td, struct freebsd4_getfsstat_args *uap)
594 struct statfs *buf, *sp;
599 if (uap->bufsize < 0)
601 count = uap->bufsize / sizeof(struct ostatfs);
602 if (count > SIZE_MAX / sizeof(struct statfs))
604 size = count * sizeof(struct statfs);
605 error = kern_getfsstat(td, &buf, size, &count, UIO_SYSSPACE,
608 td->td_retval[0] = count;
611 while (count != 0 && error == 0) {
612 freebsd4_cvtstatfs(sp, &osb);
613 error = copyout(&osb, uap->buf, sizeof(osb));
624 * Implement fstatfs() for (NFS) file handles.
626 #ifndef _SYS_SYSPROTO_H_
627 struct freebsd4_fhstatfs_args {
628 struct fhandle *u_fhp;
633 freebsd4_fhstatfs(struct thread *td, struct freebsd4_fhstatfs_args *uap)
640 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
643 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
644 error = kern_fhstatfs(td, fh, sfp);
646 freebsd4_cvtstatfs(sfp, &osb);
647 error = copyout(&osb, uap->buf, sizeof(osb));
654 * Convert a new format statfs structure to an old format statfs structure.
657 freebsd4_cvtstatfs(struct statfs *nsp, struct ostatfs *osp)
660 statfs_scale_blocks(nsp, LONG_MAX);
661 bzero(osp, sizeof(*osp));
662 osp->f_bsize = nsp->f_bsize;
663 osp->f_iosize = MIN(nsp->f_iosize, LONG_MAX);
664 osp->f_blocks = nsp->f_blocks;
665 osp->f_bfree = nsp->f_bfree;
666 osp->f_bavail = nsp->f_bavail;
667 osp->f_files = MIN(nsp->f_files, LONG_MAX);
668 osp->f_ffree = MIN(nsp->f_ffree, LONG_MAX);
669 osp->f_owner = nsp->f_owner;
670 osp->f_type = nsp->f_type;
671 osp->f_flags = nsp->f_flags;
672 osp->f_syncwrites = MIN(nsp->f_syncwrites, LONG_MAX);
673 osp->f_asyncwrites = MIN(nsp->f_asyncwrites, LONG_MAX);
674 osp->f_syncreads = MIN(nsp->f_syncreads, LONG_MAX);
675 osp->f_asyncreads = MIN(nsp->f_asyncreads, LONG_MAX);
676 strlcpy(osp->f_fstypename, nsp->f_fstypename,
677 MIN(MFSNAMELEN, OMFSNAMELEN));
678 strlcpy(osp->f_mntonname, nsp->f_mntonname,
679 MIN(MNAMELEN, OMNAMELEN));
680 strlcpy(osp->f_mntfromname, nsp->f_mntfromname,
681 MIN(MNAMELEN, OMNAMELEN));
682 osp->f_fsid = nsp->f_fsid;
684 #endif /* COMPAT_FREEBSD4 */
686 #if defined(COMPAT_FREEBSD11)
688 * Get old format filesystem statistics.
690 static void freebsd11_cvtstatfs(struct statfs *, struct freebsd11_statfs *);
693 freebsd11_statfs(struct thread *td, struct freebsd11_statfs_args *uap)
695 struct freebsd11_statfs osb;
699 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
700 error = kern_statfs(td, uap->path, UIO_USERSPACE, sfp);
702 freebsd11_cvtstatfs(sfp, &osb);
703 error = copyout(&osb, uap->buf, sizeof(osb));
710 * Get filesystem statistics.
713 freebsd11_fstatfs(struct thread *td, struct freebsd11_fstatfs_args *uap)
715 struct freebsd11_statfs osb;
719 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
720 error = kern_fstatfs(td, uap->fd, sfp);
722 freebsd11_cvtstatfs(sfp, &osb);
723 error = copyout(&osb, uap->buf, sizeof(osb));
730 * Get statistics on all filesystems.
733 freebsd11_getfsstat(struct thread *td, struct freebsd11_getfsstat_args *uap)
735 struct freebsd11_statfs osb;
736 struct statfs *buf, *sp;
740 count = uap->bufsize / sizeof(struct ostatfs);
741 size = count * sizeof(struct statfs);
742 error = kern_getfsstat(td, &buf, size, &count, UIO_SYSSPACE,
745 td->td_retval[0] = count;
748 while (count > 0 && error == 0) {
749 freebsd11_cvtstatfs(sp, &osb);
750 error = copyout(&osb, uap->buf, sizeof(osb));
761 * Implement fstatfs() for (NFS) file handles.
764 freebsd11_fhstatfs(struct thread *td, struct freebsd11_fhstatfs_args *uap)
766 struct freebsd11_statfs osb;
771 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
774 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
775 error = kern_fhstatfs(td, fh, sfp);
777 freebsd11_cvtstatfs(sfp, &osb);
778 error = copyout(&osb, uap->buf, sizeof(osb));
785 * Convert a new format statfs structure to an old format statfs structure.
788 freebsd11_cvtstatfs(struct statfs *nsp, struct freebsd11_statfs *osp)
791 bzero(osp, sizeof(*osp));
792 osp->f_version = FREEBSD11_STATFS_VERSION;
793 osp->f_type = nsp->f_type;
794 osp->f_flags = nsp->f_flags;
795 osp->f_bsize = nsp->f_bsize;
796 osp->f_iosize = nsp->f_iosize;
797 osp->f_blocks = nsp->f_blocks;
798 osp->f_bfree = nsp->f_bfree;
799 osp->f_bavail = nsp->f_bavail;
800 osp->f_files = nsp->f_files;
801 osp->f_ffree = nsp->f_ffree;
802 osp->f_syncwrites = nsp->f_syncwrites;
803 osp->f_asyncwrites = nsp->f_asyncwrites;
804 osp->f_syncreads = nsp->f_syncreads;
805 osp->f_asyncreads = nsp->f_asyncreads;
806 osp->f_namemax = nsp->f_namemax;
807 osp->f_owner = nsp->f_owner;
808 osp->f_fsid = nsp->f_fsid;
809 strlcpy(osp->f_fstypename, nsp->f_fstypename,
810 MIN(MFSNAMELEN, sizeof(osp->f_fstypename)));
811 strlcpy(osp->f_mntonname, nsp->f_mntonname,
812 MIN(MNAMELEN, sizeof(osp->f_mntonname)));
813 strlcpy(osp->f_mntfromname, nsp->f_mntfromname,
814 MIN(MNAMELEN, sizeof(osp->f_mntfromname)));
816 #endif /* COMPAT_FREEBSD11 */
819 * Change current working directory to a given file descriptor.
821 #ifndef _SYS_SYSPROTO_H_
827 sys_fchdir(struct thread *td, struct fchdir_args *uap)
829 struct vnode *vp, *tdp;
834 AUDIT_ARG_FD(uap->fd);
835 error = getvnode(td, uap->fd, &cap_fchdir_rights,
842 vn_lock(vp, LK_SHARED | LK_RETRY);
843 AUDIT_ARG_VNODE1(vp);
844 error = change_dir(vp, td);
845 while (!error && (mp = vp->v_mountedhere) != NULL) {
848 error = VFS_ROOT(mp, LK_SHARED, &tdp);
865 * Change current working directory (``.'').
867 #ifndef _SYS_SYSPROTO_H_
873 sys_chdir(struct thread *td, struct chdir_args *uap)
876 return (kern_chdir(td, uap->path, UIO_USERSPACE));
880 kern_chdir(struct thread *td, const char *path, enum uio_seg pathseg)
885 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
887 if ((error = namei(&nd)) != 0)
889 if ((error = change_dir(nd.ni_vp, td)) != 0) {
891 NDFREE(&nd, NDF_ONLY_PNBUF);
894 VOP_UNLOCK(nd.ni_vp);
895 NDFREE(&nd, NDF_ONLY_PNBUF);
896 pwd_chdir(td, nd.ni_vp);
901 * Change notion of root (``/'') directory.
903 #ifndef _SYS_SYSPROTO_H_
909 sys_chroot(struct thread *td, struct chroot_args *uap)
914 error = priv_check(td, PRIV_VFS_CHROOT);
917 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
918 UIO_USERSPACE, uap->path, td);
922 error = change_dir(nd.ni_vp, td);
926 error = mac_vnode_check_chroot(td->td_ucred, nd.ni_vp);
930 VOP_UNLOCK(nd.ni_vp);
931 error = pwd_chroot(td, nd.ni_vp);
933 NDFREE(&nd, NDF_ONLY_PNBUF);
938 NDFREE(&nd, NDF_ONLY_PNBUF);
943 * Common routine for chroot and chdir. Callers must provide a locked vnode
947 change_dir(struct vnode *vp, struct thread *td)
953 ASSERT_VOP_LOCKED(vp, "change_dir(): vp not locked");
954 if (vp->v_type != VDIR)
957 error = mac_vnode_check_chdir(td->td_ucred, vp);
961 return (VOP_ACCESS(vp, VEXEC, td->td_ucred, td));
965 flags_to_rights(int flags, cap_rights_t *rightsp)
968 if (flags & O_EXEC) {
969 cap_rights_set_one(rightsp, CAP_FEXECVE);
971 switch ((flags & O_ACCMODE)) {
973 cap_rights_set_one(rightsp, CAP_READ);
976 cap_rights_set_one(rightsp, CAP_READ);
979 cap_rights_set_one(rightsp, CAP_WRITE);
980 if (!(flags & (O_APPEND | O_TRUNC)))
981 cap_rights_set_one(rightsp, CAP_SEEK);
987 cap_rights_set_one(rightsp, CAP_CREATE);
990 cap_rights_set_one(rightsp, CAP_FTRUNCATE);
992 if (flags & (O_SYNC | O_FSYNC))
993 cap_rights_set_one(rightsp, CAP_FSYNC);
995 if (flags & (O_EXLOCK | O_SHLOCK))
996 cap_rights_set_one(rightsp, CAP_FLOCK);
1000 * Check permissions, allocate an open file structure, and call the device
1001 * open routine if any.
1003 #ifndef _SYS_SYSPROTO_H_
1011 sys_open(struct thread *td, struct open_args *uap)
1014 return (kern_openat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1015 uap->flags, uap->mode));
1018 #ifndef _SYS_SYSPROTO_H_
1019 struct openat_args {
1027 sys_openat(struct thread *td, struct openat_args *uap)
1030 AUDIT_ARG_FD(uap->fd);
1031 return (kern_openat(td, uap->fd, uap->path, UIO_USERSPACE, uap->flag,
1036 kern_openat(struct thread *td, int fd, const char *path, enum uio_seg pathseg,
1037 int flags, int mode)
1039 struct proc *p = td->td_proc;
1040 struct filedesc *fdp = p->p_fd;
1043 struct nameidata nd;
1044 cap_rights_t rights;
1045 int cmode, error, indx;
1049 AUDIT_ARG_FFLAGS(flags);
1050 AUDIT_ARG_MODE(mode);
1051 cap_rights_init_one(&rights, CAP_LOOKUP);
1052 flags_to_rights(flags, &rights);
1054 * Only one of the O_EXEC, O_RDONLY, O_WRONLY and O_RDWR flags
1057 if (flags & O_EXEC) {
1058 if (flags & O_ACCMODE)
1060 } else if ((flags & O_ACCMODE) == O_ACCMODE) {
1063 flags = FFLAGS(flags);
1067 * Allocate a file structure. The descriptor to reference it
1068 * is allocated and set by finstall() below.
1070 error = falloc_noinstall(td, &fp);
1074 * An extra reference on `fp' has been held for us by
1075 * falloc_noinstall().
1077 /* Set the flags early so the finit in devfs can pick them up. */
1078 fp->f_flag = flags & FMASK;
1079 cmode = ((mode & ~fdp->fd_cmask) & ALLPERMS) & ~S_ISTXT;
1080 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, fd,
1082 td->td_dupfd = -1; /* XXX check for fdopen */
1083 error = vn_open(&nd, &flags, cmode, fp);
1086 * If the vn_open replaced the method vector, something
1087 * wonderous happened deep below and we just pass it up
1088 * pretending we know what we do.
1090 if (error == ENXIO && fp->f_ops != &badfileops)
1094 * Handle special fdopen() case. bleh.
1096 * Don't do this for relative (capability) lookups; we don't
1097 * understand exactly what would happen, and we don't think
1098 * that it ever should.
1100 if ((nd.ni_lcf & NI_LCF_STRICTRELATIVE) == 0 &&
1101 (error == ENODEV || error == ENXIO) &&
1102 td->td_dupfd >= 0) {
1103 error = dupfdopen(td, fdp, td->td_dupfd, flags, error,
1112 NDFREE(&nd, NDF_ONLY_PNBUF);
1116 * Store the vnode, for any f_type. Typically, the vnode use
1117 * count is decremented by direct call to vn_closefile() for
1118 * files that switched type in the cdevsw fdopen() method.
1122 * If the file wasn't claimed by devfs bind it to the normal
1123 * vnode operations here.
1125 if (fp->f_ops == &badfileops) {
1126 KASSERT(vp->v_type != VFIFO, ("Unexpected fifo."));
1127 fp->f_seqcount[UIO_READ] = 1;
1128 fp->f_seqcount[UIO_WRITE] = 1;
1129 finit(fp, (flags & FMASK) | (fp->f_flag & FHASLOCK),
1130 DTYPE_VNODE, vp, &vnops);
1134 if (flags & O_TRUNC) {
1135 error = fo_truncate(fp, 0, td->td_ucred, td);
1141 * If we haven't already installed the FD (for dupfdopen), do so now.
1144 struct filecaps *fcaps;
1147 if ((nd.ni_lcf & NI_LCF_STRICTRELATIVE) != 0)
1148 fcaps = &nd.ni_filecaps;
1152 error = finstall(td, fp, &indx, flags, fcaps);
1153 /* On success finstall() consumes fcaps. */
1155 filecaps_free(&nd.ni_filecaps);
1159 filecaps_free(&nd.ni_filecaps);
1163 * Release our private reference, leaving the one associated with
1164 * the descriptor table intact.
1167 td->td_retval[0] = indx;
1170 KASSERT(indx == -1, ("indx=%d, should be -1", indx));
1179 #ifndef _SYS_SYSPROTO_H_
1180 struct ocreat_args {
1186 ocreat(struct thread *td, struct ocreat_args *uap)
1189 return (kern_openat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1190 O_WRONLY | O_CREAT | O_TRUNC, uap->mode));
1192 #endif /* COMPAT_43 */
1195 * Create a special file.
1197 #ifndef _SYS_SYSPROTO_H_
1198 struct mknodat_args {
1206 sys_mknodat(struct thread *td, struct mknodat_args *uap)
1209 return (kern_mknodat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode,
1213 #if defined(COMPAT_FREEBSD11)
1215 freebsd11_mknod(struct thread *td,
1216 struct freebsd11_mknod_args *uap)
1219 return (kern_mknodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1220 uap->mode, uap->dev));
1224 freebsd11_mknodat(struct thread *td,
1225 struct freebsd11_mknodat_args *uap)
1228 return (kern_mknodat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode,
1231 #endif /* COMPAT_FREEBSD11 */
1234 kern_mknodat(struct thread *td, int fd, const char *path, enum uio_seg pathseg,
1235 int mode, dev_t dev)
1240 struct nameidata nd;
1241 int error, whiteout = 0;
1243 AUDIT_ARG_MODE(mode);
1245 switch (mode & S_IFMT) {
1248 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
1249 if (error == 0 && dev == VNOVAL)
1253 error = priv_check(td, PRIV_VFS_MKNOD_WHT);
1257 return (kern_mkfifoat(td, fd, path, pathseg, mode));
1267 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
1268 NOCACHE, pathseg, path, fd, &cap_mknodat_rights,
1270 if ((error = namei(&nd)) != 0)
1274 NDFREE(&nd, NDF_ONLY_PNBUF);
1275 if (vp == nd.ni_dvp)
1283 vattr.va_mode = (mode & ALLPERMS) &
1284 ~td->td_proc->p_fd->fd_cmask;
1285 vattr.va_rdev = dev;
1288 switch (mode & S_IFMT) {
1290 vattr.va_type = VCHR;
1293 vattr.va_type = VBLK;
1299 panic("kern_mknod: invalid mode");
1302 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1303 NDFREE(&nd, NDF_ONLY_PNBUF);
1305 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1310 if (error == 0 && !whiteout)
1311 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp,
1312 &nd.ni_cnd, &vattr);
1316 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, CREATE);
1318 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp,
1319 &nd.ni_cnd, &vattr);
1324 NDFREE(&nd, NDF_ONLY_PNBUF);
1326 vn_finished_write(mp);
1331 * Create a named pipe.
1333 #ifndef _SYS_SYSPROTO_H_
1334 struct mkfifo_args {
1340 sys_mkfifo(struct thread *td, struct mkfifo_args *uap)
1343 return (kern_mkfifoat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1347 #ifndef _SYS_SYSPROTO_H_
1348 struct mkfifoat_args {
1355 sys_mkfifoat(struct thread *td, struct mkfifoat_args *uap)
1358 return (kern_mkfifoat(td, uap->fd, uap->path, UIO_USERSPACE,
1363 kern_mkfifoat(struct thread *td, int fd, const char *path,
1364 enum uio_seg pathseg, int mode)
1368 struct nameidata nd;
1371 AUDIT_ARG_MODE(mode);
1374 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
1375 NOCACHE, pathseg, path, fd, &cap_mkfifoat_rights,
1377 if ((error = namei(&nd)) != 0)
1379 if (nd.ni_vp != NULL) {
1380 NDFREE(&nd, NDF_ONLY_PNBUF);
1381 if (nd.ni_vp == nd.ni_dvp)
1388 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1389 NDFREE(&nd, NDF_ONLY_PNBUF);
1391 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1396 vattr.va_type = VFIFO;
1397 vattr.va_mode = (mode & ALLPERMS) & ~td->td_proc->p_fd->fd_cmask;
1399 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1404 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
1411 vn_finished_write(mp);
1412 NDFREE(&nd, NDF_ONLY_PNBUF);
1417 * Make a hard file link.
1419 #ifndef _SYS_SYSPROTO_H_
1426 sys_link(struct thread *td, struct link_args *uap)
1429 return (kern_linkat(td, AT_FDCWD, AT_FDCWD, uap->path, uap->link,
1430 UIO_USERSPACE, FOLLOW));
1433 #ifndef _SYS_SYSPROTO_H_
1434 struct linkat_args {
1443 sys_linkat(struct thread *td, struct linkat_args *uap)
1448 if ((flag & ~(AT_SYMLINK_FOLLOW | AT_BENEATH)) != 0)
1451 return (kern_linkat(td, uap->fd1, uap->fd2, uap->path1, uap->path2,
1452 UIO_USERSPACE, ((flag & AT_SYMLINK_FOLLOW) != 0 ? FOLLOW :
1453 NOFOLLOW) | ((flag & AT_BENEATH) != 0 ? BENEATH : 0)));
1456 int hardlink_check_uid = 0;
1457 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
1458 &hardlink_check_uid, 0,
1459 "Unprivileged processes cannot create hard links to files owned by other "
1461 static int hardlink_check_gid = 0;
1462 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
1463 &hardlink_check_gid, 0,
1464 "Unprivileged processes cannot create hard links to files owned by other "
1468 can_hardlink(struct vnode *vp, struct ucred *cred)
1473 if (!hardlink_check_uid && !hardlink_check_gid)
1476 error = VOP_GETATTR(vp, &va, cred);
1480 if (hardlink_check_uid && cred->cr_uid != va.va_uid) {
1481 error = priv_check_cred(cred, PRIV_VFS_LINK);
1486 if (hardlink_check_gid && !groupmember(va.va_gid, cred)) {
1487 error = priv_check_cred(cred, PRIV_VFS_LINK);
1496 kern_linkat(struct thread *td, int fd1, int fd2, const char *path1,
1497 const char *path2, enum uio_seg segflag, int follow)
1499 struct nameidata nd;
1504 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, segflag,
1505 path1, fd1, &cap_linkat_source_rights, td);
1506 if ((error = namei(&nd)) != 0)
1508 NDFREE(&nd, NDF_ONLY_PNBUF);
1509 error = kern_linkat_vp(td, nd.ni_vp, fd2, path2, segflag);
1510 } while (error == EAGAIN);
1515 kern_linkat_vp(struct thread *td, struct vnode *vp, int fd, const char *path,
1516 enum uio_seg segflag)
1518 struct nameidata nd;
1522 if (vp->v_type == VDIR) {
1524 return (EPERM); /* POSIX */
1526 NDINIT_ATRIGHTS(&nd, CREATE,
1527 LOCKPARENT | SAVENAME | AUDITVNODE2 | NOCACHE, segflag, path, fd,
1528 &cap_linkat_target_rights, td);
1529 if ((error = namei(&nd)) == 0) {
1530 if (nd.ni_vp != NULL) {
1531 NDFREE(&nd, NDF_ONLY_PNBUF);
1532 if (nd.ni_dvp == nd.ni_vp)
1539 } else if (nd.ni_dvp->v_mount != vp->v_mount) {
1541 * Cross-device link. No need to recheck
1542 * vp->v_type, since it cannot change, except
1545 NDFREE(&nd, NDF_ONLY_PNBUF);
1549 } else if ((error = vn_lock(vp, LK_EXCLUSIVE)) == 0) {
1550 error = can_hardlink(vp, td->td_ucred);
1553 error = mac_vnode_check_link(td->td_ucred,
1554 nd.ni_dvp, vp, &nd.ni_cnd);
1559 NDFREE(&nd, NDF_ONLY_PNBUF);
1562 error = vn_start_write(vp, &mp, V_NOWAIT);
1566 NDFREE(&nd, NDF_ONLY_PNBUF);
1567 error = vn_start_write(NULL, &mp,
1573 error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
1576 vn_finished_write(mp);
1577 NDFREE(&nd, NDF_ONLY_PNBUF);
1580 NDFREE(&nd, NDF_ONLY_PNBUF);
1590 * Make a symbolic link.
1592 #ifndef _SYS_SYSPROTO_H_
1593 struct symlink_args {
1599 sys_symlink(struct thread *td, struct symlink_args *uap)
1602 return (kern_symlinkat(td, uap->path, AT_FDCWD, uap->link,
1606 #ifndef _SYS_SYSPROTO_H_
1607 struct symlinkat_args {
1614 sys_symlinkat(struct thread *td, struct symlinkat_args *uap)
1617 return (kern_symlinkat(td, uap->path1, uap->fd, uap->path2,
1622 kern_symlinkat(struct thread *td, const char *path1, int fd, const char *path2,
1623 enum uio_seg segflg)
1627 const char *syspath;
1629 struct nameidata nd;
1632 if (segflg == UIO_SYSSPACE) {
1635 tmppath = uma_zalloc(namei_zone, M_WAITOK);
1636 if ((error = copyinstr(path1, tmppath, MAXPATHLEN, NULL)) != 0)
1640 AUDIT_ARG_TEXT(syspath);
1643 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
1644 NOCACHE, segflg, path2, fd, &cap_symlinkat_rights,
1646 if ((error = namei(&nd)) != 0)
1649 NDFREE(&nd, NDF_ONLY_PNBUF);
1650 if (nd.ni_vp == nd.ni_dvp)
1658 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1659 NDFREE(&nd, NDF_ONLY_PNBUF);
1661 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1666 vattr.va_mode = ACCESSPERMS &~ td->td_proc->p_fd->fd_cmask;
1668 vattr.va_type = VLNK;
1669 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1674 error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr, syspath);
1680 NDFREE(&nd, NDF_ONLY_PNBUF);
1682 vn_finished_write(mp);
1684 if (segflg != UIO_SYSSPACE)
1685 uma_zfree(namei_zone, tmppath);
1690 * Delete a whiteout from the filesystem.
1692 #ifndef _SYS_SYSPROTO_H_
1693 struct undelete_args {
1698 sys_undelete(struct thread *td, struct undelete_args *uap)
1701 struct nameidata nd;
1706 NDINIT(&nd, DELETE, LOCKPARENT | DOWHITEOUT | AUDITVNODE1,
1707 UIO_USERSPACE, uap->path, td);
1712 if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & ISWHITEOUT)) {
1713 NDFREE(&nd, NDF_ONLY_PNBUF);
1714 if (nd.ni_vp == nd.ni_dvp)
1722 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1723 NDFREE(&nd, NDF_ONLY_PNBUF);
1725 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1729 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, DELETE);
1730 NDFREE(&nd, NDF_ONLY_PNBUF);
1732 vn_finished_write(mp);
1737 * Delete a name from the filesystem.
1739 #ifndef _SYS_SYSPROTO_H_
1740 struct unlink_args {
1745 sys_unlink(struct thread *td, struct unlink_args *uap)
1748 return (kern_funlinkat(td, AT_FDCWD, uap->path, FD_NONE, UIO_USERSPACE,
1753 kern_funlinkat_ex(struct thread *td, int dfd, const char *path, int fd,
1754 int flag, enum uio_seg pathseg, ino_t oldinum)
1757 if ((flag & ~AT_REMOVEDIR) != 0)
1760 if ((flag & AT_REMOVEDIR) != 0)
1761 return (kern_frmdirat(td, dfd, path, fd, UIO_USERSPACE, 0));
1763 return (kern_funlinkat(td, dfd, path, fd, UIO_USERSPACE, 0, 0));
1766 #ifndef _SYS_SYSPROTO_H_
1767 struct unlinkat_args {
1774 sys_unlinkat(struct thread *td, struct unlinkat_args *uap)
1777 return (kern_funlinkat_ex(td, uap->fd, uap->path, FD_NONE, uap->flag,
1781 #ifndef _SYS_SYSPROTO_H_
1782 struct funlinkat_args {
1790 sys_funlinkat(struct thread *td, struct funlinkat_args *uap)
1793 return (kern_funlinkat_ex(td, uap->dfd, uap->path, uap->fd, uap->flag,
1798 kern_funlinkat(struct thread *td, int dfd, const char *path, int fd,
1799 enum uio_seg pathseg, int flag, ino_t oldinum)
1804 struct nameidata nd;
1809 if (fd != FD_NONE) {
1810 error = getvnode(td, fd, &cap_no_rights, &fp);
1817 NDINIT_ATRIGHTS(&nd, DELETE, LOCKPARENT | LOCKLEAF | AUDITVNODE1 |
1818 ((flag & AT_BENEATH) != 0 ? BENEATH : 0),
1819 pathseg, path, dfd, &cap_unlinkat_rights, td);
1820 if ((error = namei(&nd)) != 0) {
1821 if (error == EINVAL)
1826 if (vp->v_type == VDIR && oldinum == 0) {
1827 error = EPERM; /* POSIX */
1828 } else if (oldinum != 0 &&
1829 ((error = vn_stat(vp, &sb, td->td_ucred, NOCRED, td)) == 0) &&
1830 sb.st_ino != oldinum) {
1831 error = EIDRM; /* Identifier removed */
1832 } else if (fp != NULL && fp->f_vnode != vp) {
1833 if (VN_IS_DOOMED(fp->f_vnode))
1839 * The root of a mounted filesystem cannot be deleted.
1841 * XXX: can this only be a VDIR case?
1843 if (vp->v_vflag & VV_ROOT)
1847 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1848 NDFREE(&nd, NDF_ONLY_PNBUF);
1850 if (vp == nd.ni_dvp)
1854 if ((error = vn_start_write(NULL, &mp,
1855 V_XSLEEP | PCATCH)) != 0) {
1861 error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp,
1866 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_UNLINK);
1867 error = VOP_REMOVE(nd.ni_dvp, vp, &nd.ni_cnd);
1871 vn_finished_write(mp);
1873 NDFREE(&nd, NDF_ONLY_PNBUF);
1875 if (vp == nd.ni_dvp)
1886 * Reposition read/write file offset.
1888 #ifndef _SYS_SYSPROTO_H_
1897 sys_lseek(struct thread *td, struct lseek_args *uap)
1900 return (kern_lseek(td, uap->fd, uap->offset, uap->whence));
1904 kern_lseek(struct thread *td, int fd, off_t offset, int whence)
1910 error = fget(td, fd, &cap_seek_rights, &fp);
1913 error = (fp->f_ops->fo_flags & DFLAG_SEEKABLE) != 0 ?
1914 fo_seek(fp, offset, whence, td) : ESPIPE;
1919 #if defined(COMPAT_43)
1921 * Reposition read/write file offset.
1923 #ifndef _SYS_SYSPROTO_H_
1924 struct olseek_args {
1931 olseek(struct thread *td, struct olseek_args *uap)
1934 return (kern_lseek(td, uap->fd, uap->offset, uap->whence));
1936 #endif /* COMPAT_43 */
1938 #if defined(COMPAT_FREEBSD6)
1939 /* Version with the 'pad' argument */
1941 freebsd6_lseek(struct thread *td, struct freebsd6_lseek_args *uap)
1944 return (kern_lseek(td, uap->fd, uap->offset, uap->whence));
1949 * Check access permissions using passed credentials.
1952 vn_access(struct vnode *vp, int user_flags, struct ucred *cred,
1958 /* Flags == 0 means only check for existence. */
1959 if (user_flags == 0)
1963 if (user_flags & R_OK)
1965 if (user_flags & W_OK)
1967 if (user_flags & X_OK)
1970 error = mac_vnode_check_access(cred, vp, accmode);
1974 if ((accmode & VWRITE) == 0 || (error = vn_writechk(vp)) == 0)
1975 error = VOP_ACCESS(vp, accmode, cred, td);
1980 * Check access permissions using "real" credentials.
1982 #ifndef _SYS_SYSPROTO_H_
1983 struct access_args {
1989 sys_access(struct thread *td, struct access_args *uap)
1992 return (kern_accessat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1996 #ifndef _SYS_SYSPROTO_H_
1997 struct faccessat_args {
2005 sys_faccessat(struct thread *td, struct faccessat_args *uap)
2008 return (kern_accessat(td, uap->fd, uap->path, UIO_USERSPACE, uap->flag,
2013 kern_accessat(struct thread *td, int fd, const char *path,
2014 enum uio_seg pathseg, int flag, int amode)
2016 struct ucred *cred, *usecred;
2018 struct nameidata nd;
2021 if ((flag & ~(AT_EACCESS | AT_BENEATH)) != 0)
2023 if (amode != F_OK && (amode & ~(R_OK | W_OK | X_OK)) != 0)
2027 * Create and modify a temporary credential instead of one that
2028 * is potentially shared (if we need one).
2030 cred = td->td_ucred;
2031 if ((flag & AT_EACCESS) == 0 &&
2032 ((cred->cr_uid != cred->cr_ruid ||
2033 cred->cr_rgid != cred->cr_groups[0]))) {
2034 usecred = crdup(cred);
2035 usecred->cr_uid = cred->cr_ruid;
2036 usecred->cr_groups[0] = cred->cr_rgid;
2037 td->td_ucred = usecred;
2040 AUDIT_ARG_VALUE(amode);
2041 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF |
2042 AUDITVNODE1 | ((flag & AT_BENEATH) != 0 ? BENEATH : 0),
2043 pathseg, path, fd, &cap_fstat_rights, td);
2044 if ((error = namei(&nd)) != 0)
2048 error = vn_access(vp, amode, usecred, td);
2049 NDFREE(&nd, NDF_ONLY_PNBUF);
2052 if (usecred != cred) {
2053 td->td_ucred = cred;
2060 * Check access permissions using "effective" credentials.
2062 #ifndef _SYS_SYSPROTO_H_
2063 struct eaccess_args {
2069 sys_eaccess(struct thread *td, struct eaccess_args *uap)
2072 return (kern_accessat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2073 AT_EACCESS, uap->amode));
2076 #if defined(COMPAT_43)
2078 * Get file status; this version follows links.
2080 #ifndef _SYS_SYSPROTO_H_
2087 ostat(struct thread *td, struct ostat_args *uap)
2093 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
2098 return (copyout(&osb, uap->ub, sizeof (osb)));
2102 * Get file status; this version does not follow links.
2104 #ifndef _SYS_SYSPROTO_H_
2105 struct olstat_args {
2111 olstat(struct thread *td, struct olstat_args *uap)
2117 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2118 UIO_USERSPACE, &sb, NULL);
2122 return (copyout(&osb, uap->ub, sizeof (osb)));
2126 * Convert from an old to a new stat structure.
2127 * XXX: many values are blindly truncated.
2130 cvtstat(struct stat *st, struct ostat *ost)
2133 bzero(ost, sizeof(*ost));
2134 ost->st_dev = st->st_dev;
2135 ost->st_ino = st->st_ino;
2136 ost->st_mode = st->st_mode;
2137 ost->st_nlink = st->st_nlink;
2138 ost->st_uid = st->st_uid;
2139 ost->st_gid = st->st_gid;
2140 ost->st_rdev = st->st_rdev;
2141 ost->st_size = MIN(st->st_size, INT32_MAX);
2142 ost->st_atim = st->st_atim;
2143 ost->st_mtim = st->st_mtim;
2144 ost->st_ctim = st->st_ctim;
2145 ost->st_blksize = st->st_blksize;
2146 ost->st_blocks = st->st_blocks;
2147 ost->st_flags = st->st_flags;
2148 ost->st_gen = st->st_gen;
2150 #endif /* COMPAT_43 */
2152 #if defined(COMPAT_43) || defined(COMPAT_FREEBSD11)
2153 int ino64_trunc_error;
2154 SYSCTL_INT(_vfs, OID_AUTO, ino64_trunc_error, CTLFLAG_RW,
2155 &ino64_trunc_error, 0,
2156 "Error on truncation of device, file or inode number, or link count");
2159 freebsd11_cvtstat(struct stat *st, struct freebsd11_stat *ost)
2162 ost->st_dev = st->st_dev;
2163 if (ost->st_dev != st->st_dev) {
2164 switch (ino64_trunc_error) {
2167 * Since dev_t is almost raw, don't clamp to the
2168 * maximum for case 2, but ignore the error.
2175 ost->st_ino = st->st_ino;
2176 if (ost->st_ino != st->st_ino) {
2177 switch (ino64_trunc_error) {
2184 ost->st_ino = UINT32_MAX;
2188 ost->st_mode = st->st_mode;
2189 ost->st_nlink = st->st_nlink;
2190 if (ost->st_nlink != st->st_nlink) {
2191 switch (ino64_trunc_error) {
2198 ost->st_nlink = UINT16_MAX;
2202 ost->st_uid = st->st_uid;
2203 ost->st_gid = st->st_gid;
2204 ost->st_rdev = st->st_rdev;
2205 if (ost->st_rdev != st->st_rdev) {
2206 switch (ino64_trunc_error) {
2213 ost->st_atim = st->st_atim;
2214 ost->st_mtim = st->st_mtim;
2215 ost->st_ctim = st->st_ctim;
2216 ost->st_size = st->st_size;
2217 ost->st_blocks = st->st_blocks;
2218 ost->st_blksize = st->st_blksize;
2219 ost->st_flags = st->st_flags;
2220 ost->st_gen = st->st_gen;
2222 ost->st_birthtim = st->st_birthtim;
2223 bzero((char *)&ost->st_birthtim + sizeof(ost->st_birthtim),
2224 sizeof(*ost) - offsetof(struct freebsd11_stat,
2225 st_birthtim) - sizeof(ost->st_birthtim));
2230 freebsd11_stat(struct thread *td, struct freebsd11_stat_args* uap)
2233 struct freebsd11_stat osb;
2236 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
2240 error = freebsd11_cvtstat(&sb, &osb);
2242 error = copyout(&osb, uap->ub, sizeof(osb));
2247 freebsd11_lstat(struct thread *td, struct freebsd11_lstat_args* uap)
2250 struct freebsd11_stat osb;
2253 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2254 UIO_USERSPACE, &sb, NULL);
2257 error = freebsd11_cvtstat(&sb, &osb);
2259 error = copyout(&osb, uap->ub, sizeof(osb));
2264 freebsd11_fhstat(struct thread *td, struct freebsd11_fhstat_args* uap)
2268 struct freebsd11_stat osb;
2271 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
2274 error = kern_fhstat(td, fh, &sb);
2277 error = freebsd11_cvtstat(&sb, &osb);
2279 error = copyout(&osb, uap->sb, sizeof(osb));
2284 freebsd11_fstatat(struct thread *td, struct freebsd11_fstatat_args* uap)
2287 struct freebsd11_stat osb;
2290 error = kern_statat(td, uap->flag, uap->fd, uap->path,
2291 UIO_USERSPACE, &sb, NULL);
2294 error = freebsd11_cvtstat(&sb, &osb);
2296 error = copyout(&osb, uap->buf, sizeof(osb));
2299 #endif /* COMPAT_FREEBSD11 */
2304 #ifndef _SYS_SYSPROTO_H_
2305 struct fstatat_args {
2313 sys_fstatat(struct thread *td, struct fstatat_args *uap)
2318 error = kern_statat(td, uap->flag, uap->fd, uap->path,
2319 UIO_USERSPACE, &sb, NULL);
2321 error = copyout(&sb, uap->buf, sizeof (sb));
2326 kern_statat(struct thread *td, int flag, int fd, const char *path,
2327 enum uio_seg pathseg, struct stat *sbp,
2328 void (*hook)(struct vnode *vp, struct stat *sbp))
2330 struct nameidata nd;
2333 if ((flag & ~(AT_SYMLINK_NOFOLLOW | AT_BENEATH)) != 0)
2336 NDINIT_ATRIGHTS(&nd, LOOKUP, ((flag & AT_SYMLINK_NOFOLLOW) != 0 ?
2337 NOFOLLOW : FOLLOW) | ((flag & AT_BENEATH) != 0 ? BENEATH : 0) |
2338 LOCKSHARED | LOCKLEAF | AUDITVNODE1, pathseg, path, fd,
2339 &cap_fstat_rights, td);
2341 if ((error = namei(&nd)) != 0)
2343 error = vn_stat(nd.ni_vp, sbp, td->td_ucred, NOCRED, td);
2345 SDT_PROBE2(vfs, , stat, mode, path, sbp->st_mode);
2346 if (S_ISREG(sbp->st_mode))
2347 SDT_PROBE2(vfs, , stat, reg, path, pathseg);
2348 if (__predict_false(hook != NULL))
2349 hook(nd.ni_vp, sbp);
2351 NDFREE(&nd, NDF_ONLY_PNBUF);
2353 #ifdef __STAT_TIME_T_EXT
2354 sbp->st_atim_ext = 0;
2355 sbp->st_mtim_ext = 0;
2356 sbp->st_ctim_ext = 0;
2357 sbp->st_btim_ext = 0;
2360 if (KTRPOINT(td, KTR_STRUCT))
2361 ktrstat_error(sbp, error);
2366 #if defined(COMPAT_FREEBSD11)
2368 * Implementation of the NetBSD [l]stat() functions.
2371 freebsd11_cvtnstat(struct stat *sb, struct nstat *nsb)
2374 bzero(nsb, sizeof(*nsb));
2375 nsb->st_dev = sb->st_dev;
2376 nsb->st_ino = sb->st_ino;
2377 nsb->st_mode = sb->st_mode;
2378 nsb->st_nlink = sb->st_nlink;
2379 nsb->st_uid = sb->st_uid;
2380 nsb->st_gid = sb->st_gid;
2381 nsb->st_rdev = sb->st_rdev;
2382 nsb->st_atim = sb->st_atim;
2383 nsb->st_mtim = sb->st_mtim;
2384 nsb->st_ctim = sb->st_ctim;
2385 nsb->st_size = sb->st_size;
2386 nsb->st_blocks = sb->st_blocks;
2387 nsb->st_blksize = sb->st_blksize;
2388 nsb->st_flags = sb->st_flags;
2389 nsb->st_gen = sb->st_gen;
2390 nsb->st_birthtim = sb->st_birthtim;
2393 #ifndef _SYS_SYSPROTO_H_
2394 struct freebsd11_nstat_args {
2400 freebsd11_nstat(struct thread *td, struct freebsd11_nstat_args *uap)
2406 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
2410 freebsd11_cvtnstat(&sb, &nsb);
2411 return (copyout(&nsb, uap->ub, sizeof (nsb)));
2415 * NetBSD lstat. Get file status; this version does not follow links.
2417 #ifndef _SYS_SYSPROTO_H_
2418 struct freebsd11_nlstat_args {
2424 freebsd11_nlstat(struct thread *td, struct freebsd11_nlstat_args *uap)
2430 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2431 UIO_USERSPACE, &sb, NULL);
2434 freebsd11_cvtnstat(&sb, &nsb);
2435 return (copyout(&nsb, uap->ub, sizeof (nsb)));
2437 #endif /* COMPAT_FREEBSD11 */
2440 * Get configurable pathname variables.
2442 #ifndef _SYS_SYSPROTO_H_
2443 struct pathconf_args {
2449 sys_pathconf(struct thread *td, struct pathconf_args *uap)
2454 error = kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name, FOLLOW,
2457 td->td_retval[0] = value;
2461 #ifndef _SYS_SYSPROTO_H_
2462 struct lpathconf_args {
2468 sys_lpathconf(struct thread *td, struct lpathconf_args *uap)
2473 error = kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name,
2476 td->td_retval[0] = value;
2481 kern_pathconf(struct thread *td, const char *path, enum uio_seg pathseg,
2482 int name, u_long flags, long *valuep)
2484 struct nameidata nd;
2487 NDINIT(&nd, LOOKUP, LOCKSHARED | LOCKLEAF | AUDITVNODE1 | flags,
2489 if ((error = namei(&nd)) != 0)
2491 NDFREE(&nd, NDF_ONLY_PNBUF);
2493 error = VOP_PATHCONF(nd.ni_vp, name, valuep);
2499 * Return target name of a symbolic link.
2501 #ifndef _SYS_SYSPROTO_H_
2502 struct readlink_args {
2509 sys_readlink(struct thread *td, struct readlink_args *uap)
2512 return (kern_readlinkat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2513 uap->buf, UIO_USERSPACE, uap->count));
2515 #ifndef _SYS_SYSPROTO_H_
2516 struct readlinkat_args {
2524 sys_readlinkat(struct thread *td, struct readlinkat_args *uap)
2527 return (kern_readlinkat(td, uap->fd, uap->path, UIO_USERSPACE,
2528 uap->buf, UIO_USERSPACE, uap->bufsize));
2532 kern_readlinkat(struct thread *td, int fd, const char *path,
2533 enum uio_seg pathseg, char *buf, enum uio_seg bufseg, size_t count)
2536 struct nameidata nd;
2539 if (count > IOSIZE_MAX)
2542 NDINIT_AT(&nd, LOOKUP, NOFOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
2543 pathseg, path, fd, td);
2545 if ((error = namei(&nd)) != 0)
2547 NDFREE(&nd, NDF_ONLY_PNBUF);
2550 error = kern_readlink_vp(vp, buf, bufseg, count, td);
2557 * Helper function to readlink from a vnode
2560 kern_readlink_vp(struct vnode *vp, char *buf, enum uio_seg bufseg, size_t count,
2567 ASSERT_VOP_LOCKED(vp, "kern_readlink_vp(): vp not locked");
2569 error = mac_vnode_check_readlink(td->td_ucred, vp);
2573 if (vp->v_type != VLNK && (vp->v_vflag & VV_READLINK) == 0)
2576 aiov.iov_base = buf;
2577 aiov.iov_len = count;
2578 auio.uio_iov = &aiov;
2579 auio.uio_iovcnt = 1;
2580 auio.uio_offset = 0;
2581 auio.uio_rw = UIO_READ;
2582 auio.uio_segflg = bufseg;
2584 auio.uio_resid = count;
2585 error = VOP_READLINK(vp, &auio, td->td_ucred);
2586 td->td_retval[0] = count - auio.uio_resid;
2591 * Common implementation code for chflags() and fchflags().
2594 setfflags(struct thread *td, struct vnode *vp, u_long flags)
2600 /* We can't support the value matching VNOVAL. */
2601 if (flags == VNOVAL)
2602 return (EOPNOTSUPP);
2605 * Prevent non-root users from setting flags on devices. When
2606 * a device is reused, users can retain ownership of the device
2607 * if they are allowed to set flags and programs assume that
2608 * chown can't fail when done as root.
2610 if (vp->v_type == VCHR || vp->v_type == VBLK) {
2611 error = priv_check(td, PRIV_VFS_CHFLAGS_DEV);
2616 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2619 vattr.va_flags = flags;
2620 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2622 error = mac_vnode_check_setflags(td->td_ucred, vp, vattr.va_flags);
2625 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
2627 vn_finished_write(mp);
2632 * Change flags of a file given a path name.
2634 #ifndef _SYS_SYSPROTO_H_
2635 struct chflags_args {
2641 sys_chflags(struct thread *td, struct chflags_args *uap)
2644 return (kern_chflagsat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2648 #ifndef _SYS_SYSPROTO_H_
2649 struct chflagsat_args {
2657 sys_chflagsat(struct thread *td, struct chflagsat_args *uap)
2660 if ((uap->atflag & ~(AT_SYMLINK_NOFOLLOW | AT_BENEATH)) != 0)
2663 return (kern_chflagsat(td, uap->fd, uap->path, UIO_USERSPACE,
2664 uap->flags, uap->atflag));
2668 * Same as chflags() but doesn't follow symlinks.
2670 #ifndef _SYS_SYSPROTO_H_
2671 struct lchflags_args {
2677 sys_lchflags(struct thread *td, struct lchflags_args *uap)
2680 return (kern_chflagsat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2681 uap->flags, AT_SYMLINK_NOFOLLOW));
2685 kern_chflagsat(struct thread *td, int fd, const char *path,
2686 enum uio_seg pathseg, u_long flags, int atflag)
2688 struct nameidata nd;
2691 AUDIT_ARG_FFLAGS(flags);
2692 follow = (atflag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
2693 follow |= (atflag & AT_BENEATH) != 0 ? BENEATH : 0;
2694 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, pathseg, path, fd,
2695 &cap_fchflags_rights, td);
2696 if ((error = namei(&nd)) != 0)
2698 NDFREE(&nd, NDF_ONLY_PNBUF);
2699 error = setfflags(td, nd.ni_vp, flags);
2705 * Change flags of a file given a file descriptor.
2707 #ifndef _SYS_SYSPROTO_H_
2708 struct fchflags_args {
2714 sys_fchflags(struct thread *td, struct fchflags_args *uap)
2719 AUDIT_ARG_FD(uap->fd);
2720 AUDIT_ARG_FFLAGS(uap->flags);
2721 error = getvnode(td, uap->fd, &cap_fchflags_rights,
2726 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
2727 AUDIT_ARG_VNODE1(fp->f_vnode);
2728 VOP_UNLOCK(fp->f_vnode);
2730 error = setfflags(td, fp->f_vnode, uap->flags);
2736 * Common implementation code for chmod(), lchmod() and fchmod().
2739 setfmode(struct thread *td, struct ucred *cred, struct vnode *vp, int mode)
2745 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2747 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2749 vattr.va_mode = mode & ALLPERMS;
2751 error = mac_vnode_check_setmode(cred, vp, vattr.va_mode);
2754 error = VOP_SETATTR(vp, &vattr, cred);
2756 vn_finished_write(mp);
2761 * Change mode of a file given path name.
2763 #ifndef _SYS_SYSPROTO_H_
2770 sys_chmod(struct thread *td, struct chmod_args *uap)
2773 return (kern_fchmodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2777 #ifndef _SYS_SYSPROTO_H_
2778 struct fchmodat_args {
2786 sys_fchmodat(struct thread *td, struct fchmodat_args *uap)
2789 if ((uap->flag & ~(AT_SYMLINK_NOFOLLOW | AT_BENEATH)) != 0)
2792 return (kern_fchmodat(td, uap->fd, uap->path, UIO_USERSPACE,
2793 uap->mode, uap->flag));
2797 * Change mode of a file given path name (don't follow links.)
2799 #ifndef _SYS_SYSPROTO_H_
2800 struct lchmod_args {
2806 sys_lchmod(struct thread *td, struct lchmod_args *uap)
2809 return (kern_fchmodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2810 uap->mode, AT_SYMLINK_NOFOLLOW));
2814 kern_fchmodat(struct thread *td, int fd, const char *path,
2815 enum uio_seg pathseg, mode_t mode, int flag)
2817 struct nameidata nd;
2820 AUDIT_ARG_MODE(mode);
2821 follow = (flag & AT_SYMLINK_NOFOLLOW) != 0 ? NOFOLLOW : FOLLOW;
2822 follow |= (flag & AT_BENEATH) != 0 ? BENEATH : 0;
2823 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, pathseg, path, fd,
2824 &cap_fchmod_rights, td);
2825 if ((error = namei(&nd)) != 0)
2827 NDFREE(&nd, NDF_ONLY_PNBUF);
2828 error = setfmode(td, td->td_ucred, nd.ni_vp, mode);
2834 * Change mode of a file given a file descriptor.
2836 #ifndef _SYS_SYSPROTO_H_
2837 struct fchmod_args {
2843 sys_fchmod(struct thread *td, struct fchmod_args *uap)
2848 AUDIT_ARG_FD(uap->fd);
2849 AUDIT_ARG_MODE(uap->mode);
2851 error = fget(td, uap->fd, &cap_fchmod_rights, &fp);
2854 error = fo_chmod(fp, uap->mode, td->td_ucred, td);
2860 * Common implementation for chown(), lchown(), and fchown()
2863 setfown(struct thread *td, struct ucred *cred, struct vnode *vp, uid_t uid,
2870 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2872 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2877 error = mac_vnode_check_setowner(cred, vp, vattr.va_uid,
2881 error = VOP_SETATTR(vp, &vattr, cred);
2883 vn_finished_write(mp);
2888 * Set ownership given a path name.
2890 #ifndef _SYS_SYSPROTO_H_
2898 sys_chown(struct thread *td, struct chown_args *uap)
2901 return (kern_fchownat(td, AT_FDCWD, uap->path, UIO_USERSPACE, uap->uid,
2905 #ifndef _SYS_SYSPROTO_H_
2906 struct fchownat_args {
2915 sys_fchownat(struct thread *td, struct fchownat_args *uap)
2918 if ((uap->flag & ~(AT_SYMLINK_NOFOLLOW | AT_BENEATH)) != 0)
2921 return (kern_fchownat(td, uap->fd, uap->path, UIO_USERSPACE, uap->uid,
2922 uap->gid, uap->flag));
2926 kern_fchownat(struct thread *td, int fd, const char *path,
2927 enum uio_seg pathseg, int uid, int gid, int flag)
2929 struct nameidata nd;
2932 AUDIT_ARG_OWNER(uid, gid);
2933 follow = (flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
2934 follow |= (flag & AT_BENEATH) != 0 ? BENEATH : 0;
2935 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, pathseg, path, fd,
2936 &cap_fchown_rights, td);
2938 if ((error = namei(&nd)) != 0)
2940 NDFREE(&nd, NDF_ONLY_PNBUF);
2941 error = setfown(td, td->td_ucred, nd.ni_vp, uid, gid);
2947 * Set ownership given a path name, do not cross symlinks.
2949 #ifndef _SYS_SYSPROTO_H_
2950 struct lchown_args {
2957 sys_lchown(struct thread *td, struct lchown_args *uap)
2960 return (kern_fchownat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2961 uap->uid, uap->gid, AT_SYMLINK_NOFOLLOW));
2965 * Set ownership given a file descriptor.
2967 #ifndef _SYS_SYSPROTO_H_
2968 struct fchown_args {
2975 sys_fchown(struct thread *td, struct fchown_args *uap)
2980 AUDIT_ARG_FD(uap->fd);
2981 AUDIT_ARG_OWNER(uap->uid, uap->gid);
2982 error = fget(td, uap->fd, &cap_fchown_rights, &fp);
2985 error = fo_chown(fp, uap->uid, uap->gid, td->td_ucred, td);
2991 * Common implementation code for utimes(), lutimes(), and futimes().
2994 getutimes(const struct timeval *usrtvp, enum uio_seg tvpseg,
2995 struct timespec *tsp)
2997 struct timeval tv[2];
2998 const struct timeval *tvp;
3001 if (usrtvp == NULL) {
3002 vfs_timestamp(&tsp[0]);
3005 if (tvpseg == UIO_SYSSPACE) {
3008 if ((error = copyin(usrtvp, tv, sizeof(tv))) != 0)
3013 if (tvp[0].tv_usec < 0 || tvp[0].tv_usec >= 1000000 ||
3014 tvp[1].tv_usec < 0 || tvp[1].tv_usec >= 1000000)
3016 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
3017 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
3023 * Common implementation code for futimens(), utimensat().
3025 #define UTIMENS_NULL 0x1
3026 #define UTIMENS_EXIT 0x2
3028 getutimens(const struct timespec *usrtsp, enum uio_seg tspseg,
3029 struct timespec *tsp, int *retflags)
3031 struct timespec tsnow;
3034 vfs_timestamp(&tsnow);
3036 if (usrtsp == NULL) {
3039 *retflags |= UTIMENS_NULL;
3042 if (tspseg == UIO_SYSSPACE) {
3045 } else if ((error = copyin(usrtsp, tsp, sizeof(*tsp) * 2)) != 0)
3047 if (tsp[0].tv_nsec == UTIME_OMIT && tsp[1].tv_nsec == UTIME_OMIT)
3048 *retflags |= UTIMENS_EXIT;
3049 if (tsp[0].tv_nsec == UTIME_NOW && tsp[1].tv_nsec == UTIME_NOW)
3050 *retflags |= UTIMENS_NULL;
3051 if (tsp[0].tv_nsec == UTIME_OMIT)
3052 tsp[0].tv_sec = VNOVAL;
3053 else if (tsp[0].tv_nsec == UTIME_NOW)
3055 else if (tsp[0].tv_nsec < 0 || tsp[0].tv_nsec >= 1000000000L)
3057 if (tsp[1].tv_nsec == UTIME_OMIT)
3058 tsp[1].tv_sec = VNOVAL;
3059 else if (tsp[1].tv_nsec == UTIME_NOW)
3061 else if (tsp[1].tv_nsec < 0 || tsp[1].tv_nsec >= 1000000000L)
3068 * Common implementation code for utimes(), lutimes(), futimes(), futimens(),
3072 setutimes(struct thread *td, struct vnode *vp, const struct timespec *ts,
3073 int numtimes, int nullflag)
3077 int error, setbirthtime;
3079 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
3081 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3083 if (numtimes < 3 && !VOP_GETATTR(vp, &vattr, td->td_ucred) &&
3084 timespeccmp(&ts[1], &vattr.va_birthtime, < ))
3087 vattr.va_atime = ts[0];
3088 vattr.va_mtime = ts[1];
3090 vattr.va_birthtime = ts[1];
3092 vattr.va_birthtime = ts[2];
3094 vattr.va_vaflags |= VA_UTIMES_NULL;
3096 error = mac_vnode_check_setutimes(td->td_ucred, vp, vattr.va_atime,
3100 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3102 vn_finished_write(mp);
3107 * Set the access and modification times of a file.
3109 #ifndef _SYS_SYSPROTO_H_
3110 struct utimes_args {
3112 struct timeval *tptr;
3116 sys_utimes(struct thread *td, struct utimes_args *uap)
3119 return (kern_utimesat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
3120 uap->tptr, UIO_USERSPACE));
3123 #ifndef _SYS_SYSPROTO_H_
3124 struct futimesat_args {
3127 const struct timeval * times;
3131 sys_futimesat(struct thread *td, struct futimesat_args *uap)
3134 return (kern_utimesat(td, uap->fd, uap->path, UIO_USERSPACE,
3135 uap->times, UIO_USERSPACE));
3139 kern_utimesat(struct thread *td, int fd, const char *path,
3140 enum uio_seg pathseg, struct timeval *tptr, enum uio_seg tptrseg)
3142 struct nameidata nd;
3143 struct timespec ts[2];
3146 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3148 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, fd,
3149 &cap_futimes_rights, td);
3151 if ((error = namei(&nd)) != 0)
3153 NDFREE(&nd, NDF_ONLY_PNBUF);
3154 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3160 * Set the access and modification times of a file.
3162 #ifndef _SYS_SYSPROTO_H_
3163 struct lutimes_args {
3165 struct timeval *tptr;
3169 sys_lutimes(struct thread *td, struct lutimes_args *uap)
3172 return (kern_lutimes(td, uap->path, UIO_USERSPACE, uap->tptr,
3177 kern_lutimes(struct thread *td, const char *path, enum uio_seg pathseg,
3178 struct timeval *tptr, enum uio_seg tptrseg)
3180 struct timespec ts[2];
3181 struct nameidata nd;
3184 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3186 NDINIT(&nd, LOOKUP, NOFOLLOW | AUDITVNODE1, pathseg, path, td);
3187 if ((error = namei(&nd)) != 0)
3189 NDFREE(&nd, NDF_ONLY_PNBUF);
3190 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3196 * Set the access and modification times of a file.
3198 #ifndef _SYS_SYSPROTO_H_
3199 struct futimes_args {
3201 struct timeval *tptr;
3205 sys_futimes(struct thread *td, struct futimes_args *uap)
3208 return (kern_futimes(td, uap->fd, uap->tptr, UIO_USERSPACE));
3212 kern_futimes(struct thread *td, int fd, struct timeval *tptr,
3213 enum uio_seg tptrseg)
3215 struct timespec ts[2];
3220 error = getutimes(tptr, tptrseg, ts);
3223 error = getvnode(td, fd, &cap_futimes_rights, &fp);
3227 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
3228 AUDIT_ARG_VNODE1(fp->f_vnode);
3229 VOP_UNLOCK(fp->f_vnode);
3231 error = setutimes(td, fp->f_vnode, ts, 2, tptr == NULL);
3237 sys_futimens(struct thread *td, struct futimens_args *uap)
3240 return (kern_futimens(td, uap->fd, uap->times, UIO_USERSPACE));
3244 kern_futimens(struct thread *td, int fd, struct timespec *tptr,
3245 enum uio_seg tptrseg)
3247 struct timespec ts[2];
3252 error = getutimens(tptr, tptrseg, ts, &flags);
3255 if (flags & UTIMENS_EXIT)
3257 error = getvnode(td, fd, &cap_futimes_rights, &fp);
3261 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
3262 AUDIT_ARG_VNODE1(fp->f_vnode);
3263 VOP_UNLOCK(fp->f_vnode);
3265 error = setutimes(td, fp->f_vnode, ts, 2, flags & UTIMENS_NULL);
3271 sys_utimensat(struct thread *td, struct utimensat_args *uap)
3274 return (kern_utimensat(td, uap->fd, uap->path, UIO_USERSPACE,
3275 uap->times, UIO_USERSPACE, uap->flag));
3279 kern_utimensat(struct thread *td, int fd, const char *path,
3280 enum uio_seg pathseg, struct timespec *tptr, enum uio_seg tptrseg,
3283 struct nameidata nd;
3284 struct timespec ts[2];
3287 if ((flag & ~(AT_SYMLINK_NOFOLLOW | AT_BENEATH)) != 0)
3290 if ((error = getutimens(tptr, tptrseg, ts, &flags)) != 0)
3292 NDINIT_ATRIGHTS(&nd, LOOKUP, ((flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW :
3293 FOLLOW) | ((flag & AT_BENEATH) != 0 ? BENEATH : 0) | AUDITVNODE1,
3294 pathseg, path, fd, &cap_futimes_rights, td);
3295 if ((error = namei(&nd)) != 0)
3298 * We are allowed to call namei() regardless of 2xUTIME_OMIT.
3300 * "If both tv_nsec fields are UTIME_OMIT... EACCESS may be detected."
3301 * "Search permission is denied by a component of the path prefix."
3303 NDFREE(&nd, NDF_ONLY_PNBUF);
3304 if ((flags & UTIMENS_EXIT) == 0)
3305 error = setutimes(td, nd.ni_vp, ts, 2, flags & UTIMENS_NULL);
3311 * Truncate a file given its path name.
3313 #ifndef _SYS_SYSPROTO_H_
3314 struct truncate_args {
3321 sys_truncate(struct thread *td, struct truncate_args *uap)
3324 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3328 kern_truncate(struct thread *td, const char *path, enum uio_seg pathseg,
3335 struct nameidata nd;
3340 NDINIT(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, td);
3341 if ((error = namei(&nd)) != 0)
3344 rl_cookie = vn_rangelock_wlock(vp, 0, OFF_MAX);
3345 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0) {
3346 vn_rangelock_unlock(vp, rl_cookie);
3350 NDFREE(&nd, NDF_ONLY_PNBUF);
3351 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3352 if (vp->v_type == VDIR)
3355 else if ((error = mac_vnode_check_write(td->td_ucred, NOCRED, vp))) {
3358 else if ((error = vn_writechk(vp)) == 0 &&
3359 (error = VOP_ACCESS(vp, VWRITE, td->td_ucred, td)) == 0) {
3361 vattr.va_size = length;
3362 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3365 vn_finished_write(mp);
3366 vn_rangelock_unlock(vp, rl_cookie);
3371 #if defined(COMPAT_43)
3373 * Truncate a file given its path name.
3375 #ifndef _SYS_SYSPROTO_H_
3376 struct otruncate_args {
3382 otruncate(struct thread *td, struct otruncate_args *uap)
3385 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3387 #endif /* COMPAT_43 */
3389 #if defined(COMPAT_FREEBSD6)
3390 /* Versions with the pad argument */
3392 freebsd6_truncate(struct thread *td, struct freebsd6_truncate_args *uap)
3395 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3399 freebsd6_ftruncate(struct thread *td, struct freebsd6_ftruncate_args *uap)
3402 return (kern_ftruncate(td, uap->fd, uap->length));
3407 kern_fsync(struct thread *td, int fd, bool fullsync)
3412 int error, lock_flags;
3415 error = getvnode(td, fd, &cap_fsync_rights, &fp);
3421 /* XXXKIB: compete outstanding aio writes */;
3423 error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
3426 if (MNT_SHARED_WRITES(mp) ||
3427 ((mp == NULL) && MNT_SHARED_WRITES(vp->v_mount))) {
3428 lock_flags = LK_SHARED;
3430 lock_flags = LK_EXCLUSIVE;
3432 vn_lock(vp, lock_flags | LK_RETRY);
3433 AUDIT_ARG_VNODE1(vp);
3434 if (vp->v_object != NULL) {
3435 VM_OBJECT_WLOCK(vp->v_object);
3436 vm_object_page_clean(vp->v_object, 0, 0, 0);
3437 VM_OBJECT_WUNLOCK(vp->v_object);
3439 error = fullsync ? VOP_FSYNC(vp, MNT_WAIT, td) : VOP_FDATASYNC(vp, td);
3441 vn_finished_write(mp);
3448 * Sync an open file.
3450 #ifndef _SYS_SYSPROTO_H_
3456 sys_fsync(struct thread *td, struct fsync_args *uap)
3459 return (kern_fsync(td, uap->fd, true));
3463 sys_fdatasync(struct thread *td, struct fdatasync_args *uap)
3466 return (kern_fsync(td, uap->fd, false));
3470 * Rename files. Source and destination must either both be directories, or
3471 * both not be directories. If target is a directory, it must be empty.
3473 #ifndef _SYS_SYSPROTO_H_
3474 struct rename_args {
3480 sys_rename(struct thread *td, struct rename_args *uap)
3483 return (kern_renameat(td, AT_FDCWD, uap->from, AT_FDCWD,
3484 uap->to, UIO_USERSPACE));
3487 #ifndef _SYS_SYSPROTO_H_
3488 struct renameat_args {
3496 sys_renameat(struct thread *td, struct renameat_args *uap)
3499 return (kern_renameat(td, uap->oldfd, uap->old, uap->newfd, uap->new,
3504 kern_renameat(struct thread *td, int oldfd, const char *old, int newfd,
3505 const char *new, enum uio_seg pathseg)
3507 struct mount *mp = NULL;
3508 struct vnode *tvp, *fvp, *tdvp;
3509 struct nameidata fromnd, tond;
3515 NDINIT_ATRIGHTS(&fromnd, DELETE, LOCKPARENT | LOCKLEAF | SAVESTART |
3516 AUDITVNODE1, pathseg, old, oldfd,
3517 &cap_renameat_source_rights, td);
3519 NDINIT_ATRIGHTS(&fromnd, DELETE, WANTPARENT | SAVESTART | AUDITVNODE1,
3520 pathseg, old, oldfd,
3521 &cap_renameat_source_rights, td);
3524 if ((error = namei(&fromnd)) != 0)
3527 error = mac_vnode_check_rename_from(td->td_ucred, fromnd.ni_dvp,
3528 fromnd.ni_vp, &fromnd.ni_cnd);
3529 VOP_UNLOCK(fromnd.ni_dvp);
3530 if (fromnd.ni_dvp != fromnd.ni_vp)
3531 VOP_UNLOCK(fromnd.ni_vp);
3534 NDINIT_ATRIGHTS(&tond, RENAME, LOCKPARENT | LOCKLEAF | NOCACHE |
3535 SAVESTART | AUDITVNODE2, pathseg, new, newfd,
3536 &cap_renameat_target_rights, td);
3537 if (fromnd.ni_vp->v_type == VDIR)
3538 tond.ni_cnd.cn_flags |= WILLBEDIR;
3539 if ((error = namei(&tond)) != 0) {
3540 /* Translate error code for rename("dir1", "dir2/."). */
3541 if (error == EISDIR && fvp->v_type == VDIR)
3543 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3544 vrele(fromnd.ni_dvp);
3550 error = vn_start_write(fvp, &mp, V_NOWAIT);
3552 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3553 NDFREE(&tond, NDF_ONLY_PNBUF);
3560 vrele(fromnd.ni_dvp);
3562 vrele(tond.ni_startdir);
3563 if (fromnd.ni_startdir != NULL)
3564 vrele(fromnd.ni_startdir);
3565 error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH);
3571 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
3574 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
3579 if (newfd != AT_FDCWD && (tond.ni_resflags & NIRES_ABS) == 0) {
3581 * If the target already exists we require CAP_UNLINKAT
3582 * from 'newfd', when newfd was used for the lookup.
3584 error = cap_check(&tond.ni_filecaps.fc_rights,
3585 &cap_unlinkat_rights);
3596 * If the source is the same as the destination (that is, if they
3597 * are links to the same vnode), then there is nothing to do.
3603 error = mac_vnode_check_rename_to(td->td_ucred, tdvp,
3604 tond.ni_vp, fromnd.ni_dvp == tdvp, &tond.ni_cnd);
3608 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
3609 tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
3610 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3611 NDFREE(&tond, NDF_ONLY_PNBUF);
3613 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3614 NDFREE(&tond, NDF_ONLY_PNBUF);
3621 vrele(fromnd.ni_dvp);
3624 vrele(tond.ni_startdir);
3625 vn_finished_write(mp);
3627 if (fromnd.ni_startdir)
3628 vrele(fromnd.ni_startdir);
3635 * Make a directory file.
3637 #ifndef _SYS_SYSPROTO_H_
3644 sys_mkdir(struct thread *td, struct mkdir_args *uap)
3647 return (kern_mkdirat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
3651 #ifndef _SYS_SYSPROTO_H_
3652 struct mkdirat_args {
3659 sys_mkdirat(struct thread *td, struct mkdirat_args *uap)
3662 return (kern_mkdirat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode));
3666 kern_mkdirat(struct thread *td, int fd, const char *path, enum uio_seg segflg,
3672 struct nameidata nd;
3675 AUDIT_ARG_MODE(mode);
3678 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
3679 NOCACHE, segflg, path, fd, &cap_mkdirat_rights,
3681 nd.ni_cnd.cn_flags |= WILLBEDIR;
3682 if ((error = namei(&nd)) != 0)
3686 NDFREE(&nd, NDF_ONLY_PNBUF);
3688 * XXX namei called with LOCKPARENT but not LOCKLEAF has
3689 * the strange behaviour of leaving the vnode unlocked
3690 * if the target is the same vnode as the parent.
3692 if (vp == nd.ni_dvp)
3699 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3700 NDFREE(&nd, NDF_ONLY_PNBUF);
3702 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3707 vattr.va_type = VDIR;
3708 vattr.va_mode = (mode & ACCESSPERMS) &~ td->td_proc->p_fd->fd_cmask;
3710 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
3715 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
3719 NDFREE(&nd, NDF_ONLY_PNBUF);
3723 vn_finished_write(mp);
3728 * Remove a directory file.
3730 #ifndef _SYS_SYSPROTO_H_
3736 sys_rmdir(struct thread *td, struct rmdir_args *uap)
3739 return (kern_frmdirat(td, AT_FDCWD, uap->path, FD_NONE, UIO_USERSPACE,
3744 kern_frmdirat(struct thread *td, int dfd, const char *path, int fd,
3745 enum uio_seg pathseg, int flag)
3750 struct nameidata nd;
3751 cap_rights_t rights;
3755 if (fd != FD_NONE) {
3756 error = getvnode(td, fd, cap_rights_init_one(&rights, CAP_LOOKUP),
3764 NDINIT_ATRIGHTS(&nd, DELETE, LOCKPARENT | LOCKLEAF | AUDITVNODE1 |
3765 ((flag & AT_BENEATH) != 0 ? BENEATH : 0),
3766 pathseg, path, dfd, &cap_unlinkat_rights, td);
3767 if ((error = namei(&nd)) != 0)
3770 if (vp->v_type != VDIR) {
3775 * No rmdir "." please.
3777 if (nd.ni_dvp == vp) {
3782 * The root of a mounted filesystem cannot be deleted.
3784 if (vp->v_vflag & VV_ROOT) {
3789 if (fp != NULL && fp->f_vnode != vp) {
3790 if (VN_IS_DOOMED(fp->f_vnode))
3798 error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp,
3803 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3804 NDFREE(&nd, NDF_ONLY_PNBUF);
3806 if (nd.ni_dvp == vp)
3810 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3814 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_UNLINK);
3815 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
3816 vn_finished_write(mp);
3818 NDFREE(&nd, NDF_ONLY_PNBUF);
3820 if (nd.ni_dvp == vp)
3830 #if defined(COMPAT_43) || defined(COMPAT_FREEBSD11)
3832 freebsd11_kern_getdirentries(struct thread *td, int fd, char *ubuf, u_int count,
3833 long *basep, void (*func)(struct freebsd11_dirent *))
3835 struct freebsd11_dirent dstdp;
3836 struct dirent *dp, *edp;
3839 ssize_t resid, ucount;
3842 /* XXX arbitrary sanity limit on `count'. */
3843 count = min(count, 64 * 1024);
3845 dirbuf = malloc(count, M_TEMP, M_WAITOK);
3847 error = kern_getdirentries(td, fd, dirbuf, count, &base, &resid,
3855 for (dp = (struct dirent *)dirbuf,
3856 edp = (struct dirent *)&dirbuf[count - resid];
3857 ucount < count && dp < edp; ) {
3858 if (dp->d_reclen == 0)
3860 MPASS(dp->d_reclen >= _GENERIC_DIRLEN(0));
3861 if (dp->d_namlen >= sizeof(dstdp.d_name))
3863 dstdp.d_type = dp->d_type;
3864 dstdp.d_namlen = dp->d_namlen;
3865 dstdp.d_fileno = dp->d_fileno; /* truncate */
3866 if (dstdp.d_fileno != dp->d_fileno) {
3867 switch (ino64_trunc_error) {
3875 dstdp.d_fileno = UINT32_MAX;
3879 dstdp.d_reclen = sizeof(dstdp) - sizeof(dstdp.d_name) +
3880 ((dp->d_namlen + 1 + 3) &~ 3);
3881 bcopy(dp->d_name, dstdp.d_name, dstdp.d_namlen);
3882 bzero(dstdp.d_name + dstdp.d_namlen,
3883 dstdp.d_reclen - offsetof(struct freebsd11_dirent, d_name) -
3885 MPASS(dstdp.d_reclen <= dp->d_reclen);
3886 MPASS(ucount + dstdp.d_reclen <= count);
3889 error = copyout(&dstdp, ubuf + ucount, dstdp.d_reclen);
3892 dp = (struct dirent *)((char *)dp + dp->d_reclen);
3893 ucount += dstdp.d_reclen;
3897 free(dirbuf, M_TEMP);
3899 td->td_retval[0] = ucount;
3906 ogetdirentries_cvt(struct freebsd11_dirent *dp)
3908 #if (BYTE_ORDER == LITTLE_ENDIAN)
3910 * The expected low byte of dp->d_namlen is our dp->d_type.
3911 * The high MBZ byte of dp->d_namlen is our dp->d_namlen.
3913 dp->d_type = dp->d_namlen;
3917 * The dp->d_type is the high byte of the expected dp->d_namlen,
3918 * so must be zero'ed.
3925 * Read a block of directory entries in a filesystem independent format.
3927 #ifndef _SYS_SYSPROTO_H_
3928 struct ogetdirentries_args {
3936 ogetdirentries(struct thread *td, struct ogetdirentries_args *uap)
3941 error = kern_ogetdirentries(td, uap, &loff);
3943 error = copyout(&loff, uap->basep, sizeof(long));
3948 kern_ogetdirentries(struct thread *td, struct ogetdirentries_args *uap,
3954 /* XXX arbitrary sanity limit on `count'. */
3955 if (uap->count > 64 * 1024)
3958 error = freebsd11_kern_getdirentries(td, uap->fd, uap->buf, uap->count,
3959 &base, ogetdirentries_cvt);
3961 if (error == 0 && uap->basep != NULL)
3962 error = copyout(&base, uap->basep, sizeof(long));
3966 #endif /* COMPAT_43 */
3968 #if defined(COMPAT_FREEBSD11)
3969 #ifndef _SYS_SYSPROTO_H_
3970 struct freebsd11_getdirentries_args {
3978 freebsd11_getdirentries(struct thread *td,
3979 struct freebsd11_getdirentries_args *uap)
3984 error = freebsd11_kern_getdirentries(td, uap->fd, uap->buf, uap->count,
3987 if (error == 0 && uap->basep != NULL)
3988 error = copyout(&base, uap->basep, sizeof(long));
3993 freebsd11_getdents(struct thread *td, struct freebsd11_getdents_args *uap)
3995 struct freebsd11_getdirentries_args ap;
3999 ap.count = uap->count;
4001 return (freebsd11_getdirentries(td, &ap));
4003 #endif /* COMPAT_FREEBSD11 */
4006 * Read a block of directory entries in a filesystem independent format.
4009 sys_getdirentries(struct thread *td, struct getdirentries_args *uap)
4014 error = kern_getdirentries(td, uap->fd, uap->buf, uap->count, &base,
4015 NULL, UIO_USERSPACE);
4018 if (uap->basep != NULL)
4019 error = copyout(&base, uap->basep, sizeof(off_t));
4024 kern_getdirentries(struct thread *td, int fd, char *buf, size_t count,
4025 off_t *basep, ssize_t *residp, enum uio_seg bufseg)
4036 if (count > IOSIZE_MAX)
4038 auio.uio_resid = count;
4039 error = getvnode(td, fd, &cap_read_rights, &fp);
4042 if ((fp->f_flag & FREAD) == 0) {
4047 foffset = foffset_lock(fp, 0);
4049 if (vp->v_type != VDIR) {
4053 aiov.iov_base = buf;
4054 aiov.iov_len = count;
4055 auio.uio_iov = &aiov;
4056 auio.uio_iovcnt = 1;
4057 auio.uio_rw = UIO_READ;
4058 auio.uio_segflg = bufseg;
4060 vn_lock(vp, LK_SHARED | LK_RETRY);
4061 AUDIT_ARG_VNODE1(vp);
4062 loff = auio.uio_offset = foffset;
4064 error = mac_vnode_check_readdir(td->td_ucred, vp);
4067 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL,
4069 foffset = auio.uio_offset;
4074 if (count == auio.uio_resid &&
4075 (vp->v_vflag & VV_ROOT) &&
4076 (vp->v_mount->mnt_flag & MNT_UNION)) {
4077 struct vnode *tvp = vp;
4079 vp = vp->v_mount->mnt_vnodecovered;
4090 *residp = auio.uio_resid;
4091 td->td_retval[0] = count - auio.uio_resid;
4093 foffset_unlock(fp, foffset, 0);
4099 * Set the mode mask for creation of filesystem nodes.
4101 #ifndef _SYS_SYSPROTO_H_
4107 sys_umask(struct thread *td, struct umask_args *uap)
4109 struct filedesc *fdp;
4111 fdp = td->td_proc->p_fd;
4112 FILEDESC_XLOCK(fdp);
4113 td->td_retval[0] = fdp->fd_cmask;
4114 fdp->fd_cmask = uap->newmask & ALLPERMS;
4115 FILEDESC_XUNLOCK(fdp);
4120 * Void all references to file by ripping underlying filesystem away from
4123 #ifndef _SYS_SYSPROTO_H_
4124 struct revoke_args {
4129 sys_revoke(struct thread *td, struct revoke_args *uap)
4133 struct nameidata nd;
4136 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
4138 if ((error = namei(&nd)) != 0)
4141 NDFREE(&nd, NDF_ONLY_PNBUF);
4142 if (vp->v_type != VCHR || vp->v_rdev == NULL) {
4147 error = mac_vnode_check_revoke(td->td_ucred, vp);
4151 error = VOP_GETATTR(vp, &vattr, td->td_ucred);
4154 if (td->td_ucred->cr_uid != vattr.va_uid) {
4155 error = priv_check(td, PRIV_VFS_ADMIN);
4159 if (vp->v_usecount > 1 || vcount(vp) > 1)
4160 VOP_REVOKE(vp, REVOKEALL);
4167 * Convert a user file descriptor to a kernel file entry and check that, if it
4168 * is a capability, the correct rights are present. A reference on the file
4169 * entry is held upon returning.
4172 getvnode(struct thread *td, int fd, cap_rights_t *rightsp, struct file **fpp)
4177 error = fget_unlocked(td->td_proc->p_fd, fd, rightsp, &fp);
4182 * The file could be not of the vnode type, or it may be not
4183 * yet fully initialized, in which case the f_vnode pointer
4184 * may be set, but f_ops is still badfileops. E.g.,
4185 * devfs_open() transiently create such situation to
4186 * facilitate csw d_fdopen().
4188 * Dupfdopen() handling in kern_openat() installs the
4189 * half-baked file into the process descriptor table, allowing
4190 * other thread to dereference it. Guard against the race by
4193 if (fp->f_vnode == NULL || fp->f_ops == &badfileops) {
4202 * Get an (NFS) file handle.
4204 #ifndef _SYS_SYSPROTO_H_
4205 struct lgetfh_args {
4211 sys_lgetfh(struct thread *td, struct lgetfh_args *uap)
4214 return (kern_getfhat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->fname,
4215 UIO_USERSPACE, uap->fhp));
4218 #ifndef _SYS_SYSPROTO_H_
4225 sys_getfh(struct thread *td, struct getfh_args *uap)
4228 return (kern_getfhat(td, 0, AT_FDCWD, uap->fname, UIO_USERSPACE,
4233 * syscall for the rpc.lockd to use to translate an open descriptor into
4234 * a NFS file handle.
4236 * warning: do not remove the priv_check() call or this becomes one giant
4239 #ifndef _SYS_SYSPROTO_H_
4240 struct getfhat_args {
4248 sys_getfhat(struct thread *td, struct getfhat_args *uap)
4251 if ((uap->flags & ~(AT_SYMLINK_NOFOLLOW | AT_BENEATH)) != 0)
4253 return (kern_getfhat(td, uap->flags, uap->fd, uap->path, UIO_USERSPACE,
4258 kern_getfhat(struct thread *td, int flags, int fd, const char *path,
4259 enum uio_seg pathseg, fhandle_t *fhp)
4261 struct nameidata nd;
4266 error = priv_check(td, PRIV_VFS_GETFH);
4269 NDINIT_AT(&nd, LOOKUP, ((flags & AT_SYMLINK_NOFOLLOW) != 0 ? NOFOLLOW :
4270 FOLLOW) | ((flags & AT_BENEATH) != 0 ? BENEATH : 0) | LOCKLEAF |
4271 AUDITVNODE1, pathseg, path, fd, td);
4275 NDFREE(&nd, NDF_ONLY_PNBUF);
4277 bzero(&fh, sizeof(fh));
4278 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
4279 error = VOP_VPTOFH(vp, &fh.fh_fid);
4282 error = copyout(&fh, fhp, sizeof (fh));
4286 #ifndef _SYS_SYSPROTO_H_
4287 struct fhlink_args {
4293 sys_fhlink(struct thread *td, struct fhlink_args *uap)
4296 return (kern_fhlinkat(td, AT_FDCWD, uap->to, UIO_USERSPACE, uap->fhp));
4299 #ifndef _SYS_SYSPROTO_H_
4300 struct fhlinkat_args {
4307 sys_fhlinkat(struct thread *td, struct fhlinkat_args *uap)
4310 return (kern_fhlinkat(td, uap->tofd, uap->to, UIO_USERSPACE, uap->fhp));
4314 kern_fhlinkat(struct thread *td, int fd, const char *path,
4315 enum uio_seg pathseg, fhandle_t *fhp)
4322 error = priv_check(td, PRIV_VFS_GETFH);
4325 error = copyin(fhp, &fh, sizeof(fh));
4330 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4332 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_SHARED, &vp);
4337 } while ((error = kern_linkat_vp(td, vp, fd, path, pathseg)) == EAGAIN);
4341 #ifndef _SYS_SYSPROTO_H_
4342 struct fhreadlink_args {
4349 sys_fhreadlink(struct thread *td, struct fhreadlink_args *uap)
4356 error = priv_check(td, PRIV_VFS_GETFH);
4359 if (uap->bufsize > IOSIZE_MAX)
4361 error = copyin(uap->fhp, &fh, sizeof(fh));
4364 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4366 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_SHARED, &vp);
4370 error = kern_readlink_vp(vp, uap->buf, UIO_USERSPACE, uap->bufsize, td);
4376 * syscall for the rpc.lockd to use to translate a NFS file handle into an
4379 * warning: do not remove the priv_check() call or this becomes one giant
4382 #ifndef _SYS_SYSPROTO_H_
4383 struct fhopen_args {
4384 const struct fhandle *u_fhp;
4389 sys_fhopen(struct thread *td, struct fhopen_args *uap)
4398 error = priv_check(td, PRIV_VFS_FHOPEN);
4402 fmode = FFLAGS(uap->flags);
4403 /* why not allow a non-read/write open for our lockd? */
4404 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
4406 error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
4409 /* find the mount point */
4410 mp = vfs_busyfs(&fhp.fh_fsid);
4413 /* now give me my vnode, it gets returned to me locked */
4414 error = VFS_FHTOVP(mp, &fhp.fh_fid, LK_EXCLUSIVE, &vp);
4419 error = falloc_noinstall(td, &fp);
4425 * An extra reference on `fp' has been held for us by
4426 * falloc_noinstall().
4432 error = vn_open_vnode(vp, fmode, td->td_ucred, td, fp);
4434 KASSERT(fp->f_ops == &badfileops,
4435 ("VOP_OPEN in fhopen() set f_ops"));
4436 KASSERT(td->td_dupfd < 0,
4437 ("fhopen() encountered fdopen()"));
4446 fp->f_seqcount[UIO_READ] = 1;
4447 fp->f_seqcount[UIO_WRITE] = 1;
4448 finit(fp, (fmode & FMASK) | (fp->f_flag & FHASLOCK), DTYPE_VNODE, vp,
4451 if ((fmode & O_TRUNC) != 0) {
4452 error = fo_truncate(fp, 0, td->td_ucred, td);
4457 error = finstall(td, fp, &indx, fmode, NULL);
4460 td->td_retval[0] = indx;
4465 * Stat an (NFS) file handle.
4467 #ifndef _SYS_SYSPROTO_H_
4468 struct fhstat_args {
4469 struct fhandle *u_fhp;
4474 sys_fhstat(struct thread *td, struct fhstat_args *uap)
4480 error = copyin(uap->u_fhp, &fh, sizeof(fh));
4483 error = kern_fhstat(td, fh, &sb);
4485 error = copyout(&sb, uap->sb, sizeof(sb));
4490 kern_fhstat(struct thread *td, struct fhandle fh, struct stat *sb)
4496 error = priv_check(td, PRIV_VFS_FHSTAT);
4499 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4501 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_EXCLUSIVE, &vp);
4505 error = vn_stat(vp, sb, td->td_ucred, NOCRED, td);
4511 * Implement fstatfs() for (NFS) file handles.
4513 #ifndef _SYS_SYSPROTO_H_
4514 struct fhstatfs_args {
4515 struct fhandle *u_fhp;
4520 sys_fhstatfs(struct thread *td, struct fhstatfs_args *uap)
4526 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4529 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
4530 error = kern_fhstatfs(td, fh, sfp);
4532 error = copyout(sfp, uap->buf, sizeof(*sfp));
4533 free(sfp, M_STATFS);
4538 kern_fhstatfs(struct thread *td, fhandle_t fh, struct statfs *buf)
4544 error = priv_check(td, PRIV_VFS_FHSTATFS);
4547 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4549 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_EXCLUSIVE, &vp);
4555 error = prison_canseemount(td->td_ucred, mp);
4559 error = mac_mount_check_stat(td->td_ucred, mp);
4563 error = VFS_STATFS(mp, buf);
4570 * Unlike madvise(2), we do not make a best effort to remember every
4571 * possible caching hint. Instead, we remember the last setting with
4572 * the exception that we will allow POSIX_FADV_NORMAL to adjust the
4573 * region of any current setting.
4576 kern_posix_fadvise(struct thread *td, int fd, off_t offset, off_t len,
4579 struct fadvise_info *fa, *new;
4585 if (offset < 0 || len < 0 || offset > OFF_MAX - len)
4587 AUDIT_ARG_VALUE(advice);
4589 case POSIX_FADV_SEQUENTIAL:
4590 case POSIX_FADV_RANDOM:
4591 case POSIX_FADV_NOREUSE:
4592 new = malloc(sizeof(*fa), M_FADVISE, M_WAITOK);
4594 case POSIX_FADV_NORMAL:
4595 case POSIX_FADV_WILLNEED:
4596 case POSIX_FADV_DONTNEED:
4602 /* XXX: CAP_POSIX_FADVISE? */
4604 error = fget(td, fd, &cap_no_rights, &fp);
4607 AUDIT_ARG_FILE(td->td_proc, fp);
4608 if ((fp->f_ops->fo_flags & DFLAG_SEEKABLE) == 0) {
4612 if (fp->f_type != DTYPE_VNODE) {
4617 if (vp->v_type != VREG) {
4624 end = offset + len - 1;
4626 case POSIX_FADV_SEQUENTIAL:
4627 case POSIX_FADV_RANDOM:
4628 case POSIX_FADV_NOREUSE:
4630 * Try to merge any existing non-standard region with
4631 * this new region if possible, otherwise create a new
4632 * non-standard region for this request.
4634 mtx_pool_lock(mtxpool_sleep, fp);
4636 if (fa != NULL && fa->fa_advice == advice &&
4637 ((fa->fa_start <= end && fa->fa_end >= offset) ||
4638 (end != OFF_MAX && fa->fa_start == end + 1) ||
4639 (fa->fa_end != OFF_MAX && fa->fa_end + 1 == offset))) {
4640 if (offset < fa->fa_start)
4641 fa->fa_start = offset;
4642 if (end > fa->fa_end)
4645 new->fa_advice = advice;
4646 new->fa_start = offset;
4651 mtx_pool_unlock(mtxpool_sleep, fp);
4653 case POSIX_FADV_NORMAL:
4655 * If a the "normal" region overlaps with an existing
4656 * non-standard region, trim or remove the
4657 * non-standard region.
4659 mtx_pool_lock(mtxpool_sleep, fp);
4662 if (offset <= fa->fa_start && end >= fa->fa_end) {
4664 fp->f_advice = NULL;
4665 } else if (offset <= fa->fa_start &&
4666 end >= fa->fa_start)
4667 fa->fa_start = end + 1;
4668 else if (offset <= fa->fa_end && end >= fa->fa_end)
4669 fa->fa_end = offset - 1;
4670 else if (offset >= fa->fa_start && end <= fa->fa_end) {
4672 * If the "normal" region is a middle
4673 * portion of the existing
4674 * non-standard region, just remove
4675 * the whole thing rather than picking
4676 * one side or the other to
4680 fp->f_advice = NULL;
4683 mtx_pool_unlock(mtxpool_sleep, fp);
4685 case POSIX_FADV_WILLNEED:
4686 case POSIX_FADV_DONTNEED:
4687 error = VOP_ADVISE(vp, offset, end, advice);
4693 free(new, M_FADVISE);
4698 sys_posix_fadvise(struct thread *td, struct posix_fadvise_args *uap)
4702 error = kern_posix_fadvise(td, uap->fd, uap->offset, uap->len,
4704 return (kern_posix_error(td, error));
4708 kern_copy_file_range(struct thread *td, int infd, off_t *inoffp, int outfd,
4709 off_t *outoffp, size_t len, unsigned int flags)
4711 struct file *infp, *outfp;
4712 struct vnode *invp, *outvp;
4715 void *rl_rcookie, *rl_wcookie;
4716 off_t savinoff, savoutoff;
4718 infp = outfp = NULL;
4719 rl_rcookie = rl_wcookie = NULL;
4728 if (len > SSIZE_MAX)
4730 * Although the len argument is size_t, the return argument
4731 * is ssize_t (which is signed). Therefore a size that won't
4732 * fit in ssize_t can't be returned.
4736 /* Get the file structures for the file descriptors. */
4737 error = fget_read(td, infd, &cap_read_rights, &infp);
4740 if (infp->f_ops == &badfileops) {
4744 if (infp->f_vnode == NULL) {
4748 error = fget_write(td, outfd, &cap_write_rights, &outfp);
4751 if (outfp->f_ops == &badfileops) {
4755 if (outfp->f_vnode == NULL) {
4760 /* Set the offset pointers to the correct place. */
4762 inoffp = &infp->f_offset;
4763 if (outoffp == NULL)
4764 outoffp = &outfp->f_offset;
4766 savoutoff = *outoffp;
4768 invp = infp->f_vnode;
4769 outvp = outfp->f_vnode;
4770 /* Sanity check the f_flag bits. */
4771 if ((outfp->f_flag & (FWRITE | FAPPEND)) != FWRITE ||
4772 (infp->f_flag & FREAD) == 0) {
4777 /* If len == 0, just return 0. */
4782 * If infp and outfp refer to the same file, the byte ranges cannot
4785 if (invp == outvp && ((savinoff <= savoutoff && savinoff + len >
4786 savoutoff) || (savinoff > savoutoff && savoutoff + len >
4792 /* Range lock the byte ranges for both invp and outvp. */
4794 rl_wcookie = vn_rangelock_wlock(outvp, *outoffp, *outoffp +
4796 rl_rcookie = vn_rangelock_tryrlock(invp, *inoffp, *inoffp +
4798 if (rl_rcookie != NULL)
4800 vn_rangelock_unlock(outvp, rl_wcookie);
4801 rl_rcookie = vn_rangelock_rlock(invp, *inoffp, *inoffp + len);
4802 vn_rangelock_unlock(invp, rl_rcookie);
4806 error = vn_copy_file_range(invp, inoffp, outvp, outoffp, &retlen,
4807 flags, infp->f_cred, outfp->f_cred, td);
4809 if (rl_rcookie != NULL)
4810 vn_rangelock_unlock(invp, rl_rcookie);
4811 if (rl_wcookie != NULL)
4812 vn_rangelock_unlock(outvp, rl_wcookie);
4813 if (savinoff != -1 && (error == EINTR || error == ERESTART)) {
4815 *outoffp = savoutoff;
4821 td->td_retval[0] = retlen;
4826 sys_copy_file_range(struct thread *td, struct copy_file_range_args *uap)
4828 off_t inoff, outoff, *inoffp, *outoffp;
4831 inoffp = outoffp = NULL;
4832 if (uap->inoffp != NULL) {
4833 error = copyin(uap->inoffp, &inoff, sizeof(off_t));
4838 if (uap->outoffp != NULL) {
4839 error = copyin(uap->outoffp, &outoff, sizeof(off_t));
4844 error = kern_copy_file_range(td, uap->infd, inoffp, uap->outfd,
4845 outoffp, uap->len, uap->flags);
4846 if (error == 0 && uap->inoffp != NULL)
4847 error = copyout(inoffp, uap->inoffp, sizeof(off_t));
4848 if (error == 0 && uap->outoffp != NULL)
4849 error = copyout(outoffp, uap->outoffp, sizeof(off_t));