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/capsicum.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>
64 #include <sys/rwlock.h>
68 #include <sys/unistd.h>
69 #include <sys/vnode.h>
72 #include <sys/dirent.h>
74 #include <sys/syscallsubr.h>
75 #include <sys/sysctl.h>
77 #include <sys/ktrace.h>
80 #include <machine/stdarg.h>
82 #include <security/audit/audit.h>
83 #include <security/mac/mac_framework.h>
86 #include <vm/vm_object.h>
87 #include <vm/vm_page.h>
90 #include <ufs/ufs/quota.h>
92 MALLOC_DEFINE(M_FADVISE, "fadvise", "posix_fadvise(2) information");
94 SDT_PROVIDER_DEFINE(vfs);
95 SDT_PROBE_DEFINE2(vfs, , stat, mode, "char *", "int");
96 SDT_PROBE_DEFINE2(vfs, , stat, reg, "char *", "int");
98 static int chroot_refuse_vdir_fds(struct filedesc *fdp);
99 static int kern_chflags(struct thread *td, const char *path,
100 enum uio_seg pathseg, u_long flags);
101 static int kern_chflagsat(struct thread *td, int fd, const char *path,
102 enum uio_seg pathseg, u_long flags, int atflag);
103 static int setfflags(struct thread *td, struct vnode *, u_long);
104 static int getutimes(const struct timeval *, enum uio_seg, struct timespec *);
105 static int getutimens(const struct timespec *, enum uio_seg,
106 struct timespec *, int *);
107 static int setutimes(struct thread *td, struct vnode *,
108 const struct timespec *, int, int);
109 static int vn_access(struct vnode *vp, int user_flags, struct ucred *cred,
113 * The module initialization routine for POSIX asynchronous I/O will
114 * set this to the version of AIO that it implements. (Zero means
115 * that it is not implemented.) This value is used here by pathconf()
116 * and in kern_descrip.c by fpathconf().
118 int async_io_version;
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 if ((mp->mnt_flag & MNT_RDONLY) == 0 &&
144 vn_start_write(NULL, &mp, V_NOWAIT) == 0) {
145 save = curthread_pflags_set(TDP_SYNCIO);
146 vfs_msync(mp, MNT_NOWAIT);
147 VFS_SYNC(mp, MNT_NOWAIT);
148 curthread_pflags_restore(save);
149 vn_finished_write(mp);
151 mtx_lock(&mountlist_mtx);
152 nmp = TAILQ_NEXT(mp, mnt_list);
155 mtx_unlock(&mountlist_mtx);
160 * Change filesystem quotas.
162 #ifndef _SYS_SYSPROTO_H_
163 struct quotactl_args {
171 sys_quotactl(td, uap)
173 register struct quotactl_args /* {
184 AUDIT_ARG_CMD(uap->cmd);
185 AUDIT_ARG_UID(uap->uid);
186 if (!prison_allow(td->td_ucred, PR_ALLOW_QUOTAS))
188 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
190 if ((error = namei(&nd)) != 0)
192 NDFREE(&nd, NDF_ONLY_PNBUF);
193 mp = nd.ni_vp->v_mount;
196 error = vfs_busy(mp, 0);
200 error = VFS_QUOTACTL(mp, uap->cmd, uap->uid, uap->arg);
203 * Since quota on operation typically needs to open quota
204 * file, the Q_QUOTAON handler needs to unbusy the mount point
205 * before calling into namei. Otherwise, unmount might be
206 * started between two vfs_busy() invocations (first is our,
207 * second is from mount point cross-walk code in lookup()),
210 * Require that Q_QUOTAON handles the vfs_busy() reference on
211 * its own, always returning with ubusied mount point.
213 if ((uap->cmd >> SUBCMDSHIFT) != Q_QUOTAON)
219 * Used by statfs conversion routines to scale the block size up if
220 * necessary so that all of the block counts are <= 'max_size'. Note
221 * that 'max_size' should be a bitmask, i.e. 2^n - 1 for some non-zero
225 statfs_scale_blocks(struct statfs *sf, long max_size)
230 KASSERT(powerof2(max_size + 1), ("%s: invalid max_size", __func__));
233 * Attempt to scale the block counts to give a more accurate
234 * overview to userland of the ratio of free space to used
235 * space. To do this, find the largest block count and compute
236 * a divisor that lets it fit into a signed integer <= max_size.
238 if (sf->f_bavail < 0)
239 count = -sf->f_bavail;
241 count = sf->f_bavail;
242 count = MAX(sf->f_blocks, MAX(sf->f_bfree, count));
243 if (count <= max_size)
246 count >>= flsl(max_size);
253 sf->f_bsize <<= shift;
254 sf->f_blocks >>= shift;
255 sf->f_bfree >>= shift;
256 sf->f_bavail >>= shift;
260 * Get filesystem statistics.
262 #ifndef _SYS_SYSPROTO_H_
271 register struct statfs_args /* {
279 error = kern_statfs(td, uap->path, UIO_USERSPACE, &sf);
281 error = copyout(&sf, uap->buf, sizeof(sf));
286 kern_statfs(struct thread *td, char *path, enum uio_seg pathseg,
290 struct statfs *sp, sb;
294 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
299 mp = nd.ni_vp->v_mount;
301 NDFREE(&nd, NDF_ONLY_PNBUF);
303 error = vfs_busy(mp, 0);
308 error = mac_mount_check_stat(td->td_ucred, mp);
313 * Set these in case the underlying filesystem fails to do so.
316 sp->f_version = STATFS_VERSION;
317 sp->f_namemax = NAME_MAX;
318 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
319 error = VFS_STATFS(mp, sp);
322 if (priv_check(td, PRIV_VFS_GENERATION)) {
323 bcopy(sp, &sb, sizeof(sb));
324 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
325 prison_enforce_statfs(td->td_ucred, mp, &sb);
335 * Get filesystem statistics.
337 #ifndef _SYS_SYSPROTO_H_
338 struct fstatfs_args {
346 register struct fstatfs_args /* {
354 error = kern_fstatfs(td, uap->fd, &sf);
356 error = copyout(&sf, uap->buf, sizeof(sf));
361 kern_fstatfs(struct thread *td, int fd, struct statfs *buf)
365 struct statfs *sp, sb;
371 error = getvnode(td->td_proc->p_fd, fd,
372 cap_rights_init(&rights, CAP_FSTATFS), &fp);
376 vn_lock(vp, LK_SHARED | LK_RETRY);
378 AUDIT_ARG_VNODE1(vp);
389 error = vfs_busy(mp, 0);
394 error = mac_mount_check_stat(td->td_ucred, mp);
399 * Set these in case the underlying filesystem fails to do so.
402 sp->f_version = STATFS_VERSION;
403 sp->f_namemax = NAME_MAX;
404 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
405 error = VFS_STATFS(mp, sp);
408 if (priv_check(td, PRIV_VFS_GENERATION)) {
409 bcopy(sp, &sb, sizeof(sb));
410 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
411 prison_enforce_statfs(td->td_ucred, mp, &sb);
422 * Get statistics on all filesystems.
424 #ifndef _SYS_SYSPROTO_H_
425 struct getfsstat_args {
432 sys_getfsstat(td, uap)
434 register struct getfsstat_args /* {
441 return (kern_getfsstat(td, &uap->buf, uap->bufsize, UIO_USERSPACE,
446 * If (bufsize > 0 && bufseg == UIO_SYSSPACE)
447 * The caller is responsible for freeing memory which will be allocated
451 kern_getfsstat(struct thread *td, struct statfs **buf, size_t bufsize,
452 enum uio_seg bufseg, int flags)
454 struct mount *mp, *nmp;
455 struct statfs *sfsp, *sp, sb;
456 size_t count, maxcount;
459 maxcount = bufsize / sizeof(struct statfs);
462 else if (bufseg == UIO_USERSPACE)
464 else /* if (bufseg == UIO_SYSSPACE) */ {
466 mtx_lock(&mountlist_mtx);
467 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
470 mtx_unlock(&mountlist_mtx);
471 if (maxcount > count)
473 sfsp = *buf = malloc(maxcount * sizeof(struct statfs), M_TEMP,
477 mtx_lock(&mountlist_mtx);
478 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
479 if (prison_canseemount(td->td_ucred, mp) != 0) {
480 nmp = TAILQ_NEXT(mp, mnt_list);
484 if (mac_mount_check_stat(td->td_ucred, mp) != 0) {
485 nmp = TAILQ_NEXT(mp, mnt_list);
489 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK)) {
490 nmp = TAILQ_NEXT(mp, mnt_list);
493 if (sfsp && count < maxcount) {
496 * Set these in case the underlying filesystem
499 sp->f_version = STATFS_VERSION;
500 sp->f_namemax = NAME_MAX;
501 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
503 * If MNT_NOWAIT or MNT_LAZY is specified, do not
504 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
505 * overrides MNT_WAIT.
507 if (((flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
508 (flags & MNT_WAIT)) &&
509 (error = VFS_STATFS(mp, sp))) {
510 mtx_lock(&mountlist_mtx);
511 nmp = TAILQ_NEXT(mp, mnt_list);
515 if (priv_check(td, PRIV_VFS_GENERATION)) {
516 bcopy(sp, &sb, sizeof(sb));
517 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
518 prison_enforce_statfs(td->td_ucred, mp, &sb);
521 if (bufseg == UIO_SYSSPACE)
522 bcopy(sp, sfsp, sizeof(*sp));
523 else /* if (bufseg == UIO_USERSPACE) */ {
524 error = copyout(sp, sfsp, sizeof(*sp));
533 mtx_lock(&mountlist_mtx);
534 nmp = TAILQ_NEXT(mp, mnt_list);
537 mtx_unlock(&mountlist_mtx);
538 if (sfsp && count > maxcount)
539 td->td_retval[0] = maxcount;
541 td->td_retval[0] = count;
545 #ifdef COMPAT_FREEBSD4
547 * Get old format filesystem statistics.
549 static void cvtstatfs(struct statfs *, struct ostatfs *);
551 #ifndef _SYS_SYSPROTO_H_
552 struct freebsd4_statfs_args {
558 freebsd4_statfs(td, uap)
560 struct freebsd4_statfs_args /* {
569 error = kern_statfs(td, uap->path, UIO_USERSPACE, &sf);
572 cvtstatfs(&sf, &osb);
573 return (copyout(&osb, uap->buf, sizeof(osb)));
577 * Get filesystem statistics.
579 #ifndef _SYS_SYSPROTO_H_
580 struct freebsd4_fstatfs_args {
586 freebsd4_fstatfs(td, uap)
588 struct freebsd4_fstatfs_args /* {
597 error = kern_fstatfs(td, uap->fd, &sf);
600 cvtstatfs(&sf, &osb);
601 return (copyout(&osb, uap->buf, sizeof(osb)));
605 * Get statistics on all filesystems.
607 #ifndef _SYS_SYSPROTO_H_
608 struct freebsd4_getfsstat_args {
615 freebsd4_getfsstat(td, uap)
617 register struct freebsd4_getfsstat_args /* {
623 struct statfs *buf, *sp;
628 count = uap->bufsize / sizeof(struct ostatfs);
629 size = count * sizeof(struct statfs);
630 error = kern_getfsstat(td, &buf, size, UIO_SYSSPACE, uap->flags);
632 count = td->td_retval[0];
634 while (count > 0 && error == 0) {
636 error = copyout(&osb, uap->buf, sizeof(osb));
647 * Implement fstatfs() for (NFS) file handles.
649 #ifndef _SYS_SYSPROTO_H_
650 struct freebsd4_fhstatfs_args {
651 struct fhandle *u_fhp;
656 freebsd4_fhstatfs(td, uap)
658 struct freebsd4_fhstatfs_args /* {
659 struct fhandle *u_fhp;
668 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
671 error = kern_fhstatfs(td, fh, &sf);
674 cvtstatfs(&sf, &osb);
675 return (copyout(&osb, uap->buf, sizeof(osb)));
679 * Convert a new format statfs structure to an old format statfs structure.
687 statfs_scale_blocks(nsp, LONG_MAX);
688 bzero(osp, sizeof(*osp));
689 osp->f_bsize = nsp->f_bsize;
690 osp->f_iosize = MIN(nsp->f_iosize, LONG_MAX);
691 osp->f_blocks = nsp->f_blocks;
692 osp->f_bfree = nsp->f_bfree;
693 osp->f_bavail = nsp->f_bavail;
694 osp->f_files = MIN(nsp->f_files, LONG_MAX);
695 osp->f_ffree = MIN(nsp->f_ffree, LONG_MAX);
696 osp->f_owner = nsp->f_owner;
697 osp->f_type = nsp->f_type;
698 osp->f_flags = nsp->f_flags;
699 osp->f_syncwrites = MIN(nsp->f_syncwrites, LONG_MAX);
700 osp->f_asyncwrites = MIN(nsp->f_asyncwrites, LONG_MAX);
701 osp->f_syncreads = MIN(nsp->f_syncreads, LONG_MAX);
702 osp->f_asyncreads = MIN(nsp->f_asyncreads, LONG_MAX);
703 strlcpy(osp->f_fstypename, nsp->f_fstypename,
704 MIN(MFSNAMELEN, OMFSNAMELEN));
705 strlcpy(osp->f_mntonname, nsp->f_mntonname,
706 MIN(MNAMELEN, OMNAMELEN));
707 strlcpy(osp->f_mntfromname, nsp->f_mntfromname,
708 MIN(MNAMELEN, OMNAMELEN));
709 osp->f_fsid = nsp->f_fsid;
711 #endif /* COMPAT_FREEBSD4 */
714 * Change current working directory to a given file descriptor.
716 #ifndef _SYS_SYSPROTO_H_
724 struct fchdir_args /* {
728 register struct filedesc *fdp = td->td_proc->p_fd;
729 struct vnode *vp, *tdp, *vpold;
735 AUDIT_ARG_FD(uap->fd);
736 error = getvnode(fdp, uap->fd, cap_rights_init(&rights, CAP_FCHDIR),
743 vn_lock(vp, LK_SHARED | LK_RETRY);
744 AUDIT_ARG_VNODE1(vp);
745 error = change_dir(vp, td);
746 while (!error && (mp = vp->v_mountedhere) != NULL) {
749 error = VFS_ROOT(mp, LK_SHARED, &tdp);
762 vpold = fdp->fd_cdir;
764 FILEDESC_XUNLOCK(fdp);
770 * Change current working directory (``.'').
772 #ifndef _SYS_SYSPROTO_H_
780 struct chdir_args /* {
785 return (kern_chdir(td, uap->path, UIO_USERSPACE));
789 kern_chdir(struct thread *td, char *path, enum uio_seg pathseg)
791 register struct filedesc *fdp = td->td_proc->p_fd;
796 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
798 if ((error = namei(&nd)) != 0)
800 if ((error = change_dir(nd.ni_vp, td)) != 0) {
802 NDFREE(&nd, NDF_ONLY_PNBUF);
805 VOP_UNLOCK(nd.ni_vp, 0);
806 NDFREE(&nd, NDF_ONLY_PNBUF);
809 fdp->fd_cdir = nd.ni_vp;
810 FILEDESC_XUNLOCK(fdp);
816 * Helper function for raised chroot(2) security function: Refuse if
817 * any filedescriptors are open directories.
820 chroot_refuse_vdir_fds(fdp)
821 struct filedesc *fdp;
827 FILEDESC_LOCK_ASSERT(fdp);
829 for (fd = 0; fd <= fdp->fd_lastfile; fd++) {
830 fp = fget_locked(fdp, fd);
833 if (fp->f_type == DTYPE_VNODE) {
835 if (vp->v_type == VDIR)
843 * This sysctl determines if we will allow a process to chroot(2) if it
844 * has a directory open:
845 * 0: disallowed for all processes.
846 * 1: allowed for processes that were not already chroot(2)'ed.
847 * 2: allowed for all processes.
850 static int chroot_allow_open_directories = 1;
852 SYSCTL_INT(_kern, OID_AUTO, chroot_allow_open_directories, CTLFLAG_RW,
853 &chroot_allow_open_directories, 0,
854 "Allow a process to chroot(2) if it has a directory open");
857 * Change notion of root (``/'') directory.
859 #ifndef _SYS_SYSPROTO_H_
867 struct chroot_args /* {
874 error = priv_check(td, PRIV_VFS_CHROOT);
877 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
878 UIO_USERSPACE, uap->path, td);
882 error = change_dir(nd.ni_vp, td);
886 error = mac_vnode_check_chroot(td->td_ucred, nd.ni_vp);
890 VOP_UNLOCK(nd.ni_vp, 0);
891 error = change_root(nd.ni_vp, td);
893 NDFREE(&nd, NDF_ONLY_PNBUF);
898 NDFREE(&nd, NDF_ONLY_PNBUF);
903 * Common routine for chroot and chdir. Callers must provide a locked vnode
915 ASSERT_VOP_LOCKED(vp, "change_dir(): vp not locked");
916 if (vp->v_type != VDIR)
919 error = mac_vnode_check_chdir(td->td_ucred, vp);
923 return (VOP_ACCESS(vp, VEXEC, td->td_ucred, td));
927 * Common routine for kern_chroot() and jail_attach(). The caller is
928 * responsible for invoking priv_check() and mac_vnode_check_chroot() to
929 * authorize this operation.
936 struct filedesc *fdp;
940 fdp = td->td_proc->p_fd;
942 if (chroot_allow_open_directories == 0 ||
943 (chroot_allow_open_directories == 1 && fdp->fd_rdir != rootvnode)) {
944 error = chroot_refuse_vdir_fds(fdp);
946 FILEDESC_XUNLOCK(fdp);
950 oldvp = fdp->fd_rdir;
957 FILEDESC_XUNLOCK(fdp);
963 flags_to_rights(int flags, cap_rights_t *rightsp)
966 if (flags & O_EXEC) {
967 cap_rights_set(rightsp, CAP_FEXECVE);
969 switch ((flags & O_ACCMODE)) {
971 cap_rights_set(rightsp, CAP_READ);
974 cap_rights_set(rightsp, CAP_READ);
977 cap_rights_set(rightsp, CAP_WRITE);
978 if (!(flags & (O_APPEND | O_TRUNC)))
979 cap_rights_set(rightsp, CAP_SEEK);
985 cap_rights_set(rightsp, CAP_CREATE);
988 cap_rights_set(rightsp, CAP_FTRUNCATE);
990 if (flags & (O_SYNC | O_FSYNC))
991 cap_rights_set(rightsp, CAP_FSYNC);
993 if (flags & (O_EXLOCK | O_SHLOCK))
994 cap_rights_set(rightsp, CAP_FLOCK);
998 * Check permissions, allocate an open file structure, and call the device
999 * open routine if any.
1001 #ifndef _SYS_SYSPROTO_H_
1011 register struct open_args /* {
1018 return (kern_open(td, uap->path, UIO_USERSPACE, uap->flags, uap->mode));
1021 #ifndef _SYS_SYSPROTO_H_
1022 struct openat_args {
1030 sys_openat(struct thread *td, struct openat_args *uap)
1033 return (kern_openat(td, uap->fd, uap->path, UIO_USERSPACE, uap->flag,
1038 kern_open(struct thread *td, char *path, enum uio_seg pathseg, int flags,
1042 return (kern_openat(td, AT_FDCWD, path, pathseg, flags, mode));
1046 kern_openat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1047 int flags, int mode)
1049 struct proc *p = td->td_proc;
1050 struct filedesc *fdp = p->p_fd;
1053 struct nameidata nd;
1054 cap_rights_t rights;
1055 int cmode, error, indx;
1059 AUDIT_ARG_FFLAGS(flags);
1060 AUDIT_ARG_MODE(mode);
1061 /* XXX: audit dirfd */
1062 cap_rights_init(&rights, CAP_LOOKUP);
1063 flags_to_rights(flags, &rights);
1065 * Only one of the O_EXEC, O_RDONLY, O_WRONLY and O_RDWR flags
1068 if (flags & O_EXEC) {
1069 if (flags & O_ACCMODE)
1071 } else if ((flags & O_ACCMODE) == O_ACCMODE) {
1074 flags = FFLAGS(flags);
1078 * Allocate the file descriptor, but don't install a descriptor yet.
1080 error = falloc_noinstall(td, &fp);
1084 * An extra reference on `fp' has been held for us by
1085 * falloc_noinstall().
1087 /* Set the flags early so the finit in devfs can pick them up. */
1088 fp->f_flag = flags & FMASK;
1089 cmode = ((mode & ~fdp->fd_cmask) & ALLPERMS) & ~S_ISTXT;
1090 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, fd,
1092 td->td_dupfd = -1; /* XXX check for fdopen */
1093 error = vn_open(&nd, &flags, cmode, fp);
1096 * If the vn_open replaced the method vector, something
1097 * wonderous happened deep below and we just pass it up
1098 * pretending we know what we do.
1100 if (error == ENXIO && fp->f_ops != &badfileops)
1104 * Handle special fdopen() case. bleh.
1106 * Don't do this for relative (capability) lookups; we don't
1107 * understand exactly what would happen, and we don't think
1108 * that it ever should.
1110 if (nd.ni_strictrelative == 0 &&
1111 (error == ENODEV || error == ENXIO) &&
1112 td->td_dupfd >= 0) {
1113 error = dupfdopen(td, fdp, td->td_dupfd, flags, error,
1122 NDFREE(&nd, NDF_ONLY_PNBUF);
1126 * Store the vnode, for any f_type. Typically, the vnode use
1127 * count is decremented by direct call to vn_closefile() for
1128 * files that switched type in the cdevsw fdopen() method.
1132 * If the file wasn't claimed by devfs bind it to the normal
1133 * vnode operations here.
1135 if (fp->f_ops == &badfileops) {
1136 KASSERT(vp->v_type != VFIFO, ("Unexpected fifo."));
1138 finit(fp, (flags & FMASK) | (fp->f_flag & FHASLOCK),
1139 DTYPE_VNODE, vp, &vnops);
1143 if (flags & O_TRUNC) {
1144 error = fo_truncate(fp, 0, td->td_ucred, td);
1150 * If we haven't already installed the FD (for dupfdopen), do so now.
1153 struct filecaps *fcaps;
1156 if (nd.ni_strictrelative == 1)
1157 fcaps = &nd.ni_filecaps;
1161 error = finstall(td, fp, &indx, flags, fcaps);
1162 /* On success finstall() consumes fcaps. */
1164 filecaps_free(&nd.ni_filecaps);
1168 filecaps_free(&nd.ni_filecaps);
1172 * Release our private reference, leaving the one associated with
1173 * the descriptor table intact.
1176 td->td_retval[0] = indx;
1179 KASSERT(indx == -1, ("indx=%d, should be -1", indx));
1188 #ifndef _SYS_SYSPROTO_H_
1189 struct ocreat_args {
1197 register struct ocreat_args /* {
1203 return (kern_open(td, uap->path, UIO_USERSPACE,
1204 O_WRONLY | O_CREAT | O_TRUNC, uap->mode));
1206 #endif /* COMPAT_43 */
1209 * Create a special file.
1211 #ifndef _SYS_SYSPROTO_H_
1221 register struct mknod_args /* {
1228 return (kern_mknod(td, uap->path, UIO_USERSPACE, uap->mode, uap->dev));
1231 #ifndef _SYS_SYSPROTO_H_
1232 struct mknodat_args {
1240 sys_mknodat(struct thread *td, struct mknodat_args *uap)
1243 return (kern_mknodat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode,
1248 kern_mknod(struct thread *td, char *path, enum uio_seg pathseg, int mode,
1252 return (kern_mknodat(td, AT_FDCWD, path, pathseg, mode, dev));
1256 kern_mknodat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1262 struct nameidata nd;
1263 cap_rights_t rights;
1264 int error, whiteout = 0;
1266 AUDIT_ARG_MODE(mode);
1268 switch (mode & S_IFMT) {
1271 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
1272 if (error == 0 && dev == VNOVAL)
1276 error = priv_check(td, PRIV_VFS_MKNOD_BAD);
1279 error = priv_check(td, PRIV_VFS_MKNOD_WHT);
1283 return (kern_mkfifoat(td, fd, path, pathseg, mode));
1293 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
1294 NOCACHE, pathseg, path, fd, cap_rights_init(&rights, CAP_MKNODAT),
1296 if ((error = namei(&nd)) != 0)
1300 NDFREE(&nd, NDF_ONLY_PNBUF);
1301 if (vp == nd.ni_dvp)
1309 vattr.va_mode = (mode & ALLPERMS) &
1310 ~td->td_proc->p_fd->fd_cmask;
1311 vattr.va_rdev = dev;
1314 switch (mode & S_IFMT) {
1315 case S_IFMT: /* used by badsect to flag bad sectors */
1316 vattr.va_type = VBAD;
1319 vattr.va_type = VCHR;
1322 vattr.va_type = VBLK;
1328 panic("kern_mknod: invalid mode");
1331 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1332 NDFREE(&nd, NDF_ONLY_PNBUF);
1334 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1339 if (error == 0 && !whiteout)
1340 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp,
1341 &nd.ni_cnd, &vattr);
1345 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, CREATE);
1347 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp,
1348 &nd.ni_cnd, &vattr);
1353 NDFREE(&nd, NDF_ONLY_PNBUF);
1355 vn_finished_write(mp);
1360 * Create a named pipe.
1362 #ifndef _SYS_SYSPROTO_H_
1363 struct mkfifo_args {
1371 register struct mkfifo_args /* {
1377 return (kern_mkfifo(td, uap->path, UIO_USERSPACE, uap->mode));
1380 #ifndef _SYS_SYSPROTO_H_
1381 struct mkfifoat_args {
1388 sys_mkfifoat(struct thread *td, struct mkfifoat_args *uap)
1391 return (kern_mkfifoat(td, uap->fd, uap->path, UIO_USERSPACE,
1396 kern_mkfifo(struct thread *td, char *path, enum uio_seg pathseg, int mode)
1399 return (kern_mkfifoat(td, AT_FDCWD, path, pathseg, mode));
1403 kern_mkfifoat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1408 struct nameidata nd;
1409 cap_rights_t rights;
1412 AUDIT_ARG_MODE(mode);
1415 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
1416 NOCACHE, pathseg, path, fd, cap_rights_init(&rights, CAP_MKFIFOAT),
1418 if ((error = namei(&nd)) != 0)
1420 if (nd.ni_vp != NULL) {
1421 NDFREE(&nd, NDF_ONLY_PNBUF);
1422 if (nd.ni_vp == nd.ni_dvp)
1429 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1430 NDFREE(&nd, NDF_ONLY_PNBUF);
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 NDFREE(&nd, NDF_ONLY_PNBUF);
1458 * Make a hard file link.
1460 #ifndef _SYS_SYSPROTO_H_
1469 register struct link_args /* {
1475 return (kern_link(td, uap->path, uap->link, UIO_USERSPACE));
1478 #ifndef _SYS_SYSPROTO_H_
1479 struct linkat_args {
1488 sys_linkat(struct thread *td, struct linkat_args *uap)
1493 if (flag & ~AT_SYMLINK_FOLLOW)
1496 return (kern_linkat(td, uap->fd1, uap->fd2, uap->path1, uap->path2,
1497 UIO_USERSPACE, (flag & AT_SYMLINK_FOLLOW) ? FOLLOW : NOFOLLOW));
1500 int hardlink_check_uid = 0;
1501 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
1502 &hardlink_check_uid, 0,
1503 "Unprivileged processes cannot create hard links to files owned by other "
1505 static int hardlink_check_gid = 0;
1506 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
1507 &hardlink_check_gid, 0,
1508 "Unprivileged processes cannot create hard links to files owned by other "
1512 can_hardlink(struct vnode *vp, struct ucred *cred)
1517 if (!hardlink_check_uid && !hardlink_check_gid)
1520 error = VOP_GETATTR(vp, &va, cred);
1524 if (hardlink_check_uid && cred->cr_uid != va.va_uid) {
1525 error = priv_check_cred(cred, PRIV_VFS_LINK, 0);
1530 if (hardlink_check_gid && !groupmember(va.va_gid, cred)) {
1531 error = priv_check_cred(cred, PRIV_VFS_LINK, 0);
1540 kern_link(struct thread *td, char *path, char *link, enum uio_seg segflg)
1543 return (kern_linkat(td, AT_FDCWD, AT_FDCWD, path,link, segflg, FOLLOW));
1547 kern_linkat(struct thread *td, int fd1, int fd2, char *path1, char *path2,
1548 enum uio_seg segflg, int follow)
1552 struct nameidata nd;
1553 cap_rights_t rights;
1558 NDINIT_AT(&nd, LOOKUP, follow | AUDITVNODE1, segflg, path1, fd1, td);
1560 if ((error = namei(&nd)) != 0)
1562 NDFREE(&nd, NDF_ONLY_PNBUF);
1564 if (vp->v_type == VDIR) {
1566 return (EPERM); /* POSIX */
1568 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE2 |
1569 NOCACHE, segflg, path2, fd2, cap_rights_init(&rights, CAP_LINKAT),
1571 if ((error = namei(&nd)) == 0) {
1572 if (nd.ni_vp != NULL) {
1573 NDFREE(&nd, NDF_ONLY_PNBUF);
1574 if (nd.ni_dvp == nd.ni_vp)
1581 } else if (nd.ni_dvp->v_mount != vp->v_mount) {
1583 * Cross-device link. No need to recheck
1584 * vp->v_type, since it cannot change, except
1587 NDFREE(&nd, NDF_ONLY_PNBUF);
1591 } else if ((error = vn_lock(vp, LK_EXCLUSIVE)) == 0) {
1592 error = can_hardlink(vp, td->td_ucred);
1595 error = mac_vnode_check_link(td->td_ucred,
1596 nd.ni_dvp, vp, &nd.ni_cnd);
1601 NDFREE(&nd, NDF_ONLY_PNBUF);
1604 error = vn_start_write(vp, &mp, V_NOWAIT);
1608 NDFREE(&nd, NDF_ONLY_PNBUF);
1609 error = vn_start_write(NULL, &mp,
1615 error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
1618 vn_finished_write(mp);
1619 NDFREE(&nd, NDF_ONLY_PNBUF);
1622 NDFREE(&nd, NDF_ONLY_PNBUF);
1632 * Make a symbolic link.
1634 #ifndef _SYS_SYSPROTO_H_
1635 struct symlink_args {
1641 sys_symlink(td, uap)
1643 register struct symlink_args /* {
1649 return (kern_symlink(td, uap->path, uap->link, UIO_USERSPACE));
1652 #ifndef _SYS_SYSPROTO_H_
1653 struct symlinkat_args {
1660 sys_symlinkat(struct thread *td, struct symlinkat_args *uap)
1663 return (kern_symlinkat(td, uap->path1, uap->fd, uap->path2,
1668 kern_symlink(struct thread *td, char *path, char *link, enum uio_seg segflg)
1671 return (kern_symlinkat(td, path, AT_FDCWD, link, segflg));
1675 kern_symlinkat(struct thread *td, char *path1, int fd, char *path2,
1676 enum uio_seg segflg)
1681 struct nameidata nd;
1683 cap_rights_t rights;
1685 if (segflg == UIO_SYSSPACE) {
1688 syspath = uma_zalloc(namei_zone, M_WAITOK);
1689 if ((error = copyinstr(path1, syspath, MAXPATHLEN, NULL)) != 0)
1692 AUDIT_ARG_TEXT(syspath);
1695 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
1696 NOCACHE, segflg, path2, fd, cap_rights_init(&rights, CAP_SYMLINKAT),
1698 if ((error = namei(&nd)) != 0)
1701 NDFREE(&nd, NDF_ONLY_PNBUF);
1702 if (nd.ni_vp == nd.ni_dvp)
1710 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1711 NDFREE(&nd, NDF_ONLY_PNBUF);
1713 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1718 vattr.va_mode = ACCESSPERMS &~ td->td_proc->p_fd->fd_cmask;
1720 vattr.va_type = VLNK;
1721 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1726 error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr, syspath);
1732 NDFREE(&nd, NDF_ONLY_PNBUF);
1734 vn_finished_write(mp);
1736 if (segflg != UIO_SYSSPACE)
1737 uma_zfree(namei_zone, syspath);
1742 * Delete a whiteout from the filesystem.
1745 sys_undelete(td, uap)
1747 register struct undelete_args /* {
1752 struct nameidata nd;
1757 NDINIT(&nd, DELETE, LOCKPARENT | DOWHITEOUT | AUDITVNODE1,
1758 UIO_USERSPACE, uap->path, td);
1763 if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & ISWHITEOUT)) {
1764 NDFREE(&nd, NDF_ONLY_PNBUF);
1765 if (nd.ni_vp == nd.ni_dvp)
1773 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1774 NDFREE(&nd, NDF_ONLY_PNBUF);
1776 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1780 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, DELETE);
1781 NDFREE(&nd, NDF_ONLY_PNBUF);
1783 vn_finished_write(mp);
1788 * Delete a name from the filesystem.
1790 #ifndef _SYS_SYSPROTO_H_
1791 struct unlink_args {
1798 struct unlink_args /* {
1803 return (kern_unlink(td, uap->path, UIO_USERSPACE));
1806 #ifndef _SYS_SYSPROTO_H_
1807 struct unlinkat_args {
1814 sys_unlinkat(struct thread *td, struct unlinkat_args *uap)
1816 int flag = uap->flag;
1818 char *path = uap->path;
1820 if (flag & ~AT_REMOVEDIR)
1823 if (flag & AT_REMOVEDIR)
1824 return (kern_rmdirat(td, fd, path, UIO_USERSPACE));
1826 return (kern_unlinkat(td, fd, path, UIO_USERSPACE, 0));
1830 kern_unlink(struct thread *td, char *path, enum uio_seg pathseg)
1833 return (kern_unlinkat(td, AT_FDCWD, path, pathseg, 0));
1837 kern_unlinkat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1842 struct nameidata nd;
1844 cap_rights_t rights;
1849 NDINIT_ATRIGHTS(&nd, DELETE, LOCKPARENT | LOCKLEAF | AUDITVNODE1,
1850 pathseg, path, fd, cap_rights_init(&rights, CAP_UNLINKAT), td);
1851 if ((error = namei(&nd)) != 0)
1852 return (error == EINVAL ? EPERM : error);
1854 if (vp->v_type == VDIR && oldinum == 0) {
1855 error = EPERM; /* POSIX */
1856 } else if (oldinum != 0 &&
1857 ((error = vn_stat(vp, &sb, td->td_ucred, NOCRED, td)) == 0) &&
1858 sb.st_ino != oldinum) {
1859 error = EIDRM; /* Identifier removed */
1862 * The root of a mounted filesystem cannot be deleted.
1864 * XXX: can this only be a VDIR case?
1866 if (vp->v_vflag & VV_ROOT)
1870 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1871 NDFREE(&nd, NDF_ONLY_PNBUF);
1873 if (vp == nd.ni_dvp)
1877 if ((error = vn_start_write(NULL, &mp,
1878 V_XSLEEP | PCATCH)) != 0)
1883 error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp,
1888 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_UNLINK);
1889 error = VOP_REMOVE(nd.ni_dvp, vp, &nd.ni_cnd);
1893 vn_finished_write(mp);
1895 NDFREE(&nd, NDF_ONLY_PNBUF);
1897 if (vp == nd.ni_dvp)
1905 * Reposition read/write file offset.
1907 #ifndef _SYS_SYSPROTO_H_
1918 register struct lseek_args /* {
1926 cap_rights_t rights;
1929 AUDIT_ARG_FD(uap->fd);
1930 error = fget(td, uap->fd, cap_rights_init(&rights, CAP_SEEK), &fp);
1933 error = (fp->f_ops->fo_flags & DFLAG_SEEKABLE) != 0 ?
1934 fo_seek(fp, uap->offset, uap->whence, td) : ESPIPE;
1939 #if defined(COMPAT_43)
1941 * Reposition read/write file offset.
1943 #ifndef _SYS_SYSPROTO_H_
1944 struct olseek_args {
1953 register struct olseek_args /* {
1959 struct lseek_args /* {
1967 nuap.offset = uap->offset;
1968 nuap.whence = uap->whence;
1969 return (sys_lseek(td, &nuap));
1971 #endif /* COMPAT_43 */
1973 /* Version with the 'pad' argument */
1975 freebsd6_lseek(td, uap)
1977 register struct freebsd6_lseek_args *uap;
1979 struct lseek_args ouap;
1982 ouap.offset = uap->offset;
1983 ouap.whence = uap->whence;
1984 return (sys_lseek(td, &ouap));
1988 * Check access permissions using passed credentials.
1991 vn_access(vp, user_flags, cred, td)
2000 /* Flags == 0 means only check for existence. */
2004 if (user_flags & R_OK)
2006 if (user_flags & W_OK)
2008 if (user_flags & X_OK)
2011 error = mac_vnode_check_access(cred, vp, accmode);
2015 if ((accmode & VWRITE) == 0 || (error = vn_writechk(vp)) == 0)
2016 error = VOP_ACCESS(vp, accmode, cred, td);
2022 * Check access permissions using "real" credentials.
2024 #ifndef _SYS_SYSPROTO_H_
2025 struct access_args {
2033 register struct access_args /* {
2039 return (kern_access(td, uap->path, UIO_USERSPACE, uap->amode));
2042 #ifndef _SYS_SYSPROTO_H_
2043 struct faccessat_args {
2051 sys_faccessat(struct thread *td, struct faccessat_args *uap)
2054 if (uap->flag & ~AT_EACCESS)
2056 return (kern_accessat(td, uap->fd, uap->path, UIO_USERSPACE, uap->flag,
2061 kern_access(struct thread *td, char *path, enum uio_seg pathseg, int amode)
2064 return (kern_accessat(td, AT_FDCWD, path, pathseg, 0, amode));
2068 kern_accessat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2069 int flag, int amode)
2071 struct ucred *cred, *tmpcred;
2073 struct nameidata nd;
2074 cap_rights_t rights;
2078 * Create and modify a temporary credential instead of one that
2079 * is potentially shared.
2081 if (!(flag & AT_EACCESS)) {
2082 cred = td->td_ucred;
2083 tmpcred = crdup(cred);
2084 tmpcred->cr_uid = cred->cr_ruid;
2085 tmpcred->cr_groups[0] = cred->cr_rgid;
2086 td->td_ucred = tmpcred;
2088 cred = tmpcred = td->td_ucred;
2089 AUDIT_ARG_VALUE(amode);
2090 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF |
2091 AUDITVNODE1, pathseg, path, fd, cap_rights_init(&rights, CAP_FSTAT),
2093 if ((error = namei(&nd)) != 0)
2097 error = vn_access(vp, amode, tmpcred, td);
2098 NDFREE(&nd, NDF_ONLY_PNBUF);
2101 if (!(flag & AT_EACCESS)) {
2102 td->td_ucred = cred;
2109 * Check access permissions using "effective" credentials.
2111 #ifndef _SYS_SYSPROTO_H_
2112 struct eaccess_args {
2118 sys_eaccess(td, uap)
2120 register struct eaccess_args /* {
2126 return (kern_eaccess(td, uap->path, UIO_USERSPACE, uap->amode));
2130 kern_eaccess(struct thread *td, char *path, enum uio_seg pathseg, int amode)
2133 return (kern_accessat(td, AT_FDCWD, path, pathseg, AT_EACCESS, amode));
2136 #if defined(COMPAT_43)
2138 * Get file status; this version follows links.
2140 #ifndef _SYS_SYSPROTO_H_
2149 register struct ostat_args /* {
2158 error = kern_stat(td, uap->path, UIO_USERSPACE, &sb);
2162 return (copyout(&osb, uap->ub, sizeof (osb)));
2166 * Get file status; this version does not follow links.
2168 #ifndef _SYS_SYSPROTO_H_
2169 struct olstat_args {
2177 register struct olstat_args /* {
2186 error = kern_lstat(td, uap->path, UIO_USERSPACE, &sb);
2190 return (copyout(&osb, uap->ub, sizeof (osb)));
2194 * Convert from an old to a new stat structure.
2202 bzero(ost, sizeof(*ost));
2203 ost->st_dev = st->st_dev;
2204 ost->st_ino = st->st_ino;
2205 ost->st_mode = st->st_mode;
2206 ost->st_nlink = st->st_nlink;
2207 ost->st_uid = st->st_uid;
2208 ost->st_gid = st->st_gid;
2209 ost->st_rdev = st->st_rdev;
2210 if (st->st_size < (quad_t)1 << 32)
2211 ost->st_size = st->st_size;
2214 ost->st_atim = st->st_atim;
2215 ost->st_mtim = st->st_mtim;
2216 ost->st_ctim = st->st_ctim;
2217 ost->st_blksize = st->st_blksize;
2218 ost->st_blocks = st->st_blocks;
2219 ost->st_flags = st->st_flags;
2220 ost->st_gen = st->st_gen;
2222 #endif /* COMPAT_43 */
2225 * Get file status; this version follows links.
2227 #ifndef _SYS_SYSPROTO_H_
2236 register struct stat_args /* {
2244 error = kern_stat(td, uap->path, UIO_USERSPACE, &sb);
2246 error = copyout(&sb, uap->ub, sizeof (sb));
2250 #ifndef _SYS_SYSPROTO_H_
2251 struct fstatat_args {
2259 sys_fstatat(struct thread *td, struct fstatat_args *uap)
2264 error = kern_statat(td, uap->flag, uap->fd, uap->path,
2265 UIO_USERSPACE, &sb);
2267 error = copyout(&sb, uap->buf, sizeof (sb));
2272 kern_stat(struct thread *td, char *path, enum uio_seg pathseg, struct stat *sbp)
2275 return (kern_statat(td, 0, AT_FDCWD, path, pathseg, sbp));
2279 kern_statat(struct thread *td, int flag, int fd, char *path,
2280 enum uio_seg pathseg, struct stat *sbp)
2283 return (kern_statat_vnhook(td, flag, fd, path, pathseg, sbp, NULL));
2287 kern_statat_vnhook(struct thread *td, int flag, int fd, char *path,
2288 enum uio_seg pathseg, struct stat *sbp,
2289 void (*hook)(struct vnode *vp, struct stat *sbp))
2291 struct nameidata nd;
2293 cap_rights_t rights;
2296 if (flag & ~AT_SYMLINK_NOFOLLOW)
2299 NDINIT_ATRIGHTS(&nd, LOOKUP, ((flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW :
2300 FOLLOW) | LOCKSHARED | LOCKLEAF | AUDITVNODE1, pathseg, path, fd,
2301 cap_rights_init(&rights, CAP_FSTAT), td);
2303 if ((error = namei(&nd)) != 0)
2305 error = vn_stat(nd.ni_vp, &sb, td->td_ucred, NOCRED, td);
2307 SDT_PROBE2(vfs, , stat, mode, path, sb.st_mode);
2308 if (S_ISREG(sb.st_mode))
2309 SDT_PROBE2(vfs, , stat, reg, path, pathseg);
2310 if (__predict_false(hook != NULL))
2311 hook(nd.ni_vp, &sb);
2313 NDFREE(&nd, NDF_ONLY_PNBUF);
2319 if (KTRPOINT(td, KTR_STRUCT))
2326 * Get file status; this version does not follow links.
2328 #ifndef _SYS_SYSPROTO_H_
2337 register struct lstat_args /* {
2345 error = kern_lstat(td, uap->path, UIO_USERSPACE, &sb);
2347 error = copyout(&sb, uap->ub, sizeof (sb));
2352 kern_lstat(struct thread *td, char *path, enum uio_seg pathseg, struct stat *sbp)
2355 return (kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, path, pathseg,
2360 * Implementation of the NetBSD [l]stat() functions.
2368 bzero(nsb, sizeof *nsb);
2369 nsb->st_dev = sb->st_dev;
2370 nsb->st_ino = sb->st_ino;
2371 nsb->st_mode = sb->st_mode;
2372 nsb->st_nlink = sb->st_nlink;
2373 nsb->st_uid = sb->st_uid;
2374 nsb->st_gid = sb->st_gid;
2375 nsb->st_rdev = sb->st_rdev;
2376 nsb->st_atim = sb->st_atim;
2377 nsb->st_mtim = sb->st_mtim;
2378 nsb->st_ctim = sb->st_ctim;
2379 nsb->st_size = sb->st_size;
2380 nsb->st_blocks = sb->st_blocks;
2381 nsb->st_blksize = sb->st_blksize;
2382 nsb->st_flags = sb->st_flags;
2383 nsb->st_gen = sb->st_gen;
2384 nsb->st_birthtim = sb->st_birthtim;
2387 #ifndef _SYS_SYSPROTO_H_
2396 register struct nstat_args /* {
2405 error = kern_stat(td, uap->path, UIO_USERSPACE, &sb);
2408 cvtnstat(&sb, &nsb);
2409 return (copyout(&nsb, uap->ub, sizeof (nsb)));
2413 * NetBSD lstat. Get file status; this version does not follow links.
2415 #ifndef _SYS_SYSPROTO_H_
2424 register struct nlstat_args /* {
2433 error = kern_lstat(td, uap->path, UIO_USERSPACE, &sb);
2436 cvtnstat(&sb, &nsb);
2437 return (copyout(&nsb, uap->ub, sizeof (nsb)));
2441 * Get configurable pathname variables.
2443 #ifndef _SYS_SYSPROTO_H_
2444 struct pathconf_args {
2450 sys_pathconf(td, uap)
2452 register struct pathconf_args /* {
2458 return (kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name, FOLLOW));
2461 #ifndef _SYS_SYSPROTO_H_
2462 struct lpathconf_args {
2468 sys_lpathconf(td, uap)
2470 register struct lpathconf_args /* {
2476 return (kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name,
2481 kern_pathconf(struct thread *td, char *path, enum uio_seg pathseg, int name,
2484 struct nameidata nd;
2487 NDINIT(&nd, LOOKUP, LOCKSHARED | LOCKLEAF | AUDITVNODE1 | flags,
2489 if ((error = namei(&nd)) != 0)
2491 NDFREE(&nd, NDF_ONLY_PNBUF);
2493 /* If asynchronous I/O is available, it works for all files. */
2494 if (name == _PC_ASYNC_IO)
2495 td->td_retval[0] = async_io_version;
2497 error = VOP_PATHCONF(nd.ni_vp, name, td->td_retval);
2503 * Return target name of a symbolic link.
2505 #ifndef _SYS_SYSPROTO_H_
2506 struct readlink_args {
2513 sys_readlink(td, uap)
2515 register struct readlink_args /* {
2522 return (kern_readlink(td, uap->path, UIO_USERSPACE, uap->buf,
2523 UIO_USERSPACE, uap->count));
2525 #ifndef _SYS_SYSPROTO_H_
2526 struct readlinkat_args {
2534 sys_readlinkat(struct thread *td, struct readlinkat_args *uap)
2537 return (kern_readlinkat(td, uap->fd, uap->path, UIO_USERSPACE,
2538 uap->buf, UIO_USERSPACE, uap->bufsize));
2542 kern_readlink(struct thread *td, char *path, enum uio_seg pathseg, char *buf,
2543 enum uio_seg bufseg, size_t count)
2546 return (kern_readlinkat(td, AT_FDCWD, path, pathseg, buf, bufseg,
2551 kern_readlinkat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2552 char *buf, enum uio_seg bufseg, size_t count)
2557 struct nameidata nd;
2560 if (count > IOSIZE_MAX)
2563 NDINIT_AT(&nd, LOOKUP, NOFOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
2564 pathseg, path, fd, td);
2566 if ((error = namei(&nd)) != 0)
2568 NDFREE(&nd, NDF_ONLY_PNBUF);
2571 error = mac_vnode_check_readlink(td->td_ucred, vp);
2577 if (vp->v_type != VLNK)
2580 aiov.iov_base = buf;
2581 aiov.iov_len = count;
2582 auio.uio_iov = &aiov;
2583 auio.uio_iovcnt = 1;
2584 auio.uio_offset = 0;
2585 auio.uio_rw = UIO_READ;
2586 auio.uio_segflg = bufseg;
2588 auio.uio_resid = count;
2589 error = VOP_READLINK(vp, &auio, td->td_ucred);
2590 td->td_retval[0] = count - auio.uio_resid;
2597 * Common implementation code for chflags() and fchflags().
2600 setfflags(td, vp, flags)
2609 /* We can't support the value matching VNOVAL. */
2610 if (flags == VNOVAL)
2611 return (EOPNOTSUPP);
2614 * Prevent non-root users from setting flags on devices. When
2615 * a device is reused, users can retain ownership of the device
2616 * if they are allowed to set flags and programs assume that
2617 * chown can't fail when done as root.
2619 if (vp->v_type == VCHR || vp->v_type == VBLK) {
2620 error = priv_check(td, PRIV_VFS_CHFLAGS_DEV);
2625 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2628 vattr.va_flags = flags;
2629 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2631 error = mac_vnode_check_setflags(td->td_ucred, vp, vattr.va_flags);
2634 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
2636 vn_finished_write(mp);
2641 * Change flags of a file given a path name.
2643 #ifndef _SYS_SYSPROTO_H_
2644 struct chflags_args {
2650 sys_chflags(td, uap)
2652 register struct chflags_args /* {
2658 return (kern_chflags(td, uap->path, UIO_USERSPACE, uap->flags));
2661 #ifndef _SYS_SYSPROTO_H_
2662 struct chflagsat_args {
2670 sys_chflagsat(struct thread *td, struct chflagsat_args *uap)
2673 const char *path = uap->path;
2674 u_long flags = uap->flags;
2675 int atflag = uap->atflag;
2677 if (atflag & ~AT_SYMLINK_NOFOLLOW)
2680 return (kern_chflagsat(td, fd, path, UIO_USERSPACE, flags, atflag));
2684 kern_chflags(struct thread *td, const char *path, enum uio_seg pathseg,
2688 return (kern_chflagsat(td, AT_FDCWD, path, pathseg, flags, 0));
2692 * Same as chflags() but doesn't follow symlinks.
2695 sys_lchflags(td, uap)
2697 register struct lchflags_args /* {
2703 return (kern_chflagsat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2704 uap->flags, AT_SYMLINK_NOFOLLOW));
2708 kern_chflagsat(struct thread *td, int fd, const char *path,
2709 enum uio_seg pathseg, u_long flags, int atflag)
2711 struct nameidata nd;
2712 cap_rights_t rights;
2715 AUDIT_ARG_FFLAGS(flags);
2716 follow = (atflag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
2717 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, pathseg, path, fd,
2718 cap_rights_init(&rights, CAP_FCHFLAGS), td);
2719 if ((error = namei(&nd)) != 0)
2721 NDFREE(&nd, NDF_ONLY_PNBUF);
2722 error = setfflags(td, nd.ni_vp, flags);
2728 * Change flags of a file given a file descriptor.
2730 #ifndef _SYS_SYSPROTO_H_
2731 struct fchflags_args {
2737 sys_fchflags(td, uap)
2739 register struct fchflags_args /* {
2745 cap_rights_t rights;
2748 AUDIT_ARG_FD(uap->fd);
2749 AUDIT_ARG_FFLAGS(uap->flags);
2750 error = getvnode(td->td_proc->p_fd, uap->fd,
2751 cap_rights_init(&rights, CAP_FCHFLAGS), &fp);
2755 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
2756 AUDIT_ARG_VNODE1(fp->f_vnode);
2757 VOP_UNLOCK(fp->f_vnode, 0);
2759 error = setfflags(td, fp->f_vnode, uap->flags);
2765 * Common implementation code for chmod(), lchmod() and fchmod().
2768 setfmode(td, cred, vp, mode)
2778 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2780 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2782 vattr.va_mode = mode & ALLPERMS;
2784 error = mac_vnode_check_setmode(cred, vp, vattr.va_mode);
2787 error = VOP_SETATTR(vp, &vattr, cred);
2789 vn_finished_write(mp);
2794 * Change mode of a file given path name.
2796 #ifndef _SYS_SYSPROTO_H_
2805 register struct chmod_args /* {
2811 return (kern_chmod(td, uap->path, UIO_USERSPACE, uap->mode));
2814 #ifndef _SYS_SYSPROTO_H_
2815 struct fchmodat_args {
2823 sys_fchmodat(struct thread *td, struct fchmodat_args *uap)
2825 int flag = uap->flag;
2827 char *path = uap->path;
2828 mode_t mode = uap->mode;
2830 if (flag & ~AT_SYMLINK_NOFOLLOW)
2833 return (kern_fchmodat(td, fd, path, UIO_USERSPACE, mode, flag));
2837 kern_chmod(struct thread *td, char *path, enum uio_seg pathseg, int mode)
2840 return (kern_fchmodat(td, AT_FDCWD, path, pathseg, mode, 0));
2844 * Change mode of a file given path name (don't follow links.)
2846 #ifndef _SYS_SYSPROTO_H_
2847 struct lchmod_args {
2855 register struct lchmod_args /* {
2861 return (kern_fchmodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2862 uap->mode, AT_SYMLINK_NOFOLLOW));
2866 kern_fchmodat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2867 mode_t mode, int flag)
2869 struct nameidata nd;
2870 cap_rights_t rights;
2873 AUDIT_ARG_MODE(mode);
2874 follow = (flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
2875 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, pathseg, path, fd,
2876 cap_rights_init(&rights, CAP_FCHMOD), td);
2877 if ((error = namei(&nd)) != 0)
2879 NDFREE(&nd, NDF_ONLY_PNBUF);
2880 error = setfmode(td, td->td_ucred, nd.ni_vp, mode);
2886 * Change mode of a file given a file descriptor.
2888 #ifndef _SYS_SYSPROTO_H_
2889 struct fchmod_args {
2895 sys_fchmod(struct thread *td, struct fchmod_args *uap)
2898 cap_rights_t rights;
2901 AUDIT_ARG_FD(uap->fd);
2902 AUDIT_ARG_MODE(uap->mode);
2904 error = fget(td, uap->fd, cap_rights_init(&rights, CAP_FCHMOD), &fp);
2907 error = fo_chmod(fp, uap->mode, td->td_ucred, td);
2913 * Common implementation for chown(), lchown(), and fchown()
2916 setfown(td, cred, vp, uid, gid)
2927 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2929 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2934 error = mac_vnode_check_setowner(cred, vp, vattr.va_uid,
2938 error = VOP_SETATTR(vp, &vattr, cred);
2940 vn_finished_write(mp);
2945 * Set ownership given a path name.
2947 #ifndef _SYS_SYSPROTO_H_
2957 register struct chown_args /* {
2964 return (kern_chown(td, uap->path, UIO_USERSPACE, uap->uid, uap->gid));
2967 #ifndef _SYS_SYSPROTO_H_
2968 struct fchownat_args {
2977 sys_fchownat(struct thread *td, struct fchownat_args *uap)
2982 if (flag & ~AT_SYMLINK_NOFOLLOW)
2985 return (kern_fchownat(td, uap->fd, uap->path, UIO_USERSPACE, uap->uid,
2986 uap->gid, uap->flag));
2990 kern_chown(struct thread *td, char *path, enum uio_seg pathseg, int uid,
2994 return (kern_fchownat(td, AT_FDCWD, path, pathseg, uid, gid, 0));
2998 kern_fchownat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2999 int uid, int gid, int flag)
3001 struct nameidata nd;
3002 cap_rights_t rights;
3005 AUDIT_ARG_OWNER(uid, gid);
3006 follow = (flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
3007 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, pathseg, path, fd,
3008 cap_rights_init(&rights, CAP_FCHOWN), td);
3010 if ((error = namei(&nd)) != 0)
3012 NDFREE(&nd, NDF_ONLY_PNBUF);
3013 error = setfown(td, td->td_ucred, nd.ni_vp, uid, gid);
3019 * Set ownership given a path name, do not cross symlinks.
3021 #ifndef _SYS_SYSPROTO_H_
3022 struct lchown_args {
3031 register struct lchown_args /* {
3038 return (kern_lchown(td, uap->path, UIO_USERSPACE, uap->uid, uap->gid));
3042 kern_lchown(struct thread *td, char *path, enum uio_seg pathseg, int uid,
3046 return (kern_fchownat(td, AT_FDCWD, path, pathseg, uid, gid,
3047 AT_SYMLINK_NOFOLLOW));
3051 * Set ownership given a file descriptor.
3053 #ifndef _SYS_SYSPROTO_H_
3054 struct fchown_args {
3063 register struct fchown_args /* {
3070 cap_rights_t rights;
3073 AUDIT_ARG_FD(uap->fd);
3074 AUDIT_ARG_OWNER(uap->uid, uap->gid);
3075 error = fget(td, uap->fd, cap_rights_init(&rights, CAP_FCHOWN), &fp);
3078 error = fo_chown(fp, uap->uid, uap->gid, td->td_ucred, td);
3084 * Common implementation code for utimes(), lutimes(), and futimes().
3087 getutimes(usrtvp, tvpseg, tsp)
3088 const struct timeval *usrtvp;
3089 enum uio_seg tvpseg;
3090 struct timespec *tsp;
3092 struct timeval tv[2];
3093 const struct timeval *tvp;
3096 if (usrtvp == NULL) {
3097 vfs_timestamp(&tsp[0]);
3100 if (tvpseg == UIO_SYSSPACE) {
3103 if ((error = copyin(usrtvp, tv, sizeof(tv))) != 0)
3108 if (tvp[0].tv_usec < 0 || tvp[0].tv_usec >= 1000000 ||
3109 tvp[1].tv_usec < 0 || tvp[1].tv_usec >= 1000000)
3111 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
3112 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
3118 * Common implementation code for futimens(), utimensat().
3120 #define UTIMENS_NULL 0x1
3121 #define UTIMENS_EXIT 0x2
3123 getutimens(const struct timespec *usrtsp, enum uio_seg tspseg,
3124 struct timespec *tsp, int *retflags)
3126 struct timespec tsnow;
3129 vfs_timestamp(&tsnow);
3131 if (usrtsp == NULL) {
3134 *retflags |= UTIMENS_NULL;
3137 if (tspseg == UIO_SYSSPACE) {
3140 } else if ((error = copyin(usrtsp, tsp, sizeof(*tsp) * 2)) != 0)
3142 if (tsp[0].tv_nsec == UTIME_OMIT && tsp[1].tv_nsec == UTIME_OMIT)
3143 *retflags |= UTIMENS_EXIT;
3144 if (tsp[0].tv_nsec == UTIME_NOW && tsp[1].tv_nsec == UTIME_NOW)
3145 *retflags |= UTIMENS_NULL;
3146 if (tsp[0].tv_nsec == UTIME_OMIT)
3147 tsp[0].tv_sec = VNOVAL;
3148 else if (tsp[0].tv_nsec == UTIME_NOW)
3150 else if (tsp[0].tv_nsec < 0 || tsp[0].tv_nsec >= 1000000000L)
3152 if (tsp[1].tv_nsec == UTIME_OMIT)
3153 tsp[1].tv_sec = VNOVAL;
3154 else if (tsp[1].tv_nsec == UTIME_NOW)
3156 else if (tsp[1].tv_nsec < 0 || tsp[1].tv_nsec >= 1000000000L)
3163 * Common implementation code for utimes(), lutimes(), futimes(), futimens(),
3167 setutimes(td, vp, ts, numtimes, nullflag)
3170 const struct timespec *ts;
3176 int error, setbirthtime;
3178 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
3180 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3182 if (numtimes < 3 && !VOP_GETATTR(vp, &vattr, td->td_ucred) &&
3183 timespeccmp(&ts[1], &vattr.va_birthtime, < ))
3186 vattr.va_atime = ts[0];
3187 vattr.va_mtime = ts[1];
3189 vattr.va_birthtime = ts[1];
3191 vattr.va_birthtime = ts[2];
3193 vattr.va_vaflags |= VA_UTIMES_NULL;
3195 error = mac_vnode_check_setutimes(td->td_ucred, vp, vattr.va_atime,
3199 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3201 vn_finished_write(mp);
3206 * Set the access and modification times of a file.
3208 #ifndef _SYS_SYSPROTO_H_
3209 struct utimes_args {
3211 struct timeval *tptr;
3217 register struct utimes_args /* {
3219 struct timeval *tptr;
3223 return (kern_utimes(td, uap->path, UIO_USERSPACE, uap->tptr,
3227 #ifndef _SYS_SYSPROTO_H_
3228 struct futimesat_args {
3231 const struct timeval * times;
3235 sys_futimesat(struct thread *td, struct futimesat_args *uap)
3238 return (kern_utimesat(td, uap->fd, uap->path, UIO_USERSPACE,
3239 uap->times, UIO_USERSPACE));
3243 kern_utimes(struct thread *td, char *path, enum uio_seg pathseg,
3244 struct timeval *tptr, enum uio_seg tptrseg)
3247 return (kern_utimesat(td, AT_FDCWD, path, pathseg, tptr, tptrseg));
3251 kern_utimesat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
3252 struct timeval *tptr, enum uio_seg tptrseg)
3254 struct nameidata nd;
3255 struct timespec ts[2];
3256 cap_rights_t rights;
3259 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3261 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, fd,
3262 cap_rights_init(&rights, CAP_FUTIMES), td);
3264 if ((error = namei(&nd)) != 0)
3266 NDFREE(&nd, NDF_ONLY_PNBUF);
3267 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3273 * Set the access and modification times of a file.
3275 #ifndef _SYS_SYSPROTO_H_
3276 struct lutimes_args {
3278 struct timeval *tptr;
3282 sys_lutimes(td, uap)
3284 register struct lutimes_args /* {
3286 struct timeval *tptr;
3290 return (kern_lutimes(td, uap->path, UIO_USERSPACE, uap->tptr,
3295 kern_lutimes(struct thread *td, char *path, enum uio_seg pathseg,
3296 struct timeval *tptr, enum uio_seg tptrseg)
3298 struct timespec ts[2];
3299 struct nameidata nd;
3302 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3304 NDINIT(&nd, LOOKUP, NOFOLLOW | AUDITVNODE1, pathseg, path, td);
3305 if ((error = namei(&nd)) != 0)
3307 NDFREE(&nd, NDF_ONLY_PNBUF);
3308 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3314 * Set the access and modification times of a file.
3316 #ifndef _SYS_SYSPROTO_H_
3317 struct futimes_args {
3319 struct timeval *tptr;
3323 sys_futimes(td, uap)
3325 register struct futimes_args /* {
3327 struct timeval *tptr;
3331 return (kern_futimes(td, uap->fd, uap->tptr, UIO_USERSPACE));
3335 kern_futimes(struct thread *td, int fd, struct timeval *tptr,
3336 enum uio_seg tptrseg)
3338 struct timespec ts[2];
3340 cap_rights_t rights;
3344 error = getutimes(tptr, tptrseg, ts);
3347 error = getvnode(td->td_proc->p_fd, fd,
3348 cap_rights_init(&rights, CAP_FUTIMES), &fp);
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);
3362 sys_futimens(struct thread *td, struct futimens_args *uap)
3365 return (kern_futimens(td, uap->fd, uap->times, UIO_USERSPACE));
3369 kern_futimens(struct thread *td, int fd, struct timespec *tptr,
3370 enum uio_seg tptrseg)
3372 struct timespec ts[2];
3374 cap_rights_t rights;
3378 error = getutimens(tptr, tptrseg, ts, &flags);
3381 if (flags & UTIMENS_EXIT)
3383 error = getvnode(td->td_proc->p_fd, fd,
3384 cap_rights_init(&rights, CAP_FUTIMES), &fp);
3388 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
3389 AUDIT_ARG_VNODE1(fp->f_vnode);
3390 VOP_UNLOCK(fp->f_vnode, 0);
3392 error = setutimes(td, fp->f_vnode, ts, 2, flags & UTIMENS_NULL);
3398 sys_utimensat(struct thread *td, struct utimensat_args *uap)
3401 return (kern_utimensat(td, uap->fd, uap->path, UIO_USERSPACE,
3402 uap->times, UIO_USERSPACE, uap->flag));
3406 kern_utimensat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
3407 struct timespec *tptr, enum uio_seg tptrseg, int flag)
3409 struct nameidata nd;
3410 struct timespec ts[2];
3411 cap_rights_t rights;
3414 if (flag & ~AT_SYMLINK_NOFOLLOW)
3417 if ((error = getutimens(tptr, tptrseg, ts, &flags)) != 0)
3419 NDINIT_ATRIGHTS(&nd, LOOKUP, ((flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW :
3420 FOLLOW) | AUDITVNODE1, pathseg, path, fd,
3421 cap_rights_init(&rights, CAP_FUTIMES), td);
3422 if ((error = namei(&nd)) != 0)
3425 * We are allowed to call namei() regardless of 2xUTIME_OMIT.
3427 * "If both tv_nsec fields are UTIME_OMIT... EACCESS may be detected."
3428 * "Search permission is denied by a component of the path prefix."
3430 NDFREE(&nd, NDF_ONLY_PNBUF);
3431 if ((flags & UTIMENS_EXIT) == 0)
3432 error = setutimes(td, nd.ni_vp, ts, 2, flags & UTIMENS_NULL);
3438 * Truncate a file given its path name.
3440 #ifndef _SYS_SYSPROTO_H_
3441 struct truncate_args {
3448 sys_truncate(td, uap)
3450 register struct truncate_args /* {
3457 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3461 kern_truncate(struct thread *td, char *path, enum uio_seg pathseg, off_t length)
3467 struct nameidata nd;
3472 NDINIT(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, td);
3473 if ((error = namei(&nd)) != 0)
3476 rl_cookie = vn_rangelock_wlock(vp, 0, OFF_MAX);
3477 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0) {
3478 vn_rangelock_unlock(vp, rl_cookie);
3482 NDFREE(&nd, NDF_ONLY_PNBUF);
3483 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3484 if (vp->v_type == VDIR)
3487 else if ((error = mac_vnode_check_write(td->td_ucred, NOCRED, vp))) {
3490 else if ((error = vn_writechk(vp)) == 0 &&
3491 (error = VOP_ACCESS(vp, VWRITE, td->td_ucred, td)) == 0) {
3493 vattr.va_size = length;
3494 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3497 vn_finished_write(mp);
3498 vn_rangelock_unlock(vp, rl_cookie);
3503 #if defined(COMPAT_43)
3505 * Truncate a file given its path name.
3507 #ifndef _SYS_SYSPROTO_H_
3508 struct otruncate_args {
3516 register struct otruncate_args /* {
3521 struct truncate_args /* {
3527 nuap.path = uap->path;
3528 nuap.length = uap->length;
3529 return (sys_truncate(td, &nuap));
3531 #endif /* COMPAT_43 */
3533 /* Versions with the pad argument */
3535 freebsd6_truncate(struct thread *td, struct freebsd6_truncate_args *uap)
3537 struct truncate_args ouap;
3539 ouap.path = uap->path;
3540 ouap.length = uap->length;
3541 return (sys_truncate(td, &ouap));
3545 freebsd6_ftruncate(struct thread *td, struct freebsd6_ftruncate_args *uap)
3547 struct ftruncate_args ouap;
3550 ouap.length = uap->length;
3551 return (sys_ftruncate(td, &ouap));
3555 * Sync an open file.
3557 #ifndef _SYS_SYSPROTO_H_
3565 struct fsync_args /* {
3572 cap_rights_t rights;
3573 int error, lock_flags;
3575 AUDIT_ARG_FD(uap->fd);
3576 error = getvnode(td->td_proc->p_fd, uap->fd,
3577 cap_rights_init(&rights, CAP_FSYNC), &fp);
3581 error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
3584 if (MNT_SHARED_WRITES(mp) ||
3585 ((mp == NULL) && MNT_SHARED_WRITES(vp->v_mount))) {
3586 lock_flags = LK_SHARED;
3588 lock_flags = LK_EXCLUSIVE;
3590 vn_lock(vp, lock_flags | LK_RETRY);
3591 AUDIT_ARG_VNODE1(vp);
3592 if (vp->v_object != NULL) {
3593 VM_OBJECT_WLOCK(vp->v_object);
3594 vm_object_page_clean(vp->v_object, 0, 0, 0);
3595 VM_OBJECT_WUNLOCK(vp->v_object);
3597 error = VOP_FSYNC(vp, MNT_WAIT, td);
3600 vn_finished_write(mp);
3607 * Rename files. Source and destination must either both be directories, or
3608 * both not be directories. If target is a directory, it must be empty.
3610 #ifndef _SYS_SYSPROTO_H_
3611 struct rename_args {
3619 register struct rename_args /* {
3625 return (kern_rename(td, uap->from, uap->to, UIO_USERSPACE));
3628 #ifndef _SYS_SYSPROTO_H_
3629 struct renameat_args {
3637 sys_renameat(struct thread *td, struct renameat_args *uap)
3640 return (kern_renameat(td, uap->oldfd, uap->old, uap->newfd, uap->new,
3645 kern_rename(struct thread *td, char *from, char *to, enum uio_seg pathseg)
3648 return (kern_renameat(td, AT_FDCWD, from, AT_FDCWD, to, pathseg));
3652 kern_renameat(struct thread *td, int oldfd, char *old, int newfd, char *new,
3653 enum uio_seg pathseg)
3655 struct mount *mp = NULL;
3656 struct vnode *tvp, *fvp, *tdvp;
3657 struct nameidata fromnd, tond;
3658 cap_rights_t rights;
3664 NDINIT_ATRIGHTS(&fromnd, DELETE, LOCKPARENT | LOCKLEAF | SAVESTART |
3665 AUDITVNODE1, pathseg, old, oldfd,
3666 cap_rights_init(&rights, CAP_RENAMEAT), td);
3668 NDINIT_ATRIGHTS(&fromnd, DELETE, WANTPARENT | SAVESTART | AUDITVNODE1,
3669 pathseg, old, oldfd, cap_rights_init(&rights, CAP_RENAMEAT), td);
3672 if ((error = namei(&fromnd)) != 0)
3675 error = mac_vnode_check_rename_from(td->td_ucred, fromnd.ni_dvp,
3676 fromnd.ni_vp, &fromnd.ni_cnd);
3677 VOP_UNLOCK(fromnd.ni_dvp, 0);
3678 if (fromnd.ni_dvp != fromnd.ni_vp)
3679 VOP_UNLOCK(fromnd.ni_vp, 0);
3682 NDINIT_ATRIGHTS(&tond, RENAME, LOCKPARENT | LOCKLEAF | NOCACHE |
3683 SAVESTART | AUDITVNODE2, pathseg, new, newfd,
3684 cap_rights_init(&rights, CAP_LINKAT), td);
3685 if (fromnd.ni_vp->v_type == VDIR)
3686 tond.ni_cnd.cn_flags |= WILLBEDIR;
3687 if ((error = namei(&tond)) != 0) {
3688 /* Translate error code for rename("dir1", "dir2/."). */
3689 if (error == EISDIR && fvp->v_type == VDIR)
3691 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3692 vrele(fromnd.ni_dvp);
3698 error = vn_start_write(fvp, &mp, V_NOWAIT);
3700 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3701 NDFREE(&tond, NDF_ONLY_PNBUF);
3708 vrele(fromnd.ni_dvp);
3710 vrele(tond.ni_startdir);
3711 if (fromnd.ni_startdir != NULL)
3712 vrele(fromnd.ni_startdir);
3713 error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH);
3719 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
3722 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
3727 if (newfd != AT_FDCWD) {
3729 * If the target already exists we require CAP_UNLINKAT
3732 error = cap_check(&tond.ni_filecaps.fc_rights,
3733 cap_rights_init(&rights, CAP_UNLINKAT));
3744 * If the source is the same as the destination (that is, if they
3745 * are links to the same vnode), then there is nothing to do.
3751 error = mac_vnode_check_rename_to(td->td_ucred, tdvp,
3752 tond.ni_vp, fromnd.ni_dvp == tdvp, &tond.ni_cnd);
3756 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
3757 tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
3758 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3759 NDFREE(&tond, NDF_ONLY_PNBUF);
3761 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3762 NDFREE(&tond, NDF_ONLY_PNBUF);
3769 vrele(fromnd.ni_dvp);
3772 vrele(tond.ni_startdir);
3773 vn_finished_write(mp);
3775 if (fromnd.ni_startdir)
3776 vrele(fromnd.ni_startdir);
3783 * Make a directory file.
3785 #ifndef _SYS_SYSPROTO_H_
3794 register struct mkdir_args /* {
3800 return (kern_mkdir(td, uap->path, UIO_USERSPACE, uap->mode));
3803 #ifndef _SYS_SYSPROTO_H_
3804 struct mkdirat_args {
3811 sys_mkdirat(struct thread *td, struct mkdirat_args *uap)
3814 return (kern_mkdirat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode));
3818 kern_mkdir(struct thread *td, char *path, enum uio_seg segflg, int mode)
3821 return (kern_mkdirat(td, AT_FDCWD, path, segflg, mode));
3825 kern_mkdirat(struct thread *td, int fd, char *path, enum uio_seg segflg,
3831 struct nameidata nd;
3832 cap_rights_t rights;
3835 AUDIT_ARG_MODE(mode);
3838 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
3839 NOCACHE, segflg, path, fd, cap_rights_init(&rights, CAP_MKDIRAT),
3841 nd.ni_cnd.cn_flags |= WILLBEDIR;
3842 if ((error = namei(&nd)) != 0)
3846 NDFREE(&nd, NDF_ONLY_PNBUF);
3848 * XXX namei called with LOCKPARENT but not LOCKLEAF has
3849 * the strange behaviour of leaving the vnode unlocked
3850 * if the target is the same vnode as the parent.
3852 if (vp == nd.ni_dvp)
3859 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3860 NDFREE(&nd, NDF_ONLY_PNBUF);
3862 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3867 vattr.va_type = VDIR;
3868 vattr.va_mode = (mode & ACCESSPERMS) &~ td->td_proc->p_fd->fd_cmask;
3870 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
3875 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
3879 NDFREE(&nd, NDF_ONLY_PNBUF);
3883 vn_finished_write(mp);
3888 * Remove a directory file.
3890 #ifndef _SYS_SYSPROTO_H_
3898 struct rmdir_args /* {
3903 return (kern_rmdir(td, uap->path, UIO_USERSPACE));
3907 kern_rmdir(struct thread *td, char *path, enum uio_seg pathseg)
3910 return (kern_rmdirat(td, AT_FDCWD, path, pathseg));
3914 kern_rmdirat(struct thread *td, int fd, char *path, enum uio_seg pathseg)
3918 struct nameidata nd;
3919 cap_rights_t rights;
3924 NDINIT_ATRIGHTS(&nd, DELETE, LOCKPARENT | LOCKLEAF | AUDITVNODE1,
3925 pathseg, path, fd, cap_rights_init(&rights, CAP_UNLINKAT), td);
3926 if ((error = namei(&nd)) != 0)
3929 if (vp->v_type != VDIR) {
3934 * No rmdir "." please.
3936 if (nd.ni_dvp == vp) {
3941 * The root of a mounted filesystem cannot be deleted.
3943 if (vp->v_vflag & VV_ROOT) {
3948 error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp,
3953 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3954 NDFREE(&nd, NDF_ONLY_PNBUF);
3956 if (nd.ni_dvp == vp)
3960 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3964 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_UNLINK);
3965 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
3966 vn_finished_write(mp);
3968 NDFREE(&nd, NDF_ONLY_PNBUF);
3970 if (nd.ni_dvp == vp)
3979 * Read a block of directory entries in a filesystem independent format.
3981 #ifndef _SYS_SYSPROTO_H_
3982 struct ogetdirentries_args {
3990 ogetdirentries(struct thread *td, struct ogetdirentries_args *uap)
3995 error = kern_ogetdirentries(td, uap, &loff);
3997 error = copyout(&loff, uap->basep, sizeof(long));
4002 kern_ogetdirentries(struct thread *td, struct ogetdirentries_args *uap,
4007 struct uio auio, kuio;
4008 struct iovec aiov, kiov;
4009 struct dirent *dp, *edp;
4010 cap_rights_t rights;
4012 int error, eofflag, readcnt;
4016 /* XXX arbitrary sanity limit on `count'. */
4017 if (uap->count > 64 * 1024)
4019 error = getvnode(td->td_proc->p_fd, uap->fd,
4020 cap_rights_init(&rights, CAP_READ), &fp);
4023 if ((fp->f_flag & FREAD) == 0) {
4028 foffset = foffset_lock(fp, 0);
4030 if (vp->v_type != VDIR) {
4031 foffset_unlock(fp, foffset, 0);
4035 aiov.iov_base = uap->buf;
4036 aiov.iov_len = uap->count;
4037 auio.uio_iov = &aiov;
4038 auio.uio_iovcnt = 1;
4039 auio.uio_rw = UIO_READ;
4040 auio.uio_segflg = UIO_USERSPACE;
4042 auio.uio_resid = uap->count;
4043 vn_lock(vp, LK_SHARED | LK_RETRY);
4044 loff = auio.uio_offset = foffset;
4046 error = mac_vnode_check_readdir(td->td_ucred, vp);
4049 foffset_unlock(fp, foffset, FOF_NOUPDATE);
4054 # if (BYTE_ORDER != LITTLE_ENDIAN)
4055 if (vp->v_mount->mnt_maxsymlinklen <= 0) {
4056 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag,
4058 foffset = auio.uio_offset;
4063 kuio.uio_iov = &kiov;
4064 kuio.uio_segflg = UIO_SYSSPACE;
4065 kiov.iov_len = uap->count;
4066 dirbuf = malloc(uap->count, M_TEMP, M_WAITOK);
4067 kiov.iov_base = dirbuf;
4068 error = VOP_READDIR(vp, &kuio, fp->f_cred, &eofflag,
4070 foffset = kuio.uio_offset;
4072 readcnt = uap->count - kuio.uio_resid;
4073 edp = (struct dirent *)&dirbuf[readcnt];
4074 for (dp = (struct dirent *)dirbuf; dp < edp; ) {
4075 # if (BYTE_ORDER == LITTLE_ENDIAN)
4077 * The expected low byte of
4078 * dp->d_namlen is our dp->d_type.
4079 * The high MBZ byte of dp->d_namlen
4080 * is our dp->d_namlen.
4082 dp->d_type = dp->d_namlen;
4086 * The dp->d_type is the high byte
4087 * of the expected dp->d_namlen,
4088 * so must be zero'ed.
4092 if (dp->d_reclen > 0) {
4093 dp = (struct dirent *)
4094 ((char *)dp + dp->d_reclen);
4101 error = uiomove(dirbuf, readcnt, &auio);
4103 free(dirbuf, M_TEMP);
4107 foffset_unlock(fp, foffset, 0);
4111 if (uap->count == auio.uio_resid &&
4112 (vp->v_vflag & VV_ROOT) &&
4113 (vp->v_mount->mnt_flag & MNT_UNION)) {
4114 struct vnode *tvp = vp;
4115 vp = vp->v_mount->mnt_vnodecovered;
4124 foffset_unlock(fp, foffset, 0);
4126 td->td_retval[0] = uap->count - auio.uio_resid;
4131 #endif /* COMPAT_43 */
4134 * Read a block of directory entries in a filesystem independent format.
4136 #ifndef _SYS_SYSPROTO_H_
4137 struct getdirentries_args {
4145 sys_getdirentries(td, uap)
4147 register struct getdirentries_args /* {
4157 error = kern_getdirentries(td, uap->fd, uap->buf, uap->count, &base,
4158 NULL, UIO_USERSPACE);
4161 if (uap->basep != NULL)
4162 error = copyout(&base, uap->basep, sizeof(long));
4167 kern_getdirentries(struct thread *td, int fd, char *buf, u_int count,
4168 long *basep, ssize_t *residp, enum uio_seg bufseg)
4174 cap_rights_t rights;
4180 if (count > IOSIZE_MAX)
4182 auio.uio_resid = count;
4183 error = getvnode(td->td_proc->p_fd, fd,
4184 cap_rights_init(&rights, CAP_READ), &fp);
4187 if ((fp->f_flag & FREAD) == 0) {
4192 foffset = foffset_lock(fp, 0);
4194 if (vp->v_type != VDIR) {
4198 aiov.iov_base = buf;
4199 aiov.iov_len = count;
4200 auio.uio_iov = &aiov;
4201 auio.uio_iovcnt = 1;
4202 auio.uio_rw = UIO_READ;
4203 auio.uio_segflg = bufseg;
4205 vn_lock(vp, LK_SHARED | LK_RETRY);
4206 AUDIT_ARG_VNODE1(vp);
4207 loff = auio.uio_offset = foffset;
4209 error = mac_vnode_check_readdir(td->td_ucred, vp);
4212 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL,
4214 foffset = auio.uio_offset;
4219 if (count == auio.uio_resid &&
4220 (vp->v_vflag & VV_ROOT) &&
4221 (vp->v_mount->mnt_flag & MNT_UNION)) {
4222 struct vnode *tvp = vp;
4224 vp = vp->v_mount->mnt_vnodecovered;
4235 *residp = auio.uio_resid;
4236 td->td_retval[0] = count - auio.uio_resid;
4238 foffset_unlock(fp, foffset, 0);
4243 #ifndef _SYS_SYSPROTO_H_
4244 struct getdents_args {
4251 sys_getdents(td, uap)
4253 register struct getdents_args /* {
4259 struct getdirentries_args ap;
4263 ap.count = uap->count;
4265 return (sys_getdirentries(td, &ap));
4269 * Set the mode mask for creation of filesystem nodes.
4271 #ifndef _SYS_SYSPROTO_H_
4279 struct umask_args /* {
4283 register struct filedesc *fdp;
4285 FILEDESC_XLOCK(td->td_proc->p_fd);
4286 fdp = td->td_proc->p_fd;
4287 td->td_retval[0] = fdp->fd_cmask;
4288 fdp->fd_cmask = uap->newmask & ALLPERMS;
4289 FILEDESC_XUNLOCK(td->td_proc->p_fd);
4294 * Void all references to file by ripping underlying filesystem away from
4297 #ifndef _SYS_SYSPROTO_H_
4298 struct revoke_args {
4305 register struct revoke_args /* {
4311 struct nameidata nd;
4314 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
4316 if ((error = namei(&nd)) != 0)
4319 NDFREE(&nd, NDF_ONLY_PNBUF);
4320 if (vp->v_type != VCHR || vp->v_rdev == NULL) {
4325 error = mac_vnode_check_revoke(td->td_ucred, vp);
4329 error = VOP_GETATTR(vp, &vattr, td->td_ucred);
4332 if (td->td_ucred->cr_uid != vattr.va_uid) {
4333 error = priv_check(td, PRIV_VFS_ADMIN);
4338 VOP_REVOKE(vp, REVOKEALL);
4345 * Convert a user file descriptor to a kernel file entry and check that, if it
4346 * is a capability, the correct rights are present. A reference on the file
4347 * entry is held upon returning.
4350 getvnode(struct filedesc *fdp, int fd, cap_rights_t *rightsp, struct file **fpp)
4355 error = fget_unlocked(fdp, fd, rightsp, 0, &fp, NULL);
4360 * The file could be not of the vnode type, or it may be not
4361 * yet fully initialized, in which case the f_vnode pointer
4362 * may be set, but f_ops is still badfileops. E.g.,
4363 * devfs_open() transiently create such situation to
4364 * facilitate csw d_fdopen().
4366 * Dupfdopen() handling in kern_openat() installs the
4367 * half-baked file into the process descriptor table, allowing
4368 * other thread to dereference it. Guard against the race by
4371 if (fp->f_vnode == NULL || fp->f_ops == &badfileops) {
4372 fdrop(fp, curthread);
4381 * Get an (NFS) file handle.
4383 #ifndef _SYS_SYSPROTO_H_
4384 struct lgetfh_args {
4392 register struct lgetfh_args *uap;
4394 struct nameidata nd;
4396 register struct vnode *vp;
4399 error = priv_check(td, PRIV_VFS_GETFH);
4402 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
4407 NDFREE(&nd, NDF_ONLY_PNBUF);
4409 bzero(&fh, sizeof(fh));
4410 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
4411 error = VOP_VPTOFH(vp, &fh.fh_fid);
4414 error = copyout(&fh, uap->fhp, sizeof (fh));
4418 #ifndef _SYS_SYSPROTO_H_
4427 register struct getfh_args *uap;
4429 struct nameidata nd;
4431 register struct vnode *vp;
4434 error = priv_check(td, PRIV_VFS_GETFH);
4437 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
4442 NDFREE(&nd, NDF_ONLY_PNBUF);
4444 bzero(&fh, sizeof(fh));
4445 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
4446 error = VOP_VPTOFH(vp, &fh.fh_fid);
4449 error = copyout(&fh, uap->fhp, sizeof (fh));
4454 * syscall for the rpc.lockd to use to translate a NFS file handle into an
4457 * warning: do not remove the priv_check() call or this becomes one giant
4460 #ifndef _SYS_SYSPROTO_H_
4461 struct fhopen_args {
4462 const struct fhandle *u_fhp;
4469 struct fhopen_args /* {
4470 const struct fhandle *u_fhp;
4481 error = priv_check(td, PRIV_VFS_FHOPEN);
4485 fmode = FFLAGS(uap->flags);
4486 /* why not allow a non-read/write open for our lockd? */
4487 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
4489 error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
4492 /* find the mount point */
4493 mp = vfs_busyfs(&fhp.fh_fsid);
4496 /* now give me my vnode, it gets returned to me locked */
4497 error = VFS_FHTOVP(mp, &fhp.fh_fid, LK_EXCLUSIVE, &vp);
4502 error = falloc_noinstall(td, &fp);
4508 * An extra reference on `fp' has been held for us by
4509 * falloc_noinstall().
4515 error = vn_open_vnode(vp, fmode, td->td_ucred, td, fp);
4517 KASSERT(fp->f_ops == &badfileops,
4518 ("VOP_OPEN in fhopen() set f_ops"));
4519 KASSERT(td->td_dupfd < 0,
4520 ("fhopen() encountered fdopen()"));
4530 finit(fp, (fmode & FMASK) | (fp->f_flag & FHASLOCK), DTYPE_VNODE, vp,
4533 if ((fmode & O_TRUNC) != 0) {
4534 error = fo_truncate(fp, 0, td->td_ucred, td);
4539 error = finstall(td, fp, &indx, fmode, NULL);
4542 td->td_retval[0] = indx;
4547 * Stat an (NFS) file handle.
4549 #ifndef _SYS_SYSPROTO_H_
4550 struct fhstat_args {
4551 struct fhandle *u_fhp;
4558 register struct fhstat_args /* {
4559 struct fhandle *u_fhp;
4567 error = copyin(uap->u_fhp, &fh, sizeof(fh));
4570 error = kern_fhstat(td, fh, &sb);
4572 error = copyout(&sb, uap->sb, sizeof(sb));
4577 kern_fhstat(struct thread *td, struct fhandle fh, struct stat *sb)
4583 error = priv_check(td, PRIV_VFS_FHSTAT);
4586 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4588 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_EXCLUSIVE, &vp);
4592 error = vn_stat(vp, sb, td->td_ucred, NOCRED, td);
4598 * Implement fstatfs() for (NFS) file handles.
4600 #ifndef _SYS_SYSPROTO_H_
4601 struct fhstatfs_args {
4602 struct fhandle *u_fhp;
4607 sys_fhstatfs(td, uap)
4609 struct fhstatfs_args /* {
4610 struct fhandle *u_fhp;
4618 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4621 error = kern_fhstatfs(td, fh, &sf);
4624 return (copyout(&sf, uap->buf, sizeof(sf)));
4628 kern_fhstatfs(struct thread *td, fhandle_t fh, struct statfs *buf)
4635 error = priv_check(td, PRIV_VFS_FHSTATFS);
4638 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4640 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_EXCLUSIVE, &vp);
4646 error = prison_canseemount(td->td_ucred, mp);
4650 error = mac_mount_check_stat(td->td_ucred, mp);
4655 * Set these in case the underlying filesystem fails to do so.
4658 sp->f_version = STATFS_VERSION;
4659 sp->f_namemax = NAME_MAX;
4660 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
4661 error = VFS_STATFS(mp, sp);
4670 kern_posix_fallocate(struct thread *td, int fd, off_t offset, off_t len)
4675 cap_rights_t rights;
4676 off_t olen, ooffset;
4679 if (offset < 0 || len <= 0)
4681 /* Check for wrap. */
4682 if (offset > OFF_MAX - len)
4684 error = fget(td, fd, cap_rights_init(&rights, CAP_WRITE), &fp);
4687 if ((fp->f_ops->fo_flags & DFLAG_SEEKABLE) == 0) {
4691 if ((fp->f_flag & FWRITE) == 0) {
4695 if (fp->f_type != DTYPE_VNODE) {
4700 if (vp->v_type != VREG) {
4705 /* Allocating blocks may take a long time, so iterate. */
4712 error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
4715 error = vn_lock(vp, LK_EXCLUSIVE);
4717 vn_finished_write(mp);
4721 error = mac_vnode_check_write(td->td_ucred, fp->f_cred, vp);
4724 error = VOP_ALLOCATE(vp, &offset, &len);
4726 vn_finished_write(mp);
4728 if (olen + ooffset != offset + len) {
4729 panic("offset + len changed from %jx/%jx to %jx/%jx",
4730 ooffset, olen, offset, len);
4732 if (error != 0 || len == 0)
4734 KASSERT(olen > len, ("Iteration did not make progress?"));
4743 sys_posix_fallocate(struct thread *td, struct posix_fallocate_args *uap)
4746 td->td_retval[0] = kern_posix_fallocate(td, uap->fd, uap->offset,
4752 * Unlike madvise(2), we do not make a best effort to remember every
4753 * possible caching hint. Instead, we remember the last setting with
4754 * the exception that we will allow POSIX_FADV_NORMAL to adjust the
4755 * region of any current setting.
4758 kern_posix_fadvise(struct thread *td, int fd, off_t offset, off_t len,
4761 struct fadvise_info *fa, *new;
4764 cap_rights_t rights;
4768 if (offset < 0 || len < 0 || offset > OFF_MAX - len)
4771 case POSIX_FADV_SEQUENTIAL:
4772 case POSIX_FADV_RANDOM:
4773 case POSIX_FADV_NOREUSE:
4774 new = malloc(sizeof(*fa), M_FADVISE, M_WAITOK);
4776 case POSIX_FADV_NORMAL:
4777 case POSIX_FADV_WILLNEED:
4778 case POSIX_FADV_DONTNEED:
4784 /* XXX: CAP_POSIX_FADVISE? */
4785 error = fget(td, fd, cap_rights_init(&rights), &fp);
4788 if ((fp->f_ops->fo_flags & DFLAG_SEEKABLE) == 0) {
4792 if (fp->f_type != DTYPE_VNODE) {
4797 if (vp->v_type != VREG) {
4804 end = offset + len - 1;
4806 case POSIX_FADV_SEQUENTIAL:
4807 case POSIX_FADV_RANDOM:
4808 case POSIX_FADV_NOREUSE:
4810 * Try to merge any existing non-standard region with
4811 * this new region if possible, otherwise create a new
4812 * non-standard region for this request.
4814 mtx_pool_lock(mtxpool_sleep, fp);
4816 if (fa != NULL && fa->fa_advice == advice &&
4817 ((fa->fa_start <= end && fa->fa_end >= offset) ||
4818 (end != OFF_MAX && fa->fa_start == end + 1) ||
4819 (fa->fa_end != OFF_MAX && fa->fa_end + 1 == offset))) {
4820 if (offset < fa->fa_start)
4821 fa->fa_start = offset;
4822 if (end > fa->fa_end)
4825 new->fa_advice = advice;
4826 new->fa_start = offset;
4828 new->fa_prevstart = 0;
4829 new->fa_prevend = 0;
4833 mtx_pool_unlock(mtxpool_sleep, fp);
4835 case POSIX_FADV_NORMAL:
4837 * If a the "normal" region overlaps with an existing
4838 * non-standard region, trim or remove the
4839 * non-standard region.
4841 mtx_pool_lock(mtxpool_sleep, fp);
4844 if (offset <= fa->fa_start && end >= fa->fa_end) {
4846 fp->f_advice = NULL;
4847 } else if (offset <= fa->fa_start &&
4848 end >= fa->fa_start)
4849 fa->fa_start = end + 1;
4850 else if (offset <= fa->fa_end && end >= fa->fa_end)
4851 fa->fa_end = offset - 1;
4852 else if (offset >= fa->fa_start && end <= fa->fa_end) {
4854 * If the "normal" region is a middle
4855 * portion of the existing
4856 * non-standard region, just remove
4857 * the whole thing rather than picking
4858 * one side or the other to
4862 fp->f_advice = NULL;
4865 mtx_pool_unlock(mtxpool_sleep, fp);
4867 case POSIX_FADV_WILLNEED:
4868 case POSIX_FADV_DONTNEED:
4869 error = VOP_ADVISE(vp, offset, end, advice);
4875 free(new, M_FADVISE);
4880 sys_posix_fadvise(struct thread *td, struct posix_fadvise_args *uap)
4883 td->td_retval[0] = kern_posix_fadvise(td, uap->fd, uap->offset,
4884 uap->len, uap->advice);