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/capability.h>
50 #include <sys/sysent.h>
51 #include <sys/malloc.h>
52 #include <sys/mount.h>
53 #include <sys/mutex.h>
54 #include <sys/sysproto.h>
55 #include <sys/namei.h>
56 #include <sys/filedesc.h>
57 #include <sys/kernel.h>
58 #include <sys/fcntl.h>
60 #include <sys/filio.h>
61 #include <sys/limits.h>
62 #include <sys/linker.h>
63 #include <sys/rwlock.h>
67 #include <sys/unistd.h>
68 #include <sys/vnode.h>
71 #include <sys/dirent.h>
73 #include <sys/syscallsubr.h>
74 #include <sys/sysctl.h>
76 #include <sys/ktrace.h>
79 #include <machine/stdarg.h>
81 #include <security/audit/audit.h>
82 #include <security/mac/mac_framework.h>
85 #include <vm/vm_object.h>
86 #include <vm/vm_page.h>
89 #include <ufs/ufs/quota.h>
91 MALLOC_DEFINE(M_FADVISE, "fadvise", "posix_fadvise(2) information");
93 SDT_PROVIDER_DEFINE(vfs);
94 SDT_PROBE_DEFINE2(vfs, , stat, mode, "char *", "int");
95 SDT_PROBE_DEFINE2(vfs, , stat, reg, "char *", "int");
97 static int chroot_refuse_vdir_fds(struct filedesc *fdp);
98 static int getutimes(const struct timeval *, enum uio_seg, struct timespec *);
99 static int kern_chflags(struct thread *td, const char *path,
100 enum uio_seg pathseg, u_long flags);
101 static int kern_chflagsat(struct thread *td, int fd, const char *path,
102 enum uio_seg pathseg, u_long flags, int atflag);
103 static int setfflags(struct thread *td, struct vnode *, u_long);
104 static int setutimes(struct thread *td, struct vnode *,
105 const struct timespec *, int, int);
106 static int vn_access(struct vnode *vp, int user_flags, struct ucred *cred,
110 * The module initialization routine for POSIX asynchronous I/O will
111 * set this to the version of AIO that it implements. (Zero means
112 * that it is not implemented.) This value is used here by pathconf()
113 * and in kern_descrip.c by fpathconf().
115 int async_io_version;
118 static int syncprt = 0;
119 SYSCTL_INT(_debug, OID_AUTO, syncprt, CTLFLAG_RW, &syncprt, 0, "");
123 * Sync each mounted filesystem.
125 #ifndef _SYS_SYSPROTO_H_
134 struct sync_args *uap;
136 struct mount *mp, *nmp;
139 mtx_lock(&mountlist_mtx);
140 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
141 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK)) {
142 nmp = TAILQ_NEXT(mp, mnt_list);
145 if ((mp->mnt_flag & MNT_RDONLY) == 0 &&
146 vn_start_write(NULL, &mp, V_NOWAIT) == 0) {
147 save = curthread_pflags_set(TDP_SYNCIO);
148 vfs_msync(mp, MNT_NOWAIT);
149 VFS_SYNC(mp, MNT_NOWAIT);
150 curthread_pflags_restore(save);
151 vn_finished_write(mp);
153 mtx_lock(&mountlist_mtx);
154 nmp = TAILQ_NEXT(mp, mnt_list);
157 mtx_unlock(&mountlist_mtx);
162 * Change filesystem quotas.
164 #ifndef _SYS_SYSPROTO_H_
165 struct quotactl_args {
173 sys_quotactl(td, uap)
175 register struct quotactl_args /* {
186 AUDIT_ARG_CMD(uap->cmd);
187 AUDIT_ARG_UID(uap->uid);
188 if (!prison_allow(td->td_ucred, PR_ALLOW_QUOTAS))
190 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
192 if ((error = namei(&nd)) != 0)
194 NDFREE(&nd, NDF_ONLY_PNBUF);
195 mp = nd.ni_vp->v_mount;
198 error = vfs_busy(mp, 0);
202 error = VFS_QUOTACTL(mp, uap->cmd, uap->uid, uap->arg);
205 * Since quota on operation typically needs to open quota
206 * file, the Q_QUOTAON handler needs to unbusy the mount point
207 * before calling into namei. Otherwise, unmount might be
208 * started between two vfs_busy() invocations (first is our,
209 * second is from mount point cross-walk code in lookup()),
212 * Require that Q_QUOTAON handles the vfs_busy() reference on
213 * its own, always returning with ubusied mount point.
215 if ((uap->cmd >> SUBCMDSHIFT) != Q_QUOTAON)
221 * Used by statfs conversion routines to scale the block size up if
222 * necessary so that all of the block counts are <= 'max_size'. Note
223 * that 'max_size' should be a bitmask, i.e. 2^n - 1 for some non-zero
227 statfs_scale_blocks(struct statfs *sf, long max_size)
232 KASSERT(powerof2(max_size + 1), ("%s: invalid max_size", __func__));
235 * Attempt to scale the block counts to give a more accurate
236 * overview to userland of the ratio of free space to used
237 * space. To do this, find the largest block count and compute
238 * a divisor that lets it fit into a signed integer <= max_size.
240 if (sf->f_bavail < 0)
241 count = -sf->f_bavail;
243 count = sf->f_bavail;
244 count = MAX(sf->f_blocks, MAX(sf->f_bfree, count));
245 if (count <= max_size)
248 count >>= flsl(max_size);
255 sf->f_bsize <<= shift;
256 sf->f_blocks >>= shift;
257 sf->f_bfree >>= shift;
258 sf->f_bavail >>= shift;
262 * Get filesystem statistics.
264 #ifndef _SYS_SYSPROTO_H_
273 register struct statfs_args /* {
281 error = kern_statfs(td, uap->path, UIO_USERSPACE, &sf);
283 error = copyout(&sf, uap->buf, sizeof(sf));
288 kern_statfs(struct thread *td, char *path, enum uio_seg pathseg,
292 struct statfs *sp, sb;
296 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
301 mp = nd.ni_vp->v_mount;
303 NDFREE(&nd, NDF_ONLY_PNBUF);
305 error = vfs_busy(mp, 0);
310 error = mac_mount_check_stat(td->td_ucred, mp);
315 * Set these in case the underlying filesystem fails to do so.
318 sp->f_version = STATFS_VERSION;
319 sp->f_namemax = NAME_MAX;
320 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
321 error = VFS_STATFS(mp, sp);
324 if (priv_check(td, PRIV_VFS_GENERATION)) {
325 bcopy(sp, &sb, sizeof(sb));
326 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
327 prison_enforce_statfs(td->td_ucred, mp, &sb);
337 * Get filesystem statistics.
339 #ifndef _SYS_SYSPROTO_H_
340 struct fstatfs_args {
348 register struct fstatfs_args /* {
356 error = kern_fstatfs(td, uap->fd, &sf);
358 error = copyout(&sf, uap->buf, sizeof(sf));
363 kern_fstatfs(struct thread *td, int fd, struct statfs *buf)
367 struct statfs *sp, sb;
373 error = getvnode(td->td_proc->p_fd, fd,
374 cap_rights_init(&rights, CAP_FSTATFS), &fp);
378 vn_lock(vp, LK_SHARED | LK_RETRY);
380 AUDIT_ARG_VNODE1(vp);
391 error = vfs_busy(mp, 0);
396 error = mac_mount_check_stat(td->td_ucred, mp);
401 * Set these in case the underlying filesystem fails to do so.
404 sp->f_version = STATFS_VERSION;
405 sp->f_namemax = NAME_MAX;
406 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
407 error = VFS_STATFS(mp, sp);
410 if (priv_check(td, PRIV_VFS_GENERATION)) {
411 bcopy(sp, &sb, sizeof(sb));
412 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
413 prison_enforce_statfs(td->td_ucred, mp, &sb);
424 * Get statistics on all filesystems.
426 #ifndef _SYS_SYSPROTO_H_
427 struct getfsstat_args {
434 sys_getfsstat(td, uap)
436 register struct getfsstat_args /* {
443 return (kern_getfsstat(td, &uap->buf, uap->bufsize, UIO_USERSPACE,
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 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)
541 td->td_retval[0] = maxcount;
543 td->td_retval[0] = count;
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 count = uap->bufsize / sizeof(struct ostatfs);
631 size = count * sizeof(struct statfs);
632 error = kern_getfsstat(td, &buf, size, UIO_SYSSPACE, uap->flags);
634 count = td->td_retval[0];
636 while (count > 0 && error == 0) {
638 error = copyout(&osb, uap->buf, sizeof(osb));
649 * Implement fstatfs() for (NFS) file handles.
651 #ifndef _SYS_SYSPROTO_H_
652 struct freebsd4_fhstatfs_args {
653 struct fhandle *u_fhp;
658 freebsd4_fhstatfs(td, uap)
660 struct freebsd4_fhstatfs_args /* {
661 struct fhandle *u_fhp;
670 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
673 error = kern_fhstatfs(td, fh, &sf);
676 cvtstatfs(&sf, &osb);
677 return (copyout(&osb, uap->buf, sizeof(osb)));
681 * Convert a new format statfs structure to an old format statfs structure.
689 statfs_scale_blocks(nsp, LONG_MAX);
690 bzero(osp, sizeof(*osp));
691 osp->f_bsize = nsp->f_bsize;
692 osp->f_iosize = MIN(nsp->f_iosize, LONG_MAX);
693 osp->f_blocks = nsp->f_blocks;
694 osp->f_bfree = nsp->f_bfree;
695 osp->f_bavail = nsp->f_bavail;
696 osp->f_files = MIN(nsp->f_files, LONG_MAX);
697 osp->f_ffree = MIN(nsp->f_ffree, LONG_MAX);
698 osp->f_owner = nsp->f_owner;
699 osp->f_type = nsp->f_type;
700 osp->f_flags = nsp->f_flags;
701 osp->f_syncwrites = MIN(nsp->f_syncwrites, LONG_MAX);
702 osp->f_asyncwrites = MIN(nsp->f_asyncwrites, LONG_MAX);
703 osp->f_syncreads = MIN(nsp->f_syncreads, LONG_MAX);
704 osp->f_asyncreads = MIN(nsp->f_asyncreads, LONG_MAX);
705 strlcpy(osp->f_fstypename, nsp->f_fstypename,
706 MIN(MFSNAMELEN, OMFSNAMELEN));
707 strlcpy(osp->f_mntonname, nsp->f_mntonname,
708 MIN(MNAMELEN, OMNAMELEN));
709 strlcpy(osp->f_mntfromname, nsp->f_mntfromname,
710 MIN(MNAMELEN, OMNAMELEN));
711 osp->f_fsid = nsp->f_fsid;
713 #endif /* COMPAT_FREEBSD4 */
716 * Change current working directory to a given file descriptor.
718 #ifndef _SYS_SYSPROTO_H_
726 struct fchdir_args /* {
730 register struct filedesc *fdp = td->td_proc->p_fd;
731 struct vnode *vp, *tdp, *vpold;
737 AUDIT_ARG_FD(uap->fd);
738 error = getvnode(fdp, uap->fd, cap_rights_init(&rights, CAP_FCHDIR),
745 vn_lock(vp, LK_SHARED | LK_RETRY);
746 AUDIT_ARG_VNODE1(vp);
747 error = change_dir(vp, td);
748 while (!error && (mp = vp->v_mountedhere) != NULL) {
751 error = VFS_ROOT(mp, LK_SHARED, &tdp);
764 vpold = fdp->fd_cdir;
766 FILEDESC_XUNLOCK(fdp);
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)
793 register struct filedesc *fdp = td->td_proc->p_fd;
798 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
800 if ((error = namei(&nd)) != 0)
802 if ((error = change_dir(nd.ni_vp, td)) != 0) {
804 NDFREE(&nd, NDF_ONLY_PNBUF);
807 VOP_UNLOCK(nd.ni_vp, 0);
808 NDFREE(&nd, NDF_ONLY_PNBUF);
811 fdp->fd_cdir = nd.ni_vp;
812 FILEDESC_XUNLOCK(fdp);
818 * Helper function for raised chroot(2) security function: Refuse if
819 * any filedescriptors are open directories.
822 chroot_refuse_vdir_fds(fdp)
823 struct filedesc *fdp;
829 FILEDESC_LOCK_ASSERT(fdp);
831 for (fd = 0; fd < fdp->fd_nfiles ; fd++) {
832 fp = fget_locked(fdp, fd);
835 if (fp->f_type == DTYPE_VNODE) {
837 if (vp->v_type == VDIR)
845 * This sysctl determines if we will allow a process to chroot(2) if it
846 * has a directory open:
847 * 0: disallowed for all processes.
848 * 1: allowed for processes that were not already chroot(2)'ed.
849 * 2: allowed for all processes.
852 static int chroot_allow_open_directories = 1;
854 SYSCTL_INT(_kern, OID_AUTO, chroot_allow_open_directories, CTLFLAG_RW,
855 &chroot_allow_open_directories, 0,
856 "Allow a process to chroot(2) if it has a directory open");
859 * Change notion of root (``/'') directory.
861 #ifndef _SYS_SYSPROTO_H_
869 struct chroot_args /* {
876 error = priv_check(td, PRIV_VFS_CHROOT);
879 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
880 UIO_USERSPACE, uap->path, td);
884 error = change_dir(nd.ni_vp, td);
888 error = mac_vnode_check_chroot(td->td_ucred, nd.ni_vp);
892 VOP_UNLOCK(nd.ni_vp, 0);
893 error = change_root(nd.ni_vp, td);
895 NDFREE(&nd, NDF_ONLY_PNBUF);
900 NDFREE(&nd, NDF_ONLY_PNBUF);
905 * Common routine for chroot and chdir. Callers must provide a locked vnode
917 ASSERT_VOP_LOCKED(vp, "change_dir(): vp not locked");
918 if (vp->v_type != VDIR)
921 error = mac_vnode_check_chdir(td->td_ucred, vp);
925 return (VOP_ACCESS(vp, VEXEC, td->td_ucred, td));
929 * Common routine for kern_chroot() and jail_attach(). The caller is
930 * responsible for invoking priv_check() and mac_vnode_check_chroot() to
931 * authorize this operation.
938 struct filedesc *fdp;
942 fdp = td->td_proc->p_fd;
944 if (chroot_allow_open_directories == 0 ||
945 (chroot_allow_open_directories == 1 && fdp->fd_rdir != rootvnode)) {
946 error = chroot_refuse_vdir_fds(fdp);
948 FILEDESC_XUNLOCK(fdp);
952 oldvp = fdp->fd_rdir;
959 FILEDESC_XUNLOCK(fdp);
965 flags_to_rights(int flags, cap_rights_t *rightsp)
968 if (flags & O_EXEC) {
969 cap_rights_set(rightsp, CAP_FEXECVE);
971 switch ((flags & O_ACCMODE)) {
973 cap_rights_set(rightsp, CAP_READ);
976 cap_rights_set(rightsp, CAP_READ);
979 cap_rights_set(rightsp, CAP_WRITE);
980 if (!(flags & (O_APPEND | O_TRUNC)))
981 cap_rights_set(rightsp, CAP_SEEK);
987 cap_rights_set(rightsp, CAP_CREATE);
990 cap_rights_set(rightsp, CAP_FTRUNCATE);
992 if (flags & (O_SYNC | O_FSYNC))
993 cap_rights_set(rightsp, CAP_FSYNC);
995 if (flags & (O_EXLOCK | O_SHLOCK))
996 cap_rights_set(rightsp, CAP_FLOCK);
1000 * Check permissions, allocate an open file structure, and call the device
1001 * open routine if any.
1003 #ifndef _SYS_SYSPROTO_H_
1013 register struct open_args /* {
1020 return (kern_open(td, uap->path, UIO_USERSPACE, uap->flags, uap->mode));
1023 #ifndef _SYS_SYSPROTO_H_
1024 struct openat_args {
1032 sys_openat(struct thread *td, struct openat_args *uap)
1035 return (kern_openat(td, uap->fd, uap->path, UIO_USERSPACE, uap->flag,
1040 kern_open(struct thread *td, char *path, enum uio_seg pathseg, int flags,
1044 return (kern_openat(td, AT_FDCWD, path, pathseg, flags, mode));
1048 kern_openat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1049 int flags, int mode)
1051 struct proc *p = td->td_proc;
1052 struct filedesc *fdp = p->p_fd;
1055 struct nameidata nd;
1056 cap_rights_t rights;
1057 int cmode, error, indx;
1061 AUDIT_ARG_FFLAGS(flags);
1062 AUDIT_ARG_MODE(mode);
1063 /* XXX: audit dirfd */
1064 cap_rights_init(&rights, CAP_LOOKUP);
1065 flags_to_rights(flags, &rights);
1067 * Only one of the O_EXEC, O_RDONLY, O_WRONLY and O_RDWR flags
1070 if (flags & O_EXEC) {
1071 if (flags & O_ACCMODE)
1073 } else if ((flags & O_ACCMODE) == O_ACCMODE) {
1076 flags = FFLAGS(flags);
1080 * Allocate the file descriptor, but don't install a descriptor yet.
1082 error = falloc_noinstall(td, &fp);
1086 * An extra reference on `fp' has been held for us by
1087 * falloc_noinstall().
1089 /* Set the flags early so the finit in devfs can pick them up. */
1090 fp->f_flag = flags & FMASK;
1091 cmode = ((mode & ~fdp->fd_cmask) & ALLPERMS) & ~S_ISTXT;
1092 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, fd,
1094 td->td_dupfd = -1; /* XXX check for fdopen */
1095 error = vn_open(&nd, &flags, cmode, fp);
1098 * If the vn_open replaced the method vector, something
1099 * wonderous happened deep below and we just pass it up
1100 * pretending we know what we do.
1102 if (error == ENXIO && fp->f_ops != &badfileops)
1106 * Handle special fdopen() case. bleh.
1108 * Don't do this for relative (capability) lookups; we don't
1109 * understand exactly what would happen, and we don't think
1110 * that it ever should.
1112 if (nd.ni_strictrelative == 0 &&
1113 (error == ENODEV || error == ENXIO) &&
1114 td->td_dupfd >= 0) {
1115 error = dupfdopen(td, fdp, td->td_dupfd, flags, error,
1124 NDFREE(&nd, NDF_ONLY_PNBUF);
1128 * Store the vnode, for any f_type. Typically, the vnode use
1129 * count is decremented by direct call to vn_closefile() for
1130 * files that switched type in the cdevsw fdopen() method.
1134 * If the file wasn't claimed by devfs bind it to the normal
1135 * vnode operations here.
1137 if (fp->f_ops == &badfileops) {
1138 KASSERT(vp->v_type != VFIFO, ("Unexpected fifo."));
1140 finit(fp, (flags & FMASK) | (fp->f_flag & FHASLOCK),
1141 DTYPE_VNODE, vp, &vnops);
1145 if (flags & O_TRUNC) {
1146 error = fo_truncate(fp, 0, td->td_ucred, td);
1152 * If we haven't already installed the FD (for dupfdopen), do so now.
1155 struct filecaps *fcaps;
1158 if (nd.ni_strictrelative == 1)
1159 fcaps = &nd.ni_filecaps;
1163 error = finstall(td, fp, &indx, flags, fcaps);
1164 /* On success finstall() consumes fcaps. */
1166 filecaps_free(&nd.ni_filecaps);
1170 filecaps_free(&nd.ni_filecaps);
1174 * Release our private reference, leaving the one associated with
1175 * the descriptor table intact.
1178 td->td_retval[0] = indx;
1181 KASSERT(indx == -1, ("indx=%d, should be -1", indx));
1190 #ifndef _SYS_SYSPROTO_H_
1191 struct ocreat_args {
1199 register struct ocreat_args /* {
1205 return (kern_open(td, uap->path, UIO_USERSPACE,
1206 O_WRONLY | O_CREAT | O_TRUNC, uap->mode));
1208 #endif /* COMPAT_43 */
1211 * Create a special file.
1213 #ifndef _SYS_SYSPROTO_H_
1223 register struct mknod_args /* {
1230 return (kern_mknod(td, uap->path, UIO_USERSPACE, uap->mode, uap->dev));
1233 #ifndef _SYS_SYSPROTO_H_
1234 struct mknodat_args {
1242 sys_mknodat(struct thread *td, struct mknodat_args *uap)
1245 return (kern_mknodat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode,
1250 kern_mknod(struct thread *td, char *path, enum uio_seg pathseg, int mode,
1254 return (kern_mknodat(td, AT_FDCWD, path, pathseg, mode, dev));
1258 kern_mknodat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1264 struct nameidata nd;
1265 cap_rights_t rights;
1266 int error, whiteout = 0;
1268 AUDIT_ARG_MODE(mode);
1270 switch (mode & S_IFMT) {
1273 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
1276 error = priv_check(td, PRIV_VFS_MKNOD_BAD);
1279 error = priv_check(td, PRIV_VFS_MKNOD_WHT);
1283 return (kern_mkfifoat(td, fd, path, pathseg, mode));
1293 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1,
1294 pathseg, path, fd, cap_rights_init(&rights, CAP_MKNODAT), td);
1295 if ((error = namei(&nd)) != 0)
1299 NDFREE(&nd, NDF_ONLY_PNBUF);
1300 if (vp == nd.ni_dvp)
1308 vattr.va_mode = (mode & ALLPERMS) &
1309 ~td->td_proc->p_fd->fd_cmask;
1310 vattr.va_rdev = dev;
1313 switch (mode & S_IFMT) {
1314 case S_IFMT: /* used by badsect to flag bad sectors */
1315 vattr.va_type = VBAD;
1318 vattr.va_type = VCHR;
1321 vattr.va_type = VBLK;
1327 panic("kern_mknod: invalid mode");
1330 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1331 NDFREE(&nd, NDF_ONLY_PNBUF);
1333 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1338 if (error == 0 && !whiteout)
1339 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp,
1340 &nd.ni_cnd, &vattr);
1344 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, CREATE);
1346 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp,
1347 &nd.ni_cnd, &vattr);
1352 NDFREE(&nd, NDF_ONLY_PNBUF);
1354 vn_finished_write(mp);
1359 * Create a named pipe.
1361 #ifndef _SYS_SYSPROTO_H_
1362 struct mkfifo_args {
1370 register struct mkfifo_args /* {
1376 return (kern_mkfifo(td, uap->path, UIO_USERSPACE, uap->mode));
1379 #ifndef _SYS_SYSPROTO_H_
1380 struct mkfifoat_args {
1387 sys_mkfifoat(struct thread *td, struct mkfifoat_args *uap)
1390 return (kern_mkfifoat(td, uap->fd, uap->path, UIO_USERSPACE,
1395 kern_mkfifo(struct thread *td, char *path, enum uio_seg pathseg, int mode)
1398 return (kern_mkfifoat(td, AT_FDCWD, path, pathseg, mode));
1402 kern_mkfifoat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1407 struct nameidata nd;
1408 cap_rights_t rights;
1411 AUDIT_ARG_MODE(mode);
1414 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1,
1415 pathseg, path, fd, cap_rights_init(&rights, CAP_MKFIFOAT), td);
1416 if ((error = namei(&nd)) != 0)
1418 if (nd.ni_vp != NULL) {
1419 NDFREE(&nd, NDF_ONLY_PNBUF);
1420 if (nd.ni_vp == nd.ni_dvp)
1427 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1428 NDFREE(&nd, NDF_ONLY_PNBUF);
1430 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1435 vattr.va_type = VFIFO;
1436 vattr.va_mode = (mode & ALLPERMS) & ~td->td_proc->p_fd->fd_cmask;
1438 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1443 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
1450 vn_finished_write(mp);
1451 NDFREE(&nd, NDF_ONLY_PNBUF);
1456 * Make a hard file link.
1458 #ifndef _SYS_SYSPROTO_H_
1467 register struct link_args /* {
1473 return (kern_link(td, uap->path, uap->link, UIO_USERSPACE));
1476 #ifndef _SYS_SYSPROTO_H_
1477 struct linkat_args {
1486 sys_linkat(struct thread *td, struct linkat_args *uap)
1491 if (flag & ~AT_SYMLINK_FOLLOW)
1494 return (kern_linkat(td, uap->fd1, uap->fd2, uap->path1, uap->path2,
1495 UIO_USERSPACE, (flag & AT_SYMLINK_FOLLOW) ? FOLLOW : NOFOLLOW));
1498 int hardlink_check_uid = 0;
1499 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
1500 &hardlink_check_uid, 0,
1501 "Unprivileged processes cannot create hard links to files owned by other "
1503 static int hardlink_check_gid = 0;
1504 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
1505 &hardlink_check_gid, 0,
1506 "Unprivileged processes cannot create hard links to files owned by other "
1510 can_hardlink(struct vnode *vp, struct ucred *cred)
1515 if (!hardlink_check_uid && !hardlink_check_gid)
1518 error = VOP_GETATTR(vp, &va, cred);
1522 if (hardlink_check_uid && cred->cr_uid != va.va_uid) {
1523 error = priv_check_cred(cred, PRIV_VFS_LINK, 0);
1528 if (hardlink_check_gid && !groupmember(va.va_gid, cred)) {
1529 error = priv_check_cred(cred, PRIV_VFS_LINK, 0);
1538 kern_link(struct thread *td, char *path, char *link, enum uio_seg segflg)
1541 return (kern_linkat(td, AT_FDCWD, AT_FDCWD, path,link, segflg, FOLLOW));
1545 kern_linkat(struct thread *td, int fd1, int fd2, char *path1, char *path2,
1546 enum uio_seg segflg, int follow)
1550 struct nameidata nd;
1551 cap_rights_t rights;
1555 NDINIT_AT(&nd, LOOKUP, follow | AUDITVNODE1, segflg, path1, fd1, td);
1557 if ((error = namei(&nd)) != 0)
1559 NDFREE(&nd, NDF_ONLY_PNBUF);
1561 if (vp->v_type == VDIR) {
1563 return (EPERM); /* POSIX */
1565 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0) {
1569 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE2,
1570 segflg, path2, fd2, cap_rights_init(&rights, CAP_LINKAT), td);
1571 if ((error = namei(&nd)) == 0) {
1572 if (nd.ni_vp != NULL) {
1573 if (nd.ni_dvp == nd.ni_vp)
1579 } else if ((error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY))
1581 error = can_hardlink(vp, td->td_ucred);
1584 error = mac_vnode_check_link(td->td_ucred,
1585 nd.ni_dvp, vp, &nd.ni_cnd);
1588 error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
1592 NDFREE(&nd, NDF_ONLY_PNBUF);
1595 vn_finished_write(mp);
1600 * Make a symbolic link.
1602 #ifndef _SYS_SYSPROTO_H_
1603 struct symlink_args {
1609 sys_symlink(td, uap)
1611 register struct symlink_args /* {
1617 return (kern_symlink(td, uap->path, uap->link, UIO_USERSPACE));
1620 #ifndef _SYS_SYSPROTO_H_
1621 struct symlinkat_args {
1628 sys_symlinkat(struct thread *td, struct symlinkat_args *uap)
1631 return (kern_symlinkat(td, uap->path1, uap->fd, uap->path2,
1636 kern_symlink(struct thread *td, char *path, char *link, enum uio_seg segflg)
1639 return (kern_symlinkat(td, path, AT_FDCWD, link, segflg));
1643 kern_symlinkat(struct thread *td, char *path1, int fd, char *path2,
1644 enum uio_seg segflg)
1649 struct nameidata nd;
1651 cap_rights_t rights;
1653 if (segflg == UIO_SYSSPACE) {
1656 syspath = uma_zalloc(namei_zone, M_WAITOK);
1657 if ((error = copyinstr(path1, syspath, MAXPATHLEN, NULL)) != 0)
1660 AUDIT_ARG_TEXT(syspath);
1663 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1,
1664 segflg, path2, fd, cap_rights_init(&rights, CAP_SYMLINKAT), td);
1665 if ((error = namei(&nd)) != 0)
1668 NDFREE(&nd, NDF_ONLY_PNBUF);
1669 if (nd.ni_vp == nd.ni_dvp)
1677 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1678 NDFREE(&nd, NDF_ONLY_PNBUF);
1680 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1685 vattr.va_mode = ACCESSPERMS &~ td->td_proc->p_fd->fd_cmask;
1687 vattr.va_type = VLNK;
1688 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1693 error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr, syspath);
1699 NDFREE(&nd, NDF_ONLY_PNBUF);
1701 vn_finished_write(mp);
1703 if (segflg != UIO_SYSSPACE)
1704 uma_zfree(namei_zone, syspath);
1709 * Delete a whiteout from the filesystem.
1712 sys_undelete(td, uap)
1714 register struct undelete_args /* {
1719 struct nameidata nd;
1724 NDINIT(&nd, DELETE, LOCKPARENT | DOWHITEOUT | AUDITVNODE1,
1725 UIO_USERSPACE, uap->path, td);
1730 if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & ISWHITEOUT)) {
1731 NDFREE(&nd, NDF_ONLY_PNBUF);
1732 if (nd.ni_vp == nd.ni_dvp)
1740 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1741 NDFREE(&nd, NDF_ONLY_PNBUF);
1743 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1747 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, DELETE);
1748 NDFREE(&nd, NDF_ONLY_PNBUF);
1750 vn_finished_write(mp);
1755 * Delete a name from the filesystem.
1757 #ifndef _SYS_SYSPROTO_H_
1758 struct unlink_args {
1765 struct unlink_args /* {
1770 return (kern_unlink(td, uap->path, UIO_USERSPACE));
1773 #ifndef _SYS_SYSPROTO_H_
1774 struct unlinkat_args {
1781 sys_unlinkat(struct thread *td, struct unlinkat_args *uap)
1783 int flag = uap->flag;
1785 char *path = uap->path;
1787 if (flag & ~AT_REMOVEDIR)
1790 if (flag & AT_REMOVEDIR)
1791 return (kern_rmdirat(td, fd, path, UIO_USERSPACE));
1793 return (kern_unlinkat(td, fd, path, UIO_USERSPACE, 0));
1797 kern_unlink(struct thread *td, char *path, enum uio_seg pathseg)
1800 return (kern_unlinkat(td, AT_FDCWD, path, pathseg, 0));
1804 kern_unlinkat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1809 struct nameidata nd;
1811 cap_rights_t rights;
1816 NDINIT_ATRIGHTS(&nd, DELETE, LOCKPARENT | LOCKLEAF | AUDITVNODE1,
1817 pathseg, path, fd, cap_rights_init(&rights, CAP_UNLINKAT), td);
1818 if ((error = namei(&nd)) != 0)
1819 return (error == EINVAL ? EPERM : error);
1821 if (vp->v_type == VDIR && oldinum == 0) {
1822 error = EPERM; /* POSIX */
1823 } else if (oldinum != 0 &&
1824 ((error = vn_stat(vp, &sb, td->td_ucred, NOCRED, td)) == 0) &&
1825 sb.st_ino != oldinum) {
1826 error = EIDRM; /* Identifier removed */
1829 * The root of a mounted filesystem cannot be deleted.
1831 * XXX: can this only be a VDIR case?
1833 if (vp->v_vflag & VV_ROOT)
1837 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1838 NDFREE(&nd, NDF_ONLY_PNBUF);
1840 if (vp == nd.ni_dvp)
1844 if ((error = vn_start_write(NULL, &mp,
1845 V_XSLEEP | PCATCH)) != 0)
1850 error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp,
1855 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_UNLINK);
1856 error = VOP_REMOVE(nd.ni_dvp, vp, &nd.ni_cnd);
1860 vn_finished_write(mp);
1862 NDFREE(&nd, NDF_ONLY_PNBUF);
1864 if (vp == nd.ni_dvp)
1872 * Reposition read/write file offset.
1874 #ifndef _SYS_SYSPROTO_H_
1885 register struct lseek_args /* {
1893 cap_rights_t rights;
1896 AUDIT_ARG_FD(uap->fd);
1897 error = fget(td, uap->fd, cap_rights_init(&rights, CAP_SEEK), &fp);
1900 error = (fp->f_ops->fo_flags & DFLAG_SEEKABLE) != 0 ?
1901 fo_seek(fp, uap->offset, uap->whence, td) : ESPIPE;
1906 #if defined(COMPAT_43)
1908 * Reposition read/write file offset.
1910 #ifndef _SYS_SYSPROTO_H_
1911 struct olseek_args {
1920 register struct olseek_args /* {
1926 struct lseek_args /* {
1934 nuap.offset = uap->offset;
1935 nuap.whence = uap->whence;
1936 return (sys_lseek(td, &nuap));
1938 #endif /* COMPAT_43 */
1940 /* Version with the 'pad' argument */
1942 freebsd6_lseek(td, uap)
1944 register struct freebsd6_lseek_args *uap;
1946 struct lseek_args ouap;
1949 ouap.offset = uap->offset;
1950 ouap.whence = uap->whence;
1951 return (sys_lseek(td, &ouap));
1955 * Check access permissions using passed credentials.
1958 vn_access(vp, user_flags, cred, td)
1967 /* Flags == 0 means only check for existence. */
1971 if (user_flags & R_OK)
1973 if (user_flags & W_OK)
1975 if (user_flags & X_OK)
1978 error = mac_vnode_check_access(cred, vp, accmode);
1982 if ((accmode & VWRITE) == 0 || (error = vn_writechk(vp)) == 0)
1983 error = VOP_ACCESS(vp, accmode, cred, td);
1989 * Check access permissions using "real" credentials.
1991 #ifndef _SYS_SYSPROTO_H_
1992 struct access_args {
2000 register struct access_args /* {
2006 return (kern_access(td, uap->path, UIO_USERSPACE, uap->amode));
2009 #ifndef _SYS_SYSPROTO_H_
2010 struct faccessat_args {
2018 sys_faccessat(struct thread *td, struct faccessat_args *uap)
2021 if (uap->flag & ~AT_EACCESS)
2023 return (kern_accessat(td, uap->fd, uap->path, UIO_USERSPACE, uap->flag,
2028 kern_access(struct thread *td, char *path, enum uio_seg pathseg, int amode)
2031 return (kern_accessat(td, AT_FDCWD, path, pathseg, 0, amode));
2035 kern_accessat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2036 int flag, int amode)
2038 struct ucred *cred, *tmpcred;
2040 struct nameidata nd;
2041 cap_rights_t rights;
2045 * Create and modify a temporary credential instead of one that
2046 * is potentially shared.
2048 if (!(flag & AT_EACCESS)) {
2049 cred = td->td_ucred;
2050 tmpcred = crdup(cred);
2051 tmpcred->cr_uid = cred->cr_ruid;
2052 tmpcred->cr_groups[0] = cred->cr_rgid;
2053 td->td_ucred = tmpcred;
2055 cred = tmpcred = td->td_ucred;
2056 AUDIT_ARG_VALUE(amode);
2057 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF |
2058 AUDITVNODE1, pathseg, path, fd, cap_rights_init(&rights, CAP_FSTAT),
2060 if ((error = namei(&nd)) != 0)
2064 error = vn_access(vp, amode, tmpcred, td);
2065 NDFREE(&nd, NDF_ONLY_PNBUF);
2068 if (!(flag & AT_EACCESS)) {
2069 td->td_ucred = cred;
2076 * Check access permissions using "effective" credentials.
2078 #ifndef _SYS_SYSPROTO_H_
2079 struct eaccess_args {
2085 sys_eaccess(td, uap)
2087 register struct eaccess_args /* {
2093 return (kern_eaccess(td, uap->path, UIO_USERSPACE, uap->amode));
2097 kern_eaccess(struct thread *td, char *path, enum uio_seg pathseg, int amode)
2100 return (kern_accessat(td, AT_FDCWD, path, pathseg, AT_EACCESS, amode));
2103 #if defined(COMPAT_43)
2105 * Get file status; this version follows links.
2107 #ifndef _SYS_SYSPROTO_H_
2116 register struct ostat_args /* {
2125 error = kern_stat(td, uap->path, UIO_USERSPACE, &sb);
2129 return (copyout(&osb, uap->ub, sizeof (osb)));
2133 * Get file status; this version does not follow links.
2135 #ifndef _SYS_SYSPROTO_H_
2136 struct olstat_args {
2144 register struct olstat_args /* {
2153 error = kern_lstat(td, uap->path, UIO_USERSPACE, &sb);
2157 return (copyout(&osb, uap->ub, sizeof (osb)));
2161 * Convert from an old to a new stat structure.
2169 ost->st_dev = st->st_dev;
2170 ost->st_ino = st->st_ino;
2171 ost->st_mode = st->st_mode;
2172 ost->st_nlink = st->st_nlink;
2173 ost->st_uid = st->st_uid;
2174 ost->st_gid = st->st_gid;
2175 ost->st_rdev = st->st_rdev;
2176 if (st->st_size < (quad_t)1 << 32)
2177 ost->st_size = st->st_size;
2180 ost->st_atim = st->st_atim;
2181 ost->st_mtim = st->st_mtim;
2182 ost->st_ctim = st->st_ctim;
2183 ost->st_blksize = st->st_blksize;
2184 ost->st_blocks = st->st_blocks;
2185 ost->st_flags = st->st_flags;
2186 ost->st_gen = st->st_gen;
2188 #endif /* COMPAT_43 */
2191 * Get file status; this version follows links.
2193 #ifndef _SYS_SYSPROTO_H_
2202 register struct stat_args /* {
2210 error = kern_stat(td, uap->path, UIO_USERSPACE, &sb);
2212 error = copyout(&sb, uap->ub, sizeof (sb));
2216 #ifndef _SYS_SYSPROTO_H_
2217 struct fstatat_args {
2225 sys_fstatat(struct thread *td, struct fstatat_args *uap)
2230 error = kern_statat(td, uap->flag, uap->fd, uap->path,
2231 UIO_USERSPACE, &sb);
2233 error = copyout(&sb, uap->buf, sizeof (sb));
2238 kern_stat(struct thread *td, char *path, enum uio_seg pathseg, struct stat *sbp)
2241 return (kern_statat(td, 0, AT_FDCWD, path, pathseg, sbp));
2245 kern_statat(struct thread *td, int flag, int fd, char *path,
2246 enum uio_seg pathseg, struct stat *sbp)
2249 return (kern_statat_vnhook(td, flag, fd, path, pathseg, sbp, NULL));
2253 kern_statat_vnhook(struct thread *td, int flag, int fd, char *path,
2254 enum uio_seg pathseg, struct stat *sbp,
2255 void (*hook)(struct vnode *vp, struct stat *sbp))
2257 struct nameidata nd;
2259 cap_rights_t rights;
2262 if (flag & ~AT_SYMLINK_NOFOLLOW)
2265 NDINIT_ATRIGHTS(&nd, LOOKUP, ((flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW :
2266 FOLLOW) | LOCKSHARED | LOCKLEAF | AUDITVNODE1, pathseg, path, fd,
2267 cap_rights_init(&rights, CAP_FSTAT), td);
2269 if ((error = namei(&nd)) != 0)
2271 error = vn_stat(nd.ni_vp, &sb, td->td_ucred, NOCRED, td);
2273 SDT_PROBE(vfs, , stat, mode, path, sb.st_mode, 0, 0, 0);
2274 if (S_ISREG(sb.st_mode))
2275 SDT_PROBE(vfs, , stat, reg, path, pathseg, 0, 0, 0);
2276 if (__predict_false(hook != NULL))
2277 hook(nd.ni_vp, &sb);
2279 NDFREE(&nd, NDF_ONLY_PNBUF);
2285 if (KTRPOINT(td, KTR_STRUCT))
2292 * Get file status; this version does not follow links.
2294 #ifndef _SYS_SYSPROTO_H_
2303 register struct lstat_args /* {
2311 error = kern_lstat(td, uap->path, UIO_USERSPACE, &sb);
2313 error = copyout(&sb, uap->ub, sizeof (sb));
2318 kern_lstat(struct thread *td, char *path, enum uio_seg pathseg, struct stat *sbp)
2321 return (kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, path, pathseg,
2326 * Implementation of the NetBSD [l]stat() functions.
2334 bzero(nsb, sizeof *nsb);
2335 nsb->st_dev = sb->st_dev;
2336 nsb->st_ino = sb->st_ino;
2337 nsb->st_mode = sb->st_mode;
2338 nsb->st_nlink = sb->st_nlink;
2339 nsb->st_uid = sb->st_uid;
2340 nsb->st_gid = sb->st_gid;
2341 nsb->st_rdev = sb->st_rdev;
2342 nsb->st_atim = sb->st_atim;
2343 nsb->st_mtim = sb->st_mtim;
2344 nsb->st_ctim = sb->st_ctim;
2345 nsb->st_size = sb->st_size;
2346 nsb->st_blocks = sb->st_blocks;
2347 nsb->st_blksize = sb->st_blksize;
2348 nsb->st_flags = sb->st_flags;
2349 nsb->st_gen = sb->st_gen;
2350 nsb->st_birthtim = sb->st_birthtim;
2353 #ifndef _SYS_SYSPROTO_H_
2362 register struct nstat_args /* {
2371 error = kern_stat(td, uap->path, UIO_USERSPACE, &sb);
2374 cvtnstat(&sb, &nsb);
2375 return (copyout(&nsb, uap->ub, sizeof (nsb)));
2379 * NetBSD lstat. Get file status; this version does not follow links.
2381 #ifndef _SYS_SYSPROTO_H_
2390 register struct nlstat_args /* {
2399 error = kern_lstat(td, uap->path, UIO_USERSPACE, &sb);
2402 cvtnstat(&sb, &nsb);
2403 return (copyout(&nsb, uap->ub, sizeof (nsb)));
2407 * Get configurable pathname variables.
2409 #ifndef _SYS_SYSPROTO_H_
2410 struct pathconf_args {
2416 sys_pathconf(td, uap)
2418 register struct pathconf_args /* {
2424 return (kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name, FOLLOW));
2427 #ifndef _SYS_SYSPROTO_H_
2428 struct lpathconf_args {
2434 sys_lpathconf(td, uap)
2436 register struct lpathconf_args /* {
2442 return (kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name,
2447 kern_pathconf(struct thread *td, char *path, enum uio_seg pathseg, int name,
2450 struct nameidata nd;
2453 NDINIT(&nd, LOOKUP, LOCKSHARED | LOCKLEAF | AUDITVNODE1 | flags,
2455 if ((error = namei(&nd)) != 0)
2457 NDFREE(&nd, NDF_ONLY_PNBUF);
2459 /* If asynchronous I/O is available, it works for all files. */
2460 if (name == _PC_ASYNC_IO)
2461 td->td_retval[0] = async_io_version;
2463 error = VOP_PATHCONF(nd.ni_vp, name, td->td_retval);
2469 * Return target name of a symbolic link.
2471 #ifndef _SYS_SYSPROTO_H_
2472 struct readlink_args {
2479 sys_readlink(td, uap)
2481 register struct readlink_args /* {
2488 return (kern_readlink(td, uap->path, UIO_USERSPACE, uap->buf,
2489 UIO_USERSPACE, uap->count));
2491 #ifndef _SYS_SYSPROTO_H_
2492 struct readlinkat_args {
2500 sys_readlinkat(struct thread *td, struct readlinkat_args *uap)
2503 return (kern_readlinkat(td, uap->fd, uap->path, UIO_USERSPACE,
2504 uap->buf, UIO_USERSPACE, uap->bufsize));
2508 kern_readlink(struct thread *td, char *path, enum uio_seg pathseg, char *buf,
2509 enum uio_seg bufseg, size_t count)
2512 return (kern_readlinkat(td, AT_FDCWD, path, pathseg, buf, bufseg,
2517 kern_readlinkat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2518 char *buf, enum uio_seg bufseg, size_t count)
2523 struct nameidata nd;
2526 if (count > IOSIZE_MAX)
2529 NDINIT_AT(&nd, LOOKUP, NOFOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
2530 pathseg, path, fd, td);
2532 if ((error = namei(&nd)) != 0)
2534 NDFREE(&nd, NDF_ONLY_PNBUF);
2537 error = mac_vnode_check_readlink(td->td_ucred, vp);
2543 if (vp->v_type != VLNK)
2546 aiov.iov_base = buf;
2547 aiov.iov_len = count;
2548 auio.uio_iov = &aiov;
2549 auio.uio_iovcnt = 1;
2550 auio.uio_offset = 0;
2551 auio.uio_rw = UIO_READ;
2552 auio.uio_segflg = bufseg;
2554 auio.uio_resid = count;
2555 error = VOP_READLINK(vp, &auio, td->td_ucred);
2558 td->td_retval[0] = count - auio.uio_resid;
2563 * Common implementation code for chflags() and fchflags().
2566 setfflags(td, vp, flags)
2575 /* We can't support the value matching VNOVAL. */
2576 if (flags == VNOVAL)
2577 return (EOPNOTSUPP);
2580 * Prevent non-root users from setting flags on devices. When
2581 * a device is reused, users can retain ownership of the device
2582 * if they are allowed to set flags and programs assume that
2583 * chown can't fail when done as root.
2585 if (vp->v_type == VCHR || vp->v_type == VBLK) {
2586 error = priv_check(td, PRIV_VFS_CHFLAGS_DEV);
2591 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2594 vattr.va_flags = flags;
2595 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2597 error = mac_vnode_check_setflags(td->td_ucred, vp, vattr.va_flags);
2600 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
2602 vn_finished_write(mp);
2607 * Change flags of a file given a path name.
2609 #ifndef _SYS_SYSPROTO_H_
2610 struct chflags_args {
2616 sys_chflags(td, uap)
2618 register struct chflags_args /* {
2624 return (kern_chflags(td, uap->path, UIO_USERSPACE, uap->flags));
2627 #ifndef _SYS_SYSPROTO_H_
2628 struct chflagsat_args {
2636 sys_chflagsat(struct thread *td, struct chflagsat_args *uap)
2639 const char *path = uap->path;
2640 u_long flags = uap->flags;
2641 int atflag = uap->atflag;
2643 if (atflag & ~AT_SYMLINK_NOFOLLOW)
2646 return (kern_chflagsat(td, fd, path, UIO_USERSPACE, flags, atflag));
2650 kern_chflags(struct thread *td, const char *path, enum uio_seg pathseg,
2654 return (kern_chflagsat(td, AT_FDCWD, path, pathseg, flags, 0));
2658 * Same as chflags() but doesn't follow symlinks.
2661 sys_lchflags(td, uap)
2663 register struct lchflags_args /* {
2669 return (kern_chflagsat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2670 uap->flags, AT_SYMLINK_NOFOLLOW));
2674 kern_chflagsat(struct thread *td, int fd, const char *path,
2675 enum uio_seg pathseg, u_long flags, int atflag)
2677 struct nameidata nd;
2678 cap_rights_t rights;
2681 AUDIT_ARG_FFLAGS(flags);
2682 follow = (atflag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
2683 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, pathseg, path, fd,
2684 cap_rights_init(&rights, CAP_FCHFLAGS), td);
2685 if ((error = namei(&nd)) != 0)
2687 NDFREE(&nd, NDF_ONLY_PNBUF);
2688 error = setfflags(td, nd.ni_vp, flags);
2694 * Change flags of a file given a file descriptor.
2696 #ifndef _SYS_SYSPROTO_H_
2697 struct fchflags_args {
2703 sys_fchflags(td, uap)
2705 register struct fchflags_args /* {
2711 cap_rights_t rights;
2714 AUDIT_ARG_FD(uap->fd);
2715 AUDIT_ARG_FFLAGS(uap->flags);
2716 error = getvnode(td->td_proc->p_fd, uap->fd,
2717 cap_rights_init(&rights, CAP_FCHFLAGS), &fp);
2721 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
2722 AUDIT_ARG_VNODE1(fp->f_vnode);
2723 VOP_UNLOCK(fp->f_vnode, 0);
2725 error = setfflags(td, fp->f_vnode, uap->flags);
2731 * Common implementation code for chmod(), lchmod() and fchmod().
2734 setfmode(td, cred, vp, mode)
2744 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2746 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2748 vattr.va_mode = mode & ALLPERMS;
2750 error = mac_vnode_check_setmode(cred, vp, vattr.va_mode);
2753 error = VOP_SETATTR(vp, &vattr, cred);
2755 vn_finished_write(mp);
2760 * Change mode of a file given path name.
2762 #ifndef _SYS_SYSPROTO_H_
2771 register struct chmod_args /* {
2777 return (kern_chmod(td, uap->path, UIO_USERSPACE, uap->mode));
2780 #ifndef _SYS_SYSPROTO_H_
2781 struct fchmodat_args {
2789 sys_fchmodat(struct thread *td, struct fchmodat_args *uap)
2791 int flag = uap->flag;
2793 char *path = uap->path;
2794 mode_t mode = uap->mode;
2796 if (flag & ~AT_SYMLINK_NOFOLLOW)
2799 return (kern_fchmodat(td, fd, path, UIO_USERSPACE, mode, flag));
2803 kern_chmod(struct thread *td, char *path, enum uio_seg pathseg, int mode)
2806 return (kern_fchmodat(td, AT_FDCWD, path, pathseg, mode, 0));
2810 * Change mode of a file given path name (don't follow links.)
2812 #ifndef _SYS_SYSPROTO_H_
2813 struct lchmod_args {
2821 register struct lchmod_args /* {
2827 return (kern_fchmodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2828 uap->mode, AT_SYMLINK_NOFOLLOW));
2832 kern_fchmodat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2833 mode_t mode, int flag)
2835 struct nameidata nd;
2836 cap_rights_t rights;
2839 AUDIT_ARG_MODE(mode);
2840 follow = (flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
2841 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, pathseg, path, fd,
2842 cap_rights_init(&rights, CAP_FCHMOD), td);
2843 if ((error = namei(&nd)) != 0)
2845 NDFREE(&nd, NDF_ONLY_PNBUF);
2846 error = setfmode(td, td->td_ucred, nd.ni_vp, mode);
2852 * Change mode of a file given a file descriptor.
2854 #ifndef _SYS_SYSPROTO_H_
2855 struct fchmod_args {
2861 sys_fchmod(struct thread *td, struct fchmod_args *uap)
2864 cap_rights_t rights;
2867 AUDIT_ARG_FD(uap->fd);
2868 AUDIT_ARG_MODE(uap->mode);
2870 error = fget(td, uap->fd, cap_rights_init(&rights, CAP_FCHMOD), &fp);
2873 error = fo_chmod(fp, uap->mode, td->td_ucred, td);
2879 * Common implementation for chown(), lchown(), and fchown()
2882 setfown(td, cred, vp, uid, gid)
2893 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2895 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2900 error = mac_vnode_check_setowner(cred, vp, vattr.va_uid,
2904 error = VOP_SETATTR(vp, &vattr, cred);
2906 vn_finished_write(mp);
2911 * Set ownership given a path name.
2913 #ifndef _SYS_SYSPROTO_H_
2923 register struct chown_args /* {
2930 return (kern_chown(td, uap->path, UIO_USERSPACE, uap->uid, uap->gid));
2933 #ifndef _SYS_SYSPROTO_H_
2934 struct fchownat_args {
2943 sys_fchownat(struct thread *td, struct fchownat_args *uap)
2948 if (flag & ~AT_SYMLINK_NOFOLLOW)
2951 return (kern_fchownat(td, uap->fd, uap->path, UIO_USERSPACE, uap->uid,
2952 uap->gid, uap->flag));
2956 kern_chown(struct thread *td, char *path, enum uio_seg pathseg, int uid,
2960 return (kern_fchownat(td, AT_FDCWD, path, pathseg, uid, gid, 0));
2964 kern_fchownat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2965 int uid, int gid, int flag)
2967 struct nameidata nd;
2968 cap_rights_t rights;
2971 AUDIT_ARG_OWNER(uid, gid);
2972 follow = (flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
2973 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, pathseg, path, fd,
2974 cap_rights_init(&rights, CAP_FCHOWN), td);
2976 if ((error = namei(&nd)) != 0)
2978 NDFREE(&nd, NDF_ONLY_PNBUF);
2979 error = setfown(td, td->td_ucred, nd.ni_vp, uid, gid);
2985 * Set ownership given a path name, do not cross symlinks.
2987 #ifndef _SYS_SYSPROTO_H_
2988 struct lchown_args {
2997 register struct lchown_args /* {
3004 return (kern_lchown(td, uap->path, UIO_USERSPACE, uap->uid, uap->gid));
3008 kern_lchown(struct thread *td, char *path, enum uio_seg pathseg, int uid,
3012 return (kern_fchownat(td, AT_FDCWD, path, pathseg, uid, gid,
3013 AT_SYMLINK_NOFOLLOW));
3017 * Set ownership given a file descriptor.
3019 #ifndef _SYS_SYSPROTO_H_
3020 struct fchown_args {
3029 register struct fchown_args /* {
3036 cap_rights_t rights;
3039 AUDIT_ARG_FD(uap->fd);
3040 AUDIT_ARG_OWNER(uap->uid, uap->gid);
3041 error = fget(td, uap->fd, cap_rights_init(&rights, CAP_FCHOWN), &fp);
3044 error = fo_chown(fp, uap->uid, uap->gid, td->td_ucred, td);
3050 * Common implementation code for utimes(), lutimes(), and futimes().
3053 getutimes(usrtvp, tvpseg, tsp)
3054 const struct timeval *usrtvp;
3055 enum uio_seg tvpseg;
3056 struct timespec *tsp;
3058 struct timeval tv[2];
3059 const struct timeval *tvp;
3062 if (usrtvp == NULL) {
3063 vfs_timestamp(&tsp[0]);
3066 if (tvpseg == UIO_SYSSPACE) {
3069 if ((error = copyin(usrtvp, tv, sizeof(tv))) != 0)
3074 if (tvp[0].tv_usec < 0 || tvp[0].tv_usec >= 1000000 ||
3075 tvp[1].tv_usec < 0 || tvp[1].tv_usec >= 1000000)
3077 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
3078 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
3084 * Common implementation code for utimes(), lutimes(), and futimes().
3087 setutimes(td, vp, ts, numtimes, nullflag)
3090 const struct timespec *ts;
3096 int error, setbirthtime;
3098 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
3100 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3102 if (numtimes < 3 && !VOP_GETATTR(vp, &vattr, td->td_ucred) &&
3103 timespeccmp(&ts[1], &vattr.va_birthtime, < ))
3106 vattr.va_atime = ts[0];
3107 vattr.va_mtime = ts[1];
3109 vattr.va_birthtime = ts[1];
3111 vattr.va_birthtime = ts[2];
3113 vattr.va_vaflags |= VA_UTIMES_NULL;
3115 error = mac_vnode_check_setutimes(td->td_ucred, vp, vattr.va_atime,
3119 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3121 vn_finished_write(mp);
3126 * Set the access and modification times of a file.
3128 #ifndef _SYS_SYSPROTO_H_
3129 struct utimes_args {
3131 struct timeval *tptr;
3137 register struct utimes_args /* {
3139 struct timeval *tptr;
3143 return (kern_utimes(td, uap->path, UIO_USERSPACE, uap->tptr,
3147 #ifndef _SYS_SYSPROTO_H_
3148 struct futimesat_args {
3151 const struct timeval * times;
3155 sys_futimesat(struct thread *td, struct futimesat_args *uap)
3158 return (kern_utimesat(td, uap->fd, uap->path, UIO_USERSPACE,
3159 uap->times, UIO_USERSPACE));
3163 kern_utimes(struct thread *td, char *path, enum uio_seg pathseg,
3164 struct timeval *tptr, enum uio_seg tptrseg)
3167 return (kern_utimesat(td, AT_FDCWD, path, pathseg, tptr, tptrseg));
3171 kern_utimesat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
3172 struct timeval *tptr, enum uio_seg tptrseg)
3174 struct nameidata nd;
3175 struct timespec ts[2];
3176 cap_rights_t rights;
3179 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3181 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, fd,
3182 cap_rights_init(&rights, CAP_FUTIMES), td);
3184 if ((error = namei(&nd)) != 0)
3186 NDFREE(&nd, NDF_ONLY_PNBUF);
3187 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3193 * Set the access and modification times of a file.
3195 #ifndef _SYS_SYSPROTO_H_
3196 struct lutimes_args {
3198 struct timeval *tptr;
3202 sys_lutimes(td, uap)
3204 register struct lutimes_args /* {
3206 struct timeval *tptr;
3210 return (kern_lutimes(td, uap->path, UIO_USERSPACE, uap->tptr,
3215 kern_lutimes(struct thread *td, char *path, enum uio_seg pathseg,
3216 struct timeval *tptr, enum uio_seg tptrseg)
3218 struct timespec ts[2];
3219 struct nameidata nd;
3222 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3224 NDINIT(&nd, LOOKUP, NOFOLLOW | AUDITVNODE1, pathseg, path, td);
3225 if ((error = namei(&nd)) != 0)
3227 NDFREE(&nd, NDF_ONLY_PNBUF);
3228 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3234 * Set the access and modification times of a file.
3236 #ifndef _SYS_SYSPROTO_H_
3237 struct futimes_args {
3239 struct timeval *tptr;
3243 sys_futimes(td, uap)
3245 register struct futimes_args /* {
3247 struct timeval *tptr;
3251 return (kern_futimes(td, uap->fd, uap->tptr, UIO_USERSPACE));
3255 kern_futimes(struct thread *td, int fd, struct timeval *tptr,
3256 enum uio_seg tptrseg)
3258 struct timespec ts[2];
3260 cap_rights_t rights;
3264 error = getutimes(tptr, tptrseg, ts);
3267 error = getvnode(td->td_proc->p_fd, fd,
3268 cap_rights_init(&rights, CAP_FUTIMES), &fp);
3272 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
3273 AUDIT_ARG_VNODE1(fp->f_vnode);
3274 VOP_UNLOCK(fp->f_vnode, 0);
3276 error = setutimes(td, fp->f_vnode, ts, 2, tptr == NULL);
3282 * Truncate a file given its path name.
3284 #ifndef _SYS_SYSPROTO_H_
3285 struct truncate_args {
3292 sys_truncate(td, uap)
3294 register struct truncate_args /* {
3301 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3305 kern_truncate(struct thread *td, char *path, enum uio_seg pathseg, off_t length)
3311 struct nameidata nd;
3316 NDINIT(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, td);
3317 if ((error = namei(&nd)) != 0)
3320 rl_cookie = vn_rangelock_wlock(vp, 0, OFF_MAX);
3321 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0) {
3322 vn_rangelock_unlock(vp, rl_cookie);
3326 NDFREE(&nd, NDF_ONLY_PNBUF);
3327 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3328 if (vp->v_type == VDIR)
3331 else if ((error = mac_vnode_check_write(td->td_ucred, NOCRED, vp))) {
3334 else if ((error = vn_writechk(vp)) == 0 &&
3335 (error = VOP_ACCESS(vp, VWRITE, td->td_ucred, td)) == 0) {
3337 vattr.va_size = length;
3338 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3341 vn_finished_write(mp);
3342 vn_rangelock_unlock(vp, rl_cookie);
3347 #if defined(COMPAT_43)
3349 * Truncate a file given its path name.
3351 #ifndef _SYS_SYSPROTO_H_
3352 struct otruncate_args {
3360 register struct otruncate_args /* {
3365 struct truncate_args /* {
3371 nuap.path = uap->path;
3372 nuap.length = uap->length;
3373 return (sys_truncate(td, &nuap));
3375 #endif /* COMPAT_43 */
3377 /* Versions with the pad argument */
3379 freebsd6_truncate(struct thread *td, struct freebsd6_truncate_args *uap)
3381 struct truncate_args ouap;
3383 ouap.path = uap->path;
3384 ouap.length = uap->length;
3385 return (sys_truncate(td, &ouap));
3389 freebsd6_ftruncate(struct thread *td, struct freebsd6_ftruncate_args *uap)
3391 struct ftruncate_args ouap;
3394 ouap.length = uap->length;
3395 return (sys_ftruncate(td, &ouap));
3399 * Sync an open file.
3401 #ifndef _SYS_SYSPROTO_H_
3409 struct fsync_args /* {
3416 cap_rights_t rights;
3417 int error, lock_flags;
3419 AUDIT_ARG_FD(uap->fd);
3420 error = getvnode(td->td_proc->p_fd, uap->fd,
3421 cap_rights_init(&rights, CAP_FSYNC), &fp);
3425 error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
3428 if (MNT_SHARED_WRITES(mp) ||
3429 ((mp == NULL) && MNT_SHARED_WRITES(vp->v_mount))) {
3430 lock_flags = LK_SHARED;
3432 lock_flags = LK_EXCLUSIVE;
3434 vn_lock(vp, lock_flags | LK_RETRY);
3435 AUDIT_ARG_VNODE1(vp);
3436 if (vp->v_object != NULL) {
3437 VM_OBJECT_WLOCK(vp->v_object);
3438 vm_object_page_clean(vp->v_object, 0, 0, 0);
3439 VM_OBJECT_WUNLOCK(vp->v_object);
3441 error = VOP_FSYNC(vp, MNT_WAIT, td);
3444 vn_finished_write(mp);
3451 * Rename files. Source and destination must either both be directories, or
3452 * both not be directories. If target is a directory, it must be empty.
3454 #ifndef _SYS_SYSPROTO_H_
3455 struct rename_args {
3463 register struct rename_args /* {
3469 return (kern_rename(td, uap->from, uap->to, UIO_USERSPACE));
3472 #ifndef _SYS_SYSPROTO_H_
3473 struct renameat_args {
3481 sys_renameat(struct thread *td, struct renameat_args *uap)
3484 return (kern_renameat(td, uap->oldfd, uap->old, uap->newfd, uap->new,
3489 kern_rename(struct thread *td, char *from, char *to, enum uio_seg pathseg)
3492 return (kern_renameat(td, AT_FDCWD, from, AT_FDCWD, to, pathseg));
3496 kern_renameat(struct thread *td, int oldfd, char *old, int newfd, char *new,
3497 enum uio_seg pathseg)
3499 struct mount *mp = NULL;
3500 struct vnode *tvp, *fvp, *tdvp;
3501 struct nameidata fromnd, tond;
3502 cap_rights_t rights;
3507 NDINIT_ATRIGHTS(&fromnd, DELETE, LOCKPARENT | LOCKLEAF | SAVESTART |
3508 AUDITVNODE1, pathseg, old, oldfd,
3509 cap_rights_init(&rights, CAP_RENAMEAT), td);
3511 NDINIT_ATRIGHTS(&fromnd, DELETE, WANTPARENT | SAVESTART | AUDITVNODE1,
3512 pathseg, old, oldfd, cap_rights_init(&rights, CAP_RENAMEAT), td);
3515 if ((error = namei(&fromnd)) != 0)
3518 error = mac_vnode_check_rename_from(td->td_ucred, fromnd.ni_dvp,
3519 fromnd.ni_vp, &fromnd.ni_cnd);
3520 VOP_UNLOCK(fromnd.ni_dvp, 0);
3521 if (fromnd.ni_dvp != fromnd.ni_vp)
3522 VOP_UNLOCK(fromnd.ni_vp, 0);
3526 error = vn_start_write(fvp, &mp, V_WAIT | PCATCH);
3528 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3529 vrele(fromnd.ni_dvp);
3533 NDINIT_ATRIGHTS(&tond, RENAME, LOCKPARENT | LOCKLEAF | NOCACHE |
3534 SAVESTART | AUDITVNODE2, pathseg, new, newfd,
3535 cap_rights_init(&rights, CAP_LINKAT), td);
3536 if (fromnd.ni_vp->v_type == VDIR)
3537 tond.ni_cnd.cn_flags |= WILLBEDIR;
3538 if ((error = namei(&tond)) != 0) {
3539 /* Translate error code for rename("dir1", "dir2/."). */
3540 if (error == EISDIR && fvp->v_type == VDIR)
3542 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3543 vrele(fromnd.ni_dvp);
3545 vn_finished_write(mp);
3551 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
3554 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
3559 if (newfd != AT_FDCWD) {
3561 * If the target already exists we require CAP_UNLINKAT
3564 error = cap_check(&tond.ni_filecaps.fc_rights,
3565 cap_rights_init(&rights, CAP_UNLINKAT));
3576 * If the source is the same as the destination (that is, if they
3577 * are links to the same vnode), then there is nothing to do.
3583 error = mac_vnode_check_rename_to(td->td_ucred, tdvp,
3584 tond.ni_vp, fromnd.ni_dvp == tdvp, &tond.ni_cnd);
3588 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
3589 tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
3590 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3591 NDFREE(&tond, NDF_ONLY_PNBUF);
3593 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3594 NDFREE(&tond, NDF_ONLY_PNBUF);
3601 vrele(fromnd.ni_dvp);
3604 vrele(tond.ni_startdir);
3605 vn_finished_write(mp);
3607 if (fromnd.ni_startdir)
3608 vrele(fromnd.ni_startdir);
3615 * Make a directory file.
3617 #ifndef _SYS_SYSPROTO_H_
3626 register struct mkdir_args /* {
3632 return (kern_mkdir(td, uap->path, UIO_USERSPACE, uap->mode));
3635 #ifndef _SYS_SYSPROTO_H_
3636 struct mkdirat_args {
3643 sys_mkdirat(struct thread *td, struct mkdirat_args *uap)
3646 return (kern_mkdirat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode));
3650 kern_mkdir(struct thread *td, char *path, enum uio_seg segflg, int mode)
3653 return (kern_mkdirat(td, AT_FDCWD, path, segflg, mode));
3657 kern_mkdirat(struct thread *td, int fd, char *path, enum uio_seg segflg,
3663 struct nameidata nd;
3664 cap_rights_t rights;
3667 AUDIT_ARG_MODE(mode);
3670 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1,
3671 segflg, path, fd, cap_rights_init(&rights, CAP_MKDIRAT), td);
3672 nd.ni_cnd.cn_flags |= WILLBEDIR;
3673 if ((error = namei(&nd)) != 0)
3677 NDFREE(&nd, NDF_ONLY_PNBUF);
3679 * XXX namei called with LOCKPARENT but not LOCKLEAF has
3680 * the strange behaviour of leaving the vnode unlocked
3681 * if the target is the same vnode as the parent.
3683 if (vp == nd.ni_dvp)
3690 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3691 NDFREE(&nd, NDF_ONLY_PNBUF);
3693 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3698 vattr.va_type = VDIR;
3699 vattr.va_mode = (mode & ACCESSPERMS) &~ td->td_proc->p_fd->fd_cmask;
3701 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
3706 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
3710 NDFREE(&nd, NDF_ONLY_PNBUF);
3714 vn_finished_write(mp);
3719 * Remove a directory file.
3721 #ifndef _SYS_SYSPROTO_H_
3729 struct rmdir_args /* {
3734 return (kern_rmdir(td, uap->path, UIO_USERSPACE));
3738 kern_rmdir(struct thread *td, char *path, enum uio_seg pathseg)
3741 return (kern_rmdirat(td, AT_FDCWD, path, pathseg));
3745 kern_rmdirat(struct thread *td, int fd, char *path, enum uio_seg pathseg)
3749 struct nameidata nd;
3750 cap_rights_t rights;
3755 NDINIT_ATRIGHTS(&nd, DELETE, LOCKPARENT | LOCKLEAF | AUDITVNODE1,
3756 pathseg, path, fd, cap_rights_init(&rights, CAP_UNLINKAT), td);
3757 if ((error = namei(&nd)) != 0)
3760 if (vp->v_type != VDIR) {
3765 * No rmdir "." please.
3767 if (nd.ni_dvp == vp) {
3772 * The root of a mounted filesystem cannot be deleted.
3774 if (vp->v_vflag & VV_ROOT) {
3779 error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp,
3784 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3785 NDFREE(&nd, NDF_ONLY_PNBUF);
3787 if (nd.ni_dvp == vp)
3791 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3795 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_UNLINK);
3796 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
3797 vn_finished_write(mp);
3799 NDFREE(&nd, NDF_ONLY_PNBUF);
3801 if (nd.ni_dvp == vp)
3810 * Read a block of directory entries in a filesystem independent format.
3812 #ifndef _SYS_SYSPROTO_H_
3813 struct ogetdirentries_args {
3821 ogetdirentries(struct thread *td, struct ogetdirentries_args *uap)
3826 error = kern_ogetdirentries(td, uap, &loff);
3828 error = copyout(&loff, uap->basep, sizeof(long));
3833 kern_ogetdirentries(struct thread *td, struct ogetdirentries_args *uap,
3838 struct uio auio, kuio;
3839 struct iovec aiov, kiov;
3840 struct dirent *dp, *edp;
3841 cap_rights_t rights;
3843 int error, eofflag, readcnt;
3847 /* XXX arbitrary sanity limit on `count'. */
3848 if (uap->count > 64 * 1024)
3850 error = getvnode(td->td_proc->p_fd, uap->fd,
3851 cap_rights_init(&rights, CAP_READ), &fp);
3854 if ((fp->f_flag & FREAD) == 0) {
3859 foffset = foffset_lock(fp, 0);
3861 if (vp->v_type != VDIR) {
3862 foffset_unlock(fp, foffset, 0);
3866 aiov.iov_base = uap->buf;
3867 aiov.iov_len = uap->count;
3868 auio.uio_iov = &aiov;
3869 auio.uio_iovcnt = 1;
3870 auio.uio_rw = UIO_READ;
3871 auio.uio_segflg = UIO_USERSPACE;
3873 auio.uio_resid = uap->count;
3874 vn_lock(vp, LK_SHARED | LK_RETRY);
3875 loff = auio.uio_offset = foffset;
3877 error = mac_vnode_check_readdir(td->td_ucred, vp);
3880 foffset_unlock(fp, foffset, FOF_NOUPDATE);
3885 # if (BYTE_ORDER != LITTLE_ENDIAN)
3886 if (vp->v_mount->mnt_maxsymlinklen <= 0) {
3887 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag,
3889 foffset = auio.uio_offset;
3894 kuio.uio_iov = &kiov;
3895 kuio.uio_segflg = UIO_SYSSPACE;
3896 kiov.iov_len = uap->count;
3897 dirbuf = malloc(uap->count, M_TEMP, M_WAITOK);
3898 kiov.iov_base = dirbuf;
3899 error = VOP_READDIR(vp, &kuio, fp->f_cred, &eofflag,
3901 foffset = kuio.uio_offset;
3903 readcnt = uap->count - kuio.uio_resid;
3904 edp = (struct dirent *)&dirbuf[readcnt];
3905 for (dp = (struct dirent *)dirbuf; dp < edp; ) {
3906 # if (BYTE_ORDER == LITTLE_ENDIAN)
3908 * The expected low byte of
3909 * dp->d_namlen is our dp->d_type.
3910 * The high MBZ byte of dp->d_namlen
3911 * is our dp->d_namlen.
3913 dp->d_type = dp->d_namlen;
3917 * The dp->d_type is the high byte
3918 * of the expected dp->d_namlen,
3919 * so must be zero'ed.
3923 if (dp->d_reclen > 0) {
3924 dp = (struct dirent *)
3925 ((char *)dp + dp->d_reclen);
3932 error = uiomove(dirbuf, readcnt, &auio);
3934 free(dirbuf, M_TEMP);
3938 foffset_unlock(fp, foffset, 0);
3942 if (uap->count == auio.uio_resid &&
3943 (vp->v_vflag & VV_ROOT) &&
3944 (vp->v_mount->mnt_flag & MNT_UNION)) {
3945 struct vnode *tvp = vp;
3946 vp = vp->v_mount->mnt_vnodecovered;
3955 foffset_unlock(fp, foffset, 0);
3957 td->td_retval[0] = uap->count - auio.uio_resid;
3962 #endif /* COMPAT_43 */
3965 * Read a block of directory entries in a filesystem independent format.
3967 #ifndef _SYS_SYSPROTO_H_
3968 struct getdirentries_args {
3976 sys_getdirentries(td, uap)
3978 register struct getdirentries_args /* {
3988 error = kern_getdirentries(td, uap->fd, uap->buf, uap->count, &base,
3989 NULL, UIO_USERSPACE);
3992 if (uap->basep != NULL)
3993 error = copyout(&base, uap->basep, sizeof(long));
3998 kern_getdirentries(struct thread *td, int fd, char *buf, u_int count,
3999 long *basep, ssize_t *residp, enum uio_seg bufseg)
4005 cap_rights_t rights;
4011 if (count > IOSIZE_MAX)
4013 auio.uio_resid = count;
4014 error = getvnode(td->td_proc->p_fd, fd,
4015 cap_rights_init(&rights, CAP_READ), &fp);
4018 if ((fp->f_flag & FREAD) == 0) {
4023 foffset = foffset_lock(fp, 0);
4025 if (vp->v_type != VDIR) {
4029 aiov.iov_base = buf;
4030 aiov.iov_len = count;
4031 auio.uio_iov = &aiov;
4032 auio.uio_iovcnt = 1;
4033 auio.uio_rw = UIO_READ;
4034 auio.uio_segflg = bufseg;
4036 vn_lock(vp, LK_SHARED | LK_RETRY);
4037 AUDIT_ARG_VNODE1(vp);
4038 loff = auio.uio_offset = foffset;
4040 error = mac_vnode_check_readdir(td->td_ucred, vp);
4043 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL,
4045 foffset = auio.uio_offset;
4050 if (count == auio.uio_resid &&
4051 (vp->v_vflag & VV_ROOT) &&
4052 (vp->v_mount->mnt_flag & MNT_UNION)) {
4053 struct vnode *tvp = vp;
4055 vp = vp->v_mount->mnt_vnodecovered;
4066 *residp = auio.uio_resid;
4067 td->td_retval[0] = count - auio.uio_resid;
4069 foffset_unlock(fp, foffset, 0);
4074 #ifndef _SYS_SYSPROTO_H_
4075 struct getdents_args {
4082 sys_getdents(td, uap)
4084 register struct getdents_args /* {
4090 struct getdirentries_args ap;
4094 ap.count = uap->count;
4096 return (sys_getdirentries(td, &ap));
4100 * Set the mode mask for creation of filesystem nodes.
4102 #ifndef _SYS_SYSPROTO_H_
4110 struct umask_args /* {
4114 register struct filedesc *fdp;
4116 FILEDESC_XLOCK(td->td_proc->p_fd);
4117 fdp = td->td_proc->p_fd;
4118 td->td_retval[0] = fdp->fd_cmask;
4119 fdp->fd_cmask = uap->newmask & ALLPERMS;
4120 FILEDESC_XUNLOCK(td->td_proc->p_fd);
4125 * Void all references to file by ripping underlying filesystem away from
4128 #ifndef _SYS_SYSPROTO_H_
4129 struct revoke_args {
4136 register struct revoke_args /* {
4142 struct nameidata nd;
4145 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
4147 if ((error = namei(&nd)) != 0)
4150 NDFREE(&nd, NDF_ONLY_PNBUF);
4151 if (vp->v_type != VCHR || vp->v_rdev == NULL) {
4156 error = mac_vnode_check_revoke(td->td_ucred, vp);
4160 error = VOP_GETATTR(vp, &vattr, td->td_ucred);
4163 if (td->td_ucred->cr_uid != vattr.va_uid) {
4164 error = priv_check(td, PRIV_VFS_ADMIN);
4169 VOP_REVOKE(vp, REVOKEALL);
4176 * Convert a user file descriptor to a kernel file entry and check that, if it
4177 * is a capability, the correct rights are present. A reference on the file
4178 * entry is held upon returning.
4181 getvnode(struct filedesc *fdp, int fd, cap_rights_t *rightsp, struct file **fpp)
4186 error = fget_unlocked(fdp, fd, rightsp, 0, &fp, NULL);
4191 * The file could be not of the vnode type, or it may be not
4192 * yet fully initialized, in which case the f_vnode pointer
4193 * may be set, but f_ops is still badfileops. E.g.,
4194 * devfs_open() transiently create such situation to
4195 * facilitate csw d_fdopen().
4197 * Dupfdopen() handling in kern_openat() installs the
4198 * half-baked file into the process descriptor table, allowing
4199 * other thread to dereference it. Guard against the race by
4202 if (fp->f_vnode == NULL || fp->f_ops == &badfileops) {
4203 fdrop(fp, curthread);
4212 * Get an (NFS) file handle.
4214 #ifndef _SYS_SYSPROTO_H_
4215 struct lgetfh_args {
4223 register struct lgetfh_args *uap;
4225 struct nameidata nd;
4227 register struct vnode *vp;
4230 error = priv_check(td, PRIV_VFS_GETFH);
4233 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
4238 NDFREE(&nd, NDF_ONLY_PNBUF);
4240 bzero(&fh, sizeof(fh));
4241 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
4242 error = VOP_VPTOFH(vp, &fh.fh_fid);
4245 error = copyout(&fh, uap->fhp, sizeof (fh));
4249 #ifndef _SYS_SYSPROTO_H_
4258 register struct getfh_args *uap;
4260 struct nameidata nd;
4262 register struct vnode *vp;
4265 error = priv_check(td, PRIV_VFS_GETFH);
4268 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
4273 NDFREE(&nd, NDF_ONLY_PNBUF);
4275 bzero(&fh, sizeof(fh));
4276 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
4277 error = VOP_VPTOFH(vp, &fh.fh_fid);
4280 error = copyout(&fh, uap->fhp, sizeof (fh));
4285 * syscall for the rpc.lockd to use to translate a NFS file handle into an
4288 * warning: do not remove the priv_check() call or this becomes one giant
4291 #ifndef _SYS_SYSPROTO_H_
4292 struct fhopen_args {
4293 const struct fhandle *u_fhp;
4300 struct fhopen_args /* {
4301 const struct fhandle *u_fhp;
4312 error = priv_check(td, PRIV_VFS_FHOPEN);
4316 fmode = FFLAGS(uap->flags);
4317 /* why not allow a non-read/write open for our lockd? */
4318 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
4320 error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
4323 /* find the mount point */
4324 mp = vfs_busyfs(&fhp.fh_fsid);
4327 /* now give me my vnode, it gets returned to me locked */
4328 error = VFS_FHTOVP(mp, &fhp.fh_fid, LK_EXCLUSIVE, &vp);
4333 error = falloc_noinstall(td, &fp);
4339 * An extra reference on `fp' has been held for us by
4340 * falloc_noinstall().
4346 error = vn_open_vnode(vp, fmode, td->td_ucred, td, fp);
4348 KASSERT(fp->f_ops == &badfileops,
4349 ("VOP_OPEN in fhopen() set f_ops"));
4350 KASSERT(td->td_dupfd < 0,
4351 ("fhopen() encountered fdopen()"));
4361 finit(fp, (fmode & FMASK) | (fp->f_flag & FHASLOCK), DTYPE_VNODE, vp,
4364 if ((fmode & O_TRUNC) != 0) {
4365 error = fo_truncate(fp, 0, td->td_ucred, td);
4370 error = finstall(td, fp, &indx, fmode, NULL);
4373 td->td_retval[0] = indx;
4378 * Stat an (NFS) file handle.
4380 #ifndef _SYS_SYSPROTO_H_
4381 struct fhstat_args {
4382 struct fhandle *u_fhp;
4389 register struct fhstat_args /* {
4390 struct fhandle *u_fhp;
4398 error = copyin(uap->u_fhp, &fh, sizeof(fh));
4401 error = kern_fhstat(td, fh, &sb);
4403 error = copyout(&sb, uap->sb, sizeof(sb));
4408 kern_fhstat(struct thread *td, struct fhandle fh, struct stat *sb)
4414 error = priv_check(td, PRIV_VFS_FHSTAT);
4417 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4419 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_EXCLUSIVE, &vp);
4423 error = vn_stat(vp, sb, td->td_ucred, NOCRED, td);
4429 * Implement fstatfs() for (NFS) file handles.
4431 #ifndef _SYS_SYSPROTO_H_
4432 struct fhstatfs_args {
4433 struct fhandle *u_fhp;
4438 sys_fhstatfs(td, uap)
4440 struct fhstatfs_args /* {
4441 struct fhandle *u_fhp;
4449 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4452 error = kern_fhstatfs(td, fh, &sf);
4455 return (copyout(&sf, uap->buf, sizeof(sf)));
4459 kern_fhstatfs(struct thread *td, fhandle_t fh, struct statfs *buf)
4466 error = priv_check(td, PRIV_VFS_FHSTATFS);
4469 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4471 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_EXCLUSIVE, &vp);
4477 error = prison_canseemount(td->td_ucred, mp);
4481 error = mac_mount_check_stat(td->td_ucred, mp);
4486 * Set these in case the underlying filesystem fails to do so.
4489 sp->f_version = STATFS_VERSION;
4490 sp->f_namemax = NAME_MAX;
4491 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
4492 error = VFS_STATFS(mp, sp);
4501 kern_posix_fallocate(struct thread *td, int fd, off_t offset, off_t len)
4506 cap_rights_t rights;
4507 off_t olen, ooffset;
4511 error = fget(td, fd, cap_rights_init(&rights, CAP_WRITE), &fp);
4515 switch (fp->f_type) {
4526 if ((fp->f_flag & FWRITE) == 0) {
4531 if (vp->v_type != VREG) {
4535 if (offset < 0 || len <= 0) {
4539 /* Check for wrap. */
4540 if (offset > OFF_MAX - len) {
4545 /* Allocating blocks may take a long time, so iterate. */
4552 error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
4555 error = vn_lock(vp, LK_EXCLUSIVE);
4557 vn_finished_write(mp);
4561 error = mac_vnode_check_write(td->td_ucred, fp->f_cred, vp);
4564 error = VOP_ALLOCATE(vp, &offset, &len);
4566 vn_finished_write(mp);
4568 if (olen + ooffset != offset + len) {
4569 panic("offset + len changed from %jx/%jx to %jx/%jx",
4570 ooffset, olen, offset, len);
4572 if (error != 0 || len == 0)
4574 KASSERT(olen > len, ("Iteration did not make progress?"));
4584 sys_posix_fallocate(struct thread *td, struct posix_fallocate_args *uap)
4587 return (kern_posix_fallocate(td, uap->fd, uap->offset, uap->len));
4591 * Unlike madvise(2), we do not make a best effort to remember every
4592 * possible caching hint. Instead, we remember the last setting with
4593 * the exception that we will allow POSIX_FADV_NORMAL to adjust the
4594 * region of any current setting.
4597 kern_posix_fadvise(struct thread *td, int fd, off_t offset, off_t len,
4600 struct fadvise_info *fa, *new;
4603 cap_rights_t rights;
4607 if (offset < 0 || len < 0 || offset > OFF_MAX - len)
4610 case POSIX_FADV_SEQUENTIAL:
4611 case POSIX_FADV_RANDOM:
4612 case POSIX_FADV_NOREUSE:
4613 new = malloc(sizeof(*fa), M_FADVISE, M_WAITOK);
4615 case POSIX_FADV_NORMAL:
4616 case POSIX_FADV_WILLNEED:
4617 case POSIX_FADV_DONTNEED:
4623 /* XXX: CAP_POSIX_FADVISE? */
4624 error = fget(td, fd, cap_rights_init(&rights), &fp);
4628 switch (fp->f_type) {
4640 if (vp->v_type != VREG) {
4647 end = offset + len - 1;
4649 case POSIX_FADV_SEQUENTIAL:
4650 case POSIX_FADV_RANDOM:
4651 case POSIX_FADV_NOREUSE:
4653 * Try to merge any existing non-standard region with
4654 * this new region if possible, otherwise create a new
4655 * non-standard region for this request.
4657 mtx_pool_lock(mtxpool_sleep, fp);
4659 if (fa != NULL && fa->fa_advice == advice &&
4660 ((fa->fa_start <= end && fa->fa_end >= offset) ||
4661 (end != OFF_MAX && fa->fa_start == end + 1) ||
4662 (fa->fa_end != OFF_MAX && fa->fa_end + 1 == offset))) {
4663 if (offset < fa->fa_start)
4664 fa->fa_start = offset;
4665 if (end > fa->fa_end)
4668 new->fa_advice = advice;
4669 new->fa_start = offset;
4671 new->fa_prevstart = 0;
4672 new->fa_prevend = 0;
4676 mtx_pool_unlock(mtxpool_sleep, fp);
4678 case POSIX_FADV_NORMAL:
4680 * If a the "normal" region overlaps with an existing
4681 * non-standard region, trim or remove the
4682 * non-standard region.
4684 mtx_pool_lock(mtxpool_sleep, fp);
4687 if (offset <= fa->fa_start && end >= fa->fa_end) {
4689 fp->f_advice = NULL;
4690 } else if (offset <= fa->fa_start &&
4691 end >= fa->fa_start)
4692 fa->fa_start = end + 1;
4693 else if (offset <= fa->fa_end && end >= fa->fa_end)
4694 fa->fa_end = offset - 1;
4695 else if (offset >= fa->fa_start && end <= fa->fa_end) {
4697 * If the "normal" region is a middle
4698 * portion of the existing
4699 * non-standard region, just remove
4700 * the whole thing rather than picking
4701 * one side or the other to
4705 fp->f_advice = NULL;
4708 mtx_pool_unlock(mtxpool_sleep, fp);
4710 case POSIX_FADV_WILLNEED:
4711 case POSIX_FADV_DONTNEED:
4712 error = VOP_ADVISE(vp, offset, end, advice);
4718 free(new, M_FADVISE);
4723 sys_posix_fadvise(struct thread *td, struct posix_fadvise_args *uap)
4726 return (kern_posix_fadvise(td, uap->fd, uap->offset, uap->len,