2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
6 * (c) UNIX System Laboratories, Inc.
7 * All or some portions of this file are derived from material licensed
8 * to the University of California by American Telephone and Telegraph
9 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10 * the permission of UNIX System Laboratories, Inc.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * @(#)vfs_syscalls.c 8.13 (Berkeley) 4/15/94
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
42 #include "opt_capsicum.h"
43 #include "opt_ktrace.h"
45 #include <sys/param.h>
46 #include <sys/systm.h>
49 #include <sys/capsicum.h>
51 #include <sys/sysent.h>
52 #include <sys/malloc.h>
53 #include <sys/mount.h>
54 #include <sys/mutex.h>
55 #include <sys/sysproto.h>
56 #include <sys/namei.h>
57 #include <sys/filedesc.h>
58 #include <sys/kernel.h>
59 #include <sys/fcntl.h>
61 #include <sys/filio.h>
62 #include <sys/limits.h>
63 #include <sys/linker.h>
64 #include <sys/rwlock.h>
68 #include <sys/unistd.h>
69 #include <sys/vnode.h>
72 #include <sys/dirent.h>
74 #include <sys/syscallsubr.h>
75 #include <sys/sysctl.h>
77 #include <sys/ktrace.h>
80 #include <machine/stdarg.h>
82 #include <security/audit/audit.h>
83 #include <security/mac/mac_framework.h>
86 #include <vm/vm_object.h>
87 #include <vm/vm_page.h>
90 #include <ufs/ufs/quota.h>
92 MALLOC_DEFINE(M_FADVISE, "fadvise", "posix_fadvise(2) information");
94 SDT_PROVIDER_DEFINE(vfs);
95 SDT_PROBE_DEFINE2(vfs, , stat, mode, "char *", "int");
96 SDT_PROBE_DEFINE2(vfs, , stat, reg, "char *", "int");
98 static int kern_chflagsat(struct thread *td, int fd, const char *path,
99 enum uio_seg pathseg, u_long flags, int atflag);
100 static int setfflags(struct thread *td, struct vnode *, u_long);
101 static int getutimes(const struct timeval *, enum uio_seg, struct timespec *);
102 static int getutimens(const struct timespec *, enum uio_seg,
103 struct timespec *, int *);
104 static int setutimes(struct thread *td, struct vnode *,
105 const struct timespec *, int, int);
106 static int vn_access(struct vnode *vp, int user_flags, struct ucred *cred,
110 * Sync each mounted filesystem.
112 #ifndef _SYS_SYSPROTO_H_
119 sys_sync(struct thread *td, struct sync_args *uap)
121 struct mount *mp, *nmp;
124 mtx_lock(&mountlist_mtx);
125 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
126 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK)) {
127 nmp = TAILQ_NEXT(mp, mnt_list);
130 if ((mp->mnt_flag & MNT_RDONLY) == 0 &&
131 vn_start_write(NULL, &mp, V_NOWAIT) == 0) {
132 save = curthread_pflags_set(TDP_SYNCIO);
133 vfs_msync(mp, MNT_NOWAIT);
134 VFS_SYNC(mp, MNT_NOWAIT);
135 curthread_pflags_restore(save);
136 vn_finished_write(mp);
138 mtx_lock(&mountlist_mtx);
139 nmp = TAILQ_NEXT(mp, mnt_list);
142 mtx_unlock(&mountlist_mtx);
147 * Change filesystem quotas.
149 #ifndef _SYS_SYSPROTO_H_
150 struct quotactl_args {
158 sys_quotactl(struct thread *td, struct quotactl_args *uap)
164 AUDIT_ARG_CMD(uap->cmd);
165 AUDIT_ARG_UID(uap->uid);
166 if (!prison_allow(td->td_ucred, PR_ALLOW_QUOTAS))
168 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
170 if ((error = namei(&nd)) != 0)
172 NDFREE(&nd, NDF_ONLY_PNBUF);
173 mp = nd.ni_vp->v_mount;
176 error = vfs_busy(mp, 0);
180 error = VFS_QUOTACTL(mp, uap->cmd, uap->uid, uap->arg);
183 * Since quota on operation typically needs to open quota
184 * file, the Q_QUOTAON handler needs to unbusy the mount point
185 * before calling into namei. Otherwise, unmount might be
186 * started between two vfs_busy() invocations (first is our,
187 * second is from mount point cross-walk code in lookup()),
190 * Require that Q_QUOTAON handles the vfs_busy() reference on
191 * its own, always returning with ubusied mount point.
193 if ((uap->cmd >> SUBCMDSHIFT) != Q_QUOTAON &&
194 (uap->cmd >> SUBCMDSHIFT) != Q_QUOTAOFF)
200 * Used by statfs conversion routines to scale the block size up if
201 * necessary so that all of the block counts are <= 'max_size'. Note
202 * that 'max_size' should be a bitmask, i.e. 2^n - 1 for some non-zero
206 statfs_scale_blocks(struct statfs *sf, long max_size)
211 KASSERT(powerof2(max_size + 1), ("%s: invalid max_size", __func__));
214 * Attempt to scale the block counts to give a more accurate
215 * overview to userland of the ratio of free space to used
216 * space. To do this, find the largest block count and compute
217 * a divisor that lets it fit into a signed integer <= max_size.
219 if (sf->f_bavail < 0)
220 count = -sf->f_bavail;
222 count = sf->f_bavail;
223 count = MAX(sf->f_blocks, MAX(sf->f_bfree, count));
224 if (count <= max_size)
227 count >>= flsl(max_size);
234 sf->f_bsize <<= shift;
235 sf->f_blocks >>= shift;
236 sf->f_bfree >>= shift;
237 sf->f_bavail >>= shift;
241 kern_do_statfs(struct thread *td, struct mount *mp, struct statfs *buf)
248 error = vfs_busy(mp, 0);
253 error = mac_mount_check_stat(td->td_ucred, mp);
258 * Set these in case the underlying filesystem fails to do so.
261 sp->f_version = STATFS_VERSION;
262 sp->f_namemax = NAME_MAX;
263 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
264 error = VFS_STATFS(mp, sp);
268 if (priv_check(td, PRIV_VFS_GENERATION)) {
269 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
270 prison_enforce_statfs(td->td_ucred, mp, buf);
278 * Get filesystem statistics.
280 #ifndef _SYS_SYSPROTO_H_
287 sys_statfs(struct thread *td, struct statfs_args *uap)
292 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
293 error = kern_statfs(td, uap->path, UIO_USERSPACE, sfp);
295 error = copyout(sfp, uap->buf, sizeof(struct statfs));
301 kern_statfs(struct thread *td, char *path, enum uio_seg pathseg,
308 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
313 mp = nd.ni_vp->v_mount;
315 NDFREE(&nd, NDF_ONLY_PNBUF);
317 return (kern_do_statfs(td, mp, buf));
321 * Get filesystem statistics.
323 #ifndef _SYS_SYSPROTO_H_
324 struct fstatfs_args {
330 sys_fstatfs(struct thread *td, struct fstatfs_args *uap)
335 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
336 error = kern_fstatfs(td, uap->fd, sfp);
338 error = copyout(sfp, uap->buf, sizeof(struct statfs));
344 kern_fstatfs(struct thread *td, int fd, struct statfs *buf)
352 error = getvnode(td, fd, &cap_fstatfs_rights, &fp);
356 vn_lock(vp, LK_SHARED | LK_RETRY);
358 AUDIT_ARG_VNODE1(vp);
365 return (kern_do_statfs(td, mp, buf));
369 * Get statistics on all filesystems.
371 #ifndef _SYS_SYSPROTO_H_
372 struct getfsstat_args {
379 sys_getfsstat(struct thread *td, struct getfsstat_args *uap)
384 if (uap->bufsize < 0 || uap->bufsize > SIZE_MAX)
386 error = kern_getfsstat(td, &uap->buf, uap->bufsize, &count,
387 UIO_USERSPACE, uap->mode);
389 td->td_retval[0] = count;
394 * If (bufsize > 0 && bufseg == UIO_SYSSPACE)
395 * The caller is responsible for freeing memory which will be allocated
399 kern_getfsstat(struct thread *td, struct statfs **buf, size_t bufsize,
400 size_t *countp, enum uio_seg bufseg, int mode)
402 struct mount *mp, *nmp;
403 struct statfs *sfsp, *sp, *sptmp, *tofree;
404 size_t count, maxcount;
412 if (bufseg == UIO_SYSSPACE)
417 maxcount = bufsize / sizeof(struct statfs);
421 } else if (bufseg == UIO_USERSPACE) {
424 } else /* if (bufseg == UIO_SYSSPACE) */ {
426 mtx_lock(&mountlist_mtx);
427 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
430 mtx_unlock(&mountlist_mtx);
431 if (maxcount > count)
433 tofree = sfsp = *buf = malloc(maxcount * sizeof(struct statfs),
437 mtx_lock(&mountlist_mtx);
438 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
439 if (prison_canseemount(td->td_ucred, mp) != 0) {
440 nmp = TAILQ_NEXT(mp, mnt_list);
444 if (mac_mount_check_stat(td->td_ucred, mp) != 0) {
445 nmp = TAILQ_NEXT(mp, mnt_list);
449 if (mode == MNT_WAIT) {
450 if (vfs_busy(mp, MBF_MNTLSTLOCK) != 0) {
452 * If vfs_busy() failed, and MBF_NOWAIT
453 * wasn't passed, then the mp is gone.
454 * Furthermore, because of MBF_MNTLSTLOCK,
455 * the mountlist_mtx was dropped. We have
456 * no other choice than to start over.
458 mtx_unlock(&mountlist_mtx);
459 free(tofree, M_STATFS);
463 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK) != 0) {
464 nmp = TAILQ_NEXT(mp, mnt_list);
468 if (sfsp != NULL && count < maxcount) {
471 * Set these in case the underlying filesystem
474 sp->f_version = STATFS_VERSION;
475 sp->f_namemax = NAME_MAX;
476 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
478 * If MNT_NOWAIT is specified, do not refresh
481 if (mode != MNT_NOWAIT) {
482 error = VFS_STATFS(mp, sp);
484 mtx_lock(&mountlist_mtx);
485 nmp = TAILQ_NEXT(mp, mnt_list);
490 if (priv_check(td, PRIV_VFS_GENERATION)) {
491 sptmp = malloc(sizeof(struct statfs), M_STATFS,
494 sptmp->f_fsid.val[0] = sptmp->f_fsid.val[1] = 0;
495 prison_enforce_statfs(td->td_ucred, mp, sptmp);
499 if (bufseg == UIO_SYSSPACE) {
500 bcopy(sp, sfsp, sizeof(*sp));
501 free(sptmp, M_STATFS);
502 } else /* if (bufseg == UIO_USERSPACE) */ {
503 error = copyout(sp, sfsp, sizeof(*sp));
504 free(sptmp, M_STATFS);
513 mtx_lock(&mountlist_mtx);
514 nmp = TAILQ_NEXT(mp, mnt_list);
517 mtx_unlock(&mountlist_mtx);
518 if (sfsp != NULL && count > maxcount)
525 #ifdef COMPAT_FREEBSD4
527 * Get old format filesystem statistics.
529 static void freebsd4_cvtstatfs(struct statfs *, struct ostatfs *);
531 #ifndef _SYS_SYSPROTO_H_
532 struct freebsd4_statfs_args {
538 freebsd4_statfs(struct thread *td, struct freebsd4_statfs_args *uap)
544 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
545 error = kern_statfs(td, uap->path, UIO_USERSPACE, sfp);
547 freebsd4_cvtstatfs(sfp, &osb);
548 error = copyout(&osb, uap->buf, sizeof(osb));
555 * Get filesystem statistics.
557 #ifndef _SYS_SYSPROTO_H_
558 struct freebsd4_fstatfs_args {
564 freebsd4_fstatfs(struct thread *td, struct freebsd4_fstatfs_args *uap)
570 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
571 error = kern_fstatfs(td, uap->fd, sfp);
573 freebsd4_cvtstatfs(sfp, &osb);
574 error = copyout(&osb, uap->buf, sizeof(osb));
581 * Get statistics on all filesystems.
583 #ifndef _SYS_SYSPROTO_H_
584 struct freebsd4_getfsstat_args {
591 freebsd4_getfsstat(struct thread *td, struct freebsd4_getfsstat_args *uap)
593 struct statfs *buf, *sp;
598 if (uap->bufsize < 0)
600 count = uap->bufsize / sizeof(struct ostatfs);
601 if (count > SIZE_MAX / sizeof(struct statfs))
603 size = count * sizeof(struct statfs);
604 error = kern_getfsstat(td, &buf, size, &count, UIO_SYSSPACE,
607 td->td_retval[0] = count;
610 while (count != 0 && error == 0) {
611 freebsd4_cvtstatfs(sp, &osb);
612 error = copyout(&osb, uap->buf, sizeof(osb));
623 * Implement fstatfs() for (NFS) file handles.
625 #ifndef _SYS_SYSPROTO_H_
626 struct freebsd4_fhstatfs_args {
627 struct fhandle *u_fhp;
632 freebsd4_fhstatfs(struct thread *td, struct freebsd4_fhstatfs_args *uap)
639 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
642 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
643 error = kern_fhstatfs(td, fh, sfp);
645 freebsd4_cvtstatfs(sfp, &osb);
646 error = copyout(&osb, uap->buf, sizeof(osb));
653 * Convert a new format statfs structure to an old format statfs structure.
656 freebsd4_cvtstatfs(struct statfs *nsp, struct ostatfs *osp)
659 statfs_scale_blocks(nsp, LONG_MAX);
660 bzero(osp, sizeof(*osp));
661 osp->f_bsize = nsp->f_bsize;
662 osp->f_iosize = MIN(nsp->f_iosize, LONG_MAX);
663 osp->f_blocks = nsp->f_blocks;
664 osp->f_bfree = nsp->f_bfree;
665 osp->f_bavail = nsp->f_bavail;
666 osp->f_files = MIN(nsp->f_files, LONG_MAX);
667 osp->f_ffree = MIN(nsp->f_ffree, LONG_MAX);
668 osp->f_owner = nsp->f_owner;
669 osp->f_type = nsp->f_type;
670 osp->f_flags = nsp->f_flags;
671 osp->f_syncwrites = MIN(nsp->f_syncwrites, LONG_MAX);
672 osp->f_asyncwrites = MIN(nsp->f_asyncwrites, LONG_MAX);
673 osp->f_syncreads = MIN(nsp->f_syncreads, LONG_MAX);
674 osp->f_asyncreads = MIN(nsp->f_asyncreads, LONG_MAX);
675 strlcpy(osp->f_fstypename, nsp->f_fstypename,
676 MIN(MFSNAMELEN, OMFSNAMELEN));
677 strlcpy(osp->f_mntonname, nsp->f_mntonname,
678 MIN(MNAMELEN, OMNAMELEN));
679 strlcpy(osp->f_mntfromname, nsp->f_mntfromname,
680 MIN(MNAMELEN, OMNAMELEN));
681 osp->f_fsid = nsp->f_fsid;
683 #endif /* COMPAT_FREEBSD4 */
685 #if defined(COMPAT_FREEBSD11)
687 * Get old format filesystem statistics.
689 static void freebsd11_cvtstatfs(struct statfs *, struct freebsd11_statfs *);
692 freebsd11_statfs(struct thread *td, struct freebsd11_statfs_args *uap)
694 struct freebsd11_statfs osb;
698 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
699 error = kern_statfs(td, uap->path, UIO_USERSPACE, sfp);
701 freebsd11_cvtstatfs(sfp, &osb);
702 error = copyout(&osb, uap->buf, sizeof(osb));
709 * Get filesystem statistics.
712 freebsd11_fstatfs(struct thread *td, struct freebsd11_fstatfs_args *uap)
714 struct freebsd11_statfs osb;
718 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
719 error = kern_fstatfs(td, uap->fd, sfp);
721 freebsd11_cvtstatfs(sfp, &osb);
722 error = copyout(&osb, uap->buf, sizeof(osb));
729 * Get statistics on all filesystems.
732 freebsd11_getfsstat(struct thread *td, struct freebsd11_getfsstat_args *uap)
734 struct freebsd11_statfs osb;
735 struct statfs *buf, *sp;
739 count = uap->bufsize / sizeof(struct ostatfs);
740 size = count * sizeof(struct statfs);
741 error = kern_getfsstat(td, &buf, size, &count, UIO_SYSSPACE,
744 td->td_retval[0] = count;
747 while (count > 0 && error == 0) {
748 freebsd11_cvtstatfs(sp, &osb);
749 error = copyout(&osb, uap->buf, sizeof(osb));
760 * Implement fstatfs() for (NFS) file handles.
763 freebsd11_fhstatfs(struct thread *td, struct freebsd11_fhstatfs_args *uap)
765 struct freebsd11_statfs osb;
770 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
773 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
774 error = kern_fhstatfs(td, fh, sfp);
776 freebsd11_cvtstatfs(sfp, &osb);
777 error = copyout(&osb, uap->buf, sizeof(osb));
784 * Convert a new format statfs structure to an old format statfs structure.
787 freebsd11_cvtstatfs(struct statfs *nsp, struct freebsd11_statfs *osp)
790 bzero(osp, sizeof(*osp));
791 osp->f_version = FREEBSD11_STATFS_VERSION;
792 osp->f_type = nsp->f_type;
793 osp->f_flags = nsp->f_flags;
794 osp->f_bsize = nsp->f_bsize;
795 osp->f_iosize = nsp->f_iosize;
796 osp->f_blocks = nsp->f_blocks;
797 osp->f_bfree = nsp->f_bfree;
798 osp->f_bavail = nsp->f_bavail;
799 osp->f_files = nsp->f_files;
800 osp->f_ffree = nsp->f_ffree;
801 osp->f_syncwrites = nsp->f_syncwrites;
802 osp->f_asyncwrites = nsp->f_asyncwrites;
803 osp->f_syncreads = nsp->f_syncreads;
804 osp->f_asyncreads = nsp->f_asyncreads;
805 osp->f_namemax = nsp->f_namemax;
806 osp->f_owner = nsp->f_owner;
807 osp->f_fsid = nsp->f_fsid;
808 strlcpy(osp->f_fstypename, nsp->f_fstypename,
809 MIN(MFSNAMELEN, sizeof(osp->f_fstypename)));
810 strlcpy(osp->f_mntonname, nsp->f_mntonname,
811 MIN(MNAMELEN, sizeof(osp->f_mntonname)));
812 strlcpy(osp->f_mntfromname, nsp->f_mntfromname,
813 MIN(MNAMELEN, sizeof(osp->f_mntfromname)));
815 #endif /* COMPAT_FREEBSD11 */
818 * Change current working directory to a given file descriptor.
820 #ifndef _SYS_SYSPROTO_H_
826 sys_fchdir(struct thread *td, struct fchdir_args *uap)
828 struct vnode *vp, *tdp;
833 AUDIT_ARG_FD(uap->fd);
834 error = getvnode(td, uap->fd, &cap_fchdir_rights,
841 vn_lock(vp, LK_SHARED | LK_RETRY);
842 AUDIT_ARG_VNODE1(vp);
843 error = change_dir(vp, td);
844 while (!error && (mp = vp->v_mountedhere) != NULL) {
847 error = VFS_ROOT(mp, LK_SHARED, &tdp);
864 * Change current working directory (``.'').
866 #ifndef _SYS_SYSPROTO_H_
872 sys_chdir(struct thread *td, struct chdir_args *uap)
875 return (kern_chdir(td, uap->path, UIO_USERSPACE));
879 kern_chdir(struct thread *td, char *path, enum uio_seg pathseg)
884 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
886 if ((error = namei(&nd)) != 0)
888 if ((error = change_dir(nd.ni_vp, td)) != 0) {
890 NDFREE(&nd, NDF_ONLY_PNBUF);
893 VOP_UNLOCK(nd.ni_vp, 0);
894 NDFREE(&nd, NDF_ONLY_PNBUF);
895 pwd_chdir(td, nd.ni_vp);
900 * Change notion of root (``/'') directory.
902 #ifndef _SYS_SYSPROTO_H_
908 sys_chroot(struct thread *td, struct chroot_args *uap)
913 error = priv_check(td, PRIV_VFS_CHROOT);
916 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
917 UIO_USERSPACE, uap->path, td);
921 error = change_dir(nd.ni_vp, td);
925 error = mac_vnode_check_chroot(td->td_ucred, nd.ni_vp);
929 VOP_UNLOCK(nd.ni_vp, 0);
930 error = pwd_chroot(td, nd.ni_vp);
932 NDFREE(&nd, NDF_ONLY_PNBUF);
937 NDFREE(&nd, NDF_ONLY_PNBUF);
942 * Common routine for chroot and chdir. Callers must provide a locked vnode
946 change_dir(struct vnode *vp, struct thread *td)
952 ASSERT_VOP_LOCKED(vp, "change_dir(): vp not locked");
953 if (vp->v_type != VDIR)
956 error = mac_vnode_check_chdir(td->td_ucred, vp);
960 return (VOP_ACCESS(vp, VEXEC, td->td_ucred, td));
964 flags_to_rights(int flags, cap_rights_t *rightsp)
967 if (flags & O_EXEC) {
968 cap_rights_set(rightsp, CAP_FEXECVE);
970 switch ((flags & O_ACCMODE)) {
972 cap_rights_set(rightsp, CAP_READ);
975 cap_rights_set(rightsp, CAP_READ);
978 cap_rights_set(rightsp, CAP_WRITE);
979 if (!(flags & (O_APPEND | O_TRUNC)))
980 cap_rights_set(rightsp, CAP_SEEK);
986 cap_rights_set(rightsp, CAP_CREATE);
989 cap_rights_set(rightsp, CAP_FTRUNCATE);
991 if (flags & (O_SYNC | O_FSYNC))
992 cap_rights_set(rightsp, CAP_FSYNC);
994 if (flags & (O_EXLOCK | O_SHLOCK))
995 cap_rights_set(rightsp, CAP_FLOCK);
999 * Check permissions, allocate an open file structure, and call the device
1000 * open routine if any.
1002 #ifndef _SYS_SYSPROTO_H_
1010 sys_open(struct thread *td, struct open_args *uap)
1013 return (kern_openat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1014 uap->flags, uap->mode));
1017 #ifndef _SYS_SYSPROTO_H_
1018 struct openat_args {
1026 sys_openat(struct thread *td, struct openat_args *uap)
1029 AUDIT_ARG_FD(uap->fd);
1030 return (kern_openat(td, uap->fd, uap->path, UIO_USERSPACE, uap->flag,
1035 kern_openat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1036 int flags, int mode)
1038 struct proc *p = td->td_proc;
1039 struct filedesc *fdp = p->p_fd;
1042 struct nameidata nd;
1043 cap_rights_t rights;
1044 int cmode, error, indx;
1048 AUDIT_ARG_FFLAGS(flags);
1049 AUDIT_ARG_MODE(mode);
1050 cap_rights_init(&rights, CAP_LOOKUP);
1051 flags_to_rights(flags, &rights);
1053 * Only one of the O_EXEC, O_RDONLY, O_WRONLY and O_RDWR flags
1056 if (flags & O_EXEC) {
1057 if (flags & O_ACCMODE)
1059 } else if ((flags & O_ACCMODE) == O_ACCMODE) {
1062 flags = FFLAGS(flags);
1066 * Allocate a file structure. The descriptor to reference it
1067 * is allocated and set by finstall() below.
1069 error = falloc_noinstall(td, &fp);
1073 * An extra reference on `fp' has been held for us by
1074 * falloc_noinstall().
1076 /* Set the flags early so the finit in devfs can pick them up. */
1077 fp->f_flag = flags & FMASK;
1078 cmode = ((mode & ~fdp->fd_cmask) & ALLPERMS) & ~S_ISTXT;
1079 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, fd,
1081 td->td_dupfd = -1; /* XXX check for fdopen */
1082 error = vn_open(&nd, &flags, cmode, fp);
1085 * If the vn_open replaced the method vector, something
1086 * wonderous happened deep below and we just pass it up
1087 * pretending we know what we do.
1089 if (error == ENXIO && fp->f_ops != &badfileops)
1093 * Handle special fdopen() case. bleh.
1095 * Don't do this for relative (capability) lookups; we don't
1096 * understand exactly what would happen, and we don't think
1097 * that it ever should.
1099 if ((nd.ni_lcf & NI_LCF_STRICTRELATIVE) == 0 &&
1100 (error == ENODEV || error == ENXIO) &&
1101 td->td_dupfd >= 0) {
1102 error = dupfdopen(td, fdp, td->td_dupfd, flags, error,
1111 NDFREE(&nd, NDF_ONLY_PNBUF);
1115 * Store the vnode, for any f_type. Typically, the vnode use
1116 * count is decremented by direct call to vn_closefile() for
1117 * files that switched type in the cdevsw fdopen() method.
1121 * If the file wasn't claimed by devfs bind it to the normal
1122 * vnode operations here.
1124 if (fp->f_ops == &badfileops) {
1125 KASSERT(vp->v_type != VFIFO, ("Unexpected fifo."));
1127 finit(fp, (flags & FMASK) | (fp->f_flag & FHASLOCK),
1128 DTYPE_VNODE, vp, &vnops);
1132 if (flags & O_TRUNC) {
1133 error = fo_truncate(fp, 0, td->td_ucred, td);
1139 * If we haven't already installed the FD (for dupfdopen), do so now.
1142 struct filecaps *fcaps;
1145 if ((nd.ni_lcf & NI_LCF_STRICTRELATIVE) != 0)
1146 fcaps = &nd.ni_filecaps;
1150 error = finstall(td, fp, &indx, flags, fcaps);
1151 /* On success finstall() consumes fcaps. */
1153 filecaps_free(&nd.ni_filecaps);
1157 filecaps_free(&nd.ni_filecaps);
1161 * Release our private reference, leaving the one associated with
1162 * the descriptor table intact.
1165 td->td_retval[0] = indx;
1168 KASSERT(indx == -1, ("indx=%d, should be -1", indx));
1177 #ifndef _SYS_SYSPROTO_H_
1178 struct ocreat_args {
1184 ocreat(struct thread *td, struct ocreat_args *uap)
1187 return (kern_openat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1188 O_WRONLY | O_CREAT | O_TRUNC, uap->mode));
1190 #endif /* COMPAT_43 */
1193 * Create a special file.
1195 #ifndef _SYS_SYSPROTO_H_
1196 struct mknodat_args {
1204 sys_mknodat(struct thread *td, struct mknodat_args *uap)
1207 return (kern_mknodat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode,
1211 #if defined(COMPAT_FREEBSD11)
1213 freebsd11_mknod(struct thread *td,
1214 struct freebsd11_mknod_args *uap)
1217 return (kern_mknodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1218 uap->mode, uap->dev));
1222 freebsd11_mknodat(struct thread *td,
1223 struct freebsd11_mknodat_args *uap)
1226 return (kern_mknodat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode,
1229 #endif /* COMPAT_FREEBSD11 */
1232 kern_mknodat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1233 int mode, dev_t dev)
1238 struct nameidata nd;
1239 int error, whiteout = 0;
1241 AUDIT_ARG_MODE(mode);
1243 switch (mode & S_IFMT) {
1246 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
1247 if (error == 0 && dev == VNOVAL)
1251 error = priv_check(td, PRIV_VFS_MKNOD_WHT);
1255 return (kern_mkfifoat(td, fd, path, pathseg, mode));
1265 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
1266 NOCACHE, pathseg, path, fd, &cap_mknodat_rights,
1268 if ((error = namei(&nd)) != 0)
1272 NDFREE(&nd, NDF_ONLY_PNBUF);
1273 if (vp == nd.ni_dvp)
1281 vattr.va_mode = (mode & ALLPERMS) &
1282 ~td->td_proc->p_fd->fd_cmask;
1283 vattr.va_rdev = dev;
1286 switch (mode & S_IFMT) {
1288 vattr.va_type = VCHR;
1291 vattr.va_type = VBLK;
1297 panic("kern_mknod: invalid mode");
1300 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1301 NDFREE(&nd, NDF_ONLY_PNBUF);
1303 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1308 if (error == 0 && !whiteout)
1309 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp,
1310 &nd.ni_cnd, &vattr);
1314 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, CREATE);
1316 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp,
1317 &nd.ni_cnd, &vattr);
1322 NDFREE(&nd, NDF_ONLY_PNBUF);
1324 vn_finished_write(mp);
1329 * Create a named pipe.
1331 #ifndef _SYS_SYSPROTO_H_
1332 struct mkfifo_args {
1338 sys_mkfifo(struct thread *td, struct mkfifo_args *uap)
1341 return (kern_mkfifoat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1345 #ifndef _SYS_SYSPROTO_H_
1346 struct mkfifoat_args {
1353 sys_mkfifoat(struct thread *td, struct mkfifoat_args *uap)
1356 return (kern_mkfifoat(td, uap->fd, uap->path, UIO_USERSPACE,
1361 kern_mkfifoat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1366 struct nameidata nd;
1369 AUDIT_ARG_MODE(mode);
1372 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
1373 NOCACHE, pathseg, path, fd, &cap_mkfifoat_rights,
1375 if ((error = namei(&nd)) != 0)
1377 if (nd.ni_vp != NULL) {
1378 NDFREE(&nd, NDF_ONLY_PNBUF);
1379 if (nd.ni_vp == nd.ni_dvp)
1386 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1387 NDFREE(&nd, NDF_ONLY_PNBUF);
1389 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1394 vattr.va_type = VFIFO;
1395 vattr.va_mode = (mode & ALLPERMS) & ~td->td_proc->p_fd->fd_cmask;
1397 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1402 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
1409 vn_finished_write(mp);
1410 NDFREE(&nd, NDF_ONLY_PNBUF);
1415 * Make a hard file link.
1417 #ifndef _SYS_SYSPROTO_H_
1424 sys_link(struct thread *td, struct link_args *uap)
1427 return (kern_linkat(td, AT_FDCWD, AT_FDCWD, uap->path, uap->link,
1428 UIO_USERSPACE, FOLLOW));
1431 #ifndef _SYS_SYSPROTO_H_
1432 struct linkat_args {
1441 sys_linkat(struct thread *td, struct linkat_args *uap)
1446 if (flag & ~AT_SYMLINK_FOLLOW)
1449 return (kern_linkat(td, uap->fd1, uap->fd2, uap->path1, uap->path2,
1450 UIO_USERSPACE, (flag & AT_SYMLINK_FOLLOW) ? FOLLOW : NOFOLLOW));
1453 int hardlink_check_uid = 0;
1454 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
1455 &hardlink_check_uid, 0,
1456 "Unprivileged processes cannot create hard links to files owned by other "
1458 static int hardlink_check_gid = 0;
1459 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
1460 &hardlink_check_gid, 0,
1461 "Unprivileged processes cannot create hard links to files owned by other "
1465 can_hardlink(struct vnode *vp, struct ucred *cred)
1470 if (!hardlink_check_uid && !hardlink_check_gid)
1473 error = VOP_GETATTR(vp, &va, cred);
1477 if (hardlink_check_uid && cred->cr_uid != va.va_uid) {
1478 error = priv_check_cred(cred, PRIV_VFS_LINK, 0);
1483 if (hardlink_check_gid && !groupmember(va.va_gid, cred)) {
1484 error = priv_check_cred(cred, PRIV_VFS_LINK, 0);
1493 kern_linkat(struct thread *td, int fd1, int fd2, char *path1, char *path2,
1494 enum uio_seg segflg, int follow)
1498 struct nameidata nd;
1503 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, segflg, path1, fd1,
1504 &cap_linkat_source_rights, td);
1506 if ((error = namei(&nd)) != 0)
1508 NDFREE(&nd, NDF_ONLY_PNBUF);
1510 if (vp->v_type == VDIR) {
1512 return (EPERM); /* POSIX */
1514 NDINIT_ATRIGHTS(&nd, CREATE,
1515 LOCKPARENT | SAVENAME | AUDITVNODE2 | NOCACHE, segflg, path2, fd2,
1516 &cap_linkat_target_rights, td);
1517 if ((error = namei(&nd)) == 0) {
1518 if (nd.ni_vp != NULL) {
1519 NDFREE(&nd, NDF_ONLY_PNBUF);
1520 if (nd.ni_dvp == nd.ni_vp)
1527 } else if (nd.ni_dvp->v_mount != vp->v_mount) {
1529 * Cross-device link. No need to recheck
1530 * vp->v_type, since it cannot change, except
1533 NDFREE(&nd, NDF_ONLY_PNBUF);
1537 } else if ((error = vn_lock(vp, LK_EXCLUSIVE)) == 0) {
1538 error = can_hardlink(vp, td->td_ucred);
1541 error = mac_vnode_check_link(td->td_ucred,
1542 nd.ni_dvp, vp, &nd.ni_cnd);
1547 NDFREE(&nd, NDF_ONLY_PNBUF);
1550 error = vn_start_write(vp, &mp, V_NOWAIT);
1554 NDFREE(&nd, NDF_ONLY_PNBUF);
1555 error = vn_start_write(NULL, &mp,
1561 error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
1564 vn_finished_write(mp);
1565 NDFREE(&nd, NDF_ONLY_PNBUF);
1568 NDFREE(&nd, NDF_ONLY_PNBUF);
1578 * Make a symbolic link.
1580 #ifndef _SYS_SYSPROTO_H_
1581 struct symlink_args {
1587 sys_symlink(struct thread *td, struct symlink_args *uap)
1590 return (kern_symlinkat(td, uap->path, AT_FDCWD, uap->link,
1594 #ifndef _SYS_SYSPROTO_H_
1595 struct symlinkat_args {
1602 sys_symlinkat(struct thread *td, struct symlinkat_args *uap)
1605 return (kern_symlinkat(td, uap->path1, uap->fd, uap->path2,
1610 kern_symlinkat(struct thread *td, char *path1, int fd, char *path2,
1611 enum uio_seg segflg)
1616 struct nameidata nd;
1619 if (segflg == UIO_SYSSPACE) {
1622 syspath = uma_zalloc(namei_zone, M_WAITOK);
1623 if ((error = copyinstr(path1, syspath, MAXPATHLEN, NULL)) != 0)
1626 AUDIT_ARG_TEXT(syspath);
1629 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
1630 NOCACHE, segflg, path2, fd, &cap_symlinkat_rights,
1632 if ((error = namei(&nd)) != 0)
1635 NDFREE(&nd, NDF_ONLY_PNBUF);
1636 if (nd.ni_vp == nd.ni_dvp)
1644 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1645 NDFREE(&nd, NDF_ONLY_PNBUF);
1647 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1652 vattr.va_mode = ACCESSPERMS &~ td->td_proc->p_fd->fd_cmask;
1654 vattr.va_type = VLNK;
1655 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1660 error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr, syspath);
1666 NDFREE(&nd, NDF_ONLY_PNBUF);
1668 vn_finished_write(mp);
1670 if (segflg != UIO_SYSSPACE)
1671 uma_zfree(namei_zone, syspath);
1676 * Delete a whiteout from the filesystem.
1678 #ifndef _SYS_SYSPROTO_H_
1679 struct undelete_args {
1684 sys_undelete(struct thread *td, struct undelete_args *uap)
1687 struct nameidata nd;
1692 NDINIT(&nd, DELETE, LOCKPARENT | DOWHITEOUT | AUDITVNODE1,
1693 UIO_USERSPACE, uap->path, td);
1698 if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & ISWHITEOUT)) {
1699 NDFREE(&nd, NDF_ONLY_PNBUF);
1700 if (nd.ni_vp == nd.ni_dvp)
1708 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1709 NDFREE(&nd, NDF_ONLY_PNBUF);
1711 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1715 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, DELETE);
1716 NDFREE(&nd, NDF_ONLY_PNBUF);
1718 vn_finished_write(mp);
1723 * Delete a name from the filesystem.
1725 #ifndef _SYS_SYSPROTO_H_
1726 struct unlink_args {
1731 sys_unlink(struct thread *td, struct unlink_args *uap)
1734 return (kern_unlinkat(td, AT_FDCWD, uap->path, UIO_USERSPACE, 0));
1737 #ifndef _SYS_SYSPROTO_H_
1738 struct unlinkat_args {
1745 sys_unlinkat(struct thread *td, struct unlinkat_args *uap)
1747 int flag = uap->flag;
1749 char *path = uap->path;
1751 if (flag & ~AT_REMOVEDIR)
1754 if (flag & AT_REMOVEDIR)
1755 return (kern_rmdirat(td, fd, path, UIO_USERSPACE));
1757 return (kern_unlinkat(td, fd, path, UIO_USERSPACE, 0));
1761 kern_unlinkat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1766 struct nameidata nd;
1772 NDINIT_ATRIGHTS(&nd, DELETE, LOCKPARENT | LOCKLEAF | AUDITVNODE1,
1773 pathseg, path, fd, &cap_unlinkat_rights, td);
1774 if ((error = namei(&nd)) != 0)
1775 return (error == EINVAL ? EPERM : error);
1777 if (vp->v_type == VDIR && oldinum == 0) {
1778 error = EPERM; /* POSIX */
1779 } else if (oldinum != 0 &&
1780 ((error = vn_stat(vp, &sb, td->td_ucred, NOCRED, td)) == 0) &&
1781 sb.st_ino != oldinum) {
1782 error = EIDRM; /* Identifier removed */
1785 * The root of a mounted filesystem cannot be deleted.
1787 * XXX: can this only be a VDIR case?
1789 if (vp->v_vflag & VV_ROOT)
1793 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1794 NDFREE(&nd, NDF_ONLY_PNBUF);
1796 if (vp == nd.ni_dvp)
1800 if ((error = vn_start_write(NULL, &mp,
1801 V_XSLEEP | PCATCH)) != 0)
1806 error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp,
1811 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_UNLINK);
1812 error = VOP_REMOVE(nd.ni_dvp, vp, &nd.ni_cnd);
1816 vn_finished_write(mp);
1818 NDFREE(&nd, NDF_ONLY_PNBUF);
1820 if (vp == nd.ni_dvp)
1828 * Reposition read/write file offset.
1830 #ifndef _SYS_SYSPROTO_H_
1839 sys_lseek(struct thread *td, struct lseek_args *uap)
1842 return (kern_lseek(td, uap->fd, uap->offset, uap->whence));
1846 kern_lseek(struct thread *td, int fd, off_t offset, int whence)
1852 error = fget(td, fd, &cap_seek_rights, &fp);
1855 error = (fp->f_ops->fo_flags & DFLAG_SEEKABLE) != 0 ?
1856 fo_seek(fp, offset, whence, td) : ESPIPE;
1861 #if defined(COMPAT_43)
1863 * Reposition read/write file offset.
1865 #ifndef _SYS_SYSPROTO_H_
1866 struct olseek_args {
1873 olseek(struct thread *td, struct olseek_args *uap)
1876 return (kern_lseek(td, uap->fd, uap->offset, uap->whence));
1878 #endif /* COMPAT_43 */
1880 #if defined(COMPAT_FREEBSD6)
1881 /* Version with the 'pad' argument */
1883 freebsd6_lseek(struct thread *td, struct freebsd6_lseek_args *uap)
1886 return (kern_lseek(td, uap->fd, uap->offset, uap->whence));
1891 * Check access permissions using passed credentials.
1894 vn_access(struct vnode *vp, int user_flags, struct ucred *cred,
1900 /* Flags == 0 means only check for existence. */
1901 if (user_flags == 0)
1905 if (user_flags & R_OK)
1907 if (user_flags & W_OK)
1909 if (user_flags & X_OK)
1912 error = mac_vnode_check_access(cred, vp, accmode);
1916 if ((accmode & VWRITE) == 0 || (error = vn_writechk(vp)) == 0)
1917 error = VOP_ACCESS(vp, accmode, cred, td);
1922 * Check access permissions using "real" credentials.
1924 #ifndef _SYS_SYSPROTO_H_
1925 struct access_args {
1931 sys_access(struct thread *td, struct access_args *uap)
1934 return (kern_accessat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1938 #ifndef _SYS_SYSPROTO_H_
1939 struct faccessat_args {
1947 sys_faccessat(struct thread *td, struct faccessat_args *uap)
1950 return (kern_accessat(td, uap->fd, uap->path, UIO_USERSPACE, uap->flag,
1955 kern_accessat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1956 int flag, int amode)
1958 struct ucred *cred, *usecred;
1960 struct nameidata nd;
1963 if (flag & ~AT_EACCESS)
1965 if (amode != F_OK && (amode & ~(R_OK | W_OK | X_OK)) != 0)
1969 * Create and modify a temporary credential instead of one that
1970 * is potentially shared (if we need one).
1972 cred = td->td_ucred;
1973 if ((flag & AT_EACCESS) == 0 &&
1974 ((cred->cr_uid != cred->cr_ruid ||
1975 cred->cr_rgid != cred->cr_groups[0]))) {
1976 usecred = crdup(cred);
1977 usecred->cr_uid = cred->cr_ruid;
1978 usecred->cr_groups[0] = cred->cr_rgid;
1979 td->td_ucred = usecred;
1982 AUDIT_ARG_VALUE(amode);
1983 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF |
1984 AUDITVNODE1, pathseg, path, fd, &cap_fstat_rights,
1986 if ((error = namei(&nd)) != 0)
1990 error = vn_access(vp, amode, usecred, td);
1991 NDFREE(&nd, NDF_ONLY_PNBUF);
1994 if (usecred != cred) {
1995 td->td_ucred = cred;
2002 * Check access permissions using "effective" credentials.
2004 #ifndef _SYS_SYSPROTO_H_
2005 struct eaccess_args {
2011 sys_eaccess(struct thread *td, struct eaccess_args *uap)
2014 return (kern_accessat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2015 AT_EACCESS, uap->amode));
2018 #if defined(COMPAT_43)
2020 * Get file status; this version follows links.
2022 #ifndef _SYS_SYSPROTO_H_
2029 ostat(struct thread *td, struct ostat_args *uap)
2035 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
2040 return (copyout(&osb, uap->ub, sizeof (osb)));
2044 * Get file status; this version does not follow links.
2046 #ifndef _SYS_SYSPROTO_H_
2047 struct olstat_args {
2053 olstat(struct thread *td, struct olstat_args *uap)
2059 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2060 UIO_USERSPACE, &sb, NULL);
2064 return (copyout(&osb, uap->ub, sizeof (osb)));
2068 * Convert from an old to a new stat structure.
2069 * XXX: many values are blindly truncated.
2072 cvtstat(struct stat *st, struct ostat *ost)
2075 bzero(ost, sizeof(*ost));
2076 ost->st_dev = st->st_dev;
2077 ost->st_ino = st->st_ino;
2078 ost->st_mode = st->st_mode;
2079 ost->st_nlink = st->st_nlink;
2080 ost->st_uid = st->st_uid;
2081 ost->st_gid = st->st_gid;
2082 ost->st_rdev = st->st_rdev;
2083 ost->st_size = MIN(st->st_size, INT32_MAX);
2084 ost->st_atim = st->st_atim;
2085 ost->st_mtim = st->st_mtim;
2086 ost->st_ctim = st->st_ctim;
2087 ost->st_blksize = st->st_blksize;
2088 ost->st_blocks = st->st_blocks;
2089 ost->st_flags = st->st_flags;
2090 ost->st_gen = st->st_gen;
2092 #endif /* COMPAT_43 */
2094 #if defined(COMPAT_43) || defined(COMPAT_FREEBSD11)
2095 int ino64_trunc_error;
2096 SYSCTL_INT(_vfs, OID_AUTO, ino64_trunc_error, CTLFLAG_RW,
2097 &ino64_trunc_error, 0,
2098 "Error on truncation of device, file or inode number, or link count");
2101 freebsd11_cvtstat(struct stat *st, struct freebsd11_stat *ost)
2104 ost->st_dev = st->st_dev;
2105 if (ost->st_dev != st->st_dev) {
2106 switch (ino64_trunc_error) {
2109 * Since dev_t is almost raw, don't clamp to the
2110 * maximum for case 2, but ignore the error.
2117 ost->st_ino = st->st_ino;
2118 if (ost->st_ino != st->st_ino) {
2119 switch (ino64_trunc_error) {
2126 ost->st_ino = UINT32_MAX;
2130 ost->st_mode = st->st_mode;
2131 ost->st_nlink = st->st_nlink;
2132 if (ost->st_nlink != st->st_nlink) {
2133 switch (ino64_trunc_error) {
2140 ost->st_nlink = UINT16_MAX;
2144 ost->st_uid = st->st_uid;
2145 ost->st_gid = st->st_gid;
2146 ost->st_rdev = st->st_rdev;
2147 if (ost->st_rdev != st->st_rdev) {
2148 switch (ino64_trunc_error) {
2155 ost->st_atim = st->st_atim;
2156 ost->st_mtim = st->st_mtim;
2157 ost->st_ctim = st->st_ctim;
2158 ost->st_size = st->st_size;
2159 ost->st_blocks = st->st_blocks;
2160 ost->st_blksize = st->st_blksize;
2161 ost->st_flags = st->st_flags;
2162 ost->st_gen = st->st_gen;
2164 ost->st_birthtim = st->st_birthtim;
2165 bzero((char *)&ost->st_birthtim + sizeof(ost->st_birthtim),
2166 sizeof(*ost) - offsetof(struct freebsd11_stat,
2167 st_birthtim) - sizeof(ost->st_birthtim));
2172 freebsd11_stat(struct thread *td, struct freebsd11_stat_args* uap)
2175 struct freebsd11_stat osb;
2178 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
2182 error = freebsd11_cvtstat(&sb, &osb);
2184 error = copyout(&osb, uap->ub, sizeof(osb));
2189 freebsd11_lstat(struct thread *td, struct freebsd11_lstat_args* uap)
2192 struct freebsd11_stat osb;
2195 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2196 UIO_USERSPACE, &sb, NULL);
2199 error = freebsd11_cvtstat(&sb, &osb);
2201 error = copyout(&osb, uap->ub, sizeof(osb));
2206 freebsd11_fhstat(struct thread *td, struct freebsd11_fhstat_args* uap)
2210 struct freebsd11_stat osb;
2213 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
2216 error = kern_fhstat(td, fh, &sb);
2219 error = freebsd11_cvtstat(&sb, &osb);
2221 error = copyout(&osb, uap->sb, sizeof(osb));
2226 freebsd11_fstatat(struct thread *td, struct freebsd11_fstatat_args* uap)
2229 struct freebsd11_stat osb;
2232 error = kern_statat(td, uap->flag, uap->fd, uap->path,
2233 UIO_USERSPACE, &sb, NULL);
2236 error = freebsd11_cvtstat(&sb, &osb);
2238 error = copyout(&osb, uap->buf, sizeof(osb));
2241 #endif /* COMPAT_FREEBSD11 */
2246 #ifndef _SYS_SYSPROTO_H_
2247 struct fstatat_args {
2255 sys_fstatat(struct thread *td, struct fstatat_args *uap)
2260 error = kern_statat(td, uap->flag, uap->fd, uap->path,
2261 UIO_USERSPACE, &sb, NULL);
2263 error = copyout(&sb, uap->buf, sizeof (sb));
2268 kern_statat(struct thread *td, int flag, int fd, char *path,
2269 enum uio_seg pathseg, struct stat *sbp,
2270 void (*hook)(struct vnode *vp, struct stat *sbp))
2272 struct nameidata nd;
2276 if (flag & ~AT_SYMLINK_NOFOLLOW)
2279 NDINIT_ATRIGHTS(&nd, LOOKUP, ((flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW :
2280 FOLLOW) | LOCKSHARED | LOCKLEAF | AUDITVNODE1, pathseg, path, fd,
2281 &cap_fstat_rights, td);
2283 if ((error = namei(&nd)) != 0)
2285 error = vn_stat(nd.ni_vp, &sb, td->td_ucred, NOCRED, td);
2287 SDT_PROBE2(vfs, , stat, mode, path, sb.st_mode);
2288 if (S_ISREG(sb.st_mode))
2289 SDT_PROBE2(vfs, , stat, reg, path, pathseg);
2290 if (__predict_false(hook != NULL))
2291 hook(nd.ni_vp, &sb);
2293 NDFREE(&nd, NDF_ONLY_PNBUF);
2297 #ifdef __STAT_TIME_T_EXT
2305 if (KTRPOINT(td, KTR_STRUCT))
2311 #if defined(COMPAT_FREEBSD11)
2313 * Implementation of the NetBSD [l]stat() functions.
2316 freebsd11_cvtnstat(struct stat *sb, struct nstat *nsb)
2319 bzero(nsb, sizeof(*nsb));
2320 nsb->st_dev = sb->st_dev;
2321 nsb->st_ino = sb->st_ino;
2322 nsb->st_mode = sb->st_mode;
2323 nsb->st_nlink = sb->st_nlink;
2324 nsb->st_uid = sb->st_uid;
2325 nsb->st_gid = sb->st_gid;
2326 nsb->st_rdev = sb->st_rdev;
2327 nsb->st_atim = sb->st_atim;
2328 nsb->st_mtim = sb->st_mtim;
2329 nsb->st_ctim = sb->st_ctim;
2330 nsb->st_size = sb->st_size;
2331 nsb->st_blocks = sb->st_blocks;
2332 nsb->st_blksize = sb->st_blksize;
2333 nsb->st_flags = sb->st_flags;
2334 nsb->st_gen = sb->st_gen;
2335 nsb->st_birthtim = sb->st_birthtim;
2338 #ifndef _SYS_SYSPROTO_H_
2339 struct freebsd11_nstat_args {
2345 freebsd11_nstat(struct thread *td, struct freebsd11_nstat_args *uap)
2351 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
2355 freebsd11_cvtnstat(&sb, &nsb);
2356 return (copyout(&nsb, uap->ub, sizeof (nsb)));
2360 * NetBSD lstat. Get file status; this version does not follow links.
2362 #ifndef _SYS_SYSPROTO_H_
2363 struct freebsd11_nlstat_args {
2369 freebsd11_nlstat(struct thread *td, struct freebsd11_nlstat_args *uap)
2375 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2376 UIO_USERSPACE, &sb, NULL);
2379 freebsd11_cvtnstat(&sb, &nsb);
2380 return (copyout(&nsb, uap->ub, sizeof (nsb)));
2382 #endif /* COMPAT_FREEBSD11 */
2385 * Get configurable pathname variables.
2387 #ifndef _SYS_SYSPROTO_H_
2388 struct pathconf_args {
2394 sys_pathconf(struct thread *td, struct pathconf_args *uap)
2399 error = kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name, FOLLOW,
2402 td->td_retval[0] = value;
2406 #ifndef _SYS_SYSPROTO_H_
2407 struct lpathconf_args {
2413 sys_lpathconf(struct thread *td, struct lpathconf_args *uap)
2418 error = kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name,
2421 td->td_retval[0] = value;
2426 kern_pathconf(struct thread *td, char *path, enum uio_seg pathseg, int name,
2427 u_long flags, long *valuep)
2429 struct nameidata nd;
2432 NDINIT(&nd, LOOKUP, LOCKSHARED | LOCKLEAF | AUDITVNODE1 | flags,
2434 if ((error = namei(&nd)) != 0)
2436 NDFREE(&nd, NDF_ONLY_PNBUF);
2438 error = VOP_PATHCONF(nd.ni_vp, name, valuep);
2444 * Return target name of a symbolic link.
2446 #ifndef _SYS_SYSPROTO_H_
2447 struct readlink_args {
2454 sys_readlink(struct thread *td, struct readlink_args *uap)
2457 return (kern_readlinkat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2458 uap->buf, UIO_USERSPACE, uap->count));
2460 #ifndef _SYS_SYSPROTO_H_
2461 struct readlinkat_args {
2469 sys_readlinkat(struct thread *td, struct readlinkat_args *uap)
2472 return (kern_readlinkat(td, uap->fd, uap->path, UIO_USERSPACE,
2473 uap->buf, UIO_USERSPACE, uap->bufsize));
2477 kern_readlinkat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2478 char *buf, enum uio_seg bufseg, size_t count)
2483 struct nameidata nd;
2486 if (count > IOSIZE_MAX)
2489 NDINIT_AT(&nd, LOOKUP, NOFOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
2490 pathseg, path, fd, td);
2492 if ((error = namei(&nd)) != 0)
2494 NDFREE(&nd, NDF_ONLY_PNBUF);
2497 error = mac_vnode_check_readlink(td->td_ucred, vp);
2503 if (vp->v_type != VLNK && (vp->v_vflag & VV_READLINK) == 0)
2506 aiov.iov_base = buf;
2507 aiov.iov_len = count;
2508 auio.uio_iov = &aiov;
2509 auio.uio_iovcnt = 1;
2510 auio.uio_offset = 0;
2511 auio.uio_rw = UIO_READ;
2512 auio.uio_segflg = bufseg;
2514 auio.uio_resid = count;
2515 error = VOP_READLINK(vp, &auio, td->td_ucred);
2516 td->td_retval[0] = count - auio.uio_resid;
2523 * Common implementation code for chflags() and fchflags().
2526 setfflags(struct thread *td, struct vnode *vp, u_long flags)
2532 /* We can't support the value matching VNOVAL. */
2533 if (flags == VNOVAL)
2534 return (EOPNOTSUPP);
2537 * Prevent non-root users from setting flags on devices. When
2538 * a device is reused, users can retain ownership of the device
2539 * if they are allowed to set flags and programs assume that
2540 * chown can't fail when done as root.
2542 if (vp->v_type == VCHR || vp->v_type == VBLK) {
2543 error = priv_check(td, PRIV_VFS_CHFLAGS_DEV);
2548 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2551 vattr.va_flags = flags;
2552 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2554 error = mac_vnode_check_setflags(td->td_ucred, vp, vattr.va_flags);
2557 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
2559 vn_finished_write(mp);
2564 * Change flags of a file given a path name.
2566 #ifndef _SYS_SYSPROTO_H_
2567 struct chflags_args {
2573 sys_chflags(struct thread *td, struct chflags_args *uap)
2576 return (kern_chflagsat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2580 #ifndef _SYS_SYSPROTO_H_
2581 struct chflagsat_args {
2589 sys_chflagsat(struct thread *td, struct chflagsat_args *uap)
2592 const char *path = uap->path;
2593 u_long flags = uap->flags;
2594 int atflag = uap->atflag;
2596 if (atflag & ~AT_SYMLINK_NOFOLLOW)
2599 return (kern_chflagsat(td, fd, path, UIO_USERSPACE, flags, atflag));
2603 * Same as chflags() but doesn't follow symlinks.
2605 #ifndef _SYS_SYSPROTO_H_
2606 struct lchflags_args {
2612 sys_lchflags(struct thread *td, struct lchflags_args *uap)
2615 return (kern_chflagsat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2616 uap->flags, AT_SYMLINK_NOFOLLOW));
2620 kern_chflagsat(struct thread *td, int fd, const char *path,
2621 enum uio_seg pathseg, u_long flags, int atflag)
2623 struct nameidata nd;
2626 AUDIT_ARG_FFLAGS(flags);
2627 follow = (atflag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
2628 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, pathseg, path, fd,
2629 &cap_fchflags_rights, td);
2630 if ((error = namei(&nd)) != 0)
2632 NDFREE(&nd, NDF_ONLY_PNBUF);
2633 error = setfflags(td, nd.ni_vp, flags);
2639 * Change flags of a file given a file descriptor.
2641 #ifndef _SYS_SYSPROTO_H_
2642 struct fchflags_args {
2648 sys_fchflags(struct thread *td, struct fchflags_args *uap)
2653 AUDIT_ARG_FD(uap->fd);
2654 AUDIT_ARG_FFLAGS(uap->flags);
2655 error = getvnode(td, uap->fd, &cap_fchflags_rights,
2660 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
2661 AUDIT_ARG_VNODE1(fp->f_vnode);
2662 VOP_UNLOCK(fp->f_vnode, 0);
2664 error = setfflags(td, fp->f_vnode, uap->flags);
2670 * Common implementation code for chmod(), lchmod() and fchmod().
2673 setfmode(struct thread *td, struct ucred *cred, struct vnode *vp, int mode)
2679 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2681 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2683 vattr.va_mode = mode & ALLPERMS;
2685 error = mac_vnode_check_setmode(cred, vp, vattr.va_mode);
2688 error = VOP_SETATTR(vp, &vattr, cred);
2690 vn_finished_write(mp);
2695 * Change mode of a file given path name.
2697 #ifndef _SYS_SYSPROTO_H_
2704 sys_chmod(struct thread *td, struct chmod_args *uap)
2707 return (kern_fchmodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2711 #ifndef _SYS_SYSPROTO_H_
2712 struct fchmodat_args {
2720 sys_fchmodat(struct thread *td, struct fchmodat_args *uap)
2722 int flag = uap->flag;
2724 char *path = uap->path;
2725 mode_t mode = uap->mode;
2727 if (flag & ~AT_SYMLINK_NOFOLLOW)
2730 return (kern_fchmodat(td, fd, path, UIO_USERSPACE, mode, flag));
2734 * Change mode of a file given path name (don't follow links.)
2736 #ifndef _SYS_SYSPROTO_H_
2737 struct lchmod_args {
2743 sys_lchmod(struct thread *td, struct lchmod_args *uap)
2746 return (kern_fchmodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2747 uap->mode, AT_SYMLINK_NOFOLLOW));
2751 kern_fchmodat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2752 mode_t mode, int flag)
2754 struct nameidata nd;
2757 AUDIT_ARG_MODE(mode);
2758 follow = (flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
2759 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, pathseg, path, fd,
2760 &cap_fchmod_rights, td);
2761 if ((error = namei(&nd)) != 0)
2763 NDFREE(&nd, NDF_ONLY_PNBUF);
2764 error = setfmode(td, td->td_ucred, nd.ni_vp, mode);
2770 * Change mode of a file given a file descriptor.
2772 #ifndef _SYS_SYSPROTO_H_
2773 struct fchmod_args {
2779 sys_fchmod(struct thread *td, struct fchmod_args *uap)
2784 AUDIT_ARG_FD(uap->fd);
2785 AUDIT_ARG_MODE(uap->mode);
2787 error = fget(td, uap->fd, &cap_fchmod_rights, &fp);
2790 error = fo_chmod(fp, uap->mode, td->td_ucred, td);
2796 * Common implementation for chown(), lchown(), and fchown()
2799 setfown(struct thread *td, struct ucred *cred, struct vnode *vp, uid_t uid,
2806 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2808 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2813 error = mac_vnode_check_setowner(cred, vp, vattr.va_uid,
2817 error = VOP_SETATTR(vp, &vattr, cred);
2819 vn_finished_write(mp);
2824 * Set ownership given a path name.
2826 #ifndef _SYS_SYSPROTO_H_
2834 sys_chown(struct thread *td, struct chown_args *uap)
2837 return (kern_fchownat(td, AT_FDCWD, uap->path, UIO_USERSPACE, uap->uid,
2841 #ifndef _SYS_SYSPROTO_H_
2842 struct fchownat_args {
2851 sys_fchownat(struct thread *td, struct fchownat_args *uap)
2856 if (flag & ~AT_SYMLINK_NOFOLLOW)
2859 return (kern_fchownat(td, uap->fd, uap->path, UIO_USERSPACE, uap->uid,
2860 uap->gid, uap->flag));
2864 kern_fchownat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2865 int uid, int gid, int flag)
2867 struct nameidata nd;
2870 AUDIT_ARG_OWNER(uid, gid);
2871 follow = (flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
2872 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, pathseg, path, fd,
2873 &cap_fchown_rights, td);
2875 if ((error = namei(&nd)) != 0)
2877 NDFREE(&nd, NDF_ONLY_PNBUF);
2878 error = setfown(td, td->td_ucred, nd.ni_vp, uid, gid);
2884 * Set ownership given a path name, do not cross symlinks.
2886 #ifndef _SYS_SYSPROTO_H_
2887 struct lchown_args {
2894 sys_lchown(struct thread *td, struct lchown_args *uap)
2897 return (kern_fchownat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2898 uap->uid, uap->gid, AT_SYMLINK_NOFOLLOW));
2902 * Set ownership given a file descriptor.
2904 #ifndef _SYS_SYSPROTO_H_
2905 struct fchown_args {
2912 sys_fchown(struct thread *td, struct fchown_args *uap)
2917 AUDIT_ARG_FD(uap->fd);
2918 AUDIT_ARG_OWNER(uap->uid, uap->gid);
2919 error = fget(td, uap->fd, &cap_fchown_rights, &fp);
2922 error = fo_chown(fp, uap->uid, uap->gid, td->td_ucred, td);
2928 * Common implementation code for utimes(), lutimes(), and futimes().
2931 getutimes(const struct timeval *usrtvp, enum uio_seg tvpseg,
2932 struct timespec *tsp)
2934 struct timeval tv[2];
2935 const struct timeval *tvp;
2938 if (usrtvp == NULL) {
2939 vfs_timestamp(&tsp[0]);
2942 if (tvpseg == UIO_SYSSPACE) {
2945 if ((error = copyin(usrtvp, tv, sizeof(tv))) != 0)
2950 if (tvp[0].tv_usec < 0 || tvp[0].tv_usec >= 1000000 ||
2951 tvp[1].tv_usec < 0 || tvp[1].tv_usec >= 1000000)
2953 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
2954 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
2960 * Common implementation code for futimens(), utimensat().
2962 #define UTIMENS_NULL 0x1
2963 #define UTIMENS_EXIT 0x2
2965 getutimens(const struct timespec *usrtsp, enum uio_seg tspseg,
2966 struct timespec *tsp, int *retflags)
2968 struct timespec tsnow;
2971 vfs_timestamp(&tsnow);
2973 if (usrtsp == NULL) {
2976 *retflags |= UTIMENS_NULL;
2979 if (tspseg == UIO_SYSSPACE) {
2982 } else if ((error = copyin(usrtsp, tsp, sizeof(*tsp) * 2)) != 0)
2984 if (tsp[0].tv_nsec == UTIME_OMIT && tsp[1].tv_nsec == UTIME_OMIT)
2985 *retflags |= UTIMENS_EXIT;
2986 if (tsp[0].tv_nsec == UTIME_NOW && tsp[1].tv_nsec == UTIME_NOW)
2987 *retflags |= UTIMENS_NULL;
2988 if (tsp[0].tv_nsec == UTIME_OMIT)
2989 tsp[0].tv_sec = VNOVAL;
2990 else if (tsp[0].tv_nsec == UTIME_NOW)
2992 else if (tsp[0].tv_nsec < 0 || tsp[0].tv_nsec >= 1000000000L)
2994 if (tsp[1].tv_nsec == UTIME_OMIT)
2995 tsp[1].tv_sec = VNOVAL;
2996 else if (tsp[1].tv_nsec == UTIME_NOW)
2998 else if (tsp[1].tv_nsec < 0 || tsp[1].tv_nsec >= 1000000000L)
3005 * Common implementation code for utimes(), lutimes(), futimes(), futimens(),
3009 setutimes(struct thread *td, struct vnode *vp, const struct timespec *ts,
3010 int numtimes, int nullflag)
3014 int error, setbirthtime;
3016 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
3018 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3020 if (numtimes < 3 && !VOP_GETATTR(vp, &vattr, td->td_ucred) &&
3021 timespeccmp(&ts[1], &vattr.va_birthtime, < ))
3024 vattr.va_atime = ts[0];
3025 vattr.va_mtime = ts[1];
3027 vattr.va_birthtime = ts[1];
3029 vattr.va_birthtime = ts[2];
3031 vattr.va_vaflags |= VA_UTIMES_NULL;
3033 error = mac_vnode_check_setutimes(td->td_ucred, vp, vattr.va_atime,
3037 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3039 vn_finished_write(mp);
3044 * Set the access and modification times of a file.
3046 #ifndef _SYS_SYSPROTO_H_
3047 struct utimes_args {
3049 struct timeval *tptr;
3053 sys_utimes(struct thread *td, struct utimes_args *uap)
3056 return (kern_utimesat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
3057 uap->tptr, UIO_USERSPACE));
3060 #ifndef _SYS_SYSPROTO_H_
3061 struct futimesat_args {
3064 const struct timeval * times;
3068 sys_futimesat(struct thread *td, struct futimesat_args *uap)
3071 return (kern_utimesat(td, uap->fd, uap->path, UIO_USERSPACE,
3072 uap->times, UIO_USERSPACE));
3076 kern_utimesat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
3077 struct timeval *tptr, enum uio_seg tptrseg)
3079 struct nameidata nd;
3080 struct timespec ts[2];
3083 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3085 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, fd,
3086 &cap_futimes_rights, td);
3088 if ((error = namei(&nd)) != 0)
3090 NDFREE(&nd, NDF_ONLY_PNBUF);
3091 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3097 * Set the access and modification times of a file.
3099 #ifndef _SYS_SYSPROTO_H_
3100 struct lutimes_args {
3102 struct timeval *tptr;
3106 sys_lutimes(struct thread *td, struct lutimes_args *uap)
3109 return (kern_lutimes(td, uap->path, UIO_USERSPACE, uap->tptr,
3114 kern_lutimes(struct thread *td, char *path, enum uio_seg pathseg,
3115 struct timeval *tptr, enum uio_seg tptrseg)
3117 struct timespec ts[2];
3118 struct nameidata nd;
3121 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3123 NDINIT(&nd, LOOKUP, NOFOLLOW | AUDITVNODE1, pathseg, path, td);
3124 if ((error = namei(&nd)) != 0)
3126 NDFREE(&nd, NDF_ONLY_PNBUF);
3127 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3133 * Set the access and modification times of a file.
3135 #ifndef _SYS_SYSPROTO_H_
3136 struct futimes_args {
3138 struct timeval *tptr;
3142 sys_futimes(struct thread *td, struct futimes_args *uap)
3145 return (kern_futimes(td, uap->fd, uap->tptr, UIO_USERSPACE));
3149 kern_futimes(struct thread *td, int fd, struct timeval *tptr,
3150 enum uio_seg tptrseg)
3152 struct timespec ts[2];
3157 error = getutimes(tptr, tptrseg, ts);
3160 error = getvnode(td, fd, &cap_futimes_rights, &fp);
3164 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
3165 AUDIT_ARG_VNODE1(fp->f_vnode);
3166 VOP_UNLOCK(fp->f_vnode, 0);
3168 error = setutimes(td, fp->f_vnode, ts, 2, tptr == NULL);
3174 sys_futimens(struct thread *td, struct futimens_args *uap)
3177 return (kern_futimens(td, uap->fd, uap->times, UIO_USERSPACE));
3181 kern_futimens(struct thread *td, int fd, struct timespec *tptr,
3182 enum uio_seg tptrseg)
3184 struct timespec ts[2];
3189 error = getutimens(tptr, tptrseg, ts, &flags);
3192 if (flags & UTIMENS_EXIT)
3194 error = getvnode(td, fd, &cap_futimes_rights, &fp);
3198 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
3199 AUDIT_ARG_VNODE1(fp->f_vnode);
3200 VOP_UNLOCK(fp->f_vnode, 0);
3202 error = setutimes(td, fp->f_vnode, ts, 2, flags & UTIMENS_NULL);
3208 sys_utimensat(struct thread *td, struct utimensat_args *uap)
3211 return (kern_utimensat(td, uap->fd, uap->path, UIO_USERSPACE,
3212 uap->times, UIO_USERSPACE, uap->flag));
3216 kern_utimensat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
3217 struct timespec *tptr, enum uio_seg tptrseg, int flag)
3219 struct nameidata nd;
3220 struct timespec ts[2];
3223 if (flag & ~AT_SYMLINK_NOFOLLOW)
3226 if ((error = getutimens(tptr, tptrseg, ts, &flags)) != 0)
3228 NDINIT_ATRIGHTS(&nd, LOOKUP, ((flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW :
3229 FOLLOW) | AUDITVNODE1, pathseg, path, fd,
3230 &cap_futimes_rights, td);
3231 if ((error = namei(&nd)) != 0)
3234 * We are allowed to call namei() regardless of 2xUTIME_OMIT.
3236 * "If both tv_nsec fields are UTIME_OMIT... EACCESS may be detected."
3237 * "Search permission is denied by a component of the path prefix."
3239 NDFREE(&nd, NDF_ONLY_PNBUF);
3240 if ((flags & UTIMENS_EXIT) == 0)
3241 error = setutimes(td, nd.ni_vp, ts, 2, flags & UTIMENS_NULL);
3247 * Truncate a file given its path name.
3249 #ifndef _SYS_SYSPROTO_H_
3250 struct truncate_args {
3257 sys_truncate(struct thread *td, struct truncate_args *uap)
3260 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3264 kern_truncate(struct thread *td, char *path, enum uio_seg pathseg, off_t length)
3270 struct nameidata nd;
3275 NDINIT(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, td);
3276 if ((error = namei(&nd)) != 0)
3279 rl_cookie = vn_rangelock_wlock(vp, 0, OFF_MAX);
3280 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0) {
3281 vn_rangelock_unlock(vp, rl_cookie);
3285 NDFREE(&nd, NDF_ONLY_PNBUF);
3286 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3287 if (vp->v_type == VDIR)
3290 else if ((error = mac_vnode_check_write(td->td_ucred, NOCRED, vp))) {
3293 else if ((error = vn_writechk(vp)) == 0 &&
3294 (error = VOP_ACCESS(vp, VWRITE, td->td_ucred, td)) == 0) {
3296 vattr.va_size = length;
3297 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3300 vn_finished_write(mp);
3301 vn_rangelock_unlock(vp, rl_cookie);
3306 #if defined(COMPAT_43)
3308 * Truncate a file given its path name.
3310 #ifndef _SYS_SYSPROTO_H_
3311 struct otruncate_args {
3317 otruncate(struct thread *td, struct otruncate_args *uap)
3320 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3322 #endif /* COMPAT_43 */
3324 #if defined(COMPAT_FREEBSD6)
3325 /* Versions with the pad argument */
3327 freebsd6_truncate(struct thread *td, struct freebsd6_truncate_args *uap)
3330 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3334 freebsd6_ftruncate(struct thread *td, struct freebsd6_ftruncate_args *uap)
3337 return (kern_ftruncate(td, uap->fd, uap->length));
3342 kern_fsync(struct thread *td, int fd, bool fullsync)
3347 int error, lock_flags;
3350 error = getvnode(td, fd, &cap_fsync_rights, &fp);
3356 /* XXXKIB: compete outstanding aio writes */;
3358 error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
3361 if (MNT_SHARED_WRITES(mp) ||
3362 ((mp == NULL) && MNT_SHARED_WRITES(vp->v_mount))) {
3363 lock_flags = LK_SHARED;
3365 lock_flags = LK_EXCLUSIVE;
3367 vn_lock(vp, lock_flags | LK_RETRY);
3368 AUDIT_ARG_VNODE1(vp);
3369 if (vp->v_object != NULL) {
3370 VM_OBJECT_WLOCK(vp->v_object);
3371 vm_object_page_clean(vp->v_object, 0, 0, 0);
3372 VM_OBJECT_WUNLOCK(vp->v_object);
3374 error = fullsync ? VOP_FSYNC(vp, MNT_WAIT, td) : VOP_FDATASYNC(vp, td);
3376 vn_finished_write(mp);
3383 * Sync an open file.
3385 #ifndef _SYS_SYSPROTO_H_
3391 sys_fsync(struct thread *td, struct fsync_args *uap)
3394 return (kern_fsync(td, uap->fd, true));
3398 sys_fdatasync(struct thread *td, struct fdatasync_args *uap)
3401 return (kern_fsync(td, uap->fd, false));
3405 * Rename files. Source and destination must either both be directories, or
3406 * both not be directories. If target is a directory, it must be empty.
3408 #ifndef _SYS_SYSPROTO_H_
3409 struct rename_args {
3415 sys_rename(struct thread *td, struct rename_args *uap)
3418 return (kern_renameat(td, AT_FDCWD, uap->from, AT_FDCWD,
3419 uap->to, UIO_USERSPACE));
3422 #ifndef _SYS_SYSPROTO_H_
3423 struct renameat_args {
3431 sys_renameat(struct thread *td, struct renameat_args *uap)
3434 return (kern_renameat(td, uap->oldfd, uap->old, uap->newfd, uap->new,
3439 kern_renameat(struct thread *td, int oldfd, char *old, int newfd, char *new,
3440 enum uio_seg pathseg)
3442 struct mount *mp = NULL;
3443 struct vnode *tvp, *fvp, *tdvp;
3444 struct nameidata fromnd, tond;
3450 NDINIT_ATRIGHTS(&fromnd, DELETE, LOCKPARENT | LOCKLEAF | SAVESTART |
3451 AUDITVNODE1, pathseg, old, oldfd,
3452 &cap_renameat_source_rights, td);
3454 NDINIT_ATRIGHTS(&fromnd, DELETE, WANTPARENT | SAVESTART | AUDITVNODE1,
3455 pathseg, old, oldfd,
3456 &cap_renameat_source_rights, td);
3459 if ((error = namei(&fromnd)) != 0)
3462 error = mac_vnode_check_rename_from(td->td_ucred, fromnd.ni_dvp,
3463 fromnd.ni_vp, &fromnd.ni_cnd);
3464 VOP_UNLOCK(fromnd.ni_dvp, 0);
3465 if (fromnd.ni_dvp != fromnd.ni_vp)
3466 VOP_UNLOCK(fromnd.ni_vp, 0);
3469 NDINIT_ATRIGHTS(&tond, RENAME, LOCKPARENT | LOCKLEAF | NOCACHE |
3470 SAVESTART | AUDITVNODE2, pathseg, new, newfd,
3471 &cap_renameat_target_rights, td);
3472 if (fromnd.ni_vp->v_type == VDIR)
3473 tond.ni_cnd.cn_flags |= WILLBEDIR;
3474 if ((error = namei(&tond)) != 0) {
3475 /* Translate error code for rename("dir1", "dir2/."). */
3476 if (error == EISDIR && fvp->v_type == VDIR)
3478 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3479 vrele(fromnd.ni_dvp);
3485 error = vn_start_write(fvp, &mp, V_NOWAIT);
3487 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3488 NDFREE(&tond, NDF_ONLY_PNBUF);
3495 vrele(fromnd.ni_dvp);
3497 vrele(tond.ni_startdir);
3498 if (fromnd.ni_startdir != NULL)
3499 vrele(fromnd.ni_startdir);
3500 error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH);
3506 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
3509 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
3514 if (newfd != AT_FDCWD) {
3516 * If the target already exists we require CAP_UNLINKAT
3519 error = cap_check(&tond.ni_filecaps.fc_rights,
3520 &cap_unlinkat_rights);
3531 * If the source is the same as the destination (that is, if they
3532 * are links to the same vnode), then there is nothing to do.
3538 error = mac_vnode_check_rename_to(td->td_ucred, tdvp,
3539 tond.ni_vp, fromnd.ni_dvp == tdvp, &tond.ni_cnd);
3543 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
3544 tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
3545 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3546 NDFREE(&tond, NDF_ONLY_PNBUF);
3548 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3549 NDFREE(&tond, NDF_ONLY_PNBUF);
3556 vrele(fromnd.ni_dvp);
3559 vrele(tond.ni_startdir);
3560 vn_finished_write(mp);
3562 if (fromnd.ni_startdir)
3563 vrele(fromnd.ni_startdir);
3570 * Make a directory file.
3572 #ifndef _SYS_SYSPROTO_H_
3579 sys_mkdir(struct thread *td, struct mkdir_args *uap)
3582 return (kern_mkdirat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
3586 #ifndef _SYS_SYSPROTO_H_
3587 struct mkdirat_args {
3594 sys_mkdirat(struct thread *td, struct mkdirat_args *uap)
3597 return (kern_mkdirat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode));
3601 kern_mkdirat(struct thread *td, int fd, char *path, enum uio_seg segflg,
3607 struct nameidata nd;
3610 AUDIT_ARG_MODE(mode);
3613 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
3614 NOCACHE, segflg, path, fd, &cap_mkdirat_rights,
3616 nd.ni_cnd.cn_flags |= WILLBEDIR;
3617 if ((error = namei(&nd)) != 0)
3621 NDFREE(&nd, NDF_ONLY_PNBUF);
3623 * XXX namei called with LOCKPARENT but not LOCKLEAF has
3624 * the strange behaviour of leaving the vnode unlocked
3625 * if the target is the same vnode as the parent.
3627 if (vp == nd.ni_dvp)
3634 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3635 NDFREE(&nd, NDF_ONLY_PNBUF);
3637 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3642 vattr.va_type = VDIR;
3643 vattr.va_mode = (mode & ACCESSPERMS) &~ td->td_proc->p_fd->fd_cmask;
3645 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
3650 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
3654 NDFREE(&nd, NDF_ONLY_PNBUF);
3658 vn_finished_write(mp);
3663 * Remove a directory file.
3665 #ifndef _SYS_SYSPROTO_H_
3671 sys_rmdir(struct thread *td, struct rmdir_args *uap)
3674 return (kern_rmdirat(td, AT_FDCWD, uap->path, UIO_USERSPACE));
3678 kern_rmdirat(struct thread *td, int fd, char *path, enum uio_seg pathseg)
3682 struct nameidata nd;
3687 NDINIT_ATRIGHTS(&nd, DELETE, LOCKPARENT | LOCKLEAF | AUDITVNODE1,
3688 pathseg, path, fd, &cap_unlinkat_rights, td);
3689 if ((error = namei(&nd)) != 0)
3692 if (vp->v_type != VDIR) {
3697 * No rmdir "." please.
3699 if (nd.ni_dvp == vp) {
3704 * The root of a mounted filesystem cannot be deleted.
3706 if (vp->v_vflag & VV_ROOT) {
3711 error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp,
3716 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3717 NDFREE(&nd, NDF_ONLY_PNBUF);
3719 if (nd.ni_dvp == vp)
3723 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3727 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_UNLINK);
3728 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
3729 vn_finished_write(mp);
3731 NDFREE(&nd, NDF_ONLY_PNBUF);
3733 if (nd.ni_dvp == vp)
3740 #if defined(COMPAT_43) || defined(COMPAT_FREEBSD11)
3742 freebsd11_kern_getdirentries(struct thread *td, int fd, char *ubuf, u_int count,
3743 long *basep, void (*func)(struct freebsd11_dirent *))
3745 struct freebsd11_dirent dstdp;
3746 struct dirent *dp, *edp;
3749 ssize_t resid, ucount;
3752 /* XXX arbitrary sanity limit on `count'. */
3753 count = min(count, 64 * 1024);
3755 dirbuf = malloc(count, M_TEMP, M_WAITOK);
3757 error = kern_getdirentries(td, fd, dirbuf, count, &base, &resid,
3765 for (dp = (struct dirent *)dirbuf,
3766 edp = (struct dirent *)&dirbuf[count - resid];
3767 ucount < count && dp < edp; ) {
3768 if (dp->d_reclen == 0)
3770 MPASS(dp->d_reclen >= _GENERIC_DIRLEN(0));
3771 if (dp->d_namlen >= sizeof(dstdp.d_name))
3773 dstdp.d_type = dp->d_type;
3774 dstdp.d_namlen = dp->d_namlen;
3775 dstdp.d_fileno = dp->d_fileno; /* truncate */
3776 if (dstdp.d_fileno != dp->d_fileno) {
3777 switch (ino64_trunc_error) {
3785 dstdp.d_fileno = UINT32_MAX;
3789 dstdp.d_reclen = sizeof(dstdp) - sizeof(dstdp.d_name) +
3790 ((dp->d_namlen + 1 + 3) &~ 3);
3791 bcopy(dp->d_name, dstdp.d_name, dstdp.d_namlen);
3792 bzero(dstdp.d_name + dstdp.d_namlen,
3793 dstdp.d_reclen - offsetof(struct freebsd11_dirent, d_name) -
3795 MPASS(dstdp.d_reclen <= dp->d_reclen);
3796 MPASS(ucount + dstdp.d_reclen <= count);
3799 error = copyout(&dstdp, ubuf + ucount, dstdp.d_reclen);
3802 dp = (struct dirent *)((char *)dp + dp->d_reclen);
3803 ucount += dstdp.d_reclen;
3807 free(dirbuf, M_TEMP);
3809 td->td_retval[0] = ucount;
3816 ogetdirentries_cvt(struct freebsd11_dirent *dp)
3818 #if (BYTE_ORDER == LITTLE_ENDIAN)
3820 * The expected low byte of dp->d_namlen is our dp->d_type.
3821 * The high MBZ byte of dp->d_namlen is our dp->d_namlen.
3823 dp->d_type = dp->d_namlen;
3827 * The dp->d_type is the high byte of the expected dp->d_namlen,
3828 * so must be zero'ed.
3835 * Read a block of directory entries in a filesystem independent format.
3837 #ifndef _SYS_SYSPROTO_H_
3838 struct ogetdirentries_args {
3846 ogetdirentries(struct thread *td, struct ogetdirentries_args *uap)
3851 error = kern_ogetdirentries(td, uap, &loff);
3853 error = copyout(&loff, uap->basep, sizeof(long));
3858 kern_ogetdirentries(struct thread *td, struct ogetdirentries_args *uap,
3864 /* XXX arbitrary sanity limit on `count'. */
3865 if (uap->count > 64 * 1024)
3868 error = freebsd11_kern_getdirentries(td, uap->fd, uap->buf, uap->count,
3869 &base, ogetdirentries_cvt);
3871 if (error == 0 && uap->basep != NULL)
3872 error = copyout(&base, uap->basep, sizeof(long));
3876 #endif /* COMPAT_43 */
3878 #if defined(COMPAT_FREEBSD11)
3879 #ifndef _SYS_SYSPROTO_H_
3880 struct freebsd11_getdirentries_args {
3888 freebsd11_getdirentries(struct thread *td,
3889 struct freebsd11_getdirentries_args *uap)
3894 error = freebsd11_kern_getdirentries(td, uap->fd, uap->buf, uap->count,
3897 if (error == 0 && uap->basep != NULL)
3898 error = copyout(&base, uap->basep, sizeof(long));
3903 freebsd11_getdents(struct thread *td, struct freebsd11_getdents_args *uap)
3905 struct freebsd11_getdirentries_args ap;
3909 ap.count = uap->count;
3911 return (freebsd11_getdirentries(td, &ap));
3913 #endif /* COMPAT_FREEBSD11 */
3916 * Read a block of directory entries in a filesystem independent format.
3919 sys_getdirentries(struct thread *td, struct getdirentries_args *uap)
3924 error = kern_getdirentries(td, uap->fd, uap->buf, uap->count, &base,
3925 NULL, UIO_USERSPACE);
3928 if (uap->basep != NULL)
3929 error = copyout(&base, uap->basep, sizeof(off_t));
3934 kern_getdirentries(struct thread *td, int fd, char *buf, size_t count,
3935 off_t *basep, ssize_t *residp, enum uio_seg bufseg)
3946 if (count > IOSIZE_MAX)
3948 auio.uio_resid = count;
3949 error = getvnode(td, fd, &cap_read_rights, &fp);
3952 if ((fp->f_flag & FREAD) == 0) {
3957 foffset = foffset_lock(fp, 0);
3959 if (vp->v_type != VDIR) {
3963 aiov.iov_base = buf;
3964 aiov.iov_len = count;
3965 auio.uio_iov = &aiov;
3966 auio.uio_iovcnt = 1;
3967 auio.uio_rw = UIO_READ;
3968 auio.uio_segflg = bufseg;
3970 vn_lock(vp, LK_SHARED | LK_RETRY);
3971 AUDIT_ARG_VNODE1(vp);
3972 loff = auio.uio_offset = foffset;
3974 error = mac_vnode_check_readdir(td->td_ucred, vp);
3977 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL,
3979 foffset = auio.uio_offset;
3984 if (count == auio.uio_resid &&
3985 (vp->v_vflag & VV_ROOT) &&
3986 (vp->v_mount->mnt_flag & MNT_UNION)) {
3987 struct vnode *tvp = vp;
3989 vp = vp->v_mount->mnt_vnodecovered;
4000 *residp = auio.uio_resid;
4001 td->td_retval[0] = count - auio.uio_resid;
4003 foffset_unlock(fp, foffset, 0);
4009 * Set the mode mask for creation of filesystem nodes.
4011 #ifndef _SYS_SYSPROTO_H_
4017 sys_umask(struct thread *td, struct umask_args *uap)
4019 struct filedesc *fdp;
4021 fdp = td->td_proc->p_fd;
4022 FILEDESC_XLOCK(fdp);
4023 td->td_retval[0] = fdp->fd_cmask;
4024 fdp->fd_cmask = uap->newmask & ALLPERMS;
4025 FILEDESC_XUNLOCK(fdp);
4030 * Void all references to file by ripping underlying filesystem away from
4033 #ifndef _SYS_SYSPROTO_H_
4034 struct revoke_args {
4039 sys_revoke(struct thread *td, struct revoke_args *uap)
4043 struct nameidata nd;
4046 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
4048 if ((error = namei(&nd)) != 0)
4051 NDFREE(&nd, NDF_ONLY_PNBUF);
4052 if (vp->v_type != VCHR || vp->v_rdev == NULL) {
4057 error = mac_vnode_check_revoke(td->td_ucred, vp);
4061 error = VOP_GETATTR(vp, &vattr, td->td_ucred);
4064 if (td->td_ucred->cr_uid != vattr.va_uid) {
4065 error = priv_check(td, PRIV_VFS_ADMIN);
4070 VOP_REVOKE(vp, REVOKEALL);
4077 * Convert a user file descriptor to a kernel file entry and check that, if it
4078 * is a capability, the correct rights are present. A reference on the file
4079 * entry is held upon returning.
4082 getvnode(struct thread *td, int fd, cap_rights_t *rightsp, struct file **fpp)
4087 error = fget_unlocked(td->td_proc->p_fd, fd, rightsp, &fp, NULL);
4092 * The file could be not of the vnode type, or it may be not
4093 * yet fully initialized, in which case the f_vnode pointer
4094 * may be set, but f_ops is still badfileops. E.g.,
4095 * devfs_open() transiently create such situation to
4096 * facilitate csw d_fdopen().
4098 * Dupfdopen() handling in kern_openat() installs the
4099 * half-baked file into the process descriptor table, allowing
4100 * other thread to dereference it. Guard against the race by
4103 if (fp->f_vnode == NULL || fp->f_ops == &badfileops) {
4113 * Get an (NFS) file handle.
4115 #ifndef _SYS_SYSPROTO_H_
4116 struct lgetfh_args {
4122 sys_lgetfh(struct thread *td, struct lgetfh_args *uap)
4124 struct nameidata nd;
4129 error = priv_check(td, PRIV_VFS_GETFH);
4132 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
4137 NDFREE(&nd, NDF_ONLY_PNBUF);
4139 bzero(&fh, sizeof(fh));
4140 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
4141 error = VOP_VPTOFH(vp, &fh.fh_fid);
4144 error = copyout(&fh, uap->fhp, sizeof (fh));
4148 #ifndef _SYS_SYSPROTO_H_
4155 sys_getfh(struct thread *td, struct getfh_args *uap)
4157 struct nameidata nd;
4162 error = priv_check(td, PRIV_VFS_GETFH);
4165 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
4170 NDFREE(&nd, NDF_ONLY_PNBUF);
4172 bzero(&fh, sizeof(fh));
4173 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
4174 error = VOP_VPTOFH(vp, &fh.fh_fid);
4177 error = copyout(&fh, uap->fhp, sizeof (fh));
4182 * syscall for the rpc.lockd to use to translate a NFS file handle into an
4185 * warning: do not remove the priv_check() call or this becomes one giant
4188 #ifndef _SYS_SYSPROTO_H_
4189 struct fhopen_args {
4190 const struct fhandle *u_fhp;
4195 sys_fhopen(struct thread *td, struct fhopen_args *uap)
4204 error = priv_check(td, PRIV_VFS_FHOPEN);
4208 fmode = FFLAGS(uap->flags);
4209 /* why not allow a non-read/write open for our lockd? */
4210 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
4212 error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
4215 /* find the mount point */
4216 mp = vfs_busyfs(&fhp.fh_fsid);
4219 /* now give me my vnode, it gets returned to me locked */
4220 error = VFS_FHTOVP(mp, &fhp.fh_fid, LK_EXCLUSIVE, &vp);
4225 error = falloc_noinstall(td, &fp);
4231 * An extra reference on `fp' has been held for us by
4232 * falloc_noinstall().
4238 error = vn_open_vnode(vp, fmode, td->td_ucred, td, fp);
4240 KASSERT(fp->f_ops == &badfileops,
4241 ("VOP_OPEN in fhopen() set f_ops"));
4242 KASSERT(td->td_dupfd < 0,
4243 ("fhopen() encountered fdopen()"));
4253 finit(fp, (fmode & FMASK) | (fp->f_flag & FHASLOCK), DTYPE_VNODE, vp,
4256 if ((fmode & O_TRUNC) != 0) {
4257 error = fo_truncate(fp, 0, td->td_ucred, td);
4262 error = finstall(td, fp, &indx, fmode, NULL);
4265 td->td_retval[0] = indx;
4270 * Stat an (NFS) file handle.
4272 #ifndef _SYS_SYSPROTO_H_
4273 struct fhstat_args {
4274 struct fhandle *u_fhp;
4279 sys_fhstat(struct thread *td, struct fhstat_args *uap)
4285 error = copyin(uap->u_fhp, &fh, sizeof(fh));
4288 error = kern_fhstat(td, fh, &sb);
4290 error = copyout(&sb, uap->sb, sizeof(sb));
4295 kern_fhstat(struct thread *td, struct fhandle fh, struct stat *sb)
4301 error = priv_check(td, PRIV_VFS_FHSTAT);
4304 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4306 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_EXCLUSIVE, &vp);
4310 error = vn_stat(vp, sb, td->td_ucred, NOCRED, td);
4316 * Implement fstatfs() for (NFS) file handles.
4318 #ifndef _SYS_SYSPROTO_H_
4319 struct fhstatfs_args {
4320 struct fhandle *u_fhp;
4325 sys_fhstatfs(struct thread *td, struct fhstatfs_args *uap)
4331 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4334 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
4335 error = kern_fhstatfs(td, fh, sfp);
4337 error = copyout(sfp, uap->buf, sizeof(*sfp));
4338 free(sfp, M_STATFS);
4343 kern_fhstatfs(struct thread *td, fhandle_t fh, struct statfs *buf)
4350 error = priv_check(td, PRIV_VFS_FHSTATFS);
4353 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4355 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_EXCLUSIVE, &vp);
4361 error = prison_canseemount(td->td_ucred, mp);
4365 error = mac_mount_check_stat(td->td_ucred, mp);
4370 * Set these in case the underlying filesystem fails to do so.
4373 sp->f_version = STATFS_VERSION;
4374 sp->f_namemax = NAME_MAX;
4375 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
4376 error = VFS_STATFS(mp, sp);
4385 kern_posix_fallocate(struct thread *td, int fd, off_t offset, off_t len)
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_pwrite_rights, &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;
4493 if (offset < 0 || len < 0 || offset > OFF_MAX - len)
4495 AUDIT_ARG_VALUE(advice);
4497 case POSIX_FADV_SEQUENTIAL:
4498 case POSIX_FADV_RANDOM:
4499 case POSIX_FADV_NOREUSE:
4500 new = malloc(sizeof(*fa), M_FADVISE, M_WAITOK);
4502 case POSIX_FADV_NORMAL:
4503 case POSIX_FADV_WILLNEED:
4504 case POSIX_FADV_DONTNEED:
4510 /* XXX: CAP_POSIX_FADVISE? */
4512 error = fget(td, fd, &cap_no_rights, &fp);
4515 AUDIT_ARG_FILE(td->td_proc, fp);
4516 if ((fp->f_ops->fo_flags & DFLAG_SEEKABLE) == 0) {
4520 if (fp->f_type != DTYPE_VNODE) {
4525 if (vp->v_type != VREG) {
4532 end = offset + len - 1;
4534 case POSIX_FADV_SEQUENTIAL:
4535 case POSIX_FADV_RANDOM:
4536 case POSIX_FADV_NOREUSE:
4538 * Try to merge any existing non-standard region with
4539 * this new region if possible, otherwise create a new
4540 * non-standard region for this request.
4542 mtx_pool_lock(mtxpool_sleep, fp);
4544 if (fa != NULL && fa->fa_advice == advice &&
4545 ((fa->fa_start <= end && fa->fa_end >= offset) ||
4546 (end != OFF_MAX && fa->fa_start == end + 1) ||
4547 (fa->fa_end != OFF_MAX && fa->fa_end + 1 == offset))) {
4548 if (offset < fa->fa_start)
4549 fa->fa_start = offset;
4550 if (end > fa->fa_end)
4553 new->fa_advice = advice;
4554 new->fa_start = offset;
4559 mtx_pool_unlock(mtxpool_sleep, fp);
4561 case POSIX_FADV_NORMAL:
4563 * If a the "normal" region overlaps with an existing
4564 * non-standard region, trim or remove the
4565 * non-standard region.
4567 mtx_pool_lock(mtxpool_sleep, fp);
4570 if (offset <= fa->fa_start && end >= fa->fa_end) {
4572 fp->f_advice = NULL;
4573 } else if (offset <= fa->fa_start &&
4574 end >= fa->fa_start)
4575 fa->fa_start = end + 1;
4576 else if (offset <= fa->fa_end && end >= fa->fa_end)
4577 fa->fa_end = offset - 1;
4578 else if (offset >= fa->fa_start && end <= fa->fa_end) {
4580 * If the "normal" region is a middle
4581 * portion of the existing
4582 * non-standard region, just remove
4583 * the whole thing rather than picking
4584 * one side or the other to
4588 fp->f_advice = NULL;
4591 mtx_pool_unlock(mtxpool_sleep, fp);
4593 case POSIX_FADV_WILLNEED:
4594 case POSIX_FADV_DONTNEED:
4595 error = VOP_ADVISE(vp, offset, end, advice);
4601 free(new, M_FADVISE);
4606 sys_posix_fadvise(struct thread *td, struct posix_fadvise_args *uap)
4610 error = kern_posix_fadvise(td, uap->fd, uap->offset, uap->len,
4612 return (kern_posix_error(td, error));