2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
4 * (c) UNIX System Laboratories, Inc.
5 * All or some portions of this file are derived from material licensed
6 * to the University of California by American Telephone and Telegraph
7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 * the permission of UNIX System Laboratories, Inc.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * @(#)vfs_syscalls.c 8.13 (Berkeley) 4/15/94
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
40 #include "opt_capsicum.h"
41 #include "opt_compat.h"
42 #include "opt_ktrace.h"
44 #include <sys/param.h>
45 #include <sys/systm.h>
48 #include <sys/capsicum.h>
50 #include <sys/sysent.h>
51 #include <sys/malloc.h>
52 #include <sys/mount.h>
53 #include <sys/mutex.h>
54 #include <sys/sysproto.h>
55 #include <sys/namei.h>
56 #include <sys/filedesc.h>
57 #include <sys/kernel.h>
58 #include <sys/fcntl.h>
60 #include <sys/filio.h>
61 #include <sys/limits.h>
62 #include <sys/linker.h>
63 #include <sys/rwlock.h>
67 #include <sys/unistd.h>
68 #include <sys/vnode.h>
71 #include <sys/dirent.h>
73 #include <sys/syscallsubr.h>
74 #include <sys/sysctl.h>
76 #include <sys/ktrace.h>
79 #include <machine/stdarg.h>
81 #include <security/audit/audit.h>
82 #include <security/mac/mac_framework.h>
85 #include <vm/vm_object.h>
86 #include <vm/vm_page.h>
89 #include <ufs/ufs/quota.h>
91 MALLOC_DEFINE(M_FADVISE, "fadvise", "posix_fadvise(2) information");
93 SDT_PROVIDER_DEFINE(vfs);
94 SDT_PROBE_DEFINE2(vfs, , stat, mode, "char *", "int");
95 SDT_PROBE_DEFINE2(vfs, , stat, reg, "char *", "int");
97 static int kern_chflagsat(struct thread *td, int fd, const char *path,
98 enum uio_seg pathseg, u_long flags, int atflag);
99 static int setfflags(struct thread *td, struct vnode *, u_long);
100 static int getutimes(const struct timeval *, enum uio_seg, struct timespec *);
101 static int getutimens(const struct timespec *, enum uio_seg,
102 struct timespec *, int *);
103 static int setutimes(struct thread *td, struct vnode *,
104 const struct timespec *, int, int);
105 static int vn_access(struct vnode *vp, int user_flags, struct ucred *cred,
109 * Sync each mounted filesystem.
111 #ifndef _SYS_SYSPROTO_H_
118 sys_sync(struct thread *td, struct sync_args *uap)
120 struct mount *mp, *nmp;
123 mtx_lock(&mountlist_mtx);
124 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
125 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK)) {
126 nmp = TAILQ_NEXT(mp, mnt_list);
129 if ((mp->mnt_flag & MNT_RDONLY) == 0 &&
130 vn_start_write(NULL, &mp, V_NOWAIT) == 0) {
131 save = curthread_pflags_set(TDP_SYNCIO);
132 vfs_msync(mp, MNT_NOWAIT);
133 VFS_SYNC(mp, MNT_NOWAIT);
134 curthread_pflags_restore(save);
135 vn_finished_write(mp);
137 mtx_lock(&mountlist_mtx);
138 nmp = TAILQ_NEXT(mp, mnt_list);
141 mtx_unlock(&mountlist_mtx);
146 * Change filesystem quotas.
148 #ifndef _SYS_SYSPROTO_H_
149 struct quotactl_args {
157 sys_quotactl(struct thread *td, struct quotactl_args *uap)
163 AUDIT_ARG_CMD(uap->cmd);
164 AUDIT_ARG_UID(uap->uid);
165 if (!prison_allow(td->td_ucred, PR_ALLOW_QUOTAS))
167 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
169 if ((error = namei(&nd)) != 0)
171 NDFREE(&nd, NDF_ONLY_PNBUF);
172 mp = nd.ni_vp->v_mount;
175 error = vfs_busy(mp, 0);
179 error = VFS_QUOTACTL(mp, uap->cmd, uap->uid, uap->arg);
182 * Since quota on operation typically needs to open quota
183 * file, the Q_QUOTAON handler needs to unbusy the mount point
184 * before calling into namei. Otherwise, unmount might be
185 * started between two vfs_busy() invocations (first is our,
186 * second is from mount point cross-walk code in lookup()),
189 * Require that Q_QUOTAON handles the vfs_busy() reference on
190 * its own, always returning with ubusied mount point.
192 if ((uap->cmd >> SUBCMDSHIFT) != Q_QUOTAON)
198 * Used by statfs conversion routines to scale the block size up if
199 * necessary so that all of the block counts are <= 'max_size'. Note
200 * that 'max_size' should be a bitmask, i.e. 2^n - 1 for some non-zero
204 statfs_scale_blocks(struct statfs *sf, long max_size)
209 KASSERT(powerof2(max_size + 1), ("%s: invalid max_size", __func__));
212 * Attempt to scale the block counts to give a more accurate
213 * overview to userland of the ratio of free space to used
214 * space. To do this, find the largest block count and compute
215 * a divisor that lets it fit into a signed integer <= max_size.
217 if (sf->f_bavail < 0)
218 count = -sf->f_bavail;
220 count = sf->f_bavail;
221 count = MAX(sf->f_blocks, MAX(sf->f_bfree, count));
222 if (count <= max_size)
225 count >>= flsl(max_size);
232 sf->f_bsize <<= shift;
233 sf->f_blocks >>= shift;
234 sf->f_bfree >>= shift;
235 sf->f_bavail >>= shift;
239 kern_do_statfs(struct thread *td, struct mount *mp, struct statfs *buf)
246 error = vfs_busy(mp, 0);
251 error = mac_mount_check_stat(td->td_ucred, mp);
256 * Set these in case the underlying filesystem fails to do so.
259 sp->f_version = STATFS_VERSION;
260 sp->f_namemax = NAME_MAX;
261 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
262 error = VFS_STATFS(mp, sp);
266 if (priv_check(td, PRIV_VFS_GENERATION)) {
267 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
268 prison_enforce_statfs(td->td_ucred, mp, buf);
276 * Get filesystem statistics.
278 #ifndef _SYS_SYSPROTO_H_
285 sys_statfs(struct thread *td, struct statfs_args *uap)
290 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
291 error = kern_statfs(td, uap->path, UIO_USERSPACE, sfp);
293 error = copyout(sfp, uap->buf, sizeof(struct statfs));
299 kern_statfs(struct thread *td, char *path, enum uio_seg pathseg,
306 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
311 mp = nd.ni_vp->v_mount;
313 NDFREE(&nd, NDF_ONLY_PNBUF);
315 return (kern_do_statfs(td, mp, buf));
319 * Get filesystem statistics.
321 #ifndef _SYS_SYSPROTO_H_
322 struct fstatfs_args {
328 sys_fstatfs(struct thread *td, struct fstatfs_args *uap)
333 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
334 error = kern_fstatfs(td, uap->fd, sfp);
336 error = copyout(sfp, uap->buf, sizeof(struct statfs));
342 kern_fstatfs(struct thread *td, int fd, struct statfs *buf)
351 error = getvnode(td, fd, cap_rights_init(&rights, CAP_FSTATFS), &fp);
355 vn_lock(vp, LK_SHARED | LK_RETRY);
357 AUDIT_ARG_VNODE1(vp);
364 return (kern_do_statfs(td, mp, buf));
368 * Get statistics on all filesystems.
370 #ifndef _SYS_SYSPROTO_H_
371 struct getfsstat_args {
378 sys_getfsstat(struct thread *td, struct getfsstat_args *uap)
383 if (uap->bufsize < 0 || uap->bufsize > SIZE_MAX)
385 error = kern_getfsstat(td, &uap->buf, uap->bufsize, &count,
386 UIO_USERSPACE, uap->mode);
388 td->td_retval[0] = count;
393 * If (bufsize > 0 && bufseg == UIO_SYSSPACE)
394 * The caller is responsible for freeing memory which will be allocated
398 kern_getfsstat(struct thread *td, struct statfs **buf, size_t bufsize,
399 size_t *countp, enum uio_seg bufseg, int mode)
401 struct mount *mp, *nmp;
402 struct statfs *sfsp, *sp, *sptmp, *tofree;
403 size_t count, maxcount;
411 if (bufseg == UIO_SYSSPACE)
416 maxcount = bufsize / sizeof(struct statfs);
420 } else if (bufseg == UIO_USERSPACE) {
423 } else /* if (bufseg == UIO_SYSSPACE) */ {
425 mtx_lock(&mountlist_mtx);
426 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
429 mtx_unlock(&mountlist_mtx);
430 if (maxcount > count)
432 tofree = sfsp = *buf = malloc(maxcount * sizeof(struct statfs),
436 mtx_lock(&mountlist_mtx);
437 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
438 if (prison_canseemount(td->td_ucred, mp) != 0) {
439 nmp = TAILQ_NEXT(mp, mnt_list);
443 if (mac_mount_check_stat(td->td_ucred, mp) != 0) {
444 nmp = TAILQ_NEXT(mp, mnt_list);
448 if (mode == MNT_WAIT) {
449 if (vfs_busy(mp, MBF_MNTLSTLOCK) != 0) {
451 * If vfs_busy() failed, and MBF_NOWAIT
452 * wasn't passed, then the mp is gone.
453 * Furthermore, because of MBF_MNTLSTLOCK,
454 * the mountlist_mtx was dropped. We have
455 * no other choice than to start over.
457 mtx_unlock(&mountlist_mtx);
458 free(tofree, M_STATFS);
462 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK) != 0) {
463 nmp = TAILQ_NEXT(mp, mnt_list);
467 if (sfsp != NULL && count < maxcount) {
470 * Set these in case the underlying filesystem
473 sp->f_version = STATFS_VERSION;
474 sp->f_namemax = NAME_MAX;
475 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
477 * If MNT_NOWAIT is specified, do not refresh
480 if (mode != MNT_NOWAIT) {
481 error = VFS_STATFS(mp, sp);
483 mtx_lock(&mountlist_mtx);
484 nmp = TAILQ_NEXT(mp, mnt_list);
489 if (priv_check(td, PRIV_VFS_GENERATION)) {
490 sptmp = malloc(sizeof(struct statfs), M_STATFS,
493 sptmp->f_fsid.val[0] = sptmp->f_fsid.val[1] = 0;
494 prison_enforce_statfs(td->td_ucred, mp, sptmp);
498 if (bufseg == UIO_SYSSPACE) {
499 bcopy(sp, sfsp, sizeof(*sp));
500 free(sptmp, M_STATFS);
501 } else /* if (bufseg == UIO_USERSPACE) */ {
502 error = copyout(sp, sfsp, sizeof(*sp));
503 free(sptmp, M_STATFS);
512 mtx_lock(&mountlist_mtx);
513 nmp = TAILQ_NEXT(mp, mnt_list);
516 mtx_unlock(&mountlist_mtx);
517 if (sfsp != NULL && count > maxcount)
524 #ifdef COMPAT_FREEBSD4
526 * Get old format filesystem statistics.
528 static void freebsd4_cvtstatfs(struct statfs *, struct ostatfs *);
530 #ifndef _SYS_SYSPROTO_H_
531 struct freebsd4_statfs_args {
537 freebsd4_statfs(struct thread *td, struct freebsd4_statfs_args *uap)
543 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
544 error = kern_statfs(td, uap->path, UIO_USERSPACE, sfp);
546 freebsd4_cvtstatfs(sfp, &osb);
547 error = copyout(&osb, uap->buf, sizeof(osb));
554 * Get filesystem statistics.
556 #ifndef _SYS_SYSPROTO_H_
557 struct freebsd4_fstatfs_args {
563 freebsd4_fstatfs(struct thread *td, struct freebsd4_fstatfs_args *uap)
569 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
570 error = kern_fstatfs(td, uap->fd, sfp);
572 freebsd4_cvtstatfs(sfp, &osb);
573 error = copyout(&osb, uap->buf, sizeof(osb));
580 * Get statistics on all filesystems.
582 #ifndef _SYS_SYSPROTO_H_
583 struct freebsd4_getfsstat_args {
590 freebsd4_getfsstat(struct thread *td, struct freebsd4_getfsstat_args *uap)
592 struct statfs *buf, *sp;
597 if (uap->bufsize < 0)
599 count = uap->bufsize / sizeof(struct ostatfs);
600 if (count > SIZE_MAX / sizeof(struct statfs))
602 size = count * sizeof(struct statfs);
603 error = kern_getfsstat(td, &buf, size, &count, UIO_SYSSPACE,
606 td->td_retval[0] = count;
609 while (count != 0 && error == 0) {
610 freebsd4_cvtstatfs(sp, &osb);
611 error = copyout(&osb, uap->buf, sizeof(osb));
622 * Implement fstatfs() for (NFS) file handles.
624 #ifndef _SYS_SYSPROTO_H_
625 struct freebsd4_fhstatfs_args {
626 struct fhandle *u_fhp;
631 freebsd4_fhstatfs(struct thread *td, struct freebsd4_fhstatfs_args *uap)
638 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
641 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
642 error = kern_fhstatfs(td, fh, sfp);
644 freebsd4_cvtstatfs(sfp, &osb);
645 error = copyout(&osb, uap->buf, sizeof(osb));
652 * Convert a new format statfs structure to an old format statfs structure.
655 freebsd4_cvtstatfs(struct statfs *nsp, struct ostatfs *osp)
658 statfs_scale_blocks(nsp, LONG_MAX);
659 bzero(osp, sizeof(*osp));
660 osp->f_bsize = nsp->f_bsize;
661 osp->f_iosize = MIN(nsp->f_iosize, LONG_MAX);
662 osp->f_blocks = nsp->f_blocks;
663 osp->f_bfree = nsp->f_bfree;
664 osp->f_bavail = nsp->f_bavail;
665 osp->f_files = MIN(nsp->f_files, LONG_MAX);
666 osp->f_ffree = MIN(nsp->f_ffree, LONG_MAX);
667 osp->f_owner = nsp->f_owner;
668 osp->f_type = nsp->f_type;
669 osp->f_flags = nsp->f_flags;
670 osp->f_syncwrites = MIN(nsp->f_syncwrites, LONG_MAX);
671 osp->f_asyncwrites = MIN(nsp->f_asyncwrites, LONG_MAX);
672 osp->f_syncreads = MIN(nsp->f_syncreads, LONG_MAX);
673 osp->f_asyncreads = MIN(nsp->f_asyncreads, LONG_MAX);
674 strlcpy(osp->f_fstypename, nsp->f_fstypename,
675 MIN(MFSNAMELEN, OMFSNAMELEN));
676 strlcpy(osp->f_mntonname, nsp->f_mntonname,
677 MIN(MNAMELEN, OMNAMELEN));
678 strlcpy(osp->f_mntfromname, nsp->f_mntfromname,
679 MIN(MNAMELEN, OMNAMELEN));
680 osp->f_fsid = nsp->f_fsid;
682 #endif /* COMPAT_FREEBSD4 */
684 #if defined(COMPAT_FREEBSD11)
686 * Get old format filesystem statistics.
688 static void freebsd11_cvtstatfs(struct statfs *, struct freebsd11_statfs *);
691 freebsd11_statfs(struct thread *td, struct freebsd11_statfs_args *uap)
693 struct freebsd11_statfs osb;
697 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
698 error = kern_statfs(td, uap->path, UIO_USERSPACE, sfp);
700 freebsd11_cvtstatfs(sfp, &osb);
701 error = copyout(&osb, uap->buf, sizeof(osb));
708 * Get filesystem statistics.
711 freebsd11_fstatfs(struct thread *td, struct freebsd11_fstatfs_args *uap)
713 struct freebsd11_statfs osb;
717 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
718 error = kern_fstatfs(td, uap->fd, sfp);
720 freebsd11_cvtstatfs(sfp, &osb);
721 error = copyout(&osb, uap->buf, sizeof(osb));
728 * Get statistics on all filesystems.
731 freebsd11_getfsstat(struct thread *td, struct freebsd11_getfsstat_args *uap)
733 struct freebsd11_statfs osb;
734 struct statfs *buf, *sp;
738 count = uap->bufsize / sizeof(struct ostatfs);
739 size = count * sizeof(struct statfs);
740 error = kern_getfsstat(td, &buf, size, &count, UIO_SYSSPACE,
743 td->td_retval[0] = count;
746 while (count > 0 && error == 0) {
747 freebsd11_cvtstatfs(sp, &osb);
748 error = copyout(&osb, uap->buf, sizeof(osb));
759 * Implement fstatfs() for (NFS) file handles.
762 freebsd11_fhstatfs(struct thread *td, struct freebsd11_fhstatfs_args *uap)
764 struct freebsd11_statfs osb;
769 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
772 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
773 error = kern_fhstatfs(td, fh, sfp);
775 freebsd11_cvtstatfs(sfp, &osb);
776 error = copyout(&osb, uap->buf, sizeof(osb));
783 * Convert a new format statfs structure to an old format statfs structure.
786 freebsd11_cvtstatfs(struct statfs *nsp, struct freebsd11_statfs *osp)
789 bzero(osp, sizeof(*osp));
790 osp->f_version = FREEBSD11_STATFS_VERSION;
791 osp->f_type = nsp->f_type;
792 osp->f_flags = nsp->f_flags;
793 osp->f_bsize = nsp->f_bsize;
794 osp->f_iosize = nsp->f_iosize;
795 osp->f_blocks = nsp->f_blocks;
796 osp->f_bfree = nsp->f_bfree;
797 osp->f_bavail = nsp->f_bavail;
798 osp->f_files = nsp->f_files;
799 osp->f_ffree = nsp->f_ffree;
800 osp->f_syncwrites = nsp->f_syncwrites;
801 osp->f_asyncwrites = nsp->f_asyncwrites;
802 osp->f_syncreads = nsp->f_syncreads;
803 osp->f_asyncreads = nsp->f_asyncreads;
804 osp->f_namemax = nsp->f_namemax;
805 osp->f_owner = nsp->f_owner;
806 osp->f_fsid = nsp->f_fsid;
807 strlcpy(osp->f_fstypename, nsp->f_fstypename,
808 MIN(MFSNAMELEN, sizeof(osp->f_fstypename)));
809 strlcpy(osp->f_mntonname, nsp->f_mntonname,
810 MIN(MNAMELEN, sizeof(osp->f_mntonname)));
811 strlcpy(osp->f_mntfromname, nsp->f_mntfromname,
812 MIN(MNAMELEN, sizeof(osp->f_mntfromname)));
814 #endif /* COMPAT_FREEBSD11 */
817 * Change current working directory to a given file descriptor.
819 #ifndef _SYS_SYSPROTO_H_
825 sys_fchdir(struct thread *td, struct fchdir_args *uap)
827 struct vnode *vp, *tdp;
833 AUDIT_ARG_FD(uap->fd);
834 error = getvnode(td, uap->fd, cap_rights_init(&rights, CAP_FCHDIR),
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_BAD);
1255 error = priv_check(td, PRIV_VFS_MKNOD_WHT);
1259 return (kern_mkfifoat(td, fd, path, pathseg, mode));
1269 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
1270 NOCACHE, pathseg, path, fd, cap_rights_init(&rights, CAP_MKNODAT),
1272 if ((error = namei(&nd)) != 0)
1276 NDFREE(&nd, NDF_ONLY_PNBUF);
1277 if (vp == nd.ni_dvp)
1285 vattr.va_mode = (mode & ALLPERMS) &
1286 ~td->td_proc->p_fd->fd_cmask;
1287 vattr.va_rdev = dev;
1290 switch (mode & S_IFMT) {
1291 case S_IFMT: /* used by badsect to flag bad sectors */
1292 vattr.va_type = VBAD;
1295 vattr.va_type = VCHR;
1298 vattr.va_type = VBLK;
1304 panic("kern_mknod: invalid mode");
1307 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1308 NDFREE(&nd, NDF_ONLY_PNBUF);
1310 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1315 if (error == 0 && !whiteout)
1316 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp,
1317 &nd.ni_cnd, &vattr);
1321 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, CREATE);
1323 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp,
1324 &nd.ni_cnd, &vattr);
1329 NDFREE(&nd, NDF_ONLY_PNBUF);
1331 vn_finished_write(mp);
1336 * Create a named pipe.
1338 #ifndef _SYS_SYSPROTO_H_
1339 struct mkfifo_args {
1345 sys_mkfifo(struct thread *td, struct mkfifo_args *uap)
1348 return (kern_mkfifoat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1352 #ifndef _SYS_SYSPROTO_H_
1353 struct mkfifoat_args {
1360 sys_mkfifoat(struct thread *td, struct mkfifoat_args *uap)
1363 return (kern_mkfifoat(td, uap->fd, uap->path, UIO_USERSPACE,
1368 kern_mkfifoat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1373 struct nameidata nd;
1374 cap_rights_t rights;
1377 AUDIT_ARG_MODE(mode);
1380 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
1381 NOCACHE, pathseg, path, fd, cap_rights_init(&rights, CAP_MKFIFOAT),
1383 if ((error = namei(&nd)) != 0)
1385 if (nd.ni_vp != NULL) {
1386 NDFREE(&nd, NDF_ONLY_PNBUF);
1387 if (nd.ni_vp == nd.ni_dvp)
1394 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1395 NDFREE(&nd, NDF_ONLY_PNBUF);
1397 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1402 vattr.va_type = VFIFO;
1403 vattr.va_mode = (mode & ALLPERMS) & ~td->td_proc->p_fd->fd_cmask;
1405 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1410 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
1417 vn_finished_write(mp);
1418 NDFREE(&nd, NDF_ONLY_PNBUF);
1423 * Make a hard file link.
1425 #ifndef _SYS_SYSPROTO_H_
1432 sys_link(struct thread *td, struct link_args *uap)
1435 return (kern_linkat(td, AT_FDCWD, AT_FDCWD, uap->path, uap->link,
1436 UIO_USERSPACE, FOLLOW));
1439 #ifndef _SYS_SYSPROTO_H_
1440 struct linkat_args {
1449 sys_linkat(struct thread *td, struct linkat_args *uap)
1454 if (flag & ~AT_SYMLINK_FOLLOW)
1457 return (kern_linkat(td, uap->fd1, uap->fd2, uap->path1, uap->path2,
1458 UIO_USERSPACE, (flag & AT_SYMLINK_FOLLOW) ? FOLLOW : NOFOLLOW));
1461 int hardlink_check_uid = 0;
1462 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
1463 &hardlink_check_uid, 0,
1464 "Unprivileged processes cannot create hard links to files owned by other "
1466 static int hardlink_check_gid = 0;
1467 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
1468 &hardlink_check_gid, 0,
1469 "Unprivileged processes cannot create hard links to files owned by other "
1473 can_hardlink(struct vnode *vp, struct ucred *cred)
1478 if (!hardlink_check_uid && !hardlink_check_gid)
1481 error = VOP_GETATTR(vp, &va, cred);
1485 if (hardlink_check_uid && cred->cr_uid != va.va_uid) {
1486 error = priv_check_cred(cred, PRIV_VFS_LINK, 0);
1491 if (hardlink_check_gid && !groupmember(va.va_gid, cred)) {
1492 error = priv_check_cred(cred, PRIV_VFS_LINK, 0);
1501 kern_linkat(struct thread *td, int fd1, int fd2, char *path1, char *path2,
1502 enum uio_seg segflg, int follow)
1506 struct nameidata nd;
1507 cap_rights_t rights;
1512 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, segflg, path1, fd1,
1513 cap_rights_init(&rights, CAP_LINKAT_SOURCE), td);
1515 if ((error = namei(&nd)) != 0)
1517 NDFREE(&nd, NDF_ONLY_PNBUF);
1519 if (vp->v_type == VDIR) {
1521 return (EPERM); /* POSIX */
1523 NDINIT_ATRIGHTS(&nd, CREATE,
1524 LOCKPARENT | SAVENAME | AUDITVNODE2 | NOCACHE, segflg, path2, fd2,
1525 cap_rights_init(&rights, CAP_LINKAT_TARGET), td);
1526 if ((error = namei(&nd)) == 0) {
1527 if (nd.ni_vp != NULL) {
1528 NDFREE(&nd, NDF_ONLY_PNBUF);
1529 if (nd.ni_dvp == nd.ni_vp)
1536 } else if (nd.ni_dvp->v_mount != vp->v_mount) {
1538 * Cross-device link. No need to recheck
1539 * vp->v_type, since it cannot change, except
1542 NDFREE(&nd, NDF_ONLY_PNBUF);
1546 } else if ((error = vn_lock(vp, LK_EXCLUSIVE)) == 0) {
1547 error = can_hardlink(vp, td->td_ucred);
1550 error = mac_vnode_check_link(td->td_ucred,
1551 nd.ni_dvp, vp, &nd.ni_cnd);
1556 NDFREE(&nd, NDF_ONLY_PNBUF);
1559 error = vn_start_write(vp, &mp, V_NOWAIT);
1563 NDFREE(&nd, NDF_ONLY_PNBUF);
1564 error = vn_start_write(NULL, &mp,
1570 error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
1573 vn_finished_write(mp);
1574 NDFREE(&nd, NDF_ONLY_PNBUF);
1577 NDFREE(&nd, NDF_ONLY_PNBUF);
1587 * Make a symbolic link.
1589 #ifndef _SYS_SYSPROTO_H_
1590 struct symlink_args {
1596 sys_symlink(struct thread *td, struct symlink_args *uap)
1599 return (kern_symlinkat(td, uap->path, AT_FDCWD, uap->link,
1603 #ifndef _SYS_SYSPROTO_H_
1604 struct symlinkat_args {
1611 sys_symlinkat(struct thread *td, struct symlinkat_args *uap)
1614 return (kern_symlinkat(td, uap->path1, uap->fd, uap->path2,
1619 kern_symlinkat(struct thread *td, char *path1, int fd, char *path2,
1620 enum uio_seg segflg)
1625 struct nameidata nd;
1627 cap_rights_t rights;
1629 if (segflg == UIO_SYSSPACE) {
1632 syspath = uma_zalloc(namei_zone, M_WAITOK);
1633 if ((error = copyinstr(path1, syspath, MAXPATHLEN, NULL)) != 0)
1636 AUDIT_ARG_TEXT(syspath);
1639 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
1640 NOCACHE, segflg, path2, fd, cap_rights_init(&rights, CAP_SYMLINKAT),
1642 if ((error = namei(&nd)) != 0)
1645 NDFREE(&nd, NDF_ONLY_PNBUF);
1646 if (nd.ni_vp == nd.ni_dvp)
1654 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1655 NDFREE(&nd, NDF_ONLY_PNBUF);
1657 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1662 vattr.va_mode = ACCESSPERMS &~ td->td_proc->p_fd->fd_cmask;
1664 vattr.va_type = VLNK;
1665 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1670 error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr, syspath);
1676 NDFREE(&nd, NDF_ONLY_PNBUF);
1678 vn_finished_write(mp);
1680 if (segflg != UIO_SYSSPACE)
1681 uma_zfree(namei_zone, syspath);
1686 * Delete a whiteout from the filesystem.
1688 #ifndef _SYS_SYSPROTO_H_
1689 struct undelete_args {
1694 sys_undelete(struct thread *td, struct undelete_args *uap)
1697 struct nameidata nd;
1702 NDINIT(&nd, DELETE, LOCKPARENT | DOWHITEOUT | AUDITVNODE1,
1703 UIO_USERSPACE, uap->path, td);
1708 if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & ISWHITEOUT)) {
1709 NDFREE(&nd, NDF_ONLY_PNBUF);
1710 if (nd.ni_vp == nd.ni_dvp)
1718 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1719 NDFREE(&nd, NDF_ONLY_PNBUF);
1721 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1725 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, DELETE);
1726 NDFREE(&nd, NDF_ONLY_PNBUF);
1728 vn_finished_write(mp);
1733 * Delete a name from the filesystem.
1735 #ifndef _SYS_SYSPROTO_H_
1736 struct unlink_args {
1741 sys_unlink(struct thread *td, struct unlink_args *uap)
1744 return (kern_unlinkat(td, AT_FDCWD, uap->path, UIO_USERSPACE, 0));
1747 #ifndef _SYS_SYSPROTO_H_
1748 struct unlinkat_args {
1755 sys_unlinkat(struct thread *td, struct unlinkat_args *uap)
1757 int flag = uap->flag;
1759 char *path = uap->path;
1761 if (flag & ~AT_REMOVEDIR)
1764 if (flag & AT_REMOVEDIR)
1765 return (kern_rmdirat(td, fd, path, UIO_USERSPACE));
1767 return (kern_unlinkat(td, fd, path, UIO_USERSPACE, 0));
1771 kern_unlinkat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1776 struct nameidata nd;
1778 cap_rights_t rights;
1783 NDINIT_ATRIGHTS(&nd, DELETE, LOCKPARENT | LOCKLEAF | AUDITVNODE1,
1784 pathseg, path, fd, cap_rights_init(&rights, CAP_UNLINKAT), td);
1785 if ((error = namei(&nd)) != 0)
1786 return (error == EINVAL ? EPERM : error);
1788 if (vp->v_type == VDIR && oldinum == 0) {
1789 error = EPERM; /* POSIX */
1790 } else if (oldinum != 0 &&
1791 ((error = vn_stat(vp, &sb, td->td_ucred, NOCRED, td)) == 0) &&
1792 sb.st_ino != oldinum) {
1793 error = EIDRM; /* Identifier removed */
1796 * The root of a mounted filesystem cannot be deleted.
1798 * XXX: can this only be a VDIR case?
1800 if (vp->v_vflag & VV_ROOT)
1804 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1805 NDFREE(&nd, NDF_ONLY_PNBUF);
1807 if (vp == nd.ni_dvp)
1811 if ((error = vn_start_write(NULL, &mp,
1812 V_XSLEEP | PCATCH)) != 0)
1817 error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp,
1822 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_UNLINK);
1823 error = VOP_REMOVE(nd.ni_dvp, vp, &nd.ni_cnd);
1827 vn_finished_write(mp);
1829 NDFREE(&nd, NDF_ONLY_PNBUF);
1831 if (vp == nd.ni_dvp)
1839 * Reposition read/write file offset.
1841 #ifndef _SYS_SYSPROTO_H_
1850 sys_lseek(struct thread *td, struct lseek_args *uap)
1853 return (kern_lseek(td, uap->fd, uap->offset, uap->whence));
1857 kern_lseek(struct thread *td, int fd, off_t offset, int whence)
1860 cap_rights_t rights;
1864 error = fget(td, fd, cap_rights_init(&rights, CAP_SEEK), &fp);
1867 error = (fp->f_ops->fo_flags & DFLAG_SEEKABLE) != 0 ?
1868 fo_seek(fp, offset, whence, td) : ESPIPE;
1873 #if defined(COMPAT_43)
1875 * Reposition read/write file offset.
1877 #ifndef _SYS_SYSPROTO_H_
1878 struct olseek_args {
1885 olseek(struct thread *td, struct olseek_args *uap)
1888 return (kern_lseek(td, uap->fd, uap->offset, uap->whence));
1890 #endif /* COMPAT_43 */
1892 #if defined(COMPAT_FREEBSD6)
1893 /* Version with the 'pad' argument */
1895 freebsd6_lseek(struct thread *td, struct freebsd6_lseek_args *uap)
1898 return (kern_lseek(td, uap->fd, uap->offset, uap->whence));
1903 * Check access permissions using passed credentials.
1906 vn_access(struct vnode *vp, int user_flags, struct ucred *cred,
1912 /* Flags == 0 means only check for existence. */
1913 if (user_flags == 0)
1917 if (user_flags & R_OK)
1919 if (user_flags & W_OK)
1921 if (user_flags & X_OK)
1924 error = mac_vnode_check_access(cred, vp, accmode);
1928 if ((accmode & VWRITE) == 0 || (error = vn_writechk(vp)) == 0)
1929 error = VOP_ACCESS(vp, accmode, cred, td);
1934 * Check access permissions using "real" credentials.
1936 #ifndef _SYS_SYSPROTO_H_
1937 struct access_args {
1943 sys_access(struct thread *td, struct access_args *uap)
1946 return (kern_accessat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1950 #ifndef _SYS_SYSPROTO_H_
1951 struct faccessat_args {
1959 sys_faccessat(struct thread *td, struct faccessat_args *uap)
1962 return (kern_accessat(td, uap->fd, uap->path, UIO_USERSPACE, uap->flag,
1967 kern_accessat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1968 int flag, int amode)
1970 struct ucred *cred, *usecred;
1972 struct nameidata nd;
1973 cap_rights_t rights;
1976 if (flag & ~AT_EACCESS)
1978 if (amode != F_OK && (amode & ~(R_OK | W_OK | X_OK)) != 0)
1982 * Create and modify a temporary credential instead of one that
1983 * is potentially shared (if we need one).
1985 cred = td->td_ucred;
1986 if ((flag & AT_EACCESS) == 0 &&
1987 ((cred->cr_uid != cred->cr_ruid ||
1988 cred->cr_rgid != cred->cr_groups[0]))) {
1989 usecred = crdup(cred);
1990 usecred->cr_uid = cred->cr_ruid;
1991 usecred->cr_groups[0] = cred->cr_rgid;
1992 td->td_ucred = usecred;
1995 AUDIT_ARG_VALUE(amode);
1996 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF |
1997 AUDITVNODE1, pathseg, path, fd, cap_rights_init(&rights, CAP_FSTAT),
1999 if ((error = namei(&nd)) != 0)
2003 error = vn_access(vp, amode, usecred, td);
2004 NDFREE(&nd, NDF_ONLY_PNBUF);
2007 if (usecred != cred) {
2008 td->td_ucred = cred;
2015 * Check access permissions using "effective" credentials.
2017 #ifndef _SYS_SYSPROTO_H_
2018 struct eaccess_args {
2024 sys_eaccess(struct thread *td, struct eaccess_args *uap)
2027 return (kern_accessat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2028 AT_EACCESS, uap->amode));
2031 #if defined(COMPAT_43)
2033 * Get file status; this version follows links.
2035 #ifndef _SYS_SYSPROTO_H_
2042 ostat(struct thread *td, struct ostat_args *uap)
2048 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
2053 return (copyout(&osb, uap->ub, sizeof (osb)));
2057 * Get file status; this version does not follow links.
2059 #ifndef _SYS_SYSPROTO_H_
2060 struct olstat_args {
2066 olstat(struct thread *td, struct olstat_args *uap)
2072 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2073 UIO_USERSPACE, &sb, NULL);
2077 return (copyout(&osb, uap->ub, sizeof (osb)));
2081 * Convert from an old to a new stat structure.
2084 cvtstat(struct stat *st, struct ostat *ost)
2087 bzero(ost, sizeof(*ost));
2088 ost->st_dev = st->st_dev;
2089 ost->st_ino = st->st_ino;
2090 ost->st_mode = st->st_mode;
2091 ost->st_nlink = st->st_nlink;
2092 ost->st_uid = st->st_uid;
2093 ost->st_gid = st->st_gid;
2094 ost->st_rdev = st->st_rdev;
2095 if (st->st_size < (quad_t)1 << 32)
2096 ost->st_size = st->st_size;
2099 ost->st_atim = st->st_atim;
2100 ost->st_mtim = st->st_mtim;
2101 ost->st_ctim = st->st_ctim;
2102 ost->st_blksize = st->st_blksize;
2103 ost->st_blocks = st->st_blocks;
2104 ost->st_flags = st->st_flags;
2105 ost->st_gen = st->st_gen;
2107 #endif /* COMPAT_43 */
2109 #if defined(COMPAT_43) || defined(COMPAT_FREEBSD11)
2110 int ino64_trunc_error;
2111 SYSCTL_INT(_vfs, OID_AUTO, ino64_trunc_error, CTLFLAG_RW,
2112 &ino64_trunc_error, 0,
2113 "Error on truncation of inode number, device id or link count");
2115 freebsd11_cvtstat(struct stat *st, struct freebsd11_stat *ost)
2118 ost->st_dev = st->st_dev;
2119 ost->st_ino = st->st_ino;
2120 if (ost->st_ino != st->st_ino) {
2121 switch (ino64_trunc_error) {
2128 ost->st_ino = UINT32_MAX;
2132 ost->st_mode = st->st_mode;
2133 ost->st_nlink = st->st_nlink;
2134 if (ost->st_nlink != st->st_nlink) {
2135 switch (ino64_trunc_error) {
2142 ost->st_nlink = UINT16_MAX;
2146 ost->st_uid = st->st_uid;
2147 ost->st_gid = st->st_gid;
2148 ost->st_rdev = st->st_rdev;
2149 ost->st_atim = st->st_atim;
2150 ost->st_mtim = st->st_mtim;
2151 ost->st_ctim = st->st_ctim;
2152 ost->st_size = st->st_size;
2153 ost->st_blocks = st->st_blocks;
2154 ost->st_blksize = st->st_blksize;
2155 ost->st_flags = st->st_flags;
2156 ost->st_gen = st->st_gen;
2158 ost->st_birthtim = st->st_birthtim;
2159 bzero((char *)&ost->st_birthtim + sizeof(ost->st_birthtim),
2160 sizeof(*ost) - offsetof(struct freebsd11_stat,
2161 st_birthtim) - sizeof(ost->st_birthtim));
2166 freebsd11_stat(struct thread *td, struct freebsd11_stat_args* uap)
2169 struct freebsd11_stat osb;
2172 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
2176 error = freebsd11_cvtstat(&sb, &osb);
2178 error = copyout(&osb, uap->ub, sizeof(osb));
2183 freebsd11_lstat(struct thread *td, struct freebsd11_lstat_args* uap)
2186 struct freebsd11_stat osb;
2189 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2190 UIO_USERSPACE, &sb, NULL);
2193 error = freebsd11_cvtstat(&sb, &osb);
2195 error = copyout(&osb, uap->ub, sizeof(osb));
2200 freebsd11_fhstat(struct thread *td, struct freebsd11_fhstat_args* uap)
2204 struct freebsd11_stat osb;
2207 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
2210 error = kern_fhstat(td, fh, &sb);
2213 error = freebsd11_cvtstat(&sb, &osb);
2215 error = copyout(&osb, uap->sb, sizeof(osb));
2220 freebsd11_fstatat(struct thread *td, struct freebsd11_fstatat_args* uap)
2223 struct freebsd11_stat osb;
2226 error = kern_statat(td, uap->flag, uap->fd, uap->path,
2227 UIO_USERSPACE, &sb, NULL);
2230 error = freebsd11_cvtstat(&sb, &osb);
2232 error = copyout(&osb, uap->buf, sizeof(osb));
2235 #endif /* COMPAT_FREEBSD11 */
2240 #ifndef _SYS_SYSPROTO_H_
2241 struct fstatat_args {
2249 sys_fstatat(struct thread *td, struct fstatat_args *uap)
2254 error = kern_statat(td, uap->flag, uap->fd, uap->path,
2255 UIO_USERSPACE, &sb, NULL);
2257 error = copyout(&sb, uap->buf, sizeof (sb));
2262 kern_statat(struct thread *td, int flag, int fd, char *path,
2263 enum uio_seg pathseg, struct stat *sbp,
2264 void (*hook)(struct vnode *vp, struct stat *sbp))
2266 struct nameidata nd;
2268 cap_rights_t rights;
2271 if (flag & ~AT_SYMLINK_NOFOLLOW)
2274 NDINIT_ATRIGHTS(&nd, LOOKUP, ((flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW :
2275 FOLLOW) | LOCKSHARED | LOCKLEAF | AUDITVNODE1, pathseg, path, fd,
2276 cap_rights_init(&rights, CAP_FSTAT), td);
2278 if ((error = namei(&nd)) != 0)
2280 error = vn_stat(nd.ni_vp, &sb, td->td_ucred, NOCRED, td);
2282 SDT_PROBE2(vfs, , stat, mode, path, sb.st_mode);
2283 if (S_ISREG(sb.st_mode))
2284 SDT_PROBE2(vfs, , stat, reg, path, pathseg);
2285 if (__predict_false(hook != NULL))
2286 hook(nd.ni_vp, &sb);
2288 NDFREE(&nd, NDF_ONLY_PNBUF);
2292 #ifdef __STAT_TIME_T_EXT
2300 if (KTRPOINT(td, KTR_STRUCT))
2306 #if defined(COMPAT_FREEBSD11)
2308 * Implementation of the NetBSD [l]stat() functions.
2311 freebsd11_cvtnstat(struct stat *sb, struct nstat *nsb)
2314 bzero(nsb, sizeof(*nsb));
2315 nsb->st_dev = sb->st_dev;
2316 nsb->st_ino = sb->st_ino;
2317 nsb->st_mode = sb->st_mode;
2318 nsb->st_nlink = sb->st_nlink;
2319 nsb->st_uid = sb->st_uid;
2320 nsb->st_gid = sb->st_gid;
2321 nsb->st_rdev = sb->st_rdev;
2322 nsb->st_atim = sb->st_atim;
2323 nsb->st_mtim = sb->st_mtim;
2324 nsb->st_ctim = sb->st_ctim;
2325 nsb->st_size = sb->st_size;
2326 nsb->st_blocks = sb->st_blocks;
2327 nsb->st_blksize = sb->st_blksize;
2328 nsb->st_flags = sb->st_flags;
2329 nsb->st_gen = sb->st_gen;
2330 nsb->st_birthtim = sb->st_birthtim;
2333 #ifndef _SYS_SYSPROTO_H_
2334 struct freebsd11_nstat_args {
2340 freebsd11_nstat(struct thread *td, struct freebsd11_nstat_args *uap)
2346 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
2350 freebsd11_cvtnstat(&sb, &nsb);
2351 return (copyout(&nsb, uap->ub, sizeof (nsb)));
2355 * NetBSD lstat. Get file status; this version does not follow links.
2357 #ifndef _SYS_SYSPROTO_H_
2358 struct freebsd11_nlstat_args {
2364 freebsd11_nlstat(struct thread *td, struct freebsd11_nlstat_args *uap)
2370 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2371 UIO_USERSPACE, &sb, NULL);
2374 freebsd11_cvtnstat(&sb, &nsb);
2375 return (copyout(&nsb, uap->ub, sizeof (nsb)));
2377 #endif /* COMPAT_FREEBSD11 */
2380 * Get configurable pathname variables.
2382 #ifndef _SYS_SYSPROTO_H_
2383 struct pathconf_args {
2389 sys_pathconf(struct thread *td, struct pathconf_args *uap)
2392 return (kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name, FOLLOW));
2395 #ifndef _SYS_SYSPROTO_H_
2396 struct lpathconf_args {
2402 sys_lpathconf(struct thread *td, struct lpathconf_args *uap)
2405 return (kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name,
2410 kern_pathconf(struct thread *td, char *path, enum uio_seg pathseg, int name,
2413 struct nameidata nd;
2416 NDINIT(&nd, LOOKUP, LOCKSHARED | LOCKLEAF | AUDITVNODE1 | flags,
2418 if ((error = namei(&nd)) != 0)
2420 NDFREE(&nd, NDF_ONLY_PNBUF);
2422 error = VOP_PATHCONF(nd.ni_vp, name, td->td_retval);
2428 * Return target name of a symbolic link.
2430 #ifndef _SYS_SYSPROTO_H_
2431 struct readlink_args {
2438 sys_readlink(struct thread *td, struct readlink_args *uap)
2441 return (kern_readlinkat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2442 uap->buf, UIO_USERSPACE, uap->count));
2444 #ifndef _SYS_SYSPROTO_H_
2445 struct readlinkat_args {
2453 sys_readlinkat(struct thread *td, struct readlinkat_args *uap)
2456 return (kern_readlinkat(td, uap->fd, uap->path, UIO_USERSPACE,
2457 uap->buf, UIO_USERSPACE, uap->bufsize));
2461 kern_readlinkat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2462 char *buf, enum uio_seg bufseg, size_t count)
2467 struct nameidata nd;
2470 if (count > IOSIZE_MAX)
2473 NDINIT_AT(&nd, LOOKUP, NOFOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
2474 pathseg, path, fd, td);
2476 if ((error = namei(&nd)) != 0)
2478 NDFREE(&nd, NDF_ONLY_PNBUF);
2481 error = mac_vnode_check_readlink(td->td_ucred, vp);
2487 if (vp->v_type != VLNK && (vp->v_vflag & VV_READLINK) == 0)
2490 aiov.iov_base = buf;
2491 aiov.iov_len = count;
2492 auio.uio_iov = &aiov;
2493 auio.uio_iovcnt = 1;
2494 auio.uio_offset = 0;
2495 auio.uio_rw = UIO_READ;
2496 auio.uio_segflg = bufseg;
2498 auio.uio_resid = count;
2499 error = VOP_READLINK(vp, &auio, td->td_ucred);
2500 td->td_retval[0] = count - auio.uio_resid;
2507 * Common implementation code for chflags() and fchflags().
2510 setfflags(struct thread *td, struct vnode *vp, u_long flags)
2516 /* We can't support the value matching VNOVAL. */
2517 if (flags == VNOVAL)
2518 return (EOPNOTSUPP);
2521 * Prevent non-root users from setting flags on devices. When
2522 * a device is reused, users can retain ownership of the device
2523 * if they are allowed to set flags and programs assume that
2524 * chown can't fail when done as root.
2526 if (vp->v_type == VCHR || vp->v_type == VBLK) {
2527 error = priv_check(td, PRIV_VFS_CHFLAGS_DEV);
2532 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2535 vattr.va_flags = flags;
2536 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2538 error = mac_vnode_check_setflags(td->td_ucred, vp, vattr.va_flags);
2541 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
2543 vn_finished_write(mp);
2548 * Change flags of a file given a path name.
2550 #ifndef _SYS_SYSPROTO_H_
2551 struct chflags_args {
2557 sys_chflags(struct thread *td, struct chflags_args *uap)
2560 return (kern_chflagsat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2564 #ifndef _SYS_SYSPROTO_H_
2565 struct chflagsat_args {
2573 sys_chflagsat(struct thread *td, struct chflagsat_args *uap)
2576 const char *path = uap->path;
2577 u_long flags = uap->flags;
2578 int atflag = uap->atflag;
2580 if (atflag & ~AT_SYMLINK_NOFOLLOW)
2583 return (kern_chflagsat(td, fd, path, UIO_USERSPACE, flags, atflag));
2587 * Same as chflags() but doesn't follow symlinks.
2589 #ifndef _SYS_SYSPROTO_H_
2590 struct lchflags_args {
2596 sys_lchflags(struct thread *td, struct lchflags_args *uap)
2599 return (kern_chflagsat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2600 uap->flags, AT_SYMLINK_NOFOLLOW));
2604 kern_chflagsat(struct thread *td, int fd, const char *path,
2605 enum uio_seg pathseg, u_long flags, int atflag)
2607 struct nameidata nd;
2608 cap_rights_t rights;
2611 AUDIT_ARG_FFLAGS(flags);
2612 follow = (atflag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
2613 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, pathseg, path, fd,
2614 cap_rights_init(&rights, CAP_FCHFLAGS), td);
2615 if ((error = namei(&nd)) != 0)
2617 NDFREE(&nd, NDF_ONLY_PNBUF);
2618 error = setfflags(td, nd.ni_vp, flags);
2624 * Change flags of a file given a file descriptor.
2626 #ifndef _SYS_SYSPROTO_H_
2627 struct fchflags_args {
2633 sys_fchflags(struct thread *td, struct fchflags_args *uap)
2636 cap_rights_t rights;
2639 AUDIT_ARG_FD(uap->fd);
2640 AUDIT_ARG_FFLAGS(uap->flags);
2641 error = getvnode(td, uap->fd, cap_rights_init(&rights, CAP_FCHFLAGS),
2646 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
2647 AUDIT_ARG_VNODE1(fp->f_vnode);
2648 VOP_UNLOCK(fp->f_vnode, 0);
2650 error = setfflags(td, fp->f_vnode, uap->flags);
2656 * Common implementation code for chmod(), lchmod() and fchmod().
2659 setfmode(struct thread *td, struct ucred *cred, struct vnode *vp, int mode)
2665 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2667 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2669 vattr.va_mode = mode & ALLPERMS;
2671 error = mac_vnode_check_setmode(cred, vp, vattr.va_mode);
2674 error = VOP_SETATTR(vp, &vattr, cred);
2676 vn_finished_write(mp);
2681 * Change mode of a file given path name.
2683 #ifndef _SYS_SYSPROTO_H_
2690 sys_chmod(struct thread *td, struct chmod_args *uap)
2693 return (kern_fchmodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2697 #ifndef _SYS_SYSPROTO_H_
2698 struct fchmodat_args {
2706 sys_fchmodat(struct thread *td, struct fchmodat_args *uap)
2708 int flag = uap->flag;
2710 char *path = uap->path;
2711 mode_t mode = uap->mode;
2713 if (flag & ~AT_SYMLINK_NOFOLLOW)
2716 return (kern_fchmodat(td, fd, path, UIO_USERSPACE, mode, flag));
2720 * Change mode of a file given path name (don't follow links.)
2722 #ifndef _SYS_SYSPROTO_H_
2723 struct lchmod_args {
2729 sys_lchmod(struct thread *td, struct lchmod_args *uap)
2732 return (kern_fchmodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2733 uap->mode, AT_SYMLINK_NOFOLLOW));
2737 kern_fchmodat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2738 mode_t mode, int flag)
2740 struct nameidata nd;
2741 cap_rights_t rights;
2744 AUDIT_ARG_MODE(mode);
2745 follow = (flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
2746 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, pathseg, path, fd,
2747 cap_rights_init(&rights, CAP_FCHMOD), td);
2748 if ((error = namei(&nd)) != 0)
2750 NDFREE(&nd, NDF_ONLY_PNBUF);
2751 error = setfmode(td, td->td_ucred, nd.ni_vp, mode);
2757 * Change mode of a file given a file descriptor.
2759 #ifndef _SYS_SYSPROTO_H_
2760 struct fchmod_args {
2766 sys_fchmod(struct thread *td, struct fchmod_args *uap)
2769 cap_rights_t rights;
2772 AUDIT_ARG_FD(uap->fd);
2773 AUDIT_ARG_MODE(uap->mode);
2775 error = fget(td, uap->fd, cap_rights_init(&rights, CAP_FCHMOD), &fp);
2778 error = fo_chmod(fp, uap->mode, td->td_ucred, td);
2784 * Common implementation for chown(), lchown(), and fchown()
2787 setfown(struct thread *td, struct ucred *cred, struct vnode *vp, uid_t uid,
2794 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2796 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2801 error = mac_vnode_check_setowner(cred, vp, vattr.va_uid,
2805 error = VOP_SETATTR(vp, &vattr, cred);
2807 vn_finished_write(mp);
2812 * Set ownership given a path name.
2814 #ifndef _SYS_SYSPROTO_H_
2822 sys_chown(struct thread *td, struct chown_args *uap)
2825 return (kern_fchownat(td, AT_FDCWD, uap->path, UIO_USERSPACE, uap->uid,
2829 #ifndef _SYS_SYSPROTO_H_
2830 struct fchownat_args {
2839 sys_fchownat(struct thread *td, struct fchownat_args *uap)
2844 if (flag & ~AT_SYMLINK_NOFOLLOW)
2847 return (kern_fchownat(td, uap->fd, uap->path, UIO_USERSPACE, uap->uid,
2848 uap->gid, uap->flag));
2852 kern_fchownat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2853 int uid, int gid, int flag)
2855 struct nameidata nd;
2856 cap_rights_t rights;
2859 AUDIT_ARG_OWNER(uid, gid);
2860 follow = (flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
2861 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, pathseg, path, fd,
2862 cap_rights_init(&rights, CAP_FCHOWN), td);
2864 if ((error = namei(&nd)) != 0)
2866 NDFREE(&nd, NDF_ONLY_PNBUF);
2867 error = setfown(td, td->td_ucred, nd.ni_vp, uid, gid);
2873 * Set ownership given a path name, do not cross symlinks.
2875 #ifndef _SYS_SYSPROTO_H_
2876 struct lchown_args {
2883 sys_lchown(struct thread *td, struct lchown_args *uap)
2886 return (kern_fchownat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2887 uap->uid, uap->gid, AT_SYMLINK_NOFOLLOW));
2891 * Set ownership given a file descriptor.
2893 #ifndef _SYS_SYSPROTO_H_
2894 struct fchown_args {
2901 sys_fchown(struct thread *td, struct fchown_args *uap)
2904 cap_rights_t rights;
2907 AUDIT_ARG_FD(uap->fd);
2908 AUDIT_ARG_OWNER(uap->uid, uap->gid);
2909 error = fget(td, uap->fd, cap_rights_init(&rights, CAP_FCHOWN), &fp);
2912 error = fo_chown(fp, uap->uid, uap->gid, td->td_ucred, td);
2918 * Common implementation code for utimes(), lutimes(), and futimes().
2921 getutimes(const struct timeval *usrtvp, enum uio_seg tvpseg,
2922 struct timespec *tsp)
2924 struct timeval tv[2];
2925 const struct timeval *tvp;
2928 if (usrtvp == NULL) {
2929 vfs_timestamp(&tsp[0]);
2932 if (tvpseg == UIO_SYSSPACE) {
2935 if ((error = copyin(usrtvp, tv, sizeof(tv))) != 0)
2940 if (tvp[0].tv_usec < 0 || tvp[0].tv_usec >= 1000000 ||
2941 tvp[1].tv_usec < 0 || tvp[1].tv_usec >= 1000000)
2943 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
2944 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
2950 * Common implementation code for futimens(), utimensat().
2952 #define UTIMENS_NULL 0x1
2953 #define UTIMENS_EXIT 0x2
2955 getutimens(const struct timespec *usrtsp, enum uio_seg tspseg,
2956 struct timespec *tsp, int *retflags)
2958 struct timespec tsnow;
2961 vfs_timestamp(&tsnow);
2963 if (usrtsp == NULL) {
2966 *retflags |= UTIMENS_NULL;
2969 if (tspseg == UIO_SYSSPACE) {
2972 } else if ((error = copyin(usrtsp, tsp, sizeof(*tsp) * 2)) != 0)
2974 if (tsp[0].tv_nsec == UTIME_OMIT && tsp[1].tv_nsec == UTIME_OMIT)
2975 *retflags |= UTIMENS_EXIT;
2976 if (tsp[0].tv_nsec == UTIME_NOW && tsp[1].tv_nsec == UTIME_NOW)
2977 *retflags |= UTIMENS_NULL;
2978 if (tsp[0].tv_nsec == UTIME_OMIT)
2979 tsp[0].tv_sec = VNOVAL;
2980 else if (tsp[0].tv_nsec == UTIME_NOW)
2982 else if (tsp[0].tv_nsec < 0 || tsp[0].tv_nsec >= 1000000000L)
2984 if (tsp[1].tv_nsec == UTIME_OMIT)
2985 tsp[1].tv_sec = VNOVAL;
2986 else if (tsp[1].tv_nsec == UTIME_NOW)
2988 else if (tsp[1].tv_nsec < 0 || tsp[1].tv_nsec >= 1000000000L)
2995 * Common implementation code for utimes(), lutimes(), futimes(), futimens(),
2999 setutimes(struct thread *td, struct vnode *vp, const struct timespec *ts,
3000 int numtimes, int nullflag)
3004 int error, setbirthtime;
3006 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
3008 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3010 if (numtimes < 3 && !VOP_GETATTR(vp, &vattr, td->td_ucred) &&
3011 timespeccmp(&ts[1], &vattr.va_birthtime, < ))
3014 vattr.va_atime = ts[0];
3015 vattr.va_mtime = ts[1];
3017 vattr.va_birthtime = ts[1];
3019 vattr.va_birthtime = ts[2];
3021 vattr.va_vaflags |= VA_UTIMES_NULL;
3023 error = mac_vnode_check_setutimes(td->td_ucred, vp, vattr.va_atime,
3027 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3029 vn_finished_write(mp);
3034 * Set the access and modification times of a file.
3036 #ifndef _SYS_SYSPROTO_H_
3037 struct utimes_args {
3039 struct timeval *tptr;
3043 sys_utimes(struct thread *td, struct utimes_args *uap)
3046 return (kern_utimesat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
3047 uap->tptr, UIO_USERSPACE));
3050 #ifndef _SYS_SYSPROTO_H_
3051 struct futimesat_args {
3054 const struct timeval * times;
3058 sys_futimesat(struct thread *td, struct futimesat_args *uap)
3061 return (kern_utimesat(td, uap->fd, uap->path, UIO_USERSPACE,
3062 uap->times, UIO_USERSPACE));
3066 kern_utimesat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
3067 struct timeval *tptr, enum uio_seg tptrseg)
3069 struct nameidata nd;
3070 struct timespec ts[2];
3071 cap_rights_t rights;
3074 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3076 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, fd,
3077 cap_rights_init(&rights, CAP_FUTIMES), td);
3079 if ((error = namei(&nd)) != 0)
3081 NDFREE(&nd, NDF_ONLY_PNBUF);
3082 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3088 * Set the access and modification times of a file.
3090 #ifndef _SYS_SYSPROTO_H_
3091 struct lutimes_args {
3093 struct timeval *tptr;
3097 sys_lutimes(struct thread *td, struct lutimes_args *uap)
3100 return (kern_lutimes(td, uap->path, UIO_USERSPACE, uap->tptr,
3105 kern_lutimes(struct thread *td, char *path, enum uio_seg pathseg,
3106 struct timeval *tptr, enum uio_seg tptrseg)
3108 struct timespec ts[2];
3109 struct nameidata nd;
3112 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3114 NDINIT(&nd, LOOKUP, NOFOLLOW | AUDITVNODE1, pathseg, path, td);
3115 if ((error = namei(&nd)) != 0)
3117 NDFREE(&nd, NDF_ONLY_PNBUF);
3118 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3124 * Set the access and modification times of a file.
3126 #ifndef _SYS_SYSPROTO_H_
3127 struct futimes_args {
3129 struct timeval *tptr;
3133 sys_futimes(struct thread *td, struct futimes_args *uap)
3136 return (kern_futimes(td, uap->fd, uap->tptr, UIO_USERSPACE));
3140 kern_futimes(struct thread *td, int fd, struct timeval *tptr,
3141 enum uio_seg tptrseg)
3143 struct timespec ts[2];
3145 cap_rights_t rights;
3149 error = getutimes(tptr, tptrseg, ts);
3152 error = getvnode(td, fd, cap_rights_init(&rights, CAP_FUTIMES), &fp);
3156 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
3157 AUDIT_ARG_VNODE1(fp->f_vnode);
3158 VOP_UNLOCK(fp->f_vnode, 0);
3160 error = setutimes(td, fp->f_vnode, ts, 2, tptr == NULL);
3166 sys_futimens(struct thread *td, struct futimens_args *uap)
3169 return (kern_futimens(td, uap->fd, uap->times, UIO_USERSPACE));
3173 kern_futimens(struct thread *td, int fd, struct timespec *tptr,
3174 enum uio_seg tptrseg)
3176 struct timespec ts[2];
3178 cap_rights_t rights;
3182 error = getutimens(tptr, tptrseg, ts, &flags);
3185 if (flags & UTIMENS_EXIT)
3187 error = getvnode(td, fd, cap_rights_init(&rights, CAP_FUTIMES), &fp);
3191 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
3192 AUDIT_ARG_VNODE1(fp->f_vnode);
3193 VOP_UNLOCK(fp->f_vnode, 0);
3195 error = setutimes(td, fp->f_vnode, ts, 2, flags & UTIMENS_NULL);
3201 sys_utimensat(struct thread *td, struct utimensat_args *uap)
3204 return (kern_utimensat(td, uap->fd, uap->path, UIO_USERSPACE,
3205 uap->times, UIO_USERSPACE, uap->flag));
3209 kern_utimensat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
3210 struct timespec *tptr, enum uio_seg tptrseg, int flag)
3212 struct nameidata nd;
3213 struct timespec ts[2];
3214 cap_rights_t rights;
3217 if (flag & ~AT_SYMLINK_NOFOLLOW)
3220 if ((error = getutimens(tptr, tptrseg, ts, &flags)) != 0)
3222 NDINIT_ATRIGHTS(&nd, LOOKUP, ((flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW :
3223 FOLLOW) | AUDITVNODE1, pathseg, path, fd,
3224 cap_rights_init(&rights, CAP_FUTIMES), td);
3225 if ((error = namei(&nd)) != 0)
3228 * We are allowed to call namei() regardless of 2xUTIME_OMIT.
3230 * "If both tv_nsec fields are UTIME_OMIT... EACCESS may be detected."
3231 * "Search permission is denied by a component of the path prefix."
3233 NDFREE(&nd, NDF_ONLY_PNBUF);
3234 if ((flags & UTIMENS_EXIT) == 0)
3235 error = setutimes(td, nd.ni_vp, ts, 2, flags & UTIMENS_NULL);
3241 * Truncate a file given its path name.
3243 #ifndef _SYS_SYSPROTO_H_
3244 struct truncate_args {
3251 sys_truncate(struct thread *td, struct truncate_args *uap)
3254 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3258 kern_truncate(struct thread *td, char *path, enum uio_seg pathseg, off_t length)
3264 struct nameidata nd;
3269 NDINIT(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, td);
3270 if ((error = namei(&nd)) != 0)
3273 rl_cookie = vn_rangelock_wlock(vp, 0, OFF_MAX);
3274 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0) {
3275 vn_rangelock_unlock(vp, rl_cookie);
3279 NDFREE(&nd, NDF_ONLY_PNBUF);
3280 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3281 if (vp->v_type == VDIR)
3284 else if ((error = mac_vnode_check_write(td->td_ucred, NOCRED, vp))) {
3287 else if ((error = vn_writechk(vp)) == 0 &&
3288 (error = VOP_ACCESS(vp, VWRITE, td->td_ucred, td)) == 0) {
3290 vattr.va_size = length;
3291 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3294 vn_finished_write(mp);
3295 vn_rangelock_unlock(vp, rl_cookie);
3300 #if defined(COMPAT_43)
3302 * Truncate a file given its path name.
3304 #ifndef _SYS_SYSPROTO_H_
3305 struct otruncate_args {
3311 otruncate(struct thread *td, struct otruncate_args *uap)
3314 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3316 #endif /* COMPAT_43 */
3318 #if defined(COMPAT_FREEBSD6)
3319 /* Versions with the pad argument */
3321 freebsd6_truncate(struct thread *td, struct freebsd6_truncate_args *uap)
3324 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3328 freebsd6_ftruncate(struct thread *td, struct freebsd6_ftruncate_args *uap)
3331 return (kern_ftruncate(td, uap->fd, uap->length));
3336 kern_fsync(struct thread *td, int fd, bool fullsync)
3341 cap_rights_t rights;
3342 int error, lock_flags;
3345 error = getvnode(td, fd, cap_rights_init(&rights, CAP_FSYNC), &fp);
3351 /* XXXKIB: compete outstanding aio writes */;
3353 error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
3356 if (MNT_SHARED_WRITES(mp) ||
3357 ((mp == NULL) && MNT_SHARED_WRITES(vp->v_mount))) {
3358 lock_flags = LK_SHARED;
3360 lock_flags = LK_EXCLUSIVE;
3362 vn_lock(vp, lock_flags | LK_RETRY);
3363 AUDIT_ARG_VNODE1(vp);
3364 if (vp->v_object != NULL) {
3365 VM_OBJECT_WLOCK(vp->v_object);
3366 vm_object_page_clean(vp->v_object, 0, 0, 0);
3367 VM_OBJECT_WUNLOCK(vp->v_object);
3369 error = fullsync ? VOP_FSYNC(vp, MNT_WAIT, td) : VOP_FDATASYNC(vp, td);
3371 vn_finished_write(mp);
3378 * Sync an open file.
3380 #ifndef _SYS_SYSPROTO_H_
3386 sys_fsync(struct thread *td, struct fsync_args *uap)
3389 return (kern_fsync(td, uap->fd, true));
3393 sys_fdatasync(struct thread *td, struct fdatasync_args *uap)
3396 return (kern_fsync(td, uap->fd, false));
3400 * Rename files. Source and destination must either both be directories, or
3401 * both not be directories. If target is a directory, it must be empty.
3403 #ifndef _SYS_SYSPROTO_H_
3404 struct rename_args {
3410 sys_rename(struct thread *td, struct rename_args *uap)
3413 return (kern_renameat(td, AT_FDCWD, uap->from, AT_FDCWD,
3414 uap->to, UIO_USERSPACE));
3417 #ifndef _SYS_SYSPROTO_H_
3418 struct renameat_args {
3426 sys_renameat(struct thread *td, struct renameat_args *uap)
3429 return (kern_renameat(td, uap->oldfd, uap->old, uap->newfd, uap->new,
3434 kern_renameat(struct thread *td, int oldfd, char *old, int newfd, char *new,
3435 enum uio_seg pathseg)
3437 struct mount *mp = NULL;
3438 struct vnode *tvp, *fvp, *tdvp;
3439 struct nameidata fromnd, tond;
3440 cap_rights_t rights;
3446 NDINIT_ATRIGHTS(&fromnd, DELETE, LOCKPARENT | LOCKLEAF | SAVESTART |
3447 AUDITVNODE1, pathseg, old, oldfd,
3448 cap_rights_init(&rights, CAP_RENAMEAT_SOURCE), td);
3450 NDINIT_ATRIGHTS(&fromnd, DELETE, WANTPARENT | SAVESTART | AUDITVNODE1,
3451 pathseg, old, oldfd,
3452 cap_rights_init(&rights, CAP_RENAMEAT_SOURCE), td);
3455 if ((error = namei(&fromnd)) != 0)
3458 error = mac_vnode_check_rename_from(td->td_ucred, fromnd.ni_dvp,
3459 fromnd.ni_vp, &fromnd.ni_cnd);
3460 VOP_UNLOCK(fromnd.ni_dvp, 0);
3461 if (fromnd.ni_dvp != fromnd.ni_vp)
3462 VOP_UNLOCK(fromnd.ni_vp, 0);
3465 NDINIT_ATRIGHTS(&tond, RENAME, LOCKPARENT | LOCKLEAF | NOCACHE |
3466 SAVESTART | AUDITVNODE2, pathseg, new, newfd,
3467 cap_rights_init(&rights, CAP_RENAMEAT_TARGET), td);
3468 if (fromnd.ni_vp->v_type == VDIR)
3469 tond.ni_cnd.cn_flags |= WILLBEDIR;
3470 if ((error = namei(&tond)) != 0) {
3471 /* Translate error code for rename("dir1", "dir2/."). */
3472 if (error == EISDIR && fvp->v_type == VDIR)
3474 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3475 vrele(fromnd.ni_dvp);
3481 error = vn_start_write(fvp, &mp, V_NOWAIT);
3483 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3484 NDFREE(&tond, NDF_ONLY_PNBUF);
3491 vrele(fromnd.ni_dvp);
3493 vrele(tond.ni_startdir);
3494 if (fromnd.ni_startdir != NULL)
3495 vrele(fromnd.ni_startdir);
3496 error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH);
3502 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
3505 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
3510 if (newfd != AT_FDCWD) {
3512 * If the target already exists we require CAP_UNLINKAT
3515 error = cap_check(&tond.ni_filecaps.fc_rights,
3516 cap_rights_init(&rights, CAP_UNLINKAT));
3527 * If the source is the same as the destination (that is, if they
3528 * are links to the same vnode), then there is nothing to do.
3534 error = mac_vnode_check_rename_to(td->td_ucred, tdvp,
3535 tond.ni_vp, fromnd.ni_dvp == tdvp, &tond.ni_cnd);
3539 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
3540 tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
3541 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3542 NDFREE(&tond, NDF_ONLY_PNBUF);
3544 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3545 NDFREE(&tond, NDF_ONLY_PNBUF);
3552 vrele(fromnd.ni_dvp);
3555 vrele(tond.ni_startdir);
3556 vn_finished_write(mp);
3558 if (fromnd.ni_startdir)
3559 vrele(fromnd.ni_startdir);
3566 * Make a directory file.
3568 #ifndef _SYS_SYSPROTO_H_
3575 sys_mkdir(struct thread *td, struct mkdir_args *uap)
3578 return (kern_mkdirat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
3582 #ifndef _SYS_SYSPROTO_H_
3583 struct mkdirat_args {
3590 sys_mkdirat(struct thread *td, struct mkdirat_args *uap)
3593 return (kern_mkdirat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode));
3597 kern_mkdirat(struct thread *td, int fd, char *path, enum uio_seg segflg,
3603 struct nameidata nd;
3604 cap_rights_t rights;
3607 AUDIT_ARG_MODE(mode);
3610 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
3611 NOCACHE, segflg, path, fd, cap_rights_init(&rights, CAP_MKDIRAT),
3613 nd.ni_cnd.cn_flags |= WILLBEDIR;
3614 if ((error = namei(&nd)) != 0)
3618 NDFREE(&nd, NDF_ONLY_PNBUF);
3620 * XXX namei called with LOCKPARENT but not LOCKLEAF has
3621 * the strange behaviour of leaving the vnode unlocked
3622 * if the target is the same vnode as the parent.
3624 if (vp == nd.ni_dvp)
3631 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3632 NDFREE(&nd, NDF_ONLY_PNBUF);
3634 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3639 vattr.va_type = VDIR;
3640 vattr.va_mode = (mode & ACCESSPERMS) &~ td->td_proc->p_fd->fd_cmask;
3642 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
3647 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
3651 NDFREE(&nd, NDF_ONLY_PNBUF);
3655 vn_finished_write(mp);
3660 * Remove a directory file.
3662 #ifndef _SYS_SYSPROTO_H_
3668 sys_rmdir(struct thread *td, struct rmdir_args *uap)
3671 return (kern_rmdirat(td, AT_FDCWD, uap->path, UIO_USERSPACE));
3675 kern_rmdirat(struct thread *td, int fd, char *path, enum uio_seg pathseg)
3679 struct nameidata nd;
3680 cap_rights_t rights;
3685 NDINIT_ATRIGHTS(&nd, DELETE, LOCKPARENT | LOCKLEAF | AUDITVNODE1,
3686 pathseg, path, fd, cap_rights_init(&rights, CAP_UNLINKAT), td);
3687 if ((error = namei(&nd)) != 0)
3690 if (vp->v_type != VDIR) {
3695 * No rmdir "." please.
3697 if (nd.ni_dvp == vp) {
3702 * The root of a mounted filesystem cannot be deleted.
3704 if (vp->v_vflag & VV_ROOT) {
3709 error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp,
3714 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3715 NDFREE(&nd, NDF_ONLY_PNBUF);
3717 if (nd.ni_dvp == vp)
3721 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3725 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_UNLINK);
3726 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
3727 vn_finished_write(mp);
3729 NDFREE(&nd, NDF_ONLY_PNBUF);
3731 if (nd.ni_dvp == vp)
3738 #if defined(COMPAT_43) || defined(COMPAT_FREEBSD11)
3740 freebsd11_kern_getdirentries(struct thread *td, int fd, char *ubuf, u_int count,
3741 long *basep, void (*func)(struct freebsd11_dirent *))
3743 struct freebsd11_dirent dstdp;
3744 struct dirent *dp, *edp;
3747 ssize_t resid, ucount;
3750 /* XXX arbitrary sanity limit on `count'. */
3751 count = min(count, 64 * 1024);
3753 dirbuf = malloc(count, M_TEMP, M_WAITOK);
3755 error = kern_getdirentries(td, fd, dirbuf, count, &base, &resid,
3763 for (dp = (struct dirent *)dirbuf,
3764 edp = (struct dirent *)&dirbuf[count - resid];
3765 ucount < count && dp < edp; ) {
3766 if (dp->d_reclen == 0)
3768 MPASS(dp->d_reclen >= _GENERIC_DIRLEN(0));
3769 if (dp->d_namlen >= sizeof(dstdp.d_name))
3771 dstdp.d_type = dp->d_type;
3772 dstdp.d_namlen = dp->d_namlen;
3773 dstdp.d_fileno = dp->d_fileno; /* truncate */
3774 if (dstdp.d_fileno != dp->d_fileno) {
3775 switch (ino64_trunc_error) {
3783 dstdp.d_fileno = UINT32_MAX;
3787 dstdp.d_reclen = sizeof(dstdp) - sizeof(dstdp.d_name) +
3788 ((dp->d_namlen + 1 + 3) &~ 3);
3789 bcopy(dp->d_name, dstdp.d_name, dstdp.d_namlen);
3790 bzero(dstdp.d_name + dstdp.d_namlen,
3791 dstdp.d_reclen - offsetof(struct freebsd11_dirent, d_name) -
3793 MPASS(dstdp.d_reclen <= dp->d_reclen);
3794 MPASS(ucount + dstdp.d_reclen <= count);
3797 error = copyout(&dstdp, ubuf + ucount, dstdp.d_reclen);
3800 dp = (struct dirent *)((char *)dp + dp->d_reclen);
3801 ucount += dstdp.d_reclen;
3805 free(dirbuf, M_TEMP);
3807 td->td_retval[0] = ucount;
3814 ogetdirentries_cvt(struct freebsd11_dirent *dp)
3816 #if (BYTE_ORDER == LITTLE_ENDIAN)
3818 * The expected low byte of dp->d_namlen is our dp->d_type.
3819 * The high MBZ byte of dp->d_namlen is our dp->d_namlen.
3821 dp->d_type = dp->d_namlen;
3825 * The dp->d_type is the high byte of the expected dp->d_namlen,
3826 * so must be zero'ed.
3833 * Read a block of directory entries in a filesystem independent format.
3835 #ifndef _SYS_SYSPROTO_H_
3836 struct ogetdirentries_args {
3844 ogetdirentries(struct thread *td, struct ogetdirentries_args *uap)
3849 error = kern_ogetdirentries(td, uap, &loff);
3851 error = copyout(&loff, uap->basep, sizeof(long));
3856 kern_ogetdirentries(struct thread *td, struct ogetdirentries_args *uap,
3862 /* XXX arbitrary sanity limit on `count'. */
3863 if (uap->count > 64 * 1024)
3866 error = freebsd11_kern_getdirentries(td, uap->fd, uap->buf, uap->count,
3867 &base, ogetdirentries_cvt);
3869 if (error == 0 && uap->basep != NULL)
3870 error = copyout(&base, uap->basep, sizeof(long));
3874 #endif /* COMPAT_43 */
3876 #if defined(COMPAT_FREEBSD11)
3877 #ifndef _SYS_SYSPROTO_H_
3878 struct freebsd11_getdirentries_args {
3886 freebsd11_getdirentries(struct thread *td,
3887 struct freebsd11_getdirentries_args *uap)
3892 error = freebsd11_kern_getdirentries(td, uap->fd, uap->buf, uap->count,
3895 if (error == 0 && uap->basep != NULL)
3896 error = copyout(&base, uap->basep, sizeof(long));
3901 freebsd11_getdents(struct thread *td, struct freebsd11_getdents_args *uap)
3903 struct freebsd11_getdirentries_args ap;
3907 ap.count = uap->count;
3909 return (freebsd11_getdirentries(td, &ap));
3911 #endif /* COMPAT_FREEBSD11 */
3914 * Read a block of directory entries in a filesystem independent format.
3917 sys_getdirentries(struct thread *td, struct getdirentries_args *uap)
3922 error = kern_getdirentries(td, uap->fd, uap->buf, uap->count, &base,
3923 NULL, UIO_USERSPACE);
3926 if (uap->basep != NULL)
3927 error = copyout(&base, uap->basep, sizeof(off_t));
3932 kern_getdirentries(struct thread *td, int fd, char *buf, size_t count,
3933 off_t *basep, ssize_t *residp, enum uio_seg bufseg)
3939 cap_rights_t rights;
3945 if (count > IOSIZE_MAX)
3947 auio.uio_resid = count;
3948 error = getvnode(td, fd, cap_rights_init(&rights, CAP_READ), &fp);
3951 if ((fp->f_flag & FREAD) == 0) {
3956 foffset = foffset_lock(fp, 0);
3958 if (vp->v_type != VDIR) {
3962 aiov.iov_base = buf;
3963 aiov.iov_len = count;
3964 auio.uio_iov = &aiov;
3965 auio.uio_iovcnt = 1;
3966 auio.uio_rw = UIO_READ;
3967 auio.uio_segflg = bufseg;
3969 vn_lock(vp, LK_SHARED | LK_RETRY);
3970 AUDIT_ARG_VNODE1(vp);
3971 loff = auio.uio_offset = foffset;
3973 error = mac_vnode_check_readdir(td->td_ucred, vp);
3976 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL,
3978 foffset = auio.uio_offset;
3983 if (count == auio.uio_resid &&
3984 (vp->v_vflag & VV_ROOT) &&
3985 (vp->v_mount->mnt_flag & MNT_UNION)) {
3986 struct vnode *tvp = vp;
3988 vp = vp->v_mount->mnt_vnodecovered;
3999 *residp = auio.uio_resid;
4000 td->td_retval[0] = count - auio.uio_resid;
4002 foffset_unlock(fp, foffset, 0);
4008 * Set the mode mask for creation of filesystem nodes.
4010 #ifndef _SYS_SYSPROTO_H_
4016 sys_umask(struct thread *td, struct umask_args *uap)
4018 struct filedesc *fdp;
4020 fdp = td->td_proc->p_fd;
4021 FILEDESC_XLOCK(fdp);
4022 td->td_retval[0] = fdp->fd_cmask;
4023 fdp->fd_cmask = uap->newmask & ALLPERMS;
4024 FILEDESC_XUNLOCK(fdp);
4029 * Void all references to file by ripping underlying filesystem away from
4032 #ifndef _SYS_SYSPROTO_H_
4033 struct revoke_args {
4038 sys_revoke(struct thread *td, struct revoke_args *uap)
4042 struct nameidata nd;
4045 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
4047 if ((error = namei(&nd)) != 0)
4050 NDFREE(&nd, NDF_ONLY_PNBUF);
4051 if (vp->v_type != VCHR || vp->v_rdev == NULL) {
4056 error = mac_vnode_check_revoke(td->td_ucred, vp);
4060 error = VOP_GETATTR(vp, &vattr, td->td_ucred);
4063 if (td->td_ucred->cr_uid != vattr.va_uid) {
4064 error = priv_check(td, PRIV_VFS_ADMIN);
4069 VOP_REVOKE(vp, REVOKEALL);
4076 * Convert a user file descriptor to a kernel file entry and check that, if it
4077 * is a capability, the correct rights are present. A reference on the file
4078 * entry is held upon returning.
4081 getvnode(struct thread *td, int fd, cap_rights_t *rightsp, struct file **fpp)
4086 error = fget_unlocked(td->td_proc->p_fd, fd, rightsp, &fp, NULL);
4091 * The file could be not of the vnode type, or it may be not
4092 * yet fully initialized, in which case the f_vnode pointer
4093 * may be set, but f_ops is still badfileops. E.g.,
4094 * devfs_open() transiently create such situation to
4095 * facilitate csw d_fdopen().
4097 * Dupfdopen() handling in kern_openat() installs the
4098 * half-baked file into the process descriptor table, allowing
4099 * other thread to dereference it. Guard against the race by
4102 if (fp->f_vnode == NULL || fp->f_ops == &badfileops) {
4112 * Get an (NFS) file handle.
4114 #ifndef _SYS_SYSPROTO_H_
4115 struct lgetfh_args {
4121 sys_lgetfh(struct thread *td, struct lgetfh_args *uap)
4123 struct nameidata nd;
4128 error = priv_check(td, PRIV_VFS_GETFH);
4131 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
4136 NDFREE(&nd, NDF_ONLY_PNBUF);
4138 bzero(&fh, sizeof(fh));
4139 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
4140 error = VOP_VPTOFH(vp, &fh.fh_fid);
4143 error = copyout(&fh, uap->fhp, sizeof (fh));
4147 #ifndef _SYS_SYSPROTO_H_
4154 sys_getfh(struct thread *td, struct getfh_args *uap)
4156 struct nameidata nd;
4161 error = priv_check(td, PRIV_VFS_GETFH);
4164 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
4169 NDFREE(&nd, NDF_ONLY_PNBUF);
4171 bzero(&fh, sizeof(fh));
4172 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
4173 error = VOP_VPTOFH(vp, &fh.fh_fid);
4176 error = copyout(&fh, uap->fhp, sizeof (fh));
4181 * syscall for the rpc.lockd to use to translate a NFS file handle into an
4184 * warning: do not remove the priv_check() call or this becomes one giant
4187 #ifndef _SYS_SYSPROTO_H_
4188 struct fhopen_args {
4189 const struct fhandle *u_fhp;
4194 sys_fhopen(struct thread *td, struct fhopen_args *uap)
4203 error = priv_check(td, PRIV_VFS_FHOPEN);
4207 fmode = FFLAGS(uap->flags);
4208 /* why not allow a non-read/write open for our lockd? */
4209 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
4211 error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
4214 /* find the mount point */
4215 mp = vfs_busyfs(&fhp.fh_fsid);
4218 /* now give me my vnode, it gets returned to me locked */
4219 error = VFS_FHTOVP(mp, &fhp.fh_fid, LK_EXCLUSIVE, &vp);
4224 error = falloc_noinstall(td, &fp);
4230 * An extra reference on `fp' has been held for us by
4231 * falloc_noinstall().
4237 error = vn_open_vnode(vp, fmode, td->td_ucred, td, fp);
4239 KASSERT(fp->f_ops == &badfileops,
4240 ("VOP_OPEN in fhopen() set f_ops"));
4241 KASSERT(td->td_dupfd < 0,
4242 ("fhopen() encountered fdopen()"));
4252 finit(fp, (fmode & FMASK) | (fp->f_flag & FHASLOCK), DTYPE_VNODE, vp,
4255 if ((fmode & O_TRUNC) != 0) {
4256 error = fo_truncate(fp, 0, td->td_ucred, td);
4261 error = finstall(td, fp, &indx, fmode, NULL);
4264 td->td_retval[0] = indx;
4269 * Stat an (NFS) file handle.
4271 #ifndef _SYS_SYSPROTO_H_
4272 struct fhstat_args {
4273 struct fhandle *u_fhp;
4278 sys_fhstat(struct thread *td, struct fhstat_args *uap)
4284 error = copyin(uap->u_fhp, &fh, sizeof(fh));
4287 error = kern_fhstat(td, fh, &sb);
4289 error = copyout(&sb, uap->sb, sizeof(sb));
4294 kern_fhstat(struct thread *td, struct fhandle fh, struct stat *sb)
4300 error = priv_check(td, PRIV_VFS_FHSTAT);
4303 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4305 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_EXCLUSIVE, &vp);
4309 error = vn_stat(vp, sb, td->td_ucred, NOCRED, td);
4315 * Implement fstatfs() for (NFS) file handles.
4317 #ifndef _SYS_SYSPROTO_H_
4318 struct fhstatfs_args {
4319 struct fhandle *u_fhp;
4324 sys_fhstatfs(struct thread *td, struct fhstatfs_args *uap)
4330 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4333 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
4334 error = kern_fhstatfs(td, fh, sfp);
4336 error = copyout(sfp, uap->buf, sizeof(*sfp));
4337 free(sfp, M_STATFS);
4342 kern_fhstatfs(struct thread *td, fhandle_t fh, struct statfs *buf)
4349 error = priv_check(td, PRIV_VFS_FHSTATFS);
4352 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4354 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_EXCLUSIVE, &vp);
4360 error = prison_canseemount(td->td_ucred, mp);
4364 error = mac_mount_check_stat(td->td_ucred, mp);
4369 * Set these in case the underlying filesystem fails to do so.
4372 sp->f_version = STATFS_VERSION;
4373 sp->f_namemax = NAME_MAX;
4374 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
4375 error = VFS_STATFS(mp, sp);
4384 kern_posix_fallocate(struct thread *td, int fd, off_t offset, off_t len)
4389 cap_rights_t rights;
4390 off_t olen, ooffset;
4393 int audited_vnode1 = 0;
4397 if (offset < 0 || len <= 0)
4399 /* Check for wrap. */
4400 if (offset > OFF_MAX - len)
4403 error = fget(td, fd, cap_rights_init(&rights, CAP_WRITE), &fp);
4406 AUDIT_ARG_FILE(td->td_proc, fp);
4407 if ((fp->f_ops->fo_flags & DFLAG_SEEKABLE) == 0) {
4411 if ((fp->f_flag & FWRITE) == 0) {
4415 if (fp->f_type != DTYPE_VNODE) {
4420 if (vp->v_type != VREG) {
4425 /* Allocating blocks may take a long time, so iterate. */
4432 error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
4435 error = vn_lock(vp, LK_EXCLUSIVE);
4437 vn_finished_write(mp);
4441 if (!audited_vnode1) {
4442 AUDIT_ARG_VNODE1(vp);
4447 error = mac_vnode_check_write(td->td_ucred, fp->f_cred, vp);
4450 error = VOP_ALLOCATE(vp, &offset, &len);
4452 vn_finished_write(mp);
4454 if (olen + ooffset != offset + len) {
4455 panic("offset + len changed from %jx/%jx to %jx/%jx",
4456 ooffset, olen, offset, len);
4458 if (error != 0 || len == 0)
4460 KASSERT(olen > len, ("Iteration did not make progress?"));
4469 sys_posix_fallocate(struct thread *td, struct posix_fallocate_args *uap)
4473 error = kern_posix_fallocate(td, uap->fd, uap->offset, uap->len);
4474 return (kern_posix_error(td, error));
4478 * Unlike madvise(2), we do not make a best effort to remember every
4479 * possible caching hint. Instead, we remember the last setting with
4480 * the exception that we will allow POSIX_FADV_NORMAL to adjust the
4481 * region of any current setting.
4484 kern_posix_fadvise(struct thread *td, int fd, off_t offset, off_t len,
4487 struct fadvise_info *fa, *new;
4490 cap_rights_t rights;
4494 if (offset < 0 || len < 0 || offset > OFF_MAX - len)
4496 AUDIT_ARG_VALUE(advice);
4498 case POSIX_FADV_SEQUENTIAL:
4499 case POSIX_FADV_RANDOM:
4500 case POSIX_FADV_NOREUSE:
4501 new = malloc(sizeof(*fa), M_FADVISE, M_WAITOK);
4503 case POSIX_FADV_NORMAL:
4504 case POSIX_FADV_WILLNEED:
4505 case POSIX_FADV_DONTNEED:
4511 /* XXX: CAP_POSIX_FADVISE? */
4513 error = fget(td, fd, cap_rights_init(&rights), &fp);
4516 AUDIT_ARG_FILE(td->td_proc, fp);
4517 if ((fp->f_ops->fo_flags & DFLAG_SEEKABLE) == 0) {
4521 if (fp->f_type != DTYPE_VNODE) {
4526 if (vp->v_type != VREG) {
4533 end = offset + len - 1;
4535 case POSIX_FADV_SEQUENTIAL:
4536 case POSIX_FADV_RANDOM:
4537 case POSIX_FADV_NOREUSE:
4539 * Try to merge any existing non-standard region with
4540 * this new region if possible, otherwise create a new
4541 * non-standard region for this request.
4543 mtx_pool_lock(mtxpool_sleep, fp);
4545 if (fa != NULL && fa->fa_advice == advice &&
4546 ((fa->fa_start <= end && fa->fa_end >= offset) ||
4547 (end != OFF_MAX && fa->fa_start == end + 1) ||
4548 (fa->fa_end != OFF_MAX && fa->fa_end + 1 == offset))) {
4549 if (offset < fa->fa_start)
4550 fa->fa_start = offset;
4551 if (end > fa->fa_end)
4554 new->fa_advice = advice;
4555 new->fa_start = offset;
4560 mtx_pool_unlock(mtxpool_sleep, fp);
4562 case POSIX_FADV_NORMAL:
4564 * If a the "normal" region overlaps with an existing
4565 * non-standard region, trim or remove the
4566 * non-standard region.
4568 mtx_pool_lock(mtxpool_sleep, fp);
4571 if (offset <= fa->fa_start && end >= fa->fa_end) {
4573 fp->f_advice = NULL;
4574 } else if (offset <= fa->fa_start &&
4575 end >= fa->fa_start)
4576 fa->fa_start = end + 1;
4577 else if (offset <= fa->fa_end && end >= fa->fa_end)
4578 fa->fa_end = offset - 1;
4579 else if (offset >= fa->fa_start && end <= fa->fa_end) {
4581 * If the "normal" region is a middle
4582 * portion of the existing
4583 * non-standard region, just remove
4584 * the whole thing rather than picking
4585 * one side or the other to
4589 fp->f_advice = NULL;
4592 mtx_pool_unlock(mtxpool_sleep, fp);
4594 case POSIX_FADV_WILLNEED:
4595 case POSIX_FADV_DONTNEED:
4596 error = VOP_ADVISE(vp, offset, end, advice);
4602 free(new, M_FADVISE);
4607 sys_posix_fadvise(struct thread *td, struct posix_fadvise_args *uap)
4611 error = kern_posix_fadvise(td, uap->fd, uap->offset, uap->len,
4613 return (kern_posix_error(td, error));