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_compat.h"
44 #include "opt_ktrace.h"
46 #include <sys/param.h>
47 #include <sys/systm.h>
50 #include <sys/capsicum.h>
52 #include <sys/sysent.h>
53 #include <sys/malloc.h>
54 #include <sys/mount.h>
55 #include <sys/mutex.h>
56 #include <sys/sysproto.h>
57 #include <sys/namei.h>
58 #include <sys/filedesc.h>
59 #include <sys/kernel.h>
60 #include <sys/fcntl.h>
62 #include <sys/filio.h>
63 #include <sys/limits.h>
64 #include <sys/linker.h>
65 #include <sys/rwlock.h>
69 #include <sys/unistd.h>
70 #include <sys/vnode.h>
73 #include <sys/dirent.h>
75 #include <sys/syscallsubr.h>
76 #include <sys/sysctl.h>
78 #include <sys/ktrace.h>
81 #include <machine/stdarg.h>
83 #include <security/audit/audit.h>
84 #include <security/mac/mac_framework.h>
87 #include <vm/vm_object.h>
88 #include <vm/vm_page.h>
91 #include <ufs/ufs/quota.h>
93 MALLOC_DEFINE(M_FADVISE, "fadvise", "posix_fadvise(2) information");
95 SDT_PROVIDER_DEFINE(vfs);
96 SDT_PROBE_DEFINE2(vfs, , stat, mode, "char *", "int");
97 SDT_PROBE_DEFINE2(vfs, , stat, reg, "char *", "int");
99 static int kern_chflagsat(struct thread *td, int fd, const char *path,
100 enum uio_seg pathseg, u_long flags, int atflag);
101 static int setfflags(struct thread *td, struct vnode *, u_long);
102 static int getutimes(const struct timeval *, enum uio_seg, struct timespec *);
103 static int getutimens(const struct timespec *, enum uio_seg,
104 struct timespec *, int *);
105 static int setutimes(struct thread *td, struct vnode *,
106 const struct timespec *, int, int);
107 static int vn_access(struct vnode *vp, int user_flags, struct ucred *cred,
111 * Sync each mounted filesystem.
113 #ifndef _SYS_SYSPROTO_H_
120 sys_sync(struct thread *td, struct sync_args *uap)
122 struct mount *mp, *nmp;
125 mtx_lock(&mountlist_mtx);
126 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
127 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK)) {
128 nmp = TAILQ_NEXT(mp, mnt_list);
131 if ((mp->mnt_flag & MNT_RDONLY) == 0 &&
132 vn_start_write(NULL, &mp, V_NOWAIT) == 0) {
133 save = curthread_pflags_set(TDP_SYNCIO);
134 vfs_msync(mp, MNT_NOWAIT);
135 VFS_SYNC(mp, MNT_NOWAIT);
136 curthread_pflags_restore(save);
137 vn_finished_write(mp);
139 mtx_lock(&mountlist_mtx);
140 nmp = TAILQ_NEXT(mp, mnt_list);
143 mtx_unlock(&mountlist_mtx);
148 * Change filesystem quotas.
150 #ifndef _SYS_SYSPROTO_H_
151 struct quotactl_args {
159 sys_quotactl(struct thread *td, struct quotactl_args *uap)
165 AUDIT_ARG_CMD(uap->cmd);
166 AUDIT_ARG_UID(uap->uid);
167 if (!prison_allow(td->td_ucred, PR_ALLOW_QUOTAS))
169 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
171 if ((error = namei(&nd)) != 0)
173 NDFREE(&nd, NDF_ONLY_PNBUF);
174 mp = nd.ni_vp->v_mount;
177 error = vfs_busy(mp, 0);
181 error = VFS_QUOTACTL(mp, uap->cmd, uap->uid, uap->arg);
184 * Since quota on operation typically needs to open quota
185 * file, the Q_QUOTAON handler needs to unbusy the mount point
186 * before calling into namei. Otherwise, unmount might be
187 * started between two vfs_busy() invocations (first is our,
188 * second is from mount point cross-walk code in lookup()),
191 * Require that Q_QUOTAON handles the vfs_busy() reference on
192 * its own, always returning with ubusied mount point.
194 if ((uap->cmd >> SUBCMDSHIFT) != Q_QUOTAON)
200 * Used by statfs conversion routines to scale the block size up if
201 * necessary so that all of the block counts are <= 'max_size'. Note
202 * that 'max_size' should be a bitmask, i.e. 2^n - 1 for some non-zero
206 statfs_scale_blocks(struct statfs *sf, long max_size)
211 KASSERT(powerof2(max_size + 1), ("%s: invalid max_size", __func__));
214 * Attempt to scale the block counts to give a more accurate
215 * overview to userland of the ratio of free space to used
216 * space. To do this, find the largest block count and compute
217 * a divisor that lets it fit into a signed integer <= max_size.
219 if (sf->f_bavail < 0)
220 count = -sf->f_bavail;
222 count = sf->f_bavail;
223 count = MAX(sf->f_blocks, MAX(sf->f_bfree, count));
224 if (count <= max_size)
227 count >>= flsl(max_size);
234 sf->f_bsize <<= shift;
235 sf->f_blocks >>= shift;
236 sf->f_bfree >>= shift;
237 sf->f_bavail >>= shift;
241 kern_do_statfs(struct thread *td, struct mount *mp, struct statfs *buf)
248 error = vfs_busy(mp, 0);
253 error = mac_mount_check_stat(td->td_ucred, mp);
258 * Set these in case the underlying filesystem fails to do so.
261 sp->f_version = STATFS_VERSION;
262 sp->f_namemax = NAME_MAX;
263 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
264 error = VFS_STATFS(mp, sp);
268 if (priv_check(td, PRIV_VFS_GENERATION)) {
269 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
270 prison_enforce_statfs(td->td_ucred, mp, buf);
278 * Get filesystem statistics.
280 #ifndef _SYS_SYSPROTO_H_
287 sys_statfs(struct thread *td, struct statfs_args *uap)
292 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
293 error = kern_statfs(td, uap->path, UIO_USERSPACE, sfp);
295 error = copyout(sfp, uap->buf, sizeof(struct statfs));
301 kern_statfs(struct thread *td, char *path, enum uio_seg pathseg,
308 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
313 mp = nd.ni_vp->v_mount;
315 NDFREE(&nd, NDF_ONLY_PNBUF);
317 return (kern_do_statfs(td, mp, buf));
321 * Get filesystem statistics.
323 #ifndef _SYS_SYSPROTO_H_
324 struct fstatfs_args {
330 sys_fstatfs(struct thread *td, struct fstatfs_args *uap)
335 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
336 error = kern_fstatfs(td, uap->fd, sfp);
338 error = copyout(sfp, uap->buf, sizeof(struct statfs));
344 kern_fstatfs(struct thread *td, int fd, struct statfs *buf)
353 error = getvnode(td, fd, cap_rights_init(&rights, CAP_FSTATFS), &fp);
357 vn_lock(vp, LK_SHARED | LK_RETRY);
359 AUDIT_ARG_VNODE1(vp);
366 return (kern_do_statfs(td, mp, buf));
370 * Get statistics on all filesystems.
372 #ifndef _SYS_SYSPROTO_H_
373 struct getfsstat_args {
380 sys_getfsstat(struct thread *td, struct getfsstat_args *uap)
385 if (uap->bufsize < 0 || uap->bufsize > SIZE_MAX)
387 error = kern_getfsstat(td, &uap->buf, uap->bufsize, &count,
388 UIO_USERSPACE, uap->mode);
390 td->td_retval[0] = count;
395 * If (bufsize > 0 && bufseg == UIO_SYSSPACE)
396 * The caller is responsible for freeing memory which will be allocated
400 kern_getfsstat(struct thread *td, struct statfs **buf, size_t bufsize,
401 size_t *countp, enum uio_seg bufseg, int mode)
403 struct mount *mp, *nmp;
404 struct statfs *sfsp, *sp, *sptmp, *tofree;
405 size_t count, maxcount;
413 if (bufseg == UIO_SYSSPACE)
418 maxcount = bufsize / sizeof(struct statfs);
422 } else if (bufseg == UIO_USERSPACE) {
425 } else /* if (bufseg == UIO_SYSSPACE) */ {
427 mtx_lock(&mountlist_mtx);
428 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
431 mtx_unlock(&mountlist_mtx);
432 if (maxcount > count)
434 tofree = sfsp = *buf = malloc(maxcount * sizeof(struct statfs),
438 mtx_lock(&mountlist_mtx);
439 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
440 if (prison_canseemount(td->td_ucred, mp) != 0) {
441 nmp = TAILQ_NEXT(mp, mnt_list);
445 if (mac_mount_check_stat(td->td_ucred, mp) != 0) {
446 nmp = TAILQ_NEXT(mp, mnt_list);
450 if (mode == MNT_WAIT) {
451 if (vfs_busy(mp, MBF_MNTLSTLOCK) != 0) {
453 * If vfs_busy() failed, and MBF_NOWAIT
454 * wasn't passed, then the mp is gone.
455 * Furthermore, because of MBF_MNTLSTLOCK,
456 * the mountlist_mtx was dropped. We have
457 * no other choice than to start over.
459 mtx_unlock(&mountlist_mtx);
460 free(tofree, M_STATFS);
464 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK) != 0) {
465 nmp = TAILQ_NEXT(mp, mnt_list);
469 if (sfsp != NULL && count < maxcount) {
472 * Set these in case the underlying filesystem
475 sp->f_version = STATFS_VERSION;
476 sp->f_namemax = NAME_MAX;
477 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
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(td, PRIV_VFS_GENERATION)) {
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;
835 AUDIT_ARG_FD(uap->fd);
836 error = getvnode(td, uap->fd, cap_rights_init(&rights, CAP_FCHDIR),
843 vn_lock(vp, LK_SHARED | LK_RETRY);
844 AUDIT_ARG_VNODE1(vp);
845 error = change_dir(vp, td);
846 while (!error && (mp = vp->v_mountedhere) != NULL) {
849 error = VFS_ROOT(mp, LK_SHARED, &tdp);
866 * Change current working directory (``.'').
868 #ifndef _SYS_SYSPROTO_H_
874 sys_chdir(struct thread *td, struct chdir_args *uap)
877 return (kern_chdir(td, uap->path, UIO_USERSPACE));
881 kern_chdir(struct thread *td, char *path, enum uio_seg pathseg)
886 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
888 if ((error = namei(&nd)) != 0)
890 if ((error = change_dir(nd.ni_vp, td)) != 0) {
892 NDFREE(&nd, NDF_ONLY_PNBUF);
895 VOP_UNLOCK(nd.ni_vp, 0);
896 NDFREE(&nd, NDF_ONLY_PNBUF);
897 pwd_chdir(td, nd.ni_vp);
902 * Change notion of root (``/'') directory.
904 #ifndef _SYS_SYSPROTO_H_
910 sys_chroot(struct thread *td, struct chroot_args *uap)
915 error = priv_check(td, PRIV_VFS_CHROOT);
918 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
919 UIO_USERSPACE, uap->path, td);
923 error = change_dir(nd.ni_vp, td);
927 error = mac_vnode_check_chroot(td->td_ucred, nd.ni_vp);
931 VOP_UNLOCK(nd.ni_vp, 0);
932 error = pwd_chroot(td, nd.ni_vp);
934 NDFREE(&nd, NDF_ONLY_PNBUF);
939 NDFREE(&nd, NDF_ONLY_PNBUF);
944 * Common routine for chroot and chdir. Callers must provide a locked vnode
948 change_dir(struct vnode *vp, struct thread *td)
954 ASSERT_VOP_LOCKED(vp, "change_dir(): vp not locked");
955 if (vp->v_type != VDIR)
958 error = mac_vnode_check_chdir(td->td_ucred, vp);
962 return (VOP_ACCESS(vp, VEXEC, td->td_ucred, td));
966 flags_to_rights(int flags, cap_rights_t *rightsp)
969 if (flags & O_EXEC) {
970 cap_rights_set(rightsp, CAP_FEXECVE);
972 switch ((flags & O_ACCMODE)) {
974 cap_rights_set(rightsp, CAP_READ);
977 cap_rights_set(rightsp, CAP_READ);
980 cap_rights_set(rightsp, CAP_WRITE);
981 if (!(flags & (O_APPEND | O_TRUNC)))
982 cap_rights_set(rightsp, CAP_SEEK);
988 cap_rights_set(rightsp, CAP_CREATE);
991 cap_rights_set(rightsp, CAP_FTRUNCATE);
993 if (flags & (O_SYNC | O_FSYNC))
994 cap_rights_set(rightsp, CAP_FSYNC);
996 if (flags & (O_EXLOCK | O_SHLOCK))
997 cap_rights_set(rightsp, CAP_FLOCK);
1001 * Check permissions, allocate an open file structure, and call the device
1002 * open routine if any.
1004 #ifndef _SYS_SYSPROTO_H_
1012 sys_open(struct thread *td, struct open_args *uap)
1015 return (kern_openat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1016 uap->flags, uap->mode));
1019 #ifndef _SYS_SYSPROTO_H_
1020 struct openat_args {
1028 sys_openat(struct thread *td, struct openat_args *uap)
1031 AUDIT_ARG_FD(uap->fd);
1032 return (kern_openat(td, uap->fd, uap->path, UIO_USERSPACE, uap->flag,
1037 kern_openat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1038 int flags, int mode)
1040 struct proc *p = td->td_proc;
1041 struct filedesc *fdp = p->p_fd;
1044 struct nameidata nd;
1045 cap_rights_t rights;
1046 int cmode, error, indx;
1050 AUDIT_ARG_FFLAGS(flags);
1051 AUDIT_ARG_MODE(mode);
1052 cap_rights_init(&rights, CAP_LOOKUP);
1053 flags_to_rights(flags, &rights);
1055 * Only one of the O_EXEC, O_RDONLY, O_WRONLY and O_RDWR flags
1058 if (flags & O_EXEC) {
1059 if (flags & O_ACCMODE)
1061 } else if ((flags & O_ACCMODE) == O_ACCMODE) {
1064 flags = FFLAGS(flags);
1068 * Allocate a file structure. The descriptor to reference it
1069 * is allocated and set by finstall() below.
1071 error = falloc_noinstall(td, &fp);
1075 * An extra reference on `fp' has been held for us by
1076 * falloc_noinstall().
1078 /* Set the flags early so the finit in devfs can pick them up. */
1079 fp->f_flag = flags & FMASK;
1080 cmode = ((mode & ~fdp->fd_cmask) & ALLPERMS) & ~S_ISTXT;
1081 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, fd,
1083 td->td_dupfd = -1; /* XXX check for fdopen */
1084 error = vn_open(&nd, &flags, cmode, fp);
1087 * If the vn_open replaced the method vector, something
1088 * wonderous happened deep below and we just pass it up
1089 * pretending we know what we do.
1091 if (error == ENXIO && fp->f_ops != &badfileops)
1095 * Handle special fdopen() case. bleh.
1097 * Don't do this for relative (capability) lookups; we don't
1098 * understand exactly what would happen, and we don't think
1099 * that it ever should.
1101 if ((nd.ni_lcf & NI_LCF_STRICTRELATIVE) == 0 &&
1102 (error == ENODEV || error == ENXIO) &&
1103 td->td_dupfd >= 0) {
1104 error = dupfdopen(td, fdp, td->td_dupfd, flags, error,
1113 NDFREE(&nd, NDF_ONLY_PNBUF);
1117 * Store the vnode, for any f_type. Typically, the vnode use
1118 * count is decremented by direct call to vn_closefile() for
1119 * files that switched type in the cdevsw fdopen() method.
1123 * If the file wasn't claimed by devfs bind it to the normal
1124 * vnode operations here.
1126 if (fp->f_ops == &badfileops) {
1127 KASSERT(vp->v_type != VFIFO, ("Unexpected fifo."));
1129 finit(fp, (flags & FMASK) | (fp->f_flag & FHASLOCK),
1130 DTYPE_VNODE, vp, &vnops);
1134 if (flags & O_TRUNC) {
1135 error = fo_truncate(fp, 0, td->td_ucred, td);
1141 * If we haven't already installed the FD (for dupfdopen), do so now.
1144 struct filecaps *fcaps;
1147 if ((nd.ni_lcf & NI_LCF_STRICTRELATIVE) != 0)
1148 fcaps = &nd.ni_filecaps;
1152 error = finstall(td, fp, &indx, flags, fcaps);
1153 /* On success finstall() consumes fcaps. */
1155 filecaps_free(&nd.ni_filecaps);
1159 filecaps_free(&nd.ni_filecaps);
1163 * Release our private reference, leaving the one associated with
1164 * the descriptor table intact.
1167 td->td_retval[0] = indx;
1170 KASSERT(indx == -1, ("indx=%d, should be -1", indx));
1179 #ifndef _SYS_SYSPROTO_H_
1180 struct ocreat_args {
1186 ocreat(struct thread *td, struct ocreat_args *uap)
1189 return (kern_openat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1190 O_WRONLY | O_CREAT | O_TRUNC, uap->mode));
1192 #endif /* COMPAT_43 */
1195 * Create a special file.
1197 #ifndef _SYS_SYSPROTO_H_
1198 struct mknodat_args {
1206 sys_mknodat(struct thread *td, struct mknodat_args *uap)
1209 return (kern_mknodat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode,
1213 #if defined(COMPAT_FREEBSD11)
1215 freebsd11_mknod(struct thread *td,
1216 struct freebsd11_mknod_args *uap)
1219 return (kern_mknodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1220 uap->mode, uap->dev));
1224 freebsd11_mknodat(struct thread *td,
1225 struct freebsd11_mknodat_args *uap)
1228 return (kern_mknodat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode,
1231 #endif /* COMPAT_FREEBSD11 */
1234 kern_mknodat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1235 int mode, dev_t dev)
1240 struct nameidata nd;
1241 cap_rights_t rights;
1242 int error, whiteout = 0;
1244 AUDIT_ARG_MODE(mode);
1246 switch (mode & S_IFMT) {
1249 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
1250 if (error == 0 && dev == VNOVAL)
1254 error = priv_check(td, PRIV_VFS_MKNOD_WHT);
1258 return (kern_mkfifoat(td, fd, path, pathseg, mode));
1268 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
1269 NOCACHE, pathseg, path, fd, cap_rights_init(&rights, CAP_MKNODAT),
1271 if ((error = namei(&nd)) != 0)
1275 NDFREE(&nd, NDF_ONLY_PNBUF);
1276 if (vp == nd.ni_dvp)
1284 vattr.va_mode = (mode & ALLPERMS) &
1285 ~td->td_proc->p_fd->fd_cmask;
1286 vattr.va_rdev = dev;
1289 switch (mode & S_IFMT) {
1291 vattr.va_type = VCHR;
1294 vattr.va_type = VBLK;
1300 panic("kern_mknod: invalid mode");
1303 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1304 NDFREE(&nd, NDF_ONLY_PNBUF);
1306 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1311 if (error == 0 && !whiteout)
1312 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp,
1313 &nd.ni_cnd, &vattr);
1317 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, CREATE);
1319 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp,
1320 &nd.ni_cnd, &vattr);
1325 NDFREE(&nd, NDF_ONLY_PNBUF);
1327 vn_finished_write(mp);
1332 * Create a named pipe.
1334 #ifndef _SYS_SYSPROTO_H_
1335 struct mkfifo_args {
1341 sys_mkfifo(struct thread *td, struct mkfifo_args *uap)
1344 return (kern_mkfifoat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1348 #ifndef _SYS_SYSPROTO_H_
1349 struct mkfifoat_args {
1356 sys_mkfifoat(struct thread *td, struct mkfifoat_args *uap)
1359 return (kern_mkfifoat(td, uap->fd, uap->path, UIO_USERSPACE,
1364 kern_mkfifoat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1369 struct nameidata nd;
1370 cap_rights_t rights;
1373 AUDIT_ARG_MODE(mode);
1376 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
1377 NOCACHE, pathseg, path, fd, cap_rights_init(&rights, CAP_MKFIFOAT),
1379 if ((error = namei(&nd)) != 0)
1381 if (nd.ni_vp != NULL) {
1382 NDFREE(&nd, NDF_ONLY_PNBUF);
1383 if (nd.ni_vp == nd.ni_dvp)
1390 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1391 NDFREE(&nd, NDF_ONLY_PNBUF);
1393 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1398 vattr.va_type = VFIFO;
1399 vattr.va_mode = (mode & ALLPERMS) & ~td->td_proc->p_fd->fd_cmask;
1401 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1406 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
1413 vn_finished_write(mp);
1414 NDFREE(&nd, NDF_ONLY_PNBUF);
1419 * Make a hard file link.
1421 #ifndef _SYS_SYSPROTO_H_
1428 sys_link(struct thread *td, struct link_args *uap)
1431 return (kern_linkat(td, AT_FDCWD, AT_FDCWD, uap->path, uap->link,
1432 UIO_USERSPACE, FOLLOW));
1435 #ifndef _SYS_SYSPROTO_H_
1436 struct linkat_args {
1445 sys_linkat(struct thread *td, struct linkat_args *uap)
1450 if (flag & ~AT_SYMLINK_FOLLOW)
1453 return (kern_linkat(td, uap->fd1, uap->fd2, uap->path1, uap->path2,
1454 UIO_USERSPACE, (flag & AT_SYMLINK_FOLLOW) ? FOLLOW : NOFOLLOW));
1457 int hardlink_check_uid = 0;
1458 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
1459 &hardlink_check_uid, 0,
1460 "Unprivileged processes cannot create hard links to files owned by other "
1462 static int hardlink_check_gid = 0;
1463 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
1464 &hardlink_check_gid, 0,
1465 "Unprivileged processes cannot create hard links to files owned by other "
1469 can_hardlink(struct vnode *vp, struct ucred *cred)
1474 if (!hardlink_check_uid && !hardlink_check_gid)
1477 error = VOP_GETATTR(vp, &va, cred);
1481 if (hardlink_check_uid && cred->cr_uid != va.va_uid) {
1482 error = priv_check_cred(cred, PRIV_VFS_LINK, 0);
1487 if (hardlink_check_gid && !groupmember(va.va_gid, cred)) {
1488 error = priv_check_cred(cred, PRIV_VFS_LINK, 0);
1497 kern_linkat(struct thread *td, int fd1, int fd2, char *path1, char *path2,
1498 enum uio_seg segflg, int follow)
1502 struct nameidata nd;
1503 cap_rights_t rights;
1508 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, segflg, path1, fd1,
1509 cap_rights_init(&rights, CAP_LINKAT_SOURCE), td);
1511 if ((error = namei(&nd)) != 0)
1513 NDFREE(&nd, NDF_ONLY_PNBUF);
1515 if (vp->v_type == VDIR) {
1517 return (EPERM); /* POSIX */
1519 NDINIT_ATRIGHTS(&nd, CREATE,
1520 LOCKPARENT | SAVENAME | AUDITVNODE2 | NOCACHE, segflg, path2, fd2,
1521 cap_rights_init(&rights, CAP_LINKAT_TARGET), td);
1522 if ((error = namei(&nd)) == 0) {
1523 if (nd.ni_vp != NULL) {
1524 NDFREE(&nd, NDF_ONLY_PNBUF);
1525 if (nd.ni_dvp == nd.ni_vp)
1532 } else if (nd.ni_dvp->v_mount != vp->v_mount) {
1534 * Cross-device link. No need to recheck
1535 * vp->v_type, since it cannot change, except
1538 NDFREE(&nd, NDF_ONLY_PNBUF);
1542 } else if ((error = vn_lock(vp, LK_EXCLUSIVE)) == 0) {
1543 error = can_hardlink(vp, td->td_ucred);
1546 error = mac_vnode_check_link(td->td_ucred,
1547 nd.ni_dvp, vp, &nd.ni_cnd);
1552 NDFREE(&nd, NDF_ONLY_PNBUF);
1555 error = vn_start_write(vp, &mp, V_NOWAIT);
1559 NDFREE(&nd, NDF_ONLY_PNBUF);
1560 error = vn_start_write(NULL, &mp,
1566 error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
1569 vn_finished_write(mp);
1570 NDFREE(&nd, NDF_ONLY_PNBUF);
1573 NDFREE(&nd, NDF_ONLY_PNBUF);
1583 * Make a symbolic link.
1585 #ifndef _SYS_SYSPROTO_H_
1586 struct symlink_args {
1592 sys_symlink(struct thread *td, struct symlink_args *uap)
1595 return (kern_symlinkat(td, uap->path, AT_FDCWD, uap->link,
1599 #ifndef _SYS_SYSPROTO_H_
1600 struct symlinkat_args {
1607 sys_symlinkat(struct thread *td, struct symlinkat_args *uap)
1610 return (kern_symlinkat(td, uap->path1, uap->fd, uap->path2,
1615 kern_symlinkat(struct thread *td, char *path1, int fd, char *path2,
1616 enum uio_seg segflg)
1621 struct nameidata nd;
1623 cap_rights_t rights;
1625 if (segflg == UIO_SYSSPACE) {
1628 syspath = uma_zalloc(namei_zone, M_WAITOK);
1629 if ((error = copyinstr(path1, syspath, MAXPATHLEN, NULL)) != 0)
1632 AUDIT_ARG_TEXT(syspath);
1635 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
1636 NOCACHE, segflg, path2, fd, cap_rights_init(&rights, CAP_SYMLINKAT),
1638 if ((error = namei(&nd)) != 0)
1641 NDFREE(&nd, NDF_ONLY_PNBUF);
1642 if (nd.ni_vp == nd.ni_dvp)
1650 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1651 NDFREE(&nd, NDF_ONLY_PNBUF);
1653 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1658 vattr.va_mode = ACCESSPERMS &~ td->td_proc->p_fd->fd_cmask;
1660 vattr.va_type = VLNK;
1661 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1666 error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr, syspath);
1672 NDFREE(&nd, NDF_ONLY_PNBUF);
1674 vn_finished_write(mp);
1676 if (segflg != UIO_SYSSPACE)
1677 uma_zfree(namei_zone, syspath);
1682 * Delete a whiteout from the filesystem.
1684 #ifndef _SYS_SYSPROTO_H_
1685 struct undelete_args {
1690 sys_undelete(struct thread *td, struct undelete_args *uap)
1693 struct nameidata nd;
1698 NDINIT(&nd, DELETE, LOCKPARENT | DOWHITEOUT | AUDITVNODE1,
1699 UIO_USERSPACE, uap->path, td);
1704 if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & ISWHITEOUT)) {
1705 NDFREE(&nd, NDF_ONLY_PNBUF);
1706 if (nd.ni_vp == nd.ni_dvp)
1714 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1715 NDFREE(&nd, NDF_ONLY_PNBUF);
1717 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1721 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, DELETE);
1722 NDFREE(&nd, NDF_ONLY_PNBUF);
1724 vn_finished_write(mp);
1729 * Delete a name from the filesystem.
1731 #ifndef _SYS_SYSPROTO_H_
1732 struct unlink_args {
1737 sys_unlink(struct thread *td, struct unlink_args *uap)
1740 return (kern_unlinkat(td, AT_FDCWD, uap->path, UIO_USERSPACE, 0));
1743 #ifndef _SYS_SYSPROTO_H_
1744 struct unlinkat_args {
1751 sys_unlinkat(struct thread *td, struct unlinkat_args *uap)
1753 int flag = uap->flag;
1755 char *path = uap->path;
1757 if (flag & ~AT_REMOVEDIR)
1760 if (flag & AT_REMOVEDIR)
1761 return (kern_rmdirat(td, fd, path, UIO_USERSPACE));
1763 return (kern_unlinkat(td, fd, path, UIO_USERSPACE, 0));
1767 kern_unlinkat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1772 struct nameidata nd;
1774 cap_rights_t rights;
1779 NDINIT_ATRIGHTS(&nd, DELETE, LOCKPARENT | LOCKLEAF | AUDITVNODE1,
1780 pathseg, path, fd, cap_rights_init(&rights, CAP_UNLINKAT), td);
1781 if ((error = namei(&nd)) != 0)
1782 return (error == EINVAL ? EPERM : error);
1784 if (vp->v_type == VDIR && oldinum == 0) {
1785 error = EPERM; /* POSIX */
1786 } else if (oldinum != 0 &&
1787 ((error = vn_stat(vp, &sb, td->td_ucred, NOCRED, td)) == 0) &&
1788 sb.st_ino != oldinum) {
1789 error = EIDRM; /* Identifier removed */
1792 * The root of a mounted filesystem cannot be deleted.
1794 * XXX: can this only be a VDIR case?
1796 if (vp->v_vflag & VV_ROOT)
1800 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1801 NDFREE(&nd, NDF_ONLY_PNBUF);
1803 if (vp == nd.ni_dvp)
1807 if ((error = vn_start_write(NULL, &mp,
1808 V_XSLEEP | PCATCH)) != 0)
1813 error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp,
1818 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_UNLINK);
1819 error = VOP_REMOVE(nd.ni_dvp, vp, &nd.ni_cnd);
1823 vn_finished_write(mp);
1825 NDFREE(&nd, NDF_ONLY_PNBUF);
1827 if (vp == nd.ni_dvp)
1835 * Reposition read/write file offset.
1837 #ifndef _SYS_SYSPROTO_H_
1846 sys_lseek(struct thread *td, struct lseek_args *uap)
1849 return (kern_lseek(td, uap->fd, uap->offset, uap->whence));
1853 kern_lseek(struct thread *td, int fd, off_t offset, int whence)
1856 cap_rights_t rights;
1860 error = fget(td, fd, cap_rights_init(&rights, CAP_SEEK), &fp);
1863 error = (fp->f_ops->fo_flags & DFLAG_SEEKABLE) != 0 ?
1864 fo_seek(fp, offset, whence, td) : ESPIPE;
1869 #if defined(COMPAT_43)
1871 * Reposition read/write file offset.
1873 #ifndef _SYS_SYSPROTO_H_
1874 struct olseek_args {
1881 olseek(struct thread *td, struct olseek_args *uap)
1884 return (kern_lseek(td, uap->fd, uap->offset, uap->whence));
1886 #endif /* COMPAT_43 */
1888 #if defined(COMPAT_FREEBSD6)
1889 /* Version with the 'pad' argument */
1891 freebsd6_lseek(struct thread *td, struct freebsd6_lseek_args *uap)
1894 return (kern_lseek(td, uap->fd, uap->offset, uap->whence));
1899 * Check access permissions using passed credentials.
1902 vn_access(struct vnode *vp, int user_flags, struct ucred *cred,
1908 /* Flags == 0 means only check for existence. */
1909 if (user_flags == 0)
1913 if (user_flags & R_OK)
1915 if (user_flags & W_OK)
1917 if (user_flags & X_OK)
1920 error = mac_vnode_check_access(cred, vp, accmode);
1924 if ((accmode & VWRITE) == 0 || (error = vn_writechk(vp)) == 0)
1925 error = VOP_ACCESS(vp, accmode, cred, td);
1930 * Check access permissions using "real" credentials.
1932 #ifndef _SYS_SYSPROTO_H_
1933 struct access_args {
1939 sys_access(struct thread *td, struct access_args *uap)
1942 return (kern_accessat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1946 #ifndef _SYS_SYSPROTO_H_
1947 struct faccessat_args {
1955 sys_faccessat(struct thread *td, struct faccessat_args *uap)
1958 return (kern_accessat(td, uap->fd, uap->path, UIO_USERSPACE, uap->flag,
1963 kern_accessat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1964 int flag, int amode)
1966 struct ucred *cred, *usecred;
1968 struct nameidata nd;
1969 cap_rights_t rights;
1972 if (flag & ~AT_EACCESS)
1974 if (amode != F_OK && (amode & ~(R_OK | W_OK | X_OK)) != 0)
1978 * Create and modify a temporary credential instead of one that
1979 * is potentially shared (if we need one).
1981 cred = td->td_ucred;
1982 if ((flag & AT_EACCESS) == 0 &&
1983 ((cred->cr_uid != cred->cr_ruid ||
1984 cred->cr_rgid != cred->cr_groups[0]))) {
1985 usecred = crdup(cred);
1986 usecred->cr_uid = cred->cr_ruid;
1987 usecred->cr_groups[0] = cred->cr_rgid;
1988 td->td_ucred = usecred;
1991 AUDIT_ARG_VALUE(amode);
1992 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF |
1993 AUDITVNODE1, pathseg, path, fd, cap_rights_init(&rights, CAP_FSTAT),
1995 if ((error = namei(&nd)) != 0)
1999 error = vn_access(vp, amode, usecred, td);
2000 NDFREE(&nd, NDF_ONLY_PNBUF);
2003 if (usecred != cred) {
2004 td->td_ucred = cred;
2011 * Check access permissions using "effective" credentials.
2013 #ifndef _SYS_SYSPROTO_H_
2014 struct eaccess_args {
2020 sys_eaccess(struct thread *td, struct eaccess_args *uap)
2023 return (kern_accessat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2024 AT_EACCESS, uap->amode));
2027 #if defined(COMPAT_43)
2029 * Get file status; this version follows links.
2031 #ifndef _SYS_SYSPROTO_H_
2038 ostat(struct thread *td, struct ostat_args *uap)
2044 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
2049 return (copyout(&osb, uap->ub, sizeof (osb)));
2053 * Get file status; this version does not follow links.
2055 #ifndef _SYS_SYSPROTO_H_
2056 struct olstat_args {
2062 olstat(struct thread *td, struct olstat_args *uap)
2068 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2069 UIO_USERSPACE, &sb, NULL);
2073 return (copyout(&osb, uap->ub, sizeof (osb)));
2077 * Convert from an old to a new stat structure.
2080 cvtstat(struct stat *st, struct ostat *ost)
2083 bzero(ost, sizeof(*ost));
2084 ost->st_dev = st->st_dev;
2085 ost->st_ino = st->st_ino;
2086 ost->st_mode = st->st_mode;
2087 ost->st_nlink = st->st_nlink;
2088 ost->st_uid = st->st_uid;
2089 ost->st_gid = st->st_gid;
2090 ost->st_rdev = st->st_rdev;
2091 if (st->st_size < (quad_t)1 << 32)
2092 ost->st_size = st->st_size;
2095 ost->st_atim = st->st_atim;
2096 ost->st_mtim = st->st_mtim;
2097 ost->st_ctim = st->st_ctim;
2098 ost->st_blksize = st->st_blksize;
2099 ost->st_blocks = st->st_blocks;
2100 ost->st_flags = st->st_flags;
2101 ost->st_gen = st->st_gen;
2103 #endif /* COMPAT_43 */
2105 #if defined(COMPAT_43) || defined(COMPAT_FREEBSD11)
2106 int ino64_trunc_error;
2107 SYSCTL_INT(_vfs, OID_AUTO, ino64_trunc_error, CTLFLAG_RW,
2108 &ino64_trunc_error, 0,
2109 "Error on truncation of inode number, device id or link count");
2111 freebsd11_cvtstat(struct stat *st, struct freebsd11_stat *ost)
2114 ost->st_dev = st->st_dev;
2115 ost->st_ino = st->st_ino;
2116 if (ost->st_ino != st->st_ino) {
2117 switch (ino64_trunc_error) {
2124 ost->st_ino = UINT32_MAX;
2128 ost->st_mode = st->st_mode;
2129 ost->st_nlink = st->st_nlink;
2130 if (ost->st_nlink != st->st_nlink) {
2131 switch (ino64_trunc_error) {
2138 ost->st_nlink = UINT16_MAX;
2142 ost->st_uid = st->st_uid;
2143 ost->st_gid = st->st_gid;
2144 ost->st_rdev = st->st_rdev;
2145 ost->st_atim = st->st_atim;
2146 ost->st_mtim = st->st_mtim;
2147 ost->st_ctim = st->st_ctim;
2148 ost->st_size = st->st_size;
2149 ost->st_blocks = st->st_blocks;
2150 ost->st_blksize = st->st_blksize;
2151 ost->st_flags = st->st_flags;
2152 ost->st_gen = st->st_gen;
2154 ost->st_birthtim = st->st_birthtim;
2155 bzero((char *)&ost->st_birthtim + sizeof(ost->st_birthtim),
2156 sizeof(*ost) - offsetof(struct freebsd11_stat,
2157 st_birthtim) - sizeof(ost->st_birthtim));
2162 freebsd11_stat(struct thread *td, struct freebsd11_stat_args* uap)
2165 struct freebsd11_stat osb;
2168 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
2172 error = freebsd11_cvtstat(&sb, &osb);
2174 error = copyout(&osb, uap->ub, sizeof(osb));
2179 freebsd11_lstat(struct thread *td, struct freebsd11_lstat_args* uap)
2182 struct freebsd11_stat osb;
2185 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2186 UIO_USERSPACE, &sb, NULL);
2189 error = freebsd11_cvtstat(&sb, &osb);
2191 error = copyout(&osb, uap->ub, sizeof(osb));
2196 freebsd11_fhstat(struct thread *td, struct freebsd11_fhstat_args* uap)
2200 struct freebsd11_stat osb;
2203 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
2206 error = kern_fhstat(td, fh, &sb);
2209 error = freebsd11_cvtstat(&sb, &osb);
2211 error = copyout(&osb, uap->sb, sizeof(osb));
2216 freebsd11_fstatat(struct thread *td, struct freebsd11_fstatat_args* uap)
2219 struct freebsd11_stat osb;
2222 error = kern_statat(td, uap->flag, uap->fd, uap->path,
2223 UIO_USERSPACE, &sb, NULL);
2226 error = freebsd11_cvtstat(&sb, &osb);
2228 error = copyout(&osb, uap->buf, sizeof(osb));
2231 #endif /* COMPAT_FREEBSD11 */
2236 #ifndef _SYS_SYSPROTO_H_
2237 struct fstatat_args {
2245 sys_fstatat(struct thread *td, struct fstatat_args *uap)
2250 error = kern_statat(td, uap->flag, uap->fd, uap->path,
2251 UIO_USERSPACE, &sb, NULL);
2253 error = copyout(&sb, uap->buf, sizeof (sb));
2258 kern_statat(struct thread *td, int flag, int fd, char *path,
2259 enum uio_seg pathseg, struct stat *sbp,
2260 void (*hook)(struct vnode *vp, struct stat *sbp))
2262 struct nameidata nd;
2264 cap_rights_t rights;
2267 if (flag & ~AT_SYMLINK_NOFOLLOW)
2270 NDINIT_ATRIGHTS(&nd, LOOKUP, ((flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW :
2271 FOLLOW) | LOCKSHARED | LOCKLEAF | AUDITVNODE1, pathseg, path, fd,
2272 cap_rights_init(&rights, CAP_FSTAT), td);
2274 if ((error = namei(&nd)) != 0)
2276 error = vn_stat(nd.ni_vp, &sb, td->td_ucred, NOCRED, td);
2278 SDT_PROBE2(vfs, , stat, mode, path, sb.st_mode);
2279 if (S_ISREG(sb.st_mode))
2280 SDT_PROBE2(vfs, , stat, reg, path, pathseg);
2281 if (__predict_false(hook != NULL))
2282 hook(nd.ni_vp, &sb);
2284 NDFREE(&nd, NDF_ONLY_PNBUF);
2288 #ifdef __STAT_TIME_T_EXT
2296 if (KTRPOINT(td, KTR_STRUCT))
2302 #if defined(COMPAT_FREEBSD11)
2304 * Implementation of the NetBSD [l]stat() functions.
2307 freebsd11_cvtnstat(struct stat *sb, struct nstat *nsb)
2310 bzero(nsb, sizeof(*nsb));
2311 nsb->st_dev = sb->st_dev;
2312 nsb->st_ino = sb->st_ino;
2313 nsb->st_mode = sb->st_mode;
2314 nsb->st_nlink = sb->st_nlink;
2315 nsb->st_uid = sb->st_uid;
2316 nsb->st_gid = sb->st_gid;
2317 nsb->st_rdev = sb->st_rdev;
2318 nsb->st_atim = sb->st_atim;
2319 nsb->st_mtim = sb->st_mtim;
2320 nsb->st_ctim = sb->st_ctim;
2321 nsb->st_size = sb->st_size;
2322 nsb->st_blocks = sb->st_blocks;
2323 nsb->st_blksize = sb->st_blksize;
2324 nsb->st_flags = sb->st_flags;
2325 nsb->st_gen = sb->st_gen;
2326 nsb->st_birthtim = sb->st_birthtim;
2329 #ifndef _SYS_SYSPROTO_H_
2330 struct freebsd11_nstat_args {
2336 freebsd11_nstat(struct thread *td, struct freebsd11_nstat_args *uap)
2342 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
2346 freebsd11_cvtnstat(&sb, &nsb);
2347 return (copyout(&nsb, uap->ub, sizeof (nsb)));
2351 * NetBSD lstat. Get file status; this version does not follow links.
2353 #ifndef _SYS_SYSPROTO_H_
2354 struct freebsd11_nlstat_args {
2360 freebsd11_nlstat(struct thread *td, struct freebsd11_nlstat_args *uap)
2366 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2367 UIO_USERSPACE, &sb, NULL);
2370 freebsd11_cvtnstat(&sb, &nsb);
2371 return (copyout(&nsb, uap->ub, sizeof (nsb)));
2373 #endif /* COMPAT_FREEBSD11 */
2376 * Get configurable pathname variables.
2378 #ifndef _SYS_SYSPROTO_H_
2379 struct pathconf_args {
2385 sys_pathconf(struct thread *td, struct pathconf_args *uap)
2388 return (kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name, FOLLOW));
2391 #ifndef _SYS_SYSPROTO_H_
2392 struct lpathconf_args {
2398 sys_lpathconf(struct thread *td, struct lpathconf_args *uap)
2401 return (kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name,
2406 kern_pathconf(struct thread *td, char *path, enum uio_seg pathseg, int name,
2409 struct nameidata nd;
2412 NDINIT(&nd, LOOKUP, LOCKSHARED | LOCKLEAF | AUDITVNODE1 | flags,
2414 if ((error = namei(&nd)) != 0)
2416 NDFREE(&nd, NDF_ONLY_PNBUF);
2418 error = VOP_PATHCONF(nd.ni_vp, name, td->td_retval);
2424 * Return target name of a symbolic link.
2426 #ifndef _SYS_SYSPROTO_H_
2427 struct readlink_args {
2434 sys_readlink(struct thread *td, struct readlink_args *uap)
2437 return (kern_readlinkat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2438 uap->buf, UIO_USERSPACE, uap->count));
2440 #ifndef _SYS_SYSPROTO_H_
2441 struct readlinkat_args {
2449 sys_readlinkat(struct thread *td, struct readlinkat_args *uap)
2452 return (kern_readlinkat(td, uap->fd, uap->path, UIO_USERSPACE,
2453 uap->buf, UIO_USERSPACE, uap->bufsize));
2457 kern_readlinkat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2458 char *buf, enum uio_seg bufseg, size_t count)
2463 struct nameidata nd;
2466 if (count > IOSIZE_MAX)
2469 NDINIT_AT(&nd, LOOKUP, NOFOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
2470 pathseg, path, fd, td);
2472 if ((error = namei(&nd)) != 0)
2474 NDFREE(&nd, NDF_ONLY_PNBUF);
2477 error = mac_vnode_check_readlink(td->td_ucred, vp);
2483 if (vp->v_type != VLNK && (vp->v_vflag & VV_READLINK) == 0)
2486 aiov.iov_base = buf;
2487 aiov.iov_len = count;
2488 auio.uio_iov = &aiov;
2489 auio.uio_iovcnt = 1;
2490 auio.uio_offset = 0;
2491 auio.uio_rw = UIO_READ;
2492 auio.uio_segflg = bufseg;
2494 auio.uio_resid = count;
2495 error = VOP_READLINK(vp, &auio, td->td_ucred);
2496 td->td_retval[0] = count - auio.uio_resid;
2503 * Common implementation code for chflags() and fchflags().
2506 setfflags(struct thread *td, struct vnode *vp, u_long flags)
2512 /* We can't support the value matching VNOVAL. */
2513 if (flags == VNOVAL)
2514 return (EOPNOTSUPP);
2517 * Prevent non-root users from setting flags on devices. When
2518 * a device is reused, users can retain ownership of the device
2519 * if they are allowed to set flags and programs assume that
2520 * chown can't fail when done as root.
2522 if (vp->v_type == VCHR || vp->v_type == VBLK) {
2523 error = priv_check(td, PRIV_VFS_CHFLAGS_DEV);
2528 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2531 vattr.va_flags = flags;
2532 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2534 error = mac_vnode_check_setflags(td->td_ucred, vp, vattr.va_flags);
2537 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
2539 vn_finished_write(mp);
2544 * Change flags of a file given a path name.
2546 #ifndef _SYS_SYSPROTO_H_
2547 struct chflags_args {
2553 sys_chflags(struct thread *td, struct chflags_args *uap)
2556 return (kern_chflagsat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2560 #ifndef _SYS_SYSPROTO_H_
2561 struct chflagsat_args {
2569 sys_chflagsat(struct thread *td, struct chflagsat_args *uap)
2572 const char *path = uap->path;
2573 u_long flags = uap->flags;
2574 int atflag = uap->atflag;
2576 if (atflag & ~AT_SYMLINK_NOFOLLOW)
2579 return (kern_chflagsat(td, fd, path, UIO_USERSPACE, flags, atflag));
2583 * Same as chflags() but doesn't follow symlinks.
2585 #ifndef _SYS_SYSPROTO_H_
2586 struct lchflags_args {
2592 sys_lchflags(struct thread *td, struct lchflags_args *uap)
2595 return (kern_chflagsat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2596 uap->flags, AT_SYMLINK_NOFOLLOW));
2600 kern_chflagsat(struct thread *td, int fd, const char *path,
2601 enum uio_seg pathseg, u_long flags, int atflag)
2603 struct nameidata nd;
2604 cap_rights_t rights;
2607 AUDIT_ARG_FFLAGS(flags);
2608 follow = (atflag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
2609 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, pathseg, path, fd,
2610 cap_rights_init(&rights, CAP_FCHFLAGS), td);
2611 if ((error = namei(&nd)) != 0)
2613 NDFREE(&nd, NDF_ONLY_PNBUF);
2614 error = setfflags(td, nd.ni_vp, flags);
2620 * Change flags of a file given a file descriptor.
2622 #ifndef _SYS_SYSPROTO_H_
2623 struct fchflags_args {
2629 sys_fchflags(struct thread *td, struct fchflags_args *uap)
2632 cap_rights_t rights;
2635 AUDIT_ARG_FD(uap->fd);
2636 AUDIT_ARG_FFLAGS(uap->flags);
2637 error = getvnode(td, uap->fd, cap_rights_init(&rights, CAP_FCHFLAGS),
2642 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
2643 AUDIT_ARG_VNODE1(fp->f_vnode);
2644 VOP_UNLOCK(fp->f_vnode, 0);
2646 error = setfflags(td, fp->f_vnode, uap->flags);
2652 * Common implementation code for chmod(), lchmod() and fchmod().
2655 setfmode(struct thread *td, struct ucred *cred, struct vnode *vp, int mode)
2661 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2663 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2665 vattr.va_mode = mode & ALLPERMS;
2667 error = mac_vnode_check_setmode(cred, vp, vattr.va_mode);
2670 error = VOP_SETATTR(vp, &vattr, cred);
2672 vn_finished_write(mp);
2677 * Change mode of a file given path name.
2679 #ifndef _SYS_SYSPROTO_H_
2686 sys_chmod(struct thread *td, struct chmod_args *uap)
2689 return (kern_fchmodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2693 #ifndef _SYS_SYSPROTO_H_
2694 struct fchmodat_args {
2702 sys_fchmodat(struct thread *td, struct fchmodat_args *uap)
2704 int flag = uap->flag;
2706 char *path = uap->path;
2707 mode_t mode = uap->mode;
2709 if (flag & ~AT_SYMLINK_NOFOLLOW)
2712 return (kern_fchmodat(td, fd, path, UIO_USERSPACE, mode, flag));
2716 * Change mode of a file given path name (don't follow links.)
2718 #ifndef _SYS_SYSPROTO_H_
2719 struct lchmod_args {
2725 sys_lchmod(struct thread *td, struct lchmod_args *uap)
2728 return (kern_fchmodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2729 uap->mode, AT_SYMLINK_NOFOLLOW));
2733 kern_fchmodat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2734 mode_t mode, int flag)
2736 struct nameidata nd;
2737 cap_rights_t rights;
2740 AUDIT_ARG_MODE(mode);
2741 follow = (flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
2742 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, pathseg, path, fd,
2743 cap_rights_init(&rights, CAP_FCHMOD), td);
2744 if ((error = namei(&nd)) != 0)
2746 NDFREE(&nd, NDF_ONLY_PNBUF);
2747 error = setfmode(td, td->td_ucred, nd.ni_vp, mode);
2753 * Change mode of a file given a file descriptor.
2755 #ifndef _SYS_SYSPROTO_H_
2756 struct fchmod_args {
2762 sys_fchmod(struct thread *td, struct fchmod_args *uap)
2765 cap_rights_t rights;
2768 AUDIT_ARG_FD(uap->fd);
2769 AUDIT_ARG_MODE(uap->mode);
2771 error = fget(td, uap->fd, cap_rights_init(&rights, CAP_FCHMOD), &fp);
2774 error = fo_chmod(fp, uap->mode, td->td_ucred, td);
2780 * Common implementation for chown(), lchown(), and fchown()
2783 setfown(struct thread *td, struct ucred *cred, struct vnode *vp, uid_t uid,
2790 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2792 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2797 error = mac_vnode_check_setowner(cred, vp, vattr.va_uid,
2801 error = VOP_SETATTR(vp, &vattr, cred);
2803 vn_finished_write(mp);
2808 * Set ownership given a path name.
2810 #ifndef _SYS_SYSPROTO_H_
2818 sys_chown(struct thread *td, struct chown_args *uap)
2821 return (kern_fchownat(td, AT_FDCWD, uap->path, UIO_USERSPACE, uap->uid,
2825 #ifndef _SYS_SYSPROTO_H_
2826 struct fchownat_args {
2835 sys_fchownat(struct thread *td, struct fchownat_args *uap)
2840 if (flag & ~AT_SYMLINK_NOFOLLOW)
2843 return (kern_fchownat(td, uap->fd, uap->path, UIO_USERSPACE, uap->uid,
2844 uap->gid, uap->flag));
2848 kern_fchownat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2849 int uid, int gid, int flag)
2851 struct nameidata nd;
2852 cap_rights_t rights;
2855 AUDIT_ARG_OWNER(uid, gid);
2856 follow = (flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
2857 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, pathseg, path, fd,
2858 cap_rights_init(&rights, CAP_FCHOWN), td);
2860 if ((error = namei(&nd)) != 0)
2862 NDFREE(&nd, NDF_ONLY_PNBUF);
2863 error = setfown(td, td->td_ucred, nd.ni_vp, uid, gid);
2869 * Set ownership given a path name, do not cross symlinks.
2871 #ifndef _SYS_SYSPROTO_H_
2872 struct lchown_args {
2879 sys_lchown(struct thread *td, struct lchown_args *uap)
2882 return (kern_fchownat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2883 uap->uid, uap->gid, AT_SYMLINK_NOFOLLOW));
2887 * Set ownership given a file descriptor.
2889 #ifndef _SYS_SYSPROTO_H_
2890 struct fchown_args {
2897 sys_fchown(struct thread *td, struct fchown_args *uap)
2900 cap_rights_t rights;
2903 AUDIT_ARG_FD(uap->fd);
2904 AUDIT_ARG_OWNER(uap->uid, uap->gid);
2905 error = fget(td, uap->fd, cap_rights_init(&rights, CAP_FCHOWN), &fp);
2908 error = fo_chown(fp, uap->uid, uap->gid, td->td_ucred, td);
2914 * Common implementation code for utimes(), lutimes(), and futimes().
2917 getutimes(const struct timeval *usrtvp, enum uio_seg tvpseg,
2918 struct timespec *tsp)
2920 struct timeval tv[2];
2921 const struct timeval *tvp;
2924 if (usrtvp == NULL) {
2925 vfs_timestamp(&tsp[0]);
2928 if (tvpseg == UIO_SYSSPACE) {
2931 if ((error = copyin(usrtvp, tv, sizeof(tv))) != 0)
2936 if (tvp[0].tv_usec < 0 || tvp[0].tv_usec >= 1000000 ||
2937 tvp[1].tv_usec < 0 || tvp[1].tv_usec >= 1000000)
2939 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
2940 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
2946 * Common implementation code for futimens(), utimensat().
2948 #define UTIMENS_NULL 0x1
2949 #define UTIMENS_EXIT 0x2
2951 getutimens(const struct timespec *usrtsp, enum uio_seg tspseg,
2952 struct timespec *tsp, int *retflags)
2954 struct timespec tsnow;
2957 vfs_timestamp(&tsnow);
2959 if (usrtsp == NULL) {
2962 *retflags |= UTIMENS_NULL;
2965 if (tspseg == UIO_SYSSPACE) {
2968 } else if ((error = copyin(usrtsp, tsp, sizeof(*tsp) * 2)) != 0)
2970 if (tsp[0].tv_nsec == UTIME_OMIT && tsp[1].tv_nsec == UTIME_OMIT)
2971 *retflags |= UTIMENS_EXIT;
2972 if (tsp[0].tv_nsec == UTIME_NOW && tsp[1].tv_nsec == UTIME_NOW)
2973 *retflags |= UTIMENS_NULL;
2974 if (tsp[0].tv_nsec == UTIME_OMIT)
2975 tsp[0].tv_sec = VNOVAL;
2976 else if (tsp[0].tv_nsec == UTIME_NOW)
2978 else if (tsp[0].tv_nsec < 0 || tsp[0].tv_nsec >= 1000000000L)
2980 if (tsp[1].tv_nsec == UTIME_OMIT)
2981 tsp[1].tv_sec = VNOVAL;
2982 else if (tsp[1].tv_nsec == UTIME_NOW)
2984 else if (tsp[1].tv_nsec < 0 || tsp[1].tv_nsec >= 1000000000L)
2991 * Common implementation code for utimes(), lutimes(), futimes(), futimens(),
2995 setutimes(struct thread *td, struct vnode *vp, const struct timespec *ts,
2996 int numtimes, int nullflag)
3000 int error, setbirthtime;
3002 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
3004 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3006 if (numtimes < 3 && !VOP_GETATTR(vp, &vattr, td->td_ucred) &&
3007 timespeccmp(&ts[1], &vattr.va_birthtime, < ))
3010 vattr.va_atime = ts[0];
3011 vattr.va_mtime = ts[1];
3013 vattr.va_birthtime = ts[1];
3015 vattr.va_birthtime = ts[2];
3017 vattr.va_vaflags |= VA_UTIMES_NULL;
3019 error = mac_vnode_check_setutimes(td->td_ucred, vp, vattr.va_atime,
3023 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3025 vn_finished_write(mp);
3030 * Set the access and modification times of a file.
3032 #ifndef _SYS_SYSPROTO_H_
3033 struct utimes_args {
3035 struct timeval *tptr;
3039 sys_utimes(struct thread *td, struct utimes_args *uap)
3042 return (kern_utimesat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
3043 uap->tptr, UIO_USERSPACE));
3046 #ifndef _SYS_SYSPROTO_H_
3047 struct futimesat_args {
3050 const struct timeval * times;
3054 sys_futimesat(struct thread *td, struct futimesat_args *uap)
3057 return (kern_utimesat(td, uap->fd, uap->path, UIO_USERSPACE,
3058 uap->times, UIO_USERSPACE));
3062 kern_utimesat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
3063 struct timeval *tptr, enum uio_seg tptrseg)
3065 struct nameidata nd;
3066 struct timespec ts[2];
3067 cap_rights_t rights;
3070 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3072 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, fd,
3073 cap_rights_init(&rights, CAP_FUTIMES), td);
3075 if ((error = namei(&nd)) != 0)
3077 NDFREE(&nd, NDF_ONLY_PNBUF);
3078 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3084 * Set the access and modification times of a file.
3086 #ifndef _SYS_SYSPROTO_H_
3087 struct lutimes_args {
3089 struct timeval *tptr;
3093 sys_lutimes(struct thread *td, struct lutimes_args *uap)
3096 return (kern_lutimes(td, uap->path, UIO_USERSPACE, uap->tptr,
3101 kern_lutimes(struct thread *td, char *path, enum uio_seg pathseg,
3102 struct timeval *tptr, enum uio_seg tptrseg)
3104 struct timespec ts[2];
3105 struct nameidata nd;
3108 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3110 NDINIT(&nd, LOOKUP, NOFOLLOW | AUDITVNODE1, pathseg, path, td);
3111 if ((error = namei(&nd)) != 0)
3113 NDFREE(&nd, NDF_ONLY_PNBUF);
3114 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3120 * Set the access and modification times of a file.
3122 #ifndef _SYS_SYSPROTO_H_
3123 struct futimes_args {
3125 struct timeval *tptr;
3129 sys_futimes(struct thread *td, struct futimes_args *uap)
3132 return (kern_futimes(td, uap->fd, uap->tptr, UIO_USERSPACE));
3136 kern_futimes(struct thread *td, int fd, struct timeval *tptr,
3137 enum uio_seg tptrseg)
3139 struct timespec ts[2];
3141 cap_rights_t rights;
3145 error = getutimes(tptr, tptrseg, ts);
3148 error = getvnode(td, fd, cap_rights_init(&rights, CAP_FUTIMES), &fp);
3152 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
3153 AUDIT_ARG_VNODE1(fp->f_vnode);
3154 VOP_UNLOCK(fp->f_vnode, 0);
3156 error = setutimes(td, fp->f_vnode, ts, 2, tptr == NULL);
3162 sys_futimens(struct thread *td, struct futimens_args *uap)
3165 return (kern_futimens(td, uap->fd, uap->times, UIO_USERSPACE));
3169 kern_futimens(struct thread *td, int fd, struct timespec *tptr,
3170 enum uio_seg tptrseg)
3172 struct timespec ts[2];
3174 cap_rights_t rights;
3178 error = getutimens(tptr, tptrseg, ts, &flags);
3181 if (flags & UTIMENS_EXIT)
3183 error = getvnode(td, fd, cap_rights_init(&rights, CAP_FUTIMES), &fp);
3187 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
3188 AUDIT_ARG_VNODE1(fp->f_vnode);
3189 VOP_UNLOCK(fp->f_vnode, 0);
3191 error = setutimes(td, fp->f_vnode, ts, 2, flags & UTIMENS_NULL);
3197 sys_utimensat(struct thread *td, struct utimensat_args *uap)
3200 return (kern_utimensat(td, uap->fd, uap->path, UIO_USERSPACE,
3201 uap->times, UIO_USERSPACE, uap->flag));
3205 kern_utimensat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
3206 struct timespec *tptr, enum uio_seg tptrseg, int flag)
3208 struct nameidata nd;
3209 struct timespec ts[2];
3210 cap_rights_t rights;
3213 if (flag & ~AT_SYMLINK_NOFOLLOW)
3216 if ((error = getutimens(tptr, tptrseg, ts, &flags)) != 0)
3218 NDINIT_ATRIGHTS(&nd, LOOKUP, ((flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW :
3219 FOLLOW) | AUDITVNODE1, pathseg, path, fd,
3220 cap_rights_init(&rights, CAP_FUTIMES), td);
3221 if ((error = namei(&nd)) != 0)
3224 * We are allowed to call namei() regardless of 2xUTIME_OMIT.
3226 * "If both tv_nsec fields are UTIME_OMIT... EACCESS may be detected."
3227 * "Search permission is denied by a component of the path prefix."
3229 NDFREE(&nd, NDF_ONLY_PNBUF);
3230 if ((flags & UTIMENS_EXIT) == 0)
3231 error = setutimes(td, nd.ni_vp, ts, 2, flags & UTIMENS_NULL);
3237 * Truncate a file given its path name.
3239 #ifndef _SYS_SYSPROTO_H_
3240 struct truncate_args {
3247 sys_truncate(struct thread *td, struct truncate_args *uap)
3250 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3254 kern_truncate(struct thread *td, char *path, enum uio_seg pathseg, off_t length)
3260 struct nameidata nd;
3265 NDINIT(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, td);
3266 if ((error = namei(&nd)) != 0)
3269 rl_cookie = vn_rangelock_wlock(vp, 0, OFF_MAX);
3270 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0) {
3271 vn_rangelock_unlock(vp, rl_cookie);
3275 NDFREE(&nd, NDF_ONLY_PNBUF);
3276 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3277 if (vp->v_type == VDIR)
3280 else if ((error = mac_vnode_check_write(td->td_ucred, NOCRED, vp))) {
3283 else if ((error = vn_writechk(vp)) == 0 &&
3284 (error = VOP_ACCESS(vp, VWRITE, td->td_ucred, td)) == 0) {
3286 vattr.va_size = length;
3287 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3290 vn_finished_write(mp);
3291 vn_rangelock_unlock(vp, rl_cookie);
3296 #if defined(COMPAT_43)
3298 * Truncate a file given its path name.
3300 #ifndef _SYS_SYSPROTO_H_
3301 struct otruncate_args {
3307 otruncate(struct thread *td, struct otruncate_args *uap)
3310 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3312 #endif /* COMPAT_43 */
3314 #if defined(COMPAT_FREEBSD6)
3315 /* Versions with the pad argument */
3317 freebsd6_truncate(struct thread *td, struct freebsd6_truncate_args *uap)
3320 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3324 freebsd6_ftruncate(struct thread *td, struct freebsd6_ftruncate_args *uap)
3327 return (kern_ftruncate(td, uap->fd, uap->length));
3332 kern_fsync(struct thread *td, int fd, bool fullsync)
3337 cap_rights_t rights;
3338 int error, lock_flags;
3341 error = getvnode(td, fd, cap_rights_init(&rights, CAP_FSYNC), &fp);
3347 /* XXXKIB: compete outstanding aio writes */;
3349 error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
3352 if (MNT_SHARED_WRITES(mp) ||
3353 ((mp == NULL) && MNT_SHARED_WRITES(vp->v_mount))) {
3354 lock_flags = LK_SHARED;
3356 lock_flags = LK_EXCLUSIVE;
3358 vn_lock(vp, lock_flags | LK_RETRY);
3359 AUDIT_ARG_VNODE1(vp);
3360 if (vp->v_object != NULL) {
3361 VM_OBJECT_WLOCK(vp->v_object);
3362 vm_object_page_clean(vp->v_object, 0, 0, 0);
3363 VM_OBJECT_WUNLOCK(vp->v_object);
3365 error = fullsync ? VOP_FSYNC(vp, MNT_WAIT, td) : VOP_FDATASYNC(vp, td);
3367 vn_finished_write(mp);
3374 * Sync an open file.
3376 #ifndef _SYS_SYSPROTO_H_
3382 sys_fsync(struct thread *td, struct fsync_args *uap)
3385 return (kern_fsync(td, uap->fd, true));
3389 sys_fdatasync(struct thread *td, struct fdatasync_args *uap)
3392 return (kern_fsync(td, uap->fd, false));
3396 * Rename files. Source and destination must either both be directories, or
3397 * both not be directories. If target is a directory, it must be empty.
3399 #ifndef _SYS_SYSPROTO_H_
3400 struct rename_args {
3406 sys_rename(struct thread *td, struct rename_args *uap)
3409 return (kern_renameat(td, AT_FDCWD, uap->from, AT_FDCWD,
3410 uap->to, UIO_USERSPACE));
3413 #ifndef _SYS_SYSPROTO_H_
3414 struct renameat_args {
3422 sys_renameat(struct thread *td, struct renameat_args *uap)
3425 return (kern_renameat(td, uap->oldfd, uap->old, uap->newfd, uap->new,
3430 kern_renameat(struct thread *td, int oldfd, char *old, int newfd, char *new,
3431 enum uio_seg pathseg)
3433 struct mount *mp = NULL;
3434 struct vnode *tvp, *fvp, *tdvp;
3435 struct nameidata fromnd, tond;
3436 cap_rights_t rights;
3442 NDINIT_ATRIGHTS(&fromnd, DELETE, LOCKPARENT | LOCKLEAF | SAVESTART |
3443 AUDITVNODE1, pathseg, old, oldfd,
3444 cap_rights_init(&rights, CAP_RENAMEAT_SOURCE), td);
3446 NDINIT_ATRIGHTS(&fromnd, DELETE, WANTPARENT | SAVESTART | AUDITVNODE1,
3447 pathseg, old, oldfd,
3448 cap_rights_init(&rights, CAP_RENAMEAT_SOURCE), td);
3451 if ((error = namei(&fromnd)) != 0)
3454 error = mac_vnode_check_rename_from(td->td_ucred, fromnd.ni_dvp,
3455 fromnd.ni_vp, &fromnd.ni_cnd);
3456 VOP_UNLOCK(fromnd.ni_dvp, 0);
3457 if (fromnd.ni_dvp != fromnd.ni_vp)
3458 VOP_UNLOCK(fromnd.ni_vp, 0);
3461 NDINIT_ATRIGHTS(&tond, RENAME, LOCKPARENT | LOCKLEAF | NOCACHE |
3462 SAVESTART | AUDITVNODE2, pathseg, new, newfd,
3463 cap_rights_init(&rights, CAP_RENAMEAT_TARGET), td);
3464 if (fromnd.ni_vp->v_type == VDIR)
3465 tond.ni_cnd.cn_flags |= WILLBEDIR;
3466 if ((error = namei(&tond)) != 0) {
3467 /* Translate error code for rename("dir1", "dir2/."). */
3468 if (error == EISDIR && fvp->v_type == VDIR)
3470 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3471 vrele(fromnd.ni_dvp);
3477 error = vn_start_write(fvp, &mp, V_NOWAIT);
3479 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3480 NDFREE(&tond, NDF_ONLY_PNBUF);
3487 vrele(fromnd.ni_dvp);
3489 vrele(tond.ni_startdir);
3490 if (fromnd.ni_startdir != NULL)
3491 vrele(fromnd.ni_startdir);
3492 error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH);
3498 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
3501 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
3506 if (newfd != AT_FDCWD) {
3508 * If the target already exists we require CAP_UNLINKAT
3511 error = cap_check(&tond.ni_filecaps.fc_rights,
3512 cap_rights_init(&rights, CAP_UNLINKAT));
3523 * If the source is the same as the destination (that is, if they
3524 * are links to the same vnode), then there is nothing to do.
3530 error = mac_vnode_check_rename_to(td->td_ucred, tdvp,
3531 tond.ni_vp, fromnd.ni_dvp == tdvp, &tond.ni_cnd);
3535 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
3536 tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
3537 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3538 NDFREE(&tond, NDF_ONLY_PNBUF);
3540 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3541 NDFREE(&tond, NDF_ONLY_PNBUF);
3548 vrele(fromnd.ni_dvp);
3551 vrele(tond.ni_startdir);
3552 vn_finished_write(mp);
3554 if (fromnd.ni_startdir)
3555 vrele(fromnd.ni_startdir);
3562 * Make a directory file.
3564 #ifndef _SYS_SYSPROTO_H_
3571 sys_mkdir(struct thread *td, struct mkdir_args *uap)
3574 return (kern_mkdirat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
3578 #ifndef _SYS_SYSPROTO_H_
3579 struct mkdirat_args {
3586 sys_mkdirat(struct thread *td, struct mkdirat_args *uap)
3589 return (kern_mkdirat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode));
3593 kern_mkdirat(struct thread *td, int fd, char *path, enum uio_seg segflg,
3599 struct nameidata nd;
3600 cap_rights_t rights;
3603 AUDIT_ARG_MODE(mode);
3606 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
3607 NOCACHE, segflg, path, fd, cap_rights_init(&rights, CAP_MKDIRAT),
3609 nd.ni_cnd.cn_flags |= WILLBEDIR;
3610 if ((error = namei(&nd)) != 0)
3614 NDFREE(&nd, NDF_ONLY_PNBUF);
3616 * XXX namei called with LOCKPARENT but not LOCKLEAF has
3617 * the strange behaviour of leaving the vnode unlocked
3618 * if the target is the same vnode as the parent.
3620 if (vp == nd.ni_dvp)
3627 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3628 NDFREE(&nd, NDF_ONLY_PNBUF);
3630 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3635 vattr.va_type = VDIR;
3636 vattr.va_mode = (mode & ACCESSPERMS) &~ td->td_proc->p_fd->fd_cmask;
3638 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
3643 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
3647 NDFREE(&nd, NDF_ONLY_PNBUF);
3651 vn_finished_write(mp);
3656 * Remove a directory file.
3658 #ifndef _SYS_SYSPROTO_H_
3664 sys_rmdir(struct thread *td, struct rmdir_args *uap)
3667 return (kern_rmdirat(td, AT_FDCWD, uap->path, UIO_USERSPACE));
3671 kern_rmdirat(struct thread *td, int fd, char *path, enum uio_seg pathseg)
3675 struct nameidata nd;
3676 cap_rights_t rights;
3681 NDINIT_ATRIGHTS(&nd, DELETE, LOCKPARENT | LOCKLEAF | AUDITVNODE1,
3682 pathseg, path, fd, cap_rights_init(&rights, CAP_UNLINKAT), td);
3683 if ((error = namei(&nd)) != 0)
3686 if (vp->v_type != VDIR) {
3691 * No rmdir "." please.
3693 if (nd.ni_dvp == vp) {
3698 * The root of a mounted filesystem cannot be deleted.
3700 if (vp->v_vflag & VV_ROOT) {
3705 error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp,
3710 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3711 NDFREE(&nd, NDF_ONLY_PNBUF);
3713 if (nd.ni_dvp == vp)
3717 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3721 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_UNLINK);
3722 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
3723 vn_finished_write(mp);
3725 NDFREE(&nd, NDF_ONLY_PNBUF);
3727 if (nd.ni_dvp == vp)
3734 #if defined(COMPAT_43) || defined(COMPAT_FREEBSD11)
3736 freebsd11_kern_getdirentries(struct thread *td, int fd, char *ubuf, u_int count,
3737 long *basep, void (*func)(struct freebsd11_dirent *))
3739 struct freebsd11_dirent dstdp;
3740 struct dirent *dp, *edp;
3743 ssize_t resid, ucount;
3746 /* XXX arbitrary sanity limit on `count'. */
3747 count = min(count, 64 * 1024);
3749 dirbuf = malloc(count, M_TEMP, M_WAITOK);
3751 error = kern_getdirentries(td, fd, dirbuf, count, &base, &resid,
3759 for (dp = (struct dirent *)dirbuf,
3760 edp = (struct dirent *)&dirbuf[count - resid];
3761 ucount < count && dp < edp; ) {
3762 if (dp->d_reclen == 0)
3764 MPASS(dp->d_reclen >= _GENERIC_DIRLEN(0));
3765 if (dp->d_namlen >= sizeof(dstdp.d_name))
3767 dstdp.d_type = dp->d_type;
3768 dstdp.d_namlen = dp->d_namlen;
3769 dstdp.d_fileno = dp->d_fileno; /* truncate */
3770 if (dstdp.d_fileno != dp->d_fileno) {
3771 switch (ino64_trunc_error) {
3779 dstdp.d_fileno = UINT32_MAX;
3783 dstdp.d_reclen = sizeof(dstdp) - sizeof(dstdp.d_name) +
3784 ((dp->d_namlen + 1 + 3) &~ 3);
3785 bcopy(dp->d_name, dstdp.d_name, dstdp.d_namlen);
3786 bzero(dstdp.d_name + dstdp.d_namlen,
3787 dstdp.d_reclen - offsetof(struct freebsd11_dirent, d_name) -
3789 MPASS(dstdp.d_reclen <= dp->d_reclen);
3790 MPASS(ucount + dstdp.d_reclen <= count);
3793 error = copyout(&dstdp, ubuf + ucount, dstdp.d_reclen);
3796 dp = (struct dirent *)((char *)dp + dp->d_reclen);
3797 ucount += dstdp.d_reclen;
3801 free(dirbuf, M_TEMP);
3803 td->td_retval[0] = ucount;
3810 ogetdirentries_cvt(struct freebsd11_dirent *dp)
3812 #if (BYTE_ORDER == LITTLE_ENDIAN)
3814 * The expected low byte of dp->d_namlen is our dp->d_type.
3815 * The high MBZ byte of dp->d_namlen is our dp->d_namlen.
3817 dp->d_type = dp->d_namlen;
3821 * The dp->d_type is the high byte of the expected dp->d_namlen,
3822 * so must be zero'ed.
3829 * Read a block of directory entries in a filesystem independent format.
3831 #ifndef _SYS_SYSPROTO_H_
3832 struct ogetdirentries_args {
3840 ogetdirentries(struct thread *td, struct ogetdirentries_args *uap)
3845 error = kern_ogetdirentries(td, uap, &loff);
3847 error = copyout(&loff, uap->basep, sizeof(long));
3852 kern_ogetdirentries(struct thread *td, struct ogetdirentries_args *uap,
3858 /* XXX arbitrary sanity limit on `count'. */
3859 if (uap->count > 64 * 1024)
3862 error = freebsd11_kern_getdirentries(td, uap->fd, uap->buf, uap->count,
3863 &base, ogetdirentries_cvt);
3865 if (error == 0 && uap->basep != NULL)
3866 error = copyout(&base, uap->basep, sizeof(long));
3870 #endif /* COMPAT_43 */
3872 #if defined(COMPAT_FREEBSD11)
3873 #ifndef _SYS_SYSPROTO_H_
3874 struct freebsd11_getdirentries_args {
3882 freebsd11_getdirentries(struct thread *td,
3883 struct freebsd11_getdirentries_args *uap)
3888 error = freebsd11_kern_getdirentries(td, uap->fd, uap->buf, uap->count,
3891 if (error == 0 && uap->basep != NULL)
3892 error = copyout(&base, uap->basep, sizeof(long));
3897 freebsd11_getdents(struct thread *td, struct freebsd11_getdents_args *uap)
3899 struct freebsd11_getdirentries_args ap;
3903 ap.count = uap->count;
3905 return (freebsd11_getdirentries(td, &ap));
3907 #endif /* COMPAT_FREEBSD11 */
3910 * Read a block of directory entries in a filesystem independent format.
3913 sys_getdirentries(struct thread *td, struct getdirentries_args *uap)
3918 error = kern_getdirentries(td, uap->fd, uap->buf, uap->count, &base,
3919 NULL, UIO_USERSPACE);
3922 if (uap->basep != NULL)
3923 error = copyout(&base, uap->basep, sizeof(off_t));
3928 kern_getdirentries(struct thread *td, int fd, char *buf, size_t count,
3929 off_t *basep, ssize_t *residp, enum uio_seg bufseg)
3935 cap_rights_t rights;
3941 if (count > IOSIZE_MAX)
3943 auio.uio_resid = count;
3944 error = getvnode(td, fd, cap_rights_init(&rights, CAP_READ), &fp);
3947 if ((fp->f_flag & FREAD) == 0) {
3952 foffset = foffset_lock(fp, 0);
3954 if (vp->v_type != VDIR) {
3958 aiov.iov_base = buf;
3959 aiov.iov_len = count;
3960 auio.uio_iov = &aiov;
3961 auio.uio_iovcnt = 1;
3962 auio.uio_rw = UIO_READ;
3963 auio.uio_segflg = bufseg;
3965 vn_lock(vp, LK_SHARED | LK_RETRY);
3966 AUDIT_ARG_VNODE1(vp);
3967 loff = auio.uio_offset = foffset;
3969 error = mac_vnode_check_readdir(td->td_ucred, vp);
3972 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL,
3974 foffset = auio.uio_offset;
3979 if (count == auio.uio_resid &&
3980 (vp->v_vflag & VV_ROOT) &&
3981 (vp->v_mount->mnt_flag & MNT_UNION)) {
3982 struct vnode *tvp = vp;
3984 vp = vp->v_mount->mnt_vnodecovered;
3995 *residp = auio.uio_resid;
3996 td->td_retval[0] = count - auio.uio_resid;
3998 foffset_unlock(fp, foffset, 0);
4004 * Set the mode mask for creation of filesystem nodes.
4006 #ifndef _SYS_SYSPROTO_H_
4012 sys_umask(struct thread *td, struct umask_args *uap)
4014 struct filedesc *fdp;
4016 fdp = td->td_proc->p_fd;
4017 FILEDESC_XLOCK(fdp);
4018 td->td_retval[0] = fdp->fd_cmask;
4019 fdp->fd_cmask = uap->newmask & ALLPERMS;
4020 FILEDESC_XUNLOCK(fdp);
4025 * Void all references to file by ripping underlying filesystem away from
4028 #ifndef _SYS_SYSPROTO_H_
4029 struct revoke_args {
4034 sys_revoke(struct thread *td, struct revoke_args *uap)
4038 struct nameidata nd;
4041 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
4043 if ((error = namei(&nd)) != 0)
4046 NDFREE(&nd, NDF_ONLY_PNBUF);
4047 if (vp->v_type != VCHR || vp->v_rdev == NULL) {
4052 error = mac_vnode_check_revoke(td->td_ucred, vp);
4056 error = VOP_GETATTR(vp, &vattr, td->td_ucred);
4059 if (td->td_ucred->cr_uid != vattr.va_uid) {
4060 error = priv_check(td, PRIV_VFS_ADMIN);
4065 VOP_REVOKE(vp, REVOKEALL);
4072 * Convert a user file descriptor to a kernel file entry and check that, if it
4073 * is a capability, the correct rights are present. A reference on the file
4074 * entry is held upon returning.
4077 getvnode(struct thread *td, int fd, cap_rights_t *rightsp, struct file **fpp)
4082 error = fget_unlocked(td->td_proc->p_fd, fd, rightsp, &fp, NULL);
4087 * The file could be not of the vnode type, or it may be not
4088 * yet fully initialized, in which case the f_vnode pointer
4089 * may be set, but f_ops is still badfileops. E.g.,
4090 * devfs_open() transiently create such situation to
4091 * facilitate csw d_fdopen().
4093 * Dupfdopen() handling in kern_openat() installs the
4094 * half-baked file into the process descriptor table, allowing
4095 * other thread to dereference it. Guard against the race by
4098 if (fp->f_vnode == NULL || fp->f_ops == &badfileops) {
4108 * Get an (NFS) file handle.
4110 #ifndef _SYS_SYSPROTO_H_
4111 struct lgetfh_args {
4117 sys_lgetfh(struct thread *td, struct lgetfh_args *uap)
4119 struct nameidata nd;
4124 error = priv_check(td, PRIV_VFS_GETFH);
4127 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
4132 NDFREE(&nd, NDF_ONLY_PNBUF);
4134 bzero(&fh, sizeof(fh));
4135 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
4136 error = VOP_VPTOFH(vp, &fh.fh_fid);
4139 error = copyout(&fh, uap->fhp, sizeof (fh));
4143 #ifndef _SYS_SYSPROTO_H_
4150 sys_getfh(struct thread *td, struct getfh_args *uap)
4152 struct nameidata nd;
4157 error = priv_check(td, PRIV_VFS_GETFH);
4160 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
4165 NDFREE(&nd, NDF_ONLY_PNBUF);
4167 bzero(&fh, sizeof(fh));
4168 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
4169 error = VOP_VPTOFH(vp, &fh.fh_fid);
4172 error = copyout(&fh, uap->fhp, sizeof (fh));
4177 * syscall for the rpc.lockd to use to translate a NFS file handle into an
4180 * warning: do not remove the priv_check() call or this becomes one giant
4183 #ifndef _SYS_SYSPROTO_H_
4184 struct fhopen_args {
4185 const struct fhandle *u_fhp;
4190 sys_fhopen(struct thread *td, struct fhopen_args *uap)
4199 error = priv_check(td, PRIV_VFS_FHOPEN);
4203 fmode = FFLAGS(uap->flags);
4204 /* why not allow a non-read/write open for our lockd? */
4205 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
4207 error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
4210 /* find the mount point */
4211 mp = vfs_busyfs(&fhp.fh_fsid);
4214 /* now give me my vnode, it gets returned to me locked */
4215 error = VFS_FHTOVP(mp, &fhp.fh_fid, LK_EXCLUSIVE, &vp);
4220 error = falloc_noinstall(td, &fp);
4226 * An extra reference on `fp' has been held for us by
4227 * falloc_noinstall().
4233 error = vn_open_vnode(vp, fmode, td->td_ucred, td, fp);
4235 KASSERT(fp->f_ops == &badfileops,
4236 ("VOP_OPEN in fhopen() set f_ops"));
4237 KASSERT(td->td_dupfd < 0,
4238 ("fhopen() encountered fdopen()"));
4248 finit(fp, (fmode & FMASK) | (fp->f_flag & FHASLOCK), DTYPE_VNODE, vp,
4251 if ((fmode & O_TRUNC) != 0) {
4252 error = fo_truncate(fp, 0, td->td_ucred, td);
4257 error = finstall(td, fp, &indx, fmode, NULL);
4260 td->td_retval[0] = indx;
4265 * Stat an (NFS) file handle.
4267 #ifndef _SYS_SYSPROTO_H_
4268 struct fhstat_args {
4269 struct fhandle *u_fhp;
4274 sys_fhstat(struct thread *td, struct fhstat_args *uap)
4280 error = copyin(uap->u_fhp, &fh, sizeof(fh));
4283 error = kern_fhstat(td, fh, &sb);
4285 error = copyout(&sb, uap->sb, sizeof(sb));
4290 kern_fhstat(struct thread *td, struct fhandle fh, struct stat *sb)
4296 error = priv_check(td, PRIV_VFS_FHSTAT);
4299 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4301 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_EXCLUSIVE, &vp);
4305 error = vn_stat(vp, sb, td->td_ucred, NOCRED, td);
4311 * Implement fstatfs() for (NFS) file handles.
4313 #ifndef _SYS_SYSPROTO_H_
4314 struct fhstatfs_args {
4315 struct fhandle *u_fhp;
4320 sys_fhstatfs(struct thread *td, struct fhstatfs_args *uap)
4326 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4329 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
4330 error = kern_fhstatfs(td, fh, sfp);
4332 error = copyout(sfp, uap->buf, sizeof(*sfp));
4333 free(sfp, M_STATFS);
4338 kern_fhstatfs(struct thread *td, fhandle_t fh, struct statfs *buf)
4345 error = priv_check(td, PRIV_VFS_FHSTATFS);
4348 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4350 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_EXCLUSIVE, &vp);
4356 error = prison_canseemount(td->td_ucred, mp);
4360 error = mac_mount_check_stat(td->td_ucred, mp);
4365 * Set these in case the underlying filesystem fails to do so.
4368 sp->f_version = STATFS_VERSION;
4369 sp->f_namemax = NAME_MAX;
4370 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
4371 error = VFS_STATFS(mp, sp);
4380 kern_posix_fallocate(struct thread *td, int fd, off_t offset, off_t len)
4385 cap_rights_t rights;
4386 off_t olen, ooffset;
4389 int audited_vnode1 = 0;
4393 if (offset < 0 || len <= 0)
4395 /* Check for wrap. */
4396 if (offset > OFF_MAX - len)
4399 error = fget(td, fd, cap_rights_init(&rights, CAP_PWRITE), &fp);
4402 AUDIT_ARG_FILE(td->td_proc, fp);
4403 if ((fp->f_ops->fo_flags & DFLAG_SEEKABLE) == 0) {
4407 if ((fp->f_flag & FWRITE) == 0) {
4411 if (fp->f_type != DTYPE_VNODE) {
4416 if (vp->v_type != VREG) {
4421 /* Allocating blocks may take a long time, so iterate. */
4428 error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
4431 error = vn_lock(vp, LK_EXCLUSIVE);
4433 vn_finished_write(mp);
4437 if (!audited_vnode1) {
4438 AUDIT_ARG_VNODE1(vp);
4443 error = mac_vnode_check_write(td->td_ucred, fp->f_cred, vp);
4446 error = VOP_ALLOCATE(vp, &offset, &len);
4448 vn_finished_write(mp);
4450 if (olen + ooffset != offset + len) {
4451 panic("offset + len changed from %jx/%jx to %jx/%jx",
4452 ooffset, olen, offset, len);
4454 if (error != 0 || len == 0)
4456 KASSERT(olen > len, ("Iteration did not make progress?"));
4465 sys_posix_fallocate(struct thread *td, struct posix_fallocate_args *uap)
4469 error = kern_posix_fallocate(td, uap->fd, uap->offset, uap->len);
4470 return (kern_posix_error(td, error));
4474 * Unlike madvise(2), we do not make a best effort to remember every
4475 * possible caching hint. Instead, we remember the last setting with
4476 * the exception that we will allow POSIX_FADV_NORMAL to adjust the
4477 * region of any current setting.
4480 kern_posix_fadvise(struct thread *td, int fd, off_t offset, off_t len,
4483 struct fadvise_info *fa, *new;
4486 cap_rights_t rights;
4490 if (offset < 0 || len < 0 || offset > OFF_MAX - len)
4492 AUDIT_ARG_VALUE(advice);
4494 case POSIX_FADV_SEQUENTIAL:
4495 case POSIX_FADV_RANDOM:
4496 case POSIX_FADV_NOREUSE:
4497 new = malloc(sizeof(*fa), M_FADVISE, M_WAITOK);
4499 case POSIX_FADV_NORMAL:
4500 case POSIX_FADV_WILLNEED:
4501 case POSIX_FADV_DONTNEED:
4507 /* XXX: CAP_POSIX_FADVISE? */
4509 error = fget(td, fd, cap_rights_init(&rights), &fp);
4512 AUDIT_ARG_FILE(td->td_proc, fp);
4513 if ((fp->f_ops->fo_flags & DFLAG_SEEKABLE) == 0) {
4517 if (fp->f_type != DTYPE_VNODE) {
4522 if (vp->v_type != VREG) {
4529 end = offset + len - 1;
4531 case POSIX_FADV_SEQUENTIAL:
4532 case POSIX_FADV_RANDOM:
4533 case POSIX_FADV_NOREUSE:
4535 * Try to merge any existing non-standard region with
4536 * this new region if possible, otherwise create a new
4537 * non-standard region for this request.
4539 mtx_pool_lock(mtxpool_sleep, fp);
4541 if (fa != NULL && fa->fa_advice == advice &&
4542 ((fa->fa_start <= end && fa->fa_end >= offset) ||
4543 (end != OFF_MAX && fa->fa_start == end + 1) ||
4544 (fa->fa_end != OFF_MAX && fa->fa_end + 1 == offset))) {
4545 if (offset < fa->fa_start)
4546 fa->fa_start = offset;
4547 if (end > fa->fa_end)
4550 new->fa_advice = advice;
4551 new->fa_start = offset;
4556 mtx_pool_unlock(mtxpool_sleep, fp);
4558 case POSIX_FADV_NORMAL:
4560 * If a the "normal" region overlaps with an existing
4561 * non-standard region, trim or remove the
4562 * non-standard region.
4564 mtx_pool_lock(mtxpool_sleep, fp);
4567 if (offset <= fa->fa_start && end >= fa->fa_end) {
4569 fp->f_advice = NULL;
4570 } else if (offset <= fa->fa_start &&
4571 end >= fa->fa_start)
4572 fa->fa_start = end + 1;
4573 else if (offset <= fa->fa_end && end >= fa->fa_end)
4574 fa->fa_end = offset - 1;
4575 else if (offset >= fa->fa_start && end <= fa->fa_end) {
4577 * If the "normal" region is a middle
4578 * portion of the existing
4579 * non-standard region, just remove
4580 * the whole thing rather than picking
4581 * one side or the other to
4585 fp->f_advice = NULL;
4588 mtx_pool_unlock(mtxpool_sleep, fp);
4590 case POSIX_FADV_WILLNEED:
4591 case POSIX_FADV_DONTNEED:
4592 error = VOP_ADVISE(vp, offset, end, advice);
4598 free(new, M_FADVISE);
4603 sys_posix_fadvise(struct thread *td, struct posix_fadvise_args *uap)
4607 error = kern_posix_fadvise(td, uap->fd, uap->offset, uap->len,
4609 return (kern_posix_error(td, error));