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)
352 error = getvnode(td, fd, cap_rights_init(&rights, CAP_FSTATFS), &fp);
356 vn_lock(vp, LK_SHARED | LK_RETRY);
358 AUDIT_ARG_VNODE1(vp);
365 return (kern_do_statfs(td, mp, buf));
369 * Get statistics on all filesystems.
371 #ifndef _SYS_SYSPROTO_H_
372 struct getfsstat_args {
379 sys_getfsstat(struct thread *td, struct getfsstat_args *uap)
384 if (uap->bufsize < 0 || uap->bufsize > SIZE_MAX)
386 error = kern_getfsstat(td, &uap->buf, uap->bufsize, &count,
387 UIO_USERSPACE, uap->mode);
389 td->td_retval[0] = count;
394 * If (bufsize > 0 && bufseg == UIO_SYSSPACE)
395 * The caller is responsible for freeing memory which will be allocated
399 kern_getfsstat(struct thread *td, struct statfs **buf, size_t bufsize,
400 size_t *countp, enum uio_seg bufseg, int mode)
402 struct mount *mp, *nmp;
403 struct statfs *sfsp, *sp, *sptmp, *tofree;
404 size_t count, maxcount;
412 if (bufseg == UIO_SYSSPACE)
417 maxcount = bufsize / sizeof(struct statfs);
421 } else if (bufseg == UIO_USERSPACE) {
424 } else /* if (bufseg == UIO_SYSSPACE) */ {
426 mtx_lock(&mountlist_mtx);
427 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
430 mtx_unlock(&mountlist_mtx);
431 if (maxcount > count)
433 tofree = sfsp = *buf = malloc(maxcount * sizeof(struct statfs),
437 mtx_lock(&mountlist_mtx);
438 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
439 if (prison_canseemount(td->td_ucred, mp) != 0) {
440 nmp = TAILQ_NEXT(mp, mnt_list);
444 if (mac_mount_check_stat(td->td_ucred, mp) != 0) {
445 nmp = TAILQ_NEXT(mp, mnt_list);
449 if (mode == MNT_WAIT) {
450 if (vfs_busy(mp, MBF_MNTLSTLOCK) != 0) {
452 * If vfs_busy() failed, and MBF_NOWAIT
453 * wasn't passed, then the mp is gone.
454 * Furthermore, because of MBF_MNTLSTLOCK,
455 * the mountlist_mtx was dropped. We have
456 * no other choice than to start over.
458 mtx_unlock(&mountlist_mtx);
459 free(tofree, M_STATFS);
463 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK) != 0) {
464 nmp = TAILQ_NEXT(mp, mnt_list);
468 if (sfsp != NULL && count < maxcount) {
471 * Set these in case the underlying filesystem
474 sp->f_version = STATFS_VERSION;
475 sp->f_namemax = NAME_MAX;
476 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
478 * If MNT_NOWAIT is specified, do not refresh
481 if (mode != MNT_NOWAIT) {
482 error = VFS_STATFS(mp, sp);
484 mtx_lock(&mountlist_mtx);
485 nmp = TAILQ_NEXT(mp, mnt_list);
490 if (priv_check(td, PRIV_VFS_GENERATION)) {
491 sptmp = malloc(sizeof(struct statfs), M_STATFS,
494 sptmp->f_fsid.val[0] = sptmp->f_fsid.val[1] = 0;
495 prison_enforce_statfs(td->td_ucred, mp, sptmp);
499 if (bufseg == UIO_SYSSPACE) {
500 bcopy(sp, sfsp, sizeof(*sp));
501 free(sptmp, M_STATFS);
502 } else /* if (bufseg == UIO_USERSPACE) */ {
503 error = copyout(sp, sfsp, sizeof(*sp));
504 free(sptmp, M_STATFS);
513 mtx_lock(&mountlist_mtx);
514 nmp = TAILQ_NEXT(mp, mnt_list);
517 mtx_unlock(&mountlist_mtx);
518 if (sfsp != NULL && count > maxcount)
525 #ifdef COMPAT_FREEBSD4
527 * Get old format filesystem statistics.
529 static void freebsd4_cvtstatfs(struct statfs *, struct ostatfs *);
531 #ifndef _SYS_SYSPROTO_H_
532 struct freebsd4_statfs_args {
538 freebsd4_statfs(struct thread *td, struct freebsd4_statfs_args *uap)
544 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
545 error = kern_statfs(td, uap->path, UIO_USERSPACE, sfp);
547 freebsd4_cvtstatfs(sfp, &osb);
548 error = copyout(&osb, uap->buf, sizeof(osb));
555 * Get filesystem statistics.
557 #ifndef _SYS_SYSPROTO_H_
558 struct freebsd4_fstatfs_args {
564 freebsd4_fstatfs(struct thread *td, struct freebsd4_fstatfs_args *uap)
570 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
571 error = kern_fstatfs(td, uap->fd, sfp);
573 freebsd4_cvtstatfs(sfp, &osb);
574 error = copyout(&osb, uap->buf, sizeof(osb));
581 * Get statistics on all filesystems.
583 #ifndef _SYS_SYSPROTO_H_
584 struct freebsd4_getfsstat_args {
591 freebsd4_getfsstat(struct thread *td, struct freebsd4_getfsstat_args *uap)
593 struct statfs *buf, *sp;
598 if (uap->bufsize < 0)
600 count = uap->bufsize / sizeof(struct ostatfs);
601 if (count > SIZE_MAX / sizeof(struct statfs))
603 size = count * sizeof(struct statfs);
604 error = kern_getfsstat(td, &buf, size, &count, UIO_SYSSPACE,
607 td->td_retval[0] = count;
610 while (count != 0 && error == 0) {
611 freebsd4_cvtstatfs(sp, &osb);
612 error = copyout(&osb, uap->buf, sizeof(osb));
623 * Implement fstatfs() for (NFS) file handles.
625 #ifndef _SYS_SYSPROTO_H_
626 struct freebsd4_fhstatfs_args {
627 struct fhandle *u_fhp;
632 freebsd4_fhstatfs(struct thread *td, struct freebsd4_fhstatfs_args *uap)
639 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
642 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
643 error = kern_fhstatfs(td, fh, sfp);
645 freebsd4_cvtstatfs(sfp, &osb);
646 error = copyout(&osb, uap->buf, sizeof(osb));
653 * Convert a new format statfs structure to an old format statfs structure.
656 freebsd4_cvtstatfs(struct statfs *nsp, struct ostatfs *osp)
659 statfs_scale_blocks(nsp, LONG_MAX);
660 bzero(osp, sizeof(*osp));
661 osp->f_bsize = nsp->f_bsize;
662 osp->f_iosize = MIN(nsp->f_iosize, LONG_MAX);
663 osp->f_blocks = nsp->f_blocks;
664 osp->f_bfree = nsp->f_bfree;
665 osp->f_bavail = nsp->f_bavail;
666 osp->f_files = MIN(nsp->f_files, LONG_MAX);
667 osp->f_ffree = MIN(nsp->f_ffree, LONG_MAX);
668 osp->f_owner = nsp->f_owner;
669 osp->f_type = nsp->f_type;
670 osp->f_flags = nsp->f_flags;
671 osp->f_syncwrites = MIN(nsp->f_syncwrites, LONG_MAX);
672 osp->f_asyncwrites = MIN(nsp->f_asyncwrites, LONG_MAX);
673 osp->f_syncreads = MIN(nsp->f_syncreads, LONG_MAX);
674 osp->f_asyncreads = MIN(nsp->f_asyncreads, LONG_MAX);
675 strlcpy(osp->f_fstypename, nsp->f_fstypename,
676 MIN(MFSNAMELEN, OMFSNAMELEN));
677 strlcpy(osp->f_mntonname, nsp->f_mntonname,
678 MIN(MNAMELEN, OMNAMELEN));
679 strlcpy(osp->f_mntfromname, nsp->f_mntfromname,
680 MIN(MNAMELEN, OMNAMELEN));
681 osp->f_fsid = nsp->f_fsid;
683 #endif /* COMPAT_FREEBSD4 */
685 #if defined(COMPAT_FREEBSD11)
687 * Get old format filesystem statistics.
689 static void freebsd11_cvtstatfs(struct statfs *, struct freebsd11_statfs *);
692 freebsd11_statfs(struct thread *td, struct freebsd11_statfs_args *uap)
694 struct freebsd11_statfs osb;
698 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
699 error = kern_statfs(td, uap->path, UIO_USERSPACE, sfp);
701 freebsd11_cvtstatfs(sfp, &osb);
702 error = copyout(&osb, uap->buf, sizeof(osb));
709 * Get filesystem statistics.
712 freebsd11_fstatfs(struct thread *td, struct freebsd11_fstatfs_args *uap)
714 struct freebsd11_statfs osb;
718 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
719 error = kern_fstatfs(td, uap->fd, sfp);
721 freebsd11_cvtstatfs(sfp, &osb);
722 error = copyout(&osb, uap->buf, sizeof(osb));
729 * Get statistics on all filesystems.
732 freebsd11_getfsstat(struct thread *td, struct freebsd11_getfsstat_args *uap)
734 struct freebsd11_statfs osb;
735 struct statfs *buf, *sp;
739 count = uap->bufsize / sizeof(struct ostatfs);
740 size = count * sizeof(struct statfs);
741 error = kern_getfsstat(td, &buf, size, &count, UIO_SYSSPACE,
744 td->td_retval[0] = count;
747 while (count > 0 && error == 0) {
748 freebsd11_cvtstatfs(sp, &osb);
749 error = copyout(&osb, uap->buf, sizeof(osb));
760 * Implement fstatfs() for (NFS) file handles.
763 freebsd11_fhstatfs(struct thread *td, struct freebsd11_fhstatfs_args *uap)
765 struct freebsd11_statfs osb;
770 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
773 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
774 error = kern_fhstatfs(td, fh, sfp);
776 freebsd11_cvtstatfs(sfp, &osb);
777 error = copyout(&osb, uap->buf, sizeof(osb));
784 * Convert a new format statfs structure to an old format statfs structure.
787 freebsd11_cvtstatfs(struct statfs *nsp, struct freebsd11_statfs *osp)
790 bzero(osp, sizeof(*osp));
791 osp->f_version = FREEBSD11_STATFS_VERSION;
792 osp->f_type = nsp->f_type;
793 osp->f_flags = nsp->f_flags;
794 osp->f_bsize = nsp->f_bsize;
795 osp->f_iosize = nsp->f_iosize;
796 osp->f_blocks = nsp->f_blocks;
797 osp->f_bfree = nsp->f_bfree;
798 osp->f_bavail = nsp->f_bavail;
799 osp->f_files = nsp->f_files;
800 osp->f_ffree = nsp->f_ffree;
801 osp->f_syncwrites = nsp->f_syncwrites;
802 osp->f_asyncwrites = nsp->f_asyncwrites;
803 osp->f_syncreads = nsp->f_syncreads;
804 osp->f_asyncreads = nsp->f_asyncreads;
805 osp->f_namemax = nsp->f_namemax;
806 osp->f_owner = nsp->f_owner;
807 osp->f_fsid = nsp->f_fsid;
808 strlcpy(osp->f_fstypename, nsp->f_fstypename,
809 MIN(MFSNAMELEN, sizeof(osp->f_fstypename)));
810 strlcpy(osp->f_mntonname, nsp->f_mntonname,
811 MIN(MNAMELEN, sizeof(osp->f_mntonname)));
812 strlcpy(osp->f_mntfromname, nsp->f_mntfromname,
813 MIN(MNAMELEN, sizeof(osp->f_mntfromname)));
815 #endif /* COMPAT_FREEBSD11 */
818 * Change current working directory to a given file descriptor.
820 #ifndef _SYS_SYSPROTO_H_
826 sys_fchdir(struct thread *td, struct fchdir_args *uap)
828 struct vnode *vp, *tdp;
833 AUDIT_ARG_FD(uap->fd);
834 error = getvnode(td, uap->fd, &cap_fchdir_rights,
841 vn_lock(vp, LK_SHARED | LK_RETRY);
842 AUDIT_ARG_VNODE1(vp);
843 error = change_dir(vp, td);
844 while (!error && (mp = vp->v_mountedhere) != NULL) {
847 error = VFS_ROOT(mp, LK_SHARED, &tdp);
864 * Change current working directory (``.'').
866 #ifndef _SYS_SYSPROTO_H_
872 sys_chdir(struct thread *td, struct chdir_args *uap)
875 return (kern_chdir(td, uap->path, UIO_USERSPACE));
879 kern_chdir(struct thread *td, char *path, enum uio_seg pathseg)
884 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
886 if ((error = namei(&nd)) != 0)
888 if ((error = change_dir(nd.ni_vp, td)) != 0) {
890 NDFREE(&nd, NDF_ONLY_PNBUF);
893 VOP_UNLOCK(nd.ni_vp, 0);
894 NDFREE(&nd, NDF_ONLY_PNBUF);
895 pwd_chdir(td, nd.ni_vp);
900 * Change notion of root (``/'') directory.
902 #ifndef _SYS_SYSPROTO_H_
908 sys_chroot(struct thread *td, struct chroot_args *uap)
913 error = priv_check(td, PRIV_VFS_CHROOT);
916 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
917 UIO_USERSPACE, uap->path, td);
921 error = change_dir(nd.ni_vp, td);
925 error = mac_vnode_check_chroot(td->td_ucred, nd.ni_vp);
929 VOP_UNLOCK(nd.ni_vp, 0);
930 error = pwd_chroot(td, nd.ni_vp);
932 NDFREE(&nd, NDF_ONLY_PNBUF);
937 NDFREE(&nd, NDF_ONLY_PNBUF);
942 * Common routine for chroot and chdir. Callers must provide a locked vnode
946 change_dir(struct vnode *vp, struct thread *td)
952 ASSERT_VOP_LOCKED(vp, "change_dir(): vp not locked");
953 if (vp->v_type != VDIR)
956 error = mac_vnode_check_chdir(td->td_ucred, vp);
960 return (VOP_ACCESS(vp, VEXEC, td->td_ucred, td));
964 flags_to_rights(int flags, cap_rights_t *rightsp)
967 if (flags & O_EXEC) {
968 cap_rights_set(rightsp, CAP_FEXECVE);
970 switch ((flags & O_ACCMODE)) {
972 cap_rights_set(rightsp, CAP_READ);
975 cap_rights_set(rightsp, CAP_READ);
978 cap_rights_set(rightsp, CAP_WRITE);
979 if (!(flags & (O_APPEND | O_TRUNC)))
980 cap_rights_set(rightsp, CAP_SEEK);
986 cap_rights_set(rightsp, CAP_CREATE);
989 cap_rights_set(rightsp, CAP_FTRUNCATE);
991 if (flags & (O_SYNC | O_FSYNC))
992 cap_rights_set(rightsp, CAP_FSYNC);
994 if (flags & (O_EXLOCK | O_SHLOCK))
995 cap_rights_set(rightsp, CAP_FLOCK);
999 * Check permissions, allocate an open file structure, and call the device
1000 * open routine if any.
1002 #ifndef _SYS_SYSPROTO_H_
1010 sys_open(struct thread *td, struct open_args *uap)
1013 return (kern_openat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1014 uap->flags, uap->mode));
1017 #ifndef _SYS_SYSPROTO_H_
1018 struct openat_args {
1026 sys_openat(struct thread *td, struct openat_args *uap)
1029 AUDIT_ARG_FD(uap->fd);
1030 return (kern_openat(td, uap->fd, uap->path, UIO_USERSPACE, uap->flag,
1035 kern_openat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1036 int flags, int mode)
1038 struct proc *p = td->td_proc;
1039 struct filedesc *fdp = p->p_fd;
1042 struct nameidata nd;
1043 cap_rights_t rights;
1044 int cmode, error, indx;
1048 AUDIT_ARG_FFLAGS(flags);
1049 AUDIT_ARG_MODE(mode);
1050 cap_rights_init(&rights, CAP_LOOKUP);
1051 flags_to_rights(flags, &rights);
1053 * Only one of the O_EXEC, O_RDONLY, O_WRONLY and O_RDWR flags
1056 if (flags & O_EXEC) {
1057 if (flags & O_ACCMODE)
1059 } else if ((flags & O_ACCMODE) == O_ACCMODE) {
1062 flags = FFLAGS(flags);
1066 * Allocate a file structure. The descriptor to reference it
1067 * is allocated and set by finstall() below.
1069 error = falloc_noinstall(td, &fp);
1073 * An extra reference on `fp' has been held for us by
1074 * falloc_noinstall().
1076 /* Set the flags early so the finit in devfs can pick them up. */
1077 fp->f_flag = flags & FMASK;
1078 cmode = ((mode & ~fdp->fd_cmask) & ALLPERMS) & ~S_ISTXT;
1079 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, fd,
1081 td->td_dupfd = -1; /* XXX check for fdopen */
1082 error = vn_open(&nd, &flags, cmode, fp);
1085 * If the vn_open replaced the method vector, something
1086 * wonderous happened deep below and we just pass it up
1087 * pretending we know what we do.
1089 if (error == ENXIO && fp->f_ops != &badfileops)
1093 * Handle special fdopen() case. bleh.
1095 * Don't do this for relative (capability) lookups; we don't
1096 * understand exactly what would happen, and we don't think
1097 * that it ever should.
1099 if ((nd.ni_lcf & NI_LCF_STRICTRELATIVE) == 0 &&
1100 (error == ENODEV || error == ENXIO) &&
1101 td->td_dupfd >= 0) {
1102 error = dupfdopen(td, fdp, td->td_dupfd, flags, error,
1111 NDFREE(&nd, NDF_ONLY_PNBUF);
1115 * Store the vnode, for any f_type. Typically, the vnode use
1116 * count is decremented by direct call to vn_closefile() for
1117 * files that switched type in the cdevsw fdopen() method.
1121 * If the file wasn't claimed by devfs bind it to the normal
1122 * vnode operations here.
1124 if (fp->f_ops == &badfileops) {
1125 KASSERT(vp->v_type != VFIFO, ("Unexpected fifo."));
1127 finit(fp, (flags & FMASK) | (fp->f_flag & FHASLOCK),
1128 DTYPE_VNODE, vp, &vnops);
1132 if (flags & O_TRUNC) {
1133 error = fo_truncate(fp, 0, td->td_ucred, td);
1139 * If we haven't already installed the FD (for dupfdopen), do so now.
1142 struct filecaps *fcaps;
1145 if ((nd.ni_lcf & NI_LCF_STRICTRELATIVE) != 0)
1146 fcaps = &nd.ni_filecaps;
1150 error = finstall(td, fp, &indx, flags, fcaps);
1151 /* On success finstall() consumes fcaps. */
1153 filecaps_free(&nd.ni_filecaps);
1157 filecaps_free(&nd.ni_filecaps);
1161 * Release our private reference, leaving the one associated with
1162 * the descriptor table intact.
1165 td->td_retval[0] = indx;
1168 KASSERT(indx == -1, ("indx=%d, should be -1", indx));
1177 #ifndef _SYS_SYSPROTO_H_
1178 struct ocreat_args {
1184 ocreat(struct thread *td, struct ocreat_args *uap)
1187 return (kern_openat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1188 O_WRONLY | O_CREAT | O_TRUNC, uap->mode));
1190 #endif /* COMPAT_43 */
1193 * Create a special file.
1195 #ifndef _SYS_SYSPROTO_H_
1196 struct mknodat_args {
1204 sys_mknodat(struct thread *td, struct mknodat_args *uap)
1207 return (kern_mknodat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode,
1211 #if defined(COMPAT_FREEBSD11)
1213 freebsd11_mknod(struct thread *td,
1214 struct freebsd11_mknod_args *uap)
1217 return (kern_mknodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1218 uap->mode, uap->dev));
1222 freebsd11_mknodat(struct thread *td,
1223 struct freebsd11_mknodat_args *uap)
1226 return (kern_mknodat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode,
1229 #endif /* COMPAT_FREEBSD11 */
1232 kern_mknodat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1233 int mode, dev_t dev)
1238 struct nameidata nd;
1239 cap_rights_t rights;
1240 int error, whiteout = 0;
1242 AUDIT_ARG_MODE(mode);
1244 switch (mode & S_IFMT) {
1247 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
1248 if (error == 0 && dev == VNOVAL)
1252 error = priv_check(td, PRIV_VFS_MKNOD_WHT);
1256 return (kern_mkfifoat(td, fd, path, pathseg, mode));
1266 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
1267 NOCACHE, pathseg, path, fd, cap_rights_init(&rights, CAP_MKNODAT),
1269 if ((error = namei(&nd)) != 0)
1273 NDFREE(&nd, NDF_ONLY_PNBUF);
1274 if (vp == nd.ni_dvp)
1282 vattr.va_mode = (mode & ALLPERMS) &
1283 ~td->td_proc->p_fd->fd_cmask;
1284 vattr.va_rdev = dev;
1287 switch (mode & S_IFMT) {
1289 vattr.va_type = VCHR;
1292 vattr.va_type = VBLK;
1298 panic("kern_mknod: invalid mode");
1301 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1302 NDFREE(&nd, NDF_ONLY_PNBUF);
1304 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1309 if (error == 0 && !whiteout)
1310 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp,
1311 &nd.ni_cnd, &vattr);
1315 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, CREATE);
1317 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp,
1318 &nd.ni_cnd, &vattr);
1323 NDFREE(&nd, NDF_ONLY_PNBUF);
1325 vn_finished_write(mp);
1330 * Create a named pipe.
1332 #ifndef _SYS_SYSPROTO_H_
1333 struct mkfifo_args {
1339 sys_mkfifo(struct thread *td, struct mkfifo_args *uap)
1342 return (kern_mkfifoat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1346 #ifndef _SYS_SYSPROTO_H_
1347 struct mkfifoat_args {
1354 sys_mkfifoat(struct thread *td, struct mkfifoat_args *uap)
1357 return (kern_mkfifoat(td, uap->fd, uap->path, UIO_USERSPACE,
1362 kern_mkfifoat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1367 struct nameidata nd;
1368 cap_rights_t rights;
1371 AUDIT_ARG_MODE(mode);
1374 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
1375 NOCACHE, pathseg, path, fd, cap_rights_init(&rights, CAP_MKFIFOAT),
1377 if ((error = namei(&nd)) != 0)
1379 if (nd.ni_vp != NULL) {
1380 NDFREE(&nd, NDF_ONLY_PNBUF);
1381 if (nd.ni_vp == nd.ni_dvp)
1388 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1389 NDFREE(&nd, NDF_ONLY_PNBUF);
1391 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1396 vattr.va_type = VFIFO;
1397 vattr.va_mode = (mode & ALLPERMS) & ~td->td_proc->p_fd->fd_cmask;
1399 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1404 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
1411 vn_finished_write(mp);
1412 NDFREE(&nd, NDF_ONLY_PNBUF);
1417 * Make a hard file link.
1419 #ifndef _SYS_SYSPROTO_H_
1426 sys_link(struct thread *td, struct link_args *uap)
1429 return (kern_linkat(td, AT_FDCWD, AT_FDCWD, uap->path, uap->link,
1430 UIO_USERSPACE, FOLLOW));
1433 #ifndef _SYS_SYSPROTO_H_
1434 struct linkat_args {
1443 sys_linkat(struct thread *td, struct linkat_args *uap)
1448 if (flag & ~AT_SYMLINK_FOLLOW)
1451 return (kern_linkat(td, uap->fd1, uap->fd2, uap->path1, uap->path2,
1452 UIO_USERSPACE, (flag & AT_SYMLINK_FOLLOW) ? FOLLOW : NOFOLLOW));
1455 int hardlink_check_uid = 0;
1456 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
1457 &hardlink_check_uid, 0,
1458 "Unprivileged processes cannot create hard links to files owned by other "
1460 static int hardlink_check_gid = 0;
1461 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
1462 &hardlink_check_gid, 0,
1463 "Unprivileged processes cannot create hard links to files owned by other "
1467 can_hardlink(struct vnode *vp, struct ucred *cred)
1472 if (!hardlink_check_uid && !hardlink_check_gid)
1475 error = VOP_GETATTR(vp, &va, cred);
1479 if (hardlink_check_uid && cred->cr_uid != va.va_uid) {
1480 error = priv_check_cred(cred, PRIV_VFS_LINK, 0);
1485 if (hardlink_check_gid && !groupmember(va.va_gid, cred)) {
1486 error = priv_check_cred(cred, PRIV_VFS_LINK, 0);
1495 kern_linkat(struct thread *td, int fd1, int fd2, char *path1, char *path2,
1496 enum uio_seg segflg, int follow)
1500 struct nameidata nd;
1501 cap_rights_t rights;
1506 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, segflg, path1, fd1,
1507 cap_rights_init(&rights, CAP_LINKAT_SOURCE), td);
1509 if ((error = namei(&nd)) != 0)
1511 NDFREE(&nd, NDF_ONLY_PNBUF);
1513 if (vp->v_type == VDIR) {
1515 return (EPERM); /* POSIX */
1517 NDINIT_ATRIGHTS(&nd, CREATE,
1518 LOCKPARENT | SAVENAME | AUDITVNODE2 | NOCACHE, segflg, path2, fd2,
1519 cap_rights_init(&rights, CAP_LINKAT_TARGET), td);
1520 if ((error = namei(&nd)) == 0) {
1521 if (nd.ni_vp != NULL) {
1522 NDFREE(&nd, NDF_ONLY_PNBUF);
1523 if (nd.ni_dvp == nd.ni_vp)
1530 } else if (nd.ni_dvp->v_mount != vp->v_mount) {
1532 * Cross-device link. No need to recheck
1533 * vp->v_type, since it cannot change, except
1536 NDFREE(&nd, NDF_ONLY_PNBUF);
1540 } else if ((error = vn_lock(vp, LK_EXCLUSIVE)) == 0) {
1541 error = can_hardlink(vp, td->td_ucred);
1544 error = mac_vnode_check_link(td->td_ucred,
1545 nd.ni_dvp, vp, &nd.ni_cnd);
1550 NDFREE(&nd, NDF_ONLY_PNBUF);
1553 error = vn_start_write(vp, &mp, V_NOWAIT);
1557 NDFREE(&nd, NDF_ONLY_PNBUF);
1558 error = vn_start_write(NULL, &mp,
1564 error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
1567 vn_finished_write(mp);
1568 NDFREE(&nd, NDF_ONLY_PNBUF);
1571 NDFREE(&nd, NDF_ONLY_PNBUF);
1581 * Make a symbolic link.
1583 #ifndef _SYS_SYSPROTO_H_
1584 struct symlink_args {
1590 sys_symlink(struct thread *td, struct symlink_args *uap)
1593 return (kern_symlinkat(td, uap->path, AT_FDCWD, uap->link,
1597 #ifndef _SYS_SYSPROTO_H_
1598 struct symlinkat_args {
1605 sys_symlinkat(struct thread *td, struct symlinkat_args *uap)
1608 return (kern_symlinkat(td, uap->path1, uap->fd, uap->path2,
1613 kern_symlinkat(struct thread *td, char *path1, int fd, char *path2,
1614 enum uio_seg segflg)
1619 struct nameidata nd;
1621 cap_rights_t rights;
1623 if (segflg == UIO_SYSSPACE) {
1626 syspath = uma_zalloc(namei_zone, M_WAITOK);
1627 if ((error = copyinstr(path1, syspath, MAXPATHLEN, NULL)) != 0)
1630 AUDIT_ARG_TEXT(syspath);
1633 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
1634 NOCACHE, segflg, path2, fd, cap_rights_init(&rights, CAP_SYMLINKAT),
1636 if ((error = namei(&nd)) != 0)
1639 NDFREE(&nd, NDF_ONLY_PNBUF);
1640 if (nd.ni_vp == nd.ni_dvp)
1648 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1649 NDFREE(&nd, NDF_ONLY_PNBUF);
1651 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1656 vattr.va_mode = ACCESSPERMS &~ td->td_proc->p_fd->fd_cmask;
1658 vattr.va_type = VLNK;
1659 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1664 error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr, syspath);
1670 NDFREE(&nd, NDF_ONLY_PNBUF);
1672 vn_finished_write(mp);
1674 if (segflg != UIO_SYSSPACE)
1675 uma_zfree(namei_zone, syspath);
1680 * Delete a whiteout from the filesystem.
1682 #ifndef _SYS_SYSPROTO_H_
1683 struct undelete_args {
1688 sys_undelete(struct thread *td, struct undelete_args *uap)
1691 struct nameidata nd;
1696 NDINIT(&nd, DELETE, LOCKPARENT | DOWHITEOUT | AUDITVNODE1,
1697 UIO_USERSPACE, uap->path, td);
1702 if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & ISWHITEOUT)) {
1703 NDFREE(&nd, NDF_ONLY_PNBUF);
1704 if (nd.ni_vp == nd.ni_dvp)
1712 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1713 NDFREE(&nd, NDF_ONLY_PNBUF);
1715 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1719 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, DELETE);
1720 NDFREE(&nd, NDF_ONLY_PNBUF);
1722 vn_finished_write(mp);
1727 * Delete a name from the filesystem.
1729 #ifndef _SYS_SYSPROTO_H_
1730 struct unlink_args {
1735 sys_unlink(struct thread *td, struct unlink_args *uap)
1738 return (kern_unlinkat(td, AT_FDCWD, uap->path, UIO_USERSPACE, 0));
1741 #ifndef _SYS_SYSPROTO_H_
1742 struct unlinkat_args {
1749 sys_unlinkat(struct thread *td, struct unlinkat_args *uap)
1751 int flag = uap->flag;
1753 char *path = uap->path;
1755 if (flag & ~AT_REMOVEDIR)
1758 if (flag & AT_REMOVEDIR)
1759 return (kern_rmdirat(td, fd, path, UIO_USERSPACE));
1761 return (kern_unlinkat(td, fd, path, UIO_USERSPACE, 0));
1765 kern_unlinkat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1770 struct nameidata nd;
1772 cap_rights_t rights;
1777 NDINIT_ATRIGHTS(&nd, DELETE, LOCKPARENT | LOCKLEAF | AUDITVNODE1,
1778 pathseg, path, fd, cap_rights_init(&rights, CAP_UNLINKAT), td);
1779 if ((error = namei(&nd)) != 0)
1780 return (error == EINVAL ? EPERM : error);
1782 if (vp->v_type == VDIR && oldinum == 0) {
1783 error = EPERM; /* POSIX */
1784 } else if (oldinum != 0 &&
1785 ((error = vn_stat(vp, &sb, td->td_ucred, NOCRED, td)) == 0) &&
1786 sb.st_ino != oldinum) {
1787 error = EIDRM; /* Identifier removed */
1790 * The root of a mounted filesystem cannot be deleted.
1792 * XXX: can this only be a VDIR case?
1794 if (vp->v_vflag & VV_ROOT)
1798 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1799 NDFREE(&nd, NDF_ONLY_PNBUF);
1801 if (vp == nd.ni_dvp)
1805 if ((error = vn_start_write(NULL, &mp,
1806 V_XSLEEP | PCATCH)) != 0)
1811 error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp,
1816 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_UNLINK);
1817 error = VOP_REMOVE(nd.ni_dvp, vp, &nd.ni_cnd);
1821 vn_finished_write(mp);
1823 NDFREE(&nd, NDF_ONLY_PNBUF);
1825 if (vp == nd.ni_dvp)
1833 * Reposition read/write file offset.
1835 #ifndef _SYS_SYSPROTO_H_
1844 sys_lseek(struct thread *td, struct lseek_args *uap)
1847 return (kern_lseek(td, uap->fd, uap->offset, uap->whence));
1851 kern_lseek(struct thread *td, int fd, off_t offset, int whence)
1854 cap_rights_t rights;
1858 error = fget(td, fd, cap_rights_init(&rights, CAP_SEEK), &fp);
1861 error = (fp->f_ops->fo_flags & DFLAG_SEEKABLE) != 0 ?
1862 fo_seek(fp, offset, whence, td) : ESPIPE;
1867 #if defined(COMPAT_43)
1869 * Reposition read/write file offset.
1871 #ifndef _SYS_SYSPROTO_H_
1872 struct olseek_args {
1879 olseek(struct thread *td, struct olseek_args *uap)
1882 return (kern_lseek(td, uap->fd, uap->offset, uap->whence));
1884 #endif /* COMPAT_43 */
1886 #if defined(COMPAT_FREEBSD6)
1887 /* Version with the 'pad' argument */
1889 freebsd6_lseek(struct thread *td, struct freebsd6_lseek_args *uap)
1892 return (kern_lseek(td, uap->fd, uap->offset, uap->whence));
1897 * Check access permissions using passed credentials.
1900 vn_access(struct vnode *vp, int user_flags, struct ucred *cred,
1906 /* Flags == 0 means only check for existence. */
1907 if (user_flags == 0)
1911 if (user_flags & R_OK)
1913 if (user_flags & W_OK)
1915 if (user_flags & X_OK)
1918 error = mac_vnode_check_access(cred, vp, accmode);
1922 if ((accmode & VWRITE) == 0 || (error = vn_writechk(vp)) == 0)
1923 error = VOP_ACCESS(vp, accmode, cred, td);
1928 * Check access permissions using "real" credentials.
1930 #ifndef _SYS_SYSPROTO_H_
1931 struct access_args {
1937 sys_access(struct thread *td, struct access_args *uap)
1940 return (kern_accessat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1944 #ifndef _SYS_SYSPROTO_H_
1945 struct faccessat_args {
1953 sys_faccessat(struct thread *td, struct faccessat_args *uap)
1956 return (kern_accessat(td, uap->fd, uap->path, UIO_USERSPACE, uap->flag,
1961 kern_accessat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1962 int flag, int amode)
1964 struct ucred *cred, *usecred;
1966 struct nameidata nd;
1967 cap_rights_t rights;
1970 if (flag & ~AT_EACCESS)
1972 if (amode != F_OK && (amode & ~(R_OK | W_OK | X_OK)) != 0)
1976 * Create and modify a temporary credential instead of one that
1977 * is potentially shared (if we need one).
1979 cred = td->td_ucred;
1980 if ((flag & AT_EACCESS) == 0 &&
1981 ((cred->cr_uid != cred->cr_ruid ||
1982 cred->cr_rgid != cred->cr_groups[0]))) {
1983 usecred = crdup(cred);
1984 usecred->cr_uid = cred->cr_ruid;
1985 usecred->cr_groups[0] = cred->cr_rgid;
1986 td->td_ucred = usecred;
1989 AUDIT_ARG_VALUE(amode);
1990 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF |
1991 AUDITVNODE1, pathseg, path, fd, cap_rights_init(&rights, CAP_FSTAT),
1993 if ((error = namei(&nd)) != 0)
1997 error = vn_access(vp, amode, usecred, td);
1998 NDFREE(&nd, NDF_ONLY_PNBUF);
2001 if (usecred != cred) {
2002 td->td_ucred = cred;
2009 * Check access permissions using "effective" credentials.
2011 #ifndef _SYS_SYSPROTO_H_
2012 struct eaccess_args {
2018 sys_eaccess(struct thread *td, struct eaccess_args *uap)
2021 return (kern_accessat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2022 AT_EACCESS, uap->amode));
2025 #if defined(COMPAT_43)
2027 * Get file status; this version follows links.
2029 #ifndef _SYS_SYSPROTO_H_
2036 ostat(struct thread *td, struct ostat_args *uap)
2042 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
2047 return (copyout(&osb, uap->ub, sizeof (osb)));
2051 * Get file status; this version does not follow links.
2053 #ifndef _SYS_SYSPROTO_H_
2054 struct olstat_args {
2060 olstat(struct thread *td, struct olstat_args *uap)
2066 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2067 UIO_USERSPACE, &sb, NULL);
2071 return (copyout(&osb, uap->ub, sizeof (osb)));
2075 * Convert from an old to a new stat structure.
2078 cvtstat(struct stat *st, struct ostat *ost)
2081 bzero(ost, sizeof(*ost));
2082 ost->st_dev = st->st_dev;
2083 ost->st_ino = st->st_ino;
2084 ost->st_mode = st->st_mode;
2085 ost->st_nlink = st->st_nlink;
2086 ost->st_uid = st->st_uid;
2087 ost->st_gid = st->st_gid;
2088 ost->st_rdev = st->st_rdev;
2089 if (st->st_size < (quad_t)1 << 32)
2090 ost->st_size = st->st_size;
2093 ost->st_atim = st->st_atim;
2094 ost->st_mtim = st->st_mtim;
2095 ost->st_ctim = st->st_ctim;
2096 ost->st_blksize = st->st_blksize;
2097 ost->st_blocks = st->st_blocks;
2098 ost->st_flags = st->st_flags;
2099 ost->st_gen = st->st_gen;
2101 #endif /* COMPAT_43 */
2103 #if defined(COMPAT_43) || defined(COMPAT_FREEBSD11)
2104 int ino64_trunc_error;
2105 SYSCTL_INT(_vfs, OID_AUTO, ino64_trunc_error, CTLFLAG_RW,
2106 &ino64_trunc_error, 0,
2107 "Error on truncation of inode number, device id or link count");
2109 freebsd11_cvtstat(struct stat *st, struct freebsd11_stat *ost)
2112 ost->st_dev = st->st_dev;
2113 ost->st_ino = st->st_ino;
2114 if (ost->st_ino != st->st_ino) {
2115 switch (ino64_trunc_error) {
2122 ost->st_ino = UINT32_MAX;
2126 ost->st_mode = st->st_mode;
2127 ost->st_nlink = st->st_nlink;
2128 if (ost->st_nlink != st->st_nlink) {
2129 switch (ino64_trunc_error) {
2136 ost->st_nlink = UINT16_MAX;
2140 ost->st_uid = st->st_uid;
2141 ost->st_gid = st->st_gid;
2142 ost->st_rdev = st->st_rdev;
2143 ost->st_atim = st->st_atim;
2144 ost->st_mtim = st->st_mtim;
2145 ost->st_ctim = st->st_ctim;
2146 ost->st_size = st->st_size;
2147 ost->st_blocks = st->st_blocks;
2148 ost->st_blksize = st->st_blksize;
2149 ost->st_flags = st->st_flags;
2150 ost->st_gen = st->st_gen;
2152 ost->st_birthtim = st->st_birthtim;
2153 bzero((char *)&ost->st_birthtim + sizeof(ost->st_birthtim),
2154 sizeof(*ost) - offsetof(struct freebsd11_stat,
2155 st_birthtim) - sizeof(ost->st_birthtim));
2160 freebsd11_stat(struct thread *td, struct freebsd11_stat_args* uap)
2163 struct freebsd11_stat osb;
2166 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
2170 error = freebsd11_cvtstat(&sb, &osb);
2172 error = copyout(&osb, uap->ub, sizeof(osb));
2177 freebsd11_lstat(struct thread *td, struct freebsd11_lstat_args* uap)
2180 struct freebsd11_stat osb;
2183 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2184 UIO_USERSPACE, &sb, NULL);
2187 error = freebsd11_cvtstat(&sb, &osb);
2189 error = copyout(&osb, uap->ub, sizeof(osb));
2194 freebsd11_fhstat(struct thread *td, struct freebsd11_fhstat_args* uap)
2198 struct freebsd11_stat osb;
2201 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
2204 error = kern_fhstat(td, fh, &sb);
2207 error = freebsd11_cvtstat(&sb, &osb);
2209 error = copyout(&osb, uap->sb, sizeof(osb));
2214 freebsd11_fstatat(struct thread *td, struct freebsd11_fstatat_args* uap)
2217 struct freebsd11_stat osb;
2220 error = kern_statat(td, uap->flag, uap->fd, uap->path,
2221 UIO_USERSPACE, &sb, NULL);
2224 error = freebsd11_cvtstat(&sb, &osb);
2226 error = copyout(&osb, uap->buf, sizeof(osb));
2229 #endif /* COMPAT_FREEBSD11 */
2234 #ifndef _SYS_SYSPROTO_H_
2235 struct fstatat_args {
2243 sys_fstatat(struct thread *td, struct fstatat_args *uap)
2248 error = kern_statat(td, uap->flag, uap->fd, uap->path,
2249 UIO_USERSPACE, &sb, NULL);
2251 error = copyout(&sb, uap->buf, sizeof (sb));
2256 kern_statat(struct thread *td, int flag, int fd, char *path,
2257 enum uio_seg pathseg, struct stat *sbp,
2258 void (*hook)(struct vnode *vp, struct stat *sbp))
2260 struct nameidata nd;
2264 if (flag & ~AT_SYMLINK_NOFOLLOW)
2267 NDINIT_ATRIGHTS(&nd, LOOKUP, ((flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW :
2268 FOLLOW) | LOCKSHARED | LOCKLEAF | AUDITVNODE1, pathseg, path, fd,
2269 &cap_fstat_rights, td);
2271 if ((error = namei(&nd)) != 0)
2273 error = vn_stat(nd.ni_vp, &sb, td->td_ucred, NOCRED, td);
2275 SDT_PROBE2(vfs, , stat, mode, path, sb.st_mode);
2276 if (S_ISREG(sb.st_mode))
2277 SDT_PROBE2(vfs, , stat, reg, path, pathseg);
2278 if (__predict_false(hook != NULL))
2279 hook(nd.ni_vp, &sb);
2281 NDFREE(&nd, NDF_ONLY_PNBUF);
2285 #ifdef __STAT_TIME_T_EXT
2293 if (KTRPOINT(td, KTR_STRUCT))
2299 #if defined(COMPAT_FREEBSD11)
2301 * Implementation of the NetBSD [l]stat() functions.
2304 freebsd11_cvtnstat(struct stat *sb, struct nstat *nsb)
2307 bzero(nsb, sizeof(*nsb));
2308 nsb->st_dev = sb->st_dev;
2309 nsb->st_ino = sb->st_ino;
2310 nsb->st_mode = sb->st_mode;
2311 nsb->st_nlink = sb->st_nlink;
2312 nsb->st_uid = sb->st_uid;
2313 nsb->st_gid = sb->st_gid;
2314 nsb->st_rdev = sb->st_rdev;
2315 nsb->st_atim = sb->st_atim;
2316 nsb->st_mtim = sb->st_mtim;
2317 nsb->st_ctim = sb->st_ctim;
2318 nsb->st_size = sb->st_size;
2319 nsb->st_blocks = sb->st_blocks;
2320 nsb->st_blksize = sb->st_blksize;
2321 nsb->st_flags = sb->st_flags;
2322 nsb->st_gen = sb->st_gen;
2323 nsb->st_birthtim = sb->st_birthtim;
2326 #ifndef _SYS_SYSPROTO_H_
2327 struct freebsd11_nstat_args {
2333 freebsd11_nstat(struct thread *td, struct freebsd11_nstat_args *uap)
2339 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
2343 freebsd11_cvtnstat(&sb, &nsb);
2344 return (copyout(&nsb, uap->ub, sizeof (nsb)));
2348 * NetBSD lstat. Get file status; this version does not follow links.
2350 #ifndef _SYS_SYSPROTO_H_
2351 struct freebsd11_nlstat_args {
2357 freebsd11_nlstat(struct thread *td, struct freebsd11_nlstat_args *uap)
2363 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2364 UIO_USERSPACE, &sb, NULL);
2367 freebsd11_cvtnstat(&sb, &nsb);
2368 return (copyout(&nsb, uap->ub, sizeof (nsb)));
2370 #endif /* COMPAT_FREEBSD11 */
2373 * Get configurable pathname variables.
2375 #ifndef _SYS_SYSPROTO_H_
2376 struct pathconf_args {
2382 sys_pathconf(struct thread *td, struct pathconf_args *uap)
2387 error = kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name, FOLLOW,
2390 td->td_retval[0] = value;
2394 #ifndef _SYS_SYSPROTO_H_
2395 struct lpathconf_args {
2401 sys_lpathconf(struct thread *td, struct lpathconf_args *uap)
2406 error = kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name,
2409 td->td_retval[0] = value;
2414 kern_pathconf(struct thread *td, char *path, enum uio_seg pathseg, int name,
2415 u_long flags, long *valuep)
2417 struct nameidata nd;
2420 NDINIT(&nd, LOOKUP, LOCKSHARED | LOCKLEAF | AUDITVNODE1 | flags,
2422 if ((error = namei(&nd)) != 0)
2424 NDFREE(&nd, NDF_ONLY_PNBUF);
2426 error = VOP_PATHCONF(nd.ni_vp, name, valuep);
2432 * Return target name of a symbolic link.
2434 #ifndef _SYS_SYSPROTO_H_
2435 struct readlink_args {
2442 sys_readlink(struct thread *td, struct readlink_args *uap)
2445 return (kern_readlinkat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2446 uap->buf, UIO_USERSPACE, uap->count));
2448 #ifndef _SYS_SYSPROTO_H_
2449 struct readlinkat_args {
2457 sys_readlinkat(struct thread *td, struct readlinkat_args *uap)
2460 return (kern_readlinkat(td, uap->fd, uap->path, UIO_USERSPACE,
2461 uap->buf, UIO_USERSPACE, uap->bufsize));
2465 kern_readlinkat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2466 char *buf, enum uio_seg bufseg, size_t count)
2471 struct nameidata nd;
2474 if (count > IOSIZE_MAX)
2477 NDINIT_AT(&nd, LOOKUP, NOFOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
2478 pathseg, path, fd, td);
2480 if ((error = namei(&nd)) != 0)
2482 NDFREE(&nd, NDF_ONLY_PNBUF);
2485 error = mac_vnode_check_readlink(td->td_ucred, vp);
2491 if (vp->v_type != VLNK && (vp->v_vflag & VV_READLINK) == 0)
2494 aiov.iov_base = buf;
2495 aiov.iov_len = count;
2496 auio.uio_iov = &aiov;
2497 auio.uio_iovcnt = 1;
2498 auio.uio_offset = 0;
2499 auio.uio_rw = UIO_READ;
2500 auio.uio_segflg = bufseg;
2502 auio.uio_resid = count;
2503 error = VOP_READLINK(vp, &auio, td->td_ucred);
2504 td->td_retval[0] = count - auio.uio_resid;
2511 * Common implementation code for chflags() and fchflags().
2514 setfflags(struct thread *td, struct vnode *vp, u_long flags)
2520 /* We can't support the value matching VNOVAL. */
2521 if (flags == VNOVAL)
2522 return (EOPNOTSUPP);
2525 * Prevent non-root users from setting flags on devices. When
2526 * a device is reused, users can retain ownership of the device
2527 * if they are allowed to set flags and programs assume that
2528 * chown can't fail when done as root.
2530 if (vp->v_type == VCHR || vp->v_type == VBLK) {
2531 error = priv_check(td, PRIV_VFS_CHFLAGS_DEV);
2536 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2539 vattr.va_flags = flags;
2540 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2542 error = mac_vnode_check_setflags(td->td_ucred, vp, vattr.va_flags);
2545 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
2547 vn_finished_write(mp);
2552 * Change flags of a file given a path name.
2554 #ifndef _SYS_SYSPROTO_H_
2555 struct chflags_args {
2561 sys_chflags(struct thread *td, struct chflags_args *uap)
2564 return (kern_chflagsat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2568 #ifndef _SYS_SYSPROTO_H_
2569 struct chflagsat_args {
2577 sys_chflagsat(struct thread *td, struct chflagsat_args *uap)
2580 const char *path = uap->path;
2581 u_long flags = uap->flags;
2582 int atflag = uap->atflag;
2584 if (atflag & ~AT_SYMLINK_NOFOLLOW)
2587 return (kern_chflagsat(td, fd, path, UIO_USERSPACE, flags, atflag));
2591 * Same as chflags() but doesn't follow symlinks.
2593 #ifndef _SYS_SYSPROTO_H_
2594 struct lchflags_args {
2600 sys_lchflags(struct thread *td, struct lchflags_args *uap)
2603 return (kern_chflagsat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2604 uap->flags, AT_SYMLINK_NOFOLLOW));
2608 kern_chflagsat(struct thread *td, int fd, const char *path,
2609 enum uio_seg pathseg, u_long flags, int atflag)
2611 struct nameidata nd;
2612 cap_rights_t rights;
2615 AUDIT_ARG_FFLAGS(flags);
2616 follow = (atflag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
2617 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, pathseg, path, fd,
2618 cap_rights_init(&rights, CAP_FCHFLAGS), td);
2619 if ((error = namei(&nd)) != 0)
2621 NDFREE(&nd, NDF_ONLY_PNBUF);
2622 error = setfflags(td, nd.ni_vp, flags);
2628 * Change flags of a file given a file descriptor.
2630 #ifndef _SYS_SYSPROTO_H_
2631 struct fchflags_args {
2637 sys_fchflags(struct thread *td, struct fchflags_args *uap)
2640 cap_rights_t rights;
2643 AUDIT_ARG_FD(uap->fd);
2644 AUDIT_ARG_FFLAGS(uap->flags);
2645 error = getvnode(td, uap->fd, cap_rights_init(&rights, CAP_FCHFLAGS),
2650 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
2651 AUDIT_ARG_VNODE1(fp->f_vnode);
2652 VOP_UNLOCK(fp->f_vnode, 0);
2654 error = setfflags(td, fp->f_vnode, uap->flags);
2660 * Common implementation code for chmod(), lchmod() and fchmod().
2663 setfmode(struct thread *td, struct ucred *cred, struct vnode *vp, int mode)
2669 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2671 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2673 vattr.va_mode = mode & ALLPERMS;
2675 error = mac_vnode_check_setmode(cred, vp, vattr.va_mode);
2678 error = VOP_SETATTR(vp, &vattr, cred);
2680 vn_finished_write(mp);
2685 * Change mode of a file given path name.
2687 #ifndef _SYS_SYSPROTO_H_
2694 sys_chmod(struct thread *td, struct chmod_args *uap)
2697 return (kern_fchmodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2701 #ifndef _SYS_SYSPROTO_H_
2702 struct fchmodat_args {
2710 sys_fchmodat(struct thread *td, struct fchmodat_args *uap)
2712 int flag = uap->flag;
2714 char *path = uap->path;
2715 mode_t mode = uap->mode;
2717 if (flag & ~AT_SYMLINK_NOFOLLOW)
2720 return (kern_fchmodat(td, fd, path, UIO_USERSPACE, mode, flag));
2724 * Change mode of a file given path name (don't follow links.)
2726 #ifndef _SYS_SYSPROTO_H_
2727 struct lchmod_args {
2733 sys_lchmod(struct thread *td, struct lchmod_args *uap)
2736 return (kern_fchmodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2737 uap->mode, AT_SYMLINK_NOFOLLOW));
2741 kern_fchmodat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2742 mode_t mode, int flag)
2744 struct nameidata nd;
2745 cap_rights_t rights;
2748 AUDIT_ARG_MODE(mode);
2749 follow = (flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
2750 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, pathseg, path, fd,
2751 cap_rights_init(&rights, CAP_FCHMOD), td);
2752 if ((error = namei(&nd)) != 0)
2754 NDFREE(&nd, NDF_ONLY_PNBUF);
2755 error = setfmode(td, td->td_ucred, nd.ni_vp, mode);
2761 * Change mode of a file given a file descriptor.
2763 #ifndef _SYS_SYSPROTO_H_
2764 struct fchmod_args {
2770 sys_fchmod(struct thread *td, struct fchmod_args *uap)
2773 cap_rights_t rights;
2776 AUDIT_ARG_FD(uap->fd);
2777 AUDIT_ARG_MODE(uap->mode);
2779 error = fget(td, uap->fd, cap_rights_init(&rights, CAP_FCHMOD), &fp);
2782 error = fo_chmod(fp, uap->mode, td->td_ucred, td);
2788 * Common implementation for chown(), lchown(), and fchown()
2791 setfown(struct thread *td, struct ucred *cred, struct vnode *vp, uid_t uid,
2798 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2800 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2805 error = mac_vnode_check_setowner(cred, vp, vattr.va_uid,
2809 error = VOP_SETATTR(vp, &vattr, cred);
2811 vn_finished_write(mp);
2816 * Set ownership given a path name.
2818 #ifndef _SYS_SYSPROTO_H_
2826 sys_chown(struct thread *td, struct chown_args *uap)
2829 return (kern_fchownat(td, AT_FDCWD, uap->path, UIO_USERSPACE, uap->uid,
2833 #ifndef _SYS_SYSPROTO_H_
2834 struct fchownat_args {
2843 sys_fchownat(struct thread *td, struct fchownat_args *uap)
2848 if (flag & ~AT_SYMLINK_NOFOLLOW)
2851 return (kern_fchownat(td, uap->fd, uap->path, UIO_USERSPACE, uap->uid,
2852 uap->gid, uap->flag));
2856 kern_fchownat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2857 int uid, int gid, int flag)
2859 struct nameidata nd;
2860 cap_rights_t rights;
2863 AUDIT_ARG_OWNER(uid, gid);
2864 follow = (flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
2865 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, pathseg, path, fd,
2866 cap_rights_init(&rights, CAP_FCHOWN), td);
2868 if ((error = namei(&nd)) != 0)
2870 NDFREE(&nd, NDF_ONLY_PNBUF);
2871 error = setfown(td, td->td_ucred, nd.ni_vp, uid, gid);
2877 * Set ownership given a path name, do not cross symlinks.
2879 #ifndef _SYS_SYSPROTO_H_
2880 struct lchown_args {
2887 sys_lchown(struct thread *td, struct lchown_args *uap)
2890 return (kern_fchownat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2891 uap->uid, uap->gid, AT_SYMLINK_NOFOLLOW));
2895 * Set ownership given a file descriptor.
2897 #ifndef _SYS_SYSPROTO_H_
2898 struct fchown_args {
2905 sys_fchown(struct thread *td, struct fchown_args *uap)
2908 cap_rights_t rights;
2911 AUDIT_ARG_FD(uap->fd);
2912 AUDIT_ARG_OWNER(uap->uid, uap->gid);
2913 error = fget(td, uap->fd, cap_rights_init(&rights, CAP_FCHOWN), &fp);
2916 error = fo_chown(fp, uap->uid, uap->gid, td->td_ucred, td);
2922 * Common implementation code for utimes(), lutimes(), and futimes().
2925 getutimes(const struct timeval *usrtvp, enum uio_seg tvpseg,
2926 struct timespec *tsp)
2928 struct timeval tv[2];
2929 const struct timeval *tvp;
2932 if (usrtvp == NULL) {
2933 vfs_timestamp(&tsp[0]);
2936 if (tvpseg == UIO_SYSSPACE) {
2939 if ((error = copyin(usrtvp, tv, sizeof(tv))) != 0)
2944 if (tvp[0].tv_usec < 0 || tvp[0].tv_usec >= 1000000 ||
2945 tvp[1].tv_usec < 0 || tvp[1].tv_usec >= 1000000)
2947 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
2948 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
2954 * Common implementation code for futimens(), utimensat().
2956 #define UTIMENS_NULL 0x1
2957 #define UTIMENS_EXIT 0x2
2959 getutimens(const struct timespec *usrtsp, enum uio_seg tspseg,
2960 struct timespec *tsp, int *retflags)
2962 struct timespec tsnow;
2965 vfs_timestamp(&tsnow);
2967 if (usrtsp == NULL) {
2970 *retflags |= UTIMENS_NULL;
2973 if (tspseg == UIO_SYSSPACE) {
2976 } else if ((error = copyin(usrtsp, tsp, sizeof(*tsp) * 2)) != 0)
2978 if (tsp[0].tv_nsec == UTIME_OMIT && tsp[1].tv_nsec == UTIME_OMIT)
2979 *retflags |= UTIMENS_EXIT;
2980 if (tsp[0].tv_nsec == UTIME_NOW && tsp[1].tv_nsec == UTIME_NOW)
2981 *retflags |= UTIMENS_NULL;
2982 if (tsp[0].tv_nsec == UTIME_OMIT)
2983 tsp[0].tv_sec = VNOVAL;
2984 else if (tsp[0].tv_nsec == UTIME_NOW)
2986 else if (tsp[0].tv_nsec < 0 || tsp[0].tv_nsec >= 1000000000L)
2988 if (tsp[1].tv_nsec == UTIME_OMIT)
2989 tsp[1].tv_sec = VNOVAL;
2990 else if (tsp[1].tv_nsec == UTIME_NOW)
2992 else if (tsp[1].tv_nsec < 0 || tsp[1].tv_nsec >= 1000000000L)
2999 * Common implementation code for utimes(), lutimes(), futimes(), futimens(),
3003 setutimes(struct thread *td, struct vnode *vp, const struct timespec *ts,
3004 int numtimes, int nullflag)
3008 int error, setbirthtime;
3010 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
3012 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3014 if (numtimes < 3 && !VOP_GETATTR(vp, &vattr, td->td_ucred) &&
3015 timespeccmp(&ts[1], &vattr.va_birthtime, < ))
3018 vattr.va_atime = ts[0];
3019 vattr.va_mtime = ts[1];
3021 vattr.va_birthtime = ts[1];
3023 vattr.va_birthtime = ts[2];
3025 vattr.va_vaflags |= VA_UTIMES_NULL;
3027 error = mac_vnode_check_setutimes(td->td_ucred, vp, vattr.va_atime,
3031 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3033 vn_finished_write(mp);
3038 * Set the access and modification times of a file.
3040 #ifndef _SYS_SYSPROTO_H_
3041 struct utimes_args {
3043 struct timeval *tptr;
3047 sys_utimes(struct thread *td, struct utimes_args *uap)
3050 return (kern_utimesat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
3051 uap->tptr, UIO_USERSPACE));
3054 #ifndef _SYS_SYSPROTO_H_
3055 struct futimesat_args {
3058 const struct timeval * times;
3062 sys_futimesat(struct thread *td, struct futimesat_args *uap)
3065 return (kern_utimesat(td, uap->fd, uap->path, UIO_USERSPACE,
3066 uap->times, UIO_USERSPACE));
3070 kern_utimesat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
3071 struct timeval *tptr, enum uio_seg tptrseg)
3073 struct nameidata nd;
3074 struct timespec ts[2];
3075 cap_rights_t rights;
3078 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3080 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, fd,
3081 cap_rights_init(&rights, CAP_FUTIMES), td);
3083 if ((error = namei(&nd)) != 0)
3085 NDFREE(&nd, NDF_ONLY_PNBUF);
3086 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3092 * Set the access and modification times of a file.
3094 #ifndef _SYS_SYSPROTO_H_
3095 struct lutimes_args {
3097 struct timeval *tptr;
3101 sys_lutimes(struct thread *td, struct lutimes_args *uap)
3104 return (kern_lutimes(td, uap->path, UIO_USERSPACE, uap->tptr,
3109 kern_lutimes(struct thread *td, char *path, enum uio_seg pathseg,
3110 struct timeval *tptr, enum uio_seg tptrseg)
3112 struct timespec ts[2];
3113 struct nameidata nd;
3116 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3118 NDINIT(&nd, LOOKUP, NOFOLLOW | AUDITVNODE1, pathseg, path, td);
3119 if ((error = namei(&nd)) != 0)
3121 NDFREE(&nd, NDF_ONLY_PNBUF);
3122 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3128 * Set the access and modification times of a file.
3130 #ifndef _SYS_SYSPROTO_H_
3131 struct futimes_args {
3133 struct timeval *tptr;
3137 sys_futimes(struct thread *td, struct futimes_args *uap)
3140 return (kern_futimes(td, uap->fd, uap->tptr, UIO_USERSPACE));
3144 kern_futimes(struct thread *td, int fd, struct timeval *tptr,
3145 enum uio_seg tptrseg)
3147 struct timespec ts[2];
3149 cap_rights_t rights;
3153 error = getutimes(tptr, tptrseg, ts);
3156 error = getvnode(td, fd, cap_rights_init(&rights, CAP_FUTIMES), &fp);
3160 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
3161 AUDIT_ARG_VNODE1(fp->f_vnode);
3162 VOP_UNLOCK(fp->f_vnode, 0);
3164 error = setutimes(td, fp->f_vnode, ts, 2, tptr == NULL);
3170 sys_futimens(struct thread *td, struct futimens_args *uap)
3173 return (kern_futimens(td, uap->fd, uap->times, UIO_USERSPACE));
3177 kern_futimens(struct thread *td, int fd, struct timespec *tptr,
3178 enum uio_seg tptrseg)
3180 struct timespec ts[2];
3182 cap_rights_t rights;
3186 error = getutimens(tptr, tptrseg, ts, &flags);
3189 if (flags & UTIMENS_EXIT)
3191 error = getvnode(td, fd, cap_rights_init(&rights, CAP_FUTIMES), &fp);
3195 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
3196 AUDIT_ARG_VNODE1(fp->f_vnode);
3197 VOP_UNLOCK(fp->f_vnode, 0);
3199 error = setutimes(td, fp->f_vnode, ts, 2, flags & UTIMENS_NULL);
3205 sys_utimensat(struct thread *td, struct utimensat_args *uap)
3208 return (kern_utimensat(td, uap->fd, uap->path, UIO_USERSPACE,
3209 uap->times, UIO_USERSPACE, uap->flag));
3213 kern_utimensat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
3214 struct timespec *tptr, enum uio_seg tptrseg, int flag)
3216 struct nameidata nd;
3217 struct timespec ts[2];
3218 cap_rights_t rights;
3221 if (flag & ~AT_SYMLINK_NOFOLLOW)
3224 if ((error = getutimens(tptr, tptrseg, ts, &flags)) != 0)
3226 NDINIT_ATRIGHTS(&nd, LOOKUP, ((flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW :
3227 FOLLOW) | AUDITVNODE1, pathseg, path, fd,
3228 cap_rights_init(&rights, CAP_FUTIMES), td);
3229 if ((error = namei(&nd)) != 0)
3232 * We are allowed to call namei() regardless of 2xUTIME_OMIT.
3234 * "If both tv_nsec fields are UTIME_OMIT... EACCESS may be detected."
3235 * "Search permission is denied by a component of the path prefix."
3237 NDFREE(&nd, NDF_ONLY_PNBUF);
3238 if ((flags & UTIMENS_EXIT) == 0)
3239 error = setutimes(td, nd.ni_vp, ts, 2, flags & UTIMENS_NULL);
3245 * Truncate a file given its path name.
3247 #ifndef _SYS_SYSPROTO_H_
3248 struct truncate_args {
3255 sys_truncate(struct thread *td, struct truncate_args *uap)
3258 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3262 kern_truncate(struct thread *td, char *path, enum uio_seg pathseg, off_t length)
3268 struct nameidata nd;
3273 NDINIT(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, td);
3274 if ((error = namei(&nd)) != 0)
3277 rl_cookie = vn_rangelock_wlock(vp, 0, OFF_MAX);
3278 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0) {
3279 vn_rangelock_unlock(vp, rl_cookie);
3283 NDFREE(&nd, NDF_ONLY_PNBUF);
3284 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3285 if (vp->v_type == VDIR)
3288 else if ((error = mac_vnode_check_write(td->td_ucred, NOCRED, vp))) {
3291 else if ((error = vn_writechk(vp)) == 0 &&
3292 (error = VOP_ACCESS(vp, VWRITE, td->td_ucred, td)) == 0) {
3294 vattr.va_size = length;
3295 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3298 vn_finished_write(mp);
3299 vn_rangelock_unlock(vp, rl_cookie);
3304 #if defined(COMPAT_43)
3306 * Truncate a file given its path name.
3308 #ifndef _SYS_SYSPROTO_H_
3309 struct otruncate_args {
3315 otruncate(struct thread *td, struct otruncate_args *uap)
3318 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3320 #endif /* COMPAT_43 */
3322 #if defined(COMPAT_FREEBSD6)
3323 /* Versions with the pad argument */
3325 freebsd6_truncate(struct thread *td, struct freebsd6_truncate_args *uap)
3328 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3332 freebsd6_ftruncate(struct thread *td, struct freebsd6_ftruncate_args *uap)
3335 return (kern_ftruncate(td, uap->fd, uap->length));
3340 kern_fsync(struct thread *td, int fd, bool fullsync)
3345 cap_rights_t rights;
3346 int error, lock_flags;
3349 error = getvnode(td, fd, cap_rights_init(&rights, CAP_FSYNC), &fp);
3355 /* XXXKIB: compete outstanding aio writes */;
3357 error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
3360 if (MNT_SHARED_WRITES(mp) ||
3361 ((mp == NULL) && MNT_SHARED_WRITES(vp->v_mount))) {
3362 lock_flags = LK_SHARED;
3364 lock_flags = LK_EXCLUSIVE;
3366 vn_lock(vp, lock_flags | LK_RETRY);
3367 AUDIT_ARG_VNODE1(vp);
3368 if (vp->v_object != NULL) {
3369 VM_OBJECT_WLOCK(vp->v_object);
3370 vm_object_page_clean(vp->v_object, 0, 0, 0);
3371 VM_OBJECT_WUNLOCK(vp->v_object);
3373 error = fullsync ? VOP_FSYNC(vp, MNT_WAIT, td) : VOP_FDATASYNC(vp, td);
3375 vn_finished_write(mp);
3382 * Sync an open file.
3384 #ifndef _SYS_SYSPROTO_H_
3390 sys_fsync(struct thread *td, struct fsync_args *uap)
3393 return (kern_fsync(td, uap->fd, true));
3397 sys_fdatasync(struct thread *td, struct fdatasync_args *uap)
3400 return (kern_fsync(td, uap->fd, false));
3404 * Rename files. Source and destination must either both be directories, or
3405 * both not be directories. If target is a directory, it must be empty.
3407 #ifndef _SYS_SYSPROTO_H_
3408 struct rename_args {
3414 sys_rename(struct thread *td, struct rename_args *uap)
3417 return (kern_renameat(td, AT_FDCWD, uap->from, AT_FDCWD,
3418 uap->to, UIO_USERSPACE));
3421 #ifndef _SYS_SYSPROTO_H_
3422 struct renameat_args {
3430 sys_renameat(struct thread *td, struct renameat_args *uap)
3433 return (kern_renameat(td, uap->oldfd, uap->old, uap->newfd, uap->new,
3438 kern_renameat(struct thread *td, int oldfd, char *old, int newfd, char *new,
3439 enum uio_seg pathseg)
3441 struct mount *mp = NULL;
3442 struct vnode *tvp, *fvp, *tdvp;
3443 struct nameidata fromnd, tond;
3444 cap_rights_t rights;
3450 NDINIT_ATRIGHTS(&fromnd, DELETE, LOCKPARENT | LOCKLEAF | SAVESTART |
3451 AUDITVNODE1, pathseg, old, oldfd,
3452 cap_rights_init(&rights, CAP_RENAMEAT_SOURCE), td);
3454 NDINIT_ATRIGHTS(&fromnd, DELETE, WANTPARENT | SAVESTART | AUDITVNODE1,
3455 pathseg, old, oldfd,
3456 cap_rights_init(&rights, CAP_RENAMEAT_SOURCE), td);
3459 if ((error = namei(&fromnd)) != 0)
3462 error = mac_vnode_check_rename_from(td->td_ucred, fromnd.ni_dvp,
3463 fromnd.ni_vp, &fromnd.ni_cnd);
3464 VOP_UNLOCK(fromnd.ni_dvp, 0);
3465 if (fromnd.ni_dvp != fromnd.ni_vp)
3466 VOP_UNLOCK(fromnd.ni_vp, 0);
3469 NDINIT_ATRIGHTS(&tond, RENAME, LOCKPARENT | LOCKLEAF | NOCACHE |
3470 SAVESTART | AUDITVNODE2, pathseg, new, newfd,
3471 cap_rights_init(&rights, CAP_RENAMEAT_TARGET), td);
3472 if (fromnd.ni_vp->v_type == VDIR)
3473 tond.ni_cnd.cn_flags |= WILLBEDIR;
3474 if ((error = namei(&tond)) != 0) {
3475 /* Translate error code for rename("dir1", "dir2/."). */
3476 if (error == EISDIR && fvp->v_type == VDIR)
3478 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3479 vrele(fromnd.ni_dvp);
3485 error = vn_start_write(fvp, &mp, V_NOWAIT);
3487 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3488 NDFREE(&tond, NDF_ONLY_PNBUF);
3495 vrele(fromnd.ni_dvp);
3497 vrele(tond.ni_startdir);
3498 if (fromnd.ni_startdir != NULL)
3499 vrele(fromnd.ni_startdir);
3500 error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH);
3506 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
3509 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
3514 if (newfd != AT_FDCWD) {
3516 * If the target already exists we require CAP_UNLINKAT
3519 error = cap_check(&tond.ni_filecaps.fc_rights,
3520 cap_rights_init(&rights, CAP_UNLINKAT));
3531 * If the source is the same as the destination (that is, if they
3532 * are links to the same vnode), then there is nothing to do.
3538 error = mac_vnode_check_rename_to(td->td_ucred, tdvp,
3539 tond.ni_vp, fromnd.ni_dvp == tdvp, &tond.ni_cnd);
3543 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
3544 tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
3545 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3546 NDFREE(&tond, NDF_ONLY_PNBUF);
3548 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3549 NDFREE(&tond, NDF_ONLY_PNBUF);
3556 vrele(fromnd.ni_dvp);
3559 vrele(tond.ni_startdir);
3560 vn_finished_write(mp);
3562 if (fromnd.ni_startdir)
3563 vrele(fromnd.ni_startdir);
3570 * Make a directory file.
3572 #ifndef _SYS_SYSPROTO_H_
3579 sys_mkdir(struct thread *td, struct mkdir_args *uap)
3582 return (kern_mkdirat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
3586 #ifndef _SYS_SYSPROTO_H_
3587 struct mkdirat_args {
3594 sys_mkdirat(struct thread *td, struct mkdirat_args *uap)
3597 return (kern_mkdirat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode));
3601 kern_mkdirat(struct thread *td, int fd, char *path, enum uio_seg segflg,
3607 struct nameidata nd;
3608 cap_rights_t rights;
3611 AUDIT_ARG_MODE(mode);
3614 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
3615 NOCACHE, segflg, path, fd, cap_rights_init(&rights, CAP_MKDIRAT),
3617 nd.ni_cnd.cn_flags |= WILLBEDIR;
3618 if ((error = namei(&nd)) != 0)
3622 NDFREE(&nd, NDF_ONLY_PNBUF);
3624 * XXX namei called with LOCKPARENT but not LOCKLEAF has
3625 * the strange behaviour of leaving the vnode unlocked
3626 * if the target is the same vnode as the parent.
3628 if (vp == nd.ni_dvp)
3635 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3636 NDFREE(&nd, NDF_ONLY_PNBUF);
3638 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3643 vattr.va_type = VDIR;
3644 vattr.va_mode = (mode & ACCESSPERMS) &~ td->td_proc->p_fd->fd_cmask;
3646 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
3651 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
3655 NDFREE(&nd, NDF_ONLY_PNBUF);
3659 vn_finished_write(mp);
3664 * Remove a directory file.
3666 #ifndef _SYS_SYSPROTO_H_
3672 sys_rmdir(struct thread *td, struct rmdir_args *uap)
3675 return (kern_rmdirat(td, AT_FDCWD, uap->path, UIO_USERSPACE));
3679 kern_rmdirat(struct thread *td, int fd, char *path, enum uio_seg pathseg)
3683 struct nameidata nd;
3684 cap_rights_t rights;
3689 NDINIT_ATRIGHTS(&nd, DELETE, LOCKPARENT | LOCKLEAF | AUDITVNODE1,
3690 pathseg, path, fd, cap_rights_init(&rights, CAP_UNLINKAT), td);
3691 if ((error = namei(&nd)) != 0)
3694 if (vp->v_type != VDIR) {
3699 * No rmdir "." please.
3701 if (nd.ni_dvp == vp) {
3706 * The root of a mounted filesystem cannot be deleted.
3708 if (vp->v_vflag & VV_ROOT) {
3713 error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp,
3718 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3719 NDFREE(&nd, NDF_ONLY_PNBUF);
3721 if (nd.ni_dvp == vp)
3725 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3729 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_UNLINK);
3730 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
3731 vn_finished_write(mp);
3733 NDFREE(&nd, NDF_ONLY_PNBUF);
3735 if (nd.ni_dvp == vp)
3742 #if defined(COMPAT_43) || defined(COMPAT_FREEBSD11)
3744 freebsd11_kern_getdirentries(struct thread *td, int fd, char *ubuf, u_int count,
3745 long *basep, void (*func)(struct freebsd11_dirent *))
3747 struct freebsd11_dirent dstdp;
3748 struct dirent *dp, *edp;
3751 ssize_t resid, ucount;
3754 /* XXX arbitrary sanity limit on `count'. */
3755 count = min(count, 64 * 1024);
3757 dirbuf = malloc(count, M_TEMP, M_WAITOK);
3759 error = kern_getdirentries(td, fd, dirbuf, count, &base, &resid,
3767 for (dp = (struct dirent *)dirbuf,
3768 edp = (struct dirent *)&dirbuf[count - resid];
3769 ucount < count && dp < edp; ) {
3770 if (dp->d_reclen == 0)
3772 MPASS(dp->d_reclen >= _GENERIC_DIRLEN(0));
3773 if (dp->d_namlen >= sizeof(dstdp.d_name))
3775 dstdp.d_type = dp->d_type;
3776 dstdp.d_namlen = dp->d_namlen;
3777 dstdp.d_fileno = dp->d_fileno; /* truncate */
3778 if (dstdp.d_fileno != dp->d_fileno) {
3779 switch (ino64_trunc_error) {
3787 dstdp.d_fileno = UINT32_MAX;
3791 dstdp.d_reclen = sizeof(dstdp) - sizeof(dstdp.d_name) +
3792 ((dp->d_namlen + 1 + 3) &~ 3);
3793 bcopy(dp->d_name, dstdp.d_name, dstdp.d_namlen);
3794 bzero(dstdp.d_name + dstdp.d_namlen,
3795 dstdp.d_reclen - offsetof(struct freebsd11_dirent, d_name) -
3797 MPASS(dstdp.d_reclen <= dp->d_reclen);
3798 MPASS(ucount + dstdp.d_reclen <= count);
3801 error = copyout(&dstdp, ubuf + ucount, dstdp.d_reclen);
3804 dp = (struct dirent *)((char *)dp + dp->d_reclen);
3805 ucount += dstdp.d_reclen;
3809 free(dirbuf, M_TEMP);
3811 td->td_retval[0] = ucount;
3818 ogetdirentries_cvt(struct freebsd11_dirent *dp)
3820 #if (BYTE_ORDER == LITTLE_ENDIAN)
3822 * The expected low byte of dp->d_namlen is our dp->d_type.
3823 * The high MBZ byte of dp->d_namlen is our dp->d_namlen.
3825 dp->d_type = dp->d_namlen;
3829 * The dp->d_type is the high byte of the expected dp->d_namlen,
3830 * so must be zero'ed.
3837 * Read a block of directory entries in a filesystem independent format.
3839 #ifndef _SYS_SYSPROTO_H_
3840 struct ogetdirentries_args {
3848 ogetdirentries(struct thread *td, struct ogetdirentries_args *uap)
3853 error = kern_ogetdirentries(td, uap, &loff);
3855 error = copyout(&loff, uap->basep, sizeof(long));
3860 kern_ogetdirentries(struct thread *td, struct ogetdirentries_args *uap,
3866 /* XXX arbitrary sanity limit on `count'. */
3867 if (uap->count > 64 * 1024)
3870 error = freebsd11_kern_getdirentries(td, uap->fd, uap->buf, uap->count,
3871 &base, ogetdirentries_cvt);
3873 if (error == 0 && uap->basep != NULL)
3874 error = copyout(&base, uap->basep, sizeof(long));
3878 #endif /* COMPAT_43 */
3880 #if defined(COMPAT_FREEBSD11)
3881 #ifndef _SYS_SYSPROTO_H_
3882 struct freebsd11_getdirentries_args {
3890 freebsd11_getdirentries(struct thread *td,
3891 struct freebsd11_getdirentries_args *uap)
3896 error = freebsd11_kern_getdirentries(td, uap->fd, uap->buf, uap->count,
3899 if (error == 0 && uap->basep != NULL)
3900 error = copyout(&base, uap->basep, sizeof(long));
3905 freebsd11_getdents(struct thread *td, struct freebsd11_getdents_args *uap)
3907 struct freebsd11_getdirentries_args ap;
3911 ap.count = uap->count;
3913 return (freebsd11_getdirentries(td, &ap));
3915 #endif /* COMPAT_FREEBSD11 */
3918 * Read a block of directory entries in a filesystem independent format.
3921 sys_getdirentries(struct thread *td, struct getdirentries_args *uap)
3926 error = kern_getdirentries(td, uap->fd, uap->buf, uap->count, &base,
3927 NULL, UIO_USERSPACE);
3930 if (uap->basep != NULL)
3931 error = copyout(&base, uap->basep, sizeof(off_t));
3936 kern_getdirentries(struct thread *td, int fd, char *buf, size_t count,
3937 off_t *basep, ssize_t *residp, enum uio_seg bufseg)
3948 if (count > IOSIZE_MAX)
3950 auio.uio_resid = count;
3951 error = getvnode(td, fd, &cap_read_rights, &fp);
3954 if ((fp->f_flag & FREAD) == 0) {
3959 foffset = foffset_lock(fp, 0);
3961 if (vp->v_type != VDIR) {
3965 aiov.iov_base = buf;
3966 aiov.iov_len = count;
3967 auio.uio_iov = &aiov;
3968 auio.uio_iovcnt = 1;
3969 auio.uio_rw = UIO_READ;
3970 auio.uio_segflg = bufseg;
3972 vn_lock(vp, LK_SHARED | LK_RETRY);
3973 AUDIT_ARG_VNODE1(vp);
3974 loff = auio.uio_offset = foffset;
3976 error = mac_vnode_check_readdir(td->td_ucred, vp);
3979 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL,
3981 foffset = auio.uio_offset;
3986 if (count == auio.uio_resid &&
3987 (vp->v_vflag & VV_ROOT) &&
3988 (vp->v_mount->mnt_flag & MNT_UNION)) {
3989 struct vnode *tvp = vp;
3991 vp = vp->v_mount->mnt_vnodecovered;
4002 *residp = auio.uio_resid;
4003 td->td_retval[0] = count - auio.uio_resid;
4005 foffset_unlock(fp, foffset, 0);
4011 * Set the mode mask for creation of filesystem nodes.
4013 #ifndef _SYS_SYSPROTO_H_
4019 sys_umask(struct thread *td, struct umask_args *uap)
4021 struct filedesc *fdp;
4023 fdp = td->td_proc->p_fd;
4024 FILEDESC_XLOCK(fdp);
4025 td->td_retval[0] = fdp->fd_cmask;
4026 fdp->fd_cmask = uap->newmask & ALLPERMS;
4027 FILEDESC_XUNLOCK(fdp);
4032 * Void all references to file by ripping underlying filesystem away from
4035 #ifndef _SYS_SYSPROTO_H_
4036 struct revoke_args {
4041 sys_revoke(struct thread *td, struct revoke_args *uap)
4045 struct nameidata nd;
4048 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
4050 if ((error = namei(&nd)) != 0)
4053 NDFREE(&nd, NDF_ONLY_PNBUF);
4054 if (vp->v_type != VCHR || vp->v_rdev == NULL) {
4059 error = mac_vnode_check_revoke(td->td_ucred, vp);
4063 error = VOP_GETATTR(vp, &vattr, td->td_ucred);
4066 if (td->td_ucred->cr_uid != vattr.va_uid) {
4067 error = priv_check(td, PRIV_VFS_ADMIN);
4072 VOP_REVOKE(vp, REVOKEALL);
4079 * Convert a user file descriptor to a kernel file entry and check that, if it
4080 * is a capability, the correct rights are present. A reference on the file
4081 * entry is held upon returning.
4084 getvnode(struct thread *td, int fd, cap_rights_t *rightsp, struct file **fpp)
4089 error = fget_unlocked(td->td_proc->p_fd, fd, rightsp, &fp, NULL);
4094 * The file could be not of the vnode type, or it may be not
4095 * yet fully initialized, in which case the f_vnode pointer
4096 * may be set, but f_ops is still badfileops. E.g.,
4097 * devfs_open() transiently create such situation to
4098 * facilitate csw d_fdopen().
4100 * Dupfdopen() handling in kern_openat() installs the
4101 * half-baked file into the process descriptor table, allowing
4102 * other thread to dereference it. Guard against the race by
4105 if (fp->f_vnode == NULL || fp->f_ops == &badfileops) {
4115 * Get an (NFS) file handle.
4117 #ifndef _SYS_SYSPROTO_H_
4118 struct lgetfh_args {
4124 sys_lgetfh(struct thread *td, struct lgetfh_args *uap)
4126 struct nameidata nd;
4131 error = priv_check(td, PRIV_VFS_GETFH);
4134 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
4139 NDFREE(&nd, NDF_ONLY_PNBUF);
4141 bzero(&fh, sizeof(fh));
4142 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
4143 error = VOP_VPTOFH(vp, &fh.fh_fid);
4146 error = copyout(&fh, uap->fhp, sizeof (fh));
4150 #ifndef _SYS_SYSPROTO_H_
4157 sys_getfh(struct thread *td, struct getfh_args *uap)
4159 struct nameidata nd;
4164 error = priv_check(td, PRIV_VFS_GETFH);
4167 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
4172 NDFREE(&nd, NDF_ONLY_PNBUF);
4174 bzero(&fh, sizeof(fh));
4175 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
4176 error = VOP_VPTOFH(vp, &fh.fh_fid);
4179 error = copyout(&fh, uap->fhp, sizeof (fh));
4184 * syscall for the rpc.lockd to use to translate a NFS file handle into an
4187 * warning: do not remove the priv_check() call or this becomes one giant
4190 #ifndef _SYS_SYSPROTO_H_
4191 struct fhopen_args {
4192 const struct fhandle *u_fhp;
4197 sys_fhopen(struct thread *td, struct fhopen_args *uap)
4206 error = priv_check(td, PRIV_VFS_FHOPEN);
4210 fmode = FFLAGS(uap->flags);
4211 /* why not allow a non-read/write open for our lockd? */
4212 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
4214 error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
4217 /* find the mount point */
4218 mp = vfs_busyfs(&fhp.fh_fsid);
4221 /* now give me my vnode, it gets returned to me locked */
4222 error = VFS_FHTOVP(mp, &fhp.fh_fid, LK_EXCLUSIVE, &vp);
4227 error = falloc_noinstall(td, &fp);
4233 * An extra reference on `fp' has been held for us by
4234 * falloc_noinstall().
4240 error = vn_open_vnode(vp, fmode, td->td_ucred, td, fp);
4242 KASSERT(fp->f_ops == &badfileops,
4243 ("VOP_OPEN in fhopen() set f_ops"));
4244 KASSERT(td->td_dupfd < 0,
4245 ("fhopen() encountered fdopen()"));
4255 finit(fp, (fmode & FMASK) | (fp->f_flag & FHASLOCK), DTYPE_VNODE, vp,
4258 if ((fmode & O_TRUNC) != 0) {
4259 error = fo_truncate(fp, 0, td->td_ucred, td);
4264 error = finstall(td, fp, &indx, fmode, NULL);
4267 td->td_retval[0] = indx;
4272 * Stat an (NFS) file handle.
4274 #ifndef _SYS_SYSPROTO_H_
4275 struct fhstat_args {
4276 struct fhandle *u_fhp;
4281 sys_fhstat(struct thread *td, struct fhstat_args *uap)
4287 error = copyin(uap->u_fhp, &fh, sizeof(fh));
4290 error = kern_fhstat(td, fh, &sb);
4292 error = copyout(&sb, uap->sb, sizeof(sb));
4297 kern_fhstat(struct thread *td, struct fhandle fh, struct stat *sb)
4303 error = priv_check(td, PRIV_VFS_FHSTAT);
4306 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4308 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_EXCLUSIVE, &vp);
4312 error = vn_stat(vp, sb, td->td_ucred, NOCRED, td);
4318 * Implement fstatfs() for (NFS) file handles.
4320 #ifndef _SYS_SYSPROTO_H_
4321 struct fhstatfs_args {
4322 struct fhandle *u_fhp;
4327 sys_fhstatfs(struct thread *td, struct fhstatfs_args *uap)
4333 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4336 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
4337 error = kern_fhstatfs(td, fh, sfp);
4339 error = copyout(sfp, uap->buf, sizeof(*sfp));
4340 free(sfp, M_STATFS);
4345 kern_fhstatfs(struct thread *td, fhandle_t fh, struct statfs *buf)
4352 error = priv_check(td, PRIV_VFS_FHSTATFS);
4355 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4357 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_EXCLUSIVE, &vp);
4363 error = prison_canseemount(td->td_ucred, mp);
4367 error = mac_mount_check_stat(td->td_ucred, mp);
4372 * Set these in case the underlying filesystem fails to do so.
4375 sp->f_version = STATFS_VERSION;
4376 sp->f_namemax = NAME_MAX;
4377 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
4378 error = VFS_STATFS(mp, sp);
4387 kern_posix_fallocate(struct thread *td, int fd, off_t offset, off_t len)
4392 off_t olen, ooffset;
4395 int audited_vnode1 = 0;
4399 if (offset < 0 || len <= 0)
4401 /* Check for wrap. */
4402 if (offset > OFF_MAX - len)
4405 error = fget(td, fd, &cap_pwrite_rights, &fp);
4408 AUDIT_ARG_FILE(td->td_proc, fp);
4409 if ((fp->f_ops->fo_flags & DFLAG_SEEKABLE) == 0) {
4413 if ((fp->f_flag & FWRITE) == 0) {
4417 if (fp->f_type != DTYPE_VNODE) {
4422 if (vp->v_type != VREG) {
4427 /* Allocating blocks may take a long time, so iterate. */
4434 error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
4437 error = vn_lock(vp, LK_EXCLUSIVE);
4439 vn_finished_write(mp);
4443 if (!audited_vnode1) {
4444 AUDIT_ARG_VNODE1(vp);
4449 error = mac_vnode_check_write(td->td_ucred, fp->f_cred, vp);
4452 error = VOP_ALLOCATE(vp, &offset, &len);
4454 vn_finished_write(mp);
4456 if (olen + ooffset != offset + len) {
4457 panic("offset + len changed from %jx/%jx to %jx/%jx",
4458 ooffset, olen, offset, len);
4460 if (error != 0 || len == 0)
4462 KASSERT(olen > len, ("Iteration did not make progress?"));
4471 sys_posix_fallocate(struct thread *td, struct posix_fallocate_args *uap)
4475 error = kern_posix_fallocate(td, uap->fd, uap->offset, uap->len);
4476 return (kern_posix_error(td, error));
4480 * Unlike madvise(2), we do not make a best effort to remember every
4481 * possible caching hint. Instead, we remember the last setting with
4482 * the exception that we will allow POSIX_FADV_NORMAL to adjust the
4483 * region of any current setting.
4486 kern_posix_fadvise(struct thread *td, int fd, off_t offset, off_t len,
4489 struct fadvise_info *fa, *new;
4495 if (offset < 0 || len < 0 || offset > OFF_MAX - len)
4497 AUDIT_ARG_VALUE(advice);
4499 case POSIX_FADV_SEQUENTIAL:
4500 case POSIX_FADV_RANDOM:
4501 case POSIX_FADV_NOREUSE:
4502 new = malloc(sizeof(*fa), M_FADVISE, M_WAITOK);
4504 case POSIX_FADV_NORMAL:
4505 case POSIX_FADV_WILLNEED:
4506 case POSIX_FADV_DONTNEED:
4512 /* XXX: CAP_POSIX_FADVISE? */
4514 error = fget(td, fd, &cap_no_rights, &fp);
4517 AUDIT_ARG_FILE(td->td_proc, fp);
4518 if ((fp->f_ops->fo_flags & DFLAG_SEEKABLE) == 0) {
4522 if (fp->f_type != DTYPE_VNODE) {
4527 if (vp->v_type != VREG) {
4534 end = offset + len - 1;
4536 case POSIX_FADV_SEQUENTIAL:
4537 case POSIX_FADV_RANDOM:
4538 case POSIX_FADV_NOREUSE:
4540 * Try to merge any existing non-standard region with
4541 * this new region if possible, otherwise create a new
4542 * non-standard region for this request.
4544 mtx_pool_lock(mtxpool_sleep, fp);
4546 if (fa != NULL && fa->fa_advice == advice &&
4547 ((fa->fa_start <= end && fa->fa_end >= offset) ||
4548 (end != OFF_MAX && fa->fa_start == end + 1) ||
4549 (fa->fa_end != OFF_MAX && fa->fa_end + 1 == offset))) {
4550 if (offset < fa->fa_start)
4551 fa->fa_start = offset;
4552 if (end > fa->fa_end)
4555 new->fa_advice = advice;
4556 new->fa_start = offset;
4561 mtx_pool_unlock(mtxpool_sleep, fp);
4563 case POSIX_FADV_NORMAL:
4565 * If a the "normal" region overlaps with an existing
4566 * non-standard region, trim or remove the
4567 * non-standard region.
4569 mtx_pool_lock(mtxpool_sleep, fp);
4572 if (offset <= fa->fa_start && end >= fa->fa_end) {
4574 fp->f_advice = NULL;
4575 } else if (offset <= fa->fa_start &&
4576 end >= fa->fa_start)
4577 fa->fa_start = end + 1;
4578 else if (offset <= fa->fa_end && end >= fa->fa_end)
4579 fa->fa_end = offset - 1;
4580 else if (offset >= fa->fa_start && end <= fa->fa_end) {
4582 * If the "normal" region is a middle
4583 * portion of the existing
4584 * non-standard region, just remove
4585 * the whole thing rather than picking
4586 * one side or the other to
4590 fp->f_advice = NULL;
4593 mtx_pool_unlock(mtxpool_sleep, fp);
4595 case POSIX_FADV_WILLNEED:
4596 case POSIX_FADV_DONTNEED:
4597 error = VOP_ADVISE(vp, offset, end, advice);
4603 free(new, M_FADVISE);
4608 sys_posix_fadvise(struct thread *td, struct posix_fadvise_args *uap)
4612 error = kern_posix_fadvise(td, uap->fd, uap->offset, uap->len,
4614 return (kern_posix_error(td, error));