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 /* {
438 return (kern_getfsstat(td, &uap->buf, uap->bufsize, UIO_USERSPACE,
443 * If (bufsize > 0 && bufseg == UIO_SYSSPACE)
444 * The caller is responsible for freeing memory which will be allocated
448 kern_getfsstat(struct thread *td, struct statfs **buf, size_t bufsize,
449 enum uio_seg bufseg, int flags)
451 struct mount *mp, *nmp;
452 struct statfs *sfsp, *sp, sb;
453 size_t count, maxcount;
456 maxcount = bufsize / sizeof(struct statfs);
459 else if (bufseg == UIO_USERSPACE)
461 else /* if (bufseg == UIO_SYSSPACE) */ {
463 mtx_lock(&mountlist_mtx);
464 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
467 mtx_unlock(&mountlist_mtx);
468 if (maxcount > count)
470 sfsp = *buf = malloc(maxcount * sizeof(struct statfs), M_TEMP,
474 mtx_lock(&mountlist_mtx);
475 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
476 if (prison_canseemount(td->td_ucred, mp) != 0) {
477 nmp = TAILQ_NEXT(mp, mnt_list);
481 if (mac_mount_check_stat(td->td_ucred, mp) != 0) {
482 nmp = TAILQ_NEXT(mp, mnt_list);
486 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK)) {
487 nmp = TAILQ_NEXT(mp, mnt_list);
490 if (sfsp && count < maxcount) {
493 * Set these in case the underlying filesystem
496 sp->f_version = STATFS_VERSION;
497 sp->f_namemax = NAME_MAX;
498 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
500 * If MNT_NOWAIT or MNT_LAZY is specified, do not
501 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
502 * overrides MNT_WAIT.
504 if (((flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
505 (flags & MNT_WAIT)) &&
506 (error = VFS_STATFS(mp, sp))) {
507 mtx_lock(&mountlist_mtx);
508 nmp = TAILQ_NEXT(mp, mnt_list);
512 if (priv_check(td, PRIV_VFS_GENERATION)) {
513 bcopy(sp, &sb, sizeof(sb));
514 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
515 prison_enforce_statfs(td->td_ucred, mp, &sb);
518 if (bufseg == UIO_SYSSPACE)
519 bcopy(sp, sfsp, sizeof(*sp));
520 else /* if (bufseg == UIO_USERSPACE) */ {
521 error = copyout(sp, sfsp, sizeof(*sp));
530 mtx_lock(&mountlist_mtx);
531 nmp = TAILQ_NEXT(mp, mnt_list);
534 mtx_unlock(&mountlist_mtx);
535 if (sfsp && count > maxcount)
536 td->td_retval[0] = maxcount;
538 td->td_retval[0] = count;
542 #ifdef COMPAT_FREEBSD4
544 * Get old format filesystem statistics.
546 static void cvtstatfs(struct statfs *, struct ostatfs *);
548 #ifndef _SYS_SYSPROTO_H_
549 struct freebsd4_statfs_args {
555 freebsd4_statfs(td, uap)
557 struct freebsd4_statfs_args /* {
566 error = kern_statfs(td, uap->path, UIO_USERSPACE, &sf);
569 cvtstatfs(&sf, &osb);
570 return (copyout(&osb, uap->buf, sizeof(osb)));
574 * Get filesystem statistics.
576 #ifndef _SYS_SYSPROTO_H_
577 struct freebsd4_fstatfs_args {
583 freebsd4_fstatfs(td, uap)
585 struct freebsd4_fstatfs_args /* {
594 error = kern_fstatfs(td, uap->fd, &sf);
597 cvtstatfs(&sf, &osb);
598 return (copyout(&osb, uap->buf, sizeof(osb)));
602 * Get statistics on all filesystems.
604 #ifndef _SYS_SYSPROTO_H_
605 struct freebsd4_getfsstat_args {
612 freebsd4_getfsstat(td, uap)
614 register struct freebsd4_getfsstat_args /* {
620 struct statfs *buf, *sp;
625 count = uap->bufsize / sizeof(struct ostatfs);
626 size = count * sizeof(struct statfs);
627 error = kern_getfsstat(td, &buf, size, UIO_SYSSPACE, uap->flags);
629 count = td->td_retval[0];
631 while (count > 0 && error == 0) {
633 error = copyout(&osb, uap->buf, sizeof(osb));
644 * Implement fstatfs() for (NFS) file handles.
646 #ifndef _SYS_SYSPROTO_H_
647 struct freebsd4_fhstatfs_args {
648 struct fhandle *u_fhp;
653 freebsd4_fhstatfs(td, uap)
655 struct freebsd4_fhstatfs_args /* {
656 struct fhandle *u_fhp;
665 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
668 error = kern_fhstatfs(td, fh, &sf);
671 cvtstatfs(&sf, &osb);
672 return (copyout(&osb, uap->buf, sizeof(osb)));
676 * Convert a new format statfs structure to an old format statfs structure.
684 statfs_scale_blocks(nsp, LONG_MAX);
685 bzero(osp, sizeof(*osp));
686 osp->f_bsize = nsp->f_bsize;
687 osp->f_iosize = MIN(nsp->f_iosize, LONG_MAX);
688 osp->f_blocks = nsp->f_blocks;
689 osp->f_bfree = nsp->f_bfree;
690 osp->f_bavail = nsp->f_bavail;
691 osp->f_files = MIN(nsp->f_files, LONG_MAX);
692 osp->f_ffree = MIN(nsp->f_ffree, LONG_MAX);
693 osp->f_owner = nsp->f_owner;
694 osp->f_type = nsp->f_type;
695 osp->f_flags = nsp->f_flags;
696 osp->f_syncwrites = MIN(nsp->f_syncwrites, LONG_MAX);
697 osp->f_asyncwrites = MIN(nsp->f_asyncwrites, LONG_MAX);
698 osp->f_syncreads = MIN(nsp->f_syncreads, LONG_MAX);
699 osp->f_asyncreads = MIN(nsp->f_asyncreads, LONG_MAX);
700 strlcpy(osp->f_fstypename, nsp->f_fstypename,
701 MIN(MFSNAMELEN, OMFSNAMELEN));
702 strlcpy(osp->f_mntonname, nsp->f_mntonname,
703 MIN(MNAMELEN, OMNAMELEN));
704 strlcpy(osp->f_mntfromname, nsp->f_mntfromname,
705 MIN(MNAMELEN, OMNAMELEN));
706 osp->f_fsid = nsp->f_fsid;
708 #endif /* COMPAT_FREEBSD4 */
711 * Change current working directory to a given file descriptor.
713 #ifndef _SYS_SYSPROTO_H_
721 struct fchdir_args /* {
725 register struct filedesc *fdp = td->td_proc->p_fd;
726 struct vnode *vp, *tdp, *vpold;
732 AUDIT_ARG_FD(uap->fd);
733 error = getvnode(fdp, uap->fd, cap_rights_init(&rights, CAP_FCHDIR),
740 vn_lock(vp, LK_SHARED | LK_RETRY);
741 AUDIT_ARG_VNODE1(vp);
742 error = change_dir(vp, td);
743 while (!error && (mp = vp->v_mountedhere) != NULL) {
746 error = VFS_ROOT(mp, LK_SHARED, &tdp);
759 vpold = fdp->fd_cdir;
761 FILEDESC_XUNLOCK(fdp);
767 * Change current working directory (``.'').
769 #ifndef _SYS_SYSPROTO_H_
777 struct chdir_args /* {
782 return (kern_chdir(td, uap->path, UIO_USERSPACE));
786 kern_chdir(struct thread *td, char *path, enum uio_seg pathseg)
788 register struct filedesc *fdp = td->td_proc->p_fd;
793 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
795 if ((error = namei(&nd)) != 0)
797 if ((error = change_dir(nd.ni_vp, td)) != 0) {
799 NDFREE(&nd, NDF_ONLY_PNBUF);
802 VOP_UNLOCK(nd.ni_vp, 0);
803 NDFREE(&nd, NDF_ONLY_PNBUF);
806 fdp->fd_cdir = nd.ni_vp;
807 FILEDESC_XUNLOCK(fdp);
813 * Helper function for raised chroot(2) security function: Refuse if
814 * any filedescriptors are open directories.
817 chroot_refuse_vdir_fds(fdp)
818 struct filedesc *fdp;
824 FILEDESC_LOCK_ASSERT(fdp);
826 for (fd = 0; fd <= fdp->fd_lastfile; fd++) {
827 fp = fget_locked(fdp, fd);
830 if (fp->f_type == DTYPE_VNODE) {
832 if (vp->v_type == VDIR)
840 * This sysctl determines if we will allow a process to chroot(2) if it
841 * has a directory open:
842 * 0: disallowed for all processes.
843 * 1: allowed for processes that were not already chroot(2)'ed.
844 * 2: allowed for all processes.
847 static int chroot_allow_open_directories = 1;
849 SYSCTL_INT(_kern, OID_AUTO, chroot_allow_open_directories, CTLFLAG_RW,
850 &chroot_allow_open_directories, 0,
851 "Allow a process to chroot(2) if it has a directory open");
854 * Change notion of root (``/'') directory.
856 #ifndef _SYS_SYSPROTO_H_
864 struct chroot_args /* {
871 error = priv_check(td, PRIV_VFS_CHROOT);
874 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
875 UIO_USERSPACE, uap->path, td);
879 error = change_dir(nd.ni_vp, td);
883 error = mac_vnode_check_chroot(td->td_ucred, nd.ni_vp);
887 VOP_UNLOCK(nd.ni_vp, 0);
888 error = change_root(nd.ni_vp, td);
890 NDFREE(&nd, NDF_ONLY_PNBUF);
895 NDFREE(&nd, NDF_ONLY_PNBUF);
900 * Common routine for chroot and chdir. Callers must provide a locked vnode
912 ASSERT_VOP_LOCKED(vp, "change_dir(): vp not locked");
913 if (vp->v_type != VDIR)
916 error = mac_vnode_check_chdir(td->td_ucred, vp);
920 return (VOP_ACCESS(vp, VEXEC, td->td_ucred, td));
924 * Common routine for kern_chroot() and jail_attach(). The caller is
925 * responsible for invoking priv_check() and mac_vnode_check_chroot() to
926 * authorize this operation.
933 struct filedesc *fdp;
937 fdp = td->td_proc->p_fd;
939 if (chroot_allow_open_directories == 0 ||
940 (chroot_allow_open_directories == 1 && fdp->fd_rdir != rootvnode)) {
941 error = chroot_refuse_vdir_fds(fdp);
943 FILEDESC_XUNLOCK(fdp);
947 oldvp = fdp->fd_rdir;
954 FILEDESC_XUNLOCK(fdp);
960 flags_to_rights(int flags, cap_rights_t *rightsp)
963 if (flags & O_EXEC) {
964 cap_rights_set(rightsp, CAP_FEXECVE);
966 switch ((flags & O_ACCMODE)) {
968 cap_rights_set(rightsp, CAP_READ);
971 cap_rights_set(rightsp, CAP_READ);
974 cap_rights_set(rightsp, CAP_WRITE);
975 if (!(flags & (O_APPEND | O_TRUNC)))
976 cap_rights_set(rightsp, CAP_SEEK);
982 cap_rights_set(rightsp, CAP_CREATE);
985 cap_rights_set(rightsp, CAP_FTRUNCATE);
987 if (flags & (O_SYNC | O_FSYNC))
988 cap_rights_set(rightsp, CAP_FSYNC);
990 if (flags & (O_EXLOCK | O_SHLOCK))
991 cap_rights_set(rightsp, CAP_FLOCK);
995 * Check permissions, allocate an open file structure, and call the device
996 * open routine if any.
998 #ifndef _SYS_SYSPROTO_H_
1008 register struct open_args /* {
1015 return (kern_openat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1016 uap->flags, uap->mode));
1019 #ifndef _SYS_SYSPROTO_H_
1020 struct openat_args {
1028 sys_openat(struct thread *td, struct openat_args *uap)
1031 return (kern_openat(td, uap->fd, uap->path, UIO_USERSPACE, uap->flag,
1036 kern_openat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1037 int flags, int mode)
1039 struct proc *p = td->td_proc;
1040 struct filedesc *fdp = p->p_fd;
1043 struct nameidata nd;
1044 cap_rights_t rights;
1045 int cmode, error, indx;
1049 AUDIT_ARG_FFLAGS(flags);
1050 AUDIT_ARG_MODE(mode);
1051 /* XXX: audit dirfd */
1052 cap_rights_init(&rights, CAP_LOOKUP);
1053 flags_to_rights(flags, &rights);
1055 * Only one of the O_EXEC, O_RDONLY, O_WRONLY and O_RDWR flags
1058 if (flags & O_EXEC) {
1059 if (flags & O_ACCMODE)
1061 } else if ((flags & O_ACCMODE) == O_ACCMODE) {
1064 flags = FFLAGS(flags);
1068 * Allocate the file descriptor, but don't install a descriptor yet.
1070 error = falloc_noinstall(td, &fp);
1074 * An extra reference on `fp' has been held for us by
1075 * falloc_noinstall().
1077 /* Set the flags early so the finit in devfs can pick them up. */
1078 fp->f_flag = flags & FMASK;
1079 cmode = ((mode & ~fdp->fd_cmask) & ALLPERMS) & ~S_ISTXT;
1080 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, fd,
1082 td->td_dupfd = -1; /* XXX check for fdopen */
1083 error = vn_open(&nd, &flags, cmode, fp);
1086 * If the vn_open replaced the method vector, something
1087 * wonderous happened deep below and we just pass it up
1088 * pretending we know what we do.
1090 if (error == ENXIO && fp->f_ops != &badfileops)
1094 * Handle special fdopen() case. bleh.
1096 * Don't do this for relative (capability) lookups; we don't
1097 * understand exactly what would happen, and we don't think
1098 * that it ever should.
1100 if (nd.ni_strictrelative == 0 &&
1101 (error == ENODEV || error == ENXIO) &&
1102 td->td_dupfd >= 0) {
1103 error = dupfdopen(td, fdp, td->td_dupfd, flags, error,
1112 NDFREE(&nd, NDF_ONLY_PNBUF);
1116 * Store the vnode, for any f_type. Typically, the vnode use
1117 * count is decremented by direct call to vn_closefile() for
1118 * files that switched type in the cdevsw fdopen() method.
1122 * If the file wasn't claimed by devfs bind it to the normal
1123 * vnode operations here.
1125 if (fp->f_ops == &badfileops) {
1126 KASSERT(vp->v_type != VFIFO, ("Unexpected fifo."));
1128 finit(fp, (flags & FMASK) | (fp->f_flag & FHASLOCK),
1129 DTYPE_VNODE, vp, &vnops);
1133 if (flags & O_TRUNC) {
1134 error = fo_truncate(fp, 0, td->td_ucred, td);
1140 * If we haven't already installed the FD (for dupfdopen), do so now.
1143 struct filecaps *fcaps;
1146 if (nd.ni_strictrelative == 1)
1147 fcaps = &nd.ni_filecaps;
1151 error = finstall(td, fp, &indx, flags, fcaps);
1152 /* On success finstall() consumes fcaps. */
1154 filecaps_free(&nd.ni_filecaps);
1158 filecaps_free(&nd.ni_filecaps);
1162 * Release our private reference, leaving the one associated with
1163 * the descriptor table intact.
1166 td->td_retval[0] = indx;
1169 KASSERT(indx == -1, ("indx=%d, should be -1", indx));
1178 #ifndef _SYS_SYSPROTO_H_
1179 struct ocreat_args {
1187 register struct ocreat_args /* {
1193 return (kern_openat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1194 O_WRONLY | O_CREAT | O_TRUNC, uap->mode));
1196 #endif /* COMPAT_43 */
1199 * Create a special file.
1201 #ifndef _SYS_SYSPROTO_H_
1211 register struct mknod_args /* {
1218 return (kern_mknodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1219 uap->mode, uap->dev));
1222 #ifndef _SYS_SYSPROTO_H_
1223 struct mknodat_args {
1231 sys_mknodat(struct thread *td, struct mknodat_args *uap)
1234 return (kern_mknodat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode,
1239 kern_mknodat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1245 struct nameidata nd;
1246 cap_rights_t rights;
1247 int error, whiteout = 0;
1249 AUDIT_ARG_MODE(mode);
1251 switch (mode & S_IFMT) {
1254 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
1257 error = priv_check(td, PRIV_VFS_MKNOD_BAD);
1260 error = priv_check(td, PRIV_VFS_MKNOD_WHT);
1264 return (kern_mkfifoat(td, fd, path, pathseg, mode));
1274 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
1275 NOCACHE, pathseg, path, fd, cap_rights_init(&rights, CAP_MKNODAT),
1277 if ((error = namei(&nd)) != 0)
1281 NDFREE(&nd, NDF_ONLY_PNBUF);
1282 if (vp == nd.ni_dvp)
1290 vattr.va_mode = (mode & ALLPERMS) &
1291 ~td->td_proc->p_fd->fd_cmask;
1292 vattr.va_rdev = dev;
1295 switch (mode & S_IFMT) {
1296 case S_IFMT: /* used by badsect to flag bad sectors */
1297 vattr.va_type = VBAD;
1300 vattr.va_type = VCHR;
1303 vattr.va_type = VBLK;
1309 panic("kern_mknod: invalid mode");
1312 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1313 NDFREE(&nd, NDF_ONLY_PNBUF);
1315 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1320 if (error == 0 && !whiteout)
1321 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp,
1322 &nd.ni_cnd, &vattr);
1326 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, CREATE);
1328 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp,
1329 &nd.ni_cnd, &vattr);
1334 NDFREE(&nd, NDF_ONLY_PNBUF);
1336 vn_finished_write(mp);
1341 * Create a named pipe.
1343 #ifndef _SYS_SYSPROTO_H_
1344 struct mkfifo_args {
1352 register struct mkfifo_args /* {
1358 return (kern_mkfifoat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1362 #ifndef _SYS_SYSPROTO_H_
1363 struct mkfifoat_args {
1370 sys_mkfifoat(struct thread *td, struct mkfifoat_args *uap)
1373 return (kern_mkfifoat(td, uap->fd, uap->path, UIO_USERSPACE,
1378 kern_mkfifoat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1383 struct nameidata nd;
1384 cap_rights_t rights;
1387 AUDIT_ARG_MODE(mode);
1390 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
1391 NOCACHE, pathseg, path, fd, cap_rights_init(&rights, CAP_MKFIFOAT),
1393 if ((error = namei(&nd)) != 0)
1395 if (nd.ni_vp != NULL) {
1396 NDFREE(&nd, NDF_ONLY_PNBUF);
1397 if (nd.ni_vp == nd.ni_dvp)
1404 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1405 NDFREE(&nd, NDF_ONLY_PNBUF);
1407 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1412 vattr.va_type = VFIFO;
1413 vattr.va_mode = (mode & ALLPERMS) & ~td->td_proc->p_fd->fd_cmask;
1415 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1420 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
1427 vn_finished_write(mp);
1428 NDFREE(&nd, NDF_ONLY_PNBUF);
1433 * Make a hard file link.
1435 #ifndef _SYS_SYSPROTO_H_
1444 register struct link_args /* {
1450 return (kern_linkat(td, AT_FDCWD, AT_FDCWD, uap->path, uap->link,
1451 UIO_USERSPACE, FOLLOW));
1454 #ifndef _SYS_SYSPROTO_H_
1455 struct linkat_args {
1464 sys_linkat(struct thread *td, struct linkat_args *uap)
1469 if (flag & ~AT_SYMLINK_FOLLOW)
1472 return (kern_linkat(td, uap->fd1, uap->fd2, uap->path1, uap->path2,
1473 UIO_USERSPACE, (flag & AT_SYMLINK_FOLLOW) ? FOLLOW : NOFOLLOW));
1476 int hardlink_check_uid = 0;
1477 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
1478 &hardlink_check_uid, 0,
1479 "Unprivileged processes cannot create hard links to files owned by other "
1481 static int hardlink_check_gid = 0;
1482 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
1483 &hardlink_check_gid, 0,
1484 "Unprivileged processes cannot create hard links to files owned by other "
1488 can_hardlink(struct vnode *vp, struct ucred *cred)
1493 if (!hardlink_check_uid && !hardlink_check_gid)
1496 error = VOP_GETATTR(vp, &va, cred);
1500 if (hardlink_check_uid && cred->cr_uid != va.va_uid) {
1501 error = priv_check_cred(cred, PRIV_VFS_LINK, 0);
1506 if (hardlink_check_gid && !groupmember(va.va_gid, cred)) {
1507 error = priv_check_cred(cred, PRIV_VFS_LINK, 0);
1516 kern_linkat(struct thread *td, int fd1, int fd2, char *path1, char *path2,
1517 enum uio_seg segflg, int follow)
1521 struct nameidata nd;
1522 cap_rights_t rights;
1527 NDINIT_AT(&nd, LOOKUP, follow | AUDITVNODE1, segflg, path1, fd1, td);
1529 if ((error = namei(&nd)) != 0)
1531 NDFREE(&nd, NDF_ONLY_PNBUF);
1533 if (vp->v_type == VDIR) {
1535 return (EPERM); /* POSIX */
1537 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE2 |
1538 NOCACHE, segflg, path2, fd2, cap_rights_init(&rights, CAP_LINKAT),
1540 if ((error = namei(&nd)) == 0) {
1541 if (nd.ni_vp != NULL) {
1542 NDFREE(&nd, NDF_ONLY_PNBUF);
1543 if (nd.ni_dvp == nd.ni_vp)
1550 } else if (nd.ni_dvp->v_mount != vp->v_mount) {
1552 * Cross-device link. No need to recheck
1553 * vp->v_type, since it cannot change, except
1556 NDFREE(&nd, NDF_ONLY_PNBUF);
1560 } else if ((error = vn_lock(vp, LK_EXCLUSIVE)) == 0) {
1561 error = can_hardlink(vp, td->td_ucred);
1564 error = mac_vnode_check_link(td->td_ucred,
1565 nd.ni_dvp, vp, &nd.ni_cnd);
1570 NDFREE(&nd, NDF_ONLY_PNBUF);
1573 error = vn_start_write(vp, &mp, V_NOWAIT);
1577 NDFREE(&nd, NDF_ONLY_PNBUF);
1578 error = vn_start_write(NULL, &mp,
1584 error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
1587 vn_finished_write(mp);
1588 NDFREE(&nd, NDF_ONLY_PNBUF);
1591 NDFREE(&nd, NDF_ONLY_PNBUF);
1601 * Make a symbolic link.
1603 #ifndef _SYS_SYSPROTO_H_
1604 struct symlink_args {
1610 sys_symlink(td, uap)
1612 register struct symlink_args /* {
1618 return (kern_symlinkat(td, uap->path, AT_FDCWD, uap->link,
1622 #ifndef _SYS_SYSPROTO_H_
1623 struct symlinkat_args {
1630 sys_symlinkat(struct thread *td, struct symlinkat_args *uap)
1633 return (kern_symlinkat(td, uap->path1, uap->fd, uap->path2,
1638 kern_symlinkat(struct thread *td, char *path1, int fd, char *path2,
1639 enum uio_seg segflg)
1644 struct nameidata nd;
1646 cap_rights_t rights;
1648 if (segflg == UIO_SYSSPACE) {
1651 syspath = uma_zalloc(namei_zone, M_WAITOK);
1652 if ((error = copyinstr(path1, syspath, MAXPATHLEN, NULL)) != 0)
1655 AUDIT_ARG_TEXT(syspath);
1658 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
1659 NOCACHE, segflg, path2, fd, cap_rights_init(&rights, CAP_SYMLINKAT),
1661 if ((error = namei(&nd)) != 0)
1664 NDFREE(&nd, NDF_ONLY_PNBUF);
1665 if (nd.ni_vp == nd.ni_dvp)
1673 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1674 NDFREE(&nd, NDF_ONLY_PNBUF);
1676 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1681 vattr.va_mode = ACCESSPERMS &~ td->td_proc->p_fd->fd_cmask;
1683 vattr.va_type = VLNK;
1684 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1689 error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr, syspath);
1695 NDFREE(&nd, NDF_ONLY_PNBUF);
1697 vn_finished_write(mp);
1699 if (segflg != UIO_SYSSPACE)
1700 uma_zfree(namei_zone, syspath);
1705 * Delete a whiteout from the filesystem.
1708 sys_undelete(td, uap)
1710 register struct undelete_args /* {
1715 struct nameidata nd;
1720 NDINIT(&nd, DELETE, LOCKPARENT | DOWHITEOUT | AUDITVNODE1,
1721 UIO_USERSPACE, uap->path, td);
1726 if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & ISWHITEOUT)) {
1727 NDFREE(&nd, NDF_ONLY_PNBUF);
1728 if (nd.ni_vp == nd.ni_dvp)
1736 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1737 NDFREE(&nd, NDF_ONLY_PNBUF);
1739 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1743 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, DELETE);
1744 NDFREE(&nd, NDF_ONLY_PNBUF);
1746 vn_finished_write(mp);
1751 * Delete a name from the filesystem.
1753 #ifndef _SYS_SYSPROTO_H_
1754 struct unlink_args {
1761 struct unlink_args /* {
1766 return (kern_unlinkat(td, AT_FDCWD, uap->path, UIO_USERSPACE, 0));
1769 #ifndef _SYS_SYSPROTO_H_
1770 struct unlinkat_args {
1777 sys_unlinkat(struct thread *td, struct unlinkat_args *uap)
1779 int flag = uap->flag;
1781 char *path = uap->path;
1783 if (flag & ~AT_REMOVEDIR)
1786 if (flag & AT_REMOVEDIR)
1787 return (kern_rmdirat(td, fd, path, UIO_USERSPACE));
1789 return (kern_unlinkat(td, fd, path, UIO_USERSPACE, 0));
1793 kern_unlinkat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1798 struct nameidata nd;
1800 cap_rights_t rights;
1805 NDINIT_ATRIGHTS(&nd, DELETE, LOCKPARENT | LOCKLEAF | AUDITVNODE1,
1806 pathseg, path, fd, cap_rights_init(&rights, CAP_UNLINKAT), td);
1807 if ((error = namei(&nd)) != 0)
1808 return (error == EINVAL ? EPERM : error);
1810 if (vp->v_type == VDIR && oldinum == 0) {
1811 error = EPERM; /* POSIX */
1812 } else if (oldinum != 0 &&
1813 ((error = vn_stat(vp, &sb, td->td_ucred, NOCRED, td)) == 0) &&
1814 sb.st_ino != oldinum) {
1815 error = EIDRM; /* Identifier removed */
1818 * The root of a mounted filesystem cannot be deleted.
1820 * XXX: can this only be a VDIR case?
1822 if (vp->v_vflag & VV_ROOT)
1826 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1827 NDFREE(&nd, NDF_ONLY_PNBUF);
1829 if (vp == nd.ni_dvp)
1833 if ((error = vn_start_write(NULL, &mp,
1834 V_XSLEEP | PCATCH)) != 0)
1839 error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp,
1844 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_UNLINK);
1845 error = VOP_REMOVE(nd.ni_dvp, vp, &nd.ni_cnd);
1849 vn_finished_write(mp);
1851 NDFREE(&nd, NDF_ONLY_PNBUF);
1853 if (vp == nd.ni_dvp)
1861 * Reposition read/write file offset.
1863 #ifndef _SYS_SYSPROTO_H_
1874 register struct lseek_args /* {
1882 cap_rights_t rights;
1885 AUDIT_ARG_FD(uap->fd);
1886 error = fget(td, uap->fd, cap_rights_init(&rights, CAP_SEEK), &fp);
1889 error = (fp->f_ops->fo_flags & DFLAG_SEEKABLE) != 0 ?
1890 fo_seek(fp, uap->offset, uap->whence, td) : ESPIPE;
1895 #if defined(COMPAT_43)
1897 * Reposition read/write file offset.
1899 #ifndef _SYS_SYSPROTO_H_
1900 struct olseek_args {
1909 register struct olseek_args /* {
1915 struct lseek_args /* {
1923 nuap.offset = uap->offset;
1924 nuap.whence = uap->whence;
1925 return (sys_lseek(td, &nuap));
1927 #endif /* COMPAT_43 */
1929 /* Version with the 'pad' argument */
1931 freebsd6_lseek(td, uap)
1933 register struct freebsd6_lseek_args *uap;
1935 struct lseek_args ouap;
1938 ouap.offset = uap->offset;
1939 ouap.whence = uap->whence;
1940 return (sys_lseek(td, &ouap));
1944 * Check access permissions using passed credentials.
1947 vn_access(vp, user_flags, cred, td)
1956 /* Flags == 0 means only check for existence. */
1957 if (user_flags == 0)
1961 if (user_flags & R_OK)
1963 if (user_flags & W_OK)
1965 if (user_flags & X_OK)
1968 error = mac_vnode_check_access(cred, vp, accmode);
1972 if ((accmode & VWRITE) == 0 || (error = vn_writechk(vp)) == 0)
1973 error = VOP_ACCESS(vp, accmode, cred, td);
1978 * Check access permissions using "real" credentials.
1980 #ifndef _SYS_SYSPROTO_H_
1981 struct access_args {
1989 register struct access_args /* {
1995 return (kern_accessat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1999 #ifndef _SYS_SYSPROTO_H_
2000 struct faccessat_args {
2008 sys_faccessat(struct thread *td, struct faccessat_args *uap)
2011 return (kern_accessat(td, uap->fd, uap->path, UIO_USERSPACE, uap->flag,
2016 kern_accessat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2017 int flag, int amode)
2019 struct ucred *cred, *usecred;
2021 struct nameidata nd;
2022 cap_rights_t rights;
2025 if (flag & ~AT_EACCESS)
2027 if (amode != F_OK && (amode & ~(R_OK | W_OK | X_OK)) != 0)
2031 * Create and modify a temporary credential instead of one that
2032 * is potentially shared (if we need one).
2034 cred = td->td_ucred;
2035 if ((flag & AT_EACCESS) == 0 &&
2036 ((cred->cr_uid != cred->cr_ruid ||
2037 cred->cr_rgid != cred->cr_groups[0]))) {
2038 usecred = crdup(cred);
2039 usecred->cr_uid = cred->cr_ruid;
2040 usecred->cr_groups[0] = cred->cr_rgid;
2041 td->td_ucred = usecred;
2044 AUDIT_ARG_VALUE(amode);
2045 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF |
2046 AUDITVNODE1, pathseg, path, fd, cap_rights_init(&rights, CAP_FSTAT),
2048 if ((error = namei(&nd)) != 0)
2052 error = vn_access(vp, amode, usecred, td);
2053 NDFREE(&nd, NDF_ONLY_PNBUF);
2056 if (usecred != cred) {
2057 td->td_ucred = cred;
2064 * Check access permissions using "effective" credentials.
2066 #ifndef _SYS_SYSPROTO_H_
2067 struct eaccess_args {
2073 sys_eaccess(td, uap)
2075 register struct eaccess_args /* {
2081 return (kern_accessat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2082 AT_EACCESS, uap->amode));
2085 #if defined(COMPAT_43)
2087 * Get file status; this version follows links.
2089 #ifndef _SYS_SYSPROTO_H_
2098 register struct ostat_args /* {
2107 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
2112 return (copyout(&osb, uap->ub, sizeof (osb)));
2116 * Get file status; this version does not follow links.
2118 #ifndef _SYS_SYSPROTO_H_
2119 struct olstat_args {
2127 register struct olstat_args /* {
2136 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2137 UIO_USERSPACE, &sb, NULL);
2141 return (copyout(&osb, uap->ub, sizeof (osb)));
2145 * Convert from an old to a new stat structure.
2153 ost->st_dev = st->st_dev;
2154 ost->st_ino = st->st_ino;
2155 ost->st_mode = st->st_mode;
2156 ost->st_nlink = st->st_nlink;
2157 ost->st_uid = st->st_uid;
2158 ost->st_gid = st->st_gid;
2159 ost->st_rdev = st->st_rdev;
2160 if (st->st_size < (quad_t)1 << 32)
2161 ost->st_size = st->st_size;
2164 ost->st_atim = st->st_atim;
2165 ost->st_mtim = st->st_mtim;
2166 ost->st_ctim = st->st_ctim;
2167 ost->st_blksize = st->st_blksize;
2168 ost->st_blocks = st->st_blocks;
2169 ost->st_flags = st->st_flags;
2170 ost->st_gen = st->st_gen;
2172 #endif /* COMPAT_43 */
2175 * Get file status; this version follows links.
2177 #ifndef _SYS_SYSPROTO_H_
2186 register struct stat_args /* {
2194 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
2197 error = copyout(&sb, uap->ub, sizeof (sb));
2201 #ifndef _SYS_SYSPROTO_H_
2202 struct fstatat_args {
2210 sys_fstatat(struct thread *td, struct fstatat_args *uap)
2215 error = kern_statat(td, uap->flag, uap->fd, uap->path,
2216 UIO_USERSPACE, &sb, NULL);
2218 error = copyout(&sb, uap->buf, sizeof (sb));
2223 kern_statat(struct thread *td, int flag, int fd, char *path,
2224 enum uio_seg pathseg, struct stat *sbp,
2225 void (*hook)(struct vnode *vp, struct stat *sbp))
2227 struct nameidata nd;
2229 cap_rights_t rights;
2232 if (flag & ~AT_SYMLINK_NOFOLLOW)
2235 NDINIT_ATRIGHTS(&nd, LOOKUP, ((flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW :
2236 FOLLOW) | LOCKSHARED | LOCKLEAF | AUDITVNODE1, pathseg, path, fd,
2237 cap_rights_init(&rights, CAP_FSTAT), td);
2239 if ((error = namei(&nd)) != 0)
2241 error = vn_stat(nd.ni_vp, &sb, td->td_ucred, NOCRED, td);
2243 SDT_PROBE(vfs, , stat, mode, path, sb.st_mode, 0, 0, 0);
2244 if (S_ISREG(sb.st_mode))
2245 SDT_PROBE(vfs, , stat, reg, path, pathseg, 0, 0, 0);
2246 if (__predict_false(hook != NULL))
2247 hook(nd.ni_vp, &sb);
2249 NDFREE(&nd, NDF_ONLY_PNBUF);
2255 if (KTRPOINT(td, KTR_STRUCT))
2262 * Get file status; this version does not follow links.
2264 #ifndef _SYS_SYSPROTO_H_
2273 register struct lstat_args /* {
2281 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2282 UIO_USERSPACE, &sb, NULL);
2284 error = copyout(&sb, uap->ub, sizeof (sb));
2289 * Implementation of the NetBSD [l]stat() functions.
2297 bzero(nsb, sizeof *nsb);
2298 nsb->st_dev = sb->st_dev;
2299 nsb->st_ino = sb->st_ino;
2300 nsb->st_mode = sb->st_mode;
2301 nsb->st_nlink = sb->st_nlink;
2302 nsb->st_uid = sb->st_uid;
2303 nsb->st_gid = sb->st_gid;
2304 nsb->st_rdev = sb->st_rdev;
2305 nsb->st_atim = sb->st_atim;
2306 nsb->st_mtim = sb->st_mtim;
2307 nsb->st_ctim = sb->st_ctim;
2308 nsb->st_size = sb->st_size;
2309 nsb->st_blocks = sb->st_blocks;
2310 nsb->st_blksize = sb->st_blksize;
2311 nsb->st_flags = sb->st_flags;
2312 nsb->st_gen = sb->st_gen;
2313 nsb->st_birthtim = sb->st_birthtim;
2316 #ifndef _SYS_SYSPROTO_H_
2325 register struct nstat_args /* {
2334 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
2338 cvtnstat(&sb, &nsb);
2339 return (copyout(&nsb, uap->ub, sizeof (nsb)));
2343 * NetBSD lstat. Get file status; this version does not follow links.
2345 #ifndef _SYS_SYSPROTO_H_
2354 register struct nlstat_args /* {
2363 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2364 UIO_USERSPACE, &sb, NULL);
2367 cvtnstat(&sb, &nsb);
2368 return (copyout(&nsb, uap->ub, sizeof (nsb)));
2372 * Get configurable pathname variables.
2374 #ifndef _SYS_SYSPROTO_H_
2375 struct pathconf_args {
2381 sys_pathconf(td, uap)
2383 register struct pathconf_args /* {
2389 return (kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name, FOLLOW));
2392 #ifndef _SYS_SYSPROTO_H_
2393 struct lpathconf_args {
2399 sys_lpathconf(td, uap)
2401 register struct lpathconf_args /* {
2407 return (kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name,
2412 kern_pathconf(struct thread *td, char *path, enum uio_seg pathseg, int name,
2415 struct nameidata nd;
2418 NDINIT(&nd, LOOKUP, LOCKSHARED | LOCKLEAF | AUDITVNODE1 | flags,
2420 if ((error = namei(&nd)) != 0)
2422 NDFREE(&nd, NDF_ONLY_PNBUF);
2424 /* If asynchronous I/O is available, it works for all files. */
2425 if (name == _PC_ASYNC_IO)
2426 td->td_retval[0] = async_io_version;
2428 error = VOP_PATHCONF(nd.ni_vp, name, td->td_retval);
2434 * Return target name of a symbolic link.
2436 #ifndef _SYS_SYSPROTO_H_
2437 struct readlink_args {
2444 sys_readlink(td, uap)
2446 register struct readlink_args /* {
2453 return (kern_readlinkat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2454 uap->buf, UIO_USERSPACE, uap->count));
2456 #ifndef _SYS_SYSPROTO_H_
2457 struct readlinkat_args {
2465 sys_readlinkat(struct thread *td, struct readlinkat_args *uap)
2468 return (kern_readlinkat(td, uap->fd, uap->path, UIO_USERSPACE,
2469 uap->buf, UIO_USERSPACE, uap->bufsize));
2473 kern_readlinkat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2474 char *buf, enum uio_seg bufseg, size_t count)
2479 struct nameidata nd;
2482 if (count > IOSIZE_MAX)
2485 NDINIT_AT(&nd, LOOKUP, NOFOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
2486 pathseg, path, fd, td);
2488 if ((error = namei(&nd)) != 0)
2490 NDFREE(&nd, NDF_ONLY_PNBUF);
2493 error = mac_vnode_check_readlink(td->td_ucred, vp);
2499 if (vp->v_type != VLNK)
2502 aiov.iov_base = buf;
2503 aiov.iov_len = count;
2504 auio.uio_iov = &aiov;
2505 auio.uio_iovcnt = 1;
2506 auio.uio_offset = 0;
2507 auio.uio_rw = UIO_READ;
2508 auio.uio_segflg = bufseg;
2510 auio.uio_resid = count;
2511 error = VOP_READLINK(vp, &auio, td->td_ucred);
2512 td->td_retval[0] = count - auio.uio_resid;
2519 * Common implementation code for chflags() and fchflags().
2522 setfflags(td, vp, flags)
2531 /* We can't support the value matching VNOVAL. */
2532 if (flags == VNOVAL)
2533 return (EOPNOTSUPP);
2536 * Prevent non-root users from setting flags on devices. When
2537 * a device is reused, users can retain ownership of the device
2538 * if they are allowed to set flags and programs assume that
2539 * chown can't fail when done as root.
2541 if (vp->v_type == VCHR || vp->v_type == VBLK) {
2542 error = priv_check(td, PRIV_VFS_CHFLAGS_DEV);
2547 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2550 vattr.va_flags = flags;
2551 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2553 error = mac_vnode_check_setflags(td->td_ucred, vp, vattr.va_flags);
2556 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
2558 vn_finished_write(mp);
2563 * Change flags of a file given a path name.
2565 #ifndef _SYS_SYSPROTO_H_
2566 struct chflags_args {
2572 sys_chflags(td, uap)
2574 register struct chflags_args /* {
2580 return (kern_chflagsat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2584 #ifndef _SYS_SYSPROTO_H_
2585 struct chflagsat_args {
2593 sys_chflagsat(struct thread *td, struct chflagsat_args *uap)
2596 const char *path = uap->path;
2597 u_long flags = uap->flags;
2598 int atflag = uap->atflag;
2600 if (atflag & ~AT_SYMLINK_NOFOLLOW)
2603 return (kern_chflagsat(td, fd, path, UIO_USERSPACE, flags, atflag));
2607 * Same as chflags() but doesn't follow symlinks.
2610 sys_lchflags(td, uap)
2612 register struct lchflags_args /* {
2618 return (kern_chflagsat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2619 uap->flags, AT_SYMLINK_NOFOLLOW));
2623 kern_chflagsat(struct thread *td, int fd, const char *path,
2624 enum uio_seg pathseg, u_long flags, int atflag)
2626 struct nameidata nd;
2627 cap_rights_t rights;
2630 AUDIT_ARG_FFLAGS(flags);
2631 follow = (atflag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
2632 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, pathseg, path, fd,
2633 cap_rights_init(&rights, CAP_FCHFLAGS), td);
2634 if ((error = namei(&nd)) != 0)
2636 NDFREE(&nd, NDF_ONLY_PNBUF);
2637 error = setfflags(td, nd.ni_vp, flags);
2643 * Change flags of a file given a file descriptor.
2645 #ifndef _SYS_SYSPROTO_H_
2646 struct fchflags_args {
2652 sys_fchflags(td, uap)
2654 register struct fchflags_args /* {
2660 cap_rights_t rights;
2663 AUDIT_ARG_FD(uap->fd);
2664 AUDIT_ARG_FFLAGS(uap->flags);
2665 error = getvnode(td->td_proc->p_fd, uap->fd,
2666 cap_rights_init(&rights, CAP_FCHFLAGS), &fp);
2670 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
2671 AUDIT_ARG_VNODE1(fp->f_vnode);
2672 VOP_UNLOCK(fp->f_vnode, 0);
2674 error = setfflags(td, fp->f_vnode, uap->flags);
2680 * Common implementation code for chmod(), lchmod() and fchmod().
2683 setfmode(td, cred, vp, mode)
2693 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2695 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2697 vattr.va_mode = mode & ALLPERMS;
2699 error = mac_vnode_check_setmode(cred, vp, vattr.va_mode);
2702 error = VOP_SETATTR(vp, &vattr, cred);
2704 vn_finished_write(mp);
2709 * Change mode of a file given path name.
2711 #ifndef _SYS_SYSPROTO_H_
2720 register struct chmod_args /* {
2726 return (kern_fchmodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2730 #ifndef _SYS_SYSPROTO_H_
2731 struct fchmodat_args {
2739 sys_fchmodat(struct thread *td, struct fchmodat_args *uap)
2741 int flag = uap->flag;
2743 char *path = uap->path;
2744 mode_t mode = uap->mode;
2746 if (flag & ~AT_SYMLINK_NOFOLLOW)
2749 return (kern_fchmodat(td, fd, path, UIO_USERSPACE, mode, flag));
2753 * Change mode of a file given path name (don't follow links.)
2755 #ifndef _SYS_SYSPROTO_H_
2756 struct lchmod_args {
2764 register struct lchmod_args /* {
2770 return (kern_fchmodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2771 uap->mode, AT_SYMLINK_NOFOLLOW));
2775 kern_fchmodat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2776 mode_t mode, int flag)
2778 struct nameidata nd;
2779 cap_rights_t rights;
2782 AUDIT_ARG_MODE(mode);
2783 follow = (flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
2784 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, pathseg, path, fd,
2785 cap_rights_init(&rights, CAP_FCHMOD), td);
2786 if ((error = namei(&nd)) != 0)
2788 NDFREE(&nd, NDF_ONLY_PNBUF);
2789 error = setfmode(td, td->td_ucred, nd.ni_vp, mode);
2795 * Change mode of a file given a file descriptor.
2797 #ifndef _SYS_SYSPROTO_H_
2798 struct fchmod_args {
2804 sys_fchmod(struct thread *td, struct fchmod_args *uap)
2807 cap_rights_t rights;
2810 AUDIT_ARG_FD(uap->fd);
2811 AUDIT_ARG_MODE(uap->mode);
2813 error = fget(td, uap->fd, cap_rights_init(&rights, CAP_FCHMOD), &fp);
2816 error = fo_chmod(fp, uap->mode, td->td_ucred, td);
2822 * Common implementation for chown(), lchown(), and fchown()
2825 setfown(td, cred, vp, uid, gid)
2836 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2838 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2843 error = mac_vnode_check_setowner(cred, vp, vattr.va_uid,
2847 error = VOP_SETATTR(vp, &vattr, cred);
2849 vn_finished_write(mp);
2854 * Set ownership given a path name.
2856 #ifndef _SYS_SYSPROTO_H_
2866 register struct chown_args /* {
2873 return (kern_fchownat(td, AT_FDCWD, uap->path, UIO_USERSPACE, uap->uid,
2877 #ifndef _SYS_SYSPROTO_H_
2878 struct fchownat_args {
2887 sys_fchownat(struct thread *td, struct fchownat_args *uap)
2892 if (flag & ~AT_SYMLINK_NOFOLLOW)
2895 return (kern_fchownat(td, uap->fd, uap->path, UIO_USERSPACE, uap->uid,
2896 uap->gid, uap->flag));
2900 kern_fchownat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2901 int uid, int gid, int flag)
2903 struct nameidata nd;
2904 cap_rights_t rights;
2907 AUDIT_ARG_OWNER(uid, gid);
2908 follow = (flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
2909 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, pathseg, path, fd,
2910 cap_rights_init(&rights, CAP_FCHOWN), td);
2912 if ((error = namei(&nd)) != 0)
2914 NDFREE(&nd, NDF_ONLY_PNBUF);
2915 error = setfown(td, td->td_ucred, nd.ni_vp, uid, gid);
2921 * Set ownership given a path name, do not cross symlinks.
2923 #ifndef _SYS_SYSPROTO_H_
2924 struct lchown_args {
2933 register struct lchown_args /* {
2940 return (kern_fchownat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2941 uap->uid, uap->gid, AT_SYMLINK_NOFOLLOW));
2945 * Set ownership given a file descriptor.
2947 #ifndef _SYS_SYSPROTO_H_
2948 struct fchown_args {
2957 register struct fchown_args /* {
2964 cap_rights_t rights;
2967 AUDIT_ARG_FD(uap->fd);
2968 AUDIT_ARG_OWNER(uap->uid, uap->gid);
2969 error = fget(td, uap->fd, cap_rights_init(&rights, CAP_FCHOWN), &fp);
2972 error = fo_chown(fp, uap->uid, uap->gid, td->td_ucred, td);
2978 * Common implementation code for utimes(), lutimes(), and futimes().
2981 getutimes(usrtvp, tvpseg, tsp)
2982 const struct timeval *usrtvp;
2983 enum uio_seg tvpseg;
2984 struct timespec *tsp;
2986 struct timeval tv[2];
2987 const struct timeval *tvp;
2990 if (usrtvp == NULL) {
2991 vfs_timestamp(&tsp[0]);
2994 if (tvpseg == UIO_SYSSPACE) {
2997 if ((error = copyin(usrtvp, tv, sizeof(tv))) != 0)
3002 if (tvp[0].tv_usec < 0 || tvp[0].tv_usec >= 1000000 ||
3003 tvp[1].tv_usec < 0 || tvp[1].tv_usec >= 1000000)
3005 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
3006 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
3012 * Common implementation code for futimens(), utimensat().
3014 #define UTIMENS_NULL 0x1
3015 #define UTIMENS_EXIT 0x2
3017 getutimens(const struct timespec *usrtsp, enum uio_seg tspseg,
3018 struct timespec *tsp, int *retflags)
3020 struct timespec tsnow;
3023 vfs_timestamp(&tsnow);
3025 if (usrtsp == NULL) {
3028 *retflags |= UTIMENS_NULL;
3031 if (tspseg == UIO_SYSSPACE) {
3034 } else if ((error = copyin(usrtsp, tsp, sizeof(*tsp) * 2)) != 0)
3036 if (tsp[0].tv_nsec == UTIME_OMIT && tsp[1].tv_nsec == UTIME_OMIT)
3037 *retflags |= UTIMENS_EXIT;
3038 if (tsp[0].tv_nsec == UTIME_NOW && tsp[1].tv_nsec == UTIME_NOW)
3039 *retflags |= UTIMENS_NULL;
3040 if (tsp[0].tv_nsec == UTIME_OMIT)
3041 tsp[0].tv_sec = VNOVAL;
3042 else if (tsp[0].tv_nsec == UTIME_NOW)
3044 else if (tsp[0].tv_nsec < 0 || tsp[0].tv_nsec >= 1000000000L)
3046 if (tsp[1].tv_nsec == UTIME_OMIT)
3047 tsp[1].tv_sec = VNOVAL;
3048 else if (tsp[1].tv_nsec == UTIME_NOW)
3050 else if (tsp[1].tv_nsec < 0 || tsp[1].tv_nsec >= 1000000000L)
3057 * Common implementation code for utimes(), lutimes(), futimes(), futimens(),
3061 setutimes(td, vp, ts, numtimes, nullflag)
3064 const struct timespec *ts;
3070 int error, setbirthtime;
3072 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
3074 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3076 if (numtimes < 3 && !VOP_GETATTR(vp, &vattr, td->td_ucred) &&
3077 timespeccmp(&ts[1], &vattr.va_birthtime, < ))
3080 vattr.va_atime = ts[0];
3081 vattr.va_mtime = ts[1];
3083 vattr.va_birthtime = ts[1];
3085 vattr.va_birthtime = ts[2];
3087 vattr.va_vaflags |= VA_UTIMES_NULL;
3089 error = mac_vnode_check_setutimes(td->td_ucred, vp, vattr.va_atime,
3093 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3095 vn_finished_write(mp);
3100 * Set the access and modification times of a file.
3102 #ifndef _SYS_SYSPROTO_H_
3103 struct utimes_args {
3105 struct timeval *tptr;
3111 register struct utimes_args /* {
3113 struct timeval *tptr;
3117 return (kern_utimesat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
3118 uap->tptr, UIO_USERSPACE));
3121 #ifndef _SYS_SYSPROTO_H_
3122 struct futimesat_args {
3125 const struct timeval * times;
3129 sys_futimesat(struct thread *td, struct futimesat_args *uap)
3132 return (kern_utimesat(td, uap->fd, uap->path, UIO_USERSPACE,
3133 uap->times, UIO_USERSPACE));
3137 kern_utimesat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
3138 struct timeval *tptr, enum uio_seg tptrseg)
3140 struct nameidata nd;
3141 struct timespec ts[2];
3142 cap_rights_t rights;
3145 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3147 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, fd,
3148 cap_rights_init(&rights, CAP_FUTIMES), td);
3150 if ((error = namei(&nd)) != 0)
3152 NDFREE(&nd, NDF_ONLY_PNBUF);
3153 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3159 * Set the access and modification times of a file.
3161 #ifndef _SYS_SYSPROTO_H_
3162 struct lutimes_args {
3164 struct timeval *tptr;
3168 sys_lutimes(td, uap)
3170 register struct lutimes_args /* {
3172 struct timeval *tptr;
3176 return (kern_lutimes(td, uap->path, UIO_USERSPACE, uap->tptr,
3181 kern_lutimes(struct thread *td, char *path, enum uio_seg pathseg,
3182 struct timeval *tptr, enum uio_seg tptrseg)
3184 struct timespec ts[2];
3185 struct nameidata nd;
3188 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3190 NDINIT(&nd, LOOKUP, NOFOLLOW | AUDITVNODE1, pathseg, path, td);
3191 if ((error = namei(&nd)) != 0)
3193 NDFREE(&nd, NDF_ONLY_PNBUF);
3194 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3200 * Set the access and modification times of a file.
3202 #ifndef _SYS_SYSPROTO_H_
3203 struct futimes_args {
3205 struct timeval *tptr;
3209 sys_futimes(td, uap)
3211 register struct futimes_args /* {
3213 struct timeval *tptr;
3217 return (kern_futimes(td, uap->fd, uap->tptr, UIO_USERSPACE));
3221 kern_futimes(struct thread *td, int fd, struct timeval *tptr,
3222 enum uio_seg tptrseg)
3224 struct timespec ts[2];
3226 cap_rights_t rights;
3230 error = getutimes(tptr, tptrseg, ts);
3233 error = getvnode(td->td_proc->p_fd, fd,
3234 cap_rights_init(&rights, CAP_FUTIMES), &fp);
3238 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
3239 AUDIT_ARG_VNODE1(fp->f_vnode);
3240 VOP_UNLOCK(fp->f_vnode, 0);
3242 error = setutimes(td, fp->f_vnode, ts, 2, tptr == NULL);
3248 sys_futimens(struct thread *td, struct futimens_args *uap)
3251 return (kern_futimens(td, uap->fd, uap->times, UIO_USERSPACE));
3255 kern_futimens(struct thread *td, int fd, struct timespec *tptr,
3256 enum uio_seg tptrseg)
3258 struct timespec ts[2];
3260 cap_rights_t rights;
3264 error = getutimens(tptr, tptrseg, ts, &flags);
3267 if (flags & UTIMENS_EXIT)
3269 error = getvnode(td->td_proc->p_fd, fd,
3270 cap_rights_init(&rights, CAP_FUTIMES), &fp);
3274 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
3275 AUDIT_ARG_VNODE1(fp->f_vnode);
3276 VOP_UNLOCK(fp->f_vnode, 0);
3278 error = setutimes(td, fp->f_vnode, ts, 2, flags & UTIMENS_NULL);
3284 sys_utimensat(struct thread *td, struct utimensat_args *uap)
3287 return (kern_utimensat(td, uap->fd, uap->path, UIO_USERSPACE,
3288 uap->times, UIO_USERSPACE, uap->flag));
3292 kern_utimensat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
3293 struct timespec *tptr, enum uio_seg tptrseg, int flag)
3295 struct nameidata nd;
3296 struct timespec ts[2];
3299 if (flag & ~AT_SYMLINK_NOFOLLOW)
3302 if ((error = getutimens(tptr, tptrseg, ts, &flags)) != 0)
3304 NDINIT_AT(&nd, LOOKUP, ((flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW :
3305 FOLLOW) | AUDITVNODE1, pathseg, path, fd, td);
3306 if ((error = namei(&nd)) != 0)
3309 * We are allowed to call namei() regardless of 2xUTIME_OMIT.
3311 * "If both tv_nsec fields are UTIME_OMIT... EACCESS may be detected."
3312 * "Search permission is denied by a component of the path prefix."
3314 NDFREE(&nd, NDF_ONLY_PNBUF);
3315 if ((flags & UTIMENS_EXIT) == 0)
3316 error = setutimes(td, nd.ni_vp, ts, 2, flags & UTIMENS_NULL);
3322 * Truncate a file given its path name.
3324 #ifndef _SYS_SYSPROTO_H_
3325 struct truncate_args {
3332 sys_truncate(td, uap)
3334 register struct truncate_args /* {
3341 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3345 kern_truncate(struct thread *td, char *path, enum uio_seg pathseg, off_t length)
3351 struct nameidata nd;
3356 NDINIT(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, td);
3357 if ((error = namei(&nd)) != 0)
3360 rl_cookie = vn_rangelock_wlock(vp, 0, OFF_MAX);
3361 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0) {
3362 vn_rangelock_unlock(vp, rl_cookie);
3366 NDFREE(&nd, NDF_ONLY_PNBUF);
3367 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3368 if (vp->v_type == VDIR)
3371 else if ((error = mac_vnode_check_write(td->td_ucred, NOCRED, vp))) {
3374 else if ((error = vn_writechk(vp)) == 0 &&
3375 (error = VOP_ACCESS(vp, VWRITE, td->td_ucred, td)) == 0) {
3377 vattr.va_size = length;
3378 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3381 vn_finished_write(mp);
3382 vn_rangelock_unlock(vp, rl_cookie);
3387 #if defined(COMPAT_43)
3389 * Truncate a file given its path name.
3391 #ifndef _SYS_SYSPROTO_H_
3392 struct otruncate_args {
3400 register struct otruncate_args /* {
3405 struct truncate_args /* {
3411 nuap.path = uap->path;
3412 nuap.length = uap->length;
3413 return (sys_truncate(td, &nuap));
3415 #endif /* COMPAT_43 */
3417 /* Versions with the pad argument */
3419 freebsd6_truncate(struct thread *td, struct freebsd6_truncate_args *uap)
3421 struct truncate_args ouap;
3423 ouap.path = uap->path;
3424 ouap.length = uap->length;
3425 return (sys_truncate(td, &ouap));
3429 freebsd6_ftruncate(struct thread *td, struct freebsd6_ftruncate_args *uap)
3431 struct ftruncate_args ouap;
3434 ouap.length = uap->length;
3435 return (sys_ftruncate(td, &ouap));
3439 * Sync an open file.
3441 #ifndef _SYS_SYSPROTO_H_
3449 struct fsync_args /* {
3456 cap_rights_t rights;
3457 int error, lock_flags;
3459 AUDIT_ARG_FD(uap->fd);
3460 error = getvnode(td->td_proc->p_fd, uap->fd,
3461 cap_rights_init(&rights, CAP_FSYNC), &fp);
3465 error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
3468 if (MNT_SHARED_WRITES(mp) ||
3469 ((mp == NULL) && MNT_SHARED_WRITES(vp->v_mount))) {
3470 lock_flags = LK_SHARED;
3472 lock_flags = LK_EXCLUSIVE;
3474 vn_lock(vp, lock_flags | LK_RETRY);
3475 AUDIT_ARG_VNODE1(vp);
3476 if (vp->v_object != NULL) {
3477 VM_OBJECT_WLOCK(vp->v_object);
3478 vm_object_page_clean(vp->v_object, 0, 0, 0);
3479 VM_OBJECT_WUNLOCK(vp->v_object);
3481 error = VOP_FSYNC(vp, MNT_WAIT, td);
3484 vn_finished_write(mp);
3491 * Rename files. Source and destination must either both be directories, or
3492 * both not be directories. If target is a directory, it must be empty.
3494 #ifndef _SYS_SYSPROTO_H_
3495 struct rename_args {
3503 register struct rename_args /* {
3509 return (kern_renameat(td, AT_FDCWD, uap->from, AT_FDCWD,
3510 uap->to, UIO_USERSPACE));
3513 #ifndef _SYS_SYSPROTO_H_
3514 struct renameat_args {
3522 sys_renameat(struct thread *td, struct renameat_args *uap)
3525 return (kern_renameat(td, uap->oldfd, uap->old, uap->newfd, uap->new,
3530 kern_renameat(struct thread *td, int oldfd, char *old, int newfd, char *new,
3531 enum uio_seg pathseg)
3533 struct mount *mp = NULL;
3534 struct vnode *tvp, *fvp, *tdvp;
3535 struct nameidata fromnd, tond;
3536 cap_rights_t rights;
3542 NDINIT_ATRIGHTS(&fromnd, DELETE, LOCKPARENT | LOCKLEAF | SAVESTART |
3543 AUDITVNODE1, pathseg, old, oldfd,
3544 cap_rights_init(&rights, CAP_RENAMEAT), td);
3546 NDINIT_ATRIGHTS(&fromnd, DELETE, WANTPARENT | SAVESTART | AUDITVNODE1,
3547 pathseg, old, oldfd, cap_rights_init(&rights, CAP_RENAMEAT), td);
3550 if ((error = namei(&fromnd)) != 0)
3553 error = mac_vnode_check_rename_from(td->td_ucred, fromnd.ni_dvp,
3554 fromnd.ni_vp, &fromnd.ni_cnd);
3555 VOP_UNLOCK(fromnd.ni_dvp, 0);
3556 if (fromnd.ni_dvp != fromnd.ni_vp)
3557 VOP_UNLOCK(fromnd.ni_vp, 0);
3560 NDINIT_ATRIGHTS(&tond, RENAME, LOCKPARENT | LOCKLEAF | NOCACHE |
3561 SAVESTART | AUDITVNODE2, pathseg, new, newfd,
3562 cap_rights_init(&rights, CAP_LINKAT), td);
3563 if (fromnd.ni_vp->v_type == VDIR)
3564 tond.ni_cnd.cn_flags |= WILLBEDIR;
3565 if ((error = namei(&tond)) != 0) {
3566 /* Translate error code for rename("dir1", "dir2/."). */
3567 if (error == EISDIR && fvp->v_type == VDIR)
3569 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3570 vrele(fromnd.ni_dvp);
3576 error = vn_start_write(fvp, &mp, V_NOWAIT);
3578 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3579 NDFREE(&tond, NDF_ONLY_PNBUF);
3586 vrele(fromnd.ni_dvp);
3588 vrele(tond.ni_startdir);
3589 if (fromnd.ni_startdir != NULL)
3590 vrele(fromnd.ni_startdir);
3591 error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH);
3597 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
3600 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
3605 if (newfd != AT_FDCWD) {
3607 * If the target already exists we require CAP_UNLINKAT
3610 error = cap_check(&tond.ni_filecaps.fc_rights,
3611 cap_rights_init(&rights, CAP_UNLINKAT));
3622 * If the source is the same as the destination (that is, if they
3623 * are links to the same vnode), then there is nothing to do.
3629 error = mac_vnode_check_rename_to(td->td_ucred, tdvp,
3630 tond.ni_vp, fromnd.ni_dvp == tdvp, &tond.ni_cnd);
3634 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
3635 tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
3636 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3637 NDFREE(&tond, NDF_ONLY_PNBUF);
3639 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3640 NDFREE(&tond, NDF_ONLY_PNBUF);
3647 vrele(fromnd.ni_dvp);
3650 vrele(tond.ni_startdir);
3651 vn_finished_write(mp);
3653 if (fromnd.ni_startdir)
3654 vrele(fromnd.ni_startdir);
3661 * Make a directory file.
3663 #ifndef _SYS_SYSPROTO_H_
3672 register struct mkdir_args /* {
3678 return (kern_mkdirat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
3682 #ifndef _SYS_SYSPROTO_H_
3683 struct mkdirat_args {
3690 sys_mkdirat(struct thread *td, struct mkdirat_args *uap)
3693 return (kern_mkdirat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode));
3697 kern_mkdirat(struct thread *td, int fd, char *path, enum uio_seg segflg,
3703 struct nameidata nd;
3704 cap_rights_t rights;
3707 AUDIT_ARG_MODE(mode);
3710 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
3711 NOCACHE, segflg, path, fd, cap_rights_init(&rights, CAP_MKDIRAT),
3713 nd.ni_cnd.cn_flags |= WILLBEDIR;
3714 if ((error = namei(&nd)) != 0)
3718 NDFREE(&nd, NDF_ONLY_PNBUF);
3720 * XXX namei called with LOCKPARENT but not LOCKLEAF has
3721 * the strange behaviour of leaving the vnode unlocked
3722 * if the target is the same vnode as the parent.
3724 if (vp == nd.ni_dvp)
3731 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3732 NDFREE(&nd, NDF_ONLY_PNBUF);
3734 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3739 vattr.va_type = VDIR;
3740 vattr.va_mode = (mode & ACCESSPERMS) &~ td->td_proc->p_fd->fd_cmask;
3742 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
3747 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
3751 NDFREE(&nd, NDF_ONLY_PNBUF);
3755 vn_finished_write(mp);
3760 * Remove a directory file.
3762 #ifndef _SYS_SYSPROTO_H_
3770 struct rmdir_args /* {
3775 return (kern_rmdirat(td, AT_FDCWD, uap->path, UIO_USERSPACE));
3779 kern_rmdirat(struct thread *td, int fd, char *path, enum uio_seg pathseg)
3783 struct nameidata nd;
3784 cap_rights_t rights;
3789 NDINIT_ATRIGHTS(&nd, DELETE, LOCKPARENT | LOCKLEAF | AUDITVNODE1,
3790 pathseg, path, fd, cap_rights_init(&rights, CAP_UNLINKAT), td);
3791 if ((error = namei(&nd)) != 0)
3794 if (vp->v_type != VDIR) {
3799 * No rmdir "." please.
3801 if (nd.ni_dvp == vp) {
3806 * The root of a mounted filesystem cannot be deleted.
3808 if (vp->v_vflag & VV_ROOT) {
3813 error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp,
3818 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3819 NDFREE(&nd, NDF_ONLY_PNBUF);
3821 if (nd.ni_dvp == vp)
3825 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3829 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_UNLINK);
3830 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
3831 vn_finished_write(mp);
3833 NDFREE(&nd, NDF_ONLY_PNBUF);
3835 if (nd.ni_dvp == vp)
3844 * Read a block of directory entries in a filesystem independent format.
3846 #ifndef _SYS_SYSPROTO_H_
3847 struct ogetdirentries_args {
3855 ogetdirentries(struct thread *td, struct ogetdirentries_args *uap)
3860 error = kern_ogetdirentries(td, uap, &loff);
3862 error = copyout(&loff, uap->basep, sizeof(long));
3867 kern_ogetdirentries(struct thread *td, struct ogetdirentries_args *uap,
3872 struct uio auio, kuio;
3873 struct iovec aiov, kiov;
3874 struct dirent *dp, *edp;
3875 cap_rights_t rights;
3877 int error, eofflag, readcnt;
3881 /* XXX arbitrary sanity limit on `count'. */
3882 if (uap->count > 64 * 1024)
3884 error = getvnode(td->td_proc->p_fd, uap->fd,
3885 cap_rights_init(&rights, CAP_READ), &fp);
3888 if ((fp->f_flag & FREAD) == 0) {
3893 foffset = foffset_lock(fp, 0);
3895 if (vp->v_type != VDIR) {
3896 foffset_unlock(fp, foffset, 0);
3900 aiov.iov_base = uap->buf;
3901 aiov.iov_len = uap->count;
3902 auio.uio_iov = &aiov;
3903 auio.uio_iovcnt = 1;
3904 auio.uio_rw = UIO_READ;
3905 auio.uio_segflg = UIO_USERSPACE;
3907 auio.uio_resid = uap->count;
3908 vn_lock(vp, LK_SHARED | LK_RETRY);
3909 loff = auio.uio_offset = foffset;
3911 error = mac_vnode_check_readdir(td->td_ucred, vp);
3914 foffset_unlock(fp, foffset, FOF_NOUPDATE);
3919 # if (BYTE_ORDER != LITTLE_ENDIAN)
3920 if (vp->v_mount->mnt_maxsymlinklen <= 0) {
3921 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag,
3923 foffset = auio.uio_offset;
3928 kuio.uio_iov = &kiov;
3929 kuio.uio_segflg = UIO_SYSSPACE;
3930 kiov.iov_len = uap->count;
3931 dirbuf = malloc(uap->count, M_TEMP, M_WAITOK);
3932 kiov.iov_base = dirbuf;
3933 error = VOP_READDIR(vp, &kuio, fp->f_cred, &eofflag,
3935 foffset = kuio.uio_offset;
3937 readcnt = uap->count - kuio.uio_resid;
3938 edp = (struct dirent *)&dirbuf[readcnt];
3939 for (dp = (struct dirent *)dirbuf; dp < edp; ) {
3940 # if (BYTE_ORDER == LITTLE_ENDIAN)
3942 * The expected low byte of
3943 * dp->d_namlen is our dp->d_type.
3944 * The high MBZ byte of dp->d_namlen
3945 * is our dp->d_namlen.
3947 dp->d_type = dp->d_namlen;
3951 * The dp->d_type is the high byte
3952 * of the expected dp->d_namlen,
3953 * so must be zero'ed.
3957 if (dp->d_reclen > 0) {
3958 dp = (struct dirent *)
3959 ((char *)dp + dp->d_reclen);
3966 error = uiomove(dirbuf, readcnt, &auio);
3968 free(dirbuf, M_TEMP);
3972 foffset_unlock(fp, foffset, 0);
3976 if (uap->count == auio.uio_resid &&
3977 (vp->v_vflag & VV_ROOT) &&
3978 (vp->v_mount->mnt_flag & MNT_UNION)) {
3979 struct vnode *tvp = vp;
3980 vp = vp->v_mount->mnt_vnodecovered;
3989 foffset_unlock(fp, foffset, 0);
3991 td->td_retval[0] = uap->count - auio.uio_resid;
3996 #endif /* COMPAT_43 */
3999 * Read a block of directory entries in a filesystem independent format.
4001 #ifndef _SYS_SYSPROTO_H_
4002 struct getdirentries_args {
4010 sys_getdirentries(td, uap)
4012 register struct getdirentries_args /* {
4022 error = kern_getdirentries(td, uap->fd, uap->buf, uap->count, &base,
4023 NULL, UIO_USERSPACE);
4026 if (uap->basep != NULL)
4027 error = copyout(&base, uap->basep, sizeof(long));
4032 kern_getdirentries(struct thread *td, int fd, char *buf, u_int count,
4033 long *basep, ssize_t *residp, enum uio_seg bufseg)
4039 cap_rights_t rights;
4045 if (count > IOSIZE_MAX)
4047 auio.uio_resid = count;
4048 error = getvnode(td->td_proc->p_fd, fd,
4049 cap_rights_init(&rights, CAP_READ), &fp);
4052 if ((fp->f_flag & FREAD) == 0) {
4057 foffset = foffset_lock(fp, 0);
4059 if (vp->v_type != VDIR) {
4063 aiov.iov_base = buf;
4064 aiov.iov_len = count;
4065 auio.uio_iov = &aiov;
4066 auio.uio_iovcnt = 1;
4067 auio.uio_rw = UIO_READ;
4068 auio.uio_segflg = bufseg;
4070 vn_lock(vp, LK_SHARED | LK_RETRY);
4071 AUDIT_ARG_VNODE1(vp);
4072 loff = auio.uio_offset = foffset;
4074 error = mac_vnode_check_readdir(td->td_ucred, vp);
4077 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL,
4079 foffset = auio.uio_offset;
4084 if (count == auio.uio_resid &&
4085 (vp->v_vflag & VV_ROOT) &&
4086 (vp->v_mount->mnt_flag & MNT_UNION)) {
4087 struct vnode *tvp = vp;
4089 vp = vp->v_mount->mnt_vnodecovered;
4100 *residp = auio.uio_resid;
4101 td->td_retval[0] = count - auio.uio_resid;
4103 foffset_unlock(fp, foffset, 0);
4108 #ifndef _SYS_SYSPROTO_H_
4109 struct getdents_args {
4116 sys_getdents(td, uap)
4118 register struct getdents_args /* {
4124 struct getdirentries_args ap;
4128 ap.count = uap->count;
4130 return (sys_getdirentries(td, &ap));
4134 * Set the mode mask for creation of filesystem nodes.
4136 #ifndef _SYS_SYSPROTO_H_
4144 struct umask_args /* {
4148 register struct filedesc *fdp;
4150 FILEDESC_XLOCK(td->td_proc->p_fd);
4151 fdp = td->td_proc->p_fd;
4152 td->td_retval[0] = fdp->fd_cmask;
4153 fdp->fd_cmask = uap->newmask & ALLPERMS;
4154 FILEDESC_XUNLOCK(td->td_proc->p_fd);
4159 * Void all references to file by ripping underlying filesystem away from
4162 #ifndef _SYS_SYSPROTO_H_
4163 struct revoke_args {
4170 register struct revoke_args /* {
4176 struct nameidata nd;
4179 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
4181 if ((error = namei(&nd)) != 0)
4184 NDFREE(&nd, NDF_ONLY_PNBUF);
4185 if (vp->v_type != VCHR || vp->v_rdev == NULL) {
4190 error = mac_vnode_check_revoke(td->td_ucred, vp);
4194 error = VOP_GETATTR(vp, &vattr, td->td_ucred);
4197 if (td->td_ucred->cr_uid != vattr.va_uid) {
4198 error = priv_check(td, PRIV_VFS_ADMIN);
4203 VOP_REVOKE(vp, REVOKEALL);
4210 * Convert a user file descriptor to a kernel file entry and check that, if it
4211 * is a capability, the correct rights are present. A reference on the file
4212 * entry is held upon returning.
4215 getvnode(struct filedesc *fdp, int fd, cap_rights_t *rightsp, struct file **fpp)
4220 error = fget_unlocked(fdp, fd, rightsp, 0, &fp, NULL);
4225 * The file could be not of the vnode type, or it may be not
4226 * yet fully initialized, in which case the f_vnode pointer
4227 * may be set, but f_ops is still badfileops. E.g.,
4228 * devfs_open() transiently create such situation to
4229 * facilitate csw d_fdopen().
4231 * Dupfdopen() handling in kern_openat() installs the
4232 * half-baked file into the process descriptor table, allowing
4233 * other thread to dereference it. Guard against the race by
4236 if (fp->f_vnode == NULL || fp->f_ops == &badfileops) {
4237 fdrop(fp, curthread);
4246 * Get an (NFS) file handle.
4248 #ifndef _SYS_SYSPROTO_H_
4249 struct lgetfh_args {
4257 register struct lgetfh_args *uap;
4259 struct nameidata nd;
4261 register struct vnode *vp;
4264 error = priv_check(td, PRIV_VFS_GETFH);
4267 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
4272 NDFREE(&nd, NDF_ONLY_PNBUF);
4274 bzero(&fh, sizeof(fh));
4275 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
4276 error = VOP_VPTOFH(vp, &fh.fh_fid);
4279 error = copyout(&fh, uap->fhp, sizeof (fh));
4283 #ifndef _SYS_SYSPROTO_H_
4292 register struct getfh_args *uap;
4294 struct nameidata nd;
4296 register struct vnode *vp;
4299 error = priv_check(td, PRIV_VFS_GETFH);
4302 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
4307 NDFREE(&nd, NDF_ONLY_PNBUF);
4309 bzero(&fh, sizeof(fh));
4310 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
4311 error = VOP_VPTOFH(vp, &fh.fh_fid);
4314 error = copyout(&fh, uap->fhp, sizeof (fh));
4319 * syscall for the rpc.lockd to use to translate a NFS file handle into an
4322 * warning: do not remove the priv_check() call or this becomes one giant
4325 #ifndef _SYS_SYSPROTO_H_
4326 struct fhopen_args {
4327 const struct fhandle *u_fhp;
4334 struct fhopen_args /* {
4335 const struct fhandle *u_fhp;
4346 error = priv_check(td, PRIV_VFS_FHOPEN);
4350 fmode = FFLAGS(uap->flags);
4351 /* why not allow a non-read/write open for our lockd? */
4352 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
4354 error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
4357 /* find the mount point */
4358 mp = vfs_busyfs(&fhp.fh_fsid);
4361 /* now give me my vnode, it gets returned to me locked */
4362 error = VFS_FHTOVP(mp, &fhp.fh_fid, LK_EXCLUSIVE, &vp);
4367 error = falloc_noinstall(td, &fp);
4373 * An extra reference on `fp' has been held for us by
4374 * falloc_noinstall().
4380 error = vn_open_vnode(vp, fmode, td->td_ucred, td, fp);
4382 KASSERT(fp->f_ops == &badfileops,
4383 ("VOP_OPEN in fhopen() set f_ops"));
4384 KASSERT(td->td_dupfd < 0,
4385 ("fhopen() encountered fdopen()"));
4395 finit(fp, (fmode & FMASK) | (fp->f_flag & FHASLOCK), DTYPE_VNODE, vp,
4398 if ((fmode & O_TRUNC) != 0) {
4399 error = fo_truncate(fp, 0, td->td_ucred, td);
4404 error = finstall(td, fp, &indx, fmode, NULL);
4407 td->td_retval[0] = indx;
4412 * Stat an (NFS) file handle.
4414 #ifndef _SYS_SYSPROTO_H_
4415 struct fhstat_args {
4416 struct fhandle *u_fhp;
4423 register struct fhstat_args /* {
4424 struct fhandle *u_fhp;
4432 error = copyin(uap->u_fhp, &fh, sizeof(fh));
4435 error = kern_fhstat(td, fh, &sb);
4437 error = copyout(&sb, uap->sb, sizeof(sb));
4442 kern_fhstat(struct thread *td, struct fhandle fh, struct stat *sb)
4448 error = priv_check(td, PRIV_VFS_FHSTAT);
4451 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4453 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_EXCLUSIVE, &vp);
4457 error = vn_stat(vp, sb, td->td_ucred, NOCRED, td);
4463 * Implement fstatfs() for (NFS) file handles.
4465 #ifndef _SYS_SYSPROTO_H_
4466 struct fhstatfs_args {
4467 struct fhandle *u_fhp;
4472 sys_fhstatfs(td, uap)
4474 struct fhstatfs_args /* {
4475 struct fhandle *u_fhp;
4483 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4486 error = kern_fhstatfs(td, fh, &sf);
4489 return (copyout(&sf, uap->buf, sizeof(sf)));
4493 kern_fhstatfs(struct thread *td, fhandle_t fh, struct statfs *buf)
4500 error = priv_check(td, PRIV_VFS_FHSTATFS);
4503 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4505 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_EXCLUSIVE, &vp);
4511 error = prison_canseemount(td->td_ucred, mp);
4515 error = mac_mount_check_stat(td->td_ucred, mp);
4520 * Set these in case the underlying filesystem fails to do so.
4523 sp->f_version = STATFS_VERSION;
4524 sp->f_namemax = NAME_MAX;
4525 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
4526 error = VFS_STATFS(mp, sp);
4535 kern_posix_fallocate(struct thread *td, int fd, off_t offset, off_t len)
4540 cap_rights_t rights;
4541 off_t olen, ooffset;
4544 if (offset < 0 || len <= 0)
4546 /* Check for wrap. */
4547 if (offset > OFF_MAX - len)
4549 error = fget(td, fd, cap_rights_init(&rights, CAP_WRITE), &fp);
4552 if ((fp->f_ops->fo_flags & DFLAG_SEEKABLE) == 0) {
4556 if ((fp->f_flag & FWRITE) == 0) {
4560 if (fp->f_type != DTYPE_VNODE) {
4565 if (vp->v_type != VREG) {
4570 /* Allocating blocks may take a long time, so iterate. */
4577 error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
4580 error = vn_lock(vp, LK_EXCLUSIVE);
4582 vn_finished_write(mp);
4586 error = mac_vnode_check_write(td->td_ucred, fp->f_cred, vp);
4589 error = VOP_ALLOCATE(vp, &offset, &len);
4591 vn_finished_write(mp);
4593 if (olen + ooffset != offset + len) {
4594 panic("offset + len changed from %jx/%jx to %jx/%jx",
4595 ooffset, olen, offset, len);
4597 if (error != 0 || len == 0)
4599 KASSERT(olen > len, ("Iteration did not make progress?"));
4608 sys_posix_fallocate(struct thread *td, struct posix_fallocate_args *uap)
4611 td->td_retval[0] = kern_posix_fallocate(td, uap->fd, uap->offset,
4617 * Unlike madvise(2), we do not make a best effort to remember every
4618 * possible caching hint. Instead, we remember the last setting with
4619 * the exception that we will allow POSIX_FADV_NORMAL to adjust the
4620 * region of any current setting.
4623 kern_posix_fadvise(struct thread *td, int fd, off_t offset, off_t len,
4626 struct fadvise_info *fa, *new;
4629 cap_rights_t rights;
4633 if (offset < 0 || len < 0 || offset > OFF_MAX - len)
4636 case POSIX_FADV_SEQUENTIAL:
4637 case POSIX_FADV_RANDOM:
4638 case POSIX_FADV_NOREUSE:
4639 new = malloc(sizeof(*fa), M_FADVISE, M_WAITOK);
4641 case POSIX_FADV_NORMAL:
4642 case POSIX_FADV_WILLNEED:
4643 case POSIX_FADV_DONTNEED:
4649 /* XXX: CAP_POSIX_FADVISE? */
4650 error = fget(td, fd, cap_rights_init(&rights), &fp);
4653 if ((fp->f_ops->fo_flags & DFLAG_SEEKABLE) == 0) {
4657 if (fp->f_type != DTYPE_VNODE) {
4662 if (vp->v_type != VREG) {
4669 end = offset + len - 1;
4671 case POSIX_FADV_SEQUENTIAL:
4672 case POSIX_FADV_RANDOM:
4673 case POSIX_FADV_NOREUSE:
4675 * Try to merge any existing non-standard region with
4676 * this new region if possible, otherwise create a new
4677 * non-standard region for this request.
4679 mtx_pool_lock(mtxpool_sleep, fp);
4681 if (fa != NULL && fa->fa_advice == advice &&
4682 ((fa->fa_start <= end && fa->fa_end >= offset) ||
4683 (end != OFF_MAX && fa->fa_start == end + 1) ||
4684 (fa->fa_end != OFF_MAX && fa->fa_end + 1 == offset))) {
4685 if (offset < fa->fa_start)
4686 fa->fa_start = offset;
4687 if (end > fa->fa_end)
4690 new->fa_advice = advice;
4691 new->fa_start = offset;
4693 new->fa_prevstart = 0;
4694 new->fa_prevend = 0;
4698 mtx_pool_unlock(mtxpool_sleep, fp);
4700 case POSIX_FADV_NORMAL:
4702 * If a the "normal" region overlaps with an existing
4703 * non-standard region, trim or remove the
4704 * non-standard region.
4706 mtx_pool_lock(mtxpool_sleep, fp);
4709 if (offset <= fa->fa_start && end >= fa->fa_end) {
4711 fp->f_advice = NULL;
4712 } else if (offset <= fa->fa_start &&
4713 end >= fa->fa_start)
4714 fa->fa_start = end + 1;
4715 else if (offset <= fa->fa_end && end >= fa->fa_end)
4716 fa->fa_end = offset - 1;
4717 else if (offset >= fa->fa_start && end <= fa->fa_end) {
4719 * If the "normal" region is a middle
4720 * portion of the existing
4721 * non-standard region, just remove
4722 * the whole thing rather than picking
4723 * one side or the other to
4727 fp->f_advice = NULL;
4730 mtx_pool_unlock(mtxpool_sleep, fp);
4732 case POSIX_FADV_WILLNEED:
4733 case POSIX_FADV_DONTNEED:
4734 error = VOP_ADVISE(vp, offset, end, advice);
4740 free(new, M_FADVISE);
4745 sys_posix_fadvise(struct thread *td, struct posix_fadvise_args *uap)
4748 td->td_retval[0] = kern_posix_fadvise(td, uap->fd, uap->offset,
4749 uap->len, uap->advice);