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 ost->st_dev = st->st_dev;
2330 ost->st_ino = st->st_ino;
2331 ost->st_mode = st->st_mode;
2332 ost->st_nlink = st->st_nlink;
2333 ost->st_uid = st->st_uid;
2334 ost->st_gid = st->st_gid;
2335 ost->st_rdev = st->st_rdev;
2336 if (st->st_size < (quad_t)1 << 32)
2337 ost->st_size = st->st_size;
2340 ost->st_atim = st->st_atim;
2341 ost->st_mtim = st->st_mtim;
2342 ost->st_ctim = st->st_ctim;
2343 ost->st_blksize = st->st_blksize;
2344 ost->st_blocks = st->st_blocks;
2345 ost->st_flags = st->st_flags;
2346 ost->st_gen = st->st_gen;
2348 #endif /* COMPAT_43 */
2351 * Get file status; this version follows links.
2353 #ifndef _SYS_SYSPROTO_H_
2362 register struct stat_args /* {
2370 error = kern_stat(td, uap->path, UIO_USERSPACE, &sb);
2372 error = copyout(&sb, uap->ub, sizeof (sb));
2376 #ifndef _SYS_SYSPROTO_H_
2377 struct fstatat_args {
2385 sys_fstatat(struct thread *td, struct fstatat_args *uap)
2390 error = kern_statat(td, uap->flag, uap->fd, uap->path,
2391 UIO_USERSPACE, &sb);
2393 error = copyout(&sb, uap->buf, sizeof (sb));
2398 kern_stat(struct thread *td, char *path, enum uio_seg pathseg, struct stat *sbp)
2401 return (kern_statat(td, 0, AT_FDCWD, path, pathseg, sbp));
2405 kern_statat(struct thread *td, int flag, int fd, char *path,
2406 enum uio_seg pathseg, struct stat *sbp)
2409 return (kern_statat_vnhook(td, flag, fd, path, pathseg, sbp, NULL));
2413 kern_statat_vnhook(struct thread *td, int flag, int fd, char *path,
2414 enum uio_seg pathseg, struct stat *sbp,
2415 void (*hook)(struct vnode *vp, struct stat *sbp))
2417 struct nameidata nd;
2419 int error, vfslocked;
2421 if (flag & ~AT_SYMLINK_NOFOLLOW)
2424 NDINIT_ATRIGHTS(&nd, LOOKUP, ((flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW :
2425 FOLLOW) | LOCKSHARED | LOCKLEAF | AUDITVNODE1 | MPSAFE, pathseg,
2426 path, fd, CAP_FSTAT, td);
2428 if ((error = namei(&nd)) != 0)
2430 vfslocked = NDHASGIANT(&nd);
2431 error = vn_stat(nd.ni_vp, &sb, td->td_ucred, NOCRED, td);
2433 SDT_PROBE(vfs, , stat, mode, path, sb.st_mode, 0, 0, 0);
2434 if (S_ISREG(sb.st_mode))
2435 SDT_PROBE(vfs, , stat, reg, path, pathseg, 0, 0, 0);
2436 if (__predict_false(hook != NULL))
2437 hook(nd.ni_vp, &sb);
2439 NDFREE(&nd, NDF_ONLY_PNBUF);
2441 VFS_UNLOCK_GIANT(vfslocked);
2446 if (KTRPOINT(td, KTR_STRUCT))
2453 * Get file status; this version does not follow links.
2455 #ifndef _SYS_SYSPROTO_H_
2464 register struct lstat_args /* {
2472 error = kern_lstat(td, uap->path, UIO_USERSPACE, &sb);
2474 error = copyout(&sb, uap->ub, sizeof (sb));
2479 kern_lstat(struct thread *td, char *path, enum uio_seg pathseg, struct stat *sbp)
2482 return (kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, path, pathseg,
2487 * Implementation of the NetBSD [l]stat() functions.
2494 bzero(nsb, sizeof *nsb);
2495 nsb->st_dev = sb->st_dev;
2496 nsb->st_ino = sb->st_ino;
2497 nsb->st_mode = sb->st_mode;
2498 nsb->st_nlink = sb->st_nlink;
2499 nsb->st_uid = sb->st_uid;
2500 nsb->st_gid = sb->st_gid;
2501 nsb->st_rdev = sb->st_rdev;
2502 nsb->st_atim = sb->st_atim;
2503 nsb->st_mtim = sb->st_mtim;
2504 nsb->st_ctim = sb->st_ctim;
2505 nsb->st_size = sb->st_size;
2506 nsb->st_blocks = sb->st_blocks;
2507 nsb->st_blksize = sb->st_blksize;
2508 nsb->st_flags = sb->st_flags;
2509 nsb->st_gen = sb->st_gen;
2510 nsb->st_birthtim = sb->st_birthtim;
2513 #ifndef _SYS_SYSPROTO_H_
2522 register struct nstat_args /* {
2531 error = kern_stat(td, uap->path, UIO_USERSPACE, &sb);
2534 cvtnstat(&sb, &nsb);
2535 error = copyout(&nsb, uap->ub, sizeof (nsb));
2540 * NetBSD lstat. Get file status; this version does not follow links.
2542 #ifndef _SYS_SYSPROTO_H_
2551 register struct nlstat_args /* {
2560 error = kern_lstat(td, uap->path, UIO_USERSPACE, &sb);
2563 cvtnstat(&sb, &nsb);
2564 error = copyout(&nsb, uap->ub, sizeof (nsb));
2569 * Get configurable pathname variables.
2571 #ifndef _SYS_SYSPROTO_H_
2572 struct pathconf_args {
2578 sys_pathconf(td, uap)
2580 register struct pathconf_args /* {
2586 return (kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name, FOLLOW));
2589 #ifndef _SYS_SYSPROTO_H_
2590 struct lpathconf_args {
2596 sys_lpathconf(td, uap)
2598 register struct lpathconf_args /* {
2604 return (kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name, NOFOLLOW));
2608 kern_pathconf(struct thread *td, char *path, enum uio_seg pathseg, int name,
2611 struct nameidata nd;
2612 int error, vfslocked;
2614 NDINIT(&nd, LOOKUP, LOCKSHARED | LOCKLEAF | MPSAFE | AUDITVNODE1 |
2615 flags, pathseg, path, td);
2616 if ((error = namei(&nd)) != 0)
2618 vfslocked = NDHASGIANT(&nd);
2619 NDFREE(&nd, NDF_ONLY_PNBUF);
2621 /* If asynchronous I/O is available, it works for all files. */
2622 if (name == _PC_ASYNC_IO)
2623 td->td_retval[0] = async_io_version;
2625 error = VOP_PATHCONF(nd.ni_vp, name, td->td_retval);
2627 VFS_UNLOCK_GIANT(vfslocked);
2632 * Return target name of a symbolic link.
2634 #ifndef _SYS_SYSPROTO_H_
2635 struct readlink_args {
2642 sys_readlink(td, uap)
2644 register struct readlink_args /* {
2651 return (kern_readlink(td, uap->path, UIO_USERSPACE, uap->buf,
2652 UIO_USERSPACE, uap->count));
2654 #ifndef _SYS_SYSPROTO_H_
2655 struct readlinkat_args {
2663 sys_readlinkat(struct thread *td, struct readlinkat_args *uap)
2666 return (kern_readlinkat(td, uap->fd, uap->path, UIO_USERSPACE,
2667 uap->buf, UIO_USERSPACE, uap->bufsize));
2671 kern_readlink(struct thread *td, char *path, enum uio_seg pathseg, char *buf,
2672 enum uio_seg bufseg, size_t count)
2675 return (kern_readlinkat(td, AT_FDCWD, path, pathseg, buf, bufseg,
2680 kern_readlinkat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2681 char *buf, enum uio_seg bufseg, size_t count)
2687 struct nameidata nd;
2690 if (count > IOSIZE_MAX)
2693 NDINIT_AT(&nd, LOOKUP, NOFOLLOW | LOCKSHARED | LOCKLEAF | MPSAFE |
2694 AUDITVNODE1, pathseg, path, fd, td);
2696 if ((error = namei(&nd)) != 0)
2698 NDFREE(&nd, NDF_ONLY_PNBUF);
2699 vfslocked = NDHASGIANT(&nd);
2702 error = mac_vnode_check_readlink(td->td_ucred, vp);
2705 VFS_UNLOCK_GIANT(vfslocked);
2709 if (vp->v_type != VLNK)
2712 aiov.iov_base = buf;
2713 aiov.iov_len = count;
2714 auio.uio_iov = &aiov;
2715 auio.uio_iovcnt = 1;
2716 auio.uio_offset = 0;
2717 auio.uio_rw = UIO_READ;
2718 auio.uio_segflg = bufseg;
2720 auio.uio_resid = count;
2721 error = VOP_READLINK(vp, &auio, td->td_ucred);
2722 td->td_retval[0] = count - auio.uio_resid;
2725 VFS_UNLOCK_GIANT(vfslocked);
2730 * Common implementation code for chflags() and fchflags().
2733 setfflags(td, vp, flags)
2742 /* We can't support the value matching VNOVAL. */
2743 if (flags == VNOVAL)
2744 return (EOPNOTSUPP);
2747 * Prevent non-root users from setting flags on devices. When
2748 * a device is reused, users can retain ownership of the device
2749 * if they are allowed to set flags and programs assume that
2750 * chown can't fail when done as root.
2752 if (vp->v_type == VCHR || vp->v_type == VBLK) {
2753 error = priv_check(td, PRIV_VFS_CHFLAGS_DEV);
2758 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2760 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2762 vattr.va_flags = flags;
2764 error = mac_vnode_check_setflags(td->td_ucred, vp, vattr.va_flags);
2767 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
2769 vn_finished_write(mp);
2774 * Change flags of a file given a path name.
2776 #ifndef _SYS_SYSPROTO_H_
2777 struct chflags_args {
2783 sys_chflags(td, uap)
2785 register struct chflags_args /* {
2791 struct nameidata nd;
2794 AUDIT_ARG_FFLAGS(uap->flags);
2795 NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE | AUDITVNODE1, UIO_USERSPACE,
2797 if ((error = namei(&nd)) != 0)
2799 NDFREE(&nd, NDF_ONLY_PNBUF);
2800 vfslocked = NDHASGIANT(&nd);
2801 error = setfflags(td, nd.ni_vp, uap->flags);
2803 VFS_UNLOCK_GIANT(vfslocked);
2808 * Same as chflags() but doesn't follow symlinks.
2811 sys_lchflags(td, uap)
2813 register struct lchflags_args /* {
2819 struct nameidata nd;
2822 AUDIT_ARG_FFLAGS(uap->flags);
2823 NDINIT(&nd, LOOKUP, NOFOLLOW | MPSAFE | AUDITVNODE1, UIO_USERSPACE,
2825 if ((error = namei(&nd)) != 0)
2827 vfslocked = NDHASGIANT(&nd);
2828 NDFREE(&nd, NDF_ONLY_PNBUF);
2829 error = setfflags(td, nd.ni_vp, uap->flags);
2831 VFS_UNLOCK_GIANT(vfslocked);
2836 * Change flags of a file given a file descriptor.
2838 #ifndef _SYS_SYSPROTO_H_
2839 struct fchflags_args {
2845 sys_fchflags(td, uap)
2847 register struct fchflags_args /* {
2856 AUDIT_ARG_FD(uap->fd);
2857 AUDIT_ARG_FFLAGS(uap->flags);
2858 if ((error = getvnode(td->td_proc->p_fd, uap->fd, CAP_FCHFLAGS,
2861 vfslocked = VFS_LOCK_GIANT(fp->f_vnode->v_mount);
2863 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
2864 AUDIT_ARG_VNODE1(fp->f_vnode);
2865 VOP_UNLOCK(fp->f_vnode, 0);
2867 error = setfflags(td, fp->f_vnode, uap->flags);
2868 VFS_UNLOCK_GIANT(vfslocked);
2874 * Common implementation code for chmod(), lchmod() and fchmod().
2877 setfmode(td, cred, vp, mode)
2887 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2889 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2891 vattr.va_mode = mode & ALLPERMS;
2893 error = mac_vnode_check_setmode(cred, vp, vattr.va_mode);
2896 error = VOP_SETATTR(vp, &vattr, cred);
2898 vn_finished_write(mp);
2903 * Change mode of a file given path name.
2905 #ifndef _SYS_SYSPROTO_H_
2914 register struct chmod_args /* {
2920 return (kern_chmod(td, uap->path, UIO_USERSPACE, uap->mode));
2923 #ifndef _SYS_SYSPROTO_H_
2924 struct fchmodat_args {
2932 sys_fchmodat(struct thread *td, struct fchmodat_args *uap)
2934 int flag = uap->flag;
2936 char *path = uap->path;
2937 mode_t mode = uap->mode;
2939 if (flag & ~AT_SYMLINK_NOFOLLOW)
2942 return (kern_fchmodat(td, fd, path, UIO_USERSPACE, mode, flag));
2946 kern_chmod(struct thread *td, char *path, enum uio_seg pathseg, int mode)
2949 return (kern_fchmodat(td, AT_FDCWD, path, pathseg, mode, 0));
2953 * Change mode of a file given path name (don't follow links.)
2955 #ifndef _SYS_SYSPROTO_H_
2956 struct lchmod_args {
2964 register struct lchmod_args /* {
2970 return (kern_fchmodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2971 uap->mode, AT_SYMLINK_NOFOLLOW));
2976 kern_fchmodat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2977 mode_t mode, int flag)
2980 struct nameidata nd;
2984 AUDIT_ARG_MODE(mode);
2985 follow = (flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
2986 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | MPSAFE | AUDITVNODE1, pathseg,
2987 path, fd, CAP_FCHMOD, td);
2988 if ((error = namei(&nd)) != 0)
2990 vfslocked = NDHASGIANT(&nd);
2991 NDFREE(&nd, NDF_ONLY_PNBUF);
2992 error = setfmode(td, td->td_ucred, nd.ni_vp, mode);
2994 VFS_UNLOCK_GIANT(vfslocked);
2999 * Change mode of a file given a file descriptor.
3001 #ifndef _SYS_SYSPROTO_H_
3002 struct fchmod_args {
3008 sys_fchmod(struct thread *td, struct fchmod_args *uap)
3013 AUDIT_ARG_FD(uap->fd);
3014 AUDIT_ARG_MODE(uap->mode);
3016 error = fget(td, uap->fd, CAP_FCHMOD, &fp);
3019 error = fo_chmod(fp, uap->mode, td->td_ucred, td);
3025 * Common implementation for chown(), lchown(), and fchown()
3028 setfown(td, cred, vp, uid, gid)
3039 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
3041 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3046 error = mac_vnode_check_setowner(cred, vp, vattr.va_uid,
3050 error = VOP_SETATTR(vp, &vattr, cred);
3052 vn_finished_write(mp);
3057 * Set ownership given a path name.
3059 #ifndef _SYS_SYSPROTO_H_
3069 register struct chown_args /* {
3076 return (kern_chown(td, uap->path, UIO_USERSPACE, uap->uid, uap->gid));
3079 #ifndef _SYS_SYSPROTO_H_
3080 struct fchownat_args {
3089 sys_fchownat(struct thread *td, struct fchownat_args *uap)
3094 if (flag & ~AT_SYMLINK_NOFOLLOW)
3097 return (kern_fchownat(td, uap->fd, uap->path, UIO_USERSPACE, uap->uid,
3098 uap->gid, uap->flag));
3102 kern_chown(struct thread *td, char *path, enum uio_seg pathseg, int uid,
3106 return (kern_fchownat(td, AT_FDCWD, path, pathseg, uid, gid, 0));
3110 kern_fchownat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
3111 int uid, int gid, int flag)
3113 struct nameidata nd;
3114 int error, vfslocked, follow;
3116 AUDIT_ARG_OWNER(uid, gid);
3117 follow = (flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
3118 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | MPSAFE | AUDITVNODE1, pathseg,
3119 path, fd, CAP_FCHOWN, td);
3121 if ((error = namei(&nd)) != 0)
3123 vfslocked = NDHASGIANT(&nd);
3124 NDFREE(&nd, NDF_ONLY_PNBUF);
3125 error = setfown(td, td->td_ucred, nd.ni_vp, uid, gid);
3127 VFS_UNLOCK_GIANT(vfslocked);
3132 * Set ownership given a path name, do not cross symlinks.
3134 #ifndef _SYS_SYSPROTO_H_
3135 struct lchown_args {
3144 register struct lchown_args /* {
3151 return (kern_lchown(td, uap->path, UIO_USERSPACE, uap->uid, uap->gid));
3155 kern_lchown(struct thread *td, char *path, enum uio_seg pathseg, int uid,
3159 return (kern_fchownat(td, AT_FDCWD, path, pathseg, uid, gid,
3160 AT_SYMLINK_NOFOLLOW));
3164 * Set ownership given a file descriptor.
3166 #ifndef _SYS_SYSPROTO_H_
3167 struct fchown_args {
3176 register struct fchown_args /* {
3185 AUDIT_ARG_FD(uap->fd);
3186 AUDIT_ARG_OWNER(uap->uid, uap->gid);
3187 error = fget(td, uap->fd, CAP_FCHOWN, &fp);
3190 error = fo_chown(fp, uap->uid, uap->gid, td->td_ucred, td);
3196 * Common implementation code for utimes(), lutimes(), and futimes().
3199 getutimes(usrtvp, tvpseg, tsp)
3200 const struct timeval *usrtvp;
3201 enum uio_seg tvpseg;
3202 struct timespec *tsp;
3204 struct timeval tv[2];
3205 const struct timeval *tvp;
3208 if (usrtvp == NULL) {
3209 vfs_timestamp(&tsp[0]);
3212 if (tvpseg == UIO_SYSSPACE) {
3215 if ((error = copyin(usrtvp, tv, sizeof(tv))) != 0)
3220 if (tvp[0].tv_usec < 0 || tvp[0].tv_usec >= 1000000 ||
3221 tvp[1].tv_usec < 0 || tvp[1].tv_usec >= 1000000)
3223 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
3224 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
3230 * Common implementation code for utimes(), lutimes(), and futimes().
3233 setutimes(td, vp, ts, numtimes, nullflag)
3236 const struct timespec *ts;
3240 int error, setbirthtime;
3244 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
3246 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3248 if (numtimes < 3 && !VOP_GETATTR(vp, &vattr, td->td_ucred) &&
3249 timespeccmp(&ts[1], &vattr.va_birthtime, < ))
3252 vattr.va_atime = ts[0];
3253 vattr.va_mtime = ts[1];
3255 vattr.va_birthtime = ts[1];
3257 vattr.va_birthtime = ts[2];
3259 vattr.va_vaflags |= VA_UTIMES_NULL;
3261 error = mac_vnode_check_setutimes(td->td_ucred, vp, vattr.va_atime,
3265 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3267 vn_finished_write(mp);
3272 * Set the access and modification times of a file.
3274 #ifndef _SYS_SYSPROTO_H_
3275 struct utimes_args {
3277 struct timeval *tptr;
3283 register struct utimes_args /* {
3285 struct timeval *tptr;
3289 return (kern_utimes(td, uap->path, UIO_USERSPACE, uap->tptr,
3293 #ifndef _SYS_SYSPROTO_H_
3294 struct futimesat_args {
3297 const struct timeval * times;
3301 sys_futimesat(struct thread *td, struct futimesat_args *uap)
3304 return (kern_utimesat(td, uap->fd, uap->path, UIO_USERSPACE,
3305 uap->times, UIO_USERSPACE));
3309 kern_utimes(struct thread *td, char *path, enum uio_seg pathseg,
3310 struct timeval *tptr, enum uio_seg tptrseg)
3313 return (kern_utimesat(td, AT_FDCWD, path, pathseg, tptr, tptrseg));
3317 kern_utimesat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
3318 struct timeval *tptr, enum uio_seg tptrseg)
3320 struct nameidata nd;
3321 struct timespec ts[2];
3322 int error, vfslocked;
3324 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3326 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | MPSAFE | AUDITVNODE1, pathseg,
3327 path, fd, CAP_FUTIMES, td);
3329 if ((error = namei(&nd)) != 0)
3331 vfslocked = NDHASGIANT(&nd);
3332 NDFREE(&nd, NDF_ONLY_PNBUF);
3333 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3335 VFS_UNLOCK_GIANT(vfslocked);
3340 * Set the access and modification times of a file.
3342 #ifndef _SYS_SYSPROTO_H_
3343 struct lutimes_args {
3345 struct timeval *tptr;
3349 sys_lutimes(td, uap)
3351 register struct lutimes_args /* {
3353 struct timeval *tptr;
3357 return (kern_lutimes(td, uap->path, UIO_USERSPACE, uap->tptr,
3362 kern_lutimes(struct thread *td, char *path, enum uio_seg pathseg,
3363 struct timeval *tptr, enum uio_seg tptrseg)
3365 struct timespec ts[2];
3367 struct nameidata nd;
3370 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3372 NDINIT(&nd, LOOKUP, NOFOLLOW | MPSAFE | AUDITVNODE1, pathseg, path, td);
3373 if ((error = namei(&nd)) != 0)
3375 vfslocked = NDHASGIANT(&nd);
3376 NDFREE(&nd, NDF_ONLY_PNBUF);
3377 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3379 VFS_UNLOCK_GIANT(vfslocked);
3384 * Set the access and modification times of a file.
3386 #ifndef _SYS_SYSPROTO_H_
3387 struct futimes_args {
3389 struct timeval *tptr;
3393 sys_futimes(td, uap)
3395 register struct futimes_args /* {
3397 struct timeval *tptr;
3401 return (kern_futimes(td, uap->fd, uap->tptr, UIO_USERSPACE));
3405 kern_futimes(struct thread *td, int fd, struct timeval *tptr,
3406 enum uio_seg tptrseg)
3408 struct timespec ts[2];
3414 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3416 if ((error = getvnode(td->td_proc->p_fd, fd, CAP_FUTIMES, &fp))
3419 vfslocked = VFS_LOCK_GIANT(fp->f_vnode->v_mount);
3421 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
3422 AUDIT_ARG_VNODE1(fp->f_vnode);
3423 VOP_UNLOCK(fp->f_vnode, 0);
3425 error = setutimes(td, fp->f_vnode, ts, 2, tptr == NULL);
3426 VFS_UNLOCK_GIANT(vfslocked);
3432 * Truncate a file given its path name.
3434 #ifndef _SYS_SYSPROTO_H_
3435 struct truncate_args {
3442 sys_truncate(td, uap)
3444 register struct truncate_args /* {
3451 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3455 kern_truncate(struct thread *td, char *path, enum uio_seg pathseg, off_t length)
3461 struct nameidata nd;
3462 int error, vfslocked;
3466 NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE | AUDITVNODE1, pathseg, path, td);
3467 if ((error = namei(&nd)) != 0)
3469 vfslocked = NDHASGIANT(&nd);
3471 rl_cookie = vn_rangelock_wlock(vp, 0, OFF_MAX);
3472 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0) {
3473 vn_rangelock_unlock(vp, rl_cookie);
3475 VFS_UNLOCK_GIANT(vfslocked);
3478 NDFREE(&nd, NDF_ONLY_PNBUF);
3479 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3480 if (vp->v_type == VDIR)
3483 else if ((error = mac_vnode_check_write(td->td_ucred, NOCRED, vp))) {
3486 else if ((error = vn_writechk(vp)) == 0 &&
3487 (error = VOP_ACCESS(vp, VWRITE, td->td_ucred, td)) == 0) {
3489 vattr.va_size = length;
3490 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3493 vn_finished_write(mp);
3494 vn_rangelock_unlock(vp, rl_cookie);
3496 VFS_UNLOCK_GIANT(vfslocked);
3500 #if defined(COMPAT_43)
3502 * Truncate a file given its path name.
3504 #ifndef _SYS_SYSPROTO_H_
3505 struct otruncate_args {
3513 register struct otruncate_args /* {
3518 struct truncate_args /* {
3524 nuap.path = uap->path;
3525 nuap.length = uap->length;
3526 return (sys_truncate(td, &nuap));
3528 #endif /* COMPAT_43 */
3530 /* Versions with the pad argument */
3532 freebsd6_truncate(struct thread *td, struct freebsd6_truncate_args *uap)
3534 struct truncate_args ouap;
3536 ouap.path = uap->path;
3537 ouap.length = uap->length;
3538 return (sys_truncate(td, &ouap));
3542 freebsd6_ftruncate(struct thread *td, struct freebsd6_ftruncate_args *uap)
3544 struct ftruncate_args ouap;
3547 ouap.length = uap->length;
3548 return (sys_ftruncate(td, &ouap));
3552 * Sync an open file.
3554 #ifndef _SYS_SYSPROTO_H_
3562 struct fsync_args /* {
3570 int error, lock_flags;
3572 AUDIT_ARG_FD(uap->fd);
3573 if ((error = getvnode(td->td_proc->p_fd, uap->fd, CAP_FSYNC,
3577 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
3578 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
3580 if (MNT_SHARED_WRITES(mp) ||
3581 ((mp == NULL) && MNT_SHARED_WRITES(vp->v_mount))) {
3582 lock_flags = LK_SHARED;
3584 lock_flags = LK_EXCLUSIVE;
3586 vn_lock(vp, lock_flags | LK_RETRY);
3587 AUDIT_ARG_VNODE1(vp);
3588 if (vp->v_object != NULL) {
3589 VM_OBJECT_LOCK(vp->v_object);
3590 vm_object_page_clean(vp->v_object, 0, 0, 0);
3591 VM_OBJECT_UNLOCK(vp->v_object);
3593 error = VOP_FSYNC(vp, MNT_WAIT, td);
3596 vn_finished_write(mp);
3598 VFS_UNLOCK_GIANT(vfslocked);
3604 * Rename files. Source and destination must either both be directories, or
3605 * both not be directories. If target is a directory, it must be empty.
3607 #ifndef _SYS_SYSPROTO_H_
3608 struct rename_args {
3616 register struct rename_args /* {
3622 return (kern_rename(td, uap->from, uap->to, UIO_USERSPACE));
3625 #ifndef _SYS_SYSPROTO_H_
3626 struct renameat_args {
3634 sys_renameat(struct thread *td, struct renameat_args *uap)
3637 return (kern_renameat(td, uap->oldfd, uap->old, uap->newfd, uap->new,
3642 kern_rename(struct thread *td, char *from, char *to, enum uio_seg pathseg)
3645 return (kern_renameat(td, AT_FDCWD, from, AT_FDCWD, to, pathseg));
3649 kern_renameat(struct thread *td, int oldfd, char *old, int newfd, char *new,
3650 enum uio_seg pathseg)
3652 struct mount *mp = NULL;
3653 struct vnode *tvp, *fvp, *tdvp;
3654 struct nameidata fromnd, tond;
3661 NDINIT_ATRIGHTS(&fromnd, DELETE, LOCKPARENT | LOCKLEAF | SAVESTART |
3662 MPSAFE | AUDITVNODE1, pathseg, old, oldfd, CAP_DELETE, td);
3664 NDINIT_ATRIGHTS(&fromnd, DELETE, WANTPARENT | SAVESTART | MPSAFE |
3665 AUDITVNODE1, pathseg, old, oldfd, CAP_DELETE, td);
3668 if ((error = namei(&fromnd)) != 0)
3670 fvfslocked = NDHASGIANT(&fromnd);
3673 error = mac_vnode_check_rename_from(td->td_ucred, fromnd.ni_dvp,
3674 fromnd.ni_vp, &fromnd.ni_cnd);
3675 VOP_UNLOCK(fromnd.ni_dvp, 0);
3676 if (fromnd.ni_dvp != fromnd.ni_vp)
3677 VOP_UNLOCK(fromnd.ni_vp, 0);
3681 error = vn_start_write(fvp, &mp, V_WAIT | PCATCH);
3683 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3684 vrele(fromnd.ni_dvp);
3688 NDINIT_ATRIGHTS(&tond, RENAME, LOCKPARENT | LOCKLEAF | NOCACHE |
3689 SAVESTART | MPSAFE | AUDITVNODE2, pathseg, new, newfd, CAP_CREATE,
3691 if (fromnd.ni_vp->v_type == VDIR)
3692 tond.ni_cnd.cn_flags |= WILLBEDIR;
3693 if ((error = namei(&tond)) != 0) {
3694 /* Translate error code for rename("dir1", "dir2/."). */
3695 if (error == EISDIR && fvp->v_type == VDIR)
3697 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3698 vrele(fromnd.ni_dvp);
3700 vn_finished_write(mp);
3703 tvfslocked = NDHASGIANT(&tond);
3707 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
3710 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
3720 * If the source is the same as the destination (that is, if they
3721 * are links to the same vnode), then there is nothing to do.
3727 error = mac_vnode_check_rename_to(td->td_ucred, tdvp,
3728 tond.ni_vp, fromnd.ni_dvp == tdvp, &tond.ni_cnd);
3732 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
3733 tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
3734 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3735 NDFREE(&tond, NDF_ONLY_PNBUF);
3737 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3738 NDFREE(&tond, NDF_ONLY_PNBUF);
3745 vrele(fromnd.ni_dvp);
3748 vrele(tond.ni_startdir);
3749 vn_finished_write(mp);
3751 if (fromnd.ni_startdir)
3752 vrele(fromnd.ni_startdir);
3753 VFS_UNLOCK_GIANT(fvfslocked);
3754 VFS_UNLOCK_GIANT(tvfslocked);
3761 * Make a directory file.
3763 #ifndef _SYS_SYSPROTO_H_
3772 register struct mkdir_args /* {
3778 return (kern_mkdir(td, uap->path, UIO_USERSPACE, uap->mode));
3781 #ifndef _SYS_SYSPROTO_H_
3782 struct mkdirat_args {
3789 sys_mkdirat(struct thread *td, struct mkdirat_args *uap)
3792 return (kern_mkdirat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode));
3796 kern_mkdir(struct thread *td, char *path, enum uio_seg segflg, int mode)
3799 return (kern_mkdirat(td, AT_FDCWD, path, segflg, mode));
3803 kern_mkdirat(struct thread *td, int fd, char *path, enum uio_seg segflg,
3810 struct nameidata nd;
3813 AUDIT_ARG_MODE(mode);
3816 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | MPSAFE |
3817 AUDITVNODE1, segflg, path, fd, CAP_MKDIR, td);
3818 nd.ni_cnd.cn_flags |= WILLBEDIR;
3819 if ((error = namei(&nd)) != 0)
3821 vfslocked = NDHASGIANT(&nd);
3824 NDFREE(&nd, NDF_ONLY_PNBUF);
3826 * XXX namei called with LOCKPARENT but not LOCKLEAF has
3827 * the strange behaviour of leaving the vnode unlocked
3828 * if the target is the same vnode as the parent.
3830 if (vp == nd.ni_dvp)
3835 VFS_UNLOCK_GIANT(vfslocked);
3838 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3839 NDFREE(&nd, NDF_ONLY_PNBUF);
3841 VFS_UNLOCK_GIANT(vfslocked);
3842 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3847 vattr.va_type = VDIR;
3848 vattr.va_mode = (mode & ACCESSPERMS) &~ td->td_proc->p_fd->fd_cmask;
3850 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
3855 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
3859 NDFREE(&nd, NDF_ONLY_PNBUF);
3863 vn_finished_write(mp);
3864 VFS_UNLOCK_GIANT(vfslocked);
3869 * Remove a directory file.
3871 #ifndef _SYS_SYSPROTO_H_
3879 struct rmdir_args /* {
3884 return (kern_rmdir(td, uap->path, UIO_USERSPACE));
3888 kern_rmdir(struct thread *td, char *path, enum uio_seg pathseg)
3891 return (kern_rmdirat(td, AT_FDCWD, path, pathseg));
3895 kern_rmdirat(struct thread *td, int fd, char *path, enum uio_seg pathseg)
3900 struct nameidata nd;
3905 NDINIT_ATRIGHTS(&nd, DELETE, LOCKPARENT | LOCKLEAF | MPSAFE |
3906 AUDITVNODE1, pathseg, path, fd, CAP_RMDIR, td);
3907 if ((error = namei(&nd)) != 0)
3909 vfslocked = NDHASGIANT(&nd);
3911 if (vp->v_type != VDIR) {
3916 * No rmdir "." please.
3918 if (nd.ni_dvp == vp) {
3923 * The root of a mounted filesystem cannot be deleted.
3925 if (vp->v_vflag & VV_ROOT) {
3930 error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp,
3935 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3936 NDFREE(&nd, NDF_ONLY_PNBUF);
3938 if (nd.ni_dvp == vp)
3942 VFS_UNLOCK_GIANT(vfslocked);
3943 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3947 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_UNLINK);
3948 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
3949 vn_finished_write(mp);
3951 NDFREE(&nd, NDF_ONLY_PNBUF);
3953 if (nd.ni_dvp == vp)
3957 VFS_UNLOCK_GIANT(vfslocked);
3963 * Read a block of directory entries in a filesystem independent format.
3965 #ifndef _SYS_SYSPROTO_H_
3966 struct ogetdirentries_args {
3974 ogetdirentries(struct thread *td, struct ogetdirentries_args *uap)
3979 error = kern_ogetdirentries(td, uap, &loff);
3981 error = copyout(&loff, uap->basep, sizeof(long));
3986 kern_ogetdirentries(struct thread *td, struct ogetdirentries_args *uap,
3991 struct uio auio, kuio;
3992 struct iovec aiov, kiov;
3993 struct dirent *dp, *edp;
3995 int error, eofflag, readcnt, vfslocked;
3999 /* XXX arbitrary sanity limit on `count'. */
4000 if (uap->count > 64 * 1024)
4002 if ((error = getvnode(td->td_proc->p_fd, uap->fd, CAP_READ,
4005 if ((fp->f_flag & FREAD) == 0) {
4010 foffset = foffset_lock(fp, 0);
4012 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
4013 if (vp->v_type != VDIR) {
4014 VFS_UNLOCK_GIANT(vfslocked);
4015 foffset_unlock(fp, foffset, 0);
4019 aiov.iov_base = uap->buf;
4020 aiov.iov_len = uap->count;
4021 auio.uio_iov = &aiov;
4022 auio.uio_iovcnt = 1;
4023 auio.uio_rw = UIO_READ;
4024 auio.uio_segflg = UIO_USERSPACE;
4026 auio.uio_resid = uap->count;
4027 vn_lock(vp, LK_SHARED | LK_RETRY);
4028 loff = auio.uio_offset = foffset;
4030 error = mac_vnode_check_readdir(td->td_ucred, vp);
4033 VFS_UNLOCK_GIANT(vfslocked);
4034 foffset_unlock(fp, foffset, FOF_NOUPDATE);
4039 # if (BYTE_ORDER != LITTLE_ENDIAN)
4040 if (vp->v_mount->mnt_maxsymlinklen <= 0) {
4041 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag,
4043 foffset = auio.uio_offset;
4048 kuio.uio_iov = &kiov;
4049 kuio.uio_segflg = UIO_SYSSPACE;
4050 kiov.iov_len = uap->count;
4051 dirbuf = malloc(uap->count, M_TEMP, M_WAITOK);
4052 kiov.iov_base = dirbuf;
4053 error = VOP_READDIR(vp, &kuio, fp->f_cred, &eofflag,
4055 foffset = kuio.uio_offset;
4057 readcnt = uap->count - kuio.uio_resid;
4058 edp = (struct dirent *)&dirbuf[readcnt];
4059 for (dp = (struct dirent *)dirbuf; dp < edp; ) {
4060 # if (BYTE_ORDER == LITTLE_ENDIAN)
4062 * The expected low byte of
4063 * dp->d_namlen is our dp->d_type.
4064 * The high MBZ byte of dp->d_namlen
4065 * is our dp->d_namlen.
4067 dp->d_type = dp->d_namlen;
4071 * The dp->d_type is the high byte
4072 * of the expected dp->d_namlen,
4073 * so must be zero'ed.
4077 if (dp->d_reclen > 0) {
4078 dp = (struct dirent *)
4079 ((char *)dp + dp->d_reclen);
4086 error = uiomove(dirbuf, readcnt, &auio);
4088 free(dirbuf, M_TEMP);
4092 VFS_UNLOCK_GIANT(vfslocked);
4093 foffset_unlock(fp, foffset, 0);
4097 if (uap->count == auio.uio_resid &&
4098 (vp->v_vflag & VV_ROOT) &&
4099 (vp->v_mount->mnt_flag & MNT_UNION)) {
4100 struct vnode *tvp = vp;
4101 vp = vp->v_mount->mnt_vnodecovered;
4107 VFS_UNLOCK_GIANT(vfslocked);
4111 VFS_UNLOCK_GIANT(vfslocked);
4112 foffset_unlock(fp, foffset, 0);
4114 td->td_retval[0] = uap->count - auio.uio_resid;
4119 #endif /* COMPAT_43 */
4122 * Read a block of directory entries in a filesystem independent format.
4124 #ifndef _SYS_SYSPROTO_H_
4125 struct getdirentries_args {
4133 sys_getdirentries(td, uap)
4135 register struct getdirentries_args /* {
4145 error = kern_getdirentries(td, uap->fd, uap->buf, uap->count, &base);
4148 if (uap->basep != NULL)
4149 error = copyout(&base, uap->basep, sizeof(long));
4154 kern_getdirentries(struct thread *td, int fd, char *buf, u_int count,
4167 auio.uio_resid = count;
4168 if (auio.uio_resid > IOSIZE_MAX)
4170 if ((error = getvnode(td->td_proc->p_fd, fd, CAP_READ | CAP_SEEK,
4173 if ((fp->f_flag & FREAD) == 0) {
4178 foffset = foffset_lock(fp, 0);
4180 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
4181 if (vp->v_type != VDIR) {
4182 VFS_UNLOCK_GIANT(vfslocked);
4186 aiov.iov_base = buf;
4187 aiov.iov_len = count;
4188 auio.uio_iov = &aiov;
4189 auio.uio_iovcnt = 1;
4190 auio.uio_rw = UIO_READ;
4191 auio.uio_segflg = UIO_USERSPACE;
4193 vn_lock(vp, LK_SHARED | LK_RETRY);
4194 AUDIT_ARG_VNODE1(vp);
4195 loff = auio.uio_offset = foffset;
4197 error = mac_vnode_check_readdir(td->td_ucred, vp);
4200 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL,
4202 foffset = auio.uio_offset;
4205 VFS_UNLOCK_GIANT(vfslocked);
4208 if (count == auio.uio_resid &&
4209 (vp->v_vflag & VV_ROOT) &&
4210 (vp->v_mount->mnt_flag & MNT_UNION)) {
4211 struct vnode *tvp = vp;
4212 vp = vp->v_mount->mnt_vnodecovered;
4218 VFS_UNLOCK_GIANT(vfslocked);
4222 VFS_UNLOCK_GIANT(vfslocked);
4224 td->td_retval[0] = count - auio.uio_resid;
4226 foffset_unlock(fp, foffset, 0);
4231 #ifndef _SYS_SYSPROTO_H_
4232 struct getdents_args {
4239 sys_getdents(td, uap)
4241 register struct getdents_args /* {
4247 struct getdirentries_args ap;
4250 ap.count = uap->count;
4252 return (sys_getdirentries(td, &ap));
4256 * Set the mode mask for creation of filesystem nodes.
4258 #ifndef _SYS_SYSPROTO_H_
4266 struct umask_args /* {
4270 register struct filedesc *fdp;
4272 FILEDESC_XLOCK(td->td_proc->p_fd);
4273 fdp = td->td_proc->p_fd;
4274 td->td_retval[0] = fdp->fd_cmask;
4275 fdp->fd_cmask = uap->newmask & ALLPERMS;
4276 FILEDESC_XUNLOCK(td->td_proc->p_fd);
4281 * Void all references to file by ripping underlying filesystem away from
4284 #ifndef _SYS_SYSPROTO_H_
4285 struct revoke_args {
4292 register struct revoke_args /* {
4299 struct nameidata nd;
4302 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | MPSAFE | AUDITVNODE1,
4303 UIO_USERSPACE, uap->path, td);
4304 if ((error = namei(&nd)) != 0)
4306 vfslocked = NDHASGIANT(&nd);
4308 NDFREE(&nd, NDF_ONLY_PNBUF);
4309 if (vp->v_type != VCHR || vp->v_rdev == NULL) {
4314 error = mac_vnode_check_revoke(td->td_ucred, vp);
4318 error = VOP_GETATTR(vp, &vattr, td->td_ucred);
4321 if (td->td_ucred->cr_uid != vattr.va_uid) {
4322 error = priv_check(td, PRIV_VFS_ADMIN);
4327 VOP_REVOKE(vp, REVOKEALL);
4330 VFS_UNLOCK_GIANT(vfslocked);
4335 * Convert a user file descriptor to a kernel file entry and check that, if it
4336 * is a capability, the correct rights are present. A reference on the file
4337 * entry is held upon returning.
4340 getvnode(struct filedesc *fdp, int fd, cap_rights_t rights,
4345 struct file *fp_fromcap;
4351 if ((fdp == NULL) || (fp = fget_unlocked(fdp, fd)) == NULL)
4355 * If the file descriptor is for a capability, test rights and use the
4356 * file descriptor referenced by the capability.
4358 error = cap_funwrap(fp, rights, &fp_fromcap);
4360 fdrop(fp, curthread);
4363 if (fp != fp_fromcap) {
4365 fdrop(fp, curthread);
4368 #endif /* CAPABILITIES */
4371 * The file could be not of the vnode type, or it may be not
4372 * yet fully initialized, in which case the f_vnode pointer
4373 * may be set, but f_ops is still badfileops. E.g.,
4374 * devfs_open() transiently create such situation to
4375 * facilitate csw d_fdopen().
4377 * Dupfdopen() handling in kern_openat() installs the
4378 * half-baked file into the process descriptor table, allowing
4379 * other thread to dereference it. Guard against the race by
4382 if (fp->f_vnode == NULL || fp->f_ops == &badfileops) {
4383 fdrop(fp, curthread);
4392 * Get an (NFS) file handle.
4394 #ifndef _SYS_SYSPROTO_H_
4395 struct lgetfh_args {
4403 register struct lgetfh_args *uap;
4405 struct nameidata nd;
4407 register struct vnode *vp;
4411 error = priv_check(td, PRIV_VFS_GETFH);
4414 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | MPSAFE | AUDITVNODE1,
4415 UIO_USERSPACE, uap->fname, td);
4419 vfslocked = NDHASGIANT(&nd);
4420 NDFREE(&nd, NDF_ONLY_PNBUF);
4422 bzero(&fh, sizeof(fh));
4423 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
4424 error = VOP_VPTOFH(vp, &fh.fh_fid);
4426 VFS_UNLOCK_GIANT(vfslocked);
4429 error = copyout(&fh, uap->fhp, sizeof (fh));
4433 #ifndef _SYS_SYSPROTO_H_
4442 register struct getfh_args *uap;
4444 struct nameidata nd;
4446 register struct vnode *vp;
4450 error = priv_check(td, PRIV_VFS_GETFH);
4453 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | MPSAFE | AUDITVNODE1,
4454 UIO_USERSPACE, uap->fname, td);
4458 vfslocked = NDHASGIANT(&nd);
4459 NDFREE(&nd, NDF_ONLY_PNBUF);
4461 bzero(&fh, sizeof(fh));
4462 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
4463 error = VOP_VPTOFH(vp, &fh.fh_fid);
4465 VFS_UNLOCK_GIANT(vfslocked);
4468 error = copyout(&fh, uap->fhp, sizeof (fh));
4473 * syscall for the rpc.lockd to use to translate a NFS file handle into an
4476 * warning: do not remove the priv_check() call or this becomes one giant
4479 #ifndef _SYS_SYSPROTO_H_
4480 struct fhopen_args {
4481 const struct fhandle *u_fhp;
4488 struct fhopen_args /* {
4489 const struct fhandle *u_fhp;
4493 struct proc *p = td->td_proc;
4498 struct vattr *vap = &vat;
4501 register struct filedesc *fdp = p->p_fd;
4502 int fmode, error, type;
4508 error = priv_check(td, PRIV_VFS_FHOPEN);
4511 fmode = FFLAGS(uap->flags);
4512 /* why not allow a non-read/write open for our lockd? */
4513 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
4515 error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
4518 /* find the mount point */
4519 mp = vfs_busyfs(&fhp.fh_fsid);
4522 vfslocked = VFS_LOCK_GIANT(mp);
4523 /* now give me my vnode, it gets returned to me locked */
4524 error = VFS_FHTOVP(mp, &fhp.fh_fid, LK_EXCLUSIVE, &vp);
4529 * from now on we have to make sure not
4530 * to forget about the vnode
4531 * any error that causes an abort must vput(vp)
4532 * just set error = err and 'goto bad;'.
4538 if (vp->v_type == VLNK) {
4542 if (vp->v_type == VSOCK) {
4546 if (vp->v_type != VDIR && fmode & O_DIRECTORY) {
4551 if (fmode & (FWRITE | O_TRUNC)) {
4552 if (vp->v_type == VDIR) {
4556 error = vn_writechk(vp);
4563 if ((fmode & O_APPEND) && (fmode & FWRITE))
4566 error = mac_vnode_check_open(td->td_ucred, vp, accmode);
4571 error = VOP_ACCESS(vp, accmode, td->td_ucred, td);
4575 if (fmode & O_TRUNC) {
4577 VOP_UNLOCK(vp, 0); /* XXX */
4578 if ((error = vn_start_write(NULL, &mp, V_WAIT | PCATCH)) != 0) {
4583 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); /* XXX */
4587 * We don't yet have fp->f_cred, so use td->td_ucred, which
4590 error = mac_vnode_check_write(td->td_ucred, td->td_ucred, vp);
4595 error = VOP_SETATTR(vp, vap, td->td_ucred);
4599 vn_finished_write(mp);
4603 error = VOP_OPEN(vp, fmode, td->td_ucred, td, NULL);
4611 * end of vn_open code
4614 if ((error = falloc(td, &nfp, &indx, fmode)) != 0) {
4619 /* An extra reference on `nfp' has been held for us by falloc(). */
4622 finit(nfp, fmode & FMASK, DTYPE_VNODE, vp, &vnops);
4623 if (fmode & (O_EXLOCK | O_SHLOCK)) {
4624 lf.l_whence = SEEK_SET;
4627 if (fmode & O_EXLOCK)
4628 lf.l_type = F_WRLCK;
4630 lf.l_type = F_RDLCK;
4632 if ((fmode & FNONBLOCK) == 0)
4635 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf,
4638 * The lock request failed. Normally close the
4639 * descriptor but handle the case where someone might
4640 * have dup()d or close()d it when we weren't looking.
4642 fdclose(fdp, fp, indx, td);
4645 * release our private reference
4650 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
4651 atomic_set_int(&fp->f_flag, FHASLOCK);
4656 VFS_UNLOCK_GIANT(vfslocked);
4657 td->td_retval[0] = indx;
4663 VFS_UNLOCK_GIANT(vfslocked);
4668 * Stat an (NFS) file handle.
4670 #ifndef _SYS_SYSPROTO_H_
4671 struct fhstat_args {
4672 struct fhandle *u_fhp;
4679 register struct fhstat_args /* {
4680 struct fhandle *u_fhp;
4691 error = priv_check(td, PRIV_VFS_FHSTAT);
4694 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4697 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4699 vfslocked = VFS_LOCK_GIANT(mp);
4700 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_EXCLUSIVE, &vp);
4703 VFS_UNLOCK_GIANT(vfslocked);
4706 error = vn_stat(vp, &sb, td->td_ucred, NOCRED, td);
4708 VFS_UNLOCK_GIANT(vfslocked);
4711 error = copyout(&sb, uap->sb, sizeof(sb));
4716 * Implement fstatfs() for (NFS) file handles.
4718 #ifndef _SYS_SYSPROTO_H_
4719 struct fhstatfs_args {
4720 struct fhandle *u_fhp;
4725 sys_fhstatfs(td, uap)
4727 struct fhstatfs_args /* {
4728 struct fhandle *u_fhp;
4736 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4739 error = kern_fhstatfs(td, fh, &sf);
4742 return (copyout(&sf, uap->buf, sizeof(sf)));
4746 kern_fhstatfs(struct thread *td, fhandle_t fh, struct statfs *buf)
4754 error = priv_check(td, PRIV_VFS_FHSTATFS);
4757 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4759 vfslocked = VFS_LOCK_GIANT(mp);
4760 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_EXCLUSIVE, &vp);
4763 VFS_UNLOCK_GIANT(vfslocked);
4767 error = prison_canseemount(td->td_ucred, mp);
4771 error = mac_mount_check_stat(td->td_ucred, mp);
4776 * Set these in case the underlying filesystem fails to do so.
4779 sp->f_version = STATFS_VERSION;
4780 sp->f_namemax = NAME_MAX;
4781 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
4782 error = VFS_STATFS(mp, sp);
4787 VFS_UNLOCK_GIANT(vfslocked);
4792 kern_posix_fallocate(struct thread *td, int fd, off_t offset, off_t len)
4797 off_t olen, ooffset;
4798 int error, vfslocked;
4802 error = fget(td, fd, CAP_WRITE, &fp);
4806 switch (fp->f_type) {
4817 if ((fp->f_flag & FWRITE) == 0) {
4822 if (vp->v_type != VREG) {
4826 if (offset < 0 || len <= 0) {
4830 /* Check for wrap. */
4831 if (offset > OFF_MAX - len) {
4836 /* Allocating blocks may take a long time, so iterate. */
4842 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
4844 error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
4846 VFS_UNLOCK_GIANT(vfslocked);
4849 error = vn_lock(vp, LK_EXCLUSIVE);
4851 vn_finished_write(mp);
4852 VFS_UNLOCK_GIANT(vfslocked);
4856 error = mac_vnode_check_write(td->td_ucred, fp->f_cred, vp);
4859 error = VOP_ALLOCATE(vp, &offset, &len);
4861 vn_finished_write(mp);
4862 VFS_UNLOCK_GIANT(vfslocked);
4864 if (olen + ooffset != offset + len) {
4865 panic("offset + len changed from %jx/%jx to %jx/%jx",
4866 ooffset, olen, offset, len);
4868 if (error != 0 || len == 0)
4870 KASSERT(olen > len, ("Iteration did not make progress?"));
4880 sys_posix_fallocate(struct thread *td, struct posix_fallocate_args *uap)
4883 td->td_retval[0] = kern_posix_fallocate(td, uap->fd, uap->offset,
4889 * Unlike madvise(2), we do not make a best effort to remember every
4890 * possible caching hint. Instead, we remember the last setting with
4891 * the exception that we will allow POSIX_FADV_NORMAL to adjust the
4892 * region of any current setting.
4895 kern_posix_fadvise(struct thread *td, int fd, off_t offset, off_t len,
4898 struct fadvise_info *fa, *new;
4904 if (offset < 0 || len < 0 || offset > OFF_MAX - len)
4907 case POSIX_FADV_SEQUENTIAL:
4908 case POSIX_FADV_RANDOM:
4909 case POSIX_FADV_NOREUSE:
4910 new = malloc(sizeof(*fa), M_FADVISE, M_WAITOK);
4912 case POSIX_FADV_NORMAL:
4913 case POSIX_FADV_WILLNEED:
4914 case POSIX_FADV_DONTNEED:
4920 /* XXX: CAP_POSIX_FADVISE? */
4921 error = fget(td, fd, 0, &fp);
4925 switch (fp->f_type) {
4937 if (vp->v_type != VREG) {
4944 end = offset + len - 1;
4946 case POSIX_FADV_SEQUENTIAL:
4947 case POSIX_FADV_RANDOM:
4948 case POSIX_FADV_NOREUSE:
4950 * Try to merge any existing non-standard region with
4951 * this new region if possible, otherwise create a new
4952 * non-standard region for this request.
4954 mtx_pool_lock(mtxpool_sleep, fp);
4956 if (fa != NULL && fa->fa_advice == advice &&
4957 ((fa->fa_start <= end && fa->fa_end >= offset) ||
4958 (end != OFF_MAX && fa->fa_start == end + 1) ||
4959 (fa->fa_end != OFF_MAX && fa->fa_end + 1 == offset))) {
4960 if (offset < fa->fa_start)
4961 fa->fa_start = offset;
4962 if (end > fa->fa_end)
4965 new->fa_advice = advice;
4966 new->fa_start = offset;
4968 new->fa_prevstart = 0;
4969 new->fa_prevend = 0;
4973 mtx_pool_unlock(mtxpool_sleep, fp);
4975 case POSIX_FADV_NORMAL:
4977 * If a the "normal" region overlaps with an existing
4978 * non-standard region, trim or remove the
4979 * non-standard region.
4981 mtx_pool_lock(mtxpool_sleep, fp);
4984 if (offset <= fa->fa_start && end >= fa->fa_end) {
4986 fp->f_advice = NULL;
4987 } else if (offset <= fa->fa_start &&
4988 end >= fa->fa_start)
4989 fa->fa_start = end + 1;
4990 else if (offset <= fa->fa_end && end >= fa->fa_end)
4991 fa->fa_end = offset - 1;
4992 else if (offset >= fa->fa_start && end <= fa->fa_end) {
4994 * If the "normal" region is a middle
4995 * portion of the existing
4996 * non-standard region, just remove
4997 * the whole thing rather than picking
4998 * one side or the other to
5002 fp->f_advice = NULL;
5005 mtx_pool_unlock(mtxpool_sleep, fp);
5007 case POSIX_FADV_WILLNEED:
5008 case POSIX_FADV_DONTNEED:
5009 error = VOP_ADVISE(vp, offset, end, advice);
5015 free(new, M_FADVISE);
5020 sys_posix_fadvise(struct thread *td, struct posix_fadvise_args *uap)
5023 td->td_retval[0] = kern_posix_fadvise(td, uap->fd, uap->offset,
5024 uap->len, uap->advice);