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_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;
2262 cap_rights_t rights;
2265 if (flag & ~AT_SYMLINK_NOFOLLOW)
2268 NDINIT_ATRIGHTS(&nd, LOOKUP, ((flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW :
2269 FOLLOW) | LOCKSHARED | LOCKLEAF | AUDITVNODE1, pathseg, path, fd,
2270 cap_rights_init(&rights, CAP_FSTAT), td);
2272 if ((error = namei(&nd)) != 0)
2274 error = vn_stat(nd.ni_vp, &sb, td->td_ucred, NOCRED, td);
2276 SDT_PROBE2(vfs, , stat, mode, path, sb.st_mode);
2277 if (S_ISREG(sb.st_mode))
2278 SDT_PROBE2(vfs, , stat, reg, path, pathseg);
2279 if (__predict_false(hook != NULL))
2280 hook(nd.ni_vp, &sb);
2282 NDFREE(&nd, NDF_ONLY_PNBUF);
2286 #ifdef __STAT_TIME_T_EXT
2294 if (KTRPOINT(td, KTR_STRUCT))
2300 #if defined(COMPAT_FREEBSD11)
2302 * Implementation of the NetBSD [l]stat() functions.
2305 freebsd11_cvtnstat(struct stat *sb, struct nstat *nsb)
2308 bzero(nsb, sizeof(*nsb));
2309 nsb->st_dev = sb->st_dev;
2310 nsb->st_ino = sb->st_ino;
2311 nsb->st_mode = sb->st_mode;
2312 nsb->st_nlink = sb->st_nlink;
2313 nsb->st_uid = sb->st_uid;
2314 nsb->st_gid = sb->st_gid;
2315 nsb->st_rdev = sb->st_rdev;
2316 nsb->st_atim = sb->st_atim;
2317 nsb->st_mtim = sb->st_mtim;
2318 nsb->st_ctim = sb->st_ctim;
2319 nsb->st_size = sb->st_size;
2320 nsb->st_blocks = sb->st_blocks;
2321 nsb->st_blksize = sb->st_blksize;
2322 nsb->st_flags = sb->st_flags;
2323 nsb->st_gen = sb->st_gen;
2324 nsb->st_birthtim = sb->st_birthtim;
2327 #ifndef _SYS_SYSPROTO_H_
2328 struct freebsd11_nstat_args {
2334 freebsd11_nstat(struct thread *td, struct freebsd11_nstat_args *uap)
2340 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
2344 freebsd11_cvtnstat(&sb, &nsb);
2345 return (copyout(&nsb, uap->ub, sizeof (nsb)));
2349 * NetBSD lstat. Get file status; this version does not follow links.
2351 #ifndef _SYS_SYSPROTO_H_
2352 struct freebsd11_nlstat_args {
2358 freebsd11_nlstat(struct thread *td, struct freebsd11_nlstat_args *uap)
2364 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2365 UIO_USERSPACE, &sb, NULL);
2368 freebsd11_cvtnstat(&sb, &nsb);
2369 return (copyout(&nsb, uap->ub, sizeof (nsb)));
2371 #endif /* COMPAT_FREEBSD11 */
2374 * Get configurable pathname variables.
2376 #ifndef _SYS_SYSPROTO_H_
2377 struct pathconf_args {
2383 sys_pathconf(struct thread *td, struct pathconf_args *uap)
2386 return (kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name, FOLLOW));
2389 #ifndef _SYS_SYSPROTO_H_
2390 struct lpathconf_args {
2396 sys_lpathconf(struct thread *td, struct lpathconf_args *uap)
2399 return (kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name,
2404 kern_pathconf(struct thread *td, char *path, enum uio_seg pathseg, int name,
2407 struct nameidata nd;
2410 NDINIT(&nd, LOOKUP, LOCKSHARED | LOCKLEAF | AUDITVNODE1 | flags,
2412 if ((error = namei(&nd)) != 0)
2414 NDFREE(&nd, NDF_ONLY_PNBUF);
2416 error = VOP_PATHCONF(nd.ni_vp, name, td->td_retval);
2422 * Return target name of a symbolic link.
2424 #ifndef _SYS_SYSPROTO_H_
2425 struct readlink_args {
2432 sys_readlink(struct thread *td, struct readlink_args *uap)
2435 return (kern_readlinkat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2436 uap->buf, UIO_USERSPACE, uap->count));
2438 #ifndef _SYS_SYSPROTO_H_
2439 struct readlinkat_args {
2447 sys_readlinkat(struct thread *td, struct readlinkat_args *uap)
2450 return (kern_readlinkat(td, uap->fd, uap->path, UIO_USERSPACE,
2451 uap->buf, UIO_USERSPACE, uap->bufsize));
2455 kern_readlinkat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2456 char *buf, enum uio_seg bufseg, size_t count)
2461 struct nameidata nd;
2464 if (count > IOSIZE_MAX)
2467 NDINIT_AT(&nd, LOOKUP, NOFOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
2468 pathseg, path, fd, td);
2470 if ((error = namei(&nd)) != 0)
2472 NDFREE(&nd, NDF_ONLY_PNBUF);
2475 error = mac_vnode_check_readlink(td->td_ucred, vp);
2481 if (vp->v_type != VLNK && (vp->v_vflag & VV_READLINK) == 0)
2484 aiov.iov_base = buf;
2485 aiov.iov_len = count;
2486 auio.uio_iov = &aiov;
2487 auio.uio_iovcnt = 1;
2488 auio.uio_offset = 0;
2489 auio.uio_rw = UIO_READ;
2490 auio.uio_segflg = bufseg;
2492 auio.uio_resid = count;
2493 error = VOP_READLINK(vp, &auio, td->td_ucred);
2494 td->td_retval[0] = count - auio.uio_resid;
2501 * Common implementation code for chflags() and fchflags().
2504 setfflags(struct thread *td, struct vnode *vp, u_long flags)
2510 /* We can't support the value matching VNOVAL. */
2511 if (flags == VNOVAL)
2512 return (EOPNOTSUPP);
2515 * Prevent non-root users from setting flags on devices. When
2516 * a device is reused, users can retain ownership of the device
2517 * if they are allowed to set flags and programs assume that
2518 * chown can't fail when done as root.
2520 if (vp->v_type == VCHR || vp->v_type == VBLK) {
2521 error = priv_check(td, PRIV_VFS_CHFLAGS_DEV);
2526 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2529 vattr.va_flags = flags;
2530 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2532 error = mac_vnode_check_setflags(td->td_ucred, vp, vattr.va_flags);
2535 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
2537 vn_finished_write(mp);
2542 * Change flags of a file given a path name.
2544 #ifndef _SYS_SYSPROTO_H_
2545 struct chflags_args {
2551 sys_chflags(struct thread *td, struct chflags_args *uap)
2554 return (kern_chflagsat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2558 #ifndef _SYS_SYSPROTO_H_
2559 struct chflagsat_args {
2567 sys_chflagsat(struct thread *td, struct chflagsat_args *uap)
2570 const char *path = uap->path;
2571 u_long flags = uap->flags;
2572 int atflag = uap->atflag;
2574 if (atflag & ~AT_SYMLINK_NOFOLLOW)
2577 return (kern_chflagsat(td, fd, path, UIO_USERSPACE, flags, atflag));
2581 * Same as chflags() but doesn't follow symlinks.
2583 #ifndef _SYS_SYSPROTO_H_
2584 struct lchflags_args {
2590 sys_lchflags(struct thread *td, struct lchflags_args *uap)
2593 return (kern_chflagsat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2594 uap->flags, AT_SYMLINK_NOFOLLOW));
2598 kern_chflagsat(struct thread *td, int fd, const char *path,
2599 enum uio_seg pathseg, u_long flags, int atflag)
2601 struct nameidata nd;
2602 cap_rights_t rights;
2605 AUDIT_ARG_FFLAGS(flags);
2606 follow = (atflag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
2607 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, pathseg, path, fd,
2608 cap_rights_init(&rights, CAP_FCHFLAGS), td);
2609 if ((error = namei(&nd)) != 0)
2611 NDFREE(&nd, NDF_ONLY_PNBUF);
2612 error = setfflags(td, nd.ni_vp, flags);
2618 * Change flags of a file given a file descriptor.
2620 #ifndef _SYS_SYSPROTO_H_
2621 struct fchflags_args {
2627 sys_fchflags(struct thread *td, struct fchflags_args *uap)
2630 cap_rights_t rights;
2633 AUDIT_ARG_FD(uap->fd);
2634 AUDIT_ARG_FFLAGS(uap->flags);
2635 error = getvnode(td, uap->fd, cap_rights_init(&rights, CAP_FCHFLAGS),
2640 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
2641 AUDIT_ARG_VNODE1(fp->f_vnode);
2642 VOP_UNLOCK(fp->f_vnode, 0);
2644 error = setfflags(td, fp->f_vnode, uap->flags);
2650 * Common implementation code for chmod(), lchmod() and fchmod().
2653 setfmode(struct thread *td, struct ucred *cred, struct vnode *vp, int mode)
2659 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2661 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2663 vattr.va_mode = mode & ALLPERMS;
2665 error = mac_vnode_check_setmode(cred, vp, vattr.va_mode);
2668 error = VOP_SETATTR(vp, &vattr, cred);
2670 vn_finished_write(mp);
2675 * Change mode of a file given path name.
2677 #ifndef _SYS_SYSPROTO_H_
2684 sys_chmod(struct thread *td, struct chmod_args *uap)
2687 return (kern_fchmodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2691 #ifndef _SYS_SYSPROTO_H_
2692 struct fchmodat_args {
2700 sys_fchmodat(struct thread *td, struct fchmodat_args *uap)
2702 int flag = uap->flag;
2704 char *path = uap->path;
2705 mode_t mode = uap->mode;
2707 if (flag & ~AT_SYMLINK_NOFOLLOW)
2710 return (kern_fchmodat(td, fd, path, UIO_USERSPACE, mode, flag));
2714 * Change mode of a file given path name (don't follow links.)
2716 #ifndef _SYS_SYSPROTO_H_
2717 struct lchmod_args {
2723 sys_lchmod(struct thread *td, struct lchmod_args *uap)
2726 return (kern_fchmodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2727 uap->mode, AT_SYMLINK_NOFOLLOW));
2731 kern_fchmodat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2732 mode_t mode, int flag)
2734 struct nameidata nd;
2735 cap_rights_t rights;
2738 AUDIT_ARG_MODE(mode);
2739 follow = (flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
2740 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, pathseg, path, fd,
2741 cap_rights_init(&rights, CAP_FCHMOD), td);
2742 if ((error = namei(&nd)) != 0)
2744 NDFREE(&nd, NDF_ONLY_PNBUF);
2745 error = setfmode(td, td->td_ucred, nd.ni_vp, mode);
2751 * Change mode of a file given a file descriptor.
2753 #ifndef _SYS_SYSPROTO_H_
2754 struct fchmod_args {
2760 sys_fchmod(struct thread *td, struct fchmod_args *uap)
2763 cap_rights_t rights;
2766 AUDIT_ARG_FD(uap->fd);
2767 AUDIT_ARG_MODE(uap->mode);
2769 error = fget(td, uap->fd, cap_rights_init(&rights, CAP_FCHMOD), &fp);
2772 error = fo_chmod(fp, uap->mode, td->td_ucred, td);
2778 * Common implementation for chown(), lchown(), and fchown()
2781 setfown(struct thread *td, struct ucred *cred, struct vnode *vp, uid_t uid,
2788 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2790 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2795 error = mac_vnode_check_setowner(cred, vp, vattr.va_uid,
2799 error = VOP_SETATTR(vp, &vattr, cred);
2801 vn_finished_write(mp);
2806 * Set ownership given a path name.
2808 #ifndef _SYS_SYSPROTO_H_
2816 sys_chown(struct thread *td, struct chown_args *uap)
2819 return (kern_fchownat(td, AT_FDCWD, uap->path, UIO_USERSPACE, uap->uid,
2823 #ifndef _SYS_SYSPROTO_H_
2824 struct fchownat_args {
2833 sys_fchownat(struct thread *td, struct fchownat_args *uap)
2838 if (flag & ~AT_SYMLINK_NOFOLLOW)
2841 return (kern_fchownat(td, uap->fd, uap->path, UIO_USERSPACE, uap->uid,
2842 uap->gid, uap->flag));
2846 kern_fchownat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2847 int uid, int gid, int flag)
2849 struct nameidata nd;
2850 cap_rights_t rights;
2853 AUDIT_ARG_OWNER(uid, gid);
2854 follow = (flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
2855 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, pathseg, path, fd,
2856 cap_rights_init(&rights, CAP_FCHOWN), td);
2858 if ((error = namei(&nd)) != 0)
2860 NDFREE(&nd, NDF_ONLY_PNBUF);
2861 error = setfown(td, td->td_ucred, nd.ni_vp, uid, gid);
2867 * Set ownership given a path name, do not cross symlinks.
2869 #ifndef _SYS_SYSPROTO_H_
2870 struct lchown_args {
2877 sys_lchown(struct thread *td, struct lchown_args *uap)
2880 return (kern_fchownat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2881 uap->uid, uap->gid, AT_SYMLINK_NOFOLLOW));
2885 * Set ownership given a file descriptor.
2887 #ifndef _SYS_SYSPROTO_H_
2888 struct fchown_args {
2895 sys_fchown(struct thread *td, struct fchown_args *uap)
2898 cap_rights_t rights;
2901 AUDIT_ARG_FD(uap->fd);
2902 AUDIT_ARG_OWNER(uap->uid, uap->gid);
2903 error = fget(td, uap->fd, cap_rights_init(&rights, CAP_FCHOWN), &fp);
2906 error = fo_chown(fp, uap->uid, uap->gid, td->td_ucred, td);
2912 * Common implementation code for utimes(), lutimes(), and futimes().
2915 getutimes(const struct timeval *usrtvp, enum uio_seg tvpseg,
2916 struct timespec *tsp)
2918 struct timeval tv[2];
2919 const struct timeval *tvp;
2922 if (usrtvp == NULL) {
2923 vfs_timestamp(&tsp[0]);
2926 if (tvpseg == UIO_SYSSPACE) {
2929 if ((error = copyin(usrtvp, tv, sizeof(tv))) != 0)
2934 if (tvp[0].tv_usec < 0 || tvp[0].tv_usec >= 1000000 ||
2935 tvp[1].tv_usec < 0 || tvp[1].tv_usec >= 1000000)
2937 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
2938 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
2944 * Common implementation code for futimens(), utimensat().
2946 #define UTIMENS_NULL 0x1
2947 #define UTIMENS_EXIT 0x2
2949 getutimens(const struct timespec *usrtsp, enum uio_seg tspseg,
2950 struct timespec *tsp, int *retflags)
2952 struct timespec tsnow;
2955 vfs_timestamp(&tsnow);
2957 if (usrtsp == NULL) {
2960 *retflags |= UTIMENS_NULL;
2963 if (tspseg == UIO_SYSSPACE) {
2966 } else if ((error = copyin(usrtsp, tsp, sizeof(*tsp) * 2)) != 0)
2968 if (tsp[0].tv_nsec == UTIME_OMIT && tsp[1].tv_nsec == UTIME_OMIT)
2969 *retflags |= UTIMENS_EXIT;
2970 if (tsp[0].tv_nsec == UTIME_NOW && tsp[1].tv_nsec == UTIME_NOW)
2971 *retflags |= UTIMENS_NULL;
2972 if (tsp[0].tv_nsec == UTIME_OMIT)
2973 tsp[0].tv_sec = VNOVAL;
2974 else if (tsp[0].tv_nsec == UTIME_NOW)
2976 else if (tsp[0].tv_nsec < 0 || tsp[0].tv_nsec >= 1000000000L)
2978 if (tsp[1].tv_nsec == UTIME_OMIT)
2979 tsp[1].tv_sec = VNOVAL;
2980 else if (tsp[1].tv_nsec == UTIME_NOW)
2982 else if (tsp[1].tv_nsec < 0 || tsp[1].tv_nsec >= 1000000000L)
2989 * Common implementation code for utimes(), lutimes(), futimes(), futimens(),
2993 setutimes(struct thread *td, struct vnode *vp, const struct timespec *ts,
2994 int numtimes, int nullflag)
2998 int error, setbirthtime;
3000 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
3002 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3004 if (numtimes < 3 && !VOP_GETATTR(vp, &vattr, td->td_ucred) &&
3005 timespeccmp(&ts[1], &vattr.va_birthtime, < ))
3008 vattr.va_atime = ts[0];
3009 vattr.va_mtime = ts[1];
3011 vattr.va_birthtime = ts[1];
3013 vattr.va_birthtime = ts[2];
3015 vattr.va_vaflags |= VA_UTIMES_NULL;
3017 error = mac_vnode_check_setutimes(td->td_ucred, vp, vattr.va_atime,
3021 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3023 vn_finished_write(mp);
3028 * Set the access and modification times of a file.
3030 #ifndef _SYS_SYSPROTO_H_
3031 struct utimes_args {
3033 struct timeval *tptr;
3037 sys_utimes(struct thread *td, struct utimes_args *uap)
3040 return (kern_utimesat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
3041 uap->tptr, UIO_USERSPACE));
3044 #ifndef _SYS_SYSPROTO_H_
3045 struct futimesat_args {
3048 const struct timeval * times;
3052 sys_futimesat(struct thread *td, struct futimesat_args *uap)
3055 return (kern_utimesat(td, uap->fd, uap->path, UIO_USERSPACE,
3056 uap->times, UIO_USERSPACE));
3060 kern_utimesat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
3061 struct timeval *tptr, enum uio_seg tptrseg)
3063 struct nameidata nd;
3064 struct timespec ts[2];
3065 cap_rights_t rights;
3068 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3070 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, fd,
3071 cap_rights_init(&rights, CAP_FUTIMES), td);
3073 if ((error = namei(&nd)) != 0)
3075 NDFREE(&nd, NDF_ONLY_PNBUF);
3076 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3082 * Set the access and modification times of a file.
3084 #ifndef _SYS_SYSPROTO_H_
3085 struct lutimes_args {
3087 struct timeval *tptr;
3091 sys_lutimes(struct thread *td, struct lutimes_args *uap)
3094 return (kern_lutimes(td, uap->path, UIO_USERSPACE, uap->tptr,
3099 kern_lutimes(struct thread *td, char *path, enum uio_seg pathseg,
3100 struct timeval *tptr, enum uio_seg tptrseg)
3102 struct timespec ts[2];
3103 struct nameidata nd;
3106 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3108 NDINIT(&nd, LOOKUP, NOFOLLOW | AUDITVNODE1, pathseg, path, td);
3109 if ((error = namei(&nd)) != 0)
3111 NDFREE(&nd, NDF_ONLY_PNBUF);
3112 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3118 * Set the access and modification times of a file.
3120 #ifndef _SYS_SYSPROTO_H_
3121 struct futimes_args {
3123 struct timeval *tptr;
3127 sys_futimes(struct thread *td, struct futimes_args *uap)
3130 return (kern_futimes(td, uap->fd, uap->tptr, UIO_USERSPACE));
3134 kern_futimes(struct thread *td, int fd, struct timeval *tptr,
3135 enum uio_seg tptrseg)
3137 struct timespec ts[2];
3139 cap_rights_t rights;
3143 error = getutimes(tptr, tptrseg, ts);
3146 error = getvnode(td, fd, cap_rights_init(&rights, CAP_FUTIMES), &fp);
3150 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
3151 AUDIT_ARG_VNODE1(fp->f_vnode);
3152 VOP_UNLOCK(fp->f_vnode, 0);
3154 error = setutimes(td, fp->f_vnode, ts, 2, tptr == NULL);
3160 sys_futimens(struct thread *td, struct futimens_args *uap)
3163 return (kern_futimens(td, uap->fd, uap->times, UIO_USERSPACE));
3167 kern_futimens(struct thread *td, int fd, struct timespec *tptr,
3168 enum uio_seg tptrseg)
3170 struct timespec ts[2];
3172 cap_rights_t rights;
3176 error = getutimens(tptr, tptrseg, ts, &flags);
3179 if (flags & UTIMENS_EXIT)
3181 error = getvnode(td, fd, cap_rights_init(&rights, CAP_FUTIMES), &fp);
3185 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
3186 AUDIT_ARG_VNODE1(fp->f_vnode);
3187 VOP_UNLOCK(fp->f_vnode, 0);
3189 error = setutimes(td, fp->f_vnode, ts, 2, flags & UTIMENS_NULL);
3195 sys_utimensat(struct thread *td, struct utimensat_args *uap)
3198 return (kern_utimensat(td, uap->fd, uap->path, UIO_USERSPACE,
3199 uap->times, UIO_USERSPACE, uap->flag));
3203 kern_utimensat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
3204 struct timespec *tptr, enum uio_seg tptrseg, int flag)
3206 struct nameidata nd;
3207 struct timespec ts[2];
3208 cap_rights_t rights;
3211 if (flag & ~AT_SYMLINK_NOFOLLOW)
3214 if ((error = getutimens(tptr, tptrseg, ts, &flags)) != 0)
3216 NDINIT_ATRIGHTS(&nd, LOOKUP, ((flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW :
3217 FOLLOW) | AUDITVNODE1, pathseg, path, fd,
3218 cap_rights_init(&rights, CAP_FUTIMES), td);
3219 if ((error = namei(&nd)) != 0)
3222 * We are allowed to call namei() regardless of 2xUTIME_OMIT.
3224 * "If both tv_nsec fields are UTIME_OMIT... EACCESS may be detected."
3225 * "Search permission is denied by a component of the path prefix."
3227 NDFREE(&nd, NDF_ONLY_PNBUF);
3228 if ((flags & UTIMENS_EXIT) == 0)
3229 error = setutimes(td, nd.ni_vp, ts, 2, flags & UTIMENS_NULL);
3235 * Truncate a file given its path name.
3237 #ifndef _SYS_SYSPROTO_H_
3238 struct truncate_args {
3245 sys_truncate(struct thread *td, struct truncate_args *uap)
3248 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3252 kern_truncate(struct thread *td, char *path, enum uio_seg pathseg, off_t length)
3258 struct nameidata nd;
3263 NDINIT(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, td);
3264 if ((error = namei(&nd)) != 0)
3267 rl_cookie = vn_rangelock_wlock(vp, 0, OFF_MAX);
3268 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0) {
3269 vn_rangelock_unlock(vp, rl_cookie);
3273 NDFREE(&nd, NDF_ONLY_PNBUF);
3274 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3275 if (vp->v_type == VDIR)
3278 else if ((error = mac_vnode_check_write(td->td_ucred, NOCRED, vp))) {
3281 else if ((error = vn_writechk(vp)) == 0 &&
3282 (error = VOP_ACCESS(vp, VWRITE, td->td_ucred, td)) == 0) {
3284 vattr.va_size = length;
3285 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3288 vn_finished_write(mp);
3289 vn_rangelock_unlock(vp, rl_cookie);
3294 #if defined(COMPAT_43)
3296 * Truncate a file given its path name.
3298 #ifndef _SYS_SYSPROTO_H_
3299 struct otruncate_args {
3305 otruncate(struct thread *td, struct otruncate_args *uap)
3308 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3310 #endif /* COMPAT_43 */
3312 #if defined(COMPAT_FREEBSD6)
3313 /* Versions with the pad argument */
3315 freebsd6_truncate(struct thread *td, struct freebsd6_truncate_args *uap)
3318 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3322 freebsd6_ftruncate(struct thread *td, struct freebsd6_ftruncate_args *uap)
3325 return (kern_ftruncate(td, uap->fd, uap->length));
3330 kern_fsync(struct thread *td, int fd, bool fullsync)
3335 cap_rights_t rights;
3336 int error, lock_flags;
3339 error = getvnode(td, fd, cap_rights_init(&rights, CAP_FSYNC), &fp);
3345 /* XXXKIB: compete outstanding aio writes */;
3347 error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
3350 if (MNT_SHARED_WRITES(mp) ||
3351 ((mp == NULL) && MNT_SHARED_WRITES(vp->v_mount))) {
3352 lock_flags = LK_SHARED;
3354 lock_flags = LK_EXCLUSIVE;
3356 vn_lock(vp, lock_flags | LK_RETRY);
3357 AUDIT_ARG_VNODE1(vp);
3358 if (vp->v_object != NULL) {
3359 VM_OBJECT_WLOCK(vp->v_object);
3360 vm_object_page_clean(vp->v_object, 0, 0, 0);
3361 VM_OBJECT_WUNLOCK(vp->v_object);
3363 error = fullsync ? VOP_FSYNC(vp, MNT_WAIT, td) : VOP_FDATASYNC(vp, td);
3365 vn_finished_write(mp);
3372 * Sync an open file.
3374 #ifndef _SYS_SYSPROTO_H_
3380 sys_fsync(struct thread *td, struct fsync_args *uap)
3383 return (kern_fsync(td, uap->fd, true));
3387 sys_fdatasync(struct thread *td, struct fdatasync_args *uap)
3390 return (kern_fsync(td, uap->fd, false));
3394 * Rename files. Source and destination must either both be directories, or
3395 * both not be directories. If target is a directory, it must be empty.
3397 #ifndef _SYS_SYSPROTO_H_
3398 struct rename_args {
3404 sys_rename(struct thread *td, struct rename_args *uap)
3407 return (kern_renameat(td, AT_FDCWD, uap->from, AT_FDCWD,
3408 uap->to, UIO_USERSPACE));
3411 #ifndef _SYS_SYSPROTO_H_
3412 struct renameat_args {
3420 sys_renameat(struct thread *td, struct renameat_args *uap)
3423 return (kern_renameat(td, uap->oldfd, uap->old, uap->newfd, uap->new,
3428 kern_renameat(struct thread *td, int oldfd, char *old, int newfd, char *new,
3429 enum uio_seg pathseg)
3431 struct mount *mp = NULL;
3432 struct vnode *tvp, *fvp, *tdvp;
3433 struct nameidata fromnd, tond;
3434 cap_rights_t rights;
3440 NDINIT_ATRIGHTS(&fromnd, DELETE, LOCKPARENT | LOCKLEAF | SAVESTART |
3441 AUDITVNODE1, pathseg, old, oldfd,
3442 cap_rights_init(&rights, CAP_RENAMEAT_SOURCE), td);
3444 NDINIT_ATRIGHTS(&fromnd, DELETE, WANTPARENT | SAVESTART | AUDITVNODE1,
3445 pathseg, old, oldfd,
3446 cap_rights_init(&rights, CAP_RENAMEAT_SOURCE), td);
3449 if ((error = namei(&fromnd)) != 0)
3452 error = mac_vnode_check_rename_from(td->td_ucred, fromnd.ni_dvp,
3453 fromnd.ni_vp, &fromnd.ni_cnd);
3454 VOP_UNLOCK(fromnd.ni_dvp, 0);
3455 if (fromnd.ni_dvp != fromnd.ni_vp)
3456 VOP_UNLOCK(fromnd.ni_vp, 0);
3459 NDINIT_ATRIGHTS(&tond, RENAME, LOCKPARENT | LOCKLEAF | NOCACHE |
3460 SAVESTART | AUDITVNODE2, pathseg, new, newfd,
3461 cap_rights_init(&rights, CAP_RENAMEAT_TARGET), td);
3462 if (fromnd.ni_vp->v_type == VDIR)
3463 tond.ni_cnd.cn_flags |= WILLBEDIR;
3464 if ((error = namei(&tond)) != 0) {
3465 /* Translate error code for rename("dir1", "dir2/."). */
3466 if (error == EISDIR && fvp->v_type == VDIR)
3468 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3469 vrele(fromnd.ni_dvp);
3475 error = vn_start_write(fvp, &mp, V_NOWAIT);
3477 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3478 NDFREE(&tond, NDF_ONLY_PNBUF);
3485 vrele(fromnd.ni_dvp);
3487 vrele(tond.ni_startdir);
3488 if (fromnd.ni_startdir != NULL)
3489 vrele(fromnd.ni_startdir);
3490 error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH);
3496 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
3499 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
3504 if (newfd != AT_FDCWD) {
3506 * If the target already exists we require CAP_UNLINKAT
3509 error = cap_check(&tond.ni_filecaps.fc_rights,
3510 cap_rights_init(&rights, CAP_UNLINKAT));
3521 * If the source is the same as the destination (that is, if they
3522 * are links to the same vnode), then there is nothing to do.
3528 error = mac_vnode_check_rename_to(td->td_ucred, tdvp,
3529 tond.ni_vp, fromnd.ni_dvp == tdvp, &tond.ni_cnd);
3533 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
3534 tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
3535 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3536 NDFREE(&tond, NDF_ONLY_PNBUF);
3538 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3539 NDFREE(&tond, NDF_ONLY_PNBUF);
3546 vrele(fromnd.ni_dvp);
3549 vrele(tond.ni_startdir);
3550 vn_finished_write(mp);
3552 if (fromnd.ni_startdir)
3553 vrele(fromnd.ni_startdir);
3560 * Make a directory file.
3562 #ifndef _SYS_SYSPROTO_H_
3569 sys_mkdir(struct thread *td, struct mkdir_args *uap)
3572 return (kern_mkdirat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
3576 #ifndef _SYS_SYSPROTO_H_
3577 struct mkdirat_args {
3584 sys_mkdirat(struct thread *td, struct mkdirat_args *uap)
3587 return (kern_mkdirat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode));
3591 kern_mkdirat(struct thread *td, int fd, char *path, enum uio_seg segflg,
3597 struct nameidata nd;
3598 cap_rights_t rights;
3601 AUDIT_ARG_MODE(mode);
3604 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
3605 NOCACHE, segflg, path, fd, cap_rights_init(&rights, CAP_MKDIRAT),
3607 nd.ni_cnd.cn_flags |= WILLBEDIR;
3608 if ((error = namei(&nd)) != 0)
3612 NDFREE(&nd, NDF_ONLY_PNBUF);
3614 * XXX namei called with LOCKPARENT but not LOCKLEAF has
3615 * the strange behaviour of leaving the vnode unlocked
3616 * if the target is the same vnode as the parent.
3618 if (vp == nd.ni_dvp)
3625 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3626 NDFREE(&nd, NDF_ONLY_PNBUF);
3628 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3633 vattr.va_type = VDIR;
3634 vattr.va_mode = (mode & ACCESSPERMS) &~ td->td_proc->p_fd->fd_cmask;
3636 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
3641 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
3645 NDFREE(&nd, NDF_ONLY_PNBUF);
3649 vn_finished_write(mp);
3654 * Remove a directory file.
3656 #ifndef _SYS_SYSPROTO_H_
3662 sys_rmdir(struct thread *td, struct rmdir_args *uap)
3665 return (kern_rmdirat(td, AT_FDCWD, uap->path, UIO_USERSPACE));
3669 kern_rmdirat(struct thread *td, int fd, char *path, enum uio_seg pathseg)
3673 struct nameidata nd;
3674 cap_rights_t rights;
3679 NDINIT_ATRIGHTS(&nd, DELETE, LOCKPARENT | LOCKLEAF | AUDITVNODE1,
3680 pathseg, path, fd, cap_rights_init(&rights, CAP_UNLINKAT), td);
3681 if ((error = namei(&nd)) != 0)
3684 if (vp->v_type != VDIR) {
3689 * No rmdir "." please.
3691 if (nd.ni_dvp == vp) {
3696 * The root of a mounted filesystem cannot be deleted.
3698 if (vp->v_vflag & VV_ROOT) {
3703 error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp,
3708 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3709 NDFREE(&nd, NDF_ONLY_PNBUF);
3711 if (nd.ni_dvp == vp)
3715 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3719 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_UNLINK);
3720 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
3721 vn_finished_write(mp);
3723 NDFREE(&nd, NDF_ONLY_PNBUF);
3725 if (nd.ni_dvp == vp)
3732 #if defined(COMPAT_43) || defined(COMPAT_FREEBSD11)
3734 freebsd11_kern_getdirentries(struct thread *td, int fd, char *ubuf, u_int count,
3735 long *basep, void (*func)(struct freebsd11_dirent *))
3737 struct freebsd11_dirent dstdp;
3738 struct dirent *dp, *edp;
3741 ssize_t resid, ucount;
3744 /* XXX arbitrary sanity limit on `count'. */
3745 count = min(count, 64 * 1024);
3747 dirbuf = malloc(count, M_TEMP, M_WAITOK);
3749 error = kern_getdirentries(td, fd, dirbuf, count, &base, &resid,
3757 for (dp = (struct dirent *)dirbuf,
3758 edp = (struct dirent *)&dirbuf[count - resid];
3759 ucount < count && dp < edp; ) {
3760 if (dp->d_reclen == 0)
3762 MPASS(dp->d_reclen >= _GENERIC_DIRLEN(0));
3763 if (dp->d_namlen >= sizeof(dstdp.d_name))
3765 dstdp.d_type = dp->d_type;
3766 dstdp.d_namlen = dp->d_namlen;
3767 dstdp.d_fileno = dp->d_fileno; /* truncate */
3768 if (dstdp.d_fileno != dp->d_fileno) {
3769 switch (ino64_trunc_error) {
3777 dstdp.d_fileno = UINT32_MAX;
3781 dstdp.d_reclen = sizeof(dstdp) - sizeof(dstdp.d_name) +
3782 ((dp->d_namlen + 1 + 3) &~ 3);
3783 bcopy(dp->d_name, dstdp.d_name, dstdp.d_namlen);
3784 bzero(dstdp.d_name + dstdp.d_namlen,
3785 dstdp.d_reclen - offsetof(struct freebsd11_dirent, d_name) -
3787 MPASS(dstdp.d_reclen <= dp->d_reclen);
3788 MPASS(ucount + dstdp.d_reclen <= count);
3791 error = copyout(&dstdp, ubuf + ucount, dstdp.d_reclen);
3794 dp = (struct dirent *)((char *)dp + dp->d_reclen);
3795 ucount += dstdp.d_reclen;
3799 free(dirbuf, M_TEMP);
3801 td->td_retval[0] = ucount;
3808 ogetdirentries_cvt(struct freebsd11_dirent *dp)
3810 #if (BYTE_ORDER == LITTLE_ENDIAN)
3812 * The expected low byte of dp->d_namlen is our dp->d_type.
3813 * The high MBZ byte of dp->d_namlen is our dp->d_namlen.
3815 dp->d_type = dp->d_namlen;
3819 * The dp->d_type is the high byte of the expected dp->d_namlen,
3820 * so must be zero'ed.
3827 * Read a block of directory entries in a filesystem independent format.
3829 #ifndef _SYS_SYSPROTO_H_
3830 struct ogetdirentries_args {
3838 ogetdirentries(struct thread *td, struct ogetdirentries_args *uap)
3843 error = kern_ogetdirentries(td, uap, &loff);
3845 error = copyout(&loff, uap->basep, sizeof(long));
3850 kern_ogetdirentries(struct thread *td, struct ogetdirentries_args *uap,
3856 /* XXX arbitrary sanity limit on `count'. */
3857 if (uap->count > 64 * 1024)
3860 error = freebsd11_kern_getdirentries(td, uap->fd, uap->buf, uap->count,
3861 &base, ogetdirentries_cvt);
3863 if (error == 0 && uap->basep != NULL)
3864 error = copyout(&base, uap->basep, sizeof(long));
3868 #endif /* COMPAT_43 */
3870 #if defined(COMPAT_FREEBSD11)
3871 #ifndef _SYS_SYSPROTO_H_
3872 struct freebsd11_getdirentries_args {
3880 freebsd11_getdirentries(struct thread *td,
3881 struct freebsd11_getdirentries_args *uap)
3886 error = freebsd11_kern_getdirentries(td, uap->fd, uap->buf, uap->count,
3889 if (error == 0 && uap->basep != NULL)
3890 error = copyout(&base, uap->basep, sizeof(long));
3895 freebsd11_getdents(struct thread *td, struct freebsd11_getdents_args *uap)
3897 struct freebsd11_getdirentries_args ap;
3901 ap.count = uap->count;
3903 return (freebsd11_getdirentries(td, &ap));
3905 #endif /* COMPAT_FREEBSD11 */
3908 * Read a block of directory entries in a filesystem independent format.
3911 sys_getdirentries(struct thread *td, struct getdirentries_args *uap)
3916 error = kern_getdirentries(td, uap->fd, uap->buf, uap->count, &base,
3917 NULL, UIO_USERSPACE);
3920 if (uap->basep != NULL)
3921 error = copyout(&base, uap->basep, sizeof(off_t));
3926 kern_getdirentries(struct thread *td, int fd, char *buf, size_t count,
3927 off_t *basep, ssize_t *residp, enum uio_seg bufseg)
3933 cap_rights_t rights;
3939 if (count > IOSIZE_MAX)
3941 auio.uio_resid = count;
3942 error = getvnode(td, fd, cap_rights_init(&rights, CAP_READ), &fp);
3945 if ((fp->f_flag & FREAD) == 0) {
3950 foffset = foffset_lock(fp, 0);
3952 if (vp->v_type != VDIR) {
3956 aiov.iov_base = buf;
3957 aiov.iov_len = count;
3958 auio.uio_iov = &aiov;
3959 auio.uio_iovcnt = 1;
3960 auio.uio_rw = UIO_READ;
3961 auio.uio_segflg = bufseg;
3963 vn_lock(vp, LK_SHARED | LK_RETRY);
3964 AUDIT_ARG_VNODE1(vp);
3965 loff = auio.uio_offset = foffset;
3967 error = mac_vnode_check_readdir(td->td_ucred, vp);
3970 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL,
3972 foffset = auio.uio_offset;
3977 if (count == auio.uio_resid &&
3978 (vp->v_vflag & VV_ROOT) &&
3979 (vp->v_mount->mnt_flag & MNT_UNION)) {
3980 struct vnode *tvp = vp;
3982 vp = vp->v_mount->mnt_vnodecovered;
3993 *residp = auio.uio_resid;
3994 td->td_retval[0] = count - auio.uio_resid;
3996 foffset_unlock(fp, foffset, 0);
4002 * Set the mode mask for creation of filesystem nodes.
4004 #ifndef _SYS_SYSPROTO_H_
4010 sys_umask(struct thread *td, struct umask_args *uap)
4012 struct filedesc *fdp;
4014 fdp = td->td_proc->p_fd;
4015 FILEDESC_XLOCK(fdp);
4016 td->td_retval[0] = fdp->fd_cmask;
4017 fdp->fd_cmask = uap->newmask & ALLPERMS;
4018 FILEDESC_XUNLOCK(fdp);
4023 * Void all references to file by ripping underlying filesystem away from
4026 #ifndef _SYS_SYSPROTO_H_
4027 struct revoke_args {
4032 sys_revoke(struct thread *td, struct revoke_args *uap)
4036 struct nameidata nd;
4039 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
4041 if ((error = namei(&nd)) != 0)
4044 NDFREE(&nd, NDF_ONLY_PNBUF);
4045 if (vp->v_type != VCHR || vp->v_rdev == NULL) {
4050 error = mac_vnode_check_revoke(td->td_ucred, vp);
4054 error = VOP_GETATTR(vp, &vattr, td->td_ucred);
4057 if (td->td_ucred->cr_uid != vattr.va_uid) {
4058 error = priv_check(td, PRIV_VFS_ADMIN);
4063 VOP_REVOKE(vp, REVOKEALL);
4070 * Convert a user file descriptor to a kernel file entry and check that, if it
4071 * is a capability, the correct rights are present. A reference on the file
4072 * entry is held upon returning.
4075 getvnode(struct thread *td, int fd, cap_rights_t *rightsp, struct file **fpp)
4080 error = fget_unlocked(td->td_proc->p_fd, fd, rightsp, &fp, NULL);
4085 * The file could be not of the vnode type, or it may be not
4086 * yet fully initialized, in which case the f_vnode pointer
4087 * may be set, but f_ops is still badfileops. E.g.,
4088 * devfs_open() transiently create such situation to
4089 * facilitate csw d_fdopen().
4091 * Dupfdopen() handling in kern_openat() installs the
4092 * half-baked file into the process descriptor table, allowing
4093 * other thread to dereference it. Guard against the race by
4096 if (fp->f_vnode == NULL || fp->f_ops == &badfileops) {
4106 * Get an (NFS) file handle.
4108 #ifndef _SYS_SYSPROTO_H_
4109 struct lgetfh_args {
4115 sys_lgetfh(struct thread *td, struct lgetfh_args *uap)
4117 struct nameidata nd;
4122 error = priv_check(td, PRIV_VFS_GETFH);
4125 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
4130 NDFREE(&nd, NDF_ONLY_PNBUF);
4132 bzero(&fh, sizeof(fh));
4133 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
4134 error = VOP_VPTOFH(vp, &fh.fh_fid);
4137 error = copyout(&fh, uap->fhp, sizeof (fh));
4141 #ifndef _SYS_SYSPROTO_H_
4148 sys_getfh(struct thread *td, struct getfh_args *uap)
4150 struct nameidata nd;
4155 error = priv_check(td, PRIV_VFS_GETFH);
4158 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
4163 NDFREE(&nd, NDF_ONLY_PNBUF);
4165 bzero(&fh, sizeof(fh));
4166 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
4167 error = VOP_VPTOFH(vp, &fh.fh_fid);
4170 error = copyout(&fh, uap->fhp, sizeof (fh));
4175 * syscall for the rpc.lockd to use to translate a NFS file handle into an
4178 * warning: do not remove the priv_check() call or this becomes one giant
4181 #ifndef _SYS_SYSPROTO_H_
4182 struct fhopen_args {
4183 const struct fhandle *u_fhp;
4188 sys_fhopen(struct thread *td, struct fhopen_args *uap)
4197 error = priv_check(td, PRIV_VFS_FHOPEN);
4201 fmode = FFLAGS(uap->flags);
4202 /* why not allow a non-read/write open for our lockd? */
4203 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
4205 error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
4208 /* find the mount point */
4209 mp = vfs_busyfs(&fhp.fh_fsid);
4212 /* now give me my vnode, it gets returned to me locked */
4213 error = VFS_FHTOVP(mp, &fhp.fh_fid, LK_EXCLUSIVE, &vp);
4218 error = falloc_noinstall(td, &fp);
4224 * An extra reference on `fp' has been held for us by
4225 * falloc_noinstall().
4231 error = vn_open_vnode(vp, fmode, td->td_ucred, td, fp);
4233 KASSERT(fp->f_ops == &badfileops,
4234 ("VOP_OPEN in fhopen() set f_ops"));
4235 KASSERT(td->td_dupfd < 0,
4236 ("fhopen() encountered fdopen()"));
4246 finit(fp, (fmode & FMASK) | (fp->f_flag & FHASLOCK), DTYPE_VNODE, vp,
4249 if ((fmode & O_TRUNC) != 0) {
4250 error = fo_truncate(fp, 0, td->td_ucred, td);
4255 error = finstall(td, fp, &indx, fmode, NULL);
4258 td->td_retval[0] = indx;
4263 * Stat an (NFS) file handle.
4265 #ifndef _SYS_SYSPROTO_H_
4266 struct fhstat_args {
4267 struct fhandle *u_fhp;
4272 sys_fhstat(struct thread *td, struct fhstat_args *uap)
4278 error = copyin(uap->u_fhp, &fh, sizeof(fh));
4281 error = kern_fhstat(td, fh, &sb);
4283 error = copyout(&sb, uap->sb, sizeof(sb));
4288 kern_fhstat(struct thread *td, struct fhandle fh, struct stat *sb)
4294 error = priv_check(td, PRIV_VFS_FHSTAT);
4297 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4299 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_EXCLUSIVE, &vp);
4303 error = vn_stat(vp, sb, td->td_ucred, NOCRED, td);
4309 * Implement fstatfs() for (NFS) file handles.
4311 #ifndef _SYS_SYSPROTO_H_
4312 struct fhstatfs_args {
4313 struct fhandle *u_fhp;
4318 sys_fhstatfs(struct thread *td, struct fhstatfs_args *uap)
4324 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4327 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
4328 error = kern_fhstatfs(td, fh, sfp);
4330 error = copyout(sfp, uap->buf, sizeof(*sfp));
4331 free(sfp, M_STATFS);
4336 kern_fhstatfs(struct thread *td, fhandle_t fh, struct statfs *buf)
4343 error = priv_check(td, PRIV_VFS_FHSTATFS);
4346 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4348 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_EXCLUSIVE, &vp);
4354 error = prison_canseemount(td->td_ucred, mp);
4358 error = mac_mount_check_stat(td->td_ucred, mp);
4363 * Set these in case the underlying filesystem fails to do so.
4366 sp->f_version = STATFS_VERSION;
4367 sp->f_namemax = NAME_MAX;
4368 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
4369 error = VFS_STATFS(mp, sp);
4378 kern_posix_fallocate(struct thread *td, int fd, off_t offset, off_t len)
4383 cap_rights_t rights;
4384 off_t olen, ooffset;
4387 int audited_vnode1 = 0;
4391 if (offset < 0 || len <= 0)
4393 /* Check for wrap. */
4394 if (offset > OFF_MAX - len)
4397 error = fget(td, fd, cap_rights_init(&rights, CAP_PWRITE), &fp);
4400 AUDIT_ARG_FILE(td->td_proc, fp);
4401 if ((fp->f_ops->fo_flags & DFLAG_SEEKABLE) == 0) {
4405 if ((fp->f_flag & FWRITE) == 0) {
4409 if (fp->f_type != DTYPE_VNODE) {
4414 if (vp->v_type != VREG) {
4419 /* Allocating blocks may take a long time, so iterate. */
4426 error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
4429 error = vn_lock(vp, LK_EXCLUSIVE);
4431 vn_finished_write(mp);
4435 if (!audited_vnode1) {
4436 AUDIT_ARG_VNODE1(vp);
4441 error = mac_vnode_check_write(td->td_ucred, fp->f_cred, vp);
4444 error = VOP_ALLOCATE(vp, &offset, &len);
4446 vn_finished_write(mp);
4448 if (olen + ooffset != offset + len) {
4449 panic("offset + len changed from %jx/%jx to %jx/%jx",
4450 ooffset, olen, offset, len);
4452 if (error != 0 || len == 0)
4454 KASSERT(olen > len, ("Iteration did not make progress?"));
4463 sys_posix_fallocate(struct thread *td, struct posix_fallocate_args *uap)
4467 error = kern_posix_fallocate(td, uap->fd, uap->offset, uap->len);
4468 return (kern_posix_error(td, error));
4472 * Unlike madvise(2), we do not make a best effort to remember every
4473 * possible caching hint. Instead, we remember the last setting with
4474 * the exception that we will allow POSIX_FADV_NORMAL to adjust the
4475 * region of any current setting.
4478 kern_posix_fadvise(struct thread *td, int fd, off_t offset, off_t len,
4481 struct fadvise_info *fa, *new;
4484 cap_rights_t rights;
4488 if (offset < 0 || len < 0 || offset > OFF_MAX - len)
4490 AUDIT_ARG_VALUE(advice);
4492 case POSIX_FADV_SEQUENTIAL:
4493 case POSIX_FADV_RANDOM:
4494 case POSIX_FADV_NOREUSE:
4495 new = malloc(sizeof(*fa), M_FADVISE, M_WAITOK);
4497 case POSIX_FADV_NORMAL:
4498 case POSIX_FADV_WILLNEED:
4499 case POSIX_FADV_DONTNEED:
4505 /* XXX: CAP_POSIX_FADVISE? */
4507 error = fget(td, fd, cap_rights_init(&rights), &fp);
4510 AUDIT_ARG_FILE(td->td_proc, fp);
4511 if ((fp->f_ops->fo_flags & DFLAG_SEEKABLE) == 0) {
4515 if (fp->f_type != DTYPE_VNODE) {
4520 if (vp->v_type != VREG) {
4527 end = offset + len - 1;
4529 case POSIX_FADV_SEQUENTIAL:
4530 case POSIX_FADV_RANDOM:
4531 case POSIX_FADV_NOREUSE:
4533 * Try to merge any existing non-standard region with
4534 * this new region if possible, otherwise create a new
4535 * non-standard region for this request.
4537 mtx_pool_lock(mtxpool_sleep, fp);
4539 if (fa != NULL && fa->fa_advice == advice &&
4540 ((fa->fa_start <= end && fa->fa_end >= offset) ||
4541 (end != OFF_MAX && fa->fa_start == end + 1) ||
4542 (fa->fa_end != OFF_MAX && fa->fa_end + 1 == offset))) {
4543 if (offset < fa->fa_start)
4544 fa->fa_start = offset;
4545 if (end > fa->fa_end)
4548 new->fa_advice = advice;
4549 new->fa_start = offset;
4554 mtx_pool_unlock(mtxpool_sleep, fp);
4556 case POSIX_FADV_NORMAL:
4558 * If a the "normal" region overlaps with an existing
4559 * non-standard region, trim or remove the
4560 * non-standard region.
4562 mtx_pool_lock(mtxpool_sleep, fp);
4565 if (offset <= fa->fa_start && end >= fa->fa_end) {
4567 fp->f_advice = NULL;
4568 } else if (offset <= fa->fa_start &&
4569 end >= fa->fa_start)
4570 fa->fa_start = end + 1;
4571 else if (offset <= fa->fa_end && end >= fa->fa_end)
4572 fa->fa_end = offset - 1;
4573 else if (offset >= fa->fa_start && end <= fa->fa_end) {
4575 * If the "normal" region is a middle
4576 * portion of the existing
4577 * non-standard region, just remove
4578 * the whole thing rather than picking
4579 * one side or the other to
4583 fp->f_advice = NULL;
4586 mtx_pool_unlock(mtxpool_sleep, fp);
4588 case POSIX_FADV_WILLNEED:
4589 case POSIX_FADV_DONTNEED:
4590 error = VOP_ADVISE(vp, offset, end, advice);
4596 free(new, M_FADVISE);
4601 sys_posix_fadvise(struct thread *td, struct posix_fadvise_args *uap)
4605 error = kern_posix_fadvise(td, uap->fd, uap->offset, uap->len,
4607 return (kern_posix_error(td, error));