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);
1387 if (error == ERELOOKUP)
1393 * Create a named pipe.
1395 #ifndef _SYS_SYSPROTO_H_
1396 struct mkfifo_args {
1402 sys_mkfifo(struct thread *td, struct mkfifo_args *uap)
1405 return (kern_mkfifoat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1409 #ifndef _SYS_SYSPROTO_H_
1410 struct mkfifoat_args {
1417 sys_mkfifoat(struct thread *td, struct mkfifoat_args *uap)
1420 return (kern_mkfifoat(td, uap->fd, uap->path, UIO_USERSPACE,
1425 kern_mkfifoat(struct thread *td, int fd, const char *path,
1426 enum uio_seg pathseg, int mode)
1430 struct nameidata nd;
1433 AUDIT_ARG_MODE(mode);
1436 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
1437 NOCACHE, pathseg, path, fd, &cap_mkfifoat_rights,
1439 if ((error = namei(&nd)) != 0)
1441 if (nd.ni_vp != NULL) {
1442 NDFREE(&nd, NDF_ONLY_PNBUF);
1443 if (nd.ni_vp == nd.ni_dvp)
1450 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1451 NDFREE(&nd, NDF_ONLY_PNBUF);
1453 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1458 vattr.va_type = VFIFO;
1459 vattr.va_mode = (mode & ALLPERMS) & ~td->td_proc->p_fd->fd_cmask;
1461 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1466 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
1473 vn_finished_write(mp);
1474 NDFREE(&nd, NDF_ONLY_PNBUF);
1475 if (error == ERELOOKUP)
1481 * Make a hard file link.
1483 #ifndef _SYS_SYSPROTO_H_
1490 sys_link(struct thread *td, struct link_args *uap)
1493 return (kern_linkat(td, AT_FDCWD, AT_FDCWD, uap->path, uap->link,
1494 UIO_USERSPACE, FOLLOW));
1497 #ifndef _SYS_SYSPROTO_H_
1498 struct linkat_args {
1507 sys_linkat(struct thread *td, struct linkat_args *uap)
1512 if ((flag & ~(AT_SYMLINK_FOLLOW | AT_BENEATH |
1513 AT_RESOLVE_BENEATH)) != 0)
1516 return (kern_linkat(td, uap->fd1, uap->fd2, uap->path1, uap->path2,
1517 UIO_USERSPACE, at2cnpflags(flag, AT_SYMLINK_FOLLOW | AT_BENEATH |
1518 AT_RESOLVE_BENEATH)));
1521 int hardlink_check_uid = 0;
1522 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
1523 &hardlink_check_uid, 0,
1524 "Unprivileged processes cannot create hard links to files owned by other "
1526 static int hardlink_check_gid = 0;
1527 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
1528 &hardlink_check_gid, 0,
1529 "Unprivileged processes cannot create hard links to files owned by other "
1533 can_hardlink(struct vnode *vp, struct ucred *cred)
1538 if (!hardlink_check_uid && !hardlink_check_gid)
1541 error = VOP_GETATTR(vp, &va, cred);
1545 if (hardlink_check_uid && cred->cr_uid != va.va_uid) {
1546 error = priv_check_cred(cred, PRIV_VFS_LINK);
1551 if (hardlink_check_gid && !groupmember(va.va_gid, cred)) {
1552 error = priv_check_cred(cred, PRIV_VFS_LINK);
1561 kern_linkat(struct thread *td, int fd1, int fd2, const char *path1,
1562 const char *path2, enum uio_seg segflag, int follow)
1564 struct nameidata nd;
1569 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, segflag,
1570 path1, fd1, &cap_linkat_source_rights, td);
1571 if ((error = namei(&nd)) != 0)
1573 NDFREE(&nd, NDF_ONLY_PNBUF);
1574 error = kern_linkat_vp(td, nd.ni_vp, fd2, path2, segflag);
1575 } while (error == EAGAIN || error == ERELOOKUP);
1580 kern_linkat_vp(struct thread *td, struct vnode *vp, int fd, const char *path,
1581 enum uio_seg segflag)
1583 struct nameidata nd;
1587 if (vp->v_type == VDIR) {
1589 return (EPERM); /* POSIX */
1591 NDINIT_ATRIGHTS(&nd, CREATE,
1592 LOCKPARENT | SAVENAME | AUDITVNODE2 | NOCACHE, segflag, path, fd,
1593 &cap_linkat_target_rights, td);
1594 if ((error = namei(&nd)) == 0) {
1595 if (nd.ni_vp != NULL) {
1596 NDFREE(&nd, NDF_ONLY_PNBUF);
1597 if (nd.ni_dvp == nd.ni_vp)
1604 } else if (nd.ni_dvp->v_mount != vp->v_mount) {
1606 * Cross-device link. No need to recheck
1607 * vp->v_type, since it cannot change, except
1610 NDFREE(&nd, NDF_ONLY_PNBUF);
1614 } else if ((error = vn_lock(vp, LK_EXCLUSIVE)) == 0) {
1615 error = can_hardlink(vp, td->td_ucred);
1618 error = mac_vnode_check_link(td->td_ucred,
1619 nd.ni_dvp, vp, &nd.ni_cnd);
1624 NDFREE(&nd, NDF_ONLY_PNBUF);
1627 error = vn_start_write(vp, &mp, V_NOWAIT);
1631 NDFREE(&nd, NDF_ONLY_PNBUF);
1632 error = vn_start_write(NULL, &mp,
1638 error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
1641 vn_finished_write(mp);
1642 NDFREE(&nd, NDF_ONLY_PNBUF);
1645 NDFREE(&nd, NDF_ONLY_PNBUF);
1655 * Make a symbolic link.
1657 #ifndef _SYS_SYSPROTO_H_
1658 struct symlink_args {
1664 sys_symlink(struct thread *td, struct symlink_args *uap)
1667 return (kern_symlinkat(td, uap->path, AT_FDCWD, uap->link,
1671 #ifndef _SYS_SYSPROTO_H_
1672 struct symlinkat_args {
1679 sys_symlinkat(struct thread *td, struct symlinkat_args *uap)
1682 return (kern_symlinkat(td, uap->path1, uap->fd, uap->path2,
1687 kern_symlinkat(struct thread *td, const char *path1, int fd, const char *path2,
1688 enum uio_seg segflg)
1692 const char *syspath;
1694 struct nameidata nd;
1697 if (segflg == UIO_SYSSPACE) {
1700 tmppath = uma_zalloc(namei_zone, M_WAITOK);
1701 if ((error = copyinstr(path1, tmppath, MAXPATHLEN, NULL)) != 0)
1705 AUDIT_ARG_TEXT(syspath);
1708 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
1709 NOCACHE, segflg, path2, fd, &cap_symlinkat_rights,
1711 if ((error = namei(&nd)) != 0)
1714 NDFREE(&nd, NDF_ONLY_PNBUF);
1715 if (nd.ni_vp == nd.ni_dvp)
1723 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1724 NDFREE(&nd, NDF_ONLY_PNBUF);
1726 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1731 vattr.va_mode = ACCESSPERMS &~ td->td_proc->p_fd->fd_cmask;
1733 vattr.va_type = VLNK;
1734 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1739 error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr, syspath);
1745 NDFREE(&nd, NDF_ONLY_PNBUF);
1747 vn_finished_write(mp);
1748 if (error == ERELOOKUP)
1751 if (segflg != UIO_SYSSPACE)
1752 uma_zfree(namei_zone, tmppath);
1757 * Delete a whiteout from the filesystem.
1759 #ifndef _SYS_SYSPROTO_H_
1760 struct undelete_args {
1765 sys_undelete(struct thread *td, struct undelete_args *uap)
1768 struct nameidata nd;
1773 NDINIT(&nd, DELETE, LOCKPARENT | DOWHITEOUT | AUDITVNODE1,
1774 UIO_USERSPACE, uap->path, td);
1779 if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & ISWHITEOUT)) {
1780 NDFREE(&nd, NDF_ONLY_PNBUF);
1781 if (nd.ni_vp == nd.ni_dvp)
1789 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1790 NDFREE(&nd, NDF_ONLY_PNBUF);
1792 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1796 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, DELETE);
1797 NDFREE(&nd, NDF_ONLY_PNBUF);
1799 vn_finished_write(mp);
1800 if (error == ERELOOKUP)
1806 * Delete a name from the filesystem.
1808 #ifndef _SYS_SYSPROTO_H_
1809 struct unlink_args {
1814 sys_unlink(struct thread *td, struct unlink_args *uap)
1817 return (kern_funlinkat(td, AT_FDCWD, uap->path, FD_NONE, UIO_USERSPACE,
1822 kern_funlinkat_ex(struct thread *td, int dfd, const char *path, int fd,
1823 int flag, enum uio_seg pathseg, ino_t oldinum)
1826 if ((flag & ~AT_REMOVEDIR) != 0)
1829 if ((flag & AT_REMOVEDIR) != 0)
1830 return (kern_frmdirat(td, dfd, path, fd, UIO_USERSPACE, 0));
1832 return (kern_funlinkat(td, dfd, path, fd, UIO_USERSPACE, 0, 0));
1835 #ifndef _SYS_SYSPROTO_H_
1836 struct unlinkat_args {
1843 sys_unlinkat(struct thread *td, struct unlinkat_args *uap)
1846 return (kern_funlinkat_ex(td, uap->fd, uap->path, FD_NONE, uap->flag,
1850 #ifndef _SYS_SYSPROTO_H_
1851 struct funlinkat_args {
1859 sys_funlinkat(struct thread *td, struct funlinkat_args *uap)
1862 return (kern_funlinkat_ex(td, uap->dfd, uap->path, uap->fd, uap->flag,
1867 kern_funlinkat(struct thread *td, int dfd, const char *path, int fd,
1868 enum uio_seg pathseg, int flag, ino_t oldinum)
1873 struct nameidata nd;
1878 if (fd != FD_NONE) {
1879 error = getvnode(td, fd, &cap_no_rights, &fp);
1886 NDINIT_ATRIGHTS(&nd, DELETE, LOCKPARENT | LOCKLEAF | AUDITVNODE1 |
1887 at2cnpflags(flag, AT_BENEATH | AT_RESOLVE_BENEATH),
1888 pathseg, path, dfd, &cap_unlinkat_rights, td);
1889 if ((error = namei(&nd)) != 0) {
1890 if (error == EINVAL)
1895 if (vp->v_type == VDIR && oldinum == 0) {
1896 error = EPERM; /* POSIX */
1897 } else if (oldinum != 0 &&
1898 ((error = VOP_STAT(vp, &sb, td->td_ucred, NOCRED, td)) == 0) &&
1899 sb.st_ino != oldinum) {
1900 error = EIDRM; /* Identifier removed */
1901 } else if (fp != NULL && fp->f_vnode != vp) {
1902 if (VN_IS_DOOMED(fp->f_vnode))
1908 * The root of a mounted filesystem cannot be deleted.
1910 * XXX: can this only be a VDIR case?
1912 if (vp->v_vflag & VV_ROOT)
1916 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1917 NDFREE(&nd, NDF_ONLY_PNBUF);
1919 if (vp == nd.ni_dvp)
1923 if ((error = vn_start_write(NULL, &mp,
1924 V_XSLEEP | PCATCH)) != 0) {
1930 error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp,
1935 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_UNLINK);
1936 error = VOP_REMOVE(nd.ni_dvp, vp, &nd.ni_cnd);
1940 vn_finished_write(mp);
1942 NDFREE(&nd, NDF_ONLY_PNBUF);
1944 if (vp == nd.ni_dvp)
1948 if (error == ERELOOKUP)
1957 * Reposition read/write file offset.
1959 #ifndef _SYS_SYSPROTO_H_
1968 sys_lseek(struct thread *td, struct lseek_args *uap)
1971 return (kern_lseek(td, uap->fd, uap->offset, uap->whence));
1975 kern_lseek(struct thread *td, int fd, off_t offset, int whence)
1981 error = fget(td, fd, &cap_seek_rights, &fp);
1984 error = (fp->f_ops->fo_flags & DFLAG_SEEKABLE) != 0 ?
1985 fo_seek(fp, offset, whence, td) : ESPIPE;
1990 #if defined(COMPAT_43)
1992 * Reposition read/write file offset.
1994 #ifndef _SYS_SYSPROTO_H_
1995 struct olseek_args {
2002 olseek(struct thread *td, struct olseek_args *uap)
2005 return (kern_lseek(td, uap->fd, uap->offset, uap->whence));
2007 #endif /* COMPAT_43 */
2009 #if defined(COMPAT_FREEBSD6)
2010 /* Version with the 'pad' argument */
2012 freebsd6_lseek(struct thread *td, struct freebsd6_lseek_args *uap)
2015 return (kern_lseek(td, uap->fd, uap->offset, uap->whence));
2020 * Check access permissions using passed credentials.
2023 vn_access(struct vnode *vp, int user_flags, struct ucred *cred,
2029 /* Flags == 0 means only check for existence. */
2030 if (user_flags == 0)
2034 if (user_flags & R_OK)
2036 if (user_flags & W_OK)
2038 if (user_flags & X_OK)
2041 error = mac_vnode_check_access(cred, vp, accmode);
2045 if ((accmode & VWRITE) == 0 || (error = vn_writechk(vp)) == 0)
2046 error = VOP_ACCESS(vp, accmode, cred, td);
2051 * Check access permissions using "real" credentials.
2053 #ifndef _SYS_SYSPROTO_H_
2054 struct access_args {
2060 sys_access(struct thread *td, struct access_args *uap)
2063 return (kern_accessat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2067 #ifndef _SYS_SYSPROTO_H_
2068 struct faccessat_args {
2076 sys_faccessat(struct thread *td, struct faccessat_args *uap)
2079 return (kern_accessat(td, uap->fd, uap->path, UIO_USERSPACE, uap->flag,
2084 kern_accessat(struct thread *td, int fd, const char *path,
2085 enum uio_seg pathseg, int flag, int amode)
2087 struct ucred *cred, *usecred;
2089 struct nameidata nd;
2092 if ((flag & ~(AT_EACCESS | AT_BENEATH | AT_RESOLVE_BENEATH)) != 0)
2094 if (amode != F_OK && (amode & ~(R_OK | W_OK | X_OK)) != 0)
2098 * Create and modify a temporary credential instead of one that
2099 * is potentially shared (if we need one).
2101 cred = td->td_ucred;
2102 if ((flag & AT_EACCESS) == 0 &&
2103 ((cred->cr_uid != cred->cr_ruid ||
2104 cred->cr_rgid != cred->cr_groups[0]))) {
2105 usecred = crdup(cred);
2106 usecred->cr_uid = cred->cr_ruid;
2107 usecred->cr_groups[0] = cred->cr_rgid;
2108 td->td_ucred = usecred;
2111 AUDIT_ARG_VALUE(amode);
2112 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF |
2113 AUDITVNODE1 | at2cnpflags(flag, AT_BENEATH | AT_RESOLVE_BENEATH),
2114 pathseg, path, fd, &cap_fstat_rights, td);
2115 if ((error = namei(&nd)) != 0)
2119 error = vn_access(vp, amode, usecred, td);
2120 NDFREE(&nd, NDF_ONLY_PNBUF);
2123 if (usecred != cred) {
2124 td->td_ucred = cred;
2131 * Check access permissions using "effective" credentials.
2133 #ifndef _SYS_SYSPROTO_H_
2134 struct eaccess_args {
2140 sys_eaccess(struct thread *td, struct eaccess_args *uap)
2143 return (kern_accessat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2144 AT_EACCESS, uap->amode));
2147 #if defined(COMPAT_43)
2149 * Get file status; this version follows links.
2151 #ifndef _SYS_SYSPROTO_H_
2158 ostat(struct thread *td, struct ostat_args *uap)
2164 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
2169 return (copyout(&osb, uap->ub, sizeof (osb)));
2173 * Get file status; this version does not follow links.
2175 #ifndef _SYS_SYSPROTO_H_
2176 struct olstat_args {
2182 olstat(struct thread *td, struct olstat_args *uap)
2188 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2189 UIO_USERSPACE, &sb, NULL);
2193 return (copyout(&osb, uap->ub, sizeof (osb)));
2197 * Convert from an old to a new stat structure.
2198 * XXX: many values are blindly truncated.
2201 cvtstat(struct stat *st, struct ostat *ost)
2204 bzero(ost, sizeof(*ost));
2205 ost->st_dev = st->st_dev;
2206 ost->st_ino = st->st_ino;
2207 ost->st_mode = st->st_mode;
2208 ost->st_nlink = st->st_nlink;
2209 ost->st_uid = st->st_uid;
2210 ost->st_gid = st->st_gid;
2211 ost->st_rdev = st->st_rdev;
2212 ost->st_size = MIN(st->st_size, INT32_MAX);
2213 ost->st_atim = st->st_atim;
2214 ost->st_mtim = st->st_mtim;
2215 ost->st_ctim = st->st_ctim;
2216 ost->st_blksize = st->st_blksize;
2217 ost->st_blocks = st->st_blocks;
2218 ost->st_flags = st->st_flags;
2219 ost->st_gen = st->st_gen;
2221 #endif /* COMPAT_43 */
2223 #if defined(COMPAT_43) || defined(COMPAT_FREEBSD11)
2224 int ino64_trunc_error;
2225 SYSCTL_INT(_vfs, OID_AUTO, ino64_trunc_error, CTLFLAG_RW,
2226 &ino64_trunc_error, 0,
2227 "Error on truncation of device, file or inode number, or link count");
2230 freebsd11_cvtstat(struct stat *st, struct freebsd11_stat *ost)
2233 ost->st_dev = st->st_dev;
2234 if (ost->st_dev != st->st_dev) {
2235 switch (ino64_trunc_error) {
2238 * Since dev_t is almost raw, don't clamp to the
2239 * maximum for case 2, but ignore the error.
2246 ost->st_ino = st->st_ino;
2247 if (ost->st_ino != st->st_ino) {
2248 switch (ino64_trunc_error) {
2255 ost->st_ino = UINT32_MAX;
2259 ost->st_mode = st->st_mode;
2260 ost->st_nlink = st->st_nlink;
2261 if (ost->st_nlink != st->st_nlink) {
2262 switch (ino64_trunc_error) {
2269 ost->st_nlink = UINT16_MAX;
2273 ost->st_uid = st->st_uid;
2274 ost->st_gid = st->st_gid;
2275 ost->st_rdev = st->st_rdev;
2276 if (ost->st_rdev != st->st_rdev) {
2277 switch (ino64_trunc_error) {
2284 ost->st_atim = st->st_atim;
2285 ost->st_mtim = st->st_mtim;
2286 ost->st_ctim = st->st_ctim;
2287 ost->st_size = st->st_size;
2288 ost->st_blocks = st->st_blocks;
2289 ost->st_blksize = st->st_blksize;
2290 ost->st_flags = st->st_flags;
2291 ost->st_gen = st->st_gen;
2293 ost->st_birthtim = st->st_birthtim;
2294 bzero((char *)&ost->st_birthtim + sizeof(ost->st_birthtim),
2295 sizeof(*ost) - offsetof(struct freebsd11_stat,
2296 st_birthtim) - sizeof(ost->st_birthtim));
2301 freebsd11_stat(struct thread *td, struct freebsd11_stat_args* uap)
2304 struct freebsd11_stat osb;
2307 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
2311 error = freebsd11_cvtstat(&sb, &osb);
2313 error = copyout(&osb, uap->ub, sizeof(osb));
2318 freebsd11_lstat(struct thread *td, struct freebsd11_lstat_args* uap)
2321 struct freebsd11_stat osb;
2324 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2325 UIO_USERSPACE, &sb, NULL);
2328 error = freebsd11_cvtstat(&sb, &osb);
2330 error = copyout(&osb, uap->ub, sizeof(osb));
2335 freebsd11_fhstat(struct thread *td, struct freebsd11_fhstat_args* uap)
2339 struct freebsd11_stat osb;
2342 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
2345 error = kern_fhstat(td, fh, &sb);
2348 error = freebsd11_cvtstat(&sb, &osb);
2350 error = copyout(&osb, uap->sb, sizeof(osb));
2355 freebsd11_fstatat(struct thread *td, struct freebsd11_fstatat_args* uap)
2358 struct freebsd11_stat osb;
2361 error = kern_statat(td, uap->flag, uap->fd, uap->path,
2362 UIO_USERSPACE, &sb, NULL);
2365 error = freebsd11_cvtstat(&sb, &osb);
2367 error = copyout(&osb, uap->buf, sizeof(osb));
2370 #endif /* COMPAT_FREEBSD11 */
2375 #ifndef _SYS_SYSPROTO_H_
2376 struct fstatat_args {
2384 sys_fstatat(struct thread *td, struct fstatat_args *uap)
2389 error = kern_statat(td, uap->flag, uap->fd, uap->path,
2390 UIO_USERSPACE, &sb, NULL);
2392 error = copyout(&sb, uap->buf, sizeof (sb));
2397 kern_statat(struct thread *td, int flag, int fd, const char *path,
2398 enum uio_seg pathseg, struct stat *sbp,
2399 void (*hook)(struct vnode *vp, struct stat *sbp))
2401 struct nameidata nd;
2404 if ((flag & ~(AT_SYMLINK_NOFOLLOW | AT_BENEATH |
2405 AT_RESOLVE_BENEATH)) != 0)
2408 NDINIT_ATRIGHTS(&nd, LOOKUP, at2cnpflags(flag, AT_BENEATH |
2409 AT_RESOLVE_BENEATH | AT_SYMLINK_NOFOLLOW) | LOCKSHARED | LOCKLEAF |
2410 AUDITVNODE1, pathseg, path, fd, &cap_fstat_rights, td);
2412 if ((error = namei(&nd)) != 0)
2414 error = VOP_STAT(nd.ni_vp, sbp, td->td_ucred, NOCRED, td);
2416 if (__predict_false(hook != NULL))
2417 hook(nd.ni_vp, sbp);
2419 NDFREE(&nd, NDF_ONLY_PNBUF);
2421 #ifdef __STAT_TIME_T_EXT
2422 sbp->st_atim_ext = 0;
2423 sbp->st_mtim_ext = 0;
2424 sbp->st_ctim_ext = 0;
2425 sbp->st_btim_ext = 0;
2428 if (KTRPOINT(td, KTR_STRUCT))
2429 ktrstat_error(sbp, error);
2434 #if defined(COMPAT_FREEBSD11)
2436 * Implementation of the NetBSD [l]stat() functions.
2439 freebsd11_cvtnstat(struct stat *sb, struct nstat *nsb)
2442 bzero(nsb, sizeof(*nsb));
2443 nsb->st_dev = sb->st_dev;
2444 nsb->st_ino = sb->st_ino;
2445 nsb->st_mode = sb->st_mode;
2446 nsb->st_nlink = sb->st_nlink;
2447 nsb->st_uid = sb->st_uid;
2448 nsb->st_gid = sb->st_gid;
2449 nsb->st_rdev = sb->st_rdev;
2450 nsb->st_atim = sb->st_atim;
2451 nsb->st_mtim = sb->st_mtim;
2452 nsb->st_ctim = sb->st_ctim;
2453 nsb->st_size = sb->st_size;
2454 nsb->st_blocks = sb->st_blocks;
2455 nsb->st_blksize = sb->st_blksize;
2456 nsb->st_flags = sb->st_flags;
2457 nsb->st_gen = sb->st_gen;
2458 nsb->st_birthtim = sb->st_birthtim;
2461 #ifndef _SYS_SYSPROTO_H_
2462 struct freebsd11_nstat_args {
2468 freebsd11_nstat(struct thread *td, struct freebsd11_nstat_args *uap)
2474 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
2478 freebsd11_cvtnstat(&sb, &nsb);
2479 return (copyout(&nsb, uap->ub, sizeof (nsb)));
2483 * NetBSD lstat. Get file status; this version does not follow links.
2485 #ifndef _SYS_SYSPROTO_H_
2486 struct freebsd11_nlstat_args {
2492 freebsd11_nlstat(struct thread *td, struct freebsd11_nlstat_args *uap)
2498 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2499 UIO_USERSPACE, &sb, NULL);
2502 freebsd11_cvtnstat(&sb, &nsb);
2503 return (copyout(&nsb, uap->ub, sizeof (nsb)));
2505 #endif /* COMPAT_FREEBSD11 */
2508 * Get configurable pathname variables.
2510 #ifndef _SYS_SYSPROTO_H_
2511 struct pathconf_args {
2517 sys_pathconf(struct thread *td, struct pathconf_args *uap)
2522 error = kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name, FOLLOW,
2525 td->td_retval[0] = value;
2529 #ifndef _SYS_SYSPROTO_H_
2530 struct lpathconf_args {
2536 sys_lpathconf(struct thread *td, struct lpathconf_args *uap)
2541 error = kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name,
2544 td->td_retval[0] = value;
2549 kern_pathconf(struct thread *td, const char *path, enum uio_seg pathseg,
2550 int name, u_long flags, long *valuep)
2552 struct nameidata nd;
2555 NDINIT(&nd, LOOKUP, LOCKSHARED | LOCKLEAF | AUDITVNODE1 | flags,
2557 if ((error = namei(&nd)) != 0)
2559 NDFREE(&nd, NDF_ONLY_PNBUF);
2561 error = VOP_PATHCONF(nd.ni_vp, name, valuep);
2567 * Return target name of a symbolic link.
2569 #ifndef _SYS_SYSPROTO_H_
2570 struct readlink_args {
2577 sys_readlink(struct thread *td, struct readlink_args *uap)
2580 return (kern_readlinkat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2581 uap->buf, UIO_USERSPACE, uap->count));
2583 #ifndef _SYS_SYSPROTO_H_
2584 struct readlinkat_args {
2592 sys_readlinkat(struct thread *td, struct readlinkat_args *uap)
2595 return (kern_readlinkat(td, uap->fd, uap->path, UIO_USERSPACE,
2596 uap->buf, UIO_USERSPACE, uap->bufsize));
2600 kern_readlinkat(struct thread *td, int fd, const char *path,
2601 enum uio_seg pathseg, char *buf, enum uio_seg bufseg, size_t count)
2604 struct nameidata nd;
2607 if (count > IOSIZE_MAX)
2610 NDINIT_AT(&nd, LOOKUP, NOFOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
2611 pathseg, path, fd, td);
2613 if ((error = namei(&nd)) != 0)
2615 NDFREE(&nd, NDF_ONLY_PNBUF);
2618 error = kern_readlink_vp(vp, buf, bufseg, count, td);
2625 * Helper function to readlink from a vnode
2628 kern_readlink_vp(struct vnode *vp, char *buf, enum uio_seg bufseg, size_t count,
2635 ASSERT_VOP_LOCKED(vp, "kern_readlink_vp(): vp not locked");
2637 error = mac_vnode_check_readlink(td->td_ucred, vp);
2641 if (vp->v_type != VLNK && (vp->v_vflag & VV_READLINK) == 0)
2644 aiov.iov_base = buf;
2645 aiov.iov_len = count;
2646 auio.uio_iov = &aiov;
2647 auio.uio_iovcnt = 1;
2648 auio.uio_offset = 0;
2649 auio.uio_rw = UIO_READ;
2650 auio.uio_segflg = bufseg;
2652 auio.uio_resid = count;
2653 error = VOP_READLINK(vp, &auio, td->td_ucred);
2654 td->td_retval[0] = count - auio.uio_resid;
2659 * Common implementation code for chflags() and fchflags().
2662 setfflags(struct thread *td, struct vnode *vp, u_long flags)
2668 /* We can't support the value matching VNOVAL. */
2669 if (flags == VNOVAL)
2670 return (EOPNOTSUPP);
2673 * Prevent non-root users from setting flags on devices. When
2674 * a device is reused, users can retain ownership of the device
2675 * if they are allowed to set flags and programs assume that
2676 * chown can't fail when done as root.
2678 if (vp->v_type == VCHR || vp->v_type == VBLK) {
2679 error = priv_check(td, PRIV_VFS_CHFLAGS_DEV);
2684 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2687 vattr.va_flags = flags;
2688 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2690 error = mac_vnode_check_setflags(td->td_ucred, vp, vattr.va_flags);
2693 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
2695 vn_finished_write(mp);
2700 * Change flags of a file given a path name.
2702 #ifndef _SYS_SYSPROTO_H_
2703 struct chflags_args {
2709 sys_chflags(struct thread *td, struct chflags_args *uap)
2712 return (kern_chflagsat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2716 #ifndef _SYS_SYSPROTO_H_
2717 struct chflagsat_args {
2725 sys_chflagsat(struct thread *td, struct chflagsat_args *uap)
2728 if ((uap->atflag & ~(AT_SYMLINK_NOFOLLOW | AT_BENEATH |
2729 AT_RESOLVE_BENEATH)) != 0)
2732 return (kern_chflagsat(td, uap->fd, uap->path, UIO_USERSPACE,
2733 uap->flags, uap->atflag));
2737 * Same as chflags() but doesn't follow symlinks.
2739 #ifndef _SYS_SYSPROTO_H_
2740 struct lchflags_args {
2746 sys_lchflags(struct thread *td, struct lchflags_args *uap)
2749 return (kern_chflagsat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2750 uap->flags, AT_SYMLINK_NOFOLLOW));
2754 kern_chflagsat(struct thread *td, int fd, const char *path,
2755 enum uio_seg pathseg, u_long flags, int atflag)
2757 struct nameidata nd;
2760 AUDIT_ARG_FFLAGS(flags);
2761 NDINIT_ATRIGHTS(&nd, LOOKUP, at2cnpflags(atflag, AT_SYMLINK_NOFOLLOW |
2762 AT_BENEATH | AT_RESOLVE_BENEATH) | AUDITVNODE1, pathseg, path, fd,
2763 &cap_fchflags_rights, td);
2764 if ((error = namei(&nd)) != 0)
2766 NDFREE(&nd, NDF_ONLY_PNBUF);
2767 error = setfflags(td, nd.ni_vp, flags);
2773 * Change flags of a file given a file descriptor.
2775 #ifndef _SYS_SYSPROTO_H_
2776 struct fchflags_args {
2782 sys_fchflags(struct thread *td, struct fchflags_args *uap)
2787 AUDIT_ARG_FD(uap->fd);
2788 AUDIT_ARG_FFLAGS(uap->flags);
2789 error = getvnode(td, uap->fd, &cap_fchflags_rights,
2794 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
2795 AUDIT_ARG_VNODE1(fp->f_vnode);
2796 VOP_UNLOCK(fp->f_vnode);
2798 error = setfflags(td, fp->f_vnode, uap->flags);
2804 * Common implementation code for chmod(), lchmod() and fchmod().
2807 setfmode(struct thread *td, struct ucred *cred, struct vnode *vp, int mode)
2813 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2815 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2817 vattr.va_mode = mode & ALLPERMS;
2819 error = mac_vnode_check_setmode(cred, vp, vattr.va_mode);
2822 error = VOP_SETATTR(vp, &vattr, cred);
2824 vn_finished_write(mp);
2829 * Change mode of a file given path name.
2831 #ifndef _SYS_SYSPROTO_H_
2838 sys_chmod(struct thread *td, struct chmod_args *uap)
2841 return (kern_fchmodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2845 #ifndef _SYS_SYSPROTO_H_
2846 struct fchmodat_args {
2854 sys_fchmodat(struct thread *td, struct fchmodat_args *uap)
2857 if ((uap->flag & ~(AT_SYMLINK_NOFOLLOW | AT_BENEATH |
2858 AT_RESOLVE_BENEATH)) != 0)
2861 return (kern_fchmodat(td, uap->fd, uap->path, UIO_USERSPACE,
2862 uap->mode, uap->flag));
2866 * Change mode of a file given path name (don't follow links.)
2868 #ifndef _SYS_SYSPROTO_H_
2869 struct lchmod_args {
2875 sys_lchmod(struct thread *td, struct lchmod_args *uap)
2878 return (kern_fchmodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2879 uap->mode, AT_SYMLINK_NOFOLLOW));
2883 kern_fchmodat(struct thread *td, int fd, const char *path,
2884 enum uio_seg pathseg, mode_t mode, int flag)
2886 struct nameidata nd;
2889 AUDIT_ARG_MODE(mode);
2890 NDINIT_ATRIGHTS(&nd, LOOKUP, at2cnpflags(flag, AT_SYMLINK_NOFOLLOW |
2891 AT_BENEATH | AT_RESOLVE_BENEATH) | AUDITVNODE1, pathseg, path, fd,
2892 &cap_fchmod_rights, td);
2893 if ((error = namei(&nd)) != 0)
2895 NDFREE(&nd, NDF_ONLY_PNBUF);
2896 error = setfmode(td, td->td_ucred, nd.ni_vp, mode);
2902 * Change mode of a file given a file descriptor.
2904 #ifndef _SYS_SYSPROTO_H_
2905 struct fchmod_args {
2911 sys_fchmod(struct thread *td, struct fchmod_args *uap)
2916 AUDIT_ARG_FD(uap->fd);
2917 AUDIT_ARG_MODE(uap->mode);
2919 error = fget(td, uap->fd, &cap_fchmod_rights, &fp);
2922 error = fo_chmod(fp, uap->mode, td->td_ucred, td);
2928 * Common implementation for chown(), lchown(), and fchown()
2931 setfown(struct thread *td, struct ucred *cred, struct vnode *vp, uid_t uid,
2938 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2940 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2945 error = mac_vnode_check_setowner(cred, vp, vattr.va_uid,
2949 error = VOP_SETATTR(vp, &vattr, cred);
2951 vn_finished_write(mp);
2956 * Set ownership given a path name.
2958 #ifndef _SYS_SYSPROTO_H_
2966 sys_chown(struct thread *td, struct chown_args *uap)
2969 return (kern_fchownat(td, AT_FDCWD, uap->path, UIO_USERSPACE, uap->uid,
2973 #ifndef _SYS_SYSPROTO_H_
2974 struct fchownat_args {
2983 sys_fchownat(struct thread *td, struct fchownat_args *uap)
2986 if ((uap->flag & ~(AT_SYMLINK_NOFOLLOW | AT_BENEATH |
2987 AT_RESOLVE_BENEATH)) != 0)
2990 return (kern_fchownat(td, uap->fd, uap->path, UIO_USERSPACE, uap->uid,
2991 uap->gid, uap->flag));
2995 kern_fchownat(struct thread *td, int fd, const char *path,
2996 enum uio_seg pathseg, int uid, int gid, int flag)
2998 struct nameidata nd;
3001 AUDIT_ARG_OWNER(uid, gid);
3002 NDINIT_ATRIGHTS(&nd, LOOKUP, at2cnpflags(flag, AT_SYMLINK_NOFOLLOW |
3003 AT_BENEATH | AT_RESOLVE_BENEATH) | AUDITVNODE1, pathseg, path, fd,
3004 &cap_fchown_rights, td);
3006 if ((error = namei(&nd)) != 0)
3008 NDFREE(&nd, NDF_ONLY_PNBUF);
3009 error = setfown(td, td->td_ucred, nd.ni_vp, uid, gid);
3015 * Set ownership given a path name, do not cross symlinks.
3017 #ifndef _SYS_SYSPROTO_H_
3018 struct lchown_args {
3025 sys_lchown(struct thread *td, struct lchown_args *uap)
3028 return (kern_fchownat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
3029 uap->uid, uap->gid, AT_SYMLINK_NOFOLLOW));
3033 * Set ownership given a file descriptor.
3035 #ifndef _SYS_SYSPROTO_H_
3036 struct fchown_args {
3043 sys_fchown(struct thread *td, struct fchown_args *uap)
3048 AUDIT_ARG_FD(uap->fd);
3049 AUDIT_ARG_OWNER(uap->uid, uap->gid);
3050 error = fget(td, uap->fd, &cap_fchown_rights, &fp);
3053 error = fo_chown(fp, uap->uid, uap->gid, td->td_ucred, td);
3059 * Common implementation code for utimes(), lutimes(), and futimes().
3062 getutimes(const struct timeval *usrtvp, enum uio_seg tvpseg,
3063 struct timespec *tsp)
3065 struct timeval tv[2];
3066 const struct timeval *tvp;
3069 if (usrtvp == NULL) {
3070 vfs_timestamp(&tsp[0]);
3073 if (tvpseg == UIO_SYSSPACE) {
3076 if ((error = copyin(usrtvp, tv, sizeof(tv))) != 0)
3081 if (tvp[0].tv_usec < 0 || tvp[0].tv_usec >= 1000000 ||
3082 tvp[1].tv_usec < 0 || tvp[1].tv_usec >= 1000000)
3084 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
3085 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
3091 * Common implementation code for futimens(), utimensat().
3093 #define UTIMENS_NULL 0x1
3094 #define UTIMENS_EXIT 0x2
3096 getutimens(const struct timespec *usrtsp, enum uio_seg tspseg,
3097 struct timespec *tsp, int *retflags)
3099 struct timespec tsnow;
3102 vfs_timestamp(&tsnow);
3104 if (usrtsp == NULL) {
3107 *retflags |= UTIMENS_NULL;
3110 if (tspseg == UIO_SYSSPACE) {
3113 } else if ((error = copyin(usrtsp, tsp, sizeof(*tsp) * 2)) != 0)
3115 if (tsp[0].tv_nsec == UTIME_OMIT && tsp[1].tv_nsec == UTIME_OMIT)
3116 *retflags |= UTIMENS_EXIT;
3117 if (tsp[0].tv_nsec == UTIME_NOW && tsp[1].tv_nsec == UTIME_NOW)
3118 *retflags |= UTIMENS_NULL;
3119 if (tsp[0].tv_nsec == UTIME_OMIT)
3120 tsp[0].tv_sec = VNOVAL;
3121 else if (tsp[0].tv_nsec == UTIME_NOW)
3123 else if (tsp[0].tv_nsec < 0 || tsp[0].tv_nsec >= 1000000000L)
3125 if (tsp[1].tv_nsec == UTIME_OMIT)
3126 tsp[1].tv_sec = VNOVAL;
3127 else if (tsp[1].tv_nsec == UTIME_NOW)
3129 else if (tsp[1].tv_nsec < 0 || tsp[1].tv_nsec >= 1000000000L)
3136 * Common implementation code for utimes(), lutimes(), futimes(), futimens(),
3140 setutimes(struct thread *td, struct vnode *vp, const struct timespec *ts,
3141 int numtimes, int nullflag)
3145 int error, setbirthtime;
3147 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
3149 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3151 if (numtimes < 3 && !VOP_GETATTR(vp, &vattr, td->td_ucred) &&
3152 timespeccmp(&ts[1], &vattr.va_birthtime, < ))
3155 vattr.va_atime = ts[0];
3156 vattr.va_mtime = ts[1];
3158 vattr.va_birthtime = ts[1];
3160 vattr.va_birthtime = ts[2];
3162 vattr.va_vaflags |= VA_UTIMES_NULL;
3164 error = mac_vnode_check_setutimes(td->td_ucred, vp, vattr.va_atime,
3168 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3170 vn_finished_write(mp);
3175 * Set the access and modification times of a file.
3177 #ifndef _SYS_SYSPROTO_H_
3178 struct utimes_args {
3180 struct timeval *tptr;
3184 sys_utimes(struct thread *td, struct utimes_args *uap)
3187 return (kern_utimesat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
3188 uap->tptr, UIO_USERSPACE));
3191 #ifndef _SYS_SYSPROTO_H_
3192 struct futimesat_args {
3195 const struct timeval * times;
3199 sys_futimesat(struct thread *td, struct futimesat_args *uap)
3202 return (kern_utimesat(td, uap->fd, uap->path, UIO_USERSPACE,
3203 uap->times, UIO_USERSPACE));
3207 kern_utimesat(struct thread *td, int fd, const char *path,
3208 enum uio_seg pathseg, struct timeval *tptr, enum uio_seg tptrseg)
3210 struct nameidata nd;
3211 struct timespec ts[2];
3214 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3216 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, fd,
3217 &cap_futimes_rights, td);
3219 if ((error = namei(&nd)) != 0)
3221 NDFREE(&nd, NDF_ONLY_PNBUF);
3222 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3228 * Set the access and modification times of a file.
3230 #ifndef _SYS_SYSPROTO_H_
3231 struct lutimes_args {
3233 struct timeval *tptr;
3237 sys_lutimes(struct thread *td, struct lutimes_args *uap)
3240 return (kern_lutimes(td, uap->path, UIO_USERSPACE, uap->tptr,
3245 kern_lutimes(struct thread *td, const char *path, enum uio_seg pathseg,
3246 struct timeval *tptr, enum uio_seg tptrseg)
3248 struct timespec ts[2];
3249 struct nameidata nd;
3252 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3254 NDINIT(&nd, LOOKUP, NOFOLLOW | AUDITVNODE1, pathseg, path, td);
3255 if ((error = namei(&nd)) != 0)
3257 NDFREE(&nd, NDF_ONLY_PNBUF);
3258 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3264 * Set the access and modification times of a file.
3266 #ifndef _SYS_SYSPROTO_H_
3267 struct futimes_args {
3269 struct timeval *tptr;
3273 sys_futimes(struct thread *td, struct futimes_args *uap)
3276 return (kern_futimes(td, uap->fd, uap->tptr, UIO_USERSPACE));
3280 kern_futimes(struct thread *td, int fd, struct timeval *tptr,
3281 enum uio_seg tptrseg)
3283 struct timespec ts[2];
3288 error = getutimes(tptr, tptrseg, ts);
3291 error = getvnode(td, fd, &cap_futimes_rights, &fp);
3295 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
3296 AUDIT_ARG_VNODE1(fp->f_vnode);
3297 VOP_UNLOCK(fp->f_vnode);
3299 error = setutimes(td, fp->f_vnode, ts, 2, tptr == NULL);
3305 sys_futimens(struct thread *td, struct futimens_args *uap)
3308 return (kern_futimens(td, uap->fd, uap->times, UIO_USERSPACE));
3312 kern_futimens(struct thread *td, int fd, struct timespec *tptr,
3313 enum uio_seg tptrseg)
3315 struct timespec ts[2];
3320 error = getutimens(tptr, tptrseg, ts, &flags);
3323 if (flags & UTIMENS_EXIT)
3325 error = getvnode(td, fd, &cap_futimes_rights, &fp);
3329 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
3330 AUDIT_ARG_VNODE1(fp->f_vnode);
3331 VOP_UNLOCK(fp->f_vnode);
3333 error = setutimes(td, fp->f_vnode, ts, 2, flags & UTIMENS_NULL);
3339 sys_utimensat(struct thread *td, struct utimensat_args *uap)
3342 return (kern_utimensat(td, uap->fd, uap->path, UIO_USERSPACE,
3343 uap->times, UIO_USERSPACE, uap->flag));
3347 kern_utimensat(struct thread *td, int fd, const char *path,
3348 enum uio_seg pathseg, struct timespec *tptr, enum uio_seg tptrseg,
3351 struct nameidata nd;
3352 struct timespec ts[2];
3355 if ((flag & ~(AT_SYMLINK_NOFOLLOW | AT_BENEATH |
3356 AT_RESOLVE_BENEATH)) != 0)
3359 if ((error = getutimens(tptr, tptrseg, ts, &flags)) != 0)
3361 NDINIT_ATRIGHTS(&nd, LOOKUP, at2cnpflags(flag, AT_SYMLINK_NOFOLLOW |
3362 AT_BENEATH | AT_RESOLVE_BENEATH) | AUDITVNODE1,
3363 pathseg, path, fd, &cap_futimes_rights, td);
3364 if ((error = namei(&nd)) != 0)
3367 * We are allowed to call namei() regardless of 2xUTIME_OMIT.
3369 * "If both tv_nsec fields are UTIME_OMIT... EACCESS may be detected."
3370 * "Search permission is denied by a component of the path prefix."
3372 NDFREE(&nd, NDF_ONLY_PNBUF);
3373 if ((flags & UTIMENS_EXIT) == 0)
3374 error = setutimes(td, nd.ni_vp, ts, 2, flags & UTIMENS_NULL);
3380 * Truncate a file given its path name.
3382 #ifndef _SYS_SYSPROTO_H_
3383 struct truncate_args {
3390 sys_truncate(struct thread *td, struct truncate_args *uap)
3393 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3397 kern_truncate(struct thread *td, const char *path, enum uio_seg pathseg,
3404 struct nameidata nd;
3410 NDINIT(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, td);
3411 if ((error = namei(&nd)) != 0)
3414 rl_cookie = vn_rangelock_wlock(vp, 0, OFF_MAX);
3415 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0) {
3416 vn_rangelock_unlock(vp, rl_cookie);
3420 NDFREE(&nd, NDF_ONLY_PNBUF);
3421 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3422 if (vp->v_type == VDIR)
3425 else if ((error = mac_vnode_check_write(td->td_ucred, NOCRED, vp))) {
3428 else if ((error = vn_writechk(vp)) == 0 &&
3429 (error = VOP_ACCESS(vp, VWRITE, td->td_ucred, td)) == 0) {
3431 vattr.va_size = length;
3432 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3435 vn_finished_write(mp);
3436 vn_rangelock_unlock(vp, rl_cookie);
3438 if (error == ERELOOKUP)
3443 #if defined(COMPAT_43)
3445 * Truncate a file given its path name.
3447 #ifndef _SYS_SYSPROTO_H_
3448 struct otruncate_args {
3454 otruncate(struct thread *td, struct otruncate_args *uap)
3457 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3459 #endif /* COMPAT_43 */
3461 #if defined(COMPAT_FREEBSD6)
3462 /* Versions with the pad argument */
3464 freebsd6_truncate(struct thread *td, struct freebsd6_truncate_args *uap)
3467 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3471 freebsd6_ftruncate(struct thread *td, struct freebsd6_ftruncate_args *uap)
3474 return (kern_ftruncate(td, uap->fd, uap->length));
3479 kern_fsync(struct thread *td, int fd, bool fullsync)
3484 int error, lock_flags;
3487 error = getvnode(td, fd, &cap_fsync_rights, &fp);
3493 /* XXXKIB: compete outstanding aio writes */;
3496 error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
3499 if (MNT_SHARED_WRITES(mp) ||
3500 ((mp == NULL) && MNT_SHARED_WRITES(vp->v_mount))) {
3501 lock_flags = LK_SHARED;
3503 lock_flags = LK_EXCLUSIVE;
3505 vn_lock(vp, lock_flags | LK_RETRY);
3506 AUDIT_ARG_VNODE1(vp);
3507 if (vp->v_object != NULL) {
3508 VM_OBJECT_WLOCK(vp->v_object);
3509 vm_object_page_clean(vp->v_object, 0, 0, 0);
3510 VM_OBJECT_WUNLOCK(vp->v_object);
3512 error = fullsync ? VOP_FSYNC(vp, MNT_WAIT, td) : VOP_FDATASYNC(vp, td);
3514 vn_finished_write(mp);
3515 if (error == ERELOOKUP)
3523 * Sync an open file.
3525 #ifndef _SYS_SYSPROTO_H_
3531 sys_fsync(struct thread *td, struct fsync_args *uap)
3534 return (kern_fsync(td, uap->fd, true));
3538 sys_fdatasync(struct thread *td, struct fdatasync_args *uap)
3541 return (kern_fsync(td, uap->fd, false));
3545 * Rename files. Source and destination must either both be directories, or
3546 * both not be directories. If target is a directory, it must be empty.
3548 #ifndef _SYS_SYSPROTO_H_
3549 struct rename_args {
3555 sys_rename(struct thread *td, struct rename_args *uap)
3558 return (kern_renameat(td, AT_FDCWD, uap->from, AT_FDCWD,
3559 uap->to, UIO_USERSPACE));
3562 #ifndef _SYS_SYSPROTO_H_
3563 struct renameat_args {
3571 sys_renameat(struct thread *td, struct renameat_args *uap)
3574 return (kern_renameat(td, uap->oldfd, uap->old, uap->newfd, uap->new,
3580 kern_renameat_mac(struct thread *td, int oldfd, const char *old, int newfd,
3581 const char *new, enum uio_seg pathseg, struct nameidata *fromnd)
3585 NDINIT_ATRIGHTS(fromnd, DELETE, LOCKPARENT | LOCKLEAF | SAVESTART |
3586 AUDITVNODE1, pathseg, old, oldfd, &cap_renameat_source_rights, td);
3587 if ((error = namei(fromnd)) != 0)
3589 error = mac_vnode_check_rename_from(td->td_ucred, fromnd->ni_dvp,
3590 fromnd->ni_vp, &fromnd->ni_cnd);
3591 VOP_UNLOCK(fromnd->ni_dvp);
3592 if (fromnd->ni_dvp != fromnd->ni_vp)
3593 VOP_UNLOCK(fromnd->ni_vp);
3595 NDFREE(fromnd, NDF_ONLY_PNBUF);
3596 vrele(fromnd->ni_dvp);
3597 vrele(fromnd->ni_vp);
3598 if (fromnd->ni_startdir)
3599 vrele(fromnd->ni_startdir);
3606 kern_renameat(struct thread *td, int oldfd, const char *old, int newfd,
3607 const char *new, enum uio_seg pathseg)
3609 struct mount *mp = NULL;
3610 struct vnode *tvp, *fvp, *tdvp;
3611 struct nameidata fromnd, tond;
3617 if (mac_vnode_check_rename_from_enabled()) {
3618 error = kern_renameat_mac(td, oldfd, old, newfd, new, pathseg,
3624 NDINIT_ATRIGHTS(&fromnd, DELETE, WANTPARENT | SAVESTART | AUDITVNODE1,
3625 pathseg, old, oldfd, &cap_renameat_source_rights, td);
3626 if ((error = namei(&fromnd)) != 0)
3632 NDINIT_ATRIGHTS(&tond, RENAME, LOCKPARENT | LOCKLEAF | NOCACHE |
3633 SAVESTART | AUDITVNODE2, pathseg, new, newfd,
3634 &cap_renameat_target_rights, td);
3635 if (fromnd.ni_vp->v_type == VDIR)
3636 tond.ni_cnd.cn_flags |= WILLBEDIR;
3637 if ((error = namei(&tond)) != 0) {
3638 /* Translate error code for rename("dir1", "dir2/."). */
3639 if (error == EISDIR && fvp->v_type == VDIR)
3641 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3642 vrele(fromnd.ni_dvp);
3648 error = vn_start_write(fvp, &mp, V_NOWAIT);
3650 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3651 NDFREE(&tond, NDF_ONLY_PNBUF);
3658 vrele(fromnd.ni_dvp);
3660 vrele(tond.ni_startdir);
3661 if (fromnd.ni_startdir != NULL)
3662 vrele(fromnd.ni_startdir);
3663 error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH);
3669 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
3672 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
3677 if (newfd != AT_FDCWD && (tond.ni_resflags & NIRES_ABS) == 0) {
3679 * If the target already exists we require CAP_UNLINKAT
3680 * from 'newfd', when newfd was used for the lookup.
3682 error = cap_check(&tond.ni_filecaps.fc_rights,
3683 &cap_unlinkat_rights);
3694 * If the source is the same as the destination (that is, if they
3695 * are links to the same vnode), then there is nothing to do.
3701 error = mac_vnode_check_rename_to(td->td_ucred, tdvp,
3702 tond.ni_vp, fromnd.ni_dvp == tdvp, &tond.ni_cnd);
3706 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
3707 tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
3708 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3709 NDFREE(&tond, NDF_ONLY_PNBUF);
3711 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3712 NDFREE(&tond, NDF_ONLY_PNBUF);
3719 vrele(fromnd.ni_dvp);
3722 vrele(tond.ni_startdir);
3723 vn_finished_write(mp);
3725 if (fromnd.ni_startdir)
3726 vrele(fromnd.ni_startdir);
3727 if (error == ERESTART)
3729 if (error == ERELOOKUP)
3735 * Make a directory file.
3737 #ifndef _SYS_SYSPROTO_H_
3744 sys_mkdir(struct thread *td, struct mkdir_args *uap)
3747 return (kern_mkdirat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
3751 #ifndef _SYS_SYSPROTO_H_
3752 struct mkdirat_args {
3759 sys_mkdirat(struct thread *td, struct mkdirat_args *uap)
3762 return (kern_mkdirat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode));
3766 kern_mkdirat(struct thread *td, int fd, const char *path, enum uio_seg segflg,
3772 struct nameidata nd;
3775 AUDIT_ARG_MODE(mode);
3778 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
3779 NC_NOMAKEENTRY | NC_KEEPPOSENTRY, segflg, path, fd,
3780 &cap_mkdirat_rights, td);
3781 nd.ni_cnd.cn_flags |= WILLBEDIR;
3782 if ((error = namei(&nd)) != 0)
3786 NDFREE(&nd, NDF_ONLY_PNBUF);
3788 * XXX namei called with LOCKPARENT but not LOCKLEAF has
3789 * the strange behaviour of leaving the vnode unlocked
3790 * if the target is the same vnode as the parent.
3792 if (vp == nd.ni_dvp)
3799 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3800 NDFREE(&nd, NDF_ONLY_PNBUF);
3802 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3807 vattr.va_type = VDIR;
3808 vattr.va_mode = (mode & ACCESSPERMS) &~ td->td_proc->p_fd->fd_cmask;
3810 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
3815 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
3819 NDFREE(&nd, NDF_ONLY_PNBUF);
3823 vn_finished_write(mp);
3824 if (error == ERELOOKUP)
3830 * Remove a directory file.
3832 #ifndef _SYS_SYSPROTO_H_
3838 sys_rmdir(struct thread *td, struct rmdir_args *uap)
3841 return (kern_frmdirat(td, AT_FDCWD, uap->path, FD_NONE, UIO_USERSPACE,
3846 kern_frmdirat(struct thread *td, int dfd, const char *path, int fd,
3847 enum uio_seg pathseg, int flag)
3852 struct nameidata nd;
3853 cap_rights_t rights;
3857 if (fd != FD_NONE) {
3858 error = getvnode(td, fd, cap_rights_init_one(&rights, CAP_LOOKUP),
3866 NDINIT_ATRIGHTS(&nd, DELETE, LOCKPARENT | LOCKLEAF | AUDITVNODE1 |
3867 at2cnpflags(flag, AT_BENEATH | AT_RESOLVE_BENEATH),
3868 pathseg, path, dfd, &cap_unlinkat_rights, td);
3869 if ((error = namei(&nd)) != 0)
3872 if (vp->v_type != VDIR) {
3877 * No rmdir "." please.
3879 if (nd.ni_dvp == vp) {
3884 * The root of a mounted filesystem cannot be deleted.
3886 if (vp->v_vflag & VV_ROOT) {
3891 if (fp != NULL && fp->f_vnode != vp) {
3892 if (VN_IS_DOOMED(fp->f_vnode))
3900 error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp,
3905 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3906 NDFREE(&nd, NDF_ONLY_PNBUF);
3908 if (nd.ni_dvp == vp)
3912 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3916 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_UNLINK);
3917 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
3918 vn_finished_write(mp);
3920 NDFREE(&nd, NDF_ONLY_PNBUF);
3922 if (nd.ni_dvp == vp)
3926 if (error == ERELOOKUP)
3934 #if defined(COMPAT_43) || defined(COMPAT_FREEBSD11)
3936 freebsd11_kern_getdirentries(struct thread *td, int fd, char *ubuf, u_int count,
3937 long *basep, void (*func)(struct freebsd11_dirent *))
3939 struct freebsd11_dirent dstdp;
3940 struct dirent *dp, *edp;
3943 ssize_t resid, ucount;
3946 /* XXX arbitrary sanity limit on `count'. */
3947 count = min(count, 64 * 1024);
3949 dirbuf = malloc(count, M_TEMP, M_WAITOK);
3951 error = kern_getdirentries(td, fd, dirbuf, count, &base, &resid,
3959 for (dp = (struct dirent *)dirbuf,
3960 edp = (struct dirent *)&dirbuf[count - resid];
3961 ucount < count && dp < edp; ) {
3962 if (dp->d_reclen == 0)
3964 MPASS(dp->d_reclen >= _GENERIC_DIRLEN(0));
3965 if (dp->d_namlen >= sizeof(dstdp.d_name))
3967 dstdp.d_type = dp->d_type;
3968 dstdp.d_namlen = dp->d_namlen;
3969 dstdp.d_fileno = dp->d_fileno; /* truncate */
3970 if (dstdp.d_fileno != dp->d_fileno) {
3971 switch (ino64_trunc_error) {
3979 dstdp.d_fileno = UINT32_MAX;
3983 dstdp.d_reclen = sizeof(dstdp) - sizeof(dstdp.d_name) +
3984 ((dp->d_namlen + 1 + 3) &~ 3);
3985 bcopy(dp->d_name, dstdp.d_name, dstdp.d_namlen);
3986 bzero(dstdp.d_name + dstdp.d_namlen,
3987 dstdp.d_reclen - offsetof(struct freebsd11_dirent, d_name) -
3989 MPASS(dstdp.d_reclen <= dp->d_reclen);
3990 MPASS(ucount + dstdp.d_reclen <= count);
3993 error = copyout(&dstdp, ubuf + ucount, dstdp.d_reclen);
3996 dp = (struct dirent *)((char *)dp + dp->d_reclen);
3997 ucount += dstdp.d_reclen;
4001 free(dirbuf, M_TEMP);
4003 td->td_retval[0] = ucount;
4010 ogetdirentries_cvt(struct freebsd11_dirent *dp)
4012 #if (BYTE_ORDER == LITTLE_ENDIAN)
4014 * The expected low byte of dp->d_namlen is our dp->d_type.
4015 * The high MBZ byte of dp->d_namlen is our dp->d_namlen.
4017 dp->d_type = dp->d_namlen;
4021 * The dp->d_type is the high byte of the expected dp->d_namlen,
4022 * so must be zero'ed.
4029 * Read a block of directory entries in a filesystem independent format.
4031 #ifndef _SYS_SYSPROTO_H_
4032 struct ogetdirentries_args {
4040 ogetdirentries(struct thread *td, struct ogetdirentries_args *uap)
4045 error = kern_ogetdirentries(td, uap, &loff);
4047 error = copyout(&loff, uap->basep, sizeof(long));
4052 kern_ogetdirentries(struct thread *td, struct ogetdirentries_args *uap,
4058 /* XXX arbitrary sanity limit on `count'. */
4059 if (uap->count > 64 * 1024)
4062 error = freebsd11_kern_getdirentries(td, uap->fd, uap->buf, uap->count,
4063 &base, ogetdirentries_cvt);
4065 if (error == 0 && uap->basep != NULL)
4066 error = copyout(&base, uap->basep, sizeof(long));
4070 #endif /* COMPAT_43 */
4072 #if defined(COMPAT_FREEBSD11)
4073 #ifndef _SYS_SYSPROTO_H_
4074 struct freebsd11_getdirentries_args {
4082 freebsd11_getdirentries(struct thread *td,
4083 struct freebsd11_getdirentries_args *uap)
4088 error = freebsd11_kern_getdirentries(td, uap->fd, uap->buf, uap->count,
4091 if (error == 0 && uap->basep != NULL)
4092 error = copyout(&base, uap->basep, sizeof(long));
4097 freebsd11_getdents(struct thread *td, struct freebsd11_getdents_args *uap)
4099 struct freebsd11_getdirentries_args ap;
4103 ap.count = uap->count;
4105 return (freebsd11_getdirentries(td, &ap));
4107 #endif /* COMPAT_FREEBSD11 */
4110 * Read a block of directory entries in a filesystem independent format.
4113 sys_getdirentries(struct thread *td, struct getdirentries_args *uap)
4118 error = kern_getdirentries(td, uap->fd, uap->buf, uap->count, &base,
4119 NULL, UIO_USERSPACE);
4122 if (uap->basep != NULL)
4123 error = copyout(&base, uap->basep, sizeof(off_t));
4128 kern_getdirentries(struct thread *td, int fd, char *buf, size_t count,
4129 off_t *basep, ssize_t *residp, enum uio_seg bufseg)
4140 if (count > IOSIZE_MAX)
4142 auio.uio_resid = count;
4143 error = getvnode(td, fd, &cap_read_rights, &fp);
4146 if ((fp->f_flag & FREAD) == 0) {
4151 foffset = foffset_lock(fp, 0);
4153 if (vp->v_type != VDIR) {
4157 aiov.iov_base = buf;
4158 aiov.iov_len = count;
4159 auio.uio_iov = &aiov;
4160 auio.uio_iovcnt = 1;
4161 auio.uio_rw = UIO_READ;
4162 auio.uio_segflg = bufseg;
4164 vn_lock(vp, LK_SHARED | LK_RETRY);
4165 AUDIT_ARG_VNODE1(vp);
4166 loff = auio.uio_offset = foffset;
4168 error = mac_vnode_check_readdir(td->td_ucred, vp);
4171 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL,
4173 foffset = auio.uio_offset;
4178 if (count == auio.uio_resid &&
4179 (vp->v_vflag & VV_ROOT) &&
4180 (vp->v_mount->mnt_flag & MNT_UNION)) {
4181 struct vnode *tvp = vp;
4183 vp = vp->v_mount->mnt_vnodecovered;
4193 *residp = auio.uio_resid;
4194 td->td_retval[0] = count - auio.uio_resid;
4196 foffset_unlock(fp, foffset, 0);
4202 * Set the mode mask for creation of filesystem nodes.
4204 #ifndef _SYS_SYSPROTO_H_
4210 sys_umask(struct thread *td, struct umask_args *uap)
4212 struct filedesc *fdp;
4214 fdp = td->td_proc->p_fd;
4215 FILEDESC_XLOCK(fdp);
4216 td->td_retval[0] = fdp->fd_cmask;
4217 fdp->fd_cmask = uap->newmask & ALLPERMS;
4218 FILEDESC_XUNLOCK(fdp);
4223 * Void all references to file by ripping underlying filesystem away from
4226 #ifndef _SYS_SYSPROTO_H_
4227 struct revoke_args {
4232 sys_revoke(struct thread *td, struct revoke_args *uap)
4236 struct nameidata nd;
4239 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
4241 if ((error = namei(&nd)) != 0)
4244 NDFREE(&nd, NDF_ONLY_PNBUF);
4245 if (vp->v_type != VCHR || vp->v_rdev == NULL) {
4250 error = mac_vnode_check_revoke(td->td_ucred, vp);
4254 error = VOP_GETATTR(vp, &vattr, td->td_ucred);
4257 if (td->td_ucred->cr_uid != vattr.va_uid) {
4258 error = priv_check(td, PRIV_VFS_ADMIN);
4262 if (devfs_usecount(vp) > 0)
4263 VOP_REVOKE(vp, REVOKEALL);
4270 * Convert a user file descriptor to a kernel file entry and check that, if it
4271 * is a capability, the correct rights are present. A reference on the file
4272 * entry is held upon returning.
4275 getvnode(struct thread *td, int fd, cap_rights_t *rightsp, struct file **fpp)
4280 error = fget_unlocked(td->td_proc->p_fd, fd, rightsp, &fp);
4285 * The file could be not of the vnode type, or it may be not
4286 * yet fully initialized, in which case the f_vnode pointer
4287 * may be set, but f_ops is still badfileops. E.g.,
4288 * devfs_open() transiently create such situation to
4289 * facilitate csw d_fdopen().
4291 * Dupfdopen() handling in kern_openat() installs the
4292 * half-baked file into the process descriptor table, allowing
4293 * other thread to dereference it. Guard against the race by
4296 if (fp->f_vnode == NULL || fp->f_ops == &badfileops) {
4305 * Get an (NFS) file handle.
4307 #ifndef _SYS_SYSPROTO_H_
4308 struct lgetfh_args {
4314 sys_lgetfh(struct thread *td, struct lgetfh_args *uap)
4317 return (kern_getfhat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->fname,
4318 UIO_USERSPACE, uap->fhp));
4321 #ifndef _SYS_SYSPROTO_H_
4328 sys_getfh(struct thread *td, struct getfh_args *uap)
4331 return (kern_getfhat(td, 0, AT_FDCWD, uap->fname, UIO_USERSPACE,
4336 * syscall for the rpc.lockd to use to translate an open descriptor into
4337 * a NFS file handle.
4339 * warning: do not remove the priv_check() call or this becomes one giant
4342 #ifndef _SYS_SYSPROTO_H_
4343 struct getfhat_args {
4351 sys_getfhat(struct thread *td, struct getfhat_args *uap)
4354 if ((uap->flags & ~(AT_SYMLINK_NOFOLLOW | AT_BENEATH |
4355 AT_RESOLVE_BENEATH)) != 0)
4357 return (kern_getfhat(td, uap->flags, uap->fd, uap->path, UIO_USERSPACE,
4362 kern_getfhat(struct thread *td, int flags, int fd, const char *path,
4363 enum uio_seg pathseg, fhandle_t *fhp)
4365 struct nameidata nd;
4370 error = priv_check(td, PRIV_VFS_GETFH);
4373 NDINIT_AT(&nd, LOOKUP, at2cnpflags(flags, AT_SYMLINK_NOFOLLOW |
4374 AT_BENEATH | AT_RESOLVE_BENEATH) | LOCKLEAF | AUDITVNODE1,
4375 pathseg, path, fd, td);
4379 NDFREE(&nd, NDF_ONLY_PNBUF);
4381 bzero(&fh, sizeof(fh));
4382 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
4383 error = VOP_VPTOFH(vp, &fh.fh_fid);
4386 error = copyout(&fh, fhp, sizeof (fh));
4390 #ifndef _SYS_SYSPROTO_H_
4391 struct fhlink_args {
4397 sys_fhlink(struct thread *td, struct fhlink_args *uap)
4400 return (kern_fhlinkat(td, AT_FDCWD, uap->to, UIO_USERSPACE, uap->fhp));
4403 #ifndef _SYS_SYSPROTO_H_
4404 struct fhlinkat_args {
4411 sys_fhlinkat(struct thread *td, struct fhlinkat_args *uap)
4414 return (kern_fhlinkat(td, uap->tofd, uap->to, UIO_USERSPACE, uap->fhp));
4418 kern_fhlinkat(struct thread *td, int fd, const char *path,
4419 enum uio_seg pathseg, fhandle_t *fhp)
4426 error = priv_check(td, PRIV_VFS_GETFH);
4429 error = copyin(fhp, &fh, sizeof(fh));
4434 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4436 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_SHARED, &vp);
4441 error = kern_linkat_vp(td, vp, fd, path, pathseg);
4442 } while (error == EAGAIN || error == ERELOOKUP);
4446 #ifndef _SYS_SYSPROTO_H_
4447 struct fhreadlink_args {
4454 sys_fhreadlink(struct thread *td, struct fhreadlink_args *uap)
4461 error = priv_check(td, PRIV_VFS_GETFH);
4464 if (uap->bufsize > IOSIZE_MAX)
4466 error = copyin(uap->fhp, &fh, sizeof(fh));
4469 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4471 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_SHARED, &vp);
4475 error = kern_readlink_vp(vp, uap->buf, UIO_USERSPACE, uap->bufsize, td);
4481 * syscall for the rpc.lockd to use to translate a NFS file handle into an
4484 * warning: do not remove the priv_check() call or this becomes one giant
4487 #ifndef _SYS_SYSPROTO_H_
4488 struct fhopen_args {
4489 const struct fhandle *u_fhp;
4494 sys_fhopen(struct thread *td, struct fhopen_args *uap)
4503 error = priv_check(td, PRIV_VFS_FHOPEN);
4507 fmode = FFLAGS(uap->flags);
4508 /* why not allow a non-read/write open for our lockd? */
4509 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
4511 error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
4514 /* find the mount point */
4515 mp = vfs_busyfs(&fhp.fh_fsid);
4518 /* now give me my vnode, it gets returned to me locked */
4519 error = VFS_FHTOVP(mp, &fhp.fh_fid, LK_EXCLUSIVE, &vp);
4524 error = falloc_noinstall(td, &fp);
4530 * An extra reference on `fp' has been held for us by
4531 * falloc_noinstall().
4537 error = vn_open_vnode(vp, fmode, td->td_ucred, td, fp);
4539 KASSERT(fp->f_ops == &badfileops,
4540 ("VOP_OPEN in fhopen() set f_ops"));
4541 KASSERT(td->td_dupfd < 0,
4542 ("fhopen() encountered fdopen()"));
4551 finit_vnode(fp, fmode, NULL, &vnops);
4553 if ((fmode & O_TRUNC) != 0) {
4554 error = fo_truncate(fp, 0, td->td_ucred, td);
4559 error = finstall(td, fp, &indx, fmode, NULL);
4562 td->td_retval[0] = indx;
4567 * Stat an (NFS) file handle.
4569 #ifndef _SYS_SYSPROTO_H_
4570 struct fhstat_args {
4571 struct fhandle *u_fhp;
4576 sys_fhstat(struct thread *td, struct fhstat_args *uap)
4582 error = copyin(uap->u_fhp, &fh, sizeof(fh));
4585 error = kern_fhstat(td, fh, &sb);
4587 error = copyout(&sb, uap->sb, sizeof(sb));
4592 kern_fhstat(struct thread *td, struct fhandle fh, struct stat *sb)
4598 error = priv_check(td, PRIV_VFS_FHSTAT);
4601 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4603 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_EXCLUSIVE, &vp);
4607 error = VOP_STAT(vp, sb, td->td_ucred, NOCRED, td);
4613 * Implement fstatfs() for (NFS) file handles.
4615 #ifndef _SYS_SYSPROTO_H_
4616 struct fhstatfs_args {
4617 struct fhandle *u_fhp;
4622 sys_fhstatfs(struct thread *td, struct fhstatfs_args *uap)
4628 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4631 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
4632 error = kern_fhstatfs(td, fh, sfp);
4634 error = copyout(sfp, uap->buf, sizeof(*sfp));
4635 free(sfp, M_STATFS);
4640 kern_fhstatfs(struct thread *td, fhandle_t fh, struct statfs *buf)
4646 error = priv_check(td, PRIV_VFS_FHSTATFS);
4649 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4651 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_EXCLUSIVE, &vp);
4657 error = prison_canseemount(td->td_ucred, mp);
4661 error = mac_mount_check_stat(td->td_ucred, mp);
4665 error = VFS_STATFS(mp, buf);
4672 * Unlike madvise(2), we do not make a best effort to remember every
4673 * possible caching hint. Instead, we remember the last setting with
4674 * the exception that we will allow POSIX_FADV_NORMAL to adjust the
4675 * region of any current setting.
4678 kern_posix_fadvise(struct thread *td, int fd, off_t offset, off_t len,
4681 struct fadvise_info *fa, *new;
4687 if (offset < 0 || len < 0 || offset > OFF_MAX - len)
4689 AUDIT_ARG_VALUE(advice);
4691 case POSIX_FADV_SEQUENTIAL:
4692 case POSIX_FADV_RANDOM:
4693 case POSIX_FADV_NOREUSE:
4694 new = malloc(sizeof(*fa), M_FADVISE, M_WAITOK);
4696 case POSIX_FADV_NORMAL:
4697 case POSIX_FADV_WILLNEED:
4698 case POSIX_FADV_DONTNEED:
4704 /* XXX: CAP_POSIX_FADVISE? */
4706 error = fget(td, fd, &cap_no_rights, &fp);
4709 AUDIT_ARG_FILE(td->td_proc, fp);
4710 if ((fp->f_ops->fo_flags & DFLAG_SEEKABLE) == 0) {
4714 if (fp->f_type != DTYPE_VNODE) {
4719 if (vp->v_type != VREG) {
4726 end = offset + len - 1;
4728 case POSIX_FADV_SEQUENTIAL:
4729 case POSIX_FADV_RANDOM:
4730 case POSIX_FADV_NOREUSE:
4732 * Try to merge any existing non-standard region with
4733 * this new region if possible, otherwise create a new
4734 * non-standard region for this request.
4736 mtx_pool_lock(mtxpool_sleep, fp);
4738 if (fa != NULL && fa->fa_advice == advice &&
4739 ((fa->fa_start <= end && fa->fa_end >= offset) ||
4740 (end != OFF_MAX && fa->fa_start == end + 1) ||
4741 (fa->fa_end != OFF_MAX && fa->fa_end + 1 == offset))) {
4742 if (offset < fa->fa_start)
4743 fa->fa_start = offset;
4744 if (end > fa->fa_end)
4747 new->fa_advice = advice;
4748 new->fa_start = offset;
4753 mtx_pool_unlock(mtxpool_sleep, fp);
4755 case POSIX_FADV_NORMAL:
4757 * If a the "normal" region overlaps with an existing
4758 * non-standard region, trim or remove the
4759 * non-standard region.
4761 mtx_pool_lock(mtxpool_sleep, fp);
4764 if (offset <= fa->fa_start && end >= fa->fa_end) {
4766 fp->f_advice = NULL;
4767 } else if (offset <= fa->fa_start &&
4768 end >= fa->fa_start)
4769 fa->fa_start = end + 1;
4770 else if (offset <= fa->fa_end && end >= fa->fa_end)
4771 fa->fa_end = offset - 1;
4772 else if (offset >= fa->fa_start && end <= fa->fa_end) {
4774 * If the "normal" region is a middle
4775 * portion of the existing
4776 * non-standard region, just remove
4777 * the whole thing rather than picking
4778 * one side or the other to
4782 fp->f_advice = NULL;
4785 mtx_pool_unlock(mtxpool_sleep, fp);
4787 case POSIX_FADV_WILLNEED:
4788 case POSIX_FADV_DONTNEED:
4789 error = VOP_ADVISE(vp, offset, end, advice);
4795 free(new, M_FADVISE);
4800 sys_posix_fadvise(struct thread *td, struct posix_fadvise_args *uap)
4804 error = kern_posix_fadvise(td, uap->fd, uap->offset, uap->len,
4806 return (kern_posix_error(td, error));
4810 kern_copy_file_range(struct thread *td, int infd, off_t *inoffp, int outfd,
4811 off_t *outoffp, size_t len, unsigned int flags)
4813 struct file *infp, *outfp;
4814 struct vnode *invp, *outvp;
4817 void *rl_rcookie, *rl_wcookie;
4818 off_t savinoff, savoutoff;
4820 infp = outfp = NULL;
4821 rl_rcookie = rl_wcookie = NULL;
4830 if (len > SSIZE_MAX)
4832 * Although the len argument is size_t, the return argument
4833 * is ssize_t (which is signed). Therefore a size that won't
4834 * fit in ssize_t can't be returned.
4838 /* Get the file structures for the file descriptors. */
4839 error = fget_read(td, infd, &cap_read_rights, &infp);
4842 if (infp->f_ops == &badfileops) {
4846 if (infp->f_vnode == NULL) {
4850 error = fget_write(td, outfd, &cap_write_rights, &outfp);
4853 if (outfp->f_ops == &badfileops) {
4857 if (outfp->f_vnode == NULL) {
4862 /* Set the offset pointers to the correct place. */
4864 inoffp = &infp->f_offset;
4865 if (outoffp == NULL)
4866 outoffp = &outfp->f_offset;
4868 savoutoff = *outoffp;
4870 invp = infp->f_vnode;
4871 outvp = outfp->f_vnode;
4872 /* Sanity check the f_flag bits. */
4873 if ((outfp->f_flag & (FWRITE | FAPPEND)) != FWRITE ||
4874 (infp->f_flag & FREAD) == 0) {
4879 /* If len == 0, just return 0. */
4884 * If infp and outfp refer to the same file, the byte ranges cannot
4887 if (invp == outvp && ((savinoff <= savoutoff && savinoff + len >
4888 savoutoff) || (savinoff > savoutoff && savoutoff + len >
4894 /* Range lock the byte ranges for both invp and outvp. */
4896 rl_wcookie = vn_rangelock_wlock(outvp, *outoffp, *outoffp +
4898 rl_rcookie = vn_rangelock_tryrlock(invp, *inoffp, *inoffp +
4900 if (rl_rcookie != NULL)
4902 vn_rangelock_unlock(outvp, rl_wcookie);
4903 rl_rcookie = vn_rangelock_rlock(invp, *inoffp, *inoffp + len);
4904 vn_rangelock_unlock(invp, rl_rcookie);
4908 error = vn_copy_file_range(invp, inoffp, outvp, outoffp, &retlen,
4909 flags, infp->f_cred, outfp->f_cred, td);
4911 if (rl_rcookie != NULL)
4912 vn_rangelock_unlock(invp, rl_rcookie);
4913 if (rl_wcookie != NULL)
4914 vn_rangelock_unlock(outvp, rl_wcookie);
4915 if (savinoff != -1 && (error == EINTR || error == ERESTART)) {
4917 *outoffp = savoutoff;
4923 td->td_retval[0] = retlen;
4928 sys_copy_file_range(struct thread *td, struct copy_file_range_args *uap)
4930 off_t inoff, outoff, *inoffp, *outoffp;
4933 inoffp = outoffp = NULL;
4934 if (uap->inoffp != NULL) {
4935 error = copyin(uap->inoffp, &inoff, sizeof(off_t));
4940 if (uap->outoffp != NULL) {
4941 error = copyin(uap->outoffp, &outoff, sizeof(off_t));
4946 error = kern_copy_file_range(td, uap->infd, inoffp, uap->outfd,
4947 outoffp, uap->len, uap->flags);
4948 if (error == 0 && uap->inoffp != NULL)
4949 error = copyout(inoffp, uap->inoffp, sizeof(off_t));
4950 if (error == 0 && uap->outoffp != NULL)
4951 error = copyout(outoffp, uap->outoffp, sizeof(off_t));