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"
45 #include <sys/param.h>
46 #include <sys/systm.h>
49 #include <sys/sysent.h>
50 #include <sys/malloc.h>
51 #include <sys/mount.h>
52 #include <sys/mutex.h>
53 #include <sys/sysproto.h>
54 #include <sys/namei.h>
55 #include <sys/filedesc.h>
56 #include <sys/kernel.h>
57 #include <sys/fcntl.h>
59 #include <sys/filio.h>
60 #include <sys/limits.h>
61 #include <sys/linker.h>
65 #include <sys/unistd.h>
66 #include <sys/vnode.h>
69 #include <sys/dirent.h>
71 #include <sys/syscallsubr.h>
72 #include <sys/sysctl.h>
74 #include <sys/ktrace.h>
77 #include <machine/stdarg.h>
79 #include <security/audit/audit.h>
80 #include <security/mac/mac_framework.h>
83 #include <vm/vm_object.h>
84 #include <vm/vm_page.h>
87 SDT_PROVIDER_DEFINE(vfs);
88 SDT_PROBE_DEFINE(vfs, , stat, mode);
89 SDT_PROBE_ARGTYPE(vfs, , stat, mode, 0, "char *");
90 SDT_PROBE_ARGTYPE(vfs, , stat, mode, 1, "int");
91 SDT_PROBE_DEFINE(vfs, , stat, reg);
92 SDT_PROBE_ARGTYPE(vfs, , stat, reg, 0, "char *");
93 SDT_PROBE_ARGTYPE(vfs, , stat, reg, 1, "int");
95 static int chroot_refuse_vdir_fds(struct filedesc *fdp);
96 static int getutimes(const struct timeval *, enum uio_seg, struct timespec *);
97 static int setfown(struct thread *td, struct vnode *, uid_t, gid_t);
98 static int setfmode(struct thread *td, struct vnode *, int);
99 static int setfflags(struct thread *td, struct vnode *, int);
100 static int setutimes(struct thread *td, struct vnode *,
101 const struct timespec *, int, int);
102 static int vn_access(struct vnode *vp, int user_flags, struct ucred *cred,
106 * The module initialization routine for POSIX asynchronous I/O will
107 * set this to the version of AIO that it implements. (Zero means
108 * that it is not implemented.) This value is used here by pathconf()
109 * and in kern_descrip.c by fpathconf().
111 int async_io_version;
114 static int syncprt = 0;
115 SYSCTL_INT(_debug, OID_AUTO, syncprt, CTLFLAG_RW, &syncprt, 0, "");
119 * Sync each mounted filesystem.
121 #ifndef _SYS_SYSPROTO_H_
130 struct sync_args *uap;
132 struct mount *mp, *nmp;
135 mtx_lock(&mountlist_mtx);
136 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
137 if (vfs_busy(mp, LK_NOWAIT, &mountlist_mtx, td)) {
138 nmp = TAILQ_NEXT(mp, mnt_list);
141 vfslocked = VFS_LOCK_GIANT(mp);
142 if ((mp->mnt_flag & MNT_RDONLY) == 0 &&
143 vn_start_write(NULL, &mp, V_NOWAIT) == 0) {
146 mp->mnt_kern_flag &= ~MNTK_ASYNC;
148 vfs_msync(mp, MNT_NOWAIT);
149 VFS_SYNC(mp, MNT_NOWAIT, td);
152 if ((mp->mnt_flag & MNT_ASYNC) != 0 &&
153 mp->mnt_noasync == 0)
154 mp->mnt_kern_flag |= MNTK_ASYNC;
156 vn_finished_write(mp);
158 VFS_UNLOCK_GIANT(vfslocked);
159 mtx_lock(&mountlist_mtx);
160 nmp = TAILQ_NEXT(mp, mnt_list);
163 mtx_unlock(&mountlist_mtx);
167 /* XXX PRISON: could be per prison flag */
168 static int prison_quotas;
170 SYSCTL_INT(_kern_prison, OID_AUTO, quotas, CTLFLAG_RW, &prison_quotas, 0, "");
174 * Change filesystem quotas.
176 #ifndef _SYS_SYSPROTO_H_
177 struct quotactl_args {
187 register struct quotactl_args /* {
199 AUDIT_ARG(cmd, uap->cmd);
200 AUDIT_ARG(uid, uap->uid);
201 if (jailed(td->td_ucred) && !prison_quotas)
203 NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE | AUDITVNODE1,
204 UIO_USERSPACE, uap->path, td);
205 if ((error = namei(&nd)) != 0)
207 vfslocked = NDHASGIANT(&nd);
208 NDFREE(&nd, NDF_ONLY_PNBUF);
209 mp = nd.ni_vp->v_mount;
210 if ((error = vfs_busy(mp, 0, NULL, td))) {
212 VFS_UNLOCK_GIANT(vfslocked);
216 error = VFS_QUOTACTL(mp, uap->cmd, uap->uid, uap->arg, td);
218 VFS_UNLOCK_GIANT(vfslocked);
223 * Used by statfs conversion routines to scale the block size up if
224 * necessary so that all of the block counts are <= 'max_size'. Note
225 * that 'max_size' should be a bitmask, i.e. 2^n - 1 for some non-zero
229 statfs_scale_blocks(struct statfs *sf, long max_size)
234 KASSERT(powerof2(max_size + 1), ("%s: invalid max_size", __func__));
237 * Attempt to scale the block counts to give a more accurate
238 * overview to userland of the ratio of free space to used
239 * space. To do this, find the largest block count and compute
240 * a divisor that lets it fit into a signed integer <= max_size.
242 if (sf->f_bavail < 0)
243 count = -sf->f_bavail;
245 count = sf->f_bavail;
246 count = MAX(sf->f_blocks, MAX(sf->f_bfree, count));
247 if (count <= max_size)
250 count >>= flsl(max_size);
257 sf->f_bsize <<= shift;
258 sf->f_blocks >>= shift;
259 sf->f_bfree >>= shift;
260 sf->f_bavail >>= shift;
264 * Get filesystem statistics.
266 #ifndef _SYS_SYSPROTO_H_
275 register struct statfs_args /* {
283 error = kern_statfs(td, uap->path, UIO_USERSPACE, &sf);
285 error = copyout(&sf, uap->buf, sizeof(sf));
290 kern_statfs(struct thread *td, char *path, enum uio_seg pathseg,
294 struct statfs *sp, sb;
299 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | MPSAFE |
300 AUDITVNODE1, pathseg, path, td);
304 vfslocked = NDHASGIANT(&nd);
305 mp = nd.ni_vp->v_mount;
307 NDFREE(&nd, NDF_ONLY_PNBUF);
310 error = mac_check_mount_stat(td->td_ucred, mp);
315 * Set these in case the underlying filesystem fails to do so.
318 sp->f_version = STATFS_VERSION;
319 sp->f_namemax = NAME_MAX;
320 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
321 error = VFS_STATFS(mp, sp, td);
324 if (priv_check(td, PRIV_VFS_GENERATION)) {
325 bcopy(sp, &sb, sizeof(sb));
326 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
327 prison_enforce_statfs(td->td_ucred, mp, &sb);
333 VFS_UNLOCK_GIANT(vfslocked);
334 if (mtx_owned(&Giant))
335 printf("statfs(%d): %s: %d\n", vfslocked, path, error);
340 * Get filesystem statistics.
342 #ifndef _SYS_SYSPROTO_H_
343 struct fstatfs_args {
351 register struct fstatfs_args /* {
359 error = kern_fstatfs(td, uap->fd, &sf);
361 error = copyout(&sf, uap->buf, sizeof(sf));
366 kern_fstatfs(struct thread *td, int fd, struct statfs *buf)
370 struct statfs *sp, sb;
376 error = getvnode(td->td_proc->p_fd, fd, &fp);
380 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
381 vn_lock(vp, LK_SHARED | LK_RETRY, td);
383 AUDIT_ARG(vnode, vp, ARG_VNODE1);
388 VOP_UNLOCK(vp, 0, td);
390 if (vp->v_iflag & VI_DOOMED) {
395 error = mac_check_mount_stat(td->td_ucred, mp);
400 * Set these in case the underlying filesystem fails to do so.
403 sp->f_version = STATFS_VERSION;
404 sp->f_namemax = NAME_MAX;
405 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
406 error = VFS_STATFS(mp, sp, td);
409 if (priv_check(td, PRIV_VFS_GENERATION)) {
410 bcopy(sp, &sb, sizeof(sb));
411 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
412 prison_enforce_statfs(td->td_ucred, mp, &sb);
419 VFS_UNLOCK_GIANT(vfslocked);
424 * Get statistics on all filesystems.
426 #ifndef _SYS_SYSPROTO_H_
427 struct getfsstat_args {
436 register struct getfsstat_args /* {
443 return (kern_getfsstat(td, &uap->buf, uap->bufsize, UIO_USERSPACE,
448 * If (bufsize > 0 && bufseg == UIO_SYSSPACE)
449 * The caller is responsible for freeing memory which will be allocated
453 kern_getfsstat(struct thread *td, struct statfs **buf, size_t bufsize,
454 enum uio_seg bufseg, int flags)
456 struct mount *mp, *nmp;
457 struct statfs *sfsp, *sp, sb;
458 size_t count, maxcount;
462 maxcount = bufsize / sizeof(struct statfs);
465 else if (bufseg == UIO_USERSPACE)
467 else /* if (bufseg == UIO_SYSSPACE) */ {
469 mtx_lock(&mountlist_mtx);
470 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
473 mtx_unlock(&mountlist_mtx);
474 if (maxcount > count)
476 sfsp = *buf = malloc(maxcount * sizeof(struct statfs), M_TEMP,
480 mtx_lock(&mountlist_mtx);
481 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
482 if (prison_canseemount(td->td_ucred, mp) != 0) {
483 nmp = TAILQ_NEXT(mp, mnt_list);
487 if (mac_check_mount_stat(td->td_ucred, mp) != 0) {
488 nmp = TAILQ_NEXT(mp, mnt_list);
492 if (vfs_busy(mp, LK_NOWAIT, &mountlist_mtx, td)) {
493 nmp = TAILQ_NEXT(mp, mnt_list);
496 vfslocked = VFS_LOCK_GIANT(mp);
497 if (sfsp && count < maxcount) {
500 * Set these in case the underlying filesystem
503 sp->f_version = STATFS_VERSION;
504 sp->f_namemax = NAME_MAX;
505 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
507 * If MNT_NOWAIT or MNT_LAZY is specified, do not
508 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
509 * overrides MNT_WAIT.
511 if (((flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
512 (flags & MNT_WAIT)) &&
513 (error = VFS_STATFS(mp, sp, td))) {
514 VFS_UNLOCK_GIANT(vfslocked);
515 mtx_lock(&mountlist_mtx);
516 nmp = TAILQ_NEXT(mp, mnt_list);
520 if (priv_check(td, PRIV_VFS_GENERATION)) {
521 bcopy(sp, &sb, sizeof(sb));
522 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
523 prison_enforce_statfs(td->td_ucred, mp, &sb);
526 if (bufseg == UIO_SYSSPACE)
527 bcopy(sp, sfsp, sizeof(*sp));
528 else /* if (bufseg == UIO_USERSPACE) */ {
529 error = copyout(sp, sfsp, sizeof(*sp));
532 VFS_UNLOCK_GIANT(vfslocked);
538 VFS_UNLOCK_GIANT(vfslocked);
540 mtx_lock(&mountlist_mtx);
541 nmp = TAILQ_NEXT(mp, mnt_list);
544 mtx_unlock(&mountlist_mtx);
545 if (sfsp && count > maxcount)
546 td->td_retval[0] = maxcount;
548 td->td_retval[0] = count;
552 #ifdef COMPAT_FREEBSD4
554 * Get old format filesystem statistics.
556 static void cvtstatfs(struct statfs *, struct ostatfs *);
558 #ifndef _SYS_SYSPROTO_H_
559 struct freebsd4_statfs_args {
565 freebsd4_statfs(td, uap)
567 struct freebsd4_statfs_args /* {
576 error = kern_statfs(td, uap->path, UIO_USERSPACE, &sf);
579 cvtstatfs(&sf, &osb);
580 return (copyout(&osb, uap->buf, sizeof(osb)));
584 * Get filesystem statistics.
586 #ifndef _SYS_SYSPROTO_H_
587 struct freebsd4_fstatfs_args {
593 freebsd4_fstatfs(td, uap)
595 struct freebsd4_fstatfs_args /* {
604 error = kern_fstatfs(td, uap->fd, &sf);
607 cvtstatfs(&sf, &osb);
608 return (copyout(&osb, uap->buf, sizeof(osb)));
612 * Get statistics on all filesystems.
614 #ifndef _SYS_SYSPROTO_H_
615 struct freebsd4_getfsstat_args {
622 freebsd4_getfsstat(td, uap)
624 register struct freebsd4_getfsstat_args /* {
630 struct statfs *buf, *sp;
635 count = uap->bufsize / sizeof(struct ostatfs);
636 size = count * sizeof(struct statfs);
637 error = kern_getfsstat(td, &buf, size, UIO_SYSSPACE, uap->flags);
639 count = td->td_retval[0];
641 while (count > 0 && error == 0) {
643 error = copyout(&osb, uap->buf, sizeof(osb));
654 * Implement fstatfs() for (NFS) file handles.
656 #ifndef _SYS_SYSPROTO_H_
657 struct freebsd4_fhstatfs_args {
658 struct fhandle *u_fhp;
663 freebsd4_fhstatfs(td, uap)
665 struct freebsd4_fhstatfs_args /* {
666 struct fhandle *u_fhp;
675 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
678 error = kern_fhstatfs(td, fh, &sf);
681 cvtstatfs(&sf, &osb);
682 return (copyout(&osb, uap->buf, sizeof(osb)));
686 * Convert a new format statfs structure to an old format statfs structure.
694 statfs_scale_blocks(nsp, LONG_MAX);
695 bzero(osp, sizeof(*osp));
696 osp->f_bsize = nsp->f_bsize;
697 osp->f_iosize = MIN(nsp->f_iosize, LONG_MAX);
698 osp->f_blocks = nsp->f_blocks;
699 osp->f_bfree = nsp->f_bfree;
700 osp->f_bavail = nsp->f_bavail;
701 osp->f_files = MIN(nsp->f_files, LONG_MAX);
702 osp->f_ffree = MIN(nsp->f_ffree, LONG_MAX);
703 osp->f_owner = nsp->f_owner;
704 osp->f_type = nsp->f_type;
705 osp->f_flags = nsp->f_flags;
706 osp->f_syncwrites = MIN(nsp->f_syncwrites, LONG_MAX);
707 osp->f_asyncwrites = MIN(nsp->f_asyncwrites, LONG_MAX);
708 osp->f_syncreads = MIN(nsp->f_syncreads, LONG_MAX);
709 osp->f_asyncreads = MIN(nsp->f_asyncreads, LONG_MAX);
710 strlcpy(osp->f_fstypename, nsp->f_fstypename,
711 MIN(MFSNAMELEN, OMFSNAMELEN));
712 strlcpy(osp->f_mntonname, nsp->f_mntonname,
713 MIN(MNAMELEN, OMNAMELEN));
714 strlcpy(osp->f_mntfromname, nsp->f_mntfromname,
715 MIN(MNAMELEN, OMNAMELEN));
716 osp->f_fsid = nsp->f_fsid;
718 #endif /* COMPAT_FREEBSD4 */
721 * Change current working directory to a given file descriptor.
723 #ifndef _SYS_SYSPROTO_H_
731 struct fchdir_args /* {
735 register struct filedesc *fdp = td->td_proc->p_fd;
736 struct vnode *vp, *tdp, *vpold;
742 AUDIT_ARG(fd, uap->fd);
743 if ((error = getvnode(fdp, uap->fd, &fp)) != 0)
748 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
749 vn_lock(vp, LK_SHARED | LK_RETRY, td);
750 AUDIT_ARG(vnode, vp, ARG_VNODE1);
751 error = change_dir(vp, td);
752 while (!error && (mp = vp->v_mountedhere) != NULL) {
754 if (vfs_busy(mp, 0, 0, td))
756 tvfslocked = VFS_LOCK_GIANT(mp);
757 error = VFS_ROOT(mp, LK_SHARED, &tdp, td);
760 VFS_UNLOCK_GIANT(tvfslocked);
764 VFS_UNLOCK_GIANT(vfslocked);
766 vfslocked = tvfslocked;
770 VFS_UNLOCK_GIANT(vfslocked);
773 VOP_UNLOCK(vp, 0, td);
774 VFS_UNLOCK_GIANT(vfslocked);
776 vpold = fdp->fd_cdir;
778 FILEDESC_XUNLOCK(fdp);
779 vfslocked = VFS_LOCK_GIANT(vpold->v_mount);
781 VFS_UNLOCK_GIANT(vfslocked);
786 * Change current working directory (``.'').
788 #ifndef _SYS_SYSPROTO_H_
796 struct chdir_args /* {
801 return (kern_chdir(td, uap->path, UIO_USERSPACE));
805 kern_chdir(struct thread *td, char *path, enum uio_seg pathseg)
807 register struct filedesc *fdp = td->td_proc->p_fd;
813 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1 |
814 MPSAFE, pathseg, path, td);
815 if ((error = namei(&nd)) != 0)
817 vfslocked = NDHASGIANT(&nd);
818 if ((error = change_dir(nd.ni_vp, td)) != 0) {
820 VFS_UNLOCK_GIANT(vfslocked);
821 NDFREE(&nd, NDF_ONLY_PNBUF);
824 VOP_UNLOCK(nd.ni_vp, 0, td);
825 VFS_UNLOCK_GIANT(vfslocked);
826 NDFREE(&nd, NDF_ONLY_PNBUF);
829 fdp->fd_cdir = nd.ni_vp;
830 FILEDESC_XUNLOCK(fdp);
831 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
833 VFS_UNLOCK_GIANT(vfslocked);
838 * Helper function for raised chroot(2) security function: Refuse if
839 * any filedescriptors are open directories.
842 chroot_refuse_vdir_fds(fdp)
843 struct filedesc *fdp;
849 FILEDESC_LOCK_ASSERT(fdp);
851 for (fd = 0; fd < fdp->fd_nfiles ; fd++) {
852 fp = fget_locked(fdp, fd);
855 if (fp->f_type == DTYPE_VNODE) {
857 if (vp->v_type == VDIR)
865 * This sysctl determines if we will allow a process to chroot(2) if it
866 * has a directory open:
867 * 0: disallowed for all processes.
868 * 1: allowed for processes that were not already chroot(2)'ed.
869 * 2: allowed for all processes.
872 static int chroot_allow_open_directories = 1;
874 SYSCTL_INT(_kern, OID_AUTO, chroot_allow_open_directories, CTLFLAG_RW,
875 &chroot_allow_open_directories, 0, "");
878 * Change notion of root (``/'') directory.
880 #ifndef _SYS_SYSPROTO_H_
888 struct chroot_args /* {
896 error = priv_check(td, PRIV_VFS_CHROOT);
899 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | MPSAFE |
900 AUDITVNODE1, UIO_USERSPACE, uap->path, td);
904 vfslocked = NDHASGIANT(&nd);
905 if ((error = change_dir(nd.ni_vp, td)) != 0)
908 if ((error = mac_check_vnode_chroot(td->td_ucred, nd.ni_vp)))
911 VOP_UNLOCK(nd.ni_vp, 0, td);
912 error = change_root(nd.ni_vp, td);
914 VFS_UNLOCK_GIANT(vfslocked);
915 NDFREE(&nd, NDF_ONLY_PNBUF);
919 VFS_UNLOCK_GIANT(vfslocked);
921 NDFREE(&nd, NDF_ONLY_PNBUF);
926 * Common routine for chroot and chdir. Callers must provide a locked vnode
936 ASSERT_VOP_LOCKED(vp, "change_dir(): vp not locked");
937 if (vp->v_type != VDIR)
940 error = mac_check_vnode_chdir(td->td_ucred, vp);
944 error = VOP_ACCESS(vp, VEXEC, td->td_ucred, td);
949 * Common routine for kern_chroot() and jail_attach(). The caller is
950 * responsible for invoking priv_check() and mac_check_chroot() to authorize
958 struct filedesc *fdp;
963 VFS_ASSERT_GIANT(vp->v_mount);
964 fdp = td->td_proc->p_fd;
966 if (chroot_allow_open_directories == 0 ||
967 (chroot_allow_open_directories == 1 && fdp->fd_rdir != rootvnode)) {
968 error = chroot_refuse_vdir_fds(fdp);
970 FILEDESC_XUNLOCK(fdp);
974 oldvp = fdp->fd_rdir;
981 FILEDESC_XUNLOCK(fdp);
982 vfslocked = VFS_LOCK_GIANT(oldvp->v_mount);
984 VFS_UNLOCK_GIANT(vfslocked);
989 * Check permissions, allocate an open file structure, and call the device
990 * open routine if any.
992 #ifndef _SYS_SYSPROTO_H_
1002 register struct open_args /* {
1009 return kern_open(td, uap->path, UIO_USERSPACE, uap->flags, uap->mode);
1013 kern_open(struct thread *td, char *path, enum uio_seg pathseg, int flags,
1016 struct proc *p = td->td_proc;
1017 struct filedesc *fdp = p->p_fd;
1024 int type, indx, error;
1026 struct nameidata nd;
1029 AUDIT_ARG(fflags, flags);
1030 AUDIT_ARG(mode, mode);
1031 if ((flags & O_ACCMODE) == O_ACCMODE)
1033 flags = FFLAGS(flags);
1034 error = falloc(td, &nfp, &indx);
1037 /* An extra reference on `nfp' has been held for us by falloc(). */
1039 cmode = ((mode &~ fdp->fd_cmask) & ALLPERMS) &~ S_ISTXT;
1040 NDINIT(&nd, LOOKUP, FOLLOW | AUDITVNODE1 | MPSAFE, pathseg, path, td);
1041 td->td_dupfd = -1; /* XXX check for fdopen */
1042 error = vn_open(&nd, &flags, cmode, fp);
1045 * If the vn_open replaced the method vector, something
1046 * wonderous happened deep below and we just pass it up
1047 * pretending we know what we do.
1049 if (error == ENXIO && fp->f_ops != &badfileops) {
1051 td->td_retval[0] = indx;
1056 * handle special fdopen() case. bleh. dupfdopen() is
1057 * responsible for dropping the old contents of ofiles[indx]
1060 if ((error == ENODEV || error == ENXIO) &&
1061 td->td_dupfd >= 0 && /* XXX from fdopen */
1063 dupfdopen(td, fdp, indx, td->td_dupfd, flags, error)) == 0) {
1064 td->td_retval[0] = indx;
1069 * Clean up the descriptor, but only if another thread hadn't
1070 * replaced or closed it.
1072 fdclose(fdp, fp, indx, td);
1075 if (error == ERESTART)
1080 vfslocked = NDHASGIANT(&nd);
1081 NDFREE(&nd, NDF_ONLY_PNBUF);
1086 if (fp->f_data == NULL)
1088 fp->f_flag = flags & FMASK;
1090 fp->f_type = (vp->v_type == VFIFO ? DTYPE_FIFO : DTYPE_VNODE);
1091 if (fp->f_ops == &badfileops)
1095 VOP_UNLOCK(vp, 0, td);
1096 if (flags & (O_EXLOCK | O_SHLOCK)) {
1097 lf.l_whence = SEEK_SET;
1100 if (flags & O_EXLOCK)
1101 lf.l_type = F_WRLCK;
1103 lf.l_type = F_RDLCK;
1105 if ((flags & FNONBLOCK) == 0)
1107 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf,
1110 fp->f_flag |= FHASLOCK;
1112 if (flags & O_TRUNC) {
1113 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
1115 VOP_LEASE(vp, td, td->td_ucred, LEASE_WRITE);
1118 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
1120 error = mac_check_vnode_write(td->td_ucred, fp->f_cred, vp);
1123 error = VOP_SETATTR(vp, &vat, td->td_ucred, td);
1124 VOP_UNLOCK(vp, 0, td);
1125 vn_finished_write(mp);
1129 VFS_UNLOCK_GIANT(vfslocked);
1131 * Release our private reference, leaving the one associated with
1132 * the descriptor table intact.
1135 td->td_retval[0] = indx;
1138 VFS_UNLOCK_GIANT(vfslocked);
1139 fdclose(fdp, fp, indx, td);
1148 #ifndef _SYS_SYSPROTO_H_
1149 struct ocreat_args {
1157 register struct ocreat_args /* {
1163 return (kern_open(td, uap->path, UIO_USERSPACE,
1164 O_WRONLY | O_CREAT | O_TRUNC, uap->mode));
1166 #endif /* COMPAT_43 */
1169 * Create a special file.
1171 #ifndef _SYS_SYSPROTO_H_
1181 register struct mknod_args /* {
1188 return (kern_mknod(td, uap->path, UIO_USERSPACE, uap->mode, uap->dev));
1192 kern_mknod(struct thread *td, char *path, enum uio_seg pathseg, int mode,
1200 struct nameidata nd;
1203 AUDIT_ARG(mode, mode);
1204 AUDIT_ARG(dev, dev);
1205 switch (mode & S_IFMT) {
1208 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
1211 error = priv_check(td, PRIV_VFS_MKNOD_BAD);
1214 error = priv_check(td, PRIV_VFS_MKNOD_WHT);
1224 NDINIT(&nd, CREATE, LOCKPARENT | SAVENAME | MPSAFE | AUDITVNODE1,
1226 if ((error = namei(&nd)) != 0)
1228 vfslocked = NDHASGIANT(&nd);
1231 NDFREE(&nd, NDF_ONLY_PNBUF);
1232 if (vp == nd.ni_dvp)
1237 VFS_UNLOCK_GIANT(vfslocked);
1241 FILEDESC_SLOCK(td->td_proc->p_fd);
1242 vattr.va_mode = (mode & ALLPERMS) &
1243 ~td->td_proc->p_fd->fd_cmask;
1244 FILEDESC_SUNLOCK(td->td_proc->p_fd);
1245 vattr.va_rdev = dev;
1248 switch (mode & S_IFMT) {
1249 case S_IFMT: /* used by badsect to flag bad sectors */
1250 vattr.va_type = VBAD;
1253 vattr.va_type = VCHR;
1256 vattr.va_type = VBLK;
1262 panic("kern_mknod: invalid mode");
1265 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1266 NDFREE(&nd, NDF_ONLY_PNBUF);
1268 VFS_UNLOCK_GIANT(vfslocked);
1269 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1274 if (error == 0 && !whiteout)
1275 error = mac_check_vnode_create(td->td_ucred, nd.ni_dvp,
1276 &nd.ni_cnd, &vattr);
1279 VOP_LEASE(nd.ni_dvp, td, td->td_ucred, LEASE_WRITE);
1281 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, CREATE);
1283 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp,
1284 &nd.ni_cnd, &vattr);
1289 NDFREE(&nd, NDF_ONLY_PNBUF);
1291 vn_finished_write(mp);
1292 VFS_UNLOCK_GIANT(vfslocked);
1297 * Create a named pipe.
1299 #ifndef _SYS_SYSPROTO_H_
1300 struct mkfifo_args {
1308 register struct mkfifo_args /* {
1314 return (kern_mkfifo(td, uap->path, UIO_USERSPACE, uap->mode));
1318 kern_mkfifo(struct thread *td, char *path, enum uio_seg pathseg, int mode)
1323 struct nameidata nd;
1326 AUDIT_ARG(mode, mode);
1329 NDINIT(&nd, CREATE, LOCKPARENT | SAVENAME | MPSAFE | AUDITVNODE1,
1331 if ((error = namei(&nd)) != 0)
1333 vfslocked = NDHASGIANT(&nd);
1334 if (nd.ni_vp != NULL) {
1335 NDFREE(&nd, NDF_ONLY_PNBUF);
1336 if (nd.ni_vp == nd.ni_dvp)
1341 VFS_UNLOCK_GIANT(vfslocked);
1344 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1345 NDFREE(&nd, NDF_ONLY_PNBUF);
1347 VFS_UNLOCK_GIANT(vfslocked);
1348 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1353 vattr.va_type = VFIFO;
1354 FILEDESC_SLOCK(td->td_proc->p_fd);
1355 vattr.va_mode = (mode & ALLPERMS) & ~td->td_proc->p_fd->fd_cmask;
1356 FILEDESC_SUNLOCK(td->td_proc->p_fd);
1358 error = mac_check_vnode_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1363 VOP_LEASE(nd.ni_dvp, td, td->td_ucred, LEASE_WRITE);
1364 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
1371 vn_finished_write(mp);
1372 VFS_UNLOCK_GIANT(vfslocked);
1373 NDFREE(&nd, NDF_ONLY_PNBUF);
1378 * Make a hard file link.
1380 #ifndef _SYS_SYSPROTO_H_
1389 register struct link_args /* {
1396 error = kern_link(td, uap->path, uap->link, UIO_USERSPACE);
1400 static int hardlink_check_uid = 0;
1401 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
1402 &hardlink_check_uid, 0,
1403 "Unprivileged processes cannot create hard links to files owned by other "
1405 static int hardlink_check_gid = 0;
1406 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
1407 &hardlink_check_gid, 0,
1408 "Unprivileged processes cannot create hard links to files owned by other "
1412 can_hardlink(struct vnode *vp, struct thread *td, struct ucred *cred)
1417 if (!hardlink_check_uid && !hardlink_check_gid)
1420 error = VOP_GETATTR(vp, &va, cred, td);
1424 if (hardlink_check_uid && cred->cr_uid != va.va_uid) {
1425 error = priv_check_cred(cred, PRIV_VFS_LINK, 0);
1430 if (hardlink_check_gid && !groupmember(va.va_gid, cred)) {
1431 error = priv_check_cred(cred, PRIV_VFS_LINK, 0);
1440 kern_link(struct thread *td, char *path, char *link, enum uio_seg segflg)
1444 struct nameidata nd;
1450 NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE | AUDITVNODE1, segflg, path, td);
1451 if ((error = namei(&nd)) != 0)
1453 vfslocked = NDHASGIANT(&nd);
1454 NDFREE(&nd, NDF_ONLY_PNBUF);
1456 if (vp->v_type == VDIR) {
1458 VFS_UNLOCK_GIANT(vfslocked);
1459 return (EPERM); /* POSIX */
1461 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0) {
1463 VFS_UNLOCK_GIANT(vfslocked);
1466 NDINIT(&nd, CREATE, LOCKPARENT | SAVENAME | MPSAFE | AUDITVNODE2,
1468 if ((error = namei(&nd)) == 0) {
1469 lvfslocked = NDHASGIANT(&nd);
1470 if (nd.ni_vp != NULL) {
1471 if (nd.ni_dvp == nd.ni_vp)
1477 } else if ((error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td))
1479 VOP_LEASE(nd.ni_dvp, td, td->td_ucred, LEASE_WRITE);
1480 VOP_LEASE(vp, td, td->td_ucred, LEASE_WRITE);
1481 error = can_hardlink(vp, td, td->td_ucred);
1484 error = mac_check_vnode_link(td->td_ucred,
1485 nd.ni_dvp, vp, &nd.ni_cnd);
1488 error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
1489 VOP_UNLOCK(vp, 0, td);
1492 NDFREE(&nd, NDF_ONLY_PNBUF);
1493 VFS_UNLOCK_GIANT(lvfslocked);
1496 vn_finished_write(mp);
1497 VFS_UNLOCK_GIANT(vfslocked);
1502 * Make a symbolic link.
1504 #ifndef _SYS_SYSPROTO_H_
1505 struct symlink_args {
1513 register struct symlink_args /* {
1519 return (kern_symlink(td, uap->path, uap->link, UIO_USERSPACE));
1523 kern_symlink(struct thread *td, char *path, char *link, enum uio_seg segflg)
1529 struct nameidata nd;
1532 if (segflg == UIO_SYSSPACE) {
1535 syspath = uma_zalloc(namei_zone, M_WAITOK);
1536 if ((error = copyinstr(path, syspath, MAXPATHLEN, NULL)) != 0)
1539 AUDIT_ARG(text, syspath);
1542 NDINIT(&nd, CREATE, LOCKPARENT | SAVENAME | MPSAFE | AUDITVNODE1,
1544 if ((error = namei(&nd)) != 0)
1546 vfslocked = NDHASGIANT(&nd);
1548 NDFREE(&nd, NDF_ONLY_PNBUF);
1549 if (nd.ni_vp == nd.ni_dvp)
1554 VFS_UNLOCK_GIANT(vfslocked);
1558 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1559 NDFREE(&nd, NDF_ONLY_PNBUF);
1561 VFS_UNLOCK_GIANT(vfslocked);
1562 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1567 FILEDESC_SLOCK(td->td_proc->p_fd);
1568 vattr.va_mode = ACCESSPERMS &~ td->td_proc->p_fd->fd_cmask;
1569 FILEDESC_SUNLOCK(td->td_proc->p_fd);
1571 vattr.va_type = VLNK;
1572 error = mac_check_vnode_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1577 VOP_LEASE(nd.ni_dvp, td, td->td_ucred, LEASE_WRITE);
1578 error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr, syspath);
1584 NDFREE(&nd, NDF_ONLY_PNBUF);
1586 vn_finished_write(mp);
1587 VFS_UNLOCK_GIANT(vfslocked);
1589 if (segflg != UIO_SYSSPACE)
1590 uma_zfree(namei_zone, syspath);
1595 * Delete a whiteout from the filesystem.
1600 register struct undelete_args /* {
1606 struct nameidata nd;
1611 NDINIT(&nd, DELETE, LOCKPARENT | DOWHITEOUT | MPSAFE | AUDITVNODE1,
1612 UIO_USERSPACE, uap->path, td);
1616 vfslocked = NDHASGIANT(&nd);
1618 if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & ISWHITEOUT)) {
1619 NDFREE(&nd, NDF_ONLY_PNBUF);
1620 if (nd.ni_vp == nd.ni_dvp)
1626 VFS_UNLOCK_GIANT(vfslocked);
1629 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1630 NDFREE(&nd, NDF_ONLY_PNBUF);
1632 VFS_UNLOCK_GIANT(vfslocked);
1633 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1637 VOP_LEASE(nd.ni_dvp, td, td->td_ucred, LEASE_WRITE);
1638 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, DELETE);
1639 NDFREE(&nd, NDF_ONLY_PNBUF);
1641 vn_finished_write(mp);
1642 VFS_UNLOCK_GIANT(vfslocked);
1647 * Delete a name from the filesystem.
1649 #ifndef _SYS_SYSPROTO_H_
1650 struct unlink_args {
1657 struct unlink_args /* {
1663 error = kern_unlink(td, uap->path, UIO_USERSPACE);
1668 kern_unlink(struct thread *td, char *path, enum uio_seg pathseg)
1673 struct nameidata nd;
1678 NDINIT(&nd, DELETE, LOCKPARENT | LOCKLEAF | MPSAFE | AUDITVNODE1,
1680 if ((error = namei(&nd)) != 0)
1681 return (error == EINVAL ? EPERM : error);
1682 vfslocked = NDHASGIANT(&nd);
1684 if (vp->v_type == VDIR)
1685 error = EPERM; /* POSIX */
1688 * The root of a mounted filesystem cannot be deleted.
1690 * XXX: can this only be a VDIR case?
1692 if (vp->v_vflag & VV_ROOT)
1696 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1697 NDFREE(&nd, NDF_ONLY_PNBUF);
1699 if (vp == nd.ni_dvp)
1703 VFS_UNLOCK_GIANT(vfslocked);
1704 if ((error = vn_start_write(NULL, &mp,
1705 V_XSLEEP | PCATCH)) != 0)
1710 error = mac_check_vnode_unlink(td->td_ucred, nd.ni_dvp, vp,
1715 VOP_LEASE(nd.ni_dvp, td, td->td_ucred, LEASE_WRITE);
1716 error = VOP_REMOVE(nd.ni_dvp, vp, &nd.ni_cnd);
1720 vn_finished_write(mp);
1722 NDFREE(&nd, NDF_ONLY_PNBUF);
1724 if (vp == nd.ni_dvp)
1728 VFS_UNLOCK_GIANT(vfslocked);
1733 * Reposition read/write file offset.
1735 #ifndef _SYS_SYSPROTO_H_
1746 register struct lseek_args /* {
1753 struct ucred *cred = td->td_ucred;
1761 if ((error = fget(td, uap->fd, &fp)) != 0)
1763 if (!(fp->f_ops->fo_flags & DFLAG_SEEKABLE)) {
1768 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
1769 noneg = (vp->v_type != VCHR);
1770 offset = uap->offset;
1771 switch (uap->whence) {
1774 (fp->f_offset < 0 ||
1775 (offset > 0 && fp->f_offset > OFF_MAX - offset))) {
1779 offset += fp->f_offset;
1782 vn_lock(vp, LK_SHARED | LK_RETRY, td);
1783 error = VOP_GETATTR(vp, &vattr, cred, td);
1784 VOP_UNLOCK(vp, 0, td);
1788 (vattr.va_size > OFF_MAX ||
1789 (offset > 0 && vattr.va_size > OFF_MAX - offset))) {
1793 offset += vattr.va_size;
1798 error = fo_ioctl(fp, FIOSEEKDATA, &offset, cred, td);
1801 error = fo_ioctl(fp, FIOSEEKHOLE, &offset, cred, td);
1806 if (error == 0 && noneg && offset < 0)
1810 fp->f_offset = offset;
1811 *(off_t *)(td->td_retval) = fp->f_offset;
1814 VFS_UNLOCK_GIANT(vfslocked);
1818 #if defined(COMPAT_43)
1820 * Reposition read/write file offset.
1822 #ifndef _SYS_SYSPROTO_H_
1823 struct olseek_args {
1832 register struct olseek_args /* {
1838 struct lseek_args /* {
1846 nuap.offset = uap->offset;
1847 nuap.whence = uap->whence;
1848 return (lseek(td, &nuap));
1850 #endif /* COMPAT_43 */
1852 /* Version with the 'pad' argument */
1854 freebsd6_lseek(td, uap)
1856 register struct freebsd6_lseek_args *uap;
1858 struct lseek_args ouap;
1861 ouap.offset = uap->offset;
1862 ouap.whence = uap->whence;
1863 return (lseek(td, &ouap));
1867 * Check access permissions using passed credentials.
1870 vn_access(vp, user_flags, cred, td)
1878 /* Flags == 0 means only check for existence. */
1882 if (user_flags & R_OK)
1884 if (user_flags & W_OK)
1886 if (user_flags & X_OK)
1889 error = mac_check_vnode_access(cred, vp, flags);
1893 if ((flags & VWRITE) == 0 || (error = vn_writechk(vp)) == 0)
1894 error = VOP_ACCESS(vp, flags, cred, td);
1900 * Check access permissions using "real" credentials.
1902 #ifndef _SYS_SYSPROTO_H_
1903 struct access_args {
1911 register struct access_args /* {
1917 return (kern_access(td, uap->path, UIO_USERSPACE, uap->flags));
1921 kern_access(struct thread *td, char *path, enum uio_seg pathseg, int flags)
1923 struct ucred *cred, *tmpcred;
1924 register struct vnode *vp;
1925 struct nameidata nd;
1930 * Create and modify a temporary credential instead of one that
1931 * is potentially shared. This could also mess up socket
1932 * buffer accounting which can run in an interrupt context.
1934 cred = td->td_ucred;
1935 tmpcred = crdup(cred);
1936 tmpcred->cr_uid = cred->cr_ruid;
1937 tmpcred->cr_groups[0] = cred->cr_rgid;
1938 td->td_ucred = tmpcred;
1939 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | MPSAFE |
1940 AUDITVNODE1, pathseg, path, td);
1941 if ((error = namei(&nd)) != 0)
1943 vfslocked = NDHASGIANT(&nd);
1946 error = vn_access(vp, flags, tmpcred, td);
1947 NDFREE(&nd, NDF_ONLY_PNBUF);
1949 VFS_UNLOCK_GIANT(vfslocked);
1951 td->td_ucred = cred;
1957 * Check access permissions using "effective" credentials.
1959 #ifndef _SYS_SYSPROTO_H_
1960 struct eaccess_args {
1968 register struct eaccess_args /* {
1974 return (kern_eaccess(td, uap->path, UIO_USERSPACE, uap->flags));
1978 kern_eaccess(struct thread *td, char *path, enum uio_seg pathseg, int flags)
1980 struct nameidata nd;
1985 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | MPSAFE | AUDITVNODE1,
1987 if ((error = namei(&nd)) != 0)
1990 vfslocked = NDHASGIANT(&nd);
1991 error = vn_access(vp, flags, td->td_ucred, td);
1992 NDFREE(&nd, NDF_ONLY_PNBUF);
1994 VFS_UNLOCK_GIANT(vfslocked);
1998 #if defined(COMPAT_43)
2000 * Get file status; this version follows links.
2002 #ifndef _SYS_SYSPROTO_H_
2011 register struct ostat_args /* {
2020 error = kern_stat(td, uap->path, UIO_USERSPACE, &sb);
2024 error = copyout(&osb, uap->ub, sizeof (osb));
2029 * Get file status; this version does not follow links.
2031 #ifndef _SYS_SYSPROTO_H_
2032 struct olstat_args {
2040 register struct olstat_args /* {
2049 error = kern_lstat(td, uap->path, UIO_USERSPACE, &sb);
2053 error = copyout(&osb, uap->ub, sizeof (osb));
2058 * Convert from an old to a new stat structure.
2066 ost->st_dev = st->st_dev;
2067 ost->st_ino = st->st_ino;
2068 ost->st_mode = st->st_mode;
2069 ost->st_nlink = st->st_nlink;
2070 ost->st_uid = st->st_uid;
2071 ost->st_gid = st->st_gid;
2072 ost->st_rdev = st->st_rdev;
2073 if (st->st_size < (quad_t)1 << 32)
2074 ost->st_size = st->st_size;
2077 ost->st_atime = st->st_atime;
2078 ost->st_mtime = st->st_mtime;
2079 ost->st_ctime = st->st_ctime;
2080 ost->st_blksize = st->st_blksize;
2081 ost->st_blocks = st->st_blocks;
2082 ost->st_flags = st->st_flags;
2083 ost->st_gen = st->st_gen;
2085 #endif /* COMPAT_43 */
2088 * Get file status; this version follows links.
2090 #ifndef _SYS_SYSPROTO_H_
2099 register struct stat_args /* {
2107 error = kern_stat(td, uap->path, UIO_USERSPACE, &sb);
2109 error = copyout(&sb, uap->ub, sizeof (sb));
2114 kern_stat(struct thread *td, char *path, enum uio_seg pathseg, struct stat *sbp)
2116 struct nameidata nd;
2118 int error, vfslocked;
2121 FOLLOW | LOCKSHARED | LOCKLEAF | MPSAFE | AUDITVNODE1,
2123 if ((error = namei(&nd)) != 0)
2125 vfslocked = NDHASGIANT(&nd);
2126 error = vn_stat(nd.ni_vp, &sb, td->td_ucred, NOCRED, td);
2128 SDT_PROBE(vfs, , stat, mode, path, sb.st_mode, 0, 0, 0);
2129 if (S_ISREG(sb.st_mode))
2130 SDT_PROBE(vfs, , stat, reg, path, pathseg, 0, 0, 0);
2132 NDFREE(&nd, NDF_ONLY_PNBUF);
2134 VFS_UNLOCK_GIANT(vfslocked);
2135 if (mtx_owned(&Giant))
2136 printf("stat(%d): %s\n", vfslocked, path);
2141 if (KTRPOINT(td, KTR_STRUCT))
2148 * Get file status; this version does not follow links.
2150 #ifndef _SYS_SYSPROTO_H_
2159 register struct lstat_args /* {
2167 error = kern_lstat(td, uap->path, UIO_USERSPACE, &sb);
2169 error = copyout(&sb, uap->ub, sizeof (sb));
2174 kern_lstat(struct thread *td, char *path, enum uio_seg pathseg, struct stat *sbp)
2178 struct nameidata nd;
2179 int error, vfslocked;
2182 NOFOLLOW | LOCKLEAF | LOCKSHARED | MPSAFE | AUDITVNODE1,
2184 if ((error = namei(&nd)) != 0)
2186 vfslocked = NDHASGIANT(&nd);
2188 error = vn_stat(vp, &sb, td->td_ucred, NOCRED, td);
2189 NDFREE(&nd, NDF_ONLY_PNBUF);
2191 VFS_UNLOCK_GIANT(vfslocked);
2196 if (KTRPOINT(td, KTR_STRUCT))
2203 * Implementation of the NetBSD [l]stat() functions.
2210 bzero(nsb, sizeof *nsb);
2211 nsb->st_dev = sb->st_dev;
2212 nsb->st_ino = sb->st_ino;
2213 nsb->st_mode = sb->st_mode;
2214 nsb->st_nlink = sb->st_nlink;
2215 nsb->st_uid = sb->st_uid;
2216 nsb->st_gid = sb->st_gid;
2217 nsb->st_rdev = sb->st_rdev;
2218 nsb->st_atimespec = sb->st_atimespec;
2219 nsb->st_mtimespec = sb->st_mtimespec;
2220 nsb->st_ctimespec = sb->st_ctimespec;
2221 nsb->st_size = sb->st_size;
2222 nsb->st_blocks = sb->st_blocks;
2223 nsb->st_blksize = sb->st_blksize;
2224 nsb->st_flags = sb->st_flags;
2225 nsb->st_gen = sb->st_gen;
2226 nsb->st_birthtimespec = sb->st_birthtimespec;
2229 #ifndef _SYS_SYSPROTO_H_
2238 register struct nstat_args /* {
2247 error = kern_stat(td, uap->path, UIO_USERSPACE, &sb);
2250 cvtnstat(&sb, &nsb);
2251 error = copyout(&nsb, uap->ub, sizeof (nsb));
2256 * NetBSD lstat. Get file status; this version does not follow links.
2258 #ifndef _SYS_SYSPROTO_H_
2267 register struct nlstat_args /* {
2276 error = kern_lstat(td, uap->path, UIO_USERSPACE, &sb);
2279 cvtnstat(&sb, &nsb);
2280 error = copyout(&nsb, uap->ub, sizeof (nsb));
2285 * Get configurable pathname variables.
2287 #ifndef _SYS_SYSPROTO_H_
2288 struct pathconf_args {
2296 register struct pathconf_args /* {
2302 return (kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name));
2306 kern_pathconf(struct thread *td, char *path, enum uio_seg pathseg, int name)
2308 struct nameidata nd;
2309 int error, vfslocked;
2311 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | MPSAFE |
2312 AUDITVNODE1, pathseg, path, td);
2313 if ((error = namei(&nd)) != 0)
2315 vfslocked = NDHASGIANT(&nd);
2316 NDFREE(&nd, NDF_ONLY_PNBUF);
2318 /* If asynchronous I/O is available, it works for all files. */
2319 if (name == _PC_ASYNC_IO)
2320 td->td_retval[0] = async_io_version;
2322 error = VOP_PATHCONF(nd.ni_vp, name, td->td_retval);
2324 VFS_UNLOCK_GIANT(vfslocked);
2329 * Return target name of a symbolic link.
2331 #ifndef _SYS_SYSPROTO_H_
2332 struct readlink_args {
2341 register struct readlink_args /* {
2348 return (kern_readlink(td, uap->path, UIO_USERSPACE, uap->buf,
2349 UIO_USERSPACE, uap->count));
2353 kern_readlink(struct thread *td, char *path, enum uio_seg pathseg, char *buf,
2354 enum uio_seg bufseg, int count)
2356 register struct vnode *vp;
2360 struct nameidata nd;
2363 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKSHARED | LOCKLEAF | MPSAFE |
2364 AUDITVNODE1, pathseg, path, td);
2365 if ((error = namei(&nd)) != 0)
2367 NDFREE(&nd, NDF_ONLY_PNBUF);
2368 vfslocked = NDHASGIANT(&nd);
2371 error = mac_check_vnode_readlink(td->td_ucred, vp);
2374 VFS_UNLOCK_GIANT(vfslocked);
2378 if (vp->v_type != VLNK)
2381 aiov.iov_base = buf;
2382 aiov.iov_len = count;
2383 auio.uio_iov = &aiov;
2384 auio.uio_iovcnt = 1;
2385 auio.uio_offset = 0;
2386 auio.uio_rw = UIO_READ;
2387 auio.uio_segflg = bufseg;
2389 auio.uio_resid = count;
2390 error = VOP_READLINK(vp, &auio, td->td_ucred);
2393 VFS_UNLOCK_GIANT(vfslocked);
2394 td->td_retval[0] = count - auio.uio_resid;
2399 * Common implementation code for chflags() and fchflags().
2402 setfflags(td, vp, flags)
2412 * Prevent non-root users from setting flags on devices. When
2413 * a device is reused, users can retain ownership of the device
2414 * if they are allowed to set flags and programs assume that
2415 * chown can't fail when done as root.
2417 if (vp->v_type == VCHR || vp->v_type == VBLK) {
2418 error = priv_check(td, PRIV_VFS_CHFLAGS_DEV);
2423 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2425 VOP_LEASE(vp, td, td->td_ucred, LEASE_WRITE);
2426 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2428 vattr.va_flags = flags;
2430 error = mac_check_vnode_setflags(td->td_ucred, vp, vattr.va_flags);
2433 error = VOP_SETATTR(vp, &vattr, td->td_ucred, td);
2434 VOP_UNLOCK(vp, 0, td);
2435 vn_finished_write(mp);
2440 * Change flags of a file given a path name.
2442 #ifndef _SYS_SYSPROTO_H_
2443 struct chflags_args {
2451 register struct chflags_args /* {
2457 struct nameidata nd;
2460 AUDIT_ARG(fflags, uap->flags);
2461 NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE | AUDITVNODE1, UIO_USERSPACE,
2463 if ((error = namei(&nd)) != 0)
2465 NDFREE(&nd, NDF_ONLY_PNBUF);
2466 vfslocked = NDHASGIANT(&nd);
2467 error = setfflags(td, nd.ni_vp, uap->flags);
2469 VFS_UNLOCK_GIANT(vfslocked);
2474 * Same as chflags() but doesn't follow symlinks.
2479 register struct lchflags_args /* {
2485 struct nameidata nd;
2488 AUDIT_ARG(fflags, uap->flags);
2489 NDINIT(&nd, LOOKUP, NOFOLLOW | MPSAFE | AUDITVNODE1, UIO_USERSPACE,
2491 if ((error = namei(&nd)) != 0)
2493 vfslocked = NDHASGIANT(&nd);
2494 NDFREE(&nd, NDF_ONLY_PNBUF);
2495 error = setfflags(td, nd.ni_vp, uap->flags);
2497 VFS_UNLOCK_GIANT(vfslocked);
2502 * Change flags of a file given a file descriptor.
2504 #ifndef _SYS_SYSPROTO_H_
2505 struct fchflags_args {
2513 register struct fchflags_args /* {
2522 AUDIT_ARG(fd, uap->fd);
2523 AUDIT_ARG(fflags, uap->flags);
2524 if ((error = getvnode(td->td_proc->p_fd, uap->fd, &fp)) != 0)
2526 vfslocked = VFS_LOCK_GIANT(fp->f_vnode->v_mount);
2528 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY, td);
2529 AUDIT_ARG(vnode, fp->f_vnode, ARG_VNODE1);
2530 VOP_UNLOCK(fp->f_vnode, 0, td);
2532 error = setfflags(td, fp->f_vnode, uap->flags);
2533 VFS_UNLOCK_GIANT(vfslocked);
2539 * Common implementation code for chmod(), lchmod() and fchmod().
2542 setfmode(td, vp, mode)
2551 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2553 VOP_LEASE(vp, td, td->td_ucred, LEASE_WRITE);
2554 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2556 vattr.va_mode = mode & ALLPERMS;
2558 error = mac_check_vnode_setmode(td->td_ucred, vp, vattr.va_mode);
2561 error = VOP_SETATTR(vp, &vattr, td->td_ucred, td);
2562 VOP_UNLOCK(vp, 0, td);
2563 vn_finished_write(mp);
2568 * Change mode of a file given path name.
2570 #ifndef _SYS_SYSPROTO_H_
2579 register struct chmod_args /* {
2585 return (kern_chmod(td, uap->path, UIO_USERSPACE, uap->mode));
2589 kern_chmod(struct thread *td, char *path, enum uio_seg pathseg, int mode)
2592 struct nameidata nd;
2595 AUDIT_ARG(mode, mode);
2596 NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE | AUDITVNODE1, pathseg, path, td);
2597 if ((error = namei(&nd)) != 0)
2599 vfslocked = NDHASGIANT(&nd);
2600 NDFREE(&nd, NDF_ONLY_PNBUF);
2601 error = setfmode(td, nd.ni_vp, mode);
2603 VFS_UNLOCK_GIANT(vfslocked);
2608 * Change mode of a file given path name (don't follow links.)
2610 #ifndef _SYS_SYSPROTO_H_
2611 struct lchmod_args {
2619 register struct lchmod_args /* {
2625 struct nameidata nd;
2628 AUDIT_ARG(mode, (mode_t)uap->mode);
2629 NDINIT(&nd, LOOKUP, NOFOLLOW | MPSAFE | AUDITVNODE1, UIO_USERSPACE,
2631 if ((error = namei(&nd)) != 0)
2633 vfslocked = NDHASGIANT(&nd);
2634 NDFREE(&nd, NDF_ONLY_PNBUF);
2635 error = setfmode(td, nd.ni_vp, uap->mode);
2637 VFS_UNLOCK_GIANT(vfslocked);
2642 * Change mode of a file given a file descriptor.
2644 #ifndef _SYS_SYSPROTO_H_
2645 struct fchmod_args {
2653 register struct fchmod_args /* {
2662 AUDIT_ARG(fd, uap->fd);
2663 AUDIT_ARG(mode, uap->mode);
2664 if ((error = getvnode(td->td_proc->p_fd, uap->fd, &fp)) != 0)
2666 vfslocked = VFS_LOCK_GIANT(fp->f_vnode->v_mount);
2668 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY, td);
2669 AUDIT_ARG(vnode, fp->f_vnode, ARG_VNODE1);
2670 VOP_UNLOCK(fp->f_vnode, 0, td);
2672 error = setfmode(td, fp->f_vnode, uap->mode);
2673 VFS_UNLOCK_GIANT(vfslocked);
2679 * Common implementation for chown(), lchown(), and fchown()
2682 setfown(td, vp, uid, gid)
2692 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2694 VOP_LEASE(vp, td, td->td_ucred, LEASE_WRITE);
2695 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2700 error = mac_check_vnode_setowner(td->td_ucred, vp, vattr.va_uid,
2704 error = VOP_SETATTR(vp, &vattr, td->td_ucred, td);
2705 VOP_UNLOCK(vp, 0, td);
2706 vn_finished_write(mp);
2711 * Set ownership given a path name.
2713 #ifndef _SYS_SYSPROTO_H_
2723 register struct chown_args /* {
2730 return (kern_chown(td, uap->path, UIO_USERSPACE, uap->uid, uap->gid));
2734 kern_chown(struct thread *td, char *path, enum uio_seg pathseg, int uid,
2738 struct nameidata nd;
2741 AUDIT_ARG(owner, uid, gid);
2742 NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE | AUDITVNODE1, pathseg, path, td);
2743 if ((error = namei(&nd)) != 0)
2745 vfslocked = NDHASGIANT(&nd);
2746 NDFREE(&nd, NDF_ONLY_PNBUF);
2747 error = setfown(td, nd.ni_vp, uid, gid);
2749 VFS_UNLOCK_GIANT(vfslocked);
2754 * Set ownership given a path name, do not cross symlinks.
2756 #ifndef _SYS_SYSPROTO_H_
2757 struct lchown_args {
2766 register struct lchown_args /* {
2773 return (kern_lchown(td, uap->path, UIO_USERSPACE, uap->uid, uap->gid));
2777 kern_lchown(struct thread *td, char *path, enum uio_seg pathseg, int uid,
2781 struct nameidata nd;
2784 AUDIT_ARG(owner, uid, gid);
2785 NDINIT(&nd, LOOKUP, NOFOLLOW | MPSAFE | AUDITVNODE1, pathseg, path, td);
2786 if ((error = namei(&nd)) != 0)
2788 vfslocked = NDHASGIANT(&nd);
2789 NDFREE(&nd, NDF_ONLY_PNBUF);
2790 error = setfown(td, nd.ni_vp, uid, gid);
2792 VFS_UNLOCK_GIANT(vfslocked);
2797 * Set ownership given a file descriptor.
2799 #ifndef _SYS_SYSPROTO_H_
2800 struct fchown_args {
2809 register struct fchown_args /* {
2819 AUDIT_ARG(fd, uap->fd);
2820 AUDIT_ARG(owner, uap->uid, uap->gid);
2821 if ((error = getvnode(td->td_proc->p_fd, uap->fd, &fp)) != 0)
2823 vfslocked = VFS_LOCK_GIANT(fp->f_vnode->v_mount);
2825 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY, td);
2826 AUDIT_ARG(vnode, fp->f_vnode, ARG_VNODE1);
2827 VOP_UNLOCK(fp->f_vnode, 0, td);
2829 error = setfown(td, fp->f_vnode, uap->uid, uap->gid);
2830 VFS_UNLOCK_GIANT(vfslocked);
2836 * Common implementation code for utimes(), lutimes(), and futimes().
2839 getutimes(usrtvp, tvpseg, tsp)
2840 const struct timeval *usrtvp;
2841 enum uio_seg tvpseg;
2842 struct timespec *tsp;
2844 struct timeval tv[2];
2845 const struct timeval *tvp;
2848 if (usrtvp == NULL) {
2850 TIMEVAL_TO_TIMESPEC(&tv[0], &tsp[0]);
2853 if (tvpseg == UIO_SYSSPACE) {
2856 if ((error = copyin(usrtvp, tv, sizeof(tv))) != 0)
2861 if (tvp[0].tv_usec < 0 || tvp[0].tv_usec >= 1000000 ||
2862 tvp[1].tv_usec < 0 || tvp[1].tv_usec >= 1000000)
2864 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
2865 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
2871 * Common implementation code for utimes(), lutimes(), and futimes().
2874 setutimes(td, vp, ts, numtimes, nullflag)
2877 const struct timespec *ts;
2881 int error, setbirthtime;
2885 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2887 VOP_LEASE(vp, td, td->td_ucred, LEASE_WRITE);
2888 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2890 if (numtimes < 3 && VOP_GETATTR(vp, &vattr, td->td_ucred, td) == 0 &&
2891 timespeccmp(&ts[1], &vattr.va_birthtime, < ))
2894 vattr.va_atime = ts[0];
2895 vattr.va_mtime = ts[1];
2897 vattr.va_birthtime = ts[1];
2899 vattr.va_birthtime = ts[2];
2901 vattr.va_vaflags |= VA_UTIMES_NULL;
2903 error = mac_check_vnode_setutimes(td->td_ucred, vp, vattr.va_atime,
2907 error = VOP_SETATTR(vp, &vattr, td->td_ucred, td);
2908 VOP_UNLOCK(vp, 0, td);
2909 vn_finished_write(mp);
2914 * Set the access and modification times of a file.
2916 #ifndef _SYS_SYSPROTO_H_
2917 struct utimes_args {
2919 struct timeval *tptr;
2925 register struct utimes_args /* {
2927 struct timeval *tptr;
2931 return (kern_utimes(td, uap->path, UIO_USERSPACE, uap->tptr,
2936 kern_utimes(struct thread *td, char *path, enum uio_seg pathseg,
2937 struct timeval *tptr, enum uio_seg tptrseg)
2939 struct timespec ts[2];
2941 struct nameidata nd;
2944 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
2946 NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE | AUDITVNODE1, pathseg, path, td);
2947 if ((error = namei(&nd)) != 0)
2949 vfslocked = NDHASGIANT(&nd);
2950 NDFREE(&nd, NDF_ONLY_PNBUF);
2951 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
2953 VFS_UNLOCK_GIANT(vfslocked);
2958 * Set the access and modification times of a file.
2960 #ifndef _SYS_SYSPROTO_H_
2961 struct lutimes_args {
2963 struct timeval *tptr;
2969 register struct lutimes_args /* {
2971 struct timeval *tptr;
2975 return (kern_lutimes(td, uap->path, UIO_USERSPACE, uap->tptr,
2980 kern_lutimes(struct thread *td, char *path, enum uio_seg pathseg,
2981 struct timeval *tptr, enum uio_seg tptrseg)
2983 struct timespec ts[2];
2985 struct nameidata nd;
2988 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
2990 NDINIT(&nd, LOOKUP, NOFOLLOW | MPSAFE | AUDITVNODE1, pathseg, path, td);
2991 if ((error = namei(&nd)) != 0)
2993 vfslocked = NDHASGIANT(&nd);
2994 NDFREE(&nd, NDF_ONLY_PNBUF);
2995 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
2997 VFS_UNLOCK_GIANT(vfslocked);
3002 * Set the access and modification times of a file.
3004 #ifndef _SYS_SYSPROTO_H_
3005 struct futimes_args {
3007 struct timeval *tptr;
3013 register struct futimes_args /* {
3015 struct timeval *tptr;
3019 return (kern_futimes(td, uap->fd, uap->tptr, UIO_USERSPACE));
3023 kern_futimes(struct thread *td, int fd, struct timeval *tptr,
3024 enum uio_seg tptrseg)
3026 struct timespec ts[2];
3032 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3034 if ((error = getvnode(td->td_proc->p_fd, fd, &fp)) != 0)
3036 vfslocked = VFS_LOCK_GIANT(fp->f_vnode->v_mount);
3038 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY, td);
3039 AUDIT_ARG(vnode, fp->f_vnode, ARG_VNODE1);
3040 VOP_UNLOCK(fp->f_vnode, 0, td);
3042 error = setutimes(td, fp->f_vnode, ts, 2, tptr == NULL);
3043 VFS_UNLOCK_GIANT(vfslocked);
3049 * Truncate a file given its path name.
3051 #ifndef _SYS_SYSPROTO_H_
3052 struct truncate_args {
3061 register struct truncate_args /* {
3068 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3072 kern_truncate(struct thread *td, char *path, enum uio_seg pathseg, off_t length)
3078 struct nameidata nd;
3083 NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE | AUDITVNODE1, pathseg, path, td);
3084 if ((error = namei(&nd)) != 0)
3086 vfslocked = NDHASGIANT(&nd);
3088 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0) {
3090 VFS_UNLOCK_GIANT(vfslocked);
3093 NDFREE(&nd, NDF_ONLY_PNBUF);
3094 VOP_LEASE(vp, td, td->td_ucred, LEASE_WRITE);
3095 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
3096 if (vp->v_type == VDIR)
3099 else if ((error = mac_check_vnode_write(td->td_ucred, NOCRED, vp))) {
3102 else if ((error = vn_writechk(vp)) == 0 &&
3103 (error = VOP_ACCESS(vp, VWRITE, td->td_ucred, td)) == 0) {
3105 vattr.va_size = length;
3106 error = VOP_SETATTR(vp, &vattr, td->td_ucred, td);
3109 vn_finished_write(mp);
3110 VFS_UNLOCK_GIANT(vfslocked);
3115 * Truncate a file given a file descriptor.
3117 #ifndef _SYS_SYSPROTO_H_
3118 struct ftruncate_args {
3127 register struct ftruncate_args /* {
3140 AUDIT_ARG(fd, uap->fd);
3141 if (uap->length < 0)
3143 if ((error = getvnode(td->td_proc->p_fd, uap->fd, &fp)) != 0)
3145 if ((fp->f_flag & FWRITE) == 0) {
3150 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
3151 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
3153 VOP_LEASE(vp, td, td->td_ucred, LEASE_WRITE);
3154 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
3155 AUDIT_ARG(vnode, vp, ARG_VNODE1);
3156 if (vp->v_type == VDIR)
3159 else if ((error = mac_check_vnode_write(td->td_ucred, fp->f_cred,
3163 else if ((error = vn_writechk(vp)) == 0) {
3165 vattr.va_size = uap->length;
3166 error = VOP_SETATTR(vp, &vattr, fp->f_cred, td);
3168 VOP_UNLOCK(vp, 0, td);
3169 vn_finished_write(mp);
3171 VFS_UNLOCK_GIANT(vfslocked);
3176 #if defined(COMPAT_43)
3178 * Truncate a file given its path name.
3180 #ifndef _SYS_SYSPROTO_H_
3181 struct otruncate_args {
3189 register struct otruncate_args /* {
3194 struct truncate_args /* {
3200 nuap.path = uap->path;
3201 nuap.length = uap->length;
3202 return (truncate(td, &nuap));
3206 * Truncate a file given a file descriptor.
3208 #ifndef _SYS_SYSPROTO_H_
3209 struct oftruncate_args {
3217 register struct oftruncate_args /* {
3222 struct ftruncate_args /* {
3229 nuap.length = uap->length;
3230 return (ftruncate(td, &nuap));
3232 #endif /* COMPAT_43 */
3234 /* Versions with the pad argument */
3236 freebsd6_truncate(struct thread *td, struct freebsd6_truncate_args *uap)
3238 struct truncate_args ouap;
3240 ouap.path = uap->path;
3241 ouap.length = uap->length;
3242 return (truncate(td, &ouap));
3246 freebsd6_ftruncate(struct thread *td, struct freebsd6_ftruncate_args *uap)
3248 struct ftruncate_args ouap;
3251 ouap.length = uap->length;
3252 return (ftruncate(td, &ouap));
3256 * Sync an open file.
3258 #ifndef _SYS_SYSPROTO_H_
3266 struct fsync_args /* {
3276 AUDIT_ARG(fd, uap->fd);
3277 if ((error = getvnode(td->td_proc->p_fd, uap->fd, &fp)) != 0)
3280 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
3281 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
3283 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
3284 AUDIT_ARG(vnode, vp, ARG_VNODE1);
3285 if (vp->v_object != NULL) {
3286 VM_OBJECT_LOCK(vp->v_object);
3287 vm_object_page_clean(vp->v_object, 0, 0, 0);
3288 VM_OBJECT_UNLOCK(vp->v_object);
3290 error = VOP_FSYNC(vp, MNT_WAIT, td);
3292 VOP_UNLOCK(vp, 0, td);
3293 vn_finished_write(mp);
3295 VFS_UNLOCK_GIANT(vfslocked);
3301 * Rename files. Source and destination must either both be directories, or
3302 * both not be directories. If target is a directory, it must be empty.
3304 #ifndef _SYS_SYSPROTO_H_
3305 struct rename_args {
3313 register struct rename_args /* {
3319 return (kern_rename(td, uap->from, uap->to, UIO_USERSPACE));
3323 kern_rename(struct thread *td, char *from, char *to, enum uio_seg pathseg)
3325 struct mount *mp = NULL;
3326 struct vnode *tvp, *fvp, *tdvp;
3327 struct nameidata fromnd, tond;
3334 NDINIT(&fromnd, DELETE, LOCKPARENT | LOCKLEAF | SAVESTART | MPSAFE |
3335 AUDITVNODE1, pathseg, from, td);
3337 NDINIT(&fromnd, DELETE, WANTPARENT | SAVESTART | MPSAFE |
3338 AUDITVNODE1, pathseg, from, td);
3340 if ((error = namei(&fromnd)) != 0)
3342 fvfslocked = NDHASGIANT(&fromnd);
3345 error = mac_check_vnode_rename_from(td->td_ucred, fromnd.ni_dvp,
3346 fromnd.ni_vp, &fromnd.ni_cnd);
3347 VOP_UNLOCK(fromnd.ni_dvp, 0, td);
3348 if (fromnd.ni_dvp != fromnd.ni_vp)
3349 VOP_UNLOCK(fromnd.ni_vp, 0, td);
3353 error = vn_start_write(fvp, &mp, V_WAIT | PCATCH);
3355 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3356 vrele(fromnd.ni_dvp);
3360 NDINIT(&tond, RENAME, LOCKPARENT | LOCKLEAF | NOCACHE | SAVESTART |
3361 MPSAFE | AUDITVNODE2, pathseg, to, td);
3362 if (fromnd.ni_vp->v_type == VDIR)
3363 tond.ni_cnd.cn_flags |= WILLBEDIR;
3364 if ((error = namei(&tond)) != 0) {
3365 /* Translate error code for rename("dir1", "dir2/."). */
3366 if (error == EISDIR && fvp->v_type == VDIR)
3368 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3369 vrele(fromnd.ni_dvp);
3371 vn_finished_write(mp);
3374 tvfslocked = NDHASGIANT(&tond);
3378 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
3381 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
3389 * If the source is the same as the destination (that is, if they
3390 * are links to the same vnode), then there is nothing to do.
3396 error = mac_check_vnode_rename_to(td->td_ucred, tdvp,
3397 tond.ni_vp, fromnd.ni_dvp == tdvp, &tond.ni_cnd);
3401 VOP_LEASE(tdvp, td, td->td_ucred, LEASE_WRITE);
3402 if (fromnd.ni_dvp != tdvp) {
3403 VOP_LEASE(fromnd.ni_dvp, td, td->td_ucred, LEASE_WRITE);
3406 VOP_LEASE(tvp, td, td->td_ucred, LEASE_WRITE);
3408 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
3409 tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
3410 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3411 NDFREE(&tond, NDF_ONLY_PNBUF);
3413 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3414 NDFREE(&tond, NDF_ONLY_PNBUF);
3421 vrele(fromnd.ni_dvp);
3424 vrele(tond.ni_startdir);
3425 vn_finished_write(mp);
3427 if (fromnd.ni_startdir)
3428 vrele(fromnd.ni_startdir);
3429 VFS_UNLOCK_GIANT(fvfslocked);
3430 VFS_UNLOCK_GIANT(tvfslocked);
3437 * Make a directory file.
3439 #ifndef _SYS_SYSPROTO_H_
3448 register struct mkdir_args /* {
3454 return (kern_mkdir(td, uap->path, UIO_USERSPACE, uap->mode));
3458 kern_mkdir(struct thread *td, char *path, enum uio_seg segflg, int mode)
3464 struct nameidata nd;
3467 AUDIT_ARG(mode, mode);
3470 NDINIT(&nd, CREATE, LOCKPARENT | SAVENAME | MPSAFE | AUDITVNODE1,
3472 nd.ni_cnd.cn_flags |= WILLBEDIR;
3473 if ((error = namei(&nd)) != 0)
3475 vfslocked = NDHASGIANT(&nd);
3478 NDFREE(&nd, NDF_ONLY_PNBUF);
3480 * XXX namei called with LOCKPARENT but not LOCKLEAF has
3481 * the strange behaviour of leaving the vnode unlocked
3482 * if the target is the same vnode as the parent.
3484 if (vp == nd.ni_dvp)
3489 VFS_UNLOCK_GIANT(vfslocked);
3492 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3493 NDFREE(&nd, NDF_ONLY_PNBUF);
3495 VFS_UNLOCK_GIANT(vfslocked);
3496 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3501 vattr.va_type = VDIR;
3502 FILEDESC_SLOCK(td->td_proc->p_fd);
3503 vattr.va_mode = (mode & ACCESSPERMS) &~ td->td_proc->p_fd->fd_cmask;
3504 FILEDESC_SUNLOCK(td->td_proc->p_fd);
3506 error = mac_check_vnode_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
3511 VOP_LEASE(nd.ni_dvp, td, td->td_ucred, LEASE_WRITE);
3512 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
3516 NDFREE(&nd, NDF_ONLY_PNBUF);
3520 vn_finished_write(mp);
3521 VFS_UNLOCK_GIANT(vfslocked);
3526 * Remove a directory file.
3528 #ifndef _SYS_SYSPROTO_H_
3536 struct rmdir_args /* {
3541 return (kern_rmdir(td, uap->path, UIO_USERSPACE));
3545 kern_rmdir(struct thread *td, char *path, enum uio_seg pathseg)
3550 struct nameidata nd;
3555 NDINIT(&nd, DELETE, LOCKPARENT | LOCKLEAF | MPSAFE | AUDITVNODE1,
3557 if ((error = namei(&nd)) != 0)
3559 vfslocked = NDHASGIANT(&nd);
3561 if (vp->v_type != VDIR) {
3566 * No rmdir "." please.
3568 if (nd.ni_dvp == vp) {
3573 * The root of a mounted filesystem cannot be deleted.
3575 if (vp->v_vflag & VV_ROOT) {
3580 error = mac_check_vnode_unlink(td->td_ucred, nd.ni_dvp, vp,
3585 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3586 NDFREE(&nd, NDF_ONLY_PNBUF);
3588 if (nd.ni_dvp == vp)
3592 VFS_UNLOCK_GIANT(vfslocked);
3593 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3597 VOP_LEASE(nd.ni_dvp, td, td->td_ucred, LEASE_WRITE);
3598 VOP_LEASE(vp, td, td->td_ucred, LEASE_WRITE);
3599 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
3600 vn_finished_write(mp);
3602 NDFREE(&nd, NDF_ONLY_PNBUF);
3604 if (nd.ni_dvp == vp)
3608 VFS_UNLOCK_GIANT(vfslocked);
3614 * Read a block of directory entries in a filesystem independent format.
3616 #ifndef _SYS_SYSPROTO_H_
3617 struct ogetdirentries_args {
3625 ogetdirentries(td, uap)
3627 register struct ogetdirentries_args /* {
3636 struct uio auio, kuio;
3637 struct iovec aiov, kiov;
3638 struct dirent *dp, *edp;
3640 int error, eofflag, readcnt, vfslocked;
3643 /* XXX arbitrary sanity limit on `count'. */
3644 if (uap->count > 64 * 1024)
3646 if ((error = getvnode(td->td_proc->p_fd, uap->fd, &fp)) != 0)
3648 if ((fp->f_flag & FREAD) == 0) {
3654 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
3655 if (vp->v_type != VDIR) {
3656 VFS_UNLOCK_GIANT(vfslocked);
3660 aiov.iov_base = uap->buf;
3661 aiov.iov_len = uap->count;
3662 auio.uio_iov = &aiov;
3663 auio.uio_iovcnt = 1;
3664 auio.uio_rw = UIO_READ;
3665 auio.uio_segflg = UIO_USERSPACE;
3667 auio.uio_resid = uap->count;
3668 vn_lock(vp, LK_SHARED | LK_RETRY, td);
3669 loff = auio.uio_offset = fp->f_offset;
3671 error = mac_check_vnode_readdir(td->td_ucred, vp);
3673 VOP_UNLOCK(vp, 0, td);
3674 VFS_UNLOCK_GIANT(vfslocked);
3679 # if (BYTE_ORDER != LITTLE_ENDIAN)
3680 if (vp->v_mount->mnt_maxsymlinklen <= 0) {
3681 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag,
3683 fp->f_offset = auio.uio_offset;
3688 kuio.uio_iov = &kiov;
3689 kuio.uio_segflg = UIO_SYSSPACE;
3690 kiov.iov_len = uap->count;
3691 MALLOC(dirbuf, caddr_t, uap->count, M_TEMP, M_WAITOK);
3692 kiov.iov_base = dirbuf;
3693 error = VOP_READDIR(vp, &kuio, fp->f_cred, &eofflag,
3695 fp->f_offset = kuio.uio_offset;
3697 readcnt = uap->count - kuio.uio_resid;
3698 edp = (struct dirent *)&dirbuf[readcnt];
3699 for (dp = (struct dirent *)dirbuf; dp < edp; ) {
3700 # if (BYTE_ORDER == LITTLE_ENDIAN)
3702 * The expected low byte of
3703 * dp->d_namlen is our dp->d_type.
3704 * The high MBZ byte of dp->d_namlen
3705 * is our dp->d_namlen.
3707 dp->d_type = dp->d_namlen;
3711 * The dp->d_type is the high byte
3712 * of the expected dp->d_namlen,
3713 * so must be zero'ed.
3717 if (dp->d_reclen > 0) {
3718 dp = (struct dirent *)
3719 ((char *)dp + dp->d_reclen);
3726 error = uiomove(dirbuf, readcnt, &auio);
3728 FREE(dirbuf, M_TEMP);
3731 VOP_UNLOCK(vp, 0, td);
3732 VFS_UNLOCK_GIANT(vfslocked);
3736 if (uap->count == auio.uio_resid &&
3737 (vp->v_vflag & VV_ROOT) &&
3738 (vp->v_mount->mnt_flag & MNT_UNION)) {
3739 struct vnode *tvp = vp;
3740 vp = vp->v_mount->mnt_vnodecovered;
3746 VFS_UNLOCK_GIANT(vfslocked);
3749 VOP_UNLOCK(vp, 0, td);
3750 VFS_UNLOCK_GIANT(vfslocked);
3751 error = copyout(&loff, uap->basep, sizeof(long));
3753 td->td_retval[0] = uap->count - auio.uio_resid;
3756 #endif /* COMPAT_43 */
3759 * Read a block of directory entries in a filesystem independent format.
3761 #ifndef _SYS_SYSPROTO_H_
3762 struct getdirentries_args {
3770 getdirentries(td, uap)
3772 register struct getdirentries_args /* {
3782 error = kern_getdirentries(td, uap->fd, uap->buf, uap->count, &base);
3785 if (uap->basep != NULL)
3786 error = copyout(&base, uap->basep, sizeof(long));
3791 kern_getdirentries(struct thread *td, int fd, char *buf, u_int count,
3803 if (count > INT_MAX)
3805 if ((error = getvnode(td->td_proc->p_fd, fd, &fp)) != 0)
3807 if ((fp->f_flag & FREAD) == 0) {
3813 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
3814 if (vp->v_type != VDIR) {
3815 VFS_UNLOCK_GIANT(vfslocked);
3819 aiov.iov_base = buf;
3820 aiov.iov_len = count;
3821 auio.uio_iov = &aiov;
3822 auio.uio_iovcnt = 1;
3823 auio.uio_rw = UIO_READ;
3824 auio.uio_segflg = UIO_USERSPACE;
3826 auio.uio_resid = count;
3827 vn_lock(vp, LK_SHARED | LK_RETRY, td);
3828 AUDIT_ARG(vnode, vp, ARG_VNODE1);
3829 loff = auio.uio_offset = fp->f_offset;
3831 error = mac_check_vnode_readdir(td->td_ucred, vp);
3834 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL,
3836 fp->f_offset = auio.uio_offset;
3838 VOP_UNLOCK(vp, 0, td);
3839 VFS_UNLOCK_GIANT(vfslocked);
3842 if (count == auio.uio_resid &&
3843 (vp->v_vflag & VV_ROOT) &&
3844 (vp->v_mount->mnt_flag & MNT_UNION)) {
3845 struct vnode *tvp = vp;
3846 vp = vp->v_mount->mnt_vnodecovered;
3852 VFS_UNLOCK_GIANT(vfslocked);
3855 VOP_UNLOCK(vp, 0, td);
3856 VFS_UNLOCK_GIANT(vfslocked);
3858 td->td_retval[0] = count - auio.uio_resid;
3864 #ifndef _SYS_SYSPROTO_H_
3865 struct getdents_args {
3874 register struct getdents_args /* {
3880 struct getdirentries_args ap;
3883 ap.count = uap->count;
3885 return (getdirentries(td, &ap));
3889 * Set the mode mask for creation of filesystem nodes.
3891 #ifndef _SYS_SYSPROTO_H_
3899 struct umask_args /* {
3903 register struct filedesc *fdp;
3905 FILEDESC_XLOCK(td->td_proc->p_fd);
3906 fdp = td->td_proc->p_fd;
3907 td->td_retval[0] = fdp->fd_cmask;
3908 fdp->fd_cmask = uap->newmask & ALLPERMS;
3909 FILEDESC_XUNLOCK(td->td_proc->p_fd);
3914 * Void all references to file by ripping underlying filesystem away from
3917 #ifndef _SYS_SYSPROTO_H_
3918 struct revoke_args {
3925 register struct revoke_args /* {
3932 struct nameidata nd;
3935 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | MPSAFE | AUDITVNODE1,
3936 UIO_USERSPACE, uap->path, td);
3937 if ((error = namei(&nd)) != 0)
3939 vfslocked = NDHASGIANT(&nd);
3941 NDFREE(&nd, NDF_ONLY_PNBUF);
3942 if (vp->v_type != VCHR) {
3947 error = mac_check_vnode_revoke(td->td_ucred, vp);
3951 error = VOP_GETATTR(vp, &vattr, td->td_ucred, td);
3954 if (td->td_ucred->cr_uid != vattr.va_uid) {
3955 error = priv_check(td, PRIV_VFS_ADMIN);
3960 VOP_REVOKE(vp, REVOKEALL);
3963 VFS_UNLOCK_GIANT(vfslocked);
3968 * Convert a user file descriptor to a kernel file entry.
3969 * A reference on the file entry is held upon returning.
3972 getvnode(fdp, fd, fpp)
3973 struct filedesc *fdp;
3984 FILEDESC_SLOCK(fdp);
3985 if ((u_int)fd >= fdp->fd_nfiles ||
3986 (fp = fdp->fd_ofiles[fd]) == NULL)
3988 else if (fp->f_vnode == NULL) {
3995 FILEDESC_SUNLOCK(fdp);
4002 * Get an (NFS) file handle.
4004 #ifndef _SYS_SYSPROTO_H_
4005 struct lgetfh_args {
4013 register struct lgetfh_args *uap;
4015 struct nameidata nd;
4017 register struct vnode *vp;
4021 error = priv_check(td, PRIV_VFS_GETFH);
4024 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | MPSAFE | AUDITVNODE1,
4025 UIO_USERSPACE, uap->fname, td);
4029 vfslocked = NDHASGIANT(&nd);
4030 NDFREE(&nd, NDF_ONLY_PNBUF);
4032 bzero(&fh, sizeof(fh));
4033 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
4034 error = VOP_VPTOFH(vp, &fh.fh_fid);
4036 VFS_UNLOCK_GIANT(vfslocked);
4039 error = copyout(&fh, uap->fhp, sizeof (fh));
4043 #ifndef _SYS_SYSPROTO_H_
4052 register struct getfh_args *uap;
4054 struct nameidata nd;
4056 register struct vnode *vp;
4060 error = priv_check(td, PRIV_VFS_GETFH);
4063 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | MPSAFE | AUDITVNODE1,
4064 UIO_USERSPACE, uap->fname, td);
4068 vfslocked = NDHASGIANT(&nd);
4069 NDFREE(&nd, NDF_ONLY_PNBUF);
4071 bzero(&fh, sizeof(fh));
4072 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
4073 error = VOP_VPTOFH(vp, &fh.fh_fid);
4075 VFS_UNLOCK_GIANT(vfslocked);
4078 error = copyout(&fh, uap->fhp, sizeof (fh));
4083 * syscall for the rpc.lockd to use to translate a NFS file handle into an
4086 * warning: do not remove the priv_check() call or this becomes one giant
4089 #ifndef _SYS_SYSPROTO_H_
4090 struct fhopen_args {
4091 const struct fhandle *u_fhp;
4098 struct fhopen_args /* {
4099 const struct fhandle *u_fhp;
4103 struct proc *p = td->td_proc;
4108 struct vattr *vap = &vat;
4111 register struct filedesc *fdp = p->p_fd;
4112 int fmode, mode, error, type;
4117 error = priv_check(td, PRIV_VFS_FHOPEN);
4120 fmode = FFLAGS(uap->flags);
4121 /* why not allow a non-read/write open for our lockd? */
4122 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
4124 error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
4127 /* find the mount point */
4128 mp = vfs_getvfs(&fhp.fh_fsid);
4131 vfslocked = VFS_LOCK_GIANT(mp);
4132 /* now give me my vnode, it gets returned to me locked */
4133 error = VFS_FHTOVP(mp, &fhp.fh_fid, &vp);
4137 * from now on we have to make sure not
4138 * to forget about the vnode
4139 * any error that causes an abort must vput(vp)
4140 * just set error = err and 'goto bad;'.
4146 if (vp->v_type == VLNK) {
4150 if (vp->v_type == VSOCK) {
4155 if (fmode & (FWRITE | O_TRUNC)) {
4156 if (vp->v_type == VDIR) {
4160 error = vn_writechk(vp);
4167 if (fmode & O_APPEND)
4170 error = mac_check_vnode_open(td->td_ucred, vp, mode);
4175 error = VOP_ACCESS(vp, mode, td->td_ucred, td);
4179 if (fmode & O_TRUNC) {
4180 VOP_UNLOCK(vp, 0, td); /* XXX */
4181 if ((error = vn_start_write(NULL, &mp, V_WAIT | PCATCH)) != 0) {
4185 VOP_LEASE(vp, td, td->td_ucred, LEASE_WRITE);
4186 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td); /* XXX */
4189 * We don't yet have fp->f_cred, so use td->td_ucred, which
4192 error = mac_check_vnode_write(td->td_ucred, td->td_ucred, vp);
4197 error = VOP_SETATTR(vp, vap, td->td_ucred, td);
4201 vn_finished_write(mp);
4205 error = VOP_OPEN(vp, fmode, td->td_ucred, td, NULL);
4213 * end of vn_open code
4216 if ((error = falloc(td, &nfp, &indx)) != 0) {
4221 /* An extra reference on `nfp' has been held for us by falloc(). */
4227 nfp->f_flag = fmode & FMASK;
4228 nfp->f_type = DTYPE_VNODE;
4229 nfp->f_ops = &vnops;
4231 if (fmode & (O_EXLOCK | O_SHLOCK)) {
4232 lf.l_whence = SEEK_SET;
4235 if (fmode & O_EXLOCK)
4236 lf.l_type = F_WRLCK;
4238 lf.l_type = F_RDLCK;
4240 if ((fmode & FNONBLOCK) == 0)
4242 VOP_UNLOCK(vp, 0, td);
4243 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf,
4246 * The lock request failed. Normally close the
4247 * descriptor but handle the case where someone might
4248 * have dup()d or close()d it when we weren't looking.
4250 fdclose(fdp, fp, indx, td);
4253 * release our private reference
4258 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
4259 fp->f_flag |= FHASLOCK;
4262 VOP_UNLOCK(vp, 0, td);
4265 VFS_UNLOCK_GIANT(vfslocked);
4266 td->td_retval[0] = indx;
4273 VFS_UNLOCK_GIANT(vfslocked);
4278 * Stat an (NFS) file handle.
4280 #ifndef _SYS_SYSPROTO_H_
4281 struct fhstat_args {
4282 struct fhandle *u_fhp;
4289 register struct fhstat_args /* {
4290 struct fhandle *u_fhp;
4301 error = priv_check(td, PRIV_VFS_FHSTAT);
4304 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4307 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
4309 vfslocked = VFS_LOCK_GIANT(mp);
4310 if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp))) {
4312 VFS_UNLOCK_GIANT(vfslocked);
4315 error = vn_stat(vp, &sb, td->td_ucred, NOCRED, td);
4318 VFS_UNLOCK_GIANT(vfslocked);
4321 error = copyout(&sb, uap->sb, sizeof(sb));
4326 * Implement fstatfs() for (NFS) file handles.
4328 #ifndef _SYS_SYSPROTO_H_
4329 struct fhstatfs_args {
4330 struct fhandle *u_fhp;
4337 struct fhstatfs_args /* {
4338 struct fhandle *u_fhp;
4346 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4349 error = kern_fhstatfs(td, fh, &sf);
4352 return (copyout(&sf, uap->buf, sizeof(sf)));
4356 kern_fhstatfs(struct thread *td, fhandle_t fh, struct statfs *buf)
4364 error = priv_check(td, PRIV_VFS_FHSTATFS);
4367 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
4369 vfslocked = VFS_LOCK_GIANT(mp);
4370 error = VFS_FHTOVP(mp, &fh.fh_fid, &vp);
4372 VFS_UNLOCK_GIANT(vfslocked);
4377 error = prison_canseemount(td->td_ucred, mp);
4381 error = mac_check_mount_stat(td->td_ucred, mp);
4386 * Set these in case the underlying filesystem fails to do so.
4389 sp->f_version = STATFS_VERSION;
4390 sp->f_namemax = NAME_MAX;
4391 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
4392 error = VFS_STATFS(mp, sp, td);
4397 VFS_UNLOCK_GIANT(vfslocked);