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 * 4. 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 chroot_refuse_vdir_fds(struct filedesc *fdp);
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 * The module initialization routine for POSIX asynchronous I/O will
111 * set this to the version of AIO that it implements. (Zero means
112 * that it is not implemented.) This value is used here by pathconf()
113 * and in kern_descrip.c by fpathconf().
115 int async_io_version;
118 * Sync each mounted filesystem.
120 #ifndef _SYS_SYSPROTO_H_
129 struct sync_args *uap;
131 struct mount *mp, *nmp;
134 mtx_lock(&mountlist_mtx);
135 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
136 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK)) {
137 nmp = TAILQ_NEXT(mp, mnt_list);
140 if ((mp->mnt_flag & MNT_RDONLY) == 0 &&
141 vn_start_write(NULL, &mp, V_NOWAIT) == 0) {
142 save = curthread_pflags_set(TDP_SYNCIO);
143 vfs_msync(mp, MNT_NOWAIT);
144 VFS_SYNC(mp, MNT_NOWAIT);
145 curthread_pflags_restore(save);
146 vn_finished_write(mp);
148 mtx_lock(&mountlist_mtx);
149 nmp = TAILQ_NEXT(mp, mnt_list);
152 mtx_unlock(&mountlist_mtx);
157 * Change filesystem quotas.
159 #ifndef _SYS_SYSPROTO_H_
160 struct quotactl_args {
168 sys_quotactl(td, uap)
170 register struct quotactl_args /* {
181 AUDIT_ARG_CMD(uap->cmd);
182 AUDIT_ARG_UID(uap->uid);
183 if (!prison_allow(td->td_ucred, PR_ALLOW_QUOTAS))
185 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
187 if ((error = namei(&nd)) != 0)
189 NDFREE(&nd, NDF_ONLY_PNBUF);
190 mp = nd.ni_vp->v_mount;
193 error = vfs_busy(mp, 0);
197 error = VFS_QUOTACTL(mp, uap->cmd, uap->uid, uap->arg);
200 * Since quota on operation typically needs to open quota
201 * file, the Q_QUOTAON handler needs to unbusy the mount point
202 * before calling into namei. Otherwise, unmount might be
203 * started between two vfs_busy() invocations (first is our,
204 * second is from mount point cross-walk code in lookup()),
207 * Require that Q_QUOTAON handles the vfs_busy() reference on
208 * its own, always returning with ubusied mount point.
210 if ((uap->cmd >> SUBCMDSHIFT) != Q_QUOTAON)
216 * Used by statfs conversion routines to scale the block size up if
217 * necessary so that all of the block counts are <= 'max_size'. Note
218 * that 'max_size' should be a bitmask, i.e. 2^n - 1 for some non-zero
222 statfs_scale_blocks(struct statfs *sf, long max_size)
227 KASSERT(powerof2(max_size + 1), ("%s: invalid max_size", __func__));
230 * Attempt to scale the block counts to give a more accurate
231 * overview to userland of the ratio of free space to used
232 * space. To do this, find the largest block count and compute
233 * a divisor that lets it fit into a signed integer <= max_size.
235 if (sf->f_bavail < 0)
236 count = -sf->f_bavail;
238 count = sf->f_bavail;
239 count = MAX(sf->f_blocks, MAX(sf->f_bfree, count));
240 if (count <= max_size)
243 count >>= flsl(max_size);
250 sf->f_bsize <<= shift;
251 sf->f_blocks >>= shift;
252 sf->f_bfree >>= shift;
253 sf->f_bavail >>= shift;
257 * Get filesystem statistics.
259 #ifndef _SYS_SYSPROTO_H_
268 register struct statfs_args /* {
276 error = kern_statfs(td, uap->path, UIO_USERSPACE, &sf);
278 error = copyout(&sf, uap->buf, sizeof(sf));
283 kern_statfs(struct thread *td, char *path, enum uio_seg pathseg,
287 struct statfs *sp, sb;
291 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
296 mp = nd.ni_vp->v_mount;
298 NDFREE(&nd, NDF_ONLY_PNBUF);
300 error = vfs_busy(mp, 0);
305 error = mac_mount_check_stat(td->td_ucred, mp);
310 * Set these in case the underlying filesystem fails to do so.
313 sp->f_version = STATFS_VERSION;
314 sp->f_namemax = NAME_MAX;
315 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
316 error = VFS_STATFS(mp, sp);
319 if (priv_check(td, PRIV_VFS_GENERATION)) {
320 bcopy(sp, &sb, sizeof(sb));
321 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
322 prison_enforce_statfs(td->td_ucred, mp, &sb);
332 * Get filesystem statistics.
334 #ifndef _SYS_SYSPROTO_H_
335 struct fstatfs_args {
343 register struct fstatfs_args /* {
351 error = kern_fstatfs(td, uap->fd, &sf);
353 error = copyout(&sf, uap->buf, sizeof(sf));
358 kern_fstatfs(struct thread *td, int fd, struct statfs *buf)
362 struct statfs *sp, sb;
368 error = getvnode(td->td_proc->p_fd, fd,
369 cap_rights_init(&rights, CAP_FSTATFS), &fp);
373 vn_lock(vp, LK_SHARED | LK_RETRY);
375 AUDIT_ARG_VNODE1(vp);
386 error = vfs_busy(mp, 0);
391 error = mac_mount_check_stat(td->td_ucred, mp);
396 * Set these in case the underlying filesystem fails to do so.
399 sp->f_version = STATFS_VERSION;
400 sp->f_namemax = NAME_MAX;
401 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
402 error = VFS_STATFS(mp, sp);
405 if (priv_check(td, PRIV_VFS_GENERATION)) {
406 bcopy(sp, &sb, sizeof(sb));
407 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
408 prison_enforce_statfs(td->td_ucred, mp, &sb);
419 * Get statistics on all filesystems.
421 #ifndef _SYS_SYSPROTO_H_
422 struct getfsstat_args {
429 sys_getfsstat(td, uap)
431 register struct getfsstat_args /* {
440 error = kern_getfsstat(td, &uap->buf, uap->bufsize, &count,
441 UIO_USERSPACE, uap->flags);
443 td->td_retval[0] = count;
448 * If (bufsize > 0 && bufseg == UIO_SYSSPACE)
449 * The caller is responsible for freeing memory which will be allocated
453 kern_getfsstat(struct thread *td, struct statfs **buf, size_t bufsize,
454 size_t *countp, enum uio_seg bufseg, int flags)
456 struct mount *mp, *nmp;
457 struct statfs *sfsp, *sp, sb;
458 size_t count, maxcount;
461 maxcount = bufsize / sizeof(struct statfs);
464 else if (bufseg == UIO_USERSPACE)
466 else /* if (bufseg == UIO_SYSSPACE) */ {
468 mtx_lock(&mountlist_mtx);
469 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
472 mtx_unlock(&mountlist_mtx);
473 if (maxcount > count)
475 sfsp = *buf = malloc(maxcount * sizeof(struct statfs), M_TEMP,
479 mtx_lock(&mountlist_mtx);
480 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
481 if (prison_canseemount(td->td_ucred, mp) != 0) {
482 nmp = TAILQ_NEXT(mp, mnt_list);
486 if (mac_mount_check_stat(td->td_ucred, mp) != 0) {
487 nmp = TAILQ_NEXT(mp, mnt_list);
491 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK)) {
492 nmp = TAILQ_NEXT(mp, mnt_list);
495 if (sfsp && count < maxcount) {
498 * Set these in case the underlying filesystem
501 sp->f_version = STATFS_VERSION;
502 sp->f_namemax = NAME_MAX;
503 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
505 * If MNT_NOWAIT or MNT_LAZY is specified, do not
506 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
507 * overrides MNT_WAIT.
509 if (((flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
510 (flags & MNT_WAIT)) &&
511 (error = VFS_STATFS(mp, sp))) {
512 mtx_lock(&mountlist_mtx);
513 nmp = TAILQ_NEXT(mp, mnt_list);
517 if (priv_check(td, PRIV_VFS_GENERATION)) {
518 bcopy(sp, &sb, sizeof(sb));
519 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
520 prison_enforce_statfs(td->td_ucred, mp, &sb);
523 if (bufseg == UIO_SYSSPACE)
524 bcopy(sp, sfsp, sizeof(*sp));
525 else /* if (bufseg == UIO_USERSPACE) */ {
526 error = copyout(sp, sfsp, sizeof(*sp));
535 mtx_lock(&mountlist_mtx);
536 nmp = TAILQ_NEXT(mp, mnt_list);
539 mtx_unlock(&mountlist_mtx);
540 if (sfsp && count > maxcount)
547 #ifdef COMPAT_FREEBSD4
549 * Get old format filesystem statistics.
551 static void cvtstatfs(struct statfs *, struct ostatfs *);
553 #ifndef _SYS_SYSPROTO_H_
554 struct freebsd4_statfs_args {
560 freebsd4_statfs(td, uap)
562 struct freebsd4_statfs_args /* {
571 error = kern_statfs(td, uap->path, UIO_USERSPACE, &sf);
574 cvtstatfs(&sf, &osb);
575 return (copyout(&osb, uap->buf, sizeof(osb)));
579 * Get filesystem statistics.
581 #ifndef _SYS_SYSPROTO_H_
582 struct freebsd4_fstatfs_args {
588 freebsd4_fstatfs(td, uap)
590 struct freebsd4_fstatfs_args /* {
599 error = kern_fstatfs(td, uap->fd, &sf);
602 cvtstatfs(&sf, &osb);
603 return (copyout(&osb, uap->buf, sizeof(osb)));
607 * Get statistics on all filesystems.
609 #ifndef _SYS_SYSPROTO_H_
610 struct freebsd4_getfsstat_args {
617 freebsd4_getfsstat(td, uap)
619 register struct freebsd4_getfsstat_args /* {
625 struct statfs *buf, *sp;
630 count = uap->bufsize / sizeof(struct ostatfs);
631 size = count * sizeof(struct statfs);
632 error = kern_getfsstat(td, &buf, size, &count, UIO_SYSSPACE,
636 while (count > 0 && error == 0) {
638 error = copyout(&osb, uap->buf, sizeof(osb));
646 td->td_retval[0] = count;
651 * Implement fstatfs() for (NFS) file handles.
653 #ifndef _SYS_SYSPROTO_H_
654 struct freebsd4_fhstatfs_args {
655 struct fhandle *u_fhp;
660 freebsd4_fhstatfs(td, uap)
662 struct freebsd4_fhstatfs_args /* {
663 struct fhandle *u_fhp;
672 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
675 error = kern_fhstatfs(td, fh, &sf);
678 cvtstatfs(&sf, &osb);
679 return (copyout(&osb, uap->buf, sizeof(osb)));
683 * Convert a new format statfs structure to an old format statfs structure.
691 statfs_scale_blocks(nsp, LONG_MAX);
692 bzero(osp, sizeof(*osp));
693 osp->f_bsize = nsp->f_bsize;
694 osp->f_iosize = MIN(nsp->f_iosize, LONG_MAX);
695 osp->f_blocks = nsp->f_blocks;
696 osp->f_bfree = nsp->f_bfree;
697 osp->f_bavail = nsp->f_bavail;
698 osp->f_files = MIN(nsp->f_files, LONG_MAX);
699 osp->f_ffree = MIN(nsp->f_ffree, LONG_MAX);
700 osp->f_owner = nsp->f_owner;
701 osp->f_type = nsp->f_type;
702 osp->f_flags = nsp->f_flags;
703 osp->f_syncwrites = MIN(nsp->f_syncwrites, LONG_MAX);
704 osp->f_asyncwrites = MIN(nsp->f_asyncwrites, LONG_MAX);
705 osp->f_syncreads = MIN(nsp->f_syncreads, LONG_MAX);
706 osp->f_asyncreads = MIN(nsp->f_asyncreads, LONG_MAX);
707 strlcpy(osp->f_fstypename, nsp->f_fstypename,
708 MIN(MFSNAMELEN, OMFSNAMELEN));
709 strlcpy(osp->f_mntonname, nsp->f_mntonname,
710 MIN(MNAMELEN, OMNAMELEN));
711 strlcpy(osp->f_mntfromname, nsp->f_mntfromname,
712 MIN(MNAMELEN, OMNAMELEN));
713 osp->f_fsid = nsp->f_fsid;
715 #endif /* COMPAT_FREEBSD4 */
718 * Change current working directory to a given file descriptor.
720 #ifndef _SYS_SYSPROTO_H_
728 struct fchdir_args /* {
732 register struct filedesc *fdp = td->td_proc->p_fd;
733 struct vnode *vp, *tdp, *vpold;
739 AUDIT_ARG_FD(uap->fd);
740 error = getvnode(fdp, uap->fd, cap_rights_init(&rights, CAP_FCHDIR),
747 vn_lock(vp, LK_SHARED | LK_RETRY);
748 AUDIT_ARG_VNODE1(vp);
749 error = change_dir(vp, td);
750 while (!error && (mp = vp->v_mountedhere) != NULL) {
753 error = VFS_ROOT(mp, LK_SHARED, &tdp);
766 vpold = fdp->fd_cdir;
768 FILEDESC_XUNLOCK(fdp);
774 * Change current working directory (``.'').
776 #ifndef _SYS_SYSPROTO_H_
784 struct chdir_args /* {
789 return (kern_chdir(td, uap->path, UIO_USERSPACE));
793 kern_chdir(struct thread *td, char *path, enum uio_seg pathseg)
795 register struct filedesc *fdp = td->td_proc->p_fd;
800 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
802 if ((error = namei(&nd)) != 0)
804 if ((error = change_dir(nd.ni_vp, td)) != 0) {
806 NDFREE(&nd, NDF_ONLY_PNBUF);
809 VOP_UNLOCK(nd.ni_vp, 0);
810 NDFREE(&nd, NDF_ONLY_PNBUF);
813 fdp->fd_cdir = nd.ni_vp;
814 FILEDESC_XUNLOCK(fdp);
820 * Helper function for raised chroot(2) security function: Refuse if
821 * any filedescriptors are open directories.
824 chroot_refuse_vdir_fds(fdp)
825 struct filedesc *fdp;
831 FILEDESC_LOCK_ASSERT(fdp);
833 for (fd = 0; fd <= fdp->fd_lastfile; fd++) {
834 fp = fget_locked(fdp, fd);
837 if (fp->f_type == DTYPE_VNODE) {
839 if (vp->v_type == VDIR)
847 * This sysctl determines if we will allow a process to chroot(2) if it
848 * has a directory open:
849 * 0: disallowed for all processes.
850 * 1: allowed for processes that were not already chroot(2)'ed.
851 * 2: allowed for all processes.
854 static int chroot_allow_open_directories = 1;
856 SYSCTL_INT(_kern, OID_AUTO, chroot_allow_open_directories, CTLFLAG_RW,
857 &chroot_allow_open_directories, 0,
858 "Allow a process to chroot(2) if it has a directory open");
861 * Change notion of root (``/'') directory.
863 #ifndef _SYS_SYSPROTO_H_
871 struct chroot_args /* {
878 error = priv_check(td, PRIV_VFS_CHROOT);
881 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
882 UIO_USERSPACE, uap->path, td);
886 error = change_dir(nd.ni_vp, td);
890 error = mac_vnode_check_chroot(td->td_ucred, nd.ni_vp);
894 VOP_UNLOCK(nd.ni_vp, 0);
895 error = change_root(nd.ni_vp, td);
897 NDFREE(&nd, NDF_ONLY_PNBUF);
902 NDFREE(&nd, NDF_ONLY_PNBUF);
907 * Common routine for chroot and chdir. Callers must provide a locked vnode
919 ASSERT_VOP_LOCKED(vp, "change_dir(): vp not locked");
920 if (vp->v_type != VDIR)
923 error = mac_vnode_check_chdir(td->td_ucred, vp);
927 return (VOP_ACCESS(vp, VEXEC, td->td_ucred, td));
931 * Common routine for kern_chroot() and jail_attach(). The caller is
932 * responsible for invoking priv_check() and mac_vnode_check_chroot() to
933 * authorize this operation.
940 struct filedesc *fdp;
944 fdp = td->td_proc->p_fd;
946 if (chroot_allow_open_directories == 0 ||
947 (chroot_allow_open_directories == 1 && fdp->fd_rdir != rootvnode)) {
948 error = chroot_refuse_vdir_fds(fdp);
950 FILEDESC_XUNLOCK(fdp);
954 oldvp = fdp->fd_rdir;
961 FILEDESC_XUNLOCK(fdp);
967 flags_to_rights(int flags, cap_rights_t *rightsp)
970 if (flags & O_EXEC) {
971 cap_rights_set(rightsp, CAP_FEXECVE);
973 switch ((flags & O_ACCMODE)) {
975 cap_rights_set(rightsp, CAP_READ);
978 cap_rights_set(rightsp, CAP_READ);
981 cap_rights_set(rightsp, CAP_WRITE);
982 if (!(flags & (O_APPEND | O_TRUNC)))
983 cap_rights_set(rightsp, CAP_SEEK);
989 cap_rights_set(rightsp, CAP_CREATE);
992 cap_rights_set(rightsp, CAP_FTRUNCATE);
994 if (flags & (O_SYNC | O_FSYNC))
995 cap_rights_set(rightsp, CAP_FSYNC);
997 if (flags & (O_EXLOCK | O_SHLOCK))
998 cap_rights_set(rightsp, CAP_FLOCK);
1002 * Check permissions, allocate an open file structure, and call the device
1003 * open routine if any.
1005 #ifndef _SYS_SYSPROTO_H_
1015 register struct open_args /* {
1022 return (kern_openat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1023 uap->flags, uap->mode));
1026 #ifndef _SYS_SYSPROTO_H_
1027 struct openat_args {
1035 sys_openat(struct thread *td, struct openat_args *uap)
1038 return (kern_openat(td, uap->fd, uap->path, UIO_USERSPACE, uap->flag,
1043 kern_openat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1044 int flags, int mode)
1046 struct proc *p = td->td_proc;
1047 struct filedesc *fdp = p->p_fd;
1050 struct nameidata nd;
1051 cap_rights_t rights;
1052 int cmode, error, indx;
1056 AUDIT_ARG_FFLAGS(flags);
1057 AUDIT_ARG_MODE(mode);
1058 /* XXX: audit dirfd */
1059 cap_rights_init(&rights, CAP_LOOKUP);
1060 flags_to_rights(flags, &rights);
1062 * Only one of the O_EXEC, O_RDONLY, O_WRONLY and O_RDWR flags
1065 if (flags & O_EXEC) {
1066 if (flags & O_ACCMODE)
1068 } else if ((flags & O_ACCMODE) == O_ACCMODE) {
1071 flags = FFLAGS(flags);
1075 * Allocate the file descriptor, but don't install a descriptor yet.
1077 error = falloc_noinstall(td, &fp);
1081 * An extra reference on `fp' has been held for us by
1082 * falloc_noinstall().
1084 /* Set the flags early so the finit in devfs can pick them up. */
1085 fp->f_flag = flags & FMASK;
1086 cmode = ((mode & ~fdp->fd_cmask) & ALLPERMS) & ~S_ISTXT;
1087 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, fd,
1089 td->td_dupfd = -1; /* XXX check for fdopen */
1090 error = vn_open(&nd, &flags, cmode, fp);
1093 * If the vn_open replaced the method vector, something
1094 * wonderous happened deep below and we just pass it up
1095 * pretending we know what we do.
1097 if (error == ENXIO && fp->f_ops != &badfileops)
1101 * Handle special fdopen() case. bleh.
1103 * Don't do this for relative (capability) lookups; we don't
1104 * understand exactly what would happen, and we don't think
1105 * that it ever should.
1107 if (nd.ni_strictrelative == 0 &&
1108 (error == ENODEV || error == ENXIO) &&
1109 td->td_dupfd >= 0) {
1110 error = dupfdopen(td, fdp, td->td_dupfd, flags, error,
1119 NDFREE(&nd, NDF_ONLY_PNBUF);
1123 * Store the vnode, for any f_type. Typically, the vnode use
1124 * count is decremented by direct call to vn_closefile() for
1125 * files that switched type in the cdevsw fdopen() method.
1129 * If the file wasn't claimed by devfs bind it to the normal
1130 * vnode operations here.
1132 if (fp->f_ops == &badfileops) {
1133 KASSERT(vp->v_type != VFIFO, ("Unexpected fifo."));
1135 finit(fp, (flags & FMASK) | (fp->f_flag & FHASLOCK),
1136 DTYPE_VNODE, vp, &vnops);
1140 if (flags & O_TRUNC) {
1141 error = fo_truncate(fp, 0, td->td_ucred, td);
1147 * If we haven't already installed the FD (for dupfdopen), do so now.
1150 struct filecaps *fcaps;
1153 if (nd.ni_strictrelative == 1)
1154 fcaps = &nd.ni_filecaps;
1158 error = finstall(td, fp, &indx, flags, fcaps);
1159 /* On success finstall() consumes fcaps. */
1161 filecaps_free(&nd.ni_filecaps);
1165 filecaps_free(&nd.ni_filecaps);
1169 * Release our private reference, leaving the one associated with
1170 * the descriptor table intact.
1173 td->td_retval[0] = indx;
1176 KASSERT(indx == -1, ("indx=%d, should be -1", indx));
1185 #ifndef _SYS_SYSPROTO_H_
1186 struct ocreat_args {
1194 register struct ocreat_args /* {
1200 return (kern_openat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1201 O_WRONLY | O_CREAT | O_TRUNC, uap->mode));
1203 #endif /* COMPAT_43 */
1206 * Create a special file.
1208 #ifndef _SYS_SYSPROTO_H_
1218 register struct mknod_args /* {
1225 return (kern_mknodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1226 uap->mode, uap->dev));
1229 #ifndef _SYS_SYSPROTO_H_
1230 struct mknodat_args {
1238 sys_mknodat(struct thread *td, struct mknodat_args *uap)
1241 return (kern_mknodat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode,
1246 kern_mknodat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1252 struct nameidata nd;
1253 cap_rights_t rights;
1254 int error, whiteout = 0;
1256 AUDIT_ARG_MODE(mode);
1258 switch (mode & S_IFMT) {
1261 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
1264 error = priv_check(td, PRIV_VFS_MKNOD_BAD);
1267 error = priv_check(td, PRIV_VFS_MKNOD_WHT);
1271 return (kern_mkfifoat(td, fd, path, pathseg, mode));
1281 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
1282 NOCACHE, pathseg, path, fd, cap_rights_init(&rights, CAP_MKNODAT),
1284 if ((error = namei(&nd)) != 0)
1288 NDFREE(&nd, NDF_ONLY_PNBUF);
1289 if (vp == nd.ni_dvp)
1297 vattr.va_mode = (mode & ALLPERMS) &
1298 ~td->td_proc->p_fd->fd_cmask;
1299 vattr.va_rdev = dev;
1302 switch (mode & S_IFMT) {
1303 case S_IFMT: /* used by badsect to flag bad sectors */
1304 vattr.va_type = VBAD;
1307 vattr.va_type = VCHR;
1310 vattr.va_type = VBLK;
1316 panic("kern_mknod: invalid mode");
1319 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1320 NDFREE(&nd, NDF_ONLY_PNBUF);
1322 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1327 if (error == 0 && !whiteout)
1328 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp,
1329 &nd.ni_cnd, &vattr);
1333 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, CREATE);
1335 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp,
1336 &nd.ni_cnd, &vattr);
1341 NDFREE(&nd, NDF_ONLY_PNBUF);
1343 vn_finished_write(mp);
1348 * Create a named pipe.
1350 #ifndef _SYS_SYSPROTO_H_
1351 struct mkfifo_args {
1359 register struct mkfifo_args /* {
1365 return (kern_mkfifoat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1369 #ifndef _SYS_SYSPROTO_H_
1370 struct mkfifoat_args {
1377 sys_mkfifoat(struct thread *td, struct mkfifoat_args *uap)
1380 return (kern_mkfifoat(td, uap->fd, uap->path, UIO_USERSPACE,
1385 kern_mkfifoat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1390 struct nameidata nd;
1391 cap_rights_t rights;
1394 AUDIT_ARG_MODE(mode);
1397 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
1398 NOCACHE, pathseg, path, fd, cap_rights_init(&rights, CAP_MKFIFOAT),
1400 if ((error = namei(&nd)) != 0)
1402 if (nd.ni_vp != NULL) {
1403 NDFREE(&nd, NDF_ONLY_PNBUF);
1404 if (nd.ni_vp == nd.ni_dvp)
1411 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1412 NDFREE(&nd, NDF_ONLY_PNBUF);
1414 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1419 vattr.va_type = VFIFO;
1420 vattr.va_mode = (mode & ALLPERMS) & ~td->td_proc->p_fd->fd_cmask;
1422 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1427 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
1434 vn_finished_write(mp);
1435 NDFREE(&nd, NDF_ONLY_PNBUF);
1440 * Make a hard file link.
1442 #ifndef _SYS_SYSPROTO_H_
1451 register struct link_args /* {
1457 return (kern_linkat(td, AT_FDCWD, AT_FDCWD, uap->path, uap->link,
1458 UIO_USERSPACE, FOLLOW));
1461 #ifndef _SYS_SYSPROTO_H_
1462 struct linkat_args {
1471 sys_linkat(struct thread *td, struct linkat_args *uap)
1476 if (flag & ~AT_SYMLINK_FOLLOW)
1479 return (kern_linkat(td, uap->fd1, uap->fd2, uap->path1, uap->path2,
1480 UIO_USERSPACE, (flag & AT_SYMLINK_FOLLOW) ? FOLLOW : NOFOLLOW));
1483 int hardlink_check_uid = 0;
1484 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
1485 &hardlink_check_uid, 0,
1486 "Unprivileged processes cannot create hard links to files owned by other "
1488 static int hardlink_check_gid = 0;
1489 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
1490 &hardlink_check_gid, 0,
1491 "Unprivileged processes cannot create hard links to files owned by other "
1495 can_hardlink(struct vnode *vp, struct ucred *cred)
1500 if (!hardlink_check_uid && !hardlink_check_gid)
1503 error = VOP_GETATTR(vp, &va, cred);
1507 if (hardlink_check_uid && cred->cr_uid != va.va_uid) {
1508 error = priv_check_cred(cred, PRIV_VFS_LINK, 0);
1513 if (hardlink_check_gid && !groupmember(va.va_gid, cred)) {
1514 error = priv_check_cred(cred, PRIV_VFS_LINK, 0);
1523 kern_linkat(struct thread *td, int fd1, int fd2, char *path1, char *path2,
1524 enum uio_seg segflg, int follow)
1528 struct nameidata nd;
1529 cap_rights_t rights;
1534 NDINIT_AT(&nd, LOOKUP, follow | AUDITVNODE1, segflg, path1, fd1, td);
1536 if ((error = namei(&nd)) != 0)
1538 NDFREE(&nd, NDF_ONLY_PNBUF);
1540 if (vp->v_type == VDIR) {
1542 return (EPERM); /* POSIX */
1544 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE2 |
1545 NOCACHE, segflg, path2, fd2, cap_rights_init(&rights, CAP_LINKAT),
1547 if ((error = namei(&nd)) == 0) {
1548 if (nd.ni_vp != NULL) {
1549 NDFREE(&nd, NDF_ONLY_PNBUF);
1550 if (nd.ni_dvp == nd.ni_vp)
1557 } else if (nd.ni_dvp->v_mount != vp->v_mount) {
1559 * Cross-device link. No need to recheck
1560 * vp->v_type, since it cannot change, except
1563 NDFREE(&nd, NDF_ONLY_PNBUF);
1567 } else if ((error = vn_lock(vp, LK_EXCLUSIVE)) == 0) {
1568 error = can_hardlink(vp, td->td_ucred);
1571 error = mac_vnode_check_link(td->td_ucred,
1572 nd.ni_dvp, vp, &nd.ni_cnd);
1577 NDFREE(&nd, NDF_ONLY_PNBUF);
1580 error = vn_start_write(vp, &mp, V_NOWAIT);
1584 NDFREE(&nd, NDF_ONLY_PNBUF);
1585 error = vn_start_write(NULL, &mp,
1591 error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
1594 vn_finished_write(mp);
1595 NDFREE(&nd, NDF_ONLY_PNBUF);
1598 NDFREE(&nd, NDF_ONLY_PNBUF);
1608 * Make a symbolic link.
1610 #ifndef _SYS_SYSPROTO_H_
1611 struct symlink_args {
1617 sys_symlink(td, uap)
1619 register struct symlink_args /* {
1625 return (kern_symlinkat(td, uap->path, AT_FDCWD, uap->link,
1629 #ifndef _SYS_SYSPROTO_H_
1630 struct symlinkat_args {
1637 sys_symlinkat(struct thread *td, struct symlinkat_args *uap)
1640 return (kern_symlinkat(td, uap->path1, uap->fd, uap->path2,
1645 kern_symlinkat(struct thread *td, char *path1, int fd, char *path2,
1646 enum uio_seg segflg)
1651 struct nameidata nd;
1653 cap_rights_t rights;
1655 if (segflg == UIO_SYSSPACE) {
1658 syspath = uma_zalloc(namei_zone, M_WAITOK);
1659 if ((error = copyinstr(path1, syspath, MAXPATHLEN, NULL)) != 0)
1662 AUDIT_ARG_TEXT(syspath);
1665 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
1666 NOCACHE, segflg, path2, fd, cap_rights_init(&rights, CAP_SYMLINKAT),
1668 if ((error = namei(&nd)) != 0)
1671 NDFREE(&nd, NDF_ONLY_PNBUF);
1672 if (nd.ni_vp == nd.ni_dvp)
1680 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1681 NDFREE(&nd, NDF_ONLY_PNBUF);
1683 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1688 vattr.va_mode = ACCESSPERMS &~ td->td_proc->p_fd->fd_cmask;
1690 vattr.va_type = VLNK;
1691 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1696 error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr, syspath);
1702 NDFREE(&nd, NDF_ONLY_PNBUF);
1704 vn_finished_write(mp);
1706 if (segflg != UIO_SYSSPACE)
1707 uma_zfree(namei_zone, syspath);
1712 * Delete a whiteout from the filesystem.
1715 sys_undelete(td, uap)
1717 register struct undelete_args /* {
1722 struct nameidata nd;
1727 NDINIT(&nd, DELETE, LOCKPARENT | DOWHITEOUT | AUDITVNODE1,
1728 UIO_USERSPACE, uap->path, td);
1733 if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & ISWHITEOUT)) {
1734 NDFREE(&nd, NDF_ONLY_PNBUF);
1735 if (nd.ni_vp == nd.ni_dvp)
1743 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1744 NDFREE(&nd, NDF_ONLY_PNBUF);
1746 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1750 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, DELETE);
1751 NDFREE(&nd, NDF_ONLY_PNBUF);
1753 vn_finished_write(mp);
1758 * Delete a name from the filesystem.
1760 #ifndef _SYS_SYSPROTO_H_
1761 struct unlink_args {
1768 struct unlink_args /* {
1773 return (kern_unlinkat(td, AT_FDCWD, uap->path, UIO_USERSPACE, 0));
1776 #ifndef _SYS_SYSPROTO_H_
1777 struct unlinkat_args {
1784 sys_unlinkat(struct thread *td, struct unlinkat_args *uap)
1786 int flag = uap->flag;
1788 char *path = uap->path;
1790 if (flag & ~AT_REMOVEDIR)
1793 if (flag & AT_REMOVEDIR)
1794 return (kern_rmdirat(td, fd, path, UIO_USERSPACE));
1796 return (kern_unlinkat(td, fd, path, UIO_USERSPACE, 0));
1800 kern_unlinkat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1805 struct nameidata nd;
1807 cap_rights_t rights;
1812 NDINIT_ATRIGHTS(&nd, DELETE, LOCKPARENT | LOCKLEAF | AUDITVNODE1,
1813 pathseg, path, fd, cap_rights_init(&rights, CAP_UNLINKAT), td);
1814 if ((error = namei(&nd)) != 0)
1815 return (error == EINVAL ? EPERM : error);
1817 if (vp->v_type == VDIR && oldinum == 0) {
1818 error = EPERM; /* POSIX */
1819 } else if (oldinum != 0 &&
1820 ((error = vn_stat(vp, &sb, td->td_ucred, NOCRED, td)) == 0) &&
1821 sb.st_ino != oldinum) {
1822 error = EIDRM; /* Identifier removed */
1825 * The root of a mounted filesystem cannot be deleted.
1827 * XXX: can this only be a VDIR case?
1829 if (vp->v_vflag & VV_ROOT)
1833 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1834 NDFREE(&nd, NDF_ONLY_PNBUF);
1836 if (vp == nd.ni_dvp)
1840 if ((error = vn_start_write(NULL, &mp,
1841 V_XSLEEP | PCATCH)) != 0)
1846 error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp,
1851 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_UNLINK);
1852 error = VOP_REMOVE(nd.ni_dvp, vp, &nd.ni_cnd);
1856 vn_finished_write(mp);
1858 NDFREE(&nd, NDF_ONLY_PNBUF);
1860 if (vp == nd.ni_dvp)
1868 * Reposition read/write file offset.
1870 #ifndef _SYS_SYSPROTO_H_
1881 register struct lseek_args /* {
1889 cap_rights_t rights;
1892 AUDIT_ARG_FD(uap->fd);
1893 error = fget(td, uap->fd, cap_rights_init(&rights, CAP_SEEK), &fp);
1896 error = (fp->f_ops->fo_flags & DFLAG_SEEKABLE) != 0 ?
1897 fo_seek(fp, uap->offset, uap->whence, td) : ESPIPE;
1902 #if defined(COMPAT_43)
1904 * Reposition read/write file offset.
1906 #ifndef _SYS_SYSPROTO_H_
1907 struct olseek_args {
1916 register struct olseek_args /* {
1922 struct lseek_args /* {
1930 nuap.offset = uap->offset;
1931 nuap.whence = uap->whence;
1932 return (sys_lseek(td, &nuap));
1934 #endif /* COMPAT_43 */
1936 #if defined(COMPAT_FREEBSD6)
1937 /* Version with the 'pad' argument */
1939 freebsd6_lseek(td, uap)
1941 register struct freebsd6_lseek_args *uap;
1943 struct lseek_args ouap;
1946 ouap.offset = uap->offset;
1947 ouap.whence = uap->whence;
1948 return (sys_lseek(td, &ouap));
1953 * Check access permissions using passed credentials.
1956 vn_access(vp, user_flags, cred, td)
1965 /* Flags == 0 means only check for existence. */
1966 if (user_flags == 0)
1970 if (user_flags & R_OK)
1972 if (user_flags & W_OK)
1974 if (user_flags & X_OK)
1977 error = mac_vnode_check_access(cred, vp, accmode);
1981 if ((accmode & VWRITE) == 0 || (error = vn_writechk(vp)) == 0)
1982 error = VOP_ACCESS(vp, accmode, cred, td);
1987 * Check access permissions using "real" credentials.
1989 #ifndef _SYS_SYSPROTO_H_
1990 struct access_args {
1998 register struct access_args /* {
2004 return (kern_accessat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2008 #ifndef _SYS_SYSPROTO_H_
2009 struct faccessat_args {
2017 sys_faccessat(struct thread *td, struct faccessat_args *uap)
2020 return (kern_accessat(td, uap->fd, uap->path, UIO_USERSPACE, uap->flag,
2025 kern_accessat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2026 int flag, int amode)
2028 struct ucred *cred, *usecred;
2030 struct nameidata nd;
2031 cap_rights_t rights;
2034 if (flag & ~AT_EACCESS)
2036 if (amode != F_OK && (amode & ~(R_OK | W_OK | X_OK)) != 0)
2040 * Create and modify a temporary credential instead of one that
2041 * is potentially shared (if we need one).
2043 cred = td->td_ucred;
2044 if ((flag & AT_EACCESS) == 0 &&
2045 ((cred->cr_uid != cred->cr_ruid ||
2046 cred->cr_rgid != cred->cr_groups[0]))) {
2047 usecred = crdup(cred);
2048 usecred->cr_uid = cred->cr_ruid;
2049 usecred->cr_groups[0] = cred->cr_rgid;
2050 td->td_ucred = usecred;
2053 AUDIT_ARG_VALUE(amode);
2054 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF |
2055 AUDITVNODE1, pathseg, path, fd, cap_rights_init(&rights, CAP_FSTAT),
2057 if ((error = namei(&nd)) != 0)
2061 error = vn_access(vp, amode, usecred, td);
2062 NDFREE(&nd, NDF_ONLY_PNBUF);
2065 if (usecred != cred) {
2066 td->td_ucred = cred;
2073 * Check access permissions using "effective" credentials.
2075 #ifndef _SYS_SYSPROTO_H_
2076 struct eaccess_args {
2082 sys_eaccess(td, uap)
2084 register struct eaccess_args /* {
2090 return (kern_accessat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2091 AT_EACCESS, uap->amode));
2094 #if defined(COMPAT_43)
2096 * Get file status; this version follows links.
2098 #ifndef _SYS_SYSPROTO_H_
2107 register struct ostat_args /* {
2116 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
2121 return (copyout(&osb, uap->ub, sizeof (osb)));
2125 * Get file status; this version does not follow links.
2127 #ifndef _SYS_SYSPROTO_H_
2128 struct olstat_args {
2136 register struct olstat_args /* {
2145 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2146 UIO_USERSPACE, &sb, NULL);
2150 return (copyout(&osb, uap->ub, sizeof (osb)));
2154 * Convert from an old to a new stat structure.
2162 ost->st_dev = st->st_dev;
2163 ost->st_ino = st->st_ino;
2164 ost->st_mode = st->st_mode;
2165 ost->st_nlink = st->st_nlink;
2166 ost->st_uid = st->st_uid;
2167 ost->st_gid = st->st_gid;
2168 ost->st_rdev = st->st_rdev;
2169 if (st->st_size < (quad_t)1 << 32)
2170 ost->st_size = st->st_size;
2173 ost->st_atim = st->st_atim;
2174 ost->st_mtim = st->st_mtim;
2175 ost->st_ctim = st->st_ctim;
2176 ost->st_blksize = st->st_blksize;
2177 ost->st_blocks = st->st_blocks;
2178 ost->st_flags = st->st_flags;
2179 ost->st_gen = st->st_gen;
2181 #endif /* COMPAT_43 */
2184 * Get file status; this version follows links.
2186 #ifndef _SYS_SYSPROTO_H_
2195 register struct stat_args /* {
2203 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
2206 error = copyout(&sb, uap->ub, sizeof (sb));
2210 #ifndef _SYS_SYSPROTO_H_
2211 struct fstatat_args {
2219 sys_fstatat(struct thread *td, struct fstatat_args *uap)
2224 error = kern_statat(td, uap->flag, uap->fd, uap->path,
2225 UIO_USERSPACE, &sb, NULL);
2227 error = copyout(&sb, uap->buf, sizeof (sb));
2232 kern_statat(struct thread *td, int flag, int fd, char *path,
2233 enum uio_seg pathseg, struct stat *sbp,
2234 void (*hook)(struct vnode *vp, struct stat *sbp))
2236 struct nameidata nd;
2238 cap_rights_t rights;
2241 if (flag & ~AT_SYMLINK_NOFOLLOW)
2244 NDINIT_ATRIGHTS(&nd, LOOKUP, ((flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW :
2245 FOLLOW) | LOCKSHARED | LOCKLEAF | AUDITVNODE1, pathseg, path, fd,
2246 cap_rights_init(&rights, CAP_FSTAT), td);
2248 if ((error = namei(&nd)) != 0)
2250 error = vn_stat(nd.ni_vp, &sb, td->td_ucred, NOCRED, td);
2252 SDT_PROBE(vfs, , stat, mode, path, sb.st_mode, 0, 0, 0);
2253 if (S_ISREG(sb.st_mode))
2254 SDT_PROBE(vfs, , stat, reg, path, pathseg, 0, 0, 0);
2255 if (__predict_false(hook != NULL))
2256 hook(nd.ni_vp, &sb);
2258 NDFREE(&nd, NDF_ONLY_PNBUF);
2264 if (KTRPOINT(td, KTR_STRUCT))
2271 * Get file status; this version does not follow links.
2273 #ifndef _SYS_SYSPROTO_H_
2282 register struct lstat_args /* {
2290 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2291 UIO_USERSPACE, &sb, NULL);
2293 error = copyout(&sb, uap->ub, sizeof (sb));
2298 * Implementation of the NetBSD [l]stat() functions.
2306 bzero(nsb, sizeof *nsb);
2307 nsb->st_dev = sb->st_dev;
2308 nsb->st_ino = sb->st_ino;
2309 nsb->st_mode = sb->st_mode;
2310 nsb->st_nlink = sb->st_nlink;
2311 nsb->st_uid = sb->st_uid;
2312 nsb->st_gid = sb->st_gid;
2313 nsb->st_rdev = sb->st_rdev;
2314 nsb->st_atim = sb->st_atim;
2315 nsb->st_mtim = sb->st_mtim;
2316 nsb->st_ctim = sb->st_ctim;
2317 nsb->st_size = sb->st_size;
2318 nsb->st_blocks = sb->st_blocks;
2319 nsb->st_blksize = sb->st_blksize;
2320 nsb->st_flags = sb->st_flags;
2321 nsb->st_gen = sb->st_gen;
2322 nsb->st_birthtim = sb->st_birthtim;
2325 #ifndef _SYS_SYSPROTO_H_
2334 register struct nstat_args /* {
2343 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
2347 cvtnstat(&sb, &nsb);
2348 return (copyout(&nsb, uap->ub, sizeof (nsb)));
2352 * NetBSD lstat. Get file status; this version does not follow links.
2354 #ifndef _SYS_SYSPROTO_H_
2363 register struct nlstat_args /* {
2372 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2373 UIO_USERSPACE, &sb, NULL);
2376 cvtnstat(&sb, &nsb);
2377 return (copyout(&nsb, uap->ub, sizeof (nsb)));
2381 * Get configurable pathname variables.
2383 #ifndef _SYS_SYSPROTO_H_
2384 struct pathconf_args {
2390 sys_pathconf(td, uap)
2392 register struct pathconf_args /* {
2398 return (kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name, FOLLOW));
2401 #ifndef _SYS_SYSPROTO_H_
2402 struct lpathconf_args {
2408 sys_lpathconf(td, uap)
2410 register struct lpathconf_args /* {
2416 return (kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name,
2421 kern_pathconf(struct thread *td, char *path, enum uio_seg pathseg, int name,
2424 struct nameidata nd;
2427 NDINIT(&nd, LOOKUP, LOCKSHARED | LOCKLEAF | AUDITVNODE1 | flags,
2429 if ((error = namei(&nd)) != 0)
2431 NDFREE(&nd, NDF_ONLY_PNBUF);
2433 /* If asynchronous I/O is available, it works for all files. */
2434 if (name == _PC_ASYNC_IO)
2435 td->td_retval[0] = async_io_version;
2437 error = VOP_PATHCONF(nd.ni_vp, name, td->td_retval);
2443 * Return target name of a symbolic link.
2445 #ifndef _SYS_SYSPROTO_H_
2446 struct readlink_args {
2453 sys_readlink(td, uap)
2455 register struct readlink_args /* {
2462 return (kern_readlinkat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2463 uap->buf, UIO_USERSPACE, uap->count));
2465 #ifndef _SYS_SYSPROTO_H_
2466 struct readlinkat_args {
2474 sys_readlinkat(struct thread *td, struct readlinkat_args *uap)
2477 return (kern_readlinkat(td, uap->fd, uap->path, UIO_USERSPACE,
2478 uap->buf, UIO_USERSPACE, uap->bufsize));
2482 kern_readlinkat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2483 char *buf, enum uio_seg bufseg, size_t count)
2488 struct nameidata nd;
2491 if (count > IOSIZE_MAX)
2494 NDINIT_AT(&nd, LOOKUP, NOFOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
2495 pathseg, path, fd, td);
2497 if ((error = namei(&nd)) != 0)
2499 NDFREE(&nd, NDF_ONLY_PNBUF);
2502 error = mac_vnode_check_readlink(td->td_ucred, vp);
2508 if (vp->v_type != VLNK)
2511 aiov.iov_base = buf;
2512 aiov.iov_len = count;
2513 auio.uio_iov = &aiov;
2514 auio.uio_iovcnt = 1;
2515 auio.uio_offset = 0;
2516 auio.uio_rw = UIO_READ;
2517 auio.uio_segflg = bufseg;
2519 auio.uio_resid = count;
2520 error = VOP_READLINK(vp, &auio, td->td_ucred);
2521 td->td_retval[0] = count - auio.uio_resid;
2528 * Common implementation code for chflags() and fchflags().
2531 setfflags(td, vp, flags)
2540 /* We can't support the value matching VNOVAL. */
2541 if (flags == VNOVAL)
2542 return (EOPNOTSUPP);
2545 * Prevent non-root users from setting flags on devices. When
2546 * a device is reused, users can retain ownership of the device
2547 * if they are allowed to set flags and programs assume that
2548 * chown can't fail when done as root.
2550 if (vp->v_type == VCHR || vp->v_type == VBLK) {
2551 error = priv_check(td, PRIV_VFS_CHFLAGS_DEV);
2556 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2559 vattr.va_flags = flags;
2560 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2562 error = mac_vnode_check_setflags(td->td_ucred, vp, vattr.va_flags);
2565 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
2567 vn_finished_write(mp);
2572 * Change flags of a file given a path name.
2574 #ifndef _SYS_SYSPROTO_H_
2575 struct chflags_args {
2581 sys_chflags(td, uap)
2583 register struct chflags_args /* {
2589 return (kern_chflagsat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2593 #ifndef _SYS_SYSPROTO_H_
2594 struct chflagsat_args {
2602 sys_chflagsat(struct thread *td, struct chflagsat_args *uap)
2605 const char *path = uap->path;
2606 u_long flags = uap->flags;
2607 int atflag = uap->atflag;
2609 if (atflag & ~AT_SYMLINK_NOFOLLOW)
2612 return (kern_chflagsat(td, fd, path, UIO_USERSPACE, flags, atflag));
2616 * Same as chflags() but doesn't follow symlinks.
2619 sys_lchflags(td, uap)
2621 register struct lchflags_args /* {
2627 return (kern_chflagsat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2628 uap->flags, AT_SYMLINK_NOFOLLOW));
2632 kern_chflagsat(struct thread *td, int fd, const char *path,
2633 enum uio_seg pathseg, u_long flags, int atflag)
2635 struct nameidata nd;
2636 cap_rights_t rights;
2639 AUDIT_ARG_FFLAGS(flags);
2640 follow = (atflag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
2641 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, pathseg, path, fd,
2642 cap_rights_init(&rights, CAP_FCHFLAGS), td);
2643 if ((error = namei(&nd)) != 0)
2645 NDFREE(&nd, NDF_ONLY_PNBUF);
2646 error = setfflags(td, nd.ni_vp, flags);
2652 * Change flags of a file given a file descriptor.
2654 #ifndef _SYS_SYSPROTO_H_
2655 struct fchflags_args {
2661 sys_fchflags(td, uap)
2663 register struct fchflags_args /* {
2669 cap_rights_t rights;
2672 AUDIT_ARG_FD(uap->fd);
2673 AUDIT_ARG_FFLAGS(uap->flags);
2674 error = getvnode(td->td_proc->p_fd, uap->fd,
2675 cap_rights_init(&rights, CAP_FCHFLAGS), &fp);
2679 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
2680 AUDIT_ARG_VNODE1(fp->f_vnode);
2681 VOP_UNLOCK(fp->f_vnode, 0);
2683 error = setfflags(td, fp->f_vnode, uap->flags);
2689 * Common implementation code for chmod(), lchmod() and fchmod().
2692 setfmode(td, cred, vp, mode)
2702 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2704 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2706 vattr.va_mode = mode & ALLPERMS;
2708 error = mac_vnode_check_setmode(cred, vp, vattr.va_mode);
2711 error = VOP_SETATTR(vp, &vattr, cred);
2713 vn_finished_write(mp);
2718 * Change mode of a file given path name.
2720 #ifndef _SYS_SYSPROTO_H_
2729 register struct chmod_args /* {
2735 return (kern_fchmodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2739 #ifndef _SYS_SYSPROTO_H_
2740 struct fchmodat_args {
2748 sys_fchmodat(struct thread *td, struct fchmodat_args *uap)
2750 int flag = uap->flag;
2752 char *path = uap->path;
2753 mode_t mode = uap->mode;
2755 if (flag & ~AT_SYMLINK_NOFOLLOW)
2758 return (kern_fchmodat(td, fd, path, UIO_USERSPACE, mode, flag));
2762 * Change mode of a file given path name (don't follow links.)
2764 #ifndef _SYS_SYSPROTO_H_
2765 struct lchmod_args {
2773 register struct lchmod_args /* {
2779 return (kern_fchmodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2780 uap->mode, AT_SYMLINK_NOFOLLOW));
2784 kern_fchmodat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2785 mode_t mode, int flag)
2787 struct nameidata nd;
2788 cap_rights_t rights;
2791 AUDIT_ARG_MODE(mode);
2792 follow = (flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
2793 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, pathseg, path, fd,
2794 cap_rights_init(&rights, CAP_FCHMOD), td);
2795 if ((error = namei(&nd)) != 0)
2797 NDFREE(&nd, NDF_ONLY_PNBUF);
2798 error = setfmode(td, td->td_ucred, nd.ni_vp, mode);
2804 * Change mode of a file given a file descriptor.
2806 #ifndef _SYS_SYSPROTO_H_
2807 struct fchmod_args {
2813 sys_fchmod(struct thread *td, struct fchmod_args *uap)
2816 cap_rights_t rights;
2819 AUDIT_ARG_FD(uap->fd);
2820 AUDIT_ARG_MODE(uap->mode);
2822 error = fget(td, uap->fd, cap_rights_init(&rights, CAP_FCHMOD), &fp);
2825 error = fo_chmod(fp, uap->mode, td->td_ucred, td);
2831 * Common implementation for chown(), lchown(), and fchown()
2834 setfown(td, cred, vp, uid, gid)
2845 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2847 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2852 error = mac_vnode_check_setowner(cred, vp, vattr.va_uid,
2856 error = VOP_SETATTR(vp, &vattr, cred);
2858 vn_finished_write(mp);
2863 * Set ownership given a path name.
2865 #ifndef _SYS_SYSPROTO_H_
2875 register struct chown_args /* {
2882 return (kern_fchownat(td, AT_FDCWD, uap->path, UIO_USERSPACE, uap->uid,
2886 #ifndef _SYS_SYSPROTO_H_
2887 struct fchownat_args {
2896 sys_fchownat(struct thread *td, struct fchownat_args *uap)
2901 if (flag & ~AT_SYMLINK_NOFOLLOW)
2904 return (kern_fchownat(td, uap->fd, uap->path, UIO_USERSPACE, uap->uid,
2905 uap->gid, uap->flag));
2909 kern_fchownat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2910 int uid, int gid, int flag)
2912 struct nameidata nd;
2913 cap_rights_t rights;
2916 AUDIT_ARG_OWNER(uid, gid);
2917 follow = (flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
2918 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, pathseg, path, fd,
2919 cap_rights_init(&rights, CAP_FCHOWN), td);
2921 if ((error = namei(&nd)) != 0)
2923 NDFREE(&nd, NDF_ONLY_PNBUF);
2924 error = setfown(td, td->td_ucred, nd.ni_vp, uid, gid);
2930 * Set ownership given a path name, do not cross symlinks.
2932 #ifndef _SYS_SYSPROTO_H_
2933 struct lchown_args {
2942 register struct lchown_args /* {
2949 return (kern_fchownat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2950 uap->uid, uap->gid, AT_SYMLINK_NOFOLLOW));
2954 * Set ownership given a file descriptor.
2956 #ifndef _SYS_SYSPROTO_H_
2957 struct fchown_args {
2966 register struct fchown_args /* {
2973 cap_rights_t rights;
2976 AUDIT_ARG_FD(uap->fd);
2977 AUDIT_ARG_OWNER(uap->uid, uap->gid);
2978 error = fget(td, uap->fd, cap_rights_init(&rights, CAP_FCHOWN), &fp);
2981 error = fo_chown(fp, uap->uid, uap->gid, td->td_ucred, td);
2987 * Common implementation code for utimes(), lutimes(), and futimes().
2990 getutimes(usrtvp, tvpseg, tsp)
2991 const struct timeval *usrtvp;
2992 enum uio_seg tvpseg;
2993 struct timespec *tsp;
2995 struct timeval tv[2];
2996 const struct timeval *tvp;
2999 if (usrtvp == NULL) {
3000 vfs_timestamp(&tsp[0]);
3003 if (tvpseg == UIO_SYSSPACE) {
3006 if ((error = copyin(usrtvp, tv, sizeof(tv))) != 0)
3011 if (tvp[0].tv_usec < 0 || tvp[0].tv_usec >= 1000000 ||
3012 tvp[1].tv_usec < 0 || tvp[1].tv_usec >= 1000000)
3014 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
3015 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
3021 * Common implementation code for futimens(), utimensat().
3023 #define UTIMENS_NULL 0x1
3024 #define UTIMENS_EXIT 0x2
3026 getutimens(const struct timespec *usrtsp, enum uio_seg tspseg,
3027 struct timespec *tsp, int *retflags)
3029 struct timespec tsnow;
3032 vfs_timestamp(&tsnow);
3034 if (usrtsp == NULL) {
3037 *retflags |= UTIMENS_NULL;
3040 if (tspseg == UIO_SYSSPACE) {
3043 } else if ((error = copyin(usrtsp, tsp, sizeof(*tsp) * 2)) != 0)
3045 if (tsp[0].tv_nsec == UTIME_OMIT && tsp[1].tv_nsec == UTIME_OMIT)
3046 *retflags |= UTIMENS_EXIT;
3047 if (tsp[0].tv_nsec == UTIME_NOW && tsp[1].tv_nsec == UTIME_NOW)
3048 *retflags |= UTIMENS_NULL;
3049 if (tsp[0].tv_nsec == UTIME_OMIT)
3050 tsp[0].tv_sec = VNOVAL;
3051 else if (tsp[0].tv_nsec == UTIME_NOW)
3053 else if (tsp[0].tv_nsec < 0 || tsp[0].tv_nsec >= 1000000000L)
3055 if (tsp[1].tv_nsec == UTIME_OMIT)
3056 tsp[1].tv_sec = VNOVAL;
3057 else if (tsp[1].tv_nsec == UTIME_NOW)
3059 else if (tsp[1].tv_nsec < 0 || tsp[1].tv_nsec >= 1000000000L)
3066 * Common implementation code for utimes(), lutimes(), futimes(), futimens(),
3070 setutimes(td, vp, ts, numtimes, nullflag)
3073 const struct timespec *ts;
3079 int error, setbirthtime;
3081 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
3083 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3085 if (numtimes < 3 && !VOP_GETATTR(vp, &vattr, td->td_ucred) &&
3086 timespeccmp(&ts[1], &vattr.va_birthtime, < ))
3089 vattr.va_atime = ts[0];
3090 vattr.va_mtime = ts[1];
3092 vattr.va_birthtime = ts[1];
3094 vattr.va_birthtime = ts[2];
3096 vattr.va_vaflags |= VA_UTIMES_NULL;
3098 error = mac_vnode_check_setutimes(td->td_ucred, vp, vattr.va_atime,
3102 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3104 vn_finished_write(mp);
3109 * Set the access and modification times of a file.
3111 #ifndef _SYS_SYSPROTO_H_
3112 struct utimes_args {
3114 struct timeval *tptr;
3120 register struct utimes_args /* {
3122 struct timeval *tptr;
3126 return (kern_utimesat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
3127 uap->tptr, UIO_USERSPACE));
3130 #ifndef _SYS_SYSPROTO_H_
3131 struct futimesat_args {
3134 const struct timeval * times;
3138 sys_futimesat(struct thread *td, struct futimesat_args *uap)
3141 return (kern_utimesat(td, uap->fd, uap->path, UIO_USERSPACE,
3142 uap->times, UIO_USERSPACE));
3146 kern_utimesat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
3147 struct timeval *tptr, enum uio_seg tptrseg)
3149 struct nameidata nd;
3150 struct timespec ts[2];
3151 cap_rights_t rights;
3154 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3156 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, fd,
3157 cap_rights_init(&rights, CAP_FUTIMES), td);
3159 if ((error = namei(&nd)) != 0)
3161 NDFREE(&nd, NDF_ONLY_PNBUF);
3162 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3168 * Set the access and modification times of a file.
3170 #ifndef _SYS_SYSPROTO_H_
3171 struct lutimes_args {
3173 struct timeval *tptr;
3177 sys_lutimes(td, uap)
3179 register struct lutimes_args /* {
3181 struct timeval *tptr;
3185 return (kern_lutimes(td, uap->path, UIO_USERSPACE, uap->tptr,
3190 kern_lutimes(struct thread *td, char *path, enum uio_seg pathseg,
3191 struct timeval *tptr, enum uio_seg tptrseg)
3193 struct timespec ts[2];
3194 struct nameidata nd;
3197 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3199 NDINIT(&nd, LOOKUP, NOFOLLOW | AUDITVNODE1, pathseg, path, td);
3200 if ((error = namei(&nd)) != 0)
3202 NDFREE(&nd, NDF_ONLY_PNBUF);
3203 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3209 * Set the access and modification times of a file.
3211 #ifndef _SYS_SYSPROTO_H_
3212 struct futimes_args {
3214 struct timeval *tptr;
3218 sys_futimes(td, uap)
3220 register struct futimes_args /* {
3222 struct timeval *tptr;
3226 return (kern_futimes(td, uap->fd, uap->tptr, UIO_USERSPACE));
3230 kern_futimes(struct thread *td, int fd, struct timeval *tptr,
3231 enum uio_seg tptrseg)
3233 struct timespec ts[2];
3235 cap_rights_t rights;
3239 error = getutimes(tptr, tptrseg, ts);
3242 error = getvnode(td->td_proc->p_fd, fd,
3243 cap_rights_init(&rights, CAP_FUTIMES), &fp);
3247 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
3248 AUDIT_ARG_VNODE1(fp->f_vnode);
3249 VOP_UNLOCK(fp->f_vnode, 0);
3251 error = setutimes(td, fp->f_vnode, ts, 2, tptr == NULL);
3257 sys_futimens(struct thread *td, struct futimens_args *uap)
3260 return (kern_futimens(td, uap->fd, uap->times, UIO_USERSPACE));
3264 kern_futimens(struct thread *td, int fd, struct timespec *tptr,
3265 enum uio_seg tptrseg)
3267 struct timespec ts[2];
3269 cap_rights_t rights;
3273 error = getutimens(tptr, tptrseg, ts, &flags);
3276 if (flags & UTIMENS_EXIT)
3278 error = getvnode(td->td_proc->p_fd, fd,
3279 cap_rights_init(&rights, CAP_FUTIMES), &fp);
3283 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
3284 AUDIT_ARG_VNODE1(fp->f_vnode);
3285 VOP_UNLOCK(fp->f_vnode, 0);
3287 error = setutimes(td, fp->f_vnode, ts, 2, flags & UTIMENS_NULL);
3293 sys_utimensat(struct thread *td, struct utimensat_args *uap)
3296 return (kern_utimensat(td, uap->fd, uap->path, UIO_USERSPACE,
3297 uap->times, UIO_USERSPACE, uap->flag));
3301 kern_utimensat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
3302 struct timespec *tptr, enum uio_seg tptrseg, int flag)
3304 struct nameidata nd;
3305 struct timespec ts[2];
3306 cap_rights_t rights;
3309 if (flag & ~AT_SYMLINK_NOFOLLOW)
3312 if ((error = getutimens(tptr, tptrseg, ts, &flags)) != 0)
3314 NDINIT_ATRIGHTS(&nd, LOOKUP, ((flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW :
3315 FOLLOW) | AUDITVNODE1, pathseg, path, fd,
3316 cap_rights_init(&rights, CAP_FUTIMES), td);
3317 if ((error = namei(&nd)) != 0)
3320 * We are allowed to call namei() regardless of 2xUTIME_OMIT.
3322 * "If both tv_nsec fields are UTIME_OMIT... EACCESS may be detected."
3323 * "Search permission is denied by a component of the path prefix."
3325 NDFREE(&nd, NDF_ONLY_PNBUF);
3326 if ((flags & UTIMENS_EXIT) == 0)
3327 error = setutimes(td, nd.ni_vp, ts, 2, flags & UTIMENS_NULL);
3333 * Truncate a file given its path name.
3335 #ifndef _SYS_SYSPROTO_H_
3336 struct truncate_args {
3343 sys_truncate(td, uap)
3345 register struct truncate_args /* {
3352 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3356 kern_truncate(struct thread *td, char *path, enum uio_seg pathseg, off_t length)
3362 struct nameidata nd;
3367 NDINIT(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, td);
3368 if ((error = namei(&nd)) != 0)
3371 rl_cookie = vn_rangelock_wlock(vp, 0, OFF_MAX);
3372 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0) {
3373 vn_rangelock_unlock(vp, rl_cookie);
3377 NDFREE(&nd, NDF_ONLY_PNBUF);
3378 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3379 if (vp->v_type == VDIR)
3382 else if ((error = mac_vnode_check_write(td->td_ucred, NOCRED, vp))) {
3385 else if ((error = vn_writechk(vp)) == 0 &&
3386 (error = VOP_ACCESS(vp, VWRITE, td->td_ucred, td)) == 0) {
3388 vattr.va_size = length;
3389 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3392 vn_finished_write(mp);
3393 vn_rangelock_unlock(vp, rl_cookie);
3398 #if defined(COMPAT_43)
3400 * Truncate a file given its path name.
3402 #ifndef _SYS_SYSPROTO_H_
3403 struct otruncate_args {
3411 register struct otruncate_args /* {
3416 struct truncate_args /* {
3422 nuap.path = uap->path;
3423 nuap.length = uap->length;
3424 return (sys_truncate(td, &nuap));
3426 #endif /* COMPAT_43 */
3428 #if defined(COMPAT_FREEBSD6)
3429 /* Versions with the pad argument */
3431 freebsd6_truncate(struct thread *td, struct freebsd6_truncate_args *uap)
3433 struct truncate_args ouap;
3435 ouap.path = uap->path;
3436 ouap.length = uap->length;
3437 return (sys_truncate(td, &ouap));
3441 freebsd6_ftruncate(struct thread *td, struct freebsd6_ftruncate_args *uap)
3443 struct ftruncate_args ouap;
3446 ouap.length = uap->length;
3447 return (sys_ftruncate(td, &ouap));
3452 * Sync an open file.
3454 #ifndef _SYS_SYSPROTO_H_
3462 struct fsync_args /* {
3469 cap_rights_t rights;
3470 int error, lock_flags;
3472 AUDIT_ARG_FD(uap->fd);
3473 error = getvnode(td->td_proc->p_fd, uap->fd,
3474 cap_rights_init(&rights, CAP_FSYNC), &fp);
3478 error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
3481 if (MNT_SHARED_WRITES(mp) ||
3482 ((mp == NULL) && MNT_SHARED_WRITES(vp->v_mount))) {
3483 lock_flags = LK_SHARED;
3485 lock_flags = LK_EXCLUSIVE;
3487 vn_lock(vp, lock_flags | LK_RETRY);
3488 AUDIT_ARG_VNODE1(vp);
3489 if (vp->v_object != NULL) {
3490 VM_OBJECT_WLOCK(vp->v_object);
3491 vm_object_page_clean(vp->v_object, 0, 0, 0);
3492 VM_OBJECT_WUNLOCK(vp->v_object);
3494 error = VOP_FSYNC(vp, MNT_WAIT, td);
3497 vn_finished_write(mp);
3504 * Rename files. Source and destination must either both be directories, or
3505 * both not be directories. If target is a directory, it must be empty.
3507 #ifndef _SYS_SYSPROTO_H_
3508 struct rename_args {
3516 register struct rename_args /* {
3522 return (kern_renameat(td, AT_FDCWD, uap->from, AT_FDCWD,
3523 uap->to, UIO_USERSPACE));
3526 #ifndef _SYS_SYSPROTO_H_
3527 struct renameat_args {
3535 sys_renameat(struct thread *td, struct renameat_args *uap)
3538 return (kern_renameat(td, uap->oldfd, uap->old, uap->newfd, uap->new,
3543 kern_renameat(struct thread *td, int oldfd, char *old, int newfd, char *new,
3544 enum uio_seg pathseg)
3546 struct mount *mp = NULL;
3547 struct vnode *tvp, *fvp, *tdvp;
3548 struct nameidata fromnd, tond;
3549 cap_rights_t rights;
3555 NDINIT_ATRIGHTS(&fromnd, DELETE, LOCKPARENT | LOCKLEAF | SAVESTART |
3556 AUDITVNODE1, pathseg, old, oldfd,
3557 cap_rights_init(&rights, CAP_RENAMEAT), td);
3559 NDINIT_ATRIGHTS(&fromnd, DELETE, WANTPARENT | SAVESTART | AUDITVNODE1,
3560 pathseg, old, oldfd, cap_rights_init(&rights, CAP_RENAMEAT), td);
3563 if ((error = namei(&fromnd)) != 0)
3566 error = mac_vnode_check_rename_from(td->td_ucred, fromnd.ni_dvp,
3567 fromnd.ni_vp, &fromnd.ni_cnd);
3568 VOP_UNLOCK(fromnd.ni_dvp, 0);
3569 if (fromnd.ni_dvp != fromnd.ni_vp)
3570 VOP_UNLOCK(fromnd.ni_vp, 0);
3573 NDINIT_ATRIGHTS(&tond, RENAME, LOCKPARENT | LOCKLEAF | NOCACHE |
3574 SAVESTART | AUDITVNODE2, pathseg, new, newfd,
3575 cap_rights_init(&rights, CAP_LINKAT), td);
3576 if (fromnd.ni_vp->v_type == VDIR)
3577 tond.ni_cnd.cn_flags |= WILLBEDIR;
3578 if ((error = namei(&tond)) != 0) {
3579 /* Translate error code for rename("dir1", "dir2/."). */
3580 if (error == EISDIR && fvp->v_type == VDIR)
3582 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3583 vrele(fromnd.ni_dvp);
3589 error = vn_start_write(fvp, &mp, V_NOWAIT);
3591 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3592 NDFREE(&tond, NDF_ONLY_PNBUF);
3599 vrele(fromnd.ni_dvp);
3601 vrele(tond.ni_startdir);
3602 if (fromnd.ni_startdir != NULL)
3603 vrele(fromnd.ni_startdir);
3604 error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH);
3610 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
3613 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
3618 if (newfd != AT_FDCWD) {
3620 * If the target already exists we require CAP_UNLINKAT
3623 error = cap_check(&tond.ni_filecaps.fc_rights,
3624 cap_rights_init(&rights, CAP_UNLINKAT));
3635 * If the source is the same as the destination (that is, if they
3636 * are links to the same vnode), then there is nothing to do.
3642 error = mac_vnode_check_rename_to(td->td_ucred, tdvp,
3643 tond.ni_vp, fromnd.ni_dvp == tdvp, &tond.ni_cnd);
3647 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
3648 tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
3649 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3650 NDFREE(&tond, NDF_ONLY_PNBUF);
3652 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3653 NDFREE(&tond, NDF_ONLY_PNBUF);
3660 vrele(fromnd.ni_dvp);
3663 vrele(tond.ni_startdir);
3664 vn_finished_write(mp);
3666 if (fromnd.ni_startdir)
3667 vrele(fromnd.ni_startdir);
3674 * Make a directory file.
3676 #ifndef _SYS_SYSPROTO_H_
3685 register struct mkdir_args /* {
3691 return (kern_mkdirat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
3695 #ifndef _SYS_SYSPROTO_H_
3696 struct mkdirat_args {
3703 sys_mkdirat(struct thread *td, struct mkdirat_args *uap)
3706 return (kern_mkdirat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode));
3710 kern_mkdirat(struct thread *td, int fd, char *path, enum uio_seg segflg,
3716 struct nameidata nd;
3717 cap_rights_t rights;
3720 AUDIT_ARG_MODE(mode);
3723 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
3724 NOCACHE, segflg, path, fd, cap_rights_init(&rights, CAP_MKDIRAT),
3726 nd.ni_cnd.cn_flags |= WILLBEDIR;
3727 if ((error = namei(&nd)) != 0)
3731 NDFREE(&nd, NDF_ONLY_PNBUF);
3733 * XXX namei called with LOCKPARENT but not LOCKLEAF has
3734 * the strange behaviour of leaving the vnode unlocked
3735 * if the target is the same vnode as the parent.
3737 if (vp == nd.ni_dvp)
3744 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3745 NDFREE(&nd, NDF_ONLY_PNBUF);
3747 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3752 vattr.va_type = VDIR;
3753 vattr.va_mode = (mode & ACCESSPERMS) &~ td->td_proc->p_fd->fd_cmask;
3755 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
3760 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
3764 NDFREE(&nd, NDF_ONLY_PNBUF);
3768 vn_finished_write(mp);
3773 * Remove a directory file.
3775 #ifndef _SYS_SYSPROTO_H_
3783 struct rmdir_args /* {
3788 return (kern_rmdirat(td, AT_FDCWD, uap->path, UIO_USERSPACE));
3792 kern_rmdirat(struct thread *td, int fd, char *path, enum uio_seg pathseg)
3796 struct nameidata nd;
3797 cap_rights_t rights;
3802 NDINIT_ATRIGHTS(&nd, DELETE, LOCKPARENT | LOCKLEAF | AUDITVNODE1,
3803 pathseg, path, fd, cap_rights_init(&rights, CAP_UNLINKAT), td);
3804 if ((error = namei(&nd)) != 0)
3807 if (vp->v_type != VDIR) {
3812 * No rmdir "." please.
3814 if (nd.ni_dvp == vp) {
3819 * The root of a mounted filesystem cannot be deleted.
3821 if (vp->v_vflag & VV_ROOT) {
3826 error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp,
3831 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3832 NDFREE(&nd, NDF_ONLY_PNBUF);
3834 if (nd.ni_dvp == vp)
3838 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3842 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_UNLINK);
3843 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
3844 vn_finished_write(mp);
3846 NDFREE(&nd, NDF_ONLY_PNBUF);
3848 if (nd.ni_dvp == vp)
3857 * Read a block of directory entries in a filesystem independent format.
3859 #ifndef _SYS_SYSPROTO_H_
3860 struct ogetdirentries_args {
3868 ogetdirentries(struct thread *td, struct ogetdirentries_args *uap)
3873 error = kern_ogetdirentries(td, uap, &loff);
3875 error = copyout(&loff, uap->basep, sizeof(long));
3880 kern_ogetdirentries(struct thread *td, struct ogetdirentries_args *uap,
3885 struct uio auio, kuio;
3886 struct iovec aiov, kiov;
3887 struct dirent *dp, *edp;
3888 cap_rights_t rights;
3890 int error, eofflag, readcnt;
3894 /* XXX arbitrary sanity limit on `count'. */
3895 if (uap->count > 64 * 1024)
3897 error = getvnode(td->td_proc->p_fd, uap->fd,
3898 cap_rights_init(&rights, CAP_READ), &fp);
3901 if ((fp->f_flag & FREAD) == 0) {
3906 foffset = foffset_lock(fp, 0);
3908 if (vp->v_type != VDIR) {
3909 foffset_unlock(fp, foffset, 0);
3913 aiov.iov_base = uap->buf;
3914 aiov.iov_len = uap->count;
3915 auio.uio_iov = &aiov;
3916 auio.uio_iovcnt = 1;
3917 auio.uio_rw = UIO_READ;
3918 auio.uio_segflg = UIO_USERSPACE;
3920 auio.uio_resid = uap->count;
3921 vn_lock(vp, LK_SHARED | LK_RETRY);
3922 loff = auio.uio_offset = foffset;
3924 error = mac_vnode_check_readdir(td->td_ucred, vp);
3927 foffset_unlock(fp, foffset, FOF_NOUPDATE);
3932 # if (BYTE_ORDER != LITTLE_ENDIAN)
3933 if (vp->v_mount->mnt_maxsymlinklen <= 0) {
3934 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag,
3936 foffset = auio.uio_offset;
3941 kuio.uio_iov = &kiov;
3942 kuio.uio_segflg = UIO_SYSSPACE;
3943 kiov.iov_len = uap->count;
3944 dirbuf = malloc(uap->count, M_TEMP, M_WAITOK);
3945 kiov.iov_base = dirbuf;
3946 error = VOP_READDIR(vp, &kuio, fp->f_cred, &eofflag,
3948 foffset = kuio.uio_offset;
3950 readcnt = uap->count - kuio.uio_resid;
3951 edp = (struct dirent *)&dirbuf[readcnt];
3952 for (dp = (struct dirent *)dirbuf; dp < edp; ) {
3953 # if (BYTE_ORDER == LITTLE_ENDIAN)
3955 * The expected low byte of
3956 * dp->d_namlen is our dp->d_type.
3957 * The high MBZ byte of dp->d_namlen
3958 * is our dp->d_namlen.
3960 dp->d_type = dp->d_namlen;
3964 * The dp->d_type is the high byte
3965 * of the expected dp->d_namlen,
3966 * so must be zero'ed.
3970 if (dp->d_reclen > 0) {
3971 dp = (struct dirent *)
3972 ((char *)dp + dp->d_reclen);
3979 error = uiomove(dirbuf, readcnt, &auio);
3981 free(dirbuf, M_TEMP);
3985 foffset_unlock(fp, foffset, 0);
3989 if (uap->count == auio.uio_resid &&
3990 (vp->v_vflag & VV_ROOT) &&
3991 (vp->v_mount->mnt_flag & MNT_UNION)) {
3992 struct vnode *tvp = vp;
3993 vp = vp->v_mount->mnt_vnodecovered;
4002 foffset_unlock(fp, foffset, 0);
4004 td->td_retval[0] = uap->count - auio.uio_resid;
4009 #endif /* COMPAT_43 */
4012 * Read a block of directory entries in a filesystem independent format.
4014 #ifndef _SYS_SYSPROTO_H_
4015 struct getdirentries_args {
4023 sys_getdirentries(td, uap)
4025 register struct getdirentries_args /* {
4035 error = kern_getdirentries(td, uap->fd, uap->buf, uap->count, &base,
4036 NULL, UIO_USERSPACE);
4039 if (uap->basep != NULL)
4040 error = copyout(&base, uap->basep, sizeof(long));
4045 kern_getdirentries(struct thread *td, int fd, char *buf, u_int count,
4046 long *basep, ssize_t *residp, enum uio_seg bufseg)
4052 cap_rights_t rights;
4058 if (count > IOSIZE_MAX)
4060 auio.uio_resid = count;
4061 error = getvnode(td->td_proc->p_fd, fd,
4062 cap_rights_init(&rights, CAP_READ), &fp);
4065 if ((fp->f_flag & FREAD) == 0) {
4070 foffset = foffset_lock(fp, 0);
4072 if (vp->v_type != VDIR) {
4076 aiov.iov_base = buf;
4077 aiov.iov_len = count;
4078 auio.uio_iov = &aiov;
4079 auio.uio_iovcnt = 1;
4080 auio.uio_rw = UIO_READ;
4081 auio.uio_segflg = bufseg;
4083 vn_lock(vp, LK_SHARED | LK_RETRY);
4084 AUDIT_ARG_VNODE1(vp);
4085 loff = auio.uio_offset = foffset;
4087 error = mac_vnode_check_readdir(td->td_ucred, vp);
4090 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL,
4092 foffset = auio.uio_offset;
4097 if (count == auio.uio_resid &&
4098 (vp->v_vflag & VV_ROOT) &&
4099 (vp->v_mount->mnt_flag & MNT_UNION)) {
4100 struct vnode *tvp = vp;
4102 vp = vp->v_mount->mnt_vnodecovered;
4113 *residp = auio.uio_resid;
4114 td->td_retval[0] = count - auio.uio_resid;
4116 foffset_unlock(fp, foffset, 0);
4121 #ifndef _SYS_SYSPROTO_H_
4122 struct getdents_args {
4129 sys_getdents(td, uap)
4131 register struct getdents_args /* {
4137 struct getdirentries_args ap;
4141 ap.count = uap->count;
4143 return (sys_getdirentries(td, &ap));
4147 * Set the mode mask for creation of filesystem nodes.
4149 #ifndef _SYS_SYSPROTO_H_
4157 struct umask_args /* {
4161 register struct filedesc *fdp;
4163 FILEDESC_XLOCK(td->td_proc->p_fd);
4164 fdp = td->td_proc->p_fd;
4165 td->td_retval[0] = fdp->fd_cmask;
4166 fdp->fd_cmask = uap->newmask & ALLPERMS;
4167 FILEDESC_XUNLOCK(td->td_proc->p_fd);
4172 * Void all references to file by ripping underlying filesystem away from
4175 #ifndef _SYS_SYSPROTO_H_
4176 struct revoke_args {
4183 register struct revoke_args /* {
4189 struct nameidata nd;
4192 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
4194 if ((error = namei(&nd)) != 0)
4197 NDFREE(&nd, NDF_ONLY_PNBUF);
4198 if (vp->v_type != VCHR || vp->v_rdev == NULL) {
4203 error = mac_vnode_check_revoke(td->td_ucred, vp);
4207 error = VOP_GETATTR(vp, &vattr, td->td_ucred);
4210 if (td->td_ucred->cr_uid != vattr.va_uid) {
4211 error = priv_check(td, PRIV_VFS_ADMIN);
4216 VOP_REVOKE(vp, REVOKEALL);
4223 * Convert a user file descriptor to a kernel file entry and check that, if it
4224 * is a capability, the correct rights are present. A reference on the file
4225 * entry is held upon returning.
4228 getvnode(struct filedesc *fdp, int fd, cap_rights_t *rightsp, struct file **fpp)
4233 error = fget_unlocked(fdp, fd, rightsp, &fp, NULL);
4238 * The file could be not of the vnode type, or it may be not
4239 * yet fully initialized, in which case the f_vnode pointer
4240 * may be set, but f_ops is still badfileops. E.g.,
4241 * devfs_open() transiently create such situation to
4242 * facilitate csw d_fdopen().
4244 * Dupfdopen() handling in kern_openat() installs the
4245 * half-baked file into the process descriptor table, allowing
4246 * other thread to dereference it. Guard against the race by
4249 if (fp->f_vnode == NULL || fp->f_ops == &badfileops) {
4250 fdrop(fp, curthread);
4259 * Get an (NFS) file handle.
4261 #ifndef _SYS_SYSPROTO_H_
4262 struct lgetfh_args {
4270 register struct lgetfh_args *uap;
4272 struct nameidata nd;
4274 register struct vnode *vp;
4277 error = priv_check(td, PRIV_VFS_GETFH);
4280 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
4285 NDFREE(&nd, NDF_ONLY_PNBUF);
4287 bzero(&fh, sizeof(fh));
4288 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
4289 error = VOP_VPTOFH(vp, &fh.fh_fid);
4292 error = copyout(&fh, uap->fhp, sizeof (fh));
4296 #ifndef _SYS_SYSPROTO_H_
4305 register struct getfh_args *uap;
4307 struct nameidata nd;
4309 register struct vnode *vp;
4312 error = priv_check(td, PRIV_VFS_GETFH);
4315 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
4320 NDFREE(&nd, NDF_ONLY_PNBUF);
4322 bzero(&fh, sizeof(fh));
4323 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
4324 error = VOP_VPTOFH(vp, &fh.fh_fid);
4327 error = copyout(&fh, uap->fhp, sizeof (fh));
4332 * syscall for the rpc.lockd to use to translate a NFS file handle into an
4335 * warning: do not remove the priv_check() call or this becomes one giant
4338 #ifndef _SYS_SYSPROTO_H_
4339 struct fhopen_args {
4340 const struct fhandle *u_fhp;
4347 struct fhopen_args /* {
4348 const struct fhandle *u_fhp;
4359 error = priv_check(td, PRIV_VFS_FHOPEN);
4363 fmode = FFLAGS(uap->flags);
4364 /* why not allow a non-read/write open for our lockd? */
4365 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
4367 error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
4370 /* find the mount point */
4371 mp = vfs_busyfs(&fhp.fh_fsid);
4374 /* now give me my vnode, it gets returned to me locked */
4375 error = VFS_FHTOVP(mp, &fhp.fh_fid, LK_EXCLUSIVE, &vp);
4380 error = falloc_noinstall(td, &fp);
4386 * An extra reference on `fp' has been held for us by
4387 * falloc_noinstall().
4393 error = vn_open_vnode(vp, fmode, td->td_ucred, td, fp);
4395 KASSERT(fp->f_ops == &badfileops,
4396 ("VOP_OPEN in fhopen() set f_ops"));
4397 KASSERT(td->td_dupfd < 0,
4398 ("fhopen() encountered fdopen()"));
4408 finit(fp, (fmode & FMASK) | (fp->f_flag & FHASLOCK), DTYPE_VNODE, vp,
4411 if ((fmode & O_TRUNC) != 0) {
4412 error = fo_truncate(fp, 0, td->td_ucred, td);
4417 error = finstall(td, fp, &indx, fmode, NULL);
4420 td->td_retval[0] = indx;
4425 * Stat an (NFS) file handle.
4427 #ifndef _SYS_SYSPROTO_H_
4428 struct fhstat_args {
4429 struct fhandle *u_fhp;
4436 register struct fhstat_args /* {
4437 struct fhandle *u_fhp;
4445 error = copyin(uap->u_fhp, &fh, sizeof(fh));
4448 error = kern_fhstat(td, fh, &sb);
4450 error = copyout(&sb, uap->sb, sizeof(sb));
4455 kern_fhstat(struct thread *td, struct fhandle fh, struct stat *sb)
4461 error = priv_check(td, PRIV_VFS_FHSTAT);
4464 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4466 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_EXCLUSIVE, &vp);
4470 error = vn_stat(vp, sb, td->td_ucred, NOCRED, td);
4476 * Implement fstatfs() for (NFS) file handles.
4478 #ifndef _SYS_SYSPROTO_H_
4479 struct fhstatfs_args {
4480 struct fhandle *u_fhp;
4485 sys_fhstatfs(td, uap)
4487 struct fhstatfs_args /* {
4488 struct fhandle *u_fhp;
4496 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4499 error = kern_fhstatfs(td, fh, &sf);
4502 return (copyout(&sf, uap->buf, sizeof(sf)));
4506 kern_fhstatfs(struct thread *td, fhandle_t fh, struct statfs *buf)
4513 error = priv_check(td, PRIV_VFS_FHSTATFS);
4516 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4518 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_EXCLUSIVE, &vp);
4524 error = prison_canseemount(td->td_ucred, mp);
4528 error = mac_mount_check_stat(td->td_ucred, mp);
4533 * Set these in case the underlying filesystem fails to do so.
4536 sp->f_version = STATFS_VERSION;
4537 sp->f_namemax = NAME_MAX;
4538 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
4539 error = VFS_STATFS(mp, sp);
4548 kern_posix_fallocate(struct thread *td, int fd, off_t offset, off_t len)
4553 cap_rights_t rights;
4554 off_t olen, ooffset;
4557 if (offset < 0 || len <= 0)
4559 /* Check for wrap. */
4560 if (offset > OFF_MAX - len)
4562 error = fget(td, fd, cap_rights_init(&rights, CAP_WRITE), &fp);
4565 if ((fp->f_ops->fo_flags & DFLAG_SEEKABLE) == 0) {
4569 if ((fp->f_flag & FWRITE) == 0) {
4573 if (fp->f_type != DTYPE_VNODE) {
4578 if (vp->v_type != VREG) {
4583 /* Allocating blocks may take a long time, so iterate. */
4590 error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
4593 error = vn_lock(vp, LK_EXCLUSIVE);
4595 vn_finished_write(mp);
4599 error = mac_vnode_check_write(td->td_ucred, fp->f_cred, vp);
4602 error = VOP_ALLOCATE(vp, &offset, &len);
4604 vn_finished_write(mp);
4606 if (olen + ooffset != offset + len) {
4607 panic("offset + len changed from %jx/%jx to %jx/%jx",
4608 ooffset, olen, offset, len);
4610 if (error != 0 || len == 0)
4612 KASSERT(olen > len, ("Iteration did not make progress?"));
4621 sys_posix_fallocate(struct thread *td, struct posix_fallocate_args *uap)
4624 td->td_retval[0] = kern_posix_fallocate(td, uap->fd, uap->offset,
4630 * Unlike madvise(2), we do not make a best effort to remember every
4631 * possible caching hint. Instead, we remember the last setting with
4632 * the exception that we will allow POSIX_FADV_NORMAL to adjust the
4633 * region of any current setting.
4636 kern_posix_fadvise(struct thread *td, int fd, off_t offset, off_t len,
4639 struct fadvise_info *fa, *new;
4642 cap_rights_t rights;
4646 if (offset < 0 || len < 0 || offset > OFF_MAX - len)
4649 case POSIX_FADV_SEQUENTIAL:
4650 case POSIX_FADV_RANDOM:
4651 case POSIX_FADV_NOREUSE:
4652 new = malloc(sizeof(*fa), M_FADVISE, M_WAITOK);
4654 case POSIX_FADV_NORMAL:
4655 case POSIX_FADV_WILLNEED:
4656 case POSIX_FADV_DONTNEED:
4662 /* XXX: CAP_POSIX_FADVISE? */
4663 error = fget(td, fd, cap_rights_init(&rights), &fp);
4666 if ((fp->f_ops->fo_flags & DFLAG_SEEKABLE) == 0) {
4670 if (fp->f_type != DTYPE_VNODE) {
4675 if (vp->v_type != VREG) {
4682 end = offset + len - 1;
4684 case POSIX_FADV_SEQUENTIAL:
4685 case POSIX_FADV_RANDOM:
4686 case POSIX_FADV_NOREUSE:
4688 * Try to merge any existing non-standard region with
4689 * this new region if possible, otherwise create a new
4690 * non-standard region for this request.
4692 mtx_pool_lock(mtxpool_sleep, fp);
4694 if (fa != NULL && fa->fa_advice == advice &&
4695 ((fa->fa_start <= end && fa->fa_end >= offset) ||
4696 (end != OFF_MAX && fa->fa_start == end + 1) ||
4697 (fa->fa_end != OFF_MAX && fa->fa_end + 1 == offset))) {
4698 if (offset < fa->fa_start)
4699 fa->fa_start = offset;
4700 if (end > fa->fa_end)
4703 new->fa_advice = advice;
4704 new->fa_start = offset;
4706 new->fa_prevstart = 0;
4707 new->fa_prevend = 0;
4711 mtx_pool_unlock(mtxpool_sleep, fp);
4713 case POSIX_FADV_NORMAL:
4715 * If a the "normal" region overlaps with an existing
4716 * non-standard region, trim or remove the
4717 * non-standard region.
4719 mtx_pool_lock(mtxpool_sleep, fp);
4722 if (offset <= fa->fa_start && end >= fa->fa_end) {
4724 fp->f_advice = NULL;
4725 } else if (offset <= fa->fa_start &&
4726 end >= fa->fa_start)
4727 fa->fa_start = end + 1;
4728 else if (offset <= fa->fa_end && end >= fa->fa_end)
4729 fa->fa_end = offset - 1;
4730 else if (offset >= fa->fa_start && end <= fa->fa_end) {
4732 * If the "normal" region is a middle
4733 * portion of the existing
4734 * non-standard region, just remove
4735 * the whole thing rather than picking
4736 * one side or the other to
4740 fp->f_advice = NULL;
4743 mtx_pool_unlock(mtxpool_sleep, fp);
4745 case POSIX_FADV_WILLNEED:
4746 case POSIX_FADV_DONTNEED:
4747 error = VOP_ADVISE(vp, offset, end, advice);
4753 free(new, M_FADVISE);
4758 sys_posix_fadvise(struct thread *td, struct posix_fadvise_args *uap)
4761 td->td_retval[0] = kern_posix_fadvise(td, uap->fd, uap->offset,
4762 uap->len, uap->advice);