2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
4 * (c) UNIX System Laboratories, Inc.
5 * All or some portions of this file are derived from material licensed
6 * to the University of California by American Telephone and Telegraph
7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 * the permission of UNIX System Laboratories, Inc.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 4. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * @(#)vfs_syscalls.c 8.13 (Berkeley) 4/15/94
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
40 #include "opt_capsicum.h"
41 #include "opt_compat.h"
42 #include "opt_kdtrace.h"
43 #include "opt_ktrace.h"
45 #include <sys/param.h>
46 #include <sys/systm.h>
49 #include <sys/capability.h>
51 #include <sys/sysent.h>
52 #include <sys/malloc.h>
53 #include <sys/mount.h>
54 #include <sys/mutex.h>
55 #include <sys/sysproto.h>
56 #include <sys/namei.h>
57 #include <sys/filedesc.h>
58 #include <sys/kernel.h>
59 #include <sys/fcntl.h>
61 #include <sys/filio.h>
62 #include <sys/limits.h>
63 #include <sys/linker.h>
67 #include <sys/unistd.h>
68 #include <sys/vnode.h>
71 #include <sys/dirent.h>
73 #include <sys/syscallsubr.h>
74 #include <sys/sysctl.h>
76 #include <sys/ktrace.h>
79 #include <machine/stdarg.h>
81 #include <security/audit/audit.h>
82 #include <security/mac/mac_framework.h>
85 #include <vm/vm_object.h>
86 #include <vm/vm_page.h>
89 SDT_PROVIDER_DEFINE(vfs);
90 SDT_PROBE_DEFINE(vfs, , stat, mode, mode);
91 SDT_PROBE_ARGTYPE(vfs, , stat, mode, 0, "char *");
92 SDT_PROBE_ARGTYPE(vfs, , stat, mode, 1, "int");
93 SDT_PROBE_DEFINE(vfs, , stat, reg, reg);
94 SDT_PROBE_ARGTYPE(vfs, , stat, reg, 0, "char *");
95 SDT_PROBE_ARGTYPE(vfs, , stat, reg, 1, "int");
97 static int chroot_refuse_vdir_fds(struct filedesc *fdp);
98 static int getutimes(const struct timeval *, enum uio_seg, struct timespec *);
99 static int setfown(struct thread *td, struct vnode *, uid_t, gid_t);
100 static int setfmode(struct thread *td, struct vnode *, int);
101 static int setfflags(struct thread *td, struct vnode *, int);
102 static int setutimes(struct thread *td, struct vnode *,
103 const struct timespec *, int, int);
104 static int vn_access(struct vnode *vp, int user_flags, struct ucred *cred,
108 * The module initialization routine for POSIX asynchronous I/O will
109 * set this to the version of AIO that it implements. (Zero means
110 * that it is not implemented.) This value is used here by pathconf()
111 * and in kern_descrip.c by fpathconf().
113 int async_io_version;
116 static int syncprt = 0;
117 SYSCTL_INT(_debug, OID_AUTO, syncprt, CTLFLAG_RW, &syncprt, 0, "");
121 * Sync each mounted filesystem.
123 #ifndef _SYS_SYSPROTO_H_
132 struct sync_args *uap;
134 struct mount *mp, *nmp;
137 mtx_lock(&mountlist_mtx);
138 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
139 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK)) {
140 nmp = TAILQ_NEXT(mp, mnt_list);
143 vfslocked = VFS_LOCK_GIANT(mp);
144 if ((mp->mnt_flag & MNT_RDONLY) == 0 &&
145 vn_start_write(NULL, &mp, V_NOWAIT) == 0) {
148 mp->mnt_kern_flag &= ~MNTK_ASYNC;
150 vfs_msync(mp, MNT_NOWAIT);
151 VFS_SYNC(mp, MNT_NOWAIT);
154 if ((mp->mnt_flag & MNT_ASYNC) != 0 &&
155 mp->mnt_noasync == 0)
156 mp->mnt_kern_flag |= MNTK_ASYNC;
158 vn_finished_write(mp);
160 VFS_UNLOCK_GIANT(vfslocked);
161 mtx_lock(&mountlist_mtx);
162 nmp = TAILQ_NEXT(mp, mnt_list);
165 mtx_unlock(&mountlist_mtx);
170 * Change filesystem quotas.
172 #ifndef _SYS_SYSPROTO_H_
173 struct quotactl_args {
183 register struct quotactl_args /* {
195 AUDIT_ARG_CMD(uap->cmd);
196 AUDIT_ARG_UID(uap->uid);
197 if (!prison_allow(td->td_ucred, PR_ALLOW_QUOTAS))
199 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | MPSAFE | AUDITVNODE1,
200 UIO_USERSPACE, uap->path, td);
201 if ((error = namei(&nd)) != 0)
203 vfslocked = NDHASGIANT(&nd);
204 NDFREE(&nd, NDF_ONLY_PNBUF);
205 mp = nd.ni_vp->v_mount;
208 error = vfs_busy(mp, 0);
211 VFS_UNLOCK_GIANT(vfslocked);
214 error = VFS_QUOTACTL(mp, uap->cmd, uap->uid, uap->arg);
216 VFS_UNLOCK_GIANT(vfslocked);
221 * Used by statfs conversion routines to scale the block size up if
222 * necessary so that all of the block counts are <= 'max_size'. Note
223 * that 'max_size' should be a bitmask, i.e. 2^n - 1 for some non-zero
227 statfs_scale_blocks(struct statfs *sf, long max_size)
232 KASSERT(powerof2(max_size + 1), ("%s: invalid max_size", __func__));
235 * Attempt to scale the block counts to give a more accurate
236 * overview to userland of the ratio of free space to used
237 * space. To do this, find the largest block count and compute
238 * a divisor that lets it fit into a signed integer <= max_size.
240 if (sf->f_bavail < 0)
241 count = -sf->f_bavail;
243 count = sf->f_bavail;
244 count = MAX(sf->f_blocks, MAX(sf->f_bfree, count));
245 if (count <= max_size)
248 count >>= flsl(max_size);
255 sf->f_bsize <<= shift;
256 sf->f_blocks >>= shift;
257 sf->f_bfree >>= shift;
258 sf->f_bavail >>= shift;
262 * Get filesystem statistics.
264 #ifndef _SYS_SYSPROTO_H_
273 register struct statfs_args /* {
281 error = kern_statfs(td, uap->path, UIO_USERSPACE, &sf);
283 error = copyout(&sf, uap->buf, sizeof(sf));
288 kern_statfs(struct thread *td, char *path, enum uio_seg pathseg,
292 struct statfs *sp, sb;
297 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | MPSAFE |
298 AUDITVNODE1, pathseg, path, td);
302 vfslocked = NDHASGIANT(&nd);
303 mp = nd.ni_vp->v_mount;
305 NDFREE(&nd, NDF_ONLY_PNBUF);
307 error = vfs_busy(mp, 0);
310 VFS_UNLOCK_GIANT(vfslocked);
314 error = mac_mount_check_stat(td->td_ucred, mp);
319 * Set these in case the underlying filesystem fails to do so.
322 sp->f_version = STATFS_VERSION;
323 sp->f_namemax = NAME_MAX;
324 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
325 error = VFS_STATFS(mp, sp);
328 if (priv_check(td, PRIV_VFS_GENERATION)) {
329 bcopy(sp, &sb, sizeof(sb));
330 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
331 prison_enforce_statfs(td->td_ucred, mp, &sb);
337 VFS_UNLOCK_GIANT(vfslocked);
342 * Get filesystem statistics.
344 #ifndef _SYS_SYSPROTO_H_
345 struct fstatfs_args {
353 register struct fstatfs_args /* {
361 error = kern_fstatfs(td, uap->fd, &sf);
363 error = copyout(&sf, uap->buf, sizeof(sf));
368 kern_fstatfs(struct thread *td, int fd, struct statfs *buf)
372 struct statfs *sp, sb;
378 error = getvnode(td->td_proc->p_fd, fd, CAP_FSTATFS, &fp);
382 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
383 vn_lock(vp, LK_SHARED | LK_RETRY);
385 AUDIT_ARG_VNODE1(vp);
396 error = vfs_busy(mp, 0);
399 VFS_UNLOCK_GIANT(vfslocked);
403 error = mac_mount_check_stat(td->td_ucred, mp);
408 * Set these in case the underlying filesystem fails to do so.
411 sp->f_version = STATFS_VERSION;
412 sp->f_namemax = NAME_MAX;
413 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
414 error = VFS_STATFS(mp, sp);
417 if (priv_check(td, PRIV_VFS_GENERATION)) {
418 bcopy(sp, &sb, sizeof(sb));
419 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
420 prison_enforce_statfs(td->td_ucred, mp, &sb);
427 VFS_UNLOCK_GIANT(vfslocked);
432 * Get statistics on all filesystems.
434 #ifndef _SYS_SYSPROTO_H_
435 struct getfsstat_args {
444 register struct getfsstat_args /* {
451 return (kern_getfsstat(td, &uap->buf, uap->bufsize, UIO_USERSPACE,
456 * If (bufsize > 0 && bufseg == UIO_SYSSPACE)
457 * The caller is responsible for freeing memory which will be allocated
461 kern_getfsstat(struct thread *td, struct statfs **buf, size_t bufsize,
462 enum uio_seg bufseg, int flags)
464 struct mount *mp, *nmp;
465 struct statfs *sfsp, *sp, sb;
466 size_t count, maxcount;
470 maxcount = bufsize / sizeof(struct statfs);
473 else if (bufseg == UIO_USERSPACE)
475 else /* if (bufseg == UIO_SYSSPACE) */ {
477 mtx_lock(&mountlist_mtx);
478 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
481 mtx_unlock(&mountlist_mtx);
482 if (maxcount > count)
484 sfsp = *buf = malloc(maxcount * sizeof(struct statfs), M_TEMP,
488 mtx_lock(&mountlist_mtx);
489 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
490 if (prison_canseemount(td->td_ucred, mp) != 0) {
491 nmp = TAILQ_NEXT(mp, mnt_list);
495 if (mac_mount_check_stat(td->td_ucred, mp) != 0) {
496 nmp = TAILQ_NEXT(mp, mnt_list);
500 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK)) {
501 nmp = TAILQ_NEXT(mp, mnt_list);
504 vfslocked = VFS_LOCK_GIANT(mp);
505 if (sfsp && count < maxcount) {
508 * Set these in case the underlying filesystem
511 sp->f_version = STATFS_VERSION;
512 sp->f_namemax = NAME_MAX;
513 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
515 * If MNT_NOWAIT or MNT_LAZY is specified, do not
516 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
517 * overrides MNT_WAIT.
519 if (((flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
520 (flags & MNT_WAIT)) &&
521 (error = VFS_STATFS(mp, sp))) {
522 VFS_UNLOCK_GIANT(vfslocked);
523 mtx_lock(&mountlist_mtx);
524 nmp = TAILQ_NEXT(mp, mnt_list);
528 if (priv_check(td, PRIV_VFS_GENERATION)) {
529 bcopy(sp, &sb, sizeof(sb));
530 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
531 prison_enforce_statfs(td->td_ucred, mp, &sb);
534 if (bufseg == UIO_SYSSPACE)
535 bcopy(sp, sfsp, sizeof(*sp));
536 else /* if (bufseg == UIO_USERSPACE) */ {
537 error = copyout(sp, sfsp, sizeof(*sp));
540 VFS_UNLOCK_GIANT(vfslocked);
546 VFS_UNLOCK_GIANT(vfslocked);
548 mtx_lock(&mountlist_mtx);
549 nmp = TAILQ_NEXT(mp, mnt_list);
552 mtx_unlock(&mountlist_mtx);
553 if (sfsp && count > maxcount)
554 td->td_retval[0] = maxcount;
556 td->td_retval[0] = count;
560 #ifdef COMPAT_FREEBSD4
562 * Get old format filesystem statistics.
564 static void cvtstatfs(struct statfs *, struct ostatfs *);
566 #ifndef _SYS_SYSPROTO_H_
567 struct freebsd4_statfs_args {
573 freebsd4_statfs(td, uap)
575 struct freebsd4_statfs_args /* {
584 error = kern_statfs(td, uap->path, UIO_USERSPACE, &sf);
587 cvtstatfs(&sf, &osb);
588 return (copyout(&osb, uap->buf, sizeof(osb)));
592 * Get filesystem statistics.
594 #ifndef _SYS_SYSPROTO_H_
595 struct freebsd4_fstatfs_args {
601 freebsd4_fstatfs(td, uap)
603 struct freebsd4_fstatfs_args /* {
612 error = kern_fstatfs(td, uap->fd, &sf);
615 cvtstatfs(&sf, &osb);
616 return (copyout(&osb, uap->buf, sizeof(osb)));
620 * Get statistics on all filesystems.
622 #ifndef _SYS_SYSPROTO_H_
623 struct freebsd4_getfsstat_args {
630 freebsd4_getfsstat(td, uap)
632 register struct freebsd4_getfsstat_args /* {
638 struct statfs *buf, *sp;
643 count = uap->bufsize / sizeof(struct ostatfs);
644 size = count * sizeof(struct statfs);
645 error = kern_getfsstat(td, &buf, size, UIO_SYSSPACE, uap->flags);
647 count = td->td_retval[0];
649 while (count > 0 && error == 0) {
651 error = copyout(&osb, uap->buf, sizeof(osb));
662 * Implement fstatfs() for (NFS) file handles.
664 #ifndef _SYS_SYSPROTO_H_
665 struct freebsd4_fhstatfs_args {
666 struct fhandle *u_fhp;
671 freebsd4_fhstatfs(td, uap)
673 struct freebsd4_fhstatfs_args /* {
674 struct fhandle *u_fhp;
683 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
686 error = kern_fhstatfs(td, fh, &sf);
689 cvtstatfs(&sf, &osb);
690 return (copyout(&osb, uap->buf, sizeof(osb)));
694 * Convert a new format statfs structure to an old format statfs structure.
702 statfs_scale_blocks(nsp, LONG_MAX);
703 bzero(osp, sizeof(*osp));
704 osp->f_bsize = nsp->f_bsize;
705 osp->f_iosize = MIN(nsp->f_iosize, LONG_MAX);
706 osp->f_blocks = nsp->f_blocks;
707 osp->f_bfree = nsp->f_bfree;
708 osp->f_bavail = nsp->f_bavail;
709 osp->f_files = MIN(nsp->f_files, LONG_MAX);
710 osp->f_ffree = MIN(nsp->f_ffree, LONG_MAX);
711 osp->f_owner = nsp->f_owner;
712 osp->f_type = nsp->f_type;
713 osp->f_flags = nsp->f_flags;
714 osp->f_syncwrites = MIN(nsp->f_syncwrites, LONG_MAX);
715 osp->f_asyncwrites = MIN(nsp->f_asyncwrites, LONG_MAX);
716 osp->f_syncreads = MIN(nsp->f_syncreads, LONG_MAX);
717 osp->f_asyncreads = MIN(nsp->f_asyncreads, LONG_MAX);
718 strlcpy(osp->f_fstypename, nsp->f_fstypename,
719 MIN(MFSNAMELEN, OMFSNAMELEN));
720 strlcpy(osp->f_mntonname, nsp->f_mntonname,
721 MIN(MNAMELEN, OMNAMELEN));
722 strlcpy(osp->f_mntfromname, nsp->f_mntfromname,
723 MIN(MNAMELEN, OMNAMELEN));
724 osp->f_fsid = nsp->f_fsid;
726 #endif /* COMPAT_FREEBSD4 */
729 * Change current working directory to a given file descriptor.
731 #ifndef _SYS_SYSPROTO_H_
739 struct fchdir_args /* {
743 register struct filedesc *fdp = td->td_proc->p_fd;
744 struct vnode *vp, *tdp, *vpold;
750 AUDIT_ARG_FD(uap->fd);
751 if ((error = getvnode(fdp, uap->fd, CAP_FCHDIR, &fp)) != 0)
756 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
757 vn_lock(vp, LK_SHARED | LK_RETRY);
758 AUDIT_ARG_VNODE1(vp);
759 error = change_dir(vp, td);
760 while (!error && (mp = vp->v_mountedhere) != NULL) {
764 tvfslocked = VFS_LOCK_GIANT(mp);
765 error = VFS_ROOT(mp, LK_SHARED, &tdp);
768 VFS_UNLOCK_GIANT(tvfslocked);
772 VFS_UNLOCK_GIANT(vfslocked);
774 vfslocked = tvfslocked;
778 VFS_UNLOCK_GIANT(vfslocked);
782 VFS_UNLOCK_GIANT(vfslocked);
784 vpold = fdp->fd_cdir;
786 FILEDESC_XUNLOCK(fdp);
787 vfslocked = VFS_LOCK_GIANT(vpold->v_mount);
789 VFS_UNLOCK_GIANT(vfslocked);
794 * Change current working directory (``.'').
796 #ifndef _SYS_SYSPROTO_H_
804 struct chdir_args /* {
809 return (kern_chdir(td, uap->path, UIO_USERSPACE));
813 kern_chdir(struct thread *td, char *path, enum uio_seg pathseg)
815 register struct filedesc *fdp = td->td_proc->p_fd;
821 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1 |
822 MPSAFE, pathseg, path, td);
823 if ((error = namei(&nd)) != 0)
825 vfslocked = NDHASGIANT(&nd);
826 if ((error = change_dir(nd.ni_vp, td)) != 0) {
828 VFS_UNLOCK_GIANT(vfslocked);
829 NDFREE(&nd, NDF_ONLY_PNBUF);
832 VOP_UNLOCK(nd.ni_vp, 0);
833 VFS_UNLOCK_GIANT(vfslocked);
834 NDFREE(&nd, NDF_ONLY_PNBUF);
837 fdp->fd_cdir = nd.ni_vp;
838 FILEDESC_XUNLOCK(fdp);
839 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
841 VFS_UNLOCK_GIANT(vfslocked);
846 * Helper function for raised chroot(2) security function: Refuse if
847 * any filedescriptors are open directories.
850 chroot_refuse_vdir_fds(fdp)
851 struct filedesc *fdp;
857 FILEDESC_LOCK_ASSERT(fdp);
859 for (fd = 0; fd < fdp->fd_nfiles ; fd++) {
860 fp = fget_locked(fdp, fd);
863 if (fp->f_type == DTYPE_VNODE) {
865 if (vp->v_type == VDIR)
873 * This sysctl determines if we will allow a process to chroot(2) if it
874 * has a directory open:
875 * 0: disallowed for all processes.
876 * 1: allowed for processes that were not already chroot(2)'ed.
877 * 2: allowed for all processes.
880 static int chroot_allow_open_directories = 1;
882 SYSCTL_INT(_kern, OID_AUTO, chroot_allow_open_directories, CTLFLAG_RW,
883 &chroot_allow_open_directories, 0, "");
886 * Change notion of root (``/'') directory.
888 #ifndef _SYS_SYSPROTO_H_
896 struct chroot_args /* {
904 error = priv_check(td, PRIV_VFS_CHROOT);
907 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | MPSAFE |
908 AUDITVNODE1, UIO_USERSPACE, uap->path, td);
912 vfslocked = NDHASGIANT(&nd);
913 if ((error = change_dir(nd.ni_vp, td)) != 0)
916 if ((error = mac_vnode_check_chroot(td->td_ucred, nd.ni_vp)))
919 VOP_UNLOCK(nd.ni_vp, 0);
920 error = change_root(nd.ni_vp, td);
922 VFS_UNLOCK_GIANT(vfslocked);
923 NDFREE(&nd, NDF_ONLY_PNBUF);
927 VFS_UNLOCK_GIANT(vfslocked);
929 NDFREE(&nd, NDF_ONLY_PNBUF);
934 * Common routine for chroot and chdir. Callers must provide a locked vnode
944 ASSERT_VOP_LOCKED(vp, "change_dir(): vp not locked");
945 if (vp->v_type != VDIR)
948 error = mac_vnode_check_chdir(td->td_ucred, vp);
952 error = VOP_ACCESS(vp, VEXEC, td->td_ucred, td);
957 * Common routine for kern_chroot() and jail_attach(). The caller is
958 * responsible for invoking priv_check() and mac_vnode_check_chroot() to
959 * authorize this operation.
966 struct filedesc *fdp;
971 VFS_ASSERT_GIANT(vp->v_mount);
972 fdp = td->td_proc->p_fd;
974 if (chroot_allow_open_directories == 0 ||
975 (chroot_allow_open_directories == 1 && fdp->fd_rdir != rootvnode)) {
976 error = chroot_refuse_vdir_fds(fdp);
978 FILEDESC_XUNLOCK(fdp);
982 oldvp = fdp->fd_rdir;
989 FILEDESC_XUNLOCK(fdp);
990 vfslocked = VFS_LOCK_GIANT(oldvp->v_mount);
992 VFS_UNLOCK_GIANT(vfslocked);
996 static __inline cap_rights_t
997 flags_to_rights(int flags)
999 cap_rights_t rights = 0;
1001 switch ((flags & O_ACCMODE)) {
1011 rights |= CAP_WRITE;
1015 rights |= CAP_FEXECVE;
1019 if (flags & O_CREAT)
1020 rights |= CAP_CREATE;
1022 if (flags & O_TRUNC)
1023 rights |= CAP_FTRUNCATE;
1025 if ((flags & O_EXLOCK) || (flags & O_SHLOCK))
1026 rights |= CAP_FLOCK;
1032 * Check permissions, allocate an open file structure, and call the device
1033 * open routine if any.
1035 #ifndef _SYS_SYSPROTO_H_
1045 register struct open_args /* {
1052 return (kern_open(td, uap->path, UIO_USERSPACE, uap->flags, uap->mode));
1055 #ifndef _SYS_SYSPROTO_H_
1056 struct openat_args {
1064 openat(struct thread *td, struct openat_args *uap)
1067 return (kern_openat(td, uap->fd, uap->path, UIO_USERSPACE, uap->flag,
1072 kern_open(struct thread *td, char *path, enum uio_seg pathseg, int flags,
1076 return (kern_openat(td, AT_FDCWD, path, pathseg, flags, mode));
1080 kern_openat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1081 int flags, int mode)
1083 struct proc *p = td->td_proc;
1084 struct filedesc *fdp = p->p_fd;
1089 int type, indx = -1, error, error_open;
1091 struct nameidata nd;
1093 cap_rights_t rights_needed = CAP_LOOKUP;
1095 AUDIT_ARG_FFLAGS(flags);
1096 AUDIT_ARG_MODE(mode);
1097 /* XXX: audit dirfd */
1098 rights_needed |= flags_to_rights(flags);
1100 * Only one of the O_EXEC, O_RDONLY, O_WRONLY and O_RDWR flags
1103 if (flags & O_EXEC) {
1104 if (flags & O_ACCMODE)
1106 } else if ((flags & O_ACCMODE) == O_ACCMODE)
1109 flags = FFLAGS(flags);
1112 * allocate the file descriptor, but don't install a descriptor yet
1114 error = falloc_noinstall(td, &nfp);
1117 /* An extra reference on `nfp' has been held for us by falloc_noinstall(). */
1119 /* Set the flags early so the finit in devfs can pick them up. */
1120 fp->f_flag = flags & FMASK;
1121 cmode = ((mode &~ fdp->fd_cmask) & ALLPERMS) &~ S_ISTXT;
1122 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | AUDITVNODE1 | MPSAFE, pathseg,
1123 path, fd, rights_needed, td);
1124 td->td_dupfd = -1; /* XXX check for fdopen */
1125 error = vn_open(&nd, &flags, cmode, fp);
1128 * If the vn_open replaced the method vector, something
1129 * wonderous happened deep below and we just pass it up
1130 * pretending we know what we do.
1132 if (error == ENXIO && fp->f_ops != &badfileops)
1136 * handle special fdopen() case. bleh. dupfdopen() is
1137 * responsible for dropping the old contents of ofiles[indx]
1140 * Don't do this for relative (capability) lookups; we don't
1141 * understand exactly what would happen, and we don't think
1142 * that it ever should.
1144 if ((nd.ni_strictrelative == 0) &&
1145 (error == ENODEV || error == ENXIO) &&
1146 (td->td_dupfd >= 0)) {
1147 /* XXX from fdopen */
1149 if ((error = finstall(td, fp, &indx, flags)) != 0)
1151 if ((error = dupfdopen(td, fdp, indx, td->td_dupfd,
1152 flags, error_open)) == 0)
1156 * Clean up the descriptor, but only if another thread hadn't
1157 * replaced or closed it.
1160 fdclose(fdp, fp, indx, td);
1163 if (error == ERESTART)
1168 vfslocked = NDHASGIANT(&nd);
1169 NDFREE(&nd, NDF_ONLY_PNBUF);
1173 * Store the vnode, for any f_type. Typically, the vnode use
1174 * count is decremented by direct call to vn_closefile() for
1175 * files that switched type in the cdevsw fdopen() method.
1179 * If the file wasn't claimed by devfs bind it to the normal
1180 * vnode operations here.
1182 if (fp->f_ops == &badfileops) {
1183 KASSERT(vp->v_type != VFIFO, ("Unexpected fifo."));
1185 finit(fp, flags & FMASK, DTYPE_VNODE, vp, &vnops);
1189 if (fp->f_type == DTYPE_VNODE && (flags & (O_EXLOCK | O_SHLOCK)) != 0) {
1190 lf.l_whence = SEEK_SET;
1193 if (flags & O_EXLOCK)
1194 lf.l_type = F_WRLCK;
1196 lf.l_type = F_RDLCK;
1198 if ((flags & FNONBLOCK) == 0)
1200 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf,
1203 atomic_set_int(&fp->f_flag, FHASLOCK);
1205 if (flags & O_TRUNC) {
1206 error = fo_truncate(fp, 0, td->td_ucred, td);
1210 VFS_UNLOCK_GIANT(vfslocked);
1213 * If we haven't already installed the FD (for dupfdopen), do so now.
1217 if (nd.ni_strictrelative == 1) {
1219 * We are doing a strict relative lookup; wrap the
1220 * result in a capability.
1222 if ((error = kern_capwrap(td, fp, nd.ni_baserights,
1227 if ((error = finstall(td, fp, &indx, flags)) != 0)
1233 * Release our private reference, leaving the one associated with
1234 * the descriptor table intact.
1237 td->td_retval[0] = indx;
1240 VFS_UNLOCK_GIANT(vfslocked);
1243 fdclose(fdp, fp, indx, td);
1245 td->td_retval[0] = -1;
1253 #ifndef _SYS_SYSPROTO_H_
1254 struct ocreat_args {
1262 register struct ocreat_args /* {
1268 return (kern_open(td, uap->path, UIO_USERSPACE,
1269 O_WRONLY | O_CREAT | O_TRUNC, uap->mode));
1271 #endif /* COMPAT_43 */
1274 * Create a special file.
1276 #ifndef _SYS_SYSPROTO_H_
1286 register struct mknod_args /* {
1293 return (kern_mknod(td, uap->path, UIO_USERSPACE, uap->mode, uap->dev));
1296 #ifndef _SYS_SYSPROTO_H_
1297 struct mknodat_args {
1305 mknodat(struct thread *td, struct mknodat_args *uap)
1308 return (kern_mknodat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode,
1313 kern_mknod(struct thread *td, char *path, enum uio_seg pathseg, int mode,
1317 return (kern_mknodat(td, AT_FDCWD, path, pathseg, mode, dev));
1321 kern_mknodat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1329 struct nameidata nd;
1332 AUDIT_ARG_MODE(mode);
1334 switch (mode & S_IFMT) {
1337 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
1340 error = priv_check(td, PRIV_VFS_MKNOD_BAD);
1343 error = priv_check(td, PRIV_VFS_MKNOD_WHT);
1347 return (kern_mkfifoat(td, fd, path, pathseg, mode));
1357 NDINIT_ATRIGHTS(&nd, CREATE,
1358 LOCKPARENT | SAVENAME | MPSAFE | AUDITVNODE1, pathseg, path, fd,
1360 if ((error = namei(&nd)) != 0)
1362 vfslocked = NDHASGIANT(&nd);
1365 NDFREE(&nd, NDF_ONLY_PNBUF);
1366 if (vp == nd.ni_dvp)
1371 VFS_UNLOCK_GIANT(vfslocked);
1375 vattr.va_mode = (mode & ALLPERMS) &
1376 ~td->td_proc->p_fd->fd_cmask;
1377 vattr.va_rdev = dev;
1380 switch (mode & S_IFMT) {
1381 case S_IFMT: /* used by badsect to flag bad sectors */
1382 vattr.va_type = VBAD;
1385 vattr.va_type = VCHR;
1388 vattr.va_type = VBLK;
1394 panic("kern_mknod: invalid mode");
1397 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1398 NDFREE(&nd, NDF_ONLY_PNBUF);
1400 VFS_UNLOCK_GIANT(vfslocked);
1401 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1406 if (error == 0 && !whiteout)
1407 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp,
1408 &nd.ni_cnd, &vattr);
1412 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, CREATE);
1414 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp,
1415 &nd.ni_cnd, &vattr);
1420 NDFREE(&nd, NDF_ONLY_PNBUF);
1422 vn_finished_write(mp);
1423 VFS_UNLOCK_GIANT(vfslocked);
1428 * Create a named pipe.
1430 #ifndef _SYS_SYSPROTO_H_
1431 struct mkfifo_args {
1439 register struct mkfifo_args /* {
1445 return (kern_mkfifo(td, uap->path, UIO_USERSPACE, uap->mode));
1448 #ifndef _SYS_SYSPROTO_H_
1449 struct mkfifoat_args {
1456 mkfifoat(struct thread *td, struct mkfifoat_args *uap)
1459 return (kern_mkfifoat(td, uap->fd, uap->path, UIO_USERSPACE,
1464 kern_mkfifo(struct thread *td, char *path, enum uio_seg pathseg, int mode)
1467 return (kern_mkfifoat(td, AT_FDCWD, path, pathseg, mode));
1471 kern_mkfifoat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1477 struct nameidata nd;
1480 AUDIT_ARG_MODE(mode);
1483 NDINIT_AT(&nd, CREATE, LOCKPARENT | SAVENAME | MPSAFE | AUDITVNODE1,
1484 pathseg, path, fd, td);
1485 if ((error = namei(&nd)) != 0)
1487 vfslocked = NDHASGIANT(&nd);
1488 if (nd.ni_vp != NULL) {
1489 NDFREE(&nd, NDF_ONLY_PNBUF);
1490 if (nd.ni_vp == nd.ni_dvp)
1495 VFS_UNLOCK_GIANT(vfslocked);
1498 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1499 NDFREE(&nd, NDF_ONLY_PNBUF);
1501 VFS_UNLOCK_GIANT(vfslocked);
1502 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1507 vattr.va_type = VFIFO;
1508 vattr.va_mode = (mode & ALLPERMS) & ~td->td_proc->p_fd->fd_cmask;
1510 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1515 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
1522 vn_finished_write(mp);
1523 VFS_UNLOCK_GIANT(vfslocked);
1524 NDFREE(&nd, NDF_ONLY_PNBUF);
1529 * Make a hard file link.
1531 #ifndef _SYS_SYSPROTO_H_
1540 register struct link_args /* {
1546 return (kern_link(td, uap->path, uap->link, UIO_USERSPACE));
1549 #ifndef _SYS_SYSPROTO_H_
1550 struct linkat_args {
1559 linkat(struct thread *td, struct linkat_args *uap)
1564 if (flag & ~AT_SYMLINK_FOLLOW)
1567 return (kern_linkat(td, uap->fd1, uap->fd2, uap->path1, uap->path2,
1568 UIO_USERSPACE, (flag & AT_SYMLINK_FOLLOW) ? FOLLOW : NOFOLLOW));
1571 int hardlink_check_uid = 0;
1572 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
1573 &hardlink_check_uid, 0,
1574 "Unprivileged processes cannot create hard links to files owned by other "
1576 static int hardlink_check_gid = 0;
1577 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
1578 &hardlink_check_gid, 0,
1579 "Unprivileged processes cannot create hard links to files owned by other "
1583 can_hardlink(struct vnode *vp, struct ucred *cred)
1588 if (!hardlink_check_uid && !hardlink_check_gid)
1591 error = VOP_GETATTR(vp, &va, cred);
1595 if (hardlink_check_uid && cred->cr_uid != va.va_uid) {
1596 error = priv_check_cred(cred, PRIV_VFS_LINK, 0);
1601 if (hardlink_check_gid && !groupmember(va.va_gid, cred)) {
1602 error = priv_check_cred(cred, PRIV_VFS_LINK, 0);
1611 kern_link(struct thread *td, char *path, char *link, enum uio_seg segflg)
1614 return (kern_linkat(td, AT_FDCWD, AT_FDCWD, path,link, segflg, FOLLOW));
1618 kern_linkat(struct thread *td, int fd1, int fd2, char *path1, char *path2,
1619 enum uio_seg segflg, int follow)
1623 struct nameidata nd;
1629 NDINIT_AT(&nd, LOOKUP, follow | MPSAFE | AUDITVNODE1, segflg, path1,
1632 if ((error = namei(&nd)) != 0)
1634 vfslocked = NDHASGIANT(&nd);
1635 NDFREE(&nd, NDF_ONLY_PNBUF);
1637 if (vp->v_type == VDIR) {
1639 VFS_UNLOCK_GIANT(vfslocked);
1640 return (EPERM); /* POSIX */
1642 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0) {
1644 VFS_UNLOCK_GIANT(vfslocked);
1647 NDINIT_AT(&nd, CREATE, LOCKPARENT | SAVENAME | MPSAFE | AUDITVNODE2,
1648 segflg, path2, fd2, td);
1649 if ((error = namei(&nd)) == 0) {
1650 lvfslocked = NDHASGIANT(&nd);
1651 if (nd.ni_vp != NULL) {
1652 if (nd.ni_dvp == nd.ni_vp)
1658 } else if ((error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY))
1660 error = can_hardlink(vp, td->td_ucred);
1663 error = mac_vnode_check_link(td->td_ucred,
1664 nd.ni_dvp, vp, &nd.ni_cnd);
1667 error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
1671 NDFREE(&nd, NDF_ONLY_PNBUF);
1672 VFS_UNLOCK_GIANT(lvfslocked);
1675 vn_finished_write(mp);
1676 VFS_UNLOCK_GIANT(vfslocked);
1681 * Make a symbolic link.
1683 #ifndef _SYS_SYSPROTO_H_
1684 struct symlink_args {
1692 register struct symlink_args /* {
1698 return (kern_symlink(td, uap->path, uap->link, UIO_USERSPACE));
1701 #ifndef _SYS_SYSPROTO_H_
1702 struct symlinkat_args {
1709 symlinkat(struct thread *td, struct symlinkat_args *uap)
1712 return (kern_symlinkat(td, uap->path1, uap->fd, uap->path2,
1717 kern_symlink(struct thread *td, char *path, char *link, enum uio_seg segflg)
1720 return (kern_symlinkat(td, path, AT_FDCWD, link, segflg));
1724 kern_symlinkat(struct thread *td, char *path1, int fd, char *path2,
1725 enum uio_seg segflg)
1731 struct nameidata nd;
1734 if (segflg == UIO_SYSSPACE) {
1737 syspath = uma_zalloc(namei_zone, M_WAITOK);
1738 if ((error = copyinstr(path1, syspath, MAXPATHLEN, NULL)) != 0)
1741 AUDIT_ARG_TEXT(syspath);
1744 NDINIT_AT(&nd, CREATE, LOCKPARENT | SAVENAME | MPSAFE | AUDITVNODE1,
1745 segflg, path2, fd, td);
1746 if ((error = namei(&nd)) != 0)
1748 vfslocked = NDHASGIANT(&nd);
1750 NDFREE(&nd, NDF_ONLY_PNBUF);
1751 if (nd.ni_vp == nd.ni_dvp)
1756 VFS_UNLOCK_GIANT(vfslocked);
1760 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1761 NDFREE(&nd, NDF_ONLY_PNBUF);
1763 VFS_UNLOCK_GIANT(vfslocked);
1764 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1769 vattr.va_mode = ACCESSPERMS &~ td->td_proc->p_fd->fd_cmask;
1771 vattr.va_type = VLNK;
1772 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1777 error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr, syspath);
1783 NDFREE(&nd, NDF_ONLY_PNBUF);
1785 vn_finished_write(mp);
1786 VFS_UNLOCK_GIANT(vfslocked);
1788 if (segflg != UIO_SYSSPACE)
1789 uma_zfree(namei_zone, syspath);
1794 * Delete a whiteout from the filesystem.
1799 register struct undelete_args /* {
1805 struct nameidata nd;
1810 NDINIT(&nd, DELETE, LOCKPARENT | DOWHITEOUT | MPSAFE | AUDITVNODE1,
1811 UIO_USERSPACE, uap->path, td);
1815 vfslocked = NDHASGIANT(&nd);
1817 if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & ISWHITEOUT)) {
1818 NDFREE(&nd, NDF_ONLY_PNBUF);
1819 if (nd.ni_vp == nd.ni_dvp)
1825 VFS_UNLOCK_GIANT(vfslocked);
1828 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1829 NDFREE(&nd, NDF_ONLY_PNBUF);
1831 VFS_UNLOCK_GIANT(vfslocked);
1832 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1836 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, DELETE);
1837 NDFREE(&nd, NDF_ONLY_PNBUF);
1839 vn_finished_write(mp);
1840 VFS_UNLOCK_GIANT(vfslocked);
1845 * Delete a name from the filesystem.
1847 #ifndef _SYS_SYSPROTO_H_
1848 struct unlink_args {
1855 struct unlink_args /* {
1860 return (kern_unlink(td, uap->path, UIO_USERSPACE));
1863 #ifndef _SYS_SYSPROTO_H_
1864 struct unlinkat_args {
1871 unlinkat(struct thread *td, struct unlinkat_args *uap)
1873 int flag = uap->flag;
1875 char *path = uap->path;
1877 if (flag & ~AT_REMOVEDIR)
1880 if (flag & AT_REMOVEDIR)
1881 return (kern_rmdirat(td, fd, path, UIO_USERSPACE));
1883 return (kern_unlinkat(td, fd, path, UIO_USERSPACE, 0));
1887 kern_unlink(struct thread *td, char *path, enum uio_seg pathseg)
1890 return (kern_unlinkat(td, AT_FDCWD, path, pathseg, 0));
1894 kern_unlinkat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1900 struct nameidata nd;
1906 NDINIT_AT(&nd, DELETE, LOCKPARENT | LOCKLEAF | MPSAFE | AUDITVNODE1,
1907 pathseg, path, fd, td);
1908 if ((error = namei(&nd)) != 0)
1909 return (error == EINVAL ? EPERM : error);
1910 vfslocked = NDHASGIANT(&nd);
1912 if (vp->v_type == VDIR && oldinum == 0) {
1913 error = EPERM; /* POSIX */
1914 } else if (oldinum != 0 &&
1915 ((error = vn_stat(vp, &sb, td->td_ucred, NOCRED, td)) == 0) &&
1916 sb.st_ino != oldinum) {
1917 error = EIDRM; /* Identifier removed */
1920 * The root of a mounted filesystem cannot be deleted.
1922 * XXX: can this only be a VDIR case?
1924 if (vp->v_vflag & VV_ROOT)
1928 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1929 NDFREE(&nd, NDF_ONLY_PNBUF);
1931 if (vp == nd.ni_dvp)
1935 VFS_UNLOCK_GIANT(vfslocked);
1936 if ((error = vn_start_write(NULL, &mp,
1937 V_XSLEEP | PCATCH)) != 0)
1942 error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp,
1947 error = VOP_REMOVE(nd.ni_dvp, vp, &nd.ni_cnd);
1951 vn_finished_write(mp);
1953 NDFREE(&nd, NDF_ONLY_PNBUF);
1955 if (vp == nd.ni_dvp)
1959 VFS_UNLOCK_GIANT(vfslocked);
1964 * Reposition read/write file offset.
1966 #ifndef _SYS_SYSPROTO_H_
1977 register struct lseek_args /* {
1984 struct ucred *cred = td->td_ucred;
1992 AUDIT_ARG_FD(uap->fd);
1993 if ((error = fget(td, uap->fd, CAP_SEEK, &fp)) != 0)
1995 if (!(fp->f_ops->fo_flags & DFLAG_SEEKABLE)) {
2000 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
2001 noneg = (vp->v_type != VCHR);
2002 offset = uap->offset;
2003 switch (uap->whence) {
2006 (fp->f_offset < 0 ||
2007 (offset > 0 && fp->f_offset > OFF_MAX - offset))) {
2011 offset += fp->f_offset;
2014 vn_lock(vp, LK_SHARED | LK_RETRY);
2015 error = VOP_GETATTR(vp, &vattr, cred);
2021 * If the file references a disk device, then fetch
2022 * the media size and use that to determine the ending
2025 if (vattr.va_size == 0 && vp->v_type == VCHR &&
2026 fo_ioctl(fp, DIOCGMEDIASIZE, &size, cred, td) == 0)
2027 vattr.va_size = size;
2029 (vattr.va_size > OFF_MAX ||
2030 (offset > 0 && vattr.va_size > OFF_MAX - offset))) {
2034 offset += vattr.va_size;
2039 error = fo_ioctl(fp, FIOSEEKDATA, &offset, cred, td);
2042 error = fo_ioctl(fp, FIOSEEKHOLE, &offset, cred, td);
2047 if (error == 0 && noneg && offset < 0)
2051 fp->f_offset = offset;
2052 *(off_t *)(td->td_retval) = fp->f_offset;
2055 VFS_UNLOCK_GIANT(vfslocked);
2059 #if defined(COMPAT_43)
2061 * Reposition read/write file offset.
2063 #ifndef _SYS_SYSPROTO_H_
2064 struct olseek_args {
2073 register struct olseek_args /* {
2079 struct lseek_args /* {
2087 nuap.offset = uap->offset;
2088 nuap.whence = uap->whence;
2089 return (lseek(td, &nuap));
2091 #endif /* COMPAT_43 */
2093 /* Version with the 'pad' argument */
2095 freebsd6_lseek(td, uap)
2097 register struct freebsd6_lseek_args *uap;
2099 struct lseek_args ouap;
2102 ouap.offset = uap->offset;
2103 ouap.whence = uap->whence;
2104 return (lseek(td, &ouap));
2108 * Check access permissions using passed credentials.
2111 vn_access(vp, user_flags, cred, td)
2120 /* Flags == 0 means only check for existence. */
2124 if (user_flags & R_OK)
2126 if (user_flags & W_OK)
2128 if (user_flags & X_OK)
2131 error = mac_vnode_check_access(cred, vp, accmode);
2135 if ((accmode & VWRITE) == 0 || (error = vn_writechk(vp)) == 0)
2136 error = VOP_ACCESS(vp, accmode, cred, td);
2142 * Check access permissions using "real" credentials.
2144 #ifndef _SYS_SYSPROTO_H_
2145 struct access_args {
2153 register struct access_args /* {
2159 return (kern_access(td, uap->path, UIO_USERSPACE, uap->flags));
2162 #ifndef _SYS_SYSPROTO_H_
2163 struct faccessat_args {
2171 faccessat(struct thread *td, struct faccessat_args *uap)
2174 if (uap->flag & ~AT_EACCESS)
2176 return (kern_accessat(td, uap->fd, uap->path, UIO_USERSPACE, uap->flag,
2181 kern_access(struct thread *td, char *path, enum uio_seg pathseg, int mode)
2184 return (kern_accessat(td, AT_FDCWD, path, pathseg, 0, mode));
2188 kern_accessat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2189 int flags, int mode)
2191 struct ucred *cred, *tmpcred;
2193 struct nameidata nd;
2198 * Create and modify a temporary credential instead of one that
2199 * is potentially shared.
2201 if (!(flags & AT_EACCESS)) {
2202 cred = td->td_ucred;
2203 tmpcred = crdup(cred);
2204 tmpcred->cr_uid = cred->cr_ruid;
2205 tmpcred->cr_groups[0] = cred->cr_rgid;
2206 td->td_ucred = tmpcred;
2208 cred = tmpcred = td->td_ucred;
2209 AUDIT_ARG_VALUE(mode);
2210 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | MPSAFE |
2211 AUDITVNODE1, pathseg, path, fd, CAP_FSTAT, td);
2212 if ((error = namei(&nd)) != 0)
2214 vfslocked = NDHASGIANT(&nd);
2217 error = vn_access(vp, mode, tmpcred, td);
2218 NDFREE(&nd, NDF_ONLY_PNBUF);
2220 VFS_UNLOCK_GIANT(vfslocked);
2222 if (!(flags & AT_EACCESS)) {
2223 td->td_ucred = cred;
2230 * Check access permissions using "effective" credentials.
2232 #ifndef _SYS_SYSPROTO_H_
2233 struct eaccess_args {
2241 register struct eaccess_args /* {
2247 return (kern_eaccess(td, uap->path, UIO_USERSPACE, uap->flags));
2251 kern_eaccess(struct thread *td, char *path, enum uio_seg pathseg, int flags)
2254 return (kern_accessat(td, AT_FDCWD, path, pathseg, AT_EACCESS, flags));
2257 #if defined(COMPAT_43)
2259 * Get file status; this version follows links.
2261 #ifndef _SYS_SYSPROTO_H_
2270 register struct ostat_args /* {
2279 error = kern_stat(td, uap->path, UIO_USERSPACE, &sb);
2283 error = copyout(&osb, uap->ub, sizeof (osb));
2288 * Get file status; this version does not follow links.
2290 #ifndef _SYS_SYSPROTO_H_
2291 struct olstat_args {
2299 register struct olstat_args /* {
2308 error = kern_lstat(td, uap->path, UIO_USERSPACE, &sb);
2312 error = copyout(&osb, uap->ub, sizeof (osb));
2317 * Convert from an old to a new stat structure.
2325 ost->st_dev = st->st_dev;
2326 ost->st_ino = st->st_ino;
2327 ost->st_mode = st->st_mode;
2328 ost->st_nlink = st->st_nlink;
2329 ost->st_uid = st->st_uid;
2330 ost->st_gid = st->st_gid;
2331 ost->st_rdev = st->st_rdev;
2332 if (st->st_size < (quad_t)1 << 32)
2333 ost->st_size = st->st_size;
2336 ost->st_atim = st->st_atim;
2337 ost->st_mtim = st->st_mtim;
2338 ost->st_ctim = st->st_ctim;
2339 ost->st_blksize = st->st_blksize;
2340 ost->st_blocks = st->st_blocks;
2341 ost->st_flags = st->st_flags;
2342 ost->st_gen = st->st_gen;
2344 #endif /* COMPAT_43 */
2347 * Get file status; this version follows links.
2349 #ifndef _SYS_SYSPROTO_H_
2358 register struct stat_args /* {
2366 error = kern_stat(td, uap->path, UIO_USERSPACE, &sb);
2368 error = copyout(&sb, uap->ub, sizeof (sb));
2372 #ifndef _SYS_SYSPROTO_H_
2373 struct fstatat_args {
2381 fstatat(struct thread *td, struct fstatat_args *uap)
2386 error = kern_statat(td, uap->flag, uap->fd, uap->path,
2387 UIO_USERSPACE, &sb);
2389 error = copyout(&sb, uap->buf, sizeof (sb));
2394 kern_stat(struct thread *td, char *path, enum uio_seg pathseg, struct stat *sbp)
2397 return (kern_statat(td, 0, AT_FDCWD, path, pathseg, sbp));
2401 kern_statat(struct thread *td, int flag, int fd, char *path,
2402 enum uio_seg pathseg, struct stat *sbp)
2405 return (kern_statat_vnhook(td, flag, fd, path, pathseg, sbp, NULL));
2409 kern_statat_vnhook(struct thread *td, int flag, int fd, char *path,
2410 enum uio_seg pathseg, struct stat *sbp,
2411 void (*hook)(struct vnode *vp, struct stat *sbp))
2413 struct nameidata nd;
2415 int error, vfslocked;
2417 if (flag & ~AT_SYMLINK_NOFOLLOW)
2420 NDINIT_ATRIGHTS(&nd, LOOKUP, ((flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW :
2421 FOLLOW) | LOCKSHARED | LOCKLEAF | AUDITVNODE1 | MPSAFE, pathseg,
2422 path, fd, CAP_FSTAT, td);
2424 if ((error = namei(&nd)) != 0)
2426 vfslocked = NDHASGIANT(&nd);
2427 error = vn_stat(nd.ni_vp, &sb, td->td_ucred, NOCRED, td);
2429 SDT_PROBE(vfs, , stat, mode, path, sb.st_mode, 0, 0, 0);
2430 if (S_ISREG(sb.st_mode))
2431 SDT_PROBE(vfs, , stat, reg, path, pathseg, 0, 0, 0);
2432 if (__predict_false(hook != NULL))
2433 hook(nd.ni_vp, &sb);
2435 NDFREE(&nd, NDF_ONLY_PNBUF);
2437 VFS_UNLOCK_GIANT(vfslocked);
2442 if (KTRPOINT(td, KTR_STRUCT))
2449 * Get file status; this version does not follow links.
2451 #ifndef _SYS_SYSPROTO_H_
2460 register struct lstat_args /* {
2468 error = kern_lstat(td, uap->path, UIO_USERSPACE, &sb);
2470 error = copyout(&sb, uap->ub, sizeof (sb));
2475 kern_lstat(struct thread *td, char *path, enum uio_seg pathseg, struct stat *sbp)
2478 return (kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, path, pathseg,
2483 * Implementation of the NetBSD [l]stat() functions.
2490 bzero(nsb, sizeof *nsb);
2491 nsb->st_dev = sb->st_dev;
2492 nsb->st_ino = sb->st_ino;
2493 nsb->st_mode = sb->st_mode;
2494 nsb->st_nlink = sb->st_nlink;
2495 nsb->st_uid = sb->st_uid;
2496 nsb->st_gid = sb->st_gid;
2497 nsb->st_rdev = sb->st_rdev;
2498 nsb->st_atim = sb->st_atim;
2499 nsb->st_mtim = sb->st_mtim;
2500 nsb->st_ctim = sb->st_ctim;
2501 nsb->st_size = sb->st_size;
2502 nsb->st_blocks = sb->st_blocks;
2503 nsb->st_blksize = sb->st_blksize;
2504 nsb->st_flags = sb->st_flags;
2505 nsb->st_gen = sb->st_gen;
2506 nsb->st_birthtim = sb->st_birthtim;
2509 #ifndef _SYS_SYSPROTO_H_
2518 register struct nstat_args /* {
2527 error = kern_stat(td, uap->path, UIO_USERSPACE, &sb);
2530 cvtnstat(&sb, &nsb);
2531 error = copyout(&nsb, uap->ub, sizeof (nsb));
2536 * NetBSD lstat. Get file status; this version does not follow links.
2538 #ifndef _SYS_SYSPROTO_H_
2547 register struct nlstat_args /* {
2556 error = kern_lstat(td, uap->path, UIO_USERSPACE, &sb);
2559 cvtnstat(&sb, &nsb);
2560 error = copyout(&nsb, uap->ub, sizeof (nsb));
2565 * Get configurable pathname variables.
2567 #ifndef _SYS_SYSPROTO_H_
2568 struct pathconf_args {
2576 register struct pathconf_args /* {
2582 return (kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name, FOLLOW));
2585 #ifndef _SYS_SYSPROTO_H_
2586 struct lpathconf_args {
2594 register struct lpathconf_args /* {
2600 return (kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name, NOFOLLOW));
2604 kern_pathconf(struct thread *td, char *path, enum uio_seg pathseg, int name,
2607 struct nameidata nd;
2608 int error, vfslocked;
2610 NDINIT(&nd, LOOKUP, LOCKSHARED | LOCKLEAF | MPSAFE | AUDITVNODE1 |
2611 flags, pathseg, path, td);
2612 if ((error = namei(&nd)) != 0)
2614 vfslocked = NDHASGIANT(&nd);
2615 NDFREE(&nd, NDF_ONLY_PNBUF);
2617 /* If asynchronous I/O is available, it works for all files. */
2618 if (name == _PC_ASYNC_IO)
2619 td->td_retval[0] = async_io_version;
2621 error = VOP_PATHCONF(nd.ni_vp, name, td->td_retval);
2623 VFS_UNLOCK_GIANT(vfslocked);
2628 * Return target name of a symbolic link.
2630 #ifndef _SYS_SYSPROTO_H_
2631 struct readlink_args {
2640 register struct readlink_args /* {
2647 return (kern_readlink(td, uap->path, UIO_USERSPACE, uap->buf,
2648 UIO_USERSPACE, uap->count));
2650 #ifndef _SYS_SYSPROTO_H_
2651 struct readlinkat_args {
2659 readlinkat(struct thread *td, struct readlinkat_args *uap)
2662 return (kern_readlinkat(td, uap->fd, uap->path, UIO_USERSPACE,
2663 uap->buf, UIO_USERSPACE, uap->bufsize));
2667 kern_readlink(struct thread *td, char *path, enum uio_seg pathseg, char *buf,
2668 enum uio_seg bufseg, size_t count)
2671 return (kern_readlinkat(td, AT_FDCWD, path, pathseg, buf, bufseg,
2676 kern_readlinkat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2677 char *buf, enum uio_seg bufseg, size_t count)
2683 struct nameidata nd;
2686 if (count > INT_MAX)
2689 NDINIT_AT(&nd, LOOKUP, NOFOLLOW | LOCKSHARED | LOCKLEAF | MPSAFE |
2690 AUDITVNODE1, pathseg, path, fd, td);
2692 if ((error = namei(&nd)) != 0)
2694 NDFREE(&nd, NDF_ONLY_PNBUF);
2695 vfslocked = NDHASGIANT(&nd);
2698 error = mac_vnode_check_readlink(td->td_ucred, vp);
2701 VFS_UNLOCK_GIANT(vfslocked);
2705 if (vp->v_type != VLNK)
2708 aiov.iov_base = buf;
2709 aiov.iov_len = count;
2710 auio.uio_iov = &aiov;
2711 auio.uio_iovcnt = 1;
2712 auio.uio_offset = 0;
2713 auio.uio_rw = UIO_READ;
2714 auio.uio_segflg = bufseg;
2716 auio.uio_resid = count;
2717 error = VOP_READLINK(vp, &auio, td->td_ucred);
2720 VFS_UNLOCK_GIANT(vfslocked);
2721 td->td_retval[0] = count - auio.uio_resid;
2726 * Common implementation code for chflags() and fchflags().
2729 setfflags(td, vp, flags)
2739 * Prevent non-root users from setting flags on devices. When
2740 * a device is reused, users can retain ownership of the device
2741 * if they are allowed to set flags and programs assume that
2742 * chown can't fail when done as root.
2744 if (vp->v_type == VCHR || vp->v_type == VBLK) {
2745 error = priv_check(td, PRIV_VFS_CHFLAGS_DEV);
2750 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2752 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2754 vattr.va_flags = flags;
2756 error = mac_vnode_check_setflags(td->td_ucred, vp, vattr.va_flags);
2759 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
2761 vn_finished_write(mp);
2766 * Change flags of a file given a path name.
2768 #ifndef _SYS_SYSPROTO_H_
2769 struct chflags_args {
2777 register struct chflags_args /* {
2783 struct nameidata nd;
2786 AUDIT_ARG_FFLAGS(uap->flags);
2787 NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE | AUDITVNODE1, UIO_USERSPACE,
2789 if ((error = namei(&nd)) != 0)
2791 NDFREE(&nd, NDF_ONLY_PNBUF);
2792 vfslocked = NDHASGIANT(&nd);
2793 error = setfflags(td, nd.ni_vp, uap->flags);
2795 VFS_UNLOCK_GIANT(vfslocked);
2800 * Same as chflags() but doesn't follow symlinks.
2805 register struct lchflags_args /* {
2811 struct nameidata nd;
2814 AUDIT_ARG_FFLAGS(uap->flags);
2815 NDINIT(&nd, LOOKUP, NOFOLLOW | MPSAFE | AUDITVNODE1, UIO_USERSPACE,
2817 if ((error = namei(&nd)) != 0)
2819 vfslocked = NDHASGIANT(&nd);
2820 NDFREE(&nd, NDF_ONLY_PNBUF);
2821 error = setfflags(td, nd.ni_vp, uap->flags);
2823 VFS_UNLOCK_GIANT(vfslocked);
2828 * Change flags of a file given a file descriptor.
2830 #ifndef _SYS_SYSPROTO_H_
2831 struct fchflags_args {
2839 register struct fchflags_args /* {
2848 AUDIT_ARG_FD(uap->fd);
2849 AUDIT_ARG_FFLAGS(uap->flags);
2850 if ((error = getvnode(td->td_proc->p_fd, uap->fd, CAP_FCHFLAGS,
2853 vfslocked = VFS_LOCK_GIANT(fp->f_vnode->v_mount);
2855 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
2856 AUDIT_ARG_VNODE1(fp->f_vnode);
2857 VOP_UNLOCK(fp->f_vnode, 0);
2859 error = setfflags(td, fp->f_vnode, uap->flags);
2860 VFS_UNLOCK_GIANT(vfslocked);
2866 * Common implementation code for chmod(), lchmod() and fchmod().
2869 setfmode(td, vp, mode)
2878 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2880 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2882 vattr.va_mode = mode & ALLPERMS;
2884 error = mac_vnode_check_setmode(td->td_ucred, vp, vattr.va_mode);
2887 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
2889 vn_finished_write(mp);
2894 * Change mode of a file given path name.
2896 #ifndef _SYS_SYSPROTO_H_
2905 register struct chmod_args /* {
2911 return (kern_chmod(td, uap->path, UIO_USERSPACE, uap->mode));
2914 #ifndef _SYS_SYSPROTO_H_
2915 struct fchmodat_args {
2923 fchmodat(struct thread *td, struct fchmodat_args *uap)
2925 int flag = uap->flag;
2927 char *path = uap->path;
2928 mode_t mode = uap->mode;
2930 if (flag & ~AT_SYMLINK_NOFOLLOW)
2933 return (kern_fchmodat(td, fd, path, UIO_USERSPACE, mode, flag));
2937 kern_chmod(struct thread *td, char *path, enum uio_seg pathseg, int mode)
2940 return (kern_fchmodat(td, AT_FDCWD, path, pathseg, mode, 0));
2944 * Change mode of a file given path name (don't follow links.)
2946 #ifndef _SYS_SYSPROTO_H_
2947 struct lchmod_args {
2955 register struct lchmod_args /* {
2961 return (kern_fchmodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2962 uap->mode, AT_SYMLINK_NOFOLLOW));
2967 kern_fchmodat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2968 mode_t mode, int flag)
2971 struct nameidata nd;
2975 AUDIT_ARG_MODE(mode);
2976 follow = (flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
2977 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | MPSAFE | AUDITVNODE1, pathseg,
2978 path, fd, CAP_FCHMOD, td);
2979 if ((error = namei(&nd)) != 0)
2981 vfslocked = NDHASGIANT(&nd);
2982 NDFREE(&nd, NDF_ONLY_PNBUF);
2983 error = setfmode(td, nd.ni_vp, mode);
2985 VFS_UNLOCK_GIANT(vfslocked);
2990 * Change mode of a file given a file descriptor.
2992 #ifndef _SYS_SYSPROTO_H_
2993 struct fchmod_args {
3001 register struct fchmod_args /* {
3010 AUDIT_ARG_FD(uap->fd);
3011 AUDIT_ARG_MODE(uap->mode);
3012 if ((error = getvnode(td->td_proc->p_fd, uap->fd, CAP_FCHMOD,
3015 vfslocked = VFS_LOCK_GIANT(fp->f_vnode->v_mount);
3017 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
3018 AUDIT_ARG_VNODE1(fp->f_vnode);
3019 VOP_UNLOCK(fp->f_vnode, 0);
3021 error = setfmode(td, fp->f_vnode, uap->mode);
3022 VFS_UNLOCK_GIANT(vfslocked);
3028 * Common implementation for chown(), lchown(), and fchown()
3031 setfown(td, vp, uid, gid)
3041 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
3043 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3048 error = mac_vnode_check_setowner(td->td_ucred, vp, vattr.va_uid,
3052 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3054 vn_finished_write(mp);
3059 * Set ownership given a path name.
3061 #ifndef _SYS_SYSPROTO_H_
3071 register struct chown_args /* {
3078 return (kern_chown(td, uap->path, UIO_USERSPACE, uap->uid, uap->gid));
3081 #ifndef _SYS_SYSPROTO_H_
3082 struct fchownat_args {
3091 fchownat(struct thread *td, struct fchownat_args *uap)
3096 if (flag & ~AT_SYMLINK_NOFOLLOW)
3099 return (kern_fchownat(td, uap->fd, uap->path, UIO_USERSPACE, uap->uid,
3100 uap->gid, uap->flag));
3104 kern_chown(struct thread *td, char *path, enum uio_seg pathseg, int uid,
3108 return (kern_fchownat(td, AT_FDCWD, path, pathseg, uid, gid, 0));
3112 kern_fchownat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
3113 int uid, int gid, int flag)
3115 struct nameidata nd;
3116 int error, vfslocked, follow;
3118 AUDIT_ARG_OWNER(uid, gid);
3119 follow = (flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
3120 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | MPSAFE | AUDITVNODE1, pathseg,
3121 path, fd, CAP_FCHOWN, td);
3123 if ((error = namei(&nd)) != 0)
3125 vfslocked = NDHASGIANT(&nd);
3126 NDFREE(&nd, NDF_ONLY_PNBUF);
3127 error = setfown(td, nd.ni_vp, uid, gid);
3129 VFS_UNLOCK_GIANT(vfslocked);
3134 * Set ownership given a path name, do not cross symlinks.
3136 #ifndef _SYS_SYSPROTO_H_
3137 struct lchown_args {
3146 register struct lchown_args /* {
3153 return (kern_lchown(td, uap->path, UIO_USERSPACE, uap->uid, uap->gid));
3157 kern_lchown(struct thread *td, char *path, enum uio_seg pathseg, int uid,
3161 return (kern_fchownat(td, AT_FDCWD, path, pathseg, uid, gid,
3162 AT_SYMLINK_NOFOLLOW));
3166 * Set ownership given a file descriptor.
3168 #ifndef _SYS_SYSPROTO_H_
3169 struct fchown_args {
3178 register struct fchown_args /* {
3188 AUDIT_ARG_FD(uap->fd);
3189 AUDIT_ARG_OWNER(uap->uid, uap->gid);
3190 if ((error = getvnode(td->td_proc->p_fd, uap->fd, CAP_FCHOWN, &fp))
3193 vfslocked = VFS_LOCK_GIANT(fp->f_vnode->v_mount);
3195 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
3196 AUDIT_ARG_VNODE1(fp->f_vnode);
3197 VOP_UNLOCK(fp->f_vnode, 0);
3199 error = setfown(td, fp->f_vnode, uap->uid, uap->gid);
3200 VFS_UNLOCK_GIANT(vfslocked);
3206 * Common implementation code for utimes(), lutimes(), and futimes().
3209 getutimes(usrtvp, tvpseg, tsp)
3210 const struct timeval *usrtvp;
3211 enum uio_seg tvpseg;
3212 struct timespec *tsp;
3214 struct timeval tv[2];
3215 const struct timeval *tvp;
3218 if (usrtvp == NULL) {
3219 vfs_timestamp(&tsp[0]);
3222 if (tvpseg == UIO_SYSSPACE) {
3225 if ((error = copyin(usrtvp, tv, sizeof(tv))) != 0)
3230 if (tvp[0].tv_usec < 0 || tvp[0].tv_usec >= 1000000 ||
3231 tvp[1].tv_usec < 0 || tvp[1].tv_usec >= 1000000)
3233 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
3234 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
3240 * Common implementation code for utimes(), lutimes(), and futimes().
3243 setutimes(td, vp, ts, numtimes, nullflag)
3246 const struct timespec *ts;
3250 int error, setbirthtime;
3254 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
3256 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3258 if (numtimes < 3 && !VOP_GETATTR(vp, &vattr, td->td_ucred) &&
3259 timespeccmp(&ts[1], &vattr.va_birthtime, < ))
3262 vattr.va_atime = ts[0];
3263 vattr.va_mtime = ts[1];
3265 vattr.va_birthtime = ts[1];
3267 vattr.va_birthtime = ts[2];
3269 vattr.va_vaflags |= VA_UTIMES_NULL;
3271 error = mac_vnode_check_setutimes(td->td_ucred, vp, vattr.va_atime,
3275 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3277 vn_finished_write(mp);
3282 * Set the access and modification times of a file.
3284 #ifndef _SYS_SYSPROTO_H_
3285 struct utimes_args {
3287 struct timeval *tptr;
3293 register struct utimes_args /* {
3295 struct timeval *tptr;
3299 return (kern_utimes(td, uap->path, UIO_USERSPACE, uap->tptr,
3303 #ifndef _SYS_SYSPROTO_H_
3304 struct futimesat_args {
3307 const struct timeval * times;
3311 futimesat(struct thread *td, struct futimesat_args *uap)
3314 return (kern_utimesat(td, uap->fd, uap->path, UIO_USERSPACE,
3315 uap->times, UIO_USERSPACE));
3319 kern_utimes(struct thread *td, char *path, enum uio_seg pathseg,
3320 struct timeval *tptr, enum uio_seg tptrseg)
3323 return (kern_utimesat(td, AT_FDCWD, path, pathseg, tptr, tptrseg));
3327 kern_utimesat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
3328 struct timeval *tptr, enum uio_seg tptrseg)
3330 struct nameidata nd;
3331 struct timespec ts[2];
3332 int error, vfslocked;
3334 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3336 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | MPSAFE | AUDITVNODE1, pathseg,
3337 path, fd, CAP_FUTIMES, td);
3339 if ((error = namei(&nd)) != 0)
3341 vfslocked = NDHASGIANT(&nd);
3342 NDFREE(&nd, NDF_ONLY_PNBUF);
3343 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3345 VFS_UNLOCK_GIANT(vfslocked);
3350 * Set the access and modification times of a file.
3352 #ifndef _SYS_SYSPROTO_H_
3353 struct lutimes_args {
3355 struct timeval *tptr;
3361 register struct lutimes_args /* {
3363 struct timeval *tptr;
3367 return (kern_lutimes(td, uap->path, UIO_USERSPACE, uap->tptr,
3372 kern_lutimes(struct thread *td, char *path, enum uio_seg pathseg,
3373 struct timeval *tptr, enum uio_seg tptrseg)
3375 struct timespec ts[2];
3377 struct nameidata nd;
3380 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3382 NDINIT(&nd, LOOKUP, NOFOLLOW | MPSAFE | AUDITVNODE1, pathseg, path, td);
3383 if ((error = namei(&nd)) != 0)
3385 vfslocked = NDHASGIANT(&nd);
3386 NDFREE(&nd, NDF_ONLY_PNBUF);
3387 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3389 VFS_UNLOCK_GIANT(vfslocked);
3394 * Set the access and modification times of a file.
3396 #ifndef _SYS_SYSPROTO_H_
3397 struct futimes_args {
3399 struct timeval *tptr;
3405 register struct futimes_args /* {
3407 struct timeval *tptr;
3411 return (kern_futimes(td, uap->fd, uap->tptr, UIO_USERSPACE));
3415 kern_futimes(struct thread *td, int fd, struct timeval *tptr,
3416 enum uio_seg tptrseg)
3418 struct timespec ts[2];
3424 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3426 if ((error = getvnode(td->td_proc->p_fd, fd, CAP_FUTIMES, &fp))
3429 vfslocked = VFS_LOCK_GIANT(fp->f_vnode->v_mount);
3431 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
3432 AUDIT_ARG_VNODE1(fp->f_vnode);
3433 VOP_UNLOCK(fp->f_vnode, 0);
3435 error = setutimes(td, fp->f_vnode, ts, 2, tptr == NULL);
3436 VFS_UNLOCK_GIANT(vfslocked);
3442 * Truncate a file given its path name.
3444 #ifndef _SYS_SYSPROTO_H_
3445 struct truncate_args {
3454 register struct truncate_args /* {
3461 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3465 kern_truncate(struct thread *td, char *path, enum uio_seg pathseg, off_t length)
3471 struct nameidata nd;
3476 NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE | AUDITVNODE1, pathseg, path, td);
3477 if ((error = namei(&nd)) != 0)
3479 vfslocked = NDHASGIANT(&nd);
3481 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0) {
3483 VFS_UNLOCK_GIANT(vfslocked);
3486 NDFREE(&nd, NDF_ONLY_PNBUF);
3487 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3488 if (vp->v_type == VDIR)
3491 else if ((error = mac_vnode_check_write(td->td_ucred, NOCRED, vp))) {
3494 else if ((error = vn_writechk(vp)) == 0 &&
3495 (error = VOP_ACCESS(vp, VWRITE, td->td_ucred, td)) == 0) {
3497 vattr.va_size = length;
3498 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3501 vn_finished_write(mp);
3502 VFS_UNLOCK_GIANT(vfslocked);
3506 #if defined(COMPAT_43)
3508 * Truncate a file given its path name.
3510 #ifndef _SYS_SYSPROTO_H_
3511 struct otruncate_args {
3519 register struct otruncate_args /* {
3524 struct truncate_args /* {
3530 nuap.path = uap->path;
3531 nuap.length = uap->length;
3532 return (truncate(td, &nuap));
3534 #endif /* COMPAT_43 */
3536 /* Versions with the pad argument */
3538 freebsd6_truncate(struct thread *td, struct freebsd6_truncate_args *uap)
3540 struct truncate_args ouap;
3542 ouap.path = uap->path;
3543 ouap.length = uap->length;
3544 return (truncate(td, &ouap));
3548 freebsd6_ftruncate(struct thread *td, struct freebsd6_ftruncate_args *uap)
3550 struct ftruncate_args ouap;
3553 ouap.length = uap->length;
3554 return (ftruncate(td, &ouap));
3558 * Sync an open file.
3560 #ifndef _SYS_SYSPROTO_H_
3568 struct fsync_args /* {
3576 int error, lock_flags;
3578 AUDIT_ARG_FD(uap->fd);
3579 if ((error = getvnode(td->td_proc->p_fd, uap->fd, CAP_FSYNC,
3583 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
3584 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
3586 if (MNT_SHARED_WRITES(mp) ||
3587 ((mp == NULL) && MNT_SHARED_WRITES(vp->v_mount))) {
3588 lock_flags = LK_SHARED;
3590 lock_flags = LK_EXCLUSIVE;
3592 vn_lock(vp, lock_flags | LK_RETRY);
3593 AUDIT_ARG_VNODE1(vp);
3594 if (vp->v_object != NULL) {
3595 VM_OBJECT_LOCK(vp->v_object);
3596 vm_object_page_clean(vp->v_object, 0, 0, 0);
3597 VM_OBJECT_UNLOCK(vp->v_object);
3599 error = VOP_FSYNC(vp, MNT_WAIT, td);
3602 vn_finished_write(mp);
3604 VFS_UNLOCK_GIANT(vfslocked);
3610 * Rename files. Source and destination must either both be directories, or
3611 * both not be directories. If target is a directory, it must be empty.
3613 #ifndef _SYS_SYSPROTO_H_
3614 struct rename_args {
3622 register struct rename_args /* {
3628 return (kern_rename(td, uap->from, uap->to, UIO_USERSPACE));
3631 #ifndef _SYS_SYSPROTO_H_
3632 struct renameat_args {
3640 renameat(struct thread *td, struct renameat_args *uap)
3643 return (kern_renameat(td, uap->oldfd, uap->old, uap->newfd, uap->new,
3648 kern_rename(struct thread *td, char *from, char *to, enum uio_seg pathseg)
3651 return (kern_renameat(td, AT_FDCWD, from, AT_FDCWD, to, pathseg));
3655 kern_renameat(struct thread *td, int oldfd, char *old, int newfd, char *new,
3656 enum uio_seg pathseg)
3658 struct mount *mp = NULL;
3659 struct vnode *tvp, *fvp, *tdvp;
3660 struct nameidata fromnd, tond;
3667 NDINIT_ATRIGHTS(&fromnd, DELETE, LOCKPARENT | LOCKLEAF | SAVESTART |
3668 MPSAFE | AUDITVNODE1, pathseg, old, oldfd, CAP_DELETE, td);
3670 NDINIT_ATRIGHTS(&fromnd, DELETE, WANTPARENT | SAVESTART | MPSAFE |
3671 AUDITVNODE1, pathseg, old, oldfd, CAP_DELETE, td);
3674 if ((error = namei(&fromnd)) != 0)
3676 fvfslocked = NDHASGIANT(&fromnd);
3679 error = mac_vnode_check_rename_from(td->td_ucred, fromnd.ni_dvp,
3680 fromnd.ni_vp, &fromnd.ni_cnd);
3681 VOP_UNLOCK(fromnd.ni_dvp, 0);
3682 if (fromnd.ni_dvp != fromnd.ni_vp)
3683 VOP_UNLOCK(fromnd.ni_vp, 0);
3687 error = vn_start_write(fvp, &mp, V_WAIT | PCATCH);
3689 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3690 vrele(fromnd.ni_dvp);
3694 NDINIT_ATRIGHTS(&tond, RENAME, LOCKPARENT | LOCKLEAF | NOCACHE |
3695 SAVESTART | MPSAFE | AUDITVNODE2, pathseg, new, newfd, CAP_CREATE,
3697 if (fromnd.ni_vp->v_type == VDIR)
3698 tond.ni_cnd.cn_flags |= WILLBEDIR;
3699 if ((error = namei(&tond)) != 0) {
3700 /* Translate error code for rename("dir1", "dir2/."). */
3701 if (error == EISDIR && fvp->v_type == VDIR)
3703 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3704 vrele(fromnd.ni_dvp);
3706 vn_finished_write(mp);
3709 tvfslocked = NDHASGIANT(&tond);
3713 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
3716 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
3726 * If the source is the same as the destination (that is, if they
3727 * are links to the same vnode), then there is nothing to do.
3733 error = mac_vnode_check_rename_to(td->td_ucred, tdvp,
3734 tond.ni_vp, fromnd.ni_dvp == tdvp, &tond.ni_cnd);
3738 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
3739 tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
3740 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3741 NDFREE(&tond, NDF_ONLY_PNBUF);
3743 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3744 NDFREE(&tond, NDF_ONLY_PNBUF);
3751 vrele(fromnd.ni_dvp);
3754 vrele(tond.ni_startdir);
3755 vn_finished_write(mp);
3757 if (fromnd.ni_startdir)
3758 vrele(fromnd.ni_startdir);
3759 VFS_UNLOCK_GIANT(fvfslocked);
3760 VFS_UNLOCK_GIANT(tvfslocked);
3767 * Make a directory file.
3769 #ifndef _SYS_SYSPROTO_H_
3778 register struct mkdir_args /* {
3784 return (kern_mkdir(td, uap->path, UIO_USERSPACE, uap->mode));
3787 #ifndef _SYS_SYSPROTO_H_
3788 struct mkdirat_args {
3795 mkdirat(struct thread *td, struct mkdirat_args *uap)
3798 return (kern_mkdirat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode));
3802 kern_mkdir(struct thread *td, char *path, enum uio_seg segflg, int mode)
3805 return (kern_mkdirat(td, AT_FDCWD, path, segflg, mode));
3809 kern_mkdirat(struct thread *td, int fd, char *path, enum uio_seg segflg,
3816 struct nameidata nd;
3819 AUDIT_ARG_MODE(mode);
3822 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | MPSAFE |
3823 AUDITVNODE1, segflg, path, fd, CAP_MKDIR, td);
3824 nd.ni_cnd.cn_flags |= WILLBEDIR;
3825 if ((error = namei(&nd)) != 0)
3827 vfslocked = NDHASGIANT(&nd);
3830 NDFREE(&nd, NDF_ONLY_PNBUF);
3832 * XXX namei called with LOCKPARENT but not LOCKLEAF has
3833 * the strange behaviour of leaving the vnode unlocked
3834 * if the target is the same vnode as the parent.
3836 if (vp == nd.ni_dvp)
3841 VFS_UNLOCK_GIANT(vfslocked);
3844 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3845 NDFREE(&nd, NDF_ONLY_PNBUF);
3847 VFS_UNLOCK_GIANT(vfslocked);
3848 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3853 vattr.va_type = VDIR;
3854 vattr.va_mode = (mode & ACCESSPERMS) &~ td->td_proc->p_fd->fd_cmask;
3856 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
3861 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
3865 NDFREE(&nd, NDF_ONLY_PNBUF);
3869 vn_finished_write(mp);
3870 VFS_UNLOCK_GIANT(vfslocked);
3875 * Remove a directory file.
3877 #ifndef _SYS_SYSPROTO_H_
3885 struct rmdir_args /* {
3890 return (kern_rmdir(td, uap->path, UIO_USERSPACE));
3894 kern_rmdir(struct thread *td, char *path, enum uio_seg pathseg)
3897 return (kern_rmdirat(td, AT_FDCWD, path, pathseg));
3901 kern_rmdirat(struct thread *td, int fd, char *path, enum uio_seg pathseg)
3906 struct nameidata nd;
3911 NDINIT_ATRIGHTS(&nd, DELETE, LOCKPARENT | LOCKLEAF | MPSAFE |
3912 AUDITVNODE1, pathseg, path, fd, CAP_RMDIR, td);
3913 if ((error = namei(&nd)) != 0)
3915 vfslocked = NDHASGIANT(&nd);
3917 if (vp->v_type != VDIR) {
3922 * No rmdir "." please.
3924 if (nd.ni_dvp == vp) {
3929 * The root of a mounted filesystem cannot be deleted.
3931 if (vp->v_vflag & VV_ROOT) {
3936 error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp,
3941 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3942 NDFREE(&nd, NDF_ONLY_PNBUF);
3944 if (nd.ni_dvp == vp)
3948 VFS_UNLOCK_GIANT(vfslocked);
3949 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3953 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
3954 vn_finished_write(mp);
3956 NDFREE(&nd, NDF_ONLY_PNBUF);
3958 if (nd.ni_dvp == vp)
3962 VFS_UNLOCK_GIANT(vfslocked);
3968 * Read a block of directory entries in a filesystem independent format.
3970 #ifndef _SYS_SYSPROTO_H_
3971 struct ogetdirentries_args {
3979 ogetdirentries(struct thread *td, struct ogetdirentries_args *uap)
3984 error = kern_ogetdirentries(td, uap, &loff);
3986 error = copyout(&loff, uap->basep, sizeof(long));
3991 kern_ogetdirentries(struct thread *td, struct ogetdirentries_args *uap,
3996 struct uio auio, kuio;
3997 struct iovec aiov, kiov;
3998 struct dirent *dp, *edp;
4000 int error, eofflag, readcnt, vfslocked;
4003 /* XXX arbitrary sanity limit on `count'. */
4004 if (uap->count > 64 * 1024)
4006 if ((error = getvnode(td->td_proc->p_fd, uap->fd, CAP_READ,
4009 if ((fp->f_flag & FREAD) == 0) {
4015 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
4016 if (vp->v_type != VDIR) {
4017 VFS_UNLOCK_GIANT(vfslocked);
4021 aiov.iov_base = uap->buf;
4022 aiov.iov_len = uap->count;
4023 auio.uio_iov = &aiov;
4024 auio.uio_iovcnt = 1;
4025 auio.uio_rw = UIO_READ;
4026 auio.uio_segflg = UIO_USERSPACE;
4028 auio.uio_resid = uap->count;
4029 vn_lock(vp, LK_SHARED | LK_RETRY);
4030 loff = auio.uio_offset = fp->f_offset;
4032 error = mac_vnode_check_readdir(td->td_ucred, vp);
4035 VFS_UNLOCK_GIANT(vfslocked);
4040 # if (BYTE_ORDER != LITTLE_ENDIAN)
4041 if (vp->v_mount->mnt_maxsymlinklen <= 0) {
4042 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag,
4044 fp->f_offset = auio.uio_offset;
4049 kuio.uio_iov = &kiov;
4050 kuio.uio_segflg = UIO_SYSSPACE;
4051 kiov.iov_len = uap->count;
4052 dirbuf = malloc(uap->count, M_TEMP, M_WAITOK);
4053 kiov.iov_base = dirbuf;
4054 error = VOP_READDIR(vp, &kuio, fp->f_cred, &eofflag,
4056 fp->f_offset = kuio.uio_offset;
4058 readcnt = uap->count - kuio.uio_resid;
4059 edp = (struct dirent *)&dirbuf[readcnt];
4060 for (dp = (struct dirent *)dirbuf; dp < edp; ) {
4061 # if (BYTE_ORDER == LITTLE_ENDIAN)
4063 * The expected low byte of
4064 * dp->d_namlen is our dp->d_type.
4065 * The high MBZ byte of dp->d_namlen
4066 * is our dp->d_namlen.
4068 dp->d_type = dp->d_namlen;
4072 * The dp->d_type is the high byte
4073 * of the expected dp->d_namlen,
4074 * so must be zero'ed.
4078 if (dp->d_reclen > 0) {
4079 dp = (struct dirent *)
4080 ((char *)dp + dp->d_reclen);
4087 error = uiomove(dirbuf, readcnt, &auio);
4089 free(dirbuf, M_TEMP);
4093 VFS_UNLOCK_GIANT(vfslocked);
4097 if (uap->count == auio.uio_resid &&
4098 (vp->v_vflag & VV_ROOT) &&
4099 (vp->v_mount->mnt_flag & MNT_UNION)) {
4100 struct vnode *tvp = vp;
4101 vp = vp->v_mount->mnt_vnodecovered;
4107 VFS_UNLOCK_GIANT(vfslocked);
4111 VFS_UNLOCK_GIANT(vfslocked);
4113 td->td_retval[0] = uap->count - auio.uio_resid;
4118 #endif /* COMPAT_43 */
4121 * Read a block of directory entries in a filesystem independent format.
4123 #ifndef _SYS_SYSPROTO_H_
4124 struct getdirentries_args {
4132 getdirentries(td, uap)
4134 register struct getdirentries_args /* {
4144 error = kern_getdirentries(td, uap->fd, uap->buf, uap->count, &base);
4147 if (uap->basep != NULL)
4148 error = copyout(&base, uap->basep, sizeof(long));
4153 kern_getdirentries(struct thread *td, int fd, char *buf, u_int count,
4165 if (count > INT_MAX)
4167 if ((error = getvnode(td->td_proc->p_fd, fd, CAP_READ | CAP_SEEK,
4170 if ((fp->f_flag & FREAD) == 0) {
4176 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
4177 if (vp->v_type != VDIR) {
4178 VFS_UNLOCK_GIANT(vfslocked);
4182 aiov.iov_base = buf;
4183 aiov.iov_len = count;
4184 auio.uio_iov = &aiov;
4185 auio.uio_iovcnt = 1;
4186 auio.uio_rw = UIO_READ;
4187 auio.uio_segflg = UIO_USERSPACE;
4189 auio.uio_resid = count;
4190 vn_lock(vp, LK_SHARED | LK_RETRY);
4191 AUDIT_ARG_VNODE1(vp);
4192 loff = auio.uio_offset = fp->f_offset;
4194 error = mac_vnode_check_readdir(td->td_ucred, vp);
4197 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL,
4199 fp->f_offset = auio.uio_offset;
4202 VFS_UNLOCK_GIANT(vfslocked);
4205 if (count == auio.uio_resid &&
4206 (vp->v_vflag & VV_ROOT) &&
4207 (vp->v_mount->mnt_flag & MNT_UNION)) {
4208 struct vnode *tvp = vp;
4209 vp = vp->v_mount->mnt_vnodecovered;
4215 VFS_UNLOCK_GIANT(vfslocked);
4219 VFS_UNLOCK_GIANT(vfslocked);
4221 td->td_retval[0] = count - auio.uio_resid;
4227 #ifndef _SYS_SYSPROTO_H_
4228 struct getdents_args {
4237 register struct getdents_args /* {
4243 struct getdirentries_args ap;
4246 ap.count = uap->count;
4248 return (getdirentries(td, &ap));
4252 * Set the mode mask for creation of filesystem nodes.
4254 #ifndef _SYS_SYSPROTO_H_
4262 struct umask_args /* {
4266 register struct filedesc *fdp;
4268 FILEDESC_XLOCK(td->td_proc->p_fd);
4269 fdp = td->td_proc->p_fd;
4270 td->td_retval[0] = fdp->fd_cmask;
4271 fdp->fd_cmask = uap->newmask & ALLPERMS;
4272 FILEDESC_XUNLOCK(td->td_proc->p_fd);
4277 * Void all references to file by ripping underlying filesystem away from
4280 #ifndef _SYS_SYSPROTO_H_
4281 struct revoke_args {
4288 register struct revoke_args /* {
4295 struct nameidata nd;
4298 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | MPSAFE | AUDITVNODE1,
4299 UIO_USERSPACE, uap->path, td);
4300 if ((error = namei(&nd)) != 0)
4302 vfslocked = NDHASGIANT(&nd);
4304 NDFREE(&nd, NDF_ONLY_PNBUF);
4305 if (vp->v_type != VCHR || vp->v_rdev == NULL) {
4310 error = mac_vnode_check_revoke(td->td_ucred, vp);
4314 error = VOP_GETATTR(vp, &vattr, td->td_ucred);
4317 if (td->td_ucred->cr_uid != vattr.va_uid) {
4318 error = priv_check(td, PRIV_VFS_ADMIN);
4323 VOP_REVOKE(vp, REVOKEALL);
4326 VFS_UNLOCK_GIANT(vfslocked);
4331 * Convert a user file descriptor to a kernel file entry and check that, if it
4332 * is a capability, the correct rights are present. A reference on the file
4333 * entry is held upon returning.
4336 getvnode(struct filedesc *fdp, int fd, cap_rights_t rights,
4341 struct file *fp_fromcap;
4347 if ((fdp == NULL) || (fp = fget_unlocked(fdp, fd)) == NULL)
4351 * If the file descriptor is for a capability, test rights and use the
4352 * file descriptor referenced by the capability.
4354 error = cap_funwrap(fp, rights, &fp_fromcap);
4356 fdrop(fp, curthread);
4359 if (fp != fp_fromcap) {
4361 fdrop(fp, curthread);
4364 #endif /* CAPABILITIES */
4365 if (fp->f_vnode == NULL) {
4366 fdrop(fp, curthread);
4375 * Get an (NFS) file handle.
4377 #ifndef _SYS_SYSPROTO_H_
4378 struct lgetfh_args {
4386 register struct lgetfh_args *uap;
4388 struct nameidata nd;
4390 register struct vnode *vp;
4394 error = priv_check(td, PRIV_VFS_GETFH);
4397 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | MPSAFE | AUDITVNODE1,
4398 UIO_USERSPACE, uap->fname, td);
4402 vfslocked = NDHASGIANT(&nd);
4403 NDFREE(&nd, NDF_ONLY_PNBUF);
4405 bzero(&fh, sizeof(fh));
4406 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
4407 error = VOP_VPTOFH(vp, &fh.fh_fid);
4409 VFS_UNLOCK_GIANT(vfslocked);
4412 error = copyout(&fh, uap->fhp, sizeof (fh));
4416 #ifndef _SYS_SYSPROTO_H_
4425 register struct getfh_args *uap;
4427 struct nameidata nd;
4429 register struct vnode *vp;
4433 error = priv_check(td, PRIV_VFS_GETFH);
4436 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | MPSAFE | AUDITVNODE1,
4437 UIO_USERSPACE, uap->fname, td);
4441 vfslocked = NDHASGIANT(&nd);
4442 NDFREE(&nd, NDF_ONLY_PNBUF);
4444 bzero(&fh, sizeof(fh));
4445 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
4446 error = VOP_VPTOFH(vp, &fh.fh_fid);
4448 VFS_UNLOCK_GIANT(vfslocked);
4451 error = copyout(&fh, uap->fhp, sizeof (fh));
4456 * syscall for the rpc.lockd to use to translate a NFS file handle into an
4459 * warning: do not remove the priv_check() call or this becomes one giant
4462 #ifndef _SYS_SYSPROTO_H_
4463 struct fhopen_args {
4464 const struct fhandle *u_fhp;
4471 struct fhopen_args /* {
4472 const struct fhandle *u_fhp;
4476 struct proc *p = td->td_proc;
4481 struct vattr *vap = &vat;
4484 register struct filedesc *fdp = p->p_fd;
4485 int fmode, error, type;
4491 error = priv_check(td, PRIV_VFS_FHOPEN);
4494 fmode = FFLAGS(uap->flags);
4495 /* why not allow a non-read/write open for our lockd? */
4496 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
4498 error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
4501 /* find the mount point */
4502 mp = vfs_busyfs(&fhp.fh_fsid);
4505 vfslocked = VFS_LOCK_GIANT(mp);
4506 /* now give me my vnode, it gets returned to me locked */
4507 error = VFS_FHTOVP(mp, &fhp.fh_fid, LK_EXCLUSIVE, &vp);
4512 * from now on we have to make sure not
4513 * to forget about the vnode
4514 * any error that causes an abort must vput(vp)
4515 * just set error = err and 'goto bad;'.
4521 if (vp->v_type == VLNK) {
4525 if (vp->v_type == VSOCK) {
4529 if (vp->v_type != VDIR && fmode & O_DIRECTORY) {
4534 if (fmode & (FWRITE | O_TRUNC)) {
4535 if (vp->v_type == VDIR) {
4539 error = vn_writechk(vp);
4546 if ((fmode & O_APPEND) && (fmode & FWRITE))
4549 error = mac_vnode_check_open(td->td_ucred, vp, accmode);
4554 error = VOP_ACCESS(vp, accmode, td->td_ucred, td);
4558 if (fmode & O_TRUNC) {
4560 VOP_UNLOCK(vp, 0); /* XXX */
4561 if ((error = vn_start_write(NULL, &mp, V_WAIT | PCATCH)) != 0) {
4566 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); /* XXX */
4570 * We don't yet have fp->f_cred, so use td->td_ucred, which
4573 error = mac_vnode_check_write(td->td_ucred, td->td_ucred, vp);
4578 error = VOP_SETATTR(vp, vap, td->td_ucred);
4582 vn_finished_write(mp);
4586 error = VOP_OPEN(vp, fmode, td->td_ucred, td, NULL);
4594 * end of vn_open code
4597 if ((error = falloc(td, &nfp, &indx, fmode)) != 0) {
4602 /* An extra reference on `nfp' has been held for us by falloc(). */
4605 finit(nfp, fmode & FMASK, DTYPE_VNODE, vp, &vnops);
4606 if (fmode & (O_EXLOCK | O_SHLOCK)) {
4607 lf.l_whence = SEEK_SET;
4610 if (fmode & O_EXLOCK)
4611 lf.l_type = F_WRLCK;
4613 lf.l_type = F_RDLCK;
4615 if ((fmode & FNONBLOCK) == 0)
4618 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf,
4621 * The lock request failed. Normally close the
4622 * descriptor but handle the case where someone might
4623 * have dup()d or close()d it when we weren't looking.
4625 fdclose(fdp, fp, indx, td);
4628 * release our private reference
4633 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
4634 atomic_set_int(&fp->f_flag, FHASLOCK);
4639 VFS_UNLOCK_GIANT(vfslocked);
4640 td->td_retval[0] = indx;
4646 VFS_UNLOCK_GIANT(vfslocked);
4651 * Stat an (NFS) file handle.
4653 #ifndef _SYS_SYSPROTO_H_
4654 struct fhstat_args {
4655 struct fhandle *u_fhp;
4662 register struct fhstat_args /* {
4663 struct fhandle *u_fhp;
4674 error = priv_check(td, PRIV_VFS_FHSTAT);
4677 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4680 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4682 vfslocked = VFS_LOCK_GIANT(mp);
4683 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_EXCLUSIVE, &vp);
4686 VFS_UNLOCK_GIANT(vfslocked);
4689 error = vn_stat(vp, &sb, td->td_ucred, NOCRED, td);
4691 VFS_UNLOCK_GIANT(vfslocked);
4694 error = copyout(&sb, uap->sb, sizeof(sb));
4699 * Implement fstatfs() for (NFS) file handles.
4701 #ifndef _SYS_SYSPROTO_H_
4702 struct fhstatfs_args {
4703 struct fhandle *u_fhp;
4710 struct fhstatfs_args /* {
4711 struct fhandle *u_fhp;
4719 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4722 error = kern_fhstatfs(td, fh, &sf);
4725 return (copyout(&sf, uap->buf, sizeof(sf)));
4729 kern_fhstatfs(struct thread *td, fhandle_t fh, struct statfs *buf)
4737 error = priv_check(td, PRIV_VFS_FHSTATFS);
4740 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4742 vfslocked = VFS_LOCK_GIANT(mp);
4743 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_EXCLUSIVE, &vp);
4746 VFS_UNLOCK_GIANT(vfslocked);
4750 error = prison_canseemount(td->td_ucred, mp);
4754 error = mac_mount_check_stat(td->td_ucred, mp);
4759 * Set these in case the underlying filesystem fails to do so.
4762 sp->f_version = STATFS_VERSION;
4763 sp->f_namemax = NAME_MAX;
4764 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
4765 error = VFS_STATFS(mp, sp);
4770 VFS_UNLOCK_GIANT(vfslocked);
4775 kern_posix_fallocate(struct thread *td, int fd, off_t offset, off_t len)
4780 off_t olen, ooffset;
4781 int error, vfslocked;
4785 error = fget(td, fd, CAP_WRITE, &fp);
4789 switch (fp->f_type) {
4800 if ((fp->f_flag & FWRITE) == 0) {
4805 if (vp->v_type != VREG) {
4809 if (offset < 0 || len <= 0) {
4813 /* Check for wrap. */
4814 if (offset > OFF_MAX - len) {
4819 /* Allocating blocks may take a long time, so iterate. */
4825 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
4827 error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
4829 VFS_UNLOCK_GIANT(vfslocked);
4832 error = vn_lock(vp, LK_EXCLUSIVE);
4834 vn_finished_write(mp);
4835 VFS_UNLOCK_GIANT(vfslocked);
4839 error = mac_vnode_check_write(td->td_ucred, fp->f_cred, vp);
4842 error = VOP_ALLOCATE(vp, &offset, &len);
4844 vn_finished_write(mp);
4845 VFS_UNLOCK_GIANT(vfslocked);
4847 if (olen + ooffset != offset + len) {
4848 panic("offset + len changed from %jx/%jx to %jx/%jx",
4849 ooffset, olen, offset, len);
4851 if (error != 0 || len == 0)
4853 KASSERT(olen > len, ("Iteration did not make progress?"));
4863 posix_fallocate(struct thread *td, struct posix_fallocate_args *uap)
4866 return (kern_posix_fallocate(td, uap->fd, uap->offset, uap->len));