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_lastfile; 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);
1559 if ((error = namei(&nd)) != 0)
1561 NDFREE(&nd, NDF_ONLY_PNBUF);
1563 if (vp->v_type == VDIR) {
1565 return (EPERM); /* POSIX */
1567 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0) {
1571 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE2,
1572 segflg, path2, fd2, cap_rights_init(&rights, CAP_LINKAT), td);
1573 if ((error = namei(&nd)) == 0) {
1574 if (nd.ni_vp != NULL) {
1575 if (nd.ni_dvp == nd.ni_vp)
1581 } else if ((error = vn_lock(vp, LK_EXCLUSIVE)) == 0) {
1583 * Check for cross-device links. No need to
1584 * recheck vp->v_type, since it cannot change
1585 * for non-doomed vnode.
1587 if (nd.ni_dvp->v_mount != vp->v_mount)
1590 error = can_hardlink(vp, td->td_ucred);
1593 error = mac_vnode_check_link(td->td_ucred,
1594 nd.ni_dvp, vp, &nd.ni_cnd);
1597 error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
1602 NDFREE(&nd, NDF_ONLY_PNBUF);
1604 vn_finished_write(mp);
1607 NDFREE(&nd, NDF_ONLY_PNBUF);
1610 vn_finished_write(mp);
1615 * Make a symbolic link.
1617 #ifndef _SYS_SYSPROTO_H_
1618 struct symlink_args {
1624 sys_symlink(td, uap)
1626 register struct symlink_args /* {
1632 return (kern_symlink(td, uap->path, uap->link, UIO_USERSPACE));
1635 #ifndef _SYS_SYSPROTO_H_
1636 struct symlinkat_args {
1643 sys_symlinkat(struct thread *td, struct symlinkat_args *uap)
1646 return (kern_symlinkat(td, uap->path1, uap->fd, uap->path2,
1651 kern_symlink(struct thread *td, char *path, char *link, enum uio_seg segflg)
1654 return (kern_symlinkat(td, path, AT_FDCWD, link, segflg));
1658 kern_symlinkat(struct thread *td, char *path1, int fd, char *path2,
1659 enum uio_seg segflg)
1664 struct nameidata nd;
1666 cap_rights_t rights;
1668 if (segflg == UIO_SYSSPACE) {
1671 syspath = uma_zalloc(namei_zone, M_WAITOK);
1672 if ((error = copyinstr(path1, syspath, MAXPATHLEN, NULL)) != 0)
1675 AUDIT_ARG_TEXT(syspath);
1678 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1,
1679 segflg, path2, fd, cap_rights_init(&rights, CAP_SYMLINKAT), td);
1680 if ((error = namei(&nd)) != 0)
1683 NDFREE(&nd, NDF_ONLY_PNBUF);
1684 if (nd.ni_vp == nd.ni_dvp)
1692 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1693 NDFREE(&nd, NDF_ONLY_PNBUF);
1695 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1700 vattr.va_mode = ACCESSPERMS &~ td->td_proc->p_fd->fd_cmask;
1702 vattr.va_type = VLNK;
1703 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1708 error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr, syspath);
1714 NDFREE(&nd, NDF_ONLY_PNBUF);
1716 vn_finished_write(mp);
1718 if (segflg != UIO_SYSSPACE)
1719 uma_zfree(namei_zone, syspath);
1724 * Delete a whiteout from the filesystem.
1727 sys_undelete(td, uap)
1729 register struct undelete_args /* {
1734 struct nameidata nd;
1739 NDINIT(&nd, DELETE, LOCKPARENT | DOWHITEOUT | AUDITVNODE1,
1740 UIO_USERSPACE, uap->path, td);
1745 if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & ISWHITEOUT)) {
1746 NDFREE(&nd, NDF_ONLY_PNBUF);
1747 if (nd.ni_vp == nd.ni_dvp)
1755 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1756 NDFREE(&nd, NDF_ONLY_PNBUF);
1758 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1762 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, DELETE);
1763 NDFREE(&nd, NDF_ONLY_PNBUF);
1765 vn_finished_write(mp);
1770 * Delete a name from the filesystem.
1772 #ifndef _SYS_SYSPROTO_H_
1773 struct unlink_args {
1780 struct unlink_args /* {
1785 return (kern_unlink(td, uap->path, UIO_USERSPACE));
1788 #ifndef _SYS_SYSPROTO_H_
1789 struct unlinkat_args {
1796 sys_unlinkat(struct thread *td, struct unlinkat_args *uap)
1798 int flag = uap->flag;
1800 char *path = uap->path;
1802 if (flag & ~AT_REMOVEDIR)
1805 if (flag & AT_REMOVEDIR)
1806 return (kern_rmdirat(td, fd, path, UIO_USERSPACE));
1808 return (kern_unlinkat(td, fd, path, UIO_USERSPACE, 0));
1812 kern_unlink(struct thread *td, char *path, enum uio_seg pathseg)
1815 return (kern_unlinkat(td, AT_FDCWD, path, pathseg, 0));
1819 kern_unlinkat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1824 struct nameidata nd;
1826 cap_rights_t rights;
1831 NDINIT_ATRIGHTS(&nd, DELETE, LOCKPARENT | LOCKLEAF | AUDITVNODE1,
1832 pathseg, path, fd, cap_rights_init(&rights, CAP_UNLINKAT), td);
1833 if ((error = namei(&nd)) != 0)
1834 return (error == EINVAL ? EPERM : error);
1836 if (vp->v_type == VDIR && oldinum == 0) {
1837 error = EPERM; /* POSIX */
1838 } else if (oldinum != 0 &&
1839 ((error = vn_stat(vp, &sb, td->td_ucred, NOCRED, td)) == 0) &&
1840 sb.st_ino != oldinum) {
1841 error = EIDRM; /* Identifier removed */
1844 * The root of a mounted filesystem cannot be deleted.
1846 * XXX: can this only be a VDIR case?
1848 if (vp->v_vflag & VV_ROOT)
1852 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1853 NDFREE(&nd, NDF_ONLY_PNBUF);
1855 if (vp == nd.ni_dvp)
1859 if ((error = vn_start_write(NULL, &mp,
1860 V_XSLEEP | PCATCH)) != 0)
1865 error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp,
1870 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_UNLINK);
1871 error = VOP_REMOVE(nd.ni_dvp, vp, &nd.ni_cnd);
1875 vn_finished_write(mp);
1877 NDFREE(&nd, NDF_ONLY_PNBUF);
1879 if (vp == nd.ni_dvp)
1887 * Reposition read/write file offset.
1889 #ifndef _SYS_SYSPROTO_H_
1900 register struct lseek_args /* {
1908 cap_rights_t rights;
1911 AUDIT_ARG_FD(uap->fd);
1912 error = fget(td, uap->fd, cap_rights_init(&rights, CAP_SEEK), &fp);
1915 error = (fp->f_ops->fo_flags & DFLAG_SEEKABLE) != 0 ?
1916 fo_seek(fp, uap->offset, uap->whence, td) : ESPIPE;
1921 #if defined(COMPAT_43)
1923 * Reposition read/write file offset.
1925 #ifndef _SYS_SYSPROTO_H_
1926 struct olseek_args {
1935 register struct olseek_args /* {
1941 struct lseek_args /* {
1949 nuap.offset = uap->offset;
1950 nuap.whence = uap->whence;
1951 return (sys_lseek(td, &nuap));
1953 #endif /* COMPAT_43 */
1955 /* Version with the 'pad' argument */
1957 freebsd6_lseek(td, uap)
1959 register struct freebsd6_lseek_args *uap;
1961 struct lseek_args ouap;
1964 ouap.offset = uap->offset;
1965 ouap.whence = uap->whence;
1966 return (sys_lseek(td, &ouap));
1970 * Check access permissions using passed credentials.
1973 vn_access(vp, user_flags, cred, td)
1982 /* Flags == 0 means only check for existence. */
1986 if (user_flags & R_OK)
1988 if (user_flags & W_OK)
1990 if (user_flags & X_OK)
1993 error = mac_vnode_check_access(cred, vp, accmode);
1997 if ((accmode & VWRITE) == 0 || (error = vn_writechk(vp)) == 0)
1998 error = VOP_ACCESS(vp, accmode, cred, td);
2004 * Check access permissions using "real" credentials.
2006 #ifndef _SYS_SYSPROTO_H_
2007 struct access_args {
2015 register struct access_args /* {
2021 return (kern_access(td, uap->path, UIO_USERSPACE, uap->amode));
2024 #ifndef _SYS_SYSPROTO_H_
2025 struct faccessat_args {
2033 sys_faccessat(struct thread *td, struct faccessat_args *uap)
2036 if (uap->flag & ~AT_EACCESS)
2038 return (kern_accessat(td, uap->fd, uap->path, UIO_USERSPACE, uap->flag,
2043 kern_access(struct thread *td, char *path, enum uio_seg pathseg, int amode)
2046 return (kern_accessat(td, AT_FDCWD, path, pathseg, 0, amode));
2050 kern_accessat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2051 int flag, int amode)
2053 struct ucred *cred, *tmpcred;
2055 struct nameidata nd;
2056 cap_rights_t rights;
2060 * Create and modify a temporary credential instead of one that
2061 * is potentially shared.
2063 if (!(flag & AT_EACCESS)) {
2064 cred = td->td_ucred;
2065 tmpcred = crdup(cred);
2066 tmpcred->cr_uid = cred->cr_ruid;
2067 tmpcred->cr_groups[0] = cred->cr_rgid;
2068 td->td_ucred = tmpcred;
2070 cred = tmpcred = td->td_ucred;
2071 AUDIT_ARG_VALUE(amode);
2072 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF |
2073 AUDITVNODE1, pathseg, path, fd, cap_rights_init(&rights, CAP_FSTAT),
2075 if ((error = namei(&nd)) != 0)
2079 error = vn_access(vp, amode, tmpcred, td);
2080 NDFREE(&nd, NDF_ONLY_PNBUF);
2083 if (!(flag & AT_EACCESS)) {
2084 td->td_ucred = cred;
2091 * Check access permissions using "effective" credentials.
2093 #ifndef _SYS_SYSPROTO_H_
2094 struct eaccess_args {
2100 sys_eaccess(td, uap)
2102 register struct eaccess_args /* {
2108 return (kern_eaccess(td, uap->path, UIO_USERSPACE, uap->amode));
2112 kern_eaccess(struct thread *td, char *path, enum uio_seg pathseg, int amode)
2115 return (kern_accessat(td, AT_FDCWD, path, pathseg, AT_EACCESS, amode));
2118 #if defined(COMPAT_43)
2120 * Get file status; this version follows links.
2122 #ifndef _SYS_SYSPROTO_H_
2131 register struct ostat_args /* {
2140 error = kern_stat(td, uap->path, UIO_USERSPACE, &sb);
2144 return (copyout(&osb, uap->ub, sizeof (osb)));
2148 * Get file status; this version does not follow links.
2150 #ifndef _SYS_SYSPROTO_H_
2151 struct olstat_args {
2159 register struct olstat_args /* {
2168 error = kern_lstat(td, uap->path, UIO_USERSPACE, &sb);
2172 return (copyout(&osb, uap->ub, sizeof (osb)));
2176 * Convert from an old to a new stat structure.
2184 bzero(ost, sizeof(*ost));
2185 ost->st_dev = st->st_dev;
2186 ost->st_ino = st->st_ino;
2187 ost->st_mode = st->st_mode;
2188 ost->st_nlink = st->st_nlink;
2189 ost->st_uid = st->st_uid;
2190 ost->st_gid = st->st_gid;
2191 ost->st_rdev = st->st_rdev;
2192 if (st->st_size < (quad_t)1 << 32)
2193 ost->st_size = st->st_size;
2196 ost->st_atim = st->st_atim;
2197 ost->st_mtim = st->st_mtim;
2198 ost->st_ctim = st->st_ctim;
2199 ost->st_blksize = st->st_blksize;
2200 ost->st_blocks = st->st_blocks;
2201 ost->st_flags = st->st_flags;
2202 ost->st_gen = st->st_gen;
2204 #endif /* COMPAT_43 */
2207 * Get file status; this version follows links.
2209 #ifndef _SYS_SYSPROTO_H_
2218 register struct stat_args /* {
2226 error = kern_stat(td, uap->path, UIO_USERSPACE, &sb);
2228 error = copyout(&sb, uap->ub, sizeof (sb));
2232 #ifndef _SYS_SYSPROTO_H_
2233 struct fstatat_args {
2241 sys_fstatat(struct thread *td, struct fstatat_args *uap)
2246 error = kern_statat(td, uap->flag, uap->fd, uap->path,
2247 UIO_USERSPACE, &sb);
2249 error = copyout(&sb, uap->buf, sizeof (sb));
2254 kern_stat(struct thread *td, char *path, enum uio_seg pathseg, struct stat *sbp)
2257 return (kern_statat(td, 0, AT_FDCWD, path, pathseg, sbp));
2261 kern_statat(struct thread *td, int flag, int fd, char *path,
2262 enum uio_seg pathseg, struct stat *sbp)
2265 return (kern_statat_vnhook(td, flag, fd, path, pathseg, sbp, NULL));
2269 kern_statat_vnhook(struct thread *td, int flag, int fd, char *path,
2270 enum uio_seg pathseg, struct stat *sbp,
2271 void (*hook)(struct vnode *vp, struct stat *sbp))
2273 struct nameidata nd;
2275 cap_rights_t rights;
2278 if (flag & ~AT_SYMLINK_NOFOLLOW)
2281 NDINIT_ATRIGHTS(&nd, LOOKUP, ((flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW :
2282 FOLLOW) | LOCKSHARED | LOCKLEAF | AUDITVNODE1, pathseg, path, fd,
2283 cap_rights_init(&rights, CAP_FSTAT), td);
2285 if ((error = namei(&nd)) != 0)
2287 error = vn_stat(nd.ni_vp, &sb, td->td_ucred, NOCRED, td);
2289 SDT_PROBE(vfs, , stat, mode, path, sb.st_mode, 0, 0, 0);
2290 if (S_ISREG(sb.st_mode))
2291 SDT_PROBE(vfs, , stat, reg, path, pathseg, 0, 0, 0);
2292 if (__predict_false(hook != NULL))
2293 hook(nd.ni_vp, &sb);
2295 NDFREE(&nd, NDF_ONLY_PNBUF);
2301 if (KTRPOINT(td, KTR_STRUCT))
2308 * Get file status; this version does not follow links.
2310 #ifndef _SYS_SYSPROTO_H_
2319 register struct lstat_args /* {
2327 error = kern_lstat(td, uap->path, UIO_USERSPACE, &sb);
2329 error = copyout(&sb, uap->ub, sizeof (sb));
2334 kern_lstat(struct thread *td, char *path, enum uio_seg pathseg, struct stat *sbp)
2337 return (kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, path, pathseg,
2342 * Implementation of the NetBSD [l]stat() functions.
2350 bzero(nsb, sizeof *nsb);
2351 nsb->st_dev = sb->st_dev;
2352 nsb->st_ino = sb->st_ino;
2353 nsb->st_mode = sb->st_mode;
2354 nsb->st_nlink = sb->st_nlink;
2355 nsb->st_uid = sb->st_uid;
2356 nsb->st_gid = sb->st_gid;
2357 nsb->st_rdev = sb->st_rdev;
2358 nsb->st_atim = sb->st_atim;
2359 nsb->st_mtim = sb->st_mtim;
2360 nsb->st_ctim = sb->st_ctim;
2361 nsb->st_size = sb->st_size;
2362 nsb->st_blocks = sb->st_blocks;
2363 nsb->st_blksize = sb->st_blksize;
2364 nsb->st_flags = sb->st_flags;
2365 nsb->st_gen = sb->st_gen;
2366 nsb->st_birthtim = sb->st_birthtim;
2369 #ifndef _SYS_SYSPROTO_H_
2378 register struct nstat_args /* {
2387 error = kern_stat(td, uap->path, UIO_USERSPACE, &sb);
2390 cvtnstat(&sb, &nsb);
2391 return (copyout(&nsb, uap->ub, sizeof (nsb)));
2395 * NetBSD lstat. Get file status; this version does not follow links.
2397 #ifndef _SYS_SYSPROTO_H_
2406 register struct nlstat_args /* {
2415 error = kern_lstat(td, uap->path, UIO_USERSPACE, &sb);
2418 cvtnstat(&sb, &nsb);
2419 return (copyout(&nsb, uap->ub, sizeof (nsb)));
2423 * Get configurable pathname variables.
2425 #ifndef _SYS_SYSPROTO_H_
2426 struct pathconf_args {
2432 sys_pathconf(td, uap)
2434 register struct pathconf_args /* {
2440 return (kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name, FOLLOW));
2443 #ifndef _SYS_SYSPROTO_H_
2444 struct lpathconf_args {
2450 sys_lpathconf(td, uap)
2452 register struct lpathconf_args /* {
2458 return (kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name,
2463 kern_pathconf(struct thread *td, char *path, enum uio_seg pathseg, int name,
2466 struct nameidata nd;
2469 NDINIT(&nd, LOOKUP, LOCKSHARED | LOCKLEAF | AUDITVNODE1 | flags,
2471 if ((error = namei(&nd)) != 0)
2473 NDFREE(&nd, NDF_ONLY_PNBUF);
2475 /* If asynchronous I/O is available, it works for all files. */
2476 if (name == _PC_ASYNC_IO)
2477 td->td_retval[0] = async_io_version;
2479 error = VOP_PATHCONF(nd.ni_vp, name, td->td_retval);
2485 * Return target name of a symbolic link.
2487 #ifndef _SYS_SYSPROTO_H_
2488 struct readlink_args {
2495 sys_readlink(td, uap)
2497 register struct readlink_args /* {
2504 return (kern_readlink(td, uap->path, UIO_USERSPACE, uap->buf,
2505 UIO_USERSPACE, uap->count));
2507 #ifndef _SYS_SYSPROTO_H_
2508 struct readlinkat_args {
2516 sys_readlinkat(struct thread *td, struct readlinkat_args *uap)
2519 return (kern_readlinkat(td, uap->fd, uap->path, UIO_USERSPACE,
2520 uap->buf, UIO_USERSPACE, uap->bufsize));
2524 kern_readlink(struct thread *td, char *path, enum uio_seg pathseg, char *buf,
2525 enum uio_seg bufseg, size_t count)
2528 return (kern_readlinkat(td, AT_FDCWD, path, pathseg, buf, bufseg,
2533 kern_readlinkat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2534 char *buf, enum uio_seg bufseg, size_t count)
2539 struct nameidata nd;
2542 if (count > IOSIZE_MAX)
2545 NDINIT_AT(&nd, LOOKUP, NOFOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
2546 pathseg, path, fd, td);
2548 if ((error = namei(&nd)) != 0)
2550 NDFREE(&nd, NDF_ONLY_PNBUF);
2553 error = mac_vnode_check_readlink(td->td_ucred, vp);
2559 if (vp->v_type != VLNK)
2562 aiov.iov_base = buf;
2563 aiov.iov_len = count;
2564 auio.uio_iov = &aiov;
2565 auio.uio_iovcnt = 1;
2566 auio.uio_offset = 0;
2567 auio.uio_rw = UIO_READ;
2568 auio.uio_segflg = bufseg;
2570 auio.uio_resid = count;
2571 error = VOP_READLINK(vp, &auio, td->td_ucred);
2572 td->td_retval[0] = count - auio.uio_resid;
2579 * Common implementation code for chflags() and fchflags().
2582 setfflags(td, vp, flags)
2591 /* We can't support the value matching VNOVAL. */
2592 if (flags == VNOVAL)
2593 return (EOPNOTSUPP);
2596 * Prevent non-root users from setting flags on devices. When
2597 * a device is reused, users can retain ownership of the device
2598 * if they are allowed to set flags and programs assume that
2599 * chown can't fail when done as root.
2601 if (vp->v_type == VCHR || vp->v_type == VBLK) {
2602 error = priv_check(td, PRIV_VFS_CHFLAGS_DEV);
2607 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2610 vattr.va_flags = flags;
2611 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2613 error = mac_vnode_check_setflags(td->td_ucred, vp, vattr.va_flags);
2616 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
2618 vn_finished_write(mp);
2623 * Change flags of a file given a path name.
2625 #ifndef _SYS_SYSPROTO_H_
2626 struct chflags_args {
2632 sys_chflags(td, uap)
2634 register struct chflags_args /* {
2640 return (kern_chflags(td, uap->path, UIO_USERSPACE, uap->flags));
2643 #ifndef _SYS_SYSPROTO_H_
2644 struct chflagsat_args {
2652 sys_chflagsat(struct thread *td, struct chflagsat_args *uap)
2655 const char *path = uap->path;
2656 u_long flags = uap->flags;
2657 int atflag = uap->atflag;
2659 if (atflag & ~AT_SYMLINK_NOFOLLOW)
2662 return (kern_chflagsat(td, fd, path, UIO_USERSPACE, flags, atflag));
2666 kern_chflags(struct thread *td, const char *path, enum uio_seg pathseg,
2670 return (kern_chflagsat(td, AT_FDCWD, path, pathseg, flags, 0));
2674 * Same as chflags() but doesn't follow symlinks.
2677 sys_lchflags(td, uap)
2679 register struct lchflags_args /* {
2685 return (kern_chflagsat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2686 uap->flags, AT_SYMLINK_NOFOLLOW));
2690 kern_chflagsat(struct thread *td, int fd, const char *path,
2691 enum uio_seg pathseg, u_long flags, int atflag)
2693 struct nameidata nd;
2694 cap_rights_t rights;
2697 AUDIT_ARG_FFLAGS(flags);
2698 follow = (atflag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
2699 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, pathseg, path, fd,
2700 cap_rights_init(&rights, CAP_FCHFLAGS), td);
2701 if ((error = namei(&nd)) != 0)
2703 NDFREE(&nd, NDF_ONLY_PNBUF);
2704 error = setfflags(td, nd.ni_vp, flags);
2710 * Change flags of a file given a file descriptor.
2712 #ifndef _SYS_SYSPROTO_H_
2713 struct fchflags_args {
2719 sys_fchflags(td, uap)
2721 register struct fchflags_args /* {
2727 cap_rights_t rights;
2730 AUDIT_ARG_FD(uap->fd);
2731 AUDIT_ARG_FFLAGS(uap->flags);
2732 error = getvnode(td->td_proc->p_fd, uap->fd,
2733 cap_rights_init(&rights, CAP_FCHFLAGS), &fp);
2737 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
2738 AUDIT_ARG_VNODE1(fp->f_vnode);
2739 VOP_UNLOCK(fp->f_vnode, 0);
2741 error = setfflags(td, fp->f_vnode, uap->flags);
2747 * Common implementation code for chmod(), lchmod() and fchmod().
2750 setfmode(td, cred, vp, mode)
2760 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2762 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2764 vattr.va_mode = mode & ALLPERMS;
2766 error = mac_vnode_check_setmode(cred, vp, vattr.va_mode);
2769 error = VOP_SETATTR(vp, &vattr, cred);
2771 vn_finished_write(mp);
2776 * Change mode of a file given path name.
2778 #ifndef _SYS_SYSPROTO_H_
2787 register struct chmod_args /* {
2793 return (kern_chmod(td, uap->path, UIO_USERSPACE, uap->mode));
2796 #ifndef _SYS_SYSPROTO_H_
2797 struct fchmodat_args {
2805 sys_fchmodat(struct thread *td, struct fchmodat_args *uap)
2807 int flag = uap->flag;
2809 char *path = uap->path;
2810 mode_t mode = uap->mode;
2812 if (flag & ~AT_SYMLINK_NOFOLLOW)
2815 return (kern_fchmodat(td, fd, path, UIO_USERSPACE, mode, flag));
2819 kern_chmod(struct thread *td, char *path, enum uio_seg pathseg, int mode)
2822 return (kern_fchmodat(td, AT_FDCWD, path, pathseg, mode, 0));
2826 * Change mode of a file given path name (don't follow links.)
2828 #ifndef _SYS_SYSPROTO_H_
2829 struct lchmod_args {
2837 register struct lchmod_args /* {
2843 return (kern_fchmodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2844 uap->mode, AT_SYMLINK_NOFOLLOW));
2848 kern_fchmodat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2849 mode_t mode, int flag)
2851 struct nameidata nd;
2852 cap_rights_t rights;
2855 AUDIT_ARG_MODE(mode);
2856 follow = (flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
2857 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, pathseg, path, fd,
2858 cap_rights_init(&rights, CAP_FCHMOD), td);
2859 if ((error = namei(&nd)) != 0)
2861 NDFREE(&nd, NDF_ONLY_PNBUF);
2862 error = setfmode(td, td->td_ucred, nd.ni_vp, mode);
2868 * Change mode of a file given a file descriptor.
2870 #ifndef _SYS_SYSPROTO_H_
2871 struct fchmod_args {
2877 sys_fchmod(struct thread *td, struct fchmod_args *uap)
2880 cap_rights_t rights;
2883 AUDIT_ARG_FD(uap->fd);
2884 AUDIT_ARG_MODE(uap->mode);
2886 error = fget(td, uap->fd, cap_rights_init(&rights, CAP_FCHMOD), &fp);
2889 error = fo_chmod(fp, uap->mode, td->td_ucred, td);
2895 * Common implementation for chown(), lchown(), and fchown()
2898 setfown(td, cred, vp, uid, gid)
2909 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2911 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2916 error = mac_vnode_check_setowner(cred, vp, vattr.va_uid,
2920 error = VOP_SETATTR(vp, &vattr, cred);
2922 vn_finished_write(mp);
2927 * Set ownership given a path name.
2929 #ifndef _SYS_SYSPROTO_H_
2939 register struct chown_args /* {
2946 return (kern_chown(td, uap->path, UIO_USERSPACE, uap->uid, uap->gid));
2949 #ifndef _SYS_SYSPROTO_H_
2950 struct fchownat_args {
2959 sys_fchownat(struct thread *td, struct fchownat_args *uap)
2964 if (flag & ~AT_SYMLINK_NOFOLLOW)
2967 return (kern_fchownat(td, uap->fd, uap->path, UIO_USERSPACE, uap->uid,
2968 uap->gid, uap->flag));
2972 kern_chown(struct thread *td, char *path, enum uio_seg pathseg, int uid,
2976 return (kern_fchownat(td, AT_FDCWD, path, pathseg, uid, gid, 0));
2980 kern_fchownat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2981 int uid, int gid, int flag)
2983 struct nameidata nd;
2984 cap_rights_t rights;
2987 AUDIT_ARG_OWNER(uid, gid);
2988 follow = (flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
2989 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, pathseg, path, fd,
2990 cap_rights_init(&rights, CAP_FCHOWN), td);
2992 if ((error = namei(&nd)) != 0)
2994 NDFREE(&nd, NDF_ONLY_PNBUF);
2995 error = setfown(td, td->td_ucred, nd.ni_vp, uid, gid);
3001 * Set ownership given a path name, do not cross symlinks.
3003 #ifndef _SYS_SYSPROTO_H_
3004 struct lchown_args {
3013 register struct lchown_args /* {
3020 return (kern_lchown(td, uap->path, UIO_USERSPACE, uap->uid, uap->gid));
3024 kern_lchown(struct thread *td, char *path, enum uio_seg pathseg, int uid,
3028 return (kern_fchownat(td, AT_FDCWD, path, pathseg, uid, gid,
3029 AT_SYMLINK_NOFOLLOW));
3033 * Set ownership given a file descriptor.
3035 #ifndef _SYS_SYSPROTO_H_
3036 struct fchown_args {
3045 register struct fchown_args /* {
3052 cap_rights_t rights;
3055 AUDIT_ARG_FD(uap->fd);
3056 AUDIT_ARG_OWNER(uap->uid, uap->gid);
3057 error = fget(td, uap->fd, cap_rights_init(&rights, CAP_FCHOWN), &fp);
3060 error = fo_chown(fp, uap->uid, uap->gid, td->td_ucred, td);
3066 * Common implementation code for utimes(), lutimes(), and futimes().
3069 getutimes(usrtvp, tvpseg, tsp)
3070 const struct timeval *usrtvp;
3071 enum uio_seg tvpseg;
3072 struct timespec *tsp;
3074 struct timeval tv[2];
3075 const struct timeval *tvp;
3078 if (usrtvp == NULL) {
3079 vfs_timestamp(&tsp[0]);
3082 if (tvpseg == UIO_SYSSPACE) {
3085 if ((error = copyin(usrtvp, tv, sizeof(tv))) != 0)
3090 if (tvp[0].tv_usec < 0 || tvp[0].tv_usec >= 1000000 ||
3091 tvp[1].tv_usec < 0 || tvp[1].tv_usec >= 1000000)
3093 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
3094 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
3100 * Common implementation code for utimes(), lutimes(), and futimes().
3103 setutimes(td, vp, ts, numtimes, nullflag)
3106 const struct timespec *ts;
3112 int error, setbirthtime;
3114 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
3116 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3118 if (numtimes < 3 && !VOP_GETATTR(vp, &vattr, td->td_ucred) &&
3119 timespeccmp(&ts[1], &vattr.va_birthtime, < ))
3122 vattr.va_atime = ts[0];
3123 vattr.va_mtime = ts[1];
3125 vattr.va_birthtime = ts[1];
3127 vattr.va_birthtime = ts[2];
3129 vattr.va_vaflags |= VA_UTIMES_NULL;
3131 error = mac_vnode_check_setutimes(td->td_ucred, vp, vattr.va_atime,
3135 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3137 vn_finished_write(mp);
3142 * Set the access and modification times of a file.
3144 #ifndef _SYS_SYSPROTO_H_
3145 struct utimes_args {
3147 struct timeval *tptr;
3153 register struct utimes_args /* {
3155 struct timeval *tptr;
3159 return (kern_utimes(td, uap->path, UIO_USERSPACE, uap->tptr,
3163 #ifndef _SYS_SYSPROTO_H_
3164 struct futimesat_args {
3167 const struct timeval * times;
3171 sys_futimesat(struct thread *td, struct futimesat_args *uap)
3174 return (kern_utimesat(td, uap->fd, uap->path, UIO_USERSPACE,
3175 uap->times, UIO_USERSPACE));
3179 kern_utimes(struct thread *td, char *path, enum uio_seg pathseg,
3180 struct timeval *tptr, enum uio_seg tptrseg)
3183 return (kern_utimesat(td, AT_FDCWD, path, pathseg, tptr, tptrseg));
3187 kern_utimesat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
3188 struct timeval *tptr, enum uio_seg tptrseg)
3190 struct nameidata nd;
3191 struct timespec ts[2];
3192 cap_rights_t rights;
3195 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3197 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, fd,
3198 cap_rights_init(&rights, CAP_FUTIMES), td);
3200 if ((error = namei(&nd)) != 0)
3202 NDFREE(&nd, NDF_ONLY_PNBUF);
3203 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3209 * Set the access and modification times of a file.
3211 #ifndef _SYS_SYSPROTO_H_
3212 struct lutimes_args {
3214 struct timeval *tptr;
3218 sys_lutimes(td, uap)
3220 register struct lutimes_args /* {
3222 struct timeval *tptr;
3226 return (kern_lutimes(td, uap->path, UIO_USERSPACE, uap->tptr,
3231 kern_lutimes(struct thread *td, char *path, enum uio_seg pathseg,
3232 struct timeval *tptr, enum uio_seg tptrseg)
3234 struct timespec ts[2];
3235 struct nameidata nd;
3238 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3240 NDINIT(&nd, LOOKUP, NOFOLLOW | AUDITVNODE1, pathseg, path, td);
3241 if ((error = namei(&nd)) != 0)
3243 NDFREE(&nd, NDF_ONLY_PNBUF);
3244 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3250 * Set the access and modification times of a file.
3252 #ifndef _SYS_SYSPROTO_H_
3253 struct futimes_args {
3255 struct timeval *tptr;
3259 sys_futimes(td, uap)
3261 register struct futimes_args /* {
3263 struct timeval *tptr;
3267 return (kern_futimes(td, uap->fd, uap->tptr, UIO_USERSPACE));
3271 kern_futimes(struct thread *td, int fd, struct timeval *tptr,
3272 enum uio_seg tptrseg)
3274 struct timespec ts[2];
3276 cap_rights_t rights;
3280 error = getutimes(tptr, tptrseg, ts);
3283 error = getvnode(td->td_proc->p_fd, fd,
3284 cap_rights_init(&rights, CAP_FUTIMES), &fp);
3288 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
3289 AUDIT_ARG_VNODE1(fp->f_vnode);
3290 VOP_UNLOCK(fp->f_vnode, 0);
3292 error = setutimes(td, fp->f_vnode, ts, 2, tptr == NULL);
3298 * Truncate a file given its path name.
3300 #ifndef _SYS_SYSPROTO_H_
3301 struct truncate_args {
3308 sys_truncate(td, uap)
3310 register struct truncate_args /* {
3317 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3321 kern_truncate(struct thread *td, char *path, enum uio_seg pathseg, off_t length)
3327 struct nameidata nd;
3332 NDINIT(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, td);
3333 if ((error = namei(&nd)) != 0)
3336 rl_cookie = vn_rangelock_wlock(vp, 0, OFF_MAX);
3337 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0) {
3338 vn_rangelock_unlock(vp, rl_cookie);
3342 NDFREE(&nd, NDF_ONLY_PNBUF);
3343 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3344 if (vp->v_type == VDIR)
3347 else if ((error = mac_vnode_check_write(td->td_ucred, NOCRED, vp))) {
3350 else if ((error = vn_writechk(vp)) == 0 &&
3351 (error = VOP_ACCESS(vp, VWRITE, td->td_ucred, td)) == 0) {
3353 vattr.va_size = length;
3354 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3357 vn_finished_write(mp);
3358 vn_rangelock_unlock(vp, rl_cookie);
3363 #if defined(COMPAT_43)
3365 * Truncate a file given its path name.
3367 #ifndef _SYS_SYSPROTO_H_
3368 struct otruncate_args {
3376 register struct otruncate_args /* {
3381 struct truncate_args /* {
3387 nuap.path = uap->path;
3388 nuap.length = uap->length;
3389 return (sys_truncate(td, &nuap));
3391 #endif /* COMPAT_43 */
3393 /* Versions with the pad argument */
3395 freebsd6_truncate(struct thread *td, struct freebsd6_truncate_args *uap)
3397 struct truncate_args ouap;
3399 ouap.path = uap->path;
3400 ouap.length = uap->length;
3401 return (sys_truncate(td, &ouap));
3405 freebsd6_ftruncate(struct thread *td, struct freebsd6_ftruncate_args *uap)
3407 struct ftruncate_args ouap;
3410 ouap.length = uap->length;
3411 return (sys_ftruncate(td, &ouap));
3415 * Sync an open file.
3417 #ifndef _SYS_SYSPROTO_H_
3425 struct fsync_args /* {
3432 cap_rights_t rights;
3433 int error, lock_flags;
3435 AUDIT_ARG_FD(uap->fd);
3436 error = getvnode(td->td_proc->p_fd, uap->fd,
3437 cap_rights_init(&rights, CAP_FSYNC), &fp);
3441 error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
3444 if (MNT_SHARED_WRITES(mp) ||
3445 ((mp == NULL) && MNT_SHARED_WRITES(vp->v_mount))) {
3446 lock_flags = LK_SHARED;
3448 lock_flags = LK_EXCLUSIVE;
3450 vn_lock(vp, lock_flags | LK_RETRY);
3451 AUDIT_ARG_VNODE1(vp);
3452 if (vp->v_object != NULL) {
3453 VM_OBJECT_WLOCK(vp->v_object);
3454 vm_object_page_clean(vp->v_object, 0, 0, 0);
3455 VM_OBJECT_WUNLOCK(vp->v_object);
3457 error = VOP_FSYNC(vp, MNT_WAIT, td);
3460 vn_finished_write(mp);
3467 * Rename files. Source and destination must either both be directories, or
3468 * both not be directories. If target is a directory, it must be empty.
3470 #ifndef _SYS_SYSPROTO_H_
3471 struct rename_args {
3479 register struct rename_args /* {
3485 return (kern_rename(td, uap->from, uap->to, UIO_USERSPACE));
3488 #ifndef _SYS_SYSPROTO_H_
3489 struct renameat_args {
3497 sys_renameat(struct thread *td, struct renameat_args *uap)
3500 return (kern_renameat(td, uap->oldfd, uap->old, uap->newfd, uap->new,
3505 kern_rename(struct thread *td, char *from, char *to, enum uio_seg pathseg)
3508 return (kern_renameat(td, AT_FDCWD, from, AT_FDCWD, to, pathseg));
3512 kern_renameat(struct thread *td, int oldfd, char *old, int newfd, char *new,
3513 enum uio_seg pathseg)
3515 struct mount *mp = NULL;
3516 struct vnode *tvp, *fvp, *tdvp;
3517 struct nameidata fromnd, tond;
3518 cap_rights_t rights;
3523 NDINIT_ATRIGHTS(&fromnd, DELETE, LOCKPARENT | LOCKLEAF | SAVESTART |
3524 AUDITVNODE1, pathseg, old, oldfd,
3525 cap_rights_init(&rights, CAP_RENAMEAT), td);
3527 NDINIT_ATRIGHTS(&fromnd, DELETE, WANTPARENT | SAVESTART | AUDITVNODE1,
3528 pathseg, old, oldfd, cap_rights_init(&rights, CAP_RENAMEAT), td);
3531 if ((error = namei(&fromnd)) != 0)
3534 error = mac_vnode_check_rename_from(td->td_ucred, fromnd.ni_dvp,
3535 fromnd.ni_vp, &fromnd.ni_cnd);
3536 VOP_UNLOCK(fromnd.ni_dvp, 0);
3537 if (fromnd.ni_dvp != fromnd.ni_vp)
3538 VOP_UNLOCK(fromnd.ni_vp, 0);
3542 error = vn_start_write(fvp, &mp, V_WAIT | PCATCH);
3544 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3545 vrele(fromnd.ni_dvp);
3549 NDINIT_ATRIGHTS(&tond, RENAME, LOCKPARENT | LOCKLEAF | NOCACHE |
3550 SAVESTART | AUDITVNODE2, pathseg, new, newfd,
3551 cap_rights_init(&rights, CAP_LINKAT), td);
3552 if (fromnd.ni_vp->v_type == VDIR)
3553 tond.ni_cnd.cn_flags |= WILLBEDIR;
3554 if ((error = namei(&tond)) != 0) {
3555 /* Translate error code for rename("dir1", "dir2/."). */
3556 if (error == EISDIR && fvp->v_type == VDIR)
3558 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3559 vrele(fromnd.ni_dvp);
3561 vn_finished_write(mp);
3567 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
3570 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
3575 if (newfd != AT_FDCWD) {
3577 * If the target already exists we require CAP_UNLINKAT
3580 error = cap_check(&tond.ni_filecaps.fc_rights,
3581 cap_rights_init(&rights, CAP_UNLINKAT));
3592 * If the source is the same as the destination (that is, if they
3593 * are links to the same vnode), then there is nothing to do.
3599 error = mac_vnode_check_rename_to(td->td_ucred, tdvp,
3600 tond.ni_vp, fromnd.ni_dvp == tdvp, &tond.ni_cnd);
3604 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
3605 tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
3606 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3607 NDFREE(&tond, NDF_ONLY_PNBUF);
3609 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3610 NDFREE(&tond, NDF_ONLY_PNBUF);
3617 vrele(fromnd.ni_dvp);
3620 vrele(tond.ni_startdir);
3621 vn_finished_write(mp);
3623 if (fromnd.ni_startdir)
3624 vrele(fromnd.ni_startdir);
3631 * Make a directory file.
3633 #ifndef _SYS_SYSPROTO_H_
3642 register struct mkdir_args /* {
3648 return (kern_mkdir(td, uap->path, UIO_USERSPACE, uap->mode));
3651 #ifndef _SYS_SYSPROTO_H_
3652 struct mkdirat_args {
3659 sys_mkdirat(struct thread *td, struct mkdirat_args *uap)
3662 return (kern_mkdirat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode));
3666 kern_mkdir(struct thread *td, char *path, enum uio_seg segflg, int mode)
3669 return (kern_mkdirat(td, AT_FDCWD, path, segflg, mode));
3673 kern_mkdirat(struct thread *td, int fd, char *path, enum uio_seg segflg,
3679 struct nameidata nd;
3680 cap_rights_t rights;
3683 AUDIT_ARG_MODE(mode);
3686 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1,
3687 segflg, path, fd, cap_rights_init(&rights, CAP_MKDIRAT), td);
3688 nd.ni_cnd.cn_flags |= WILLBEDIR;
3689 if ((error = namei(&nd)) != 0)
3693 NDFREE(&nd, NDF_ONLY_PNBUF);
3695 * XXX namei called with LOCKPARENT but not LOCKLEAF has
3696 * the strange behaviour of leaving the vnode unlocked
3697 * if the target is the same vnode as the parent.
3699 if (vp == nd.ni_dvp)
3706 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3707 NDFREE(&nd, NDF_ONLY_PNBUF);
3709 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3714 vattr.va_type = VDIR;
3715 vattr.va_mode = (mode & ACCESSPERMS) &~ td->td_proc->p_fd->fd_cmask;
3717 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
3722 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
3726 NDFREE(&nd, NDF_ONLY_PNBUF);
3730 vn_finished_write(mp);
3735 * Remove a directory file.
3737 #ifndef _SYS_SYSPROTO_H_
3745 struct rmdir_args /* {
3750 return (kern_rmdir(td, uap->path, UIO_USERSPACE));
3754 kern_rmdir(struct thread *td, char *path, enum uio_seg pathseg)
3757 return (kern_rmdirat(td, AT_FDCWD, path, pathseg));
3761 kern_rmdirat(struct thread *td, int fd, char *path, enum uio_seg pathseg)
3765 struct nameidata nd;
3766 cap_rights_t rights;
3771 NDINIT_ATRIGHTS(&nd, DELETE, LOCKPARENT | LOCKLEAF | AUDITVNODE1,
3772 pathseg, path, fd, cap_rights_init(&rights, CAP_UNLINKAT), td);
3773 if ((error = namei(&nd)) != 0)
3776 if (vp->v_type != VDIR) {
3781 * No rmdir "." please.
3783 if (nd.ni_dvp == vp) {
3788 * The root of a mounted filesystem cannot be deleted.
3790 if (vp->v_vflag & VV_ROOT) {
3795 error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp,
3800 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3801 NDFREE(&nd, NDF_ONLY_PNBUF);
3803 if (nd.ni_dvp == vp)
3807 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3811 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_UNLINK);
3812 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
3813 vn_finished_write(mp);
3815 NDFREE(&nd, NDF_ONLY_PNBUF);
3817 if (nd.ni_dvp == vp)
3826 * Read a block of directory entries in a filesystem independent format.
3828 #ifndef _SYS_SYSPROTO_H_
3829 struct ogetdirentries_args {
3837 ogetdirentries(struct thread *td, struct ogetdirentries_args *uap)
3842 error = kern_ogetdirentries(td, uap, &loff);
3844 error = copyout(&loff, uap->basep, sizeof(long));
3849 kern_ogetdirentries(struct thread *td, struct ogetdirentries_args *uap,
3854 struct uio auio, kuio;
3855 struct iovec aiov, kiov;
3856 struct dirent *dp, *edp;
3857 cap_rights_t rights;
3859 int error, eofflag, readcnt;
3863 /* XXX arbitrary sanity limit on `count'. */
3864 if (uap->count > 64 * 1024)
3866 error = getvnode(td->td_proc->p_fd, uap->fd,
3867 cap_rights_init(&rights, CAP_READ), &fp);
3870 if ((fp->f_flag & FREAD) == 0) {
3875 foffset = foffset_lock(fp, 0);
3877 if (vp->v_type != VDIR) {
3878 foffset_unlock(fp, foffset, 0);
3882 aiov.iov_base = uap->buf;
3883 aiov.iov_len = uap->count;
3884 auio.uio_iov = &aiov;
3885 auio.uio_iovcnt = 1;
3886 auio.uio_rw = UIO_READ;
3887 auio.uio_segflg = UIO_USERSPACE;
3889 auio.uio_resid = uap->count;
3890 vn_lock(vp, LK_SHARED | LK_RETRY);
3891 loff = auio.uio_offset = foffset;
3893 error = mac_vnode_check_readdir(td->td_ucred, vp);
3896 foffset_unlock(fp, foffset, FOF_NOUPDATE);
3901 # if (BYTE_ORDER != LITTLE_ENDIAN)
3902 if (vp->v_mount->mnt_maxsymlinklen <= 0) {
3903 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag,
3905 foffset = auio.uio_offset;
3910 kuio.uio_iov = &kiov;
3911 kuio.uio_segflg = UIO_SYSSPACE;
3912 kiov.iov_len = uap->count;
3913 dirbuf = malloc(uap->count, M_TEMP, M_WAITOK);
3914 kiov.iov_base = dirbuf;
3915 error = VOP_READDIR(vp, &kuio, fp->f_cred, &eofflag,
3917 foffset = kuio.uio_offset;
3919 readcnt = uap->count - kuio.uio_resid;
3920 edp = (struct dirent *)&dirbuf[readcnt];
3921 for (dp = (struct dirent *)dirbuf; dp < edp; ) {
3922 # if (BYTE_ORDER == LITTLE_ENDIAN)
3924 * The expected low byte of
3925 * dp->d_namlen is our dp->d_type.
3926 * The high MBZ byte of dp->d_namlen
3927 * is our dp->d_namlen.
3929 dp->d_type = dp->d_namlen;
3933 * The dp->d_type is the high byte
3934 * of the expected dp->d_namlen,
3935 * so must be zero'ed.
3939 if (dp->d_reclen > 0) {
3940 dp = (struct dirent *)
3941 ((char *)dp + dp->d_reclen);
3948 error = uiomove(dirbuf, readcnt, &auio);
3950 free(dirbuf, M_TEMP);
3954 foffset_unlock(fp, foffset, 0);
3958 if (uap->count == auio.uio_resid &&
3959 (vp->v_vflag & VV_ROOT) &&
3960 (vp->v_mount->mnt_flag & MNT_UNION)) {
3961 struct vnode *tvp = vp;
3962 vp = vp->v_mount->mnt_vnodecovered;
3971 foffset_unlock(fp, foffset, 0);
3973 td->td_retval[0] = uap->count - auio.uio_resid;
3978 #endif /* COMPAT_43 */
3981 * Read a block of directory entries in a filesystem independent format.
3983 #ifndef _SYS_SYSPROTO_H_
3984 struct getdirentries_args {
3992 sys_getdirentries(td, uap)
3994 register struct getdirentries_args /* {
4004 error = kern_getdirentries(td, uap->fd, uap->buf, uap->count, &base,
4005 NULL, UIO_USERSPACE);
4008 if (uap->basep != NULL)
4009 error = copyout(&base, uap->basep, sizeof(long));
4014 kern_getdirentries(struct thread *td, int fd, char *buf, u_int count,
4015 long *basep, ssize_t *residp, enum uio_seg bufseg)
4021 cap_rights_t rights;
4027 if (count > IOSIZE_MAX)
4029 auio.uio_resid = count;
4030 error = getvnode(td->td_proc->p_fd, fd,
4031 cap_rights_init(&rights, CAP_READ), &fp);
4034 if ((fp->f_flag & FREAD) == 0) {
4039 foffset = foffset_lock(fp, 0);
4041 if (vp->v_type != VDIR) {
4045 aiov.iov_base = buf;
4046 aiov.iov_len = count;
4047 auio.uio_iov = &aiov;
4048 auio.uio_iovcnt = 1;
4049 auio.uio_rw = UIO_READ;
4050 auio.uio_segflg = bufseg;
4052 vn_lock(vp, LK_SHARED | LK_RETRY);
4053 AUDIT_ARG_VNODE1(vp);
4054 loff = auio.uio_offset = foffset;
4056 error = mac_vnode_check_readdir(td->td_ucred, vp);
4059 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL,
4061 foffset = auio.uio_offset;
4066 if (count == auio.uio_resid &&
4067 (vp->v_vflag & VV_ROOT) &&
4068 (vp->v_mount->mnt_flag & MNT_UNION)) {
4069 struct vnode *tvp = vp;
4071 vp = vp->v_mount->mnt_vnodecovered;
4082 *residp = auio.uio_resid;
4083 td->td_retval[0] = count - auio.uio_resid;
4085 foffset_unlock(fp, foffset, 0);
4090 #ifndef _SYS_SYSPROTO_H_
4091 struct getdents_args {
4098 sys_getdents(td, uap)
4100 register struct getdents_args /* {
4106 struct getdirentries_args ap;
4110 ap.count = uap->count;
4112 return (sys_getdirentries(td, &ap));
4116 * Set the mode mask for creation of filesystem nodes.
4118 #ifndef _SYS_SYSPROTO_H_
4126 struct umask_args /* {
4130 register struct filedesc *fdp;
4132 FILEDESC_XLOCK(td->td_proc->p_fd);
4133 fdp = td->td_proc->p_fd;
4134 td->td_retval[0] = fdp->fd_cmask;
4135 fdp->fd_cmask = uap->newmask & ALLPERMS;
4136 FILEDESC_XUNLOCK(td->td_proc->p_fd);
4141 * Void all references to file by ripping underlying filesystem away from
4144 #ifndef _SYS_SYSPROTO_H_
4145 struct revoke_args {
4152 register struct revoke_args /* {
4158 struct nameidata nd;
4161 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
4163 if ((error = namei(&nd)) != 0)
4166 NDFREE(&nd, NDF_ONLY_PNBUF);
4167 if (vp->v_type != VCHR || vp->v_rdev == NULL) {
4172 error = mac_vnode_check_revoke(td->td_ucred, vp);
4176 error = VOP_GETATTR(vp, &vattr, td->td_ucred);
4179 if (td->td_ucred->cr_uid != vattr.va_uid) {
4180 error = priv_check(td, PRIV_VFS_ADMIN);
4185 VOP_REVOKE(vp, REVOKEALL);
4192 * Convert a user file descriptor to a kernel file entry and check that, if it
4193 * is a capability, the correct rights are present. A reference on the file
4194 * entry is held upon returning.
4197 getvnode(struct filedesc *fdp, int fd, cap_rights_t *rightsp, struct file **fpp)
4202 error = fget_unlocked(fdp, fd, rightsp, 0, &fp, NULL);
4207 * The file could be not of the vnode type, or it may be not
4208 * yet fully initialized, in which case the f_vnode pointer
4209 * may be set, but f_ops is still badfileops. E.g.,
4210 * devfs_open() transiently create such situation to
4211 * facilitate csw d_fdopen().
4213 * Dupfdopen() handling in kern_openat() installs the
4214 * half-baked file into the process descriptor table, allowing
4215 * other thread to dereference it. Guard against the race by
4218 if (fp->f_vnode == NULL || fp->f_ops == &badfileops) {
4219 fdrop(fp, curthread);
4228 * Get an (NFS) file handle.
4230 #ifndef _SYS_SYSPROTO_H_
4231 struct lgetfh_args {
4239 register struct lgetfh_args *uap;
4241 struct nameidata nd;
4243 register struct vnode *vp;
4246 error = priv_check(td, PRIV_VFS_GETFH);
4249 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
4254 NDFREE(&nd, NDF_ONLY_PNBUF);
4256 bzero(&fh, sizeof(fh));
4257 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
4258 error = VOP_VPTOFH(vp, &fh.fh_fid);
4261 error = copyout(&fh, uap->fhp, sizeof (fh));
4265 #ifndef _SYS_SYSPROTO_H_
4274 register struct getfh_args *uap;
4276 struct nameidata nd;
4278 register struct vnode *vp;
4281 error = priv_check(td, PRIV_VFS_GETFH);
4284 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
4289 NDFREE(&nd, NDF_ONLY_PNBUF);
4291 bzero(&fh, sizeof(fh));
4292 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
4293 error = VOP_VPTOFH(vp, &fh.fh_fid);
4296 error = copyout(&fh, uap->fhp, sizeof (fh));
4301 * syscall for the rpc.lockd to use to translate a NFS file handle into an
4304 * warning: do not remove the priv_check() call or this becomes one giant
4307 #ifndef _SYS_SYSPROTO_H_
4308 struct fhopen_args {
4309 const struct fhandle *u_fhp;
4316 struct fhopen_args /* {
4317 const struct fhandle *u_fhp;
4328 error = priv_check(td, PRIV_VFS_FHOPEN);
4332 fmode = FFLAGS(uap->flags);
4333 /* why not allow a non-read/write open for our lockd? */
4334 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
4336 error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
4339 /* find the mount point */
4340 mp = vfs_busyfs(&fhp.fh_fsid);
4343 /* now give me my vnode, it gets returned to me locked */
4344 error = VFS_FHTOVP(mp, &fhp.fh_fid, LK_EXCLUSIVE, &vp);
4349 error = falloc_noinstall(td, &fp);
4355 * An extra reference on `fp' has been held for us by
4356 * falloc_noinstall().
4362 error = vn_open_vnode(vp, fmode, td->td_ucred, td, fp);
4364 KASSERT(fp->f_ops == &badfileops,
4365 ("VOP_OPEN in fhopen() set f_ops"));
4366 KASSERT(td->td_dupfd < 0,
4367 ("fhopen() encountered fdopen()"));
4377 finit(fp, (fmode & FMASK) | (fp->f_flag & FHASLOCK), DTYPE_VNODE, vp,
4380 if ((fmode & O_TRUNC) != 0) {
4381 error = fo_truncate(fp, 0, td->td_ucred, td);
4386 error = finstall(td, fp, &indx, fmode, NULL);
4389 td->td_retval[0] = indx;
4394 * Stat an (NFS) file handle.
4396 #ifndef _SYS_SYSPROTO_H_
4397 struct fhstat_args {
4398 struct fhandle *u_fhp;
4405 register struct fhstat_args /* {
4406 struct fhandle *u_fhp;
4414 error = copyin(uap->u_fhp, &fh, sizeof(fh));
4417 error = kern_fhstat(td, fh, &sb);
4419 error = copyout(&sb, uap->sb, sizeof(sb));
4424 kern_fhstat(struct thread *td, struct fhandle fh, struct stat *sb)
4430 error = priv_check(td, PRIV_VFS_FHSTAT);
4433 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4435 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_EXCLUSIVE, &vp);
4439 error = vn_stat(vp, sb, td->td_ucred, NOCRED, td);
4445 * Implement fstatfs() for (NFS) file handles.
4447 #ifndef _SYS_SYSPROTO_H_
4448 struct fhstatfs_args {
4449 struct fhandle *u_fhp;
4454 sys_fhstatfs(td, uap)
4456 struct fhstatfs_args /* {
4457 struct fhandle *u_fhp;
4465 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4468 error = kern_fhstatfs(td, fh, &sf);
4471 return (copyout(&sf, uap->buf, sizeof(sf)));
4475 kern_fhstatfs(struct thread *td, fhandle_t fh, struct statfs *buf)
4482 error = priv_check(td, PRIV_VFS_FHSTATFS);
4485 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4487 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_EXCLUSIVE, &vp);
4493 error = prison_canseemount(td->td_ucred, mp);
4497 error = mac_mount_check_stat(td->td_ucred, mp);
4502 * Set these in case the underlying filesystem fails to do so.
4505 sp->f_version = STATFS_VERSION;
4506 sp->f_namemax = NAME_MAX;
4507 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
4508 error = VFS_STATFS(mp, sp);
4517 kern_posix_fallocate(struct thread *td, int fd, off_t offset, off_t len)
4522 cap_rights_t rights;
4523 off_t olen, ooffset;
4527 error = fget(td, fd, cap_rights_init(&rights, CAP_WRITE), &fp);
4531 switch (fp->f_type) {
4542 if ((fp->f_flag & FWRITE) == 0) {
4547 if (vp->v_type != VREG) {
4551 if (offset < 0 || len <= 0) {
4555 /* Check for wrap. */
4556 if (offset > OFF_MAX - len) {
4561 /* Allocating blocks may take a long time, so iterate. */
4568 error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
4571 error = vn_lock(vp, LK_EXCLUSIVE);
4573 vn_finished_write(mp);
4577 error = mac_vnode_check_write(td->td_ucred, fp->f_cred, vp);
4580 error = VOP_ALLOCATE(vp, &offset, &len);
4582 vn_finished_write(mp);
4584 if (olen + ooffset != offset + len) {
4585 panic("offset + len changed from %jx/%jx to %jx/%jx",
4586 ooffset, olen, offset, len);
4588 if (error != 0 || len == 0)
4590 KASSERT(olen > len, ("Iteration did not make progress?"));
4600 sys_posix_fallocate(struct thread *td, struct posix_fallocate_args *uap)
4603 td->td_retval[0] = kern_posix_fallocate(td, uap->fd, uap->offset,
4609 * Unlike madvise(2), we do not make a best effort to remember every
4610 * possible caching hint. Instead, we remember the last setting with
4611 * the exception that we will allow POSIX_FADV_NORMAL to adjust the
4612 * region of any current setting.
4615 kern_posix_fadvise(struct thread *td, int fd, off_t offset, off_t len,
4618 struct fadvise_info *fa, *new;
4621 cap_rights_t rights;
4625 if (offset < 0 || len < 0 || offset > OFF_MAX - len)
4628 case POSIX_FADV_SEQUENTIAL:
4629 case POSIX_FADV_RANDOM:
4630 case POSIX_FADV_NOREUSE:
4631 new = malloc(sizeof(*fa), M_FADVISE, M_WAITOK);
4633 case POSIX_FADV_NORMAL:
4634 case POSIX_FADV_WILLNEED:
4635 case POSIX_FADV_DONTNEED:
4641 /* XXX: CAP_POSIX_FADVISE? */
4642 error = fget(td, fd, cap_rights_init(&rights), &fp);
4646 switch (fp->f_type) {
4658 if (vp->v_type != VREG) {
4665 end = offset + len - 1;
4667 case POSIX_FADV_SEQUENTIAL:
4668 case POSIX_FADV_RANDOM:
4669 case POSIX_FADV_NOREUSE:
4671 * Try to merge any existing non-standard region with
4672 * this new region if possible, otherwise create a new
4673 * non-standard region for this request.
4675 mtx_pool_lock(mtxpool_sleep, fp);
4677 if (fa != NULL && fa->fa_advice == advice &&
4678 ((fa->fa_start <= end && fa->fa_end >= offset) ||
4679 (end != OFF_MAX && fa->fa_start == end + 1) ||
4680 (fa->fa_end != OFF_MAX && fa->fa_end + 1 == offset))) {
4681 if (offset < fa->fa_start)
4682 fa->fa_start = offset;
4683 if (end > fa->fa_end)
4686 new->fa_advice = advice;
4687 new->fa_start = offset;
4689 new->fa_prevstart = 0;
4690 new->fa_prevend = 0;
4694 mtx_pool_unlock(mtxpool_sleep, fp);
4696 case POSIX_FADV_NORMAL:
4698 * If a the "normal" region overlaps with an existing
4699 * non-standard region, trim or remove the
4700 * non-standard region.
4702 mtx_pool_lock(mtxpool_sleep, fp);
4705 if (offset <= fa->fa_start && end >= fa->fa_end) {
4707 fp->f_advice = NULL;
4708 } else if (offset <= fa->fa_start &&
4709 end >= fa->fa_start)
4710 fa->fa_start = end + 1;
4711 else if (offset <= fa->fa_end && end >= fa->fa_end)
4712 fa->fa_end = offset - 1;
4713 else if (offset >= fa->fa_start && end <= fa->fa_end) {
4715 * If the "normal" region is a middle
4716 * portion of the existing
4717 * non-standard region, just remove
4718 * the whole thing rather than picking
4719 * one side or the other to
4723 fp->f_advice = NULL;
4726 mtx_pool_unlock(mtxpool_sleep, fp);
4728 case POSIX_FADV_WILLNEED:
4729 case POSIX_FADV_DONTNEED:
4730 error = VOP_ADVISE(vp, offset, end, advice);
4736 free(new, M_FADVISE);
4741 sys_posix_fadvise(struct thread *td, struct posix_fadvise_args *uap)
4744 td->td_retval[0] = kern_posix_fadvise(td, uap->fd, uap->offset,
4745 uap->len, uap->advice);