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 * Sync each mounted filesystem.
120 #ifndef _SYS_SYSPROTO_H_
127 sys_sync(struct thread *td, struct sync_args *uap)
129 struct mount *mp, *nmp;
132 mtx_lock(&mountlist_mtx);
133 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
134 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK)) {
135 nmp = TAILQ_NEXT(mp, mnt_list);
138 if ((mp->mnt_flag & MNT_RDONLY) == 0 &&
139 vn_start_write(NULL, &mp, V_NOWAIT) == 0) {
140 save = curthread_pflags_set(TDP_SYNCIO);
141 vfs_msync(mp, MNT_NOWAIT);
142 VFS_SYNC(mp, MNT_NOWAIT);
143 curthread_pflags_restore(save);
144 vn_finished_write(mp);
146 mtx_lock(&mountlist_mtx);
147 nmp = TAILQ_NEXT(mp, mnt_list);
150 mtx_unlock(&mountlist_mtx);
155 * Change filesystem quotas.
157 #ifndef _SYS_SYSPROTO_H_
158 struct quotactl_args {
166 sys_quotactl(struct thread *td, struct quotactl_args *uap)
172 AUDIT_ARG_CMD(uap->cmd);
173 AUDIT_ARG_UID(uap->uid);
174 if (!prison_allow(td->td_ucred, PR_ALLOW_QUOTAS))
176 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
178 if ((error = namei(&nd)) != 0)
180 NDFREE(&nd, NDF_ONLY_PNBUF);
181 mp = nd.ni_vp->v_mount;
184 error = vfs_busy(mp, 0);
188 error = VFS_QUOTACTL(mp, uap->cmd, uap->uid, uap->arg);
191 * Since quota on operation typically needs to open quota
192 * file, the Q_QUOTAON handler needs to unbusy the mount point
193 * before calling into namei. Otherwise, unmount might be
194 * started between two vfs_busy() invocations (first is our,
195 * second is from mount point cross-walk code in lookup()),
198 * Require that Q_QUOTAON handles the vfs_busy() reference on
199 * its own, always returning with ubusied mount point.
201 if ((uap->cmd >> SUBCMDSHIFT) != Q_QUOTAON &&
202 (uap->cmd >> SUBCMDSHIFT) != Q_QUOTAOFF)
208 * Used by statfs conversion routines to scale the block size up if
209 * necessary so that all of the block counts are <= 'max_size'. Note
210 * that 'max_size' should be a bitmask, i.e. 2^n - 1 for some non-zero
214 statfs_scale_blocks(struct statfs *sf, long max_size)
219 KASSERT(powerof2(max_size + 1), ("%s: invalid max_size", __func__));
222 * Attempt to scale the block counts to give a more accurate
223 * overview to userland of the ratio of free space to used
224 * space. To do this, find the largest block count and compute
225 * a divisor that lets it fit into a signed integer <= max_size.
227 if (sf->f_bavail < 0)
228 count = -sf->f_bavail;
230 count = sf->f_bavail;
231 count = MAX(sf->f_blocks, MAX(sf->f_bfree, count));
232 if (count <= max_size)
235 count >>= flsl(max_size);
242 sf->f_bsize <<= shift;
243 sf->f_blocks >>= shift;
244 sf->f_bfree >>= shift;
245 sf->f_bavail >>= shift;
249 kern_do_statfs(struct thread *td, struct mount *mp, struct statfs *buf)
255 error = vfs_busy(mp, 0);
260 error = mac_mount_check_stat(td->td_ucred, mp);
264 error = VFS_STATFS(mp, buf);
267 if (priv_check(td, PRIV_VFS_GENERATION)) {
268 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
269 prison_enforce_statfs(td->td_ucred, mp, buf);
277 * Get filesystem statistics.
279 #ifndef _SYS_SYSPROTO_H_
286 sys_statfs(struct thread *td, struct statfs_args *uap)
291 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
292 error = kern_statfs(td, uap->path, UIO_USERSPACE, sfp);
294 error = copyout(sfp, uap->buf, sizeof(struct statfs));
300 kern_statfs(struct thread *td, const char *path, enum uio_seg pathseg,
307 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
312 mp = nd.ni_vp->v_mount;
314 NDFREE(&nd, NDF_ONLY_PNBUF);
316 return (kern_do_statfs(td, mp, buf));
320 * Get filesystem statistics.
322 #ifndef _SYS_SYSPROTO_H_
323 struct fstatfs_args {
329 sys_fstatfs(struct thread *td, struct fstatfs_args *uap)
334 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
335 error = kern_fstatfs(td, uap->fd, sfp);
337 error = copyout(sfp, uap->buf, sizeof(struct statfs));
343 kern_fstatfs(struct thread *td, int fd, struct statfs *buf)
351 error = getvnode(td, fd, &cap_fstatfs_rights, &fp);
355 vn_lock(vp, LK_SHARED | LK_RETRY);
357 AUDIT_ARG_VNODE1(vp);
364 return (kern_do_statfs(td, mp, buf));
368 * Get statistics on all filesystems.
370 #ifndef _SYS_SYSPROTO_H_
371 struct getfsstat_args {
378 sys_getfsstat(struct thread *td, struct getfsstat_args *uap)
383 if (uap->bufsize < 0 || uap->bufsize > SIZE_MAX)
385 error = kern_getfsstat(td, &uap->buf, uap->bufsize, &count,
386 UIO_USERSPACE, uap->mode);
388 td->td_retval[0] = count;
393 * If (bufsize > 0 && bufseg == UIO_SYSSPACE)
394 * The caller is responsible for freeing memory which will be allocated
398 kern_getfsstat(struct thread *td, struct statfs **buf, size_t bufsize,
399 size_t *countp, enum uio_seg bufseg, int mode)
401 struct mount *mp, *nmp;
402 struct statfs *sfsp, *sp, *sptmp, *tofree;
403 size_t count, maxcount;
411 if (bufseg == UIO_SYSSPACE)
416 maxcount = bufsize / sizeof(struct statfs);
420 } else if (bufseg == UIO_USERSPACE) {
423 } else /* if (bufseg == UIO_SYSSPACE) */ {
425 mtx_lock(&mountlist_mtx);
426 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
429 mtx_unlock(&mountlist_mtx);
430 if (maxcount > count)
432 tofree = sfsp = *buf = malloc(maxcount * sizeof(struct statfs),
436 mtx_lock(&mountlist_mtx);
437 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
438 if (prison_canseemount(td->td_ucred, mp) != 0) {
439 nmp = TAILQ_NEXT(mp, mnt_list);
443 if (mac_mount_check_stat(td->td_ucred, mp) != 0) {
444 nmp = TAILQ_NEXT(mp, mnt_list);
448 if (mode == MNT_WAIT) {
449 if (vfs_busy(mp, MBF_MNTLSTLOCK) != 0) {
451 * If vfs_busy() failed, and MBF_NOWAIT
452 * wasn't passed, then the mp is gone.
453 * Furthermore, because of MBF_MNTLSTLOCK,
454 * the mountlist_mtx was dropped. We have
455 * no other choice than to start over.
457 mtx_unlock(&mountlist_mtx);
458 free(tofree, M_STATFS);
462 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK) != 0) {
463 nmp = TAILQ_NEXT(mp, mnt_list);
467 if (sfsp != NULL && count < maxcount) {
470 * If MNT_NOWAIT is specified, do not refresh
473 if (mode != MNT_NOWAIT) {
474 error = VFS_STATFS(mp, sp);
476 mtx_lock(&mountlist_mtx);
477 nmp = TAILQ_NEXT(mp, mnt_list);
482 if (priv_check(td, PRIV_VFS_GENERATION)) {
483 sptmp = malloc(sizeof(struct statfs), M_STATFS,
486 sptmp->f_fsid.val[0] = sptmp->f_fsid.val[1] = 0;
487 prison_enforce_statfs(td->td_ucred, mp, sptmp);
491 if (bufseg == UIO_SYSSPACE) {
492 bcopy(sp, sfsp, sizeof(*sp));
493 free(sptmp, M_STATFS);
494 } else /* if (bufseg == UIO_USERSPACE) */ {
495 error = copyout(sp, sfsp, sizeof(*sp));
496 free(sptmp, M_STATFS);
505 mtx_lock(&mountlist_mtx);
506 nmp = TAILQ_NEXT(mp, mnt_list);
509 mtx_unlock(&mountlist_mtx);
510 if (sfsp != NULL && count > maxcount)
517 #ifdef COMPAT_FREEBSD4
519 * Get old format filesystem statistics.
521 static void freebsd4_cvtstatfs(struct statfs *, struct ostatfs *);
523 #ifndef _SYS_SYSPROTO_H_
524 struct freebsd4_statfs_args {
530 freebsd4_statfs(struct thread *td, struct freebsd4_statfs_args *uap)
536 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
537 error = kern_statfs(td, uap->path, UIO_USERSPACE, sfp);
539 freebsd4_cvtstatfs(sfp, &osb);
540 error = copyout(&osb, uap->buf, sizeof(osb));
547 * Get filesystem statistics.
549 #ifndef _SYS_SYSPROTO_H_
550 struct freebsd4_fstatfs_args {
556 freebsd4_fstatfs(struct thread *td, struct freebsd4_fstatfs_args *uap)
562 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
563 error = kern_fstatfs(td, uap->fd, sfp);
565 freebsd4_cvtstatfs(sfp, &osb);
566 error = copyout(&osb, uap->buf, sizeof(osb));
573 * Get statistics on all filesystems.
575 #ifndef _SYS_SYSPROTO_H_
576 struct freebsd4_getfsstat_args {
583 freebsd4_getfsstat(struct thread *td, struct freebsd4_getfsstat_args *uap)
585 struct statfs *buf, *sp;
590 if (uap->bufsize < 0)
592 count = uap->bufsize / sizeof(struct ostatfs);
593 if (count > SIZE_MAX / sizeof(struct statfs))
595 size = count * sizeof(struct statfs);
596 error = kern_getfsstat(td, &buf, size, &count, UIO_SYSSPACE,
599 td->td_retval[0] = count;
602 while (count != 0 && error == 0) {
603 freebsd4_cvtstatfs(sp, &osb);
604 error = copyout(&osb, uap->buf, sizeof(osb));
615 * Implement fstatfs() for (NFS) file handles.
617 #ifndef _SYS_SYSPROTO_H_
618 struct freebsd4_fhstatfs_args {
619 struct fhandle *u_fhp;
624 freebsd4_fhstatfs(struct thread *td, struct freebsd4_fhstatfs_args *uap)
631 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
634 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
635 error = kern_fhstatfs(td, fh, sfp);
637 freebsd4_cvtstatfs(sfp, &osb);
638 error = copyout(&osb, uap->buf, sizeof(osb));
645 * Convert a new format statfs structure to an old format statfs structure.
648 freebsd4_cvtstatfs(struct statfs *nsp, struct ostatfs *osp)
651 statfs_scale_blocks(nsp, LONG_MAX);
652 bzero(osp, sizeof(*osp));
653 osp->f_bsize = nsp->f_bsize;
654 osp->f_iosize = MIN(nsp->f_iosize, LONG_MAX);
655 osp->f_blocks = nsp->f_blocks;
656 osp->f_bfree = nsp->f_bfree;
657 osp->f_bavail = nsp->f_bavail;
658 osp->f_files = MIN(nsp->f_files, LONG_MAX);
659 osp->f_ffree = MIN(nsp->f_ffree, LONG_MAX);
660 osp->f_owner = nsp->f_owner;
661 osp->f_type = nsp->f_type;
662 osp->f_flags = nsp->f_flags;
663 osp->f_syncwrites = MIN(nsp->f_syncwrites, LONG_MAX);
664 osp->f_asyncwrites = MIN(nsp->f_asyncwrites, LONG_MAX);
665 osp->f_syncreads = MIN(nsp->f_syncreads, LONG_MAX);
666 osp->f_asyncreads = MIN(nsp->f_asyncreads, LONG_MAX);
667 strlcpy(osp->f_fstypename, nsp->f_fstypename,
668 MIN(MFSNAMELEN, OMFSNAMELEN));
669 strlcpy(osp->f_mntonname, nsp->f_mntonname,
670 MIN(MNAMELEN, OMNAMELEN));
671 strlcpy(osp->f_mntfromname, nsp->f_mntfromname,
672 MIN(MNAMELEN, OMNAMELEN));
673 osp->f_fsid = nsp->f_fsid;
675 #endif /* COMPAT_FREEBSD4 */
677 #if defined(COMPAT_FREEBSD11)
679 * Get old format filesystem statistics.
681 static void freebsd11_cvtstatfs(struct statfs *, struct freebsd11_statfs *);
684 freebsd11_statfs(struct thread *td, struct freebsd11_statfs_args *uap)
686 struct freebsd11_statfs osb;
690 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
691 error = kern_statfs(td, uap->path, UIO_USERSPACE, sfp);
693 freebsd11_cvtstatfs(sfp, &osb);
694 error = copyout(&osb, uap->buf, sizeof(osb));
701 * Get filesystem statistics.
704 freebsd11_fstatfs(struct thread *td, struct freebsd11_fstatfs_args *uap)
706 struct freebsd11_statfs osb;
710 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
711 error = kern_fstatfs(td, uap->fd, sfp);
713 freebsd11_cvtstatfs(sfp, &osb);
714 error = copyout(&osb, uap->buf, sizeof(osb));
721 * Get statistics on all filesystems.
724 freebsd11_getfsstat(struct thread *td, struct freebsd11_getfsstat_args *uap)
726 struct freebsd11_statfs osb;
727 struct statfs *buf, *sp;
731 count = uap->bufsize / sizeof(struct ostatfs);
732 size = count * sizeof(struct statfs);
733 error = kern_getfsstat(td, &buf, size, &count, UIO_SYSSPACE,
736 td->td_retval[0] = count;
739 while (count > 0 && error == 0) {
740 freebsd11_cvtstatfs(sp, &osb);
741 error = copyout(&osb, uap->buf, sizeof(osb));
752 * Implement fstatfs() for (NFS) file handles.
755 freebsd11_fhstatfs(struct thread *td, struct freebsd11_fhstatfs_args *uap)
757 struct freebsd11_statfs osb;
762 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
765 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
766 error = kern_fhstatfs(td, fh, sfp);
768 freebsd11_cvtstatfs(sfp, &osb);
769 error = copyout(&osb, uap->buf, sizeof(osb));
776 * Convert a new format statfs structure to an old format statfs structure.
779 freebsd11_cvtstatfs(struct statfs *nsp, struct freebsd11_statfs *osp)
782 bzero(osp, sizeof(*osp));
783 osp->f_version = FREEBSD11_STATFS_VERSION;
784 osp->f_type = nsp->f_type;
785 osp->f_flags = nsp->f_flags;
786 osp->f_bsize = nsp->f_bsize;
787 osp->f_iosize = nsp->f_iosize;
788 osp->f_blocks = nsp->f_blocks;
789 osp->f_bfree = nsp->f_bfree;
790 osp->f_bavail = nsp->f_bavail;
791 osp->f_files = nsp->f_files;
792 osp->f_ffree = nsp->f_ffree;
793 osp->f_syncwrites = nsp->f_syncwrites;
794 osp->f_asyncwrites = nsp->f_asyncwrites;
795 osp->f_syncreads = nsp->f_syncreads;
796 osp->f_asyncreads = nsp->f_asyncreads;
797 osp->f_namemax = nsp->f_namemax;
798 osp->f_owner = nsp->f_owner;
799 osp->f_fsid = nsp->f_fsid;
800 strlcpy(osp->f_fstypename, nsp->f_fstypename,
801 MIN(MFSNAMELEN, sizeof(osp->f_fstypename)));
802 strlcpy(osp->f_mntonname, nsp->f_mntonname,
803 MIN(MNAMELEN, sizeof(osp->f_mntonname)));
804 strlcpy(osp->f_mntfromname, nsp->f_mntfromname,
805 MIN(MNAMELEN, sizeof(osp->f_mntfromname)));
807 #endif /* COMPAT_FREEBSD11 */
810 * Change current working directory to a given file descriptor.
812 #ifndef _SYS_SYSPROTO_H_
818 sys_fchdir(struct thread *td, struct fchdir_args *uap)
820 struct vnode *vp, *tdp;
825 AUDIT_ARG_FD(uap->fd);
826 error = getvnode(td, uap->fd, &cap_fchdir_rights,
833 vn_lock(vp, LK_SHARED | LK_RETRY);
834 AUDIT_ARG_VNODE1(vp);
835 error = change_dir(vp, td);
836 while (!error && (mp = vp->v_mountedhere) != NULL) {
839 error = VFS_ROOT(mp, LK_SHARED, &tdp);
856 * Change current working directory (``.'').
858 #ifndef _SYS_SYSPROTO_H_
864 sys_chdir(struct thread *td, struct chdir_args *uap)
867 return (kern_chdir(td, uap->path, UIO_USERSPACE));
871 kern_chdir(struct thread *td, const char *path, enum uio_seg pathseg)
876 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
878 if ((error = namei(&nd)) != 0)
880 if ((error = change_dir(nd.ni_vp, td)) != 0) {
882 NDFREE(&nd, NDF_ONLY_PNBUF);
885 VOP_UNLOCK(nd.ni_vp, 0);
886 NDFREE(&nd, NDF_ONLY_PNBUF);
887 pwd_chdir(td, nd.ni_vp);
892 * Change notion of root (``/'') directory.
894 #ifndef _SYS_SYSPROTO_H_
900 sys_chroot(struct thread *td, struct chroot_args *uap)
905 error = priv_check(td, PRIV_VFS_CHROOT);
908 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
909 UIO_USERSPACE, uap->path, td);
913 error = change_dir(nd.ni_vp, td);
917 error = mac_vnode_check_chroot(td->td_ucred, nd.ni_vp);
921 VOP_UNLOCK(nd.ni_vp, 0);
922 error = pwd_chroot(td, nd.ni_vp);
924 NDFREE(&nd, NDF_ONLY_PNBUF);
929 NDFREE(&nd, NDF_ONLY_PNBUF);
934 * Common routine for chroot and chdir. Callers must provide a locked vnode
938 change_dir(struct vnode *vp, struct thread *td)
944 ASSERT_VOP_LOCKED(vp, "change_dir(): vp not locked");
945 if (vp->v_type != VDIR)
948 error = mac_vnode_check_chdir(td->td_ucred, vp);
952 return (VOP_ACCESS(vp, VEXEC, td->td_ucred, td));
956 flags_to_rights(int flags, cap_rights_t *rightsp)
959 if (flags & O_EXEC) {
960 cap_rights_set(rightsp, CAP_FEXECVE);
962 switch ((flags & O_ACCMODE)) {
964 cap_rights_set(rightsp, CAP_READ);
967 cap_rights_set(rightsp, CAP_READ);
970 cap_rights_set(rightsp, CAP_WRITE);
971 if (!(flags & (O_APPEND | O_TRUNC)))
972 cap_rights_set(rightsp, CAP_SEEK);
978 cap_rights_set(rightsp, CAP_CREATE);
981 cap_rights_set(rightsp, CAP_FTRUNCATE);
983 if (flags & (O_SYNC | O_FSYNC))
984 cap_rights_set(rightsp, CAP_FSYNC);
986 if (flags & (O_EXLOCK | O_SHLOCK))
987 cap_rights_set(rightsp, CAP_FLOCK);
991 * Check permissions, allocate an open file structure, and call the device
992 * open routine if any.
994 #ifndef _SYS_SYSPROTO_H_
1002 sys_open(struct thread *td, struct open_args *uap)
1005 return (kern_openat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1006 uap->flags, uap->mode));
1009 #ifndef _SYS_SYSPROTO_H_
1010 struct openat_args {
1018 sys_openat(struct thread *td, struct openat_args *uap)
1021 AUDIT_ARG_FD(uap->fd);
1022 return (kern_openat(td, uap->fd, uap->path, UIO_USERSPACE, uap->flag,
1027 kern_openat(struct thread *td, int fd, const char *path, enum uio_seg pathseg,
1028 int flags, int mode)
1030 struct proc *p = td->td_proc;
1031 struct filedesc *fdp = p->p_fd;
1034 struct nameidata nd;
1035 cap_rights_t rights;
1036 int cmode, error, indx;
1040 AUDIT_ARG_FFLAGS(flags);
1041 AUDIT_ARG_MODE(mode);
1042 cap_rights_init(&rights, CAP_LOOKUP);
1043 flags_to_rights(flags, &rights);
1045 * Only one of the O_EXEC, O_RDONLY, O_WRONLY and O_RDWR flags
1048 if (flags & O_EXEC) {
1049 if (flags & O_ACCMODE)
1051 } else if ((flags & O_ACCMODE) == O_ACCMODE) {
1054 flags = FFLAGS(flags);
1058 * Allocate a file structure. The descriptor to reference it
1059 * is allocated and set by finstall() below.
1061 error = falloc_noinstall(td, &fp);
1065 * An extra reference on `fp' has been held for us by
1066 * falloc_noinstall().
1068 /* Set the flags early so the finit in devfs can pick them up. */
1069 fp->f_flag = flags & FMASK;
1070 cmode = ((mode & ~fdp->fd_cmask) & ALLPERMS) & ~S_ISTXT;
1071 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, fd,
1073 td->td_dupfd = -1; /* XXX check for fdopen */
1074 error = vn_open(&nd, &flags, cmode, fp);
1077 * If the vn_open replaced the method vector, something
1078 * wonderous happened deep below and we just pass it up
1079 * pretending we know what we do.
1081 if (error == ENXIO && fp->f_ops != &badfileops)
1085 * Handle special fdopen() case. bleh.
1087 * Don't do this for relative (capability) lookups; we don't
1088 * understand exactly what would happen, and we don't think
1089 * that it ever should.
1091 if ((nd.ni_lcf & NI_LCF_STRICTRELATIVE) == 0 &&
1092 (error == ENODEV || error == ENXIO) &&
1093 td->td_dupfd >= 0) {
1094 error = dupfdopen(td, fdp, td->td_dupfd, flags, error,
1103 NDFREE(&nd, NDF_ONLY_PNBUF);
1107 * Store the vnode, for any f_type. Typically, the vnode use
1108 * count is decremented by direct call to vn_closefile() for
1109 * files that switched type in the cdevsw fdopen() method.
1113 * If the file wasn't claimed by devfs bind it to the normal
1114 * vnode operations here.
1116 if (fp->f_ops == &badfileops) {
1117 KASSERT(vp->v_type != VFIFO, ("Unexpected fifo."));
1119 finit(fp, (flags & FMASK) | (fp->f_flag & FHASLOCK),
1120 DTYPE_VNODE, vp, &vnops);
1124 if (flags & O_TRUNC) {
1125 error = fo_truncate(fp, 0, td->td_ucred, td);
1131 * If we haven't already installed the FD (for dupfdopen), do so now.
1134 struct filecaps *fcaps;
1137 if ((nd.ni_lcf & NI_LCF_STRICTRELATIVE) != 0)
1138 fcaps = &nd.ni_filecaps;
1142 error = finstall(td, fp, &indx, flags, fcaps);
1143 /* On success finstall() consumes fcaps. */
1145 filecaps_free(&nd.ni_filecaps);
1149 filecaps_free(&nd.ni_filecaps);
1153 * Release our private reference, leaving the one associated with
1154 * the descriptor table intact.
1157 td->td_retval[0] = indx;
1160 KASSERT(indx == -1, ("indx=%d, should be -1", indx));
1169 #ifndef _SYS_SYSPROTO_H_
1170 struct ocreat_args {
1176 ocreat(struct thread *td, struct ocreat_args *uap)
1179 return (kern_openat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1180 O_WRONLY | O_CREAT | O_TRUNC, uap->mode));
1182 #endif /* COMPAT_43 */
1185 * Create a special file.
1187 #ifndef _SYS_SYSPROTO_H_
1188 struct mknodat_args {
1196 sys_mknodat(struct thread *td, struct mknodat_args *uap)
1199 return (kern_mknodat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode,
1203 #if defined(COMPAT_FREEBSD11)
1205 freebsd11_mknod(struct thread *td,
1206 struct freebsd11_mknod_args *uap)
1209 return (kern_mknodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1210 uap->mode, uap->dev));
1214 freebsd11_mknodat(struct thread *td,
1215 struct freebsd11_mknodat_args *uap)
1218 return (kern_mknodat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode,
1221 #endif /* COMPAT_FREEBSD11 */
1224 kern_mknodat(struct thread *td, int fd, const char *path, enum uio_seg pathseg,
1225 int mode, dev_t dev)
1230 struct nameidata nd;
1231 int error, whiteout = 0;
1233 AUDIT_ARG_MODE(mode);
1235 switch (mode & S_IFMT) {
1238 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
1239 if (error == 0 && dev == VNOVAL)
1243 error = priv_check(td, PRIV_VFS_MKNOD_WHT);
1247 return (kern_mkfifoat(td, fd, path, pathseg, mode));
1257 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
1258 NOCACHE, pathseg, path, fd, &cap_mknodat_rights,
1260 if ((error = namei(&nd)) != 0)
1264 NDFREE(&nd, NDF_ONLY_PNBUF);
1265 if (vp == nd.ni_dvp)
1273 vattr.va_mode = (mode & ALLPERMS) &
1274 ~td->td_proc->p_fd->fd_cmask;
1275 vattr.va_rdev = dev;
1278 switch (mode & S_IFMT) {
1280 vattr.va_type = VCHR;
1283 vattr.va_type = VBLK;
1289 panic("kern_mknod: invalid mode");
1292 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1293 NDFREE(&nd, NDF_ONLY_PNBUF);
1295 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1300 if (error == 0 && !whiteout)
1301 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp,
1302 &nd.ni_cnd, &vattr);
1306 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, CREATE);
1308 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp,
1309 &nd.ni_cnd, &vattr);
1314 NDFREE(&nd, NDF_ONLY_PNBUF);
1316 vn_finished_write(mp);
1321 * Create a named pipe.
1323 #ifndef _SYS_SYSPROTO_H_
1324 struct mkfifo_args {
1330 sys_mkfifo(struct thread *td, struct mkfifo_args *uap)
1333 return (kern_mkfifoat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1337 #ifndef _SYS_SYSPROTO_H_
1338 struct mkfifoat_args {
1345 sys_mkfifoat(struct thread *td, struct mkfifoat_args *uap)
1348 return (kern_mkfifoat(td, uap->fd, uap->path, UIO_USERSPACE,
1353 kern_mkfifoat(struct thread *td, int fd, const char *path,
1354 enum uio_seg pathseg, int mode)
1358 struct nameidata nd;
1361 AUDIT_ARG_MODE(mode);
1364 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
1365 NOCACHE, pathseg, path, fd, &cap_mkfifoat_rights,
1367 if ((error = namei(&nd)) != 0)
1369 if (nd.ni_vp != NULL) {
1370 NDFREE(&nd, NDF_ONLY_PNBUF);
1371 if (nd.ni_vp == nd.ni_dvp)
1378 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1379 NDFREE(&nd, NDF_ONLY_PNBUF);
1381 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1386 vattr.va_type = VFIFO;
1387 vattr.va_mode = (mode & ALLPERMS) & ~td->td_proc->p_fd->fd_cmask;
1389 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1394 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
1401 vn_finished_write(mp);
1402 NDFREE(&nd, NDF_ONLY_PNBUF);
1407 * Make a hard file link.
1409 #ifndef _SYS_SYSPROTO_H_
1416 sys_link(struct thread *td, struct link_args *uap)
1419 return (kern_linkat(td, AT_FDCWD, AT_FDCWD, uap->path, uap->link,
1420 UIO_USERSPACE, FOLLOW));
1423 #ifndef _SYS_SYSPROTO_H_
1424 struct linkat_args {
1433 sys_linkat(struct thread *td, struct linkat_args *uap)
1438 if ((flag & ~(AT_SYMLINK_FOLLOW | AT_BENEATH)) != 0)
1441 return (kern_linkat(td, uap->fd1, uap->fd2, uap->path1, uap->path2,
1442 UIO_USERSPACE, ((flag & AT_SYMLINK_FOLLOW) != 0 ? FOLLOW :
1443 NOFOLLOW) | ((flag & AT_BENEATH) != 0 ? BENEATH : 0)));
1446 int hardlink_check_uid = 0;
1447 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
1448 &hardlink_check_uid, 0,
1449 "Unprivileged processes cannot create hard links to files owned by other "
1451 static int hardlink_check_gid = 0;
1452 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
1453 &hardlink_check_gid, 0,
1454 "Unprivileged processes cannot create hard links to files owned by other "
1458 can_hardlink(struct vnode *vp, struct ucred *cred)
1463 if (!hardlink_check_uid && !hardlink_check_gid)
1466 error = VOP_GETATTR(vp, &va, cred);
1470 if (hardlink_check_uid && cred->cr_uid != va.va_uid) {
1471 error = priv_check_cred(cred, PRIV_VFS_LINK);
1476 if (hardlink_check_gid && !groupmember(va.va_gid, cred)) {
1477 error = priv_check_cred(cred, PRIV_VFS_LINK);
1486 kern_linkat(struct thread *td, int fd1, int fd2, const char *path1,
1487 const char *path2, enum uio_seg segflag, int follow)
1489 struct nameidata nd;
1494 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, segflag,
1495 path1, fd1, &cap_linkat_source_rights, td);
1496 if ((error = namei(&nd)) != 0)
1498 NDFREE(&nd, NDF_ONLY_PNBUF);
1499 error = kern_linkat_vp(td, nd.ni_vp, fd2, path2, segflag);
1500 } while (error == EAGAIN);
1505 kern_linkat_vp(struct thread *td, struct vnode *vp, int fd, const char *path,
1506 enum uio_seg segflag)
1508 struct nameidata nd;
1512 if (vp->v_type == VDIR) {
1514 return (EPERM); /* POSIX */
1516 NDINIT_ATRIGHTS(&nd, CREATE,
1517 LOCKPARENT | SAVENAME | AUDITVNODE2 | NOCACHE, segflag, path, fd,
1518 &cap_linkat_target_rights, td);
1519 if ((error = namei(&nd)) == 0) {
1520 if (nd.ni_vp != NULL) {
1521 NDFREE(&nd, NDF_ONLY_PNBUF);
1522 if (nd.ni_dvp == nd.ni_vp)
1529 } else if (nd.ni_dvp->v_mount != vp->v_mount) {
1531 * Cross-device link. No need to recheck
1532 * vp->v_type, since it cannot change, except
1535 NDFREE(&nd, NDF_ONLY_PNBUF);
1539 } else if ((error = vn_lock(vp, LK_EXCLUSIVE)) == 0) {
1540 error = can_hardlink(vp, td->td_ucred);
1543 error = mac_vnode_check_link(td->td_ucred,
1544 nd.ni_dvp, vp, &nd.ni_cnd);
1549 NDFREE(&nd, NDF_ONLY_PNBUF);
1552 error = vn_start_write(vp, &mp, V_NOWAIT);
1556 NDFREE(&nd, NDF_ONLY_PNBUF);
1557 error = vn_start_write(NULL, &mp,
1563 error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
1566 vn_finished_write(mp);
1567 NDFREE(&nd, NDF_ONLY_PNBUF);
1570 NDFREE(&nd, NDF_ONLY_PNBUF);
1580 * Make a symbolic link.
1582 #ifndef _SYS_SYSPROTO_H_
1583 struct symlink_args {
1589 sys_symlink(struct thread *td, struct symlink_args *uap)
1592 return (kern_symlinkat(td, uap->path, AT_FDCWD, uap->link,
1596 #ifndef _SYS_SYSPROTO_H_
1597 struct symlinkat_args {
1604 sys_symlinkat(struct thread *td, struct symlinkat_args *uap)
1607 return (kern_symlinkat(td, uap->path1, uap->fd, uap->path2,
1612 kern_symlinkat(struct thread *td, const char *path1, int fd, const char *path2,
1613 enum uio_seg segflg)
1617 const char *syspath;
1619 struct nameidata nd;
1622 if (segflg == UIO_SYSSPACE) {
1625 tmppath = uma_zalloc(namei_zone, M_WAITOK);
1626 if ((error = copyinstr(path1, tmppath, MAXPATHLEN, NULL)) != 0)
1630 AUDIT_ARG_TEXT(syspath);
1633 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
1634 NOCACHE, segflg, path2, fd, &cap_symlinkat_rights,
1636 if ((error = namei(&nd)) != 0)
1639 NDFREE(&nd, NDF_ONLY_PNBUF);
1640 if (nd.ni_vp == nd.ni_dvp)
1648 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1649 NDFREE(&nd, NDF_ONLY_PNBUF);
1651 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1656 vattr.va_mode = ACCESSPERMS &~ td->td_proc->p_fd->fd_cmask;
1658 vattr.va_type = VLNK;
1659 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1664 error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr, syspath);
1670 NDFREE(&nd, NDF_ONLY_PNBUF);
1672 vn_finished_write(mp);
1674 if (segflg != UIO_SYSSPACE)
1675 uma_zfree(namei_zone, tmppath);
1680 * Delete a whiteout from the filesystem.
1682 #ifndef _SYS_SYSPROTO_H_
1683 struct undelete_args {
1688 sys_undelete(struct thread *td, struct undelete_args *uap)
1691 struct nameidata nd;
1696 NDINIT(&nd, DELETE, LOCKPARENT | DOWHITEOUT | AUDITVNODE1,
1697 UIO_USERSPACE, uap->path, td);
1702 if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & ISWHITEOUT)) {
1703 NDFREE(&nd, NDF_ONLY_PNBUF);
1704 if (nd.ni_vp == nd.ni_dvp)
1712 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1713 NDFREE(&nd, NDF_ONLY_PNBUF);
1715 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1719 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, DELETE);
1720 NDFREE(&nd, NDF_ONLY_PNBUF);
1722 vn_finished_write(mp);
1727 * Delete a name from the filesystem.
1729 #ifndef _SYS_SYSPROTO_H_
1730 struct unlink_args {
1735 sys_unlink(struct thread *td, struct unlink_args *uap)
1738 return (kern_funlinkat(td, AT_FDCWD, uap->path, FD_NONE, UIO_USERSPACE,
1743 kern_funlinkat_ex(struct thread *td, int dfd, const char *path, int fd,
1744 int flag, enum uio_seg pathseg, ino_t oldinum)
1747 if ((flag & ~AT_REMOVEDIR) != 0)
1750 if ((flag & AT_REMOVEDIR) != 0)
1751 return (kern_frmdirat(td, dfd, path, fd, UIO_USERSPACE, 0));
1753 return (kern_funlinkat(td, dfd, path, fd, UIO_USERSPACE, 0, 0));
1756 #ifndef _SYS_SYSPROTO_H_
1757 struct unlinkat_args {
1764 sys_unlinkat(struct thread *td, struct unlinkat_args *uap)
1767 return (kern_funlinkat_ex(td, uap->fd, uap->path, FD_NONE, uap->flag,
1771 #ifndef _SYS_SYSPROTO_H_
1772 struct funlinkat_args {
1780 sys_funlinkat(struct thread *td, struct funlinkat_args *uap)
1783 return (kern_funlinkat_ex(td, uap->dfd, uap->path, uap->fd, uap->flag,
1788 kern_funlinkat(struct thread *td, int dfd, const char *path, int fd,
1789 enum uio_seg pathseg, int flag, ino_t oldinum)
1794 struct nameidata nd;
1799 if (fd != FD_NONE) {
1800 error = getvnode(td, fd, &cap_no_rights, &fp);
1807 NDINIT_ATRIGHTS(&nd, DELETE, LOCKPARENT | LOCKLEAF | AUDITVNODE1 |
1808 ((flag & AT_BENEATH) != 0 ? BENEATH : 0),
1809 pathseg, path, dfd, &cap_unlinkat_rights, td);
1810 if ((error = namei(&nd)) != 0) {
1811 if (error == EINVAL)
1816 if (vp->v_type == VDIR && oldinum == 0) {
1817 error = EPERM; /* POSIX */
1818 } else if (oldinum != 0 &&
1819 ((error = vn_stat(vp, &sb, td->td_ucred, NOCRED, td)) == 0) &&
1820 sb.st_ino != oldinum) {
1821 error = EIDRM; /* Identifier removed */
1822 } else if (fp != NULL && fp->f_vnode != vp) {
1823 if ((fp->f_vnode->v_iflag & VI_DOOMED) != 0)
1829 * The root of a mounted filesystem cannot be deleted.
1831 * XXX: can this only be a VDIR case?
1833 if (vp->v_vflag & VV_ROOT)
1837 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1838 NDFREE(&nd, NDF_ONLY_PNBUF);
1840 if (vp == nd.ni_dvp)
1844 if ((error = vn_start_write(NULL, &mp,
1845 V_XSLEEP | PCATCH)) != 0) {
1851 error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp,
1856 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_UNLINK);
1857 error = VOP_REMOVE(nd.ni_dvp, vp, &nd.ni_cnd);
1861 vn_finished_write(mp);
1863 NDFREE(&nd, NDF_ONLY_PNBUF);
1865 if (vp == nd.ni_dvp)
1876 * Reposition read/write file offset.
1878 #ifndef _SYS_SYSPROTO_H_
1887 sys_lseek(struct thread *td, struct lseek_args *uap)
1890 return (kern_lseek(td, uap->fd, uap->offset, uap->whence));
1894 kern_lseek(struct thread *td, int fd, off_t offset, int whence)
1900 error = fget(td, fd, &cap_seek_rights, &fp);
1903 error = (fp->f_ops->fo_flags & DFLAG_SEEKABLE) != 0 ?
1904 fo_seek(fp, offset, whence, td) : ESPIPE;
1909 #if defined(COMPAT_43)
1911 * Reposition read/write file offset.
1913 #ifndef _SYS_SYSPROTO_H_
1914 struct olseek_args {
1921 olseek(struct thread *td, struct olseek_args *uap)
1924 return (kern_lseek(td, uap->fd, uap->offset, uap->whence));
1926 #endif /* COMPAT_43 */
1928 #if defined(COMPAT_FREEBSD6)
1929 /* Version with the 'pad' argument */
1931 freebsd6_lseek(struct thread *td, struct freebsd6_lseek_args *uap)
1934 return (kern_lseek(td, uap->fd, uap->offset, uap->whence));
1939 * Check access permissions using passed credentials.
1942 vn_access(struct vnode *vp, int user_flags, struct ucred *cred,
1948 /* Flags == 0 means only check for existence. */
1949 if (user_flags == 0)
1953 if (user_flags & R_OK)
1955 if (user_flags & W_OK)
1957 if (user_flags & X_OK)
1960 error = mac_vnode_check_access(cred, vp, accmode);
1964 if ((accmode & VWRITE) == 0 || (error = vn_writechk(vp)) == 0)
1965 error = VOP_ACCESS(vp, accmode, cred, td);
1970 * Check access permissions using "real" credentials.
1972 #ifndef _SYS_SYSPROTO_H_
1973 struct access_args {
1979 sys_access(struct thread *td, struct access_args *uap)
1982 return (kern_accessat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1986 #ifndef _SYS_SYSPROTO_H_
1987 struct faccessat_args {
1995 sys_faccessat(struct thread *td, struct faccessat_args *uap)
1998 return (kern_accessat(td, uap->fd, uap->path, UIO_USERSPACE, uap->flag,
2003 kern_accessat(struct thread *td, int fd, const char *path,
2004 enum uio_seg pathseg, int flag, int amode)
2006 struct ucred *cred, *usecred;
2008 struct nameidata nd;
2011 if ((flag & ~(AT_EACCESS | AT_BENEATH)) != 0)
2013 if (amode != F_OK && (amode & ~(R_OK | W_OK | X_OK)) != 0)
2017 * Create and modify a temporary credential instead of one that
2018 * is potentially shared (if we need one).
2020 cred = td->td_ucred;
2021 if ((flag & AT_EACCESS) == 0 &&
2022 ((cred->cr_uid != cred->cr_ruid ||
2023 cred->cr_rgid != cred->cr_groups[0]))) {
2024 usecred = crdup(cred);
2025 usecred->cr_uid = cred->cr_ruid;
2026 usecred->cr_groups[0] = cred->cr_rgid;
2027 td->td_ucred = usecred;
2030 AUDIT_ARG_VALUE(amode);
2031 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF |
2032 AUDITVNODE1 | ((flag & AT_BENEATH) != 0 ? BENEATH : 0),
2033 pathseg, path, fd, &cap_fstat_rights, td);
2034 if ((error = namei(&nd)) != 0)
2038 error = vn_access(vp, amode, usecred, td);
2039 NDFREE(&nd, NDF_ONLY_PNBUF);
2042 if (usecred != cred) {
2043 td->td_ucred = cred;
2050 * Check access permissions using "effective" credentials.
2052 #ifndef _SYS_SYSPROTO_H_
2053 struct eaccess_args {
2059 sys_eaccess(struct thread *td, struct eaccess_args *uap)
2062 return (kern_accessat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2063 AT_EACCESS, uap->amode));
2066 #if defined(COMPAT_43)
2068 * Get file status; this version follows links.
2070 #ifndef _SYS_SYSPROTO_H_
2077 ostat(struct thread *td, struct ostat_args *uap)
2083 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
2088 return (copyout(&osb, uap->ub, sizeof (osb)));
2092 * Get file status; this version does not follow links.
2094 #ifndef _SYS_SYSPROTO_H_
2095 struct olstat_args {
2101 olstat(struct thread *td, struct olstat_args *uap)
2107 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2108 UIO_USERSPACE, &sb, NULL);
2112 return (copyout(&osb, uap->ub, sizeof (osb)));
2116 * Convert from an old to a new stat structure.
2117 * XXX: many values are blindly truncated.
2120 cvtstat(struct stat *st, struct ostat *ost)
2123 bzero(ost, sizeof(*ost));
2124 ost->st_dev = st->st_dev;
2125 ost->st_ino = st->st_ino;
2126 ost->st_mode = st->st_mode;
2127 ost->st_nlink = st->st_nlink;
2128 ost->st_uid = st->st_uid;
2129 ost->st_gid = st->st_gid;
2130 ost->st_rdev = st->st_rdev;
2131 ost->st_size = MIN(st->st_size, INT32_MAX);
2132 ost->st_atim = st->st_atim;
2133 ost->st_mtim = st->st_mtim;
2134 ost->st_ctim = st->st_ctim;
2135 ost->st_blksize = st->st_blksize;
2136 ost->st_blocks = st->st_blocks;
2137 ost->st_flags = st->st_flags;
2138 ost->st_gen = st->st_gen;
2140 #endif /* COMPAT_43 */
2142 #if defined(COMPAT_43) || defined(COMPAT_FREEBSD11)
2143 int ino64_trunc_error;
2144 SYSCTL_INT(_vfs, OID_AUTO, ino64_trunc_error, CTLFLAG_RW,
2145 &ino64_trunc_error, 0,
2146 "Error on truncation of device, file or inode number, or link count");
2149 freebsd11_cvtstat(struct stat *st, struct freebsd11_stat *ost)
2152 ost->st_dev = st->st_dev;
2153 if (ost->st_dev != st->st_dev) {
2154 switch (ino64_trunc_error) {
2157 * Since dev_t is almost raw, don't clamp to the
2158 * maximum for case 2, but ignore the error.
2165 ost->st_ino = st->st_ino;
2166 if (ost->st_ino != st->st_ino) {
2167 switch (ino64_trunc_error) {
2174 ost->st_ino = UINT32_MAX;
2178 ost->st_mode = st->st_mode;
2179 ost->st_nlink = st->st_nlink;
2180 if (ost->st_nlink != st->st_nlink) {
2181 switch (ino64_trunc_error) {
2188 ost->st_nlink = UINT16_MAX;
2192 ost->st_uid = st->st_uid;
2193 ost->st_gid = st->st_gid;
2194 ost->st_rdev = st->st_rdev;
2195 if (ost->st_rdev != st->st_rdev) {
2196 switch (ino64_trunc_error) {
2203 ost->st_atim = st->st_atim;
2204 ost->st_mtim = st->st_mtim;
2205 ost->st_ctim = st->st_ctim;
2206 ost->st_size = st->st_size;
2207 ost->st_blocks = st->st_blocks;
2208 ost->st_blksize = st->st_blksize;
2209 ost->st_flags = st->st_flags;
2210 ost->st_gen = st->st_gen;
2212 ost->st_birthtim = st->st_birthtim;
2213 bzero((char *)&ost->st_birthtim + sizeof(ost->st_birthtim),
2214 sizeof(*ost) - offsetof(struct freebsd11_stat,
2215 st_birthtim) - sizeof(ost->st_birthtim));
2220 freebsd11_stat(struct thread *td, struct freebsd11_stat_args* uap)
2223 struct freebsd11_stat osb;
2226 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
2230 error = freebsd11_cvtstat(&sb, &osb);
2232 error = copyout(&osb, uap->ub, sizeof(osb));
2237 freebsd11_lstat(struct thread *td, struct freebsd11_lstat_args* uap)
2240 struct freebsd11_stat osb;
2243 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2244 UIO_USERSPACE, &sb, NULL);
2247 error = freebsd11_cvtstat(&sb, &osb);
2249 error = copyout(&osb, uap->ub, sizeof(osb));
2254 freebsd11_fhstat(struct thread *td, struct freebsd11_fhstat_args* uap)
2258 struct freebsd11_stat osb;
2261 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
2264 error = kern_fhstat(td, fh, &sb);
2267 error = freebsd11_cvtstat(&sb, &osb);
2269 error = copyout(&osb, uap->sb, sizeof(osb));
2274 freebsd11_fstatat(struct thread *td, struct freebsd11_fstatat_args* uap)
2277 struct freebsd11_stat osb;
2280 error = kern_statat(td, uap->flag, uap->fd, uap->path,
2281 UIO_USERSPACE, &sb, NULL);
2284 error = freebsd11_cvtstat(&sb, &osb);
2286 error = copyout(&osb, uap->buf, sizeof(osb));
2289 #endif /* COMPAT_FREEBSD11 */
2294 #ifndef _SYS_SYSPROTO_H_
2295 struct fstatat_args {
2303 sys_fstatat(struct thread *td, struct fstatat_args *uap)
2308 error = kern_statat(td, uap->flag, uap->fd, uap->path,
2309 UIO_USERSPACE, &sb, NULL);
2311 error = copyout(&sb, uap->buf, sizeof (sb));
2316 kern_statat(struct thread *td, int flag, int fd, const char *path,
2317 enum uio_seg pathseg, struct stat *sbp,
2318 void (*hook)(struct vnode *vp, struct stat *sbp))
2320 struct nameidata nd;
2323 if ((flag & ~(AT_SYMLINK_NOFOLLOW | AT_BENEATH)) != 0)
2326 NDINIT_ATRIGHTS(&nd, LOOKUP, ((flag & AT_SYMLINK_NOFOLLOW) != 0 ?
2327 NOFOLLOW : FOLLOW) | ((flag & AT_BENEATH) != 0 ? BENEATH : 0) |
2328 LOCKSHARED | LOCKLEAF | AUDITVNODE1, pathseg, path, fd,
2329 &cap_fstat_rights, td);
2331 if ((error = namei(&nd)) != 0)
2333 error = vn_stat(nd.ni_vp, sbp, td->td_ucred, NOCRED, td);
2335 SDT_PROBE2(vfs, , stat, mode, path, sbp->st_mode);
2336 if (S_ISREG(sbp->st_mode))
2337 SDT_PROBE2(vfs, , stat, reg, path, pathseg);
2338 if (__predict_false(hook != NULL))
2339 hook(nd.ni_vp, sbp);
2341 NDFREE(&nd, NDF_ONLY_PNBUF);
2345 #ifdef __STAT_TIME_T_EXT
2346 sbp->st_atim_ext = 0;
2347 sbp->st_mtim_ext = 0;
2348 sbp->st_ctim_ext = 0;
2349 sbp->st_btim_ext = 0;
2352 if (KTRPOINT(td, KTR_STRUCT))
2358 #if defined(COMPAT_FREEBSD11)
2360 * Implementation of the NetBSD [l]stat() functions.
2363 freebsd11_cvtnstat(struct stat *sb, struct nstat *nsb)
2366 bzero(nsb, sizeof(*nsb));
2367 nsb->st_dev = sb->st_dev;
2368 nsb->st_ino = sb->st_ino;
2369 nsb->st_mode = sb->st_mode;
2370 nsb->st_nlink = sb->st_nlink;
2371 nsb->st_uid = sb->st_uid;
2372 nsb->st_gid = sb->st_gid;
2373 nsb->st_rdev = sb->st_rdev;
2374 nsb->st_atim = sb->st_atim;
2375 nsb->st_mtim = sb->st_mtim;
2376 nsb->st_ctim = sb->st_ctim;
2377 nsb->st_size = sb->st_size;
2378 nsb->st_blocks = sb->st_blocks;
2379 nsb->st_blksize = sb->st_blksize;
2380 nsb->st_flags = sb->st_flags;
2381 nsb->st_gen = sb->st_gen;
2382 nsb->st_birthtim = sb->st_birthtim;
2385 #ifndef _SYS_SYSPROTO_H_
2386 struct freebsd11_nstat_args {
2392 freebsd11_nstat(struct thread *td, struct freebsd11_nstat_args *uap)
2398 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
2402 freebsd11_cvtnstat(&sb, &nsb);
2403 return (copyout(&nsb, uap->ub, sizeof (nsb)));
2407 * NetBSD lstat. Get file status; this version does not follow links.
2409 #ifndef _SYS_SYSPROTO_H_
2410 struct freebsd11_nlstat_args {
2416 freebsd11_nlstat(struct thread *td, struct freebsd11_nlstat_args *uap)
2422 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2423 UIO_USERSPACE, &sb, NULL);
2426 freebsd11_cvtnstat(&sb, &nsb);
2427 return (copyout(&nsb, uap->ub, sizeof (nsb)));
2429 #endif /* COMPAT_FREEBSD11 */
2432 * Get configurable pathname variables.
2434 #ifndef _SYS_SYSPROTO_H_
2435 struct pathconf_args {
2441 sys_pathconf(struct thread *td, struct pathconf_args *uap)
2446 error = kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name, FOLLOW,
2449 td->td_retval[0] = value;
2453 #ifndef _SYS_SYSPROTO_H_
2454 struct lpathconf_args {
2460 sys_lpathconf(struct thread *td, struct lpathconf_args *uap)
2465 error = kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name,
2468 td->td_retval[0] = value;
2473 kern_pathconf(struct thread *td, const char *path, enum uio_seg pathseg,
2474 int name, u_long flags, long *valuep)
2476 struct nameidata nd;
2479 NDINIT(&nd, LOOKUP, LOCKSHARED | LOCKLEAF | AUDITVNODE1 | flags,
2481 if ((error = namei(&nd)) != 0)
2483 NDFREE(&nd, NDF_ONLY_PNBUF);
2485 error = VOP_PATHCONF(nd.ni_vp, name, valuep);
2491 * Return target name of a symbolic link.
2493 #ifndef _SYS_SYSPROTO_H_
2494 struct readlink_args {
2501 sys_readlink(struct thread *td, struct readlink_args *uap)
2504 return (kern_readlinkat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2505 uap->buf, UIO_USERSPACE, uap->count));
2507 #ifndef _SYS_SYSPROTO_H_
2508 struct readlinkat_args {
2516 sys_readlinkat(struct thread *td, struct readlinkat_args *uap)
2519 return (kern_readlinkat(td, uap->fd, uap->path, UIO_USERSPACE,
2520 uap->buf, UIO_USERSPACE, uap->bufsize));
2524 kern_readlinkat(struct thread *td, int fd, const char *path,
2525 enum uio_seg pathseg, char *buf, enum uio_seg bufseg, size_t count)
2528 struct nameidata nd;
2531 if (count > IOSIZE_MAX)
2534 NDINIT_AT(&nd, LOOKUP, NOFOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
2535 pathseg, path, fd, td);
2537 if ((error = namei(&nd)) != 0)
2539 NDFREE(&nd, NDF_ONLY_PNBUF);
2542 error = kern_readlink_vp(vp, buf, bufseg, count, td);
2549 * Helper function to readlink from a vnode
2552 kern_readlink_vp(struct vnode *vp, char *buf, enum uio_seg bufseg, size_t count,
2559 ASSERT_VOP_LOCKED(vp, "kern_readlink_vp(): vp not locked");
2561 error = mac_vnode_check_readlink(td->td_ucred, vp);
2565 if (vp->v_type != VLNK && (vp->v_vflag & VV_READLINK) == 0)
2568 aiov.iov_base = buf;
2569 aiov.iov_len = count;
2570 auio.uio_iov = &aiov;
2571 auio.uio_iovcnt = 1;
2572 auio.uio_offset = 0;
2573 auio.uio_rw = UIO_READ;
2574 auio.uio_segflg = bufseg;
2576 auio.uio_resid = count;
2577 error = VOP_READLINK(vp, &auio, td->td_ucred);
2578 td->td_retval[0] = count - auio.uio_resid;
2583 * Common implementation code for chflags() and fchflags().
2586 setfflags(struct thread *td, struct vnode *vp, u_long flags)
2592 /* We can't support the value matching VNOVAL. */
2593 if (flags == VNOVAL)
2594 return (EOPNOTSUPP);
2597 * Prevent non-root users from setting flags on devices. When
2598 * a device is reused, users can retain ownership of the device
2599 * if they are allowed to set flags and programs assume that
2600 * chown can't fail when done as root.
2602 if (vp->v_type == VCHR || vp->v_type == VBLK) {
2603 error = priv_check(td, PRIV_VFS_CHFLAGS_DEV);
2608 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2611 vattr.va_flags = flags;
2612 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2614 error = mac_vnode_check_setflags(td->td_ucred, vp, vattr.va_flags);
2617 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
2619 vn_finished_write(mp);
2624 * Change flags of a file given a path name.
2626 #ifndef _SYS_SYSPROTO_H_
2627 struct chflags_args {
2633 sys_chflags(struct thread *td, struct chflags_args *uap)
2636 return (kern_chflagsat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2640 #ifndef _SYS_SYSPROTO_H_
2641 struct chflagsat_args {
2649 sys_chflagsat(struct thread *td, struct chflagsat_args *uap)
2652 if ((uap->atflag & ~(AT_SYMLINK_NOFOLLOW | AT_BENEATH)) != 0)
2655 return (kern_chflagsat(td, uap->fd, uap->path, UIO_USERSPACE,
2656 uap->flags, uap->atflag));
2660 * Same as chflags() but doesn't follow symlinks.
2662 #ifndef _SYS_SYSPROTO_H_
2663 struct lchflags_args {
2669 sys_lchflags(struct thread *td, struct lchflags_args *uap)
2672 return (kern_chflagsat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2673 uap->flags, AT_SYMLINK_NOFOLLOW));
2677 kern_chflagsat(struct thread *td, int fd, const char *path,
2678 enum uio_seg pathseg, u_long flags, int atflag)
2680 struct nameidata nd;
2683 AUDIT_ARG_FFLAGS(flags);
2684 follow = (atflag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
2685 follow |= (atflag & AT_BENEATH) != 0 ? BENEATH : 0;
2686 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, pathseg, path, fd,
2687 &cap_fchflags_rights, td);
2688 if ((error = namei(&nd)) != 0)
2690 NDFREE(&nd, NDF_ONLY_PNBUF);
2691 error = setfflags(td, nd.ni_vp, flags);
2697 * Change flags of a file given a file descriptor.
2699 #ifndef _SYS_SYSPROTO_H_
2700 struct fchflags_args {
2706 sys_fchflags(struct thread *td, struct fchflags_args *uap)
2711 AUDIT_ARG_FD(uap->fd);
2712 AUDIT_ARG_FFLAGS(uap->flags);
2713 error = getvnode(td, uap->fd, &cap_fchflags_rights,
2718 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
2719 AUDIT_ARG_VNODE1(fp->f_vnode);
2720 VOP_UNLOCK(fp->f_vnode, 0);
2722 error = setfflags(td, fp->f_vnode, uap->flags);
2728 * Common implementation code for chmod(), lchmod() and fchmod().
2731 setfmode(struct thread *td, struct ucred *cred, struct vnode *vp, int mode)
2737 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2739 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2741 vattr.va_mode = mode & ALLPERMS;
2743 error = mac_vnode_check_setmode(cred, vp, vattr.va_mode);
2746 error = VOP_SETATTR(vp, &vattr, cred);
2748 vn_finished_write(mp);
2753 * Change mode of a file given path name.
2755 #ifndef _SYS_SYSPROTO_H_
2762 sys_chmod(struct thread *td, struct chmod_args *uap)
2765 return (kern_fchmodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2769 #ifndef _SYS_SYSPROTO_H_
2770 struct fchmodat_args {
2778 sys_fchmodat(struct thread *td, struct fchmodat_args *uap)
2781 if ((uap->flag & ~(AT_SYMLINK_NOFOLLOW | AT_BENEATH)) != 0)
2784 return (kern_fchmodat(td, uap->fd, uap->path, UIO_USERSPACE,
2785 uap->mode, uap->flag));
2789 * Change mode of a file given path name (don't follow links.)
2791 #ifndef _SYS_SYSPROTO_H_
2792 struct lchmod_args {
2798 sys_lchmod(struct thread *td, struct lchmod_args *uap)
2801 return (kern_fchmodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2802 uap->mode, AT_SYMLINK_NOFOLLOW));
2806 kern_fchmodat(struct thread *td, int fd, const char *path,
2807 enum uio_seg pathseg, mode_t mode, int flag)
2809 struct nameidata nd;
2812 AUDIT_ARG_MODE(mode);
2813 follow = (flag & AT_SYMLINK_NOFOLLOW) != 0 ? NOFOLLOW : FOLLOW;
2814 follow |= (flag & AT_BENEATH) != 0 ? BENEATH : 0;
2815 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, pathseg, path, fd,
2816 &cap_fchmod_rights, td);
2817 if ((error = namei(&nd)) != 0)
2819 NDFREE(&nd, NDF_ONLY_PNBUF);
2820 error = setfmode(td, td->td_ucred, nd.ni_vp, mode);
2826 * Change mode of a file given a file descriptor.
2828 #ifndef _SYS_SYSPROTO_H_
2829 struct fchmod_args {
2835 sys_fchmod(struct thread *td, struct fchmod_args *uap)
2840 AUDIT_ARG_FD(uap->fd);
2841 AUDIT_ARG_MODE(uap->mode);
2843 error = fget(td, uap->fd, &cap_fchmod_rights, &fp);
2846 error = fo_chmod(fp, uap->mode, td->td_ucred, td);
2852 * Common implementation for chown(), lchown(), and fchown()
2855 setfown(struct thread *td, struct ucred *cred, struct vnode *vp, uid_t uid,
2862 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2864 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2869 error = mac_vnode_check_setowner(cred, vp, vattr.va_uid,
2873 error = VOP_SETATTR(vp, &vattr, cred);
2875 vn_finished_write(mp);
2880 * Set ownership given a path name.
2882 #ifndef _SYS_SYSPROTO_H_
2890 sys_chown(struct thread *td, struct chown_args *uap)
2893 return (kern_fchownat(td, AT_FDCWD, uap->path, UIO_USERSPACE, uap->uid,
2897 #ifndef _SYS_SYSPROTO_H_
2898 struct fchownat_args {
2907 sys_fchownat(struct thread *td, struct fchownat_args *uap)
2910 if ((uap->flag & ~(AT_SYMLINK_NOFOLLOW | AT_BENEATH)) != 0)
2913 return (kern_fchownat(td, uap->fd, uap->path, UIO_USERSPACE, uap->uid,
2914 uap->gid, uap->flag));
2918 kern_fchownat(struct thread *td, int fd, const char *path,
2919 enum uio_seg pathseg, int uid, int gid, int flag)
2921 struct nameidata nd;
2924 AUDIT_ARG_OWNER(uid, gid);
2925 follow = (flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
2926 follow |= (flag & AT_BENEATH) != 0 ? BENEATH : 0;
2927 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, pathseg, path, fd,
2928 &cap_fchown_rights, td);
2930 if ((error = namei(&nd)) != 0)
2932 NDFREE(&nd, NDF_ONLY_PNBUF);
2933 error = setfown(td, td->td_ucred, nd.ni_vp, uid, gid);
2939 * Set ownership given a path name, do not cross symlinks.
2941 #ifndef _SYS_SYSPROTO_H_
2942 struct lchown_args {
2949 sys_lchown(struct thread *td, struct lchown_args *uap)
2952 return (kern_fchownat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2953 uap->uid, uap->gid, AT_SYMLINK_NOFOLLOW));
2957 * Set ownership given a file descriptor.
2959 #ifndef _SYS_SYSPROTO_H_
2960 struct fchown_args {
2967 sys_fchown(struct thread *td, struct fchown_args *uap)
2972 AUDIT_ARG_FD(uap->fd);
2973 AUDIT_ARG_OWNER(uap->uid, uap->gid);
2974 error = fget(td, uap->fd, &cap_fchown_rights, &fp);
2977 error = fo_chown(fp, uap->uid, uap->gid, td->td_ucred, td);
2983 * Common implementation code for utimes(), lutimes(), and futimes().
2986 getutimes(const struct timeval *usrtvp, enum uio_seg tvpseg,
2987 struct timespec *tsp)
2989 struct timeval tv[2];
2990 const struct timeval *tvp;
2993 if (usrtvp == NULL) {
2994 vfs_timestamp(&tsp[0]);
2997 if (tvpseg == UIO_SYSSPACE) {
3000 if ((error = copyin(usrtvp, tv, sizeof(tv))) != 0)
3005 if (tvp[0].tv_usec < 0 || tvp[0].tv_usec >= 1000000 ||
3006 tvp[1].tv_usec < 0 || tvp[1].tv_usec >= 1000000)
3008 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
3009 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
3015 * Common implementation code for futimens(), utimensat().
3017 #define UTIMENS_NULL 0x1
3018 #define UTIMENS_EXIT 0x2
3020 getutimens(const struct timespec *usrtsp, enum uio_seg tspseg,
3021 struct timespec *tsp, int *retflags)
3023 struct timespec tsnow;
3026 vfs_timestamp(&tsnow);
3028 if (usrtsp == NULL) {
3031 *retflags |= UTIMENS_NULL;
3034 if (tspseg == UIO_SYSSPACE) {
3037 } else if ((error = copyin(usrtsp, tsp, sizeof(*tsp) * 2)) != 0)
3039 if (tsp[0].tv_nsec == UTIME_OMIT && tsp[1].tv_nsec == UTIME_OMIT)
3040 *retflags |= UTIMENS_EXIT;
3041 if (tsp[0].tv_nsec == UTIME_NOW && tsp[1].tv_nsec == UTIME_NOW)
3042 *retflags |= UTIMENS_NULL;
3043 if (tsp[0].tv_nsec == UTIME_OMIT)
3044 tsp[0].tv_sec = VNOVAL;
3045 else if (tsp[0].tv_nsec == UTIME_NOW)
3047 else if (tsp[0].tv_nsec < 0 || tsp[0].tv_nsec >= 1000000000L)
3049 if (tsp[1].tv_nsec == UTIME_OMIT)
3050 tsp[1].tv_sec = VNOVAL;
3051 else if (tsp[1].tv_nsec == UTIME_NOW)
3053 else if (tsp[1].tv_nsec < 0 || tsp[1].tv_nsec >= 1000000000L)
3060 * Common implementation code for utimes(), lutimes(), futimes(), futimens(),
3064 setutimes(struct thread *td, struct vnode *vp, const struct timespec *ts,
3065 int numtimes, int nullflag)
3069 int error, setbirthtime;
3071 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
3073 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3075 if (numtimes < 3 && !VOP_GETATTR(vp, &vattr, td->td_ucred) &&
3076 timespeccmp(&ts[1], &vattr.va_birthtime, < ))
3079 vattr.va_atime = ts[0];
3080 vattr.va_mtime = ts[1];
3082 vattr.va_birthtime = ts[1];
3084 vattr.va_birthtime = ts[2];
3086 vattr.va_vaflags |= VA_UTIMES_NULL;
3088 error = mac_vnode_check_setutimes(td->td_ucred, vp, vattr.va_atime,
3092 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3094 vn_finished_write(mp);
3099 * Set the access and modification times of a file.
3101 #ifndef _SYS_SYSPROTO_H_
3102 struct utimes_args {
3104 struct timeval *tptr;
3108 sys_utimes(struct thread *td, struct utimes_args *uap)
3111 return (kern_utimesat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
3112 uap->tptr, UIO_USERSPACE));
3115 #ifndef _SYS_SYSPROTO_H_
3116 struct futimesat_args {
3119 const struct timeval * times;
3123 sys_futimesat(struct thread *td, struct futimesat_args *uap)
3126 return (kern_utimesat(td, uap->fd, uap->path, UIO_USERSPACE,
3127 uap->times, UIO_USERSPACE));
3131 kern_utimesat(struct thread *td, int fd, const char *path,
3132 enum uio_seg pathseg, struct timeval *tptr, enum uio_seg tptrseg)
3134 struct nameidata nd;
3135 struct timespec ts[2];
3138 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3140 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, fd,
3141 &cap_futimes_rights, td);
3143 if ((error = namei(&nd)) != 0)
3145 NDFREE(&nd, NDF_ONLY_PNBUF);
3146 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3152 * Set the access and modification times of a file.
3154 #ifndef _SYS_SYSPROTO_H_
3155 struct lutimes_args {
3157 struct timeval *tptr;
3161 sys_lutimes(struct thread *td, struct lutimes_args *uap)
3164 return (kern_lutimes(td, uap->path, UIO_USERSPACE, uap->tptr,
3169 kern_lutimes(struct thread *td, const char *path, enum uio_seg pathseg,
3170 struct timeval *tptr, enum uio_seg tptrseg)
3172 struct timespec ts[2];
3173 struct nameidata nd;
3176 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3178 NDINIT(&nd, LOOKUP, NOFOLLOW | AUDITVNODE1, pathseg, path, td);
3179 if ((error = namei(&nd)) != 0)
3181 NDFREE(&nd, NDF_ONLY_PNBUF);
3182 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3188 * Set the access and modification times of a file.
3190 #ifndef _SYS_SYSPROTO_H_
3191 struct futimes_args {
3193 struct timeval *tptr;
3197 sys_futimes(struct thread *td, struct futimes_args *uap)
3200 return (kern_futimes(td, uap->fd, uap->tptr, UIO_USERSPACE));
3204 kern_futimes(struct thread *td, int fd, struct timeval *tptr,
3205 enum uio_seg tptrseg)
3207 struct timespec ts[2];
3212 error = getutimes(tptr, tptrseg, ts);
3215 error = getvnode(td, fd, &cap_futimes_rights, &fp);
3219 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
3220 AUDIT_ARG_VNODE1(fp->f_vnode);
3221 VOP_UNLOCK(fp->f_vnode, 0);
3223 error = setutimes(td, fp->f_vnode, ts, 2, tptr == NULL);
3229 sys_futimens(struct thread *td, struct futimens_args *uap)
3232 return (kern_futimens(td, uap->fd, uap->times, UIO_USERSPACE));
3236 kern_futimens(struct thread *td, int fd, struct timespec *tptr,
3237 enum uio_seg tptrseg)
3239 struct timespec ts[2];
3244 error = getutimens(tptr, tptrseg, ts, &flags);
3247 if (flags & UTIMENS_EXIT)
3249 error = getvnode(td, fd, &cap_futimes_rights, &fp);
3253 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
3254 AUDIT_ARG_VNODE1(fp->f_vnode);
3255 VOP_UNLOCK(fp->f_vnode, 0);
3257 error = setutimes(td, fp->f_vnode, ts, 2, flags & UTIMENS_NULL);
3263 sys_utimensat(struct thread *td, struct utimensat_args *uap)
3266 return (kern_utimensat(td, uap->fd, uap->path, UIO_USERSPACE,
3267 uap->times, UIO_USERSPACE, uap->flag));
3271 kern_utimensat(struct thread *td, int fd, const char *path,
3272 enum uio_seg pathseg, struct timespec *tptr, enum uio_seg tptrseg,
3275 struct nameidata nd;
3276 struct timespec ts[2];
3279 if ((flag & ~(AT_SYMLINK_NOFOLLOW | AT_BENEATH)) != 0)
3282 if ((error = getutimens(tptr, tptrseg, ts, &flags)) != 0)
3284 NDINIT_ATRIGHTS(&nd, LOOKUP, ((flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW :
3285 FOLLOW) | ((flag & AT_BENEATH) != 0 ? BENEATH : 0) | AUDITVNODE1,
3286 pathseg, path, fd, &cap_futimes_rights, td);
3287 if ((error = namei(&nd)) != 0)
3290 * We are allowed to call namei() regardless of 2xUTIME_OMIT.
3292 * "If both tv_nsec fields are UTIME_OMIT... EACCESS may be detected."
3293 * "Search permission is denied by a component of the path prefix."
3295 NDFREE(&nd, NDF_ONLY_PNBUF);
3296 if ((flags & UTIMENS_EXIT) == 0)
3297 error = setutimes(td, nd.ni_vp, ts, 2, flags & UTIMENS_NULL);
3303 * Truncate a file given its path name.
3305 #ifndef _SYS_SYSPROTO_H_
3306 struct truncate_args {
3313 sys_truncate(struct thread *td, struct truncate_args *uap)
3316 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3320 kern_truncate(struct thread *td, const char *path, enum uio_seg pathseg,
3327 struct nameidata nd;
3332 NDINIT(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, td);
3333 if ((error = namei(&nd)) != 0)
3336 rl_cookie = vn_rangelock_wlock(vp, 0, OFF_MAX);
3337 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0) {
3338 vn_rangelock_unlock(vp, rl_cookie);
3342 NDFREE(&nd, NDF_ONLY_PNBUF);
3343 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3344 if (vp->v_type == VDIR)
3347 else if ((error = mac_vnode_check_write(td->td_ucred, NOCRED, vp))) {
3350 else if ((error = vn_writechk(vp)) == 0 &&
3351 (error = VOP_ACCESS(vp, VWRITE, td->td_ucred, td)) == 0) {
3353 vattr.va_size = length;
3354 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3357 vn_finished_write(mp);
3358 vn_rangelock_unlock(vp, rl_cookie);
3363 #if defined(COMPAT_43)
3365 * Truncate a file given its path name.
3367 #ifndef _SYS_SYSPROTO_H_
3368 struct otruncate_args {
3374 otruncate(struct thread *td, struct otruncate_args *uap)
3377 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3379 #endif /* COMPAT_43 */
3381 #if defined(COMPAT_FREEBSD6)
3382 /* Versions with the pad argument */
3384 freebsd6_truncate(struct thread *td, struct freebsd6_truncate_args *uap)
3387 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3391 freebsd6_ftruncate(struct thread *td, struct freebsd6_ftruncate_args *uap)
3394 return (kern_ftruncate(td, uap->fd, uap->length));
3399 kern_fsync(struct thread *td, int fd, bool fullsync)
3404 int error, lock_flags;
3407 error = getvnode(td, fd, &cap_fsync_rights, &fp);
3413 /* XXXKIB: compete outstanding aio writes */;
3415 error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
3418 if (MNT_SHARED_WRITES(mp) ||
3419 ((mp == NULL) && MNT_SHARED_WRITES(vp->v_mount))) {
3420 lock_flags = LK_SHARED;
3422 lock_flags = LK_EXCLUSIVE;
3424 vn_lock(vp, lock_flags | LK_RETRY);
3425 AUDIT_ARG_VNODE1(vp);
3426 if (vp->v_object != NULL) {
3427 VM_OBJECT_WLOCK(vp->v_object);
3428 vm_object_page_clean(vp->v_object, 0, 0, 0);
3429 VM_OBJECT_WUNLOCK(vp->v_object);
3431 error = fullsync ? VOP_FSYNC(vp, MNT_WAIT, td) : VOP_FDATASYNC(vp, td);
3433 vn_finished_write(mp);
3440 * Sync an open file.
3442 #ifndef _SYS_SYSPROTO_H_
3448 sys_fsync(struct thread *td, struct fsync_args *uap)
3451 return (kern_fsync(td, uap->fd, true));
3455 sys_fdatasync(struct thread *td, struct fdatasync_args *uap)
3458 return (kern_fsync(td, uap->fd, false));
3462 * Rename files. Source and destination must either both be directories, or
3463 * both not be directories. If target is a directory, it must be empty.
3465 #ifndef _SYS_SYSPROTO_H_
3466 struct rename_args {
3472 sys_rename(struct thread *td, struct rename_args *uap)
3475 return (kern_renameat(td, AT_FDCWD, uap->from, AT_FDCWD,
3476 uap->to, UIO_USERSPACE));
3479 #ifndef _SYS_SYSPROTO_H_
3480 struct renameat_args {
3488 sys_renameat(struct thread *td, struct renameat_args *uap)
3491 return (kern_renameat(td, uap->oldfd, uap->old, uap->newfd, uap->new,
3496 kern_renameat(struct thread *td, int oldfd, const char *old, int newfd,
3497 const char *new, enum uio_seg pathseg)
3499 struct mount *mp = NULL;
3500 struct vnode *tvp, *fvp, *tdvp;
3501 struct nameidata fromnd, tond;
3507 NDINIT_ATRIGHTS(&fromnd, DELETE, LOCKPARENT | LOCKLEAF | SAVESTART |
3508 AUDITVNODE1, pathseg, old, oldfd,
3509 &cap_renameat_source_rights, td);
3511 NDINIT_ATRIGHTS(&fromnd, DELETE, WANTPARENT | SAVESTART | AUDITVNODE1,
3512 pathseg, old, oldfd,
3513 &cap_renameat_source_rights, td);
3516 if ((error = namei(&fromnd)) != 0)
3519 error = mac_vnode_check_rename_from(td->td_ucred, fromnd.ni_dvp,
3520 fromnd.ni_vp, &fromnd.ni_cnd);
3521 VOP_UNLOCK(fromnd.ni_dvp, 0);
3522 if (fromnd.ni_dvp != fromnd.ni_vp)
3523 VOP_UNLOCK(fromnd.ni_vp, 0);
3526 NDINIT_ATRIGHTS(&tond, RENAME, LOCKPARENT | LOCKLEAF | NOCACHE |
3527 SAVESTART | AUDITVNODE2, pathseg, new, newfd,
3528 &cap_renameat_target_rights, td);
3529 if (fromnd.ni_vp->v_type == VDIR)
3530 tond.ni_cnd.cn_flags |= WILLBEDIR;
3531 if ((error = namei(&tond)) != 0) {
3532 /* Translate error code for rename("dir1", "dir2/."). */
3533 if (error == EISDIR && fvp->v_type == VDIR)
3535 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3536 vrele(fromnd.ni_dvp);
3542 error = vn_start_write(fvp, &mp, V_NOWAIT);
3544 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3545 NDFREE(&tond, NDF_ONLY_PNBUF);
3552 vrele(fromnd.ni_dvp);
3554 vrele(tond.ni_startdir);
3555 if (fromnd.ni_startdir != NULL)
3556 vrele(fromnd.ni_startdir);
3557 error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH);
3563 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
3566 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
3571 if (newfd != AT_FDCWD && (tond.ni_resflags & NIRES_ABS) == 0) {
3573 * If the target already exists we require CAP_UNLINKAT
3574 * from 'newfd', when newfd was used for the lookup.
3576 error = cap_check(&tond.ni_filecaps.fc_rights,
3577 &cap_unlinkat_rights);
3588 * If the source is the same as the destination (that is, if they
3589 * are links to the same vnode), then there is nothing to do.
3595 error = mac_vnode_check_rename_to(td->td_ucred, tdvp,
3596 tond.ni_vp, fromnd.ni_dvp == tdvp, &tond.ni_cnd);
3600 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
3601 tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
3602 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3603 NDFREE(&tond, NDF_ONLY_PNBUF);
3605 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3606 NDFREE(&tond, NDF_ONLY_PNBUF);
3613 vrele(fromnd.ni_dvp);
3616 vrele(tond.ni_startdir);
3617 vn_finished_write(mp);
3619 if (fromnd.ni_startdir)
3620 vrele(fromnd.ni_startdir);
3627 * Make a directory file.
3629 #ifndef _SYS_SYSPROTO_H_
3636 sys_mkdir(struct thread *td, struct mkdir_args *uap)
3639 return (kern_mkdirat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
3643 #ifndef _SYS_SYSPROTO_H_
3644 struct mkdirat_args {
3651 sys_mkdirat(struct thread *td, struct mkdirat_args *uap)
3654 return (kern_mkdirat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode));
3658 kern_mkdirat(struct thread *td, int fd, const char *path, enum uio_seg segflg,
3664 struct nameidata nd;
3667 AUDIT_ARG_MODE(mode);
3670 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
3671 NOCACHE, segflg, path, fd, &cap_mkdirat_rights,
3673 nd.ni_cnd.cn_flags |= WILLBEDIR;
3674 if ((error = namei(&nd)) != 0)
3678 NDFREE(&nd, NDF_ONLY_PNBUF);
3680 * XXX namei called with LOCKPARENT but not LOCKLEAF has
3681 * the strange behaviour of leaving the vnode unlocked
3682 * if the target is the same vnode as the parent.
3684 if (vp == nd.ni_dvp)
3691 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3692 NDFREE(&nd, NDF_ONLY_PNBUF);
3694 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3699 vattr.va_type = VDIR;
3700 vattr.va_mode = (mode & ACCESSPERMS) &~ td->td_proc->p_fd->fd_cmask;
3702 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
3707 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
3711 NDFREE(&nd, NDF_ONLY_PNBUF);
3715 vn_finished_write(mp);
3720 * Remove a directory file.
3722 #ifndef _SYS_SYSPROTO_H_
3728 sys_rmdir(struct thread *td, struct rmdir_args *uap)
3731 return (kern_frmdirat(td, AT_FDCWD, uap->path, FD_NONE, UIO_USERSPACE,
3736 kern_frmdirat(struct thread *td, int dfd, const char *path, int fd,
3737 enum uio_seg pathseg, int flag)
3742 struct nameidata nd;
3743 cap_rights_t rights;
3747 if (fd != FD_NONE) {
3748 error = getvnode(td, fd, cap_rights_init(&rights, CAP_LOOKUP),
3756 NDINIT_ATRIGHTS(&nd, DELETE, LOCKPARENT | LOCKLEAF | AUDITVNODE1 |
3757 ((flag & AT_BENEATH) != 0 ? BENEATH : 0),
3758 pathseg, path, dfd, &cap_unlinkat_rights, td);
3759 if ((error = namei(&nd)) != 0)
3762 if (vp->v_type != VDIR) {
3767 * No rmdir "." please.
3769 if (nd.ni_dvp == vp) {
3774 * The root of a mounted filesystem cannot be deleted.
3776 if (vp->v_vflag & VV_ROOT) {
3781 if (fp != NULL && fp->f_vnode != vp) {
3782 if ((fp->f_vnode->v_iflag & VI_DOOMED) != 0)
3790 error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp,
3795 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3796 NDFREE(&nd, NDF_ONLY_PNBUF);
3798 if (nd.ni_dvp == vp)
3802 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3806 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_UNLINK);
3807 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
3808 vn_finished_write(mp);
3810 NDFREE(&nd, NDF_ONLY_PNBUF);
3812 if (nd.ni_dvp == vp)
3822 #if defined(COMPAT_43) || defined(COMPAT_FREEBSD11)
3824 freebsd11_kern_getdirentries(struct thread *td, int fd, char *ubuf, u_int count,
3825 long *basep, void (*func)(struct freebsd11_dirent *))
3827 struct freebsd11_dirent dstdp;
3828 struct dirent *dp, *edp;
3831 ssize_t resid, ucount;
3834 /* XXX arbitrary sanity limit on `count'. */
3835 count = min(count, 64 * 1024);
3837 dirbuf = malloc(count, M_TEMP, M_WAITOK);
3839 error = kern_getdirentries(td, fd, dirbuf, count, &base, &resid,
3847 for (dp = (struct dirent *)dirbuf,
3848 edp = (struct dirent *)&dirbuf[count - resid];
3849 ucount < count && dp < edp; ) {
3850 if (dp->d_reclen == 0)
3852 MPASS(dp->d_reclen >= _GENERIC_DIRLEN(0));
3853 if (dp->d_namlen >= sizeof(dstdp.d_name))
3855 dstdp.d_type = dp->d_type;
3856 dstdp.d_namlen = dp->d_namlen;
3857 dstdp.d_fileno = dp->d_fileno; /* truncate */
3858 if (dstdp.d_fileno != dp->d_fileno) {
3859 switch (ino64_trunc_error) {
3867 dstdp.d_fileno = UINT32_MAX;
3871 dstdp.d_reclen = sizeof(dstdp) - sizeof(dstdp.d_name) +
3872 ((dp->d_namlen + 1 + 3) &~ 3);
3873 bcopy(dp->d_name, dstdp.d_name, dstdp.d_namlen);
3874 bzero(dstdp.d_name + dstdp.d_namlen,
3875 dstdp.d_reclen - offsetof(struct freebsd11_dirent, d_name) -
3877 MPASS(dstdp.d_reclen <= dp->d_reclen);
3878 MPASS(ucount + dstdp.d_reclen <= count);
3881 error = copyout(&dstdp, ubuf + ucount, dstdp.d_reclen);
3884 dp = (struct dirent *)((char *)dp + dp->d_reclen);
3885 ucount += dstdp.d_reclen;
3889 free(dirbuf, M_TEMP);
3891 td->td_retval[0] = ucount;
3898 ogetdirentries_cvt(struct freebsd11_dirent *dp)
3900 #if (BYTE_ORDER == LITTLE_ENDIAN)
3902 * The expected low byte of dp->d_namlen is our dp->d_type.
3903 * The high MBZ byte of dp->d_namlen is our dp->d_namlen.
3905 dp->d_type = dp->d_namlen;
3909 * The dp->d_type is the high byte of the expected dp->d_namlen,
3910 * so must be zero'ed.
3917 * Read a block of directory entries in a filesystem independent format.
3919 #ifndef _SYS_SYSPROTO_H_
3920 struct ogetdirentries_args {
3928 ogetdirentries(struct thread *td, struct ogetdirentries_args *uap)
3933 error = kern_ogetdirentries(td, uap, &loff);
3935 error = copyout(&loff, uap->basep, sizeof(long));
3940 kern_ogetdirentries(struct thread *td, struct ogetdirentries_args *uap,
3946 /* XXX arbitrary sanity limit on `count'. */
3947 if (uap->count > 64 * 1024)
3950 error = freebsd11_kern_getdirentries(td, uap->fd, uap->buf, uap->count,
3951 &base, ogetdirentries_cvt);
3953 if (error == 0 && uap->basep != NULL)
3954 error = copyout(&base, uap->basep, sizeof(long));
3958 #endif /* COMPAT_43 */
3960 #if defined(COMPAT_FREEBSD11)
3961 #ifndef _SYS_SYSPROTO_H_
3962 struct freebsd11_getdirentries_args {
3970 freebsd11_getdirentries(struct thread *td,
3971 struct freebsd11_getdirentries_args *uap)
3976 error = freebsd11_kern_getdirentries(td, uap->fd, uap->buf, uap->count,
3979 if (error == 0 && uap->basep != NULL)
3980 error = copyout(&base, uap->basep, sizeof(long));
3985 freebsd11_getdents(struct thread *td, struct freebsd11_getdents_args *uap)
3987 struct freebsd11_getdirentries_args ap;
3991 ap.count = uap->count;
3993 return (freebsd11_getdirentries(td, &ap));
3995 #endif /* COMPAT_FREEBSD11 */
3998 * Read a block of directory entries in a filesystem independent format.
4001 sys_getdirentries(struct thread *td, struct getdirentries_args *uap)
4006 error = kern_getdirentries(td, uap->fd, uap->buf, uap->count, &base,
4007 NULL, UIO_USERSPACE);
4010 if (uap->basep != NULL)
4011 error = copyout(&base, uap->basep, sizeof(off_t));
4016 kern_getdirentries(struct thread *td, int fd, char *buf, size_t count,
4017 off_t *basep, ssize_t *residp, enum uio_seg bufseg)
4028 if (count > IOSIZE_MAX)
4030 auio.uio_resid = count;
4031 error = getvnode(td, fd, &cap_read_rights, &fp);
4034 if ((fp->f_flag & FREAD) == 0) {
4039 foffset = foffset_lock(fp, 0);
4041 if (vp->v_type != VDIR) {
4045 aiov.iov_base = buf;
4046 aiov.iov_len = count;
4047 auio.uio_iov = &aiov;
4048 auio.uio_iovcnt = 1;
4049 auio.uio_rw = UIO_READ;
4050 auio.uio_segflg = bufseg;
4052 vn_lock(vp, LK_SHARED | LK_RETRY);
4053 AUDIT_ARG_VNODE1(vp);
4054 loff = auio.uio_offset = foffset;
4056 error = mac_vnode_check_readdir(td->td_ucred, vp);
4059 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL,
4061 foffset = auio.uio_offset;
4066 if (count == auio.uio_resid &&
4067 (vp->v_vflag & VV_ROOT) &&
4068 (vp->v_mount->mnt_flag & MNT_UNION)) {
4069 struct vnode *tvp = vp;
4071 vp = vp->v_mount->mnt_vnodecovered;
4082 *residp = auio.uio_resid;
4083 td->td_retval[0] = count - auio.uio_resid;
4085 foffset_unlock(fp, foffset, 0);
4091 * Set the mode mask for creation of filesystem nodes.
4093 #ifndef _SYS_SYSPROTO_H_
4099 sys_umask(struct thread *td, struct umask_args *uap)
4101 struct filedesc *fdp;
4103 fdp = td->td_proc->p_fd;
4104 FILEDESC_XLOCK(fdp);
4105 td->td_retval[0] = fdp->fd_cmask;
4106 fdp->fd_cmask = uap->newmask & ALLPERMS;
4107 FILEDESC_XUNLOCK(fdp);
4112 * Void all references to file by ripping underlying filesystem away from
4115 #ifndef _SYS_SYSPROTO_H_
4116 struct revoke_args {
4121 sys_revoke(struct thread *td, struct revoke_args *uap)
4125 struct nameidata nd;
4128 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
4130 if ((error = namei(&nd)) != 0)
4133 NDFREE(&nd, NDF_ONLY_PNBUF);
4134 if (vp->v_type != VCHR || vp->v_rdev == NULL) {
4139 error = mac_vnode_check_revoke(td->td_ucred, vp);
4143 error = VOP_GETATTR(vp, &vattr, td->td_ucred);
4146 if (td->td_ucred->cr_uid != vattr.va_uid) {
4147 error = priv_check(td, PRIV_VFS_ADMIN);
4152 VOP_REVOKE(vp, REVOKEALL);
4159 * Convert a user file descriptor to a kernel file entry and check that, if it
4160 * is a capability, the correct rights are present. A reference on the file
4161 * entry is held upon returning.
4164 getvnode(struct thread *td, int fd, cap_rights_t *rightsp, struct file **fpp)
4169 error = fget_unlocked(td->td_proc->p_fd, fd, rightsp, &fp, NULL);
4174 * The file could be not of the vnode type, or it may be not
4175 * yet fully initialized, in which case the f_vnode pointer
4176 * may be set, but f_ops is still badfileops. E.g.,
4177 * devfs_open() transiently create such situation to
4178 * facilitate csw d_fdopen().
4180 * Dupfdopen() handling in kern_openat() installs the
4181 * half-baked file into the process descriptor table, allowing
4182 * other thread to dereference it. Guard against the race by
4185 if (fp->f_vnode == NULL || fp->f_ops == &badfileops) {
4195 * Get an (NFS) file handle.
4197 #ifndef _SYS_SYSPROTO_H_
4198 struct lgetfh_args {
4204 sys_lgetfh(struct thread *td, struct lgetfh_args *uap)
4207 return (kern_getfhat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->fname,
4208 UIO_USERSPACE, uap->fhp));
4211 #ifndef _SYS_SYSPROTO_H_
4218 sys_getfh(struct thread *td, struct getfh_args *uap)
4221 return (kern_getfhat(td, 0, AT_FDCWD, uap->fname, UIO_USERSPACE,
4226 * syscall for the rpc.lockd to use to translate an open descriptor into
4227 * a NFS file handle.
4229 * warning: do not remove the priv_check() call or this becomes one giant
4232 #ifndef _SYS_SYSPROTO_H_
4233 struct getfhat_args {
4241 sys_getfhat(struct thread *td, struct getfhat_args *uap)
4244 if ((uap->flags & ~(AT_SYMLINK_NOFOLLOW | AT_BENEATH)) != 0)
4246 return (kern_getfhat(td, uap->flags, uap->fd, uap->path, UIO_USERSPACE,
4251 kern_getfhat(struct thread *td, int flags, int fd, const char *path,
4252 enum uio_seg pathseg, fhandle_t *fhp)
4254 struct nameidata nd;
4259 error = priv_check(td, PRIV_VFS_GETFH);
4262 NDINIT_AT(&nd, LOOKUP, ((flags & AT_SYMLINK_NOFOLLOW) != 0 ? NOFOLLOW :
4263 FOLLOW) | ((flags & AT_BENEATH) != 0 ? BENEATH : 0) | LOCKLEAF |
4264 AUDITVNODE1, pathseg, path, fd, td);
4268 NDFREE(&nd, NDF_ONLY_PNBUF);
4270 bzero(&fh, sizeof(fh));
4271 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
4272 error = VOP_VPTOFH(vp, &fh.fh_fid);
4275 error = copyout(&fh, fhp, sizeof (fh));
4279 #ifndef _SYS_SYSPROTO_H_
4280 struct fhlink_args {
4286 sys_fhlink(struct thread *td, struct fhlink_args *uap)
4289 return (kern_fhlinkat(td, AT_FDCWD, uap->to, UIO_USERSPACE, uap->fhp));
4292 #ifndef _SYS_SYSPROTO_H_
4293 struct fhlinkat_args {
4300 sys_fhlinkat(struct thread *td, struct fhlinkat_args *uap)
4303 return (kern_fhlinkat(td, uap->tofd, uap->to, UIO_USERSPACE, uap->fhp));
4307 kern_fhlinkat(struct thread *td, int fd, const char *path,
4308 enum uio_seg pathseg, fhandle_t *fhp)
4315 error = priv_check(td, PRIV_VFS_GETFH);
4318 error = copyin(fhp, &fh, sizeof(fh));
4323 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4325 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_SHARED, &vp);
4330 } while ((error = kern_linkat_vp(td, vp, fd, path, pathseg)) == EAGAIN);
4334 #ifndef _SYS_SYSPROTO_H_
4335 struct fhreadlink_args {
4342 sys_fhreadlink(struct thread *td, struct fhreadlink_args *uap)
4349 error = priv_check(td, PRIV_VFS_GETFH);
4352 if (uap->bufsize > IOSIZE_MAX)
4354 error = copyin(uap->fhp, &fh, sizeof(fh));
4357 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4359 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_SHARED, &vp);
4363 error = kern_readlink_vp(vp, uap->buf, UIO_USERSPACE, uap->bufsize, td);
4369 * syscall for the rpc.lockd to use to translate a NFS file handle into an
4372 * warning: do not remove the priv_check() call or this becomes one giant
4375 #ifndef _SYS_SYSPROTO_H_
4376 struct fhopen_args {
4377 const struct fhandle *u_fhp;
4382 sys_fhopen(struct thread *td, struct fhopen_args *uap)
4391 error = priv_check(td, PRIV_VFS_FHOPEN);
4395 fmode = FFLAGS(uap->flags);
4396 /* why not allow a non-read/write open for our lockd? */
4397 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
4399 error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
4402 /* find the mount point */
4403 mp = vfs_busyfs(&fhp.fh_fsid);
4406 /* now give me my vnode, it gets returned to me locked */
4407 error = VFS_FHTOVP(mp, &fhp.fh_fid, LK_EXCLUSIVE, &vp);
4412 error = falloc_noinstall(td, &fp);
4418 * An extra reference on `fp' has been held for us by
4419 * falloc_noinstall().
4425 error = vn_open_vnode(vp, fmode, td->td_ucred, td, fp);
4427 KASSERT(fp->f_ops == &badfileops,
4428 ("VOP_OPEN in fhopen() set f_ops"));
4429 KASSERT(td->td_dupfd < 0,
4430 ("fhopen() encountered fdopen()"));
4440 finit(fp, (fmode & FMASK) | (fp->f_flag & FHASLOCK), DTYPE_VNODE, vp,
4443 if ((fmode & O_TRUNC) != 0) {
4444 error = fo_truncate(fp, 0, td->td_ucred, td);
4449 error = finstall(td, fp, &indx, fmode, NULL);
4452 td->td_retval[0] = indx;
4457 * Stat an (NFS) file handle.
4459 #ifndef _SYS_SYSPROTO_H_
4460 struct fhstat_args {
4461 struct fhandle *u_fhp;
4466 sys_fhstat(struct thread *td, struct fhstat_args *uap)
4472 error = copyin(uap->u_fhp, &fh, sizeof(fh));
4475 error = kern_fhstat(td, fh, &sb);
4477 error = copyout(&sb, uap->sb, sizeof(sb));
4482 kern_fhstat(struct thread *td, struct fhandle fh, struct stat *sb)
4488 error = priv_check(td, PRIV_VFS_FHSTAT);
4491 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4493 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_EXCLUSIVE, &vp);
4497 error = vn_stat(vp, sb, td->td_ucred, NOCRED, td);
4503 * Implement fstatfs() for (NFS) file handles.
4505 #ifndef _SYS_SYSPROTO_H_
4506 struct fhstatfs_args {
4507 struct fhandle *u_fhp;
4512 sys_fhstatfs(struct thread *td, struct fhstatfs_args *uap)
4518 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4521 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
4522 error = kern_fhstatfs(td, fh, sfp);
4524 error = copyout(sfp, uap->buf, sizeof(*sfp));
4525 free(sfp, M_STATFS);
4530 kern_fhstatfs(struct thread *td, fhandle_t fh, struct statfs *buf)
4536 error = priv_check(td, PRIV_VFS_FHSTATFS);
4539 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4541 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_EXCLUSIVE, &vp);
4547 error = prison_canseemount(td->td_ucred, mp);
4551 error = mac_mount_check_stat(td->td_ucred, mp);
4555 error = VFS_STATFS(mp, buf);
4562 kern_posix_fallocate(struct thread *td, int fd, off_t offset, off_t len)
4567 off_t olen, ooffset;
4570 int audited_vnode1 = 0;
4574 if (offset < 0 || len <= 0)
4576 /* Check for wrap. */
4577 if (offset > OFF_MAX - len)
4580 error = fget(td, fd, &cap_pwrite_rights, &fp);
4583 AUDIT_ARG_FILE(td->td_proc, fp);
4584 if ((fp->f_ops->fo_flags & DFLAG_SEEKABLE) == 0) {
4588 if ((fp->f_flag & FWRITE) == 0) {
4592 if (fp->f_type != DTYPE_VNODE) {
4597 if (vp->v_type != VREG) {
4602 /* Allocating blocks may take a long time, so iterate. */
4609 error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
4612 error = vn_lock(vp, LK_EXCLUSIVE);
4614 vn_finished_write(mp);
4618 if (!audited_vnode1) {
4619 AUDIT_ARG_VNODE1(vp);
4624 error = mac_vnode_check_write(td->td_ucred, fp->f_cred, vp);
4627 error = VOP_ALLOCATE(vp, &offset, &len);
4629 vn_finished_write(mp);
4631 if (olen + ooffset != offset + len) {
4632 panic("offset + len changed from %jx/%jx to %jx/%jx",
4633 ooffset, olen, offset, len);
4635 if (error != 0 || len == 0)
4637 KASSERT(olen > len, ("Iteration did not make progress?"));
4646 sys_posix_fallocate(struct thread *td, struct posix_fallocate_args *uap)
4650 error = kern_posix_fallocate(td, uap->fd, uap->offset, uap->len);
4651 return (kern_posix_error(td, error));
4655 * Unlike madvise(2), we do not make a best effort to remember every
4656 * possible caching hint. Instead, we remember the last setting with
4657 * the exception that we will allow POSIX_FADV_NORMAL to adjust the
4658 * region of any current setting.
4661 kern_posix_fadvise(struct thread *td, int fd, off_t offset, off_t len,
4664 struct fadvise_info *fa, *new;
4670 if (offset < 0 || len < 0 || offset > OFF_MAX - len)
4672 AUDIT_ARG_VALUE(advice);
4674 case POSIX_FADV_SEQUENTIAL:
4675 case POSIX_FADV_RANDOM:
4676 case POSIX_FADV_NOREUSE:
4677 new = malloc(sizeof(*fa), M_FADVISE, M_WAITOK);
4679 case POSIX_FADV_NORMAL:
4680 case POSIX_FADV_WILLNEED:
4681 case POSIX_FADV_DONTNEED:
4687 /* XXX: CAP_POSIX_FADVISE? */
4689 error = fget(td, fd, &cap_no_rights, &fp);
4692 AUDIT_ARG_FILE(td->td_proc, fp);
4693 if ((fp->f_ops->fo_flags & DFLAG_SEEKABLE) == 0) {
4697 if (fp->f_type != DTYPE_VNODE) {
4702 if (vp->v_type != VREG) {
4709 end = offset + len - 1;
4711 case POSIX_FADV_SEQUENTIAL:
4712 case POSIX_FADV_RANDOM:
4713 case POSIX_FADV_NOREUSE:
4715 * Try to merge any existing non-standard region with
4716 * this new region if possible, otherwise create a new
4717 * non-standard region for this request.
4719 mtx_pool_lock(mtxpool_sleep, fp);
4721 if (fa != NULL && fa->fa_advice == advice &&
4722 ((fa->fa_start <= end && fa->fa_end >= offset) ||
4723 (end != OFF_MAX && fa->fa_start == end + 1) ||
4724 (fa->fa_end != OFF_MAX && fa->fa_end + 1 == offset))) {
4725 if (offset < fa->fa_start)
4726 fa->fa_start = offset;
4727 if (end > fa->fa_end)
4730 new->fa_advice = advice;
4731 new->fa_start = offset;
4736 mtx_pool_unlock(mtxpool_sleep, fp);
4738 case POSIX_FADV_NORMAL:
4740 * If a the "normal" region overlaps with an existing
4741 * non-standard region, trim or remove the
4742 * non-standard region.
4744 mtx_pool_lock(mtxpool_sleep, fp);
4747 if (offset <= fa->fa_start && end >= fa->fa_end) {
4749 fp->f_advice = NULL;
4750 } else if (offset <= fa->fa_start &&
4751 end >= fa->fa_start)
4752 fa->fa_start = end + 1;
4753 else if (offset <= fa->fa_end && end >= fa->fa_end)
4754 fa->fa_end = offset - 1;
4755 else if (offset >= fa->fa_start && end <= fa->fa_end) {
4757 * If the "normal" region is a middle
4758 * portion of the existing
4759 * non-standard region, just remove
4760 * the whole thing rather than picking
4761 * one side or the other to
4765 fp->f_advice = NULL;
4768 mtx_pool_unlock(mtxpool_sleep, fp);
4770 case POSIX_FADV_WILLNEED:
4771 case POSIX_FADV_DONTNEED:
4772 error = VOP_ADVISE(vp, offset, end, advice);
4778 free(new, M_FADVISE);
4783 sys_posix_fadvise(struct thread *td, struct posix_fadvise_args *uap)
4787 error = kern_posix_fadvise(td, uap->fd, uap->offset, uap->len,
4789 return (kern_posix_error(td, error));
4793 kern_copy_file_range(struct thread *td, int infd, off_t *inoffp, int outfd,
4794 off_t *outoffp, size_t len, unsigned int flags)
4796 struct file *infp, *outfp;
4797 struct vnode *invp, *outvp;
4800 void *rl_rcookie, *rl_wcookie;
4801 off_t savinoff, savoutoff;
4803 infp = outfp = NULL;
4804 rl_rcookie = rl_wcookie = NULL;
4813 if (len > SSIZE_MAX)
4815 * Although the len argument is size_t, the return argument
4816 * is ssize_t (which is signed). Therefore a size that won't
4817 * fit in ssize_t can't be returned.
4821 /* Get the file structures for the file descriptors. */
4822 error = fget_read(td, infd, &cap_read_rights, &infp);
4825 error = fget_write(td, outfd, &cap_write_rights, &outfp);
4829 /* Set the offset pointers to the correct place. */
4831 inoffp = &infp->f_offset;
4832 if (outoffp == NULL)
4833 outoffp = &outfp->f_offset;
4835 savoutoff = *outoffp;
4837 invp = infp->f_vnode;
4838 outvp = outfp->f_vnode;
4839 /* Sanity check the f_flag bits. */
4840 if ((outfp->f_flag & (FWRITE | FAPPEND)) != FWRITE ||
4841 (infp->f_flag & FREAD) == 0) {
4846 /* If len == 0, just return 0. */
4851 * If infp and outfp refer to the same file, the byte ranges cannot
4854 if (invp == outvp && ((savinoff <= savoutoff && savinoff + len >
4855 savoutoff) || (savinoff > savoutoff && savoutoff + len >
4861 /* Range lock the byte ranges for both invp and outvp. */
4863 rl_wcookie = vn_rangelock_wlock(outvp, *outoffp, *outoffp +
4865 rl_rcookie = vn_rangelock_tryrlock(invp, *inoffp, *inoffp +
4867 if (rl_rcookie != NULL)
4869 vn_rangelock_unlock(outvp, rl_wcookie);
4870 rl_rcookie = vn_rangelock_rlock(invp, *inoffp, *inoffp + len);
4871 vn_rangelock_unlock(invp, rl_rcookie);
4875 error = vn_copy_file_range(invp, inoffp, outvp, outoffp, &retlen,
4876 flags, infp->f_cred, outfp->f_cred, td);
4878 if (rl_rcookie != NULL)
4879 vn_rangelock_unlock(invp, rl_rcookie);
4880 if (rl_wcookie != NULL)
4881 vn_rangelock_unlock(outvp, rl_wcookie);
4882 if (savinoff != -1 && (error == EINTR || error == ERESTART)) {
4884 *outoffp = savoutoff;
4890 td->td_retval[0] = retlen;
4895 sys_copy_file_range(struct thread *td, struct copy_file_range_args *uap)
4897 off_t inoff, outoff, *inoffp, *outoffp;
4900 inoffp = outoffp = NULL;
4901 if (uap->inoffp != NULL) {
4902 error = copyin(uap->inoffp, &inoff, sizeof(off_t));
4907 if (uap->outoffp != NULL) {
4908 error = copyin(uap->outoffp, &outoff, sizeof(off_t));
4913 error = kern_copy_file_range(td, uap->infd, inoffp, uap->outfd,
4914 outoffp, uap->len, uap->flags);
4915 if (error == 0 && uap->inoffp != NULL)
4916 error = copyout(inoffp, uap->inoffp, sizeof(off_t));
4917 if (error == 0 && uap->outoffp != NULL)
4918 error = copyout(outoffp, uap->outoffp, sizeof(off_t));