2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
6 * (c) UNIX System Laboratories, Inc.
7 * All or some portions of this file are derived from material licensed
8 * to the University of California by American Telephone and Telegraph
9 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10 * the permission of UNIX System Laboratories, Inc.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * @(#)vfs_syscalls.c 8.13 (Berkeley) 4/15/94
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
42 #include "opt_capsicum.h"
43 #include "opt_ktrace.h"
45 #include <sys/param.h>
46 #include <sys/systm.h>
49 #include <sys/capsicum.h>
51 #include <sys/sysent.h>
52 #include <sys/malloc.h>
53 #include <sys/mount.h>
54 #include <sys/mutex.h>
55 #include <sys/sysproto.h>
56 #include <sys/namei.h>
57 #include <sys/filedesc.h>
58 #include <sys/kernel.h>
59 #include <sys/fcntl.h>
61 #include <sys/filio.h>
62 #include <sys/limits.h>
63 #include <sys/linker.h>
64 #include <sys/rwlock.h>
68 #include <sys/unistd.h>
69 #include <sys/vnode.h>
72 #include <sys/dirent.h>
74 #include <sys/syscallsubr.h>
75 #include <sys/sysctl.h>
77 #include <sys/ktrace.h>
80 #include <machine/stdarg.h>
82 #include <security/audit/audit.h>
83 #include <security/mac/mac_framework.h>
86 #include <vm/vm_object.h>
87 #include <vm/vm_page.h>
90 #include <fs/devfs/devfs.h>
92 #include <ufs/ufs/quota.h>
94 MALLOC_DEFINE(M_FADVISE, "fadvise", "posix_fadvise(2) information");
96 static int kern_chflagsat(struct thread *td, int fd, const char *path,
97 enum uio_seg pathseg, u_long flags, int atflag);
98 static int setfflags(struct thread *td, struct vnode *, u_long);
99 static int getutimes(const struct timeval *, enum uio_seg, struct timespec *);
100 static int getutimens(const struct timespec *, enum uio_seg,
101 struct timespec *, int *);
102 static int setutimes(struct thread *td, struct vnode *,
103 const struct timespec *, int, int);
104 static int vn_access(struct vnode *vp, int user_flags, struct ucred *cred,
106 static int kern_fhlinkat(struct thread *td, int fd, const char *path,
107 enum uio_seg pathseg, fhandle_t *fhp);
108 static int kern_getfhat(struct thread *td, int flags, int fd,
109 const char *path, enum uio_seg pathseg, fhandle_t *fhp);
110 static int kern_readlink_vp(struct vnode *vp, char *buf, enum uio_seg bufseg,
111 size_t count, struct thread *td);
112 static int kern_linkat_vp(struct thread *td, struct vnode *vp, int fd,
113 const char *path, enum uio_seg segflag);
116 at2cnpflags(u_int at_flags, u_int mask)
120 MPASS((at_flags & (AT_SYMLINK_FOLLOW | AT_SYMLINK_NOFOLLOW)) !=
121 (AT_SYMLINK_FOLLOW | AT_SYMLINK_NOFOLLOW));
125 if ((at_flags & AT_BENEATH) != 0)
127 if ((at_flags & AT_RESOLVE_BENEATH) != 0)
129 if ((at_flags & AT_SYMLINK_FOLLOW) != 0)
131 /* NOFOLLOW is pseudo flag */
132 if ((mask & AT_SYMLINK_NOFOLLOW) != 0) {
133 res |= (at_flags & AT_SYMLINK_NOFOLLOW) != 0 ? NOFOLLOW :
140 kern_sync(struct thread *td)
142 struct mount *mp, *nmp;
145 mtx_lock(&mountlist_mtx);
146 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
147 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK)) {
148 nmp = TAILQ_NEXT(mp, mnt_list);
151 if ((mp->mnt_flag & MNT_RDONLY) == 0 &&
152 vn_start_write(NULL, &mp, V_NOWAIT) == 0) {
153 save = curthread_pflags_set(TDP_SYNCIO);
154 vfs_periodic(mp, MNT_NOWAIT);
155 VFS_SYNC(mp, MNT_NOWAIT);
156 curthread_pflags_restore(save);
157 vn_finished_write(mp);
159 mtx_lock(&mountlist_mtx);
160 nmp = TAILQ_NEXT(mp, mnt_list);
163 mtx_unlock(&mountlist_mtx);
168 * Sync each mounted filesystem.
170 #ifndef _SYS_SYSPROTO_H_
177 sys_sync(struct thread *td, struct sync_args *uap)
180 return (kern_sync(td));
184 * Change filesystem quotas.
186 #ifndef _SYS_SYSPROTO_H_
187 struct quotactl_args {
195 sys_quotactl(struct thread *td, struct quotactl_args *uap)
201 AUDIT_ARG_CMD(uap->cmd);
202 AUDIT_ARG_UID(uap->uid);
203 if (!prison_allow(td->td_ucred, PR_ALLOW_QUOTAS))
205 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
207 if ((error = namei(&nd)) != 0)
209 NDFREE(&nd, NDF_ONLY_PNBUF);
210 mp = nd.ni_vp->v_mount;
213 error = vfs_busy(mp, 0);
218 error = VFS_QUOTACTL(mp, uap->cmd, uap->uid, uap->arg);
221 * Since quota on operation typically needs to open quota
222 * file, the Q_QUOTAON handler needs to unbusy the mount point
223 * before calling into namei. Otherwise, unmount might be
224 * started between two vfs_busy() invocations (first is our,
225 * second is from mount point cross-walk code in lookup()),
228 * Require that Q_QUOTAON handles the vfs_busy() reference on
229 * its own, always returning with ubusied mount point.
231 if ((uap->cmd >> SUBCMDSHIFT) != Q_QUOTAON &&
232 (uap->cmd >> SUBCMDSHIFT) != Q_QUOTAOFF)
239 * Used by statfs conversion routines to scale the block size up if
240 * necessary so that all of the block counts are <= 'max_size'. Note
241 * that 'max_size' should be a bitmask, i.e. 2^n - 1 for some non-zero
245 statfs_scale_blocks(struct statfs *sf, long max_size)
250 KASSERT(powerof2(max_size + 1), ("%s: invalid max_size", __func__));
253 * Attempt to scale the block counts to give a more accurate
254 * overview to userland of the ratio of free space to used
255 * space. To do this, find the largest block count and compute
256 * a divisor that lets it fit into a signed integer <= max_size.
258 if (sf->f_bavail < 0)
259 count = -sf->f_bavail;
261 count = sf->f_bavail;
262 count = MAX(sf->f_blocks, MAX(sf->f_bfree, count));
263 if (count <= max_size)
266 count >>= flsl(max_size);
273 sf->f_bsize <<= shift;
274 sf->f_blocks >>= shift;
275 sf->f_bfree >>= shift;
276 sf->f_bavail >>= shift;
280 kern_do_statfs(struct thread *td, struct mount *mp, struct statfs *buf)
286 error = vfs_busy(mp, 0);
291 error = mac_mount_check_stat(td->td_ucred, mp);
295 error = VFS_STATFS(mp, buf);
298 if (priv_check_cred_vfs_generation(td->td_ucred)) {
299 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
300 prison_enforce_statfs(td->td_ucred, mp, buf);
308 * Get filesystem statistics.
310 #ifndef _SYS_SYSPROTO_H_
317 sys_statfs(struct thread *td, struct statfs_args *uap)
322 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
323 error = kern_statfs(td, uap->path, UIO_USERSPACE, sfp);
325 error = copyout(sfp, uap->buf, sizeof(struct statfs));
331 kern_statfs(struct thread *td, const char *path, enum uio_seg pathseg,
338 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
343 mp = nd.ni_vp->v_mount;
345 NDFREE(&nd, NDF_ONLY_PNBUF);
347 return (kern_do_statfs(td, mp, buf));
351 * Get filesystem statistics.
353 #ifndef _SYS_SYSPROTO_H_
354 struct fstatfs_args {
360 sys_fstatfs(struct thread *td, struct fstatfs_args *uap)
365 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
366 error = kern_fstatfs(td, uap->fd, sfp);
368 error = copyout(sfp, uap->buf, sizeof(struct statfs));
374 kern_fstatfs(struct thread *td, int fd, struct statfs *buf)
382 error = getvnode(td, fd, &cap_fstatfs_rights, &fp);
386 vn_lock(vp, LK_SHARED | LK_RETRY);
388 AUDIT_ARG_VNODE1(vp);
395 return (kern_do_statfs(td, mp, buf));
399 * Get statistics on all filesystems.
401 #ifndef _SYS_SYSPROTO_H_
402 struct getfsstat_args {
409 sys_getfsstat(struct thread *td, struct getfsstat_args *uap)
414 if (uap->bufsize < 0 || uap->bufsize > SIZE_MAX)
416 error = kern_getfsstat(td, &uap->buf, uap->bufsize, &count,
417 UIO_USERSPACE, uap->mode);
419 td->td_retval[0] = count;
424 * If (bufsize > 0 && bufseg == UIO_SYSSPACE)
425 * The caller is responsible for freeing memory which will be allocated
429 kern_getfsstat(struct thread *td, struct statfs **buf, size_t bufsize,
430 size_t *countp, enum uio_seg bufseg, int mode)
432 struct mount *mp, *nmp;
433 struct statfs *sfsp, *sp, *sptmp, *tofree;
434 size_t count, maxcount;
442 if (bufseg == UIO_SYSSPACE)
447 maxcount = bufsize / sizeof(struct statfs);
451 } else if (bufseg == UIO_USERSPACE) {
454 } else /* if (bufseg == UIO_SYSSPACE) */ {
456 mtx_lock(&mountlist_mtx);
457 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
460 mtx_unlock(&mountlist_mtx);
461 if (maxcount > count)
463 tofree = sfsp = *buf = malloc(maxcount * sizeof(struct statfs),
470 * If there is no target buffer they only want the count.
472 * This could be TAILQ_FOREACH but it is open-coded to match the original
476 mtx_lock(&mountlist_mtx);
477 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
478 if (prison_canseemount(td->td_ucred, mp) != 0) {
479 nmp = TAILQ_NEXT(mp, mnt_list);
483 if (mac_mount_check_stat(td->td_ucred, mp) != 0) {
484 nmp = TAILQ_NEXT(mp, mnt_list);
489 nmp = TAILQ_NEXT(mp, mnt_list);
491 mtx_unlock(&mountlist_mtx);
497 * They want the entire thing.
499 * Short-circuit the corner case of no room for anything, avoids
506 mtx_lock(&mountlist_mtx);
507 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
508 if (prison_canseemount(td->td_ucred, mp) != 0) {
509 nmp = TAILQ_NEXT(mp, mnt_list);
513 if (mac_mount_check_stat(td->td_ucred, mp) != 0) {
514 nmp = TAILQ_NEXT(mp, mnt_list);
518 if (mode == MNT_WAIT) {
519 if (vfs_busy(mp, MBF_MNTLSTLOCK) != 0) {
521 * If vfs_busy() failed, and MBF_NOWAIT
522 * wasn't passed, then the mp is gone.
523 * Furthermore, because of MBF_MNTLSTLOCK,
524 * the mountlist_mtx was dropped. We have
525 * no other choice than to start over.
527 mtx_unlock(&mountlist_mtx);
528 free(tofree, M_STATFS);
532 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK) != 0) {
533 nmp = TAILQ_NEXT(mp, mnt_list);
539 * If MNT_NOWAIT is specified, do not refresh
542 if (mode != MNT_NOWAIT) {
543 error = VFS_STATFS(mp, sp);
545 mtx_lock(&mountlist_mtx);
546 nmp = TAILQ_NEXT(mp, mnt_list);
551 if (priv_check_cred_vfs_generation(td->td_ucred)) {
552 sptmp = malloc(sizeof(struct statfs), M_STATFS,
555 sptmp->f_fsid.val[0] = sptmp->f_fsid.val[1] = 0;
556 prison_enforce_statfs(td->td_ucred, mp, sptmp);
560 if (bufseg == UIO_SYSSPACE) {
561 bcopy(sp, sfsp, sizeof(*sp));
562 free(sptmp, M_STATFS);
563 } else /* if (bufseg == UIO_USERSPACE) */ {
564 error = copyout(sp, sfsp, sizeof(*sp));
565 free(sptmp, M_STATFS);
574 if (count == maxcount) {
579 mtx_lock(&mountlist_mtx);
580 nmp = TAILQ_NEXT(mp, mnt_list);
583 mtx_unlock(&mountlist_mtx);
589 #ifdef COMPAT_FREEBSD4
591 * Get old format filesystem statistics.
593 static void freebsd4_cvtstatfs(struct statfs *, struct ostatfs *);
595 #ifndef _SYS_SYSPROTO_H_
596 struct freebsd4_statfs_args {
602 freebsd4_statfs(struct thread *td, struct freebsd4_statfs_args *uap)
608 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
609 error = kern_statfs(td, uap->path, UIO_USERSPACE, sfp);
611 freebsd4_cvtstatfs(sfp, &osb);
612 error = copyout(&osb, uap->buf, sizeof(osb));
619 * Get filesystem statistics.
621 #ifndef _SYS_SYSPROTO_H_
622 struct freebsd4_fstatfs_args {
628 freebsd4_fstatfs(struct thread *td, struct freebsd4_fstatfs_args *uap)
634 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
635 error = kern_fstatfs(td, uap->fd, sfp);
637 freebsd4_cvtstatfs(sfp, &osb);
638 error = copyout(&osb, uap->buf, sizeof(osb));
645 * Get statistics on all filesystems.
647 #ifndef _SYS_SYSPROTO_H_
648 struct freebsd4_getfsstat_args {
655 freebsd4_getfsstat(struct thread *td, struct freebsd4_getfsstat_args *uap)
657 struct statfs *buf, *sp;
662 if (uap->bufsize < 0)
664 count = uap->bufsize / sizeof(struct ostatfs);
665 if (count > SIZE_MAX / sizeof(struct statfs))
667 size = count * sizeof(struct statfs);
668 error = kern_getfsstat(td, &buf, size, &count, UIO_SYSSPACE,
671 td->td_retval[0] = count;
674 while (count != 0 && error == 0) {
675 freebsd4_cvtstatfs(sp, &osb);
676 error = copyout(&osb, uap->buf, sizeof(osb));
687 * Implement fstatfs() for (NFS) file handles.
689 #ifndef _SYS_SYSPROTO_H_
690 struct freebsd4_fhstatfs_args {
691 struct fhandle *u_fhp;
696 freebsd4_fhstatfs(struct thread *td, struct freebsd4_fhstatfs_args *uap)
703 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
706 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
707 error = kern_fhstatfs(td, fh, sfp);
709 freebsd4_cvtstatfs(sfp, &osb);
710 error = copyout(&osb, uap->buf, sizeof(osb));
717 * Convert a new format statfs structure to an old format statfs structure.
720 freebsd4_cvtstatfs(struct statfs *nsp, struct ostatfs *osp)
723 statfs_scale_blocks(nsp, LONG_MAX);
724 bzero(osp, sizeof(*osp));
725 osp->f_bsize = nsp->f_bsize;
726 osp->f_iosize = MIN(nsp->f_iosize, LONG_MAX);
727 osp->f_blocks = nsp->f_blocks;
728 osp->f_bfree = nsp->f_bfree;
729 osp->f_bavail = nsp->f_bavail;
730 osp->f_files = MIN(nsp->f_files, LONG_MAX);
731 osp->f_ffree = MIN(nsp->f_ffree, LONG_MAX);
732 osp->f_owner = nsp->f_owner;
733 osp->f_type = nsp->f_type;
734 osp->f_flags = nsp->f_flags;
735 osp->f_syncwrites = MIN(nsp->f_syncwrites, LONG_MAX);
736 osp->f_asyncwrites = MIN(nsp->f_asyncwrites, LONG_MAX);
737 osp->f_syncreads = MIN(nsp->f_syncreads, LONG_MAX);
738 osp->f_asyncreads = MIN(nsp->f_asyncreads, LONG_MAX);
739 strlcpy(osp->f_fstypename, nsp->f_fstypename,
740 MIN(MFSNAMELEN, OMFSNAMELEN));
741 strlcpy(osp->f_mntonname, nsp->f_mntonname,
742 MIN(MNAMELEN, OMNAMELEN));
743 strlcpy(osp->f_mntfromname, nsp->f_mntfromname,
744 MIN(MNAMELEN, OMNAMELEN));
745 osp->f_fsid = nsp->f_fsid;
747 #endif /* COMPAT_FREEBSD4 */
749 #if defined(COMPAT_FREEBSD11)
751 * Get old format filesystem statistics.
753 static void freebsd11_cvtstatfs(struct statfs *, struct freebsd11_statfs *);
756 freebsd11_statfs(struct thread *td, struct freebsd11_statfs_args *uap)
758 struct freebsd11_statfs osb;
762 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
763 error = kern_statfs(td, uap->path, UIO_USERSPACE, sfp);
765 freebsd11_cvtstatfs(sfp, &osb);
766 error = copyout(&osb, uap->buf, sizeof(osb));
773 * Get filesystem statistics.
776 freebsd11_fstatfs(struct thread *td, struct freebsd11_fstatfs_args *uap)
778 struct freebsd11_statfs osb;
782 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
783 error = kern_fstatfs(td, uap->fd, sfp);
785 freebsd11_cvtstatfs(sfp, &osb);
786 error = copyout(&osb, uap->buf, sizeof(osb));
793 * Get statistics on all filesystems.
796 freebsd11_getfsstat(struct thread *td, struct freebsd11_getfsstat_args *uap)
798 struct freebsd11_statfs osb;
799 struct statfs *buf, *sp;
803 count = uap->bufsize / sizeof(struct ostatfs);
804 size = count * sizeof(struct statfs);
805 error = kern_getfsstat(td, &buf, size, &count, UIO_SYSSPACE,
808 td->td_retval[0] = count;
811 while (count > 0 && error == 0) {
812 freebsd11_cvtstatfs(sp, &osb);
813 error = copyout(&osb, uap->buf, sizeof(osb));
824 * Implement fstatfs() for (NFS) file handles.
827 freebsd11_fhstatfs(struct thread *td, struct freebsd11_fhstatfs_args *uap)
829 struct freebsd11_statfs osb;
834 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
837 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
838 error = kern_fhstatfs(td, fh, sfp);
840 freebsd11_cvtstatfs(sfp, &osb);
841 error = copyout(&osb, uap->buf, sizeof(osb));
848 * Convert a new format statfs structure to an old format statfs structure.
851 freebsd11_cvtstatfs(struct statfs *nsp, struct freebsd11_statfs *osp)
854 bzero(osp, sizeof(*osp));
855 osp->f_version = FREEBSD11_STATFS_VERSION;
856 osp->f_type = nsp->f_type;
857 osp->f_flags = nsp->f_flags;
858 osp->f_bsize = nsp->f_bsize;
859 osp->f_iosize = nsp->f_iosize;
860 osp->f_blocks = nsp->f_blocks;
861 osp->f_bfree = nsp->f_bfree;
862 osp->f_bavail = nsp->f_bavail;
863 osp->f_files = nsp->f_files;
864 osp->f_ffree = nsp->f_ffree;
865 osp->f_syncwrites = nsp->f_syncwrites;
866 osp->f_asyncwrites = nsp->f_asyncwrites;
867 osp->f_syncreads = nsp->f_syncreads;
868 osp->f_asyncreads = nsp->f_asyncreads;
869 osp->f_namemax = nsp->f_namemax;
870 osp->f_owner = nsp->f_owner;
871 osp->f_fsid = nsp->f_fsid;
872 strlcpy(osp->f_fstypename, nsp->f_fstypename,
873 MIN(MFSNAMELEN, sizeof(osp->f_fstypename)));
874 strlcpy(osp->f_mntonname, nsp->f_mntonname,
875 MIN(MNAMELEN, sizeof(osp->f_mntonname)));
876 strlcpy(osp->f_mntfromname, nsp->f_mntfromname,
877 MIN(MNAMELEN, sizeof(osp->f_mntfromname)));
879 #endif /* COMPAT_FREEBSD11 */
882 * Change current working directory to a given file descriptor.
884 #ifndef _SYS_SYSPROTO_H_
890 sys_fchdir(struct thread *td, struct fchdir_args *uap)
892 struct vnode *vp, *tdp;
897 AUDIT_ARG_FD(uap->fd);
898 error = getvnode(td, uap->fd, &cap_fchdir_rights,
905 vn_lock(vp, LK_SHARED | LK_RETRY);
906 AUDIT_ARG_VNODE1(vp);
907 error = change_dir(vp, td);
908 while (!error && (mp = vp->v_mountedhere) != NULL) {
911 error = VFS_ROOT(mp, LK_SHARED, &tdp);
928 * Change current working directory (``.'').
930 #ifndef _SYS_SYSPROTO_H_
936 sys_chdir(struct thread *td, struct chdir_args *uap)
939 return (kern_chdir(td, uap->path, UIO_USERSPACE));
943 kern_chdir(struct thread *td, const char *path, enum uio_seg pathseg)
948 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
950 if ((error = namei(&nd)) != 0)
952 if ((error = change_dir(nd.ni_vp, td)) != 0) {
954 NDFREE(&nd, NDF_ONLY_PNBUF);
957 VOP_UNLOCK(nd.ni_vp);
958 NDFREE(&nd, NDF_ONLY_PNBUF);
959 pwd_chdir(td, nd.ni_vp);
964 * Change notion of root (``/'') directory.
966 #ifndef _SYS_SYSPROTO_H_
972 sys_chroot(struct thread *td, struct chroot_args *uap)
977 error = priv_check(td, PRIV_VFS_CHROOT);
980 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
981 UIO_USERSPACE, uap->path, td);
985 error = change_dir(nd.ni_vp, td);
989 error = mac_vnode_check_chroot(td->td_ucred, nd.ni_vp);
993 VOP_UNLOCK(nd.ni_vp);
994 error = pwd_chroot(td, nd.ni_vp);
996 NDFREE(&nd, NDF_ONLY_PNBUF);
1001 NDFREE(&nd, NDF_ONLY_PNBUF);
1006 * Common routine for chroot and chdir. Callers must provide a locked vnode
1010 change_dir(struct vnode *vp, struct thread *td)
1016 ASSERT_VOP_LOCKED(vp, "change_dir(): vp not locked");
1017 if (vp->v_type != VDIR)
1020 error = mac_vnode_check_chdir(td->td_ucred, vp);
1024 return (VOP_ACCESS(vp, VEXEC, td->td_ucred, td));
1027 static __inline void
1028 flags_to_rights(int flags, cap_rights_t *rightsp)
1031 if (flags & O_EXEC) {
1032 cap_rights_set_one(rightsp, CAP_FEXECVE);
1034 switch ((flags & O_ACCMODE)) {
1036 cap_rights_set_one(rightsp, CAP_READ);
1039 cap_rights_set_one(rightsp, CAP_READ);
1042 cap_rights_set_one(rightsp, CAP_WRITE);
1043 if (!(flags & (O_APPEND | O_TRUNC)))
1044 cap_rights_set_one(rightsp, CAP_SEEK);
1049 if (flags & O_CREAT)
1050 cap_rights_set_one(rightsp, CAP_CREATE);
1052 if (flags & O_TRUNC)
1053 cap_rights_set_one(rightsp, CAP_FTRUNCATE);
1055 if (flags & (O_SYNC | O_FSYNC))
1056 cap_rights_set_one(rightsp, CAP_FSYNC);
1058 if (flags & (O_EXLOCK | O_SHLOCK))
1059 cap_rights_set_one(rightsp, CAP_FLOCK);
1063 * Check permissions, allocate an open file structure, and call the device
1064 * open routine if any.
1066 #ifndef _SYS_SYSPROTO_H_
1074 sys_open(struct thread *td, struct open_args *uap)
1077 return (kern_openat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1078 uap->flags, uap->mode));
1081 #ifndef _SYS_SYSPROTO_H_
1082 struct openat_args {
1090 sys_openat(struct thread *td, struct openat_args *uap)
1093 AUDIT_ARG_FD(uap->fd);
1094 return (kern_openat(td, uap->fd, uap->path, UIO_USERSPACE, uap->flag,
1099 kern_openat(struct thread *td, int fd, const char *path, enum uio_seg pathseg,
1100 int flags, int mode)
1102 struct proc *p = td->td_proc;
1103 struct filedesc *fdp = p->p_fd;
1106 struct nameidata nd;
1107 cap_rights_t rights;
1108 int cmode, error, indx;
1112 AUDIT_ARG_FFLAGS(flags);
1113 AUDIT_ARG_MODE(mode);
1114 cap_rights_init_one(&rights, CAP_LOOKUP);
1115 flags_to_rights(flags, &rights);
1117 * Only one of the O_EXEC, O_RDONLY, O_WRONLY and O_RDWR flags
1120 if (flags & O_EXEC) {
1121 if (flags & O_ACCMODE)
1123 } else if ((flags & O_ACCMODE) == O_ACCMODE) {
1126 flags = FFLAGS(flags);
1130 * Allocate a file structure. The descriptor to reference it
1131 * is allocated and set by finstall() below.
1133 error = falloc_noinstall(td, &fp);
1137 * An extra reference on `fp' has been held for us by
1138 * falloc_noinstall().
1140 /* Set the flags early so the finit in devfs can pick them up. */
1141 fp->f_flag = flags & FMASK;
1142 cmode = ((mode & ~fdp->fd_cmask) & ALLPERMS) & ~S_ISTXT;
1143 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, fd,
1145 td->td_dupfd = -1; /* XXX check for fdopen */
1146 error = vn_open(&nd, &flags, cmode, fp);
1149 * If the vn_open replaced the method vector, something
1150 * wonderous happened deep below and we just pass it up
1151 * pretending we know what we do.
1153 if (error == ENXIO && fp->f_ops != &badfileops)
1157 * Handle special fdopen() case. bleh.
1159 * Don't do this for relative (capability) lookups; we don't
1160 * understand exactly what would happen, and we don't think
1161 * that it ever should.
1163 if ((nd.ni_resflags & NIRES_STRICTREL) == 0 &&
1164 (error == ENODEV || error == ENXIO) &&
1165 td->td_dupfd >= 0) {
1166 error = dupfdopen(td, fdp, td->td_dupfd, flags, error,
1175 NDFREE(&nd, NDF_ONLY_PNBUF);
1179 * Store the vnode, for any f_type. Typically, the vnode use
1180 * count is decremented by direct call to vn_closefile() for
1181 * files that switched type in the cdevsw fdopen() method.
1185 * If the file wasn't claimed by devfs bind it to the normal
1186 * vnode operations here.
1188 if (fp->f_ops == &badfileops) {
1189 KASSERT(vp->v_type != VFIFO, ("Unexpected fifo."));
1190 finit_vnode(fp, flags, NULL, &vnops);
1194 if (flags & O_TRUNC) {
1195 error = fo_truncate(fp, 0, td->td_ucred, td);
1201 * If we haven't already installed the FD (for dupfdopen), do so now.
1204 struct filecaps *fcaps;
1207 if ((nd.ni_resflags & NIRES_STRICTREL) != 0)
1208 fcaps = &nd.ni_filecaps;
1212 error = finstall(td, fp, &indx, flags, fcaps);
1213 /* On success finstall() consumes fcaps. */
1215 filecaps_free(&nd.ni_filecaps);
1219 filecaps_free(&nd.ni_filecaps);
1223 * Release our private reference, leaving the one associated with
1224 * the descriptor table intact.
1227 td->td_retval[0] = indx;
1230 KASSERT(indx == -1, ("indx=%d, should be -1", indx));
1239 #ifndef _SYS_SYSPROTO_H_
1240 struct ocreat_args {
1246 ocreat(struct thread *td, struct ocreat_args *uap)
1249 return (kern_openat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1250 O_WRONLY | O_CREAT | O_TRUNC, uap->mode));
1252 #endif /* COMPAT_43 */
1255 * Create a special file.
1257 #ifndef _SYS_SYSPROTO_H_
1258 struct mknodat_args {
1266 sys_mknodat(struct thread *td, struct mknodat_args *uap)
1269 return (kern_mknodat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode,
1273 #if defined(COMPAT_FREEBSD11)
1275 freebsd11_mknod(struct thread *td,
1276 struct freebsd11_mknod_args *uap)
1279 return (kern_mknodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1280 uap->mode, uap->dev));
1284 freebsd11_mknodat(struct thread *td,
1285 struct freebsd11_mknodat_args *uap)
1288 return (kern_mknodat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode,
1291 #endif /* COMPAT_FREEBSD11 */
1294 kern_mknodat(struct thread *td, int fd, const char *path, enum uio_seg pathseg,
1295 int mode, dev_t dev)
1300 struct nameidata nd;
1301 int error, whiteout = 0;
1303 AUDIT_ARG_MODE(mode);
1305 switch (mode & S_IFMT) {
1308 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
1309 if (error == 0 && dev == VNOVAL)
1313 error = priv_check(td, PRIV_VFS_MKNOD_WHT);
1317 return (kern_mkfifoat(td, fd, path, pathseg, mode));
1327 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
1328 NOCACHE, pathseg, path, fd, &cap_mknodat_rights,
1330 if ((error = namei(&nd)) != 0)
1334 NDFREE(&nd, NDF_ONLY_PNBUF);
1335 if (vp == nd.ni_dvp)
1343 vattr.va_mode = (mode & ALLPERMS) &
1344 ~td->td_proc->p_fd->fd_cmask;
1345 vattr.va_rdev = dev;
1348 switch (mode & S_IFMT) {
1350 vattr.va_type = VCHR;
1353 vattr.va_type = VBLK;
1359 panic("kern_mknod: invalid mode");
1362 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1363 NDFREE(&nd, NDF_ONLY_PNBUF);
1365 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1370 if (error == 0 && !whiteout)
1371 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp,
1372 &nd.ni_cnd, &vattr);
1376 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, CREATE);
1378 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp,
1379 &nd.ni_cnd, &vattr);
1384 NDFREE(&nd, NDF_ONLY_PNBUF);
1386 vn_finished_write(mp);
1391 * Create a named pipe.
1393 #ifndef _SYS_SYSPROTO_H_
1394 struct mkfifo_args {
1400 sys_mkfifo(struct thread *td, struct mkfifo_args *uap)
1403 return (kern_mkfifoat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1407 #ifndef _SYS_SYSPROTO_H_
1408 struct mkfifoat_args {
1415 sys_mkfifoat(struct thread *td, struct mkfifoat_args *uap)
1418 return (kern_mkfifoat(td, uap->fd, uap->path, UIO_USERSPACE,
1423 kern_mkfifoat(struct thread *td, int fd, const char *path,
1424 enum uio_seg pathseg, int mode)
1428 struct nameidata nd;
1431 AUDIT_ARG_MODE(mode);
1434 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
1435 NOCACHE, pathseg, path, fd, &cap_mkfifoat_rights,
1437 if ((error = namei(&nd)) != 0)
1439 if (nd.ni_vp != NULL) {
1440 NDFREE(&nd, NDF_ONLY_PNBUF);
1441 if (nd.ni_vp == nd.ni_dvp)
1448 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1449 NDFREE(&nd, NDF_ONLY_PNBUF);
1451 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1456 vattr.va_type = VFIFO;
1457 vattr.va_mode = (mode & ALLPERMS) & ~td->td_proc->p_fd->fd_cmask;
1459 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1464 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
1471 vn_finished_write(mp);
1472 NDFREE(&nd, NDF_ONLY_PNBUF);
1477 * Make a hard file link.
1479 #ifndef _SYS_SYSPROTO_H_
1486 sys_link(struct thread *td, struct link_args *uap)
1489 return (kern_linkat(td, AT_FDCWD, AT_FDCWD, uap->path, uap->link,
1490 UIO_USERSPACE, FOLLOW));
1493 #ifndef _SYS_SYSPROTO_H_
1494 struct linkat_args {
1503 sys_linkat(struct thread *td, struct linkat_args *uap)
1508 if ((flag & ~(AT_SYMLINK_FOLLOW | AT_BENEATH |
1509 AT_RESOLVE_BENEATH)) != 0)
1512 return (kern_linkat(td, uap->fd1, uap->fd2, uap->path1, uap->path2,
1513 UIO_USERSPACE, at2cnpflags(flag, AT_SYMLINK_FOLLOW | AT_BENEATH |
1514 AT_RESOLVE_BENEATH)));
1517 int hardlink_check_uid = 0;
1518 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
1519 &hardlink_check_uid, 0,
1520 "Unprivileged processes cannot create hard links to files owned by other "
1522 static int hardlink_check_gid = 0;
1523 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
1524 &hardlink_check_gid, 0,
1525 "Unprivileged processes cannot create hard links to files owned by other "
1529 can_hardlink(struct vnode *vp, struct ucred *cred)
1534 if (!hardlink_check_uid && !hardlink_check_gid)
1537 error = VOP_GETATTR(vp, &va, cred);
1541 if (hardlink_check_uid && cred->cr_uid != va.va_uid) {
1542 error = priv_check_cred(cred, PRIV_VFS_LINK);
1547 if (hardlink_check_gid && !groupmember(va.va_gid, cred)) {
1548 error = priv_check_cred(cred, PRIV_VFS_LINK);
1557 kern_linkat(struct thread *td, int fd1, int fd2, const char *path1,
1558 const char *path2, enum uio_seg segflag, int follow)
1560 struct nameidata nd;
1565 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, segflag,
1566 path1, fd1, &cap_linkat_source_rights, td);
1567 if ((error = namei(&nd)) != 0)
1569 NDFREE(&nd, NDF_ONLY_PNBUF);
1570 error = kern_linkat_vp(td, nd.ni_vp, fd2, path2, segflag);
1571 } while (error == EAGAIN);
1576 kern_linkat_vp(struct thread *td, struct vnode *vp, int fd, const char *path,
1577 enum uio_seg segflag)
1579 struct nameidata nd;
1583 if (vp->v_type == VDIR) {
1585 return (EPERM); /* POSIX */
1587 NDINIT_ATRIGHTS(&nd, CREATE,
1588 LOCKPARENT | SAVENAME | AUDITVNODE2 | NOCACHE, segflag, path, fd,
1589 &cap_linkat_target_rights, td);
1590 if ((error = namei(&nd)) == 0) {
1591 if (nd.ni_vp != NULL) {
1592 NDFREE(&nd, NDF_ONLY_PNBUF);
1593 if (nd.ni_dvp == nd.ni_vp)
1600 } else if (nd.ni_dvp->v_mount != vp->v_mount) {
1602 * Cross-device link. No need to recheck
1603 * vp->v_type, since it cannot change, except
1606 NDFREE(&nd, NDF_ONLY_PNBUF);
1610 } else if ((error = vn_lock(vp, LK_EXCLUSIVE)) == 0) {
1611 error = can_hardlink(vp, td->td_ucred);
1614 error = mac_vnode_check_link(td->td_ucred,
1615 nd.ni_dvp, vp, &nd.ni_cnd);
1620 NDFREE(&nd, NDF_ONLY_PNBUF);
1623 error = vn_start_write(vp, &mp, V_NOWAIT);
1627 NDFREE(&nd, NDF_ONLY_PNBUF);
1628 error = vn_start_write(NULL, &mp,
1634 error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
1637 vn_finished_write(mp);
1638 NDFREE(&nd, NDF_ONLY_PNBUF);
1641 NDFREE(&nd, NDF_ONLY_PNBUF);
1651 * Make a symbolic link.
1653 #ifndef _SYS_SYSPROTO_H_
1654 struct symlink_args {
1660 sys_symlink(struct thread *td, struct symlink_args *uap)
1663 return (kern_symlinkat(td, uap->path, AT_FDCWD, uap->link,
1667 #ifndef _SYS_SYSPROTO_H_
1668 struct symlinkat_args {
1675 sys_symlinkat(struct thread *td, struct symlinkat_args *uap)
1678 return (kern_symlinkat(td, uap->path1, uap->fd, uap->path2,
1683 kern_symlinkat(struct thread *td, const char *path1, int fd, const char *path2,
1684 enum uio_seg segflg)
1688 const char *syspath;
1690 struct nameidata nd;
1693 if (segflg == UIO_SYSSPACE) {
1696 tmppath = uma_zalloc(namei_zone, M_WAITOK);
1697 if ((error = copyinstr(path1, tmppath, MAXPATHLEN, NULL)) != 0)
1701 AUDIT_ARG_TEXT(syspath);
1704 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
1705 NOCACHE, segflg, path2, fd, &cap_symlinkat_rights,
1707 if ((error = namei(&nd)) != 0)
1710 NDFREE(&nd, NDF_ONLY_PNBUF);
1711 if (nd.ni_vp == nd.ni_dvp)
1719 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1720 NDFREE(&nd, NDF_ONLY_PNBUF);
1722 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1727 vattr.va_mode = ACCESSPERMS &~ td->td_proc->p_fd->fd_cmask;
1729 vattr.va_type = VLNK;
1730 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1735 error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr, syspath);
1741 NDFREE(&nd, NDF_ONLY_PNBUF);
1743 vn_finished_write(mp);
1745 if (segflg != UIO_SYSSPACE)
1746 uma_zfree(namei_zone, tmppath);
1751 * Delete a whiteout from the filesystem.
1753 #ifndef _SYS_SYSPROTO_H_
1754 struct undelete_args {
1759 sys_undelete(struct thread *td, struct undelete_args *uap)
1762 struct nameidata nd;
1767 NDINIT(&nd, DELETE, LOCKPARENT | DOWHITEOUT | AUDITVNODE1,
1768 UIO_USERSPACE, uap->path, td);
1773 if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & ISWHITEOUT)) {
1774 NDFREE(&nd, NDF_ONLY_PNBUF);
1775 if (nd.ni_vp == nd.ni_dvp)
1783 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1784 NDFREE(&nd, NDF_ONLY_PNBUF);
1786 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1790 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, DELETE);
1791 NDFREE(&nd, NDF_ONLY_PNBUF);
1793 vn_finished_write(mp);
1798 * Delete a name from the filesystem.
1800 #ifndef _SYS_SYSPROTO_H_
1801 struct unlink_args {
1806 sys_unlink(struct thread *td, struct unlink_args *uap)
1809 return (kern_funlinkat(td, AT_FDCWD, uap->path, FD_NONE, UIO_USERSPACE,
1814 kern_funlinkat_ex(struct thread *td, int dfd, const char *path, int fd,
1815 int flag, enum uio_seg pathseg, ino_t oldinum)
1818 if ((flag & ~AT_REMOVEDIR) != 0)
1821 if ((flag & AT_REMOVEDIR) != 0)
1822 return (kern_frmdirat(td, dfd, path, fd, UIO_USERSPACE, 0));
1824 return (kern_funlinkat(td, dfd, path, fd, UIO_USERSPACE, 0, 0));
1827 #ifndef _SYS_SYSPROTO_H_
1828 struct unlinkat_args {
1835 sys_unlinkat(struct thread *td, struct unlinkat_args *uap)
1838 return (kern_funlinkat_ex(td, uap->fd, uap->path, FD_NONE, uap->flag,
1842 #ifndef _SYS_SYSPROTO_H_
1843 struct funlinkat_args {
1851 sys_funlinkat(struct thread *td, struct funlinkat_args *uap)
1854 return (kern_funlinkat_ex(td, uap->dfd, uap->path, uap->fd, uap->flag,
1859 kern_funlinkat(struct thread *td, int dfd, const char *path, int fd,
1860 enum uio_seg pathseg, int flag, ino_t oldinum)
1865 struct nameidata nd;
1870 if (fd != FD_NONE) {
1871 error = getvnode(td, fd, &cap_no_rights, &fp);
1878 NDINIT_ATRIGHTS(&nd, DELETE, LOCKPARENT | LOCKLEAF | AUDITVNODE1 |
1879 at2cnpflags(flag, AT_BENEATH | AT_RESOLVE_BENEATH),
1880 pathseg, path, dfd, &cap_unlinkat_rights, td);
1881 if ((error = namei(&nd)) != 0) {
1882 if (error == EINVAL)
1887 if (vp->v_type == VDIR && oldinum == 0) {
1888 error = EPERM; /* POSIX */
1889 } else if (oldinum != 0 &&
1890 ((error = VOP_STAT(vp, &sb, td->td_ucred, NOCRED, td)) == 0) &&
1891 sb.st_ino != oldinum) {
1892 error = EIDRM; /* Identifier removed */
1893 } else if (fp != NULL && fp->f_vnode != vp) {
1894 if (VN_IS_DOOMED(fp->f_vnode))
1900 * The root of a mounted filesystem cannot be deleted.
1902 * XXX: can this only be a VDIR case?
1904 if (vp->v_vflag & VV_ROOT)
1908 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1909 NDFREE(&nd, NDF_ONLY_PNBUF);
1911 if (vp == nd.ni_dvp)
1915 if ((error = vn_start_write(NULL, &mp,
1916 V_XSLEEP | PCATCH)) != 0) {
1922 error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp,
1927 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_UNLINK);
1928 error = VOP_REMOVE(nd.ni_dvp, vp, &nd.ni_cnd);
1932 vn_finished_write(mp);
1934 NDFREE(&nd, NDF_ONLY_PNBUF);
1936 if (vp == nd.ni_dvp)
1947 * Reposition read/write file offset.
1949 #ifndef _SYS_SYSPROTO_H_
1958 sys_lseek(struct thread *td, struct lseek_args *uap)
1961 return (kern_lseek(td, uap->fd, uap->offset, uap->whence));
1965 kern_lseek(struct thread *td, int fd, off_t offset, int whence)
1971 error = fget(td, fd, &cap_seek_rights, &fp);
1974 error = (fp->f_ops->fo_flags & DFLAG_SEEKABLE) != 0 ?
1975 fo_seek(fp, offset, whence, td) : ESPIPE;
1980 #if defined(COMPAT_43)
1982 * Reposition read/write file offset.
1984 #ifndef _SYS_SYSPROTO_H_
1985 struct olseek_args {
1992 olseek(struct thread *td, struct olseek_args *uap)
1995 return (kern_lseek(td, uap->fd, uap->offset, uap->whence));
1997 #endif /* COMPAT_43 */
1999 #if defined(COMPAT_FREEBSD6)
2000 /* Version with the 'pad' argument */
2002 freebsd6_lseek(struct thread *td, struct freebsd6_lseek_args *uap)
2005 return (kern_lseek(td, uap->fd, uap->offset, uap->whence));
2010 * Check access permissions using passed credentials.
2013 vn_access(struct vnode *vp, int user_flags, struct ucred *cred,
2019 /* Flags == 0 means only check for existence. */
2020 if (user_flags == 0)
2024 if (user_flags & R_OK)
2026 if (user_flags & W_OK)
2028 if (user_flags & X_OK)
2031 error = mac_vnode_check_access(cred, vp, accmode);
2035 if ((accmode & VWRITE) == 0 || (error = vn_writechk(vp)) == 0)
2036 error = VOP_ACCESS(vp, accmode, cred, td);
2041 * Check access permissions using "real" credentials.
2043 #ifndef _SYS_SYSPROTO_H_
2044 struct access_args {
2050 sys_access(struct thread *td, struct access_args *uap)
2053 return (kern_accessat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2057 #ifndef _SYS_SYSPROTO_H_
2058 struct faccessat_args {
2066 sys_faccessat(struct thread *td, struct faccessat_args *uap)
2069 return (kern_accessat(td, uap->fd, uap->path, UIO_USERSPACE, uap->flag,
2074 kern_accessat(struct thread *td, int fd, const char *path,
2075 enum uio_seg pathseg, int flag, int amode)
2077 struct ucred *cred, *usecred;
2079 struct nameidata nd;
2082 if ((flag & ~(AT_EACCESS | AT_BENEATH | AT_RESOLVE_BENEATH)) != 0)
2084 if (amode != F_OK && (amode & ~(R_OK | W_OK | X_OK)) != 0)
2088 * Create and modify a temporary credential instead of one that
2089 * is potentially shared (if we need one).
2091 cred = td->td_ucred;
2092 if ((flag & AT_EACCESS) == 0 &&
2093 ((cred->cr_uid != cred->cr_ruid ||
2094 cred->cr_rgid != cred->cr_groups[0]))) {
2095 usecred = crdup(cred);
2096 usecred->cr_uid = cred->cr_ruid;
2097 usecred->cr_groups[0] = cred->cr_rgid;
2098 td->td_ucred = usecred;
2101 AUDIT_ARG_VALUE(amode);
2102 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF |
2103 AUDITVNODE1 | at2cnpflags(flag, AT_BENEATH | AT_RESOLVE_BENEATH),
2104 pathseg, path, fd, &cap_fstat_rights, td);
2105 if ((error = namei(&nd)) != 0)
2109 error = vn_access(vp, amode, usecred, td);
2110 NDFREE(&nd, NDF_ONLY_PNBUF);
2113 if (usecred != cred) {
2114 td->td_ucred = cred;
2121 * Check access permissions using "effective" credentials.
2123 #ifndef _SYS_SYSPROTO_H_
2124 struct eaccess_args {
2130 sys_eaccess(struct thread *td, struct eaccess_args *uap)
2133 return (kern_accessat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2134 AT_EACCESS, uap->amode));
2137 #if defined(COMPAT_43)
2139 * Get file status; this version follows links.
2141 #ifndef _SYS_SYSPROTO_H_
2148 ostat(struct thread *td, struct ostat_args *uap)
2154 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
2159 return (copyout(&osb, uap->ub, sizeof (osb)));
2163 * Get file status; this version does not follow links.
2165 #ifndef _SYS_SYSPROTO_H_
2166 struct olstat_args {
2172 olstat(struct thread *td, struct olstat_args *uap)
2178 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2179 UIO_USERSPACE, &sb, NULL);
2183 return (copyout(&osb, uap->ub, sizeof (osb)));
2187 * Convert from an old to a new stat structure.
2188 * XXX: many values are blindly truncated.
2191 cvtstat(struct stat *st, struct ostat *ost)
2194 bzero(ost, sizeof(*ost));
2195 ost->st_dev = st->st_dev;
2196 ost->st_ino = st->st_ino;
2197 ost->st_mode = st->st_mode;
2198 ost->st_nlink = st->st_nlink;
2199 ost->st_uid = st->st_uid;
2200 ost->st_gid = st->st_gid;
2201 ost->st_rdev = st->st_rdev;
2202 ost->st_size = MIN(st->st_size, INT32_MAX);
2203 ost->st_atim = st->st_atim;
2204 ost->st_mtim = st->st_mtim;
2205 ost->st_ctim = st->st_ctim;
2206 ost->st_blksize = st->st_blksize;
2207 ost->st_blocks = st->st_blocks;
2208 ost->st_flags = st->st_flags;
2209 ost->st_gen = st->st_gen;
2211 #endif /* COMPAT_43 */
2213 #if defined(COMPAT_43) || defined(COMPAT_FREEBSD11)
2214 int ino64_trunc_error;
2215 SYSCTL_INT(_vfs, OID_AUTO, ino64_trunc_error, CTLFLAG_RW,
2216 &ino64_trunc_error, 0,
2217 "Error on truncation of device, file or inode number, or link count");
2220 freebsd11_cvtstat(struct stat *st, struct freebsd11_stat *ost)
2223 ost->st_dev = st->st_dev;
2224 if (ost->st_dev != st->st_dev) {
2225 switch (ino64_trunc_error) {
2228 * Since dev_t is almost raw, don't clamp to the
2229 * maximum for case 2, but ignore the error.
2236 ost->st_ino = st->st_ino;
2237 if (ost->st_ino != st->st_ino) {
2238 switch (ino64_trunc_error) {
2245 ost->st_ino = UINT32_MAX;
2249 ost->st_mode = st->st_mode;
2250 ost->st_nlink = st->st_nlink;
2251 if (ost->st_nlink != st->st_nlink) {
2252 switch (ino64_trunc_error) {
2259 ost->st_nlink = UINT16_MAX;
2263 ost->st_uid = st->st_uid;
2264 ost->st_gid = st->st_gid;
2265 ost->st_rdev = st->st_rdev;
2266 if (ost->st_rdev != st->st_rdev) {
2267 switch (ino64_trunc_error) {
2274 ost->st_atim = st->st_atim;
2275 ost->st_mtim = st->st_mtim;
2276 ost->st_ctim = st->st_ctim;
2277 ost->st_size = st->st_size;
2278 ost->st_blocks = st->st_blocks;
2279 ost->st_blksize = st->st_blksize;
2280 ost->st_flags = st->st_flags;
2281 ost->st_gen = st->st_gen;
2283 ost->st_birthtim = st->st_birthtim;
2284 bzero((char *)&ost->st_birthtim + sizeof(ost->st_birthtim),
2285 sizeof(*ost) - offsetof(struct freebsd11_stat,
2286 st_birthtim) - sizeof(ost->st_birthtim));
2291 freebsd11_stat(struct thread *td, struct freebsd11_stat_args* uap)
2294 struct freebsd11_stat osb;
2297 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
2301 error = freebsd11_cvtstat(&sb, &osb);
2303 error = copyout(&osb, uap->ub, sizeof(osb));
2308 freebsd11_lstat(struct thread *td, struct freebsd11_lstat_args* uap)
2311 struct freebsd11_stat osb;
2314 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2315 UIO_USERSPACE, &sb, NULL);
2318 error = freebsd11_cvtstat(&sb, &osb);
2320 error = copyout(&osb, uap->ub, sizeof(osb));
2325 freebsd11_fhstat(struct thread *td, struct freebsd11_fhstat_args* uap)
2329 struct freebsd11_stat osb;
2332 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
2335 error = kern_fhstat(td, fh, &sb);
2338 error = freebsd11_cvtstat(&sb, &osb);
2340 error = copyout(&osb, uap->sb, sizeof(osb));
2345 freebsd11_fstatat(struct thread *td, struct freebsd11_fstatat_args* uap)
2348 struct freebsd11_stat osb;
2351 error = kern_statat(td, uap->flag, uap->fd, uap->path,
2352 UIO_USERSPACE, &sb, NULL);
2355 error = freebsd11_cvtstat(&sb, &osb);
2357 error = copyout(&osb, uap->buf, sizeof(osb));
2360 #endif /* COMPAT_FREEBSD11 */
2365 #ifndef _SYS_SYSPROTO_H_
2366 struct fstatat_args {
2374 sys_fstatat(struct thread *td, struct fstatat_args *uap)
2379 error = kern_statat(td, uap->flag, uap->fd, uap->path,
2380 UIO_USERSPACE, &sb, NULL);
2382 error = copyout(&sb, uap->buf, sizeof (sb));
2387 kern_statat(struct thread *td, int flag, int fd, const char *path,
2388 enum uio_seg pathseg, struct stat *sbp,
2389 void (*hook)(struct vnode *vp, struct stat *sbp))
2391 struct nameidata nd;
2394 if ((flag & ~(AT_SYMLINK_NOFOLLOW | AT_BENEATH |
2395 AT_RESOLVE_BENEATH)) != 0)
2398 NDINIT_ATRIGHTS(&nd, LOOKUP, at2cnpflags(flag, AT_BENEATH |
2399 AT_RESOLVE_BENEATH | AT_SYMLINK_NOFOLLOW) | LOCKSHARED | LOCKLEAF |
2400 AUDITVNODE1, pathseg, path, fd, &cap_fstat_rights, td);
2402 if ((error = namei(&nd)) != 0)
2404 error = VOP_STAT(nd.ni_vp, sbp, td->td_ucred, NOCRED, td);
2406 if (__predict_false(hook != NULL))
2407 hook(nd.ni_vp, sbp);
2409 NDFREE(&nd, NDF_ONLY_PNBUF);
2411 #ifdef __STAT_TIME_T_EXT
2412 sbp->st_atim_ext = 0;
2413 sbp->st_mtim_ext = 0;
2414 sbp->st_ctim_ext = 0;
2415 sbp->st_btim_ext = 0;
2418 if (KTRPOINT(td, KTR_STRUCT))
2419 ktrstat_error(sbp, error);
2424 #if defined(COMPAT_FREEBSD11)
2426 * Implementation of the NetBSD [l]stat() functions.
2429 freebsd11_cvtnstat(struct stat *sb, struct nstat *nsb)
2432 bzero(nsb, sizeof(*nsb));
2433 nsb->st_dev = sb->st_dev;
2434 nsb->st_ino = sb->st_ino;
2435 nsb->st_mode = sb->st_mode;
2436 nsb->st_nlink = sb->st_nlink;
2437 nsb->st_uid = sb->st_uid;
2438 nsb->st_gid = sb->st_gid;
2439 nsb->st_rdev = sb->st_rdev;
2440 nsb->st_atim = sb->st_atim;
2441 nsb->st_mtim = sb->st_mtim;
2442 nsb->st_ctim = sb->st_ctim;
2443 nsb->st_size = sb->st_size;
2444 nsb->st_blocks = sb->st_blocks;
2445 nsb->st_blksize = sb->st_blksize;
2446 nsb->st_flags = sb->st_flags;
2447 nsb->st_gen = sb->st_gen;
2448 nsb->st_birthtim = sb->st_birthtim;
2451 #ifndef _SYS_SYSPROTO_H_
2452 struct freebsd11_nstat_args {
2458 freebsd11_nstat(struct thread *td, struct freebsd11_nstat_args *uap)
2464 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
2468 freebsd11_cvtnstat(&sb, &nsb);
2469 return (copyout(&nsb, uap->ub, sizeof (nsb)));
2473 * NetBSD lstat. Get file status; this version does not follow links.
2475 #ifndef _SYS_SYSPROTO_H_
2476 struct freebsd11_nlstat_args {
2482 freebsd11_nlstat(struct thread *td, struct freebsd11_nlstat_args *uap)
2488 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2489 UIO_USERSPACE, &sb, NULL);
2492 freebsd11_cvtnstat(&sb, &nsb);
2493 return (copyout(&nsb, uap->ub, sizeof (nsb)));
2495 #endif /* COMPAT_FREEBSD11 */
2498 * Get configurable pathname variables.
2500 #ifndef _SYS_SYSPROTO_H_
2501 struct pathconf_args {
2507 sys_pathconf(struct thread *td, struct pathconf_args *uap)
2512 error = kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name, FOLLOW,
2515 td->td_retval[0] = value;
2519 #ifndef _SYS_SYSPROTO_H_
2520 struct lpathconf_args {
2526 sys_lpathconf(struct thread *td, struct lpathconf_args *uap)
2531 error = kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name,
2534 td->td_retval[0] = value;
2539 kern_pathconf(struct thread *td, const char *path, enum uio_seg pathseg,
2540 int name, u_long flags, long *valuep)
2542 struct nameidata nd;
2545 NDINIT(&nd, LOOKUP, LOCKSHARED | LOCKLEAF | AUDITVNODE1 | flags,
2547 if ((error = namei(&nd)) != 0)
2549 NDFREE(&nd, NDF_ONLY_PNBUF);
2551 error = VOP_PATHCONF(nd.ni_vp, name, valuep);
2557 * Return target name of a symbolic link.
2559 #ifndef _SYS_SYSPROTO_H_
2560 struct readlink_args {
2567 sys_readlink(struct thread *td, struct readlink_args *uap)
2570 return (kern_readlinkat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2571 uap->buf, UIO_USERSPACE, uap->count));
2573 #ifndef _SYS_SYSPROTO_H_
2574 struct readlinkat_args {
2582 sys_readlinkat(struct thread *td, struct readlinkat_args *uap)
2585 return (kern_readlinkat(td, uap->fd, uap->path, UIO_USERSPACE,
2586 uap->buf, UIO_USERSPACE, uap->bufsize));
2590 kern_readlinkat(struct thread *td, int fd, const char *path,
2591 enum uio_seg pathseg, char *buf, enum uio_seg bufseg, size_t count)
2594 struct nameidata nd;
2597 if (count > IOSIZE_MAX)
2600 NDINIT_AT(&nd, LOOKUP, NOFOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
2601 pathseg, path, fd, td);
2603 if ((error = namei(&nd)) != 0)
2605 NDFREE(&nd, NDF_ONLY_PNBUF);
2608 error = kern_readlink_vp(vp, buf, bufseg, count, td);
2615 * Helper function to readlink from a vnode
2618 kern_readlink_vp(struct vnode *vp, char *buf, enum uio_seg bufseg, size_t count,
2625 ASSERT_VOP_LOCKED(vp, "kern_readlink_vp(): vp not locked");
2627 error = mac_vnode_check_readlink(td->td_ucred, vp);
2631 if (vp->v_type != VLNK && (vp->v_vflag & VV_READLINK) == 0)
2634 aiov.iov_base = buf;
2635 aiov.iov_len = count;
2636 auio.uio_iov = &aiov;
2637 auio.uio_iovcnt = 1;
2638 auio.uio_offset = 0;
2639 auio.uio_rw = UIO_READ;
2640 auio.uio_segflg = bufseg;
2642 auio.uio_resid = count;
2643 error = VOP_READLINK(vp, &auio, td->td_ucred);
2644 td->td_retval[0] = count - auio.uio_resid;
2649 * Common implementation code for chflags() and fchflags().
2652 setfflags(struct thread *td, struct vnode *vp, u_long flags)
2658 /* We can't support the value matching VNOVAL. */
2659 if (flags == VNOVAL)
2660 return (EOPNOTSUPP);
2663 * Prevent non-root users from setting flags on devices. When
2664 * a device is reused, users can retain ownership of the device
2665 * if they are allowed to set flags and programs assume that
2666 * chown can't fail when done as root.
2668 if (vp->v_type == VCHR || vp->v_type == VBLK) {
2669 error = priv_check(td, PRIV_VFS_CHFLAGS_DEV);
2674 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2677 vattr.va_flags = flags;
2678 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2680 error = mac_vnode_check_setflags(td->td_ucred, vp, vattr.va_flags);
2683 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
2685 vn_finished_write(mp);
2690 * Change flags of a file given a path name.
2692 #ifndef _SYS_SYSPROTO_H_
2693 struct chflags_args {
2699 sys_chflags(struct thread *td, struct chflags_args *uap)
2702 return (kern_chflagsat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2706 #ifndef _SYS_SYSPROTO_H_
2707 struct chflagsat_args {
2715 sys_chflagsat(struct thread *td, struct chflagsat_args *uap)
2718 if ((uap->atflag & ~(AT_SYMLINK_NOFOLLOW | AT_BENEATH |
2719 AT_RESOLVE_BENEATH)) != 0)
2722 return (kern_chflagsat(td, uap->fd, uap->path, UIO_USERSPACE,
2723 uap->flags, uap->atflag));
2727 * Same as chflags() but doesn't follow symlinks.
2729 #ifndef _SYS_SYSPROTO_H_
2730 struct lchflags_args {
2736 sys_lchflags(struct thread *td, struct lchflags_args *uap)
2739 return (kern_chflagsat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2740 uap->flags, AT_SYMLINK_NOFOLLOW));
2744 kern_chflagsat(struct thread *td, int fd, const char *path,
2745 enum uio_seg pathseg, u_long flags, int atflag)
2747 struct nameidata nd;
2750 AUDIT_ARG_FFLAGS(flags);
2751 NDINIT_ATRIGHTS(&nd, LOOKUP, at2cnpflags(atflag, AT_SYMLINK_NOFOLLOW |
2752 AT_BENEATH | AT_RESOLVE_BENEATH) | AUDITVNODE1, pathseg, path, fd,
2753 &cap_fchflags_rights, td);
2754 if ((error = namei(&nd)) != 0)
2756 NDFREE(&nd, NDF_ONLY_PNBUF);
2757 error = setfflags(td, nd.ni_vp, flags);
2763 * Change flags of a file given a file descriptor.
2765 #ifndef _SYS_SYSPROTO_H_
2766 struct fchflags_args {
2772 sys_fchflags(struct thread *td, struct fchflags_args *uap)
2777 AUDIT_ARG_FD(uap->fd);
2778 AUDIT_ARG_FFLAGS(uap->flags);
2779 error = getvnode(td, uap->fd, &cap_fchflags_rights,
2784 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
2785 AUDIT_ARG_VNODE1(fp->f_vnode);
2786 VOP_UNLOCK(fp->f_vnode);
2788 error = setfflags(td, fp->f_vnode, uap->flags);
2794 * Common implementation code for chmod(), lchmod() and fchmod().
2797 setfmode(struct thread *td, struct ucred *cred, struct vnode *vp, int mode)
2803 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2805 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2807 vattr.va_mode = mode & ALLPERMS;
2809 error = mac_vnode_check_setmode(cred, vp, vattr.va_mode);
2812 error = VOP_SETATTR(vp, &vattr, cred);
2814 vn_finished_write(mp);
2819 * Change mode of a file given path name.
2821 #ifndef _SYS_SYSPROTO_H_
2828 sys_chmod(struct thread *td, struct chmod_args *uap)
2831 return (kern_fchmodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2835 #ifndef _SYS_SYSPROTO_H_
2836 struct fchmodat_args {
2844 sys_fchmodat(struct thread *td, struct fchmodat_args *uap)
2847 if ((uap->flag & ~(AT_SYMLINK_NOFOLLOW | AT_BENEATH |
2848 AT_RESOLVE_BENEATH)) != 0)
2851 return (kern_fchmodat(td, uap->fd, uap->path, UIO_USERSPACE,
2852 uap->mode, uap->flag));
2856 * Change mode of a file given path name (don't follow links.)
2858 #ifndef _SYS_SYSPROTO_H_
2859 struct lchmod_args {
2865 sys_lchmod(struct thread *td, struct lchmod_args *uap)
2868 return (kern_fchmodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2869 uap->mode, AT_SYMLINK_NOFOLLOW));
2873 kern_fchmodat(struct thread *td, int fd, const char *path,
2874 enum uio_seg pathseg, mode_t mode, int flag)
2876 struct nameidata nd;
2879 AUDIT_ARG_MODE(mode);
2880 NDINIT_ATRIGHTS(&nd, LOOKUP, at2cnpflags(flag, AT_SYMLINK_NOFOLLOW |
2881 AT_BENEATH | AT_RESOLVE_BENEATH) | AUDITVNODE1, pathseg, path, fd,
2882 &cap_fchmod_rights, td);
2883 if ((error = namei(&nd)) != 0)
2885 NDFREE(&nd, NDF_ONLY_PNBUF);
2886 error = setfmode(td, td->td_ucred, nd.ni_vp, mode);
2892 * Change mode of a file given a file descriptor.
2894 #ifndef _SYS_SYSPROTO_H_
2895 struct fchmod_args {
2901 sys_fchmod(struct thread *td, struct fchmod_args *uap)
2906 AUDIT_ARG_FD(uap->fd);
2907 AUDIT_ARG_MODE(uap->mode);
2909 error = fget(td, uap->fd, &cap_fchmod_rights, &fp);
2912 error = fo_chmod(fp, uap->mode, td->td_ucred, td);
2918 * Common implementation for chown(), lchown(), and fchown()
2921 setfown(struct thread *td, struct ucred *cred, struct vnode *vp, uid_t uid,
2928 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2930 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2935 error = mac_vnode_check_setowner(cred, vp, vattr.va_uid,
2939 error = VOP_SETATTR(vp, &vattr, cred);
2941 vn_finished_write(mp);
2946 * Set ownership given a path name.
2948 #ifndef _SYS_SYSPROTO_H_
2956 sys_chown(struct thread *td, struct chown_args *uap)
2959 return (kern_fchownat(td, AT_FDCWD, uap->path, UIO_USERSPACE, uap->uid,
2963 #ifndef _SYS_SYSPROTO_H_
2964 struct fchownat_args {
2973 sys_fchownat(struct thread *td, struct fchownat_args *uap)
2976 if ((uap->flag & ~(AT_SYMLINK_NOFOLLOW | AT_BENEATH |
2977 AT_RESOLVE_BENEATH)) != 0)
2980 return (kern_fchownat(td, uap->fd, uap->path, UIO_USERSPACE, uap->uid,
2981 uap->gid, uap->flag));
2985 kern_fchownat(struct thread *td, int fd, const char *path,
2986 enum uio_seg pathseg, int uid, int gid, int flag)
2988 struct nameidata nd;
2991 AUDIT_ARG_OWNER(uid, gid);
2992 NDINIT_ATRIGHTS(&nd, LOOKUP, at2cnpflags(flag, AT_SYMLINK_NOFOLLOW |
2993 AT_BENEATH | AT_RESOLVE_BENEATH) | AUDITVNODE1, pathseg, path, fd,
2994 &cap_fchown_rights, td);
2996 if ((error = namei(&nd)) != 0)
2998 NDFREE(&nd, NDF_ONLY_PNBUF);
2999 error = setfown(td, td->td_ucred, nd.ni_vp, uid, gid);
3005 * Set ownership given a path name, do not cross symlinks.
3007 #ifndef _SYS_SYSPROTO_H_
3008 struct lchown_args {
3015 sys_lchown(struct thread *td, struct lchown_args *uap)
3018 return (kern_fchownat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
3019 uap->uid, uap->gid, AT_SYMLINK_NOFOLLOW));
3023 * Set ownership given a file descriptor.
3025 #ifndef _SYS_SYSPROTO_H_
3026 struct fchown_args {
3033 sys_fchown(struct thread *td, struct fchown_args *uap)
3038 AUDIT_ARG_FD(uap->fd);
3039 AUDIT_ARG_OWNER(uap->uid, uap->gid);
3040 error = fget(td, uap->fd, &cap_fchown_rights, &fp);
3043 error = fo_chown(fp, uap->uid, uap->gid, td->td_ucred, td);
3049 * Common implementation code for utimes(), lutimes(), and futimes().
3052 getutimes(const struct timeval *usrtvp, enum uio_seg tvpseg,
3053 struct timespec *tsp)
3055 struct timeval tv[2];
3056 const struct timeval *tvp;
3059 if (usrtvp == NULL) {
3060 vfs_timestamp(&tsp[0]);
3063 if (tvpseg == UIO_SYSSPACE) {
3066 if ((error = copyin(usrtvp, tv, sizeof(tv))) != 0)
3071 if (tvp[0].tv_usec < 0 || tvp[0].tv_usec >= 1000000 ||
3072 tvp[1].tv_usec < 0 || tvp[1].tv_usec >= 1000000)
3074 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
3075 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
3081 * Common implementation code for futimens(), utimensat().
3083 #define UTIMENS_NULL 0x1
3084 #define UTIMENS_EXIT 0x2
3086 getutimens(const struct timespec *usrtsp, enum uio_seg tspseg,
3087 struct timespec *tsp, int *retflags)
3089 struct timespec tsnow;
3092 vfs_timestamp(&tsnow);
3094 if (usrtsp == NULL) {
3097 *retflags |= UTIMENS_NULL;
3100 if (tspseg == UIO_SYSSPACE) {
3103 } else if ((error = copyin(usrtsp, tsp, sizeof(*tsp) * 2)) != 0)
3105 if (tsp[0].tv_nsec == UTIME_OMIT && tsp[1].tv_nsec == UTIME_OMIT)
3106 *retflags |= UTIMENS_EXIT;
3107 if (tsp[0].tv_nsec == UTIME_NOW && tsp[1].tv_nsec == UTIME_NOW)
3108 *retflags |= UTIMENS_NULL;
3109 if (tsp[0].tv_nsec == UTIME_OMIT)
3110 tsp[0].tv_sec = VNOVAL;
3111 else if (tsp[0].tv_nsec == UTIME_NOW)
3113 else if (tsp[0].tv_nsec < 0 || tsp[0].tv_nsec >= 1000000000L)
3115 if (tsp[1].tv_nsec == UTIME_OMIT)
3116 tsp[1].tv_sec = VNOVAL;
3117 else if (tsp[1].tv_nsec == UTIME_NOW)
3119 else if (tsp[1].tv_nsec < 0 || tsp[1].tv_nsec >= 1000000000L)
3126 * Common implementation code for utimes(), lutimes(), futimes(), futimens(),
3130 setutimes(struct thread *td, struct vnode *vp, const struct timespec *ts,
3131 int numtimes, int nullflag)
3135 int error, setbirthtime;
3137 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
3139 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3141 if (numtimes < 3 && !VOP_GETATTR(vp, &vattr, td->td_ucred) &&
3142 timespeccmp(&ts[1], &vattr.va_birthtime, < ))
3145 vattr.va_atime = ts[0];
3146 vattr.va_mtime = ts[1];
3148 vattr.va_birthtime = ts[1];
3150 vattr.va_birthtime = ts[2];
3152 vattr.va_vaflags |= VA_UTIMES_NULL;
3154 error = mac_vnode_check_setutimes(td->td_ucred, vp, vattr.va_atime,
3158 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3160 vn_finished_write(mp);
3165 * Set the access and modification times of a file.
3167 #ifndef _SYS_SYSPROTO_H_
3168 struct utimes_args {
3170 struct timeval *tptr;
3174 sys_utimes(struct thread *td, struct utimes_args *uap)
3177 return (kern_utimesat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
3178 uap->tptr, UIO_USERSPACE));
3181 #ifndef _SYS_SYSPROTO_H_
3182 struct futimesat_args {
3185 const struct timeval * times;
3189 sys_futimesat(struct thread *td, struct futimesat_args *uap)
3192 return (kern_utimesat(td, uap->fd, uap->path, UIO_USERSPACE,
3193 uap->times, UIO_USERSPACE));
3197 kern_utimesat(struct thread *td, int fd, const char *path,
3198 enum uio_seg pathseg, struct timeval *tptr, enum uio_seg tptrseg)
3200 struct nameidata nd;
3201 struct timespec ts[2];
3204 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3206 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, fd,
3207 &cap_futimes_rights, td);
3209 if ((error = namei(&nd)) != 0)
3211 NDFREE(&nd, NDF_ONLY_PNBUF);
3212 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3218 * Set the access and modification times of a file.
3220 #ifndef _SYS_SYSPROTO_H_
3221 struct lutimes_args {
3223 struct timeval *tptr;
3227 sys_lutimes(struct thread *td, struct lutimes_args *uap)
3230 return (kern_lutimes(td, uap->path, UIO_USERSPACE, uap->tptr,
3235 kern_lutimes(struct thread *td, const char *path, enum uio_seg pathseg,
3236 struct timeval *tptr, enum uio_seg tptrseg)
3238 struct timespec ts[2];
3239 struct nameidata nd;
3242 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3244 NDINIT(&nd, LOOKUP, NOFOLLOW | AUDITVNODE1, pathseg, path, td);
3245 if ((error = namei(&nd)) != 0)
3247 NDFREE(&nd, NDF_ONLY_PNBUF);
3248 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3254 * Set the access and modification times of a file.
3256 #ifndef _SYS_SYSPROTO_H_
3257 struct futimes_args {
3259 struct timeval *tptr;
3263 sys_futimes(struct thread *td, struct futimes_args *uap)
3266 return (kern_futimes(td, uap->fd, uap->tptr, UIO_USERSPACE));
3270 kern_futimes(struct thread *td, int fd, struct timeval *tptr,
3271 enum uio_seg tptrseg)
3273 struct timespec ts[2];
3278 error = getutimes(tptr, tptrseg, ts);
3281 error = getvnode(td, fd, &cap_futimes_rights, &fp);
3285 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
3286 AUDIT_ARG_VNODE1(fp->f_vnode);
3287 VOP_UNLOCK(fp->f_vnode);
3289 error = setutimes(td, fp->f_vnode, ts, 2, tptr == NULL);
3295 sys_futimens(struct thread *td, struct futimens_args *uap)
3298 return (kern_futimens(td, uap->fd, uap->times, UIO_USERSPACE));
3302 kern_futimens(struct thread *td, int fd, struct timespec *tptr,
3303 enum uio_seg tptrseg)
3305 struct timespec ts[2];
3310 error = getutimens(tptr, tptrseg, ts, &flags);
3313 if (flags & UTIMENS_EXIT)
3315 error = getvnode(td, fd, &cap_futimes_rights, &fp);
3319 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
3320 AUDIT_ARG_VNODE1(fp->f_vnode);
3321 VOP_UNLOCK(fp->f_vnode);
3323 error = setutimes(td, fp->f_vnode, ts, 2, flags & UTIMENS_NULL);
3329 sys_utimensat(struct thread *td, struct utimensat_args *uap)
3332 return (kern_utimensat(td, uap->fd, uap->path, UIO_USERSPACE,
3333 uap->times, UIO_USERSPACE, uap->flag));
3337 kern_utimensat(struct thread *td, int fd, const char *path,
3338 enum uio_seg pathseg, struct timespec *tptr, enum uio_seg tptrseg,
3341 struct nameidata nd;
3342 struct timespec ts[2];
3345 if ((flag & ~(AT_SYMLINK_NOFOLLOW | AT_BENEATH |
3346 AT_RESOLVE_BENEATH)) != 0)
3349 if ((error = getutimens(tptr, tptrseg, ts, &flags)) != 0)
3351 NDINIT_ATRIGHTS(&nd, LOOKUP, at2cnpflags(flag, AT_SYMLINK_NOFOLLOW |
3352 AT_BENEATH | AT_RESOLVE_BENEATH) | AUDITVNODE1,
3353 pathseg, path, fd, &cap_futimes_rights, td);
3354 if ((error = namei(&nd)) != 0)
3357 * We are allowed to call namei() regardless of 2xUTIME_OMIT.
3359 * "If both tv_nsec fields are UTIME_OMIT... EACCESS may be detected."
3360 * "Search permission is denied by a component of the path prefix."
3362 NDFREE(&nd, NDF_ONLY_PNBUF);
3363 if ((flags & UTIMENS_EXIT) == 0)
3364 error = setutimes(td, nd.ni_vp, ts, 2, flags & UTIMENS_NULL);
3370 * Truncate a file given its path name.
3372 #ifndef _SYS_SYSPROTO_H_
3373 struct truncate_args {
3380 sys_truncate(struct thread *td, struct truncate_args *uap)
3383 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3387 kern_truncate(struct thread *td, const char *path, enum uio_seg pathseg,
3394 struct nameidata nd;
3399 NDINIT(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, td);
3400 if ((error = namei(&nd)) != 0)
3403 rl_cookie = vn_rangelock_wlock(vp, 0, OFF_MAX);
3404 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0) {
3405 vn_rangelock_unlock(vp, rl_cookie);
3409 NDFREE(&nd, NDF_ONLY_PNBUF);
3410 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3411 if (vp->v_type == VDIR)
3414 else if ((error = mac_vnode_check_write(td->td_ucred, NOCRED, vp))) {
3417 else if ((error = vn_writechk(vp)) == 0 &&
3418 (error = VOP_ACCESS(vp, VWRITE, td->td_ucred, td)) == 0) {
3420 vattr.va_size = length;
3421 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3424 vn_finished_write(mp);
3425 vn_rangelock_unlock(vp, rl_cookie);
3430 #if defined(COMPAT_43)
3432 * Truncate a file given its path name.
3434 #ifndef _SYS_SYSPROTO_H_
3435 struct otruncate_args {
3441 otruncate(struct thread *td, struct otruncate_args *uap)
3444 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3446 #endif /* COMPAT_43 */
3448 #if defined(COMPAT_FREEBSD6)
3449 /* Versions with the pad argument */
3451 freebsd6_truncate(struct thread *td, struct freebsd6_truncate_args *uap)
3454 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3458 freebsd6_ftruncate(struct thread *td, struct freebsd6_ftruncate_args *uap)
3461 return (kern_ftruncate(td, uap->fd, uap->length));
3466 kern_fsync(struct thread *td, int fd, bool fullsync)
3471 int error, lock_flags;
3474 error = getvnode(td, fd, &cap_fsync_rights, &fp);
3480 /* XXXKIB: compete outstanding aio writes */;
3482 error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
3485 if (MNT_SHARED_WRITES(mp) ||
3486 ((mp == NULL) && MNT_SHARED_WRITES(vp->v_mount))) {
3487 lock_flags = LK_SHARED;
3489 lock_flags = LK_EXCLUSIVE;
3491 vn_lock(vp, lock_flags | LK_RETRY);
3492 AUDIT_ARG_VNODE1(vp);
3493 if (vp->v_object != NULL) {
3494 VM_OBJECT_WLOCK(vp->v_object);
3495 vm_object_page_clean(vp->v_object, 0, 0, 0);
3496 VM_OBJECT_WUNLOCK(vp->v_object);
3498 error = fullsync ? VOP_FSYNC(vp, MNT_WAIT, td) : VOP_FDATASYNC(vp, td);
3500 vn_finished_write(mp);
3507 * Sync an open file.
3509 #ifndef _SYS_SYSPROTO_H_
3515 sys_fsync(struct thread *td, struct fsync_args *uap)
3518 return (kern_fsync(td, uap->fd, true));
3522 sys_fdatasync(struct thread *td, struct fdatasync_args *uap)
3525 return (kern_fsync(td, uap->fd, false));
3529 * Rename files. Source and destination must either both be directories, or
3530 * both not be directories. If target is a directory, it must be empty.
3532 #ifndef _SYS_SYSPROTO_H_
3533 struct rename_args {
3539 sys_rename(struct thread *td, struct rename_args *uap)
3542 return (kern_renameat(td, AT_FDCWD, uap->from, AT_FDCWD,
3543 uap->to, UIO_USERSPACE));
3546 #ifndef _SYS_SYSPROTO_H_
3547 struct renameat_args {
3555 sys_renameat(struct thread *td, struct renameat_args *uap)
3558 return (kern_renameat(td, uap->oldfd, uap->old, uap->newfd, uap->new,
3564 kern_renameat_mac(struct thread *td, int oldfd, const char *old, int newfd,
3565 const char *new, enum uio_seg pathseg, struct nameidata *fromnd)
3569 NDINIT_ATRIGHTS(fromnd, DELETE, LOCKPARENT | LOCKLEAF | SAVESTART |
3570 AUDITVNODE1, pathseg, old, oldfd, &cap_renameat_source_rights, td);
3571 if ((error = namei(fromnd)) != 0)
3573 error = mac_vnode_check_rename_from(td->td_ucred, fromnd->ni_dvp,
3574 fromnd->ni_vp, &fromnd->ni_cnd);
3575 VOP_UNLOCK(fromnd->ni_dvp);
3576 if (fromnd->ni_dvp != fromnd->ni_vp)
3577 VOP_UNLOCK(fromnd->ni_vp);
3579 NDFREE(fromnd, NDF_ONLY_PNBUF);
3580 vrele(fromnd->ni_dvp);
3581 vrele(fromnd->ni_vp);
3582 if (fromnd->ni_startdir)
3583 vrele(fromnd->ni_startdir);
3590 kern_renameat(struct thread *td, int oldfd, const char *old, int newfd,
3591 const char *new, enum uio_seg pathseg)
3593 struct mount *mp = NULL;
3594 struct vnode *tvp, *fvp, *tdvp;
3595 struct nameidata fromnd, tond;
3601 if (mac_vnode_check_rename_from_enabled()) {
3602 error = kern_renameat_mac(td, oldfd, old, newfd, new, pathseg,
3608 NDINIT_ATRIGHTS(&fromnd, DELETE, WANTPARENT | SAVESTART | AUDITVNODE1,
3609 pathseg, old, oldfd, &cap_renameat_source_rights, td);
3610 if ((error = namei(&fromnd)) != 0)
3616 NDINIT_ATRIGHTS(&tond, RENAME, LOCKPARENT | LOCKLEAF | NOCACHE |
3617 SAVESTART | AUDITVNODE2, pathseg, new, newfd,
3618 &cap_renameat_target_rights, td);
3619 if (fromnd.ni_vp->v_type == VDIR)
3620 tond.ni_cnd.cn_flags |= WILLBEDIR;
3621 if ((error = namei(&tond)) != 0) {
3622 /* Translate error code for rename("dir1", "dir2/."). */
3623 if (error == EISDIR && fvp->v_type == VDIR)
3625 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3626 vrele(fromnd.ni_dvp);
3632 error = vn_start_write(fvp, &mp, V_NOWAIT);
3634 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3635 NDFREE(&tond, NDF_ONLY_PNBUF);
3642 vrele(fromnd.ni_dvp);
3644 vrele(tond.ni_startdir);
3645 if (fromnd.ni_startdir != NULL)
3646 vrele(fromnd.ni_startdir);
3647 error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH);
3653 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
3656 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
3661 if (newfd != AT_FDCWD && (tond.ni_resflags & NIRES_ABS) == 0) {
3663 * If the target already exists we require CAP_UNLINKAT
3664 * from 'newfd', when newfd was used for the lookup.
3666 error = cap_check(&tond.ni_filecaps.fc_rights,
3667 &cap_unlinkat_rights);
3678 * If the source is the same as the destination (that is, if they
3679 * are links to the same vnode), then there is nothing to do.
3685 error = mac_vnode_check_rename_to(td->td_ucred, tdvp,
3686 tond.ni_vp, fromnd.ni_dvp == tdvp, &tond.ni_cnd);
3690 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
3691 tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
3692 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3693 NDFREE(&tond, NDF_ONLY_PNBUF);
3695 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3696 NDFREE(&tond, NDF_ONLY_PNBUF);
3703 vrele(fromnd.ni_dvp);
3706 vrele(tond.ni_startdir);
3707 vn_finished_write(mp);
3709 if (fromnd.ni_startdir)
3710 vrele(fromnd.ni_startdir);
3717 * Make a directory file.
3719 #ifndef _SYS_SYSPROTO_H_
3726 sys_mkdir(struct thread *td, struct mkdir_args *uap)
3729 return (kern_mkdirat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
3733 #ifndef _SYS_SYSPROTO_H_
3734 struct mkdirat_args {
3741 sys_mkdirat(struct thread *td, struct mkdirat_args *uap)
3744 return (kern_mkdirat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode));
3748 kern_mkdirat(struct thread *td, int fd, const char *path, enum uio_seg segflg,
3754 struct nameidata nd;
3757 AUDIT_ARG_MODE(mode);
3760 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
3761 NOCACHE, segflg, path, fd, &cap_mkdirat_rights,
3763 nd.ni_cnd.cn_flags |= WILLBEDIR;
3764 if ((error = namei(&nd)) != 0)
3768 NDFREE(&nd, NDF_ONLY_PNBUF);
3770 * XXX namei called with LOCKPARENT but not LOCKLEAF has
3771 * the strange behaviour of leaving the vnode unlocked
3772 * if the target is the same vnode as the parent.
3774 if (vp == nd.ni_dvp)
3781 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3782 NDFREE(&nd, NDF_ONLY_PNBUF);
3784 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3789 vattr.va_type = VDIR;
3790 vattr.va_mode = (mode & ACCESSPERMS) &~ td->td_proc->p_fd->fd_cmask;
3792 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
3797 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
3801 NDFREE(&nd, NDF_ONLY_PNBUF);
3805 vn_finished_write(mp);
3810 * Remove a directory file.
3812 #ifndef _SYS_SYSPROTO_H_
3818 sys_rmdir(struct thread *td, struct rmdir_args *uap)
3821 return (kern_frmdirat(td, AT_FDCWD, uap->path, FD_NONE, UIO_USERSPACE,
3826 kern_frmdirat(struct thread *td, int dfd, const char *path, int fd,
3827 enum uio_seg pathseg, int flag)
3832 struct nameidata nd;
3833 cap_rights_t rights;
3837 if (fd != FD_NONE) {
3838 error = getvnode(td, fd, cap_rights_init_one(&rights, CAP_LOOKUP),
3846 NDINIT_ATRIGHTS(&nd, DELETE, LOCKPARENT | LOCKLEAF | AUDITVNODE1 |
3847 at2cnpflags(flag, AT_BENEATH | AT_RESOLVE_BENEATH),
3848 pathseg, path, dfd, &cap_unlinkat_rights, td);
3849 if ((error = namei(&nd)) != 0)
3852 if (vp->v_type != VDIR) {
3857 * No rmdir "." please.
3859 if (nd.ni_dvp == vp) {
3864 * The root of a mounted filesystem cannot be deleted.
3866 if (vp->v_vflag & VV_ROOT) {
3871 if (fp != NULL && fp->f_vnode != vp) {
3872 if (VN_IS_DOOMED(fp->f_vnode))
3880 error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp,
3885 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3886 NDFREE(&nd, NDF_ONLY_PNBUF);
3888 if (nd.ni_dvp == vp)
3892 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3896 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_UNLINK);
3897 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
3898 vn_finished_write(mp);
3900 NDFREE(&nd, NDF_ONLY_PNBUF);
3902 if (nd.ni_dvp == vp)
3912 #if defined(COMPAT_43) || defined(COMPAT_FREEBSD11)
3914 freebsd11_kern_getdirentries(struct thread *td, int fd, char *ubuf, u_int count,
3915 long *basep, void (*func)(struct freebsd11_dirent *))
3917 struct freebsd11_dirent dstdp;
3918 struct dirent *dp, *edp;
3921 ssize_t resid, ucount;
3924 /* XXX arbitrary sanity limit on `count'. */
3925 count = min(count, 64 * 1024);
3927 dirbuf = malloc(count, M_TEMP, M_WAITOK);
3929 error = kern_getdirentries(td, fd, dirbuf, count, &base, &resid,
3937 for (dp = (struct dirent *)dirbuf,
3938 edp = (struct dirent *)&dirbuf[count - resid];
3939 ucount < count && dp < edp; ) {
3940 if (dp->d_reclen == 0)
3942 MPASS(dp->d_reclen >= _GENERIC_DIRLEN(0));
3943 if (dp->d_namlen >= sizeof(dstdp.d_name))
3945 dstdp.d_type = dp->d_type;
3946 dstdp.d_namlen = dp->d_namlen;
3947 dstdp.d_fileno = dp->d_fileno; /* truncate */
3948 if (dstdp.d_fileno != dp->d_fileno) {
3949 switch (ino64_trunc_error) {
3957 dstdp.d_fileno = UINT32_MAX;
3961 dstdp.d_reclen = sizeof(dstdp) - sizeof(dstdp.d_name) +
3962 ((dp->d_namlen + 1 + 3) &~ 3);
3963 bcopy(dp->d_name, dstdp.d_name, dstdp.d_namlen);
3964 bzero(dstdp.d_name + dstdp.d_namlen,
3965 dstdp.d_reclen - offsetof(struct freebsd11_dirent, d_name) -
3967 MPASS(dstdp.d_reclen <= dp->d_reclen);
3968 MPASS(ucount + dstdp.d_reclen <= count);
3971 error = copyout(&dstdp, ubuf + ucount, dstdp.d_reclen);
3974 dp = (struct dirent *)((char *)dp + dp->d_reclen);
3975 ucount += dstdp.d_reclen;
3979 free(dirbuf, M_TEMP);
3981 td->td_retval[0] = ucount;
3988 ogetdirentries_cvt(struct freebsd11_dirent *dp)
3990 #if (BYTE_ORDER == LITTLE_ENDIAN)
3992 * The expected low byte of dp->d_namlen is our dp->d_type.
3993 * The high MBZ byte of dp->d_namlen is our dp->d_namlen.
3995 dp->d_type = dp->d_namlen;
3999 * The dp->d_type is the high byte of the expected dp->d_namlen,
4000 * so must be zero'ed.
4007 * Read a block of directory entries in a filesystem independent format.
4009 #ifndef _SYS_SYSPROTO_H_
4010 struct ogetdirentries_args {
4018 ogetdirentries(struct thread *td, struct ogetdirentries_args *uap)
4023 error = kern_ogetdirentries(td, uap, &loff);
4025 error = copyout(&loff, uap->basep, sizeof(long));
4030 kern_ogetdirentries(struct thread *td, struct ogetdirentries_args *uap,
4036 /* XXX arbitrary sanity limit on `count'. */
4037 if (uap->count > 64 * 1024)
4040 error = freebsd11_kern_getdirentries(td, uap->fd, uap->buf, uap->count,
4041 &base, ogetdirentries_cvt);
4043 if (error == 0 && uap->basep != NULL)
4044 error = copyout(&base, uap->basep, sizeof(long));
4048 #endif /* COMPAT_43 */
4050 #if defined(COMPAT_FREEBSD11)
4051 #ifndef _SYS_SYSPROTO_H_
4052 struct freebsd11_getdirentries_args {
4060 freebsd11_getdirentries(struct thread *td,
4061 struct freebsd11_getdirentries_args *uap)
4066 error = freebsd11_kern_getdirentries(td, uap->fd, uap->buf, uap->count,
4069 if (error == 0 && uap->basep != NULL)
4070 error = copyout(&base, uap->basep, sizeof(long));
4075 freebsd11_getdents(struct thread *td, struct freebsd11_getdents_args *uap)
4077 struct freebsd11_getdirentries_args ap;
4081 ap.count = uap->count;
4083 return (freebsd11_getdirentries(td, &ap));
4085 #endif /* COMPAT_FREEBSD11 */
4088 * Read a block of directory entries in a filesystem independent format.
4091 sys_getdirentries(struct thread *td, struct getdirentries_args *uap)
4096 error = kern_getdirentries(td, uap->fd, uap->buf, uap->count, &base,
4097 NULL, UIO_USERSPACE);
4100 if (uap->basep != NULL)
4101 error = copyout(&base, uap->basep, sizeof(off_t));
4106 kern_getdirentries(struct thread *td, int fd, char *buf, size_t count,
4107 off_t *basep, ssize_t *residp, enum uio_seg bufseg)
4118 if (count > IOSIZE_MAX)
4120 auio.uio_resid = count;
4121 error = getvnode(td, fd, &cap_read_rights, &fp);
4124 if ((fp->f_flag & FREAD) == 0) {
4129 foffset = foffset_lock(fp, 0);
4131 if (vp->v_type != VDIR) {
4135 aiov.iov_base = buf;
4136 aiov.iov_len = count;
4137 auio.uio_iov = &aiov;
4138 auio.uio_iovcnt = 1;
4139 auio.uio_rw = UIO_READ;
4140 auio.uio_segflg = bufseg;
4142 vn_lock(vp, LK_SHARED | LK_RETRY);
4143 AUDIT_ARG_VNODE1(vp);
4144 loff = auio.uio_offset = foffset;
4146 error = mac_vnode_check_readdir(td->td_ucred, vp);
4149 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL,
4151 foffset = auio.uio_offset;
4156 if (count == auio.uio_resid &&
4157 (vp->v_vflag & VV_ROOT) &&
4158 (vp->v_mount->mnt_flag & MNT_UNION)) {
4159 struct vnode *tvp = vp;
4161 vp = vp->v_mount->mnt_vnodecovered;
4171 *residp = auio.uio_resid;
4172 td->td_retval[0] = count - auio.uio_resid;
4174 foffset_unlock(fp, foffset, 0);
4180 * Set the mode mask for creation of filesystem nodes.
4182 #ifndef _SYS_SYSPROTO_H_
4188 sys_umask(struct thread *td, struct umask_args *uap)
4190 struct filedesc *fdp;
4192 fdp = td->td_proc->p_fd;
4193 FILEDESC_XLOCK(fdp);
4194 td->td_retval[0] = fdp->fd_cmask;
4195 fdp->fd_cmask = uap->newmask & ALLPERMS;
4196 FILEDESC_XUNLOCK(fdp);
4201 * Void all references to file by ripping underlying filesystem away from
4204 #ifndef _SYS_SYSPROTO_H_
4205 struct revoke_args {
4210 sys_revoke(struct thread *td, struct revoke_args *uap)
4214 struct nameidata nd;
4217 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
4219 if ((error = namei(&nd)) != 0)
4222 NDFREE(&nd, NDF_ONLY_PNBUF);
4223 if (vp->v_type != VCHR || vp->v_rdev == NULL) {
4228 error = mac_vnode_check_revoke(td->td_ucred, vp);
4232 error = VOP_GETATTR(vp, &vattr, td->td_ucred);
4235 if (td->td_ucred->cr_uid != vattr.va_uid) {
4236 error = priv_check(td, PRIV_VFS_ADMIN);
4240 if (devfs_usecount(vp) > 0)
4241 VOP_REVOKE(vp, REVOKEALL);
4248 * Convert a user file descriptor to a kernel file entry and check that, if it
4249 * is a capability, the correct rights are present. A reference on the file
4250 * entry is held upon returning.
4253 getvnode(struct thread *td, int fd, cap_rights_t *rightsp, struct file **fpp)
4258 error = fget_unlocked(td->td_proc->p_fd, fd, rightsp, &fp);
4263 * The file could be not of the vnode type, or it may be not
4264 * yet fully initialized, in which case the f_vnode pointer
4265 * may be set, but f_ops is still badfileops. E.g.,
4266 * devfs_open() transiently create such situation to
4267 * facilitate csw d_fdopen().
4269 * Dupfdopen() handling in kern_openat() installs the
4270 * half-baked file into the process descriptor table, allowing
4271 * other thread to dereference it. Guard against the race by
4274 if (fp->f_vnode == NULL || fp->f_ops == &badfileops) {
4283 * Get an (NFS) file handle.
4285 #ifndef _SYS_SYSPROTO_H_
4286 struct lgetfh_args {
4292 sys_lgetfh(struct thread *td, struct lgetfh_args *uap)
4295 return (kern_getfhat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->fname,
4296 UIO_USERSPACE, uap->fhp));
4299 #ifndef _SYS_SYSPROTO_H_
4306 sys_getfh(struct thread *td, struct getfh_args *uap)
4309 return (kern_getfhat(td, 0, AT_FDCWD, uap->fname, UIO_USERSPACE,
4314 * syscall for the rpc.lockd to use to translate an open descriptor into
4315 * a NFS file handle.
4317 * warning: do not remove the priv_check() call or this becomes one giant
4320 #ifndef _SYS_SYSPROTO_H_
4321 struct getfhat_args {
4329 sys_getfhat(struct thread *td, struct getfhat_args *uap)
4332 if ((uap->flags & ~(AT_SYMLINK_NOFOLLOW | AT_BENEATH |
4333 AT_RESOLVE_BENEATH)) != 0)
4335 return (kern_getfhat(td, uap->flags, uap->fd, uap->path, UIO_USERSPACE,
4340 kern_getfhat(struct thread *td, int flags, int fd, const char *path,
4341 enum uio_seg pathseg, fhandle_t *fhp)
4343 struct nameidata nd;
4348 error = priv_check(td, PRIV_VFS_GETFH);
4351 NDINIT_AT(&nd, LOOKUP, at2cnpflags(flags, AT_SYMLINK_NOFOLLOW |
4352 AT_BENEATH | AT_RESOLVE_BENEATH) | LOCKLEAF | AUDITVNODE1,
4353 pathseg, path, fd, td);
4357 NDFREE(&nd, NDF_ONLY_PNBUF);
4359 bzero(&fh, sizeof(fh));
4360 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
4361 error = VOP_VPTOFH(vp, &fh.fh_fid);
4364 error = copyout(&fh, fhp, sizeof (fh));
4368 #ifndef _SYS_SYSPROTO_H_
4369 struct fhlink_args {
4375 sys_fhlink(struct thread *td, struct fhlink_args *uap)
4378 return (kern_fhlinkat(td, AT_FDCWD, uap->to, UIO_USERSPACE, uap->fhp));
4381 #ifndef _SYS_SYSPROTO_H_
4382 struct fhlinkat_args {
4389 sys_fhlinkat(struct thread *td, struct fhlinkat_args *uap)
4392 return (kern_fhlinkat(td, uap->tofd, uap->to, UIO_USERSPACE, uap->fhp));
4396 kern_fhlinkat(struct thread *td, int fd, const char *path,
4397 enum uio_seg pathseg, fhandle_t *fhp)
4404 error = priv_check(td, PRIV_VFS_GETFH);
4407 error = copyin(fhp, &fh, sizeof(fh));
4412 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4414 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_SHARED, &vp);
4419 } while ((error = kern_linkat_vp(td, vp, fd, path, pathseg)) == EAGAIN);
4423 #ifndef _SYS_SYSPROTO_H_
4424 struct fhreadlink_args {
4431 sys_fhreadlink(struct thread *td, struct fhreadlink_args *uap)
4438 error = priv_check(td, PRIV_VFS_GETFH);
4441 if (uap->bufsize > IOSIZE_MAX)
4443 error = copyin(uap->fhp, &fh, sizeof(fh));
4446 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4448 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_SHARED, &vp);
4452 error = kern_readlink_vp(vp, uap->buf, UIO_USERSPACE, uap->bufsize, td);
4458 * syscall for the rpc.lockd to use to translate a NFS file handle into an
4461 * warning: do not remove the priv_check() call or this becomes one giant
4464 #ifndef _SYS_SYSPROTO_H_
4465 struct fhopen_args {
4466 const struct fhandle *u_fhp;
4471 sys_fhopen(struct thread *td, struct fhopen_args *uap)
4480 error = priv_check(td, PRIV_VFS_FHOPEN);
4484 fmode = FFLAGS(uap->flags);
4485 /* why not allow a non-read/write open for our lockd? */
4486 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
4488 error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
4491 /* find the mount point */
4492 mp = vfs_busyfs(&fhp.fh_fsid);
4495 /* now give me my vnode, it gets returned to me locked */
4496 error = VFS_FHTOVP(mp, &fhp.fh_fid, LK_EXCLUSIVE, &vp);
4501 error = falloc_noinstall(td, &fp);
4507 * An extra reference on `fp' has been held for us by
4508 * falloc_noinstall().
4514 error = vn_open_vnode(vp, fmode, td->td_ucred, td, fp);
4516 KASSERT(fp->f_ops == &badfileops,
4517 ("VOP_OPEN in fhopen() set f_ops"));
4518 KASSERT(td->td_dupfd < 0,
4519 ("fhopen() encountered fdopen()"));
4528 finit_vnode(fp, fmode, NULL, &vnops);
4530 if ((fmode & O_TRUNC) != 0) {
4531 error = fo_truncate(fp, 0, td->td_ucred, td);
4536 error = finstall(td, fp, &indx, fmode, NULL);
4539 td->td_retval[0] = indx;
4544 * Stat an (NFS) file handle.
4546 #ifndef _SYS_SYSPROTO_H_
4547 struct fhstat_args {
4548 struct fhandle *u_fhp;
4553 sys_fhstat(struct thread *td, struct fhstat_args *uap)
4559 error = copyin(uap->u_fhp, &fh, sizeof(fh));
4562 error = kern_fhstat(td, fh, &sb);
4564 error = copyout(&sb, uap->sb, sizeof(sb));
4569 kern_fhstat(struct thread *td, struct fhandle fh, struct stat *sb)
4575 error = priv_check(td, PRIV_VFS_FHSTAT);
4578 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4580 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_EXCLUSIVE, &vp);
4584 error = VOP_STAT(vp, sb, td->td_ucred, NOCRED, td);
4590 * Implement fstatfs() for (NFS) file handles.
4592 #ifndef _SYS_SYSPROTO_H_
4593 struct fhstatfs_args {
4594 struct fhandle *u_fhp;
4599 sys_fhstatfs(struct thread *td, struct fhstatfs_args *uap)
4605 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4608 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
4609 error = kern_fhstatfs(td, fh, sfp);
4611 error = copyout(sfp, uap->buf, sizeof(*sfp));
4612 free(sfp, M_STATFS);
4617 kern_fhstatfs(struct thread *td, fhandle_t fh, struct statfs *buf)
4623 error = priv_check(td, PRIV_VFS_FHSTATFS);
4626 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4628 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_EXCLUSIVE, &vp);
4634 error = prison_canseemount(td->td_ucred, mp);
4638 error = mac_mount_check_stat(td->td_ucred, mp);
4642 error = VFS_STATFS(mp, buf);
4649 * Unlike madvise(2), we do not make a best effort to remember every
4650 * possible caching hint. Instead, we remember the last setting with
4651 * the exception that we will allow POSIX_FADV_NORMAL to adjust the
4652 * region of any current setting.
4655 kern_posix_fadvise(struct thread *td, int fd, off_t offset, off_t len,
4658 struct fadvise_info *fa, *new;
4664 if (offset < 0 || len < 0 || offset > OFF_MAX - len)
4666 AUDIT_ARG_VALUE(advice);
4668 case POSIX_FADV_SEQUENTIAL:
4669 case POSIX_FADV_RANDOM:
4670 case POSIX_FADV_NOREUSE:
4671 new = malloc(sizeof(*fa), M_FADVISE, M_WAITOK);
4673 case POSIX_FADV_NORMAL:
4674 case POSIX_FADV_WILLNEED:
4675 case POSIX_FADV_DONTNEED:
4681 /* XXX: CAP_POSIX_FADVISE? */
4683 error = fget(td, fd, &cap_no_rights, &fp);
4686 AUDIT_ARG_FILE(td->td_proc, fp);
4687 if ((fp->f_ops->fo_flags & DFLAG_SEEKABLE) == 0) {
4691 if (fp->f_type != DTYPE_VNODE) {
4696 if (vp->v_type != VREG) {
4703 end = offset + len - 1;
4705 case POSIX_FADV_SEQUENTIAL:
4706 case POSIX_FADV_RANDOM:
4707 case POSIX_FADV_NOREUSE:
4709 * Try to merge any existing non-standard region with
4710 * this new region if possible, otherwise create a new
4711 * non-standard region for this request.
4713 mtx_pool_lock(mtxpool_sleep, fp);
4715 if (fa != NULL && fa->fa_advice == advice &&
4716 ((fa->fa_start <= end && fa->fa_end >= offset) ||
4717 (end != OFF_MAX && fa->fa_start == end + 1) ||
4718 (fa->fa_end != OFF_MAX && fa->fa_end + 1 == offset))) {
4719 if (offset < fa->fa_start)
4720 fa->fa_start = offset;
4721 if (end > fa->fa_end)
4724 new->fa_advice = advice;
4725 new->fa_start = offset;
4730 mtx_pool_unlock(mtxpool_sleep, fp);
4732 case POSIX_FADV_NORMAL:
4734 * If a the "normal" region overlaps with an existing
4735 * non-standard region, trim or remove the
4736 * non-standard region.
4738 mtx_pool_lock(mtxpool_sleep, fp);
4741 if (offset <= fa->fa_start && end >= fa->fa_end) {
4743 fp->f_advice = NULL;
4744 } else if (offset <= fa->fa_start &&
4745 end >= fa->fa_start)
4746 fa->fa_start = end + 1;
4747 else if (offset <= fa->fa_end && end >= fa->fa_end)
4748 fa->fa_end = offset - 1;
4749 else if (offset >= fa->fa_start && end <= fa->fa_end) {
4751 * If the "normal" region is a middle
4752 * portion of the existing
4753 * non-standard region, just remove
4754 * the whole thing rather than picking
4755 * one side or the other to
4759 fp->f_advice = NULL;
4762 mtx_pool_unlock(mtxpool_sleep, fp);
4764 case POSIX_FADV_WILLNEED:
4765 case POSIX_FADV_DONTNEED:
4766 error = VOP_ADVISE(vp, offset, end, advice);
4772 free(new, M_FADVISE);
4777 sys_posix_fadvise(struct thread *td, struct posix_fadvise_args *uap)
4781 error = kern_posix_fadvise(td, uap->fd, uap->offset, uap->len,
4783 return (kern_posix_error(td, error));
4787 kern_copy_file_range(struct thread *td, int infd, off_t *inoffp, int outfd,
4788 off_t *outoffp, size_t len, unsigned int flags)
4790 struct file *infp, *outfp;
4791 struct vnode *invp, *outvp;
4794 void *rl_rcookie, *rl_wcookie;
4795 off_t savinoff, savoutoff;
4797 infp = outfp = NULL;
4798 rl_rcookie = rl_wcookie = NULL;
4807 if (len > SSIZE_MAX)
4809 * Although the len argument is size_t, the return argument
4810 * is ssize_t (which is signed). Therefore a size that won't
4811 * fit in ssize_t can't be returned.
4815 /* Get the file structures for the file descriptors. */
4816 error = fget_read(td, infd, &cap_read_rights, &infp);
4819 if (infp->f_ops == &badfileops) {
4823 if (infp->f_vnode == NULL) {
4827 error = fget_write(td, outfd, &cap_write_rights, &outfp);
4830 if (outfp->f_ops == &badfileops) {
4834 if (outfp->f_vnode == NULL) {
4839 /* Set the offset pointers to the correct place. */
4841 inoffp = &infp->f_offset;
4842 if (outoffp == NULL)
4843 outoffp = &outfp->f_offset;
4845 savoutoff = *outoffp;
4847 invp = infp->f_vnode;
4848 outvp = outfp->f_vnode;
4849 /* Sanity check the f_flag bits. */
4850 if ((outfp->f_flag & (FWRITE | FAPPEND)) != FWRITE ||
4851 (infp->f_flag & FREAD) == 0) {
4856 /* If len == 0, just return 0. */
4861 * If infp and outfp refer to the same file, the byte ranges cannot
4864 if (invp == outvp && ((savinoff <= savoutoff && savinoff + len >
4865 savoutoff) || (savinoff > savoutoff && savoutoff + len >
4871 /* Range lock the byte ranges for both invp and outvp. */
4873 rl_wcookie = vn_rangelock_wlock(outvp, *outoffp, *outoffp +
4875 rl_rcookie = vn_rangelock_tryrlock(invp, *inoffp, *inoffp +
4877 if (rl_rcookie != NULL)
4879 vn_rangelock_unlock(outvp, rl_wcookie);
4880 rl_rcookie = vn_rangelock_rlock(invp, *inoffp, *inoffp + len);
4881 vn_rangelock_unlock(invp, rl_rcookie);
4885 error = vn_copy_file_range(invp, inoffp, outvp, outoffp, &retlen,
4886 flags, infp->f_cred, outfp->f_cred, td);
4888 if (rl_rcookie != NULL)
4889 vn_rangelock_unlock(invp, rl_rcookie);
4890 if (rl_wcookie != NULL)
4891 vn_rangelock_unlock(outvp, rl_wcookie);
4892 if (savinoff != -1 && (error == EINTR || error == ERESTART)) {
4894 *outoffp = savoutoff;
4900 td->td_retval[0] = retlen;
4905 sys_copy_file_range(struct thread *td, struct copy_file_range_args *uap)
4907 off_t inoff, outoff, *inoffp, *outoffp;
4910 inoffp = outoffp = NULL;
4911 if (uap->inoffp != NULL) {
4912 error = copyin(uap->inoffp, &inoff, sizeof(off_t));
4917 if (uap->outoffp != NULL) {
4918 error = copyin(uap->outoffp, &outoff, sizeof(off_t));
4923 error = kern_copy_file_range(td, uap->infd, inoffp, uap->outfd,
4924 outoffp, uap->len, uap->flags);
4925 if (error == 0 && uap->inoffp != NULL)
4926 error = copyout(inoffp, uap->inoffp, sizeof(off_t));
4927 if (error == 0 && uap->outoffp != NULL)
4928 error = copyout(outoffp, uap->outoffp, sizeof(off_t));