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, fd, cap_rights_init(&rights, CAP_FSTATFS), &fp);
372 vn_lock(vp, LK_SHARED | LK_RETRY);
374 AUDIT_ARG_VNODE1(vp);
385 error = vfs_busy(mp, 0);
390 error = mac_mount_check_stat(td->td_ucred, mp);
395 * Set these in case the underlying filesystem fails to do so.
398 sp->f_version = STATFS_VERSION;
399 sp->f_namemax = NAME_MAX;
400 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
401 error = VFS_STATFS(mp, sp);
404 if (priv_check(td, PRIV_VFS_GENERATION)) {
405 bcopy(sp, &sb, sizeof(sb));
406 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
407 prison_enforce_statfs(td->td_ucred, mp, &sb);
418 * Get statistics on all filesystems.
420 #ifndef _SYS_SYSPROTO_H_
421 struct getfsstat_args {
428 sys_getfsstat(td, uap)
430 register struct getfsstat_args /* {
439 error = kern_getfsstat(td, &uap->buf, uap->bufsize, &count,
440 UIO_USERSPACE, uap->flags);
442 td->td_retval[0] = count;
447 * If (bufsize > 0 && bufseg == UIO_SYSSPACE)
448 * The caller is responsible for freeing memory which will be allocated
452 kern_getfsstat(struct thread *td, struct statfs **buf, size_t bufsize,
453 size_t *countp, enum uio_seg bufseg, int flags)
455 struct mount *mp, *nmp;
456 struct statfs *sfsp, *sp, sb;
457 size_t count, maxcount;
460 maxcount = bufsize / sizeof(struct statfs);
463 else if (bufseg == UIO_USERSPACE)
465 else /* if (bufseg == UIO_SYSSPACE) */ {
467 mtx_lock(&mountlist_mtx);
468 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
471 mtx_unlock(&mountlist_mtx);
472 if (maxcount > count)
474 sfsp = *buf = malloc(maxcount * sizeof(struct statfs), M_TEMP,
478 mtx_lock(&mountlist_mtx);
479 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
480 if (prison_canseemount(td->td_ucred, mp) != 0) {
481 nmp = TAILQ_NEXT(mp, mnt_list);
485 if (mac_mount_check_stat(td->td_ucred, mp) != 0) {
486 nmp = TAILQ_NEXT(mp, mnt_list);
490 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK)) {
491 nmp = TAILQ_NEXT(mp, mnt_list);
494 if (sfsp && count < maxcount) {
497 * Set these in case the underlying filesystem
500 sp->f_version = STATFS_VERSION;
501 sp->f_namemax = NAME_MAX;
502 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
504 * If MNT_NOWAIT or MNT_LAZY is specified, do not
505 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
506 * overrides MNT_WAIT.
508 if (((flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
509 (flags & MNT_WAIT)) &&
510 (error = VFS_STATFS(mp, sp))) {
511 mtx_lock(&mountlist_mtx);
512 nmp = TAILQ_NEXT(mp, mnt_list);
516 if (priv_check(td, PRIV_VFS_GENERATION)) {
517 bcopy(sp, &sb, sizeof(sb));
518 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
519 prison_enforce_statfs(td->td_ucred, mp, &sb);
522 if (bufseg == UIO_SYSSPACE)
523 bcopy(sp, sfsp, sizeof(*sp));
524 else /* if (bufseg == UIO_USERSPACE) */ {
525 error = copyout(sp, sfsp, sizeof(*sp));
534 mtx_lock(&mountlist_mtx);
535 nmp = TAILQ_NEXT(mp, mnt_list);
538 mtx_unlock(&mountlist_mtx);
539 if (sfsp && count > maxcount)
546 #ifdef COMPAT_FREEBSD4
548 * Get old format filesystem statistics.
550 static void cvtstatfs(struct statfs *, struct ostatfs *);
552 #ifndef _SYS_SYSPROTO_H_
553 struct freebsd4_statfs_args {
559 freebsd4_statfs(td, uap)
561 struct freebsd4_statfs_args /* {
570 error = kern_statfs(td, uap->path, UIO_USERSPACE, &sf);
573 cvtstatfs(&sf, &osb);
574 return (copyout(&osb, uap->buf, sizeof(osb)));
578 * Get filesystem statistics.
580 #ifndef _SYS_SYSPROTO_H_
581 struct freebsd4_fstatfs_args {
587 freebsd4_fstatfs(td, uap)
589 struct freebsd4_fstatfs_args /* {
598 error = kern_fstatfs(td, uap->fd, &sf);
601 cvtstatfs(&sf, &osb);
602 return (copyout(&osb, uap->buf, sizeof(osb)));
606 * Get statistics on all filesystems.
608 #ifndef _SYS_SYSPROTO_H_
609 struct freebsd4_getfsstat_args {
616 freebsd4_getfsstat(td, uap)
618 register struct freebsd4_getfsstat_args /* {
624 struct statfs *buf, *sp;
629 count = uap->bufsize / sizeof(struct ostatfs);
630 size = count * sizeof(struct statfs);
631 error = kern_getfsstat(td, &buf, size, &count, UIO_SYSSPACE,
635 while (count > 0 && error == 0) {
637 error = copyout(&osb, uap->buf, sizeof(osb));
645 td->td_retval[0] = count;
650 * Implement fstatfs() for (NFS) file handles.
652 #ifndef _SYS_SYSPROTO_H_
653 struct freebsd4_fhstatfs_args {
654 struct fhandle *u_fhp;
659 freebsd4_fhstatfs(td, uap)
661 struct freebsd4_fhstatfs_args /* {
662 struct fhandle *u_fhp;
671 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
674 error = kern_fhstatfs(td, fh, &sf);
677 cvtstatfs(&sf, &osb);
678 return (copyout(&osb, uap->buf, sizeof(osb)));
682 * Convert a new format statfs structure to an old format statfs structure.
690 statfs_scale_blocks(nsp, LONG_MAX);
691 bzero(osp, sizeof(*osp));
692 osp->f_bsize = nsp->f_bsize;
693 osp->f_iosize = MIN(nsp->f_iosize, LONG_MAX);
694 osp->f_blocks = nsp->f_blocks;
695 osp->f_bfree = nsp->f_bfree;
696 osp->f_bavail = nsp->f_bavail;
697 osp->f_files = MIN(nsp->f_files, LONG_MAX);
698 osp->f_ffree = MIN(nsp->f_ffree, LONG_MAX);
699 osp->f_owner = nsp->f_owner;
700 osp->f_type = nsp->f_type;
701 osp->f_flags = nsp->f_flags;
702 osp->f_syncwrites = MIN(nsp->f_syncwrites, LONG_MAX);
703 osp->f_asyncwrites = MIN(nsp->f_asyncwrites, LONG_MAX);
704 osp->f_syncreads = MIN(nsp->f_syncreads, LONG_MAX);
705 osp->f_asyncreads = MIN(nsp->f_asyncreads, LONG_MAX);
706 strlcpy(osp->f_fstypename, nsp->f_fstypename,
707 MIN(MFSNAMELEN, OMFSNAMELEN));
708 strlcpy(osp->f_mntonname, nsp->f_mntonname,
709 MIN(MNAMELEN, OMNAMELEN));
710 strlcpy(osp->f_mntfromname, nsp->f_mntfromname,
711 MIN(MNAMELEN, OMNAMELEN));
712 osp->f_fsid = nsp->f_fsid;
714 #endif /* COMPAT_FREEBSD4 */
717 * Change current working directory to a given file descriptor.
719 #ifndef _SYS_SYSPROTO_H_
727 struct fchdir_args /* {
731 register struct filedesc *fdp = td->td_proc->p_fd;
732 struct vnode *vp, *tdp, *vpold;
738 AUDIT_ARG_FD(uap->fd);
739 error = getvnode(td, uap->fd, cap_rights_init(&rights, CAP_FCHDIR),
746 vn_lock(vp, LK_SHARED | LK_RETRY);
747 AUDIT_ARG_VNODE1(vp);
748 error = change_dir(vp, td);
749 while (!error && (mp = vp->v_mountedhere) != NULL) {
752 error = VFS_ROOT(mp, LK_SHARED, &tdp);
765 vpold = fdp->fd_cdir;
767 FILEDESC_XUNLOCK(fdp);
773 * Change current working directory (``.'').
775 #ifndef _SYS_SYSPROTO_H_
783 struct chdir_args /* {
788 return (kern_chdir(td, uap->path, UIO_USERSPACE));
792 kern_chdir(struct thread *td, char *path, enum uio_seg pathseg)
794 register struct filedesc *fdp = td->td_proc->p_fd;
799 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
801 if ((error = namei(&nd)) != 0)
803 if ((error = change_dir(nd.ni_vp, td)) != 0) {
805 NDFREE(&nd, NDF_ONLY_PNBUF);
808 VOP_UNLOCK(nd.ni_vp, 0);
809 NDFREE(&nd, NDF_ONLY_PNBUF);
812 fdp->fd_cdir = nd.ni_vp;
813 FILEDESC_XUNLOCK(fdp);
819 * Helper function for raised chroot(2) security function: Refuse if
820 * any filedescriptors are open directories.
823 chroot_refuse_vdir_fds(fdp)
824 struct filedesc *fdp;
830 FILEDESC_LOCK_ASSERT(fdp);
832 for (fd = 0; fd <= fdp->fd_lastfile; fd++) {
833 fp = fget_locked(fdp, fd);
836 if (fp->f_type == DTYPE_VNODE) {
838 if (vp->v_type == VDIR)
846 * This sysctl determines if we will allow a process to chroot(2) if it
847 * has a directory open:
848 * 0: disallowed for all processes.
849 * 1: allowed for processes that were not already chroot(2)'ed.
850 * 2: allowed for all processes.
853 static int chroot_allow_open_directories = 1;
855 SYSCTL_INT(_kern, OID_AUTO, chroot_allow_open_directories, CTLFLAG_RW,
856 &chroot_allow_open_directories, 0,
857 "Allow a process to chroot(2) if it has a directory open");
860 * Change notion of root (``/'') directory.
862 #ifndef _SYS_SYSPROTO_H_
870 struct chroot_args /* {
877 error = priv_check(td, PRIV_VFS_CHROOT);
880 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
881 UIO_USERSPACE, uap->path, td);
885 error = change_dir(nd.ni_vp, td);
889 error = mac_vnode_check_chroot(td->td_ucred, nd.ni_vp);
893 VOP_UNLOCK(nd.ni_vp, 0);
894 error = change_root(nd.ni_vp, td);
896 NDFREE(&nd, NDF_ONLY_PNBUF);
901 NDFREE(&nd, NDF_ONLY_PNBUF);
906 * Common routine for chroot and chdir. Callers must provide a locked vnode
918 ASSERT_VOP_LOCKED(vp, "change_dir(): vp not locked");
919 if (vp->v_type != VDIR)
922 error = mac_vnode_check_chdir(td->td_ucred, vp);
926 return (VOP_ACCESS(vp, VEXEC, td->td_ucred, td));
930 * Common routine for kern_chroot() and jail_attach(). The caller is
931 * responsible for invoking priv_check() and mac_vnode_check_chroot() to
932 * authorize this operation.
939 struct filedesc *fdp;
943 fdp = td->td_proc->p_fd;
945 if (chroot_allow_open_directories == 0 ||
946 (chroot_allow_open_directories == 1 && fdp->fd_rdir != rootvnode)) {
947 error = chroot_refuse_vdir_fds(fdp);
949 FILEDESC_XUNLOCK(fdp);
953 oldvp = fdp->fd_rdir;
960 FILEDESC_XUNLOCK(fdp);
966 flags_to_rights(int flags, cap_rights_t *rightsp)
969 if (flags & O_EXEC) {
970 cap_rights_set(rightsp, CAP_FEXECVE);
972 switch ((flags & O_ACCMODE)) {
974 cap_rights_set(rightsp, CAP_READ);
977 cap_rights_set(rightsp, CAP_READ);
980 cap_rights_set(rightsp, CAP_WRITE);
981 if (!(flags & (O_APPEND | O_TRUNC)))
982 cap_rights_set(rightsp, CAP_SEEK);
988 cap_rights_set(rightsp, CAP_CREATE);
991 cap_rights_set(rightsp, CAP_FTRUNCATE);
993 if (flags & (O_SYNC | O_FSYNC))
994 cap_rights_set(rightsp, CAP_FSYNC);
996 if (flags & (O_EXLOCK | O_SHLOCK))
997 cap_rights_set(rightsp, CAP_FLOCK);
1001 * Check permissions, allocate an open file structure, and call the device
1002 * open routine if any.
1004 #ifndef _SYS_SYSPROTO_H_
1014 register struct open_args /* {
1021 return (kern_openat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1022 uap->flags, uap->mode));
1025 #ifndef _SYS_SYSPROTO_H_
1026 struct openat_args {
1034 sys_openat(struct thread *td, struct openat_args *uap)
1037 return (kern_openat(td, uap->fd, uap->path, UIO_USERSPACE, uap->flag,
1042 kern_openat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1043 int flags, int mode)
1045 struct proc *p = td->td_proc;
1046 struct filedesc *fdp = p->p_fd;
1049 struct nameidata nd;
1050 cap_rights_t rights;
1051 int cmode, error, indx;
1055 AUDIT_ARG_FFLAGS(flags);
1056 AUDIT_ARG_MODE(mode);
1057 /* XXX: audit dirfd */
1058 cap_rights_init(&rights, CAP_LOOKUP);
1059 flags_to_rights(flags, &rights);
1061 * Only one of the O_EXEC, O_RDONLY, O_WRONLY and O_RDWR flags
1064 if (flags & O_EXEC) {
1065 if (flags & O_ACCMODE)
1067 } else if ((flags & O_ACCMODE) == O_ACCMODE) {
1070 flags = FFLAGS(flags);
1074 * Allocate the file descriptor, but don't install a descriptor yet.
1076 error = falloc_noinstall(td, &fp);
1080 * An extra reference on `fp' has been held for us by
1081 * falloc_noinstall().
1083 /* Set the flags early so the finit in devfs can pick them up. */
1084 fp->f_flag = flags & FMASK;
1085 cmode = ((mode & ~fdp->fd_cmask) & ALLPERMS) & ~S_ISTXT;
1086 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, fd,
1088 td->td_dupfd = -1; /* XXX check for fdopen */
1089 error = vn_open(&nd, &flags, cmode, fp);
1092 * If the vn_open replaced the method vector, something
1093 * wonderous happened deep below and we just pass it up
1094 * pretending we know what we do.
1096 if (error == ENXIO && fp->f_ops != &badfileops)
1100 * Handle special fdopen() case. bleh.
1102 * Don't do this for relative (capability) lookups; we don't
1103 * understand exactly what would happen, and we don't think
1104 * that it ever should.
1106 if (nd.ni_strictrelative == 0 &&
1107 (error == ENODEV || error == ENXIO) &&
1108 td->td_dupfd >= 0) {
1109 error = dupfdopen(td, fdp, td->td_dupfd, flags, error,
1118 NDFREE(&nd, NDF_ONLY_PNBUF);
1122 * Store the vnode, for any f_type. Typically, the vnode use
1123 * count is decremented by direct call to vn_closefile() for
1124 * files that switched type in the cdevsw fdopen() method.
1128 * If the file wasn't claimed by devfs bind it to the normal
1129 * vnode operations here.
1131 if (fp->f_ops == &badfileops) {
1132 KASSERT(vp->v_type != VFIFO, ("Unexpected fifo."));
1134 finit(fp, (flags & FMASK) | (fp->f_flag & FHASLOCK),
1135 DTYPE_VNODE, vp, &vnops);
1139 if (flags & O_TRUNC) {
1140 error = fo_truncate(fp, 0, td->td_ucred, td);
1146 * If we haven't already installed the FD (for dupfdopen), do so now.
1149 struct filecaps *fcaps;
1152 if (nd.ni_strictrelative == 1)
1153 fcaps = &nd.ni_filecaps;
1157 error = finstall(td, fp, &indx, flags, fcaps);
1158 /* On success finstall() consumes fcaps. */
1160 filecaps_free(&nd.ni_filecaps);
1164 filecaps_free(&nd.ni_filecaps);
1168 * Release our private reference, leaving the one associated with
1169 * the descriptor table intact.
1172 td->td_retval[0] = indx;
1175 KASSERT(indx == -1, ("indx=%d, should be -1", indx));
1184 #ifndef _SYS_SYSPROTO_H_
1185 struct ocreat_args {
1193 register struct ocreat_args /* {
1199 return (kern_openat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1200 O_WRONLY | O_CREAT | O_TRUNC, uap->mode));
1202 #endif /* COMPAT_43 */
1205 * Create a special file.
1207 #ifndef _SYS_SYSPROTO_H_
1217 register struct mknod_args /* {
1224 return (kern_mknodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1225 uap->mode, uap->dev));
1228 #ifndef _SYS_SYSPROTO_H_
1229 struct mknodat_args {
1237 sys_mknodat(struct thread *td, struct mknodat_args *uap)
1240 return (kern_mknodat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode,
1245 kern_mknodat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1251 struct nameidata nd;
1252 cap_rights_t rights;
1253 int error, whiteout = 0;
1255 AUDIT_ARG_MODE(mode);
1257 switch (mode & S_IFMT) {
1260 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
1263 error = priv_check(td, PRIV_VFS_MKNOD_BAD);
1266 error = priv_check(td, PRIV_VFS_MKNOD_WHT);
1270 return (kern_mkfifoat(td, fd, path, pathseg, mode));
1280 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
1281 NOCACHE, pathseg, path, fd, cap_rights_init(&rights, CAP_MKNODAT),
1283 if ((error = namei(&nd)) != 0)
1287 NDFREE(&nd, NDF_ONLY_PNBUF);
1288 if (vp == nd.ni_dvp)
1296 vattr.va_mode = (mode & ALLPERMS) &
1297 ~td->td_proc->p_fd->fd_cmask;
1298 vattr.va_rdev = dev;
1301 switch (mode & S_IFMT) {
1302 case S_IFMT: /* used by badsect to flag bad sectors */
1303 vattr.va_type = VBAD;
1306 vattr.va_type = VCHR;
1309 vattr.va_type = VBLK;
1315 panic("kern_mknod: invalid mode");
1318 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1319 NDFREE(&nd, NDF_ONLY_PNBUF);
1321 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1326 if (error == 0 && !whiteout)
1327 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp,
1328 &nd.ni_cnd, &vattr);
1332 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, CREATE);
1334 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp,
1335 &nd.ni_cnd, &vattr);
1340 NDFREE(&nd, NDF_ONLY_PNBUF);
1342 vn_finished_write(mp);
1347 * Create a named pipe.
1349 #ifndef _SYS_SYSPROTO_H_
1350 struct mkfifo_args {
1358 register struct mkfifo_args /* {
1364 return (kern_mkfifoat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1368 #ifndef _SYS_SYSPROTO_H_
1369 struct mkfifoat_args {
1376 sys_mkfifoat(struct thread *td, struct mkfifoat_args *uap)
1379 return (kern_mkfifoat(td, uap->fd, uap->path, UIO_USERSPACE,
1384 kern_mkfifoat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1389 struct nameidata nd;
1390 cap_rights_t rights;
1393 AUDIT_ARG_MODE(mode);
1396 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
1397 NOCACHE, pathseg, path, fd, cap_rights_init(&rights, CAP_MKFIFOAT),
1399 if ((error = namei(&nd)) != 0)
1401 if (nd.ni_vp != NULL) {
1402 NDFREE(&nd, NDF_ONLY_PNBUF);
1403 if (nd.ni_vp == nd.ni_dvp)
1410 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1411 NDFREE(&nd, NDF_ONLY_PNBUF);
1413 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1418 vattr.va_type = VFIFO;
1419 vattr.va_mode = (mode & ALLPERMS) & ~td->td_proc->p_fd->fd_cmask;
1421 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1426 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
1433 vn_finished_write(mp);
1434 NDFREE(&nd, NDF_ONLY_PNBUF);
1439 * Make a hard file link.
1441 #ifndef _SYS_SYSPROTO_H_
1450 register struct link_args /* {
1456 return (kern_linkat(td, AT_FDCWD, AT_FDCWD, uap->path, uap->link,
1457 UIO_USERSPACE, FOLLOW));
1460 #ifndef _SYS_SYSPROTO_H_
1461 struct linkat_args {
1470 sys_linkat(struct thread *td, struct linkat_args *uap)
1475 if (flag & ~AT_SYMLINK_FOLLOW)
1478 return (kern_linkat(td, uap->fd1, uap->fd2, uap->path1, uap->path2,
1479 UIO_USERSPACE, (flag & AT_SYMLINK_FOLLOW) ? FOLLOW : NOFOLLOW));
1482 int hardlink_check_uid = 0;
1483 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
1484 &hardlink_check_uid, 0,
1485 "Unprivileged processes cannot create hard links to files owned by other "
1487 static int hardlink_check_gid = 0;
1488 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
1489 &hardlink_check_gid, 0,
1490 "Unprivileged processes cannot create hard links to files owned by other "
1494 can_hardlink(struct vnode *vp, struct ucred *cred)
1499 if (!hardlink_check_uid && !hardlink_check_gid)
1502 error = VOP_GETATTR(vp, &va, cred);
1506 if (hardlink_check_uid && cred->cr_uid != va.va_uid) {
1507 error = priv_check_cred(cred, PRIV_VFS_LINK, 0);
1512 if (hardlink_check_gid && !groupmember(va.va_gid, cred)) {
1513 error = priv_check_cred(cred, PRIV_VFS_LINK, 0);
1522 kern_linkat(struct thread *td, int fd1, int fd2, char *path1, char *path2,
1523 enum uio_seg segflg, int follow)
1527 struct nameidata nd;
1528 cap_rights_t rights;
1533 NDINIT_AT(&nd, LOOKUP, follow | AUDITVNODE1, segflg, path1, fd1, td);
1535 if ((error = namei(&nd)) != 0)
1537 NDFREE(&nd, NDF_ONLY_PNBUF);
1539 if (vp->v_type == VDIR) {
1541 return (EPERM); /* POSIX */
1543 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE2 |
1544 NOCACHE, segflg, path2, fd2, cap_rights_init(&rights, CAP_LINKAT),
1546 if ((error = namei(&nd)) == 0) {
1547 if (nd.ni_vp != NULL) {
1548 NDFREE(&nd, NDF_ONLY_PNBUF);
1549 if (nd.ni_dvp == nd.ni_vp)
1556 } else if (nd.ni_dvp->v_mount != vp->v_mount) {
1558 * Cross-device link. No need to recheck
1559 * vp->v_type, since it cannot change, except
1562 NDFREE(&nd, NDF_ONLY_PNBUF);
1566 } else if ((error = vn_lock(vp, LK_EXCLUSIVE)) == 0) {
1567 error = can_hardlink(vp, td->td_ucred);
1570 error = mac_vnode_check_link(td->td_ucred,
1571 nd.ni_dvp, vp, &nd.ni_cnd);
1576 NDFREE(&nd, NDF_ONLY_PNBUF);
1579 error = vn_start_write(vp, &mp, V_NOWAIT);
1583 NDFREE(&nd, NDF_ONLY_PNBUF);
1584 error = vn_start_write(NULL, &mp,
1590 error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
1593 vn_finished_write(mp);
1594 NDFREE(&nd, NDF_ONLY_PNBUF);
1597 NDFREE(&nd, NDF_ONLY_PNBUF);
1607 * Make a symbolic link.
1609 #ifndef _SYS_SYSPROTO_H_
1610 struct symlink_args {
1616 sys_symlink(td, uap)
1618 register struct symlink_args /* {
1624 return (kern_symlinkat(td, uap->path, AT_FDCWD, uap->link,
1628 #ifndef _SYS_SYSPROTO_H_
1629 struct symlinkat_args {
1636 sys_symlinkat(struct thread *td, struct symlinkat_args *uap)
1639 return (kern_symlinkat(td, uap->path1, uap->fd, uap->path2,
1644 kern_symlinkat(struct thread *td, char *path1, int fd, char *path2,
1645 enum uio_seg segflg)
1650 struct nameidata nd;
1652 cap_rights_t rights;
1654 if (segflg == UIO_SYSSPACE) {
1657 syspath = uma_zalloc(namei_zone, M_WAITOK);
1658 if ((error = copyinstr(path1, syspath, MAXPATHLEN, NULL)) != 0)
1661 AUDIT_ARG_TEXT(syspath);
1664 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
1665 NOCACHE, segflg, path2, fd, cap_rights_init(&rights, CAP_SYMLINKAT),
1667 if ((error = namei(&nd)) != 0)
1670 NDFREE(&nd, NDF_ONLY_PNBUF);
1671 if (nd.ni_vp == nd.ni_dvp)
1679 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1680 NDFREE(&nd, NDF_ONLY_PNBUF);
1682 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1687 vattr.va_mode = ACCESSPERMS &~ td->td_proc->p_fd->fd_cmask;
1689 vattr.va_type = VLNK;
1690 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1695 error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr, syspath);
1701 NDFREE(&nd, NDF_ONLY_PNBUF);
1703 vn_finished_write(mp);
1705 if (segflg != UIO_SYSSPACE)
1706 uma_zfree(namei_zone, syspath);
1711 * Delete a whiteout from the filesystem.
1714 sys_undelete(td, uap)
1716 register struct undelete_args /* {
1721 struct nameidata nd;
1726 NDINIT(&nd, DELETE, LOCKPARENT | DOWHITEOUT | AUDITVNODE1,
1727 UIO_USERSPACE, uap->path, td);
1732 if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & ISWHITEOUT)) {
1733 NDFREE(&nd, NDF_ONLY_PNBUF);
1734 if (nd.ni_vp == nd.ni_dvp)
1742 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1743 NDFREE(&nd, NDF_ONLY_PNBUF);
1745 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1749 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, DELETE);
1750 NDFREE(&nd, NDF_ONLY_PNBUF);
1752 vn_finished_write(mp);
1757 * Delete a name from the filesystem.
1759 #ifndef _SYS_SYSPROTO_H_
1760 struct unlink_args {
1767 struct unlink_args /* {
1772 return (kern_unlinkat(td, AT_FDCWD, uap->path, UIO_USERSPACE, 0));
1775 #ifndef _SYS_SYSPROTO_H_
1776 struct unlinkat_args {
1783 sys_unlinkat(struct thread *td, struct unlinkat_args *uap)
1785 int flag = uap->flag;
1787 char *path = uap->path;
1789 if (flag & ~AT_REMOVEDIR)
1792 if (flag & AT_REMOVEDIR)
1793 return (kern_rmdirat(td, fd, path, UIO_USERSPACE));
1795 return (kern_unlinkat(td, fd, path, UIO_USERSPACE, 0));
1799 kern_unlinkat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1804 struct nameidata nd;
1806 cap_rights_t rights;
1811 NDINIT_ATRIGHTS(&nd, DELETE, LOCKPARENT | LOCKLEAF | AUDITVNODE1,
1812 pathseg, path, fd, cap_rights_init(&rights, CAP_UNLINKAT), td);
1813 if ((error = namei(&nd)) != 0)
1814 return (error == EINVAL ? EPERM : error);
1816 if (vp->v_type == VDIR && oldinum == 0) {
1817 error = EPERM; /* POSIX */
1818 } else if (oldinum != 0 &&
1819 ((error = vn_stat(vp, &sb, td->td_ucred, NOCRED, td)) == 0) &&
1820 sb.st_ino != oldinum) {
1821 error = EIDRM; /* Identifier removed */
1824 * The root of a mounted filesystem cannot be deleted.
1826 * XXX: can this only be a VDIR case?
1828 if (vp->v_vflag & VV_ROOT)
1832 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1833 NDFREE(&nd, NDF_ONLY_PNBUF);
1835 if (vp == nd.ni_dvp)
1839 if ((error = vn_start_write(NULL, &mp,
1840 V_XSLEEP | PCATCH)) != 0)
1845 error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp,
1850 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_UNLINK);
1851 error = VOP_REMOVE(nd.ni_dvp, vp, &nd.ni_cnd);
1855 vn_finished_write(mp);
1857 NDFREE(&nd, NDF_ONLY_PNBUF);
1859 if (vp == nd.ni_dvp)
1867 * Reposition read/write file offset.
1869 #ifndef _SYS_SYSPROTO_H_
1880 register struct lseek_args /* {
1888 cap_rights_t rights;
1891 AUDIT_ARG_FD(uap->fd);
1892 error = fget(td, uap->fd, cap_rights_init(&rights, CAP_SEEK), &fp);
1895 error = (fp->f_ops->fo_flags & DFLAG_SEEKABLE) != 0 ?
1896 fo_seek(fp, uap->offset, uap->whence, td) : ESPIPE;
1901 #if defined(COMPAT_43)
1903 * Reposition read/write file offset.
1905 #ifndef _SYS_SYSPROTO_H_
1906 struct olseek_args {
1915 register struct olseek_args /* {
1921 struct lseek_args /* {
1929 nuap.offset = uap->offset;
1930 nuap.whence = uap->whence;
1931 return (sys_lseek(td, &nuap));
1933 #endif /* COMPAT_43 */
1935 #if defined(COMPAT_FREEBSD6)
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));
1952 * Check access permissions using passed credentials.
1955 vn_access(vp, user_flags, cred, td)
1964 /* Flags == 0 means only check for existence. */
1965 if (user_flags == 0)
1969 if (user_flags & R_OK)
1971 if (user_flags & W_OK)
1973 if (user_flags & X_OK)
1976 error = mac_vnode_check_access(cred, vp, accmode);
1980 if ((accmode & VWRITE) == 0 || (error = vn_writechk(vp)) == 0)
1981 error = VOP_ACCESS(vp, accmode, cred, td);
1986 * Check access permissions using "real" credentials.
1988 #ifndef _SYS_SYSPROTO_H_
1989 struct access_args {
1997 register struct access_args /* {
2003 return (kern_accessat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2007 #ifndef _SYS_SYSPROTO_H_
2008 struct faccessat_args {
2016 sys_faccessat(struct thread *td, struct faccessat_args *uap)
2019 return (kern_accessat(td, uap->fd, uap->path, UIO_USERSPACE, uap->flag,
2024 kern_accessat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2025 int flag, int amode)
2027 struct ucred *cred, *usecred;
2029 struct nameidata nd;
2030 cap_rights_t rights;
2033 if (flag & ~AT_EACCESS)
2035 if (amode != F_OK && (amode & ~(R_OK | W_OK | X_OK)) != 0)
2039 * Create and modify a temporary credential instead of one that
2040 * is potentially shared (if we need one).
2042 cred = td->td_ucred;
2043 if ((flag & AT_EACCESS) == 0 &&
2044 ((cred->cr_uid != cred->cr_ruid ||
2045 cred->cr_rgid != cred->cr_groups[0]))) {
2046 usecred = crdup(cred);
2047 usecred->cr_uid = cred->cr_ruid;
2048 usecred->cr_groups[0] = cred->cr_rgid;
2049 td->td_ucred = usecred;
2052 AUDIT_ARG_VALUE(amode);
2053 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF |
2054 AUDITVNODE1, pathseg, path, fd, cap_rights_init(&rights, CAP_FSTAT),
2056 if ((error = namei(&nd)) != 0)
2060 error = vn_access(vp, amode, usecred, td);
2061 NDFREE(&nd, NDF_ONLY_PNBUF);
2064 if (usecred != cred) {
2065 td->td_ucred = cred;
2072 * Check access permissions using "effective" credentials.
2074 #ifndef _SYS_SYSPROTO_H_
2075 struct eaccess_args {
2081 sys_eaccess(td, uap)
2083 register struct eaccess_args /* {
2089 return (kern_accessat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2090 AT_EACCESS, uap->amode));
2093 #if defined(COMPAT_43)
2095 * Get file status; this version follows links.
2097 #ifndef _SYS_SYSPROTO_H_
2106 register struct ostat_args /* {
2115 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
2120 return (copyout(&osb, uap->ub, sizeof (osb)));
2124 * Get file status; this version does not follow links.
2126 #ifndef _SYS_SYSPROTO_H_
2127 struct olstat_args {
2135 register struct olstat_args /* {
2144 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2145 UIO_USERSPACE, &sb, NULL);
2149 return (copyout(&osb, uap->ub, sizeof (osb)));
2153 * Convert from an old to a new stat structure.
2161 ost->st_dev = st->st_dev;
2162 ost->st_ino = st->st_ino;
2163 ost->st_mode = st->st_mode;
2164 ost->st_nlink = st->st_nlink;
2165 ost->st_uid = st->st_uid;
2166 ost->st_gid = st->st_gid;
2167 ost->st_rdev = st->st_rdev;
2168 if (st->st_size < (quad_t)1 << 32)
2169 ost->st_size = st->st_size;
2172 ost->st_atim = st->st_atim;
2173 ost->st_mtim = st->st_mtim;
2174 ost->st_ctim = st->st_ctim;
2175 ost->st_blksize = st->st_blksize;
2176 ost->st_blocks = st->st_blocks;
2177 ost->st_flags = st->st_flags;
2178 ost->st_gen = st->st_gen;
2180 #endif /* COMPAT_43 */
2183 * Get file status; this version follows links.
2185 #ifndef _SYS_SYSPROTO_H_
2194 register struct stat_args /* {
2202 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
2205 error = copyout(&sb, uap->ub, sizeof (sb));
2209 #ifndef _SYS_SYSPROTO_H_
2210 struct fstatat_args {
2218 sys_fstatat(struct thread *td, struct fstatat_args *uap)
2223 error = kern_statat(td, uap->flag, uap->fd, uap->path,
2224 UIO_USERSPACE, &sb, NULL);
2226 error = copyout(&sb, uap->buf, sizeof (sb));
2231 kern_statat(struct thread *td, int flag, int fd, char *path,
2232 enum uio_seg pathseg, struct stat *sbp,
2233 void (*hook)(struct vnode *vp, struct stat *sbp))
2235 struct nameidata nd;
2237 cap_rights_t rights;
2240 if (flag & ~AT_SYMLINK_NOFOLLOW)
2243 NDINIT_ATRIGHTS(&nd, LOOKUP, ((flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW :
2244 FOLLOW) | LOCKSHARED | LOCKLEAF | AUDITVNODE1, pathseg, path, fd,
2245 cap_rights_init(&rights, CAP_FSTAT), td);
2247 if ((error = namei(&nd)) != 0)
2249 error = vn_stat(nd.ni_vp, &sb, td->td_ucred, NOCRED, td);
2251 SDT_PROBE(vfs, , stat, mode, path, sb.st_mode, 0, 0, 0);
2252 if (S_ISREG(sb.st_mode))
2253 SDT_PROBE(vfs, , stat, reg, path, pathseg, 0, 0, 0);
2254 if (__predict_false(hook != NULL))
2255 hook(nd.ni_vp, &sb);
2257 NDFREE(&nd, NDF_ONLY_PNBUF);
2263 if (KTRPOINT(td, KTR_STRUCT))
2270 * Get file status; this version does not follow links.
2272 #ifndef _SYS_SYSPROTO_H_
2281 register struct lstat_args /* {
2289 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2290 UIO_USERSPACE, &sb, NULL);
2292 error = copyout(&sb, uap->ub, sizeof (sb));
2297 * Implementation of the NetBSD [l]stat() functions.
2305 bzero(nsb, sizeof *nsb);
2306 nsb->st_dev = sb->st_dev;
2307 nsb->st_ino = sb->st_ino;
2308 nsb->st_mode = sb->st_mode;
2309 nsb->st_nlink = sb->st_nlink;
2310 nsb->st_uid = sb->st_uid;
2311 nsb->st_gid = sb->st_gid;
2312 nsb->st_rdev = sb->st_rdev;
2313 nsb->st_atim = sb->st_atim;
2314 nsb->st_mtim = sb->st_mtim;
2315 nsb->st_ctim = sb->st_ctim;
2316 nsb->st_size = sb->st_size;
2317 nsb->st_blocks = sb->st_blocks;
2318 nsb->st_blksize = sb->st_blksize;
2319 nsb->st_flags = sb->st_flags;
2320 nsb->st_gen = sb->st_gen;
2321 nsb->st_birthtim = sb->st_birthtim;
2324 #ifndef _SYS_SYSPROTO_H_
2333 register struct nstat_args /* {
2342 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
2346 cvtnstat(&sb, &nsb);
2347 return (copyout(&nsb, uap->ub, sizeof (nsb)));
2351 * NetBSD lstat. Get file status; this version does not follow links.
2353 #ifndef _SYS_SYSPROTO_H_
2362 register struct nlstat_args /* {
2371 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2372 UIO_USERSPACE, &sb, NULL);
2375 cvtnstat(&sb, &nsb);
2376 return (copyout(&nsb, uap->ub, sizeof (nsb)));
2380 * Get configurable pathname variables.
2382 #ifndef _SYS_SYSPROTO_H_
2383 struct pathconf_args {
2389 sys_pathconf(td, uap)
2391 register struct pathconf_args /* {
2397 return (kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name, FOLLOW));
2400 #ifndef _SYS_SYSPROTO_H_
2401 struct lpathconf_args {
2407 sys_lpathconf(td, uap)
2409 register struct lpathconf_args /* {
2415 return (kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name,
2420 kern_pathconf(struct thread *td, char *path, enum uio_seg pathseg, int name,
2423 struct nameidata nd;
2426 NDINIT(&nd, LOOKUP, LOCKSHARED | LOCKLEAF | AUDITVNODE1 | flags,
2428 if ((error = namei(&nd)) != 0)
2430 NDFREE(&nd, NDF_ONLY_PNBUF);
2432 /* If asynchronous I/O is available, it works for all files. */
2433 if (name == _PC_ASYNC_IO)
2434 td->td_retval[0] = async_io_version;
2436 error = VOP_PATHCONF(nd.ni_vp, name, td->td_retval);
2442 * Return target name of a symbolic link.
2444 #ifndef _SYS_SYSPROTO_H_
2445 struct readlink_args {
2452 sys_readlink(td, uap)
2454 register struct readlink_args /* {
2461 return (kern_readlinkat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2462 uap->buf, UIO_USERSPACE, uap->count));
2464 #ifndef _SYS_SYSPROTO_H_
2465 struct readlinkat_args {
2473 sys_readlinkat(struct thread *td, struct readlinkat_args *uap)
2476 return (kern_readlinkat(td, uap->fd, uap->path, UIO_USERSPACE,
2477 uap->buf, UIO_USERSPACE, uap->bufsize));
2481 kern_readlinkat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2482 char *buf, enum uio_seg bufseg, size_t count)
2487 struct nameidata nd;
2490 if (count > IOSIZE_MAX)
2493 NDINIT_AT(&nd, LOOKUP, NOFOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
2494 pathseg, path, fd, td);
2496 if ((error = namei(&nd)) != 0)
2498 NDFREE(&nd, NDF_ONLY_PNBUF);
2501 error = mac_vnode_check_readlink(td->td_ucred, vp);
2507 if (vp->v_type != VLNK)
2510 aiov.iov_base = buf;
2511 aiov.iov_len = count;
2512 auio.uio_iov = &aiov;
2513 auio.uio_iovcnt = 1;
2514 auio.uio_offset = 0;
2515 auio.uio_rw = UIO_READ;
2516 auio.uio_segflg = bufseg;
2518 auio.uio_resid = count;
2519 error = VOP_READLINK(vp, &auio, td->td_ucred);
2520 td->td_retval[0] = count - auio.uio_resid;
2527 * Common implementation code for chflags() and fchflags().
2530 setfflags(td, vp, flags)
2539 /* We can't support the value matching VNOVAL. */
2540 if (flags == VNOVAL)
2541 return (EOPNOTSUPP);
2544 * Prevent non-root users from setting flags on devices. When
2545 * a device is reused, users can retain ownership of the device
2546 * if they are allowed to set flags and programs assume that
2547 * chown can't fail when done as root.
2549 if (vp->v_type == VCHR || vp->v_type == VBLK) {
2550 error = priv_check(td, PRIV_VFS_CHFLAGS_DEV);
2555 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2558 vattr.va_flags = flags;
2559 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2561 error = mac_vnode_check_setflags(td->td_ucred, vp, vattr.va_flags);
2564 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
2566 vn_finished_write(mp);
2571 * Change flags of a file given a path name.
2573 #ifndef _SYS_SYSPROTO_H_
2574 struct chflags_args {
2580 sys_chflags(td, uap)
2582 register struct chflags_args /* {
2588 return (kern_chflagsat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2592 #ifndef _SYS_SYSPROTO_H_
2593 struct chflagsat_args {
2601 sys_chflagsat(struct thread *td, struct chflagsat_args *uap)
2604 const char *path = uap->path;
2605 u_long flags = uap->flags;
2606 int atflag = uap->atflag;
2608 if (atflag & ~AT_SYMLINK_NOFOLLOW)
2611 return (kern_chflagsat(td, fd, path, UIO_USERSPACE, flags, atflag));
2615 * Same as chflags() but doesn't follow symlinks.
2618 sys_lchflags(td, uap)
2620 register struct lchflags_args /* {
2626 return (kern_chflagsat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2627 uap->flags, AT_SYMLINK_NOFOLLOW));
2631 kern_chflagsat(struct thread *td, int fd, const char *path,
2632 enum uio_seg pathseg, u_long flags, int atflag)
2634 struct nameidata nd;
2635 cap_rights_t rights;
2638 AUDIT_ARG_FFLAGS(flags);
2639 follow = (atflag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
2640 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, pathseg, path, fd,
2641 cap_rights_init(&rights, CAP_FCHFLAGS), td);
2642 if ((error = namei(&nd)) != 0)
2644 NDFREE(&nd, NDF_ONLY_PNBUF);
2645 error = setfflags(td, nd.ni_vp, flags);
2651 * Change flags of a file given a file descriptor.
2653 #ifndef _SYS_SYSPROTO_H_
2654 struct fchflags_args {
2660 sys_fchflags(td, uap)
2662 register struct fchflags_args /* {
2668 cap_rights_t rights;
2671 AUDIT_ARG_FD(uap->fd);
2672 AUDIT_ARG_FFLAGS(uap->flags);
2673 error = getvnode(td, uap->fd, cap_rights_init(&rights, CAP_FCHFLAGS),
2678 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
2679 AUDIT_ARG_VNODE1(fp->f_vnode);
2680 VOP_UNLOCK(fp->f_vnode, 0);
2682 error = setfflags(td, fp->f_vnode, uap->flags);
2688 * Common implementation code for chmod(), lchmod() and fchmod().
2691 setfmode(td, cred, vp, mode)
2701 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2703 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2705 vattr.va_mode = mode & ALLPERMS;
2707 error = mac_vnode_check_setmode(cred, vp, vattr.va_mode);
2710 error = VOP_SETATTR(vp, &vattr, cred);
2712 vn_finished_write(mp);
2717 * Change mode of a file given path name.
2719 #ifndef _SYS_SYSPROTO_H_
2728 register struct chmod_args /* {
2734 return (kern_fchmodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2738 #ifndef _SYS_SYSPROTO_H_
2739 struct fchmodat_args {
2747 sys_fchmodat(struct thread *td, struct fchmodat_args *uap)
2749 int flag = uap->flag;
2751 char *path = uap->path;
2752 mode_t mode = uap->mode;
2754 if (flag & ~AT_SYMLINK_NOFOLLOW)
2757 return (kern_fchmodat(td, fd, path, UIO_USERSPACE, mode, flag));
2761 * Change mode of a file given path name (don't follow links.)
2763 #ifndef _SYS_SYSPROTO_H_
2764 struct lchmod_args {
2772 register struct lchmod_args /* {
2778 return (kern_fchmodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2779 uap->mode, AT_SYMLINK_NOFOLLOW));
2783 kern_fchmodat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2784 mode_t mode, int flag)
2786 struct nameidata nd;
2787 cap_rights_t rights;
2790 AUDIT_ARG_MODE(mode);
2791 follow = (flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
2792 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, pathseg, path, fd,
2793 cap_rights_init(&rights, CAP_FCHMOD), td);
2794 if ((error = namei(&nd)) != 0)
2796 NDFREE(&nd, NDF_ONLY_PNBUF);
2797 error = setfmode(td, td->td_ucred, nd.ni_vp, mode);
2803 * Change mode of a file given a file descriptor.
2805 #ifndef _SYS_SYSPROTO_H_
2806 struct fchmod_args {
2812 sys_fchmod(struct thread *td, struct fchmod_args *uap)
2815 cap_rights_t rights;
2818 AUDIT_ARG_FD(uap->fd);
2819 AUDIT_ARG_MODE(uap->mode);
2821 error = fget(td, uap->fd, cap_rights_init(&rights, CAP_FCHMOD), &fp);
2824 error = fo_chmod(fp, uap->mode, td->td_ucred, td);
2830 * Common implementation for chown(), lchown(), and fchown()
2833 setfown(td, cred, vp, uid, gid)
2844 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2846 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2851 error = mac_vnode_check_setowner(cred, vp, vattr.va_uid,
2855 error = VOP_SETATTR(vp, &vattr, cred);
2857 vn_finished_write(mp);
2862 * Set ownership given a path name.
2864 #ifndef _SYS_SYSPROTO_H_
2874 register struct chown_args /* {
2881 return (kern_fchownat(td, AT_FDCWD, uap->path, UIO_USERSPACE, uap->uid,
2885 #ifndef _SYS_SYSPROTO_H_
2886 struct fchownat_args {
2895 sys_fchownat(struct thread *td, struct fchownat_args *uap)
2900 if (flag & ~AT_SYMLINK_NOFOLLOW)
2903 return (kern_fchownat(td, uap->fd, uap->path, UIO_USERSPACE, uap->uid,
2904 uap->gid, uap->flag));
2908 kern_fchownat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2909 int uid, int gid, int flag)
2911 struct nameidata nd;
2912 cap_rights_t rights;
2915 AUDIT_ARG_OWNER(uid, gid);
2916 follow = (flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
2917 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, pathseg, path, fd,
2918 cap_rights_init(&rights, CAP_FCHOWN), td);
2920 if ((error = namei(&nd)) != 0)
2922 NDFREE(&nd, NDF_ONLY_PNBUF);
2923 error = setfown(td, td->td_ucred, nd.ni_vp, uid, gid);
2929 * Set ownership given a path name, do not cross symlinks.
2931 #ifndef _SYS_SYSPROTO_H_
2932 struct lchown_args {
2941 register struct lchown_args /* {
2948 return (kern_fchownat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2949 uap->uid, uap->gid, AT_SYMLINK_NOFOLLOW));
2953 * Set ownership given a file descriptor.
2955 #ifndef _SYS_SYSPROTO_H_
2956 struct fchown_args {
2965 register struct fchown_args /* {
2972 cap_rights_t rights;
2975 AUDIT_ARG_FD(uap->fd);
2976 AUDIT_ARG_OWNER(uap->uid, uap->gid);
2977 error = fget(td, uap->fd, cap_rights_init(&rights, CAP_FCHOWN), &fp);
2980 error = fo_chown(fp, uap->uid, uap->gid, td->td_ucred, td);
2986 * Common implementation code for utimes(), lutimes(), and futimes().
2989 getutimes(usrtvp, tvpseg, tsp)
2990 const struct timeval *usrtvp;
2991 enum uio_seg tvpseg;
2992 struct timespec *tsp;
2994 struct timeval tv[2];
2995 const struct timeval *tvp;
2998 if (usrtvp == NULL) {
2999 vfs_timestamp(&tsp[0]);
3002 if (tvpseg == UIO_SYSSPACE) {
3005 if ((error = copyin(usrtvp, tv, sizeof(tv))) != 0)
3010 if (tvp[0].tv_usec < 0 || tvp[0].tv_usec >= 1000000 ||
3011 tvp[1].tv_usec < 0 || tvp[1].tv_usec >= 1000000)
3013 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
3014 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
3020 * Common implementation code for futimens(), utimensat().
3022 #define UTIMENS_NULL 0x1
3023 #define UTIMENS_EXIT 0x2
3025 getutimens(const struct timespec *usrtsp, enum uio_seg tspseg,
3026 struct timespec *tsp, int *retflags)
3028 struct timespec tsnow;
3031 vfs_timestamp(&tsnow);
3033 if (usrtsp == NULL) {
3036 *retflags |= UTIMENS_NULL;
3039 if (tspseg == UIO_SYSSPACE) {
3042 } else if ((error = copyin(usrtsp, tsp, sizeof(*tsp) * 2)) != 0)
3044 if (tsp[0].tv_nsec == UTIME_OMIT && tsp[1].tv_nsec == UTIME_OMIT)
3045 *retflags |= UTIMENS_EXIT;
3046 if (tsp[0].tv_nsec == UTIME_NOW && tsp[1].tv_nsec == UTIME_NOW)
3047 *retflags |= UTIMENS_NULL;
3048 if (tsp[0].tv_nsec == UTIME_OMIT)
3049 tsp[0].tv_sec = VNOVAL;
3050 else if (tsp[0].tv_nsec == UTIME_NOW)
3052 else if (tsp[0].tv_nsec < 0 || tsp[0].tv_nsec >= 1000000000L)
3054 if (tsp[1].tv_nsec == UTIME_OMIT)
3055 tsp[1].tv_sec = VNOVAL;
3056 else if (tsp[1].tv_nsec == UTIME_NOW)
3058 else if (tsp[1].tv_nsec < 0 || tsp[1].tv_nsec >= 1000000000L)
3065 * Common implementation code for utimes(), lutimes(), futimes(), futimens(),
3069 setutimes(td, vp, ts, numtimes, nullflag)
3072 const struct timespec *ts;
3078 int error, setbirthtime;
3080 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
3082 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3084 if (numtimes < 3 && !VOP_GETATTR(vp, &vattr, td->td_ucred) &&
3085 timespeccmp(&ts[1], &vattr.va_birthtime, < ))
3088 vattr.va_atime = ts[0];
3089 vattr.va_mtime = ts[1];
3091 vattr.va_birthtime = ts[1];
3093 vattr.va_birthtime = ts[2];
3095 vattr.va_vaflags |= VA_UTIMES_NULL;
3097 error = mac_vnode_check_setutimes(td->td_ucred, vp, vattr.va_atime,
3101 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3103 vn_finished_write(mp);
3108 * Set the access and modification times of a file.
3110 #ifndef _SYS_SYSPROTO_H_
3111 struct utimes_args {
3113 struct timeval *tptr;
3119 register struct utimes_args /* {
3121 struct timeval *tptr;
3125 return (kern_utimesat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
3126 uap->tptr, UIO_USERSPACE));
3129 #ifndef _SYS_SYSPROTO_H_
3130 struct futimesat_args {
3133 const struct timeval * times;
3137 sys_futimesat(struct thread *td, struct futimesat_args *uap)
3140 return (kern_utimesat(td, uap->fd, uap->path, UIO_USERSPACE,
3141 uap->times, UIO_USERSPACE));
3145 kern_utimesat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
3146 struct timeval *tptr, enum uio_seg tptrseg)
3148 struct nameidata nd;
3149 struct timespec ts[2];
3150 cap_rights_t rights;
3153 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3155 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, fd,
3156 cap_rights_init(&rights, CAP_FUTIMES), td);
3158 if ((error = namei(&nd)) != 0)
3160 NDFREE(&nd, NDF_ONLY_PNBUF);
3161 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3167 * Set the access and modification times of a file.
3169 #ifndef _SYS_SYSPROTO_H_
3170 struct lutimes_args {
3172 struct timeval *tptr;
3176 sys_lutimes(td, uap)
3178 register struct lutimes_args /* {
3180 struct timeval *tptr;
3184 return (kern_lutimes(td, uap->path, UIO_USERSPACE, uap->tptr,
3189 kern_lutimes(struct thread *td, char *path, enum uio_seg pathseg,
3190 struct timeval *tptr, enum uio_seg tptrseg)
3192 struct timespec ts[2];
3193 struct nameidata nd;
3196 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3198 NDINIT(&nd, LOOKUP, NOFOLLOW | AUDITVNODE1, pathseg, path, td);
3199 if ((error = namei(&nd)) != 0)
3201 NDFREE(&nd, NDF_ONLY_PNBUF);
3202 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3208 * Set the access and modification times of a file.
3210 #ifndef _SYS_SYSPROTO_H_
3211 struct futimes_args {
3213 struct timeval *tptr;
3217 sys_futimes(td, uap)
3219 register struct futimes_args /* {
3221 struct timeval *tptr;
3225 return (kern_futimes(td, uap->fd, uap->tptr, UIO_USERSPACE));
3229 kern_futimes(struct thread *td, int fd, struct timeval *tptr,
3230 enum uio_seg tptrseg)
3232 struct timespec ts[2];
3234 cap_rights_t rights;
3238 error = getutimes(tptr, tptrseg, ts);
3241 error = getvnode(td, fd, 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, fd, cap_rights_init(&rights, CAP_FUTIMES), &fp);
3280 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
3281 AUDIT_ARG_VNODE1(fp->f_vnode);
3282 VOP_UNLOCK(fp->f_vnode, 0);
3284 error = setutimes(td, fp->f_vnode, ts, 2, flags & UTIMENS_NULL);
3290 sys_utimensat(struct thread *td, struct utimensat_args *uap)
3293 return (kern_utimensat(td, uap->fd, uap->path, UIO_USERSPACE,
3294 uap->times, UIO_USERSPACE, uap->flag));
3298 kern_utimensat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
3299 struct timespec *tptr, enum uio_seg tptrseg, int flag)
3301 struct nameidata nd;
3302 struct timespec ts[2];
3303 cap_rights_t rights;
3306 if (flag & ~AT_SYMLINK_NOFOLLOW)
3309 if ((error = getutimens(tptr, tptrseg, ts, &flags)) != 0)
3311 NDINIT_ATRIGHTS(&nd, LOOKUP, ((flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW :
3312 FOLLOW) | AUDITVNODE1, pathseg, path, fd,
3313 cap_rights_init(&rights, CAP_FUTIMES), td);
3314 if ((error = namei(&nd)) != 0)
3317 * We are allowed to call namei() regardless of 2xUTIME_OMIT.
3319 * "If both tv_nsec fields are UTIME_OMIT... EACCESS may be detected."
3320 * "Search permission is denied by a component of the path prefix."
3322 NDFREE(&nd, NDF_ONLY_PNBUF);
3323 if ((flags & UTIMENS_EXIT) == 0)
3324 error = setutimes(td, nd.ni_vp, ts, 2, flags & UTIMENS_NULL);
3330 * Truncate a file given its path name.
3332 #ifndef _SYS_SYSPROTO_H_
3333 struct truncate_args {
3340 sys_truncate(td, uap)
3342 register struct truncate_args /* {
3349 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3353 kern_truncate(struct thread *td, char *path, enum uio_seg pathseg, off_t length)
3359 struct nameidata nd;
3364 NDINIT(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, td);
3365 if ((error = namei(&nd)) != 0)
3368 rl_cookie = vn_rangelock_wlock(vp, 0, OFF_MAX);
3369 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0) {
3370 vn_rangelock_unlock(vp, rl_cookie);
3374 NDFREE(&nd, NDF_ONLY_PNBUF);
3375 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3376 if (vp->v_type == VDIR)
3379 else if ((error = mac_vnode_check_write(td->td_ucred, NOCRED, vp))) {
3382 else if ((error = vn_writechk(vp)) == 0 &&
3383 (error = VOP_ACCESS(vp, VWRITE, td->td_ucred, td)) == 0) {
3385 vattr.va_size = length;
3386 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3389 vn_finished_write(mp);
3390 vn_rangelock_unlock(vp, rl_cookie);
3395 #if defined(COMPAT_43)
3397 * Truncate a file given its path name.
3399 #ifndef _SYS_SYSPROTO_H_
3400 struct otruncate_args {
3408 register struct otruncate_args /* {
3413 struct truncate_args /* {
3419 nuap.path = uap->path;
3420 nuap.length = uap->length;
3421 return (sys_truncate(td, &nuap));
3423 #endif /* COMPAT_43 */
3425 #if defined(COMPAT_FREEBSD6)
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));
3449 * Sync an open file.
3451 #ifndef _SYS_SYSPROTO_H_
3459 struct fsync_args /* {
3466 cap_rights_t rights;
3467 int error, lock_flags;
3469 AUDIT_ARG_FD(uap->fd);
3470 error = getvnode(td, uap->fd, 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, uap->fd, cap_rights_init(&rights, CAP_READ), &fp);
3896 if ((fp->f_flag & FREAD) == 0) {
3901 foffset = foffset_lock(fp, 0);
3903 if (vp->v_type != VDIR) {
3904 foffset_unlock(fp, foffset, 0);
3908 aiov.iov_base = uap->buf;
3909 aiov.iov_len = uap->count;
3910 auio.uio_iov = &aiov;
3911 auio.uio_iovcnt = 1;
3912 auio.uio_rw = UIO_READ;
3913 auio.uio_segflg = UIO_USERSPACE;
3915 auio.uio_resid = uap->count;
3916 vn_lock(vp, LK_SHARED | LK_RETRY);
3917 loff = auio.uio_offset = foffset;
3919 error = mac_vnode_check_readdir(td->td_ucred, vp);
3922 foffset_unlock(fp, foffset, FOF_NOUPDATE);
3927 # if (BYTE_ORDER != LITTLE_ENDIAN)
3928 if (vp->v_mount->mnt_maxsymlinklen <= 0) {
3929 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag,
3931 foffset = auio.uio_offset;
3936 kuio.uio_iov = &kiov;
3937 kuio.uio_segflg = UIO_SYSSPACE;
3938 kiov.iov_len = uap->count;
3939 dirbuf = malloc(uap->count, M_TEMP, M_WAITOK);
3940 kiov.iov_base = dirbuf;
3941 error = VOP_READDIR(vp, &kuio, fp->f_cred, &eofflag,
3943 foffset = kuio.uio_offset;
3945 readcnt = uap->count - kuio.uio_resid;
3946 edp = (struct dirent *)&dirbuf[readcnt];
3947 for (dp = (struct dirent *)dirbuf; dp < edp; ) {
3948 # if (BYTE_ORDER == LITTLE_ENDIAN)
3950 * The expected low byte of
3951 * dp->d_namlen is our dp->d_type.
3952 * The high MBZ byte of dp->d_namlen
3953 * is our dp->d_namlen.
3955 dp->d_type = dp->d_namlen;
3959 * The dp->d_type is the high byte
3960 * of the expected dp->d_namlen,
3961 * so must be zero'ed.
3965 if (dp->d_reclen > 0) {
3966 dp = (struct dirent *)
3967 ((char *)dp + dp->d_reclen);
3974 error = uiomove(dirbuf, readcnt, &auio);
3976 free(dirbuf, M_TEMP);
3980 foffset_unlock(fp, foffset, 0);
3984 if (uap->count == auio.uio_resid &&
3985 (vp->v_vflag & VV_ROOT) &&
3986 (vp->v_mount->mnt_flag & MNT_UNION)) {
3987 struct vnode *tvp = vp;
3988 vp = vp->v_mount->mnt_vnodecovered;
3997 foffset_unlock(fp, foffset, 0);
3999 td->td_retval[0] = uap->count - auio.uio_resid;
4004 #endif /* COMPAT_43 */
4007 * Read a block of directory entries in a filesystem independent format.
4009 #ifndef _SYS_SYSPROTO_H_
4010 struct getdirentries_args {
4018 sys_getdirentries(td, uap)
4020 register struct getdirentries_args /* {
4030 error = kern_getdirentries(td, uap->fd, uap->buf, uap->count, &base,
4031 NULL, UIO_USERSPACE);
4034 if (uap->basep != NULL)
4035 error = copyout(&base, uap->basep, sizeof(long));
4040 kern_getdirentries(struct thread *td, int fd, char *buf, u_int count,
4041 long *basep, ssize_t *residp, enum uio_seg bufseg)
4047 cap_rights_t rights;
4053 if (count > IOSIZE_MAX)
4055 auio.uio_resid = count;
4056 error = getvnode(td, fd, cap_rights_init(&rights, CAP_READ), &fp);
4059 if ((fp->f_flag & FREAD) == 0) {
4064 foffset = foffset_lock(fp, 0);
4066 if (vp->v_type != VDIR) {
4070 aiov.iov_base = buf;
4071 aiov.iov_len = count;
4072 auio.uio_iov = &aiov;
4073 auio.uio_iovcnt = 1;
4074 auio.uio_rw = UIO_READ;
4075 auio.uio_segflg = bufseg;
4077 vn_lock(vp, LK_SHARED | LK_RETRY);
4078 AUDIT_ARG_VNODE1(vp);
4079 loff = auio.uio_offset = foffset;
4081 error = mac_vnode_check_readdir(td->td_ucred, vp);
4084 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL,
4086 foffset = auio.uio_offset;
4091 if (count == auio.uio_resid &&
4092 (vp->v_vflag & VV_ROOT) &&
4093 (vp->v_mount->mnt_flag & MNT_UNION)) {
4094 struct vnode *tvp = vp;
4096 vp = vp->v_mount->mnt_vnodecovered;
4107 *residp = auio.uio_resid;
4108 td->td_retval[0] = count - auio.uio_resid;
4110 foffset_unlock(fp, foffset, 0);
4115 #ifndef _SYS_SYSPROTO_H_
4116 struct getdents_args {
4123 sys_getdents(td, uap)
4125 register struct getdents_args /* {
4131 struct getdirentries_args ap;
4135 ap.count = uap->count;
4137 return (sys_getdirentries(td, &ap));
4141 * Set the mode mask for creation of filesystem nodes.
4143 #ifndef _SYS_SYSPROTO_H_
4151 struct umask_args /* {
4155 struct filedesc *fdp;
4157 fdp = td->td_proc->p_fd;
4158 FILEDESC_XLOCK(fdp);
4159 td->td_retval[0] = fdp->fd_cmask;
4160 fdp->fd_cmask = uap->newmask & ALLPERMS;
4161 FILEDESC_XUNLOCK(fdp);
4166 * Void all references to file by ripping underlying filesystem away from
4169 #ifndef _SYS_SYSPROTO_H_
4170 struct revoke_args {
4177 register struct revoke_args /* {
4183 struct nameidata nd;
4186 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
4188 if ((error = namei(&nd)) != 0)
4191 NDFREE(&nd, NDF_ONLY_PNBUF);
4192 if (vp->v_type != VCHR || vp->v_rdev == NULL) {
4197 error = mac_vnode_check_revoke(td->td_ucred, vp);
4201 error = VOP_GETATTR(vp, &vattr, td->td_ucred);
4204 if (td->td_ucred->cr_uid != vattr.va_uid) {
4205 error = priv_check(td, PRIV_VFS_ADMIN);
4210 VOP_REVOKE(vp, REVOKEALL);
4217 * Convert a user file descriptor to a kernel file entry and check that, if it
4218 * is a capability, the correct rights are present. A reference on the file
4219 * entry is held upon returning.
4222 getvnode(struct thread *td, int fd, cap_rights_t *rightsp, struct file **fpp)
4227 error = fget_unlocked(td->td_proc->p_fd, fd, rightsp, &fp, NULL);
4232 * The file could be not of the vnode type, or it may be not
4233 * yet fully initialized, in which case the f_vnode pointer
4234 * may be set, but f_ops is still badfileops. E.g.,
4235 * devfs_open() transiently create such situation to
4236 * facilitate csw d_fdopen().
4238 * Dupfdopen() handling in kern_openat() installs the
4239 * half-baked file into the process descriptor table, allowing
4240 * other thread to dereference it. Guard against the race by
4243 if (fp->f_vnode == NULL || fp->f_ops == &badfileops) {
4253 * Get an (NFS) file handle.
4255 #ifndef _SYS_SYSPROTO_H_
4256 struct lgetfh_args {
4264 register struct lgetfh_args *uap;
4266 struct nameidata nd;
4268 register struct vnode *vp;
4271 error = priv_check(td, PRIV_VFS_GETFH);
4274 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
4279 NDFREE(&nd, NDF_ONLY_PNBUF);
4281 bzero(&fh, sizeof(fh));
4282 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
4283 error = VOP_VPTOFH(vp, &fh.fh_fid);
4286 error = copyout(&fh, uap->fhp, sizeof (fh));
4290 #ifndef _SYS_SYSPROTO_H_
4299 register struct getfh_args *uap;
4301 struct nameidata nd;
4303 register struct vnode *vp;
4306 error = priv_check(td, PRIV_VFS_GETFH);
4309 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
4314 NDFREE(&nd, NDF_ONLY_PNBUF);
4316 bzero(&fh, sizeof(fh));
4317 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
4318 error = VOP_VPTOFH(vp, &fh.fh_fid);
4321 error = copyout(&fh, uap->fhp, sizeof (fh));
4326 * syscall for the rpc.lockd to use to translate a NFS file handle into an
4329 * warning: do not remove the priv_check() call or this becomes one giant
4332 #ifndef _SYS_SYSPROTO_H_
4333 struct fhopen_args {
4334 const struct fhandle *u_fhp;
4341 struct fhopen_args /* {
4342 const struct fhandle *u_fhp;
4353 error = priv_check(td, PRIV_VFS_FHOPEN);
4357 fmode = FFLAGS(uap->flags);
4358 /* why not allow a non-read/write open for our lockd? */
4359 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
4361 error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
4364 /* find the mount point */
4365 mp = vfs_busyfs(&fhp.fh_fsid);
4368 /* now give me my vnode, it gets returned to me locked */
4369 error = VFS_FHTOVP(mp, &fhp.fh_fid, LK_EXCLUSIVE, &vp);
4374 error = falloc_noinstall(td, &fp);
4380 * An extra reference on `fp' has been held for us by
4381 * falloc_noinstall().
4387 error = vn_open_vnode(vp, fmode, td->td_ucred, td, fp);
4389 KASSERT(fp->f_ops == &badfileops,
4390 ("VOP_OPEN in fhopen() set f_ops"));
4391 KASSERT(td->td_dupfd < 0,
4392 ("fhopen() encountered fdopen()"));
4402 finit(fp, (fmode & FMASK) | (fp->f_flag & FHASLOCK), DTYPE_VNODE, vp,
4405 if ((fmode & O_TRUNC) != 0) {
4406 error = fo_truncate(fp, 0, td->td_ucred, td);
4411 error = finstall(td, fp, &indx, fmode, NULL);
4414 td->td_retval[0] = indx;
4419 * Stat an (NFS) file handle.
4421 #ifndef _SYS_SYSPROTO_H_
4422 struct fhstat_args {
4423 struct fhandle *u_fhp;
4430 register struct fhstat_args /* {
4431 struct fhandle *u_fhp;
4439 error = copyin(uap->u_fhp, &fh, sizeof(fh));
4442 error = kern_fhstat(td, fh, &sb);
4444 error = copyout(&sb, uap->sb, sizeof(sb));
4449 kern_fhstat(struct thread *td, struct fhandle fh, struct stat *sb)
4455 error = priv_check(td, PRIV_VFS_FHSTAT);
4458 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4460 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_EXCLUSIVE, &vp);
4464 error = vn_stat(vp, sb, td->td_ucred, NOCRED, td);
4470 * Implement fstatfs() for (NFS) file handles.
4472 #ifndef _SYS_SYSPROTO_H_
4473 struct fhstatfs_args {
4474 struct fhandle *u_fhp;
4479 sys_fhstatfs(td, uap)
4481 struct fhstatfs_args /* {
4482 struct fhandle *u_fhp;
4490 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4493 error = kern_fhstatfs(td, fh, &sf);
4496 return (copyout(&sf, uap->buf, sizeof(sf)));
4500 kern_fhstatfs(struct thread *td, fhandle_t fh, struct statfs *buf)
4507 error = priv_check(td, PRIV_VFS_FHSTATFS);
4510 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4512 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_EXCLUSIVE, &vp);
4518 error = prison_canseemount(td->td_ucred, mp);
4522 error = mac_mount_check_stat(td->td_ucred, mp);
4527 * Set these in case the underlying filesystem fails to do so.
4530 sp->f_version = STATFS_VERSION;
4531 sp->f_namemax = NAME_MAX;
4532 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
4533 error = VFS_STATFS(mp, sp);
4542 kern_posix_fallocate(struct thread *td, int fd, off_t offset, off_t len)
4547 cap_rights_t rights;
4548 off_t olen, ooffset;
4551 if (offset < 0 || len <= 0)
4553 /* Check for wrap. */
4554 if (offset > OFF_MAX - len)
4556 error = fget(td, fd, cap_rights_init(&rights, CAP_WRITE), &fp);
4559 if ((fp->f_ops->fo_flags & DFLAG_SEEKABLE) == 0) {
4563 if ((fp->f_flag & FWRITE) == 0) {
4567 if (fp->f_type != DTYPE_VNODE) {
4572 if (vp->v_type != VREG) {
4577 /* Allocating blocks may take a long time, so iterate. */
4584 error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
4587 error = vn_lock(vp, LK_EXCLUSIVE);
4589 vn_finished_write(mp);
4593 error = mac_vnode_check_write(td->td_ucred, fp->f_cred, vp);
4596 error = VOP_ALLOCATE(vp, &offset, &len);
4598 vn_finished_write(mp);
4600 if (olen + ooffset != offset + len) {
4601 panic("offset + len changed from %jx/%jx to %jx/%jx",
4602 ooffset, olen, offset, len);
4604 if (error != 0 || len == 0)
4606 KASSERT(olen > len, ("Iteration did not make progress?"));
4615 sys_posix_fallocate(struct thread *td, struct posix_fallocate_args *uap)
4618 td->td_retval[0] = kern_posix_fallocate(td, uap->fd, uap->offset,
4624 * Unlike madvise(2), we do not make a best effort to remember every
4625 * possible caching hint. Instead, we remember the last setting with
4626 * the exception that we will allow POSIX_FADV_NORMAL to adjust the
4627 * region of any current setting.
4630 kern_posix_fadvise(struct thread *td, int fd, off_t offset, off_t len,
4633 struct fadvise_info *fa, *new;
4636 cap_rights_t rights;
4640 if (offset < 0 || len < 0 || offset > OFF_MAX - len)
4643 case POSIX_FADV_SEQUENTIAL:
4644 case POSIX_FADV_RANDOM:
4645 case POSIX_FADV_NOREUSE:
4646 new = malloc(sizeof(*fa), M_FADVISE, M_WAITOK);
4648 case POSIX_FADV_NORMAL:
4649 case POSIX_FADV_WILLNEED:
4650 case POSIX_FADV_DONTNEED:
4656 /* XXX: CAP_POSIX_FADVISE? */
4657 error = fget(td, fd, cap_rights_init(&rights), &fp);
4660 if ((fp->f_ops->fo_flags & DFLAG_SEEKABLE) == 0) {
4664 if (fp->f_type != DTYPE_VNODE) {
4669 if (vp->v_type != VREG) {
4676 end = offset + len - 1;
4678 case POSIX_FADV_SEQUENTIAL:
4679 case POSIX_FADV_RANDOM:
4680 case POSIX_FADV_NOREUSE:
4682 * Try to merge any existing non-standard region with
4683 * this new region if possible, otherwise create a new
4684 * non-standard region for this request.
4686 mtx_pool_lock(mtxpool_sleep, fp);
4688 if (fa != NULL && fa->fa_advice == advice &&
4689 ((fa->fa_start <= end && fa->fa_end >= offset) ||
4690 (end != OFF_MAX && fa->fa_start == end + 1) ||
4691 (fa->fa_end != OFF_MAX && fa->fa_end + 1 == offset))) {
4692 if (offset < fa->fa_start)
4693 fa->fa_start = offset;
4694 if (end > fa->fa_end)
4697 new->fa_advice = advice;
4698 new->fa_start = offset;
4700 new->fa_prevstart = 0;
4701 new->fa_prevend = 0;
4705 mtx_pool_unlock(mtxpool_sleep, fp);
4707 case POSIX_FADV_NORMAL:
4709 * If a the "normal" region overlaps with an existing
4710 * non-standard region, trim or remove the
4711 * non-standard region.
4713 mtx_pool_lock(mtxpool_sleep, fp);
4716 if (offset <= fa->fa_start && end >= fa->fa_end) {
4718 fp->f_advice = NULL;
4719 } else if (offset <= fa->fa_start &&
4720 end >= fa->fa_start)
4721 fa->fa_start = end + 1;
4722 else if (offset <= fa->fa_end && end >= fa->fa_end)
4723 fa->fa_end = offset - 1;
4724 else if (offset >= fa->fa_start && end <= fa->fa_end) {
4726 * If the "normal" region is a middle
4727 * portion of the existing
4728 * non-standard region, just remove
4729 * the whole thing rather than picking
4730 * one side or the other to
4734 fp->f_advice = NULL;
4737 mtx_pool_unlock(mtxpool_sleep, fp);
4739 case POSIX_FADV_WILLNEED:
4740 case POSIX_FADV_DONTNEED:
4741 error = VOP_ADVISE(vp, offset, end, advice);
4747 free(new, M_FADVISE);
4752 sys_posix_fadvise(struct thread *td, struct posix_fadvise_args *uap)
4755 td->td_retval[0] = kern_posix_fadvise(td, uap->fd, uap->offset,
4756 uap->len, uap->advice);