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_compat.h"
41 #include "opt_kdtrace.h"
42 #include "opt_ktrace.h"
44 #include <sys/param.h>
45 #include <sys/systm.h>
48 #include <sys/sysent.h>
49 #include <sys/malloc.h>
50 #include <sys/mount.h>
51 #include <sys/mutex.h>
52 #include <sys/sysproto.h>
53 #include <sys/namei.h>
54 #include <sys/filedesc.h>
55 #include <sys/kernel.h>
56 #include <sys/fcntl.h>
58 #include <sys/filio.h>
59 #include <sys/limits.h>
60 #include <sys/linker.h>
64 #include <sys/unistd.h>
65 #include <sys/vnode.h>
68 #include <sys/dirent.h>
70 #include <sys/syscallsubr.h>
71 #include <sys/sysctl.h>
73 #include <sys/ktrace.h>
76 #include <machine/stdarg.h>
78 #include <security/audit/audit.h>
79 #include <security/mac/mac_framework.h>
82 #include <vm/vm_object.h>
83 #include <vm/vm_page.h>
86 SDT_PROVIDER_DEFINE(vfs);
87 SDT_PROBE_DEFINE(vfs, , stat, mode);
88 SDT_PROBE_ARGTYPE(vfs, , stat, mode, 0, "char *");
89 SDT_PROBE_ARGTYPE(vfs, , stat, mode, 1, "int");
90 SDT_PROBE_DEFINE(vfs, , stat, reg);
91 SDT_PROBE_ARGTYPE(vfs, , stat, reg, 0, "char *");
92 SDT_PROBE_ARGTYPE(vfs, , stat, reg, 1, "int");
94 static int chroot_refuse_vdir_fds(struct filedesc *fdp);
95 static int getutimes(const struct timeval *, enum uio_seg, struct timespec *);
96 static int setfown(struct thread *td, struct vnode *, uid_t, gid_t);
97 static int setfmode(struct thread *td, struct vnode *, int);
98 static int setfflags(struct thread *td, struct vnode *, int);
99 static int setutimes(struct thread *td, struct vnode *,
100 const struct timespec *, int, int);
101 static int vn_access(struct vnode *vp, int user_flags, struct ucred *cred,
105 * The module initialization routine for POSIX asynchronous I/O will
106 * set this to the version of AIO that it implements. (Zero means
107 * that it is not implemented.) This value is used here by pathconf()
108 * and in kern_descrip.c by fpathconf().
110 int async_io_version;
113 static int syncprt = 0;
114 SYSCTL_INT(_debug, OID_AUTO, syncprt, CTLFLAG_RW, &syncprt, 0, "");
118 * Sync each mounted filesystem.
120 #ifndef _SYS_SYSPROTO_H_
129 struct sync_args *uap;
131 struct mount *mp, *nmp;
134 mtx_lock(&mountlist_mtx);
135 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
136 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK)) {
137 nmp = TAILQ_NEXT(mp, mnt_list);
140 vfslocked = VFS_LOCK_GIANT(mp);
141 if ((mp->mnt_flag & MNT_RDONLY) == 0 &&
142 vn_start_write(NULL, &mp, V_NOWAIT) == 0) {
145 mp->mnt_kern_flag &= ~MNTK_ASYNC;
147 vfs_msync(mp, MNT_NOWAIT);
148 VFS_SYNC(mp, MNT_NOWAIT);
151 if ((mp->mnt_flag & MNT_ASYNC) != 0 &&
152 mp->mnt_noasync == 0)
153 mp->mnt_kern_flag |= MNTK_ASYNC;
155 vn_finished_write(mp);
157 VFS_UNLOCK_GIANT(vfslocked);
158 mtx_lock(&mountlist_mtx);
159 nmp = TAILQ_NEXT(mp, mnt_list);
162 mtx_unlock(&mountlist_mtx);
167 * Change filesystem quotas.
169 #ifndef _SYS_SYSPROTO_H_
170 struct quotactl_args {
180 register struct quotactl_args /* {
192 AUDIT_ARG_CMD(uap->cmd);
193 AUDIT_ARG_UID(uap->uid);
194 if (!prison_allow(td->td_ucred, PR_ALLOW_QUOTAS))
196 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | MPSAFE | AUDITVNODE1,
197 UIO_USERSPACE, uap->path, td);
198 if ((error = namei(&nd)) != 0)
200 vfslocked = NDHASGIANT(&nd);
201 NDFREE(&nd, NDF_ONLY_PNBUF);
202 mp = nd.ni_vp->v_mount;
205 error = vfs_busy(mp, 0);
208 VFS_UNLOCK_GIANT(vfslocked);
211 error = VFS_QUOTACTL(mp, uap->cmd, uap->uid, uap->arg);
213 VFS_UNLOCK_GIANT(vfslocked);
218 * Used by statfs conversion routines to scale the block size up if
219 * necessary so that all of the block counts are <= 'max_size'. Note
220 * that 'max_size' should be a bitmask, i.e. 2^n - 1 for some non-zero
224 statfs_scale_blocks(struct statfs *sf, long max_size)
229 KASSERT(powerof2(max_size + 1), ("%s: invalid max_size", __func__));
232 * Attempt to scale the block counts to give a more accurate
233 * overview to userland of the ratio of free space to used
234 * space. To do this, find the largest block count and compute
235 * a divisor that lets it fit into a signed integer <= max_size.
237 if (sf->f_bavail < 0)
238 count = -sf->f_bavail;
240 count = sf->f_bavail;
241 count = MAX(sf->f_blocks, MAX(sf->f_bfree, count));
242 if (count <= max_size)
245 count >>= flsl(max_size);
252 sf->f_bsize <<= shift;
253 sf->f_blocks >>= shift;
254 sf->f_bfree >>= shift;
255 sf->f_bavail >>= shift;
259 * Get filesystem statistics.
261 #ifndef _SYS_SYSPROTO_H_
270 register struct statfs_args /* {
278 error = kern_statfs(td, uap->path, UIO_USERSPACE, &sf);
280 error = copyout(&sf, uap->buf, sizeof(sf));
285 kern_statfs(struct thread *td, char *path, enum uio_seg pathseg,
289 struct statfs *sp, sb;
294 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | MPSAFE |
295 AUDITVNODE1, pathseg, path, td);
299 vfslocked = NDHASGIANT(&nd);
300 mp = nd.ni_vp->v_mount;
302 NDFREE(&nd, NDF_ONLY_PNBUF);
304 error = vfs_busy(mp, 0);
307 VFS_UNLOCK_GIANT(vfslocked);
311 error = mac_mount_check_stat(td->td_ucred, mp);
316 * Set these in case the underlying filesystem fails to do so.
319 sp->f_version = STATFS_VERSION;
320 sp->f_namemax = NAME_MAX;
321 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
322 error = VFS_STATFS(mp, sp);
325 if (priv_check(td, PRIV_VFS_GENERATION)) {
326 bcopy(sp, &sb, sizeof(sb));
327 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
328 prison_enforce_statfs(td->td_ucred, mp, &sb);
334 VFS_UNLOCK_GIANT(vfslocked);
339 * Get filesystem statistics.
341 #ifndef _SYS_SYSPROTO_H_
342 struct fstatfs_args {
350 register struct fstatfs_args /* {
358 error = kern_fstatfs(td, uap->fd, &sf);
360 error = copyout(&sf, uap->buf, sizeof(sf));
365 kern_fstatfs(struct thread *td, int fd, struct statfs *buf)
369 struct statfs *sp, sb;
375 error = getvnode(td->td_proc->p_fd, fd, &fp);
379 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
380 vn_lock(vp, LK_SHARED | LK_RETRY);
382 AUDIT_ARG_VNODE1(vp);
393 error = vfs_busy(mp, 0);
396 VFS_UNLOCK_GIANT(vfslocked);
400 error = mac_mount_check_stat(td->td_ucred, mp);
405 * Set these in case the underlying filesystem fails to do so.
408 sp->f_version = STATFS_VERSION;
409 sp->f_namemax = NAME_MAX;
410 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
411 error = VFS_STATFS(mp, sp);
414 if (priv_check(td, PRIV_VFS_GENERATION)) {
415 bcopy(sp, &sb, sizeof(sb));
416 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
417 prison_enforce_statfs(td->td_ucred, mp, &sb);
424 VFS_UNLOCK_GIANT(vfslocked);
429 * Get statistics on all filesystems.
431 #ifndef _SYS_SYSPROTO_H_
432 struct getfsstat_args {
441 register struct getfsstat_args /* {
448 return (kern_getfsstat(td, &uap->buf, uap->bufsize, UIO_USERSPACE,
453 * If (bufsize > 0 && bufseg == UIO_SYSSPACE)
454 * The caller is responsible for freeing memory which will be allocated
458 kern_getfsstat(struct thread *td, struct statfs **buf, size_t bufsize,
459 enum uio_seg bufseg, int flags)
461 struct mount *mp, *nmp;
462 struct statfs *sfsp, *sp, sb;
463 size_t count, maxcount;
467 maxcount = bufsize / sizeof(struct statfs);
470 else if (bufseg == UIO_USERSPACE)
472 else /* if (bufseg == UIO_SYSSPACE) */ {
474 mtx_lock(&mountlist_mtx);
475 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
478 mtx_unlock(&mountlist_mtx);
479 if (maxcount > count)
481 sfsp = *buf = malloc(maxcount * sizeof(struct statfs), M_TEMP,
485 mtx_lock(&mountlist_mtx);
486 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
487 if (prison_canseemount(td->td_ucred, mp) != 0) {
488 nmp = TAILQ_NEXT(mp, mnt_list);
492 if (mac_mount_check_stat(td->td_ucred, mp) != 0) {
493 nmp = TAILQ_NEXT(mp, mnt_list);
497 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK)) {
498 nmp = TAILQ_NEXT(mp, mnt_list);
501 vfslocked = VFS_LOCK_GIANT(mp);
502 if (sfsp && count < maxcount) {
505 * Set these in case the underlying filesystem
508 sp->f_version = STATFS_VERSION;
509 sp->f_namemax = NAME_MAX;
510 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
512 * If MNT_NOWAIT or MNT_LAZY is specified, do not
513 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
514 * overrides MNT_WAIT.
516 if (((flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
517 (flags & MNT_WAIT)) &&
518 (error = VFS_STATFS(mp, sp))) {
519 VFS_UNLOCK_GIANT(vfslocked);
520 mtx_lock(&mountlist_mtx);
521 nmp = TAILQ_NEXT(mp, mnt_list);
525 if (priv_check(td, PRIV_VFS_GENERATION)) {
526 bcopy(sp, &sb, sizeof(sb));
527 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
528 prison_enforce_statfs(td->td_ucred, mp, &sb);
531 if (bufseg == UIO_SYSSPACE)
532 bcopy(sp, sfsp, sizeof(*sp));
533 else /* if (bufseg == UIO_USERSPACE) */ {
534 error = copyout(sp, sfsp, sizeof(*sp));
537 VFS_UNLOCK_GIANT(vfslocked);
543 VFS_UNLOCK_GIANT(vfslocked);
545 mtx_lock(&mountlist_mtx);
546 nmp = TAILQ_NEXT(mp, mnt_list);
549 mtx_unlock(&mountlist_mtx);
550 if (sfsp && count > maxcount)
551 td->td_retval[0] = maxcount;
553 td->td_retval[0] = count;
557 #ifdef COMPAT_FREEBSD4
559 * Get old format filesystem statistics.
561 static void cvtstatfs(struct statfs *, struct ostatfs *);
563 #ifndef _SYS_SYSPROTO_H_
564 struct freebsd4_statfs_args {
570 freebsd4_statfs(td, uap)
572 struct freebsd4_statfs_args /* {
581 error = kern_statfs(td, uap->path, UIO_USERSPACE, &sf);
584 cvtstatfs(&sf, &osb);
585 return (copyout(&osb, uap->buf, sizeof(osb)));
589 * Get filesystem statistics.
591 #ifndef _SYS_SYSPROTO_H_
592 struct freebsd4_fstatfs_args {
598 freebsd4_fstatfs(td, uap)
600 struct freebsd4_fstatfs_args /* {
609 error = kern_fstatfs(td, uap->fd, &sf);
612 cvtstatfs(&sf, &osb);
613 return (copyout(&osb, uap->buf, sizeof(osb)));
617 * Get statistics on all filesystems.
619 #ifndef _SYS_SYSPROTO_H_
620 struct freebsd4_getfsstat_args {
627 freebsd4_getfsstat(td, uap)
629 register struct freebsd4_getfsstat_args /* {
635 struct statfs *buf, *sp;
640 count = uap->bufsize / sizeof(struct ostatfs);
641 size = count * sizeof(struct statfs);
642 error = kern_getfsstat(td, &buf, size, UIO_SYSSPACE, uap->flags);
644 count = td->td_retval[0];
646 while (count > 0 && error == 0) {
648 error = copyout(&osb, uap->buf, sizeof(osb));
659 * Implement fstatfs() for (NFS) file handles.
661 #ifndef _SYS_SYSPROTO_H_
662 struct freebsd4_fhstatfs_args {
663 struct fhandle *u_fhp;
668 freebsd4_fhstatfs(td, uap)
670 struct freebsd4_fhstatfs_args /* {
671 struct fhandle *u_fhp;
680 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
683 error = kern_fhstatfs(td, fh, &sf);
686 cvtstatfs(&sf, &osb);
687 return (copyout(&osb, uap->buf, sizeof(osb)));
691 * Convert a new format statfs structure to an old format statfs structure.
699 statfs_scale_blocks(nsp, LONG_MAX);
700 bzero(osp, sizeof(*osp));
701 osp->f_bsize = nsp->f_bsize;
702 osp->f_iosize = MIN(nsp->f_iosize, LONG_MAX);
703 osp->f_blocks = nsp->f_blocks;
704 osp->f_bfree = nsp->f_bfree;
705 osp->f_bavail = nsp->f_bavail;
706 osp->f_files = MIN(nsp->f_files, LONG_MAX);
707 osp->f_ffree = MIN(nsp->f_ffree, LONG_MAX);
708 osp->f_owner = nsp->f_owner;
709 osp->f_type = nsp->f_type;
710 osp->f_flags = nsp->f_flags;
711 osp->f_syncwrites = MIN(nsp->f_syncwrites, LONG_MAX);
712 osp->f_asyncwrites = MIN(nsp->f_asyncwrites, LONG_MAX);
713 osp->f_syncreads = MIN(nsp->f_syncreads, LONG_MAX);
714 osp->f_asyncreads = MIN(nsp->f_asyncreads, LONG_MAX);
715 strlcpy(osp->f_fstypename, nsp->f_fstypename,
716 MIN(MFSNAMELEN, OMFSNAMELEN));
717 strlcpy(osp->f_mntonname, nsp->f_mntonname,
718 MIN(MNAMELEN, OMNAMELEN));
719 strlcpy(osp->f_mntfromname, nsp->f_mntfromname,
720 MIN(MNAMELEN, OMNAMELEN));
721 osp->f_fsid = nsp->f_fsid;
723 #endif /* COMPAT_FREEBSD4 */
726 * Change current working directory to a given file descriptor.
728 #ifndef _SYS_SYSPROTO_H_
736 struct fchdir_args /* {
740 register struct filedesc *fdp = td->td_proc->p_fd;
741 struct vnode *vp, *tdp, *vpold;
747 AUDIT_ARG_FD(uap->fd);
748 if ((error = getvnode(fdp, uap->fd, &fp)) != 0)
753 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
754 vn_lock(vp, LK_SHARED | LK_RETRY);
755 AUDIT_ARG_VNODE1(vp);
756 error = change_dir(vp, td);
757 while (!error && (mp = vp->v_mountedhere) != NULL) {
761 tvfslocked = VFS_LOCK_GIANT(mp);
762 error = VFS_ROOT(mp, LK_SHARED, &tdp);
765 VFS_UNLOCK_GIANT(tvfslocked);
769 VFS_UNLOCK_GIANT(vfslocked);
771 vfslocked = tvfslocked;
775 VFS_UNLOCK_GIANT(vfslocked);
779 VFS_UNLOCK_GIANT(vfslocked);
781 vpold = fdp->fd_cdir;
783 FILEDESC_XUNLOCK(fdp);
784 vfslocked = VFS_LOCK_GIANT(vpold->v_mount);
786 VFS_UNLOCK_GIANT(vfslocked);
791 * Change current working directory (``.'').
793 #ifndef _SYS_SYSPROTO_H_
801 struct chdir_args /* {
806 return (kern_chdir(td, uap->path, UIO_USERSPACE));
810 kern_chdir(struct thread *td, char *path, enum uio_seg pathseg)
812 register struct filedesc *fdp = td->td_proc->p_fd;
818 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1 |
819 MPSAFE, pathseg, path, td);
820 if ((error = namei(&nd)) != 0)
822 vfslocked = NDHASGIANT(&nd);
823 if ((error = change_dir(nd.ni_vp, td)) != 0) {
825 VFS_UNLOCK_GIANT(vfslocked);
826 NDFREE(&nd, NDF_ONLY_PNBUF);
829 VOP_UNLOCK(nd.ni_vp, 0);
830 VFS_UNLOCK_GIANT(vfslocked);
831 NDFREE(&nd, NDF_ONLY_PNBUF);
834 fdp->fd_cdir = nd.ni_vp;
835 FILEDESC_XUNLOCK(fdp);
836 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
838 VFS_UNLOCK_GIANT(vfslocked);
843 * Helper function for raised chroot(2) security function: Refuse if
844 * any filedescriptors are open directories.
847 chroot_refuse_vdir_fds(fdp)
848 struct filedesc *fdp;
854 FILEDESC_LOCK_ASSERT(fdp);
856 for (fd = 0; fd < fdp->fd_nfiles ; fd++) {
857 fp = fget_locked(fdp, fd);
860 if (fp->f_type == DTYPE_VNODE) {
862 if (vp->v_type == VDIR)
870 * This sysctl determines if we will allow a process to chroot(2) if it
871 * has a directory open:
872 * 0: disallowed for all processes.
873 * 1: allowed for processes that were not already chroot(2)'ed.
874 * 2: allowed for all processes.
877 static int chroot_allow_open_directories = 1;
879 SYSCTL_INT(_kern, OID_AUTO, chroot_allow_open_directories, CTLFLAG_RW,
880 &chroot_allow_open_directories, 0, "");
883 * Change notion of root (``/'') directory.
885 #ifndef _SYS_SYSPROTO_H_
893 struct chroot_args /* {
901 error = priv_check(td, PRIV_VFS_CHROOT);
904 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | MPSAFE |
905 AUDITVNODE1, UIO_USERSPACE, uap->path, td);
909 vfslocked = NDHASGIANT(&nd);
910 if ((error = change_dir(nd.ni_vp, td)) != 0)
913 if ((error = mac_vnode_check_chroot(td->td_ucred, nd.ni_vp)))
916 VOP_UNLOCK(nd.ni_vp, 0);
917 error = change_root(nd.ni_vp, td);
919 VFS_UNLOCK_GIANT(vfslocked);
920 NDFREE(&nd, NDF_ONLY_PNBUF);
924 VFS_UNLOCK_GIANT(vfslocked);
926 NDFREE(&nd, NDF_ONLY_PNBUF);
931 * Common routine for chroot and chdir. Callers must provide a locked vnode
941 ASSERT_VOP_LOCKED(vp, "change_dir(): vp not locked");
942 if (vp->v_type != VDIR)
945 error = mac_vnode_check_chdir(td->td_ucred, vp);
949 error = VOP_ACCESS(vp, VEXEC, td->td_ucred, td);
954 * Common routine for kern_chroot() and jail_attach(). The caller is
955 * responsible for invoking priv_check() and mac_vnode_check_chroot() to
956 * authorize this operation.
963 struct filedesc *fdp;
968 VFS_ASSERT_GIANT(vp->v_mount);
969 fdp = td->td_proc->p_fd;
971 if (chroot_allow_open_directories == 0 ||
972 (chroot_allow_open_directories == 1 && fdp->fd_rdir != rootvnode)) {
973 error = chroot_refuse_vdir_fds(fdp);
975 FILEDESC_XUNLOCK(fdp);
979 oldvp = fdp->fd_rdir;
986 FILEDESC_XUNLOCK(fdp);
987 vfslocked = VFS_LOCK_GIANT(oldvp->v_mount);
989 VFS_UNLOCK_GIANT(vfslocked);
994 * Check permissions, allocate an open file structure, and call the device
995 * open routine if any.
997 #ifndef _SYS_SYSPROTO_H_
1007 register struct open_args /* {
1014 return (kern_open(td, uap->path, UIO_USERSPACE, uap->flags, uap->mode));
1017 #ifndef _SYS_SYSPROTO_H_
1018 struct openat_args {
1026 openat(struct thread *td, struct openat_args *uap)
1029 return (kern_openat(td, uap->fd, uap->path, UIO_USERSPACE, uap->flag,
1034 kern_open(struct thread *td, char *path, enum uio_seg pathseg, int flags,
1038 return (kern_openat(td, AT_FDCWD, path, pathseg, flags, mode));
1042 kern_openat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1043 int flags, int mode)
1045 struct proc *p = td->td_proc;
1046 struct filedesc *fdp = p->p_fd;
1053 int type, indx, error;
1055 struct nameidata nd;
1058 AUDIT_ARG_FFLAGS(flags);
1059 AUDIT_ARG_MODE(mode);
1060 /* XXX: audit dirfd */
1062 * Only one of the O_EXEC, O_RDONLY, O_WRONLY and O_RDWR may
1065 if (flags & O_EXEC) {
1066 if (flags & O_ACCMODE)
1068 } else if ((flags & O_ACCMODE) == O_ACCMODE)
1071 flags = FFLAGS(flags);
1073 error = falloc(td, &nfp, &indx);
1076 /* An extra reference on `nfp' has been held for us by falloc(). */
1078 /* Set the flags early so the finit in devfs can pick them up. */
1079 fp->f_flag = flags & FMASK;
1080 cmode = ((mode &~ fdp->fd_cmask) & ALLPERMS) &~ S_ISTXT;
1081 NDINIT_AT(&nd, LOOKUP, FOLLOW | AUDITVNODE1 | MPSAFE, pathseg, path, fd,
1083 td->td_dupfd = -1; /* XXX check for fdopen */
1084 error = vn_open(&nd, &flags, cmode, fp);
1087 * If the vn_open replaced the method vector, something
1088 * wonderous happened deep below and we just pass it up
1089 * pretending we know what we do.
1091 if (error == ENXIO && fp->f_ops != &badfileops) {
1093 td->td_retval[0] = indx;
1098 * handle special fdopen() case. bleh. dupfdopen() is
1099 * responsible for dropping the old contents of ofiles[indx]
1102 if ((error == ENODEV || error == ENXIO) &&
1103 td->td_dupfd >= 0 && /* XXX from fdopen */
1105 dupfdopen(td, fdp, indx, td->td_dupfd, flags, error)) == 0) {
1106 td->td_retval[0] = indx;
1111 * Clean up the descriptor, but only if another thread hadn't
1112 * replaced or closed it.
1114 fdclose(fdp, fp, indx, td);
1117 if (error == ERESTART)
1122 vfslocked = NDHASGIANT(&nd);
1123 NDFREE(&nd, NDF_ONLY_PNBUF);
1126 fp->f_vnode = vp; /* XXX Does devfs need this? */
1128 * If the file wasn't claimed by devfs bind it to the normal
1129 * vnode operations here.
1131 if (fp->f_ops == &badfileops) {
1132 KASSERT(vp->v_type != VFIFO, ("Unexpected fifo."));
1134 finit(fp, flags & FMASK, DTYPE_VNODE, vp, &vnops);
1138 if (flags & (O_EXLOCK | O_SHLOCK)) {
1139 lf.l_whence = SEEK_SET;
1142 if (flags & O_EXLOCK)
1143 lf.l_type = F_WRLCK;
1145 lf.l_type = F_RDLCK;
1147 if ((flags & FNONBLOCK) == 0)
1149 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf,
1152 atomic_set_int(&fp->f_flag, FHASLOCK);
1154 if (flags & O_TRUNC) {
1155 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
1159 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1161 error = mac_vnode_check_write(td->td_ucred, fp->f_cred, vp);
1164 error = VOP_SETATTR(vp, &vat, td->td_ucred);
1166 vn_finished_write(mp);
1170 VFS_UNLOCK_GIANT(vfslocked);
1172 * Release our private reference, leaving the one associated with
1173 * the descriptor table intact.
1176 td->td_retval[0] = indx;
1179 VFS_UNLOCK_GIANT(vfslocked);
1180 fdclose(fdp, fp, indx, td);
1189 #ifndef _SYS_SYSPROTO_H_
1190 struct ocreat_args {
1198 register struct ocreat_args /* {
1204 return (kern_open(td, uap->path, UIO_USERSPACE,
1205 O_WRONLY | O_CREAT | O_TRUNC, uap->mode));
1207 #endif /* COMPAT_43 */
1210 * Create a special file.
1212 #ifndef _SYS_SYSPROTO_H_
1222 register struct mknod_args /* {
1229 return (kern_mknod(td, uap->path, UIO_USERSPACE, uap->mode, uap->dev));
1232 #ifndef _SYS_SYSPROTO_H_
1233 struct mknodat_args {
1241 mknodat(struct thread *td, struct mknodat_args *uap)
1244 return (kern_mknodat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode,
1249 kern_mknod(struct thread *td, char *path, enum uio_seg pathseg, int mode,
1253 return (kern_mknodat(td, AT_FDCWD, path, pathseg, mode, dev));
1257 kern_mknodat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1265 struct nameidata nd;
1268 AUDIT_ARG_MODE(mode);
1270 switch (mode & S_IFMT) {
1273 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
1276 error = priv_check(td, PRIV_VFS_MKNOD_BAD);
1279 error = priv_check(td, PRIV_VFS_MKNOD_WHT);
1283 return (kern_mkfifoat(td, fd, path, pathseg, mode));
1293 NDINIT_AT(&nd, CREATE, LOCKPARENT | SAVENAME | MPSAFE | AUDITVNODE1,
1294 pathseg, path, fd, td);
1295 if ((error = namei(&nd)) != 0)
1297 vfslocked = NDHASGIANT(&nd);
1300 NDFREE(&nd, NDF_ONLY_PNBUF);
1301 if (vp == nd.ni_dvp)
1306 VFS_UNLOCK_GIANT(vfslocked);
1310 vattr.va_mode = (mode & ALLPERMS) &
1311 ~td->td_proc->p_fd->fd_cmask;
1312 vattr.va_rdev = dev;
1315 switch (mode & S_IFMT) {
1316 case S_IFMT: /* used by badsect to flag bad sectors */
1317 vattr.va_type = VBAD;
1320 vattr.va_type = VCHR;
1323 vattr.va_type = VBLK;
1329 panic("kern_mknod: invalid mode");
1332 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1333 NDFREE(&nd, NDF_ONLY_PNBUF);
1335 VFS_UNLOCK_GIANT(vfslocked);
1336 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1341 if (error == 0 && !whiteout)
1342 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp,
1343 &nd.ni_cnd, &vattr);
1347 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, CREATE);
1349 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp,
1350 &nd.ni_cnd, &vattr);
1355 NDFREE(&nd, NDF_ONLY_PNBUF);
1357 vn_finished_write(mp);
1358 VFS_UNLOCK_GIANT(vfslocked);
1363 * Create a named pipe.
1365 #ifndef _SYS_SYSPROTO_H_
1366 struct mkfifo_args {
1374 register struct mkfifo_args /* {
1380 return (kern_mkfifo(td, uap->path, UIO_USERSPACE, uap->mode));
1383 #ifndef _SYS_SYSPROTO_H_
1384 struct mkfifoat_args {
1391 mkfifoat(struct thread *td, struct mkfifoat_args *uap)
1394 return (kern_mkfifoat(td, uap->fd, uap->path, UIO_USERSPACE,
1399 kern_mkfifo(struct thread *td, char *path, enum uio_seg pathseg, int mode)
1402 return (kern_mkfifoat(td, AT_FDCWD, path, pathseg, mode));
1406 kern_mkfifoat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1412 struct nameidata nd;
1415 AUDIT_ARG_MODE(mode);
1418 NDINIT_AT(&nd, CREATE, LOCKPARENT | SAVENAME | MPSAFE | AUDITVNODE1,
1419 pathseg, path, fd, td);
1420 if ((error = namei(&nd)) != 0)
1422 vfslocked = NDHASGIANT(&nd);
1423 if (nd.ni_vp != NULL) {
1424 NDFREE(&nd, NDF_ONLY_PNBUF);
1425 if (nd.ni_vp == nd.ni_dvp)
1430 VFS_UNLOCK_GIANT(vfslocked);
1433 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1434 NDFREE(&nd, NDF_ONLY_PNBUF);
1436 VFS_UNLOCK_GIANT(vfslocked);
1437 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1442 vattr.va_type = VFIFO;
1443 vattr.va_mode = (mode & ALLPERMS) & ~td->td_proc->p_fd->fd_cmask;
1445 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1450 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
1457 vn_finished_write(mp);
1458 VFS_UNLOCK_GIANT(vfslocked);
1459 NDFREE(&nd, NDF_ONLY_PNBUF);
1464 * Make a hard file link.
1466 #ifndef _SYS_SYSPROTO_H_
1475 register struct link_args /* {
1481 return (kern_link(td, uap->path, uap->link, UIO_USERSPACE));
1484 #ifndef _SYS_SYSPROTO_H_
1485 struct linkat_args {
1494 linkat(struct thread *td, struct linkat_args *uap)
1499 if (flag & ~AT_SYMLINK_FOLLOW)
1502 return (kern_linkat(td, uap->fd1, uap->fd2, uap->path1, uap->path2,
1503 UIO_USERSPACE, (flag & AT_SYMLINK_FOLLOW) ? FOLLOW : NOFOLLOW));
1506 int hardlink_check_uid = 0;
1507 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
1508 &hardlink_check_uid, 0,
1509 "Unprivileged processes cannot create hard links to files owned by other "
1511 static int hardlink_check_gid = 0;
1512 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
1513 &hardlink_check_gid, 0,
1514 "Unprivileged processes cannot create hard links to files owned by other "
1518 can_hardlink(struct vnode *vp, struct ucred *cred)
1523 if (!hardlink_check_uid && !hardlink_check_gid)
1526 error = VOP_GETATTR(vp, &va, cred);
1530 if (hardlink_check_uid && cred->cr_uid != va.va_uid) {
1531 error = priv_check_cred(cred, PRIV_VFS_LINK, 0);
1536 if (hardlink_check_gid && !groupmember(va.va_gid, cred)) {
1537 error = priv_check_cred(cred, PRIV_VFS_LINK, 0);
1546 kern_link(struct thread *td, char *path, char *link, enum uio_seg segflg)
1549 return (kern_linkat(td, AT_FDCWD, AT_FDCWD, path,link, segflg, FOLLOW));
1553 kern_linkat(struct thread *td, int fd1, int fd2, char *path1, char *path2,
1554 enum uio_seg segflg, int follow)
1558 struct nameidata nd;
1564 NDINIT_AT(&nd, LOOKUP, follow | MPSAFE | AUDITVNODE1, segflg, path1,
1567 if ((error = namei(&nd)) != 0)
1569 vfslocked = NDHASGIANT(&nd);
1570 NDFREE(&nd, NDF_ONLY_PNBUF);
1572 if (vp->v_type == VDIR) {
1574 VFS_UNLOCK_GIANT(vfslocked);
1575 return (EPERM); /* POSIX */
1577 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0) {
1579 VFS_UNLOCK_GIANT(vfslocked);
1582 NDINIT_AT(&nd, CREATE, LOCKPARENT | SAVENAME | MPSAFE | AUDITVNODE2,
1583 segflg, path2, fd2, td);
1584 if ((error = namei(&nd)) == 0) {
1585 lvfslocked = NDHASGIANT(&nd);
1586 if (nd.ni_vp != NULL) {
1587 if (nd.ni_dvp == nd.ni_vp)
1593 } else if ((error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY))
1595 error = can_hardlink(vp, td->td_ucred);
1598 error = mac_vnode_check_link(td->td_ucred,
1599 nd.ni_dvp, vp, &nd.ni_cnd);
1602 error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
1606 NDFREE(&nd, NDF_ONLY_PNBUF);
1607 VFS_UNLOCK_GIANT(lvfslocked);
1610 vn_finished_write(mp);
1611 VFS_UNLOCK_GIANT(vfslocked);
1616 * Make a symbolic link.
1618 #ifndef _SYS_SYSPROTO_H_
1619 struct symlink_args {
1627 register struct symlink_args /* {
1633 return (kern_symlink(td, uap->path, uap->link, UIO_USERSPACE));
1636 #ifndef _SYS_SYSPROTO_H_
1637 struct symlinkat_args {
1644 symlinkat(struct thread *td, struct symlinkat_args *uap)
1647 return (kern_symlinkat(td, uap->path1, uap->fd, uap->path2,
1652 kern_symlink(struct thread *td, char *path, char *link, enum uio_seg segflg)
1655 return (kern_symlinkat(td, path, AT_FDCWD, link, segflg));
1659 kern_symlinkat(struct thread *td, char *path1, int fd, char *path2,
1660 enum uio_seg segflg)
1666 struct nameidata nd;
1669 if (segflg == UIO_SYSSPACE) {
1672 syspath = uma_zalloc(namei_zone, M_WAITOK);
1673 if ((error = copyinstr(path1, syspath, MAXPATHLEN, NULL)) != 0)
1676 AUDIT_ARG_TEXT(syspath);
1679 NDINIT_AT(&nd, CREATE, LOCKPARENT | SAVENAME | MPSAFE | AUDITVNODE1,
1680 segflg, path2, fd, td);
1681 if ((error = namei(&nd)) != 0)
1683 vfslocked = NDHASGIANT(&nd);
1685 NDFREE(&nd, NDF_ONLY_PNBUF);
1686 if (nd.ni_vp == nd.ni_dvp)
1691 VFS_UNLOCK_GIANT(vfslocked);
1695 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1696 NDFREE(&nd, NDF_ONLY_PNBUF);
1698 VFS_UNLOCK_GIANT(vfslocked);
1699 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1704 vattr.va_mode = ACCESSPERMS &~ td->td_proc->p_fd->fd_cmask;
1706 vattr.va_type = VLNK;
1707 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1712 error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr, syspath);
1718 NDFREE(&nd, NDF_ONLY_PNBUF);
1720 vn_finished_write(mp);
1721 VFS_UNLOCK_GIANT(vfslocked);
1723 if (segflg != UIO_SYSSPACE)
1724 uma_zfree(namei_zone, syspath);
1729 * Delete a whiteout from the filesystem.
1734 register struct undelete_args /* {
1740 struct nameidata nd;
1745 NDINIT(&nd, DELETE, LOCKPARENT | DOWHITEOUT | MPSAFE | AUDITVNODE1,
1746 UIO_USERSPACE, uap->path, td);
1750 vfslocked = NDHASGIANT(&nd);
1752 if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & ISWHITEOUT)) {
1753 NDFREE(&nd, NDF_ONLY_PNBUF);
1754 if (nd.ni_vp == nd.ni_dvp)
1760 VFS_UNLOCK_GIANT(vfslocked);
1763 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1764 NDFREE(&nd, NDF_ONLY_PNBUF);
1766 VFS_UNLOCK_GIANT(vfslocked);
1767 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1771 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, DELETE);
1772 NDFREE(&nd, NDF_ONLY_PNBUF);
1774 vn_finished_write(mp);
1775 VFS_UNLOCK_GIANT(vfslocked);
1780 * Delete a name from the filesystem.
1782 #ifndef _SYS_SYSPROTO_H_
1783 struct unlink_args {
1790 struct unlink_args /* {
1795 return (kern_unlink(td, uap->path, UIO_USERSPACE));
1798 #ifndef _SYS_SYSPROTO_H_
1799 struct unlinkat_args {
1806 unlinkat(struct thread *td, struct unlinkat_args *uap)
1808 int flag = uap->flag;
1810 char *path = uap->path;
1812 if (flag & ~AT_REMOVEDIR)
1815 if (flag & AT_REMOVEDIR)
1816 return (kern_rmdirat(td, fd, path, UIO_USERSPACE));
1818 return (kern_unlinkat(td, fd, path, UIO_USERSPACE, 0));
1822 kern_unlink(struct thread *td, char *path, enum uio_seg pathseg)
1825 return (kern_unlinkat(td, AT_FDCWD, path, pathseg, 0));
1829 kern_unlinkat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1835 struct nameidata nd;
1841 NDINIT_AT(&nd, DELETE, LOCKPARENT | LOCKLEAF | MPSAFE | AUDITVNODE1,
1842 pathseg, path, fd, td);
1843 if ((error = namei(&nd)) != 0)
1844 return (error == EINVAL ? EPERM : error);
1845 vfslocked = NDHASGIANT(&nd);
1847 if (vp->v_type == VDIR && oldinum == 0) {
1848 error = EPERM; /* POSIX */
1849 } else if (oldinum != 0 &&
1850 ((error = vn_stat(vp, &sb, td->td_ucred, NOCRED, td)) == 0) &&
1851 sb.st_ino != oldinum) {
1852 error = EIDRM; /* Identifier removed */
1855 * The root of a mounted filesystem cannot be deleted.
1857 * XXX: can this only be a VDIR case?
1859 if (vp->v_vflag & VV_ROOT)
1863 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1864 NDFREE(&nd, NDF_ONLY_PNBUF);
1866 if (vp == nd.ni_dvp)
1870 VFS_UNLOCK_GIANT(vfslocked);
1871 if ((error = vn_start_write(NULL, &mp,
1872 V_XSLEEP | PCATCH)) != 0)
1877 error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp,
1882 error = VOP_REMOVE(nd.ni_dvp, vp, &nd.ni_cnd);
1886 vn_finished_write(mp);
1888 NDFREE(&nd, NDF_ONLY_PNBUF);
1890 if (vp == nd.ni_dvp)
1894 VFS_UNLOCK_GIANT(vfslocked);
1899 * Reposition read/write file offset.
1901 #ifndef _SYS_SYSPROTO_H_
1912 register struct lseek_args /* {
1919 struct ucred *cred = td->td_ucred;
1927 AUDIT_ARG_FD(uap->fd);
1928 if ((error = fget(td, uap->fd, &fp)) != 0)
1930 if (!(fp->f_ops->fo_flags & DFLAG_SEEKABLE)) {
1935 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
1936 noneg = (vp->v_type != VCHR);
1937 offset = uap->offset;
1938 switch (uap->whence) {
1941 (fp->f_offset < 0 ||
1942 (offset > 0 && fp->f_offset > OFF_MAX - offset))) {
1946 offset += fp->f_offset;
1949 vn_lock(vp, LK_SHARED | LK_RETRY);
1950 error = VOP_GETATTR(vp, &vattr, cred);
1955 (vattr.va_size > OFF_MAX ||
1956 (offset > 0 && vattr.va_size > OFF_MAX - offset))) {
1960 offset += vattr.va_size;
1965 error = fo_ioctl(fp, FIOSEEKDATA, &offset, cred, td);
1968 error = fo_ioctl(fp, FIOSEEKHOLE, &offset, cred, td);
1973 if (error == 0 && noneg && offset < 0)
1977 fp->f_offset = offset;
1978 *(off_t *)(td->td_retval) = fp->f_offset;
1981 VFS_UNLOCK_GIANT(vfslocked);
1985 #if defined(COMPAT_43)
1987 * Reposition read/write file offset.
1989 #ifndef _SYS_SYSPROTO_H_
1990 struct olseek_args {
1999 register struct olseek_args /* {
2005 struct lseek_args /* {
2013 nuap.offset = uap->offset;
2014 nuap.whence = uap->whence;
2015 return (lseek(td, &nuap));
2017 #endif /* COMPAT_43 */
2019 /* Version with the 'pad' argument */
2021 freebsd6_lseek(td, uap)
2023 register struct freebsd6_lseek_args *uap;
2025 struct lseek_args ouap;
2028 ouap.offset = uap->offset;
2029 ouap.whence = uap->whence;
2030 return (lseek(td, &ouap));
2034 * Check access permissions using passed credentials.
2037 vn_access(vp, user_flags, cred, td)
2046 /* Flags == 0 means only check for existence. */
2050 if (user_flags & R_OK)
2052 if (user_flags & W_OK)
2054 if (user_flags & X_OK)
2057 error = mac_vnode_check_access(cred, vp, accmode);
2061 if ((accmode & VWRITE) == 0 || (error = vn_writechk(vp)) == 0)
2062 error = VOP_ACCESS(vp, accmode, cred, td);
2068 * Check access permissions using "real" credentials.
2070 #ifndef _SYS_SYSPROTO_H_
2071 struct access_args {
2079 register struct access_args /* {
2085 return (kern_access(td, uap->path, UIO_USERSPACE, uap->flags));
2088 #ifndef _SYS_SYSPROTO_H_
2089 struct faccessat_args {
2097 faccessat(struct thread *td, struct faccessat_args *uap)
2100 if (uap->flag & ~AT_EACCESS)
2102 return (kern_accessat(td, uap->fd, uap->path, UIO_USERSPACE, uap->flag,
2107 kern_access(struct thread *td, char *path, enum uio_seg pathseg, int mode)
2110 return (kern_accessat(td, AT_FDCWD, path, pathseg, 0, mode));
2114 kern_accessat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2115 int flags, int mode)
2117 struct ucred *cred, *tmpcred;
2119 struct nameidata nd;
2124 * Create and modify a temporary credential instead of one that
2125 * is potentially shared.
2127 if (!(flags & AT_EACCESS)) {
2128 cred = td->td_ucred;
2129 tmpcred = crdup(cred);
2130 tmpcred->cr_uid = cred->cr_ruid;
2131 tmpcred->cr_groups[0] = cred->cr_rgid;
2132 td->td_ucred = tmpcred;
2134 cred = tmpcred = td->td_ucred;
2135 AUDIT_ARG_VALUE(mode);
2136 NDINIT_AT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | MPSAFE |
2137 AUDITVNODE1, pathseg, path, fd, td);
2138 if ((error = namei(&nd)) != 0)
2140 vfslocked = NDHASGIANT(&nd);
2143 error = vn_access(vp, mode, tmpcred, td);
2144 NDFREE(&nd, NDF_ONLY_PNBUF);
2146 VFS_UNLOCK_GIANT(vfslocked);
2148 if (!(flags & AT_EACCESS)) {
2149 td->td_ucred = cred;
2156 * Check access permissions using "effective" credentials.
2158 #ifndef _SYS_SYSPROTO_H_
2159 struct eaccess_args {
2167 register struct eaccess_args /* {
2173 return (kern_eaccess(td, uap->path, UIO_USERSPACE, uap->flags));
2177 kern_eaccess(struct thread *td, char *path, enum uio_seg pathseg, int flags)
2180 return (kern_accessat(td, AT_FDCWD, path, pathseg, AT_EACCESS, flags));
2183 #if defined(COMPAT_43)
2185 * Get file status; this version follows links.
2187 #ifndef _SYS_SYSPROTO_H_
2196 register struct ostat_args /* {
2205 error = kern_stat(td, uap->path, UIO_USERSPACE, &sb);
2209 error = copyout(&osb, uap->ub, sizeof (osb));
2214 * Get file status; this version does not follow links.
2216 #ifndef _SYS_SYSPROTO_H_
2217 struct olstat_args {
2225 register struct olstat_args /* {
2234 error = kern_lstat(td, uap->path, UIO_USERSPACE, &sb);
2238 error = copyout(&osb, uap->ub, sizeof (osb));
2243 * Convert from an old to a new stat structure.
2251 ost->st_dev = st->st_dev;
2252 ost->st_ino = st->st_ino;
2253 ost->st_mode = st->st_mode;
2254 ost->st_nlink = st->st_nlink;
2255 ost->st_uid = st->st_uid;
2256 ost->st_gid = st->st_gid;
2257 ost->st_rdev = st->st_rdev;
2258 if (st->st_size < (quad_t)1 << 32)
2259 ost->st_size = st->st_size;
2262 ost->st_atime = st->st_atime;
2263 ost->st_mtime = st->st_mtime;
2264 ost->st_ctime = st->st_ctime;
2265 ost->st_blksize = st->st_blksize;
2266 ost->st_blocks = st->st_blocks;
2267 ost->st_flags = st->st_flags;
2268 ost->st_gen = st->st_gen;
2270 #endif /* COMPAT_43 */
2273 * Get file status; this version follows links.
2275 #ifndef _SYS_SYSPROTO_H_
2284 register struct stat_args /* {
2292 error = kern_stat(td, uap->path, UIO_USERSPACE, &sb);
2294 error = copyout(&sb, uap->ub, sizeof (sb));
2298 #ifndef _SYS_SYSPROTO_H_
2299 struct fstatat_args {
2307 fstatat(struct thread *td, struct fstatat_args *uap)
2312 error = kern_statat(td, uap->flag, uap->fd, uap->path,
2313 UIO_USERSPACE, &sb);
2315 error = copyout(&sb, uap->buf, sizeof (sb));
2320 kern_stat(struct thread *td, char *path, enum uio_seg pathseg, struct stat *sbp)
2323 return (kern_statat(td, 0, AT_FDCWD, path, pathseg, sbp));
2327 kern_statat(struct thread *td, int flag, int fd, char *path,
2328 enum uio_seg pathseg, struct stat *sbp)
2331 return (kern_statat_vnhook(td, flag, fd, path, pathseg, sbp, NULL));
2335 kern_statat_vnhook(struct thread *td, int flag, int fd, char *path,
2336 enum uio_seg pathseg, struct stat *sbp,
2337 void (*hook)(struct vnode *vp, struct stat *sbp))
2339 struct nameidata nd;
2341 int error, vfslocked;
2343 if (flag & ~AT_SYMLINK_NOFOLLOW)
2346 NDINIT_AT(&nd, LOOKUP, ((flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW :
2347 FOLLOW) | LOCKSHARED | LOCKLEAF | AUDITVNODE1 | MPSAFE, pathseg,
2350 if ((error = namei(&nd)) != 0)
2352 vfslocked = NDHASGIANT(&nd);
2353 error = vn_stat(nd.ni_vp, &sb, td->td_ucred, NOCRED, td);
2355 SDT_PROBE(vfs, , stat, mode, path, sb.st_mode, 0, 0, 0);
2356 if (S_ISREG(sb.st_mode))
2357 SDT_PROBE(vfs, , stat, reg, path, pathseg, 0, 0, 0);
2358 if (__predict_false(hook != NULL))
2359 hook(nd.ni_vp, &sb);
2361 NDFREE(&nd, NDF_ONLY_PNBUF);
2363 VFS_UNLOCK_GIANT(vfslocked);
2368 if (KTRPOINT(td, KTR_STRUCT))
2375 * Get file status; this version does not follow links.
2377 #ifndef _SYS_SYSPROTO_H_
2386 register struct lstat_args /* {
2394 error = kern_lstat(td, uap->path, UIO_USERSPACE, &sb);
2396 error = copyout(&sb, uap->ub, sizeof (sb));
2401 kern_lstat(struct thread *td, char *path, enum uio_seg pathseg, struct stat *sbp)
2404 return (kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, path, pathseg,
2409 * Implementation of the NetBSD [l]stat() functions.
2416 bzero(nsb, sizeof *nsb);
2417 nsb->st_dev = sb->st_dev;
2418 nsb->st_ino = sb->st_ino;
2419 nsb->st_mode = sb->st_mode;
2420 nsb->st_nlink = sb->st_nlink;
2421 nsb->st_uid = sb->st_uid;
2422 nsb->st_gid = sb->st_gid;
2423 nsb->st_rdev = sb->st_rdev;
2424 nsb->st_atimespec = sb->st_atimespec;
2425 nsb->st_mtimespec = sb->st_mtimespec;
2426 nsb->st_ctimespec = sb->st_ctimespec;
2427 nsb->st_size = sb->st_size;
2428 nsb->st_blocks = sb->st_blocks;
2429 nsb->st_blksize = sb->st_blksize;
2430 nsb->st_flags = sb->st_flags;
2431 nsb->st_gen = sb->st_gen;
2432 nsb->st_birthtimespec = sb->st_birthtimespec;
2435 #ifndef _SYS_SYSPROTO_H_
2444 register struct nstat_args /* {
2453 error = kern_stat(td, uap->path, UIO_USERSPACE, &sb);
2456 cvtnstat(&sb, &nsb);
2457 error = copyout(&nsb, uap->ub, sizeof (nsb));
2462 * NetBSD lstat. Get file status; this version does not follow links.
2464 #ifndef _SYS_SYSPROTO_H_
2473 register struct nlstat_args /* {
2482 error = kern_lstat(td, uap->path, UIO_USERSPACE, &sb);
2485 cvtnstat(&sb, &nsb);
2486 error = copyout(&nsb, uap->ub, sizeof (nsb));
2491 * Get configurable pathname variables.
2493 #ifndef _SYS_SYSPROTO_H_
2494 struct pathconf_args {
2502 register struct pathconf_args /* {
2508 return (kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name, FOLLOW));
2511 #ifndef _SYS_SYSPROTO_H_
2512 struct lpathconf_args {
2520 register struct lpathconf_args /* {
2526 return (kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name, NOFOLLOW));
2530 kern_pathconf(struct thread *td, char *path, enum uio_seg pathseg, int name,
2533 struct nameidata nd;
2534 int error, vfslocked;
2536 NDINIT(&nd, LOOKUP, LOCKSHARED | LOCKLEAF | MPSAFE | AUDITVNODE1 |
2537 flags, pathseg, path, td);
2538 if ((error = namei(&nd)) != 0)
2540 vfslocked = NDHASGIANT(&nd);
2541 NDFREE(&nd, NDF_ONLY_PNBUF);
2543 /* If asynchronous I/O is available, it works for all files. */
2544 if (name == _PC_ASYNC_IO)
2545 td->td_retval[0] = async_io_version;
2547 error = VOP_PATHCONF(nd.ni_vp, name, td->td_retval);
2549 VFS_UNLOCK_GIANT(vfslocked);
2554 * Return target name of a symbolic link.
2556 #ifndef _SYS_SYSPROTO_H_
2557 struct readlink_args {
2566 register struct readlink_args /* {
2573 return (kern_readlink(td, uap->path, UIO_USERSPACE, uap->buf,
2574 UIO_USERSPACE, uap->count));
2576 #ifndef _SYS_SYSPROTO_H_
2577 struct readlinkat_args {
2585 readlinkat(struct thread *td, struct readlinkat_args *uap)
2588 return (kern_readlinkat(td, uap->fd, uap->path, UIO_USERSPACE,
2589 uap->buf, UIO_USERSPACE, uap->bufsize));
2593 kern_readlink(struct thread *td, char *path, enum uio_seg pathseg, char *buf,
2594 enum uio_seg bufseg, size_t count)
2597 return (kern_readlinkat(td, AT_FDCWD, path, pathseg, buf, bufseg,
2602 kern_readlinkat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2603 char *buf, enum uio_seg bufseg, size_t count)
2609 struct nameidata nd;
2612 if (count > INT_MAX)
2615 NDINIT_AT(&nd, LOOKUP, NOFOLLOW | LOCKSHARED | LOCKLEAF | MPSAFE |
2616 AUDITVNODE1, pathseg, path, fd, td);
2618 if ((error = namei(&nd)) != 0)
2620 NDFREE(&nd, NDF_ONLY_PNBUF);
2621 vfslocked = NDHASGIANT(&nd);
2624 error = mac_vnode_check_readlink(td->td_ucred, vp);
2627 VFS_UNLOCK_GIANT(vfslocked);
2631 if (vp->v_type != VLNK)
2634 aiov.iov_base = buf;
2635 aiov.iov_len = count;
2636 auio.uio_iov = &aiov;
2637 auio.uio_iovcnt = 1;
2638 auio.uio_offset = 0;
2639 auio.uio_rw = UIO_READ;
2640 auio.uio_segflg = bufseg;
2642 auio.uio_resid = count;
2643 error = VOP_READLINK(vp, &auio, td->td_ucred);
2646 VFS_UNLOCK_GIANT(vfslocked);
2647 td->td_retval[0] = count - auio.uio_resid;
2652 * Common implementation code for chflags() and fchflags().
2655 setfflags(td, vp, flags)
2665 * Prevent non-root users from setting flags on devices. When
2666 * a device is reused, users can retain ownership of the device
2667 * if they are allowed to set flags and programs assume that
2668 * chown can't fail when done as root.
2670 if (vp->v_type == VCHR || vp->v_type == VBLK) {
2671 error = priv_check(td, PRIV_VFS_CHFLAGS_DEV);
2676 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2678 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2680 vattr.va_flags = flags;
2682 error = mac_vnode_check_setflags(td->td_ucred, vp, vattr.va_flags);
2685 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
2687 vn_finished_write(mp);
2692 * Change flags of a file given a path name.
2694 #ifndef _SYS_SYSPROTO_H_
2695 struct chflags_args {
2703 register struct chflags_args /* {
2709 struct nameidata nd;
2712 AUDIT_ARG_FFLAGS(uap->flags);
2713 NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE | AUDITVNODE1, UIO_USERSPACE,
2715 if ((error = namei(&nd)) != 0)
2717 NDFREE(&nd, NDF_ONLY_PNBUF);
2718 vfslocked = NDHASGIANT(&nd);
2719 error = setfflags(td, nd.ni_vp, uap->flags);
2721 VFS_UNLOCK_GIANT(vfslocked);
2726 * Same as chflags() but doesn't follow symlinks.
2731 register struct lchflags_args /* {
2737 struct nameidata nd;
2740 AUDIT_ARG_FFLAGS(uap->flags);
2741 NDINIT(&nd, LOOKUP, NOFOLLOW | MPSAFE | AUDITVNODE1, UIO_USERSPACE,
2743 if ((error = namei(&nd)) != 0)
2745 vfslocked = NDHASGIANT(&nd);
2746 NDFREE(&nd, NDF_ONLY_PNBUF);
2747 error = setfflags(td, nd.ni_vp, uap->flags);
2749 VFS_UNLOCK_GIANT(vfslocked);
2754 * Change flags of a file given a file descriptor.
2756 #ifndef _SYS_SYSPROTO_H_
2757 struct fchflags_args {
2765 register struct fchflags_args /* {
2774 AUDIT_ARG_FD(uap->fd);
2775 AUDIT_ARG_FFLAGS(uap->flags);
2776 if ((error = getvnode(td->td_proc->p_fd, uap->fd, &fp)) != 0)
2778 vfslocked = VFS_LOCK_GIANT(fp->f_vnode->v_mount);
2780 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
2781 AUDIT_ARG_VNODE1(fp->f_vnode);
2782 VOP_UNLOCK(fp->f_vnode, 0);
2784 error = setfflags(td, fp->f_vnode, uap->flags);
2785 VFS_UNLOCK_GIANT(vfslocked);
2791 * Common implementation code for chmod(), lchmod() and fchmod().
2794 setfmode(td, vp, mode)
2803 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2805 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2807 vattr.va_mode = mode & ALLPERMS;
2809 error = mac_vnode_check_setmode(td->td_ucred, vp, vattr.va_mode);
2812 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
2814 vn_finished_write(mp);
2819 * Change mode of a file given path name.
2821 #ifndef _SYS_SYSPROTO_H_
2830 register struct chmod_args /* {
2836 return (kern_chmod(td, uap->path, UIO_USERSPACE, uap->mode));
2839 #ifndef _SYS_SYSPROTO_H_
2840 struct fchmodat_args {
2848 fchmodat(struct thread *td, struct fchmodat_args *uap)
2850 int flag = uap->flag;
2852 char *path = uap->path;
2853 mode_t mode = uap->mode;
2855 if (flag & ~AT_SYMLINK_NOFOLLOW)
2858 return (kern_fchmodat(td, fd, path, UIO_USERSPACE, mode, flag));
2862 kern_chmod(struct thread *td, char *path, enum uio_seg pathseg, int mode)
2865 return (kern_fchmodat(td, AT_FDCWD, path, pathseg, mode, 0));
2869 * Change mode of a file given path name (don't follow links.)
2871 #ifndef _SYS_SYSPROTO_H_
2872 struct lchmod_args {
2880 register struct lchmod_args /* {
2886 return (kern_fchmodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2887 uap->mode, AT_SYMLINK_NOFOLLOW));
2892 kern_fchmodat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2893 mode_t mode, int flag)
2896 struct nameidata nd;
2900 AUDIT_ARG_MODE(mode);
2901 follow = (flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
2902 NDINIT_AT(&nd, LOOKUP, follow | MPSAFE | AUDITVNODE1, pathseg, path,
2904 if ((error = namei(&nd)) != 0)
2906 vfslocked = NDHASGIANT(&nd);
2907 NDFREE(&nd, NDF_ONLY_PNBUF);
2908 error = setfmode(td, nd.ni_vp, mode);
2910 VFS_UNLOCK_GIANT(vfslocked);
2915 * Change mode of a file given a file descriptor.
2917 #ifndef _SYS_SYSPROTO_H_
2918 struct fchmod_args {
2926 register struct fchmod_args /* {
2935 AUDIT_ARG_FD(uap->fd);
2936 AUDIT_ARG_MODE(uap->mode);
2937 if ((error = getvnode(td->td_proc->p_fd, uap->fd, &fp)) != 0)
2939 vfslocked = VFS_LOCK_GIANT(fp->f_vnode->v_mount);
2941 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
2942 AUDIT_ARG_VNODE1(fp->f_vnode);
2943 VOP_UNLOCK(fp->f_vnode, 0);
2945 error = setfmode(td, fp->f_vnode, uap->mode);
2946 VFS_UNLOCK_GIANT(vfslocked);
2952 * Common implementation for chown(), lchown(), and fchown()
2955 setfown(td, vp, uid, gid)
2965 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2967 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2972 error = mac_vnode_check_setowner(td->td_ucred, vp, vattr.va_uid,
2976 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
2978 vn_finished_write(mp);
2983 * Set ownership given a path name.
2985 #ifndef _SYS_SYSPROTO_H_
2995 register struct chown_args /* {
3002 return (kern_chown(td, uap->path, UIO_USERSPACE, uap->uid, uap->gid));
3005 #ifndef _SYS_SYSPROTO_H_
3006 struct fchownat_args {
3015 fchownat(struct thread *td, struct fchownat_args *uap)
3020 if (flag & ~AT_SYMLINK_NOFOLLOW)
3023 return (kern_fchownat(td, uap->fd, uap->path, UIO_USERSPACE, uap->uid,
3024 uap->gid, uap->flag));
3028 kern_chown(struct thread *td, char *path, enum uio_seg pathseg, int uid,
3032 return (kern_fchownat(td, AT_FDCWD, path, pathseg, uid, gid, 0));
3036 kern_fchownat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
3037 int uid, int gid, int flag)
3039 struct nameidata nd;
3040 int error, vfslocked, follow;
3042 AUDIT_ARG_OWNER(uid, gid);
3043 follow = (flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
3044 NDINIT_AT(&nd, LOOKUP, follow | MPSAFE | AUDITVNODE1, pathseg, path,
3047 if ((error = namei(&nd)) != 0)
3049 vfslocked = NDHASGIANT(&nd);
3050 NDFREE(&nd, NDF_ONLY_PNBUF);
3051 error = setfown(td, nd.ni_vp, uid, gid);
3053 VFS_UNLOCK_GIANT(vfslocked);
3058 * Set ownership given a path name, do not cross symlinks.
3060 #ifndef _SYS_SYSPROTO_H_
3061 struct lchown_args {
3070 register struct lchown_args /* {
3077 return (kern_lchown(td, uap->path, UIO_USERSPACE, uap->uid, uap->gid));
3081 kern_lchown(struct thread *td, char *path, enum uio_seg pathseg, int uid,
3085 return (kern_fchownat(td, AT_FDCWD, path, pathseg, uid, gid,
3086 AT_SYMLINK_NOFOLLOW));
3090 * Set ownership given a file descriptor.
3092 #ifndef _SYS_SYSPROTO_H_
3093 struct fchown_args {
3102 register struct fchown_args /* {
3112 AUDIT_ARG_FD(uap->fd);
3113 AUDIT_ARG_OWNER(uap->uid, uap->gid);
3114 if ((error = getvnode(td->td_proc->p_fd, uap->fd, &fp)) != 0)
3116 vfslocked = VFS_LOCK_GIANT(fp->f_vnode->v_mount);
3118 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
3119 AUDIT_ARG_VNODE1(fp->f_vnode);
3120 VOP_UNLOCK(fp->f_vnode, 0);
3122 error = setfown(td, fp->f_vnode, uap->uid, uap->gid);
3123 VFS_UNLOCK_GIANT(vfslocked);
3129 * Common implementation code for utimes(), lutimes(), and futimes().
3132 getutimes(usrtvp, tvpseg, tsp)
3133 const struct timeval *usrtvp;
3134 enum uio_seg tvpseg;
3135 struct timespec *tsp;
3137 struct timeval tv[2];
3138 const struct timeval *tvp;
3141 if (usrtvp == NULL) {
3142 vfs_timestamp(&tsp[0]);
3145 if (tvpseg == UIO_SYSSPACE) {
3148 if ((error = copyin(usrtvp, tv, sizeof(tv))) != 0)
3153 if (tvp[0].tv_usec < 0 || tvp[0].tv_usec >= 1000000 ||
3154 tvp[1].tv_usec < 0 || tvp[1].tv_usec >= 1000000)
3156 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
3157 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
3163 * Common implementation code for utimes(), lutimes(), and futimes().
3166 setutimes(td, vp, ts, numtimes, nullflag)
3169 const struct timespec *ts;
3173 int error, setbirthtime;
3177 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
3179 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3181 if (numtimes < 3 && !VOP_GETATTR(vp, &vattr, td->td_ucred) &&
3182 timespeccmp(&ts[1], &vattr.va_birthtime, < ))
3185 vattr.va_atime = ts[0];
3186 vattr.va_mtime = ts[1];
3188 vattr.va_birthtime = ts[1];
3190 vattr.va_birthtime = ts[2];
3192 vattr.va_vaflags |= VA_UTIMES_NULL;
3194 error = mac_vnode_check_setutimes(td->td_ucred, vp, vattr.va_atime,
3198 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3200 vn_finished_write(mp);
3205 * Set the access and modification times of a file.
3207 #ifndef _SYS_SYSPROTO_H_
3208 struct utimes_args {
3210 struct timeval *tptr;
3216 register struct utimes_args /* {
3218 struct timeval *tptr;
3222 return (kern_utimes(td, uap->path, UIO_USERSPACE, uap->tptr,
3226 #ifndef _SYS_SYSPROTO_H_
3227 struct futimesat_args {
3230 const struct timeval * times;
3234 futimesat(struct thread *td, struct futimesat_args *uap)
3237 return (kern_utimesat(td, uap->fd, uap->path, UIO_USERSPACE,
3238 uap->times, UIO_USERSPACE));
3242 kern_utimes(struct thread *td, char *path, enum uio_seg pathseg,
3243 struct timeval *tptr, enum uio_seg tptrseg)
3246 return (kern_utimesat(td, AT_FDCWD, path, pathseg, tptr, tptrseg));
3250 kern_utimesat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
3251 struct timeval *tptr, enum uio_seg tptrseg)
3253 struct nameidata nd;
3254 struct timespec ts[2];
3255 int error, vfslocked;
3257 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3259 NDINIT_AT(&nd, LOOKUP, FOLLOW | MPSAFE | AUDITVNODE1, pathseg, path,
3262 if ((error = namei(&nd)) != 0)
3264 vfslocked = NDHASGIANT(&nd);
3265 NDFREE(&nd, NDF_ONLY_PNBUF);
3266 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3268 VFS_UNLOCK_GIANT(vfslocked);
3273 * Set the access and modification times of a file.
3275 #ifndef _SYS_SYSPROTO_H_
3276 struct lutimes_args {
3278 struct timeval *tptr;
3284 register struct lutimes_args /* {
3286 struct timeval *tptr;
3290 return (kern_lutimes(td, uap->path, UIO_USERSPACE, uap->tptr,
3295 kern_lutimes(struct thread *td, char *path, enum uio_seg pathseg,
3296 struct timeval *tptr, enum uio_seg tptrseg)
3298 struct timespec ts[2];
3300 struct nameidata nd;
3303 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3305 NDINIT(&nd, LOOKUP, NOFOLLOW | MPSAFE | AUDITVNODE1, pathseg, path, td);
3306 if ((error = namei(&nd)) != 0)
3308 vfslocked = NDHASGIANT(&nd);
3309 NDFREE(&nd, NDF_ONLY_PNBUF);
3310 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3312 VFS_UNLOCK_GIANT(vfslocked);
3317 * Set the access and modification times of a file.
3319 #ifndef _SYS_SYSPROTO_H_
3320 struct futimes_args {
3322 struct timeval *tptr;
3328 register struct futimes_args /* {
3330 struct timeval *tptr;
3334 return (kern_futimes(td, uap->fd, uap->tptr, UIO_USERSPACE));
3338 kern_futimes(struct thread *td, int fd, struct timeval *tptr,
3339 enum uio_seg tptrseg)
3341 struct timespec ts[2];
3347 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3349 if ((error = getvnode(td->td_proc->p_fd, fd, &fp)) != 0)
3351 vfslocked = VFS_LOCK_GIANT(fp->f_vnode->v_mount);
3353 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
3354 AUDIT_ARG_VNODE1(fp->f_vnode);
3355 VOP_UNLOCK(fp->f_vnode, 0);
3357 error = setutimes(td, fp->f_vnode, ts, 2, tptr == NULL);
3358 VFS_UNLOCK_GIANT(vfslocked);
3364 * Truncate a file given its path name.
3366 #ifndef _SYS_SYSPROTO_H_
3367 struct truncate_args {
3376 register struct truncate_args /* {
3383 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3387 kern_truncate(struct thread *td, char *path, enum uio_seg pathseg, off_t length)
3393 struct nameidata nd;
3398 NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE | AUDITVNODE1, pathseg, path, td);
3399 if ((error = namei(&nd)) != 0)
3401 vfslocked = NDHASGIANT(&nd);
3403 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0) {
3405 VFS_UNLOCK_GIANT(vfslocked);
3408 NDFREE(&nd, NDF_ONLY_PNBUF);
3409 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3410 if (vp->v_type == VDIR)
3413 else if ((error = mac_vnode_check_write(td->td_ucred, NOCRED, vp))) {
3416 else if ((error = vn_writechk(vp)) == 0 &&
3417 (error = VOP_ACCESS(vp, VWRITE, td->td_ucred, td)) == 0) {
3419 vattr.va_size = length;
3420 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3423 vn_finished_write(mp);
3424 VFS_UNLOCK_GIANT(vfslocked);
3428 #if defined(COMPAT_43)
3430 * Truncate a file given its path name.
3432 #ifndef _SYS_SYSPROTO_H_
3433 struct otruncate_args {
3441 register struct otruncate_args /* {
3446 struct truncate_args /* {
3452 nuap.path = uap->path;
3453 nuap.length = uap->length;
3454 return (truncate(td, &nuap));
3456 #endif /* COMPAT_43 */
3458 /* Versions with the pad argument */
3460 freebsd6_truncate(struct thread *td, struct freebsd6_truncate_args *uap)
3462 struct truncate_args ouap;
3464 ouap.path = uap->path;
3465 ouap.length = uap->length;
3466 return (truncate(td, &ouap));
3470 freebsd6_ftruncate(struct thread *td, struct freebsd6_ftruncate_args *uap)
3472 struct ftruncate_args ouap;
3475 ouap.length = uap->length;
3476 return (ftruncate(td, &ouap));
3480 * Sync an open file.
3482 #ifndef _SYS_SYSPROTO_H_
3490 struct fsync_args /* {
3498 int error, lock_flags;
3500 AUDIT_ARG_FD(uap->fd);
3501 if ((error = getvnode(td->td_proc->p_fd, uap->fd, &fp)) != 0)
3504 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
3505 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
3507 if (MNT_SHARED_WRITES(mp) ||
3508 ((mp == NULL) && MNT_SHARED_WRITES(vp->v_mount))) {
3509 lock_flags = LK_SHARED;
3511 lock_flags = LK_EXCLUSIVE;
3513 vn_lock(vp, lock_flags | LK_RETRY);
3514 AUDIT_ARG_VNODE1(vp);
3515 if (vp->v_object != NULL) {
3516 VM_OBJECT_LOCK(vp->v_object);
3517 vm_object_page_clean(vp->v_object, 0, 0, 0);
3518 VM_OBJECT_UNLOCK(vp->v_object);
3520 error = VOP_FSYNC(vp, MNT_WAIT, td);
3523 vn_finished_write(mp);
3525 VFS_UNLOCK_GIANT(vfslocked);
3531 * Rename files. Source and destination must either both be directories, or
3532 * both not be directories. If target is a directory, it must be empty.
3534 #ifndef _SYS_SYSPROTO_H_
3535 struct rename_args {
3543 register struct rename_args /* {
3549 return (kern_rename(td, uap->from, uap->to, UIO_USERSPACE));
3552 #ifndef _SYS_SYSPROTO_H_
3553 struct renameat_args {
3561 renameat(struct thread *td, struct renameat_args *uap)
3564 return (kern_renameat(td, uap->oldfd, uap->old, uap->newfd, uap->new,
3569 kern_rename(struct thread *td, char *from, char *to, enum uio_seg pathseg)
3572 return (kern_renameat(td, AT_FDCWD, from, AT_FDCWD, to, pathseg));
3576 kern_renameat(struct thread *td, int oldfd, char *old, int newfd, char *new,
3577 enum uio_seg pathseg)
3579 struct mount *mp = NULL;
3580 struct vnode *tvp, *fvp, *tdvp;
3581 struct nameidata fromnd, tond;
3588 NDINIT_AT(&fromnd, DELETE, LOCKPARENT | LOCKLEAF | SAVESTART | MPSAFE |
3589 AUDITVNODE1, pathseg, old, oldfd, td);
3591 NDINIT_AT(&fromnd, DELETE, WANTPARENT | SAVESTART | MPSAFE |
3592 AUDITVNODE1, pathseg, old, oldfd, td);
3595 if ((error = namei(&fromnd)) != 0)
3597 fvfslocked = NDHASGIANT(&fromnd);
3600 error = mac_vnode_check_rename_from(td->td_ucred, fromnd.ni_dvp,
3601 fromnd.ni_vp, &fromnd.ni_cnd);
3602 VOP_UNLOCK(fromnd.ni_dvp, 0);
3603 if (fromnd.ni_dvp != fromnd.ni_vp)
3604 VOP_UNLOCK(fromnd.ni_vp, 0);
3608 error = vn_start_write(fvp, &mp, V_WAIT | PCATCH);
3610 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3611 vrele(fromnd.ni_dvp);
3615 NDINIT_AT(&tond, RENAME, LOCKPARENT | LOCKLEAF | NOCACHE | SAVESTART |
3616 MPSAFE | AUDITVNODE2, pathseg, new, newfd, td);
3617 if (fromnd.ni_vp->v_type == VDIR)
3618 tond.ni_cnd.cn_flags |= WILLBEDIR;
3619 if ((error = namei(&tond)) != 0) {
3620 /* Translate error code for rename("dir1", "dir2/."). */
3621 if (error == EISDIR && fvp->v_type == VDIR)
3623 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3624 vrele(fromnd.ni_dvp);
3626 vn_finished_write(mp);
3629 tvfslocked = NDHASGIANT(&tond);
3633 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
3636 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
3646 * If the source is the same as the destination (that is, if they
3647 * are links to the same vnode), then there is nothing to do.
3653 error = mac_vnode_check_rename_to(td->td_ucred, tdvp,
3654 tond.ni_vp, fromnd.ni_dvp == tdvp, &tond.ni_cnd);
3658 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
3659 tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
3660 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3661 NDFREE(&tond, NDF_ONLY_PNBUF);
3663 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3664 NDFREE(&tond, NDF_ONLY_PNBUF);
3671 vrele(fromnd.ni_dvp);
3674 vrele(tond.ni_startdir);
3675 vn_finished_write(mp);
3677 if (fromnd.ni_startdir)
3678 vrele(fromnd.ni_startdir);
3679 VFS_UNLOCK_GIANT(fvfslocked);
3680 VFS_UNLOCK_GIANT(tvfslocked);
3687 * Make a directory file.
3689 #ifndef _SYS_SYSPROTO_H_
3698 register struct mkdir_args /* {
3704 return (kern_mkdir(td, uap->path, UIO_USERSPACE, uap->mode));
3707 #ifndef _SYS_SYSPROTO_H_
3708 struct mkdirat_args {
3715 mkdirat(struct thread *td, struct mkdirat_args *uap)
3718 return (kern_mkdirat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode));
3722 kern_mkdir(struct thread *td, char *path, enum uio_seg segflg, int mode)
3725 return (kern_mkdirat(td, AT_FDCWD, path, segflg, mode));
3729 kern_mkdirat(struct thread *td, int fd, char *path, enum uio_seg segflg,
3736 struct nameidata nd;
3739 AUDIT_ARG_MODE(mode);
3742 NDINIT_AT(&nd, CREATE, LOCKPARENT | SAVENAME | MPSAFE | AUDITVNODE1,
3743 segflg, path, fd, td);
3744 nd.ni_cnd.cn_flags |= WILLBEDIR;
3745 if ((error = namei(&nd)) != 0)
3747 vfslocked = NDHASGIANT(&nd);
3750 NDFREE(&nd, NDF_ONLY_PNBUF);
3752 * XXX namei called with LOCKPARENT but not LOCKLEAF has
3753 * the strange behaviour of leaving the vnode unlocked
3754 * if the target is the same vnode as the parent.
3756 if (vp == nd.ni_dvp)
3761 VFS_UNLOCK_GIANT(vfslocked);
3764 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3765 NDFREE(&nd, NDF_ONLY_PNBUF);
3767 VFS_UNLOCK_GIANT(vfslocked);
3768 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3773 vattr.va_type = VDIR;
3774 vattr.va_mode = (mode & ACCESSPERMS) &~ td->td_proc->p_fd->fd_cmask;
3776 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
3781 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
3785 NDFREE(&nd, NDF_ONLY_PNBUF);
3789 vn_finished_write(mp);
3790 VFS_UNLOCK_GIANT(vfslocked);
3795 * Remove a directory file.
3797 #ifndef _SYS_SYSPROTO_H_
3805 struct rmdir_args /* {
3810 return (kern_rmdir(td, uap->path, UIO_USERSPACE));
3814 kern_rmdir(struct thread *td, char *path, enum uio_seg pathseg)
3817 return (kern_rmdirat(td, AT_FDCWD, path, pathseg));
3821 kern_rmdirat(struct thread *td, int fd, char *path, enum uio_seg pathseg)
3826 struct nameidata nd;
3831 NDINIT_AT(&nd, DELETE, LOCKPARENT | LOCKLEAF | MPSAFE | AUDITVNODE1,
3832 pathseg, path, fd, td);
3833 if ((error = namei(&nd)) != 0)
3835 vfslocked = NDHASGIANT(&nd);
3837 if (vp->v_type != VDIR) {
3842 * No rmdir "." please.
3844 if (nd.ni_dvp == vp) {
3849 * The root of a mounted filesystem cannot be deleted.
3851 if (vp->v_vflag & VV_ROOT) {
3856 error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp,
3861 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3862 NDFREE(&nd, NDF_ONLY_PNBUF);
3864 if (nd.ni_dvp == vp)
3868 VFS_UNLOCK_GIANT(vfslocked);
3869 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3873 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
3874 vn_finished_write(mp);
3876 NDFREE(&nd, NDF_ONLY_PNBUF);
3878 if (nd.ni_dvp == vp)
3882 VFS_UNLOCK_GIANT(vfslocked);
3888 * Read a block of directory entries in a filesystem independent format.
3890 #ifndef _SYS_SYSPROTO_H_
3891 struct ogetdirentries_args {
3899 ogetdirentries(td, uap)
3901 register struct ogetdirentries_args /* {
3910 struct uio auio, kuio;
3911 struct iovec aiov, kiov;
3912 struct dirent *dp, *edp;
3914 int error, eofflag, readcnt, vfslocked;
3917 /* XXX arbitrary sanity limit on `count'. */
3918 if (uap->count > 64 * 1024)
3920 if ((error = getvnode(td->td_proc->p_fd, uap->fd, &fp)) != 0)
3922 if ((fp->f_flag & FREAD) == 0) {
3928 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
3929 if (vp->v_type != VDIR) {
3930 VFS_UNLOCK_GIANT(vfslocked);
3934 aiov.iov_base = uap->buf;
3935 aiov.iov_len = uap->count;
3936 auio.uio_iov = &aiov;
3937 auio.uio_iovcnt = 1;
3938 auio.uio_rw = UIO_READ;
3939 auio.uio_segflg = UIO_USERSPACE;
3941 auio.uio_resid = uap->count;
3942 vn_lock(vp, LK_SHARED | LK_RETRY);
3943 loff = auio.uio_offset = fp->f_offset;
3945 error = mac_vnode_check_readdir(td->td_ucred, vp);
3948 VFS_UNLOCK_GIANT(vfslocked);
3953 # if (BYTE_ORDER != LITTLE_ENDIAN)
3954 if (vp->v_mount->mnt_maxsymlinklen <= 0) {
3955 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag,
3957 fp->f_offset = auio.uio_offset;
3962 kuio.uio_iov = &kiov;
3963 kuio.uio_segflg = UIO_SYSSPACE;
3964 kiov.iov_len = uap->count;
3965 dirbuf = malloc(uap->count, M_TEMP, M_WAITOK);
3966 kiov.iov_base = dirbuf;
3967 error = VOP_READDIR(vp, &kuio, fp->f_cred, &eofflag,
3969 fp->f_offset = kuio.uio_offset;
3971 readcnt = uap->count - kuio.uio_resid;
3972 edp = (struct dirent *)&dirbuf[readcnt];
3973 for (dp = (struct dirent *)dirbuf; dp < edp; ) {
3974 # if (BYTE_ORDER == LITTLE_ENDIAN)
3976 * The expected low byte of
3977 * dp->d_namlen is our dp->d_type.
3978 * The high MBZ byte of dp->d_namlen
3979 * is our dp->d_namlen.
3981 dp->d_type = dp->d_namlen;
3985 * The dp->d_type is the high byte
3986 * of the expected dp->d_namlen,
3987 * so must be zero'ed.
3991 if (dp->d_reclen > 0) {
3992 dp = (struct dirent *)
3993 ((char *)dp + dp->d_reclen);
4000 error = uiomove(dirbuf, readcnt, &auio);
4002 free(dirbuf, M_TEMP);
4006 VFS_UNLOCK_GIANT(vfslocked);
4010 if (uap->count == auio.uio_resid &&
4011 (vp->v_vflag & VV_ROOT) &&
4012 (vp->v_mount->mnt_flag & MNT_UNION)) {
4013 struct vnode *tvp = vp;
4014 vp = vp->v_mount->mnt_vnodecovered;
4020 VFS_UNLOCK_GIANT(vfslocked);
4024 VFS_UNLOCK_GIANT(vfslocked);
4025 error = copyout(&loff, uap->basep, sizeof(long));
4027 td->td_retval[0] = uap->count - auio.uio_resid;
4030 #endif /* COMPAT_43 */
4033 * Read a block of directory entries in a filesystem independent format.
4035 #ifndef _SYS_SYSPROTO_H_
4036 struct getdirentries_args {
4044 getdirentries(td, uap)
4046 register struct getdirentries_args /* {
4056 error = kern_getdirentries(td, uap->fd, uap->buf, uap->count, &base);
4059 if (uap->basep != NULL)
4060 error = copyout(&base, uap->basep, sizeof(long));
4065 kern_getdirentries(struct thread *td, int fd, char *buf, u_int count,
4077 if (count > INT_MAX)
4079 if ((error = getvnode(td->td_proc->p_fd, fd, &fp)) != 0)
4081 if ((fp->f_flag & FREAD) == 0) {
4087 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
4088 if (vp->v_type != VDIR) {
4089 VFS_UNLOCK_GIANT(vfslocked);
4093 aiov.iov_base = buf;
4094 aiov.iov_len = count;
4095 auio.uio_iov = &aiov;
4096 auio.uio_iovcnt = 1;
4097 auio.uio_rw = UIO_READ;
4098 auio.uio_segflg = UIO_USERSPACE;
4100 auio.uio_resid = count;
4101 vn_lock(vp, LK_SHARED | LK_RETRY);
4102 AUDIT_ARG_VNODE1(vp);
4103 loff = auio.uio_offset = fp->f_offset;
4105 error = mac_vnode_check_readdir(td->td_ucred, vp);
4108 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL,
4110 fp->f_offset = auio.uio_offset;
4113 VFS_UNLOCK_GIANT(vfslocked);
4116 if (count == auio.uio_resid &&
4117 (vp->v_vflag & VV_ROOT) &&
4118 (vp->v_mount->mnt_flag & MNT_UNION)) {
4119 struct vnode *tvp = vp;
4120 vp = vp->v_mount->mnt_vnodecovered;
4126 VFS_UNLOCK_GIANT(vfslocked);
4130 VFS_UNLOCK_GIANT(vfslocked);
4132 td->td_retval[0] = count - auio.uio_resid;
4138 #ifndef _SYS_SYSPROTO_H_
4139 struct getdents_args {
4148 register struct getdents_args /* {
4154 struct getdirentries_args ap;
4157 ap.count = uap->count;
4159 return (getdirentries(td, &ap));
4163 * Set the mode mask for creation of filesystem nodes.
4165 #ifndef _SYS_SYSPROTO_H_
4173 struct umask_args /* {
4177 register struct filedesc *fdp;
4179 FILEDESC_XLOCK(td->td_proc->p_fd);
4180 fdp = td->td_proc->p_fd;
4181 td->td_retval[0] = fdp->fd_cmask;
4182 fdp->fd_cmask = uap->newmask & ALLPERMS;
4183 FILEDESC_XUNLOCK(td->td_proc->p_fd);
4188 * Void all references to file by ripping underlying filesystem away from
4191 #ifndef _SYS_SYSPROTO_H_
4192 struct revoke_args {
4199 register struct revoke_args /* {
4206 struct nameidata nd;
4209 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | MPSAFE | AUDITVNODE1,
4210 UIO_USERSPACE, uap->path, td);
4211 if ((error = namei(&nd)) != 0)
4213 vfslocked = NDHASGIANT(&nd);
4215 NDFREE(&nd, NDF_ONLY_PNBUF);
4216 if (vp->v_type != VCHR) {
4221 error = mac_vnode_check_revoke(td->td_ucred, vp);
4225 error = VOP_GETATTR(vp, &vattr, td->td_ucred);
4228 if (td->td_ucred->cr_uid != vattr.va_uid) {
4229 error = priv_check(td, PRIV_VFS_ADMIN);
4234 VOP_REVOKE(vp, REVOKEALL);
4237 VFS_UNLOCK_GIANT(vfslocked);
4242 * Convert a user file descriptor to a kernel file entry.
4243 * A reference on the file entry is held upon returning.
4246 getvnode(fdp, fd, fpp)
4247 struct filedesc *fdp;
4256 if (fdp == NULL || (fp = fget_unlocked(fdp, fd)) == NULL)
4258 else if (fp->f_vnode == NULL) {
4260 fdrop(fp, curthread);
4267 * Get an (NFS) file handle.
4269 #ifndef _SYS_SYSPROTO_H_
4270 struct lgetfh_args {
4278 register struct lgetfh_args *uap;
4280 struct nameidata nd;
4282 register struct vnode *vp;
4286 error = priv_check(td, PRIV_VFS_GETFH);
4289 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | MPSAFE | AUDITVNODE1,
4290 UIO_USERSPACE, uap->fname, td);
4294 vfslocked = NDHASGIANT(&nd);
4295 NDFREE(&nd, NDF_ONLY_PNBUF);
4297 bzero(&fh, sizeof(fh));
4298 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
4299 error = VOP_VPTOFH(vp, &fh.fh_fid);
4301 VFS_UNLOCK_GIANT(vfslocked);
4304 error = copyout(&fh, uap->fhp, sizeof (fh));
4308 #ifndef _SYS_SYSPROTO_H_
4317 register struct getfh_args *uap;
4319 struct nameidata nd;
4321 register struct vnode *vp;
4325 error = priv_check(td, PRIV_VFS_GETFH);
4328 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | MPSAFE | AUDITVNODE1,
4329 UIO_USERSPACE, uap->fname, td);
4333 vfslocked = NDHASGIANT(&nd);
4334 NDFREE(&nd, NDF_ONLY_PNBUF);
4336 bzero(&fh, sizeof(fh));
4337 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
4338 error = VOP_VPTOFH(vp, &fh.fh_fid);
4340 VFS_UNLOCK_GIANT(vfslocked);
4343 error = copyout(&fh, uap->fhp, sizeof (fh));
4348 * syscall for the rpc.lockd to use to translate a NFS file handle into an
4351 * warning: do not remove the priv_check() call or this becomes one giant
4354 #ifndef _SYS_SYSPROTO_H_
4355 struct fhopen_args {
4356 const struct fhandle *u_fhp;
4363 struct fhopen_args /* {
4364 const struct fhandle *u_fhp;
4368 struct proc *p = td->td_proc;
4373 struct vattr *vap = &vat;
4376 register struct filedesc *fdp = p->p_fd;
4377 int fmode, error, type;
4383 error = priv_check(td, PRIV_VFS_FHOPEN);
4386 fmode = FFLAGS(uap->flags);
4387 /* why not allow a non-read/write open for our lockd? */
4388 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
4390 error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
4393 /* find the mount point */
4394 mp = vfs_busyfs(&fhp.fh_fsid);
4397 vfslocked = VFS_LOCK_GIANT(mp);
4398 /* now give me my vnode, it gets returned to me locked */
4399 error = VFS_FHTOVP(mp, &fhp.fh_fid, &vp);
4404 * from now on we have to make sure not
4405 * to forget about the vnode
4406 * any error that causes an abort must vput(vp)
4407 * just set error = err and 'goto bad;'.
4413 if (vp->v_type == VLNK) {
4417 if (vp->v_type == VSOCK) {
4422 if (fmode & (FWRITE | O_TRUNC)) {
4423 if (vp->v_type == VDIR) {
4427 error = vn_writechk(vp);
4434 if ((fmode & O_APPEND) && (fmode & FWRITE))
4437 error = mac_vnode_check_open(td->td_ucred, vp, accmode);
4442 error = VOP_ACCESS(vp, accmode, td->td_ucred, td);
4446 if (fmode & O_TRUNC) {
4448 VOP_UNLOCK(vp, 0); /* XXX */
4449 if ((error = vn_start_write(NULL, &mp, V_WAIT | PCATCH)) != 0) {
4454 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); /* XXX */
4458 * We don't yet have fp->f_cred, so use td->td_ucred, which
4461 error = mac_vnode_check_write(td->td_ucred, td->td_ucred, vp);
4466 error = VOP_SETATTR(vp, vap, td->td_ucred);
4470 vn_finished_write(mp);
4474 error = VOP_OPEN(vp, fmode, td->td_ucred, td, NULL);
4482 * end of vn_open code
4485 if ((error = falloc(td, &nfp, &indx)) != 0) {
4490 /* An extra reference on `nfp' has been held for us by falloc(). */
4493 finit(nfp, fmode & FMASK, DTYPE_VNODE, vp, &vnops);
4494 if (fmode & (O_EXLOCK | O_SHLOCK)) {
4495 lf.l_whence = SEEK_SET;
4498 if (fmode & O_EXLOCK)
4499 lf.l_type = F_WRLCK;
4501 lf.l_type = F_RDLCK;
4503 if ((fmode & FNONBLOCK) == 0)
4506 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf,
4509 * The lock request failed. Normally close the
4510 * descriptor but handle the case where someone might
4511 * have dup()d or close()d it when we weren't looking.
4513 fdclose(fdp, fp, indx, td);
4516 * release our private reference
4521 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
4522 atomic_set_int(&fp->f_flag, FHASLOCK);
4527 VFS_UNLOCK_GIANT(vfslocked);
4528 td->td_retval[0] = indx;
4534 VFS_UNLOCK_GIANT(vfslocked);
4539 * Stat an (NFS) file handle.
4541 #ifndef _SYS_SYSPROTO_H_
4542 struct fhstat_args {
4543 struct fhandle *u_fhp;
4550 register struct fhstat_args /* {
4551 struct fhandle *u_fhp;
4562 error = priv_check(td, PRIV_VFS_FHSTAT);
4565 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4568 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4570 vfslocked = VFS_LOCK_GIANT(mp);
4571 error = VFS_FHTOVP(mp, &fh.fh_fid, &vp);
4574 VFS_UNLOCK_GIANT(vfslocked);
4577 error = vn_stat(vp, &sb, td->td_ucred, NOCRED, td);
4579 VFS_UNLOCK_GIANT(vfslocked);
4582 error = copyout(&sb, uap->sb, sizeof(sb));
4587 * Implement fstatfs() for (NFS) file handles.
4589 #ifndef _SYS_SYSPROTO_H_
4590 struct fhstatfs_args {
4591 struct fhandle *u_fhp;
4598 struct fhstatfs_args /* {
4599 struct fhandle *u_fhp;
4607 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4610 error = kern_fhstatfs(td, fh, &sf);
4613 return (copyout(&sf, uap->buf, sizeof(sf)));
4617 kern_fhstatfs(struct thread *td, fhandle_t fh, struct statfs *buf)
4625 error = priv_check(td, PRIV_VFS_FHSTATFS);
4628 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4630 vfslocked = VFS_LOCK_GIANT(mp);
4631 error = VFS_FHTOVP(mp, &fh.fh_fid, &vp);
4634 VFS_UNLOCK_GIANT(vfslocked);
4638 error = prison_canseemount(td->td_ucred, mp);
4642 error = mac_mount_check_stat(td->td_ucred, mp);
4647 * Set these in case the underlying filesystem fails to do so.
4650 sp->f_version = STATFS_VERSION;
4651 sp->f_namemax = NAME_MAX;
4652 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
4653 error = VFS_STATFS(mp, sp);
4658 VFS_UNLOCK_GIANT(vfslocked);