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 /* Version with the 'pad' argument */
1938 freebsd6_lseek(td, uap)
1940 register struct freebsd6_lseek_args *uap;
1942 struct lseek_args ouap;
1945 ouap.offset = uap->offset;
1946 ouap.whence = uap->whence;
1947 return (sys_lseek(td, &ouap));
1951 * Check access permissions using passed credentials.
1954 vn_access(vp, user_flags, cred, td)
1963 /* Flags == 0 means only check for existence. */
1964 if (user_flags == 0)
1968 if (user_flags & R_OK)
1970 if (user_flags & W_OK)
1972 if (user_flags & X_OK)
1975 error = mac_vnode_check_access(cred, vp, accmode);
1979 if ((accmode & VWRITE) == 0 || (error = vn_writechk(vp)) == 0)
1980 error = VOP_ACCESS(vp, accmode, cred, td);
1985 * Check access permissions using "real" credentials.
1987 #ifndef _SYS_SYSPROTO_H_
1988 struct access_args {
1996 register struct access_args /* {
2002 return (kern_accessat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2006 #ifndef _SYS_SYSPROTO_H_
2007 struct faccessat_args {
2015 sys_faccessat(struct thread *td, struct faccessat_args *uap)
2018 return (kern_accessat(td, uap->fd, uap->path, UIO_USERSPACE, uap->flag,
2023 kern_accessat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2024 int flag, int amode)
2026 struct ucred *cred, *usecred;
2028 struct nameidata nd;
2029 cap_rights_t rights;
2032 if (flag & ~AT_EACCESS)
2034 if (amode != F_OK && (amode & ~(R_OK | W_OK | X_OK)) != 0)
2038 * Create and modify a temporary credential instead of one that
2039 * is potentially shared (if we need one).
2041 cred = td->td_ucred;
2042 if ((flag & AT_EACCESS) == 0 &&
2043 ((cred->cr_uid != cred->cr_ruid ||
2044 cred->cr_rgid != cred->cr_groups[0]))) {
2045 usecred = crdup(cred);
2046 usecred->cr_uid = cred->cr_ruid;
2047 usecred->cr_groups[0] = cred->cr_rgid;
2048 td->td_ucred = usecred;
2051 AUDIT_ARG_VALUE(amode);
2052 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF |
2053 AUDITVNODE1, pathseg, path, fd, cap_rights_init(&rights, CAP_FSTAT),
2055 if ((error = namei(&nd)) != 0)
2059 error = vn_access(vp, amode, usecred, td);
2060 NDFREE(&nd, NDF_ONLY_PNBUF);
2063 if (usecred != cred) {
2064 td->td_ucred = cred;
2071 * Check access permissions using "effective" credentials.
2073 #ifndef _SYS_SYSPROTO_H_
2074 struct eaccess_args {
2080 sys_eaccess(td, uap)
2082 register struct eaccess_args /* {
2088 return (kern_accessat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2089 AT_EACCESS, uap->amode));
2092 #if defined(COMPAT_43)
2094 * Get file status; this version follows links.
2096 #ifndef _SYS_SYSPROTO_H_
2105 register struct ostat_args /* {
2114 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
2119 return (copyout(&osb, uap->ub, sizeof (osb)));
2123 * Get file status; this version does not follow links.
2125 #ifndef _SYS_SYSPROTO_H_
2126 struct olstat_args {
2134 register struct olstat_args /* {
2143 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2144 UIO_USERSPACE, &sb, NULL);
2148 return (copyout(&osb, uap->ub, sizeof (osb)));
2152 * Convert from an old to a new stat structure.
2160 ost->st_dev = st->st_dev;
2161 ost->st_ino = st->st_ino;
2162 ost->st_mode = st->st_mode;
2163 ost->st_nlink = st->st_nlink;
2164 ost->st_uid = st->st_uid;
2165 ost->st_gid = st->st_gid;
2166 ost->st_rdev = st->st_rdev;
2167 if (st->st_size < (quad_t)1 << 32)
2168 ost->st_size = st->st_size;
2171 ost->st_atim = st->st_atim;
2172 ost->st_mtim = st->st_mtim;
2173 ost->st_ctim = st->st_ctim;
2174 ost->st_blksize = st->st_blksize;
2175 ost->st_blocks = st->st_blocks;
2176 ost->st_flags = st->st_flags;
2177 ost->st_gen = st->st_gen;
2179 #endif /* COMPAT_43 */
2182 * Get file status; this version follows links.
2184 #ifndef _SYS_SYSPROTO_H_
2193 register struct stat_args /* {
2201 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
2204 error = copyout(&sb, uap->ub, sizeof (sb));
2208 #ifndef _SYS_SYSPROTO_H_
2209 struct fstatat_args {
2217 sys_fstatat(struct thread *td, struct fstatat_args *uap)
2222 error = kern_statat(td, uap->flag, uap->fd, uap->path,
2223 UIO_USERSPACE, &sb, NULL);
2225 error = copyout(&sb, uap->buf, sizeof (sb));
2230 kern_statat(struct thread *td, int flag, int fd, char *path,
2231 enum uio_seg pathseg, struct stat *sbp,
2232 void (*hook)(struct vnode *vp, struct stat *sbp))
2234 struct nameidata nd;
2236 cap_rights_t rights;
2239 if (flag & ~AT_SYMLINK_NOFOLLOW)
2242 NDINIT_ATRIGHTS(&nd, LOOKUP, ((flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW :
2243 FOLLOW) | LOCKSHARED | LOCKLEAF | AUDITVNODE1, pathseg, path, fd,
2244 cap_rights_init(&rights, CAP_FSTAT), td);
2246 if ((error = namei(&nd)) != 0)
2248 error = vn_stat(nd.ni_vp, &sb, td->td_ucred, NOCRED, td);
2250 SDT_PROBE(vfs, , stat, mode, path, sb.st_mode, 0, 0, 0);
2251 if (S_ISREG(sb.st_mode))
2252 SDT_PROBE(vfs, , stat, reg, path, pathseg, 0, 0, 0);
2253 if (__predict_false(hook != NULL))
2254 hook(nd.ni_vp, &sb);
2256 NDFREE(&nd, NDF_ONLY_PNBUF);
2262 if (KTRPOINT(td, KTR_STRUCT))
2269 * Get file status; this version does not follow links.
2271 #ifndef _SYS_SYSPROTO_H_
2280 register struct lstat_args /* {
2288 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2289 UIO_USERSPACE, &sb, NULL);
2291 error = copyout(&sb, uap->ub, sizeof (sb));
2296 * Implementation of the NetBSD [l]stat() functions.
2304 bzero(nsb, sizeof *nsb);
2305 nsb->st_dev = sb->st_dev;
2306 nsb->st_ino = sb->st_ino;
2307 nsb->st_mode = sb->st_mode;
2308 nsb->st_nlink = sb->st_nlink;
2309 nsb->st_uid = sb->st_uid;
2310 nsb->st_gid = sb->st_gid;
2311 nsb->st_rdev = sb->st_rdev;
2312 nsb->st_atim = sb->st_atim;
2313 nsb->st_mtim = sb->st_mtim;
2314 nsb->st_ctim = sb->st_ctim;
2315 nsb->st_size = sb->st_size;
2316 nsb->st_blocks = sb->st_blocks;
2317 nsb->st_blksize = sb->st_blksize;
2318 nsb->st_flags = sb->st_flags;
2319 nsb->st_gen = sb->st_gen;
2320 nsb->st_birthtim = sb->st_birthtim;
2323 #ifndef _SYS_SYSPROTO_H_
2332 register struct nstat_args /* {
2341 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
2345 cvtnstat(&sb, &nsb);
2346 return (copyout(&nsb, uap->ub, sizeof (nsb)));
2350 * NetBSD lstat. Get file status; this version does not follow links.
2352 #ifndef _SYS_SYSPROTO_H_
2361 register struct nlstat_args /* {
2370 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2371 UIO_USERSPACE, &sb, NULL);
2374 cvtnstat(&sb, &nsb);
2375 return (copyout(&nsb, uap->ub, sizeof (nsb)));
2379 * Get configurable pathname variables.
2381 #ifndef _SYS_SYSPROTO_H_
2382 struct pathconf_args {
2388 sys_pathconf(td, uap)
2390 register struct pathconf_args /* {
2396 return (kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name, FOLLOW));
2399 #ifndef _SYS_SYSPROTO_H_
2400 struct lpathconf_args {
2406 sys_lpathconf(td, uap)
2408 register struct lpathconf_args /* {
2414 return (kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name,
2419 kern_pathconf(struct thread *td, char *path, enum uio_seg pathseg, int name,
2422 struct nameidata nd;
2425 NDINIT(&nd, LOOKUP, LOCKSHARED | LOCKLEAF | AUDITVNODE1 | flags,
2427 if ((error = namei(&nd)) != 0)
2429 NDFREE(&nd, NDF_ONLY_PNBUF);
2431 /* If asynchronous I/O is available, it works for all files. */
2432 if (name == _PC_ASYNC_IO)
2433 td->td_retval[0] = async_io_version;
2435 error = VOP_PATHCONF(nd.ni_vp, name, td->td_retval);
2441 * Return target name of a symbolic link.
2443 #ifndef _SYS_SYSPROTO_H_
2444 struct readlink_args {
2451 sys_readlink(td, uap)
2453 register struct readlink_args /* {
2460 return (kern_readlinkat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2461 uap->buf, UIO_USERSPACE, uap->count));
2463 #ifndef _SYS_SYSPROTO_H_
2464 struct readlinkat_args {
2472 sys_readlinkat(struct thread *td, struct readlinkat_args *uap)
2475 return (kern_readlinkat(td, uap->fd, uap->path, UIO_USERSPACE,
2476 uap->buf, UIO_USERSPACE, uap->bufsize));
2480 kern_readlinkat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2481 char *buf, enum uio_seg bufseg, size_t count)
2486 struct nameidata nd;
2489 if (count > IOSIZE_MAX)
2492 NDINIT_AT(&nd, LOOKUP, NOFOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
2493 pathseg, path, fd, td);
2495 if ((error = namei(&nd)) != 0)
2497 NDFREE(&nd, NDF_ONLY_PNBUF);
2500 error = mac_vnode_check_readlink(td->td_ucred, vp);
2506 if (vp->v_type != VLNK)
2509 aiov.iov_base = buf;
2510 aiov.iov_len = count;
2511 auio.uio_iov = &aiov;
2512 auio.uio_iovcnt = 1;
2513 auio.uio_offset = 0;
2514 auio.uio_rw = UIO_READ;
2515 auio.uio_segflg = bufseg;
2517 auio.uio_resid = count;
2518 error = VOP_READLINK(vp, &auio, td->td_ucred);
2519 td->td_retval[0] = count - auio.uio_resid;
2526 * Common implementation code for chflags() and fchflags().
2529 setfflags(td, vp, flags)
2538 /* We can't support the value matching VNOVAL. */
2539 if (flags == VNOVAL)
2540 return (EOPNOTSUPP);
2543 * Prevent non-root users from setting flags on devices. When
2544 * a device is reused, users can retain ownership of the device
2545 * if they are allowed to set flags and programs assume that
2546 * chown can't fail when done as root.
2548 if (vp->v_type == VCHR || vp->v_type == VBLK) {
2549 error = priv_check(td, PRIV_VFS_CHFLAGS_DEV);
2554 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2557 vattr.va_flags = flags;
2558 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2560 error = mac_vnode_check_setflags(td->td_ucred, vp, vattr.va_flags);
2563 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
2565 vn_finished_write(mp);
2570 * Change flags of a file given a path name.
2572 #ifndef _SYS_SYSPROTO_H_
2573 struct chflags_args {
2579 sys_chflags(td, uap)
2581 register struct chflags_args /* {
2587 return (kern_chflagsat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2591 #ifndef _SYS_SYSPROTO_H_
2592 struct chflagsat_args {
2600 sys_chflagsat(struct thread *td, struct chflagsat_args *uap)
2603 const char *path = uap->path;
2604 u_long flags = uap->flags;
2605 int atflag = uap->atflag;
2607 if (atflag & ~AT_SYMLINK_NOFOLLOW)
2610 return (kern_chflagsat(td, fd, path, UIO_USERSPACE, flags, atflag));
2614 * Same as chflags() but doesn't follow symlinks.
2617 sys_lchflags(td, uap)
2619 register struct lchflags_args /* {
2625 return (kern_chflagsat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2626 uap->flags, AT_SYMLINK_NOFOLLOW));
2630 kern_chflagsat(struct thread *td, int fd, const char *path,
2631 enum uio_seg pathseg, u_long flags, int atflag)
2633 struct nameidata nd;
2634 cap_rights_t rights;
2637 AUDIT_ARG_FFLAGS(flags);
2638 follow = (atflag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
2639 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, pathseg, path, fd,
2640 cap_rights_init(&rights, CAP_FCHFLAGS), td);
2641 if ((error = namei(&nd)) != 0)
2643 NDFREE(&nd, NDF_ONLY_PNBUF);
2644 error = setfflags(td, nd.ni_vp, flags);
2650 * Change flags of a file given a file descriptor.
2652 #ifndef _SYS_SYSPROTO_H_
2653 struct fchflags_args {
2659 sys_fchflags(td, uap)
2661 register struct fchflags_args /* {
2667 cap_rights_t rights;
2670 AUDIT_ARG_FD(uap->fd);
2671 AUDIT_ARG_FFLAGS(uap->flags);
2672 error = getvnode(td->td_proc->p_fd, uap->fd,
2673 cap_rights_init(&rights, CAP_FCHFLAGS), &fp);
2677 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
2678 AUDIT_ARG_VNODE1(fp->f_vnode);
2679 VOP_UNLOCK(fp->f_vnode, 0);
2681 error = setfflags(td, fp->f_vnode, uap->flags);
2687 * Common implementation code for chmod(), lchmod() and fchmod().
2690 setfmode(td, cred, vp, mode)
2700 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2702 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2704 vattr.va_mode = mode & ALLPERMS;
2706 error = mac_vnode_check_setmode(cred, vp, vattr.va_mode);
2709 error = VOP_SETATTR(vp, &vattr, cred);
2711 vn_finished_write(mp);
2716 * Change mode of a file given path name.
2718 #ifndef _SYS_SYSPROTO_H_
2727 register struct chmod_args /* {
2733 return (kern_fchmodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2737 #ifndef _SYS_SYSPROTO_H_
2738 struct fchmodat_args {
2746 sys_fchmodat(struct thread *td, struct fchmodat_args *uap)
2748 int flag = uap->flag;
2750 char *path = uap->path;
2751 mode_t mode = uap->mode;
2753 if (flag & ~AT_SYMLINK_NOFOLLOW)
2756 return (kern_fchmodat(td, fd, path, UIO_USERSPACE, mode, flag));
2760 * Change mode of a file given path name (don't follow links.)
2762 #ifndef _SYS_SYSPROTO_H_
2763 struct lchmod_args {
2771 register struct lchmod_args /* {
2777 return (kern_fchmodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2778 uap->mode, AT_SYMLINK_NOFOLLOW));
2782 kern_fchmodat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2783 mode_t mode, int flag)
2785 struct nameidata nd;
2786 cap_rights_t rights;
2789 AUDIT_ARG_MODE(mode);
2790 follow = (flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
2791 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, pathseg, path, fd,
2792 cap_rights_init(&rights, CAP_FCHMOD), td);
2793 if ((error = namei(&nd)) != 0)
2795 NDFREE(&nd, NDF_ONLY_PNBUF);
2796 error = setfmode(td, td->td_ucred, nd.ni_vp, mode);
2802 * Change mode of a file given a file descriptor.
2804 #ifndef _SYS_SYSPROTO_H_
2805 struct fchmod_args {
2811 sys_fchmod(struct thread *td, struct fchmod_args *uap)
2814 cap_rights_t rights;
2817 AUDIT_ARG_FD(uap->fd);
2818 AUDIT_ARG_MODE(uap->mode);
2820 error = fget(td, uap->fd, cap_rights_init(&rights, CAP_FCHMOD), &fp);
2823 error = fo_chmod(fp, uap->mode, td->td_ucred, td);
2829 * Common implementation for chown(), lchown(), and fchown()
2832 setfown(td, cred, vp, uid, gid)
2843 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2845 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2850 error = mac_vnode_check_setowner(cred, vp, vattr.va_uid,
2854 error = VOP_SETATTR(vp, &vattr, cred);
2856 vn_finished_write(mp);
2861 * Set ownership given a path name.
2863 #ifndef _SYS_SYSPROTO_H_
2873 register struct chown_args /* {
2880 return (kern_fchownat(td, AT_FDCWD, uap->path, UIO_USERSPACE, uap->uid,
2884 #ifndef _SYS_SYSPROTO_H_
2885 struct fchownat_args {
2894 sys_fchownat(struct thread *td, struct fchownat_args *uap)
2899 if (flag & ~AT_SYMLINK_NOFOLLOW)
2902 return (kern_fchownat(td, uap->fd, uap->path, UIO_USERSPACE, uap->uid,
2903 uap->gid, uap->flag));
2907 kern_fchownat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2908 int uid, int gid, int flag)
2910 struct nameidata nd;
2911 cap_rights_t rights;
2914 AUDIT_ARG_OWNER(uid, gid);
2915 follow = (flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
2916 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, pathseg, path, fd,
2917 cap_rights_init(&rights, CAP_FCHOWN), td);
2919 if ((error = namei(&nd)) != 0)
2921 NDFREE(&nd, NDF_ONLY_PNBUF);
2922 error = setfown(td, td->td_ucred, nd.ni_vp, uid, gid);
2928 * Set ownership given a path name, do not cross symlinks.
2930 #ifndef _SYS_SYSPROTO_H_
2931 struct lchown_args {
2940 register struct lchown_args /* {
2947 return (kern_fchownat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2948 uap->uid, uap->gid, AT_SYMLINK_NOFOLLOW));
2952 * Set ownership given a file descriptor.
2954 #ifndef _SYS_SYSPROTO_H_
2955 struct fchown_args {
2964 register struct fchown_args /* {
2971 cap_rights_t rights;
2974 AUDIT_ARG_FD(uap->fd);
2975 AUDIT_ARG_OWNER(uap->uid, uap->gid);
2976 error = fget(td, uap->fd, cap_rights_init(&rights, CAP_FCHOWN), &fp);
2979 error = fo_chown(fp, uap->uid, uap->gid, td->td_ucred, td);
2985 * Common implementation code for utimes(), lutimes(), and futimes().
2988 getutimes(usrtvp, tvpseg, tsp)
2989 const struct timeval *usrtvp;
2990 enum uio_seg tvpseg;
2991 struct timespec *tsp;
2993 struct timeval tv[2];
2994 const struct timeval *tvp;
2997 if (usrtvp == NULL) {
2998 vfs_timestamp(&tsp[0]);
3001 if (tvpseg == UIO_SYSSPACE) {
3004 if ((error = copyin(usrtvp, tv, sizeof(tv))) != 0)
3009 if (tvp[0].tv_usec < 0 || tvp[0].tv_usec >= 1000000 ||
3010 tvp[1].tv_usec < 0 || tvp[1].tv_usec >= 1000000)
3012 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
3013 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
3019 * Common implementation code for futimens(), utimensat().
3021 #define UTIMENS_NULL 0x1
3022 #define UTIMENS_EXIT 0x2
3024 getutimens(const struct timespec *usrtsp, enum uio_seg tspseg,
3025 struct timespec *tsp, int *retflags)
3027 struct timespec tsnow;
3030 vfs_timestamp(&tsnow);
3032 if (usrtsp == NULL) {
3035 *retflags |= UTIMENS_NULL;
3038 if (tspseg == UIO_SYSSPACE) {
3041 } else if ((error = copyin(usrtsp, tsp, sizeof(*tsp) * 2)) != 0)
3043 if (tsp[0].tv_nsec == UTIME_OMIT && tsp[1].tv_nsec == UTIME_OMIT)
3044 *retflags |= UTIMENS_EXIT;
3045 if (tsp[0].tv_nsec == UTIME_NOW && tsp[1].tv_nsec == UTIME_NOW)
3046 *retflags |= UTIMENS_NULL;
3047 if (tsp[0].tv_nsec == UTIME_OMIT)
3048 tsp[0].tv_sec = VNOVAL;
3049 else if (tsp[0].tv_nsec == UTIME_NOW)
3051 else if (tsp[0].tv_nsec < 0 || tsp[0].tv_nsec >= 1000000000L)
3053 if (tsp[1].tv_nsec == UTIME_OMIT)
3054 tsp[1].tv_sec = VNOVAL;
3055 else if (tsp[1].tv_nsec == UTIME_NOW)
3057 else if (tsp[1].tv_nsec < 0 || tsp[1].tv_nsec >= 1000000000L)
3064 * Common implementation code for utimes(), lutimes(), futimes(), futimens(),
3068 setutimes(td, vp, ts, numtimes, nullflag)
3071 const struct timespec *ts;
3077 int error, setbirthtime;
3079 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
3081 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3083 if (numtimes < 3 && !VOP_GETATTR(vp, &vattr, td->td_ucred) &&
3084 timespeccmp(&ts[1], &vattr.va_birthtime, < ))
3087 vattr.va_atime = ts[0];
3088 vattr.va_mtime = ts[1];
3090 vattr.va_birthtime = ts[1];
3092 vattr.va_birthtime = ts[2];
3094 vattr.va_vaflags |= VA_UTIMES_NULL;
3096 error = mac_vnode_check_setutimes(td->td_ucred, vp, vattr.va_atime,
3100 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3102 vn_finished_write(mp);
3107 * Set the access and modification times of a file.
3109 #ifndef _SYS_SYSPROTO_H_
3110 struct utimes_args {
3112 struct timeval *tptr;
3118 register struct utimes_args /* {
3120 struct timeval *tptr;
3124 return (kern_utimesat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
3125 uap->tptr, UIO_USERSPACE));
3128 #ifndef _SYS_SYSPROTO_H_
3129 struct futimesat_args {
3132 const struct timeval * times;
3136 sys_futimesat(struct thread *td, struct futimesat_args *uap)
3139 return (kern_utimesat(td, uap->fd, uap->path, UIO_USERSPACE,
3140 uap->times, UIO_USERSPACE));
3144 kern_utimesat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
3145 struct timeval *tptr, enum uio_seg tptrseg)
3147 struct nameidata nd;
3148 struct timespec ts[2];
3149 cap_rights_t rights;
3152 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3154 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, fd,
3155 cap_rights_init(&rights, CAP_FUTIMES), td);
3157 if ((error = namei(&nd)) != 0)
3159 NDFREE(&nd, NDF_ONLY_PNBUF);
3160 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3166 * Set the access and modification times of a file.
3168 #ifndef _SYS_SYSPROTO_H_
3169 struct lutimes_args {
3171 struct timeval *tptr;
3175 sys_lutimes(td, uap)
3177 register struct lutimes_args /* {
3179 struct timeval *tptr;
3183 return (kern_lutimes(td, uap->path, UIO_USERSPACE, uap->tptr,
3188 kern_lutimes(struct thread *td, char *path, enum uio_seg pathseg,
3189 struct timeval *tptr, enum uio_seg tptrseg)
3191 struct timespec ts[2];
3192 struct nameidata nd;
3195 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3197 NDINIT(&nd, LOOKUP, NOFOLLOW | AUDITVNODE1, pathseg, path, td);
3198 if ((error = namei(&nd)) != 0)
3200 NDFREE(&nd, NDF_ONLY_PNBUF);
3201 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3207 * Set the access and modification times of a file.
3209 #ifndef _SYS_SYSPROTO_H_
3210 struct futimes_args {
3212 struct timeval *tptr;
3216 sys_futimes(td, uap)
3218 register struct futimes_args /* {
3220 struct timeval *tptr;
3224 return (kern_futimes(td, uap->fd, uap->tptr, UIO_USERSPACE));
3228 kern_futimes(struct thread *td, int fd, struct timeval *tptr,
3229 enum uio_seg tptrseg)
3231 struct timespec ts[2];
3233 cap_rights_t rights;
3237 error = getutimes(tptr, tptrseg, ts);
3240 error = getvnode(td->td_proc->p_fd, fd,
3241 cap_rights_init(&rights, CAP_FUTIMES), &fp);
3245 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
3246 AUDIT_ARG_VNODE1(fp->f_vnode);
3247 VOP_UNLOCK(fp->f_vnode, 0);
3249 error = setutimes(td, fp->f_vnode, ts, 2, tptr == NULL);
3255 sys_futimens(struct thread *td, struct futimens_args *uap)
3258 return (kern_futimens(td, uap->fd, uap->times, UIO_USERSPACE));
3262 kern_futimens(struct thread *td, int fd, struct timespec *tptr,
3263 enum uio_seg tptrseg)
3265 struct timespec ts[2];
3267 cap_rights_t rights;
3271 error = getutimens(tptr, tptrseg, ts, &flags);
3274 if (flags & UTIMENS_EXIT)
3276 error = getvnode(td->td_proc->p_fd, fd,
3277 cap_rights_init(&rights, CAP_FUTIMES), &fp);
3281 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
3282 AUDIT_ARG_VNODE1(fp->f_vnode);
3283 VOP_UNLOCK(fp->f_vnode, 0);
3285 error = setutimes(td, fp->f_vnode, ts, 2, flags & UTIMENS_NULL);
3291 sys_utimensat(struct thread *td, struct utimensat_args *uap)
3294 return (kern_utimensat(td, uap->fd, uap->path, UIO_USERSPACE,
3295 uap->times, UIO_USERSPACE, uap->flag));
3299 kern_utimensat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
3300 struct timespec *tptr, enum uio_seg tptrseg, int flag)
3302 struct nameidata nd;
3303 struct timespec ts[2];
3304 cap_rights_t rights;
3307 if (flag & ~AT_SYMLINK_NOFOLLOW)
3310 if ((error = getutimens(tptr, tptrseg, ts, &flags)) != 0)
3312 NDINIT_ATRIGHTS(&nd, LOOKUP, ((flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW :
3313 FOLLOW) | AUDITVNODE1, pathseg, path, fd,
3314 cap_rights_init(&rights, CAP_FUTIMES), td);
3315 if ((error = namei(&nd)) != 0)
3318 * We are allowed to call namei() regardless of 2xUTIME_OMIT.
3320 * "If both tv_nsec fields are UTIME_OMIT... EACCESS may be detected."
3321 * "Search permission is denied by a component of the path prefix."
3323 NDFREE(&nd, NDF_ONLY_PNBUF);
3324 if ((flags & UTIMENS_EXIT) == 0)
3325 error = setutimes(td, nd.ni_vp, ts, 2, flags & UTIMENS_NULL);
3331 * Truncate a file given its path name.
3333 #ifndef _SYS_SYSPROTO_H_
3334 struct truncate_args {
3341 sys_truncate(td, uap)
3343 register struct truncate_args /* {
3350 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3354 kern_truncate(struct thread *td, char *path, enum uio_seg pathseg, off_t length)
3360 struct nameidata nd;
3365 NDINIT(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, td);
3366 if ((error = namei(&nd)) != 0)
3369 rl_cookie = vn_rangelock_wlock(vp, 0, OFF_MAX);
3370 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0) {
3371 vn_rangelock_unlock(vp, rl_cookie);
3375 NDFREE(&nd, NDF_ONLY_PNBUF);
3376 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3377 if (vp->v_type == VDIR)
3380 else if ((error = mac_vnode_check_write(td->td_ucred, NOCRED, vp))) {
3383 else if ((error = vn_writechk(vp)) == 0 &&
3384 (error = VOP_ACCESS(vp, VWRITE, td->td_ucred, td)) == 0) {
3386 vattr.va_size = length;
3387 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3390 vn_finished_write(mp);
3391 vn_rangelock_unlock(vp, rl_cookie);
3396 #if defined(COMPAT_43)
3398 * Truncate a file given its path name.
3400 #ifndef _SYS_SYSPROTO_H_
3401 struct otruncate_args {
3409 register struct otruncate_args /* {
3414 struct truncate_args /* {
3420 nuap.path = uap->path;
3421 nuap.length = uap->length;
3422 return (sys_truncate(td, &nuap));
3424 #endif /* COMPAT_43 */
3426 /* Versions with the pad argument */
3428 freebsd6_truncate(struct thread *td, struct freebsd6_truncate_args *uap)
3430 struct truncate_args ouap;
3432 ouap.path = uap->path;
3433 ouap.length = uap->length;
3434 return (sys_truncate(td, &ouap));
3438 freebsd6_ftruncate(struct thread *td, struct freebsd6_ftruncate_args *uap)
3440 struct ftruncate_args ouap;
3443 ouap.length = uap->length;
3444 return (sys_ftruncate(td, &ouap));
3448 * Sync an open file.
3450 #ifndef _SYS_SYSPROTO_H_
3458 struct fsync_args /* {
3465 cap_rights_t rights;
3466 int error, lock_flags;
3468 AUDIT_ARG_FD(uap->fd);
3469 error = getvnode(td->td_proc->p_fd, uap->fd,
3470 cap_rights_init(&rights, CAP_FSYNC), &fp);
3474 error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
3477 if (MNT_SHARED_WRITES(mp) ||
3478 ((mp == NULL) && MNT_SHARED_WRITES(vp->v_mount))) {
3479 lock_flags = LK_SHARED;
3481 lock_flags = LK_EXCLUSIVE;
3483 vn_lock(vp, lock_flags | LK_RETRY);
3484 AUDIT_ARG_VNODE1(vp);
3485 if (vp->v_object != NULL) {
3486 VM_OBJECT_WLOCK(vp->v_object);
3487 vm_object_page_clean(vp->v_object, 0, 0, 0);
3488 VM_OBJECT_WUNLOCK(vp->v_object);
3490 error = VOP_FSYNC(vp, MNT_WAIT, td);
3493 vn_finished_write(mp);
3500 * Rename files. Source and destination must either both be directories, or
3501 * both not be directories. If target is a directory, it must be empty.
3503 #ifndef _SYS_SYSPROTO_H_
3504 struct rename_args {
3512 register struct rename_args /* {
3518 return (kern_renameat(td, AT_FDCWD, uap->from, AT_FDCWD,
3519 uap->to, UIO_USERSPACE));
3522 #ifndef _SYS_SYSPROTO_H_
3523 struct renameat_args {
3531 sys_renameat(struct thread *td, struct renameat_args *uap)
3534 return (kern_renameat(td, uap->oldfd, uap->old, uap->newfd, uap->new,
3539 kern_renameat(struct thread *td, int oldfd, char *old, int newfd, char *new,
3540 enum uio_seg pathseg)
3542 struct mount *mp = NULL;
3543 struct vnode *tvp, *fvp, *tdvp;
3544 struct nameidata fromnd, tond;
3545 cap_rights_t rights;
3551 NDINIT_ATRIGHTS(&fromnd, DELETE, LOCKPARENT | LOCKLEAF | SAVESTART |
3552 AUDITVNODE1, pathseg, old, oldfd,
3553 cap_rights_init(&rights, CAP_RENAMEAT), td);
3555 NDINIT_ATRIGHTS(&fromnd, DELETE, WANTPARENT | SAVESTART | AUDITVNODE1,
3556 pathseg, old, oldfd, cap_rights_init(&rights, CAP_RENAMEAT), td);
3559 if ((error = namei(&fromnd)) != 0)
3562 error = mac_vnode_check_rename_from(td->td_ucred, fromnd.ni_dvp,
3563 fromnd.ni_vp, &fromnd.ni_cnd);
3564 VOP_UNLOCK(fromnd.ni_dvp, 0);
3565 if (fromnd.ni_dvp != fromnd.ni_vp)
3566 VOP_UNLOCK(fromnd.ni_vp, 0);
3569 NDINIT_ATRIGHTS(&tond, RENAME, LOCKPARENT | LOCKLEAF | NOCACHE |
3570 SAVESTART | AUDITVNODE2, pathseg, new, newfd,
3571 cap_rights_init(&rights, CAP_LINKAT), td);
3572 if (fromnd.ni_vp->v_type == VDIR)
3573 tond.ni_cnd.cn_flags |= WILLBEDIR;
3574 if ((error = namei(&tond)) != 0) {
3575 /* Translate error code for rename("dir1", "dir2/."). */
3576 if (error == EISDIR && fvp->v_type == VDIR)
3578 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3579 vrele(fromnd.ni_dvp);
3585 error = vn_start_write(fvp, &mp, V_NOWAIT);
3587 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3588 NDFREE(&tond, NDF_ONLY_PNBUF);
3595 vrele(fromnd.ni_dvp);
3597 vrele(tond.ni_startdir);
3598 if (fromnd.ni_startdir != NULL)
3599 vrele(fromnd.ni_startdir);
3600 error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH);
3606 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
3609 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
3614 if (newfd != AT_FDCWD) {
3616 * If the target already exists we require CAP_UNLINKAT
3619 error = cap_check(&tond.ni_filecaps.fc_rights,
3620 cap_rights_init(&rights, CAP_UNLINKAT));
3631 * If the source is the same as the destination (that is, if they
3632 * are links to the same vnode), then there is nothing to do.
3638 error = mac_vnode_check_rename_to(td->td_ucred, tdvp,
3639 tond.ni_vp, fromnd.ni_dvp == tdvp, &tond.ni_cnd);
3643 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
3644 tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
3645 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3646 NDFREE(&tond, NDF_ONLY_PNBUF);
3648 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3649 NDFREE(&tond, NDF_ONLY_PNBUF);
3656 vrele(fromnd.ni_dvp);
3659 vrele(tond.ni_startdir);
3660 vn_finished_write(mp);
3662 if (fromnd.ni_startdir)
3663 vrele(fromnd.ni_startdir);
3670 * Make a directory file.
3672 #ifndef _SYS_SYSPROTO_H_
3681 register struct mkdir_args /* {
3687 return (kern_mkdirat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
3691 #ifndef _SYS_SYSPROTO_H_
3692 struct mkdirat_args {
3699 sys_mkdirat(struct thread *td, struct mkdirat_args *uap)
3702 return (kern_mkdirat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode));
3706 kern_mkdirat(struct thread *td, int fd, char *path, enum uio_seg segflg,
3712 struct nameidata nd;
3713 cap_rights_t rights;
3716 AUDIT_ARG_MODE(mode);
3719 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
3720 NOCACHE, segflg, path, fd, cap_rights_init(&rights, CAP_MKDIRAT),
3722 nd.ni_cnd.cn_flags |= WILLBEDIR;
3723 if ((error = namei(&nd)) != 0)
3727 NDFREE(&nd, NDF_ONLY_PNBUF);
3729 * XXX namei called with LOCKPARENT but not LOCKLEAF has
3730 * the strange behaviour of leaving the vnode unlocked
3731 * if the target is the same vnode as the parent.
3733 if (vp == nd.ni_dvp)
3740 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3741 NDFREE(&nd, NDF_ONLY_PNBUF);
3743 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3748 vattr.va_type = VDIR;
3749 vattr.va_mode = (mode & ACCESSPERMS) &~ td->td_proc->p_fd->fd_cmask;
3751 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
3756 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
3760 NDFREE(&nd, NDF_ONLY_PNBUF);
3764 vn_finished_write(mp);
3769 * Remove a directory file.
3771 #ifndef _SYS_SYSPROTO_H_
3779 struct rmdir_args /* {
3784 return (kern_rmdirat(td, AT_FDCWD, uap->path, UIO_USERSPACE));
3788 kern_rmdirat(struct thread *td, int fd, char *path, enum uio_seg pathseg)
3792 struct nameidata nd;
3793 cap_rights_t rights;
3798 NDINIT_ATRIGHTS(&nd, DELETE, LOCKPARENT | LOCKLEAF | AUDITVNODE1,
3799 pathseg, path, fd, cap_rights_init(&rights, CAP_UNLINKAT), td);
3800 if ((error = namei(&nd)) != 0)
3803 if (vp->v_type != VDIR) {
3808 * No rmdir "." please.
3810 if (nd.ni_dvp == vp) {
3815 * The root of a mounted filesystem cannot be deleted.
3817 if (vp->v_vflag & VV_ROOT) {
3822 error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp,
3827 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3828 NDFREE(&nd, NDF_ONLY_PNBUF);
3830 if (nd.ni_dvp == vp)
3834 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3838 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_UNLINK);
3839 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
3840 vn_finished_write(mp);
3842 NDFREE(&nd, NDF_ONLY_PNBUF);
3844 if (nd.ni_dvp == vp)
3853 * Read a block of directory entries in a filesystem independent format.
3855 #ifndef _SYS_SYSPROTO_H_
3856 struct ogetdirentries_args {
3864 ogetdirentries(struct thread *td, struct ogetdirentries_args *uap)
3869 error = kern_ogetdirentries(td, uap, &loff);
3871 error = copyout(&loff, uap->basep, sizeof(long));
3876 kern_ogetdirentries(struct thread *td, struct ogetdirentries_args *uap,
3881 struct uio auio, kuio;
3882 struct iovec aiov, kiov;
3883 struct dirent *dp, *edp;
3884 cap_rights_t rights;
3886 int error, eofflag, readcnt;
3890 /* XXX arbitrary sanity limit on `count'. */
3891 if (uap->count > 64 * 1024)
3893 error = getvnode(td->td_proc->p_fd, uap->fd,
3894 cap_rights_init(&rights, CAP_READ), &fp);
3897 if ((fp->f_flag & FREAD) == 0) {
3902 foffset = foffset_lock(fp, 0);
3904 if (vp->v_type != VDIR) {
3905 foffset_unlock(fp, foffset, 0);
3909 aiov.iov_base = uap->buf;
3910 aiov.iov_len = uap->count;
3911 auio.uio_iov = &aiov;
3912 auio.uio_iovcnt = 1;
3913 auio.uio_rw = UIO_READ;
3914 auio.uio_segflg = UIO_USERSPACE;
3916 auio.uio_resid = uap->count;
3917 vn_lock(vp, LK_SHARED | LK_RETRY);
3918 loff = auio.uio_offset = foffset;
3920 error = mac_vnode_check_readdir(td->td_ucred, vp);
3923 foffset_unlock(fp, foffset, FOF_NOUPDATE);
3928 # if (BYTE_ORDER != LITTLE_ENDIAN)
3929 if (vp->v_mount->mnt_maxsymlinklen <= 0) {
3930 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag,
3932 foffset = auio.uio_offset;
3937 kuio.uio_iov = &kiov;
3938 kuio.uio_segflg = UIO_SYSSPACE;
3939 kiov.iov_len = uap->count;
3940 dirbuf = malloc(uap->count, M_TEMP, M_WAITOK);
3941 kiov.iov_base = dirbuf;
3942 error = VOP_READDIR(vp, &kuio, fp->f_cred, &eofflag,
3944 foffset = kuio.uio_offset;
3946 readcnt = uap->count - kuio.uio_resid;
3947 edp = (struct dirent *)&dirbuf[readcnt];
3948 for (dp = (struct dirent *)dirbuf; dp < edp; ) {
3949 # if (BYTE_ORDER == LITTLE_ENDIAN)
3951 * The expected low byte of
3952 * dp->d_namlen is our dp->d_type.
3953 * The high MBZ byte of dp->d_namlen
3954 * is our dp->d_namlen.
3956 dp->d_type = dp->d_namlen;
3960 * The dp->d_type is the high byte
3961 * of the expected dp->d_namlen,
3962 * so must be zero'ed.
3966 if (dp->d_reclen > 0) {
3967 dp = (struct dirent *)
3968 ((char *)dp + dp->d_reclen);
3975 error = uiomove(dirbuf, readcnt, &auio);
3977 free(dirbuf, M_TEMP);
3981 foffset_unlock(fp, foffset, 0);
3985 if (uap->count == auio.uio_resid &&
3986 (vp->v_vflag & VV_ROOT) &&
3987 (vp->v_mount->mnt_flag & MNT_UNION)) {
3988 struct vnode *tvp = vp;
3989 vp = vp->v_mount->mnt_vnodecovered;
3998 foffset_unlock(fp, foffset, 0);
4000 td->td_retval[0] = uap->count - auio.uio_resid;
4005 #endif /* COMPAT_43 */
4008 * Read a block of directory entries in a filesystem independent format.
4010 #ifndef _SYS_SYSPROTO_H_
4011 struct getdirentries_args {
4019 sys_getdirentries(td, uap)
4021 register struct getdirentries_args /* {
4031 error = kern_getdirentries(td, uap->fd, uap->buf, uap->count, &base,
4032 NULL, UIO_USERSPACE);
4035 if (uap->basep != NULL)
4036 error = copyout(&base, uap->basep, sizeof(long));
4041 kern_getdirentries(struct thread *td, int fd, char *buf, u_int count,
4042 long *basep, ssize_t *residp, enum uio_seg bufseg)
4048 cap_rights_t rights;
4054 if (count > IOSIZE_MAX)
4056 auio.uio_resid = count;
4057 error = getvnode(td->td_proc->p_fd, fd,
4058 cap_rights_init(&rights, CAP_READ), &fp);
4061 if ((fp->f_flag & FREAD) == 0) {
4066 foffset = foffset_lock(fp, 0);
4068 if (vp->v_type != VDIR) {
4072 aiov.iov_base = buf;
4073 aiov.iov_len = count;
4074 auio.uio_iov = &aiov;
4075 auio.uio_iovcnt = 1;
4076 auio.uio_rw = UIO_READ;
4077 auio.uio_segflg = bufseg;
4079 vn_lock(vp, LK_SHARED | LK_RETRY);
4080 AUDIT_ARG_VNODE1(vp);
4081 loff = auio.uio_offset = foffset;
4083 error = mac_vnode_check_readdir(td->td_ucred, vp);
4086 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL,
4088 foffset = auio.uio_offset;
4093 if (count == auio.uio_resid &&
4094 (vp->v_vflag & VV_ROOT) &&
4095 (vp->v_mount->mnt_flag & MNT_UNION)) {
4096 struct vnode *tvp = vp;
4098 vp = vp->v_mount->mnt_vnodecovered;
4109 *residp = auio.uio_resid;
4110 td->td_retval[0] = count - auio.uio_resid;
4112 foffset_unlock(fp, foffset, 0);
4117 #ifndef _SYS_SYSPROTO_H_
4118 struct getdents_args {
4125 sys_getdents(td, uap)
4127 register struct getdents_args /* {
4133 struct getdirentries_args ap;
4137 ap.count = uap->count;
4139 return (sys_getdirentries(td, &ap));
4143 * Set the mode mask for creation of filesystem nodes.
4145 #ifndef _SYS_SYSPROTO_H_
4153 struct umask_args /* {
4157 register struct filedesc *fdp;
4159 FILEDESC_XLOCK(td->td_proc->p_fd);
4160 fdp = td->td_proc->p_fd;
4161 td->td_retval[0] = fdp->fd_cmask;
4162 fdp->fd_cmask = uap->newmask & ALLPERMS;
4163 FILEDESC_XUNLOCK(td->td_proc->p_fd);
4168 * Void all references to file by ripping underlying filesystem away from
4171 #ifndef _SYS_SYSPROTO_H_
4172 struct revoke_args {
4179 register struct revoke_args /* {
4185 struct nameidata nd;
4188 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
4190 if ((error = namei(&nd)) != 0)
4193 NDFREE(&nd, NDF_ONLY_PNBUF);
4194 if (vp->v_type != VCHR || vp->v_rdev == NULL) {
4199 error = mac_vnode_check_revoke(td->td_ucred, vp);
4203 error = VOP_GETATTR(vp, &vattr, td->td_ucred);
4206 if (td->td_ucred->cr_uid != vattr.va_uid) {
4207 error = priv_check(td, PRIV_VFS_ADMIN);
4212 VOP_REVOKE(vp, REVOKEALL);
4219 * Convert a user file descriptor to a kernel file entry and check that, if it
4220 * is a capability, the correct rights are present. A reference on the file
4221 * entry is held upon returning.
4224 getvnode(struct filedesc *fdp, int fd, cap_rights_t *rightsp, struct file **fpp)
4229 error = fget_unlocked(fdp, fd, rightsp, &fp, NULL);
4234 * The file could be not of the vnode type, or it may be not
4235 * yet fully initialized, in which case the f_vnode pointer
4236 * may be set, but f_ops is still badfileops. E.g.,
4237 * devfs_open() transiently create such situation to
4238 * facilitate csw d_fdopen().
4240 * Dupfdopen() handling in kern_openat() installs the
4241 * half-baked file into the process descriptor table, allowing
4242 * other thread to dereference it. Guard against the race by
4245 if (fp->f_vnode == NULL || fp->f_ops == &badfileops) {
4246 fdrop(fp, curthread);
4255 * Get an (NFS) file handle.
4257 #ifndef _SYS_SYSPROTO_H_
4258 struct lgetfh_args {
4266 register struct lgetfh_args *uap;
4268 struct nameidata nd;
4270 register struct vnode *vp;
4273 error = priv_check(td, PRIV_VFS_GETFH);
4276 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
4281 NDFREE(&nd, NDF_ONLY_PNBUF);
4283 bzero(&fh, sizeof(fh));
4284 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
4285 error = VOP_VPTOFH(vp, &fh.fh_fid);
4288 error = copyout(&fh, uap->fhp, sizeof (fh));
4292 #ifndef _SYS_SYSPROTO_H_
4301 register struct getfh_args *uap;
4303 struct nameidata nd;
4305 register struct vnode *vp;
4308 error = priv_check(td, PRIV_VFS_GETFH);
4311 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
4316 NDFREE(&nd, NDF_ONLY_PNBUF);
4318 bzero(&fh, sizeof(fh));
4319 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
4320 error = VOP_VPTOFH(vp, &fh.fh_fid);
4323 error = copyout(&fh, uap->fhp, sizeof (fh));
4328 * syscall for the rpc.lockd to use to translate a NFS file handle into an
4331 * warning: do not remove the priv_check() call or this becomes one giant
4334 #ifndef _SYS_SYSPROTO_H_
4335 struct fhopen_args {
4336 const struct fhandle *u_fhp;
4343 struct fhopen_args /* {
4344 const struct fhandle *u_fhp;
4355 error = priv_check(td, PRIV_VFS_FHOPEN);
4359 fmode = FFLAGS(uap->flags);
4360 /* why not allow a non-read/write open for our lockd? */
4361 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
4363 error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
4366 /* find the mount point */
4367 mp = vfs_busyfs(&fhp.fh_fsid);
4370 /* now give me my vnode, it gets returned to me locked */
4371 error = VFS_FHTOVP(mp, &fhp.fh_fid, LK_EXCLUSIVE, &vp);
4376 error = falloc_noinstall(td, &fp);
4382 * An extra reference on `fp' has been held for us by
4383 * falloc_noinstall().
4389 error = vn_open_vnode(vp, fmode, td->td_ucred, td, fp);
4391 KASSERT(fp->f_ops == &badfileops,
4392 ("VOP_OPEN in fhopen() set f_ops"));
4393 KASSERT(td->td_dupfd < 0,
4394 ("fhopen() encountered fdopen()"));
4404 finit(fp, (fmode & FMASK) | (fp->f_flag & FHASLOCK), DTYPE_VNODE, vp,
4407 if ((fmode & O_TRUNC) != 0) {
4408 error = fo_truncate(fp, 0, td->td_ucred, td);
4413 error = finstall(td, fp, &indx, fmode, NULL);
4416 td->td_retval[0] = indx;
4421 * Stat an (NFS) file handle.
4423 #ifndef _SYS_SYSPROTO_H_
4424 struct fhstat_args {
4425 struct fhandle *u_fhp;
4432 register struct fhstat_args /* {
4433 struct fhandle *u_fhp;
4441 error = copyin(uap->u_fhp, &fh, sizeof(fh));
4444 error = kern_fhstat(td, fh, &sb);
4446 error = copyout(&sb, uap->sb, sizeof(sb));
4451 kern_fhstat(struct thread *td, struct fhandle fh, struct stat *sb)
4457 error = priv_check(td, PRIV_VFS_FHSTAT);
4460 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4462 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_EXCLUSIVE, &vp);
4466 error = vn_stat(vp, sb, td->td_ucred, NOCRED, td);
4472 * Implement fstatfs() for (NFS) file handles.
4474 #ifndef _SYS_SYSPROTO_H_
4475 struct fhstatfs_args {
4476 struct fhandle *u_fhp;
4481 sys_fhstatfs(td, uap)
4483 struct fhstatfs_args /* {
4484 struct fhandle *u_fhp;
4492 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4495 error = kern_fhstatfs(td, fh, &sf);
4498 return (copyout(&sf, uap->buf, sizeof(sf)));
4502 kern_fhstatfs(struct thread *td, fhandle_t fh, struct statfs *buf)
4509 error = priv_check(td, PRIV_VFS_FHSTATFS);
4512 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4514 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_EXCLUSIVE, &vp);
4520 error = prison_canseemount(td->td_ucred, mp);
4524 error = mac_mount_check_stat(td->td_ucred, mp);
4529 * Set these in case the underlying filesystem fails to do so.
4532 sp->f_version = STATFS_VERSION;
4533 sp->f_namemax = NAME_MAX;
4534 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
4535 error = VFS_STATFS(mp, sp);
4544 kern_posix_fallocate(struct thread *td, int fd, off_t offset, off_t len)
4549 cap_rights_t rights;
4550 off_t olen, ooffset;
4553 if (offset < 0 || len <= 0)
4555 /* Check for wrap. */
4556 if (offset > OFF_MAX - len)
4558 error = fget(td, fd, cap_rights_init(&rights, CAP_WRITE), &fp);
4561 if ((fp->f_ops->fo_flags & DFLAG_SEEKABLE) == 0) {
4565 if ((fp->f_flag & FWRITE) == 0) {
4569 if (fp->f_type != DTYPE_VNODE) {
4574 if (vp->v_type != VREG) {
4579 /* Allocating blocks may take a long time, so iterate. */
4586 error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
4589 error = vn_lock(vp, LK_EXCLUSIVE);
4591 vn_finished_write(mp);
4595 error = mac_vnode_check_write(td->td_ucred, fp->f_cred, vp);
4598 error = VOP_ALLOCATE(vp, &offset, &len);
4600 vn_finished_write(mp);
4602 if (olen + ooffset != offset + len) {
4603 panic("offset + len changed from %jx/%jx to %jx/%jx",
4604 ooffset, olen, offset, len);
4606 if (error != 0 || len == 0)
4608 KASSERT(olen > len, ("Iteration did not make progress?"));
4617 sys_posix_fallocate(struct thread *td, struct posix_fallocate_args *uap)
4620 td->td_retval[0] = kern_posix_fallocate(td, uap->fd, uap->offset,
4626 * Unlike madvise(2), we do not make a best effort to remember every
4627 * possible caching hint. Instead, we remember the last setting with
4628 * the exception that we will allow POSIX_FADV_NORMAL to adjust the
4629 * region of any current setting.
4632 kern_posix_fadvise(struct thread *td, int fd, off_t offset, off_t len,
4635 struct fadvise_info *fa, *new;
4638 cap_rights_t rights;
4642 if (offset < 0 || len < 0 || offset > OFF_MAX - len)
4645 case POSIX_FADV_SEQUENTIAL:
4646 case POSIX_FADV_RANDOM:
4647 case POSIX_FADV_NOREUSE:
4648 new = malloc(sizeof(*fa), M_FADVISE, M_WAITOK);
4650 case POSIX_FADV_NORMAL:
4651 case POSIX_FADV_WILLNEED:
4652 case POSIX_FADV_DONTNEED:
4658 /* XXX: CAP_POSIX_FADVISE? */
4659 error = fget(td, fd, cap_rights_init(&rights), &fp);
4662 if ((fp->f_ops->fo_flags & DFLAG_SEEKABLE) == 0) {
4666 if (fp->f_type != DTYPE_VNODE) {
4671 if (vp->v_type != VREG) {
4678 end = offset + len - 1;
4680 case POSIX_FADV_SEQUENTIAL:
4681 case POSIX_FADV_RANDOM:
4682 case POSIX_FADV_NOREUSE:
4684 * Try to merge any existing non-standard region with
4685 * this new region if possible, otherwise create a new
4686 * non-standard region for this request.
4688 mtx_pool_lock(mtxpool_sleep, fp);
4690 if (fa != NULL && fa->fa_advice == advice &&
4691 ((fa->fa_start <= end && fa->fa_end >= offset) ||
4692 (end != OFF_MAX && fa->fa_start == end + 1) ||
4693 (fa->fa_end != OFF_MAX && fa->fa_end + 1 == offset))) {
4694 if (offset < fa->fa_start)
4695 fa->fa_start = offset;
4696 if (end > fa->fa_end)
4699 new->fa_advice = advice;
4700 new->fa_start = offset;
4702 new->fa_prevstart = 0;
4703 new->fa_prevend = 0;
4707 mtx_pool_unlock(mtxpool_sleep, fp);
4709 case POSIX_FADV_NORMAL:
4711 * If a the "normal" region overlaps with an existing
4712 * non-standard region, trim or remove the
4713 * non-standard region.
4715 mtx_pool_lock(mtxpool_sleep, fp);
4718 if (offset <= fa->fa_start && end >= fa->fa_end) {
4720 fp->f_advice = NULL;
4721 } else if (offset <= fa->fa_start &&
4722 end >= fa->fa_start)
4723 fa->fa_start = end + 1;
4724 else if (offset <= fa->fa_end && end >= fa->fa_end)
4725 fa->fa_end = offset - 1;
4726 else if (offset >= fa->fa_start && end <= fa->fa_end) {
4728 * If the "normal" region is a middle
4729 * portion of the existing
4730 * non-standard region, just remove
4731 * the whole thing rather than picking
4732 * one side or the other to
4736 fp->f_advice = NULL;
4739 mtx_pool_unlock(mtxpool_sleep, fp);
4741 case POSIX_FADV_WILLNEED:
4742 case POSIX_FADV_DONTNEED:
4743 error = VOP_ADVISE(vp, offset, end, advice);
4749 free(new, M_FADVISE);
4754 sys_posix_fadvise(struct thread *td, struct posix_fadvise_args *uap)
4757 td->td_retval[0] = kern_posix_fadvise(td, uap->fd, uap->offset,
4758 uap->len, uap->advice);