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/capsicum.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 * Sync each mounted filesystem.
121 #ifndef _SYS_SYSPROTO_H_
130 struct sync_args *uap;
132 struct mount *mp, *nmp;
135 mtx_lock(&mountlist_mtx);
136 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
137 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK)) {
138 nmp = TAILQ_NEXT(mp, mnt_list);
141 if ((mp->mnt_flag & MNT_RDONLY) == 0 &&
142 vn_start_write(NULL, &mp, V_NOWAIT) == 0) {
143 save = curthread_pflags_set(TDP_SYNCIO);
144 vfs_msync(mp, MNT_NOWAIT);
145 VFS_SYNC(mp, MNT_NOWAIT);
146 curthread_pflags_restore(save);
147 vn_finished_write(mp);
149 mtx_lock(&mountlist_mtx);
150 nmp = TAILQ_NEXT(mp, mnt_list);
153 mtx_unlock(&mountlist_mtx);
158 * Change filesystem quotas.
160 #ifndef _SYS_SYSPROTO_H_
161 struct quotactl_args {
169 sys_quotactl(td, uap)
171 register struct quotactl_args /* {
182 AUDIT_ARG_CMD(uap->cmd);
183 AUDIT_ARG_UID(uap->uid);
184 if (!prison_allow(td->td_ucred, PR_ALLOW_QUOTAS))
186 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
188 if ((error = namei(&nd)) != 0)
190 NDFREE(&nd, NDF_ONLY_PNBUF);
191 mp = nd.ni_vp->v_mount;
194 error = vfs_busy(mp, 0);
198 error = VFS_QUOTACTL(mp, uap->cmd, uap->uid, uap->arg);
201 * Since quota on operation typically needs to open quota
202 * file, the Q_QUOTAON handler needs to unbusy the mount point
203 * before calling into namei. Otherwise, unmount might be
204 * started between two vfs_busy() invocations (first is our,
205 * second is from mount point cross-walk code in lookup()),
208 * Require that Q_QUOTAON handles the vfs_busy() reference on
209 * its own, always returning with ubusied mount point.
211 if ((uap->cmd >> SUBCMDSHIFT) != Q_QUOTAON)
217 * Used by statfs conversion routines to scale the block size up if
218 * necessary so that all of the block counts are <= 'max_size'. Note
219 * that 'max_size' should be a bitmask, i.e. 2^n - 1 for some non-zero
223 statfs_scale_blocks(struct statfs *sf, long max_size)
228 KASSERT(powerof2(max_size + 1), ("%s: invalid max_size", __func__));
231 * Attempt to scale the block counts to give a more accurate
232 * overview to userland of the ratio of free space to used
233 * space. To do this, find the largest block count and compute
234 * a divisor that lets it fit into a signed integer <= max_size.
236 if (sf->f_bavail < 0)
237 count = -sf->f_bavail;
239 count = sf->f_bavail;
240 count = MAX(sf->f_blocks, MAX(sf->f_bfree, count));
241 if (count <= max_size)
244 count >>= flsl(max_size);
251 sf->f_bsize <<= shift;
252 sf->f_blocks >>= shift;
253 sf->f_bfree >>= shift;
254 sf->f_bavail >>= shift;
258 * Get filesystem statistics.
260 #ifndef _SYS_SYSPROTO_H_
269 register struct statfs_args /* {
277 error = kern_statfs(td, uap->path, UIO_USERSPACE, &sf);
279 error = copyout(&sf, uap->buf, sizeof(sf));
284 kern_statfs(struct thread *td, char *path, enum uio_seg pathseg,
288 struct statfs *sp, sb;
292 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
297 mp = nd.ni_vp->v_mount;
299 NDFREE(&nd, NDF_ONLY_PNBUF);
301 error = vfs_busy(mp, 0);
306 error = mac_mount_check_stat(td->td_ucred, mp);
311 * Set these in case the underlying filesystem fails to do so.
314 sp->f_version = STATFS_VERSION;
315 sp->f_namemax = NAME_MAX;
316 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
317 error = VFS_STATFS(mp, sp);
320 if (priv_check(td, PRIV_VFS_GENERATION)) {
321 bcopy(sp, &sb, sizeof(sb));
322 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
323 prison_enforce_statfs(td->td_ucred, mp, &sb);
333 * Get filesystem statistics.
335 #ifndef _SYS_SYSPROTO_H_
336 struct fstatfs_args {
344 register struct fstatfs_args /* {
352 error = kern_fstatfs(td, uap->fd, &sf);
354 error = copyout(&sf, uap->buf, sizeof(sf));
359 kern_fstatfs(struct thread *td, int fd, struct statfs *buf)
363 struct statfs *sp, sb;
369 error = getvnode(td->td_proc->p_fd, fd,
370 cap_rights_init(&rights, CAP_FSTATFS), &fp);
374 vn_lock(vp, LK_SHARED | LK_RETRY);
376 AUDIT_ARG_VNODE1(vp);
387 error = vfs_busy(mp, 0);
392 error = mac_mount_check_stat(td->td_ucred, mp);
397 * Set these in case the underlying filesystem fails to do so.
400 sp->f_version = STATFS_VERSION;
401 sp->f_namemax = NAME_MAX;
402 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
403 error = VFS_STATFS(mp, sp);
406 if (priv_check(td, PRIV_VFS_GENERATION)) {
407 bcopy(sp, &sb, sizeof(sb));
408 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
409 prison_enforce_statfs(td->td_ucred, mp, &sb);
420 * Get statistics on all filesystems.
422 #ifndef _SYS_SYSPROTO_H_
423 struct getfsstat_args {
430 sys_getfsstat(td, uap)
432 register struct getfsstat_args /* {
439 return (kern_getfsstat(td, &uap->buf, uap->bufsize, UIO_USERSPACE,
444 * If (bufsize > 0 && bufseg == UIO_SYSSPACE)
445 * The caller is responsible for freeing memory which will be allocated
449 kern_getfsstat(struct thread *td, struct statfs **buf, size_t bufsize,
450 enum uio_seg bufseg, int flags)
452 struct mount *mp, *nmp;
453 struct statfs *sfsp, *sp, sb;
454 size_t count, maxcount;
457 maxcount = bufsize / sizeof(struct statfs);
460 else if (bufseg == UIO_USERSPACE)
462 else /* if (bufseg == UIO_SYSSPACE) */ {
464 mtx_lock(&mountlist_mtx);
465 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
468 mtx_unlock(&mountlist_mtx);
469 if (maxcount > count)
471 sfsp = *buf = malloc(maxcount * sizeof(struct statfs), M_TEMP,
475 mtx_lock(&mountlist_mtx);
476 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
477 if (prison_canseemount(td->td_ucred, mp) != 0) {
478 nmp = TAILQ_NEXT(mp, mnt_list);
482 if (mac_mount_check_stat(td->td_ucred, mp) != 0) {
483 nmp = TAILQ_NEXT(mp, mnt_list);
487 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK)) {
488 nmp = TAILQ_NEXT(mp, mnt_list);
491 if (sfsp && count < maxcount) {
494 * Set these in case the underlying filesystem
497 sp->f_version = STATFS_VERSION;
498 sp->f_namemax = NAME_MAX;
499 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
501 * If MNT_NOWAIT or MNT_LAZY is specified, do not
502 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
503 * overrides MNT_WAIT.
505 if (((flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
506 (flags & MNT_WAIT)) &&
507 (error = VFS_STATFS(mp, sp))) {
508 mtx_lock(&mountlist_mtx);
509 nmp = TAILQ_NEXT(mp, mnt_list);
513 if (priv_check(td, PRIV_VFS_GENERATION)) {
514 bcopy(sp, &sb, sizeof(sb));
515 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
516 prison_enforce_statfs(td->td_ucred, mp, &sb);
519 if (bufseg == UIO_SYSSPACE)
520 bcopy(sp, sfsp, sizeof(*sp));
521 else /* if (bufseg == UIO_USERSPACE) */ {
522 error = copyout(sp, sfsp, sizeof(*sp));
531 mtx_lock(&mountlist_mtx);
532 nmp = TAILQ_NEXT(mp, mnt_list);
535 mtx_unlock(&mountlist_mtx);
536 if (sfsp && count > maxcount)
537 td->td_retval[0] = maxcount;
539 td->td_retval[0] = count;
543 #ifdef COMPAT_FREEBSD4
545 * Get old format filesystem statistics.
547 static void cvtstatfs(struct statfs *, struct ostatfs *);
549 #ifndef _SYS_SYSPROTO_H_
550 struct freebsd4_statfs_args {
556 freebsd4_statfs(td, uap)
558 struct freebsd4_statfs_args /* {
567 error = kern_statfs(td, uap->path, UIO_USERSPACE, &sf);
570 cvtstatfs(&sf, &osb);
571 return (copyout(&osb, uap->buf, sizeof(osb)));
575 * Get filesystem statistics.
577 #ifndef _SYS_SYSPROTO_H_
578 struct freebsd4_fstatfs_args {
584 freebsd4_fstatfs(td, uap)
586 struct freebsd4_fstatfs_args /* {
595 error = kern_fstatfs(td, uap->fd, &sf);
598 cvtstatfs(&sf, &osb);
599 return (copyout(&osb, uap->buf, sizeof(osb)));
603 * Get statistics on all filesystems.
605 #ifndef _SYS_SYSPROTO_H_
606 struct freebsd4_getfsstat_args {
613 freebsd4_getfsstat(td, uap)
615 register struct freebsd4_getfsstat_args /* {
621 struct statfs *buf, *sp;
626 count = uap->bufsize / sizeof(struct ostatfs);
627 size = count * sizeof(struct statfs);
628 error = kern_getfsstat(td, &buf, size, UIO_SYSSPACE, uap->flags);
630 count = td->td_retval[0];
632 while (count > 0 && error == 0) {
634 error = copyout(&osb, uap->buf, sizeof(osb));
645 * Implement fstatfs() for (NFS) file handles.
647 #ifndef _SYS_SYSPROTO_H_
648 struct freebsd4_fhstatfs_args {
649 struct fhandle *u_fhp;
654 freebsd4_fhstatfs(td, uap)
656 struct freebsd4_fhstatfs_args /* {
657 struct fhandle *u_fhp;
666 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
669 error = kern_fhstatfs(td, fh, &sf);
672 cvtstatfs(&sf, &osb);
673 return (copyout(&osb, uap->buf, sizeof(osb)));
677 * Convert a new format statfs structure to an old format statfs structure.
685 statfs_scale_blocks(nsp, LONG_MAX);
686 bzero(osp, sizeof(*osp));
687 osp->f_bsize = nsp->f_bsize;
688 osp->f_iosize = MIN(nsp->f_iosize, LONG_MAX);
689 osp->f_blocks = nsp->f_blocks;
690 osp->f_bfree = nsp->f_bfree;
691 osp->f_bavail = nsp->f_bavail;
692 osp->f_files = MIN(nsp->f_files, LONG_MAX);
693 osp->f_ffree = MIN(nsp->f_ffree, LONG_MAX);
694 osp->f_owner = nsp->f_owner;
695 osp->f_type = nsp->f_type;
696 osp->f_flags = nsp->f_flags;
697 osp->f_syncwrites = MIN(nsp->f_syncwrites, LONG_MAX);
698 osp->f_asyncwrites = MIN(nsp->f_asyncwrites, LONG_MAX);
699 osp->f_syncreads = MIN(nsp->f_syncreads, LONG_MAX);
700 osp->f_asyncreads = MIN(nsp->f_asyncreads, LONG_MAX);
701 strlcpy(osp->f_fstypename, nsp->f_fstypename,
702 MIN(MFSNAMELEN, OMFSNAMELEN));
703 strlcpy(osp->f_mntonname, nsp->f_mntonname,
704 MIN(MNAMELEN, OMNAMELEN));
705 strlcpy(osp->f_mntfromname, nsp->f_mntfromname,
706 MIN(MNAMELEN, OMNAMELEN));
707 osp->f_fsid = nsp->f_fsid;
709 #endif /* COMPAT_FREEBSD4 */
712 * Change current working directory to a given file descriptor.
714 #ifndef _SYS_SYSPROTO_H_
722 struct fchdir_args /* {
726 register struct filedesc *fdp = td->td_proc->p_fd;
727 struct vnode *vp, *tdp, *vpold;
733 AUDIT_ARG_FD(uap->fd);
734 error = getvnode(fdp, uap->fd, cap_rights_init(&rights, CAP_FCHDIR),
741 vn_lock(vp, LK_SHARED | LK_RETRY);
742 AUDIT_ARG_VNODE1(vp);
743 error = change_dir(vp, td);
744 while (!error && (mp = vp->v_mountedhere) != NULL) {
747 error = VFS_ROOT(mp, LK_SHARED, &tdp);
760 vpold = fdp->fd_cdir;
762 FILEDESC_XUNLOCK(fdp);
768 * Change current working directory (``.'').
770 #ifndef _SYS_SYSPROTO_H_
778 struct chdir_args /* {
783 return (kern_chdir(td, uap->path, UIO_USERSPACE));
787 kern_chdir(struct thread *td, char *path, enum uio_seg pathseg)
789 register struct filedesc *fdp = td->td_proc->p_fd;
794 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
796 if ((error = namei(&nd)) != 0)
798 if ((error = change_dir(nd.ni_vp, td)) != 0) {
800 NDFREE(&nd, NDF_ONLY_PNBUF);
803 VOP_UNLOCK(nd.ni_vp, 0);
804 NDFREE(&nd, NDF_ONLY_PNBUF);
807 fdp->fd_cdir = nd.ni_vp;
808 FILEDESC_XUNLOCK(fdp);
814 * Helper function for raised chroot(2) security function: Refuse if
815 * any filedescriptors are open directories.
818 chroot_refuse_vdir_fds(fdp)
819 struct filedesc *fdp;
825 FILEDESC_LOCK_ASSERT(fdp);
827 for (fd = 0; fd <= fdp->fd_lastfile; fd++) {
828 fp = fget_locked(fdp, fd);
831 if (fp->f_type == DTYPE_VNODE) {
833 if (vp->v_type == VDIR)
841 * This sysctl determines if we will allow a process to chroot(2) if it
842 * has a directory open:
843 * 0: disallowed for all processes.
844 * 1: allowed for processes that were not already chroot(2)'ed.
845 * 2: allowed for all processes.
848 static int chroot_allow_open_directories = 1;
850 SYSCTL_INT(_kern, OID_AUTO, chroot_allow_open_directories, CTLFLAG_RW,
851 &chroot_allow_open_directories, 0,
852 "Allow a process to chroot(2) if it has a directory open");
855 * Change notion of root (``/'') directory.
857 #ifndef _SYS_SYSPROTO_H_
865 struct chroot_args /* {
872 error = priv_check(td, PRIV_VFS_CHROOT);
875 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
876 UIO_USERSPACE, uap->path, td);
880 error = change_dir(nd.ni_vp, td);
884 error = mac_vnode_check_chroot(td->td_ucred, nd.ni_vp);
888 VOP_UNLOCK(nd.ni_vp, 0);
889 error = change_root(nd.ni_vp, td);
891 NDFREE(&nd, NDF_ONLY_PNBUF);
896 NDFREE(&nd, NDF_ONLY_PNBUF);
901 * 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 return (VOP_ACCESS(vp, VEXEC, td->td_ucred, td));
925 * Common routine for kern_chroot() and jail_attach(). The caller is
926 * responsible for invoking priv_check() and mac_vnode_check_chroot() to
927 * authorize this operation.
934 struct filedesc *fdp;
938 fdp = td->td_proc->p_fd;
940 if (chroot_allow_open_directories == 0 ||
941 (chroot_allow_open_directories == 1 && fdp->fd_rdir != rootvnode)) {
942 error = chroot_refuse_vdir_fds(fdp);
944 FILEDESC_XUNLOCK(fdp);
948 oldvp = fdp->fd_rdir;
955 FILEDESC_XUNLOCK(fdp);
961 flags_to_rights(int flags, cap_rights_t *rightsp)
964 if (flags & O_EXEC) {
965 cap_rights_set(rightsp, CAP_FEXECVE);
967 switch ((flags & O_ACCMODE)) {
969 cap_rights_set(rightsp, CAP_READ);
972 cap_rights_set(rightsp, CAP_READ);
975 cap_rights_set(rightsp, CAP_WRITE);
976 if (!(flags & (O_APPEND | O_TRUNC)))
977 cap_rights_set(rightsp, CAP_SEEK);
983 cap_rights_set(rightsp, CAP_CREATE);
986 cap_rights_set(rightsp, CAP_FTRUNCATE);
988 if (flags & (O_SYNC | O_FSYNC))
989 cap_rights_set(rightsp, CAP_FSYNC);
991 if (flags & (O_EXLOCK | O_SHLOCK))
992 cap_rights_set(rightsp, CAP_FLOCK);
996 * Check permissions, allocate an open file structure, and call the device
997 * open routine if any.
999 #ifndef _SYS_SYSPROTO_H_
1009 register struct open_args /* {
1016 return (kern_open(td, uap->path, UIO_USERSPACE, uap->flags, uap->mode));
1019 #ifndef _SYS_SYSPROTO_H_
1020 struct openat_args {
1028 sys_openat(struct thread *td, struct openat_args *uap)
1031 return (kern_openat(td, uap->fd, uap->path, UIO_USERSPACE, uap->flag,
1036 kern_open(struct thread *td, char *path, enum uio_seg pathseg, int flags,
1040 return (kern_openat(td, AT_FDCWD, path, pathseg, flags, mode));
1044 kern_openat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1045 int flags, int mode)
1047 struct proc *p = td->td_proc;
1048 struct filedesc *fdp = p->p_fd;
1051 struct nameidata nd;
1052 cap_rights_t rights;
1053 int cmode, error, indx;
1057 AUDIT_ARG_FFLAGS(flags);
1058 AUDIT_ARG_MODE(mode);
1059 /* XXX: audit dirfd */
1060 cap_rights_init(&rights, CAP_LOOKUP);
1061 flags_to_rights(flags, &rights);
1063 * Only one of the O_EXEC, O_RDONLY, O_WRONLY and O_RDWR flags
1066 if (flags & O_EXEC) {
1067 if (flags & O_ACCMODE)
1069 } else if ((flags & O_ACCMODE) == O_ACCMODE) {
1072 flags = FFLAGS(flags);
1076 * Allocate the file descriptor, but don't install a descriptor yet.
1078 error = falloc_noinstall(td, &fp);
1082 * An extra reference on `fp' has been held for us by
1083 * falloc_noinstall().
1085 /* Set the flags early so the finit in devfs can pick them up. */
1086 fp->f_flag = flags & FMASK;
1087 cmode = ((mode & ~fdp->fd_cmask) & ALLPERMS) & ~S_ISTXT;
1088 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, fd,
1090 td->td_dupfd = -1; /* XXX check for fdopen */
1091 error = vn_open(&nd, &flags, cmode, fp);
1094 * If the vn_open replaced the method vector, something
1095 * wonderous happened deep below and we just pass it up
1096 * pretending we know what we do.
1098 if (error == ENXIO && fp->f_ops != &badfileops)
1102 * Handle special fdopen() case. bleh.
1104 * Don't do this for relative (capability) lookups; we don't
1105 * understand exactly what would happen, and we don't think
1106 * that it ever should.
1108 if (nd.ni_strictrelative == 0 &&
1109 (error == ENODEV || error == ENXIO) &&
1110 td->td_dupfd >= 0) {
1111 error = dupfdopen(td, fdp, td->td_dupfd, flags, error,
1120 NDFREE(&nd, NDF_ONLY_PNBUF);
1124 * Store the vnode, for any f_type. Typically, the vnode use
1125 * count is decremented by direct call to vn_closefile() for
1126 * files that switched type in the cdevsw fdopen() method.
1130 * If the file wasn't claimed by devfs bind it to the normal
1131 * vnode operations here.
1133 if (fp->f_ops == &badfileops) {
1134 KASSERT(vp->v_type != VFIFO, ("Unexpected fifo."));
1136 finit(fp, (flags & FMASK) | (fp->f_flag & FHASLOCK),
1137 DTYPE_VNODE, vp, &vnops);
1141 if (flags & O_TRUNC) {
1142 error = fo_truncate(fp, 0, td->td_ucred, td);
1148 * If we haven't already installed the FD (for dupfdopen), do so now.
1151 struct filecaps *fcaps;
1154 if (nd.ni_strictrelative == 1)
1155 fcaps = &nd.ni_filecaps;
1159 error = finstall(td, fp, &indx, flags, fcaps);
1160 /* On success finstall() consumes fcaps. */
1162 filecaps_free(&nd.ni_filecaps);
1166 filecaps_free(&nd.ni_filecaps);
1170 * Release our private reference, leaving the one associated with
1171 * the descriptor table intact.
1174 td->td_retval[0] = indx;
1177 KASSERT(indx == -1, ("indx=%d, should be -1", indx));
1186 #ifndef _SYS_SYSPROTO_H_
1187 struct ocreat_args {
1195 register struct ocreat_args /* {
1201 return (kern_open(td, uap->path, UIO_USERSPACE,
1202 O_WRONLY | O_CREAT | O_TRUNC, uap->mode));
1204 #endif /* COMPAT_43 */
1207 * Create a special file.
1209 #ifndef _SYS_SYSPROTO_H_
1219 register struct mknod_args /* {
1226 return (kern_mknod(td, uap->path, UIO_USERSPACE, uap->mode, uap->dev));
1229 #ifndef _SYS_SYSPROTO_H_
1230 struct mknodat_args {
1238 sys_mknodat(struct thread *td, struct mknodat_args *uap)
1241 return (kern_mknodat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode,
1246 kern_mknod(struct thread *td, char *path, enum uio_seg pathseg, int mode,
1250 return (kern_mknodat(td, AT_FDCWD, path, pathseg, mode, dev));
1254 kern_mknodat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1260 struct nameidata nd;
1261 cap_rights_t rights;
1262 int error, whiteout = 0;
1264 AUDIT_ARG_MODE(mode);
1266 switch (mode & S_IFMT) {
1269 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
1272 error = priv_check(td, PRIV_VFS_MKNOD_BAD);
1275 error = priv_check(td, PRIV_VFS_MKNOD_WHT);
1279 return (kern_mkfifoat(td, fd, path, pathseg, mode));
1289 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
1290 NOCACHE, pathseg, path, fd, cap_rights_init(&rights, CAP_MKNODAT),
1292 if ((error = namei(&nd)) != 0)
1296 NDFREE(&nd, NDF_ONLY_PNBUF);
1297 if (vp == nd.ni_dvp)
1305 vattr.va_mode = (mode & ALLPERMS) &
1306 ~td->td_proc->p_fd->fd_cmask;
1307 vattr.va_rdev = dev;
1310 switch (mode & S_IFMT) {
1311 case S_IFMT: /* used by badsect to flag bad sectors */
1312 vattr.va_type = VBAD;
1315 vattr.va_type = VCHR;
1318 vattr.va_type = VBLK;
1324 panic("kern_mknod: invalid mode");
1327 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1328 NDFREE(&nd, NDF_ONLY_PNBUF);
1330 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1335 if (error == 0 && !whiteout)
1336 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp,
1337 &nd.ni_cnd, &vattr);
1341 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, CREATE);
1343 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp,
1344 &nd.ni_cnd, &vattr);
1349 NDFREE(&nd, NDF_ONLY_PNBUF);
1351 vn_finished_write(mp);
1356 * Create a named pipe.
1358 #ifndef _SYS_SYSPROTO_H_
1359 struct mkfifo_args {
1367 register struct mkfifo_args /* {
1373 return (kern_mkfifo(td, uap->path, UIO_USERSPACE, uap->mode));
1376 #ifndef _SYS_SYSPROTO_H_
1377 struct mkfifoat_args {
1384 sys_mkfifoat(struct thread *td, struct mkfifoat_args *uap)
1387 return (kern_mkfifoat(td, uap->fd, uap->path, UIO_USERSPACE,
1392 kern_mkfifo(struct thread *td, char *path, enum uio_seg pathseg, int mode)
1395 return (kern_mkfifoat(td, AT_FDCWD, path, pathseg, mode));
1399 kern_mkfifoat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1404 struct nameidata nd;
1405 cap_rights_t rights;
1408 AUDIT_ARG_MODE(mode);
1411 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
1412 NOCACHE, pathseg, path, fd, cap_rights_init(&rights, CAP_MKFIFOAT),
1414 if ((error = namei(&nd)) != 0)
1416 if (nd.ni_vp != NULL) {
1417 NDFREE(&nd, NDF_ONLY_PNBUF);
1418 if (nd.ni_vp == nd.ni_dvp)
1425 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1426 NDFREE(&nd, NDF_ONLY_PNBUF);
1428 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1433 vattr.va_type = VFIFO;
1434 vattr.va_mode = (mode & ALLPERMS) & ~td->td_proc->p_fd->fd_cmask;
1436 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1441 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
1448 vn_finished_write(mp);
1449 NDFREE(&nd, NDF_ONLY_PNBUF);
1454 * Make a hard file link.
1456 #ifndef _SYS_SYSPROTO_H_
1465 register struct link_args /* {
1471 return (kern_link(td, uap->path, uap->link, UIO_USERSPACE));
1474 #ifndef _SYS_SYSPROTO_H_
1475 struct linkat_args {
1484 sys_linkat(struct thread *td, struct linkat_args *uap)
1489 if (flag & ~AT_SYMLINK_FOLLOW)
1492 return (kern_linkat(td, uap->fd1, uap->fd2, uap->path1, uap->path2,
1493 UIO_USERSPACE, (flag & AT_SYMLINK_FOLLOW) ? FOLLOW : NOFOLLOW));
1496 int hardlink_check_uid = 0;
1497 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
1498 &hardlink_check_uid, 0,
1499 "Unprivileged processes cannot create hard links to files owned by other "
1501 static int hardlink_check_gid = 0;
1502 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
1503 &hardlink_check_gid, 0,
1504 "Unprivileged processes cannot create hard links to files owned by other "
1508 can_hardlink(struct vnode *vp, struct ucred *cred)
1513 if (!hardlink_check_uid && !hardlink_check_gid)
1516 error = VOP_GETATTR(vp, &va, cred);
1520 if (hardlink_check_uid && cred->cr_uid != va.va_uid) {
1521 error = priv_check_cred(cred, PRIV_VFS_LINK, 0);
1526 if (hardlink_check_gid && !groupmember(va.va_gid, cred)) {
1527 error = priv_check_cred(cred, PRIV_VFS_LINK, 0);
1536 kern_link(struct thread *td, char *path, char *link, enum uio_seg segflg)
1539 return (kern_linkat(td, AT_FDCWD, AT_FDCWD, path,link, segflg, FOLLOW));
1543 kern_linkat(struct thread *td, int fd1, int fd2, char *path1, char *path2,
1544 enum uio_seg segflg, int follow)
1548 struct nameidata nd;
1549 cap_rights_t rights;
1554 NDINIT_AT(&nd, LOOKUP, follow | AUDITVNODE1, segflg, path1, fd1, td);
1556 if ((error = namei(&nd)) != 0)
1558 NDFREE(&nd, NDF_ONLY_PNBUF);
1560 if (vp->v_type == VDIR) {
1562 return (EPERM); /* POSIX */
1564 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE2 |
1565 NOCACHE, segflg, path2, fd2, cap_rights_init(&rights, CAP_LINKAT),
1567 if ((error = namei(&nd)) == 0) {
1568 if (nd.ni_vp != NULL) {
1569 NDFREE(&nd, NDF_ONLY_PNBUF);
1570 if (nd.ni_dvp == nd.ni_vp)
1577 } else if (nd.ni_dvp->v_mount != vp->v_mount) {
1579 * Cross-device link. No need to recheck
1580 * vp->v_type, since it cannot change, except
1583 NDFREE(&nd, NDF_ONLY_PNBUF);
1587 } else if ((error = vn_lock(vp, LK_EXCLUSIVE)) == 0) {
1588 error = can_hardlink(vp, td->td_ucred);
1591 error = mac_vnode_check_link(td->td_ucred,
1592 nd.ni_dvp, vp, &nd.ni_cnd);
1597 NDFREE(&nd, NDF_ONLY_PNBUF);
1600 error = vn_start_write(vp, &mp, V_NOWAIT);
1604 NDFREE(&nd, NDF_ONLY_PNBUF);
1605 error = vn_start_write(NULL, &mp,
1611 error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
1614 vn_finished_write(mp);
1615 NDFREE(&nd, NDF_ONLY_PNBUF);
1618 NDFREE(&nd, NDF_ONLY_PNBUF);
1628 * Make a symbolic link.
1630 #ifndef _SYS_SYSPROTO_H_
1631 struct symlink_args {
1637 sys_symlink(td, uap)
1639 register struct symlink_args /* {
1645 return (kern_symlink(td, uap->path, uap->link, UIO_USERSPACE));
1648 #ifndef _SYS_SYSPROTO_H_
1649 struct symlinkat_args {
1656 sys_symlinkat(struct thread *td, struct symlinkat_args *uap)
1659 return (kern_symlinkat(td, uap->path1, uap->fd, uap->path2,
1664 kern_symlink(struct thread *td, char *path, char *link, enum uio_seg segflg)
1667 return (kern_symlinkat(td, path, AT_FDCWD, link, segflg));
1671 kern_symlinkat(struct thread *td, char *path1, int fd, char *path2,
1672 enum uio_seg segflg)
1677 struct nameidata nd;
1679 cap_rights_t rights;
1681 if (segflg == UIO_SYSSPACE) {
1684 syspath = uma_zalloc(namei_zone, M_WAITOK);
1685 if ((error = copyinstr(path1, syspath, MAXPATHLEN, NULL)) != 0)
1688 AUDIT_ARG_TEXT(syspath);
1691 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
1692 NOCACHE, segflg, path2, fd, cap_rights_init(&rights, CAP_SYMLINKAT),
1694 if ((error = namei(&nd)) != 0)
1697 NDFREE(&nd, NDF_ONLY_PNBUF);
1698 if (nd.ni_vp == nd.ni_dvp)
1706 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1707 NDFREE(&nd, NDF_ONLY_PNBUF);
1709 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1714 vattr.va_mode = ACCESSPERMS &~ td->td_proc->p_fd->fd_cmask;
1716 vattr.va_type = VLNK;
1717 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1722 error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr, syspath);
1728 NDFREE(&nd, NDF_ONLY_PNBUF);
1730 vn_finished_write(mp);
1732 if (segflg != UIO_SYSSPACE)
1733 uma_zfree(namei_zone, syspath);
1738 * Delete a whiteout from the filesystem.
1741 sys_undelete(td, uap)
1743 register struct undelete_args /* {
1748 struct nameidata nd;
1753 NDINIT(&nd, DELETE, LOCKPARENT | DOWHITEOUT | AUDITVNODE1,
1754 UIO_USERSPACE, uap->path, td);
1759 if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & ISWHITEOUT)) {
1760 NDFREE(&nd, NDF_ONLY_PNBUF);
1761 if (nd.ni_vp == nd.ni_dvp)
1769 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1770 NDFREE(&nd, NDF_ONLY_PNBUF);
1772 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1776 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, DELETE);
1777 NDFREE(&nd, NDF_ONLY_PNBUF);
1779 vn_finished_write(mp);
1784 * Delete a name from the filesystem.
1786 #ifndef _SYS_SYSPROTO_H_
1787 struct unlink_args {
1794 struct unlink_args /* {
1799 return (kern_unlink(td, uap->path, UIO_USERSPACE));
1802 #ifndef _SYS_SYSPROTO_H_
1803 struct unlinkat_args {
1810 sys_unlinkat(struct thread *td, struct unlinkat_args *uap)
1812 int flag = uap->flag;
1814 char *path = uap->path;
1816 if (flag & ~AT_REMOVEDIR)
1819 if (flag & AT_REMOVEDIR)
1820 return (kern_rmdirat(td, fd, path, UIO_USERSPACE));
1822 return (kern_unlinkat(td, fd, path, UIO_USERSPACE, 0));
1826 kern_unlink(struct thread *td, char *path, enum uio_seg pathseg)
1829 return (kern_unlinkat(td, AT_FDCWD, path, pathseg, 0));
1833 kern_unlinkat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1838 struct nameidata nd;
1840 cap_rights_t rights;
1845 NDINIT_ATRIGHTS(&nd, DELETE, LOCKPARENT | LOCKLEAF | AUDITVNODE1,
1846 pathseg, path, fd, cap_rights_init(&rights, CAP_UNLINKAT), td);
1847 if ((error = namei(&nd)) != 0)
1848 return (error == EINVAL ? EPERM : error);
1850 if (vp->v_type == VDIR && oldinum == 0) {
1851 error = EPERM; /* POSIX */
1852 } else if (oldinum != 0 &&
1853 ((error = vn_stat(vp, &sb, td->td_ucred, NOCRED, td)) == 0) &&
1854 sb.st_ino != oldinum) {
1855 error = EIDRM; /* Identifier removed */
1858 * The root of a mounted filesystem cannot be deleted.
1860 * XXX: can this only be a VDIR case?
1862 if (vp->v_vflag & VV_ROOT)
1866 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1867 NDFREE(&nd, NDF_ONLY_PNBUF);
1869 if (vp == nd.ni_dvp)
1873 if ((error = vn_start_write(NULL, &mp,
1874 V_XSLEEP | PCATCH)) != 0)
1879 error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp,
1884 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_UNLINK);
1885 error = VOP_REMOVE(nd.ni_dvp, vp, &nd.ni_cnd);
1889 vn_finished_write(mp);
1891 NDFREE(&nd, NDF_ONLY_PNBUF);
1893 if (vp == nd.ni_dvp)
1901 * Reposition read/write file offset.
1903 #ifndef _SYS_SYSPROTO_H_
1914 register struct lseek_args /* {
1922 cap_rights_t rights;
1925 AUDIT_ARG_FD(uap->fd);
1926 error = fget(td, uap->fd, cap_rights_init(&rights, CAP_SEEK), &fp);
1929 error = (fp->f_ops->fo_flags & DFLAG_SEEKABLE) != 0 ?
1930 fo_seek(fp, uap->offset, uap->whence, td) : ESPIPE;
1935 #if defined(COMPAT_43)
1937 * Reposition read/write file offset.
1939 #ifndef _SYS_SYSPROTO_H_
1940 struct olseek_args {
1949 register struct olseek_args /* {
1955 struct lseek_args /* {
1963 nuap.offset = uap->offset;
1964 nuap.whence = uap->whence;
1965 return (sys_lseek(td, &nuap));
1967 #endif /* COMPAT_43 */
1969 /* Version with the 'pad' argument */
1971 freebsd6_lseek(td, uap)
1973 register struct freebsd6_lseek_args *uap;
1975 struct lseek_args ouap;
1978 ouap.offset = uap->offset;
1979 ouap.whence = uap->whence;
1980 return (sys_lseek(td, &ouap));
1984 * Check access permissions using passed credentials.
1987 vn_access(vp, user_flags, cred, td)
1996 /* Flags == 0 means only check for existence. */
2000 if (user_flags & R_OK)
2002 if (user_flags & W_OK)
2004 if (user_flags & X_OK)
2007 error = mac_vnode_check_access(cred, vp, accmode);
2011 if ((accmode & VWRITE) == 0 || (error = vn_writechk(vp)) == 0)
2012 error = VOP_ACCESS(vp, accmode, cred, td);
2018 * Check access permissions using "real" credentials.
2020 #ifndef _SYS_SYSPROTO_H_
2021 struct access_args {
2029 register struct access_args /* {
2035 return (kern_access(td, uap->path, UIO_USERSPACE, uap->amode));
2038 #ifndef _SYS_SYSPROTO_H_
2039 struct faccessat_args {
2047 sys_faccessat(struct thread *td, struct faccessat_args *uap)
2050 if (uap->flag & ~AT_EACCESS)
2052 return (kern_accessat(td, uap->fd, uap->path, UIO_USERSPACE, uap->flag,
2057 kern_access(struct thread *td, char *path, enum uio_seg pathseg, int amode)
2060 return (kern_accessat(td, AT_FDCWD, path, pathseg, 0, amode));
2064 kern_accessat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2065 int flag, int amode)
2067 struct ucred *cred, *tmpcred;
2069 struct nameidata nd;
2070 cap_rights_t rights;
2074 * Create and modify a temporary credential instead of one that
2075 * is potentially shared.
2077 if (!(flag & AT_EACCESS)) {
2078 cred = td->td_ucred;
2079 tmpcred = crdup(cred);
2080 tmpcred->cr_uid = cred->cr_ruid;
2081 tmpcred->cr_groups[0] = cred->cr_rgid;
2082 td->td_ucred = tmpcred;
2084 cred = tmpcred = td->td_ucred;
2085 AUDIT_ARG_VALUE(amode);
2086 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF |
2087 AUDITVNODE1, pathseg, path, fd, cap_rights_init(&rights, CAP_FSTAT),
2089 if ((error = namei(&nd)) != 0)
2093 error = vn_access(vp, amode, tmpcred, td);
2094 NDFREE(&nd, NDF_ONLY_PNBUF);
2097 if (!(flag & AT_EACCESS)) {
2098 td->td_ucred = cred;
2105 * Check access permissions using "effective" credentials.
2107 #ifndef _SYS_SYSPROTO_H_
2108 struct eaccess_args {
2114 sys_eaccess(td, uap)
2116 register struct eaccess_args /* {
2122 return (kern_eaccess(td, uap->path, UIO_USERSPACE, uap->amode));
2126 kern_eaccess(struct thread *td, char *path, enum uio_seg pathseg, int amode)
2129 return (kern_accessat(td, AT_FDCWD, path, pathseg, AT_EACCESS, amode));
2132 #if defined(COMPAT_43)
2134 * Get file status; this version follows links.
2136 #ifndef _SYS_SYSPROTO_H_
2145 register struct ostat_args /* {
2154 error = kern_stat(td, uap->path, UIO_USERSPACE, &sb);
2158 return (copyout(&osb, uap->ub, sizeof (osb)));
2162 * Get file status; this version does not follow links.
2164 #ifndef _SYS_SYSPROTO_H_
2165 struct olstat_args {
2173 register struct olstat_args /* {
2182 error = kern_lstat(td, uap->path, UIO_USERSPACE, &sb);
2186 return (copyout(&osb, uap->ub, sizeof (osb)));
2190 * Convert from an old to a new stat structure.
2198 ost->st_dev = st->st_dev;
2199 ost->st_ino = st->st_ino;
2200 ost->st_mode = st->st_mode;
2201 ost->st_nlink = st->st_nlink;
2202 ost->st_uid = st->st_uid;
2203 ost->st_gid = st->st_gid;
2204 ost->st_rdev = st->st_rdev;
2205 if (st->st_size < (quad_t)1 << 32)
2206 ost->st_size = st->st_size;
2209 ost->st_atim = st->st_atim;
2210 ost->st_mtim = st->st_mtim;
2211 ost->st_ctim = st->st_ctim;
2212 ost->st_blksize = st->st_blksize;
2213 ost->st_blocks = st->st_blocks;
2214 ost->st_flags = st->st_flags;
2215 ost->st_gen = st->st_gen;
2217 #endif /* COMPAT_43 */
2220 * Get file status; this version follows links.
2222 #ifndef _SYS_SYSPROTO_H_
2231 register struct stat_args /* {
2239 error = kern_stat(td, uap->path, UIO_USERSPACE, &sb);
2241 error = copyout(&sb, uap->ub, sizeof (sb));
2245 #ifndef _SYS_SYSPROTO_H_
2246 struct fstatat_args {
2254 sys_fstatat(struct thread *td, struct fstatat_args *uap)
2259 error = kern_statat(td, uap->flag, uap->fd, uap->path,
2260 UIO_USERSPACE, &sb);
2262 error = copyout(&sb, uap->buf, sizeof (sb));
2267 kern_stat(struct thread *td, char *path, enum uio_seg pathseg, struct stat *sbp)
2270 return (kern_statat(td, 0, AT_FDCWD, path, pathseg, sbp));
2274 kern_statat(struct thread *td, int flag, int fd, char *path,
2275 enum uio_seg pathseg, struct stat *sbp)
2278 return (kern_statat_vnhook(td, flag, fd, path, pathseg, sbp, NULL));
2282 kern_statat_vnhook(struct thread *td, int flag, int fd, char *path,
2283 enum uio_seg pathseg, struct stat *sbp,
2284 void (*hook)(struct vnode *vp, struct stat *sbp))
2286 struct nameidata nd;
2288 cap_rights_t rights;
2291 if (flag & ~AT_SYMLINK_NOFOLLOW)
2294 NDINIT_ATRIGHTS(&nd, LOOKUP, ((flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW :
2295 FOLLOW) | LOCKSHARED | LOCKLEAF | AUDITVNODE1, pathseg, path, fd,
2296 cap_rights_init(&rights, CAP_FSTAT), td);
2298 if ((error = namei(&nd)) != 0)
2300 error = vn_stat(nd.ni_vp, &sb, td->td_ucred, NOCRED, td);
2302 SDT_PROBE(vfs, , stat, mode, path, sb.st_mode, 0, 0, 0);
2303 if (S_ISREG(sb.st_mode))
2304 SDT_PROBE(vfs, , stat, reg, path, pathseg, 0, 0, 0);
2305 if (__predict_false(hook != NULL))
2306 hook(nd.ni_vp, &sb);
2308 NDFREE(&nd, NDF_ONLY_PNBUF);
2314 if (KTRPOINT(td, KTR_STRUCT))
2321 * Get file status; this version does not follow links.
2323 #ifndef _SYS_SYSPROTO_H_
2332 register struct lstat_args /* {
2340 error = kern_lstat(td, uap->path, UIO_USERSPACE, &sb);
2342 error = copyout(&sb, uap->ub, sizeof (sb));
2347 kern_lstat(struct thread *td, char *path, enum uio_seg pathseg, struct stat *sbp)
2350 return (kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, path, pathseg,
2355 * Implementation of the NetBSD [l]stat() functions.
2363 bzero(nsb, sizeof *nsb);
2364 nsb->st_dev = sb->st_dev;
2365 nsb->st_ino = sb->st_ino;
2366 nsb->st_mode = sb->st_mode;
2367 nsb->st_nlink = sb->st_nlink;
2368 nsb->st_uid = sb->st_uid;
2369 nsb->st_gid = sb->st_gid;
2370 nsb->st_rdev = sb->st_rdev;
2371 nsb->st_atim = sb->st_atim;
2372 nsb->st_mtim = sb->st_mtim;
2373 nsb->st_ctim = sb->st_ctim;
2374 nsb->st_size = sb->st_size;
2375 nsb->st_blocks = sb->st_blocks;
2376 nsb->st_blksize = sb->st_blksize;
2377 nsb->st_flags = sb->st_flags;
2378 nsb->st_gen = sb->st_gen;
2379 nsb->st_birthtim = sb->st_birthtim;
2382 #ifndef _SYS_SYSPROTO_H_
2391 register struct nstat_args /* {
2400 error = kern_stat(td, uap->path, UIO_USERSPACE, &sb);
2403 cvtnstat(&sb, &nsb);
2404 return (copyout(&nsb, uap->ub, sizeof (nsb)));
2408 * NetBSD lstat. Get file status; this version does not follow links.
2410 #ifndef _SYS_SYSPROTO_H_
2419 register struct nlstat_args /* {
2428 error = kern_lstat(td, uap->path, UIO_USERSPACE, &sb);
2431 cvtnstat(&sb, &nsb);
2432 return (copyout(&nsb, uap->ub, sizeof (nsb)));
2436 * Get configurable pathname variables.
2438 #ifndef _SYS_SYSPROTO_H_
2439 struct pathconf_args {
2445 sys_pathconf(td, uap)
2447 register struct pathconf_args /* {
2453 return (kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name, FOLLOW));
2456 #ifndef _SYS_SYSPROTO_H_
2457 struct lpathconf_args {
2463 sys_lpathconf(td, uap)
2465 register struct lpathconf_args /* {
2471 return (kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name,
2476 kern_pathconf(struct thread *td, char *path, enum uio_seg pathseg, int name,
2479 struct nameidata nd;
2482 NDINIT(&nd, LOOKUP, LOCKSHARED | LOCKLEAF | AUDITVNODE1 | flags,
2484 if ((error = namei(&nd)) != 0)
2486 NDFREE(&nd, NDF_ONLY_PNBUF);
2488 /* If asynchronous I/O is available, it works for all files. */
2489 if (name == _PC_ASYNC_IO)
2490 td->td_retval[0] = async_io_version;
2492 error = VOP_PATHCONF(nd.ni_vp, name, td->td_retval);
2498 * Return target name of a symbolic link.
2500 #ifndef _SYS_SYSPROTO_H_
2501 struct readlink_args {
2508 sys_readlink(td, uap)
2510 register struct readlink_args /* {
2517 return (kern_readlink(td, uap->path, UIO_USERSPACE, uap->buf,
2518 UIO_USERSPACE, uap->count));
2520 #ifndef _SYS_SYSPROTO_H_
2521 struct readlinkat_args {
2529 sys_readlinkat(struct thread *td, struct readlinkat_args *uap)
2532 return (kern_readlinkat(td, uap->fd, uap->path, UIO_USERSPACE,
2533 uap->buf, UIO_USERSPACE, uap->bufsize));
2537 kern_readlink(struct thread *td, char *path, enum uio_seg pathseg, char *buf,
2538 enum uio_seg bufseg, size_t count)
2541 return (kern_readlinkat(td, AT_FDCWD, path, pathseg, buf, bufseg,
2546 kern_readlinkat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2547 char *buf, enum uio_seg bufseg, size_t count)
2552 struct nameidata nd;
2555 if (count > IOSIZE_MAX)
2558 NDINIT_AT(&nd, LOOKUP, NOFOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
2559 pathseg, path, fd, td);
2561 if ((error = namei(&nd)) != 0)
2563 NDFREE(&nd, NDF_ONLY_PNBUF);
2566 error = mac_vnode_check_readlink(td->td_ucred, vp);
2572 if (vp->v_type != VLNK)
2575 aiov.iov_base = buf;
2576 aiov.iov_len = count;
2577 auio.uio_iov = &aiov;
2578 auio.uio_iovcnt = 1;
2579 auio.uio_offset = 0;
2580 auio.uio_rw = UIO_READ;
2581 auio.uio_segflg = bufseg;
2583 auio.uio_resid = count;
2584 error = VOP_READLINK(vp, &auio, td->td_ucred);
2585 td->td_retval[0] = count - auio.uio_resid;
2592 * Common implementation code for chflags() and fchflags().
2595 setfflags(td, vp, flags)
2604 /* We can't support the value matching VNOVAL. */
2605 if (flags == VNOVAL)
2606 return (EOPNOTSUPP);
2609 * Prevent non-root users from setting flags on devices. When
2610 * a device is reused, users can retain ownership of the device
2611 * if they are allowed to set flags and programs assume that
2612 * chown can't fail when done as root.
2614 if (vp->v_type == VCHR || vp->v_type == VBLK) {
2615 error = priv_check(td, PRIV_VFS_CHFLAGS_DEV);
2620 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2623 vattr.va_flags = flags;
2624 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2626 error = mac_vnode_check_setflags(td->td_ucred, vp, vattr.va_flags);
2629 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
2631 vn_finished_write(mp);
2636 * Change flags of a file given a path name.
2638 #ifndef _SYS_SYSPROTO_H_
2639 struct chflags_args {
2645 sys_chflags(td, uap)
2647 register struct chflags_args /* {
2653 return (kern_chflags(td, uap->path, UIO_USERSPACE, uap->flags));
2656 #ifndef _SYS_SYSPROTO_H_
2657 struct chflagsat_args {
2665 sys_chflagsat(struct thread *td, struct chflagsat_args *uap)
2668 const char *path = uap->path;
2669 u_long flags = uap->flags;
2670 int atflag = uap->atflag;
2672 if (atflag & ~AT_SYMLINK_NOFOLLOW)
2675 return (kern_chflagsat(td, fd, path, UIO_USERSPACE, flags, atflag));
2679 kern_chflags(struct thread *td, const char *path, enum uio_seg pathseg,
2683 return (kern_chflagsat(td, AT_FDCWD, path, pathseg, flags, 0));
2687 * Same as chflags() but doesn't follow symlinks.
2690 sys_lchflags(td, uap)
2692 register struct lchflags_args /* {
2698 return (kern_chflagsat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2699 uap->flags, AT_SYMLINK_NOFOLLOW));
2703 kern_chflagsat(struct thread *td, int fd, const char *path,
2704 enum uio_seg pathseg, u_long flags, int atflag)
2706 struct nameidata nd;
2707 cap_rights_t rights;
2710 AUDIT_ARG_FFLAGS(flags);
2711 follow = (atflag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
2712 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, pathseg, path, fd,
2713 cap_rights_init(&rights, CAP_FCHFLAGS), td);
2714 if ((error = namei(&nd)) != 0)
2716 NDFREE(&nd, NDF_ONLY_PNBUF);
2717 error = setfflags(td, nd.ni_vp, flags);
2723 * Change flags of a file given a file descriptor.
2725 #ifndef _SYS_SYSPROTO_H_
2726 struct fchflags_args {
2732 sys_fchflags(td, uap)
2734 register struct fchflags_args /* {
2740 cap_rights_t rights;
2743 AUDIT_ARG_FD(uap->fd);
2744 AUDIT_ARG_FFLAGS(uap->flags);
2745 error = getvnode(td->td_proc->p_fd, uap->fd,
2746 cap_rights_init(&rights, CAP_FCHFLAGS), &fp);
2750 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
2751 AUDIT_ARG_VNODE1(fp->f_vnode);
2752 VOP_UNLOCK(fp->f_vnode, 0);
2754 error = setfflags(td, fp->f_vnode, uap->flags);
2760 * Common implementation code for chmod(), lchmod() and fchmod().
2763 setfmode(td, cred, vp, mode)
2773 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2775 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2777 vattr.va_mode = mode & ALLPERMS;
2779 error = mac_vnode_check_setmode(cred, vp, vattr.va_mode);
2782 error = VOP_SETATTR(vp, &vattr, cred);
2784 vn_finished_write(mp);
2789 * Change mode of a file given path name.
2791 #ifndef _SYS_SYSPROTO_H_
2800 register struct chmod_args /* {
2806 return (kern_chmod(td, uap->path, UIO_USERSPACE, uap->mode));
2809 #ifndef _SYS_SYSPROTO_H_
2810 struct fchmodat_args {
2818 sys_fchmodat(struct thread *td, struct fchmodat_args *uap)
2820 int flag = uap->flag;
2822 char *path = uap->path;
2823 mode_t mode = uap->mode;
2825 if (flag & ~AT_SYMLINK_NOFOLLOW)
2828 return (kern_fchmodat(td, fd, path, UIO_USERSPACE, mode, flag));
2832 kern_chmod(struct thread *td, char *path, enum uio_seg pathseg, int mode)
2835 return (kern_fchmodat(td, AT_FDCWD, path, pathseg, mode, 0));
2839 * Change mode of a file given path name (don't follow links.)
2841 #ifndef _SYS_SYSPROTO_H_
2842 struct lchmod_args {
2850 register struct lchmod_args /* {
2856 return (kern_fchmodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2857 uap->mode, AT_SYMLINK_NOFOLLOW));
2861 kern_fchmodat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2862 mode_t mode, int flag)
2864 struct nameidata nd;
2865 cap_rights_t rights;
2868 AUDIT_ARG_MODE(mode);
2869 follow = (flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
2870 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, pathseg, path, fd,
2871 cap_rights_init(&rights, CAP_FCHMOD), td);
2872 if ((error = namei(&nd)) != 0)
2874 NDFREE(&nd, NDF_ONLY_PNBUF);
2875 error = setfmode(td, td->td_ucred, nd.ni_vp, mode);
2881 * Change mode of a file given a file descriptor.
2883 #ifndef _SYS_SYSPROTO_H_
2884 struct fchmod_args {
2890 sys_fchmod(struct thread *td, struct fchmod_args *uap)
2893 cap_rights_t rights;
2896 AUDIT_ARG_FD(uap->fd);
2897 AUDIT_ARG_MODE(uap->mode);
2899 error = fget(td, uap->fd, cap_rights_init(&rights, CAP_FCHMOD), &fp);
2902 error = fo_chmod(fp, uap->mode, td->td_ucred, td);
2908 * Common implementation for chown(), lchown(), and fchown()
2911 setfown(td, cred, vp, uid, gid)
2922 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2924 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2929 error = mac_vnode_check_setowner(cred, vp, vattr.va_uid,
2933 error = VOP_SETATTR(vp, &vattr, cred);
2935 vn_finished_write(mp);
2940 * Set ownership given a path name.
2942 #ifndef _SYS_SYSPROTO_H_
2952 register struct chown_args /* {
2959 return (kern_chown(td, uap->path, UIO_USERSPACE, uap->uid, uap->gid));
2962 #ifndef _SYS_SYSPROTO_H_
2963 struct fchownat_args {
2972 sys_fchownat(struct thread *td, struct fchownat_args *uap)
2977 if (flag & ~AT_SYMLINK_NOFOLLOW)
2980 return (kern_fchownat(td, uap->fd, uap->path, UIO_USERSPACE, uap->uid,
2981 uap->gid, uap->flag));
2985 kern_chown(struct thread *td, char *path, enum uio_seg pathseg, int uid,
2989 return (kern_fchownat(td, AT_FDCWD, path, pathseg, uid, gid, 0));
2993 kern_fchownat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2994 int uid, int gid, int flag)
2996 struct nameidata nd;
2997 cap_rights_t rights;
3000 AUDIT_ARG_OWNER(uid, gid);
3001 follow = (flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
3002 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, pathseg, path, fd,
3003 cap_rights_init(&rights, CAP_FCHOWN), td);
3005 if ((error = namei(&nd)) != 0)
3007 NDFREE(&nd, NDF_ONLY_PNBUF);
3008 error = setfown(td, td->td_ucred, nd.ni_vp, uid, gid);
3014 * Set ownership given a path name, do not cross symlinks.
3016 #ifndef _SYS_SYSPROTO_H_
3017 struct lchown_args {
3026 register struct lchown_args /* {
3033 return (kern_lchown(td, uap->path, UIO_USERSPACE, uap->uid, uap->gid));
3037 kern_lchown(struct thread *td, char *path, enum uio_seg pathseg, int uid,
3041 return (kern_fchownat(td, AT_FDCWD, path, pathseg, uid, gid,
3042 AT_SYMLINK_NOFOLLOW));
3046 * Set ownership given a file descriptor.
3048 #ifndef _SYS_SYSPROTO_H_
3049 struct fchown_args {
3058 register struct fchown_args /* {
3065 cap_rights_t rights;
3068 AUDIT_ARG_FD(uap->fd);
3069 AUDIT_ARG_OWNER(uap->uid, uap->gid);
3070 error = fget(td, uap->fd, cap_rights_init(&rights, CAP_FCHOWN), &fp);
3073 error = fo_chown(fp, uap->uid, uap->gid, td->td_ucred, td);
3079 * Common implementation code for utimes(), lutimes(), and futimes().
3082 getutimes(usrtvp, tvpseg, tsp)
3083 const struct timeval *usrtvp;
3084 enum uio_seg tvpseg;
3085 struct timespec *tsp;
3087 struct timeval tv[2];
3088 const struct timeval *tvp;
3091 if (usrtvp == NULL) {
3092 vfs_timestamp(&tsp[0]);
3095 if (tvpseg == UIO_SYSSPACE) {
3098 if ((error = copyin(usrtvp, tv, sizeof(tv))) != 0)
3103 if (tvp[0].tv_usec < 0 || tvp[0].tv_usec >= 1000000 ||
3104 tvp[1].tv_usec < 0 || tvp[1].tv_usec >= 1000000)
3106 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
3107 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
3113 * Common implementation code for utimes(), lutimes(), and futimes().
3116 setutimes(td, vp, ts, numtimes, nullflag)
3119 const struct timespec *ts;
3125 int error, setbirthtime;
3127 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
3129 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3131 if (numtimes < 3 && !VOP_GETATTR(vp, &vattr, td->td_ucred) &&
3132 timespeccmp(&ts[1], &vattr.va_birthtime, < ))
3135 vattr.va_atime = ts[0];
3136 vattr.va_mtime = ts[1];
3138 vattr.va_birthtime = ts[1];
3140 vattr.va_birthtime = ts[2];
3142 vattr.va_vaflags |= VA_UTIMES_NULL;
3144 error = mac_vnode_check_setutimes(td->td_ucred, vp, vattr.va_atime,
3148 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3150 vn_finished_write(mp);
3155 * Set the access and modification times of a file.
3157 #ifndef _SYS_SYSPROTO_H_
3158 struct utimes_args {
3160 struct timeval *tptr;
3166 register struct utimes_args /* {
3168 struct timeval *tptr;
3172 return (kern_utimes(td, uap->path, UIO_USERSPACE, uap->tptr,
3176 #ifndef _SYS_SYSPROTO_H_
3177 struct futimesat_args {
3180 const struct timeval * times;
3184 sys_futimesat(struct thread *td, struct futimesat_args *uap)
3187 return (kern_utimesat(td, uap->fd, uap->path, UIO_USERSPACE,
3188 uap->times, UIO_USERSPACE));
3192 kern_utimes(struct thread *td, char *path, enum uio_seg pathseg,
3193 struct timeval *tptr, enum uio_seg tptrseg)
3196 return (kern_utimesat(td, AT_FDCWD, path, pathseg, tptr, tptrseg));
3200 kern_utimesat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
3201 struct timeval *tptr, enum uio_seg tptrseg)
3203 struct nameidata nd;
3204 struct timespec ts[2];
3205 cap_rights_t rights;
3208 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3210 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, fd,
3211 cap_rights_init(&rights, CAP_FUTIMES), td);
3213 if ((error = namei(&nd)) != 0)
3215 NDFREE(&nd, NDF_ONLY_PNBUF);
3216 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3222 * Set the access and modification times of a file.
3224 #ifndef _SYS_SYSPROTO_H_
3225 struct lutimes_args {
3227 struct timeval *tptr;
3231 sys_lutimes(td, uap)
3233 register struct lutimes_args /* {
3235 struct timeval *tptr;
3239 return (kern_lutimes(td, uap->path, UIO_USERSPACE, uap->tptr,
3244 kern_lutimes(struct thread *td, char *path, enum uio_seg pathseg,
3245 struct timeval *tptr, enum uio_seg tptrseg)
3247 struct timespec ts[2];
3248 struct nameidata nd;
3251 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3253 NDINIT(&nd, LOOKUP, NOFOLLOW | AUDITVNODE1, pathseg, path, td);
3254 if ((error = namei(&nd)) != 0)
3256 NDFREE(&nd, NDF_ONLY_PNBUF);
3257 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3263 * Set the access and modification times of a file.
3265 #ifndef _SYS_SYSPROTO_H_
3266 struct futimes_args {
3268 struct timeval *tptr;
3272 sys_futimes(td, uap)
3274 register struct futimes_args /* {
3276 struct timeval *tptr;
3280 return (kern_futimes(td, uap->fd, uap->tptr, UIO_USERSPACE));
3284 kern_futimes(struct thread *td, int fd, struct timeval *tptr,
3285 enum uio_seg tptrseg)
3287 struct timespec ts[2];
3289 cap_rights_t rights;
3293 error = getutimes(tptr, tptrseg, ts);
3296 error = getvnode(td->td_proc->p_fd, fd,
3297 cap_rights_init(&rights, CAP_FUTIMES), &fp);
3301 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
3302 AUDIT_ARG_VNODE1(fp->f_vnode);
3303 VOP_UNLOCK(fp->f_vnode, 0);
3305 error = setutimes(td, fp->f_vnode, ts, 2, tptr == NULL);
3311 * Truncate a file given its path name.
3313 #ifndef _SYS_SYSPROTO_H_
3314 struct truncate_args {
3321 sys_truncate(td, uap)
3323 register struct truncate_args /* {
3330 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3334 kern_truncate(struct thread *td, char *path, enum uio_seg pathseg, off_t length)
3340 struct nameidata nd;
3345 NDINIT(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, td);
3346 if ((error = namei(&nd)) != 0)
3349 rl_cookie = vn_rangelock_wlock(vp, 0, OFF_MAX);
3350 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0) {
3351 vn_rangelock_unlock(vp, rl_cookie);
3355 NDFREE(&nd, NDF_ONLY_PNBUF);
3356 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3357 if (vp->v_type == VDIR)
3360 else if ((error = mac_vnode_check_write(td->td_ucred, NOCRED, vp))) {
3363 else if ((error = vn_writechk(vp)) == 0 &&
3364 (error = VOP_ACCESS(vp, VWRITE, td->td_ucred, td)) == 0) {
3366 vattr.va_size = length;
3367 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3370 vn_finished_write(mp);
3371 vn_rangelock_unlock(vp, rl_cookie);
3376 #if defined(COMPAT_43)
3378 * Truncate a file given its path name.
3380 #ifndef _SYS_SYSPROTO_H_
3381 struct otruncate_args {
3389 register struct otruncate_args /* {
3394 struct truncate_args /* {
3400 nuap.path = uap->path;
3401 nuap.length = uap->length;
3402 return (sys_truncate(td, &nuap));
3404 #endif /* COMPAT_43 */
3406 /* Versions with the pad argument */
3408 freebsd6_truncate(struct thread *td, struct freebsd6_truncate_args *uap)
3410 struct truncate_args ouap;
3412 ouap.path = uap->path;
3413 ouap.length = uap->length;
3414 return (sys_truncate(td, &ouap));
3418 freebsd6_ftruncate(struct thread *td, struct freebsd6_ftruncate_args *uap)
3420 struct ftruncate_args ouap;
3423 ouap.length = uap->length;
3424 return (sys_ftruncate(td, &ouap));
3428 * Sync an open file.
3430 #ifndef _SYS_SYSPROTO_H_
3438 struct fsync_args /* {
3445 cap_rights_t rights;
3446 int error, lock_flags;
3448 AUDIT_ARG_FD(uap->fd);
3449 error = getvnode(td->td_proc->p_fd, uap->fd,
3450 cap_rights_init(&rights, CAP_FSYNC), &fp);
3454 error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
3457 if (MNT_SHARED_WRITES(mp) ||
3458 ((mp == NULL) && MNT_SHARED_WRITES(vp->v_mount))) {
3459 lock_flags = LK_SHARED;
3461 lock_flags = LK_EXCLUSIVE;
3463 vn_lock(vp, lock_flags | LK_RETRY);
3464 AUDIT_ARG_VNODE1(vp);
3465 if (vp->v_object != NULL) {
3466 VM_OBJECT_WLOCK(vp->v_object);
3467 vm_object_page_clean(vp->v_object, 0, 0, 0);
3468 VM_OBJECT_WUNLOCK(vp->v_object);
3470 error = VOP_FSYNC(vp, MNT_WAIT, td);
3473 vn_finished_write(mp);
3480 * Rename files. Source and destination must either both be directories, or
3481 * both not be directories. If target is a directory, it must be empty.
3483 #ifndef _SYS_SYSPROTO_H_
3484 struct rename_args {
3492 register struct rename_args /* {
3498 return (kern_rename(td, uap->from, uap->to, UIO_USERSPACE));
3501 #ifndef _SYS_SYSPROTO_H_
3502 struct renameat_args {
3510 sys_renameat(struct thread *td, struct renameat_args *uap)
3513 return (kern_renameat(td, uap->oldfd, uap->old, uap->newfd, uap->new,
3518 kern_rename(struct thread *td, char *from, char *to, enum uio_seg pathseg)
3521 return (kern_renameat(td, AT_FDCWD, from, AT_FDCWD, to, pathseg));
3525 kern_renameat(struct thread *td, int oldfd, char *old, int newfd, char *new,
3526 enum uio_seg pathseg)
3528 struct mount *mp = NULL;
3529 struct vnode *tvp, *fvp, *tdvp;
3530 struct nameidata fromnd, tond;
3531 cap_rights_t rights;
3537 NDINIT_ATRIGHTS(&fromnd, DELETE, LOCKPARENT | LOCKLEAF | SAVESTART |
3538 AUDITVNODE1, pathseg, old, oldfd,
3539 cap_rights_init(&rights, CAP_RENAMEAT), td);
3541 NDINIT_ATRIGHTS(&fromnd, DELETE, WANTPARENT | SAVESTART | AUDITVNODE1,
3542 pathseg, old, oldfd, cap_rights_init(&rights, CAP_RENAMEAT), td);
3545 if ((error = namei(&fromnd)) != 0)
3548 error = mac_vnode_check_rename_from(td->td_ucred, fromnd.ni_dvp,
3549 fromnd.ni_vp, &fromnd.ni_cnd);
3550 VOP_UNLOCK(fromnd.ni_dvp, 0);
3551 if (fromnd.ni_dvp != fromnd.ni_vp)
3552 VOP_UNLOCK(fromnd.ni_vp, 0);
3555 NDINIT_ATRIGHTS(&tond, RENAME, LOCKPARENT | LOCKLEAF | NOCACHE |
3556 SAVESTART | AUDITVNODE2, pathseg, new, newfd,
3557 cap_rights_init(&rights, CAP_LINKAT), td);
3558 if (fromnd.ni_vp->v_type == VDIR)
3559 tond.ni_cnd.cn_flags |= WILLBEDIR;
3560 if ((error = namei(&tond)) != 0) {
3561 /* Translate error code for rename("dir1", "dir2/."). */
3562 if (error == EISDIR && fvp->v_type == VDIR)
3564 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3565 vrele(fromnd.ni_dvp);
3571 error = vn_start_write(fvp, &mp, V_NOWAIT);
3573 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3574 NDFREE(&tond, NDF_ONLY_PNBUF);
3581 vrele(fromnd.ni_dvp);
3583 vrele(tond.ni_startdir);
3584 if (fromnd.ni_startdir != NULL)
3585 vrele(fromnd.ni_startdir);
3586 error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH);
3592 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
3595 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
3600 if (newfd != AT_FDCWD) {
3602 * If the target already exists we require CAP_UNLINKAT
3605 error = cap_check(&tond.ni_filecaps.fc_rights,
3606 cap_rights_init(&rights, CAP_UNLINKAT));
3617 * If the source is the same as the destination (that is, if they
3618 * are links to the same vnode), then there is nothing to do.
3624 error = mac_vnode_check_rename_to(td->td_ucred, tdvp,
3625 tond.ni_vp, fromnd.ni_dvp == tdvp, &tond.ni_cnd);
3629 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
3630 tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
3631 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3632 NDFREE(&tond, NDF_ONLY_PNBUF);
3634 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3635 NDFREE(&tond, NDF_ONLY_PNBUF);
3642 vrele(fromnd.ni_dvp);
3645 vrele(tond.ni_startdir);
3646 vn_finished_write(mp);
3648 if (fromnd.ni_startdir)
3649 vrele(fromnd.ni_startdir);
3656 * Make a directory file.
3658 #ifndef _SYS_SYSPROTO_H_
3667 register struct mkdir_args /* {
3673 return (kern_mkdir(td, uap->path, UIO_USERSPACE, uap->mode));
3676 #ifndef _SYS_SYSPROTO_H_
3677 struct mkdirat_args {
3684 sys_mkdirat(struct thread *td, struct mkdirat_args *uap)
3687 return (kern_mkdirat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode));
3691 kern_mkdir(struct thread *td, char *path, enum uio_seg segflg, int mode)
3694 return (kern_mkdirat(td, AT_FDCWD, path, segflg, mode));
3698 kern_mkdirat(struct thread *td, int fd, char *path, enum uio_seg segflg,
3704 struct nameidata nd;
3705 cap_rights_t rights;
3708 AUDIT_ARG_MODE(mode);
3711 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
3712 NOCACHE, segflg, path, fd, cap_rights_init(&rights, CAP_MKDIRAT),
3714 nd.ni_cnd.cn_flags |= WILLBEDIR;
3715 if ((error = namei(&nd)) != 0)
3719 NDFREE(&nd, NDF_ONLY_PNBUF);
3721 * XXX namei called with LOCKPARENT but not LOCKLEAF has
3722 * the strange behaviour of leaving the vnode unlocked
3723 * if the target is the same vnode as the parent.
3725 if (vp == nd.ni_dvp)
3732 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3733 NDFREE(&nd, NDF_ONLY_PNBUF);
3735 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3740 vattr.va_type = VDIR;
3741 vattr.va_mode = (mode & ACCESSPERMS) &~ td->td_proc->p_fd->fd_cmask;
3743 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
3748 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
3752 NDFREE(&nd, NDF_ONLY_PNBUF);
3756 vn_finished_write(mp);
3761 * Remove a directory file.
3763 #ifndef _SYS_SYSPROTO_H_
3771 struct rmdir_args /* {
3776 return (kern_rmdir(td, uap->path, UIO_USERSPACE));
3780 kern_rmdir(struct thread *td, char *path, enum uio_seg pathseg)
3783 return (kern_rmdirat(td, AT_FDCWD, path, pathseg));
3787 kern_rmdirat(struct thread *td, int fd, char *path, enum uio_seg pathseg)
3791 struct nameidata nd;
3792 cap_rights_t rights;
3797 NDINIT_ATRIGHTS(&nd, DELETE, LOCKPARENT | LOCKLEAF | AUDITVNODE1,
3798 pathseg, path, fd, cap_rights_init(&rights, CAP_UNLINKAT), td);
3799 if ((error = namei(&nd)) != 0)
3802 if (vp->v_type != VDIR) {
3807 * No rmdir "." please.
3809 if (nd.ni_dvp == vp) {
3814 * The root of a mounted filesystem cannot be deleted.
3816 if (vp->v_vflag & VV_ROOT) {
3821 error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp,
3826 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3827 NDFREE(&nd, NDF_ONLY_PNBUF);
3829 if (nd.ni_dvp == vp)
3833 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3837 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_UNLINK);
3838 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
3839 vn_finished_write(mp);
3841 NDFREE(&nd, NDF_ONLY_PNBUF);
3843 if (nd.ni_dvp == vp)
3852 * Read a block of directory entries in a filesystem independent format.
3854 #ifndef _SYS_SYSPROTO_H_
3855 struct ogetdirentries_args {
3863 ogetdirentries(struct thread *td, struct ogetdirentries_args *uap)
3868 error = kern_ogetdirentries(td, uap, &loff);
3870 error = copyout(&loff, uap->basep, sizeof(long));
3875 kern_ogetdirentries(struct thread *td, struct ogetdirentries_args *uap,
3880 struct uio auio, kuio;
3881 struct iovec aiov, kiov;
3882 struct dirent *dp, *edp;
3883 cap_rights_t rights;
3885 int error, eofflag, readcnt;
3889 /* XXX arbitrary sanity limit on `count'. */
3890 if (uap->count > 64 * 1024)
3892 error = getvnode(td->td_proc->p_fd, uap->fd,
3893 cap_rights_init(&rights, CAP_READ), &fp);
3896 if ((fp->f_flag & FREAD) == 0) {
3901 foffset = foffset_lock(fp, 0);
3903 if (vp->v_type != VDIR) {
3904 foffset_unlock(fp, foffset, 0);
3908 aiov.iov_base = uap->buf;
3909 aiov.iov_len = uap->count;
3910 auio.uio_iov = &aiov;
3911 auio.uio_iovcnt = 1;
3912 auio.uio_rw = UIO_READ;
3913 auio.uio_segflg = UIO_USERSPACE;
3915 auio.uio_resid = uap->count;
3916 vn_lock(vp, LK_SHARED | LK_RETRY);
3917 loff = auio.uio_offset = foffset;
3919 error = mac_vnode_check_readdir(td->td_ucred, vp);
3922 foffset_unlock(fp, foffset, FOF_NOUPDATE);
3927 # if (BYTE_ORDER != LITTLE_ENDIAN)
3928 if (vp->v_mount->mnt_maxsymlinklen <= 0) {
3929 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag,
3931 foffset = auio.uio_offset;
3936 kuio.uio_iov = &kiov;
3937 kuio.uio_segflg = UIO_SYSSPACE;
3938 kiov.iov_len = uap->count;
3939 dirbuf = malloc(uap->count, M_TEMP, M_WAITOK);
3940 kiov.iov_base = dirbuf;
3941 error = VOP_READDIR(vp, &kuio, fp->f_cred, &eofflag,
3943 foffset = kuio.uio_offset;
3945 readcnt = uap->count - kuio.uio_resid;
3946 edp = (struct dirent *)&dirbuf[readcnt];
3947 for (dp = (struct dirent *)dirbuf; dp < edp; ) {
3948 # if (BYTE_ORDER == LITTLE_ENDIAN)
3950 * The expected low byte of
3951 * dp->d_namlen is our dp->d_type.
3952 * The high MBZ byte of dp->d_namlen
3953 * is our dp->d_namlen.
3955 dp->d_type = dp->d_namlen;
3959 * The dp->d_type is the high byte
3960 * of the expected dp->d_namlen,
3961 * so must be zero'ed.
3965 if (dp->d_reclen > 0) {
3966 dp = (struct dirent *)
3967 ((char *)dp + dp->d_reclen);
3974 error = uiomove(dirbuf, readcnt, &auio);
3976 free(dirbuf, M_TEMP);
3980 foffset_unlock(fp, foffset, 0);
3984 if (uap->count == auio.uio_resid &&
3985 (vp->v_vflag & VV_ROOT) &&
3986 (vp->v_mount->mnt_flag & MNT_UNION)) {
3987 struct vnode *tvp = vp;
3988 vp = vp->v_mount->mnt_vnodecovered;
3997 foffset_unlock(fp, foffset, 0);
3999 td->td_retval[0] = uap->count - auio.uio_resid;
4004 #endif /* COMPAT_43 */
4007 * Read a block of directory entries in a filesystem independent format.
4009 #ifndef _SYS_SYSPROTO_H_
4010 struct getdirentries_args {
4018 sys_getdirentries(td, uap)
4020 register struct getdirentries_args /* {
4030 error = kern_getdirentries(td, uap->fd, uap->buf, uap->count, &base,
4031 NULL, UIO_USERSPACE);
4034 if (uap->basep != NULL)
4035 error = copyout(&base, uap->basep, sizeof(long));
4040 kern_getdirentries(struct thread *td, int fd, char *buf, u_int count,
4041 long *basep, ssize_t *residp, enum uio_seg bufseg)
4047 cap_rights_t rights;
4053 if (count > IOSIZE_MAX)
4055 auio.uio_resid = count;
4056 error = getvnode(td->td_proc->p_fd, fd,
4057 cap_rights_init(&rights, CAP_READ), &fp);
4060 if ((fp->f_flag & FREAD) == 0) {
4065 foffset = foffset_lock(fp, 0);
4067 if (vp->v_type != VDIR) {
4071 aiov.iov_base = buf;
4072 aiov.iov_len = count;
4073 auio.uio_iov = &aiov;
4074 auio.uio_iovcnt = 1;
4075 auio.uio_rw = UIO_READ;
4076 auio.uio_segflg = bufseg;
4078 vn_lock(vp, LK_SHARED | LK_RETRY);
4079 AUDIT_ARG_VNODE1(vp);
4080 loff = auio.uio_offset = foffset;
4082 error = mac_vnode_check_readdir(td->td_ucred, vp);
4085 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL,
4087 foffset = auio.uio_offset;
4092 if (count == auio.uio_resid &&
4093 (vp->v_vflag & VV_ROOT) &&
4094 (vp->v_mount->mnt_flag & MNT_UNION)) {
4095 struct vnode *tvp = vp;
4097 vp = vp->v_mount->mnt_vnodecovered;
4108 *residp = auio.uio_resid;
4109 td->td_retval[0] = count - auio.uio_resid;
4111 foffset_unlock(fp, foffset, 0);
4116 #ifndef _SYS_SYSPROTO_H_
4117 struct getdents_args {
4124 sys_getdents(td, uap)
4126 register struct getdents_args /* {
4132 struct getdirentries_args ap;
4136 ap.count = uap->count;
4138 return (sys_getdirentries(td, &ap));
4142 * Set the mode mask for creation of filesystem nodes.
4144 #ifndef _SYS_SYSPROTO_H_
4152 struct umask_args /* {
4156 register struct filedesc *fdp;
4158 FILEDESC_XLOCK(td->td_proc->p_fd);
4159 fdp = td->td_proc->p_fd;
4160 td->td_retval[0] = fdp->fd_cmask;
4161 fdp->fd_cmask = uap->newmask & ALLPERMS;
4162 FILEDESC_XUNLOCK(td->td_proc->p_fd);
4167 * Void all references to file by ripping underlying filesystem away from
4170 #ifndef _SYS_SYSPROTO_H_
4171 struct revoke_args {
4178 register struct revoke_args /* {
4184 struct nameidata nd;
4187 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
4189 if ((error = namei(&nd)) != 0)
4192 NDFREE(&nd, NDF_ONLY_PNBUF);
4193 if (vp->v_type != VCHR || vp->v_rdev == NULL) {
4198 error = mac_vnode_check_revoke(td->td_ucred, vp);
4202 error = VOP_GETATTR(vp, &vattr, td->td_ucred);
4205 if (td->td_ucred->cr_uid != vattr.va_uid) {
4206 error = priv_check(td, PRIV_VFS_ADMIN);
4211 VOP_REVOKE(vp, REVOKEALL);
4218 * Convert a user file descriptor to a kernel file entry and check that, if it
4219 * is a capability, the correct rights are present. A reference on the file
4220 * entry is held upon returning.
4223 getvnode(struct filedesc *fdp, int fd, cap_rights_t *rightsp, struct file **fpp)
4228 error = fget_unlocked(fdp, fd, rightsp, 0, &fp, NULL);
4233 * The file could be not of the vnode type, or it may be not
4234 * yet fully initialized, in which case the f_vnode pointer
4235 * may be set, but f_ops is still badfileops. E.g.,
4236 * devfs_open() transiently create such situation to
4237 * facilitate csw d_fdopen().
4239 * Dupfdopen() handling in kern_openat() installs the
4240 * half-baked file into the process descriptor table, allowing
4241 * other thread to dereference it. Guard against the race by
4244 if (fp->f_vnode == NULL || fp->f_ops == &badfileops) {
4245 fdrop(fp, curthread);
4254 * Get an (NFS) file handle.
4256 #ifndef _SYS_SYSPROTO_H_
4257 struct lgetfh_args {
4265 register struct lgetfh_args *uap;
4267 struct nameidata nd;
4269 register struct vnode *vp;
4272 error = priv_check(td, PRIV_VFS_GETFH);
4275 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
4280 NDFREE(&nd, NDF_ONLY_PNBUF);
4282 bzero(&fh, sizeof(fh));
4283 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
4284 error = VOP_VPTOFH(vp, &fh.fh_fid);
4287 error = copyout(&fh, uap->fhp, sizeof (fh));
4291 #ifndef _SYS_SYSPROTO_H_
4300 register struct getfh_args *uap;
4302 struct nameidata nd;
4304 register struct vnode *vp;
4307 error = priv_check(td, PRIV_VFS_GETFH);
4310 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
4315 NDFREE(&nd, NDF_ONLY_PNBUF);
4317 bzero(&fh, sizeof(fh));
4318 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
4319 error = VOP_VPTOFH(vp, &fh.fh_fid);
4322 error = copyout(&fh, uap->fhp, sizeof (fh));
4327 * syscall for the rpc.lockd to use to translate a NFS file handle into an
4330 * warning: do not remove the priv_check() call or this becomes one giant
4333 #ifndef _SYS_SYSPROTO_H_
4334 struct fhopen_args {
4335 const struct fhandle *u_fhp;
4342 struct fhopen_args /* {
4343 const struct fhandle *u_fhp;
4354 error = priv_check(td, PRIV_VFS_FHOPEN);
4358 fmode = FFLAGS(uap->flags);
4359 /* why not allow a non-read/write open for our lockd? */
4360 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
4362 error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
4365 /* find the mount point */
4366 mp = vfs_busyfs(&fhp.fh_fsid);
4369 /* now give me my vnode, it gets returned to me locked */
4370 error = VFS_FHTOVP(mp, &fhp.fh_fid, LK_EXCLUSIVE, &vp);
4375 error = falloc_noinstall(td, &fp);
4381 * An extra reference on `fp' has been held for us by
4382 * falloc_noinstall().
4388 error = vn_open_vnode(vp, fmode, td->td_ucred, td, fp);
4390 KASSERT(fp->f_ops == &badfileops,
4391 ("VOP_OPEN in fhopen() set f_ops"));
4392 KASSERT(td->td_dupfd < 0,
4393 ("fhopen() encountered fdopen()"));
4403 finit(fp, (fmode & FMASK) | (fp->f_flag & FHASLOCK), DTYPE_VNODE, vp,
4406 if ((fmode & O_TRUNC) != 0) {
4407 error = fo_truncate(fp, 0, td->td_ucred, td);
4412 error = finstall(td, fp, &indx, fmode, NULL);
4415 td->td_retval[0] = indx;
4420 * Stat an (NFS) file handle.
4422 #ifndef _SYS_SYSPROTO_H_
4423 struct fhstat_args {
4424 struct fhandle *u_fhp;
4431 register struct fhstat_args /* {
4432 struct fhandle *u_fhp;
4440 error = copyin(uap->u_fhp, &fh, sizeof(fh));
4443 error = kern_fhstat(td, fh, &sb);
4445 error = copyout(&sb, uap->sb, sizeof(sb));
4450 kern_fhstat(struct thread *td, struct fhandle fh, struct stat *sb)
4456 error = priv_check(td, PRIV_VFS_FHSTAT);
4459 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4461 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_EXCLUSIVE, &vp);
4465 error = vn_stat(vp, sb, td->td_ucred, NOCRED, td);
4471 * Implement fstatfs() for (NFS) file handles.
4473 #ifndef _SYS_SYSPROTO_H_
4474 struct fhstatfs_args {
4475 struct fhandle *u_fhp;
4480 sys_fhstatfs(td, uap)
4482 struct fhstatfs_args /* {
4483 struct fhandle *u_fhp;
4491 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4494 error = kern_fhstatfs(td, fh, &sf);
4497 return (copyout(&sf, uap->buf, sizeof(sf)));
4501 kern_fhstatfs(struct thread *td, fhandle_t fh, struct statfs *buf)
4508 error = priv_check(td, PRIV_VFS_FHSTATFS);
4511 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4513 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_EXCLUSIVE, &vp);
4519 error = prison_canseemount(td->td_ucred, mp);
4523 error = mac_mount_check_stat(td->td_ucred, mp);
4528 * Set these in case the underlying filesystem fails to do so.
4531 sp->f_version = STATFS_VERSION;
4532 sp->f_namemax = NAME_MAX;
4533 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
4534 error = VFS_STATFS(mp, sp);
4543 kern_posix_fallocate(struct thread *td, int fd, off_t offset, off_t len)
4548 cap_rights_t rights;
4549 off_t olen, ooffset;
4552 if (offset < 0 || len <= 0)
4554 /* Check for wrap. */
4555 if (offset > OFF_MAX - len)
4557 error = fget(td, fd, cap_rights_init(&rights, CAP_WRITE), &fp);
4560 if ((fp->f_ops->fo_flags & DFLAG_SEEKABLE) == 0) {
4564 if ((fp->f_flag & FWRITE) == 0) {
4568 if (fp->f_type != DTYPE_VNODE) {
4573 if (vp->v_type != VREG) {
4578 /* Allocating blocks may take a long time, so iterate. */
4585 error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
4588 error = vn_lock(vp, LK_EXCLUSIVE);
4590 vn_finished_write(mp);
4594 error = mac_vnode_check_write(td->td_ucred, fp->f_cred, vp);
4597 error = VOP_ALLOCATE(vp, &offset, &len);
4599 vn_finished_write(mp);
4601 if (olen + ooffset != offset + len) {
4602 panic("offset + len changed from %jx/%jx to %jx/%jx",
4603 ooffset, olen, offset, len);
4605 if (error != 0 || len == 0)
4607 KASSERT(olen > len, ("Iteration did not make progress?"));
4616 sys_posix_fallocate(struct thread *td, struct posix_fallocate_args *uap)
4619 td->td_retval[0] = kern_posix_fallocate(td, uap->fd, uap->offset,
4625 * Unlike madvise(2), we do not make a best effort to remember every
4626 * possible caching hint. Instead, we remember the last setting with
4627 * the exception that we will allow POSIX_FADV_NORMAL to adjust the
4628 * region of any current setting.
4631 kern_posix_fadvise(struct thread *td, int fd, off_t offset, off_t len,
4634 struct fadvise_info *fa, *new;
4637 cap_rights_t rights;
4641 if (offset < 0 || len < 0 || offset > OFF_MAX - len)
4644 case POSIX_FADV_SEQUENTIAL:
4645 case POSIX_FADV_RANDOM:
4646 case POSIX_FADV_NOREUSE:
4647 new = malloc(sizeof(*fa), M_FADVISE, M_WAITOK);
4649 case POSIX_FADV_NORMAL:
4650 case POSIX_FADV_WILLNEED:
4651 case POSIX_FADV_DONTNEED:
4657 /* XXX: CAP_POSIX_FADVISE? */
4658 error = fget(td, fd, cap_rights_init(&rights), &fp);
4661 if ((fp->f_ops->fo_flags & DFLAG_SEEKABLE) == 0) {
4665 if (fp->f_type != DTYPE_VNODE) {
4670 if (vp->v_type != VREG) {
4677 end = offset + len - 1;
4679 case POSIX_FADV_SEQUENTIAL:
4680 case POSIX_FADV_RANDOM:
4681 case POSIX_FADV_NOREUSE:
4683 * Try to merge any existing non-standard region with
4684 * this new region if possible, otherwise create a new
4685 * non-standard region for this request.
4687 mtx_pool_lock(mtxpool_sleep, fp);
4689 if (fa != NULL && fa->fa_advice == advice &&
4690 ((fa->fa_start <= end && fa->fa_end >= offset) ||
4691 (end != OFF_MAX && fa->fa_start == end + 1) ||
4692 (fa->fa_end != OFF_MAX && fa->fa_end + 1 == offset))) {
4693 if (offset < fa->fa_start)
4694 fa->fa_start = offset;
4695 if (end > fa->fa_end)
4698 new->fa_advice = advice;
4699 new->fa_start = offset;
4701 new->fa_prevstart = 0;
4702 new->fa_prevend = 0;
4706 mtx_pool_unlock(mtxpool_sleep, fp);
4708 case POSIX_FADV_NORMAL:
4710 * If a the "normal" region overlaps with an existing
4711 * non-standard region, trim or remove the
4712 * non-standard region.
4714 mtx_pool_lock(mtxpool_sleep, fp);
4717 if (offset <= fa->fa_start && end >= fa->fa_end) {
4719 fp->f_advice = NULL;
4720 } else if (offset <= fa->fa_start &&
4721 end >= fa->fa_start)
4722 fa->fa_start = end + 1;
4723 else if (offset <= fa->fa_end && end >= fa->fa_end)
4724 fa->fa_end = offset - 1;
4725 else if (offset >= fa->fa_start && end <= fa->fa_end) {
4727 * If the "normal" region is a middle
4728 * portion of the existing
4729 * non-standard region, just remove
4730 * the whole thing rather than picking
4731 * one side or the other to
4735 fp->f_advice = NULL;
4738 mtx_pool_unlock(mtxpool_sleep, fp);
4740 case POSIX_FADV_WILLNEED:
4741 case POSIX_FADV_DONTNEED:
4742 error = VOP_ADVISE(vp, offset, end, advice);
4748 free(new, M_FADVISE);
4753 sys_posix_fadvise(struct thread *td, struct posix_fadvise_args *uap)
4756 td->td_retval[0] = kern_posix_fadvise(td, uap->fd, uap->offset,
4757 uap->len, uap->advice);