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,
110 * Sync each mounted filesystem.
112 #ifndef _SYS_SYSPROTO_H_
119 sys_sync(struct thread *td, struct sync_args *uap)
121 struct mount *mp, *nmp;
124 mtx_lock(&mountlist_mtx);
125 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
126 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK)) {
127 nmp = TAILQ_NEXT(mp, mnt_list);
130 if ((mp->mnt_flag & MNT_RDONLY) == 0 &&
131 vn_start_write(NULL, &mp, V_NOWAIT) == 0) {
132 save = curthread_pflags_set(TDP_SYNCIO);
133 vfs_msync(mp, MNT_NOWAIT);
134 VFS_SYNC(mp, MNT_NOWAIT);
135 curthread_pflags_restore(save);
136 vn_finished_write(mp);
138 mtx_lock(&mountlist_mtx);
139 nmp = TAILQ_NEXT(mp, mnt_list);
142 mtx_unlock(&mountlist_mtx);
147 * Change filesystem quotas.
149 #ifndef _SYS_SYSPROTO_H_
150 struct quotactl_args {
158 sys_quotactl(struct thread *td, struct quotactl_args *uap)
164 AUDIT_ARG_CMD(uap->cmd);
165 AUDIT_ARG_UID(uap->uid);
166 if (!prison_allow(td->td_ucred, PR_ALLOW_QUOTAS))
168 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
170 if ((error = namei(&nd)) != 0)
172 NDFREE(&nd, NDF_ONLY_PNBUF);
173 mp = nd.ni_vp->v_mount;
176 error = vfs_busy(mp, 0);
180 error = VFS_QUOTACTL(mp, uap->cmd, uap->uid, uap->arg);
183 * Since quota on operation typically needs to open quota
184 * file, the Q_QUOTAON handler needs to unbusy the mount point
185 * before calling into namei. Otherwise, unmount might be
186 * started between two vfs_busy() invocations (first is our,
187 * second is from mount point cross-walk code in lookup()),
190 * Require that Q_QUOTAON handles the vfs_busy() reference on
191 * its own, always returning with ubusied mount point.
193 if ((uap->cmd >> SUBCMDSHIFT) != Q_QUOTAON)
199 * Used by statfs conversion routines to scale the block size up if
200 * necessary so that all of the block counts are <= 'max_size'. Note
201 * that 'max_size' should be a bitmask, i.e. 2^n - 1 for some non-zero
205 statfs_scale_blocks(struct statfs *sf, long max_size)
210 KASSERT(powerof2(max_size + 1), ("%s: invalid max_size", __func__));
213 * Attempt to scale the block counts to give a more accurate
214 * overview to userland of the ratio of free space to used
215 * space. To do this, find the largest block count and compute
216 * a divisor that lets it fit into a signed integer <= max_size.
218 if (sf->f_bavail < 0)
219 count = -sf->f_bavail;
221 count = sf->f_bavail;
222 count = MAX(sf->f_blocks, MAX(sf->f_bfree, count));
223 if (count <= max_size)
226 count >>= flsl(max_size);
233 sf->f_bsize <<= shift;
234 sf->f_blocks >>= shift;
235 sf->f_bfree >>= shift;
236 sf->f_bavail >>= shift;
240 kern_do_statfs(struct thread *td, struct mount *mp, struct statfs *buf)
247 error = vfs_busy(mp, 0);
252 error = mac_mount_check_stat(td->td_ucred, mp);
257 * Set these in case the underlying filesystem fails to do so.
260 sp->f_version = STATFS_VERSION;
261 sp->f_namemax = NAME_MAX;
262 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
263 error = VFS_STATFS(mp, sp);
267 if (priv_check(td, PRIV_VFS_GENERATION)) {
268 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
269 prison_enforce_statfs(td->td_ucred, mp, buf);
277 * Get filesystem statistics.
279 #ifndef _SYS_SYSPROTO_H_
286 sys_statfs(struct thread *td, struct statfs_args *uap)
291 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
292 error = kern_statfs(td, uap->path, UIO_USERSPACE, sfp);
294 error = copyout(sfp, uap->buf, sizeof(struct statfs));
300 kern_statfs(struct thread *td, char *path, enum uio_seg pathseg,
307 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
312 mp = nd.ni_vp->v_mount;
314 NDFREE(&nd, NDF_ONLY_PNBUF);
316 return (kern_do_statfs(td, mp, buf));
320 * Get filesystem statistics.
322 #ifndef _SYS_SYSPROTO_H_
323 struct fstatfs_args {
329 sys_fstatfs(struct thread *td, struct fstatfs_args *uap)
334 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
335 error = kern_fstatfs(td, uap->fd, sfp);
337 error = copyout(sfp, uap->buf, sizeof(struct statfs));
343 kern_fstatfs(struct thread *td, int fd, struct statfs *buf)
351 error = getvnode(td, fd, &cap_fstatfs_rights, &fp);
355 vn_lock(vp, LK_SHARED | LK_RETRY);
357 AUDIT_ARG_VNODE1(vp);
364 return (kern_do_statfs(td, mp, buf));
368 * Get statistics on all filesystems.
370 #ifndef _SYS_SYSPROTO_H_
371 struct getfsstat_args {
378 sys_getfsstat(struct thread *td, struct getfsstat_args *uap)
383 if (uap->bufsize < 0 || uap->bufsize > SIZE_MAX)
385 error = kern_getfsstat(td, &uap->buf, uap->bufsize, &count,
386 UIO_USERSPACE, uap->mode);
388 td->td_retval[0] = count;
393 * If (bufsize > 0 && bufseg == UIO_SYSSPACE)
394 * The caller is responsible for freeing memory which will be allocated
398 kern_getfsstat(struct thread *td, struct statfs **buf, size_t bufsize,
399 size_t *countp, enum uio_seg bufseg, int mode)
401 struct mount *mp, *nmp;
402 struct statfs *sfsp, *sp, *sptmp, *tofree;
403 size_t count, maxcount;
411 if (bufseg == UIO_SYSSPACE)
416 maxcount = bufsize / sizeof(struct statfs);
420 } else if (bufseg == UIO_USERSPACE) {
423 } else /* if (bufseg == UIO_SYSSPACE) */ {
425 mtx_lock(&mountlist_mtx);
426 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
429 mtx_unlock(&mountlist_mtx);
430 if (maxcount > count)
432 tofree = sfsp = *buf = malloc(maxcount * sizeof(struct statfs),
436 mtx_lock(&mountlist_mtx);
437 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
438 if (prison_canseemount(td->td_ucred, mp) != 0) {
439 nmp = TAILQ_NEXT(mp, mnt_list);
443 if (mac_mount_check_stat(td->td_ucred, mp) != 0) {
444 nmp = TAILQ_NEXT(mp, mnt_list);
448 if (mode == MNT_WAIT) {
449 if (vfs_busy(mp, MBF_MNTLSTLOCK) != 0) {
451 * If vfs_busy() failed, and MBF_NOWAIT
452 * wasn't passed, then the mp is gone.
453 * Furthermore, because of MBF_MNTLSTLOCK,
454 * the mountlist_mtx was dropped. We have
455 * no other choice than to start over.
457 mtx_unlock(&mountlist_mtx);
458 free(tofree, M_STATFS);
462 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK) != 0) {
463 nmp = TAILQ_NEXT(mp, mnt_list);
467 if (sfsp != NULL && count < maxcount) {
470 * Set these in case the underlying filesystem
473 sp->f_version = STATFS_VERSION;
474 sp->f_namemax = NAME_MAX;
475 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
477 * If MNT_NOWAIT is specified, do not refresh
480 if (mode != MNT_NOWAIT) {
481 error = VFS_STATFS(mp, sp);
483 mtx_lock(&mountlist_mtx);
484 nmp = TAILQ_NEXT(mp, mnt_list);
489 if (priv_check(td, PRIV_VFS_GENERATION)) {
490 sptmp = malloc(sizeof(struct statfs), M_STATFS,
493 sptmp->f_fsid.val[0] = sptmp->f_fsid.val[1] = 0;
494 prison_enforce_statfs(td->td_ucred, mp, sptmp);
498 if (bufseg == UIO_SYSSPACE) {
499 bcopy(sp, sfsp, sizeof(*sp));
500 free(sptmp, M_STATFS);
501 } else /* if (bufseg == UIO_USERSPACE) */ {
502 error = copyout(sp, sfsp, sizeof(*sp));
503 free(sptmp, M_STATFS);
512 mtx_lock(&mountlist_mtx);
513 nmp = TAILQ_NEXT(mp, mnt_list);
516 mtx_unlock(&mountlist_mtx);
517 if (sfsp != NULL && count > maxcount)
524 #ifdef COMPAT_FREEBSD4
526 * Get old format filesystem statistics.
528 static void freebsd4_cvtstatfs(struct statfs *, struct ostatfs *);
530 #ifndef _SYS_SYSPROTO_H_
531 struct freebsd4_statfs_args {
537 freebsd4_statfs(struct thread *td, struct freebsd4_statfs_args *uap)
543 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
544 error = kern_statfs(td, uap->path, UIO_USERSPACE, sfp);
546 freebsd4_cvtstatfs(sfp, &osb);
547 error = copyout(&osb, uap->buf, sizeof(osb));
554 * Get filesystem statistics.
556 #ifndef _SYS_SYSPROTO_H_
557 struct freebsd4_fstatfs_args {
563 freebsd4_fstatfs(struct thread *td, struct freebsd4_fstatfs_args *uap)
569 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
570 error = kern_fstatfs(td, uap->fd, sfp);
572 freebsd4_cvtstatfs(sfp, &osb);
573 error = copyout(&osb, uap->buf, sizeof(osb));
580 * Get statistics on all filesystems.
582 #ifndef _SYS_SYSPROTO_H_
583 struct freebsd4_getfsstat_args {
590 freebsd4_getfsstat(struct thread *td, struct freebsd4_getfsstat_args *uap)
592 struct statfs *buf, *sp;
597 if (uap->bufsize < 0)
599 count = uap->bufsize / sizeof(struct ostatfs);
600 if (count > SIZE_MAX / sizeof(struct statfs))
602 size = count * sizeof(struct statfs);
603 error = kern_getfsstat(td, &buf, size, &count, UIO_SYSSPACE,
606 td->td_retval[0] = count;
609 while (count != 0 && error == 0) {
610 freebsd4_cvtstatfs(sp, &osb);
611 error = copyout(&osb, uap->buf, sizeof(osb));
622 * Implement fstatfs() for (NFS) file handles.
624 #ifndef _SYS_SYSPROTO_H_
625 struct freebsd4_fhstatfs_args {
626 struct fhandle *u_fhp;
631 freebsd4_fhstatfs(struct thread *td, struct freebsd4_fhstatfs_args *uap)
638 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
641 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
642 error = kern_fhstatfs(td, fh, sfp);
644 freebsd4_cvtstatfs(sfp, &osb);
645 error = copyout(&osb, uap->buf, sizeof(osb));
652 * Convert a new format statfs structure to an old format statfs structure.
655 freebsd4_cvtstatfs(struct statfs *nsp, struct ostatfs *osp)
658 statfs_scale_blocks(nsp, LONG_MAX);
659 bzero(osp, sizeof(*osp));
660 osp->f_bsize = nsp->f_bsize;
661 osp->f_iosize = MIN(nsp->f_iosize, LONG_MAX);
662 osp->f_blocks = nsp->f_blocks;
663 osp->f_bfree = nsp->f_bfree;
664 osp->f_bavail = nsp->f_bavail;
665 osp->f_files = MIN(nsp->f_files, LONG_MAX);
666 osp->f_ffree = MIN(nsp->f_ffree, LONG_MAX);
667 osp->f_owner = nsp->f_owner;
668 osp->f_type = nsp->f_type;
669 osp->f_flags = nsp->f_flags;
670 osp->f_syncwrites = MIN(nsp->f_syncwrites, LONG_MAX);
671 osp->f_asyncwrites = MIN(nsp->f_asyncwrites, LONG_MAX);
672 osp->f_syncreads = MIN(nsp->f_syncreads, LONG_MAX);
673 osp->f_asyncreads = MIN(nsp->f_asyncreads, LONG_MAX);
674 strlcpy(osp->f_fstypename, nsp->f_fstypename,
675 MIN(MFSNAMELEN, OMFSNAMELEN));
676 strlcpy(osp->f_mntonname, nsp->f_mntonname,
677 MIN(MNAMELEN, OMNAMELEN));
678 strlcpy(osp->f_mntfromname, nsp->f_mntfromname,
679 MIN(MNAMELEN, OMNAMELEN));
680 osp->f_fsid = nsp->f_fsid;
682 #endif /* COMPAT_FREEBSD4 */
684 #if defined(COMPAT_FREEBSD11)
686 * Get old format filesystem statistics.
688 static void freebsd11_cvtstatfs(struct statfs *, struct freebsd11_statfs *);
691 freebsd11_statfs(struct thread *td, struct freebsd11_statfs_args *uap)
693 struct freebsd11_statfs osb;
697 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
698 error = kern_statfs(td, uap->path, UIO_USERSPACE, sfp);
700 freebsd11_cvtstatfs(sfp, &osb);
701 error = copyout(&osb, uap->buf, sizeof(osb));
708 * Get filesystem statistics.
711 freebsd11_fstatfs(struct thread *td, struct freebsd11_fstatfs_args *uap)
713 struct freebsd11_statfs osb;
717 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
718 error = kern_fstatfs(td, uap->fd, sfp);
720 freebsd11_cvtstatfs(sfp, &osb);
721 error = copyout(&osb, uap->buf, sizeof(osb));
728 * Get statistics on all filesystems.
731 freebsd11_getfsstat(struct thread *td, struct freebsd11_getfsstat_args *uap)
733 struct freebsd11_statfs osb;
734 struct statfs *buf, *sp;
738 count = uap->bufsize / sizeof(struct ostatfs);
739 size = count * sizeof(struct statfs);
740 error = kern_getfsstat(td, &buf, size, &count, UIO_SYSSPACE,
743 td->td_retval[0] = count;
746 while (count > 0 && error == 0) {
747 freebsd11_cvtstatfs(sp, &osb);
748 error = copyout(&osb, uap->buf, sizeof(osb));
759 * Implement fstatfs() for (NFS) file handles.
762 freebsd11_fhstatfs(struct thread *td, struct freebsd11_fhstatfs_args *uap)
764 struct freebsd11_statfs osb;
769 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
772 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
773 error = kern_fhstatfs(td, fh, sfp);
775 freebsd11_cvtstatfs(sfp, &osb);
776 error = copyout(&osb, uap->buf, sizeof(osb));
783 * Convert a new format statfs structure to an old format statfs structure.
786 freebsd11_cvtstatfs(struct statfs *nsp, struct freebsd11_statfs *osp)
789 bzero(osp, sizeof(*osp));
790 osp->f_version = FREEBSD11_STATFS_VERSION;
791 osp->f_type = nsp->f_type;
792 osp->f_flags = nsp->f_flags;
793 osp->f_bsize = nsp->f_bsize;
794 osp->f_iosize = nsp->f_iosize;
795 osp->f_blocks = nsp->f_blocks;
796 osp->f_bfree = nsp->f_bfree;
797 osp->f_bavail = nsp->f_bavail;
798 osp->f_files = nsp->f_files;
799 osp->f_ffree = nsp->f_ffree;
800 osp->f_syncwrites = nsp->f_syncwrites;
801 osp->f_asyncwrites = nsp->f_asyncwrites;
802 osp->f_syncreads = nsp->f_syncreads;
803 osp->f_asyncreads = nsp->f_asyncreads;
804 osp->f_namemax = nsp->f_namemax;
805 osp->f_owner = nsp->f_owner;
806 osp->f_fsid = nsp->f_fsid;
807 strlcpy(osp->f_fstypename, nsp->f_fstypename,
808 MIN(MFSNAMELEN, sizeof(osp->f_fstypename)));
809 strlcpy(osp->f_mntonname, nsp->f_mntonname,
810 MIN(MNAMELEN, sizeof(osp->f_mntonname)));
811 strlcpy(osp->f_mntfromname, nsp->f_mntfromname,
812 MIN(MNAMELEN, sizeof(osp->f_mntfromname)));
814 #endif /* COMPAT_FREEBSD11 */
817 * Change current working directory to a given file descriptor.
819 #ifndef _SYS_SYSPROTO_H_
825 sys_fchdir(struct thread *td, struct fchdir_args *uap)
827 struct vnode *vp, *tdp;
832 AUDIT_ARG_FD(uap->fd);
833 error = getvnode(td, uap->fd, &cap_fchdir_rights,
840 vn_lock(vp, LK_SHARED | LK_RETRY);
841 AUDIT_ARG_VNODE1(vp);
842 error = change_dir(vp, td);
843 while (!error && (mp = vp->v_mountedhere) != NULL) {
846 error = VFS_ROOT(mp, LK_SHARED, &tdp);
863 * Change current working directory (``.'').
865 #ifndef _SYS_SYSPROTO_H_
871 sys_chdir(struct thread *td, struct chdir_args *uap)
874 return (kern_chdir(td, uap->path, UIO_USERSPACE));
878 kern_chdir(struct thread *td, char *path, enum uio_seg pathseg)
883 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
885 if ((error = namei(&nd)) != 0)
887 if ((error = change_dir(nd.ni_vp, td)) != 0) {
889 NDFREE(&nd, NDF_ONLY_PNBUF);
892 VOP_UNLOCK(nd.ni_vp, 0);
893 NDFREE(&nd, NDF_ONLY_PNBUF);
894 pwd_chdir(td, nd.ni_vp);
899 * Change notion of root (``/'') directory.
901 #ifndef _SYS_SYSPROTO_H_
907 sys_chroot(struct thread *td, struct chroot_args *uap)
912 error = priv_check(td, PRIV_VFS_CHROOT);
915 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
916 UIO_USERSPACE, uap->path, td);
920 error = change_dir(nd.ni_vp, td);
924 error = mac_vnode_check_chroot(td->td_ucred, nd.ni_vp);
928 VOP_UNLOCK(nd.ni_vp, 0);
929 error = pwd_chroot(td, nd.ni_vp);
931 NDFREE(&nd, NDF_ONLY_PNBUF);
936 NDFREE(&nd, NDF_ONLY_PNBUF);
941 * Common routine for chroot and chdir. Callers must provide a locked vnode
945 change_dir(struct vnode *vp, struct thread *td)
951 ASSERT_VOP_LOCKED(vp, "change_dir(): vp not locked");
952 if (vp->v_type != VDIR)
955 error = mac_vnode_check_chdir(td->td_ucred, vp);
959 return (VOP_ACCESS(vp, VEXEC, td->td_ucred, td));
963 flags_to_rights(int flags, cap_rights_t *rightsp)
966 if (flags & O_EXEC) {
967 cap_rights_set(rightsp, CAP_FEXECVE);
969 switch ((flags & O_ACCMODE)) {
971 cap_rights_set(rightsp, CAP_READ);
974 cap_rights_set(rightsp, CAP_READ);
977 cap_rights_set(rightsp, CAP_WRITE);
978 if (!(flags & (O_APPEND | O_TRUNC)))
979 cap_rights_set(rightsp, CAP_SEEK);
985 cap_rights_set(rightsp, CAP_CREATE);
988 cap_rights_set(rightsp, CAP_FTRUNCATE);
990 if (flags & (O_SYNC | O_FSYNC))
991 cap_rights_set(rightsp, CAP_FSYNC);
993 if (flags & (O_EXLOCK | O_SHLOCK))
994 cap_rights_set(rightsp, CAP_FLOCK);
998 * Check permissions, allocate an open file structure, and call the device
999 * open routine if any.
1001 #ifndef _SYS_SYSPROTO_H_
1009 sys_open(struct thread *td, struct open_args *uap)
1012 return (kern_openat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1013 uap->flags, uap->mode));
1016 #ifndef _SYS_SYSPROTO_H_
1017 struct openat_args {
1025 sys_openat(struct thread *td, struct openat_args *uap)
1028 AUDIT_ARG_FD(uap->fd);
1029 return (kern_openat(td, uap->fd, uap->path, UIO_USERSPACE, uap->flag,
1034 kern_openat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1035 int flags, int mode)
1037 struct proc *p = td->td_proc;
1038 struct filedesc *fdp = p->p_fd;
1041 struct nameidata nd;
1042 cap_rights_t rights;
1043 int cmode, error, indx;
1047 AUDIT_ARG_FFLAGS(flags);
1048 AUDIT_ARG_MODE(mode);
1049 cap_rights_init(&rights, CAP_LOOKUP);
1050 flags_to_rights(flags, &rights);
1052 * Only one of the O_EXEC, O_RDONLY, O_WRONLY and O_RDWR flags
1055 if (flags & O_EXEC) {
1056 if (flags & O_ACCMODE)
1058 } else if ((flags & O_ACCMODE) == O_ACCMODE) {
1061 flags = FFLAGS(flags);
1065 * Allocate a file structure. The descriptor to reference it
1066 * is allocated and set by finstall() below.
1068 error = falloc_noinstall(td, &fp);
1072 * An extra reference on `fp' has been held for us by
1073 * falloc_noinstall().
1075 /* Set the flags early so the finit in devfs can pick them up. */
1076 fp->f_flag = flags & FMASK;
1077 cmode = ((mode & ~fdp->fd_cmask) & ALLPERMS) & ~S_ISTXT;
1078 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, fd,
1080 td->td_dupfd = -1; /* XXX check for fdopen */
1081 error = vn_open(&nd, &flags, cmode, fp);
1084 * If the vn_open replaced the method vector, something
1085 * wonderous happened deep below and we just pass it up
1086 * pretending we know what we do.
1088 if (error == ENXIO && fp->f_ops != &badfileops)
1092 * Handle special fdopen() case. bleh.
1094 * Don't do this for relative (capability) lookups; we don't
1095 * understand exactly what would happen, and we don't think
1096 * that it ever should.
1098 if ((nd.ni_lcf & NI_LCF_STRICTRELATIVE) == 0 &&
1099 (error == ENODEV || error == ENXIO) &&
1100 td->td_dupfd >= 0) {
1101 error = dupfdopen(td, fdp, td->td_dupfd, flags, error,
1110 NDFREE(&nd, NDF_ONLY_PNBUF);
1114 * Store the vnode, for any f_type. Typically, the vnode use
1115 * count is decremented by direct call to vn_closefile() for
1116 * files that switched type in the cdevsw fdopen() method.
1120 * If the file wasn't claimed by devfs bind it to the normal
1121 * vnode operations here.
1123 if (fp->f_ops == &badfileops) {
1124 KASSERT(vp->v_type != VFIFO, ("Unexpected fifo."));
1126 finit(fp, (flags & FMASK) | (fp->f_flag & FHASLOCK),
1127 DTYPE_VNODE, vp, &vnops);
1131 if (flags & O_TRUNC) {
1132 error = fo_truncate(fp, 0, td->td_ucred, td);
1138 * If we haven't already installed the FD (for dupfdopen), do so now.
1141 struct filecaps *fcaps;
1144 if ((nd.ni_lcf & NI_LCF_STRICTRELATIVE) != 0)
1145 fcaps = &nd.ni_filecaps;
1149 error = finstall(td, fp, &indx, flags, fcaps);
1150 /* On success finstall() consumes fcaps. */
1152 filecaps_free(&nd.ni_filecaps);
1156 filecaps_free(&nd.ni_filecaps);
1160 * Release our private reference, leaving the one associated with
1161 * the descriptor table intact.
1164 td->td_retval[0] = indx;
1167 KASSERT(indx == -1, ("indx=%d, should be -1", indx));
1176 #ifndef _SYS_SYSPROTO_H_
1177 struct ocreat_args {
1183 ocreat(struct thread *td, struct ocreat_args *uap)
1186 return (kern_openat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1187 O_WRONLY | O_CREAT | O_TRUNC, uap->mode));
1189 #endif /* COMPAT_43 */
1192 * Create a special file.
1194 #ifndef _SYS_SYSPROTO_H_
1195 struct mknodat_args {
1203 sys_mknodat(struct thread *td, struct mknodat_args *uap)
1206 return (kern_mknodat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode,
1210 #if defined(COMPAT_FREEBSD11)
1212 freebsd11_mknod(struct thread *td,
1213 struct freebsd11_mknod_args *uap)
1216 return (kern_mknodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1217 uap->mode, uap->dev));
1221 freebsd11_mknodat(struct thread *td,
1222 struct freebsd11_mknodat_args *uap)
1225 return (kern_mknodat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode,
1228 #endif /* COMPAT_FREEBSD11 */
1231 kern_mknodat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1232 int mode, dev_t dev)
1237 struct nameidata nd;
1238 int error, whiteout = 0;
1240 AUDIT_ARG_MODE(mode);
1242 switch (mode & S_IFMT) {
1245 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
1246 if (error == 0 && dev == VNOVAL)
1250 error = priv_check(td, PRIV_VFS_MKNOD_WHT);
1254 return (kern_mkfifoat(td, fd, path, pathseg, mode));
1264 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
1265 NOCACHE, pathseg, path, fd, &cap_mknodat_rights,
1267 if ((error = namei(&nd)) != 0)
1271 NDFREE(&nd, NDF_ONLY_PNBUF);
1272 if (vp == nd.ni_dvp)
1280 vattr.va_mode = (mode & ALLPERMS) &
1281 ~td->td_proc->p_fd->fd_cmask;
1282 vattr.va_rdev = dev;
1285 switch (mode & S_IFMT) {
1287 vattr.va_type = VCHR;
1290 vattr.va_type = VBLK;
1296 panic("kern_mknod: invalid mode");
1299 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1300 NDFREE(&nd, NDF_ONLY_PNBUF);
1302 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1307 if (error == 0 && !whiteout)
1308 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp,
1309 &nd.ni_cnd, &vattr);
1313 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, CREATE);
1315 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp,
1316 &nd.ni_cnd, &vattr);
1321 NDFREE(&nd, NDF_ONLY_PNBUF);
1323 vn_finished_write(mp);
1328 * Create a named pipe.
1330 #ifndef _SYS_SYSPROTO_H_
1331 struct mkfifo_args {
1337 sys_mkfifo(struct thread *td, struct mkfifo_args *uap)
1340 return (kern_mkfifoat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1344 #ifndef _SYS_SYSPROTO_H_
1345 struct mkfifoat_args {
1352 sys_mkfifoat(struct thread *td, struct mkfifoat_args *uap)
1355 return (kern_mkfifoat(td, uap->fd, uap->path, UIO_USERSPACE,
1360 kern_mkfifoat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1365 struct nameidata nd;
1368 AUDIT_ARG_MODE(mode);
1371 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
1372 NOCACHE, pathseg, path, fd, &cap_mkfifoat_rights,
1374 if ((error = namei(&nd)) != 0)
1376 if (nd.ni_vp != NULL) {
1377 NDFREE(&nd, NDF_ONLY_PNBUF);
1378 if (nd.ni_vp == nd.ni_dvp)
1385 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1386 NDFREE(&nd, NDF_ONLY_PNBUF);
1388 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1393 vattr.va_type = VFIFO;
1394 vattr.va_mode = (mode & ALLPERMS) & ~td->td_proc->p_fd->fd_cmask;
1396 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1401 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
1408 vn_finished_write(mp);
1409 NDFREE(&nd, NDF_ONLY_PNBUF);
1414 * Make a hard file link.
1416 #ifndef _SYS_SYSPROTO_H_
1423 sys_link(struct thread *td, struct link_args *uap)
1426 return (kern_linkat(td, AT_FDCWD, AT_FDCWD, uap->path, uap->link,
1427 UIO_USERSPACE, FOLLOW));
1430 #ifndef _SYS_SYSPROTO_H_
1431 struct linkat_args {
1440 sys_linkat(struct thread *td, struct linkat_args *uap)
1445 if (flag & ~AT_SYMLINK_FOLLOW)
1448 return (kern_linkat(td, uap->fd1, uap->fd2, uap->path1, uap->path2,
1449 UIO_USERSPACE, (flag & AT_SYMLINK_FOLLOW) ? FOLLOW : NOFOLLOW));
1452 int hardlink_check_uid = 0;
1453 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
1454 &hardlink_check_uid, 0,
1455 "Unprivileged processes cannot create hard links to files owned by other "
1457 static int hardlink_check_gid = 0;
1458 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
1459 &hardlink_check_gid, 0,
1460 "Unprivileged processes cannot create hard links to files owned by other "
1464 can_hardlink(struct vnode *vp, struct ucred *cred)
1469 if (!hardlink_check_uid && !hardlink_check_gid)
1472 error = VOP_GETATTR(vp, &va, cred);
1476 if (hardlink_check_uid && cred->cr_uid != va.va_uid) {
1477 error = priv_check_cred(cred, PRIV_VFS_LINK, 0);
1482 if (hardlink_check_gid && !groupmember(va.va_gid, cred)) {
1483 error = priv_check_cred(cred, PRIV_VFS_LINK, 0);
1492 kern_linkat(struct thread *td, int fd1, int fd2, char *path1, char *path2,
1493 enum uio_seg segflg, int follow)
1497 struct nameidata nd;
1502 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, segflg, path1, fd1,
1503 &cap_linkat_source_rights, td);
1505 if ((error = namei(&nd)) != 0)
1507 NDFREE(&nd, NDF_ONLY_PNBUF);
1509 if (vp->v_type == VDIR) {
1511 return (EPERM); /* POSIX */
1513 NDINIT_ATRIGHTS(&nd, CREATE,
1514 LOCKPARENT | SAVENAME | AUDITVNODE2 | NOCACHE, segflg, path2, fd2,
1515 &cap_linkat_target_rights, td);
1516 if ((error = namei(&nd)) == 0) {
1517 if (nd.ni_vp != NULL) {
1518 NDFREE(&nd, NDF_ONLY_PNBUF);
1519 if (nd.ni_dvp == nd.ni_vp)
1526 } else if (nd.ni_dvp->v_mount != vp->v_mount) {
1528 * Cross-device link. No need to recheck
1529 * vp->v_type, since it cannot change, except
1532 NDFREE(&nd, NDF_ONLY_PNBUF);
1536 } else if ((error = vn_lock(vp, LK_EXCLUSIVE)) == 0) {
1537 error = can_hardlink(vp, td->td_ucred);
1540 error = mac_vnode_check_link(td->td_ucred,
1541 nd.ni_dvp, vp, &nd.ni_cnd);
1546 NDFREE(&nd, NDF_ONLY_PNBUF);
1549 error = vn_start_write(vp, &mp, V_NOWAIT);
1553 NDFREE(&nd, NDF_ONLY_PNBUF);
1554 error = vn_start_write(NULL, &mp,
1560 error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
1563 vn_finished_write(mp);
1564 NDFREE(&nd, NDF_ONLY_PNBUF);
1567 NDFREE(&nd, NDF_ONLY_PNBUF);
1577 * Make a symbolic link.
1579 #ifndef _SYS_SYSPROTO_H_
1580 struct symlink_args {
1586 sys_symlink(struct thread *td, struct symlink_args *uap)
1589 return (kern_symlinkat(td, uap->path, AT_FDCWD, uap->link,
1593 #ifndef _SYS_SYSPROTO_H_
1594 struct symlinkat_args {
1601 sys_symlinkat(struct thread *td, struct symlinkat_args *uap)
1604 return (kern_symlinkat(td, uap->path1, uap->fd, uap->path2,
1609 kern_symlinkat(struct thread *td, char *path1, int fd, char *path2,
1610 enum uio_seg segflg)
1615 struct nameidata nd;
1618 if (segflg == UIO_SYSSPACE) {
1621 syspath = uma_zalloc(namei_zone, M_WAITOK);
1622 if ((error = copyinstr(path1, syspath, MAXPATHLEN, NULL)) != 0)
1625 AUDIT_ARG_TEXT(syspath);
1628 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
1629 NOCACHE, segflg, path2, fd, &cap_symlinkat_rights,
1631 if ((error = namei(&nd)) != 0)
1634 NDFREE(&nd, NDF_ONLY_PNBUF);
1635 if (nd.ni_vp == nd.ni_dvp)
1643 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1644 NDFREE(&nd, NDF_ONLY_PNBUF);
1646 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1651 vattr.va_mode = ACCESSPERMS &~ td->td_proc->p_fd->fd_cmask;
1653 vattr.va_type = VLNK;
1654 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1659 error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr, syspath);
1665 NDFREE(&nd, NDF_ONLY_PNBUF);
1667 vn_finished_write(mp);
1669 if (segflg != UIO_SYSSPACE)
1670 uma_zfree(namei_zone, syspath);
1675 * Delete a whiteout from the filesystem.
1677 #ifndef _SYS_SYSPROTO_H_
1678 struct undelete_args {
1683 sys_undelete(struct thread *td, struct undelete_args *uap)
1686 struct nameidata nd;
1691 NDINIT(&nd, DELETE, LOCKPARENT | DOWHITEOUT | AUDITVNODE1,
1692 UIO_USERSPACE, uap->path, td);
1697 if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & ISWHITEOUT)) {
1698 NDFREE(&nd, NDF_ONLY_PNBUF);
1699 if (nd.ni_vp == nd.ni_dvp)
1707 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1708 NDFREE(&nd, NDF_ONLY_PNBUF);
1710 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1714 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, DELETE);
1715 NDFREE(&nd, NDF_ONLY_PNBUF);
1717 vn_finished_write(mp);
1722 * Delete a name from the filesystem.
1724 #ifndef _SYS_SYSPROTO_H_
1725 struct unlink_args {
1730 sys_unlink(struct thread *td, struct unlink_args *uap)
1733 return (kern_unlinkat(td, AT_FDCWD, uap->path, UIO_USERSPACE, 0));
1736 #ifndef _SYS_SYSPROTO_H_
1737 struct unlinkat_args {
1744 sys_unlinkat(struct thread *td, struct unlinkat_args *uap)
1746 int flag = uap->flag;
1748 char *path = uap->path;
1750 if (flag & ~AT_REMOVEDIR)
1753 if (flag & AT_REMOVEDIR)
1754 return (kern_rmdirat(td, fd, path, UIO_USERSPACE));
1756 return (kern_unlinkat(td, fd, path, UIO_USERSPACE, 0));
1760 kern_unlinkat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1765 struct nameidata nd;
1771 NDINIT_ATRIGHTS(&nd, DELETE, LOCKPARENT | LOCKLEAF | AUDITVNODE1,
1772 pathseg, path, fd, &cap_unlinkat_rights, td);
1773 if ((error = namei(&nd)) != 0)
1774 return (error == EINVAL ? EPERM : error);
1776 if (vp->v_type == VDIR && oldinum == 0) {
1777 error = EPERM; /* POSIX */
1778 } else if (oldinum != 0 &&
1779 ((error = vn_stat(vp, &sb, td->td_ucred, NOCRED, td)) == 0) &&
1780 sb.st_ino != oldinum) {
1781 error = EIDRM; /* Identifier removed */
1784 * The root of a mounted filesystem cannot be deleted.
1786 * XXX: can this only be a VDIR case?
1788 if (vp->v_vflag & VV_ROOT)
1792 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1793 NDFREE(&nd, NDF_ONLY_PNBUF);
1795 if (vp == nd.ni_dvp)
1799 if ((error = vn_start_write(NULL, &mp,
1800 V_XSLEEP | PCATCH)) != 0)
1805 error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp,
1810 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_UNLINK);
1811 error = VOP_REMOVE(nd.ni_dvp, vp, &nd.ni_cnd);
1815 vn_finished_write(mp);
1817 NDFREE(&nd, NDF_ONLY_PNBUF);
1819 if (vp == nd.ni_dvp)
1827 * Reposition read/write file offset.
1829 #ifndef _SYS_SYSPROTO_H_
1838 sys_lseek(struct thread *td, struct lseek_args *uap)
1841 return (kern_lseek(td, uap->fd, uap->offset, uap->whence));
1845 kern_lseek(struct thread *td, int fd, off_t offset, int whence)
1851 error = fget(td, fd, &cap_seek_rights, &fp);
1854 error = (fp->f_ops->fo_flags & DFLAG_SEEKABLE) != 0 ?
1855 fo_seek(fp, offset, whence, td) : ESPIPE;
1860 #if defined(COMPAT_43)
1862 * Reposition read/write file offset.
1864 #ifndef _SYS_SYSPROTO_H_
1865 struct olseek_args {
1872 olseek(struct thread *td, struct olseek_args *uap)
1875 return (kern_lseek(td, uap->fd, uap->offset, uap->whence));
1877 #endif /* COMPAT_43 */
1879 #if defined(COMPAT_FREEBSD6)
1880 /* Version with the 'pad' argument */
1882 freebsd6_lseek(struct thread *td, struct freebsd6_lseek_args *uap)
1885 return (kern_lseek(td, uap->fd, uap->offset, uap->whence));
1890 * Check access permissions using passed credentials.
1893 vn_access(struct vnode *vp, int user_flags, struct ucred *cred,
1899 /* Flags == 0 means only check for existence. */
1900 if (user_flags == 0)
1904 if (user_flags & R_OK)
1906 if (user_flags & W_OK)
1908 if (user_flags & X_OK)
1911 error = mac_vnode_check_access(cred, vp, accmode);
1915 if ((accmode & VWRITE) == 0 || (error = vn_writechk(vp)) == 0)
1916 error = VOP_ACCESS(vp, accmode, cred, td);
1921 * Check access permissions using "real" credentials.
1923 #ifndef _SYS_SYSPROTO_H_
1924 struct access_args {
1930 sys_access(struct thread *td, struct access_args *uap)
1933 return (kern_accessat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1937 #ifndef _SYS_SYSPROTO_H_
1938 struct faccessat_args {
1946 sys_faccessat(struct thread *td, struct faccessat_args *uap)
1949 return (kern_accessat(td, uap->fd, uap->path, UIO_USERSPACE, uap->flag,
1954 kern_accessat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1955 int flag, int amode)
1957 struct ucred *cred, *usecred;
1959 struct nameidata nd;
1962 if (flag & ~AT_EACCESS)
1964 if (amode != F_OK && (amode & ~(R_OK | W_OK | X_OK)) != 0)
1968 * Create and modify a temporary credential instead of one that
1969 * is potentially shared (if we need one).
1971 cred = td->td_ucred;
1972 if ((flag & AT_EACCESS) == 0 &&
1973 ((cred->cr_uid != cred->cr_ruid ||
1974 cred->cr_rgid != cred->cr_groups[0]))) {
1975 usecred = crdup(cred);
1976 usecred->cr_uid = cred->cr_ruid;
1977 usecred->cr_groups[0] = cred->cr_rgid;
1978 td->td_ucred = usecred;
1981 AUDIT_ARG_VALUE(amode);
1982 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF |
1983 AUDITVNODE1, pathseg, path, fd, &cap_fstat_rights,
1985 if ((error = namei(&nd)) != 0)
1989 error = vn_access(vp, amode, usecred, td);
1990 NDFREE(&nd, NDF_ONLY_PNBUF);
1993 if (usecred != cred) {
1994 td->td_ucred = cred;
2001 * Check access permissions using "effective" credentials.
2003 #ifndef _SYS_SYSPROTO_H_
2004 struct eaccess_args {
2010 sys_eaccess(struct thread *td, struct eaccess_args *uap)
2013 return (kern_accessat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2014 AT_EACCESS, uap->amode));
2017 #if defined(COMPAT_43)
2019 * Get file status; this version follows links.
2021 #ifndef _SYS_SYSPROTO_H_
2028 ostat(struct thread *td, struct ostat_args *uap)
2034 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
2039 return (copyout(&osb, uap->ub, sizeof (osb)));
2043 * Get file status; this version does not follow links.
2045 #ifndef _SYS_SYSPROTO_H_
2046 struct olstat_args {
2052 olstat(struct thread *td, struct olstat_args *uap)
2058 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2059 UIO_USERSPACE, &sb, NULL);
2063 return (copyout(&osb, uap->ub, sizeof (osb)));
2067 * Convert from an old to a new stat structure.
2070 cvtstat(struct stat *st, struct ostat *ost)
2073 bzero(ost, sizeof(*ost));
2074 ost->st_dev = st->st_dev;
2075 ost->st_ino = st->st_ino;
2076 ost->st_mode = st->st_mode;
2077 ost->st_nlink = st->st_nlink;
2078 ost->st_uid = st->st_uid;
2079 ost->st_gid = st->st_gid;
2080 ost->st_rdev = st->st_rdev;
2081 if (st->st_size < (quad_t)1 << 32)
2082 ost->st_size = st->st_size;
2085 ost->st_atim = st->st_atim;
2086 ost->st_mtim = st->st_mtim;
2087 ost->st_ctim = st->st_ctim;
2088 ost->st_blksize = st->st_blksize;
2089 ost->st_blocks = st->st_blocks;
2090 ost->st_flags = st->st_flags;
2091 ost->st_gen = st->st_gen;
2093 #endif /* COMPAT_43 */
2095 #if defined(COMPAT_43) || defined(COMPAT_FREEBSD11)
2096 int ino64_trunc_error;
2097 SYSCTL_INT(_vfs, OID_AUTO, ino64_trunc_error, CTLFLAG_RW,
2098 &ino64_trunc_error, 0,
2099 "Error on truncation of inode number, device id or link count");
2101 freebsd11_cvtstat(struct stat *st, struct freebsd11_stat *ost)
2104 ost->st_dev = st->st_dev;
2105 ost->st_ino = st->st_ino;
2106 if (ost->st_ino != st->st_ino) {
2107 switch (ino64_trunc_error) {
2114 ost->st_ino = UINT32_MAX;
2118 ost->st_mode = st->st_mode;
2119 ost->st_nlink = st->st_nlink;
2120 if (ost->st_nlink != st->st_nlink) {
2121 switch (ino64_trunc_error) {
2128 ost->st_nlink = UINT16_MAX;
2132 ost->st_uid = st->st_uid;
2133 ost->st_gid = st->st_gid;
2134 ost->st_rdev = st->st_rdev;
2135 ost->st_atim = st->st_atim;
2136 ost->st_mtim = st->st_mtim;
2137 ost->st_ctim = st->st_ctim;
2138 ost->st_size = st->st_size;
2139 ost->st_blocks = st->st_blocks;
2140 ost->st_blksize = st->st_blksize;
2141 ost->st_flags = st->st_flags;
2142 ost->st_gen = st->st_gen;
2144 ost->st_birthtim = st->st_birthtim;
2145 bzero((char *)&ost->st_birthtim + sizeof(ost->st_birthtim),
2146 sizeof(*ost) - offsetof(struct freebsd11_stat,
2147 st_birthtim) - sizeof(ost->st_birthtim));
2152 freebsd11_stat(struct thread *td, struct freebsd11_stat_args* uap)
2155 struct freebsd11_stat osb;
2158 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
2162 error = freebsd11_cvtstat(&sb, &osb);
2164 error = copyout(&osb, uap->ub, sizeof(osb));
2169 freebsd11_lstat(struct thread *td, struct freebsd11_lstat_args* uap)
2172 struct freebsd11_stat osb;
2175 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2176 UIO_USERSPACE, &sb, NULL);
2179 error = freebsd11_cvtstat(&sb, &osb);
2181 error = copyout(&osb, uap->ub, sizeof(osb));
2186 freebsd11_fhstat(struct thread *td, struct freebsd11_fhstat_args* uap)
2190 struct freebsd11_stat osb;
2193 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
2196 error = kern_fhstat(td, fh, &sb);
2199 error = freebsd11_cvtstat(&sb, &osb);
2201 error = copyout(&osb, uap->sb, sizeof(osb));
2206 freebsd11_fstatat(struct thread *td, struct freebsd11_fstatat_args* uap)
2209 struct freebsd11_stat osb;
2212 error = kern_statat(td, uap->flag, uap->fd, uap->path,
2213 UIO_USERSPACE, &sb, NULL);
2216 error = freebsd11_cvtstat(&sb, &osb);
2218 error = copyout(&osb, uap->buf, sizeof(osb));
2221 #endif /* COMPAT_FREEBSD11 */
2226 #ifndef _SYS_SYSPROTO_H_
2227 struct fstatat_args {
2235 sys_fstatat(struct thread *td, struct fstatat_args *uap)
2240 error = kern_statat(td, uap->flag, uap->fd, uap->path,
2241 UIO_USERSPACE, &sb, NULL);
2243 error = copyout(&sb, uap->buf, sizeof (sb));
2248 kern_statat(struct thread *td, int flag, int fd, char *path,
2249 enum uio_seg pathseg, struct stat *sbp,
2250 void (*hook)(struct vnode *vp, struct stat *sbp))
2252 struct nameidata nd;
2256 if (flag & ~AT_SYMLINK_NOFOLLOW)
2259 NDINIT_ATRIGHTS(&nd, LOOKUP, ((flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW :
2260 FOLLOW) | LOCKSHARED | LOCKLEAF | AUDITVNODE1, pathseg, path, fd,
2261 &cap_fstat_rights, td);
2263 if ((error = namei(&nd)) != 0)
2265 error = vn_stat(nd.ni_vp, &sb, td->td_ucred, NOCRED, td);
2267 SDT_PROBE2(vfs, , stat, mode, path, sb.st_mode);
2268 if (S_ISREG(sb.st_mode))
2269 SDT_PROBE2(vfs, , stat, reg, path, pathseg);
2270 if (__predict_false(hook != NULL))
2271 hook(nd.ni_vp, &sb);
2273 NDFREE(&nd, NDF_ONLY_PNBUF);
2277 #ifdef __STAT_TIME_T_EXT
2285 if (KTRPOINT(td, KTR_STRUCT))
2291 #if defined(COMPAT_FREEBSD11)
2293 * Implementation of the NetBSD [l]stat() functions.
2296 freebsd11_cvtnstat(struct stat *sb, struct nstat *nsb)
2299 bzero(nsb, sizeof(*nsb));
2300 nsb->st_dev = sb->st_dev;
2301 nsb->st_ino = sb->st_ino;
2302 nsb->st_mode = sb->st_mode;
2303 nsb->st_nlink = sb->st_nlink;
2304 nsb->st_uid = sb->st_uid;
2305 nsb->st_gid = sb->st_gid;
2306 nsb->st_rdev = sb->st_rdev;
2307 nsb->st_atim = sb->st_atim;
2308 nsb->st_mtim = sb->st_mtim;
2309 nsb->st_ctim = sb->st_ctim;
2310 nsb->st_size = sb->st_size;
2311 nsb->st_blocks = sb->st_blocks;
2312 nsb->st_blksize = sb->st_blksize;
2313 nsb->st_flags = sb->st_flags;
2314 nsb->st_gen = sb->st_gen;
2315 nsb->st_birthtim = sb->st_birthtim;
2318 #ifndef _SYS_SYSPROTO_H_
2319 struct freebsd11_nstat_args {
2325 freebsd11_nstat(struct thread *td, struct freebsd11_nstat_args *uap)
2331 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
2335 freebsd11_cvtnstat(&sb, &nsb);
2336 return (copyout(&nsb, uap->ub, sizeof (nsb)));
2340 * NetBSD lstat. Get file status; this version does not follow links.
2342 #ifndef _SYS_SYSPROTO_H_
2343 struct freebsd11_nlstat_args {
2349 freebsd11_nlstat(struct thread *td, struct freebsd11_nlstat_args *uap)
2355 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2356 UIO_USERSPACE, &sb, NULL);
2359 freebsd11_cvtnstat(&sb, &nsb);
2360 return (copyout(&nsb, uap->ub, sizeof (nsb)));
2362 #endif /* COMPAT_FREEBSD11 */
2365 * Get configurable pathname variables.
2367 #ifndef _SYS_SYSPROTO_H_
2368 struct pathconf_args {
2374 sys_pathconf(struct thread *td, struct pathconf_args *uap)
2379 error = kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name, FOLLOW,
2382 td->td_retval[0] = value;
2386 #ifndef _SYS_SYSPROTO_H_
2387 struct lpathconf_args {
2393 sys_lpathconf(struct thread *td, struct lpathconf_args *uap)
2398 error = kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name,
2401 td->td_retval[0] = value;
2406 kern_pathconf(struct thread *td, char *path, enum uio_seg pathseg, int name,
2407 u_long flags, long *valuep)
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, valuep);
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;
2606 AUDIT_ARG_FFLAGS(flags);
2607 follow = (atflag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
2608 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, pathseg, path, fd,
2609 &cap_fchflags_rights, td);
2610 if ((error = namei(&nd)) != 0)
2612 NDFREE(&nd, NDF_ONLY_PNBUF);
2613 error = setfflags(td, nd.ni_vp, flags);
2619 * Change flags of a file given a file descriptor.
2621 #ifndef _SYS_SYSPROTO_H_
2622 struct fchflags_args {
2628 sys_fchflags(struct thread *td, struct fchflags_args *uap)
2633 AUDIT_ARG_FD(uap->fd);
2634 AUDIT_ARG_FFLAGS(uap->flags);
2635 error = getvnode(td, uap->fd, &cap_fchflags_rights,
2640 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
2641 AUDIT_ARG_VNODE1(fp->f_vnode);
2642 VOP_UNLOCK(fp->f_vnode, 0);
2644 error = setfflags(td, fp->f_vnode, uap->flags);
2650 * Common implementation code for chmod(), lchmod() and fchmod().
2653 setfmode(struct thread *td, struct ucred *cred, struct vnode *vp, int mode)
2659 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2661 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2663 vattr.va_mode = mode & ALLPERMS;
2665 error = mac_vnode_check_setmode(cred, vp, vattr.va_mode);
2668 error = VOP_SETATTR(vp, &vattr, cred);
2670 vn_finished_write(mp);
2675 * Change mode of a file given path name.
2677 #ifndef _SYS_SYSPROTO_H_
2684 sys_chmod(struct thread *td, struct chmod_args *uap)
2687 return (kern_fchmodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2691 #ifndef _SYS_SYSPROTO_H_
2692 struct fchmodat_args {
2700 sys_fchmodat(struct thread *td, struct fchmodat_args *uap)
2702 int flag = uap->flag;
2704 char *path = uap->path;
2705 mode_t mode = uap->mode;
2707 if (flag & ~AT_SYMLINK_NOFOLLOW)
2710 return (kern_fchmodat(td, fd, path, UIO_USERSPACE, mode, flag));
2714 * Change mode of a file given path name (don't follow links.)
2716 #ifndef _SYS_SYSPROTO_H_
2717 struct lchmod_args {
2723 sys_lchmod(struct thread *td, struct lchmod_args *uap)
2726 return (kern_fchmodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2727 uap->mode, AT_SYMLINK_NOFOLLOW));
2731 kern_fchmodat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2732 mode_t mode, int flag)
2734 struct nameidata nd;
2737 AUDIT_ARG_MODE(mode);
2738 follow = (flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
2739 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, pathseg, path, fd,
2740 &cap_fchmod_rights, td);
2741 if ((error = namei(&nd)) != 0)
2743 NDFREE(&nd, NDF_ONLY_PNBUF);
2744 error = setfmode(td, td->td_ucred, nd.ni_vp, mode);
2750 * Change mode of a file given a file descriptor.
2752 #ifndef _SYS_SYSPROTO_H_
2753 struct fchmod_args {
2759 sys_fchmod(struct thread *td, struct fchmod_args *uap)
2764 AUDIT_ARG_FD(uap->fd);
2765 AUDIT_ARG_MODE(uap->mode);
2767 error = fget(td, uap->fd, &cap_fchmod_rights, &fp);
2770 error = fo_chmod(fp, uap->mode, td->td_ucred, td);
2776 * Common implementation for chown(), lchown(), and fchown()
2779 setfown(struct thread *td, struct ucred *cred, struct vnode *vp, uid_t uid,
2786 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2788 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2793 error = mac_vnode_check_setowner(cred, vp, vattr.va_uid,
2797 error = VOP_SETATTR(vp, &vattr, cred);
2799 vn_finished_write(mp);
2804 * Set ownership given a path name.
2806 #ifndef _SYS_SYSPROTO_H_
2814 sys_chown(struct thread *td, struct chown_args *uap)
2817 return (kern_fchownat(td, AT_FDCWD, uap->path, UIO_USERSPACE, uap->uid,
2821 #ifndef _SYS_SYSPROTO_H_
2822 struct fchownat_args {
2831 sys_fchownat(struct thread *td, struct fchownat_args *uap)
2836 if (flag & ~AT_SYMLINK_NOFOLLOW)
2839 return (kern_fchownat(td, uap->fd, uap->path, UIO_USERSPACE, uap->uid,
2840 uap->gid, uap->flag));
2844 kern_fchownat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2845 int uid, int gid, int flag)
2847 struct nameidata nd;
2850 AUDIT_ARG_OWNER(uid, gid);
2851 follow = (flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
2852 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, pathseg, path, fd,
2853 &cap_fchown_rights, td);
2855 if ((error = namei(&nd)) != 0)
2857 NDFREE(&nd, NDF_ONLY_PNBUF);
2858 error = setfown(td, td->td_ucred, nd.ni_vp, uid, gid);
2864 * Set ownership given a path name, do not cross symlinks.
2866 #ifndef _SYS_SYSPROTO_H_
2867 struct lchown_args {
2874 sys_lchown(struct thread *td, struct lchown_args *uap)
2877 return (kern_fchownat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2878 uap->uid, uap->gid, AT_SYMLINK_NOFOLLOW));
2882 * Set ownership given a file descriptor.
2884 #ifndef _SYS_SYSPROTO_H_
2885 struct fchown_args {
2892 sys_fchown(struct thread *td, struct fchown_args *uap)
2897 AUDIT_ARG_FD(uap->fd);
2898 AUDIT_ARG_OWNER(uap->uid, uap->gid);
2899 error = fget(td, uap->fd, &cap_fchown_rights, &fp);
2902 error = fo_chown(fp, uap->uid, uap->gid, td->td_ucred, td);
2908 * Common implementation code for utimes(), lutimes(), and futimes().
2911 getutimes(const struct timeval *usrtvp, enum uio_seg tvpseg,
2912 struct timespec *tsp)
2914 struct timeval tv[2];
2915 const struct timeval *tvp;
2918 if (usrtvp == NULL) {
2919 vfs_timestamp(&tsp[0]);
2922 if (tvpseg == UIO_SYSSPACE) {
2925 if ((error = copyin(usrtvp, tv, sizeof(tv))) != 0)
2930 if (tvp[0].tv_usec < 0 || tvp[0].tv_usec >= 1000000 ||
2931 tvp[1].tv_usec < 0 || tvp[1].tv_usec >= 1000000)
2933 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
2934 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
2940 * Common implementation code for futimens(), utimensat().
2942 #define UTIMENS_NULL 0x1
2943 #define UTIMENS_EXIT 0x2
2945 getutimens(const struct timespec *usrtsp, enum uio_seg tspseg,
2946 struct timespec *tsp, int *retflags)
2948 struct timespec tsnow;
2951 vfs_timestamp(&tsnow);
2953 if (usrtsp == NULL) {
2956 *retflags |= UTIMENS_NULL;
2959 if (tspseg == UIO_SYSSPACE) {
2962 } else if ((error = copyin(usrtsp, tsp, sizeof(*tsp) * 2)) != 0)
2964 if (tsp[0].tv_nsec == UTIME_OMIT && tsp[1].tv_nsec == UTIME_OMIT)
2965 *retflags |= UTIMENS_EXIT;
2966 if (tsp[0].tv_nsec == UTIME_NOW && tsp[1].tv_nsec == UTIME_NOW)
2967 *retflags |= UTIMENS_NULL;
2968 if (tsp[0].tv_nsec == UTIME_OMIT)
2969 tsp[0].tv_sec = VNOVAL;
2970 else if (tsp[0].tv_nsec == UTIME_NOW)
2972 else if (tsp[0].tv_nsec < 0 || tsp[0].tv_nsec >= 1000000000L)
2974 if (tsp[1].tv_nsec == UTIME_OMIT)
2975 tsp[1].tv_sec = VNOVAL;
2976 else if (tsp[1].tv_nsec == UTIME_NOW)
2978 else if (tsp[1].tv_nsec < 0 || tsp[1].tv_nsec >= 1000000000L)
2985 * Common implementation code for utimes(), lutimes(), futimes(), futimens(),
2989 setutimes(struct thread *td, struct vnode *vp, const struct timespec *ts,
2990 int numtimes, int nullflag)
2994 int error, setbirthtime;
2996 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2998 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3000 if (numtimes < 3 && !VOP_GETATTR(vp, &vattr, td->td_ucred) &&
3001 timespeccmp(&ts[1], &vattr.va_birthtime, < ))
3004 vattr.va_atime = ts[0];
3005 vattr.va_mtime = ts[1];
3007 vattr.va_birthtime = ts[1];
3009 vattr.va_birthtime = ts[2];
3011 vattr.va_vaflags |= VA_UTIMES_NULL;
3013 error = mac_vnode_check_setutimes(td->td_ucred, vp, vattr.va_atime,
3017 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3019 vn_finished_write(mp);
3024 * Set the access and modification times of a file.
3026 #ifndef _SYS_SYSPROTO_H_
3027 struct utimes_args {
3029 struct timeval *tptr;
3033 sys_utimes(struct thread *td, struct utimes_args *uap)
3036 return (kern_utimesat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
3037 uap->tptr, UIO_USERSPACE));
3040 #ifndef _SYS_SYSPROTO_H_
3041 struct futimesat_args {
3044 const struct timeval * times;
3048 sys_futimesat(struct thread *td, struct futimesat_args *uap)
3051 return (kern_utimesat(td, uap->fd, uap->path, UIO_USERSPACE,
3052 uap->times, UIO_USERSPACE));
3056 kern_utimesat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
3057 struct timeval *tptr, enum uio_seg tptrseg)
3059 struct nameidata nd;
3060 struct timespec ts[2];
3063 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3065 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, fd,
3066 &cap_futimes_rights, td);
3068 if ((error = namei(&nd)) != 0)
3070 NDFREE(&nd, NDF_ONLY_PNBUF);
3071 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3077 * Set the access and modification times of a file.
3079 #ifndef _SYS_SYSPROTO_H_
3080 struct lutimes_args {
3082 struct timeval *tptr;
3086 sys_lutimes(struct thread *td, struct lutimes_args *uap)
3089 return (kern_lutimes(td, uap->path, UIO_USERSPACE, uap->tptr,
3094 kern_lutimes(struct thread *td, char *path, enum uio_seg pathseg,
3095 struct timeval *tptr, enum uio_seg tptrseg)
3097 struct timespec ts[2];
3098 struct nameidata nd;
3101 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3103 NDINIT(&nd, LOOKUP, NOFOLLOW | AUDITVNODE1, pathseg, path, td);
3104 if ((error = namei(&nd)) != 0)
3106 NDFREE(&nd, NDF_ONLY_PNBUF);
3107 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3113 * Set the access and modification times of a file.
3115 #ifndef _SYS_SYSPROTO_H_
3116 struct futimes_args {
3118 struct timeval *tptr;
3122 sys_futimes(struct thread *td, struct futimes_args *uap)
3125 return (kern_futimes(td, uap->fd, uap->tptr, UIO_USERSPACE));
3129 kern_futimes(struct thread *td, int fd, struct timeval *tptr,
3130 enum uio_seg tptrseg)
3132 struct timespec ts[2];
3137 error = getutimes(tptr, tptrseg, ts);
3140 error = getvnode(td, fd, &cap_futimes_rights, &fp);
3144 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
3145 AUDIT_ARG_VNODE1(fp->f_vnode);
3146 VOP_UNLOCK(fp->f_vnode, 0);
3148 error = setutimes(td, fp->f_vnode, ts, 2, tptr == NULL);
3154 sys_futimens(struct thread *td, struct futimens_args *uap)
3157 return (kern_futimens(td, uap->fd, uap->times, UIO_USERSPACE));
3161 kern_futimens(struct thread *td, int fd, struct timespec *tptr,
3162 enum uio_seg tptrseg)
3164 struct timespec ts[2];
3169 error = getutimens(tptr, tptrseg, ts, &flags);
3172 if (flags & UTIMENS_EXIT)
3174 error = getvnode(td, fd, &cap_futimes_rights, &fp);
3178 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
3179 AUDIT_ARG_VNODE1(fp->f_vnode);
3180 VOP_UNLOCK(fp->f_vnode, 0);
3182 error = setutimes(td, fp->f_vnode, ts, 2, flags & UTIMENS_NULL);
3188 sys_utimensat(struct thread *td, struct utimensat_args *uap)
3191 return (kern_utimensat(td, uap->fd, uap->path, UIO_USERSPACE,
3192 uap->times, UIO_USERSPACE, uap->flag));
3196 kern_utimensat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
3197 struct timespec *tptr, enum uio_seg tptrseg, int flag)
3199 struct nameidata nd;
3200 struct timespec ts[2];
3203 if (flag & ~AT_SYMLINK_NOFOLLOW)
3206 if ((error = getutimens(tptr, tptrseg, ts, &flags)) != 0)
3208 NDINIT_ATRIGHTS(&nd, LOOKUP, ((flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW :
3209 FOLLOW) | AUDITVNODE1, pathseg, path, fd,
3210 &cap_futimes_rights, td);
3211 if ((error = namei(&nd)) != 0)
3214 * We are allowed to call namei() regardless of 2xUTIME_OMIT.
3216 * "If both tv_nsec fields are UTIME_OMIT... EACCESS may be detected."
3217 * "Search permission is denied by a component of the path prefix."
3219 NDFREE(&nd, NDF_ONLY_PNBUF);
3220 if ((flags & UTIMENS_EXIT) == 0)
3221 error = setutimes(td, nd.ni_vp, ts, 2, flags & UTIMENS_NULL);
3227 * Truncate a file given its path name.
3229 #ifndef _SYS_SYSPROTO_H_
3230 struct truncate_args {
3237 sys_truncate(struct thread *td, struct truncate_args *uap)
3240 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3244 kern_truncate(struct thread *td, char *path, enum uio_seg pathseg, off_t length)
3250 struct nameidata nd;
3255 NDINIT(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, td);
3256 if ((error = namei(&nd)) != 0)
3259 rl_cookie = vn_rangelock_wlock(vp, 0, OFF_MAX);
3260 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0) {
3261 vn_rangelock_unlock(vp, rl_cookie);
3265 NDFREE(&nd, NDF_ONLY_PNBUF);
3266 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3267 if (vp->v_type == VDIR)
3270 else if ((error = mac_vnode_check_write(td->td_ucred, NOCRED, vp))) {
3273 else if ((error = vn_writechk(vp)) == 0 &&
3274 (error = VOP_ACCESS(vp, VWRITE, td->td_ucred, td)) == 0) {
3276 vattr.va_size = length;
3277 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3280 vn_finished_write(mp);
3281 vn_rangelock_unlock(vp, rl_cookie);
3286 #if defined(COMPAT_43)
3288 * Truncate a file given its path name.
3290 #ifndef _SYS_SYSPROTO_H_
3291 struct otruncate_args {
3297 otruncate(struct thread *td, struct otruncate_args *uap)
3300 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3302 #endif /* COMPAT_43 */
3304 #if defined(COMPAT_FREEBSD6)
3305 /* Versions with the pad argument */
3307 freebsd6_truncate(struct thread *td, struct freebsd6_truncate_args *uap)
3310 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3314 freebsd6_ftruncate(struct thread *td, struct freebsd6_ftruncate_args *uap)
3317 return (kern_ftruncate(td, uap->fd, uap->length));
3322 kern_fsync(struct thread *td, int fd, bool fullsync)
3327 int error, lock_flags;
3330 error = getvnode(td, fd, &cap_fsync_rights, &fp);
3336 /* XXXKIB: compete outstanding aio writes */;
3338 error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
3341 if (MNT_SHARED_WRITES(mp) ||
3342 ((mp == NULL) && MNT_SHARED_WRITES(vp->v_mount))) {
3343 lock_flags = LK_SHARED;
3345 lock_flags = LK_EXCLUSIVE;
3347 vn_lock(vp, lock_flags | LK_RETRY);
3348 AUDIT_ARG_VNODE1(vp);
3349 if (vp->v_object != NULL) {
3350 VM_OBJECT_WLOCK(vp->v_object);
3351 vm_object_page_clean(vp->v_object, 0, 0, 0);
3352 VM_OBJECT_WUNLOCK(vp->v_object);
3354 error = fullsync ? VOP_FSYNC(vp, MNT_WAIT, td) : VOP_FDATASYNC(vp, td);
3356 vn_finished_write(mp);
3363 * Sync an open file.
3365 #ifndef _SYS_SYSPROTO_H_
3371 sys_fsync(struct thread *td, struct fsync_args *uap)
3374 return (kern_fsync(td, uap->fd, true));
3378 sys_fdatasync(struct thread *td, struct fdatasync_args *uap)
3381 return (kern_fsync(td, uap->fd, false));
3385 * Rename files. Source and destination must either both be directories, or
3386 * both not be directories. If target is a directory, it must be empty.
3388 #ifndef _SYS_SYSPROTO_H_
3389 struct rename_args {
3395 sys_rename(struct thread *td, struct rename_args *uap)
3398 return (kern_renameat(td, AT_FDCWD, uap->from, AT_FDCWD,
3399 uap->to, UIO_USERSPACE));
3402 #ifndef _SYS_SYSPROTO_H_
3403 struct renameat_args {
3411 sys_renameat(struct thread *td, struct renameat_args *uap)
3414 return (kern_renameat(td, uap->oldfd, uap->old, uap->newfd, uap->new,
3419 kern_renameat(struct thread *td, int oldfd, char *old, int newfd, char *new,
3420 enum uio_seg pathseg)
3422 struct mount *mp = NULL;
3423 struct vnode *tvp, *fvp, *tdvp;
3424 struct nameidata fromnd, tond;
3430 NDINIT_ATRIGHTS(&fromnd, DELETE, LOCKPARENT | LOCKLEAF | SAVESTART |
3431 AUDITVNODE1, pathseg, old, oldfd,
3432 &cap_renameat_source_rights, td);
3434 NDINIT_ATRIGHTS(&fromnd, DELETE, WANTPARENT | SAVESTART | AUDITVNODE1,
3435 pathseg, old, oldfd,
3436 &cap_renameat_source_rights, td);
3439 if ((error = namei(&fromnd)) != 0)
3442 error = mac_vnode_check_rename_from(td->td_ucred, fromnd.ni_dvp,
3443 fromnd.ni_vp, &fromnd.ni_cnd);
3444 VOP_UNLOCK(fromnd.ni_dvp, 0);
3445 if (fromnd.ni_dvp != fromnd.ni_vp)
3446 VOP_UNLOCK(fromnd.ni_vp, 0);
3449 NDINIT_ATRIGHTS(&tond, RENAME, LOCKPARENT | LOCKLEAF | NOCACHE |
3450 SAVESTART | AUDITVNODE2, pathseg, new, newfd,
3451 &cap_renameat_target_rights, td);
3452 if (fromnd.ni_vp->v_type == VDIR)
3453 tond.ni_cnd.cn_flags |= WILLBEDIR;
3454 if ((error = namei(&tond)) != 0) {
3455 /* Translate error code for rename("dir1", "dir2/."). */
3456 if (error == EISDIR && fvp->v_type == VDIR)
3458 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3459 vrele(fromnd.ni_dvp);
3465 error = vn_start_write(fvp, &mp, V_NOWAIT);
3467 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3468 NDFREE(&tond, NDF_ONLY_PNBUF);
3475 vrele(fromnd.ni_dvp);
3477 vrele(tond.ni_startdir);
3478 if (fromnd.ni_startdir != NULL)
3479 vrele(fromnd.ni_startdir);
3480 error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH);
3486 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
3489 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
3494 if (newfd != AT_FDCWD) {
3496 * If the target already exists we require CAP_UNLINKAT
3499 error = cap_check(&tond.ni_filecaps.fc_rights,
3500 &cap_unlinkat_rights);
3511 * If the source is the same as the destination (that is, if they
3512 * are links to the same vnode), then there is nothing to do.
3518 error = mac_vnode_check_rename_to(td->td_ucred, tdvp,
3519 tond.ni_vp, fromnd.ni_dvp == tdvp, &tond.ni_cnd);
3523 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
3524 tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
3525 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3526 NDFREE(&tond, NDF_ONLY_PNBUF);
3528 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3529 NDFREE(&tond, NDF_ONLY_PNBUF);
3536 vrele(fromnd.ni_dvp);
3539 vrele(tond.ni_startdir);
3540 vn_finished_write(mp);
3542 if (fromnd.ni_startdir)
3543 vrele(fromnd.ni_startdir);
3550 * Make a directory file.
3552 #ifndef _SYS_SYSPROTO_H_
3559 sys_mkdir(struct thread *td, struct mkdir_args *uap)
3562 return (kern_mkdirat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
3566 #ifndef _SYS_SYSPROTO_H_
3567 struct mkdirat_args {
3574 sys_mkdirat(struct thread *td, struct mkdirat_args *uap)
3577 return (kern_mkdirat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode));
3581 kern_mkdirat(struct thread *td, int fd, char *path, enum uio_seg segflg,
3587 struct nameidata nd;
3590 AUDIT_ARG_MODE(mode);
3593 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
3594 NOCACHE, segflg, path, fd, &cap_mkdirat_rights,
3596 nd.ni_cnd.cn_flags |= WILLBEDIR;
3597 if ((error = namei(&nd)) != 0)
3601 NDFREE(&nd, NDF_ONLY_PNBUF);
3603 * XXX namei called with LOCKPARENT but not LOCKLEAF has
3604 * the strange behaviour of leaving the vnode unlocked
3605 * if the target is the same vnode as the parent.
3607 if (vp == nd.ni_dvp)
3614 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3615 NDFREE(&nd, NDF_ONLY_PNBUF);
3617 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3622 vattr.va_type = VDIR;
3623 vattr.va_mode = (mode & ACCESSPERMS) &~ td->td_proc->p_fd->fd_cmask;
3625 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
3630 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
3634 NDFREE(&nd, NDF_ONLY_PNBUF);
3638 vn_finished_write(mp);
3643 * Remove a directory file.
3645 #ifndef _SYS_SYSPROTO_H_
3651 sys_rmdir(struct thread *td, struct rmdir_args *uap)
3654 return (kern_rmdirat(td, AT_FDCWD, uap->path, UIO_USERSPACE));
3658 kern_rmdirat(struct thread *td, int fd, char *path, enum uio_seg pathseg)
3662 struct nameidata nd;
3667 NDINIT_ATRIGHTS(&nd, DELETE, LOCKPARENT | LOCKLEAF | AUDITVNODE1,
3668 pathseg, path, fd, &cap_unlinkat_rights, td);
3669 if ((error = namei(&nd)) != 0)
3672 if (vp->v_type != VDIR) {
3677 * No rmdir "." please.
3679 if (nd.ni_dvp == vp) {
3684 * The root of a mounted filesystem cannot be deleted.
3686 if (vp->v_vflag & VV_ROOT) {
3691 error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp,
3696 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3697 NDFREE(&nd, NDF_ONLY_PNBUF);
3699 if (nd.ni_dvp == vp)
3703 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3707 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_UNLINK);
3708 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
3709 vn_finished_write(mp);
3711 NDFREE(&nd, NDF_ONLY_PNBUF);
3713 if (nd.ni_dvp == vp)
3720 #if defined(COMPAT_43) || defined(COMPAT_FREEBSD11)
3722 freebsd11_kern_getdirentries(struct thread *td, int fd, char *ubuf, u_int count,
3723 long *basep, void (*func)(struct freebsd11_dirent *))
3725 struct freebsd11_dirent dstdp;
3726 struct dirent *dp, *edp;
3729 ssize_t resid, ucount;
3732 /* XXX arbitrary sanity limit on `count'. */
3733 count = min(count, 64 * 1024);
3735 dirbuf = malloc(count, M_TEMP, M_WAITOK);
3737 error = kern_getdirentries(td, fd, dirbuf, count, &base, &resid,
3745 for (dp = (struct dirent *)dirbuf,
3746 edp = (struct dirent *)&dirbuf[count - resid];
3747 ucount < count && dp < edp; ) {
3748 if (dp->d_reclen == 0)
3750 MPASS(dp->d_reclen >= _GENERIC_DIRLEN(0));
3751 if (dp->d_namlen >= sizeof(dstdp.d_name))
3753 dstdp.d_type = dp->d_type;
3754 dstdp.d_namlen = dp->d_namlen;
3755 dstdp.d_fileno = dp->d_fileno; /* truncate */
3756 if (dstdp.d_fileno != dp->d_fileno) {
3757 switch (ino64_trunc_error) {
3765 dstdp.d_fileno = UINT32_MAX;
3769 dstdp.d_reclen = sizeof(dstdp) - sizeof(dstdp.d_name) +
3770 ((dp->d_namlen + 1 + 3) &~ 3);
3771 bcopy(dp->d_name, dstdp.d_name, dstdp.d_namlen);
3772 bzero(dstdp.d_name + dstdp.d_namlen,
3773 dstdp.d_reclen - offsetof(struct freebsd11_dirent, d_name) -
3775 MPASS(dstdp.d_reclen <= dp->d_reclen);
3776 MPASS(ucount + dstdp.d_reclen <= count);
3779 error = copyout(&dstdp, ubuf + ucount, dstdp.d_reclen);
3782 dp = (struct dirent *)((char *)dp + dp->d_reclen);
3783 ucount += dstdp.d_reclen;
3787 free(dirbuf, M_TEMP);
3789 td->td_retval[0] = ucount;
3796 ogetdirentries_cvt(struct freebsd11_dirent *dp)
3798 #if (BYTE_ORDER == LITTLE_ENDIAN)
3800 * The expected low byte of dp->d_namlen is our dp->d_type.
3801 * The high MBZ byte of dp->d_namlen is our dp->d_namlen.
3803 dp->d_type = dp->d_namlen;
3807 * The dp->d_type is the high byte of the expected dp->d_namlen,
3808 * so must be zero'ed.
3815 * Read a block of directory entries in a filesystem independent format.
3817 #ifndef _SYS_SYSPROTO_H_
3818 struct ogetdirentries_args {
3826 ogetdirentries(struct thread *td, struct ogetdirentries_args *uap)
3831 error = kern_ogetdirentries(td, uap, &loff);
3833 error = copyout(&loff, uap->basep, sizeof(long));
3838 kern_ogetdirentries(struct thread *td, struct ogetdirentries_args *uap,
3844 /* XXX arbitrary sanity limit on `count'. */
3845 if (uap->count > 64 * 1024)
3848 error = freebsd11_kern_getdirentries(td, uap->fd, uap->buf, uap->count,
3849 &base, ogetdirentries_cvt);
3851 if (error == 0 && uap->basep != NULL)
3852 error = copyout(&base, uap->basep, sizeof(long));
3856 #endif /* COMPAT_43 */
3858 #if defined(COMPAT_FREEBSD11)
3859 #ifndef _SYS_SYSPROTO_H_
3860 struct freebsd11_getdirentries_args {
3868 freebsd11_getdirentries(struct thread *td,
3869 struct freebsd11_getdirentries_args *uap)
3874 error = freebsd11_kern_getdirentries(td, uap->fd, uap->buf, uap->count,
3877 if (error == 0 && uap->basep != NULL)
3878 error = copyout(&base, uap->basep, sizeof(long));
3883 freebsd11_getdents(struct thread *td, struct freebsd11_getdents_args *uap)
3885 struct freebsd11_getdirentries_args ap;
3889 ap.count = uap->count;
3891 return (freebsd11_getdirentries(td, &ap));
3893 #endif /* COMPAT_FREEBSD11 */
3896 * Read a block of directory entries in a filesystem independent format.
3899 sys_getdirentries(struct thread *td, struct getdirentries_args *uap)
3904 error = kern_getdirentries(td, uap->fd, uap->buf, uap->count, &base,
3905 NULL, UIO_USERSPACE);
3908 if (uap->basep != NULL)
3909 error = copyout(&base, uap->basep, sizeof(off_t));
3914 kern_getdirentries(struct thread *td, int fd, char *buf, size_t count,
3915 off_t *basep, ssize_t *residp, enum uio_seg bufseg)
3926 if (count > IOSIZE_MAX)
3928 auio.uio_resid = count;
3929 error = getvnode(td, fd, &cap_read_rights, &fp);
3932 if ((fp->f_flag & FREAD) == 0) {
3937 foffset = foffset_lock(fp, 0);
3939 if (vp->v_type != VDIR) {
3943 aiov.iov_base = buf;
3944 aiov.iov_len = count;
3945 auio.uio_iov = &aiov;
3946 auio.uio_iovcnt = 1;
3947 auio.uio_rw = UIO_READ;
3948 auio.uio_segflg = bufseg;
3950 vn_lock(vp, LK_SHARED | LK_RETRY);
3951 AUDIT_ARG_VNODE1(vp);
3952 loff = auio.uio_offset = foffset;
3954 error = mac_vnode_check_readdir(td->td_ucred, vp);
3957 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL,
3959 foffset = auio.uio_offset;
3964 if (count == auio.uio_resid &&
3965 (vp->v_vflag & VV_ROOT) &&
3966 (vp->v_mount->mnt_flag & MNT_UNION)) {
3967 struct vnode *tvp = vp;
3969 vp = vp->v_mount->mnt_vnodecovered;
3980 *residp = auio.uio_resid;
3981 td->td_retval[0] = count - auio.uio_resid;
3983 foffset_unlock(fp, foffset, 0);
3989 * Set the mode mask for creation of filesystem nodes.
3991 #ifndef _SYS_SYSPROTO_H_
3997 sys_umask(struct thread *td, struct umask_args *uap)
3999 struct filedesc *fdp;
4001 fdp = td->td_proc->p_fd;
4002 FILEDESC_XLOCK(fdp);
4003 td->td_retval[0] = fdp->fd_cmask;
4004 fdp->fd_cmask = uap->newmask & ALLPERMS;
4005 FILEDESC_XUNLOCK(fdp);
4010 * Void all references to file by ripping underlying filesystem away from
4013 #ifndef _SYS_SYSPROTO_H_
4014 struct revoke_args {
4019 sys_revoke(struct thread *td, struct revoke_args *uap)
4023 struct nameidata nd;
4026 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
4028 if ((error = namei(&nd)) != 0)
4031 NDFREE(&nd, NDF_ONLY_PNBUF);
4032 if (vp->v_type != VCHR || vp->v_rdev == NULL) {
4037 error = mac_vnode_check_revoke(td->td_ucred, vp);
4041 error = VOP_GETATTR(vp, &vattr, td->td_ucred);
4044 if (td->td_ucred->cr_uid != vattr.va_uid) {
4045 error = priv_check(td, PRIV_VFS_ADMIN);
4050 VOP_REVOKE(vp, REVOKEALL);
4057 * Convert a user file descriptor to a kernel file entry and check that, if it
4058 * is a capability, the correct rights are present. A reference on the file
4059 * entry is held upon returning.
4062 getvnode(struct thread *td, int fd, cap_rights_t *rightsp, struct file **fpp)
4067 error = fget_unlocked(td->td_proc->p_fd, fd, rightsp, &fp, NULL);
4072 * The file could be not of the vnode type, or it may be not
4073 * yet fully initialized, in which case the f_vnode pointer
4074 * may be set, but f_ops is still badfileops. E.g.,
4075 * devfs_open() transiently create such situation to
4076 * facilitate csw d_fdopen().
4078 * Dupfdopen() handling in kern_openat() installs the
4079 * half-baked file into the process descriptor table, allowing
4080 * other thread to dereference it. Guard against the race by
4083 if (fp->f_vnode == NULL || fp->f_ops == &badfileops) {
4093 * Get an (NFS) file handle.
4095 #ifndef _SYS_SYSPROTO_H_
4096 struct lgetfh_args {
4102 sys_lgetfh(struct thread *td, struct lgetfh_args *uap)
4104 struct nameidata nd;
4109 error = priv_check(td, PRIV_VFS_GETFH);
4112 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
4117 NDFREE(&nd, NDF_ONLY_PNBUF);
4119 bzero(&fh, sizeof(fh));
4120 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
4121 error = VOP_VPTOFH(vp, &fh.fh_fid);
4124 error = copyout(&fh, uap->fhp, sizeof (fh));
4128 #ifndef _SYS_SYSPROTO_H_
4135 sys_getfh(struct thread *td, struct getfh_args *uap)
4137 struct nameidata nd;
4142 error = priv_check(td, PRIV_VFS_GETFH);
4145 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
4150 NDFREE(&nd, NDF_ONLY_PNBUF);
4152 bzero(&fh, sizeof(fh));
4153 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
4154 error = VOP_VPTOFH(vp, &fh.fh_fid);
4157 error = copyout(&fh, uap->fhp, sizeof (fh));
4162 * syscall for the rpc.lockd to use to translate a NFS file handle into an
4165 * warning: do not remove the priv_check() call or this becomes one giant
4168 #ifndef _SYS_SYSPROTO_H_
4169 struct fhopen_args {
4170 const struct fhandle *u_fhp;
4175 sys_fhopen(struct thread *td, struct fhopen_args *uap)
4184 error = priv_check(td, PRIV_VFS_FHOPEN);
4188 fmode = FFLAGS(uap->flags);
4189 /* why not allow a non-read/write open for our lockd? */
4190 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
4192 error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
4195 /* find the mount point */
4196 mp = vfs_busyfs(&fhp.fh_fsid);
4199 /* now give me my vnode, it gets returned to me locked */
4200 error = VFS_FHTOVP(mp, &fhp.fh_fid, LK_EXCLUSIVE, &vp);
4205 error = falloc_noinstall(td, &fp);
4211 * An extra reference on `fp' has been held for us by
4212 * falloc_noinstall().
4218 error = vn_open_vnode(vp, fmode, td->td_ucred, td, fp);
4220 KASSERT(fp->f_ops == &badfileops,
4221 ("VOP_OPEN in fhopen() set f_ops"));
4222 KASSERT(td->td_dupfd < 0,
4223 ("fhopen() encountered fdopen()"));
4233 finit(fp, (fmode & FMASK) | (fp->f_flag & FHASLOCK), DTYPE_VNODE, vp,
4236 if ((fmode & O_TRUNC) != 0) {
4237 error = fo_truncate(fp, 0, td->td_ucred, td);
4242 error = finstall(td, fp, &indx, fmode, NULL);
4245 td->td_retval[0] = indx;
4250 * Stat an (NFS) file handle.
4252 #ifndef _SYS_SYSPROTO_H_
4253 struct fhstat_args {
4254 struct fhandle *u_fhp;
4259 sys_fhstat(struct thread *td, struct fhstat_args *uap)
4265 error = copyin(uap->u_fhp, &fh, sizeof(fh));
4268 error = kern_fhstat(td, fh, &sb);
4270 error = copyout(&sb, uap->sb, sizeof(sb));
4275 kern_fhstat(struct thread *td, struct fhandle fh, struct stat *sb)
4281 error = priv_check(td, PRIV_VFS_FHSTAT);
4284 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4286 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_EXCLUSIVE, &vp);
4290 error = vn_stat(vp, sb, td->td_ucred, NOCRED, td);
4296 * Implement fstatfs() for (NFS) file handles.
4298 #ifndef _SYS_SYSPROTO_H_
4299 struct fhstatfs_args {
4300 struct fhandle *u_fhp;
4305 sys_fhstatfs(struct thread *td, struct fhstatfs_args *uap)
4311 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4314 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
4315 error = kern_fhstatfs(td, fh, sfp);
4317 error = copyout(sfp, uap->buf, sizeof(*sfp));
4318 free(sfp, M_STATFS);
4323 kern_fhstatfs(struct thread *td, fhandle_t fh, struct statfs *buf)
4330 error = priv_check(td, PRIV_VFS_FHSTATFS);
4333 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4335 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_EXCLUSIVE, &vp);
4341 error = prison_canseemount(td->td_ucred, mp);
4345 error = mac_mount_check_stat(td->td_ucred, mp);
4350 * Set these in case the underlying filesystem fails to do so.
4353 sp->f_version = STATFS_VERSION;
4354 sp->f_namemax = NAME_MAX;
4355 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
4356 error = VFS_STATFS(mp, sp);
4365 kern_posix_fallocate(struct thread *td, int fd, off_t offset, off_t len)
4370 off_t olen, ooffset;
4373 int audited_vnode1 = 0;
4377 if (offset < 0 || len <= 0)
4379 /* Check for wrap. */
4380 if (offset > OFF_MAX - len)
4383 error = fget(td, fd, &cap_pwrite_rights, &fp);
4386 AUDIT_ARG_FILE(td->td_proc, fp);
4387 if ((fp->f_ops->fo_flags & DFLAG_SEEKABLE) == 0) {
4391 if ((fp->f_flag & FWRITE) == 0) {
4395 if (fp->f_type != DTYPE_VNODE) {
4400 if (vp->v_type != VREG) {
4405 /* Allocating blocks may take a long time, so iterate. */
4412 error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
4415 error = vn_lock(vp, LK_EXCLUSIVE);
4417 vn_finished_write(mp);
4421 if (!audited_vnode1) {
4422 AUDIT_ARG_VNODE1(vp);
4427 error = mac_vnode_check_write(td->td_ucred, fp->f_cred, vp);
4430 error = VOP_ALLOCATE(vp, &offset, &len);
4432 vn_finished_write(mp);
4434 if (olen + ooffset != offset + len) {
4435 panic("offset + len changed from %jx/%jx to %jx/%jx",
4436 ooffset, olen, offset, len);
4438 if (error != 0 || len == 0)
4440 KASSERT(olen > len, ("Iteration did not make progress?"));
4449 sys_posix_fallocate(struct thread *td, struct posix_fallocate_args *uap)
4453 error = kern_posix_fallocate(td, uap->fd, uap->offset, uap->len);
4454 return (kern_posix_error(td, error));
4458 * Unlike madvise(2), we do not make a best effort to remember every
4459 * possible caching hint. Instead, we remember the last setting with
4460 * the exception that we will allow POSIX_FADV_NORMAL to adjust the
4461 * region of any current setting.
4464 kern_posix_fadvise(struct thread *td, int fd, off_t offset, off_t len,
4467 struct fadvise_info *fa, *new;
4473 if (offset < 0 || len < 0 || offset > OFF_MAX - len)
4475 AUDIT_ARG_VALUE(advice);
4477 case POSIX_FADV_SEQUENTIAL:
4478 case POSIX_FADV_RANDOM:
4479 case POSIX_FADV_NOREUSE:
4480 new = malloc(sizeof(*fa), M_FADVISE, M_WAITOK);
4482 case POSIX_FADV_NORMAL:
4483 case POSIX_FADV_WILLNEED:
4484 case POSIX_FADV_DONTNEED:
4490 /* XXX: CAP_POSIX_FADVISE? */
4492 error = fget(td, fd, &cap_no_rights, &fp);
4495 AUDIT_ARG_FILE(td->td_proc, fp);
4496 if ((fp->f_ops->fo_flags & DFLAG_SEEKABLE) == 0) {
4500 if (fp->f_type != DTYPE_VNODE) {
4505 if (vp->v_type != VREG) {
4512 end = offset + len - 1;
4514 case POSIX_FADV_SEQUENTIAL:
4515 case POSIX_FADV_RANDOM:
4516 case POSIX_FADV_NOREUSE:
4518 * Try to merge any existing non-standard region with
4519 * this new region if possible, otherwise create a new
4520 * non-standard region for this request.
4522 mtx_pool_lock(mtxpool_sleep, fp);
4524 if (fa != NULL && fa->fa_advice == advice &&
4525 ((fa->fa_start <= end && fa->fa_end >= offset) ||
4526 (end != OFF_MAX && fa->fa_start == end + 1) ||
4527 (fa->fa_end != OFF_MAX && fa->fa_end + 1 == offset))) {
4528 if (offset < fa->fa_start)
4529 fa->fa_start = offset;
4530 if (end > fa->fa_end)
4533 new->fa_advice = advice;
4534 new->fa_start = offset;
4539 mtx_pool_unlock(mtxpool_sleep, fp);
4541 case POSIX_FADV_NORMAL:
4543 * If a the "normal" region overlaps with an existing
4544 * non-standard region, trim or remove the
4545 * non-standard region.
4547 mtx_pool_lock(mtxpool_sleep, fp);
4550 if (offset <= fa->fa_start && end >= fa->fa_end) {
4552 fp->f_advice = NULL;
4553 } else if (offset <= fa->fa_start &&
4554 end >= fa->fa_start)
4555 fa->fa_start = end + 1;
4556 else if (offset <= fa->fa_end && end >= fa->fa_end)
4557 fa->fa_end = offset - 1;
4558 else if (offset >= fa->fa_start && end <= fa->fa_end) {
4560 * If the "normal" region is a middle
4561 * portion of the existing
4562 * non-standard region, just remove
4563 * the whole thing rather than picking
4564 * one side or the other to
4568 fp->f_advice = NULL;
4571 mtx_pool_unlock(mtxpool_sleep, fp);
4573 case POSIX_FADV_WILLNEED:
4574 case POSIX_FADV_DONTNEED:
4575 error = VOP_ADVISE(vp, offset, end, advice);
4581 free(new, M_FADVISE);
4586 sys_posix_fadvise(struct thread *td, struct posix_fadvise_args *uap)
4590 error = kern_posix_fadvise(td, uap->fd, uap->offset, uap->len,
4592 return (kern_posix_error(td, error));