2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
4 * (c) UNIX System Laboratories, Inc.
5 * All or some portions of this file are derived from material licensed
6 * to the University of California by American Telephone and Telegraph
7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 * the permission of UNIX System Laboratories, Inc.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 4. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * @(#)vfs_syscalls.c 8.13 (Berkeley) 4/15/94
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
40 #include "opt_capsicum.h"
41 #include "opt_compat.h"
42 #include "opt_kdtrace.h"
43 #include "opt_ktrace.h"
45 #include <sys/param.h>
46 #include <sys/systm.h>
49 #include <sys/capability.h>
51 #include <sys/sysent.h>
52 #include <sys/malloc.h>
53 #include <sys/mount.h>
54 #include <sys/mutex.h>
55 #include <sys/sysproto.h>
56 #include <sys/namei.h>
57 #include <sys/filedesc.h>
58 #include <sys/kernel.h>
59 #include <sys/fcntl.h>
61 #include <sys/filio.h>
62 #include <sys/limits.h>
63 #include <sys/linker.h>
64 #include <sys/rwlock.h>
68 #include <sys/unistd.h>
69 #include <sys/vnode.h>
72 #include <sys/dirent.h>
74 #include <sys/syscallsubr.h>
75 #include <sys/sysctl.h>
77 #include <sys/ktrace.h>
80 #include <machine/stdarg.h>
82 #include <security/audit/audit.h>
83 #include <security/mac/mac_framework.h>
86 #include <vm/vm_object.h>
87 #include <vm/vm_page.h>
90 #include <ufs/ufs/quota.h>
92 MALLOC_DEFINE(M_FADVISE, "fadvise", "posix_fadvise(2) information");
94 SDT_PROVIDER_DEFINE(vfs);
95 SDT_PROBE_DEFINE2(vfs, , stat, mode, "char *", "int");
96 SDT_PROBE_DEFINE2(vfs, , stat, reg, "char *", "int");
98 static int chroot_refuse_vdir_fds(struct filedesc *fdp);
99 static int getutimes(const struct timeval *, enum uio_seg, struct timespec *);
100 static int kern_chflags(struct thread *td, const char *path,
101 enum uio_seg pathseg, u_long flags);
102 static int kern_chflagsat(struct thread *td, int fd, const char *path,
103 enum uio_seg pathseg, u_long flags, int atflag);
104 static int setfflags(struct thread *td, struct vnode *, u_long);
105 static int setutimes(struct thread *td, struct vnode *,
106 const struct timespec *, int, int);
107 static int vn_access(struct vnode *vp, int user_flags, struct ucred *cred,
111 * The module initialization routine for POSIX asynchronous I/O will
112 * set this to the version of AIO that it implements. (Zero means
113 * that it is not implemented.) This value is used here by pathconf()
114 * and in kern_descrip.c by fpathconf().
116 int async_io_version;
119 static int syncprt = 0;
120 SYSCTL_INT(_debug, OID_AUTO, syncprt, CTLFLAG_RW, &syncprt, 0, "");
124 * Sync each mounted filesystem.
126 #ifndef _SYS_SYSPROTO_H_
135 struct sync_args *uap;
137 struct mount *mp, *nmp;
140 mtx_lock(&mountlist_mtx);
141 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
142 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK)) {
143 nmp = TAILQ_NEXT(mp, mnt_list);
146 if ((mp->mnt_flag & MNT_RDONLY) == 0 &&
147 vn_start_write(NULL, &mp, V_NOWAIT) == 0) {
148 save = curthread_pflags_set(TDP_SYNCIO);
149 vfs_msync(mp, MNT_NOWAIT);
150 VFS_SYNC(mp, MNT_NOWAIT);
151 curthread_pflags_restore(save);
152 vn_finished_write(mp);
154 mtx_lock(&mountlist_mtx);
155 nmp = TAILQ_NEXT(mp, mnt_list);
158 mtx_unlock(&mountlist_mtx);
163 * Change filesystem quotas.
165 #ifndef _SYS_SYSPROTO_H_
166 struct quotactl_args {
174 sys_quotactl(td, uap)
176 register struct quotactl_args /* {
187 AUDIT_ARG_CMD(uap->cmd);
188 AUDIT_ARG_UID(uap->uid);
189 if (!prison_allow(td->td_ucred, PR_ALLOW_QUOTAS))
191 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
193 if ((error = namei(&nd)) != 0)
195 NDFREE(&nd, NDF_ONLY_PNBUF);
196 mp = nd.ni_vp->v_mount;
199 error = vfs_busy(mp, 0);
203 error = VFS_QUOTACTL(mp, uap->cmd, uap->uid, uap->arg);
206 * Since quota on operation typically needs to open quota
207 * file, the Q_QUOTAON handler needs to unbusy the mount point
208 * before calling into namei. Otherwise, unmount might be
209 * started between two vfs_busy() invocations (first is our,
210 * second is from mount point cross-walk code in lookup()),
213 * Require that Q_QUOTAON handles the vfs_busy() reference on
214 * its own, always returning with ubusied mount point.
216 if ((uap->cmd >> SUBCMDSHIFT) != Q_QUOTAON)
222 * Used by statfs conversion routines to scale the block size up if
223 * necessary so that all of the block counts are <= 'max_size'. Note
224 * that 'max_size' should be a bitmask, i.e. 2^n - 1 for some non-zero
228 statfs_scale_blocks(struct statfs *sf, long max_size)
233 KASSERT(powerof2(max_size + 1), ("%s: invalid max_size", __func__));
236 * Attempt to scale the block counts to give a more accurate
237 * overview to userland of the ratio of free space to used
238 * space. To do this, find the largest block count and compute
239 * a divisor that lets it fit into a signed integer <= max_size.
241 if (sf->f_bavail < 0)
242 count = -sf->f_bavail;
244 count = sf->f_bavail;
245 count = MAX(sf->f_blocks, MAX(sf->f_bfree, count));
246 if (count <= max_size)
249 count >>= flsl(max_size);
256 sf->f_bsize <<= shift;
257 sf->f_blocks >>= shift;
258 sf->f_bfree >>= shift;
259 sf->f_bavail >>= shift;
263 * Get filesystem statistics.
265 #ifndef _SYS_SYSPROTO_H_
274 register struct statfs_args /* {
282 error = kern_statfs(td, uap->path, UIO_USERSPACE, &sf);
284 error = copyout(&sf, uap->buf, sizeof(sf));
289 kern_statfs(struct thread *td, char *path, enum uio_seg pathseg,
293 struct statfs *sp, sb;
297 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
302 mp = nd.ni_vp->v_mount;
304 NDFREE(&nd, NDF_ONLY_PNBUF);
306 error = vfs_busy(mp, 0);
311 error = mac_mount_check_stat(td->td_ucred, mp);
316 * Set these in case the underlying filesystem fails to do so.
319 sp->f_version = STATFS_VERSION;
320 sp->f_namemax = NAME_MAX;
321 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
322 error = VFS_STATFS(mp, sp);
325 if (priv_check(td, PRIV_VFS_GENERATION)) {
326 bcopy(sp, &sb, sizeof(sb));
327 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
328 prison_enforce_statfs(td->td_ucred, mp, &sb);
338 * Get filesystem statistics.
340 #ifndef _SYS_SYSPROTO_H_
341 struct fstatfs_args {
349 register struct fstatfs_args /* {
357 error = kern_fstatfs(td, uap->fd, &sf);
359 error = copyout(&sf, uap->buf, sizeof(sf));
364 kern_fstatfs(struct thread *td, int fd, struct statfs *buf)
368 struct statfs *sp, sb;
374 error = getvnode(td->td_proc->p_fd, fd,
375 cap_rights_init(&rights, CAP_FSTATFS), &fp);
379 vn_lock(vp, LK_SHARED | LK_RETRY);
381 AUDIT_ARG_VNODE1(vp);
392 error = vfs_busy(mp, 0);
397 error = mac_mount_check_stat(td->td_ucred, mp);
402 * Set these in case the underlying filesystem fails to do so.
405 sp->f_version = STATFS_VERSION;
406 sp->f_namemax = NAME_MAX;
407 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
408 error = VFS_STATFS(mp, sp);
411 if (priv_check(td, PRIV_VFS_GENERATION)) {
412 bcopy(sp, &sb, sizeof(sb));
413 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
414 prison_enforce_statfs(td->td_ucred, mp, &sb);
425 * Get statistics on all filesystems.
427 #ifndef _SYS_SYSPROTO_H_
428 struct getfsstat_args {
435 sys_getfsstat(td, uap)
437 register struct getfsstat_args /* {
444 return (kern_getfsstat(td, &uap->buf, uap->bufsize, UIO_USERSPACE,
449 * If (bufsize > 0 && bufseg == UIO_SYSSPACE)
450 * The caller is responsible for freeing memory which will be allocated
454 kern_getfsstat(struct thread *td, struct statfs **buf, size_t bufsize,
455 enum uio_seg bufseg, int flags)
457 struct mount *mp, *nmp;
458 struct statfs *sfsp, *sp, sb;
459 size_t count, maxcount;
462 maxcount = bufsize / sizeof(struct statfs);
465 else if (bufseg == UIO_USERSPACE)
467 else /* if (bufseg == UIO_SYSSPACE) */ {
469 mtx_lock(&mountlist_mtx);
470 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
473 mtx_unlock(&mountlist_mtx);
474 if (maxcount > count)
476 sfsp = *buf = malloc(maxcount * sizeof(struct statfs), M_TEMP,
480 mtx_lock(&mountlist_mtx);
481 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
482 if (prison_canseemount(td->td_ucred, mp) != 0) {
483 nmp = TAILQ_NEXT(mp, mnt_list);
487 if (mac_mount_check_stat(td->td_ucred, mp) != 0) {
488 nmp = TAILQ_NEXT(mp, mnt_list);
492 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK)) {
493 nmp = TAILQ_NEXT(mp, mnt_list);
496 if (sfsp && count < maxcount) {
499 * Set these in case the underlying filesystem
502 sp->f_version = STATFS_VERSION;
503 sp->f_namemax = NAME_MAX;
504 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
506 * If MNT_NOWAIT or MNT_LAZY is specified, do not
507 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
508 * overrides MNT_WAIT.
510 if (((flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
511 (flags & MNT_WAIT)) &&
512 (error = VFS_STATFS(mp, sp))) {
513 mtx_lock(&mountlist_mtx);
514 nmp = TAILQ_NEXT(mp, mnt_list);
518 if (priv_check(td, PRIV_VFS_GENERATION)) {
519 bcopy(sp, &sb, sizeof(sb));
520 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
521 prison_enforce_statfs(td->td_ucred, mp, &sb);
524 if (bufseg == UIO_SYSSPACE)
525 bcopy(sp, sfsp, sizeof(*sp));
526 else /* if (bufseg == UIO_USERSPACE) */ {
527 error = copyout(sp, sfsp, sizeof(*sp));
536 mtx_lock(&mountlist_mtx);
537 nmp = TAILQ_NEXT(mp, mnt_list);
540 mtx_unlock(&mountlist_mtx);
541 if (sfsp && count > maxcount)
542 td->td_retval[0] = maxcount;
544 td->td_retval[0] = count;
548 #ifdef COMPAT_FREEBSD4
550 * Get old format filesystem statistics.
552 static void cvtstatfs(struct statfs *, struct ostatfs *);
554 #ifndef _SYS_SYSPROTO_H_
555 struct freebsd4_statfs_args {
561 freebsd4_statfs(td, uap)
563 struct freebsd4_statfs_args /* {
572 error = kern_statfs(td, uap->path, UIO_USERSPACE, &sf);
575 cvtstatfs(&sf, &osb);
576 return (copyout(&osb, uap->buf, sizeof(osb)));
580 * Get filesystem statistics.
582 #ifndef _SYS_SYSPROTO_H_
583 struct freebsd4_fstatfs_args {
589 freebsd4_fstatfs(td, uap)
591 struct freebsd4_fstatfs_args /* {
600 error = kern_fstatfs(td, uap->fd, &sf);
603 cvtstatfs(&sf, &osb);
604 return (copyout(&osb, uap->buf, sizeof(osb)));
608 * Get statistics on all filesystems.
610 #ifndef _SYS_SYSPROTO_H_
611 struct freebsd4_getfsstat_args {
618 freebsd4_getfsstat(td, uap)
620 register struct freebsd4_getfsstat_args /* {
626 struct statfs *buf, *sp;
631 count = uap->bufsize / sizeof(struct ostatfs);
632 size = count * sizeof(struct statfs);
633 error = kern_getfsstat(td, &buf, size, UIO_SYSSPACE, uap->flags);
635 count = td->td_retval[0];
637 while (count > 0 && error == 0) {
639 error = copyout(&osb, uap->buf, sizeof(osb));
650 * Implement fstatfs() for (NFS) file handles.
652 #ifndef _SYS_SYSPROTO_H_
653 struct freebsd4_fhstatfs_args {
654 struct fhandle *u_fhp;
659 freebsd4_fhstatfs(td, uap)
661 struct freebsd4_fhstatfs_args /* {
662 struct fhandle *u_fhp;
671 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
674 error = kern_fhstatfs(td, fh, &sf);
677 cvtstatfs(&sf, &osb);
678 return (copyout(&osb, uap->buf, sizeof(osb)));
682 * Convert a new format statfs structure to an old format statfs structure.
690 statfs_scale_blocks(nsp, LONG_MAX);
691 bzero(osp, sizeof(*osp));
692 osp->f_bsize = nsp->f_bsize;
693 osp->f_iosize = MIN(nsp->f_iosize, LONG_MAX);
694 osp->f_blocks = nsp->f_blocks;
695 osp->f_bfree = nsp->f_bfree;
696 osp->f_bavail = nsp->f_bavail;
697 osp->f_files = MIN(nsp->f_files, LONG_MAX);
698 osp->f_ffree = MIN(nsp->f_ffree, LONG_MAX);
699 osp->f_owner = nsp->f_owner;
700 osp->f_type = nsp->f_type;
701 osp->f_flags = nsp->f_flags;
702 osp->f_syncwrites = MIN(nsp->f_syncwrites, LONG_MAX);
703 osp->f_asyncwrites = MIN(nsp->f_asyncwrites, LONG_MAX);
704 osp->f_syncreads = MIN(nsp->f_syncreads, LONG_MAX);
705 osp->f_asyncreads = MIN(nsp->f_asyncreads, LONG_MAX);
706 strlcpy(osp->f_fstypename, nsp->f_fstypename,
707 MIN(MFSNAMELEN, OMFSNAMELEN));
708 strlcpy(osp->f_mntonname, nsp->f_mntonname,
709 MIN(MNAMELEN, OMNAMELEN));
710 strlcpy(osp->f_mntfromname, nsp->f_mntfromname,
711 MIN(MNAMELEN, OMNAMELEN));
712 osp->f_fsid = nsp->f_fsid;
714 #endif /* COMPAT_FREEBSD4 */
717 * Change current working directory to a given file descriptor.
719 #ifndef _SYS_SYSPROTO_H_
727 struct fchdir_args /* {
731 register struct filedesc *fdp = td->td_proc->p_fd;
732 struct vnode *vp, *tdp, *vpold;
738 AUDIT_ARG_FD(uap->fd);
739 error = getvnode(fdp, uap->fd, cap_rights_init(&rights, CAP_FCHDIR),
746 vn_lock(vp, LK_SHARED | LK_RETRY);
747 AUDIT_ARG_VNODE1(vp);
748 error = change_dir(vp, td);
749 while (!error && (mp = vp->v_mountedhere) != NULL) {
752 error = VFS_ROOT(mp, LK_SHARED, &tdp);
765 vpold = fdp->fd_cdir;
767 FILEDESC_XUNLOCK(fdp);
773 * Change current working directory (``.'').
775 #ifndef _SYS_SYSPROTO_H_
783 struct chdir_args /* {
788 return (kern_chdir(td, uap->path, UIO_USERSPACE));
792 kern_chdir(struct thread *td, char *path, enum uio_seg pathseg)
794 register struct filedesc *fdp = td->td_proc->p_fd;
799 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
801 if ((error = namei(&nd)) != 0)
803 if ((error = change_dir(nd.ni_vp, td)) != 0) {
805 NDFREE(&nd, NDF_ONLY_PNBUF);
808 VOP_UNLOCK(nd.ni_vp, 0);
809 NDFREE(&nd, NDF_ONLY_PNBUF);
812 fdp->fd_cdir = nd.ni_vp;
813 FILEDESC_XUNLOCK(fdp);
819 * Helper function for raised chroot(2) security function: Refuse if
820 * any filedescriptors are open directories.
823 chroot_refuse_vdir_fds(fdp)
824 struct filedesc *fdp;
830 FILEDESC_LOCK_ASSERT(fdp);
832 for (fd = 0; fd < fdp->fd_nfiles ; fd++) {
833 fp = fget_locked(fdp, fd);
836 if (fp->f_type == DTYPE_VNODE) {
838 if (vp->v_type == VDIR)
846 * This sysctl determines if we will allow a process to chroot(2) if it
847 * has a directory open:
848 * 0: disallowed for all processes.
849 * 1: allowed for processes that were not already chroot(2)'ed.
850 * 2: allowed for all processes.
853 static int chroot_allow_open_directories = 1;
855 SYSCTL_INT(_kern, OID_AUTO, chroot_allow_open_directories, CTLFLAG_RW,
856 &chroot_allow_open_directories, 0,
857 "Allow a process to chroot(2) if it has a directory open");
860 * Change notion of root (``/'') directory.
862 #ifndef _SYS_SYSPROTO_H_
870 struct chroot_args /* {
877 error = priv_check(td, PRIV_VFS_CHROOT);
880 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
881 UIO_USERSPACE, uap->path, td);
885 error = change_dir(nd.ni_vp, td);
889 error = mac_vnode_check_chroot(td->td_ucred, nd.ni_vp);
893 VOP_UNLOCK(nd.ni_vp, 0);
894 error = change_root(nd.ni_vp, td);
896 NDFREE(&nd, NDF_ONLY_PNBUF);
901 NDFREE(&nd, NDF_ONLY_PNBUF);
906 * Common routine for chroot and chdir. Callers must provide a locked vnode
918 ASSERT_VOP_LOCKED(vp, "change_dir(): vp not locked");
919 if (vp->v_type != VDIR)
922 error = mac_vnode_check_chdir(td->td_ucred, vp);
926 return (VOP_ACCESS(vp, VEXEC, td->td_ucred, td));
930 * Common routine for kern_chroot() and jail_attach(). The caller is
931 * responsible for invoking priv_check() and mac_vnode_check_chroot() to
932 * authorize this operation.
939 struct filedesc *fdp;
943 fdp = td->td_proc->p_fd;
945 if (chroot_allow_open_directories == 0 ||
946 (chroot_allow_open_directories == 1 && fdp->fd_rdir != rootvnode)) {
947 error = chroot_refuse_vdir_fds(fdp);
949 FILEDESC_XUNLOCK(fdp);
953 oldvp = fdp->fd_rdir;
960 FILEDESC_XUNLOCK(fdp);
966 flags_to_rights(int flags, cap_rights_t *rightsp)
969 if (flags & O_EXEC) {
970 cap_rights_set(rightsp, CAP_FEXECVE);
972 switch ((flags & O_ACCMODE)) {
974 cap_rights_set(rightsp, CAP_READ);
977 cap_rights_set(rightsp, CAP_READ);
980 cap_rights_set(rightsp, CAP_WRITE);
981 if (!(flags & (O_APPEND | O_TRUNC)))
982 cap_rights_set(rightsp, CAP_SEEK);
988 cap_rights_set(rightsp, CAP_CREATE);
991 cap_rights_set(rightsp, CAP_FTRUNCATE);
993 if (flags & (O_SYNC | O_FSYNC))
994 cap_rights_set(rightsp, CAP_FSYNC);
996 if (flags & (O_EXLOCK | O_SHLOCK))
997 cap_rights_set(rightsp, CAP_FLOCK);
1001 * Check permissions, allocate an open file structure, and call the device
1002 * open routine if any.
1004 #ifndef _SYS_SYSPROTO_H_
1014 register struct open_args /* {
1021 return (kern_open(td, uap->path, UIO_USERSPACE, uap->flags, uap->mode));
1024 #ifndef _SYS_SYSPROTO_H_
1025 struct openat_args {
1033 sys_openat(struct thread *td, struct openat_args *uap)
1036 return (kern_openat(td, uap->fd, uap->path, UIO_USERSPACE, uap->flag,
1041 kern_open(struct thread *td, char *path, enum uio_seg pathseg, int flags,
1045 return (kern_openat(td, AT_FDCWD, path, pathseg, flags, mode));
1049 kern_openat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1050 int flags, int mode)
1052 struct proc *p = td->td_proc;
1053 struct filedesc *fdp = p->p_fd;
1056 struct nameidata nd;
1057 cap_rights_t rights;
1058 int cmode, error, indx;
1062 AUDIT_ARG_FFLAGS(flags);
1063 AUDIT_ARG_MODE(mode);
1064 /* XXX: audit dirfd */
1065 cap_rights_init(&rights, CAP_LOOKUP);
1066 flags_to_rights(flags, &rights);
1068 * Only one of the O_EXEC, O_RDONLY, O_WRONLY and O_RDWR flags
1071 if (flags & O_EXEC) {
1072 if (flags & O_ACCMODE)
1074 } else if ((flags & O_ACCMODE) == O_ACCMODE) {
1077 flags = FFLAGS(flags);
1081 * Allocate the file descriptor, but don't install a descriptor yet.
1083 error = falloc_noinstall(td, &fp);
1087 * An extra reference on `fp' has been held for us by
1088 * falloc_noinstall().
1090 /* Set the flags early so the finit in devfs can pick them up. */
1091 fp->f_flag = flags & FMASK;
1092 cmode = ((mode & ~fdp->fd_cmask) & ALLPERMS) & ~S_ISTXT;
1093 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, fd,
1095 td->td_dupfd = -1; /* XXX check for fdopen */
1096 error = vn_open(&nd, &flags, cmode, fp);
1099 * If the vn_open replaced the method vector, something
1100 * wonderous happened deep below and we just pass it up
1101 * pretending we know what we do.
1103 if (error == ENXIO && fp->f_ops != &badfileops)
1107 * Handle special fdopen() case. bleh.
1109 * Don't do this for relative (capability) lookups; we don't
1110 * understand exactly what would happen, and we don't think
1111 * that it ever should.
1113 if (nd.ni_strictrelative == 0 &&
1114 (error == ENODEV || error == ENXIO) &&
1115 td->td_dupfd >= 0) {
1116 error = dupfdopen(td, fdp, td->td_dupfd, flags, error,
1125 NDFREE(&nd, NDF_ONLY_PNBUF);
1129 * Store the vnode, for any f_type. Typically, the vnode use
1130 * count is decremented by direct call to vn_closefile() for
1131 * files that switched type in the cdevsw fdopen() method.
1135 * If the file wasn't claimed by devfs bind it to the normal
1136 * vnode operations here.
1138 if (fp->f_ops == &badfileops) {
1139 KASSERT(vp->v_type != VFIFO, ("Unexpected fifo."));
1141 finit(fp, (flags & FMASK) | (fp->f_flag & FHASLOCK),
1142 DTYPE_VNODE, vp, &vnops);
1146 if (flags & O_TRUNC) {
1147 error = fo_truncate(fp, 0, td->td_ucred, td);
1153 * If we haven't already installed the FD (for dupfdopen), do so now.
1156 struct filecaps *fcaps;
1159 if (nd.ni_strictrelative == 1)
1160 fcaps = &nd.ni_filecaps;
1164 error = finstall(td, fp, &indx, flags, fcaps);
1165 /* On success finstall() consumes fcaps. */
1167 filecaps_free(&nd.ni_filecaps);
1171 filecaps_free(&nd.ni_filecaps);
1175 * Release our private reference, leaving the one associated with
1176 * the descriptor table intact.
1179 td->td_retval[0] = indx;
1182 KASSERT(indx == -1, ("indx=%d, should be -1", indx));
1191 #ifndef _SYS_SYSPROTO_H_
1192 struct ocreat_args {
1200 register struct ocreat_args /* {
1206 return (kern_open(td, uap->path, UIO_USERSPACE,
1207 O_WRONLY | O_CREAT | O_TRUNC, uap->mode));
1209 #endif /* COMPAT_43 */
1212 * Create a special file.
1214 #ifndef _SYS_SYSPROTO_H_
1224 register struct mknod_args /* {
1231 return (kern_mknod(td, uap->path, UIO_USERSPACE, uap->mode, uap->dev));
1234 #ifndef _SYS_SYSPROTO_H_
1235 struct mknodat_args {
1243 sys_mknodat(struct thread *td, struct mknodat_args *uap)
1246 return (kern_mknodat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode,
1251 kern_mknod(struct thread *td, char *path, enum uio_seg pathseg, int mode,
1255 return (kern_mknodat(td, AT_FDCWD, path, pathseg, mode, dev));
1259 kern_mknodat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1265 struct nameidata nd;
1266 cap_rights_t rights;
1267 int error, whiteout = 0;
1269 AUDIT_ARG_MODE(mode);
1271 switch (mode & S_IFMT) {
1274 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
1277 error = priv_check(td, PRIV_VFS_MKNOD_BAD);
1280 error = priv_check(td, PRIV_VFS_MKNOD_WHT);
1284 return (kern_mkfifoat(td, fd, path, pathseg, mode));
1294 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1,
1295 pathseg, path, fd, cap_rights_init(&rights, CAP_MKNODAT), td);
1296 if ((error = namei(&nd)) != 0)
1300 NDFREE(&nd, NDF_ONLY_PNBUF);
1301 if (vp == nd.ni_dvp)
1309 vattr.va_mode = (mode & ALLPERMS) &
1310 ~td->td_proc->p_fd->fd_cmask;
1311 vattr.va_rdev = dev;
1314 switch (mode & S_IFMT) {
1315 case S_IFMT: /* used by badsect to flag bad sectors */
1316 vattr.va_type = VBAD;
1319 vattr.va_type = VCHR;
1322 vattr.va_type = VBLK;
1328 panic("kern_mknod: invalid mode");
1331 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1332 NDFREE(&nd, NDF_ONLY_PNBUF);
1334 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1339 if (error == 0 && !whiteout)
1340 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp,
1341 &nd.ni_cnd, &vattr);
1345 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, CREATE);
1347 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp,
1348 &nd.ni_cnd, &vattr);
1353 NDFREE(&nd, NDF_ONLY_PNBUF);
1355 vn_finished_write(mp);
1360 * Create a named pipe.
1362 #ifndef _SYS_SYSPROTO_H_
1363 struct mkfifo_args {
1371 register struct mkfifo_args /* {
1377 return (kern_mkfifo(td, uap->path, UIO_USERSPACE, uap->mode));
1380 #ifndef _SYS_SYSPROTO_H_
1381 struct mkfifoat_args {
1388 sys_mkfifoat(struct thread *td, struct mkfifoat_args *uap)
1391 return (kern_mkfifoat(td, uap->fd, uap->path, UIO_USERSPACE,
1396 kern_mkfifo(struct thread *td, char *path, enum uio_seg pathseg, int mode)
1399 return (kern_mkfifoat(td, AT_FDCWD, path, pathseg, mode));
1403 kern_mkfifoat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1408 struct nameidata nd;
1409 cap_rights_t rights;
1412 AUDIT_ARG_MODE(mode);
1415 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1,
1416 pathseg, path, fd, cap_rights_init(&rights, CAP_MKFIFOAT), td);
1417 if ((error = namei(&nd)) != 0)
1419 if (nd.ni_vp != NULL) {
1420 NDFREE(&nd, NDF_ONLY_PNBUF);
1421 if (nd.ni_vp == nd.ni_dvp)
1428 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1429 NDFREE(&nd, NDF_ONLY_PNBUF);
1431 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1436 vattr.va_type = VFIFO;
1437 vattr.va_mode = (mode & ALLPERMS) & ~td->td_proc->p_fd->fd_cmask;
1439 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1444 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
1451 vn_finished_write(mp);
1452 NDFREE(&nd, NDF_ONLY_PNBUF);
1457 * Make a hard file link.
1459 #ifndef _SYS_SYSPROTO_H_
1468 register struct link_args /* {
1474 return (kern_link(td, uap->path, uap->link, UIO_USERSPACE));
1477 #ifndef _SYS_SYSPROTO_H_
1478 struct linkat_args {
1487 sys_linkat(struct thread *td, struct linkat_args *uap)
1492 if (flag & ~AT_SYMLINK_FOLLOW)
1495 return (kern_linkat(td, uap->fd1, uap->fd2, uap->path1, uap->path2,
1496 UIO_USERSPACE, (flag & AT_SYMLINK_FOLLOW) ? FOLLOW : NOFOLLOW));
1499 int hardlink_check_uid = 0;
1500 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
1501 &hardlink_check_uid, 0,
1502 "Unprivileged processes cannot create hard links to files owned by other "
1504 static int hardlink_check_gid = 0;
1505 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
1506 &hardlink_check_gid, 0,
1507 "Unprivileged processes cannot create hard links to files owned by other "
1511 can_hardlink(struct vnode *vp, struct ucred *cred)
1516 if (!hardlink_check_uid && !hardlink_check_gid)
1519 error = VOP_GETATTR(vp, &va, cred);
1523 if (hardlink_check_uid && cred->cr_uid != va.va_uid) {
1524 error = priv_check_cred(cred, PRIV_VFS_LINK, 0);
1529 if (hardlink_check_gid && !groupmember(va.va_gid, cred)) {
1530 error = priv_check_cred(cred, PRIV_VFS_LINK, 0);
1539 kern_link(struct thread *td, char *path, char *link, enum uio_seg segflg)
1542 return (kern_linkat(td, AT_FDCWD, AT_FDCWD, path,link, segflg, FOLLOW));
1546 kern_linkat(struct thread *td, int fd1, int fd2, char *path1, char *path2,
1547 enum uio_seg segflg, int follow)
1551 struct nameidata nd;
1552 cap_rights_t rights;
1556 NDINIT_AT(&nd, LOOKUP, follow | AUDITVNODE1, segflg, path1, fd1, td);
1558 if ((error = namei(&nd)) != 0)
1560 NDFREE(&nd, NDF_ONLY_PNBUF);
1562 if (vp->v_type == VDIR) {
1564 return (EPERM); /* POSIX */
1566 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0) {
1570 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE2,
1571 segflg, path2, fd2, cap_rights_init(&rights, CAP_LINKAT), td);
1572 if ((error = namei(&nd)) == 0) {
1573 if (nd.ni_vp != NULL) {
1574 if (nd.ni_dvp == nd.ni_vp)
1580 } else if ((error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY))
1582 error = can_hardlink(vp, td->td_ucred);
1585 error = mac_vnode_check_link(td->td_ucred,
1586 nd.ni_dvp, vp, &nd.ni_cnd);
1589 error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
1593 NDFREE(&nd, NDF_ONLY_PNBUF);
1596 vn_finished_write(mp);
1601 * Make a symbolic link.
1603 #ifndef _SYS_SYSPROTO_H_
1604 struct symlink_args {
1610 sys_symlink(td, uap)
1612 register struct symlink_args /* {
1618 return (kern_symlink(td, uap->path, uap->link, UIO_USERSPACE));
1621 #ifndef _SYS_SYSPROTO_H_
1622 struct symlinkat_args {
1629 sys_symlinkat(struct thread *td, struct symlinkat_args *uap)
1632 return (kern_symlinkat(td, uap->path1, uap->fd, uap->path2,
1637 kern_symlink(struct thread *td, char *path, char *link, enum uio_seg segflg)
1640 return (kern_symlinkat(td, path, AT_FDCWD, link, segflg));
1644 kern_symlinkat(struct thread *td, char *path1, int fd, char *path2,
1645 enum uio_seg segflg)
1650 struct nameidata nd;
1652 cap_rights_t rights;
1654 if (segflg == UIO_SYSSPACE) {
1657 syspath = uma_zalloc(namei_zone, M_WAITOK);
1658 if ((error = copyinstr(path1, syspath, MAXPATHLEN, NULL)) != 0)
1661 AUDIT_ARG_TEXT(syspath);
1664 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1,
1665 segflg, path2, fd, cap_rights_init(&rights, CAP_SYMLINKAT), td);
1666 if ((error = namei(&nd)) != 0)
1669 NDFREE(&nd, NDF_ONLY_PNBUF);
1670 if (nd.ni_vp == nd.ni_dvp)
1678 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1679 NDFREE(&nd, NDF_ONLY_PNBUF);
1681 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1686 vattr.va_mode = ACCESSPERMS &~ td->td_proc->p_fd->fd_cmask;
1688 vattr.va_type = VLNK;
1689 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1694 error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr, syspath);
1700 NDFREE(&nd, NDF_ONLY_PNBUF);
1702 vn_finished_write(mp);
1704 if (segflg != UIO_SYSSPACE)
1705 uma_zfree(namei_zone, syspath);
1710 * Delete a whiteout from the filesystem.
1713 sys_undelete(td, uap)
1715 register struct undelete_args /* {
1720 struct nameidata nd;
1725 NDINIT(&nd, DELETE, LOCKPARENT | DOWHITEOUT | AUDITVNODE1,
1726 UIO_USERSPACE, uap->path, td);
1731 if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & ISWHITEOUT)) {
1732 NDFREE(&nd, NDF_ONLY_PNBUF);
1733 if (nd.ni_vp == nd.ni_dvp)
1741 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1742 NDFREE(&nd, NDF_ONLY_PNBUF);
1744 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1748 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, DELETE);
1749 NDFREE(&nd, NDF_ONLY_PNBUF);
1751 vn_finished_write(mp);
1756 * Delete a name from the filesystem.
1758 #ifndef _SYS_SYSPROTO_H_
1759 struct unlink_args {
1766 struct unlink_args /* {
1771 return (kern_unlink(td, uap->path, UIO_USERSPACE));
1774 #ifndef _SYS_SYSPROTO_H_
1775 struct unlinkat_args {
1782 sys_unlinkat(struct thread *td, struct unlinkat_args *uap)
1784 int flag = uap->flag;
1786 char *path = uap->path;
1788 if (flag & ~AT_REMOVEDIR)
1791 if (flag & AT_REMOVEDIR)
1792 return (kern_rmdirat(td, fd, path, UIO_USERSPACE));
1794 return (kern_unlinkat(td, fd, path, UIO_USERSPACE, 0));
1798 kern_unlink(struct thread *td, char *path, enum uio_seg pathseg)
1801 return (kern_unlinkat(td, AT_FDCWD, path, pathseg, 0));
1805 kern_unlinkat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1810 struct nameidata nd;
1812 cap_rights_t rights;
1817 NDINIT_ATRIGHTS(&nd, DELETE, LOCKPARENT | LOCKLEAF | AUDITVNODE1,
1818 pathseg, path, fd, cap_rights_init(&rights, CAP_UNLINKAT), td);
1819 if ((error = namei(&nd)) != 0)
1820 return (error == EINVAL ? EPERM : error);
1822 if (vp->v_type == VDIR && oldinum == 0) {
1823 error = EPERM; /* POSIX */
1824 } else if (oldinum != 0 &&
1825 ((error = vn_stat(vp, &sb, td->td_ucred, NOCRED, td)) == 0) &&
1826 sb.st_ino != oldinum) {
1827 error = EIDRM; /* Identifier removed */
1830 * The root of a mounted filesystem cannot be deleted.
1832 * XXX: can this only be a VDIR case?
1834 if (vp->v_vflag & VV_ROOT)
1838 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1839 NDFREE(&nd, NDF_ONLY_PNBUF);
1841 if (vp == nd.ni_dvp)
1845 if ((error = vn_start_write(NULL, &mp,
1846 V_XSLEEP | PCATCH)) != 0)
1851 error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp,
1856 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_UNLINK);
1857 error = VOP_REMOVE(nd.ni_dvp, vp, &nd.ni_cnd);
1861 vn_finished_write(mp);
1863 NDFREE(&nd, NDF_ONLY_PNBUF);
1865 if (vp == nd.ni_dvp)
1873 * Reposition read/write file offset.
1875 #ifndef _SYS_SYSPROTO_H_
1886 register struct lseek_args /* {
1894 cap_rights_t rights;
1897 AUDIT_ARG_FD(uap->fd);
1898 error = fget(td, uap->fd, cap_rights_init(&rights, CAP_SEEK), &fp);
1901 error = (fp->f_ops->fo_flags & DFLAG_SEEKABLE) != 0 ?
1902 fo_seek(fp, uap->offset, uap->whence, td) : ESPIPE;
1907 #if defined(COMPAT_43)
1909 * Reposition read/write file offset.
1911 #ifndef _SYS_SYSPROTO_H_
1912 struct olseek_args {
1921 register struct olseek_args /* {
1927 struct lseek_args /* {
1935 nuap.offset = uap->offset;
1936 nuap.whence = uap->whence;
1937 return (sys_lseek(td, &nuap));
1939 #endif /* COMPAT_43 */
1941 /* Version with the 'pad' argument */
1943 freebsd6_lseek(td, uap)
1945 register struct freebsd6_lseek_args *uap;
1947 struct lseek_args ouap;
1950 ouap.offset = uap->offset;
1951 ouap.whence = uap->whence;
1952 return (sys_lseek(td, &ouap));
1956 * Check access permissions using passed credentials.
1959 vn_access(vp, user_flags, cred, td)
1968 /* Flags == 0 means only check for existence. */
1972 if (user_flags & R_OK)
1974 if (user_flags & W_OK)
1976 if (user_flags & X_OK)
1979 error = mac_vnode_check_access(cred, vp, accmode);
1983 if ((accmode & VWRITE) == 0 || (error = vn_writechk(vp)) == 0)
1984 error = VOP_ACCESS(vp, accmode, cred, td);
1990 * Check access permissions using "real" credentials.
1992 #ifndef _SYS_SYSPROTO_H_
1993 struct access_args {
2001 register struct access_args /* {
2007 return (kern_access(td, uap->path, UIO_USERSPACE, uap->amode));
2010 #ifndef _SYS_SYSPROTO_H_
2011 struct faccessat_args {
2019 sys_faccessat(struct thread *td, struct faccessat_args *uap)
2022 if (uap->flag & ~AT_EACCESS)
2024 return (kern_accessat(td, uap->fd, uap->path, UIO_USERSPACE, uap->flag,
2029 kern_access(struct thread *td, char *path, enum uio_seg pathseg, int amode)
2032 return (kern_accessat(td, AT_FDCWD, path, pathseg, 0, amode));
2036 kern_accessat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2037 int flag, int amode)
2039 struct ucred *cred, *tmpcred;
2041 struct nameidata nd;
2042 cap_rights_t rights;
2046 * Create and modify a temporary credential instead of one that
2047 * is potentially shared.
2049 if (!(flag & AT_EACCESS)) {
2050 cred = td->td_ucred;
2051 tmpcred = crdup(cred);
2052 tmpcred->cr_uid = cred->cr_ruid;
2053 tmpcred->cr_groups[0] = cred->cr_rgid;
2054 td->td_ucred = tmpcred;
2056 cred = tmpcred = td->td_ucred;
2057 AUDIT_ARG_VALUE(amode);
2058 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF |
2059 AUDITVNODE1, pathseg, path, fd, cap_rights_init(&rights, CAP_FSTAT),
2061 if ((error = namei(&nd)) != 0)
2065 error = vn_access(vp, amode, tmpcred, td);
2066 NDFREE(&nd, NDF_ONLY_PNBUF);
2069 if (!(flag & AT_EACCESS)) {
2070 td->td_ucred = cred;
2077 * Check access permissions using "effective" credentials.
2079 #ifndef _SYS_SYSPROTO_H_
2080 struct eaccess_args {
2086 sys_eaccess(td, uap)
2088 register struct eaccess_args /* {
2094 return (kern_eaccess(td, uap->path, UIO_USERSPACE, uap->amode));
2098 kern_eaccess(struct thread *td, char *path, enum uio_seg pathseg, int amode)
2101 return (kern_accessat(td, AT_FDCWD, path, pathseg, AT_EACCESS, amode));
2104 #if defined(COMPAT_43)
2106 * Get file status; this version follows links.
2108 #ifndef _SYS_SYSPROTO_H_
2117 register struct ostat_args /* {
2126 error = kern_stat(td, uap->path, UIO_USERSPACE, &sb);
2130 return (copyout(&osb, uap->ub, sizeof (osb)));
2134 * Get file status; this version does not follow links.
2136 #ifndef _SYS_SYSPROTO_H_
2137 struct olstat_args {
2145 register struct olstat_args /* {
2154 error = kern_lstat(td, uap->path, UIO_USERSPACE, &sb);
2158 return (copyout(&osb, uap->ub, sizeof (osb)));
2162 * Convert from an old to a new stat structure.
2170 ost->st_dev = st->st_dev;
2171 ost->st_ino = st->st_ino;
2172 ost->st_mode = st->st_mode;
2173 ost->st_nlink = st->st_nlink;
2174 ost->st_uid = st->st_uid;
2175 ost->st_gid = st->st_gid;
2176 ost->st_rdev = st->st_rdev;
2177 if (st->st_size < (quad_t)1 << 32)
2178 ost->st_size = st->st_size;
2181 ost->st_atim = st->st_atim;
2182 ost->st_mtim = st->st_mtim;
2183 ost->st_ctim = st->st_ctim;
2184 ost->st_blksize = st->st_blksize;
2185 ost->st_blocks = st->st_blocks;
2186 ost->st_flags = st->st_flags;
2187 ost->st_gen = st->st_gen;
2189 #endif /* COMPAT_43 */
2192 * Get file status; this version follows links.
2194 #ifndef _SYS_SYSPROTO_H_
2203 register struct stat_args /* {
2211 error = kern_stat(td, uap->path, UIO_USERSPACE, &sb);
2213 error = copyout(&sb, uap->ub, sizeof (sb));
2217 #ifndef _SYS_SYSPROTO_H_
2218 struct fstatat_args {
2226 sys_fstatat(struct thread *td, struct fstatat_args *uap)
2231 error = kern_statat(td, uap->flag, uap->fd, uap->path,
2232 UIO_USERSPACE, &sb);
2234 error = copyout(&sb, uap->buf, sizeof (sb));
2239 kern_stat(struct thread *td, char *path, enum uio_seg pathseg, struct stat *sbp)
2242 return (kern_statat(td, 0, AT_FDCWD, path, pathseg, sbp));
2246 kern_statat(struct thread *td, int flag, int fd, char *path,
2247 enum uio_seg pathseg, struct stat *sbp)
2250 return (kern_statat_vnhook(td, flag, fd, path, pathseg, sbp, NULL));
2254 kern_statat_vnhook(struct thread *td, int flag, int fd, char *path,
2255 enum uio_seg pathseg, struct stat *sbp,
2256 void (*hook)(struct vnode *vp, struct stat *sbp))
2258 struct nameidata nd;
2260 cap_rights_t rights;
2263 if (flag & ~AT_SYMLINK_NOFOLLOW)
2266 NDINIT_ATRIGHTS(&nd, LOOKUP, ((flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW :
2267 FOLLOW) | LOCKSHARED | LOCKLEAF | AUDITVNODE1, pathseg, path, fd,
2268 cap_rights_init(&rights, CAP_FSTAT), td);
2270 if ((error = namei(&nd)) != 0)
2272 error = vn_stat(nd.ni_vp, &sb, td->td_ucred, NOCRED, td);
2274 SDT_PROBE(vfs, , stat, mode, path, sb.st_mode, 0, 0, 0);
2275 if (S_ISREG(sb.st_mode))
2276 SDT_PROBE(vfs, , stat, reg, path, pathseg, 0, 0, 0);
2277 if (__predict_false(hook != NULL))
2278 hook(nd.ni_vp, &sb);
2280 NDFREE(&nd, NDF_ONLY_PNBUF);
2286 if (KTRPOINT(td, KTR_STRUCT))
2293 * Get file status; this version does not follow links.
2295 #ifndef _SYS_SYSPROTO_H_
2304 register struct lstat_args /* {
2312 error = kern_lstat(td, uap->path, UIO_USERSPACE, &sb);
2314 error = copyout(&sb, uap->ub, sizeof (sb));
2319 kern_lstat(struct thread *td, char *path, enum uio_seg pathseg, struct stat *sbp)
2322 return (kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, path, pathseg,
2327 * Implementation of the NetBSD [l]stat() functions.
2335 bzero(nsb, sizeof *nsb);
2336 nsb->st_dev = sb->st_dev;
2337 nsb->st_ino = sb->st_ino;
2338 nsb->st_mode = sb->st_mode;
2339 nsb->st_nlink = sb->st_nlink;
2340 nsb->st_uid = sb->st_uid;
2341 nsb->st_gid = sb->st_gid;
2342 nsb->st_rdev = sb->st_rdev;
2343 nsb->st_atim = sb->st_atim;
2344 nsb->st_mtim = sb->st_mtim;
2345 nsb->st_ctim = sb->st_ctim;
2346 nsb->st_size = sb->st_size;
2347 nsb->st_blocks = sb->st_blocks;
2348 nsb->st_blksize = sb->st_blksize;
2349 nsb->st_flags = sb->st_flags;
2350 nsb->st_gen = sb->st_gen;
2351 nsb->st_birthtim = sb->st_birthtim;
2354 #ifndef _SYS_SYSPROTO_H_
2363 register struct nstat_args /* {
2372 error = kern_stat(td, uap->path, UIO_USERSPACE, &sb);
2375 cvtnstat(&sb, &nsb);
2376 return (copyout(&nsb, uap->ub, sizeof (nsb)));
2380 * NetBSD lstat. Get file status; this version does not follow links.
2382 #ifndef _SYS_SYSPROTO_H_
2391 register struct nlstat_args /* {
2400 error = kern_lstat(td, uap->path, UIO_USERSPACE, &sb);
2403 cvtnstat(&sb, &nsb);
2404 return (copyout(&nsb, uap->ub, sizeof (nsb)));
2408 * Get configurable pathname variables.
2410 #ifndef _SYS_SYSPROTO_H_
2411 struct pathconf_args {
2417 sys_pathconf(td, uap)
2419 register struct pathconf_args /* {
2425 return (kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name, FOLLOW));
2428 #ifndef _SYS_SYSPROTO_H_
2429 struct lpathconf_args {
2435 sys_lpathconf(td, uap)
2437 register struct lpathconf_args /* {
2443 return (kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name,
2448 kern_pathconf(struct thread *td, char *path, enum uio_seg pathseg, int name,
2451 struct nameidata nd;
2454 NDINIT(&nd, LOOKUP, LOCKSHARED | LOCKLEAF | AUDITVNODE1 | flags,
2456 if ((error = namei(&nd)) != 0)
2458 NDFREE(&nd, NDF_ONLY_PNBUF);
2460 /* If asynchronous I/O is available, it works for all files. */
2461 if (name == _PC_ASYNC_IO)
2462 td->td_retval[0] = async_io_version;
2464 error = VOP_PATHCONF(nd.ni_vp, name, td->td_retval);
2470 * Return target name of a symbolic link.
2472 #ifndef _SYS_SYSPROTO_H_
2473 struct readlink_args {
2480 sys_readlink(td, uap)
2482 register struct readlink_args /* {
2489 return (kern_readlink(td, uap->path, UIO_USERSPACE, uap->buf,
2490 UIO_USERSPACE, uap->count));
2492 #ifndef _SYS_SYSPROTO_H_
2493 struct readlinkat_args {
2501 sys_readlinkat(struct thread *td, struct readlinkat_args *uap)
2504 return (kern_readlinkat(td, uap->fd, uap->path, UIO_USERSPACE,
2505 uap->buf, UIO_USERSPACE, uap->bufsize));
2509 kern_readlink(struct thread *td, char *path, enum uio_seg pathseg, char *buf,
2510 enum uio_seg bufseg, size_t count)
2513 return (kern_readlinkat(td, AT_FDCWD, path, pathseg, buf, bufseg,
2518 kern_readlinkat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2519 char *buf, enum uio_seg bufseg, size_t count)
2524 struct nameidata nd;
2527 if (count > IOSIZE_MAX)
2530 NDINIT_AT(&nd, LOOKUP, NOFOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
2531 pathseg, path, fd, td);
2533 if ((error = namei(&nd)) != 0)
2535 NDFREE(&nd, NDF_ONLY_PNBUF);
2538 error = mac_vnode_check_readlink(td->td_ucred, vp);
2544 if (vp->v_type != VLNK)
2547 aiov.iov_base = buf;
2548 aiov.iov_len = count;
2549 auio.uio_iov = &aiov;
2550 auio.uio_iovcnt = 1;
2551 auio.uio_offset = 0;
2552 auio.uio_rw = UIO_READ;
2553 auio.uio_segflg = bufseg;
2555 auio.uio_resid = count;
2556 error = VOP_READLINK(vp, &auio, td->td_ucred);
2559 td->td_retval[0] = count - auio.uio_resid;
2564 * Common implementation code for chflags() and fchflags().
2567 setfflags(td, vp, flags)
2576 /* We can't support the value matching VNOVAL. */
2577 if (flags == VNOVAL)
2578 return (EOPNOTSUPP);
2581 * Prevent non-root users from setting flags on devices. When
2582 * a device is reused, users can retain ownership of the device
2583 * if they are allowed to set flags and programs assume that
2584 * chown can't fail when done as root.
2586 if (vp->v_type == VCHR || vp->v_type == VBLK) {
2587 error = priv_check(td, PRIV_VFS_CHFLAGS_DEV);
2592 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2595 vattr.va_flags = flags;
2596 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2598 error = mac_vnode_check_setflags(td->td_ucred, vp, vattr.va_flags);
2601 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
2603 vn_finished_write(mp);
2608 * Change flags of a file given a path name.
2610 #ifndef _SYS_SYSPROTO_H_
2611 struct chflags_args {
2617 sys_chflags(td, uap)
2619 register struct chflags_args /* {
2625 return (kern_chflags(td, uap->path, UIO_USERSPACE, uap->flags));
2628 #ifndef _SYS_SYSPROTO_H_
2629 struct chflagsat_args {
2637 sys_chflagsat(struct thread *td, struct chflagsat_args *uap)
2640 const char *path = uap->path;
2641 u_long flags = uap->flags;
2642 int atflag = uap->atflag;
2644 if (atflag & ~AT_SYMLINK_NOFOLLOW)
2647 return (kern_chflagsat(td, fd, path, UIO_USERSPACE, flags, atflag));
2651 kern_chflags(struct thread *td, const char *path, enum uio_seg pathseg,
2655 return (kern_chflagsat(td, AT_FDCWD, path, pathseg, flags, 0));
2659 * Same as chflags() but doesn't follow symlinks.
2662 sys_lchflags(td, uap)
2664 register struct lchflags_args /* {
2670 return (kern_chflagsat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2671 uap->flags, AT_SYMLINK_NOFOLLOW));
2675 kern_chflagsat(struct thread *td, int fd, const char *path,
2676 enum uio_seg pathseg, u_long flags, int atflag)
2678 struct nameidata nd;
2679 cap_rights_t rights;
2682 AUDIT_ARG_FFLAGS(flags);
2683 follow = (atflag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
2684 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, pathseg, path, fd,
2685 cap_rights_init(&rights, CAP_FCHFLAGS), td);
2686 if ((error = namei(&nd)) != 0)
2688 NDFREE(&nd, NDF_ONLY_PNBUF);
2689 error = setfflags(td, nd.ni_vp, flags);
2695 * Change flags of a file given a file descriptor.
2697 #ifndef _SYS_SYSPROTO_H_
2698 struct fchflags_args {
2704 sys_fchflags(td, uap)
2706 register struct fchflags_args /* {
2712 cap_rights_t rights;
2715 AUDIT_ARG_FD(uap->fd);
2716 AUDIT_ARG_FFLAGS(uap->flags);
2717 error = getvnode(td->td_proc->p_fd, uap->fd,
2718 cap_rights_init(&rights, CAP_FCHFLAGS), &fp);
2722 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
2723 AUDIT_ARG_VNODE1(fp->f_vnode);
2724 VOP_UNLOCK(fp->f_vnode, 0);
2726 error = setfflags(td, fp->f_vnode, uap->flags);
2732 * Common implementation code for chmod(), lchmod() and fchmod().
2735 setfmode(td, cred, vp, mode)
2745 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2747 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2749 vattr.va_mode = mode & ALLPERMS;
2751 error = mac_vnode_check_setmode(cred, vp, vattr.va_mode);
2754 error = VOP_SETATTR(vp, &vattr, cred);
2756 vn_finished_write(mp);
2761 * Change mode of a file given path name.
2763 #ifndef _SYS_SYSPROTO_H_
2772 register struct chmod_args /* {
2778 return (kern_chmod(td, uap->path, UIO_USERSPACE, uap->mode));
2781 #ifndef _SYS_SYSPROTO_H_
2782 struct fchmodat_args {
2790 sys_fchmodat(struct thread *td, struct fchmodat_args *uap)
2792 int flag = uap->flag;
2794 char *path = uap->path;
2795 mode_t mode = uap->mode;
2797 if (flag & ~AT_SYMLINK_NOFOLLOW)
2800 return (kern_fchmodat(td, fd, path, UIO_USERSPACE, mode, flag));
2804 kern_chmod(struct thread *td, char *path, enum uio_seg pathseg, int mode)
2807 return (kern_fchmodat(td, AT_FDCWD, path, pathseg, mode, 0));
2811 * Change mode of a file given path name (don't follow links.)
2813 #ifndef _SYS_SYSPROTO_H_
2814 struct lchmod_args {
2822 register struct lchmod_args /* {
2828 return (kern_fchmodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2829 uap->mode, AT_SYMLINK_NOFOLLOW));
2833 kern_fchmodat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2834 mode_t mode, int flag)
2836 struct nameidata nd;
2837 cap_rights_t rights;
2840 AUDIT_ARG_MODE(mode);
2841 follow = (flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
2842 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, pathseg, path, fd,
2843 cap_rights_init(&rights, CAP_FCHMOD), td);
2844 if ((error = namei(&nd)) != 0)
2846 NDFREE(&nd, NDF_ONLY_PNBUF);
2847 error = setfmode(td, td->td_ucred, nd.ni_vp, mode);
2853 * Change mode of a file given a file descriptor.
2855 #ifndef _SYS_SYSPROTO_H_
2856 struct fchmod_args {
2862 sys_fchmod(struct thread *td, struct fchmod_args *uap)
2865 cap_rights_t rights;
2868 AUDIT_ARG_FD(uap->fd);
2869 AUDIT_ARG_MODE(uap->mode);
2871 error = fget(td, uap->fd, cap_rights_init(&rights, CAP_FCHMOD), &fp);
2874 error = fo_chmod(fp, uap->mode, td->td_ucred, td);
2880 * Common implementation for chown(), lchown(), and fchown()
2883 setfown(td, cred, vp, uid, gid)
2894 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2896 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2901 error = mac_vnode_check_setowner(cred, vp, vattr.va_uid,
2905 error = VOP_SETATTR(vp, &vattr, cred);
2907 vn_finished_write(mp);
2912 * Set ownership given a path name.
2914 #ifndef _SYS_SYSPROTO_H_
2924 register struct chown_args /* {
2931 return (kern_chown(td, uap->path, UIO_USERSPACE, uap->uid, uap->gid));
2934 #ifndef _SYS_SYSPROTO_H_
2935 struct fchownat_args {
2944 sys_fchownat(struct thread *td, struct fchownat_args *uap)
2949 if (flag & ~AT_SYMLINK_NOFOLLOW)
2952 return (kern_fchownat(td, uap->fd, uap->path, UIO_USERSPACE, uap->uid,
2953 uap->gid, uap->flag));
2957 kern_chown(struct thread *td, char *path, enum uio_seg pathseg, int uid,
2961 return (kern_fchownat(td, AT_FDCWD, path, pathseg, uid, gid, 0));
2965 kern_fchownat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2966 int uid, int gid, int flag)
2968 struct nameidata nd;
2969 cap_rights_t rights;
2972 AUDIT_ARG_OWNER(uid, gid);
2973 follow = (flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
2974 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, pathseg, path, fd,
2975 cap_rights_init(&rights, CAP_FCHOWN), td);
2977 if ((error = namei(&nd)) != 0)
2979 NDFREE(&nd, NDF_ONLY_PNBUF);
2980 error = setfown(td, td->td_ucred, nd.ni_vp, uid, gid);
2986 * Set ownership given a path name, do not cross symlinks.
2988 #ifndef _SYS_SYSPROTO_H_
2989 struct lchown_args {
2998 register struct lchown_args /* {
3005 return (kern_lchown(td, uap->path, UIO_USERSPACE, uap->uid, uap->gid));
3009 kern_lchown(struct thread *td, char *path, enum uio_seg pathseg, int uid,
3013 return (kern_fchownat(td, AT_FDCWD, path, pathseg, uid, gid,
3014 AT_SYMLINK_NOFOLLOW));
3018 * Set ownership given a file descriptor.
3020 #ifndef _SYS_SYSPROTO_H_
3021 struct fchown_args {
3030 register struct fchown_args /* {
3037 cap_rights_t rights;
3040 AUDIT_ARG_FD(uap->fd);
3041 AUDIT_ARG_OWNER(uap->uid, uap->gid);
3042 error = fget(td, uap->fd, cap_rights_init(&rights, CAP_FCHOWN), &fp);
3045 error = fo_chown(fp, uap->uid, uap->gid, td->td_ucred, td);
3051 * Common implementation code for utimes(), lutimes(), and futimes().
3054 getutimes(usrtvp, tvpseg, tsp)
3055 const struct timeval *usrtvp;
3056 enum uio_seg tvpseg;
3057 struct timespec *tsp;
3059 struct timeval tv[2];
3060 const struct timeval *tvp;
3063 if (usrtvp == NULL) {
3064 vfs_timestamp(&tsp[0]);
3067 if (tvpseg == UIO_SYSSPACE) {
3070 if ((error = copyin(usrtvp, tv, sizeof(tv))) != 0)
3075 if (tvp[0].tv_usec < 0 || tvp[0].tv_usec >= 1000000 ||
3076 tvp[1].tv_usec < 0 || tvp[1].tv_usec >= 1000000)
3078 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
3079 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
3085 * Common implementation code for utimes(), lutimes(), and futimes().
3088 setutimes(td, vp, ts, numtimes, nullflag)
3091 const struct timespec *ts;
3097 int error, setbirthtime;
3099 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
3101 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3103 if (numtimes < 3 && !VOP_GETATTR(vp, &vattr, td->td_ucred) &&
3104 timespeccmp(&ts[1], &vattr.va_birthtime, < ))
3107 vattr.va_atime = ts[0];
3108 vattr.va_mtime = ts[1];
3110 vattr.va_birthtime = ts[1];
3112 vattr.va_birthtime = ts[2];
3114 vattr.va_vaflags |= VA_UTIMES_NULL;
3116 error = mac_vnode_check_setutimes(td->td_ucred, vp, vattr.va_atime,
3120 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3122 vn_finished_write(mp);
3127 * Set the access and modification times of a file.
3129 #ifndef _SYS_SYSPROTO_H_
3130 struct utimes_args {
3132 struct timeval *tptr;
3138 register struct utimes_args /* {
3140 struct timeval *tptr;
3144 return (kern_utimes(td, uap->path, UIO_USERSPACE, uap->tptr,
3148 #ifndef _SYS_SYSPROTO_H_
3149 struct futimesat_args {
3152 const struct timeval * times;
3156 sys_futimesat(struct thread *td, struct futimesat_args *uap)
3159 return (kern_utimesat(td, uap->fd, uap->path, UIO_USERSPACE,
3160 uap->times, UIO_USERSPACE));
3164 kern_utimes(struct thread *td, char *path, enum uio_seg pathseg,
3165 struct timeval *tptr, enum uio_seg tptrseg)
3168 return (kern_utimesat(td, AT_FDCWD, path, pathseg, tptr, tptrseg));
3172 kern_utimesat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
3173 struct timeval *tptr, enum uio_seg tptrseg)
3175 struct nameidata nd;
3176 struct timespec ts[2];
3177 cap_rights_t rights;
3180 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3182 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, fd,
3183 cap_rights_init(&rights, CAP_FUTIMES), td);
3185 if ((error = namei(&nd)) != 0)
3187 NDFREE(&nd, NDF_ONLY_PNBUF);
3188 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3194 * Set the access and modification times of a file.
3196 #ifndef _SYS_SYSPROTO_H_
3197 struct lutimes_args {
3199 struct timeval *tptr;
3203 sys_lutimes(td, uap)
3205 register struct lutimes_args /* {
3207 struct timeval *tptr;
3211 return (kern_lutimes(td, uap->path, UIO_USERSPACE, uap->tptr,
3216 kern_lutimes(struct thread *td, char *path, enum uio_seg pathseg,
3217 struct timeval *tptr, enum uio_seg tptrseg)
3219 struct timespec ts[2];
3220 struct nameidata nd;
3223 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3225 NDINIT(&nd, LOOKUP, NOFOLLOW | AUDITVNODE1, pathseg, path, td);
3226 if ((error = namei(&nd)) != 0)
3228 NDFREE(&nd, NDF_ONLY_PNBUF);
3229 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3235 * Set the access and modification times of a file.
3237 #ifndef _SYS_SYSPROTO_H_
3238 struct futimes_args {
3240 struct timeval *tptr;
3244 sys_futimes(td, uap)
3246 register struct futimes_args /* {
3248 struct timeval *tptr;
3252 return (kern_futimes(td, uap->fd, uap->tptr, UIO_USERSPACE));
3256 kern_futimes(struct thread *td, int fd, struct timeval *tptr,
3257 enum uio_seg tptrseg)
3259 struct timespec ts[2];
3261 cap_rights_t rights;
3265 error = getutimes(tptr, tptrseg, ts);
3268 error = getvnode(td->td_proc->p_fd, fd,
3269 cap_rights_init(&rights, CAP_FUTIMES), &fp);
3273 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
3274 AUDIT_ARG_VNODE1(fp->f_vnode);
3275 VOP_UNLOCK(fp->f_vnode, 0);
3277 error = setutimes(td, fp->f_vnode, ts, 2, tptr == NULL);
3283 * Truncate a file given its path name.
3285 #ifndef _SYS_SYSPROTO_H_
3286 struct truncate_args {
3293 sys_truncate(td, uap)
3295 register struct truncate_args /* {
3302 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3306 kern_truncate(struct thread *td, char *path, enum uio_seg pathseg, off_t length)
3312 struct nameidata nd;
3317 NDINIT(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, td);
3318 if ((error = namei(&nd)) != 0)
3321 rl_cookie = vn_rangelock_wlock(vp, 0, OFF_MAX);
3322 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0) {
3323 vn_rangelock_unlock(vp, rl_cookie);
3327 NDFREE(&nd, NDF_ONLY_PNBUF);
3328 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3329 if (vp->v_type == VDIR)
3332 else if ((error = mac_vnode_check_write(td->td_ucred, NOCRED, vp))) {
3335 else if ((error = vn_writechk(vp)) == 0 &&
3336 (error = VOP_ACCESS(vp, VWRITE, td->td_ucred, td)) == 0) {
3338 vattr.va_size = length;
3339 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3342 vn_finished_write(mp);
3343 vn_rangelock_unlock(vp, rl_cookie);
3348 #if defined(COMPAT_43)
3350 * Truncate a file given its path name.
3352 #ifndef _SYS_SYSPROTO_H_
3353 struct otruncate_args {
3361 register struct otruncate_args /* {
3366 struct truncate_args /* {
3372 nuap.path = uap->path;
3373 nuap.length = uap->length;
3374 return (sys_truncate(td, &nuap));
3376 #endif /* COMPAT_43 */
3378 /* Versions with the pad argument */
3380 freebsd6_truncate(struct thread *td, struct freebsd6_truncate_args *uap)
3382 struct truncate_args ouap;
3384 ouap.path = uap->path;
3385 ouap.length = uap->length;
3386 return (sys_truncate(td, &ouap));
3390 freebsd6_ftruncate(struct thread *td, struct freebsd6_ftruncate_args *uap)
3392 struct ftruncate_args ouap;
3395 ouap.length = uap->length;
3396 return (sys_ftruncate(td, &ouap));
3400 * Sync an open file.
3402 #ifndef _SYS_SYSPROTO_H_
3410 struct fsync_args /* {
3417 cap_rights_t rights;
3418 int error, lock_flags;
3420 AUDIT_ARG_FD(uap->fd);
3421 error = getvnode(td->td_proc->p_fd, uap->fd,
3422 cap_rights_init(&rights, CAP_FSYNC), &fp);
3426 error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
3429 if (MNT_SHARED_WRITES(mp) ||
3430 ((mp == NULL) && MNT_SHARED_WRITES(vp->v_mount))) {
3431 lock_flags = LK_SHARED;
3433 lock_flags = LK_EXCLUSIVE;
3435 vn_lock(vp, lock_flags | LK_RETRY);
3436 AUDIT_ARG_VNODE1(vp);
3437 if (vp->v_object != NULL) {
3438 VM_OBJECT_WLOCK(vp->v_object);
3439 vm_object_page_clean(vp->v_object, 0, 0, 0);
3440 VM_OBJECT_WUNLOCK(vp->v_object);
3442 error = VOP_FSYNC(vp, MNT_WAIT, td);
3445 vn_finished_write(mp);
3452 * Rename files. Source and destination must either both be directories, or
3453 * both not be directories. If target is a directory, it must be empty.
3455 #ifndef _SYS_SYSPROTO_H_
3456 struct rename_args {
3464 register struct rename_args /* {
3470 return (kern_rename(td, uap->from, uap->to, UIO_USERSPACE));
3473 #ifndef _SYS_SYSPROTO_H_
3474 struct renameat_args {
3482 sys_renameat(struct thread *td, struct renameat_args *uap)
3485 return (kern_renameat(td, uap->oldfd, uap->old, uap->newfd, uap->new,
3490 kern_rename(struct thread *td, char *from, char *to, enum uio_seg pathseg)
3493 return (kern_renameat(td, AT_FDCWD, from, AT_FDCWD, to, pathseg));
3497 kern_renameat(struct thread *td, int oldfd, char *old, int newfd, char *new,
3498 enum uio_seg pathseg)
3500 struct mount *mp = NULL;
3501 struct vnode *tvp, *fvp, *tdvp;
3502 struct nameidata fromnd, tond;
3503 cap_rights_t rights;
3508 NDINIT_ATRIGHTS(&fromnd, DELETE, LOCKPARENT | LOCKLEAF | SAVESTART |
3509 AUDITVNODE1, pathseg, old, oldfd,
3510 cap_rights_init(&rights, CAP_RENAMEAT), td);
3512 NDINIT_ATRIGHTS(&fromnd, DELETE, WANTPARENT | SAVESTART | AUDITVNODE1,
3513 pathseg, old, oldfd, cap_rights_init(&rights, CAP_RENAMEAT), td);
3516 if ((error = namei(&fromnd)) != 0)
3519 error = mac_vnode_check_rename_from(td->td_ucred, fromnd.ni_dvp,
3520 fromnd.ni_vp, &fromnd.ni_cnd);
3521 VOP_UNLOCK(fromnd.ni_dvp, 0);
3522 if (fromnd.ni_dvp != fromnd.ni_vp)
3523 VOP_UNLOCK(fromnd.ni_vp, 0);
3527 error = vn_start_write(fvp, &mp, V_WAIT | PCATCH);
3529 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3530 vrele(fromnd.ni_dvp);
3534 NDINIT_ATRIGHTS(&tond, RENAME, LOCKPARENT | LOCKLEAF | NOCACHE |
3535 SAVESTART | AUDITVNODE2, pathseg, new, newfd,
3536 cap_rights_init(&rights, CAP_LINKAT), td);
3537 if (fromnd.ni_vp->v_type == VDIR)
3538 tond.ni_cnd.cn_flags |= WILLBEDIR;
3539 if ((error = namei(&tond)) != 0) {
3540 /* Translate error code for rename("dir1", "dir2/."). */
3541 if (error == EISDIR && fvp->v_type == VDIR)
3543 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3544 vrele(fromnd.ni_dvp);
3546 vn_finished_write(mp);
3552 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
3555 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
3560 if (newfd != AT_FDCWD) {
3562 * If the target already exists we require CAP_UNLINKAT
3565 error = cap_check(&tond.ni_filecaps.fc_rights,
3566 cap_rights_init(&rights, CAP_UNLINKAT));
3577 * If the source is the same as the destination (that is, if they
3578 * are links to the same vnode), then there is nothing to do.
3584 error = mac_vnode_check_rename_to(td->td_ucred, tdvp,
3585 tond.ni_vp, fromnd.ni_dvp == tdvp, &tond.ni_cnd);
3589 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
3590 tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
3591 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3592 NDFREE(&tond, NDF_ONLY_PNBUF);
3594 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3595 NDFREE(&tond, NDF_ONLY_PNBUF);
3602 vrele(fromnd.ni_dvp);
3605 vrele(tond.ni_startdir);
3606 vn_finished_write(mp);
3608 if (fromnd.ni_startdir)
3609 vrele(fromnd.ni_startdir);
3616 * Make a directory file.
3618 #ifndef _SYS_SYSPROTO_H_
3627 register struct mkdir_args /* {
3633 return (kern_mkdir(td, uap->path, UIO_USERSPACE, uap->mode));
3636 #ifndef _SYS_SYSPROTO_H_
3637 struct mkdirat_args {
3644 sys_mkdirat(struct thread *td, struct mkdirat_args *uap)
3647 return (kern_mkdirat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode));
3651 kern_mkdir(struct thread *td, char *path, enum uio_seg segflg, int mode)
3654 return (kern_mkdirat(td, AT_FDCWD, path, segflg, mode));
3658 kern_mkdirat(struct thread *td, int fd, char *path, enum uio_seg segflg,
3664 struct nameidata nd;
3665 cap_rights_t rights;
3668 AUDIT_ARG_MODE(mode);
3671 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1,
3672 segflg, path, fd, cap_rights_init(&rights, CAP_MKDIRAT), td);
3673 nd.ni_cnd.cn_flags |= WILLBEDIR;
3674 if ((error = namei(&nd)) != 0)
3678 NDFREE(&nd, NDF_ONLY_PNBUF);
3680 * XXX namei called with LOCKPARENT but not LOCKLEAF has
3681 * the strange behaviour of leaving the vnode unlocked
3682 * if the target is the same vnode as the parent.
3684 if (vp == nd.ni_dvp)
3691 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3692 NDFREE(&nd, NDF_ONLY_PNBUF);
3694 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3699 vattr.va_type = VDIR;
3700 vattr.va_mode = (mode & ACCESSPERMS) &~ td->td_proc->p_fd->fd_cmask;
3702 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
3707 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
3711 NDFREE(&nd, NDF_ONLY_PNBUF);
3715 vn_finished_write(mp);
3720 * Remove a directory file.
3722 #ifndef _SYS_SYSPROTO_H_
3730 struct rmdir_args /* {
3735 return (kern_rmdir(td, uap->path, UIO_USERSPACE));
3739 kern_rmdir(struct thread *td, char *path, enum uio_seg pathseg)
3742 return (kern_rmdirat(td, AT_FDCWD, path, pathseg));
3746 kern_rmdirat(struct thread *td, int fd, char *path, enum uio_seg pathseg)
3750 struct nameidata nd;
3751 cap_rights_t rights;
3756 NDINIT_ATRIGHTS(&nd, DELETE, LOCKPARENT | LOCKLEAF | AUDITVNODE1,
3757 pathseg, path, fd, cap_rights_init(&rights, CAP_UNLINKAT), td);
3758 if ((error = namei(&nd)) != 0)
3761 if (vp->v_type != VDIR) {
3766 * No rmdir "." please.
3768 if (nd.ni_dvp == vp) {
3773 * The root of a mounted filesystem cannot be deleted.
3775 if (vp->v_vflag & VV_ROOT) {
3780 error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp,
3785 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3786 NDFREE(&nd, NDF_ONLY_PNBUF);
3788 if (nd.ni_dvp == vp)
3792 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3796 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_UNLINK);
3797 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
3798 vn_finished_write(mp);
3800 NDFREE(&nd, NDF_ONLY_PNBUF);
3802 if (nd.ni_dvp == vp)
3811 * Read a block of directory entries in a filesystem independent format.
3813 #ifndef _SYS_SYSPROTO_H_
3814 struct ogetdirentries_args {
3822 ogetdirentries(struct thread *td, struct ogetdirentries_args *uap)
3827 error = kern_ogetdirentries(td, uap, &loff);
3829 error = copyout(&loff, uap->basep, sizeof(long));
3834 kern_ogetdirentries(struct thread *td, struct ogetdirentries_args *uap,
3839 struct uio auio, kuio;
3840 struct iovec aiov, kiov;
3841 struct dirent *dp, *edp;
3842 cap_rights_t rights;
3844 int error, eofflag, readcnt;
3848 /* XXX arbitrary sanity limit on `count'. */
3849 if (uap->count > 64 * 1024)
3851 error = getvnode(td->td_proc->p_fd, uap->fd,
3852 cap_rights_init(&rights, CAP_READ), &fp);
3855 if ((fp->f_flag & FREAD) == 0) {
3860 foffset = foffset_lock(fp, 0);
3862 if (vp->v_type != VDIR) {
3863 foffset_unlock(fp, foffset, 0);
3867 aiov.iov_base = uap->buf;
3868 aiov.iov_len = uap->count;
3869 auio.uio_iov = &aiov;
3870 auio.uio_iovcnt = 1;
3871 auio.uio_rw = UIO_READ;
3872 auio.uio_segflg = UIO_USERSPACE;
3874 auio.uio_resid = uap->count;
3875 vn_lock(vp, LK_SHARED | LK_RETRY);
3876 loff = auio.uio_offset = foffset;
3878 error = mac_vnode_check_readdir(td->td_ucred, vp);
3881 foffset_unlock(fp, foffset, FOF_NOUPDATE);
3886 # if (BYTE_ORDER != LITTLE_ENDIAN)
3887 if (vp->v_mount->mnt_maxsymlinklen <= 0) {
3888 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag,
3890 foffset = auio.uio_offset;
3895 kuio.uio_iov = &kiov;
3896 kuio.uio_segflg = UIO_SYSSPACE;
3897 kiov.iov_len = uap->count;
3898 dirbuf = malloc(uap->count, M_TEMP, M_WAITOK);
3899 kiov.iov_base = dirbuf;
3900 error = VOP_READDIR(vp, &kuio, fp->f_cred, &eofflag,
3902 foffset = kuio.uio_offset;
3904 readcnt = uap->count - kuio.uio_resid;
3905 edp = (struct dirent *)&dirbuf[readcnt];
3906 for (dp = (struct dirent *)dirbuf; dp < edp; ) {
3907 # if (BYTE_ORDER == LITTLE_ENDIAN)
3909 * The expected low byte of
3910 * dp->d_namlen is our dp->d_type.
3911 * The high MBZ byte of dp->d_namlen
3912 * is our dp->d_namlen.
3914 dp->d_type = dp->d_namlen;
3918 * The dp->d_type is the high byte
3919 * of the expected dp->d_namlen,
3920 * so must be zero'ed.
3924 if (dp->d_reclen > 0) {
3925 dp = (struct dirent *)
3926 ((char *)dp + dp->d_reclen);
3933 error = uiomove(dirbuf, readcnt, &auio);
3935 free(dirbuf, M_TEMP);
3939 foffset_unlock(fp, foffset, 0);
3943 if (uap->count == auio.uio_resid &&
3944 (vp->v_vflag & VV_ROOT) &&
3945 (vp->v_mount->mnt_flag & MNT_UNION)) {
3946 struct vnode *tvp = vp;
3947 vp = vp->v_mount->mnt_vnodecovered;
3956 foffset_unlock(fp, foffset, 0);
3958 td->td_retval[0] = uap->count - auio.uio_resid;
3963 #endif /* COMPAT_43 */
3966 * Read a block of directory entries in a filesystem independent format.
3968 #ifndef _SYS_SYSPROTO_H_
3969 struct getdirentries_args {
3977 sys_getdirentries(td, uap)
3979 register struct getdirentries_args /* {
3989 error = kern_getdirentries(td, uap->fd, uap->buf, uap->count, &base,
3990 NULL, UIO_USERSPACE);
3993 if (uap->basep != NULL)
3994 error = copyout(&base, uap->basep, sizeof(long));
3999 kern_getdirentries(struct thread *td, int fd, char *buf, u_int count,
4000 long *basep, ssize_t *residp, enum uio_seg bufseg)
4006 cap_rights_t rights;
4012 if (count > IOSIZE_MAX)
4014 auio.uio_resid = count;
4015 error = getvnode(td->td_proc->p_fd, fd,
4016 cap_rights_init(&rights, CAP_READ), &fp);
4019 if ((fp->f_flag & FREAD) == 0) {
4024 foffset = foffset_lock(fp, 0);
4026 if (vp->v_type != VDIR) {
4030 aiov.iov_base = buf;
4031 aiov.iov_len = count;
4032 auio.uio_iov = &aiov;
4033 auio.uio_iovcnt = 1;
4034 auio.uio_rw = UIO_READ;
4035 auio.uio_segflg = bufseg;
4037 vn_lock(vp, LK_SHARED | LK_RETRY);
4038 AUDIT_ARG_VNODE1(vp);
4039 loff = auio.uio_offset = foffset;
4041 error = mac_vnode_check_readdir(td->td_ucred, vp);
4044 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL,
4046 foffset = auio.uio_offset;
4051 if (count == auio.uio_resid &&
4052 (vp->v_vflag & VV_ROOT) &&
4053 (vp->v_mount->mnt_flag & MNT_UNION)) {
4054 struct vnode *tvp = vp;
4056 vp = vp->v_mount->mnt_vnodecovered;
4067 *residp = auio.uio_resid;
4068 td->td_retval[0] = count - auio.uio_resid;
4070 foffset_unlock(fp, foffset, 0);
4075 #ifndef _SYS_SYSPROTO_H_
4076 struct getdents_args {
4083 sys_getdents(td, uap)
4085 register struct getdents_args /* {
4091 struct getdirentries_args ap;
4095 ap.count = uap->count;
4097 return (sys_getdirentries(td, &ap));
4101 * Set the mode mask for creation of filesystem nodes.
4103 #ifndef _SYS_SYSPROTO_H_
4111 struct umask_args /* {
4115 register struct filedesc *fdp;
4117 FILEDESC_XLOCK(td->td_proc->p_fd);
4118 fdp = td->td_proc->p_fd;
4119 td->td_retval[0] = fdp->fd_cmask;
4120 fdp->fd_cmask = uap->newmask & ALLPERMS;
4121 FILEDESC_XUNLOCK(td->td_proc->p_fd);
4126 * Void all references to file by ripping underlying filesystem away from
4129 #ifndef _SYS_SYSPROTO_H_
4130 struct revoke_args {
4137 register struct revoke_args /* {
4143 struct nameidata nd;
4146 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
4148 if ((error = namei(&nd)) != 0)
4151 NDFREE(&nd, NDF_ONLY_PNBUF);
4152 if (vp->v_type != VCHR || vp->v_rdev == NULL) {
4157 error = mac_vnode_check_revoke(td->td_ucred, vp);
4161 error = VOP_GETATTR(vp, &vattr, td->td_ucred);
4164 if (td->td_ucred->cr_uid != vattr.va_uid) {
4165 error = priv_check(td, PRIV_VFS_ADMIN);
4170 VOP_REVOKE(vp, REVOKEALL);
4177 * Convert a user file descriptor to a kernel file entry and check that, if it
4178 * is a capability, the correct rights are present. A reference on the file
4179 * entry is held upon returning.
4182 getvnode(struct filedesc *fdp, int fd, cap_rights_t *rightsp, struct file **fpp)
4187 error = fget_unlocked(fdp, fd, rightsp, 0, &fp, NULL);
4192 * The file could be not of the vnode type, or it may be not
4193 * yet fully initialized, in which case the f_vnode pointer
4194 * may be set, but f_ops is still badfileops. E.g.,
4195 * devfs_open() transiently create such situation to
4196 * facilitate csw d_fdopen().
4198 * Dupfdopen() handling in kern_openat() installs the
4199 * half-baked file into the process descriptor table, allowing
4200 * other thread to dereference it. Guard against the race by
4203 if (fp->f_vnode == NULL || fp->f_ops == &badfileops) {
4204 fdrop(fp, curthread);
4213 * Get an (NFS) file handle.
4215 #ifndef _SYS_SYSPROTO_H_
4216 struct lgetfh_args {
4224 register struct lgetfh_args *uap;
4226 struct nameidata nd;
4228 register struct vnode *vp;
4231 error = priv_check(td, PRIV_VFS_GETFH);
4234 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
4239 NDFREE(&nd, NDF_ONLY_PNBUF);
4241 bzero(&fh, sizeof(fh));
4242 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
4243 error = VOP_VPTOFH(vp, &fh.fh_fid);
4246 error = copyout(&fh, uap->fhp, sizeof (fh));
4250 #ifndef _SYS_SYSPROTO_H_
4259 register struct getfh_args *uap;
4261 struct nameidata nd;
4263 register struct vnode *vp;
4266 error = priv_check(td, PRIV_VFS_GETFH);
4269 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
4274 NDFREE(&nd, NDF_ONLY_PNBUF);
4276 bzero(&fh, sizeof(fh));
4277 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
4278 error = VOP_VPTOFH(vp, &fh.fh_fid);
4281 error = copyout(&fh, uap->fhp, sizeof (fh));
4286 * syscall for the rpc.lockd to use to translate a NFS file handle into an
4289 * warning: do not remove the priv_check() call or this becomes one giant
4292 #ifndef _SYS_SYSPROTO_H_
4293 struct fhopen_args {
4294 const struct fhandle *u_fhp;
4301 struct fhopen_args /* {
4302 const struct fhandle *u_fhp;
4313 error = priv_check(td, PRIV_VFS_FHOPEN);
4317 fmode = FFLAGS(uap->flags);
4318 /* why not allow a non-read/write open for our lockd? */
4319 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
4321 error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
4324 /* find the mount point */
4325 mp = vfs_busyfs(&fhp.fh_fsid);
4328 /* now give me my vnode, it gets returned to me locked */
4329 error = VFS_FHTOVP(mp, &fhp.fh_fid, LK_EXCLUSIVE, &vp);
4334 error = falloc_noinstall(td, &fp);
4340 * An extra reference on `fp' has been held for us by
4341 * falloc_noinstall().
4347 error = vn_open_vnode(vp, fmode, td->td_ucred, td, fp);
4349 KASSERT(fp->f_ops == &badfileops,
4350 ("VOP_OPEN in fhopen() set f_ops"));
4351 KASSERT(td->td_dupfd < 0,
4352 ("fhopen() encountered fdopen()"));
4362 finit(fp, (fmode & FMASK) | (fp->f_flag & FHASLOCK), DTYPE_VNODE, vp,
4365 if ((fmode & O_TRUNC) != 0) {
4366 error = fo_truncate(fp, 0, td->td_ucred, td);
4371 error = finstall(td, fp, &indx, fmode, NULL);
4374 td->td_retval[0] = indx;
4379 * Stat an (NFS) file handle.
4381 #ifndef _SYS_SYSPROTO_H_
4382 struct fhstat_args {
4383 struct fhandle *u_fhp;
4390 register struct fhstat_args /* {
4391 struct fhandle *u_fhp;
4399 error = copyin(uap->u_fhp, &fh, sizeof(fh));
4402 error = kern_fhstat(td, fh, &sb);
4404 error = copyout(&sb, uap->sb, sizeof(sb));
4409 kern_fhstat(struct thread *td, struct fhandle fh, struct stat *sb)
4415 error = priv_check(td, PRIV_VFS_FHSTAT);
4418 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4420 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_EXCLUSIVE, &vp);
4424 error = vn_stat(vp, sb, td->td_ucred, NOCRED, td);
4430 * Implement fstatfs() for (NFS) file handles.
4432 #ifndef _SYS_SYSPROTO_H_
4433 struct fhstatfs_args {
4434 struct fhandle *u_fhp;
4439 sys_fhstatfs(td, uap)
4441 struct fhstatfs_args /* {
4442 struct fhandle *u_fhp;
4450 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4453 error = kern_fhstatfs(td, fh, &sf);
4456 return (copyout(&sf, uap->buf, sizeof(sf)));
4460 kern_fhstatfs(struct thread *td, fhandle_t fh, struct statfs *buf)
4467 error = priv_check(td, PRIV_VFS_FHSTATFS);
4470 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4472 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_EXCLUSIVE, &vp);
4478 error = prison_canseemount(td->td_ucred, mp);
4482 error = mac_mount_check_stat(td->td_ucred, mp);
4487 * Set these in case the underlying filesystem fails to do so.
4490 sp->f_version = STATFS_VERSION;
4491 sp->f_namemax = NAME_MAX;
4492 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
4493 error = VFS_STATFS(mp, sp);
4502 kern_posix_fallocate(struct thread *td, int fd, off_t offset, off_t len)
4507 cap_rights_t rights;
4508 off_t olen, ooffset;
4512 error = fget(td, fd, cap_rights_init(&rights, CAP_WRITE), &fp);
4516 switch (fp->f_type) {
4527 if ((fp->f_flag & FWRITE) == 0) {
4532 if (vp->v_type != VREG) {
4536 if (offset < 0 || len <= 0) {
4540 /* Check for wrap. */
4541 if (offset > OFF_MAX - len) {
4546 /* Allocating blocks may take a long time, so iterate. */
4553 error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
4556 error = vn_lock(vp, LK_EXCLUSIVE);
4558 vn_finished_write(mp);
4562 error = mac_vnode_check_write(td->td_ucred, fp->f_cred, vp);
4565 error = VOP_ALLOCATE(vp, &offset, &len);
4567 vn_finished_write(mp);
4569 if (olen + ooffset != offset + len) {
4570 panic("offset + len changed from %jx/%jx to %jx/%jx",
4571 ooffset, olen, offset, len);
4573 if (error != 0 || len == 0)
4575 KASSERT(olen > len, ("Iteration did not make progress?"));
4585 sys_posix_fallocate(struct thread *td, struct posix_fallocate_args *uap)
4588 td->td_retval[0] = kern_posix_fallocate(td, uap->fd, uap->offset,
4594 * Unlike madvise(2), we do not make a best effort to remember every
4595 * possible caching hint. Instead, we remember the last setting with
4596 * the exception that we will allow POSIX_FADV_NORMAL to adjust the
4597 * region of any current setting.
4600 kern_posix_fadvise(struct thread *td, int fd, off_t offset, off_t len,
4603 struct fadvise_info *fa, *new;
4606 cap_rights_t rights;
4610 if (offset < 0 || len < 0 || offset > OFF_MAX - len)
4613 case POSIX_FADV_SEQUENTIAL:
4614 case POSIX_FADV_RANDOM:
4615 case POSIX_FADV_NOREUSE:
4616 new = malloc(sizeof(*fa), M_FADVISE, M_WAITOK);
4618 case POSIX_FADV_NORMAL:
4619 case POSIX_FADV_WILLNEED:
4620 case POSIX_FADV_DONTNEED:
4626 /* XXX: CAP_POSIX_FADVISE? */
4627 error = fget(td, fd, cap_rights_init(&rights), &fp);
4631 switch (fp->f_type) {
4643 if (vp->v_type != VREG) {
4650 end = offset + len - 1;
4652 case POSIX_FADV_SEQUENTIAL:
4653 case POSIX_FADV_RANDOM:
4654 case POSIX_FADV_NOREUSE:
4656 * Try to merge any existing non-standard region with
4657 * this new region if possible, otherwise create a new
4658 * non-standard region for this request.
4660 mtx_pool_lock(mtxpool_sleep, fp);
4662 if (fa != NULL && fa->fa_advice == advice &&
4663 ((fa->fa_start <= end && fa->fa_end >= offset) ||
4664 (end != OFF_MAX && fa->fa_start == end + 1) ||
4665 (fa->fa_end != OFF_MAX && fa->fa_end + 1 == offset))) {
4666 if (offset < fa->fa_start)
4667 fa->fa_start = offset;
4668 if (end > fa->fa_end)
4671 new->fa_advice = advice;
4672 new->fa_start = offset;
4674 new->fa_prevstart = 0;
4675 new->fa_prevend = 0;
4679 mtx_pool_unlock(mtxpool_sleep, fp);
4681 case POSIX_FADV_NORMAL:
4683 * If a the "normal" region overlaps with an existing
4684 * non-standard region, trim or remove the
4685 * non-standard region.
4687 mtx_pool_lock(mtxpool_sleep, fp);
4690 if (offset <= fa->fa_start && end >= fa->fa_end) {
4692 fp->f_advice = NULL;
4693 } else if (offset <= fa->fa_start &&
4694 end >= fa->fa_start)
4695 fa->fa_start = end + 1;
4696 else if (offset <= fa->fa_end && end >= fa->fa_end)
4697 fa->fa_end = offset - 1;
4698 else if (offset >= fa->fa_start && end <= fa->fa_end) {
4700 * If the "normal" region is a middle
4701 * portion of the existing
4702 * non-standard region, just remove
4703 * the whole thing rather than picking
4704 * one side or the other to
4708 fp->f_advice = NULL;
4711 mtx_pool_unlock(mtxpool_sleep, fp);
4713 case POSIX_FADV_WILLNEED:
4714 case POSIX_FADV_DONTNEED:
4715 error = VOP_ADVISE(vp, offset, end, advice);
4721 free(new, M_FADVISE);
4726 sys_posix_fadvise(struct thread *td, struct posix_fadvise_args *uap)
4729 td->td_retval[0] = kern_posix_fadvise(td, uap->fd, uap->offset,
4730 uap->len, uap->advice);