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)
2390 error = kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name, FOLLOW,
2393 td->td_retval[0] = value;
2397 #ifndef _SYS_SYSPROTO_H_
2398 struct lpathconf_args {
2404 sys_lpathconf(struct thread *td, struct lpathconf_args *uap)
2409 error = kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name,
2412 td->td_retval[0] = value;
2417 kern_pathconf(struct thread *td, char *path, enum uio_seg pathseg, int name,
2418 u_long flags, long *valuep)
2420 struct nameidata nd;
2423 NDINIT(&nd, LOOKUP, LOCKSHARED | LOCKLEAF | AUDITVNODE1 | flags,
2425 if ((error = namei(&nd)) != 0)
2427 NDFREE(&nd, NDF_ONLY_PNBUF);
2429 error = VOP_PATHCONF(nd.ni_vp, name, valuep);
2435 * Return target name of a symbolic link.
2437 #ifndef _SYS_SYSPROTO_H_
2438 struct readlink_args {
2445 sys_readlink(struct thread *td, struct readlink_args *uap)
2448 return (kern_readlinkat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2449 uap->buf, UIO_USERSPACE, uap->count));
2451 #ifndef _SYS_SYSPROTO_H_
2452 struct readlinkat_args {
2460 sys_readlinkat(struct thread *td, struct readlinkat_args *uap)
2463 return (kern_readlinkat(td, uap->fd, uap->path, UIO_USERSPACE,
2464 uap->buf, UIO_USERSPACE, uap->bufsize));
2468 kern_readlinkat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2469 char *buf, enum uio_seg bufseg, size_t count)
2474 struct nameidata nd;
2477 if (count > IOSIZE_MAX)
2480 NDINIT_AT(&nd, LOOKUP, NOFOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
2481 pathseg, path, fd, td);
2483 if ((error = namei(&nd)) != 0)
2485 NDFREE(&nd, NDF_ONLY_PNBUF);
2488 error = mac_vnode_check_readlink(td->td_ucred, vp);
2494 if (vp->v_type != VLNK && (vp->v_vflag & VV_READLINK) == 0)
2497 aiov.iov_base = buf;
2498 aiov.iov_len = count;
2499 auio.uio_iov = &aiov;
2500 auio.uio_iovcnt = 1;
2501 auio.uio_offset = 0;
2502 auio.uio_rw = UIO_READ;
2503 auio.uio_segflg = bufseg;
2505 auio.uio_resid = count;
2506 error = VOP_READLINK(vp, &auio, td->td_ucred);
2507 td->td_retval[0] = count - auio.uio_resid;
2514 * Common implementation code for chflags() and fchflags().
2517 setfflags(struct thread *td, struct vnode *vp, u_long flags)
2523 /* We can't support the value matching VNOVAL. */
2524 if (flags == VNOVAL)
2525 return (EOPNOTSUPP);
2528 * Prevent non-root users from setting flags on devices. When
2529 * a device is reused, users can retain ownership of the device
2530 * if they are allowed to set flags and programs assume that
2531 * chown can't fail when done as root.
2533 if (vp->v_type == VCHR || vp->v_type == VBLK) {
2534 error = priv_check(td, PRIV_VFS_CHFLAGS_DEV);
2539 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2542 vattr.va_flags = flags;
2543 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2545 error = mac_vnode_check_setflags(td->td_ucred, vp, vattr.va_flags);
2548 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
2550 vn_finished_write(mp);
2555 * Change flags of a file given a path name.
2557 #ifndef _SYS_SYSPROTO_H_
2558 struct chflags_args {
2564 sys_chflags(struct thread *td, struct chflags_args *uap)
2567 return (kern_chflagsat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2571 #ifndef _SYS_SYSPROTO_H_
2572 struct chflagsat_args {
2580 sys_chflagsat(struct thread *td, struct chflagsat_args *uap)
2583 const char *path = uap->path;
2584 u_long flags = uap->flags;
2585 int atflag = uap->atflag;
2587 if (atflag & ~AT_SYMLINK_NOFOLLOW)
2590 return (kern_chflagsat(td, fd, path, UIO_USERSPACE, flags, atflag));
2594 * Same as chflags() but doesn't follow symlinks.
2596 #ifndef _SYS_SYSPROTO_H_
2597 struct lchflags_args {
2603 sys_lchflags(struct thread *td, struct lchflags_args *uap)
2606 return (kern_chflagsat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2607 uap->flags, AT_SYMLINK_NOFOLLOW));
2611 kern_chflagsat(struct thread *td, int fd, const char *path,
2612 enum uio_seg pathseg, u_long flags, int atflag)
2614 struct nameidata nd;
2615 cap_rights_t rights;
2618 AUDIT_ARG_FFLAGS(flags);
2619 follow = (atflag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
2620 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, pathseg, path, fd,
2621 cap_rights_init(&rights, CAP_FCHFLAGS), td);
2622 if ((error = namei(&nd)) != 0)
2624 NDFREE(&nd, NDF_ONLY_PNBUF);
2625 error = setfflags(td, nd.ni_vp, flags);
2631 * Change flags of a file given a file descriptor.
2633 #ifndef _SYS_SYSPROTO_H_
2634 struct fchflags_args {
2640 sys_fchflags(struct thread *td, struct fchflags_args *uap)
2643 cap_rights_t rights;
2646 AUDIT_ARG_FD(uap->fd);
2647 AUDIT_ARG_FFLAGS(uap->flags);
2648 error = getvnode(td, uap->fd, cap_rights_init(&rights, CAP_FCHFLAGS),
2653 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
2654 AUDIT_ARG_VNODE1(fp->f_vnode);
2655 VOP_UNLOCK(fp->f_vnode, 0);
2657 error = setfflags(td, fp->f_vnode, uap->flags);
2663 * Common implementation code for chmod(), lchmod() and fchmod().
2666 setfmode(struct thread *td, struct ucred *cred, struct vnode *vp, int mode)
2672 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2674 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2676 vattr.va_mode = mode & ALLPERMS;
2678 error = mac_vnode_check_setmode(cred, vp, vattr.va_mode);
2681 error = VOP_SETATTR(vp, &vattr, cred);
2683 vn_finished_write(mp);
2688 * Change mode of a file given path name.
2690 #ifndef _SYS_SYSPROTO_H_
2697 sys_chmod(struct thread *td, struct chmod_args *uap)
2700 return (kern_fchmodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2704 #ifndef _SYS_SYSPROTO_H_
2705 struct fchmodat_args {
2713 sys_fchmodat(struct thread *td, struct fchmodat_args *uap)
2715 int flag = uap->flag;
2717 char *path = uap->path;
2718 mode_t mode = uap->mode;
2720 if (flag & ~AT_SYMLINK_NOFOLLOW)
2723 return (kern_fchmodat(td, fd, path, UIO_USERSPACE, mode, flag));
2727 * Change mode of a file given path name (don't follow links.)
2729 #ifndef _SYS_SYSPROTO_H_
2730 struct lchmod_args {
2736 sys_lchmod(struct thread *td, struct lchmod_args *uap)
2739 return (kern_fchmodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2740 uap->mode, AT_SYMLINK_NOFOLLOW));
2744 kern_fchmodat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2745 mode_t mode, int flag)
2747 struct nameidata nd;
2748 cap_rights_t rights;
2751 AUDIT_ARG_MODE(mode);
2752 follow = (flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
2753 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, pathseg, path, fd,
2754 cap_rights_init(&rights, CAP_FCHMOD), td);
2755 if ((error = namei(&nd)) != 0)
2757 NDFREE(&nd, NDF_ONLY_PNBUF);
2758 error = setfmode(td, td->td_ucred, nd.ni_vp, mode);
2764 * Change mode of a file given a file descriptor.
2766 #ifndef _SYS_SYSPROTO_H_
2767 struct fchmod_args {
2773 sys_fchmod(struct thread *td, struct fchmod_args *uap)
2776 cap_rights_t rights;
2779 AUDIT_ARG_FD(uap->fd);
2780 AUDIT_ARG_MODE(uap->mode);
2782 error = fget(td, uap->fd, cap_rights_init(&rights, CAP_FCHMOD), &fp);
2785 error = fo_chmod(fp, uap->mode, td->td_ucred, td);
2791 * Common implementation for chown(), lchown(), and fchown()
2794 setfown(struct thread *td, struct ucred *cred, struct vnode *vp, uid_t uid,
2801 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2803 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2808 error = mac_vnode_check_setowner(cred, vp, vattr.va_uid,
2812 error = VOP_SETATTR(vp, &vattr, cred);
2814 vn_finished_write(mp);
2819 * Set ownership given a path name.
2821 #ifndef _SYS_SYSPROTO_H_
2829 sys_chown(struct thread *td, struct chown_args *uap)
2832 return (kern_fchownat(td, AT_FDCWD, uap->path, UIO_USERSPACE, uap->uid,
2836 #ifndef _SYS_SYSPROTO_H_
2837 struct fchownat_args {
2846 sys_fchownat(struct thread *td, struct fchownat_args *uap)
2851 if (flag & ~AT_SYMLINK_NOFOLLOW)
2854 return (kern_fchownat(td, uap->fd, uap->path, UIO_USERSPACE, uap->uid,
2855 uap->gid, uap->flag));
2859 kern_fchownat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2860 int uid, int gid, int flag)
2862 struct nameidata nd;
2863 cap_rights_t rights;
2866 AUDIT_ARG_OWNER(uid, gid);
2867 follow = (flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
2868 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, pathseg, path, fd,
2869 cap_rights_init(&rights, CAP_FCHOWN), td);
2871 if ((error = namei(&nd)) != 0)
2873 NDFREE(&nd, NDF_ONLY_PNBUF);
2874 error = setfown(td, td->td_ucred, nd.ni_vp, uid, gid);
2880 * Set ownership given a path name, do not cross symlinks.
2882 #ifndef _SYS_SYSPROTO_H_
2883 struct lchown_args {
2890 sys_lchown(struct thread *td, struct lchown_args *uap)
2893 return (kern_fchownat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2894 uap->uid, uap->gid, AT_SYMLINK_NOFOLLOW));
2898 * Set ownership given a file descriptor.
2900 #ifndef _SYS_SYSPROTO_H_
2901 struct fchown_args {
2908 sys_fchown(struct thread *td, struct fchown_args *uap)
2911 cap_rights_t rights;
2914 AUDIT_ARG_FD(uap->fd);
2915 AUDIT_ARG_OWNER(uap->uid, uap->gid);
2916 error = fget(td, uap->fd, cap_rights_init(&rights, CAP_FCHOWN), &fp);
2919 error = fo_chown(fp, uap->uid, uap->gid, td->td_ucred, td);
2925 * Common implementation code for utimes(), lutimes(), and futimes().
2928 getutimes(const struct timeval *usrtvp, enum uio_seg tvpseg,
2929 struct timespec *tsp)
2931 struct timeval tv[2];
2932 const struct timeval *tvp;
2935 if (usrtvp == NULL) {
2936 vfs_timestamp(&tsp[0]);
2939 if (tvpseg == UIO_SYSSPACE) {
2942 if ((error = copyin(usrtvp, tv, sizeof(tv))) != 0)
2947 if (tvp[0].tv_usec < 0 || tvp[0].tv_usec >= 1000000 ||
2948 tvp[1].tv_usec < 0 || tvp[1].tv_usec >= 1000000)
2950 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
2951 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
2957 * Common implementation code for futimens(), utimensat().
2959 #define UTIMENS_NULL 0x1
2960 #define UTIMENS_EXIT 0x2
2962 getutimens(const struct timespec *usrtsp, enum uio_seg tspseg,
2963 struct timespec *tsp, int *retflags)
2965 struct timespec tsnow;
2968 vfs_timestamp(&tsnow);
2970 if (usrtsp == NULL) {
2973 *retflags |= UTIMENS_NULL;
2976 if (tspseg == UIO_SYSSPACE) {
2979 } else if ((error = copyin(usrtsp, tsp, sizeof(*tsp) * 2)) != 0)
2981 if (tsp[0].tv_nsec == UTIME_OMIT && tsp[1].tv_nsec == UTIME_OMIT)
2982 *retflags |= UTIMENS_EXIT;
2983 if (tsp[0].tv_nsec == UTIME_NOW && tsp[1].tv_nsec == UTIME_NOW)
2984 *retflags |= UTIMENS_NULL;
2985 if (tsp[0].tv_nsec == UTIME_OMIT)
2986 tsp[0].tv_sec = VNOVAL;
2987 else if (tsp[0].tv_nsec == UTIME_NOW)
2989 else if (tsp[0].tv_nsec < 0 || tsp[0].tv_nsec >= 1000000000L)
2991 if (tsp[1].tv_nsec == UTIME_OMIT)
2992 tsp[1].tv_sec = VNOVAL;
2993 else if (tsp[1].tv_nsec == UTIME_NOW)
2995 else if (tsp[1].tv_nsec < 0 || tsp[1].tv_nsec >= 1000000000L)
3002 * Common implementation code for utimes(), lutimes(), futimes(), futimens(),
3006 setutimes(struct thread *td, struct vnode *vp, const struct timespec *ts,
3007 int numtimes, int nullflag)
3011 int error, setbirthtime;
3013 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
3015 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3017 if (numtimes < 3 && !VOP_GETATTR(vp, &vattr, td->td_ucred) &&
3018 timespeccmp(&ts[1], &vattr.va_birthtime, < ))
3021 vattr.va_atime = ts[0];
3022 vattr.va_mtime = ts[1];
3024 vattr.va_birthtime = ts[1];
3026 vattr.va_birthtime = ts[2];
3028 vattr.va_vaflags |= VA_UTIMES_NULL;
3030 error = mac_vnode_check_setutimes(td->td_ucred, vp, vattr.va_atime,
3034 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3036 vn_finished_write(mp);
3041 * Set the access and modification times of a file.
3043 #ifndef _SYS_SYSPROTO_H_
3044 struct utimes_args {
3046 struct timeval *tptr;
3050 sys_utimes(struct thread *td, struct utimes_args *uap)
3053 return (kern_utimesat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
3054 uap->tptr, UIO_USERSPACE));
3057 #ifndef _SYS_SYSPROTO_H_
3058 struct futimesat_args {
3061 const struct timeval * times;
3065 sys_futimesat(struct thread *td, struct futimesat_args *uap)
3068 return (kern_utimesat(td, uap->fd, uap->path, UIO_USERSPACE,
3069 uap->times, UIO_USERSPACE));
3073 kern_utimesat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
3074 struct timeval *tptr, enum uio_seg tptrseg)
3076 struct nameidata nd;
3077 struct timespec ts[2];
3078 cap_rights_t rights;
3081 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3083 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, fd,
3084 cap_rights_init(&rights, CAP_FUTIMES), td);
3086 if ((error = namei(&nd)) != 0)
3088 NDFREE(&nd, NDF_ONLY_PNBUF);
3089 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3095 * Set the access and modification times of a file.
3097 #ifndef _SYS_SYSPROTO_H_
3098 struct lutimes_args {
3100 struct timeval *tptr;
3104 sys_lutimes(struct thread *td, struct lutimes_args *uap)
3107 return (kern_lutimes(td, uap->path, UIO_USERSPACE, uap->tptr,
3112 kern_lutimes(struct thread *td, char *path, enum uio_seg pathseg,
3113 struct timeval *tptr, enum uio_seg tptrseg)
3115 struct timespec ts[2];
3116 struct nameidata nd;
3119 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3121 NDINIT(&nd, LOOKUP, NOFOLLOW | AUDITVNODE1, pathseg, path, td);
3122 if ((error = namei(&nd)) != 0)
3124 NDFREE(&nd, NDF_ONLY_PNBUF);
3125 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3131 * Set the access and modification times of a file.
3133 #ifndef _SYS_SYSPROTO_H_
3134 struct futimes_args {
3136 struct timeval *tptr;
3140 sys_futimes(struct thread *td, struct futimes_args *uap)
3143 return (kern_futimes(td, uap->fd, uap->tptr, UIO_USERSPACE));
3147 kern_futimes(struct thread *td, int fd, struct timeval *tptr,
3148 enum uio_seg tptrseg)
3150 struct timespec ts[2];
3152 cap_rights_t rights;
3156 error = getutimes(tptr, tptrseg, ts);
3159 error = getvnode(td, fd, cap_rights_init(&rights, CAP_FUTIMES), &fp);
3163 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
3164 AUDIT_ARG_VNODE1(fp->f_vnode);
3165 VOP_UNLOCK(fp->f_vnode, 0);
3167 error = setutimes(td, fp->f_vnode, ts, 2, tptr == NULL);
3173 sys_futimens(struct thread *td, struct futimens_args *uap)
3176 return (kern_futimens(td, uap->fd, uap->times, UIO_USERSPACE));
3180 kern_futimens(struct thread *td, int fd, struct timespec *tptr,
3181 enum uio_seg tptrseg)
3183 struct timespec ts[2];
3185 cap_rights_t rights;
3189 error = getutimens(tptr, tptrseg, ts, &flags);
3192 if (flags & UTIMENS_EXIT)
3194 error = getvnode(td, fd, cap_rights_init(&rights, CAP_FUTIMES), &fp);
3198 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
3199 AUDIT_ARG_VNODE1(fp->f_vnode);
3200 VOP_UNLOCK(fp->f_vnode, 0);
3202 error = setutimes(td, fp->f_vnode, ts, 2, flags & UTIMENS_NULL);
3208 sys_utimensat(struct thread *td, struct utimensat_args *uap)
3211 return (kern_utimensat(td, uap->fd, uap->path, UIO_USERSPACE,
3212 uap->times, UIO_USERSPACE, uap->flag));
3216 kern_utimensat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
3217 struct timespec *tptr, enum uio_seg tptrseg, int flag)
3219 struct nameidata nd;
3220 struct timespec ts[2];
3221 cap_rights_t rights;
3224 if (flag & ~AT_SYMLINK_NOFOLLOW)
3227 if ((error = getutimens(tptr, tptrseg, ts, &flags)) != 0)
3229 NDINIT_ATRIGHTS(&nd, LOOKUP, ((flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW :
3230 FOLLOW) | AUDITVNODE1, pathseg, path, fd,
3231 cap_rights_init(&rights, CAP_FUTIMES), td);
3232 if ((error = namei(&nd)) != 0)
3235 * We are allowed to call namei() regardless of 2xUTIME_OMIT.
3237 * "If both tv_nsec fields are UTIME_OMIT... EACCESS may be detected."
3238 * "Search permission is denied by a component of the path prefix."
3240 NDFREE(&nd, NDF_ONLY_PNBUF);
3241 if ((flags & UTIMENS_EXIT) == 0)
3242 error = setutimes(td, nd.ni_vp, ts, 2, flags & UTIMENS_NULL);
3248 * Truncate a file given its path name.
3250 #ifndef _SYS_SYSPROTO_H_
3251 struct truncate_args {
3258 sys_truncate(struct thread *td, struct truncate_args *uap)
3261 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3265 kern_truncate(struct thread *td, char *path, enum uio_seg pathseg, off_t length)
3271 struct nameidata nd;
3276 NDINIT(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, td);
3277 if ((error = namei(&nd)) != 0)
3280 rl_cookie = vn_rangelock_wlock(vp, 0, OFF_MAX);
3281 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0) {
3282 vn_rangelock_unlock(vp, rl_cookie);
3286 NDFREE(&nd, NDF_ONLY_PNBUF);
3287 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3288 if (vp->v_type == VDIR)
3291 else if ((error = mac_vnode_check_write(td->td_ucred, NOCRED, vp))) {
3294 else if ((error = vn_writechk(vp)) == 0 &&
3295 (error = VOP_ACCESS(vp, VWRITE, td->td_ucred, td)) == 0) {
3297 vattr.va_size = length;
3298 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3301 vn_finished_write(mp);
3302 vn_rangelock_unlock(vp, rl_cookie);
3307 #if defined(COMPAT_43)
3309 * Truncate a file given its path name.
3311 #ifndef _SYS_SYSPROTO_H_
3312 struct otruncate_args {
3318 otruncate(struct thread *td, struct otruncate_args *uap)
3321 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3323 #endif /* COMPAT_43 */
3325 #if defined(COMPAT_FREEBSD6)
3326 /* Versions with the pad argument */
3328 freebsd6_truncate(struct thread *td, struct freebsd6_truncate_args *uap)
3331 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3335 freebsd6_ftruncate(struct thread *td, struct freebsd6_ftruncate_args *uap)
3338 return (kern_ftruncate(td, uap->fd, uap->length));
3343 kern_fsync(struct thread *td, int fd, bool fullsync)
3348 cap_rights_t rights;
3349 int error, lock_flags;
3352 error = getvnode(td, fd, cap_rights_init(&rights, CAP_FSYNC), &fp);
3358 /* XXXKIB: compete outstanding aio writes */;
3360 error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
3363 if (MNT_SHARED_WRITES(mp) ||
3364 ((mp == NULL) && MNT_SHARED_WRITES(vp->v_mount))) {
3365 lock_flags = LK_SHARED;
3367 lock_flags = LK_EXCLUSIVE;
3369 vn_lock(vp, lock_flags | LK_RETRY);
3370 AUDIT_ARG_VNODE1(vp);
3371 if (vp->v_object != NULL) {
3372 VM_OBJECT_WLOCK(vp->v_object);
3373 vm_object_page_clean(vp->v_object, 0, 0, 0);
3374 VM_OBJECT_WUNLOCK(vp->v_object);
3376 error = fullsync ? VOP_FSYNC(vp, MNT_WAIT, td) : VOP_FDATASYNC(vp, td);
3378 vn_finished_write(mp);
3385 * Sync an open file.
3387 #ifndef _SYS_SYSPROTO_H_
3393 sys_fsync(struct thread *td, struct fsync_args *uap)
3396 return (kern_fsync(td, uap->fd, true));
3400 sys_fdatasync(struct thread *td, struct fdatasync_args *uap)
3403 return (kern_fsync(td, uap->fd, false));
3407 * Rename files. Source and destination must either both be directories, or
3408 * both not be directories. If target is a directory, it must be empty.
3410 #ifndef _SYS_SYSPROTO_H_
3411 struct rename_args {
3417 sys_rename(struct thread *td, struct rename_args *uap)
3420 return (kern_renameat(td, AT_FDCWD, uap->from, AT_FDCWD,
3421 uap->to, UIO_USERSPACE));
3424 #ifndef _SYS_SYSPROTO_H_
3425 struct renameat_args {
3433 sys_renameat(struct thread *td, struct renameat_args *uap)
3436 return (kern_renameat(td, uap->oldfd, uap->old, uap->newfd, uap->new,
3441 kern_renameat(struct thread *td, int oldfd, char *old, int newfd, char *new,
3442 enum uio_seg pathseg)
3444 struct mount *mp = NULL;
3445 struct vnode *tvp, *fvp, *tdvp;
3446 struct nameidata fromnd, tond;
3447 cap_rights_t rights;
3453 NDINIT_ATRIGHTS(&fromnd, DELETE, LOCKPARENT | LOCKLEAF | SAVESTART |
3454 AUDITVNODE1, pathseg, old, oldfd,
3455 cap_rights_init(&rights, CAP_RENAMEAT_SOURCE), td);
3457 NDINIT_ATRIGHTS(&fromnd, DELETE, WANTPARENT | SAVESTART | AUDITVNODE1,
3458 pathseg, old, oldfd,
3459 cap_rights_init(&rights, CAP_RENAMEAT_SOURCE), td);
3462 if ((error = namei(&fromnd)) != 0)
3465 error = mac_vnode_check_rename_from(td->td_ucred, fromnd.ni_dvp,
3466 fromnd.ni_vp, &fromnd.ni_cnd);
3467 VOP_UNLOCK(fromnd.ni_dvp, 0);
3468 if (fromnd.ni_dvp != fromnd.ni_vp)
3469 VOP_UNLOCK(fromnd.ni_vp, 0);
3472 NDINIT_ATRIGHTS(&tond, RENAME, LOCKPARENT | LOCKLEAF | NOCACHE |
3473 SAVESTART | AUDITVNODE2, pathseg, new, newfd,
3474 cap_rights_init(&rights, CAP_RENAMEAT_TARGET), td);
3475 if (fromnd.ni_vp->v_type == VDIR)
3476 tond.ni_cnd.cn_flags |= WILLBEDIR;
3477 if ((error = namei(&tond)) != 0) {
3478 /* Translate error code for rename("dir1", "dir2/."). */
3479 if (error == EISDIR && fvp->v_type == VDIR)
3481 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3482 vrele(fromnd.ni_dvp);
3488 error = vn_start_write(fvp, &mp, V_NOWAIT);
3490 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3491 NDFREE(&tond, NDF_ONLY_PNBUF);
3498 vrele(fromnd.ni_dvp);
3500 vrele(tond.ni_startdir);
3501 if (fromnd.ni_startdir != NULL)
3502 vrele(fromnd.ni_startdir);
3503 error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH);
3509 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
3512 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
3517 if (newfd != AT_FDCWD) {
3519 * If the target already exists we require CAP_UNLINKAT
3522 error = cap_check(&tond.ni_filecaps.fc_rights,
3523 cap_rights_init(&rights, CAP_UNLINKAT));
3534 * If the source is the same as the destination (that is, if they
3535 * are links to the same vnode), then there is nothing to do.
3541 error = mac_vnode_check_rename_to(td->td_ucred, tdvp,
3542 tond.ni_vp, fromnd.ni_dvp == tdvp, &tond.ni_cnd);
3546 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
3547 tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
3548 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3549 NDFREE(&tond, NDF_ONLY_PNBUF);
3551 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3552 NDFREE(&tond, NDF_ONLY_PNBUF);
3559 vrele(fromnd.ni_dvp);
3562 vrele(tond.ni_startdir);
3563 vn_finished_write(mp);
3565 if (fromnd.ni_startdir)
3566 vrele(fromnd.ni_startdir);
3573 * Make a directory file.
3575 #ifndef _SYS_SYSPROTO_H_
3582 sys_mkdir(struct thread *td, struct mkdir_args *uap)
3585 return (kern_mkdirat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
3589 #ifndef _SYS_SYSPROTO_H_
3590 struct mkdirat_args {
3597 sys_mkdirat(struct thread *td, struct mkdirat_args *uap)
3600 return (kern_mkdirat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode));
3604 kern_mkdirat(struct thread *td, int fd, char *path, enum uio_seg segflg,
3610 struct nameidata nd;
3611 cap_rights_t rights;
3614 AUDIT_ARG_MODE(mode);
3617 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
3618 NOCACHE, segflg, path, fd, cap_rights_init(&rights, CAP_MKDIRAT),
3620 nd.ni_cnd.cn_flags |= WILLBEDIR;
3621 if ((error = namei(&nd)) != 0)
3625 NDFREE(&nd, NDF_ONLY_PNBUF);
3627 * XXX namei called with LOCKPARENT but not LOCKLEAF has
3628 * the strange behaviour of leaving the vnode unlocked
3629 * if the target is the same vnode as the parent.
3631 if (vp == nd.ni_dvp)
3638 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3639 NDFREE(&nd, NDF_ONLY_PNBUF);
3641 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3646 vattr.va_type = VDIR;
3647 vattr.va_mode = (mode & ACCESSPERMS) &~ td->td_proc->p_fd->fd_cmask;
3649 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
3654 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
3658 NDFREE(&nd, NDF_ONLY_PNBUF);
3662 vn_finished_write(mp);
3667 * Remove a directory file.
3669 #ifndef _SYS_SYSPROTO_H_
3675 sys_rmdir(struct thread *td, struct rmdir_args *uap)
3678 return (kern_rmdirat(td, AT_FDCWD, uap->path, UIO_USERSPACE));
3682 kern_rmdirat(struct thread *td, int fd, char *path, enum uio_seg pathseg)
3686 struct nameidata nd;
3687 cap_rights_t rights;
3692 NDINIT_ATRIGHTS(&nd, DELETE, LOCKPARENT | LOCKLEAF | AUDITVNODE1,
3693 pathseg, path, fd, cap_rights_init(&rights, CAP_UNLINKAT), td);
3694 if ((error = namei(&nd)) != 0)
3697 if (vp->v_type != VDIR) {
3702 * No rmdir "." please.
3704 if (nd.ni_dvp == vp) {
3709 * The root of a mounted filesystem cannot be deleted.
3711 if (vp->v_vflag & VV_ROOT) {
3716 error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp,
3721 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3722 NDFREE(&nd, NDF_ONLY_PNBUF);
3724 if (nd.ni_dvp == vp)
3728 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3732 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_UNLINK);
3733 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
3734 vn_finished_write(mp);
3736 NDFREE(&nd, NDF_ONLY_PNBUF);
3738 if (nd.ni_dvp == vp)
3745 #if defined(COMPAT_43) || defined(COMPAT_FREEBSD11)
3747 freebsd11_kern_getdirentries(struct thread *td, int fd, char *ubuf, u_int count,
3748 long *basep, void (*func)(struct freebsd11_dirent *))
3750 struct freebsd11_dirent dstdp;
3751 struct dirent *dp, *edp;
3754 ssize_t resid, ucount;
3757 /* XXX arbitrary sanity limit on `count'. */
3758 count = min(count, 64 * 1024);
3760 dirbuf = malloc(count, M_TEMP, M_WAITOK);
3762 error = kern_getdirentries(td, fd, dirbuf, count, &base, &resid,
3770 for (dp = (struct dirent *)dirbuf,
3771 edp = (struct dirent *)&dirbuf[count - resid];
3772 ucount < count && dp < edp; ) {
3773 if (dp->d_reclen == 0)
3775 MPASS(dp->d_reclen >= _GENERIC_DIRLEN(0));
3776 if (dp->d_namlen >= sizeof(dstdp.d_name))
3778 dstdp.d_type = dp->d_type;
3779 dstdp.d_namlen = dp->d_namlen;
3780 dstdp.d_fileno = dp->d_fileno; /* truncate */
3781 if (dstdp.d_fileno != dp->d_fileno) {
3782 switch (ino64_trunc_error) {
3790 dstdp.d_fileno = UINT32_MAX;
3794 dstdp.d_reclen = sizeof(dstdp) - sizeof(dstdp.d_name) +
3795 ((dp->d_namlen + 1 + 3) &~ 3);
3796 bcopy(dp->d_name, dstdp.d_name, dstdp.d_namlen);
3797 bzero(dstdp.d_name + dstdp.d_namlen,
3798 dstdp.d_reclen - offsetof(struct freebsd11_dirent, d_name) -
3800 MPASS(dstdp.d_reclen <= dp->d_reclen);
3801 MPASS(ucount + dstdp.d_reclen <= count);
3804 error = copyout(&dstdp, ubuf + ucount, dstdp.d_reclen);
3807 dp = (struct dirent *)((char *)dp + dp->d_reclen);
3808 ucount += dstdp.d_reclen;
3812 free(dirbuf, M_TEMP);
3814 td->td_retval[0] = ucount;
3821 ogetdirentries_cvt(struct freebsd11_dirent *dp)
3823 #if (BYTE_ORDER == LITTLE_ENDIAN)
3825 * The expected low byte of dp->d_namlen is our dp->d_type.
3826 * The high MBZ byte of dp->d_namlen is our dp->d_namlen.
3828 dp->d_type = dp->d_namlen;
3832 * The dp->d_type is the high byte of the expected dp->d_namlen,
3833 * so must be zero'ed.
3840 * Read a block of directory entries in a filesystem independent format.
3842 #ifndef _SYS_SYSPROTO_H_
3843 struct ogetdirentries_args {
3851 ogetdirentries(struct thread *td, struct ogetdirentries_args *uap)
3856 error = kern_ogetdirentries(td, uap, &loff);
3858 error = copyout(&loff, uap->basep, sizeof(long));
3863 kern_ogetdirentries(struct thread *td, struct ogetdirentries_args *uap,
3869 /* XXX arbitrary sanity limit on `count'. */
3870 if (uap->count > 64 * 1024)
3873 error = freebsd11_kern_getdirentries(td, uap->fd, uap->buf, uap->count,
3874 &base, ogetdirentries_cvt);
3876 if (error == 0 && uap->basep != NULL)
3877 error = copyout(&base, uap->basep, sizeof(long));
3881 #endif /* COMPAT_43 */
3883 #if defined(COMPAT_FREEBSD11)
3884 #ifndef _SYS_SYSPROTO_H_
3885 struct freebsd11_getdirentries_args {
3893 freebsd11_getdirentries(struct thread *td,
3894 struct freebsd11_getdirentries_args *uap)
3899 error = freebsd11_kern_getdirentries(td, uap->fd, uap->buf, uap->count,
3902 if (error == 0 && uap->basep != NULL)
3903 error = copyout(&base, uap->basep, sizeof(long));
3908 freebsd11_getdents(struct thread *td, struct freebsd11_getdents_args *uap)
3910 struct freebsd11_getdirentries_args ap;
3914 ap.count = uap->count;
3916 return (freebsd11_getdirentries(td, &ap));
3918 #endif /* COMPAT_FREEBSD11 */
3921 * Read a block of directory entries in a filesystem independent format.
3924 sys_getdirentries(struct thread *td, struct getdirentries_args *uap)
3929 error = kern_getdirentries(td, uap->fd, uap->buf, uap->count, &base,
3930 NULL, UIO_USERSPACE);
3933 if (uap->basep != NULL)
3934 error = copyout(&base, uap->basep, sizeof(off_t));
3939 kern_getdirentries(struct thread *td, int fd, char *buf, size_t count,
3940 off_t *basep, ssize_t *residp, enum uio_seg bufseg)
3946 cap_rights_t rights;
3952 if (count > IOSIZE_MAX)
3954 auio.uio_resid = count;
3955 error = getvnode(td, fd, cap_rights_init(&rights, CAP_READ), &fp);
3958 if ((fp->f_flag & FREAD) == 0) {
3963 foffset = foffset_lock(fp, 0);
3965 if (vp->v_type != VDIR) {
3969 aiov.iov_base = buf;
3970 aiov.iov_len = count;
3971 auio.uio_iov = &aiov;
3972 auio.uio_iovcnt = 1;
3973 auio.uio_rw = UIO_READ;
3974 auio.uio_segflg = bufseg;
3976 vn_lock(vp, LK_SHARED | LK_RETRY);
3977 AUDIT_ARG_VNODE1(vp);
3978 loff = auio.uio_offset = foffset;
3980 error = mac_vnode_check_readdir(td->td_ucred, vp);
3983 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL,
3985 foffset = auio.uio_offset;
3990 if (count == auio.uio_resid &&
3991 (vp->v_vflag & VV_ROOT) &&
3992 (vp->v_mount->mnt_flag & MNT_UNION)) {
3993 struct vnode *tvp = vp;
3995 vp = vp->v_mount->mnt_vnodecovered;
4006 *residp = auio.uio_resid;
4007 td->td_retval[0] = count - auio.uio_resid;
4009 foffset_unlock(fp, foffset, 0);
4015 * Set the mode mask for creation of filesystem nodes.
4017 #ifndef _SYS_SYSPROTO_H_
4023 sys_umask(struct thread *td, struct umask_args *uap)
4025 struct filedesc *fdp;
4027 fdp = td->td_proc->p_fd;
4028 FILEDESC_XLOCK(fdp);
4029 td->td_retval[0] = fdp->fd_cmask;
4030 fdp->fd_cmask = uap->newmask & ALLPERMS;
4031 FILEDESC_XUNLOCK(fdp);
4036 * Void all references to file by ripping underlying filesystem away from
4039 #ifndef _SYS_SYSPROTO_H_
4040 struct revoke_args {
4045 sys_revoke(struct thread *td, struct revoke_args *uap)
4049 struct nameidata nd;
4052 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
4054 if ((error = namei(&nd)) != 0)
4057 NDFREE(&nd, NDF_ONLY_PNBUF);
4058 if (vp->v_type != VCHR || vp->v_rdev == NULL) {
4063 error = mac_vnode_check_revoke(td->td_ucred, vp);
4067 error = VOP_GETATTR(vp, &vattr, td->td_ucred);
4070 if (td->td_ucred->cr_uid != vattr.va_uid) {
4071 error = priv_check(td, PRIV_VFS_ADMIN);
4076 VOP_REVOKE(vp, REVOKEALL);
4083 * Convert a user file descriptor to a kernel file entry and check that, if it
4084 * is a capability, the correct rights are present. A reference on the file
4085 * entry is held upon returning.
4088 getvnode(struct thread *td, int fd, cap_rights_t *rightsp, struct file **fpp)
4093 error = fget_unlocked(td->td_proc->p_fd, fd, rightsp, &fp, NULL);
4098 * The file could be not of the vnode type, or it may be not
4099 * yet fully initialized, in which case the f_vnode pointer
4100 * may be set, but f_ops is still badfileops. E.g.,
4101 * devfs_open() transiently create such situation to
4102 * facilitate csw d_fdopen().
4104 * Dupfdopen() handling in kern_openat() installs the
4105 * half-baked file into the process descriptor table, allowing
4106 * other thread to dereference it. Guard against the race by
4109 if (fp->f_vnode == NULL || fp->f_ops == &badfileops) {
4119 * Get an (NFS) file handle.
4121 #ifndef _SYS_SYSPROTO_H_
4122 struct lgetfh_args {
4128 sys_lgetfh(struct thread *td, struct lgetfh_args *uap)
4130 struct nameidata nd;
4135 error = priv_check(td, PRIV_VFS_GETFH);
4138 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
4143 NDFREE(&nd, NDF_ONLY_PNBUF);
4145 bzero(&fh, sizeof(fh));
4146 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
4147 error = VOP_VPTOFH(vp, &fh.fh_fid);
4150 error = copyout(&fh, uap->fhp, sizeof (fh));
4154 #ifndef _SYS_SYSPROTO_H_
4161 sys_getfh(struct thread *td, struct getfh_args *uap)
4163 struct nameidata nd;
4168 error = priv_check(td, PRIV_VFS_GETFH);
4171 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
4176 NDFREE(&nd, NDF_ONLY_PNBUF);
4178 bzero(&fh, sizeof(fh));
4179 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
4180 error = VOP_VPTOFH(vp, &fh.fh_fid);
4183 error = copyout(&fh, uap->fhp, sizeof (fh));
4188 * syscall for the rpc.lockd to use to translate a NFS file handle into an
4191 * warning: do not remove the priv_check() call or this becomes one giant
4194 #ifndef _SYS_SYSPROTO_H_
4195 struct fhopen_args {
4196 const struct fhandle *u_fhp;
4201 sys_fhopen(struct thread *td, struct fhopen_args *uap)
4210 error = priv_check(td, PRIV_VFS_FHOPEN);
4214 fmode = FFLAGS(uap->flags);
4215 /* why not allow a non-read/write open for our lockd? */
4216 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
4218 error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
4221 /* find the mount point */
4222 mp = vfs_busyfs(&fhp.fh_fsid);
4225 /* now give me my vnode, it gets returned to me locked */
4226 error = VFS_FHTOVP(mp, &fhp.fh_fid, LK_EXCLUSIVE, &vp);
4231 error = falloc_noinstall(td, &fp);
4237 * An extra reference on `fp' has been held for us by
4238 * falloc_noinstall().
4244 error = vn_open_vnode(vp, fmode, td->td_ucred, td, fp);
4246 KASSERT(fp->f_ops == &badfileops,
4247 ("VOP_OPEN in fhopen() set f_ops"));
4248 KASSERT(td->td_dupfd < 0,
4249 ("fhopen() encountered fdopen()"));
4259 finit(fp, (fmode & FMASK) | (fp->f_flag & FHASLOCK), DTYPE_VNODE, vp,
4262 if ((fmode & O_TRUNC) != 0) {
4263 error = fo_truncate(fp, 0, td->td_ucred, td);
4268 error = finstall(td, fp, &indx, fmode, NULL);
4271 td->td_retval[0] = indx;
4276 * Stat an (NFS) file handle.
4278 #ifndef _SYS_SYSPROTO_H_
4279 struct fhstat_args {
4280 struct fhandle *u_fhp;
4285 sys_fhstat(struct thread *td, struct fhstat_args *uap)
4291 error = copyin(uap->u_fhp, &fh, sizeof(fh));
4294 error = kern_fhstat(td, fh, &sb);
4296 error = copyout(&sb, uap->sb, sizeof(sb));
4301 kern_fhstat(struct thread *td, struct fhandle fh, struct stat *sb)
4307 error = priv_check(td, PRIV_VFS_FHSTAT);
4310 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4312 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_EXCLUSIVE, &vp);
4316 error = vn_stat(vp, sb, td->td_ucred, NOCRED, td);
4322 * Implement fstatfs() for (NFS) file handles.
4324 #ifndef _SYS_SYSPROTO_H_
4325 struct fhstatfs_args {
4326 struct fhandle *u_fhp;
4331 sys_fhstatfs(struct thread *td, struct fhstatfs_args *uap)
4337 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4340 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
4341 error = kern_fhstatfs(td, fh, sfp);
4343 error = copyout(sfp, uap->buf, sizeof(*sfp));
4344 free(sfp, M_STATFS);
4349 kern_fhstatfs(struct thread *td, fhandle_t fh, struct statfs *buf)
4356 error = priv_check(td, PRIV_VFS_FHSTATFS);
4359 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4361 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_EXCLUSIVE, &vp);
4367 error = prison_canseemount(td->td_ucred, mp);
4371 error = mac_mount_check_stat(td->td_ucred, mp);
4376 * Set these in case the underlying filesystem fails to do so.
4379 sp->f_version = STATFS_VERSION;
4380 sp->f_namemax = NAME_MAX;
4381 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
4382 error = VFS_STATFS(mp, sp);
4391 kern_posix_fallocate(struct thread *td, int fd, off_t offset, off_t len)
4396 cap_rights_t rights;
4397 off_t olen, ooffset;
4400 int audited_vnode1 = 0;
4404 if (offset < 0 || len <= 0)
4406 /* Check for wrap. */
4407 if (offset > OFF_MAX - len)
4410 error = fget(td, fd, cap_rights_init(&rights, CAP_PWRITE), &fp);
4413 AUDIT_ARG_FILE(td->td_proc, fp);
4414 if ((fp->f_ops->fo_flags & DFLAG_SEEKABLE) == 0) {
4418 if ((fp->f_flag & FWRITE) == 0) {
4422 if (fp->f_type != DTYPE_VNODE) {
4427 if (vp->v_type != VREG) {
4432 /* Allocating blocks may take a long time, so iterate. */
4439 error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
4442 error = vn_lock(vp, LK_EXCLUSIVE);
4444 vn_finished_write(mp);
4448 if (!audited_vnode1) {
4449 AUDIT_ARG_VNODE1(vp);
4454 error = mac_vnode_check_write(td->td_ucred, fp->f_cred, vp);
4457 error = VOP_ALLOCATE(vp, &offset, &len);
4459 vn_finished_write(mp);
4461 if (olen + ooffset != offset + len) {
4462 panic("offset + len changed from %jx/%jx to %jx/%jx",
4463 ooffset, olen, offset, len);
4465 if (error != 0 || len == 0)
4467 KASSERT(olen > len, ("Iteration did not make progress?"));
4476 sys_posix_fallocate(struct thread *td, struct posix_fallocate_args *uap)
4480 error = kern_posix_fallocate(td, uap->fd, uap->offset, uap->len);
4481 return (kern_posix_error(td, error));
4485 * Unlike madvise(2), we do not make a best effort to remember every
4486 * possible caching hint. Instead, we remember the last setting with
4487 * the exception that we will allow POSIX_FADV_NORMAL to adjust the
4488 * region of any current setting.
4491 kern_posix_fadvise(struct thread *td, int fd, off_t offset, off_t len,
4494 struct fadvise_info *fa, *new;
4497 cap_rights_t rights;
4501 if (offset < 0 || len < 0 || offset > OFF_MAX - len)
4503 AUDIT_ARG_VALUE(advice);
4505 case POSIX_FADV_SEQUENTIAL:
4506 case POSIX_FADV_RANDOM:
4507 case POSIX_FADV_NOREUSE:
4508 new = malloc(sizeof(*fa), M_FADVISE, M_WAITOK);
4510 case POSIX_FADV_NORMAL:
4511 case POSIX_FADV_WILLNEED:
4512 case POSIX_FADV_DONTNEED:
4518 /* XXX: CAP_POSIX_FADVISE? */
4520 error = fget(td, fd, cap_rights_init(&rights), &fp);
4523 AUDIT_ARG_FILE(td->td_proc, fp);
4524 if ((fp->f_ops->fo_flags & DFLAG_SEEKABLE) == 0) {
4528 if (fp->f_type != DTYPE_VNODE) {
4533 if (vp->v_type != VREG) {
4540 end = offset + len - 1;
4542 case POSIX_FADV_SEQUENTIAL:
4543 case POSIX_FADV_RANDOM:
4544 case POSIX_FADV_NOREUSE:
4546 * Try to merge any existing non-standard region with
4547 * this new region if possible, otherwise create a new
4548 * non-standard region for this request.
4550 mtx_pool_lock(mtxpool_sleep, fp);
4552 if (fa != NULL && fa->fa_advice == advice &&
4553 ((fa->fa_start <= end && fa->fa_end >= offset) ||
4554 (end != OFF_MAX && fa->fa_start == end + 1) ||
4555 (fa->fa_end != OFF_MAX && fa->fa_end + 1 == offset))) {
4556 if (offset < fa->fa_start)
4557 fa->fa_start = offset;
4558 if (end > fa->fa_end)
4561 new->fa_advice = advice;
4562 new->fa_start = offset;
4567 mtx_pool_unlock(mtxpool_sleep, fp);
4569 case POSIX_FADV_NORMAL:
4571 * If a the "normal" region overlaps with an existing
4572 * non-standard region, trim or remove the
4573 * non-standard region.
4575 mtx_pool_lock(mtxpool_sleep, fp);
4578 if (offset <= fa->fa_start && end >= fa->fa_end) {
4580 fp->f_advice = NULL;
4581 } else if (offset <= fa->fa_start &&
4582 end >= fa->fa_start)
4583 fa->fa_start = end + 1;
4584 else if (offset <= fa->fa_end && end >= fa->fa_end)
4585 fa->fa_end = offset - 1;
4586 else if (offset >= fa->fa_start && end <= fa->fa_end) {
4588 * If the "normal" region is a middle
4589 * portion of the existing
4590 * non-standard region, just remove
4591 * the whole thing rather than picking
4592 * one side or the other to
4596 fp->f_advice = NULL;
4599 mtx_pool_unlock(mtxpool_sleep, fp);
4601 case POSIX_FADV_WILLNEED:
4602 case POSIX_FADV_DONTNEED:
4603 error = VOP_ADVISE(vp, offset, end, advice);
4609 free(new, M_FADVISE);
4614 sys_posix_fadvise(struct thread *td, struct posix_fadvise_args *uap)
4618 error = kern_posix_fadvise(td, uap->fd, uap->offset, uap->len,
4620 return (kern_posix_error(td, error));