2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
4 * (c) UNIX System Laboratories, Inc.
5 * All or some portions of this file are derived from material licensed
6 * to the University of California by American Telephone and Telegraph
7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 * the permission of UNIX System Laboratories, Inc.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 4. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * @(#)vfs_syscalls.c 8.13 (Berkeley) 4/15/94
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
40 #include "opt_capsicum.h"
41 #include "opt_compat.h"
42 #include "opt_ktrace.h"
44 #include <sys/param.h>
45 #include <sys/systm.h>
48 #include <sys/capsicum.h>
50 #include <sys/sysent.h>
51 #include <sys/malloc.h>
52 #include <sys/mount.h>
53 #include <sys/mutex.h>
54 #include <sys/sysproto.h>
55 #include <sys/namei.h>
56 #include <sys/filedesc.h>
57 #include <sys/kernel.h>
58 #include <sys/fcntl.h>
60 #include <sys/filio.h>
61 #include <sys/limits.h>
62 #include <sys/linker.h>
63 #include <sys/rwlock.h>
67 #include <sys/unistd.h>
68 #include <sys/vnode.h>
71 #include <sys/dirent.h>
73 #include <sys/syscallsubr.h>
74 #include <sys/sysctl.h>
76 #include <sys/ktrace.h>
79 #include <machine/stdarg.h>
81 #include <security/audit/audit.h>
82 #include <security/mac/mac_framework.h>
85 #include <vm/vm_object.h>
86 #include <vm/vm_page.h>
89 #include <ufs/ufs/quota.h>
91 MALLOC_DEFINE(M_FADVISE, "fadvise", "posix_fadvise(2) information");
93 SDT_PROVIDER_DEFINE(vfs);
94 SDT_PROBE_DEFINE2(vfs, , stat, mode, "char *", "int");
95 SDT_PROBE_DEFINE2(vfs, , stat, reg, "char *", "int");
97 static int chroot_refuse_vdir_fds(struct filedesc *fdp);
98 static int getutimes(const struct timeval *, enum uio_seg, struct timespec *);
99 static int kern_chflagsat(struct thread *td, int fd, const char *path,
100 enum uio_seg pathseg, u_long flags, int atflag);
101 static int setfflags(struct thread *td, struct vnode *, u_long);
102 static int setutimes(struct thread *td, struct vnode *,
103 const struct timespec *, int, int);
104 static int vn_access(struct vnode *vp, int user_flags, struct ucred *cred,
108 * The module initialization routine for POSIX asynchronous I/O will
109 * set this to the version of AIO that it implements. (Zero means
110 * that it is not implemented.) This value is used here by pathconf()
111 * and in kern_descrip.c by fpathconf().
113 int async_io_version;
116 * Sync each mounted filesystem.
118 #ifndef _SYS_SYSPROTO_H_
127 struct sync_args *uap;
129 struct mount *mp, *nmp;
132 mtx_lock(&mountlist_mtx);
133 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
134 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK)) {
135 nmp = TAILQ_NEXT(mp, mnt_list);
138 if ((mp->mnt_flag & MNT_RDONLY) == 0 &&
139 vn_start_write(NULL, &mp, V_NOWAIT) == 0) {
140 save = curthread_pflags_set(TDP_SYNCIO);
141 vfs_msync(mp, MNT_NOWAIT);
142 VFS_SYNC(mp, MNT_NOWAIT);
143 curthread_pflags_restore(save);
144 vn_finished_write(mp);
146 mtx_lock(&mountlist_mtx);
147 nmp = TAILQ_NEXT(mp, mnt_list);
150 mtx_unlock(&mountlist_mtx);
155 * Change filesystem quotas.
157 #ifndef _SYS_SYSPROTO_H_
158 struct quotactl_args {
166 sys_quotactl(td, uap)
168 register struct quotactl_args /* {
179 AUDIT_ARG_CMD(uap->cmd);
180 AUDIT_ARG_UID(uap->uid);
181 if (!prison_allow(td->td_ucred, PR_ALLOW_QUOTAS))
183 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
185 if ((error = namei(&nd)) != 0)
187 NDFREE(&nd, NDF_ONLY_PNBUF);
188 mp = nd.ni_vp->v_mount;
191 error = vfs_busy(mp, 0);
195 error = VFS_QUOTACTL(mp, uap->cmd, uap->uid, uap->arg);
198 * Since quota on operation typically needs to open quota
199 * file, the Q_QUOTAON handler needs to unbusy the mount point
200 * before calling into namei. Otherwise, unmount might be
201 * started between two vfs_busy() invocations (first is our,
202 * second is from mount point cross-walk code in lookup()),
205 * Require that Q_QUOTAON handles the vfs_busy() reference on
206 * its own, always returning with ubusied mount point.
208 if ((uap->cmd >> SUBCMDSHIFT) != Q_QUOTAON)
214 * Used by statfs conversion routines to scale the block size up if
215 * necessary so that all of the block counts are <= 'max_size'. Note
216 * that 'max_size' should be a bitmask, i.e. 2^n - 1 for some non-zero
220 statfs_scale_blocks(struct statfs *sf, long max_size)
225 KASSERT(powerof2(max_size + 1), ("%s: invalid max_size", __func__));
228 * Attempt to scale the block counts to give a more accurate
229 * overview to userland of the ratio of free space to used
230 * space. To do this, find the largest block count and compute
231 * a divisor that lets it fit into a signed integer <= max_size.
233 if (sf->f_bavail < 0)
234 count = -sf->f_bavail;
236 count = sf->f_bavail;
237 count = MAX(sf->f_blocks, MAX(sf->f_bfree, count));
238 if (count <= max_size)
241 count >>= flsl(max_size);
248 sf->f_bsize <<= shift;
249 sf->f_blocks >>= shift;
250 sf->f_bfree >>= shift;
251 sf->f_bavail >>= shift;
255 * Get filesystem statistics.
257 #ifndef _SYS_SYSPROTO_H_
266 register struct statfs_args /* {
274 error = kern_statfs(td, uap->path, UIO_USERSPACE, &sf);
276 error = copyout(&sf, uap->buf, sizeof(sf));
281 kern_statfs(struct thread *td, char *path, enum uio_seg pathseg,
285 struct statfs *sp, sb;
289 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
294 mp = nd.ni_vp->v_mount;
296 NDFREE(&nd, NDF_ONLY_PNBUF);
298 error = vfs_busy(mp, 0);
303 error = mac_mount_check_stat(td->td_ucred, mp);
308 * Set these in case the underlying filesystem fails to do so.
311 sp->f_version = STATFS_VERSION;
312 sp->f_namemax = NAME_MAX;
313 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
314 error = VFS_STATFS(mp, sp);
317 if (priv_check(td, PRIV_VFS_GENERATION)) {
318 bcopy(sp, &sb, sizeof(sb));
319 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
320 prison_enforce_statfs(td->td_ucred, mp, &sb);
330 * Get filesystem statistics.
332 #ifndef _SYS_SYSPROTO_H_
333 struct fstatfs_args {
341 register struct fstatfs_args /* {
349 error = kern_fstatfs(td, uap->fd, &sf);
351 error = copyout(&sf, uap->buf, sizeof(sf));
356 kern_fstatfs(struct thread *td, int fd, struct statfs *buf)
360 struct statfs *sp, sb;
366 error = getvnode(td->td_proc->p_fd, fd,
367 cap_rights_init(&rights, CAP_FSTATFS), &fp);
371 vn_lock(vp, LK_SHARED | LK_RETRY);
373 AUDIT_ARG_VNODE1(vp);
384 error = vfs_busy(mp, 0);
389 error = mac_mount_check_stat(td->td_ucred, mp);
394 * Set these in case the underlying filesystem fails to do so.
397 sp->f_version = STATFS_VERSION;
398 sp->f_namemax = NAME_MAX;
399 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
400 error = VFS_STATFS(mp, sp);
403 if (priv_check(td, PRIV_VFS_GENERATION)) {
404 bcopy(sp, &sb, sizeof(sb));
405 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
406 prison_enforce_statfs(td->td_ucred, mp, &sb);
417 * Get statistics on all filesystems.
419 #ifndef _SYS_SYSPROTO_H_
420 struct getfsstat_args {
427 sys_getfsstat(td, uap)
429 register struct getfsstat_args /* {
436 return (kern_getfsstat(td, &uap->buf, uap->bufsize, UIO_USERSPACE,
441 * If (bufsize > 0 && bufseg == UIO_SYSSPACE)
442 * The caller is responsible for freeing memory which will be allocated
446 kern_getfsstat(struct thread *td, struct statfs **buf, size_t bufsize,
447 enum uio_seg bufseg, int flags)
449 struct mount *mp, *nmp;
450 struct statfs *sfsp, *sp, sb;
451 size_t count, maxcount;
454 maxcount = bufsize / sizeof(struct statfs);
457 else if (bufseg == UIO_USERSPACE)
459 else /* if (bufseg == UIO_SYSSPACE) */ {
461 mtx_lock(&mountlist_mtx);
462 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
465 mtx_unlock(&mountlist_mtx);
466 if (maxcount > count)
468 sfsp = *buf = malloc(maxcount * sizeof(struct statfs), M_TEMP,
472 mtx_lock(&mountlist_mtx);
473 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
474 if (prison_canseemount(td->td_ucred, mp) != 0) {
475 nmp = TAILQ_NEXT(mp, mnt_list);
479 if (mac_mount_check_stat(td->td_ucred, mp) != 0) {
480 nmp = TAILQ_NEXT(mp, mnt_list);
484 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK)) {
485 nmp = TAILQ_NEXT(mp, mnt_list);
488 if (sfsp && count < maxcount) {
491 * Set these in case the underlying filesystem
494 sp->f_version = STATFS_VERSION;
495 sp->f_namemax = NAME_MAX;
496 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
498 * If MNT_NOWAIT or MNT_LAZY is specified, do not
499 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
500 * overrides MNT_WAIT.
502 if (((flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
503 (flags & MNT_WAIT)) &&
504 (error = VFS_STATFS(mp, sp))) {
505 mtx_lock(&mountlist_mtx);
506 nmp = TAILQ_NEXT(mp, mnt_list);
510 if (priv_check(td, PRIV_VFS_GENERATION)) {
511 bcopy(sp, &sb, sizeof(sb));
512 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
513 prison_enforce_statfs(td->td_ucred, mp, &sb);
516 if (bufseg == UIO_SYSSPACE)
517 bcopy(sp, sfsp, sizeof(*sp));
518 else /* if (bufseg == UIO_USERSPACE) */ {
519 error = copyout(sp, sfsp, sizeof(*sp));
528 mtx_lock(&mountlist_mtx);
529 nmp = TAILQ_NEXT(mp, mnt_list);
532 mtx_unlock(&mountlist_mtx);
533 if (sfsp && count > maxcount)
534 td->td_retval[0] = maxcount;
536 td->td_retval[0] = count;
540 #ifdef COMPAT_FREEBSD4
542 * Get old format filesystem statistics.
544 static void cvtstatfs(struct statfs *, struct ostatfs *);
546 #ifndef _SYS_SYSPROTO_H_
547 struct freebsd4_statfs_args {
553 freebsd4_statfs(td, uap)
555 struct freebsd4_statfs_args /* {
564 error = kern_statfs(td, uap->path, UIO_USERSPACE, &sf);
567 cvtstatfs(&sf, &osb);
568 return (copyout(&osb, uap->buf, sizeof(osb)));
572 * Get filesystem statistics.
574 #ifndef _SYS_SYSPROTO_H_
575 struct freebsd4_fstatfs_args {
581 freebsd4_fstatfs(td, uap)
583 struct freebsd4_fstatfs_args /* {
592 error = kern_fstatfs(td, uap->fd, &sf);
595 cvtstatfs(&sf, &osb);
596 return (copyout(&osb, uap->buf, sizeof(osb)));
600 * Get statistics on all filesystems.
602 #ifndef _SYS_SYSPROTO_H_
603 struct freebsd4_getfsstat_args {
610 freebsd4_getfsstat(td, uap)
612 register struct freebsd4_getfsstat_args /* {
618 struct statfs *buf, *sp;
623 count = uap->bufsize / sizeof(struct ostatfs);
624 size = count * sizeof(struct statfs);
625 error = kern_getfsstat(td, &buf, size, UIO_SYSSPACE, uap->flags);
627 count = td->td_retval[0];
629 while (count > 0 && error == 0) {
631 error = copyout(&osb, uap->buf, sizeof(osb));
642 * Implement fstatfs() for (NFS) file handles.
644 #ifndef _SYS_SYSPROTO_H_
645 struct freebsd4_fhstatfs_args {
646 struct fhandle *u_fhp;
651 freebsd4_fhstatfs(td, uap)
653 struct freebsd4_fhstatfs_args /* {
654 struct fhandle *u_fhp;
663 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
666 error = kern_fhstatfs(td, fh, &sf);
669 cvtstatfs(&sf, &osb);
670 return (copyout(&osb, uap->buf, sizeof(osb)));
674 * Convert a new format statfs structure to an old format statfs structure.
682 statfs_scale_blocks(nsp, LONG_MAX);
683 bzero(osp, sizeof(*osp));
684 osp->f_bsize = nsp->f_bsize;
685 osp->f_iosize = MIN(nsp->f_iosize, LONG_MAX);
686 osp->f_blocks = nsp->f_blocks;
687 osp->f_bfree = nsp->f_bfree;
688 osp->f_bavail = nsp->f_bavail;
689 osp->f_files = MIN(nsp->f_files, LONG_MAX);
690 osp->f_ffree = MIN(nsp->f_ffree, LONG_MAX);
691 osp->f_owner = nsp->f_owner;
692 osp->f_type = nsp->f_type;
693 osp->f_flags = nsp->f_flags;
694 osp->f_syncwrites = MIN(nsp->f_syncwrites, LONG_MAX);
695 osp->f_asyncwrites = MIN(nsp->f_asyncwrites, LONG_MAX);
696 osp->f_syncreads = MIN(nsp->f_syncreads, LONG_MAX);
697 osp->f_asyncreads = MIN(nsp->f_asyncreads, LONG_MAX);
698 strlcpy(osp->f_fstypename, nsp->f_fstypename,
699 MIN(MFSNAMELEN, OMFSNAMELEN));
700 strlcpy(osp->f_mntonname, nsp->f_mntonname,
701 MIN(MNAMELEN, OMNAMELEN));
702 strlcpy(osp->f_mntfromname, nsp->f_mntfromname,
703 MIN(MNAMELEN, OMNAMELEN));
704 osp->f_fsid = nsp->f_fsid;
706 #endif /* COMPAT_FREEBSD4 */
709 * Change current working directory to a given file descriptor.
711 #ifndef _SYS_SYSPROTO_H_
719 struct fchdir_args /* {
723 register struct filedesc *fdp = td->td_proc->p_fd;
724 struct vnode *vp, *tdp, *vpold;
730 AUDIT_ARG_FD(uap->fd);
731 error = getvnode(fdp, uap->fd, cap_rights_init(&rights, CAP_FCHDIR),
738 vn_lock(vp, LK_SHARED | LK_RETRY);
739 AUDIT_ARG_VNODE1(vp);
740 error = change_dir(vp, td);
741 while (!error && (mp = vp->v_mountedhere) != NULL) {
744 error = VFS_ROOT(mp, LK_SHARED, &tdp);
757 vpold = fdp->fd_cdir;
759 FILEDESC_XUNLOCK(fdp);
765 * Change current working directory (``.'').
767 #ifndef _SYS_SYSPROTO_H_
775 struct chdir_args /* {
780 return (kern_chdir(td, uap->path, UIO_USERSPACE));
784 kern_chdir(struct thread *td, char *path, enum uio_seg pathseg)
786 register struct filedesc *fdp = td->td_proc->p_fd;
791 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
793 if ((error = namei(&nd)) != 0)
795 if ((error = change_dir(nd.ni_vp, td)) != 0) {
797 NDFREE(&nd, NDF_ONLY_PNBUF);
800 VOP_UNLOCK(nd.ni_vp, 0);
801 NDFREE(&nd, NDF_ONLY_PNBUF);
804 fdp->fd_cdir = nd.ni_vp;
805 FILEDESC_XUNLOCK(fdp);
811 * Helper function for raised chroot(2) security function: Refuse if
812 * any filedescriptors are open directories.
815 chroot_refuse_vdir_fds(fdp)
816 struct filedesc *fdp;
822 FILEDESC_LOCK_ASSERT(fdp);
824 for (fd = 0; fd <= fdp->fd_lastfile; fd++) {
825 fp = fget_locked(fdp, fd);
828 if (fp->f_type == DTYPE_VNODE) {
830 if (vp->v_type == VDIR)
838 * This sysctl determines if we will allow a process to chroot(2) if it
839 * has a directory open:
840 * 0: disallowed for all processes.
841 * 1: allowed for processes that were not already chroot(2)'ed.
842 * 2: allowed for all processes.
845 static int chroot_allow_open_directories = 1;
847 SYSCTL_INT(_kern, OID_AUTO, chroot_allow_open_directories, CTLFLAG_RW,
848 &chroot_allow_open_directories, 0,
849 "Allow a process to chroot(2) if it has a directory open");
852 * Change notion of root (``/'') directory.
854 #ifndef _SYS_SYSPROTO_H_
862 struct chroot_args /* {
869 error = priv_check(td, PRIV_VFS_CHROOT);
872 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
873 UIO_USERSPACE, uap->path, td);
877 error = change_dir(nd.ni_vp, td);
881 error = mac_vnode_check_chroot(td->td_ucred, nd.ni_vp);
885 VOP_UNLOCK(nd.ni_vp, 0);
886 error = change_root(nd.ni_vp, td);
888 NDFREE(&nd, NDF_ONLY_PNBUF);
893 NDFREE(&nd, NDF_ONLY_PNBUF);
898 * Common routine for chroot and chdir. Callers must provide a locked vnode
910 ASSERT_VOP_LOCKED(vp, "change_dir(): vp not locked");
911 if (vp->v_type != VDIR)
914 error = mac_vnode_check_chdir(td->td_ucred, vp);
918 return (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);
958 flags_to_rights(int flags, cap_rights_t *rightsp)
961 if (flags & O_EXEC) {
962 cap_rights_set(rightsp, CAP_FEXECVE);
964 switch ((flags & O_ACCMODE)) {
966 cap_rights_set(rightsp, CAP_READ);
969 cap_rights_set(rightsp, CAP_READ);
972 cap_rights_set(rightsp, CAP_WRITE);
973 if (!(flags & (O_APPEND | O_TRUNC)))
974 cap_rights_set(rightsp, CAP_SEEK);
980 cap_rights_set(rightsp, CAP_CREATE);
983 cap_rights_set(rightsp, CAP_FTRUNCATE);
985 if (flags & (O_SYNC | O_FSYNC))
986 cap_rights_set(rightsp, CAP_FSYNC);
988 if (flags & (O_EXLOCK | O_SHLOCK))
989 cap_rights_set(rightsp, CAP_FLOCK);
993 * Check permissions, allocate an open file structure, and call the device
994 * open routine if any.
996 #ifndef _SYS_SYSPROTO_H_
1006 register struct open_args /* {
1013 return (kern_openat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1014 uap->flags, uap->mode));
1017 #ifndef _SYS_SYSPROTO_H_
1018 struct openat_args {
1026 sys_openat(struct thread *td, struct openat_args *uap)
1029 return (kern_openat(td, uap->fd, uap->path, UIO_USERSPACE, uap->flag,
1034 kern_openat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1035 int flags, int mode)
1037 struct proc *p = td->td_proc;
1038 struct filedesc *fdp = p->p_fd;
1041 struct nameidata nd;
1042 cap_rights_t rights;
1043 int cmode, error, indx;
1047 AUDIT_ARG_FFLAGS(flags);
1048 AUDIT_ARG_MODE(mode);
1049 /* XXX: audit dirfd */
1050 cap_rights_init(&rights, CAP_LOOKUP);
1051 flags_to_rights(flags, &rights);
1053 * Only one of the O_EXEC, O_RDONLY, O_WRONLY and O_RDWR flags
1056 if (flags & O_EXEC) {
1057 if (flags & O_ACCMODE)
1059 } else if ((flags & O_ACCMODE) == O_ACCMODE) {
1062 flags = FFLAGS(flags);
1066 * Allocate the file descriptor, but don't install a descriptor yet.
1068 error = falloc_noinstall(td, &fp);
1072 * An extra reference on `fp' has been held for us by
1073 * falloc_noinstall().
1075 /* Set the flags early so the finit in devfs can pick them up. */
1076 fp->f_flag = flags & FMASK;
1077 cmode = ((mode & ~fdp->fd_cmask) & ALLPERMS) & ~S_ISTXT;
1078 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, fd,
1080 td->td_dupfd = -1; /* XXX check for fdopen */
1081 error = vn_open(&nd, &flags, cmode, fp);
1084 * If the vn_open replaced the method vector, something
1085 * wonderous happened deep below and we just pass it up
1086 * pretending we know what we do.
1088 if (error == ENXIO && fp->f_ops != &badfileops)
1092 * Handle special fdopen() case. bleh.
1094 * Don't do this for relative (capability) lookups; we don't
1095 * understand exactly what would happen, and we don't think
1096 * that it ever should.
1098 if (nd.ni_strictrelative == 0 &&
1099 (error == ENODEV || error == ENXIO) &&
1100 td->td_dupfd >= 0) {
1101 error = dupfdopen(td, fdp, td->td_dupfd, flags, error,
1110 NDFREE(&nd, NDF_ONLY_PNBUF);
1114 * Store the vnode, for any f_type. Typically, the vnode use
1115 * count is decremented by direct call to vn_closefile() for
1116 * files that switched type in the cdevsw fdopen() method.
1120 * If the file wasn't claimed by devfs bind it to the normal
1121 * vnode operations here.
1123 if (fp->f_ops == &badfileops) {
1124 KASSERT(vp->v_type != VFIFO, ("Unexpected fifo."));
1126 finit(fp, (flags & FMASK) | (fp->f_flag & FHASLOCK),
1127 DTYPE_VNODE, vp, &vnops);
1131 if (flags & O_TRUNC) {
1132 error = fo_truncate(fp, 0, td->td_ucred, td);
1138 * If we haven't already installed the FD (for dupfdopen), do so now.
1141 struct filecaps *fcaps;
1144 if (nd.ni_strictrelative == 1)
1145 fcaps = &nd.ni_filecaps;
1149 error = finstall(td, fp, &indx, flags, fcaps);
1150 /* On success finstall() consumes fcaps. */
1152 filecaps_free(&nd.ni_filecaps);
1156 filecaps_free(&nd.ni_filecaps);
1160 * Release our private reference, leaving the one associated with
1161 * the descriptor table intact.
1164 td->td_retval[0] = indx;
1167 KASSERT(indx == -1, ("indx=%d, should be -1", indx));
1176 #ifndef _SYS_SYSPROTO_H_
1177 struct ocreat_args {
1185 register struct ocreat_args /* {
1191 return (kern_openat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1192 O_WRONLY | O_CREAT | O_TRUNC, uap->mode));
1194 #endif /* COMPAT_43 */
1197 * Create a special file.
1199 #ifndef _SYS_SYSPROTO_H_
1209 register struct mknod_args /* {
1216 return (kern_mknodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1217 uap->mode, uap->dev));
1220 #ifndef _SYS_SYSPROTO_H_
1221 struct mknodat_args {
1229 sys_mknodat(struct thread *td, struct mknodat_args *uap)
1232 return (kern_mknodat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode,
1237 kern_mknodat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1243 struct nameidata nd;
1244 cap_rights_t rights;
1245 int error, whiteout = 0;
1247 AUDIT_ARG_MODE(mode);
1249 switch (mode & S_IFMT) {
1252 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
1255 error = priv_check(td, PRIV_VFS_MKNOD_BAD);
1258 error = priv_check(td, PRIV_VFS_MKNOD_WHT);
1262 return (kern_mkfifoat(td, fd, path, pathseg, mode));
1272 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1,
1273 pathseg, path, fd, cap_rights_init(&rights, CAP_MKNODAT), td);
1274 if ((error = namei(&nd)) != 0)
1278 NDFREE(&nd, NDF_ONLY_PNBUF);
1279 if (vp == nd.ni_dvp)
1287 vattr.va_mode = (mode & ALLPERMS) &
1288 ~td->td_proc->p_fd->fd_cmask;
1289 vattr.va_rdev = dev;
1292 switch (mode & S_IFMT) {
1293 case S_IFMT: /* used by badsect to flag bad sectors */
1294 vattr.va_type = VBAD;
1297 vattr.va_type = VCHR;
1300 vattr.va_type = VBLK;
1306 panic("kern_mknod: invalid mode");
1309 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1310 NDFREE(&nd, NDF_ONLY_PNBUF);
1312 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1317 if (error == 0 && !whiteout)
1318 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp,
1319 &nd.ni_cnd, &vattr);
1323 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, CREATE);
1325 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp,
1326 &nd.ni_cnd, &vattr);
1331 NDFREE(&nd, NDF_ONLY_PNBUF);
1333 vn_finished_write(mp);
1338 * Create a named pipe.
1340 #ifndef _SYS_SYSPROTO_H_
1341 struct mkfifo_args {
1349 register struct mkfifo_args /* {
1355 return (kern_mkfifoat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1359 #ifndef _SYS_SYSPROTO_H_
1360 struct mkfifoat_args {
1367 sys_mkfifoat(struct thread *td, struct mkfifoat_args *uap)
1370 return (kern_mkfifoat(td, uap->fd, uap->path, UIO_USERSPACE,
1375 kern_mkfifoat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1380 struct nameidata nd;
1381 cap_rights_t rights;
1384 AUDIT_ARG_MODE(mode);
1387 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1,
1388 pathseg, path, fd, cap_rights_init(&rights, CAP_MKFIFOAT), td);
1389 if ((error = namei(&nd)) != 0)
1391 if (nd.ni_vp != NULL) {
1392 NDFREE(&nd, NDF_ONLY_PNBUF);
1393 if (nd.ni_vp == nd.ni_dvp)
1400 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1401 NDFREE(&nd, NDF_ONLY_PNBUF);
1403 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1408 vattr.va_type = VFIFO;
1409 vattr.va_mode = (mode & ALLPERMS) & ~td->td_proc->p_fd->fd_cmask;
1411 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1416 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
1423 vn_finished_write(mp);
1424 NDFREE(&nd, NDF_ONLY_PNBUF);
1429 * Make a hard file link.
1431 #ifndef _SYS_SYSPROTO_H_
1440 register struct link_args /* {
1446 return (kern_linkat(td, AT_FDCWD, AT_FDCWD, uap->path, uap->link,
1447 UIO_USERSPACE, FOLLOW));
1450 #ifndef _SYS_SYSPROTO_H_
1451 struct linkat_args {
1460 sys_linkat(struct thread *td, struct linkat_args *uap)
1465 if (flag & ~AT_SYMLINK_FOLLOW)
1468 return (kern_linkat(td, uap->fd1, uap->fd2, uap->path1, uap->path2,
1469 UIO_USERSPACE, (flag & AT_SYMLINK_FOLLOW) ? FOLLOW : NOFOLLOW));
1472 int hardlink_check_uid = 0;
1473 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
1474 &hardlink_check_uid, 0,
1475 "Unprivileged processes cannot create hard links to files owned by other "
1477 static int hardlink_check_gid = 0;
1478 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
1479 &hardlink_check_gid, 0,
1480 "Unprivileged processes cannot create hard links to files owned by other "
1484 can_hardlink(struct vnode *vp, struct ucred *cred)
1489 if (!hardlink_check_uid && !hardlink_check_gid)
1492 error = VOP_GETATTR(vp, &va, cred);
1496 if (hardlink_check_uid && cred->cr_uid != va.va_uid) {
1497 error = priv_check_cred(cred, PRIV_VFS_LINK, 0);
1502 if (hardlink_check_gid && !groupmember(va.va_gid, cred)) {
1503 error = priv_check_cred(cred, PRIV_VFS_LINK, 0);
1512 kern_linkat(struct thread *td, int fd1, int fd2, char *path1, char *path2,
1513 enum uio_seg segflg, int follow)
1517 struct nameidata nd;
1518 cap_rights_t rights;
1523 NDINIT_AT(&nd, LOOKUP, follow | AUDITVNODE1, segflg, path1, fd1, td);
1525 if ((error = namei(&nd)) != 0)
1527 NDFREE(&nd, NDF_ONLY_PNBUF);
1529 if (vp->v_type == VDIR) {
1531 return (EPERM); /* POSIX */
1533 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE2,
1534 segflg, path2, fd2, cap_rights_init(&rights, CAP_LINKAT), td);
1535 if ((error = namei(&nd)) == 0) {
1536 if (nd.ni_vp != NULL) {
1537 NDFREE(&nd, NDF_ONLY_PNBUF);
1538 if (nd.ni_dvp == nd.ni_vp)
1545 } else if (nd.ni_dvp->v_mount != vp->v_mount) {
1547 * Cross-device link. No need to recheck
1548 * vp->v_type, since it cannot change, except
1551 NDFREE(&nd, NDF_ONLY_PNBUF);
1555 } else if ((error = vn_lock(vp, LK_EXCLUSIVE)) == 0) {
1556 error = can_hardlink(vp, td->td_ucred);
1559 error = mac_vnode_check_link(td->td_ucred,
1560 nd.ni_dvp, vp, &nd.ni_cnd);
1565 NDFREE(&nd, NDF_ONLY_PNBUF);
1568 error = vn_start_write(vp, &mp, V_NOWAIT);
1572 NDFREE(&nd, NDF_ONLY_PNBUF);
1573 error = vn_start_write(NULL, &mp,
1579 error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
1582 vn_finished_write(mp);
1583 NDFREE(&nd, NDF_ONLY_PNBUF);
1586 NDFREE(&nd, NDF_ONLY_PNBUF);
1596 * Make a symbolic link.
1598 #ifndef _SYS_SYSPROTO_H_
1599 struct symlink_args {
1605 sys_symlink(td, uap)
1607 register struct symlink_args /* {
1613 return (kern_symlinkat(td, uap->path, AT_FDCWD, uap->link,
1617 #ifndef _SYS_SYSPROTO_H_
1618 struct symlinkat_args {
1625 sys_symlinkat(struct thread *td, struct symlinkat_args *uap)
1628 return (kern_symlinkat(td, uap->path1, uap->fd, uap->path2,
1633 kern_symlinkat(struct thread *td, char *path1, int fd, char *path2,
1634 enum uio_seg segflg)
1639 struct nameidata nd;
1641 cap_rights_t rights;
1643 if (segflg == UIO_SYSSPACE) {
1646 syspath = uma_zalloc(namei_zone, M_WAITOK);
1647 if ((error = copyinstr(path1, syspath, MAXPATHLEN, NULL)) != 0)
1650 AUDIT_ARG_TEXT(syspath);
1653 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1,
1654 segflg, path2, fd, cap_rights_init(&rights, CAP_SYMLINKAT), td);
1655 if ((error = namei(&nd)) != 0)
1658 NDFREE(&nd, NDF_ONLY_PNBUF);
1659 if (nd.ni_vp == nd.ni_dvp)
1667 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1668 NDFREE(&nd, NDF_ONLY_PNBUF);
1670 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1675 vattr.va_mode = ACCESSPERMS &~ td->td_proc->p_fd->fd_cmask;
1677 vattr.va_type = VLNK;
1678 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1683 error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr, syspath);
1689 NDFREE(&nd, NDF_ONLY_PNBUF);
1691 vn_finished_write(mp);
1693 if (segflg != UIO_SYSSPACE)
1694 uma_zfree(namei_zone, syspath);
1699 * Delete a whiteout from the filesystem.
1702 sys_undelete(td, uap)
1704 register struct undelete_args /* {
1709 struct nameidata nd;
1714 NDINIT(&nd, DELETE, LOCKPARENT | DOWHITEOUT | AUDITVNODE1,
1715 UIO_USERSPACE, uap->path, td);
1720 if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & ISWHITEOUT)) {
1721 NDFREE(&nd, NDF_ONLY_PNBUF);
1722 if (nd.ni_vp == nd.ni_dvp)
1730 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1731 NDFREE(&nd, NDF_ONLY_PNBUF);
1733 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1737 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, DELETE);
1738 NDFREE(&nd, NDF_ONLY_PNBUF);
1740 vn_finished_write(mp);
1745 * Delete a name from the filesystem.
1747 #ifndef _SYS_SYSPROTO_H_
1748 struct unlink_args {
1755 struct unlink_args /* {
1760 return (kern_unlinkat(td, AT_FDCWD, uap->path, UIO_USERSPACE, 0));
1763 #ifndef _SYS_SYSPROTO_H_
1764 struct unlinkat_args {
1771 sys_unlinkat(struct thread *td, struct unlinkat_args *uap)
1773 int flag = uap->flag;
1775 char *path = uap->path;
1777 if (flag & ~AT_REMOVEDIR)
1780 if (flag & AT_REMOVEDIR)
1781 return (kern_rmdirat(td, fd, path, UIO_USERSPACE));
1783 return (kern_unlinkat(td, fd, path, UIO_USERSPACE, 0));
1787 kern_unlinkat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1792 struct nameidata nd;
1794 cap_rights_t rights;
1799 NDINIT_ATRIGHTS(&nd, DELETE, LOCKPARENT | LOCKLEAF | AUDITVNODE1,
1800 pathseg, path, fd, cap_rights_init(&rights, CAP_UNLINKAT), td);
1801 if ((error = namei(&nd)) != 0)
1802 return (error == EINVAL ? EPERM : error);
1804 if (vp->v_type == VDIR && oldinum == 0) {
1805 error = EPERM; /* POSIX */
1806 } else if (oldinum != 0 &&
1807 ((error = vn_stat(vp, &sb, td->td_ucred, NOCRED, td)) == 0) &&
1808 sb.st_ino != oldinum) {
1809 error = EIDRM; /* Identifier removed */
1812 * The root of a mounted filesystem cannot be deleted.
1814 * XXX: can this only be a VDIR case?
1816 if (vp->v_vflag & VV_ROOT)
1820 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1821 NDFREE(&nd, NDF_ONLY_PNBUF);
1823 if (vp == nd.ni_dvp)
1827 if ((error = vn_start_write(NULL, &mp,
1828 V_XSLEEP | PCATCH)) != 0)
1833 error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp,
1838 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_UNLINK);
1839 error = VOP_REMOVE(nd.ni_dvp, vp, &nd.ni_cnd);
1843 vn_finished_write(mp);
1845 NDFREE(&nd, NDF_ONLY_PNBUF);
1847 if (vp == nd.ni_dvp)
1855 * Reposition read/write file offset.
1857 #ifndef _SYS_SYSPROTO_H_
1868 register struct lseek_args /* {
1876 cap_rights_t rights;
1879 AUDIT_ARG_FD(uap->fd);
1880 error = fget(td, uap->fd, cap_rights_init(&rights, CAP_SEEK), &fp);
1883 error = (fp->f_ops->fo_flags & DFLAG_SEEKABLE) != 0 ?
1884 fo_seek(fp, uap->offset, uap->whence, td) : ESPIPE;
1889 #if defined(COMPAT_43)
1891 * Reposition read/write file offset.
1893 #ifndef _SYS_SYSPROTO_H_
1894 struct olseek_args {
1903 register struct olseek_args /* {
1909 struct lseek_args /* {
1917 nuap.offset = uap->offset;
1918 nuap.whence = uap->whence;
1919 return (sys_lseek(td, &nuap));
1921 #endif /* COMPAT_43 */
1923 /* Version with the 'pad' argument */
1925 freebsd6_lseek(td, uap)
1927 register struct freebsd6_lseek_args *uap;
1929 struct lseek_args ouap;
1932 ouap.offset = uap->offset;
1933 ouap.whence = uap->whence;
1934 return (sys_lseek(td, &ouap));
1938 * Check access permissions using passed credentials.
1941 vn_access(vp, user_flags, cred, td)
1950 /* Flags == 0 means only check for existence. */
1951 if (user_flags == 0)
1955 if (user_flags & R_OK)
1957 if (user_flags & W_OK)
1959 if (user_flags & X_OK)
1962 error = mac_vnode_check_access(cred, vp, accmode);
1966 if ((accmode & VWRITE) == 0 || (error = vn_writechk(vp)) == 0)
1967 error = VOP_ACCESS(vp, accmode, cred, td);
1972 * Check access permissions using "real" credentials.
1974 #ifndef _SYS_SYSPROTO_H_
1975 struct access_args {
1983 register struct access_args /* {
1989 return (kern_accessat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1993 #ifndef _SYS_SYSPROTO_H_
1994 struct faccessat_args {
2002 sys_faccessat(struct thread *td, struct faccessat_args *uap)
2005 return (kern_accessat(td, uap->fd, uap->path, UIO_USERSPACE, uap->flag,
2010 kern_accessat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2011 int flag, int amode)
2013 struct ucred *cred, *usecred;
2015 struct nameidata nd;
2016 cap_rights_t rights;
2019 if (flag & ~AT_EACCESS)
2021 if (amode != F_OK && (amode & ~(R_OK | W_OK | X_OK)) != 0)
2025 * Create and modify a temporary credential instead of one that
2026 * is potentially shared (if we need one).
2028 cred = td->td_ucred;
2029 if ((flag & AT_EACCESS) == 0 &&
2030 ((cred->cr_uid != cred->cr_ruid ||
2031 cred->cr_rgid != cred->cr_groups[0]))) {
2032 usecred = crdup(cred);
2033 usecred->cr_uid = cred->cr_ruid;
2034 usecred->cr_groups[0] = cred->cr_rgid;
2035 td->td_ucred = usecred;
2038 AUDIT_ARG_VALUE(amode);
2039 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF |
2040 AUDITVNODE1, pathseg, path, fd, cap_rights_init(&rights, CAP_FSTAT),
2042 if ((error = namei(&nd)) != 0)
2046 error = vn_access(vp, amode, usecred, td);
2047 NDFREE(&nd, NDF_ONLY_PNBUF);
2050 if (usecred != cred) {
2051 td->td_ucred = cred;
2058 * Check access permissions using "effective" credentials.
2060 #ifndef _SYS_SYSPROTO_H_
2061 struct eaccess_args {
2067 sys_eaccess(td, uap)
2069 register struct eaccess_args /* {
2075 return (kern_accessat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2076 AT_EACCESS, uap->amode));
2079 #if defined(COMPAT_43)
2081 * Get file status; this version follows links.
2083 #ifndef _SYS_SYSPROTO_H_
2092 register struct ostat_args /* {
2101 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
2106 return (copyout(&osb, uap->ub, sizeof (osb)));
2110 * Get file status; this version does not follow links.
2112 #ifndef _SYS_SYSPROTO_H_
2113 struct olstat_args {
2121 register struct olstat_args /* {
2130 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2131 UIO_USERSPACE, &sb, NULL);
2135 return (copyout(&osb, uap->ub, sizeof (osb)));
2139 * Convert from an old to a new stat structure.
2147 ost->st_dev = st->st_dev;
2148 ost->st_ino = st->st_ino;
2149 ost->st_mode = st->st_mode;
2150 ost->st_nlink = st->st_nlink;
2151 ost->st_uid = st->st_uid;
2152 ost->st_gid = st->st_gid;
2153 ost->st_rdev = st->st_rdev;
2154 if (st->st_size < (quad_t)1 << 32)
2155 ost->st_size = st->st_size;
2158 ost->st_atim = st->st_atim;
2159 ost->st_mtim = st->st_mtim;
2160 ost->st_ctim = st->st_ctim;
2161 ost->st_blksize = st->st_blksize;
2162 ost->st_blocks = st->st_blocks;
2163 ost->st_flags = st->st_flags;
2164 ost->st_gen = st->st_gen;
2166 #endif /* COMPAT_43 */
2169 * Get file status; this version follows links.
2171 #ifndef _SYS_SYSPROTO_H_
2180 register struct stat_args /* {
2188 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
2191 error = copyout(&sb, uap->ub, sizeof (sb));
2195 #ifndef _SYS_SYSPROTO_H_
2196 struct fstatat_args {
2204 sys_fstatat(struct thread *td, struct fstatat_args *uap)
2209 error = kern_statat(td, uap->flag, uap->fd, uap->path,
2210 UIO_USERSPACE, &sb, NULL);
2212 error = copyout(&sb, uap->buf, sizeof (sb));
2217 kern_statat(struct thread *td, int flag, int fd, char *path,
2218 enum uio_seg pathseg, struct stat *sbp,
2219 void (*hook)(struct vnode *vp, struct stat *sbp))
2221 struct nameidata nd;
2223 cap_rights_t rights;
2226 if (flag & ~AT_SYMLINK_NOFOLLOW)
2229 NDINIT_ATRIGHTS(&nd, LOOKUP, ((flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW :
2230 FOLLOW) | LOCKSHARED | LOCKLEAF | AUDITVNODE1, pathseg, path, fd,
2231 cap_rights_init(&rights, CAP_FSTAT), td);
2233 if ((error = namei(&nd)) != 0)
2235 error = vn_stat(nd.ni_vp, &sb, td->td_ucred, NOCRED, td);
2237 SDT_PROBE(vfs, , stat, mode, path, sb.st_mode, 0, 0, 0);
2238 if (S_ISREG(sb.st_mode))
2239 SDT_PROBE(vfs, , stat, reg, path, pathseg, 0, 0, 0);
2240 if (__predict_false(hook != NULL))
2241 hook(nd.ni_vp, &sb);
2243 NDFREE(&nd, NDF_ONLY_PNBUF);
2249 if (KTRPOINT(td, KTR_STRUCT))
2256 * Get file status; this version does not follow links.
2258 #ifndef _SYS_SYSPROTO_H_
2267 register struct lstat_args /* {
2275 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2276 UIO_USERSPACE, &sb, NULL);
2278 error = copyout(&sb, uap->ub, sizeof (sb));
2283 * Implementation of the NetBSD [l]stat() functions.
2291 bzero(nsb, sizeof *nsb);
2292 nsb->st_dev = sb->st_dev;
2293 nsb->st_ino = sb->st_ino;
2294 nsb->st_mode = sb->st_mode;
2295 nsb->st_nlink = sb->st_nlink;
2296 nsb->st_uid = sb->st_uid;
2297 nsb->st_gid = sb->st_gid;
2298 nsb->st_rdev = sb->st_rdev;
2299 nsb->st_atim = sb->st_atim;
2300 nsb->st_mtim = sb->st_mtim;
2301 nsb->st_ctim = sb->st_ctim;
2302 nsb->st_size = sb->st_size;
2303 nsb->st_blocks = sb->st_blocks;
2304 nsb->st_blksize = sb->st_blksize;
2305 nsb->st_flags = sb->st_flags;
2306 nsb->st_gen = sb->st_gen;
2307 nsb->st_birthtim = sb->st_birthtim;
2310 #ifndef _SYS_SYSPROTO_H_
2319 register struct nstat_args /* {
2328 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
2332 cvtnstat(&sb, &nsb);
2333 return (copyout(&nsb, uap->ub, sizeof (nsb)));
2337 * NetBSD lstat. Get file status; this version does not follow links.
2339 #ifndef _SYS_SYSPROTO_H_
2348 register struct nlstat_args /* {
2357 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2358 UIO_USERSPACE, &sb, NULL);
2361 cvtnstat(&sb, &nsb);
2362 return (copyout(&nsb, uap->ub, sizeof (nsb)));
2366 * Get configurable pathname variables.
2368 #ifndef _SYS_SYSPROTO_H_
2369 struct pathconf_args {
2375 sys_pathconf(td, uap)
2377 register struct pathconf_args /* {
2383 return (kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name, FOLLOW));
2386 #ifndef _SYS_SYSPROTO_H_
2387 struct lpathconf_args {
2393 sys_lpathconf(td, uap)
2395 register struct lpathconf_args /* {
2401 return (kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name,
2406 kern_pathconf(struct thread *td, char *path, enum uio_seg pathseg, int name,
2409 struct nameidata nd;
2412 NDINIT(&nd, LOOKUP, LOCKSHARED | LOCKLEAF | AUDITVNODE1 | flags,
2414 if ((error = namei(&nd)) != 0)
2416 NDFREE(&nd, NDF_ONLY_PNBUF);
2418 /* If asynchronous I/O is available, it works for all files. */
2419 if (name == _PC_ASYNC_IO)
2420 td->td_retval[0] = async_io_version;
2422 error = VOP_PATHCONF(nd.ni_vp, name, td->td_retval);
2428 * Return target name of a symbolic link.
2430 #ifndef _SYS_SYSPROTO_H_
2431 struct readlink_args {
2438 sys_readlink(td, uap)
2440 register struct readlink_args /* {
2447 return (kern_readlinkat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2448 uap->buf, UIO_USERSPACE, uap->count));
2450 #ifndef _SYS_SYSPROTO_H_
2451 struct readlinkat_args {
2459 sys_readlinkat(struct thread *td, struct readlinkat_args *uap)
2462 return (kern_readlinkat(td, uap->fd, uap->path, UIO_USERSPACE,
2463 uap->buf, UIO_USERSPACE, uap->bufsize));
2467 kern_readlinkat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2468 char *buf, enum uio_seg bufseg, size_t count)
2473 struct nameidata nd;
2476 if (count > IOSIZE_MAX)
2479 NDINIT_AT(&nd, LOOKUP, NOFOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
2480 pathseg, path, fd, td);
2482 if ((error = namei(&nd)) != 0)
2484 NDFREE(&nd, NDF_ONLY_PNBUF);
2487 error = mac_vnode_check_readlink(td->td_ucred, vp);
2493 if (vp->v_type != VLNK)
2496 aiov.iov_base = buf;
2497 aiov.iov_len = count;
2498 auio.uio_iov = &aiov;
2499 auio.uio_iovcnt = 1;
2500 auio.uio_offset = 0;
2501 auio.uio_rw = UIO_READ;
2502 auio.uio_segflg = bufseg;
2504 auio.uio_resid = count;
2505 error = VOP_READLINK(vp, &auio, td->td_ucred);
2506 td->td_retval[0] = count - auio.uio_resid;
2513 * Common implementation code for chflags() and fchflags().
2516 setfflags(td, vp, flags)
2525 /* We can't support the value matching VNOVAL. */
2526 if (flags == VNOVAL)
2527 return (EOPNOTSUPP);
2530 * Prevent non-root users from setting flags on devices. When
2531 * a device is reused, users can retain ownership of the device
2532 * if they are allowed to set flags and programs assume that
2533 * chown can't fail when done as root.
2535 if (vp->v_type == VCHR || vp->v_type == VBLK) {
2536 error = priv_check(td, PRIV_VFS_CHFLAGS_DEV);
2541 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2544 vattr.va_flags = flags;
2545 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2547 error = mac_vnode_check_setflags(td->td_ucred, vp, vattr.va_flags);
2550 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
2552 vn_finished_write(mp);
2557 * Change flags of a file given a path name.
2559 #ifndef _SYS_SYSPROTO_H_
2560 struct chflags_args {
2566 sys_chflags(td, uap)
2568 register struct chflags_args /* {
2574 return (kern_chflagsat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2578 #ifndef _SYS_SYSPROTO_H_
2579 struct chflagsat_args {
2587 sys_chflagsat(struct thread *td, struct chflagsat_args *uap)
2590 const char *path = uap->path;
2591 u_long flags = uap->flags;
2592 int atflag = uap->atflag;
2594 if (atflag & ~AT_SYMLINK_NOFOLLOW)
2597 return (kern_chflagsat(td, fd, path, UIO_USERSPACE, flags, atflag));
2601 * Same as chflags() but doesn't follow symlinks.
2604 sys_lchflags(td, uap)
2606 register struct lchflags_args /* {
2612 return (kern_chflagsat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2613 uap->flags, AT_SYMLINK_NOFOLLOW));
2617 kern_chflagsat(struct thread *td, int fd, const char *path,
2618 enum uio_seg pathseg, u_long flags, int atflag)
2620 struct nameidata nd;
2621 cap_rights_t rights;
2624 AUDIT_ARG_FFLAGS(flags);
2625 follow = (atflag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
2626 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, pathseg, path, fd,
2627 cap_rights_init(&rights, CAP_FCHFLAGS), td);
2628 if ((error = namei(&nd)) != 0)
2630 NDFREE(&nd, NDF_ONLY_PNBUF);
2631 error = setfflags(td, nd.ni_vp, flags);
2637 * Change flags of a file given a file descriptor.
2639 #ifndef _SYS_SYSPROTO_H_
2640 struct fchflags_args {
2646 sys_fchflags(td, uap)
2648 register struct fchflags_args /* {
2654 cap_rights_t rights;
2657 AUDIT_ARG_FD(uap->fd);
2658 AUDIT_ARG_FFLAGS(uap->flags);
2659 error = getvnode(td->td_proc->p_fd, uap->fd,
2660 cap_rights_init(&rights, CAP_FCHFLAGS), &fp);
2664 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
2665 AUDIT_ARG_VNODE1(fp->f_vnode);
2666 VOP_UNLOCK(fp->f_vnode, 0);
2668 error = setfflags(td, fp->f_vnode, uap->flags);
2674 * Common implementation code for chmod(), lchmod() and fchmod().
2677 setfmode(td, cred, vp, mode)
2687 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2689 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2691 vattr.va_mode = mode & ALLPERMS;
2693 error = mac_vnode_check_setmode(cred, vp, vattr.va_mode);
2696 error = VOP_SETATTR(vp, &vattr, cred);
2698 vn_finished_write(mp);
2703 * Change mode of a file given path name.
2705 #ifndef _SYS_SYSPROTO_H_
2714 register struct chmod_args /* {
2720 return (kern_fchmodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2724 #ifndef _SYS_SYSPROTO_H_
2725 struct fchmodat_args {
2733 sys_fchmodat(struct thread *td, struct fchmodat_args *uap)
2735 int flag = uap->flag;
2737 char *path = uap->path;
2738 mode_t mode = uap->mode;
2740 if (flag & ~AT_SYMLINK_NOFOLLOW)
2743 return (kern_fchmodat(td, fd, path, UIO_USERSPACE, mode, flag));
2747 * Change mode of a file given path name (don't follow links.)
2749 #ifndef _SYS_SYSPROTO_H_
2750 struct lchmod_args {
2758 register struct lchmod_args /* {
2764 return (kern_fchmodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2765 uap->mode, AT_SYMLINK_NOFOLLOW));
2769 kern_fchmodat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2770 mode_t mode, int flag)
2772 struct nameidata nd;
2773 cap_rights_t rights;
2776 AUDIT_ARG_MODE(mode);
2777 follow = (flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
2778 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, pathseg, path, fd,
2779 cap_rights_init(&rights, CAP_FCHMOD), td);
2780 if ((error = namei(&nd)) != 0)
2782 NDFREE(&nd, NDF_ONLY_PNBUF);
2783 error = setfmode(td, td->td_ucred, nd.ni_vp, mode);
2789 * Change mode of a file given a file descriptor.
2791 #ifndef _SYS_SYSPROTO_H_
2792 struct fchmod_args {
2798 sys_fchmod(struct thread *td, struct fchmod_args *uap)
2801 cap_rights_t rights;
2804 AUDIT_ARG_FD(uap->fd);
2805 AUDIT_ARG_MODE(uap->mode);
2807 error = fget(td, uap->fd, cap_rights_init(&rights, CAP_FCHMOD), &fp);
2810 error = fo_chmod(fp, uap->mode, td->td_ucred, td);
2816 * Common implementation for chown(), lchown(), and fchown()
2819 setfown(td, cred, vp, uid, gid)
2830 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2832 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2837 error = mac_vnode_check_setowner(cred, vp, vattr.va_uid,
2841 error = VOP_SETATTR(vp, &vattr, cred);
2843 vn_finished_write(mp);
2848 * Set ownership given a path name.
2850 #ifndef _SYS_SYSPROTO_H_
2860 register struct chown_args /* {
2867 return (kern_fchownat(td, AT_FDCWD, uap->path, UIO_USERSPACE, uap->uid,
2871 #ifndef _SYS_SYSPROTO_H_
2872 struct fchownat_args {
2881 sys_fchownat(struct thread *td, struct fchownat_args *uap)
2886 if (flag & ~AT_SYMLINK_NOFOLLOW)
2889 return (kern_fchownat(td, uap->fd, uap->path, UIO_USERSPACE, uap->uid,
2890 uap->gid, uap->flag));
2894 kern_fchownat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2895 int uid, int gid, int flag)
2897 struct nameidata nd;
2898 cap_rights_t rights;
2901 AUDIT_ARG_OWNER(uid, gid);
2902 follow = (flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
2903 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, pathseg, path, fd,
2904 cap_rights_init(&rights, CAP_FCHOWN), td);
2906 if ((error = namei(&nd)) != 0)
2908 NDFREE(&nd, NDF_ONLY_PNBUF);
2909 error = setfown(td, td->td_ucred, nd.ni_vp, uid, gid);
2915 * Set ownership given a path name, do not cross symlinks.
2917 #ifndef _SYS_SYSPROTO_H_
2918 struct lchown_args {
2927 register struct lchown_args /* {
2934 return (kern_fchownat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2935 uap->uid, uap->gid, AT_SYMLINK_NOFOLLOW));
2939 * Set ownership given a file descriptor.
2941 #ifndef _SYS_SYSPROTO_H_
2942 struct fchown_args {
2951 register struct fchown_args /* {
2958 cap_rights_t rights;
2961 AUDIT_ARG_FD(uap->fd);
2962 AUDIT_ARG_OWNER(uap->uid, uap->gid);
2963 error = fget(td, uap->fd, cap_rights_init(&rights, CAP_FCHOWN), &fp);
2966 error = fo_chown(fp, uap->uid, uap->gid, td->td_ucred, td);
2972 * Common implementation code for utimes(), lutimes(), and futimes().
2975 getutimes(usrtvp, tvpseg, tsp)
2976 const struct timeval *usrtvp;
2977 enum uio_seg tvpseg;
2978 struct timespec *tsp;
2980 struct timeval tv[2];
2981 const struct timeval *tvp;
2984 if (usrtvp == NULL) {
2985 vfs_timestamp(&tsp[0]);
2988 if (tvpseg == UIO_SYSSPACE) {
2991 if ((error = copyin(usrtvp, tv, sizeof(tv))) != 0)
2996 if (tvp[0].tv_usec < 0 || tvp[0].tv_usec >= 1000000 ||
2997 tvp[1].tv_usec < 0 || tvp[1].tv_usec >= 1000000)
2999 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
3000 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
3006 * Common implementation code for utimes(), lutimes(), and futimes().
3009 setutimes(td, vp, ts, numtimes, nullflag)
3012 const struct timespec *ts;
3018 int error, setbirthtime;
3020 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
3022 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3024 if (numtimes < 3 && !VOP_GETATTR(vp, &vattr, td->td_ucred) &&
3025 timespeccmp(&ts[1], &vattr.va_birthtime, < ))
3028 vattr.va_atime = ts[0];
3029 vattr.va_mtime = ts[1];
3031 vattr.va_birthtime = ts[1];
3033 vattr.va_birthtime = ts[2];
3035 vattr.va_vaflags |= VA_UTIMES_NULL;
3037 error = mac_vnode_check_setutimes(td->td_ucred, vp, vattr.va_atime,
3041 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3043 vn_finished_write(mp);
3048 * Set the access and modification times of a file.
3050 #ifndef _SYS_SYSPROTO_H_
3051 struct utimes_args {
3053 struct timeval *tptr;
3059 register struct utimes_args /* {
3061 struct timeval *tptr;
3065 return (kern_utimesat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
3066 uap->tptr, UIO_USERSPACE));
3069 #ifndef _SYS_SYSPROTO_H_
3070 struct futimesat_args {
3073 const struct timeval * times;
3077 sys_futimesat(struct thread *td, struct futimesat_args *uap)
3080 return (kern_utimesat(td, uap->fd, uap->path, UIO_USERSPACE,
3081 uap->times, UIO_USERSPACE));
3085 kern_utimesat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
3086 struct timeval *tptr, enum uio_seg tptrseg)
3088 struct nameidata nd;
3089 struct timespec ts[2];
3090 cap_rights_t rights;
3093 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3095 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, fd,
3096 cap_rights_init(&rights, CAP_FUTIMES), td);
3098 if ((error = namei(&nd)) != 0)
3100 NDFREE(&nd, NDF_ONLY_PNBUF);
3101 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3107 * Set the access and modification times of a file.
3109 #ifndef _SYS_SYSPROTO_H_
3110 struct lutimes_args {
3112 struct timeval *tptr;
3116 sys_lutimes(td, uap)
3118 register struct lutimes_args /* {
3120 struct timeval *tptr;
3124 return (kern_lutimes(td, uap->path, UIO_USERSPACE, uap->tptr,
3129 kern_lutimes(struct thread *td, char *path, enum uio_seg pathseg,
3130 struct timeval *tptr, enum uio_seg tptrseg)
3132 struct timespec ts[2];
3133 struct nameidata nd;
3136 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3138 NDINIT(&nd, LOOKUP, NOFOLLOW | AUDITVNODE1, pathseg, path, td);
3139 if ((error = namei(&nd)) != 0)
3141 NDFREE(&nd, NDF_ONLY_PNBUF);
3142 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3148 * Set the access and modification times of a file.
3150 #ifndef _SYS_SYSPROTO_H_
3151 struct futimes_args {
3153 struct timeval *tptr;
3157 sys_futimes(td, uap)
3159 register struct futimes_args /* {
3161 struct timeval *tptr;
3165 return (kern_futimes(td, uap->fd, uap->tptr, UIO_USERSPACE));
3169 kern_futimes(struct thread *td, int fd, struct timeval *tptr,
3170 enum uio_seg tptrseg)
3172 struct timespec ts[2];
3174 cap_rights_t rights;
3178 error = getutimes(tptr, tptrseg, ts);
3181 error = getvnode(td->td_proc->p_fd, fd,
3182 cap_rights_init(&rights, CAP_FUTIMES), &fp);
3186 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
3187 AUDIT_ARG_VNODE1(fp->f_vnode);
3188 VOP_UNLOCK(fp->f_vnode, 0);
3190 error = setutimes(td, fp->f_vnode, ts, 2, tptr == NULL);
3196 * Truncate a file given its path name.
3198 #ifndef _SYS_SYSPROTO_H_
3199 struct truncate_args {
3206 sys_truncate(td, uap)
3208 register struct truncate_args /* {
3215 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3219 kern_truncate(struct thread *td, char *path, enum uio_seg pathseg, off_t length)
3225 struct nameidata nd;
3230 NDINIT(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, td);
3231 if ((error = namei(&nd)) != 0)
3234 rl_cookie = vn_rangelock_wlock(vp, 0, OFF_MAX);
3235 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0) {
3236 vn_rangelock_unlock(vp, rl_cookie);
3240 NDFREE(&nd, NDF_ONLY_PNBUF);
3241 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3242 if (vp->v_type == VDIR)
3245 else if ((error = mac_vnode_check_write(td->td_ucred, NOCRED, vp))) {
3248 else if ((error = vn_writechk(vp)) == 0 &&
3249 (error = VOP_ACCESS(vp, VWRITE, td->td_ucred, td)) == 0) {
3251 vattr.va_size = length;
3252 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3255 vn_finished_write(mp);
3256 vn_rangelock_unlock(vp, rl_cookie);
3261 #if defined(COMPAT_43)
3263 * Truncate a file given its path name.
3265 #ifndef _SYS_SYSPROTO_H_
3266 struct otruncate_args {
3274 register struct otruncate_args /* {
3279 struct truncate_args /* {
3285 nuap.path = uap->path;
3286 nuap.length = uap->length;
3287 return (sys_truncate(td, &nuap));
3289 #endif /* COMPAT_43 */
3291 /* Versions with the pad argument */
3293 freebsd6_truncate(struct thread *td, struct freebsd6_truncate_args *uap)
3295 struct truncate_args ouap;
3297 ouap.path = uap->path;
3298 ouap.length = uap->length;
3299 return (sys_truncate(td, &ouap));
3303 freebsd6_ftruncate(struct thread *td, struct freebsd6_ftruncate_args *uap)
3305 struct ftruncate_args ouap;
3308 ouap.length = uap->length;
3309 return (sys_ftruncate(td, &ouap));
3313 * Sync an open file.
3315 #ifndef _SYS_SYSPROTO_H_
3323 struct fsync_args /* {
3330 cap_rights_t rights;
3331 int error, lock_flags;
3333 AUDIT_ARG_FD(uap->fd);
3334 error = getvnode(td->td_proc->p_fd, uap->fd,
3335 cap_rights_init(&rights, CAP_FSYNC), &fp);
3339 error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
3342 if (MNT_SHARED_WRITES(mp) ||
3343 ((mp == NULL) && MNT_SHARED_WRITES(vp->v_mount))) {
3344 lock_flags = LK_SHARED;
3346 lock_flags = LK_EXCLUSIVE;
3348 vn_lock(vp, lock_flags | LK_RETRY);
3349 AUDIT_ARG_VNODE1(vp);
3350 if (vp->v_object != NULL) {
3351 VM_OBJECT_WLOCK(vp->v_object);
3352 vm_object_page_clean(vp->v_object, 0, 0, 0);
3353 VM_OBJECT_WUNLOCK(vp->v_object);
3355 error = VOP_FSYNC(vp, MNT_WAIT, td);
3358 vn_finished_write(mp);
3365 * Rename files. Source and destination must either both be directories, or
3366 * both not be directories. If target is a directory, it must be empty.
3368 #ifndef _SYS_SYSPROTO_H_
3369 struct rename_args {
3377 register struct rename_args /* {
3383 return (kern_renameat(td, AT_FDCWD, uap->from, AT_FDCWD,
3384 uap->to, UIO_USERSPACE));
3387 #ifndef _SYS_SYSPROTO_H_
3388 struct renameat_args {
3396 sys_renameat(struct thread *td, struct renameat_args *uap)
3399 return (kern_renameat(td, uap->oldfd, uap->old, uap->newfd, uap->new,
3404 kern_renameat(struct thread *td, int oldfd, char *old, int newfd, char *new,
3405 enum uio_seg pathseg)
3407 struct mount *mp = NULL;
3408 struct vnode *tvp, *fvp, *tdvp;
3409 struct nameidata fromnd, tond;
3410 cap_rights_t rights;
3416 NDINIT_ATRIGHTS(&fromnd, DELETE, LOCKPARENT | LOCKLEAF | SAVESTART |
3417 AUDITVNODE1, pathseg, old, oldfd,
3418 cap_rights_init(&rights, CAP_RENAMEAT), td);
3420 NDINIT_ATRIGHTS(&fromnd, DELETE, WANTPARENT | SAVESTART | AUDITVNODE1,
3421 pathseg, old, oldfd, cap_rights_init(&rights, CAP_RENAMEAT), td);
3424 if ((error = namei(&fromnd)) != 0)
3427 error = mac_vnode_check_rename_from(td->td_ucred, fromnd.ni_dvp,
3428 fromnd.ni_vp, &fromnd.ni_cnd);
3429 VOP_UNLOCK(fromnd.ni_dvp, 0);
3430 if (fromnd.ni_dvp != fromnd.ni_vp)
3431 VOP_UNLOCK(fromnd.ni_vp, 0);
3434 NDINIT_ATRIGHTS(&tond, RENAME, LOCKPARENT | LOCKLEAF | NOCACHE |
3435 SAVESTART | AUDITVNODE2, pathseg, new, newfd,
3436 cap_rights_init(&rights, CAP_LINKAT), td);
3437 if (fromnd.ni_vp->v_type == VDIR)
3438 tond.ni_cnd.cn_flags |= WILLBEDIR;
3439 if ((error = namei(&tond)) != 0) {
3440 /* Translate error code for rename("dir1", "dir2/."). */
3441 if (error == EISDIR && fvp->v_type == VDIR)
3443 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3444 vrele(fromnd.ni_dvp);
3450 error = vn_start_write(fvp, &mp, V_NOWAIT);
3452 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3453 NDFREE(&tond, NDF_ONLY_PNBUF);
3460 vrele(fromnd.ni_dvp);
3462 vrele(tond.ni_startdir);
3463 if (fromnd.ni_startdir != NULL)
3464 vrele(fromnd.ni_startdir);
3465 error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH);
3471 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
3474 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
3479 if (newfd != AT_FDCWD) {
3481 * If the target already exists we require CAP_UNLINKAT
3484 error = cap_check(&tond.ni_filecaps.fc_rights,
3485 cap_rights_init(&rights, CAP_UNLINKAT));
3496 * If the source is the same as the destination (that is, if they
3497 * are links to the same vnode), then there is nothing to do.
3503 error = mac_vnode_check_rename_to(td->td_ucred, tdvp,
3504 tond.ni_vp, fromnd.ni_dvp == tdvp, &tond.ni_cnd);
3508 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
3509 tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
3510 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3511 NDFREE(&tond, NDF_ONLY_PNBUF);
3513 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3514 NDFREE(&tond, NDF_ONLY_PNBUF);
3521 vrele(fromnd.ni_dvp);
3524 vrele(tond.ni_startdir);
3525 vn_finished_write(mp);
3527 if (fromnd.ni_startdir)
3528 vrele(fromnd.ni_startdir);
3535 * Make a directory file.
3537 #ifndef _SYS_SYSPROTO_H_
3546 register struct mkdir_args /* {
3552 return (kern_mkdirat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
3556 #ifndef _SYS_SYSPROTO_H_
3557 struct mkdirat_args {
3564 sys_mkdirat(struct thread *td, struct mkdirat_args *uap)
3567 return (kern_mkdirat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode));
3571 kern_mkdirat(struct thread *td, int fd, char *path, enum uio_seg segflg,
3577 struct nameidata nd;
3578 cap_rights_t rights;
3581 AUDIT_ARG_MODE(mode);
3584 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1,
3585 segflg, path, fd, cap_rights_init(&rights, CAP_MKDIRAT), td);
3586 nd.ni_cnd.cn_flags |= WILLBEDIR;
3587 if ((error = namei(&nd)) != 0)
3591 NDFREE(&nd, NDF_ONLY_PNBUF);
3593 * XXX namei called with LOCKPARENT but not LOCKLEAF has
3594 * the strange behaviour of leaving the vnode unlocked
3595 * if the target is the same vnode as the parent.
3597 if (vp == nd.ni_dvp)
3604 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3605 NDFREE(&nd, NDF_ONLY_PNBUF);
3607 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3612 vattr.va_type = VDIR;
3613 vattr.va_mode = (mode & ACCESSPERMS) &~ td->td_proc->p_fd->fd_cmask;
3615 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
3620 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
3624 NDFREE(&nd, NDF_ONLY_PNBUF);
3628 vn_finished_write(mp);
3633 * Remove a directory file.
3635 #ifndef _SYS_SYSPROTO_H_
3643 struct rmdir_args /* {
3648 return (kern_rmdirat(td, AT_FDCWD, uap->path, UIO_USERSPACE));
3652 kern_rmdirat(struct thread *td, int fd, char *path, enum uio_seg pathseg)
3656 struct nameidata nd;
3657 cap_rights_t rights;
3662 NDINIT_ATRIGHTS(&nd, DELETE, LOCKPARENT | LOCKLEAF | AUDITVNODE1,
3663 pathseg, path, fd, cap_rights_init(&rights, CAP_UNLINKAT), td);
3664 if ((error = namei(&nd)) != 0)
3667 if (vp->v_type != VDIR) {
3672 * No rmdir "." please.
3674 if (nd.ni_dvp == vp) {
3679 * The root of a mounted filesystem cannot be deleted.
3681 if (vp->v_vflag & VV_ROOT) {
3686 error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp,
3691 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3692 NDFREE(&nd, NDF_ONLY_PNBUF);
3694 if (nd.ni_dvp == vp)
3698 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3702 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_UNLINK);
3703 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
3704 vn_finished_write(mp);
3706 NDFREE(&nd, NDF_ONLY_PNBUF);
3708 if (nd.ni_dvp == vp)
3717 * Read a block of directory entries in a filesystem independent format.
3719 #ifndef _SYS_SYSPROTO_H_
3720 struct ogetdirentries_args {
3728 ogetdirentries(struct thread *td, struct ogetdirentries_args *uap)
3733 error = kern_ogetdirentries(td, uap, &loff);
3735 error = copyout(&loff, uap->basep, sizeof(long));
3740 kern_ogetdirentries(struct thread *td, struct ogetdirentries_args *uap,
3745 struct uio auio, kuio;
3746 struct iovec aiov, kiov;
3747 struct dirent *dp, *edp;
3748 cap_rights_t rights;
3750 int error, eofflag, readcnt;
3754 /* XXX arbitrary sanity limit on `count'. */
3755 if (uap->count > 64 * 1024)
3757 error = getvnode(td->td_proc->p_fd, uap->fd,
3758 cap_rights_init(&rights, CAP_READ), &fp);
3761 if ((fp->f_flag & FREAD) == 0) {
3766 foffset = foffset_lock(fp, 0);
3768 if (vp->v_type != VDIR) {
3769 foffset_unlock(fp, foffset, 0);
3773 aiov.iov_base = uap->buf;
3774 aiov.iov_len = uap->count;
3775 auio.uio_iov = &aiov;
3776 auio.uio_iovcnt = 1;
3777 auio.uio_rw = UIO_READ;
3778 auio.uio_segflg = UIO_USERSPACE;
3780 auio.uio_resid = uap->count;
3781 vn_lock(vp, LK_SHARED | LK_RETRY);
3782 loff = auio.uio_offset = foffset;
3784 error = mac_vnode_check_readdir(td->td_ucred, vp);
3787 foffset_unlock(fp, foffset, FOF_NOUPDATE);
3792 # if (BYTE_ORDER != LITTLE_ENDIAN)
3793 if (vp->v_mount->mnt_maxsymlinklen <= 0) {
3794 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag,
3796 foffset = auio.uio_offset;
3801 kuio.uio_iov = &kiov;
3802 kuio.uio_segflg = UIO_SYSSPACE;
3803 kiov.iov_len = uap->count;
3804 dirbuf = malloc(uap->count, M_TEMP, M_WAITOK);
3805 kiov.iov_base = dirbuf;
3806 error = VOP_READDIR(vp, &kuio, fp->f_cred, &eofflag,
3808 foffset = kuio.uio_offset;
3810 readcnt = uap->count - kuio.uio_resid;
3811 edp = (struct dirent *)&dirbuf[readcnt];
3812 for (dp = (struct dirent *)dirbuf; dp < edp; ) {
3813 # if (BYTE_ORDER == LITTLE_ENDIAN)
3815 * The expected low byte of
3816 * dp->d_namlen is our dp->d_type.
3817 * The high MBZ byte of dp->d_namlen
3818 * is our dp->d_namlen.
3820 dp->d_type = dp->d_namlen;
3824 * The dp->d_type is the high byte
3825 * of the expected dp->d_namlen,
3826 * so must be zero'ed.
3830 if (dp->d_reclen > 0) {
3831 dp = (struct dirent *)
3832 ((char *)dp + dp->d_reclen);
3839 error = uiomove(dirbuf, readcnt, &auio);
3841 free(dirbuf, M_TEMP);
3845 foffset_unlock(fp, foffset, 0);
3849 if (uap->count == auio.uio_resid &&
3850 (vp->v_vflag & VV_ROOT) &&
3851 (vp->v_mount->mnt_flag & MNT_UNION)) {
3852 struct vnode *tvp = vp;
3853 vp = vp->v_mount->mnt_vnodecovered;
3862 foffset_unlock(fp, foffset, 0);
3864 td->td_retval[0] = uap->count - auio.uio_resid;
3869 #endif /* COMPAT_43 */
3872 * Read a block of directory entries in a filesystem independent format.
3874 #ifndef _SYS_SYSPROTO_H_
3875 struct getdirentries_args {
3883 sys_getdirentries(td, uap)
3885 register struct getdirentries_args /* {
3895 error = kern_getdirentries(td, uap->fd, uap->buf, uap->count, &base,
3896 NULL, UIO_USERSPACE);
3899 if (uap->basep != NULL)
3900 error = copyout(&base, uap->basep, sizeof(long));
3905 kern_getdirentries(struct thread *td, int fd, char *buf, u_int count,
3906 long *basep, ssize_t *residp, enum uio_seg bufseg)
3912 cap_rights_t rights;
3918 if (count > IOSIZE_MAX)
3920 auio.uio_resid = count;
3921 error = getvnode(td->td_proc->p_fd, fd,
3922 cap_rights_init(&rights, CAP_READ), &fp);
3925 if ((fp->f_flag & FREAD) == 0) {
3930 foffset = foffset_lock(fp, 0);
3932 if (vp->v_type != VDIR) {
3936 aiov.iov_base = buf;
3937 aiov.iov_len = count;
3938 auio.uio_iov = &aiov;
3939 auio.uio_iovcnt = 1;
3940 auio.uio_rw = UIO_READ;
3941 auio.uio_segflg = bufseg;
3943 vn_lock(vp, LK_SHARED | LK_RETRY);
3944 AUDIT_ARG_VNODE1(vp);
3945 loff = auio.uio_offset = foffset;
3947 error = mac_vnode_check_readdir(td->td_ucred, vp);
3950 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL,
3952 foffset = auio.uio_offset;
3957 if (count == auio.uio_resid &&
3958 (vp->v_vflag & VV_ROOT) &&
3959 (vp->v_mount->mnt_flag & MNT_UNION)) {
3960 struct vnode *tvp = vp;
3962 vp = vp->v_mount->mnt_vnodecovered;
3973 *residp = auio.uio_resid;
3974 td->td_retval[0] = count - auio.uio_resid;
3976 foffset_unlock(fp, foffset, 0);
3981 #ifndef _SYS_SYSPROTO_H_
3982 struct getdents_args {
3989 sys_getdents(td, uap)
3991 register struct getdents_args /* {
3997 struct getdirentries_args ap;
4001 ap.count = uap->count;
4003 return (sys_getdirentries(td, &ap));
4007 * Set the mode mask for creation of filesystem nodes.
4009 #ifndef _SYS_SYSPROTO_H_
4017 struct umask_args /* {
4021 register struct filedesc *fdp;
4023 FILEDESC_XLOCK(td->td_proc->p_fd);
4024 fdp = td->td_proc->p_fd;
4025 td->td_retval[0] = fdp->fd_cmask;
4026 fdp->fd_cmask = uap->newmask & ALLPERMS;
4027 FILEDESC_XUNLOCK(td->td_proc->p_fd);
4032 * Void all references to file by ripping underlying filesystem away from
4035 #ifndef _SYS_SYSPROTO_H_
4036 struct revoke_args {
4043 register struct revoke_args /* {
4049 struct nameidata nd;
4052 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
4054 if ((error = namei(&nd)) != 0)
4057 NDFREE(&nd, NDF_ONLY_PNBUF);
4058 if (vp->v_type != VCHR || vp->v_rdev == NULL) {
4063 error = mac_vnode_check_revoke(td->td_ucred, vp);
4067 error = VOP_GETATTR(vp, &vattr, td->td_ucred);
4070 if (td->td_ucred->cr_uid != vattr.va_uid) {
4071 error = priv_check(td, PRIV_VFS_ADMIN);
4076 VOP_REVOKE(vp, REVOKEALL);
4083 * Convert a user file descriptor to a kernel file entry and check that, if it
4084 * is a capability, the correct rights are present. A reference on the file
4085 * entry is held upon returning.
4088 getvnode(struct filedesc *fdp, int fd, cap_rights_t *rightsp, struct file **fpp)
4093 error = fget_unlocked(fdp, fd, rightsp, 0, &fp, NULL);
4098 * The file could be not of the vnode type, or it may be not
4099 * yet fully initialized, in which case the f_vnode pointer
4100 * may be set, but f_ops is still badfileops. E.g.,
4101 * devfs_open() transiently create such situation to
4102 * facilitate csw d_fdopen().
4104 * Dupfdopen() handling in kern_openat() installs the
4105 * half-baked file into the process descriptor table, allowing
4106 * other thread to dereference it. Guard against the race by
4109 if (fp->f_vnode == NULL || fp->f_ops == &badfileops) {
4110 fdrop(fp, curthread);
4119 * Get an (NFS) file handle.
4121 #ifndef _SYS_SYSPROTO_H_
4122 struct lgetfh_args {
4130 register struct lgetfh_args *uap;
4132 struct nameidata nd;
4134 register struct vnode *vp;
4137 error = priv_check(td, PRIV_VFS_GETFH);
4140 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
4145 NDFREE(&nd, NDF_ONLY_PNBUF);
4147 bzero(&fh, sizeof(fh));
4148 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
4149 error = VOP_VPTOFH(vp, &fh.fh_fid);
4152 error = copyout(&fh, uap->fhp, sizeof (fh));
4156 #ifndef _SYS_SYSPROTO_H_
4165 register struct getfh_args *uap;
4167 struct nameidata nd;
4169 register struct vnode *vp;
4172 error = priv_check(td, PRIV_VFS_GETFH);
4175 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
4180 NDFREE(&nd, NDF_ONLY_PNBUF);
4182 bzero(&fh, sizeof(fh));
4183 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
4184 error = VOP_VPTOFH(vp, &fh.fh_fid);
4187 error = copyout(&fh, uap->fhp, sizeof (fh));
4192 * syscall for the rpc.lockd to use to translate a NFS file handle into an
4195 * warning: do not remove the priv_check() call or this becomes one giant
4198 #ifndef _SYS_SYSPROTO_H_
4199 struct fhopen_args {
4200 const struct fhandle *u_fhp;
4207 struct fhopen_args /* {
4208 const struct fhandle *u_fhp;
4219 error = priv_check(td, PRIV_VFS_FHOPEN);
4223 fmode = FFLAGS(uap->flags);
4224 /* why not allow a non-read/write open for our lockd? */
4225 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
4227 error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
4230 /* find the mount point */
4231 mp = vfs_busyfs(&fhp.fh_fsid);
4234 /* now give me my vnode, it gets returned to me locked */
4235 error = VFS_FHTOVP(mp, &fhp.fh_fid, LK_EXCLUSIVE, &vp);
4240 error = falloc_noinstall(td, &fp);
4246 * An extra reference on `fp' has been held for us by
4247 * falloc_noinstall().
4253 error = vn_open_vnode(vp, fmode, td->td_ucred, td, fp);
4255 KASSERT(fp->f_ops == &badfileops,
4256 ("VOP_OPEN in fhopen() set f_ops"));
4257 KASSERT(td->td_dupfd < 0,
4258 ("fhopen() encountered fdopen()"));
4268 finit(fp, (fmode & FMASK) | (fp->f_flag & FHASLOCK), DTYPE_VNODE, vp,
4271 if ((fmode & O_TRUNC) != 0) {
4272 error = fo_truncate(fp, 0, td->td_ucred, td);
4277 error = finstall(td, fp, &indx, fmode, NULL);
4280 td->td_retval[0] = indx;
4285 * Stat an (NFS) file handle.
4287 #ifndef _SYS_SYSPROTO_H_
4288 struct fhstat_args {
4289 struct fhandle *u_fhp;
4296 register struct fhstat_args /* {
4297 struct fhandle *u_fhp;
4305 error = copyin(uap->u_fhp, &fh, sizeof(fh));
4308 error = kern_fhstat(td, fh, &sb);
4310 error = copyout(&sb, uap->sb, sizeof(sb));
4315 kern_fhstat(struct thread *td, struct fhandle fh, struct stat *sb)
4321 error = priv_check(td, PRIV_VFS_FHSTAT);
4324 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4326 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_EXCLUSIVE, &vp);
4330 error = vn_stat(vp, sb, td->td_ucred, NOCRED, td);
4336 * Implement fstatfs() for (NFS) file handles.
4338 #ifndef _SYS_SYSPROTO_H_
4339 struct fhstatfs_args {
4340 struct fhandle *u_fhp;
4345 sys_fhstatfs(td, uap)
4347 struct fhstatfs_args /* {
4348 struct fhandle *u_fhp;
4356 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4359 error = kern_fhstatfs(td, fh, &sf);
4362 return (copyout(&sf, uap->buf, sizeof(sf)));
4366 kern_fhstatfs(struct thread *td, fhandle_t fh, struct statfs *buf)
4373 error = priv_check(td, PRIV_VFS_FHSTATFS);
4376 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4378 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_EXCLUSIVE, &vp);
4384 error = prison_canseemount(td->td_ucred, mp);
4388 error = mac_mount_check_stat(td->td_ucred, mp);
4393 * Set these in case the underlying filesystem fails to do so.
4396 sp->f_version = STATFS_VERSION;
4397 sp->f_namemax = NAME_MAX;
4398 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
4399 error = VFS_STATFS(mp, sp);
4408 kern_posix_fallocate(struct thread *td, int fd, off_t offset, off_t len)
4413 cap_rights_t rights;
4414 off_t olen, ooffset;
4417 if (offset < 0 || len <= 0)
4419 /* Check for wrap. */
4420 if (offset > OFF_MAX - len)
4422 error = fget(td, fd, cap_rights_init(&rights, CAP_WRITE), &fp);
4425 if ((fp->f_ops->fo_flags & DFLAG_SEEKABLE) == 0) {
4429 if ((fp->f_flag & FWRITE) == 0) {
4433 if (fp->f_type != DTYPE_VNODE) {
4438 if (vp->v_type != VREG) {
4443 /* Allocating blocks may take a long time, so iterate. */
4450 error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
4453 error = vn_lock(vp, LK_EXCLUSIVE);
4455 vn_finished_write(mp);
4459 error = mac_vnode_check_write(td->td_ucred, fp->f_cred, vp);
4462 error = VOP_ALLOCATE(vp, &offset, &len);
4464 vn_finished_write(mp);
4466 if (olen + ooffset != offset + len) {
4467 panic("offset + len changed from %jx/%jx to %jx/%jx",
4468 ooffset, olen, offset, len);
4470 if (error != 0 || len == 0)
4472 KASSERT(olen > len, ("Iteration did not make progress?"));
4481 sys_posix_fallocate(struct thread *td, struct posix_fallocate_args *uap)
4484 td->td_retval[0] = kern_posix_fallocate(td, uap->fd, uap->offset,
4490 * Unlike madvise(2), we do not make a best effort to remember every
4491 * possible caching hint. Instead, we remember the last setting with
4492 * the exception that we will allow POSIX_FADV_NORMAL to adjust the
4493 * region of any current setting.
4496 kern_posix_fadvise(struct thread *td, int fd, off_t offset, off_t len,
4499 struct fadvise_info *fa, *new;
4502 cap_rights_t rights;
4506 if (offset < 0 || len < 0 || offset > OFF_MAX - len)
4509 case POSIX_FADV_SEQUENTIAL:
4510 case POSIX_FADV_RANDOM:
4511 case POSIX_FADV_NOREUSE:
4512 new = malloc(sizeof(*fa), M_FADVISE, M_WAITOK);
4514 case POSIX_FADV_NORMAL:
4515 case POSIX_FADV_WILLNEED:
4516 case POSIX_FADV_DONTNEED:
4522 /* XXX: CAP_POSIX_FADVISE? */
4523 error = fget(td, fd, cap_rights_init(&rights), &fp);
4526 if ((fp->f_ops->fo_flags & DFLAG_SEEKABLE) == 0) {
4530 if (fp->f_type != DTYPE_VNODE) {
4535 if (vp->v_type != VREG) {
4542 end = offset + len - 1;
4544 case POSIX_FADV_SEQUENTIAL:
4545 case POSIX_FADV_RANDOM:
4546 case POSIX_FADV_NOREUSE:
4548 * Try to merge any existing non-standard region with
4549 * this new region if possible, otherwise create a new
4550 * non-standard region for this request.
4552 mtx_pool_lock(mtxpool_sleep, fp);
4554 if (fa != NULL && fa->fa_advice == advice &&
4555 ((fa->fa_start <= end && fa->fa_end >= offset) ||
4556 (end != OFF_MAX && fa->fa_start == end + 1) ||
4557 (fa->fa_end != OFF_MAX && fa->fa_end + 1 == offset))) {
4558 if (offset < fa->fa_start)
4559 fa->fa_start = offset;
4560 if (end > fa->fa_end)
4563 new->fa_advice = advice;
4564 new->fa_start = offset;
4566 new->fa_prevstart = 0;
4567 new->fa_prevend = 0;
4571 mtx_pool_unlock(mtxpool_sleep, fp);
4573 case POSIX_FADV_NORMAL:
4575 * If a the "normal" region overlaps with an existing
4576 * non-standard region, trim or remove the
4577 * non-standard region.
4579 mtx_pool_lock(mtxpool_sleep, fp);
4582 if (offset <= fa->fa_start && end >= fa->fa_end) {
4584 fp->f_advice = NULL;
4585 } else if (offset <= fa->fa_start &&
4586 end >= fa->fa_start)
4587 fa->fa_start = end + 1;
4588 else if (offset <= fa->fa_end && end >= fa->fa_end)
4589 fa->fa_end = offset - 1;
4590 else if (offset >= fa->fa_start && end <= fa->fa_end) {
4592 * If the "normal" region is a middle
4593 * portion of the existing
4594 * non-standard region, just remove
4595 * the whole thing rather than picking
4596 * one side or the other to
4600 fp->f_advice = NULL;
4603 mtx_pool_unlock(mtxpool_sleep, fp);
4605 case POSIX_FADV_WILLNEED:
4606 case POSIX_FADV_DONTNEED:
4607 error = VOP_ADVISE(vp, offset, end, advice);
4613 free(new, M_FADVISE);
4618 sys_posix_fadvise(struct thread *td, struct posix_fadvise_args *uap)
4621 td->td_retval[0] = kern_posix_fadvise(td, uap->fd, uap->offset,
4622 uap->len, uap->advice);