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_kdtrace.h"
43 #include "opt_ktrace.h"
45 #include <sys/param.h>
46 #include <sys/systm.h>
49 #include <sys/capability.h>
51 #include <sys/sysent.h>
52 #include <sys/malloc.h>
53 #include <sys/mount.h>
54 #include <sys/mutex.h>
55 #include <sys/sysproto.h>
56 #include <sys/namei.h>
57 #include <sys/filedesc.h>
58 #include <sys/kernel.h>
59 #include <sys/fcntl.h>
61 #include <sys/filio.h>
62 #include <sys/limits.h>
63 #include <sys/linker.h>
64 #include <sys/rwlock.h>
68 #include <sys/unistd.h>
69 #include <sys/vnode.h>
72 #include <sys/dirent.h>
74 #include <sys/syscallsubr.h>
75 #include <sys/sysctl.h>
77 #include <sys/ktrace.h>
80 #include <machine/stdarg.h>
82 #include <security/audit/audit.h>
83 #include <security/mac/mac_framework.h>
86 #include <vm/vm_object.h>
87 #include <vm/vm_page.h>
90 #include <ufs/ufs/quota.h>
92 MALLOC_DEFINE(M_FADVISE, "fadvise", "posix_fadvise(2) information");
94 SDT_PROVIDER_DEFINE(vfs);
95 SDT_PROBE_DEFINE2(vfs, , stat, mode, mode, "char *", "int");
96 SDT_PROBE_DEFINE2(vfs, , stat, reg, reg, "char *", "int");
98 static int chroot_refuse_vdir_fds(struct filedesc *fdp);
99 static int getutimes(const struct timeval *, enum uio_seg, struct timespec *);
100 static int kern_chflags(struct thread *td, const char *path,
101 enum uio_seg pathseg, u_long flags);
102 static int kern_chflagsat(struct thread *td, int fd, const char *path,
103 enum uio_seg pathseg, u_long flags, int atflag);
104 static int setfflags(struct thread *td, struct vnode *, u_long);
105 static int setutimes(struct thread *td, struct vnode *,
106 const struct timespec *, int, int);
107 static int vn_access(struct vnode *vp, int user_flags, struct ucred *cred,
111 * The module initialization routine for POSIX asynchronous I/O will
112 * set this to the version of AIO that it implements. (Zero means
113 * that it is not implemented.) This value is used here by pathconf()
114 * and in kern_descrip.c by fpathconf().
116 int async_io_version;
119 static int syncprt = 0;
120 SYSCTL_INT(_debug, OID_AUTO, syncprt, CTLFLAG_RW, &syncprt, 0, "");
124 * Sync each mounted filesystem.
126 #ifndef _SYS_SYSPROTO_H_
135 struct sync_args *uap;
137 struct mount *mp, *nmp;
140 mtx_lock(&mountlist_mtx);
141 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
142 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK)) {
143 nmp = TAILQ_NEXT(mp, mnt_list);
146 if ((mp->mnt_flag & MNT_RDONLY) == 0 &&
147 vn_start_write(NULL, &mp, V_NOWAIT) == 0) {
148 save = curthread_pflags_set(TDP_SYNCIO);
149 vfs_msync(mp, MNT_NOWAIT);
150 VFS_SYNC(mp, MNT_NOWAIT);
151 curthread_pflags_restore(save);
152 vn_finished_write(mp);
154 mtx_lock(&mountlist_mtx);
155 nmp = TAILQ_NEXT(mp, mnt_list);
158 mtx_unlock(&mountlist_mtx);
163 * Change filesystem quotas.
165 #ifndef _SYS_SYSPROTO_H_
166 struct quotactl_args {
174 sys_quotactl(td, uap)
176 register struct quotactl_args /* {
187 AUDIT_ARG_CMD(uap->cmd);
188 AUDIT_ARG_UID(uap->uid);
189 if (!prison_allow(td->td_ucred, PR_ALLOW_QUOTAS))
191 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
193 if ((error = namei(&nd)) != 0)
195 NDFREE(&nd, NDF_ONLY_PNBUF);
196 mp = nd.ni_vp->v_mount;
199 error = vfs_busy(mp, 0);
203 error = VFS_QUOTACTL(mp, uap->cmd, uap->uid, uap->arg);
206 * Since quota on operation typically needs to open quota
207 * file, the Q_QUOTAON handler needs to unbusy the mount point
208 * before calling into namei. Otherwise, unmount might be
209 * started between two vfs_busy() invocations (first is our,
210 * second is from mount point cross-walk code in lookup()),
213 * Require that Q_QUOTAON handles the vfs_busy() reference on
214 * its own, always returning with ubusied mount point.
216 if ((uap->cmd >> SUBCMDSHIFT) != Q_QUOTAON)
222 * Used by statfs conversion routines to scale the block size up if
223 * necessary so that all of the block counts are <= 'max_size'. Note
224 * that 'max_size' should be a bitmask, i.e. 2^n - 1 for some non-zero
228 statfs_scale_blocks(struct statfs *sf, long max_size)
233 KASSERT(powerof2(max_size + 1), ("%s: invalid max_size", __func__));
236 * Attempt to scale the block counts to give a more accurate
237 * overview to userland of the ratio of free space to used
238 * space. To do this, find the largest block count and compute
239 * a divisor that lets it fit into a signed integer <= max_size.
241 if (sf->f_bavail < 0)
242 count = -sf->f_bavail;
244 count = sf->f_bavail;
245 count = MAX(sf->f_blocks, MAX(sf->f_bfree, count));
246 if (count <= max_size)
249 count >>= flsl(max_size);
256 sf->f_bsize <<= shift;
257 sf->f_blocks >>= shift;
258 sf->f_bfree >>= shift;
259 sf->f_bavail >>= shift;
263 * Get filesystem statistics.
265 #ifndef _SYS_SYSPROTO_H_
274 register struct statfs_args /* {
282 error = kern_statfs(td, uap->path, UIO_USERSPACE, &sf);
284 error = copyout(&sf, uap->buf, sizeof(sf));
289 kern_statfs(struct thread *td, char *path, enum uio_seg pathseg,
293 struct statfs *sp, sb;
297 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
302 mp = nd.ni_vp->v_mount;
304 NDFREE(&nd, NDF_ONLY_PNBUF);
306 error = vfs_busy(mp, 0);
311 error = mac_mount_check_stat(td->td_ucred, mp);
316 * Set these in case the underlying filesystem fails to do so.
319 sp->f_version = STATFS_VERSION;
320 sp->f_namemax = NAME_MAX;
321 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
322 error = VFS_STATFS(mp, sp);
325 if (priv_check(td, PRIV_VFS_GENERATION)) {
326 bcopy(sp, &sb, sizeof(sb));
327 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
328 prison_enforce_statfs(td->td_ucred, mp, &sb);
338 * Get filesystem statistics.
340 #ifndef _SYS_SYSPROTO_H_
341 struct fstatfs_args {
349 register struct fstatfs_args /* {
357 error = kern_fstatfs(td, uap->fd, &sf);
359 error = copyout(&sf, uap->buf, sizeof(sf));
364 kern_fstatfs(struct thread *td, int fd, struct statfs *buf)
368 struct statfs *sp, sb;
373 error = getvnode(td->td_proc->p_fd, fd, CAP_FSTATFS, &fp);
377 vn_lock(vp, LK_SHARED | LK_RETRY);
379 AUDIT_ARG_VNODE1(vp);
390 error = vfs_busy(mp, 0);
395 error = mac_mount_check_stat(td->td_ucred, mp);
400 * Set these in case the underlying filesystem fails to do so.
403 sp->f_version = STATFS_VERSION;
404 sp->f_namemax = NAME_MAX;
405 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
406 error = VFS_STATFS(mp, sp);
409 if (priv_check(td, PRIV_VFS_GENERATION)) {
410 bcopy(sp, &sb, sizeof(sb));
411 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
412 prison_enforce_statfs(td->td_ucred, mp, &sb);
423 * Get statistics on all filesystems.
425 #ifndef _SYS_SYSPROTO_H_
426 struct getfsstat_args {
433 sys_getfsstat(td, uap)
435 register struct getfsstat_args /* {
442 return (kern_getfsstat(td, &uap->buf, uap->bufsize, UIO_USERSPACE,
447 * If (bufsize > 0 && bufseg == UIO_SYSSPACE)
448 * The caller is responsible for freeing memory which will be allocated
452 kern_getfsstat(struct thread *td, struct statfs **buf, size_t bufsize,
453 enum uio_seg bufseg, int flags)
455 struct mount *mp, *nmp;
456 struct statfs *sfsp, *sp, sb;
457 size_t count, maxcount;
460 maxcount = bufsize / sizeof(struct statfs);
463 else if (bufseg == UIO_USERSPACE)
465 else /* if (bufseg == UIO_SYSSPACE) */ {
467 mtx_lock(&mountlist_mtx);
468 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
471 mtx_unlock(&mountlist_mtx);
472 if (maxcount > count)
474 sfsp = *buf = malloc(maxcount * sizeof(struct statfs), M_TEMP,
478 mtx_lock(&mountlist_mtx);
479 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
480 if (prison_canseemount(td->td_ucred, mp) != 0) {
481 nmp = TAILQ_NEXT(mp, mnt_list);
485 if (mac_mount_check_stat(td->td_ucred, mp) != 0) {
486 nmp = TAILQ_NEXT(mp, mnt_list);
490 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK)) {
491 nmp = TAILQ_NEXT(mp, mnt_list);
494 if (sfsp && count < maxcount) {
497 * Set these in case the underlying filesystem
500 sp->f_version = STATFS_VERSION;
501 sp->f_namemax = NAME_MAX;
502 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
504 * If MNT_NOWAIT or MNT_LAZY is specified, do not
505 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
506 * overrides MNT_WAIT.
508 if (((flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
509 (flags & MNT_WAIT)) &&
510 (error = VFS_STATFS(mp, sp))) {
511 mtx_lock(&mountlist_mtx);
512 nmp = TAILQ_NEXT(mp, mnt_list);
516 if (priv_check(td, PRIV_VFS_GENERATION)) {
517 bcopy(sp, &sb, sizeof(sb));
518 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
519 prison_enforce_statfs(td->td_ucred, mp, &sb);
522 if (bufseg == UIO_SYSSPACE)
523 bcopy(sp, sfsp, sizeof(*sp));
524 else /* if (bufseg == UIO_USERSPACE) */ {
525 error = copyout(sp, sfsp, sizeof(*sp));
534 mtx_lock(&mountlist_mtx);
535 nmp = TAILQ_NEXT(mp, mnt_list);
538 mtx_unlock(&mountlist_mtx);
539 if (sfsp && count > maxcount)
540 td->td_retval[0] = maxcount;
542 td->td_retval[0] = count;
546 #ifdef COMPAT_FREEBSD4
548 * Get old format filesystem statistics.
550 static void cvtstatfs(struct statfs *, struct ostatfs *);
552 #ifndef _SYS_SYSPROTO_H_
553 struct freebsd4_statfs_args {
559 freebsd4_statfs(td, uap)
561 struct freebsd4_statfs_args /* {
570 error = kern_statfs(td, uap->path, UIO_USERSPACE, &sf);
573 cvtstatfs(&sf, &osb);
574 return (copyout(&osb, uap->buf, sizeof(osb)));
578 * Get filesystem statistics.
580 #ifndef _SYS_SYSPROTO_H_
581 struct freebsd4_fstatfs_args {
587 freebsd4_fstatfs(td, uap)
589 struct freebsd4_fstatfs_args /* {
598 error = kern_fstatfs(td, uap->fd, &sf);
601 cvtstatfs(&sf, &osb);
602 return (copyout(&osb, uap->buf, sizeof(osb)));
606 * Get statistics on all filesystems.
608 #ifndef _SYS_SYSPROTO_H_
609 struct freebsd4_getfsstat_args {
616 freebsd4_getfsstat(td, uap)
618 register struct freebsd4_getfsstat_args /* {
624 struct statfs *buf, *sp;
629 count = uap->bufsize / sizeof(struct ostatfs);
630 size = count * sizeof(struct statfs);
631 error = kern_getfsstat(td, &buf, size, UIO_SYSSPACE, uap->flags);
633 count = td->td_retval[0];
635 while (count > 0 && error == 0) {
637 error = copyout(&osb, uap->buf, sizeof(osb));
648 * Implement fstatfs() for (NFS) file handles.
650 #ifndef _SYS_SYSPROTO_H_
651 struct freebsd4_fhstatfs_args {
652 struct fhandle *u_fhp;
657 freebsd4_fhstatfs(td, uap)
659 struct freebsd4_fhstatfs_args /* {
660 struct fhandle *u_fhp;
669 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
672 error = kern_fhstatfs(td, fh, &sf);
675 cvtstatfs(&sf, &osb);
676 return (copyout(&osb, uap->buf, sizeof(osb)));
680 * Convert a new format statfs structure to an old format statfs structure.
688 statfs_scale_blocks(nsp, LONG_MAX);
689 bzero(osp, sizeof(*osp));
690 osp->f_bsize = nsp->f_bsize;
691 osp->f_iosize = MIN(nsp->f_iosize, LONG_MAX);
692 osp->f_blocks = nsp->f_blocks;
693 osp->f_bfree = nsp->f_bfree;
694 osp->f_bavail = nsp->f_bavail;
695 osp->f_files = MIN(nsp->f_files, LONG_MAX);
696 osp->f_ffree = MIN(nsp->f_ffree, LONG_MAX);
697 osp->f_owner = nsp->f_owner;
698 osp->f_type = nsp->f_type;
699 osp->f_flags = nsp->f_flags;
700 osp->f_syncwrites = MIN(nsp->f_syncwrites, LONG_MAX);
701 osp->f_asyncwrites = MIN(nsp->f_asyncwrites, LONG_MAX);
702 osp->f_syncreads = MIN(nsp->f_syncreads, LONG_MAX);
703 osp->f_asyncreads = MIN(nsp->f_asyncreads, LONG_MAX);
704 strlcpy(osp->f_fstypename, nsp->f_fstypename,
705 MIN(MFSNAMELEN, OMFSNAMELEN));
706 strlcpy(osp->f_mntonname, nsp->f_mntonname,
707 MIN(MNAMELEN, OMNAMELEN));
708 strlcpy(osp->f_mntfromname, nsp->f_mntfromname,
709 MIN(MNAMELEN, OMNAMELEN));
710 osp->f_fsid = nsp->f_fsid;
712 #endif /* COMPAT_FREEBSD4 */
715 * Change current working directory to a given file descriptor.
717 #ifndef _SYS_SYSPROTO_H_
725 struct fchdir_args /* {
729 register struct filedesc *fdp = td->td_proc->p_fd;
730 struct vnode *vp, *tdp, *vpold;
735 AUDIT_ARG_FD(uap->fd);
736 if ((error = getvnode(fdp, uap->fd, CAP_FCHDIR, &fp)) != 0)
741 vn_lock(vp, LK_SHARED | LK_RETRY);
742 AUDIT_ARG_VNODE1(vp);
743 error = change_dir(vp, td);
744 while (!error && (mp = vp->v_mountedhere) != NULL) {
747 error = VFS_ROOT(mp, LK_SHARED, &tdp);
760 vpold = fdp->fd_cdir;
762 FILEDESC_XUNLOCK(fdp);
768 * Change current working directory (``.'').
770 #ifndef _SYS_SYSPROTO_H_
778 struct chdir_args /* {
783 return (kern_chdir(td, uap->path, UIO_USERSPACE));
787 kern_chdir(struct thread *td, char *path, enum uio_seg pathseg)
789 register struct filedesc *fdp = td->td_proc->p_fd;
794 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
796 if ((error = namei(&nd)) != 0)
798 if ((error = change_dir(nd.ni_vp, td)) != 0) {
800 NDFREE(&nd, NDF_ONLY_PNBUF);
803 VOP_UNLOCK(nd.ni_vp, 0);
804 NDFREE(&nd, NDF_ONLY_PNBUF);
807 fdp->fd_cdir = nd.ni_vp;
808 FILEDESC_XUNLOCK(fdp);
814 * Helper function for raised chroot(2) security function: Refuse if
815 * any filedescriptors are open directories.
818 chroot_refuse_vdir_fds(fdp)
819 struct filedesc *fdp;
825 FILEDESC_LOCK_ASSERT(fdp);
827 for (fd = 0; fd < fdp->fd_nfiles ; fd++) {
828 fp = fget_locked(fdp, fd);
831 if (fp->f_type == DTYPE_VNODE) {
833 if (vp->v_type == VDIR)
841 * This sysctl determines if we will allow a process to chroot(2) if it
842 * has a directory open:
843 * 0: disallowed for all processes.
844 * 1: allowed for processes that were not already chroot(2)'ed.
845 * 2: allowed for all processes.
848 static int chroot_allow_open_directories = 1;
850 SYSCTL_INT(_kern, OID_AUTO, chroot_allow_open_directories, CTLFLAG_RW,
851 &chroot_allow_open_directories, 0,
852 "Allow a process to chroot(2) if it has a directory open");
855 * Change notion of root (``/'') directory.
857 #ifndef _SYS_SYSPROTO_H_
865 struct chroot_args /* {
872 error = priv_check(td, PRIV_VFS_CHROOT);
875 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
876 UIO_USERSPACE, uap->path, td);
880 if ((error = change_dir(nd.ni_vp, td)) != 0)
883 if ((error = mac_vnode_check_chroot(td->td_ucred, nd.ni_vp)))
886 VOP_UNLOCK(nd.ni_vp, 0);
887 error = change_root(nd.ni_vp, td);
889 NDFREE(&nd, NDF_ONLY_PNBUF);
894 NDFREE(&nd, NDF_ONLY_PNBUF);
899 * Common routine for chroot and chdir. Callers must provide a locked vnode
909 ASSERT_VOP_LOCKED(vp, "change_dir(): vp not locked");
910 if (vp->v_type != VDIR)
913 error = mac_vnode_check_chdir(td->td_ucred, vp);
917 error = VOP_ACCESS(vp, VEXEC, td->td_ucred, td);
922 * Common routine for kern_chroot() and jail_attach(). The caller is
923 * responsible for invoking priv_check() and mac_vnode_check_chroot() to
924 * authorize this operation.
931 struct filedesc *fdp;
935 fdp = td->td_proc->p_fd;
937 if (chroot_allow_open_directories == 0 ||
938 (chroot_allow_open_directories == 1 && fdp->fd_rdir != rootvnode)) {
939 error = chroot_refuse_vdir_fds(fdp);
941 FILEDESC_XUNLOCK(fdp);
945 oldvp = fdp->fd_rdir;
952 FILEDESC_XUNLOCK(fdp);
957 static __inline cap_rights_t
958 flags_to_rights(int flags)
960 cap_rights_t rights = 0;
962 if (flags & O_EXEC) {
963 rights |= CAP_FEXECVE;
965 switch ((flags & O_ACCMODE)) {
974 if (!(flags & (O_APPEND | O_TRUNC)))
981 rights |= CAP_CREATE;
984 rights |= CAP_FTRUNCATE;
986 if (flags & (O_SYNC | O_FSYNC))
989 if (flags & (O_EXLOCK | O_SHLOCK))
996 * Check permissions, allocate an open file structure, and call the device
997 * open routine if any.
999 #ifndef _SYS_SYSPROTO_H_
1009 register struct open_args /* {
1016 return (kern_open(td, uap->path, UIO_USERSPACE, uap->flags, uap->mode));
1019 #ifndef _SYS_SYSPROTO_H_
1020 struct openat_args {
1028 sys_openat(struct thread *td, struct openat_args *uap)
1031 return (kern_openat(td, uap->fd, uap->path, UIO_USERSPACE, uap->flag,
1036 kern_open(struct thread *td, char *path, enum uio_seg pathseg, int flags,
1040 return (kern_openat(td, AT_FDCWD, path, pathseg, flags, mode));
1044 kern_openat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1045 int flags, int mode)
1047 struct proc *p = td->td_proc;
1048 struct filedesc *fdp = p->p_fd;
1052 int indx = -1, error;
1053 struct nameidata nd;
1054 cap_rights_t rights_needed = CAP_LOOKUP;
1056 AUDIT_ARG_FFLAGS(flags);
1057 AUDIT_ARG_MODE(mode);
1058 /* XXX: audit dirfd */
1059 rights_needed |= flags_to_rights(flags);
1061 * Only one of the O_EXEC, O_RDONLY, O_WRONLY and O_RDWR flags
1064 if (flags & O_EXEC) {
1065 if (flags & O_ACCMODE)
1067 } else if ((flags & O_ACCMODE) == O_ACCMODE) {
1070 flags = FFLAGS(flags);
1074 * Allocate the file descriptor, but don't install a descriptor yet.
1076 error = falloc_noinstall(td, &fp);
1080 * An extra reference on `fp' has been held for us by
1081 * falloc_noinstall().
1083 /* Set the flags early so the finit in devfs can pick them up. */
1084 fp->f_flag = flags & FMASK;
1085 cmode = ((mode &~ fdp->fd_cmask) & ALLPERMS) &~ S_ISTXT;
1086 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, fd,
1088 td->td_dupfd = -1; /* XXX check for fdopen */
1089 error = vn_open(&nd, &flags, cmode, fp);
1092 * If the vn_open replaced the method vector, something
1093 * wonderous happened deep below and we just pass it up
1094 * pretending we know what we do.
1096 if (error == ENXIO && fp->f_ops != &badfileops)
1100 * Handle special fdopen() case. bleh.
1102 * Don't do this for relative (capability) lookups; we don't
1103 * understand exactly what would happen, and we don't think
1104 * that it ever should.
1106 if (nd.ni_strictrelative == 0 &&
1107 (error == ENODEV || error == ENXIO) &&
1108 td->td_dupfd >= 0) {
1109 error = dupfdopen(td, fdp, td->td_dupfd, flags, error,
1118 NDFREE(&nd, NDF_ONLY_PNBUF);
1122 * Store the vnode, for any f_type. Typically, the vnode use
1123 * count is decremented by direct call to vn_closefile() for
1124 * files that switched type in the cdevsw fdopen() method.
1128 * If the file wasn't claimed by devfs bind it to the normal
1129 * vnode operations here.
1131 if (fp->f_ops == &badfileops) {
1132 KASSERT(vp->v_type != VFIFO, ("Unexpected fifo."));
1134 finit(fp, (flags & FMASK) | (fp->f_flag & FHASLOCK), DTYPE_VNODE,
1139 if (flags & O_TRUNC) {
1140 error = fo_truncate(fp, 0, td->td_ucred, td);
1146 * If we haven't already installed the FD (for dupfdopen), do so now.
1149 struct filecaps *fcaps;
1152 if (nd.ni_strictrelative == 1)
1153 fcaps = &nd.ni_filecaps;
1157 error = finstall(td, fp, &indx, flags, fcaps);
1158 /* On success finstall() consumes fcaps. */
1160 filecaps_free(&nd.ni_filecaps);
1164 filecaps_free(&nd.ni_filecaps);
1168 * Release our private reference, leaving the one associated with
1169 * the descriptor table intact.
1172 td->td_retval[0] = indx;
1175 KASSERT(indx == -1, ("indx=%d, should be -1", indx));
1184 #ifndef _SYS_SYSPROTO_H_
1185 struct ocreat_args {
1193 register struct ocreat_args /* {
1199 return (kern_open(td, uap->path, UIO_USERSPACE,
1200 O_WRONLY | O_CREAT | O_TRUNC, uap->mode));
1202 #endif /* COMPAT_43 */
1205 * Create a special file.
1207 #ifndef _SYS_SYSPROTO_H_
1217 register struct mknod_args /* {
1224 return (kern_mknod(td, uap->path, UIO_USERSPACE, uap->mode, uap->dev));
1227 #ifndef _SYS_SYSPROTO_H_
1228 struct mknodat_args {
1236 sys_mknodat(struct thread *td, struct mknodat_args *uap)
1239 return (kern_mknodat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode,
1244 kern_mknod(struct thread *td, char *path, enum uio_seg pathseg, int mode,
1248 return (kern_mknodat(td, AT_FDCWD, path, pathseg, mode, dev));
1252 kern_mknodat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1260 struct nameidata nd;
1262 AUDIT_ARG_MODE(mode);
1264 switch (mode & S_IFMT) {
1267 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
1270 error = priv_check(td, PRIV_VFS_MKNOD_BAD);
1273 error = priv_check(td, PRIV_VFS_MKNOD_WHT);
1277 return (kern_mkfifoat(td, fd, path, pathseg, mode));
1287 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1,
1288 pathseg, path, fd, CAP_MKNODAT, td);
1289 if ((error = namei(&nd)) != 0)
1293 NDFREE(&nd, NDF_ONLY_PNBUF);
1294 if (vp == nd.ni_dvp)
1302 vattr.va_mode = (mode & ALLPERMS) &
1303 ~td->td_proc->p_fd->fd_cmask;
1304 vattr.va_rdev = dev;
1307 switch (mode & S_IFMT) {
1308 case S_IFMT: /* used by badsect to flag bad sectors */
1309 vattr.va_type = VBAD;
1312 vattr.va_type = VCHR;
1315 vattr.va_type = VBLK;
1321 panic("kern_mknod: invalid mode");
1324 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1325 NDFREE(&nd, NDF_ONLY_PNBUF);
1327 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1332 if (error == 0 && !whiteout)
1333 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp,
1334 &nd.ni_cnd, &vattr);
1338 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, CREATE);
1340 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp,
1341 &nd.ni_cnd, &vattr);
1346 NDFREE(&nd, NDF_ONLY_PNBUF);
1348 vn_finished_write(mp);
1353 * Create a named pipe.
1355 #ifndef _SYS_SYSPROTO_H_
1356 struct mkfifo_args {
1364 register struct mkfifo_args /* {
1370 return (kern_mkfifo(td, uap->path, UIO_USERSPACE, uap->mode));
1373 #ifndef _SYS_SYSPROTO_H_
1374 struct mkfifoat_args {
1381 sys_mkfifoat(struct thread *td, struct mkfifoat_args *uap)
1384 return (kern_mkfifoat(td, uap->fd, uap->path, UIO_USERSPACE,
1389 kern_mkfifo(struct thread *td, char *path, enum uio_seg pathseg, int mode)
1392 return (kern_mkfifoat(td, AT_FDCWD, path, pathseg, mode));
1396 kern_mkfifoat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1402 struct nameidata nd;
1404 AUDIT_ARG_MODE(mode);
1407 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1,
1408 pathseg, path, fd, CAP_MKFIFOAT, td);
1409 if ((error = namei(&nd)) != 0)
1411 if (nd.ni_vp != NULL) {
1412 NDFREE(&nd, NDF_ONLY_PNBUF);
1413 if (nd.ni_vp == nd.ni_dvp)
1420 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1421 NDFREE(&nd, NDF_ONLY_PNBUF);
1423 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1428 vattr.va_type = VFIFO;
1429 vattr.va_mode = (mode & ALLPERMS) & ~td->td_proc->p_fd->fd_cmask;
1431 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1436 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
1443 vn_finished_write(mp);
1444 NDFREE(&nd, NDF_ONLY_PNBUF);
1449 * Make a hard file link.
1451 #ifndef _SYS_SYSPROTO_H_
1460 register struct link_args /* {
1466 return (kern_link(td, uap->path, uap->link, UIO_USERSPACE));
1469 #ifndef _SYS_SYSPROTO_H_
1470 struct linkat_args {
1479 sys_linkat(struct thread *td, struct linkat_args *uap)
1484 if (flag & ~AT_SYMLINK_FOLLOW)
1487 return (kern_linkat(td, uap->fd1, uap->fd2, uap->path1, uap->path2,
1488 UIO_USERSPACE, (flag & AT_SYMLINK_FOLLOW) ? FOLLOW : NOFOLLOW));
1491 int hardlink_check_uid = 0;
1492 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
1493 &hardlink_check_uid, 0,
1494 "Unprivileged processes cannot create hard links to files owned by other "
1496 static int hardlink_check_gid = 0;
1497 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
1498 &hardlink_check_gid, 0,
1499 "Unprivileged processes cannot create hard links to files owned by other "
1503 can_hardlink(struct vnode *vp, struct ucred *cred)
1508 if (!hardlink_check_uid && !hardlink_check_gid)
1511 error = VOP_GETATTR(vp, &va, cred);
1515 if (hardlink_check_uid && cred->cr_uid != va.va_uid) {
1516 error = priv_check_cred(cred, PRIV_VFS_LINK, 0);
1521 if (hardlink_check_gid && !groupmember(va.va_gid, cred)) {
1522 error = priv_check_cred(cred, PRIV_VFS_LINK, 0);
1531 kern_link(struct thread *td, char *path, char *link, enum uio_seg segflg)
1534 return (kern_linkat(td, AT_FDCWD, AT_FDCWD, path,link, segflg, FOLLOW));
1538 kern_linkat(struct thread *td, int fd1, int fd2, char *path1, char *path2,
1539 enum uio_seg segflg, int follow)
1543 struct nameidata nd;
1547 NDINIT_AT(&nd, LOOKUP, follow | AUDITVNODE1, segflg, path1, fd1, td);
1549 if ((error = namei(&nd)) != 0)
1551 NDFREE(&nd, NDF_ONLY_PNBUF);
1553 if (vp->v_type == VDIR) {
1555 return (EPERM); /* POSIX */
1557 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0) {
1561 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE2,
1562 segflg, path2, fd2, CAP_LINKAT, td);
1563 if ((error = namei(&nd)) == 0) {
1564 if (nd.ni_vp != NULL) {
1565 if (nd.ni_dvp == nd.ni_vp)
1571 } else if ((error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY))
1573 error = can_hardlink(vp, td->td_ucred);
1576 error = mac_vnode_check_link(td->td_ucred,
1577 nd.ni_dvp, vp, &nd.ni_cnd);
1580 error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
1584 NDFREE(&nd, NDF_ONLY_PNBUF);
1587 vn_finished_write(mp);
1592 * Make a symbolic link.
1594 #ifndef _SYS_SYSPROTO_H_
1595 struct symlink_args {
1601 sys_symlink(td, uap)
1603 register struct symlink_args /* {
1609 return (kern_symlink(td, uap->path, uap->link, UIO_USERSPACE));
1612 #ifndef _SYS_SYSPROTO_H_
1613 struct symlinkat_args {
1620 sys_symlinkat(struct thread *td, struct symlinkat_args *uap)
1623 return (kern_symlinkat(td, uap->path1, uap->fd, uap->path2,
1628 kern_symlink(struct thread *td, char *path, char *link, enum uio_seg segflg)
1631 return (kern_symlinkat(td, path, AT_FDCWD, link, segflg));
1635 kern_symlinkat(struct thread *td, char *path1, int fd, char *path2,
1636 enum uio_seg segflg)
1642 struct nameidata nd;
1644 if (segflg == UIO_SYSSPACE) {
1647 syspath = uma_zalloc(namei_zone, M_WAITOK);
1648 if ((error = copyinstr(path1, syspath, MAXPATHLEN, NULL)) != 0)
1651 AUDIT_ARG_TEXT(syspath);
1654 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1,
1655 segflg, path2, fd, CAP_SYMLINKAT, td);
1656 if ((error = namei(&nd)) != 0)
1659 NDFREE(&nd, NDF_ONLY_PNBUF);
1660 if (nd.ni_vp == nd.ni_dvp)
1668 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1669 NDFREE(&nd, NDF_ONLY_PNBUF);
1671 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1676 vattr.va_mode = ACCESSPERMS &~ td->td_proc->p_fd->fd_cmask;
1678 vattr.va_type = VLNK;
1679 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1684 error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr, syspath);
1690 NDFREE(&nd, NDF_ONLY_PNBUF);
1692 vn_finished_write(mp);
1694 if (segflg != UIO_SYSSPACE)
1695 uma_zfree(namei_zone, syspath);
1700 * Delete a whiteout from the filesystem.
1703 sys_undelete(td, uap)
1705 register struct undelete_args /* {
1711 struct nameidata nd;
1715 NDINIT(&nd, DELETE, LOCKPARENT | DOWHITEOUT | AUDITVNODE1,
1716 UIO_USERSPACE, uap->path, td);
1721 if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & ISWHITEOUT)) {
1722 NDFREE(&nd, NDF_ONLY_PNBUF);
1723 if (nd.ni_vp == nd.ni_dvp)
1731 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1732 NDFREE(&nd, NDF_ONLY_PNBUF);
1734 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1738 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, DELETE);
1739 NDFREE(&nd, NDF_ONLY_PNBUF);
1741 vn_finished_write(mp);
1746 * Delete a name from the filesystem.
1748 #ifndef _SYS_SYSPROTO_H_
1749 struct unlink_args {
1756 struct unlink_args /* {
1761 return (kern_unlink(td, uap->path, UIO_USERSPACE));
1764 #ifndef _SYS_SYSPROTO_H_
1765 struct unlinkat_args {
1772 sys_unlinkat(struct thread *td, struct unlinkat_args *uap)
1774 int flag = uap->flag;
1776 char *path = uap->path;
1778 if (flag & ~AT_REMOVEDIR)
1781 if (flag & AT_REMOVEDIR)
1782 return (kern_rmdirat(td, fd, path, UIO_USERSPACE));
1784 return (kern_unlinkat(td, fd, path, UIO_USERSPACE, 0));
1788 kern_unlink(struct thread *td, char *path, enum uio_seg pathseg)
1791 return (kern_unlinkat(td, AT_FDCWD, path, pathseg, 0));
1795 kern_unlinkat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1801 struct nameidata nd;
1806 NDINIT_ATRIGHTS(&nd, DELETE, LOCKPARENT | LOCKLEAF | AUDITVNODE1,
1807 pathseg, path, fd, CAP_UNLINKAT, td);
1808 if ((error = namei(&nd)) != 0)
1809 return (error == EINVAL ? EPERM : error);
1811 if (vp->v_type == VDIR && oldinum == 0) {
1812 error = EPERM; /* POSIX */
1813 } else if (oldinum != 0 &&
1814 ((error = vn_stat(vp, &sb, td->td_ucred, NOCRED, td)) == 0) &&
1815 sb.st_ino != oldinum) {
1816 error = EIDRM; /* Identifier removed */
1819 * The root of a mounted filesystem cannot be deleted.
1821 * XXX: can this only be a VDIR case?
1823 if (vp->v_vflag & VV_ROOT)
1827 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1828 NDFREE(&nd, NDF_ONLY_PNBUF);
1830 if (vp == nd.ni_dvp)
1834 if ((error = vn_start_write(NULL, &mp,
1835 V_XSLEEP | PCATCH)) != 0)
1840 error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp,
1845 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_UNLINK);
1846 error = VOP_REMOVE(nd.ni_dvp, vp, &nd.ni_cnd);
1850 vn_finished_write(mp);
1852 NDFREE(&nd, NDF_ONLY_PNBUF);
1854 if (vp == nd.ni_dvp)
1862 * Reposition read/write file offset.
1864 #ifndef _SYS_SYSPROTO_H_
1875 register struct lseek_args /* {
1885 AUDIT_ARG_FD(uap->fd);
1886 if ((error = fget(td, uap->fd, CAP_SEEK, &fp)) != 0)
1888 error = (fp->f_ops->fo_flags & DFLAG_SEEKABLE) != 0 ?
1889 fo_seek(fp, uap->offset, uap->whence, td) : ESPIPE;
1894 #if defined(COMPAT_43)
1896 * Reposition read/write file offset.
1898 #ifndef _SYS_SYSPROTO_H_
1899 struct olseek_args {
1908 register struct olseek_args /* {
1914 struct lseek_args /* {
1922 nuap.offset = uap->offset;
1923 nuap.whence = uap->whence;
1924 return (sys_lseek(td, &nuap));
1926 #endif /* COMPAT_43 */
1928 /* Version with the 'pad' argument */
1930 freebsd6_lseek(td, uap)
1932 register struct freebsd6_lseek_args *uap;
1934 struct lseek_args ouap;
1937 ouap.offset = uap->offset;
1938 ouap.whence = uap->whence;
1939 return (sys_lseek(td, &ouap));
1943 * Check access permissions using passed credentials.
1946 vn_access(vp, user_flags, cred, td)
1955 /* Flags == 0 means only check for existence. */
1959 if (user_flags & R_OK)
1961 if (user_flags & W_OK)
1963 if (user_flags & X_OK)
1966 error = mac_vnode_check_access(cred, vp, accmode);
1970 if ((accmode & VWRITE) == 0 || (error = vn_writechk(vp)) == 0)
1971 error = VOP_ACCESS(vp, accmode, cred, td);
1977 * Check access permissions using "real" credentials.
1979 #ifndef _SYS_SYSPROTO_H_
1980 struct access_args {
1988 register struct access_args /* {
1994 return (kern_access(td, uap->path, UIO_USERSPACE, uap->amode));
1997 #ifndef _SYS_SYSPROTO_H_
1998 struct faccessat_args {
2006 sys_faccessat(struct thread *td, struct faccessat_args *uap)
2009 if (uap->flag & ~AT_EACCESS)
2011 return (kern_accessat(td, uap->fd, uap->path, UIO_USERSPACE, uap->flag,
2016 kern_access(struct thread *td, char *path, enum uio_seg pathseg, int amode)
2019 return (kern_accessat(td, AT_FDCWD, path, pathseg, 0, amode));
2023 kern_accessat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2024 int flag, int amode)
2026 struct ucred *cred, *tmpcred;
2028 struct nameidata nd;
2032 * Create and modify a temporary credential instead of one that
2033 * is potentially shared.
2035 if (!(flag & AT_EACCESS)) {
2036 cred = td->td_ucred;
2037 tmpcred = crdup(cred);
2038 tmpcred->cr_uid = cred->cr_ruid;
2039 tmpcred->cr_groups[0] = cred->cr_rgid;
2040 td->td_ucred = tmpcred;
2042 cred = tmpcred = td->td_ucred;
2043 AUDIT_ARG_VALUE(amode);
2044 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF |
2045 AUDITVNODE1, pathseg, path, fd, CAP_FSTAT, td);
2046 if ((error = namei(&nd)) != 0)
2050 error = vn_access(vp, amode, tmpcred, td);
2051 NDFREE(&nd, NDF_ONLY_PNBUF);
2054 if (!(flag & AT_EACCESS)) {
2055 td->td_ucred = cred;
2062 * Check access permissions using "effective" credentials.
2064 #ifndef _SYS_SYSPROTO_H_
2065 struct eaccess_args {
2071 sys_eaccess(td, uap)
2073 register struct eaccess_args /* {
2079 return (kern_eaccess(td, uap->path, UIO_USERSPACE, uap->amode));
2083 kern_eaccess(struct thread *td, char *path, enum uio_seg pathseg, int amode)
2086 return (kern_accessat(td, AT_FDCWD, path, pathseg, AT_EACCESS, amode));
2089 #if defined(COMPAT_43)
2091 * Get file status; this version follows links.
2093 #ifndef _SYS_SYSPROTO_H_
2102 register struct ostat_args /* {
2111 error = kern_stat(td, uap->path, UIO_USERSPACE, &sb);
2115 error = copyout(&osb, uap->ub, sizeof (osb));
2120 * Get file status; this version does not follow links.
2122 #ifndef _SYS_SYSPROTO_H_
2123 struct olstat_args {
2131 register struct olstat_args /* {
2140 error = kern_lstat(td, uap->path, UIO_USERSPACE, &sb);
2144 error = copyout(&osb, uap->ub, sizeof (osb));
2149 * Convert from an old to a new stat structure.
2157 ost->st_dev = st->st_dev;
2158 ost->st_ino = st->st_ino;
2159 ost->st_mode = st->st_mode;
2160 ost->st_nlink = st->st_nlink;
2161 ost->st_uid = st->st_uid;
2162 ost->st_gid = st->st_gid;
2163 ost->st_rdev = st->st_rdev;
2164 if (st->st_size < (quad_t)1 << 32)
2165 ost->st_size = st->st_size;
2168 ost->st_atim = st->st_atim;
2169 ost->st_mtim = st->st_mtim;
2170 ost->st_ctim = st->st_ctim;
2171 ost->st_blksize = st->st_blksize;
2172 ost->st_blocks = st->st_blocks;
2173 ost->st_flags = st->st_flags;
2174 ost->st_gen = st->st_gen;
2176 #endif /* COMPAT_43 */
2179 * Get file status; this version follows links.
2181 #ifndef _SYS_SYSPROTO_H_
2190 register struct stat_args /* {
2198 error = kern_stat(td, uap->path, UIO_USERSPACE, &sb);
2200 error = copyout(&sb, uap->ub, sizeof (sb));
2204 #ifndef _SYS_SYSPROTO_H_
2205 struct fstatat_args {
2213 sys_fstatat(struct thread *td, struct fstatat_args *uap)
2218 error = kern_statat(td, uap->flag, uap->fd, uap->path,
2219 UIO_USERSPACE, &sb);
2221 error = copyout(&sb, uap->buf, sizeof (sb));
2226 kern_stat(struct thread *td, char *path, enum uio_seg pathseg, struct stat *sbp)
2229 return (kern_statat(td, 0, AT_FDCWD, path, pathseg, sbp));
2233 kern_statat(struct thread *td, int flag, int fd, char *path,
2234 enum uio_seg pathseg, struct stat *sbp)
2237 return (kern_statat_vnhook(td, flag, fd, path, pathseg, sbp, NULL));
2241 kern_statat_vnhook(struct thread *td, int flag, int fd, char *path,
2242 enum uio_seg pathseg, struct stat *sbp,
2243 void (*hook)(struct vnode *vp, struct stat *sbp))
2245 struct nameidata nd;
2249 if (flag & ~AT_SYMLINK_NOFOLLOW)
2252 NDINIT_ATRIGHTS(&nd, LOOKUP, ((flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW :
2253 FOLLOW) | LOCKSHARED | LOCKLEAF | AUDITVNODE1, pathseg, path, fd,
2256 if ((error = namei(&nd)) != 0)
2258 error = vn_stat(nd.ni_vp, &sb, td->td_ucred, NOCRED, td);
2260 SDT_PROBE(vfs, , stat, mode, path, sb.st_mode, 0, 0, 0);
2261 if (S_ISREG(sb.st_mode))
2262 SDT_PROBE(vfs, , stat, reg, path, pathseg, 0, 0, 0);
2263 if (__predict_false(hook != NULL))
2264 hook(nd.ni_vp, &sb);
2266 NDFREE(&nd, NDF_ONLY_PNBUF);
2272 if (KTRPOINT(td, KTR_STRUCT))
2279 * Get file status; this version does not follow links.
2281 #ifndef _SYS_SYSPROTO_H_
2290 register struct lstat_args /* {
2298 error = kern_lstat(td, uap->path, UIO_USERSPACE, &sb);
2300 error = copyout(&sb, uap->ub, sizeof (sb));
2305 kern_lstat(struct thread *td, char *path, enum uio_seg pathseg, struct stat *sbp)
2308 return (kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, path, pathseg,
2313 * Implementation of the NetBSD [l]stat() functions.
2320 bzero(nsb, sizeof *nsb);
2321 nsb->st_dev = sb->st_dev;
2322 nsb->st_ino = sb->st_ino;
2323 nsb->st_mode = sb->st_mode;
2324 nsb->st_nlink = sb->st_nlink;
2325 nsb->st_uid = sb->st_uid;
2326 nsb->st_gid = sb->st_gid;
2327 nsb->st_rdev = sb->st_rdev;
2328 nsb->st_atim = sb->st_atim;
2329 nsb->st_mtim = sb->st_mtim;
2330 nsb->st_ctim = sb->st_ctim;
2331 nsb->st_size = sb->st_size;
2332 nsb->st_blocks = sb->st_blocks;
2333 nsb->st_blksize = sb->st_blksize;
2334 nsb->st_flags = sb->st_flags;
2335 nsb->st_gen = sb->st_gen;
2336 nsb->st_birthtim = sb->st_birthtim;
2339 #ifndef _SYS_SYSPROTO_H_
2348 register struct nstat_args /* {
2357 error = kern_stat(td, uap->path, UIO_USERSPACE, &sb);
2360 cvtnstat(&sb, &nsb);
2361 error = copyout(&nsb, uap->ub, sizeof (nsb));
2366 * NetBSD lstat. Get file status; this version does not follow links.
2368 #ifndef _SYS_SYSPROTO_H_
2377 register struct nlstat_args /* {
2386 error = kern_lstat(td, uap->path, UIO_USERSPACE, &sb);
2389 cvtnstat(&sb, &nsb);
2390 error = copyout(&nsb, uap->ub, sizeof (nsb));
2395 * Get configurable pathname variables.
2397 #ifndef _SYS_SYSPROTO_H_
2398 struct pathconf_args {
2404 sys_pathconf(td, uap)
2406 register struct pathconf_args /* {
2412 return (kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name, FOLLOW));
2415 #ifndef _SYS_SYSPROTO_H_
2416 struct lpathconf_args {
2422 sys_lpathconf(td, uap)
2424 register struct lpathconf_args /* {
2430 return (kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name,
2435 kern_pathconf(struct thread *td, char *path, enum uio_seg pathseg, int name,
2438 struct nameidata nd;
2441 NDINIT(&nd, LOOKUP, LOCKSHARED | LOCKLEAF | AUDITVNODE1 | flags,
2443 if ((error = namei(&nd)) != 0)
2445 NDFREE(&nd, NDF_ONLY_PNBUF);
2447 /* If asynchronous I/O is available, it works for all files. */
2448 if (name == _PC_ASYNC_IO)
2449 td->td_retval[0] = async_io_version;
2451 error = VOP_PATHCONF(nd.ni_vp, name, td->td_retval);
2457 * Return target name of a symbolic link.
2459 #ifndef _SYS_SYSPROTO_H_
2460 struct readlink_args {
2467 sys_readlink(td, uap)
2469 register struct readlink_args /* {
2476 return (kern_readlink(td, uap->path, UIO_USERSPACE, uap->buf,
2477 UIO_USERSPACE, uap->count));
2479 #ifndef _SYS_SYSPROTO_H_
2480 struct readlinkat_args {
2488 sys_readlinkat(struct thread *td, struct readlinkat_args *uap)
2491 return (kern_readlinkat(td, uap->fd, uap->path, UIO_USERSPACE,
2492 uap->buf, UIO_USERSPACE, uap->bufsize));
2496 kern_readlink(struct thread *td, char *path, enum uio_seg pathseg, char *buf,
2497 enum uio_seg bufseg, size_t count)
2500 return (kern_readlinkat(td, AT_FDCWD, path, pathseg, buf, bufseg,
2505 kern_readlinkat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2506 char *buf, enum uio_seg bufseg, size_t count)
2512 struct nameidata nd;
2514 if (count > IOSIZE_MAX)
2517 NDINIT_AT(&nd, LOOKUP, NOFOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
2518 pathseg, path, fd, td);
2520 if ((error = namei(&nd)) != 0)
2522 NDFREE(&nd, NDF_ONLY_PNBUF);
2525 error = mac_vnode_check_readlink(td->td_ucred, vp);
2531 if (vp->v_type != VLNK)
2534 aiov.iov_base = buf;
2535 aiov.iov_len = count;
2536 auio.uio_iov = &aiov;
2537 auio.uio_iovcnt = 1;
2538 auio.uio_offset = 0;
2539 auio.uio_rw = UIO_READ;
2540 auio.uio_segflg = bufseg;
2542 auio.uio_resid = count;
2543 error = VOP_READLINK(vp, &auio, td->td_ucred);
2546 td->td_retval[0] = count - auio.uio_resid;
2551 * Common implementation code for chflags() and fchflags().
2554 setfflags(td, vp, flags)
2563 /* We can't support the value matching VNOVAL. */
2564 if (flags == VNOVAL)
2565 return (EOPNOTSUPP);
2568 * Prevent non-root users from setting flags on devices. When
2569 * a device is reused, users can retain ownership of the device
2570 * if they are allowed to set flags and programs assume that
2571 * chown can't fail when done as root.
2573 if (vp->v_type == VCHR || vp->v_type == VBLK) {
2574 error = priv_check(td, PRIV_VFS_CHFLAGS_DEV);
2579 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2582 vattr.va_flags = flags;
2583 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2585 error = mac_vnode_check_setflags(td->td_ucred, vp, vattr.va_flags);
2588 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
2590 vn_finished_write(mp);
2595 * Change flags of a file given a path name.
2597 #ifndef _SYS_SYSPROTO_H_
2598 struct chflags_args {
2604 sys_chflags(td, uap)
2606 register struct chflags_args /* {
2612 return (kern_chflags(td, uap->path, UIO_USERSPACE, uap->flags));
2615 #ifndef _SYS_SYSPROTO_H_
2616 struct chflagsat_args {
2624 sys_chflagsat(struct thread *td, struct chflagsat_args *uap)
2627 const char *path = uap->path;
2628 u_long flags = uap->flags;
2629 int atflag = uap->atflag;
2631 if (atflag & ~AT_SYMLINK_NOFOLLOW)
2634 return (kern_chflagsat(td, fd, path, UIO_USERSPACE, flags, atflag));
2638 kern_chflags(struct thread *td, const char *path, enum uio_seg pathseg,
2642 return (kern_chflagsat(td, AT_FDCWD, path, pathseg, flags, 0));
2646 * Same as chflags() but doesn't follow symlinks.
2649 sys_lchflags(td, uap)
2651 register struct lchflags_args /* {
2657 return (kern_chflagsat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2658 uap->flags, AT_SYMLINK_NOFOLLOW));
2662 kern_chflagsat(struct thread *td, int fd, const char *path,
2663 enum uio_seg pathseg, u_long flags, int atflag)
2665 struct nameidata nd;
2668 AUDIT_ARG_FFLAGS(flags);
2669 follow = (atflag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
2670 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, pathseg, path, fd,
2672 if ((error = namei(&nd)) != 0)
2674 NDFREE(&nd, NDF_ONLY_PNBUF);
2675 error = setfflags(td, nd.ni_vp, flags);
2681 * Change flags of a file given a file descriptor.
2683 #ifndef _SYS_SYSPROTO_H_
2684 struct fchflags_args {
2690 sys_fchflags(td, uap)
2692 register struct fchflags_args /* {
2700 AUDIT_ARG_FD(uap->fd);
2701 AUDIT_ARG_FFLAGS(uap->flags);
2702 if ((error = getvnode(td->td_proc->p_fd, uap->fd, CAP_FCHFLAGS,
2706 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
2707 AUDIT_ARG_VNODE1(fp->f_vnode);
2708 VOP_UNLOCK(fp->f_vnode, 0);
2710 error = setfflags(td, fp->f_vnode, uap->flags);
2716 * Common implementation code for chmod(), lchmod() and fchmod().
2719 setfmode(td, cred, vp, mode)
2729 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2731 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2733 vattr.va_mode = mode & ALLPERMS;
2735 error = mac_vnode_check_setmode(cred, vp, vattr.va_mode);
2738 error = VOP_SETATTR(vp, &vattr, cred);
2740 vn_finished_write(mp);
2745 * Change mode of a file given path name.
2747 #ifndef _SYS_SYSPROTO_H_
2756 register struct chmod_args /* {
2762 return (kern_chmod(td, uap->path, UIO_USERSPACE, uap->mode));
2765 #ifndef _SYS_SYSPROTO_H_
2766 struct fchmodat_args {
2774 sys_fchmodat(struct thread *td, struct fchmodat_args *uap)
2776 int flag = uap->flag;
2778 char *path = uap->path;
2779 mode_t mode = uap->mode;
2781 if (flag & ~AT_SYMLINK_NOFOLLOW)
2784 return (kern_fchmodat(td, fd, path, UIO_USERSPACE, mode, flag));
2788 kern_chmod(struct thread *td, char *path, enum uio_seg pathseg, int mode)
2791 return (kern_fchmodat(td, AT_FDCWD, path, pathseg, mode, 0));
2795 * Change mode of a file given path name (don't follow links.)
2797 #ifndef _SYS_SYSPROTO_H_
2798 struct lchmod_args {
2806 register struct lchmod_args /* {
2812 return (kern_fchmodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2813 uap->mode, AT_SYMLINK_NOFOLLOW));
2817 kern_fchmodat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2818 mode_t mode, int flag)
2821 struct nameidata nd;
2824 AUDIT_ARG_MODE(mode);
2825 follow = (flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
2826 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, pathseg, path, fd,
2828 if ((error = namei(&nd)) != 0)
2830 NDFREE(&nd, NDF_ONLY_PNBUF);
2831 error = setfmode(td, td->td_ucred, nd.ni_vp, mode);
2837 * Change mode of a file given a file descriptor.
2839 #ifndef _SYS_SYSPROTO_H_
2840 struct fchmod_args {
2846 sys_fchmod(struct thread *td, struct fchmod_args *uap)
2851 AUDIT_ARG_FD(uap->fd);
2852 AUDIT_ARG_MODE(uap->mode);
2854 error = fget(td, uap->fd, CAP_FCHMOD, &fp);
2857 error = fo_chmod(fp, uap->mode, td->td_ucred, td);
2863 * Common implementation for chown(), lchown(), and fchown()
2866 setfown(td, cred, vp, uid, gid)
2877 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2879 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2884 error = mac_vnode_check_setowner(cred, vp, vattr.va_uid,
2888 error = VOP_SETATTR(vp, &vattr, cred);
2890 vn_finished_write(mp);
2895 * Set ownership given a path name.
2897 #ifndef _SYS_SYSPROTO_H_
2907 register struct chown_args /* {
2914 return (kern_chown(td, uap->path, UIO_USERSPACE, uap->uid, uap->gid));
2917 #ifndef _SYS_SYSPROTO_H_
2918 struct fchownat_args {
2927 sys_fchownat(struct thread *td, struct fchownat_args *uap)
2932 if (flag & ~AT_SYMLINK_NOFOLLOW)
2935 return (kern_fchownat(td, uap->fd, uap->path, UIO_USERSPACE, uap->uid,
2936 uap->gid, uap->flag));
2940 kern_chown(struct thread *td, char *path, enum uio_seg pathseg, int uid,
2944 return (kern_fchownat(td, AT_FDCWD, path, pathseg, uid, gid, 0));
2948 kern_fchownat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2949 int uid, int gid, int flag)
2951 struct nameidata nd;
2954 AUDIT_ARG_OWNER(uid, gid);
2955 follow = (flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
2956 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, pathseg, path, fd,
2959 if ((error = namei(&nd)) != 0)
2961 NDFREE(&nd, NDF_ONLY_PNBUF);
2962 error = setfown(td, td->td_ucred, nd.ni_vp, uid, gid);
2968 * Set ownership given a path name, do not cross symlinks.
2970 #ifndef _SYS_SYSPROTO_H_
2971 struct lchown_args {
2980 register struct lchown_args /* {
2987 return (kern_lchown(td, uap->path, UIO_USERSPACE, uap->uid, uap->gid));
2991 kern_lchown(struct thread *td, char *path, enum uio_seg pathseg, int uid,
2995 return (kern_fchownat(td, AT_FDCWD, path, pathseg, uid, gid,
2996 AT_SYMLINK_NOFOLLOW));
3000 * Set ownership given a file descriptor.
3002 #ifndef _SYS_SYSPROTO_H_
3003 struct fchown_args {
3012 register struct fchown_args /* {
3021 AUDIT_ARG_FD(uap->fd);
3022 AUDIT_ARG_OWNER(uap->uid, uap->gid);
3023 error = fget(td, uap->fd, CAP_FCHOWN, &fp);
3026 error = fo_chown(fp, uap->uid, uap->gid, td->td_ucred, td);
3032 * Common implementation code for utimes(), lutimes(), and futimes().
3035 getutimes(usrtvp, tvpseg, tsp)
3036 const struct timeval *usrtvp;
3037 enum uio_seg tvpseg;
3038 struct timespec *tsp;
3040 struct timeval tv[2];
3041 const struct timeval *tvp;
3044 if (usrtvp == NULL) {
3045 vfs_timestamp(&tsp[0]);
3048 if (tvpseg == UIO_SYSSPACE) {
3051 if ((error = copyin(usrtvp, tv, sizeof(tv))) != 0)
3056 if (tvp[0].tv_usec < 0 || tvp[0].tv_usec >= 1000000 ||
3057 tvp[1].tv_usec < 0 || tvp[1].tv_usec >= 1000000)
3059 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
3060 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
3066 * Common implementation code for utimes(), lutimes(), and futimes().
3069 setutimes(td, vp, ts, numtimes, nullflag)
3072 const struct timespec *ts;
3076 int error, setbirthtime;
3080 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
3082 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3084 if (numtimes < 3 && !VOP_GETATTR(vp, &vattr, td->td_ucred) &&
3085 timespeccmp(&ts[1], &vattr.va_birthtime, < ))
3088 vattr.va_atime = ts[0];
3089 vattr.va_mtime = ts[1];
3091 vattr.va_birthtime = ts[1];
3093 vattr.va_birthtime = ts[2];
3095 vattr.va_vaflags |= VA_UTIMES_NULL;
3097 error = mac_vnode_check_setutimes(td->td_ucred, vp, vattr.va_atime,
3101 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3103 vn_finished_write(mp);
3108 * Set the access and modification times of a file.
3110 #ifndef _SYS_SYSPROTO_H_
3111 struct utimes_args {
3113 struct timeval *tptr;
3119 register struct utimes_args /* {
3121 struct timeval *tptr;
3125 return (kern_utimes(td, uap->path, UIO_USERSPACE, uap->tptr,
3129 #ifndef _SYS_SYSPROTO_H_
3130 struct futimesat_args {
3133 const struct timeval * times;
3137 sys_futimesat(struct thread *td, struct futimesat_args *uap)
3140 return (kern_utimesat(td, uap->fd, uap->path, UIO_USERSPACE,
3141 uap->times, UIO_USERSPACE));
3145 kern_utimes(struct thread *td, char *path, enum uio_seg pathseg,
3146 struct timeval *tptr, enum uio_seg tptrseg)
3149 return (kern_utimesat(td, AT_FDCWD, path, pathseg, tptr, tptrseg));
3153 kern_utimesat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
3154 struct timeval *tptr, enum uio_seg tptrseg)
3156 struct nameidata nd;
3157 struct timespec ts[2];
3160 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3162 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, fd,
3165 if ((error = namei(&nd)) != 0)
3167 NDFREE(&nd, NDF_ONLY_PNBUF);
3168 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3174 * Set the access and modification times of a file.
3176 #ifndef _SYS_SYSPROTO_H_
3177 struct lutimes_args {
3179 struct timeval *tptr;
3183 sys_lutimes(td, uap)
3185 register struct lutimes_args /* {
3187 struct timeval *tptr;
3191 return (kern_lutimes(td, uap->path, UIO_USERSPACE, uap->tptr,
3196 kern_lutimes(struct thread *td, char *path, enum uio_seg pathseg,
3197 struct timeval *tptr, enum uio_seg tptrseg)
3199 struct timespec ts[2];
3201 struct nameidata nd;
3203 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3205 NDINIT(&nd, LOOKUP, NOFOLLOW | AUDITVNODE1, pathseg, path, td);
3206 if ((error = namei(&nd)) != 0)
3208 NDFREE(&nd, NDF_ONLY_PNBUF);
3209 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3215 * Set the access and modification times of a file.
3217 #ifndef _SYS_SYSPROTO_H_
3218 struct futimes_args {
3220 struct timeval *tptr;
3224 sys_futimes(td, uap)
3226 register struct futimes_args /* {
3228 struct timeval *tptr;
3232 return (kern_futimes(td, uap->fd, uap->tptr, UIO_USERSPACE));
3236 kern_futimes(struct thread *td, int fd, struct timeval *tptr,
3237 enum uio_seg tptrseg)
3239 struct timespec ts[2];
3244 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3246 if ((error = getvnode(td->td_proc->p_fd, fd, CAP_FUTIMES, &fp)) != 0)
3249 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
3250 AUDIT_ARG_VNODE1(fp->f_vnode);
3251 VOP_UNLOCK(fp->f_vnode, 0);
3253 error = setutimes(td, fp->f_vnode, ts, 2, tptr == NULL);
3259 * Truncate a file given its path name.
3261 #ifndef _SYS_SYSPROTO_H_
3262 struct truncate_args {
3269 sys_truncate(td, uap)
3271 register struct truncate_args /* {
3278 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3282 kern_truncate(struct thread *td, char *path, enum uio_seg pathseg, off_t length)
3288 struct nameidata nd;
3293 NDINIT(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, td);
3294 if ((error = namei(&nd)) != 0)
3297 rl_cookie = vn_rangelock_wlock(vp, 0, OFF_MAX);
3298 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0) {
3299 vn_rangelock_unlock(vp, rl_cookie);
3303 NDFREE(&nd, NDF_ONLY_PNBUF);
3304 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3305 if (vp->v_type == VDIR)
3308 else if ((error = mac_vnode_check_write(td->td_ucred, NOCRED, vp))) {
3311 else if ((error = vn_writechk(vp)) == 0 &&
3312 (error = VOP_ACCESS(vp, VWRITE, td->td_ucred, td)) == 0) {
3314 vattr.va_size = length;
3315 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3318 vn_finished_write(mp);
3319 vn_rangelock_unlock(vp, rl_cookie);
3324 #if defined(COMPAT_43)
3326 * Truncate a file given its path name.
3328 #ifndef _SYS_SYSPROTO_H_
3329 struct otruncate_args {
3337 register struct otruncate_args /* {
3342 struct truncate_args /* {
3348 nuap.path = uap->path;
3349 nuap.length = uap->length;
3350 return (sys_truncate(td, &nuap));
3352 #endif /* COMPAT_43 */
3354 /* Versions with the pad argument */
3356 freebsd6_truncate(struct thread *td, struct freebsd6_truncate_args *uap)
3358 struct truncate_args ouap;
3360 ouap.path = uap->path;
3361 ouap.length = uap->length;
3362 return (sys_truncate(td, &ouap));
3366 freebsd6_ftruncate(struct thread *td, struct freebsd6_ftruncate_args *uap)
3368 struct ftruncate_args ouap;
3371 ouap.length = uap->length;
3372 return (sys_ftruncate(td, &ouap));
3376 * Sync an open file.
3378 #ifndef _SYS_SYSPROTO_H_
3386 struct fsync_args /* {
3393 int error, lock_flags;
3395 AUDIT_ARG_FD(uap->fd);
3396 if ((error = getvnode(td->td_proc->p_fd, uap->fd, CAP_FSYNC, &fp)) != 0)
3399 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
3401 if (MNT_SHARED_WRITES(mp) ||
3402 ((mp == NULL) && MNT_SHARED_WRITES(vp->v_mount))) {
3403 lock_flags = LK_SHARED;
3405 lock_flags = LK_EXCLUSIVE;
3407 vn_lock(vp, lock_flags | LK_RETRY);
3408 AUDIT_ARG_VNODE1(vp);
3409 if (vp->v_object != NULL) {
3410 VM_OBJECT_WLOCK(vp->v_object);
3411 vm_object_page_clean(vp->v_object, 0, 0, 0);
3412 VM_OBJECT_WUNLOCK(vp->v_object);
3414 error = VOP_FSYNC(vp, MNT_WAIT, td);
3417 vn_finished_write(mp);
3424 * Rename files. Source and destination must either both be directories, or
3425 * both not be directories. If target is a directory, it must be empty.
3427 #ifndef _SYS_SYSPROTO_H_
3428 struct rename_args {
3436 register struct rename_args /* {
3442 return (kern_rename(td, uap->from, uap->to, UIO_USERSPACE));
3445 #ifndef _SYS_SYSPROTO_H_
3446 struct renameat_args {
3454 sys_renameat(struct thread *td, struct renameat_args *uap)
3457 return (kern_renameat(td, uap->oldfd, uap->old, uap->newfd, uap->new,
3462 kern_rename(struct thread *td, char *from, char *to, enum uio_seg pathseg)
3465 return (kern_renameat(td, AT_FDCWD, from, AT_FDCWD, to, pathseg));
3469 kern_renameat(struct thread *td, int oldfd, char *old, int newfd, char *new,
3470 enum uio_seg pathseg)
3472 struct mount *mp = NULL;
3473 struct vnode *tvp, *fvp, *tdvp;
3474 struct nameidata fromnd, tond;
3479 NDINIT_ATRIGHTS(&fromnd, DELETE, LOCKPARENT | LOCKLEAF | SAVESTART |
3480 AUDITVNODE1, pathseg, old, oldfd, CAP_RENAMEAT, td);
3482 NDINIT_ATRIGHTS(&fromnd, DELETE, WANTPARENT | SAVESTART | AUDITVNODE1,
3483 pathseg, old, oldfd, CAP_RENAMEAT, td);
3486 if ((error = namei(&fromnd)) != 0)
3489 error = mac_vnode_check_rename_from(td->td_ucred, fromnd.ni_dvp,
3490 fromnd.ni_vp, &fromnd.ni_cnd);
3491 VOP_UNLOCK(fromnd.ni_dvp, 0);
3492 if (fromnd.ni_dvp != fromnd.ni_vp)
3493 VOP_UNLOCK(fromnd.ni_vp, 0);
3497 error = vn_start_write(fvp, &mp, V_WAIT | PCATCH);
3499 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3500 vrele(fromnd.ni_dvp);
3504 NDINIT_ATRIGHTS(&tond, RENAME, LOCKPARENT | LOCKLEAF | NOCACHE |
3505 SAVESTART | AUDITVNODE2, pathseg, new, newfd, CAP_LINKAT, td);
3506 if (fromnd.ni_vp->v_type == VDIR)
3507 tond.ni_cnd.cn_flags |= WILLBEDIR;
3508 if ((error = namei(&tond)) != 0) {
3509 /* Translate error code for rename("dir1", "dir2/."). */
3510 if (error == EISDIR && fvp->v_type == VDIR)
3512 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3513 vrele(fromnd.ni_dvp);
3515 vn_finished_write(mp);
3521 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
3524 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
3529 if (newfd != AT_FDCWD) {
3531 * If the target already exists we require CAP_UNLINKAT
3534 error = cap_check(tond.ni_filecaps.fc_rights,
3546 * If the source is the same as the destination (that is, if they
3547 * are links to the same vnode), then there is nothing to do.
3553 error = mac_vnode_check_rename_to(td->td_ucred, tdvp,
3554 tond.ni_vp, fromnd.ni_dvp == tdvp, &tond.ni_cnd);
3558 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
3559 tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
3560 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3561 NDFREE(&tond, NDF_ONLY_PNBUF);
3563 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3564 NDFREE(&tond, NDF_ONLY_PNBUF);
3571 vrele(fromnd.ni_dvp);
3574 vrele(tond.ni_startdir);
3575 vn_finished_write(mp);
3577 if (fromnd.ni_startdir)
3578 vrele(fromnd.ni_startdir);
3585 * Make a directory file.
3587 #ifndef _SYS_SYSPROTO_H_
3596 register struct mkdir_args /* {
3602 return (kern_mkdir(td, uap->path, UIO_USERSPACE, uap->mode));
3605 #ifndef _SYS_SYSPROTO_H_
3606 struct mkdirat_args {
3613 sys_mkdirat(struct thread *td, struct mkdirat_args *uap)
3616 return (kern_mkdirat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode));
3620 kern_mkdir(struct thread *td, char *path, enum uio_seg segflg, int mode)
3623 return (kern_mkdirat(td, AT_FDCWD, path, segflg, mode));
3627 kern_mkdirat(struct thread *td, int fd, char *path, enum uio_seg segflg,
3634 struct nameidata nd;
3636 AUDIT_ARG_MODE(mode);
3639 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1,
3640 segflg, path, fd, CAP_MKDIRAT, td);
3641 nd.ni_cnd.cn_flags |= WILLBEDIR;
3642 if ((error = namei(&nd)) != 0)
3646 NDFREE(&nd, NDF_ONLY_PNBUF);
3648 * XXX namei called with LOCKPARENT but not LOCKLEAF has
3649 * the strange behaviour of leaving the vnode unlocked
3650 * if the target is the same vnode as the parent.
3652 if (vp == nd.ni_dvp)
3659 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3660 NDFREE(&nd, NDF_ONLY_PNBUF);
3662 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3667 vattr.va_type = VDIR;
3668 vattr.va_mode = (mode & ACCESSPERMS) &~ td->td_proc->p_fd->fd_cmask;
3670 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
3675 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
3679 NDFREE(&nd, NDF_ONLY_PNBUF);
3683 vn_finished_write(mp);
3688 * Remove a directory file.
3690 #ifndef _SYS_SYSPROTO_H_
3698 struct rmdir_args /* {
3703 return (kern_rmdir(td, uap->path, UIO_USERSPACE));
3707 kern_rmdir(struct thread *td, char *path, enum uio_seg pathseg)
3710 return (kern_rmdirat(td, AT_FDCWD, path, pathseg));
3714 kern_rmdirat(struct thread *td, int fd, char *path, enum uio_seg pathseg)
3719 struct nameidata nd;
3723 NDINIT_ATRIGHTS(&nd, DELETE, LOCKPARENT | LOCKLEAF | AUDITVNODE1,
3724 pathseg, path, fd, CAP_UNLINKAT, td);
3725 if ((error = namei(&nd)) != 0)
3728 if (vp->v_type != VDIR) {
3733 * No rmdir "." please.
3735 if (nd.ni_dvp == vp) {
3740 * The root of a mounted filesystem cannot be deleted.
3742 if (vp->v_vflag & VV_ROOT) {
3747 error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp,
3752 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3753 NDFREE(&nd, NDF_ONLY_PNBUF);
3755 if (nd.ni_dvp == vp)
3759 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3763 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_UNLINK);
3764 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
3765 vn_finished_write(mp);
3767 NDFREE(&nd, NDF_ONLY_PNBUF);
3769 if (nd.ni_dvp == vp)
3778 * Read a block of directory entries in a filesystem independent format.
3780 #ifndef _SYS_SYSPROTO_H_
3781 struct ogetdirentries_args {
3789 ogetdirentries(struct thread *td, struct ogetdirentries_args *uap)
3794 error = kern_ogetdirentries(td, uap, &loff);
3796 error = copyout(&loff, uap->basep, sizeof(long));
3801 kern_ogetdirentries(struct thread *td, struct ogetdirentries_args *uap,
3806 struct uio auio, kuio;
3807 struct iovec aiov, kiov;
3808 struct dirent *dp, *edp;
3810 int error, eofflag, readcnt;
3814 /* XXX arbitrary sanity limit on `count'. */
3815 if (uap->count > 64 * 1024)
3817 if ((error = getvnode(td->td_proc->p_fd, uap->fd, CAP_READ, &fp)) != 0)
3819 if ((fp->f_flag & FREAD) == 0) {
3824 foffset = foffset_lock(fp, 0);
3826 if (vp->v_type != VDIR) {
3827 foffset_unlock(fp, foffset, 0);
3831 aiov.iov_base = uap->buf;
3832 aiov.iov_len = uap->count;
3833 auio.uio_iov = &aiov;
3834 auio.uio_iovcnt = 1;
3835 auio.uio_rw = UIO_READ;
3836 auio.uio_segflg = UIO_USERSPACE;
3838 auio.uio_resid = uap->count;
3839 vn_lock(vp, LK_SHARED | LK_RETRY);
3840 loff = auio.uio_offset = foffset;
3842 error = mac_vnode_check_readdir(td->td_ucred, vp);
3845 foffset_unlock(fp, foffset, FOF_NOUPDATE);
3850 # if (BYTE_ORDER != LITTLE_ENDIAN)
3851 if (vp->v_mount->mnt_maxsymlinklen <= 0) {
3852 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag,
3854 foffset = auio.uio_offset;
3859 kuio.uio_iov = &kiov;
3860 kuio.uio_segflg = UIO_SYSSPACE;
3861 kiov.iov_len = uap->count;
3862 dirbuf = malloc(uap->count, M_TEMP, M_WAITOK);
3863 kiov.iov_base = dirbuf;
3864 error = VOP_READDIR(vp, &kuio, fp->f_cred, &eofflag,
3866 foffset = kuio.uio_offset;
3868 readcnt = uap->count - kuio.uio_resid;
3869 edp = (struct dirent *)&dirbuf[readcnt];
3870 for (dp = (struct dirent *)dirbuf; dp < edp; ) {
3871 # if (BYTE_ORDER == LITTLE_ENDIAN)
3873 * The expected low byte of
3874 * dp->d_namlen is our dp->d_type.
3875 * The high MBZ byte of dp->d_namlen
3876 * is our dp->d_namlen.
3878 dp->d_type = dp->d_namlen;
3882 * The dp->d_type is the high byte
3883 * of the expected dp->d_namlen,
3884 * so must be zero'ed.
3888 if (dp->d_reclen > 0) {
3889 dp = (struct dirent *)
3890 ((char *)dp + dp->d_reclen);
3897 error = uiomove(dirbuf, readcnt, &auio);
3899 free(dirbuf, M_TEMP);
3903 foffset_unlock(fp, foffset, 0);
3907 if (uap->count == auio.uio_resid &&
3908 (vp->v_vflag & VV_ROOT) &&
3909 (vp->v_mount->mnt_flag & MNT_UNION)) {
3910 struct vnode *tvp = vp;
3911 vp = vp->v_mount->mnt_vnodecovered;
3920 foffset_unlock(fp, foffset, 0);
3922 td->td_retval[0] = uap->count - auio.uio_resid;
3927 #endif /* COMPAT_43 */
3930 * Read a block of directory entries in a filesystem independent format.
3932 #ifndef _SYS_SYSPROTO_H_
3933 struct getdirentries_args {
3941 sys_getdirentries(td, uap)
3943 register struct getdirentries_args /* {
3953 error = kern_getdirentries(td, uap->fd, uap->buf, uap->count, &base,
3954 NULL, UIO_USERSPACE);
3957 if (uap->basep != NULL)
3958 error = copyout(&base, uap->basep, sizeof(long));
3963 kern_getdirentries(struct thread *td, int fd, char *buf, u_int count,
3964 long *basep, ssize_t *residp, enum uio_seg bufseg)
3975 if (count > IOSIZE_MAX)
3977 auio.uio_resid = count;
3978 if ((error = getvnode(td->td_proc->p_fd, fd, CAP_READ, &fp)) != 0)
3980 if ((fp->f_flag & FREAD) == 0) {
3985 foffset = foffset_lock(fp, 0);
3987 if (vp->v_type != VDIR) {
3991 aiov.iov_base = buf;
3992 aiov.iov_len = count;
3993 auio.uio_iov = &aiov;
3994 auio.uio_iovcnt = 1;
3995 auio.uio_rw = UIO_READ;
3996 auio.uio_segflg = bufseg;
3998 vn_lock(vp, LK_SHARED | LK_RETRY);
3999 AUDIT_ARG_VNODE1(vp);
4000 loff = auio.uio_offset = foffset;
4002 error = mac_vnode_check_readdir(td->td_ucred, vp);
4005 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL,
4007 foffset = auio.uio_offset;
4012 if (count == auio.uio_resid &&
4013 (vp->v_vflag & VV_ROOT) &&
4014 (vp->v_mount->mnt_flag & MNT_UNION)) {
4015 struct vnode *tvp = vp;
4016 vp = vp->v_mount->mnt_vnodecovered;
4027 *residp = auio.uio_resid;
4028 td->td_retval[0] = count - auio.uio_resid;
4030 foffset_unlock(fp, foffset, 0);
4035 #ifndef _SYS_SYSPROTO_H_
4036 struct getdents_args {
4043 sys_getdents(td, uap)
4045 register struct getdents_args /* {
4051 struct getdirentries_args ap;
4054 ap.count = uap->count;
4056 return (sys_getdirentries(td, &ap));
4060 * Set the mode mask for creation of filesystem nodes.
4062 #ifndef _SYS_SYSPROTO_H_
4070 struct umask_args /* {
4074 register struct filedesc *fdp;
4076 FILEDESC_XLOCK(td->td_proc->p_fd);
4077 fdp = td->td_proc->p_fd;
4078 td->td_retval[0] = fdp->fd_cmask;
4079 fdp->fd_cmask = uap->newmask & ALLPERMS;
4080 FILEDESC_XUNLOCK(td->td_proc->p_fd);
4085 * Void all references to file by ripping underlying filesystem away from
4088 #ifndef _SYS_SYSPROTO_H_
4089 struct revoke_args {
4096 register struct revoke_args /* {
4103 struct nameidata nd;
4105 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
4107 if ((error = namei(&nd)) != 0)
4110 NDFREE(&nd, NDF_ONLY_PNBUF);
4111 if (vp->v_type != VCHR || vp->v_rdev == NULL) {
4116 error = mac_vnode_check_revoke(td->td_ucred, vp);
4120 error = VOP_GETATTR(vp, &vattr, td->td_ucred);
4123 if (td->td_ucred->cr_uid != vattr.va_uid) {
4124 error = priv_check(td, PRIV_VFS_ADMIN);
4129 VOP_REVOKE(vp, REVOKEALL);
4136 * Convert a user file descriptor to a kernel file entry and check that, if it
4137 * is a capability, the correct rights are present. A reference on the file
4138 * entry is held upon returning.
4141 getvnode(struct filedesc *fdp, int fd, cap_rights_t rights, struct file **fpp)
4146 error = fget_unlocked(fdp, fd, rights, 0, &fp, NULL);
4151 * The file could be not of the vnode type, or it may be not
4152 * yet fully initialized, in which case the f_vnode pointer
4153 * may be set, but f_ops is still badfileops. E.g.,
4154 * devfs_open() transiently create such situation to
4155 * facilitate csw d_fdopen().
4157 * Dupfdopen() handling in kern_openat() installs the
4158 * half-baked file into the process descriptor table, allowing
4159 * other thread to dereference it. Guard against the race by
4162 if (fp->f_vnode == NULL || fp->f_ops == &badfileops) {
4163 fdrop(fp, curthread);
4172 * Get an (NFS) file handle.
4174 #ifndef _SYS_SYSPROTO_H_
4175 struct lgetfh_args {
4183 register struct lgetfh_args *uap;
4185 struct nameidata nd;
4187 register struct vnode *vp;
4190 error = priv_check(td, PRIV_VFS_GETFH);
4193 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
4198 NDFREE(&nd, NDF_ONLY_PNBUF);
4200 bzero(&fh, sizeof(fh));
4201 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
4202 error = VOP_VPTOFH(vp, &fh.fh_fid);
4206 error = copyout(&fh, uap->fhp, sizeof (fh));
4210 #ifndef _SYS_SYSPROTO_H_
4219 register struct getfh_args *uap;
4221 struct nameidata nd;
4223 register struct vnode *vp;
4226 error = priv_check(td, PRIV_VFS_GETFH);
4229 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
4234 NDFREE(&nd, NDF_ONLY_PNBUF);
4236 bzero(&fh, sizeof(fh));
4237 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
4238 error = VOP_VPTOFH(vp, &fh.fh_fid);
4242 error = copyout(&fh, uap->fhp, sizeof (fh));
4247 * syscall for the rpc.lockd to use to translate a NFS file handle into an
4250 * warning: do not remove the priv_check() call or this becomes one giant
4253 #ifndef _SYS_SYSPROTO_H_
4254 struct fhopen_args {
4255 const struct fhandle *u_fhp;
4262 struct fhopen_args /* {
4263 const struct fhandle *u_fhp;
4274 error = priv_check(td, PRIV_VFS_FHOPEN);
4278 fmode = FFLAGS(uap->flags);
4279 /* why not allow a non-read/write open for our lockd? */
4280 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
4282 error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
4285 /* find the mount point */
4286 mp = vfs_busyfs(&fhp.fh_fsid);
4289 /* now give me my vnode, it gets returned to me locked */
4290 error = VFS_FHTOVP(mp, &fhp.fh_fid, LK_EXCLUSIVE, &vp);
4295 error = falloc_noinstall(td, &fp);
4301 * An extra reference on `fp' has been held for us by
4302 * falloc_noinstall().
4308 error = vn_open_vnode(vp, fmode, td->td_ucred, td, fp);
4310 KASSERT(fp->f_ops == &badfileops,
4311 ("VOP_OPEN in fhopen() set f_ops"));
4312 KASSERT(td->td_dupfd < 0,
4313 ("fhopen() encountered fdopen()"));
4323 finit(fp, (fmode & FMASK) | (fp->f_flag & FHASLOCK), DTYPE_VNODE, vp,
4326 if (fmode & O_TRUNC) {
4327 error = fo_truncate(fp, 0, td->td_ucred, td);
4332 error = finstall(td, fp, &indx, fmode, NULL);
4335 td->td_retval[0] = indx;
4340 * Stat an (NFS) file handle.
4342 #ifndef _SYS_SYSPROTO_H_
4343 struct fhstat_args {
4344 struct fhandle *u_fhp;
4351 register struct fhstat_args /* {
4352 struct fhandle *u_fhp;
4360 error = copyin(uap->u_fhp, &fh, sizeof(fh));
4363 error = kern_fhstat(td, fh, &sb);
4366 error = copyout(&sb, uap->sb, sizeof(sb));
4371 kern_fhstat(struct thread *td, struct fhandle fh, struct stat *sb)
4377 error = priv_check(td, PRIV_VFS_FHSTAT);
4380 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4382 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_EXCLUSIVE, &vp);
4386 error = vn_stat(vp, sb, td->td_ucred, NOCRED, td);
4392 * Implement fstatfs() for (NFS) file handles.
4394 #ifndef _SYS_SYSPROTO_H_
4395 struct fhstatfs_args {
4396 struct fhandle *u_fhp;
4401 sys_fhstatfs(td, uap)
4403 struct fhstatfs_args /* {
4404 struct fhandle *u_fhp;
4412 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4415 error = kern_fhstatfs(td, fh, &sf);
4418 return (copyout(&sf, uap->buf, sizeof(sf)));
4422 kern_fhstatfs(struct thread *td, fhandle_t fh, struct statfs *buf)
4429 error = priv_check(td, PRIV_VFS_FHSTATFS);
4432 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4434 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_EXCLUSIVE, &vp);
4440 error = prison_canseemount(td->td_ucred, mp);
4444 error = mac_mount_check_stat(td->td_ucred, mp);
4449 * Set these in case the underlying filesystem fails to do so.
4452 sp->f_version = STATFS_VERSION;
4453 sp->f_namemax = NAME_MAX;
4454 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
4455 error = VFS_STATFS(mp, sp);
4464 kern_posix_fallocate(struct thread *td, int fd, off_t offset, off_t len)
4469 off_t olen, ooffset;
4473 error = fget(td, fd, CAP_WRITE, &fp);
4477 switch (fp->f_type) {
4488 if ((fp->f_flag & FWRITE) == 0) {
4493 if (vp->v_type != VREG) {
4497 if (offset < 0 || len <= 0) {
4501 /* Check for wrap. */
4502 if (offset > OFF_MAX - len) {
4507 /* Allocating blocks may take a long time, so iterate. */
4514 error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
4517 error = vn_lock(vp, LK_EXCLUSIVE);
4519 vn_finished_write(mp);
4523 error = mac_vnode_check_write(td->td_ucred, fp->f_cred, vp);
4526 error = VOP_ALLOCATE(vp, &offset, &len);
4528 vn_finished_write(mp);
4530 if (olen + ooffset != offset + len) {
4531 panic("offset + len changed from %jx/%jx to %jx/%jx",
4532 ooffset, olen, offset, len);
4534 if (error != 0 || len == 0)
4536 KASSERT(olen > len, ("Iteration did not make progress?"));
4546 sys_posix_fallocate(struct thread *td, struct posix_fallocate_args *uap)
4549 return (kern_posix_fallocate(td, uap->fd, uap->offset, uap->len));
4553 * Unlike madvise(2), we do not make a best effort to remember every
4554 * possible caching hint. Instead, we remember the last setting with
4555 * the exception that we will allow POSIX_FADV_NORMAL to adjust the
4556 * region of any current setting.
4559 kern_posix_fadvise(struct thread *td, int fd, off_t offset, off_t len,
4562 struct fadvise_info *fa, *new;
4568 if (offset < 0 || len < 0 || offset > OFF_MAX - len)
4571 case POSIX_FADV_SEQUENTIAL:
4572 case POSIX_FADV_RANDOM:
4573 case POSIX_FADV_NOREUSE:
4574 new = malloc(sizeof(*fa), M_FADVISE, M_WAITOK);
4576 case POSIX_FADV_NORMAL:
4577 case POSIX_FADV_WILLNEED:
4578 case POSIX_FADV_DONTNEED:
4584 /* XXX: CAP_POSIX_FADVISE? */
4585 error = fget(td, fd, CAP_NONE, &fp);
4589 switch (fp->f_type) {
4601 if (vp->v_type != VREG) {
4608 end = offset + len - 1;
4610 case POSIX_FADV_SEQUENTIAL:
4611 case POSIX_FADV_RANDOM:
4612 case POSIX_FADV_NOREUSE:
4614 * Try to merge any existing non-standard region with
4615 * this new region if possible, otherwise create a new
4616 * non-standard region for this request.
4618 mtx_pool_lock(mtxpool_sleep, fp);
4620 if (fa != NULL && fa->fa_advice == advice &&
4621 ((fa->fa_start <= end && fa->fa_end >= offset) ||
4622 (end != OFF_MAX && fa->fa_start == end + 1) ||
4623 (fa->fa_end != OFF_MAX && fa->fa_end + 1 == offset))) {
4624 if (offset < fa->fa_start)
4625 fa->fa_start = offset;
4626 if (end > fa->fa_end)
4629 new->fa_advice = advice;
4630 new->fa_start = offset;
4632 new->fa_prevstart = 0;
4633 new->fa_prevend = 0;
4637 mtx_pool_unlock(mtxpool_sleep, fp);
4639 case POSIX_FADV_NORMAL:
4641 * If a the "normal" region overlaps with an existing
4642 * non-standard region, trim or remove the
4643 * non-standard region.
4645 mtx_pool_lock(mtxpool_sleep, fp);
4648 if (offset <= fa->fa_start && end >= fa->fa_end) {
4650 fp->f_advice = NULL;
4651 } else if (offset <= fa->fa_start &&
4652 end >= fa->fa_start)
4653 fa->fa_start = end + 1;
4654 else if (offset <= fa->fa_end && end >= fa->fa_end)
4655 fa->fa_end = offset - 1;
4656 else if (offset >= fa->fa_start && end <= fa->fa_end) {
4658 * If the "normal" region is a middle
4659 * portion of the existing
4660 * non-standard region, just remove
4661 * the whole thing rather than picking
4662 * one side or the other to
4666 fp->f_advice = NULL;
4669 mtx_pool_unlock(mtxpool_sleep, fp);
4671 case POSIX_FADV_WILLNEED:
4672 case POSIX_FADV_DONTNEED:
4673 error = VOP_ADVISE(vp, offset, end, advice);
4679 free(new, M_FADVISE);
4684 sys_posix_fadvise(struct thread *td, struct posix_fadvise_args *uap)
4687 return (kern_posix_fadvise(td, uap->fd, uap->offset, uap->len,