2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
4 * (c) UNIX System Laboratories, Inc.
5 * All or some portions of this file are derived from material licensed
6 * to the University of California by American Telephone and Telegraph
7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 * the permission of UNIX System Laboratories, Inc.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 4. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * @(#)vfs_syscalls.c 8.13 (Berkeley) 4/15/94
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
40 #include "opt_compat.h"
41 #include "opt_kdtrace.h"
42 #include "opt_ktrace.h"
44 #include <sys/param.h>
45 #include <sys/systm.h>
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 static MALLOC_DEFINE(M_FADVISE, "fadvise", "posix_fadvise(2) information");
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, &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, &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);
997 * Check permissions, allocate an open file structure, and call the device
998 * open routine if any.
1000 #ifndef _SYS_SYSPROTO_H_
1010 register struct open_args /* {
1017 return (kern_open(td, uap->path, UIO_USERSPACE, uap->flags, uap->mode));
1020 #ifndef _SYS_SYSPROTO_H_
1021 struct openat_args {
1029 openat(struct thread *td, struct openat_args *uap)
1032 return (kern_openat(td, uap->fd, uap->path, UIO_USERSPACE, uap->flag,
1037 kern_open(struct thread *td, char *path, enum uio_seg pathseg, int flags,
1041 return (kern_openat(td, AT_FDCWD, path, pathseg, flags, mode));
1045 kern_openat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1046 int flags, int mode)
1048 struct proc *p = td->td_proc;
1049 struct filedesc *fdp = p->p_fd;
1054 int type, indx, error;
1056 struct nameidata nd;
1059 AUDIT_ARG_FFLAGS(flags);
1060 AUDIT_ARG_MODE(mode);
1061 /* XXX: audit dirfd */
1063 * Only one of the O_EXEC, O_RDONLY, O_WRONLY and O_RDWR may
1066 if (flags & O_EXEC) {
1067 if (flags & O_ACCMODE)
1069 } else if ((flags & O_ACCMODE) == O_ACCMODE)
1072 flags = FFLAGS(flags);
1074 error = fallocf(td, &nfp, &indx, flags);
1077 /* An extra reference on `nfp' has been held for us by falloc(). */
1079 /* Set the flags early so the finit in devfs can pick them up. */
1080 fp->f_flag = flags & FMASK;
1081 cmode = ((mode &~ fdp->fd_cmask) & ALLPERMS) &~ S_ISTXT;
1082 NDINIT_AT(&nd, LOOKUP, FOLLOW | AUDITVNODE1 | MPSAFE, pathseg, path, fd,
1084 td->td_dupfd = -1; /* XXX check for fdopen */
1085 error = vn_open(&nd, &flags, cmode, fp);
1088 * If the vn_open replaced the method vector, something
1089 * wonderous happened deep below and we just pass it up
1090 * pretending we know what we do.
1092 if (error == ENXIO && fp->f_ops != &badfileops) {
1094 td->td_retval[0] = indx;
1099 * handle special fdopen() case. bleh. dupfdopen() is
1100 * responsible for dropping the old contents of ofiles[indx]
1103 if ((error == ENODEV || error == ENXIO) &&
1104 td->td_dupfd >= 0 && /* XXX from fdopen */
1106 dupfdopen(td, fdp, indx, td->td_dupfd, flags, error)) == 0) {
1107 td->td_retval[0] = indx;
1112 * Clean up the descriptor, but only if another thread hadn't
1113 * replaced or closed it.
1115 fdclose(fdp, fp, indx, td);
1118 if (error == ERESTART)
1123 vfslocked = NDHASGIANT(&nd);
1124 NDFREE(&nd, NDF_ONLY_PNBUF);
1128 * Store the vnode, for any f_type. Typically, the vnode use
1129 * count is decremented by direct call to vn_closefile() for
1130 * files that switched type in the cdevsw fdopen() method.
1134 * If the file wasn't claimed by devfs bind it to the normal
1135 * vnode operations here.
1137 if (fp->f_ops == &badfileops) {
1138 KASSERT(vp->v_type != VFIFO, ("Unexpected fifo."));
1140 finit(fp, flags & FMASK, DTYPE_VNODE, vp, &vnops);
1144 if (fp->f_type == DTYPE_VNODE && (flags & (O_EXLOCK | O_SHLOCK)) != 0) {
1145 lf.l_whence = SEEK_SET;
1148 if (flags & O_EXLOCK)
1149 lf.l_type = F_WRLCK;
1151 lf.l_type = F_RDLCK;
1153 if ((flags & FNONBLOCK) == 0)
1155 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf,
1158 atomic_set_int(&fp->f_flag, FHASLOCK);
1160 if (flags & O_TRUNC) {
1161 error = fo_truncate(fp, 0, td->td_ucred, td);
1165 VFS_UNLOCK_GIANT(vfslocked);
1167 * Release our private reference, leaving the one associated with
1168 * the descriptor table intact.
1171 td->td_retval[0] = indx;
1174 VFS_UNLOCK_GIANT(vfslocked);
1175 fdclose(fdp, fp, indx, td);
1184 #ifndef _SYS_SYSPROTO_H_
1185 struct ocreat_args {
1193 register struct ocreat_args /* {
1199 return (kern_open(td, uap->path, UIO_USERSPACE,
1200 O_WRONLY | O_CREAT | O_TRUNC, uap->mode));
1202 #endif /* COMPAT_43 */
1205 * Create a special file.
1207 #ifndef _SYS_SYSPROTO_H_
1217 register struct mknod_args /* {
1224 return (kern_mknod(td, uap->path, UIO_USERSPACE, uap->mode, uap->dev));
1227 #ifndef _SYS_SYSPROTO_H_
1228 struct mknodat_args {
1236 mknodat(struct thread *td, struct mknodat_args *uap)
1239 return (kern_mknodat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode,
1244 kern_mknod(struct thread *td, char *path, enum uio_seg pathseg, int mode,
1248 return (kern_mknodat(td, AT_FDCWD, path, pathseg, mode, dev));
1252 kern_mknodat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1260 struct nameidata nd;
1263 AUDIT_ARG_MODE(mode);
1265 switch (mode & S_IFMT) {
1268 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
1271 error = priv_check(td, PRIV_VFS_MKNOD_BAD);
1274 error = priv_check(td, PRIV_VFS_MKNOD_WHT);
1278 return (kern_mkfifoat(td, fd, path, pathseg, mode));
1288 NDINIT_AT(&nd, CREATE, LOCKPARENT | SAVENAME | MPSAFE | AUDITVNODE1,
1289 pathseg, path, fd, td);
1290 if ((error = namei(&nd)) != 0)
1292 vfslocked = NDHASGIANT(&nd);
1295 NDFREE(&nd, NDF_ONLY_PNBUF);
1296 if (vp == nd.ni_dvp)
1301 VFS_UNLOCK_GIANT(vfslocked);
1305 vattr.va_mode = (mode & ALLPERMS) &
1306 ~td->td_proc->p_fd->fd_cmask;
1307 vattr.va_rdev = dev;
1310 switch (mode & S_IFMT) {
1311 case S_IFMT: /* used by badsect to flag bad sectors */
1312 vattr.va_type = VBAD;
1315 vattr.va_type = VCHR;
1318 vattr.va_type = VBLK;
1324 panic("kern_mknod: invalid mode");
1327 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1328 NDFREE(&nd, NDF_ONLY_PNBUF);
1330 VFS_UNLOCK_GIANT(vfslocked);
1331 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1336 if (error == 0 && !whiteout)
1337 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp,
1338 &nd.ni_cnd, &vattr);
1342 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, CREATE);
1344 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp,
1345 &nd.ni_cnd, &vattr);
1350 NDFREE(&nd, NDF_ONLY_PNBUF);
1352 vn_finished_write(mp);
1353 VFS_UNLOCK_GIANT(vfslocked);
1358 * Create a named pipe.
1360 #ifndef _SYS_SYSPROTO_H_
1361 struct mkfifo_args {
1369 register struct mkfifo_args /* {
1375 return (kern_mkfifo(td, uap->path, UIO_USERSPACE, uap->mode));
1378 #ifndef _SYS_SYSPROTO_H_
1379 struct mkfifoat_args {
1386 mkfifoat(struct thread *td, struct mkfifoat_args *uap)
1389 return (kern_mkfifoat(td, uap->fd, uap->path, UIO_USERSPACE,
1394 kern_mkfifo(struct thread *td, char *path, enum uio_seg pathseg, int mode)
1397 return (kern_mkfifoat(td, AT_FDCWD, path, pathseg, mode));
1401 kern_mkfifoat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1407 struct nameidata nd;
1410 AUDIT_ARG_MODE(mode);
1413 NDINIT_AT(&nd, CREATE, LOCKPARENT | SAVENAME | MPSAFE | AUDITVNODE1,
1414 pathseg, path, fd, td);
1415 if ((error = namei(&nd)) != 0)
1417 vfslocked = NDHASGIANT(&nd);
1418 if (nd.ni_vp != NULL) {
1419 NDFREE(&nd, NDF_ONLY_PNBUF);
1420 if (nd.ni_vp == nd.ni_dvp)
1425 VFS_UNLOCK_GIANT(vfslocked);
1428 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1429 NDFREE(&nd, NDF_ONLY_PNBUF);
1431 VFS_UNLOCK_GIANT(vfslocked);
1432 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1437 vattr.va_type = VFIFO;
1438 vattr.va_mode = (mode & ALLPERMS) & ~td->td_proc->p_fd->fd_cmask;
1440 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1445 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
1452 vn_finished_write(mp);
1453 VFS_UNLOCK_GIANT(vfslocked);
1454 NDFREE(&nd, NDF_ONLY_PNBUF);
1459 * Make a hard file link.
1461 #ifndef _SYS_SYSPROTO_H_
1470 register struct link_args /* {
1476 return (kern_link(td, uap->path, uap->link, UIO_USERSPACE));
1479 #ifndef _SYS_SYSPROTO_H_
1480 struct linkat_args {
1489 linkat(struct thread *td, struct linkat_args *uap)
1494 if (flag & ~AT_SYMLINK_FOLLOW)
1497 return (kern_linkat(td, uap->fd1, uap->fd2, uap->path1, uap->path2,
1498 UIO_USERSPACE, (flag & AT_SYMLINK_FOLLOW) ? FOLLOW : NOFOLLOW));
1501 int hardlink_check_uid = 0;
1502 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
1503 &hardlink_check_uid, 0,
1504 "Unprivileged processes cannot create hard links to files owned by other "
1506 static int hardlink_check_gid = 0;
1507 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
1508 &hardlink_check_gid, 0,
1509 "Unprivileged processes cannot create hard links to files owned by other "
1513 can_hardlink(struct vnode *vp, struct ucred *cred)
1518 if (!hardlink_check_uid && !hardlink_check_gid)
1521 error = VOP_GETATTR(vp, &va, cred);
1525 if (hardlink_check_uid && cred->cr_uid != va.va_uid) {
1526 error = priv_check_cred(cred, PRIV_VFS_LINK, 0);
1531 if (hardlink_check_gid && !groupmember(va.va_gid, cred)) {
1532 error = priv_check_cred(cred, PRIV_VFS_LINK, 0);
1541 kern_link(struct thread *td, char *path, char *link, enum uio_seg segflg)
1544 return (kern_linkat(td, AT_FDCWD, AT_FDCWD, path,link, segflg, FOLLOW));
1548 kern_linkat(struct thread *td, int fd1, int fd2, char *path1, char *path2,
1549 enum uio_seg segflg, int follow)
1553 struct nameidata nd;
1559 NDINIT_AT(&nd, LOOKUP, follow | MPSAFE | AUDITVNODE1, segflg, path1,
1562 if ((error = namei(&nd)) != 0)
1564 vfslocked = NDHASGIANT(&nd);
1565 NDFREE(&nd, NDF_ONLY_PNBUF);
1567 if (vp->v_type == VDIR) {
1569 VFS_UNLOCK_GIANT(vfslocked);
1570 return (EPERM); /* POSIX */
1572 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0) {
1574 VFS_UNLOCK_GIANT(vfslocked);
1577 NDINIT_AT(&nd, CREATE, LOCKPARENT | SAVENAME | MPSAFE | AUDITVNODE2,
1578 segflg, path2, fd2, td);
1579 if ((error = namei(&nd)) == 0) {
1580 lvfslocked = NDHASGIANT(&nd);
1581 if (nd.ni_vp != NULL) {
1582 if (nd.ni_dvp == nd.ni_vp)
1588 } else if ((error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY))
1590 error = can_hardlink(vp, td->td_ucred);
1593 error = mac_vnode_check_link(td->td_ucred,
1594 nd.ni_dvp, vp, &nd.ni_cnd);
1597 error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
1601 NDFREE(&nd, NDF_ONLY_PNBUF);
1602 VFS_UNLOCK_GIANT(lvfslocked);
1605 vn_finished_write(mp);
1606 VFS_UNLOCK_GIANT(vfslocked);
1611 * Make a symbolic link.
1613 #ifndef _SYS_SYSPROTO_H_
1614 struct symlink_args {
1622 register struct symlink_args /* {
1628 return (kern_symlink(td, uap->path, uap->link, UIO_USERSPACE));
1631 #ifndef _SYS_SYSPROTO_H_
1632 struct symlinkat_args {
1639 symlinkat(struct thread *td, struct symlinkat_args *uap)
1642 return (kern_symlinkat(td, uap->path1, uap->fd, uap->path2,
1647 kern_symlink(struct thread *td, char *path, char *link, enum uio_seg segflg)
1650 return (kern_symlinkat(td, path, AT_FDCWD, link, segflg));
1654 kern_symlinkat(struct thread *td, char *path1, int fd, char *path2,
1655 enum uio_seg segflg)
1661 struct nameidata nd;
1664 if (segflg == UIO_SYSSPACE) {
1667 syspath = uma_zalloc(namei_zone, M_WAITOK);
1668 if ((error = copyinstr(path1, syspath, MAXPATHLEN, NULL)) != 0)
1671 AUDIT_ARG_TEXT(syspath);
1674 NDINIT_AT(&nd, CREATE, LOCKPARENT | SAVENAME | MPSAFE | AUDITVNODE1,
1675 segflg, path2, fd, td);
1676 if ((error = namei(&nd)) != 0)
1678 vfslocked = NDHASGIANT(&nd);
1680 NDFREE(&nd, NDF_ONLY_PNBUF);
1681 if (nd.ni_vp == nd.ni_dvp)
1686 VFS_UNLOCK_GIANT(vfslocked);
1690 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1691 NDFREE(&nd, NDF_ONLY_PNBUF);
1693 VFS_UNLOCK_GIANT(vfslocked);
1694 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1699 vattr.va_mode = ACCESSPERMS &~ td->td_proc->p_fd->fd_cmask;
1701 vattr.va_type = VLNK;
1702 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1707 error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr, syspath);
1713 NDFREE(&nd, NDF_ONLY_PNBUF);
1715 vn_finished_write(mp);
1716 VFS_UNLOCK_GIANT(vfslocked);
1718 if (segflg != UIO_SYSSPACE)
1719 uma_zfree(namei_zone, syspath);
1724 * Delete a whiteout from the filesystem.
1729 register struct undelete_args /* {
1735 struct nameidata nd;
1740 NDINIT(&nd, DELETE, LOCKPARENT | DOWHITEOUT | MPSAFE | AUDITVNODE1,
1741 UIO_USERSPACE, uap->path, td);
1745 vfslocked = NDHASGIANT(&nd);
1747 if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & ISWHITEOUT)) {
1748 NDFREE(&nd, NDF_ONLY_PNBUF);
1749 if (nd.ni_vp == nd.ni_dvp)
1755 VFS_UNLOCK_GIANT(vfslocked);
1758 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1759 NDFREE(&nd, NDF_ONLY_PNBUF);
1761 VFS_UNLOCK_GIANT(vfslocked);
1762 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1766 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, DELETE);
1767 NDFREE(&nd, NDF_ONLY_PNBUF);
1769 vn_finished_write(mp);
1770 VFS_UNLOCK_GIANT(vfslocked);
1775 * Delete a name from the filesystem.
1777 #ifndef _SYS_SYSPROTO_H_
1778 struct unlink_args {
1785 struct unlink_args /* {
1790 return (kern_unlink(td, uap->path, UIO_USERSPACE));
1793 #ifndef _SYS_SYSPROTO_H_
1794 struct unlinkat_args {
1801 unlinkat(struct thread *td, struct unlinkat_args *uap)
1803 int flag = uap->flag;
1805 char *path = uap->path;
1807 if (flag & ~AT_REMOVEDIR)
1810 if (flag & AT_REMOVEDIR)
1811 return (kern_rmdirat(td, fd, path, UIO_USERSPACE));
1813 return (kern_unlinkat(td, fd, path, UIO_USERSPACE));
1817 kern_unlink(struct thread *td, char *path, enum uio_seg pathseg)
1820 return (kern_unlinkat(td, AT_FDCWD, path, pathseg));
1824 kern_unlinkat(struct thread *td, int fd, char *path, enum uio_seg pathseg)
1829 struct nameidata nd;
1834 NDINIT_AT(&nd, DELETE, LOCKPARENT | LOCKLEAF | MPSAFE | AUDITVNODE1,
1835 pathseg, path, fd, td);
1836 if ((error = namei(&nd)) != 0)
1837 return (error == EINVAL ? EPERM : error);
1838 vfslocked = NDHASGIANT(&nd);
1840 if (vp->v_type == VDIR)
1841 error = EPERM; /* POSIX */
1844 * The root of a mounted filesystem cannot be deleted.
1846 * XXX: can this only be a VDIR case?
1848 if (vp->v_vflag & VV_ROOT)
1852 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1853 NDFREE(&nd, NDF_ONLY_PNBUF);
1855 if (vp == nd.ni_dvp)
1859 VFS_UNLOCK_GIANT(vfslocked);
1860 if ((error = vn_start_write(NULL, &mp,
1861 V_XSLEEP | PCATCH)) != 0)
1866 error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp,
1871 error = VOP_REMOVE(nd.ni_dvp, vp, &nd.ni_cnd);
1875 vn_finished_write(mp);
1877 NDFREE(&nd, NDF_ONLY_PNBUF);
1879 if (vp == nd.ni_dvp)
1883 VFS_UNLOCK_GIANT(vfslocked);
1888 * Reposition read/write file offset.
1890 #ifndef _SYS_SYSPROTO_H_
1901 register struct lseek_args /* {
1908 struct ucred *cred = td->td_ucred;
1916 AUDIT_ARG_FD(uap->fd);
1917 if ((error = fget(td, uap->fd, &fp)) != 0)
1919 if (!(fp->f_ops->fo_flags & DFLAG_SEEKABLE)) {
1924 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
1925 noneg = (vp->v_type != VCHR);
1926 offset = uap->offset;
1927 switch (uap->whence) {
1930 (fp->f_offset < 0 ||
1931 (offset > 0 && fp->f_offset > OFF_MAX - offset))) {
1935 offset += fp->f_offset;
1938 vn_lock(vp, LK_SHARED | LK_RETRY);
1939 error = VOP_GETATTR(vp, &vattr, cred);
1945 * If the file references a disk device, then fetch
1946 * the media size and use that to determine the ending
1949 if (vattr.va_size == 0 && vp->v_type == VCHR &&
1950 fo_ioctl(fp, DIOCGMEDIASIZE, &size, cred, td) == 0)
1951 vattr.va_size = size;
1953 (vattr.va_size > OFF_MAX ||
1954 (offset > 0 && vattr.va_size > OFF_MAX - offset))) {
1958 offset += vattr.va_size;
1963 error = fo_ioctl(fp, FIOSEEKDATA, &offset, cred, td);
1966 error = fo_ioctl(fp, FIOSEEKHOLE, &offset, cred, td);
1971 if (error == 0 && noneg && offset < 0)
1975 fp->f_offset = offset;
1976 VFS_KNOTE_UNLOCKED(vp, 0);
1977 *(off_t *)(td->td_retval) = fp->f_offset;
1980 VFS_UNLOCK_GIANT(vfslocked);
1984 #if defined(COMPAT_43)
1986 * Reposition read/write file offset.
1988 #ifndef _SYS_SYSPROTO_H_
1989 struct olseek_args {
1998 register struct olseek_args /* {
2004 struct lseek_args /* {
2012 nuap.offset = uap->offset;
2013 nuap.whence = uap->whence;
2014 return (lseek(td, &nuap));
2016 #endif /* COMPAT_43 */
2018 /* Version with the 'pad' argument */
2020 freebsd6_lseek(td, uap)
2022 register struct freebsd6_lseek_args *uap;
2024 struct lseek_args ouap;
2027 ouap.offset = uap->offset;
2028 ouap.whence = uap->whence;
2029 return (lseek(td, &ouap));
2033 * Check access permissions using passed credentials.
2036 vn_access(vp, user_flags, cred, td)
2045 /* Flags == 0 means only check for existence. */
2049 if (user_flags & R_OK)
2051 if (user_flags & W_OK)
2053 if (user_flags & X_OK)
2056 error = mac_vnode_check_access(cred, vp, accmode);
2060 if ((accmode & VWRITE) == 0 || (error = vn_writechk(vp)) == 0)
2061 error = VOP_ACCESS(vp, accmode, cred, td);
2067 * Check access permissions using "real" credentials.
2069 #ifndef _SYS_SYSPROTO_H_
2070 struct access_args {
2078 register struct access_args /* {
2084 return (kern_access(td, uap->path, UIO_USERSPACE, uap->flags));
2087 #ifndef _SYS_SYSPROTO_H_
2088 struct faccessat_args {
2096 faccessat(struct thread *td, struct faccessat_args *uap)
2099 if (uap->flag & ~AT_EACCESS)
2101 return (kern_accessat(td, uap->fd, uap->path, UIO_USERSPACE, uap->flag,
2106 kern_access(struct thread *td, char *path, enum uio_seg pathseg, int mode)
2109 return (kern_accessat(td, AT_FDCWD, path, pathseg, 0, mode));
2113 kern_accessat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2114 int flags, int mode)
2116 struct ucred *cred, *tmpcred;
2118 struct nameidata nd;
2123 * Create and modify a temporary credential instead of one that
2124 * is potentially shared.
2126 if (!(flags & AT_EACCESS)) {
2127 cred = td->td_ucred;
2128 tmpcred = crdup(cred);
2129 tmpcred->cr_uid = cred->cr_ruid;
2130 tmpcred->cr_groups[0] = cred->cr_rgid;
2131 td->td_ucred = tmpcred;
2133 cred = tmpcred = td->td_ucred;
2134 AUDIT_ARG_VALUE(mode);
2135 NDINIT_AT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | MPSAFE |
2136 AUDITVNODE1, pathseg, path, fd, td);
2137 if ((error = namei(&nd)) != 0)
2139 vfslocked = NDHASGIANT(&nd);
2142 error = vn_access(vp, mode, tmpcred, td);
2143 NDFREE(&nd, NDF_ONLY_PNBUF);
2145 VFS_UNLOCK_GIANT(vfslocked);
2147 if (!(flags & AT_EACCESS)) {
2148 td->td_ucred = cred;
2155 * Check access permissions using "effective" credentials.
2157 #ifndef _SYS_SYSPROTO_H_
2158 struct eaccess_args {
2166 register struct eaccess_args /* {
2172 return (kern_eaccess(td, uap->path, UIO_USERSPACE, uap->flags));
2176 kern_eaccess(struct thread *td, char *path, enum uio_seg pathseg, int flags)
2179 return (kern_accessat(td, AT_FDCWD, path, pathseg, AT_EACCESS, flags));
2182 #if defined(COMPAT_43)
2184 * Get file status; this version follows links.
2186 #ifndef _SYS_SYSPROTO_H_
2195 register struct ostat_args /* {
2204 error = kern_stat(td, uap->path, UIO_USERSPACE, &sb);
2208 error = copyout(&osb, uap->ub, sizeof (osb));
2213 * Get file status; this version does not follow links.
2215 #ifndef _SYS_SYSPROTO_H_
2216 struct olstat_args {
2224 register struct olstat_args /* {
2233 error = kern_lstat(td, uap->path, UIO_USERSPACE, &sb);
2237 error = copyout(&osb, uap->ub, sizeof (osb));
2242 * Convert from an old to a new stat structure.
2250 ost->st_dev = st->st_dev;
2251 ost->st_ino = st->st_ino;
2252 ost->st_mode = st->st_mode;
2253 ost->st_nlink = st->st_nlink;
2254 ost->st_uid = st->st_uid;
2255 ost->st_gid = st->st_gid;
2256 ost->st_rdev = st->st_rdev;
2257 if (st->st_size < (quad_t)1 << 32)
2258 ost->st_size = st->st_size;
2261 ost->st_atime = st->st_atime;
2262 ost->st_mtime = st->st_mtime;
2263 ost->st_ctime = st->st_ctime;
2264 ost->st_blksize = st->st_blksize;
2265 ost->st_blocks = st->st_blocks;
2266 ost->st_flags = st->st_flags;
2267 ost->st_gen = st->st_gen;
2269 #endif /* COMPAT_43 */
2272 * Get file status; this version follows links.
2274 #ifndef _SYS_SYSPROTO_H_
2283 register struct stat_args /* {
2291 error = kern_stat(td, uap->path, UIO_USERSPACE, &sb);
2293 error = copyout(&sb, uap->ub, sizeof (sb));
2297 #ifndef _SYS_SYSPROTO_H_
2298 struct fstatat_args {
2306 fstatat(struct thread *td, struct fstatat_args *uap)
2311 error = kern_statat(td, uap->flag, uap->fd, uap->path,
2312 UIO_USERSPACE, &sb);
2314 error = copyout(&sb, uap->buf, sizeof (sb));
2319 kern_stat(struct thread *td, char *path, enum uio_seg pathseg, struct stat *sbp)
2322 return (kern_statat(td, 0, AT_FDCWD, path, pathseg, sbp));
2326 kern_statat(struct thread *td, int flag, int fd, char *path,
2327 enum uio_seg pathseg, struct stat *sbp)
2330 return (kern_statat_vnhook(td, flag, fd, path, pathseg, sbp, NULL));
2334 kern_statat_vnhook(struct thread *td, int flag, int fd, char *path,
2335 enum uio_seg pathseg, struct stat *sbp,
2336 void (*hook)(struct vnode *vp, struct stat *sbp))
2338 struct nameidata nd;
2340 int error, vfslocked;
2342 if (flag & ~AT_SYMLINK_NOFOLLOW)
2345 NDINIT_AT(&nd, LOOKUP, ((flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW :
2346 FOLLOW) | LOCKSHARED | LOCKLEAF | AUDITVNODE1 | MPSAFE, pathseg,
2349 if ((error = namei(&nd)) != 0)
2351 vfslocked = NDHASGIANT(&nd);
2352 error = vn_stat(nd.ni_vp, &sb, td->td_ucred, NOCRED, td);
2354 SDT_PROBE(vfs, , stat, mode, path, sb.st_mode, 0, 0, 0);
2355 if (S_ISREG(sb.st_mode))
2356 SDT_PROBE(vfs, , stat, reg, path, pathseg, 0, 0, 0);
2357 if (__predict_false(hook != NULL))
2358 hook(nd.ni_vp, &sb);
2360 NDFREE(&nd, NDF_ONLY_PNBUF);
2362 VFS_UNLOCK_GIANT(vfslocked);
2367 if (KTRPOINT(td, KTR_STRUCT))
2374 * Get file status; this version does not follow links.
2376 #ifndef _SYS_SYSPROTO_H_
2385 register struct lstat_args /* {
2393 error = kern_lstat(td, uap->path, UIO_USERSPACE, &sb);
2395 error = copyout(&sb, uap->ub, sizeof (sb));
2400 kern_lstat(struct thread *td, char *path, enum uio_seg pathseg, struct stat *sbp)
2403 return (kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, path, pathseg,
2408 * Implementation of the NetBSD [l]stat() functions.
2415 bzero(nsb, sizeof *nsb);
2416 nsb->st_dev = sb->st_dev;
2417 nsb->st_ino = sb->st_ino;
2418 nsb->st_mode = sb->st_mode;
2419 nsb->st_nlink = sb->st_nlink;
2420 nsb->st_uid = sb->st_uid;
2421 nsb->st_gid = sb->st_gid;
2422 nsb->st_rdev = sb->st_rdev;
2423 nsb->st_atimespec = sb->st_atimespec;
2424 nsb->st_mtimespec = sb->st_mtimespec;
2425 nsb->st_ctimespec = sb->st_ctimespec;
2426 nsb->st_size = sb->st_size;
2427 nsb->st_blocks = sb->st_blocks;
2428 nsb->st_blksize = sb->st_blksize;
2429 nsb->st_flags = sb->st_flags;
2430 nsb->st_gen = sb->st_gen;
2431 nsb->st_birthtimespec = sb->st_birthtimespec;
2434 #ifndef _SYS_SYSPROTO_H_
2443 register struct nstat_args /* {
2452 error = kern_stat(td, uap->path, UIO_USERSPACE, &sb);
2455 cvtnstat(&sb, &nsb);
2456 error = copyout(&nsb, uap->ub, sizeof (nsb));
2461 * NetBSD lstat. Get file status; this version does not follow links.
2463 #ifndef _SYS_SYSPROTO_H_
2472 register struct nlstat_args /* {
2481 error = kern_lstat(td, uap->path, UIO_USERSPACE, &sb);
2484 cvtnstat(&sb, &nsb);
2485 error = copyout(&nsb, uap->ub, sizeof (nsb));
2490 * Get configurable pathname variables.
2492 #ifndef _SYS_SYSPROTO_H_
2493 struct pathconf_args {
2501 register struct pathconf_args /* {
2507 return (kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name, FOLLOW));
2510 #ifndef _SYS_SYSPROTO_H_
2511 struct lpathconf_args {
2519 register struct lpathconf_args /* {
2525 return (kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name, NOFOLLOW));
2529 kern_pathconf(struct thread *td, char *path, enum uio_seg pathseg, int name,
2532 struct nameidata nd;
2533 int error, vfslocked;
2535 NDINIT(&nd, LOOKUP, LOCKSHARED | LOCKLEAF | MPSAFE | AUDITVNODE1 |
2536 flags, pathseg, path, td);
2537 if ((error = namei(&nd)) != 0)
2539 vfslocked = NDHASGIANT(&nd);
2540 NDFREE(&nd, NDF_ONLY_PNBUF);
2542 /* If asynchronous I/O is available, it works for all files. */
2543 if (name == _PC_ASYNC_IO)
2544 td->td_retval[0] = async_io_version;
2546 error = VOP_PATHCONF(nd.ni_vp, name, td->td_retval);
2548 VFS_UNLOCK_GIANT(vfslocked);
2553 * Return target name of a symbolic link.
2555 #ifndef _SYS_SYSPROTO_H_
2556 struct readlink_args {
2565 register struct readlink_args /* {
2572 return (kern_readlink(td, uap->path, UIO_USERSPACE, uap->buf,
2573 UIO_USERSPACE, uap->count));
2575 #ifndef _SYS_SYSPROTO_H_
2576 struct readlinkat_args {
2584 readlinkat(struct thread *td, struct readlinkat_args *uap)
2587 return (kern_readlinkat(td, uap->fd, uap->path, UIO_USERSPACE,
2588 uap->buf, UIO_USERSPACE, uap->bufsize));
2592 kern_readlink(struct thread *td, char *path, enum uio_seg pathseg, char *buf,
2593 enum uio_seg bufseg, size_t count)
2596 return (kern_readlinkat(td, AT_FDCWD, path, pathseg, buf, bufseg,
2601 kern_readlinkat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2602 char *buf, enum uio_seg bufseg, size_t count)
2608 struct nameidata nd;
2611 if (count > INT_MAX)
2614 NDINIT_AT(&nd, LOOKUP, NOFOLLOW | LOCKSHARED | LOCKLEAF | MPSAFE |
2615 AUDITVNODE1, pathseg, path, fd, td);
2617 if ((error = namei(&nd)) != 0)
2619 NDFREE(&nd, NDF_ONLY_PNBUF);
2620 vfslocked = NDHASGIANT(&nd);
2623 error = mac_vnode_check_readlink(td->td_ucred, vp);
2626 VFS_UNLOCK_GIANT(vfslocked);
2630 if (vp->v_type != VLNK)
2633 aiov.iov_base = buf;
2634 aiov.iov_len = count;
2635 auio.uio_iov = &aiov;
2636 auio.uio_iovcnt = 1;
2637 auio.uio_offset = 0;
2638 auio.uio_rw = UIO_READ;
2639 auio.uio_segflg = bufseg;
2641 auio.uio_resid = count;
2642 error = VOP_READLINK(vp, &auio, td->td_ucred);
2645 VFS_UNLOCK_GIANT(vfslocked);
2646 td->td_retval[0] = count - auio.uio_resid;
2651 * Common implementation code for chflags() and fchflags().
2654 setfflags(td, vp, flags)
2664 * Prevent non-root users from setting flags on devices. When
2665 * a device is reused, users can retain ownership of the device
2666 * if they are allowed to set flags and programs assume that
2667 * chown can't fail when done as root.
2669 if (vp->v_type == VCHR || vp->v_type == VBLK) {
2670 error = priv_check(td, PRIV_VFS_CHFLAGS_DEV);
2675 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2677 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2679 vattr.va_flags = flags;
2681 error = mac_vnode_check_setflags(td->td_ucred, vp, vattr.va_flags);
2684 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
2686 vn_finished_write(mp);
2691 * Change flags of a file given a path name.
2693 #ifndef _SYS_SYSPROTO_H_
2694 struct chflags_args {
2702 register struct chflags_args /* {
2708 struct nameidata nd;
2711 AUDIT_ARG_FFLAGS(uap->flags);
2712 NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE | AUDITVNODE1, UIO_USERSPACE,
2714 if ((error = namei(&nd)) != 0)
2716 NDFREE(&nd, NDF_ONLY_PNBUF);
2717 vfslocked = NDHASGIANT(&nd);
2718 error = setfflags(td, nd.ni_vp, uap->flags);
2720 VFS_UNLOCK_GIANT(vfslocked);
2725 * Same as chflags() but doesn't follow symlinks.
2730 register struct lchflags_args /* {
2736 struct nameidata nd;
2739 AUDIT_ARG_FFLAGS(uap->flags);
2740 NDINIT(&nd, LOOKUP, NOFOLLOW | MPSAFE | AUDITVNODE1, UIO_USERSPACE,
2742 if ((error = namei(&nd)) != 0)
2744 vfslocked = NDHASGIANT(&nd);
2745 NDFREE(&nd, NDF_ONLY_PNBUF);
2746 error = setfflags(td, nd.ni_vp, uap->flags);
2748 VFS_UNLOCK_GIANT(vfslocked);
2753 * Change flags of a file given a file descriptor.
2755 #ifndef _SYS_SYSPROTO_H_
2756 struct fchflags_args {
2764 register struct fchflags_args /* {
2773 AUDIT_ARG_FD(uap->fd);
2774 AUDIT_ARG_FFLAGS(uap->flags);
2775 if ((error = getvnode(td->td_proc->p_fd, uap->fd, &fp)) != 0)
2777 vfslocked = VFS_LOCK_GIANT(fp->f_vnode->v_mount);
2779 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
2780 AUDIT_ARG_VNODE1(fp->f_vnode);
2781 VOP_UNLOCK(fp->f_vnode, 0);
2783 error = setfflags(td, fp->f_vnode, uap->flags);
2784 VFS_UNLOCK_GIANT(vfslocked);
2790 * Common implementation code for chmod(), lchmod() and fchmod().
2793 setfmode(td, vp, mode)
2802 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2804 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2806 vattr.va_mode = mode & ALLPERMS;
2808 error = mac_vnode_check_setmode(td->td_ucred, vp, vattr.va_mode);
2811 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
2813 vn_finished_write(mp);
2818 * Change mode of a file given path name.
2820 #ifndef _SYS_SYSPROTO_H_
2829 register struct chmod_args /* {
2835 return (kern_chmod(td, uap->path, UIO_USERSPACE, uap->mode));
2838 #ifndef _SYS_SYSPROTO_H_
2839 struct fchmodat_args {
2847 fchmodat(struct thread *td, struct fchmodat_args *uap)
2849 int flag = uap->flag;
2851 char *path = uap->path;
2852 mode_t mode = uap->mode;
2854 if (flag & ~AT_SYMLINK_NOFOLLOW)
2857 return (kern_fchmodat(td, fd, path, UIO_USERSPACE, mode, flag));
2861 kern_chmod(struct thread *td, char *path, enum uio_seg pathseg, int mode)
2864 return (kern_fchmodat(td, AT_FDCWD, path, pathseg, mode, 0));
2868 * Change mode of a file given path name (don't follow links.)
2870 #ifndef _SYS_SYSPROTO_H_
2871 struct lchmod_args {
2879 register struct lchmod_args /* {
2885 return (kern_fchmodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2886 uap->mode, AT_SYMLINK_NOFOLLOW));
2891 kern_fchmodat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2892 mode_t mode, int flag)
2895 struct nameidata nd;
2899 AUDIT_ARG_MODE(mode);
2900 follow = (flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
2901 NDINIT_AT(&nd, LOOKUP, follow | MPSAFE | AUDITVNODE1, pathseg, path,
2903 if ((error = namei(&nd)) != 0)
2905 vfslocked = NDHASGIANT(&nd);
2906 NDFREE(&nd, NDF_ONLY_PNBUF);
2907 error = setfmode(td, nd.ni_vp, mode);
2909 VFS_UNLOCK_GIANT(vfslocked);
2914 * Change mode of a file given a file descriptor.
2916 #ifndef _SYS_SYSPROTO_H_
2917 struct fchmod_args {
2925 register struct fchmod_args /* {
2934 AUDIT_ARG_FD(uap->fd);
2935 AUDIT_ARG_MODE(uap->mode);
2936 if ((error = getvnode(td->td_proc->p_fd, uap->fd, &fp)) != 0)
2938 vfslocked = VFS_LOCK_GIANT(fp->f_vnode->v_mount);
2940 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
2941 AUDIT_ARG_VNODE1(fp->f_vnode);
2942 VOP_UNLOCK(fp->f_vnode, 0);
2944 error = setfmode(td, fp->f_vnode, uap->mode);
2945 VFS_UNLOCK_GIANT(vfslocked);
2951 * Common implementation for chown(), lchown(), and fchown()
2954 setfown(td, vp, uid, gid)
2964 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2966 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2971 error = mac_vnode_check_setowner(td->td_ucred, vp, vattr.va_uid,
2975 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
2977 vn_finished_write(mp);
2982 * Set ownership given a path name.
2984 #ifndef _SYS_SYSPROTO_H_
2994 register struct chown_args /* {
3001 return (kern_chown(td, uap->path, UIO_USERSPACE, uap->uid, uap->gid));
3004 #ifndef _SYS_SYSPROTO_H_
3005 struct fchownat_args {
3014 fchownat(struct thread *td, struct fchownat_args *uap)
3019 if (flag & ~AT_SYMLINK_NOFOLLOW)
3022 return (kern_fchownat(td, uap->fd, uap->path, UIO_USERSPACE, uap->uid,
3023 uap->gid, uap->flag));
3027 kern_chown(struct thread *td, char *path, enum uio_seg pathseg, int uid,
3031 return (kern_fchownat(td, AT_FDCWD, path, pathseg, uid, gid, 0));
3035 kern_fchownat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
3036 int uid, int gid, int flag)
3038 struct nameidata nd;
3039 int error, vfslocked, follow;
3041 AUDIT_ARG_OWNER(uid, gid);
3042 follow = (flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
3043 NDINIT_AT(&nd, LOOKUP, follow | MPSAFE | AUDITVNODE1, pathseg, path,
3046 if ((error = namei(&nd)) != 0)
3048 vfslocked = NDHASGIANT(&nd);
3049 NDFREE(&nd, NDF_ONLY_PNBUF);
3050 error = setfown(td, nd.ni_vp, uid, gid);
3052 VFS_UNLOCK_GIANT(vfslocked);
3057 * Set ownership given a path name, do not cross symlinks.
3059 #ifndef _SYS_SYSPROTO_H_
3060 struct lchown_args {
3069 register struct lchown_args /* {
3076 return (kern_lchown(td, uap->path, UIO_USERSPACE, uap->uid, uap->gid));
3080 kern_lchown(struct thread *td, char *path, enum uio_seg pathseg, int uid,
3084 return (kern_fchownat(td, AT_FDCWD, path, pathseg, uid, gid,
3085 AT_SYMLINK_NOFOLLOW));
3089 * Set ownership given a file descriptor.
3091 #ifndef _SYS_SYSPROTO_H_
3092 struct fchown_args {
3101 register struct fchown_args /* {
3111 AUDIT_ARG_FD(uap->fd);
3112 AUDIT_ARG_OWNER(uap->uid, uap->gid);
3113 if ((error = getvnode(td->td_proc->p_fd, uap->fd, &fp)) != 0)
3115 vfslocked = VFS_LOCK_GIANT(fp->f_vnode->v_mount);
3117 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
3118 AUDIT_ARG_VNODE1(fp->f_vnode);
3119 VOP_UNLOCK(fp->f_vnode, 0);
3121 error = setfown(td, fp->f_vnode, uap->uid, uap->gid);
3122 VFS_UNLOCK_GIANT(vfslocked);
3128 * Common implementation code for utimes(), lutimes(), and futimes().
3131 getutimes(usrtvp, tvpseg, tsp)
3132 const struct timeval *usrtvp;
3133 enum uio_seg tvpseg;
3134 struct timespec *tsp;
3136 struct timeval tv[2];
3137 const struct timeval *tvp;
3140 if (usrtvp == NULL) {
3141 vfs_timestamp(&tsp[0]);
3144 if (tvpseg == UIO_SYSSPACE) {
3147 if ((error = copyin(usrtvp, tv, sizeof(tv))) != 0)
3152 if (tvp[0].tv_usec < 0 || tvp[0].tv_usec >= 1000000 ||
3153 tvp[1].tv_usec < 0 || tvp[1].tv_usec >= 1000000)
3155 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
3156 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
3162 * Common implementation code for utimes(), lutimes(), and futimes().
3165 setutimes(td, vp, ts, numtimes, nullflag)
3168 const struct timespec *ts;
3172 int error, setbirthtime;
3176 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
3178 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3180 if (numtimes < 3 && !VOP_GETATTR(vp, &vattr, td->td_ucred) &&
3181 timespeccmp(&ts[1], &vattr.va_birthtime, < ))
3184 vattr.va_atime = ts[0];
3185 vattr.va_mtime = ts[1];
3187 vattr.va_birthtime = ts[1];
3189 vattr.va_birthtime = ts[2];
3191 vattr.va_vaflags |= VA_UTIMES_NULL;
3193 error = mac_vnode_check_setutimes(td->td_ucred, vp, vattr.va_atime,
3197 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3199 vn_finished_write(mp);
3204 * Set the access and modification times of a file.
3206 #ifndef _SYS_SYSPROTO_H_
3207 struct utimes_args {
3209 struct timeval *tptr;
3215 register struct utimes_args /* {
3217 struct timeval *tptr;
3221 return (kern_utimes(td, uap->path, UIO_USERSPACE, uap->tptr,
3225 #ifndef _SYS_SYSPROTO_H_
3226 struct futimesat_args {
3229 const struct timeval * times;
3233 futimesat(struct thread *td, struct futimesat_args *uap)
3236 return (kern_utimesat(td, uap->fd, uap->path, UIO_USERSPACE,
3237 uap->times, UIO_USERSPACE));
3241 kern_utimes(struct thread *td, char *path, enum uio_seg pathseg,
3242 struct timeval *tptr, enum uio_seg tptrseg)
3245 return (kern_utimesat(td, AT_FDCWD, path, pathseg, tptr, tptrseg));
3249 kern_utimesat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
3250 struct timeval *tptr, enum uio_seg tptrseg)
3252 struct nameidata nd;
3253 struct timespec ts[2];
3254 int error, vfslocked;
3256 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3258 NDINIT_AT(&nd, LOOKUP, FOLLOW | MPSAFE | AUDITVNODE1, pathseg, path,
3261 if ((error = namei(&nd)) != 0)
3263 vfslocked = NDHASGIANT(&nd);
3264 NDFREE(&nd, NDF_ONLY_PNBUF);
3265 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3267 VFS_UNLOCK_GIANT(vfslocked);
3272 * Set the access and modification times of a file.
3274 #ifndef _SYS_SYSPROTO_H_
3275 struct lutimes_args {
3277 struct timeval *tptr;
3283 register struct lutimes_args /* {
3285 struct timeval *tptr;
3289 return (kern_lutimes(td, uap->path, UIO_USERSPACE, uap->tptr,
3294 kern_lutimes(struct thread *td, char *path, enum uio_seg pathseg,
3295 struct timeval *tptr, enum uio_seg tptrseg)
3297 struct timespec ts[2];
3299 struct nameidata nd;
3302 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3304 NDINIT(&nd, LOOKUP, NOFOLLOW | MPSAFE | AUDITVNODE1, pathseg, path, td);
3305 if ((error = namei(&nd)) != 0)
3307 vfslocked = NDHASGIANT(&nd);
3308 NDFREE(&nd, NDF_ONLY_PNBUF);
3309 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3311 VFS_UNLOCK_GIANT(vfslocked);
3316 * Set the access and modification times of a file.
3318 #ifndef _SYS_SYSPROTO_H_
3319 struct futimes_args {
3321 struct timeval *tptr;
3327 register struct futimes_args /* {
3329 struct timeval *tptr;
3333 return (kern_futimes(td, uap->fd, uap->tptr, UIO_USERSPACE));
3337 kern_futimes(struct thread *td, int fd, struct timeval *tptr,
3338 enum uio_seg tptrseg)
3340 struct timespec ts[2];
3346 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3348 if ((error = getvnode(td->td_proc->p_fd, fd, &fp)) != 0)
3350 vfslocked = VFS_LOCK_GIANT(fp->f_vnode->v_mount);
3352 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
3353 AUDIT_ARG_VNODE1(fp->f_vnode);
3354 VOP_UNLOCK(fp->f_vnode, 0);
3356 error = setutimes(td, fp->f_vnode, ts, 2, tptr == NULL);
3357 VFS_UNLOCK_GIANT(vfslocked);
3363 * Truncate a file given its path name.
3365 #ifndef _SYS_SYSPROTO_H_
3366 struct truncate_args {
3375 register struct truncate_args /* {
3382 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3386 kern_truncate(struct thread *td, char *path, enum uio_seg pathseg, off_t length)
3392 struct nameidata nd;
3397 NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE | AUDITVNODE1, pathseg, path, td);
3398 if ((error = namei(&nd)) != 0)
3400 vfslocked = NDHASGIANT(&nd);
3402 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0) {
3404 VFS_UNLOCK_GIANT(vfslocked);
3407 NDFREE(&nd, NDF_ONLY_PNBUF);
3408 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3409 if (vp->v_type == VDIR)
3412 else if ((error = mac_vnode_check_write(td->td_ucred, NOCRED, vp))) {
3415 else if ((error = vn_writechk(vp)) == 0 &&
3416 (error = VOP_ACCESS(vp, VWRITE, td->td_ucred, td)) == 0) {
3418 vattr.va_size = length;
3419 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3422 vn_finished_write(mp);
3423 VFS_UNLOCK_GIANT(vfslocked);
3427 #if defined(COMPAT_43)
3429 * Truncate a file given its path name.
3431 #ifndef _SYS_SYSPROTO_H_
3432 struct otruncate_args {
3440 register struct otruncate_args /* {
3445 struct truncate_args /* {
3451 nuap.path = uap->path;
3452 nuap.length = uap->length;
3453 return (truncate(td, &nuap));
3455 #endif /* COMPAT_43 */
3457 /* Versions with the pad argument */
3459 freebsd6_truncate(struct thread *td, struct freebsd6_truncate_args *uap)
3461 struct truncate_args ouap;
3463 ouap.path = uap->path;
3464 ouap.length = uap->length;
3465 return (truncate(td, &ouap));
3469 freebsd6_ftruncate(struct thread *td, struct freebsd6_ftruncate_args *uap)
3471 struct ftruncate_args ouap;
3474 ouap.length = uap->length;
3475 return (ftruncate(td, &ouap));
3479 * Sync an open file.
3481 #ifndef _SYS_SYSPROTO_H_
3489 struct fsync_args /* {
3497 int error, lock_flags;
3499 AUDIT_ARG_FD(uap->fd);
3500 if ((error = getvnode(td->td_proc->p_fd, uap->fd, &fp)) != 0)
3503 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
3504 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
3506 if (MNT_SHARED_WRITES(mp) ||
3507 ((mp == NULL) && MNT_SHARED_WRITES(vp->v_mount))) {
3508 lock_flags = LK_SHARED;
3510 lock_flags = LK_EXCLUSIVE;
3512 vn_lock(vp, lock_flags | LK_RETRY);
3513 AUDIT_ARG_VNODE1(vp);
3514 if (vp->v_object != NULL) {
3515 VM_OBJECT_LOCK(vp->v_object);
3516 vm_object_page_clean(vp->v_object, 0, 0, 0);
3517 VM_OBJECT_UNLOCK(vp->v_object);
3519 error = VOP_FSYNC(vp, MNT_WAIT, td);
3522 vn_finished_write(mp);
3524 VFS_UNLOCK_GIANT(vfslocked);
3530 * Rename files. Source and destination must either both be directories, or
3531 * both not be directories. If target is a directory, it must be empty.
3533 #ifndef _SYS_SYSPROTO_H_
3534 struct rename_args {
3542 register struct rename_args /* {
3548 return (kern_rename(td, uap->from, uap->to, UIO_USERSPACE));
3551 #ifndef _SYS_SYSPROTO_H_
3552 struct renameat_args {
3560 renameat(struct thread *td, struct renameat_args *uap)
3563 return (kern_renameat(td, uap->oldfd, uap->old, uap->newfd, uap->new,
3568 kern_rename(struct thread *td, char *from, char *to, enum uio_seg pathseg)
3571 return (kern_renameat(td, AT_FDCWD, from, AT_FDCWD, to, pathseg));
3575 kern_renameat(struct thread *td, int oldfd, char *old, int newfd, char *new,
3576 enum uio_seg pathseg)
3578 struct mount *mp = NULL;
3579 struct vnode *tvp, *fvp, *tdvp;
3580 struct nameidata fromnd, tond;
3587 NDINIT_AT(&fromnd, DELETE, LOCKPARENT | LOCKLEAF | SAVESTART | MPSAFE |
3588 AUDITVNODE1, pathseg, old, oldfd, td);
3590 NDINIT_AT(&fromnd, DELETE, WANTPARENT | SAVESTART | MPSAFE |
3591 AUDITVNODE1, pathseg, old, oldfd, td);
3594 if ((error = namei(&fromnd)) != 0)
3596 fvfslocked = NDHASGIANT(&fromnd);
3599 error = mac_vnode_check_rename_from(td->td_ucred, fromnd.ni_dvp,
3600 fromnd.ni_vp, &fromnd.ni_cnd);
3601 VOP_UNLOCK(fromnd.ni_dvp, 0);
3602 if (fromnd.ni_dvp != fromnd.ni_vp)
3603 VOP_UNLOCK(fromnd.ni_vp, 0);
3607 error = vn_start_write(fvp, &mp, V_WAIT | PCATCH);
3609 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3610 vrele(fromnd.ni_dvp);
3614 NDINIT_AT(&tond, RENAME, LOCKPARENT | LOCKLEAF | NOCACHE | SAVESTART |
3615 MPSAFE | AUDITVNODE2, pathseg, new, newfd, td);
3616 if (fromnd.ni_vp->v_type == VDIR)
3617 tond.ni_cnd.cn_flags |= WILLBEDIR;
3618 if ((error = namei(&tond)) != 0) {
3619 /* Translate error code for rename("dir1", "dir2/."). */
3620 if (error == EISDIR && fvp->v_type == VDIR)
3622 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3623 vrele(fromnd.ni_dvp);
3625 vn_finished_write(mp);
3628 tvfslocked = NDHASGIANT(&tond);
3632 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
3635 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
3645 * If the source is the same as the destination (that is, if they
3646 * are links to the same vnode), then there is nothing to do.
3652 error = mac_vnode_check_rename_to(td->td_ucred, tdvp,
3653 tond.ni_vp, fromnd.ni_dvp == tdvp, &tond.ni_cnd);
3657 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
3658 tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
3659 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3660 NDFREE(&tond, NDF_ONLY_PNBUF);
3662 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3663 NDFREE(&tond, NDF_ONLY_PNBUF);
3670 vrele(fromnd.ni_dvp);
3673 vrele(tond.ni_startdir);
3674 vn_finished_write(mp);
3676 if (fromnd.ni_startdir)
3677 vrele(fromnd.ni_startdir);
3678 VFS_UNLOCK_GIANT(fvfslocked);
3679 VFS_UNLOCK_GIANT(tvfslocked);
3686 * Make a directory file.
3688 #ifndef _SYS_SYSPROTO_H_
3697 register struct mkdir_args /* {
3703 return (kern_mkdir(td, uap->path, UIO_USERSPACE, uap->mode));
3706 #ifndef _SYS_SYSPROTO_H_
3707 struct mkdirat_args {
3714 mkdirat(struct thread *td, struct mkdirat_args *uap)
3717 return (kern_mkdirat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode));
3721 kern_mkdir(struct thread *td, char *path, enum uio_seg segflg, int mode)
3724 return (kern_mkdirat(td, AT_FDCWD, path, segflg, mode));
3728 kern_mkdirat(struct thread *td, int fd, char *path, enum uio_seg segflg,
3735 struct nameidata nd;
3738 AUDIT_ARG_MODE(mode);
3741 NDINIT_AT(&nd, CREATE, LOCKPARENT | SAVENAME | MPSAFE | AUDITVNODE1,
3742 segflg, path, fd, td);
3743 nd.ni_cnd.cn_flags |= WILLBEDIR;
3744 if ((error = namei(&nd)) != 0)
3746 vfslocked = NDHASGIANT(&nd);
3749 NDFREE(&nd, NDF_ONLY_PNBUF);
3751 * XXX namei called with LOCKPARENT but not LOCKLEAF has
3752 * the strange behaviour of leaving the vnode unlocked
3753 * if the target is the same vnode as the parent.
3755 if (vp == nd.ni_dvp)
3760 VFS_UNLOCK_GIANT(vfslocked);
3763 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3764 NDFREE(&nd, NDF_ONLY_PNBUF);
3766 VFS_UNLOCK_GIANT(vfslocked);
3767 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3772 vattr.va_type = VDIR;
3773 vattr.va_mode = (mode & ACCESSPERMS) &~ td->td_proc->p_fd->fd_cmask;
3775 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
3780 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
3784 NDFREE(&nd, NDF_ONLY_PNBUF);
3788 vn_finished_write(mp);
3789 VFS_UNLOCK_GIANT(vfslocked);
3794 * Remove a directory file.
3796 #ifndef _SYS_SYSPROTO_H_
3804 struct rmdir_args /* {
3809 return (kern_rmdir(td, uap->path, UIO_USERSPACE));
3813 kern_rmdir(struct thread *td, char *path, enum uio_seg pathseg)
3816 return (kern_rmdirat(td, AT_FDCWD, path, pathseg));
3820 kern_rmdirat(struct thread *td, int fd, char *path, enum uio_seg pathseg)
3825 struct nameidata nd;
3830 NDINIT_AT(&nd, DELETE, LOCKPARENT | LOCKLEAF | MPSAFE | AUDITVNODE1,
3831 pathseg, path, fd, td);
3832 if ((error = namei(&nd)) != 0)
3834 vfslocked = NDHASGIANT(&nd);
3836 if (vp->v_type != VDIR) {
3841 * No rmdir "." please.
3843 if (nd.ni_dvp == vp) {
3848 * The root of a mounted filesystem cannot be deleted.
3850 if (vp->v_vflag & VV_ROOT) {
3855 error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp,
3860 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3861 NDFREE(&nd, NDF_ONLY_PNBUF);
3863 if (nd.ni_dvp == vp)
3867 VFS_UNLOCK_GIANT(vfslocked);
3868 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3872 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
3873 vn_finished_write(mp);
3875 NDFREE(&nd, NDF_ONLY_PNBUF);
3877 if (nd.ni_dvp == vp)
3881 VFS_UNLOCK_GIANT(vfslocked);
3887 * Read a block of directory entries in a filesystem independent format.
3889 #ifndef _SYS_SYSPROTO_H_
3890 struct ogetdirentries_args {
3898 ogetdirentries(td, uap)
3900 register struct ogetdirentries_args /* {
3909 struct uio auio, kuio;
3910 struct iovec aiov, kiov;
3911 struct dirent *dp, *edp;
3913 int error, eofflag, readcnt, vfslocked;
3916 /* XXX arbitrary sanity limit on `count'. */
3917 if (uap->count > 64 * 1024)
3919 if ((error = getvnode(td->td_proc->p_fd, uap->fd, &fp)) != 0)
3921 if ((fp->f_flag & FREAD) == 0) {
3927 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
3928 if (vp->v_type != VDIR) {
3929 VFS_UNLOCK_GIANT(vfslocked);
3933 aiov.iov_base = uap->buf;
3934 aiov.iov_len = uap->count;
3935 auio.uio_iov = &aiov;
3936 auio.uio_iovcnt = 1;
3937 auio.uio_rw = UIO_READ;
3938 auio.uio_segflg = UIO_USERSPACE;
3940 auio.uio_resid = uap->count;
3941 vn_lock(vp, LK_SHARED | LK_RETRY);
3942 loff = auio.uio_offset = fp->f_offset;
3944 error = mac_vnode_check_readdir(td->td_ucred, vp);
3947 VFS_UNLOCK_GIANT(vfslocked);
3952 # if (BYTE_ORDER != LITTLE_ENDIAN)
3953 if (vp->v_mount->mnt_maxsymlinklen <= 0) {
3954 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag,
3956 fp->f_offset = auio.uio_offset;
3961 kuio.uio_iov = &kiov;
3962 kuio.uio_segflg = UIO_SYSSPACE;
3963 kiov.iov_len = uap->count;
3964 dirbuf = malloc(uap->count, M_TEMP, M_WAITOK);
3965 kiov.iov_base = dirbuf;
3966 error = VOP_READDIR(vp, &kuio, fp->f_cred, &eofflag,
3968 fp->f_offset = kuio.uio_offset;
3970 readcnt = uap->count - kuio.uio_resid;
3971 edp = (struct dirent *)&dirbuf[readcnt];
3972 for (dp = (struct dirent *)dirbuf; dp < edp; ) {
3973 # if (BYTE_ORDER == LITTLE_ENDIAN)
3975 * The expected low byte of
3976 * dp->d_namlen is our dp->d_type.
3977 * The high MBZ byte of dp->d_namlen
3978 * is our dp->d_namlen.
3980 dp->d_type = dp->d_namlen;
3984 * The dp->d_type is the high byte
3985 * of the expected dp->d_namlen,
3986 * so must be zero'ed.
3990 if (dp->d_reclen > 0) {
3991 dp = (struct dirent *)
3992 ((char *)dp + dp->d_reclen);
3999 error = uiomove(dirbuf, readcnt, &auio);
4001 free(dirbuf, M_TEMP);
4005 VFS_UNLOCK_GIANT(vfslocked);
4009 if (uap->count == auio.uio_resid &&
4010 (vp->v_vflag & VV_ROOT) &&
4011 (vp->v_mount->mnt_flag & MNT_UNION)) {
4012 struct vnode *tvp = vp;
4013 vp = vp->v_mount->mnt_vnodecovered;
4019 VFS_UNLOCK_GIANT(vfslocked);
4023 VFS_UNLOCK_GIANT(vfslocked);
4024 error = copyout(&loff, uap->basep, sizeof(long));
4026 td->td_retval[0] = uap->count - auio.uio_resid;
4029 #endif /* COMPAT_43 */
4032 * Read a block of directory entries in a filesystem independent format.
4034 #ifndef _SYS_SYSPROTO_H_
4035 struct getdirentries_args {
4043 getdirentries(td, uap)
4045 register struct getdirentries_args /* {
4055 error = kern_getdirentries(td, uap->fd, uap->buf, uap->count, &base);
4058 if (uap->basep != NULL)
4059 error = copyout(&base, uap->basep, sizeof(long));
4064 kern_getdirentries(struct thread *td, int fd, char *buf, u_int count,
4076 if (count > INT_MAX)
4078 if ((error = getvnode(td->td_proc->p_fd, fd, &fp)) != 0)
4080 if ((fp->f_flag & FREAD) == 0) {
4086 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
4087 if (vp->v_type != VDIR) {
4088 VFS_UNLOCK_GIANT(vfslocked);
4092 aiov.iov_base = buf;
4093 aiov.iov_len = count;
4094 auio.uio_iov = &aiov;
4095 auio.uio_iovcnt = 1;
4096 auio.uio_rw = UIO_READ;
4097 auio.uio_segflg = UIO_USERSPACE;
4099 auio.uio_resid = count;
4100 vn_lock(vp, LK_SHARED | LK_RETRY);
4101 AUDIT_ARG_VNODE1(vp);
4102 loff = auio.uio_offset = fp->f_offset;
4104 error = mac_vnode_check_readdir(td->td_ucred, vp);
4107 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL,
4109 fp->f_offset = auio.uio_offset;
4112 VFS_UNLOCK_GIANT(vfslocked);
4115 if (count == auio.uio_resid &&
4116 (vp->v_vflag & VV_ROOT) &&
4117 (vp->v_mount->mnt_flag & MNT_UNION)) {
4118 struct vnode *tvp = vp;
4119 vp = vp->v_mount->mnt_vnodecovered;
4125 VFS_UNLOCK_GIANT(vfslocked);
4129 VFS_UNLOCK_GIANT(vfslocked);
4131 td->td_retval[0] = count - auio.uio_resid;
4137 #ifndef _SYS_SYSPROTO_H_
4138 struct getdents_args {
4147 register struct getdents_args /* {
4153 struct getdirentries_args ap;
4156 ap.count = uap->count;
4158 return (getdirentries(td, &ap));
4162 * Set the mode mask for creation of filesystem nodes.
4164 #ifndef _SYS_SYSPROTO_H_
4172 struct umask_args /* {
4176 register struct filedesc *fdp;
4178 FILEDESC_XLOCK(td->td_proc->p_fd);
4179 fdp = td->td_proc->p_fd;
4180 td->td_retval[0] = fdp->fd_cmask;
4181 fdp->fd_cmask = uap->newmask & ALLPERMS;
4182 FILEDESC_XUNLOCK(td->td_proc->p_fd);
4187 * Void all references to file by ripping underlying filesystem away from
4190 #ifndef _SYS_SYSPROTO_H_
4191 struct revoke_args {
4198 register struct revoke_args /* {
4205 struct nameidata nd;
4208 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | MPSAFE | AUDITVNODE1,
4209 UIO_USERSPACE, uap->path, td);
4210 if ((error = namei(&nd)) != 0)
4212 vfslocked = NDHASGIANT(&nd);
4214 NDFREE(&nd, NDF_ONLY_PNBUF);
4215 if (vp->v_type != VCHR || vp->v_rdev == NULL) {
4220 error = mac_vnode_check_revoke(td->td_ucred, vp);
4224 error = VOP_GETATTR(vp, &vattr, td->td_ucred);
4227 if (td->td_ucred->cr_uid != vattr.va_uid) {
4228 error = priv_check(td, PRIV_VFS_ADMIN);
4233 VOP_REVOKE(vp, REVOKEALL);
4236 VFS_UNLOCK_GIANT(vfslocked);
4241 * Convert a user file descriptor to a kernel file entry.
4242 * A reference on the file entry is held upon returning.
4245 getvnode(fdp, fd, fpp)
4246 struct filedesc *fdp;
4255 if (fdp == NULL || (fp = fget_unlocked(fdp, fd)) == NULL)
4257 else if (fp->f_vnode == NULL) {
4259 fdrop(fp, curthread);
4266 * Get an (NFS) file handle.
4268 #ifndef _SYS_SYSPROTO_H_
4269 struct lgetfh_args {
4277 register struct lgetfh_args *uap;
4279 struct nameidata nd;
4281 register struct vnode *vp;
4285 error = priv_check(td, PRIV_VFS_GETFH);
4288 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | MPSAFE | AUDITVNODE1,
4289 UIO_USERSPACE, uap->fname, td);
4293 vfslocked = NDHASGIANT(&nd);
4294 NDFREE(&nd, NDF_ONLY_PNBUF);
4296 bzero(&fh, sizeof(fh));
4297 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
4298 error = VOP_VPTOFH(vp, &fh.fh_fid);
4300 VFS_UNLOCK_GIANT(vfslocked);
4303 error = copyout(&fh, uap->fhp, sizeof (fh));
4307 #ifndef _SYS_SYSPROTO_H_
4316 register struct getfh_args *uap;
4318 struct nameidata nd;
4320 register struct vnode *vp;
4324 error = priv_check(td, PRIV_VFS_GETFH);
4327 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | MPSAFE | AUDITVNODE1,
4328 UIO_USERSPACE, uap->fname, td);
4332 vfslocked = NDHASGIANT(&nd);
4333 NDFREE(&nd, NDF_ONLY_PNBUF);
4335 bzero(&fh, sizeof(fh));
4336 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
4337 error = VOP_VPTOFH(vp, &fh.fh_fid);
4339 VFS_UNLOCK_GIANT(vfslocked);
4342 error = copyout(&fh, uap->fhp, sizeof (fh));
4347 * syscall for the rpc.lockd to use to translate a NFS file handle into an
4350 * warning: do not remove the priv_check() call or this becomes one giant
4353 #ifndef _SYS_SYSPROTO_H_
4354 struct fhopen_args {
4355 const struct fhandle *u_fhp;
4362 struct fhopen_args /* {
4363 const struct fhandle *u_fhp;
4367 struct proc *p = td->td_proc;
4372 struct vattr *vap = &vat;
4375 register struct filedesc *fdp = p->p_fd;
4376 int fmode, error, type;
4382 error = priv_check(td, PRIV_VFS_FHOPEN);
4385 fmode = FFLAGS(uap->flags);
4386 /* why not allow a non-read/write open for our lockd? */
4387 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
4389 error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
4392 /* find the mount point */
4393 mp = vfs_busyfs(&fhp.fh_fsid);
4396 vfslocked = VFS_LOCK_GIANT(mp);
4397 /* now give me my vnode, it gets returned to me locked */
4398 error = VFS_FHTOVP(mp, &fhp.fh_fid, &vp);
4403 * from now on we have to make sure not
4404 * to forget about the vnode
4405 * any error that causes an abort must vput(vp)
4406 * just set error = err and 'goto bad;'.
4412 if (vp->v_type == VLNK) {
4416 if (vp->v_type == VSOCK) {
4421 if (fmode & (FWRITE | O_TRUNC)) {
4422 if (vp->v_type == VDIR) {
4426 error = vn_writechk(vp);
4433 if ((fmode & O_APPEND) && (fmode & FWRITE))
4436 error = mac_vnode_check_open(td->td_ucred, vp, accmode);
4441 error = VOP_ACCESS(vp, accmode, td->td_ucred, td);
4445 if (fmode & O_TRUNC) {
4447 VOP_UNLOCK(vp, 0); /* XXX */
4448 if ((error = vn_start_write(NULL, &mp, V_WAIT | PCATCH)) != 0) {
4453 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); /* XXX */
4457 * We don't yet have fp->f_cred, so use td->td_ucred, which
4460 error = mac_vnode_check_write(td->td_ucred, td->td_ucred, vp);
4465 error = VOP_SETATTR(vp, vap, td->td_ucred);
4469 vn_finished_write(mp);
4473 error = VOP_OPEN(vp, fmode, td->td_ucred, td, NULL);
4481 * end of vn_open code
4484 if ((error = fallocf(td, &nfp, &indx, fmode)) != 0) {
4489 /* An extra reference on `nfp' has been held for us by falloc(). */
4492 finit(nfp, fmode & FMASK, DTYPE_VNODE, vp, &vnops);
4493 if (fmode & (O_EXLOCK | O_SHLOCK)) {
4494 lf.l_whence = SEEK_SET;
4497 if (fmode & O_EXLOCK)
4498 lf.l_type = F_WRLCK;
4500 lf.l_type = F_RDLCK;
4502 if ((fmode & FNONBLOCK) == 0)
4505 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf,
4508 * The lock request failed. Normally close the
4509 * descriptor but handle the case where someone might
4510 * have dup()d or close()d it when we weren't looking.
4512 fdclose(fdp, fp, indx, td);
4515 * release our private reference
4520 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
4521 atomic_set_int(&fp->f_flag, FHASLOCK);
4526 VFS_UNLOCK_GIANT(vfslocked);
4527 td->td_retval[0] = indx;
4533 VFS_UNLOCK_GIANT(vfslocked);
4538 * Stat an (NFS) file handle.
4540 #ifndef _SYS_SYSPROTO_H_
4541 struct fhstat_args {
4542 struct fhandle *u_fhp;
4549 register struct fhstat_args /* {
4550 struct fhandle *u_fhp;
4561 error = priv_check(td, PRIV_VFS_FHSTAT);
4564 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4567 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4569 vfslocked = VFS_LOCK_GIANT(mp);
4570 error = VFS_FHTOVP(mp, &fh.fh_fid, &vp);
4573 VFS_UNLOCK_GIANT(vfslocked);
4576 error = vn_stat(vp, &sb, td->td_ucred, NOCRED, td);
4578 VFS_UNLOCK_GIANT(vfslocked);
4581 error = copyout(&sb, uap->sb, sizeof(sb));
4586 * Implement fstatfs() for (NFS) file handles.
4588 #ifndef _SYS_SYSPROTO_H_
4589 struct fhstatfs_args {
4590 struct fhandle *u_fhp;
4597 struct fhstatfs_args /* {
4598 struct fhandle *u_fhp;
4606 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4609 error = kern_fhstatfs(td, fh, &sf);
4612 return (copyout(&sf, uap->buf, sizeof(sf)));
4616 kern_fhstatfs(struct thread *td, fhandle_t fh, struct statfs *buf)
4624 error = priv_check(td, PRIV_VFS_FHSTATFS);
4627 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4629 vfslocked = VFS_LOCK_GIANT(mp);
4630 error = VFS_FHTOVP(mp, &fh.fh_fid, &vp);
4633 VFS_UNLOCK_GIANT(vfslocked);
4637 error = prison_canseemount(td->td_ucred, mp);
4641 error = mac_mount_check_stat(td->td_ucred, mp);
4646 * Set these in case the underlying filesystem fails to do so.
4649 sp->f_version = STATFS_VERSION;
4650 sp->f_namemax = NAME_MAX;
4651 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
4652 error = VFS_STATFS(mp, sp);
4657 VFS_UNLOCK_GIANT(vfslocked);
4662 kern_posix_fallocate(struct thread *td, int fd, off_t offset, off_t len)
4667 off_t olen, ooffset;
4668 int error, vfslocked;
4672 error = fget(td, fd, &fp);
4676 switch (fp->f_type) {
4687 if ((fp->f_flag & FWRITE) == 0) {
4692 if (vp->v_type != VREG) {
4696 if (offset < 0 || len <= 0) {
4700 /* Check for wrap. */
4701 if (offset > OFF_MAX - len) {
4706 /* Allocating blocks may take a long time, so iterate. */
4712 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
4714 error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
4716 VFS_UNLOCK_GIANT(vfslocked);
4719 error = vn_lock(vp, LK_EXCLUSIVE);
4721 vn_finished_write(mp);
4722 VFS_UNLOCK_GIANT(vfslocked);
4726 error = mac_vnode_check_write(td->td_ucred, fp->f_cred, vp);
4729 error = VOP_ALLOCATE(vp, &offset, &len);
4731 vn_finished_write(mp);
4732 VFS_UNLOCK_GIANT(vfslocked);
4734 if (olen + ooffset != offset + len) {
4735 panic("offset + len changed from %jx/%jx to %jx/%jx",
4736 ooffset, olen, offset, len);
4738 if (error != 0 || len == 0)
4740 KASSERT(olen > len, ("Iteration did not make progress?"));
4750 posix_fallocate(struct thread *td, struct posix_fallocate_args *uap)
4753 return (kern_posix_fallocate(td, uap->fd, uap->offset, uap->len));
4757 * Unlike madvise(2), we do not make a best effort to remember every
4758 * possible caching hint. Instead, we remember the last setting with
4759 * the exception that we will allow POSIX_FADV_NORMAL to adjust the
4760 * region of any current setting.
4763 kern_posix_fadvise(struct thread *td, int fd, off_t offset, off_t len,
4766 struct fadvise_info *fa, *new;
4772 if (offset < 0 || len < 0 || offset > OFF_MAX - len)
4775 case POSIX_FADV_SEQUENTIAL:
4776 case POSIX_FADV_RANDOM:
4777 case POSIX_FADV_NOREUSE:
4778 new = malloc(sizeof(*fa), M_FADVISE, M_WAITOK);
4780 case POSIX_FADV_NORMAL:
4781 case POSIX_FADV_WILLNEED:
4782 case POSIX_FADV_DONTNEED:
4788 error = fget(td, fd, &fp);
4792 switch (fp->f_type) {
4804 if (vp->v_type != VREG) {
4811 end = offset + len - 1;
4813 case POSIX_FADV_SEQUENTIAL:
4814 case POSIX_FADV_RANDOM:
4815 case POSIX_FADV_NOREUSE:
4817 * Try to merge any existing non-standard region with
4818 * this new region if possible, otherwise create a new
4819 * non-standard region for this request.
4821 mtx_pool_lock(mtxpool_sleep, fp);
4823 if (fa != NULL && fa->fa_advice == advice &&
4824 ((fa->fa_start <= end && fa->fa_end >= offset) ||
4825 (end != OFF_MAX && fa->fa_start == end + 1) ||
4826 (fa->fa_end != OFF_MAX && fa->fa_end + 1 == offset))) {
4827 if (offset < fa->fa_start)
4828 fa->fa_start = offset;
4829 if (end > fa->fa_end)
4832 new->fa_advice = advice;
4833 new->fa_start = offset;
4838 mtx_pool_unlock(mtxpool_sleep, fp);
4840 case POSIX_FADV_NORMAL:
4842 * If a the "normal" region overlaps with an existing
4843 * non-standard region, trim or remove the
4844 * non-standard region.
4846 mtx_pool_lock(mtxpool_sleep, fp);
4849 if (offset <= fa->fa_start && end >= fa->fa_end) {
4851 fp->f_advice = NULL;
4852 } else if (offset <= fa->fa_start &&
4853 end >= fa->fa_start)
4854 fa->fa_start = end + 1;
4855 else if (offset <= fa->fa_end && end >= fa->fa_end)
4856 fa->fa_end = offset - 1;
4857 else if (offset >= fa->fa_start && end <= fa->fa_end) {
4859 * If the "normal" region is a middle
4860 * portion of the existing
4861 * non-standard region, just remove
4862 * the whole thing rather than picking
4863 * one side or the other to
4867 fp->f_advice = NULL;
4870 mtx_pool_unlock(mtxpool_sleep, fp);
4872 case POSIX_FADV_WILLNEED:
4873 case POSIX_FADV_DONTNEED:
4874 error = VOP_ADVISE(vp, offset, end, advice);
4880 free(new, M_FADVISE);
4885 posix_fadvise(struct thread *td, struct posix_fadvise_args *uap)
4888 return (kern_posix_fadvise(td, uap->fd, uap->offset, uap->len,