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 kern_chflags(struct thread *td, const char *path,
100 enum uio_seg pathseg, u_long flags);
101 static int kern_chflagsat(struct thread *td, int fd, const char *path,
102 enum uio_seg pathseg, u_long flags, int atflag);
103 static int setfflags(struct thread *td, struct vnode *, u_long);
104 static int getutimes(const struct timeval *, enum uio_seg, struct timespec *);
105 static int getutimens(const struct timespec *, enum uio_seg,
106 struct timespec *, int *);
107 static int setutimes(struct thread *td, struct vnode *,
108 const struct timespec *, int, int);
109 static int vn_access(struct vnode *vp, int user_flags, struct ucred *cred,
113 * The module initialization routine for POSIX asynchronous I/O will
114 * set this to the version of AIO that it implements. (Zero means
115 * that it is not implemented.) This value is used here by pathconf()
116 * and in kern_descrip.c by fpathconf().
118 int async_io_version;
121 * Sync each mounted filesystem.
123 #ifndef _SYS_SYSPROTO_H_
132 struct sync_args *uap;
134 struct mount *mp, *nmp;
137 mtx_lock(&mountlist_mtx);
138 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
139 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK)) {
140 nmp = TAILQ_NEXT(mp, mnt_list);
143 if ((mp->mnt_flag & MNT_RDONLY) == 0 &&
144 vn_start_write(NULL, &mp, V_NOWAIT) == 0) {
145 save = curthread_pflags_set(TDP_SYNCIO);
146 vfs_msync(mp, MNT_NOWAIT);
147 VFS_SYNC(mp, MNT_NOWAIT);
148 curthread_pflags_restore(save);
149 vn_finished_write(mp);
151 mtx_lock(&mountlist_mtx);
152 nmp = TAILQ_NEXT(mp, mnt_list);
155 mtx_unlock(&mountlist_mtx);
160 * Change filesystem quotas.
162 #ifndef _SYS_SYSPROTO_H_
163 struct quotactl_args {
171 sys_quotactl(td, uap)
173 register struct quotactl_args /* {
184 AUDIT_ARG_CMD(uap->cmd);
185 AUDIT_ARG_UID(uap->uid);
186 if (!prison_allow(td->td_ucred, PR_ALLOW_QUOTAS))
188 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
190 if ((error = namei(&nd)) != 0)
192 NDFREE(&nd, NDF_ONLY_PNBUF);
193 mp = nd.ni_vp->v_mount;
196 error = vfs_busy(mp, 0);
200 error = VFS_QUOTACTL(mp, uap->cmd, uap->uid, uap->arg);
203 * Since quota on operation typically needs to open quota
204 * file, the Q_QUOTAON handler needs to unbusy the mount point
205 * before calling into namei. Otherwise, unmount might be
206 * started between two vfs_busy() invocations (first is our,
207 * second is from mount point cross-walk code in lookup()),
210 * Require that Q_QUOTAON handles the vfs_busy() reference on
211 * its own, always returning with ubusied mount point.
213 if ((uap->cmd >> SUBCMDSHIFT) != Q_QUOTAON)
219 * Used by statfs conversion routines to scale the block size up if
220 * necessary so that all of the block counts are <= 'max_size'. Note
221 * that 'max_size' should be a bitmask, i.e. 2^n - 1 for some non-zero
225 statfs_scale_blocks(struct statfs *sf, long max_size)
230 KASSERT(powerof2(max_size + 1), ("%s: invalid max_size", __func__));
233 * Attempt to scale the block counts to give a more accurate
234 * overview to userland of the ratio of free space to used
235 * space. To do this, find the largest block count and compute
236 * a divisor that lets it fit into a signed integer <= max_size.
238 if (sf->f_bavail < 0)
239 count = -sf->f_bavail;
241 count = sf->f_bavail;
242 count = MAX(sf->f_blocks, MAX(sf->f_bfree, count));
243 if (count <= max_size)
246 count >>= flsl(max_size);
253 sf->f_bsize <<= shift;
254 sf->f_blocks >>= shift;
255 sf->f_bfree >>= shift;
256 sf->f_bavail >>= shift;
260 kern_do_statfs(struct thread *td, struct mount *mp, struct statfs *buf)
267 error = vfs_busy(mp, 0);
272 error = mac_mount_check_stat(td->td_ucred, mp);
277 * Set these in case the underlying filesystem fails to do so.
280 sp->f_version = STATFS_VERSION;
281 sp->f_namemax = NAME_MAX;
282 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
283 error = VFS_STATFS(mp, sp);
287 if (priv_check(td, PRIV_VFS_GENERATION)) {
288 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
289 prison_enforce_statfs(td->td_ucred, mp, buf);
297 * Get filesystem statistics.
299 #ifndef _SYS_SYSPROTO_H_
308 register struct statfs_args /* {
316 error = kern_statfs(td, uap->path, UIO_USERSPACE, &sf);
318 error = copyout(&sf, uap->buf, sizeof(sf));
323 kern_statfs(struct thread *td, char *path, enum uio_seg pathseg,
330 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
335 mp = nd.ni_vp->v_mount;
337 NDFREE(&nd, NDF_ONLY_PNBUF);
339 return (kern_do_statfs(td, mp, buf));
343 * Get filesystem statistics.
345 #ifndef _SYS_SYSPROTO_H_
346 struct fstatfs_args {
354 register struct fstatfs_args /* {
362 error = kern_fstatfs(td, uap->fd, &sf);
364 error = copyout(&sf, uap->buf, sizeof(sf));
369 kern_fstatfs(struct thread *td, int fd, struct statfs *buf)
378 error = getvnode(td->td_proc->p_fd, fd,
379 cap_rights_init(&rights, CAP_FSTATFS), &fp);
383 vn_lock(vp, LK_SHARED | LK_RETRY);
385 AUDIT_ARG_VNODE1(vp);
392 return (kern_do_statfs(td, mp, buf));
396 * Get statistics on all filesystems.
398 #ifndef _SYS_SYSPROTO_H_
399 struct getfsstat_args {
406 sys_getfsstat(td, uap)
408 register struct getfsstat_args /* {
415 return (kern_getfsstat(td, &uap->buf, uap->bufsize, UIO_USERSPACE,
420 * If (bufsize > 0 && bufseg == UIO_SYSSPACE)
421 * The caller is responsible for freeing memory which will be allocated
425 kern_getfsstat(struct thread *td, struct statfs **buf, size_t bufsize,
426 enum uio_seg bufseg, int flags)
428 struct mount *mp, *nmp;
429 struct statfs *sfsp, *sp, sb;
430 size_t count, maxcount;
433 maxcount = bufsize / sizeof(struct statfs);
436 else if (bufseg == UIO_USERSPACE)
438 else /* if (bufseg == UIO_SYSSPACE) */ {
440 mtx_lock(&mountlist_mtx);
441 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
444 mtx_unlock(&mountlist_mtx);
445 if (maxcount > count)
447 sfsp = *buf = malloc(maxcount * sizeof(struct statfs), M_TEMP,
451 mtx_lock(&mountlist_mtx);
452 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
453 if (prison_canseemount(td->td_ucred, mp) != 0) {
454 nmp = TAILQ_NEXT(mp, mnt_list);
458 if (mac_mount_check_stat(td->td_ucred, mp) != 0) {
459 nmp = TAILQ_NEXT(mp, mnt_list);
463 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK)) {
464 nmp = TAILQ_NEXT(mp, mnt_list);
467 if (sfsp != NULL && count < maxcount) {
470 * Set these in case the underlying filesystem
473 sp->f_version = STATFS_VERSION;
474 sp->f_namemax = NAME_MAX;
475 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
477 * If MNT_NOWAIT or MNT_LAZY is specified, do not
478 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
479 * overrides MNT_WAIT.
481 if (((flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
482 (flags & MNT_WAIT)) &&
483 (error = VFS_STATFS(mp, sp))) {
484 mtx_lock(&mountlist_mtx);
485 nmp = TAILQ_NEXT(mp, mnt_list);
489 if (priv_check(td, PRIV_VFS_GENERATION)) {
490 bcopy(sp, &sb, sizeof(sb));
491 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
492 prison_enforce_statfs(td->td_ucred, mp, &sb);
495 if (bufseg == UIO_SYSSPACE)
496 bcopy(sp, sfsp, sizeof(*sp));
497 else /* if (bufseg == UIO_USERSPACE) */ {
498 error = copyout(sp, sfsp, sizeof(*sp));
507 mtx_lock(&mountlist_mtx);
508 nmp = TAILQ_NEXT(mp, mnt_list);
511 mtx_unlock(&mountlist_mtx);
512 if (sfsp != NULL && count > maxcount)
513 td->td_retval[0] = maxcount;
515 td->td_retval[0] = count;
519 #ifdef COMPAT_FREEBSD4
521 * Get old format filesystem statistics.
523 static void cvtstatfs(struct statfs *, struct ostatfs *);
525 #ifndef _SYS_SYSPROTO_H_
526 struct freebsd4_statfs_args {
532 freebsd4_statfs(td, uap)
534 struct freebsd4_statfs_args /* {
543 error = kern_statfs(td, uap->path, UIO_USERSPACE, &sf);
546 cvtstatfs(&sf, &osb);
547 return (copyout(&osb, uap->buf, sizeof(osb)));
551 * Get filesystem statistics.
553 #ifndef _SYS_SYSPROTO_H_
554 struct freebsd4_fstatfs_args {
560 freebsd4_fstatfs(td, uap)
562 struct freebsd4_fstatfs_args /* {
571 error = kern_fstatfs(td, uap->fd, &sf);
574 cvtstatfs(&sf, &osb);
575 return (copyout(&osb, uap->buf, sizeof(osb)));
579 * Get statistics on all filesystems.
581 #ifndef _SYS_SYSPROTO_H_
582 struct freebsd4_getfsstat_args {
589 freebsd4_getfsstat(td, uap)
591 register struct freebsd4_getfsstat_args /* {
597 struct statfs *buf, *sp;
602 count = uap->bufsize / sizeof(struct ostatfs);
603 size = count * sizeof(struct statfs);
604 error = kern_getfsstat(td, &buf, size, UIO_SYSSPACE, uap->flags);
606 count = td->td_retval[0];
608 while (count > 0 && error == 0) {
610 error = copyout(&osb, uap->buf, sizeof(osb));
621 * Implement fstatfs() for (NFS) file handles.
623 #ifndef _SYS_SYSPROTO_H_
624 struct freebsd4_fhstatfs_args {
625 struct fhandle *u_fhp;
630 freebsd4_fhstatfs(td, uap)
632 struct freebsd4_fhstatfs_args /* {
633 struct fhandle *u_fhp;
642 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
645 error = kern_fhstatfs(td, fh, &sf);
648 cvtstatfs(&sf, &osb);
649 return (copyout(&osb, uap->buf, sizeof(osb)));
653 * Convert a new format statfs structure to an old format statfs structure.
661 statfs_scale_blocks(nsp, LONG_MAX);
662 bzero(osp, sizeof(*osp));
663 osp->f_bsize = nsp->f_bsize;
664 osp->f_iosize = MIN(nsp->f_iosize, LONG_MAX);
665 osp->f_blocks = nsp->f_blocks;
666 osp->f_bfree = nsp->f_bfree;
667 osp->f_bavail = nsp->f_bavail;
668 osp->f_files = MIN(nsp->f_files, LONG_MAX);
669 osp->f_ffree = MIN(nsp->f_ffree, LONG_MAX);
670 osp->f_owner = nsp->f_owner;
671 osp->f_type = nsp->f_type;
672 osp->f_flags = nsp->f_flags;
673 osp->f_syncwrites = MIN(nsp->f_syncwrites, LONG_MAX);
674 osp->f_asyncwrites = MIN(nsp->f_asyncwrites, LONG_MAX);
675 osp->f_syncreads = MIN(nsp->f_syncreads, LONG_MAX);
676 osp->f_asyncreads = MIN(nsp->f_asyncreads, LONG_MAX);
677 strlcpy(osp->f_fstypename, nsp->f_fstypename,
678 MIN(MFSNAMELEN, OMFSNAMELEN));
679 strlcpy(osp->f_mntonname, nsp->f_mntonname,
680 MIN(MNAMELEN, OMNAMELEN));
681 strlcpy(osp->f_mntfromname, nsp->f_mntfromname,
682 MIN(MNAMELEN, OMNAMELEN));
683 osp->f_fsid = nsp->f_fsid;
685 #endif /* COMPAT_FREEBSD4 */
688 * Change current working directory to a given file descriptor.
690 #ifndef _SYS_SYSPROTO_H_
698 struct fchdir_args /* {
702 register struct filedesc *fdp = td->td_proc->p_fd;
703 struct vnode *vp, *tdp, *vpold;
709 AUDIT_ARG_FD(uap->fd);
710 error = getvnode(fdp, uap->fd, cap_rights_init(&rights, CAP_FCHDIR),
717 vn_lock(vp, LK_SHARED | LK_RETRY);
718 AUDIT_ARG_VNODE1(vp);
719 error = change_dir(vp, td);
720 while (!error && (mp = vp->v_mountedhere) != NULL) {
723 error = VFS_ROOT(mp, LK_SHARED, &tdp);
736 vpold = fdp->fd_cdir;
738 FILEDESC_XUNLOCK(fdp);
744 * Change current working directory (``.'').
746 #ifndef _SYS_SYSPROTO_H_
754 struct chdir_args /* {
759 return (kern_chdir(td, uap->path, UIO_USERSPACE));
763 kern_chdir(struct thread *td, char *path, enum uio_seg pathseg)
765 register struct filedesc *fdp = td->td_proc->p_fd;
770 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
772 if ((error = namei(&nd)) != 0)
774 if ((error = change_dir(nd.ni_vp, td)) != 0) {
776 NDFREE(&nd, NDF_ONLY_PNBUF);
779 VOP_UNLOCK(nd.ni_vp, 0);
780 NDFREE(&nd, NDF_ONLY_PNBUF);
783 fdp->fd_cdir = nd.ni_vp;
784 FILEDESC_XUNLOCK(fdp);
790 * Helper function for raised chroot(2) security function: Refuse if
791 * any filedescriptors are open directories.
794 chroot_refuse_vdir_fds(fdp)
795 struct filedesc *fdp;
801 FILEDESC_LOCK_ASSERT(fdp);
803 for (fd = 0; fd <= fdp->fd_lastfile; fd++) {
804 fp = fget_locked(fdp, fd);
807 if (fp->f_type == DTYPE_VNODE) {
809 if (vp->v_type == VDIR)
817 * This sysctl determines if we will allow a process to chroot(2) if it
818 * has a directory open:
819 * 0: disallowed for all processes.
820 * 1: allowed for processes that were not already chroot(2)'ed.
821 * 2: allowed for all processes.
824 static int chroot_allow_open_directories = 1;
826 SYSCTL_INT(_kern, OID_AUTO, chroot_allow_open_directories, CTLFLAG_RW,
827 &chroot_allow_open_directories, 0,
828 "Allow a process to chroot(2) if it has a directory open");
831 * Change notion of root (``/'') directory.
833 #ifndef _SYS_SYSPROTO_H_
841 struct chroot_args /* {
848 error = priv_check(td, PRIV_VFS_CHROOT);
851 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
852 UIO_USERSPACE, uap->path, td);
856 error = change_dir(nd.ni_vp, td);
860 error = mac_vnode_check_chroot(td->td_ucred, nd.ni_vp);
864 VOP_UNLOCK(nd.ni_vp, 0);
865 error = change_root(nd.ni_vp, td);
867 NDFREE(&nd, NDF_ONLY_PNBUF);
872 NDFREE(&nd, NDF_ONLY_PNBUF);
877 * Common routine for chroot and chdir. Callers must provide a locked vnode
889 ASSERT_VOP_LOCKED(vp, "change_dir(): vp not locked");
890 if (vp->v_type != VDIR)
893 error = mac_vnode_check_chdir(td->td_ucred, vp);
897 return (VOP_ACCESS(vp, VEXEC, td->td_ucred, td));
901 * Common routine for kern_chroot() and jail_attach(). The caller is
902 * responsible for invoking priv_check() and mac_vnode_check_chroot() to
903 * authorize this operation.
910 struct filedesc *fdp;
914 fdp = td->td_proc->p_fd;
916 if (chroot_allow_open_directories == 0 ||
917 (chroot_allow_open_directories == 1 && fdp->fd_rdir != rootvnode)) {
918 error = chroot_refuse_vdir_fds(fdp);
920 FILEDESC_XUNLOCK(fdp);
924 oldvp = fdp->fd_rdir;
931 FILEDESC_XUNLOCK(fdp);
937 flags_to_rights(int flags, cap_rights_t *rightsp)
940 if (flags & O_EXEC) {
941 cap_rights_set(rightsp, CAP_FEXECVE);
943 switch ((flags & O_ACCMODE)) {
945 cap_rights_set(rightsp, CAP_READ);
948 cap_rights_set(rightsp, CAP_READ);
951 cap_rights_set(rightsp, CAP_WRITE);
952 if (!(flags & (O_APPEND | O_TRUNC)))
953 cap_rights_set(rightsp, CAP_SEEK);
959 cap_rights_set(rightsp, CAP_CREATE);
962 cap_rights_set(rightsp, CAP_FTRUNCATE);
964 if (flags & (O_SYNC | O_FSYNC))
965 cap_rights_set(rightsp, CAP_FSYNC);
967 if (flags & (O_EXLOCK | O_SHLOCK))
968 cap_rights_set(rightsp, CAP_FLOCK);
972 * Check permissions, allocate an open file structure, and call the device
973 * open routine if any.
975 #ifndef _SYS_SYSPROTO_H_
985 register struct open_args /* {
992 return (kern_open(td, uap->path, UIO_USERSPACE, uap->flags, uap->mode));
995 #ifndef _SYS_SYSPROTO_H_
1004 sys_openat(struct thread *td, struct openat_args *uap)
1007 return (kern_openat(td, uap->fd, uap->path, UIO_USERSPACE, uap->flag,
1012 kern_open(struct thread *td, char *path, enum uio_seg pathseg, int flags,
1016 return (kern_openat(td, AT_FDCWD, path, pathseg, flags, mode));
1020 kern_openat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1021 int flags, int mode)
1023 struct proc *p = td->td_proc;
1024 struct filedesc *fdp = p->p_fd;
1027 struct nameidata nd;
1028 cap_rights_t rights;
1029 int cmode, error, indx;
1033 AUDIT_ARG_FFLAGS(flags);
1034 AUDIT_ARG_MODE(mode);
1035 /* XXX: audit dirfd */
1036 cap_rights_init(&rights, CAP_LOOKUP);
1037 flags_to_rights(flags, &rights);
1039 * Only one of the O_EXEC, O_RDONLY, O_WRONLY and O_RDWR flags
1042 if (flags & O_EXEC) {
1043 if (flags & O_ACCMODE)
1045 } else if ((flags & O_ACCMODE) == O_ACCMODE) {
1048 flags = FFLAGS(flags);
1052 * Allocate the file descriptor, but don't install a descriptor yet.
1054 error = falloc_noinstall(td, &fp);
1058 * An extra reference on `fp' has been held for us by
1059 * falloc_noinstall().
1061 /* Set the flags early so the finit in devfs can pick them up. */
1062 fp->f_flag = flags & FMASK;
1063 cmode = ((mode & ~fdp->fd_cmask) & ALLPERMS) & ~S_ISTXT;
1064 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, fd,
1066 td->td_dupfd = -1; /* XXX check for fdopen */
1067 error = vn_open(&nd, &flags, cmode, fp);
1070 * If the vn_open replaced the method vector, something
1071 * wonderous happened deep below and we just pass it up
1072 * pretending we know what we do.
1074 if (error == ENXIO && fp->f_ops != &badfileops)
1078 * Handle special fdopen() case. bleh.
1080 * Don't do this for relative (capability) lookups; we don't
1081 * understand exactly what would happen, and we don't think
1082 * that it ever should.
1084 if (nd.ni_strictrelative == 0 &&
1085 (error == ENODEV || error == ENXIO) &&
1086 td->td_dupfd >= 0) {
1087 error = dupfdopen(td, fdp, td->td_dupfd, flags, error,
1096 NDFREE(&nd, NDF_ONLY_PNBUF);
1100 * Store the vnode, for any f_type. Typically, the vnode use
1101 * count is decremented by direct call to vn_closefile() for
1102 * files that switched type in the cdevsw fdopen() method.
1106 * If the file wasn't claimed by devfs bind it to the normal
1107 * vnode operations here.
1109 if (fp->f_ops == &badfileops) {
1110 KASSERT(vp->v_type != VFIFO, ("Unexpected fifo."));
1112 finit(fp, (flags & FMASK) | (fp->f_flag & FHASLOCK),
1113 DTYPE_VNODE, vp, &vnops);
1117 if (flags & O_TRUNC) {
1118 error = fo_truncate(fp, 0, td->td_ucred, td);
1124 * If we haven't already installed the FD (for dupfdopen), do so now.
1127 struct filecaps *fcaps;
1130 if (nd.ni_strictrelative == 1)
1131 fcaps = &nd.ni_filecaps;
1135 error = finstall(td, fp, &indx, flags, fcaps);
1136 /* On success finstall() consumes fcaps. */
1138 filecaps_free(&nd.ni_filecaps);
1142 filecaps_free(&nd.ni_filecaps);
1146 * Release our private reference, leaving the one associated with
1147 * the descriptor table intact.
1150 td->td_retval[0] = indx;
1153 KASSERT(indx == -1, ("indx=%d, should be -1", indx));
1162 #ifndef _SYS_SYSPROTO_H_
1163 struct ocreat_args {
1171 register struct ocreat_args /* {
1177 return (kern_open(td, uap->path, UIO_USERSPACE,
1178 O_WRONLY | O_CREAT | O_TRUNC, uap->mode));
1180 #endif /* COMPAT_43 */
1183 * Create a special file.
1185 #ifndef _SYS_SYSPROTO_H_
1195 register struct mknod_args /* {
1202 return (kern_mknod(td, uap->path, UIO_USERSPACE, uap->mode, uap->dev));
1205 #ifndef _SYS_SYSPROTO_H_
1206 struct mknodat_args {
1214 sys_mknodat(struct thread *td, struct mknodat_args *uap)
1217 return (kern_mknodat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode,
1222 kern_mknod(struct thread *td, char *path, enum uio_seg pathseg, int mode,
1226 return (kern_mknodat(td, AT_FDCWD, path, pathseg, mode, dev));
1230 kern_mknodat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1236 struct nameidata nd;
1237 cap_rights_t rights;
1238 int error, whiteout = 0;
1240 AUDIT_ARG_MODE(mode);
1242 switch (mode & S_IFMT) {
1245 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
1246 if (error == 0 && dev == VNOVAL)
1250 error = priv_check(td, PRIV_VFS_MKNOD_BAD);
1253 error = priv_check(td, PRIV_VFS_MKNOD_WHT);
1257 return (kern_mkfifoat(td, fd, path, pathseg, mode));
1267 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
1268 NOCACHE, pathseg, path, fd, cap_rights_init(&rights, CAP_MKNODAT),
1270 if ((error = namei(&nd)) != 0)
1274 NDFREE(&nd, NDF_ONLY_PNBUF);
1275 if (vp == nd.ni_dvp)
1283 vattr.va_mode = (mode & ALLPERMS) &
1284 ~td->td_proc->p_fd->fd_cmask;
1285 vattr.va_rdev = dev;
1288 switch (mode & S_IFMT) {
1289 case S_IFMT: /* used by badsect to flag bad sectors */
1290 vattr.va_type = VBAD;
1293 vattr.va_type = VCHR;
1296 vattr.va_type = VBLK;
1302 panic("kern_mknod: invalid mode");
1305 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1306 NDFREE(&nd, NDF_ONLY_PNBUF);
1308 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1313 if (error == 0 && !whiteout)
1314 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp,
1315 &nd.ni_cnd, &vattr);
1319 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, CREATE);
1321 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp,
1322 &nd.ni_cnd, &vattr);
1327 NDFREE(&nd, NDF_ONLY_PNBUF);
1329 vn_finished_write(mp);
1334 * Create a named pipe.
1336 #ifndef _SYS_SYSPROTO_H_
1337 struct mkfifo_args {
1345 register struct mkfifo_args /* {
1351 return (kern_mkfifo(td, uap->path, UIO_USERSPACE, uap->mode));
1354 #ifndef _SYS_SYSPROTO_H_
1355 struct mkfifoat_args {
1362 sys_mkfifoat(struct thread *td, struct mkfifoat_args *uap)
1365 return (kern_mkfifoat(td, uap->fd, uap->path, UIO_USERSPACE,
1370 kern_mkfifo(struct thread *td, char *path, enum uio_seg pathseg, int mode)
1373 return (kern_mkfifoat(td, AT_FDCWD, path, pathseg, mode));
1377 kern_mkfifoat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1382 struct nameidata nd;
1383 cap_rights_t rights;
1386 AUDIT_ARG_MODE(mode);
1389 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
1390 NOCACHE, pathseg, path, fd, cap_rights_init(&rights, CAP_MKFIFOAT),
1392 if ((error = namei(&nd)) != 0)
1394 if (nd.ni_vp != NULL) {
1395 NDFREE(&nd, NDF_ONLY_PNBUF);
1396 if (nd.ni_vp == nd.ni_dvp)
1403 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1404 NDFREE(&nd, NDF_ONLY_PNBUF);
1406 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1411 vattr.va_type = VFIFO;
1412 vattr.va_mode = (mode & ALLPERMS) & ~td->td_proc->p_fd->fd_cmask;
1414 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1419 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
1426 vn_finished_write(mp);
1427 NDFREE(&nd, NDF_ONLY_PNBUF);
1432 * Make a hard file link.
1434 #ifndef _SYS_SYSPROTO_H_
1443 register struct link_args /* {
1449 return (kern_link(td, uap->path, uap->link, UIO_USERSPACE));
1452 #ifndef _SYS_SYSPROTO_H_
1453 struct linkat_args {
1462 sys_linkat(struct thread *td, struct linkat_args *uap)
1467 if (flag & ~AT_SYMLINK_FOLLOW)
1470 return (kern_linkat(td, uap->fd1, uap->fd2, uap->path1, uap->path2,
1471 UIO_USERSPACE, (flag & AT_SYMLINK_FOLLOW) ? FOLLOW : NOFOLLOW));
1474 int hardlink_check_uid = 0;
1475 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
1476 &hardlink_check_uid, 0,
1477 "Unprivileged processes cannot create hard links to files owned by other "
1479 static int hardlink_check_gid = 0;
1480 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
1481 &hardlink_check_gid, 0,
1482 "Unprivileged processes cannot create hard links to files owned by other "
1486 can_hardlink(struct vnode *vp, struct ucred *cred)
1491 if (!hardlink_check_uid && !hardlink_check_gid)
1494 error = VOP_GETATTR(vp, &va, cred);
1498 if (hardlink_check_uid && cred->cr_uid != va.va_uid) {
1499 error = priv_check_cred(cred, PRIV_VFS_LINK, 0);
1504 if (hardlink_check_gid && !groupmember(va.va_gid, cred)) {
1505 error = priv_check_cred(cred, PRIV_VFS_LINK, 0);
1514 kern_link(struct thread *td, char *path, char *link, enum uio_seg segflg)
1517 return (kern_linkat(td, AT_FDCWD, AT_FDCWD, path,link, segflg, FOLLOW));
1521 kern_linkat(struct thread *td, int fd1, int fd2, char *path1, char *path2,
1522 enum uio_seg segflg, int follow)
1526 struct nameidata nd;
1527 cap_rights_t rights;
1532 NDINIT_AT(&nd, LOOKUP, follow | AUDITVNODE1, segflg, path1, fd1, td);
1534 if ((error = namei(&nd)) != 0)
1536 NDFREE(&nd, NDF_ONLY_PNBUF);
1538 if (vp->v_type == VDIR) {
1540 return (EPERM); /* POSIX */
1542 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE2 |
1543 NOCACHE, segflg, path2, fd2, cap_rights_init(&rights, CAP_LINKAT),
1545 if ((error = namei(&nd)) == 0) {
1546 if (nd.ni_vp != NULL) {
1547 NDFREE(&nd, NDF_ONLY_PNBUF);
1548 if (nd.ni_dvp == nd.ni_vp)
1555 } else if (nd.ni_dvp->v_mount != vp->v_mount) {
1557 * Cross-device link. No need to recheck
1558 * vp->v_type, since it cannot change, except
1561 NDFREE(&nd, NDF_ONLY_PNBUF);
1565 } else if ((error = vn_lock(vp, LK_EXCLUSIVE)) == 0) {
1566 error = can_hardlink(vp, td->td_ucred);
1569 error = mac_vnode_check_link(td->td_ucred,
1570 nd.ni_dvp, vp, &nd.ni_cnd);
1575 NDFREE(&nd, NDF_ONLY_PNBUF);
1578 error = vn_start_write(vp, &mp, V_NOWAIT);
1582 NDFREE(&nd, NDF_ONLY_PNBUF);
1583 error = vn_start_write(NULL, &mp,
1589 error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
1592 vn_finished_write(mp);
1593 NDFREE(&nd, NDF_ONLY_PNBUF);
1596 NDFREE(&nd, NDF_ONLY_PNBUF);
1606 * Make a symbolic link.
1608 #ifndef _SYS_SYSPROTO_H_
1609 struct symlink_args {
1615 sys_symlink(td, uap)
1617 register struct symlink_args /* {
1623 return (kern_symlink(td, uap->path, uap->link, UIO_USERSPACE));
1626 #ifndef _SYS_SYSPROTO_H_
1627 struct symlinkat_args {
1634 sys_symlinkat(struct thread *td, struct symlinkat_args *uap)
1637 return (kern_symlinkat(td, uap->path1, uap->fd, uap->path2,
1642 kern_symlink(struct thread *td, char *path, char *link, enum uio_seg segflg)
1645 return (kern_symlinkat(td, path, AT_FDCWD, link, segflg));
1649 kern_symlinkat(struct thread *td, char *path1, int fd, char *path2,
1650 enum uio_seg segflg)
1655 struct nameidata nd;
1657 cap_rights_t rights;
1659 if (segflg == UIO_SYSSPACE) {
1662 syspath = uma_zalloc(namei_zone, M_WAITOK);
1663 if ((error = copyinstr(path1, syspath, MAXPATHLEN, NULL)) != 0)
1666 AUDIT_ARG_TEXT(syspath);
1669 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
1670 NOCACHE, segflg, path2, fd, cap_rights_init(&rights, CAP_SYMLINKAT),
1672 if ((error = namei(&nd)) != 0)
1675 NDFREE(&nd, NDF_ONLY_PNBUF);
1676 if (nd.ni_vp == nd.ni_dvp)
1684 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1685 NDFREE(&nd, NDF_ONLY_PNBUF);
1687 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1692 vattr.va_mode = ACCESSPERMS &~ td->td_proc->p_fd->fd_cmask;
1694 vattr.va_type = VLNK;
1695 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1700 error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr, syspath);
1706 NDFREE(&nd, NDF_ONLY_PNBUF);
1708 vn_finished_write(mp);
1710 if (segflg != UIO_SYSSPACE)
1711 uma_zfree(namei_zone, syspath);
1716 * Delete a whiteout from the filesystem.
1719 sys_undelete(td, uap)
1721 register struct undelete_args /* {
1726 struct nameidata nd;
1731 NDINIT(&nd, DELETE, LOCKPARENT | DOWHITEOUT | AUDITVNODE1,
1732 UIO_USERSPACE, uap->path, td);
1737 if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & ISWHITEOUT)) {
1738 NDFREE(&nd, NDF_ONLY_PNBUF);
1739 if (nd.ni_vp == nd.ni_dvp)
1747 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1748 NDFREE(&nd, NDF_ONLY_PNBUF);
1750 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1754 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, DELETE);
1755 NDFREE(&nd, NDF_ONLY_PNBUF);
1757 vn_finished_write(mp);
1762 * Delete a name from the filesystem.
1764 #ifndef _SYS_SYSPROTO_H_
1765 struct unlink_args {
1772 struct unlink_args /* {
1777 return (kern_unlink(td, uap->path, UIO_USERSPACE));
1780 #ifndef _SYS_SYSPROTO_H_
1781 struct unlinkat_args {
1788 sys_unlinkat(struct thread *td, struct unlinkat_args *uap)
1790 int flag = uap->flag;
1792 char *path = uap->path;
1794 if (flag & ~AT_REMOVEDIR)
1797 if (flag & AT_REMOVEDIR)
1798 return (kern_rmdirat(td, fd, path, UIO_USERSPACE));
1800 return (kern_unlinkat(td, fd, path, UIO_USERSPACE, 0));
1804 kern_unlink(struct thread *td, char *path, enum uio_seg pathseg)
1807 return (kern_unlinkat(td, AT_FDCWD, path, pathseg, 0));
1811 kern_unlinkat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1816 struct nameidata nd;
1818 cap_rights_t rights;
1823 NDINIT_ATRIGHTS(&nd, DELETE, LOCKPARENT | LOCKLEAF | AUDITVNODE1,
1824 pathseg, path, fd, cap_rights_init(&rights, CAP_UNLINKAT), td);
1825 if ((error = namei(&nd)) != 0)
1826 return (error == EINVAL ? EPERM : error);
1828 if (vp->v_type == VDIR && oldinum == 0) {
1829 error = EPERM; /* POSIX */
1830 } else if (oldinum != 0 &&
1831 ((error = vn_stat(vp, &sb, td->td_ucred, NOCRED, td)) == 0) &&
1832 sb.st_ino != oldinum) {
1833 error = EIDRM; /* Identifier removed */
1836 * The root of a mounted filesystem cannot be deleted.
1838 * XXX: can this only be a VDIR case?
1840 if (vp->v_vflag & VV_ROOT)
1844 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1845 NDFREE(&nd, NDF_ONLY_PNBUF);
1847 if (vp == nd.ni_dvp)
1851 if ((error = vn_start_write(NULL, &mp,
1852 V_XSLEEP | PCATCH)) != 0)
1857 error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp,
1862 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_UNLINK);
1863 error = VOP_REMOVE(nd.ni_dvp, vp, &nd.ni_cnd);
1867 vn_finished_write(mp);
1869 NDFREE(&nd, NDF_ONLY_PNBUF);
1871 if (vp == nd.ni_dvp)
1879 * Reposition read/write file offset.
1881 #ifndef _SYS_SYSPROTO_H_
1892 register struct lseek_args /* {
1900 cap_rights_t rights;
1903 AUDIT_ARG_FD(uap->fd);
1904 error = fget(td, uap->fd, cap_rights_init(&rights, CAP_SEEK), &fp);
1907 error = (fp->f_ops->fo_flags & DFLAG_SEEKABLE) != 0 ?
1908 fo_seek(fp, uap->offset, uap->whence, td) : ESPIPE;
1913 #if defined(COMPAT_43)
1915 * Reposition read/write file offset.
1917 #ifndef _SYS_SYSPROTO_H_
1918 struct olseek_args {
1927 register struct olseek_args /* {
1933 struct lseek_args /* {
1941 nuap.offset = uap->offset;
1942 nuap.whence = uap->whence;
1943 return (sys_lseek(td, &nuap));
1945 #endif /* COMPAT_43 */
1947 /* Version with the 'pad' argument */
1949 freebsd6_lseek(td, uap)
1951 register struct freebsd6_lseek_args *uap;
1953 struct lseek_args ouap;
1956 ouap.offset = uap->offset;
1957 ouap.whence = uap->whence;
1958 return (sys_lseek(td, &ouap));
1962 * Check access permissions using passed credentials.
1965 vn_access(vp, user_flags, cred, td)
1974 /* Flags == 0 means only check for existence. */
1978 if (user_flags & R_OK)
1980 if (user_flags & W_OK)
1982 if (user_flags & X_OK)
1985 error = mac_vnode_check_access(cred, vp, accmode);
1989 if ((accmode & VWRITE) == 0 || (error = vn_writechk(vp)) == 0)
1990 error = VOP_ACCESS(vp, accmode, cred, td);
1996 * Check access permissions using "real" credentials.
1998 #ifndef _SYS_SYSPROTO_H_
1999 struct access_args {
2007 register struct access_args /* {
2013 return (kern_access(td, uap->path, UIO_USERSPACE, uap->amode));
2016 #ifndef _SYS_SYSPROTO_H_
2017 struct faccessat_args {
2025 sys_faccessat(struct thread *td, struct faccessat_args *uap)
2028 if (uap->flag & ~AT_EACCESS)
2030 return (kern_accessat(td, uap->fd, uap->path, UIO_USERSPACE, uap->flag,
2035 kern_access(struct thread *td, char *path, enum uio_seg pathseg, int amode)
2038 return (kern_accessat(td, AT_FDCWD, path, pathseg, 0, amode));
2042 kern_accessat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2043 int flag, int amode)
2045 struct ucred *cred, *tmpcred;
2047 struct nameidata nd;
2048 cap_rights_t rights;
2052 * Create and modify a temporary credential instead of one that
2053 * is potentially shared.
2055 if (!(flag & AT_EACCESS)) {
2056 cred = td->td_ucred;
2057 tmpcred = crdup(cred);
2058 tmpcred->cr_uid = cred->cr_ruid;
2059 tmpcred->cr_groups[0] = cred->cr_rgid;
2060 td->td_ucred = tmpcred;
2062 cred = tmpcred = td->td_ucred;
2063 AUDIT_ARG_VALUE(amode);
2064 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF |
2065 AUDITVNODE1, pathseg, path, fd, cap_rights_init(&rights, CAP_FSTAT),
2067 if ((error = namei(&nd)) != 0)
2071 error = vn_access(vp, amode, tmpcred, td);
2072 NDFREE(&nd, NDF_ONLY_PNBUF);
2075 if (!(flag & AT_EACCESS)) {
2076 td->td_ucred = cred;
2083 * Check access permissions using "effective" credentials.
2085 #ifndef _SYS_SYSPROTO_H_
2086 struct eaccess_args {
2092 sys_eaccess(td, uap)
2094 register struct eaccess_args /* {
2100 return (kern_eaccess(td, uap->path, UIO_USERSPACE, uap->amode));
2104 kern_eaccess(struct thread *td, char *path, enum uio_seg pathseg, int amode)
2107 return (kern_accessat(td, AT_FDCWD, path, pathseg, AT_EACCESS, amode));
2110 #if defined(COMPAT_43)
2112 * Get file status; this version follows links.
2114 #ifndef _SYS_SYSPROTO_H_
2123 register struct ostat_args /* {
2132 error = kern_stat(td, uap->path, UIO_USERSPACE, &sb);
2136 return (copyout(&osb, uap->ub, sizeof (osb)));
2140 * Get file status; this version does not follow links.
2142 #ifndef _SYS_SYSPROTO_H_
2143 struct olstat_args {
2151 register struct olstat_args /* {
2160 error = kern_lstat(td, uap->path, UIO_USERSPACE, &sb);
2164 return (copyout(&osb, uap->ub, sizeof (osb)));
2168 * Convert from an old to a new stat structure.
2176 bzero(ost, sizeof(*ost));
2177 ost->st_dev = st->st_dev;
2178 ost->st_ino = st->st_ino;
2179 ost->st_mode = st->st_mode;
2180 ost->st_nlink = st->st_nlink;
2181 ost->st_uid = st->st_uid;
2182 ost->st_gid = st->st_gid;
2183 ost->st_rdev = st->st_rdev;
2184 if (st->st_size < (quad_t)1 << 32)
2185 ost->st_size = st->st_size;
2188 ost->st_atim = st->st_atim;
2189 ost->st_mtim = st->st_mtim;
2190 ost->st_ctim = st->st_ctim;
2191 ost->st_blksize = st->st_blksize;
2192 ost->st_blocks = st->st_blocks;
2193 ost->st_flags = st->st_flags;
2194 ost->st_gen = st->st_gen;
2196 #endif /* COMPAT_43 */
2199 * Get file status; this version follows links.
2201 #ifndef _SYS_SYSPROTO_H_
2210 register struct stat_args /* {
2218 error = kern_stat(td, uap->path, UIO_USERSPACE, &sb);
2220 error = copyout(&sb, uap->ub, sizeof (sb));
2224 #ifndef _SYS_SYSPROTO_H_
2225 struct fstatat_args {
2233 sys_fstatat(struct thread *td, struct fstatat_args *uap)
2238 error = kern_statat(td, uap->flag, uap->fd, uap->path,
2239 UIO_USERSPACE, &sb);
2241 error = copyout(&sb, uap->buf, sizeof (sb));
2246 kern_stat(struct thread *td, char *path, enum uio_seg pathseg, struct stat *sbp)
2249 return (kern_statat(td, 0, AT_FDCWD, path, pathseg, sbp));
2253 kern_statat(struct thread *td, int flag, int fd, char *path,
2254 enum uio_seg pathseg, struct stat *sbp)
2257 return (kern_statat_vnhook(td, flag, fd, path, pathseg, sbp, NULL));
2261 kern_statat_vnhook(struct thread *td, int flag, int fd, char *path,
2262 enum uio_seg pathseg, struct stat *sbp,
2263 void (*hook)(struct vnode *vp, struct stat *sbp))
2265 struct nameidata nd;
2267 cap_rights_t rights;
2270 if (flag & ~AT_SYMLINK_NOFOLLOW)
2273 NDINIT_ATRIGHTS(&nd, LOOKUP, ((flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW :
2274 FOLLOW) | LOCKSHARED | LOCKLEAF | AUDITVNODE1, pathseg, path, fd,
2275 cap_rights_init(&rights, CAP_FSTAT), td);
2277 if ((error = namei(&nd)) != 0)
2279 error = vn_stat(nd.ni_vp, &sb, td->td_ucred, NOCRED, td);
2281 SDT_PROBE2(vfs, , stat, mode, path, sb.st_mode);
2282 if (S_ISREG(sb.st_mode))
2283 SDT_PROBE2(vfs, , stat, reg, path, pathseg);
2284 if (__predict_false(hook != NULL))
2285 hook(nd.ni_vp, &sb);
2287 NDFREE(&nd, NDF_ONLY_PNBUF);
2293 if (KTRPOINT(td, KTR_STRUCT))
2300 * Get file status; this version does not follow links.
2302 #ifndef _SYS_SYSPROTO_H_
2311 register struct lstat_args /* {
2319 error = kern_lstat(td, uap->path, UIO_USERSPACE, &sb);
2321 error = copyout(&sb, uap->ub, sizeof (sb));
2326 kern_lstat(struct thread *td, char *path, enum uio_seg pathseg, struct stat *sbp)
2329 return (kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, path, pathseg,
2334 * Implementation of the NetBSD [l]stat() functions.
2342 bzero(nsb, sizeof *nsb);
2343 nsb->st_dev = sb->st_dev;
2344 nsb->st_ino = sb->st_ino;
2345 nsb->st_mode = sb->st_mode;
2346 nsb->st_nlink = sb->st_nlink;
2347 nsb->st_uid = sb->st_uid;
2348 nsb->st_gid = sb->st_gid;
2349 nsb->st_rdev = sb->st_rdev;
2350 nsb->st_atim = sb->st_atim;
2351 nsb->st_mtim = sb->st_mtim;
2352 nsb->st_ctim = sb->st_ctim;
2353 nsb->st_size = sb->st_size;
2354 nsb->st_blocks = sb->st_blocks;
2355 nsb->st_blksize = sb->st_blksize;
2356 nsb->st_flags = sb->st_flags;
2357 nsb->st_gen = sb->st_gen;
2358 nsb->st_birthtim = sb->st_birthtim;
2361 #ifndef _SYS_SYSPROTO_H_
2370 register struct nstat_args /* {
2379 error = kern_stat(td, uap->path, UIO_USERSPACE, &sb);
2382 cvtnstat(&sb, &nsb);
2383 return (copyout(&nsb, uap->ub, sizeof (nsb)));
2387 * NetBSD lstat. Get file status; this version does not follow links.
2389 #ifndef _SYS_SYSPROTO_H_
2398 register struct nlstat_args /* {
2407 error = kern_lstat(td, uap->path, UIO_USERSPACE, &sb);
2410 cvtnstat(&sb, &nsb);
2411 return (copyout(&nsb, uap->ub, sizeof (nsb)));
2415 * Get configurable pathname variables.
2417 #ifndef _SYS_SYSPROTO_H_
2418 struct pathconf_args {
2424 sys_pathconf(td, uap)
2426 register struct pathconf_args /* {
2432 return (kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name, FOLLOW));
2435 #ifndef _SYS_SYSPROTO_H_
2436 struct lpathconf_args {
2442 sys_lpathconf(td, uap)
2444 register struct lpathconf_args /* {
2450 return (kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name,
2455 kern_pathconf(struct thread *td, char *path, enum uio_seg pathseg, int name,
2458 struct nameidata nd;
2461 NDINIT(&nd, LOOKUP, LOCKSHARED | LOCKLEAF | AUDITVNODE1 | flags,
2463 if ((error = namei(&nd)) != 0)
2465 NDFREE(&nd, NDF_ONLY_PNBUF);
2467 /* If asynchronous I/O is available, it works for all files. */
2468 if (name == _PC_ASYNC_IO)
2469 td->td_retval[0] = async_io_version;
2471 error = VOP_PATHCONF(nd.ni_vp, name, td->td_retval);
2477 * Return target name of a symbolic link.
2479 #ifndef _SYS_SYSPROTO_H_
2480 struct readlink_args {
2487 sys_readlink(td, uap)
2489 register struct readlink_args /* {
2496 return (kern_readlink(td, uap->path, UIO_USERSPACE, uap->buf,
2497 UIO_USERSPACE, uap->count));
2499 #ifndef _SYS_SYSPROTO_H_
2500 struct readlinkat_args {
2508 sys_readlinkat(struct thread *td, struct readlinkat_args *uap)
2511 return (kern_readlinkat(td, uap->fd, uap->path, UIO_USERSPACE,
2512 uap->buf, UIO_USERSPACE, uap->bufsize));
2516 kern_readlink(struct thread *td, char *path, enum uio_seg pathseg, char *buf,
2517 enum uio_seg bufseg, size_t count)
2520 return (kern_readlinkat(td, AT_FDCWD, path, pathseg, buf, bufseg,
2525 kern_readlinkat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2526 char *buf, enum uio_seg bufseg, size_t count)
2531 struct nameidata nd;
2534 if (count > IOSIZE_MAX)
2537 NDINIT_AT(&nd, LOOKUP, NOFOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
2538 pathseg, path, fd, td);
2540 if ((error = namei(&nd)) != 0)
2542 NDFREE(&nd, NDF_ONLY_PNBUF);
2545 error = mac_vnode_check_readlink(td->td_ucred, vp);
2551 if (vp->v_type != VLNK)
2554 aiov.iov_base = buf;
2555 aiov.iov_len = count;
2556 auio.uio_iov = &aiov;
2557 auio.uio_iovcnt = 1;
2558 auio.uio_offset = 0;
2559 auio.uio_rw = UIO_READ;
2560 auio.uio_segflg = bufseg;
2562 auio.uio_resid = count;
2563 error = VOP_READLINK(vp, &auio, td->td_ucred);
2564 td->td_retval[0] = count - auio.uio_resid;
2571 * Common implementation code for chflags() and fchflags().
2574 setfflags(td, vp, flags)
2583 /* We can't support the value matching VNOVAL. */
2584 if (flags == VNOVAL)
2585 return (EOPNOTSUPP);
2588 * Prevent non-root users from setting flags on devices. When
2589 * a device is reused, users can retain ownership of the device
2590 * if they are allowed to set flags and programs assume that
2591 * chown can't fail when done as root.
2593 if (vp->v_type == VCHR || vp->v_type == VBLK) {
2594 error = priv_check(td, PRIV_VFS_CHFLAGS_DEV);
2599 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2602 vattr.va_flags = flags;
2603 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2605 error = mac_vnode_check_setflags(td->td_ucred, vp, vattr.va_flags);
2608 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
2610 vn_finished_write(mp);
2615 * Change flags of a file given a path name.
2617 #ifndef _SYS_SYSPROTO_H_
2618 struct chflags_args {
2624 sys_chflags(td, uap)
2626 register struct chflags_args /* {
2632 return (kern_chflags(td, uap->path, UIO_USERSPACE, uap->flags));
2635 #ifndef _SYS_SYSPROTO_H_
2636 struct chflagsat_args {
2644 sys_chflagsat(struct thread *td, struct chflagsat_args *uap)
2647 const char *path = uap->path;
2648 u_long flags = uap->flags;
2649 int atflag = uap->atflag;
2651 if (atflag & ~AT_SYMLINK_NOFOLLOW)
2654 return (kern_chflagsat(td, fd, path, UIO_USERSPACE, flags, atflag));
2658 kern_chflags(struct thread *td, const char *path, enum uio_seg pathseg,
2662 return (kern_chflagsat(td, AT_FDCWD, path, pathseg, flags, 0));
2666 * Same as chflags() but doesn't follow symlinks.
2669 sys_lchflags(td, uap)
2671 register struct lchflags_args /* {
2677 return (kern_chflagsat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2678 uap->flags, AT_SYMLINK_NOFOLLOW));
2682 kern_chflagsat(struct thread *td, int fd, const char *path,
2683 enum uio_seg pathseg, u_long flags, int atflag)
2685 struct nameidata nd;
2686 cap_rights_t rights;
2689 AUDIT_ARG_FFLAGS(flags);
2690 follow = (atflag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
2691 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, pathseg, path, fd,
2692 cap_rights_init(&rights, CAP_FCHFLAGS), td);
2693 if ((error = namei(&nd)) != 0)
2695 NDFREE(&nd, NDF_ONLY_PNBUF);
2696 error = setfflags(td, nd.ni_vp, flags);
2702 * Change flags of a file given a file descriptor.
2704 #ifndef _SYS_SYSPROTO_H_
2705 struct fchflags_args {
2711 sys_fchflags(td, uap)
2713 register struct fchflags_args /* {
2719 cap_rights_t rights;
2722 AUDIT_ARG_FD(uap->fd);
2723 AUDIT_ARG_FFLAGS(uap->flags);
2724 error = getvnode(td->td_proc->p_fd, uap->fd,
2725 cap_rights_init(&rights, CAP_FCHFLAGS), &fp);
2729 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
2730 AUDIT_ARG_VNODE1(fp->f_vnode);
2731 VOP_UNLOCK(fp->f_vnode, 0);
2733 error = setfflags(td, fp->f_vnode, uap->flags);
2739 * Common implementation code for chmod(), lchmod() and fchmod().
2742 setfmode(td, cred, vp, mode)
2752 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2754 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2756 vattr.va_mode = mode & ALLPERMS;
2758 error = mac_vnode_check_setmode(cred, vp, vattr.va_mode);
2761 error = VOP_SETATTR(vp, &vattr, cred);
2763 vn_finished_write(mp);
2768 * Change mode of a file given path name.
2770 #ifndef _SYS_SYSPROTO_H_
2779 register struct chmod_args /* {
2785 return (kern_chmod(td, uap->path, UIO_USERSPACE, uap->mode));
2788 #ifndef _SYS_SYSPROTO_H_
2789 struct fchmodat_args {
2797 sys_fchmodat(struct thread *td, struct fchmodat_args *uap)
2799 int flag = uap->flag;
2801 char *path = uap->path;
2802 mode_t mode = uap->mode;
2804 if (flag & ~AT_SYMLINK_NOFOLLOW)
2807 return (kern_fchmodat(td, fd, path, UIO_USERSPACE, mode, flag));
2811 kern_chmod(struct thread *td, char *path, enum uio_seg pathseg, int mode)
2814 return (kern_fchmodat(td, AT_FDCWD, path, pathseg, mode, 0));
2818 * Change mode of a file given path name (don't follow links.)
2820 #ifndef _SYS_SYSPROTO_H_
2821 struct lchmod_args {
2829 register struct lchmod_args /* {
2835 return (kern_fchmodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2836 uap->mode, AT_SYMLINK_NOFOLLOW));
2840 kern_fchmodat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2841 mode_t mode, int flag)
2843 struct nameidata nd;
2844 cap_rights_t rights;
2847 AUDIT_ARG_MODE(mode);
2848 follow = (flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
2849 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, pathseg, path, fd,
2850 cap_rights_init(&rights, CAP_FCHMOD), td);
2851 if ((error = namei(&nd)) != 0)
2853 NDFREE(&nd, NDF_ONLY_PNBUF);
2854 error = setfmode(td, td->td_ucred, nd.ni_vp, mode);
2860 * Change mode of a file given a file descriptor.
2862 #ifndef _SYS_SYSPROTO_H_
2863 struct fchmod_args {
2869 sys_fchmod(struct thread *td, struct fchmod_args *uap)
2872 cap_rights_t rights;
2875 AUDIT_ARG_FD(uap->fd);
2876 AUDIT_ARG_MODE(uap->mode);
2878 error = fget(td, uap->fd, cap_rights_init(&rights, CAP_FCHMOD), &fp);
2881 error = fo_chmod(fp, uap->mode, td->td_ucred, td);
2887 * Common implementation for chown(), lchown(), and fchown()
2890 setfown(td, cred, vp, uid, gid)
2901 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2903 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2908 error = mac_vnode_check_setowner(cred, vp, vattr.va_uid,
2912 error = VOP_SETATTR(vp, &vattr, cred);
2914 vn_finished_write(mp);
2919 * Set ownership given a path name.
2921 #ifndef _SYS_SYSPROTO_H_
2931 register struct chown_args /* {
2938 return (kern_chown(td, uap->path, UIO_USERSPACE, uap->uid, uap->gid));
2941 #ifndef _SYS_SYSPROTO_H_
2942 struct fchownat_args {
2951 sys_fchownat(struct thread *td, struct fchownat_args *uap)
2956 if (flag & ~AT_SYMLINK_NOFOLLOW)
2959 return (kern_fchownat(td, uap->fd, uap->path, UIO_USERSPACE, uap->uid,
2960 uap->gid, uap->flag));
2964 kern_chown(struct thread *td, char *path, enum uio_seg pathseg, int uid,
2968 return (kern_fchownat(td, AT_FDCWD, path, pathseg, uid, gid, 0));
2972 kern_fchownat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2973 int uid, int gid, int flag)
2975 struct nameidata nd;
2976 cap_rights_t rights;
2979 AUDIT_ARG_OWNER(uid, gid);
2980 follow = (flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
2981 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, pathseg, path, fd,
2982 cap_rights_init(&rights, CAP_FCHOWN), td);
2984 if ((error = namei(&nd)) != 0)
2986 NDFREE(&nd, NDF_ONLY_PNBUF);
2987 error = setfown(td, td->td_ucred, nd.ni_vp, uid, gid);
2993 * Set ownership given a path name, do not cross symlinks.
2995 #ifndef _SYS_SYSPROTO_H_
2996 struct lchown_args {
3005 register struct lchown_args /* {
3012 return (kern_lchown(td, uap->path, UIO_USERSPACE, uap->uid, uap->gid));
3016 kern_lchown(struct thread *td, char *path, enum uio_seg pathseg, int uid,
3020 return (kern_fchownat(td, AT_FDCWD, path, pathseg, uid, gid,
3021 AT_SYMLINK_NOFOLLOW));
3025 * Set ownership given a file descriptor.
3027 #ifndef _SYS_SYSPROTO_H_
3028 struct fchown_args {
3037 register struct fchown_args /* {
3044 cap_rights_t rights;
3047 AUDIT_ARG_FD(uap->fd);
3048 AUDIT_ARG_OWNER(uap->uid, uap->gid);
3049 error = fget(td, uap->fd, cap_rights_init(&rights, CAP_FCHOWN), &fp);
3052 error = fo_chown(fp, uap->uid, uap->gid, td->td_ucred, td);
3058 * Common implementation code for utimes(), lutimes(), and futimes().
3061 getutimes(usrtvp, tvpseg, tsp)
3062 const struct timeval *usrtvp;
3063 enum uio_seg tvpseg;
3064 struct timespec *tsp;
3066 struct timeval tv[2];
3067 const struct timeval *tvp;
3070 if (usrtvp == NULL) {
3071 vfs_timestamp(&tsp[0]);
3074 if (tvpseg == UIO_SYSSPACE) {
3077 if ((error = copyin(usrtvp, tv, sizeof(tv))) != 0)
3082 if (tvp[0].tv_usec < 0 || tvp[0].tv_usec >= 1000000 ||
3083 tvp[1].tv_usec < 0 || tvp[1].tv_usec >= 1000000)
3085 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
3086 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
3092 * Common implementation code for futimens(), utimensat().
3094 #define UTIMENS_NULL 0x1
3095 #define UTIMENS_EXIT 0x2
3097 getutimens(const struct timespec *usrtsp, enum uio_seg tspseg,
3098 struct timespec *tsp, int *retflags)
3100 struct timespec tsnow;
3103 vfs_timestamp(&tsnow);
3105 if (usrtsp == NULL) {
3108 *retflags |= UTIMENS_NULL;
3111 if (tspseg == UIO_SYSSPACE) {
3114 } else if ((error = copyin(usrtsp, tsp, sizeof(*tsp) * 2)) != 0)
3116 if (tsp[0].tv_nsec == UTIME_OMIT && tsp[1].tv_nsec == UTIME_OMIT)
3117 *retflags |= UTIMENS_EXIT;
3118 if (tsp[0].tv_nsec == UTIME_NOW && tsp[1].tv_nsec == UTIME_NOW)
3119 *retflags |= UTIMENS_NULL;
3120 if (tsp[0].tv_nsec == UTIME_OMIT)
3121 tsp[0].tv_sec = VNOVAL;
3122 else if (tsp[0].tv_nsec == UTIME_NOW)
3124 else if (tsp[0].tv_nsec < 0 || tsp[0].tv_nsec >= 1000000000L)
3126 if (tsp[1].tv_nsec == UTIME_OMIT)
3127 tsp[1].tv_sec = VNOVAL;
3128 else if (tsp[1].tv_nsec == UTIME_NOW)
3130 else if (tsp[1].tv_nsec < 0 || tsp[1].tv_nsec >= 1000000000L)
3137 * Common implementation code for utimes(), lutimes(), futimes(), futimens(),
3141 setutimes(td, vp, ts, numtimes, nullflag)
3144 const struct timespec *ts;
3150 int error, setbirthtime;
3152 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
3154 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3156 if (numtimes < 3 && !VOP_GETATTR(vp, &vattr, td->td_ucred) &&
3157 timespeccmp(&ts[1], &vattr.va_birthtime, < ))
3160 vattr.va_atime = ts[0];
3161 vattr.va_mtime = ts[1];
3163 vattr.va_birthtime = ts[1];
3165 vattr.va_birthtime = ts[2];
3167 vattr.va_vaflags |= VA_UTIMES_NULL;
3169 error = mac_vnode_check_setutimes(td->td_ucred, vp, vattr.va_atime,
3173 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3175 vn_finished_write(mp);
3180 * Set the access and modification times of a file.
3182 #ifndef _SYS_SYSPROTO_H_
3183 struct utimes_args {
3185 struct timeval *tptr;
3191 register struct utimes_args /* {
3193 struct timeval *tptr;
3197 return (kern_utimes(td, uap->path, UIO_USERSPACE, uap->tptr,
3201 #ifndef _SYS_SYSPROTO_H_
3202 struct futimesat_args {
3205 const struct timeval * times;
3209 sys_futimesat(struct thread *td, struct futimesat_args *uap)
3212 return (kern_utimesat(td, uap->fd, uap->path, UIO_USERSPACE,
3213 uap->times, UIO_USERSPACE));
3217 kern_utimes(struct thread *td, char *path, enum uio_seg pathseg,
3218 struct timeval *tptr, enum uio_seg tptrseg)
3221 return (kern_utimesat(td, AT_FDCWD, path, pathseg, tptr, tptrseg));
3225 kern_utimesat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
3226 struct timeval *tptr, enum uio_seg tptrseg)
3228 struct nameidata nd;
3229 struct timespec ts[2];
3230 cap_rights_t rights;
3233 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3235 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, fd,
3236 cap_rights_init(&rights, CAP_FUTIMES), td);
3238 if ((error = namei(&nd)) != 0)
3240 NDFREE(&nd, NDF_ONLY_PNBUF);
3241 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3247 * Set the access and modification times of a file.
3249 #ifndef _SYS_SYSPROTO_H_
3250 struct lutimes_args {
3252 struct timeval *tptr;
3256 sys_lutimes(td, uap)
3258 register struct lutimes_args /* {
3260 struct timeval *tptr;
3264 return (kern_lutimes(td, uap->path, UIO_USERSPACE, uap->tptr,
3269 kern_lutimes(struct thread *td, char *path, enum uio_seg pathseg,
3270 struct timeval *tptr, enum uio_seg tptrseg)
3272 struct timespec ts[2];
3273 struct nameidata nd;
3276 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3278 NDINIT(&nd, LOOKUP, NOFOLLOW | AUDITVNODE1, pathseg, path, td);
3279 if ((error = namei(&nd)) != 0)
3281 NDFREE(&nd, NDF_ONLY_PNBUF);
3282 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3288 * Set the access and modification times of a file.
3290 #ifndef _SYS_SYSPROTO_H_
3291 struct futimes_args {
3293 struct timeval *tptr;
3297 sys_futimes(td, uap)
3299 register struct futimes_args /* {
3301 struct timeval *tptr;
3305 return (kern_futimes(td, uap->fd, uap->tptr, UIO_USERSPACE));
3309 kern_futimes(struct thread *td, int fd, struct timeval *tptr,
3310 enum uio_seg tptrseg)
3312 struct timespec ts[2];
3314 cap_rights_t rights;
3318 error = getutimes(tptr, tptrseg, ts);
3321 error = getvnode(td->td_proc->p_fd, fd,
3322 cap_rights_init(&rights, CAP_FUTIMES), &fp);
3326 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
3327 AUDIT_ARG_VNODE1(fp->f_vnode);
3328 VOP_UNLOCK(fp->f_vnode, 0);
3330 error = setutimes(td, fp->f_vnode, ts, 2, tptr == NULL);
3336 sys_futimens(struct thread *td, struct futimens_args *uap)
3339 return (kern_futimens(td, uap->fd, uap->times, UIO_USERSPACE));
3343 kern_futimens(struct thread *td, int fd, struct timespec *tptr,
3344 enum uio_seg tptrseg)
3346 struct timespec ts[2];
3348 cap_rights_t rights;
3352 error = getutimens(tptr, tptrseg, ts, &flags);
3355 if (flags & UTIMENS_EXIT)
3357 error = getvnode(td->td_proc->p_fd, fd,
3358 cap_rights_init(&rights, CAP_FUTIMES), &fp);
3362 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
3363 AUDIT_ARG_VNODE1(fp->f_vnode);
3364 VOP_UNLOCK(fp->f_vnode, 0);
3366 error = setutimes(td, fp->f_vnode, ts, 2, flags & UTIMENS_NULL);
3372 sys_utimensat(struct thread *td, struct utimensat_args *uap)
3375 return (kern_utimensat(td, uap->fd, uap->path, UIO_USERSPACE,
3376 uap->times, UIO_USERSPACE, uap->flag));
3380 kern_utimensat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
3381 struct timespec *tptr, enum uio_seg tptrseg, int flag)
3383 struct nameidata nd;
3384 struct timespec ts[2];
3385 cap_rights_t rights;
3388 if (flag & ~AT_SYMLINK_NOFOLLOW)
3391 if ((error = getutimens(tptr, tptrseg, ts, &flags)) != 0)
3393 NDINIT_ATRIGHTS(&nd, LOOKUP, ((flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW :
3394 FOLLOW) | AUDITVNODE1, pathseg, path, fd,
3395 cap_rights_init(&rights, CAP_FUTIMES), td);
3396 if ((error = namei(&nd)) != 0)
3399 * We are allowed to call namei() regardless of 2xUTIME_OMIT.
3401 * "If both tv_nsec fields are UTIME_OMIT... EACCESS may be detected."
3402 * "Search permission is denied by a component of the path prefix."
3404 NDFREE(&nd, NDF_ONLY_PNBUF);
3405 if ((flags & UTIMENS_EXIT) == 0)
3406 error = setutimes(td, nd.ni_vp, ts, 2, flags & UTIMENS_NULL);
3412 * Truncate a file given its path name.
3414 #ifndef _SYS_SYSPROTO_H_
3415 struct truncate_args {
3422 sys_truncate(td, uap)
3424 register struct truncate_args /* {
3431 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3435 kern_truncate(struct thread *td, char *path, enum uio_seg pathseg, off_t length)
3441 struct nameidata nd;
3446 NDINIT(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, td);
3447 if ((error = namei(&nd)) != 0)
3450 rl_cookie = vn_rangelock_wlock(vp, 0, OFF_MAX);
3451 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0) {
3452 vn_rangelock_unlock(vp, rl_cookie);
3456 NDFREE(&nd, NDF_ONLY_PNBUF);
3457 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3458 if (vp->v_type == VDIR)
3461 else if ((error = mac_vnode_check_write(td->td_ucred, NOCRED, vp))) {
3464 else if ((error = vn_writechk(vp)) == 0 &&
3465 (error = VOP_ACCESS(vp, VWRITE, td->td_ucred, td)) == 0) {
3467 vattr.va_size = length;
3468 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3471 vn_finished_write(mp);
3472 vn_rangelock_unlock(vp, rl_cookie);
3477 #if defined(COMPAT_43)
3479 * Truncate a file given its path name.
3481 #ifndef _SYS_SYSPROTO_H_
3482 struct otruncate_args {
3490 register struct otruncate_args /* {
3495 struct truncate_args /* {
3501 nuap.path = uap->path;
3502 nuap.length = uap->length;
3503 return (sys_truncate(td, &nuap));
3505 #endif /* COMPAT_43 */
3507 /* Versions with the pad argument */
3509 freebsd6_truncate(struct thread *td, struct freebsd6_truncate_args *uap)
3511 struct truncate_args ouap;
3513 ouap.path = uap->path;
3514 ouap.length = uap->length;
3515 return (sys_truncate(td, &ouap));
3519 freebsd6_ftruncate(struct thread *td, struct freebsd6_ftruncate_args *uap)
3521 struct ftruncate_args ouap;
3524 ouap.length = uap->length;
3525 return (sys_ftruncate(td, &ouap));
3529 * Sync an open file.
3531 #ifndef _SYS_SYSPROTO_H_
3539 struct fsync_args /* {
3546 cap_rights_t rights;
3547 int error, lock_flags;
3549 AUDIT_ARG_FD(uap->fd);
3550 error = getvnode(td->td_proc->p_fd, uap->fd,
3551 cap_rights_init(&rights, CAP_FSYNC), &fp);
3555 error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
3558 if (MNT_SHARED_WRITES(mp) ||
3559 ((mp == NULL) && MNT_SHARED_WRITES(vp->v_mount))) {
3560 lock_flags = LK_SHARED;
3562 lock_flags = LK_EXCLUSIVE;
3564 vn_lock(vp, lock_flags | LK_RETRY);
3565 AUDIT_ARG_VNODE1(vp);
3566 if (vp->v_object != NULL) {
3567 VM_OBJECT_WLOCK(vp->v_object);
3568 vm_object_page_clean(vp->v_object, 0, 0, 0);
3569 VM_OBJECT_WUNLOCK(vp->v_object);
3571 error = VOP_FSYNC(vp, MNT_WAIT, td);
3574 vn_finished_write(mp);
3581 * Rename files. Source and destination must either both be directories, or
3582 * both not be directories. If target is a directory, it must be empty.
3584 #ifndef _SYS_SYSPROTO_H_
3585 struct rename_args {
3593 register struct rename_args /* {
3599 return (kern_rename(td, uap->from, uap->to, UIO_USERSPACE));
3602 #ifndef _SYS_SYSPROTO_H_
3603 struct renameat_args {
3611 sys_renameat(struct thread *td, struct renameat_args *uap)
3614 return (kern_renameat(td, uap->oldfd, uap->old, uap->newfd, uap->new,
3619 kern_rename(struct thread *td, char *from, char *to, enum uio_seg pathseg)
3622 return (kern_renameat(td, AT_FDCWD, from, AT_FDCWD, to, pathseg));
3626 kern_renameat(struct thread *td, int oldfd, char *old, int newfd, char *new,
3627 enum uio_seg pathseg)
3629 struct mount *mp = NULL;
3630 struct vnode *tvp, *fvp, *tdvp;
3631 struct nameidata fromnd, tond;
3632 cap_rights_t rights;
3638 NDINIT_ATRIGHTS(&fromnd, DELETE, LOCKPARENT | LOCKLEAF | SAVESTART |
3639 AUDITVNODE1, pathseg, old, oldfd,
3640 cap_rights_init(&rights, CAP_RENAMEAT), td);
3642 NDINIT_ATRIGHTS(&fromnd, DELETE, WANTPARENT | SAVESTART | AUDITVNODE1,
3643 pathseg, old, oldfd, cap_rights_init(&rights, CAP_RENAMEAT), td);
3646 if ((error = namei(&fromnd)) != 0)
3649 error = mac_vnode_check_rename_from(td->td_ucred, fromnd.ni_dvp,
3650 fromnd.ni_vp, &fromnd.ni_cnd);
3651 VOP_UNLOCK(fromnd.ni_dvp, 0);
3652 if (fromnd.ni_dvp != fromnd.ni_vp)
3653 VOP_UNLOCK(fromnd.ni_vp, 0);
3656 NDINIT_ATRIGHTS(&tond, RENAME, LOCKPARENT | LOCKLEAF | NOCACHE |
3657 SAVESTART | AUDITVNODE2, pathseg, new, newfd,
3658 cap_rights_init(&rights, CAP_LINKAT), td);
3659 if (fromnd.ni_vp->v_type == VDIR)
3660 tond.ni_cnd.cn_flags |= WILLBEDIR;
3661 if ((error = namei(&tond)) != 0) {
3662 /* Translate error code for rename("dir1", "dir2/."). */
3663 if (error == EISDIR && fvp->v_type == VDIR)
3665 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3666 vrele(fromnd.ni_dvp);
3672 error = vn_start_write(fvp, &mp, V_NOWAIT);
3674 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3675 NDFREE(&tond, NDF_ONLY_PNBUF);
3682 vrele(fromnd.ni_dvp);
3684 vrele(tond.ni_startdir);
3685 if (fromnd.ni_startdir != NULL)
3686 vrele(fromnd.ni_startdir);
3687 error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH);
3693 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
3696 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
3701 if (newfd != AT_FDCWD) {
3703 * If the target already exists we require CAP_UNLINKAT
3706 error = cap_check(&tond.ni_filecaps.fc_rights,
3707 cap_rights_init(&rights, CAP_UNLINKAT));
3718 * If the source is the same as the destination (that is, if they
3719 * are links to the same vnode), then there is nothing to do.
3725 error = mac_vnode_check_rename_to(td->td_ucred, tdvp,
3726 tond.ni_vp, fromnd.ni_dvp == tdvp, &tond.ni_cnd);
3730 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
3731 tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
3732 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3733 NDFREE(&tond, NDF_ONLY_PNBUF);
3735 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3736 NDFREE(&tond, NDF_ONLY_PNBUF);
3743 vrele(fromnd.ni_dvp);
3746 vrele(tond.ni_startdir);
3747 vn_finished_write(mp);
3749 if (fromnd.ni_startdir)
3750 vrele(fromnd.ni_startdir);
3757 * Make a directory file.
3759 #ifndef _SYS_SYSPROTO_H_
3768 register struct mkdir_args /* {
3774 return (kern_mkdir(td, uap->path, UIO_USERSPACE, uap->mode));
3777 #ifndef _SYS_SYSPROTO_H_
3778 struct mkdirat_args {
3785 sys_mkdirat(struct thread *td, struct mkdirat_args *uap)
3788 return (kern_mkdirat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode));
3792 kern_mkdir(struct thread *td, char *path, enum uio_seg segflg, int mode)
3795 return (kern_mkdirat(td, AT_FDCWD, path, segflg, mode));
3799 kern_mkdirat(struct thread *td, int fd, char *path, enum uio_seg segflg,
3805 struct nameidata nd;
3806 cap_rights_t rights;
3809 AUDIT_ARG_MODE(mode);
3812 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
3813 NOCACHE, segflg, path, fd, cap_rights_init(&rights, CAP_MKDIRAT),
3815 nd.ni_cnd.cn_flags |= WILLBEDIR;
3816 if ((error = namei(&nd)) != 0)
3820 NDFREE(&nd, NDF_ONLY_PNBUF);
3822 * XXX namei called with LOCKPARENT but not LOCKLEAF has
3823 * the strange behaviour of leaving the vnode unlocked
3824 * if the target is the same vnode as the parent.
3826 if (vp == nd.ni_dvp)
3833 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3834 NDFREE(&nd, NDF_ONLY_PNBUF);
3836 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3841 vattr.va_type = VDIR;
3842 vattr.va_mode = (mode & ACCESSPERMS) &~ td->td_proc->p_fd->fd_cmask;
3844 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
3849 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
3853 NDFREE(&nd, NDF_ONLY_PNBUF);
3857 vn_finished_write(mp);
3862 * Remove a directory file.
3864 #ifndef _SYS_SYSPROTO_H_
3872 struct rmdir_args /* {
3877 return (kern_rmdir(td, uap->path, UIO_USERSPACE));
3881 kern_rmdir(struct thread *td, char *path, enum uio_seg pathseg)
3884 return (kern_rmdirat(td, AT_FDCWD, path, pathseg));
3888 kern_rmdirat(struct thread *td, int fd, char *path, enum uio_seg pathseg)
3892 struct nameidata nd;
3893 cap_rights_t rights;
3898 NDINIT_ATRIGHTS(&nd, DELETE, LOCKPARENT | LOCKLEAF | AUDITVNODE1,
3899 pathseg, path, fd, cap_rights_init(&rights, CAP_UNLINKAT), td);
3900 if ((error = namei(&nd)) != 0)
3903 if (vp->v_type != VDIR) {
3908 * No rmdir "." please.
3910 if (nd.ni_dvp == vp) {
3915 * The root of a mounted filesystem cannot be deleted.
3917 if (vp->v_vflag & VV_ROOT) {
3922 error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp,
3927 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3928 NDFREE(&nd, NDF_ONLY_PNBUF);
3930 if (nd.ni_dvp == vp)
3934 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3938 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_UNLINK);
3939 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
3940 vn_finished_write(mp);
3942 NDFREE(&nd, NDF_ONLY_PNBUF);
3944 if (nd.ni_dvp == vp)
3953 * Read a block of directory entries in a filesystem independent format.
3955 #ifndef _SYS_SYSPROTO_H_
3956 struct ogetdirentries_args {
3964 ogetdirentries(struct thread *td, struct ogetdirentries_args *uap)
3969 error = kern_ogetdirentries(td, uap, &loff);
3971 error = copyout(&loff, uap->basep, sizeof(long));
3976 kern_ogetdirentries(struct thread *td, struct ogetdirentries_args *uap,
3981 struct uio auio, kuio;
3982 struct iovec aiov, kiov;
3983 struct dirent *dp, *edp;
3984 cap_rights_t rights;
3986 int error, eofflag, readcnt;
3990 /* XXX arbitrary sanity limit on `count'. */
3991 if (uap->count > 64 * 1024)
3993 error = getvnode(td->td_proc->p_fd, uap->fd,
3994 cap_rights_init(&rights, CAP_READ), &fp);
3997 if ((fp->f_flag & FREAD) == 0) {
4002 foffset = foffset_lock(fp, 0);
4004 if (vp->v_type != VDIR) {
4005 foffset_unlock(fp, foffset, 0);
4009 aiov.iov_base = uap->buf;
4010 aiov.iov_len = uap->count;
4011 auio.uio_iov = &aiov;
4012 auio.uio_iovcnt = 1;
4013 auio.uio_rw = UIO_READ;
4014 auio.uio_segflg = UIO_USERSPACE;
4016 auio.uio_resid = uap->count;
4017 vn_lock(vp, LK_SHARED | LK_RETRY);
4018 loff = auio.uio_offset = foffset;
4020 error = mac_vnode_check_readdir(td->td_ucred, vp);
4023 foffset_unlock(fp, foffset, FOF_NOUPDATE);
4028 # if (BYTE_ORDER != LITTLE_ENDIAN)
4029 if (vp->v_mount->mnt_maxsymlinklen <= 0) {
4030 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag,
4032 foffset = auio.uio_offset;
4037 kuio.uio_iov = &kiov;
4038 kuio.uio_segflg = UIO_SYSSPACE;
4039 kiov.iov_len = uap->count;
4040 dirbuf = malloc(uap->count, M_TEMP, M_WAITOK);
4041 kiov.iov_base = dirbuf;
4042 error = VOP_READDIR(vp, &kuio, fp->f_cred, &eofflag,
4044 foffset = kuio.uio_offset;
4046 readcnt = uap->count - kuio.uio_resid;
4047 edp = (struct dirent *)&dirbuf[readcnt];
4048 for (dp = (struct dirent *)dirbuf; dp < edp; ) {
4049 # if (BYTE_ORDER == LITTLE_ENDIAN)
4051 * The expected low byte of
4052 * dp->d_namlen is our dp->d_type.
4053 * The high MBZ byte of dp->d_namlen
4054 * is our dp->d_namlen.
4056 dp->d_type = dp->d_namlen;
4060 * The dp->d_type is the high byte
4061 * of the expected dp->d_namlen,
4062 * so must be zero'ed.
4066 if (dp->d_reclen > 0) {
4067 dp = (struct dirent *)
4068 ((char *)dp + dp->d_reclen);
4075 error = uiomove(dirbuf, readcnt, &auio);
4077 free(dirbuf, M_TEMP);
4081 foffset_unlock(fp, foffset, 0);
4085 if (uap->count == auio.uio_resid &&
4086 (vp->v_vflag & VV_ROOT) &&
4087 (vp->v_mount->mnt_flag & MNT_UNION)) {
4088 struct vnode *tvp = vp;
4089 vp = vp->v_mount->mnt_vnodecovered;
4098 foffset_unlock(fp, foffset, 0);
4100 td->td_retval[0] = uap->count - auio.uio_resid;
4105 #endif /* COMPAT_43 */
4108 * Read a block of directory entries in a filesystem independent format.
4110 #ifndef _SYS_SYSPROTO_H_
4111 struct getdirentries_args {
4119 sys_getdirentries(td, uap)
4121 register struct getdirentries_args /* {
4131 error = kern_getdirentries(td, uap->fd, uap->buf, uap->count, &base,
4132 NULL, UIO_USERSPACE);
4135 if (uap->basep != NULL)
4136 error = copyout(&base, uap->basep, sizeof(long));
4141 kern_getdirentries(struct thread *td, int fd, char *buf, u_int count,
4142 long *basep, ssize_t *residp, enum uio_seg bufseg)
4148 cap_rights_t rights;
4154 if (count > IOSIZE_MAX)
4156 auio.uio_resid = count;
4157 error = getvnode(td->td_proc->p_fd, fd,
4158 cap_rights_init(&rights, CAP_READ), &fp);
4161 if ((fp->f_flag & FREAD) == 0) {
4166 foffset = foffset_lock(fp, 0);
4168 if (vp->v_type != VDIR) {
4172 aiov.iov_base = buf;
4173 aiov.iov_len = count;
4174 auio.uio_iov = &aiov;
4175 auio.uio_iovcnt = 1;
4176 auio.uio_rw = UIO_READ;
4177 auio.uio_segflg = bufseg;
4179 vn_lock(vp, LK_SHARED | LK_RETRY);
4180 AUDIT_ARG_VNODE1(vp);
4181 loff = auio.uio_offset = foffset;
4183 error = mac_vnode_check_readdir(td->td_ucred, vp);
4186 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL,
4188 foffset = auio.uio_offset;
4193 if (count == auio.uio_resid &&
4194 (vp->v_vflag & VV_ROOT) &&
4195 (vp->v_mount->mnt_flag & MNT_UNION)) {
4196 struct vnode *tvp = vp;
4198 vp = vp->v_mount->mnt_vnodecovered;
4209 *residp = auio.uio_resid;
4210 td->td_retval[0] = count - auio.uio_resid;
4212 foffset_unlock(fp, foffset, 0);
4217 #ifndef _SYS_SYSPROTO_H_
4218 struct getdents_args {
4225 sys_getdents(td, uap)
4227 register struct getdents_args /* {
4233 struct getdirentries_args ap;
4237 ap.count = uap->count;
4239 return (sys_getdirentries(td, &ap));
4243 * Set the mode mask for creation of filesystem nodes.
4245 #ifndef _SYS_SYSPROTO_H_
4253 struct umask_args /* {
4257 register struct filedesc *fdp;
4259 FILEDESC_XLOCK(td->td_proc->p_fd);
4260 fdp = td->td_proc->p_fd;
4261 td->td_retval[0] = fdp->fd_cmask;
4262 fdp->fd_cmask = uap->newmask & ALLPERMS;
4263 FILEDESC_XUNLOCK(td->td_proc->p_fd);
4268 * Void all references to file by ripping underlying filesystem away from
4271 #ifndef _SYS_SYSPROTO_H_
4272 struct revoke_args {
4279 register struct revoke_args /* {
4285 struct nameidata nd;
4288 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
4290 if ((error = namei(&nd)) != 0)
4293 NDFREE(&nd, NDF_ONLY_PNBUF);
4294 if (vp->v_type != VCHR || vp->v_rdev == NULL) {
4299 error = mac_vnode_check_revoke(td->td_ucred, vp);
4303 error = VOP_GETATTR(vp, &vattr, td->td_ucred);
4306 if (td->td_ucred->cr_uid != vattr.va_uid) {
4307 error = priv_check(td, PRIV_VFS_ADMIN);
4312 VOP_REVOKE(vp, REVOKEALL);
4319 * Convert a user file descriptor to a kernel file entry and check that, if it
4320 * is a capability, the correct rights are present. A reference on the file
4321 * entry is held upon returning.
4324 getvnode(struct filedesc *fdp, int fd, cap_rights_t *rightsp, struct file **fpp)
4329 error = fget_unlocked(fdp, fd, rightsp, 0, &fp, NULL);
4334 * The file could be not of the vnode type, or it may be not
4335 * yet fully initialized, in which case the f_vnode pointer
4336 * may be set, but f_ops is still badfileops. E.g.,
4337 * devfs_open() transiently create such situation to
4338 * facilitate csw d_fdopen().
4340 * Dupfdopen() handling in kern_openat() installs the
4341 * half-baked file into the process descriptor table, allowing
4342 * other thread to dereference it. Guard against the race by
4345 if (fp->f_vnode == NULL || fp->f_ops == &badfileops) {
4346 fdrop(fp, curthread);
4355 * Get an (NFS) file handle.
4357 #ifndef _SYS_SYSPROTO_H_
4358 struct lgetfh_args {
4366 register struct lgetfh_args *uap;
4368 struct nameidata nd;
4370 register struct vnode *vp;
4373 error = priv_check(td, PRIV_VFS_GETFH);
4376 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
4381 NDFREE(&nd, NDF_ONLY_PNBUF);
4383 bzero(&fh, sizeof(fh));
4384 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
4385 error = VOP_VPTOFH(vp, &fh.fh_fid);
4388 error = copyout(&fh, uap->fhp, sizeof (fh));
4392 #ifndef _SYS_SYSPROTO_H_
4401 register struct getfh_args *uap;
4403 struct nameidata nd;
4405 register struct vnode *vp;
4408 error = priv_check(td, PRIV_VFS_GETFH);
4411 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
4416 NDFREE(&nd, NDF_ONLY_PNBUF);
4418 bzero(&fh, sizeof(fh));
4419 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
4420 error = VOP_VPTOFH(vp, &fh.fh_fid);
4423 error = copyout(&fh, uap->fhp, sizeof (fh));
4428 * syscall for the rpc.lockd to use to translate a NFS file handle into an
4431 * warning: do not remove the priv_check() call or this becomes one giant
4434 #ifndef _SYS_SYSPROTO_H_
4435 struct fhopen_args {
4436 const struct fhandle *u_fhp;
4443 struct fhopen_args /* {
4444 const struct fhandle *u_fhp;
4455 error = priv_check(td, PRIV_VFS_FHOPEN);
4459 fmode = FFLAGS(uap->flags);
4460 /* why not allow a non-read/write open for our lockd? */
4461 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
4463 error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
4466 /* find the mount point */
4467 mp = vfs_busyfs(&fhp.fh_fsid);
4470 /* now give me my vnode, it gets returned to me locked */
4471 error = VFS_FHTOVP(mp, &fhp.fh_fid, LK_EXCLUSIVE, &vp);
4476 error = falloc_noinstall(td, &fp);
4482 * An extra reference on `fp' has been held for us by
4483 * falloc_noinstall().
4489 error = vn_open_vnode(vp, fmode, td->td_ucred, td, fp);
4491 KASSERT(fp->f_ops == &badfileops,
4492 ("VOP_OPEN in fhopen() set f_ops"));
4493 KASSERT(td->td_dupfd < 0,
4494 ("fhopen() encountered fdopen()"));
4504 finit(fp, (fmode & FMASK) | (fp->f_flag & FHASLOCK), DTYPE_VNODE, vp,
4507 if ((fmode & O_TRUNC) != 0) {
4508 error = fo_truncate(fp, 0, td->td_ucred, td);
4513 error = finstall(td, fp, &indx, fmode, NULL);
4516 td->td_retval[0] = indx;
4521 * Stat an (NFS) file handle.
4523 #ifndef _SYS_SYSPROTO_H_
4524 struct fhstat_args {
4525 struct fhandle *u_fhp;
4532 register struct fhstat_args /* {
4533 struct fhandle *u_fhp;
4541 error = copyin(uap->u_fhp, &fh, sizeof(fh));
4544 error = kern_fhstat(td, fh, &sb);
4546 error = copyout(&sb, uap->sb, sizeof(sb));
4551 kern_fhstat(struct thread *td, struct fhandle fh, struct stat *sb)
4557 error = priv_check(td, PRIV_VFS_FHSTAT);
4560 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4562 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_EXCLUSIVE, &vp);
4566 error = vn_stat(vp, sb, td->td_ucred, NOCRED, td);
4572 * Implement fstatfs() for (NFS) file handles.
4574 #ifndef _SYS_SYSPROTO_H_
4575 struct fhstatfs_args {
4576 struct fhandle *u_fhp;
4581 sys_fhstatfs(td, uap)
4583 struct fhstatfs_args /* {
4584 struct fhandle *u_fhp;
4592 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4595 error = kern_fhstatfs(td, fh, &sf);
4598 return (copyout(&sf, uap->buf, sizeof(sf)));
4602 kern_fhstatfs(struct thread *td, fhandle_t fh, struct statfs *buf)
4609 error = priv_check(td, PRIV_VFS_FHSTATFS);
4612 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4614 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_EXCLUSIVE, &vp);
4620 error = prison_canseemount(td->td_ucred, mp);
4624 error = mac_mount_check_stat(td->td_ucred, mp);
4629 * Set these in case the underlying filesystem fails to do so.
4632 sp->f_version = STATFS_VERSION;
4633 sp->f_namemax = NAME_MAX;
4634 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
4635 error = VFS_STATFS(mp, sp);
4644 kern_posix_fallocate(struct thread *td, int fd, off_t offset, off_t len)
4649 cap_rights_t rights;
4650 off_t olen, ooffset;
4653 if (offset < 0 || len <= 0)
4655 /* Check for wrap. */
4656 if (offset > OFF_MAX - len)
4658 error = fget(td, fd, cap_rights_init(&rights, CAP_WRITE), &fp);
4661 if ((fp->f_ops->fo_flags & DFLAG_SEEKABLE) == 0) {
4665 if ((fp->f_flag & FWRITE) == 0) {
4669 if (fp->f_type != DTYPE_VNODE) {
4674 if (vp->v_type != VREG) {
4679 /* Allocating blocks may take a long time, so iterate. */
4686 error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
4689 error = vn_lock(vp, LK_EXCLUSIVE);
4691 vn_finished_write(mp);
4695 error = mac_vnode_check_write(td->td_ucred, fp->f_cred, vp);
4698 error = VOP_ALLOCATE(vp, &offset, &len);
4700 vn_finished_write(mp);
4702 if (olen + ooffset != offset + len) {
4703 panic("offset + len changed from %jx/%jx to %jx/%jx",
4704 ooffset, olen, offset, len);
4706 if (error != 0 || len == 0)
4708 KASSERT(olen > len, ("Iteration did not make progress?"));
4717 sys_posix_fallocate(struct thread *td, struct posix_fallocate_args *uap)
4720 td->td_retval[0] = kern_posix_fallocate(td, uap->fd, uap->offset,
4726 * Unlike madvise(2), we do not make a best effort to remember every
4727 * possible caching hint. Instead, we remember the last setting with
4728 * the exception that we will allow POSIX_FADV_NORMAL to adjust the
4729 * region of any current setting.
4732 kern_posix_fadvise(struct thread *td, int fd, off_t offset, off_t len,
4735 struct fadvise_info *fa, *new;
4738 cap_rights_t rights;
4742 if (offset < 0 || len < 0 || offset > OFF_MAX - len)
4745 case POSIX_FADV_SEQUENTIAL:
4746 case POSIX_FADV_RANDOM:
4747 case POSIX_FADV_NOREUSE:
4748 new = malloc(sizeof(*fa), M_FADVISE, M_WAITOK);
4750 case POSIX_FADV_NORMAL:
4751 case POSIX_FADV_WILLNEED:
4752 case POSIX_FADV_DONTNEED:
4758 /* XXX: CAP_POSIX_FADVISE? */
4759 error = fget(td, fd, cap_rights_init(&rights), &fp);
4762 if ((fp->f_ops->fo_flags & DFLAG_SEEKABLE) == 0) {
4766 if (fp->f_type != DTYPE_VNODE) {
4771 if (vp->v_type != VREG) {
4778 end = offset + len - 1;
4780 case POSIX_FADV_SEQUENTIAL:
4781 case POSIX_FADV_RANDOM:
4782 case POSIX_FADV_NOREUSE:
4784 * Try to merge any existing non-standard region with
4785 * this new region if possible, otherwise create a new
4786 * non-standard region for this request.
4788 mtx_pool_lock(mtxpool_sleep, fp);
4790 if (fa != NULL && fa->fa_advice == advice &&
4791 ((fa->fa_start <= end && fa->fa_end >= offset) ||
4792 (end != OFF_MAX && fa->fa_start == end + 1) ||
4793 (fa->fa_end != OFF_MAX && fa->fa_end + 1 == offset))) {
4794 if (offset < fa->fa_start)
4795 fa->fa_start = offset;
4796 if (end > fa->fa_end)
4799 new->fa_advice = advice;
4800 new->fa_start = offset;
4802 new->fa_prevstart = 0;
4803 new->fa_prevend = 0;
4807 mtx_pool_unlock(mtxpool_sleep, fp);
4809 case POSIX_FADV_NORMAL:
4811 * If a the "normal" region overlaps with an existing
4812 * non-standard region, trim or remove the
4813 * non-standard region.
4815 mtx_pool_lock(mtxpool_sleep, fp);
4818 if (offset <= fa->fa_start && end >= fa->fa_end) {
4820 fp->f_advice = NULL;
4821 } else if (offset <= fa->fa_start &&
4822 end >= fa->fa_start)
4823 fa->fa_start = end + 1;
4824 else if (offset <= fa->fa_end && end >= fa->fa_end)
4825 fa->fa_end = offset - 1;
4826 else if (offset >= fa->fa_start && end <= fa->fa_end) {
4828 * If the "normal" region is a middle
4829 * portion of the existing
4830 * non-standard region, just remove
4831 * the whole thing rather than picking
4832 * one side or the other to
4836 fp->f_advice = NULL;
4839 mtx_pool_unlock(mtxpool_sleep, fp);
4841 case POSIX_FADV_WILLNEED:
4842 case POSIX_FADV_DONTNEED:
4843 error = VOP_ADVISE(vp, offset, end, advice);
4849 free(new, M_FADVISE);
4854 sys_posix_fadvise(struct thread *td, struct posix_fadvise_args *uap)
4857 td->td_retval[0] = kern_posix_fadvise(td, uap->fd, uap->offset,
4858 uap->len, uap->advice);