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 * The module initialization routine for POSIX asynchronous I/O will
110 * set this to the version of AIO that it implements. (Zero means
111 * that it is not implemented.) This value is used here by pathconf()
112 * and in kern_descrip.c by fpathconf().
114 int async_io_version;
117 * Sync each mounted filesystem.
119 #ifndef _SYS_SYSPROTO_H_
128 struct sync_args *uap;
130 struct mount *mp, *nmp;
133 mtx_lock(&mountlist_mtx);
134 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
135 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK)) {
136 nmp = TAILQ_NEXT(mp, mnt_list);
139 if ((mp->mnt_flag & MNT_RDONLY) == 0 &&
140 vn_start_write(NULL, &mp, V_NOWAIT) == 0) {
141 save = curthread_pflags_set(TDP_SYNCIO);
142 vfs_msync(mp, MNT_NOWAIT);
143 VFS_SYNC(mp, MNT_NOWAIT);
144 curthread_pflags_restore(save);
145 vn_finished_write(mp);
147 mtx_lock(&mountlist_mtx);
148 nmp = TAILQ_NEXT(mp, mnt_list);
151 mtx_unlock(&mountlist_mtx);
156 * Change filesystem quotas.
158 #ifndef _SYS_SYSPROTO_H_
159 struct quotactl_args {
167 sys_quotactl(td, uap)
169 register struct quotactl_args /* {
180 AUDIT_ARG_CMD(uap->cmd);
181 AUDIT_ARG_UID(uap->uid);
182 if (!prison_allow(td->td_ucred, PR_ALLOW_QUOTAS))
184 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
186 if ((error = namei(&nd)) != 0)
188 NDFREE(&nd, NDF_ONLY_PNBUF);
189 mp = nd.ni_vp->v_mount;
192 error = vfs_busy(mp, 0);
196 error = VFS_QUOTACTL(mp, uap->cmd, uap->uid, uap->arg);
199 * Since quota on operation typically needs to open quota
200 * file, the Q_QUOTAON handler needs to unbusy the mount point
201 * before calling into namei. Otherwise, unmount might be
202 * started between two vfs_busy() invocations (first is our,
203 * second is from mount point cross-walk code in lookup()),
206 * Require that Q_QUOTAON handles the vfs_busy() reference on
207 * its own, always returning with ubusied mount point.
209 if ((uap->cmd >> SUBCMDSHIFT) != Q_QUOTAON)
215 * Used by statfs conversion routines to scale the block size up if
216 * necessary so that all of the block counts are <= 'max_size'. Note
217 * that 'max_size' should be a bitmask, i.e. 2^n - 1 for some non-zero
221 statfs_scale_blocks(struct statfs *sf, long max_size)
226 KASSERT(powerof2(max_size + 1), ("%s: invalid max_size", __func__));
229 * Attempt to scale the block counts to give a more accurate
230 * overview to userland of the ratio of free space to used
231 * space. To do this, find the largest block count and compute
232 * a divisor that lets it fit into a signed integer <= max_size.
234 if (sf->f_bavail < 0)
235 count = -sf->f_bavail;
237 count = sf->f_bavail;
238 count = MAX(sf->f_blocks, MAX(sf->f_bfree, count));
239 if (count <= max_size)
242 count >>= flsl(max_size);
249 sf->f_bsize <<= shift;
250 sf->f_blocks >>= shift;
251 sf->f_bfree >>= shift;
252 sf->f_bavail >>= shift;
256 * Get filesystem statistics.
258 #ifndef _SYS_SYSPROTO_H_
267 register struct statfs_args /* {
275 error = kern_statfs(td, uap->path, UIO_USERSPACE, &sf);
277 error = copyout(&sf, uap->buf, sizeof(sf));
282 kern_statfs(struct thread *td, char *path, enum uio_seg pathseg,
286 struct statfs *sp, sb;
290 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
295 mp = nd.ni_vp->v_mount;
297 NDFREE(&nd, NDF_ONLY_PNBUF);
299 error = vfs_busy(mp, 0);
304 error = mac_mount_check_stat(td->td_ucred, mp);
309 * Set these in case the underlying filesystem fails to do so.
312 sp->f_version = STATFS_VERSION;
313 sp->f_namemax = NAME_MAX;
314 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
315 error = VFS_STATFS(mp, sp);
318 if (priv_check(td, PRIV_VFS_GENERATION)) {
319 bcopy(sp, &sb, sizeof(sb));
320 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
321 prison_enforce_statfs(td->td_ucred, mp, &sb);
331 * Get filesystem statistics.
333 #ifndef _SYS_SYSPROTO_H_
334 struct fstatfs_args {
342 register struct fstatfs_args /* {
350 error = kern_fstatfs(td, uap->fd, &sf);
352 error = copyout(&sf, uap->buf, sizeof(sf));
357 kern_fstatfs(struct thread *td, int fd, struct statfs *buf)
361 struct statfs *sp, sb;
367 error = getvnode(td, fd, cap_rights_init(&rights, CAP_FSTATFS), &fp);
371 vn_lock(vp, LK_SHARED | LK_RETRY);
373 AUDIT_ARG_VNODE1(vp);
384 error = vfs_busy(mp, 0);
389 error = mac_mount_check_stat(td->td_ucred, mp);
394 * Set these in case the underlying filesystem fails to do so.
397 sp->f_version = STATFS_VERSION;
398 sp->f_namemax = NAME_MAX;
399 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
400 error = VFS_STATFS(mp, sp);
403 if (priv_check(td, PRIV_VFS_GENERATION)) {
404 bcopy(sp, &sb, sizeof(sb));
405 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
406 prison_enforce_statfs(td->td_ucred, mp, &sb);
417 * Get statistics on all filesystems.
419 #ifndef _SYS_SYSPROTO_H_
420 struct getfsstat_args {
427 sys_getfsstat(td, uap)
429 register struct getfsstat_args /* {
438 if (uap->bufsize < 0 || uap->bufsize > SIZE_MAX)
440 error = kern_getfsstat(td, &uap->buf, uap->bufsize, &count,
441 UIO_USERSPACE, uap->flags);
443 td->td_retval[0] = count;
448 * If (bufsize > 0 && bufseg == UIO_SYSSPACE)
449 * The caller is responsible for freeing memory which will be allocated
453 kern_getfsstat(struct thread *td, struct statfs **buf, size_t bufsize,
454 size_t *countp, enum uio_seg bufseg, int flags)
456 struct mount *mp, *nmp;
457 struct statfs *sfsp, *sp, sb;
458 size_t count, maxcount;
461 maxcount = bufsize / sizeof(struct statfs);
464 else if (bufseg == UIO_USERSPACE)
466 else /* if (bufseg == UIO_SYSSPACE) */ {
468 mtx_lock(&mountlist_mtx);
469 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
472 mtx_unlock(&mountlist_mtx);
473 if (maxcount > count)
475 sfsp = *buf = malloc(maxcount * sizeof(struct statfs), M_TEMP,
479 mtx_lock(&mountlist_mtx);
480 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
481 if (prison_canseemount(td->td_ucred, mp) != 0) {
482 nmp = TAILQ_NEXT(mp, mnt_list);
486 if (mac_mount_check_stat(td->td_ucred, mp) != 0) {
487 nmp = TAILQ_NEXT(mp, mnt_list);
491 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK)) {
492 nmp = TAILQ_NEXT(mp, mnt_list);
495 if (sfsp && count < maxcount) {
498 * Set these in case the underlying filesystem
501 sp->f_version = STATFS_VERSION;
502 sp->f_namemax = NAME_MAX;
503 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
505 * If MNT_NOWAIT or MNT_LAZY is specified, do not
506 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
507 * overrides MNT_WAIT.
509 if (((flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
510 (flags & MNT_WAIT)) &&
511 (error = VFS_STATFS(mp, sp))) {
512 mtx_lock(&mountlist_mtx);
513 nmp = TAILQ_NEXT(mp, mnt_list);
517 if (priv_check(td, PRIV_VFS_GENERATION)) {
518 bcopy(sp, &sb, sizeof(sb));
519 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
520 prison_enforce_statfs(td->td_ucred, mp, &sb);
523 if (bufseg == UIO_SYSSPACE)
524 bcopy(sp, sfsp, sizeof(*sp));
525 else /* if (bufseg == UIO_USERSPACE) */ {
526 error = copyout(sp, sfsp, sizeof(*sp));
535 mtx_lock(&mountlist_mtx);
536 nmp = TAILQ_NEXT(mp, mnt_list);
539 mtx_unlock(&mountlist_mtx);
540 if (sfsp && 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 error = kern_statfs(td, uap->path, UIO_USERSPACE, &sf);
574 cvtstatfs(&sf, &osb);
575 return (copyout(&osb, uap->buf, sizeof(osb)));
579 * Get filesystem statistics.
581 #ifndef _SYS_SYSPROTO_H_
582 struct freebsd4_fstatfs_args {
588 freebsd4_fstatfs(td, uap)
590 struct freebsd4_fstatfs_args /* {
599 error = kern_fstatfs(td, uap->fd, &sf);
602 cvtstatfs(&sf, &osb);
603 return (copyout(&osb, uap->buf, sizeof(osb)));
607 * Get statistics on all filesystems.
609 #ifndef _SYS_SYSPROTO_H_
610 struct freebsd4_getfsstat_args {
617 freebsd4_getfsstat(td, uap)
619 register struct freebsd4_getfsstat_args /* {
625 struct statfs *buf, *sp;
630 if (uap->bufsize < 0)
632 count = uap->bufsize / sizeof(struct ostatfs);
633 if (count > SIZE_MAX / sizeof(struct statfs))
635 size = count * sizeof(struct statfs);
636 error = kern_getfsstat(td, &buf, size, &count, UIO_SYSSPACE,
638 td->td_retval[0] = count;
641 while (count != 0 && error == 0) {
643 error = copyout(&osb, uap->buf, sizeof(osb));
654 * Implement fstatfs() for (NFS) file handles.
656 #ifndef _SYS_SYSPROTO_H_
657 struct freebsd4_fhstatfs_args {
658 struct fhandle *u_fhp;
663 freebsd4_fhstatfs(td, uap)
665 struct freebsd4_fhstatfs_args /* {
666 struct fhandle *u_fhp;
675 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
678 error = kern_fhstatfs(td, fh, &sf);
681 cvtstatfs(&sf, &osb);
682 return (copyout(&osb, uap->buf, sizeof(osb)));
686 * Convert a new format statfs structure to an old format statfs structure.
694 statfs_scale_blocks(nsp, LONG_MAX);
695 bzero(osp, sizeof(*osp));
696 osp->f_bsize = nsp->f_bsize;
697 osp->f_iosize = MIN(nsp->f_iosize, LONG_MAX);
698 osp->f_blocks = nsp->f_blocks;
699 osp->f_bfree = nsp->f_bfree;
700 osp->f_bavail = nsp->f_bavail;
701 osp->f_files = MIN(nsp->f_files, LONG_MAX);
702 osp->f_ffree = MIN(nsp->f_ffree, LONG_MAX);
703 osp->f_owner = nsp->f_owner;
704 osp->f_type = nsp->f_type;
705 osp->f_flags = nsp->f_flags;
706 osp->f_syncwrites = MIN(nsp->f_syncwrites, LONG_MAX);
707 osp->f_asyncwrites = MIN(nsp->f_asyncwrites, LONG_MAX);
708 osp->f_syncreads = MIN(nsp->f_syncreads, LONG_MAX);
709 osp->f_asyncreads = MIN(nsp->f_asyncreads, LONG_MAX);
710 strlcpy(osp->f_fstypename, nsp->f_fstypename,
711 MIN(MFSNAMELEN, OMFSNAMELEN));
712 strlcpy(osp->f_mntonname, nsp->f_mntonname,
713 MIN(MNAMELEN, OMNAMELEN));
714 strlcpy(osp->f_mntfromname, nsp->f_mntfromname,
715 MIN(MNAMELEN, OMNAMELEN));
716 osp->f_fsid = nsp->f_fsid;
718 #endif /* COMPAT_FREEBSD4 */
721 * Change current working directory to a given file descriptor.
723 #ifndef _SYS_SYSPROTO_H_
731 struct fchdir_args /* {
735 struct vnode *vp, *tdp;
741 AUDIT_ARG_FD(uap->fd);
742 error = getvnode(td, uap->fd, cap_rights_init(&rights, CAP_FCHDIR),
749 vn_lock(vp, LK_SHARED | LK_RETRY);
750 AUDIT_ARG_VNODE1(vp);
751 error = change_dir(vp, td);
752 while (!error && (mp = vp->v_mountedhere) != NULL) {
755 error = VFS_ROOT(mp, LK_SHARED, &tdp);
772 * Change current working directory (``.'').
774 #ifndef _SYS_SYSPROTO_H_
782 struct chdir_args /* {
787 return (kern_chdir(td, uap->path, UIO_USERSPACE));
791 kern_chdir(struct thread *td, char *path, enum uio_seg pathseg)
796 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
798 if ((error = namei(&nd)) != 0)
800 if ((error = change_dir(nd.ni_vp, td)) != 0) {
802 NDFREE(&nd, NDF_ONLY_PNBUF);
805 VOP_UNLOCK(nd.ni_vp, 0);
806 NDFREE(&nd, NDF_ONLY_PNBUF);
807 pwd_chdir(td, nd.ni_vp);
812 * Change notion of root (``/'') directory.
814 #ifndef _SYS_SYSPROTO_H_
822 struct chroot_args /* {
829 error = priv_check(td, PRIV_VFS_CHROOT);
832 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
833 UIO_USERSPACE, uap->path, td);
837 error = change_dir(nd.ni_vp, td);
841 error = mac_vnode_check_chroot(td->td_ucred, nd.ni_vp);
845 VOP_UNLOCK(nd.ni_vp, 0);
846 error = pwd_chroot(td, nd.ni_vp);
848 NDFREE(&nd, NDF_ONLY_PNBUF);
853 NDFREE(&nd, NDF_ONLY_PNBUF);
858 * Common routine for chroot and chdir. Callers must provide a locked vnode
870 ASSERT_VOP_LOCKED(vp, "change_dir(): vp not locked");
871 if (vp->v_type != VDIR)
874 error = mac_vnode_check_chdir(td->td_ucred, vp);
878 return (VOP_ACCESS(vp, VEXEC, td->td_ucred, td));
882 flags_to_rights(int flags, cap_rights_t *rightsp)
885 if (flags & O_EXEC) {
886 cap_rights_set(rightsp, CAP_FEXECVE);
888 switch ((flags & O_ACCMODE)) {
890 cap_rights_set(rightsp, CAP_READ);
893 cap_rights_set(rightsp, CAP_READ);
896 cap_rights_set(rightsp, CAP_WRITE);
897 if (!(flags & (O_APPEND | O_TRUNC)))
898 cap_rights_set(rightsp, CAP_SEEK);
904 cap_rights_set(rightsp, CAP_CREATE);
907 cap_rights_set(rightsp, CAP_FTRUNCATE);
909 if (flags & (O_SYNC | O_FSYNC))
910 cap_rights_set(rightsp, CAP_FSYNC);
912 if (flags & (O_EXLOCK | O_SHLOCK))
913 cap_rights_set(rightsp, CAP_FLOCK);
917 * Check permissions, allocate an open file structure, and call the device
918 * open routine if any.
920 #ifndef _SYS_SYSPROTO_H_
930 register struct open_args /* {
937 return (kern_openat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
938 uap->flags, uap->mode));
941 #ifndef _SYS_SYSPROTO_H_
950 sys_openat(struct thread *td, struct openat_args *uap)
953 return (kern_openat(td, uap->fd, uap->path, UIO_USERSPACE, uap->flag,
958 kern_openat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
961 struct proc *p = td->td_proc;
962 struct filedesc *fdp = p->p_fd;
967 int cmode, error, indx;
971 AUDIT_ARG_FFLAGS(flags);
972 AUDIT_ARG_MODE(mode);
973 /* XXX: audit dirfd */
974 cap_rights_init(&rights, CAP_LOOKUP);
975 flags_to_rights(flags, &rights);
977 * Only one of the O_EXEC, O_RDONLY, O_WRONLY and O_RDWR flags
980 if (flags & O_EXEC) {
981 if (flags & O_ACCMODE)
983 } else if ((flags & O_ACCMODE) == O_ACCMODE) {
986 flags = FFLAGS(flags);
990 * Allocate the file descriptor, but don't install a descriptor yet.
992 error = falloc_noinstall(td, &fp);
996 * An extra reference on `fp' has been held for us by
997 * falloc_noinstall().
999 /* Set the flags early so the finit in devfs can pick them up. */
1000 fp->f_flag = flags & FMASK;
1001 cmode = ((mode & ~fdp->fd_cmask) & ALLPERMS) & ~S_ISTXT;
1002 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, fd,
1004 td->td_dupfd = -1; /* XXX check for fdopen */
1005 error = vn_open(&nd, &flags, cmode, fp);
1008 * If the vn_open replaced the method vector, something
1009 * wonderous happened deep below and we just pass it up
1010 * pretending we know what we do.
1012 if (error == ENXIO && fp->f_ops != &badfileops)
1016 * Handle special fdopen() case. bleh.
1018 * Don't do this for relative (capability) lookups; we don't
1019 * understand exactly what would happen, and we don't think
1020 * that it ever should.
1022 if (nd.ni_strictrelative == 0 &&
1023 (error == ENODEV || error == ENXIO) &&
1024 td->td_dupfd >= 0) {
1025 error = dupfdopen(td, fdp, td->td_dupfd, flags, error,
1034 NDFREE(&nd, NDF_ONLY_PNBUF);
1038 * Store the vnode, for any f_type. Typically, the vnode use
1039 * count is decremented by direct call to vn_closefile() for
1040 * files that switched type in the cdevsw fdopen() method.
1044 * If the file wasn't claimed by devfs bind it to the normal
1045 * vnode operations here.
1047 if (fp->f_ops == &badfileops) {
1048 KASSERT(vp->v_type != VFIFO, ("Unexpected fifo."));
1050 finit(fp, (flags & FMASK) | (fp->f_flag & FHASLOCK),
1051 DTYPE_VNODE, vp, &vnops);
1055 if (flags & O_TRUNC) {
1056 error = fo_truncate(fp, 0, td->td_ucred, td);
1062 * If we haven't already installed the FD (for dupfdopen), do so now.
1065 struct filecaps *fcaps;
1068 if (nd.ni_strictrelative == 1)
1069 fcaps = &nd.ni_filecaps;
1073 error = finstall(td, fp, &indx, flags, fcaps);
1074 /* On success finstall() consumes fcaps. */
1076 filecaps_free(&nd.ni_filecaps);
1080 filecaps_free(&nd.ni_filecaps);
1084 * Release our private reference, leaving the one associated with
1085 * the descriptor table intact.
1088 td->td_retval[0] = indx;
1091 KASSERT(indx == -1, ("indx=%d, should be -1", indx));
1100 #ifndef _SYS_SYSPROTO_H_
1101 struct ocreat_args {
1109 register struct ocreat_args /* {
1115 return (kern_openat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1116 O_WRONLY | O_CREAT | O_TRUNC, uap->mode));
1118 #endif /* COMPAT_43 */
1121 * Create a special file.
1123 #ifndef _SYS_SYSPROTO_H_
1133 register struct mknod_args /* {
1140 return (kern_mknodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1141 uap->mode, uap->dev));
1144 #ifndef _SYS_SYSPROTO_H_
1145 struct mknodat_args {
1153 sys_mknodat(struct thread *td, struct mknodat_args *uap)
1156 return (kern_mknodat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode,
1161 kern_mknodat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1167 struct nameidata nd;
1168 cap_rights_t rights;
1169 int error, whiteout = 0;
1171 AUDIT_ARG_MODE(mode);
1173 switch (mode & S_IFMT) {
1176 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
1179 error = priv_check(td, PRIV_VFS_MKNOD_BAD);
1182 error = priv_check(td, PRIV_VFS_MKNOD_WHT);
1186 return (kern_mkfifoat(td, fd, path, pathseg, mode));
1196 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
1197 NOCACHE, pathseg, path, fd, cap_rights_init(&rights, CAP_MKNODAT),
1199 if ((error = namei(&nd)) != 0)
1203 NDFREE(&nd, NDF_ONLY_PNBUF);
1204 if (vp == nd.ni_dvp)
1212 vattr.va_mode = (mode & ALLPERMS) &
1213 ~td->td_proc->p_fd->fd_cmask;
1214 vattr.va_rdev = dev;
1217 switch (mode & S_IFMT) {
1218 case S_IFMT: /* used by badsect to flag bad sectors */
1219 vattr.va_type = VBAD;
1222 vattr.va_type = VCHR;
1225 vattr.va_type = VBLK;
1231 panic("kern_mknod: invalid mode");
1234 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1235 NDFREE(&nd, NDF_ONLY_PNBUF);
1237 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1242 if (error == 0 && !whiteout)
1243 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp,
1244 &nd.ni_cnd, &vattr);
1248 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, CREATE);
1250 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp,
1251 &nd.ni_cnd, &vattr);
1256 NDFREE(&nd, NDF_ONLY_PNBUF);
1258 vn_finished_write(mp);
1263 * Create a named pipe.
1265 #ifndef _SYS_SYSPROTO_H_
1266 struct mkfifo_args {
1274 register struct mkfifo_args /* {
1280 return (kern_mkfifoat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1284 #ifndef _SYS_SYSPROTO_H_
1285 struct mkfifoat_args {
1292 sys_mkfifoat(struct thread *td, struct mkfifoat_args *uap)
1295 return (kern_mkfifoat(td, uap->fd, uap->path, UIO_USERSPACE,
1300 kern_mkfifoat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1305 struct nameidata nd;
1306 cap_rights_t rights;
1309 AUDIT_ARG_MODE(mode);
1312 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
1313 NOCACHE, pathseg, path, fd, cap_rights_init(&rights, CAP_MKFIFOAT),
1315 if ((error = namei(&nd)) != 0)
1317 if (nd.ni_vp != NULL) {
1318 NDFREE(&nd, NDF_ONLY_PNBUF);
1319 if (nd.ni_vp == nd.ni_dvp)
1326 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1327 NDFREE(&nd, NDF_ONLY_PNBUF);
1329 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1334 vattr.va_type = VFIFO;
1335 vattr.va_mode = (mode & ALLPERMS) & ~td->td_proc->p_fd->fd_cmask;
1337 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1342 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
1349 vn_finished_write(mp);
1350 NDFREE(&nd, NDF_ONLY_PNBUF);
1355 * Make a hard file link.
1357 #ifndef _SYS_SYSPROTO_H_
1366 register struct link_args /* {
1372 return (kern_linkat(td, AT_FDCWD, AT_FDCWD, uap->path, uap->link,
1373 UIO_USERSPACE, FOLLOW));
1376 #ifndef _SYS_SYSPROTO_H_
1377 struct linkat_args {
1386 sys_linkat(struct thread *td, struct linkat_args *uap)
1391 if (flag & ~AT_SYMLINK_FOLLOW)
1394 return (kern_linkat(td, uap->fd1, uap->fd2, uap->path1, uap->path2,
1395 UIO_USERSPACE, (flag & AT_SYMLINK_FOLLOW) ? FOLLOW : NOFOLLOW));
1398 int hardlink_check_uid = 0;
1399 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
1400 &hardlink_check_uid, 0,
1401 "Unprivileged processes cannot create hard links to files owned by other "
1403 static int hardlink_check_gid = 0;
1404 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
1405 &hardlink_check_gid, 0,
1406 "Unprivileged processes cannot create hard links to files owned by other "
1410 can_hardlink(struct vnode *vp, struct ucred *cred)
1415 if (!hardlink_check_uid && !hardlink_check_gid)
1418 error = VOP_GETATTR(vp, &va, cred);
1422 if (hardlink_check_uid && cred->cr_uid != va.va_uid) {
1423 error = priv_check_cred(cred, PRIV_VFS_LINK, 0);
1428 if (hardlink_check_gid && !groupmember(va.va_gid, cred)) {
1429 error = priv_check_cred(cred, PRIV_VFS_LINK, 0);
1438 kern_linkat(struct thread *td, int fd1, int fd2, char *path1, char *path2,
1439 enum uio_seg segflg, int follow)
1443 struct nameidata nd;
1444 cap_rights_t rights;
1449 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, segflg, path1, fd1,
1450 cap_rights_init(&rights, CAP_LINKAT_SOURCE), td);
1452 if ((error = namei(&nd)) != 0)
1454 NDFREE(&nd, NDF_ONLY_PNBUF);
1456 if (vp->v_type == VDIR) {
1458 return (EPERM); /* POSIX */
1460 NDINIT_ATRIGHTS(&nd, CREATE,
1461 LOCKPARENT | SAVENAME | AUDITVNODE2 | NOCACHE, segflg, path2, fd2,
1462 cap_rights_init(&rights, CAP_LINKAT_TARGET), td);
1463 if ((error = namei(&nd)) == 0) {
1464 if (nd.ni_vp != NULL) {
1465 NDFREE(&nd, NDF_ONLY_PNBUF);
1466 if (nd.ni_dvp == nd.ni_vp)
1473 } else if (nd.ni_dvp->v_mount != vp->v_mount) {
1475 * Cross-device link. No need to recheck
1476 * vp->v_type, since it cannot change, except
1479 NDFREE(&nd, NDF_ONLY_PNBUF);
1483 } else if ((error = vn_lock(vp, LK_EXCLUSIVE)) == 0) {
1484 error = can_hardlink(vp, td->td_ucred);
1487 error = mac_vnode_check_link(td->td_ucred,
1488 nd.ni_dvp, vp, &nd.ni_cnd);
1493 NDFREE(&nd, NDF_ONLY_PNBUF);
1496 error = vn_start_write(vp, &mp, V_NOWAIT);
1500 NDFREE(&nd, NDF_ONLY_PNBUF);
1501 error = vn_start_write(NULL, &mp,
1507 error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
1510 vn_finished_write(mp);
1511 NDFREE(&nd, NDF_ONLY_PNBUF);
1514 NDFREE(&nd, NDF_ONLY_PNBUF);
1524 * Make a symbolic link.
1526 #ifndef _SYS_SYSPROTO_H_
1527 struct symlink_args {
1533 sys_symlink(td, uap)
1535 register struct symlink_args /* {
1541 return (kern_symlinkat(td, uap->path, AT_FDCWD, uap->link,
1545 #ifndef _SYS_SYSPROTO_H_
1546 struct symlinkat_args {
1553 sys_symlinkat(struct thread *td, struct symlinkat_args *uap)
1556 return (kern_symlinkat(td, uap->path1, uap->fd, uap->path2,
1561 kern_symlinkat(struct thread *td, char *path1, int fd, char *path2,
1562 enum uio_seg segflg)
1567 struct nameidata nd;
1569 cap_rights_t rights;
1571 if (segflg == UIO_SYSSPACE) {
1574 syspath = uma_zalloc(namei_zone, M_WAITOK);
1575 if ((error = copyinstr(path1, syspath, MAXPATHLEN, NULL)) != 0)
1578 AUDIT_ARG_TEXT(syspath);
1581 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
1582 NOCACHE, segflg, path2, fd, cap_rights_init(&rights, CAP_SYMLINKAT),
1584 if ((error = namei(&nd)) != 0)
1587 NDFREE(&nd, NDF_ONLY_PNBUF);
1588 if (nd.ni_vp == nd.ni_dvp)
1596 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1597 NDFREE(&nd, NDF_ONLY_PNBUF);
1599 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1604 vattr.va_mode = ACCESSPERMS &~ td->td_proc->p_fd->fd_cmask;
1606 vattr.va_type = VLNK;
1607 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1612 error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr, syspath);
1618 NDFREE(&nd, NDF_ONLY_PNBUF);
1620 vn_finished_write(mp);
1622 if (segflg != UIO_SYSSPACE)
1623 uma_zfree(namei_zone, syspath);
1628 * Delete a whiteout from the filesystem.
1631 sys_undelete(td, uap)
1633 register struct undelete_args /* {
1638 struct nameidata nd;
1643 NDINIT(&nd, DELETE, LOCKPARENT | DOWHITEOUT | AUDITVNODE1,
1644 UIO_USERSPACE, uap->path, td);
1649 if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & ISWHITEOUT)) {
1650 NDFREE(&nd, NDF_ONLY_PNBUF);
1651 if (nd.ni_vp == nd.ni_dvp)
1659 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1660 NDFREE(&nd, NDF_ONLY_PNBUF);
1662 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1666 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, DELETE);
1667 NDFREE(&nd, NDF_ONLY_PNBUF);
1669 vn_finished_write(mp);
1674 * Delete a name from the filesystem.
1676 #ifndef _SYS_SYSPROTO_H_
1677 struct unlink_args {
1684 struct unlink_args /* {
1689 return (kern_unlinkat(td, AT_FDCWD, uap->path, UIO_USERSPACE, 0));
1692 #ifndef _SYS_SYSPROTO_H_
1693 struct unlinkat_args {
1700 sys_unlinkat(struct thread *td, struct unlinkat_args *uap)
1702 int flag = uap->flag;
1704 char *path = uap->path;
1706 if (flag & ~AT_REMOVEDIR)
1709 if (flag & AT_REMOVEDIR)
1710 return (kern_rmdirat(td, fd, path, UIO_USERSPACE));
1712 return (kern_unlinkat(td, fd, path, UIO_USERSPACE, 0));
1716 kern_unlinkat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1721 struct nameidata nd;
1723 cap_rights_t rights;
1728 NDINIT_ATRIGHTS(&nd, DELETE, LOCKPARENT | LOCKLEAF | AUDITVNODE1,
1729 pathseg, path, fd, cap_rights_init(&rights, CAP_UNLINKAT), td);
1730 if ((error = namei(&nd)) != 0)
1731 return (error == EINVAL ? EPERM : error);
1733 if (vp->v_type == VDIR && oldinum == 0) {
1734 error = EPERM; /* POSIX */
1735 } else if (oldinum != 0 &&
1736 ((error = vn_stat(vp, &sb, td->td_ucred, NOCRED, td)) == 0) &&
1737 sb.st_ino != oldinum) {
1738 error = EIDRM; /* Identifier removed */
1741 * The root of a mounted filesystem cannot be deleted.
1743 * XXX: can this only be a VDIR case?
1745 if (vp->v_vflag & VV_ROOT)
1749 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1750 NDFREE(&nd, NDF_ONLY_PNBUF);
1752 if (vp == nd.ni_dvp)
1756 if ((error = vn_start_write(NULL, &mp,
1757 V_XSLEEP | PCATCH)) != 0)
1762 error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp,
1767 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_UNLINK);
1768 error = VOP_REMOVE(nd.ni_dvp, vp, &nd.ni_cnd);
1772 vn_finished_write(mp);
1774 NDFREE(&nd, NDF_ONLY_PNBUF);
1776 if (vp == nd.ni_dvp)
1784 * Reposition read/write file offset.
1786 #ifndef _SYS_SYSPROTO_H_
1797 register struct lseek_args /* {
1805 cap_rights_t rights;
1808 AUDIT_ARG_FD(uap->fd);
1809 error = fget(td, uap->fd, cap_rights_init(&rights, CAP_SEEK), &fp);
1812 error = (fp->f_ops->fo_flags & DFLAG_SEEKABLE) != 0 ?
1813 fo_seek(fp, uap->offset, uap->whence, td) : ESPIPE;
1818 #if defined(COMPAT_43)
1820 * Reposition read/write file offset.
1822 #ifndef _SYS_SYSPROTO_H_
1823 struct olseek_args {
1832 register struct olseek_args /* {
1838 struct lseek_args /* {
1846 nuap.offset = uap->offset;
1847 nuap.whence = uap->whence;
1848 return (sys_lseek(td, &nuap));
1850 #endif /* COMPAT_43 */
1852 #if defined(COMPAT_FREEBSD6)
1853 /* Version with the 'pad' argument */
1855 freebsd6_lseek(td, uap)
1857 register struct freebsd6_lseek_args *uap;
1859 struct lseek_args ouap;
1862 ouap.offset = uap->offset;
1863 ouap.whence = uap->whence;
1864 return (sys_lseek(td, &ouap));
1869 * Check access permissions using passed credentials.
1872 vn_access(vp, user_flags, cred, td)
1881 /* Flags == 0 means only check for existence. */
1882 if (user_flags == 0)
1886 if (user_flags & R_OK)
1888 if (user_flags & W_OK)
1890 if (user_flags & X_OK)
1893 error = mac_vnode_check_access(cred, vp, accmode);
1897 if ((accmode & VWRITE) == 0 || (error = vn_writechk(vp)) == 0)
1898 error = VOP_ACCESS(vp, accmode, cred, td);
1903 * Check access permissions using "real" credentials.
1905 #ifndef _SYS_SYSPROTO_H_
1906 struct access_args {
1914 register struct access_args /* {
1920 return (kern_accessat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1924 #ifndef _SYS_SYSPROTO_H_
1925 struct faccessat_args {
1933 sys_faccessat(struct thread *td, struct faccessat_args *uap)
1936 return (kern_accessat(td, uap->fd, uap->path, UIO_USERSPACE, uap->flag,
1941 kern_accessat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1942 int flag, int amode)
1944 struct ucred *cred, *usecred;
1946 struct nameidata nd;
1947 cap_rights_t rights;
1950 if (flag & ~AT_EACCESS)
1952 if (amode != F_OK && (amode & ~(R_OK | W_OK | X_OK)) != 0)
1956 * Create and modify a temporary credential instead of one that
1957 * is potentially shared (if we need one).
1959 cred = td->td_ucred;
1960 if ((flag & AT_EACCESS) == 0 &&
1961 ((cred->cr_uid != cred->cr_ruid ||
1962 cred->cr_rgid != cred->cr_groups[0]))) {
1963 usecred = crdup(cred);
1964 usecred->cr_uid = cred->cr_ruid;
1965 usecred->cr_groups[0] = cred->cr_rgid;
1966 td->td_ucred = usecred;
1969 AUDIT_ARG_VALUE(amode);
1970 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF |
1971 AUDITVNODE1, pathseg, path, fd, cap_rights_init(&rights, CAP_FSTAT),
1973 if ((error = namei(&nd)) != 0)
1977 error = vn_access(vp, amode, usecred, td);
1978 NDFREE(&nd, NDF_ONLY_PNBUF);
1981 if (usecred != cred) {
1982 td->td_ucred = cred;
1989 * Check access permissions using "effective" credentials.
1991 #ifndef _SYS_SYSPROTO_H_
1992 struct eaccess_args {
1998 sys_eaccess(td, uap)
2000 register struct eaccess_args /* {
2006 return (kern_accessat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2007 AT_EACCESS, uap->amode));
2010 #if defined(COMPAT_43)
2012 * Get file status; this version follows links.
2014 #ifndef _SYS_SYSPROTO_H_
2023 register struct ostat_args /* {
2032 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
2037 return (copyout(&osb, uap->ub, sizeof (osb)));
2041 * Get file status; this version does not follow links.
2043 #ifndef _SYS_SYSPROTO_H_
2044 struct olstat_args {
2052 register struct olstat_args /* {
2061 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2062 UIO_USERSPACE, &sb, NULL);
2066 return (copyout(&osb, uap->ub, sizeof (osb)));
2070 * Convert from an old to a new stat structure.
2078 ost->st_dev = st->st_dev;
2079 ost->st_ino = st->st_ino;
2080 ost->st_mode = st->st_mode;
2081 ost->st_nlink = st->st_nlink;
2082 ost->st_uid = st->st_uid;
2083 ost->st_gid = st->st_gid;
2084 ost->st_rdev = st->st_rdev;
2085 if (st->st_size < (quad_t)1 << 32)
2086 ost->st_size = st->st_size;
2089 ost->st_atim = st->st_atim;
2090 ost->st_mtim = st->st_mtim;
2091 ost->st_ctim = st->st_ctim;
2092 ost->st_blksize = st->st_blksize;
2093 ost->st_blocks = st->st_blocks;
2094 ost->st_flags = st->st_flags;
2095 ost->st_gen = st->st_gen;
2097 #endif /* COMPAT_43 */
2100 * Get file status; this version follows links.
2102 #ifndef _SYS_SYSPROTO_H_
2111 register struct stat_args /* {
2119 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
2122 error = copyout(&sb, uap->ub, sizeof (sb));
2126 #ifndef _SYS_SYSPROTO_H_
2127 struct fstatat_args {
2135 sys_fstatat(struct thread *td, struct fstatat_args *uap)
2140 error = kern_statat(td, uap->flag, uap->fd, uap->path,
2141 UIO_USERSPACE, &sb, NULL);
2143 error = copyout(&sb, uap->buf, sizeof (sb));
2148 kern_statat(struct thread *td, int flag, int fd, char *path,
2149 enum uio_seg pathseg, struct stat *sbp,
2150 void (*hook)(struct vnode *vp, struct stat *sbp))
2152 struct nameidata nd;
2154 cap_rights_t rights;
2157 if (flag & ~AT_SYMLINK_NOFOLLOW)
2160 NDINIT_ATRIGHTS(&nd, LOOKUP, ((flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW :
2161 FOLLOW) | LOCKSHARED | LOCKLEAF | AUDITVNODE1, pathseg, path, fd,
2162 cap_rights_init(&rights, CAP_FSTAT), td);
2164 if ((error = namei(&nd)) != 0)
2166 error = vn_stat(nd.ni_vp, &sb, td->td_ucred, NOCRED, td);
2168 SDT_PROBE2(vfs, , stat, mode, path, sb.st_mode);
2169 if (S_ISREG(sb.st_mode))
2170 SDT_PROBE2(vfs, , stat, reg, path, pathseg);
2171 if (__predict_false(hook != NULL))
2172 hook(nd.ni_vp, &sb);
2174 NDFREE(&nd, NDF_ONLY_PNBUF);
2180 if (KTRPOINT(td, KTR_STRUCT))
2187 * Get file status; this version does not follow links.
2189 #ifndef _SYS_SYSPROTO_H_
2198 register struct lstat_args /* {
2206 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2207 UIO_USERSPACE, &sb, NULL);
2209 error = copyout(&sb, uap->ub, sizeof (sb));
2214 * Implementation of the NetBSD [l]stat() functions.
2222 bzero(nsb, sizeof *nsb);
2223 nsb->st_dev = sb->st_dev;
2224 nsb->st_ino = sb->st_ino;
2225 nsb->st_mode = sb->st_mode;
2226 nsb->st_nlink = sb->st_nlink;
2227 nsb->st_uid = sb->st_uid;
2228 nsb->st_gid = sb->st_gid;
2229 nsb->st_rdev = sb->st_rdev;
2230 nsb->st_atim = sb->st_atim;
2231 nsb->st_mtim = sb->st_mtim;
2232 nsb->st_ctim = sb->st_ctim;
2233 nsb->st_size = sb->st_size;
2234 nsb->st_blocks = sb->st_blocks;
2235 nsb->st_blksize = sb->st_blksize;
2236 nsb->st_flags = sb->st_flags;
2237 nsb->st_gen = sb->st_gen;
2238 nsb->st_birthtim = sb->st_birthtim;
2241 #ifndef _SYS_SYSPROTO_H_
2250 register struct nstat_args /* {
2259 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
2263 cvtnstat(&sb, &nsb);
2264 return (copyout(&nsb, uap->ub, sizeof (nsb)));
2268 * NetBSD lstat. Get file status; this version does not follow links.
2270 #ifndef _SYS_SYSPROTO_H_
2279 register struct nlstat_args /* {
2288 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2289 UIO_USERSPACE, &sb, NULL);
2292 cvtnstat(&sb, &nsb);
2293 return (copyout(&nsb, uap->ub, sizeof (nsb)));
2297 * Get configurable pathname variables.
2299 #ifndef _SYS_SYSPROTO_H_
2300 struct pathconf_args {
2306 sys_pathconf(td, uap)
2308 register struct pathconf_args /* {
2314 return (kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name, FOLLOW));
2317 #ifndef _SYS_SYSPROTO_H_
2318 struct lpathconf_args {
2324 sys_lpathconf(td, uap)
2326 register struct lpathconf_args /* {
2332 return (kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name,
2337 kern_pathconf(struct thread *td, char *path, enum uio_seg pathseg, int name,
2340 struct nameidata nd;
2343 NDINIT(&nd, LOOKUP, LOCKSHARED | LOCKLEAF | AUDITVNODE1 | flags,
2345 if ((error = namei(&nd)) != 0)
2347 NDFREE(&nd, NDF_ONLY_PNBUF);
2349 /* If asynchronous I/O is available, it works for all files. */
2350 if (name == _PC_ASYNC_IO)
2351 td->td_retval[0] = async_io_version;
2353 error = VOP_PATHCONF(nd.ni_vp, name, td->td_retval);
2359 * Return target name of a symbolic link.
2361 #ifndef _SYS_SYSPROTO_H_
2362 struct readlink_args {
2369 sys_readlink(td, uap)
2371 register struct readlink_args /* {
2378 return (kern_readlinkat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2379 uap->buf, UIO_USERSPACE, uap->count));
2381 #ifndef _SYS_SYSPROTO_H_
2382 struct readlinkat_args {
2390 sys_readlinkat(struct thread *td, struct readlinkat_args *uap)
2393 return (kern_readlinkat(td, uap->fd, uap->path, UIO_USERSPACE,
2394 uap->buf, UIO_USERSPACE, uap->bufsize));
2398 kern_readlinkat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2399 char *buf, enum uio_seg bufseg, size_t count)
2404 struct nameidata nd;
2407 if (count > IOSIZE_MAX)
2410 NDINIT_AT(&nd, LOOKUP, NOFOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
2411 pathseg, path, fd, td);
2413 if ((error = namei(&nd)) != 0)
2415 NDFREE(&nd, NDF_ONLY_PNBUF);
2418 error = mac_vnode_check_readlink(td->td_ucred, vp);
2424 if (vp->v_type != VLNK)
2427 aiov.iov_base = buf;
2428 aiov.iov_len = count;
2429 auio.uio_iov = &aiov;
2430 auio.uio_iovcnt = 1;
2431 auio.uio_offset = 0;
2432 auio.uio_rw = UIO_READ;
2433 auio.uio_segflg = bufseg;
2435 auio.uio_resid = count;
2436 error = VOP_READLINK(vp, &auio, td->td_ucred);
2437 td->td_retval[0] = count - auio.uio_resid;
2444 * Common implementation code for chflags() and fchflags().
2447 setfflags(td, vp, flags)
2456 /* We can't support the value matching VNOVAL. */
2457 if (flags == VNOVAL)
2458 return (EOPNOTSUPP);
2461 * Prevent non-root users from setting flags on devices. When
2462 * a device is reused, users can retain ownership of the device
2463 * if they are allowed to set flags and programs assume that
2464 * chown can't fail when done as root.
2466 if (vp->v_type == VCHR || vp->v_type == VBLK) {
2467 error = priv_check(td, PRIV_VFS_CHFLAGS_DEV);
2472 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2475 vattr.va_flags = flags;
2476 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2478 error = mac_vnode_check_setflags(td->td_ucred, vp, vattr.va_flags);
2481 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
2483 vn_finished_write(mp);
2488 * Change flags of a file given a path name.
2490 #ifndef _SYS_SYSPROTO_H_
2491 struct chflags_args {
2497 sys_chflags(td, uap)
2499 register struct chflags_args /* {
2505 return (kern_chflagsat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2509 #ifndef _SYS_SYSPROTO_H_
2510 struct chflagsat_args {
2518 sys_chflagsat(struct thread *td, struct chflagsat_args *uap)
2521 const char *path = uap->path;
2522 u_long flags = uap->flags;
2523 int atflag = uap->atflag;
2525 if (atflag & ~AT_SYMLINK_NOFOLLOW)
2528 return (kern_chflagsat(td, fd, path, UIO_USERSPACE, flags, atflag));
2532 * Same as chflags() but doesn't follow symlinks.
2535 sys_lchflags(td, uap)
2537 register struct lchflags_args /* {
2543 return (kern_chflagsat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2544 uap->flags, AT_SYMLINK_NOFOLLOW));
2548 kern_chflagsat(struct thread *td, int fd, const char *path,
2549 enum uio_seg pathseg, u_long flags, int atflag)
2551 struct nameidata nd;
2552 cap_rights_t rights;
2555 AUDIT_ARG_FFLAGS(flags);
2556 follow = (atflag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
2557 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, pathseg, path, fd,
2558 cap_rights_init(&rights, CAP_FCHFLAGS), td);
2559 if ((error = namei(&nd)) != 0)
2561 NDFREE(&nd, NDF_ONLY_PNBUF);
2562 error = setfflags(td, nd.ni_vp, flags);
2568 * Change flags of a file given a file descriptor.
2570 #ifndef _SYS_SYSPROTO_H_
2571 struct fchflags_args {
2577 sys_fchflags(td, uap)
2579 register struct fchflags_args /* {
2585 cap_rights_t rights;
2588 AUDIT_ARG_FD(uap->fd);
2589 AUDIT_ARG_FFLAGS(uap->flags);
2590 error = getvnode(td, uap->fd, cap_rights_init(&rights, CAP_FCHFLAGS),
2595 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
2596 AUDIT_ARG_VNODE1(fp->f_vnode);
2597 VOP_UNLOCK(fp->f_vnode, 0);
2599 error = setfflags(td, fp->f_vnode, uap->flags);
2605 * Common implementation code for chmod(), lchmod() and fchmod().
2608 setfmode(td, cred, vp, mode)
2618 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2620 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2622 vattr.va_mode = mode & ALLPERMS;
2624 error = mac_vnode_check_setmode(cred, vp, vattr.va_mode);
2627 error = VOP_SETATTR(vp, &vattr, cred);
2629 vn_finished_write(mp);
2634 * Change mode of a file given path name.
2636 #ifndef _SYS_SYSPROTO_H_
2645 register struct chmod_args /* {
2651 return (kern_fchmodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2655 #ifndef _SYS_SYSPROTO_H_
2656 struct fchmodat_args {
2664 sys_fchmodat(struct thread *td, struct fchmodat_args *uap)
2666 int flag = uap->flag;
2668 char *path = uap->path;
2669 mode_t mode = uap->mode;
2671 if (flag & ~AT_SYMLINK_NOFOLLOW)
2674 return (kern_fchmodat(td, fd, path, UIO_USERSPACE, mode, flag));
2678 * Change mode of a file given path name (don't follow links.)
2680 #ifndef _SYS_SYSPROTO_H_
2681 struct lchmod_args {
2689 register struct lchmod_args /* {
2695 return (kern_fchmodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2696 uap->mode, AT_SYMLINK_NOFOLLOW));
2700 kern_fchmodat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2701 mode_t mode, int flag)
2703 struct nameidata nd;
2704 cap_rights_t rights;
2707 AUDIT_ARG_MODE(mode);
2708 follow = (flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
2709 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, pathseg, path, fd,
2710 cap_rights_init(&rights, CAP_FCHMOD), td);
2711 if ((error = namei(&nd)) != 0)
2713 NDFREE(&nd, NDF_ONLY_PNBUF);
2714 error = setfmode(td, td->td_ucred, nd.ni_vp, mode);
2720 * Change mode of a file given a file descriptor.
2722 #ifndef _SYS_SYSPROTO_H_
2723 struct fchmod_args {
2729 sys_fchmod(struct thread *td, struct fchmod_args *uap)
2732 cap_rights_t rights;
2735 AUDIT_ARG_FD(uap->fd);
2736 AUDIT_ARG_MODE(uap->mode);
2738 error = fget(td, uap->fd, cap_rights_init(&rights, CAP_FCHMOD), &fp);
2741 error = fo_chmod(fp, uap->mode, td->td_ucred, td);
2747 * Common implementation for chown(), lchown(), and fchown()
2750 setfown(td, cred, vp, uid, gid)
2761 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2763 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2768 error = mac_vnode_check_setowner(cred, vp, vattr.va_uid,
2772 error = VOP_SETATTR(vp, &vattr, cred);
2774 vn_finished_write(mp);
2779 * Set ownership given a path name.
2781 #ifndef _SYS_SYSPROTO_H_
2791 register struct chown_args /* {
2798 return (kern_fchownat(td, AT_FDCWD, uap->path, UIO_USERSPACE, uap->uid,
2802 #ifndef _SYS_SYSPROTO_H_
2803 struct fchownat_args {
2812 sys_fchownat(struct thread *td, struct fchownat_args *uap)
2817 if (flag & ~AT_SYMLINK_NOFOLLOW)
2820 return (kern_fchownat(td, uap->fd, uap->path, UIO_USERSPACE, uap->uid,
2821 uap->gid, uap->flag));
2825 kern_fchownat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2826 int uid, int gid, int flag)
2828 struct nameidata nd;
2829 cap_rights_t rights;
2832 AUDIT_ARG_OWNER(uid, gid);
2833 follow = (flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
2834 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, pathseg, path, fd,
2835 cap_rights_init(&rights, CAP_FCHOWN), td);
2837 if ((error = namei(&nd)) != 0)
2839 NDFREE(&nd, NDF_ONLY_PNBUF);
2840 error = setfown(td, td->td_ucred, nd.ni_vp, uid, gid);
2846 * Set ownership given a path name, do not cross symlinks.
2848 #ifndef _SYS_SYSPROTO_H_
2849 struct lchown_args {
2858 register struct lchown_args /* {
2865 return (kern_fchownat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2866 uap->uid, uap->gid, AT_SYMLINK_NOFOLLOW));
2870 * Set ownership given a file descriptor.
2872 #ifndef _SYS_SYSPROTO_H_
2873 struct fchown_args {
2882 register struct fchown_args /* {
2889 cap_rights_t rights;
2892 AUDIT_ARG_FD(uap->fd);
2893 AUDIT_ARG_OWNER(uap->uid, uap->gid);
2894 error = fget(td, uap->fd, cap_rights_init(&rights, CAP_FCHOWN), &fp);
2897 error = fo_chown(fp, uap->uid, uap->gid, td->td_ucred, td);
2903 * Common implementation code for utimes(), lutimes(), and futimes().
2906 getutimes(usrtvp, tvpseg, tsp)
2907 const struct timeval *usrtvp;
2908 enum uio_seg tvpseg;
2909 struct timespec *tsp;
2911 struct timeval tv[2];
2912 const struct timeval *tvp;
2915 if (usrtvp == NULL) {
2916 vfs_timestamp(&tsp[0]);
2919 if (tvpseg == UIO_SYSSPACE) {
2922 if ((error = copyin(usrtvp, tv, sizeof(tv))) != 0)
2927 if (tvp[0].tv_usec < 0 || tvp[0].tv_usec >= 1000000 ||
2928 tvp[1].tv_usec < 0 || tvp[1].tv_usec >= 1000000)
2930 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
2931 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
2937 * Common implementation code for futimens(), utimensat().
2939 #define UTIMENS_NULL 0x1
2940 #define UTIMENS_EXIT 0x2
2942 getutimens(const struct timespec *usrtsp, enum uio_seg tspseg,
2943 struct timespec *tsp, int *retflags)
2945 struct timespec tsnow;
2948 vfs_timestamp(&tsnow);
2950 if (usrtsp == NULL) {
2953 *retflags |= UTIMENS_NULL;
2956 if (tspseg == UIO_SYSSPACE) {
2959 } else if ((error = copyin(usrtsp, tsp, sizeof(*tsp) * 2)) != 0)
2961 if (tsp[0].tv_nsec == UTIME_OMIT && tsp[1].tv_nsec == UTIME_OMIT)
2962 *retflags |= UTIMENS_EXIT;
2963 if (tsp[0].tv_nsec == UTIME_NOW && tsp[1].tv_nsec == UTIME_NOW)
2964 *retflags |= UTIMENS_NULL;
2965 if (tsp[0].tv_nsec == UTIME_OMIT)
2966 tsp[0].tv_sec = VNOVAL;
2967 else if (tsp[0].tv_nsec == UTIME_NOW)
2969 else if (tsp[0].tv_nsec < 0 || tsp[0].tv_nsec >= 1000000000L)
2971 if (tsp[1].tv_nsec == UTIME_OMIT)
2972 tsp[1].tv_sec = VNOVAL;
2973 else if (tsp[1].tv_nsec == UTIME_NOW)
2975 else if (tsp[1].tv_nsec < 0 || tsp[1].tv_nsec >= 1000000000L)
2982 * Common implementation code for utimes(), lutimes(), futimes(), futimens(),
2986 setutimes(td, vp, ts, numtimes, nullflag)
2989 const struct timespec *ts;
2995 int error, setbirthtime;
2997 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2999 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3001 if (numtimes < 3 && !VOP_GETATTR(vp, &vattr, td->td_ucred) &&
3002 timespeccmp(&ts[1], &vattr.va_birthtime, < ))
3005 vattr.va_atime = ts[0];
3006 vattr.va_mtime = ts[1];
3008 vattr.va_birthtime = ts[1];
3010 vattr.va_birthtime = ts[2];
3012 vattr.va_vaflags |= VA_UTIMES_NULL;
3014 error = mac_vnode_check_setutimes(td->td_ucred, vp, vattr.va_atime,
3018 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3020 vn_finished_write(mp);
3025 * Set the access and modification times of a file.
3027 #ifndef _SYS_SYSPROTO_H_
3028 struct utimes_args {
3030 struct timeval *tptr;
3036 register struct utimes_args /* {
3038 struct timeval *tptr;
3042 return (kern_utimesat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
3043 uap->tptr, UIO_USERSPACE));
3046 #ifndef _SYS_SYSPROTO_H_
3047 struct futimesat_args {
3050 const struct timeval * times;
3054 sys_futimesat(struct thread *td, struct futimesat_args *uap)
3057 return (kern_utimesat(td, uap->fd, uap->path, UIO_USERSPACE,
3058 uap->times, UIO_USERSPACE));
3062 kern_utimesat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
3063 struct timeval *tptr, enum uio_seg tptrseg)
3065 struct nameidata nd;
3066 struct timespec ts[2];
3067 cap_rights_t rights;
3070 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3072 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, fd,
3073 cap_rights_init(&rights, CAP_FUTIMES), td);
3075 if ((error = namei(&nd)) != 0)
3077 NDFREE(&nd, NDF_ONLY_PNBUF);
3078 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3084 * Set the access and modification times of a file.
3086 #ifndef _SYS_SYSPROTO_H_
3087 struct lutimes_args {
3089 struct timeval *tptr;
3093 sys_lutimes(td, uap)
3095 register struct lutimes_args /* {
3097 struct timeval *tptr;
3101 return (kern_lutimes(td, uap->path, UIO_USERSPACE, uap->tptr,
3106 kern_lutimes(struct thread *td, char *path, enum uio_seg pathseg,
3107 struct timeval *tptr, enum uio_seg tptrseg)
3109 struct timespec ts[2];
3110 struct nameidata nd;
3113 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3115 NDINIT(&nd, LOOKUP, NOFOLLOW | AUDITVNODE1, pathseg, path, td);
3116 if ((error = namei(&nd)) != 0)
3118 NDFREE(&nd, NDF_ONLY_PNBUF);
3119 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3125 * Set the access and modification times of a file.
3127 #ifndef _SYS_SYSPROTO_H_
3128 struct futimes_args {
3130 struct timeval *tptr;
3134 sys_futimes(td, uap)
3136 register struct futimes_args /* {
3138 struct timeval *tptr;
3142 return (kern_futimes(td, uap->fd, uap->tptr, UIO_USERSPACE));
3146 kern_futimes(struct thread *td, int fd, struct timeval *tptr,
3147 enum uio_seg tptrseg)
3149 struct timespec ts[2];
3151 cap_rights_t rights;
3155 error = getutimes(tptr, tptrseg, ts);
3158 error = getvnode(td, fd, cap_rights_init(&rights, CAP_FUTIMES), &fp);
3162 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
3163 AUDIT_ARG_VNODE1(fp->f_vnode);
3164 VOP_UNLOCK(fp->f_vnode, 0);
3166 error = setutimes(td, fp->f_vnode, ts, 2, tptr == NULL);
3172 sys_futimens(struct thread *td, struct futimens_args *uap)
3175 return (kern_futimens(td, uap->fd, uap->times, UIO_USERSPACE));
3179 kern_futimens(struct thread *td, int fd, struct timespec *tptr,
3180 enum uio_seg tptrseg)
3182 struct timespec ts[2];
3184 cap_rights_t rights;
3188 error = getutimens(tptr, tptrseg, ts, &flags);
3191 if (flags & UTIMENS_EXIT)
3193 error = getvnode(td, fd, cap_rights_init(&rights, CAP_FUTIMES), &fp);
3197 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
3198 AUDIT_ARG_VNODE1(fp->f_vnode);
3199 VOP_UNLOCK(fp->f_vnode, 0);
3201 error = setutimes(td, fp->f_vnode, ts, 2, flags & UTIMENS_NULL);
3207 sys_utimensat(struct thread *td, struct utimensat_args *uap)
3210 return (kern_utimensat(td, uap->fd, uap->path, UIO_USERSPACE,
3211 uap->times, UIO_USERSPACE, uap->flag));
3215 kern_utimensat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
3216 struct timespec *tptr, enum uio_seg tptrseg, int flag)
3218 struct nameidata nd;
3219 struct timespec ts[2];
3220 cap_rights_t rights;
3223 if (flag & ~AT_SYMLINK_NOFOLLOW)
3226 if ((error = getutimens(tptr, tptrseg, ts, &flags)) != 0)
3228 NDINIT_ATRIGHTS(&nd, LOOKUP, ((flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW :
3229 FOLLOW) | AUDITVNODE1, pathseg, path, fd,
3230 cap_rights_init(&rights, CAP_FUTIMES), td);
3231 if ((error = namei(&nd)) != 0)
3234 * We are allowed to call namei() regardless of 2xUTIME_OMIT.
3236 * "If both tv_nsec fields are UTIME_OMIT... EACCESS may be detected."
3237 * "Search permission is denied by a component of the path prefix."
3239 NDFREE(&nd, NDF_ONLY_PNBUF);
3240 if ((flags & UTIMENS_EXIT) == 0)
3241 error = setutimes(td, nd.ni_vp, ts, 2, flags & UTIMENS_NULL);
3247 * Truncate a file given its path name.
3249 #ifndef _SYS_SYSPROTO_H_
3250 struct truncate_args {
3257 sys_truncate(td, uap)
3259 register struct truncate_args /* {
3266 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3270 kern_truncate(struct thread *td, char *path, enum uio_seg pathseg, off_t length)
3276 struct nameidata nd;
3281 NDINIT(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, td);
3282 if ((error = namei(&nd)) != 0)
3285 rl_cookie = vn_rangelock_wlock(vp, 0, OFF_MAX);
3286 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0) {
3287 vn_rangelock_unlock(vp, rl_cookie);
3291 NDFREE(&nd, NDF_ONLY_PNBUF);
3292 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3293 if (vp->v_type == VDIR)
3296 else if ((error = mac_vnode_check_write(td->td_ucred, NOCRED, vp))) {
3299 else if ((error = vn_writechk(vp)) == 0 &&
3300 (error = VOP_ACCESS(vp, VWRITE, td->td_ucred, td)) == 0) {
3302 vattr.va_size = length;
3303 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3306 vn_finished_write(mp);
3307 vn_rangelock_unlock(vp, rl_cookie);
3312 #if defined(COMPAT_43)
3314 * Truncate a file given its path name.
3316 #ifndef _SYS_SYSPROTO_H_
3317 struct otruncate_args {
3325 register struct otruncate_args /* {
3330 struct truncate_args /* {
3336 nuap.path = uap->path;
3337 nuap.length = uap->length;
3338 return (sys_truncate(td, &nuap));
3340 #endif /* COMPAT_43 */
3342 #if defined(COMPAT_FREEBSD6)
3343 /* Versions with the pad argument */
3345 freebsd6_truncate(struct thread *td, struct freebsd6_truncate_args *uap)
3347 struct truncate_args ouap;
3349 ouap.path = uap->path;
3350 ouap.length = uap->length;
3351 return (sys_truncate(td, &ouap));
3355 freebsd6_ftruncate(struct thread *td, struct freebsd6_ftruncate_args *uap)
3357 struct ftruncate_args ouap;
3360 ouap.length = uap->length;
3361 return (sys_ftruncate(td, &ouap));
3366 * Sync an open file.
3368 #ifndef _SYS_SYSPROTO_H_
3376 struct fsync_args /* {
3383 cap_rights_t rights;
3384 int error, lock_flags;
3386 AUDIT_ARG_FD(uap->fd);
3387 error = getvnode(td, uap->fd, cap_rights_init(&rights, CAP_FSYNC), &fp);
3391 error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
3394 if (MNT_SHARED_WRITES(mp) ||
3395 ((mp == NULL) && MNT_SHARED_WRITES(vp->v_mount))) {
3396 lock_flags = LK_SHARED;
3398 lock_flags = LK_EXCLUSIVE;
3400 vn_lock(vp, lock_flags | LK_RETRY);
3401 AUDIT_ARG_VNODE1(vp);
3402 if (vp->v_object != NULL) {
3403 VM_OBJECT_WLOCK(vp->v_object);
3404 vm_object_page_clean(vp->v_object, 0, 0, 0);
3405 VM_OBJECT_WUNLOCK(vp->v_object);
3407 error = VOP_FSYNC(vp, MNT_WAIT, td);
3410 vn_finished_write(mp);
3417 * Rename files. Source and destination must either both be directories, or
3418 * both not be directories. If target is a directory, it must be empty.
3420 #ifndef _SYS_SYSPROTO_H_
3421 struct rename_args {
3429 register struct rename_args /* {
3435 return (kern_renameat(td, AT_FDCWD, uap->from, AT_FDCWD,
3436 uap->to, UIO_USERSPACE));
3439 #ifndef _SYS_SYSPROTO_H_
3440 struct renameat_args {
3448 sys_renameat(struct thread *td, struct renameat_args *uap)
3451 return (kern_renameat(td, uap->oldfd, uap->old, uap->newfd, uap->new,
3456 kern_renameat(struct thread *td, int oldfd, char *old, int newfd, char *new,
3457 enum uio_seg pathseg)
3459 struct mount *mp = NULL;
3460 struct vnode *tvp, *fvp, *tdvp;
3461 struct nameidata fromnd, tond;
3462 cap_rights_t rights;
3468 NDINIT_ATRIGHTS(&fromnd, DELETE, LOCKPARENT | LOCKLEAF | SAVESTART |
3469 AUDITVNODE1, pathseg, old, oldfd,
3470 cap_rights_init(&rights, CAP_RENAMEAT_SOURCE), td);
3472 NDINIT_ATRIGHTS(&fromnd, DELETE, WANTPARENT | SAVESTART | AUDITVNODE1,
3473 pathseg, old, oldfd,
3474 cap_rights_init(&rights, CAP_RENAMEAT_SOURCE), td);
3477 if ((error = namei(&fromnd)) != 0)
3480 error = mac_vnode_check_rename_from(td->td_ucred, fromnd.ni_dvp,
3481 fromnd.ni_vp, &fromnd.ni_cnd);
3482 VOP_UNLOCK(fromnd.ni_dvp, 0);
3483 if (fromnd.ni_dvp != fromnd.ni_vp)
3484 VOP_UNLOCK(fromnd.ni_vp, 0);
3487 NDINIT_ATRIGHTS(&tond, RENAME, LOCKPARENT | LOCKLEAF | NOCACHE |
3488 SAVESTART | AUDITVNODE2, pathseg, new, newfd,
3489 cap_rights_init(&rights, CAP_RENAMEAT_TARGET), td);
3490 if (fromnd.ni_vp->v_type == VDIR)
3491 tond.ni_cnd.cn_flags |= WILLBEDIR;
3492 if ((error = namei(&tond)) != 0) {
3493 /* Translate error code for rename("dir1", "dir2/."). */
3494 if (error == EISDIR && fvp->v_type == VDIR)
3496 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3497 vrele(fromnd.ni_dvp);
3503 error = vn_start_write(fvp, &mp, V_NOWAIT);
3505 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3506 NDFREE(&tond, NDF_ONLY_PNBUF);
3513 vrele(fromnd.ni_dvp);
3515 vrele(tond.ni_startdir);
3516 if (fromnd.ni_startdir != NULL)
3517 vrele(fromnd.ni_startdir);
3518 error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH);
3524 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
3527 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
3532 if (newfd != AT_FDCWD) {
3534 * If the target already exists we require CAP_UNLINKAT
3537 error = cap_check(&tond.ni_filecaps.fc_rights,
3538 cap_rights_init(&rights, CAP_UNLINKAT));
3549 * If the source is the same as the destination (that is, if they
3550 * are links to the same vnode), then there is nothing to do.
3556 error = mac_vnode_check_rename_to(td->td_ucred, tdvp,
3557 tond.ni_vp, fromnd.ni_dvp == tdvp, &tond.ni_cnd);
3561 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
3562 tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
3563 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3564 NDFREE(&tond, NDF_ONLY_PNBUF);
3566 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3567 NDFREE(&tond, NDF_ONLY_PNBUF);
3574 vrele(fromnd.ni_dvp);
3577 vrele(tond.ni_startdir);
3578 vn_finished_write(mp);
3580 if (fromnd.ni_startdir)
3581 vrele(fromnd.ni_startdir);
3588 * Make a directory file.
3590 #ifndef _SYS_SYSPROTO_H_
3599 register struct mkdir_args /* {
3605 return (kern_mkdirat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
3609 #ifndef _SYS_SYSPROTO_H_
3610 struct mkdirat_args {
3617 sys_mkdirat(struct thread *td, struct mkdirat_args *uap)
3620 return (kern_mkdirat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode));
3624 kern_mkdirat(struct thread *td, int fd, char *path, enum uio_seg segflg,
3630 struct nameidata nd;
3631 cap_rights_t rights;
3634 AUDIT_ARG_MODE(mode);
3637 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
3638 NOCACHE, segflg, path, fd, cap_rights_init(&rights, CAP_MKDIRAT),
3640 nd.ni_cnd.cn_flags |= WILLBEDIR;
3641 if ((error = namei(&nd)) != 0)
3645 NDFREE(&nd, NDF_ONLY_PNBUF);
3647 * XXX namei called with LOCKPARENT but not LOCKLEAF has
3648 * the strange behaviour of leaving the vnode unlocked
3649 * if the target is the same vnode as the parent.
3651 if (vp == nd.ni_dvp)
3658 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3659 NDFREE(&nd, NDF_ONLY_PNBUF);
3661 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3666 vattr.va_type = VDIR;
3667 vattr.va_mode = (mode & ACCESSPERMS) &~ td->td_proc->p_fd->fd_cmask;
3669 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
3674 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
3678 NDFREE(&nd, NDF_ONLY_PNBUF);
3682 vn_finished_write(mp);
3687 * Remove a directory file.
3689 #ifndef _SYS_SYSPROTO_H_
3697 struct rmdir_args /* {
3702 return (kern_rmdirat(td, AT_FDCWD, uap->path, UIO_USERSPACE));
3706 kern_rmdirat(struct thread *td, int fd, char *path, enum uio_seg pathseg)
3710 struct nameidata nd;
3711 cap_rights_t rights;
3716 NDINIT_ATRIGHTS(&nd, DELETE, LOCKPARENT | LOCKLEAF | AUDITVNODE1,
3717 pathseg, path, fd, cap_rights_init(&rights, CAP_UNLINKAT), td);
3718 if ((error = namei(&nd)) != 0)
3721 if (vp->v_type != VDIR) {
3726 * No rmdir "." please.
3728 if (nd.ni_dvp == vp) {
3733 * The root of a mounted filesystem cannot be deleted.
3735 if (vp->v_vflag & VV_ROOT) {
3740 error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp,
3745 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3746 NDFREE(&nd, NDF_ONLY_PNBUF);
3748 if (nd.ni_dvp == vp)
3752 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3756 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_UNLINK);
3757 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
3758 vn_finished_write(mp);
3760 NDFREE(&nd, NDF_ONLY_PNBUF);
3762 if (nd.ni_dvp == vp)
3771 * Read a block of directory entries in a filesystem independent format.
3773 #ifndef _SYS_SYSPROTO_H_
3774 struct ogetdirentries_args {
3782 ogetdirentries(struct thread *td, struct ogetdirentries_args *uap)
3787 error = kern_ogetdirentries(td, uap, &loff);
3789 error = copyout(&loff, uap->basep, sizeof(long));
3794 kern_ogetdirentries(struct thread *td, struct ogetdirentries_args *uap,
3799 struct uio auio, kuio;
3800 struct iovec aiov, kiov;
3801 struct dirent *dp, *edp;
3802 cap_rights_t rights;
3804 int error, eofflag, readcnt;
3808 /* XXX arbitrary sanity limit on `count'. */
3809 if (uap->count > 64 * 1024)
3811 error = getvnode(td, uap->fd, cap_rights_init(&rights, CAP_READ), &fp);
3814 if ((fp->f_flag & FREAD) == 0) {
3819 foffset = foffset_lock(fp, 0);
3821 if (vp->v_type != VDIR) {
3822 foffset_unlock(fp, foffset, 0);
3826 aiov.iov_base = uap->buf;
3827 aiov.iov_len = uap->count;
3828 auio.uio_iov = &aiov;
3829 auio.uio_iovcnt = 1;
3830 auio.uio_rw = UIO_READ;
3831 auio.uio_segflg = UIO_USERSPACE;
3833 auio.uio_resid = uap->count;
3834 vn_lock(vp, LK_SHARED | LK_RETRY);
3835 loff = auio.uio_offset = foffset;
3837 error = mac_vnode_check_readdir(td->td_ucred, vp);
3840 foffset_unlock(fp, foffset, FOF_NOUPDATE);
3845 # if (BYTE_ORDER != LITTLE_ENDIAN)
3846 if (vp->v_mount->mnt_maxsymlinklen <= 0) {
3847 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag,
3849 foffset = auio.uio_offset;
3854 kuio.uio_iov = &kiov;
3855 kuio.uio_segflg = UIO_SYSSPACE;
3856 kiov.iov_len = uap->count;
3857 dirbuf = malloc(uap->count, M_TEMP, M_WAITOK);
3858 kiov.iov_base = dirbuf;
3859 error = VOP_READDIR(vp, &kuio, fp->f_cred, &eofflag,
3861 foffset = kuio.uio_offset;
3863 readcnt = uap->count - kuio.uio_resid;
3864 edp = (struct dirent *)&dirbuf[readcnt];
3865 for (dp = (struct dirent *)dirbuf; dp < edp; ) {
3866 # if (BYTE_ORDER == LITTLE_ENDIAN)
3868 * The expected low byte of
3869 * dp->d_namlen is our dp->d_type.
3870 * The high MBZ byte of dp->d_namlen
3871 * is our dp->d_namlen.
3873 dp->d_type = dp->d_namlen;
3877 * The dp->d_type is the high byte
3878 * of the expected dp->d_namlen,
3879 * so must be zero'ed.
3883 if (dp->d_reclen > 0) {
3884 dp = (struct dirent *)
3885 ((char *)dp + dp->d_reclen);
3892 error = uiomove(dirbuf, readcnt, &auio);
3894 free(dirbuf, M_TEMP);
3898 foffset_unlock(fp, foffset, 0);
3902 if (uap->count == auio.uio_resid &&
3903 (vp->v_vflag & VV_ROOT) &&
3904 (vp->v_mount->mnt_flag & MNT_UNION)) {
3905 struct vnode *tvp = vp;
3906 vp = vp->v_mount->mnt_vnodecovered;
3915 foffset_unlock(fp, foffset, 0);
3917 td->td_retval[0] = uap->count - auio.uio_resid;
3922 #endif /* COMPAT_43 */
3925 * Read a block of directory entries in a filesystem independent format.
3927 #ifndef _SYS_SYSPROTO_H_
3928 struct getdirentries_args {
3936 sys_getdirentries(td, uap)
3938 register struct getdirentries_args /* {
3948 error = kern_getdirentries(td, uap->fd, uap->buf, uap->count, &base,
3949 NULL, UIO_USERSPACE);
3952 if (uap->basep != NULL)
3953 error = copyout(&base, uap->basep, sizeof(long));
3958 kern_getdirentries(struct thread *td, int fd, char *buf, u_int count,
3959 long *basep, ssize_t *residp, enum uio_seg bufseg)
3965 cap_rights_t rights;
3971 if (count > IOSIZE_MAX)
3973 auio.uio_resid = count;
3974 error = getvnode(td, fd, cap_rights_init(&rights, CAP_READ), &fp);
3977 if ((fp->f_flag & FREAD) == 0) {
3982 foffset = foffset_lock(fp, 0);
3984 if (vp->v_type != VDIR) {
3988 aiov.iov_base = buf;
3989 aiov.iov_len = count;
3990 auio.uio_iov = &aiov;
3991 auio.uio_iovcnt = 1;
3992 auio.uio_rw = UIO_READ;
3993 auio.uio_segflg = bufseg;
3995 vn_lock(vp, LK_SHARED | LK_RETRY);
3996 AUDIT_ARG_VNODE1(vp);
3997 loff = auio.uio_offset = foffset;
3999 error = mac_vnode_check_readdir(td->td_ucred, vp);
4002 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL,
4004 foffset = auio.uio_offset;
4009 if (count == auio.uio_resid &&
4010 (vp->v_vflag & VV_ROOT) &&
4011 (vp->v_mount->mnt_flag & MNT_UNION)) {
4012 struct vnode *tvp = vp;
4014 vp = vp->v_mount->mnt_vnodecovered;
4025 *residp = auio.uio_resid;
4026 td->td_retval[0] = count - auio.uio_resid;
4028 foffset_unlock(fp, foffset, 0);
4033 #ifndef _SYS_SYSPROTO_H_
4034 struct getdents_args {
4041 sys_getdents(td, uap)
4043 register struct getdents_args /* {
4049 struct getdirentries_args ap;
4053 ap.count = uap->count;
4055 return (sys_getdirentries(td, &ap));
4059 * Set the mode mask for creation of filesystem nodes.
4061 #ifndef _SYS_SYSPROTO_H_
4069 struct umask_args /* {
4073 struct filedesc *fdp;
4075 fdp = td->td_proc->p_fd;
4076 FILEDESC_XLOCK(fdp);
4077 td->td_retval[0] = fdp->fd_cmask;
4078 fdp->fd_cmask = uap->newmask & ALLPERMS;
4079 FILEDESC_XUNLOCK(fdp);
4084 * Void all references to file by ripping underlying filesystem away from
4087 #ifndef _SYS_SYSPROTO_H_
4088 struct revoke_args {
4095 register struct revoke_args /* {
4101 struct nameidata nd;
4104 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
4106 if ((error = namei(&nd)) != 0)
4109 NDFREE(&nd, NDF_ONLY_PNBUF);
4110 if (vp->v_type != VCHR || vp->v_rdev == NULL) {
4115 error = mac_vnode_check_revoke(td->td_ucred, vp);
4119 error = VOP_GETATTR(vp, &vattr, td->td_ucred);
4122 if (td->td_ucred->cr_uid != vattr.va_uid) {
4123 error = priv_check(td, PRIV_VFS_ADMIN);
4128 VOP_REVOKE(vp, REVOKEALL);
4135 * Convert a user file descriptor to a kernel file entry and check that, if it
4136 * is a capability, the correct rights are present. A reference on the file
4137 * entry is held upon returning.
4140 getvnode(struct thread *td, int fd, cap_rights_t *rightsp, struct file **fpp)
4145 error = fget_unlocked(td->td_proc->p_fd, fd, rightsp, &fp, NULL);
4150 * The file could be not of the vnode type, or it may be not
4151 * yet fully initialized, in which case the f_vnode pointer
4152 * may be set, but f_ops is still badfileops. E.g.,
4153 * devfs_open() transiently create such situation to
4154 * facilitate csw d_fdopen().
4156 * Dupfdopen() handling in kern_openat() installs the
4157 * half-baked file into the process descriptor table, allowing
4158 * other thread to dereference it. Guard against the race by
4161 if (fp->f_vnode == NULL || fp->f_ops == &badfileops) {
4171 * Get an (NFS) file handle.
4173 #ifndef _SYS_SYSPROTO_H_
4174 struct lgetfh_args {
4182 register struct lgetfh_args *uap;
4184 struct nameidata nd;
4186 register struct vnode *vp;
4189 error = priv_check(td, PRIV_VFS_GETFH);
4192 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
4197 NDFREE(&nd, NDF_ONLY_PNBUF);
4199 bzero(&fh, sizeof(fh));
4200 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
4201 error = VOP_VPTOFH(vp, &fh.fh_fid);
4204 error = copyout(&fh, uap->fhp, sizeof (fh));
4208 #ifndef _SYS_SYSPROTO_H_
4217 register struct getfh_args *uap;
4219 struct nameidata nd;
4221 register struct vnode *vp;
4224 error = priv_check(td, PRIV_VFS_GETFH);
4227 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
4232 NDFREE(&nd, NDF_ONLY_PNBUF);
4234 bzero(&fh, sizeof(fh));
4235 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
4236 error = VOP_VPTOFH(vp, &fh.fh_fid);
4239 error = copyout(&fh, uap->fhp, sizeof (fh));
4244 * syscall for the rpc.lockd to use to translate a NFS file handle into an
4247 * warning: do not remove the priv_check() call or this becomes one giant
4250 #ifndef _SYS_SYSPROTO_H_
4251 struct fhopen_args {
4252 const struct fhandle *u_fhp;
4259 struct fhopen_args /* {
4260 const struct fhandle *u_fhp;
4271 error = priv_check(td, PRIV_VFS_FHOPEN);
4275 fmode = FFLAGS(uap->flags);
4276 /* why not allow a non-read/write open for our lockd? */
4277 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
4279 error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
4282 /* find the mount point */
4283 mp = vfs_busyfs(&fhp.fh_fsid);
4286 /* now give me my vnode, it gets returned to me locked */
4287 error = VFS_FHTOVP(mp, &fhp.fh_fid, LK_EXCLUSIVE, &vp);
4292 error = falloc_noinstall(td, &fp);
4298 * An extra reference on `fp' has been held for us by
4299 * falloc_noinstall().
4305 error = vn_open_vnode(vp, fmode, td->td_ucred, td, fp);
4307 KASSERT(fp->f_ops == &badfileops,
4308 ("VOP_OPEN in fhopen() set f_ops"));
4309 KASSERT(td->td_dupfd < 0,
4310 ("fhopen() encountered fdopen()"));
4320 finit(fp, (fmode & FMASK) | (fp->f_flag & FHASLOCK), DTYPE_VNODE, vp,
4323 if ((fmode & O_TRUNC) != 0) {
4324 error = fo_truncate(fp, 0, td->td_ucred, td);
4329 error = finstall(td, fp, &indx, fmode, NULL);
4332 td->td_retval[0] = indx;
4337 * Stat an (NFS) file handle.
4339 #ifndef _SYS_SYSPROTO_H_
4340 struct fhstat_args {
4341 struct fhandle *u_fhp;
4348 register struct fhstat_args /* {
4349 struct fhandle *u_fhp;
4357 error = copyin(uap->u_fhp, &fh, sizeof(fh));
4360 error = kern_fhstat(td, fh, &sb);
4362 error = copyout(&sb, uap->sb, sizeof(sb));
4367 kern_fhstat(struct thread *td, struct fhandle fh, struct stat *sb)
4373 error = priv_check(td, PRIV_VFS_FHSTAT);
4376 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4378 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_EXCLUSIVE, &vp);
4382 error = vn_stat(vp, sb, td->td_ucred, NOCRED, td);
4388 * Implement fstatfs() for (NFS) file handles.
4390 #ifndef _SYS_SYSPROTO_H_
4391 struct fhstatfs_args {
4392 struct fhandle *u_fhp;
4397 sys_fhstatfs(td, uap)
4399 struct fhstatfs_args /* {
4400 struct fhandle *u_fhp;
4408 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4411 error = kern_fhstatfs(td, fh, &sf);
4414 return (copyout(&sf, uap->buf, sizeof(sf)));
4418 kern_fhstatfs(struct thread *td, fhandle_t fh, struct statfs *buf)
4425 error = priv_check(td, PRIV_VFS_FHSTATFS);
4428 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4430 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_EXCLUSIVE, &vp);
4436 error = prison_canseemount(td->td_ucred, mp);
4440 error = mac_mount_check_stat(td->td_ucred, mp);
4445 * Set these in case the underlying filesystem fails to do so.
4448 sp->f_version = STATFS_VERSION;
4449 sp->f_namemax = NAME_MAX;
4450 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
4451 error = VFS_STATFS(mp, sp);
4460 kern_posix_fallocate(struct thread *td, int fd, off_t offset, off_t len)
4465 cap_rights_t rights;
4466 off_t olen, ooffset;
4469 if (offset < 0 || len <= 0)
4471 /* Check for wrap. */
4472 if (offset > OFF_MAX - len)
4474 error = fget(td, fd, cap_rights_init(&rights, CAP_WRITE), &fp);
4477 if ((fp->f_ops->fo_flags & DFLAG_SEEKABLE) == 0) {
4481 if ((fp->f_flag & FWRITE) == 0) {
4485 if (fp->f_type != DTYPE_VNODE) {
4490 if (vp->v_type != VREG) {
4495 /* Allocating blocks may take a long time, so iterate. */
4502 error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
4505 error = vn_lock(vp, LK_EXCLUSIVE);
4507 vn_finished_write(mp);
4511 error = mac_vnode_check_write(td->td_ucred, fp->f_cred, vp);
4514 error = VOP_ALLOCATE(vp, &offset, &len);
4516 vn_finished_write(mp);
4518 if (olen + ooffset != offset + len) {
4519 panic("offset + len changed from %jx/%jx to %jx/%jx",
4520 ooffset, olen, offset, len);
4522 if (error != 0 || len == 0)
4524 KASSERT(olen > len, ("Iteration did not make progress?"));
4533 sys_posix_fallocate(struct thread *td, struct posix_fallocate_args *uap)
4536 td->td_retval[0] = kern_posix_fallocate(td, uap->fd, uap->offset,
4542 * Unlike madvise(2), we do not make a best effort to remember every
4543 * possible caching hint. Instead, we remember the last setting with
4544 * the exception that we will allow POSIX_FADV_NORMAL to adjust the
4545 * region of any current setting.
4548 kern_posix_fadvise(struct thread *td, int fd, off_t offset, off_t len,
4551 struct fadvise_info *fa, *new;
4554 cap_rights_t rights;
4558 if (offset < 0 || len < 0 || offset > OFF_MAX - len)
4561 case POSIX_FADV_SEQUENTIAL:
4562 case POSIX_FADV_RANDOM:
4563 case POSIX_FADV_NOREUSE:
4564 new = malloc(sizeof(*fa), M_FADVISE, M_WAITOK);
4566 case POSIX_FADV_NORMAL:
4567 case POSIX_FADV_WILLNEED:
4568 case POSIX_FADV_DONTNEED:
4574 /* XXX: CAP_POSIX_FADVISE? */
4575 error = fget(td, fd, cap_rights_init(&rights), &fp);
4578 if ((fp->f_ops->fo_flags & DFLAG_SEEKABLE) == 0) {
4582 if (fp->f_type != DTYPE_VNODE) {
4587 if (vp->v_type != VREG) {
4594 end = offset + len - 1;
4596 case POSIX_FADV_SEQUENTIAL:
4597 case POSIX_FADV_RANDOM:
4598 case POSIX_FADV_NOREUSE:
4600 * Try to merge any existing non-standard region with
4601 * this new region if possible, otherwise create a new
4602 * non-standard region for this request.
4604 mtx_pool_lock(mtxpool_sleep, fp);
4606 if (fa != NULL && fa->fa_advice == advice &&
4607 ((fa->fa_start <= end && fa->fa_end >= offset) ||
4608 (end != OFF_MAX && fa->fa_start == end + 1) ||
4609 (fa->fa_end != OFF_MAX && fa->fa_end + 1 == offset))) {
4610 if (offset < fa->fa_start)
4611 fa->fa_start = offset;
4612 if (end > fa->fa_end)
4615 new->fa_advice = advice;
4616 new->fa_start = offset;
4621 mtx_pool_unlock(mtxpool_sleep, fp);
4623 case POSIX_FADV_NORMAL:
4625 * If a the "normal" region overlaps with an existing
4626 * non-standard region, trim or remove the
4627 * non-standard region.
4629 mtx_pool_lock(mtxpool_sleep, fp);
4632 if (offset <= fa->fa_start && end >= fa->fa_end) {
4634 fp->f_advice = NULL;
4635 } else if (offset <= fa->fa_start &&
4636 end >= fa->fa_start)
4637 fa->fa_start = end + 1;
4638 else if (offset <= fa->fa_end && end >= fa->fa_end)
4639 fa->fa_end = offset - 1;
4640 else if (offset >= fa->fa_start && end <= fa->fa_end) {
4642 * If the "normal" region is a middle
4643 * portion of the existing
4644 * non-standard region, just remove
4645 * the whole thing rather than picking
4646 * one side or the other to
4650 fp->f_advice = NULL;
4653 mtx_pool_unlock(mtxpool_sleep, fp);
4655 case POSIX_FADV_WILLNEED:
4656 case POSIX_FADV_DONTNEED:
4657 error = VOP_ADVISE(vp, offset, end, advice);
4663 free(new, M_FADVISE);
4668 sys_posix_fadvise(struct thread *td, struct posix_fadvise_args *uap)
4671 td->td_retval[0] = kern_posix_fadvise(td, uap->fd, uap->offset,
4672 uap->len, uap->advice);