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;
412 if (bufseg == UIO_SYSSPACE)
417 maxcount = bufsize / sizeof(struct statfs);
421 } else if (bufseg == UIO_USERSPACE) {
424 } else /* if (bufseg == UIO_SYSSPACE) */ {
426 mtx_lock(&mountlist_mtx);
427 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
430 mtx_unlock(&mountlist_mtx);
431 if (maxcount > count)
433 tofree = sfsp = *buf = malloc(maxcount * sizeof(struct statfs),
437 mtx_lock(&mountlist_mtx);
438 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
439 if (prison_canseemount(td->td_ucred, mp) != 0) {
440 nmp = TAILQ_NEXT(mp, mnt_list);
444 if (mac_mount_check_stat(td->td_ucred, mp) != 0) {
445 nmp = TAILQ_NEXT(mp, mnt_list);
449 if (mode == MNT_WAIT) {
450 if (vfs_busy(mp, MBF_MNTLSTLOCK) != 0) {
452 * If vfs_busy() failed, and MBF_NOWAIT
453 * wasn't passed, then the mp is gone.
454 * Furthermore, because of MBF_MNTLSTLOCK,
455 * the mountlist_mtx was dropped. We have
456 * no other choice than to start over.
458 mtx_unlock(&mountlist_mtx);
459 free(tofree, M_STATFS);
463 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK) != 0) {
464 nmp = TAILQ_NEXT(mp, mnt_list);
468 if (sfsp != NULL && count < maxcount) {
471 * Set these in case the underlying filesystem
474 sp->f_version = STATFS_VERSION;
475 sp->f_namemax = NAME_MAX;
476 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
478 * If MNT_NOWAIT is specified, do not refresh
481 if (mode != MNT_NOWAIT) {
482 error = VFS_STATFS(mp, sp);
484 mtx_lock(&mountlist_mtx);
485 nmp = TAILQ_NEXT(mp, mnt_list);
490 if (priv_check(td, PRIV_VFS_GENERATION)) {
491 sptmp = malloc(sizeof(struct statfs), M_STATFS,
494 sptmp->f_fsid.val[0] = sptmp->f_fsid.val[1] = 0;
495 prison_enforce_statfs(td->td_ucred, mp, sptmp);
499 if (bufseg == UIO_SYSSPACE) {
500 bcopy(sp, sfsp, sizeof(*sp));
501 free(sptmp, M_STATFS);
502 } else /* if (bufseg == UIO_USERSPACE) */ {
503 error = copyout(sp, sfsp, sizeof(*sp));
504 free(sptmp, M_STATFS);
513 mtx_lock(&mountlist_mtx);
514 nmp = TAILQ_NEXT(mp, mnt_list);
517 mtx_unlock(&mountlist_mtx);
518 if (sfsp != NULL && count > maxcount)
525 #ifdef COMPAT_FREEBSD4
527 * Get old format filesystem statistics.
529 static void cvtstatfs(struct statfs *, struct ostatfs *);
531 #ifndef _SYS_SYSPROTO_H_
532 struct freebsd4_statfs_args {
538 freebsd4_statfs(struct thread *td, struct freebsd4_statfs_args *uap)
544 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
545 error = kern_statfs(td, uap->path, UIO_USERSPACE, sfp);
547 cvtstatfs(sfp, &osb);
548 error = copyout(&osb, uap->buf, sizeof(osb));
555 * Get filesystem statistics.
557 #ifndef _SYS_SYSPROTO_H_
558 struct freebsd4_fstatfs_args {
564 freebsd4_fstatfs(struct thread *td, struct freebsd4_fstatfs_args *uap)
570 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
571 error = kern_fstatfs(td, uap->fd, sfp);
573 cvtstatfs(sfp, &osb);
574 error = copyout(&osb, uap->buf, sizeof(osb));
581 * Get statistics on all filesystems.
583 #ifndef _SYS_SYSPROTO_H_
584 struct freebsd4_getfsstat_args {
591 freebsd4_getfsstat(struct thread *td, struct freebsd4_getfsstat_args *uap)
593 struct statfs *buf, *sp;
598 if (uap->bufsize < 0)
600 count = uap->bufsize / sizeof(struct ostatfs);
601 if (count > SIZE_MAX / sizeof(struct statfs))
603 size = count * sizeof(struct statfs);
604 error = kern_getfsstat(td, &buf, size, &count, UIO_SYSSPACE,
608 td->td_retval[0] = count;
611 while (count != 0 && error == 0) {
613 error = copyout(&osb, uap->buf, sizeof(osb));
624 * Implement fstatfs() for (NFS) file handles.
626 #ifndef _SYS_SYSPROTO_H_
627 struct freebsd4_fhstatfs_args {
628 struct fhandle *u_fhp;
633 freebsd4_fhstatfs(struct thread *td, struct freebsd4_fhstatfs_args *uap)
640 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
643 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
644 error = kern_fhstatfs(td, fh, sfp);
646 cvtstatfs(sfp, &osb);
647 error = copyout(&osb, uap->buf, sizeof(osb));
654 * Convert a new format statfs structure to an old format statfs structure.
657 cvtstatfs(struct statfs *nsp, struct ostatfs *osp)
660 statfs_scale_blocks(nsp, LONG_MAX);
661 bzero(osp, sizeof(*osp));
662 osp->f_bsize = nsp->f_bsize;
663 osp->f_iosize = MIN(nsp->f_iosize, LONG_MAX);
664 osp->f_blocks = nsp->f_blocks;
665 osp->f_bfree = nsp->f_bfree;
666 osp->f_bavail = nsp->f_bavail;
667 osp->f_files = MIN(nsp->f_files, LONG_MAX);
668 osp->f_ffree = MIN(nsp->f_ffree, LONG_MAX);
669 osp->f_owner = nsp->f_owner;
670 osp->f_type = nsp->f_type;
671 osp->f_flags = nsp->f_flags;
672 osp->f_syncwrites = MIN(nsp->f_syncwrites, LONG_MAX);
673 osp->f_asyncwrites = MIN(nsp->f_asyncwrites, LONG_MAX);
674 osp->f_syncreads = MIN(nsp->f_syncreads, LONG_MAX);
675 osp->f_asyncreads = MIN(nsp->f_asyncreads, LONG_MAX);
676 strlcpy(osp->f_fstypename, nsp->f_fstypename,
677 MIN(MFSNAMELEN, OMFSNAMELEN));
678 strlcpy(osp->f_mntonname, nsp->f_mntonname,
679 MIN(MNAMELEN, OMNAMELEN));
680 strlcpy(osp->f_mntfromname, nsp->f_mntfromname,
681 MIN(MNAMELEN, OMNAMELEN));
682 osp->f_fsid = nsp->f_fsid;
684 #endif /* COMPAT_FREEBSD4 */
687 * Change current working directory to a given file descriptor.
689 #ifndef _SYS_SYSPROTO_H_
695 sys_fchdir(struct thread *td, struct fchdir_args *uap)
697 struct vnode *vp, *tdp;
703 AUDIT_ARG_FD(uap->fd);
704 error = getvnode(td, uap->fd, cap_rights_init(&rights, CAP_FCHDIR),
711 vn_lock(vp, LK_SHARED | LK_RETRY);
712 AUDIT_ARG_VNODE1(vp);
713 error = change_dir(vp, td);
714 while (!error && (mp = vp->v_mountedhere) != NULL) {
717 error = VFS_ROOT(mp, LK_SHARED, &tdp);
734 * Change current working directory (``.'').
736 #ifndef _SYS_SYSPROTO_H_
742 sys_chdir(struct thread *td, struct chdir_args *uap)
745 return (kern_chdir(td, uap->path, UIO_USERSPACE));
749 kern_chdir(struct thread *td, char *path, enum uio_seg pathseg)
754 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
756 if ((error = namei(&nd)) != 0)
758 if ((error = change_dir(nd.ni_vp, td)) != 0) {
760 NDFREE(&nd, NDF_ONLY_PNBUF);
763 VOP_UNLOCK(nd.ni_vp, 0);
764 NDFREE(&nd, NDF_ONLY_PNBUF);
765 pwd_chdir(td, nd.ni_vp);
770 * Change notion of root (``/'') directory.
772 #ifndef _SYS_SYSPROTO_H_
778 sys_chroot(struct thread *td, struct chroot_args *uap)
783 error = priv_check(td, PRIV_VFS_CHROOT);
786 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
787 UIO_USERSPACE, uap->path, td);
791 error = change_dir(nd.ni_vp, td);
795 error = mac_vnode_check_chroot(td->td_ucred, nd.ni_vp);
799 VOP_UNLOCK(nd.ni_vp, 0);
800 error = pwd_chroot(td, nd.ni_vp);
802 NDFREE(&nd, NDF_ONLY_PNBUF);
807 NDFREE(&nd, NDF_ONLY_PNBUF);
812 * Common routine for chroot and chdir. Callers must provide a locked vnode
816 change_dir(struct vnode *vp, struct thread *td)
822 ASSERT_VOP_LOCKED(vp, "change_dir(): vp not locked");
823 if (vp->v_type != VDIR)
826 error = mac_vnode_check_chdir(td->td_ucred, vp);
830 return (VOP_ACCESS(vp, VEXEC, td->td_ucred, td));
834 flags_to_rights(int flags, cap_rights_t *rightsp)
837 if (flags & O_EXEC) {
838 cap_rights_set(rightsp, CAP_FEXECVE);
840 switch ((flags & O_ACCMODE)) {
842 cap_rights_set(rightsp, CAP_READ);
845 cap_rights_set(rightsp, CAP_READ);
848 cap_rights_set(rightsp, CAP_WRITE);
849 if (!(flags & (O_APPEND | O_TRUNC)))
850 cap_rights_set(rightsp, CAP_SEEK);
856 cap_rights_set(rightsp, CAP_CREATE);
859 cap_rights_set(rightsp, CAP_FTRUNCATE);
861 if (flags & (O_SYNC | O_FSYNC))
862 cap_rights_set(rightsp, CAP_FSYNC);
864 if (flags & (O_EXLOCK | O_SHLOCK))
865 cap_rights_set(rightsp, CAP_FLOCK);
869 * Check permissions, allocate an open file structure, and call the device
870 * open routine if any.
872 #ifndef _SYS_SYSPROTO_H_
880 sys_open(struct thread *td, struct open_args *uap)
883 return (kern_openat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
884 uap->flags, uap->mode));
887 #ifndef _SYS_SYSPROTO_H_
896 sys_openat(struct thread *td, struct openat_args *uap)
899 AUDIT_ARG_FD(uap->fd);
900 return (kern_openat(td, uap->fd, uap->path, UIO_USERSPACE, uap->flag,
905 kern_openat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
908 struct proc *p = td->td_proc;
909 struct filedesc *fdp = p->p_fd;
914 int cmode, error, indx;
918 AUDIT_ARG_FFLAGS(flags);
919 AUDIT_ARG_MODE(mode);
920 cap_rights_init(&rights, CAP_LOOKUP);
921 flags_to_rights(flags, &rights);
923 * Only one of the O_EXEC, O_RDONLY, O_WRONLY and O_RDWR flags
926 if (flags & O_EXEC) {
927 if (flags & O_ACCMODE)
929 } else if ((flags & O_ACCMODE) == O_ACCMODE) {
932 flags = FFLAGS(flags);
936 * Allocate a file structure. The descriptor to reference it
937 * is allocated and set by finstall() below.
939 error = falloc_noinstall(td, &fp);
943 * An extra reference on `fp' has been held for us by
944 * falloc_noinstall().
946 /* Set the flags early so the finit in devfs can pick them up. */
947 fp->f_flag = flags & FMASK;
948 cmode = ((mode & ~fdp->fd_cmask) & ALLPERMS) & ~S_ISTXT;
949 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, fd,
951 td->td_dupfd = -1; /* XXX check for fdopen */
952 error = vn_open(&nd, &flags, cmode, fp);
955 * If the vn_open replaced the method vector, something
956 * wonderous happened deep below and we just pass it up
957 * pretending we know what we do.
959 if (error == ENXIO && fp->f_ops != &badfileops)
963 * Handle special fdopen() case. bleh.
965 * Don't do this for relative (capability) lookups; we don't
966 * understand exactly what would happen, and we don't think
967 * that it ever should.
969 if ((nd.ni_lcf & NI_LCF_STRICTRELATIVE) == 0 &&
970 (error == ENODEV || error == ENXIO) &&
972 error = dupfdopen(td, fdp, td->td_dupfd, flags, error,
981 NDFREE(&nd, NDF_ONLY_PNBUF);
985 * Store the vnode, for any f_type. Typically, the vnode use
986 * count is decremented by direct call to vn_closefile() for
987 * files that switched type in the cdevsw fdopen() method.
991 * If the file wasn't claimed by devfs bind it to the normal
992 * vnode operations here.
994 if (fp->f_ops == &badfileops) {
995 KASSERT(vp->v_type != VFIFO, ("Unexpected fifo."));
997 finit(fp, (flags & FMASK) | (fp->f_flag & FHASLOCK),
998 DTYPE_VNODE, vp, &vnops);
1002 if (flags & O_TRUNC) {
1003 error = fo_truncate(fp, 0, td->td_ucred, td);
1009 * If we haven't already installed the FD (for dupfdopen), do so now.
1012 struct filecaps *fcaps;
1015 if ((nd.ni_lcf & NI_LCF_STRICTRELATIVE) != 0)
1016 fcaps = &nd.ni_filecaps;
1020 error = finstall(td, fp, &indx, flags, fcaps);
1021 /* On success finstall() consumes fcaps. */
1023 filecaps_free(&nd.ni_filecaps);
1027 filecaps_free(&nd.ni_filecaps);
1031 * Release our private reference, leaving the one associated with
1032 * the descriptor table intact.
1035 td->td_retval[0] = indx;
1038 KASSERT(indx == -1, ("indx=%d, should be -1", indx));
1047 #ifndef _SYS_SYSPROTO_H_
1048 struct ocreat_args {
1054 ocreat(struct thread *td, struct ocreat_args *uap)
1057 return (kern_openat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1058 O_WRONLY | O_CREAT | O_TRUNC, uap->mode));
1060 #endif /* COMPAT_43 */
1063 * Create a special file.
1065 #ifndef _SYS_SYSPROTO_H_
1073 sys_mknod(struct thread *td, struct mknod_args *uap)
1076 return (kern_mknodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1077 uap->mode, uap->dev));
1080 #ifndef _SYS_SYSPROTO_H_
1081 struct mknodat_args {
1089 sys_mknodat(struct thread *td, struct mknodat_args *uap)
1092 return (kern_mknodat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode,
1097 kern_mknodat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1103 struct nameidata nd;
1104 cap_rights_t rights;
1105 int error, whiteout = 0;
1107 AUDIT_ARG_MODE(mode);
1109 switch (mode & S_IFMT) {
1112 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
1113 if (error == 0 && dev == VNOVAL)
1117 error = priv_check(td, PRIV_VFS_MKNOD_WHT);
1121 return (kern_mkfifoat(td, fd, path, pathseg, mode));
1131 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
1132 NOCACHE, pathseg, path, fd, cap_rights_init(&rights, CAP_MKNODAT),
1134 if ((error = namei(&nd)) != 0)
1138 NDFREE(&nd, NDF_ONLY_PNBUF);
1139 if (vp == nd.ni_dvp)
1147 vattr.va_mode = (mode & ALLPERMS) &
1148 ~td->td_proc->p_fd->fd_cmask;
1149 vattr.va_rdev = dev;
1152 switch (mode & S_IFMT) {
1154 vattr.va_type = VCHR;
1157 vattr.va_type = VBLK;
1163 panic("kern_mknod: invalid mode");
1166 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1167 NDFREE(&nd, NDF_ONLY_PNBUF);
1169 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1174 if (error == 0 && !whiteout)
1175 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp,
1176 &nd.ni_cnd, &vattr);
1180 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, CREATE);
1182 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp,
1183 &nd.ni_cnd, &vattr);
1188 NDFREE(&nd, NDF_ONLY_PNBUF);
1190 vn_finished_write(mp);
1195 * Create a named pipe.
1197 #ifndef _SYS_SYSPROTO_H_
1198 struct mkfifo_args {
1204 sys_mkfifo(struct thread *td, struct mkfifo_args *uap)
1207 return (kern_mkfifoat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1211 #ifndef _SYS_SYSPROTO_H_
1212 struct mkfifoat_args {
1219 sys_mkfifoat(struct thread *td, struct mkfifoat_args *uap)
1222 return (kern_mkfifoat(td, uap->fd, uap->path, UIO_USERSPACE,
1227 kern_mkfifoat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1232 struct nameidata nd;
1233 cap_rights_t rights;
1236 AUDIT_ARG_MODE(mode);
1239 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
1240 NOCACHE, pathseg, path, fd, cap_rights_init(&rights, CAP_MKFIFOAT),
1242 if ((error = namei(&nd)) != 0)
1244 if (nd.ni_vp != NULL) {
1245 NDFREE(&nd, NDF_ONLY_PNBUF);
1246 if (nd.ni_vp == nd.ni_dvp)
1253 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1254 NDFREE(&nd, NDF_ONLY_PNBUF);
1256 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1261 vattr.va_type = VFIFO;
1262 vattr.va_mode = (mode & ALLPERMS) & ~td->td_proc->p_fd->fd_cmask;
1264 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1269 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
1276 vn_finished_write(mp);
1277 NDFREE(&nd, NDF_ONLY_PNBUF);
1282 * Make a hard file link.
1284 #ifndef _SYS_SYSPROTO_H_
1291 sys_link(struct thread *td, struct link_args *uap)
1294 return (kern_linkat(td, AT_FDCWD, AT_FDCWD, uap->path, uap->link,
1295 UIO_USERSPACE, FOLLOW));
1298 #ifndef _SYS_SYSPROTO_H_
1299 struct linkat_args {
1308 sys_linkat(struct thread *td, struct linkat_args *uap)
1313 if (flag & ~AT_SYMLINK_FOLLOW)
1316 return (kern_linkat(td, uap->fd1, uap->fd2, uap->path1, uap->path2,
1317 UIO_USERSPACE, (flag & AT_SYMLINK_FOLLOW) ? FOLLOW : NOFOLLOW));
1320 int hardlink_check_uid = 0;
1321 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
1322 &hardlink_check_uid, 0,
1323 "Unprivileged processes cannot create hard links to files owned by other "
1325 static int hardlink_check_gid = 0;
1326 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
1327 &hardlink_check_gid, 0,
1328 "Unprivileged processes cannot create hard links to files owned by other "
1332 can_hardlink(struct vnode *vp, struct ucred *cred)
1337 if (!hardlink_check_uid && !hardlink_check_gid)
1340 error = VOP_GETATTR(vp, &va, cred);
1344 if (hardlink_check_uid && cred->cr_uid != va.va_uid) {
1345 error = priv_check_cred(cred, PRIV_VFS_LINK, 0);
1350 if (hardlink_check_gid && !groupmember(va.va_gid, cred)) {
1351 error = priv_check_cred(cred, PRIV_VFS_LINK, 0);
1360 kern_linkat(struct thread *td, int fd1, int fd2, char *path1, char *path2,
1361 enum uio_seg segflg, int follow)
1365 struct nameidata nd;
1366 cap_rights_t rights;
1371 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, segflg, path1, fd1,
1372 cap_rights_init(&rights, CAP_LINKAT_SOURCE), td);
1374 if ((error = namei(&nd)) != 0)
1376 NDFREE(&nd, NDF_ONLY_PNBUF);
1378 if (vp->v_type == VDIR) {
1380 return (EPERM); /* POSIX */
1382 NDINIT_ATRIGHTS(&nd, CREATE,
1383 LOCKPARENT | SAVENAME | AUDITVNODE2 | NOCACHE, segflg, path2, fd2,
1384 cap_rights_init(&rights, CAP_LINKAT_TARGET), td);
1385 if ((error = namei(&nd)) == 0) {
1386 if (nd.ni_vp != NULL) {
1387 NDFREE(&nd, NDF_ONLY_PNBUF);
1388 if (nd.ni_dvp == nd.ni_vp)
1395 } else if (nd.ni_dvp->v_mount != vp->v_mount) {
1397 * Cross-device link. No need to recheck
1398 * vp->v_type, since it cannot change, except
1401 NDFREE(&nd, NDF_ONLY_PNBUF);
1405 } else if ((error = vn_lock(vp, LK_EXCLUSIVE)) == 0) {
1406 error = can_hardlink(vp, td->td_ucred);
1409 error = mac_vnode_check_link(td->td_ucred,
1410 nd.ni_dvp, vp, &nd.ni_cnd);
1415 NDFREE(&nd, NDF_ONLY_PNBUF);
1418 error = vn_start_write(vp, &mp, V_NOWAIT);
1422 NDFREE(&nd, NDF_ONLY_PNBUF);
1423 error = vn_start_write(NULL, &mp,
1429 error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
1432 vn_finished_write(mp);
1433 NDFREE(&nd, NDF_ONLY_PNBUF);
1436 NDFREE(&nd, NDF_ONLY_PNBUF);
1446 * Make a symbolic link.
1448 #ifndef _SYS_SYSPROTO_H_
1449 struct symlink_args {
1455 sys_symlink(struct thread *td, struct symlink_args *uap)
1458 return (kern_symlinkat(td, uap->path, AT_FDCWD, uap->link,
1462 #ifndef _SYS_SYSPROTO_H_
1463 struct symlinkat_args {
1470 sys_symlinkat(struct thread *td, struct symlinkat_args *uap)
1473 return (kern_symlinkat(td, uap->path1, uap->fd, uap->path2,
1478 kern_symlinkat(struct thread *td, char *path1, int fd, char *path2,
1479 enum uio_seg segflg)
1484 struct nameidata nd;
1486 cap_rights_t rights;
1488 if (segflg == UIO_SYSSPACE) {
1491 syspath = uma_zalloc(namei_zone, M_WAITOK);
1492 if ((error = copyinstr(path1, syspath, MAXPATHLEN, NULL)) != 0)
1495 AUDIT_ARG_TEXT(syspath);
1498 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
1499 NOCACHE, segflg, path2, fd, cap_rights_init(&rights, CAP_SYMLINKAT),
1501 if ((error = namei(&nd)) != 0)
1504 NDFREE(&nd, NDF_ONLY_PNBUF);
1505 if (nd.ni_vp == nd.ni_dvp)
1513 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1514 NDFREE(&nd, NDF_ONLY_PNBUF);
1516 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1521 vattr.va_mode = ACCESSPERMS &~ td->td_proc->p_fd->fd_cmask;
1523 vattr.va_type = VLNK;
1524 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1529 error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr, syspath);
1535 NDFREE(&nd, NDF_ONLY_PNBUF);
1537 vn_finished_write(mp);
1539 if (segflg != UIO_SYSSPACE)
1540 uma_zfree(namei_zone, syspath);
1545 * Delete a whiteout from the filesystem.
1547 #ifndef _SYS_SYSPROTO_H_
1548 struct undelete_args {
1553 sys_undelete(struct thread *td, struct undelete_args *uap)
1556 struct nameidata nd;
1561 NDINIT(&nd, DELETE, LOCKPARENT | DOWHITEOUT | AUDITVNODE1,
1562 UIO_USERSPACE, uap->path, td);
1567 if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & ISWHITEOUT)) {
1568 NDFREE(&nd, NDF_ONLY_PNBUF);
1569 if (nd.ni_vp == nd.ni_dvp)
1577 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1578 NDFREE(&nd, NDF_ONLY_PNBUF);
1580 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1584 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, DELETE);
1585 NDFREE(&nd, NDF_ONLY_PNBUF);
1587 vn_finished_write(mp);
1592 * Delete a name from the filesystem.
1594 #ifndef _SYS_SYSPROTO_H_
1595 struct unlink_args {
1600 sys_unlink(struct thread *td, struct unlink_args *uap)
1603 return (kern_unlinkat(td, AT_FDCWD, uap->path, UIO_USERSPACE, 0));
1606 #ifndef _SYS_SYSPROTO_H_
1607 struct unlinkat_args {
1614 sys_unlinkat(struct thread *td, struct unlinkat_args *uap)
1616 int flag = uap->flag;
1618 char *path = uap->path;
1620 if (flag & ~AT_REMOVEDIR)
1623 if (flag & AT_REMOVEDIR)
1624 return (kern_rmdirat(td, fd, path, UIO_USERSPACE));
1626 return (kern_unlinkat(td, fd, path, UIO_USERSPACE, 0));
1630 kern_unlinkat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1635 struct nameidata nd;
1637 cap_rights_t rights;
1642 NDINIT_ATRIGHTS(&nd, DELETE, LOCKPARENT | LOCKLEAF | AUDITVNODE1,
1643 pathseg, path, fd, cap_rights_init(&rights, CAP_UNLINKAT), td);
1644 if ((error = namei(&nd)) != 0)
1645 return (error == EINVAL ? EPERM : error);
1647 if (vp->v_type == VDIR && oldinum == 0) {
1648 error = EPERM; /* POSIX */
1649 } else if (oldinum != 0 &&
1650 ((error = vn_stat(vp, &sb, td->td_ucred, NOCRED, td)) == 0) &&
1651 sb.st_ino != oldinum) {
1652 error = EIDRM; /* Identifier removed */
1655 * The root of a mounted filesystem cannot be deleted.
1657 * XXX: can this only be a VDIR case?
1659 if (vp->v_vflag & VV_ROOT)
1663 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1664 NDFREE(&nd, NDF_ONLY_PNBUF);
1666 if (vp == nd.ni_dvp)
1670 if ((error = vn_start_write(NULL, &mp,
1671 V_XSLEEP | PCATCH)) != 0)
1676 error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp,
1681 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_UNLINK);
1682 error = VOP_REMOVE(nd.ni_dvp, vp, &nd.ni_cnd);
1686 vn_finished_write(mp);
1688 NDFREE(&nd, NDF_ONLY_PNBUF);
1690 if (vp == nd.ni_dvp)
1698 * Reposition read/write file offset.
1700 #ifndef _SYS_SYSPROTO_H_
1709 sys_lseek(struct thread *td, struct lseek_args *uap)
1712 return (kern_lseek(td, uap->fd, uap->offset, uap->whence));
1716 kern_lseek(struct thread *td, int fd, off_t offset, int whence)
1719 cap_rights_t rights;
1723 error = fget(td, fd, cap_rights_init(&rights, CAP_SEEK), &fp);
1726 error = (fp->f_ops->fo_flags & DFLAG_SEEKABLE) != 0 ?
1727 fo_seek(fp, offset, whence, td) : ESPIPE;
1732 #if defined(COMPAT_43)
1734 * Reposition read/write file offset.
1736 #ifndef _SYS_SYSPROTO_H_
1737 struct olseek_args {
1744 olseek(struct thread *td, struct olseek_args *uap)
1747 return (kern_lseek(td, uap->fd, uap->offset, uap->whence));
1749 #endif /* COMPAT_43 */
1751 #if defined(COMPAT_FREEBSD6)
1752 /* Version with the 'pad' argument */
1754 freebsd6_lseek(struct thread *td, struct freebsd6_lseek_args *uap)
1757 return (kern_lseek(td, uap->fd, uap->offset, uap->whence));
1762 * Check access permissions using passed credentials.
1765 vn_access(struct vnode *vp, int user_flags, struct ucred *cred,
1771 /* Flags == 0 means only check for existence. */
1772 if (user_flags == 0)
1776 if (user_flags & R_OK)
1778 if (user_flags & W_OK)
1780 if (user_flags & X_OK)
1783 error = mac_vnode_check_access(cred, vp, accmode);
1787 if ((accmode & VWRITE) == 0 || (error = vn_writechk(vp)) == 0)
1788 error = VOP_ACCESS(vp, accmode, cred, td);
1793 * Check access permissions using "real" credentials.
1795 #ifndef _SYS_SYSPROTO_H_
1796 struct access_args {
1802 sys_access(struct thread *td, struct access_args *uap)
1805 return (kern_accessat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1809 #ifndef _SYS_SYSPROTO_H_
1810 struct faccessat_args {
1818 sys_faccessat(struct thread *td, struct faccessat_args *uap)
1821 return (kern_accessat(td, uap->fd, uap->path, UIO_USERSPACE, uap->flag,
1826 kern_accessat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1827 int flag, int amode)
1829 struct ucred *cred, *usecred;
1831 struct nameidata nd;
1832 cap_rights_t rights;
1835 if (flag & ~AT_EACCESS)
1837 if (amode != F_OK && (amode & ~(R_OK | W_OK | X_OK)) != 0)
1841 * Create and modify a temporary credential instead of one that
1842 * is potentially shared (if we need one).
1844 cred = td->td_ucred;
1845 if ((flag & AT_EACCESS) == 0 &&
1846 ((cred->cr_uid != cred->cr_ruid ||
1847 cred->cr_rgid != cred->cr_groups[0]))) {
1848 usecred = crdup(cred);
1849 usecred->cr_uid = cred->cr_ruid;
1850 usecred->cr_groups[0] = cred->cr_rgid;
1851 td->td_ucred = usecred;
1854 AUDIT_ARG_VALUE(amode);
1855 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF |
1856 AUDITVNODE1, pathseg, path, fd, cap_rights_init(&rights, CAP_FSTAT),
1858 if ((error = namei(&nd)) != 0)
1862 error = vn_access(vp, amode, usecred, td);
1863 NDFREE(&nd, NDF_ONLY_PNBUF);
1866 if (usecred != cred) {
1867 td->td_ucred = cred;
1874 * Check access permissions using "effective" credentials.
1876 #ifndef _SYS_SYSPROTO_H_
1877 struct eaccess_args {
1883 sys_eaccess(struct thread *td, struct eaccess_args *uap)
1886 return (kern_accessat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1887 AT_EACCESS, uap->amode));
1890 #if defined(COMPAT_43)
1892 * Get file status; this version follows links.
1894 #ifndef _SYS_SYSPROTO_H_
1901 ostat(struct thread *td, struct ostat_args *uap)
1907 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
1912 return (copyout(&osb, uap->ub, sizeof (osb)));
1916 * Get file status; this version does not follow links.
1918 #ifndef _SYS_SYSPROTO_H_
1919 struct olstat_args {
1925 olstat(struct thread *td, struct olstat_args *uap)
1931 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
1932 UIO_USERSPACE, &sb, NULL);
1936 return (copyout(&osb, uap->ub, sizeof (osb)));
1940 * Convert from an old to a new stat structure.
1943 cvtstat(struct stat *st, struct ostat *ost)
1946 bzero(ost, sizeof(*ost));
1947 ost->st_dev = st->st_dev;
1948 ost->st_ino = st->st_ino;
1949 ost->st_mode = st->st_mode;
1950 ost->st_nlink = st->st_nlink;
1951 ost->st_uid = st->st_uid;
1952 ost->st_gid = st->st_gid;
1953 ost->st_rdev = st->st_rdev;
1954 if (st->st_size < (quad_t)1 << 32)
1955 ost->st_size = st->st_size;
1958 ost->st_atim = st->st_atim;
1959 ost->st_mtim = st->st_mtim;
1960 ost->st_ctim = st->st_ctim;
1961 ost->st_blksize = st->st_blksize;
1962 ost->st_blocks = st->st_blocks;
1963 ost->st_flags = st->st_flags;
1964 ost->st_gen = st->st_gen;
1966 #endif /* COMPAT_43 */
1969 * Get file status; this version follows links.
1971 #ifndef _SYS_SYSPROTO_H_
1978 sys_stat(struct thread *td, struct stat_args *uap)
1983 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
1986 error = copyout(&sb, uap->ub, sizeof (sb));
1990 #ifndef _SYS_SYSPROTO_H_
1991 struct fstatat_args {
1999 sys_fstatat(struct thread *td, struct fstatat_args *uap)
2004 error = kern_statat(td, uap->flag, uap->fd, uap->path,
2005 UIO_USERSPACE, &sb, NULL);
2007 error = copyout(&sb, uap->buf, sizeof (sb));
2012 kern_statat(struct thread *td, int flag, int fd, char *path,
2013 enum uio_seg pathseg, struct stat *sbp,
2014 void (*hook)(struct vnode *vp, struct stat *sbp))
2016 struct nameidata nd;
2018 cap_rights_t rights;
2021 if (flag & ~AT_SYMLINK_NOFOLLOW)
2024 NDINIT_ATRIGHTS(&nd, LOOKUP, ((flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW :
2025 FOLLOW) | LOCKSHARED | LOCKLEAF | AUDITVNODE1, pathseg, path, fd,
2026 cap_rights_init(&rights, CAP_FSTAT), td);
2028 if ((error = namei(&nd)) != 0)
2030 error = vn_stat(nd.ni_vp, &sb, td->td_ucred, NOCRED, td);
2032 SDT_PROBE2(vfs, , stat, mode, path, sb.st_mode);
2033 if (S_ISREG(sb.st_mode))
2034 SDT_PROBE2(vfs, , stat, reg, path, pathseg);
2035 if (__predict_false(hook != NULL))
2036 hook(nd.ni_vp, &sb);
2038 NDFREE(&nd, NDF_ONLY_PNBUF);
2044 if (KTRPOINT(td, KTR_STRUCT))
2051 * Get file status; this version does not follow links.
2053 #ifndef _SYS_SYSPROTO_H_
2060 sys_lstat(struct thread *td, struct lstat_args *uap)
2065 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2066 UIO_USERSPACE, &sb, NULL);
2068 error = copyout(&sb, uap->ub, sizeof (sb));
2073 * Implementation of the NetBSD [l]stat() functions.
2076 cvtnstat( struct stat *sb, struct nstat *nsb)
2079 bzero(nsb, sizeof *nsb);
2080 nsb->st_dev = sb->st_dev;
2081 nsb->st_ino = sb->st_ino;
2082 nsb->st_mode = sb->st_mode;
2083 nsb->st_nlink = sb->st_nlink;
2084 nsb->st_uid = sb->st_uid;
2085 nsb->st_gid = sb->st_gid;
2086 nsb->st_rdev = sb->st_rdev;
2087 nsb->st_atim = sb->st_atim;
2088 nsb->st_mtim = sb->st_mtim;
2089 nsb->st_ctim = sb->st_ctim;
2090 nsb->st_size = sb->st_size;
2091 nsb->st_blocks = sb->st_blocks;
2092 nsb->st_blksize = sb->st_blksize;
2093 nsb->st_flags = sb->st_flags;
2094 nsb->st_gen = sb->st_gen;
2095 nsb->st_birthtim = sb->st_birthtim;
2098 #ifndef _SYS_SYSPROTO_H_
2105 sys_nstat(struct thread *td, struct nstat_args *uap)
2111 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
2115 cvtnstat(&sb, &nsb);
2116 return (copyout(&nsb, uap->ub, sizeof (nsb)));
2120 * NetBSD lstat. Get file status; this version does not follow links.
2122 #ifndef _SYS_SYSPROTO_H_
2129 sys_nlstat(struct thread *td, struct nlstat_args *uap)
2135 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2136 UIO_USERSPACE, &sb, NULL);
2139 cvtnstat(&sb, &nsb);
2140 return (copyout(&nsb, uap->ub, sizeof (nsb)));
2144 * Get configurable pathname variables.
2146 #ifndef _SYS_SYSPROTO_H_
2147 struct pathconf_args {
2153 sys_pathconf(struct thread *td, struct pathconf_args *uap)
2156 return (kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name, FOLLOW));
2159 #ifndef _SYS_SYSPROTO_H_
2160 struct lpathconf_args {
2166 sys_lpathconf(struct thread *td, struct lpathconf_args *uap)
2169 return (kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name,
2174 kern_pathconf(struct thread *td, char *path, enum uio_seg pathseg, int name,
2177 struct nameidata nd;
2180 NDINIT(&nd, LOOKUP, LOCKSHARED | LOCKLEAF | AUDITVNODE1 | flags,
2182 if ((error = namei(&nd)) != 0)
2184 NDFREE(&nd, NDF_ONLY_PNBUF);
2186 error = VOP_PATHCONF(nd.ni_vp, name, td->td_retval);
2192 * Return target name of a symbolic link.
2194 #ifndef _SYS_SYSPROTO_H_
2195 struct readlink_args {
2202 sys_readlink(struct thread *td, struct readlink_args *uap)
2205 return (kern_readlinkat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2206 uap->buf, UIO_USERSPACE, uap->count));
2208 #ifndef _SYS_SYSPROTO_H_
2209 struct readlinkat_args {
2217 sys_readlinkat(struct thread *td, struct readlinkat_args *uap)
2220 return (kern_readlinkat(td, uap->fd, uap->path, UIO_USERSPACE,
2221 uap->buf, UIO_USERSPACE, uap->bufsize));
2225 kern_readlinkat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2226 char *buf, enum uio_seg bufseg, size_t count)
2231 struct nameidata nd;
2234 if (count > IOSIZE_MAX)
2237 NDINIT_AT(&nd, LOOKUP, NOFOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
2238 pathseg, path, fd, td);
2240 if ((error = namei(&nd)) != 0)
2242 NDFREE(&nd, NDF_ONLY_PNBUF);
2245 error = mac_vnode_check_readlink(td->td_ucred, vp);
2251 if (vp->v_type != VLNK && (vp->v_vflag & VV_READLINK) == 0)
2254 aiov.iov_base = buf;
2255 aiov.iov_len = count;
2256 auio.uio_iov = &aiov;
2257 auio.uio_iovcnt = 1;
2258 auio.uio_offset = 0;
2259 auio.uio_rw = UIO_READ;
2260 auio.uio_segflg = bufseg;
2262 auio.uio_resid = count;
2263 error = VOP_READLINK(vp, &auio, td->td_ucred);
2264 td->td_retval[0] = count - auio.uio_resid;
2271 * Common implementation code for chflags() and fchflags().
2274 setfflags(struct thread *td, struct vnode *vp, u_long flags)
2280 /* We can't support the value matching VNOVAL. */
2281 if (flags == VNOVAL)
2282 return (EOPNOTSUPP);
2285 * Prevent non-root users from setting flags on devices. When
2286 * a device is reused, users can retain ownership of the device
2287 * if they are allowed to set flags and programs assume that
2288 * chown can't fail when done as root.
2290 if (vp->v_type == VCHR || vp->v_type == VBLK) {
2291 error = priv_check(td, PRIV_VFS_CHFLAGS_DEV);
2296 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2299 vattr.va_flags = flags;
2300 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2302 error = mac_vnode_check_setflags(td->td_ucred, vp, vattr.va_flags);
2305 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
2307 vn_finished_write(mp);
2312 * Change flags of a file given a path name.
2314 #ifndef _SYS_SYSPROTO_H_
2315 struct chflags_args {
2321 sys_chflags(struct thread *td, struct chflags_args *uap)
2324 return (kern_chflagsat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2328 #ifndef _SYS_SYSPROTO_H_
2329 struct chflagsat_args {
2337 sys_chflagsat(struct thread *td, struct chflagsat_args *uap)
2340 const char *path = uap->path;
2341 u_long flags = uap->flags;
2342 int atflag = uap->atflag;
2344 if (atflag & ~AT_SYMLINK_NOFOLLOW)
2347 return (kern_chflagsat(td, fd, path, UIO_USERSPACE, flags, atflag));
2351 * Same as chflags() but doesn't follow symlinks.
2353 #ifndef _SYS_SYSPROTO_H_
2354 struct lchflags_args {
2360 sys_lchflags(struct thread *td, struct lchflags_args *uap)
2363 return (kern_chflagsat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2364 uap->flags, AT_SYMLINK_NOFOLLOW));
2368 kern_chflagsat(struct thread *td, int fd, const char *path,
2369 enum uio_seg pathseg, u_long flags, int atflag)
2371 struct nameidata nd;
2372 cap_rights_t rights;
2375 AUDIT_ARG_FFLAGS(flags);
2376 follow = (atflag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
2377 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, pathseg, path, fd,
2378 cap_rights_init(&rights, CAP_FCHFLAGS), td);
2379 if ((error = namei(&nd)) != 0)
2381 NDFREE(&nd, NDF_ONLY_PNBUF);
2382 error = setfflags(td, nd.ni_vp, flags);
2388 * Change flags of a file given a file descriptor.
2390 #ifndef _SYS_SYSPROTO_H_
2391 struct fchflags_args {
2397 sys_fchflags(struct thread *td, struct fchflags_args *uap)
2400 cap_rights_t rights;
2403 AUDIT_ARG_FD(uap->fd);
2404 AUDIT_ARG_FFLAGS(uap->flags);
2405 error = getvnode(td, uap->fd, cap_rights_init(&rights, CAP_FCHFLAGS),
2410 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
2411 AUDIT_ARG_VNODE1(fp->f_vnode);
2412 VOP_UNLOCK(fp->f_vnode, 0);
2414 error = setfflags(td, fp->f_vnode, uap->flags);
2420 * Common implementation code for chmod(), lchmod() and fchmod().
2423 setfmode(struct thread *td, struct ucred *cred, struct vnode *vp, int mode)
2429 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2431 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2433 vattr.va_mode = mode & ALLPERMS;
2435 error = mac_vnode_check_setmode(cred, vp, vattr.va_mode);
2438 error = VOP_SETATTR(vp, &vattr, cred);
2440 vn_finished_write(mp);
2445 * Change mode of a file given path name.
2447 #ifndef _SYS_SYSPROTO_H_
2454 sys_chmod(struct thread *td, struct chmod_args *uap)
2457 return (kern_fchmodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2461 #ifndef _SYS_SYSPROTO_H_
2462 struct fchmodat_args {
2470 sys_fchmodat(struct thread *td, struct fchmodat_args *uap)
2472 int flag = uap->flag;
2474 char *path = uap->path;
2475 mode_t mode = uap->mode;
2477 if (flag & ~AT_SYMLINK_NOFOLLOW)
2480 return (kern_fchmodat(td, fd, path, UIO_USERSPACE, mode, flag));
2484 * Change mode of a file given path name (don't follow links.)
2486 #ifndef _SYS_SYSPROTO_H_
2487 struct lchmod_args {
2493 sys_lchmod(struct thread *td, struct lchmod_args *uap)
2496 return (kern_fchmodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2497 uap->mode, AT_SYMLINK_NOFOLLOW));
2501 kern_fchmodat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2502 mode_t mode, int flag)
2504 struct nameidata nd;
2505 cap_rights_t rights;
2508 AUDIT_ARG_MODE(mode);
2509 follow = (flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
2510 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, pathseg, path, fd,
2511 cap_rights_init(&rights, CAP_FCHMOD), td);
2512 if ((error = namei(&nd)) != 0)
2514 NDFREE(&nd, NDF_ONLY_PNBUF);
2515 error = setfmode(td, td->td_ucred, nd.ni_vp, mode);
2521 * Change mode of a file given a file descriptor.
2523 #ifndef _SYS_SYSPROTO_H_
2524 struct fchmod_args {
2530 sys_fchmod(struct thread *td, struct fchmod_args *uap)
2533 cap_rights_t rights;
2536 AUDIT_ARG_FD(uap->fd);
2537 AUDIT_ARG_MODE(uap->mode);
2539 error = fget(td, uap->fd, cap_rights_init(&rights, CAP_FCHMOD), &fp);
2542 error = fo_chmod(fp, uap->mode, td->td_ucred, td);
2548 * Common implementation for chown(), lchown(), and fchown()
2551 setfown(struct thread *td, struct ucred *cred, struct vnode *vp, uid_t uid,
2558 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2560 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2565 error = mac_vnode_check_setowner(cred, vp, vattr.va_uid,
2569 error = VOP_SETATTR(vp, &vattr, cred);
2571 vn_finished_write(mp);
2576 * Set ownership given a path name.
2578 #ifndef _SYS_SYSPROTO_H_
2586 sys_chown(struct thread *td, struct chown_args *uap)
2589 return (kern_fchownat(td, AT_FDCWD, uap->path, UIO_USERSPACE, uap->uid,
2593 #ifndef _SYS_SYSPROTO_H_
2594 struct fchownat_args {
2603 sys_fchownat(struct thread *td, struct fchownat_args *uap)
2608 if (flag & ~AT_SYMLINK_NOFOLLOW)
2611 return (kern_fchownat(td, uap->fd, uap->path, UIO_USERSPACE, uap->uid,
2612 uap->gid, uap->flag));
2616 kern_fchownat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2617 int uid, int gid, int flag)
2619 struct nameidata nd;
2620 cap_rights_t rights;
2623 AUDIT_ARG_OWNER(uid, gid);
2624 follow = (flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
2625 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, pathseg, path, fd,
2626 cap_rights_init(&rights, CAP_FCHOWN), td);
2628 if ((error = namei(&nd)) != 0)
2630 NDFREE(&nd, NDF_ONLY_PNBUF);
2631 error = setfown(td, td->td_ucred, nd.ni_vp, uid, gid);
2637 * Set ownership given a path name, do not cross symlinks.
2639 #ifndef _SYS_SYSPROTO_H_
2640 struct lchown_args {
2647 sys_lchown(struct thread *td, struct lchown_args *uap)
2650 return (kern_fchownat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2651 uap->uid, uap->gid, AT_SYMLINK_NOFOLLOW));
2655 * Set ownership given a file descriptor.
2657 #ifndef _SYS_SYSPROTO_H_
2658 struct fchown_args {
2665 sys_fchown(struct thread *td, struct fchown_args *uap)
2668 cap_rights_t rights;
2671 AUDIT_ARG_FD(uap->fd);
2672 AUDIT_ARG_OWNER(uap->uid, uap->gid);
2673 error = fget(td, uap->fd, cap_rights_init(&rights, CAP_FCHOWN), &fp);
2676 error = fo_chown(fp, uap->uid, uap->gid, td->td_ucred, td);
2682 * Common implementation code for utimes(), lutimes(), and futimes().
2685 getutimes(const struct timeval *usrtvp, enum uio_seg tvpseg,
2686 struct timespec *tsp)
2688 struct timeval tv[2];
2689 const struct timeval *tvp;
2692 if (usrtvp == NULL) {
2693 vfs_timestamp(&tsp[0]);
2696 if (tvpseg == UIO_SYSSPACE) {
2699 if ((error = copyin(usrtvp, tv, sizeof(tv))) != 0)
2704 if (tvp[0].tv_usec < 0 || tvp[0].tv_usec >= 1000000 ||
2705 tvp[1].tv_usec < 0 || tvp[1].tv_usec >= 1000000)
2707 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
2708 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
2714 * Common implementation code for futimens(), utimensat().
2716 #define UTIMENS_NULL 0x1
2717 #define UTIMENS_EXIT 0x2
2719 getutimens(const struct timespec *usrtsp, enum uio_seg tspseg,
2720 struct timespec *tsp, int *retflags)
2722 struct timespec tsnow;
2725 vfs_timestamp(&tsnow);
2727 if (usrtsp == NULL) {
2730 *retflags |= UTIMENS_NULL;
2733 if (tspseg == UIO_SYSSPACE) {
2736 } else if ((error = copyin(usrtsp, tsp, sizeof(*tsp) * 2)) != 0)
2738 if (tsp[0].tv_nsec == UTIME_OMIT && tsp[1].tv_nsec == UTIME_OMIT)
2739 *retflags |= UTIMENS_EXIT;
2740 if (tsp[0].tv_nsec == UTIME_NOW && tsp[1].tv_nsec == UTIME_NOW)
2741 *retflags |= UTIMENS_NULL;
2742 if (tsp[0].tv_nsec == UTIME_OMIT)
2743 tsp[0].tv_sec = VNOVAL;
2744 else if (tsp[0].tv_nsec == UTIME_NOW)
2746 else if (tsp[0].tv_nsec < 0 || tsp[0].tv_nsec >= 1000000000L)
2748 if (tsp[1].tv_nsec == UTIME_OMIT)
2749 tsp[1].tv_sec = VNOVAL;
2750 else if (tsp[1].tv_nsec == UTIME_NOW)
2752 else if (tsp[1].tv_nsec < 0 || tsp[1].tv_nsec >= 1000000000L)
2759 * Common implementation code for utimes(), lutimes(), futimes(), futimens(),
2763 setutimes(struct thread *td, struct vnode *vp, const struct timespec *ts,
2764 int numtimes, int nullflag)
2768 int error, setbirthtime;
2770 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2772 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2774 if (numtimes < 3 && !VOP_GETATTR(vp, &vattr, td->td_ucred) &&
2775 timespeccmp(&ts[1], &vattr.va_birthtime, < ))
2778 vattr.va_atime = ts[0];
2779 vattr.va_mtime = ts[1];
2781 vattr.va_birthtime = ts[1];
2783 vattr.va_birthtime = ts[2];
2785 vattr.va_vaflags |= VA_UTIMES_NULL;
2787 error = mac_vnode_check_setutimes(td->td_ucred, vp, vattr.va_atime,
2791 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
2793 vn_finished_write(mp);
2798 * Set the access and modification times of a file.
2800 #ifndef _SYS_SYSPROTO_H_
2801 struct utimes_args {
2803 struct timeval *tptr;
2807 sys_utimes(struct thread *td, struct utimes_args *uap)
2810 return (kern_utimesat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2811 uap->tptr, UIO_USERSPACE));
2814 #ifndef _SYS_SYSPROTO_H_
2815 struct futimesat_args {
2818 const struct timeval * times;
2822 sys_futimesat(struct thread *td, struct futimesat_args *uap)
2825 return (kern_utimesat(td, uap->fd, uap->path, UIO_USERSPACE,
2826 uap->times, UIO_USERSPACE));
2830 kern_utimesat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2831 struct timeval *tptr, enum uio_seg tptrseg)
2833 struct nameidata nd;
2834 struct timespec ts[2];
2835 cap_rights_t rights;
2838 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
2840 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, fd,
2841 cap_rights_init(&rights, CAP_FUTIMES), td);
2843 if ((error = namei(&nd)) != 0)
2845 NDFREE(&nd, NDF_ONLY_PNBUF);
2846 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
2852 * Set the access and modification times of a file.
2854 #ifndef _SYS_SYSPROTO_H_
2855 struct lutimes_args {
2857 struct timeval *tptr;
2861 sys_lutimes(struct thread *td, struct lutimes_args *uap)
2864 return (kern_lutimes(td, uap->path, UIO_USERSPACE, uap->tptr,
2869 kern_lutimes(struct thread *td, char *path, enum uio_seg pathseg,
2870 struct timeval *tptr, enum uio_seg tptrseg)
2872 struct timespec ts[2];
2873 struct nameidata nd;
2876 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
2878 NDINIT(&nd, LOOKUP, NOFOLLOW | AUDITVNODE1, pathseg, path, td);
2879 if ((error = namei(&nd)) != 0)
2881 NDFREE(&nd, NDF_ONLY_PNBUF);
2882 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
2888 * Set the access and modification times of a file.
2890 #ifndef _SYS_SYSPROTO_H_
2891 struct futimes_args {
2893 struct timeval *tptr;
2897 sys_futimes(struct thread *td, struct futimes_args *uap)
2900 return (kern_futimes(td, uap->fd, uap->tptr, UIO_USERSPACE));
2904 kern_futimes(struct thread *td, int fd, struct timeval *tptr,
2905 enum uio_seg tptrseg)
2907 struct timespec ts[2];
2909 cap_rights_t rights;
2913 error = getutimes(tptr, tptrseg, ts);
2916 error = getvnode(td, fd, cap_rights_init(&rights, CAP_FUTIMES), &fp);
2920 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
2921 AUDIT_ARG_VNODE1(fp->f_vnode);
2922 VOP_UNLOCK(fp->f_vnode, 0);
2924 error = setutimes(td, fp->f_vnode, ts, 2, tptr == NULL);
2930 sys_futimens(struct thread *td, struct futimens_args *uap)
2933 return (kern_futimens(td, uap->fd, uap->times, UIO_USERSPACE));
2937 kern_futimens(struct thread *td, int fd, struct timespec *tptr,
2938 enum uio_seg tptrseg)
2940 struct timespec ts[2];
2942 cap_rights_t rights;
2946 error = getutimens(tptr, tptrseg, ts, &flags);
2949 if (flags & UTIMENS_EXIT)
2951 error = getvnode(td, fd, cap_rights_init(&rights, CAP_FUTIMES), &fp);
2955 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
2956 AUDIT_ARG_VNODE1(fp->f_vnode);
2957 VOP_UNLOCK(fp->f_vnode, 0);
2959 error = setutimes(td, fp->f_vnode, ts, 2, flags & UTIMENS_NULL);
2965 sys_utimensat(struct thread *td, struct utimensat_args *uap)
2968 return (kern_utimensat(td, uap->fd, uap->path, UIO_USERSPACE,
2969 uap->times, UIO_USERSPACE, uap->flag));
2973 kern_utimensat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2974 struct timespec *tptr, enum uio_seg tptrseg, int flag)
2976 struct nameidata nd;
2977 struct timespec ts[2];
2978 cap_rights_t rights;
2981 if (flag & ~AT_SYMLINK_NOFOLLOW)
2984 if ((error = getutimens(tptr, tptrseg, ts, &flags)) != 0)
2986 NDINIT_ATRIGHTS(&nd, LOOKUP, ((flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW :
2987 FOLLOW) | AUDITVNODE1, pathseg, path, fd,
2988 cap_rights_init(&rights, CAP_FUTIMES), td);
2989 if ((error = namei(&nd)) != 0)
2992 * We are allowed to call namei() regardless of 2xUTIME_OMIT.
2994 * "If both tv_nsec fields are UTIME_OMIT... EACCESS may be detected."
2995 * "Search permission is denied by a component of the path prefix."
2997 NDFREE(&nd, NDF_ONLY_PNBUF);
2998 if ((flags & UTIMENS_EXIT) == 0)
2999 error = setutimes(td, nd.ni_vp, ts, 2, flags & UTIMENS_NULL);
3005 * Truncate a file given its path name.
3007 #ifndef _SYS_SYSPROTO_H_
3008 struct truncate_args {
3015 sys_truncate(struct thread *td, struct truncate_args *uap)
3018 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3022 kern_truncate(struct thread *td, char *path, enum uio_seg pathseg, off_t length)
3028 struct nameidata nd;
3033 NDINIT(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, td);
3034 if ((error = namei(&nd)) != 0)
3037 rl_cookie = vn_rangelock_wlock(vp, 0, OFF_MAX);
3038 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0) {
3039 vn_rangelock_unlock(vp, rl_cookie);
3043 NDFREE(&nd, NDF_ONLY_PNBUF);
3044 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3045 if (vp->v_type == VDIR)
3048 else if ((error = mac_vnode_check_write(td->td_ucred, NOCRED, vp))) {
3051 else if ((error = vn_writechk(vp)) == 0 &&
3052 (error = VOP_ACCESS(vp, VWRITE, td->td_ucred, td)) == 0) {
3054 vattr.va_size = length;
3055 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3058 vn_finished_write(mp);
3059 vn_rangelock_unlock(vp, rl_cookie);
3064 #if defined(COMPAT_43)
3066 * Truncate a file given its path name.
3068 #ifndef _SYS_SYSPROTO_H_
3069 struct otruncate_args {
3075 otruncate(struct thread *td, struct otruncate_args *uap)
3078 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3080 #endif /* COMPAT_43 */
3082 #if defined(COMPAT_FREEBSD6)
3083 /* Versions with the pad argument */
3085 freebsd6_truncate(struct thread *td, struct freebsd6_truncate_args *uap)
3088 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3092 freebsd6_ftruncate(struct thread *td, struct freebsd6_ftruncate_args *uap)
3095 return (kern_ftruncate(td, uap->fd, uap->length));
3100 kern_fsync(struct thread *td, int fd, bool fullsync)
3105 cap_rights_t rights;
3106 int error, lock_flags;
3109 error = getvnode(td, fd, cap_rights_init(&rights, CAP_FSYNC), &fp);
3115 /* XXXKIB: compete outstanding aio writes */;
3117 error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
3120 if (MNT_SHARED_WRITES(mp) ||
3121 ((mp == NULL) && MNT_SHARED_WRITES(vp->v_mount))) {
3122 lock_flags = LK_SHARED;
3124 lock_flags = LK_EXCLUSIVE;
3126 vn_lock(vp, lock_flags | LK_RETRY);
3127 AUDIT_ARG_VNODE1(vp);
3128 if (vp->v_object != NULL) {
3129 VM_OBJECT_WLOCK(vp->v_object);
3130 vm_object_page_clean(vp->v_object, 0, 0, 0);
3131 VM_OBJECT_WUNLOCK(vp->v_object);
3133 error = fullsync ? VOP_FSYNC(vp, MNT_WAIT, td) : VOP_FDATASYNC(vp, td);
3135 vn_finished_write(mp);
3142 * Sync an open file.
3144 #ifndef _SYS_SYSPROTO_H_
3150 sys_fsync(struct thread *td, struct fsync_args *uap)
3153 return (kern_fsync(td, uap->fd, true));
3157 sys_fdatasync(struct thread *td, struct fdatasync_args *uap)
3160 return (kern_fsync(td, uap->fd, false));
3164 * Rename files. Source and destination must either both be directories, or
3165 * both not be directories. If target is a directory, it must be empty.
3167 #ifndef _SYS_SYSPROTO_H_
3168 struct rename_args {
3174 sys_rename(struct thread *td, struct rename_args *uap)
3177 return (kern_renameat(td, AT_FDCWD, uap->from, AT_FDCWD,
3178 uap->to, UIO_USERSPACE));
3181 #ifndef _SYS_SYSPROTO_H_
3182 struct renameat_args {
3190 sys_renameat(struct thread *td, struct renameat_args *uap)
3193 return (kern_renameat(td, uap->oldfd, uap->old, uap->newfd, uap->new,
3198 kern_renameat(struct thread *td, int oldfd, char *old, int newfd, char *new,
3199 enum uio_seg pathseg)
3201 struct mount *mp = NULL;
3202 struct vnode *tvp, *fvp, *tdvp;
3203 struct nameidata fromnd, tond;
3204 cap_rights_t rights;
3210 NDINIT_ATRIGHTS(&fromnd, DELETE, LOCKPARENT | LOCKLEAF | SAVESTART |
3211 AUDITVNODE1, pathseg, old, oldfd,
3212 cap_rights_init(&rights, CAP_RENAMEAT_SOURCE), td);
3214 NDINIT_ATRIGHTS(&fromnd, DELETE, WANTPARENT | SAVESTART | AUDITVNODE1,
3215 pathseg, old, oldfd,
3216 cap_rights_init(&rights, CAP_RENAMEAT_SOURCE), td);
3219 if ((error = namei(&fromnd)) != 0)
3222 error = mac_vnode_check_rename_from(td->td_ucred, fromnd.ni_dvp,
3223 fromnd.ni_vp, &fromnd.ni_cnd);
3224 VOP_UNLOCK(fromnd.ni_dvp, 0);
3225 if (fromnd.ni_dvp != fromnd.ni_vp)
3226 VOP_UNLOCK(fromnd.ni_vp, 0);
3229 NDINIT_ATRIGHTS(&tond, RENAME, LOCKPARENT | LOCKLEAF | NOCACHE |
3230 SAVESTART | AUDITVNODE2, pathseg, new, newfd,
3231 cap_rights_init(&rights, CAP_RENAMEAT_TARGET), td);
3232 if (fromnd.ni_vp->v_type == VDIR)
3233 tond.ni_cnd.cn_flags |= WILLBEDIR;
3234 if ((error = namei(&tond)) != 0) {
3235 /* Translate error code for rename("dir1", "dir2/."). */
3236 if (error == EISDIR && fvp->v_type == VDIR)
3238 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3239 vrele(fromnd.ni_dvp);
3245 error = vn_start_write(fvp, &mp, V_NOWAIT);
3247 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3248 NDFREE(&tond, NDF_ONLY_PNBUF);
3255 vrele(fromnd.ni_dvp);
3257 vrele(tond.ni_startdir);
3258 if (fromnd.ni_startdir != NULL)
3259 vrele(fromnd.ni_startdir);
3260 error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH);
3266 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
3269 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
3274 if (newfd != AT_FDCWD) {
3276 * If the target already exists we require CAP_UNLINKAT
3279 error = cap_check(&tond.ni_filecaps.fc_rights,
3280 cap_rights_init(&rights, CAP_UNLINKAT));
3291 * If the source is the same as the destination (that is, if they
3292 * are links to the same vnode), then there is nothing to do.
3298 error = mac_vnode_check_rename_to(td->td_ucred, tdvp,
3299 tond.ni_vp, fromnd.ni_dvp == tdvp, &tond.ni_cnd);
3303 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
3304 tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
3305 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3306 NDFREE(&tond, NDF_ONLY_PNBUF);
3308 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3309 NDFREE(&tond, NDF_ONLY_PNBUF);
3316 vrele(fromnd.ni_dvp);
3319 vrele(tond.ni_startdir);
3320 vn_finished_write(mp);
3322 if (fromnd.ni_startdir)
3323 vrele(fromnd.ni_startdir);
3330 * Make a directory file.
3332 #ifndef _SYS_SYSPROTO_H_
3339 sys_mkdir(struct thread *td, struct mkdir_args *uap)
3342 return (kern_mkdirat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
3346 #ifndef _SYS_SYSPROTO_H_
3347 struct mkdirat_args {
3354 sys_mkdirat(struct thread *td, struct mkdirat_args *uap)
3357 return (kern_mkdirat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode));
3361 kern_mkdirat(struct thread *td, int fd, char *path, enum uio_seg segflg,
3367 struct nameidata nd;
3368 cap_rights_t rights;
3371 AUDIT_ARG_MODE(mode);
3374 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
3375 NOCACHE, segflg, path, fd, cap_rights_init(&rights, CAP_MKDIRAT),
3377 nd.ni_cnd.cn_flags |= WILLBEDIR;
3378 if ((error = namei(&nd)) != 0)
3382 NDFREE(&nd, NDF_ONLY_PNBUF);
3384 * XXX namei called with LOCKPARENT but not LOCKLEAF has
3385 * the strange behaviour of leaving the vnode unlocked
3386 * if the target is the same vnode as the parent.
3388 if (vp == nd.ni_dvp)
3395 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3396 NDFREE(&nd, NDF_ONLY_PNBUF);
3398 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3403 vattr.va_type = VDIR;
3404 vattr.va_mode = (mode & ACCESSPERMS) &~ td->td_proc->p_fd->fd_cmask;
3406 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
3411 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
3415 NDFREE(&nd, NDF_ONLY_PNBUF);
3419 vn_finished_write(mp);
3424 * Remove a directory file.
3426 #ifndef _SYS_SYSPROTO_H_
3432 sys_rmdir(struct thread *td, struct rmdir_args *uap)
3435 return (kern_rmdirat(td, AT_FDCWD, uap->path, UIO_USERSPACE));
3439 kern_rmdirat(struct thread *td, int fd, char *path, enum uio_seg pathseg)
3443 struct nameidata nd;
3444 cap_rights_t rights;
3449 NDINIT_ATRIGHTS(&nd, DELETE, LOCKPARENT | LOCKLEAF | AUDITVNODE1,
3450 pathseg, path, fd, cap_rights_init(&rights, CAP_UNLINKAT), td);
3451 if ((error = namei(&nd)) != 0)
3454 if (vp->v_type != VDIR) {
3459 * No rmdir "." please.
3461 if (nd.ni_dvp == vp) {
3466 * The root of a mounted filesystem cannot be deleted.
3468 if (vp->v_vflag & VV_ROOT) {
3473 error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp,
3478 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3479 NDFREE(&nd, NDF_ONLY_PNBUF);
3481 if (nd.ni_dvp == vp)
3485 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3489 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_UNLINK);
3490 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
3491 vn_finished_write(mp);
3493 NDFREE(&nd, NDF_ONLY_PNBUF);
3495 if (nd.ni_dvp == vp)
3504 * Read a block of directory entries in a filesystem independent format.
3506 #ifndef _SYS_SYSPROTO_H_
3507 struct ogetdirentries_args {
3515 ogetdirentries(struct thread *td, struct ogetdirentries_args *uap)
3520 error = kern_ogetdirentries(td, uap, &loff);
3522 error = copyout(&loff, uap->basep, sizeof(long));
3527 kern_ogetdirentries(struct thread *td, struct ogetdirentries_args *uap,
3532 struct uio auio, kuio;
3533 struct iovec aiov, kiov;
3534 struct dirent *dp, *edp;
3535 cap_rights_t rights;
3537 int error, eofflag, readcnt;
3541 /* XXX arbitrary sanity limit on `count'. */
3542 if (uap->count > 64 * 1024)
3544 error = getvnode(td, uap->fd, cap_rights_init(&rights, CAP_READ), &fp);
3547 if ((fp->f_flag & FREAD) == 0) {
3552 foffset = foffset_lock(fp, 0);
3554 if (vp->v_type != VDIR) {
3555 foffset_unlock(fp, foffset, 0);
3559 aiov.iov_base = uap->buf;
3560 aiov.iov_len = uap->count;
3561 auio.uio_iov = &aiov;
3562 auio.uio_iovcnt = 1;
3563 auio.uio_rw = UIO_READ;
3564 auio.uio_segflg = UIO_USERSPACE;
3566 auio.uio_resid = uap->count;
3567 vn_lock(vp, LK_SHARED | LK_RETRY);
3568 loff = auio.uio_offset = foffset;
3570 error = mac_vnode_check_readdir(td->td_ucred, vp);
3573 foffset_unlock(fp, foffset, FOF_NOUPDATE);
3578 # if (BYTE_ORDER != LITTLE_ENDIAN)
3579 if (vp->v_mount->mnt_maxsymlinklen <= 0) {
3580 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag,
3582 foffset = auio.uio_offset;
3587 kuio.uio_iov = &kiov;
3588 kuio.uio_segflg = UIO_SYSSPACE;
3589 kiov.iov_len = uap->count;
3590 dirbuf = malloc(uap->count, M_TEMP, M_WAITOK);
3591 kiov.iov_base = dirbuf;
3592 error = VOP_READDIR(vp, &kuio, fp->f_cred, &eofflag,
3594 foffset = kuio.uio_offset;
3596 readcnt = uap->count - kuio.uio_resid;
3597 edp = (struct dirent *)&dirbuf[readcnt];
3598 for (dp = (struct dirent *)dirbuf; dp < edp; ) {
3599 # if (BYTE_ORDER == LITTLE_ENDIAN)
3601 * The expected low byte of
3602 * dp->d_namlen is our dp->d_type.
3603 * The high MBZ byte of dp->d_namlen
3604 * is our dp->d_namlen.
3606 dp->d_type = dp->d_namlen;
3610 * The dp->d_type is the high byte
3611 * of the expected dp->d_namlen,
3612 * so must be zero'ed.
3616 if (dp->d_reclen > 0) {
3617 dp = (struct dirent *)
3618 ((char *)dp + dp->d_reclen);
3625 error = uiomove(dirbuf, readcnt, &auio);
3627 free(dirbuf, M_TEMP);
3631 foffset_unlock(fp, foffset, 0);
3635 if (uap->count == auio.uio_resid &&
3636 (vp->v_vflag & VV_ROOT) &&
3637 (vp->v_mount->mnt_flag & MNT_UNION)) {
3638 struct vnode *tvp = vp;
3639 vp = vp->v_mount->mnt_vnodecovered;
3648 foffset_unlock(fp, foffset, 0);
3650 td->td_retval[0] = uap->count - auio.uio_resid;
3655 #endif /* COMPAT_43 */
3658 * Read a block of directory entries in a filesystem independent format.
3660 #ifndef _SYS_SYSPROTO_H_
3661 struct getdirentries_args {
3669 sys_getdirentries(struct thread *td, struct getdirentries_args *uap)
3674 error = kern_getdirentries(td, uap->fd, uap->buf, uap->count, &base,
3675 NULL, UIO_USERSPACE);
3678 if (uap->basep != NULL)
3679 error = copyout(&base, uap->basep, sizeof(long));
3684 kern_getdirentries(struct thread *td, int fd, char *buf, u_int count,
3685 long *basep, ssize_t *residp, enum uio_seg bufseg)
3691 cap_rights_t rights;
3697 if (count > IOSIZE_MAX)
3699 auio.uio_resid = count;
3700 error = getvnode(td, fd, cap_rights_init(&rights, CAP_READ), &fp);
3703 if ((fp->f_flag & FREAD) == 0) {
3708 foffset = foffset_lock(fp, 0);
3710 if (vp->v_type != VDIR) {
3714 aiov.iov_base = buf;
3715 aiov.iov_len = count;
3716 auio.uio_iov = &aiov;
3717 auio.uio_iovcnt = 1;
3718 auio.uio_rw = UIO_READ;
3719 auio.uio_segflg = bufseg;
3721 vn_lock(vp, LK_SHARED | LK_RETRY);
3722 AUDIT_ARG_VNODE1(vp);
3723 loff = auio.uio_offset = foffset;
3725 error = mac_vnode_check_readdir(td->td_ucred, vp);
3728 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL,
3730 foffset = auio.uio_offset;
3735 if (count == auio.uio_resid &&
3736 (vp->v_vflag & VV_ROOT) &&
3737 (vp->v_mount->mnt_flag & MNT_UNION)) {
3738 struct vnode *tvp = vp;
3740 vp = vp->v_mount->mnt_vnodecovered;
3751 *residp = auio.uio_resid;
3752 td->td_retval[0] = count - auio.uio_resid;
3754 foffset_unlock(fp, foffset, 0);
3759 #ifndef _SYS_SYSPROTO_H_
3760 struct getdents_args {
3767 sys_getdents(struct thread *td, struct getdents_args *uap)
3769 struct getdirentries_args ap;
3773 ap.count = uap->count;
3775 return (sys_getdirentries(td, &ap));
3779 * Set the mode mask for creation of filesystem nodes.
3781 #ifndef _SYS_SYSPROTO_H_
3787 sys_umask(struct thread *td, struct umask_args *uap)
3789 struct filedesc *fdp;
3791 fdp = td->td_proc->p_fd;
3792 FILEDESC_XLOCK(fdp);
3793 td->td_retval[0] = fdp->fd_cmask;
3794 fdp->fd_cmask = uap->newmask & ALLPERMS;
3795 FILEDESC_XUNLOCK(fdp);
3800 * Void all references to file by ripping underlying filesystem away from
3803 #ifndef _SYS_SYSPROTO_H_
3804 struct revoke_args {
3809 sys_revoke(struct thread *td, struct revoke_args *uap)
3813 struct nameidata nd;
3816 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
3818 if ((error = namei(&nd)) != 0)
3821 NDFREE(&nd, NDF_ONLY_PNBUF);
3822 if (vp->v_type != VCHR || vp->v_rdev == NULL) {
3827 error = mac_vnode_check_revoke(td->td_ucred, vp);
3831 error = VOP_GETATTR(vp, &vattr, td->td_ucred);
3834 if (td->td_ucred->cr_uid != vattr.va_uid) {
3835 error = priv_check(td, PRIV_VFS_ADMIN);
3840 VOP_REVOKE(vp, REVOKEALL);
3847 * Convert a user file descriptor to a kernel file entry and check that, if it
3848 * is a capability, the correct rights are present. A reference on the file
3849 * entry is held upon returning.
3852 getvnode(struct thread *td, int fd, cap_rights_t *rightsp, struct file **fpp)
3857 error = fget_unlocked(td->td_proc->p_fd, fd, rightsp, &fp, NULL);
3862 * The file could be not of the vnode type, or it may be not
3863 * yet fully initialized, in which case the f_vnode pointer
3864 * may be set, but f_ops is still badfileops. E.g.,
3865 * devfs_open() transiently create such situation to
3866 * facilitate csw d_fdopen().
3868 * Dupfdopen() handling in kern_openat() installs the
3869 * half-baked file into the process descriptor table, allowing
3870 * other thread to dereference it. Guard against the race by
3873 if (fp->f_vnode == NULL || fp->f_ops == &badfileops) {
3883 * Get an (NFS) file handle.
3885 #ifndef _SYS_SYSPROTO_H_
3886 struct lgetfh_args {
3892 sys_lgetfh(struct thread *td, struct lgetfh_args *uap)
3894 struct nameidata nd;
3899 error = priv_check(td, PRIV_VFS_GETFH);
3902 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
3907 NDFREE(&nd, NDF_ONLY_PNBUF);
3909 bzero(&fh, sizeof(fh));
3910 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
3911 error = VOP_VPTOFH(vp, &fh.fh_fid);
3914 error = copyout(&fh, uap->fhp, sizeof (fh));
3918 #ifndef _SYS_SYSPROTO_H_
3925 sys_getfh(struct thread *td, struct getfh_args *uap)
3927 struct nameidata nd;
3932 error = priv_check(td, PRIV_VFS_GETFH);
3935 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
3940 NDFREE(&nd, NDF_ONLY_PNBUF);
3942 bzero(&fh, sizeof(fh));
3943 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
3944 error = VOP_VPTOFH(vp, &fh.fh_fid);
3947 error = copyout(&fh, uap->fhp, sizeof (fh));
3952 * syscall for the rpc.lockd to use to translate a NFS file handle into an
3955 * warning: do not remove the priv_check() call or this becomes one giant
3958 #ifndef _SYS_SYSPROTO_H_
3959 struct fhopen_args {
3960 const struct fhandle *u_fhp;
3965 sys_fhopen(struct thread *td, struct fhopen_args *uap)
3974 error = priv_check(td, PRIV_VFS_FHOPEN);
3978 fmode = FFLAGS(uap->flags);
3979 /* why not allow a non-read/write open for our lockd? */
3980 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
3982 error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
3985 /* find the mount point */
3986 mp = vfs_busyfs(&fhp.fh_fsid);
3989 /* now give me my vnode, it gets returned to me locked */
3990 error = VFS_FHTOVP(mp, &fhp.fh_fid, LK_EXCLUSIVE, &vp);
3995 error = falloc_noinstall(td, &fp);
4001 * An extra reference on `fp' has been held for us by
4002 * falloc_noinstall().
4008 error = vn_open_vnode(vp, fmode, td->td_ucred, td, fp);
4010 KASSERT(fp->f_ops == &badfileops,
4011 ("VOP_OPEN in fhopen() set f_ops"));
4012 KASSERT(td->td_dupfd < 0,
4013 ("fhopen() encountered fdopen()"));
4023 finit(fp, (fmode & FMASK) | (fp->f_flag & FHASLOCK), DTYPE_VNODE, vp,
4026 if ((fmode & O_TRUNC) != 0) {
4027 error = fo_truncate(fp, 0, td->td_ucred, td);
4032 error = finstall(td, fp, &indx, fmode, NULL);
4035 td->td_retval[0] = indx;
4040 * Stat an (NFS) file handle.
4042 #ifndef _SYS_SYSPROTO_H_
4043 struct fhstat_args {
4044 struct fhandle *u_fhp;
4049 sys_fhstat(struct thread *td, struct fhstat_args *uap)
4055 error = copyin(uap->u_fhp, &fh, sizeof(fh));
4058 error = kern_fhstat(td, fh, &sb);
4060 error = copyout(&sb, uap->sb, sizeof(sb));
4065 kern_fhstat(struct thread *td, struct fhandle fh, struct stat *sb)
4071 error = priv_check(td, PRIV_VFS_FHSTAT);
4074 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4076 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_EXCLUSIVE, &vp);
4080 error = vn_stat(vp, sb, td->td_ucred, NOCRED, td);
4086 * Implement fstatfs() for (NFS) file handles.
4088 #ifndef _SYS_SYSPROTO_H_
4089 struct fhstatfs_args {
4090 struct fhandle *u_fhp;
4095 sys_fhstatfs(struct thread *td, struct fhstatfs_args *uap)
4101 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4104 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
4105 error = kern_fhstatfs(td, fh, sfp);
4107 error = copyout(sfp, uap->buf, sizeof(*sfp));
4108 free(sfp, M_STATFS);
4113 kern_fhstatfs(struct thread *td, fhandle_t fh, struct statfs *buf)
4120 error = priv_check(td, PRIV_VFS_FHSTATFS);
4123 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4125 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_EXCLUSIVE, &vp);
4131 error = prison_canseemount(td->td_ucred, mp);
4135 error = mac_mount_check_stat(td->td_ucred, mp);
4140 * Set these in case the underlying filesystem fails to do so.
4143 sp->f_version = STATFS_VERSION;
4144 sp->f_namemax = NAME_MAX;
4145 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
4146 error = VFS_STATFS(mp, sp);
4155 kern_posix_fallocate(struct thread *td, int fd, off_t offset, off_t len)
4160 cap_rights_t rights;
4161 off_t olen, ooffset;
4164 if (offset < 0 || len <= 0)
4166 /* Check for wrap. */
4167 if (offset > OFF_MAX - len)
4169 error = fget(td, fd, cap_rights_init(&rights, CAP_PWRITE), &fp);
4172 if ((fp->f_ops->fo_flags & DFLAG_SEEKABLE) == 0) {
4176 if ((fp->f_flag & FWRITE) == 0) {
4180 if (fp->f_type != DTYPE_VNODE) {
4185 if (vp->v_type != VREG) {
4190 /* Allocating blocks may take a long time, so iterate. */
4197 error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
4200 error = vn_lock(vp, LK_EXCLUSIVE);
4202 vn_finished_write(mp);
4206 error = mac_vnode_check_write(td->td_ucred, fp->f_cred, vp);
4209 error = VOP_ALLOCATE(vp, &offset, &len);
4211 vn_finished_write(mp);
4213 if (olen + ooffset != offset + len) {
4214 panic("offset + len changed from %jx/%jx to %jx/%jx",
4215 ooffset, olen, offset, len);
4217 if (error != 0 || len == 0)
4219 KASSERT(olen > len, ("Iteration did not make progress?"));
4228 sys_posix_fallocate(struct thread *td, struct posix_fallocate_args *uap)
4232 error = kern_posix_fallocate(td, uap->fd, uap->offset, uap->len);
4233 return (kern_posix_error(td, error));
4237 * Unlike madvise(2), we do not make a best effort to remember every
4238 * possible caching hint. Instead, we remember the last setting with
4239 * the exception that we will allow POSIX_FADV_NORMAL to adjust the
4240 * region of any current setting.
4243 kern_posix_fadvise(struct thread *td, int fd, off_t offset, off_t len,
4246 struct fadvise_info *fa, *new;
4249 cap_rights_t rights;
4253 if (offset < 0 || len < 0 || offset > OFF_MAX - len)
4256 case POSIX_FADV_SEQUENTIAL:
4257 case POSIX_FADV_RANDOM:
4258 case POSIX_FADV_NOREUSE:
4259 new = malloc(sizeof(*fa), M_FADVISE, M_WAITOK);
4261 case POSIX_FADV_NORMAL:
4262 case POSIX_FADV_WILLNEED:
4263 case POSIX_FADV_DONTNEED:
4269 /* XXX: CAP_POSIX_FADVISE? */
4270 error = fget(td, fd, cap_rights_init(&rights), &fp);
4273 if ((fp->f_ops->fo_flags & DFLAG_SEEKABLE) == 0) {
4277 if (fp->f_type != DTYPE_VNODE) {
4282 if (vp->v_type != VREG) {
4289 end = offset + len - 1;
4291 case POSIX_FADV_SEQUENTIAL:
4292 case POSIX_FADV_RANDOM:
4293 case POSIX_FADV_NOREUSE:
4295 * Try to merge any existing non-standard region with
4296 * this new region if possible, otherwise create a new
4297 * non-standard region for this request.
4299 mtx_pool_lock(mtxpool_sleep, fp);
4301 if (fa != NULL && fa->fa_advice == advice &&
4302 ((fa->fa_start <= end && fa->fa_end >= offset) ||
4303 (end != OFF_MAX && fa->fa_start == end + 1) ||
4304 (fa->fa_end != OFF_MAX && fa->fa_end + 1 == offset))) {
4305 if (offset < fa->fa_start)
4306 fa->fa_start = offset;
4307 if (end > fa->fa_end)
4310 new->fa_advice = advice;
4311 new->fa_start = offset;
4316 mtx_pool_unlock(mtxpool_sleep, fp);
4318 case POSIX_FADV_NORMAL:
4320 * If a the "normal" region overlaps with an existing
4321 * non-standard region, trim or remove the
4322 * non-standard region.
4324 mtx_pool_lock(mtxpool_sleep, fp);
4327 if (offset <= fa->fa_start && end >= fa->fa_end) {
4329 fp->f_advice = NULL;
4330 } else if (offset <= fa->fa_start &&
4331 end >= fa->fa_start)
4332 fa->fa_start = end + 1;
4333 else if (offset <= fa->fa_end && end >= fa->fa_end)
4334 fa->fa_end = offset - 1;
4335 else if (offset >= fa->fa_start && end <= fa->fa_end) {
4337 * If the "normal" region is a middle
4338 * portion of the existing
4339 * non-standard region, just remove
4340 * the whole thing rather than picking
4341 * one side or the other to
4345 fp->f_advice = NULL;
4348 mtx_pool_unlock(mtxpool_sleep, fp);
4350 case POSIX_FADV_WILLNEED:
4351 case POSIX_FADV_DONTNEED:
4352 error = VOP_ADVISE(vp, offset, end, advice);
4358 free(new, M_FADVISE);
4363 sys_posix_fadvise(struct thread *td, struct posix_fadvise_args *uap)
4367 error = kern_posix_fadvise(td, uap->fd, uap->offset, uap->len,
4369 return (kern_posix_error(td, error));