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)
262 struct statfs *sp, sb;
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);
286 if (priv_check(td, PRIV_VFS_GENERATION)) {
287 bcopy(sp, &sb, sizeof(sb));
288 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
289 prison_enforce_statfs(td->td_ucred, mp, &sb);
299 * Get filesystem statistics.
301 #ifndef _SYS_SYSPROTO_H_
310 register struct statfs_args /* {
318 error = kern_statfs(td, uap->path, UIO_USERSPACE, &sf);
320 error = copyout(&sf, uap->buf, sizeof(sf));
325 kern_statfs(struct thread *td, char *path, enum uio_seg pathseg,
332 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
337 mp = nd.ni_vp->v_mount;
339 NDFREE(&nd, NDF_ONLY_PNBUF);
341 return (kern_do_statfs(td, mp, buf));
345 * Get filesystem statistics.
347 #ifndef _SYS_SYSPROTO_H_
348 struct fstatfs_args {
356 register struct fstatfs_args /* {
364 error = kern_fstatfs(td, uap->fd, &sf);
366 error = copyout(&sf, uap->buf, sizeof(sf));
371 kern_fstatfs(struct thread *td, int fd, struct statfs *buf)
380 error = getvnode(td->td_proc->p_fd, fd,
381 cap_rights_init(&rights, CAP_FSTATFS), &fp);
385 vn_lock(vp, LK_SHARED | LK_RETRY);
387 AUDIT_ARG_VNODE1(vp);
394 return (kern_do_statfs(td, mp, buf));
398 * Get statistics on all filesystems.
400 #ifndef _SYS_SYSPROTO_H_
401 struct getfsstat_args {
408 sys_getfsstat(td, uap)
410 register struct getfsstat_args /* {
417 return (kern_getfsstat(td, &uap->buf, uap->bufsize, UIO_USERSPACE,
422 * If (bufsize > 0 && bufseg == UIO_SYSSPACE)
423 * The caller is responsible for freeing memory which will be allocated
427 kern_getfsstat(struct thread *td, struct statfs **buf, size_t bufsize,
428 enum uio_seg bufseg, int flags)
430 struct mount *mp, *nmp;
431 struct statfs *sfsp, *sp, sb;
432 size_t count, maxcount;
435 maxcount = bufsize / sizeof(struct statfs);
438 else if (bufseg == UIO_USERSPACE)
440 else /* if (bufseg == UIO_SYSSPACE) */ {
442 mtx_lock(&mountlist_mtx);
443 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
446 mtx_unlock(&mountlist_mtx);
447 if (maxcount > count)
449 sfsp = *buf = malloc(maxcount * sizeof(struct statfs), M_TEMP,
453 mtx_lock(&mountlist_mtx);
454 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
455 if (prison_canseemount(td->td_ucred, mp) != 0) {
456 nmp = TAILQ_NEXT(mp, mnt_list);
460 if (mac_mount_check_stat(td->td_ucred, mp) != 0) {
461 nmp = TAILQ_NEXT(mp, mnt_list);
465 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK)) {
466 nmp = TAILQ_NEXT(mp, mnt_list);
469 if (sfsp && count < maxcount) {
472 * Set these in case the underlying filesystem
475 sp->f_version = STATFS_VERSION;
476 sp->f_namemax = NAME_MAX;
477 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
479 * If MNT_NOWAIT or MNT_LAZY is specified, do not
480 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
481 * overrides MNT_WAIT.
483 if (((flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
484 (flags & MNT_WAIT)) &&
485 (error = VFS_STATFS(mp, sp))) {
486 mtx_lock(&mountlist_mtx);
487 nmp = TAILQ_NEXT(mp, mnt_list);
491 if (priv_check(td, PRIV_VFS_GENERATION)) {
492 bcopy(sp, &sb, sizeof(sb));
493 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
494 prison_enforce_statfs(td->td_ucred, mp, &sb);
497 if (bufseg == UIO_SYSSPACE)
498 bcopy(sp, sfsp, sizeof(*sp));
499 else /* if (bufseg == UIO_USERSPACE) */ {
500 error = copyout(sp, sfsp, sizeof(*sp));
509 mtx_lock(&mountlist_mtx);
510 nmp = TAILQ_NEXT(mp, mnt_list);
513 mtx_unlock(&mountlist_mtx);
514 if (sfsp && count > maxcount)
515 td->td_retval[0] = maxcount;
517 td->td_retval[0] = count;
521 #ifdef COMPAT_FREEBSD4
523 * Get old format filesystem statistics.
525 static void cvtstatfs(struct statfs *, struct ostatfs *);
527 #ifndef _SYS_SYSPROTO_H_
528 struct freebsd4_statfs_args {
534 freebsd4_statfs(td, uap)
536 struct freebsd4_statfs_args /* {
545 error = kern_statfs(td, uap->path, UIO_USERSPACE, &sf);
548 cvtstatfs(&sf, &osb);
549 return (copyout(&osb, uap->buf, sizeof(osb)));
553 * Get filesystem statistics.
555 #ifndef _SYS_SYSPROTO_H_
556 struct freebsd4_fstatfs_args {
562 freebsd4_fstatfs(td, uap)
564 struct freebsd4_fstatfs_args /* {
573 error = kern_fstatfs(td, uap->fd, &sf);
576 cvtstatfs(&sf, &osb);
577 return (copyout(&osb, uap->buf, sizeof(osb)));
581 * Get statistics on all filesystems.
583 #ifndef _SYS_SYSPROTO_H_
584 struct freebsd4_getfsstat_args {
591 freebsd4_getfsstat(td, uap)
593 register struct freebsd4_getfsstat_args /* {
599 struct statfs *buf, *sp;
604 count = uap->bufsize / sizeof(struct ostatfs);
605 size = count * sizeof(struct statfs);
606 error = kern_getfsstat(td, &buf, size, UIO_SYSSPACE, uap->flags);
608 count = td->td_retval[0];
610 while (count > 0 && error == 0) {
612 error = copyout(&osb, uap->buf, sizeof(osb));
623 * Implement fstatfs() for (NFS) file handles.
625 #ifndef _SYS_SYSPROTO_H_
626 struct freebsd4_fhstatfs_args {
627 struct fhandle *u_fhp;
632 freebsd4_fhstatfs(td, uap)
634 struct freebsd4_fhstatfs_args /* {
635 struct fhandle *u_fhp;
644 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
647 error = kern_fhstatfs(td, fh, &sf);
650 cvtstatfs(&sf, &osb);
651 return (copyout(&osb, uap->buf, sizeof(osb)));
655 * Convert a new format statfs structure to an old format statfs structure.
663 statfs_scale_blocks(nsp, LONG_MAX);
664 bzero(osp, sizeof(*osp));
665 osp->f_bsize = nsp->f_bsize;
666 osp->f_iosize = MIN(nsp->f_iosize, LONG_MAX);
667 osp->f_blocks = nsp->f_blocks;
668 osp->f_bfree = nsp->f_bfree;
669 osp->f_bavail = nsp->f_bavail;
670 osp->f_files = MIN(nsp->f_files, LONG_MAX);
671 osp->f_ffree = MIN(nsp->f_ffree, LONG_MAX);
672 osp->f_owner = nsp->f_owner;
673 osp->f_type = nsp->f_type;
674 osp->f_flags = nsp->f_flags;
675 osp->f_syncwrites = MIN(nsp->f_syncwrites, LONG_MAX);
676 osp->f_asyncwrites = MIN(nsp->f_asyncwrites, LONG_MAX);
677 osp->f_syncreads = MIN(nsp->f_syncreads, LONG_MAX);
678 osp->f_asyncreads = MIN(nsp->f_asyncreads, LONG_MAX);
679 strlcpy(osp->f_fstypename, nsp->f_fstypename,
680 MIN(MFSNAMELEN, OMFSNAMELEN));
681 strlcpy(osp->f_mntonname, nsp->f_mntonname,
682 MIN(MNAMELEN, OMNAMELEN));
683 strlcpy(osp->f_mntfromname, nsp->f_mntfromname,
684 MIN(MNAMELEN, OMNAMELEN));
685 osp->f_fsid = nsp->f_fsid;
687 #endif /* COMPAT_FREEBSD4 */
690 * Change current working directory to a given file descriptor.
692 #ifndef _SYS_SYSPROTO_H_
700 struct fchdir_args /* {
704 register struct filedesc *fdp = td->td_proc->p_fd;
705 struct vnode *vp, *tdp, *vpold;
711 AUDIT_ARG_FD(uap->fd);
712 error = getvnode(fdp, uap->fd, cap_rights_init(&rights, CAP_FCHDIR),
719 vn_lock(vp, LK_SHARED | LK_RETRY);
720 AUDIT_ARG_VNODE1(vp);
721 error = change_dir(vp, td);
722 while (!error && (mp = vp->v_mountedhere) != NULL) {
725 error = VFS_ROOT(mp, LK_SHARED, &tdp);
738 vpold = fdp->fd_cdir;
740 FILEDESC_XUNLOCK(fdp);
746 * Change current working directory (``.'').
748 #ifndef _SYS_SYSPROTO_H_
756 struct chdir_args /* {
761 return (kern_chdir(td, uap->path, UIO_USERSPACE));
765 kern_chdir(struct thread *td, char *path, enum uio_seg pathseg)
767 register struct filedesc *fdp = td->td_proc->p_fd;
772 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
774 if ((error = namei(&nd)) != 0)
776 if ((error = change_dir(nd.ni_vp, td)) != 0) {
778 NDFREE(&nd, NDF_ONLY_PNBUF);
781 VOP_UNLOCK(nd.ni_vp, 0);
782 NDFREE(&nd, NDF_ONLY_PNBUF);
785 fdp->fd_cdir = nd.ni_vp;
786 FILEDESC_XUNLOCK(fdp);
792 * Helper function for raised chroot(2) security function: Refuse if
793 * any filedescriptors are open directories.
796 chroot_refuse_vdir_fds(fdp)
797 struct filedesc *fdp;
803 FILEDESC_LOCK_ASSERT(fdp);
805 for (fd = 0; fd <= fdp->fd_lastfile; fd++) {
806 fp = fget_locked(fdp, fd);
809 if (fp->f_type == DTYPE_VNODE) {
811 if (vp->v_type == VDIR)
819 * This sysctl determines if we will allow a process to chroot(2) if it
820 * has a directory open:
821 * 0: disallowed for all processes.
822 * 1: allowed for processes that were not already chroot(2)'ed.
823 * 2: allowed for all processes.
826 static int chroot_allow_open_directories = 1;
828 SYSCTL_INT(_kern, OID_AUTO, chroot_allow_open_directories, CTLFLAG_RW,
829 &chroot_allow_open_directories, 0,
830 "Allow a process to chroot(2) if it has a directory open");
833 * Change notion of root (``/'') directory.
835 #ifndef _SYS_SYSPROTO_H_
843 struct chroot_args /* {
850 error = priv_check(td, PRIV_VFS_CHROOT);
853 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
854 UIO_USERSPACE, uap->path, td);
858 error = change_dir(nd.ni_vp, td);
862 error = mac_vnode_check_chroot(td->td_ucred, nd.ni_vp);
866 VOP_UNLOCK(nd.ni_vp, 0);
867 error = change_root(nd.ni_vp, td);
869 NDFREE(&nd, NDF_ONLY_PNBUF);
874 NDFREE(&nd, NDF_ONLY_PNBUF);
879 * Common routine for chroot and chdir. Callers must provide a locked vnode
891 ASSERT_VOP_LOCKED(vp, "change_dir(): vp not locked");
892 if (vp->v_type != VDIR)
895 error = mac_vnode_check_chdir(td->td_ucred, vp);
899 return (VOP_ACCESS(vp, VEXEC, td->td_ucred, td));
903 * Common routine for kern_chroot() and jail_attach(). The caller is
904 * responsible for invoking priv_check() and mac_vnode_check_chroot() to
905 * authorize this operation.
912 struct filedesc *fdp;
916 fdp = td->td_proc->p_fd;
918 if (chroot_allow_open_directories == 0 ||
919 (chroot_allow_open_directories == 1 && fdp->fd_rdir != rootvnode)) {
920 error = chroot_refuse_vdir_fds(fdp);
922 FILEDESC_XUNLOCK(fdp);
926 oldvp = fdp->fd_rdir;
933 FILEDESC_XUNLOCK(fdp);
939 flags_to_rights(int flags, cap_rights_t *rightsp)
942 if (flags & O_EXEC) {
943 cap_rights_set(rightsp, CAP_FEXECVE);
945 switch ((flags & O_ACCMODE)) {
947 cap_rights_set(rightsp, CAP_READ);
950 cap_rights_set(rightsp, CAP_READ);
953 cap_rights_set(rightsp, CAP_WRITE);
954 if (!(flags & (O_APPEND | O_TRUNC)))
955 cap_rights_set(rightsp, CAP_SEEK);
961 cap_rights_set(rightsp, CAP_CREATE);
964 cap_rights_set(rightsp, CAP_FTRUNCATE);
966 if (flags & (O_SYNC | O_FSYNC))
967 cap_rights_set(rightsp, CAP_FSYNC);
969 if (flags & (O_EXLOCK | O_SHLOCK))
970 cap_rights_set(rightsp, CAP_FLOCK);
974 * Check permissions, allocate an open file structure, and call the device
975 * open routine if any.
977 #ifndef _SYS_SYSPROTO_H_
987 register struct open_args /* {
994 return (kern_open(td, uap->path, UIO_USERSPACE, uap->flags, uap->mode));
997 #ifndef _SYS_SYSPROTO_H_
1006 sys_openat(struct thread *td, struct openat_args *uap)
1009 return (kern_openat(td, uap->fd, uap->path, UIO_USERSPACE, uap->flag,
1014 kern_open(struct thread *td, char *path, enum uio_seg pathseg, int flags,
1018 return (kern_openat(td, AT_FDCWD, path, pathseg, flags, mode));
1022 kern_openat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1023 int flags, int mode)
1025 struct proc *p = td->td_proc;
1026 struct filedesc *fdp = p->p_fd;
1029 struct nameidata nd;
1030 cap_rights_t rights;
1031 int cmode, error, indx;
1035 AUDIT_ARG_FFLAGS(flags);
1036 AUDIT_ARG_MODE(mode);
1037 /* XXX: audit dirfd */
1038 cap_rights_init(&rights, CAP_LOOKUP);
1039 flags_to_rights(flags, &rights);
1041 * Only one of the O_EXEC, O_RDONLY, O_WRONLY and O_RDWR flags
1044 if (flags & O_EXEC) {
1045 if (flags & O_ACCMODE)
1047 } else if ((flags & O_ACCMODE) == O_ACCMODE) {
1050 flags = FFLAGS(flags);
1054 * Allocate the file descriptor, but don't install a descriptor yet.
1056 error = falloc_noinstall(td, &fp);
1060 * An extra reference on `fp' has been held for us by
1061 * falloc_noinstall().
1063 /* Set the flags early so the finit in devfs can pick them up. */
1064 fp->f_flag = flags & FMASK;
1065 cmode = ((mode & ~fdp->fd_cmask) & ALLPERMS) & ~S_ISTXT;
1066 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, fd,
1068 td->td_dupfd = -1; /* XXX check for fdopen */
1069 error = vn_open(&nd, &flags, cmode, fp);
1072 * If the vn_open replaced the method vector, something
1073 * wonderous happened deep below and we just pass it up
1074 * pretending we know what we do.
1076 if (error == ENXIO && fp->f_ops != &badfileops)
1080 * Handle special fdopen() case. bleh.
1082 * Don't do this for relative (capability) lookups; we don't
1083 * understand exactly what would happen, and we don't think
1084 * that it ever should.
1086 if (nd.ni_strictrelative == 0 &&
1087 (error == ENODEV || error == ENXIO) &&
1088 td->td_dupfd >= 0) {
1089 error = dupfdopen(td, fdp, td->td_dupfd, flags, error,
1098 NDFREE(&nd, NDF_ONLY_PNBUF);
1102 * Store the vnode, for any f_type. Typically, the vnode use
1103 * count is decremented by direct call to vn_closefile() for
1104 * files that switched type in the cdevsw fdopen() method.
1108 * If the file wasn't claimed by devfs bind it to the normal
1109 * vnode operations here.
1111 if (fp->f_ops == &badfileops) {
1112 KASSERT(vp->v_type != VFIFO, ("Unexpected fifo."));
1114 finit(fp, (flags & FMASK) | (fp->f_flag & FHASLOCK),
1115 DTYPE_VNODE, vp, &vnops);
1119 if (flags & O_TRUNC) {
1120 error = fo_truncate(fp, 0, td->td_ucred, td);
1126 * If we haven't already installed the FD (for dupfdopen), do so now.
1129 struct filecaps *fcaps;
1132 if (nd.ni_strictrelative == 1)
1133 fcaps = &nd.ni_filecaps;
1137 error = finstall(td, fp, &indx, flags, fcaps);
1138 /* On success finstall() consumes fcaps. */
1140 filecaps_free(&nd.ni_filecaps);
1144 filecaps_free(&nd.ni_filecaps);
1148 * Release our private reference, leaving the one associated with
1149 * the descriptor table intact.
1152 td->td_retval[0] = indx;
1155 KASSERT(indx == -1, ("indx=%d, should be -1", indx));
1164 #ifndef _SYS_SYSPROTO_H_
1165 struct ocreat_args {
1173 register struct ocreat_args /* {
1179 return (kern_open(td, uap->path, UIO_USERSPACE,
1180 O_WRONLY | O_CREAT | O_TRUNC, uap->mode));
1182 #endif /* COMPAT_43 */
1185 * Create a special file.
1187 #ifndef _SYS_SYSPROTO_H_
1197 register struct mknod_args /* {
1204 return (kern_mknod(td, uap->path, UIO_USERSPACE, uap->mode, uap->dev));
1207 #ifndef _SYS_SYSPROTO_H_
1208 struct mknodat_args {
1216 sys_mknodat(struct thread *td, struct mknodat_args *uap)
1219 return (kern_mknodat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode,
1224 kern_mknod(struct thread *td, char *path, enum uio_seg pathseg, int mode,
1228 return (kern_mknodat(td, AT_FDCWD, path, pathseg, mode, dev));
1232 kern_mknodat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1238 struct nameidata nd;
1239 cap_rights_t rights;
1240 int error, whiteout = 0;
1242 AUDIT_ARG_MODE(mode);
1244 switch (mode & S_IFMT) {
1247 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
1248 if (error == 0 && dev == VNOVAL)
1252 error = priv_check(td, PRIV_VFS_MKNOD_BAD);
1255 error = priv_check(td, PRIV_VFS_MKNOD_WHT);
1259 return (kern_mkfifoat(td, fd, path, pathseg, mode));
1269 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
1270 NOCACHE, pathseg, path, fd, cap_rights_init(&rights, CAP_MKNODAT),
1272 if ((error = namei(&nd)) != 0)
1276 NDFREE(&nd, NDF_ONLY_PNBUF);
1277 if (vp == nd.ni_dvp)
1285 vattr.va_mode = (mode & ALLPERMS) &
1286 ~td->td_proc->p_fd->fd_cmask;
1287 vattr.va_rdev = dev;
1290 switch (mode & S_IFMT) {
1291 case S_IFMT: /* used by badsect to flag bad sectors */
1292 vattr.va_type = VBAD;
1295 vattr.va_type = VCHR;
1298 vattr.va_type = VBLK;
1304 panic("kern_mknod: invalid mode");
1307 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1308 NDFREE(&nd, NDF_ONLY_PNBUF);
1310 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1315 if (error == 0 && !whiteout)
1316 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp,
1317 &nd.ni_cnd, &vattr);
1321 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, CREATE);
1323 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp,
1324 &nd.ni_cnd, &vattr);
1329 NDFREE(&nd, NDF_ONLY_PNBUF);
1331 vn_finished_write(mp);
1336 * Create a named pipe.
1338 #ifndef _SYS_SYSPROTO_H_
1339 struct mkfifo_args {
1347 register struct mkfifo_args /* {
1353 return (kern_mkfifo(td, uap->path, UIO_USERSPACE, uap->mode));
1356 #ifndef _SYS_SYSPROTO_H_
1357 struct mkfifoat_args {
1364 sys_mkfifoat(struct thread *td, struct mkfifoat_args *uap)
1367 return (kern_mkfifoat(td, uap->fd, uap->path, UIO_USERSPACE,
1372 kern_mkfifo(struct thread *td, char *path, enum uio_seg pathseg, int mode)
1375 return (kern_mkfifoat(td, AT_FDCWD, path, pathseg, mode));
1379 kern_mkfifoat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1384 struct nameidata nd;
1385 cap_rights_t rights;
1388 AUDIT_ARG_MODE(mode);
1391 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
1392 NOCACHE, pathseg, path, fd, cap_rights_init(&rights, CAP_MKFIFOAT),
1394 if ((error = namei(&nd)) != 0)
1396 if (nd.ni_vp != NULL) {
1397 NDFREE(&nd, NDF_ONLY_PNBUF);
1398 if (nd.ni_vp == nd.ni_dvp)
1405 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1406 NDFREE(&nd, NDF_ONLY_PNBUF);
1408 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1413 vattr.va_type = VFIFO;
1414 vattr.va_mode = (mode & ALLPERMS) & ~td->td_proc->p_fd->fd_cmask;
1416 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1421 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
1428 vn_finished_write(mp);
1429 NDFREE(&nd, NDF_ONLY_PNBUF);
1434 * Make a hard file link.
1436 #ifndef _SYS_SYSPROTO_H_
1445 register struct link_args /* {
1451 return (kern_link(td, uap->path, uap->link, UIO_USERSPACE));
1454 #ifndef _SYS_SYSPROTO_H_
1455 struct linkat_args {
1464 sys_linkat(struct thread *td, struct linkat_args *uap)
1469 if (flag & ~AT_SYMLINK_FOLLOW)
1472 return (kern_linkat(td, uap->fd1, uap->fd2, uap->path1, uap->path2,
1473 UIO_USERSPACE, (flag & AT_SYMLINK_FOLLOW) ? FOLLOW : NOFOLLOW));
1476 int hardlink_check_uid = 0;
1477 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
1478 &hardlink_check_uid, 0,
1479 "Unprivileged processes cannot create hard links to files owned by other "
1481 static int hardlink_check_gid = 0;
1482 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
1483 &hardlink_check_gid, 0,
1484 "Unprivileged processes cannot create hard links to files owned by other "
1488 can_hardlink(struct vnode *vp, struct ucred *cred)
1493 if (!hardlink_check_uid && !hardlink_check_gid)
1496 error = VOP_GETATTR(vp, &va, cred);
1500 if (hardlink_check_uid && cred->cr_uid != va.va_uid) {
1501 error = priv_check_cred(cred, PRIV_VFS_LINK, 0);
1506 if (hardlink_check_gid && !groupmember(va.va_gid, cred)) {
1507 error = priv_check_cred(cred, PRIV_VFS_LINK, 0);
1516 kern_link(struct thread *td, char *path, char *link, enum uio_seg segflg)
1519 return (kern_linkat(td, AT_FDCWD, AT_FDCWD, path,link, segflg, FOLLOW));
1523 kern_linkat(struct thread *td, int fd1, int fd2, char *path1, char *path2,
1524 enum uio_seg segflg, int follow)
1528 struct nameidata nd;
1529 cap_rights_t rights;
1534 NDINIT_AT(&nd, LOOKUP, follow | AUDITVNODE1, segflg, path1, fd1, td);
1536 if ((error = namei(&nd)) != 0)
1538 NDFREE(&nd, NDF_ONLY_PNBUF);
1540 if (vp->v_type == VDIR) {
1542 return (EPERM); /* POSIX */
1544 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE2 |
1545 NOCACHE, segflg, path2, fd2, cap_rights_init(&rights, CAP_LINKAT),
1547 if ((error = namei(&nd)) == 0) {
1548 if (nd.ni_vp != NULL) {
1549 NDFREE(&nd, NDF_ONLY_PNBUF);
1550 if (nd.ni_dvp == nd.ni_vp)
1557 } else if (nd.ni_dvp->v_mount != vp->v_mount) {
1559 * Cross-device link. No need to recheck
1560 * vp->v_type, since it cannot change, except
1563 NDFREE(&nd, NDF_ONLY_PNBUF);
1567 } else if ((error = vn_lock(vp, LK_EXCLUSIVE)) == 0) {
1568 error = can_hardlink(vp, td->td_ucred);
1571 error = mac_vnode_check_link(td->td_ucred,
1572 nd.ni_dvp, vp, &nd.ni_cnd);
1577 NDFREE(&nd, NDF_ONLY_PNBUF);
1580 error = vn_start_write(vp, &mp, V_NOWAIT);
1584 NDFREE(&nd, NDF_ONLY_PNBUF);
1585 error = vn_start_write(NULL, &mp,
1591 error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
1594 vn_finished_write(mp);
1595 NDFREE(&nd, NDF_ONLY_PNBUF);
1598 NDFREE(&nd, NDF_ONLY_PNBUF);
1608 * Make a symbolic link.
1610 #ifndef _SYS_SYSPROTO_H_
1611 struct symlink_args {
1617 sys_symlink(td, uap)
1619 register struct symlink_args /* {
1625 return (kern_symlink(td, uap->path, uap->link, UIO_USERSPACE));
1628 #ifndef _SYS_SYSPROTO_H_
1629 struct symlinkat_args {
1636 sys_symlinkat(struct thread *td, struct symlinkat_args *uap)
1639 return (kern_symlinkat(td, uap->path1, uap->fd, uap->path2,
1644 kern_symlink(struct thread *td, char *path, char *link, enum uio_seg segflg)
1647 return (kern_symlinkat(td, path, AT_FDCWD, link, segflg));
1651 kern_symlinkat(struct thread *td, char *path1, int fd, char *path2,
1652 enum uio_seg segflg)
1657 struct nameidata nd;
1659 cap_rights_t rights;
1661 if (segflg == UIO_SYSSPACE) {
1664 syspath = uma_zalloc(namei_zone, M_WAITOK);
1665 if ((error = copyinstr(path1, syspath, MAXPATHLEN, NULL)) != 0)
1668 AUDIT_ARG_TEXT(syspath);
1671 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
1672 NOCACHE, segflg, path2, fd, cap_rights_init(&rights, CAP_SYMLINKAT),
1674 if ((error = namei(&nd)) != 0)
1677 NDFREE(&nd, NDF_ONLY_PNBUF);
1678 if (nd.ni_vp == nd.ni_dvp)
1686 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1687 NDFREE(&nd, NDF_ONLY_PNBUF);
1689 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1694 vattr.va_mode = ACCESSPERMS &~ td->td_proc->p_fd->fd_cmask;
1696 vattr.va_type = VLNK;
1697 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1702 error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr, syspath);
1708 NDFREE(&nd, NDF_ONLY_PNBUF);
1710 vn_finished_write(mp);
1712 if (segflg != UIO_SYSSPACE)
1713 uma_zfree(namei_zone, syspath);
1718 * Delete a whiteout from the filesystem.
1721 sys_undelete(td, uap)
1723 register struct undelete_args /* {
1728 struct nameidata nd;
1733 NDINIT(&nd, DELETE, LOCKPARENT | DOWHITEOUT | AUDITVNODE1,
1734 UIO_USERSPACE, uap->path, td);
1739 if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & ISWHITEOUT)) {
1740 NDFREE(&nd, NDF_ONLY_PNBUF);
1741 if (nd.ni_vp == nd.ni_dvp)
1749 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1750 NDFREE(&nd, NDF_ONLY_PNBUF);
1752 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1756 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, DELETE);
1757 NDFREE(&nd, NDF_ONLY_PNBUF);
1759 vn_finished_write(mp);
1764 * Delete a name from the filesystem.
1766 #ifndef _SYS_SYSPROTO_H_
1767 struct unlink_args {
1774 struct unlink_args /* {
1779 return (kern_unlink(td, uap->path, UIO_USERSPACE));
1782 #ifndef _SYS_SYSPROTO_H_
1783 struct unlinkat_args {
1790 sys_unlinkat(struct thread *td, struct unlinkat_args *uap)
1792 int flag = uap->flag;
1794 char *path = uap->path;
1796 if (flag & ~AT_REMOVEDIR)
1799 if (flag & AT_REMOVEDIR)
1800 return (kern_rmdirat(td, fd, path, UIO_USERSPACE));
1802 return (kern_unlinkat(td, fd, path, UIO_USERSPACE, 0));
1806 kern_unlink(struct thread *td, char *path, enum uio_seg pathseg)
1809 return (kern_unlinkat(td, AT_FDCWD, path, pathseg, 0));
1813 kern_unlinkat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1818 struct nameidata nd;
1820 cap_rights_t rights;
1825 NDINIT_ATRIGHTS(&nd, DELETE, LOCKPARENT | LOCKLEAF | AUDITVNODE1,
1826 pathseg, path, fd, cap_rights_init(&rights, CAP_UNLINKAT), td);
1827 if ((error = namei(&nd)) != 0)
1828 return (error == EINVAL ? EPERM : error);
1830 if (vp->v_type == VDIR && oldinum == 0) {
1831 error = EPERM; /* POSIX */
1832 } else if (oldinum != 0 &&
1833 ((error = vn_stat(vp, &sb, td->td_ucred, NOCRED, td)) == 0) &&
1834 sb.st_ino != oldinum) {
1835 error = EIDRM; /* Identifier removed */
1838 * The root of a mounted filesystem cannot be deleted.
1840 * XXX: can this only be a VDIR case?
1842 if (vp->v_vflag & VV_ROOT)
1846 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1847 NDFREE(&nd, NDF_ONLY_PNBUF);
1849 if (vp == nd.ni_dvp)
1853 if ((error = vn_start_write(NULL, &mp,
1854 V_XSLEEP | PCATCH)) != 0)
1859 error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp,
1864 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_UNLINK);
1865 error = VOP_REMOVE(nd.ni_dvp, vp, &nd.ni_cnd);
1869 vn_finished_write(mp);
1871 NDFREE(&nd, NDF_ONLY_PNBUF);
1873 if (vp == nd.ni_dvp)
1881 * Reposition read/write file offset.
1883 #ifndef _SYS_SYSPROTO_H_
1894 register struct lseek_args /* {
1902 cap_rights_t rights;
1905 AUDIT_ARG_FD(uap->fd);
1906 error = fget(td, uap->fd, cap_rights_init(&rights, CAP_SEEK), &fp);
1909 error = (fp->f_ops->fo_flags & DFLAG_SEEKABLE) != 0 ?
1910 fo_seek(fp, uap->offset, uap->whence, td) : ESPIPE;
1915 #if defined(COMPAT_43)
1917 * Reposition read/write file offset.
1919 #ifndef _SYS_SYSPROTO_H_
1920 struct olseek_args {
1929 register struct olseek_args /* {
1935 struct lseek_args /* {
1943 nuap.offset = uap->offset;
1944 nuap.whence = uap->whence;
1945 return (sys_lseek(td, &nuap));
1947 #endif /* COMPAT_43 */
1949 /* Version with the 'pad' argument */
1951 freebsd6_lseek(td, uap)
1953 register struct freebsd6_lseek_args *uap;
1955 struct lseek_args ouap;
1958 ouap.offset = uap->offset;
1959 ouap.whence = uap->whence;
1960 return (sys_lseek(td, &ouap));
1964 * Check access permissions using passed credentials.
1967 vn_access(vp, user_flags, cred, td)
1976 /* Flags == 0 means only check for existence. */
1980 if (user_flags & R_OK)
1982 if (user_flags & W_OK)
1984 if (user_flags & X_OK)
1987 error = mac_vnode_check_access(cred, vp, accmode);
1991 if ((accmode & VWRITE) == 0 || (error = vn_writechk(vp)) == 0)
1992 error = VOP_ACCESS(vp, accmode, cred, td);
1998 * Check access permissions using "real" credentials.
2000 #ifndef _SYS_SYSPROTO_H_
2001 struct access_args {
2009 register struct access_args /* {
2015 return (kern_access(td, uap->path, UIO_USERSPACE, uap->amode));
2018 #ifndef _SYS_SYSPROTO_H_
2019 struct faccessat_args {
2027 sys_faccessat(struct thread *td, struct faccessat_args *uap)
2030 if (uap->flag & ~AT_EACCESS)
2032 return (kern_accessat(td, uap->fd, uap->path, UIO_USERSPACE, uap->flag,
2037 kern_access(struct thread *td, char *path, enum uio_seg pathseg, int amode)
2040 return (kern_accessat(td, AT_FDCWD, path, pathseg, 0, amode));
2044 kern_accessat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2045 int flag, int amode)
2047 struct ucred *cred, *tmpcred;
2049 struct nameidata nd;
2050 cap_rights_t rights;
2054 * Create and modify a temporary credential instead of one that
2055 * is potentially shared.
2057 if (!(flag & AT_EACCESS)) {
2058 cred = td->td_ucred;
2059 tmpcred = crdup(cred);
2060 tmpcred->cr_uid = cred->cr_ruid;
2061 tmpcred->cr_groups[0] = cred->cr_rgid;
2062 td->td_ucred = tmpcred;
2064 cred = tmpcred = td->td_ucred;
2065 AUDIT_ARG_VALUE(amode);
2066 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF |
2067 AUDITVNODE1, pathseg, path, fd, cap_rights_init(&rights, CAP_FSTAT),
2069 if ((error = namei(&nd)) != 0)
2073 error = vn_access(vp, amode, tmpcred, td);
2074 NDFREE(&nd, NDF_ONLY_PNBUF);
2077 if (!(flag & AT_EACCESS)) {
2078 td->td_ucred = cred;
2085 * Check access permissions using "effective" credentials.
2087 #ifndef _SYS_SYSPROTO_H_
2088 struct eaccess_args {
2094 sys_eaccess(td, uap)
2096 register struct eaccess_args /* {
2102 return (kern_eaccess(td, uap->path, UIO_USERSPACE, uap->amode));
2106 kern_eaccess(struct thread *td, char *path, enum uio_seg pathseg, int amode)
2109 return (kern_accessat(td, AT_FDCWD, path, pathseg, AT_EACCESS, amode));
2112 #if defined(COMPAT_43)
2114 * Get file status; this version follows links.
2116 #ifndef _SYS_SYSPROTO_H_
2125 register struct ostat_args /* {
2134 error = kern_stat(td, uap->path, UIO_USERSPACE, &sb);
2138 return (copyout(&osb, uap->ub, sizeof (osb)));
2142 * Get file status; this version does not follow links.
2144 #ifndef _SYS_SYSPROTO_H_
2145 struct olstat_args {
2153 register struct olstat_args /* {
2162 error = kern_lstat(td, uap->path, UIO_USERSPACE, &sb);
2166 return (copyout(&osb, uap->ub, sizeof (osb)));
2170 * Convert from an old to a new stat structure.
2178 bzero(ost, sizeof(*ost));
2179 ost->st_dev = st->st_dev;
2180 ost->st_ino = st->st_ino;
2181 ost->st_mode = st->st_mode;
2182 ost->st_nlink = st->st_nlink;
2183 ost->st_uid = st->st_uid;
2184 ost->st_gid = st->st_gid;
2185 ost->st_rdev = st->st_rdev;
2186 if (st->st_size < (quad_t)1 << 32)
2187 ost->st_size = st->st_size;
2190 ost->st_atim = st->st_atim;
2191 ost->st_mtim = st->st_mtim;
2192 ost->st_ctim = st->st_ctim;
2193 ost->st_blksize = st->st_blksize;
2194 ost->st_blocks = st->st_blocks;
2195 ost->st_flags = st->st_flags;
2196 ost->st_gen = st->st_gen;
2198 #endif /* COMPAT_43 */
2201 * Get file status; this version follows links.
2203 #ifndef _SYS_SYSPROTO_H_
2212 register struct stat_args /* {
2220 error = kern_stat(td, uap->path, UIO_USERSPACE, &sb);
2222 error = copyout(&sb, uap->ub, sizeof (sb));
2226 #ifndef _SYS_SYSPROTO_H_
2227 struct fstatat_args {
2235 sys_fstatat(struct thread *td, struct fstatat_args *uap)
2240 error = kern_statat(td, uap->flag, uap->fd, uap->path,
2241 UIO_USERSPACE, &sb);
2243 error = copyout(&sb, uap->buf, sizeof (sb));
2248 kern_stat(struct thread *td, char *path, enum uio_seg pathseg, struct stat *sbp)
2251 return (kern_statat(td, 0, AT_FDCWD, path, pathseg, sbp));
2255 kern_statat(struct thread *td, int flag, int fd, char *path,
2256 enum uio_seg pathseg, struct stat *sbp)
2259 return (kern_statat_vnhook(td, flag, fd, path, pathseg, sbp, NULL));
2263 kern_statat_vnhook(struct thread *td, int flag, int fd, char *path,
2264 enum uio_seg pathseg, struct stat *sbp,
2265 void (*hook)(struct vnode *vp, struct stat *sbp))
2267 struct nameidata nd;
2269 cap_rights_t rights;
2272 if (flag & ~AT_SYMLINK_NOFOLLOW)
2275 NDINIT_ATRIGHTS(&nd, LOOKUP, ((flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW :
2276 FOLLOW) | LOCKSHARED | LOCKLEAF | AUDITVNODE1, pathseg, path, fd,
2277 cap_rights_init(&rights, CAP_FSTAT), td);
2279 if ((error = namei(&nd)) != 0)
2281 error = vn_stat(nd.ni_vp, &sb, td->td_ucred, NOCRED, td);
2283 SDT_PROBE2(vfs, , stat, mode, path, sb.st_mode);
2284 if (S_ISREG(sb.st_mode))
2285 SDT_PROBE2(vfs, , stat, reg, path, pathseg);
2286 if (__predict_false(hook != NULL))
2287 hook(nd.ni_vp, &sb);
2289 NDFREE(&nd, NDF_ONLY_PNBUF);
2295 if (KTRPOINT(td, KTR_STRUCT))
2302 * Get file status; this version does not follow links.
2304 #ifndef _SYS_SYSPROTO_H_
2313 register struct lstat_args /* {
2321 error = kern_lstat(td, uap->path, UIO_USERSPACE, &sb);
2323 error = copyout(&sb, uap->ub, sizeof (sb));
2328 kern_lstat(struct thread *td, char *path, enum uio_seg pathseg, struct stat *sbp)
2331 return (kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, path, pathseg,
2336 * Implementation of the NetBSD [l]stat() functions.
2344 bzero(nsb, sizeof *nsb);
2345 nsb->st_dev = sb->st_dev;
2346 nsb->st_ino = sb->st_ino;
2347 nsb->st_mode = sb->st_mode;
2348 nsb->st_nlink = sb->st_nlink;
2349 nsb->st_uid = sb->st_uid;
2350 nsb->st_gid = sb->st_gid;
2351 nsb->st_rdev = sb->st_rdev;
2352 nsb->st_atim = sb->st_atim;
2353 nsb->st_mtim = sb->st_mtim;
2354 nsb->st_ctim = sb->st_ctim;
2355 nsb->st_size = sb->st_size;
2356 nsb->st_blocks = sb->st_blocks;
2357 nsb->st_blksize = sb->st_blksize;
2358 nsb->st_flags = sb->st_flags;
2359 nsb->st_gen = sb->st_gen;
2360 nsb->st_birthtim = sb->st_birthtim;
2363 #ifndef _SYS_SYSPROTO_H_
2372 register struct nstat_args /* {
2381 error = kern_stat(td, uap->path, UIO_USERSPACE, &sb);
2384 cvtnstat(&sb, &nsb);
2385 return (copyout(&nsb, uap->ub, sizeof (nsb)));
2389 * NetBSD lstat. Get file status; this version does not follow links.
2391 #ifndef _SYS_SYSPROTO_H_
2400 register struct nlstat_args /* {
2409 error = kern_lstat(td, uap->path, UIO_USERSPACE, &sb);
2412 cvtnstat(&sb, &nsb);
2413 return (copyout(&nsb, uap->ub, sizeof (nsb)));
2417 * Get configurable pathname variables.
2419 #ifndef _SYS_SYSPROTO_H_
2420 struct pathconf_args {
2426 sys_pathconf(td, uap)
2428 register struct pathconf_args /* {
2434 return (kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name, FOLLOW));
2437 #ifndef _SYS_SYSPROTO_H_
2438 struct lpathconf_args {
2444 sys_lpathconf(td, uap)
2446 register struct lpathconf_args /* {
2452 return (kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name,
2457 kern_pathconf(struct thread *td, char *path, enum uio_seg pathseg, int name,
2460 struct nameidata nd;
2463 NDINIT(&nd, LOOKUP, LOCKSHARED | LOCKLEAF | AUDITVNODE1 | flags,
2465 if ((error = namei(&nd)) != 0)
2467 NDFREE(&nd, NDF_ONLY_PNBUF);
2469 /* If asynchronous I/O is available, it works for all files. */
2470 if (name == _PC_ASYNC_IO)
2471 td->td_retval[0] = async_io_version;
2473 error = VOP_PATHCONF(nd.ni_vp, name, td->td_retval);
2479 * Return target name of a symbolic link.
2481 #ifndef _SYS_SYSPROTO_H_
2482 struct readlink_args {
2489 sys_readlink(td, uap)
2491 register struct readlink_args /* {
2498 return (kern_readlink(td, uap->path, UIO_USERSPACE, uap->buf,
2499 UIO_USERSPACE, uap->count));
2501 #ifndef _SYS_SYSPROTO_H_
2502 struct readlinkat_args {
2510 sys_readlinkat(struct thread *td, struct readlinkat_args *uap)
2513 return (kern_readlinkat(td, uap->fd, uap->path, UIO_USERSPACE,
2514 uap->buf, UIO_USERSPACE, uap->bufsize));
2518 kern_readlink(struct thread *td, char *path, enum uio_seg pathseg, char *buf,
2519 enum uio_seg bufseg, size_t count)
2522 return (kern_readlinkat(td, AT_FDCWD, path, pathseg, buf, bufseg,
2527 kern_readlinkat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2528 char *buf, enum uio_seg bufseg, size_t count)
2533 struct nameidata nd;
2536 if (count > IOSIZE_MAX)
2539 NDINIT_AT(&nd, LOOKUP, NOFOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
2540 pathseg, path, fd, td);
2542 if ((error = namei(&nd)) != 0)
2544 NDFREE(&nd, NDF_ONLY_PNBUF);
2547 error = mac_vnode_check_readlink(td->td_ucred, vp);
2553 if (vp->v_type != VLNK)
2556 aiov.iov_base = buf;
2557 aiov.iov_len = count;
2558 auio.uio_iov = &aiov;
2559 auio.uio_iovcnt = 1;
2560 auio.uio_offset = 0;
2561 auio.uio_rw = UIO_READ;
2562 auio.uio_segflg = bufseg;
2564 auio.uio_resid = count;
2565 error = VOP_READLINK(vp, &auio, td->td_ucred);
2566 td->td_retval[0] = count - auio.uio_resid;
2573 * Common implementation code for chflags() and fchflags().
2576 setfflags(td, vp, flags)
2585 /* We can't support the value matching VNOVAL. */
2586 if (flags == VNOVAL)
2587 return (EOPNOTSUPP);
2590 * Prevent non-root users from setting flags on devices. When
2591 * a device is reused, users can retain ownership of the device
2592 * if they are allowed to set flags and programs assume that
2593 * chown can't fail when done as root.
2595 if (vp->v_type == VCHR || vp->v_type == VBLK) {
2596 error = priv_check(td, PRIV_VFS_CHFLAGS_DEV);
2601 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2604 vattr.va_flags = flags;
2605 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2607 error = mac_vnode_check_setflags(td->td_ucred, vp, vattr.va_flags);
2610 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
2612 vn_finished_write(mp);
2617 * Change flags of a file given a path name.
2619 #ifndef _SYS_SYSPROTO_H_
2620 struct chflags_args {
2626 sys_chflags(td, uap)
2628 register struct chflags_args /* {
2634 return (kern_chflags(td, uap->path, UIO_USERSPACE, uap->flags));
2637 #ifndef _SYS_SYSPROTO_H_
2638 struct chflagsat_args {
2646 sys_chflagsat(struct thread *td, struct chflagsat_args *uap)
2649 const char *path = uap->path;
2650 u_long flags = uap->flags;
2651 int atflag = uap->atflag;
2653 if (atflag & ~AT_SYMLINK_NOFOLLOW)
2656 return (kern_chflagsat(td, fd, path, UIO_USERSPACE, flags, atflag));
2660 kern_chflags(struct thread *td, const char *path, enum uio_seg pathseg,
2664 return (kern_chflagsat(td, AT_FDCWD, path, pathseg, flags, 0));
2668 * Same as chflags() but doesn't follow symlinks.
2671 sys_lchflags(td, uap)
2673 register struct lchflags_args /* {
2679 return (kern_chflagsat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2680 uap->flags, AT_SYMLINK_NOFOLLOW));
2684 kern_chflagsat(struct thread *td, int fd, const char *path,
2685 enum uio_seg pathseg, u_long flags, int atflag)
2687 struct nameidata nd;
2688 cap_rights_t rights;
2691 AUDIT_ARG_FFLAGS(flags);
2692 follow = (atflag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
2693 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, pathseg, path, fd,
2694 cap_rights_init(&rights, CAP_FCHFLAGS), td);
2695 if ((error = namei(&nd)) != 0)
2697 NDFREE(&nd, NDF_ONLY_PNBUF);
2698 error = setfflags(td, nd.ni_vp, flags);
2704 * Change flags of a file given a file descriptor.
2706 #ifndef _SYS_SYSPROTO_H_
2707 struct fchflags_args {
2713 sys_fchflags(td, uap)
2715 register struct fchflags_args /* {
2721 cap_rights_t rights;
2724 AUDIT_ARG_FD(uap->fd);
2725 AUDIT_ARG_FFLAGS(uap->flags);
2726 error = getvnode(td->td_proc->p_fd, uap->fd,
2727 cap_rights_init(&rights, CAP_FCHFLAGS), &fp);
2731 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
2732 AUDIT_ARG_VNODE1(fp->f_vnode);
2733 VOP_UNLOCK(fp->f_vnode, 0);
2735 error = setfflags(td, fp->f_vnode, uap->flags);
2741 * Common implementation code for chmod(), lchmod() and fchmod().
2744 setfmode(td, cred, vp, mode)
2754 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2756 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2758 vattr.va_mode = mode & ALLPERMS;
2760 error = mac_vnode_check_setmode(cred, vp, vattr.va_mode);
2763 error = VOP_SETATTR(vp, &vattr, cred);
2765 vn_finished_write(mp);
2770 * Change mode of a file given path name.
2772 #ifndef _SYS_SYSPROTO_H_
2781 register struct chmod_args /* {
2787 return (kern_chmod(td, uap->path, UIO_USERSPACE, uap->mode));
2790 #ifndef _SYS_SYSPROTO_H_
2791 struct fchmodat_args {
2799 sys_fchmodat(struct thread *td, struct fchmodat_args *uap)
2801 int flag = uap->flag;
2803 char *path = uap->path;
2804 mode_t mode = uap->mode;
2806 if (flag & ~AT_SYMLINK_NOFOLLOW)
2809 return (kern_fchmodat(td, fd, path, UIO_USERSPACE, mode, flag));
2813 kern_chmod(struct thread *td, char *path, enum uio_seg pathseg, int mode)
2816 return (kern_fchmodat(td, AT_FDCWD, path, pathseg, mode, 0));
2820 * Change mode of a file given path name (don't follow links.)
2822 #ifndef _SYS_SYSPROTO_H_
2823 struct lchmod_args {
2831 register struct lchmod_args /* {
2837 return (kern_fchmodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2838 uap->mode, AT_SYMLINK_NOFOLLOW));
2842 kern_fchmodat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2843 mode_t mode, int flag)
2845 struct nameidata nd;
2846 cap_rights_t rights;
2849 AUDIT_ARG_MODE(mode);
2850 follow = (flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
2851 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, pathseg, path, fd,
2852 cap_rights_init(&rights, CAP_FCHMOD), td);
2853 if ((error = namei(&nd)) != 0)
2855 NDFREE(&nd, NDF_ONLY_PNBUF);
2856 error = setfmode(td, td->td_ucred, nd.ni_vp, mode);
2862 * Change mode of a file given a file descriptor.
2864 #ifndef _SYS_SYSPROTO_H_
2865 struct fchmod_args {
2871 sys_fchmod(struct thread *td, struct fchmod_args *uap)
2874 cap_rights_t rights;
2877 AUDIT_ARG_FD(uap->fd);
2878 AUDIT_ARG_MODE(uap->mode);
2880 error = fget(td, uap->fd, cap_rights_init(&rights, CAP_FCHMOD), &fp);
2883 error = fo_chmod(fp, uap->mode, td->td_ucred, td);
2889 * Common implementation for chown(), lchown(), and fchown()
2892 setfown(td, cred, vp, uid, gid)
2903 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2905 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2910 error = mac_vnode_check_setowner(cred, vp, vattr.va_uid,
2914 error = VOP_SETATTR(vp, &vattr, cred);
2916 vn_finished_write(mp);
2921 * Set ownership given a path name.
2923 #ifndef _SYS_SYSPROTO_H_
2933 register struct chown_args /* {
2940 return (kern_chown(td, uap->path, UIO_USERSPACE, uap->uid, uap->gid));
2943 #ifndef _SYS_SYSPROTO_H_
2944 struct fchownat_args {
2953 sys_fchownat(struct thread *td, struct fchownat_args *uap)
2958 if (flag & ~AT_SYMLINK_NOFOLLOW)
2961 return (kern_fchownat(td, uap->fd, uap->path, UIO_USERSPACE, uap->uid,
2962 uap->gid, uap->flag));
2966 kern_chown(struct thread *td, char *path, enum uio_seg pathseg, int uid,
2970 return (kern_fchownat(td, AT_FDCWD, path, pathseg, uid, gid, 0));
2974 kern_fchownat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2975 int uid, int gid, int flag)
2977 struct nameidata nd;
2978 cap_rights_t rights;
2981 AUDIT_ARG_OWNER(uid, gid);
2982 follow = (flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
2983 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, pathseg, path, fd,
2984 cap_rights_init(&rights, CAP_FCHOWN), td);
2986 if ((error = namei(&nd)) != 0)
2988 NDFREE(&nd, NDF_ONLY_PNBUF);
2989 error = setfown(td, td->td_ucred, nd.ni_vp, uid, gid);
2995 * Set ownership given a path name, do not cross symlinks.
2997 #ifndef _SYS_SYSPROTO_H_
2998 struct lchown_args {
3007 register struct lchown_args /* {
3014 return (kern_lchown(td, uap->path, UIO_USERSPACE, uap->uid, uap->gid));
3018 kern_lchown(struct thread *td, char *path, enum uio_seg pathseg, int uid,
3022 return (kern_fchownat(td, AT_FDCWD, path, pathseg, uid, gid,
3023 AT_SYMLINK_NOFOLLOW));
3027 * Set ownership given a file descriptor.
3029 #ifndef _SYS_SYSPROTO_H_
3030 struct fchown_args {
3039 register struct fchown_args /* {
3046 cap_rights_t rights;
3049 AUDIT_ARG_FD(uap->fd);
3050 AUDIT_ARG_OWNER(uap->uid, uap->gid);
3051 error = fget(td, uap->fd, cap_rights_init(&rights, CAP_FCHOWN), &fp);
3054 error = fo_chown(fp, uap->uid, uap->gid, td->td_ucred, td);
3060 * Common implementation code for utimes(), lutimes(), and futimes().
3063 getutimes(usrtvp, tvpseg, tsp)
3064 const struct timeval *usrtvp;
3065 enum uio_seg tvpseg;
3066 struct timespec *tsp;
3068 struct timeval tv[2];
3069 const struct timeval *tvp;
3072 if (usrtvp == NULL) {
3073 vfs_timestamp(&tsp[0]);
3076 if (tvpseg == UIO_SYSSPACE) {
3079 if ((error = copyin(usrtvp, tv, sizeof(tv))) != 0)
3084 if (tvp[0].tv_usec < 0 || tvp[0].tv_usec >= 1000000 ||
3085 tvp[1].tv_usec < 0 || tvp[1].tv_usec >= 1000000)
3087 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
3088 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
3094 * Common implementation code for futimens(), utimensat().
3096 #define UTIMENS_NULL 0x1
3097 #define UTIMENS_EXIT 0x2
3099 getutimens(const struct timespec *usrtsp, enum uio_seg tspseg,
3100 struct timespec *tsp, int *retflags)
3102 struct timespec tsnow;
3105 vfs_timestamp(&tsnow);
3107 if (usrtsp == NULL) {
3110 *retflags |= UTIMENS_NULL;
3113 if (tspseg == UIO_SYSSPACE) {
3116 } else if ((error = copyin(usrtsp, tsp, sizeof(*tsp) * 2)) != 0)
3118 if (tsp[0].tv_nsec == UTIME_OMIT && tsp[1].tv_nsec == UTIME_OMIT)
3119 *retflags |= UTIMENS_EXIT;
3120 if (tsp[0].tv_nsec == UTIME_NOW && tsp[1].tv_nsec == UTIME_NOW)
3121 *retflags |= UTIMENS_NULL;
3122 if (tsp[0].tv_nsec == UTIME_OMIT)
3123 tsp[0].tv_sec = VNOVAL;
3124 else if (tsp[0].tv_nsec == UTIME_NOW)
3126 else if (tsp[0].tv_nsec < 0 || tsp[0].tv_nsec >= 1000000000L)
3128 if (tsp[1].tv_nsec == UTIME_OMIT)
3129 tsp[1].tv_sec = VNOVAL;
3130 else if (tsp[1].tv_nsec == UTIME_NOW)
3132 else if (tsp[1].tv_nsec < 0 || tsp[1].tv_nsec >= 1000000000L)
3139 * Common implementation code for utimes(), lutimes(), futimes(), futimens(),
3143 setutimes(td, vp, ts, numtimes, nullflag)
3146 const struct timespec *ts;
3152 int error, setbirthtime;
3154 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
3156 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3158 if (numtimes < 3 && !VOP_GETATTR(vp, &vattr, td->td_ucred) &&
3159 timespeccmp(&ts[1], &vattr.va_birthtime, < ))
3162 vattr.va_atime = ts[0];
3163 vattr.va_mtime = ts[1];
3165 vattr.va_birthtime = ts[1];
3167 vattr.va_birthtime = ts[2];
3169 vattr.va_vaflags |= VA_UTIMES_NULL;
3171 error = mac_vnode_check_setutimes(td->td_ucred, vp, vattr.va_atime,
3175 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3177 vn_finished_write(mp);
3182 * Set the access and modification times of a file.
3184 #ifndef _SYS_SYSPROTO_H_
3185 struct utimes_args {
3187 struct timeval *tptr;
3193 register struct utimes_args /* {
3195 struct timeval *tptr;
3199 return (kern_utimes(td, uap->path, UIO_USERSPACE, uap->tptr,
3203 #ifndef _SYS_SYSPROTO_H_
3204 struct futimesat_args {
3207 const struct timeval * times;
3211 sys_futimesat(struct thread *td, struct futimesat_args *uap)
3214 return (kern_utimesat(td, uap->fd, uap->path, UIO_USERSPACE,
3215 uap->times, UIO_USERSPACE));
3219 kern_utimes(struct thread *td, char *path, enum uio_seg pathseg,
3220 struct timeval *tptr, enum uio_seg tptrseg)
3223 return (kern_utimesat(td, AT_FDCWD, path, pathseg, tptr, tptrseg));
3227 kern_utimesat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
3228 struct timeval *tptr, enum uio_seg tptrseg)
3230 struct nameidata nd;
3231 struct timespec ts[2];
3232 cap_rights_t rights;
3235 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3237 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, fd,
3238 cap_rights_init(&rights, CAP_FUTIMES), td);
3240 if ((error = namei(&nd)) != 0)
3242 NDFREE(&nd, NDF_ONLY_PNBUF);
3243 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3249 * Set the access and modification times of a file.
3251 #ifndef _SYS_SYSPROTO_H_
3252 struct lutimes_args {
3254 struct timeval *tptr;
3258 sys_lutimes(td, uap)
3260 register struct lutimes_args /* {
3262 struct timeval *tptr;
3266 return (kern_lutimes(td, uap->path, UIO_USERSPACE, uap->tptr,
3271 kern_lutimes(struct thread *td, char *path, enum uio_seg pathseg,
3272 struct timeval *tptr, enum uio_seg tptrseg)
3274 struct timespec ts[2];
3275 struct nameidata nd;
3278 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3280 NDINIT(&nd, LOOKUP, NOFOLLOW | AUDITVNODE1, pathseg, path, td);
3281 if ((error = namei(&nd)) != 0)
3283 NDFREE(&nd, NDF_ONLY_PNBUF);
3284 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3290 * Set the access and modification times of a file.
3292 #ifndef _SYS_SYSPROTO_H_
3293 struct futimes_args {
3295 struct timeval *tptr;
3299 sys_futimes(td, uap)
3301 register struct futimes_args /* {
3303 struct timeval *tptr;
3307 return (kern_futimes(td, uap->fd, uap->tptr, UIO_USERSPACE));
3311 kern_futimes(struct thread *td, int fd, struct timeval *tptr,
3312 enum uio_seg tptrseg)
3314 struct timespec ts[2];
3316 cap_rights_t rights;
3320 error = getutimes(tptr, tptrseg, ts);
3323 error = getvnode(td->td_proc->p_fd, fd,
3324 cap_rights_init(&rights, CAP_FUTIMES), &fp);
3328 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
3329 AUDIT_ARG_VNODE1(fp->f_vnode);
3330 VOP_UNLOCK(fp->f_vnode, 0);
3332 error = setutimes(td, fp->f_vnode, ts, 2, tptr == NULL);
3338 sys_futimens(struct thread *td, struct futimens_args *uap)
3341 return (kern_futimens(td, uap->fd, uap->times, UIO_USERSPACE));
3345 kern_futimens(struct thread *td, int fd, struct timespec *tptr,
3346 enum uio_seg tptrseg)
3348 struct timespec ts[2];
3350 cap_rights_t rights;
3354 error = getutimens(tptr, tptrseg, ts, &flags);
3357 if (flags & UTIMENS_EXIT)
3359 error = getvnode(td->td_proc->p_fd, fd,
3360 cap_rights_init(&rights, CAP_FUTIMES), &fp);
3364 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
3365 AUDIT_ARG_VNODE1(fp->f_vnode);
3366 VOP_UNLOCK(fp->f_vnode, 0);
3368 error = setutimes(td, fp->f_vnode, ts, 2, flags & UTIMENS_NULL);
3374 sys_utimensat(struct thread *td, struct utimensat_args *uap)
3377 return (kern_utimensat(td, uap->fd, uap->path, UIO_USERSPACE,
3378 uap->times, UIO_USERSPACE, uap->flag));
3382 kern_utimensat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
3383 struct timespec *tptr, enum uio_seg tptrseg, int flag)
3385 struct nameidata nd;
3386 struct timespec ts[2];
3387 cap_rights_t rights;
3390 if (flag & ~AT_SYMLINK_NOFOLLOW)
3393 if ((error = getutimens(tptr, tptrseg, ts, &flags)) != 0)
3395 NDINIT_ATRIGHTS(&nd, LOOKUP, ((flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW :
3396 FOLLOW) | AUDITVNODE1, pathseg, path, fd,
3397 cap_rights_init(&rights, CAP_FUTIMES), td);
3398 if ((error = namei(&nd)) != 0)
3401 * We are allowed to call namei() regardless of 2xUTIME_OMIT.
3403 * "If both tv_nsec fields are UTIME_OMIT... EACCESS may be detected."
3404 * "Search permission is denied by a component of the path prefix."
3406 NDFREE(&nd, NDF_ONLY_PNBUF);
3407 if ((flags & UTIMENS_EXIT) == 0)
3408 error = setutimes(td, nd.ni_vp, ts, 2, flags & UTIMENS_NULL);
3414 * Truncate a file given its path name.
3416 #ifndef _SYS_SYSPROTO_H_
3417 struct truncate_args {
3424 sys_truncate(td, uap)
3426 register struct truncate_args /* {
3433 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3437 kern_truncate(struct thread *td, char *path, enum uio_seg pathseg, off_t length)
3443 struct nameidata nd;
3448 NDINIT(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, td);
3449 if ((error = namei(&nd)) != 0)
3452 rl_cookie = vn_rangelock_wlock(vp, 0, OFF_MAX);
3453 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0) {
3454 vn_rangelock_unlock(vp, rl_cookie);
3458 NDFREE(&nd, NDF_ONLY_PNBUF);
3459 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3460 if (vp->v_type == VDIR)
3463 else if ((error = mac_vnode_check_write(td->td_ucred, NOCRED, vp))) {
3466 else if ((error = vn_writechk(vp)) == 0 &&
3467 (error = VOP_ACCESS(vp, VWRITE, td->td_ucred, td)) == 0) {
3469 vattr.va_size = length;
3470 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3473 vn_finished_write(mp);
3474 vn_rangelock_unlock(vp, rl_cookie);
3479 #if defined(COMPAT_43)
3481 * Truncate a file given its path name.
3483 #ifndef _SYS_SYSPROTO_H_
3484 struct otruncate_args {
3492 register struct otruncate_args /* {
3497 struct truncate_args /* {
3503 nuap.path = uap->path;
3504 nuap.length = uap->length;
3505 return (sys_truncate(td, &nuap));
3507 #endif /* COMPAT_43 */
3509 /* Versions with the pad argument */
3511 freebsd6_truncate(struct thread *td, struct freebsd6_truncate_args *uap)
3513 struct truncate_args ouap;
3515 ouap.path = uap->path;
3516 ouap.length = uap->length;
3517 return (sys_truncate(td, &ouap));
3521 freebsd6_ftruncate(struct thread *td, struct freebsd6_ftruncate_args *uap)
3523 struct ftruncate_args ouap;
3526 ouap.length = uap->length;
3527 return (sys_ftruncate(td, &ouap));
3531 * Sync an open file.
3533 #ifndef _SYS_SYSPROTO_H_
3541 struct fsync_args /* {
3548 cap_rights_t rights;
3549 int error, lock_flags;
3551 AUDIT_ARG_FD(uap->fd);
3552 error = getvnode(td->td_proc->p_fd, uap->fd,
3553 cap_rights_init(&rights, CAP_FSYNC), &fp);
3557 error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
3560 if (MNT_SHARED_WRITES(mp) ||
3561 ((mp == NULL) && MNT_SHARED_WRITES(vp->v_mount))) {
3562 lock_flags = LK_SHARED;
3564 lock_flags = LK_EXCLUSIVE;
3566 vn_lock(vp, lock_flags | LK_RETRY);
3567 AUDIT_ARG_VNODE1(vp);
3568 if (vp->v_object != NULL) {
3569 VM_OBJECT_WLOCK(vp->v_object);
3570 vm_object_page_clean(vp->v_object, 0, 0, 0);
3571 VM_OBJECT_WUNLOCK(vp->v_object);
3573 error = VOP_FSYNC(vp, MNT_WAIT, td);
3576 vn_finished_write(mp);
3583 * Rename files. Source and destination must either both be directories, or
3584 * both not be directories. If target is a directory, it must be empty.
3586 #ifndef _SYS_SYSPROTO_H_
3587 struct rename_args {
3595 register struct rename_args /* {
3601 return (kern_rename(td, uap->from, uap->to, UIO_USERSPACE));
3604 #ifndef _SYS_SYSPROTO_H_
3605 struct renameat_args {
3613 sys_renameat(struct thread *td, struct renameat_args *uap)
3616 return (kern_renameat(td, uap->oldfd, uap->old, uap->newfd, uap->new,
3621 kern_rename(struct thread *td, char *from, char *to, enum uio_seg pathseg)
3624 return (kern_renameat(td, AT_FDCWD, from, AT_FDCWD, to, pathseg));
3628 kern_renameat(struct thread *td, int oldfd, char *old, int newfd, char *new,
3629 enum uio_seg pathseg)
3631 struct mount *mp = NULL;
3632 struct vnode *tvp, *fvp, *tdvp;
3633 struct nameidata fromnd, tond;
3634 cap_rights_t rights;
3640 NDINIT_ATRIGHTS(&fromnd, DELETE, LOCKPARENT | LOCKLEAF | SAVESTART |
3641 AUDITVNODE1, pathseg, old, oldfd,
3642 cap_rights_init(&rights, CAP_RENAMEAT), td);
3644 NDINIT_ATRIGHTS(&fromnd, DELETE, WANTPARENT | SAVESTART | AUDITVNODE1,
3645 pathseg, old, oldfd, cap_rights_init(&rights, CAP_RENAMEAT), td);
3648 if ((error = namei(&fromnd)) != 0)
3651 error = mac_vnode_check_rename_from(td->td_ucred, fromnd.ni_dvp,
3652 fromnd.ni_vp, &fromnd.ni_cnd);
3653 VOP_UNLOCK(fromnd.ni_dvp, 0);
3654 if (fromnd.ni_dvp != fromnd.ni_vp)
3655 VOP_UNLOCK(fromnd.ni_vp, 0);
3658 NDINIT_ATRIGHTS(&tond, RENAME, LOCKPARENT | LOCKLEAF | NOCACHE |
3659 SAVESTART | AUDITVNODE2, pathseg, new, newfd,
3660 cap_rights_init(&rights, CAP_LINKAT), td);
3661 if (fromnd.ni_vp->v_type == VDIR)
3662 tond.ni_cnd.cn_flags |= WILLBEDIR;
3663 if ((error = namei(&tond)) != 0) {
3664 /* Translate error code for rename("dir1", "dir2/."). */
3665 if (error == EISDIR && fvp->v_type == VDIR)
3667 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3668 vrele(fromnd.ni_dvp);
3674 error = vn_start_write(fvp, &mp, V_NOWAIT);
3676 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3677 NDFREE(&tond, NDF_ONLY_PNBUF);
3684 vrele(fromnd.ni_dvp);
3686 vrele(tond.ni_startdir);
3687 if (fromnd.ni_startdir != NULL)
3688 vrele(fromnd.ni_startdir);
3689 error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH);
3695 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
3698 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
3703 if (newfd != AT_FDCWD) {
3705 * If the target already exists we require CAP_UNLINKAT
3708 error = cap_check(&tond.ni_filecaps.fc_rights,
3709 cap_rights_init(&rights, CAP_UNLINKAT));
3720 * If the source is the same as the destination (that is, if they
3721 * are links to the same vnode), then there is nothing to do.
3727 error = mac_vnode_check_rename_to(td->td_ucred, tdvp,
3728 tond.ni_vp, fromnd.ni_dvp == tdvp, &tond.ni_cnd);
3732 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
3733 tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
3734 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3735 NDFREE(&tond, NDF_ONLY_PNBUF);
3737 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3738 NDFREE(&tond, NDF_ONLY_PNBUF);
3745 vrele(fromnd.ni_dvp);
3748 vrele(tond.ni_startdir);
3749 vn_finished_write(mp);
3751 if (fromnd.ni_startdir)
3752 vrele(fromnd.ni_startdir);
3759 * Make a directory file.
3761 #ifndef _SYS_SYSPROTO_H_
3770 register struct mkdir_args /* {
3776 return (kern_mkdir(td, uap->path, UIO_USERSPACE, uap->mode));
3779 #ifndef _SYS_SYSPROTO_H_
3780 struct mkdirat_args {
3787 sys_mkdirat(struct thread *td, struct mkdirat_args *uap)
3790 return (kern_mkdirat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode));
3794 kern_mkdir(struct thread *td, char *path, enum uio_seg segflg, int mode)
3797 return (kern_mkdirat(td, AT_FDCWD, path, segflg, mode));
3801 kern_mkdirat(struct thread *td, int fd, char *path, enum uio_seg segflg,
3807 struct nameidata nd;
3808 cap_rights_t rights;
3811 AUDIT_ARG_MODE(mode);
3814 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
3815 NOCACHE, segflg, path, fd, cap_rights_init(&rights, CAP_MKDIRAT),
3817 nd.ni_cnd.cn_flags |= WILLBEDIR;
3818 if ((error = namei(&nd)) != 0)
3822 NDFREE(&nd, NDF_ONLY_PNBUF);
3824 * XXX namei called with LOCKPARENT but not LOCKLEAF has
3825 * the strange behaviour of leaving the vnode unlocked
3826 * if the target is the same vnode as the parent.
3828 if (vp == nd.ni_dvp)
3835 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3836 NDFREE(&nd, NDF_ONLY_PNBUF);
3838 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3843 vattr.va_type = VDIR;
3844 vattr.va_mode = (mode & ACCESSPERMS) &~ td->td_proc->p_fd->fd_cmask;
3846 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
3851 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
3855 NDFREE(&nd, NDF_ONLY_PNBUF);
3859 vn_finished_write(mp);
3864 * Remove a directory file.
3866 #ifndef _SYS_SYSPROTO_H_
3874 struct rmdir_args /* {
3879 return (kern_rmdir(td, uap->path, UIO_USERSPACE));
3883 kern_rmdir(struct thread *td, char *path, enum uio_seg pathseg)
3886 return (kern_rmdirat(td, AT_FDCWD, path, pathseg));
3890 kern_rmdirat(struct thread *td, int fd, char *path, enum uio_seg pathseg)
3894 struct nameidata nd;
3895 cap_rights_t rights;
3900 NDINIT_ATRIGHTS(&nd, DELETE, LOCKPARENT | LOCKLEAF | AUDITVNODE1,
3901 pathseg, path, fd, cap_rights_init(&rights, CAP_UNLINKAT), td);
3902 if ((error = namei(&nd)) != 0)
3905 if (vp->v_type != VDIR) {
3910 * No rmdir "." please.
3912 if (nd.ni_dvp == vp) {
3917 * The root of a mounted filesystem cannot be deleted.
3919 if (vp->v_vflag & VV_ROOT) {
3924 error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp,
3929 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3930 NDFREE(&nd, NDF_ONLY_PNBUF);
3932 if (nd.ni_dvp == vp)
3936 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3940 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_UNLINK);
3941 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
3942 vn_finished_write(mp);
3944 NDFREE(&nd, NDF_ONLY_PNBUF);
3946 if (nd.ni_dvp == vp)
3955 * Read a block of directory entries in a filesystem independent format.
3957 #ifndef _SYS_SYSPROTO_H_
3958 struct ogetdirentries_args {
3966 ogetdirentries(struct thread *td, struct ogetdirentries_args *uap)
3971 error = kern_ogetdirentries(td, uap, &loff);
3973 error = copyout(&loff, uap->basep, sizeof(long));
3978 kern_ogetdirentries(struct thread *td, struct ogetdirentries_args *uap,
3983 struct uio auio, kuio;
3984 struct iovec aiov, kiov;
3985 struct dirent *dp, *edp;
3986 cap_rights_t rights;
3988 int error, eofflag, readcnt;
3992 /* XXX arbitrary sanity limit on `count'. */
3993 if (uap->count > 64 * 1024)
3995 error = getvnode(td->td_proc->p_fd, uap->fd,
3996 cap_rights_init(&rights, CAP_READ), &fp);
3999 if ((fp->f_flag & FREAD) == 0) {
4004 foffset = foffset_lock(fp, 0);
4006 if (vp->v_type != VDIR) {
4007 foffset_unlock(fp, foffset, 0);
4011 aiov.iov_base = uap->buf;
4012 aiov.iov_len = uap->count;
4013 auio.uio_iov = &aiov;
4014 auio.uio_iovcnt = 1;
4015 auio.uio_rw = UIO_READ;
4016 auio.uio_segflg = UIO_USERSPACE;
4018 auio.uio_resid = uap->count;
4019 vn_lock(vp, LK_SHARED | LK_RETRY);
4020 loff = auio.uio_offset = foffset;
4022 error = mac_vnode_check_readdir(td->td_ucred, vp);
4025 foffset_unlock(fp, foffset, FOF_NOUPDATE);
4030 # if (BYTE_ORDER != LITTLE_ENDIAN)
4031 if (vp->v_mount->mnt_maxsymlinklen <= 0) {
4032 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag,
4034 foffset = auio.uio_offset;
4039 kuio.uio_iov = &kiov;
4040 kuio.uio_segflg = UIO_SYSSPACE;
4041 kiov.iov_len = uap->count;
4042 dirbuf = malloc(uap->count, M_TEMP, M_WAITOK);
4043 kiov.iov_base = dirbuf;
4044 error = VOP_READDIR(vp, &kuio, fp->f_cred, &eofflag,
4046 foffset = kuio.uio_offset;
4048 readcnt = uap->count - kuio.uio_resid;
4049 edp = (struct dirent *)&dirbuf[readcnt];
4050 for (dp = (struct dirent *)dirbuf; dp < edp; ) {
4051 # if (BYTE_ORDER == LITTLE_ENDIAN)
4053 * The expected low byte of
4054 * dp->d_namlen is our dp->d_type.
4055 * The high MBZ byte of dp->d_namlen
4056 * is our dp->d_namlen.
4058 dp->d_type = dp->d_namlen;
4062 * The dp->d_type is the high byte
4063 * of the expected dp->d_namlen,
4064 * so must be zero'ed.
4068 if (dp->d_reclen > 0) {
4069 dp = (struct dirent *)
4070 ((char *)dp + dp->d_reclen);
4077 error = uiomove(dirbuf, readcnt, &auio);
4079 free(dirbuf, M_TEMP);
4083 foffset_unlock(fp, foffset, 0);
4087 if (uap->count == auio.uio_resid &&
4088 (vp->v_vflag & VV_ROOT) &&
4089 (vp->v_mount->mnt_flag & MNT_UNION)) {
4090 struct vnode *tvp = vp;
4091 vp = vp->v_mount->mnt_vnodecovered;
4100 foffset_unlock(fp, foffset, 0);
4102 td->td_retval[0] = uap->count - auio.uio_resid;
4107 #endif /* COMPAT_43 */
4110 * Read a block of directory entries in a filesystem independent format.
4112 #ifndef _SYS_SYSPROTO_H_
4113 struct getdirentries_args {
4121 sys_getdirentries(td, uap)
4123 register struct getdirentries_args /* {
4133 error = kern_getdirentries(td, uap->fd, uap->buf, uap->count, &base,
4134 NULL, UIO_USERSPACE);
4137 if (uap->basep != NULL)
4138 error = copyout(&base, uap->basep, sizeof(long));
4143 kern_getdirentries(struct thread *td, int fd, char *buf, u_int count,
4144 long *basep, ssize_t *residp, enum uio_seg bufseg)
4150 cap_rights_t rights;
4156 if (count > IOSIZE_MAX)
4158 auio.uio_resid = count;
4159 error = getvnode(td->td_proc->p_fd, fd,
4160 cap_rights_init(&rights, CAP_READ), &fp);
4163 if ((fp->f_flag & FREAD) == 0) {
4168 foffset = foffset_lock(fp, 0);
4170 if (vp->v_type != VDIR) {
4174 aiov.iov_base = buf;
4175 aiov.iov_len = count;
4176 auio.uio_iov = &aiov;
4177 auio.uio_iovcnt = 1;
4178 auio.uio_rw = UIO_READ;
4179 auio.uio_segflg = bufseg;
4181 vn_lock(vp, LK_SHARED | LK_RETRY);
4182 AUDIT_ARG_VNODE1(vp);
4183 loff = auio.uio_offset = foffset;
4185 error = mac_vnode_check_readdir(td->td_ucred, vp);
4188 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL,
4190 foffset = auio.uio_offset;
4195 if (count == auio.uio_resid &&
4196 (vp->v_vflag & VV_ROOT) &&
4197 (vp->v_mount->mnt_flag & MNT_UNION)) {
4198 struct vnode *tvp = vp;
4200 vp = vp->v_mount->mnt_vnodecovered;
4211 *residp = auio.uio_resid;
4212 td->td_retval[0] = count - auio.uio_resid;
4214 foffset_unlock(fp, foffset, 0);
4219 #ifndef _SYS_SYSPROTO_H_
4220 struct getdents_args {
4227 sys_getdents(td, uap)
4229 register struct getdents_args /* {
4235 struct getdirentries_args ap;
4239 ap.count = uap->count;
4241 return (sys_getdirentries(td, &ap));
4245 * Set the mode mask for creation of filesystem nodes.
4247 #ifndef _SYS_SYSPROTO_H_
4255 struct umask_args /* {
4259 register struct filedesc *fdp;
4261 FILEDESC_XLOCK(td->td_proc->p_fd);
4262 fdp = td->td_proc->p_fd;
4263 td->td_retval[0] = fdp->fd_cmask;
4264 fdp->fd_cmask = uap->newmask & ALLPERMS;
4265 FILEDESC_XUNLOCK(td->td_proc->p_fd);
4270 * Void all references to file by ripping underlying filesystem away from
4273 #ifndef _SYS_SYSPROTO_H_
4274 struct revoke_args {
4281 register struct revoke_args /* {
4287 struct nameidata nd;
4290 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
4292 if ((error = namei(&nd)) != 0)
4295 NDFREE(&nd, NDF_ONLY_PNBUF);
4296 if (vp->v_type != VCHR || vp->v_rdev == NULL) {
4301 error = mac_vnode_check_revoke(td->td_ucred, vp);
4305 error = VOP_GETATTR(vp, &vattr, td->td_ucred);
4308 if (td->td_ucred->cr_uid != vattr.va_uid) {
4309 error = priv_check(td, PRIV_VFS_ADMIN);
4314 VOP_REVOKE(vp, REVOKEALL);
4321 * Convert a user file descriptor to a kernel file entry and check that, if it
4322 * is a capability, the correct rights are present. A reference on the file
4323 * entry is held upon returning.
4326 getvnode(struct filedesc *fdp, int fd, cap_rights_t *rightsp, struct file **fpp)
4331 error = fget_unlocked(fdp, fd, rightsp, 0, &fp, NULL);
4336 * The file could be not of the vnode type, or it may be not
4337 * yet fully initialized, in which case the f_vnode pointer
4338 * may be set, but f_ops is still badfileops. E.g.,
4339 * devfs_open() transiently create such situation to
4340 * facilitate csw d_fdopen().
4342 * Dupfdopen() handling in kern_openat() installs the
4343 * half-baked file into the process descriptor table, allowing
4344 * other thread to dereference it. Guard against the race by
4347 if (fp->f_vnode == NULL || fp->f_ops == &badfileops) {
4348 fdrop(fp, curthread);
4357 * Get an (NFS) file handle.
4359 #ifndef _SYS_SYSPROTO_H_
4360 struct lgetfh_args {
4368 register struct lgetfh_args *uap;
4370 struct nameidata nd;
4372 register struct vnode *vp;
4375 error = priv_check(td, PRIV_VFS_GETFH);
4378 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
4383 NDFREE(&nd, NDF_ONLY_PNBUF);
4385 bzero(&fh, sizeof(fh));
4386 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
4387 error = VOP_VPTOFH(vp, &fh.fh_fid);
4390 error = copyout(&fh, uap->fhp, sizeof (fh));
4394 #ifndef _SYS_SYSPROTO_H_
4403 register struct getfh_args *uap;
4405 struct nameidata nd;
4407 register struct vnode *vp;
4410 error = priv_check(td, PRIV_VFS_GETFH);
4413 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
4418 NDFREE(&nd, NDF_ONLY_PNBUF);
4420 bzero(&fh, sizeof(fh));
4421 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
4422 error = VOP_VPTOFH(vp, &fh.fh_fid);
4425 error = copyout(&fh, uap->fhp, sizeof (fh));
4430 * syscall for the rpc.lockd to use to translate a NFS file handle into an
4433 * warning: do not remove the priv_check() call or this becomes one giant
4436 #ifndef _SYS_SYSPROTO_H_
4437 struct fhopen_args {
4438 const struct fhandle *u_fhp;
4445 struct fhopen_args /* {
4446 const struct fhandle *u_fhp;
4457 error = priv_check(td, PRIV_VFS_FHOPEN);
4461 fmode = FFLAGS(uap->flags);
4462 /* why not allow a non-read/write open for our lockd? */
4463 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
4465 error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
4468 /* find the mount point */
4469 mp = vfs_busyfs(&fhp.fh_fsid);
4472 /* now give me my vnode, it gets returned to me locked */
4473 error = VFS_FHTOVP(mp, &fhp.fh_fid, LK_EXCLUSIVE, &vp);
4478 error = falloc_noinstall(td, &fp);
4484 * An extra reference on `fp' has been held for us by
4485 * falloc_noinstall().
4491 error = vn_open_vnode(vp, fmode, td->td_ucred, td, fp);
4493 KASSERT(fp->f_ops == &badfileops,
4494 ("VOP_OPEN in fhopen() set f_ops"));
4495 KASSERT(td->td_dupfd < 0,
4496 ("fhopen() encountered fdopen()"));
4506 finit(fp, (fmode & FMASK) | (fp->f_flag & FHASLOCK), DTYPE_VNODE, vp,
4509 if ((fmode & O_TRUNC) != 0) {
4510 error = fo_truncate(fp, 0, td->td_ucred, td);
4515 error = finstall(td, fp, &indx, fmode, NULL);
4518 td->td_retval[0] = indx;
4523 * Stat an (NFS) file handle.
4525 #ifndef _SYS_SYSPROTO_H_
4526 struct fhstat_args {
4527 struct fhandle *u_fhp;
4534 register struct fhstat_args /* {
4535 struct fhandle *u_fhp;
4543 error = copyin(uap->u_fhp, &fh, sizeof(fh));
4546 error = kern_fhstat(td, fh, &sb);
4548 error = copyout(&sb, uap->sb, sizeof(sb));
4553 kern_fhstat(struct thread *td, struct fhandle fh, struct stat *sb)
4559 error = priv_check(td, PRIV_VFS_FHSTAT);
4562 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4564 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_EXCLUSIVE, &vp);
4568 error = vn_stat(vp, sb, td->td_ucred, NOCRED, td);
4574 * Implement fstatfs() for (NFS) file handles.
4576 #ifndef _SYS_SYSPROTO_H_
4577 struct fhstatfs_args {
4578 struct fhandle *u_fhp;
4583 sys_fhstatfs(td, uap)
4585 struct fhstatfs_args /* {
4586 struct fhandle *u_fhp;
4594 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4597 error = kern_fhstatfs(td, fh, &sf);
4600 return (copyout(&sf, uap->buf, sizeof(sf)));
4604 kern_fhstatfs(struct thread *td, fhandle_t fh, struct statfs *buf)
4611 error = priv_check(td, PRIV_VFS_FHSTATFS);
4614 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4616 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_EXCLUSIVE, &vp);
4622 error = prison_canseemount(td->td_ucred, mp);
4626 error = mac_mount_check_stat(td->td_ucred, mp);
4631 * Set these in case the underlying filesystem fails to do so.
4634 sp->f_version = STATFS_VERSION;
4635 sp->f_namemax = NAME_MAX;
4636 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
4637 error = VFS_STATFS(mp, sp);
4646 kern_posix_fallocate(struct thread *td, int fd, off_t offset, off_t len)
4651 cap_rights_t rights;
4652 off_t olen, ooffset;
4655 if (offset < 0 || len <= 0)
4657 /* Check for wrap. */
4658 if (offset > OFF_MAX - len)
4660 error = fget(td, fd, cap_rights_init(&rights, CAP_WRITE), &fp);
4663 if ((fp->f_ops->fo_flags & DFLAG_SEEKABLE) == 0) {
4667 if ((fp->f_flag & FWRITE) == 0) {
4671 if (fp->f_type != DTYPE_VNODE) {
4676 if (vp->v_type != VREG) {
4681 /* Allocating blocks may take a long time, so iterate. */
4688 error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
4691 error = vn_lock(vp, LK_EXCLUSIVE);
4693 vn_finished_write(mp);
4697 error = mac_vnode_check_write(td->td_ucred, fp->f_cred, vp);
4700 error = VOP_ALLOCATE(vp, &offset, &len);
4702 vn_finished_write(mp);
4704 if (olen + ooffset != offset + len) {
4705 panic("offset + len changed from %jx/%jx to %jx/%jx",
4706 ooffset, olen, offset, len);
4708 if (error != 0 || len == 0)
4710 KASSERT(olen > len, ("Iteration did not make progress?"));
4719 sys_posix_fallocate(struct thread *td, struct posix_fallocate_args *uap)
4722 td->td_retval[0] = kern_posix_fallocate(td, uap->fd, uap->offset,
4728 * Unlike madvise(2), we do not make a best effort to remember every
4729 * possible caching hint. Instead, we remember the last setting with
4730 * the exception that we will allow POSIX_FADV_NORMAL to adjust the
4731 * region of any current setting.
4734 kern_posix_fadvise(struct thread *td, int fd, off_t offset, off_t len,
4737 struct fadvise_info *fa, *new;
4740 cap_rights_t rights;
4744 if (offset < 0 || len < 0 || offset > OFF_MAX - len)
4747 case POSIX_FADV_SEQUENTIAL:
4748 case POSIX_FADV_RANDOM:
4749 case POSIX_FADV_NOREUSE:
4750 new = malloc(sizeof(*fa), M_FADVISE, M_WAITOK);
4752 case POSIX_FADV_NORMAL:
4753 case POSIX_FADV_WILLNEED:
4754 case POSIX_FADV_DONTNEED:
4760 /* XXX: CAP_POSIX_FADVISE? */
4761 error = fget(td, fd, cap_rights_init(&rights), &fp);
4764 if ((fp->f_ops->fo_flags & DFLAG_SEEKABLE) == 0) {
4768 if (fp->f_type != DTYPE_VNODE) {
4773 if (vp->v_type != VREG) {
4780 end = offset + len - 1;
4782 case POSIX_FADV_SEQUENTIAL:
4783 case POSIX_FADV_RANDOM:
4784 case POSIX_FADV_NOREUSE:
4786 * Try to merge any existing non-standard region with
4787 * this new region if possible, otherwise create a new
4788 * non-standard region for this request.
4790 mtx_pool_lock(mtxpool_sleep, fp);
4792 if (fa != NULL && fa->fa_advice == advice &&
4793 ((fa->fa_start <= end && fa->fa_end >= offset) ||
4794 (end != OFF_MAX && fa->fa_start == end + 1) ||
4795 (fa->fa_end != OFF_MAX && fa->fa_end + 1 == offset))) {
4796 if (offset < fa->fa_start)
4797 fa->fa_start = offset;
4798 if (end > fa->fa_end)
4801 new->fa_advice = advice;
4802 new->fa_start = offset;
4804 new->fa_prevstart = 0;
4805 new->fa_prevend = 0;
4809 mtx_pool_unlock(mtxpool_sleep, fp);
4811 case POSIX_FADV_NORMAL:
4813 * If a the "normal" region overlaps with an existing
4814 * non-standard region, trim or remove the
4815 * non-standard region.
4817 mtx_pool_lock(mtxpool_sleep, fp);
4820 if (offset <= fa->fa_start && end >= fa->fa_end) {
4822 fp->f_advice = NULL;
4823 } else if (offset <= fa->fa_start &&
4824 end >= fa->fa_start)
4825 fa->fa_start = end + 1;
4826 else if (offset <= fa->fa_end && end >= fa->fa_end)
4827 fa->fa_end = offset - 1;
4828 else if (offset >= fa->fa_start && end <= fa->fa_end) {
4830 * If the "normal" region is a middle
4831 * portion of the existing
4832 * non-standard region, just remove
4833 * the whole thing rather than picking
4834 * one side or the other to
4838 fp->f_advice = NULL;
4841 mtx_pool_unlock(mtxpool_sleep, fp);
4843 case POSIX_FADV_WILLNEED:
4844 case POSIX_FADV_DONTNEED:
4845 error = VOP_ADVISE(vp, offset, end, advice);
4851 free(new, M_FADVISE);
4856 sys_posix_fadvise(struct thread *td, struct posix_fadvise_args *uap)
4859 td->td_retval[0] = kern_posix_fadvise(td, uap->fd, uap->offset,
4860 uap->len, uap->advice);