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_DEFINE(vfs, , stat, mode, mode);
96 SDT_PROBE_ARGTYPE(vfs, , stat, mode, 0, "char *");
97 SDT_PROBE_ARGTYPE(vfs, , stat, mode, 1, "int");
98 SDT_PROBE_DEFINE(vfs, , stat, reg, reg);
99 SDT_PROBE_ARGTYPE(vfs, , stat, reg, 0, "char *");
100 SDT_PROBE_ARGTYPE(vfs, , stat, reg, 1, "int");
102 static int chroot_refuse_vdir_fds(struct filedesc *fdp);
103 static int getutimes(const struct timeval *, enum uio_seg, struct timespec *);
104 static int kern_chflags(struct thread *td, const char *path,
105 enum uio_seg pathseg, u_long flags);
106 static int kern_chflagsat(struct thread *td, int fd, const char *path,
107 enum uio_seg pathseg, u_long flags, int atflag);
108 static int setfflags(struct thread *td, struct vnode *, u_long);
109 static int setutimes(struct thread *td, struct vnode *,
110 const struct timespec *, int, int);
111 static int vn_access(struct vnode *vp, int user_flags, struct ucred *cred,
115 * The module initialization routine for POSIX asynchronous I/O will
116 * set this to the version of AIO that it implements. (Zero means
117 * that it is not implemented.) This value is used here by pathconf()
118 * and in kern_descrip.c by fpathconf().
120 int async_io_version;
123 static int syncprt = 0;
124 SYSCTL_INT(_debug, OID_AUTO, syncprt, CTLFLAG_RW, &syncprt, 0, "");
128 * Sync each mounted filesystem.
130 #ifndef _SYS_SYSPROTO_H_
139 struct sync_args *uap;
141 struct mount *mp, *nmp;
144 mtx_lock(&mountlist_mtx);
145 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
146 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK)) {
147 nmp = TAILQ_NEXT(mp, mnt_list);
150 if ((mp->mnt_flag & MNT_RDONLY) == 0 &&
151 vn_start_write(NULL, &mp, V_NOWAIT) == 0) {
152 save = curthread_pflags_set(TDP_SYNCIO);
153 vfs_msync(mp, MNT_NOWAIT);
154 VFS_SYNC(mp, MNT_NOWAIT);
155 curthread_pflags_restore(save);
156 vn_finished_write(mp);
158 mtx_lock(&mountlist_mtx);
159 nmp = TAILQ_NEXT(mp, mnt_list);
162 mtx_unlock(&mountlist_mtx);
167 * Change filesystem quotas.
169 #ifndef _SYS_SYSPROTO_H_
170 struct quotactl_args {
178 sys_quotactl(td, uap)
180 register struct quotactl_args /* {
191 AUDIT_ARG_CMD(uap->cmd);
192 AUDIT_ARG_UID(uap->uid);
193 if (!prison_allow(td->td_ucred, PR_ALLOW_QUOTAS))
195 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
197 if ((error = namei(&nd)) != 0)
199 NDFREE(&nd, NDF_ONLY_PNBUF);
200 mp = nd.ni_vp->v_mount;
203 error = vfs_busy(mp, 0);
207 error = VFS_QUOTACTL(mp, uap->cmd, uap->uid, uap->arg);
210 * Since quota on operation typically needs to open quota
211 * file, the Q_QUOTAON handler needs to unbusy the mount point
212 * before calling into namei. Otherwise, unmount might be
213 * started between two vfs_busy() invocations (first is our,
214 * second is from mount point cross-walk code in lookup()),
217 * Require that Q_QUOTAON handles the vfs_busy() reference on
218 * its own, always returning with ubusied mount point.
220 if ((uap->cmd >> SUBCMDSHIFT) != Q_QUOTAON)
226 * Used by statfs conversion routines to scale the block size up if
227 * necessary so that all of the block counts are <= 'max_size'. Note
228 * that 'max_size' should be a bitmask, i.e. 2^n - 1 for some non-zero
232 statfs_scale_blocks(struct statfs *sf, long max_size)
237 KASSERT(powerof2(max_size + 1), ("%s: invalid max_size", __func__));
240 * Attempt to scale the block counts to give a more accurate
241 * overview to userland of the ratio of free space to used
242 * space. To do this, find the largest block count and compute
243 * a divisor that lets it fit into a signed integer <= max_size.
245 if (sf->f_bavail < 0)
246 count = -sf->f_bavail;
248 count = sf->f_bavail;
249 count = MAX(sf->f_blocks, MAX(sf->f_bfree, count));
250 if (count <= max_size)
253 count >>= flsl(max_size);
260 sf->f_bsize <<= shift;
261 sf->f_blocks >>= shift;
262 sf->f_bfree >>= shift;
263 sf->f_bavail >>= shift;
267 * Get filesystem statistics.
269 #ifndef _SYS_SYSPROTO_H_
278 register struct statfs_args /* {
286 error = kern_statfs(td, uap->path, UIO_USERSPACE, &sf);
288 error = copyout(&sf, uap->buf, sizeof(sf));
293 kern_statfs(struct thread *td, char *path, enum uio_seg pathseg,
297 struct statfs *sp, sb;
301 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
306 mp = nd.ni_vp->v_mount;
308 NDFREE(&nd, NDF_ONLY_PNBUF);
310 error = vfs_busy(mp, 0);
315 error = mac_mount_check_stat(td->td_ucred, mp);
320 * Set these in case the underlying filesystem fails to do so.
323 sp->f_version = STATFS_VERSION;
324 sp->f_namemax = NAME_MAX;
325 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
326 error = VFS_STATFS(mp, sp);
329 if (priv_check(td, PRIV_VFS_GENERATION)) {
330 bcopy(sp, &sb, sizeof(sb));
331 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
332 prison_enforce_statfs(td->td_ucred, mp, &sb);
342 * Get filesystem statistics.
344 #ifndef _SYS_SYSPROTO_H_
345 struct fstatfs_args {
353 register struct fstatfs_args /* {
361 error = kern_fstatfs(td, uap->fd, &sf);
363 error = copyout(&sf, uap->buf, sizeof(sf));
368 kern_fstatfs(struct thread *td, int fd, struct statfs *buf)
372 struct statfs *sp, sb;
377 error = getvnode(td->td_proc->p_fd, fd, CAP_FSTATFS, &fp);
381 vn_lock(vp, LK_SHARED | LK_RETRY);
383 AUDIT_ARG_VNODE1(vp);
394 error = vfs_busy(mp, 0);
399 error = mac_mount_check_stat(td->td_ucred, mp);
404 * Set these in case the underlying filesystem fails to do so.
407 sp->f_version = STATFS_VERSION;
408 sp->f_namemax = NAME_MAX;
409 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
410 error = VFS_STATFS(mp, sp);
413 if (priv_check(td, PRIV_VFS_GENERATION)) {
414 bcopy(sp, &sb, sizeof(sb));
415 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
416 prison_enforce_statfs(td->td_ucred, mp, &sb);
427 * Get statistics on all filesystems.
429 #ifndef _SYS_SYSPROTO_H_
430 struct getfsstat_args {
437 sys_getfsstat(td, uap)
439 register struct getfsstat_args /* {
446 return (kern_getfsstat(td, &uap->buf, uap->bufsize, UIO_USERSPACE,
451 * If (bufsize > 0 && bufseg == UIO_SYSSPACE)
452 * The caller is responsible for freeing memory which will be allocated
456 kern_getfsstat(struct thread *td, struct statfs **buf, size_t bufsize,
457 enum uio_seg bufseg, int flags)
459 struct mount *mp, *nmp;
460 struct statfs *sfsp, *sp, sb;
461 size_t count, maxcount;
464 maxcount = bufsize / sizeof(struct statfs);
467 else if (bufseg == UIO_USERSPACE)
469 else /* if (bufseg == UIO_SYSSPACE) */ {
471 mtx_lock(&mountlist_mtx);
472 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
475 mtx_unlock(&mountlist_mtx);
476 if (maxcount > count)
478 sfsp = *buf = malloc(maxcount * sizeof(struct statfs), M_TEMP,
482 mtx_lock(&mountlist_mtx);
483 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
484 if (prison_canseemount(td->td_ucred, mp) != 0) {
485 nmp = TAILQ_NEXT(mp, mnt_list);
489 if (mac_mount_check_stat(td->td_ucred, mp) != 0) {
490 nmp = TAILQ_NEXT(mp, mnt_list);
494 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK)) {
495 nmp = TAILQ_NEXT(mp, mnt_list);
498 if (sfsp && count < maxcount) {
501 * Set these in case the underlying filesystem
504 sp->f_version = STATFS_VERSION;
505 sp->f_namemax = NAME_MAX;
506 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
508 * If MNT_NOWAIT or MNT_LAZY is specified, do not
509 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
510 * overrides MNT_WAIT.
512 if (((flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
513 (flags & MNT_WAIT)) &&
514 (error = VFS_STATFS(mp, sp))) {
515 mtx_lock(&mountlist_mtx);
516 nmp = TAILQ_NEXT(mp, mnt_list);
520 if (priv_check(td, PRIV_VFS_GENERATION)) {
521 bcopy(sp, &sb, sizeof(sb));
522 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
523 prison_enforce_statfs(td->td_ucred, mp, &sb);
526 if (bufseg == UIO_SYSSPACE)
527 bcopy(sp, sfsp, sizeof(*sp));
528 else /* if (bufseg == UIO_USERSPACE) */ {
529 error = copyout(sp, sfsp, sizeof(*sp));
538 mtx_lock(&mountlist_mtx);
539 nmp = TAILQ_NEXT(mp, mnt_list);
542 mtx_unlock(&mountlist_mtx);
543 if (sfsp && count > maxcount)
544 td->td_retval[0] = maxcount;
546 td->td_retval[0] = count;
550 #ifdef COMPAT_FREEBSD4
552 * Get old format filesystem statistics.
554 static void cvtstatfs(struct statfs *, struct ostatfs *);
556 #ifndef _SYS_SYSPROTO_H_
557 struct freebsd4_statfs_args {
563 freebsd4_statfs(td, uap)
565 struct freebsd4_statfs_args /* {
574 error = kern_statfs(td, uap->path, UIO_USERSPACE, &sf);
577 cvtstatfs(&sf, &osb);
578 return (copyout(&osb, uap->buf, sizeof(osb)));
582 * Get filesystem statistics.
584 #ifndef _SYS_SYSPROTO_H_
585 struct freebsd4_fstatfs_args {
591 freebsd4_fstatfs(td, uap)
593 struct freebsd4_fstatfs_args /* {
602 error = kern_fstatfs(td, uap->fd, &sf);
605 cvtstatfs(&sf, &osb);
606 return (copyout(&osb, uap->buf, sizeof(osb)));
610 * Get statistics on all filesystems.
612 #ifndef _SYS_SYSPROTO_H_
613 struct freebsd4_getfsstat_args {
620 freebsd4_getfsstat(td, uap)
622 register struct freebsd4_getfsstat_args /* {
628 struct statfs *buf, *sp;
633 count = uap->bufsize / sizeof(struct ostatfs);
634 size = count * sizeof(struct statfs);
635 error = kern_getfsstat(td, &buf, size, UIO_SYSSPACE, uap->flags);
637 count = td->td_retval[0];
639 while (count > 0 && error == 0) {
641 error = copyout(&osb, uap->buf, sizeof(osb));
652 * Implement fstatfs() for (NFS) file handles.
654 #ifndef _SYS_SYSPROTO_H_
655 struct freebsd4_fhstatfs_args {
656 struct fhandle *u_fhp;
661 freebsd4_fhstatfs(td, uap)
663 struct freebsd4_fhstatfs_args /* {
664 struct fhandle *u_fhp;
673 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
676 error = kern_fhstatfs(td, fh, &sf);
679 cvtstatfs(&sf, &osb);
680 return (copyout(&osb, uap->buf, sizeof(osb)));
684 * Convert a new format statfs structure to an old format statfs structure.
692 statfs_scale_blocks(nsp, LONG_MAX);
693 bzero(osp, sizeof(*osp));
694 osp->f_bsize = nsp->f_bsize;
695 osp->f_iosize = MIN(nsp->f_iosize, LONG_MAX);
696 osp->f_blocks = nsp->f_blocks;
697 osp->f_bfree = nsp->f_bfree;
698 osp->f_bavail = nsp->f_bavail;
699 osp->f_files = MIN(nsp->f_files, LONG_MAX);
700 osp->f_ffree = MIN(nsp->f_ffree, LONG_MAX);
701 osp->f_owner = nsp->f_owner;
702 osp->f_type = nsp->f_type;
703 osp->f_flags = nsp->f_flags;
704 osp->f_syncwrites = MIN(nsp->f_syncwrites, LONG_MAX);
705 osp->f_asyncwrites = MIN(nsp->f_asyncwrites, LONG_MAX);
706 osp->f_syncreads = MIN(nsp->f_syncreads, LONG_MAX);
707 osp->f_asyncreads = MIN(nsp->f_asyncreads, LONG_MAX);
708 strlcpy(osp->f_fstypename, nsp->f_fstypename,
709 MIN(MFSNAMELEN, OMFSNAMELEN));
710 strlcpy(osp->f_mntonname, nsp->f_mntonname,
711 MIN(MNAMELEN, OMNAMELEN));
712 strlcpy(osp->f_mntfromname, nsp->f_mntfromname,
713 MIN(MNAMELEN, OMNAMELEN));
714 osp->f_fsid = nsp->f_fsid;
716 #endif /* COMPAT_FREEBSD4 */
719 * Change current working directory to a given file descriptor.
721 #ifndef _SYS_SYSPROTO_H_
729 struct fchdir_args /* {
733 register struct filedesc *fdp = td->td_proc->p_fd;
734 struct vnode *vp, *tdp, *vpold;
739 AUDIT_ARG_FD(uap->fd);
740 if ((error = getvnode(fdp, uap->fd, CAP_FCHDIR, &fp)) != 0)
745 vn_lock(vp, LK_SHARED | LK_RETRY);
746 AUDIT_ARG_VNODE1(vp);
747 error = change_dir(vp, td);
748 while (!error && (mp = vp->v_mountedhere) != NULL) {
751 error = VFS_ROOT(mp, LK_SHARED, &tdp);
764 vpold = fdp->fd_cdir;
766 FILEDESC_XUNLOCK(fdp);
772 * Change current working directory (``.'').
774 #ifndef _SYS_SYSPROTO_H_
782 struct chdir_args /* {
787 return (kern_chdir(td, uap->path, UIO_USERSPACE));
791 kern_chdir(struct thread *td, char *path, enum uio_seg pathseg)
793 register struct filedesc *fdp = td->td_proc->p_fd;
798 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
800 if ((error = namei(&nd)) != 0)
802 if ((error = change_dir(nd.ni_vp, td)) != 0) {
804 NDFREE(&nd, NDF_ONLY_PNBUF);
807 VOP_UNLOCK(nd.ni_vp, 0);
808 NDFREE(&nd, NDF_ONLY_PNBUF);
811 fdp->fd_cdir = nd.ni_vp;
812 FILEDESC_XUNLOCK(fdp);
818 * Helper function for raised chroot(2) security function: Refuse if
819 * any filedescriptors are open directories.
822 chroot_refuse_vdir_fds(fdp)
823 struct filedesc *fdp;
829 FILEDESC_LOCK_ASSERT(fdp);
831 for (fd = 0; fd < fdp->fd_nfiles ; fd++) {
832 fp = fget_locked(fdp, fd);
835 if (fp->f_type == DTYPE_VNODE) {
837 if (vp->v_type == VDIR)
845 * This sysctl determines if we will allow a process to chroot(2) if it
846 * has a directory open:
847 * 0: disallowed for all processes.
848 * 1: allowed for processes that were not already chroot(2)'ed.
849 * 2: allowed for all processes.
852 static int chroot_allow_open_directories = 1;
854 SYSCTL_INT(_kern, OID_AUTO, chroot_allow_open_directories, CTLFLAG_RW,
855 &chroot_allow_open_directories, 0,
856 "Allow a process to chroot(2) if it has a directory open");
859 * Change notion of root (``/'') directory.
861 #ifndef _SYS_SYSPROTO_H_
869 struct chroot_args /* {
876 error = priv_check(td, PRIV_VFS_CHROOT);
879 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
880 UIO_USERSPACE, uap->path, td);
884 if ((error = change_dir(nd.ni_vp, td)) != 0)
887 if ((error = mac_vnode_check_chroot(td->td_ucred, nd.ni_vp)))
890 VOP_UNLOCK(nd.ni_vp, 0);
891 error = change_root(nd.ni_vp, td);
893 NDFREE(&nd, NDF_ONLY_PNBUF);
898 NDFREE(&nd, NDF_ONLY_PNBUF);
903 * Common routine for chroot and chdir. Callers must provide a locked vnode
913 ASSERT_VOP_LOCKED(vp, "change_dir(): vp not locked");
914 if (vp->v_type != VDIR)
917 error = mac_vnode_check_chdir(td->td_ucred, vp);
921 error = VOP_ACCESS(vp, VEXEC, td->td_ucred, td);
926 * Common routine for kern_chroot() and jail_attach(). The caller is
927 * responsible for invoking priv_check() and mac_vnode_check_chroot() to
928 * authorize this operation.
935 struct filedesc *fdp;
939 fdp = td->td_proc->p_fd;
941 if (chroot_allow_open_directories == 0 ||
942 (chroot_allow_open_directories == 1 && fdp->fd_rdir != rootvnode)) {
943 error = chroot_refuse_vdir_fds(fdp);
945 FILEDESC_XUNLOCK(fdp);
949 oldvp = fdp->fd_rdir;
956 FILEDESC_XUNLOCK(fdp);
961 static __inline cap_rights_t
962 flags_to_rights(int flags)
964 cap_rights_t rights = 0;
966 if (flags & O_EXEC) {
967 rights |= CAP_FEXECVE;
969 switch ((flags & O_ACCMODE)) {
978 if (!(flags & (O_APPEND | O_TRUNC)))
985 rights |= CAP_CREATE;
988 rights |= CAP_FTRUNCATE;
990 if (flags & (O_SYNC | O_FSYNC))
993 if (flags & (O_EXLOCK | O_SHLOCK))
1000 * Check permissions, allocate an open file structure, and call the device
1001 * open routine if any.
1003 #ifndef _SYS_SYSPROTO_H_
1013 register struct open_args /* {
1020 return (kern_open(td, uap->path, UIO_USERSPACE, uap->flags, uap->mode));
1023 #ifndef _SYS_SYSPROTO_H_
1024 struct openat_args {
1032 sys_openat(struct thread *td, struct openat_args *uap)
1035 return (kern_openat(td, uap->fd, uap->path, UIO_USERSPACE, uap->flag,
1040 kern_open(struct thread *td, char *path, enum uio_seg pathseg, int flags,
1044 return (kern_openat(td, AT_FDCWD, path, pathseg, flags, mode));
1048 kern_openat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1049 int flags, int mode)
1051 struct proc *p = td->td_proc;
1052 struct filedesc *fdp = p->p_fd;
1056 int indx = -1, error;
1057 struct nameidata nd;
1058 cap_rights_t rights_needed = CAP_LOOKUP;
1060 AUDIT_ARG_FFLAGS(flags);
1061 AUDIT_ARG_MODE(mode);
1062 /* XXX: audit dirfd */
1063 rights_needed |= flags_to_rights(flags);
1065 * Only one of the O_EXEC, O_RDONLY, O_WRONLY and O_RDWR flags
1068 if (flags & O_EXEC) {
1069 if (flags & O_ACCMODE)
1071 } else if ((flags & O_ACCMODE) == O_ACCMODE) {
1074 flags = FFLAGS(flags);
1078 * Allocate the file descriptor, but don't install a descriptor yet.
1080 error = falloc_noinstall(td, &fp);
1084 * An extra reference on `fp' has been held for us by
1085 * falloc_noinstall().
1087 /* Set the flags early so the finit in devfs can pick them up. */
1088 fp->f_flag = flags & FMASK;
1089 cmode = ((mode &~ fdp->fd_cmask) & ALLPERMS) &~ S_ISTXT;
1090 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, fd,
1092 td->td_dupfd = -1; /* XXX check for fdopen */
1093 error = vn_open(&nd, &flags, cmode, fp);
1096 * If the vn_open replaced the method vector, something
1097 * wonderous happened deep below and we just pass it up
1098 * pretending we know what we do.
1100 if (error == ENXIO && fp->f_ops != &badfileops)
1104 * Handle special fdopen() case. bleh.
1106 * Don't do this for relative (capability) lookups; we don't
1107 * understand exactly what would happen, and we don't think
1108 * that it ever should.
1110 if (nd.ni_strictrelative == 0 &&
1111 (error == ENODEV || error == ENXIO) &&
1112 td->td_dupfd >= 0) {
1113 error = dupfdopen(td, fdp, td->td_dupfd, flags, error,
1122 NDFREE(&nd, NDF_ONLY_PNBUF);
1126 * Store the vnode, for any f_type. Typically, the vnode use
1127 * count is decremented by direct call to vn_closefile() for
1128 * files that switched type in the cdevsw fdopen() method.
1132 * If the file wasn't claimed by devfs bind it to the normal
1133 * vnode operations here.
1135 if (fp->f_ops == &badfileops) {
1136 KASSERT(vp->v_type != VFIFO, ("Unexpected fifo."));
1138 finit(fp, (flags & FMASK) | (fp->f_flag & FHASLOCK), DTYPE_VNODE,
1143 if (flags & O_TRUNC) {
1144 error = fo_truncate(fp, 0, td->td_ucred, td);
1150 * If we haven't already installed the FD (for dupfdopen), do so now.
1153 struct filecaps *fcaps;
1156 if (nd.ni_strictrelative == 1)
1157 fcaps = &nd.ni_filecaps;
1161 error = finstall(td, fp, &indx, flags, fcaps);
1162 /* On success finstall() consumes fcaps. */
1164 filecaps_free(&nd.ni_filecaps);
1168 filecaps_free(&nd.ni_filecaps);
1172 * Release our private reference, leaving the one associated with
1173 * the descriptor table intact.
1176 td->td_retval[0] = indx;
1179 KASSERT(indx == -1, ("indx=%d, should be -1", indx));
1188 #ifndef _SYS_SYSPROTO_H_
1189 struct ocreat_args {
1197 register struct ocreat_args /* {
1203 return (kern_open(td, uap->path, UIO_USERSPACE,
1204 O_WRONLY | O_CREAT | O_TRUNC, uap->mode));
1206 #endif /* COMPAT_43 */
1209 * Create a special file.
1211 #ifndef _SYS_SYSPROTO_H_
1221 register struct mknod_args /* {
1228 return (kern_mknod(td, uap->path, UIO_USERSPACE, uap->mode, uap->dev));
1231 #ifndef _SYS_SYSPROTO_H_
1232 struct mknodat_args {
1240 sys_mknodat(struct thread *td, struct mknodat_args *uap)
1243 return (kern_mknodat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode,
1248 kern_mknod(struct thread *td, char *path, enum uio_seg pathseg, int mode,
1252 return (kern_mknodat(td, AT_FDCWD, path, pathseg, mode, dev));
1256 kern_mknodat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1264 struct nameidata nd;
1266 AUDIT_ARG_MODE(mode);
1268 switch (mode & S_IFMT) {
1271 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
1274 error = priv_check(td, PRIV_VFS_MKNOD_BAD);
1277 error = priv_check(td, PRIV_VFS_MKNOD_WHT);
1281 return (kern_mkfifoat(td, fd, path, pathseg, mode));
1291 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1,
1292 pathseg, path, fd, CAP_MKNODAT, td);
1293 if ((error = namei(&nd)) != 0)
1297 NDFREE(&nd, NDF_ONLY_PNBUF);
1298 if (vp == nd.ni_dvp)
1306 vattr.va_mode = (mode & ALLPERMS) &
1307 ~td->td_proc->p_fd->fd_cmask;
1308 vattr.va_rdev = dev;
1311 switch (mode & S_IFMT) {
1312 case S_IFMT: /* used by badsect to flag bad sectors */
1313 vattr.va_type = VBAD;
1316 vattr.va_type = VCHR;
1319 vattr.va_type = VBLK;
1325 panic("kern_mknod: invalid mode");
1328 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1329 NDFREE(&nd, NDF_ONLY_PNBUF);
1331 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1336 if (error == 0 && !whiteout)
1337 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp,
1338 &nd.ni_cnd, &vattr);
1342 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, CREATE);
1344 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp,
1345 &nd.ni_cnd, &vattr);
1350 NDFREE(&nd, NDF_ONLY_PNBUF);
1352 vn_finished_write(mp);
1357 * Create a named pipe.
1359 #ifndef _SYS_SYSPROTO_H_
1360 struct mkfifo_args {
1368 register struct mkfifo_args /* {
1374 return (kern_mkfifo(td, uap->path, UIO_USERSPACE, uap->mode));
1377 #ifndef _SYS_SYSPROTO_H_
1378 struct mkfifoat_args {
1385 sys_mkfifoat(struct thread *td, struct mkfifoat_args *uap)
1388 return (kern_mkfifoat(td, uap->fd, uap->path, UIO_USERSPACE,
1393 kern_mkfifo(struct thread *td, char *path, enum uio_seg pathseg, int mode)
1396 return (kern_mkfifoat(td, AT_FDCWD, path, pathseg, mode));
1400 kern_mkfifoat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1406 struct nameidata nd;
1408 AUDIT_ARG_MODE(mode);
1411 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1,
1412 pathseg, path, fd, CAP_MKFIFOAT, td);
1413 if ((error = namei(&nd)) != 0)
1415 if (nd.ni_vp != NULL) {
1416 NDFREE(&nd, NDF_ONLY_PNBUF);
1417 if (nd.ni_vp == nd.ni_dvp)
1424 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1425 NDFREE(&nd, NDF_ONLY_PNBUF);
1427 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1432 vattr.va_type = VFIFO;
1433 vattr.va_mode = (mode & ALLPERMS) & ~td->td_proc->p_fd->fd_cmask;
1435 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1440 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
1447 vn_finished_write(mp);
1448 NDFREE(&nd, NDF_ONLY_PNBUF);
1453 * Make a hard file link.
1455 #ifndef _SYS_SYSPROTO_H_
1464 register struct link_args /* {
1470 return (kern_link(td, uap->path, uap->link, UIO_USERSPACE));
1473 #ifndef _SYS_SYSPROTO_H_
1474 struct linkat_args {
1483 sys_linkat(struct thread *td, struct linkat_args *uap)
1488 if (flag & ~AT_SYMLINK_FOLLOW)
1491 return (kern_linkat(td, uap->fd1, uap->fd2, uap->path1, uap->path2,
1492 UIO_USERSPACE, (flag & AT_SYMLINK_FOLLOW) ? FOLLOW : NOFOLLOW));
1495 int hardlink_check_uid = 0;
1496 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
1497 &hardlink_check_uid, 0,
1498 "Unprivileged processes cannot create hard links to files owned by other "
1500 static int hardlink_check_gid = 0;
1501 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
1502 &hardlink_check_gid, 0,
1503 "Unprivileged processes cannot create hard links to files owned by other "
1507 can_hardlink(struct vnode *vp, struct ucred *cred)
1512 if (!hardlink_check_uid && !hardlink_check_gid)
1515 error = VOP_GETATTR(vp, &va, cred);
1519 if (hardlink_check_uid && cred->cr_uid != va.va_uid) {
1520 error = priv_check_cred(cred, PRIV_VFS_LINK, 0);
1525 if (hardlink_check_gid && !groupmember(va.va_gid, cred)) {
1526 error = priv_check_cred(cred, PRIV_VFS_LINK, 0);
1535 kern_link(struct thread *td, char *path, char *link, enum uio_seg segflg)
1538 return (kern_linkat(td, AT_FDCWD, AT_FDCWD, path,link, segflg, FOLLOW));
1542 kern_linkat(struct thread *td, int fd1, int fd2, char *path1, char *path2,
1543 enum uio_seg segflg, int follow)
1547 struct nameidata nd;
1551 NDINIT_AT(&nd, LOOKUP, follow | AUDITVNODE1, segflg, path1, fd1, td);
1553 if ((error = namei(&nd)) != 0)
1555 NDFREE(&nd, NDF_ONLY_PNBUF);
1557 if (vp->v_type == VDIR) {
1559 return (EPERM); /* POSIX */
1561 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0) {
1565 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE2,
1566 segflg, path2, fd2, CAP_LINKAT, td);
1567 if ((error = namei(&nd)) == 0) {
1568 if (nd.ni_vp != NULL) {
1569 if (nd.ni_dvp == nd.ni_vp)
1575 } else if ((error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY))
1577 error = can_hardlink(vp, td->td_ucred);
1580 error = mac_vnode_check_link(td->td_ucred,
1581 nd.ni_dvp, vp, &nd.ni_cnd);
1584 error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
1588 NDFREE(&nd, NDF_ONLY_PNBUF);
1591 vn_finished_write(mp);
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_symlink(td, uap->path, uap->link, UIO_USERSPACE));
1616 #ifndef _SYS_SYSPROTO_H_
1617 struct symlinkat_args {
1624 sys_symlinkat(struct thread *td, struct symlinkat_args *uap)
1627 return (kern_symlinkat(td, uap->path1, uap->fd, uap->path2,
1632 kern_symlink(struct thread *td, char *path, char *link, enum uio_seg segflg)
1635 return (kern_symlinkat(td, path, AT_FDCWD, link, segflg));
1639 kern_symlinkat(struct thread *td, char *path1, int fd, char *path2,
1640 enum uio_seg segflg)
1646 struct nameidata nd;
1648 if (segflg == UIO_SYSSPACE) {
1651 syspath = uma_zalloc(namei_zone, M_WAITOK);
1652 if ((error = copyinstr(path1, syspath, MAXPATHLEN, NULL)) != 0)
1655 AUDIT_ARG_TEXT(syspath);
1658 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1,
1659 segflg, path2, fd, CAP_SYMLINKAT, td);
1660 if ((error = namei(&nd)) != 0)
1663 NDFREE(&nd, NDF_ONLY_PNBUF);
1664 if (nd.ni_vp == nd.ni_dvp)
1672 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1673 NDFREE(&nd, NDF_ONLY_PNBUF);
1675 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1680 vattr.va_mode = ACCESSPERMS &~ td->td_proc->p_fd->fd_cmask;
1682 vattr.va_type = VLNK;
1683 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1688 error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr, syspath);
1694 NDFREE(&nd, NDF_ONLY_PNBUF);
1696 vn_finished_write(mp);
1698 if (segflg != UIO_SYSSPACE)
1699 uma_zfree(namei_zone, syspath);
1704 * Delete a whiteout from the filesystem.
1707 sys_undelete(td, uap)
1709 register struct undelete_args /* {
1715 struct nameidata nd;
1719 NDINIT(&nd, DELETE, LOCKPARENT | DOWHITEOUT | AUDITVNODE1,
1720 UIO_USERSPACE, uap->path, td);
1725 if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & ISWHITEOUT)) {
1726 NDFREE(&nd, NDF_ONLY_PNBUF);
1727 if (nd.ni_vp == nd.ni_dvp)
1735 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1736 NDFREE(&nd, NDF_ONLY_PNBUF);
1738 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1742 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, DELETE);
1743 NDFREE(&nd, NDF_ONLY_PNBUF);
1745 vn_finished_write(mp);
1750 * Delete a name from the filesystem.
1752 #ifndef _SYS_SYSPROTO_H_
1753 struct unlink_args {
1760 struct unlink_args /* {
1765 return (kern_unlink(td, uap->path, UIO_USERSPACE));
1768 #ifndef _SYS_SYSPROTO_H_
1769 struct unlinkat_args {
1776 sys_unlinkat(struct thread *td, struct unlinkat_args *uap)
1778 int flag = uap->flag;
1780 char *path = uap->path;
1782 if (flag & ~AT_REMOVEDIR)
1785 if (flag & AT_REMOVEDIR)
1786 return (kern_rmdirat(td, fd, path, UIO_USERSPACE));
1788 return (kern_unlinkat(td, fd, path, UIO_USERSPACE, 0));
1792 kern_unlink(struct thread *td, char *path, enum uio_seg pathseg)
1795 return (kern_unlinkat(td, AT_FDCWD, path, pathseg, 0));
1799 kern_unlinkat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1805 struct nameidata nd;
1810 NDINIT_ATRIGHTS(&nd, DELETE, LOCKPARENT | LOCKLEAF | AUDITVNODE1,
1811 pathseg, path, fd, CAP_UNLINKAT, td);
1812 if ((error = namei(&nd)) != 0)
1813 return (error == EINVAL ? EPERM : error);
1815 if (vp->v_type == VDIR && oldinum == 0) {
1816 error = EPERM; /* POSIX */
1817 } else if (oldinum != 0 &&
1818 ((error = vn_stat(vp, &sb, td->td_ucred, NOCRED, td)) == 0) &&
1819 sb.st_ino != oldinum) {
1820 error = EIDRM; /* Identifier removed */
1823 * The root of a mounted filesystem cannot be deleted.
1825 * XXX: can this only be a VDIR case?
1827 if (vp->v_vflag & VV_ROOT)
1831 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1832 NDFREE(&nd, NDF_ONLY_PNBUF);
1834 if (vp == nd.ni_dvp)
1838 if ((error = vn_start_write(NULL, &mp,
1839 V_XSLEEP | PCATCH)) != 0)
1844 error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp,
1849 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_UNLINK);
1850 error = VOP_REMOVE(nd.ni_dvp, vp, &nd.ni_cnd);
1854 vn_finished_write(mp);
1856 NDFREE(&nd, NDF_ONLY_PNBUF);
1858 if (vp == nd.ni_dvp)
1866 * Reposition read/write file offset.
1868 #ifndef _SYS_SYSPROTO_H_
1879 register struct lseek_args /* {
1886 struct ucred *cred = td->td_ucred;
1890 off_t foffset, offset, size;
1893 AUDIT_ARG_FD(uap->fd);
1894 if ((error = fget(td, uap->fd, CAP_SEEK, &fp)) != 0)
1896 if (!(fp->f_ops->fo_flags & DFLAG_SEEKABLE)) {
1901 foffset = foffset_lock(fp, 0);
1902 noneg = (vp->v_type != VCHR);
1903 offset = uap->offset;
1904 switch (uap->whence) {
1908 (offset > 0 && foffset > OFF_MAX - offset))) {
1915 vn_lock(vp, LK_SHARED | LK_RETRY);
1916 error = VOP_GETATTR(vp, &vattr, cred);
1922 * If the file references a disk device, then fetch
1923 * the media size and use that to determine the ending
1926 if (vattr.va_size == 0 && vp->v_type == VCHR &&
1927 fo_ioctl(fp, DIOCGMEDIASIZE, &size, cred, td) == 0)
1928 vattr.va_size = size;
1930 (vattr.va_size > OFF_MAX ||
1931 (offset > 0 && vattr.va_size > OFF_MAX - offset))) {
1935 offset += vattr.va_size;
1940 error = fo_ioctl(fp, FIOSEEKDATA, &offset, cred, td);
1943 error = fo_ioctl(fp, FIOSEEKHOLE, &offset, cred, td);
1948 if (error == 0 && noneg && offset < 0)
1952 VFS_KNOTE_UNLOCKED(vp, 0);
1953 *(off_t *)(td->td_retval) = offset;
1956 foffset_unlock(fp, offset, error != 0 ? FOF_NOUPDATE : 0);
1960 #if defined(COMPAT_43)
1962 * Reposition read/write file offset.
1964 #ifndef _SYS_SYSPROTO_H_
1965 struct olseek_args {
1974 register struct olseek_args /* {
1980 struct lseek_args /* {
1988 nuap.offset = uap->offset;
1989 nuap.whence = uap->whence;
1990 return (sys_lseek(td, &nuap));
1992 #endif /* COMPAT_43 */
1994 /* Version with the 'pad' argument */
1996 freebsd6_lseek(td, uap)
1998 register struct freebsd6_lseek_args *uap;
2000 struct lseek_args ouap;
2003 ouap.offset = uap->offset;
2004 ouap.whence = uap->whence;
2005 return (sys_lseek(td, &ouap));
2009 * Check access permissions using passed credentials.
2012 vn_access(vp, user_flags, cred, td)
2021 /* Flags == 0 means only check for existence. */
2025 if (user_flags & R_OK)
2027 if (user_flags & W_OK)
2029 if (user_flags & X_OK)
2032 error = mac_vnode_check_access(cred, vp, accmode);
2036 if ((accmode & VWRITE) == 0 || (error = vn_writechk(vp)) == 0)
2037 error = VOP_ACCESS(vp, accmode, cred, td);
2043 * Check access permissions using "real" credentials.
2045 #ifndef _SYS_SYSPROTO_H_
2046 struct access_args {
2054 register struct access_args /* {
2060 return (kern_access(td, uap->path, UIO_USERSPACE, uap->amode));
2063 #ifndef _SYS_SYSPROTO_H_
2064 struct faccessat_args {
2072 sys_faccessat(struct thread *td, struct faccessat_args *uap)
2075 if (uap->flag & ~AT_EACCESS)
2077 return (kern_accessat(td, uap->fd, uap->path, UIO_USERSPACE, uap->flag,
2082 kern_access(struct thread *td, char *path, enum uio_seg pathseg, int amode)
2085 return (kern_accessat(td, AT_FDCWD, path, pathseg, 0, amode));
2089 kern_accessat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2090 int flag, int amode)
2092 struct ucred *cred, *tmpcred;
2094 struct nameidata nd;
2098 * Create and modify a temporary credential instead of one that
2099 * is potentially shared.
2101 if (!(flag & AT_EACCESS)) {
2102 cred = td->td_ucred;
2103 tmpcred = crdup(cred);
2104 tmpcred->cr_uid = cred->cr_ruid;
2105 tmpcred->cr_groups[0] = cred->cr_rgid;
2106 td->td_ucred = tmpcred;
2108 cred = tmpcred = td->td_ucred;
2109 AUDIT_ARG_VALUE(amode);
2110 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF |
2111 AUDITVNODE1, pathseg, path, fd, CAP_FSTAT, td);
2112 if ((error = namei(&nd)) != 0)
2116 error = vn_access(vp, amode, tmpcred, td);
2117 NDFREE(&nd, NDF_ONLY_PNBUF);
2120 if (!(flag & AT_EACCESS)) {
2121 td->td_ucred = cred;
2128 * Check access permissions using "effective" credentials.
2130 #ifndef _SYS_SYSPROTO_H_
2131 struct eaccess_args {
2137 sys_eaccess(td, uap)
2139 register struct eaccess_args /* {
2145 return (kern_eaccess(td, uap->path, UIO_USERSPACE, uap->amode));
2149 kern_eaccess(struct thread *td, char *path, enum uio_seg pathseg, int amode)
2152 return (kern_accessat(td, AT_FDCWD, path, pathseg, AT_EACCESS, amode));
2155 #if defined(COMPAT_43)
2157 * Get file status; this version follows links.
2159 #ifndef _SYS_SYSPROTO_H_
2168 register struct ostat_args /* {
2177 error = kern_stat(td, uap->path, UIO_USERSPACE, &sb);
2181 error = copyout(&osb, uap->ub, sizeof (osb));
2186 * Get file status; this version does not follow links.
2188 #ifndef _SYS_SYSPROTO_H_
2189 struct olstat_args {
2197 register struct olstat_args /* {
2206 error = kern_lstat(td, uap->path, UIO_USERSPACE, &sb);
2210 error = copyout(&osb, uap->ub, sizeof (osb));
2215 * Convert from an old to a new stat structure.
2223 ost->st_dev = st->st_dev;
2224 ost->st_ino = st->st_ino;
2225 ost->st_mode = st->st_mode;
2226 ost->st_nlink = st->st_nlink;
2227 ost->st_uid = st->st_uid;
2228 ost->st_gid = st->st_gid;
2229 ost->st_rdev = st->st_rdev;
2230 if (st->st_size < (quad_t)1 << 32)
2231 ost->st_size = st->st_size;
2234 ost->st_atim = st->st_atim;
2235 ost->st_mtim = st->st_mtim;
2236 ost->st_ctim = st->st_ctim;
2237 ost->st_blksize = st->st_blksize;
2238 ost->st_blocks = st->st_blocks;
2239 ost->st_flags = st->st_flags;
2240 ost->st_gen = st->st_gen;
2242 #endif /* COMPAT_43 */
2245 * Get file status; this version follows links.
2247 #ifndef _SYS_SYSPROTO_H_
2256 register struct stat_args /* {
2264 error = kern_stat(td, uap->path, UIO_USERSPACE, &sb);
2266 error = copyout(&sb, uap->ub, sizeof (sb));
2270 #ifndef _SYS_SYSPROTO_H_
2271 struct fstatat_args {
2279 sys_fstatat(struct thread *td, struct fstatat_args *uap)
2284 error = kern_statat(td, uap->flag, uap->fd, uap->path,
2285 UIO_USERSPACE, &sb);
2287 error = copyout(&sb, uap->buf, sizeof (sb));
2292 kern_stat(struct thread *td, char *path, enum uio_seg pathseg, struct stat *sbp)
2295 return (kern_statat(td, 0, AT_FDCWD, path, pathseg, sbp));
2299 kern_statat(struct thread *td, int flag, int fd, char *path,
2300 enum uio_seg pathseg, struct stat *sbp)
2303 return (kern_statat_vnhook(td, flag, fd, path, pathseg, sbp, NULL));
2307 kern_statat_vnhook(struct thread *td, int flag, int fd, char *path,
2308 enum uio_seg pathseg, struct stat *sbp,
2309 void (*hook)(struct vnode *vp, struct stat *sbp))
2311 struct nameidata nd;
2315 if (flag & ~AT_SYMLINK_NOFOLLOW)
2318 NDINIT_ATRIGHTS(&nd, LOOKUP, ((flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW :
2319 FOLLOW) | LOCKSHARED | LOCKLEAF | AUDITVNODE1, pathseg, path, fd,
2322 if ((error = namei(&nd)) != 0)
2324 error = vn_stat(nd.ni_vp, &sb, td->td_ucred, NOCRED, td);
2326 SDT_PROBE(vfs, , stat, mode, path, sb.st_mode, 0, 0, 0);
2327 if (S_ISREG(sb.st_mode))
2328 SDT_PROBE(vfs, , stat, reg, path, pathseg, 0, 0, 0);
2329 if (__predict_false(hook != NULL))
2330 hook(nd.ni_vp, &sb);
2332 NDFREE(&nd, NDF_ONLY_PNBUF);
2338 if (KTRPOINT(td, KTR_STRUCT))
2345 * Get file status; this version does not follow links.
2347 #ifndef _SYS_SYSPROTO_H_
2356 register struct lstat_args /* {
2364 error = kern_lstat(td, uap->path, UIO_USERSPACE, &sb);
2366 error = copyout(&sb, uap->ub, sizeof (sb));
2371 kern_lstat(struct thread *td, char *path, enum uio_seg pathseg, struct stat *sbp)
2374 return (kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, path, pathseg,
2379 * Implementation of the NetBSD [l]stat() functions.
2386 bzero(nsb, sizeof *nsb);
2387 nsb->st_dev = sb->st_dev;
2388 nsb->st_ino = sb->st_ino;
2389 nsb->st_mode = sb->st_mode;
2390 nsb->st_nlink = sb->st_nlink;
2391 nsb->st_uid = sb->st_uid;
2392 nsb->st_gid = sb->st_gid;
2393 nsb->st_rdev = sb->st_rdev;
2394 nsb->st_atim = sb->st_atim;
2395 nsb->st_mtim = sb->st_mtim;
2396 nsb->st_ctim = sb->st_ctim;
2397 nsb->st_size = sb->st_size;
2398 nsb->st_blocks = sb->st_blocks;
2399 nsb->st_blksize = sb->st_blksize;
2400 nsb->st_flags = sb->st_flags;
2401 nsb->st_gen = sb->st_gen;
2402 nsb->st_birthtim = sb->st_birthtim;
2405 #ifndef _SYS_SYSPROTO_H_
2414 register struct nstat_args /* {
2423 error = kern_stat(td, uap->path, UIO_USERSPACE, &sb);
2426 cvtnstat(&sb, &nsb);
2427 error = copyout(&nsb, uap->ub, sizeof (nsb));
2432 * NetBSD lstat. Get file status; this version does not follow links.
2434 #ifndef _SYS_SYSPROTO_H_
2443 register struct nlstat_args /* {
2452 error = kern_lstat(td, uap->path, UIO_USERSPACE, &sb);
2455 cvtnstat(&sb, &nsb);
2456 error = copyout(&nsb, uap->ub, sizeof (nsb));
2461 * Get configurable pathname variables.
2463 #ifndef _SYS_SYSPROTO_H_
2464 struct pathconf_args {
2470 sys_pathconf(td, uap)
2472 register struct pathconf_args /* {
2478 return (kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name, FOLLOW));
2481 #ifndef _SYS_SYSPROTO_H_
2482 struct lpathconf_args {
2488 sys_lpathconf(td, uap)
2490 register struct lpathconf_args /* {
2496 return (kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name,
2501 kern_pathconf(struct thread *td, char *path, enum uio_seg pathseg, int name,
2504 struct nameidata nd;
2507 NDINIT(&nd, LOOKUP, LOCKSHARED | LOCKLEAF | AUDITVNODE1 | flags,
2509 if ((error = namei(&nd)) != 0)
2511 NDFREE(&nd, NDF_ONLY_PNBUF);
2513 /* If asynchronous I/O is available, it works for all files. */
2514 if (name == _PC_ASYNC_IO)
2515 td->td_retval[0] = async_io_version;
2517 error = VOP_PATHCONF(nd.ni_vp, name, td->td_retval);
2523 * Return target name of a symbolic link.
2525 #ifndef _SYS_SYSPROTO_H_
2526 struct readlink_args {
2533 sys_readlink(td, uap)
2535 register struct readlink_args /* {
2542 return (kern_readlink(td, uap->path, UIO_USERSPACE, uap->buf,
2543 UIO_USERSPACE, uap->count));
2545 #ifndef _SYS_SYSPROTO_H_
2546 struct readlinkat_args {
2554 sys_readlinkat(struct thread *td, struct readlinkat_args *uap)
2557 return (kern_readlinkat(td, uap->fd, uap->path, UIO_USERSPACE,
2558 uap->buf, UIO_USERSPACE, uap->bufsize));
2562 kern_readlink(struct thread *td, char *path, enum uio_seg pathseg, char *buf,
2563 enum uio_seg bufseg, size_t count)
2566 return (kern_readlinkat(td, AT_FDCWD, path, pathseg, buf, bufseg,
2571 kern_readlinkat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2572 char *buf, enum uio_seg bufseg, size_t count)
2578 struct nameidata nd;
2580 if (count > IOSIZE_MAX)
2583 NDINIT_AT(&nd, LOOKUP, NOFOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
2584 pathseg, path, fd, td);
2586 if ((error = namei(&nd)) != 0)
2588 NDFREE(&nd, NDF_ONLY_PNBUF);
2591 error = mac_vnode_check_readlink(td->td_ucred, vp);
2597 if (vp->v_type != VLNK)
2600 aiov.iov_base = buf;
2601 aiov.iov_len = count;
2602 auio.uio_iov = &aiov;
2603 auio.uio_iovcnt = 1;
2604 auio.uio_offset = 0;
2605 auio.uio_rw = UIO_READ;
2606 auio.uio_segflg = bufseg;
2608 auio.uio_resid = count;
2609 error = VOP_READLINK(vp, &auio, td->td_ucred);
2612 td->td_retval[0] = count - auio.uio_resid;
2617 * Common implementation code for chflags() and fchflags().
2620 setfflags(td, vp, flags)
2629 /* We can't support the value matching VNOVAL. */
2630 if (flags == VNOVAL)
2631 return (EOPNOTSUPP);
2634 * Prevent non-root users from setting flags on devices. When
2635 * a device is reused, users can retain ownership of the device
2636 * if they are allowed to set flags and programs assume that
2637 * chown can't fail when done as root.
2639 if (vp->v_type == VCHR || vp->v_type == VBLK) {
2640 error = priv_check(td, PRIV_VFS_CHFLAGS_DEV);
2645 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2648 vattr.va_flags = flags;
2649 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2651 error = mac_vnode_check_setflags(td->td_ucred, vp, vattr.va_flags);
2654 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
2656 vn_finished_write(mp);
2661 * Change flags of a file given a path name.
2663 #ifndef _SYS_SYSPROTO_H_
2664 struct chflags_args {
2670 sys_chflags(td, uap)
2672 register struct chflags_args /* {
2678 return (kern_chflags(td, uap->path, UIO_USERSPACE, uap->flags));
2681 #ifndef _SYS_SYSPROTO_H_
2682 struct chflagsat_args {
2690 sys_chflagsat(struct thread *td, struct chflagsat_args *uap)
2693 const char *path = uap->path;
2694 u_long flags = uap->flags;
2695 int atflag = uap->atflag;
2697 if (atflag & ~AT_SYMLINK_NOFOLLOW)
2700 return (kern_chflagsat(td, fd, path, UIO_USERSPACE, flags, atflag));
2704 kern_chflags(struct thread *td, const char *path, enum uio_seg pathseg,
2708 return (kern_chflagsat(td, AT_FDCWD, path, pathseg, flags, 0));
2712 * Same as chflags() but doesn't follow symlinks.
2715 sys_lchflags(td, uap)
2717 register struct lchflags_args /* {
2723 return (kern_chflagsat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2724 uap->flags, AT_SYMLINK_NOFOLLOW));
2728 kern_chflagsat(struct thread *td, int fd, const char *path,
2729 enum uio_seg pathseg, u_long flags, int atflag)
2731 struct nameidata nd;
2734 AUDIT_ARG_FFLAGS(flags);
2735 follow = (atflag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
2736 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, pathseg, path, fd,
2738 if ((error = namei(&nd)) != 0)
2740 NDFREE(&nd, NDF_ONLY_PNBUF);
2741 error = setfflags(td, nd.ni_vp, flags);
2747 * Change flags of a file given a file descriptor.
2749 #ifndef _SYS_SYSPROTO_H_
2750 struct fchflags_args {
2756 sys_fchflags(td, uap)
2758 register struct fchflags_args /* {
2766 AUDIT_ARG_FD(uap->fd);
2767 AUDIT_ARG_FFLAGS(uap->flags);
2768 if ((error = getvnode(td->td_proc->p_fd, uap->fd, CAP_FCHFLAGS,
2772 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
2773 AUDIT_ARG_VNODE1(fp->f_vnode);
2774 VOP_UNLOCK(fp->f_vnode, 0);
2776 error = setfflags(td, fp->f_vnode, uap->flags);
2782 * Common implementation code for chmod(), lchmod() and fchmod().
2785 setfmode(td, cred, vp, mode)
2795 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2797 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2799 vattr.va_mode = mode & ALLPERMS;
2801 error = mac_vnode_check_setmode(cred, vp, vattr.va_mode);
2804 error = VOP_SETATTR(vp, &vattr, cred);
2806 vn_finished_write(mp);
2811 * Change mode of a file given path name.
2813 #ifndef _SYS_SYSPROTO_H_
2822 register struct chmod_args /* {
2828 return (kern_chmod(td, uap->path, UIO_USERSPACE, uap->mode));
2831 #ifndef _SYS_SYSPROTO_H_
2832 struct fchmodat_args {
2840 sys_fchmodat(struct thread *td, struct fchmodat_args *uap)
2842 int flag = uap->flag;
2844 char *path = uap->path;
2845 mode_t mode = uap->mode;
2847 if (flag & ~AT_SYMLINK_NOFOLLOW)
2850 return (kern_fchmodat(td, fd, path, UIO_USERSPACE, mode, flag));
2854 kern_chmod(struct thread *td, char *path, enum uio_seg pathseg, int mode)
2857 return (kern_fchmodat(td, AT_FDCWD, path, pathseg, mode, 0));
2861 * Change mode of a file given path name (don't follow links.)
2863 #ifndef _SYS_SYSPROTO_H_
2864 struct lchmod_args {
2872 register struct lchmod_args /* {
2878 return (kern_fchmodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2879 uap->mode, AT_SYMLINK_NOFOLLOW));
2883 kern_fchmodat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2884 mode_t mode, int flag)
2887 struct nameidata nd;
2890 AUDIT_ARG_MODE(mode);
2891 follow = (flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
2892 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, pathseg, path, fd,
2894 if ((error = namei(&nd)) != 0)
2896 NDFREE(&nd, NDF_ONLY_PNBUF);
2897 error = setfmode(td, td->td_ucred, nd.ni_vp, mode);
2903 * Change mode of a file given a file descriptor.
2905 #ifndef _SYS_SYSPROTO_H_
2906 struct fchmod_args {
2912 sys_fchmod(struct thread *td, struct fchmod_args *uap)
2917 AUDIT_ARG_FD(uap->fd);
2918 AUDIT_ARG_MODE(uap->mode);
2920 error = fget(td, uap->fd, CAP_FCHMOD, &fp);
2923 error = fo_chmod(fp, uap->mode, td->td_ucred, td);
2929 * Common implementation for chown(), lchown(), and fchown()
2932 setfown(td, cred, vp, uid, gid)
2943 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2945 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2950 error = mac_vnode_check_setowner(cred, vp, vattr.va_uid,
2954 error = VOP_SETATTR(vp, &vattr, cred);
2956 vn_finished_write(mp);
2961 * Set ownership given a path name.
2963 #ifndef _SYS_SYSPROTO_H_
2973 register struct chown_args /* {
2980 return (kern_chown(td, uap->path, UIO_USERSPACE, uap->uid, uap->gid));
2983 #ifndef _SYS_SYSPROTO_H_
2984 struct fchownat_args {
2993 sys_fchownat(struct thread *td, struct fchownat_args *uap)
2998 if (flag & ~AT_SYMLINK_NOFOLLOW)
3001 return (kern_fchownat(td, uap->fd, uap->path, UIO_USERSPACE, uap->uid,
3002 uap->gid, uap->flag));
3006 kern_chown(struct thread *td, char *path, enum uio_seg pathseg, int uid,
3010 return (kern_fchownat(td, AT_FDCWD, path, pathseg, uid, gid, 0));
3014 kern_fchownat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
3015 int uid, int gid, int flag)
3017 struct nameidata nd;
3020 AUDIT_ARG_OWNER(uid, gid);
3021 follow = (flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
3022 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, pathseg, path, fd,
3025 if ((error = namei(&nd)) != 0)
3027 NDFREE(&nd, NDF_ONLY_PNBUF);
3028 error = setfown(td, td->td_ucred, nd.ni_vp, uid, gid);
3034 * Set ownership given a path name, do not cross symlinks.
3036 #ifndef _SYS_SYSPROTO_H_
3037 struct lchown_args {
3046 register struct lchown_args /* {
3053 return (kern_lchown(td, uap->path, UIO_USERSPACE, uap->uid, uap->gid));
3057 kern_lchown(struct thread *td, char *path, enum uio_seg pathseg, int uid,
3061 return (kern_fchownat(td, AT_FDCWD, path, pathseg, uid, gid,
3062 AT_SYMLINK_NOFOLLOW));
3066 * Set ownership given a file descriptor.
3068 #ifndef _SYS_SYSPROTO_H_
3069 struct fchown_args {
3078 register struct fchown_args /* {
3087 AUDIT_ARG_FD(uap->fd);
3088 AUDIT_ARG_OWNER(uap->uid, uap->gid);
3089 error = fget(td, uap->fd, CAP_FCHOWN, &fp);
3092 error = fo_chown(fp, uap->uid, uap->gid, td->td_ucred, td);
3098 * Common implementation code for utimes(), lutimes(), and futimes().
3101 getutimes(usrtvp, tvpseg, tsp)
3102 const struct timeval *usrtvp;
3103 enum uio_seg tvpseg;
3104 struct timespec *tsp;
3106 struct timeval tv[2];
3107 const struct timeval *tvp;
3110 if (usrtvp == NULL) {
3111 vfs_timestamp(&tsp[0]);
3114 if (tvpseg == UIO_SYSSPACE) {
3117 if ((error = copyin(usrtvp, tv, sizeof(tv))) != 0)
3122 if (tvp[0].tv_usec < 0 || tvp[0].tv_usec >= 1000000 ||
3123 tvp[1].tv_usec < 0 || tvp[1].tv_usec >= 1000000)
3125 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
3126 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
3132 * Common implementation code for utimes(), lutimes(), and futimes().
3135 setutimes(td, vp, ts, numtimes, nullflag)
3138 const struct timespec *ts;
3142 int error, setbirthtime;
3146 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
3148 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3150 if (numtimes < 3 && !VOP_GETATTR(vp, &vattr, td->td_ucred) &&
3151 timespeccmp(&ts[1], &vattr.va_birthtime, < ))
3154 vattr.va_atime = ts[0];
3155 vattr.va_mtime = ts[1];
3157 vattr.va_birthtime = ts[1];
3159 vattr.va_birthtime = ts[2];
3161 vattr.va_vaflags |= VA_UTIMES_NULL;
3163 error = mac_vnode_check_setutimes(td->td_ucred, vp, vattr.va_atime,
3167 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3169 vn_finished_write(mp);
3174 * Set the access and modification times of a file.
3176 #ifndef _SYS_SYSPROTO_H_
3177 struct utimes_args {
3179 struct timeval *tptr;
3185 register struct utimes_args /* {
3187 struct timeval *tptr;
3191 return (kern_utimes(td, uap->path, UIO_USERSPACE, uap->tptr,
3195 #ifndef _SYS_SYSPROTO_H_
3196 struct futimesat_args {
3199 const struct timeval * times;
3203 sys_futimesat(struct thread *td, struct futimesat_args *uap)
3206 return (kern_utimesat(td, uap->fd, uap->path, UIO_USERSPACE,
3207 uap->times, UIO_USERSPACE));
3211 kern_utimes(struct thread *td, char *path, enum uio_seg pathseg,
3212 struct timeval *tptr, enum uio_seg tptrseg)
3215 return (kern_utimesat(td, AT_FDCWD, path, pathseg, tptr, tptrseg));
3219 kern_utimesat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
3220 struct timeval *tptr, enum uio_seg tptrseg)
3222 struct nameidata nd;
3223 struct timespec ts[2];
3226 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3228 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, fd,
3231 if ((error = namei(&nd)) != 0)
3233 NDFREE(&nd, NDF_ONLY_PNBUF);
3234 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3240 * Set the access and modification times of a file.
3242 #ifndef _SYS_SYSPROTO_H_
3243 struct lutimes_args {
3245 struct timeval *tptr;
3249 sys_lutimes(td, uap)
3251 register struct lutimes_args /* {
3253 struct timeval *tptr;
3257 return (kern_lutimes(td, uap->path, UIO_USERSPACE, uap->tptr,
3262 kern_lutimes(struct thread *td, char *path, enum uio_seg pathseg,
3263 struct timeval *tptr, enum uio_seg tptrseg)
3265 struct timespec ts[2];
3267 struct nameidata nd;
3269 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3271 NDINIT(&nd, LOOKUP, NOFOLLOW | AUDITVNODE1, pathseg, path, td);
3272 if ((error = namei(&nd)) != 0)
3274 NDFREE(&nd, NDF_ONLY_PNBUF);
3275 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3281 * Set the access and modification times of a file.
3283 #ifndef _SYS_SYSPROTO_H_
3284 struct futimes_args {
3286 struct timeval *tptr;
3290 sys_futimes(td, uap)
3292 register struct futimes_args /* {
3294 struct timeval *tptr;
3298 return (kern_futimes(td, uap->fd, uap->tptr, UIO_USERSPACE));
3302 kern_futimes(struct thread *td, int fd, struct timeval *tptr,
3303 enum uio_seg tptrseg)
3305 struct timespec ts[2];
3310 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3312 if ((error = getvnode(td->td_proc->p_fd, fd, CAP_FUTIMES, &fp)) != 0)
3315 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
3316 AUDIT_ARG_VNODE1(fp->f_vnode);
3317 VOP_UNLOCK(fp->f_vnode, 0);
3319 error = setutimes(td, fp->f_vnode, ts, 2, tptr == NULL);
3325 * Truncate a file given its path name.
3327 #ifndef _SYS_SYSPROTO_H_
3328 struct truncate_args {
3335 sys_truncate(td, uap)
3337 register struct truncate_args /* {
3344 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3348 kern_truncate(struct thread *td, char *path, enum uio_seg pathseg, off_t length)
3354 struct nameidata nd;
3359 NDINIT(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, td);
3360 if ((error = namei(&nd)) != 0)
3363 rl_cookie = vn_rangelock_wlock(vp, 0, OFF_MAX);
3364 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0) {
3365 vn_rangelock_unlock(vp, rl_cookie);
3369 NDFREE(&nd, NDF_ONLY_PNBUF);
3370 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3371 if (vp->v_type == VDIR)
3374 else if ((error = mac_vnode_check_write(td->td_ucred, NOCRED, vp))) {
3377 else if ((error = vn_writechk(vp)) == 0 &&
3378 (error = VOP_ACCESS(vp, VWRITE, td->td_ucred, td)) == 0) {
3380 vattr.va_size = length;
3381 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3384 vn_finished_write(mp);
3385 vn_rangelock_unlock(vp, rl_cookie);
3390 #if defined(COMPAT_43)
3392 * Truncate a file given its path name.
3394 #ifndef _SYS_SYSPROTO_H_
3395 struct otruncate_args {
3403 register struct otruncate_args /* {
3408 struct truncate_args /* {
3414 nuap.path = uap->path;
3415 nuap.length = uap->length;
3416 return (sys_truncate(td, &nuap));
3418 #endif /* COMPAT_43 */
3420 /* Versions with the pad argument */
3422 freebsd6_truncate(struct thread *td, struct freebsd6_truncate_args *uap)
3424 struct truncate_args ouap;
3426 ouap.path = uap->path;
3427 ouap.length = uap->length;
3428 return (sys_truncate(td, &ouap));
3432 freebsd6_ftruncate(struct thread *td, struct freebsd6_ftruncate_args *uap)
3434 struct ftruncate_args ouap;
3437 ouap.length = uap->length;
3438 return (sys_ftruncate(td, &ouap));
3442 * Sync an open file.
3444 #ifndef _SYS_SYSPROTO_H_
3452 struct fsync_args /* {
3459 int error, lock_flags;
3461 AUDIT_ARG_FD(uap->fd);
3462 if ((error = getvnode(td->td_proc->p_fd, uap->fd, CAP_FSYNC, &fp)) != 0)
3465 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
3467 if (MNT_SHARED_WRITES(mp) ||
3468 ((mp == NULL) && MNT_SHARED_WRITES(vp->v_mount))) {
3469 lock_flags = LK_SHARED;
3471 lock_flags = LK_EXCLUSIVE;
3473 vn_lock(vp, lock_flags | LK_RETRY);
3474 AUDIT_ARG_VNODE1(vp);
3475 if (vp->v_object != NULL) {
3476 VM_OBJECT_WLOCK(vp->v_object);
3477 vm_object_page_clean(vp->v_object, 0, 0, 0);
3478 VM_OBJECT_WUNLOCK(vp->v_object);
3480 error = VOP_FSYNC(vp, MNT_WAIT, td);
3483 vn_finished_write(mp);
3490 * Rename files. Source and destination must either both be directories, or
3491 * both not be directories. If target is a directory, it must be empty.
3493 #ifndef _SYS_SYSPROTO_H_
3494 struct rename_args {
3502 register struct rename_args /* {
3508 return (kern_rename(td, uap->from, uap->to, UIO_USERSPACE));
3511 #ifndef _SYS_SYSPROTO_H_
3512 struct renameat_args {
3520 sys_renameat(struct thread *td, struct renameat_args *uap)
3523 return (kern_renameat(td, uap->oldfd, uap->old, uap->newfd, uap->new,
3528 kern_rename(struct thread *td, char *from, char *to, enum uio_seg pathseg)
3531 return (kern_renameat(td, AT_FDCWD, from, AT_FDCWD, to, pathseg));
3535 kern_renameat(struct thread *td, int oldfd, char *old, int newfd, char *new,
3536 enum uio_seg pathseg)
3538 struct mount *mp = NULL;
3539 struct vnode *tvp, *fvp, *tdvp;
3540 struct nameidata fromnd, tond;
3545 NDINIT_ATRIGHTS(&fromnd, DELETE, LOCKPARENT | LOCKLEAF | SAVESTART |
3546 AUDITVNODE1, pathseg, old, oldfd, CAP_RENAMEAT, td);
3548 NDINIT_ATRIGHTS(&fromnd, DELETE, WANTPARENT | SAVESTART | AUDITVNODE1,
3549 pathseg, old, oldfd, CAP_RENAMEAT, td);
3552 if ((error = namei(&fromnd)) != 0)
3555 error = mac_vnode_check_rename_from(td->td_ucred, fromnd.ni_dvp,
3556 fromnd.ni_vp, &fromnd.ni_cnd);
3557 VOP_UNLOCK(fromnd.ni_dvp, 0);
3558 if (fromnd.ni_dvp != fromnd.ni_vp)
3559 VOP_UNLOCK(fromnd.ni_vp, 0);
3563 error = vn_start_write(fvp, &mp, V_WAIT | PCATCH);
3565 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3566 vrele(fromnd.ni_dvp);
3570 NDINIT_ATRIGHTS(&tond, RENAME, LOCKPARENT | LOCKLEAF | NOCACHE |
3571 SAVESTART | AUDITVNODE2, pathseg, new, newfd, CAP_LINKAT, td);
3572 if (fromnd.ni_vp->v_type == VDIR)
3573 tond.ni_cnd.cn_flags |= WILLBEDIR;
3574 if ((error = namei(&tond)) != 0) {
3575 /* Translate error code for rename("dir1", "dir2/."). */
3576 if (error == EISDIR && fvp->v_type == VDIR)
3578 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3579 vrele(fromnd.ni_dvp);
3581 vn_finished_write(mp);
3587 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
3590 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
3595 if (newfd != AT_FDCWD) {
3597 * If the target already exists we require CAP_UNLINKAT
3600 error = cap_check(tond.ni_filecaps.fc_rights,
3612 * If the source is the same as the destination (that is, if they
3613 * are links to the same vnode), then there is nothing to do.
3619 error = mac_vnode_check_rename_to(td->td_ucred, tdvp,
3620 tond.ni_vp, fromnd.ni_dvp == tdvp, &tond.ni_cnd);
3624 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
3625 tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
3626 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3627 NDFREE(&tond, NDF_ONLY_PNBUF);
3629 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3630 NDFREE(&tond, NDF_ONLY_PNBUF);
3637 vrele(fromnd.ni_dvp);
3640 vrele(tond.ni_startdir);
3641 vn_finished_write(mp);
3643 if (fromnd.ni_startdir)
3644 vrele(fromnd.ni_startdir);
3651 * Make a directory file.
3653 #ifndef _SYS_SYSPROTO_H_
3662 register struct mkdir_args /* {
3668 return (kern_mkdir(td, uap->path, UIO_USERSPACE, uap->mode));
3671 #ifndef _SYS_SYSPROTO_H_
3672 struct mkdirat_args {
3679 sys_mkdirat(struct thread *td, struct mkdirat_args *uap)
3682 return (kern_mkdirat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode));
3686 kern_mkdir(struct thread *td, char *path, enum uio_seg segflg, int mode)
3689 return (kern_mkdirat(td, AT_FDCWD, path, segflg, mode));
3693 kern_mkdirat(struct thread *td, int fd, char *path, enum uio_seg segflg,
3700 struct nameidata nd;
3702 AUDIT_ARG_MODE(mode);
3705 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1,
3706 segflg, path, fd, CAP_MKDIRAT, td);
3707 nd.ni_cnd.cn_flags |= WILLBEDIR;
3708 if ((error = namei(&nd)) != 0)
3712 NDFREE(&nd, NDF_ONLY_PNBUF);
3714 * XXX namei called with LOCKPARENT but not LOCKLEAF has
3715 * the strange behaviour of leaving the vnode unlocked
3716 * if the target is the same vnode as the parent.
3718 if (vp == nd.ni_dvp)
3725 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3726 NDFREE(&nd, NDF_ONLY_PNBUF);
3728 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3733 vattr.va_type = VDIR;
3734 vattr.va_mode = (mode & ACCESSPERMS) &~ td->td_proc->p_fd->fd_cmask;
3736 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
3741 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
3745 NDFREE(&nd, NDF_ONLY_PNBUF);
3749 vn_finished_write(mp);
3754 * Remove a directory file.
3756 #ifndef _SYS_SYSPROTO_H_
3764 struct rmdir_args /* {
3769 return (kern_rmdir(td, uap->path, UIO_USERSPACE));
3773 kern_rmdir(struct thread *td, char *path, enum uio_seg pathseg)
3776 return (kern_rmdirat(td, AT_FDCWD, path, pathseg));
3780 kern_rmdirat(struct thread *td, int fd, char *path, enum uio_seg pathseg)
3785 struct nameidata nd;
3789 NDINIT_ATRIGHTS(&nd, DELETE, LOCKPARENT | LOCKLEAF | AUDITVNODE1,
3790 pathseg, path, fd, CAP_UNLINKAT, td);
3791 if ((error = namei(&nd)) != 0)
3794 if (vp->v_type != VDIR) {
3799 * No rmdir "." please.
3801 if (nd.ni_dvp == vp) {
3806 * The root of a mounted filesystem cannot be deleted.
3808 if (vp->v_vflag & VV_ROOT) {
3813 error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp,
3818 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3819 NDFREE(&nd, NDF_ONLY_PNBUF);
3821 if (nd.ni_dvp == vp)
3825 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3829 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_UNLINK);
3830 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
3831 vn_finished_write(mp);
3833 NDFREE(&nd, NDF_ONLY_PNBUF);
3835 if (nd.ni_dvp == vp)
3844 * Read a block of directory entries in a filesystem independent format.
3846 #ifndef _SYS_SYSPROTO_H_
3847 struct ogetdirentries_args {
3855 ogetdirentries(struct thread *td, struct ogetdirentries_args *uap)
3860 error = kern_ogetdirentries(td, uap, &loff);
3862 error = copyout(&loff, uap->basep, sizeof(long));
3867 kern_ogetdirentries(struct thread *td, struct ogetdirentries_args *uap,
3872 struct uio auio, kuio;
3873 struct iovec aiov, kiov;
3874 struct dirent *dp, *edp;
3876 int error, eofflag, readcnt;
3880 /* XXX arbitrary sanity limit on `count'. */
3881 if (uap->count > 64 * 1024)
3883 if ((error = getvnode(td->td_proc->p_fd, uap->fd, CAP_READ, &fp)) != 0)
3885 if ((fp->f_flag & FREAD) == 0) {
3890 foffset = foffset_lock(fp, 0);
3892 if (vp->v_type != VDIR) {
3893 foffset_unlock(fp, foffset, 0);
3897 aiov.iov_base = uap->buf;
3898 aiov.iov_len = uap->count;
3899 auio.uio_iov = &aiov;
3900 auio.uio_iovcnt = 1;
3901 auio.uio_rw = UIO_READ;
3902 auio.uio_segflg = UIO_USERSPACE;
3904 auio.uio_resid = uap->count;
3905 vn_lock(vp, LK_SHARED | LK_RETRY);
3906 loff = auio.uio_offset = foffset;
3908 error = mac_vnode_check_readdir(td->td_ucred, vp);
3911 foffset_unlock(fp, foffset, FOF_NOUPDATE);
3916 # if (BYTE_ORDER != LITTLE_ENDIAN)
3917 if (vp->v_mount->mnt_maxsymlinklen <= 0) {
3918 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag,
3920 foffset = auio.uio_offset;
3925 kuio.uio_iov = &kiov;
3926 kuio.uio_segflg = UIO_SYSSPACE;
3927 kiov.iov_len = uap->count;
3928 dirbuf = malloc(uap->count, M_TEMP, M_WAITOK);
3929 kiov.iov_base = dirbuf;
3930 error = VOP_READDIR(vp, &kuio, fp->f_cred, &eofflag,
3932 foffset = kuio.uio_offset;
3934 readcnt = uap->count - kuio.uio_resid;
3935 edp = (struct dirent *)&dirbuf[readcnt];
3936 for (dp = (struct dirent *)dirbuf; dp < edp; ) {
3937 # if (BYTE_ORDER == LITTLE_ENDIAN)
3939 * The expected low byte of
3940 * dp->d_namlen is our dp->d_type.
3941 * The high MBZ byte of dp->d_namlen
3942 * is our dp->d_namlen.
3944 dp->d_type = dp->d_namlen;
3948 * The dp->d_type is the high byte
3949 * of the expected dp->d_namlen,
3950 * so must be zero'ed.
3954 if (dp->d_reclen > 0) {
3955 dp = (struct dirent *)
3956 ((char *)dp + dp->d_reclen);
3963 error = uiomove(dirbuf, readcnt, &auio);
3965 free(dirbuf, M_TEMP);
3969 foffset_unlock(fp, foffset, 0);
3973 if (uap->count == auio.uio_resid &&
3974 (vp->v_vflag & VV_ROOT) &&
3975 (vp->v_mount->mnt_flag & MNT_UNION)) {
3976 struct vnode *tvp = vp;
3977 vp = vp->v_mount->mnt_vnodecovered;
3986 foffset_unlock(fp, foffset, 0);
3988 td->td_retval[0] = uap->count - auio.uio_resid;
3993 #endif /* COMPAT_43 */
3996 * Read a block of directory entries in a filesystem independent format.
3998 #ifndef _SYS_SYSPROTO_H_
3999 struct getdirentries_args {
4007 sys_getdirentries(td, uap)
4009 register struct getdirentries_args /* {
4019 error = kern_getdirentries(td, uap->fd, uap->buf, uap->count, &base,
4020 NULL, UIO_USERSPACE);
4023 if (uap->basep != NULL)
4024 error = copyout(&base, uap->basep, sizeof(long));
4029 kern_getdirentries(struct thread *td, int fd, char *buf, u_int count,
4030 long *basep, ssize_t *residp, enum uio_seg bufseg)
4041 if (count > IOSIZE_MAX)
4043 auio.uio_resid = count;
4044 if ((error = getvnode(td->td_proc->p_fd, fd, CAP_READ, &fp)) != 0)
4046 if ((fp->f_flag & FREAD) == 0) {
4051 foffset = foffset_lock(fp, 0);
4053 if (vp->v_type != VDIR) {
4057 aiov.iov_base = buf;
4058 aiov.iov_len = count;
4059 auio.uio_iov = &aiov;
4060 auio.uio_iovcnt = 1;
4061 auio.uio_rw = UIO_READ;
4062 auio.uio_segflg = bufseg;
4064 vn_lock(vp, LK_SHARED | LK_RETRY);
4065 AUDIT_ARG_VNODE1(vp);
4066 loff = auio.uio_offset = foffset;
4068 error = mac_vnode_check_readdir(td->td_ucred, vp);
4071 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL,
4073 foffset = auio.uio_offset;
4078 if (count == auio.uio_resid &&
4079 (vp->v_vflag & VV_ROOT) &&
4080 (vp->v_mount->mnt_flag & MNT_UNION)) {
4081 struct vnode *tvp = vp;
4082 vp = vp->v_mount->mnt_vnodecovered;
4093 *residp = auio.uio_resid;
4094 td->td_retval[0] = count - auio.uio_resid;
4096 foffset_unlock(fp, foffset, 0);
4101 #ifndef _SYS_SYSPROTO_H_
4102 struct getdents_args {
4109 sys_getdents(td, uap)
4111 register struct getdents_args /* {
4117 struct getdirentries_args ap;
4120 ap.count = uap->count;
4122 return (sys_getdirentries(td, &ap));
4126 * Set the mode mask for creation of filesystem nodes.
4128 #ifndef _SYS_SYSPROTO_H_
4136 struct umask_args /* {
4140 register struct filedesc *fdp;
4142 FILEDESC_XLOCK(td->td_proc->p_fd);
4143 fdp = td->td_proc->p_fd;
4144 td->td_retval[0] = fdp->fd_cmask;
4145 fdp->fd_cmask = uap->newmask & ALLPERMS;
4146 FILEDESC_XUNLOCK(td->td_proc->p_fd);
4151 * Void all references to file by ripping underlying filesystem away from
4154 #ifndef _SYS_SYSPROTO_H_
4155 struct revoke_args {
4162 register struct revoke_args /* {
4169 struct nameidata nd;
4171 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
4173 if ((error = namei(&nd)) != 0)
4176 NDFREE(&nd, NDF_ONLY_PNBUF);
4177 if (vp->v_type != VCHR || vp->v_rdev == NULL) {
4182 error = mac_vnode_check_revoke(td->td_ucred, vp);
4186 error = VOP_GETATTR(vp, &vattr, td->td_ucred);
4189 if (td->td_ucred->cr_uid != vattr.va_uid) {
4190 error = priv_check(td, PRIV_VFS_ADMIN);
4195 VOP_REVOKE(vp, REVOKEALL);
4202 * Convert a user file descriptor to a kernel file entry and check that, if it
4203 * is a capability, the correct rights are present. A reference on the file
4204 * entry is held upon returning.
4207 getvnode(struct filedesc *fdp, int fd, cap_rights_t rights, struct file **fpp)
4212 error = fget_unlocked(fdp, fd, rights, 0, &fp, NULL);
4217 * The file could be not of the vnode type, or it may be not
4218 * yet fully initialized, in which case the f_vnode pointer
4219 * may be set, but f_ops is still badfileops. E.g.,
4220 * devfs_open() transiently create such situation to
4221 * facilitate csw d_fdopen().
4223 * Dupfdopen() handling in kern_openat() installs the
4224 * half-baked file into the process descriptor table, allowing
4225 * other thread to dereference it. Guard against the race by
4228 if (fp->f_vnode == NULL || fp->f_ops == &badfileops) {
4229 fdrop(fp, curthread);
4238 * Get an (NFS) file handle.
4240 #ifndef _SYS_SYSPROTO_H_
4241 struct lgetfh_args {
4249 register struct lgetfh_args *uap;
4251 struct nameidata nd;
4253 register struct vnode *vp;
4256 error = priv_check(td, PRIV_VFS_GETFH);
4259 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
4264 NDFREE(&nd, NDF_ONLY_PNBUF);
4266 bzero(&fh, sizeof(fh));
4267 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
4268 error = VOP_VPTOFH(vp, &fh.fh_fid);
4272 error = copyout(&fh, uap->fhp, sizeof (fh));
4276 #ifndef _SYS_SYSPROTO_H_
4285 register struct getfh_args *uap;
4287 struct nameidata nd;
4289 register struct vnode *vp;
4292 error = priv_check(td, PRIV_VFS_GETFH);
4295 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
4300 NDFREE(&nd, NDF_ONLY_PNBUF);
4302 bzero(&fh, sizeof(fh));
4303 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
4304 error = VOP_VPTOFH(vp, &fh.fh_fid);
4308 error = copyout(&fh, uap->fhp, sizeof (fh));
4313 * syscall for the rpc.lockd to use to translate a NFS file handle into an
4316 * warning: do not remove the priv_check() call or this becomes one giant
4319 #ifndef _SYS_SYSPROTO_H_
4320 struct fhopen_args {
4321 const struct fhandle *u_fhp;
4328 struct fhopen_args /* {
4329 const struct fhandle *u_fhp;
4340 error = priv_check(td, PRIV_VFS_FHOPEN);
4344 fmode = FFLAGS(uap->flags);
4345 /* why not allow a non-read/write open for our lockd? */
4346 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
4348 error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
4351 /* find the mount point */
4352 mp = vfs_busyfs(&fhp.fh_fsid);
4355 /* now give me my vnode, it gets returned to me locked */
4356 error = VFS_FHTOVP(mp, &fhp.fh_fid, LK_EXCLUSIVE, &vp);
4361 error = falloc_noinstall(td, &fp);
4367 * An extra reference on `fp' has been held for us by
4368 * falloc_noinstall().
4374 error = vn_open_vnode(vp, fmode, td->td_ucred, td, fp);
4376 KASSERT(fp->f_ops == &badfileops,
4377 ("VOP_OPEN in fhopen() set f_ops"));
4378 KASSERT(td->td_dupfd < 0,
4379 ("fhopen() encountered fdopen()"));
4389 finit(fp, (fmode & FMASK) | (fp->f_flag & FHASLOCK), DTYPE_VNODE, vp,
4392 if (fmode & O_TRUNC) {
4393 error = fo_truncate(fp, 0, td->td_ucred, td);
4398 error = finstall(td, fp, &indx, fmode, NULL);
4401 td->td_retval[0] = indx;
4406 * Stat an (NFS) file handle.
4408 #ifndef _SYS_SYSPROTO_H_
4409 struct fhstat_args {
4410 struct fhandle *u_fhp;
4417 register struct fhstat_args /* {
4418 struct fhandle *u_fhp;
4426 error = copyin(uap->u_fhp, &fh, sizeof(fh));
4429 error = kern_fhstat(td, fh, &sb);
4432 error = copyout(&sb, uap->sb, sizeof(sb));
4437 kern_fhstat(struct thread *td, struct fhandle fh, struct stat *sb)
4443 error = priv_check(td, PRIV_VFS_FHSTAT);
4446 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4448 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_EXCLUSIVE, &vp);
4452 error = vn_stat(vp, sb, td->td_ucred, NOCRED, td);
4458 * Implement fstatfs() for (NFS) file handles.
4460 #ifndef _SYS_SYSPROTO_H_
4461 struct fhstatfs_args {
4462 struct fhandle *u_fhp;
4467 sys_fhstatfs(td, uap)
4469 struct fhstatfs_args /* {
4470 struct fhandle *u_fhp;
4478 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4481 error = kern_fhstatfs(td, fh, &sf);
4484 return (copyout(&sf, uap->buf, sizeof(sf)));
4488 kern_fhstatfs(struct thread *td, fhandle_t fh, struct statfs *buf)
4495 error = priv_check(td, PRIV_VFS_FHSTATFS);
4498 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4500 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_EXCLUSIVE, &vp);
4506 error = prison_canseemount(td->td_ucred, mp);
4510 error = mac_mount_check_stat(td->td_ucred, mp);
4515 * Set these in case the underlying filesystem fails to do so.
4518 sp->f_version = STATFS_VERSION;
4519 sp->f_namemax = NAME_MAX;
4520 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
4521 error = VFS_STATFS(mp, sp);
4530 kern_posix_fallocate(struct thread *td, int fd, off_t offset, off_t len)
4535 off_t olen, ooffset;
4539 error = fget(td, fd, CAP_WRITE, &fp);
4543 switch (fp->f_type) {
4554 if ((fp->f_flag & FWRITE) == 0) {
4559 if (vp->v_type != VREG) {
4563 if (offset < 0 || len <= 0) {
4567 /* Check for wrap. */
4568 if (offset > OFF_MAX - len) {
4573 /* Allocating blocks may take a long time, so iterate. */
4580 error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
4583 error = vn_lock(vp, LK_EXCLUSIVE);
4585 vn_finished_write(mp);
4589 error = mac_vnode_check_write(td->td_ucred, fp->f_cred, vp);
4592 error = VOP_ALLOCATE(vp, &offset, &len);
4594 vn_finished_write(mp);
4596 if (olen + ooffset != offset + len) {
4597 panic("offset + len changed from %jx/%jx to %jx/%jx",
4598 ooffset, olen, offset, len);
4600 if (error != 0 || len == 0)
4602 KASSERT(olen > len, ("Iteration did not make progress?"));
4612 sys_posix_fallocate(struct thread *td, struct posix_fallocate_args *uap)
4615 return (kern_posix_fallocate(td, uap->fd, uap->offset, uap->len));
4619 * Unlike madvise(2), we do not make a best effort to remember every
4620 * possible caching hint. Instead, we remember the last setting with
4621 * the exception that we will allow POSIX_FADV_NORMAL to adjust the
4622 * region of any current setting.
4625 kern_posix_fadvise(struct thread *td, int fd, off_t offset, off_t len,
4628 struct fadvise_info *fa, *new;
4634 if (offset < 0 || len < 0 || offset > OFF_MAX - len)
4637 case POSIX_FADV_SEQUENTIAL:
4638 case POSIX_FADV_RANDOM:
4639 case POSIX_FADV_NOREUSE:
4640 new = malloc(sizeof(*fa), M_FADVISE, M_WAITOK);
4642 case POSIX_FADV_NORMAL:
4643 case POSIX_FADV_WILLNEED:
4644 case POSIX_FADV_DONTNEED:
4650 /* XXX: CAP_POSIX_FADVISE? */
4651 error = fget(td, fd, CAP_NONE, &fp);
4655 switch (fp->f_type) {
4667 if (vp->v_type != VREG) {
4674 end = offset + len - 1;
4676 case POSIX_FADV_SEQUENTIAL:
4677 case POSIX_FADV_RANDOM:
4678 case POSIX_FADV_NOREUSE:
4680 * Try to merge any existing non-standard region with
4681 * this new region if possible, otherwise create a new
4682 * non-standard region for this request.
4684 mtx_pool_lock(mtxpool_sleep, fp);
4686 if (fa != NULL && fa->fa_advice == advice &&
4687 ((fa->fa_start <= end && fa->fa_end >= offset) ||
4688 (end != OFF_MAX && fa->fa_start == end + 1) ||
4689 (fa->fa_end != OFF_MAX && fa->fa_end + 1 == offset))) {
4690 if (offset < fa->fa_start)
4691 fa->fa_start = offset;
4692 if (end > fa->fa_end)
4695 new->fa_advice = advice;
4696 new->fa_start = offset;
4698 new->fa_prevstart = 0;
4699 new->fa_prevend = 0;
4703 mtx_pool_unlock(mtxpool_sleep, fp);
4705 case POSIX_FADV_NORMAL:
4707 * If a the "normal" region overlaps with an existing
4708 * non-standard region, trim or remove the
4709 * non-standard region.
4711 mtx_pool_lock(mtxpool_sleep, fp);
4714 if (offset <= fa->fa_start && end >= fa->fa_end) {
4716 fp->f_advice = NULL;
4717 } else if (offset <= fa->fa_start &&
4718 end >= fa->fa_start)
4719 fa->fa_start = end + 1;
4720 else if (offset <= fa->fa_end && end >= fa->fa_end)
4721 fa->fa_end = offset - 1;
4722 else if (offset >= fa->fa_start && end <= fa->fa_end) {
4724 * If the "normal" region is a middle
4725 * portion of the existing
4726 * non-standard region, just remove
4727 * the whole thing rather than picking
4728 * one side or the other to
4732 fp->f_advice = NULL;
4735 mtx_pool_unlock(mtxpool_sleep, fp);
4737 case POSIX_FADV_WILLNEED:
4738 case POSIX_FADV_DONTNEED:
4739 error = VOP_ADVISE(vp, offset, end, advice);
4745 free(new, M_FADVISE);
4750 sys_posix_fadvise(struct thread *td, struct posix_fadvise_args *uap)
4753 return (kern_posix_fadvise(td, uap->fd, uap->offset, uap->len,