2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
6 * (c) UNIX System Laboratories, Inc.
7 * All or some portions of this file are derived from material licensed
8 * to the University of California by American Telephone and Telegraph
9 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10 * the permission of UNIX System Laboratories, Inc.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * @(#)vfs_syscalls.c 8.13 (Berkeley) 4/15/94
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
42 #include "opt_capsicum.h"
43 #include "opt_ktrace.h"
45 #include <sys/param.h>
46 #include <sys/systm.h>
49 #include <sys/capsicum.h>
51 #include <sys/sysent.h>
52 #include <sys/malloc.h>
53 #include <sys/mount.h>
54 #include <sys/mutex.h>
55 #include <sys/sysproto.h>
56 #include <sys/namei.h>
57 #include <sys/filedesc.h>
58 #include <sys/kernel.h>
59 #include <sys/fcntl.h>
61 #include <sys/filio.h>
62 #include <sys/limits.h>
63 #include <sys/linker.h>
64 #include <sys/rwlock.h>
68 #include <sys/unistd.h>
69 #include <sys/vnode.h>
72 #include <sys/dirent.h>
74 #include <sys/syscallsubr.h>
75 #include <sys/sysctl.h>
77 #include <sys/ktrace.h>
80 #include <machine/stdarg.h>
82 #include <security/audit/audit.h>
83 #include <security/mac/mac_framework.h>
86 #include <vm/vm_object.h>
87 #include <vm/vm_page.h>
90 #include <ufs/ufs/quota.h>
92 MALLOC_DEFINE(M_FADVISE, "fadvise", "posix_fadvise(2) information");
94 SDT_PROVIDER_DEFINE(vfs);
95 SDT_PROBE_DEFINE2(vfs, , stat, mode, "char *", "int");
96 SDT_PROBE_DEFINE2(vfs, , stat, reg, "char *", "int");
98 static int kern_chflagsat(struct thread *td, int fd, const char *path,
99 enum uio_seg pathseg, u_long flags, int atflag);
100 static int setfflags(struct thread *td, struct vnode *, u_long);
101 static int getutimes(const struct timeval *, enum uio_seg, struct timespec *);
102 static int getutimens(const struct timespec *, enum uio_seg,
103 struct timespec *, int *);
104 static int setutimes(struct thread *td, struct vnode *,
105 const struct timespec *, int, int);
106 static int vn_access(struct vnode *vp, int user_flags, struct ucred *cred,
108 static int kern_fhlinkat(struct thread *td, int fd, const char *path,
109 enum uio_seg pathseg, fhandle_t *fhp);
110 static int kern_getfhat(struct thread *td, int flags, int fd,
111 const char *path, enum uio_seg pathseg, fhandle_t *fhp);
112 static int kern_readlink_vp(struct vnode *vp, char *buf, enum uio_seg bufseg,
113 size_t count, struct thread *td);
114 static int kern_linkat_vp(struct thread *td, struct vnode *vp, int fd,
115 const char *path, enum uio_seg segflag);
118 kern_sync(struct thread *td)
120 struct mount *mp, *nmp;
123 mtx_lock(&mountlist_mtx);
124 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
125 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK)) {
126 nmp = TAILQ_NEXT(mp, mnt_list);
129 if ((mp->mnt_flag & MNT_RDONLY) == 0 &&
130 vn_start_write(NULL, &mp, V_NOWAIT) == 0) {
131 save = curthread_pflags_set(TDP_SYNCIO);
132 vfs_periodic(mp, MNT_NOWAIT);
133 VFS_SYNC(mp, MNT_NOWAIT);
134 curthread_pflags_restore(save);
135 vn_finished_write(mp);
137 mtx_lock(&mountlist_mtx);
138 nmp = TAILQ_NEXT(mp, mnt_list);
141 mtx_unlock(&mountlist_mtx);
146 * Sync each mounted filesystem.
148 #ifndef _SYS_SYSPROTO_H_
155 sys_sync(struct thread *td, struct sync_args *uap)
158 return (kern_sync(td));
162 * Change filesystem quotas.
164 #ifndef _SYS_SYSPROTO_H_
165 struct quotactl_args {
173 sys_quotactl(struct thread *td, struct quotactl_args *uap)
179 AUDIT_ARG_CMD(uap->cmd);
180 AUDIT_ARG_UID(uap->uid);
181 if (!prison_allow(td->td_ucred, PR_ALLOW_QUOTAS))
183 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
185 if ((error = namei(&nd)) != 0)
187 NDFREE(&nd, NDF_ONLY_PNBUF);
188 mp = nd.ni_vp->v_mount;
191 error = vfs_busy(mp, 0);
196 error = VFS_QUOTACTL(mp, uap->cmd, uap->uid, uap->arg);
199 * Since quota on operation typically needs to open quota
200 * file, the Q_QUOTAON handler needs to unbusy the mount point
201 * before calling into namei. Otherwise, unmount might be
202 * started between two vfs_busy() invocations (first is our,
203 * second is from mount point cross-walk code in lookup()),
206 * Require that Q_QUOTAON handles the vfs_busy() reference on
207 * its own, always returning with ubusied mount point.
209 if ((uap->cmd >> SUBCMDSHIFT) != Q_QUOTAON &&
210 (uap->cmd >> SUBCMDSHIFT) != Q_QUOTAOFF)
217 * Used by statfs conversion routines to scale the block size up if
218 * necessary so that all of the block counts are <= 'max_size'. Note
219 * that 'max_size' should be a bitmask, i.e. 2^n - 1 for some non-zero
223 statfs_scale_blocks(struct statfs *sf, long max_size)
228 KASSERT(powerof2(max_size + 1), ("%s: invalid max_size", __func__));
231 * Attempt to scale the block counts to give a more accurate
232 * overview to userland of the ratio of free space to used
233 * space. To do this, find the largest block count and compute
234 * a divisor that lets it fit into a signed integer <= max_size.
236 if (sf->f_bavail < 0)
237 count = -sf->f_bavail;
239 count = sf->f_bavail;
240 count = MAX(sf->f_blocks, MAX(sf->f_bfree, count));
241 if (count <= max_size)
244 count >>= flsl(max_size);
251 sf->f_bsize <<= shift;
252 sf->f_blocks >>= shift;
253 sf->f_bfree >>= shift;
254 sf->f_bavail >>= shift;
258 kern_do_statfs(struct thread *td, struct mount *mp, struct statfs *buf)
264 error = vfs_busy(mp, 0);
269 error = mac_mount_check_stat(td->td_ucred, mp);
273 error = VFS_STATFS(mp, buf);
276 if (priv_check_cred_vfs_generation(td->td_ucred)) {
277 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
278 prison_enforce_statfs(td->td_ucred, mp, buf);
286 * Get filesystem statistics.
288 #ifndef _SYS_SYSPROTO_H_
295 sys_statfs(struct thread *td, struct statfs_args *uap)
300 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
301 error = kern_statfs(td, uap->path, UIO_USERSPACE, sfp);
303 error = copyout(sfp, uap->buf, sizeof(struct statfs));
309 kern_statfs(struct thread *td, const char *path, enum uio_seg pathseg,
316 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
321 mp = nd.ni_vp->v_mount;
323 NDFREE(&nd, NDF_ONLY_PNBUF);
325 return (kern_do_statfs(td, mp, buf));
329 * Get filesystem statistics.
331 #ifndef _SYS_SYSPROTO_H_
332 struct fstatfs_args {
338 sys_fstatfs(struct thread *td, struct fstatfs_args *uap)
343 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
344 error = kern_fstatfs(td, uap->fd, sfp);
346 error = copyout(sfp, uap->buf, sizeof(struct statfs));
352 kern_fstatfs(struct thread *td, int fd, struct statfs *buf)
360 error = getvnode(td, fd, &cap_fstatfs_rights, &fp);
364 vn_lock(vp, LK_SHARED | LK_RETRY);
366 AUDIT_ARG_VNODE1(vp);
373 return (kern_do_statfs(td, mp, buf));
377 * Get statistics on all filesystems.
379 #ifndef _SYS_SYSPROTO_H_
380 struct getfsstat_args {
387 sys_getfsstat(struct thread *td, struct getfsstat_args *uap)
392 if (uap->bufsize < 0 || uap->bufsize > SIZE_MAX)
394 error = kern_getfsstat(td, &uap->buf, uap->bufsize, &count,
395 UIO_USERSPACE, uap->mode);
397 td->td_retval[0] = count;
402 * If (bufsize > 0 && bufseg == UIO_SYSSPACE)
403 * The caller is responsible for freeing memory which will be allocated
407 kern_getfsstat(struct thread *td, struct statfs **buf, size_t bufsize,
408 size_t *countp, enum uio_seg bufseg, int mode)
410 struct mount *mp, *nmp;
411 struct statfs *sfsp, *sp, *sptmp, *tofree;
412 size_t count, maxcount;
420 if (bufseg == UIO_SYSSPACE)
425 maxcount = bufsize / sizeof(struct statfs);
429 } else if (bufseg == UIO_USERSPACE) {
432 } else /* if (bufseg == UIO_SYSSPACE) */ {
434 mtx_lock(&mountlist_mtx);
435 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
438 mtx_unlock(&mountlist_mtx);
439 if (maxcount > count)
441 tofree = sfsp = *buf = malloc(maxcount * sizeof(struct statfs),
445 mtx_lock(&mountlist_mtx);
446 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
447 if (prison_canseemount(td->td_ucred, mp) != 0) {
448 nmp = TAILQ_NEXT(mp, mnt_list);
452 if (mac_mount_check_stat(td->td_ucred, mp) != 0) {
453 nmp = TAILQ_NEXT(mp, mnt_list);
457 if (mode == MNT_WAIT) {
458 if (vfs_busy(mp, MBF_MNTLSTLOCK) != 0) {
460 * If vfs_busy() failed, and MBF_NOWAIT
461 * wasn't passed, then the mp is gone.
462 * Furthermore, because of MBF_MNTLSTLOCK,
463 * the mountlist_mtx was dropped. We have
464 * no other choice than to start over.
466 mtx_unlock(&mountlist_mtx);
467 free(tofree, M_STATFS);
471 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK) != 0) {
472 nmp = TAILQ_NEXT(mp, mnt_list);
476 if (sfsp != NULL && count < maxcount) {
479 * If MNT_NOWAIT is specified, do not refresh
482 if (mode != MNT_NOWAIT) {
483 error = VFS_STATFS(mp, sp);
485 mtx_lock(&mountlist_mtx);
486 nmp = TAILQ_NEXT(mp, mnt_list);
491 if (priv_check_cred_vfs_generation(td->td_ucred)) {
492 sptmp = malloc(sizeof(struct statfs), M_STATFS,
495 sptmp->f_fsid.val[0] = sptmp->f_fsid.val[1] = 0;
496 prison_enforce_statfs(td->td_ucred, mp, sptmp);
500 if (bufseg == UIO_SYSSPACE) {
501 bcopy(sp, sfsp, sizeof(*sp));
502 free(sptmp, M_STATFS);
503 } else /* if (bufseg == UIO_USERSPACE) */ {
504 error = copyout(sp, sfsp, sizeof(*sp));
505 free(sptmp, M_STATFS);
514 mtx_lock(&mountlist_mtx);
515 nmp = TAILQ_NEXT(mp, mnt_list);
518 mtx_unlock(&mountlist_mtx);
519 if (sfsp != NULL && count > maxcount)
526 #ifdef COMPAT_FREEBSD4
528 * Get old format filesystem statistics.
530 static void freebsd4_cvtstatfs(struct statfs *, struct ostatfs *);
532 #ifndef _SYS_SYSPROTO_H_
533 struct freebsd4_statfs_args {
539 freebsd4_statfs(struct thread *td, struct freebsd4_statfs_args *uap)
545 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
546 error = kern_statfs(td, uap->path, UIO_USERSPACE, sfp);
548 freebsd4_cvtstatfs(sfp, &osb);
549 error = copyout(&osb, uap->buf, sizeof(osb));
556 * Get filesystem statistics.
558 #ifndef _SYS_SYSPROTO_H_
559 struct freebsd4_fstatfs_args {
565 freebsd4_fstatfs(struct thread *td, struct freebsd4_fstatfs_args *uap)
571 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
572 error = kern_fstatfs(td, uap->fd, sfp);
574 freebsd4_cvtstatfs(sfp, &osb);
575 error = copyout(&osb, uap->buf, sizeof(osb));
582 * Get statistics on all filesystems.
584 #ifndef _SYS_SYSPROTO_H_
585 struct freebsd4_getfsstat_args {
592 freebsd4_getfsstat(struct thread *td, struct freebsd4_getfsstat_args *uap)
594 struct statfs *buf, *sp;
599 if (uap->bufsize < 0)
601 count = uap->bufsize / sizeof(struct ostatfs);
602 if (count > SIZE_MAX / sizeof(struct statfs))
604 size = count * sizeof(struct statfs);
605 error = kern_getfsstat(td, &buf, size, &count, UIO_SYSSPACE,
608 td->td_retval[0] = count;
611 while (count != 0 && error == 0) {
612 freebsd4_cvtstatfs(sp, &osb);
613 error = copyout(&osb, uap->buf, sizeof(osb));
624 * Implement fstatfs() for (NFS) file handles.
626 #ifndef _SYS_SYSPROTO_H_
627 struct freebsd4_fhstatfs_args {
628 struct fhandle *u_fhp;
633 freebsd4_fhstatfs(struct thread *td, struct freebsd4_fhstatfs_args *uap)
640 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
643 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
644 error = kern_fhstatfs(td, fh, sfp);
646 freebsd4_cvtstatfs(sfp, &osb);
647 error = copyout(&osb, uap->buf, sizeof(osb));
654 * Convert a new format statfs structure to an old format statfs structure.
657 freebsd4_cvtstatfs(struct statfs *nsp, struct ostatfs *osp)
660 statfs_scale_blocks(nsp, LONG_MAX);
661 bzero(osp, sizeof(*osp));
662 osp->f_bsize = nsp->f_bsize;
663 osp->f_iosize = MIN(nsp->f_iosize, LONG_MAX);
664 osp->f_blocks = nsp->f_blocks;
665 osp->f_bfree = nsp->f_bfree;
666 osp->f_bavail = nsp->f_bavail;
667 osp->f_files = MIN(nsp->f_files, LONG_MAX);
668 osp->f_ffree = MIN(nsp->f_ffree, LONG_MAX);
669 osp->f_owner = nsp->f_owner;
670 osp->f_type = nsp->f_type;
671 osp->f_flags = nsp->f_flags;
672 osp->f_syncwrites = MIN(nsp->f_syncwrites, LONG_MAX);
673 osp->f_asyncwrites = MIN(nsp->f_asyncwrites, LONG_MAX);
674 osp->f_syncreads = MIN(nsp->f_syncreads, LONG_MAX);
675 osp->f_asyncreads = MIN(nsp->f_asyncreads, LONG_MAX);
676 strlcpy(osp->f_fstypename, nsp->f_fstypename,
677 MIN(MFSNAMELEN, OMFSNAMELEN));
678 strlcpy(osp->f_mntonname, nsp->f_mntonname,
679 MIN(MNAMELEN, OMNAMELEN));
680 strlcpy(osp->f_mntfromname, nsp->f_mntfromname,
681 MIN(MNAMELEN, OMNAMELEN));
682 osp->f_fsid = nsp->f_fsid;
684 #endif /* COMPAT_FREEBSD4 */
686 #if defined(COMPAT_FREEBSD11)
688 * Get old format filesystem statistics.
690 static void freebsd11_cvtstatfs(struct statfs *, struct freebsd11_statfs *);
693 freebsd11_statfs(struct thread *td, struct freebsd11_statfs_args *uap)
695 struct freebsd11_statfs osb;
699 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
700 error = kern_statfs(td, uap->path, UIO_USERSPACE, sfp);
702 freebsd11_cvtstatfs(sfp, &osb);
703 error = copyout(&osb, uap->buf, sizeof(osb));
710 * Get filesystem statistics.
713 freebsd11_fstatfs(struct thread *td, struct freebsd11_fstatfs_args *uap)
715 struct freebsd11_statfs osb;
719 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
720 error = kern_fstatfs(td, uap->fd, sfp);
722 freebsd11_cvtstatfs(sfp, &osb);
723 error = copyout(&osb, uap->buf, sizeof(osb));
730 * Get statistics on all filesystems.
733 freebsd11_getfsstat(struct thread *td, struct freebsd11_getfsstat_args *uap)
735 struct freebsd11_statfs osb;
736 struct statfs *buf, *sp;
740 count = uap->bufsize / sizeof(struct ostatfs);
741 size = count * sizeof(struct statfs);
742 error = kern_getfsstat(td, &buf, size, &count, UIO_SYSSPACE,
745 td->td_retval[0] = count;
748 while (count > 0 && error == 0) {
749 freebsd11_cvtstatfs(sp, &osb);
750 error = copyout(&osb, uap->buf, sizeof(osb));
761 * Implement fstatfs() for (NFS) file handles.
764 freebsd11_fhstatfs(struct thread *td, struct freebsd11_fhstatfs_args *uap)
766 struct freebsd11_statfs osb;
771 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
774 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
775 error = kern_fhstatfs(td, fh, sfp);
777 freebsd11_cvtstatfs(sfp, &osb);
778 error = copyout(&osb, uap->buf, sizeof(osb));
785 * Convert a new format statfs structure to an old format statfs structure.
788 freebsd11_cvtstatfs(struct statfs *nsp, struct freebsd11_statfs *osp)
791 bzero(osp, sizeof(*osp));
792 osp->f_version = FREEBSD11_STATFS_VERSION;
793 osp->f_type = nsp->f_type;
794 osp->f_flags = nsp->f_flags;
795 osp->f_bsize = nsp->f_bsize;
796 osp->f_iosize = nsp->f_iosize;
797 osp->f_blocks = nsp->f_blocks;
798 osp->f_bfree = nsp->f_bfree;
799 osp->f_bavail = nsp->f_bavail;
800 osp->f_files = nsp->f_files;
801 osp->f_ffree = nsp->f_ffree;
802 osp->f_syncwrites = nsp->f_syncwrites;
803 osp->f_asyncwrites = nsp->f_asyncwrites;
804 osp->f_syncreads = nsp->f_syncreads;
805 osp->f_asyncreads = nsp->f_asyncreads;
806 osp->f_namemax = nsp->f_namemax;
807 osp->f_owner = nsp->f_owner;
808 osp->f_fsid = nsp->f_fsid;
809 strlcpy(osp->f_fstypename, nsp->f_fstypename,
810 MIN(MFSNAMELEN, sizeof(osp->f_fstypename)));
811 strlcpy(osp->f_mntonname, nsp->f_mntonname,
812 MIN(MNAMELEN, sizeof(osp->f_mntonname)));
813 strlcpy(osp->f_mntfromname, nsp->f_mntfromname,
814 MIN(MNAMELEN, sizeof(osp->f_mntfromname)));
816 #endif /* COMPAT_FREEBSD11 */
819 * Change current working directory to a given file descriptor.
821 #ifndef _SYS_SYSPROTO_H_
827 sys_fchdir(struct thread *td, struct fchdir_args *uap)
829 struct vnode *vp, *tdp;
834 AUDIT_ARG_FD(uap->fd);
835 error = getvnode(td, uap->fd, &cap_fchdir_rights,
842 vn_lock(vp, LK_SHARED | LK_RETRY);
843 AUDIT_ARG_VNODE1(vp);
844 error = change_dir(vp, td);
845 while (!error && (mp = vp->v_mountedhere) != NULL) {
848 error = VFS_ROOT(mp, LK_SHARED, &tdp);
865 * Change current working directory (``.'').
867 #ifndef _SYS_SYSPROTO_H_
873 sys_chdir(struct thread *td, struct chdir_args *uap)
876 return (kern_chdir(td, uap->path, UIO_USERSPACE));
880 kern_chdir(struct thread *td, const char *path, enum uio_seg pathseg)
885 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
887 if ((error = namei(&nd)) != 0)
889 if ((error = change_dir(nd.ni_vp, td)) != 0) {
891 NDFREE(&nd, NDF_ONLY_PNBUF);
894 VOP_UNLOCK(nd.ni_vp);
895 NDFREE(&nd, NDF_ONLY_PNBUF);
896 pwd_chdir(td, nd.ni_vp);
901 * Change notion of root (``/'') directory.
903 #ifndef _SYS_SYSPROTO_H_
909 sys_chroot(struct thread *td, struct chroot_args *uap)
914 error = priv_check(td, PRIV_VFS_CHROOT);
917 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
918 UIO_USERSPACE, uap->path, td);
922 error = change_dir(nd.ni_vp, td);
926 error = mac_vnode_check_chroot(td->td_ucred, nd.ni_vp);
930 VOP_UNLOCK(nd.ni_vp);
931 error = pwd_chroot(td, nd.ni_vp);
933 NDFREE(&nd, NDF_ONLY_PNBUF);
938 NDFREE(&nd, NDF_ONLY_PNBUF);
943 * Common routine for chroot and chdir. Callers must provide a locked vnode
947 change_dir(struct vnode *vp, struct thread *td)
953 ASSERT_VOP_LOCKED(vp, "change_dir(): vp not locked");
954 if (vp->v_type != VDIR)
957 error = mac_vnode_check_chdir(td->td_ucred, vp);
961 return (VOP_ACCESS(vp, VEXEC, td->td_ucred, td));
965 flags_to_rights(int flags, cap_rights_t *rightsp)
968 if (flags & O_EXEC) {
969 cap_rights_set_one(rightsp, CAP_FEXECVE);
971 switch ((flags & O_ACCMODE)) {
973 cap_rights_set_one(rightsp, CAP_READ);
976 cap_rights_set_one(rightsp, CAP_READ);
979 cap_rights_set_one(rightsp, CAP_WRITE);
980 if (!(flags & (O_APPEND | O_TRUNC)))
981 cap_rights_set_one(rightsp, CAP_SEEK);
987 cap_rights_set_one(rightsp, CAP_CREATE);
990 cap_rights_set_one(rightsp, CAP_FTRUNCATE);
992 if (flags & (O_SYNC | O_FSYNC))
993 cap_rights_set_one(rightsp, CAP_FSYNC);
995 if (flags & (O_EXLOCK | O_SHLOCK))
996 cap_rights_set_one(rightsp, CAP_FLOCK);
1000 * Check permissions, allocate an open file structure, and call the device
1001 * open routine if any.
1003 #ifndef _SYS_SYSPROTO_H_
1011 sys_open(struct thread *td, struct open_args *uap)
1014 return (kern_openat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1015 uap->flags, uap->mode));
1018 #ifndef _SYS_SYSPROTO_H_
1019 struct openat_args {
1027 sys_openat(struct thread *td, struct openat_args *uap)
1030 AUDIT_ARG_FD(uap->fd);
1031 return (kern_openat(td, uap->fd, uap->path, UIO_USERSPACE, uap->flag,
1036 kern_openat(struct thread *td, int fd, const char *path, enum uio_seg pathseg,
1037 int flags, int mode)
1039 struct proc *p = td->td_proc;
1040 struct filedesc *fdp = p->p_fd;
1043 struct nameidata nd;
1044 cap_rights_t rights;
1045 int cmode, error, indx;
1049 AUDIT_ARG_FFLAGS(flags);
1050 AUDIT_ARG_MODE(mode);
1051 cap_rights_init_one(&rights, CAP_LOOKUP);
1052 flags_to_rights(flags, &rights);
1054 * Only one of the O_EXEC, O_RDONLY, O_WRONLY and O_RDWR flags
1057 if (flags & O_EXEC) {
1058 if (flags & O_ACCMODE)
1060 } else if ((flags & O_ACCMODE) == O_ACCMODE) {
1063 flags = FFLAGS(flags);
1067 * Allocate a file structure. The descriptor to reference it
1068 * is allocated and set by finstall() below.
1070 error = falloc_noinstall(td, &fp);
1074 * An extra reference on `fp' has been held for us by
1075 * falloc_noinstall().
1077 /* Set the flags early so the finit in devfs can pick them up. */
1078 fp->f_flag = flags & FMASK;
1079 cmode = ((mode & ~fdp->fd_cmask) & ALLPERMS) & ~S_ISTXT;
1080 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, fd,
1082 td->td_dupfd = -1; /* XXX check for fdopen */
1083 error = vn_open(&nd, &flags, cmode, fp);
1086 * If the vn_open replaced the method vector, something
1087 * wonderous happened deep below and we just pass it up
1088 * pretending we know what we do.
1090 if (error == ENXIO && fp->f_ops != &badfileops)
1094 * Handle special fdopen() case. bleh.
1096 * Don't do this for relative (capability) lookups; we don't
1097 * understand exactly what would happen, and we don't think
1098 * that it ever should.
1100 if ((nd.ni_lcf & NI_LCF_STRICTRELATIVE) == 0 &&
1101 (error == ENODEV || error == ENXIO) &&
1102 td->td_dupfd >= 0) {
1103 error = dupfdopen(td, fdp, td->td_dupfd, flags, error,
1112 NDFREE(&nd, NDF_ONLY_PNBUF);
1116 * Store the vnode, for any f_type. Typically, the vnode use
1117 * count is decremented by direct call to vn_closefile() for
1118 * files that switched type in the cdevsw fdopen() method.
1122 * If the file wasn't claimed by devfs bind it to the normal
1123 * vnode operations here.
1125 if (fp->f_ops == &badfileops) {
1126 KASSERT(vp->v_type != VFIFO, ("Unexpected fifo."));
1128 finit(fp, (flags & FMASK) | (fp->f_flag & FHASLOCK),
1129 DTYPE_VNODE, vp, &vnops);
1133 if (flags & O_TRUNC) {
1134 error = fo_truncate(fp, 0, td->td_ucred, td);
1140 * If we haven't already installed the FD (for dupfdopen), do so now.
1143 struct filecaps *fcaps;
1146 if ((nd.ni_lcf & NI_LCF_STRICTRELATIVE) != 0)
1147 fcaps = &nd.ni_filecaps;
1151 error = finstall(td, fp, &indx, flags, fcaps);
1152 /* On success finstall() consumes fcaps. */
1154 filecaps_free(&nd.ni_filecaps);
1158 filecaps_free(&nd.ni_filecaps);
1162 * Release our private reference, leaving the one associated with
1163 * the descriptor table intact.
1166 td->td_retval[0] = indx;
1169 KASSERT(indx == -1, ("indx=%d, should be -1", indx));
1178 #ifndef _SYS_SYSPROTO_H_
1179 struct ocreat_args {
1185 ocreat(struct thread *td, struct ocreat_args *uap)
1188 return (kern_openat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1189 O_WRONLY | O_CREAT | O_TRUNC, uap->mode));
1191 #endif /* COMPAT_43 */
1194 * Create a special file.
1196 #ifndef _SYS_SYSPROTO_H_
1197 struct mknodat_args {
1205 sys_mknodat(struct thread *td, struct mknodat_args *uap)
1208 return (kern_mknodat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode,
1212 #if defined(COMPAT_FREEBSD11)
1214 freebsd11_mknod(struct thread *td,
1215 struct freebsd11_mknod_args *uap)
1218 return (kern_mknodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1219 uap->mode, uap->dev));
1223 freebsd11_mknodat(struct thread *td,
1224 struct freebsd11_mknodat_args *uap)
1227 return (kern_mknodat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode,
1230 #endif /* COMPAT_FREEBSD11 */
1233 kern_mknodat(struct thread *td, int fd, const char *path, enum uio_seg pathseg,
1234 int mode, dev_t dev)
1239 struct nameidata nd;
1240 int error, whiteout = 0;
1242 AUDIT_ARG_MODE(mode);
1244 switch (mode & S_IFMT) {
1247 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
1248 if (error == 0 && dev == VNOVAL)
1252 error = priv_check(td, PRIV_VFS_MKNOD_WHT);
1256 return (kern_mkfifoat(td, fd, path, pathseg, mode));
1266 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
1267 NOCACHE, pathseg, path, fd, &cap_mknodat_rights,
1269 if ((error = namei(&nd)) != 0)
1273 NDFREE(&nd, NDF_ONLY_PNBUF);
1274 if (vp == nd.ni_dvp)
1282 vattr.va_mode = (mode & ALLPERMS) &
1283 ~td->td_proc->p_fd->fd_cmask;
1284 vattr.va_rdev = dev;
1287 switch (mode & S_IFMT) {
1289 vattr.va_type = VCHR;
1292 vattr.va_type = VBLK;
1298 panic("kern_mknod: invalid mode");
1301 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1302 NDFREE(&nd, NDF_ONLY_PNBUF);
1304 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1309 if (error == 0 && !whiteout)
1310 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp,
1311 &nd.ni_cnd, &vattr);
1315 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, CREATE);
1317 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp,
1318 &nd.ni_cnd, &vattr);
1323 NDFREE(&nd, NDF_ONLY_PNBUF);
1325 vn_finished_write(mp);
1330 * Create a named pipe.
1332 #ifndef _SYS_SYSPROTO_H_
1333 struct mkfifo_args {
1339 sys_mkfifo(struct thread *td, struct mkfifo_args *uap)
1342 return (kern_mkfifoat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1346 #ifndef _SYS_SYSPROTO_H_
1347 struct mkfifoat_args {
1354 sys_mkfifoat(struct thread *td, struct mkfifoat_args *uap)
1357 return (kern_mkfifoat(td, uap->fd, uap->path, UIO_USERSPACE,
1362 kern_mkfifoat(struct thread *td, int fd, const char *path,
1363 enum uio_seg pathseg, int mode)
1367 struct nameidata nd;
1370 AUDIT_ARG_MODE(mode);
1373 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
1374 NOCACHE, pathseg, path, fd, &cap_mkfifoat_rights,
1376 if ((error = namei(&nd)) != 0)
1378 if (nd.ni_vp != NULL) {
1379 NDFREE(&nd, NDF_ONLY_PNBUF);
1380 if (nd.ni_vp == nd.ni_dvp)
1387 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1388 NDFREE(&nd, NDF_ONLY_PNBUF);
1390 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1395 vattr.va_type = VFIFO;
1396 vattr.va_mode = (mode & ALLPERMS) & ~td->td_proc->p_fd->fd_cmask;
1398 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1403 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
1410 vn_finished_write(mp);
1411 NDFREE(&nd, NDF_ONLY_PNBUF);
1416 * Make a hard file link.
1418 #ifndef _SYS_SYSPROTO_H_
1425 sys_link(struct thread *td, struct link_args *uap)
1428 return (kern_linkat(td, AT_FDCWD, AT_FDCWD, uap->path, uap->link,
1429 UIO_USERSPACE, FOLLOW));
1432 #ifndef _SYS_SYSPROTO_H_
1433 struct linkat_args {
1442 sys_linkat(struct thread *td, struct linkat_args *uap)
1447 if ((flag & ~(AT_SYMLINK_FOLLOW | AT_BENEATH)) != 0)
1450 return (kern_linkat(td, uap->fd1, uap->fd2, uap->path1, uap->path2,
1451 UIO_USERSPACE, ((flag & AT_SYMLINK_FOLLOW) != 0 ? FOLLOW :
1452 NOFOLLOW) | ((flag & AT_BENEATH) != 0 ? BENEATH : 0)));
1455 int hardlink_check_uid = 0;
1456 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
1457 &hardlink_check_uid, 0,
1458 "Unprivileged processes cannot create hard links to files owned by other "
1460 static int hardlink_check_gid = 0;
1461 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
1462 &hardlink_check_gid, 0,
1463 "Unprivileged processes cannot create hard links to files owned by other "
1467 can_hardlink(struct vnode *vp, struct ucred *cred)
1472 if (!hardlink_check_uid && !hardlink_check_gid)
1475 error = VOP_GETATTR(vp, &va, cred);
1479 if (hardlink_check_uid && cred->cr_uid != va.va_uid) {
1480 error = priv_check_cred(cred, PRIV_VFS_LINK);
1485 if (hardlink_check_gid && !groupmember(va.va_gid, cred)) {
1486 error = priv_check_cred(cred, PRIV_VFS_LINK);
1495 kern_linkat(struct thread *td, int fd1, int fd2, const char *path1,
1496 const char *path2, enum uio_seg segflag, int follow)
1498 struct nameidata nd;
1503 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, segflag,
1504 path1, fd1, &cap_linkat_source_rights, td);
1505 if ((error = namei(&nd)) != 0)
1507 NDFREE(&nd, NDF_ONLY_PNBUF);
1508 error = kern_linkat_vp(td, nd.ni_vp, fd2, path2, segflag);
1509 } while (error == EAGAIN);
1514 kern_linkat_vp(struct thread *td, struct vnode *vp, int fd, const char *path,
1515 enum uio_seg segflag)
1517 struct nameidata nd;
1521 if (vp->v_type == VDIR) {
1523 return (EPERM); /* POSIX */
1525 NDINIT_ATRIGHTS(&nd, CREATE,
1526 LOCKPARENT | SAVENAME | AUDITVNODE2 | NOCACHE, segflag, path, fd,
1527 &cap_linkat_target_rights, td);
1528 if ((error = namei(&nd)) == 0) {
1529 if (nd.ni_vp != NULL) {
1530 NDFREE(&nd, NDF_ONLY_PNBUF);
1531 if (nd.ni_dvp == nd.ni_vp)
1538 } else if (nd.ni_dvp->v_mount != vp->v_mount) {
1540 * Cross-device link. No need to recheck
1541 * vp->v_type, since it cannot change, except
1544 NDFREE(&nd, NDF_ONLY_PNBUF);
1548 } else if ((error = vn_lock(vp, LK_EXCLUSIVE)) == 0) {
1549 error = can_hardlink(vp, td->td_ucred);
1552 error = mac_vnode_check_link(td->td_ucred,
1553 nd.ni_dvp, vp, &nd.ni_cnd);
1558 NDFREE(&nd, NDF_ONLY_PNBUF);
1561 error = vn_start_write(vp, &mp, V_NOWAIT);
1565 NDFREE(&nd, NDF_ONLY_PNBUF);
1566 error = vn_start_write(NULL, &mp,
1572 error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
1575 vn_finished_write(mp);
1576 NDFREE(&nd, NDF_ONLY_PNBUF);
1579 NDFREE(&nd, NDF_ONLY_PNBUF);
1589 * Make a symbolic link.
1591 #ifndef _SYS_SYSPROTO_H_
1592 struct symlink_args {
1598 sys_symlink(struct thread *td, struct symlink_args *uap)
1601 return (kern_symlinkat(td, uap->path, AT_FDCWD, uap->link,
1605 #ifndef _SYS_SYSPROTO_H_
1606 struct symlinkat_args {
1613 sys_symlinkat(struct thread *td, struct symlinkat_args *uap)
1616 return (kern_symlinkat(td, uap->path1, uap->fd, uap->path2,
1621 kern_symlinkat(struct thread *td, const char *path1, int fd, const char *path2,
1622 enum uio_seg segflg)
1626 const char *syspath;
1628 struct nameidata nd;
1631 if (segflg == UIO_SYSSPACE) {
1634 tmppath = uma_zalloc(namei_zone, M_WAITOK);
1635 if ((error = copyinstr(path1, tmppath, MAXPATHLEN, NULL)) != 0)
1639 AUDIT_ARG_TEXT(syspath);
1642 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
1643 NOCACHE, segflg, path2, fd, &cap_symlinkat_rights,
1645 if ((error = namei(&nd)) != 0)
1648 NDFREE(&nd, NDF_ONLY_PNBUF);
1649 if (nd.ni_vp == nd.ni_dvp)
1657 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1658 NDFREE(&nd, NDF_ONLY_PNBUF);
1660 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1665 vattr.va_mode = ACCESSPERMS &~ td->td_proc->p_fd->fd_cmask;
1667 vattr.va_type = VLNK;
1668 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1673 error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr, syspath);
1679 NDFREE(&nd, NDF_ONLY_PNBUF);
1681 vn_finished_write(mp);
1683 if (segflg != UIO_SYSSPACE)
1684 uma_zfree(namei_zone, tmppath);
1689 * Delete a whiteout from the filesystem.
1691 #ifndef _SYS_SYSPROTO_H_
1692 struct undelete_args {
1697 sys_undelete(struct thread *td, struct undelete_args *uap)
1700 struct nameidata nd;
1705 NDINIT(&nd, DELETE, LOCKPARENT | DOWHITEOUT | AUDITVNODE1,
1706 UIO_USERSPACE, uap->path, td);
1711 if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & ISWHITEOUT)) {
1712 NDFREE(&nd, NDF_ONLY_PNBUF);
1713 if (nd.ni_vp == nd.ni_dvp)
1721 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1722 NDFREE(&nd, NDF_ONLY_PNBUF);
1724 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1728 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, DELETE);
1729 NDFREE(&nd, NDF_ONLY_PNBUF);
1731 vn_finished_write(mp);
1736 * Delete a name from the filesystem.
1738 #ifndef _SYS_SYSPROTO_H_
1739 struct unlink_args {
1744 sys_unlink(struct thread *td, struct unlink_args *uap)
1747 return (kern_funlinkat(td, AT_FDCWD, uap->path, FD_NONE, UIO_USERSPACE,
1752 kern_funlinkat_ex(struct thread *td, int dfd, const char *path, int fd,
1753 int flag, enum uio_seg pathseg, ino_t oldinum)
1756 if ((flag & ~AT_REMOVEDIR) != 0)
1759 if ((flag & AT_REMOVEDIR) != 0)
1760 return (kern_frmdirat(td, dfd, path, fd, UIO_USERSPACE, 0));
1762 return (kern_funlinkat(td, dfd, path, fd, UIO_USERSPACE, 0, 0));
1765 #ifndef _SYS_SYSPROTO_H_
1766 struct unlinkat_args {
1773 sys_unlinkat(struct thread *td, struct unlinkat_args *uap)
1776 return (kern_funlinkat_ex(td, uap->fd, uap->path, FD_NONE, uap->flag,
1780 #ifndef _SYS_SYSPROTO_H_
1781 struct funlinkat_args {
1789 sys_funlinkat(struct thread *td, struct funlinkat_args *uap)
1792 return (kern_funlinkat_ex(td, uap->dfd, uap->path, uap->fd, uap->flag,
1797 kern_funlinkat(struct thread *td, int dfd, const char *path, int fd,
1798 enum uio_seg pathseg, int flag, ino_t oldinum)
1803 struct nameidata nd;
1808 if (fd != FD_NONE) {
1809 error = getvnode(td, fd, &cap_no_rights, &fp);
1816 NDINIT_ATRIGHTS(&nd, DELETE, LOCKPARENT | LOCKLEAF | AUDITVNODE1 |
1817 ((flag & AT_BENEATH) != 0 ? BENEATH : 0),
1818 pathseg, path, dfd, &cap_unlinkat_rights, td);
1819 if ((error = namei(&nd)) != 0) {
1820 if (error == EINVAL)
1825 if (vp->v_type == VDIR && oldinum == 0) {
1826 error = EPERM; /* POSIX */
1827 } else if (oldinum != 0 &&
1828 ((error = vn_stat(vp, &sb, td->td_ucred, NOCRED, td)) == 0) &&
1829 sb.st_ino != oldinum) {
1830 error = EIDRM; /* Identifier removed */
1831 } else if (fp != NULL && fp->f_vnode != vp) {
1832 if (VN_IS_DOOMED(fp->f_vnode))
1838 * The root of a mounted filesystem cannot be deleted.
1840 * XXX: can this only be a VDIR case?
1842 if (vp->v_vflag & VV_ROOT)
1846 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1847 NDFREE(&nd, NDF_ONLY_PNBUF);
1849 if (vp == nd.ni_dvp)
1853 if ((error = vn_start_write(NULL, &mp,
1854 V_XSLEEP | PCATCH)) != 0) {
1860 error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp,
1865 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_UNLINK);
1866 error = VOP_REMOVE(nd.ni_dvp, vp, &nd.ni_cnd);
1870 vn_finished_write(mp);
1872 NDFREE(&nd, NDF_ONLY_PNBUF);
1874 if (vp == nd.ni_dvp)
1885 * Reposition read/write file offset.
1887 #ifndef _SYS_SYSPROTO_H_
1896 sys_lseek(struct thread *td, struct lseek_args *uap)
1899 return (kern_lseek(td, uap->fd, uap->offset, uap->whence));
1903 kern_lseek(struct thread *td, int fd, off_t offset, int whence)
1909 error = fget(td, fd, &cap_seek_rights, &fp);
1912 error = (fp->f_ops->fo_flags & DFLAG_SEEKABLE) != 0 ?
1913 fo_seek(fp, offset, whence, td) : ESPIPE;
1918 #if defined(COMPAT_43)
1920 * Reposition read/write file offset.
1922 #ifndef _SYS_SYSPROTO_H_
1923 struct olseek_args {
1930 olseek(struct thread *td, struct olseek_args *uap)
1933 return (kern_lseek(td, uap->fd, uap->offset, uap->whence));
1935 #endif /* COMPAT_43 */
1937 #if defined(COMPAT_FREEBSD6)
1938 /* Version with the 'pad' argument */
1940 freebsd6_lseek(struct thread *td, struct freebsd6_lseek_args *uap)
1943 return (kern_lseek(td, uap->fd, uap->offset, uap->whence));
1948 * Check access permissions using passed credentials.
1951 vn_access(struct vnode *vp, int user_flags, struct ucred *cred,
1957 /* Flags == 0 means only check for existence. */
1958 if (user_flags == 0)
1962 if (user_flags & R_OK)
1964 if (user_flags & W_OK)
1966 if (user_flags & X_OK)
1969 error = mac_vnode_check_access(cred, vp, accmode);
1973 if ((accmode & VWRITE) == 0 || (error = vn_writechk(vp)) == 0)
1974 error = VOP_ACCESS(vp, accmode, cred, td);
1979 * Check access permissions using "real" credentials.
1981 #ifndef _SYS_SYSPROTO_H_
1982 struct access_args {
1988 sys_access(struct thread *td, struct access_args *uap)
1991 return (kern_accessat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1995 #ifndef _SYS_SYSPROTO_H_
1996 struct faccessat_args {
2004 sys_faccessat(struct thread *td, struct faccessat_args *uap)
2007 return (kern_accessat(td, uap->fd, uap->path, UIO_USERSPACE, uap->flag,
2012 kern_accessat(struct thread *td, int fd, const char *path,
2013 enum uio_seg pathseg, int flag, int amode)
2015 struct ucred *cred, *usecred;
2017 struct nameidata nd;
2020 if ((flag & ~(AT_EACCESS | AT_BENEATH)) != 0)
2022 if (amode != F_OK && (amode & ~(R_OK | W_OK | X_OK)) != 0)
2026 * Create and modify a temporary credential instead of one that
2027 * is potentially shared (if we need one).
2029 cred = td->td_ucred;
2030 if ((flag & AT_EACCESS) == 0 &&
2031 ((cred->cr_uid != cred->cr_ruid ||
2032 cred->cr_rgid != cred->cr_groups[0]))) {
2033 usecred = crdup(cred);
2034 usecred->cr_uid = cred->cr_ruid;
2035 usecred->cr_groups[0] = cred->cr_rgid;
2036 td->td_ucred = usecred;
2039 AUDIT_ARG_VALUE(amode);
2040 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF |
2041 AUDITVNODE1 | ((flag & AT_BENEATH) != 0 ? BENEATH : 0),
2042 pathseg, path, fd, &cap_fstat_rights, td);
2043 if ((error = namei(&nd)) != 0)
2047 error = vn_access(vp, amode, usecred, td);
2048 NDFREE(&nd, NDF_ONLY_PNBUF);
2051 if (usecred != cred) {
2052 td->td_ucred = cred;
2059 * Check access permissions using "effective" credentials.
2061 #ifndef _SYS_SYSPROTO_H_
2062 struct eaccess_args {
2068 sys_eaccess(struct thread *td, struct eaccess_args *uap)
2071 return (kern_accessat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2072 AT_EACCESS, uap->amode));
2075 #if defined(COMPAT_43)
2077 * Get file status; this version follows links.
2079 #ifndef _SYS_SYSPROTO_H_
2086 ostat(struct thread *td, struct ostat_args *uap)
2092 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
2097 return (copyout(&osb, uap->ub, sizeof (osb)));
2101 * Get file status; this version does not follow links.
2103 #ifndef _SYS_SYSPROTO_H_
2104 struct olstat_args {
2110 olstat(struct thread *td, struct olstat_args *uap)
2116 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2117 UIO_USERSPACE, &sb, NULL);
2121 return (copyout(&osb, uap->ub, sizeof (osb)));
2125 * Convert from an old to a new stat structure.
2126 * XXX: many values are blindly truncated.
2129 cvtstat(struct stat *st, struct ostat *ost)
2132 bzero(ost, sizeof(*ost));
2133 ost->st_dev = st->st_dev;
2134 ost->st_ino = st->st_ino;
2135 ost->st_mode = st->st_mode;
2136 ost->st_nlink = st->st_nlink;
2137 ost->st_uid = st->st_uid;
2138 ost->st_gid = st->st_gid;
2139 ost->st_rdev = st->st_rdev;
2140 ost->st_size = MIN(st->st_size, INT32_MAX);
2141 ost->st_atim = st->st_atim;
2142 ost->st_mtim = st->st_mtim;
2143 ost->st_ctim = st->st_ctim;
2144 ost->st_blksize = st->st_blksize;
2145 ost->st_blocks = st->st_blocks;
2146 ost->st_flags = st->st_flags;
2147 ost->st_gen = st->st_gen;
2149 #endif /* COMPAT_43 */
2151 #if defined(COMPAT_43) || defined(COMPAT_FREEBSD11)
2152 int ino64_trunc_error;
2153 SYSCTL_INT(_vfs, OID_AUTO, ino64_trunc_error, CTLFLAG_RW,
2154 &ino64_trunc_error, 0,
2155 "Error on truncation of device, file or inode number, or link count");
2158 freebsd11_cvtstat(struct stat *st, struct freebsd11_stat *ost)
2161 ost->st_dev = st->st_dev;
2162 if (ost->st_dev != st->st_dev) {
2163 switch (ino64_trunc_error) {
2166 * Since dev_t is almost raw, don't clamp to the
2167 * maximum for case 2, but ignore the error.
2174 ost->st_ino = st->st_ino;
2175 if (ost->st_ino != st->st_ino) {
2176 switch (ino64_trunc_error) {
2183 ost->st_ino = UINT32_MAX;
2187 ost->st_mode = st->st_mode;
2188 ost->st_nlink = st->st_nlink;
2189 if (ost->st_nlink != st->st_nlink) {
2190 switch (ino64_trunc_error) {
2197 ost->st_nlink = UINT16_MAX;
2201 ost->st_uid = st->st_uid;
2202 ost->st_gid = st->st_gid;
2203 ost->st_rdev = st->st_rdev;
2204 if (ost->st_rdev != st->st_rdev) {
2205 switch (ino64_trunc_error) {
2212 ost->st_atim = st->st_atim;
2213 ost->st_mtim = st->st_mtim;
2214 ost->st_ctim = st->st_ctim;
2215 ost->st_size = st->st_size;
2216 ost->st_blocks = st->st_blocks;
2217 ost->st_blksize = st->st_blksize;
2218 ost->st_flags = st->st_flags;
2219 ost->st_gen = st->st_gen;
2221 ost->st_birthtim = st->st_birthtim;
2222 bzero((char *)&ost->st_birthtim + sizeof(ost->st_birthtim),
2223 sizeof(*ost) - offsetof(struct freebsd11_stat,
2224 st_birthtim) - sizeof(ost->st_birthtim));
2229 freebsd11_stat(struct thread *td, struct freebsd11_stat_args* uap)
2232 struct freebsd11_stat osb;
2235 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
2239 error = freebsd11_cvtstat(&sb, &osb);
2241 error = copyout(&osb, uap->ub, sizeof(osb));
2246 freebsd11_lstat(struct thread *td, struct freebsd11_lstat_args* uap)
2249 struct freebsd11_stat osb;
2252 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2253 UIO_USERSPACE, &sb, NULL);
2256 error = freebsd11_cvtstat(&sb, &osb);
2258 error = copyout(&osb, uap->ub, sizeof(osb));
2263 freebsd11_fhstat(struct thread *td, struct freebsd11_fhstat_args* uap)
2267 struct freebsd11_stat osb;
2270 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
2273 error = kern_fhstat(td, fh, &sb);
2276 error = freebsd11_cvtstat(&sb, &osb);
2278 error = copyout(&osb, uap->sb, sizeof(osb));
2283 freebsd11_fstatat(struct thread *td, struct freebsd11_fstatat_args* uap)
2286 struct freebsd11_stat osb;
2289 error = kern_statat(td, uap->flag, uap->fd, uap->path,
2290 UIO_USERSPACE, &sb, NULL);
2293 error = freebsd11_cvtstat(&sb, &osb);
2295 error = copyout(&osb, uap->buf, sizeof(osb));
2298 #endif /* COMPAT_FREEBSD11 */
2303 #ifndef _SYS_SYSPROTO_H_
2304 struct fstatat_args {
2312 sys_fstatat(struct thread *td, struct fstatat_args *uap)
2317 error = kern_statat(td, uap->flag, uap->fd, uap->path,
2318 UIO_USERSPACE, &sb, NULL);
2320 error = copyout(&sb, uap->buf, sizeof (sb));
2325 kern_statat(struct thread *td, int flag, int fd, const char *path,
2326 enum uio_seg pathseg, struct stat *sbp,
2327 void (*hook)(struct vnode *vp, struct stat *sbp))
2329 struct nameidata nd;
2332 if ((flag & ~(AT_SYMLINK_NOFOLLOW | AT_BENEATH)) != 0)
2335 NDINIT_ATRIGHTS(&nd, LOOKUP, ((flag & AT_SYMLINK_NOFOLLOW) != 0 ?
2336 NOFOLLOW : FOLLOW) | ((flag & AT_BENEATH) != 0 ? BENEATH : 0) |
2337 LOCKSHARED | LOCKLEAF | AUDITVNODE1, pathseg, path, fd,
2338 &cap_fstat_rights, td);
2340 if ((error = namei(&nd)) != 0)
2342 error = vn_stat(nd.ni_vp, sbp, td->td_ucred, NOCRED, td);
2344 SDT_PROBE2(vfs, , stat, mode, path, sbp->st_mode);
2345 if (S_ISREG(sbp->st_mode))
2346 SDT_PROBE2(vfs, , stat, reg, path, pathseg);
2347 if (__predict_false(hook != NULL))
2348 hook(nd.ni_vp, sbp);
2350 NDFREE(&nd, NDF_ONLY_PNBUF);
2352 #ifdef __STAT_TIME_T_EXT
2353 sbp->st_atim_ext = 0;
2354 sbp->st_mtim_ext = 0;
2355 sbp->st_ctim_ext = 0;
2356 sbp->st_btim_ext = 0;
2359 if (KTRPOINT(td, KTR_STRUCT))
2360 ktrstat_error(sbp, error);
2365 #if defined(COMPAT_FREEBSD11)
2367 * Implementation of the NetBSD [l]stat() functions.
2370 freebsd11_cvtnstat(struct stat *sb, struct nstat *nsb)
2373 bzero(nsb, sizeof(*nsb));
2374 nsb->st_dev = sb->st_dev;
2375 nsb->st_ino = sb->st_ino;
2376 nsb->st_mode = sb->st_mode;
2377 nsb->st_nlink = sb->st_nlink;
2378 nsb->st_uid = sb->st_uid;
2379 nsb->st_gid = sb->st_gid;
2380 nsb->st_rdev = sb->st_rdev;
2381 nsb->st_atim = sb->st_atim;
2382 nsb->st_mtim = sb->st_mtim;
2383 nsb->st_ctim = sb->st_ctim;
2384 nsb->st_size = sb->st_size;
2385 nsb->st_blocks = sb->st_blocks;
2386 nsb->st_blksize = sb->st_blksize;
2387 nsb->st_flags = sb->st_flags;
2388 nsb->st_gen = sb->st_gen;
2389 nsb->st_birthtim = sb->st_birthtim;
2392 #ifndef _SYS_SYSPROTO_H_
2393 struct freebsd11_nstat_args {
2399 freebsd11_nstat(struct thread *td, struct freebsd11_nstat_args *uap)
2405 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
2409 freebsd11_cvtnstat(&sb, &nsb);
2410 return (copyout(&nsb, uap->ub, sizeof (nsb)));
2414 * NetBSD lstat. Get file status; this version does not follow links.
2416 #ifndef _SYS_SYSPROTO_H_
2417 struct freebsd11_nlstat_args {
2423 freebsd11_nlstat(struct thread *td, struct freebsd11_nlstat_args *uap)
2429 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2430 UIO_USERSPACE, &sb, NULL);
2433 freebsd11_cvtnstat(&sb, &nsb);
2434 return (copyout(&nsb, uap->ub, sizeof (nsb)));
2436 #endif /* COMPAT_FREEBSD11 */
2439 * Get configurable pathname variables.
2441 #ifndef _SYS_SYSPROTO_H_
2442 struct pathconf_args {
2448 sys_pathconf(struct thread *td, struct pathconf_args *uap)
2453 error = kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name, FOLLOW,
2456 td->td_retval[0] = value;
2460 #ifndef _SYS_SYSPROTO_H_
2461 struct lpathconf_args {
2467 sys_lpathconf(struct thread *td, struct lpathconf_args *uap)
2472 error = kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name,
2475 td->td_retval[0] = value;
2480 kern_pathconf(struct thread *td, const char *path, enum uio_seg pathseg,
2481 int name, u_long flags, long *valuep)
2483 struct nameidata nd;
2486 NDINIT(&nd, LOOKUP, LOCKSHARED | LOCKLEAF | AUDITVNODE1 | flags,
2488 if ((error = namei(&nd)) != 0)
2490 NDFREE(&nd, NDF_ONLY_PNBUF);
2492 error = VOP_PATHCONF(nd.ni_vp, name, valuep);
2498 * Return target name of a symbolic link.
2500 #ifndef _SYS_SYSPROTO_H_
2501 struct readlink_args {
2508 sys_readlink(struct thread *td, struct readlink_args *uap)
2511 return (kern_readlinkat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2512 uap->buf, UIO_USERSPACE, uap->count));
2514 #ifndef _SYS_SYSPROTO_H_
2515 struct readlinkat_args {
2523 sys_readlinkat(struct thread *td, struct readlinkat_args *uap)
2526 return (kern_readlinkat(td, uap->fd, uap->path, UIO_USERSPACE,
2527 uap->buf, UIO_USERSPACE, uap->bufsize));
2531 kern_readlinkat(struct thread *td, int fd, const char *path,
2532 enum uio_seg pathseg, char *buf, enum uio_seg bufseg, size_t count)
2535 struct nameidata nd;
2538 if (count > IOSIZE_MAX)
2541 NDINIT_AT(&nd, LOOKUP, NOFOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
2542 pathseg, path, fd, td);
2544 if ((error = namei(&nd)) != 0)
2546 NDFREE(&nd, NDF_ONLY_PNBUF);
2549 error = kern_readlink_vp(vp, buf, bufseg, count, td);
2556 * Helper function to readlink from a vnode
2559 kern_readlink_vp(struct vnode *vp, char *buf, enum uio_seg bufseg, size_t count,
2566 ASSERT_VOP_LOCKED(vp, "kern_readlink_vp(): vp not locked");
2568 error = mac_vnode_check_readlink(td->td_ucred, vp);
2572 if (vp->v_type != VLNK && (vp->v_vflag & VV_READLINK) == 0)
2575 aiov.iov_base = buf;
2576 aiov.iov_len = count;
2577 auio.uio_iov = &aiov;
2578 auio.uio_iovcnt = 1;
2579 auio.uio_offset = 0;
2580 auio.uio_rw = UIO_READ;
2581 auio.uio_segflg = bufseg;
2583 auio.uio_resid = count;
2584 error = VOP_READLINK(vp, &auio, td->td_ucred);
2585 td->td_retval[0] = count - auio.uio_resid;
2590 * Common implementation code for chflags() and fchflags().
2593 setfflags(struct thread *td, struct vnode *vp, u_long flags)
2599 /* We can't support the value matching VNOVAL. */
2600 if (flags == VNOVAL)
2601 return (EOPNOTSUPP);
2604 * Prevent non-root users from setting flags on devices. When
2605 * a device is reused, users can retain ownership of the device
2606 * if they are allowed to set flags and programs assume that
2607 * chown can't fail when done as root.
2609 if (vp->v_type == VCHR || vp->v_type == VBLK) {
2610 error = priv_check(td, PRIV_VFS_CHFLAGS_DEV);
2615 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2618 vattr.va_flags = flags;
2619 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2621 error = mac_vnode_check_setflags(td->td_ucred, vp, vattr.va_flags);
2624 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
2626 vn_finished_write(mp);
2631 * Change flags of a file given a path name.
2633 #ifndef _SYS_SYSPROTO_H_
2634 struct chflags_args {
2640 sys_chflags(struct thread *td, struct chflags_args *uap)
2643 return (kern_chflagsat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2647 #ifndef _SYS_SYSPROTO_H_
2648 struct chflagsat_args {
2656 sys_chflagsat(struct thread *td, struct chflagsat_args *uap)
2659 if ((uap->atflag & ~(AT_SYMLINK_NOFOLLOW | AT_BENEATH)) != 0)
2662 return (kern_chflagsat(td, uap->fd, uap->path, UIO_USERSPACE,
2663 uap->flags, uap->atflag));
2667 * Same as chflags() but doesn't follow symlinks.
2669 #ifndef _SYS_SYSPROTO_H_
2670 struct lchflags_args {
2676 sys_lchflags(struct thread *td, struct lchflags_args *uap)
2679 return (kern_chflagsat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2680 uap->flags, AT_SYMLINK_NOFOLLOW));
2684 kern_chflagsat(struct thread *td, int fd, const char *path,
2685 enum uio_seg pathseg, u_long flags, int atflag)
2687 struct nameidata nd;
2690 AUDIT_ARG_FFLAGS(flags);
2691 follow = (atflag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
2692 follow |= (atflag & AT_BENEATH) != 0 ? BENEATH : 0;
2693 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, pathseg, path, fd,
2694 &cap_fchflags_rights, td);
2695 if ((error = namei(&nd)) != 0)
2697 NDFREE(&nd, NDF_ONLY_PNBUF);
2698 error = setfflags(td, nd.ni_vp, flags);
2704 * Change flags of a file given a file descriptor.
2706 #ifndef _SYS_SYSPROTO_H_
2707 struct fchflags_args {
2713 sys_fchflags(struct thread *td, struct fchflags_args *uap)
2718 AUDIT_ARG_FD(uap->fd);
2719 AUDIT_ARG_FFLAGS(uap->flags);
2720 error = getvnode(td, uap->fd, &cap_fchflags_rights,
2725 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
2726 AUDIT_ARG_VNODE1(fp->f_vnode);
2727 VOP_UNLOCK(fp->f_vnode);
2729 error = setfflags(td, fp->f_vnode, uap->flags);
2735 * Common implementation code for chmod(), lchmod() and fchmod().
2738 setfmode(struct thread *td, struct ucred *cred, struct vnode *vp, int mode)
2744 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2746 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2748 vattr.va_mode = mode & ALLPERMS;
2750 error = mac_vnode_check_setmode(cred, vp, vattr.va_mode);
2753 error = VOP_SETATTR(vp, &vattr, cred);
2755 vn_finished_write(mp);
2760 * Change mode of a file given path name.
2762 #ifndef _SYS_SYSPROTO_H_
2769 sys_chmod(struct thread *td, struct chmod_args *uap)
2772 return (kern_fchmodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2776 #ifndef _SYS_SYSPROTO_H_
2777 struct fchmodat_args {
2785 sys_fchmodat(struct thread *td, struct fchmodat_args *uap)
2788 if ((uap->flag & ~(AT_SYMLINK_NOFOLLOW | AT_BENEATH)) != 0)
2791 return (kern_fchmodat(td, uap->fd, uap->path, UIO_USERSPACE,
2792 uap->mode, uap->flag));
2796 * Change mode of a file given path name (don't follow links.)
2798 #ifndef _SYS_SYSPROTO_H_
2799 struct lchmod_args {
2805 sys_lchmod(struct thread *td, struct lchmod_args *uap)
2808 return (kern_fchmodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2809 uap->mode, AT_SYMLINK_NOFOLLOW));
2813 kern_fchmodat(struct thread *td, int fd, const char *path,
2814 enum uio_seg pathseg, mode_t mode, int flag)
2816 struct nameidata nd;
2819 AUDIT_ARG_MODE(mode);
2820 follow = (flag & AT_SYMLINK_NOFOLLOW) != 0 ? NOFOLLOW : FOLLOW;
2821 follow |= (flag & AT_BENEATH) != 0 ? BENEATH : 0;
2822 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, pathseg, path, fd,
2823 &cap_fchmod_rights, td);
2824 if ((error = namei(&nd)) != 0)
2826 NDFREE(&nd, NDF_ONLY_PNBUF);
2827 error = setfmode(td, td->td_ucred, nd.ni_vp, mode);
2833 * Change mode of a file given a file descriptor.
2835 #ifndef _SYS_SYSPROTO_H_
2836 struct fchmod_args {
2842 sys_fchmod(struct thread *td, struct fchmod_args *uap)
2847 AUDIT_ARG_FD(uap->fd);
2848 AUDIT_ARG_MODE(uap->mode);
2850 error = fget(td, uap->fd, &cap_fchmod_rights, &fp);
2853 error = fo_chmod(fp, uap->mode, td->td_ucred, td);
2859 * Common implementation for chown(), lchown(), and fchown()
2862 setfown(struct thread *td, struct ucred *cred, struct vnode *vp, uid_t uid,
2869 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2871 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2876 error = mac_vnode_check_setowner(cred, vp, vattr.va_uid,
2880 error = VOP_SETATTR(vp, &vattr, cred);
2882 vn_finished_write(mp);
2887 * Set ownership given a path name.
2889 #ifndef _SYS_SYSPROTO_H_
2897 sys_chown(struct thread *td, struct chown_args *uap)
2900 return (kern_fchownat(td, AT_FDCWD, uap->path, UIO_USERSPACE, uap->uid,
2904 #ifndef _SYS_SYSPROTO_H_
2905 struct fchownat_args {
2914 sys_fchownat(struct thread *td, struct fchownat_args *uap)
2917 if ((uap->flag & ~(AT_SYMLINK_NOFOLLOW | AT_BENEATH)) != 0)
2920 return (kern_fchownat(td, uap->fd, uap->path, UIO_USERSPACE, uap->uid,
2921 uap->gid, uap->flag));
2925 kern_fchownat(struct thread *td, int fd, const char *path,
2926 enum uio_seg pathseg, int uid, int gid, int flag)
2928 struct nameidata nd;
2931 AUDIT_ARG_OWNER(uid, gid);
2932 follow = (flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
2933 follow |= (flag & AT_BENEATH) != 0 ? BENEATH : 0;
2934 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, pathseg, path, fd,
2935 &cap_fchown_rights, td);
2937 if ((error = namei(&nd)) != 0)
2939 NDFREE(&nd, NDF_ONLY_PNBUF);
2940 error = setfown(td, td->td_ucred, nd.ni_vp, uid, gid);
2946 * Set ownership given a path name, do not cross symlinks.
2948 #ifndef _SYS_SYSPROTO_H_
2949 struct lchown_args {
2956 sys_lchown(struct thread *td, struct lchown_args *uap)
2959 return (kern_fchownat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2960 uap->uid, uap->gid, AT_SYMLINK_NOFOLLOW));
2964 * Set ownership given a file descriptor.
2966 #ifndef _SYS_SYSPROTO_H_
2967 struct fchown_args {
2974 sys_fchown(struct thread *td, struct fchown_args *uap)
2979 AUDIT_ARG_FD(uap->fd);
2980 AUDIT_ARG_OWNER(uap->uid, uap->gid);
2981 error = fget(td, uap->fd, &cap_fchown_rights, &fp);
2984 error = fo_chown(fp, uap->uid, uap->gid, td->td_ucred, td);
2990 * Common implementation code for utimes(), lutimes(), and futimes().
2993 getutimes(const struct timeval *usrtvp, enum uio_seg tvpseg,
2994 struct timespec *tsp)
2996 struct timeval tv[2];
2997 const struct timeval *tvp;
3000 if (usrtvp == NULL) {
3001 vfs_timestamp(&tsp[0]);
3004 if (tvpseg == UIO_SYSSPACE) {
3007 if ((error = copyin(usrtvp, tv, sizeof(tv))) != 0)
3012 if (tvp[0].tv_usec < 0 || tvp[0].tv_usec >= 1000000 ||
3013 tvp[1].tv_usec < 0 || tvp[1].tv_usec >= 1000000)
3015 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
3016 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
3022 * Common implementation code for futimens(), utimensat().
3024 #define UTIMENS_NULL 0x1
3025 #define UTIMENS_EXIT 0x2
3027 getutimens(const struct timespec *usrtsp, enum uio_seg tspseg,
3028 struct timespec *tsp, int *retflags)
3030 struct timespec tsnow;
3033 vfs_timestamp(&tsnow);
3035 if (usrtsp == NULL) {
3038 *retflags |= UTIMENS_NULL;
3041 if (tspseg == UIO_SYSSPACE) {
3044 } else if ((error = copyin(usrtsp, tsp, sizeof(*tsp) * 2)) != 0)
3046 if (tsp[0].tv_nsec == UTIME_OMIT && tsp[1].tv_nsec == UTIME_OMIT)
3047 *retflags |= UTIMENS_EXIT;
3048 if (tsp[0].tv_nsec == UTIME_NOW && tsp[1].tv_nsec == UTIME_NOW)
3049 *retflags |= UTIMENS_NULL;
3050 if (tsp[0].tv_nsec == UTIME_OMIT)
3051 tsp[0].tv_sec = VNOVAL;
3052 else if (tsp[0].tv_nsec == UTIME_NOW)
3054 else if (tsp[0].tv_nsec < 0 || tsp[0].tv_nsec >= 1000000000L)
3056 if (tsp[1].tv_nsec == UTIME_OMIT)
3057 tsp[1].tv_sec = VNOVAL;
3058 else if (tsp[1].tv_nsec == UTIME_NOW)
3060 else if (tsp[1].tv_nsec < 0 || tsp[1].tv_nsec >= 1000000000L)
3067 * Common implementation code for utimes(), lutimes(), futimes(), futimens(),
3071 setutimes(struct thread *td, struct vnode *vp, const struct timespec *ts,
3072 int numtimes, int nullflag)
3076 int error, setbirthtime;
3078 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
3080 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3082 if (numtimes < 3 && !VOP_GETATTR(vp, &vattr, td->td_ucred) &&
3083 timespeccmp(&ts[1], &vattr.va_birthtime, < ))
3086 vattr.va_atime = ts[0];
3087 vattr.va_mtime = ts[1];
3089 vattr.va_birthtime = ts[1];
3091 vattr.va_birthtime = ts[2];
3093 vattr.va_vaflags |= VA_UTIMES_NULL;
3095 error = mac_vnode_check_setutimes(td->td_ucred, vp, vattr.va_atime,
3099 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3101 vn_finished_write(mp);
3106 * Set the access and modification times of a file.
3108 #ifndef _SYS_SYSPROTO_H_
3109 struct utimes_args {
3111 struct timeval *tptr;
3115 sys_utimes(struct thread *td, struct utimes_args *uap)
3118 return (kern_utimesat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
3119 uap->tptr, UIO_USERSPACE));
3122 #ifndef _SYS_SYSPROTO_H_
3123 struct futimesat_args {
3126 const struct timeval * times;
3130 sys_futimesat(struct thread *td, struct futimesat_args *uap)
3133 return (kern_utimesat(td, uap->fd, uap->path, UIO_USERSPACE,
3134 uap->times, UIO_USERSPACE));
3138 kern_utimesat(struct thread *td, int fd, const char *path,
3139 enum uio_seg pathseg, struct timeval *tptr, enum uio_seg tptrseg)
3141 struct nameidata nd;
3142 struct timespec ts[2];
3145 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3147 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, fd,
3148 &cap_futimes_rights, td);
3150 if ((error = namei(&nd)) != 0)
3152 NDFREE(&nd, NDF_ONLY_PNBUF);
3153 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3159 * Set the access and modification times of a file.
3161 #ifndef _SYS_SYSPROTO_H_
3162 struct lutimes_args {
3164 struct timeval *tptr;
3168 sys_lutimes(struct thread *td, struct lutimes_args *uap)
3171 return (kern_lutimes(td, uap->path, UIO_USERSPACE, uap->tptr,
3176 kern_lutimes(struct thread *td, const char *path, enum uio_seg pathseg,
3177 struct timeval *tptr, enum uio_seg tptrseg)
3179 struct timespec ts[2];
3180 struct nameidata nd;
3183 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3185 NDINIT(&nd, LOOKUP, NOFOLLOW | AUDITVNODE1, pathseg, path, td);
3186 if ((error = namei(&nd)) != 0)
3188 NDFREE(&nd, NDF_ONLY_PNBUF);
3189 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3195 * Set the access and modification times of a file.
3197 #ifndef _SYS_SYSPROTO_H_
3198 struct futimes_args {
3200 struct timeval *tptr;
3204 sys_futimes(struct thread *td, struct futimes_args *uap)
3207 return (kern_futimes(td, uap->fd, uap->tptr, UIO_USERSPACE));
3211 kern_futimes(struct thread *td, int fd, struct timeval *tptr,
3212 enum uio_seg tptrseg)
3214 struct timespec ts[2];
3219 error = getutimes(tptr, tptrseg, ts);
3222 error = getvnode(td, fd, &cap_futimes_rights, &fp);
3226 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
3227 AUDIT_ARG_VNODE1(fp->f_vnode);
3228 VOP_UNLOCK(fp->f_vnode);
3230 error = setutimes(td, fp->f_vnode, ts, 2, tptr == NULL);
3236 sys_futimens(struct thread *td, struct futimens_args *uap)
3239 return (kern_futimens(td, uap->fd, uap->times, UIO_USERSPACE));
3243 kern_futimens(struct thread *td, int fd, struct timespec *tptr,
3244 enum uio_seg tptrseg)
3246 struct timespec ts[2];
3251 error = getutimens(tptr, tptrseg, ts, &flags);
3254 if (flags & UTIMENS_EXIT)
3256 error = getvnode(td, fd, &cap_futimes_rights, &fp);
3260 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
3261 AUDIT_ARG_VNODE1(fp->f_vnode);
3262 VOP_UNLOCK(fp->f_vnode);
3264 error = setutimes(td, fp->f_vnode, ts, 2, flags & UTIMENS_NULL);
3270 sys_utimensat(struct thread *td, struct utimensat_args *uap)
3273 return (kern_utimensat(td, uap->fd, uap->path, UIO_USERSPACE,
3274 uap->times, UIO_USERSPACE, uap->flag));
3278 kern_utimensat(struct thread *td, int fd, const char *path,
3279 enum uio_seg pathseg, struct timespec *tptr, enum uio_seg tptrseg,
3282 struct nameidata nd;
3283 struct timespec ts[2];
3286 if ((flag & ~(AT_SYMLINK_NOFOLLOW | AT_BENEATH)) != 0)
3289 if ((error = getutimens(tptr, tptrseg, ts, &flags)) != 0)
3291 NDINIT_ATRIGHTS(&nd, LOOKUP, ((flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW :
3292 FOLLOW) | ((flag & AT_BENEATH) != 0 ? BENEATH : 0) | AUDITVNODE1,
3293 pathseg, path, fd, &cap_futimes_rights, td);
3294 if ((error = namei(&nd)) != 0)
3297 * We are allowed to call namei() regardless of 2xUTIME_OMIT.
3299 * "If both tv_nsec fields are UTIME_OMIT... EACCESS may be detected."
3300 * "Search permission is denied by a component of the path prefix."
3302 NDFREE(&nd, NDF_ONLY_PNBUF);
3303 if ((flags & UTIMENS_EXIT) == 0)
3304 error = setutimes(td, nd.ni_vp, ts, 2, flags & UTIMENS_NULL);
3310 * Truncate a file given its path name.
3312 #ifndef _SYS_SYSPROTO_H_
3313 struct truncate_args {
3320 sys_truncate(struct thread *td, struct truncate_args *uap)
3323 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3327 kern_truncate(struct thread *td, const char *path, enum uio_seg pathseg,
3334 struct nameidata nd;
3339 NDINIT(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, td);
3340 if ((error = namei(&nd)) != 0)
3343 rl_cookie = vn_rangelock_wlock(vp, 0, OFF_MAX);
3344 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0) {
3345 vn_rangelock_unlock(vp, rl_cookie);
3349 NDFREE(&nd, NDF_ONLY_PNBUF);
3350 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3351 if (vp->v_type == VDIR)
3354 else if ((error = mac_vnode_check_write(td->td_ucred, NOCRED, vp))) {
3357 else if ((error = vn_writechk(vp)) == 0 &&
3358 (error = VOP_ACCESS(vp, VWRITE, td->td_ucred, td)) == 0) {
3360 vattr.va_size = length;
3361 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3364 vn_finished_write(mp);
3365 vn_rangelock_unlock(vp, rl_cookie);
3370 #if defined(COMPAT_43)
3372 * Truncate a file given its path name.
3374 #ifndef _SYS_SYSPROTO_H_
3375 struct otruncate_args {
3381 otruncate(struct thread *td, struct otruncate_args *uap)
3384 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3386 #endif /* COMPAT_43 */
3388 #if defined(COMPAT_FREEBSD6)
3389 /* Versions with the pad argument */
3391 freebsd6_truncate(struct thread *td, struct freebsd6_truncate_args *uap)
3394 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3398 freebsd6_ftruncate(struct thread *td, struct freebsd6_ftruncate_args *uap)
3401 return (kern_ftruncate(td, uap->fd, uap->length));
3406 kern_fsync(struct thread *td, int fd, bool fullsync)
3411 int error, lock_flags;
3414 error = getvnode(td, fd, &cap_fsync_rights, &fp);
3420 /* XXXKIB: compete outstanding aio writes */;
3422 error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
3425 if (MNT_SHARED_WRITES(mp) ||
3426 ((mp == NULL) && MNT_SHARED_WRITES(vp->v_mount))) {
3427 lock_flags = LK_SHARED;
3429 lock_flags = LK_EXCLUSIVE;
3431 vn_lock(vp, lock_flags | LK_RETRY);
3432 AUDIT_ARG_VNODE1(vp);
3433 if (vp->v_object != NULL) {
3434 VM_OBJECT_WLOCK(vp->v_object);
3435 vm_object_page_clean(vp->v_object, 0, 0, 0);
3436 VM_OBJECT_WUNLOCK(vp->v_object);
3438 error = fullsync ? VOP_FSYNC(vp, MNT_WAIT, td) : VOP_FDATASYNC(vp, td);
3440 vn_finished_write(mp);
3447 * Sync an open file.
3449 #ifndef _SYS_SYSPROTO_H_
3455 sys_fsync(struct thread *td, struct fsync_args *uap)
3458 return (kern_fsync(td, uap->fd, true));
3462 sys_fdatasync(struct thread *td, struct fdatasync_args *uap)
3465 return (kern_fsync(td, uap->fd, false));
3469 * Rename files. Source and destination must either both be directories, or
3470 * both not be directories. If target is a directory, it must be empty.
3472 #ifndef _SYS_SYSPROTO_H_
3473 struct rename_args {
3479 sys_rename(struct thread *td, struct rename_args *uap)
3482 return (kern_renameat(td, AT_FDCWD, uap->from, AT_FDCWD,
3483 uap->to, UIO_USERSPACE));
3486 #ifndef _SYS_SYSPROTO_H_
3487 struct renameat_args {
3495 sys_renameat(struct thread *td, struct renameat_args *uap)
3498 return (kern_renameat(td, uap->oldfd, uap->old, uap->newfd, uap->new,
3503 kern_renameat(struct thread *td, int oldfd, const char *old, int newfd,
3504 const char *new, enum uio_seg pathseg)
3506 struct mount *mp = NULL;
3507 struct vnode *tvp, *fvp, *tdvp;
3508 struct nameidata fromnd, tond;
3514 NDINIT_ATRIGHTS(&fromnd, DELETE, LOCKPARENT | LOCKLEAF | SAVESTART |
3515 AUDITVNODE1, pathseg, old, oldfd,
3516 &cap_renameat_source_rights, td);
3518 NDINIT_ATRIGHTS(&fromnd, DELETE, WANTPARENT | SAVESTART | AUDITVNODE1,
3519 pathseg, old, oldfd,
3520 &cap_renameat_source_rights, td);
3523 if ((error = namei(&fromnd)) != 0)
3526 error = mac_vnode_check_rename_from(td->td_ucred, fromnd.ni_dvp,
3527 fromnd.ni_vp, &fromnd.ni_cnd);
3528 VOP_UNLOCK(fromnd.ni_dvp);
3529 if (fromnd.ni_dvp != fromnd.ni_vp)
3530 VOP_UNLOCK(fromnd.ni_vp);
3533 NDINIT_ATRIGHTS(&tond, RENAME, LOCKPARENT | LOCKLEAF | NOCACHE |
3534 SAVESTART | AUDITVNODE2, pathseg, new, newfd,
3535 &cap_renameat_target_rights, td);
3536 if (fromnd.ni_vp->v_type == VDIR)
3537 tond.ni_cnd.cn_flags |= WILLBEDIR;
3538 if ((error = namei(&tond)) != 0) {
3539 /* Translate error code for rename("dir1", "dir2/."). */
3540 if (error == EISDIR && fvp->v_type == VDIR)
3542 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3543 vrele(fromnd.ni_dvp);
3549 error = vn_start_write(fvp, &mp, V_NOWAIT);
3551 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3552 NDFREE(&tond, NDF_ONLY_PNBUF);
3559 vrele(fromnd.ni_dvp);
3561 vrele(tond.ni_startdir);
3562 if (fromnd.ni_startdir != NULL)
3563 vrele(fromnd.ni_startdir);
3564 error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH);
3570 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
3573 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
3578 if (newfd != AT_FDCWD && (tond.ni_resflags & NIRES_ABS) == 0) {
3580 * If the target already exists we require CAP_UNLINKAT
3581 * from 'newfd', when newfd was used for the lookup.
3583 error = cap_check(&tond.ni_filecaps.fc_rights,
3584 &cap_unlinkat_rights);
3595 * If the source is the same as the destination (that is, if they
3596 * are links to the same vnode), then there is nothing to do.
3602 error = mac_vnode_check_rename_to(td->td_ucred, tdvp,
3603 tond.ni_vp, fromnd.ni_dvp == tdvp, &tond.ni_cnd);
3607 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
3608 tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
3609 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3610 NDFREE(&tond, NDF_ONLY_PNBUF);
3612 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3613 NDFREE(&tond, NDF_ONLY_PNBUF);
3620 vrele(fromnd.ni_dvp);
3623 vrele(tond.ni_startdir);
3624 vn_finished_write(mp);
3626 if (fromnd.ni_startdir)
3627 vrele(fromnd.ni_startdir);
3634 * Make a directory file.
3636 #ifndef _SYS_SYSPROTO_H_
3643 sys_mkdir(struct thread *td, struct mkdir_args *uap)
3646 return (kern_mkdirat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
3650 #ifndef _SYS_SYSPROTO_H_
3651 struct mkdirat_args {
3658 sys_mkdirat(struct thread *td, struct mkdirat_args *uap)
3661 return (kern_mkdirat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode));
3665 kern_mkdirat(struct thread *td, int fd, const char *path, enum uio_seg segflg,
3671 struct nameidata nd;
3674 AUDIT_ARG_MODE(mode);
3677 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
3678 NOCACHE, segflg, path, fd, &cap_mkdirat_rights,
3680 nd.ni_cnd.cn_flags |= WILLBEDIR;
3681 if ((error = namei(&nd)) != 0)
3685 NDFREE(&nd, NDF_ONLY_PNBUF);
3687 * XXX namei called with LOCKPARENT but not LOCKLEAF has
3688 * the strange behaviour of leaving the vnode unlocked
3689 * if the target is the same vnode as the parent.
3691 if (vp == nd.ni_dvp)
3698 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3699 NDFREE(&nd, NDF_ONLY_PNBUF);
3701 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3706 vattr.va_type = VDIR;
3707 vattr.va_mode = (mode & ACCESSPERMS) &~ td->td_proc->p_fd->fd_cmask;
3709 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
3714 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
3718 NDFREE(&nd, NDF_ONLY_PNBUF);
3722 vn_finished_write(mp);
3727 * Remove a directory file.
3729 #ifndef _SYS_SYSPROTO_H_
3735 sys_rmdir(struct thread *td, struct rmdir_args *uap)
3738 return (kern_frmdirat(td, AT_FDCWD, uap->path, FD_NONE, UIO_USERSPACE,
3743 kern_frmdirat(struct thread *td, int dfd, const char *path, int fd,
3744 enum uio_seg pathseg, int flag)
3749 struct nameidata nd;
3750 cap_rights_t rights;
3754 if (fd != FD_NONE) {
3755 error = getvnode(td, fd, cap_rights_init_one(&rights, CAP_LOOKUP),
3763 NDINIT_ATRIGHTS(&nd, DELETE, LOCKPARENT | LOCKLEAF | AUDITVNODE1 |
3764 ((flag & AT_BENEATH) != 0 ? BENEATH : 0),
3765 pathseg, path, dfd, &cap_unlinkat_rights, td);
3766 if ((error = namei(&nd)) != 0)
3769 if (vp->v_type != VDIR) {
3774 * No rmdir "." please.
3776 if (nd.ni_dvp == vp) {
3781 * The root of a mounted filesystem cannot be deleted.
3783 if (vp->v_vflag & VV_ROOT) {
3788 if (fp != NULL && fp->f_vnode != vp) {
3789 if (VN_IS_DOOMED(fp->f_vnode))
3797 error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp,
3802 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3803 NDFREE(&nd, NDF_ONLY_PNBUF);
3805 if (nd.ni_dvp == vp)
3809 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3813 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_UNLINK);
3814 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
3815 vn_finished_write(mp);
3817 NDFREE(&nd, NDF_ONLY_PNBUF);
3819 if (nd.ni_dvp == vp)
3829 #if defined(COMPAT_43) || defined(COMPAT_FREEBSD11)
3831 freebsd11_kern_getdirentries(struct thread *td, int fd, char *ubuf, u_int count,
3832 long *basep, void (*func)(struct freebsd11_dirent *))
3834 struct freebsd11_dirent dstdp;
3835 struct dirent *dp, *edp;
3838 ssize_t resid, ucount;
3841 /* XXX arbitrary sanity limit on `count'. */
3842 count = min(count, 64 * 1024);
3844 dirbuf = malloc(count, M_TEMP, M_WAITOK);
3846 error = kern_getdirentries(td, fd, dirbuf, count, &base, &resid,
3854 for (dp = (struct dirent *)dirbuf,
3855 edp = (struct dirent *)&dirbuf[count - resid];
3856 ucount < count && dp < edp; ) {
3857 if (dp->d_reclen == 0)
3859 MPASS(dp->d_reclen >= _GENERIC_DIRLEN(0));
3860 if (dp->d_namlen >= sizeof(dstdp.d_name))
3862 dstdp.d_type = dp->d_type;
3863 dstdp.d_namlen = dp->d_namlen;
3864 dstdp.d_fileno = dp->d_fileno; /* truncate */
3865 if (dstdp.d_fileno != dp->d_fileno) {
3866 switch (ino64_trunc_error) {
3874 dstdp.d_fileno = UINT32_MAX;
3878 dstdp.d_reclen = sizeof(dstdp) - sizeof(dstdp.d_name) +
3879 ((dp->d_namlen + 1 + 3) &~ 3);
3880 bcopy(dp->d_name, dstdp.d_name, dstdp.d_namlen);
3881 bzero(dstdp.d_name + dstdp.d_namlen,
3882 dstdp.d_reclen - offsetof(struct freebsd11_dirent, d_name) -
3884 MPASS(dstdp.d_reclen <= dp->d_reclen);
3885 MPASS(ucount + dstdp.d_reclen <= count);
3888 error = copyout(&dstdp, ubuf + ucount, dstdp.d_reclen);
3891 dp = (struct dirent *)((char *)dp + dp->d_reclen);
3892 ucount += dstdp.d_reclen;
3896 free(dirbuf, M_TEMP);
3898 td->td_retval[0] = ucount;
3905 ogetdirentries_cvt(struct freebsd11_dirent *dp)
3907 #if (BYTE_ORDER == LITTLE_ENDIAN)
3909 * The expected low byte of dp->d_namlen is our dp->d_type.
3910 * The high MBZ byte of dp->d_namlen is our dp->d_namlen.
3912 dp->d_type = dp->d_namlen;
3916 * The dp->d_type is the high byte of the expected dp->d_namlen,
3917 * so must be zero'ed.
3924 * Read a block of directory entries in a filesystem independent format.
3926 #ifndef _SYS_SYSPROTO_H_
3927 struct ogetdirentries_args {
3935 ogetdirentries(struct thread *td, struct ogetdirentries_args *uap)
3940 error = kern_ogetdirentries(td, uap, &loff);
3942 error = copyout(&loff, uap->basep, sizeof(long));
3947 kern_ogetdirentries(struct thread *td, struct ogetdirentries_args *uap,
3953 /* XXX arbitrary sanity limit on `count'. */
3954 if (uap->count > 64 * 1024)
3957 error = freebsd11_kern_getdirentries(td, uap->fd, uap->buf, uap->count,
3958 &base, ogetdirentries_cvt);
3960 if (error == 0 && uap->basep != NULL)
3961 error = copyout(&base, uap->basep, sizeof(long));
3965 #endif /* COMPAT_43 */
3967 #if defined(COMPAT_FREEBSD11)
3968 #ifndef _SYS_SYSPROTO_H_
3969 struct freebsd11_getdirentries_args {
3977 freebsd11_getdirentries(struct thread *td,
3978 struct freebsd11_getdirentries_args *uap)
3983 error = freebsd11_kern_getdirentries(td, uap->fd, uap->buf, uap->count,
3986 if (error == 0 && uap->basep != NULL)
3987 error = copyout(&base, uap->basep, sizeof(long));
3992 freebsd11_getdents(struct thread *td, struct freebsd11_getdents_args *uap)
3994 struct freebsd11_getdirentries_args ap;
3998 ap.count = uap->count;
4000 return (freebsd11_getdirentries(td, &ap));
4002 #endif /* COMPAT_FREEBSD11 */
4005 * Read a block of directory entries in a filesystem independent format.
4008 sys_getdirentries(struct thread *td, struct getdirentries_args *uap)
4013 error = kern_getdirentries(td, uap->fd, uap->buf, uap->count, &base,
4014 NULL, UIO_USERSPACE);
4017 if (uap->basep != NULL)
4018 error = copyout(&base, uap->basep, sizeof(off_t));
4023 kern_getdirentries(struct thread *td, int fd, char *buf, size_t count,
4024 off_t *basep, ssize_t *residp, enum uio_seg bufseg)
4035 if (count > IOSIZE_MAX)
4037 auio.uio_resid = count;
4038 error = getvnode(td, fd, &cap_read_rights, &fp);
4041 if ((fp->f_flag & FREAD) == 0) {
4046 foffset = foffset_lock(fp, 0);
4048 if (vp->v_type != VDIR) {
4052 aiov.iov_base = buf;
4053 aiov.iov_len = count;
4054 auio.uio_iov = &aiov;
4055 auio.uio_iovcnt = 1;
4056 auio.uio_rw = UIO_READ;
4057 auio.uio_segflg = bufseg;
4059 vn_lock(vp, LK_SHARED | LK_RETRY);
4060 AUDIT_ARG_VNODE1(vp);
4061 loff = auio.uio_offset = foffset;
4063 error = mac_vnode_check_readdir(td->td_ucred, vp);
4066 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL,
4068 foffset = auio.uio_offset;
4073 if (count == auio.uio_resid &&
4074 (vp->v_vflag & VV_ROOT) &&
4075 (vp->v_mount->mnt_flag & MNT_UNION)) {
4076 struct vnode *tvp = vp;
4078 vp = vp->v_mount->mnt_vnodecovered;
4089 *residp = auio.uio_resid;
4090 td->td_retval[0] = count - auio.uio_resid;
4092 foffset_unlock(fp, foffset, 0);
4098 * Set the mode mask for creation of filesystem nodes.
4100 #ifndef _SYS_SYSPROTO_H_
4106 sys_umask(struct thread *td, struct umask_args *uap)
4108 struct filedesc *fdp;
4110 fdp = td->td_proc->p_fd;
4111 FILEDESC_XLOCK(fdp);
4112 td->td_retval[0] = fdp->fd_cmask;
4113 fdp->fd_cmask = uap->newmask & ALLPERMS;
4114 FILEDESC_XUNLOCK(fdp);
4119 * Void all references to file by ripping underlying filesystem away from
4122 #ifndef _SYS_SYSPROTO_H_
4123 struct revoke_args {
4128 sys_revoke(struct thread *td, struct revoke_args *uap)
4132 struct nameidata nd;
4135 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
4137 if ((error = namei(&nd)) != 0)
4140 NDFREE(&nd, NDF_ONLY_PNBUF);
4141 if (vp->v_type != VCHR || vp->v_rdev == NULL) {
4146 error = mac_vnode_check_revoke(td->td_ucred, vp);
4150 error = VOP_GETATTR(vp, &vattr, td->td_ucred);
4153 if (td->td_ucred->cr_uid != vattr.va_uid) {
4154 error = priv_check(td, PRIV_VFS_ADMIN);
4158 if (vp->v_usecount > 1 || vcount(vp) > 1)
4159 VOP_REVOKE(vp, REVOKEALL);
4166 * Convert a user file descriptor to a kernel file entry and check that, if it
4167 * is a capability, the correct rights are present. A reference on the file
4168 * entry is held upon returning.
4171 getvnode(struct thread *td, int fd, cap_rights_t *rightsp, struct file **fpp)
4176 error = fget_unlocked(td->td_proc->p_fd, fd, rightsp, &fp);
4181 * The file could be not of the vnode type, or it may be not
4182 * yet fully initialized, in which case the f_vnode pointer
4183 * may be set, but f_ops is still badfileops. E.g.,
4184 * devfs_open() transiently create such situation to
4185 * facilitate csw d_fdopen().
4187 * Dupfdopen() handling in kern_openat() installs the
4188 * half-baked file into the process descriptor table, allowing
4189 * other thread to dereference it. Guard against the race by
4192 if (fp->f_vnode == NULL || fp->f_ops == &badfileops) {
4201 * Get an (NFS) file handle.
4203 #ifndef _SYS_SYSPROTO_H_
4204 struct lgetfh_args {
4210 sys_lgetfh(struct thread *td, struct lgetfh_args *uap)
4213 return (kern_getfhat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->fname,
4214 UIO_USERSPACE, uap->fhp));
4217 #ifndef _SYS_SYSPROTO_H_
4224 sys_getfh(struct thread *td, struct getfh_args *uap)
4227 return (kern_getfhat(td, 0, AT_FDCWD, uap->fname, UIO_USERSPACE,
4232 * syscall for the rpc.lockd to use to translate an open descriptor into
4233 * a NFS file handle.
4235 * warning: do not remove the priv_check() call or this becomes one giant
4238 #ifndef _SYS_SYSPROTO_H_
4239 struct getfhat_args {
4247 sys_getfhat(struct thread *td, struct getfhat_args *uap)
4250 if ((uap->flags & ~(AT_SYMLINK_NOFOLLOW | AT_BENEATH)) != 0)
4252 return (kern_getfhat(td, uap->flags, uap->fd, uap->path, UIO_USERSPACE,
4257 kern_getfhat(struct thread *td, int flags, int fd, const char *path,
4258 enum uio_seg pathseg, fhandle_t *fhp)
4260 struct nameidata nd;
4265 error = priv_check(td, PRIV_VFS_GETFH);
4268 NDINIT_AT(&nd, LOOKUP, ((flags & AT_SYMLINK_NOFOLLOW) != 0 ? NOFOLLOW :
4269 FOLLOW) | ((flags & AT_BENEATH) != 0 ? BENEATH : 0) | LOCKLEAF |
4270 AUDITVNODE1, pathseg, path, fd, td);
4274 NDFREE(&nd, NDF_ONLY_PNBUF);
4276 bzero(&fh, sizeof(fh));
4277 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
4278 error = VOP_VPTOFH(vp, &fh.fh_fid);
4281 error = copyout(&fh, fhp, sizeof (fh));
4285 #ifndef _SYS_SYSPROTO_H_
4286 struct fhlink_args {
4292 sys_fhlink(struct thread *td, struct fhlink_args *uap)
4295 return (kern_fhlinkat(td, AT_FDCWD, uap->to, UIO_USERSPACE, uap->fhp));
4298 #ifndef _SYS_SYSPROTO_H_
4299 struct fhlinkat_args {
4306 sys_fhlinkat(struct thread *td, struct fhlinkat_args *uap)
4309 return (kern_fhlinkat(td, uap->tofd, uap->to, UIO_USERSPACE, uap->fhp));
4313 kern_fhlinkat(struct thread *td, int fd, const char *path,
4314 enum uio_seg pathseg, fhandle_t *fhp)
4321 error = priv_check(td, PRIV_VFS_GETFH);
4324 error = copyin(fhp, &fh, sizeof(fh));
4329 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4331 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_SHARED, &vp);
4336 } while ((error = kern_linkat_vp(td, vp, fd, path, pathseg)) == EAGAIN);
4340 #ifndef _SYS_SYSPROTO_H_
4341 struct fhreadlink_args {
4348 sys_fhreadlink(struct thread *td, struct fhreadlink_args *uap)
4355 error = priv_check(td, PRIV_VFS_GETFH);
4358 if (uap->bufsize > IOSIZE_MAX)
4360 error = copyin(uap->fhp, &fh, sizeof(fh));
4363 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4365 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_SHARED, &vp);
4369 error = kern_readlink_vp(vp, uap->buf, UIO_USERSPACE, uap->bufsize, td);
4375 * syscall for the rpc.lockd to use to translate a NFS file handle into an
4378 * warning: do not remove the priv_check() call or this becomes one giant
4381 #ifndef _SYS_SYSPROTO_H_
4382 struct fhopen_args {
4383 const struct fhandle *u_fhp;
4388 sys_fhopen(struct thread *td, struct fhopen_args *uap)
4397 error = priv_check(td, PRIV_VFS_FHOPEN);
4401 fmode = FFLAGS(uap->flags);
4402 /* why not allow a non-read/write open for our lockd? */
4403 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
4405 error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
4408 /* find the mount point */
4409 mp = vfs_busyfs(&fhp.fh_fsid);
4412 /* now give me my vnode, it gets returned to me locked */
4413 error = VFS_FHTOVP(mp, &fhp.fh_fid, LK_EXCLUSIVE, &vp);
4418 error = falloc_noinstall(td, &fp);
4424 * An extra reference on `fp' has been held for us by
4425 * falloc_noinstall().
4431 error = vn_open_vnode(vp, fmode, td->td_ucred, td, fp);
4433 KASSERT(fp->f_ops == &badfileops,
4434 ("VOP_OPEN in fhopen() set f_ops"));
4435 KASSERT(td->td_dupfd < 0,
4436 ("fhopen() encountered fdopen()"));
4446 finit(fp, (fmode & FMASK) | (fp->f_flag & FHASLOCK), DTYPE_VNODE, vp,
4449 if ((fmode & O_TRUNC) != 0) {
4450 error = fo_truncate(fp, 0, td->td_ucred, td);
4455 error = finstall(td, fp, &indx, fmode, NULL);
4458 td->td_retval[0] = indx;
4463 * Stat an (NFS) file handle.
4465 #ifndef _SYS_SYSPROTO_H_
4466 struct fhstat_args {
4467 struct fhandle *u_fhp;
4472 sys_fhstat(struct thread *td, struct fhstat_args *uap)
4478 error = copyin(uap->u_fhp, &fh, sizeof(fh));
4481 error = kern_fhstat(td, fh, &sb);
4483 error = copyout(&sb, uap->sb, sizeof(sb));
4488 kern_fhstat(struct thread *td, struct fhandle fh, struct stat *sb)
4494 error = priv_check(td, PRIV_VFS_FHSTAT);
4497 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4499 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_EXCLUSIVE, &vp);
4503 error = vn_stat(vp, sb, td->td_ucred, NOCRED, td);
4509 * Implement fstatfs() for (NFS) file handles.
4511 #ifndef _SYS_SYSPROTO_H_
4512 struct fhstatfs_args {
4513 struct fhandle *u_fhp;
4518 sys_fhstatfs(struct thread *td, struct fhstatfs_args *uap)
4524 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4527 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
4528 error = kern_fhstatfs(td, fh, sfp);
4530 error = copyout(sfp, uap->buf, sizeof(*sfp));
4531 free(sfp, M_STATFS);
4536 kern_fhstatfs(struct thread *td, fhandle_t fh, struct statfs *buf)
4542 error = priv_check(td, PRIV_VFS_FHSTATFS);
4545 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4547 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_EXCLUSIVE, &vp);
4553 error = prison_canseemount(td->td_ucred, mp);
4557 error = mac_mount_check_stat(td->td_ucred, mp);
4561 error = VFS_STATFS(mp, buf);
4568 * Unlike madvise(2), we do not make a best effort to remember every
4569 * possible caching hint. Instead, we remember the last setting with
4570 * the exception that we will allow POSIX_FADV_NORMAL to adjust the
4571 * region of any current setting.
4574 kern_posix_fadvise(struct thread *td, int fd, off_t offset, off_t len,
4577 struct fadvise_info *fa, *new;
4583 if (offset < 0 || len < 0 || offset > OFF_MAX - len)
4585 AUDIT_ARG_VALUE(advice);
4587 case POSIX_FADV_SEQUENTIAL:
4588 case POSIX_FADV_RANDOM:
4589 case POSIX_FADV_NOREUSE:
4590 new = malloc(sizeof(*fa), M_FADVISE, M_WAITOK);
4592 case POSIX_FADV_NORMAL:
4593 case POSIX_FADV_WILLNEED:
4594 case POSIX_FADV_DONTNEED:
4600 /* XXX: CAP_POSIX_FADVISE? */
4602 error = fget(td, fd, &cap_no_rights, &fp);
4605 AUDIT_ARG_FILE(td->td_proc, fp);
4606 if ((fp->f_ops->fo_flags & DFLAG_SEEKABLE) == 0) {
4610 if (fp->f_type != DTYPE_VNODE) {
4615 if (vp->v_type != VREG) {
4622 end = offset + len - 1;
4624 case POSIX_FADV_SEQUENTIAL:
4625 case POSIX_FADV_RANDOM:
4626 case POSIX_FADV_NOREUSE:
4628 * Try to merge any existing non-standard region with
4629 * this new region if possible, otherwise create a new
4630 * non-standard region for this request.
4632 mtx_pool_lock(mtxpool_sleep, fp);
4634 if (fa != NULL && fa->fa_advice == advice &&
4635 ((fa->fa_start <= end && fa->fa_end >= offset) ||
4636 (end != OFF_MAX && fa->fa_start == end + 1) ||
4637 (fa->fa_end != OFF_MAX && fa->fa_end + 1 == offset))) {
4638 if (offset < fa->fa_start)
4639 fa->fa_start = offset;
4640 if (end > fa->fa_end)
4643 new->fa_advice = advice;
4644 new->fa_start = offset;
4649 mtx_pool_unlock(mtxpool_sleep, fp);
4651 case POSIX_FADV_NORMAL:
4653 * If a the "normal" region overlaps with an existing
4654 * non-standard region, trim or remove the
4655 * non-standard region.
4657 mtx_pool_lock(mtxpool_sleep, fp);
4660 if (offset <= fa->fa_start && end >= fa->fa_end) {
4662 fp->f_advice = NULL;
4663 } else if (offset <= fa->fa_start &&
4664 end >= fa->fa_start)
4665 fa->fa_start = end + 1;
4666 else if (offset <= fa->fa_end && end >= fa->fa_end)
4667 fa->fa_end = offset - 1;
4668 else if (offset >= fa->fa_start && end <= fa->fa_end) {
4670 * If the "normal" region is a middle
4671 * portion of the existing
4672 * non-standard region, just remove
4673 * the whole thing rather than picking
4674 * one side or the other to
4678 fp->f_advice = NULL;
4681 mtx_pool_unlock(mtxpool_sleep, fp);
4683 case POSIX_FADV_WILLNEED:
4684 case POSIX_FADV_DONTNEED:
4685 error = VOP_ADVISE(vp, offset, end, advice);
4691 free(new, M_FADVISE);
4696 sys_posix_fadvise(struct thread *td, struct posix_fadvise_args *uap)
4700 error = kern_posix_fadvise(td, uap->fd, uap->offset, uap->len,
4702 return (kern_posix_error(td, error));
4706 kern_copy_file_range(struct thread *td, int infd, off_t *inoffp, int outfd,
4707 off_t *outoffp, size_t len, unsigned int flags)
4709 struct file *infp, *outfp;
4710 struct vnode *invp, *outvp;
4713 void *rl_rcookie, *rl_wcookie;
4714 off_t savinoff, savoutoff;
4716 infp = outfp = NULL;
4717 rl_rcookie = rl_wcookie = NULL;
4726 if (len > SSIZE_MAX)
4728 * Although the len argument is size_t, the return argument
4729 * is ssize_t (which is signed). Therefore a size that won't
4730 * fit in ssize_t can't be returned.
4734 /* Get the file structures for the file descriptors. */
4735 error = fget_read(td, infd, &cap_read_rights, &infp);
4738 if (infp->f_ops == &badfileops) {
4742 if (infp->f_vnode == NULL) {
4746 error = fget_write(td, outfd, &cap_write_rights, &outfp);
4749 if (outfp->f_ops == &badfileops) {
4753 if (outfp->f_vnode == NULL) {
4758 /* Set the offset pointers to the correct place. */
4760 inoffp = &infp->f_offset;
4761 if (outoffp == NULL)
4762 outoffp = &outfp->f_offset;
4764 savoutoff = *outoffp;
4766 invp = infp->f_vnode;
4767 outvp = outfp->f_vnode;
4768 /* Sanity check the f_flag bits. */
4769 if ((outfp->f_flag & (FWRITE | FAPPEND)) != FWRITE ||
4770 (infp->f_flag & FREAD) == 0) {
4775 /* If len == 0, just return 0. */
4780 * If infp and outfp refer to the same file, the byte ranges cannot
4783 if (invp == outvp && ((savinoff <= savoutoff && savinoff + len >
4784 savoutoff) || (savinoff > savoutoff && savoutoff + len >
4790 /* Range lock the byte ranges for both invp and outvp. */
4792 rl_wcookie = vn_rangelock_wlock(outvp, *outoffp, *outoffp +
4794 rl_rcookie = vn_rangelock_tryrlock(invp, *inoffp, *inoffp +
4796 if (rl_rcookie != NULL)
4798 vn_rangelock_unlock(outvp, rl_wcookie);
4799 rl_rcookie = vn_rangelock_rlock(invp, *inoffp, *inoffp + len);
4800 vn_rangelock_unlock(invp, rl_rcookie);
4804 error = vn_copy_file_range(invp, inoffp, outvp, outoffp, &retlen,
4805 flags, infp->f_cred, outfp->f_cred, td);
4807 if (rl_rcookie != NULL)
4808 vn_rangelock_unlock(invp, rl_rcookie);
4809 if (rl_wcookie != NULL)
4810 vn_rangelock_unlock(outvp, rl_wcookie);
4811 if (savinoff != -1 && (error == EINTR || error == ERESTART)) {
4813 *outoffp = savoutoff;
4819 td->td_retval[0] = retlen;
4824 sys_copy_file_range(struct thread *td, struct copy_file_range_args *uap)
4826 off_t inoff, outoff, *inoffp, *outoffp;
4829 inoffp = outoffp = NULL;
4830 if (uap->inoffp != NULL) {
4831 error = copyin(uap->inoffp, &inoff, sizeof(off_t));
4836 if (uap->outoffp != NULL) {
4837 error = copyin(uap->outoffp, &outoff, sizeof(off_t));
4842 error = kern_copy_file_range(td, uap->infd, inoffp, uap->outfd,
4843 outoffp, uap->len, uap->flags);
4844 if (error == 0 && uap->inoffp != NULL)
4845 error = copyout(inoffp, uap->inoffp, sizeof(off_t));
4846 if (error == 0 && uap->outoffp != NULL)
4847 error = copyout(outoffp, uap->outoffp, sizeof(off_t));