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 static MALLOC_DEFINE(M_FADVISE, "fadvise", "posix_fadvise(2) information");
91 SDT_PROVIDER_DEFINE(vfs);
92 SDT_PROBE_DEFINE(vfs, , stat, mode, mode);
93 SDT_PROBE_ARGTYPE(vfs, , stat, mode, 0, "char *");
94 SDT_PROBE_ARGTYPE(vfs, , stat, mode, 1, "int");
95 SDT_PROBE_DEFINE(vfs, , stat, reg, reg);
96 SDT_PROBE_ARGTYPE(vfs, , stat, reg, 0, "char *");
97 SDT_PROBE_ARGTYPE(vfs, , stat, reg, 1, "int");
99 static int chroot_refuse_vdir_fds(struct filedesc *fdp);
100 static int getutimes(const struct timeval *, enum uio_seg, struct timespec *);
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 {
181 sys_quotactl(td, uap)
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 {
442 sys_getfsstat(td, uap)
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,
884 "Allow a process to chroot(2) if it has a directory open");
887 * Change notion of root (``/'') directory.
889 #ifndef _SYS_SYSPROTO_H_
897 struct chroot_args /* {
905 error = priv_check(td, PRIV_VFS_CHROOT);
908 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | MPSAFE |
909 AUDITVNODE1, UIO_USERSPACE, uap->path, td);
913 vfslocked = NDHASGIANT(&nd);
914 if ((error = change_dir(nd.ni_vp, td)) != 0)
917 if ((error = mac_vnode_check_chroot(td->td_ucred, nd.ni_vp)))
920 VOP_UNLOCK(nd.ni_vp, 0);
921 error = change_root(nd.ni_vp, td);
923 VFS_UNLOCK_GIANT(vfslocked);
924 NDFREE(&nd, NDF_ONLY_PNBUF);
928 VFS_UNLOCK_GIANT(vfslocked);
930 NDFREE(&nd, NDF_ONLY_PNBUF);
935 * Common routine for chroot and chdir. Callers must provide a locked vnode
945 ASSERT_VOP_LOCKED(vp, "change_dir(): vp not locked");
946 if (vp->v_type != VDIR)
949 error = mac_vnode_check_chdir(td->td_ucred, vp);
953 error = VOP_ACCESS(vp, VEXEC, td->td_ucred, td);
958 * Common routine for kern_chroot() and jail_attach(). The caller is
959 * responsible for invoking priv_check() and mac_vnode_check_chroot() to
960 * authorize this operation.
967 struct filedesc *fdp;
972 VFS_ASSERT_GIANT(vp->v_mount);
973 fdp = td->td_proc->p_fd;
975 if (chroot_allow_open_directories == 0 ||
976 (chroot_allow_open_directories == 1 && fdp->fd_rdir != rootvnode)) {
977 error = chroot_refuse_vdir_fds(fdp);
979 FILEDESC_XUNLOCK(fdp);
983 oldvp = fdp->fd_rdir;
990 FILEDESC_XUNLOCK(fdp);
991 vfslocked = VFS_LOCK_GIANT(oldvp->v_mount);
993 VFS_UNLOCK_GIANT(vfslocked);
997 static __inline cap_rights_t
998 flags_to_rights(int flags)
1000 cap_rights_t rights = 0;
1002 switch ((flags & O_ACCMODE)) {
1012 rights |= CAP_WRITE;
1016 rights |= CAP_FEXECVE;
1020 if (flags & O_CREAT)
1021 rights |= CAP_CREATE;
1023 if (flags & O_TRUNC)
1024 rights |= CAP_FTRUNCATE;
1026 if ((flags & O_EXLOCK) || (flags & O_SHLOCK))
1027 rights |= CAP_FLOCK;
1033 * Check permissions, allocate an open file structure, and call the device
1034 * open routine if any.
1036 #ifndef _SYS_SYSPROTO_H_
1046 register struct open_args /* {
1053 return (kern_open(td, uap->path, UIO_USERSPACE, uap->flags, uap->mode));
1056 #ifndef _SYS_SYSPROTO_H_
1057 struct openat_args {
1065 sys_openat(struct thread *td, struct openat_args *uap)
1068 return (kern_openat(td, uap->fd, uap->path, UIO_USERSPACE, uap->flag,
1073 kern_open(struct thread *td, char *path, enum uio_seg pathseg, int flags,
1077 return (kern_openat(td, AT_FDCWD, path, pathseg, flags, mode));
1081 kern_openat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1082 int flags, int mode)
1084 struct proc *p = td->td_proc;
1085 struct filedesc *fdp = p->p_fd;
1090 int type, indx = -1, error, error_open;
1092 struct nameidata nd;
1094 cap_rights_t rights_needed = CAP_LOOKUP;
1096 AUDIT_ARG_FFLAGS(flags);
1097 AUDIT_ARG_MODE(mode);
1098 /* XXX: audit dirfd */
1099 rights_needed |= flags_to_rights(flags);
1101 * Only one of the O_EXEC, O_RDONLY, O_WRONLY and O_RDWR flags
1104 if (flags & O_EXEC) {
1105 if (flags & O_ACCMODE)
1107 } else if ((flags & O_ACCMODE) == O_ACCMODE)
1110 flags = FFLAGS(flags);
1113 * allocate the file descriptor, but don't install a descriptor yet
1115 error = falloc_noinstall(td, &nfp);
1118 /* An extra reference on `nfp' has been held for us by falloc_noinstall(). */
1120 /* Set the flags early so the finit in devfs can pick them up. */
1121 fp->f_flag = flags & FMASK;
1122 cmode = ((mode &~ fdp->fd_cmask) & ALLPERMS) &~ S_ISTXT;
1123 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | AUDITVNODE1 | MPSAFE, pathseg,
1124 path, fd, rights_needed, td);
1125 td->td_dupfd = -1; /* XXX check for fdopen */
1126 error = vn_open(&nd, &flags, cmode, fp);
1129 * If the vn_open replaced the method vector, something
1130 * wonderous happened deep below and we just pass it up
1131 * pretending we know what we do.
1133 if (error == ENXIO && fp->f_ops != &badfileops)
1137 * handle special fdopen() case. bleh. dupfdopen() is
1138 * responsible for dropping the old contents of ofiles[indx]
1141 * Don't do this for relative (capability) lookups; we don't
1142 * understand exactly what would happen, and we don't think
1143 * that it ever should.
1145 if ((nd.ni_strictrelative == 0) &&
1146 (error == ENODEV || error == ENXIO) &&
1147 (td->td_dupfd >= 0)) {
1148 /* XXX from fdopen */
1150 if ((error = finstall(td, fp, &indx, flags)) != 0)
1152 if ((error = dupfdopen(td, fdp, indx, td->td_dupfd,
1153 flags, error_open)) == 0)
1157 * Clean up the descriptor, but only if another thread hadn't
1158 * replaced or closed it.
1161 fdclose(fdp, fp, indx, td);
1164 if (error == ERESTART)
1169 vfslocked = NDHASGIANT(&nd);
1170 NDFREE(&nd, NDF_ONLY_PNBUF);
1174 * Store the vnode, for any f_type. Typically, the vnode use
1175 * count is decremented by direct call to vn_closefile() for
1176 * files that switched type in the cdevsw fdopen() method.
1180 * If the file wasn't claimed by devfs bind it to the normal
1181 * vnode operations here.
1183 if (fp->f_ops == &badfileops) {
1184 KASSERT(vp->v_type != VFIFO, ("Unexpected fifo."));
1186 finit(fp, flags & FMASK, DTYPE_VNODE, vp, &vnops);
1190 if (fp->f_type == DTYPE_VNODE && (flags & (O_EXLOCK | O_SHLOCK)) != 0) {
1191 lf.l_whence = SEEK_SET;
1194 if (flags & O_EXLOCK)
1195 lf.l_type = F_WRLCK;
1197 lf.l_type = F_RDLCK;
1199 if ((flags & FNONBLOCK) == 0)
1201 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf,
1204 atomic_set_int(&fp->f_flag, FHASLOCK);
1206 if (flags & O_TRUNC) {
1207 error = fo_truncate(fp, 0, td->td_ucred, td);
1211 VFS_UNLOCK_GIANT(vfslocked);
1214 * If we haven't already installed the FD (for dupfdopen), do so now.
1218 if (nd.ni_strictrelative == 1) {
1220 * We are doing a strict relative lookup; wrap the
1221 * result in a capability.
1223 if ((error = kern_capwrap(td, fp, nd.ni_baserights,
1228 if ((error = finstall(td, fp, &indx, flags)) != 0)
1234 * Release our private reference, leaving the one associated with
1235 * the descriptor table intact.
1238 td->td_retval[0] = indx;
1241 VFS_UNLOCK_GIANT(vfslocked);
1244 fdclose(fdp, fp, indx, td);
1246 td->td_retval[0] = -1;
1254 #ifndef _SYS_SYSPROTO_H_
1255 struct ocreat_args {
1263 register struct ocreat_args /* {
1269 return (kern_open(td, uap->path, UIO_USERSPACE,
1270 O_WRONLY | O_CREAT | O_TRUNC, uap->mode));
1272 #endif /* COMPAT_43 */
1275 * Create a special file.
1277 #ifndef _SYS_SYSPROTO_H_
1287 register struct mknod_args /* {
1294 return (kern_mknod(td, uap->path, UIO_USERSPACE, uap->mode, uap->dev));
1297 #ifndef _SYS_SYSPROTO_H_
1298 struct mknodat_args {
1306 sys_mknodat(struct thread *td, struct mknodat_args *uap)
1309 return (kern_mknodat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode,
1314 kern_mknod(struct thread *td, char *path, enum uio_seg pathseg, int mode,
1318 return (kern_mknodat(td, AT_FDCWD, path, pathseg, mode, dev));
1322 kern_mknodat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1330 struct nameidata nd;
1333 AUDIT_ARG_MODE(mode);
1335 switch (mode & S_IFMT) {
1338 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
1341 error = priv_check(td, PRIV_VFS_MKNOD_BAD);
1344 error = priv_check(td, PRIV_VFS_MKNOD_WHT);
1348 return (kern_mkfifoat(td, fd, path, pathseg, mode));
1358 NDINIT_ATRIGHTS(&nd, CREATE,
1359 LOCKPARENT | SAVENAME | MPSAFE | AUDITVNODE1, pathseg, path, fd,
1361 if ((error = namei(&nd)) != 0)
1363 vfslocked = NDHASGIANT(&nd);
1366 NDFREE(&nd, NDF_ONLY_PNBUF);
1367 if (vp == nd.ni_dvp)
1372 VFS_UNLOCK_GIANT(vfslocked);
1376 vattr.va_mode = (mode & ALLPERMS) &
1377 ~td->td_proc->p_fd->fd_cmask;
1378 vattr.va_rdev = dev;
1381 switch (mode & S_IFMT) {
1382 case S_IFMT: /* used by badsect to flag bad sectors */
1383 vattr.va_type = VBAD;
1386 vattr.va_type = VCHR;
1389 vattr.va_type = VBLK;
1395 panic("kern_mknod: invalid mode");
1398 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1399 NDFREE(&nd, NDF_ONLY_PNBUF);
1401 VFS_UNLOCK_GIANT(vfslocked);
1402 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1407 if (error == 0 && !whiteout)
1408 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp,
1409 &nd.ni_cnd, &vattr);
1413 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, CREATE);
1415 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp,
1416 &nd.ni_cnd, &vattr);
1421 NDFREE(&nd, NDF_ONLY_PNBUF);
1423 vn_finished_write(mp);
1424 VFS_UNLOCK_GIANT(vfslocked);
1429 * Create a named pipe.
1431 #ifndef _SYS_SYSPROTO_H_
1432 struct mkfifo_args {
1440 register struct mkfifo_args /* {
1446 return (kern_mkfifo(td, uap->path, UIO_USERSPACE, uap->mode));
1449 #ifndef _SYS_SYSPROTO_H_
1450 struct mkfifoat_args {
1457 sys_mkfifoat(struct thread *td, struct mkfifoat_args *uap)
1460 return (kern_mkfifoat(td, uap->fd, uap->path, UIO_USERSPACE,
1465 kern_mkfifo(struct thread *td, char *path, enum uio_seg pathseg, int mode)
1468 return (kern_mkfifoat(td, AT_FDCWD, path, pathseg, mode));
1472 kern_mkfifoat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1478 struct nameidata nd;
1481 AUDIT_ARG_MODE(mode);
1484 NDINIT_AT(&nd, CREATE, LOCKPARENT | SAVENAME | MPSAFE | AUDITVNODE1,
1485 pathseg, path, fd, td);
1486 if ((error = namei(&nd)) != 0)
1488 vfslocked = NDHASGIANT(&nd);
1489 if (nd.ni_vp != NULL) {
1490 NDFREE(&nd, NDF_ONLY_PNBUF);
1491 if (nd.ni_vp == nd.ni_dvp)
1496 VFS_UNLOCK_GIANT(vfslocked);
1499 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1500 NDFREE(&nd, NDF_ONLY_PNBUF);
1502 VFS_UNLOCK_GIANT(vfslocked);
1503 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1508 vattr.va_type = VFIFO;
1509 vattr.va_mode = (mode & ALLPERMS) & ~td->td_proc->p_fd->fd_cmask;
1511 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1516 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
1523 vn_finished_write(mp);
1524 VFS_UNLOCK_GIANT(vfslocked);
1525 NDFREE(&nd, NDF_ONLY_PNBUF);
1530 * Make a hard file link.
1532 #ifndef _SYS_SYSPROTO_H_
1541 register struct link_args /* {
1547 return (kern_link(td, uap->path, uap->link, UIO_USERSPACE));
1550 #ifndef _SYS_SYSPROTO_H_
1551 struct linkat_args {
1560 sys_linkat(struct thread *td, struct linkat_args *uap)
1565 if (flag & ~AT_SYMLINK_FOLLOW)
1568 return (kern_linkat(td, uap->fd1, uap->fd2, uap->path1, uap->path2,
1569 UIO_USERSPACE, (flag & AT_SYMLINK_FOLLOW) ? FOLLOW : NOFOLLOW));
1572 int hardlink_check_uid = 0;
1573 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
1574 &hardlink_check_uid, 0,
1575 "Unprivileged processes cannot create hard links to files owned by other "
1577 static int hardlink_check_gid = 0;
1578 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
1579 &hardlink_check_gid, 0,
1580 "Unprivileged processes cannot create hard links to files owned by other "
1584 can_hardlink(struct vnode *vp, struct ucred *cred)
1589 if (!hardlink_check_uid && !hardlink_check_gid)
1592 error = VOP_GETATTR(vp, &va, cred);
1596 if (hardlink_check_uid && cred->cr_uid != va.va_uid) {
1597 error = priv_check_cred(cred, PRIV_VFS_LINK, 0);
1602 if (hardlink_check_gid && !groupmember(va.va_gid, cred)) {
1603 error = priv_check_cred(cred, PRIV_VFS_LINK, 0);
1612 kern_link(struct thread *td, char *path, char *link, enum uio_seg segflg)
1615 return (kern_linkat(td, AT_FDCWD, AT_FDCWD, path,link, segflg, FOLLOW));
1619 kern_linkat(struct thread *td, int fd1, int fd2, char *path1, char *path2,
1620 enum uio_seg segflg, int follow)
1624 struct nameidata nd;
1630 NDINIT_AT(&nd, LOOKUP, follow | MPSAFE | AUDITVNODE1, segflg, path1,
1633 if ((error = namei(&nd)) != 0)
1635 vfslocked = NDHASGIANT(&nd);
1636 NDFREE(&nd, NDF_ONLY_PNBUF);
1638 if (vp->v_type == VDIR) {
1640 VFS_UNLOCK_GIANT(vfslocked);
1641 return (EPERM); /* POSIX */
1643 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0) {
1645 VFS_UNLOCK_GIANT(vfslocked);
1648 NDINIT_AT(&nd, CREATE, LOCKPARENT | SAVENAME | MPSAFE | AUDITVNODE2,
1649 segflg, path2, fd2, td);
1650 if ((error = namei(&nd)) == 0) {
1651 lvfslocked = NDHASGIANT(&nd);
1652 if (nd.ni_vp != NULL) {
1653 if (nd.ni_dvp == nd.ni_vp)
1659 } else if ((error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY))
1661 error = can_hardlink(vp, td->td_ucred);
1664 error = mac_vnode_check_link(td->td_ucred,
1665 nd.ni_dvp, vp, &nd.ni_cnd);
1668 error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
1672 NDFREE(&nd, NDF_ONLY_PNBUF);
1673 VFS_UNLOCK_GIANT(lvfslocked);
1676 vn_finished_write(mp);
1677 VFS_UNLOCK_GIANT(vfslocked);
1682 * Make a symbolic link.
1684 #ifndef _SYS_SYSPROTO_H_
1685 struct symlink_args {
1691 sys_symlink(td, uap)
1693 register struct symlink_args /* {
1699 return (kern_symlink(td, uap->path, uap->link, UIO_USERSPACE));
1702 #ifndef _SYS_SYSPROTO_H_
1703 struct symlinkat_args {
1710 sys_symlinkat(struct thread *td, struct symlinkat_args *uap)
1713 return (kern_symlinkat(td, uap->path1, uap->fd, uap->path2,
1718 kern_symlink(struct thread *td, char *path, char *link, enum uio_seg segflg)
1721 return (kern_symlinkat(td, path, AT_FDCWD, link, segflg));
1725 kern_symlinkat(struct thread *td, char *path1, int fd, char *path2,
1726 enum uio_seg segflg)
1732 struct nameidata nd;
1735 if (segflg == UIO_SYSSPACE) {
1738 syspath = uma_zalloc(namei_zone, M_WAITOK);
1739 if ((error = copyinstr(path1, syspath, MAXPATHLEN, NULL)) != 0)
1742 AUDIT_ARG_TEXT(syspath);
1745 NDINIT_AT(&nd, CREATE, LOCKPARENT | SAVENAME | MPSAFE | AUDITVNODE1,
1746 segflg, path2, fd, td);
1747 if ((error = namei(&nd)) != 0)
1749 vfslocked = NDHASGIANT(&nd);
1751 NDFREE(&nd, NDF_ONLY_PNBUF);
1752 if (nd.ni_vp == nd.ni_dvp)
1757 VFS_UNLOCK_GIANT(vfslocked);
1761 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1762 NDFREE(&nd, NDF_ONLY_PNBUF);
1764 VFS_UNLOCK_GIANT(vfslocked);
1765 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1770 vattr.va_mode = ACCESSPERMS &~ td->td_proc->p_fd->fd_cmask;
1772 vattr.va_type = VLNK;
1773 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1778 error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr, syspath);
1784 NDFREE(&nd, NDF_ONLY_PNBUF);
1786 vn_finished_write(mp);
1787 VFS_UNLOCK_GIANT(vfslocked);
1789 if (segflg != UIO_SYSSPACE)
1790 uma_zfree(namei_zone, syspath);
1795 * Delete a whiteout from the filesystem.
1798 sys_undelete(td, uap)
1800 register struct undelete_args /* {
1806 struct nameidata nd;
1811 NDINIT(&nd, DELETE, LOCKPARENT | DOWHITEOUT | MPSAFE | AUDITVNODE1,
1812 UIO_USERSPACE, uap->path, td);
1816 vfslocked = NDHASGIANT(&nd);
1818 if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & ISWHITEOUT)) {
1819 NDFREE(&nd, NDF_ONLY_PNBUF);
1820 if (nd.ni_vp == nd.ni_dvp)
1826 VFS_UNLOCK_GIANT(vfslocked);
1829 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1830 NDFREE(&nd, NDF_ONLY_PNBUF);
1832 VFS_UNLOCK_GIANT(vfslocked);
1833 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1837 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, DELETE);
1838 NDFREE(&nd, NDF_ONLY_PNBUF);
1840 vn_finished_write(mp);
1841 VFS_UNLOCK_GIANT(vfslocked);
1846 * Delete a name from the filesystem.
1848 #ifndef _SYS_SYSPROTO_H_
1849 struct unlink_args {
1856 struct unlink_args /* {
1861 return (kern_unlink(td, uap->path, UIO_USERSPACE));
1864 #ifndef _SYS_SYSPROTO_H_
1865 struct unlinkat_args {
1872 sys_unlinkat(struct thread *td, struct unlinkat_args *uap)
1874 int flag = uap->flag;
1876 char *path = uap->path;
1878 if (flag & ~AT_REMOVEDIR)
1881 if (flag & AT_REMOVEDIR)
1882 return (kern_rmdirat(td, fd, path, UIO_USERSPACE));
1884 return (kern_unlinkat(td, fd, path, UIO_USERSPACE, 0));
1888 kern_unlink(struct thread *td, char *path, enum uio_seg pathseg)
1891 return (kern_unlinkat(td, AT_FDCWD, path, pathseg, 0));
1895 kern_unlinkat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1901 struct nameidata nd;
1907 NDINIT_AT(&nd, DELETE, LOCKPARENT | LOCKLEAF | MPSAFE | AUDITVNODE1,
1908 pathseg, path, fd, td);
1909 if ((error = namei(&nd)) != 0)
1910 return (error == EINVAL ? EPERM : error);
1911 vfslocked = NDHASGIANT(&nd);
1913 if (vp->v_type == VDIR && oldinum == 0) {
1914 error = EPERM; /* POSIX */
1915 } else if (oldinum != 0 &&
1916 ((error = vn_stat(vp, &sb, td->td_ucred, NOCRED, td)) == 0) &&
1917 sb.st_ino != oldinum) {
1918 error = EIDRM; /* Identifier removed */
1921 * The root of a mounted filesystem cannot be deleted.
1923 * XXX: can this only be a VDIR case?
1925 if (vp->v_vflag & VV_ROOT)
1929 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1930 NDFREE(&nd, NDF_ONLY_PNBUF);
1932 if (vp == nd.ni_dvp)
1936 VFS_UNLOCK_GIANT(vfslocked);
1937 if ((error = vn_start_write(NULL, &mp,
1938 V_XSLEEP | PCATCH)) != 0)
1943 error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp,
1948 error = VOP_REMOVE(nd.ni_dvp, vp, &nd.ni_cnd);
1952 vn_finished_write(mp);
1954 NDFREE(&nd, NDF_ONLY_PNBUF);
1956 if (vp == nd.ni_dvp)
1960 VFS_UNLOCK_GIANT(vfslocked);
1965 * Reposition read/write file offset.
1967 #ifndef _SYS_SYSPROTO_H_
1978 register struct lseek_args /* {
1985 struct ucred *cred = td->td_ucred;
1993 AUDIT_ARG_FD(uap->fd);
1994 if ((error = fget(td, uap->fd, CAP_SEEK, &fp)) != 0)
1996 if (!(fp->f_ops->fo_flags & DFLAG_SEEKABLE)) {
2001 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
2002 noneg = (vp->v_type != VCHR);
2003 offset = uap->offset;
2004 switch (uap->whence) {
2007 (fp->f_offset < 0 ||
2008 (offset > 0 && fp->f_offset > OFF_MAX - offset))) {
2012 offset += fp->f_offset;
2015 vn_lock(vp, LK_SHARED | LK_RETRY);
2016 error = VOP_GETATTR(vp, &vattr, cred);
2022 * If the file references a disk device, then fetch
2023 * the media size and use that to determine the ending
2026 if (vattr.va_size == 0 && vp->v_type == VCHR &&
2027 fo_ioctl(fp, DIOCGMEDIASIZE, &size, cred, td) == 0)
2028 vattr.va_size = size;
2030 (vattr.va_size > OFF_MAX ||
2031 (offset > 0 && vattr.va_size > OFF_MAX - offset))) {
2035 offset += vattr.va_size;
2040 error = fo_ioctl(fp, FIOSEEKDATA, &offset, cred, td);
2043 error = fo_ioctl(fp, FIOSEEKHOLE, &offset, cred, td);
2048 if (error == 0 && noneg && offset < 0)
2052 fp->f_offset = offset;
2053 VFS_KNOTE_UNLOCKED(vp, 0);
2054 *(off_t *)(td->td_retval) = fp->f_offset;
2057 VFS_UNLOCK_GIANT(vfslocked);
2061 #if defined(COMPAT_43)
2063 * Reposition read/write file offset.
2065 #ifndef _SYS_SYSPROTO_H_
2066 struct olseek_args {
2075 register struct olseek_args /* {
2081 struct lseek_args /* {
2089 nuap.offset = uap->offset;
2090 nuap.whence = uap->whence;
2091 return (sys_lseek(td, &nuap));
2093 #endif /* COMPAT_43 */
2095 /* Version with the 'pad' argument */
2097 freebsd6_lseek(td, uap)
2099 register struct freebsd6_lseek_args *uap;
2101 struct lseek_args ouap;
2104 ouap.offset = uap->offset;
2105 ouap.whence = uap->whence;
2106 return (sys_lseek(td, &ouap));
2110 * Check access permissions using passed credentials.
2113 vn_access(vp, user_flags, cred, td)
2122 /* Flags == 0 means only check for existence. */
2126 if (user_flags & R_OK)
2128 if (user_flags & W_OK)
2130 if (user_flags & X_OK)
2133 error = mac_vnode_check_access(cred, vp, accmode);
2137 if ((accmode & VWRITE) == 0 || (error = vn_writechk(vp)) == 0)
2138 error = VOP_ACCESS(vp, accmode, cred, td);
2144 * Check access permissions using "real" credentials.
2146 #ifndef _SYS_SYSPROTO_H_
2147 struct access_args {
2155 register struct access_args /* {
2161 return (kern_access(td, uap->path, UIO_USERSPACE, uap->amode));
2164 #ifndef _SYS_SYSPROTO_H_
2165 struct faccessat_args {
2173 sys_faccessat(struct thread *td, struct faccessat_args *uap)
2176 if (uap->flag & ~AT_EACCESS)
2178 return (kern_accessat(td, uap->fd, uap->path, UIO_USERSPACE, uap->flag,
2183 kern_access(struct thread *td, char *path, enum uio_seg pathseg, int amode)
2186 return (kern_accessat(td, AT_FDCWD, path, pathseg, 0, amode));
2190 kern_accessat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2191 int flag, int amode)
2193 struct ucred *cred, *tmpcred;
2195 struct nameidata nd;
2200 * Create and modify a temporary credential instead of one that
2201 * is potentially shared.
2203 if (!(flag & AT_EACCESS)) {
2204 cred = td->td_ucred;
2205 tmpcred = crdup(cred);
2206 tmpcred->cr_uid = cred->cr_ruid;
2207 tmpcred->cr_groups[0] = cred->cr_rgid;
2208 td->td_ucred = tmpcred;
2210 cred = tmpcred = td->td_ucred;
2211 AUDIT_ARG_VALUE(amode);
2212 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | MPSAFE |
2213 AUDITVNODE1, pathseg, path, fd, CAP_FSTAT, td);
2214 if ((error = namei(&nd)) != 0)
2216 vfslocked = NDHASGIANT(&nd);
2219 error = vn_access(vp, amode, tmpcred, td);
2220 NDFREE(&nd, NDF_ONLY_PNBUF);
2222 VFS_UNLOCK_GIANT(vfslocked);
2224 if (!(flag & AT_EACCESS)) {
2225 td->td_ucred = cred;
2232 * Check access permissions using "effective" credentials.
2234 #ifndef _SYS_SYSPROTO_H_
2235 struct eaccess_args {
2241 sys_eaccess(td, uap)
2243 register struct eaccess_args /* {
2249 return (kern_eaccess(td, uap->path, UIO_USERSPACE, uap->amode));
2253 kern_eaccess(struct thread *td, char *path, enum uio_seg pathseg, int amode)
2256 return (kern_accessat(td, AT_FDCWD, path, pathseg, AT_EACCESS, amode));
2259 #if defined(COMPAT_43)
2261 * Get file status; this version follows links.
2263 #ifndef _SYS_SYSPROTO_H_
2272 register struct ostat_args /* {
2281 error = kern_stat(td, uap->path, UIO_USERSPACE, &sb);
2285 error = copyout(&osb, uap->ub, sizeof (osb));
2290 * Get file status; this version does not follow links.
2292 #ifndef _SYS_SYSPROTO_H_
2293 struct olstat_args {
2301 register struct olstat_args /* {
2310 error = kern_lstat(td, uap->path, UIO_USERSPACE, &sb);
2314 error = copyout(&osb, uap->ub, sizeof (osb));
2319 * Convert from an old to a new stat structure.
2327 ost->st_dev = st->st_dev;
2328 ost->st_ino = st->st_ino;
2329 ost->st_mode = st->st_mode;
2330 ost->st_nlink = st->st_nlink;
2331 ost->st_uid = st->st_uid;
2332 ost->st_gid = st->st_gid;
2333 ost->st_rdev = st->st_rdev;
2334 if (st->st_size < (quad_t)1 << 32)
2335 ost->st_size = st->st_size;
2338 ost->st_atim = st->st_atim;
2339 ost->st_mtim = st->st_mtim;
2340 ost->st_ctim = st->st_ctim;
2341 ost->st_blksize = st->st_blksize;
2342 ost->st_blocks = st->st_blocks;
2343 ost->st_flags = st->st_flags;
2344 ost->st_gen = st->st_gen;
2346 #endif /* COMPAT_43 */
2349 * Get file status; this version follows links.
2351 #ifndef _SYS_SYSPROTO_H_
2360 register struct stat_args /* {
2368 error = kern_stat(td, uap->path, UIO_USERSPACE, &sb);
2370 error = copyout(&sb, uap->ub, sizeof (sb));
2374 #ifndef _SYS_SYSPROTO_H_
2375 struct fstatat_args {
2383 sys_fstatat(struct thread *td, struct fstatat_args *uap)
2388 error = kern_statat(td, uap->flag, uap->fd, uap->path,
2389 UIO_USERSPACE, &sb);
2391 error = copyout(&sb, uap->buf, sizeof (sb));
2396 kern_stat(struct thread *td, char *path, enum uio_seg pathseg, struct stat *sbp)
2399 return (kern_statat(td, 0, AT_FDCWD, path, pathseg, sbp));
2403 kern_statat(struct thread *td, int flag, int fd, char *path,
2404 enum uio_seg pathseg, struct stat *sbp)
2407 return (kern_statat_vnhook(td, flag, fd, path, pathseg, sbp, NULL));
2411 kern_statat_vnhook(struct thread *td, int flag, int fd, char *path,
2412 enum uio_seg pathseg, struct stat *sbp,
2413 void (*hook)(struct vnode *vp, struct stat *sbp))
2415 struct nameidata nd;
2417 int error, vfslocked;
2419 if (flag & ~AT_SYMLINK_NOFOLLOW)
2422 NDINIT_ATRIGHTS(&nd, LOOKUP, ((flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW :
2423 FOLLOW) | LOCKSHARED | LOCKLEAF | AUDITVNODE1 | MPSAFE, pathseg,
2424 path, fd, CAP_FSTAT, td);
2426 if ((error = namei(&nd)) != 0)
2428 vfslocked = NDHASGIANT(&nd);
2429 error = vn_stat(nd.ni_vp, &sb, td->td_ucred, NOCRED, td);
2431 SDT_PROBE(vfs, , stat, mode, path, sb.st_mode, 0, 0, 0);
2432 if (S_ISREG(sb.st_mode))
2433 SDT_PROBE(vfs, , stat, reg, path, pathseg, 0, 0, 0);
2434 if (__predict_false(hook != NULL))
2435 hook(nd.ni_vp, &sb);
2437 NDFREE(&nd, NDF_ONLY_PNBUF);
2439 VFS_UNLOCK_GIANT(vfslocked);
2444 if (KTRPOINT(td, KTR_STRUCT))
2451 * Get file status; this version does not follow links.
2453 #ifndef _SYS_SYSPROTO_H_
2462 register struct lstat_args /* {
2470 error = kern_lstat(td, uap->path, UIO_USERSPACE, &sb);
2472 error = copyout(&sb, uap->ub, sizeof (sb));
2477 kern_lstat(struct thread *td, char *path, enum uio_seg pathseg, struct stat *sbp)
2480 return (kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, path, pathseg,
2485 * Implementation of the NetBSD [l]stat() functions.
2492 bzero(nsb, sizeof *nsb);
2493 nsb->st_dev = sb->st_dev;
2494 nsb->st_ino = sb->st_ino;
2495 nsb->st_mode = sb->st_mode;
2496 nsb->st_nlink = sb->st_nlink;
2497 nsb->st_uid = sb->st_uid;
2498 nsb->st_gid = sb->st_gid;
2499 nsb->st_rdev = sb->st_rdev;
2500 nsb->st_atim = sb->st_atim;
2501 nsb->st_mtim = sb->st_mtim;
2502 nsb->st_ctim = sb->st_ctim;
2503 nsb->st_size = sb->st_size;
2504 nsb->st_blocks = sb->st_blocks;
2505 nsb->st_blksize = sb->st_blksize;
2506 nsb->st_flags = sb->st_flags;
2507 nsb->st_gen = sb->st_gen;
2508 nsb->st_birthtim = sb->st_birthtim;
2511 #ifndef _SYS_SYSPROTO_H_
2520 register struct nstat_args /* {
2529 error = kern_stat(td, uap->path, UIO_USERSPACE, &sb);
2532 cvtnstat(&sb, &nsb);
2533 error = copyout(&nsb, uap->ub, sizeof (nsb));
2538 * NetBSD lstat. Get file status; this version does not follow links.
2540 #ifndef _SYS_SYSPROTO_H_
2549 register struct nlstat_args /* {
2558 error = kern_lstat(td, uap->path, UIO_USERSPACE, &sb);
2561 cvtnstat(&sb, &nsb);
2562 error = copyout(&nsb, uap->ub, sizeof (nsb));
2567 * Get configurable pathname variables.
2569 #ifndef _SYS_SYSPROTO_H_
2570 struct pathconf_args {
2576 sys_pathconf(td, uap)
2578 register struct pathconf_args /* {
2584 return (kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name, FOLLOW));
2587 #ifndef _SYS_SYSPROTO_H_
2588 struct lpathconf_args {
2594 sys_lpathconf(td, uap)
2596 register struct lpathconf_args /* {
2602 return (kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name, NOFOLLOW));
2606 kern_pathconf(struct thread *td, char *path, enum uio_seg pathseg, int name,
2609 struct nameidata nd;
2610 int error, vfslocked;
2612 NDINIT(&nd, LOOKUP, LOCKSHARED | LOCKLEAF | MPSAFE | AUDITVNODE1 |
2613 flags, pathseg, path, td);
2614 if ((error = namei(&nd)) != 0)
2616 vfslocked = NDHASGIANT(&nd);
2617 NDFREE(&nd, NDF_ONLY_PNBUF);
2619 /* If asynchronous I/O is available, it works for all files. */
2620 if (name == _PC_ASYNC_IO)
2621 td->td_retval[0] = async_io_version;
2623 error = VOP_PATHCONF(nd.ni_vp, name, td->td_retval);
2625 VFS_UNLOCK_GIANT(vfslocked);
2630 * Return target name of a symbolic link.
2632 #ifndef _SYS_SYSPROTO_H_
2633 struct readlink_args {
2640 sys_readlink(td, uap)
2642 register struct readlink_args /* {
2649 return (kern_readlink(td, uap->path, UIO_USERSPACE, uap->buf,
2650 UIO_USERSPACE, uap->count));
2652 #ifndef _SYS_SYSPROTO_H_
2653 struct readlinkat_args {
2661 sys_readlinkat(struct thread *td, struct readlinkat_args *uap)
2664 return (kern_readlinkat(td, uap->fd, uap->path, UIO_USERSPACE,
2665 uap->buf, UIO_USERSPACE, uap->bufsize));
2669 kern_readlink(struct thread *td, char *path, enum uio_seg pathseg, char *buf,
2670 enum uio_seg bufseg, size_t count)
2673 return (kern_readlinkat(td, AT_FDCWD, path, pathseg, buf, bufseg,
2678 kern_readlinkat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2679 char *buf, enum uio_seg bufseg, size_t count)
2685 struct nameidata nd;
2688 if (count > INT_MAX)
2691 NDINIT_AT(&nd, LOOKUP, NOFOLLOW | LOCKSHARED | LOCKLEAF | MPSAFE |
2692 AUDITVNODE1, pathseg, path, fd, td);
2694 if ((error = namei(&nd)) != 0)
2696 NDFREE(&nd, NDF_ONLY_PNBUF);
2697 vfslocked = NDHASGIANT(&nd);
2700 error = mac_vnode_check_readlink(td->td_ucred, vp);
2703 VFS_UNLOCK_GIANT(vfslocked);
2707 if (vp->v_type != VLNK)
2710 aiov.iov_base = buf;
2711 aiov.iov_len = count;
2712 auio.uio_iov = &aiov;
2713 auio.uio_iovcnt = 1;
2714 auio.uio_offset = 0;
2715 auio.uio_rw = UIO_READ;
2716 auio.uio_segflg = bufseg;
2718 auio.uio_resid = count;
2719 error = VOP_READLINK(vp, &auio, td->td_ucred);
2722 VFS_UNLOCK_GIANT(vfslocked);
2723 td->td_retval[0] = count - auio.uio_resid;
2728 * Common implementation code for chflags() and fchflags().
2731 setfflags(td, vp, flags)
2741 * Prevent non-root users from setting flags on devices. When
2742 * a device is reused, users can retain ownership of the device
2743 * if they are allowed to set flags and programs assume that
2744 * chown can't fail when done as root.
2746 if (vp->v_type == VCHR || vp->v_type == VBLK) {
2747 error = priv_check(td, PRIV_VFS_CHFLAGS_DEV);
2752 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2754 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2756 vattr.va_flags = flags;
2758 error = mac_vnode_check_setflags(td->td_ucred, vp, vattr.va_flags);
2761 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
2763 vn_finished_write(mp);
2768 * Change flags of a file given a path name.
2770 #ifndef _SYS_SYSPROTO_H_
2771 struct chflags_args {
2777 sys_chflags(td, uap)
2779 register struct chflags_args /* {
2785 struct nameidata nd;
2788 AUDIT_ARG_FFLAGS(uap->flags);
2789 NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE | AUDITVNODE1, UIO_USERSPACE,
2791 if ((error = namei(&nd)) != 0)
2793 NDFREE(&nd, NDF_ONLY_PNBUF);
2794 vfslocked = NDHASGIANT(&nd);
2795 error = setfflags(td, nd.ni_vp, uap->flags);
2797 VFS_UNLOCK_GIANT(vfslocked);
2802 * Same as chflags() but doesn't follow symlinks.
2805 sys_lchflags(td, uap)
2807 register struct lchflags_args /* {
2813 struct nameidata nd;
2816 AUDIT_ARG_FFLAGS(uap->flags);
2817 NDINIT(&nd, LOOKUP, NOFOLLOW | MPSAFE | AUDITVNODE1, UIO_USERSPACE,
2819 if ((error = namei(&nd)) != 0)
2821 vfslocked = NDHASGIANT(&nd);
2822 NDFREE(&nd, NDF_ONLY_PNBUF);
2823 error = setfflags(td, nd.ni_vp, uap->flags);
2825 VFS_UNLOCK_GIANT(vfslocked);
2830 * Change flags of a file given a file descriptor.
2832 #ifndef _SYS_SYSPROTO_H_
2833 struct fchflags_args {
2839 sys_fchflags(td, uap)
2841 register struct fchflags_args /* {
2850 AUDIT_ARG_FD(uap->fd);
2851 AUDIT_ARG_FFLAGS(uap->flags);
2852 if ((error = getvnode(td->td_proc->p_fd, uap->fd, CAP_FCHFLAGS,
2855 vfslocked = VFS_LOCK_GIANT(fp->f_vnode->v_mount);
2857 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
2858 AUDIT_ARG_VNODE1(fp->f_vnode);
2859 VOP_UNLOCK(fp->f_vnode, 0);
2861 error = setfflags(td, fp->f_vnode, uap->flags);
2862 VFS_UNLOCK_GIANT(vfslocked);
2868 * Common implementation code for chmod(), lchmod() and fchmod().
2871 setfmode(td, cred, vp, mode)
2881 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2883 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2885 vattr.va_mode = mode & ALLPERMS;
2887 error = mac_vnode_check_setmode(cred, vp, vattr.va_mode);
2890 error = VOP_SETATTR(vp, &vattr, cred);
2892 vn_finished_write(mp);
2897 * Change mode of a file given path name.
2899 #ifndef _SYS_SYSPROTO_H_
2908 register struct chmod_args /* {
2914 return (kern_chmod(td, uap->path, UIO_USERSPACE, uap->mode));
2917 #ifndef _SYS_SYSPROTO_H_
2918 struct fchmodat_args {
2926 sys_fchmodat(struct thread *td, struct fchmodat_args *uap)
2928 int flag = uap->flag;
2930 char *path = uap->path;
2931 mode_t mode = uap->mode;
2933 if (flag & ~AT_SYMLINK_NOFOLLOW)
2936 return (kern_fchmodat(td, fd, path, UIO_USERSPACE, mode, flag));
2940 kern_chmod(struct thread *td, char *path, enum uio_seg pathseg, int mode)
2943 return (kern_fchmodat(td, AT_FDCWD, path, pathseg, mode, 0));
2947 * Change mode of a file given path name (don't follow links.)
2949 #ifndef _SYS_SYSPROTO_H_
2950 struct lchmod_args {
2958 register struct lchmod_args /* {
2964 return (kern_fchmodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2965 uap->mode, AT_SYMLINK_NOFOLLOW));
2970 kern_fchmodat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2971 mode_t mode, int flag)
2974 struct nameidata nd;
2978 AUDIT_ARG_MODE(mode);
2979 follow = (flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
2980 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | MPSAFE | AUDITVNODE1, pathseg,
2981 path, fd, CAP_FCHMOD, td);
2982 if ((error = namei(&nd)) != 0)
2984 vfslocked = NDHASGIANT(&nd);
2985 NDFREE(&nd, NDF_ONLY_PNBUF);
2986 error = setfmode(td, td->td_ucred, nd.ni_vp, mode);
2988 VFS_UNLOCK_GIANT(vfslocked);
2993 * Change mode of a file given a file descriptor.
2995 #ifndef _SYS_SYSPROTO_H_
2996 struct fchmod_args {
3002 sys_fchmod(struct thread *td, struct fchmod_args *uap)
3007 AUDIT_ARG_FD(uap->fd);
3008 AUDIT_ARG_MODE(uap->mode);
3010 error = fget(td, uap->fd, CAP_FCHMOD, &fp);
3013 error = fo_chmod(fp, uap->mode, td->td_ucred, td);
3019 * Common implementation for chown(), lchown(), and fchown()
3022 setfown(td, cred, vp, uid, gid)
3033 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
3035 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3040 error = mac_vnode_check_setowner(cred, vp, vattr.va_uid,
3044 error = VOP_SETATTR(vp, &vattr, cred);
3046 vn_finished_write(mp);
3051 * Set ownership given a path name.
3053 #ifndef _SYS_SYSPROTO_H_
3063 register struct chown_args /* {
3070 return (kern_chown(td, uap->path, UIO_USERSPACE, uap->uid, uap->gid));
3073 #ifndef _SYS_SYSPROTO_H_
3074 struct fchownat_args {
3083 sys_fchownat(struct thread *td, struct fchownat_args *uap)
3088 if (flag & ~AT_SYMLINK_NOFOLLOW)
3091 return (kern_fchownat(td, uap->fd, uap->path, UIO_USERSPACE, uap->uid,
3092 uap->gid, uap->flag));
3096 kern_chown(struct thread *td, char *path, enum uio_seg pathseg, int uid,
3100 return (kern_fchownat(td, AT_FDCWD, path, pathseg, uid, gid, 0));
3104 kern_fchownat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
3105 int uid, int gid, int flag)
3107 struct nameidata nd;
3108 int error, vfslocked, follow;
3110 AUDIT_ARG_OWNER(uid, gid);
3111 follow = (flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
3112 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | MPSAFE | AUDITVNODE1, pathseg,
3113 path, fd, CAP_FCHOWN, td);
3115 if ((error = namei(&nd)) != 0)
3117 vfslocked = NDHASGIANT(&nd);
3118 NDFREE(&nd, NDF_ONLY_PNBUF);
3119 error = setfown(td, td->td_ucred, nd.ni_vp, uid, gid);
3121 VFS_UNLOCK_GIANT(vfslocked);
3126 * Set ownership given a path name, do not cross symlinks.
3128 #ifndef _SYS_SYSPROTO_H_
3129 struct lchown_args {
3138 register struct lchown_args /* {
3145 return (kern_lchown(td, uap->path, UIO_USERSPACE, uap->uid, uap->gid));
3149 kern_lchown(struct thread *td, char *path, enum uio_seg pathseg, int uid,
3153 return (kern_fchownat(td, AT_FDCWD, path, pathseg, uid, gid,
3154 AT_SYMLINK_NOFOLLOW));
3158 * Set ownership given a file descriptor.
3160 #ifndef _SYS_SYSPROTO_H_
3161 struct fchown_args {
3170 register struct fchown_args /* {
3179 AUDIT_ARG_FD(uap->fd);
3180 AUDIT_ARG_OWNER(uap->uid, uap->gid);
3181 error = fget(td, uap->fd, CAP_FCHOWN, &fp);
3184 error = fo_chown(fp, uap->uid, uap->gid, td->td_ucred, td);
3190 * Common implementation code for utimes(), lutimes(), and futimes().
3193 getutimes(usrtvp, tvpseg, tsp)
3194 const struct timeval *usrtvp;
3195 enum uio_seg tvpseg;
3196 struct timespec *tsp;
3198 struct timeval tv[2];
3199 const struct timeval *tvp;
3202 if (usrtvp == NULL) {
3203 vfs_timestamp(&tsp[0]);
3206 if (tvpseg == UIO_SYSSPACE) {
3209 if ((error = copyin(usrtvp, tv, sizeof(tv))) != 0)
3214 if (tvp[0].tv_usec < 0 || tvp[0].tv_usec >= 1000000 ||
3215 tvp[1].tv_usec < 0 || tvp[1].tv_usec >= 1000000)
3217 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
3218 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
3224 * Common implementation code for utimes(), lutimes(), and futimes().
3227 setutimes(td, vp, ts, numtimes, nullflag)
3230 const struct timespec *ts;
3234 int error, setbirthtime;
3238 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
3240 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3242 if (numtimes < 3 && !VOP_GETATTR(vp, &vattr, td->td_ucred) &&
3243 timespeccmp(&ts[1], &vattr.va_birthtime, < ))
3246 vattr.va_atime = ts[0];
3247 vattr.va_mtime = ts[1];
3249 vattr.va_birthtime = ts[1];
3251 vattr.va_birthtime = ts[2];
3253 vattr.va_vaflags |= VA_UTIMES_NULL;
3255 error = mac_vnode_check_setutimes(td->td_ucred, vp, vattr.va_atime,
3259 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3261 vn_finished_write(mp);
3266 * Set the access and modification times of a file.
3268 #ifndef _SYS_SYSPROTO_H_
3269 struct utimes_args {
3271 struct timeval *tptr;
3277 register struct utimes_args /* {
3279 struct timeval *tptr;
3283 return (kern_utimes(td, uap->path, UIO_USERSPACE, uap->tptr,
3287 #ifndef _SYS_SYSPROTO_H_
3288 struct futimesat_args {
3291 const struct timeval * times;
3295 sys_futimesat(struct thread *td, struct futimesat_args *uap)
3298 return (kern_utimesat(td, uap->fd, uap->path, UIO_USERSPACE,
3299 uap->times, UIO_USERSPACE));
3303 kern_utimes(struct thread *td, char *path, enum uio_seg pathseg,
3304 struct timeval *tptr, enum uio_seg tptrseg)
3307 return (kern_utimesat(td, AT_FDCWD, path, pathseg, tptr, tptrseg));
3311 kern_utimesat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
3312 struct timeval *tptr, enum uio_seg tptrseg)
3314 struct nameidata nd;
3315 struct timespec ts[2];
3316 int error, vfslocked;
3318 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3320 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | MPSAFE | AUDITVNODE1, pathseg,
3321 path, fd, CAP_FUTIMES, td);
3323 if ((error = namei(&nd)) != 0)
3325 vfslocked = NDHASGIANT(&nd);
3326 NDFREE(&nd, NDF_ONLY_PNBUF);
3327 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3329 VFS_UNLOCK_GIANT(vfslocked);
3334 * Set the access and modification times of a file.
3336 #ifndef _SYS_SYSPROTO_H_
3337 struct lutimes_args {
3339 struct timeval *tptr;
3343 sys_lutimes(td, uap)
3345 register struct lutimes_args /* {
3347 struct timeval *tptr;
3351 return (kern_lutimes(td, uap->path, UIO_USERSPACE, uap->tptr,
3356 kern_lutimes(struct thread *td, char *path, enum uio_seg pathseg,
3357 struct timeval *tptr, enum uio_seg tptrseg)
3359 struct timespec ts[2];
3361 struct nameidata nd;
3364 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3366 NDINIT(&nd, LOOKUP, NOFOLLOW | MPSAFE | AUDITVNODE1, pathseg, path, td);
3367 if ((error = namei(&nd)) != 0)
3369 vfslocked = NDHASGIANT(&nd);
3370 NDFREE(&nd, NDF_ONLY_PNBUF);
3371 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3373 VFS_UNLOCK_GIANT(vfslocked);
3378 * Set the access and modification times of a file.
3380 #ifndef _SYS_SYSPROTO_H_
3381 struct futimes_args {
3383 struct timeval *tptr;
3387 sys_futimes(td, uap)
3389 register struct futimes_args /* {
3391 struct timeval *tptr;
3395 return (kern_futimes(td, uap->fd, uap->tptr, UIO_USERSPACE));
3399 kern_futimes(struct thread *td, int fd, struct timeval *tptr,
3400 enum uio_seg tptrseg)
3402 struct timespec ts[2];
3408 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3410 if ((error = getvnode(td->td_proc->p_fd, fd, CAP_FUTIMES, &fp))
3413 vfslocked = VFS_LOCK_GIANT(fp->f_vnode->v_mount);
3415 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
3416 AUDIT_ARG_VNODE1(fp->f_vnode);
3417 VOP_UNLOCK(fp->f_vnode, 0);
3419 error = setutimes(td, fp->f_vnode, ts, 2, tptr == NULL);
3420 VFS_UNLOCK_GIANT(vfslocked);
3426 * Truncate a file given its path name.
3428 #ifndef _SYS_SYSPROTO_H_
3429 struct truncate_args {
3436 sys_truncate(td, uap)
3438 register struct truncate_args /* {
3445 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3449 kern_truncate(struct thread *td, char *path, enum uio_seg pathseg, off_t length)
3455 struct nameidata nd;
3460 NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE | AUDITVNODE1, pathseg, path, td);
3461 if ((error = namei(&nd)) != 0)
3463 vfslocked = NDHASGIANT(&nd);
3465 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0) {
3467 VFS_UNLOCK_GIANT(vfslocked);
3470 NDFREE(&nd, NDF_ONLY_PNBUF);
3471 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3472 if (vp->v_type == VDIR)
3475 else if ((error = mac_vnode_check_write(td->td_ucred, NOCRED, vp))) {
3478 else if ((error = vn_writechk(vp)) == 0 &&
3479 (error = VOP_ACCESS(vp, VWRITE, td->td_ucred, td)) == 0) {
3481 vattr.va_size = length;
3482 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3485 vn_finished_write(mp);
3486 VFS_UNLOCK_GIANT(vfslocked);
3490 #if defined(COMPAT_43)
3492 * Truncate a file given its path name.
3494 #ifndef _SYS_SYSPROTO_H_
3495 struct otruncate_args {
3503 register struct otruncate_args /* {
3508 struct truncate_args /* {
3514 nuap.path = uap->path;
3515 nuap.length = uap->length;
3516 return (sys_truncate(td, &nuap));
3518 #endif /* COMPAT_43 */
3520 /* Versions with the pad argument */
3522 freebsd6_truncate(struct thread *td, struct freebsd6_truncate_args *uap)
3524 struct truncate_args ouap;
3526 ouap.path = uap->path;
3527 ouap.length = uap->length;
3528 return (sys_truncate(td, &ouap));
3532 freebsd6_ftruncate(struct thread *td, struct freebsd6_ftruncate_args *uap)
3534 struct ftruncate_args ouap;
3537 ouap.length = uap->length;
3538 return (sys_ftruncate(td, &ouap));
3542 * Sync an open file.
3544 #ifndef _SYS_SYSPROTO_H_
3552 struct fsync_args /* {
3560 int error, lock_flags;
3562 AUDIT_ARG_FD(uap->fd);
3563 if ((error = getvnode(td->td_proc->p_fd, uap->fd, CAP_FSYNC,
3567 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
3568 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
3570 if (MNT_SHARED_WRITES(mp) ||
3571 ((mp == NULL) && MNT_SHARED_WRITES(vp->v_mount))) {
3572 lock_flags = LK_SHARED;
3574 lock_flags = LK_EXCLUSIVE;
3576 vn_lock(vp, lock_flags | LK_RETRY);
3577 AUDIT_ARG_VNODE1(vp);
3578 if (vp->v_object != NULL) {
3579 VM_OBJECT_LOCK(vp->v_object);
3580 vm_object_page_clean(vp->v_object, 0, 0, 0);
3581 VM_OBJECT_UNLOCK(vp->v_object);
3583 error = VOP_FSYNC(vp, MNT_WAIT, td);
3586 vn_finished_write(mp);
3588 VFS_UNLOCK_GIANT(vfslocked);
3594 * Rename files. Source and destination must either both be directories, or
3595 * both not be directories. If target is a directory, it must be empty.
3597 #ifndef _SYS_SYSPROTO_H_
3598 struct rename_args {
3606 register struct rename_args /* {
3612 return (kern_rename(td, uap->from, uap->to, UIO_USERSPACE));
3615 #ifndef _SYS_SYSPROTO_H_
3616 struct renameat_args {
3624 sys_renameat(struct thread *td, struct renameat_args *uap)
3627 return (kern_renameat(td, uap->oldfd, uap->old, uap->newfd, uap->new,
3632 kern_rename(struct thread *td, char *from, char *to, enum uio_seg pathseg)
3635 return (kern_renameat(td, AT_FDCWD, from, AT_FDCWD, to, pathseg));
3639 kern_renameat(struct thread *td, int oldfd, char *old, int newfd, char *new,
3640 enum uio_seg pathseg)
3642 struct mount *mp = NULL;
3643 struct vnode *tvp, *fvp, *tdvp;
3644 struct nameidata fromnd, tond;
3651 NDINIT_ATRIGHTS(&fromnd, DELETE, LOCKPARENT | LOCKLEAF | SAVESTART |
3652 MPSAFE | AUDITVNODE1, pathseg, old, oldfd, CAP_DELETE, td);
3654 NDINIT_ATRIGHTS(&fromnd, DELETE, WANTPARENT | SAVESTART | MPSAFE |
3655 AUDITVNODE1, pathseg, old, oldfd, CAP_DELETE, td);
3658 if ((error = namei(&fromnd)) != 0)
3660 fvfslocked = NDHASGIANT(&fromnd);
3663 error = mac_vnode_check_rename_from(td->td_ucred, fromnd.ni_dvp,
3664 fromnd.ni_vp, &fromnd.ni_cnd);
3665 VOP_UNLOCK(fromnd.ni_dvp, 0);
3666 if (fromnd.ni_dvp != fromnd.ni_vp)
3667 VOP_UNLOCK(fromnd.ni_vp, 0);
3671 error = vn_start_write(fvp, &mp, V_WAIT | PCATCH);
3673 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3674 vrele(fromnd.ni_dvp);
3678 NDINIT_ATRIGHTS(&tond, RENAME, LOCKPARENT | LOCKLEAF | NOCACHE |
3679 SAVESTART | MPSAFE | AUDITVNODE2, pathseg, new, newfd, CAP_CREATE,
3681 if (fromnd.ni_vp->v_type == VDIR)
3682 tond.ni_cnd.cn_flags |= WILLBEDIR;
3683 if ((error = namei(&tond)) != 0) {
3684 /* Translate error code for rename("dir1", "dir2/."). */
3685 if (error == EISDIR && fvp->v_type == VDIR)
3687 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3688 vrele(fromnd.ni_dvp);
3690 vn_finished_write(mp);
3693 tvfslocked = NDHASGIANT(&tond);
3697 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
3700 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
3710 * If the source is the same as the destination (that is, if they
3711 * are links to the same vnode), then there is nothing to do.
3717 error = mac_vnode_check_rename_to(td->td_ucred, tdvp,
3718 tond.ni_vp, fromnd.ni_dvp == tdvp, &tond.ni_cnd);
3722 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
3723 tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
3724 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3725 NDFREE(&tond, NDF_ONLY_PNBUF);
3727 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3728 NDFREE(&tond, NDF_ONLY_PNBUF);
3735 vrele(fromnd.ni_dvp);
3738 vrele(tond.ni_startdir);
3739 vn_finished_write(mp);
3741 if (fromnd.ni_startdir)
3742 vrele(fromnd.ni_startdir);
3743 VFS_UNLOCK_GIANT(fvfslocked);
3744 VFS_UNLOCK_GIANT(tvfslocked);
3751 * Make a directory file.
3753 #ifndef _SYS_SYSPROTO_H_
3762 register struct mkdir_args /* {
3768 return (kern_mkdir(td, uap->path, UIO_USERSPACE, uap->mode));
3771 #ifndef _SYS_SYSPROTO_H_
3772 struct mkdirat_args {
3779 sys_mkdirat(struct thread *td, struct mkdirat_args *uap)
3782 return (kern_mkdirat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode));
3786 kern_mkdir(struct thread *td, char *path, enum uio_seg segflg, int mode)
3789 return (kern_mkdirat(td, AT_FDCWD, path, segflg, mode));
3793 kern_mkdirat(struct thread *td, int fd, char *path, enum uio_seg segflg,
3800 struct nameidata nd;
3803 AUDIT_ARG_MODE(mode);
3806 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | MPSAFE |
3807 AUDITVNODE1, segflg, path, fd, CAP_MKDIR, td);
3808 nd.ni_cnd.cn_flags |= WILLBEDIR;
3809 if ((error = namei(&nd)) != 0)
3811 vfslocked = NDHASGIANT(&nd);
3814 NDFREE(&nd, NDF_ONLY_PNBUF);
3816 * XXX namei called with LOCKPARENT but not LOCKLEAF has
3817 * the strange behaviour of leaving the vnode unlocked
3818 * if the target is the same vnode as the parent.
3820 if (vp == nd.ni_dvp)
3825 VFS_UNLOCK_GIANT(vfslocked);
3828 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3829 NDFREE(&nd, NDF_ONLY_PNBUF);
3831 VFS_UNLOCK_GIANT(vfslocked);
3832 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3837 vattr.va_type = VDIR;
3838 vattr.va_mode = (mode & ACCESSPERMS) &~ td->td_proc->p_fd->fd_cmask;
3840 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
3845 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
3849 NDFREE(&nd, NDF_ONLY_PNBUF);
3853 vn_finished_write(mp);
3854 VFS_UNLOCK_GIANT(vfslocked);
3859 * Remove a directory file.
3861 #ifndef _SYS_SYSPROTO_H_
3869 struct rmdir_args /* {
3874 return (kern_rmdir(td, uap->path, UIO_USERSPACE));
3878 kern_rmdir(struct thread *td, char *path, enum uio_seg pathseg)
3881 return (kern_rmdirat(td, AT_FDCWD, path, pathseg));
3885 kern_rmdirat(struct thread *td, int fd, char *path, enum uio_seg pathseg)
3890 struct nameidata nd;
3895 NDINIT_ATRIGHTS(&nd, DELETE, LOCKPARENT | LOCKLEAF | MPSAFE |
3896 AUDITVNODE1, pathseg, path, fd, CAP_RMDIR, td);
3897 if ((error = namei(&nd)) != 0)
3899 vfslocked = NDHASGIANT(&nd);
3901 if (vp->v_type != VDIR) {
3906 * No rmdir "." please.
3908 if (nd.ni_dvp == vp) {
3913 * The root of a mounted filesystem cannot be deleted.
3915 if (vp->v_vflag & VV_ROOT) {
3920 error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp,
3925 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3926 NDFREE(&nd, NDF_ONLY_PNBUF);
3928 if (nd.ni_dvp == vp)
3932 VFS_UNLOCK_GIANT(vfslocked);
3933 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3937 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
3938 vn_finished_write(mp);
3940 NDFREE(&nd, NDF_ONLY_PNBUF);
3942 if (nd.ni_dvp == vp)
3946 VFS_UNLOCK_GIANT(vfslocked);
3952 * Read a block of directory entries in a filesystem independent format.
3954 #ifndef _SYS_SYSPROTO_H_
3955 struct ogetdirentries_args {
3963 ogetdirentries(struct thread *td, struct ogetdirentries_args *uap)
3968 error = kern_ogetdirentries(td, uap, &loff);
3970 error = copyout(&loff, uap->basep, sizeof(long));
3975 kern_ogetdirentries(struct thread *td, struct ogetdirentries_args *uap,
3980 struct uio auio, kuio;
3981 struct iovec aiov, kiov;
3982 struct dirent *dp, *edp;
3984 int error, eofflag, readcnt, vfslocked;
3987 /* XXX arbitrary sanity limit on `count'. */
3988 if (uap->count > 64 * 1024)
3990 if ((error = getvnode(td->td_proc->p_fd, uap->fd, CAP_READ,
3993 if ((fp->f_flag & FREAD) == 0) {
3999 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
4000 if (vp->v_type != VDIR) {
4001 VFS_UNLOCK_GIANT(vfslocked);
4005 aiov.iov_base = uap->buf;
4006 aiov.iov_len = uap->count;
4007 auio.uio_iov = &aiov;
4008 auio.uio_iovcnt = 1;
4009 auio.uio_rw = UIO_READ;
4010 auio.uio_segflg = UIO_USERSPACE;
4012 auio.uio_resid = uap->count;
4013 vn_lock(vp, LK_SHARED | LK_RETRY);
4014 loff = auio.uio_offset = fp->f_offset;
4016 error = mac_vnode_check_readdir(td->td_ucred, vp);
4019 VFS_UNLOCK_GIANT(vfslocked);
4024 # if (BYTE_ORDER != LITTLE_ENDIAN)
4025 if (vp->v_mount->mnt_maxsymlinklen <= 0) {
4026 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag,
4028 fp->f_offset = auio.uio_offset;
4033 kuio.uio_iov = &kiov;
4034 kuio.uio_segflg = UIO_SYSSPACE;
4035 kiov.iov_len = uap->count;
4036 dirbuf = malloc(uap->count, M_TEMP, M_WAITOK);
4037 kiov.iov_base = dirbuf;
4038 error = VOP_READDIR(vp, &kuio, fp->f_cred, &eofflag,
4040 fp->f_offset = kuio.uio_offset;
4042 readcnt = uap->count - kuio.uio_resid;
4043 edp = (struct dirent *)&dirbuf[readcnt];
4044 for (dp = (struct dirent *)dirbuf; dp < edp; ) {
4045 # if (BYTE_ORDER == LITTLE_ENDIAN)
4047 * The expected low byte of
4048 * dp->d_namlen is our dp->d_type.
4049 * The high MBZ byte of dp->d_namlen
4050 * is our dp->d_namlen.
4052 dp->d_type = dp->d_namlen;
4056 * The dp->d_type is the high byte
4057 * of the expected dp->d_namlen,
4058 * so must be zero'ed.
4062 if (dp->d_reclen > 0) {
4063 dp = (struct dirent *)
4064 ((char *)dp + dp->d_reclen);
4071 error = uiomove(dirbuf, readcnt, &auio);
4073 free(dirbuf, M_TEMP);
4077 VFS_UNLOCK_GIANT(vfslocked);
4081 if (uap->count == auio.uio_resid &&
4082 (vp->v_vflag & VV_ROOT) &&
4083 (vp->v_mount->mnt_flag & MNT_UNION)) {
4084 struct vnode *tvp = vp;
4085 vp = vp->v_mount->mnt_vnodecovered;
4091 VFS_UNLOCK_GIANT(vfslocked);
4095 VFS_UNLOCK_GIANT(vfslocked);
4097 td->td_retval[0] = uap->count - auio.uio_resid;
4102 #endif /* COMPAT_43 */
4105 * Read a block of directory entries in a filesystem independent format.
4107 #ifndef _SYS_SYSPROTO_H_
4108 struct getdirentries_args {
4116 sys_getdirentries(td, uap)
4118 register struct getdirentries_args /* {
4128 error = kern_getdirentries(td, uap->fd, uap->buf, uap->count, &base);
4131 if (uap->basep != NULL)
4132 error = copyout(&base, uap->basep, sizeof(long));
4137 kern_getdirentries(struct thread *td, int fd, char *buf, u_int count,
4149 if (count > INT_MAX)
4151 if ((error = getvnode(td->td_proc->p_fd, fd, CAP_READ | CAP_SEEK,
4154 if ((fp->f_flag & FREAD) == 0) {
4160 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
4161 if (vp->v_type != VDIR) {
4162 VFS_UNLOCK_GIANT(vfslocked);
4166 aiov.iov_base = buf;
4167 aiov.iov_len = count;
4168 auio.uio_iov = &aiov;
4169 auio.uio_iovcnt = 1;
4170 auio.uio_rw = UIO_READ;
4171 auio.uio_segflg = UIO_USERSPACE;
4173 auio.uio_resid = count;
4174 vn_lock(vp, LK_SHARED | LK_RETRY);
4175 AUDIT_ARG_VNODE1(vp);
4176 loff = auio.uio_offset = fp->f_offset;
4178 error = mac_vnode_check_readdir(td->td_ucred, vp);
4181 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL,
4183 fp->f_offset = auio.uio_offset;
4186 VFS_UNLOCK_GIANT(vfslocked);
4189 if (count == auio.uio_resid &&
4190 (vp->v_vflag & VV_ROOT) &&
4191 (vp->v_mount->mnt_flag & MNT_UNION)) {
4192 struct vnode *tvp = vp;
4193 vp = vp->v_mount->mnt_vnodecovered;
4199 VFS_UNLOCK_GIANT(vfslocked);
4203 VFS_UNLOCK_GIANT(vfslocked);
4205 td->td_retval[0] = count - auio.uio_resid;
4211 #ifndef _SYS_SYSPROTO_H_
4212 struct getdents_args {
4219 sys_getdents(td, uap)
4221 register struct getdents_args /* {
4227 struct getdirentries_args ap;
4230 ap.count = uap->count;
4232 return (sys_getdirentries(td, &ap));
4236 * Set the mode mask for creation of filesystem nodes.
4238 #ifndef _SYS_SYSPROTO_H_
4246 struct umask_args /* {
4250 register struct filedesc *fdp;
4252 FILEDESC_XLOCK(td->td_proc->p_fd);
4253 fdp = td->td_proc->p_fd;
4254 td->td_retval[0] = fdp->fd_cmask;
4255 fdp->fd_cmask = uap->newmask & ALLPERMS;
4256 FILEDESC_XUNLOCK(td->td_proc->p_fd);
4261 * Void all references to file by ripping underlying filesystem away from
4264 #ifndef _SYS_SYSPROTO_H_
4265 struct revoke_args {
4272 register struct revoke_args /* {
4279 struct nameidata nd;
4282 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | MPSAFE | AUDITVNODE1,
4283 UIO_USERSPACE, uap->path, td);
4284 if ((error = namei(&nd)) != 0)
4286 vfslocked = NDHASGIANT(&nd);
4288 NDFREE(&nd, NDF_ONLY_PNBUF);
4289 if (vp->v_type != VCHR || vp->v_rdev == NULL) {
4294 error = mac_vnode_check_revoke(td->td_ucred, vp);
4298 error = VOP_GETATTR(vp, &vattr, td->td_ucred);
4301 if (td->td_ucred->cr_uid != vattr.va_uid) {
4302 error = priv_check(td, PRIV_VFS_ADMIN);
4307 VOP_REVOKE(vp, REVOKEALL);
4310 VFS_UNLOCK_GIANT(vfslocked);
4315 * Convert a user file descriptor to a kernel file entry and check that, if it
4316 * is a capability, the correct rights are present. A reference on the file
4317 * entry is held upon returning.
4320 getvnode(struct filedesc *fdp, int fd, cap_rights_t rights,
4325 struct file *fp_fromcap;
4331 if ((fdp == NULL) || (fp = fget_unlocked(fdp, fd)) == NULL)
4335 * If the file descriptor is for a capability, test rights and use the
4336 * file descriptor referenced by the capability.
4338 error = cap_funwrap(fp, rights, &fp_fromcap);
4340 fdrop(fp, curthread);
4343 if (fp != fp_fromcap) {
4345 fdrop(fp, curthread);
4348 #endif /* CAPABILITIES */
4351 * The file could be not of the vnode type, or it may be not
4352 * yet fully initialized, in which case the f_vnode pointer
4353 * may be set, but f_ops is still badfileops. E.g.,
4354 * devfs_open() transiently create such situation to
4355 * facilitate csw d_fdopen().
4357 * Dupfdopen() handling in kern_openat() installs the
4358 * half-baked file into the process descriptor table, allowing
4359 * other thread to dereference it. Guard against the race by
4362 if (fp->f_vnode == NULL || fp->f_ops == &badfileops) {
4363 fdrop(fp, curthread);
4372 * Get an (NFS) file handle.
4374 #ifndef _SYS_SYSPROTO_H_
4375 struct lgetfh_args {
4383 register struct lgetfh_args *uap;
4385 struct nameidata nd;
4387 register struct vnode *vp;
4391 error = priv_check(td, PRIV_VFS_GETFH);
4394 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | MPSAFE | AUDITVNODE1,
4395 UIO_USERSPACE, uap->fname, td);
4399 vfslocked = NDHASGIANT(&nd);
4400 NDFREE(&nd, NDF_ONLY_PNBUF);
4402 bzero(&fh, sizeof(fh));
4403 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
4404 error = VOP_VPTOFH(vp, &fh.fh_fid);
4406 VFS_UNLOCK_GIANT(vfslocked);
4409 error = copyout(&fh, uap->fhp, sizeof (fh));
4413 #ifndef _SYS_SYSPROTO_H_
4422 register struct getfh_args *uap;
4424 struct nameidata nd;
4426 register struct vnode *vp;
4430 error = priv_check(td, PRIV_VFS_GETFH);
4433 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | MPSAFE | AUDITVNODE1,
4434 UIO_USERSPACE, uap->fname, td);
4438 vfslocked = NDHASGIANT(&nd);
4439 NDFREE(&nd, NDF_ONLY_PNBUF);
4441 bzero(&fh, sizeof(fh));
4442 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
4443 error = VOP_VPTOFH(vp, &fh.fh_fid);
4445 VFS_UNLOCK_GIANT(vfslocked);
4448 error = copyout(&fh, uap->fhp, sizeof (fh));
4453 * syscall for the rpc.lockd to use to translate a NFS file handle into an
4456 * warning: do not remove the priv_check() call or this becomes one giant
4459 #ifndef _SYS_SYSPROTO_H_
4460 struct fhopen_args {
4461 const struct fhandle *u_fhp;
4468 struct fhopen_args /* {
4469 const struct fhandle *u_fhp;
4473 struct proc *p = td->td_proc;
4478 struct vattr *vap = &vat;
4481 register struct filedesc *fdp = p->p_fd;
4482 int fmode, error, type;
4488 error = priv_check(td, PRIV_VFS_FHOPEN);
4491 fmode = FFLAGS(uap->flags);
4492 /* why not allow a non-read/write open for our lockd? */
4493 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
4495 error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
4498 /* find the mount point */
4499 mp = vfs_busyfs(&fhp.fh_fsid);
4502 vfslocked = VFS_LOCK_GIANT(mp);
4503 /* now give me my vnode, it gets returned to me locked */
4504 error = VFS_FHTOVP(mp, &fhp.fh_fid, LK_EXCLUSIVE, &vp);
4509 * from now on we have to make sure not
4510 * to forget about the vnode
4511 * any error that causes an abort must vput(vp)
4512 * just set error = err and 'goto bad;'.
4518 if (vp->v_type == VLNK) {
4522 if (vp->v_type == VSOCK) {
4526 if (vp->v_type != VDIR && fmode & O_DIRECTORY) {
4531 if (fmode & (FWRITE | O_TRUNC)) {
4532 if (vp->v_type == VDIR) {
4536 error = vn_writechk(vp);
4543 if ((fmode & O_APPEND) && (fmode & FWRITE))
4546 error = mac_vnode_check_open(td->td_ucred, vp, accmode);
4551 error = VOP_ACCESS(vp, accmode, td->td_ucred, td);
4555 if (fmode & O_TRUNC) {
4557 VOP_UNLOCK(vp, 0); /* XXX */
4558 if ((error = vn_start_write(NULL, &mp, V_WAIT | PCATCH)) != 0) {
4563 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); /* XXX */
4567 * We don't yet have fp->f_cred, so use td->td_ucred, which
4570 error = mac_vnode_check_write(td->td_ucred, td->td_ucred, vp);
4575 error = VOP_SETATTR(vp, vap, td->td_ucred);
4579 vn_finished_write(mp);
4583 error = VOP_OPEN(vp, fmode, td->td_ucred, td, NULL);
4591 * end of vn_open code
4594 if ((error = falloc(td, &nfp, &indx, fmode)) != 0) {
4599 /* An extra reference on `nfp' has been held for us by falloc(). */
4602 finit(nfp, fmode & FMASK, DTYPE_VNODE, vp, &vnops);
4603 if (fmode & (O_EXLOCK | O_SHLOCK)) {
4604 lf.l_whence = SEEK_SET;
4607 if (fmode & O_EXLOCK)
4608 lf.l_type = F_WRLCK;
4610 lf.l_type = F_RDLCK;
4612 if ((fmode & FNONBLOCK) == 0)
4615 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf,
4618 * The lock request failed. Normally close the
4619 * descriptor but handle the case where someone might
4620 * have dup()d or close()d it when we weren't looking.
4622 fdclose(fdp, fp, indx, td);
4625 * release our private reference
4630 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
4631 atomic_set_int(&fp->f_flag, FHASLOCK);
4636 VFS_UNLOCK_GIANT(vfslocked);
4637 td->td_retval[0] = indx;
4643 VFS_UNLOCK_GIANT(vfslocked);
4648 * Stat an (NFS) file handle.
4650 #ifndef _SYS_SYSPROTO_H_
4651 struct fhstat_args {
4652 struct fhandle *u_fhp;
4659 register struct fhstat_args /* {
4660 struct fhandle *u_fhp;
4671 error = priv_check(td, PRIV_VFS_FHSTAT);
4674 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4677 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4679 vfslocked = VFS_LOCK_GIANT(mp);
4680 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_EXCLUSIVE, &vp);
4683 VFS_UNLOCK_GIANT(vfslocked);
4686 error = vn_stat(vp, &sb, td->td_ucred, NOCRED, td);
4688 VFS_UNLOCK_GIANT(vfslocked);
4691 error = copyout(&sb, uap->sb, sizeof(sb));
4696 * Implement fstatfs() for (NFS) file handles.
4698 #ifndef _SYS_SYSPROTO_H_
4699 struct fhstatfs_args {
4700 struct fhandle *u_fhp;
4705 sys_fhstatfs(td, uap)
4707 struct fhstatfs_args /* {
4708 struct fhandle *u_fhp;
4716 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4719 error = kern_fhstatfs(td, fh, &sf);
4722 return (copyout(&sf, uap->buf, sizeof(sf)));
4726 kern_fhstatfs(struct thread *td, fhandle_t fh, struct statfs *buf)
4734 error = priv_check(td, PRIV_VFS_FHSTATFS);
4737 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4739 vfslocked = VFS_LOCK_GIANT(mp);
4740 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_EXCLUSIVE, &vp);
4743 VFS_UNLOCK_GIANT(vfslocked);
4747 error = prison_canseemount(td->td_ucred, mp);
4751 error = mac_mount_check_stat(td->td_ucred, mp);
4756 * Set these in case the underlying filesystem fails to do so.
4759 sp->f_version = STATFS_VERSION;
4760 sp->f_namemax = NAME_MAX;
4761 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
4762 error = VFS_STATFS(mp, sp);
4767 VFS_UNLOCK_GIANT(vfslocked);
4772 kern_posix_fallocate(struct thread *td, int fd, off_t offset, off_t len)
4777 off_t olen, ooffset;
4778 int error, vfslocked;
4782 error = fget(td, fd, CAP_WRITE, &fp);
4786 switch (fp->f_type) {
4797 if ((fp->f_flag & FWRITE) == 0) {
4802 if (vp->v_type != VREG) {
4806 if (offset < 0 || len <= 0) {
4810 /* Check for wrap. */
4811 if (offset > OFF_MAX - len) {
4816 /* Allocating blocks may take a long time, so iterate. */
4822 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
4824 error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
4826 VFS_UNLOCK_GIANT(vfslocked);
4829 error = vn_lock(vp, LK_EXCLUSIVE);
4831 vn_finished_write(mp);
4832 VFS_UNLOCK_GIANT(vfslocked);
4836 error = mac_vnode_check_write(td->td_ucred, fp->f_cred, vp);
4839 error = VOP_ALLOCATE(vp, &offset, &len);
4841 vn_finished_write(mp);
4842 VFS_UNLOCK_GIANT(vfslocked);
4844 if (olen + ooffset != offset + len) {
4845 panic("offset + len changed from %jx/%jx to %jx/%jx",
4846 ooffset, olen, offset, len);
4848 if (error != 0 || len == 0)
4850 KASSERT(olen > len, ("Iteration did not make progress?"));
4860 sys_posix_fallocate(struct thread *td, struct posix_fallocate_args *uap)
4863 return (kern_posix_fallocate(td, uap->fd, uap->offset, uap->len));
4867 * Unlike madvise(2), we do not make a best effort to remember every
4868 * possible caching hint. Instead, we remember the last setting with
4869 * the exception that we will allow POSIX_FADV_NORMAL to adjust the
4870 * region of any current setting.
4873 kern_posix_fadvise(struct thread *td, int fd, off_t offset, off_t len,
4876 struct fadvise_info *fa, *new;
4882 if (offset < 0 || len < 0 || offset > OFF_MAX - len)
4885 case POSIX_FADV_SEQUENTIAL:
4886 case POSIX_FADV_RANDOM:
4887 case POSIX_FADV_NOREUSE:
4888 new = malloc(sizeof(*fa), M_FADVISE, M_WAITOK);
4890 case POSIX_FADV_NORMAL:
4891 case POSIX_FADV_WILLNEED:
4892 case POSIX_FADV_DONTNEED:
4898 /* XXX: CAP_POSIX_FADVISE? */
4899 error = fget(td, fd, 0, &fp);
4903 switch (fp->f_type) {
4915 if (vp->v_type != VREG) {
4922 end = offset + len - 1;
4924 case POSIX_FADV_SEQUENTIAL:
4925 case POSIX_FADV_RANDOM:
4926 case POSIX_FADV_NOREUSE:
4928 * Try to merge any existing non-standard region with
4929 * this new region if possible, otherwise create a new
4930 * non-standard region for this request.
4932 mtx_pool_lock(mtxpool_sleep, fp);
4934 if (fa != NULL && fa->fa_advice == advice &&
4935 ((fa->fa_start <= end && fa->fa_end >= offset) ||
4936 (end != OFF_MAX && fa->fa_start == end + 1) ||
4937 (fa->fa_end != OFF_MAX && fa->fa_end + 1 == offset))) {
4938 if (offset < fa->fa_start)
4939 fa->fa_start = offset;
4940 if (end > fa->fa_end)
4943 new->fa_advice = advice;
4944 new->fa_start = offset;
4949 mtx_pool_unlock(mtxpool_sleep, fp);
4951 case POSIX_FADV_NORMAL:
4953 * If a the "normal" region overlaps with an existing
4954 * non-standard region, trim or remove the
4955 * non-standard region.
4957 mtx_pool_lock(mtxpool_sleep, fp);
4960 if (offset <= fa->fa_start && end >= fa->fa_end) {
4962 fp->f_advice = NULL;
4963 } else if (offset <= fa->fa_start &&
4964 end >= fa->fa_start)
4965 fa->fa_start = end + 1;
4966 else if (offset <= fa->fa_end && end >= fa->fa_end)
4967 fa->fa_end = offset - 1;
4968 else if (offset >= fa->fa_start && end <= fa->fa_end) {
4970 * If the "normal" region is a middle
4971 * portion of the existing
4972 * non-standard region, just remove
4973 * the whole thing rather than picking
4974 * one side or the other to
4978 fp->f_advice = NULL;
4981 mtx_pool_unlock(mtxpool_sleep, fp);
4983 case POSIX_FADV_WILLNEED:
4984 case POSIX_FADV_DONTNEED:
4985 error = VOP_ADVISE(vp, offset, end, advice);
4991 free(new, M_FADVISE);
4996 sys_posix_fadvise(struct thread *td, struct posix_fadvise_args *uap)
4999 return (kern_posix_fadvise(td, uap->fd, uap->offset, uap->len,