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 kern_chflagsat(struct thread *td, int fd, const char *path,
98 enum uio_seg pathseg, u_long flags, int atflag);
99 static int setfflags(struct thread *td, struct vnode *, u_long);
100 static int getutimes(const struct timeval *, enum uio_seg, struct timespec *);
101 static int getutimens(const struct timespec *, enum uio_seg,
102 struct timespec *, int *);
103 static int setutimes(struct thread *td, struct vnode *,
104 const struct timespec *, int, int);
105 static int vn_access(struct vnode *vp, int user_flags, struct ucred *cred,
109 * Sync each mounted filesystem.
111 #ifndef _SYS_SYSPROTO_H_
118 sys_sync(struct thread *td, struct sync_args *uap)
120 struct mount *mp, *nmp;
123 mtx_lock(&mountlist_mtx);
124 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
125 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK)) {
126 nmp = TAILQ_NEXT(mp, mnt_list);
129 if ((mp->mnt_flag & MNT_RDONLY) == 0 &&
130 vn_start_write(NULL, &mp, V_NOWAIT) == 0) {
131 save = curthread_pflags_set(TDP_SYNCIO);
132 vfs_msync(mp, MNT_NOWAIT);
133 VFS_SYNC(mp, MNT_NOWAIT);
134 curthread_pflags_restore(save);
135 vn_finished_write(mp);
137 mtx_lock(&mountlist_mtx);
138 nmp = TAILQ_NEXT(mp, mnt_list);
141 mtx_unlock(&mountlist_mtx);
146 * Change filesystem quotas.
148 #ifndef _SYS_SYSPROTO_H_
149 struct quotactl_args {
157 sys_quotactl(struct thread *td, struct quotactl_args *uap)
163 AUDIT_ARG_CMD(uap->cmd);
164 AUDIT_ARG_UID(uap->uid);
165 if (!prison_allow(td->td_ucred, PR_ALLOW_QUOTAS))
167 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
169 if ((error = namei(&nd)) != 0)
171 NDFREE(&nd, NDF_ONLY_PNBUF);
172 mp = nd.ni_vp->v_mount;
175 error = vfs_busy(mp, 0);
179 error = VFS_QUOTACTL(mp, uap->cmd, uap->uid, uap->arg);
182 * Since quota on operation typically needs to open quota
183 * file, the Q_QUOTAON handler needs to unbusy the mount point
184 * before calling into namei. Otherwise, unmount might be
185 * started between two vfs_busy() invocations (first is our,
186 * second is from mount point cross-walk code in lookup()),
189 * Require that Q_QUOTAON handles the vfs_busy() reference on
190 * its own, always returning with ubusied mount point.
192 if ((uap->cmd >> SUBCMDSHIFT) != Q_QUOTAON &&
193 (uap->cmd >> SUBCMDSHIFT) != Q_QUOTAOFF)
199 * Used by statfs conversion routines to scale the block size up if
200 * necessary so that all of the block counts are <= 'max_size'. Note
201 * that 'max_size' should be a bitmask, i.e. 2^n - 1 for some non-zero
205 statfs_scale_blocks(struct statfs *sf, long max_size)
210 KASSERT(powerof2(max_size + 1), ("%s: invalid max_size", __func__));
213 * Attempt to scale the block counts to give a more accurate
214 * overview to userland of the ratio of free space to used
215 * space. To do this, find the largest block count and compute
216 * a divisor that lets it fit into a signed integer <= max_size.
218 if (sf->f_bavail < 0)
219 count = -sf->f_bavail;
221 count = sf->f_bavail;
222 count = MAX(sf->f_blocks, MAX(sf->f_bfree, count));
223 if (count <= max_size)
226 count >>= flsl(max_size);
233 sf->f_bsize <<= shift;
234 sf->f_blocks >>= shift;
235 sf->f_bfree >>= shift;
236 sf->f_bavail >>= shift;
240 kern_do_statfs(struct thread *td, struct mount *mp, struct statfs *buf)
247 error = vfs_busy(mp, 0);
252 error = mac_mount_check_stat(td->td_ucred, mp);
257 * Set these in case the underlying filesystem fails to do so.
260 sp->f_version = STATFS_VERSION;
261 sp->f_namemax = NAME_MAX;
262 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
263 error = VFS_STATFS(mp, sp);
267 if (priv_check(td, PRIV_VFS_GENERATION)) {
268 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
269 prison_enforce_statfs(td->td_ucred, mp, buf);
277 * Get filesystem statistics.
279 #ifndef _SYS_SYSPROTO_H_
286 sys_statfs(struct thread *td, struct statfs_args *uap)
291 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
292 error = kern_statfs(td, uap->path, UIO_USERSPACE, sfp);
294 error = copyout(sfp, uap->buf, sizeof(struct statfs));
300 kern_statfs(struct thread *td, char *path, enum uio_seg pathseg,
307 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
312 mp = nd.ni_vp->v_mount;
314 NDFREE(&nd, NDF_ONLY_PNBUF);
316 return (kern_do_statfs(td, mp, buf));
320 * Get filesystem statistics.
322 #ifndef _SYS_SYSPROTO_H_
323 struct fstatfs_args {
329 sys_fstatfs(struct thread *td, struct fstatfs_args *uap)
334 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
335 error = kern_fstatfs(td, uap->fd, sfp);
337 error = copyout(sfp, uap->buf, sizeof(struct statfs));
343 kern_fstatfs(struct thread *td, int fd, struct statfs *buf)
352 error = getvnode(td, fd, cap_rights_init(&rights, CAP_FSTATFS), &fp);
356 vn_lock(vp, LK_SHARED | LK_RETRY);
358 AUDIT_ARG_VNODE1(vp);
365 return (kern_do_statfs(td, mp, buf));
369 * Get statistics on all filesystems.
371 #ifndef _SYS_SYSPROTO_H_
372 struct getfsstat_args {
379 sys_getfsstat(struct thread *td, struct getfsstat_args *uap)
384 if (uap->bufsize < 0 || uap->bufsize > SIZE_MAX)
386 error = kern_getfsstat(td, &uap->buf, uap->bufsize, &count,
387 UIO_USERSPACE, uap->mode);
389 td->td_retval[0] = count;
394 * If (bufsize > 0 && bufseg == UIO_SYSSPACE)
395 * The caller is responsible for freeing memory which will be allocated
399 kern_getfsstat(struct thread *td, struct statfs **buf, size_t bufsize,
400 size_t *countp, enum uio_seg bufseg, int mode)
402 struct mount *mp, *nmp;
403 struct statfs *sfsp, *sp, *sptmp, *tofree;
404 size_t count, maxcount;
415 maxcount = bufsize / sizeof(struct statfs);
419 } else if (bufseg == UIO_USERSPACE) {
422 } else /* if (bufseg == UIO_SYSSPACE) */ {
424 mtx_lock(&mountlist_mtx);
425 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
428 mtx_unlock(&mountlist_mtx);
429 if (maxcount > count)
431 tofree = sfsp = *buf = malloc(maxcount * sizeof(struct statfs),
435 mtx_lock(&mountlist_mtx);
436 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
437 if (prison_canseemount(td->td_ucred, mp) != 0) {
438 nmp = TAILQ_NEXT(mp, mnt_list);
442 if (mac_mount_check_stat(td->td_ucred, mp) != 0) {
443 nmp = TAILQ_NEXT(mp, mnt_list);
447 if (mode == MNT_WAIT) {
448 if (vfs_busy(mp, MBF_MNTLSTLOCK) != 0) {
450 * If vfs_busy() failed, and MBF_NOWAIT
451 * wasn't passed, then the mp is gone.
452 * Furthermore, because of MBF_MNTLSTLOCK,
453 * the mountlist_mtx was dropped. We have
454 * no other choice than to start over.
456 mtx_unlock(&mountlist_mtx);
457 free(tofree, M_STATFS);
461 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK) != 0) {
462 nmp = TAILQ_NEXT(mp, mnt_list);
466 if (sfsp != NULL && count < maxcount) {
469 * Set these in case the underlying filesystem
472 sp->f_version = STATFS_VERSION;
473 sp->f_namemax = NAME_MAX;
474 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
476 * If MNT_NOWAIT is specified, do not refresh
479 if (mode != MNT_NOWAIT) {
480 error = VFS_STATFS(mp, sp);
482 mtx_lock(&mountlist_mtx);
483 nmp = TAILQ_NEXT(mp, mnt_list);
488 if (priv_check(td, PRIV_VFS_GENERATION)) {
489 sptmp = malloc(sizeof(struct statfs), M_STATFS,
492 sptmp->f_fsid.val[0] = sptmp->f_fsid.val[1] = 0;
493 prison_enforce_statfs(td->td_ucred, mp, sptmp);
497 if (bufseg == UIO_SYSSPACE) {
498 bcopy(sp, sfsp, sizeof(*sp));
499 free(sptmp, M_STATFS);
500 } else /* if (bufseg == UIO_USERSPACE) */ {
501 error = copyout(sp, sfsp, sizeof(*sp));
502 free(sptmp, M_STATFS);
511 mtx_lock(&mountlist_mtx);
512 nmp = TAILQ_NEXT(mp, mnt_list);
515 mtx_unlock(&mountlist_mtx);
516 if (sfsp != NULL && count > maxcount)
523 #ifdef COMPAT_FREEBSD4
525 * Get old format filesystem statistics.
527 static void cvtstatfs(struct statfs *, struct ostatfs *);
529 #ifndef _SYS_SYSPROTO_H_
530 struct freebsd4_statfs_args {
536 freebsd4_statfs(struct thread *td, struct freebsd4_statfs_args *uap)
542 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
543 error = kern_statfs(td, uap->path, UIO_USERSPACE, sfp);
545 cvtstatfs(sfp, &osb);
546 error = copyout(&osb, uap->buf, sizeof(osb));
553 * Get filesystem statistics.
555 #ifndef _SYS_SYSPROTO_H_
556 struct freebsd4_fstatfs_args {
562 freebsd4_fstatfs(struct thread *td, struct freebsd4_fstatfs_args *uap)
568 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
569 error = kern_fstatfs(td, uap->fd, sfp);
571 cvtstatfs(sfp, &osb);
572 error = copyout(&osb, uap->buf, sizeof(osb));
579 * Get statistics on all filesystems.
581 #ifndef _SYS_SYSPROTO_H_
582 struct freebsd4_getfsstat_args {
589 freebsd4_getfsstat(struct thread *td, struct freebsd4_getfsstat_args *uap)
591 struct statfs *buf, *sp;
596 if (uap->bufsize < 0)
598 count = uap->bufsize / sizeof(struct ostatfs);
599 if (count > SIZE_MAX / sizeof(struct statfs))
601 size = count * sizeof(struct statfs);
602 error = kern_getfsstat(td, &buf, size, &count, UIO_SYSSPACE,
606 td->td_retval[0] = count;
609 while (count != 0 && error == 0) {
611 error = copyout(&osb, uap->buf, sizeof(osb));
622 * Implement fstatfs() for (NFS) file handles.
624 #ifndef _SYS_SYSPROTO_H_
625 struct freebsd4_fhstatfs_args {
626 struct fhandle *u_fhp;
631 freebsd4_fhstatfs(struct thread *td, struct freebsd4_fhstatfs_args *uap)
638 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
641 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
642 error = kern_fhstatfs(td, fh, sfp);
644 cvtstatfs(sfp, &osb);
645 error = copyout(&osb, uap->buf, sizeof(osb));
652 * Convert a new format statfs structure to an old format statfs structure.
655 cvtstatfs(struct statfs *nsp, struct ostatfs *osp)
658 statfs_scale_blocks(nsp, LONG_MAX);
659 bzero(osp, sizeof(*osp));
660 osp->f_bsize = nsp->f_bsize;
661 osp->f_iosize = MIN(nsp->f_iosize, LONG_MAX);
662 osp->f_blocks = nsp->f_blocks;
663 osp->f_bfree = nsp->f_bfree;
664 osp->f_bavail = nsp->f_bavail;
665 osp->f_files = MIN(nsp->f_files, LONG_MAX);
666 osp->f_ffree = MIN(nsp->f_ffree, LONG_MAX);
667 osp->f_owner = nsp->f_owner;
668 osp->f_type = nsp->f_type;
669 osp->f_flags = nsp->f_flags;
670 osp->f_syncwrites = MIN(nsp->f_syncwrites, LONG_MAX);
671 osp->f_asyncwrites = MIN(nsp->f_asyncwrites, LONG_MAX);
672 osp->f_syncreads = MIN(nsp->f_syncreads, LONG_MAX);
673 osp->f_asyncreads = MIN(nsp->f_asyncreads, LONG_MAX);
674 strlcpy(osp->f_fstypename, nsp->f_fstypename,
675 MIN(MFSNAMELEN, OMFSNAMELEN));
676 strlcpy(osp->f_mntonname, nsp->f_mntonname,
677 MIN(MNAMELEN, OMNAMELEN));
678 strlcpy(osp->f_mntfromname, nsp->f_mntfromname,
679 MIN(MNAMELEN, OMNAMELEN));
680 osp->f_fsid = nsp->f_fsid;
682 #endif /* COMPAT_FREEBSD4 */
685 * Change current working directory to a given file descriptor.
687 #ifndef _SYS_SYSPROTO_H_
693 sys_fchdir(struct thread *td, struct fchdir_args *uap)
695 struct vnode *vp, *tdp;
701 AUDIT_ARG_FD(uap->fd);
702 error = getvnode(td, uap->fd, cap_rights_init(&rights, CAP_FCHDIR),
709 vn_lock(vp, LK_SHARED | LK_RETRY);
710 AUDIT_ARG_VNODE1(vp);
711 error = change_dir(vp, td);
712 while (!error && (mp = vp->v_mountedhere) != NULL) {
715 error = VFS_ROOT(mp, LK_SHARED, &tdp);
732 * Change current working directory (``.'').
734 #ifndef _SYS_SYSPROTO_H_
740 sys_chdir(struct thread *td, struct chdir_args *uap)
743 return (kern_chdir(td, uap->path, UIO_USERSPACE));
747 kern_chdir(struct thread *td, char *path, enum uio_seg pathseg)
752 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
754 if ((error = namei(&nd)) != 0)
756 if ((error = change_dir(nd.ni_vp, td)) != 0) {
758 NDFREE(&nd, NDF_ONLY_PNBUF);
761 VOP_UNLOCK(nd.ni_vp, 0);
762 NDFREE(&nd, NDF_ONLY_PNBUF);
763 pwd_chdir(td, nd.ni_vp);
768 * Change notion of root (``/'') directory.
770 #ifndef _SYS_SYSPROTO_H_
776 sys_chroot(struct thread *td, struct chroot_args *uap)
781 error = priv_check(td, PRIV_VFS_CHROOT);
784 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
785 UIO_USERSPACE, uap->path, td);
789 error = change_dir(nd.ni_vp, td);
793 error = mac_vnode_check_chroot(td->td_ucred, nd.ni_vp);
797 VOP_UNLOCK(nd.ni_vp, 0);
798 error = pwd_chroot(td, nd.ni_vp);
800 NDFREE(&nd, NDF_ONLY_PNBUF);
805 NDFREE(&nd, NDF_ONLY_PNBUF);
810 * Common routine for chroot and chdir. Callers must provide a locked vnode
814 change_dir(struct vnode *vp, struct thread *td)
820 ASSERT_VOP_LOCKED(vp, "change_dir(): vp not locked");
821 if (vp->v_type != VDIR)
824 error = mac_vnode_check_chdir(td->td_ucred, vp);
828 return (VOP_ACCESS(vp, VEXEC, td->td_ucred, td));
832 flags_to_rights(int flags, cap_rights_t *rightsp)
835 if (flags & O_EXEC) {
836 cap_rights_set(rightsp, CAP_FEXECVE);
838 switch ((flags & O_ACCMODE)) {
840 cap_rights_set(rightsp, CAP_READ);
843 cap_rights_set(rightsp, CAP_READ);
846 cap_rights_set(rightsp, CAP_WRITE);
847 if (!(flags & (O_APPEND | O_TRUNC)))
848 cap_rights_set(rightsp, CAP_SEEK);
854 cap_rights_set(rightsp, CAP_CREATE);
857 cap_rights_set(rightsp, CAP_FTRUNCATE);
859 if (flags & (O_SYNC | O_FSYNC))
860 cap_rights_set(rightsp, CAP_FSYNC);
862 if (flags & (O_EXLOCK | O_SHLOCK))
863 cap_rights_set(rightsp, CAP_FLOCK);
867 * Check permissions, allocate an open file structure, and call the device
868 * open routine if any.
870 #ifndef _SYS_SYSPROTO_H_
878 sys_open(struct thread *td, struct open_args *uap)
881 return (kern_openat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
882 uap->flags, uap->mode));
885 #ifndef _SYS_SYSPROTO_H_
894 sys_openat(struct thread *td, struct openat_args *uap)
897 AUDIT_ARG_FD(uap->fd);
898 return (kern_openat(td, uap->fd, uap->path, UIO_USERSPACE, uap->flag,
903 kern_openat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
906 struct proc *p = td->td_proc;
907 struct filedesc *fdp = p->p_fd;
912 int cmode, error, indx;
916 AUDIT_ARG_FFLAGS(flags);
917 AUDIT_ARG_MODE(mode);
918 cap_rights_init(&rights, CAP_LOOKUP);
919 flags_to_rights(flags, &rights);
921 * Only one of the O_EXEC, O_RDONLY, O_WRONLY and O_RDWR flags
924 if (flags & O_EXEC) {
925 if (flags & O_ACCMODE)
927 } else if ((flags & O_ACCMODE) == O_ACCMODE) {
930 flags = FFLAGS(flags);
934 * Allocate a file structure. The descriptor to reference it
935 * is allocated and set by finstall() below.
937 error = falloc_noinstall(td, &fp);
941 * An extra reference on `fp' has been held for us by
942 * falloc_noinstall().
944 /* Set the flags early so the finit in devfs can pick them up. */
945 fp->f_flag = flags & FMASK;
946 cmode = ((mode & ~fdp->fd_cmask) & ALLPERMS) & ~S_ISTXT;
947 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, fd,
949 td->td_dupfd = -1; /* XXX check for fdopen */
950 error = vn_open(&nd, &flags, cmode, fp);
953 * If the vn_open replaced the method vector, something
954 * wonderous happened deep below and we just pass it up
955 * pretending we know what we do.
957 if (error == ENXIO && fp->f_ops != &badfileops)
961 * Handle special fdopen() case. bleh.
963 * Don't do this for relative (capability) lookups; we don't
964 * understand exactly what would happen, and we don't think
965 * that it ever should.
967 if ((nd.ni_lcf & NI_LCF_STRICTRELATIVE) == 0 &&
968 (error == ENODEV || error == ENXIO) &&
970 error = dupfdopen(td, fdp, td->td_dupfd, flags, error,
979 NDFREE(&nd, NDF_ONLY_PNBUF);
983 * Store the vnode, for any f_type. Typically, the vnode use
984 * count is decremented by direct call to vn_closefile() for
985 * files that switched type in the cdevsw fdopen() method.
989 * If the file wasn't claimed by devfs bind it to the normal
990 * vnode operations here.
992 if (fp->f_ops == &badfileops) {
993 KASSERT(vp->v_type != VFIFO, ("Unexpected fifo."));
995 finit(fp, (flags & FMASK) | (fp->f_flag & FHASLOCK),
996 DTYPE_VNODE, vp, &vnops);
1000 if (flags & O_TRUNC) {
1001 error = fo_truncate(fp, 0, td->td_ucred, td);
1007 * If we haven't already installed the FD (for dupfdopen), do so now.
1010 struct filecaps *fcaps;
1013 if ((nd.ni_lcf & NI_LCF_STRICTRELATIVE) != 0)
1014 fcaps = &nd.ni_filecaps;
1018 error = finstall(td, fp, &indx, flags, fcaps);
1019 /* On success finstall() consumes fcaps. */
1021 filecaps_free(&nd.ni_filecaps);
1025 filecaps_free(&nd.ni_filecaps);
1029 * Release our private reference, leaving the one associated with
1030 * the descriptor table intact.
1033 td->td_retval[0] = indx;
1036 KASSERT(indx == -1, ("indx=%d, should be -1", indx));
1045 #ifndef _SYS_SYSPROTO_H_
1046 struct ocreat_args {
1052 ocreat(struct thread *td, struct ocreat_args *uap)
1055 return (kern_openat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1056 O_WRONLY | O_CREAT | O_TRUNC, uap->mode));
1058 #endif /* COMPAT_43 */
1061 * Create a special file.
1063 #ifndef _SYS_SYSPROTO_H_
1071 sys_mknod(struct thread *td, struct mknod_args *uap)
1074 return (kern_mknodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1075 uap->mode, uap->dev));
1078 #ifndef _SYS_SYSPROTO_H_
1079 struct mknodat_args {
1087 sys_mknodat(struct thread *td, struct mknodat_args *uap)
1090 return (kern_mknodat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode,
1095 kern_mknodat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1101 struct nameidata nd;
1102 cap_rights_t rights;
1103 int error, whiteout = 0;
1105 AUDIT_ARG_MODE(mode);
1107 switch (mode & S_IFMT) {
1110 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
1111 if (error == 0 && dev == VNOVAL)
1115 error = priv_check(td, PRIV_VFS_MKNOD_WHT);
1119 return (kern_mkfifoat(td, fd, path, pathseg, mode));
1129 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
1130 NOCACHE, pathseg, path, fd, cap_rights_init(&rights, CAP_MKNODAT),
1132 if ((error = namei(&nd)) != 0)
1136 NDFREE(&nd, NDF_ONLY_PNBUF);
1137 if (vp == nd.ni_dvp)
1145 vattr.va_mode = (mode & ALLPERMS) &
1146 ~td->td_proc->p_fd->fd_cmask;
1147 vattr.va_rdev = dev;
1150 switch (mode & S_IFMT) {
1152 vattr.va_type = VCHR;
1155 vattr.va_type = VBLK;
1161 panic("kern_mknod: invalid mode");
1164 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1165 NDFREE(&nd, NDF_ONLY_PNBUF);
1167 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1172 if (error == 0 && !whiteout)
1173 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp,
1174 &nd.ni_cnd, &vattr);
1178 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, CREATE);
1180 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp,
1181 &nd.ni_cnd, &vattr);
1186 NDFREE(&nd, NDF_ONLY_PNBUF);
1188 vn_finished_write(mp);
1193 * Create a named pipe.
1195 #ifndef _SYS_SYSPROTO_H_
1196 struct mkfifo_args {
1202 sys_mkfifo(struct thread *td, struct mkfifo_args *uap)
1205 return (kern_mkfifoat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1209 #ifndef _SYS_SYSPROTO_H_
1210 struct mkfifoat_args {
1217 sys_mkfifoat(struct thread *td, struct mkfifoat_args *uap)
1220 return (kern_mkfifoat(td, uap->fd, uap->path, UIO_USERSPACE,
1225 kern_mkfifoat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1230 struct nameidata nd;
1231 cap_rights_t rights;
1234 AUDIT_ARG_MODE(mode);
1237 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
1238 NOCACHE, pathseg, path, fd, cap_rights_init(&rights, CAP_MKFIFOAT),
1240 if ((error = namei(&nd)) != 0)
1242 if (nd.ni_vp != NULL) {
1243 NDFREE(&nd, NDF_ONLY_PNBUF);
1244 if (nd.ni_vp == nd.ni_dvp)
1251 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1252 NDFREE(&nd, NDF_ONLY_PNBUF);
1254 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1259 vattr.va_type = VFIFO;
1260 vattr.va_mode = (mode & ALLPERMS) & ~td->td_proc->p_fd->fd_cmask;
1262 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1267 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
1274 vn_finished_write(mp);
1275 NDFREE(&nd, NDF_ONLY_PNBUF);
1280 * Make a hard file link.
1282 #ifndef _SYS_SYSPROTO_H_
1289 sys_link(struct thread *td, struct link_args *uap)
1292 return (kern_linkat(td, AT_FDCWD, AT_FDCWD, uap->path, uap->link,
1293 UIO_USERSPACE, FOLLOW));
1296 #ifndef _SYS_SYSPROTO_H_
1297 struct linkat_args {
1306 sys_linkat(struct thread *td, struct linkat_args *uap)
1311 if (flag & ~AT_SYMLINK_FOLLOW)
1314 return (kern_linkat(td, uap->fd1, uap->fd2, uap->path1, uap->path2,
1315 UIO_USERSPACE, (flag & AT_SYMLINK_FOLLOW) ? FOLLOW : NOFOLLOW));
1318 int hardlink_check_uid = 0;
1319 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
1320 &hardlink_check_uid, 0,
1321 "Unprivileged processes cannot create hard links to files owned by other "
1323 static int hardlink_check_gid = 0;
1324 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
1325 &hardlink_check_gid, 0,
1326 "Unprivileged processes cannot create hard links to files owned by other "
1330 can_hardlink(struct vnode *vp, struct ucred *cred)
1335 if (!hardlink_check_uid && !hardlink_check_gid)
1338 error = VOP_GETATTR(vp, &va, cred);
1342 if (hardlink_check_uid && cred->cr_uid != va.va_uid) {
1343 error = priv_check_cred(cred, PRIV_VFS_LINK, 0);
1348 if (hardlink_check_gid && !groupmember(va.va_gid, cred)) {
1349 error = priv_check_cred(cred, PRIV_VFS_LINK, 0);
1358 kern_linkat(struct thread *td, int fd1, int fd2, char *path1, char *path2,
1359 enum uio_seg segflg, int follow)
1363 struct nameidata nd;
1364 cap_rights_t rights;
1369 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, segflg, path1, fd1,
1370 cap_rights_init(&rights, CAP_LINKAT_SOURCE), td);
1372 if ((error = namei(&nd)) != 0)
1374 NDFREE(&nd, NDF_ONLY_PNBUF);
1376 if (vp->v_type == VDIR) {
1378 return (EPERM); /* POSIX */
1380 NDINIT_ATRIGHTS(&nd, CREATE,
1381 LOCKPARENT | SAVENAME | AUDITVNODE2 | NOCACHE, segflg, path2, fd2,
1382 cap_rights_init(&rights, CAP_LINKAT_TARGET), td);
1383 if ((error = namei(&nd)) == 0) {
1384 if (nd.ni_vp != NULL) {
1385 NDFREE(&nd, NDF_ONLY_PNBUF);
1386 if (nd.ni_dvp == nd.ni_vp)
1393 } else if (nd.ni_dvp->v_mount != vp->v_mount) {
1395 * Cross-device link. No need to recheck
1396 * vp->v_type, since it cannot change, except
1399 NDFREE(&nd, NDF_ONLY_PNBUF);
1403 } else if ((error = vn_lock(vp, LK_EXCLUSIVE)) == 0) {
1404 error = can_hardlink(vp, td->td_ucred);
1407 error = mac_vnode_check_link(td->td_ucred,
1408 nd.ni_dvp, vp, &nd.ni_cnd);
1413 NDFREE(&nd, NDF_ONLY_PNBUF);
1416 error = vn_start_write(vp, &mp, V_NOWAIT);
1420 NDFREE(&nd, NDF_ONLY_PNBUF);
1421 error = vn_start_write(NULL, &mp,
1427 error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
1430 vn_finished_write(mp);
1431 NDFREE(&nd, NDF_ONLY_PNBUF);
1434 NDFREE(&nd, NDF_ONLY_PNBUF);
1444 * Make a symbolic link.
1446 #ifndef _SYS_SYSPROTO_H_
1447 struct symlink_args {
1453 sys_symlink(struct thread *td, struct symlink_args *uap)
1456 return (kern_symlinkat(td, uap->path, AT_FDCWD, uap->link,
1460 #ifndef _SYS_SYSPROTO_H_
1461 struct symlinkat_args {
1468 sys_symlinkat(struct thread *td, struct symlinkat_args *uap)
1471 return (kern_symlinkat(td, uap->path1, uap->fd, uap->path2,
1476 kern_symlinkat(struct thread *td, char *path1, int fd, char *path2,
1477 enum uio_seg segflg)
1482 struct nameidata nd;
1484 cap_rights_t rights;
1486 if (segflg == UIO_SYSSPACE) {
1489 syspath = uma_zalloc(namei_zone, M_WAITOK);
1490 if ((error = copyinstr(path1, syspath, MAXPATHLEN, NULL)) != 0)
1493 AUDIT_ARG_TEXT(syspath);
1496 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
1497 NOCACHE, segflg, path2, fd, cap_rights_init(&rights, CAP_SYMLINKAT),
1499 if ((error = namei(&nd)) != 0)
1502 NDFREE(&nd, NDF_ONLY_PNBUF);
1503 if (nd.ni_vp == nd.ni_dvp)
1511 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1512 NDFREE(&nd, NDF_ONLY_PNBUF);
1514 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1519 vattr.va_mode = ACCESSPERMS &~ td->td_proc->p_fd->fd_cmask;
1521 vattr.va_type = VLNK;
1522 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1527 error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr, syspath);
1533 NDFREE(&nd, NDF_ONLY_PNBUF);
1535 vn_finished_write(mp);
1537 if (segflg != UIO_SYSSPACE)
1538 uma_zfree(namei_zone, syspath);
1543 * Delete a whiteout from the filesystem.
1545 #ifndef _SYS_SYSPROTO_H_
1546 struct undelete_args {
1551 sys_undelete(struct thread *td, struct undelete_args *uap)
1554 struct nameidata nd;
1559 NDINIT(&nd, DELETE, LOCKPARENT | DOWHITEOUT | AUDITVNODE1,
1560 UIO_USERSPACE, uap->path, td);
1565 if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & ISWHITEOUT)) {
1566 NDFREE(&nd, NDF_ONLY_PNBUF);
1567 if (nd.ni_vp == nd.ni_dvp)
1575 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1576 NDFREE(&nd, NDF_ONLY_PNBUF);
1578 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1582 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, DELETE);
1583 NDFREE(&nd, NDF_ONLY_PNBUF);
1585 vn_finished_write(mp);
1590 * Delete a name from the filesystem.
1592 #ifndef _SYS_SYSPROTO_H_
1593 struct unlink_args {
1598 sys_unlink(struct thread *td, struct unlink_args *uap)
1601 return (kern_unlinkat(td, AT_FDCWD, uap->path, UIO_USERSPACE, 0));
1604 #ifndef _SYS_SYSPROTO_H_
1605 struct unlinkat_args {
1612 sys_unlinkat(struct thread *td, struct unlinkat_args *uap)
1614 int flag = uap->flag;
1616 char *path = uap->path;
1618 if (flag & ~AT_REMOVEDIR)
1621 if (flag & AT_REMOVEDIR)
1622 return (kern_rmdirat(td, fd, path, UIO_USERSPACE));
1624 return (kern_unlinkat(td, fd, path, UIO_USERSPACE, 0));
1628 kern_unlinkat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1633 struct nameidata nd;
1635 cap_rights_t rights;
1640 NDINIT_ATRIGHTS(&nd, DELETE, LOCKPARENT | LOCKLEAF | AUDITVNODE1,
1641 pathseg, path, fd, cap_rights_init(&rights, CAP_UNLINKAT), td);
1642 if ((error = namei(&nd)) != 0)
1643 return (error == EINVAL ? EPERM : error);
1645 if (vp->v_type == VDIR && oldinum == 0) {
1646 error = EPERM; /* POSIX */
1647 } else if (oldinum != 0 &&
1648 ((error = vn_stat(vp, &sb, td->td_ucred, NOCRED, td)) == 0) &&
1649 sb.st_ino != oldinum) {
1650 error = EIDRM; /* Identifier removed */
1653 * The root of a mounted filesystem cannot be deleted.
1655 * XXX: can this only be a VDIR case?
1657 if (vp->v_vflag & VV_ROOT)
1661 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1662 NDFREE(&nd, NDF_ONLY_PNBUF);
1664 if (vp == nd.ni_dvp)
1668 if ((error = vn_start_write(NULL, &mp,
1669 V_XSLEEP | PCATCH)) != 0)
1674 error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp,
1679 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_UNLINK);
1680 error = VOP_REMOVE(nd.ni_dvp, vp, &nd.ni_cnd);
1684 vn_finished_write(mp);
1686 NDFREE(&nd, NDF_ONLY_PNBUF);
1688 if (vp == nd.ni_dvp)
1696 * Reposition read/write file offset.
1698 #ifndef _SYS_SYSPROTO_H_
1707 sys_lseek(struct thread *td, struct lseek_args *uap)
1710 return (kern_lseek(td, uap->fd, uap->offset, uap->whence));
1714 kern_lseek(struct thread *td, int fd, off_t offset, int whence)
1717 cap_rights_t rights;
1721 error = fget(td, fd, cap_rights_init(&rights, CAP_SEEK), &fp);
1724 error = (fp->f_ops->fo_flags & DFLAG_SEEKABLE) != 0 ?
1725 fo_seek(fp, offset, whence, td) : ESPIPE;
1730 #if defined(COMPAT_43)
1732 * Reposition read/write file offset.
1734 #ifndef _SYS_SYSPROTO_H_
1735 struct olseek_args {
1742 olseek(struct thread *td, struct olseek_args *uap)
1745 return (kern_lseek(td, uap->fd, uap->offset, uap->whence));
1747 #endif /* COMPAT_43 */
1749 #if defined(COMPAT_FREEBSD6)
1750 /* Version with the 'pad' argument */
1752 freebsd6_lseek(struct thread *td, struct freebsd6_lseek_args *uap)
1755 return (kern_lseek(td, uap->fd, uap->offset, uap->whence));
1760 * Check access permissions using passed credentials.
1763 vn_access(struct vnode *vp, int user_flags, struct ucred *cred,
1769 /* Flags == 0 means only check for existence. */
1770 if (user_flags == 0)
1774 if (user_flags & R_OK)
1776 if (user_flags & W_OK)
1778 if (user_flags & X_OK)
1781 error = mac_vnode_check_access(cred, vp, accmode);
1785 if ((accmode & VWRITE) == 0 || (error = vn_writechk(vp)) == 0)
1786 error = VOP_ACCESS(vp, accmode, cred, td);
1791 * Check access permissions using "real" credentials.
1793 #ifndef _SYS_SYSPROTO_H_
1794 struct access_args {
1800 sys_access(struct thread *td, struct access_args *uap)
1803 return (kern_accessat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1807 #ifndef _SYS_SYSPROTO_H_
1808 struct faccessat_args {
1816 sys_faccessat(struct thread *td, struct faccessat_args *uap)
1819 return (kern_accessat(td, uap->fd, uap->path, UIO_USERSPACE, uap->flag,
1824 kern_accessat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1825 int flag, int amode)
1827 struct ucred *cred, *usecred;
1829 struct nameidata nd;
1830 cap_rights_t rights;
1833 if (flag & ~AT_EACCESS)
1835 if (amode != F_OK && (amode & ~(R_OK | W_OK | X_OK)) != 0)
1839 * Create and modify a temporary credential instead of one that
1840 * is potentially shared (if we need one).
1842 cred = td->td_ucred;
1843 if ((flag & AT_EACCESS) == 0 &&
1844 ((cred->cr_uid != cred->cr_ruid ||
1845 cred->cr_rgid != cred->cr_groups[0]))) {
1846 usecred = crdup(cred);
1847 usecred->cr_uid = cred->cr_ruid;
1848 usecred->cr_groups[0] = cred->cr_rgid;
1849 td->td_ucred = usecred;
1852 AUDIT_ARG_VALUE(amode);
1853 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF |
1854 AUDITVNODE1, pathseg, path, fd, cap_rights_init(&rights, CAP_FSTAT),
1856 if ((error = namei(&nd)) != 0)
1860 error = vn_access(vp, amode, usecred, td);
1861 NDFREE(&nd, NDF_ONLY_PNBUF);
1864 if (usecred != cred) {
1865 td->td_ucred = cred;
1872 * Check access permissions using "effective" credentials.
1874 #ifndef _SYS_SYSPROTO_H_
1875 struct eaccess_args {
1881 sys_eaccess(struct thread *td, struct eaccess_args *uap)
1884 return (kern_accessat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1885 AT_EACCESS, uap->amode));
1888 #if defined(COMPAT_43)
1890 * Get file status; this version follows links.
1892 #ifndef _SYS_SYSPROTO_H_
1899 ostat(struct thread *td, struct ostat_args *uap)
1905 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
1910 return (copyout(&osb, uap->ub, sizeof (osb)));
1914 * Get file status; this version does not follow links.
1916 #ifndef _SYS_SYSPROTO_H_
1917 struct olstat_args {
1923 olstat(struct thread *td, struct olstat_args *uap)
1929 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
1930 UIO_USERSPACE, &sb, NULL);
1934 return (copyout(&osb, uap->ub, sizeof (osb)));
1938 * Convert from an old to a new stat structure.
1941 cvtstat(struct stat *st, struct ostat *ost)
1944 bzero(ost, sizeof(*ost));
1945 ost->st_dev = st->st_dev;
1946 ost->st_ino = st->st_ino;
1947 ost->st_mode = st->st_mode;
1948 ost->st_nlink = st->st_nlink;
1949 ost->st_uid = st->st_uid;
1950 ost->st_gid = st->st_gid;
1951 ost->st_rdev = st->st_rdev;
1952 if (st->st_size < (quad_t)1 << 32)
1953 ost->st_size = st->st_size;
1956 ost->st_atim = st->st_atim;
1957 ost->st_mtim = st->st_mtim;
1958 ost->st_ctim = st->st_ctim;
1959 ost->st_blksize = st->st_blksize;
1960 ost->st_blocks = st->st_blocks;
1961 ost->st_flags = st->st_flags;
1962 ost->st_gen = st->st_gen;
1964 #endif /* COMPAT_43 */
1967 * Get file status; this version follows links.
1969 #ifndef _SYS_SYSPROTO_H_
1976 sys_stat(struct thread *td, struct stat_args *uap)
1981 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
1984 error = copyout(&sb, uap->ub, sizeof (sb));
1988 #ifndef _SYS_SYSPROTO_H_
1989 struct fstatat_args {
1997 sys_fstatat(struct thread *td, struct fstatat_args *uap)
2002 error = kern_statat(td, uap->flag, uap->fd, uap->path,
2003 UIO_USERSPACE, &sb, NULL);
2005 error = copyout(&sb, uap->buf, sizeof (sb));
2010 kern_statat(struct thread *td, int flag, int fd, char *path,
2011 enum uio_seg pathseg, struct stat *sbp,
2012 void (*hook)(struct vnode *vp, struct stat *sbp))
2014 struct nameidata nd;
2016 cap_rights_t rights;
2019 if (flag & ~AT_SYMLINK_NOFOLLOW)
2022 NDINIT_ATRIGHTS(&nd, LOOKUP, ((flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW :
2023 FOLLOW) | LOCKSHARED | LOCKLEAF | AUDITVNODE1, pathseg, path, fd,
2024 cap_rights_init(&rights, CAP_FSTAT), td);
2026 if ((error = namei(&nd)) != 0)
2028 error = vn_stat(nd.ni_vp, &sb, td->td_ucred, NOCRED, td);
2030 SDT_PROBE2(vfs, , stat, mode, path, sb.st_mode);
2031 if (S_ISREG(sb.st_mode))
2032 SDT_PROBE2(vfs, , stat, reg, path, pathseg);
2033 if (__predict_false(hook != NULL))
2034 hook(nd.ni_vp, &sb);
2036 NDFREE(&nd, NDF_ONLY_PNBUF);
2042 if (KTRPOINT(td, KTR_STRUCT))
2049 * Get file status; this version does not follow links.
2051 #ifndef _SYS_SYSPROTO_H_
2058 sys_lstat(struct thread *td, struct lstat_args *uap)
2063 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2064 UIO_USERSPACE, &sb, NULL);
2066 error = copyout(&sb, uap->ub, sizeof (sb));
2071 * Implementation of the NetBSD [l]stat() functions.
2074 cvtnstat( struct stat *sb, struct nstat *nsb)
2077 bzero(nsb, sizeof *nsb);
2078 nsb->st_dev = sb->st_dev;
2079 nsb->st_ino = sb->st_ino;
2080 nsb->st_mode = sb->st_mode;
2081 nsb->st_nlink = sb->st_nlink;
2082 nsb->st_uid = sb->st_uid;
2083 nsb->st_gid = sb->st_gid;
2084 nsb->st_rdev = sb->st_rdev;
2085 nsb->st_atim = sb->st_atim;
2086 nsb->st_mtim = sb->st_mtim;
2087 nsb->st_ctim = sb->st_ctim;
2088 nsb->st_size = sb->st_size;
2089 nsb->st_blocks = sb->st_blocks;
2090 nsb->st_blksize = sb->st_blksize;
2091 nsb->st_flags = sb->st_flags;
2092 nsb->st_gen = sb->st_gen;
2093 nsb->st_birthtim = sb->st_birthtim;
2096 #ifndef _SYS_SYSPROTO_H_
2103 sys_nstat(struct thread *td, struct nstat_args *uap)
2109 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
2113 cvtnstat(&sb, &nsb);
2114 return (copyout(&nsb, uap->ub, sizeof (nsb)));
2118 * NetBSD lstat. Get file status; this version does not follow links.
2120 #ifndef _SYS_SYSPROTO_H_
2127 sys_nlstat(struct thread *td, struct nlstat_args *uap)
2133 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2134 UIO_USERSPACE, &sb, NULL);
2137 cvtnstat(&sb, &nsb);
2138 return (copyout(&nsb, uap->ub, sizeof (nsb)));
2142 * Get configurable pathname variables.
2144 #ifndef _SYS_SYSPROTO_H_
2145 struct pathconf_args {
2151 sys_pathconf(struct thread *td, struct pathconf_args *uap)
2154 return (kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name, FOLLOW));
2157 #ifndef _SYS_SYSPROTO_H_
2158 struct lpathconf_args {
2164 sys_lpathconf(struct thread *td, struct lpathconf_args *uap)
2167 return (kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name,
2172 kern_pathconf(struct thread *td, char *path, enum uio_seg pathseg, int name,
2175 struct nameidata nd;
2178 NDINIT(&nd, LOOKUP, LOCKSHARED | LOCKLEAF | AUDITVNODE1 | flags,
2180 if ((error = namei(&nd)) != 0)
2182 NDFREE(&nd, NDF_ONLY_PNBUF);
2184 error = VOP_PATHCONF(nd.ni_vp, name, td->td_retval);
2190 * Return target name of a symbolic link.
2192 #ifndef _SYS_SYSPROTO_H_
2193 struct readlink_args {
2200 sys_readlink(struct thread *td, struct readlink_args *uap)
2203 return (kern_readlinkat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2204 uap->buf, UIO_USERSPACE, uap->count));
2206 #ifndef _SYS_SYSPROTO_H_
2207 struct readlinkat_args {
2215 sys_readlinkat(struct thread *td, struct readlinkat_args *uap)
2218 return (kern_readlinkat(td, uap->fd, uap->path, UIO_USERSPACE,
2219 uap->buf, UIO_USERSPACE, uap->bufsize));
2223 kern_readlinkat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2224 char *buf, enum uio_seg bufseg, size_t count)
2229 struct nameidata nd;
2232 if (count > IOSIZE_MAX)
2235 NDINIT_AT(&nd, LOOKUP, NOFOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
2236 pathseg, path, fd, td);
2238 if ((error = namei(&nd)) != 0)
2240 NDFREE(&nd, NDF_ONLY_PNBUF);
2243 error = mac_vnode_check_readlink(td->td_ucred, vp);
2249 if (vp->v_type != VLNK && (vp->v_vflag & VV_READLINK) == 0)
2252 aiov.iov_base = buf;
2253 aiov.iov_len = count;
2254 auio.uio_iov = &aiov;
2255 auio.uio_iovcnt = 1;
2256 auio.uio_offset = 0;
2257 auio.uio_rw = UIO_READ;
2258 auio.uio_segflg = bufseg;
2260 auio.uio_resid = count;
2261 error = VOP_READLINK(vp, &auio, td->td_ucred);
2262 td->td_retval[0] = count - auio.uio_resid;
2269 * Common implementation code for chflags() and fchflags().
2272 setfflags(struct thread *td, struct vnode *vp, u_long flags)
2278 /* We can't support the value matching VNOVAL. */
2279 if (flags == VNOVAL)
2280 return (EOPNOTSUPP);
2283 * Prevent non-root users from setting flags on devices. When
2284 * a device is reused, users can retain ownership of the device
2285 * if they are allowed to set flags and programs assume that
2286 * chown can't fail when done as root.
2288 if (vp->v_type == VCHR || vp->v_type == VBLK) {
2289 error = priv_check(td, PRIV_VFS_CHFLAGS_DEV);
2294 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2297 vattr.va_flags = flags;
2298 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2300 error = mac_vnode_check_setflags(td->td_ucred, vp, vattr.va_flags);
2303 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
2305 vn_finished_write(mp);
2310 * Change flags of a file given a path name.
2312 #ifndef _SYS_SYSPROTO_H_
2313 struct chflags_args {
2319 sys_chflags(struct thread *td, struct chflags_args *uap)
2322 return (kern_chflagsat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2326 #ifndef _SYS_SYSPROTO_H_
2327 struct chflagsat_args {
2335 sys_chflagsat(struct thread *td, struct chflagsat_args *uap)
2338 const char *path = uap->path;
2339 u_long flags = uap->flags;
2340 int atflag = uap->atflag;
2342 if (atflag & ~AT_SYMLINK_NOFOLLOW)
2345 return (kern_chflagsat(td, fd, path, UIO_USERSPACE, flags, atflag));
2349 * Same as chflags() but doesn't follow symlinks.
2351 #ifndef _SYS_SYSPROTO_H_
2352 struct lchflags_args {
2358 sys_lchflags(struct thread *td, struct lchflags_args *uap)
2361 return (kern_chflagsat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2362 uap->flags, AT_SYMLINK_NOFOLLOW));
2366 kern_chflagsat(struct thread *td, int fd, const char *path,
2367 enum uio_seg pathseg, u_long flags, int atflag)
2369 struct nameidata nd;
2370 cap_rights_t rights;
2373 AUDIT_ARG_FFLAGS(flags);
2374 follow = (atflag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
2375 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, pathseg, path, fd,
2376 cap_rights_init(&rights, CAP_FCHFLAGS), td);
2377 if ((error = namei(&nd)) != 0)
2379 NDFREE(&nd, NDF_ONLY_PNBUF);
2380 error = setfflags(td, nd.ni_vp, flags);
2386 * Change flags of a file given a file descriptor.
2388 #ifndef _SYS_SYSPROTO_H_
2389 struct fchflags_args {
2395 sys_fchflags(struct thread *td, struct fchflags_args *uap)
2398 cap_rights_t rights;
2401 AUDIT_ARG_FD(uap->fd);
2402 AUDIT_ARG_FFLAGS(uap->flags);
2403 error = getvnode(td, uap->fd, cap_rights_init(&rights, CAP_FCHFLAGS),
2408 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
2409 AUDIT_ARG_VNODE1(fp->f_vnode);
2410 VOP_UNLOCK(fp->f_vnode, 0);
2412 error = setfflags(td, fp->f_vnode, uap->flags);
2418 * Common implementation code for chmod(), lchmod() and fchmod().
2421 setfmode(struct thread *td, struct ucred *cred, struct vnode *vp, int mode)
2427 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2429 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2431 vattr.va_mode = mode & ALLPERMS;
2433 error = mac_vnode_check_setmode(cred, vp, vattr.va_mode);
2436 error = VOP_SETATTR(vp, &vattr, cred);
2438 vn_finished_write(mp);
2443 * Change mode of a file given path name.
2445 #ifndef _SYS_SYSPROTO_H_
2452 sys_chmod(struct thread *td, struct chmod_args *uap)
2455 return (kern_fchmodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2459 #ifndef _SYS_SYSPROTO_H_
2460 struct fchmodat_args {
2468 sys_fchmodat(struct thread *td, struct fchmodat_args *uap)
2470 int flag = uap->flag;
2472 char *path = uap->path;
2473 mode_t mode = uap->mode;
2475 if (flag & ~AT_SYMLINK_NOFOLLOW)
2478 return (kern_fchmodat(td, fd, path, UIO_USERSPACE, mode, flag));
2482 * Change mode of a file given path name (don't follow links.)
2484 #ifndef _SYS_SYSPROTO_H_
2485 struct lchmod_args {
2491 sys_lchmod(struct thread *td, struct lchmod_args *uap)
2494 return (kern_fchmodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2495 uap->mode, AT_SYMLINK_NOFOLLOW));
2499 kern_fchmodat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2500 mode_t mode, int flag)
2502 struct nameidata nd;
2503 cap_rights_t rights;
2506 AUDIT_ARG_MODE(mode);
2507 follow = (flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
2508 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, pathseg, path, fd,
2509 cap_rights_init(&rights, CAP_FCHMOD), td);
2510 if ((error = namei(&nd)) != 0)
2512 NDFREE(&nd, NDF_ONLY_PNBUF);
2513 error = setfmode(td, td->td_ucred, nd.ni_vp, mode);
2519 * Change mode of a file given a file descriptor.
2521 #ifndef _SYS_SYSPROTO_H_
2522 struct fchmod_args {
2528 sys_fchmod(struct thread *td, struct fchmod_args *uap)
2531 cap_rights_t rights;
2534 AUDIT_ARG_FD(uap->fd);
2535 AUDIT_ARG_MODE(uap->mode);
2537 error = fget(td, uap->fd, cap_rights_init(&rights, CAP_FCHMOD), &fp);
2540 error = fo_chmod(fp, uap->mode, td->td_ucred, td);
2546 * Common implementation for chown(), lchown(), and fchown()
2549 setfown(struct thread *td, struct ucred *cred, struct vnode *vp, uid_t uid,
2556 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2558 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2563 error = mac_vnode_check_setowner(cred, vp, vattr.va_uid,
2567 error = VOP_SETATTR(vp, &vattr, cred);
2569 vn_finished_write(mp);
2574 * Set ownership given a path name.
2576 #ifndef _SYS_SYSPROTO_H_
2584 sys_chown(struct thread *td, struct chown_args *uap)
2587 return (kern_fchownat(td, AT_FDCWD, uap->path, UIO_USERSPACE, uap->uid,
2591 #ifndef _SYS_SYSPROTO_H_
2592 struct fchownat_args {
2601 sys_fchownat(struct thread *td, struct fchownat_args *uap)
2606 if (flag & ~AT_SYMLINK_NOFOLLOW)
2609 return (kern_fchownat(td, uap->fd, uap->path, UIO_USERSPACE, uap->uid,
2610 uap->gid, uap->flag));
2614 kern_fchownat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2615 int uid, int gid, int flag)
2617 struct nameidata nd;
2618 cap_rights_t rights;
2621 AUDIT_ARG_OWNER(uid, gid);
2622 follow = (flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
2623 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, pathseg, path, fd,
2624 cap_rights_init(&rights, CAP_FCHOWN), td);
2626 if ((error = namei(&nd)) != 0)
2628 NDFREE(&nd, NDF_ONLY_PNBUF);
2629 error = setfown(td, td->td_ucred, nd.ni_vp, uid, gid);
2635 * Set ownership given a path name, do not cross symlinks.
2637 #ifndef _SYS_SYSPROTO_H_
2638 struct lchown_args {
2645 sys_lchown(struct thread *td, struct lchown_args *uap)
2648 return (kern_fchownat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2649 uap->uid, uap->gid, AT_SYMLINK_NOFOLLOW));
2653 * Set ownership given a file descriptor.
2655 #ifndef _SYS_SYSPROTO_H_
2656 struct fchown_args {
2663 sys_fchown(struct thread *td, struct fchown_args *uap)
2666 cap_rights_t rights;
2669 AUDIT_ARG_FD(uap->fd);
2670 AUDIT_ARG_OWNER(uap->uid, uap->gid);
2671 error = fget(td, uap->fd, cap_rights_init(&rights, CAP_FCHOWN), &fp);
2674 error = fo_chown(fp, uap->uid, uap->gid, td->td_ucred, td);
2680 * Common implementation code for utimes(), lutimes(), and futimes().
2683 getutimes(const struct timeval *usrtvp, enum uio_seg tvpseg,
2684 struct timespec *tsp)
2686 struct timeval tv[2];
2687 const struct timeval *tvp;
2690 if (usrtvp == NULL) {
2691 vfs_timestamp(&tsp[0]);
2694 if (tvpseg == UIO_SYSSPACE) {
2697 if ((error = copyin(usrtvp, tv, sizeof(tv))) != 0)
2702 if (tvp[0].tv_usec < 0 || tvp[0].tv_usec >= 1000000 ||
2703 tvp[1].tv_usec < 0 || tvp[1].tv_usec >= 1000000)
2705 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
2706 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
2712 * Common implementation code for futimens(), utimensat().
2714 #define UTIMENS_NULL 0x1
2715 #define UTIMENS_EXIT 0x2
2717 getutimens(const struct timespec *usrtsp, enum uio_seg tspseg,
2718 struct timespec *tsp, int *retflags)
2720 struct timespec tsnow;
2723 vfs_timestamp(&tsnow);
2725 if (usrtsp == NULL) {
2728 *retflags |= UTIMENS_NULL;
2731 if (tspseg == UIO_SYSSPACE) {
2734 } else if ((error = copyin(usrtsp, tsp, sizeof(*tsp) * 2)) != 0)
2736 if (tsp[0].tv_nsec == UTIME_OMIT && tsp[1].tv_nsec == UTIME_OMIT)
2737 *retflags |= UTIMENS_EXIT;
2738 if (tsp[0].tv_nsec == UTIME_NOW && tsp[1].tv_nsec == UTIME_NOW)
2739 *retflags |= UTIMENS_NULL;
2740 if (tsp[0].tv_nsec == UTIME_OMIT)
2741 tsp[0].tv_sec = VNOVAL;
2742 else if (tsp[0].tv_nsec == UTIME_NOW)
2744 else if (tsp[0].tv_nsec < 0 || tsp[0].tv_nsec >= 1000000000L)
2746 if (tsp[1].tv_nsec == UTIME_OMIT)
2747 tsp[1].tv_sec = VNOVAL;
2748 else if (tsp[1].tv_nsec == UTIME_NOW)
2750 else if (tsp[1].tv_nsec < 0 || tsp[1].tv_nsec >= 1000000000L)
2757 * Common implementation code for utimes(), lutimes(), futimes(), futimens(),
2761 setutimes(struct thread *td, struct vnode *vp, const struct timespec *ts,
2762 int numtimes, int nullflag)
2766 int error, setbirthtime;
2768 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2770 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2772 if (numtimes < 3 && !VOP_GETATTR(vp, &vattr, td->td_ucred) &&
2773 timespeccmp(&ts[1], &vattr.va_birthtime, < ))
2776 vattr.va_atime = ts[0];
2777 vattr.va_mtime = ts[1];
2779 vattr.va_birthtime = ts[1];
2781 vattr.va_birthtime = ts[2];
2783 vattr.va_vaflags |= VA_UTIMES_NULL;
2785 error = mac_vnode_check_setutimes(td->td_ucred, vp, vattr.va_atime,
2789 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
2791 vn_finished_write(mp);
2796 * Set the access and modification times of a file.
2798 #ifndef _SYS_SYSPROTO_H_
2799 struct utimes_args {
2801 struct timeval *tptr;
2805 sys_utimes(struct thread *td, struct utimes_args *uap)
2808 return (kern_utimesat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2809 uap->tptr, UIO_USERSPACE));
2812 #ifndef _SYS_SYSPROTO_H_
2813 struct futimesat_args {
2816 const struct timeval * times;
2820 sys_futimesat(struct thread *td, struct futimesat_args *uap)
2823 return (kern_utimesat(td, uap->fd, uap->path, UIO_USERSPACE,
2824 uap->times, UIO_USERSPACE));
2828 kern_utimesat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2829 struct timeval *tptr, enum uio_seg tptrseg)
2831 struct nameidata nd;
2832 struct timespec ts[2];
2833 cap_rights_t rights;
2836 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
2838 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, fd,
2839 cap_rights_init(&rights, CAP_FUTIMES), td);
2841 if ((error = namei(&nd)) != 0)
2843 NDFREE(&nd, NDF_ONLY_PNBUF);
2844 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
2850 * Set the access and modification times of a file.
2852 #ifndef _SYS_SYSPROTO_H_
2853 struct lutimes_args {
2855 struct timeval *tptr;
2859 sys_lutimes(struct thread *td, struct lutimes_args *uap)
2862 return (kern_lutimes(td, uap->path, UIO_USERSPACE, uap->tptr,
2867 kern_lutimes(struct thread *td, char *path, enum uio_seg pathseg,
2868 struct timeval *tptr, enum uio_seg tptrseg)
2870 struct timespec ts[2];
2871 struct nameidata nd;
2874 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
2876 NDINIT(&nd, LOOKUP, NOFOLLOW | AUDITVNODE1, pathseg, path, td);
2877 if ((error = namei(&nd)) != 0)
2879 NDFREE(&nd, NDF_ONLY_PNBUF);
2880 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
2886 * Set the access and modification times of a file.
2888 #ifndef _SYS_SYSPROTO_H_
2889 struct futimes_args {
2891 struct timeval *tptr;
2895 sys_futimes(struct thread *td, struct futimes_args *uap)
2898 return (kern_futimes(td, uap->fd, uap->tptr, UIO_USERSPACE));
2902 kern_futimes(struct thread *td, int fd, struct timeval *tptr,
2903 enum uio_seg tptrseg)
2905 struct timespec ts[2];
2907 cap_rights_t rights;
2911 error = getutimes(tptr, tptrseg, ts);
2914 error = getvnode(td, fd, cap_rights_init(&rights, CAP_FUTIMES), &fp);
2918 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
2919 AUDIT_ARG_VNODE1(fp->f_vnode);
2920 VOP_UNLOCK(fp->f_vnode, 0);
2922 error = setutimes(td, fp->f_vnode, ts, 2, tptr == NULL);
2928 sys_futimens(struct thread *td, struct futimens_args *uap)
2931 return (kern_futimens(td, uap->fd, uap->times, UIO_USERSPACE));
2935 kern_futimens(struct thread *td, int fd, struct timespec *tptr,
2936 enum uio_seg tptrseg)
2938 struct timespec ts[2];
2940 cap_rights_t rights;
2944 error = getutimens(tptr, tptrseg, ts, &flags);
2947 if (flags & UTIMENS_EXIT)
2949 error = getvnode(td, fd, cap_rights_init(&rights, CAP_FUTIMES), &fp);
2953 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
2954 AUDIT_ARG_VNODE1(fp->f_vnode);
2955 VOP_UNLOCK(fp->f_vnode, 0);
2957 error = setutimes(td, fp->f_vnode, ts, 2, flags & UTIMENS_NULL);
2963 sys_utimensat(struct thread *td, struct utimensat_args *uap)
2966 return (kern_utimensat(td, uap->fd, uap->path, UIO_USERSPACE,
2967 uap->times, UIO_USERSPACE, uap->flag));
2971 kern_utimensat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2972 struct timespec *tptr, enum uio_seg tptrseg, int flag)
2974 struct nameidata nd;
2975 struct timespec ts[2];
2976 cap_rights_t rights;
2979 if (flag & ~AT_SYMLINK_NOFOLLOW)
2982 if ((error = getutimens(tptr, tptrseg, ts, &flags)) != 0)
2984 NDINIT_ATRIGHTS(&nd, LOOKUP, ((flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW :
2985 FOLLOW) | AUDITVNODE1, pathseg, path, fd,
2986 cap_rights_init(&rights, CAP_FUTIMES), td);
2987 if ((error = namei(&nd)) != 0)
2990 * We are allowed to call namei() regardless of 2xUTIME_OMIT.
2992 * "If both tv_nsec fields are UTIME_OMIT... EACCESS may be detected."
2993 * "Search permission is denied by a component of the path prefix."
2995 NDFREE(&nd, NDF_ONLY_PNBUF);
2996 if ((flags & UTIMENS_EXIT) == 0)
2997 error = setutimes(td, nd.ni_vp, ts, 2, flags & UTIMENS_NULL);
3003 * Truncate a file given its path name.
3005 #ifndef _SYS_SYSPROTO_H_
3006 struct truncate_args {
3013 sys_truncate(struct thread *td, struct truncate_args *uap)
3016 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3020 kern_truncate(struct thread *td, char *path, enum uio_seg pathseg, off_t length)
3026 struct nameidata nd;
3031 NDINIT(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, td);
3032 if ((error = namei(&nd)) != 0)
3035 rl_cookie = vn_rangelock_wlock(vp, 0, OFF_MAX);
3036 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0) {
3037 vn_rangelock_unlock(vp, rl_cookie);
3041 NDFREE(&nd, NDF_ONLY_PNBUF);
3042 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3043 if (vp->v_type == VDIR)
3046 else if ((error = mac_vnode_check_write(td->td_ucred, NOCRED, vp))) {
3049 else if ((error = vn_writechk(vp)) == 0 &&
3050 (error = VOP_ACCESS(vp, VWRITE, td->td_ucred, td)) == 0) {
3052 vattr.va_size = length;
3053 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3056 vn_finished_write(mp);
3057 vn_rangelock_unlock(vp, rl_cookie);
3062 #if defined(COMPAT_43)
3064 * Truncate a file given its path name.
3066 #ifndef _SYS_SYSPROTO_H_
3067 struct otruncate_args {
3073 otruncate(struct thread *td, struct otruncate_args *uap)
3076 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3078 #endif /* COMPAT_43 */
3080 #if defined(COMPAT_FREEBSD6)
3081 /* Versions with the pad argument */
3083 freebsd6_truncate(struct thread *td, struct freebsd6_truncate_args *uap)
3086 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3090 freebsd6_ftruncate(struct thread *td, struct freebsd6_ftruncate_args *uap)
3093 return (kern_ftruncate(td, uap->fd, uap->length));
3098 kern_fsync(struct thread *td, int fd, bool fullsync)
3103 cap_rights_t rights;
3104 int error, lock_flags;
3107 error = getvnode(td, fd, cap_rights_init(&rights, CAP_FSYNC), &fp);
3113 /* XXXKIB: compete outstanding aio writes */;
3115 error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
3118 if (MNT_SHARED_WRITES(mp) ||
3119 ((mp == NULL) && MNT_SHARED_WRITES(vp->v_mount))) {
3120 lock_flags = LK_SHARED;
3122 lock_flags = LK_EXCLUSIVE;
3124 vn_lock(vp, lock_flags | LK_RETRY);
3125 AUDIT_ARG_VNODE1(vp);
3126 if (vp->v_object != NULL) {
3127 VM_OBJECT_WLOCK(vp->v_object);
3128 vm_object_page_clean(vp->v_object, 0, 0, 0);
3129 VM_OBJECT_WUNLOCK(vp->v_object);
3131 error = fullsync ? VOP_FSYNC(vp, MNT_WAIT, td) : VOP_FDATASYNC(vp, td);
3133 vn_finished_write(mp);
3140 * Sync an open file.
3142 #ifndef _SYS_SYSPROTO_H_
3148 sys_fsync(struct thread *td, struct fsync_args *uap)
3151 return (kern_fsync(td, uap->fd, true));
3155 sys_fdatasync(struct thread *td, struct fdatasync_args *uap)
3158 return (kern_fsync(td, uap->fd, false));
3162 * Rename files. Source and destination must either both be directories, or
3163 * both not be directories. If target is a directory, it must be empty.
3165 #ifndef _SYS_SYSPROTO_H_
3166 struct rename_args {
3172 sys_rename(struct thread *td, struct rename_args *uap)
3175 return (kern_renameat(td, AT_FDCWD, uap->from, AT_FDCWD,
3176 uap->to, UIO_USERSPACE));
3179 #ifndef _SYS_SYSPROTO_H_
3180 struct renameat_args {
3188 sys_renameat(struct thread *td, struct renameat_args *uap)
3191 return (kern_renameat(td, uap->oldfd, uap->old, uap->newfd, uap->new,
3196 kern_renameat(struct thread *td, int oldfd, char *old, int newfd, char *new,
3197 enum uio_seg pathseg)
3199 struct mount *mp = NULL;
3200 struct vnode *tvp, *fvp, *tdvp;
3201 struct nameidata fromnd, tond;
3202 cap_rights_t rights;
3208 NDINIT_ATRIGHTS(&fromnd, DELETE, LOCKPARENT | LOCKLEAF | SAVESTART |
3209 AUDITVNODE1, pathseg, old, oldfd,
3210 cap_rights_init(&rights, CAP_RENAMEAT_SOURCE), td);
3212 NDINIT_ATRIGHTS(&fromnd, DELETE, WANTPARENT | SAVESTART | AUDITVNODE1,
3213 pathseg, old, oldfd,
3214 cap_rights_init(&rights, CAP_RENAMEAT_SOURCE), td);
3217 if ((error = namei(&fromnd)) != 0)
3220 error = mac_vnode_check_rename_from(td->td_ucred, fromnd.ni_dvp,
3221 fromnd.ni_vp, &fromnd.ni_cnd);
3222 VOP_UNLOCK(fromnd.ni_dvp, 0);
3223 if (fromnd.ni_dvp != fromnd.ni_vp)
3224 VOP_UNLOCK(fromnd.ni_vp, 0);
3227 NDINIT_ATRIGHTS(&tond, RENAME, LOCKPARENT | LOCKLEAF | NOCACHE |
3228 SAVESTART | AUDITVNODE2, pathseg, new, newfd,
3229 cap_rights_init(&rights, CAP_RENAMEAT_TARGET), td);
3230 if (fromnd.ni_vp->v_type == VDIR)
3231 tond.ni_cnd.cn_flags |= WILLBEDIR;
3232 if ((error = namei(&tond)) != 0) {
3233 /* Translate error code for rename("dir1", "dir2/."). */
3234 if (error == EISDIR && fvp->v_type == VDIR)
3236 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3237 vrele(fromnd.ni_dvp);
3243 error = vn_start_write(fvp, &mp, V_NOWAIT);
3245 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3246 NDFREE(&tond, NDF_ONLY_PNBUF);
3253 vrele(fromnd.ni_dvp);
3255 vrele(tond.ni_startdir);
3256 if (fromnd.ni_startdir != NULL)
3257 vrele(fromnd.ni_startdir);
3258 error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH);
3264 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
3267 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
3272 if (newfd != AT_FDCWD) {
3274 * If the target already exists we require CAP_UNLINKAT
3277 error = cap_check(&tond.ni_filecaps.fc_rights,
3278 cap_rights_init(&rights, CAP_UNLINKAT));
3289 * If the source is the same as the destination (that is, if they
3290 * are links to the same vnode), then there is nothing to do.
3296 error = mac_vnode_check_rename_to(td->td_ucred, tdvp,
3297 tond.ni_vp, fromnd.ni_dvp == tdvp, &tond.ni_cnd);
3301 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
3302 tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
3303 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3304 NDFREE(&tond, NDF_ONLY_PNBUF);
3306 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3307 NDFREE(&tond, NDF_ONLY_PNBUF);
3314 vrele(fromnd.ni_dvp);
3317 vrele(tond.ni_startdir);
3318 vn_finished_write(mp);
3320 if (fromnd.ni_startdir)
3321 vrele(fromnd.ni_startdir);
3328 * Make a directory file.
3330 #ifndef _SYS_SYSPROTO_H_
3337 sys_mkdir(struct thread *td, struct mkdir_args *uap)
3340 return (kern_mkdirat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
3344 #ifndef _SYS_SYSPROTO_H_
3345 struct mkdirat_args {
3352 sys_mkdirat(struct thread *td, struct mkdirat_args *uap)
3355 return (kern_mkdirat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode));
3359 kern_mkdirat(struct thread *td, int fd, char *path, enum uio_seg segflg,
3365 struct nameidata nd;
3366 cap_rights_t rights;
3369 AUDIT_ARG_MODE(mode);
3372 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
3373 NOCACHE, segflg, path, fd, cap_rights_init(&rights, CAP_MKDIRAT),
3375 nd.ni_cnd.cn_flags |= WILLBEDIR;
3376 if ((error = namei(&nd)) != 0)
3380 NDFREE(&nd, NDF_ONLY_PNBUF);
3382 * XXX namei called with LOCKPARENT but not LOCKLEAF has
3383 * the strange behaviour of leaving the vnode unlocked
3384 * if the target is the same vnode as the parent.
3386 if (vp == nd.ni_dvp)
3393 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3394 NDFREE(&nd, NDF_ONLY_PNBUF);
3396 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3401 vattr.va_type = VDIR;
3402 vattr.va_mode = (mode & ACCESSPERMS) &~ td->td_proc->p_fd->fd_cmask;
3404 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
3409 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
3413 NDFREE(&nd, NDF_ONLY_PNBUF);
3417 vn_finished_write(mp);
3422 * Remove a directory file.
3424 #ifndef _SYS_SYSPROTO_H_
3430 sys_rmdir(struct thread *td, struct rmdir_args *uap)
3433 return (kern_rmdirat(td, AT_FDCWD, uap->path, UIO_USERSPACE));
3437 kern_rmdirat(struct thread *td, int fd, char *path, enum uio_seg pathseg)
3441 struct nameidata nd;
3442 cap_rights_t rights;
3447 NDINIT_ATRIGHTS(&nd, DELETE, LOCKPARENT | LOCKLEAF | AUDITVNODE1,
3448 pathseg, path, fd, cap_rights_init(&rights, CAP_UNLINKAT), td);
3449 if ((error = namei(&nd)) != 0)
3452 if (vp->v_type != VDIR) {
3457 * No rmdir "." please.
3459 if (nd.ni_dvp == vp) {
3464 * The root of a mounted filesystem cannot be deleted.
3466 if (vp->v_vflag & VV_ROOT) {
3471 error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp,
3476 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3477 NDFREE(&nd, NDF_ONLY_PNBUF);
3479 if (nd.ni_dvp == vp)
3483 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3487 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_UNLINK);
3488 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
3489 vn_finished_write(mp);
3491 NDFREE(&nd, NDF_ONLY_PNBUF);
3493 if (nd.ni_dvp == vp)
3502 * Read a block of directory entries in a filesystem independent format.
3504 #ifndef _SYS_SYSPROTO_H_
3505 struct ogetdirentries_args {
3513 ogetdirentries(struct thread *td, struct ogetdirentries_args *uap)
3518 error = kern_ogetdirentries(td, uap, &loff);
3520 error = copyout(&loff, uap->basep, sizeof(long));
3525 kern_ogetdirentries(struct thread *td, struct ogetdirentries_args *uap,
3530 struct uio auio, kuio;
3531 struct iovec aiov, kiov;
3532 struct dirent *dp, *edp;
3533 cap_rights_t rights;
3535 int error, eofflag, readcnt;
3539 /* XXX arbitrary sanity limit on `count'. */
3540 if (uap->count > 64 * 1024)
3542 error = getvnode(td, uap->fd, cap_rights_init(&rights, CAP_READ), &fp);
3545 if ((fp->f_flag & FREAD) == 0) {
3550 foffset = foffset_lock(fp, 0);
3552 if (vp->v_type != VDIR) {
3553 foffset_unlock(fp, foffset, 0);
3557 aiov.iov_base = uap->buf;
3558 aiov.iov_len = uap->count;
3559 auio.uio_iov = &aiov;
3560 auio.uio_iovcnt = 1;
3561 auio.uio_rw = UIO_READ;
3562 auio.uio_segflg = UIO_USERSPACE;
3564 auio.uio_resid = uap->count;
3565 vn_lock(vp, LK_SHARED | LK_RETRY);
3566 loff = auio.uio_offset = foffset;
3568 error = mac_vnode_check_readdir(td->td_ucred, vp);
3571 foffset_unlock(fp, foffset, FOF_NOUPDATE);
3576 # if (BYTE_ORDER != LITTLE_ENDIAN)
3577 if (vp->v_mount->mnt_maxsymlinklen <= 0) {
3578 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag,
3580 foffset = auio.uio_offset;
3585 kuio.uio_iov = &kiov;
3586 kuio.uio_segflg = UIO_SYSSPACE;
3587 kiov.iov_len = uap->count;
3588 dirbuf = malloc(uap->count, M_TEMP, M_WAITOK);
3589 kiov.iov_base = dirbuf;
3590 error = VOP_READDIR(vp, &kuio, fp->f_cred, &eofflag,
3592 foffset = kuio.uio_offset;
3594 readcnt = uap->count - kuio.uio_resid;
3595 edp = (struct dirent *)&dirbuf[readcnt];
3596 for (dp = (struct dirent *)dirbuf; dp < edp; ) {
3597 # if (BYTE_ORDER == LITTLE_ENDIAN)
3599 * The expected low byte of
3600 * dp->d_namlen is our dp->d_type.
3601 * The high MBZ byte of dp->d_namlen
3602 * is our dp->d_namlen.
3604 dp->d_type = dp->d_namlen;
3608 * The dp->d_type is the high byte
3609 * of the expected dp->d_namlen,
3610 * so must be zero'ed.
3614 if (dp->d_reclen > 0) {
3615 dp = (struct dirent *)
3616 ((char *)dp + dp->d_reclen);
3623 error = uiomove(dirbuf, readcnt, &auio);
3625 free(dirbuf, M_TEMP);
3629 foffset_unlock(fp, foffset, 0);
3633 if (uap->count == auio.uio_resid &&
3634 (vp->v_vflag & VV_ROOT) &&
3635 (vp->v_mount->mnt_flag & MNT_UNION)) {
3636 struct vnode *tvp = vp;
3637 vp = vp->v_mount->mnt_vnodecovered;
3646 foffset_unlock(fp, foffset, 0);
3648 td->td_retval[0] = uap->count - auio.uio_resid;
3653 #endif /* COMPAT_43 */
3656 * Read a block of directory entries in a filesystem independent format.
3658 #ifndef _SYS_SYSPROTO_H_
3659 struct getdirentries_args {
3667 sys_getdirentries(struct thread *td, struct getdirentries_args *uap)
3672 error = kern_getdirentries(td, uap->fd, uap->buf, uap->count, &base,
3673 NULL, UIO_USERSPACE);
3676 if (uap->basep != NULL)
3677 error = copyout(&base, uap->basep, sizeof(long));
3682 kern_getdirentries(struct thread *td, int fd, char *buf, u_int count,
3683 long *basep, ssize_t *residp, enum uio_seg bufseg)
3689 cap_rights_t rights;
3695 if (count > IOSIZE_MAX)
3697 auio.uio_resid = count;
3698 error = getvnode(td, fd, cap_rights_init(&rights, CAP_READ), &fp);
3701 if ((fp->f_flag & FREAD) == 0) {
3706 foffset = foffset_lock(fp, 0);
3708 if (vp->v_type != VDIR) {
3712 aiov.iov_base = buf;
3713 aiov.iov_len = count;
3714 auio.uio_iov = &aiov;
3715 auio.uio_iovcnt = 1;
3716 auio.uio_rw = UIO_READ;
3717 auio.uio_segflg = bufseg;
3719 vn_lock(vp, LK_SHARED | LK_RETRY);
3720 AUDIT_ARG_VNODE1(vp);
3721 loff = auio.uio_offset = foffset;
3723 error = mac_vnode_check_readdir(td->td_ucred, vp);
3726 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL,
3728 foffset = auio.uio_offset;
3733 if (count == auio.uio_resid &&
3734 (vp->v_vflag & VV_ROOT) &&
3735 (vp->v_mount->mnt_flag & MNT_UNION)) {
3736 struct vnode *tvp = vp;
3738 vp = vp->v_mount->mnt_vnodecovered;
3749 *residp = auio.uio_resid;
3750 td->td_retval[0] = count - auio.uio_resid;
3752 foffset_unlock(fp, foffset, 0);
3757 #ifndef _SYS_SYSPROTO_H_
3758 struct getdents_args {
3765 sys_getdents(struct thread *td, struct getdents_args *uap)
3767 struct getdirentries_args ap;
3771 ap.count = uap->count;
3773 return (sys_getdirentries(td, &ap));
3777 * Set the mode mask for creation of filesystem nodes.
3779 #ifndef _SYS_SYSPROTO_H_
3785 sys_umask(struct thread *td, struct umask_args *uap)
3787 struct filedesc *fdp;
3789 fdp = td->td_proc->p_fd;
3790 FILEDESC_XLOCK(fdp);
3791 td->td_retval[0] = fdp->fd_cmask;
3792 fdp->fd_cmask = uap->newmask & ALLPERMS;
3793 FILEDESC_XUNLOCK(fdp);
3798 * Void all references to file by ripping underlying filesystem away from
3801 #ifndef _SYS_SYSPROTO_H_
3802 struct revoke_args {
3807 sys_revoke(struct thread *td, struct revoke_args *uap)
3811 struct nameidata nd;
3814 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
3816 if ((error = namei(&nd)) != 0)
3819 NDFREE(&nd, NDF_ONLY_PNBUF);
3820 if (vp->v_type != VCHR || vp->v_rdev == NULL) {
3825 error = mac_vnode_check_revoke(td->td_ucred, vp);
3829 error = VOP_GETATTR(vp, &vattr, td->td_ucred);
3832 if (td->td_ucred->cr_uid != vattr.va_uid) {
3833 error = priv_check(td, PRIV_VFS_ADMIN);
3838 VOP_REVOKE(vp, REVOKEALL);
3845 * Convert a user file descriptor to a kernel file entry and check that, if it
3846 * is a capability, the correct rights are present. A reference on the file
3847 * entry is held upon returning.
3850 getvnode(struct thread *td, int fd, cap_rights_t *rightsp, struct file **fpp)
3855 error = fget_unlocked(td->td_proc->p_fd, fd, rightsp, &fp, NULL);
3860 * The file could be not of the vnode type, or it may be not
3861 * yet fully initialized, in which case the f_vnode pointer
3862 * may be set, but f_ops is still badfileops. E.g.,
3863 * devfs_open() transiently create such situation to
3864 * facilitate csw d_fdopen().
3866 * Dupfdopen() handling in kern_openat() installs the
3867 * half-baked file into the process descriptor table, allowing
3868 * other thread to dereference it. Guard against the race by
3871 if (fp->f_vnode == NULL || fp->f_ops == &badfileops) {
3881 * Get an (NFS) file handle.
3883 #ifndef _SYS_SYSPROTO_H_
3884 struct lgetfh_args {
3890 sys_lgetfh(struct thread *td, struct lgetfh_args *uap)
3892 struct nameidata nd;
3897 error = priv_check(td, PRIV_VFS_GETFH);
3900 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
3905 NDFREE(&nd, NDF_ONLY_PNBUF);
3907 bzero(&fh, sizeof(fh));
3908 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
3909 error = VOP_VPTOFH(vp, &fh.fh_fid);
3912 error = copyout(&fh, uap->fhp, sizeof (fh));
3916 #ifndef _SYS_SYSPROTO_H_
3923 sys_getfh(struct thread *td, struct getfh_args *uap)
3925 struct nameidata nd;
3930 error = priv_check(td, PRIV_VFS_GETFH);
3933 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
3938 NDFREE(&nd, NDF_ONLY_PNBUF);
3940 bzero(&fh, sizeof(fh));
3941 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
3942 error = VOP_VPTOFH(vp, &fh.fh_fid);
3945 error = copyout(&fh, uap->fhp, sizeof (fh));
3950 * syscall for the rpc.lockd to use to translate a NFS file handle into an
3953 * warning: do not remove the priv_check() call or this becomes one giant
3956 #ifndef _SYS_SYSPROTO_H_
3957 struct fhopen_args {
3958 const struct fhandle *u_fhp;
3963 sys_fhopen(struct thread *td, struct fhopen_args *uap)
3972 error = priv_check(td, PRIV_VFS_FHOPEN);
3976 fmode = FFLAGS(uap->flags);
3977 /* why not allow a non-read/write open for our lockd? */
3978 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
3980 error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
3983 /* find the mount point */
3984 mp = vfs_busyfs(&fhp.fh_fsid);
3987 /* now give me my vnode, it gets returned to me locked */
3988 error = VFS_FHTOVP(mp, &fhp.fh_fid, LK_EXCLUSIVE, &vp);
3993 error = falloc_noinstall(td, &fp);
3999 * An extra reference on `fp' has been held for us by
4000 * falloc_noinstall().
4006 error = vn_open_vnode(vp, fmode, td->td_ucred, td, fp);
4008 KASSERT(fp->f_ops == &badfileops,
4009 ("VOP_OPEN in fhopen() set f_ops"));
4010 KASSERT(td->td_dupfd < 0,
4011 ("fhopen() encountered fdopen()"));
4021 finit(fp, (fmode & FMASK) | (fp->f_flag & FHASLOCK), DTYPE_VNODE, vp,
4024 if ((fmode & O_TRUNC) != 0) {
4025 error = fo_truncate(fp, 0, td->td_ucred, td);
4030 error = finstall(td, fp, &indx, fmode, NULL);
4033 td->td_retval[0] = indx;
4038 * Stat an (NFS) file handle.
4040 #ifndef _SYS_SYSPROTO_H_
4041 struct fhstat_args {
4042 struct fhandle *u_fhp;
4047 sys_fhstat(struct thread *td, struct fhstat_args *uap)
4053 error = copyin(uap->u_fhp, &fh, sizeof(fh));
4056 error = kern_fhstat(td, fh, &sb);
4058 error = copyout(&sb, uap->sb, sizeof(sb));
4063 kern_fhstat(struct thread *td, struct fhandle fh, struct stat *sb)
4069 error = priv_check(td, PRIV_VFS_FHSTAT);
4072 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4074 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_EXCLUSIVE, &vp);
4078 error = vn_stat(vp, sb, td->td_ucred, NOCRED, td);
4084 * Implement fstatfs() for (NFS) file handles.
4086 #ifndef _SYS_SYSPROTO_H_
4087 struct fhstatfs_args {
4088 struct fhandle *u_fhp;
4093 sys_fhstatfs(struct thread *td, struct fhstatfs_args *uap)
4099 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4102 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
4103 error = kern_fhstatfs(td, fh, sfp);
4105 error = copyout(sfp, uap->buf, sizeof(*sfp));
4106 free(sfp, M_STATFS);
4111 kern_fhstatfs(struct thread *td, fhandle_t fh, struct statfs *buf)
4118 error = priv_check(td, PRIV_VFS_FHSTATFS);
4121 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4123 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_EXCLUSIVE, &vp);
4129 error = prison_canseemount(td->td_ucred, mp);
4133 error = mac_mount_check_stat(td->td_ucred, mp);
4138 * Set these in case the underlying filesystem fails to do so.
4141 sp->f_version = STATFS_VERSION;
4142 sp->f_namemax = NAME_MAX;
4143 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
4144 error = VFS_STATFS(mp, sp);
4153 kern_posix_fallocate(struct thread *td, int fd, off_t offset, off_t len)
4158 cap_rights_t rights;
4159 off_t olen, ooffset;
4162 if (offset < 0 || len <= 0)
4164 /* Check for wrap. */
4165 if (offset > OFF_MAX - len)
4167 error = fget(td, fd, cap_rights_init(&rights, CAP_PWRITE), &fp);
4170 if ((fp->f_ops->fo_flags & DFLAG_SEEKABLE) == 0) {
4174 if ((fp->f_flag & FWRITE) == 0) {
4178 if (fp->f_type != DTYPE_VNODE) {
4183 if (vp->v_type != VREG) {
4188 /* Allocating blocks may take a long time, so iterate. */
4195 error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
4198 error = vn_lock(vp, LK_EXCLUSIVE);
4200 vn_finished_write(mp);
4204 error = mac_vnode_check_write(td->td_ucred, fp->f_cred, vp);
4207 error = VOP_ALLOCATE(vp, &offset, &len);
4209 vn_finished_write(mp);
4211 if (olen + ooffset != offset + len) {
4212 panic("offset + len changed from %jx/%jx to %jx/%jx",
4213 ooffset, olen, offset, len);
4215 if (error != 0 || len == 0)
4217 KASSERT(olen > len, ("Iteration did not make progress?"));
4226 sys_posix_fallocate(struct thread *td, struct posix_fallocate_args *uap)
4230 error = kern_posix_fallocate(td, uap->fd, uap->offset, uap->len);
4231 return (kern_posix_error(td, error));
4235 * Unlike madvise(2), we do not make a best effort to remember every
4236 * possible caching hint. Instead, we remember the last setting with
4237 * the exception that we will allow POSIX_FADV_NORMAL to adjust the
4238 * region of any current setting.
4241 kern_posix_fadvise(struct thread *td, int fd, off_t offset, off_t len,
4244 struct fadvise_info *fa, *new;
4247 cap_rights_t rights;
4251 if (offset < 0 || len < 0 || offset > OFF_MAX - len)
4254 case POSIX_FADV_SEQUENTIAL:
4255 case POSIX_FADV_RANDOM:
4256 case POSIX_FADV_NOREUSE:
4257 new = malloc(sizeof(*fa), M_FADVISE, M_WAITOK);
4259 case POSIX_FADV_NORMAL:
4260 case POSIX_FADV_WILLNEED:
4261 case POSIX_FADV_DONTNEED:
4267 /* XXX: CAP_POSIX_FADVISE? */
4268 error = fget(td, fd, cap_rights_init(&rights), &fp);
4271 if ((fp->f_ops->fo_flags & DFLAG_SEEKABLE) == 0) {
4275 if (fp->f_type != DTYPE_VNODE) {
4280 if (vp->v_type != VREG) {
4287 end = offset + len - 1;
4289 case POSIX_FADV_SEQUENTIAL:
4290 case POSIX_FADV_RANDOM:
4291 case POSIX_FADV_NOREUSE:
4293 * Try to merge any existing non-standard region with
4294 * this new region if possible, otherwise create a new
4295 * non-standard region for this request.
4297 mtx_pool_lock(mtxpool_sleep, fp);
4299 if (fa != NULL && fa->fa_advice == advice &&
4300 ((fa->fa_start <= end && fa->fa_end >= offset) ||
4301 (end != OFF_MAX && fa->fa_start == end + 1) ||
4302 (fa->fa_end != OFF_MAX && fa->fa_end + 1 == offset))) {
4303 if (offset < fa->fa_start)
4304 fa->fa_start = offset;
4305 if (end > fa->fa_end)
4308 new->fa_advice = advice;
4309 new->fa_start = offset;
4314 mtx_pool_unlock(mtxpool_sleep, fp);
4316 case POSIX_FADV_NORMAL:
4318 * If a the "normal" region overlaps with an existing
4319 * non-standard region, trim or remove the
4320 * non-standard region.
4322 mtx_pool_lock(mtxpool_sleep, fp);
4325 if (offset <= fa->fa_start && end >= fa->fa_end) {
4327 fp->f_advice = NULL;
4328 } else if (offset <= fa->fa_start &&
4329 end >= fa->fa_start)
4330 fa->fa_start = end + 1;
4331 else if (offset <= fa->fa_end && end >= fa->fa_end)
4332 fa->fa_end = offset - 1;
4333 else if (offset >= fa->fa_start && end <= fa->fa_end) {
4335 * If the "normal" region is a middle
4336 * portion of the existing
4337 * non-standard region, just remove
4338 * the whole thing rather than picking
4339 * one side or the other to
4343 fp->f_advice = NULL;
4346 mtx_pool_unlock(mtxpool_sleep, fp);
4348 case POSIX_FADV_WILLNEED:
4349 case POSIX_FADV_DONTNEED:
4350 error = VOP_ADVISE(vp, offset, end, advice);
4356 free(new, M_FADVISE);
4361 sys_posix_fadvise(struct thread *td, struct posix_fadvise_args *uap)
4365 error = kern_posix_fadvise(td, uap->fd, uap->offset, uap->len,
4367 return (kern_posix_error(td, error));