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_readlink_vp(struct vnode *vp, char *buf, enum uio_seg bufseg,
109 size_t count, struct thread *td);
110 static int kern_linkat_vp(struct thread *td, struct vnode *vp, int fd,
111 const char *path, enum uio_seg segflag);
114 at2cnpflags(u_int at_flags, u_int mask)
118 MPASS((at_flags & (AT_SYMLINK_FOLLOW | AT_SYMLINK_NOFOLLOW)) !=
119 (AT_SYMLINK_FOLLOW | AT_SYMLINK_NOFOLLOW));
123 if ((at_flags & AT_RESOLVE_BENEATH) != 0)
125 if ((at_flags & AT_SYMLINK_FOLLOW) != 0)
127 /* NOFOLLOW is pseudo flag */
128 if ((mask & AT_SYMLINK_NOFOLLOW) != 0) {
129 res |= (at_flags & AT_SYMLINK_NOFOLLOW) != 0 ? NOFOLLOW :
136 kern_sync(struct thread *td)
138 struct mount *mp, *nmp;
141 mtx_lock(&mountlist_mtx);
142 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
143 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK)) {
144 nmp = TAILQ_NEXT(mp, mnt_list);
147 if ((mp->mnt_flag & MNT_RDONLY) == 0 &&
148 vn_start_write(NULL, &mp, V_NOWAIT) == 0) {
149 save = curthread_pflags_set(TDP_SYNCIO);
150 vfs_periodic(mp, MNT_NOWAIT);
151 VFS_SYNC(mp, MNT_NOWAIT);
152 curthread_pflags_restore(save);
153 vn_finished_write(mp);
155 mtx_lock(&mountlist_mtx);
156 nmp = TAILQ_NEXT(mp, mnt_list);
159 mtx_unlock(&mountlist_mtx);
164 * Sync each mounted filesystem.
166 #ifndef _SYS_SYSPROTO_H_
173 sys_sync(struct thread *td, struct sync_args *uap)
176 return (kern_sync(td));
180 * Change filesystem quotas.
182 #ifndef _SYS_SYSPROTO_H_
183 struct quotactl_args {
191 sys_quotactl(struct thread *td, struct quotactl_args *uap)
197 AUDIT_ARG_CMD(uap->cmd);
198 AUDIT_ARG_UID(uap->uid);
199 if (!prison_allow(td->td_ucred, PR_ALLOW_QUOTAS))
201 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
203 if ((error = namei(&nd)) != 0)
205 NDFREE(&nd, NDF_ONLY_PNBUF);
206 mp = nd.ni_vp->v_mount;
209 error = vfs_busy(mp, 0);
214 error = VFS_QUOTACTL(mp, uap->cmd, uap->uid, uap->arg);
217 * Since quota on operation typically needs to open quota
218 * file, the Q_QUOTAON handler needs to unbusy the mount point
219 * before calling into namei. Otherwise, unmount might be
220 * started between two vfs_busy() invocations (first is our,
221 * second is from mount point cross-walk code in lookup()),
224 * Require that Q_QUOTAON handles the vfs_busy() reference on
225 * its own, always returning with ubusied mount point.
227 if ((uap->cmd >> SUBCMDSHIFT) != Q_QUOTAON &&
228 (uap->cmd >> SUBCMDSHIFT) != Q_QUOTAOFF)
235 * Used by statfs conversion routines to scale the block size up if
236 * necessary so that all of the block counts are <= 'max_size'. Note
237 * that 'max_size' should be a bitmask, i.e. 2^n - 1 for some non-zero
241 statfs_scale_blocks(struct statfs *sf, long max_size)
246 KASSERT(powerof2(max_size + 1), ("%s: invalid max_size", __func__));
249 * Attempt to scale the block counts to give a more accurate
250 * overview to userland of the ratio of free space to used
251 * space. To do this, find the largest block count and compute
252 * a divisor that lets it fit into a signed integer <= max_size.
254 if (sf->f_bavail < 0)
255 count = -sf->f_bavail;
257 count = sf->f_bavail;
258 count = MAX(sf->f_blocks, MAX(sf->f_bfree, count));
259 if (count <= max_size)
262 count >>= flsl(max_size);
269 sf->f_bsize <<= shift;
270 sf->f_blocks >>= shift;
271 sf->f_bfree >>= shift;
272 sf->f_bavail >>= shift;
276 kern_do_statfs(struct thread *td, struct mount *mp, struct statfs *buf)
282 error = vfs_busy(mp, 0);
287 error = mac_mount_check_stat(td->td_ucred, mp);
291 error = VFS_STATFS(mp, buf);
294 if (priv_check_cred_vfs_generation(td->td_ucred)) {
295 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
296 prison_enforce_statfs(td->td_ucred, mp, buf);
304 * Get filesystem statistics.
306 #ifndef _SYS_SYSPROTO_H_
313 sys_statfs(struct thread *td, struct statfs_args *uap)
318 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
319 error = kern_statfs(td, uap->path, UIO_USERSPACE, sfp);
321 error = copyout(sfp, uap->buf, sizeof(struct statfs));
327 kern_statfs(struct thread *td, const char *path, enum uio_seg pathseg,
334 NDINIT(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, td);
338 mp = vfs_ref_from_vp(nd.ni_vp);
341 return (kern_do_statfs(td, mp, buf));
345 * Get filesystem statistics.
347 #ifndef _SYS_SYSPROTO_H_
348 struct fstatfs_args {
354 sys_fstatfs(struct thread *td, struct fstatfs_args *uap)
359 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
360 error = kern_fstatfs(td, uap->fd, sfp);
362 error = copyout(sfp, uap->buf, sizeof(struct statfs));
368 kern_fstatfs(struct thread *td, int fd, struct statfs *buf)
376 error = getvnode(td, fd, &cap_fstatfs_rights, &fp);
381 if (AUDITING_TD(td)) {
382 vn_lock(vp, LK_SHARED | LK_RETRY);
383 AUDIT_ARG_VNODE1(vp);
387 mp = vfs_ref_from_vp(vp);
389 return (kern_do_statfs(td, mp, buf));
393 * Get statistics on all filesystems.
395 #ifndef _SYS_SYSPROTO_H_
396 struct getfsstat_args {
403 sys_getfsstat(struct thread *td, struct getfsstat_args *uap)
408 if (uap->bufsize < 0 || uap->bufsize > SIZE_MAX)
410 error = kern_getfsstat(td, &uap->buf, uap->bufsize, &count,
411 UIO_USERSPACE, uap->mode);
413 td->td_retval[0] = count;
418 * If (bufsize > 0 && bufseg == UIO_SYSSPACE)
419 * The caller is responsible for freeing memory which will be allocated
423 kern_getfsstat(struct thread *td, struct statfs **buf, size_t bufsize,
424 size_t *countp, enum uio_seg bufseg, int mode)
426 struct mount *mp, *nmp;
427 struct statfs *sfsp, *sp, *sptmp, *tofree;
428 size_t count, maxcount;
436 if (bufseg == UIO_SYSSPACE)
441 maxcount = bufsize / sizeof(struct statfs);
445 } else if (bufseg == UIO_USERSPACE) {
448 } else /* if (bufseg == UIO_SYSSPACE) */ {
450 mtx_lock(&mountlist_mtx);
451 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
454 mtx_unlock(&mountlist_mtx);
455 if (maxcount > count)
457 tofree = sfsp = *buf = malloc(maxcount * sizeof(struct statfs),
464 * If there is no target buffer they only want the count.
466 * This could be TAILQ_FOREACH but it is open-coded to match the original
470 mtx_lock(&mountlist_mtx);
471 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
472 if (prison_canseemount(td->td_ucred, mp) != 0) {
473 nmp = TAILQ_NEXT(mp, mnt_list);
477 if (mac_mount_check_stat(td->td_ucred, mp) != 0) {
478 nmp = TAILQ_NEXT(mp, mnt_list);
483 nmp = TAILQ_NEXT(mp, mnt_list);
485 mtx_unlock(&mountlist_mtx);
491 * They want the entire thing.
493 * Short-circuit the corner case of no room for anything, avoids
500 mtx_lock(&mountlist_mtx);
501 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
502 if (prison_canseemount(td->td_ucred, mp) != 0) {
503 nmp = TAILQ_NEXT(mp, mnt_list);
507 if (mac_mount_check_stat(td->td_ucred, mp) != 0) {
508 nmp = TAILQ_NEXT(mp, mnt_list);
512 if (mode == MNT_WAIT) {
513 if (vfs_busy(mp, MBF_MNTLSTLOCK) != 0) {
515 * If vfs_busy() failed, and MBF_NOWAIT
516 * wasn't passed, then the mp is gone.
517 * Furthermore, because of MBF_MNTLSTLOCK,
518 * the mountlist_mtx was dropped. We have
519 * no other choice than to start over.
521 mtx_unlock(&mountlist_mtx);
522 free(tofree, M_STATFS);
526 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK) != 0) {
527 nmp = TAILQ_NEXT(mp, mnt_list);
533 * If MNT_NOWAIT is specified, do not refresh
536 if (mode != MNT_NOWAIT) {
537 error = VFS_STATFS(mp, sp);
539 mtx_lock(&mountlist_mtx);
540 nmp = TAILQ_NEXT(mp, mnt_list);
545 if (priv_check_cred_vfs_generation(td->td_ucred)) {
546 sptmp = malloc(sizeof(struct statfs), M_STATFS,
549 sptmp->f_fsid.val[0] = sptmp->f_fsid.val[1] = 0;
550 prison_enforce_statfs(td->td_ucred, mp, sptmp);
554 if (bufseg == UIO_SYSSPACE) {
555 bcopy(sp, sfsp, sizeof(*sp));
556 free(sptmp, M_STATFS);
557 } else /* if (bufseg == UIO_USERSPACE) */ {
558 error = copyout(sp, sfsp, sizeof(*sp));
559 free(sptmp, M_STATFS);
568 if (count == maxcount) {
573 mtx_lock(&mountlist_mtx);
574 nmp = TAILQ_NEXT(mp, mnt_list);
577 mtx_unlock(&mountlist_mtx);
583 #ifdef COMPAT_FREEBSD4
585 * Get old format filesystem statistics.
587 static void freebsd4_cvtstatfs(struct statfs *, struct ostatfs *);
589 #ifndef _SYS_SYSPROTO_H_
590 struct freebsd4_statfs_args {
596 freebsd4_statfs(struct thread *td, struct freebsd4_statfs_args *uap)
602 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
603 error = kern_statfs(td, uap->path, UIO_USERSPACE, sfp);
605 freebsd4_cvtstatfs(sfp, &osb);
606 error = copyout(&osb, uap->buf, sizeof(osb));
613 * Get filesystem statistics.
615 #ifndef _SYS_SYSPROTO_H_
616 struct freebsd4_fstatfs_args {
622 freebsd4_fstatfs(struct thread *td, struct freebsd4_fstatfs_args *uap)
628 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
629 error = kern_fstatfs(td, uap->fd, sfp);
631 freebsd4_cvtstatfs(sfp, &osb);
632 error = copyout(&osb, uap->buf, sizeof(osb));
639 * Get statistics on all filesystems.
641 #ifndef _SYS_SYSPROTO_H_
642 struct freebsd4_getfsstat_args {
649 freebsd4_getfsstat(struct thread *td, struct freebsd4_getfsstat_args *uap)
651 struct statfs *buf, *sp;
656 if (uap->bufsize < 0)
658 count = uap->bufsize / sizeof(struct ostatfs);
659 if (count > SIZE_MAX / sizeof(struct statfs))
661 size = count * sizeof(struct statfs);
662 error = kern_getfsstat(td, &buf, size, &count, UIO_SYSSPACE,
665 td->td_retval[0] = count;
668 while (count != 0 && error == 0) {
669 freebsd4_cvtstatfs(sp, &osb);
670 error = copyout(&osb, uap->buf, sizeof(osb));
681 * Implement fstatfs() for (NFS) file handles.
683 #ifndef _SYS_SYSPROTO_H_
684 struct freebsd4_fhstatfs_args {
685 struct fhandle *u_fhp;
690 freebsd4_fhstatfs(struct thread *td, struct freebsd4_fhstatfs_args *uap)
697 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
700 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
701 error = kern_fhstatfs(td, fh, sfp);
703 freebsd4_cvtstatfs(sfp, &osb);
704 error = copyout(&osb, uap->buf, sizeof(osb));
711 * Convert a new format statfs structure to an old format statfs structure.
714 freebsd4_cvtstatfs(struct statfs *nsp, struct ostatfs *osp)
717 statfs_scale_blocks(nsp, LONG_MAX);
718 bzero(osp, sizeof(*osp));
719 osp->f_bsize = nsp->f_bsize;
720 osp->f_iosize = MIN(nsp->f_iosize, LONG_MAX);
721 osp->f_blocks = nsp->f_blocks;
722 osp->f_bfree = nsp->f_bfree;
723 osp->f_bavail = nsp->f_bavail;
724 osp->f_files = MIN(nsp->f_files, LONG_MAX);
725 osp->f_ffree = MIN(nsp->f_ffree, LONG_MAX);
726 osp->f_owner = nsp->f_owner;
727 osp->f_type = nsp->f_type;
728 osp->f_flags = nsp->f_flags;
729 osp->f_syncwrites = MIN(nsp->f_syncwrites, LONG_MAX);
730 osp->f_asyncwrites = MIN(nsp->f_asyncwrites, LONG_MAX);
731 osp->f_syncreads = MIN(nsp->f_syncreads, LONG_MAX);
732 osp->f_asyncreads = MIN(nsp->f_asyncreads, LONG_MAX);
733 strlcpy(osp->f_fstypename, nsp->f_fstypename,
734 MIN(MFSNAMELEN, OMFSNAMELEN));
735 strlcpy(osp->f_mntonname, nsp->f_mntonname,
736 MIN(MNAMELEN, OMNAMELEN));
737 strlcpy(osp->f_mntfromname, nsp->f_mntfromname,
738 MIN(MNAMELEN, OMNAMELEN));
739 osp->f_fsid = nsp->f_fsid;
741 #endif /* COMPAT_FREEBSD4 */
743 #if defined(COMPAT_FREEBSD11)
745 * Get old format filesystem statistics.
747 static void freebsd11_cvtstatfs(struct statfs *, struct freebsd11_statfs *);
750 freebsd11_statfs(struct thread *td, struct freebsd11_statfs_args *uap)
752 struct freebsd11_statfs osb;
756 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
757 error = kern_statfs(td, uap->path, UIO_USERSPACE, sfp);
759 freebsd11_cvtstatfs(sfp, &osb);
760 error = copyout(&osb, uap->buf, sizeof(osb));
767 * Get filesystem statistics.
770 freebsd11_fstatfs(struct thread *td, struct freebsd11_fstatfs_args *uap)
772 struct freebsd11_statfs osb;
776 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
777 error = kern_fstatfs(td, uap->fd, sfp);
779 freebsd11_cvtstatfs(sfp, &osb);
780 error = copyout(&osb, uap->buf, sizeof(osb));
787 * Get statistics on all filesystems.
790 freebsd11_getfsstat(struct thread *td, struct freebsd11_getfsstat_args *uap)
792 struct freebsd11_statfs osb;
793 struct statfs *buf, *sp;
797 count = uap->bufsize / sizeof(struct ostatfs);
798 size = count * sizeof(struct statfs);
799 error = kern_getfsstat(td, &buf, size, &count, UIO_SYSSPACE,
802 td->td_retval[0] = count;
805 while (count > 0 && error == 0) {
806 freebsd11_cvtstatfs(sp, &osb);
807 error = copyout(&osb, uap->buf, sizeof(osb));
818 * Implement fstatfs() for (NFS) file handles.
821 freebsd11_fhstatfs(struct thread *td, struct freebsd11_fhstatfs_args *uap)
823 struct freebsd11_statfs osb;
828 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
831 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
832 error = kern_fhstatfs(td, fh, sfp);
834 freebsd11_cvtstatfs(sfp, &osb);
835 error = copyout(&osb, uap->buf, sizeof(osb));
842 * Convert a new format statfs structure to an old format statfs structure.
845 freebsd11_cvtstatfs(struct statfs *nsp, struct freebsd11_statfs *osp)
848 bzero(osp, sizeof(*osp));
849 osp->f_version = FREEBSD11_STATFS_VERSION;
850 osp->f_type = nsp->f_type;
851 osp->f_flags = nsp->f_flags;
852 osp->f_bsize = nsp->f_bsize;
853 osp->f_iosize = nsp->f_iosize;
854 osp->f_blocks = nsp->f_blocks;
855 osp->f_bfree = nsp->f_bfree;
856 osp->f_bavail = nsp->f_bavail;
857 osp->f_files = nsp->f_files;
858 osp->f_ffree = nsp->f_ffree;
859 osp->f_syncwrites = nsp->f_syncwrites;
860 osp->f_asyncwrites = nsp->f_asyncwrites;
861 osp->f_syncreads = nsp->f_syncreads;
862 osp->f_asyncreads = nsp->f_asyncreads;
863 osp->f_namemax = nsp->f_namemax;
864 osp->f_owner = nsp->f_owner;
865 osp->f_fsid = nsp->f_fsid;
866 strlcpy(osp->f_fstypename, nsp->f_fstypename,
867 MIN(MFSNAMELEN, sizeof(osp->f_fstypename)));
868 strlcpy(osp->f_mntonname, nsp->f_mntonname,
869 MIN(MNAMELEN, sizeof(osp->f_mntonname)));
870 strlcpy(osp->f_mntfromname, nsp->f_mntfromname,
871 MIN(MNAMELEN, sizeof(osp->f_mntfromname)));
873 #endif /* COMPAT_FREEBSD11 */
876 * Change current working directory to a given file descriptor.
878 #ifndef _SYS_SYSPROTO_H_
884 sys_fchdir(struct thread *td, struct fchdir_args *uap)
886 struct vnode *vp, *tdp;
891 AUDIT_ARG_FD(uap->fd);
892 error = getvnode(td, uap->fd, &cap_fchdir_rights,
899 vn_lock(vp, LK_SHARED | LK_RETRY);
900 AUDIT_ARG_VNODE1(vp);
901 error = change_dir(vp, td);
902 while (!error && (mp = vp->v_mountedhere) != NULL) {
905 error = VFS_ROOT(mp, LK_SHARED, &tdp);
922 * Change current working directory (``.'').
924 #ifndef _SYS_SYSPROTO_H_
930 sys_chdir(struct thread *td, struct chdir_args *uap)
933 return (kern_chdir(td, uap->path, UIO_USERSPACE));
937 kern_chdir(struct thread *td, const char *path, enum uio_seg pathseg)
942 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
944 if ((error = namei(&nd)) != 0)
946 if ((error = change_dir(nd.ni_vp, td)) != 0) {
951 VOP_UNLOCK(nd.ni_vp);
953 pwd_chdir(td, nd.ni_vp);
958 * Change notion of root (``/'') directory.
960 #ifndef _SYS_SYSPROTO_H_
966 sys_chroot(struct thread *td, struct chroot_args *uap)
971 error = priv_check(td, PRIV_VFS_CHROOT);
974 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
975 UIO_USERSPACE, uap->path, td);
979 error = change_dir(nd.ni_vp, td);
983 error = mac_vnode_check_chroot(td->td_ucred, nd.ni_vp);
987 VOP_UNLOCK(nd.ni_vp);
988 error = pwd_chroot(td, nd.ni_vp);
1000 * Common routine for chroot and chdir. Callers must provide a locked vnode
1004 change_dir(struct vnode *vp, struct thread *td)
1010 ASSERT_VOP_LOCKED(vp, "change_dir(): vp not locked");
1011 if (vp->v_type != VDIR)
1014 error = mac_vnode_check_chdir(td->td_ucred, vp);
1018 return (VOP_ACCESS(vp, VEXEC, td->td_ucred, td));
1021 static __inline void
1022 flags_to_rights(int flags, cap_rights_t *rightsp)
1025 if (flags & O_EXEC) {
1026 cap_rights_set_one(rightsp, CAP_FEXECVE);
1028 switch ((flags & O_ACCMODE)) {
1030 cap_rights_set_one(rightsp, CAP_READ);
1033 cap_rights_set_one(rightsp, CAP_READ);
1036 cap_rights_set_one(rightsp, CAP_WRITE);
1037 if (!(flags & (O_APPEND | O_TRUNC)))
1038 cap_rights_set_one(rightsp, CAP_SEEK);
1043 if (flags & O_CREAT)
1044 cap_rights_set_one(rightsp, CAP_CREATE);
1046 if (flags & O_TRUNC)
1047 cap_rights_set_one(rightsp, CAP_FTRUNCATE);
1049 if (flags & (O_SYNC | O_FSYNC))
1050 cap_rights_set_one(rightsp, CAP_FSYNC);
1052 if (flags & (O_EXLOCK | O_SHLOCK))
1053 cap_rights_set_one(rightsp, CAP_FLOCK);
1057 * Check permissions, allocate an open file structure, and call the device
1058 * open routine if any.
1060 #ifndef _SYS_SYSPROTO_H_
1068 sys_open(struct thread *td, struct open_args *uap)
1071 return (kern_openat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1072 uap->flags, uap->mode));
1075 #ifndef _SYS_SYSPROTO_H_
1076 struct openat_args {
1084 sys_openat(struct thread *td, struct openat_args *uap)
1087 AUDIT_ARG_FD(uap->fd);
1088 return (kern_openat(td, uap->fd, uap->path, UIO_USERSPACE, uap->flag,
1093 kern_openat(struct thread *td, int fd, const char *path, enum uio_seg pathseg,
1094 int flags, int mode)
1096 struct proc *p = td->td_proc;
1097 struct filedesc *fdp;
1098 struct pwddesc *pdp;
1101 struct nameidata nd;
1102 cap_rights_t rights;
1103 int cmode, error, indx;
1109 AUDIT_ARG_FFLAGS(flags);
1110 AUDIT_ARG_MODE(mode);
1111 cap_rights_init_one(&rights, CAP_LOOKUP);
1112 flags_to_rights(flags, &rights);
1114 * Only one of the O_EXEC, O_RDONLY, O_WRONLY and O_RDWR flags
1117 if (flags & O_EXEC) {
1118 if (flags & O_ACCMODE)
1120 } else if ((flags & O_ACCMODE) == O_ACCMODE) {
1123 flags = FFLAGS(flags);
1127 * Allocate a file structure. The descriptor to reference it
1128 * is allocated and used by finstall_refed() below.
1130 error = falloc_noinstall(td, &fp);
1133 /* Set the flags early so the finit in devfs can pick them up. */
1134 fp->f_flag = flags & FMASK;
1135 cmode = ((mode & ~pdp->pd_cmask) & ALLPERMS) & ~S_ISTXT;
1136 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, fd,
1138 td->td_dupfd = -1; /* XXX check for fdopen */
1139 error = vn_open(&nd, &flags, cmode, fp);
1142 * If the vn_open replaced the method vector, something
1143 * wonderous happened deep below and we just pass it up
1144 * pretending we know what we do.
1146 if (error == ENXIO && fp->f_ops != &badfileops)
1150 * Handle special fdopen() case. bleh.
1152 * Don't do this for relative (capability) lookups; we don't
1153 * understand exactly what would happen, and we don't think
1154 * that it ever should.
1156 if ((nd.ni_resflags & NIRES_STRICTREL) == 0 &&
1157 (error == ENODEV || error == ENXIO) &&
1158 td->td_dupfd >= 0) {
1159 error = dupfdopen(td, fdp, td->td_dupfd, flags, error,
1168 NDFREE(&nd, NDF_ONLY_PNBUF);
1172 * Store the vnode, for any f_type. Typically, the vnode use
1173 * count is decremented by direct call to vn_closefile() for
1174 * files that switched type in the cdevsw fdopen() method.
1178 * If the file wasn't claimed by devfs bind it to the normal
1179 * vnode operations here.
1181 if (fp->f_ops == &badfileops) {
1182 KASSERT(vp->v_type != VFIFO,
1183 ("Unexpected fifo fp %p vp %p", fp, vp));
1184 finit_vnode(fp, flags, NULL, &vnops);
1188 if (flags & O_TRUNC) {
1189 error = fo_truncate(fp, 0, td->td_ucred, td);
1195 * If we haven't already installed the FD (for dupfdopen), do so now.
1198 struct filecaps *fcaps;
1201 if ((nd.ni_resflags & NIRES_STRICTREL) != 0)
1202 fcaps = &nd.ni_filecaps;
1206 error = finstall_refed(td, fp, &indx, flags, fcaps);
1207 /* On success finstall_refed() consumes fcaps. */
1209 filecaps_free(&nd.ni_filecaps);
1213 filecaps_free(&nd.ni_filecaps);
1214 falloc_abort(td, fp);
1217 td->td_retval[0] = indx;
1220 KASSERT(indx == -1, ("indx=%d, should be -1", indx));
1221 falloc_abort(td, fp);
1229 #ifndef _SYS_SYSPROTO_H_
1230 struct ocreat_args {
1236 ocreat(struct thread *td, struct ocreat_args *uap)
1239 return (kern_openat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1240 O_WRONLY | O_CREAT | O_TRUNC, uap->mode));
1242 #endif /* COMPAT_43 */
1245 * Create a special file.
1247 #ifndef _SYS_SYSPROTO_H_
1248 struct mknodat_args {
1256 sys_mknodat(struct thread *td, struct mknodat_args *uap)
1259 return (kern_mknodat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode,
1263 #if defined(COMPAT_FREEBSD11)
1265 freebsd11_mknod(struct thread *td,
1266 struct freebsd11_mknod_args *uap)
1269 return (kern_mknodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1270 uap->mode, uap->dev));
1274 freebsd11_mknodat(struct thread *td,
1275 struct freebsd11_mknodat_args *uap)
1278 return (kern_mknodat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode,
1281 #endif /* COMPAT_FREEBSD11 */
1284 kern_mknodat(struct thread *td, int fd, const char *path, enum uio_seg pathseg,
1285 int mode, dev_t dev)
1290 struct nameidata nd;
1291 int error, whiteout = 0;
1293 AUDIT_ARG_MODE(mode);
1295 switch (mode & S_IFMT) {
1298 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
1299 if (error == 0 && dev == VNOVAL)
1303 error = priv_check(td, PRIV_VFS_MKNOD_WHT);
1307 return (kern_mkfifoat(td, fd, path, pathseg, mode));
1317 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
1318 NOCACHE, pathseg, path, fd, &cap_mknodat_rights,
1320 if ((error = namei(&nd)) != 0)
1324 NDFREE(&nd, NDF_ONLY_PNBUF);
1325 if (vp == nd.ni_dvp)
1333 vattr.va_mode = (mode & ALLPERMS) &
1334 ~td->td_proc->p_pd->pd_cmask;
1335 vattr.va_rdev = dev;
1338 switch (mode & S_IFMT) {
1340 vattr.va_type = VCHR;
1343 vattr.va_type = VBLK;
1349 panic("kern_mknod: invalid mode");
1352 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1353 NDFREE(&nd, NDF_ONLY_PNBUF);
1355 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1360 if (error == 0 && !whiteout)
1361 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp,
1362 &nd.ni_cnd, &vattr);
1366 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, CREATE);
1368 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp,
1369 &nd.ni_cnd, &vattr);
1372 VOP_VPUT_PAIR(nd.ni_dvp, error == 0 && !whiteout ? &nd.ni_vp : NULL,
1374 vn_finished_write(mp);
1375 NDFREE(&nd, NDF_ONLY_PNBUF);
1376 if (error == ERELOOKUP)
1382 * Create a named pipe.
1384 #ifndef _SYS_SYSPROTO_H_
1385 struct mkfifo_args {
1391 sys_mkfifo(struct thread *td, struct mkfifo_args *uap)
1394 return (kern_mkfifoat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1398 #ifndef _SYS_SYSPROTO_H_
1399 struct mkfifoat_args {
1406 sys_mkfifoat(struct thread *td, struct mkfifoat_args *uap)
1409 return (kern_mkfifoat(td, uap->fd, uap->path, UIO_USERSPACE,
1414 kern_mkfifoat(struct thread *td, int fd, const char *path,
1415 enum uio_seg pathseg, int mode)
1419 struct nameidata nd;
1422 AUDIT_ARG_MODE(mode);
1425 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
1426 NOCACHE, pathseg, path, fd, &cap_mkfifoat_rights,
1428 if ((error = namei(&nd)) != 0)
1430 if (nd.ni_vp != NULL) {
1431 NDFREE(&nd, NDF_ONLY_PNBUF);
1432 if (nd.ni_vp == nd.ni_dvp)
1439 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1440 NDFREE(&nd, NDF_ONLY_PNBUF);
1442 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1447 vattr.va_type = VFIFO;
1448 vattr.va_mode = (mode & ALLPERMS) & ~td->td_proc->p_pd->pd_cmask;
1450 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1455 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
1459 VOP_VPUT_PAIR(nd.ni_dvp, error == 0 ? &nd.ni_vp : NULL, true);
1460 vn_finished_write(mp);
1461 NDFREE(&nd, NDF_ONLY_PNBUF);
1462 if (error == ERELOOKUP)
1468 * Make a hard file link.
1470 #ifndef _SYS_SYSPROTO_H_
1477 sys_link(struct thread *td, struct link_args *uap)
1480 return (kern_linkat(td, AT_FDCWD, AT_FDCWD, uap->path, uap->link,
1481 UIO_USERSPACE, FOLLOW));
1484 #ifndef _SYS_SYSPROTO_H_
1485 struct linkat_args {
1494 sys_linkat(struct thread *td, struct linkat_args *uap)
1499 if ((flag & ~(AT_SYMLINK_FOLLOW | AT_RESOLVE_BENEATH)) != 0)
1502 return (kern_linkat(td, uap->fd1, uap->fd2, uap->path1, uap->path2,
1503 UIO_USERSPACE, at2cnpflags(flag, AT_SYMLINK_FOLLOW |
1504 AT_RESOLVE_BENEATH)));
1507 int hardlink_check_uid = 0;
1508 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
1509 &hardlink_check_uid, 0,
1510 "Unprivileged processes cannot create hard links to files owned by other "
1512 static int hardlink_check_gid = 0;
1513 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
1514 &hardlink_check_gid, 0,
1515 "Unprivileged processes cannot create hard links to files owned by other "
1519 can_hardlink(struct vnode *vp, struct ucred *cred)
1524 if (!hardlink_check_uid && !hardlink_check_gid)
1527 error = VOP_GETATTR(vp, &va, cred);
1531 if (hardlink_check_uid && cred->cr_uid != va.va_uid) {
1532 error = priv_check_cred(cred, PRIV_VFS_LINK);
1537 if (hardlink_check_gid && !groupmember(va.va_gid, cred)) {
1538 error = priv_check_cred(cred, PRIV_VFS_LINK);
1547 kern_linkat(struct thread *td, int fd1, int fd2, const char *path1,
1548 const char *path2, enum uio_seg segflag, int follow)
1550 struct nameidata nd;
1555 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, segflag,
1556 path1, fd1, &cap_linkat_source_rights, td);
1557 if ((error = namei(&nd)) != 0)
1559 NDFREE(&nd, NDF_ONLY_PNBUF);
1560 error = kern_linkat_vp(td, nd.ni_vp, fd2, path2, segflag);
1561 } while (error == EAGAIN || error == ERELOOKUP);
1566 kern_linkat_vp(struct thread *td, struct vnode *vp, int fd, const char *path,
1567 enum uio_seg segflag)
1569 struct nameidata nd;
1573 if (vp->v_type == VDIR) {
1575 return (EPERM); /* POSIX */
1577 NDINIT_ATRIGHTS(&nd, CREATE,
1578 LOCKPARENT | SAVENAME | AUDITVNODE2 | NOCACHE, segflag, path, fd,
1579 &cap_linkat_target_rights, td);
1580 if ((error = namei(&nd)) == 0) {
1581 if (nd.ni_vp != NULL) {
1582 NDFREE(&nd, NDF_ONLY_PNBUF);
1583 if (nd.ni_dvp == nd.ni_vp)
1590 } else if (nd.ni_dvp->v_mount != vp->v_mount) {
1592 * Cross-device link. No need to recheck
1593 * vp->v_type, since it cannot change, except
1596 NDFREE(&nd, NDF_ONLY_PNBUF);
1600 } else if ((error = vn_lock(vp, LK_EXCLUSIVE)) == 0) {
1601 error = can_hardlink(vp, td->td_ucred);
1604 error = mac_vnode_check_link(td->td_ucred,
1605 nd.ni_dvp, vp, &nd.ni_cnd);
1610 NDFREE(&nd, NDF_ONLY_PNBUF);
1613 error = vn_start_write(vp, &mp, V_NOWAIT);
1617 NDFREE(&nd, NDF_ONLY_PNBUF);
1618 error = vn_start_write(NULL, &mp,
1624 error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
1625 VOP_VPUT_PAIR(nd.ni_dvp, &vp, true);
1626 vn_finished_write(mp);
1627 NDFREE(&nd, NDF_ONLY_PNBUF);
1631 NDFREE(&nd, NDF_ONLY_PNBUF);
1642 * Make a symbolic link.
1644 #ifndef _SYS_SYSPROTO_H_
1645 struct symlink_args {
1651 sys_symlink(struct thread *td, struct symlink_args *uap)
1654 return (kern_symlinkat(td, uap->path, AT_FDCWD, uap->link,
1658 #ifndef _SYS_SYSPROTO_H_
1659 struct symlinkat_args {
1666 sys_symlinkat(struct thread *td, struct symlinkat_args *uap)
1669 return (kern_symlinkat(td, uap->path1, uap->fd, uap->path2,
1674 kern_symlinkat(struct thread *td, const char *path1, int fd, const char *path2,
1675 enum uio_seg segflg)
1679 const char *syspath;
1681 struct nameidata nd;
1684 if (segflg == UIO_SYSSPACE) {
1687 tmppath = uma_zalloc(namei_zone, M_WAITOK);
1688 if ((error = copyinstr(path1, tmppath, MAXPATHLEN, NULL)) != 0)
1692 AUDIT_ARG_TEXT(syspath);
1695 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
1696 NOCACHE, segflg, path2, fd, &cap_symlinkat_rights,
1698 if ((error = namei(&nd)) != 0)
1701 NDFREE(&nd, NDF_ONLY_PNBUF);
1702 if (nd.ni_vp == nd.ni_dvp)
1711 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1712 NDFREE(&nd, NDF_ONLY_PNBUF);
1714 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1719 vattr.va_mode = ACCESSPERMS &~ td->td_proc->p_pd->pd_cmask;
1721 vattr.va_type = VLNK;
1722 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1727 error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr, syspath);
1731 VOP_VPUT_PAIR(nd.ni_dvp, error == 0 ? &nd.ni_vp : NULL, true);
1732 vn_finished_write(mp);
1733 NDFREE(&nd, NDF_ONLY_PNBUF);
1734 if (error == ERELOOKUP)
1737 if (segflg != UIO_SYSSPACE)
1738 uma_zfree(namei_zone, tmppath);
1743 * Delete a whiteout from the filesystem.
1745 #ifndef _SYS_SYSPROTO_H_
1746 struct undelete_args {
1751 sys_undelete(struct thread *td, struct undelete_args *uap)
1754 struct nameidata nd;
1759 NDINIT(&nd, DELETE, LOCKPARENT | DOWHITEOUT | AUDITVNODE1,
1760 UIO_USERSPACE, uap->path, td);
1765 if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & ISWHITEOUT)) {
1766 NDFREE(&nd, NDF_ONLY_PNBUF);
1767 if (nd.ni_vp == nd.ni_dvp)
1775 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1776 NDFREE(&nd, NDF_ONLY_PNBUF);
1778 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1782 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, DELETE);
1783 NDFREE(&nd, NDF_ONLY_PNBUF);
1785 vn_finished_write(mp);
1786 if (error == ERELOOKUP)
1792 * Delete a name from the filesystem.
1794 #ifndef _SYS_SYSPROTO_H_
1795 struct unlink_args {
1800 sys_unlink(struct thread *td, struct unlink_args *uap)
1803 return (kern_funlinkat(td, AT_FDCWD, uap->path, FD_NONE, UIO_USERSPACE,
1808 kern_funlinkat_ex(struct thread *td, int dfd, const char *path, int fd,
1809 int flag, enum uio_seg pathseg, ino_t oldinum)
1812 if ((flag & ~(AT_REMOVEDIR | AT_RESOLVE_BENEATH)) != 0)
1815 if ((flag & AT_REMOVEDIR) != 0)
1816 return (kern_frmdirat(td, dfd, path, fd, UIO_USERSPACE, 0));
1818 return (kern_funlinkat(td, dfd, path, fd, UIO_USERSPACE, 0, 0));
1821 #ifndef _SYS_SYSPROTO_H_
1822 struct unlinkat_args {
1829 sys_unlinkat(struct thread *td, struct unlinkat_args *uap)
1832 return (kern_funlinkat_ex(td, uap->fd, uap->path, FD_NONE, uap->flag,
1836 #ifndef _SYS_SYSPROTO_H_
1837 struct funlinkat_args {
1845 sys_funlinkat(struct thread *td, struct funlinkat_args *uap)
1848 return (kern_funlinkat_ex(td, uap->dfd, uap->path, uap->fd, uap->flag,
1853 kern_funlinkat(struct thread *td, int dfd, const char *path, int fd,
1854 enum uio_seg pathseg, int flag, ino_t oldinum)
1859 struct nameidata nd;
1864 if (fd != FD_NONE) {
1865 error = getvnode(td, fd, &cap_no_rights, &fp);
1872 NDINIT_ATRIGHTS(&nd, DELETE, LOCKPARENT | LOCKLEAF | AUDITVNODE1 |
1873 at2cnpflags(flag, AT_RESOLVE_BENEATH),
1874 pathseg, path, dfd, &cap_unlinkat_rights, td);
1875 if ((error = namei(&nd)) != 0) {
1876 if (error == EINVAL)
1881 if (vp->v_type == VDIR && oldinum == 0) {
1882 error = EPERM; /* POSIX */
1883 } else if (oldinum != 0 &&
1884 ((error = VOP_STAT(vp, &sb, td->td_ucred, NOCRED, td)) == 0) &&
1885 sb.st_ino != oldinum) {
1886 error = EIDRM; /* Identifier removed */
1887 } else if (fp != NULL && fp->f_vnode != vp) {
1888 if (VN_IS_DOOMED(fp->f_vnode))
1894 * The root of a mounted filesystem cannot be deleted.
1896 * XXX: can this only be a VDIR case?
1898 if (vp->v_vflag & VV_ROOT)
1902 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1903 NDFREE(&nd, NDF_ONLY_PNBUF);
1905 if (vp == nd.ni_dvp)
1909 if ((error = vn_start_write(NULL, &mp,
1910 V_XSLEEP | PCATCH)) != 0) {
1916 error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp,
1921 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_UNLINK);
1922 error = VOP_REMOVE(nd.ni_dvp, vp, &nd.ni_cnd);
1926 vn_finished_write(mp);
1928 NDFREE(&nd, NDF_ONLY_PNBUF);
1930 if (vp == nd.ni_dvp)
1934 if (error == ERELOOKUP)
1943 * Reposition read/write file offset.
1945 #ifndef _SYS_SYSPROTO_H_
1954 sys_lseek(struct thread *td, struct lseek_args *uap)
1957 return (kern_lseek(td, uap->fd, uap->offset, uap->whence));
1961 kern_lseek(struct thread *td, int fd, off_t offset, int whence)
1967 error = fget(td, fd, &cap_seek_rights, &fp);
1970 error = (fp->f_ops->fo_flags & DFLAG_SEEKABLE) != 0 ?
1971 fo_seek(fp, offset, whence, td) : ESPIPE;
1976 #if defined(COMPAT_43)
1978 * Reposition read/write file offset.
1980 #ifndef _SYS_SYSPROTO_H_
1981 struct olseek_args {
1988 olseek(struct thread *td, struct olseek_args *uap)
1991 return (kern_lseek(td, uap->fd, uap->offset, uap->whence));
1993 #endif /* COMPAT_43 */
1995 #if defined(COMPAT_FREEBSD6)
1996 /* Version with the 'pad' argument */
1998 freebsd6_lseek(struct thread *td, struct freebsd6_lseek_args *uap)
2001 return (kern_lseek(td, uap->fd, uap->offset, uap->whence));
2006 * Check access permissions using passed credentials.
2009 vn_access(struct vnode *vp, int user_flags, struct ucred *cred,
2015 /* Flags == 0 means only check for existence. */
2016 if (user_flags == 0)
2020 if (user_flags & R_OK)
2022 if (user_flags & W_OK)
2024 if (user_flags & X_OK)
2027 error = mac_vnode_check_access(cred, vp, accmode);
2031 if ((accmode & VWRITE) == 0 || (error = vn_writechk(vp)) == 0)
2032 error = VOP_ACCESS(vp, accmode, cred, td);
2037 * Check access permissions using "real" credentials.
2039 #ifndef _SYS_SYSPROTO_H_
2040 struct access_args {
2046 sys_access(struct thread *td, struct access_args *uap)
2049 return (kern_accessat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2053 #ifndef _SYS_SYSPROTO_H_
2054 struct faccessat_args {
2062 sys_faccessat(struct thread *td, struct faccessat_args *uap)
2065 return (kern_accessat(td, uap->fd, uap->path, UIO_USERSPACE, uap->flag,
2070 kern_accessat(struct thread *td, int fd, const char *path,
2071 enum uio_seg pathseg, int flag, int amode)
2073 struct ucred *cred, *usecred;
2075 struct nameidata nd;
2078 if ((flag & ~(AT_EACCESS | AT_RESOLVE_BENEATH)) != 0)
2080 if (amode != F_OK && (amode & ~(R_OK | W_OK | X_OK)) != 0)
2084 * Create and modify a temporary credential instead of one that
2085 * is potentially shared (if we need one).
2087 cred = td->td_ucred;
2088 if ((flag & AT_EACCESS) == 0 &&
2089 ((cred->cr_uid != cred->cr_ruid ||
2090 cred->cr_rgid != cred->cr_groups[0]))) {
2091 usecred = crdup(cred);
2092 usecred->cr_uid = cred->cr_ruid;
2093 usecred->cr_groups[0] = cred->cr_rgid;
2094 td->td_ucred = usecred;
2097 AUDIT_ARG_VALUE(amode);
2098 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF |
2099 AUDITVNODE1 | at2cnpflags(flag, AT_RESOLVE_BENEATH),
2100 pathseg, path, fd, &cap_fstat_rights, td);
2101 if ((error = namei(&nd)) != 0)
2105 error = vn_access(vp, amode, usecred, td);
2106 NDFREE_NOTHING(&nd);
2109 if (usecred != cred) {
2110 td->td_ucred = cred;
2117 * Check access permissions using "effective" credentials.
2119 #ifndef _SYS_SYSPROTO_H_
2120 struct eaccess_args {
2126 sys_eaccess(struct thread *td, struct eaccess_args *uap)
2129 return (kern_accessat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2130 AT_EACCESS, uap->amode));
2133 #if defined(COMPAT_43)
2135 * Get file status; this version follows links.
2137 #ifndef _SYS_SYSPROTO_H_
2144 ostat(struct thread *td, struct ostat_args *uap)
2150 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
2155 return (copyout(&osb, uap->ub, sizeof (osb)));
2159 * Get file status; this version does not follow links.
2161 #ifndef _SYS_SYSPROTO_H_
2162 struct olstat_args {
2168 olstat(struct thread *td, struct olstat_args *uap)
2174 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2175 UIO_USERSPACE, &sb, NULL);
2179 return (copyout(&osb, uap->ub, sizeof (osb)));
2183 * Convert from an old to a new stat structure.
2184 * XXX: many values are blindly truncated.
2187 cvtstat(struct stat *st, struct ostat *ost)
2190 bzero(ost, sizeof(*ost));
2191 ost->st_dev = st->st_dev;
2192 ost->st_ino = st->st_ino;
2193 ost->st_mode = st->st_mode;
2194 ost->st_nlink = st->st_nlink;
2195 ost->st_uid = st->st_uid;
2196 ost->st_gid = st->st_gid;
2197 ost->st_rdev = st->st_rdev;
2198 ost->st_size = MIN(st->st_size, INT32_MAX);
2199 ost->st_atim = st->st_atim;
2200 ost->st_mtim = st->st_mtim;
2201 ost->st_ctim = st->st_ctim;
2202 ost->st_blksize = st->st_blksize;
2203 ost->st_blocks = st->st_blocks;
2204 ost->st_flags = st->st_flags;
2205 ost->st_gen = st->st_gen;
2207 #endif /* COMPAT_43 */
2209 #if defined(COMPAT_43) || defined(COMPAT_FREEBSD11)
2210 int ino64_trunc_error;
2211 SYSCTL_INT(_vfs, OID_AUTO, ino64_trunc_error, CTLFLAG_RW,
2212 &ino64_trunc_error, 0,
2213 "Error on truncation of device, file or inode number, or link count");
2216 freebsd11_cvtstat(struct stat *st, struct freebsd11_stat *ost)
2219 ost->st_dev = st->st_dev;
2220 if (ost->st_dev != st->st_dev) {
2221 switch (ino64_trunc_error) {
2224 * Since dev_t is almost raw, don't clamp to the
2225 * maximum for case 2, but ignore the error.
2232 ost->st_ino = st->st_ino;
2233 if (ost->st_ino != st->st_ino) {
2234 switch (ino64_trunc_error) {
2241 ost->st_ino = UINT32_MAX;
2245 ost->st_mode = st->st_mode;
2246 ost->st_nlink = st->st_nlink;
2247 if (ost->st_nlink != st->st_nlink) {
2248 switch (ino64_trunc_error) {
2255 ost->st_nlink = UINT16_MAX;
2259 ost->st_uid = st->st_uid;
2260 ost->st_gid = st->st_gid;
2261 ost->st_rdev = st->st_rdev;
2262 if (ost->st_rdev != st->st_rdev) {
2263 switch (ino64_trunc_error) {
2270 ost->st_atim = st->st_atim;
2271 ost->st_mtim = st->st_mtim;
2272 ost->st_ctim = st->st_ctim;
2273 ost->st_size = st->st_size;
2274 ost->st_blocks = st->st_blocks;
2275 ost->st_blksize = st->st_blksize;
2276 ost->st_flags = st->st_flags;
2277 ost->st_gen = st->st_gen;
2279 ost->st_birthtim = st->st_birthtim;
2280 bzero((char *)&ost->st_birthtim + sizeof(ost->st_birthtim),
2281 sizeof(*ost) - offsetof(struct freebsd11_stat,
2282 st_birthtim) - sizeof(ost->st_birthtim));
2287 freebsd11_stat(struct thread *td, struct freebsd11_stat_args* uap)
2290 struct freebsd11_stat osb;
2293 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
2297 error = freebsd11_cvtstat(&sb, &osb);
2299 error = copyout(&osb, uap->ub, sizeof(osb));
2304 freebsd11_lstat(struct thread *td, struct freebsd11_lstat_args* uap)
2307 struct freebsd11_stat osb;
2310 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2311 UIO_USERSPACE, &sb, NULL);
2314 error = freebsd11_cvtstat(&sb, &osb);
2316 error = copyout(&osb, uap->ub, sizeof(osb));
2321 freebsd11_fhstat(struct thread *td, struct freebsd11_fhstat_args* uap)
2325 struct freebsd11_stat osb;
2328 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
2331 error = kern_fhstat(td, fh, &sb);
2334 error = freebsd11_cvtstat(&sb, &osb);
2336 error = copyout(&osb, uap->sb, sizeof(osb));
2341 freebsd11_fstatat(struct thread *td, struct freebsd11_fstatat_args* uap)
2344 struct freebsd11_stat osb;
2347 error = kern_statat(td, uap->flag, uap->fd, uap->path,
2348 UIO_USERSPACE, &sb, NULL);
2351 error = freebsd11_cvtstat(&sb, &osb);
2353 error = copyout(&osb, uap->buf, sizeof(osb));
2356 #endif /* COMPAT_FREEBSD11 */
2361 #ifndef _SYS_SYSPROTO_H_
2362 struct fstatat_args {
2370 sys_fstatat(struct thread *td, struct fstatat_args *uap)
2375 error = kern_statat(td, uap->flag, uap->fd, uap->path,
2376 UIO_USERSPACE, &sb, NULL);
2378 error = copyout(&sb, uap->buf, sizeof (sb));
2383 kern_statat(struct thread *td, int flag, int fd, const char *path,
2384 enum uio_seg pathseg, struct stat *sbp,
2385 void (*hook)(struct vnode *vp, struct stat *sbp))
2387 struct nameidata nd;
2390 if ((flag & ~(AT_SYMLINK_NOFOLLOW | AT_RESOLVE_BENEATH)) != 0)
2393 NDINIT_ATRIGHTS(&nd, LOOKUP, at2cnpflags(flag, AT_RESOLVE_BENEATH |
2394 AT_SYMLINK_NOFOLLOW) | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
2395 pathseg, path, fd, &cap_fstat_rights, td);
2397 if ((error = namei(&nd)) != 0)
2399 error = VOP_STAT(nd.ni_vp, sbp, td->td_ucred, NOCRED, td);
2401 if (__predict_false(hook != NULL))
2402 hook(nd.ni_vp, sbp);
2404 NDFREE_NOTHING(&nd);
2406 #ifdef __STAT_TIME_T_EXT
2407 sbp->st_atim_ext = 0;
2408 sbp->st_mtim_ext = 0;
2409 sbp->st_ctim_ext = 0;
2410 sbp->st_btim_ext = 0;
2413 if (KTRPOINT(td, KTR_STRUCT))
2414 ktrstat_error(sbp, error);
2419 #if defined(COMPAT_FREEBSD11)
2421 * Implementation of the NetBSD [l]stat() functions.
2424 freebsd11_cvtnstat(struct stat *sb, struct nstat *nsb)
2427 bzero(nsb, sizeof(*nsb));
2428 nsb->st_dev = sb->st_dev;
2429 nsb->st_ino = sb->st_ino;
2430 nsb->st_mode = sb->st_mode;
2431 nsb->st_nlink = sb->st_nlink;
2432 nsb->st_uid = sb->st_uid;
2433 nsb->st_gid = sb->st_gid;
2434 nsb->st_rdev = sb->st_rdev;
2435 nsb->st_atim = sb->st_atim;
2436 nsb->st_mtim = sb->st_mtim;
2437 nsb->st_ctim = sb->st_ctim;
2438 nsb->st_size = sb->st_size;
2439 nsb->st_blocks = sb->st_blocks;
2440 nsb->st_blksize = sb->st_blksize;
2441 nsb->st_flags = sb->st_flags;
2442 nsb->st_gen = sb->st_gen;
2443 nsb->st_birthtim = sb->st_birthtim;
2446 #ifndef _SYS_SYSPROTO_H_
2447 struct freebsd11_nstat_args {
2453 freebsd11_nstat(struct thread *td, struct freebsd11_nstat_args *uap)
2459 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
2463 freebsd11_cvtnstat(&sb, &nsb);
2464 return (copyout(&nsb, uap->ub, sizeof (nsb)));
2468 * NetBSD lstat. Get file status; this version does not follow links.
2470 #ifndef _SYS_SYSPROTO_H_
2471 struct freebsd11_nlstat_args {
2477 freebsd11_nlstat(struct thread *td, struct freebsd11_nlstat_args *uap)
2483 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2484 UIO_USERSPACE, &sb, NULL);
2487 freebsd11_cvtnstat(&sb, &nsb);
2488 return (copyout(&nsb, uap->ub, sizeof (nsb)));
2490 #endif /* COMPAT_FREEBSD11 */
2493 * Get configurable pathname variables.
2495 #ifndef _SYS_SYSPROTO_H_
2496 struct pathconf_args {
2502 sys_pathconf(struct thread *td, struct pathconf_args *uap)
2507 error = kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name, FOLLOW,
2510 td->td_retval[0] = value;
2514 #ifndef _SYS_SYSPROTO_H_
2515 struct lpathconf_args {
2521 sys_lpathconf(struct thread *td, struct lpathconf_args *uap)
2526 error = kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name,
2529 td->td_retval[0] = value;
2534 kern_pathconf(struct thread *td, const char *path, enum uio_seg pathseg,
2535 int name, u_long flags, long *valuep)
2537 struct nameidata nd;
2540 NDINIT(&nd, LOOKUP, LOCKSHARED | LOCKLEAF | AUDITVNODE1 | flags,
2542 if ((error = namei(&nd)) != 0)
2544 NDFREE_NOTHING(&nd);
2546 error = VOP_PATHCONF(nd.ni_vp, name, valuep);
2552 * Return target name of a symbolic link.
2554 #ifndef _SYS_SYSPROTO_H_
2555 struct readlink_args {
2562 sys_readlink(struct thread *td, struct readlink_args *uap)
2565 return (kern_readlinkat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2566 uap->buf, UIO_USERSPACE, uap->count));
2568 #ifndef _SYS_SYSPROTO_H_
2569 struct readlinkat_args {
2577 sys_readlinkat(struct thread *td, struct readlinkat_args *uap)
2580 return (kern_readlinkat(td, uap->fd, uap->path, UIO_USERSPACE,
2581 uap->buf, UIO_USERSPACE, uap->bufsize));
2585 kern_readlinkat(struct thread *td, int fd, const char *path,
2586 enum uio_seg pathseg, char *buf, enum uio_seg bufseg, size_t count)
2589 struct nameidata nd;
2592 if (count > IOSIZE_MAX)
2595 NDINIT_AT(&nd, LOOKUP, NOFOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
2596 pathseg, path, fd, td);
2598 if ((error = namei(&nd)) != 0)
2600 NDFREE_NOTHING(&nd);
2603 error = kern_readlink_vp(vp, buf, bufseg, count, td);
2610 * Helper function to readlink from a vnode
2613 kern_readlink_vp(struct vnode *vp, char *buf, enum uio_seg bufseg, size_t count,
2620 ASSERT_VOP_LOCKED(vp, "kern_readlink_vp(): vp not locked");
2622 error = mac_vnode_check_readlink(td->td_ucred, vp);
2626 if (vp->v_type != VLNK && (vp->v_vflag & VV_READLINK) == 0)
2629 aiov.iov_base = buf;
2630 aiov.iov_len = count;
2631 auio.uio_iov = &aiov;
2632 auio.uio_iovcnt = 1;
2633 auio.uio_offset = 0;
2634 auio.uio_rw = UIO_READ;
2635 auio.uio_segflg = bufseg;
2637 auio.uio_resid = count;
2638 error = VOP_READLINK(vp, &auio, td->td_ucred);
2639 td->td_retval[0] = count - auio.uio_resid;
2644 * Common implementation code for chflags() and fchflags().
2647 setfflags(struct thread *td, struct vnode *vp, u_long flags)
2653 /* We can't support the value matching VNOVAL. */
2654 if (flags == VNOVAL)
2655 return (EOPNOTSUPP);
2658 * Prevent non-root users from setting flags on devices. When
2659 * a device is reused, users can retain ownership of the device
2660 * if they are allowed to set flags and programs assume that
2661 * chown can't fail when done as root.
2663 if (vp->v_type == VCHR || vp->v_type == VBLK) {
2664 error = priv_check(td, PRIV_VFS_CHFLAGS_DEV);
2669 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2672 vattr.va_flags = flags;
2673 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2675 error = mac_vnode_check_setflags(td->td_ucred, vp, vattr.va_flags);
2678 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
2680 vn_finished_write(mp);
2685 * Change flags of a file given a path name.
2687 #ifndef _SYS_SYSPROTO_H_
2688 struct chflags_args {
2694 sys_chflags(struct thread *td, struct chflags_args *uap)
2697 return (kern_chflagsat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2701 #ifndef _SYS_SYSPROTO_H_
2702 struct chflagsat_args {
2710 sys_chflagsat(struct thread *td, struct chflagsat_args *uap)
2713 if ((uap->atflag & ~(AT_SYMLINK_NOFOLLOW | AT_RESOLVE_BENEATH)) != 0)
2716 return (kern_chflagsat(td, uap->fd, uap->path, UIO_USERSPACE,
2717 uap->flags, uap->atflag));
2721 * Same as chflags() but doesn't follow symlinks.
2723 #ifndef _SYS_SYSPROTO_H_
2724 struct lchflags_args {
2730 sys_lchflags(struct thread *td, struct lchflags_args *uap)
2733 return (kern_chflagsat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2734 uap->flags, AT_SYMLINK_NOFOLLOW));
2738 kern_chflagsat(struct thread *td, int fd, const char *path,
2739 enum uio_seg pathseg, u_long flags, int atflag)
2741 struct nameidata nd;
2744 AUDIT_ARG_FFLAGS(flags);
2745 NDINIT_ATRIGHTS(&nd, LOOKUP, at2cnpflags(atflag, AT_SYMLINK_NOFOLLOW |
2746 AT_RESOLVE_BENEATH) | AUDITVNODE1, pathseg, path, fd,
2747 &cap_fchflags_rights, td);
2748 if ((error = namei(&nd)) != 0)
2750 NDFREE_NOTHING(&nd);
2751 error = setfflags(td, nd.ni_vp, flags);
2757 * Change flags of a file given a file descriptor.
2759 #ifndef _SYS_SYSPROTO_H_
2760 struct fchflags_args {
2766 sys_fchflags(struct thread *td, struct fchflags_args *uap)
2771 AUDIT_ARG_FD(uap->fd);
2772 AUDIT_ARG_FFLAGS(uap->flags);
2773 error = getvnode(td, uap->fd, &cap_fchflags_rights,
2778 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
2779 AUDIT_ARG_VNODE1(fp->f_vnode);
2780 VOP_UNLOCK(fp->f_vnode);
2782 error = setfflags(td, fp->f_vnode, uap->flags);
2788 * Common implementation code for chmod(), lchmod() and fchmod().
2791 setfmode(struct thread *td, struct ucred *cred, struct vnode *vp, int mode)
2797 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2799 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2801 vattr.va_mode = mode & ALLPERMS;
2803 error = mac_vnode_check_setmode(cred, vp, vattr.va_mode);
2806 error = VOP_SETATTR(vp, &vattr, cred);
2808 vn_finished_write(mp);
2813 * Change mode of a file given path name.
2815 #ifndef _SYS_SYSPROTO_H_
2822 sys_chmod(struct thread *td, struct chmod_args *uap)
2825 return (kern_fchmodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2829 #ifndef _SYS_SYSPROTO_H_
2830 struct fchmodat_args {
2838 sys_fchmodat(struct thread *td, struct fchmodat_args *uap)
2841 if ((uap->flag & ~(AT_SYMLINK_NOFOLLOW | AT_RESOLVE_BENEATH)) != 0)
2844 return (kern_fchmodat(td, uap->fd, uap->path, UIO_USERSPACE,
2845 uap->mode, uap->flag));
2849 * Change mode of a file given path name (don't follow links.)
2851 #ifndef _SYS_SYSPROTO_H_
2852 struct lchmod_args {
2858 sys_lchmod(struct thread *td, struct lchmod_args *uap)
2861 return (kern_fchmodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2862 uap->mode, AT_SYMLINK_NOFOLLOW));
2866 kern_fchmodat(struct thread *td, int fd, const char *path,
2867 enum uio_seg pathseg, mode_t mode, int flag)
2869 struct nameidata nd;
2872 AUDIT_ARG_MODE(mode);
2873 NDINIT_ATRIGHTS(&nd, LOOKUP, at2cnpflags(flag, AT_SYMLINK_NOFOLLOW |
2874 AT_RESOLVE_BENEATH) | AUDITVNODE1, pathseg, path, fd,
2875 &cap_fchmod_rights, td);
2876 if ((error = namei(&nd)) != 0)
2878 NDFREE_NOTHING(&nd);
2879 error = setfmode(td, td->td_ucred, nd.ni_vp, mode);
2885 * Change mode of a file given a file descriptor.
2887 #ifndef _SYS_SYSPROTO_H_
2888 struct fchmod_args {
2894 sys_fchmod(struct thread *td, struct fchmod_args *uap)
2899 AUDIT_ARG_FD(uap->fd);
2900 AUDIT_ARG_MODE(uap->mode);
2902 error = fget(td, uap->fd, &cap_fchmod_rights, &fp);
2905 error = fo_chmod(fp, uap->mode, td->td_ucred, td);
2911 * Common implementation for chown(), lchown(), and fchown()
2914 setfown(struct thread *td, struct ucred *cred, struct vnode *vp, uid_t uid,
2921 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2923 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2928 error = mac_vnode_check_setowner(cred, vp, vattr.va_uid,
2932 error = VOP_SETATTR(vp, &vattr, cred);
2934 vn_finished_write(mp);
2939 * Set ownership given a path name.
2941 #ifndef _SYS_SYSPROTO_H_
2949 sys_chown(struct thread *td, struct chown_args *uap)
2952 return (kern_fchownat(td, AT_FDCWD, uap->path, UIO_USERSPACE, uap->uid,
2956 #ifndef _SYS_SYSPROTO_H_
2957 struct fchownat_args {
2966 sys_fchownat(struct thread *td, struct fchownat_args *uap)
2969 if ((uap->flag & ~(AT_SYMLINK_NOFOLLOW | AT_RESOLVE_BENEATH)) != 0)
2972 return (kern_fchownat(td, uap->fd, uap->path, UIO_USERSPACE, uap->uid,
2973 uap->gid, uap->flag));
2977 kern_fchownat(struct thread *td, int fd, const char *path,
2978 enum uio_seg pathseg, int uid, int gid, int flag)
2980 struct nameidata nd;
2983 AUDIT_ARG_OWNER(uid, gid);
2984 NDINIT_ATRIGHTS(&nd, LOOKUP, at2cnpflags(flag, AT_SYMLINK_NOFOLLOW |
2985 AT_RESOLVE_BENEATH) | AUDITVNODE1, pathseg, path, fd,
2986 &cap_fchown_rights, td);
2988 if ((error = namei(&nd)) != 0)
2990 NDFREE_NOTHING(&nd);
2991 error = setfown(td, td->td_ucred, nd.ni_vp, uid, gid);
2997 * Set ownership given a path name, do not cross symlinks.
2999 #ifndef _SYS_SYSPROTO_H_
3000 struct lchown_args {
3007 sys_lchown(struct thread *td, struct lchown_args *uap)
3010 return (kern_fchownat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
3011 uap->uid, uap->gid, AT_SYMLINK_NOFOLLOW));
3015 * Set ownership given a file descriptor.
3017 #ifndef _SYS_SYSPROTO_H_
3018 struct fchown_args {
3025 sys_fchown(struct thread *td, struct fchown_args *uap)
3030 AUDIT_ARG_FD(uap->fd);
3031 AUDIT_ARG_OWNER(uap->uid, uap->gid);
3032 error = fget(td, uap->fd, &cap_fchown_rights, &fp);
3035 error = fo_chown(fp, uap->uid, uap->gid, td->td_ucred, td);
3041 * Common implementation code for utimes(), lutimes(), and futimes().
3044 getutimes(const struct timeval *usrtvp, enum uio_seg tvpseg,
3045 struct timespec *tsp)
3047 struct timeval tv[2];
3048 const struct timeval *tvp;
3051 if (usrtvp == NULL) {
3052 vfs_timestamp(&tsp[0]);
3055 if (tvpseg == UIO_SYSSPACE) {
3058 if ((error = copyin(usrtvp, tv, sizeof(tv))) != 0)
3063 if (tvp[0].tv_usec < 0 || tvp[0].tv_usec >= 1000000 ||
3064 tvp[1].tv_usec < 0 || tvp[1].tv_usec >= 1000000)
3066 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
3067 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
3073 * Common implementation code for futimens(), utimensat().
3075 #define UTIMENS_NULL 0x1
3076 #define UTIMENS_EXIT 0x2
3078 getutimens(const struct timespec *usrtsp, enum uio_seg tspseg,
3079 struct timespec *tsp, int *retflags)
3081 struct timespec tsnow;
3084 vfs_timestamp(&tsnow);
3086 if (usrtsp == NULL) {
3089 *retflags |= UTIMENS_NULL;
3092 if (tspseg == UIO_SYSSPACE) {
3095 } else if ((error = copyin(usrtsp, tsp, sizeof(*tsp) * 2)) != 0)
3097 if (tsp[0].tv_nsec == UTIME_OMIT && tsp[1].tv_nsec == UTIME_OMIT)
3098 *retflags |= UTIMENS_EXIT;
3099 if (tsp[0].tv_nsec == UTIME_NOW && tsp[1].tv_nsec == UTIME_NOW)
3100 *retflags |= UTIMENS_NULL;
3101 if (tsp[0].tv_nsec == UTIME_OMIT)
3102 tsp[0].tv_sec = VNOVAL;
3103 else if (tsp[0].tv_nsec == UTIME_NOW)
3105 else if (tsp[0].tv_nsec < 0 || tsp[0].tv_nsec >= 1000000000L)
3107 if (tsp[1].tv_nsec == UTIME_OMIT)
3108 tsp[1].tv_sec = VNOVAL;
3109 else if (tsp[1].tv_nsec == UTIME_NOW)
3111 else if (tsp[1].tv_nsec < 0 || tsp[1].tv_nsec >= 1000000000L)
3118 * Common implementation code for utimes(), lutimes(), futimes(), futimens(),
3122 setutimes(struct thread *td, struct vnode *vp, const struct timespec *ts,
3123 int numtimes, int nullflag)
3127 int error, setbirthtime;
3129 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
3131 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3133 if (numtimes < 3 && !VOP_GETATTR(vp, &vattr, td->td_ucred) &&
3134 timespeccmp(&ts[1], &vattr.va_birthtime, < ))
3137 vattr.va_atime = ts[0];
3138 vattr.va_mtime = ts[1];
3140 vattr.va_birthtime = ts[1];
3142 vattr.va_birthtime = ts[2];
3144 vattr.va_vaflags |= VA_UTIMES_NULL;
3146 error = mac_vnode_check_setutimes(td->td_ucred, vp, vattr.va_atime,
3150 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3152 vn_finished_write(mp);
3157 * Set the access and modification times of a file.
3159 #ifndef _SYS_SYSPROTO_H_
3160 struct utimes_args {
3162 struct timeval *tptr;
3166 sys_utimes(struct thread *td, struct utimes_args *uap)
3169 return (kern_utimesat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
3170 uap->tptr, UIO_USERSPACE));
3173 #ifndef _SYS_SYSPROTO_H_
3174 struct futimesat_args {
3177 const struct timeval * times;
3181 sys_futimesat(struct thread *td, struct futimesat_args *uap)
3184 return (kern_utimesat(td, uap->fd, uap->path, UIO_USERSPACE,
3185 uap->times, UIO_USERSPACE));
3189 kern_utimesat(struct thread *td, int fd, const char *path,
3190 enum uio_seg pathseg, struct timeval *tptr, enum uio_seg tptrseg)
3192 struct nameidata nd;
3193 struct timespec ts[2];
3196 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3198 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, fd,
3199 &cap_futimes_rights, td);
3201 if ((error = namei(&nd)) != 0)
3203 NDFREE_NOTHING(&nd);
3204 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3210 * Set the access and modification times of a file.
3212 #ifndef _SYS_SYSPROTO_H_
3213 struct lutimes_args {
3215 struct timeval *tptr;
3219 sys_lutimes(struct thread *td, struct lutimes_args *uap)
3222 return (kern_lutimes(td, uap->path, UIO_USERSPACE, uap->tptr,
3227 kern_lutimes(struct thread *td, const char *path, enum uio_seg pathseg,
3228 struct timeval *tptr, enum uio_seg tptrseg)
3230 struct timespec ts[2];
3231 struct nameidata nd;
3234 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3236 NDINIT(&nd, LOOKUP, NOFOLLOW | AUDITVNODE1, pathseg, path, td);
3237 if ((error = namei(&nd)) != 0)
3239 NDFREE_NOTHING(&nd);
3240 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3246 * Set the access and modification times of a file.
3248 #ifndef _SYS_SYSPROTO_H_
3249 struct futimes_args {
3251 struct timeval *tptr;
3255 sys_futimes(struct thread *td, struct futimes_args *uap)
3258 return (kern_futimes(td, uap->fd, uap->tptr, UIO_USERSPACE));
3262 kern_futimes(struct thread *td, int fd, struct timeval *tptr,
3263 enum uio_seg tptrseg)
3265 struct timespec ts[2];
3270 error = getutimes(tptr, tptrseg, ts);
3273 error = getvnode(td, fd, &cap_futimes_rights, &fp);
3277 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
3278 AUDIT_ARG_VNODE1(fp->f_vnode);
3279 VOP_UNLOCK(fp->f_vnode);
3281 error = setutimes(td, fp->f_vnode, ts, 2, tptr == NULL);
3287 sys_futimens(struct thread *td, struct futimens_args *uap)
3290 return (kern_futimens(td, uap->fd, uap->times, UIO_USERSPACE));
3294 kern_futimens(struct thread *td, int fd, struct timespec *tptr,
3295 enum uio_seg tptrseg)
3297 struct timespec ts[2];
3302 error = getutimens(tptr, tptrseg, ts, &flags);
3305 if (flags & UTIMENS_EXIT)
3307 error = getvnode(td, fd, &cap_futimes_rights, &fp);
3311 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
3312 AUDIT_ARG_VNODE1(fp->f_vnode);
3313 VOP_UNLOCK(fp->f_vnode);
3315 error = setutimes(td, fp->f_vnode, ts, 2, flags & UTIMENS_NULL);
3321 sys_utimensat(struct thread *td, struct utimensat_args *uap)
3324 return (kern_utimensat(td, uap->fd, uap->path, UIO_USERSPACE,
3325 uap->times, UIO_USERSPACE, uap->flag));
3329 kern_utimensat(struct thread *td, int fd, const char *path,
3330 enum uio_seg pathseg, struct timespec *tptr, enum uio_seg tptrseg,
3333 struct nameidata nd;
3334 struct timespec ts[2];
3337 if ((flag & ~(AT_SYMLINK_NOFOLLOW | AT_RESOLVE_BENEATH)) != 0)
3340 if ((error = getutimens(tptr, tptrseg, ts, &flags)) != 0)
3342 NDINIT_ATRIGHTS(&nd, LOOKUP, at2cnpflags(flag, AT_SYMLINK_NOFOLLOW |
3343 AT_RESOLVE_BENEATH) | AUDITVNODE1,
3344 pathseg, path, fd, &cap_futimes_rights, td);
3345 if ((error = namei(&nd)) != 0)
3348 * We are allowed to call namei() regardless of 2xUTIME_OMIT.
3350 * "If both tv_nsec fields are UTIME_OMIT... EACCESS may be detected."
3351 * "Search permission is denied by a component of the path prefix."
3353 NDFREE_NOTHING(&nd);
3354 if ((flags & UTIMENS_EXIT) == 0)
3355 error = setutimes(td, nd.ni_vp, ts, 2, flags & UTIMENS_NULL);
3361 * Truncate a file given its path name.
3363 #ifndef _SYS_SYSPROTO_H_
3364 struct truncate_args {
3371 sys_truncate(struct thread *td, struct truncate_args *uap)
3374 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3378 kern_truncate(struct thread *td, const char *path, enum uio_seg pathseg,
3385 struct nameidata nd;
3391 NDINIT(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, td);
3392 if ((error = namei(&nd)) != 0)
3395 rl_cookie = vn_rangelock_wlock(vp, 0, OFF_MAX);
3396 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0) {
3397 vn_rangelock_unlock(vp, rl_cookie);
3401 NDFREE(&nd, NDF_ONLY_PNBUF);
3402 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3403 if (vp->v_type == VDIR)
3406 else if ((error = mac_vnode_check_write(td->td_ucred, NOCRED, vp))) {
3409 else if ((error = vn_writechk(vp)) == 0 &&
3410 (error = VOP_ACCESS(vp, VWRITE, td->td_ucred, td)) == 0) {
3412 vattr.va_size = length;
3413 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3416 vn_finished_write(mp);
3417 vn_rangelock_unlock(vp, rl_cookie);
3419 if (error == ERELOOKUP)
3424 #if defined(COMPAT_43)
3426 * Truncate a file given its path name.
3428 #ifndef _SYS_SYSPROTO_H_
3429 struct otruncate_args {
3435 otruncate(struct thread *td, struct otruncate_args *uap)
3438 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3440 #endif /* COMPAT_43 */
3442 #if defined(COMPAT_FREEBSD6)
3443 /* Versions with the pad argument */
3445 freebsd6_truncate(struct thread *td, struct freebsd6_truncate_args *uap)
3448 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3452 freebsd6_ftruncate(struct thread *td, struct freebsd6_ftruncate_args *uap)
3455 return (kern_ftruncate(td, uap->fd, uap->length));
3460 kern_fsync(struct thread *td, int fd, bool fullsync)
3465 int error, lock_flags;
3468 error = getvnode(td, fd, &cap_fsync_rights, &fp);
3474 /* XXXKIB: compete outstanding aio writes */;
3477 error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
3480 if (MNT_SHARED_WRITES(mp) ||
3481 ((mp == NULL) && MNT_SHARED_WRITES(vp->v_mount))) {
3482 lock_flags = LK_SHARED;
3484 lock_flags = LK_EXCLUSIVE;
3486 vn_lock(vp, lock_flags | LK_RETRY);
3487 AUDIT_ARG_VNODE1(vp);
3488 if (vp->v_object != NULL) {
3489 VM_OBJECT_WLOCK(vp->v_object);
3490 vm_object_page_clean(vp->v_object, 0, 0, 0);
3491 VM_OBJECT_WUNLOCK(vp->v_object);
3493 error = fullsync ? VOP_FSYNC(vp, MNT_WAIT, td) : VOP_FDATASYNC(vp, td);
3495 vn_finished_write(mp);
3496 if (error == ERELOOKUP)
3504 * Sync an open file.
3506 #ifndef _SYS_SYSPROTO_H_
3512 sys_fsync(struct thread *td, struct fsync_args *uap)
3515 return (kern_fsync(td, uap->fd, true));
3519 sys_fdatasync(struct thread *td, struct fdatasync_args *uap)
3522 return (kern_fsync(td, uap->fd, false));
3526 * Rename files. Source and destination must either both be directories, or
3527 * both not be directories. If target is a directory, it must be empty.
3529 #ifndef _SYS_SYSPROTO_H_
3530 struct rename_args {
3536 sys_rename(struct thread *td, struct rename_args *uap)
3539 return (kern_renameat(td, AT_FDCWD, uap->from, AT_FDCWD,
3540 uap->to, UIO_USERSPACE));
3543 #ifndef _SYS_SYSPROTO_H_
3544 struct renameat_args {
3552 sys_renameat(struct thread *td, struct renameat_args *uap)
3555 return (kern_renameat(td, uap->oldfd, uap->old, uap->newfd, uap->new,
3561 kern_renameat_mac(struct thread *td, int oldfd, const char *old, int newfd,
3562 const char *new, enum uio_seg pathseg, struct nameidata *fromnd)
3566 NDINIT_ATRIGHTS(fromnd, DELETE, LOCKPARENT | LOCKLEAF | SAVESTART |
3567 AUDITVNODE1, pathseg, old, oldfd, &cap_renameat_source_rights, td);
3568 if ((error = namei(fromnd)) != 0)
3570 error = mac_vnode_check_rename_from(td->td_ucred, fromnd->ni_dvp,
3571 fromnd->ni_vp, &fromnd->ni_cnd);
3572 VOP_UNLOCK(fromnd->ni_dvp);
3573 if (fromnd->ni_dvp != fromnd->ni_vp)
3574 VOP_UNLOCK(fromnd->ni_vp);
3576 NDFREE(fromnd, NDF_ONLY_PNBUF);
3577 vrele(fromnd->ni_dvp);
3578 vrele(fromnd->ni_vp);
3579 if (fromnd->ni_startdir)
3580 vrele(fromnd->ni_startdir);
3587 kern_renameat(struct thread *td, int oldfd, const char *old, int newfd,
3588 const char *new, enum uio_seg pathseg)
3590 struct mount *mp = NULL;
3591 struct vnode *tvp, *fvp, *tdvp;
3592 struct nameidata fromnd, tond;
3593 u_int64_t tondflags;
3599 if (mac_vnode_check_rename_from_enabled()) {
3600 error = kern_renameat_mac(td, oldfd, old, newfd, new, pathseg,
3606 NDINIT_ATRIGHTS(&fromnd, DELETE, WANTPARENT | SAVESTART | AUDITVNODE1,
3607 pathseg, old, oldfd, &cap_renameat_source_rights, td);
3608 if ((error = namei(&fromnd)) != 0)
3614 tondflags = LOCKPARENT | LOCKLEAF | NOCACHE | SAVESTART | AUDITVNODE2;
3615 if (fromnd.ni_vp->v_type == VDIR)
3616 tondflags |= WILLBEDIR;
3617 NDINIT_ATRIGHTS(&tond, RENAME, tondflags, pathseg, new, newfd,
3618 &cap_renameat_target_rights, td);
3619 if ((error = namei(&tond)) != 0) {
3620 /* Translate error code for rename("dir1", "dir2/."). */
3621 if (error == EISDIR && fvp->v_type == VDIR)
3623 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3624 vrele(fromnd.ni_dvp);
3630 error = vn_start_write(fvp, &mp, V_NOWAIT);
3632 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3633 NDFREE(&tond, NDF_ONLY_PNBUF);
3640 vrele(fromnd.ni_dvp);
3642 vrele(tond.ni_startdir);
3643 if (fromnd.ni_startdir != NULL)
3644 vrele(fromnd.ni_startdir);
3645 error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH);
3651 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
3654 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
3659 if (newfd != AT_FDCWD && (tond.ni_resflags & NIRES_ABS) == 0) {
3661 * If the target already exists we require CAP_UNLINKAT
3662 * from 'newfd', when newfd was used for the lookup.
3664 error = cap_check(&tond.ni_filecaps.fc_rights,
3665 &cap_unlinkat_rights);
3676 * If the source is the same as the destination (that is, if they
3677 * are links to the same vnode), then there is nothing to do.
3683 error = mac_vnode_check_rename_to(td->td_ucred, tdvp,
3684 tond.ni_vp, fromnd.ni_dvp == tdvp, &tond.ni_cnd);
3688 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
3689 tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
3690 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3691 NDFREE(&tond, NDF_ONLY_PNBUF);
3693 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3694 NDFREE(&tond, NDF_ONLY_PNBUF);
3701 vrele(fromnd.ni_dvp);
3704 vrele(tond.ni_startdir);
3705 vn_finished_write(mp);
3707 if (fromnd.ni_startdir)
3708 vrele(fromnd.ni_startdir);
3709 if (error == ERESTART)
3711 if (error == ERELOOKUP)
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,
3753 struct nameidata nd;
3756 AUDIT_ARG_MODE(mode);
3759 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
3760 NC_NOMAKEENTRY | NC_KEEPPOSENTRY | FAILIFEXISTS | WILLBEDIR,
3761 segflg, path, fd, &cap_mkdirat_rights, td);
3762 if ((error = namei(&nd)) != 0)
3764 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3765 NDFREE(&nd, NDF_ONLY_PNBUF);
3767 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3772 vattr.va_type = VDIR;
3773 vattr.va_mode = (mode & ACCESSPERMS) &~ td->td_proc->p_pd->pd_cmask;
3775 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
3780 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
3784 NDFREE(&nd, NDF_ONLY_PNBUF);
3785 VOP_VPUT_PAIR(nd.ni_dvp, error == 0 ? &nd.ni_vp : NULL, true);
3786 vn_finished_write(mp);
3787 if (error == ERELOOKUP)
3793 * Remove a directory file.
3795 #ifndef _SYS_SYSPROTO_H_
3801 sys_rmdir(struct thread *td, struct rmdir_args *uap)
3804 return (kern_frmdirat(td, AT_FDCWD, uap->path, FD_NONE, UIO_USERSPACE,
3809 kern_frmdirat(struct thread *td, int dfd, const char *path, int fd,
3810 enum uio_seg pathseg, int flag)
3815 struct nameidata nd;
3816 cap_rights_t rights;
3820 if (fd != FD_NONE) {
3821 error = getvnode(td, fd, cap_rights_init_one(&rights, CAP_LOOKUP),
3829 NDINIT_ATRIGHTS(&nd, DELETE, LOCKPARENT | LOCKLEAF | AUDITVNODE1 |
3830 at2cnpflags(flag, AT_RESOLVE_BENEATH),
3831 pathseg, path, dfd, &cap_unlinkat_rights, td);
3832 if ((error = namei(&nd)) != 0)
3835 if (vp->v_type != VDIR) {
3840 * No rmdir "." please.
3842 if (nd.ni_dvp == vp) {
3847 * The root of a mounted filesystem cannot be deleted.
3849 if (vp->v_vflag & VV_ROOT) {
3854 if (fp != NULL && fp->f_vnode != vp) {
3855 if (VN_IS_DOOMED(fp->f_vnode))
3863 error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp,
3868 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3869 NDFREE(&nd, NDF_ONLY_PNBUF);
3871 if (nd.ni_dvp == vp)
3875 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3879 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_UNLINK);
3880 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
3881 vn_finished_write(mp);
3883 NDFREE(&nd, NDF_ONLY_PNBUF);
3885 if (nd.ni_dvp == vp)
3889 if (error == ERELOOKUP)
3897 #if defined(COMPAT_43) || defined(COMPAT_FREEBSD11)
3899 freebsd11_kern_getdirentries(struct thread *td, int fd, char *ubuf, u_int count,
3900 long *basep, void (*func)(struct freebsd11_dirent *))
3902 struct freebsd11_dirent dstdp;
3903 struct dirent *dp, *edp;
3906 ssize_t resid, ucount;
3909 /* XXX arbitrary sanity limit on `count'. */
3910 count = min(count, 64 * 1024);
3912 dirbuf = malloc(count, M_TEMP, M_WAITOK);
3914 error = kern_getdirentries(td, fd, dirbuf, count, &base, &resid,
3922 for (dp = (struct dirent *)dirbuf,
3923 edp = (struct dirent *)&dirbuf[count - resid];
3924 ucount < count && dp < edp; ) {
3925 if (dp->d_reclen == 0)
3927 MPASS(dp->d_reclen >= _GENERIC_DIRLEN(0));
3928 if (dp->d_namlen >= sizeof(dstdp.d_name))
3930 dstdp.d_type = dp->d_type;
3931 dstdp.d_namlen = dp->d_namlen;
3932 dstdp.d_fileno = dp->d_fileno; /* truncate */
3933 if (dstdp.d_fileno != dp->d_fileno) {
3934 switch (ino64_trunc_error) {
3942 dstdp.d_fileno = UINT32_MAX;
3946 dstdp.d_reclen = sizeof(dstdp) - sizeof(dstdp.d_name) +
3947 ((dp->d_namlen + 1 + 3) &~ 3);
3948 bcopy(dp->d_name, dstdp.d_name, dstdp.d_namlen);
3949 bzero(dstdp.d_name + dstdp.d_namlen,
3950 dstdp.d_reclen - offsetof(struct freebsd11_dirent, d_name) -
3952 MPASS(dstdp.d_reclen <= dp->d_reclen);
3953 MPASS(ucount + dstdp.d_reclen <= count);
3956 error = copyout(&dstdp, ubuf + ucount, dstdp.d_reclen);
3959 dp = (struct dirent *)((char *)dp + dp->d_reclen);
3960 ucount += dstdp.d_reclen;
3964 free(dirbuf, M_TEMP);
3966 td->td_retval[0] = ucount;
3973 ogetdirentries_cvt(struct freebsd11_dirent *dp)
3975 #if (BYTE_ORDER == LITTLE_ENDIAN)
3977 * The expected low byte of dp->d_namlen is our dp->d_type.
3978 * The high MBZ byte of dp->d_namlen is our dp->d_namlen.
3980 dp->d_type = dp->d_namlen;
3984 * The dp->d_type is the high byte of the expected dp->d_namlen,
3985 * so must be zero'ed.
3992 * Read a block of directory entries in a filesystem independent format.
3994 #ifndef _SYS_SYSPROTO_H_
3995 struct ogetdirentries_args {
4003 ogetdirentries(struct thread *td, struct ogetdirentries_args *uap)
4008 error = kern_ogetdirentries(td, uap, &loff);
4010 error = copyout(&loff, uap->basep, sizeof(long));
4015 kern_ogetdirentries(struct thread *td, struct ogetdirentries_args *uap,
4021 /* XXX arbitrary sanity limit on `count'. */
4022 if (uap->count > 64 * 1024)
4025 error = freebsd11_kern_getdirentries(td, uap->fd, uap->buf, uap->count,
4026 &base, ogetdirentries_cvt);
4028 if (error == 0 && uap->basep != NULL)
4029 error = copyout(&base, uap->basep, sizeof(long));
4033 #endif /* COMPAT_43 */
4035 #if defined(COMPAT_FREEBSD11)
4036 #ifndef _SYS_SYSPROTO_H_
4037 struct freebsd11_getdirentries_args {
4045 freebsd11_getdirentries(struct thread *td,
4046 struct freebsd11_getdirentries_args *uap)
4051 error = freebsd11_kern_getdirentries(td, uap->fd, uap->buf, uap->count,
4054 if (error == 0 && uap->basep != NULL)
4055 error = copyout(&base, uap->basep, sizeof(long));
4060 freebsd11_getdents(struct thread *td, struct freebsd11_getdents_args *uap)
4062 struct freebsd11_getdirentries_args ap;
4066 ap.count = uap->count;
4068 return (freebsd11_getdirentries(td, &ap));
4070 #endif /* COMPAT_FREEBSD11 */
4073 * Read a block of directory entries in a filesystem independent format.
4076 sys_getdirentries(struct thread *td, struct getdirentries_args *uap)
4081 error = kern_getdirentries(td, uap->fd, uap->buf, uap->count, &base,
4082 NULL, UIO_USERSPACE);
4085 if (uap->basep != NULL)
4086 error = copyout(&base, uap->basep, sizeof(off_t));
4091 kern_getdirentries(struct thread *td, int fd, char *buf, size_t count,
4092 off_t *basep, ssize_t *residp, enum uio_seg bufseg)
4103 if (count > IOSIZE_MAX)
4105 auio.uio_resid = count;
4106 error = getvnode(td, fd, &cap_read_rights, &fp);
4109 if ((fp->f_flag & FREAD) == 0) {
4114 foffset = foffset_lock(fp, 0);
4116 if (vp->v_type != VDIR) {
4120 aiov.iov_base = buf;
4121 aiov.iov_len = count;
4122 auio.uio_iov = &aiov;
4123 auio.uio_iovcnt = 1;
4124 auio.uio_rw = UIO_READ;
4125 auio.uio_segflg = bufseg;
4127 vn_lock(vp, LK_SHARED | LK_RETRY);
4128 AUDIT_ARG_VNODE1(vp);
4129 loff = auio.uio_offset = foffset;
4131 error = mac_vnode_check_readdir(td->td_ucred, vp);
4134 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL,
4136 foffset = auio.uio_offset;
4141 if (count == auio.uio_resid &&
4142 (vp->v_vflag & VV_ROOT) &&
4143 (vp->v_mount->mnt_flag & MNT_UNION)) {
4144 struct vnode *tvp = vp;
4146 vp = vp->v_mount->mnt_vnodecovered;
4156 *residp = auio.uio_resid;
4157 td->td_retval[0] = count - auio.uio_resid;
4159 foffset_unlock(fp, foffset, 0);
4165 * Set the mode mask for creation of filesystem nodes.
4167 #ifndef _SYS_SYSPROTO_H_
4173 sys_umask(struct thread *td, struct umask_args *uap)
4175 struct pwddesc *pdp;
4177 pdp = td->td_proc->p_pd;
4179 td->td_retval[0] = pdp->pd_cmask;
4180 pdp->pd_cmask = uap->newmask & ALLPERMS;
4181 PWDDESC_XUNLOCK(pdp);
4186 * Void all references to file by ripping underlying filesystem away from
4189 #ifndef _SYS_SYSPROTO_H_
4190 struct revoke_args {
4195 sys_revoke(struct thread *td, struct revoke_args *uap)
4199 struct nameidata nd;
4202 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
4204 if ((error = namei(&nd)) != 0)
4207 NDFREE_NOTHING(&nd);
4208 if (vp->v_type != VCHR || vp->v_rdev == NULL) {
4213 error = mac_vnode_check_revoke(td->td_ucred, vp);
4217 error = VOP_GETATTR(vp, &vattr, td->td_ucred);
4220 if (td->td_ucred->cr_uid != vattr.va_uid) {
4221 error = priv_check(td, PRIV_VFS_ADMIN);
4225 if (devfs_usecount(vp) > 0)
4226 VOP_REVOKE(vp, REVOKEALL);
4233 * Convert a user file descriptor to a kernel file entry and check that, if it
4234 * is a capability, the correct rights are present. A reference on the file
4235 * entry is held upon returning.
4238 getvnode(struct thread *td, int fd, cap_rights_t *rightsp, struct file **fpp)
4243 error = fget_unlocked(td->td_proc->p_fd, fd, rightsp, &fp);
4248 * The file could be not of the vnode type, or it may be not
4249 * yet fully initialized, in which case the f_vnode pointer
4250 * may be set, but f_ops is still badfileops. E.g.,
4251 * devfs_open() transiently create such situation to
4252 * facilitate csw d_fdopen().
4254 * Dupfdopen() handling in kern_openat() installs the
4255 * half-baked file into the process descriptor table, allowing
4256 * other thread to dereference it. Guard against the race by
4259 if (fp->f_vnode == NULL || fp->f_ops == &badfileops) {
4268 * Get an (NFS) file handle.
4270 #ifndef _SYS_SYSPROTO_H_
4271 struct lgetfh_args {
4277 sys_lgetfh(struct thread *td, struct lgetfh_args *uap)
4280 return (kern_getfhat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->fname,
4281 UIO_USERSPACE, uap->fhp, UIO_USERSPACE));
4284 #ifndef _SYS_SYSPROTO_H_
4291 sys_getfh(struct thread *td, struct getfh_args *uap)
4294 return (kern_getfhat(td, 0, AT_FDCWD, uap->fname, UIO_USERSPACE,
4295 uap->fhp, UIO_USERSPACE));
4299 * syscall for the rpc.lockd to use to translate an open descriptor into
4300 * a NFS file handle.
4302 * warning: do not remove the priv_check() call or this becomes one giant
4305 #ifndef _SYS_SYSPROTO_H_
4306 struct getfhat_args {
4314 sys_getfhat(struct thread *td, struct getfhat_args *uap)
4317 if ((uap->flags & ~(AT_SYMLINK_NOFOLLOW | AT_RESOLVE_BENEATH)) != 0)
4319 return (kern_getfhat(td, uap->flags, uap->fd, uap->path, UIO_USERSPACE,
4320 uap->fhp, UIO_USERSPACE));
4324 kern_getfhat(struct thread *td, int flags, int fd, const char *path,
4325 enum uio_seg pathseg, fhandle_t *fhp, enum uio_seg fhseg)
4327 struct nameidata nd;
4332 error = priv_check(td, PRIV_VFS_GETFH);
4335 NDINIT_AT(&nd, LOOKUP, at2cnpflags(flags, AT_SYMLINK_NOFOLLOW |
4336 AT_RESOLVE_BENEATH) | LOCKLEAF | AUDITVNODE1, pathseg, path,
4341 NDFREE_NOTHING(&nd);
4343 bzero(&fh, sizeof(fh));
4344 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
4345 error = VOP_VPTOFH(vp, &fh.fh_fid);
4348 if (fhseg == UIO_USERSPACE)
4349 error = copyout(&fh, fhp, sizeof (fh));
4351 memcpy(fhp, &fh, sizeof(fh));
4356 #ifndef _SYS_SYSPROTO_H_
4357 struct fhlink_args {
4363 sys_fhlink(struct thread *td, struct fhlink_args *uap)
4366 return (kern_fhlinkat(td, AT_FDCWD, uap->to, UIO_USERSPACE, uap->fhp));
4369 #ifndef _SYS_SYSPROTO_H_
4370 struct fhlinkat_args {
4377 sys_fhlinkat(struct thread *td, struct fhlinkat_args *uap)
4380 return (kern_fhlinkat(td, uap->tofd, uap->to, UIO_USERSPACE, uap->fhp));
4384 kern_fhlinkat(struct thread *td, int fd, const char *path,
4385 enum uio_seg pathseg, fhandle_t *fhp)
4392 error = priv_check(td, PRIV_VFS_GETFH);
4395 error = copyin(fhp, &fh, sizeof(fh));
4400 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4402 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_SHARED, &vp);
4407 error = kern_linkat_vp(td, vp, fd, path, pathseg);
4408 } while (error == EAGAIN || error == ERELOOKUP);
4412 #ifndef _SYS_SYSPROTO_H_
4413 struct fhreadlink_args {
4420 sys_fhreadlink(struct thread *td, struct fhreadlink_args *uap)
4427 error = priv_check(td, PRIV_VFS_GETFH);
4430 if (uap->bufsize > IOSIZE_MAX)
4432 error = copyin(uap->fhp, &fh, sizeof(fh));
4435 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4437 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_SHARED, &vp);
4441 error = kern_readlink_vp(vp, uap->buf, UIO_USERSPACE, uap->bufsize, td);
4447 * syscall for the rpc.lockd to use to translate a NFS file handle into an
4450 * warning: do not remove the priv_check() call or this becomes one giant
4453 #ifndef _SYS_SYSPROTO_H_
4454 struct fhopen_args {
4455 const struct fhandle *u_fhp;
4460 sys_fhopen(struct thread *td, struct fhopen_args *uap)
4462 return (kern_fhopen(td, uap->u_fhp, uap->flags));
4466 kern_fhopen(struct thread *td, const struct fhandle *u_fhp, int flags)
4475 error = priv_check(td, PRIV_VFS_FHOPEN);
4479 fmode = FFLAGS(flags);
4480 /* why not allow a non-read/write open for our lockd? */
4481 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
4483 error = copyin(u_fhp, &fhp, sizeof(fhp));
4486 /* find the mount point */
4487 mp = vfs_busyfs(&fhp.fh_fsid);
4490 /* now give me my vnode, it gets returned to me locked */
4491 error = VFS_FHTOVP(mp, &fhp.fh_fid, LK_EXCLUSIVE, &vp);
4496 error = falloc_noinstall(td, &fp);
4502 * An extra reference on `fp' has been held for us by
4503 * falloc_noinstall().
4509 error = vn_open_vnode(vp, fmode, td->td_ucred, td, fp);
4511 KASSERT(fp->f_ops == &badfileops,
4512 ("VOP_OPEN in fhopen() set f_ops"));
4513 KASSERT(td->td_dupfd < 0,
4514 ("fhopen() encountered fdopen()"));
4523 finit_vnode(fp, fmode, NULL, &vnops);
4525 if ((fmode & O_TRUNC) != 0) {
4526 error = fo_truncate(fp, 0, td->td_ucred, td);
4531 error = finstall(td, fp, &indx, fmode, NULL);
4534 td->td_retval[0] = indx;
4539 * Stat an (NFS) file handle.
4541 #ifndef _SYS_SYSPROTO_H_
4542 struct fhstat_args {
4543 struct fhandle *u_fhp;
4548 sys_fhstat(struct thread *td, struct fhstat_args *uap)
4554 error = copyin(uap->u_fhp, &fh, sizeof(fh));
4557 error = kern_fhstat(td, fh, &sb);
4559 error = copyout(&sb, uap->sb, sizeof(sb));
4564 kern_fhstat(struct thread *td, struct fhandle fh, struct stat *sb)
4570 error = priv_check(td, PRIV_VFS_FHSTAT);
4573 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4575 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_EXCLUSIVE, &vp);
4579 error = VOP_STAT(vp, sb, td->td_ucred, NOCRED, td);
4585 * Implement fstatfs() for (NFS) file handles.
4587 #ifndef _SYS_SYSPROTO_H_
4588 struct fhstatfs_args {
4589 struct fhandle *u_fhp;
4594 sys_fhstatfs(struct thread *td, struct fhstatfs_args *uap)
4600 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4603 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
4604 error = kern_fhstatfs(td, fh, sfp);
4606 error = copyout(sfp, uap->buf, sizeof(*sfp));
4607 free(sfp, M_STATFS);
4612 kern_fhstatfs(struct thread *td, fhandle_t fh, struct statfs *buf)
4618 error = priv_check(td, PRIV_VFS_FHSTATFS);
4621 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4623 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_EXCLUSIVE, &vp);
4629 error = prison_canseemount(td->td_ucred, mp);
4633 error = mac_mount_check_stat(td->td_ucred, mp);
4637 error = VFS_STATFS(mp, buf);
4644 * Unlike madvise(2), we do not make a best effort to remember every
4645 * possible caching hint. Instead, we remember the last setting with
4646 * the exception that we will allow POSIX_FADV_NORMAL to adjust the
4647 * region of any current setting.
4650 kern_posix_fadvise(struct thread *td, int fd, off_t offset, off_t len,
4653 struct fadvise_info *fa, *new;
4659 if (offset < 0 || len < 0 || offset > OFF_MAX - len)
4661 AUDIT_ARG_VALUE(advice);
4663 case POSIX_FADV_SEQUENTIAL:
4664 case POSIX_FADV_RANDOM:
4665 case POSIX_FADV_NOREUSE:
4666 new = malloc(sizeof(*fa), M_FADVISE, M_WAITOK);
4668 case POSIX_FADV_NORMAL:
4669 case POSIX_FADV_WILLNEED:
4670 case POSIX_FADV_DONTNEED:
4676 /* XXX: CAP_POSIX_FADVISE? */
4678 error = fget(td, fd, &cap_no_rights, &fp);
4681 AUDIT_ARG_FILE(td->td_proc, fp);
4682 if ((fp->f_ops->fo_flags & DFLAG_SEEKABLE) == 0) {
4686 if (fp->f_type != DTYPE_VNODE) {
4691 if (vp->v_type != VREG) {
4698 end = offset + len - 1;
4700 case POSIX_FADV_SEQUENTIAL:
4701 case POSIX_FADV_RANDOM:
4702 case POSIX_FADV_NOREUSE:
4704 * Try to merge any existing non-standard region with
4705 * this new region if possible, otherwise create a new
4706 * non-standard region for this request.
4708 mtx_pool_lock(mtxpool_sleep, fp);
4710 if (fa != NULL && fa->fa_advice == advice &&
4711 ((fa->fa_start <= end && fa->fa_end >= offset) ||
4712 (end != OFF_MAX && fa->fa_start == end + 1) ||
4713 (fa->fa_end != OFF_MAX && fa->fa_end + 1 == offset))) {
4714 if (offset < fa->fa_start)
4715 fa->fa_start = offset;
4716 if (end > fa->fa_end)
4719 new->fa_advice = advice;
4720 new->fa_start = offset;
4725 mtx_pool_unlock(mtxpool_sleep, fp);
4727 case POSIX_FADV_NORMAL:
4729 * If a the "normal" region overlaps with an existing
4730 * non-standard region, trim or remove the
4731 * non-standard region.
4733 mtx_pool_lock(mtxpool_sleep, fp);
4736 if (offset <= fa->fa_start && end >= fa->fa_end) {
4738 fp->f_advice = NULL;
4739 } else if (offset <= fa->fa_start &&
4740 end >= fa->fa_start)
4741 fa->fa_start = end + 1;
4742 else if (offset <= fa->fa_end && end >= fa->fa_end)
4743 fa->fa_end = offset - 1;
4744 else if (offset >= fa->fa_start && end <= fa->fa_end) {
4746 * If the "normal" region is a middle
4747 * portion of the existing
4748 * non-standard region, just remove
4749 * the whole thing rather than picking
4750 * one side or the other to
4754 fp->f_advice = NULL;
4757 mtx_pool_unlock(mtxpool_sleep, fp);
4759 case POSIX_FADV_WILLNEED:
4760 case POSIX_FADV_DONTNEED:
4761 error = VOP_ADVISE(vp, offset, end, advice);
4767 free(new, M_FADVISE);
4772 sys_posix_fadvise(struct thread *td, struct posix_fadvise_args *uap)
4776 error = kern_posix_fadvise(td, uap->fd, uap->offset, uap->len,
4778 return (kern_posix_error(td, error));
4782 kern_copy_file_range(struct thread *td, int infd, off_t *inoffp, int outfd,
4783 off_t *outoffp, size_t len, unsigned int flags)
4785 struct file *infp, *outfp;
4786 struct vnode *invp, *outvp;
4789 void *rl_rcookie, *rl_wcookie;
4790 off_t savinoff, savoutoff;
4792 infp = outfp = NULL;
4793 rl_rcookie = rl_wcookie = NULL;
4802 if (len > SSIZE_MAX)
4804 * Although the len argument is size_t, the return argument
4805 * is ssize_t (which is signed). Therefore a size that won't
4806 * fit in ssize_t can't be returned.
4810 /* Get the file structures for the file descriptors. */
4811 error = fget_read(td, infd, &cap_read_rights, &infp);
4814 if (infp->f_ops == &badfileops) {
4818 if (infp->f_vnode == NULL) {
4822 error = fget_write(td, outfd, &cap_write_rights, &outfp);
4825 if (outfp->f_ops == &badfileops) {
4829 if (outfp->f_vnode == NULL) {
4834 /* Set the offset pointers to the correct place. */
4836 inoffp = &infp->f_offset;
4837 if (outoffp == NULL)
4838 outoffp = &outfp->f_offset;
4840 savoutoff = *outoffp;
4842 invp = infp->f_vnode;
4843 outvp = outfp->f_vnode;
4844 /* Sanity check the f_flag bits. */
4845 if ((outfp->f_flag & (FWRITE | FAPPEND)) != FWRITE ||
4846 (infp->f_flag & FREAD) == 0) {
4851 /* If len == 0, just return 0. */
4856 * If infp and outfp refer to the same file, the byte ranges cannot
4859 if (invp == outvp && ((savinoff <= savoutoff && savinoff + len >
4860 savoutoff) || (savinoff > savoutoff && savoutoff + len >
4866 /* Range lock the byte ranges for both invp and outvp. */
4868 rl_wcookie = vn_rangelock_wlock(outvp, *outoffp, *outoffp +
4870 rl_rcookie = vn_rangelock_tryrlock(invp, *inoffp, *inoffp +
4872 if (rl_rcookie != NULL)
4874 vn_rangelock_unlock(outvp, rl_wcookie);
4875 rl_rcookie = vn_rangelock_rlock(invp, *inoffp, *inoffp + len);
4876 vn_rangelock_unlock(invp, rl_rcookie);
4880 error = vn_copy_file_range(invp, inoffp, outvp, outoffp, &retlen,
4881 flags, infp->f_cred, outfp->f_cred, td);
4883 if (rl_rcookie != NULL)
4884 vn_rangelock_unlock(invp, rl_rcookie);
4885 if (rl_wcookie != NULL)
4886 vn_rangelock_unlock(outvp, rl_wcookie);
4887 if (savinoff != -1 && (error == EINTR || error == ERESTART)) {
4889 *outoffp = savoutoff;
4895 td->td_retval[0] = retlen;
4900 sys_copy_file_range(struct thread *td, struct copy_file_range_args *uap)
4902 off_t inoff, outoff, *inoffp, *outoffp;
4905 inoffp = outoffp = NULL;
4906 if (uap->inoffp != NULL) {
4907 error = copyin(uap->inoffp, &inoff, sizeof(off_t));
4912 if (uap->outoffp != NULL) {
4913 error = copyin(uap->outoffp, &outoff, sizeof(off_t));
4918 error = kern_copy_file_range(td, uap->infd, inoffp, uap->outfd,
4919 outoffp, uap->len, uap->flags);
4920 if (error == 0 && uap->inoffp != NULL)
4921 error = copyout(inoffp, uap->inoffp, sizeof(off_t));
4922 if (error == 0 && uap->outoffp != NULL)
4923 error = copyout(outoffp, uap->outoffp, sizeof(off_t));