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_
120 struct sync_args *uap;
122 struct mount *mp, *nmp;
125 mtx_lock(&mountlist_mtx);
126 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
127 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK)) {
128 nmp = TAILQ_NEXT(mp, mnt_list);
131 if ((mp->mnt_flag & MNT_RDONLY) == 0 &&
132 vn_start_write(NULL, &mp, V_NOWAIT) == 0) {
133 save = curthread_pflags_set(TDP_SYNCIO);
134 vfs_msync(mp, MNT_NOWAIT);
135 VFS_SYNC(mp, MNT_NOWAIT);
136 curthread_pflags_restore(save);
137 vn_finished_write(mp);
139 mtx_lock(&mountlist_mtx);
140 nmp = TAILQ_NEXT(mp, mnt_list);
143 mtx_unlock(&mountlist_mtx);
148 * Change filesystem quotas.
150 #ifndef _SYS_SYSPROTO_H_
151 struct quotactl_args {
159 sys_quotactl(td, uap)
161 register struct quotactl_args /* {
172 AUDIT_ARG_CMD(uap->cmd);
173 AUDIT_ARG_UID(uap->uid);
174 if (!prison_allow(td->td_ucred, PR_ALLOW_QUOTAS))
176 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
178 if ((error = namei(&nd)) != 0)
180 NDFREE(&nd, NDF_ONLY_PNBUF);
181 mp = nd.ni_vp->v_mount;
184 error = vfs_busy(mp, 0);
188 error = VFS_QUOTACTL(mp, uap->cmd, uap->uid, uap->arg);
191 * Since quota on operation typically needs to open quota
192 * file, the Q_QUOTAON handler needs to unbusy the mount point
193 * before calling into namei. Otherwise, unmount might be
194 * started between two vfs_busy() invocations (first is our,
195 * second is from mount point cross-walk code in lookup()),
198 * Require that Q_QUOTAON handles the vfs_busy() reference on
199 * its own, always returning with ubusied mount point.
201 if ((uap->cmd >> SUBCMDSHIFT) != Q_QUOTAON)
207 * Used by statfs conversion routines to scale the block size up if
208 * necessary so that all of the block counts are <= 'max_size'. Note
209 * that 'max_size' should be a bitmask, i.e. 2^n - 1 for some non-zero
213 statfs_scale_blocks(struct statfs *sf, long max_size)
218 KASSERT(powerof2(max_size + 1), ("%s: invalid max_size", __func__));
221 * Attempt to scale the block counts to give a more accurate
222 * overview to userland of the ratio of free space to used
223 * space. To do this, find the largest block count and compute
224 * a divisor that lets it fit into a signed integer <= max_size.
226 if (sf->f_bavail < 0)
227 count = -sf->f_bavail;
229 count = sf->f_bavail;
230 count = MAX(sf->f_blocks, MAX(sf->f_bfree, count));
231 if (count <= max_size)
234 count >>= flsl(max_size);
241 sf->f_bsize <<= shift;
242 sf->f_blocks >>= shift;
243 sf->f_bfree >>= shift;
244 sf->f_bavail >>= shift;
248 kern_do_statfs(struct thread *td, struct mount *mp, struct statfs *buf)
255 error = vfs_busy(mp, 0);
260 error = mac_mount_check_stat(td->td_ucred, mp);
265 * Set these in case the underlying filesystem fails to do so.
268 sp->f_version = STATFS_VERSION;
269 sp->f_namemax = NAME_MAX;
270 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
271 error = VFS_STATFS(mp, sp);
275 if (priv_check(td, PRIV_VFS_GENERATION)) {
276 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
277 prison_enforce_statfs(td->td_ucred, mp, buf);
285 * Get filesystem statistics.
287 #ifndef _SYS_SYSPROTO_H_
296 register struct statfs_args /* {
304 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
305 error = kern_statfs(td, uap->path, UIO_USERSPACE, sfp);
307 error = copyout(sfp, uap->buf, sizeof(struct statfs));
313 kern_statfs(struct thread *td, char *path, enum uio_seg pathseg,
320 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
325 mp = nd.ni_vp->v_mount;
327 NDFREE(&nd, NDF_ONLY_PNBUF);
329 return (kern_do_statfs(td, mp, buf));
333 * Get filesystem statistics.
335 #ifndef _SYS_SYSPROTO_H_
336 struct fstatfs_args {
344 register struct fstatfs_args /* {
352 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
353 error = kern_fstatfs(td, uap->fd, sfp);
355 error = copyout(sfp, uap->buf, sizeof(struct statfs));
361 kern_fstatfs(struct thread *td, int fd, struct statfs *buf)
370 error = getvnode(td, fd, cap_rights_init(&rights, CAP_FSTATFS), &fp);
374 vn_lock(vp, LK_SHARED | LK_RETRY);
376 AUDIT_ARG_VNODE1(vp);
383 return (kern_do_statfs(td, mp, buf));
387 * Get statistics on all filesystems.
389 #ifndef _SYS_SYSPROTO_H_
390 struct getfsstat_args {
397 sys_getfsstat(td, uap)
399 register struct getfsstat_args /* {
408 if (uap->bufsize < 0 || uap->bufsize > SIZE_MAX)
410 error = kern_getfsstat(td, &uap->buf, uap->bufsize, &count,
411 UIO_USERSPACE, uap->mode);
413 td->td_retval[0] = count;
418 * If (bufsize > 0 && bufseg == UIO_SYSSPACE)
419 * The caller is responsible for freeing memory which will be allocated
423 kern_getfsstat(struct thread *td, struct statfs **buf, size_t bufsize,
424 size_t *countp, enum uio_seg bufseg, int mode)
426 struct mount *mp, *nmp;
427 struct statfs *sfsp, *sp, *sptmp, *tofree;
428 size_t count, maxcount;
439 maxcount = bufsize / sizeof(struct statfs);
443 } else if (bufseg == UIO_USERSPACE) {
446 } else /* if (bufseg == UIO_SYSSPACE) */ {
448 mtx_lock(&mountlist_mtx);
449 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
452 mtx_unlock(&mountlist_mtx);
453 if (maxcount > count)
455 tofree = sfsp = *buf = malloc(maxcount * sizeof(struct statfs),
459 mtx_lock(&mountlist_mtx);
460 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
461 if (prison_canseemount(td->td_ucred, mp) != 0) {
462 nmp = TAILQ_NEXT(mp, mnt_list);
466 if (mac_mount_check_stat(td->td_ucred, mp) != 0) {
467 nmp = TAILQ_NEXT(mp, mnt_list);
471 if (mode == MNT_WAIT) {
472 if (vfs_busy(mp, MBF_MNTLSTLOCK) != 0) {
474 * If vfs_busy() failed, and MBF_NOWAIT
475 * wasn't passed, then the mp is gone.
476 * Furthermore, because of MBF_MNTLSTLOCK,
477 * the mountlist_mtx was dropped. We have
478 * no other choice than to start over.
480 mtx_unlock(&mountlist_mtx);
481 free(tofree, M_STATFS);
485 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK) != 0) {
486 nmp = TAILQ_NEXT(mp, mnt_list);
490 if (sfsp != NULL && count < maxcount) {
493 * Set these in case the underlying filesystem
496 sp->f_version = STATFS_VERSION;
497 sp->f_namemax = NAME_MAX;
498 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
500 * If MNT_NOWAIT is specified, do not refresh
503 if (mode != MNT_NOWAIT) {
504 error = VFS_STATFS(mp, sp);
506 mtx_lock(&mountlist_mtx);
507 nmp = TAILQ_NEXT(mp, mnt_list);
512 if (priv_check(td, PRIV_VFS_GENERATION)) {
513 sptmp = malloc(sizeof(struct statfs), M_STATFS,
516 sptmp->f_fsid.val[0] = sptmp->f_fsid.val[1] = 0;
517 prison_enforce_statfs(td->td_ucred, mp, sptmp);
521 if (bufseg == UIO_SYSSPACE) {
522 bcopy(sp, sfsp, sizeof(*sp));
523 free(sptmp, M_STATFS);
524 } else /* if (bufseg == UIO_USERSPACE) */ {
525 error = copyout(sp, sfsp, sizeof(*sp));
526 free(sptmp, M_STATFS);
535 mtx_lock(&mountlist_mtx);
536 nmp = TAILQ_NEXT(mp, mnt_list);
539 mtx_unlock(&mountlist_mtx);
540 if (sfsp != NULL && count > maxcount)
547 #ifdef COMPAT_FREEBSD4
549 * Get old format filesystem statistics.
551 static void cvtstatfs(struct statfs *, struct ostatfs *);
553 #ifndef _SYS_SYSPROTO_H_
554 struct freebsd4_statfs_args {
560 freebsd4_statfs(td, uap)
562 struct freebsd4_statfs_args /* {
571 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
572 error = kern_statfs(td, uap->path, UIO_USERSPACE, sfp);
574 cvtstatfs(sfp, &osb);
575 error = copyout(&osb, uap->buf, sizeof(osb));
582 * Get filesystem statistics.
584 #ifndef _SYS_SYSPROTO_H_
585 struct freebsd4_fstatfs_args {
591 freebsd4_fstatfs(td, uap)
593 struct freebsd4_fstatfs_args /* {
602 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
603 error = kern_fstatfs(td, uap->fd, sfp);
605 cvtstatfs(sfp, &osb);
606 error = copyout(&osb, uap->buf, sizeof(osb));
613 * Get statistics on all filesystems.
615 #ifndef _SYS_SYSPROTO_H_
616 struct freebsd4_getfsstat_args {
623 freebsd4_getfsstat(td, uap)
625 register struct freebsd4_getfsstat_args /* {
631 struct statfs *buf, *sp;
636 if (uap->bufsize < 0)
638 count = uap->bufsize / sizeof(struct ostatfs);
639 if (count > SIZE_MAX / sizeof(struct statfs))
641 size = count * sizeof(struct statfs);
642 error = kern_getfsstat(td, &buf, size, &count, UIO_SYSSPACE,
644 td->td_retval[0] = count;
647 while (count != 0 && error == 0) {
649 error = copyout(&osb, uap->buf, sizeof(osb));
660 * Implement fstatfs() for (NFS) file handles.
662 #ifndef _SYS_SYSPROTO_H_
663 struct freebsd4_fhstatfs_args {
664 struct fhandle *u_fhp;
669 freebsd4_fhstatfs(td, uap)
671 struct freebsd4_fhstatfs_args /* {
672 struct fhandle *u_fhp;
681 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
684 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
685 error = kern_fhstatfs(td, fh, sfp);
687 cvtstatfs(sfp, &osb);
688 error = copyout(&osb, uap->buf, sizeof(osb));
695 * Convert a new format statfs structure to an old format statfs structure.
703 statfs_scale_blocks(nsp, LONG_MAX);
704 bzero(osp, sizeof(*osp));
705 osp->f_bsize = nsp->f_bsize;
706 osp->f_iosize = MIN(nsp->f_iosize, LONG_MAX);
707 osp->f_blocks = nsp->f_blocks;
708 osp->f_bfree = nsp->f_bfree;
709 osp->f_bavail = nsp->f_bavail;
710 osp->f_files = MIN(nsp->f_files, LONG_MAX);
711 osp->f_ffree = MIN(nsp->f_ffree, LONG_MAX);
712 osp->f_owner = nsp->f_owner;
713 osp->f_type = nsp->f_type;
714 osp->f_flags = nsp->f_flags;
715 osp->f_syncwrites = MIN(nsp->f_syncwrites, LONG_MAX);
716 osp->f_asyncwrites = MIN(nsp->f_asyncwrites, LONG_MAX);
717 osp->f_syncreads = MIN(nsp->f_syncreads, LONG_MAX);
718 osp->f_asyncreads = MIN(nsp->f_asyncreads, LONG_MAX);
719 strlcpy(osp->f_fstypename, nsp->f_fstypename,
720 MIN(MFSNAMELEN, OMFSNAMELEN));
721 strlcpy(osp->f_mntonname, nsp->f_mntonname,
722 MIN(MNAMELEN, OMNAMELEN));
723 strlcpy(osp->f_mntfromname, nsp->f_mntfromname,
724 MIN(MNAMELEN, OMNAMELEN));
725 osp->f_fsid = nsp->f_fsid;
727 #endif /* COMPAT_FREEBSD4 */
730 * Change current working directory to a given file descriptor.
732 #ifndef _SYS_SYSPROTO_H_
740 struct fchdir_args /* {
744 struct vnode *vp, *tdp;
750 AUDIT_ARG_FD(uap->fd);
751 error = getvnode(td, uap->fd, cap_rights_init(&rights, CAP_FCHDIR),
758 vn_lock(vp, LK_SHARED | LK_RETRY);
759 AUDIT_ARG_VNODE1(vp);
760 error = change_dir(vp, td);
761 while (!error && (mp = vp->v_mountedhere) != NULL) {
764 error = VFS_ROOT(mp, LK_SHARED, &tdp);
781 * Change current working directory (``.'').
783 #ifndef _SYS_SYSPROTO_H_
791 struct chdir_args /* {
796 return (kern_chdir(td, uap->path, UIO_USERSPACE));
800 kern_chdir(struct thread *td, char *path, enum uio_seg pathseg)
805 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
807 if ((error = namei(&nd)) != 0)
809 if ((error = change_dir(nd.ni_vp, td)) != 0) {
811 NDFREE(&nd, NDF_ONLY_PNBUF);
814 VOP_UNLOCK(nd.ni_vp, 0);
815 NDFREE(&nd, NDF_ONLY_PNBUF);
816 pwd_chdir(td, nd.ni_vp);
821 * Change notion of root (``/'') directory.
823 #ifndef _SYS_SYSPROTO_H_
831 struct chroot_args /* {
838 error = priv_check(td, PRIV_VFS_CHROOT);
841 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
842 UIO_USERSPACE, uap->path, td);
846 error = change_dir(nd.ni_vp, td);
850 error = mac_vnode_check_chroot(td->td_ucred, nd.ni_vp);
854 VOP_UNLOCK(nd.ni_vp, 0);
855 error = pwd_chroot(td, nd.ni_vp);
857 NDFREE(&nd, NDF_ONLY_PNBUF);
862 NDFREE(&nd, NDF_ONLY_PNBUF);
867 * Common routine for chroot and chdir. Callers must provide a locked vnode
879 ASSERT_VOP_LOCKED(vp, "change_dir(): vp not locked");
880 if (vp->v_type != VDIR)
883 error = mac_vnode_check_chdir(td->td_ucred, vp);
887 return (VOP_ACCESS(vp, VEXEC, td->td_ucred, td));
891 flags_to_rights(int flags, cap_rights_t *rightsp)
894 if (flags & O_EXEC) {
895 cap_rights_set(rightsp, CAP_FEXECVE);
897 switch ((flags & O_ACCMODE)) {
899 cap_rights_set(rightsp, CAP_READ);
902 cap_rights_set(rightsp, CAP_READ);
905 cap_rights_set(rightsp, CAP_WRITE);
906 if (!(flags & (O_APPEND | O_TRUNC)))
907 cap_rights_set(rightsp, CAP_SEEK);
913 cap_rights_set(rightsp, CAP_CREATE);
916 cap_rights_set(rightsp, CAP_FTRUNCATE);
918 if (flags & (O_SYNC | O_FSYNC))
919 cap_rights_set(rightsp, CAP_FSYNC);
921 if (flags & (O_EXLOCK | O_SHLOCK))
922 cap_rights_set(rightsp, CAP_FLOCK);
926 * Check permissions, allocate an open file structure, and call the device
927 * open routine if any.
929 #ifndef _SYS_SYSPROTO_H_
939 register struct open_args /* {
946 return (kern_openat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
947 uap->flags, uap->mode));
950 #ifndef _SYS_SYSPROTO_H_
959 sys_openat(struct thread *td, struct openat_args *uap)
962 return (kern_openat(td, uap->fd, uap->path, UIO_USERSPACE, uap->flag,
967 kern_openat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
970 struct proc *p = td->td_proc;
971 struct filedesc *fdp = p->p_fd;
976 int cmode, error, indx;
980 AUDIT_ARG_FFLAGS(flags);
981 AUDIT_ARG_MODE(mode);
982 /* XXX: audit dirfd */
983 cap_rights_init(&rights, CAP_LOOKUP);
984 flags_to_rights(flags, &rights);
986 * Only one of the O_EXEC, O_RDONLY, O_WRONLY and O_RDWR flags
989 if (flags & O_EXEC) {
990 if (flags & O_ACCMODE)
992 } else if ((flags & O_ACCMODE) == O_ACCMODE) {
995 flags = FFLAGS(flags);
999 * Allocate a file structure. The descriptor to reference it
1000 * is allocated and set by finstall() below.
1002 error = falloc_noinstall(td, &fp);
1006 * An extra reference on `fp' has been held for us by
1007 * falloc_noinstall().
1009 /* Set the flags early so the finit in devfs can pick them up. */
1010 fp->f_flag = flags & FMASK;
1011 cmode = ((mode & ~fdp->fd_cmask) & ALLPERMS) & ~S_ISTXT;
1012 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, fd,
1014 td->td_dupfd = -1; /* XXX check for fdopen */
1015 error = vn_open(&nd, &flags, cmode, fp);
1018 * If the vn_open replaced the method vector, something
1019 * wonderous happened deep below and we just pass it up
1020 * pretending we know what we do.
1022 if (error == ENXIO && fp->f_ops != &badfileops)
1026 * Handle special fdopen() case. bleh.
1028 * Don't do this for relative (capability) lookups; we don't
1029 * understand exactly what would happen, and we don't think
1030 * that it ever should.
1032 if ((nd.ni_lcf & NI_LCF_STRICTRELATIVE) == 0 &&
1033 (error == ENODEV || error == ENXIO) &&
1034 td->td_dupfd >= 0) {
1035 error = dupfdopen(td, fdp, td->td_dupfd, flags, error,
1044 NDFREE(&nd, NDF_ONLY_PNBUF);
1048 * Store the vnode, for any f_type. Typically, the vnode use
1049 * count is decremented by direct call to vn_closefile() for
1050 * files that switched type in the cdevsw fdopen() method.
1054 * If the file wasn't claimed by devfs bind it to the normal
1055 * vnode operations here.
1057 if (fp->f_ops == &badfileops) {
1058 KASSERT(vp->v_type != VFIFO, ("Unexpected fifo."));
1060 finit(fp, (flags & FMASK) | (fp->f_flag & FHASLOCK),
1061 DTYPE_VNODE, vp, &vnops);
1065 if (flags & O_TRUNC) {
1066 error = fo_truncate(fp, 0, td->td_ucred, td);
1072 * If we haven't already installed the FD (for dupfdopen), do so now.
1075 struct filecaps *fcaps;
1078 if ((nd.ni_lcf & NI_LCF_STRICTRELATIVE) != 0)
1079 fcaps = &nd.ni_filecaps;
1083 error = finstall(td, fp, &indx, flags, fcaps);
1084 /* On success finstall() consumes fcaps. */
1086 filecaps_free(&nd.ni_filecaps);
1090 filecaps_free(&nd.ni_filecaps);
1094 * Release our private reference, leaving the one associated with
1095 * the descriptor table intact.
1098 td->td_retval[0] = indx;
1101 KASSERT(indx == -1, ("indx=%d, should be -1", indx));
1110 #ifndef _SYS_SYSPROTO_H_
1111 struct ocreat_args {
1119 register struct ocreat_args /* {
1125 return (kern_openat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1126 O_WRONLY | O_CREAT | O_TRUNC, uap->mode));
1128 #endif /* COMPAT_43 */
1131 * Create a special file.
1133 #ifndef _SYS_SYSPROTO_H_
1143 register struct mknod_args /* {
1150 return (kern_mknodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1151 uap->mode, uap->dev));
1154 #ifndef _SYS_SYSPROTO_H_
1155 struct mknodat_args {
1163 sys_mknodat(struct thread *td, struct mknodat_args *uap)
1166 return (kern_mknodat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode,
1171 kern_mknodat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1177 struct nameidata nd;
1178 cap_rights_t rights;
1179 int error, whiteout = 0;
1181 AUDIT_ARG_MODE(mode);
1183 switch (mode & S_IFMT) {
1186 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
1187 if (error == 0 && dev == VNOVAL)
1191 error = priv_check(td, PRIV_VFS_MKNOD_WHT);
1195 return (kern_mkfifoat(td, fd, path, pathseg, mode));
1205 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
1206 NOCACHE, pathseg, path, fd, cap_rights_init(&rights, CAP_MKNODAT),
1208 if ((error = namei(&nd)) != 0)
1212 NDFREE(&nd, NDF_ONLY_PNBUF);
1213 if (vp == nd.ni_dvp)
1221 vattr.va_mode = (mode & ALLPERMS) &
1222 ~td->td_proc->p_fd->fd_cmask;
1223 vattr.va_rdev = dev;
1226 switch (mode & S_IFMT) {
1228 vattr.va_type = VCHR;
1231 vattr.va_type = VBLK;
1237 panic("kern_mknod: invalid mode");
1240 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1241 NDFREE(&nd, NDF_ONLY_PNBUF);
1243 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1248 if (error == 0 && !whiteout)
1249 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp,
1250 &nd.ni_cnd, &vattr);
1254 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, CREATE);
1256 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp,
1257 &nd.ni_cnd, &vattr);
1262 NDFREE(&nd, NDF_ONLY_PNBUF);
1264 vn_finished_write(mp);
1269 * Create a named pipe.
1271 #ifndef _SYS_SYSPROTO_H_
1272 struct mkfifo_args {
1280 register struct mkfifo_args /* {
1286 return (kern_mkfifoat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1290 #ifndef _SYS_SYSPROTO_H_
1291 struct mkfifoat_args {
1298 sys_mkfifoat(struct thread *td, struct mkfifoat_args *uap)
1301 return (kern_mkfifoat(td, uap->fd, uap->path, UIO_USERSPACE,
1306 kern_mkfifoat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1311 struct nameidata nd;
1312 cap_rights_t rights;
1315 AUDIT_ARG_MODE(mode);
1318 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
1319 NOCACHE, pathseg, path, fd, cap_rights_init(&rights, CAP_MKFIFOAT),
1321 if ((error = namei(&nd)) != 0)
1323 if (nd.ni_vp != NULL) {
1324 NDFREE(&nd, NDF_ONLY_PNBUF);
1325 if (nd.ni_vp == nd.ni_dvp)
1332 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1333 NDFREE(&nd, NDF_ONLY_PNBUF);
1335 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1340 vattr.va_type = VFIFO;
1341 vattr.va_mode = (mode & ALLPERMS) & ~td->td_proc->p_fd->fd_cmask;
1343 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1348 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
1355 vn_finished_write(mp);
1356 NDFREE(&nd, NDF_ONLY_PNBUF);
1361 * Make a hard file link.
1363 #ifndef _SYS_SYSPROTO_H_
1372 register struct link_args /* {
1378 return (kern_linkat(td, AT_FDCWD, AT_FDCWD, uap->path, uap->link,
1379 UIO_USERSPACE, FOLLOW));
1382 #ifndef _SYS_SYSPROTO_H_
1383 struct linkat_args {
1392 sys_linkat(struct thread *td, struct linkat_args *uap)
1397 if (flag & ~AT_SYMLINK_FOLLOW)
1400 return (kern_linkat(td, uap->fd1, uap->fd2, uap->path1, uap->path2,
1401 UIO_USERSPACE, (flag & AT_SYMLINK_FOLLOW) ? FOLLOW : NOFOLLOW));
1404 int hardlink_check_uid = 0;
1405 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
1406 &hardlink_check_uid, 0,
1407 "Unprivileged processes cannot create hard links to files owned by other "
1409 static int hardlink_check_gid = 0;
1410 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
1411 &hardlink_check_gid, 0,
1412 "Unprivileged processes cannot create hard links to files owned by other "
1416 can_hardlink(struct vnode *vp, struct ucred *cred)
1421 if (!hardlink_check_uid && !hardlink_check_gid)
1424 error = VOP_GETATTR(vp, &va, cred);
1428 if (hardlink_check_uid && cred->cr_uid != va.va_uid) {
1429 error = priv_check_cred(cred, PRIV_VFS_LINK, 0);
1434 if (hardlink_check_gid && !groupmember(va.va_gid, cred)) {
1435 error = priv_check_cred(cred, PRIV_VFS_LINK, 0);
1444 kern_linkat(struct thread *td, int fd1, int fd2, char *path1, char *path2,
1445 enum uio_seg segflg, int follow)
1449 struct nameidata nd;
1450 cap_rights_t rights;
1455 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, segflg, path1, fd1,
1456 cap_rights_init(&rights, CAP_LINKAT_SOURCE), td);
1458 if ((error = namei(&nd)) != 0)
1460 NDFREE(&nd, NDF_ONLY_PNBUF);
1462 if (vp->v_type == VDIR) {
1464 return (EPERM); /* POSIX */
1466 NDINIT_ATRIGHTS(&nd, CREATE,
1467 LOCKPARENT | SAVENAME | AUDITVNODE2 | NOCACHE, segflg, path2, fd2,
1468 cap_rights_init(&rights, CAP_LINKAT_TARGET), td);
1469 if ((error = namei(&nd)) == 0) {
1470 if (nd.ni_vp != NULL) {
1471 NDFREE(&nd, NDF_ONLY_PNBUF);
1472 if (nd.ni_dvp == nd.ni_vp)
1479 } else if (nd.ni_dvp->v_mount != vp->v_mount) {
1481 * Cross-device link. No need to recheck
1482 * vp->v_type, since it cannot change, except
1485 NDFREE(&nd, NDF_ONLY_PNBUF);
1489 } else if ((error = vn_lock(vp, LK_EXCLUSIVE)) == 0) {
1490 error = can_hardlink(vp, td->td_ucred);
1493 error = mac_vnode_check_link(td->td_ucred,
1494 nd.ni_dvp, vp, &nd.ni_cnd);
1499 NDFREE(&nd, NDF_ONLY_PNBUF);
1502 error = vn_start_write(vp, &mp, V_NOWAIT);
1506 NDFREE(&nd, NDF_ONLY_PNBUF);
1507 error = vn_start_write(NULL, &mp,
1513 error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
1516 vn_finished_write(mp);
1517 NDFREE(&nd, NDF_ONLY_PNBUF);
1520 NDFREE(&nd, NDF_ONLY_PNBUF);
1530 * Make a symbolic link.
1532 #ifndef _SYS_SYSPROTO_H_
1533 struct symlink_args {
1539 sys_symlink(td, uap)
1541 register struct symlink_args /* {
1547 return (kern_symlinkat(td, uap->path, AT_FDCWD, uap->link,
1551 #ifndef _SYS_SYSPROTO_H_
1552 struct symlinkat_args {
1559 sys_symlinkat(struct thread *td, struct symlinkat_args *uap)
1562 return (kern_symlinkat(td, uap->path1, uap->fd, uap->path2,
1567 kern_symlinkat(struct thread *td, char *path1, int fd, char *path2,
1568 enum uio_seg segflg)
1573 struct nameidata nd;
1575 cap_rights_t rights;
1577 if (segflg == UIO_SYSSPACE) {
1580 syspath = uma_zalloc(namei_zone, M_WAITOK);
1581 if ((error = copyinstr(path1, syspath, MAXPATHLEN, NULL)) != 0)
1584 AUDIT_ARG_TEXT(syspath);
1587 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
1588 NOCACHE, segflg, path2, fd, cap_rights_init(&rights, CAP_SYMLINKAT),
1590 if ((error = namei(&nd)) != 0)
1593 NDFREE(&nd, NDF_ONLY_PNBUF);
1594 if (nd.ni_vp == nd.ni_dvp)
1602 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1603 NDFREE(&nd, NDF_ONLY_PNBUF);
1605 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1610 vattr.va_mode = ACCESSPERMS &~ td->td_proc->p_fd->fd_cmask;
1612 vattr.va_type = VLNK;
1613 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1618 error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr, syspath);
1624 NDFREE(&nd, NDF_ONLY_PNBUF);
1626 vn_finished_write(mp);
1628 if (segflg != UIO_SYSSPACE)
1629 uma_zfree(namei_zone, syspath);
1634 * Delete a whiteout from the filesystem.
1637 sys_undelete(td, uap)
1639 register struct undelete_args /* {
1644 struct nameidata nd;
1649 NDINIT(&nd, DELETE, LOCKPARENT | DOWHITEOUT | AUDITVNODE1,
1650 UIO_USERSPACE, uap->path, td);
1655 if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & ISWHITEOUT)) {
1656 NDFREE(&nd, NDF_ONLY_PNBUF);
1657 if (nd.ni_vp == nd.ni_dvp)
1665 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1666 NDFREE(&nd, NDF_ONLY_PNBUF);
1668 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1672 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, DELETE);
1673 NDFREE(&nd, NDF_ONLY_PNBUF);
1675 vn_finished_write(mp);
1680 * Delete a name from the filesystem.
1682 #ifndef _SYS_SYSPROTO_H_
1683 struct unlink_args {
1690 struct unlink_args /* {
1695 return (kern_unlinkat(td, AT_FDCWD, uap->path, UIO_USERSPACE, 0));
1698 #ifndef _SYS_SYSPROTO_H_
1699 struct unlinkat_args {
1706 sys_unlinkat(struct thread *td, struct unlinkat_args *uap)
1708 int flag = uap->flag;
1710 char *path = uap->path;
1712 if (flag & ~AT_REMOVEDIR)
1715 if (flag & AT_REMOVEDIR)
1716 return (kern_rmdirat(td, fd, path, UIO_USERSPACE));
1718 return (kern_unlinkat(td, fd, path, UIO_USERSPACE, 0));
1722 kern_unlinkat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1727 struct nameidata nd;
1729 cap_rights_t rights;
1734 NDINIT_ATRIGHTS(&nd, DELETE, LOCKPARENT | LOCKLEAF | AUDITVNODE1,
1735 pathseg, path, fd, cap_rights_init(&rights, CAP_UNLINKAT), td);
1736 if ((error = namei(&nd)) != 0)
1737 return (error == EINVAL ? EPERM : error);
1739 if (vp->v_type == VDIR && oldinum == 0) {
1740 error = EPERM; /* POSIX */
1741 } else if (oldinum != 0 &&
1742 ((error = vn_stat(vp, &sb, td->td_ucred, NOCRED, td)) == 0) &&
1743 sb.st_ino != oldinum) {
1744 error = EIDRM; /* Identifier removed */
1747 * The root of a mounted filesystem cannot be deleted.
1749 * XXX: can this only be a VDIR case?
1751 if (vp->v_vflag & VV_ROOT)
1755 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1756 NDFREE(&nd, NDF_ONLY_PNBUF);
1758 if (vp == nd.ni_dvp)
1762 if ((error = vn_start_write(NULL, &mp,
1763 V_XSLEEP | PCATCH)) != 0)
1768 error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp,
1773 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_UNLINK);
1774 error = VOP_REMOVE(nd.ni_dvp, vp, &nd.ni_cnd);
1778 vn_finished_write(mp);
1780 NDFREE(&nd, NDF_ONLY_PNBUF);
1782 if (vp == nd.ni_dvp)
1790 * Reposition read/write file offset.
1792 #ifndef _SYS_SYSPROTO_H_
1801 sys_lseek(struct thread *td, struct lseek_args *uap)
1804 return (kern_lseek(td, uap->fd, uap->offset, uap->whence));
1808 kern_lseek(struct thread *td, int fd, off_t offset, int whence)
1811 cap_rights_t rights;
1815 error = fget(td, fd, cap_rights_init(&rights, CAP_SEEK), &fp);
1818 error = (fp->f_ops->fo_flags & DFLAG_SEEKABLE) != 0 ?
1819 fo_seek(fp, offset, whence, td) : ESPIPE;
1824 #if defined(COMPAT_43)
1826 * Reposition read/write file offset.
1828 #ifndef _SYS_SYSPROTO_H_
1829 struct olseek_args {
1836 olseek(struct thread *td, struct olseek_args *uap)
1839 return (kern_lseek(td, uap->fd, uap->offset, uap->whence));
1841 #endif /* COMPAT_43 */
1843 #if defined(COMPAT_FREEBSD6)
1844 /* Version with the 'pad' argument */
1846 freebsd6_lseek(struct thread *td, struct freebsd6_lseek_args *uap)
1849 return (kern_lseek(td, uap->fd, uap->offset, uap->whence));
1854 * Check access permissions using passed credentials.
1857 vn_access(vp, user_flags, cred, td)
1866 /* Flags == 0 means only check for existence. */
1867 if (user_flags == 0)
1871 if (user_flags & R_OK)
1873 if (user_flags & W_OK)
1875 if (user_flags & X_OK)
1878 error = mac_vnode_check_access(cred, vp, accmode);
1882 if ((accmode & VWRITE) == 0 || (error = vn_writechk(vp)) == 0)
1883 error = VOP_ACCESS(vp, accmode, cred, td);
1888 * Check access permissions using "real" credentials.
1890 #ifndef _SYS_SYSPROTO_H_
1891 struct access_args {
1899 register struct access_args /* {
1905 return (kern_accessat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1909 #ifndef _SYS_SYSPROTO_H_
1910 struct faccessat_args {
1918 sys_faccessat(struct thread *td, struct faccessat_args *uap)
1921 return (kern_accessat(td, uap->fd, uap->path, UIO_USERSPACE, uap->flag,
1926 kern_accessat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1927 int flag, int amode)
1929 struct ucred *cred, *usecred;
1931 struct nameidata nd;
1932 cap_rights_t rights;
1935 if (flag & ~AT_EACCESS)
1937 if (amode != F_OK && (amode & ~(R_OK | W_OK | X_OK)) != 0)
1941 * Create and modify a temporary credential instead of one that
1942 * is potentially shared (if we need one).
1944 cred = td->td_ucred;
1945 if ((flag & AT_EACCESS) == 0 &&
1946 ((cred->cr_uid != cred->cr_ruid ||
1947 cred->cr_rgid != cred->cr_groups[0]))) {
1948 usecred = crdup(cred);
1949 usecred->cr_uid = cred->cr_ruid;
1950 usecred->cr_groups[0] = cred->cr_rgid;
1951 td->td_ucred = usecred;
1954 AUDIT_ARG_VALUE(amode);
1955 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF |
1956 AUDITVNODE1, pathseg, path, fd, cap_rights_init(&rights, CAP_FSTAT),
1958 if ((error = namei(&nd)) != 0)
1962 error = vn_access(vp, amode, usecred, td);
1963 NDFREE(&nd, NDF_ONLY_PNBUF);
1966 if (usecred != cred) {
1967 td->td_ucred = cred;
1974 * Check access permissions using "effective" credentials.
1976 #ifndef _SYS_SYSPROTO_H_
1977 struct eaccess_args {
1983 sys_eaccess(td, uap)
1985 register struct eaccess_args /* {
1991 return (kern_accessat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1992 AT_EACCESS, uap->amode));
1995 #if defined(COMPAT_43)
1997 * Get file status; this version follows links.
1999 #ifndef _SYS_SYSPROTO_H_
2008 register struct ostat_args /* {
2017 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
2022 return (copyout(&osb, uap->ub, sizeof (osb)));
2026 * Get file status; this version does not follow links.
2028 #ifndef _SYS_SYSPROTO_H_
2029 struct olstat_args {
2037 register struct olstat_args /* {
2046 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2047 UIO_USERSPACE, &sb, NULL);
2051 return (copyout(&osb, uap->ub, sizeof (osb)));
2055 * Convert from an old to a new stat structure.
2063 bzero(ost, sizeof(*ost));
2064 ost->st_dev = st->st_dev;
2065 ost->st_ino = st->st_ino;
2066 ost->st_mode = st->st_mode;
2067 ost->st_nlink = st->st_nlink;
2068 ost->st_uid = st->st_uid;
2069 ost->st_gid = st->st_gid;
2070 ost->st_rdev = st->st_rdev;
2071 if (st->st_size < (quad_t)1 << 32)
2072 ost->st_size = st->st_size;
2075 ost->st_atim = st->st_atim;
2076 ost->st_mtim = st->st_mtim;
2077 ost->st_ctim = st->st_ctim;
2078 ost->st_blksize = st->st_blksize;
2079 ost->st_blocks = st->st_blocks;
2080 ost->st_flags = st->st_flags;
2081 ost->st_gen = st->st_gen;
2083 #endif /* COMPAT_43 */
2086 * Get file status; this version follows links.
2088 #ifndef _SYS_SYSPROTO_H_
2097 register struct stat_args /* {
2105 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
2108 error = copyout(&sb, uap->ub, sizeof (sb));
2112 #ifndef _SYS_SYSPROTO_H_
2113 struct fstatat_args {
2121 sys_fstatat(struct thread *td, struct fstatat_args *uap)
2126 error = kern_statat(td, uap->flag, uap->fd, uap->path,
2127 UIO_USERSPACE, &sb, NULL);
2129 error = copyout(&sb, uap->buf, sizeof (sb));
2134 kern_statat(struct thread *td, int flag, int fd, char *path,
2135 enum uio_seg pathseg, struct stat *sbp,
2136 void (*hook)(struct vnode *vp, struct stat *sbp))
2138 struct nameidata nd;
2140 cap_rights_t rights;
2143 if (flag & ~AT_SYMLINK_NOFOLLOW)
2146 NDINIT_ATRIGHTS(&nd, LOOKUP, ((flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW :
2147 FOLLOW) | LOCKSHARED | LOCKLEAF | AUDITVNODE1, pathseg, path, fd,
2148 cap_rights_init(&rights, CAP_FSTAT), td);
2150 if ((error = namei(&nd)) != 0)
2152 error = vn_stat(nd.ni_vp, &sb, td->td_ucred, NOCRED, td);
2154 SDT_PROBE2(vfs, , stat, mode, path, sb.st_mode);
2155 if (S_ISREG(sb.st_mode))
2156 SDT_PROBE2(vfs, , stat, reg, path, pathseg);
2157 if (__predict_false(hook != NULL))
2158 hook(nd.ni_vp, &sb);
2160 NDFREE(&nd, NDF_ONLY_PNBUF);
2166 if (KTRPOINT(td, KTR_STRUCT))
2173 * Get file status; this version does not follow links.
2175 #ifndef _SYS_SYSPROTO_H_
2184 register struct lstat_args /* {
2192 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2193 UIO_USERSPACE, &sb, NULL);
2195 error = copyout(&sb, uap->ub, sizeof (sb));
2200 * Implementation of the NetBSD [l]stat() functions.
2208 bzero(nsb, sizeof *nsb);
2209 nsb->st_dev = sb->st_dev;
2210 nsb->st_ino = sb->st_ino;
2211 nsb->st_mode = sb->st_mode;
2212 nsb->st_nlink = sb->st_nlink;
2213 nsb->st_uid = sb->st_uid;
2214 nsb->st_gid = sb->st_gid;
2215 nsb->st_rdev = sb->st_rdev;
2216 nsb->st_atim = sb->st_atim;
2217 nsb->st_mtim = sb->st_mtim;
2218 nsb->st_ctim = sb->st_ctim;
2219 nsb->st_size = sb->st_size;
2220 nsb->st_blocks = sb->st_blocks;
2221 nsb->st_blksize = sb->st_blksize;
2222 nsb->st_flags = sb->st_flags;
2223 nsb->st_gen = sb->st_gen;
2224 nsb->st_birthtim = sb->st_birthtim;
2227 #ifndef _SYS_SYSPROTO_H_
2236 register struct nstat_args /* {
2245 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
2249 cvtnstat(&sb, &nsb);
2250 return (copyout(&nsb, uap->ub, sizeof (nsb)));
2254 * NetBSD lstat. Get file status; this version does not follow links.
2256 #ifndef _SYS_SYSPROTO_H_
2265 register struct nlstat_args /* {
2274 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2275 UIO_USERSPACE, &sb, NULL);
2278 cvtnstat(&sb, &nsb);
2279 return (copyout(&nsb, uap->ub, sizeof (nsb)));
2283 * Get configurable pathname variables.
2285 #ifndef _SYS_SYSPROTO_H_
2286 struct pathconf_args {
2292 sys_pathconf(td, uap)
2294 register struct pathconf_args /* {
2300 return (kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name, FOLLOW));
2303 #ifndef _SYS_SYSPROTO_H_
2304 struct lpathconf_args {
2310 sys_lpathconf(td, uap)
2312 register struct lpathconf_args /* {
2318 return (kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name,
2323 kern_pathconf(struct thread *td, char *path, enum uio_seg pathseg, int name,
2326 struct nameidata nd;
2329 NDINIT(&nd, LOOKUP, LOCKSHARED | LOCKLEAF | AUDITVNODE1 | flags,
2331 if ((error = namei(&nd)) != 0)
2333 NDFREE(&nd, NDF_ONLY_PNBUF);
2335 error = VOP_PATHCONF(nd.ni_vp, name, td->td_retval);
2341 * Return target name of a symbolic link.
2343 #ifndef _SYS_SYSPROTO_H_
2344 struct readlink_args {
2351 sys_readlink(td, uap)
2353 register struct readlink_args /* {
2360 return (kern_readlinkat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2361 uap->buf, UIO_USERSPACE, uap->count));
2363 #ifndef _SYS_SYSPROTO_H_
2364 struct readlinkat_args {
2372 sys_readlinkat(struct thread *td, struct readlinkat_args *uap)
2375 return (kern_readlinkat(td, uap->fd, uap->path, UIO_USERSPACE,
2376 uap->buf, UIO_USERSPACE, uap->bufsize));
2380 kern_readlinkat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2381 char *buf, enum uio_seg bufseg, size_t count)
2386 struct nameidata nd;
2389 if (count > IOSIZE_MAX)
2392 NDINIT_AT(&nd, LOOKUP, NOFOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
2393 pathseg, path, fd, td);
2395 if ((error = namei(&nd)) != 0)
2397 NDFREE(&nd, NDF_ONLY_PNBUF);
2400 error = mac_vnode_check_readlink(td->td_ucred, vp);
2406 if (vp->v_type != VLNK && (vp->v_vflag & VV_READLINK) == 0)
2409 aiov.iov_base = buf;
2410 aiov.iov_len = count;
2411 auio.uio_iov = &aiov;
2412 auio.uio_iovcnt = 1;
2413 auio.uio_offset = 0;
2414 auio.uio_rw = UIO_READ;
2415 auio.uio_segflg = bufseg;
2417 auio.uio_resid = count;
2418 error = VOP_READLINK(vp, &auio, td->td_ucred);
2419 td->td_retval[0] = count - auio.uio_resid;
2426 * Common implementation code for chflags() and fchflags().
2429 setfflags(td, vp, flags)
2438 /* We can't support the value matching VNOVAL. */
2439 if (flags == VNOVAL)
2440 return (EOPNOTSUPP);
2443 * Prevent non-root users from setting flags on devices. When
2444 * a device is reused, users can retain ownership of the device
2445 * if they are allowed to set flags and programs assume that
2446 * chown can't fail when done as root.
2448 if (vp->v_type == VCHR || vp->v_type == VBLK) {
2449 error = priv_check(td, PRIV_VFS_CHFLAGS_DEV);
2454 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2457 vattr.va_flags = flags;
2458 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2460 error = mac_vnode_check_setflags(td->td_ucred, vp, vattr.va_flags);
2463 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
2465 vn_finished_write(mp);
2470 * Change flags of a file given a path name.
2472 #ifndef _SYS_SYSPROTO_H_
2473 struct chflags_args {
2479 sys_chflags(td, uap)
2481 register struct chflags_args /* {
2487 return (kern_chflagsat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2491 #ifndef _SYS_SYSPROTO_H_
2492 struct chflagsat_args {
2500 sys_chflagsat(struct thread *td, struct chflagsat_args *uap)
2503 const char *path = uap->path;
2504 u_long flags = uap->flags;
2505 int atflag = uap->atflag;
2507 if (atflag & ~AT_SYMLINK_NOFOLLOW)
2510 return (kern_chflagsat(td, fd, path, UIO_USERSPACE, flags, atflag));
2514 * Same as chflags() but doesn't follow symlinks.
2517 sys_lchflags(td, uap)
2519 register struct lchflags_args /* {
2525 return (kern_chflagsat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2526 uap->flags, AT_SYMLINK_NOFOLLOW));
2530 kern_chflagsat(struct thread *td, int fd, const char *path,
2531 enum uio_seg pathseg, u_long flags, int atflag)
2533 struct nameidata nd;
2534 cap_rights_t rights;
2537 AUDIT_ARG_FFLAGS(flags);
2538 follow = (atflag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
2539 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, pathseg, path, fd,
2540 cap_rights_init(&rights, CAP_FCHFLAGS), td);
2541 if ((error = namei(&nd)) != 0)
2543 NDFREE(&nd, NDF_ONLY_PNBUF);
2544 error = setfflags(td, nd.ni_vp, flags);
2550 * Change flags of a file given a file descriptor.
2552 #ifndef _SYS_SYSPROTO_H_
2553 struct fchflags_args {
2559 sys_fchflags(td, uap)
2561 register struct fchflags_args /* {
2567 cap_rights_t rights;
2570 AUDIT_ARG_FD(uap->fd);
2571 AUDIT_ARG_FFLAGS(uap->flags);
2572 error = getvnode(td, uap->fd, cap_rights_init(&rights, CAP_FCHFLAGS),
2577 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
2578 AUDIT_ARG_VNODE1(fp->f_vnode);
2579 VOP_UNLOCK(fp->f_vnode, 0);
2581 error = setfflags(td, fp->f_vnode, uap->flags);
2587 * Common implementation code for chmod(), lchmod() and fchmod().
2590 setfmode(td, cred, vp, mode)
2600 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2602 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2604 vattr.va_mode = mode & ALLPERMS;
2606 error = mac_vnode_check_setmode(cred, vp, vattr.va_mode);
2609 error = VOP_SETATTR(vp, &vattr, cred);
2611 vn_finished_write(mp);
2616 * Change mode of a file given path name.
2618 #ifndef _SYS_SYSPROTO_H_
2627 register struct chmod_args /* {
2633 return (kern_fchmodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2637 #ifndef _SYS_SYSPROTO_H_
2638 struct fchmodat_args {
2646 sys_fchmodat(struct thread *td, struct fchmodat_args *uap)
2648 int flag = uap->flag;
2650 char *path = uap->path;
2651 mode_t mode = uap->mode;
2653 if (flag & ~AT_SYMLINK_NOFOLLOW)
2656 return (kern_fchmodat(td, fd, path, UIO_USERSPACE, mode, flag));
2660 * Change mode of a file given path name (don't follow links.)
2662 #ifndef _SYS_SYSPROTO_H_
2663 struct lchmod_args {
2671 register struct lchmod_args /* {
2677 return (kern_fchmodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2678 uap->mode, AT_SYMLINK_NOFOLLOW));
2682 kern_fchmodat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2683 mode_t mode, int flag)
2685 struct nameidata nd;
2686 cap_rights_t rights;
2689 AUDIT_ARG_MODE(mode);
2690 follow = (flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
2691 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, pathseg, path, fd,
2692 cap_rights_init(&rights, CAP_FCHMOD), td);
2693 if ((error = namei(&nd)) != 0)
2695 NDFREE(&nd, NDF_ONLY_PNBUF);
2696 error = setfmode(td, td->td_ucred, nd.ni_vp, mode);
2702 * Change mode of a file given a file descriptor.
2704 #ifndef _SYS_SYSPROTO_H_
2705 struct fchmod_args {
2711 sys_fchmod(struct thread *td, struct fchmod_args *uap)
2714 cap_rights_t rights;
2717 AUDIT_ARG_FD(uap->fd);
2718 AUDIT_ARG_MODE(uap->mode);
2720 error = fget(td, uap->fd, cap_rights_init(&rights, CAP_FCHMOD), &fp);
2723 error = fo_chmod(fp, uap->mode, td->td_ucred, td);
2729 * Common implementation for chown(), lchown(), and fchown()
2732 setfown(td, cred, vp, uid, gid)
2743 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2745 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2750 error = mac_vnode_check_setowner(cred, vp, vattr.va_uid,
2754 error = VOP_SETATTR(vp, &vattr, cred);
2756 vn_finished_write(mp);
2761 * Set ownership given a path name.
2763 #ifndef _SYS_SYSPROTO_H_
2773 register struct chown_args /* {
2780 return (kern_fchownat(td, AT_FDCWD, uap->path, UIO_USERSPACE, uap->uid,
2784 #ifndef _SYS_SYSPROTO_H_
2785 struct fchownat_args {
2794 sys_fchownat(struct thread *td, struct fchownat_args *uap)
2799 if (flag & ~AT_SYMLINK_NOFOLLOW)
2802 return (kern_fchownat(td, uap->fd, uap->path, UIO_USERSPACE, uap->uid,
2803 uap->gid, uap->flag));
2807 kern_fchownat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2808 int uid, int gid, int flag)
2810 struct nameidata nd;
2811 cap_rights_t rights;
2814 AUDIT_ARG_OWNER(uid, gid);
2815 follow = (flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
2816 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, pathseg, path, fd,
2817 cap_rights_init(&rights, CAP_FCHOWN), td);
2819 if ((error = namei(&nd)) != 0)
2821 NDFREE(&nd, NDF_ONLY_PNBUF);
2822 error = setfown(td, td->td_ucred, nd.ni_vp, uid, gid);
2828 * Set ownership given a path name, do not cross symlinks.
2830 #ifndef _SYS_SYSPROTO_H_
2831 struct lchown_args {
2840 register struct lchown_args /* {
2847 return (kern_fchownat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2848 uap->uid, uap->gid, AT_SYMLINK_NOFOLLOW));
2852 * Set ownership given a file descriptor.
2854 #ifndef _SYS_SYSPROTO_H_
2855 struct fchown_args {
2864 register struct fchown_args /* {
2871 cap_rights_t rights;
2874 AUDIT_ARG_FD(uap->fd);
2875 AUDIT_ARG_OWNER(uap->uid, uap->gid);
2876 error = fget(td, uap->fd, cap_rights_init(&rights, CAP_FCHOWN), &fp);
2879 error = fo_chown(fp, uap->uid, uap->gid, td->td_ucred, td);
2885 * Common implementation code for utimes(), lutimes(), and futimes().
2888 getutimes(usrtvp, tvpseg, tsp)
2889 const struct timeval *usrtvp;
2890 enum uio_seg tvpseg;
2891 struct timespec *tsp;
2893 struct timeval tv[2];
2894 const struct timeval *tvp;
2897 if (usrtvp == NULL) {
2898 vfs_timestamp(&tsp[0]);
2901 if (tvpseg == UIO_SYSSPACE) {
2904 if ((error = copyin(usrtvp, tv, sizeof(tv))) != 0)
2909 if (tvp[0].tv_usec < 0 || tvp[0].tv_usec >= 1000000 ||
2910 tvp[1].tv_usec < 0 || tvp[1].tv_usec >= 1000000)
2912 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
2913 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
2919 * Common implementation code for futimens(), utimensat().
2921 #define UTIMENS_NULL 0x1
2922 #define UTIMENS_EXIT 0x2
2924 getutimens(const struct timespec *usrtsp, enum uio_seg tspseg,
2925 struct timespec *tsp, int *retflags)
2927 struct timespec tsnow;
2930 vfs_timestamp(&tsnow);
2932 if (usrtsp == NULL) {
2935 *retflags |= UTIMENS_NULL;
2938 if (tspseg == UIO_SYSSPACE) {
2941 } else if ((error = copyin(usrtsp, tsp, sizeof(*tsp) * 2)) != 0)
2943 if (tsp[0].tv_nsec == UTIME_OMIT && tsp[1].tv_nsec == UTIME_OMIT)
2944 *retflags |= UTIMENS_EXIT;
2945 if (tsp[0].tv_nsec == UTIME_NOW && tsp[1].tv_nsec == UTIME_NOW)
2946 *retflags |= UTIMENS_NULL;
2947 if (tsp[0].tv_nsec == UTIME_OMIT)
2948 tsp[0].tv_sec = VNOVAL;
2949 else if (tsp[0].tv_nsec == UTIME_NOW)
2951 else if (tsp[0].tv_nsec < 0 || tsp[0].tv_nsec >= 1000000000L)
2953 if (tsp[1].tv_nsec == UTIME_OMIT)
2954 tsp[1].tv_sec = VNOVAL;
2955 else if (tsp[1].tv_nsec == UTIME_NOW)
2957 else if (tsp[1].tv_nsec < 0 || tsp[1].tv_nsec >= 1000000000L)
2964 * Common implementation code for utimes(), lutimes(), futimes(), futimens(),
2968 setutimes(td, vp, ts, numtimes, nullflag)
2971 const struct timespec *ts;
2977 int error, setbirthtime;
2979 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2981 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2983 if (numtimes < 3 && !VOP_GETATTR(vp, &vattr, td->td_ucred) &&
2984 timespeccmp(&ts[1], &vattr.va_birthtime, < ))
2987 vattr.va_atime = ts[0];
2988 vattr.va_mtime = ts[1];
2990 vattr.va_birthtime = ts[1];
2992 vattr.va_birthtime = ts[2];
2994 vattr.va_vaflags |= VA_UTIMES_NULL;
2996 error = mac_vnode_check_setutimes(td->td_ucred, vp, vattr.va_atime,
3000 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3002 vn_finished_write(mp);
3007 * Set the access and modification times of a file.
3009 #ifndef _SYS_SYSPROTO_H_
3010 struct utimes_args {
3012 struct timeval *tptr;
3018 register struct utimes_args /* {
3020 struct timeval *tptr;
3024 return (kern_utimesat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
3025 uap->tptr, UIO_USERSPACE));
3028 #ifndef _SYS_SYSPROTO_H_
3029 struct futimesat_args {
3032 const struct timeval * times;
3036 sys_futimesat(struct thread *td, struct futimesat_args *uap)
3039 return (kern_utimesat(td, uap->fd, uap->path, UIO_USERSPACE,
3040 uap->times, UIO_USERSPACE));
3044 kern_utimesat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
3045 struct timeval *tptr, enum uio_seg tptrseg)
3047 struct nameidata nd;
3048 struct timespec ts[2];
3049 cap_rights_t rights;
3052 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3054 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, fd,
3055 cap_rights_init(&rights, CAP_FUTIMES), td);
3057 if ((error = namei(&nd)) != 0)
3059 NDFREE(&nd, NDF_ONLY_PNBUF);
3060 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3066 * Set the access and modification times of a file.
3068 #ifndef _SYS_SYSPROTO_H_
3069 struct lutimes_args {
3071 struct timeval *tptr;
3075 sys_lutimes(td, uap)
3077 register struct lutimes_args /* {
3079 struct timeval *tptr;
3083 return (kern_lutimes(td, uap->path, UIO_USERSPACE, uap->tptr,
3088 kern_lutimes(struct thread *td, char *path, enum uio_seg pathseg,
3089 struct timeval *tptr, enum uio_seg tptrseg)
3091 struct timespec ts[2];
3092 struct nameidata nd;
3095 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3097 NDINIT(&nd, LOOKUP, NOFOLLOW | AUDITVNODE1, pathseg, path, td);
3098 if ((error = namei(&nd)) != 0)
3100 NDFREE(&nd, NDF_ONLY_PNBUF);
3101 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3107 * Set the access and modification times of a file.
3109 #ifndef _SYS_SYSPROTO_H_
3110 struct futimes_args {
3112 struct timeval *tptr;
3116 sys_futimes(td, uap)
3118 register struct futimes_args /* {
3120 struct timeval *tptr;
3124 return (kern_futimes(td, uap->fd, uap->tptr, UIO_USERSPACE));
3128 kern_futimes(struct thread *td, int fd, struct timeval *tptr,
3129 enum uio_seg tptrseg)
3131 struct timespec ts[2];
3133 cap_rights_t rights;
3137 error = getutimes(tptr, tptrseg, ts);
3140 error = getvnode(td, fd, cap_rights_init(&rights, CAP_FUTIMES), &fp);
3144 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
3145 AUDIT_ARG_VNODE1(fp->f_vnode);
3146 VOP_UNLOCK(fp->f_vnode, 0);
3148 error = setutimes(td, fp->f_vnode, ts, 2, tptr == NULL);
3154 sys_futimens(struct thread *td, struct futimens_args *uap)
3157 return (kern_futimens(td, uap->fd, uap->times, UIO_USERSPACE));
3161 kern_futimens(struct thread *td, int fd, struct timespec *tptr,
3162 enum uio_seg tptrseg)
3164 struct timespec ts[2];
3166 cap_rights_t rights;
3170 error = getutimens(tptr, tptrseg, ts, &flags);
3173 if (flags & UTIMENS_EXIT)
3175 error = getvnode(td, fd, cap_rights_init(&rights, CAP_FUTIMES), &fp);
3179 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
3180 AUDIT_ARG_VNODE1(fp->f_vnode);
3181 VOP_UNLOCK(fp->f_vnode, 0);
3183 error = setutimes(td, fp->f_vnode, ts, 2, flags & UTIMENS_NULL);
3189 sys_utimensat(struct thread *td, struct utimensat_args *uap)
3192 return (kern_utimensat(td, uap->fd, uap->path, UIO_USERSPACE,
3193 uap->times, UIO_USERSPACE, uap->flag));
3197 kern_utimensat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
3198 struct timespec *tptr, enum uio_seg tptrseg, int flag)
3200 struct nameidata nd;
3201 struct timespec ts[2];
3202 cap_rights_t rights;
3205 if (flag & ~AT_SYMLINK_NOFOLLOW)
3208 if ((error = getutimens(tptr, tptrseg, ts, &flags)) != 0)
3210 NDINIT_ATRIGHTS(&nd, LOOKUP, ((flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW :
3211 FOLLOW) | AUDITVNODE1, pathseg, path, fd,
3212 cap_rights_init(&rights, CAP_FUTIMES), td);
3213 if ((error = namei(&nd)) != 0)
3216 * We are allowed to call namei() regardless of 2xUTIME_OMIT.
3218 * "If both tv_nsec fields are UTIME_OMIT... EACCESS may be detected."
3219 * "Search permission is denied by a component of the path prefix."
3221 NDFREE(&nd, NDF_ONLY_PNBUF);
3222 if ((flags & UTIMENS_EXIT) == 0)
3223 error = setutimes(td, nd.ni_vp, ts, 2, flags & UTIMENS_NULL);
3229 * Truncate a file given its path name.
3231 #ifndef _SYS_SYSPROTO_H_
3232 struct truncate_args {
3239 sys_truncate(td, uap)
3241 register struct truncate_args /* {
3248 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3252 kern_truncate(struct thread *td, char *path, enum uio_seg pathseg, off_t length)
3258 struct nameidata nd;
3263 NDINIT(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, td);
3264 if ((error = namei(&nd)) != 0)
3267 rl_cookie = vn_rangelock_wlock(vp, 0, OFF_MAX);
3268 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0) {
3269 vn_rangelock_unlock(vp, rl_cookie);
3273 NDFREE(&nd, NDF_ONLY_PNBUF);
3274 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3275 if (vp->v_type == VDIR)
3278 else if ((error = mac_vnode_check_write(td->td_ucred, NOCRED, vp))) {
3281 else if ((error = vn_writechk(vp)) == 0 &&
3282 (error = VOP_ACCESS(vp, VWRITE, td->td_ucred, td)) == 0) {
3284 vattr.va_size = length;
3285 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3288 vn_finished_write(mp);
3289 vn_rangelock_unlock(vp, rl_cookie);
3294 #if defined(COMPAT_43)
3296 * Truncate a file given its path name.
3298 #ifndef _SYS_SYSPROTO_H_
3299 struct otruncate_args {
3305 otruncate(struct thread *td, struct otruncate_args *uap)
3308 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3310 #endif /* COMPAT_43 */
3312 #if defined(COMPAT_FREEBSD6)
3313 /* Versions with the pad argument */
3315 freebsd6_truncate(struct thread *td, struct freebsd6_truncate_args *uap)
3318 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3322 freebsd6_ftruncate(struct thread *td, struct freebsd6_ftruncate_args *uap)
3325 return (kern_ftruncate(td, uap->fd, uap->length));
3330 kern_fsync(struct thread *td, int fd, bool fullsync)
3335 cap_rights_t rights;
3336 int error, lock_flags;
3339 error = getvnode(td, fd, cap_rights_init(&rights, CAP_FSYNC), &fp);
3345 /* XXXKIB: compete outstanding aio writes */;
3347 error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
3350 if (MNT_SHARED_WRITES(mp) ||
3351 ((mp == NULL) && MNT_SHARED_WRITES(vp->v_mount))) {
3352 lock_flags = LK_SHARED;
3354 lock_flags = LK_EXCLUSIVE;
3356 vn_lock(vp, lock_flags | LK_RETRY);
3357 AUDIT_ARG_VNODE1(vp);
3358 if (vp->v_object != NULL) {
3359 VM_OBJECT_WLOCK(vp->v_object);
3360 vm_object_page_clean(vp->v_object, 0, 0, 0);
3361 VM_OBJECT_WUNLOCK(vp->v_object);
3363 error = fullsync ? VOP_FSYNC(vp, MNT_WAIT, td) : VOP_FDATASYNC(vp, td);
3365 vn_finished_write(mp);
3372 * Sync an open file.
3374 #ifndef _SYS_SYSPROTO_H_
3380 sys_fsync(struct thread *td, struct fsync_args *uap)
3383 return (kern_fsync(td, uap->fd, true));
3387 sys_fdatasync(struct thread *td, struct fdatasync_args *uap)
3390 return (kern_fsync(td, uap->fd, false));
3394 * Rename files. Source and destination must either both be directories, or
3395 * both not be directories. If target is a directory, it must be empty.
3397 #ifndef _SYS_SYSPROTO_H_
3398 struct rename_args {
3406 register struct rename_args /* {
3412 return (kern_renameat(td, AT_FDCWD, uap->from, AT_FDCWD,
3413 uap->to, UIO_USERSPACE));
3416 #ifndef _SYS_SYSPROTO_H_
3417 struct renameat_args {
3425 sys_renameat(struct thread *td, struct renameat_args *uap)
3428 return (kern_renameat(td, uap->oldfd, uap->old, uap->newfd, uap->new,
3433 kern_renameat(struct thread *td, int oldfd, char *old, int newfd, char *new,
3434 enum uio_seg pathseg)
3436 struct mount *mp = NULL;
3437 struct vnode *tvp, *fvp, *tdvp;
3438 struct nameidata fromnd, tond;
3439 cap_rights_t rights;
3445 NDINIT_ATRIGHTS(&fromnd, DELETE, LOCKPARENT | LOCKLEAF | SAVESTART |
3446 AUDITVNODE1, pathseg, old, oldfd,
3447 cap_rights_init(&rights, CAP_RENAMEAT_SOURCE), td);
3449 NDINIT_ATRIGHTS(&fromnd, DELETE, WANTPARENT | SAVESTART | AUDITVNODE1,
3450 pathseg, old, oldfd,
3451 cap_rights_init(&rights, CAP_RENAMEAT_SOURCE), td);
3454 if ((error = namei(&fromnd)) != 0)
3457 error = mac_vnode_check_rename_from(td->td_ucred, fromnd.ni_dvp,
3458 fromnd.ni_vp, &fromnd.ni_cnd);
3459 VOP_UNLOCK(fromnd.ni_dvp, 0);
3460 if (fromnd.ni_dvp != fromnd.ni_vp)
3461 VOP_UNLOCK(fromnd.ni_vp, 0);
3464 NDINIT_ATRIGHTS(&tond, RENAME, LOCKPARENT | LOCKLEAF | NOCACHE |
3465 SAVESTART | AUDITVNODE2, pathseg, new, newfd,
3466 cap_rights_init(&rights, CAP_RENAMEAT_TARGET), td);
3467 if (fromnd.ni_vp->v_type == VDIR)
3468 tond.ni_cnd.cn_flags |= WILLBEDIR;
3469 if ((error = namei(&tond)) != 0) {
3470 /* Translate error code for rename("dir1", "dir2/."). */
3471 if (error == EISDIR && fvp->v_type == VDIR)
3473 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3474 vrele(fromnd.ni_dvp);
3480 error = vn_start_write(fvp, &mp, V_NOWAIT);
3482 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3483 NDFREE(&tond, NDF_ONLY_PNBUF);
3490 vrele(fromnd.ni_dvp);
3492 vrele(tond.ni_startdir);
3493 if (fromnd.ni_startdir != NULL)
3494 vrele(fromnd.ni_startdir);
3495 error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH);
3501 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
3504 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
3509 if (newfd != AT_FDCWD) {
3511 * If the target already exists we require CAP_UNLINKAT
3514 error = cap_check(&tond.ni_filecaps.fc_rights,
3515 cap_rights_init(&rights, CAP_UNLINKAT));
3526 * If the source is the same as the destination (that is, if they
3527 * are links to the same vnode), then there is nothing to do.
3533 error = mac_vnode_check_rename_to(td->td_ucred, tdvp,
3534 tond.ni_vp, fromnd.ni_dvp == tdvp, &tond.ni_cnd);
3538 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
3539 tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
3540 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3541 NDFREE(&tond, NDF_ONLY_PNBUF);
3543 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3544 NDFREE(&tond, NDF_ONLY_PNBUF);
3551 vrele(fromnd.ni_dvp);
3554 vrele(tond.ni_startdir);
3555 vn_finished_write(mp);
3557 if (fromnd.ni_startdir)
3558 vrele(fromnd.ni_startdir);
3565 * Make a directory file.
3567 #ifndef _SYS_SYSPROTO_H_
3576 register struct mkdir_args /* {
3582 return (kern_mkdirat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
3586 #ifndef _SYS_SYSPROTO_H_
3587 struct mkdirat_args {
3594 sys_mkdirat(struct thread *td, struct mkdirat_args *uap)
3597 return (kern_mkdirat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode));
3601 kern_mkdirat(struct thread *td, int fd, char *path, enum uio_seg segflg,
3607 struct nameidata nd;
3608 cap_rights_t rights;
3611 AUDIT_ARG_MODE(mode);
3614 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
3615 NOCACHE, segflg, path, fd, cap_rights_init(&rights, CAP_MKDIRAT),
3617 nd.ni_cnd.cn_flags |= WILLBEDIR;
3618 if ((error = namei(&nd)) != 0)
3622 NDFREE(&nd, NDF_ONLY_PNBUF);
3624 * XXX namei called with LOCKPARENT but not LOCKLEAF has
3625 * the strange behaviour of leaving the vnode unlocked
3626 * if the target is the same vnode as the parent.
3628 if (vp == nd.ni_dvp)
3635 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3636 NDFREE(&nd, NDF_ONLY_PNBUF);
3638 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3643 vattr.va_type = VDIR;
3644 vattr.va_mode = (mode & ACCESSPERMS) &~ td->td_proc->p_fd->fd_cmask;
3646 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
3651 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
3655 NDFREE(&nd, NDF_ONLY_PNBUF);
3659 vn_finished_write(mp);
3664 * Remove a directory file.
3666 #ifndef _SYS_SYSPROTO_H_
3674 struct rmdir_args /* {
3679 return (kern_rmdirat(td, AT_FDCWD, uap->path, UIO_USERSPACE));
3683 kern_rmdirat(struct thread *td, int fd, char *path, enum uio_seg pathseg)
3687 struct nameidata nd;
3688 cap_rights_t rights;
3693 NDINIT_ATRIGHTS(&nd, DELETE, LOCKPARENT | LOCKLEAF | AUDITVNODE1,
3694 pathseg, path, fd, cap_rights_init(&rights, CAP_UNLINKAT), td);
3695 if ((error = namei(&nd)) != 0)
3698 if (vp->v_type != VDIR) {
3703 * No rmdir "." please.
3705 if (nd.ni_dvp == vp) {
3710 * The root of a mounted filesystem cannot be deleted.
3712 if (vp->v_vflag & VV_ROOT) {
3717 error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp,
3722 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3723 NDFREE(&nd, NDF_ONLY_PNBUF);
3725 if (nd.ni_dvp == vp)
3729 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3733 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_UNLINK);
3734 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
3735 vn_finished_write(mp);
3737 NDFREE(&nd, NDF_ONLY_PNBUF);
3739 if (nd.ni_dvp == vp)
3748 * Read a block of directory entries in a filesystem independent format.
3750 #ifndef _SYS_SYSPROTO_H_
3751 struct ogetdirentries_args {
3759 ogetdirentries(struct thread *td, struct ogetdirentries_args *uap)
3764 error = kern_ogetdirentries(td, uap, &loff);
3766 error = copyout(&loff, uap->basep, sizeof(long));
3771 kern_ogetdirentries(struct thread *td, struct ogetdirentries_args *uap,
3776 struct uio auio, kuio;
3777 struct iovec aiov, kiov;
3778 struct dirent *dp, *edp;
3779 cap_rights_t rights;
3781 int error, eofflag, readcnt;
3785 /* XXX arbitrary sanity limit on `count'. */
3786 if (uap->count > 64 * 1024)
3788 error = getvnode(td, uap->fd, cap_rights_init(&rights, CAP_READ), &fp);
3791 if ((fp->f_flag & FREAD) == 0) {
3796 foffset = foffset_lock(fp, 0);
3798 if (vp->v_type != VDIR) {
3799 foffset_unlock(fp, foffset, 0);
3803 aiov.iov_base = uap->buf;
3804 aiov.iov_len = uap->count;
3805 auio.uio_iov = &aiov;
3806 auio.uio_iovcnt = 1;
3807 auio.uio_rw = UIO_READ;
3808 auio.uio_segflg = UIO_USERSPACE;
3810 auio.uio_resid = uap->count;
3811 vn_lock(vp, LK_SHARED | LK_RETRY);
3812 loff = auio.uio_offset = foffset;
3814 error = mac_vnode_check_readdir(td->td_ucred, vp);
3817 foffset_unlock(fp, foffset, FOF_NOUPDATE);
3822 # if (BYTE_ORDER != LITTLE_ENDIAN)
3823 if (vp->v_mount->mnt_maxsymlinklen <= 0) {
3824 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag,
3826 foffset = auio.uio_offset;
3831 kuio.uio_iov = &kiov;
3832 kuio.uio_segflg = UIO_SYSSPACE;
3833 kiov.iov_len = uap->count;
3834 dirbuf = malloc(uap->count, M_TEMP, M_WAITOK);
3835 kiov.iov_base = dirbuf;
3836 error = VOP_READDIR(vp, &kuio, fp->f_cred, &eofflag,
3838 foffset = kuio.uio_offset;
3840 readcnt = uap->count - kuio.uio_resid;
3841 edp = (struct dirent *)&dirbuf[readcnt];
3842 for (dp = (struct dirent *)dirbuf; dp < edp; ) {
3843 # if (BYTE_ORDER == LITTLE_ENDIAN)
3845 * The expected low byte of
3846 * dp->d_namlen is our dp->d_type.
3847 * The high MBZ byte of dp->d_namlen
3848 * is our dp->d_namlen.
3850 dp->d_type = dp->d_namlen;
3854 * The dp->d_type is the high byte
3855 * of the expected dp->d_namlen,
3856 * so must be zero'ed.
3860 if (dp->d_reclen > 0) {
3861 dp = (struct dirent *)
3862 ((char *)dp + dp->d_reclen);
3869 error = uiomove(dirbuf, readcnt, &auio);
3871 free(dirbuf, M_TEMP);
3875 foffset_unlock(fp, foffset, 0);
3879 if (uap->count == auio.uio_resid &&
3880 (vp->v_vflag & VV_ROOT) &&
3881 (vp->v_mount->mnt_flag & MNT_UNION)) {
3882 struct vnode *tvp = vp;
3883 vp = vp->v_mount->mnt_vnodecovered;
3892 foffset_unlock(fp, foffset, 0);
3894 td->td_retval[0] = uap->count - auio.uio_resid;
3899 #endif /* COMPAT_43 */
3902 * Read a block of directory entries in a filesystem independent format.
3904 #ifndef _SYS_SYSPROTO_H_
3905 struct getdirentries_args {
3913 sys_getdirentries(td, uap)
3915 register struct getdirentries_args /* {
3925 error = kern_getdirentries(td, uap->fd, uap->buf, uap->count, &base,
3926 NULL, UIO_USERSPACE);
3929 if (uap->basep != NULL)
3930 error = copyout(&base, uap->basep, sizeof(long));
3935 kern_getdirentries(struct thread *td, int fd, char *buf, u_int count,
3936 long *basep, ssize_t *residp, enum uio_seg bufseg)
3942 cap_rights_t rights;
3948 if (count > IOSIZE_MAX)
3950 auio.uio_resid = count;
3951 error = getvnode(td, fd, cap_rights_init(&rights, CAP_READ), &fp);
3954 if ((fp->f_flag & FREAD) == 0) {
3959 foffset = foffset_lock(fp, 0);
3961 if (vp->v_type != VDIR) {
3965 aiov.iov_base = buf;
3966 aiov.iov_len = count;
3967 auio.uio_iov = &aiov;
3968 auio.uio_iovcnt = 1;
3969 auio.uio_rw = UIO_READ;
3970 auio.uio_segflg = bufseg;
3972 vn_lock(vp, LK_SHARED | LK_RETRY);
3973 AUDIT_ARG_VNODE1(vp);
3974 loff = auio.uio_offset = foffset;
3976 error = mac_vnode_check_readdir(td->td_ucred, vp);
3979 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL,
3981 foffset = auio.uio_offset;
3986 if (count == auio.uio_resid &&
3987 (vp->v_vflag & VV_ROOT) &&
3988 (vp->v_mount->mnt_flag & MNT_UNION)) {
3989 struct vnode *tvp = vp;
3991 vp = vp->v_mount->mnt_vnodecovered;
4002 *residp = auio.uio_resid;
4003 td->td_retval[0] = count - auio.uio_resid;
4005 foffset_unlock(fp, foffset, 0);
4010 #ifndef _SYS_SYSPROTO_H_
4011 struct getdents_args {
4018 sys_getdents(td, uap)
4020 register struct getdents_args /* {
4026 struct getdirentries_args ap;
4030 ap.count = uap->count;
4032 return (sys_getdirentries(td, &ap));
4036 * Set the mode mask for creation of filesystem nodes.
4038 #ifndef _SYS_SYSPROTO_H_
4046 struct umask_args /* {
4050 struct filedesc *fdp;
4052 fdp = td->td_proc->p_fd;
4053 FILEDESC_XLOCK(fdp);
4054 td->td_retval[0] = fdp->fd_cmask;
4055 fdp->fd_cmask = uap->newmask & ALLPERMS;
4056 FILEDESC_XUNLOCK(fdp);
4061 * Void all references to file by ripping underlying filesystem away from
4064 #ifndef _SYS_SYSPROTO_H_
4065 struct revoke_args {
4072 register struct revoke_args /* {
4078 struct nameidata nd;
4081 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
4083 if ((error = namei(&nd)) != 0)
4086 NDFREE(&nd, NDF_ONLY_PNBUF);
4087 if (vp->v_type != VCHR || vp->v_rdev == NULL) {
4092 error = mac_vnode_check_revoke(td->td_ucred, vp);
4096 error = VOP_GETATTR(vp, &vattr, td->td_ucred);
4099 if (td->td_ucred->cr_uid != vattr.va_uid) {
4100 error = priv_check(td, PRIV_VFS_ADMIN);
4105 VOP_REVOKE(vp, REVOKEALL);
4112 * Convert a user file descriptor to a kernel file entry and check that, if it
4113 * is a capability, the correct rights are present. A reference on the file
4114 * entry is held upon returning.
4117 getvnode(struct thread *td, int fd, cap_rights_t *rightsp, struct file **fpp)
4122 error = fget_unlocked(td->td_proc->p_fd, fd, rightsp, &fp, NULL);
4127 * The file could be not of the vnode type, or it may be not
4128 * yet fully initialized, in which case the f_vnode pointer
4129 * may be set, but f_ops is still badfileops. E.g.,
4130 * devfs_open() transiently create such situation to
4131 * facilitate csw d_fdopen().
4133 * Dupfdopen() handling in kern_openat() installs the
4134 * half-baked file into the process descriptor table, allowing
4135 * other thread to dereference it. Guard against the race by
4138 if (fp->f_vnode == NULL || fp->f_ops == &badfileops) {
4148 * Get an (NFS) file handle.
4150 #ifndef _SYS_SYSPROTO_H_
4151 struct lgetfh_args {
4159 register struct lgetfh_args *uap;
4161 struct nameidata nd;
4163 register struct vnode *vp;
4166 error = priv_check(td, PRIV_VFS_GETFH);
4169 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
4174 NDFREE(&nd, NDF_ONLY_PNBUF);
4176 bzero(&fh, sizeof(fh));
4177 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
4178 error = VOP_VPTOFH(vp, &fh.fh_fid);
4181 error = copyout(&fh, uap->fhp, sizeof (fh));
4185 #ifndef _SYS_SYSPROTO_H_
4194 register struct getfh_args *uap;
4196 struct nameidata nd;
4198 register struct vnode *vp;
4201 error = priv_check(td, PRIV_VFS_GETFH);
4204 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
4209 NDFREE(&nd, NDF_ONLY_PNBUF);
4211 bzero(&fh, sizeof(fh));
4212 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
4213 error = VOP_VPTOFH(vp, &fh.fh_fid);
4216 error = copyout(&fh, uap->fhp, sizeof (fh));
4221 * syscall for the rpc.lockd to use to translate a NFS file handle into an
4224 * warning: do not remove the priv_check() call or this becomes one giant
4227 #ifndef _SYS_SYSPROTO_H_
4228 struct fhopen_args {
4229 const struct fhandle *u_fhp;
4236 struct fhopen_args /* {
4237 const struct fhandle *u_fhp;
4248 error = priv_check(td, PRIV_VFS_FHOPEN);
4252 fmode = FFLAGS(uap->flags);
4253 /* why not allow a non-read/write open for our lockd? */
4254 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
4256 error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
4259 /* find the mount point */
4260 mp = vfs_busyfs(&fhp.fh_fsid);
4263 /* now give me my vnode, it gets returned to me locked */
4264 error = VFS_FHTOVP(mp, &fhp.fh_fid, LK_EXCLUSIVE, &vp);
4269 error = falloc_noinstall(td, &fp);
4275 * An extra reference on `fp' has been held for us by
4276 * falloc_noinstall().
4282 error = vn_open_vnode(vp, fmode, td->td_ucred, td, fp);
4284 KASSERT(fp->f_ops == &badfileops,
4285 ("VOP_OPEN in fhopen() set f_ops"));
4286 KASSERT(td->td_dupfd < 0,
4287 ("fhopen() encountered fdopen()"));
4297 finit(fp, (fmode & FMASK) | (fp->f_flag & FHASLOCK), DTYPE_VNODE, vp,
4300 if ((fmode & O_TRUNC) != 0) {
4301 error = fo_truncate(fp, 0, td->td_ucred, td);
4306 error = finstall(td, fp, &indx, fmode, NULL);
4309 td->td_retval[0] = indx;
4314 * Stat an (NFS) file handle.
4316 #ifndef _SYS_SYSPROTO_H_
4317 struct fhstat_args {
4318 struct fhandle *u_fhp;
4325 register struct fhstat_args /* {
4326 struct fhandle *u_fhp;
4334 error = copyin(uap->u_fhp, &fh, sizeof(fh));
4337 error = kern_fhstat(td, fh, &sb);
4339 error = copyout(&sb, uap->sb, sizeof(sb));
4344 kern_fhstat(struct thread *td, struct fhandle fh, struct stat *sb)
4350 error = priv_check(td, PRIV_VFS_FHSTAT);
4353 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4355 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_EXCLUSIVE, &vp);
4359 error = vn_stat(vp, sb, td->td_ucred, NOCRED, td);
4365 * Implement fstatfs() for (NFS) file handles.
4367 #ifndef _SYS_SYSPROTO_H_
4368 struct fhstatfs_args {
4369 struct fhandle *u_fhp;
4374 sys_fhstatfs(td, uap)
4376 struct fhstatfs_args /* {
4377 struct fhandle *u_fhp;
4385 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4388 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
4389 error = kern_fhstatfs(td, fh, sfp);
4391 error = copyout(sfp, uap->buf, sizeof(*sfp));
4392 free(sfp, M_STATFS);
4397 kern_fhstatfs(struct thread *td, fhandle_t fh, struct statfs *buf)
4404 error = priv_check(td, PRIV_VFS_FHSTATFS);
4407 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4409 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_EXCLUSIVE, &vp);
4415 error = prison_canseemount(td->td_ucred, mp);
4419 error = mac_mount_check_stat(td->td_ucred, mp);
4424 * Set these in case the underlying filesystem fails to do so.
4427 sp->f_version = STATFS_VERSION;
4428 sp->f_namemax = NAME_MAX;
4429 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
4430 error = VFS_STATFS(mp, sp);
4439 kern_posix_fallocate(struct thread *td, int fd, off_t offset, off_t len)
4444 cap_rights_t rights;
4445 off_t olen, ooffset;
4448 if (offset < 0 || len <= 0)
4450 /* Check for wrap. */
4451 if (offset > OFF_MAX - len)
4453 error = fget(td, fd, cap_rights_init(&rights, CAP_WRITE), &fp);
4456 if ((fp->f_ops->fo_flags & DFLAG_SEEKABLE) == 0) {
4460 if ((fp->f_flag & FWRITE) == 0) {
4464 if (fp->f_type != DTYPE_VNODE) {
4469 if (vp->v_type != VREG) {
4474 /* Allocating blocks may take a long time, so iterate. */
4481 error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
4484 error = vn_lock(vp, LK_EXCLUSIVE);
4486 vn_finished_write(mp);
4490 error = mac_vnode_check_write(td->td_ucred, fp->f_cred, vp);
4493 error = VOP_ALLOCATE(vp, &offset, &len);
4495 vn_finished_write(mp);
4497 if (olen + ooffset != offset + len) {
4498 panic("offset + len changed from %jx/%jx to %jx/%jx",
4499 ooffset, olen, offset, len);
4501 if (error != 0 || len == 0)
4503 KASSERT(olen > len, ("Iteration did not make progress?"));
4512 sys_posix_fallocate(struct thread *td, struct posix_fallocate_args *uap)
4516 error = kern_posix_fallocate(td, uap->fd, uap->offset, uap->len);
4517 return (kern_posix_error(td, error));
4521 * Unlike madvise(2), we do not make a best effort to remember every
4522 * possible caching hint. Instead, we remember the last setting with
4523 * the exception that we will allow POSIX_FADV_NORMAL to adjust the
4524 * region of any current setting.
4527 kern_posix_fadvise(struct thread *td, int fd, off_t offset, off_t len,
4530 struct fadvise_info *fa, *new;
4533 cap_rights_t rights;
4537 if (offset < 0 || len < 0 || offset > OFF_MAX - len)
4540 case POSIX_FADV_SEQUENTIAL:
4541 case POSIX_FADV_RANDOM:
4542 case POSIX_FADV_NOREUSE:
4543 new = malloc(sizeof(*fa), M_FADVISE, M_WAITOK);
4545 case POSIX_FADV_NORMAL:
4546 case POSIX_FADV_WILLNEED:
4547 case POSIX_FADV_DONTNEED:
4553 /* XXX: CAP_POSIX_FADVISE? */
4554 error = fget(td, fd, cap_rights_init(&rights), &fp);
4557 if ((fp->f_ops->fo_flags & DFLAG_SEEKABLE) == 0) {
4561 if (fp->f_type != DTYPE_VNODE) {
4566 if (vp->v_type != VREG) {
4573 end = offset + len - 1;
4575 case POSIX_FADV_SEQUENTIAL:
4576 case POSIX_FADV_RANDOM:
4577 case POSIX_FADV_NOREUSE:
4579 * Try to merge any existing non-standard region with
4580 * this new region if possible, otherwise create a new
4581 * non-standard region for this request.
4583 mtx_pool_lock(mtxpool_sleep, fp);
4585 if (fa != NULL && fa->fa_advice == advice &&
4586 ((fa->fa_start <= end && fa->fa_end >= offset) ||
4587 (end != OFF_MAX && fa->fa_start == end + 1) ||
4588 (fa->fa_end != OFF_MAX && fa->fa_end + 1 == offset))) {
4589 if (offset < fa->fa_start)
4590 fa->fa_start = offset;
4591 if (end > fa->fa_end)
4594 new->fa_advice = advice;
4595 new->fa_start = offset;
4600 mtx_pool_unlock(mtxpool_sleep, fp);
4602 case POSIX_FADV_NORMAL:
4604 * If a the "normal" region overlaps with an existing
4605 * non-standard region, trim or remove the
4606 * non-standard region.
4608 mtx_pool_lock(mtxpool_sleep, fp);
4611 if (offset <= fa->fa_start && end >= fa->fa_end) {
4613 fp->f_advice = NULL;
4614 } else if (offset <= fa->fa_start &&
4615 end >= fa->fa_start)
4616 fa->fa_start = end + 1;
4617 else if (offset <= fa->fa_end && end >= fa->fa_end)
4618 fa->fa_end = offset - 1;
4619 else if (offset >= fa->fa_start && end <= fa->fa_end) {
4621 * If the "normal" region is a middle
4622 * portion of the existing
4623 * non-standard region, just remove
4624 * the whole thing rather than picking
4625 * one side or the other to
4629 fp->f_advice = NULL;
4632 mtx_pool_unlock(mtxpool_sleep, fp);
4634 case POSIX_FADV_WILLNEED:
4635 case POSIX_FADV_DONTNEED:
4636 error = VOP_ADVISE(vp, offset, end, advice);
4642 free(new, M_FADVISE);
4647 sys_posix_fadvise(struct thread *td, struct posix_fadvise_args *uap)
4651 error = kern_posix_fadvise(td, uap->fd, uap->offset, uap->len,
4653 return (kern_posix_error(td, error));