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_WHT);
1254 return (kern_mkfifoat(td, fd, path, pathseg, mode));
1264 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
1265 NOCACHE, pathseg, path, fd, cap_rights_init(&rights, CAP_MKNODAT),
1267 if ((error = namei(&nd)) != 0)
1271 NDFREE(&nd, NDF_ONLY_PNBUF);
1272 if (vp == nd.ni_dvp)
1280 vattr.va_mode = (mode & ALLPERMS) &
1281 ~td->td_proc->p_fd->fd_cmask;
1282 vattr.va_rdev = dev;
1285 switch (mode & S_IFMT) {
1287 vattr.va_type = VCHR;
1290 vattr.va_type = VBLK;
1296 panic("kern_mknod: invalid mode");
1299 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1300 NDFREE(&nd, NDF_ONLY_PNBUF);
1302 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1307 if (error == 0 && !whiteout)
1308 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp,
1309 &nd.ni_cnd, &vattr);
1313 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, CREATE);
1315 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp,
1316 &nd.ni_cnd, &vattr);
1321 NDFREE(&nd, NDF_ONLY_PNBUF);
1323 vn_finished_write(mp);
1328 * Create a named pipe.
1330 #ifndef _SYS_SYSPROTO_H_
1331 struct mkfifo_args {
1339 register struct mkfifo_args /* {
1345 return (kern_mkfifo(td, uap->path, UIO_USERSPACE, uap->mode));
1348 #ifndef _SYS_SYSPROTO_H_
1349 struct mkfifoat_args {
1356 sys_mkfifoat(struct thread *td, struct mkfifoat_args *uap)
1359 return (kern_mkfifoat(td, uap->fd, uap->path, UIO_USERSPACE,
1364 kern_mkfifo(struct thread *td, char *path, enum uio_seg pathseg, int mode)
1367 return (kern_mkfifoat(td, AT_FDCWD, path, pathseg, mode));
1371 kern_mkfifoat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1376 struct nameidata nd;
1377 cap_rights_t rights;
1380 AUDIT_ARG_MODE(mode);
1383 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
1384 NOCACHE, pathseg, path, fd, cap_rights_init(&rights, CAP_MKFIFOAT),
1386 if ((error = namei(&nd)) != 0)
1388 if (nd.ni_vp != NULL) {
1389 NDFREE(&nd, NDF_ONLY_PNBUF);
1390 if (nd.ni_vp == nd.ni_dvp)
1397 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1398 NDFREE(&nd, NDF_ONLY_PNBUF);
1400 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1405 vattr.va_type = VFIFO;
1406 vattr.va_mode = (mode & ALLPERMS) & ~td->td_proc->p_fd->fd_cmask;
1408 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1413 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
1420 vn_finished_write(mp);
1421 NDFREE(&nd, NDF_ONLY_PNBUF);
1426 * Make a hard file link.
1428 #ifndef _SYS_SYSPROTO_H_
1437 register struct link_args /* {
1443 return (kern_link(td, uap->path, uap->link, UIO_USERSPACE));
1446 #ifndef _SYS_SYSPROTO_H_
1447 struct linkat_args {
1456 sys_linkat(struct thread *td, struct linkat_args *uap)
1461 if (flag & ~AT_SYMLINK_FOLLOW)
1464 return (kern_linkat(td, uap->fd1, uap->fd2, uap->path1, uap->path2,
1465 UIO_USERSPACE, (flag & AT_SYMLINK_FOLLOW) ? FOLLOW : NOFOLLOW));
1468 int hardlink_check_uid = 0;
1469 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
1470 &hardlink_check_uid, 0,
1471 "Unprivileged processes cannot create hard links to files owned by other "
1473 static int hardlink_check_gid = 0;
1474 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
1475 &hardlink_check_gid, 0,
1476 "Unprivileged processes cannot create hard links to files owned by other "
1480 can_hardlink(struct vnode *vp, struct ucred *cred)
1485 if (!hardlink_check_uid && !hardlink_check_gid)
1488 error = VOP_GETATTR(vp, &va, cred);
1492 if (hardlink_check_uid && cred->cr_uid != va.va_uid) {
1493 error = priv_check_cred(cred, PRIV_VFS_LINK, 0);
1498 if (hardlink_check_gid && !groupmember(va.va_gid, cred)) {
1499 error = priv_check_cred(cred, PRIV_VFS_LINK, 0);
1508 kern_link(struct thread *td, char *path, char *link, enum uio_seg segflg)
1511 return (kern_linkat(td, AT_FDCWD, AT_FDCWD, path,link, segflg, FOLLOW));
1515 kern_linkat(struct thread *td, int fd1, int fd2, char *path1, char *path2,
1516 enum uio_seg segflg, int follow)
1520 struct nameidata nd;
1521 cap_rights_t rights;
1526 NDINIT_AT(&nd, LOOKUP, follow | AUDITVNODE1, segflg, path1, fd1, td);
1528 if ((error = namei(&nd)) != 0)
1530 NDFREE(&nd, NDF_ONLY_PNBUF);
1532 if (vp->v_type == VDIR) {
1534 return (EPERM); /* POSIX */
1536 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE2 |
1537 NOCACHE, segflg, path2, fd2, cap_rights_init(&rights, CAP_LINKAT),
1539 if ((error = namei(&nd)) == 0) {
1540 if (nd.ni_vp != NULL) {
1541 NDFREE(&nd, NDF_ONLY_PNBUF);
1542 if (nd.ni_dvp == nd.ni_vp)
1549 } else if (nd.ni_dvp->v_mount != vp->v_mount) {
1551 * Cross-device link. No need to recheck
1552 * vp->v_type, since it cannot change, except
1555 NDFREE(&nd, NDF_ONLY_PNBUF);
1559 } else if ((error = vn_lock(vp, LK_EXCLUSIVE)) == 0) {
1560 error = can_hardlink(vp, td->td_ucred);
1563 error = mac_vnode_check_link(td->td_ucred,
1564 nd.ni_dvp, vp, &nd.ni_cnd);
1569 NDFREE(&nd, NDF_ONLY_PNBUF);
1572 error = vn_start_write(vp, &mp, V_NOWAIT);
1576 NDFREE(&nd, NDF_ONLY_PNBUF);
1577 error = vn_start_write(NULL, &mp,
1583 error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
1586 vn_finished_write(mp);
1587 NDFREE(&nd, NDF_ONLY_PNBUF);
1590 NDFREE(&nd, NDF_ONLY_PNBUF);
1600 * Make a symbolic link.
1602 #ifndef _SYS_SYSPROTO_H_
1603 struct symlink_args {
1609 sys_symlink(td, uap)
1611 register struct symlink_args /* {
1617 return (kern_symlink(td, uap->path, uap->link, UIO_USERSPACE));
1620 #ifndef _SYS_SYSPROTO_H_
1621 struct symlinkat_args {
1628 sys_symlinkat(struct thread *td, struct symlinkat_args *uap)
1631 return (kern_symlinkat(td, uap->path1, uap->fd, uap->path2,
1636 kern_symlink(struct thread *td, char *path, char *link, enum uio_seg segflg)
1639 return (kern_symlinkat(td, path, AT_FDCWD, link, segflg));
1643 kern_symlinkat(struct thread *td, char *path1, int fd, char *path2,
1644 enum uio_seg segflg)
1649 struct nameidata nd;
1651 cap_rights_t rights;
1653 if (segflg == UIO_SYSSPACE) {
1656 syspath = uma_zalloc(namei_zone, M_WAITOK);
1657 if ((error = copyinstr(path1, syspath, MAXPATHLEN, NULL)) != 0)
1660 AUDIT_ARG_TEXT(syspath);
1663 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
1664 NOCACHE, segflg, path2, fd, cap_rights_init(&rights, CAP_SYMLINKAT),
1666 if ((error = namei(&nd)) != 0)
1669 NDFREE(&nd, NDF_ONLY_PNBUF);
1670 if (nd.ni_vp == nd.ni_dvp)
1678 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1679 NDFREE(&nd, NDF_ONLY_PNBUF);
1681 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1686 vattr.va_mode = ACCESSPERMS &~ td->td_proc->p_fd->fd_cmask;
1688 vattr.va_type = VLNK;
1689 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1694 error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr, syspath);
1700 NDFREE(&nd, NDF_ONLY_PNBUF);
1702 vn_finished_write(mp);
1704 if (segflg != UIO_SYSSPACE)
1705 uma_zfree(namei_zone, syspath);
1710 * Delete a whiteout from the filesystem.
1713 sys_undelete(td, uap)
1715 register struct undelete_args /* {
1720 struct nameidata nd;
1725 NDINIT(&nd, DELETE, LOCKPARENT | DOWHITEOUT | AUDITVNODE1,
1726 UIO_USERSPACE, uap->path, td);
1731 if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & ISWHITEOUT)) {
1732 NDFREE(&nd, NDF_ONLY_PNBUF);
1733 if (nd.ni_vp == nd.ni_dvp)
1741 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1742 NDFREE(&nd, NDF_ONLY_PNBUF);
1744 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1748 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, DELETE);
1749 NDFREE(&nd, NDF_ONLY_PNBUF);
1751 vn_finished_write(mp);
1756 * Delete a name from the filesystem.
1758 #ifndef _SYS_SYSPROTO_H_
1759 struct unlink_args {
1766 struct unlink_args /* {
1771 return (kern_unlink(td, uap->path, UIO_USERSPACE));
1774 #ifndef _SYS_SYSPROTO_H_
1775 struct unlinkat_args {
1782 sys_unlinkat(struct thread *td, struct unlinkat_args *uap)
1784 int flag = uap->flag;
1786 char *path = uap->path;
1788 if (flag & ~AT_REMOVEDIR)
1791 if (flag & AT_REMOVEDIR)
1792 return (kern_rmdirat(td, fd, path, UIO_USERSPACE));
1794 return (kern_unlinkat(td, fd, path, UIO_USERSPACE, 0));
1798 kern_unlink(struct thread *td, char *path, enum uio_seg pathseg)
1801 return (kern_unlinkat(td, AT_FDCWD, path, pathseg, 0));
1805 kern_unlinkat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1810 struct nameidata nd;
1812 cap_rights_t rights;
1817 NDINIT_ATRIGHTS(&nd, DELETE, LOCKPARENT | LOCKLEAF | AUDITVNODE1,
1818 pathseg, path, fd, cap_rights_init(&rights, CAP_UNLINKAT), td);
1819 if ((error = namei(&nd)) != 0)
1820 return (error == EINVAL ? EPERM : error);
1822 if (vp->v_type == VDIR && oldinum == 0) {
1823 error = EPERM; /* POSIX */
1824 } else if (oldinum != 0 &&
1825 ((error = vn_stat(vp, &sb, td->td_ucred, NOCRED, td)) == 0) &&
1826 sb.st_ino != oldinum) {
1827 error = EIDRM; /* Identifier removed */
1830 * The root of a mounted filesystem cannot be deleted.
1832 * XXX: can this only be a VDIR case?
1834 if (vp->v_vflag & VV_ROOT)
1838 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1839 NDFREE(&nd, NDF_ONLY_PNBUF);
1841 if (vp == nd.ni_dvp)
1845 if ((error = vn_start_write(NULL, &mp,
1846 V_XSLEEP | PCATCH)) != 0)
1851 error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp,
1856 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_UNLINK);
1857 error = VOP_REMOVE(nd.ni_dvp, vp, &nd.ni_cnd);
1861 vn_finished_write(mp);
1863 NDFREE(&nd, NDF_ONLY_PNBUF);
1865 if (vp == nd.ni_dvp)
1873 * Reposition read/write file offset.
1875 #ifndef _SYS_SYSPROTO_H_
1886 register struct lseek_args /* {
1894 cap_rights_t rights;
1897 AUDIT_ARG_FD(uap->fd);
1898 error = fget(td, uap->fd, cap_rights_init(&rights, CAP_SEEK), &fp);
1901 error = (fp->f_ops->fo_flags & DFLAG_SEEKABLE) != 0 ?
1902 fo_seek(fp, uap->offset, uap->whence, td) : ESPIPE;
1907 #if defined(COMPAT_43)
1909 * Reposition read/write file offset.
1911 #ifndef _SYS_SYSPROTO_H_
1912 struct olseek_args {
1921 register struct olseek_args /* {
1927 struct lseek_args /* {
1935 nuap.offset = uap->offset;
1936 nuap.whence = uap->whence;
1937 return (sys_lseek(td, &nuap));
1939 #endif /* COMPAT_43 */
1941 /* Version with the 'pad' argument */
1943 freebsd6_lseek(td, uap)
1945 register struct freebsd6_lseek_args *uap;
1947 struct lseek_args ouap;
1950 ouap.offset = uap->offset;
1951 ouap.whence = uap->whence;
1952 return (sys_lseek(td, &ouap));
1956 * Check access permissions using passed credentials.
1959 vn_access(vp, user_flags, cred, td)
1968 /* Flags == 0 means only check for existence. */
1972 if (user_flags & R_OK)
1974 if (user_flags & W_OK)
1976 if (user_flags & X_OK)
1979 error = mac_vnode_check_access(cred, vp, accmode);
1983 if ((accmode & VWRITE) == 0 || (error = vn_writechk(vp)) == 0)
1984 error = VOP_ACCESS(vp, accmode, cred, td);
1990 * Check access permissions using "real" credentials.
1992 #ifndef _SYS_SYSPROTO_H_
1993 struct access_args {
2001 register struct access_args /* {
2007 return (kern_access(td, uap->path, UIO_USERSPACE, uap->amode));
2010 #ifndef _SYS_SYSPROTO_H_
2011 struct faccessat_args {
2019 sys_faccessat(struct thread *td, struct faccessat_args *uap)
2022 if (uap->flag & ~AT_EACCESS)
2024 return (kern_accessat(td, uap->fd, uap->path, UIO_USERSPACE, uap->flag,
2029 kern_access(struct thread *td, char *path, enum uio_seg pathseg, int amode)
2032 return (kern_accessat(td, AT_FDCWD, path, pathseg, 0, amode));
2036 kern_accessat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2037 int flag, int amode)
2039 struct ucred *cred, *tmpcred;
2041 struct nameidata nd;
2042 cap_rights_t rights;
2046 * Create and modify a temporary credential instead of one that
2047 * is potentially shared.
2049 if (!(flag & AT_EACCESS)) {
2050 cred = td->td_ucred;
2051 tmpcred = crdup(cred);
2052 tmpcred->cr_uid = cred->cr_ruid;
2053 tmpcred->cr_groups[0] = cred->cr_rgid;
2054 td->td_ucred = tmpcred;
2056 cred = tmpcred = td->td_ucred;
2057 AUDIT_ARG_VALUE(amode);
2058 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF |
2059 AUDITVNODE1, pathseg, path, fd, cap_rights_init(&rights, CAP_FSTAT),
2061 if ((error = namei(&nd)) != 0)
2065 error = vn_access(vp, amode, tmpcred, td);
2066 NDFREE(&nd, NDF_ONLY_PNBUF);
2069 if (!(flag & AT_EACCESS)) {
2070 td->td_ucred = cred;
2077 * Check access permissions using "effective" credentials.
2079 #ifndef _SYS_SYSPROTO_H_
2080 struct eaccess_args {
2086 sys_eaccess(td, uap)
2088 register struct eaccess_args /* {
2094 return (kern_eaccess(td, uap->path, UIO_USERSPACE, uap->amode));
2098 kern_eaccess(struct thread *td, char *path, enum uio_seg pathseg, int amode)
2101 return (kern_accessat(td, AT_FDCWD, path, pathseg, AT_EACCESS, amode));
2104 #if defined(COMPAT_43)
2106 * Get file status; this version follows links.
2108 #ifndef _SYS_SYSPROTO_H_
2117 register struct ostat_args /* {
2126 error = kern_stat(td, uap->path, UIO_USERSPACE, &sb);
2130 return (copyout(&osb, uap->ub, sizeof (osb)));
2134 * Get file status; this version does not follow links.
2136 #ifndef _SYS_SYSPROTO_H_
2137 struct olstat_args {
2145 register struct olstat_args /* {
2154 error = kern_lstat(td, uap->path, UIO_USERSPACE, &sb);
2158 return (copyout(&osb, uap->ub, sizeof (osb)));
2162 * Convert from an old to a new stat structure.
2170 bzero(ost, sizeof(*ost));
2171 ost->st_dev = st->st_dev;
2172 ost->st_ino = st->st_ino;
2173 ost->st_mode = st->st_mode;
2174 ost->st_nlink = st->st_nlink;
2175 ost->st_uid = st->st_uid;
2176 ost->st_gid = st->st_gid;
2177 ost->st_rdev = st->st_rdev;
2178 if (st->st_size < (quad_t)1 << 32)
2179 ost->st_size = st->st_size;
2182 ost->st_atim = st->st_atim;
2183 ost->st_mtim = st->st_mtim;
2184 ost->st_ctim = st->st_ctim;
2185 ost->st_blksize = st->st_blksize;
2186 ost->st_blocks = st->st_blocks;
2187 ost->st_flags = st->st_flags;
2188 ost->st_gen = st->st_gen;
2190 #endif /* COMPAT_43 */
2193 * Get file status; this version follows links.
2195 #ifndef _SYS_SYSPROTO_H_
2204 register struct stat_args /* {
2212 error = kern_stat(td, uap->path, UIO_USERSPACE, &sb);
2214 error = copyout(&sb, uap->ub, sizeof (sb));
2218 #ifndef _SYS_SYSPROTO_H_
2219 struct fstatat_args {
2227 sys_fstatat(struct thread *td, struct fstatat_args *uap)
2232 error = kern_statat(td, uap->flag, uap->fd, uap->path,
2233 UIO_USERSPACE, &sb);
2235 error = copyout(&sb, uap->buf, sizeof (sb));
2240 kern_stat(struct thread *td, char *path, enum uio_seg pathseg, struct stat *sbp)
2243 return (kern_statat(td, 0, AT_FDCWD, path, pathseg, sbp));
2247 kern_statat(struct thread *td, int flag, int fd, char *path,
2248 enum uio_seg pathseg, struct stat *sbp)
2251 return (kern_statat_vnhook(td, flag, fd, path, pathseg, sbp, NULL));
2255 kern_statat_vnhook(struct thread *td, int flag, int fd, char *path,
2256 enum uio_seg pathseg, struct stat *sbp,
2257 void (*hook)(struct vnode *vp, struct stat *sbp))
2259 struct nameidata nd;
2261 cap_rights_t rights;
2264 if (flag & ~AT_SYMLINK_NOFOLLOW)
2267 NDINIT_ATRIGHTS(&nd, LOOKUP, ((flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW :
2268 FOLLOW) | LOCKSHARED | LOCKLEAF | AUDITVNODE1, pathseg, path, fd,
2269 cap_rights_init(&rights, CAP_FSTAT), td);
2271 if ((error = namei(&nd)) != 0)
2273 error = vn_stat(nd.ni_vp, &sb, td->td_ucred, NOCRED, td);
2275 SDT_PROBE2(vfs, , stat, mode, path, sb.st_mode);
2276 if (S_ISREG(sb.st_mode))
2277 SDT_PROBE2(vfs, , stat, reg, path, pathseg);
2278 if (__predict_false(hook != NULL))
2279 hook(nd.ni_vp, &sb);
2281 NDFREE(&nd, NDF_ONLY_PNBUF);
2287 if (KTRPOINT(td, KTR_STRUCT))
2294 * Get file status; this version does not follow links.
2296 #ifndef _SYS_SYSPROTO_H_
2305 register struct lstat_args /* {
2313 error = kern_lstat(td, uap->path, UIO_USERSPACE, &sb);
2315 error = copyout(&sb, uap->ub, sizeof (sb));
2320 kern_lstat(struct thread *td, char *path, enum uio_seg pathseg, struct stat *sbp)
2323 return (kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, path, pathseg,
2328 * Implementation of the NetBSD [l]stat() functions.
2336 bzero(nsb, sizeof *nsb);
2337 nsb->st_dev = sb->st_dev;
2338 nsb->st_ino = sb->st_ino;
2339 nsb->st_mode = sb->st_mode;
2340 nsb->st_nlink = sb->st_nlink;
2341 nsb->st_uid = sb->st_uid;
2342 nsb->st_gid = sb->st_gid;
2343 nsb->st_rdev = sb->st_rdev;
2344 nsb->st_atim = sb->st_atim;
2345 nsb->st_mtim = sb->st_mtim;
2346 nsb->st_ctim = sb->st_ctim;
2347 nsb->st_size = sb->st_size;
2348 nsb->st_blocks = sb->st_blocks;
2349 nsb->st_blksize = sb->st_blksize;
2350 nsb->st_flags = sb->st_flags;
2351 nsb->st_gen = sb->st_gen;
2352 nsb->st_birthtim = sb->st_birthtim;
2355 #ifndef _SYS_SYSPROTO_H_
2364 register struct nstat_args /* {
2373 error = kern_stat(td, uap->path, UIO_USERSPACE, &sb);
2376 cvtnstat(&sb, &nsb);
2377 return (copyout(&nsb, uap->ub, sizeof (nsb)));
2381 * NetBSD lstat. Get file status; this version does not follow links.
2383 #ifndef _SYS_SYSPROTO_H_
2392 register struct nlstat_args /* {
2401 error = kern_lstat(td, uap->path, UIO_USERSPACE, &sb);
2404 cvtnstat(&sb, &nsb);
2405 return (copyout(&nsb, uap->ub, sizeof (nsb)));
2409 * Get configurable pathname variables.
2411 #ifndef _SYS_SYSPROTO_H_
2412 struct pathconf_args {
2418 sys_pathconf(td, uap)
2420 register struct pathconf_args /* {
2426 return (kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name, FOLLOW));
2429 #ifndef _SYS_SYSPROTO_H_
2430 struct lpathconf_args {
2436 sys_lpathconf(td, uap)
2438 register struct lpathconf_args /* {
2444 return (kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name,
2449 kern_pathconf(struct thread *td, char *path, enum uio_seg pathseg, int name,
2452 struct nameidata nd;
2455 NDINIT(&nd, LOOKUP, LOCKSHARED | LOCKLEAF | AUDITVNODE1 | flags,
2457 if ((error = namei(&nd)) != 0)
2459 NDFREE(&nd, NDF_ONLY_PNBUF);
2461 /* If asynchronous I/O is available, it works for all files. */
2462 if (name == _PC_ASYNC_IO)
2463 td->td_retval[0] = async_io_version;
2465 error = VOP_PATHCONF(nd.ni_vp, name, td->td_retval);
2471 * Return target name of a symbolic link.
2473 #ifndef _SYS_SYSPROTO_H_
2474 struct readlink_args {
2481 sys_readlink(td, uap)
2483 register struct readlink_args /* {
2490 return (kern_readlink(td, uap->path, UIO_USERSPACE, uap->buf,
2491 UIO_USERSPACE, uap->count));
2493 #ifndef _SYS_SYSPROTO_H_
2494 struct readlinkat_args {
2502 sys_readlinkat(struct thread *td, struct readlinkat_args *uap)
2505 return (kern_readlinkat(td, uap->fd, uap->path, UIO_USERSPACE,
2506 uap->buf, UIO_USERSPACE, uap->bufsize));
2510 kern_readlink(struct thread *td, char *path, enum uio_seg pathseg, char *buf,
2511 enum uio_seg bufseg, size_t count)
2514 return (kern_readlinkat(td, AT_FDCWD, path, pathseg, buf, bufseg,
2519 kern_readlinkat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2520 char *buf, enum uio_seg bufseg, size_t count)
2525 struct nameidata nd;
2528 if (count > IOSIZE_MAX)
2531 NDINIT_AT(&nd, LOOKUP, NOFOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
2532 pathseg, path, fd, td);
2534 if ((error = namei(&nd)) != 0)
2536 NDFREE(&nd, NDF_ONLY_PNBUF);
2539 error = mac_vnode_check_readlink(td->td_ucred, vp);
2545 if (vp->v_type != VLNK)
2548 aiov.iov_base = buf;
2549 aiov.iov_len = count;
2550 auio.uio_iov = &aiov;
2551 auio.uio_iovcnt = 1;
2552 auio.uio_offset = 0;
2553 auio.uio_rw = UIO_READ;
2554 auio.uio_segflg = bufseg;
2556 auio.uio_resid = count;
2557 error = VOP_READLINK(vp, &auio, td->td_ucred);
2558 td->td_retval[0] = count - auio.uio_resid;
2565 * Common implementation code for chflags() and fchflags().
2568 setfflags(td, vp, flags)
2577 /* We can't support the value matching VNOVAL. */
2578 if (flags == VNOVAL)
2579 return (EOPNOTSUPP);
2582 * Prevent non-root users from setting flags on devices. When
2583 * a device is reused, users can retain ownership of the device
2584 * if they are allowed to set flags and programs assume that
2585 * chown can't fail when done as root.
2587 if (vp->v_type == VCHR || vp->v_type == VBLK) {
2588 error = priv_check(td, PRIV_VFS_CHFLAGS_DEV);
2593 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2596 vattr.va_flags = flags;
2597 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2599 error = mac_vnode_check_setflags(td->td_ucred, vp, vattr.va_flags);
2602 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
2604 vn_finished_write(mp);
2609 * Change flags of a file given a path name.
2611 #ifndef _SYS_SYSPROTO_H_
2612 struct chflags_args {
2618 sys_chflags(td, uap)
2620 register struct chflags_args /* {
2626 return (kern_chflags(td, uap->path, UIO_USERSPACE, uap->flags));
2629 #ifndef _SYS_SYSPROTO_H_
2630 struct chflagsat_args {
2638 sys_chflagsat(struct thread *td, struct chflagsat_args *uap)
2641 const char *path = uap->path;
2642 u_long flags = uap->flags;
2643 int atflag = uap->atflag;
2645 if (atflag & ~AT_SYMLINK_NOFOLLOW)
2648 return (kern_chflagsat(td, fd, path, UIO_USERSPACE, flags, atflag));
2652 kern_chflags(struct thread *td, const char *path, enum uio_seg pathseg,
2656 return (kern_chflagsat(td, AT_FDCWD, path, pathseg, flags, 0));
2660 * Same as chflags() but doesn't follow symlinks.
2663 sys_lchflags(td, uap)
2665 register struct lchflags_args /* {
2671 return (kern_chflagsat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2672 uap->flags, AT_SYMLINK_NOFOLLOW));
2676 kern_chflagsat(struct thread *td, int fd, const char *path,
2677 enum uio_seg pathseg, u_long flags, int atflag)
2679 struct nameidata nd;
2680 cap_rights_t rights;
2683 AUDIT_ARG_FFLAGS(flags);
2684 follow = (atflag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
2685 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, pathseg, path, fd,
2686 cap_rights_init(&rights, CAP_FCHFLAGS), td);
2687 if ((error = namei(&nd)) != 0)
2689 NDFREE(&nd, NDF_ONLY_PNBUF);
2690 error = setfflags(td, nd.ni_vp, flags);
2696 * Change flags of a file given a file descriptor.
2698 #ifndef _SYS_SYSPROTO_H_
2699 struct fchflags_args {
2705 sys_fchflags(td, uap)
2707 register struct fchflags_args /* {
2713 cap_rights_t rights;
2716 AUDIT_ARG_FD(uap->fd);
2717 AUDIT_ARG_FFLAGS(uap->flags);
2718 error = getvnode(td->td_proc->p_fd, uap->fd,
2719 cap_rights_init(&rights, CAP_FCHFLAGS), &fp);
2723 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
2724 AUDIT_ARG_VNODE1(fp->f_vnode);
2725 VOP_UNLOCK(fp->f_vnode, 0);
2727 error = setfflags(td, fp->f_vnode, uap->flags);
2733 * Common implementation code for chmod(), lchmod() and fchmod().
2736 setfmode(td, cred, vp, mode)
2746 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2748 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2750 vattr.va_mode = mode & ALLPERMS;
2752 error = mac_vnode_check_setmode(cred, vp, vattr.va_mode);
2755 error = VOP_SETATTR(vp, &vattr, cred);
2757 vn_finished_write(mp);
2762 * Change mode of a file given path name.
2764 #ifndef _SYS_SYSPROTO_H_
2773 register struct chmod_args /* {
2779 return (kern_chmod(td, uap->path, UIO_USERSPACE, uap->mode));
2782 #ifndef _SYS_SYSPROTO_H_
2783 struct fchmodat_args {
2791 sys_fchmodat(struct thread *td, struct fchmodat_args *uap)
2793 int flag = uap->flag;
2795 char *path = uap->path;
2796 mode_t mode = uap->mode;
2798 if (flag & ~AT_SYMLINK_NOFOLLOW)
2801 return (kern_fchmodat(td, fd, path, UIO_USERSPACE, mode, flag));
2805 kern_chmod(struct thread *td, char *path, enum uio_seg pathseg, int mode)
2808 return (kern_fchmodat(td, AT_FDCWD, path, pathseg, mode, 0));
2812 * Change mode of a file given path name (don't follow links.)
2814 #ifndef _SYS_SYSPROTO_H_
2815 struct lchmod_args {
2823 register struct lchmod_args /* {
2829 return (kern_fchmodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2830 uap->mode, AT_SYMLINK_NOFOLLOW));
2834 kern_fchmodat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2835 mode_t mode, int flag)
2837 struct nameidata nd;
2838 cap_rights_t rights;
2841 AUDIT_ARG_MODE(mode);
2842 follow = (flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
2843 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, pathseg, path, fd,
2844 cap_rights_init(&rights, CAP_FCHMOD), td);
2845 if ((error = namei(&nd)) != 0)
2847 NDFREE(&nd, NDF_ONLY_PNBUF);
2848 error = setfmode(td, td->td_ucred, nd.ni_vp, mode);
2854 * Change mode of a file given a file descriptor.
2856 #ifndef _SYS_SYSPROTO_H_
2857 struct fchmod_args {
2863 sys_fchmod(struct thread *td, struct fchmod_args *uap)
2866 cap_rights_t rights;
2869 AUDIT_ARG_FD(uap->fd);
2870 AUDIT_ARG_MODE(uap->mode);
2872 error = fget(td, uap->fd, cap_rights_init(&rights, CAP_FCHMOD), &fp);
2875 error = fo_chmod(fp, uap->mode, td->td_ucred, td);
2881 * Common implementation for chown(), lchown(), and fchown()
2884 setfown(td, cred, vp, uid, gid)
2895 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2897 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2902 error = mac_vnode_check_setowner(cred, vp, vattr.va_uid,
2906 error = VOP_SETATTR(vp, &vattr, cred);
2908 vn_finished_write(mp);
2913 * Set ownership given a path name.
2915 #ifndef _SYS_SYSPROTO_H_
2925 register struct chown_args /* {
2932 return (kern_chown(td, uap->path, UIO_USERSPACE, uap->uid, uap->gid));
2935 #ifndef _SYS_SYSPROTO_H_
2936 struct fchownat_args {
2945 sys_fchownat(struct thread *td, struct fchownat_args *uap)
2950 if (flag & ~AT_SYMLINK_NOFOLLOW)
2953 return (kern_fchownat(td, uap->fd, uap->path, UIO_USERSPACE, uap->uid,
2954 uap->gid, uap->flag));
2958 kern_chown(struct thread *td, char *path, enum uio_seg pathseg, int uid,
2962 return (kern_fchownat(td, AT_FDCWD, path, pathseg, uid, gid, 0));
2966 kern_fchownat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2967 int uid, int gid, int flag)
2969 struct nameidata nd;
2970 cap_rights_t rights;
2973 AUDIT_ARG_OWNER(uid, gid);
2974 follow = (flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
2975 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, pathseg, path, fd,
2976 cap_rights_init(&rights, CAP_FCHOWN), td);
2978 if ((error = namei(&nd)) != 0)
2980 NDFREE(&nd, NDF_ONLY_PNBUF);
2981 error = setfown(td, td->td_ucred, nd.ni_vp, uid, gid);
2987 * Set ownership given a path name, do not cross symlinks.
2989 #ifndef _SYS_SYSPROTO_H_
2990 struct lchown_args {
2999 register struct lchown_args /* {
3006 return (kern_lchown(td, uap->path, UIO_USERSPACE, uap->uid, uap->gid));
3010 kern_lchown(struct thread *td, char *path, enum uio_seg pathseg, int uid,
3014 return (kern_fchownat(td, AT_FDCWD, path, pathseg, uid, gid,
3015 AT_SYMLINK_NOFOLLOW));
3019 * Set ownership given a file descriptor.
3021 #ifndef _SYS_SYSPROTO_H_
3022 struct fchown_args {
3031 register struct fchown_args /* {
3038 cap_rights_t rights;
3041 AUDIT_ARG_FD(uap->fd);
3042 AUDIT_ARG_OWNER(uap->uid, uap->gid);
3043 error = fget(td, uap->fd, cap_rights_init(&rights, CAP_FCHOWN), &fp);
3046 error = fo_chown(fp, uap->uid, uap->gid, td->td_ucred, td);
3052 * Common implementation code for utimes(), lutimes(), and futimes().
3055 getutimes(usrtvp, tvpseg, tsp)
3056 const struct timeval *usrtvp;
3057 enum uio_seg tvpseg;
3058 struct timespec *tsp;
3060 struct timeval tv[2];
3061 const struct timeval *tvp;
3064 if (usrtvp == NULL) {
3065 vfs_timestamp(&tsp[0]);
3068 if (tvpseg == UIO_SYSSPACE) {
3071 if ((error = copyin(usrtvp, tv, sizeof(tv))) != 0)
3076 if (tvp[0].tv_usec < 0 || tvp[0].tv_usec >= 1000000 ||
3077 tvp[1].tv_usec < 0 || tvp[1].tv_usec >= 1000000)
3079 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
3080 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
3086 * Common implementation code for futimens(), utimensat().
3088 #define UTIMENS_NULL 0x1
3089 #define UTIMENS_EXIT 0x2
3091 getutimens(const struct timespec *usrtsp, enum uio_seg tspseg,
3092 struct timespec *tsp, int *retflags)
3094 struct timespec tsnow;
3097 vfs_timestamp(&tsnow);
3099 if (usrtsp == NULL) {
3102 *retflags |= UTIMENS_NULL;
3105 if (tspseg == UIO_SYSSPACE) {
3108 } else if ((error = copyin(usrtsp, tsp, sizeof(*tsp) * 2)) != 0)
3110 if (tsp[0].tv_nsec == UTIME_OMIT && tsp[1].tv_nsec == UTIME_OMIT)
3111 *retflags |= UTIMENS_EXIT;
3112 if (tsp[0].tv_nsec == UTIME_NOW && tsp[1].tv_nsec == UTIME_NOW)
3113 *retflags |= UTIMENS_NULL;
3114 if (tsp[0].tv_nsec == UTIME_OMIT)
3115 tsp[0].tv_sec = VNOVAL;
3116 else if (tsp[0].tv_nsec == UTIME_NOW)
3118 else if (tsp[0].tv_nsec < 0 || tsp[0].tv_nsec >= 1000000000L)
3120 if (tsp[1].tv_nsec == UTIME_OMIT)
3121 tsp[1].tv_sec = VNOVAL;
3122 else if (tsp[1].tv_nsec == UTIME_NOW)
3124 else if (tsp[1].tv_nsec < 0 || tsp[1].tv_nsec >= 1000000000L)
3131 * Common implementation code for utimes(), lutimes(), futimes(), futimens(),
3135 setutimes(td, vp, ts, numtimes, nullflag)
3138 const struct timespec *ts;
3144 int error, setbirthtime;
3146 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
3148 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3150 if (numtimes < 3 && !VOP_GETATTR(vp, &vattr, td->td_ucred) &&
3151 timespeccmp(&ts[1], &vattr.va_birthtime, < ))
3154 vattr.va_atime = ts[0];
3155 vattr.va_mtime = ts[1];
3157 vattr.va_birthtime = ts[1];
3159 vattr.va_birthtime = ts[2];
3161 vattr.va_vaflags |= VA_UTIMES_NULL;
3163 error = mac_vnode_check_setutimes(td->td_ucred, vp, vattr.va_atime,
3167 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3169 vn_finished_write(mp);
3174 * Set the access and modification times of a file.
3176 #ifndef _SYS_SYSPROTO_H_
3177 struct utimes_args {
3179 struct timeval *tptr;
3185 register struct utimes_args /* {
3187 struct timeval *tptr;
3191 return (kern_utimes(td, uap->path, UIO_USERSPACE, uap->tptr,
3195 #ifndef _SYS_SYSPROTO_H_
3196 struct futimesat_args {
3199 const struct timeval * times;
3203 sys_futimesat(struct thread *td, struct futimesat_args *uap)
3206 return (kern_utimesat(td, uap->fd, uap->path, UIO_USERSPACE,
3207 uap->times, UIO_USERSPACE));
3211 kern_utimes(struct thread *td, char *path, enum uio_seg pathseg,
3212 struct timeval *tptr, enum uio_seg tptrseg)
3215 return (kern_utimesat(td, AT_FDCWD, path, pathseg, tptr, tptrseg));
3219 kern_utimesat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
3220 struct timeval *tptr, enum uio_seg tptrseg)
3222 struct nameidata nd;
3223 struct timespec ts[2];
3224 cap_rights_t rights;
3227 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3229 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, fd,
3230 cap_rights_init(&rights, CAP_FUTIMES), td);
3232 if ((error = namei(&nd)) != 0)
3234 NDFREE(&nd, NDF_ONLY_PNBUF);
3235 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3241 * Set the access and modification times of a file.
3243 #ifndef _SYS_SYSPROTO_H_
3244 struct lutimes_args {
3246 struct timeval *tptr;
3250 sys_lutimes(td, uap)
3252 register struct lutimes_args /* {
3254 struct timeval *tptr;
3258 return (kern_lutimes(td, uap->path, UIO_USERSPACE, uap->tptr,
3263 kern_lutimes(struct thread *td, char *path, enum uio_seg pathseg,
3264 struct timeval *tptr, enum uio_seg tptrseg)
3266 struct timespec ts[2];
3267 struct nameidata nd;
3270 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3272 NDINIT(&nd, LOOKUP, NOFOLLOW | AUDITVNODE1, pathseg, path, td);
3273 if ((error = namei(&nd)) != 0)
3275 NDFREE(&nd, NDF_ONLY_PNBUF);
3276 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3282 * Set the access and modification times of a file.
3284 #ifndef _SYS_SYSPROTO_H_
3285 struct futimes_args {
3287 struct timeval *tptr;
3291 sys_futimes(td, uap)
3293 register struct futimes_args /* {
3295 struct timeval *tptr;
3299 return (kern_futimes(td, uap->fd, uap->tptr, UIO_USERSPACE));
3303 kern_futimes(struct thread *td, int fd, struct timeval *tptr,
3304 enum uio_seg tptrseg)
3306 struct timespec ts[2];
3308 cap_rights_t rights;
3312 error = getutimes(tptr, tptrseg, ts);
3315 error = getvnode(td->td_proc->p_fd, fd,
3316 cap_rights_init(&rights, CAP_FUTIMES), &fp);
3320 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
3321 AUDIT_ARG_VNODE1(fp->f_vnode);
3322 VOP_UNLOCK(fp->f_vnode, 0);
3324 error = setutimes(td, fp->f_vnode, ts, 2, tptr == NULL);
3330 sys_futimens(struct thread *td, struct futimens_args *uap)
3333 return (kern_futimens(td, uap->fd, uap->times, UIO_USERSPACE));
3337 kern_futimens(struct thread *td, int fd, struct timespec *tptr,
3338 enum uio_seg tptrseg)
3340 struct timespec ts[2];
3342 cap_rights_t rights;
3346 error = getutimens(tptr, tptrseg, ts, &flags);
3349 if (flags & UTIMENS_EXIT)
3351 error = getvnode(td->td_proc->p_fd, fd,
3352 cap_rights_init(&rights, CAP_FUTIMES), &fp);
3356 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
3357 AUDIT_ARG_VNODE1(fp->f_vnode);
3358 VOP_UNLOCK(fp->f_vnode, 0);
3360 error = setutimes(td, fp->f_vnode, ts, 2, flags & UTIMENS_NULL);
3366 sys_utimensat(struct thread *td, struct utimensat_args *uap)
3369 return (kern_utimensat(td, uap->fd, uap->path, UIO_USERSPACE,
3370 uap->times, UIO_USERSPACE, uap->flag));
3374 kern_utimensat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
3375 struct timespec *tptr, enum uio_seg tptrseg, int flag)
3377 struct nameidata nd;
3378 struct timespec ts[2];
3379 cap_rights_t rights;
3382 if (flag & ~AT_SYMLINK_NOFOLLOW)
3385 if ((error = getutimens(tptr, tptrseg, ts, &flags)) != 0)
3387 NDINIT_ATRIGHTS(&nd, LOOKUP, ((flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW :
3388 FOLLOW) | AUDITVNODE1, pathseg, path, fd,
3389 cap_rights_init(&rights, CAP_FUTIMES), td);
3390 if ((error = namei(&nd)) != 0)
3393 * We are allowed to call namei() regardless of 2xUTIME_OMIT.
3395 * "If both tv_nsec fields are UTIME_OMIT... EACCESS may be detected."
3396 * "Search permission is denied by a component of the path prefix."
3398 NDFREE(&nd, NDF_ONLY_PNBUF);
3399 if ((flags & UTIMENS_EXIT) == 0)
3400 error = setutimes(td, nd.ni_vp, ts, 2, flags & UTIMENS_NULL);
3406 * Truncate a file given its path name.
3408 #ifndef _SYS_SYSPROTO_H_
3409 struct truncate_args {
3416 sys_truncate(td, uap)
3418 register struct truncate_args /* {
3425 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3429 kern_truncate(struct thread *td, char *path, enum uio_seg pathseg, off_t length)
3435 struct nameidata nd;
3440 NDINIT(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, td);
3441 if ((error = namei(&nd)) != 0)
3444 rl_cookie = vn_rangelock_wlock(vp, 0, OFF_MAX);
3445 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0) {
3446 vn_rangelock_unlock(vp, rl_cookie);
3450 NDFREE(&nd, NDF_ONLY_PNBUF);
3451 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3452 if (vp->v_type == VDIR)
3455 else if ((error = mac_vnode_check_write(td->td_ucred, NOCRED, vp))) {
3458 else if ((error = vn_writechk(vp)) == 0 &&
3459 (error = VOP_ACCESS(vp, VWRITE, td->td_ucred, td)) == 0) {
3461 vattr.va_size = length;
3462 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3465 vn_finished_write(mp);
3466 vn_rangelock_unlock(vp, rl_cookie);
3471 #if defined(COMPAT_43)
3473 * Truncate a file given its path name.
3475 #ifndef _SYS_SYSPROTO_H_
3476 struct otruncate_args {
3484 register struct otruncate_args /* {
3489 struct truncate_args /* {
3495 nuap.path = uap->path;
3496 nuap.length = uap->length;
3497 return (sys_truncate(td, &nuap));
3499 #endif /* COMPAT_43 */
3501 /* Versions with the pad argument */
3503 freebsd6_truncate(struct thread *td, struct freebsd6_truncate_args *uap)
3505 struct truncate_args ouap;
3507 ouap.path = uap->path;
3508 ouap.length = uap->length;
3509 return (sys_truncate(td, &ouap));
3513 freebsd6_ftruncate(struct thread *td, struct freebsd6_ftruncate_args *uap)
3515 struct ftruncate_args ouap;
3518 ouap.length = uap->length;
3519 return (sys_ftruncate(td, &ouap));
3523 * Sync an open file.
3525 #ifndef _SYS_SYSPROTO_H_
3533 struct fsync_args /* {
3540 cap_rights_t rights;
3541 int error, lock_flags;
3543 AUDIT_ARG_FD(uap->fd);
3544 error = getvnode(td->td_proc->p_fd, uap->fd,
3545 cap_rights_init(&rights, CAP_FSYNC), &fp);
3549 error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
3552 if (MNT_SHARED_WRITES(mp) ||
3553 ((mp == NULL) && MNT_SHARED_WRITES(vp->v_mount))) {
3554 lock_flags = LK_SHARED;
3556 lock_flags = LK_EXCLUSIVE;
3558 vn_lock(vp, lock_flags | LK_RETRY);
3559 AUDIT_ARG_VNODE1(vp);
3560 if (vp->v_object != NULL) {
3561 VM_OBJECT_WLOCK(vp->v_object);
3562 vm_object_page_clean(vp->v_object, 0, 0, 0);
3563 VM_OBJECT_WUNLOCK(vp->v_object);
3565 error = VOP_FSYNC(vp, MNT_WAIT, td);
3568 vn_finished_write(mp);
3575 * Rename files. Source and destination must either both be directories, or
3576 * both not be directories. If target is a directory, it must be empty.
3578 #ifndef _SYS_SYSPROTO_H_
3579 struct rename_args {
3587 register struct rename_args /* {
3593 return (kern_rename(td, uap->from, uap->to, UIO_USERSPACE));
3596 #ifndef _SYS_SYSPROTO_H_
3597 struct renameat_args {
3605 sys_renameat(struct thread *td, struct renameat_args *uap)
3608 return (kern_renameat(td, uap->oldfd, uap->old, uap->newfd, uap->new,
3613 kern_rename(struct thread *td, char *from, char *to, enum uio_seg pathseg)
3616 return (kern_renameat(td, AT_FDCWD, from, AT_FDCWD, to, pathseg));
3620 kern_renameat(struct thread *td, int oldfd, char *old, int newfd, char *new,
3621 enum uio_seg pathseg)
3623 struct mount *mp = NULL;
3624 struct vnode *tvp, *fvp, *tdvp;
3625 struct nameidata fromnd, tond;
3626 cap_rights_t rights;
3632 NDINIT_ATRIGHTS(&fromnd, DELETE, LOCKPARENT | LOCKLEAF | SAVESTART |
3633 AUDITVNODE1, pathseg, old, oldfd,
3634 cap_rights_init(&rights, CAP_RENAMEAT), td);
3636 NDINIT_ATRIGHTS(&fromnd, DELETE, WANTPARENT | SAVESTART | AUDITVNODE1,
3637 pathseg, old, oldfd, cap_rights_init(&rights, CAP_RENAMEAT), td);
3640 if ((error = namei(&fromnd)) != 0)
3643 error = mac_vnode_check_rename_from(td->td_ucred, fromnd.ni_dvp,
3644 fromnd.ni_vp, &fromnd.ni_cnd);
3645 VOP_UNLOCK(fromnd.ni_dvp, 0);
3646 if (fromnd.ni_dvp != fromnd.ni_vp)
3647 VOP_UNLOCK(fromnd.ni_vp, 0);
3650 NDINIT_ATRIGHTS(&tond, RENAME, LOCKPARENT | LOCKLEAF | NOCACHE |
3651 SAVESTART | AUDITVNODE2, pathseg, new, newfd,
3652 cap_rights_init(&rights, CAP_LINKAT), td);
3653 if (fromnd.ni_vp->v_type == VDIR)
3654 tond.ni_cnd.cn_flags |= WILLBEDIR;
3655 if ((error = namei(&tond)) != 0) {
3656 /* Translate error code for rename("dir1", "dir2/."). */
3657 if (error == EISDIR && fvp->v_type == VDIR)
3659 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3660 vrele(fromnd.ni_dvp);
3666 error = vn_start_write(fvp, &mp, V_NOWAIT);
3668 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3669 NDFREE(&tond, NDF_ONLY_PNBUF);
3676 vrele(fromnd.ni_dvp);
3678 vrele(tond.ni_startdir);
3679 if (fromnd.ni_startdir != NULL)
3680 vrele(fromnd.ni_startdir);
3681 error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH);
3687 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
3690 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
3695 if (newfd != AT_FDCWD) {
3697 * If the target already exists we require CAP_UNLINKAT
3700 error = cap_check(&tond.ni_filecaps.fc_rights,
3701 cap_rights_init(&rights, CAP_UNLINKAT));
3712 * If the source is the same as the destination (that is, if they
3713 * are links to the same vnode), then there is nothing to do.
3719 error = mac_vnode_check_rename_to(td->td_ucred, tdvp,
3720 tond.ni_vp, fromnd.ni_dvp == tdvp, &tond.ni_cnd);
3724 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
3725 tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
3726 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3727 NDFREE(&tond, NDF_ONLY_PNBUF);
3729 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3730 NDFREE(&tond, NDF_ONLY_PNBUF);
3737 vrele(fromnd.ni_dvp);
3740 vrele(tond.ni_startdir);
3741 vn_finished_write(mp);
3743 if (fromnd.ni_startdir)
3744 vrele(fromnd.ni_startdir);
3751 * Make a directory file.
3753 #ifndef _SYS_SYSPROTO_H_
3762 register struct mkdir_args /* {
3768 return (kern_mkdir(td, uap->path, UIO_USERSPACE, uap->mode));
3771 #ifndef _SYS_SYSPROTO_H_
3772 struct mkdirat_args {
3779 sys_mkdirat(struct thread *td, struct mkdirat_args *uap)
3782 return (kern_mkdirat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode));
3786 kern_mkdir(struct thread *td, char *path, enum uio_seg segflg, int mode)
3789 return (kern_mkdirat(td, AT_FDCWD, path, segflg, mode));
3793 kern_mkdirat(struct thread *td, int fd, char *path, enum uio_seg segflg,
3799 struct nameidata nd;
3800 cap_rights_t rights;
3803 AUDIT_ARG_MODE(mode);
3806 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
3807 NOCACHE, segflg, path, fd, cap_rights_init(&rights, CAP_MKDIRAT),
3809 nd.ni_cnd.cn_flags |= WILLBEDIR;
3810 if ((error = namei(&nd)) != 0)
3814 NDFREE(&nd, NDF_ONLY_PNBUF);
3816 * XXX namei called with LOCKPARENT but not LOCKLEAF has
3817 * the strange behaviour of leaving the vnode unlocked
3818 * if the target is the same vnode as the parent.
3820 if (vp == nd.ni_dvp)
3827 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3828 NDFREE(&nd, NDF_ONLY_PNBUF);
3830 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3835 vattr.va_type = VDIR;
3836 vattr.va_mode = (mode & ACCESSPERMS) &~ td->td_proc->p_fd->fd_cmask;
3838 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
3843 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
3847 NDFREE(&nd, NDF_ONLY_PNBUF);
3851 vn_finished_write(mp);
3856 * Remove a directory file.
3858 #ifndef _SYS_SYSPROTO_H_
3866 struct rmdir_args /* {
3871 return (kern_rmdir(td, uap->path, UIO_USERSPACE));
3875 kern_rmdir(struct thread *td, char *path, enum uio_seg pathseg)
3878 return (kern_rmdirat(td, AT_FDCWD, path, pathseg));
3882 kern_rmdirat(struct thread *td, int fd, char *path, enum uio_seg pathseg)
3886 struct nameidata nd;
3887 cap_rights_t rights;
3892 NDINIT_ATRIGHTS(&nd, DELETE, LOCKPARENT | LOCKLEAF | AUDITVNODE1,
3893 pathseg, path, fd, cap_rights_init(&rights, CAP_UNLINKAT), td);
3894 if ((error = namei(&nd)) != 0)
3897 if (vp->v_type != VDIR) {
3902 * No rmdir "." please.
3904 if (nd.ni_dvp == vp) {
3909 * The root of a mounted filesystem cannot be deleted.
3911 if (vp->v_vflag & VV_ROOT) {
3916 error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp,
3921 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3922 NDFREE(&nd, NDF_ONLY_PNBUF);
3924 if (nd.ni_dvp == vp)
3928 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3932 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_UNLINK);
3933 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
3934 vn_finished_write(mp);
3936 NDFREE(&nd, NDF_ONLY_PNBUF);
3938 if (nd.ni_dvp == vp)
3947 * Read a block of directory entries in a filesystem independent format.
3949 #ifndef _SYS_SYSPROTO_H_
3950 struct ogetdirentries_args {
3958 ogetdirentries(struct thread *td, struct ogetdirentries_args *uap)
3963 error = kern_ogetdirentries(td, uap, &loff);
3965 error = copyout(&loff, uap->basep, sizeof(long));
3970 kern_ogetdirentries(struct thread *td, struct ogetdirentries_args *uap,
3975 struct uio auio, kuio;
3976 struct iovec aiov, kiov;
3977 struct dirent *dp, *edp;
3978 cap_rights_t rights;
3980 int error, eofflag, readcnt;
3984 /* XXX arbitrary sanity limit on `count'. */
3985 if (uap->count > 64 * 1024)
3987 error = getvnode(td->td_proc->p_fd, uap->fd,
3988 cap_rights_init(&rights, CAP_READ), &fp);
3991 if ((fp->f_flag & FREAD) == 0) {
3996 foffset = foffset_lock(fp, 0);
3998 if (vp->v_type != VDIR) {
3999 foffset_unlock(fp, foffset, 0);
4003 aiov.iov_base = uap->buf;
4004 aiov.iov_len = uap->count;
4005 auio.uio_iov = &aiov;
4006 auio.uio_iovcnt = 1;
4007 auio.uio_rw = UIO_READ;
4008 auio.uio_segflg = UIO_USERSPACE;
4010 auio.uio_resid = uap->count;
4011 vn_lock(vp, LK_SHARED | LK_RETRY);
4012 loff = auio.uio_offset = foffset;
4014 error = mac_vnode_check_readdir(td->td_ucred, vp);
4017 foffset_unlock(fp, foffset, FOF_NOUPDATE);
4022 # if (BYTE_ORDER != LITTLE_ENDIAN)
4023 if (vp->v_mount->mnt_maxsymlinklen <= 0) {
4024 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag,
4026 foffset = auio.uio_offset;
4031 kuio.uio_iov = &kiov;
4032 kuio.uio_segflg = UIO_SYSSPACE;
4033 kiov.iov_len = uap->count;
4034 dirbuf = malloc(uap->count, M_TEMP, M_WAITOK);
4035 kiov.iov_base = dirbuf;
4036 error = VOP_READDIR(vp, &kuio, fp->f_cred, &eofflag,
4038 foffset = kuio.uio_offset;
4040 readcnt = uap->count - kuio.uio_resid;
4041 edp = (struct dirent *)&dirbuf[readcnt];
4042 for (dp = (struct dirent *)dirbuf; dp < edp; ) {
4043 # if (BYTE_ORDER == LITTLE_ENDIAN)
4045 * The expected low byte of
4046 * dp->d_namlen is our dp->d_type.
4047 * The high MBZ byte of dp->d_namlen
4048 * is our dp->d_namlen.
4050 dp->d_type = dp->d_namlen;
4054 * The dp->d_type is the high byte
4055 * of the expected dp->d_namlen,
4056 * so must be zero'ed.
4060 if (dp->d_reclen > 0) {
4061 dp = (struct dirent *)
4062 ((char *)dp + dp->d_reclen);
4069 error = uiomove(dirbuf, readcnt, &auio);
4071 free(dirbuf, M_TEMP);
4075 foffset_unlock(fp, foffset, 0);
4079 if (uap->count == auio.uio_resid &&
4080 (vp->v_vflag & VV_ROOT) &&
4081 (vp->v_mount->mnt_flag & MNT_UNION)) {
4082 struct vnode *tvp = vp;
4083 vp = vp->v_mount->mnt_vnodecovered;
4092 foffset_unlock(fp, foffset, 0);
4094 td->td_retval[0] = uap->count - auio.uio_resid;
4099 #endif /* COMPAT_43 */
4102 * Read a block of directory entries in a filesystem independent format.
4104 #ifndef _SYS_SYSPROTO_H_
4105 struct getdirentries_args {
4113 sys_getdirentries(td, uap)
4115 register struct getdirentries_args /* {
4125 error = kern_getdirentries(td, uap->fd, uap->buf, uap->count, &base,
4126 NULL, UIO_USERSPACE);
4129 if (uap->basep != NULL)
4130 error = copyout(&base, uap->basep, sizeof(long));
4135 kern_getdirentries(struct thread *td, int fd, char *buf, u_int count,
4136 long *basep, ssize_t *residp, enum uio_seg bufseg)
4142 cap_rights_t rights;
4148 if (count > IOSIZE_MAX)
4150 auio.uio_resid = count;
4151 error = getvnode(td->td_proc->p_fd, fd,
4152 cap_rights_init(&rights, CAP_READ), &fp);
4155 if ((fp->f_flag & FREAD) == 0) {
4160 foffset = foffset_lock(fp, 0);
4162 if (vp->v_type != VDIR) {
4166 aiov.iov_base = buf;
4167 aiov.iov_len = count;
4168 auio.uio_iov = &aiov;
4169 auio.uio_iovcnt = 1;
4170 auio.uio_rw = UIO_READ;
4171 auio.uio_segflg = bufseg;
4173 vn_lock(vp, LK_SHARED | LK_RETRY);
4174 AUDIT_ARG_VNODE1(vp);
4175 loff = auio.uio_offset = foffset;
4177 error = mac_vnode_check_readdir(td->td_ucred, vp);
4180 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL,
4182 foffset = auio.uio_offset;
4187 if (count == auio.uio_resid &&
4188 (vp->v_vflag & VV_ROOT) &&
4189 (vp->v_mount->mnt_flag & MNT_UNION)) {
4190 struct vnode *tvp = vp;
4192 vp = vp->v_mount->mnt_vnodecovered;
4203 *residp = auio.uio_resid;
4204 td->td_retval[0] = count - auio.uio_resid;
4206 foffset_unlock(fp, foffset, 0);
4211 #ifndef _SYS_SYSPROTO_H_
4212 struct getdents_args {
4219 sys_getdents(td, uap)
4221 register struct getdents_args /* {
4227 struct getdirentries_args ap;
4231 ap.count = uap->count;
4233 return (sys_getdirentries(td, &ap));
4237 * Set the mode mask for creation of filesystem nodes.
4239 #ifndef _SYS_SYSPROTO_H_
4247 struct umask_args /* {
4251 register struct filedesc *fdp;
4253 FILEDESC_XLOCK(td->td_proc->p_fd);
4254 fdp = td->td_proc->p_fd;
4255 td->td_retval[0] = fdp->fd_cmask;
4256 fdp->fd_cmask = uap->newmask & ALLPERMS;
4257 FILEDESC_XUNLOCK(td->td_proc->p_fd);
4262 * Void all references to file by ripping underlying filesystem away from
4265 #ifndef _SYS_SYSPROTO_H_
4266 struct revoke_args {
4273 register struct revoke_args /* {
4279 struct nameidata nd;
4282 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
4284 if ((error = namei(&nd)) != 0)
4287 NDFREE(&nd, NDF_ONLY_PNBUF);
4288 if (vp->v_type != VCHR || vp->v_rdev == NULL) {
4293 error = mac_vnode_check_revoke(td->td_ucred, vp);
4297 error = VOP_GETATTR(vp, &vattr, td->td_ucred);
4300 if (td->td_ucred->cr_uid != vattr.va_uid) {
4301 error = priv_check(td, PRIV_VFS_ADMIN);
4306 VOP_REVOKE(vp, REVOKEALL);
4313 * Convert a user file descriptor to a kernel file entry and check that, if it
4314 * is a capability, the correct rights are present. A reference on the file
4315 * entry is held upon returning.
4318 getvnode(struct filedesc *fdp, int fd, cap_rights_t *rightsp, struct file **fpp)
4323 error = fget_unlocked(fdp, fd, rightsp, 0, &fp, NULL);
4328 * The file could be not of the vnode type, or it may be not
4329 * yet fully initialized, in which case the f_vnode pointer
4330 * may be set, but f_ops is still badfileops. E.g.,
4331 * devfs_open() transiently create such situation to
4332 * facilitate csw d_fdopen().
4334 * Dupfdopen() handling in kern_openat() installs the
4335 * half-baked file into the process descriptor table, allowing
4336 * other thread to dereference it. Guard against the race by
4339 if (fp->f_vnode == NULL || fp->f_ops == &badfileops) {
4340 fdrop(fp, curthread);
4349 * Get an (NFS) file handle.
4351 #ifndef _SYS_SYSPROTO_H_
4352 struct lgetfh_args {
4360 register struct lgetfh_args *uap;
4362 struct nameidata nd;
4364 register struct vnode *vp;
4367 error = priv_check(td, PRIV_VFS_GETFH);
4370 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
4375 NDFREE(&nd, NDF_ONLY_PNBUF);
4377 bzero(&fh, sizeof(fh));
4378 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
4379 error = VOP_VPTOFH(vp, &fh.fh_fid);
4382 error = copyout(&fh, uap->fhp, sizeof (fh));
4386 #ifndef _SYS_SYSPROTO_H_
4395 register struct getfh_args *uap;
4397 struct nameidata nd;
4399 register struct vnode *vp;
4402 error = priv_check(td, PRIV_VFS_GETFH);
4405 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
4410 NDFREE(&nd, NDF_ONLY_PNBUF);
4412 bzero(&fh, sizeof(fh));
4413 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
4414 error = VOP_VPTOFH(vp, &fh.fh_fid);
4417 error = copyout(&fh, uap->fhp, sizeof (fh));
4422 * syscall for the rpc.lockd to use to translate a NFS file handle into an
4425 * warning: do not remove the priv_check() call or this becomes one giant
4428 #ifndef _SYS_SYSPROTO_H_
4429 struct fhopen_args {
4430 const struct fhandle *u_fhp;
4437 struct fhopen_args /* {
4438 const struct fhandle *u_fhp;
4449 error = priv_check(td, PRIV_VFS_FHOPEN);
4453 fmode = FFLAGS(uap->flags);
4454 /* why not allow a non-read/write open for our lockd? */
4455 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
4457 error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
4460 /* find the mount point */
4461 mp = vfs_busyfs(&fhp.fh_fsid);
4464 /* now give me my vnode, it gets returned to me locked */
4465 error = VFS_FHTOVP(mp, &fhp.fh_fid, LK_EXCLUSIVE, &vp);
4470 error = falloc_noinstall(td, &fp);
4476 * An extra reference on `fp' has been held for us by
4477 * falloc_noinstall().
4483 error = vn_open_vnode(vp, fmode, td->td_ucred, td, fp);
4485 KASSERT(fp->f_ops == &badfileops,
4486 ("VOP_OPEN in fhopen() set f_ops"));
4487 KASSERT(td->td_dupfd < 0,
4488 ("fhopen() encountered fdopen()"));
4498 finit(fp, (fmode & FMASK) | (fp->f_flag & FHASLOCK), DTYPE_VNODE, vp,
4501 if ((fmode & O_TRUNC) != 0) {
4502 error = fo_truncate(fp, 0, td->td_ucred, td);
4507 error = finstall(td, fp, &indx, fmode, NULL);
4510 td->td_retval[0] = indx;
4515 * Stat an (NFS) file handle.
4517 #ifndef _SYS_SYSPROTO_H_
4518 struct fhstat_args {
4519 struct fhandle *u_fhp;
4526 register struct fhstat_args /* {
4527 struct fhandle *u_fhp;
4535 error = copyin(uap->u_fhp, &fh, sizeof(fh));
4538 error = kern_fhstat(td, fh, &sb);
4540 error = copyout(&sb, uap->sb, sizeof(sb));
4545 kern_fhstat(struct thread *td, struct fhandle fh, struct stat *sb)
4551 error = priv_check(td, PRIV_VFS_FHSTAT);
4554 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4556 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_EXCLUSIVE, &vp);
4560 error = vn_stat(vp, sb, td->td_ucred, NOCRED, td);
4566 * Implement fstatfs() for (NFS) file handles.
4568 #ifndef _SYS_SYSPROTO_H_
4569 struct fhstatfs_args {
4570 struct fhandle *u_fhp;
4575 sys_fhstatfs(td, uap)
4577 struct fhstatfs_args /* {
4578 struct fhandle *u_fhp;
4586 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4589 error = kern_fhstatfs(td, fh, &sf);
4592 return (copyout(&sf, uap->buf, sizeof(sf)));
4596 kern_fhstatfs(struct thread *td, fhandle_t fh, struct statfs *buf)
4603 error = priv_check(td, PRIV_VFS_FHSTATFS);
4606 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4608 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_EXCLUSIVE, &vp);
4614 error = prison_canseemount(td->td_ucred, mp);
4618 error = mac_mount_check_stat(td->td_ucred, mp);
4623 * Set these in case the underlying filesystem fails to do so.
4626 sp->f_version = STATFS_VERSION;
4627 sp->f_namemax = NAME_MAX;
4628 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
4629 error = VFS_STATFS(mp, sp);
4638 kern_posix_fallocate(struct thread *td, int fd, off_t offset, off_t len)
4643 cap_rights_t rights;
4644 off_t olen, ooffset;
4647 if (offset < 0 || len <= 0)
4649 /* Check for wrap. */
4650 if (offset > OFF_MAX - len)
4652 error = fget(td, fd, cap_rights_init(&rights, CAP_WRITE), &fp);
4655 if ((fp->f_ops->fo_flags & DFLAG_SEEKABLE) == 0) {
4659 if ((fp->f_flag & FWRITE) == 0) {
4663 if (fp->f_type != DTYPE_VNODE) {
4668 if (vp->v_type != VREG) {
4673 /* Allocating blocks may take a long time, so iterate. */
4680 error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
4683 error = vn_lock(vp, LK_EXCLUSIVE);
4685 vn_finished_write(mp);
4689 error = mac_vnode_check_write(td->td_ucred, fp->f_cred, vp);
4692 error = VOP_ALLOCATE(vp, &offset, &len);
4694 vn_finished_write(mp);
4696 if (olen + ooffset != offset + len) {
4697 panic("offset + len changed from %jx/%jx to %jx/%jx",
4698 ooffset, olen, offset, len);
4700 if (error != 0 || len == 0)
4702 KASSERT(olen > len, ("Iteration did not make progress?"));
4711 sys_posix_fallocate(struct thread *td, struct posix_fallocate_args *uap)
4714 td->td_retval[0] = kern_posix_fallocate(td, uap->fd, uap->offset,
4720 * Unlike madvise(2), we do not make a best effort to remember every
4721 * possible caching hint. Instead, we remember the last setting with
4722 * the exception that we will allow POSIX_FADV_NORMAL to adjust the
4723 * region of any current setting.
4726 kern_posix_fadvise(struct thread *td, int fd, off_t offset, off_t len,
4729 struct fadvise_info *fa, *new;
4732 cap_rights_t rights;
4736 if (offset < 0 || len < 0 || offset > OFF_MAX - len)
4739 case POSIX_FADV_SEQUENTIAL:
4740 case POSIX_FADV_RANDOM:
4741 case POSIX_FADV_NOREUSE:
4742 new = malloc(sizeof(*fa), M_FADVISE, M_WAITOK);
4744 case POSIX_FADV_NORMAL:
4745 case POSIX_FADV_WILLNEED:
4746 case POSIX_FADV_DONTNEED:
4752 /* XXX: CAP_POSIX_FADVISE? */
4753 error = fget(td, fd, cap_rights_init(&rights), &fp);
4756 if ((fp->f_ops->fo_flags & DFLAG_SEEKABLE) == 0) {
4760 if (fp->f_type != DTYPE_VNODE) {
4765 if (vp->v_type != VREG) {
4772 end = offset + len - 1;
4774 case POSIX_FADV_SEQUENTIAL:
4775 case POSIX_FADV_RANDOM:
4776 case POSIX_FADV_NOREUSE:
4778 * Try to merge any existing non-standard region with
4779 * this new region if possible, otherwise create a new
4780 * non-standard region for this request.
4782 mtx_pool_lock(mtxpool_sleep, fp);
4784 if (fa != NULL && fa->fa_advice == advice &&
4785 ((fa->fa_start <= end && fa->fa_end >= offset) ||
4786 (end != OFF_MAX && fa->fa_start == end + 1) ||
4787 (fa->fa_end != OFF_MAX && fa->fa_end + 1 == offset))) {
4788 if (offset < fa->fa_start)
4789 fa->fa_start = offset;
4790 if (end > fa->fa_end)
4793 new->fa_advice = advice;
4794 new->fa_start = offset;
4796 new->fa_prevstart = 0;
4797 new->fa_prevend = 0;
4801 mtx_pool_unlock(mtxpool_sleep, fp);
4803 case POSIX_FADV_NORMAL:
4805 * If a the "normal" region overlaps with an existing
4806 * non-standard region, trim or remove the
4807 * non-standard region.
4809 mtx_pool_lock(mtxpool_sleep, fp);
4812 if (offset <= fa->fa_start && end >= fa->fa_end) {
4814 fp->f_advice = NULL;
4815 } else if (offset <= fa->fa_start &&
4816 end >= fa->fa_start)
4817 fa->fa_start = end + 1;
4818 else if (offset <= fa->fa_end && end >= fa->fa_end)
4819 fa->fa_end = offset - 1;
4820 else if (offset >= fa->fa_start && end <= fa->fa_end) {
4822 * If the "normal" region is a middle
4823 * portion of the existing
4824 * non-standard region, just remove
4825 * the whole thing rather than picking
4826 * one side or the other to
4830 fp->f_advice = NULL;
4833 mtx_pool_unlock(mtxpool_sleep, fp);
4835 case POSIX_FADV_WILLNEED:
4836 case POSIX_FADV_DONTNEED:
4837 error = VOP_ADVISE(vp, offset, end, advice);
4843 free(new, M_FADVISE);
4848 sys_posix_fadvise(struct thread *td, struct posix_fadvise_args *uap)
4851 td->td_retval[0] = kern_posix_fadvise(td, uap->fd, uap->offset,
4852 uap->len, uap->advice);