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)
256 error = vfs_busy(mp, 0);
261 error = mac_mount_check_stat(td->td_ucred, mp);
266 * Set these in case the underlying filesystem fails to do so.
269 sp->f_version = STATFS_VERSION;
270 sp->f_namemax = NAME_MAX;
271 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
272 error = VFS_STATFS(mp, sp);
276 if (priv_check(td, PRIV_VFS_GENERATION)) {
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 * Set these in case the underlying filesystem
482 sp->f_version = STATFS_VERSION;
483 sp->f_namemax = NAME_MAX;
484 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
486 * If MNT_NOWAIT is specified, do not refresh
489 if (mode != MNT_NOWAIT) {
490 error = VFS_STATFS(mp, sp);
492 mtx_lock(&mountlist_mtx);
493 nmp = TAILQ_NEXT(mp, mnt_list);
498 if (priv_check(td, PRIV_VFS_GENERATION)) {
499 sptmp = malloc(sizeof(struct statfs), M_STATFS,
502 sptmp->f_fsid.val[0] = sptmp->f_fsid.val[1] = 0;
503 prison_enforce_statfs(td->td_ucred, mp, sptmp);
507 if (bufseg == UIO_SYSSPACE) {
508 bcopy(sp, sfsp, sizeof(*sp));
509 free(sptmp, M_STATFS);
510 } else /* if (bufseg == UIO_USERSPACE) */ {
511 error = copyout(sp, sfsp, sizeof(*sp));
512 free(sptmp, M_STATFS);
521 mtx_lock(&mountlist_mtx);
522 nmp = TAILQ_NEXT(mp, mnt_list);
525 mtx_unlock(&mountlist_mtx);
526 if (sfsp != NULL && count > maxcount)
533 #ifdef COMPAT_FREEBSD4
535 * Get old format filesystem statistics.
537 static void freebsd4_cvtstatfs(struct statfs *, struct ostatfs *);
539 #ifndef _SYS_SYSPROTO_H_
540 struct freebsd4_statfs_args {
546 freebsd4_statfs(struct thread *td, struct freebsd4_statfs_args *uap)
552 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
553 error = kern_statfs(td, uap->path, UIO_USERSPACE, sfp);
555 freebsd4_cvtstatfs(sfp, &osb);
556 error = copyout(&osb, uap->buf, sizeof(osb));
563 * Get filesystem statistics.
565 #ifndef _SYS_SYSPROTO_H_
566 struct freebsd4_fstatfs_args {
572 freebsd4_fstatfs(struct thread *td, struct freebsd4_fstatfs_args *uap)
578 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
579 error = kern_fstatfs(td, uap->fd, sfp);
581 freebsd4_cvtstatfs(sfp, &osb);
582 error = copyout(&osb, uap->buf, sizeof(osb));
589 * Get statistics on all filesystems.
591 #ifndef _SYS_SYSPROTO_H_
592 struct freebsd4_getfsstat_args {
599 freebsd4_getfsstat(struct thread *td, struct freebsd4_getfsstat_args *uap)
601 struct statfs *buf, *sp;
606 if (uap->bufsize < 0)
608 count = uap->bufsize / sizeof(struct ostatfs);
609 if (count > SIZE_MAX / sizeof(struct statfs))
611 size = count * sizeof(struct statfs);
612 error = kern_getfsstat(td, &buf, size, &count, UIO_SYSSPACE,
615 td->td_retval[0] = count;
618 while (count != 0 && error == 0) {
619 freebsd4_cvtstatfs(sp, &osb);
620 error = copyout(&osb, uap->buf, sizeof(osb));
631 * Implement fstatfs() for (NFS) file handles.
633 #ifndef _SYS_SYSPROTO_H_
634 struct freebsd4_fhstatfs_args {
635 struct fhandle *u_fhp;
640 freebsd4_fhstatfs(struct thread *td, struct freebsd4_fhstatfs_args *uap)
647 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
650 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
651 error = kern_fhstatfs(td, fh, sfp);
653 freebsd4_cvtstatfs(sfp, &osb);
654 error = copyout(&osb, uap->buf, sizeof(osb));
661 * Convert a new format statfs structure to an old format statfs structure.
664 freebsd4_cvtstatfs(struct statfs *nsp, struct ostatfs *osp)
667 statfs_scale_blocks(nsp, LONG_MAX);
668 bzero(osp, sizeof(*osp));
669 osp->f_bsize = nsp->f_bsize;
670 osp->f_iosize = MIN(nsp->f_iosize, LONG_MAX);
671 osp->f_blocks = nsp->f_blocks;
672 osp->f_bfree = nsp->f_bfree;
673 osp->f_bavail = nsp->f_bavail;
674 osp->f_files = MIN(nsp->f_files, LONG_MAX);
675 osp->f_ffree = MIN(nsp->f_ffree, LONG_MAX);
676 osp->f_owner = nsp->f_owner;
677 osp->f_type = nsp->f_type;
678 osp->f_flags = nsp->f_flags;
679 osp->f_syncwrites = MIN(nsp->f_syncwrites, LONG_MAX);
680 osp->f_asyncwrites = MIN(nsp->f_asyncwrites, LONG_MAX);
681 osp->f_syncreads = MIN(nsp->f_syncreads, LONG_MAX);
682 osp->f_asyncreads = MIN(nsp->f_asyncreads, LONG_MAX);
683 strlcpy(osp->f_fstypename, nsp->f_fstypename,
684 MIN(MFSNAMELEN, OMFSNAMELEN));
685 strlcpy(osp->f_mntonname, nsp->f_mntonname,
686 MIN(MNAMELEN, OMNAMELEN));
687 strlcpy(osp->f_mntfromname, nsp->f_mntfromname,
688 MIN(MNAMELEN, OMNAMELEN));
689 osp->f_fsid = nsp->f_fsid;
691 #endif /* COMPAT_FREEBSD4 */
693 #if defined(COMPAT_FREEBSD11)
695 * Get old format filesystem statistics.
697 static void freebsd11_cvtstatfs(struct statfs *, struct freebsd11_statfs *);
700 freebsd11_statfs(struct thread *td, struct freebsd11_statfs_args *uap)
702 struct freebsd11_statfs osb;
706 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
707 error = kern_statfs(td, uap->path, UIO_USERSPACE, sfp);
709 freebsd11_cvtstatfs(sfp, &osb);
710 error = copyout(&osb, uap->buf, sizeof(osb));
717 * Get filesystem statistics.
720 freebsd11_fstatfs(struct thread *td, struct freebsd11_fstatfs_args *uap)
722 struct freebsd11_statfs osb;
726 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
727 error = kern_fstatfs(td, uap->fd, sfp);
729 freebsd11_cvtstatfs(sfp, &osb);
730 error = copyout(&osb, uap->buf, sizeof(osb));
737 * Get statistics on all filesystems.
740 freebsd11_getfsstat(struct thread *td, struct freebsd11_getfsstat_args *uap)
742 struct freebsd11_statfs osb;
743 struct statfs *buf, *sp;
747 count = uap->bufsize / sizeof(struct ostatfs);
748 size = count * sizeof(struct statfs);
749 error = kern_getfsstat(td, &buf, size, &count, UIO_SYSSPACE,
752 td->td_retval[0] = count;
755 while (count > 0 && error == 0) {
756 freebsd11_cvtstatfs(sp, &osb);
757 error = copyout(&osb, uap->buf, sizeof(osb));
768 * Implement fstatfs() for (NFS) file handles.
771 freebsd11_fhstatfs(struct thread *td, struct freebsd11_fhstatfs_args *uap)
773 struct freebsd11_statfs osb;
778 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
781 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
782 error = kern_fhstatfs(td, fh, sfp);
784 freebsd11_cvtstatfs(sfp, &osb);
785 error = copyout(&osb, uap->buf, sizeof(osb));
792 * Convert a new format statfs structure to an old format statfs structure.
795 freebsd11_cvtstatfs(struct statfs *nsp, struct freebsd11_statfs *osp)
798 bzero(osp, sizeof(*osp));
799 osp->f_version = FREEBSD11_STATFS_VERSION;
800 osp->f_type = nsp->f_type;
801 osp->f_flags = nsp->f_flags;
802 osp->f_bsize = nsp->f_bsize;
803 osp->f_iosize = nsp->f_iosize;
804 osp->f_blocks = nsp->f_blocks;
805 osp->f_bfree = nsp->f_bfree;
806 osp->f_bavail = nsp->f_bavail;
807 osp->f_files = nsp->f_files;
808 osp->f_ffree = nsp->f_ffree;
809 osp->f_syncwrites = nsp->f_syncwrites;
810 osp->f_asyncwrites = nsp->f_asyncwrites;
811 osp->f_syncreads = nsp->f_syncreads;
812 osp->f_asyncreads = nsp->f_asyncreads;
813 osp->f_namemax = nsp->f_namemax;
814 osp->f_owner = nsp->f_owner;
815 osp->f_fsid = nsp->f_fsid;
816 strlcpy(osp->f_fstypename, nsp->f_fstypename,
817 MIN(MFSNAMELEN, sizeof(osp->f_fstypename)));
818 strlcpy(osp->f_mntonname, nsp->f_mntonname,
819 MIN(MNAMELEN, sizeof(osp->f_mntonname)));
820 strlcpy(osp->f_mntfromname, nsp->f_mntfromname,
821 MIN(MNAMELEN, sizeof(osp->f_mntfromname)));
823 #endif /* COMPAT_FREEBSD11 */
826 * Change current working directory to a given file descriptor.
828 #ifndef _SYS_SYSPROTO_H_
834 sys_fchdir(struct thread *td, struct fchdir_args *uap)
836 struct vnode *vp, *tdp;
841 AUDIT_ARG_FD(uap->fd);
842 error = getvnode(td, uap->fd, &cap_fchdir_rights,
849 vn_lock(vp, LK_SHARED | LK_RETRY);
850 AUDIT_ARG_VNODE1(vp);
851 error = change_dir(vp, td);
852 while (!error && (mp = vp->v_mountedhere) != NULL) {
855 error = VFS_ROOT(mp, LK_SHARED, &tdp);
872 * Change current working directory (``.'').
874 #ifndef _SYS_SYSPROTO_H_
880 sys_chdir(struct thread *td, struct chdir_args *uap)
883 return (kern_chdir(td, uap->path, UIO_USERSPACE));
887 kern_chdir(struct thread *td, const char *path, enum uio_seg pathseg)
892 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
894 if ((error = namei(&nd)) != 0)
896 if ((error = change_dir(nd.ni_vp, td)) != 0) {
898 NDFREE(&nd, NDF_ONLY_PNBUF);
901 VOP_UNLOCK(nd.ni_vp, 0);
902 NDFREE(&nd, NDF_ONLY_PNBUF);
903 pwd_chdir(td, nd.ni_vp);
908 * Change notion of root (``/'') directory.
910 #ifndef _SYS_SYSPROTO_H_
916 sys_chroot(struct thread *td, struct chroot_args *uap)
921 error = priv_check(td, PRIV_VFS_CHROOT);
924 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
925 UIO_USERSPACE, uap->path, td);
929 error = change_dir(nd.ni_vp, td);
933 error = mac_vnode_check_chroot(td->td_ucred, nd.ni_vp);
937 VOP_UNLOCK(nd.ni_vp, 0);
938 error = pwd_chroot(td, nd.ni_vp);
940 NDFREE(&nd, NDF_ONLY_PNBUF);
945 NDFREE(&nd, NDF_ONLY_PNBUF);
950 * Common routine for chroot and chdir. Callers must provide a locked vnode
954 change_dir(struct vnode *vp, struct thread *td)
960 ASSERT_VOP_LOCKED(vp, "change_dir(): vp not locked");
961 if (vp->v_type != VDIR)
964 error = mac_vnode_check_chdir(td->td_ucred, vp);
968 return (VOP_ACCESS(vp, VEXEC, td->td_ucred, td));
972 flags_to_rights(int flags, cap_rights_t *rightsp)
975 if (flags & O_EXEC) {
976 cap_rights_set(rightsp, CAP_FEXECVE);
978 switch ((flags & O_ACCMODE)) {
980 cap_rights_set(rightsp, CAP_READ);
983 cap_rights_set(rightsp, CAP_READ);
986 cap_rights_set(rightsp, CAP_WRITE);
987 if (!(flags & (O_APPEND | O_TRUNC)))
988 cap_rights_set(rightsp, CAP_SEEK);
994 cap_rights_set(rightsp, CAP_CREATE);
997 cap_rights_set(rightsp, CAP_FTRUNCATE);
999 if (flags & (O_SYNC | O_FSYNC))
1000 cap_rights_set(rightsp, CAP_FSYNC);
1002 if (flags & (O_EXLOCK | O_SHLOCK))
1003 cap_rights_set(rightsp, CAP_FLOCK);
1007 * Check permissions, allocate an open file structure, and call the device
1008 * open routine if any.
1010 #ifndef _SYS_SYSPROTO_H_
1018 sys_open(struct thread *td, struct open_args *uap)
1021 return (kern_openat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1022 uap->flags, uap->mode));
1025 #ifndef _SYS_SYSPROTO_H_
1026 struct openat_args {
1034 sys_openat(struct thread *td, struct openat_args *uap)
1037 AUDIT_ARG_FD(uap->fd);
1038 return (kern_openat(td, uap->fd, uap->path, UIO_USERSPACE, uap->flag,
1043 kern_openat(struct thread *td, int fd, const char *path, enum uio_seg pathseg,
1044 int flags, int mode)
1046 struct proc *p = td->td_proc;
1047 struct filedesc *fdp = p->p_fd;
1050 struct nameidata nd;
1051 cap_rights_t rights;
1052 int cmode, error, indx;
1056 AUDIT_ARG_FFLAGS(flags);
1057 AUDIT_ARG_MODE(mode);
1058 cap_rights_init(&rights, CAP_LOOKUP);
1059 flags_to_rights(flags, &rights);
1061 * Only one of the O_EXEC, O_RDONLY, O_WRONLY and O_RDWR flags
1064 if (flags & O_EXEC) {
1065 if (flags & O_ACCMODE)
1067 } else if ((flags & O_ACCMODE) == O_ACCMODE) {
1070 flags = FFLAGS(flags);
1074 * Allocate a file structure. The descriptor to reference it
1075 * is allocated and set by finstall() below.
1077 error = falloc_noinstall(td, &fp);
1081 * An extra reference on `fp' has been held for us by
1082 * falloc_noinstall().
1084 /* Set the flags early so the finit in devfs can pick them up. */
1085 fp->f_flag = flags & FMASK;
1086 cmode = ((mode & ~fdp->fd_cmask) & ALLPERMS) & ~S_ISTXT;
1087 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, fd,
1089 td->td_dupfd = -1; /* XXX check for fdopen */
1090 error = vn_open(&nd, &flags, cmode, fp);
1093 * If the vn_open replaced the method vector, something
1094 * wonderous happened deep below and we just pass it up
1095 * pretending we know what we do.
1097 if (error == ENXIO && fp->f_ops != &badfileops)
1101 * Handle special fdopen() case. bleh.
1103 * Don't do this for relative (capability) lookups; we don't
1104 * understand exactly what would happen, and we don't think
1105 * that it ever should.
1107 if ((nd.ni_lcf & NI_LCF_STRICTRELATIVE) == 0 &&
1108 (error == ENODEV || error == ENXIO) &&
1109 td->td_dupfd >= 0) {
1110 error = dupfdopen(td, fdp, td->td_dupfd, flags, error,
1119 NDFREE(&nd, NDF_ONLY_PNBUF);
1123 * Store the vnode, for any f_type. Typically, the vnode use
1124 * count is decremented by direct call to vn_closefile() for
1125 * files that switched type in the cdevsw fdopen() method.
1129 * If the file wasn't claimed by devfs bind it to the normal
1130 * vnode operations here.
1132 if (fp->f_ops == &badfileops) {
1133 KASSERT(vp->v_type != VFIFO, ("Unexpected fifo."));
1135 finit(fp, (flags & FMASK) | (fp->f_flag & FHASLOCK),
1136 DTYPE_VNODE, vp, &vnops);
1140 if (flags & O_TRUNC) {
1141 error = fo_truncate(fp, 0, td->td_ucred, td);
1147 * If we haven't already installed the FD (for dupfdopen), do so now.
1150 struct filecaps *fcaps;
1153 if ((nd.ni_lcf & NI_LCF_STRICTRELATIVE) != 0)
1154 fcaps = &nd.ni_filecaps;
1158 error = finstall(td, fp, &indx, flags, fcaps);
1159 /* On success finstall() consumes fcaps. */
1161 filecaps_free(&nd.ni_filecaps);
1165 filecaps_free(&nd.ni_filecaps);
1169 * Release our private reference, leaving the one associated with
1170 * the descriptor table intact.
1173 td->td_retval[0] = indx;
1176 KASSERT(indx == -1, ("indx=%d, should be -1", indx));
1185 #ifndef _SYS_SYSPROTO_H_
1186 struct ocreat_args {
1192 ocreat(struct thread *td, struct ocreat_args *uap)
1195 return (kern_openat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1196 O_WRONLY | O_CREAT | O_TRUNC, uap->mode));
1198 #endif /* COMPAT_43 */
1201 * Create a special file.
1203 #ifndef _SYS_SYSPROTO_H_
1204 struct mknodat_args {
1212 sys_mknodat(struct thread *td, struct mknodat_args *uap)
1215 return (kern_mknodat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode,
1219 #if defined(COMPAT_FREEBSD11)
1221 freebsd11_mknod(struct thread *td,
1222 struct freebsd11_mknod_args *uap)
1225 return (kern_mknodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1226 uap->mode, uap->dev));
1230 freebsd11_mknodat(struct thread *td,
1231 struct freebsd11_mknodat_args *uap)
1234 return (kern_mknodat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode,
1237 #endif /* COMPAT_FREEBSD11 */
1240 kern_mknodat(struct thread *td, int fd, const char *path, enum uio_seg pathseg,
1241 int mode, dev_t dev)
1246 struct nameidata nd;
1247 int error, whiteout = 0;
1249 AUDIT_ARG_MODE(mode);
1251 switch (mode & S_IFMT) {
1254 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
1255 if (error == 0 && dev == VNOVAL)
1259 error = priv_check(td, PRIV_VFS_MKNOD_WHT);
1263 return (kern_mkfifoat(td, fd, path, pathseg, mode));
1273 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
1274 NOCACHE, pathseg, path, fd, &cap_mknodat_rights,
1276 if ((error = namei(&nd)) != 0)
1280 NDFREE(&nd, NDF_ONLY_PNBUF);
1281 if (vp == nd.ni_dvp)
1289 vattr.va_mode = (mode & ALLPERMS) &
1290 ~td->td_proc->p_fd->fd_cmask;
1291 vattr.va_rdev = dev;
1294 switch (mode & S_IFMT) {
1296 vattr.va_type = VCHR;
1299 vattr.va_type = VBLK;
1305 panic("kern_mknod: invalid mode");
1308 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1309 NDFREE(&nd, NDF_ONLY_PNBUF);
1311 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1316 if (error == 0 && !whiteout)
1317 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp,
1318 &nd.ni_cnd, &vattr);
1322 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, CREATE);
1324 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp,
1325 &nd.ni_cnd, &vattr);
1330 NDFREE(&nd, NDF_ONLY_PNBUF);
1332 vn_finished_write(mp);
1337 * Create a named pipe.
1339 #ifndef _SYS_SYSPROTO_H_
1340 struct mkfifo_args {
1346 sys_mkfifo(struct thread *td, struct mkfifo_args *uap)
1349 return (kern_mkfifoat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1353 #ifndef _SYS_SYSPROTO_H_
1354 struct mkfifoat_args {
1361 sys_mkfifoat(struct thread *td, struct mkfifoat_args *uap)
1364 return (kern_mkfifoat(td, uap->fd, uap->path, UIO_USERSPACE,
1369 kern_mkfifoat(struct thread *td, int fd, const char *path,
1370 enum uio_seg pathseg, int mode)
1374 struct nameidata nd;
1377 AUDIT_ARG_MODE(mode);
1380 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
1381 NOCACHE, pathseg, path, fd, &cap_mkfifoat_rights,
1383 if ((error = namei(&nd)) != 0)
1385 if (nd.ni_vp != NULL) {
1386 NDFREE(&nd, NDF_ONLY_PNBUF);
1387 if (nd.ni_vp == nd.ni_dvp)
1394 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1395 NDFREE(&nd, NDF_ONLY_PNBUF);
1397 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1402 vattr.va_type = VFIFO;
1403 vattr.va_mode = (mode & ALLPERMS) & ~td->td_proc->p_fd->fd_cmask;
1405 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1410 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
1417 vn_finished_write(mp);
1418 NDFREE(&nd, NDF_ONLY_PNBUF);
1423 * Make a hard file link.
1425 #ifndef _SYS_SYSPROTO_H_
1432 sys_link(struct thread *td, struct link_args *uap)
1435 return (kern_linkat(td, AT_FDCWD, AT_FDCWD, uap->path, uap->link,
1436 UIO_USERSPACE, FOLLOW));
1439 #ifndef _SYS_SYSPROTO_H_
1440 struct linkat_args {
1449 sys_linkat(struct thread *td, struct linkat_args *uap)
1454 if ((flag & ~(AT_SYMLINK_FOLLOW | AT_BENEATH)) != 0)
1457 return (kern_linkat(td, uap->fd1, uap->fd2, uap->path1, uap->path2,
1458 UIO_USERSPACE, ((flag & AT_SYMLINK_FOLLOW) != 0 ? FOLLOW :
1459 NOFOLLOW) | ((flag & AT_BENEATH) != 0 ? BENEATH : 0)));
1462 int hardlink_check_uid = 0;
1463 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
1464 &hardlink_check_uid, 0,
1465 "Unprivileged processes cannot create hard links to files owned by other "
1467 static int hardlink_check_gid = 0;
1468 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
1469 &hardlink_check_gid, 0,
1470 "Unprivileged processes cannot create hard links to files owned by other "
1474 can_hardlink(struct vnode *vp, struct ucred *cred)
1479 if (!hardlink_check_uid && !hardlink_check_gid)
1482 error = VOP_GETATTR(vp, &va, cred);
1486 if (hardlink_check_uid && cred->cr_uid != va.va_uid) {
1487 error = priv_check_cred(cred, PRIV_VFS_LINK);
1492 if (hardlink_check_gid && !groupmember(va.va_gid, cred)) {
1493 error = priv_check_cred(cred, PRIV_VFS_LINK);
1502 kern_linkat(struct thread *td, int fd1, int fd2, const char *path1,
1503 const char *path2, enum uio_seg segflag, int follow)
1505 struct nameidata nd;
1510 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, segflag,
1511 path1, fd1, &cap_linkat_source_rights, td);
1512 if ((error = namei(&nd)) != 0)
1514 NDFREE(&nd, NDF_ONLY_PNBUF);
1515 error = kern_linkat_vp(td, nd.ni_vp, fd2, path2, segflag);
1516 } while (error == EAGAIN);
1521 kern_linkat_vp(struct thread *td, struct vnode *vp, int fd, const char *path,
1522 enum uio_seg segflag)
1524 struct nameidata nd;
1528 if (vp->v_type == VDIR) {
1530 return (EPERM); /* POSIX */
1532 NDINIT_ATRIGHTS(&nd, CREATE,
1533 LOCKPARENT | SAVENAME | AUDITVNODE2 | NOCACHE, segflag, path, fd,
1534 &cap_linkat_target_rights, td);
1535 if ((error = namei(&nd)) == 0) {
1536 if (nd.ni_vp != NULL) {
1537 NDFREE(&nd, NDF_ONLY_PNBUF);
1538 if (nd.ni_dvp == nd.ni_vp)
1545 } else if (nd.ni_dvp->v_mount != vp->v_mount) {
1547 * Cross-device link. No need to recheck
1548 * vp->v_type, since it cannot change, except
1551 NDFREE(&nd, NDF_ONLY_PNBUF);
1555 } else if ((error = vn_lock(vp, LK_EXCLUSIVE)) == 0) {
1556 error = can_hardlink(vp, td->td_ucred);
1559 error = mac_vnode_check_link(td->td_ucred,
1560 nd.ni_dvp, vp, &nd.ni_cnd);
1565 NDFREE(&nd, NDF_ONLY_PNBUF);
1568 error = vn_start_write(vp, &mp, V_NOWAIT);
1572 NDFREE(&nd, NDF_ONLY_PNBUF);
1573 error = vn_start_write(NULL, &mp,
1579 error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
1582 vn_finished_write(mp);
1583 NDFREE(&nd, NDF_ONLY_PNBUF);
1586 NDFREE(&nd, NDF_ONLY_PNBUF);
1596 * Make a symbolic link.
1598 #ifndef _SYS_SYSPROTO_H_
1599 struct symlink_args {
1605 sys_symlink(struct thread *td, struct symlink_args *uap)
1608 return (kern_symlinkat(td, uap->path, AT_FDCWD, uap->link,
1612 #ifndef _SYS_SYSPROTO_H_
1613 struct symlinkat_args {
1620 sys_symlinkat(struct thread *td, struct symlinkat_args *uap)
1623 return (kern_symlinkat(td, uap->path1, uap->fd, uap->path2,
1628 kern_symlinkat(struct thread *td, const char *path1, int fd, const char *path2,
1629 enum uio_seg segflg)
1633 const char *syspath;
1635 struct nameidata nd;
1638 if (segflg == UIO_SYSSPACE) {
1641 tmppath = uma_zalloc(namei_zone, M_WAITOK);
1642 if ((error = copyinstr(path1, tmppath, MAXPATHLEN, NULL)) != 0)
1646 AUDIT_ARG_TEXT(syspath);
1649 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
1650 NOCACHE, segflg, path2, fd, &cap_symlinkat_rights,
1652 if ((error = namei(&nd)) != 0)
1655 NDFREE(&nd, NDF_ONLY_PNBUF);
1656 if (nd.ni_vp == nd.ni_dvp)
1664 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1665 NDFREE(&nd, NDF_ONLY_PNBUF);
1667 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1672 vattr.va_mode = ACCESSPERMS &~ td->td_proc->p_fd->fd_cmask;
1674 vattr.va_type = VLNK;
1675 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1680 error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr, syspath);
1686 NDFREE(&nd, NDF_ONLY_PNBUF);
1688 vn_finished_write(mp);
1690 if (segflg != UIO_SYSSPACE)
1691 uma_zfree(namei_zone, tmppath);
1696 * Delete a whiteout from the filesystem.
1698 #ifndef _SYS_SYSPROTO_H_
1699 struct undelete_args {
1704 sys_undelete(struct thread *td, struct undelete_args *uap)
1707 struct nameidata nd;
1712 NDINIT(&nd, DELETE, LOCKPARENT | DOWHITEOUT | AUDITVNODE1,
1713 UIO_USERSPACE, uap->path, td);
1718 if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & ISWHITEOUT)) {
1719 NDFREE(&nd, NDF_ONLY_PNBUF);
1720 if (nd.ni_vp == nd.ni_dvp)
1728 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1729 NDFREE(&nd, NDF_ONLY_PNBUF);
1731 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1735 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, DELETE);
1736 NDFREE(&nd, NDF_ONLY_PNBUF);
1738 vn_finished_write(mp);
1743 * Delete a name from the filesystem.
1745 #ifndef _SYS_SYSPROTO_H_
1746 struct unlink_args {
1751 sys_unlink(struct thread *td, struct unlink_args *uap)
1754 return (kern_unlinkat(td, AT_FDCWD, uap->path, UIO_USERSPACE, 0, 0));
1757 #ifndef _SYS_SYSPROTO_H_
1758 struct unlinkat_args {
1765 sys_unlinkat(struct thread *td, struct unlinkat_args *uap)
1774 if ((flag & ~(AT_REMOVEDIR | AT_BENEATH)) != 0)
1777 if ((uap->flag & AT_REMOVEDIR) != 0)
1778 return (kern_rmdirat(td, fd, path, UIO_USERSPACE, flag));
1780 return (kern_unlinkat(td, fd, path, UIO_USERSPACE, flag, 0));
1784 kern_unlinkat(struct thread *td, int fd, const char *path,
1785 enum uio_seg pathseg, int flag, ino_t oldinum)
1789 struct nameidata nd;
1795 NDINIT_ATRIGHTS(&nd, DELETE, LOCKPARENT | LOCKLEAF | AUDITVNODE1 |
1796 ((flag & AT_BENEATH) != 0 ? BENEATH : 0),
1797 pathseg, path, fd, &cap_unlinkat_rights, td);
1798 if ((error = namei(&nd)) != 0)
1799 return (error == EINVAL ? EPERM : error);
1801 if (vp->v_type == VDIR && oldinum == 0) {
1802 error = EPERM; /* POSIX */
1803 } else if (oldinum != 0 &&
1804 ((error = vn_stat(vp, &sb, td->td_ucred, NOCRED, td)) == 0) &&
1805 sb.st_ino != oldinum) {
1806 error = EIDRM; /* Identifier removed */
1809 * The root of a mounted filesystem cannot be deleted.
1811 * XXX: can this only be a VDIR case?
1813 if (vp->v_vflag & VV_ROOT)
1817 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1818 NDFREE(&nd, NDF_ONLY_PNBUF);
1820 if (vp == nd.ni_dvp)
1824 if ((error = vn_start_write(NULL, &mp,
1825 V_XSLEEP | PCATCH)) != 0)
1830 error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp,
1835 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_UNLINK);
1836 error = VOP_REMOVE(nd.ni_dvp, vp, &nd.ni_cnd);
1840 vn_finished_write(mp);
1842 NDFREE(&nd, NDF_ONLY_PNBUF);
1844 if (vp == nd.ni_dvp)
1852 * Reposition read/write file offset.
1854 #ifndef _SYS_SYSPROTO_H_
1863 sys_lseek(struct thread *td, struct lseek_args *uap)
1866 return (kern_lseek(td, uap->fd, uap->offset, uap->whence));
1870 kern_lseek(struct thread *td, int fd, off_t offset, int whence)
1876 error = fget(td, fd, &cap_seek_rights, &fp);
1879 error = (fp->f_ops->fo_flags & DFLAG_SEEKABLE) != 0 ?
1880 fo_seek(fp, offset, whence, td) : ESPIPE;
1885 #if defined(COMPAT_43)
1887 * Reposition read/write file offset.
1889 #ifndef _SYS_SYSPROTO_H_
1890 struct olseek_args {
1897 olseek(struct thread *td, struct olseek_args *uap)
1900 return (kern_lseek(td, uap->fd, uap->offset, uap->whence));
1902 #endif /* COMPAT_43 */
1904 #if defined(COMPAT_FREEBSD6)
1905 /* Version with the 'pad' argument */
1907 freebsd6_lseek(struct thread *td, struct freebsd6_lseek_args *uap)
1910 return (kern_lseek(td, uap->fd, uap->offset, uap->whence));
1915 * Check access permissions using passed credentials.
1918 vn_access(struct vnode *vp, int user_flags, struct ucred *cred,
1924 /* Flags == 0 means only check for existence. */
1925 if (user_flags == 0)
1929 if (user_flags & R_OK)
1931 if (user_flags & W_OK)
1933 if (user_flags & X_OK)
1936 error = mac_vnode_check_access(cred, vp, accmode);
1940 if ((accmode & VWRITE) == 0 || (error = vn_writechk(vp)) == 0)
1941 error = VOP_ACCESS(vp, accmode, cred, td);
1946 * Check access permissions using "real" credentials.
1948 #ifndef _SYS_SYSPROTO_H_
1949 struct access_args {
1955 sys_access(struct thread *td, struct access_args *uap)
1958 return (kern_accessat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1962 #ifndef _SYS_SYSPROTO_H_
1963 struct faccessat_args {
1971 sys_faccessat(struct thread *td, struct faccessat_args *uap)
1974 return (kern_accessat(td, uap->fd, uap->path, UIO_USERSPACE, uap->flag,
1979 kern_accessat(struct thread *td, int fd, const char *path,
1980 enum uio_seg pathseg, int flag, int amode)
1982 struct ucred *cred, *usecred;
1984 struct nameidata nd;
1987 if ((flag & ~(AT_EACCESS | AT_BENEATH)) != 0)
1989 if (amode != F_OK && (amode & ~(R_OK | W_OK | X_OK)) != 0)
1993 * Create and modify a temporary credential instead of one that
1994 * is potentially shared (if we need one).
1996 cred = td->td_ucred;
1997 if ((flag & AT_EACCESS) == 0 &&
1998 ((cred->cr_uid != cred->cr_ruid ||
1999 cred->cr_rgid != cred->cr_groups[0]))) {
2000 usecred = crdup(cred);
2001 usecred->cr_uid = cred->cr_ruid;
2002 usecred->cr_groups[0] = cred->cr_rgid;
2003 td->td_ucred = usecred;
2006 AUDIT_ARG_VALUE(amode);
2007 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF |
2008 AUDITVNODE1 | ((flag & AT_BENEATH) != 0 ? BENEATH : 0),
2009 pathseg, path, fd, &cap_fstat_rights, td);
2010 if ((error = namei(&nd)) != 0)
2014 error = vn_access(vp, amode, usecred, td);
2015 NDFREE(&nd, NDF_ONLY_PNBUF);
2018 if (usecred != cred) {
2019 td->td_ucred = cred;
2026 * Check access permissions using "effective" credentials.
2028 #ifndef _SYS_SYSPROTO_H_
2029 struct eaccess_args {
2035 sys_eaccess(struct thread *td, struct eaccess_args *uap)
2038 return (kern_accessat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2039 AT_EACCESS, uap->amode));
2042 #if defined(COMPAT_43)
2044 * Get file status; this version follows links.
2046 #ifndef _SYS_SYSPROTO_H_
2053 ostat(struct thread *td, struct ostat_args *uap)
2059 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
2064 return (copyout(&osb, uap->ub, sizeof (osb)));
2068 * Get file status; this version does not follow links.
2070 #ifndef _SYS_SYSPROTO_H_
2071 struct olstat_args {
2077 olstat(struct thread *td, struct olstat_args *uap)
2083 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2084 UIO_USERSPACE, &sb, NULL);
2088 return (copyout(&osb, uap->ub, sizeof (osb)));
2092 * Convert from an old to a new stat structure.
2093 * XXX: many values are blindly truncated.
2096 cvtstat(struct stat *st, struct ostat *ost)
2099 bzero(ost, sizeof(*ost));
2100 ost->st_dev = st->st_dev;
2101 ost->st_ino = st->st_ino;
2102 ost->st_mode = st->st_mode;
2103 ost->st_nlink = st->st_nlink;
2104 ost->st_uid = st->st_uid;
2105 ost->st_gid = st->st_gid;
2106 ost->st_rdev = st->st_rdev;
2107 ost->st_size = MIN(st->st_size, INT32_MAX);
2108 ost->st_atim = st->st_atim;
2109 ost->st_mtim = st->st_mtim;
2110 ost->st_ctim = st->st_ctim;
2111 ost->st_blksize = st->st_blksize;
2112 ost->st_blocks = st->st_blocks;
2113 ost->st_flags = st->st_flags;
2114 ost->st_gen = st->st_gen;
2116 #endif /* COMPAT_43 */
2118 #if defined(COMPAT_43) || defined(COMPAT_FREEBSD11)
2119 int ino64_trunc_error;
2120 SYSCTL_INT(_vfs, OID_AUTO, ino64_trunc_error, CTLFLAG_RW,
2121 &ino64_trunc_error, 0,
2122 "Error on truncation of device, file or inode number, or link count");
2125 freebsd11_cvtstat(struct stat *st, struct freebsd11_stat *ost)
2128 ost->st_dev = st->st_dev;
2129 if (ost->st_dev != st->st_dev) {
2130 switch (ino64_trunc_error) {
2133 * Since dev_t is almost raw, don't clamp to the
2134 * maximum for case 2, but ignore the error.
2141 ost->st_ino = st->st_ino;
2142 if (ost->st_ino != st->st_ino) {
2143 switch (ino64_trunc_error) {
2150 ost->st_ino = UINT32_MAX;
2154 ost->st_mode = st->st_mode;
2155 ost->st_nlink = st->st_nlink;
2156 if (ost->st_nlink != st->st_nlink) {
2157 switch (ino64_trunc_error) {
2164 ost->st_nlink = UINT16_MAX;
2168 ost->st_uid = st->st_uid;
2169 ost->st_gid = st->st_gid;
2170 ost->st_rdev = st->st_rdev;
2171 if (ost->st_rdev != st->st_rdev) {
2172 switch (ino64_trunc_error) {
2179 ost->st_atim = st->st_atim;
2180 ost->st_mtim = st->st_mtim;
2181 ost->st_ctim = st->st_ctim;
2182 ost->st_size = st->st_size;
2183 ost->st_blocks = st->st_blocks;
2184 ost->st_blksize = st->st_blksize;
2185 ost->st_flags = st->st_flags;
2186 ost->st_gen = st->st_gen;
2188 ost->st_birthtim = st->st_birthtim;
2189 bzero((char *)&ost->st_birthtim + sizeof(ost->st_birthtim),
2190 sizeof(*ost) - offsetof(struct freebsd11_stat,
2191 st_birthtim) - sizeof(ost->st_birthtim));
2196 freebsd11_stat(struct thread *td, struct freebsd11_stat_args* uap)
2199 struct freebsd11_stat osb;
2202 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
2206 error = freebsd11_cvtstat(&sb, &osb);
2208 error = copyout(&osb, uap->ub, sizeof(osb));
2213 freebsd11_lstat(struct thread *td, struct freebsd11_lstat_args* uap)
2216 struct freebsd11_stat osb;
2219 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2220 UIO_USERSPACE, &sb, NULL);
2223 error = freebsd11_cvtstat(&sb, &osb);
2225 error = copyout(&osb, uap->ub, sizeof(osb));
2230 freebsd11_fhstat(struct thread *td, struct freebsd11_fhstat_args* uap)
2234 struct freebsd11_stat osb;
2237 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
2240 error = kern_fhstat(td, fh, &sb);
2243 error = freebsd11_cvtstat(&sb, &osb);
2245 error = copyout(&osb, uap->sb, sizeof(osb));
2250 freebsd11_fstatat(struct thread *td, struct freebsd11_fstatat_args* uap)
2253 struct freebsd11_stat osb;
2256 error = kern_statat(td, uap->flag, uap->fd, uap->path,
2257 UIO_USERSPACE, &sb, NULL);
2260 error = freebsd11_cvtstat(&sb, &osb);
2262 error = copyout(&osb, uap->buf, sizeof(osb));
2265 #endif /* COMPAT_FREEBSD11 */
2270 #ifndef _SYS_SYSPROTO_H_
2271 struct fstatat_args {
2279 sys_fstatat(struct thread *td, struct fstatat_args *uap)
2284 error = kern_statat(td, uap->flag, uap->fd, uap->path,
2285 UIO_USERSPACE, &sb, NULL);
2287 error = copyout(&sb, uap->buf, sizeof (sb));
2292 kern_statat(struct thread *td, int flag, int fd, const char *path,
2293 enum uio_seg pathseg, struct stat *sbp,
2294 void (*hook)(struct vnode *vp, struct stat *sbp))
2296 struct nameidata nd;
2299 if ((flag & ~(AT_SYMLINK_NOFOLLOW | AT_BENEATH)) != 0)
2302 NDINIT_ATRIGHTS(&nd, LOOKUP, ((flag & AT_SYMLINK_NOFOLLOW) != 0 ?
2303 NOFOLLOW : FOLLOW) | ((flag & AT_BENEATH) != 0 ? BENEATH : 0) |
2304 LOCKSHARED | LOCKLEAF | AUDITVNODE1, pathseg, path, fd,
2305 &cap_fstat_rights, td);
2307 if ((error = namei(&nd)) != 0)
2309 error = vn_stat(nd.ni_vp, sbp, td->td_ucred, NOCRED, td);
2311 SDT_PROBE2(vfs, , stat, mode, path, sbp->st_mode);
2312 if (S_ISREG(sbp->st_mode))
2313 SDT_PROBE2(vfs, , stat, reg, path, pathseg);
2314 if (__predict_false(hook != NULL))
2315 hook(nd.ni_vp, sbp);
2317 NDFREE(&nd, NDF_ONLY_PNBUF);
2321 #ifdef __STAT_TIME_T_EXT
2322 sbp->st_atim_ext = 0;
2323 sbp->st_mtim_ext = 0;
2324 sbp->st_ctim_ext = 0;
2325 sbp->st_btim_ext = 0;
2328 if (KTRPOINT(td, KTR_STRUCT))
2334 #if defined(COMPAT_FREEBSD11)
2336 * Implementation of the NetBSD [l]stat() functions.
2339 freebsd11_cvtnstat(struct stat *sb, struct nstat *nsb)
2342 bzero(nsb, sizeof(*nsb));
2343 nsb->st_dev = sb->st_dev;
2344 nsb->st_ino = sb->st_ino;
2345 nsb->st_mode = sb->st_mode;
2346 nsb->st_nlink = sb->st_nlink;
2347 nsb->st_uid = sb->st_uid;
2348 nsb->st_gid = sb->st_gid;
2349 nsb->st_rdev = sb->st_rdev;
2350 nsb->st_atim = sb->st_atim;
2351 nsb->st_mtim = sb->st_mtim;
2352 nsb->st_ctim = sb->st_ctim;
2353 nsb->st_size = sb->st_size;
2354 nsb->st_blocks = sb->st_blocks;
2355 nsb->st_blksize = sb->st_blksize;
2356 nsb->st_flags = sb->st_flags;
2357 nsb->st_gen = sb->st_gen;
2358 nsb->st_birthtim = sb->st_birthtim;
2361 #ifndef _SYS_SYSPROTO_H_
2362 struct freebsd11_nstat_args {
2368 freebsd11_nstat(struct thread *td, struct freebsd11_nstat_args *uap)
2374 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
2378 freebsd11_cvtnstat(&sb, &nsb);
2379 return (copyout(&nsb, uap->ub, sizeof (nsb)));
2383 * NetBSD lstat. Get file status; this version does not follow links.
2385 #ifndef _SYS_SYSPROTO_H_
2386 struct freebsd11_nlstat_args {
2392 freebsd11_nlstat(struct thread *td, struct freebsd11_nlstat_args *uap)
2398 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2399 UIO_USERSPACE, &sb, NULL);
2402 freebsd11_cvtnstat(&sb, &nsb);
2403 return (copyout(&nsb, uap->ub, sizeof (nsb)));
2405 #endif /* COMPAT_FREEBSD11 */
2408 * Get configurable pathname variables.
2410 #ifndef _SYS_SYSPROTO_H_
2411 struct pathconf_args {
2417 sys_pathconf(struct thread *td, struct pathconf_args *uap)
2422 error = kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name, FOLLOW,
2425 td->td_retval[0] = value;
2429 #ifndef _SYS_SYSPROTO_H_
2430 struct lpathconf_args {
2436 sys_lpathconf(struct thread *td, struct lpathconf_args *uap)
2441 error = kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name,
2444 td->td_retval[0] = value;
2449 kern_pathconf(struct thread *td, const char *path, enum uio_seg pathseg,
2450 int name, u_long flags, long *valuep)
2452 struct nameidata nd;
2455 NDINIT(&nd, LOOKUP, LOCKSHARED | LOCKLEAF | AUDITVNODE1 | flags,
2457 if ((error = namei(&nd)) != 0)
2459 NDFREE(&nd, NDF_ONLY_PNBUF);
2461 error = VOP_PATHCONF(nd.ni_vp, name, valuep);
2467 * Return target name of a symbolic link.
2469 #ifndef _SYS_SYSPROTO_H_
2470 struct readlink_args {
2477 sys_readlink(struct thread *td, struct readlink_args *uap)
2480 return (kern_readlinkat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2481 uap->buf, UIO_USERSPACE, uap->count));
2483 #ifndef _SYS_SYSPROTO_H_
2484 struct readlinkat_args {
2492 sys_readlinkat(struct thread *td, struct readlinkat_args *uap)
2495 return (kern_readlinkat(td, uap->fd, uap->path, UIO_USERSPACE,
2496 uap->buf, UIO_USERSPACE, uap->bufsize));
2500 kern_readlinkat(struct thread *td, int fd, const char *path,
2501 enum uio_seg pathseg, char *buf, enum uio_seg bufseg, size_t count)
2504 struct nameidata nd;
2507 if (count > IOSIZE_MAX)
2510 NDINIT_AT(&nd, LOOKUP, NOFOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
2511 pathseg, path, fd, td);
2513 if ((error = namei(&nd)) != 0)
2515 NDFREE(&nd, NDF_ONLY_PNBUF);
2518 error = kern_readlink_vp(vp, buf, bufseg, count, td);
2525 * Helper function to readlink from a vnode
2528 kern_readlink_vp(struct vnode *vp, char *buf, enum uio_seg bufseg, size_t count,
2535 ASSERT_VOP_LOCKED(vp, "kern_readlink_vp(): vp not locked");
2537 error = mac_vnode_check_readlink(td->td_ucred, vp);
2541 if (vp->v_type != VLNK && (vp->v_vflag & VV_READLINK) == 0)
2544 aiov.iov_base = buf;
2545 aiov.iov_len = count;
2546 auio.uio_iov = &aiov;
2547 auio.uio_iovcnt = 1;
2548 auio.uio_offset = 0;
2549 auio.uio_rw = UIO_READ;
2550 auio.uio_segflg = bufseg;
2552 auio.uio_resid = count;
2553 error = VOP_READLINK(vp, &auio, td->td_ucred);
2554 td->td_retval[0] = count - auio.uio_resid;
2559 * Common implementation code for chflags() and fchflags().
2562 setfflags(struct thread *td, struct vnode *vp, u_long flags)
2568 /* We can't support the value matching VNOVAL. */
2569 if (flags == VNOVAL)
2570 return (EOPNOTSUPP);
2573 * Prevent non-root users from setting flags on devices. When
2574 * a device is reused, users can retain ownership of the device
2575 * if they are allowed to set flags and programs assume that
2576 * chown can't fail when done as root.
2578 if (vp->v_type == VCHR || vp->v_type == VBLK) {
2579 error = priv_check(td, PRIV_VFS_CHFLAGS_DEV);
2584 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2587 vattr.va_flags = flags;
2588 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2590 error = mac_vnode_check_setflags(td->td_ucred, vp, vattr.va_flags);
2593 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
2595 vn_finished_write(mp);
2600 * Change flags of a file given a path name.
2602 #ifndef _SYS_SYSPROTO_H_
2603 struct chflags_args {
2609 sys_chflags(struct thread *td, struct chflags_args *uap)
2612 return (kern_chflagsat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2616 #ifndef _SYS_SYSPROTO_H_
2617 struct chflagsat_args {
2625 sys_chflagsat(struct thread *td, struct chflagsat_args *uap)
2628 if ((uap->atflag & ~(AT_SYMLINK_NOFOLLOW | AT_BENEATH)) != 0)
2631 return (kern_chflagsat(td, uap->fd, uap->path, UIO_USERSPACE,
2632 uap->flags, uap->atflag));
2636 * Same as chflags() but doesn't follow symlinks.
2638 #ifndef _SYS_SYSPROTO_H_
2639 struct lchflags_args {
2645 sys_lchflags(struct thread *td, struct lchflags_args *uap)
2648 return (kern_chflagsat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2649 uap->flags, AT_SYMLINK_NOFOLLOW));
2653 kern_chflagsat(struct thread *td, int fd, const char *path,
2654 enum uio_seg pathseg, u_long flags, int atflag)
2656 struct nameidata nd;
2659 AUDIT_ARG_FFLAGS(flags);
2660 follow = (atflag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
2661 follow |= (atflag & AT_BENEATH) != 0 ? BENEATH : 0;
2662 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, pathseg, path, fd,
2663 &cap_fchflags_rights, td);
2664 if ((error = namei(&nd)) != 0)
2666 NDFREE(&nd, NDF_ONLY_PNBUF);
2667 error = setfflags(td, nd.ni_vp, flags);
2673 * Change flags of a file given a file descriptor.
2675 #ifndef _SYS_SYSPROTO_H_
2676 struct fchflags_args {
2682 sys_fchflags(struct thread *td, struct fchflags_args *uap)
2687 AUDIT_ARG_FD(uap->fd);
2688 AUDIT_ARG_FFLAGS(uap->flags);
2689 error = getvnode(td, uap->fd, &cap_fchflags_rights,
2694 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
2695 AUDIT_ARG_VNODE1(fp->f_vnode);
2696 VOP_UNLOCK(fp->f_vnode, 0);
2698 error = setfflags(td, fp->f_vnode, uap->flags);
2704 * Common implementation code for chmod(), lchmod() and fchmod().
2707 setfmode(struct thread *td, struct ucred *cred, struct vnode *vp, int mode)
2713 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2715 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2717 vattr.va_mode = mode & ALLPERMS;
2719 error = mac_vnode_check_setmode(cred, vp, vattr.va_mode);
2722 error = VOP_SETATTR(vp, &vattr, cred);
2724 vn_finished_write(mp);
2729 * Change mode of a file given path name.
2731 #ifndef _SYS_SYSPROTO_H_
2738 sys_chmod(struct thread *td, struct chmod_args *uap)
2741 return (kern_fchmodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2745 #ifndef _SYS_SYSPROTO_H_
2746 struct fchmodat_args {
2754 sys_fchmodat(struct thread *td, struct fchmodat_args *uap)
2757 if ((uap->flag & ~(AT_SYMLINK_NOFOLLOW | AT_BENEATH)) != 0)
2760 return (kern_fchmodat(td, uap->fd, uap->path, UIO_USERSPACE,
2761 uap->mode, uap->flag));
2765 * Change mode of a file given path name (don't follow links.)
2767 #ifndef _SYS_SYSPROTO_H_
2768 struct lchmod_args {
2774 sys_lchmod(struct thread *td, struct lchmod_args *uap)
2777 return (kern_fchmodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2778 uap->mode, AT_SYMLINK_NOFOLLOW));
2782 kern_fchmodat(struct thread *td, int fd, const char *path,
2783 enum uio_seg pathseg, mode_t mode, int flag)
2785 struct nameidata nd;
2788 AUDIT_ARG_MODE(mode);
2789 follow = (flag & AT_SYMLINK_NOFOLLOW) != 0 ? NOFOLLOW : FOLLOW;
2790 follow |= (flag & AT_BENEATH) != 0 ? BENEATH : 0;
2791 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, pathseg, path, fd,
2792 &cap_fchmod_rights, td);
2793 if ((error = namei(&nd)) != 0)
2795 NDFREE(&nd, NDF_ONLY_PNBUF);
2796 error = setfmode(td, td->td_ucred, nd.ni_vp, mode);
2802 * Change mode of a file given a file descriptor.
2804 #ifndef _SYS_SYSPROTO_H_
2805 struct fchmod_args {
2811 sys_fchmod(struct thread *td, struct fchmod_args *uap)
2816 AUDIT_ARG_FD(uap->fd);
2817 AUDIT_ARG_MODE(uap->mode);
2819 error = fget(td, uap->fd, &cap_fchmod_rights, &fp);
2822 error = fo_chmod(fp, uap->mode, td->td_ucred, td);
2828 * Common implementation for chown(), lchown(), and fchown()
2831 setfown(struct thread *td, struct ucred *cred, struct vnode *vp, uid_t uid,
2838 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2840 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2845 error = mac_vnode_check_setowner(cred, vp, vattr.va_uid,
2849 error = VOP_SETATTR(vp, &vattr, cred);
2851 vn_finished_write(mp);
2856 * Set ownership given a path name.
2858 #ifndef _SYS_SYSPROTO_H_
2866 sys_chown(struct thread *td, struct chown_args *uap)
2869 return (kern_fchownat(td, AT_FDCWD, uap->path, UIO_USERSPACE, uap->uid,
2873 #ifndef _SYS_SYSPROTO_H_
2874 struct fchownat_args {
2883 sys_fchownat(struct thread *td, struct fchownat_args *uap)
2886 if ((uap->flag & ~(AT_SYMLINK_NOFOLLOW | AT_BENEATH)) != 0)
2889 return (kern_fchownat(td, uap->fd, uap->path, UIO_USERSPACE, uap->uid,
2890 uap->gid, uap->flag));
2894 kern_fchownat(struct thread *td, int fd, const char *path,
2895 enum uio_seg pathseg, int uid, int gid, int flag)
2897 struct nameidata nd;
2900 AUDIT_ARG_OWNER(uid, gid);
2901 follow = (flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
2902 follow |= (flag & AT_BENEATH) != 0 ? BENEATH : 0;
2903 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, pathseg, path, fd,
2904 &cap_fchown_rights, td);
2906 if ((error = namei(&nd)) != 0)
2908 NDFREE(&nd, NDF_ONLY_PNBUF);
2909 error = setfown(td, td->td_ucred, nd.ni_vp, uid, gid);
2915 * Set ownership given a path name, do not cross symlinks.
2917 #ifndef _SYS_SYSPROTO_H_
2918 struct lchown_args {
2925 sys_lchown(struct thread *td, struct lchown_args *uap)
2928 return (kern_fchownat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2929 uap->uid, uap->gid, AT_SYMLINK_NOFOLLOW));
2933 * Set ownership given a file descriptor.
2935 #ifndef _SYS_SYSPROTO_H_
2936 struct fchown_args {
2943 sys_fchown(struct thread *td, struct fchown_args *uap)
2948 AUDIT_ARG_FD(uap->fd);
2949 AUDIT_ARG_OWNER(uap->uid, uap->gid);
2950 error = fget(td, uap->fd, &cap_fchown_rights, &fp);
2953 error = fo_chown(fp, uap->uid, uap->gid, td->td_ucred, td);
2959 * Common implementation code for utimes(), lutimes(), and futimes().
2962 getutimes(const struct timeval *usrtvp, enum uio_seg tvpseg,
2963 struct timespec *tsp)
2965 struct timeval tv[2];
2966 const struct timeval *tvp;
2969 if (usrtvp == NULL) {
2970 vfs_timestamp(&tsp[0]);
2973 if (tvpseg == UIO_SYSSPACE) {
2976 if ((error = copyin(usrtvp, tv, sizeof(tv))) != 0)
2981 if (tvp[0].tv_usec < 0 || tvp[0].tv_usec >= 1000000 ||
2982 tvp[1].tv_usec < 0 || tvp[1].tv_usec >= 1000000)
2984 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
2985 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
2991 * Common implementation code for futimens(), utimensat().
2993 #define UTIMENS_NULL 0x1
2994 #define UTIMENS_EXIT 0x2
2996 getutimens(const struct timespec *usrtsp, enum uio_seg tspseg,
2997 struct timespec *tsp, int *retflags)
2999 struct timespec tsnow;
3002 vfs_timestamp(&tsnow);
3004 if (usrtsp == NULL) {
3007 *retflags |= UTIMENS_NULL;
3010 if (tspseg == UIO_SYSSPACE) {
3013 } else if ((error = copyin(usrtsp, tsp, sizeof(*tsp) * 2)) != 0)
3015 if (tsp[0].tv_nsec == UTIME_OMIT && tsp[1].tv_nsec == UTIME_OMIT)
3016 *retflags |= UTIMENS_EXIT;
3017 if (tsp[0].tv_nsec == UTIME_NOW && tsp[1].tv_nsec == UTIME_NOW)
3018 *retflags |= UTIMENS_NULL;
3019 if (tsp[0].tv_nsec == UTIME_OMIT)
3020 tsp[0].tv_sec = VNOVAL;
3021 else if (tsp[0].tv_nsec == UTIME_NOW)
3023 else if (tsp[0].tv_nsec < 0 || tsp[0].tv_nsec >= 1000000000L)
3025 if (tsp[1].tv_nsec == UTIME_OMIT)
3026 tsp[1].tv_sec = VNOVAL;
3027 else if (tsp[1].tv_nsec == UTIME_NOW)
3029 else if (tsp[1].tv_nsec < 0 || tsp[1].tv_nsec >= 1000000000L)
3036 * Common implementation code for utimes(), lutimes(), futimes(), futimens(),
3040 setutimes(struct thread *td, struct vnode *vp, const struct timespec *ts,
3041 int numtimes, int nullflag)
3045 int error, setbirthtime;
3047 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
3049 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3051 if (numtimes < 3 && !VOP_GETATTR(vp, &vattr, td->td_ucred) &&
3052 timespeccmp(&ts[1], &vattr.va_birthtime, < ))
3055 vattr.va_atime = ts[0];
3056 vattr.va_mtime = ts[1];
3058 vattr.va_birthtime = ts[1];
3060 vattr.va_birthtime = ts[2];
3062 vattr.va_vaflags |= VA_UTIMES_NULL;
3064 error = mac_vnode_check_setutimes(td->td_ucred, vp, vattr.va_atime,
3068 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3070 vn_finished_write(mp);
3075 * Set the access and modification times of a file.
3077 #ifndef _SYS_SYSPROTO_H_
3078 struct utimes_args {
3080 struct timeval *tptr;
3084 sys_utimes(struct thread *td, struct utimes_args *uap)
3087 return (kern_utimesat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
3088 uap->tptr, UIO_USERSPACE));
3091 #ifndef _SYS_SYSPROTO_H_
3092 struct futimesat_args {
3095 const struct timeval * times;
3099 sys_futimesat(struct thread *td, struct futimesat_args *uap)
3102 return (kern_utimesat(td, uap->fd, uap->path, UIO_USERSPACE,
3103 uap->times, UIO_USERSPACE));
3107 kern_utimesat(struct thread *td, int fd, const char *path,
3108 enum uio_seg pathseg, struct timeval *tptr, enum uio_seg tptrseg)
3110 struct nameidata nd;
3111 struct timespec ts[2];
3114 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3116 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, fd,
3117 &cap_futimes_rights, td);
3119 if ((error = namei(&nd)) != 0)
3121 NDFREE(&nd, NDF_ONLY_PNBUF);
3122 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3128 * Set the access and modification times of a file.
3130 #ifndef _SYS_SYSPROTO_H_
3131 struct lutimes_args {
3133 struct timeval *tptr;
3137 sys_lutimes(struct thread *td, struct lutimes_args *uap)
3140 return (kern_lutimes(td, uap->path, UIO_USERSPACE, uap->tptr,
3145 kern_lutimes(struct thread *td, const char *path, enum uio_seg pathseg,
3146 struct timeval *tptr, enum uio_seg tptrseg)
3148 struct timespec ts[2];
3149 struct nameidata nd;
3152 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3154 NDINIT(&nd, LOOKUP, NOFOLLOW | AUDITVNODE1, pathseg, path, td);
3155 if ((error = namei(&nd)) != 0)
3157 NDFREE(&nd, NDF_ONLY_PNBUF);
3158 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3164 * Set the access and modification times of a file.
3166 #ifndef _SYS_SYSPROTO_H_
3167 struct futimes_args {
3169 struct timeval *tptr;
3173 sys_futimes(struct thread *td, struct futimes_args *uap)
3176 return (kern_futimes(td, uap->fd, uap->tptr, UIO_USERSPACE));
3180 kern_futimes(struct thread *td, int fd, struct timeval *tptr,
3181 enum uio_seg tptrseg)
3183 struct timespec ts[2];
3188 error = getutimes(tptr, tptrseg, ts);
3191 error = getvnode(td, fd, &cap_futimes_rights, &fp);
3195 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
3196 AUDIT_ARG_VNODE1(fp->f_vnode);
3197 VOP_UNLOCK(fp->f_vnode, 0);
3199 error = setutimes(td, fp->f_vnode, ts, 2, tptr == NULL);
3205 sys_futimens(struct thread *td, struct futimens_args *uap)
3208 return (kern_futimens(td, uap->fd, uap->times, UIO_USERSPACE));
3212 kern_futimens(struct thread *td, int fd, struct timespec *tptr,
3213 enum uio_seg tptrseg)
3215 struct timespec ts[2];
3220 error = getutimens(tptr, tptrseg, ts, &flags);
3223 if (flags & UTIMENS_EXIT)
3225 error = getvnode(td, fd, &cap_futimes_rights, &fp);
3229 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
3230 AUDIT_ARG_VNODE1(fp->f_vnode);
3231 VOP_UNLOCK(fp->f_vnode, 0);
3233 error = setutimes(td, fp->f_vnode, ts, 2, flags & UTIMENS_NULL);
3239 sys_utimensat(struct thread *td, struct utimensat_args *uap)
3242 return (kern_utimensat(td, uap->fd, uap->path, UIO_USERSPACE,
3243 uap->times, UIO_USERSPACE, uap->flag));
3247 kern_utimensat(struct thread *td, int fd, const char *path,
3248 enum uio_seg pathseg, struct timespec *tptr, enum uio_seg tptrseg,
3251 struct nameidata nd;
3252 struct timespec ts[2];
3255 if ((flag & ~(AT_SYMLINK_NOFOLLOW | AT_BENEATH)) != 0)
3258 if ((error = getutimens(tptr, tptrseg, ts, &flags)) != 0)
3260 NDINIT_ATRIGHTS(&nd, LOOKUP, ((flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW :
3261 FOLLOW) | ((flag & AT_BENEATH) != 0 ? BENEATH : 0) | AUDITVNODE1,
3262 pathseg, path, fd, &cap_futimes_rights, td);
3263 if ((error = namei(&nd)) != 0)
3266 * We are allowed to call namei() regardless of 2xUTIME_OMIT.
3268 * "If both tv_nsec fields are UTIME_OMIT... EACCESS may be detected."
3269 * "Search permission is denied by a component of the path prefix."
3271 NDFREE(&nd, NDF_ONLY_PNBUF);
3272 if ((flags & UTIMENS_EXIT) == 0)
3273 error = setutimes(td, nd.ni_vp, ts, 2, flags & UTIMENS_NULL);
3279 * Truncate a file given its path name.
3281 #ifndef _SYS_SYSPROTO_H_
3282 struct truncate_args {
3289 sys_truncate(struct thread *td, struct truncate_args *uap)
3292 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3296 kern_truncate(struct thread *td, const char *path, enum uio_seg pathseg,
3303 struct nameidata nd;
3308 NDINIT(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, td);
3309 if ((error = namei(&nd)) != 0)
3312 rl_cookie = vn_rangelock_wlock(vp, 0, OFF_MAX);
3313 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0) {
3314 vn_rangelock_unlock(vp, rl_cookie);
3318 NDFREE(&nd, NDF_ONLY_PNBUF);
3319 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3320 if (vp->v_type == VDIR)
3323 else if ((error = mac_vnode_check_write(td->td_ucred, NOCRED, vp))) {
3326 else if ((error = vn_writechk(vp)) == 0 &&
3327 (error = VOP_ACCESS(vp, VWRITE, td->td_ucred, td)) == 0) {
3329 vattr.va_size = length;
3330 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3333 vn_finished_write(mp);
3334 vn_rangelock_unlock(vp, rl_cookie);
3339 #if defined(COMPAT_43)
3341 * Truncate a file given its path name.
3343 #ifndef _SYS_SYSPROTO_H_
3344 struct otruncate_args {
3350 otruncate(struct thread *td, struct otruncate_args *uap)
3353 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3355 #endif /* COMPAT_43 */
3357 #if defined(COMPAT_FREEBSD6)
3358 /* Versions with the pad argument */
3360 freebsd6_truncate(struct thread *td, struct freebsd6_truncate_args *uap)
3363 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3367 freebsd6_ftruncate(struct thread *td, struct freebsd6_ftruncate_args *uap)
3370 return (kern_ftruncate(td, uap->fd, uap->length));
3375 kern_fsync(struct thread *td, int fd, bool fullsync)
3380 int error, lock_flags;
3383 error = getvnode(td, fd, &cap_fsync_rights, &fp);
3389 /* XXXKIB: compete outstanding aio writes */;
3391 error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
3394 if (MNT_SHARED_WRITES(mp) ||
3395 ((mp == NULL) && MNT_SHARED_WRITES(vp->v_mount))) {
3396 lock_flags = LK_SHARED;
3398 lock_flags = LK_EXCLUSIVE;
3400 vn_lock(vp, lock_flags | LK_RETRY);
3401 AUDIT_ARG_VNODE1(vp);
3402 if (vp->v_object != NULL) {
3403 VM_OBJECT_WLOCK(vp->v_object);
3404 vm_object_page_clean(vp->v_object, 0, 0, 0);
3405 VM_OBJECT_WUNLOCK(vp->v_object);
3407 error = fullsync ? VOP_FSYNC(vp, MNT_WAIT, td) : VOP_FDATASYNC(vp, td);
3409 vn_finished_write(mp);
3416 * Sync an open file.
3418 #ifndef _SYS_SYSPROTO_H_
3424 sys_fsync(struct thread *td, struct fsync_args *uap)
3427 return (kern_fsync(td, uap->fd, true));
3431 sys_fdatasync(struct thread *td, struct fdatasync_args *uap)
3434 return (kern_fsync(td, uap->fd, false));
3438 * Rename files. Source and destination must either both be directories, or
3439 * both not be directories. If target is a directory, it must be empty.
3441 #ifndef _SYS_SYSPROTO_H_
3442 struct rename_args {
3448 sys_rename(struct thread *td, struct rename_args *uap)
3451 return (kern_renameat(td, AT_FDCWD, uap->from, AT_FDCWD,
3452 uap->to, UIO_USERSPACE));
3455 #ifndef _SYS_SYSPROTO_H_
3456 struct renameat_args {
3464 sys_renameat(struct thread *td, struct renameat_args *uap)
3467 return (kern_renameat(td, uap->oldfd, uap->old, uap->newfd, uap->new,
3472 kern_renameat(struct thread *td, int oldfd, const char *old, int newfd,
3473 const char *new, enum uio_seg pathseg)
3475 struct mount *mp = NULL;
3476 struct vnode *tvp, *fvp, *tdvp;
3477 struct nameidata fromnd, tond;
3483 NDINIT_ATRIGHTS(&fromnd, DELETE, LOCKPARENT | LOCKLEAF | SAVESTART |
3484 AUDITVNODE1, pathseg, old, oldfd,
3485 &cap_renameat_source_rights, td);
3487 NDINIT_ATRIGHTS(&fromnd, DELETE, WANTPARENT | SAVESTART | AUDITVNODE1,
3488 pathseg, old, oldfd,
3489 &cap_renameat_source_rights, td);
3492 if ((error = namei(&fromnd)) != 0)
3495 error = mac_vnode_check_rename_from(td->td_ucred, fromnd.ni_dvp,
3496 fromnd.ni_vp, &fromnd.ni_cnd);
3497 VOP_UNLOCK(fromnd.ni_dvp, 0);
3498 if (fromnd.ni_dvp != fromnd.ni_vp)
3499 VOP_UNLOCK(fromnd.ni_vp, 0);
3502 NDINIT_ATRIGHTS(&tond, RENAME, LOCKPARENT | LOCKLEAF | NOCACHE |
3503 SAVESTART | AUDITVNODE2, pathseg, new, newfd,
3504 &cap_renameat_target_rights, td);
3505 if (fromnd.ni_vp->v_type == VDIR)
3506 tond.ni_cnd.cn_flags |= WILLBEDIR;
3507 if ((error = namei(&tond)) != 0) {
3508 /* Translate error code for rename("dir1", "dir2/."). */
3509 if (error == EISDIR && fvp->v_type == VDIR)
3511 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3512 vrele(fromnd.ni_dvp);
3518 error = vn_start_write(fvp, &mp, V_NOWAIT);
3520 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3521 NDFREE(&tond, NDF_ONLY_PNBUF);
3528 vrele(fromnd.ni_dvp);
3530 vrele(tond.ni_startdir);
3531 if (fromnd.ni_startdir != NULL)
3532 vrele(fromnd.ni_startdir);
3533 error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH);
3539 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
3542 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
3547 if (newfd != AT_FDCWD && (tond.ni_resflags & NIRES_ABS) == 0) {
3549 * If the target already exists we require CAP_UNLINKAT
3550 * from 'newfd', when newfd was used for the lookup.
3552 error = cap_check(&tond.ni_filecaps.fc_rights,
3553 &cap_unlinkat_rights);
3564 * If the source is the same as the destination (that is, if they
3565 * are links to the same vnode), then there is nothing to do.
3571 error = mac_vnode_check_rename_to(td->td_ucred, tdvp,
3572 tond.ni_vp, fromnd.ni_dvp == tdvp, &tond.ni_cnd);
3576 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
3577 tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
3578 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3579 NDFREE(&tond, NDF_ONLY_PNBUF);
3581 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3582 NDFREE(&tond, NDF_ONLY_PNBUF);
3589 vrele(fromnd.ni_dvp);
3592 vrele(tond.ni_startdir);
3593 vn_finished_write(mp);
3595 if (fromnd.ni_startdir)
3596 vrele(fromnd.ni_startdir);
3603 * Make a directory file.
3605 #ifndef _SYS_SYSPROTO_H_
3612 sys_mkdir(struct thread *td, struct mkdir_args *uap)
3615 return (kern_mkdirat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
3619 #ifndef _SYS_SYSPROTO_H_
3620 struct mkdirat_args {
3627 sys_mkdirat(struct thread *td, struct mkdirat_args *uap)
3630 return (kern_mkdirat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode));
3634 kern_mkdirat(struct thread *td, int fd, const char *path, enum uio_seg segflg,
3640 struct nameidata nd;
3643 AUDIT_ARG_MODE(mode);
3646 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
3647 NOCACHE, segflg, path, fd, &cap_mkdirat_rights,
3649 nd.ni_cnd.cn_flags |= WILLBEDIR;
3650 if ((error = namei(&nd)) != 0)
3654 NDFREE(&nd, NDF_ONLY_PNBUF);
3656 * XXX namei called with LOCKPARENT but not LOCKLEAF has
3657 * the strange behaviour of leaving the vnode unlocked
3658 * if the target is the same vnode as the parent.
3660 if (vp == nd.ni_dvp)
3667 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3668 NDFREE(&nd, NDF_ONLY_PNBUF);
3670 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3675 vattr.va_type = VDIR;
3676 vattr.va_mode = (mode & ACCESSPERMS) &~ td->td_proc->p_fd->fd_cmask;
3678 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
3683 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
3687 NDFREE(&nd, NDF_ONLY_PNBUF);
3691 vn_finished_write(mp);
3696 * Remove a directory file.
3698 #ifndef _SYS_SYSPROTO_H_
3704 sys_rmdir(struct thread *td, struct rmdir_args *uap)
3707 return (kern_rmdirat(td, AT_FDCWD, uap->path, UIO_USERSPACE, 0));
3711 kern_rmdirat(struct thread *td, int fd, const char *path, enum uio_seg pathseg,
3716 struct nameidata nd;
3721 NDINIT_ATRIGHTS(&nd, DELETE, LOCKPARENT | LOCKLEAF | AUDITVNODE1 |
3722 ((flag & AT_BENEATH) != 0 ? BENEATH : 0),
3723 pathseg, path, fd, &cap_unlinkat_rights, td);
3724 if ((error = namei(&nd)) != 0)
3727 if (vp->v_type != VDIR) {
3732 * No rmdir "." please.
3734 if (nd.ni_dvp == vp) {
3739 * The root of a mounted filesystem cannot be deleted.
3741 if (vp->v_vflag & VV_ROOT) {
3746 error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp,
3751 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3752 NDFREE(&nd, NDF_ONLY_PNBUF);
3754 if (nd.ni_dvp == vp)
3758 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3762 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_UNLINK);
3763 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
3764 vn_finished_write(mp);
3766 NDFREE(&nd, NDF_ONLY_PNBUF);
3768 if (nd.ni_dvp == vp)
3775 #if defined(COMPAT_43) || defined(COMPAT_FREEBSD11)
3777 freebsd11_kern_getdirentries(struct thread *td, int fd, char *ubuf, u_int count,
3778 long *basep, void (*func)(struct freebsd11_dirent *))
3780 struct freebsd11_dirent dstdp;
3781 struct dirent *dp, *edp;
3784 ssize_t resid, ucount;
3787 /* XXX arbitrary sanity limit on `count'. */
3788 count = min(count, 64 * 1024);
3790 dirbuf = malloc(count, M_TEMP, M_WAITOK);
3792 error = kern_getdirentries(td, fd, dirbuf, count, &base, &resid,
3800 for (dp = (struct dirent *)dirbuf,
3801 edp = (struct dirent *)&dirbuf[count - resid];
3802 ucount < count && dp < edp; ) {
3803 if (dp->d_reclen == 0)
3805 MPASS(dp->d_reclen >= _GENERIC_DIRLEN(0));
3806 if (dp->d_namlen >= sizeof(dstdp.d_name))
3808 dstdp.d_type = dp->d_type;
3809 dstdp.d_namlen = dp->d_namlen;
3810 dstdp.d_fileno = dp->d_fileno; /* truncate */
3811 if (dstdp.d_fileno != dp->d_fileno) {
3812 switch (ino64_trunc_error) {
3820 dstdp.d_fileno = UINT32_MAX;
3824 dstdp.d_reclen = sizeof(dstdp) - sizeof(dstdp.d_name) +
3825 ((dp->d_namlen + 1 + 3) &~ 3);
3826 bcopy(dp->d_name, dstdp.d_name, dstdp.d_namlen);
3827 bzero(dstdp.d_name + dstdp.d_namlen,
3828 dstdp.d_reclen - offsetof(struct freebsd11_dirent, d_name) -
3830 MPASS(dstdp.d_reclen <= dp->d_reclen);
3831 MPASS(ucount + dstdp.d_reclen <= count);
3834 error = copyout(&dstdp, ubuf + ucount, dstdp.d_reclen);
3837 dp = (struct dirent *)((char *)dp + dp->d_reclen);
3838 ucount += dstdp.d_reclen;
3842 free(dirbuf, M_TEMP);
3844 td->td_retval[0] = ucount;
3851 ogetdirentries_cvt(struct freebsd11_dirent *dp)
3853 #if (BYTE_ORDER == LITTLE_ENDIAN)
3855 * The expected low byte of dp->d_namlen is our dp->d_type.
3856 * The high MBZ byte of dp->d_namlen is our dp->d_namlen.
3858 dp->d_type = dp->d_namlen;
3862 * The dp->d_type is the high byte of the expected dp->d_namlen,
3863 * so must be zero'ed.
3870 * Read a block of directory entries in a filesystem independent format.
3872 #ifndef _SYS_SYSPROTO_H_
3873 struct ogetdirentries_args {
3881 ogetdirentries(struct thread *td, struct ogetdirentries_args *uap)
3886 error = kern_ogetdirentries(td, uap, &loff);
3888 error = copyout(&loff, uap->basep, sizeof(long));
3893 kern_ogetdirentries(struct thread *td, struct ogetdirentries_args *uap,
3899 /* XXX arbitrary sanity limit on `count'. */
3900 if (uap->count > 64 * 1024)
3903 error = freebsd11_kern_getdirentries(td, uap->fd, uap->buf, uap->count,
3904 &base, ogetdirentries_cvt);
3906 if (error == 0 && uap->basep != NULL)
3907 error = copyout(&base, uap->basep, sizeof(long));
3911 #endif /* COMPAT_43 */
3913 #if defined(COMPAT_FREEBSD11)
3914 #ifndef _SYS_SYSPROTO_H_
3915 struct freebsd11_getdirentries_args {
3923 freebsd11_getdirentries(struct thread *td,
3924 struct freebsd11_getdirentries_args *uap)
3929 error = freebsd11_kern_getdirentries(td, uap->fd, uap->buf, uap->count,
3932 if (error == 0 && uap->basep != NULL)
3933 error = copyout(&base, uap->basep, sizeof(long));
3938 freebsd11_getdents(struct thread *td, struct freebsd11_getdents_args *uap)
3940 struct freebsd11_getdirentries_args ap;
3944 ap.count = uap->count;
3946 return (freebsd11_getdirentries(td, &ap));
3948 #endif /* COMPAT_FREEBSD11 */
3951 * Read a block of directory entries in a filesystem independent format.
3954 sys_getdirentries(struct thread *td, struct getdirentries_args *uap)
3959 error = kern_getdirentries(td, uap->fd, uap->buf, uap->count, &base,
3960 NULL, UIO_USERSPACE);
3963 if (uap->basep != NULL)
3964 error = copyout(&base, uap->basep, sizeof(off_t));
3969 kern_getdirentries(struct thread *td, int fd, char *buf, size_t count,
3970 off_t *basep, ssize_t *residp, enum uio_seg bufseg)
3981 if (count > IOSIZE_MAX)
3983 auio.uio_resid = count;
3984 error = getvnode(td, fd, &cap_read_rights, &fp);
3987 if ((fp->f_flag & FREAD) == 0) {
3992 foffset = foffset_lock(fp, 0);
3994 if (vp->v_type != VDIR) {
3998 aiov.iov_base = buf;
3999 aiov.iov_len = count;
4000 auio.uio_iov = &aiov;
4001 auio.uio_iovcnt = 1;
4002 auio.uio_rw = UIO_READ;
4003 auio.uio_segflg = bufseg;
4005 vn_lock(vp, LK_SHARED | LK_RETRY);
4006 AUDIT_ARG_VNODE1(vp);
4007 loff = auio.uio_offset = foffset;
4009 error = mac_vnode_check_readdir(td->td_ucred, vp);
4012 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL,
4014 foffset = auio.uio_offset;
4019 if (count == auio.uio_resid &&
4020 (vp->v_vflag & VV_ROOT) &&
4021 (vp->v_mount->mnt_flag & MNT_UNION)) {
4022 struct vnode *tvp = vp;
4024 vp = vp->v_mount->mnt_vnodecovered;
4035 *residp = auio.uio_resid;
4036 td->td_retval[0] = count - auio.uio_resid;
4038 foffset_unlock(fp, foffset, 0);
4044 * Set the mode mask for creation of filesystem nodes.
4046 #ifndef _SYS_SYSPROTO_H_
4052 sys_umask(struct thread *td, struct umask_args *uap)
4054 struct filedesc *fdp;
4056 fdp = td->td_proc->p_fd;
4057 FILEDESC_XLOCK(fdp);
4058 td->td_retval[0] = fdp->fd_cmask;
4059 fdp->fd_cmask = uap->newmask & ALLPERMS;
4060 FILEDESC_XUNLOCK(fdp);
4065 * Void all references to file by ripping underlying filesystem away from
4068 #ifndef _SYS_SYSPROTO_H_
4069 struct revoke_args {
4074 sys_revoke(struct thread *td, struct revoke_args *uap)
4078 struct nameidata nd;
4081 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
4083 if ((error = namei(&nd)) != 0)
4086 NDFREE(&nd, NDF_ONLY_PNBUF);
4087 if (vp->v_type != VCHR || vp->v_rdev == NULL) {
4092 error = mac_vnode_check_revoke(td->td_ucred, vp);
4096 error = VOP_GETATTR(vp, &vattr, td->td_ucred);
4099 if (td->td_ucred->cr_uid != vattr.va_uid) {
4100 error = priv_check(td, PRIV_VFS_ADMIN);
4105 VOP_REVOKE(vp, REVOKEALL);
4112 * Convert a user file descriptor to a kernel file entry and check that, if it
4113 * is a capability, the correct rights are present. A reference on the file
4114 * entry is held upon returning.
4117 getvnode(struct thread *td, int fd, cap_rights_t *rightsp, struct file **fpp)
4122 error = fget_unlocked(td->td_proc->p_fd, fd, rightsp, &fp, NULL);
4127 * The file could be not of the vnode type, or it may be not
4128 * yet fully initialized, in which case the f_vnode pointer
4129 * may be set, but f_ops is still badfileops. E.g.,
4130 * devfs_open() transiently create such situation to
4131 * facilitate csw d_fdopen().
4133 * Dupfdopen() handling in kern_openat() installs the
4134 * half-baked file into the process descriptor table, allowing
4135 * other thread to dereference it. Guard against the race by
4138 if (fp->f_vnode == NULL || fp->f_ops == &badfileops) {
4148 * Get an (NFS) file handle.
4150 #ifndef _SYS_SYSPROTO_H_
4151 struct lgetfh_args {
4157 sys_lgetfh(struct thread *td, struct lgetfh_args *uap)
4160 return (kern_getfhat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->fname,
4161 UIO_USERSPACE, uap->fhp));
4164 #ifndef _SYS_SYSPROTO_H_
4171 sys_getfh(struct thread *td, struct getfh_args *uap)
4174 return (kern_getfhat(td, 0, AT_FDCWD, uap->fname, UIO_USERSPACE,
4179 * syscall for the rpc.lockd to use to translate an open descriptor into
4180 * a NFS file handle.
4182 * warning: do not remove the priv_check() call or this becomes one giant
4185 #ifndef _SYS_SYSPROTO_H_
4186 struct getfhat_args {
4194 sys_getfhat(struct thread *td, struct getfhat_args *uap)
4197 if ((uap->flags & ~(AT_SYMLINK_NOFOLLOW | AT_BENEATH)) != 0)
4199 return (kern_getfhat(td, uap->flags, uap->fd, uap->path, UIO_USERSPACE,
4204 kern_getfhat(struct thread *td, int flags, int fd, const char *path,
4205 enum uio_seg pathseg, fhandle_t *fhp)
4207 struct nameidata nd;
4212 error = priv_check(td, PRIV_VFS_GETFH);
4215 NDINIT_AT(&nd, LOOKUP, ((flags & AT_SYMLINK_NOFOLLOW) != 0 ? NOFOLLOW :
4216 FOLLOW) | ((flags & AT_BENEATH) != 0 ? BENEATH : 0) | LOCKLEAF |
4217 AUDITVNODE1, pathseg, path, fd, td);
4221 NDFREE(&nd, NDF_ONLY_PNBUF);
4223 bzero(&fh, sizeof(fh));
4224 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
4225 error = VOP_VPTOFH(vp, &fh.fh_fid);
4228 error = copyout(&fh, fhp, sizeof (fh));
4232 #ifndef _SYS_SYSPROTO_H_
4233 struct fhlink_args {
4239 sys_fhlink(struct thread *td, struct fhlink_args *uap)
4242 return (kern_fhlinkat(td, AT_FDCWD, uap->to, UIO_USERSPACE, uap->fhp));
4245 #ifndef _SYS_SYSPROTO_H_
4246 struct fhlinkat_args {
4253 sys_fhlinkat(struct thread *td, struct fhlinkat_args *uap)
4256 return (kern_fhlinkat(td, uap->tofd, uap->to, UIO_USERSPACE, uap->fhp));
4260 kern_fhlinkat(struct thread *td, int fd, const char *path,
4261 enum uio_seg pathseg, fhandle_t *fhp)
4268 error = priv_check(td, PRIV_VFS_GETFH);
4271 error = copyin(fhp, &fh, sizeof(fh));
4276 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4278 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_SHARED, &vp);
4283 } while ((error = kern_linkat_vp(td, vp, fd, path, pathseg)) == EAGAIN);
4287 #ifndef _SYS_SYSPROTO_H_
4288 struct fhreadlink_args {
4295 sys_fhreadlink(struct thread *td, struct fhreadlink_args *uap)
4302 error = priv_check(td, PRIV_VFS_GETFH);
4305 if (uap->bufsize > IOSIZE_MAX)
4307 error = copyin(uap->fhp, &fh, sizeof(fh));
4310 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4312 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_SHARED, &vp);
4316 error = kern_readlink_vp(vp, uap->buf, UIO_USERSPACE, uap->bufsize, td);
4322 * syscall for the rpc.lockd to use to translate a NFS file handle into an
4325 * warning: do not remove the priv_check() call or this becomes one giant
4328 #ifndef _SYS_SYSPROTO_H_
4329 struct fhopen_args {
4330 const struct fhandle *u_fhp;
4335 sys_fhopen(struct thread *td, struct fhopen_args *uap)
4344 error = priv_check(td, PRIV_VFS_FHOPEN);
4348 fmode = FFLAGS(uap->flags);
4349 /* why not allow a non-read/write open for our lockd? */
4350 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
4352 error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
4355 /* find the mount point */
4356 mp = vfs_busyfs(&fhp.fh_fsid);
4359 /* now give me my vnode, it gets returned to me locked */
4360 error = VFS_FHTOVP(mp, &fhp.fh_fid, LK_EXCLUSIVE, &vp);
4365 error = falloc_noinstall(td, &fp);
4371 * An extra reference on `fp' has been held for us by
4372 * falloc_noinstall().
4378 error = vn_open_vnode(vp, fmode, td->td_ucred, td, fp);
4380 KASSERT(fp->f_ops == &badfileops,
4381 ("VOP_OPEN in fhopen() set f_ops"));
4382 KASSERT(td->td_dupfd < 0,
4383 ("fhopen() encountered fdopen()"));
4393 finit(fp, (fmode & FMASK) | (fp->f_flag & FHASLOCK), DTYPE_VNODE, vp,
4396 if ((fmode & O_TRUNC) != 0) {
4397 error = fo_truncate(fp, 0, td->td_ucred, td);
4402 error = finstall(td, fp, &indx, fmode, NULL);
4405 td->td_retval[0] = indx;
4410 * Stat an (NFS) file handle.
4412 #ifndef _SYS_SYSPROTO_H_
4413 struct fhstat_args {
4414 struct fhandle *u_fhp;
4419 sys_fhstat(struct thread *td, struct fhstat_args *uap)
4425 error = copyin(uap->u_fhp, &fh, sizeof(fh));
4428 error = kern_fhstat(td, fh, &sb);
4430 error = copyout(&sb, uap->sb, sizeof(sb));
4435 kern_fhstat(struct thread *td, struct fhandle fh, struct stat *sb)
4441 error = priv_check(td, PRIV_VFS_FHSTAT);
4444 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4446 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_EXCLUSIVE, &vp);
4450 error = vn_stat(vp, sb, td->td_ucred, NOCRED, td);
4456 * Implement fstatfs() for (NFS) file handles.
4458 #ifndef _SYS_SYSPROTO_H_
4459 struct fhstatfs_args {
4460 struct fhandle *u_fhp;
4465 sys_fhstatfs(struct thread *td, struct fhstatfs_args *uap)
4471 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4474 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
4475 error = kern_fhstatfs(td, fh, sfp);
4477 error = copyout(sfp, uap->buf, sizeof(*sfp));
4478 free(sfp, M_STATFS);
4483 kern_fhstatfs(struct thread *td, fhandle_t fh, struct statfs *buf)
4490 error = priv_check(td, PRIV_VFS_FHSTATFS);
4493 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4495 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_EXCLUSIVE, &vp);
4501 error = prison_canseemount(td->td_ucred, mp);
4505 error = mac_mount_check_stat(td->td_ucred, mp);
4510 * Set these in case the underlying filesystem fails to do so.
4513 sp->f_version = STATFS_VERSION;
4514 sp->f_namemax = NAME_MAX;
4515 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
4516 error = VFS_STATFS(mp, sp);
4525 kern_posix_fallocate(struct thread *td, int fd, off_t offset, off_t len)
4530 off_t olen, ooffset;
4533 int audited_vnode1 = 0;
4537 if (offset < 0 || len <= 0)
4539 /* Check for wrap. */
4540 if (offset > OFF_MAX - len)
4543 error = fget(td, fd, &cap_pwrite_rights, &fp);
4546 AUDIT_ARG_FILE(td->td_proc, fp);
4547 if ((fp->f_ops->fo_flags & DFLAG_SEEKABLE) == 0) {
4551 if ((fp->f_flag & FWRITE) == 0) {
4555 if (fp->f_type != DTYPE_VNODE) {
4560 if (vp->v_type != VREG) {
4565 /* Allocating blocks may take a long time, so iterate. */
4572 error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
4575 error = vn_lock(vp, LK_EXCLUSIVE);
4577 vn_finished_write(mp);
4581 if (!audited_vnode1) {
4582 AUDIT_ARG_VNODE1(vp);
4587 error = mac_vnode_check_write(td->td_ucred, fp->f_cred, vp);
4590 error = VOP_ALLOCATE(vp, &offset, &len);
4592 vn_finished_write(mp);
4594 if (olen + ooffset != offset + len) {
4595 panic("offset + len changed from %jx/%jx to %jx/%jx",
4596 ooffset, olen, offset, len);
4598 if (error != 0 || len == 0)
4600 KASSERT(olen > len, ("Iteration did not make progress?"));
4609 sys_posix_fallocate(struct thread *td, struct posix_fallocate_args *uap)
4613 error = kern_posix_fallocate(td, uap->fd, uap->offset, uap->len);
4614 return (kern_posix_error(td, error));
4618 * Unlike madvise(2), we do not make a best effort to remember every
4619 * possible caching hint. Instead, we remember the last setting with
4620 * the exception that we will allow POSIX_FADV_NORMAL to adjust the
4621 * region of any current setting.
4624 kern_posix_fadvise(struct thread *td, int fd, off_t offset, off_t len,
4627 struct fadvise_info *fa, *new;
4633 if (offset < 0 || len < 0 || offset > OFF_MAX - len)
4635 AUDIT_ARG_VALUE(advice);
4637 case POSIX_FADV_SEQUENTIAL:
4638 case POSIX_FADV_RANDOM:
4639 case POSIX_FADV_NOREUSE:
4640 new = malloc(sizeof(*fa), M_FADVISE, M_WAITOK);
4642 case POSIX_FADV_NORMAL:
4643 case POSIX_FADV_WILLNEED:
4644 case POSIX_FADV_DONTNEED:
4650 /* XXX: CAP_POSIX_FADVISE? */
4652 error = fget(td, fd, &cap_no_rights, &fp);
4655 AUDIT_ARG_FILE(td->td_proc, fp);
4656 if ((fp->f_ops->fo_flags & DFLAG_SEEKABLE) == 0) {
4660 if (fp->f_type != DTYPE_VNODE) {
4665 if (vp->v_type != VREG) {
4672 end = offset + len - 1;
4674 case POSIX_FADV_SEQUENTIAL:
4675 case POSIX_FADV_RANDOM:
4676 case POSIX_FADV_NOREUSE:
4678 * Try to merge any existing non-standard region with
4679 * this new region if possible, otherwise create a new
4680 * non-standard region for this request.
4682 mtx_pool_lock(mtxpool_sleep, fp);
4684 if (fa != NULL && fa->fa_advice == advice &&
4685 ((fa->fa_start <= end && fa->fa_end >= offset) ||
4686 (end != OFF_MAX && fa->fa_start == end + 1) ||
4687 (fa->fa_end != OFF_MAX && fa->fa_end + 1 == offset))) {
4688 if (offset < fa->fa_start)
4689 fa->fa_start = offset;
4690 if (end > fa->fa_end)
4693 new->fa_advice = advice;
4694 new->fa_start = offset;
4699 mtx_pool_unlock(mtxpool_sleep, fp);
4701 case POSIX_FADV_NORMAL:
4703 * If a the "normal" region overlaps with an existing
4704 * non-standard region, trim or remove the
4705 * non-standard region.
4707 mtx_pool_lock(mtxpool_sleep, fp);
4710 if (offset <= fa->fa_start && end >= fa->fa_end) {
4712 fp->f_advice = NULL;
4713 } else if (offset <= fa->fa_start &&
4714 end >= fa->fa_start)
4715 fa->fa_start = end + 1;
4716 else if (offset <= fa->fa_end && end >= fa->fa_end)
4717 fa->fa_end = offset - 1;
4718 else if (offset >= fa->fa_start && end <= fa->fa_end) {
4720 * If the "normal" region is a middle
4721 * portion of the existing
4722 * non-standard region, just remove
4723 * the whole thing rather than picking
4724 * one side or the other to
4728 fp->f_advice = NULL;
4731 mtx_pool_unlock(mtxpool_sleep, fp);
4733 case POSIX_FADV_WILLNEED:
4734 case POSIX_FADV_DONTNEED:
4735 error = VOP_ADVISE(vp, offset, end, advice);
4741 free(new, M_FADVISE);
4746 sys_posix_fadvise(struct thread *td, struct posix_fadvise_args *uap)
4750 error = kern_posix_fadvise(td, uap->fd, uap->offset, uap->len,
4752 return (kern_posix_error(td, error));