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 #include <ufs/ufs/quota.h>
91 MALLOC_DEFINE(M_FADVISE, "fadvise", "posix_fadvise(2) information");
93 SDT_PROVIDER_DEFINE(vfs);
94 SDT_PROBE_DEFINE2(vfs, , stat, mode, "char *", "int");
95 SDT_PROBE_DEFINE2(vfs, , stat, reg, "char *", "int");
97 static int chroot_refuse_vdir_fds(struct filedesc *fdp);
98 static int getutimes(const struct timeval *, enum uio_seg, struct timespec *);
99 static int setfflags(struct thread *td, struct vnode *, int);
100 static int setutimes(struct thread *td, struct vnode *,
101 const struct timespec *, int, int);
102 static int vn_access(struct vnode *vp, int user_flags, struct ucred *cred,
106 * The module initialization routine for POSIX asynchronous I/O will
107 * set this to the version of AIO that it implements. (Zero means
108 * that it is not implemented.) This value is used here by pathconf()
109 * and in kern_descrip.c by fpathconf().
111 int async_io_version;
114 static int syncprt = 0;
115 SYSCTL_INT(_debug, OID_AUTO, syncprt, CTLFLAG_RW, &syncprt, 0, "");
119 * Sync each mounted filesystem.
121 #ifndef _SYS_SYSPROTO_H_
130 struct sync_args *uap;
132 struct mount *mp, *nmp;
135 mtx_lock(&mountlist_mtx);
136 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
137 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK)) {
138 nmp = TAILQ_NEXT(mp, mnt_list);
141 vfslocked = VFS_LOCK_GIANT(mp);
142 if ((mp->mnt_flag & MNT_RDONLY) == 0 &&
143 vn_start_write(NULL, &mp, V_NOWAIT) == 0) {
144 save = curthread_pflags_set(TDP_SYNCIO);
145 vfs_msync(mp, MNT_NOWAIT);
146 VFS_SYNC(mp, MNT_NOWAIT);
147 curthread_pflags_restore(save);
148 vn_finished_write(mp);
150 VFS_UNLOCK_GIANT(vfslocked);
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 /* {
185 AUDIT_ARG_CMD(uap->cmd);
186 AUDIT_ARG_UID(uap->uid);
187 if (!prison_allow(td->td_ucred, PR_ALLOW_QUOTAS))
189 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | MPSAFE | AUDITVNODE1,
190 UIO_USERSPACE, uap->path, td);
191 if ((error = namei(&nd)) != 0)
193 vfslocked = NDHASGIANT(&nd);
194 NDFREE(&nd, NDF_ONLY_PNBUF);
195 mp = nd.ni_vp->v_mount;
198 error = vfs_busy(mp, 0);
201 VFS_UNLOCK_GIANT(vfslocked);
204 error = VFS_QUOTACTL(mp, uap->cmd, uap->uid, uap->arg);
207 * Since quota on operation typically needs to open quota
208 * file, the Q_QUOTAON handler needs to unbusy the mount point
209 * before calling into namei. Otherwise, unmount might be
210 * started between two vfs_busy() invocations (first is our,
211 * second is from mount point cross-walk code in lookup()),
214 * Require that Q_QUOTAON handles the vfs_busy() reference on
215 * its own, always returning with ubusied mount point.
217 if ((uap->cmd >> SUBCMDSHIFT) != Q_QUOTAON)
219 VFS_UNLOCK_GIANT(vfslocked);
224 * Used by statfs conversion routines to scale the block size up if
225 * necessary so that all of the block counts are <= 'max_size'. Note
226 * that 'max_size' should be a bitmask, i.e. 2^n - 1 for some non-zero
230 statfs_scale_blocks(struct statfs *sf, long max_size)
235 KASSERT(powerof2(max_size + 1), ("%s: invalid max_size", __func__));
238 * Attempt to scale the block counts to give a more accurate
239 * overview to userland of the ratio of free space to used
240 * space. To do this, find the largest block count and compute
241 * a divisor that lets it fit into a signed integer <= max_size.
243 if (sf->f_bavail < 0)
244 count = -sf->f_bavail;
246 count = sf->f_bavail;
247 count = MAX(sf->f_blocks, MAX(sf->f_bfree, count));
248 if (count <= max_size)
251 count >>= flsl(max_size);
258 sf->f_bsize <<= shift;
259 sf->f_blocks >>= shift;
260 sf->f_bfree >>= shift;
261 sf->f_bavail >>= shift;
265 * Get filesystem statistics.
267 #ifndef _SYS_SYSPROTO_H_
276 register struct statfs_args /* {
284 error = kern_statfs(td, uap->path, UIO_USERSPACE, &sf);
286 error = copyout(&sf, uap->buf, sizeof(sf));
291 kern_statfs(struct thread *td, char *path, enum uio_seg pathseg,
295 struct statfs *sp, sb;
300 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | MPSAFE |
301 AUDITVNODE1, pathseg, path, td);
305 vfslocked = NDHASGIANT(&nd);
306 mp = nd.ni_vp->v_mount;
308 NDFREE(&nd, NDF_ONLY_PNBUF);
310 error = vfs_busy(mp, 0);
313 VFS_UNLOCK_GIANT(vfslocked);
317 error = mac_mount_check_stat(td->td_ucred, mp);
322 * Set these in case the underlying filesystem fails to do so.
325 sp->f_version = STATFS_VERSION;
326 sp->f_namemax = NAME_MAX;
327 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
328 error = VFS_STATFS(mp, sp);
331 if (priv_check(td, PRIV_VFS_GENERATION)) {
332 bcopy(sp, &sb, sizeof(sb));
333 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
334 prison_enforce_statfs(td->td_ucred, mp, &sb);
340 VFS_UNLOCK_GIANT(vfslocked);
345 * Get filesystem statistics.
347 #ifndef _SYS_SYSPROTO_H_
348 struct fstatfs_args {
356 register struct fstatfs_args /* {
364 error = kern_fstatfs(td, uap->fd, &sf);
366 error = copyout(&sf, uap->buf, sizeof(sf));
371 kern_fstatfs(struct thread *td, int fd, struct statfs *buf)
375 struct statfs *sp, sb;
381 error = getvnode(td->td_proc->p_fd, fd, CAP_FSTATFS, &fp);
385 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
386 vn_lock(vp, LK_SHARED | LK_RETRY);
388 AUDIT_ARG_VNODE1(vp);
399 error = vfs_busy(mp, 0);
402 VFS_UNLOCK_GIANT(vfslocked);
406 error = mac_mount_check_stat(td->td_ucred, mp);
411 * Set these in case the underlying filesystem fails to do so.
414 sp->f_version = STATFS_VERSION;
415 sp->f_namemax = NAME_MAX;
416 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
417 error = VFS_STATFS(mp, sp);
420 if (priv_check(td, PRIV_VFS_GENERATION)) {
421 bcopy(sp, &sb, sizeof(sb));
422 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
423 prison_enforce_statfs(td->td_ucred, mp, &sb);
430 VFS_UNLOCK_GIANT(vfslocked);
435 * Get statistics on all filesystems.
437 #ifndef _SYS_SYSPROTO_H_
438 struct getfsstat_args {
445 sys_getfsstat(td, uap)
447 register struct getfsstat_args /* {
454 return (kern_getfsstat(td, &uap->buf, uap->bufsize, UIO_USERSPACE,
459 * If (bufsize > 0 && bufseg == UIO_SYSSPACE)
460 * The caller is responsible for freeing memory which will be allocated
464 kern_getfsstat(struct thread *td, struct statfs **buf, size_t bufsize,
465 enum uio_seg bufseg, int flags)
467 struct mount *mp, *nmp;
468 struct statfs *sfsp, *sp, sb;
469 size_t count, maxcount;
473 maxcount = bufsize / sizeof(struct statfs);
476 else if (bufseg == UIO_USERSPACE)
478 else /* if (bufseg == UIO_SYSSPACE) */ {
480 mtx_lock(&mountlist_mtx);
481 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
484 mtx_unlock(&mountlist_mtx);
485 if (maxcount > count)
487 sfsp = *buf = malloc(maxcount * sizeof(struct statfs), M_TEMP,
491 mtx_lock(&mountlist_mtx);
492 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
493 if (prison_canseemount(td->td_ucred, mp) != 0) {
494 nmp = TAILQ_NEXT(mp, mnt_list);
498 if (mac_mount_check_stat(td->td_ucred, mp) != 0) {
499 nmp = TAILQ_NEXT(mp, mnt_list);
503 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK)) {
504 nmp = TAILQ_NEXT(mp, mnt_list);
507 vfslocked = VFS_LOCK_GIANT(mp);
508 if (sfsp && count < maxcount) {
511 * Set these in case the underlying filesystem
514 sp->f_version = STATFS_VERSION;
515 sp->f_namemax = NAME_MAX;
516 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
518 * If MNT_NOWAIT or MNT_LAZY is specified, do not
519 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
520 * overrides MNT_WAIT.
522 if (((flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
523 (flags & MNT_WAIT)) &&
524 (error = VFS_STATFS(mp, sp))) {
525 VFS_UNLOCK_GIANT(vfslocked);
526 mtx_lock(&mountlist_mtx);
527 nmp = TAILQ_NEXT(mp, mnt_list);
531 if (priv_check(td, PRIV_VFS_GENERATION)) {
532 bcopy(sp, &sb, sizeof(sb));
533 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
534 prison_enforce_statfs(td->td_ucred, mp, &sb);
537 if (bufseg == UIO_SYSSPACE)
538 bcopy(sp, sfsp, sizeof(*sp));
539 else /* if (bufseg == UIO_USERSPACE) */ {
540 error = copyout(sp, sfsp, sizeof(*sp));
543 VFS_UNLOCK_GIANT(vfslocked);
549 VFS_UNLOCK_GIANT(vfslocked);
551 mtx_lock(&mountlist_mtx);
552 nmp = TAILQ_NEXT(mp, mnt_list);
555 mtx_unlock(&mountlist_mtx);
556 if (sfsp && count > maxcount)
557 td->td_retval[0] = maxcount;
559 td->td_retval[0] = count;
563 #ifdef COMPAT_FREEBSD4
565 * Get old format filesystem statistics.
567 static void cvtstatfs(struct statfs *, struct ostatfs *);
569 #ifndef _SYS_SYSPROTO_H_
570 struct freebsd4_statfs_args {
576 freebsd4_statfs(td, uap)
578 struct freebsd4_statfs_args /* {
587 error = kern_statfs(td, uap->path, UIO_USERSPACE, &sf);
590 cvtstatfs(&sf, &osb);
591 return (copyout(&osb, uap->buf, sizeof(osb)));
595 * Get filesystem statistics.
597 #ifndef _SYS_SYSPROTO_H_
598 struct freebsd4_fstatfs_args {
604 freebsd4_fstatfs(td, uap)
606 struct freebsd4_fstatfs_args /* {
615 error = kern_fstatfs(td, uap->fd, &sf);
618 cvtstatfs(&sf, &osb);
619 return (copyout(&osb, uap->buf, sizeof(osb)));
623 * Get statistics on all filesystems.
625 #ifndef _SYS_SYSPROTO_H_
626 struct freebsd4_getfsstat_args {
633 freebsd4_getfsstat(td, uap)
635 register struct freebsd4_getfsstat_args /* {
641 struct statfs *buf, *sp;
646 count = uap->bufsize / sizeof(struct ostatfs);
647 size = count * sizeof(struct statfs);
648 error = kern_getfsstat(td, &buf, size, UIO_SYSSPACE, uap->flags);
650 count = td->td_retval[0];
652 while (count > 0 && error == 0) {
654 error = copyout(&osb, uap->buf, sizeof(osb));
665 * Implement fstatfs() for (NFS) file handles.
667 #ifndef _SYS_SYSPROTO_H_
668 struct freebsd4_fhstatfs_args {
669 struct fhandle *u_fhp;
674 freebsd4_fhstatfs(td, uap)
676 struct freebsd4_fhstatfs_args /* {
677 struct fhandle *u_fhp;
686 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
689 error = kern_fhstatfs(td, fh, &sf);
692 cvtstatfs(&sf, &osb);
693 return (copyout(&osb, uap->buf, sizeof(osb)));
697 * Convert a new format statfs structure to an old format statfs structure.
705 statfs_scale_blocks(nsp, LONG_MAX);
706 bzero(osp, sizeof(*osp));
707 osp->f_bsize = nsp->f_bsize;
708 osp->f_iosize = MIN(nsp->f_iosize, LONG_MAX);
709 osp->f_blocks = nsp->f_blocks;
710 osp->f_bfree = nsp->f_bfree;
711 osp->f_bavail = nsp->f_bavail;
712 osp->f_files = MIN(nsp->f_files, LONG_MAX);
713 osp->f_ffree = MIN(nsp->f_ffree, LONG_MAX);
714 osp->f_owner = nsp->f_owner;
715 osp->f_type = nsp->f_type;
716 osp->f_flags = nsp->f_flags;
717 osp->f_syncwrites = MIN(nsp->f_syncwrites, LONG_MAX);
718 osp->f_asyncwrites = MIN(nsp->f_asyncwrites, LONG_MAX);
719 osp->f_syncreads = MIN(nsp->f_syncreads, LONG_MAX);
720 osp->f_asyncreads = MIN(nsp->f_asyncreads, LONG_MAX);
721 strlcpy(osp->f_fstypename, nsp->f_fstypename,
722 MIN(MFSNAMELEN, OMFSNAMELEN));
723 strlcpy(osp->f_mntonname, nsp->f_mntonname,
724 MIN(MNAMELEN, OMNAMELEN));
725 strlcpy(osp->f_mntfromname, nsp->f_mntfromname,
726 MIN(MNAMELEN, OMNAMELEN));
727 osp->f_fsid = nsp->f_fsid;
729 #endif /* COMPAT_FREEBSD4 */
732 * Change current working directory to a given file descriptor.
734 #ifndef _SYS_SYSPROTO_H_
742 struct fchdir_args /* {
746 register struct filedesc *fdp = td->td_proc->p_fd;
747 struct vnode *vp, *tdp, *vpold;
753 AUDIT_ARG_FD(uap->fd);
754 if ((error = getvnode(fdp, uap->fd, CAP_FCHDIR, &fp)) != 0)
759 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
760 vn_lock(vp, LK_SHARED | LK_RETRY);
761 AUDIT_ARG_VNODE1(vp);
762 error = change_dir(vp, td);
763 while (!error && (mp = vp->v_mountedhere) != NULL) {
767 tvfslocked = VFS_LOCK_GIANT(mp);
768 error = VFS_ROOT(mp, LK_SHARED, &tdp);
771 VFS_UNLOCK_GIANT(tvfslocked);
775 VFS_UNLOCK_GIANT(vfslocked);
777 vfslocked = tvfslocked;
781 VFS_UNLOCK_GIANT(vfslocked);
785 VFS_UNLOCK_GIANT(vfslocked);
787 vpold = fdp->fd_cdir;
789 FILEDESC_XUNLOCK(fdp);
790 vfslocked = VFS_LOCK_GIANT(vpold->v_mount);
792 VFS_UNLOCK_GIANT(vfslocked);
797 * Change current working directory (``.'').
799 #ifndef _SYS_SYSPROTO_H_
807 struct chdir_args /* {
812 return (kern_chdir(td, uap->path, UIO_USERSPACE));
816 kern_chdir(struct thread *td, char *path, enum uio_seg pathseg)
818 register struct filedesc *fdp = td->td_proc->p_fd;
824 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1 |
825 MPSAFE, pathseg, path, td);
826 if ((error = namei(&nd)) != 0)
828 vfslocked = NDHASGIANT(&nd);
829 if ((error = change_dir(nd.ni_vp, td)) != 0) {
831 VFS_UNLOCK_GIANT(vfslocked);
832 NDFREE(&nd, NDF_ONLY_PNBUF);
835 VOP_UNLOCK(nd.ni_vp, 0);
836 VFS_UNLOCK_GIANT(vfslocked);
837 NDFREE(&nd, NDF_ONLY_PNBUF);
840 fdp->fd_cdir = nd.ni_vp;
841 FILEDESC_XUNLOCK(fdp);
842 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
844 VFS_UNLOCK_GIANT(vfslocked);
849 * Helper function for raised chroot(2) security function: Refuse if
850 * any filedescriptors are open directories.
853 chroot_refuse_vdir_fds(fdp)
854 struct filedesc *fdp;
860 FILEDESC_LOCK_ASSERT(fdp);
862 for (fd = 0; fd < fdp->fd_nfiles ; fd++) {
863 fp = fget_locked(fdp, fd);
866 if (fp->f_type == DTYPE_VNODE) {
868 if (vp->v_type == VDIR)
876 * This sysctl determines if we will allow a process to chroot(2) if it
877 * has a directory open:
878 * 0: disallowed for all processes.
879 * 1: allowed for processes that were not already chroot(2)'ed.
880 * 2: allowed for all processes.
883 static int chroot_allow_open_directories = 1;
885 SYSCTL_INT(_kern, OID_AUTO, chroot_allow_open_directories, CTLFLAG_RW,
886 &chroot_allow_open_directories, 0, "");
889 * Change notion of root (``/'') directory.
891 #ifndef _SYS_SYSPROTO_H_
899 struct chroot_args /* {
907 error = priv_check(td, PRIV_VFS_CHROOT);
910 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | MPSAFE |
911 AUDITVNODE1, UIO_USERSPACE, uap->path, td);
915 vfslocked = NDHASGIANT(&nd);
916 if ((error = change_dir(nd.ni_vp, td)) != 0)
919 if ((error = mac_vnode_check_chroot(td->td_ucred, nd.ni_vp)))
922 VOP_UNLOCK(nd.ni_vp, 0);
923 error = change_root(nd.ni_vp, td);
925 VFS_UNLOCK_GIANT(vfslocked);
926 NDFREE(&nd, NDF_ONLY_PNBUF);
930 VFS_UNLOCK_GIANT(vfslocked);
932 NDFREE(&nd, NDF_ONLY_PNBUF);
937 * Common routine for chroot and chdir. Callers must provide a locked vnode
947 ASSERT_VOP_LOCKED(vp, "change_dir(): vp not locked");
948 if (vp->v_type != VDIR)
951 error = mac_vnode_check_chdir(td->td_ucred, vp);
955 error = VOP_ACCESS(vp, VEXEC, td->td_ucred, td);
960 * Common routine for kern_chroot() and jail_attach(). The caller is
961 * responsible for invoking priv_check() and mac_vnode_check_chroot() to
962 * authorize this operation.
969 struct filedesc *fdp;
974 VFS_ASSERT_GIANT(vp->v_mount);
975 fdp = td->td_proc->p_fd;
977 if (chroot_allow_open_directories == 0 ||
978 (chroot_allow_open_directories == 1 && fdp->fd_rdir != rootvnode)) {
979 error = chroot_refuse_vdir_fds(fdp);
981 FILEDESC_XUNLOCK(fdp);
985 oldvp = fdp->fd_rdir;
992 FILEDESC_XUNLOCK(fdp);
993 vfslocked = VFS_LOCK_GIANT(oldvp->v_mount);
995 VFS_UNLOCK_GIANT(vfslocked);
999 static __inline cap_rights_t
1000 flags_to_rights(int flags)
1002 cap_rights_t rights = 0;
1004 switch ((flags & O_ACCMODE)) {
1014 rights |= CAP_WRITE;
1018 rights |= CAP_FEXECVE;
1022 if (flags & O_CREAT)
1023 rights |= CAP_CREATE;
1025 if (flags & O_TRUNC)
1026 rights |= CAP_FTRUNCATE;
1028 if ((flags & O_EXLOCK) || (flags & O_SHLOCK))
1029 rights |= CAP_FLOCK;
1035 * Check permissions, allocate an open file structure, and call the device
1036 * open routine if any.
1038 #ifndef _SYS_SYSPROTO_H_
1048 register struct open_args /* {
1055 return (kern_open(td, uap->path, UIO_USERSPACE, uap->flags, uap->mode));
1058 #ifndef _SYS_SYSPROTO_H_
1059 struct openat_args {
1067 sys_openat(struct thread *td, struct openat_args *uap)
1070 return (kern_openat(td, uap->fd, uap->path, UIO_USERSPACE, uap->flag,
1075 kern_open(struct thread *td, char *path, enum uio_seg pathseg, int flags,
1079 return (kern_openat(td, AT_FDCWD, path, pathseg, flags, mode));
1083 kern_openat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1084 int flags, int mode)
1086 struct proc *p = td->td_proc;
1087 struct filedesc *fdp = p->p_fd;
1092 int type, indx = -1, error, error_open;
1094 struct nameidata nd;
1096 cap_rights_t rights_needed = CAP_LOOKUP;
1098 AUDIT_ARG_FFLAGS(flags);
1099 AUDIT_ARG_MODE(mode);
1100 /* XXX: audit dirfd */
1101 rights_needed |= flags_to_rights(flags);
1103 * Only one of the O_EXEC, O_RDONLY, O_WRONLY and O_RDWR flags
1106 if (flags & O_EXEC) {
1107 if (flags & O_ACCMODE)
1109 } else if ((flags & O_ACCMODE) == O_ACCMODE)
1112 flags = FFLAGS(flags);
1115 * allocate the file descriptor, but don't install a descriptor yet
1117 error = falloc_noinstall(td, &nfp);
1120 /* An extra reference on `nfp' has been held for us by falloc_noinstall(). */
1122 /* Set the flags early so the finit in devfs can pick them up. */
1123 fp->f_flag = flags & FMASK;
1124 cmode = ((mode &~ fdp->fd_cmask) & ALLPERMS) &~ S_ISTXT;
1125 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | AUDITVNODE1 | MPSAFE, pathseg,
1126 path, fd, rights_needed, td);
1127 td->td_dupfd = -1; /* XXX check for fdopen */
1128 error = vn_open(&nd, &flags, cmode, fp);
1131 * If the vn_open replaced the method vector, something
1132 * wonderous happened deep below and we just pass it up
1133 * pretending we know what we do.
1135 if (error == ENXIO && fp->f_ops != &badfileops)
1139 * handle special fdopen() case. bleh. dupfdopen() is
1140 * responsible for dropping the old contents of ofiles[indx]
1143 * Don't do this for relative (capability) lookups; we don't
1144 * understand exactly what would happen, and we don't think
1145 * that it ever should.
1147 if ((nd.ni_strictrelative == 0) &&
1148 (error == ENODEV || error == ENXIO) &&
1149 (td->td_dupfd >= 0)) {
1150 /* XXX from fdopen */
1152 if ((error = finstall(td, fp, &indx, flags)) != 0)
1154 if ((error = dupfdopen(td, fdp, indx, td->td_dupfd,
1155 flags, error_open)) == 0)
1159 * Clean up the descriptor, but only if another thread hadn't
1160 * replaced or closed it.
1163 fdclose(fdp, fp, indx, td);
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 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_UNLINK);
1949 error = VOP_REMOVE(nd.ni_dvp, vp, &nd.ni_cnd);
1953 vn_finished_write(mp);
1955 NDFREE(&nd, NDF_ONLY_PNBUF);
1957 if (vp == nd.ni_dvp)
1961 VFS_UNLOCK_GIANT(vfslocked);
1966 * Reposition read/write file offset.
1968 #ifndef _SYS_SYSPROTO_H_
1979 register struct lseek_args /* {
1986 struct ucred *cred = td->td_ucred;
1990 off_t foffset, offset, size;
1994 AUDIT_ARG_FD(uap->fd);
1995 if ((error = fget(td, uap->fd, CAP_SEEK, &fp)) != 0)
1997 if (!(fp->f_ops->fo_flags & DFLAG_SEEKABLE)) {
2002 foffset = foffset_lock(fp, 0);
2003 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
2004 noneg = (vp->v_type != VCHR);
2005 offset = uap->offset;
2006 switch (uap->whence) {
2010 (offset > 0 && foffset > OFF_MAX - offset))) {
2017 vn_lock(vp, LK_SHARED | LK_RETRY);
2018 error = VOP_GETATTR(vp, &vattr, cred);
2024 * If the file references a disk device, then fetch
2025 * the media size and use that to determine the ending
2028 if (vattr.va_size == 0 && vp->v_type == VCHR &&
2029 fo_ioctl(fp, DIOCGMEDIASIZE, &size, cred, td) == 0)
2030 vattr.va_size = size;
2032 (vattr.va_size > OFF_MAX ||
2033 (offset > 0 && vattr.va_size > OFF_MAX - offset))) {
2037 offset += vattr.va_size;
2042 error = fo_ioctl(fp, FIOSEEKDATA, &offset, cred, td);
2045 error = fo_ioctl(fp, FIOSEEKHOLE, &offset, cred, td);
2050 if (error == 0 && noneg && offset < 0)
2054 VFS_KNOTE_UNLOCKED(vp, 0);
2055 *(off_t *)(td->td_retval) = offset;
2058 VFS_UNLOCK_GIANT(vfslocked);
2059 foffset_unlock(fp, offset, error != 0 ? FOF_NOUPDATE : 0);
2063 #if defined(COMPAT_43)
2065 * Reposition read/write file offset.
2067 #ifndef _SYS_SYSPROTO_H_
2068 struct olseek_args {
2077 register struct olseek_args /* {
2083 struct lseek_args /* {
2091 nuap.offset = uap->offset;
2092 nuap.whence = uap->whence;
2093 return (sys_lseek(td, &nuap));
2095 #endif /* COMPAT_43 */
2097 /* Version with the 'pad' argument */
2099 freebsd6_lseek(td, uap)
2101 register struct freebsd6_lseek_args *uap;
2103 struct lseek_args ouap;
2106 ouap.offset = uap->offset;
2107 ouap.whence = uap->whence;
2108 return (sys_lseek(td, &ouap));
2112 * Check access permissions using passed credentials.
2115 vn_access(vp, user_flags, cred, td)
2124 /* Flags == 0 means only check for existence. */
2128 if (user_flags & R_OK)
2130 if (user_flags & W_OK)
2132 if (user_flags & X_OK)
2135 error = mac_vnode_check_access(cred, vp, accmode);
2139 if ((accmode & VWRITE) == 0 || (error = vn_writechk(vp)) == 0)
2140 error = VOP_ACCESS(vp, accmode, cred, td);
2146 * Check access permissions using "real" credentials.
2148 #ifndef _SYS_SYSPROTO_H_
2149 struct access_args {
2157 register struct access_args /* {
2163 return (kern_access(td, uap->path, UIO_USERSPACE, uap->flags));
2166 #ifndef _SYS_SYSPROTO_H_
2167 struct faccessat_args {
2175 sys_faccessat(struct thread *td, struct faccessat_args *uap)
2178 if (uap->flag & ~AT_EACCESS)
2180 return (kern_accessat(td, uap->fd, uap->path, UIO_USERSPACE, uap->flag,
2185 kern_access(struct thread *td, char *path, enum uio_seg pathseg, int mode)
2188 return (kern_accessat(td, AT_FDCWD, path, pathseg, 0, mode));
2192 kern_accessat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2193 int flags, int mode)
2195 struct ucred *cred, *tmpcred;
2197 struct nameidata nd;
2202 * Create and modify a temporary credential instead of one that
2203 * is potentially shared.
2205 if (!(flags & AT_EACCESS)) {
2206 cred = td->td_ucred;
2207 tmpcred = crdup(cred);
2208 tmpcred->cr_uid = cred->cr_ruid;
2209 tmpcred->cr_groups[0] = cred->cr_rgid;
2210 td->td_ucred = tmpcred;
2212 cred = tmpcred = td->td_ucred;
2213 AUDIT_ARG_VALUE(mode);
2214 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | MPSAFE |
2215 AUDITVNODE1, pathseg, path, fd, CAP_FSTAT, td);
2216 if ((error = namei(&nd)) != 0)
2218 vfslocked = NDHASGIANT(&nd);
2221 error = vn_access(vp, mode, tmpcred, td);
2222 NDFREE(&nd, NDF_ONLY_PNBUF);
2224 VFS_UNLOCK_GIANT(vfslocked);
2226 if (!(flags & AT_EACCESS)) {
2227 td->td_ucred = cred;
2234 * Check access permissions using "effective" credentials.
2236 #ifndef _SYS_SYSPROTO_H_
2237 struct eaccess_args {
2243 sys_eaccess(td, uap)
2245 register struct eaccess_args /* {
2251 return (kern_eaccess(td, uap->path, UIO_USERSPACE, uap->flags));
2255 kern_eaccess(struct thread *td, char *path, enum uio_seg pathseg, int flags)
2258 return (kern_accessat(td, AT_FDCWD, path, pathseg, AT_EACCESS, flags));
2261 #if defined(COMPAT_43)
2263 * Get file status; this version follows links.
2265 #ifndef _SYS_SYSPROTO_H_
2274 register struct ostat_args /* {
2283 error = kern_stat(td, uap->path, UIO_USERSPACE, &sb);
2287 error = copyout(&osb, uap->ub, sizeof (osb));
2292 * Get file status; this version does not follow links.
2294 #ifndef _SYS_SYSPROTO_H_
2295 struct olstat_args {
2303 register struct olstat_args /* {
2312 error = kern_lstat(td, uap->path, UIO_USERSPACE, &sb);
2316 error = copyout(&osb, uap->ub, sizeof (osb));
2321 * Convert from an old to a new stat structure.
2329 bzero(ost, sizeof(*ost));
2330 ost->st_dev = st->st_dev;
2331 ost->st_ino = st->st_ino;
2332 ost->st_mode = st->st_mode;
2333 ost->st_nlink = st->st_nlink;
2334 ost->st_uid = st->st_uid;
2335 ost->st_gid = st->st_gid;
2336 ost->st_rdev = st->st_rdev;
2337 if (st->st_size < (quad_t)1 << 32)
2338 ost->st_size = st->st_size;
2341 ost->st_atim = st->st_atim;
2342 ost->st_mtim = st->st_mtim;
2343 ost->st_ctim = st->st_ctim;
2344 ost->st_blksize = st->st_blksize;
2345 ost->st_blocks = st->st_blocks;
2346 ost->st_flags = st->st_flags;
2347 ost->st_gen = st->st_gen;
2349 #endif /* COMPAT_43 */
2352 * Get file status; this version follows links.
2354 #ifndef _SYS_SYSPROTO_H_
2363 register struct stat_args /* {
2371 error = kern_stat(td, uap->path, UIO_USERSPACE, &sb);
2373 error = copyout(&sb, uap->ub, sizeof (sb));
2377 #ifndef _SYS_SYSPROTO_H_
2378 struct fstatat_args {
2386 sys_fstatat(struct thread *td, struct fstatat_args *uap)
2391 error = kern_statat(td, uap->flag, uap->fd, uap->path,
2392 UIO_USERSPACE, &sb);
2394 error = copyout(&sb, uap->buf, sizeof (sb));
2399 kern_stat(struct thread *td, char *path, enum uio_seg pathseg, struct stat *sbp)
2402 return (kern_statat(td, 0, AT_FDCWD, path, pathseg, sbp));
2406 kern_statat(struct thread *td, int flag, int fd, char *path,
2407 enum uio_seg pathseg, struct stat *sbp)
2410 return (kern_statat_vnhook(td, flag, fd, path, pathseg, sbp, NULL));
2414 kern_statat_vnhook(struct thread *td, int flag, int fd, char *path,
2415 enum uio_seg pathseg, struct stat *sbp,
2416 void (*hook)(struct vnode *vp, struct stat *sbp))
2418 struct nameidata nd;
2420 int error, vfslocked;
2422 if (flag & ~AT_SYMLINK_NOFOLLOW)
2425 NDINIT_ATRIGHTS(&nd, LOOKUP, ((flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW :
2426 FOLLOW) | LOCKSHARED | LOCKLEAF | AUDITVNODE1 | MPSAFE, pathseg,
2427 path, fd, CAP_FSTAT, td);
2429 if ((error = namei(&nd)) != 0)
2431 vfslocked = NDHASGIANT(&nd);
2432 error = vn_stat(nd.ni_vp, &sb, td->td_ucred, NOCRED, td);
2434 SDT_PROBE(vfs, , stat, mode, path, sb.st_mode, 0, 0, 0);
2435 if (S_ISREG(sb.st_mode))
2436 SDT_PROBE(vfs, , stat, reg, path, pathseg, 0, 0, 0);
2437 if (__predict_false(hook != NULL))
2438 hook(nd.ni_vp, &sb);
2440 NDFREE(&nd, NDF_ONLY_PNBUF);
2442 VFS_UNLOCK_GIANT(vfslocked);
2447 if (KTRPOINT(td, KTR_STRUCT))
2454 * Get file status; this version does not follow links.
2456 #ifndef _SYS_SYSPROTO_H_
2465 register struct lstat_args /* {
2473 error = kern_lstat(td, uap->path, UIO_USERSPACE, &sb);
2475 error = copyout(&sb, uap->ub, sizeof (sb));
2480 kern_lstat(struct thread *td, char *path, enum uio_seg pathseg, struct stat *sbp)
2483 return (kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, path, pathseg,
2488 * Implementation of the NetBSD [l]stat() functions.
2495 bzero(nsb, sizeof *nsb);
2496 nsb->st_dev = sb->st_dev;
2497 nsb->st_ino = sb->st_ino;
2498 nsb->st_mode = sb->st_mode;
2499 nsb->st_nlink = sb->st_nlink;
2500 nsb->st_uid = sb->st_uid;
2501 nsb->st_gid = sb->st_gid;
2502 nsb->st_rdev = sb->st_rdev;
2503 nsb->st_atim = sb->st_atim;
2504 nsb->st_mtim = sb->st_mtim;
2505 nsb->st_ctim = sb->st_ctim;
2506 nsb->st_size = sb->st_size;
2507 nsb->st_blocks = sb->st_blocks;
2508 nsb->st_blksize = sb->st_blksize;
2509 nsb->st_flags = sb->st_flags;
2510 nsb->st_gen = sb->st_gen;
2511 nsb->st_birthtim = sb->st_birthtim;
2514 #ifndef _SYS_SYSPROTO_H_
2523 register struct nstat_args /* {
2532 error = kern_stat(td, uap->path, UIO_USERSPACE, &sb);
2535 cvtnstat(&sb, &nsb);
2536 error = copyout(&nsb, uap->ub, sizeof (nsb));
2541 * NetBSD lstat. Get file status; this version does not follow links.
2543 #ifndef _SYS_SYSPROTO_H_
2552 register struct nlstat_args /* {
2561 error = kern_lstat(td, uap->path, UIO_USERSPACE, &sb);
2564 cvtnstat(&sb, &nsb);
2565 error = copyout(&nsb, uap->ub, sizeof (nsb));
2570 * Get configurable pathname variables.
2572 #ifndef _SYS_SYSPROTO_H_
2573 struct pathconf_args {
2579 sys_pathconf(td, uap)
2581 register struct pathconf_args /* {
2587 return (kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name, FOLLOW));
2590 #ifndef _SYS_SYSPROTO_H_
2591 struct lpathconf_args {
2597 sys_lpathconf(td, uap)
2599 register struct lpathconf_args /* {
2605 return (kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name, NOFOLLOW));
2609 kern_pathconf(struct thread *td, char *path, enum uio_seg pathseg, int name,
2612 struct nameidata nd;
2613 int error, vfslocked;
2615 NDINIT(&nd, LOOKUP, LOCKSHARED | LOCKLEAF | MPSAFE | AUDITVNODE1 |
2616 flags, pathseg, path, td);
2617 if ((error = namei(&nd)) != 0)
2619 vfslocked = NDHASGIANT(&nd);
2620 NDFREE(&nd, NDF_ONLY_PNBUF);
2622 /* If asynchronous I/O is available, it works for all files. */
2623 if (name == _PC_ASYNC_IO)
2624 td->td_retval[0] = async_io_version;
2626 error = VOP_PATHCONF(nd.ni_vp, name, td->td_retval);
2628 VFS_UNLOCK_GIANT(vfslocked);
2633 * Return target name of a symbolic link.
2635 #ifndef _SYS_SYSPROTO_H_
2636 struct readlink_args {
2643 sys_readlink(td, uap)
2645 register struct readlink_args /* {
2652 return (kern_readlink(td, uap->path, UIO_USERSPACE, uap->buf,
2653 UIO_USERSPACE, uap->count));
2655 #ifndef _SYS_SYSPROTO_H_
2656 struct readlinkat_args {
2664 sys_readlinkat(struct thread *td, struct readlinkat_args *uap)
2667 return (kern_readlinkat(td, uap->fd, uap->path, UIO_USERSPACE,
2668 uap->buf, UIO_USERSPACE, uap->bufsize));
2672 kern_readlink(struct thread *td, char *path, enum uio_seg pathseg, char *buf,
2673 enum uio_seg bufseg, size_t count)
2676 return (kern_readlinkat(td, AT_FDCWD, path, pathseg, buf, bufseg,
2681 kern_readlinkat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2682 char *buf, enum uio_seg bufseg, size_t count)
2688 struct nameidata nd;
2691 if (count > IOSIZE_MAX)
2694 NDINIT_AT(&nd, LOOKUP, NOFOLLOW | LOCKSHARED | LOCKLEAF | MPSAFE |
2695 AUDITVNODE1, pathseg, path, fd, td);
2697 if ((error = namei(&nd)) != 0)
2699 NDFREE(&nd, NDF_ONLY_PNBUF);
2700 vfslocked = NDHASGIANT(&nd);
2703 error = mac_vnode_check_readlink(td->td_ucred, vp);
2706 VFS_UNLOCK_GIANT(vfslocked);
2710 if (vp->v_type != VLNK)
2713 aiov.iov_base = buf;
2714 aiov.iov_len = count;
2715 auio.uio_iov = &aiov;
2716 auio.uio_iovcnt = 1;
2717 auio.uio_offset = 0;
2718 auio.uio_rw = UIO_READ;
2719 auio.uio_segflg = bufseg;
2721 auio.uio_resid = count;
2722 error = VOP_READLINK(vp, &auio, td->td_ucred);
2723 td->td_retval[0] = count - auio.uio_resid;
2726 VFS_UNLOCK_GIANT(vfslocked);
2731 * Common implementation code for chflags() and fchflags().
2734 setfflags(td, vp, flags)
2743 /* We can't support the value matching VNOVAL. */
2744 if (flags == VNOVAL)
2745 return (EOPNOTSUPP);
2748 * Prevent non-root users from setting flags on devices. When
2749 * a device is reused, users can retain ownership of the device
2750 * if they are allowed to set flags and programs assume that
2751 * chown can't fail when done as root.
2753 if (vp->v_type == VCHR || vp->v_type == VBLK) {
2754 error = priv_check(td, PRIV_VFS_CHFLAGS_DEV);
2759 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2761 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2763 vattr.va_flags = flags;
2765 error = mac_vnode_check_setflags(td->td_ucred, vp, vattr.va_flags);
2768 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
2770 vn_finished_write(mp);
2775 * Change flags of a file given a path name.
2777 #ifndef _SYS_SYSPROTO_H_
2778 struct chflags_args {
2784 sys_chflags(td, uap)
2786 register struct chflags_args /* {
2792 struct nameidata nd;
2795 AUDIT_ARG_FFLAGS(uap->flags);
2796 NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE | AUDITVNODE1, UIO_USERSPACE,
2798 if ((error = namei(&nd)) != 0)
2800 NDFREE(&nd, NDF_ONLY_PNBUF);
2801 vfslocked = NDHASGIANT(&nd);
2802 error = setfflags(td, nd.ni_vp, uap->flags);
2804 VFS_UNLOCK_GIANT(vfslocked);
2809 * Same as chflags() but doesn't follow symlinks.
2812 sys_lchflags(td, uap)
2814 register struct lchflags_args /* {
2820 struct nameidata nd;
2823 AUDIT_ARG_FFLAGS(uap->flags);
2824 NDINIT(&nd, LOOKUP, NOFOLLOW | MPSAFE | AUDITVNODE1, UIO_USERSPACE,
2826 if ((error = namei(&nd)) != 0)
2828 vfslocked = NDHASGIANT(&nd);
2829 NDFREE(&nd, NDF_ONLY_PNBUF);
2830 error = setfflags(td, nd.ni_vp, uap->flags);
2832 VFS_UNLOCK_GIANT(vfslocked);
2837 * Change flags of a file given a file descriptor.
2839 #ifndef _SYS_SYSPROTO_H_
2840 struct fchflags_args {
2846 sys_fchflags(td, uap)
2848 register struct fchflags_args /* {
2857 AUDIT_ARG_FD(uap->fd);
2858 AUDIT_ARG_FFLAGS(uap->flags);
2859 if ((error = getvnode(td->td_proc->p_fd, uap->fd, CAP_FCHFLAGS,
2862 vfslocked = VFS_LOCK_GIANT(fp->f_vnode->v_mount);
2864 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
2865 AUDIT_ARG_VNODE1(fp->f_vnode);
2866 VOP_UNLOCK(fp->f_vnode, 0);
2868 error = setfflags(td, fp->f_vnode, uap->flags);
2869 VFS_UNLOCK_GIANT(vfslocked);
2875 * Common implementation code for chmod(), lchmod() and fchmod().
2878 setfmode(td, cred, vp, mode)
2888 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2890 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2892 vattr.va_mode = mode & ALLPERMS;
2894 error = mac_vnode_check_setmode(cred, vp, vattr.va_mode);
2897 error = VOP_SETATTR(vp, &vattr, cred);
2899 vn_finished_write(mp);
2904 * Change mode of a file given path name.
2906 #ifndef _SYS_SYSPROTO_H_
2915 register struct chmod_args /* {
2921 return (kern_chmod(td, uap->path, UIO_USERSPACE, uap->mode));
2924 #ifndef _SYS_SYSPROTO_H_
2925 struct fchmodat_args {
2933 sys_fchmodat(struct thread *td, struct fchmodat_args *uap)
2935 int flag = uap->flag;
2937 char *path = uap->path;
2938 mode_t mode = uap->mode;
2940 if (flag & ~AT_SYMLINK_NOFOLLOW)
2943 return (kern_fchmodat(td, fd, path, UIO_USERSPACE, mode, flag));
2947 kern_chmod(struct thread *td, char *path, enum uio_seg pathseg, int mode)
2950 return (kern_fchmodat(td, AT_FDCWD, path, pathseg, mode, 0));
2954 * Change mode of a file given path name (don't follow links.)
2956 #ifndef _SYS_SYSPROTO_H_
2957 struct lchmod_args {
2965 register struct lchmod_args /* {
2971 return (kern_fchmodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2972 uap->mode, AT_SYMLINK_NOFOLLOW));
2977 kern_fchmodat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2978 mode_t mode, int flag)
2981 struct nameidata nd;
2985 AUDIT_ARG_MODE(mode);
2986 follow = (flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
2987 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | MPSAFE | AUDITVNODE1, pathseg,
2988 path, fd, CAP_FCHMOD, td);
2989 if ((error = namei(&nd)) != 0)
2991 vfslocked = NDHASGIANT(&nd);
2992 NDFREE(&nd, NDF_ONLY_PNBUF);
2993 error = setfmode(td, td->td_ucred, nd.ni_vp, mode);
2995 VFS_UNLOCK_GIANT(vfslocked);
3000 * Change mode of a file given a file descriptor.
3002 #ifndef _SYS_SYSPROTO_H_
3003 struct fchmod_args {
3009 sys_fchmod(struct thread *td, struct fchmod_args *uap)
3014 AUDIT_ARG_FD(uap->fd);
3015 AUDIT_ARG_MODE(uap->mode);
3017 error = fget(td, uap->fd, CAP_FCHMOD, &fp);
3020 error = fo_chmod(fp, uap->mode, td->td_ucred, td);
3026 * Common implementation for chown(), lchown(), and fchown()
3029 setfown(td, cred, vp, uid, gid)
3040 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
3042 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3047 error = mac_vnode_check_setowner(cred, vp, vattr.va_uid,
3051 error = VOP_SETATTR(vp, &vattr, cred);
3053 vn_finished_write(mp);
3058 * Set ownership given a path name.
3060 #ifndef _SYS_SYSPROTO_H_
3070 register struct chown_args /* {
3077 return (kern_chown(td, uap->path, UIO_USERSPACE, uap->uid, uap->gid));
3080 #ifndef _SYS_SYSPROTO_H_
3081 struct fchownat_args {
3090 sys_fchownat(struct thread *td, struct fchownat_args *uap)
3095 if (flag & ~AT_SYMLINK_NOFOLLOW)
3098 return (kern_fchownat(td, uap->fd, uap->path, UIO_USERSPACE, uap->uid,
3099 uap->gid, uap->flag));
3103 kern_chown(struct thread *td, char *path, enum uio_seg pathseg, int uid,
3107 return (kern_fchownat(td, AT_FDCWD, path, pathseg, uid, gid, 0));
3111 kern_fchownat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
3112 int uid, int gid, int flag)
3114 struct nameidata nd;
3115 int error, vfslocked, follow;
3117 AUDIT_ARG_OWNER(uid, gid);
3118 follow = (flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
3119 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | MPSAFE | AUDITVNODE1, pathseg,
3120 path, fd, CAP_FCHOWN, td);
3122 if ((error = namei(&nd)) != 0)
3124 vfslocked = NDHASGIANT(&nd);
3125 NDFREE(&nd, NDF_ONLY_PNBUF);
3126 error = setfown(td, td->td_ucred, nd.ni_vp, uid, gid);
3128 VFS_UNLOCK_GIANT(vfslocked);
3133 * Set ownership given a path name, do not cross symlinks.
3135 #ifndef _SYS_SYSPROTO_H_
3136 struct lchown_args {
3145 register struct lchown_args /* {
3152 return (kern_lchown(td, uap->path, UIO_USERSPACE, uap->uid, uap->gid));
3156 kern_lchown(struct thread *td, char *path, enum uio_seg pathseg, int uid,
3160 return (kern_fchownat(td, AT_FDCWD, path, pathseg, uid, gid,
3161 AT_SYMLINK_NOFOLLOW));
3165 * Set ownership given a file descriptor.
3167 #ifndef _SYS_SYSPROTO_H_
3168 struct fchown_args {
3177 register struct fchown_args /* {
3186 AUDIT_ARG_FD(uap->fd);
3187 AUDIT_ARG_OWNER(uap->uid, uap->gid);
3188 error = fget(td, uap->fd, CAP_FCHOWN, &fp);
3191 error = fo_chown(fp, uap->uid, uap->gid, td->td_ucred, td);
3197 * Common implementation code for utimes(), lutimes(), and futimes().
3200 getutimes(usrtvp, tvpseg, tsp)
3201 const struct timeval *usrtvp;
3202 enum uio_seg tvpseg;
3203 struct timespec *tsp;
3205 struct timeval tv[2];
3206 const struct timeval *tvp;
3209 if (usrtvp == NULL) {
3210 vfs_timestamp(&tsp[0]);
3213 if (tvpseg == UIO_SYSSPACE) {
3216 if ((error = copyin(usrtvp, tv, sizeof(tv))) != 0)
3221 if (tvp[0].tv_usec < 0 || tvp[0].tv_usec >= 1000000 ||
3222 tvp[1].tv_usec < 0 || tvp[1].tv_usec >= 1000000)
3224 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
3225 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
3231 * Common implementation code for utimes(), lutimes(), and futimes().
3234 setutimes(td, vp, ts, numtimes, nullflag)
3237 const struct timespec *ts;
3241 int error, setbirthtime;
3245 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
3247 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3249 if (numtimes < 3 && !VOP_GETATTR(vp, &vattr, td->td_ucred) &&
3250 timespeccmp(&ts[1], &vattr.va_birthtime, < ))
3253 vattr.va_atime = ts[0];
3254 vattr.va_mtime = ts[1];
3256 vattr.va_birthtime = ts[1];
3258 vattr.va_birthtime = ts[2];
3260 vattr.va_vaflags |= VA_UTIMES_NULL;
3262 error = mac_vnode_check_setutimes(td->td_ucred, vp, vattr.va_atime,
3266 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3268 vn_finished_write(mp);
3273 * Set the access and modification times of a file.
3275 #ifndef _SYS_SYSPROTO_H_
3276 struct utimes_args {
3278 struct timeval *tptr;
3284 register struct utimes_args /* {
3286 struct timeval *tptr;
3290 return (kern_utimes(td, uap->path, UIO_USERSPACE, uap->tptr,
3294 #ifndef _SYS_SYSPROTO_H_
3295 struct futimesat_args {
3298 const struct timeval * times;
3302 sys_futimesat(struct thread *td, struct futimesat_args *uap)
3305 return (kern_utimesat(td, uap->fd, uap->path, UIO_USERSPACE,
3306 uap->times, UIO_USERSPACE));
3310 kern_utimes(struct thread *td, char *path, enum uio_seg pathseg,
3311 struct timeval *tptr, enum uio_seg tptrseg)
3314 return (kern_utimesat(td, AT_FDCWD, path, pathseg, tptr, tptrseg));
3318 kern_utimesat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
3319 struct timeval *tptr, enum uio_seg tptrseg)
3321 struct nameidata nd;
3322 struct timespec ts[2];
3323 int error, vfslocked;
3325 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3327 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | MPSAFE | AUDITVNODE1, pathseg,
3328 path, fd, CAP_FUTIMES, td);
3330 if ((error = namei(&nd)) != 0)
3332 vfslocked = NDHASGIANT(&nd);
3333 NDFREE(&nd, NDF_ONLY_PNBUF);
3334 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3336 VFS_UNLOCK_GIANT(vfslocked);
3341 * Set the access and modification times of a file.
3343 #ifndef _SYS_SYSPROTO_H_
3344 struct lutimes_args {
3346 struct timeval *tptr;
3350 sys_lutimes(td, uap)
3352 register struct lutimes_args /* {
3354 struct timeval *tptr;
3358 return (kern_lutimes(td, uap->path, UIO_USERSPACE, uap->tptr,
3363 kern_lutimes(struct thread *td, char *path, enum uio_seg pathseg,
3364 struct timeval *tptr, enum uio_seg tptrseg)
3366 struct timespec ts[2];
3368 struct nameidata nd;
3371 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3373 NDINIT(&nd, LOOKUP, NOFOLLOW | MPSAFE | AUDITVNODE1, pathseg, path, td);
3374 if ((error = namei(&nd)) != 0)
3376 vfslocked = NDHASGIANT(&nd);
3377 NDFREE(&nd, NDF_ONLY_PNBUF);
3378 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3380 VFS_UNLOCK_GIANT(vfslocked);
3385 * Set the access and modification times of a file.
3387 #ifndef _SYS_SYSPROTO_H_
3388 struct futimes_args {
3390 struct timeval *tptr;
3394 sys_futimes(td, uap)
3396 register struct futimes_args /* {
3398 struct timeval *tptr;
3402 return (kern_futimes(td, uap->fd, uap->tptr, UIO_USERSPACE));
3406 kern_futimes(struct thread *td, int fd, struct timeval *tptr,
3407 enum uio_seg tptrseg)
3409 struct timespec ts[2];
3415 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3417 if ((error = getvnode(td->td_proc->p_fd, fd, CAP_FUTIMES, &fp))
3420 vfslocked = VFS_LOCK_GIANT(fp->f_vnode->v_mount);
3422 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
3423 AUDIT_ARG_VNODE1(fp->f_vnode);
3424 VOP_UNLOCK(fp->f_vnode, 0);
3426 error = setutimes(td, fp->f_vnode, ts, 2, tptr == NULL);
3427 VFS_UNLOCK_GIANT(vfslocked);
3433 * Truncate a file given its path name.
3435 #ifndef _SYS_SYSPROTO_H_
3436 struct truncate_args {
3443 sys_truncate(td, uap)
3445 register struct truncate_args /* {
3452 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3456 kern_truncate(struct thread *td, char *path, enum uio_seg pathseg, off_t length)
3462 struct nameidata nd;
3463 int error, vfslocked;
3467 NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE | AUDITVNODE1, pathseg, path, td);
3468 if ((error = namei(&nd)) != 0)
3470 vfslocked = NDHASGIANT(&nd);
3472 rl_cookie = vn_rangelock_wlock(vp, 0, OFF_MAX);
3473 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0) {
3474 vn_rangelock_unlock(vp, rl_cookie);
3476 VFS_UNLOCK_GIANT(vfslocked);
3479 NDFREE(&nd, NDF_ONLY_PNBUF);
3480 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3481 if (vp->v_type == VDIR)
3484 else if ((error = mac_vnode_check_write(td->td_ucred, NOCRED, vp))) {
3487 else if ((error = vn_writechk(vp)) == 0 &&
3488 (error = VOP_ACCESS(vp, VWRITE, td->td_ucred, td)) == 0) {
3490 vattr.va_size = length;
3491 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3494 vn_finished_write(mp);
3495 vn_rangelock_unlock(vp, rl_cookie);
3497 VFS_UNLOCK_GIANT(vfslocked);
3501 #if defined(COMPAT_43)
3503 * Truncate a file given its path name.
3505 #ifndef _SYS_SYSPROTO_H_
3506 struct otruncate_args {
3514 register struct otruncate_args /* {
3519 struct truncate_args /* {
3525 nuap.path = uap->path;
3526 nuap.length = uap->length;
3527 return (sys_truncate(td, &nuap));
3529 #endif /* COMPAT_43 */
3531 /* Versions with the pad argument */
3533 freebsd6_truncate(struct thread *td, struct freebsd6_truncate_args *uap)
3535 struct truncate_args ouap;
3537 ouap.path = uap->path;
3538 ouap.length = uap->length;
3539 return (sys_truncate(td, &ouap));
3543 freebsd6_ftruncate(struct thread *td, struct freebsd6_ftruncate_args *uap)
3545 struct ftruncate_args ouap;
3548 ouap.length = uap->length;
3549 return (sys_ftruncate(td, &ouap));
3553 * Sync an open file.
3555 #ifndef _SYS_SYSPROTO_H_
3563 struct fsync_args /* {
3571 int error, lock_flags;
3573 AUDIT_ARG_FD(uap->fd);
3574 if ((error = getvnode(td->td_proc->p_fd, uap->fd, CAP_FSYNC,
3578 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
3579 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
3581 if (MNT_SHARED_WRITES(mp) ||
3582 ((mp == NULL) && MNT_SHARED_WRITES(vp->v_mount))) {
3583 lock_flags = LK_SHARED;
3585 lock_flags = LK_EXCLUSIVE;
3587 vn_lock(vp, lock_flags | LK_RETRY);
3588 AUDIT_ARG_VNODE1(vp);
3589 if (vp->v_object != NULL) {
3590 VM_OBJECT_LOCK(vp->v_object);
3591 vm_object_page_clean(vp->v_object, 0, 0, 0);
3592 VM_OBJECT_UNLOCK(vp->v_object);
3594 error = VOP_FSYNC(vp, MNT_WAIT, td);
3597 vn_finished_write(mp);
3599 VFS_UNLOCK_GIANT(vfslocked);
3605 * Rename files. Source and destination must either both be directories, or
3606 * both not be directories. If target is a directory, it must be empty.
3608 #ifndef _SYS_SYSPROTO_H_
3609 struct rename_args {
3617 register struct rename_args /* {
3623 return (kern_rename(td, uap->from, uap->to, UIO_USERSPACE));
3626 #ifndef _SYS_SYSPROTO_H_
3627 struct renameat_args {
3635 sys_renameat(struct thread *td, struct renameat_args *uap)
3638 return (kern_renameat(td, uap->oldfd, uap->old, uap->newfd, uap->new,
3643 kern_rename(struct thread *td, char *from, char *to, enum uio_seg pathseg)
3646 return (kern_renameat(td, AT_FDCWD, from, AT_FDCWD, to, pathseg));
3650 kern_renameat(struct thread *td, int oldfd, char *old, int newfd, char *new,
3651 enum uio_seg pathseg)
3653 struct mount *mp = NULL;
3654 struct vnode *tvp, *fvp, *tdvp;
3655 struct nameidata fromnd, tond;
3662 NDINIT_ATRIGHTS(&fromnd, DELETE, LOCKPARENT | LOCKLEAF | SAVESTART |
3663 MPSAFE | AUDITVNODE1, pathseg, old, oldfd, CAP_DELETE, td);
3665 NDINIT_ATRIGHTS(&fromnd, DELETE, WANTPARENT | SAVESTART | MPSAFE |
3666 AUDITVNODE1, pathseg, old, oldfd, CAP_DELETE, td);
3669 if ((error = namei(&fromnd)) != 0)
3671 fvfslocked = NDHASGIANT(&fromnd);
3674 error = mac_vnode_check_rename_from(td->td_ucred, fromnd.ni_dvp,
3675 fromnd.ni_vp, &fromnd.ni_cnd);
3676 VOP_UNLOCK(fromnd.ni_dvp, 0);
3677 if (fromnd.ni_dvp != fromnd.ni_vp)
3678 VOP_UNLOCK(fromnd.ni_vp, 0);
3682 error = vn_start_write(fvp, &mp, V_WAIT | PCATCH);
3684 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3685 vrele(fromnd.ni_dvp);
3689 NDINIT_ATRIGHTS(&tond, RENAME, LOCKPARENT | LOCKLEAF | NOCACHE |
3690 SAVESTART | MPSAFE | AUDITVNODE2, pathseg, new, newfd, CAP_CREATE,
3692 if (fromnd.ni_vp->v_type == VDIR)
3693 tond.ni_cnd.cn_flags |= WILLBEDIR;
3694 if ((error = namei(&tond)) != 0) {
3695 /* Translate error code for rename("dir1", "dir2/."). */
3696 if (error == EISDIR && fvp->v_type == VDIR)
3698 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3699 vrele(fromnd.ni_dvp);
3701 vn_finished_write(mp);
3704 tvfslocked = NDHASGIANT(&tond);
3708 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
3711 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
3721 * If the source is the same as the destination (that is, if they
3722 * are links to the same vnode), then there is nothing to do.
3728 error = mac_vnode_check_rename_to(td->td_ucred, tdvp,
3729 tond.ni_vp, fromnd.ni_dvp == tdvp, &tond.ni_cnd);
3733 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
3734 tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
3735 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3736 NDFREE(&tond, NDF_ONLY_PNBUF);
3738 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3739 NDFREE(&tond, NDF_ONLY_PNBUF);
3746 vrele(fromnd.ni_dvp);
3749 vrele(tond.ni_startdir);
3750 vn_finished_write(mp);
3752 if (fromnd.ni_startdir)
3753 vrele(fromnd.ni_startdir);
3754 VFS_UNLOCK_GIANT(fvfslocked);
3755 VFS_UNLOCK_GIANT(tvfslocked);
3762 * Make a directory file.
3764 #ifndef _SYS_SYSPROTO_H_
3773 register struct mkdir_args /* {
3779 return (kern_mkdir(td, uap->path, UIO_USERSPACE, uap->mode));
3782 #ifndef _SYS_SYSPROTO_H_
3783 struct mkdirat_args {
3790 sys_mkdirat(struct thread *td, struct mkdirat_args *uap)
3793 return (kern_mkdirat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode));
3797 kern_mkdir(struct thread *td, char *path, enum uio_seg segflg, int mode)
3800 return (kern_mkdirat(td, AT_FDCWD, path, segflg, mode));
3804 kern_mkdirat(struct thread *td, int fd, char *path, enum uio_seg segflg,
3811 struct nameidata nd;
3814 AUDIT_ARG_MODE(mode);
3817 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | MPSAFE |
3818 AUDITVNODE1, segflg, path, fd, CAP_MKDIR, td);
3819 nd.ni_cnd.cn_flags |= WILLBEDIR;
3820 if ((error = namei(&nd)) != 0)
3822 vfslocked = NDHASGIANT(&nd);
3825 NDFREE(&nd, NDF_ONLY_PNBUF);
3827 * XXX namei called with LOCKPARENT but not LOCKLEAF has
3828 * the strange behaviour of leaving the vnode unlocked
3829 * if the target is the same vnode as the parent.
3831 if (vp == nd.ni_dvp)
3836 VFS_UNLOCK_GIANT(vfslocked);
3839 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3840 NDFREE(&nd, NDF_ONLY_PNBUF);
3842 VFS_UNLOCK_GIANT(vfslocked);
3843 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3848 vattr.va_type = VDIR;
3849 vattr.va_mode = (mode & ACCESSPERMS) &~ td->td_proc->p_fd->fd_cmask;
3851 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
3856 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
3860 NDFREE(&nd, NDF_ONLY_PNBUF);
3864 vn_finished_write(mp);
3865 VFS_UNLOCK_GIANT(vfslocked);
3870 * Remove a directory file.
3872 #ifndef _SYS_SYSPROTO_H_
3880 struct rmdir_args /* {
3885 return (kern_rmdir(td, uap->path, UIO_USERSPACE));
3889 kern_rmdir(struct thread *td, char *path, enum uio_seg pathseg)
3892 return (kern_rmdirat(td, AT_FDCWD, path, pathseg));
3896 kern_rmdirat(struct thread *td, int fd, char *path, enum uio_seg pathseg)
3901 struct nameidata nd;
3906 NDINIT_ATRIGHTS(&nd, DELETE, LOCKPARENT | LOCKLEAF | MPSAFE |
3907 AUDITVNODE1, pathseg, path, fd, CAP_RMDIR, td);
3908 if ((error = namei(&nd)) != 0)
3910 vfslocked = NDHASGIANT(&nd);
3912 if (vp->v_type != VDIR) {
3917 * No rmdir "." please.
3919 if (nd.ni_dvp == vp) {
3924 * The root of a mounted filesystem cannot be deleted.
3926 if (vp->v_vflag & VV_ROOT) {
3931 error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp,
3936 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3937 NDFREE(&nd, NDF_ONLY_PNBUF);
3939 if (nd.ni_dvp == vp)
3943 VFS_UNLOCK_GIANT(vfslocked);
3944 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3948 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_UNLINK);
3949 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
3950 vn_finished_write(mp);
3952 NDFREE(&nd, NDF_ONLY_PNBUF);
3954 if (nd.ni_dvp == vp)
3958 VFS_UNLOCK_GIANT(vfslocked);
3964 * Read a block of directory entries in a filesystem independent format.
3966 #ifndef _SYS_SYSPROTO_H_
3967 struct ogetdirentries_args {
3975 ogetdirentries(struct thread *td, struct ogetdirentries_args *uap)
3980 error = kern_ogetdirentries(td, uap, &loff);
3982 error = copyout(&loff, uap->basep, sizeof(long));
3987 kern_ogetdirentries(struct thread *td, struct ogetdirentries_args *uap,
3992 struct uio auio, kuio;
3993 struct iovec aiov, kiov;
3994 struct dirent *dp, *edp;
3996 int error, eofflag, readcnt, vfslocked;
4000 /* XXX arbitrary sanity limit on `count'. */
4001 if (uap->count > 64 * 1024)
4003 if ((error = getvnode(td->td_proc->p_fd, uap->fd, CAP_READ,
4006 if ((fp->f_flag & FREAD) == 0) {
4011 foffset = foffset_lock(fp, 0);
4013 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
4014 if (vp->v_type != VDIR) {
4015 VFS_UNLOCK_GIANT(vfslocked);
4016 foffset_unlock(fp, foffset, 0);
4020 aiov.iov_base = uap->buf;
4021 aiov.iov_len = uap->count;
4022 auio.uio_iov = &aiov;
4023 auio.uio_iovcnt = 1;
4024 auio.uio_rw = UIO_READ;
4025 auio.uio_segflg = UIO_USERSPACE;
4027 auio.uio_resid = uap->count;
4028 vn_lock(vp, LK_SHARED | LK_RETRY);
4029 loff = auio.uio_offset = foffset;
4031 error = mac_vnode_check_readdir(td->td_ucred, vp);
4034 VFS_UNLOCK_GIANT(vfslocked);
4035 foffset_unlock(fp, foffset, FOF_NOUPDATE);
4040 # if (BYTE_ORDER != LITTLE_ENDIAN)
4041 if (vp->v_mount->mnt_maxsymlinklen <= 0) {
4042 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag,
4044 foffset = auio.uio_offset;
4049 kuio.uio_iov = &kiov;
4050 kuio.uio_segflg = UIO_SYSSPACE;
4051 kiov.iov_len = uap->count;
4052 dirbuf = malloc(uap->count, M_TEMP, M_WAITOK);
4053 kiov.iov_base = dirbuf;
4054 error = VOP_READDIR(vp, &kuio, fp->f_cred, &eofflag,
4056 foffset = kuio.uio_offset;
4058 readcnt = uap->count - kuio.uio_resid;
4059 edp = (struct dirent *)&dirbuf[readcnt];
4060 for (dp = (struct dirent *)dirbuf; dp < edp; ) {
4061 # if (BYTE_ORDER == LITTLE_ENDIAN)
4063 * The expected low byte of
4064 * dp->d_namlen is our dp->d_type.
4065 * The high MBZ byte of dp->d_namlen
4066 * is our dp->d_namlen.
4068 dp->d_type = dp->d_namlen;
4072 * The dp->d_type is the high byte
4073 * of the expected dp->d_namlen,
4074 * so must be zero'ed.
4078 if (dp->d_reclen > 0) {
4079 dp = (struct dirent *)
4080 ((char *)dp + dp->d_reclen);
4087 error = uiomove(dirbuf, readcnt, &auio);
4089 free(dirbuf, M_TEMP);
4093 VFS_UNLOCK_GIANT(vfslocked);
4094 foffset_unlock(fp, foffset, 0);
4098 if (uap->count == auio.uio_resid &&
4099 (vp->v_vflag & VV_ROOT) &&
4100 (vp->v_mount->mnt_flag & MNT_UNION)) {
4101 struct vnode *tvp = vp;
4102 vp = vp->v_mount->mnt_vnodecovered;
4108 VFS_UNLOCK_GIANT(vfslocked);
4112 VFS_UNLOCK_GIANT(vfslocked);
4113 foffset_unlock(fp, foffset, 0);
4115 td->td_retval[0] = uap->count - auio.uio_resid;
4120 #endif /* COMPAT_43 */
4123 * Read a block of directory entries in a filesystem independent format.
4125 #ifndef _SYS_SYSPROTO_H_
4126 struct getdirentries_args {
4134 sys_getdirentries(td, uap)
4136 register struct getdirentries_args /* {
4146 error = kern_getdirentries(td, uap->fd, uap->buf, uap->count, &base);
4149 if (uap->basep != NULL)
4150 error = copyout(&base, uap->basep, sizeof(long));
4155 kern_getdirentries(struct thread *td, int fd, char *buf, u_int count,
4168 auio.uio_resid = count;
4169 if (auio.uio_resid > IOSIZE_MAX)
4171 if ((error = getvnode(td->td_proc->p_fd, fd, CAP_READ | CAP_SEEK,
4174 if ((fp->f_flag & FREAD) == 0) {
4179 foffset = foffset_lock(fp, 0);
4181 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
4182 if (vp->v_type != VDIR) {
4183 VFS_UNLOCK_GIANT(vfslocked);
4187 aiov.iov_base = buf;
4188 aiov.iov_len = count;
4189 auio.uio_iov = &aiov;
4190 auio.uio_iovcnt = 1;
4191 auio.uio_rw = UIO_READ;
4192 auio.uio_segflg = UIO_USERSPACE;
4194 vn_lock(vp, LK_SHARED | LK_RETRY);
4195 AUDIT_ARG_VNODE1(vp);
4196 loff = auio.uio_offset = foffset;
4198 error = mac_vnode_check_readdir(td->td_ucred, vp);
4201 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL,
4203 foffset = auio.uio_offset;
4206 VFS_UNLOCK_GIANT(vfslocked);
4209 if (count == auio.uio_resid &&
4210 (vp->v_vflag & VV_ROOT) &&
4211 (vp->v_mount->mnt_flag & MNT_UNION)) {
4212 struct vnode *tvp = vp;
4213 vp = vp->v_mount->mnt_vnodecovered;
4219 VFS_UNLOCK_GIANT(vfslocked);
4223 VFS_UNLOCK_GIANT(vfslocked);
4225 td->td_retval[0] = count - auio.uio_resid;
4227 foffset_unlock(fp, foffset, 0);
4232 #ifndef _SYS_SYSPROTO_H_
4233 struct getdents_args {
4240 sys_getdents(td, uap)
4242 register struct getdents_args /* {
4248 struct getdirentries_args ap;
4251 ap.count = uap->count;
4253 return (sys_getdirentries(td, &ap));
4257 * Set the mode mask for creation of filesystem nodes.
4259 #ifndef _SYS_SYSPROTO_H_
4267 struct umask_args /* {
4271 register struct filedesc *fdp;
4273 FILEDESC_XLOCK(td->td_proc->p_fd);
4274 fdp = td->td_proc->p_fd;
4275 td->td_retval[0] = fdp->fd_cmask;
4276 fdp->fd_cmask = uap->newmask & ALLPERMS;
4277 FILEDESC_XUNLOCK(td->td_proc->p_fd);
4282 * Void all references to file by ripping underlying filesystem away from
4285 #ifndef _SYS_SYSPROTO_H_
4286 struct revoke_args {
4293 register struct revoke_args /* {
4300 struct nameidata nd;
4303 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | MPSAFE | AUDITVNODE1,
4304 UIO_USERSPACE, uap->path, td);
4305 if ((error = namei(&nd)) != 0)
4307 vfslocked = NDHASGIANT(&nd);
4309 NDFREE(&nd, NDF_ONLY_PNBUF);
4310 if (vp->v_type != VCHR || vp->v_rdev == NULL) {
4315 error = mac_vnode_check_revoke(td->td_ucred, vp);
4319 error = VOP_GETATTR(vp, &vattr, td->td_ucred);
4322 if (td->td_ucred->cr_uid != vattr.va_uid) {
4323 error = priv_check(td, PRIV_VFS_ADMIN);
4328 VOP_REVOKE(vp, REVOKEALL);
4331 VFS_UNLOCK_GIANT(vfslocked);
4336 * Convert a user file descriptor to a kernel file entry and check that, if it
4337 * is a capability, the correct rights are present. A reference on the file
4338 * entry is held upon returning.
4341 getvnode(struct filedesc *fdp, int fd, cap_rights_t rights,
4346 struct file *fp_fromcap;
4352 if ((fdp == NULL) || (fp = fget_unlocked(fdp, fd)) == NULL)
4356 * If the file descriptor is for a capability, test rights and use the
4357 * file descriptor referenced by the capability.
4359 error = cap_funwrap(fp, rights, &fp_fromcap);
4361 fdrop(fp, curthread);
4364 if (fp != fp_fromcap) {
4366 fdrop(fp, curthread);
4369 #endif /* CAPABILITIES */
4372 * The file could be not of the vnode type, or it may be not
4373 * yet fully initialized, in which case the f_vnode pointer
4374 * may be set, but f_ops is still badfileops. E.g.,
4375 * devfs_open() transiently create such situation to
4376 * facilitate csw d_fdopen().
4378 * Dupfdopen() handling in kern_openat() installs the
4379 * half-baked file into the process descriptor table, allowing
4380 * other thread to dereference it. Guard against the race by
4383 if (fp->f_vnode == NULL || fp->f_ops == &badfileops) {
4384 fdrop(fp, curthread);
4393 * Get an (NFS) file handle.
4395 #ifndef _SYS_SYSPROTO_H_
4396 struct lgetfh_args {
4404 register struct lgetfh_args *uap;
4406 struct nameidata nd;
4408 register struct vnode *vp;
4412 error = priv_check(td, PRIV_VFS_GETFH);
4415 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | MPSAFE | AUDITVNODE1,
4416 UIO_USERSPACE, uap->fname, td);
4420 vfslocked = NDHASGIANT(&nd);
4421 NDFREE(&nd, NDF_ONLY_PNBUF);
4423 bzero(&fh, sizeof(fh));
4424 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
4425 error = VOP_VPTOFH(vp, &fh.fh_fid);
4427 VFS_UNLOCK_GIANT(vfslocked);
4430 error = copyout(&fh, uap->fhp, sizeof (fh));
4434 #ifndef _SYS_SYSPROTO_H_
4443 register struct getfh_args *uap;
4445 struct nameidata nd;
4447 register struct vnode *vp;
4451 error = priv_check(td, PRIV_VFS_GETFH);
4454 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | MPSAFE | AUDITVNODE1,
4455 UIO_USERSPACE, uap->fname, td);
4459 vfslocked = NDHASGIANT(&nd);
4460 NDFREE(&nd, NDF_ONLY_PNBUF);
4462 bzero(&fh, sizeof(fh));
4463 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
4464 error = VOP_VPTOFH(vp, &fh.fh_fid);
4466 VFS_UNLOCK_GIANT(vfslocked);
4469 error = copyout(&fh, uap->fhp, sizeof (fh));
4474 * syscall for the rpc.lockd to use to translate a NFS file handle into an
4477 * warning: do not remove the priv_check() call or this becomes one giant
4480 #ifndef _SYS_SYSPROTO_H_
4481 struct fhopen_args {
4482 const struct fhandle *u_fhp;
4489 struct fhopen_args /* {
4490 const struct fhandle *u_fhp;
4494 struct proc *p = td->td_proc;
4499 struct vattr *vap = &vat;
4502 register struct filedesc *fdp = p->p_fd;
4503 int fmode, error, type;
4509 error = priv_check(td, PRIV_VFS_FHOPEN);
4512 fmode = FFLAGS(uap->flags);
4513 /* why not allow a non-read/write open for our lockd? */
4514 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
4516 error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
4519 /* find the mount point */
4520 mp = vfs_busyfs(&fhp.fh_fsid);
4523 vfslocked = VFS_LOCK_GIANT(mp);
4524 /* now give me my vnode, it gets returned to me locked */
4525 error = VFS_FHTOVP(mp, &fhp.fh_fid, LK_EXCLUSIVE, &vp);
4530 * from now on we have to make sure not
4531 * to forget about the vnode
4532 * any error that causes an abort must vput(vp)
4533 * just set error = err and 'goto bad;'.
4539 if (vp->v_type == VLNK) {
4543 if (vp->v_type == VSOCK) {
4547 if (vp->v_type != VDIR && fmode & O_DIRECTORY) {
4552 if (fmode & (FWRITE | O_TRUNC)) {
4553 if (vp->v_type == VDIR) {
4557 error = vn_writechk(vp);
4564 if ((fmode & O_APPEND) && (fmode & FWRITE))
4567 error = mac_vnode_check_open(td->td_ucred, vp, accmode);
4572 error = VOP_ACCESS(vp, accmode, td->td_ucred, td);
4576 if (fmode & O_TRUNC) {
4578 VOP_UNLOCK(vp, 0); /* XXX */
4579 if ((error = vn_start_write(NULL, &mp, V_WAIT | PCATCH)) != 0) {
4584 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); /* XXX */
4588 * We don't yet have fp->f_cred, so use td->td_ucred, which
4591 error = mac_vnode_check_write(td->td_ucred, td->td_ucred, vp);
4596 error = VOP_SETATTR(vp, vap, td->td_ucred);
4600 vn_finished_write(mp);
4604 error = VOP_OPEN(vp, fmode, td->td_ucred, td, NULL);
4612 * end of vn_open code
4615 if ((error = falloc(td, &nfp, &indx, fmode)) != 0) {
4620 /* An extra reference on `nfp' has been held for us by falloc(). */
4623 finit(nfp, fmode & FMASK, DTYPE_VNODE, vp, &vnops);
4624 if (fmode & (O_EXLOCK | O_SHLOCK)) {
4625 lf.l_whence = SEEK_SET;
4628 if (fmode & O_EXLOCK)
4629 lf.l_type = F_WRLCK;
4631 lf.l_type = F_RDLCK;
4633 if ((fmode & FNONBLOCK) == 0)
4636 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf,
4639 * The lock request failed. Normally close the
4640 * descriptor but handle the case where someone might
4641 * have dup()d or close()d it when we weren't looking.
4643 fdclose(fdp, fp, indx, td);
4646 * release our private reference
4651 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
4652 atomic_set_int(&fp->f_flag, FHASLOCK);
4657 VFS_UNLOCK_GIANT(vfslocked);
4658 td->td_retval[0] = indx;
4664 VFS_UNLOCK_GIANT(vfslocked);
4669 * Stat an (NFS) file handle.
4671 #ifndef _SYS_SYSPROTO_H_
4672 struct fhstat_args {
4673 struct fhandle *u_fhp;
4680 register struct fhstat_args /* {
4681 struct fhandle *u_fhp;
4692 error = priv_check(td, PRIV_VFS_FHSTAT);
4695 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4698 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4700 vfslocked = VFS_LOCK_GIANT(mp);
4701 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_EXCLUSIVE, &vp);
4704 VFS_UNLOCK_GIANT(vfslocked);
4707 error = vn_stat(vp, &sb, td->td_ucred, NOCRED, td);
4709 VFS_UNLOCK_GIANT(vfslocked);
4712 error = copyout(&sb, uap->sb, sizeof(sb));
4717 * Implement fstatfs() for (NFS) file handles.
4719 #ifndef _SYS_SYSPROTO_H_
4720 struct fhstatfs_args {
4721 struct fhandle *u_fhp;
4726 sys_fhstatfs(td, uap)
4728 struct fhstatfs_args /* {
4729 struct fhandle *u_fhp;
4737 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4740 error = kern_fhstatfs(td, fh, &sf);
4743 return (copyout(&sf, uap->buf, sizeof(sf)));
4747 kern_fhstatfs(struct thread *td, fhandle_t fh, struct statfs *buf)
4755 error = priv_check(td, PRIV_VFS_FHSTATFS);
4758 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4760 vfslocked = VFS_LOCK_GIANT(mp);
4761 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_EXCLUSIVE, &vp);
4764 VFS_UNLOCK_GIANT(vfslocked);
4768 error = prison_canseemount(td->td_ucred, mp);
4772 error = mac_mount_check_stat(td->td_ucred, mp);
4777 * Set these in case the underlying filesystem fails to do so.
4780 sp->f_version = STATFS_VERSION;
4781 sp->f_namemax = NAME_MAX;
4782 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
4783 error = VFS_STATFS(mp, sp);
4788 VFS_UNLOCK_GIANT(vfslocked);
4793 kern_posix_fallocate(struct thread *td, int fd, off_t offset, off_t len)
4798 off_t olen, ooffset;
4799 int error, vfslocked;
4803 error = fget(td, fd, CAP_WRITE, &fp);
4807 switch (fp->f_type) {
4818 if ((fp->f_flag & FWRITE) == 0) {
4823 if (vp->v_type != VREG) {
4827 if (offset < 0 || len <= 0) {
4831 /* Check for wrap. */
4832 if (offset > OFF_MAX - len) {
4837 /* Allocating blocks may take a long time, so iterate. */
4843 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
4845 error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
4847 VFS_UNLOCK_GIANT(vfslocked);
4850 error = vn_lock(vp, LK_EXCLUSIVE);
4852 vn_finished_write(mp);
4853 VFS_UNLOCK_GIANT(vfslocked);
4857 error = mac_vnode_check_write(td->td_ucred, fp->f_cred, vp);
4860 error = VOP_ALLOCATE(vp, &offset, &len);
4862 vn_finished_write(mp);
4863 VFS_UNLOCK_GIANT(vfslocked);
4865 if (olen + ooffset != offset + len) {
4866 panic("offset + len changed from %jx/%jx to %jx/%jx",
4867 ooffset, olen, offset, len);
4869 if (error != 0 || len == 0)
4871 KASSERT(olen > len, ("Iteration did not make progress?"));
4881 sys_posix_fallocate(struct thread *td, struct posix_fallocate_args *uap)
4884 td->td_retval[0] = kern_posix_fallocate(td, uap->fd, uap->offset,
4890 * Unlike madvise(2), we do not make a best effort to remember every
4891 * possible caching hint. Instead, we remember the last setting with
4892 * the exception that we will allow POSIX_FADV_NORMAL to adjust the
4893 * region of any current setting.
4896 kern_posix_fadvise(struct thread *td, int fd, off_t offset, off_t len,
4899 struct fadvise_info *fa, *new;
4905 if (offset < 0 || len < 0 || offset > OFF_MAX - len)
4908 case POSIX_FADV_SEQUENTIAL:
4909 case POSIX_FADV_RANDOM:
4910 case POSIX_FADV_NOREUSE:
4911 new = malloc(sizeof(*fa), M_FADVISE, M_WAITOK);
4913 case POSIX_FADV_NORMAL:
4914 case POSIX_FADV_WILLNEED:
4915 case POSIX_FADV_DONTNEED:
4921 /* XXX: CAP_POSIX_FADVISE? */
4922 error = fget(td, fd, 0, &fp);
4926 switch (fp->f_type) {
4938 if (vp->v_type != VREG) {
4945 end = offset + len - 1;
4947 case POSIX_FADV_SEQUENTIAL:
4948 case POSIX_FADV_RANDOM:
4949 case POSIX_FADV_NOREUSE:
4951 * Try to merge any existing non-standard region with
4952 * this new region if possible, otherwise create a new
4953 * non-standard region for this request.
4955 mtx_pool_lock(mtxpool_sleep, fp);
4957 if (fa != NULL && fa->fa_advice == advice &&
4958 ((fa->fa_start <= end && fa->fa_end >= offset) ||
4959 (end != OFF_MAX && fa->fa_start == end + 1) ||
4960 (fa->fa_end != OFF_MAX && fa->fa_end + 1 == offset))) {
4961 if (offset < fa->fa_start)
4962 fa->fa_start = offset;
4963 if (end > fa->fa_end)
4966 new->fa_advice = advice;
4967 new->fa_start = offset;
4969 new->fa_prevstart = 0;
4970 new->fa_prevend = 0;
4974 mtx_pool_unlock(mtxpool_sleep, fp);
4976 case POSIX_FADV_NORMAL:
4978 * If a the "normal" region overlaps with an existing
4979 * non-standard region, trim or remove the
4980 * non-standard region.
4982 mtx_pool_lock(mtxpool_sleep, fp);
4985 if (offset <= fa->fa_start && end >= fa->fa_end) {
4987 fp->f_advice = NULL;
4988 } else if (offset <= fa->fa_start &&
4989 end >= fa->fa_start)
4990 fa->fa_start = end + 1;
4991 else if (offset <= fa->fa_end && end >= fa->fa_end)
4992 fa->fa_end = offset - 1;
4993 else if (offset >= fa->fa_start && end <= fa->fa_end) {
4995 * If the "normal" region is a middle
4996 * portion of the existing
4997 * non-standard region, just remove
4998 * the whole thing rather than picking
4999 * one side or the other to
5003 fp->f_advice = NULL;
5006 mtx_pool_unlock(mtxpool_sleep, fp);
5008 case POSIX_FADV_WILLNEED:
5009 case POSIX_FADV_DONTNEED:
5010 error = VOP_ADVISE(vp, offset, end, advice);
5016 free(new, M_FADVISE);
5021 sys_posix_fadvise(struct thread *td, struct posix_fadvise_args *uap)
5024 td->td_retval[0] = kern_posix_fadvise(td, uap->fd, uap->offset,
5025 uap->len, uap->advice);