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_DEFINE(vfs, , stat, mode, mode);
95 SDT_PROBE_ARGTYPE(vfs, , stat, mode, 0, "char *");
96 SDT_PROBE_ARGTYPE(vfs, , stat, mode, 1, "int");
97 SDT_PROBE_DEFINE(vfs, , stat, reg, reg);
98 SDT_PROBE_ARGTYPE(vfs, , stat, reg, 0, "char *");
99 SDT_PROBE_ARGTYPE(vfs, , stat, reg, 1, "int");
101 static int chroot_refuse_vdir_fds(struct filedesc *fdp);
102 static int getutimes(const struct timeval *, enum uio_seg, struct timespec *);
103 static int setfflags(struct thread *td, struct vnode *, int);
104 static int setutimes(struct thread *td, struct vnode *,
105 const struct timespec *, int, int);
106 static int vn_access(struct vnode *vp, int user_flags, struct ucred *cred,
110 * The module initialization routine for POSIX asynchronous I/O will
111 * set this to the version of AIO that it implements. (Zero means
112 * that it is not implemented.) This value is used here by pathconf()
113 * and in kern_descrip.c by fpathconf().
115 int async_io_version;
118 static int syncprt = 0;
119 SYSCTL_INT(_debug, OID_AUTO, syncprt, CTLFLAG_RW, &syncprt, 0, "");
123 * Sync each mounted filesystem.
125 #ifndef _SYS_SYSPROTO_H_
134 struct sync_args *uap;
136 struct mount *mp, *nmp;
139 mtx_lock(&mountlist_mtx);
140 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
141 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK)) {
142 nmp = TAILQ_NEXT(mp, mnt_list);
145 vfslocked = VFS_LOCK_GIANT(mp);
146 if ((mp->mnt_flag & MNT_RDONLY) == 0 &&
147 vn_start_write(NULL, &mp, V_NOWAIT) == 0) {
148 save = curthread_pflags_set(TDP_SYNCIO);
149 vfs_msync(mp, MNT_NOWAIT);
150 VFS_SYNC(mp, MNT_NOWAIT);
151 curthread_pflags_restore(save);
152 vn_finished_write(mp);
154 VFS_UNLOCK_GIANT(vfslocked);
155 mtx_lock(&mountlist_mtx);
156 nmp = TAILQ_NEXT(mp, mnt_list);
159 mtx_unlock(&mountlist_mtx);
164 * Change filesystem quotas.
166 #ifndef _SYS_SYSPROTO_H_
167 struct quotactl_args {
175 sys_quotactl(td, uap)
177 register struct quotactl_args /* {
189 AUDIT_ARG_CMD(uap->cmd);
190 AUDIT_ARG_UID(uap->uid);
191 if (!prison_allow(td->td_ucred, PR_ALLOW_QUOTAS))
193 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | MPSAFE | AUDITVNODE1,
194 UIO_USERSPACE, uap->path, td);
195 if ((error = namei(&nd)) != 0)
197 vfslocked = NDHASGIANT(&nd);
198 NDFREE(&nd, NDF_ONLY_PNBUF);
199 mp = nd.ni_vp->v_mount;
202 error = vfs_busy(mp, 0);
205 VFS_UNLOCK_GIANT(vfslocked);
208 error = VFS_QUOTACTL(mp, uap->cmd, uap->uid, uap->arg);
211 * Since quota on operation typically needs to open quota
212 * file, the Q_QUOTAON handler needs to unbusy the mount point
213 * before calling into namei. Otherwise, unmount might be
214 * started between two vfs_busy() invocations (first is our,
215 * second is from mount point cross-walk code in lookup()),
218 * Require that Q_QUOTAON handles the vfs_busy() reference on
219 * its own, always returning with ubusied mount point.
221 if ((uap->cmd >> SUBCMDSHIFT) != Q_QUOTAON)
223 VFS_UNLOCK_GIANT(vfslocked);
228 * Used by statfs conversion routines to scale the block size up if
229 * necessary so that all of the block counts are <= 'max_size'. Note
230 * that 'max_size' should be a bitmask, i.e. 2^n - 1 for some non-zero
234 statfs_scale_blocks(struct statfs *sf, long max_size)
239 KASSERT(powerof2(max_size + 1), ("%s: invalid max_size", __func__));
242 * Attempt to scale the block counts to give a more accurate
243 * overview to userland of the ratio of free space to used
244 * space. To do this, find the largest block count and compute
245 * a divisor that lets it fit into a signed integer <= max_size.
247 if (sf->f_bavail < 0)
248 count = -sf->f_bavail;
250 count = sf->f_bavail;
251 count = MAX(sf->f_blocks, MAX(sf->f_bfree, count));
252 if (count <= max_size)
255 count >>= flsl(max_size);
262 sf->f_bsize <<= shift;
263 sf->f_blocks >>= shift;
264 sf->f_bfree >>= shift;
265 sf->f_bavail >>= shift;
269 * Get filesystem statistics.
271 #ifndef _SYS_SYSPROTO_H_
280 register struct statfs_args /* {
288 error = kern_statfs(td, uap->path, UIO_USERSPACE, &sf);
290 error = copyout(&sf, uap->buf, sizeof(sf));
295 kern_statfs(struct thread *td, char *path, enum uio_seg pathseg,
299 struct statfs *sp, sb;
304 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | MPSAFE |
305 AUDITVNODE1, pathseg, path, td);
309 vfslocked = NDHASGIANT(&nd);
310 mp = nd.ni_vp->v_mount;
312 NDFREE(&nd, NDF_ONLY_PNBUF);
314 error = vfs_busy(mp, 0);
317 VFS_UNLOCK_GIANT(vfslocked);
321 error = mac_mount_check_stat(td->td_ucred, mp);
326 * Set these in case the underlying filesystem fails to do so.
329 sp->f_version = STATFS_VERSION;
330 sp->f_namemax = NAME_MAX;
331 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
332 error = VFS_STATFS(mp, sp);
335 if (priv_check(td, PRIV_VFS_GENERATION)) {
336 bcopy(sp, &sb, sizeof(sb));
337 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
338 prison_enforce_statfs(td->td_ucred, mp, &sb);
344 VFS_UNLOCK_GIANT(vfslocked);
349 * Get filesystem statistics.
351 #ifndef _SYS_SYSPROTO_H_
352 struct fstatfs_args {
360 register struct fstatfs_args /* {
368 error = kern_fstatfs(td, uap->fd, &sf);
370 error = copyout(&sf, uap->buf, sizeof(sf));
375 kern_fstatfs(struct thread *td, int fd, struct statfs *buf)
379 struct statfs *sp, sb;
385 error = getvnode(td->td_proc->p_fd, fd, CAP_FSTATFS, &fp);
389 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
390 vn_lock(vp, LK_SHARED | LK_RETRY);
392 AUDIT_ARG_VNODE1(vp);
403 error = vfs_busy(mp, 0);
406 VFS_UNLOCK_GIANT(vfslocked);
410 error = mac_mount_check_stat(td->td_ucred, mp);
415 * Set these in case the underlying filesystem fails to do so.
418 sp->f_version = STATFS_VERSION;
419 sp->f_namemax = NAME_MAX;
420 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
421 error = VFS_STATFS(mp, sp);
424 if (priv_check(td, PRIV_VFS_GENERATION)) {
425 bcopy(sp, &sb, sizeof(sb));
426 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
427 prison_enforce_statfs(td->td_ucred, mp, &sb);
434 VFS_UNLOCK_GIANT(vfslocked);
439 * Get statistics on all filesystems.
441 #ifndef _SYS_SYSPROTO_H_
442 struct getfsstat_args {
449 sys_getfsstat(td, uap)
451 register struct getfsstat_args /* {
458 return (kern_getfsstat(td, &uap->buf, uap->bufsize, UIO_USERSPACE,
463 * If (bufsize > 0 && bufseg == UIO_SYSSPACE)
464 * The caller is responsible for freeing memory which will be allocated
468 kern_getfsstat(struct thread *td, struct statfs **buf, size_t bufsize,
469 enum uio_seg bufseg, int flags)
471 struct mount *mp, *nmp;
472 struct statfs *sfsp, *sp, sb;
473 size_t count, maxcount;
477 maxcount = bufsize / sizeof(struct statfs);
480 else if (bufseg == UIO_USERSPACE)
482 else /* if (bufseg == UIO_SYSSPACE) */ {
484 mtx_lock(&mountlist_mtx);
485 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
488 mtx_unlock(&mountlist_mtx);
489 if (maxcount > count)
491 sfsp = *buf = malloc(maxcount * sizeof(struct statfs), M_TEMP,
495 mtx_lock(&mountlist_mtx);
496 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
497 if (prison_canseemount(td->td_ucred, mp) != 0) {
498 nmp = TAILQ_NEXT(mp, mnt_list);
502 if (mac_mount_check_stat(td->td_ucred, mp) != 0) {
503 nmp = TAILQ_NEXT(mp, mnt_list);
507 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK)) {
508 nmp = TAILQ_NEXT(mp, mnt_list);
511 vfslocked = VFS_LOCK_GIANT(mp);
512 if (sfsp && count < maxcount) {
515 * Set these in case the underlying filesystem
518 sp->f_version = STATFS_VERSION;
519 sp->f_namemax = NAME_MAX;
520 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
522 * If MNT_NOWAIT or MNT_LAZY is specified, do not
523 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
524 * overrides MNT_WAIT.
526 if (((flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
527 (flags & MNT_WAIT)) &&
528 (error = VFS_STATFS(mp, sp))) {
529 VFS_UNLOCK_GIANT(vfslocked);
530 mtx_lock(&mountlist_mtx);
531 nmp = TAILQ_NEXT(mp, mnt_list);
535 if (priv_check(td, PRIV_VFS_GENERATION)) {
536 bcopy(sp, &sb, sizeof(sb));
537 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
538 prison_enforce_statfs(td->td_ucred, mp, &sb);
541 if (bufseg == UIO_SYSSPACE)
542 bcopy(sp, sfsp, sizeof(*sp));
543 else /* if (bufseg == UIO_USERSPACE) */ {
544 error = copyout(sp, sfsp, sizeof(*sp));
547 VFS_UNLOCK_GIANT(vfslocked);
553 VFS_UNLOCK_GIANT(vfslocked);
555 mtx_lock(&mountlist_mtx);
556 nmp = TAILQ_NEXT(mp, mnt_list);
559 mtx_unlock(&mountlist_mtx);
560 if (sfsp && count > maxcount)
561 td->td_retval[0] = maxcount;
563 td->td_retval[0] = count;
567 #ifdef COMPAT_FREEBSD4
569 * Get old format filesystem statistics.
571 static void cvtstatfs(struct statfs *, struct ostatfs *);
573 #ifndef _SYS_SYSPROTO_H_
574 struct freebsd4_statfs_args {
580 freebsd4_statfs(td, uap)
582 struct freebsd4_statfs_args /* {
591 error = kern_statfs(td, uap->path, UIO_USERSPACE, &sf);
594 cvtstatfs(&sf, &osb);
595 return (copyout(&osb, uap->buf, sizeof(osb)));
599 * Get filesystem statistics.
601 #ifndef _SYS_SYSPROTO_H_
602 struct freebsd4_fstatfs_args {
608 freebsd4_fstatfs(td, uap)
610 struct freebsd4_fstatfs_args /* {
619 error = kern_fstatfs(td, uap->fd, &sf);
622 cvtstatfs(&sf, &osb);
623 return (copyout(&osb, uap->buf, sizeof(osb)));
627 * Get statistics on all filesystems.
629 #ifndef _SYS_SYSPROTO_H_
630 struct freebsd4_getfsstat_args {
637 freebsd4_getfsstat(td, uap)
639 register struct freebsd4_getfsstat_args /* {
645 struct statfs *buf, *sp;
650 count = uap->bufsize / sizeof(struct ostatfs);
651 size = count * sizeof(struct statfs);
652 error = kern_getfsstat(td, &buf, size, UIO_SYSSPACE, uap->flags);
654 count = td->td_retval[0];
656 while (count > 0 && error == 0) {
658 error = copyout(&osb, uap->buf, sizeof(osb));
669 * Implement fstatfs() for (NFS) file handles.
671 #ifndef _SYS_SYSPROTO_H_
672 struct freebsd4_fhstatfs_args {
673 struct fhandle *u_fhp;
678 freebsd4_fhstatfs(td, uap)
680 struct freebsd4_fhstatfs_args /* {
681 struct fhandle *u_fhp;
690 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
693 error = kern_fhstatfs(td, fh, &sf);
696 cvtstatfs(&sf, &osb);
697 return (copyout(&osb, uap->buf, sizeof(osb)));
701 * Convert a new format statfs structure to an old format statfs structure.
709 statfs_scale_blocks(nsp, LONG_MAX);
710 bzero(osp, sizeof(*osp));
711 osp->f_bsize = nsp->f_bsize;
712 osp->f_iosize = MIN(nsp->f_iosize, LONG_MAX);
713 osp->f_blocks = nsp->f_blocks;
714 osp->f_bfree = nsp->f_bfree;
715 osp->f_bavail = nsp->f_bavail;
716 osp->f_files = MIN(nsp->f_files, LONG_MAX);
717 osp->f_ffree = MIN(nsp->f_ffree, LONG_MAX);
718 osp->f_owner = nsp->f_owner;
719 osp->f_type = nsp->f_type;
720 osp->f_flags = nsp->f_flags;
721 osp->f_syncwrites = MIN(nsp->f_syncwrites, LONG_MAX);
722 osp->f_asyncwrites = MIN(nsp->f_asyncwrites, LONG_MAX);
723 osp->f_syncreads = MIN(nsp->f_syncreads, LONG_MAX);
724 osp->f_asyncreads = MIN(nsp->f_asyncreads, LONG_MAX);
725 strlcpy(osp->f_fstypename, nsp->f_fstypename,
726 MIN(MFSNAMELEN, OMFSNAMELEN));
727 strlcpy(osp->f_mntonname, nsp->f_mntonname,
728 MIN(MNAMELEN, OMNAMELEN));
729 strlcpy(osp->f_mntfromname, nsp->f_mntfromname,
730 MIN(MNAMELEN, OMNAMELEN));
731 osp->f_fsid = nsp->f_fsid;
733 #endif /* COMPAT_FREEBSD4 */
736 * Change current working directory to a given file descriptor.
738 #ifndef _SYS_SYSPROTO_H_
746 struct fchdir_args /* {
750 register struct filedesc *fdp = td->td_proc->p_fd;
751 struct vnode *vp, *tdp, *vpold;
757 AUDIT_ARG_FD(uap->fd);
758 if ((error = getvnode(fdp, uap->fd, CAP_FCHDIR, &fp)) != 0)
763 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
764 vn_lock(vp, LK_SHARED | LK_RETRY);
765 AUDIT_ARG_VNODE1(vp);
766 error = change_dir(vp, td);
767 while (!error && (mp = vp->v_mountedhere) != NULL) {
771 tvfslocked = VFS_LOCK_GIANT(mp);
772 error = VFS_ROOT(mp, LK_SHARED, &tdp);
775 VFS_UNLOCK_GIANT(tvfslocked);
779 VFS_UNLOCK_GIANT(vfslocked);
781 vfslocked = tvfslocked;
785 VFS_UNLOCK_GIANT(vfslocked);
789 VFS_UNLOCK_GIANT(vfslocked);
791 vpold = fdp->fd_cdir;
793 FILEDESC_XUNLOCK(fdp);
794 vfslocked = VFS_LOCK_GIANT(vpold->v_mount);
796 VFS_UNLOCK_GIANT(vfslocked);
801 * Change current working directory (``.'').
803 #ifndef _SYS_SYSPROTO_H_
811 struct chdir_args /* {
816 return (kern_chdir(td, uap->path, UIO_USERSPACE));
820 kern_chdir(struct thread *td, char *path, enum uio_seg pathseg)
822 register struct filedesc *fdp = td->td_proc->p_fd;
828 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1 |
829 MPSAFE, pathseg, path, td);
830 if ((error = namei(&nd)) != 0)
832 vfslocked = NDHASGIANT(&nd);
833 if ((error = change_dir(nd.ni_vp, td)) != 0) {
835 VFS_UNLOCK_GIANT(vfslocked);
836 NDFREE(&nd, NDF_ONLY_PNBUF);
839 VOP_UNLOCK(nd.ni_vp, 0);
840 VFS_UNLOCK_GIANT(vfslocked);
841 NDFREE(&nd, NDF_ONLY_PNBUF);
844 fdp->fd_cdir = nd.ni_vp;
845 FILEDESC_XUNLOCK(fdp);
846 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
848 VFS_UNLOCK_GIANT(vfslocked);
853 * Helper function for raised chroot(2) security function: Refuse if
854 * any filedescriptors are open directories.
857 chroot_refuse_vdir_fds(fdp)
858 struct filedesc *fdp;
864 FILEDESC_LOCK_ASSERT(fdp);
866 for (fd = 0; fd < fdp->fd_nfiles ; fd++) {
867 fp = fget_locked(fdp, fd);
870 if (fp->f_type == DTYPE_VNODE) {
872 if (vp->v_type == VDIR)
880 * This sysctl determines if we will allow a process to chroot(2) if it
881 * has a directory open:
882 * 0: disallowed for all processes.
883 * 1: allowed for processes that were not already chroot(2)'ed.
884 * 2: allowed for all processes.
887 static int chroot_allow_open_directories = 1;
889 SYSCTL_INT(_kern, OID_AUTO, chroot_allow_open_directories, CTLFLAG_RW,
890 &chroot_allow_open_directories, 0, "");
893 * Change notion of root (``/'') directory.
895 #ifndef _SYS_SYSPROTO_H_
903 struct chroot_args /* {
911 error = priv_check(td, PRIV_VFS_CHROOT);
914 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | MPSAFE |
915 AUDITVNODE1, UIO_USERSPACE, uap->path, td);
919 vfslocked = NDHASGIANT(&nd);
920 if ((error = change_dir(nd.ni_vp, td)) != 0)
923 if ((error = mac_vnode_check_chroot(td->td_ucred, nd.ni_vp)))
926 VOP_UNLOCK(nd.ni_vp, 0);
927 error = change_root(nd.ni_vp, td);
929 VFS_UNLOCK_GIANT(vfslocked);
930 NDFREE(&nd, NDF_ONLY_PNBUF);
934 VFS_UNLOCK_GIANT(vfslocked);
936 NDFREE(&nd, NDF_ONLY_PNBUF);
941 * Common routine for chroot and chdir. Callers must provide a locked vnode
951 ASSERT_VOP_LOCKED(vp, "change_dir(): vp not locked");
952 if (vp->v_type != VDIR)
955 error = mac_vnode_check_chdir(td->td_ucred, vp);
959 error = VOP_ACCESS(vp, VEXEC, td->td_ucred, td);
964 * Common routine for kern_chroot() and jail_attach(). The caller is
965 * responsible for invoking priv_check() and mac_vnode_check_chroot() to
966 * authorize this operation.
973 struct filedesc *fdp;
978 VFS_ASSERT_GIANT(vp->v_mount);
979 fdp = td->td_proc->p_fd;
981 if (chroot_allow_open_directories == 0 ||
982 (chroot_allow_open_directories == 1 && fdp->fd_rdir != rootvnode)) {
983 error = chroot_refuse_vdir_fds(fdp);
985 FILEDESC_XUNLOCK(fdp);
989 oldvp = fdp->fd_rdir;
996 FILEDESC_XUNLOCK(fdp);
997 vfslocked = VFS_LOCK_GIANT(oldvp->v_mount);
999 VFS_UNLOCK_GIANT(vfslocked);
1003 static __inline cap_rights_t
1004 flags_to_rights(int flags)
1006 cap_rights_t rights = 0;
1008 switch ((flags & O_ACCMODE)) {
1018 rights |= CAP_WRITE;
1022 rights |= CAP_FEXECVE;
1026 if (flags & O_CREAT)
1027 rights |= CAP_CREATE;
1029 if (flags & O_TRUNC)
1030 rights |= CAP_FTRUNCATE;
1032 if ((flags & O_EXLOCK) || (flags & O_SHLOCK))
1033 rights |= CAP_FLOCK;
1039 * Check permissions, allocate an open file structure, and call the device
1040 * open routine if any.
1042 #ifndef _SYS_SYSPROTO_H_
1052 register struct open_args /* {
1059 return (kern_open(td, uap->path, UIO_USERSPACE, uap->flags, uap->mode));
1062 #ifndef _SYS_SYSPROTO_H_
1063 struct openat_args {
1071 sys_openat(struct thread *td, struct openat_args *uap)
1074 return (kern_openat(td, uap->fd, uap->path, UIO_USERSPACE, uap->flag,
1079 kern_open(struct thread *td, char *path, enum uio_seg pathseg, int flags,
1083 return (kern_openat(td, AT_FDCWD, path, pathseg, flags, mode));
1087 kern_openat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1088 int flags, int mode)
1090 struct proc *p = td->td_proc;
1091 struct filedesc *fdp = p->p_fd;
1096 int type, indx = -1, error, error_open;
1098 struct nameidata nd;
1100 cap_rights_t rights_needed = CAP_LOOKUP;
1102 AUDIT_ARG_FFLAGS(flags);
1103 AUDIT_ARG_MODE(mode);
1104 /* XXX: audit dirfd */
1105 rights_needed |= flags_to_rights(flags);
1107 * Only one of the O_EXEC, O_RDONLY, O_WRONLY and O_RDWR flags
1110 if (flags & O_EXEC) {
1111 if (flags & O_ACCMODE)
1113 } else if ((flags & O_ACCMODE) == O_ACCMODE)
1116 flags = FFLAGS(flags);
1119 * allocate the file descriptor, but don't install a descriptor yet
1121 error = falloc_noinstall(td, &nfp);
1124 /* An extra reference on `nfp' has been held for us by falloc_noinstall(). */
1126 /* Set the flags early so the finit in devfs can pick them up. */
1127 fp->f_flag = flags & FMASK;
1128 cmode = ((mode &~ fdp->fd_cmask) & ALLPERMS) &~ S_ISTXT;
1129 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | AUDITVNODE1 | MPSAFE, pathseg,
1130 path, fd, rights_needed, td);
1131 td->td_dupfd = -1; /* XXX check for fdopen */
1132 error = vn_open(&nd, &flags, cmode, fp);
1135 * If the vn_open replaced the method vector, something
1136 * wonderous happened deep below and we just pass it up
1137 * pretending we know what we do.
1139 if (error == ENXIO && fp->f_ops != &badfileops)
1143 * handle special fdopen() case. bleh. dupfdopen() is
1144 * responsible for dropping the old contents of ofiles[indx]
1147 * Don't do this for relative (capability) lookups; we don't
1148 * understand exactly what would happen, and we don't think
1149 * that it ever should.
1151 if ((nd.ni_strictrelative == 0) &&
1152 (error == ENODEV || error == ENXIO) &&
1153 (td->td_dupfd >= 0)) {
1154 /* XXX from fdopen */
1156 if ((error = finstall(td, fp, &indx, flags)) != 0)
1158 if ((error = dupfdopen(td, fdp, indx, td->td_dupfd,
1159 flags, error_open)) == 0)
1163 * Clean up the descriptor, but only if another thread hadn't
1164 * replaced or closed it.
1167 fdclose(fdp, fp, indx, td);
1170 if (error == ERESTART)
1175 vfslocked = NDHASGIANT(&nd);
1176 NDFREE(&nd, NDF_ONLY_PNBUF);
1180 * Store the vnode, for any f_type. Typically, the vnode use
1181 * count is decremented by direct call to vn_closefile() for
1182 * files that switched type in the cdevsw fdopen() method.
1186 * If the file wasn't claimed by devfs bind it to the normal
1187 * vnode operations here.
1189 if (fp->f_ops == &badfileops) {
1190 KASSERT(vp->v_type != VFIFO, ("Unexpected fifo."));
1192 finit(fp, flags & FMASK, DTYPE_VNODE, vp, &vnops);
1196 if (fp->f_type == DTYPE_VNODE && (flags & (O_EXLOCK | O_SHLOCK)) != 0) {
1197 lf.l_whence = SEEK_SET;
1200 if (flags & O_EXLOCK)
1201 lf.l_type = F_WRLCK;
1203 lf.l_type = F_RDLCK;
1205 if ((flags & FNONBLOCK) == 0)
1207 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf,
1210 atomic_set_int(&fp->f_flag, FHASLOCK);
1212 if (flags & O_TRUNC) {
1213 error = fo_truncate(fp, 0, td->td_ucred, td);
1217 VFS_UNLOCK_GIANT(vfslocked);
1220 * If we haven't already installed the FD (for dupfdopen), do so now.
1224 if (nd.ni_strictrelative == 1) {
1226 * We are doing a strict relative lookup; wrap the
1227 * result in a capability.
1229 if ((error = kern_capwrap(td, fp, nd.ni_baserights,
1234 if ((error = finstall(td, fp, &indx, flags)) != 0)
1240 * Release our private reference, leaving the one associated with
1241 * the descriptor table intact.
1244 td->td_retval[0] = indx;
1247 VFS_UNLOCK_GIANT(vfslocked);
1250 fdclose(fdp, fp, indx, td);
1252 td->td_retval[0] = -1;
1260 #ifndef _SYS_SYSPROTO_H_
1261 struct ocreat_args {
1269 register struct ocreat_args /* {
1275 return (kern_open(td, uap->path, UIO_USERSPACE,
1276 O_WRONLY | O_CREAT | O_TRUNC, uap->mode));
1278 #endif /* COMPAT_43 */
1281 * Create a special file.
1283 #ifndef _SYS_SYSPROTO_H_
1293 register struct mknod_args /* {
1300 return (kern_mknod(td, uap->path, UIO_USERSPACE, uap->mode, uap->dev));
1303 #ifndef _SYS_SYSPROTO_H_
1304 struct mknodat_args {
1312 sys_mknodat(struct thread *td, struct mknodat_args *uap)
1315 return (kern_mknodat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode,
1320 kern_mknod(struct thread *td, char *path, enum uio_seg pathseg, int mode,
1324 return (kern_mknodat(td, AT_FDCWD, path, pathseg, mode, dev));
1328 kern_mknodat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1336 struct nameidata nd;
1339 AUDIT_ARG_MODE(mode);
1341 switch (mode & S_IFMT) {
1344 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
1347 error = priv_check(td, PRIV_VFS_MKNOD_BAD);
1350 error = priv_check(td, PRIV_VFS_MKNOD_WHT);
1354 return (kern_mkfifoat(td, fd, path, pathseg, mode));
1364 NDINIT_ATRIGHTS(&nd, CREATE,
1365 LOCKPARENT | SAVENAME | MPSAFE | AUDITVNODE1, pathseg, path, fd,
1367 if ((error = namei(&nd)) != 0)
1369 vfslocked = NDHASGIANT(&nd);
1372 NDFREE(&nd, NDF_ONLY_PNBUF);
1373 if (vp == nd.ni_dvp)
1378 VFS_UNLOCK_GIANT(vfslocked);
1382 vattr.va_mode = (mode & ALLPERMS) &
1383 ~td->td_proc->p_fd->fd_cmask;
1384 vattr.va_rdev = dev;
1387 switch (mode & S_IFMT) {
1388 case S_IFMT: /* used by badsect to flag bad sectors */
1389 vattr.va_type = VBAD;
1392 vattr.va_type = VCHR;
1395 vattr.va_type = VBLK;
1401 panic("kern_mknod: invalid mode");
1404 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1405 NDFREE(&nd, NDF_ONLY_PNBUF);
1407 VFS_UNLOCK_GIANT(vfslocked);
1408 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1413 if (error == 0 && !whiteout)
1414 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp,
1415 &nd.ni_cnd, &vattr);
1419 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, CREATE);
1421 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp,
1422 &nd.ni_cnd, &vattr);
1427 NDFREE(&nd, NDF_ONLY_PNBUF);
1429 vn_finished_write(mp);
1430 VFS_UNLOCK_GIANT(vfslocked);
1435 * Create a named pipe.
1437 #ifndef _SYS_SYSPROTO_H_
1438 struct mkfifo_args {
1446 register struct mkfifo_args /* {
1452 return (kern_mkfifo(td, uap->path, UIO_USERSPACE, uap->mode));
1455 #ifndef _SYS_SYSPROTO_H_
1456 struct mkfifoat_args {
1463 sys_mkfifoat(struct thread *td, struct mkfifoat_args *uap)
1466 return (kern_mkfifoat(td, uap->fd, uap->path, UIO_USERSPACE,
1471 kern_mkfifo(struct thread *td, char *path, enum uio_seg pathseg, int mode)
1474 return (kern_mkfifoat(td, AT_FDCWD, path, pathseg, mode));
1478 kern_mkfifoat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1484 struct nameidata nd;
1487 AUDIT_ARG_MODE(mode);
1490 NDINIT_AT(&nd, CREATE, LOCKPARENT | SAVENAME | MPSAFE | AUDITVNODE1,
1491 pathseg, path, fd, td);
1492 if ((error = namei(&nd)) != 0)
1494 vfslocked = NDHASGIANT(&nd);
1495 if (nd.ni_vp != NULL) {
1496 NDFREE(&nd, NDF_ONLY_PNBUF);
1497 if (nd.ni_vp == nd.ni_dvp)
1502 VFS_UNLOCK_GIANT(vfslocked);
1505 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1506 NDFREE(&nd, NDF_ONLY_PNBUF);
1508 VFS_UNLOCK_GIANT(vfslocked);
1509 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1514 vattr.va_type = VFIFO;
1515 vattr.va_mode = (mode & ALLPERMS) & ~td->td_proc->p_fd->fd_cmask;
1517 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1522 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
1529 vn_finished_write(mp);
1530 VFS_UNLOCK_GIANT(vfslocked);
1531 NDFREE(&nd, NDF_ONLY_PNBUF);
1536 * Make a hard file link.
1538 #ifndef _SYS_SYSPROTO_H_
1547 register struct link_args /* {
1553 return (kern_link(td, uap->path, uap->link, UIO_USERSPACE));
1556 #ifndef _SYS_SYSPROTO_H_
1557 struct linkat_args {
1566 sys_linkat(struct thread *td, struct linkat_args *uap)
1571 if (flag & ~AT_SYMLINK_FOLLOW)
1574 return (kern_linkat(td, uap->fd1, uap->fd2, uap->path1, uap->path2,
1575 UIO_USERSPACE, (flag & AT_SYMLINK_FOLLOW) ? FOLLOW : NOFOLLOW));
1578 int hardlink_check_uid = 0;
1579 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
1580 &hardlink_check_uid, 0,
1581 "Unprivileged processes cannot create hard links to files owned by other "
1583 static int hardlink_check_gid = 0;
1584 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
1585 &hardlink_check_gid, 0,
1586 "Unprivileged processes cannot create hard links to files owned by other "
1590 can_hardlink(struct vnode *vp, struct ucred *cred)
1595 if (!hardlink_check_uid && !hardlink_check_gid)
1598 error = VOP_GETATTR(vp, &va, cred);
1602 if (hardlink_check_uid && cred->cr_uid != va.va_uid) {
1603 error = priv_check_cred(cred, PRIV_VFS_LINK, 0);
1608 if (hardlink_check_gid && !groupmember(va.va_gid, cred)) {
1609 error = priv_check_cred(cred, PRIV_VFS_LINK, 0);
1618 kern_link(struct thread *td, char *path, char *link, enum uio_seg segflg)
1621 return (kern_linkat(td, AT_FDCWD, AT_FDCWD, path,link, segflg, FOLLOW));
1625 kern_linkat(struct thread *td, int fd1, int fd2, char *path1, char *path2,
1626 enum uio_seg segflg, int follow)
1630 struct nameidata nd;
1636 NDINIT_AT(&nd, LOOKUP, follow | MPSAFE | AUDITVNODE1, segflg, path1,
1639 if ((error = namei(&nd)) != 0)
1641 vfslocked = NDHASGIANT(&nd);
1642 NDFREE(&nd, NDF_ONLY_PNBUF);
1644 if (vp->v_type == VDIR) {
1646 VFS_UNLOCK_GIANT(vfslocked);
1647 return (EPERM); /* POSIX */
1649 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0) {
1651 VFS_UNLOCK_GIANT(vfslocked);
1654 NDINIT_AT(&nd, CREATE, LOCKPARENT | SAVENAME | MPSAFE | AUDITVNODE2,
1655 segflg, path2, fd2, td);
1656 if ((error = namei(&nd)) == 0) {
1657 lvfslocked = NDHASGIANT(&nd);
1658 if (nd.ni_vp != NULL) {
1659 if (nd.ni_dvp == nd.ni_vp)
1665 } else if ((error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY))
1667 error = can_hardlink(vp, td->td_ucred);
1670 error = mac_vnode_check_link(td->td_ucred,
1671 nd.ni_dvp, vp, &nd.ni_cnd);
1674 error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
1678 NDFREE(&nd, NDF_ONLY_PNBUF);
1679 VFS_UNLOCK_GIANT(lvfslocked);
1682 vn_finished_write(mp);
1683 VFS_UNLOCK_GIANT(vfslocked);
1688 * Make a symbolic link.
1690 #ifndef _SYS_SYSPROTO_H_
1691 struct symlink_args {
1697 sys_symlink(td, uap)
1699 register struct symlink_args /* {
1705 return (kern_symlink(td, uap->path, uap->link, UIO_USERSPACE));
1708 #ifndef _SYS_SYSPROTO_H_
1709 struct symlinkat_args {
1716 sys_symlinkat(struct thread *td, struct symlinkat_args *uap)
1719 return (kern_symlinkat(td, uap->path1, uap->fd, uap->path2,
1724 kern_symlink(struct thread *td, char *path, char *link, enum uio_seg segflg)
1727 return (kern_symlinkat(td, path, AT_FDCWD, link, segflg));
1731 kern_symlinkat(struct thread *td, char *path1, int fd, char *path2,
1732 enum uio_seg segflg)
1738 struct nameidata nd;
1741 if (segflg == UIO_SYSSPACE) {
1744 syspath = uma_zalloc(namei_zone, M_WAITOK);
1745 if ((error = copyinstr(path1, syspath, MAXPATHLEN, NULL)) != 0)
1748 AUDIT_ARG_TEXT(syspath);
1751 NDINIT_AT(&nd, CREATE, LOCKPARENT | SAVENAME | MPSAFE | AUDITVNODE1,
1752 segflg, path2, fd, td);
1753 if ((error = namei(&nd)) != 0)
1755 vfslocked = NDHASGIANT(&nd);
1757 NDFREE(&nd, NDF_ONLY_PNBUF);
1758 if (nd.ni_vp == nd.ni_dvp)
1763 VFS_UNLOCK_GIANT(vfslocked);
1767 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1768 NDFREE(&nd, NDF_ONLY_PNBUF);
1770 VFS_UNLOCK_GIANT(vfslocked);
1771 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1776 vattr.va_mode = ACCESSPERMS &~ td->td_proc->p_fd->fd_cmask;
1778 vattr.va_type = VLNK;
1779 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1784 error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr, syspath);
1790 NDFREE(&nd, NDF_ONLY_PNBUF);
1792 vn_finished_write(mp);
1793 VFS_UNLOCK_GIANT(vfslocked);
1795 if (segflg != UIO_SYSSPACE)
1796 uma_zfree(namei_zone, syspath);
1801 * Delete a whiteout from the filesystem.
1804 sys_undelete(td, uap)
1806 register struct undelete_args /* {
1812 struct nameidata nd;
1817 NDINIT(&nd, DELETE, LOCKPARENT | DOWHITEOUT | MPSAFE | AUDITVNODE1,
1818 UIO_USERSPACE, uap->path, td);
1822 vfslocked = NDHASGIANT(&nd);
1824 if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & ISWHITEOUT)) {
1825 NDFREE(&nd, NDF_ONLY_PNBUF);
1826 if (nd.ni_vp == nd.ni_dvp)
1832 VFS_UNLOCK_GIANT(vfslocked);
1835 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1836 NDFREE(&nd, NDF_ONLY_PNBUF);
1838 VFS_UNLOCK_GIANT(vfslocked);
1839 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1843 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, DELETE);
1844 NDFREE(&nd, NDF_ONLY_PNBUF);
1846 vn_finished_write(mp);
1847 VFS_UNLOCK_GIANT(vfslocked);
1852 * Delete a name from the filesystem.
1854 #ifndef _SYS_SYSPROTO_H_
1855 struct unlink_args {
1862 struct unlink_args /* {
1867 return (kern_unlink(td, uap->path, UIO_USERSPACE));
1870 #ifndef _SYS_SYSPROTO_H_
1871 struct unlinkat_args {
1878 sys_unlinkat(struct thread *td, struct unlinkat_args *uap)
1880 int flag = uap->flag;
1882 char *path = uap->path;
1884 if (flag & ~AT_REMOVEDIR)
1887 if (flag & AT_REMOVEDIR)
1888 return (kern_rmdirat(td, fd, path, UIO_USERSPACE));
1890 return (kern_unlinkat(td, fd, path, UIO_USERSPACE, 0));
1894 kern_unlink(struct thread *td, char *path, enum uio_seg pathseg)
1897 return (kern_unlinkat(td, AT_FDCWD, path, pathseg, 0));
1901 kern_unlinkat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1907 struct nameidata nd;
1913 NDINIT_AT(&nd, DELETE, LOCKPARENT | LOCKLEAF | MPSAFE | AUDITVNODE1,
1914 pathseg, path, fd, td);
1915 if ((error = namei(&nd)) != 0)
1916 return (error == EINVAL ? EPERM : error);
1917 vfslocked = NDHASGIANT(&nd);
1919 if (vp->v_type == VDIR && oldinum == 0) {
1920 error = EPERM; /* POSIX */
1921 } else if (oldinum != 0 &&
1922 ((error = vn_stat(vp, &sb, td->td_ucred, NOCRED, td)) == 0) &&
1923 sb.st_ino != oldinum) {
1924 error = EIDRM; /* Identifier removed */
1927 * The root of a mounted filesystem cannot be deleted.
1929 * XXX: can this only be a VDIR case?
1931 if (vp->v_vflag & VV_ROOT)
1935 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1936 NDFREE(&nd, NDF_ONLY_PNBUF);
1938 if (vp == nd.ni_dvp)
1942 VFS_UNLOCK_GIANT(vfslocked);
1943 if ((error = vn_start_write(NULL, &mp,
1944 V_XSLEEP | PCATCH)) != 0)
1949 error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp,
1954 error = VOP_REMOVE(nd.ni_dvp, vp, &nd.ni_cnd);
1958 vn_finished_write(mp);
1960 NDFREE(&nd, NDF_ONLY_PNBUF);
1962 if (vp == nd.ni_dvp)
1966 VFS_UNLOCK_GIANT(vfslocked);
1971 * Reposition read/write file offset.
1973 #ifndef _SYS_SYSPROTO_H_
1984 register struct lseek_args /* {
1991 struct ucred *cred = td->td_ucred;
1999 AUDIT_ARG_FD(uap->fd);
2000 if ((error = fget(td, uap->fd, CAP_SEEK, &fp)) != 0)
2002 if (!(fp->f_ops->fo_flags & DFLAG_SEEKABLE)) {
2007 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
2008 noneg = (vp->v_type != VCHR);
2009 offset = uap->offset;
2010 switch (uap->whence) {
2013 (fp->f_offset < 0 ||
2014 (offset > 0 && fp->f_offset > OFF_MAX - offset))) {
2018 offset += fp->f_offset;
2021 vn_lock(vp, LK_SHARED | LK_RETRY);
2022 error = VOP_GETATTR(vp, &vattr, cred);
2028 * If the file references a disk device, then fetch
2029 * the media size and use that to determine the ending
2032 if (vattr.va_size == 0 && vp->v_type == VCHR &&
2033 fo_ioctl(fp, DIOCGMEDIASIZE, &size, cred, td) == 0)
2034 vattr.va_size = size;
2036 (vattr.va_size > OFF_MAX ||
2037 (offset > 0 && vattr.va_size > OFF_MAX - offset))) {
2041 offset += vattr.va_size;
2046 error = fo_ioctl(fp, FIOSEEKDATA, &offset, cred, td);
2049 error = fo_ioctl(fp, FIOSEEKHOLE, &offset, cred, td);
2054 if (error == 0 && noneg && offset < 0)
2058 fp->f_offset = offset;
2059 VFS_KNOTE_UNLOCKED(vp, 0);
2060 *(off_t *)(td->td_retval) = fp->f_offset;
2063 VFS_UNLOCK_GIANT(vfslocked);
2067 #if defined(COMPAT_43)
2069 * Reposition read/write file offset.
2071 #ifndef _SYS_SYSPROTO_H_
2072 struct olseek_args {
2081 register struct olseek_args /* {
2087 struct lseek_args /* {
2095 nuap.offset = uap->offset;
2096 nuap.whence = uap->whence;
2097 return (sys_lseek(td, &nuap));
2099 #endif /* COMPAT_43 */
2101 /* Version with the 'pad' argument */
2103 freebsd6_lseek(td, uap)
2105 register struct freebsd6_lseek_args *uap;
2107 struct lseek_args ouap;
2110 ouap.offset = uap->offset;
2111 ouap.whence = uap->whence;
2112 return (sys_lseek(td, &ouap));
2116 * Check access permissions using passed credentials.
2119 vn_access(vp, user_flags, cred, td)
2128 /* Flags == 0 means only check for existence. */
2132 if (user_flags & R_OK)
2134 if (user_flags & W_OK)
2136 if (user_flags & X_OK)
2139 error = mac_vnode_check_access(cred, vp, accmode);
2143 if ((accmode & VWRITE) == 0 || (error = vn_writechk(vp)) == 0)
2144 error = VOP_ACCESS(vp, accmode, cred, td);
2150 * Check access permissions using "real" credentials.
2152 #ifndef _SYS_SYSPROTO_H_
2153 struct access_args {
2161 register struct access_args /* {
2167 return (kern_access(td, uap->path, UIO_USERSPACE, uap->flags));
2170 #ifndef _SYS_SYSPROTO_H_
2171 struct faccessat_args {
2179 sys_faccessat(struct thread *td, struct faccessat_args *uap)
2182 if (uap->flag & ~AT_EACCESS)
2184 return (kern_accessat(td, uap->fd, uap->path, UIO_USERSPACE, uap->flag,
2189 kern_access(struct thread *td, char *path, enum uio_seg pathseg, int mode)
2192 return (kern_accessat(td, AT_FDCWD, path, pathseg, 0, mode));
2196 kern_accessat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2197 int flags, int mode)
2199 struct ucred *cred, *tmpcred;
2201 struct nameidata nd;
2206 * Create and modify a temporary credential instead of one that
2207 * is potentially shared.
2209 if (!(flags & AT_EACCESS)) {
2210 cred = td->td_ucred;
2211 tmpcred = crdup(cred);
2212 tmpcred->cr_uid = cred->cr_ruid;
2213 tmpcred->cr_groups[0] = cred->cr_rgid;
2214 td->td_ucred = tmpcred;
2216 cred = tmpcred = td->td_ucred;
2217 AUDIT_ARG_VALUE(mode);
2218 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | MPSAFE |
2219 AUDITVNODE1, pathseg, path, fd, CAP_FSTAT, td);
2220 if ((error = namei(&nd)) != 0)
2222 vfslocked = NDHASGIANT(&nd);
2225 error = vn_access(vp, mode, tmpcred, td);
2226 NDFREE(&nd, NDF_ONLY_PNBUF);
2228 VFS_UNLOCK_GIANT(vfslocked);
2230 if (!(flags & AT_EACCESS)) {
2231 td->td_ucred = cred;
2238 * Check access permissions using "effective" credentials.
2240 #ifndef _SYS_SYSPROTO_H_
2241 struct eaccess_args {
2247 sys_eaccess(td, uap)
2249 register struct eaccess_args /* {
2255 return (kern_eaccess(td, uap->path, UIO_USERSPACE, uap->flags));
2259 kern_eaccess(struct thread *td, char *path, enum uio_seg pathseg, int flags)
2262 return (kern_accessat(td, AT_FDCWD, path, pathseg, AT_EACCESS, flags));
2265 #if defined(COMPAT_43)
2267 * Get file status; this version follows links.
2269 #ifndef _SYS_SYSPROTO_H_
2278 register struct ostat_args /* {
2287 error = kern_stat(td, uap->path, UIO_USERSPACE, &sb);
2291 error = copyout(&osb, uap->ub, sizeof (osb));
2296 * Get file status; this version does not follow links.
2298 #ifndef _SYS_SYSPROTO_H_
2299 struct olstat_args {
2307 register struct olstat_args /* {
2316 error = kern_lstat(td, uap->path, UIO_USERSPACE, &sb);
2320 error = copyout(&osb, uap->ub, sizeof (osb));
2325 * Convert from an old to a new stat structure.
2333 ost->st_dev = st->st_dev;
2334 ost->st_ino = st->st_ino;
2335 ost->st_mode = st->st_mode;
2336 ost->st_nlink = st->st_nlink;
2337 ost->st_uid = st->st_uid;
2338 ost->st_gid = st->st_gid;
2339 ost->st_rdev = st->st_rdev;
2340 if (st->st_size < (quad_t)1 << 32)
2341 ost->st_size = st->st_size;
2344 ost->st_atim = st->st_atim;
2345 ost->st_mtim = st->st_mtim;
2346 ost->st_ctim = st->st_ctim;
2347 ost->st_blksize = st->st_blksize;
2348 ost->st_blocks = st->st_blocks;
2349 ost->st_flags = st->st_flags;
2350 ost->st_gen = st->st_gen;
2352 #endif /* COMPAT_43 */
2355 * Get file status; this version follows links.
2357 #ifndef _SYS_SYSPROTO_H_
2366 register struct stat_args /* {
2374 error = kern_stat(td, uap->path, UIO_USERSPACE, &sb);
2376 error = copyout(&sb, uap->ub, sizeof (sb));
2380 #ifndef _SYS_SYSPROTO_H_
2381 struct fstatat_args {
2389 sys_fstatat(struct thread *td, struct fstatat_args *uap)
2394 error = kern_statat(td, uap->flag, uap->fd, uap->path,
2395 UIO_USERSPACE, &sb);
2397 error = copyout(&sb, uap->buf, sizeof (sb));
2402 kern_stat(struct thread *td, char *path, enum uio_seg pathseg, struct stat *sbp)
2405 return (kern_statat(td, 0, AT_FDCWD, path, pathseg, sbp));
2409 kern_statat(struct thread *td, int flag, int fd, char *path,
2410 enum uio_seg pathseg, struct stat *sbp)
2413 return (kern_statat_vnhook(td, flag, fd, path, pathseg, sbp, NULL));
2417 kern_statat_vnhook(struct thread *td, int flag, int fd, char *path,
2418 enum uio_seg pathseg, struct stat *sbp,
2419 void (*hook)(struct vnode *vp, struct stat *sbp))
2421 struct nameidata nd;
2423 int error, vfslocked;
2425 if (flag & ~AT_SYMLINK_NOFOLLOW)
2428 NDINIT_ATRIGHTS(&nd, LOOKUP, ((flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW :
2429 FOLLOW) | LOCKSHARED | LOCKLEAF | AUDITVNODE1 | MPSAFE, pathseg,
2430 path, fd, CAP_FSTAT, td);
2432 if ((error = namei(&nd)) != 0)
2434 vfslocked = NDHASGIANT(&nd);
2435 error = vn_stat(nd.ni_vp, &sb, td->td_ucred, NOCRED, td);
2437 SDT_PROBE(vfs, , stat, mode, path, sb.st_mode, 0, 0, 0);
2438 if (S_ISREG(sb.st_mode))
2439 SDT_PROBE(vfs, , stat, reg, path, pathseg, 0, 0, 0);
2440 if (__predict_false(hook != NULL))
2441 hook(nd.ni_vp, &sb);
2443 NDFREE(&nd, NDF_ONLY_PNBUF);
2445 VFS_UNLOCK_GIANT(vfslocked);
2450 if (KTRPOINT(td, KTR_STRUCT))
2457 * Get file status; this version does not follow links.
2459 #ifndef _SYS_SYSPROTO_H_
2468 register struct lstat_args /* {
2476 error = kern_lstat(td, uap->path, UIO_USERSPACE, &sb);
2478 error = copyout(&sb, uap->ub, sizeof (sb));
2483 kern_lstat(struct thread *td, char *path, enum uio_seg pathseg, struct stat *sbp)
2486 return (kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, path, pathseg,
2491 * Implementation of the NetBSD [l]stat() functions.
2498 bzero(nsb, sizeof *nsb);
2499 nsb->st_dev = sb->st_dev;
2500 nsb->st_ino = sb->st_ino;
2501 nsb->st_mode = sb->st_mode;
2502 nsb->st_nlink = sb->st_nlink;
2503 nsb->st_uid = sb->st_uid;
2504 nsb->st_gid = sb->st_gid;
2505 nsb->st_rdev = sb->st_rdev;
2506 nsb->st_atim = sb->st_atim;
2507 nsb->st_mtim = sb->st_mtim;
2508 nsb->st_ctim = sb->st_ctim;
2509 nsb->st_size = sb->st_size;
2510 nsb->st_blocks = sb->st_blocks;
2511 nsb->st_blksize = sb->st_blksize;
2512 nsb->st_flags = sb->st_flags;
2513 nsb->st_gen = sb->st_gen;
2514 nsb->st_birthtim = sb->st_birthtim;
2517 #ifndef _SYS_SYSPROTO_H_
2526 register struct nstat_args /* {
2535 error = kern_stat(td, uap->path, UIO_USERSPACE, &sb);
2538 cvtnstat(&sb, &nsb);
2539 error = copyout(&nsb, uap->ub, sizeof (nsb));
2544 * NetBSD lstat. Get file status; this version does not follow links.
2546 #ifndef _SYS_SYSPROTO_H_
2555 register struct nlstat_args /* {
2564 error = kern_lstat(td, uap->path, UIO_USERSPACE, &sb);
2567 cvtnstat(&sb, &nsb);
2568 error = copyout(&nsb, uap->ub, sizeof (nsb));
2573 * Get configurable pathname variables.
2575 #ifndef _SYS_SYSPROTO_H_
2576 struct pathconf_args {
2582 sys_pathconf(td, uap)
2584 register struct pathconf_args /* {
2590 return (kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name, FOLLOW));
2593 #ifndef _SYS_SYSPROTO_H_
2594 struct lpathconf_args {
2600 sys_lpathconf(td, uap)
2602 register struct lpathconf_args /* {
2608 return (kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name, NOFOLLOW));
2612 kern_pathconf(struct thread *td, char *path, enum uio_seg pathseg, int name,
2615 struct nameidata nd;
2616 int error, vfslocked;
2618 NDINIT(&nd, LOOKUP, LOCKSHARED | LOCKLEAF | MPSAFE | AUDITVNODE1 |
2619 flags, pathseg, path, td);
2620 if ((error = namei(&nd)) != 0)
2622 vfslocked = NDHASGIANT(&nd);
2623 NDFREE(&nd, NDF_ONLY_PNBUF);
2625 /* If asynchronous I/O is available, it works for all files. */
2626 if (name == _PC_ASYNC_IO)
2627 td->td_retval[0] = async_io_version;
2629 error = VOP_PATHCONF(nd.ni_vp, name, td->td_retval);
2631 VFS_UNLOCK_GIANT(vfslocked);
2636 * Return target name of a symbolic link.
2638 #ifndef _SYS_SYSPROTO_H_
2639 struct readlink_args {
2646 sys_readlink(td, uap)
2648 register struct readlink_args /* {
2655 return (kern_readlink(td, uap->path, UIO_USERSPACE, uap->buf,
2656 UIO_USERSPACE, uap->count));
2658 #ifndef _SYS_SYSPROTO_H_
2659 struct readlinkat_args {
2667 sys_readlinkat(struct thread *td, struct readlinkat_args *uap)
2670 return (kern_readlinkat(td, uap->fd, uap->path, UIO_USERSPACE,
2671 uap->buf, UIO_USERSPACE, uap->bufsize));
2675 kern_readlink(struct thread *td, char *path, enum uio_seg pathseg, char *buf,
2676 enum uio_seg bufseg, size_t count)
2679 return (kern_readlinkat(td, AT_FDCWD, path, pathseg, buf, bufseg,
2684 kern_readlinkat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2685 char *buf, enum uio_seg bufseg, size_t count)
2691 struct nameidata nd;
2694 if (count > IOSIZE_MAX)
2697 NDINIT_AT(&nd, LOOKUP, NOFOLLOW | LOCKSHARED | LOCKLEAF | MPSAFE |
2698 AUDITVNODE1, pathseg, path, fd, td);
2700 if ((error = namei(&nd)) != 0)
2702 NDFREE(&nd, NDF_ONLY_PNBUF);
2703 vfslocked = NDHASGIANT(&nd);
2706 error = mac_vnode_check_readlink(td->td_ucred, vp);
2709 VFS_UNLOCK_GIANT(vfslocked);
2713 if (vp->v_type != VLNK)
2716 aiov.iov_base = buf;
2717 aiov.iov_len = count;
2718 auio.uio_iov = &aiov;
2719 auio.uio_iovcnt = 1;
2720 auio.uio_offset = 0;
2721 auio.uio_rw = UIO_READ;
2722 auio.uio_segflg = bufseg;
2724 auio.uio_resid = count;
2725 error = VOP_READLINK(vp, &auio, td->td_ucred);
2728 VFS_UNLOCK_GIANT(vfslocked);
2729 td->td_retval[0] = count - auio.uio_resid;
2734 * Common implementation code for chflags() and fchflags().
2737 setfflags(td, vp, flags)
2746 /* We can't support the value matching VNOVAL. */
2747 if (flags == VNOVAL)
2748 return (EOPNOTSUPP);
2751 * Prevent non-root users from setting flags on devices. When
2752 * a device is reused, users can retain ownership of the device
2753 * if they are allowed to set flags and programs assume that
2754 * chown can't fail when done as root.
2756 if (vp->v_type == VCHR || vp->v_type == VBLK) {
2757 error = priv_check(td, PRIV_VFS_CHFLAGS_DEV);
2762 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2764 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2766 vattr.va_flags = flags;
2768 error = mac_vnode_check_setflags(td->td_ucred, vp, vattr.va_flags);
2771 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
2773 vn_finished_write(mp);
2778 * Change flags of a file given a path name.
2780 #ifndef _SYS_SYSPROTO_H_
2781 struct chflags_args {
2787 sys_chflags(td, uap)
2789 register struct chflags_args /* {
2795 struct nameidata nd;
2798 AUDIT_ARG_FFLAGS(uap->flags);
2799 NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE | AUDITVNODE1, UIO_USERSPACE,
2801 if ((error = namei(&nd)) != 0)
2803 NDFREE(&nd, NDF_ONLY_PNBUF);
2804 vfslocked = NDHASGIANT(&nd);
2805 error = setfflags(td, nd.ni_vp, uap->flags);
2807 VFS_UNLOCK_GIANT(vfslocked);
2812 * Same as chflags() but doesn't follow symlinks.
2815 sys_lchflags(td, uap)
2817 register struct lchflags_args /* {
2823 struct nameidata nd;
2826 AUDIT_ARG_FFLAGS(uap->flags);
2827 NDINIT(&nd, LOOKUP, NOFOLLOW | MPSAFE | AUDITVNODE1, UIO_USERSPACE,
2829 if ((error = namei(&nd)) != 0)
2831 vfslocked = NDHASGIANT(&nd);
2832 NDFREE(&nd, NDF_ONLY_PNBUF);
2833 error = setfflags(td, nd.ni_vp, uap->flags);
2835 VFS_UNLOCK_GIANT(vfslocked);
2840 * Change flags of a file given a file descriptor.
2842 #ifndef _SYS_SYSPROTO_H_
2843 struct fchflags_args {
2849 sys_fchflags(td, uap)
2851 register struct fchflags_args /* {
2860 AUDIT_ARG_FD(uap->fd);
2861 AUDIT_ARG_FFLAGS(uap->flags);
2862 if ((error = getvnode(td->td_proc->p_fd, uap->fd, CAP_FCHFLAGS,
2865 vfslocked = VFS_LOCK_GIANT(fp->f_vnode->v_mount);
2867 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
2868 AUDIT_ARG_VNODE1(fp->f_vnode);
2869 VOP_UNLOCK(fp->f_vnode, 0);
2871 error = setfflags(td, fp->f_vnode, uap->flags);
2872 VFS_UNLOCK_GIANT(vfslocked);
2878 * Common implementation code for chmod(), lchmod() and fchmod().
2881 setfmode(td, cred, vp, mode)
2891 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2893 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2895 vattr.va_mode = mode & ALLPERMS;
2897 error = mac_vnode_check_setmode(cred, vp, vattr.va_mode);
2900 error = VOP_SETATTR(vp, &vattr, cred);
2902 vn_finished_write(mp);
2907 * Change mode of a file given path name.
2909 #ifndef _SYS_SYSPROTO_H_
2918 register struct chmod_args /* {
2924 return (kern_chmod(td, uap->path, UIO_USERSPACE, uap->mode));
2927 #ifndef _SYS_SYSPROTO_H_
2928 struct fchmodat_args {
2936 sys_fchmodat(struct thread *td, struct fchmodat_args *uap)
2938 int flag = uap->flag;
2940 char *path = uap->path;
2941 mode_t mode = uap->mode;
2943 if (flag & ~AT_SYMLINK_NOFOLLOW)
2946 return (kern_fchmodat(td, fd, path, UIO_USERSPACE, mode, flag));
2950 kern_chmod(struct thread *td, char *path, enum uio_seg pathseg, int mode)
2953 return (kern_fchmodat(td, AT_FDCWD, path, pathseg, mode, 0));
2957 * Change mode of a file given path name (don't follow links.)
2959 #ifndef _SYS_SYSPROTO_H_
2960 struct lchmod_args {
2968 register struct lchmod_args /* {
2974 return (kern_fchmodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2975 uap->mode, AT_SYMLINK_NOFOLLOW));
2980 kern_fchmodat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2981 mode_t mode, int flag)
2984 struct nameidata nd;
2988 AUDIT_ARG_MODE(mode);
2989 follow = (flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
2990 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | MPSAFE | AUDITVNODE1, pathseg,
2991 path, fd, CAP_FCHMOD, td);
2992 if ((error = namei(&nd)) != 0)
2994 vfslocked = NDHASGIANT(&nd);
2995 NDFREE(&nd, NDF_ONLY_PNBUF);
2996 error = setfmode(td, td->td_ucred, nd.ni_vp, mode);
2998 VFS_UNLOCK_GIANT(vfslocked);
3003 * Change mode of a file given a file descriptor.
3005 #ifndef _SYS_SYSPROTO_H_
3006 struct fchmod_args {
3012 sys_fchmod(struct thread *td, struct fchmod_args *uap)
3017 AUDIT_ARG_FD(uap->fd);
3018 AUDIT_ARG_MODE(uap->mode);
3020 error = fget(td, uap->fd, CAP_FCHMOD, &fp);
3023 error = fo_chmod(fp, uap->mode, td->td_ucred, td);
3029 * Common implementation for chown(), lchown(), and fchown()
3032 setfown(td, cred, vp, uid, gid)
3043 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
3045 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3050 error = mac_vnode_check_setowner(cred, vp, vattr.va_uid,
3054 error = VOP_SETATTR(vp, &vattr, cred);
3056 vn_finished_write(mp);
3061 * Set ownership given a path name.
3063 #ifndef _SYS_SYSPROTO_H_
3073 register struct chown_args /* {
3080 return (kern_chown(td, uap->path, UIO_USERSPACE, uap->uid, uap->gid));
3083 #ifndef _SYS_SYSPROTO_H_
3084 struct fchownat_args {
3093 sys_fchownat(struct thread *td, struct fchownat_args *uap)
3098 if (flag & ~AT_SYMLINK_NOFOLLOW)
3101 return (kern_fchownat(td, uap->fd, uap->path, UIO_USERSPACE, uap->uid,
3102 uap->gid, uap->flag));
3106 kern_chown(struct thread *td, char *path, enum uio_seg pathseg, int uid,
3110 return (kern_fchownat(td, AT_FDCWD, path, pathseg, uid, gid, 0));
3114 kern_fchownat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
3115 int uid, int gid, int flag)
3117 struct nameidata nd;
3118 int error, vfslocked, follow;
3120 AUDIT_ARG_OWNER(uid, gid);
3121 follow = (flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
3122 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | MPSAFE | AUDITVNODE1, pathseg,
3123 path, fd, CAP_FCHOWN, td);
3125 if ((error = namei(&nd)) != 0)
3127 vfslocked = NDHASGIANT(&nd);
3128 NDFREE(&nd, NDF_ONLY_PNBUF);
3129 error = setfown(td, td->td_ucred, nd.ni_vp, uid, gid);
3131 VFS_UNLOCK_GIANT(vfslocked);
3136 * Set ownership given a path name, do not cross symlinks.
3138 #ifndef _SYS_SYSPROTO_H_
3139 struct lchown_args {
3148 register struct lchown_args /* {
3155 return (kern_lchown(td, uap->path, UIO_USERSPACE, uap->uid, uap->gid));
3159 kern_lchown(struct thread *td, char *path, enum uio_seg pathseg, int uid,
3163 return (kern_fchownat(td, AT_FDCWD, path, pathseg, uid, gid,
3164 AT_SYMLINK_NOFOLLOW));
3168 * Set ownership given a file descriptor.
3170 #ifndef _SYS_SYSPROTO_H_
3171 struct fchown_args {
3180 register struct fchown_args /* {
3189 AUDIT_ARG_FD(uap->fd);
3190 AUDIT_ARG_OWNER(uap->uid, uap->gid);
3191 error = fget(td, uap->fd, CAP_FCHOWN, &fp);
3194 error = fo_chown(fp, uap->uid, uap->gid, td->td_ucred, td);
3200 * Common implementation code for utimes(), lutimes(), and futimes().
3203 getutimes(usrtvp, tvpseg, tsp)
3204 const struct timeval *usrtvp;
3205 enum uio_seg tvpseg;
3206 struct timespec *tsp;
3208 struct timeval tv[2];
3209 const struct timeval *tvp;
3212 if (usrtvp == NULL) {
3213 vfs_timestamp(&tsp[0]);
3216 if (tvpseg == UIO_SYSSPACE) {
3219 if ((error = copyin(usrtvp, tv, sizeof(tv))) != 0)
3224 if (tvp[0].tv_usec < 0 || tvp[0].tv_usec >= 1000000 ||
3225 tvp[1].tv_usec < 0 || tvp[1].tv_usec >= 1000000)
3227 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
3228 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
3234 * Common implementation code for utimes(), lutimes(), and futimes().
3237 setutimes(td, vp, ts, numtimes, nullflag)
3240 const struct timespec *ts;
3244 int error, setbirthtime;
3248 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
3250 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3252 if (numtimes < 3 && !VOP_GETATTR(vp, &vattr, td->td_ucred) &&
3253 timespeccmp(&ts[1], &vattr.va_birthtime, < ))
3256 vattr.va_atime = ts[0];
3257 vattr.va_mtime = ts[1];
3259 vattr.va_birthtime = ts[1];
3261 vattr.va_birthtime = ts[2];
3263 vattr.va_vaflags |= VA_UTIMES_NULL;
3265 error = mac_vnode_check_setutimes(td->td_ucred, vp, vattr.va_atime,
3269 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3271 vn_finished_write(mp);
3276 * Set the access and modification times of a file.
3278 #ifndef _SYS_SYSPROTO_H_
3279 struct utimes_args {
3281 struct timeval *tptr;
3287 register struct utimes_args /* {
3289 struct timeval *tptr;
3293 return (kern_utimes(td, uap->path, UIO_USERSPACE, uap->tptr,
3297 #ifndef _SYS_SYSPROTO_H_
3298 struct futimesat_args {
3301 const struct timeval * times;
3305 sys_futimesat(struct thread *td, struct futimesat_args *uap)
3308 return (kern_utimesat(td, uap->fd, uap->path, UIO_USERSPACE,
3309 uap->times, UIO_USERSPACE));
3313 kern_utimes(struct thread *td, char *path, enum uio_seg pathseg,
3314 struct timeval *tptr, enum uio_seg tptrseg)
3317 return (kern_utimesat(td, AT_FDCWD, path, pathseg, tptr, tptrseg));
3321 kern_utimesat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
3322 struct timeval *tptr, enum uio_seg tptrseg)
3324 struct nameidata nd;
3325 struct timespec ts[2];
3326 int error, vfslocked;
3328 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3330 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | MPSAFE | AUDITVNODE1, pathseg,
3331 path, fd, CAP_FUTIMES, td);
3333 if ((error = namei(&nd)) != 0)
3335 vfslocked = NDHASGIANT(&nd);
3336 NDFREE(&nd, NDF_ONLY_PNBUF);
3337 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3339 VFS_UNLOCK_GIANT(vfslocked);
3344 * Set the access and modification times of a file.
3346 #ifndef _SYS_SYSPROTO_H_
3347 struct lutimes_args {
3349 struct timeval *tptr;
3353 sys_lutimes(td, uap)
3355 register struct lutimes_args /* {
3357 struct timeval *tptr;
3361 return (kern_lutimes(td, uap->path, UIO_USERSPACE, uap->tptr,
3366 kern_lutimes(struct thread *td, char *path, enum uio_seg pathseg,
3367 struct timeval *tptr, enum uio_seg tptrseg)
3369 struct timespec ts[2];
3371 struct nameidata nd;
3374 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3376 NDINIT(&nd, LOOKUP, NOFOLLOW | MPSAFE | AUDITVNODE1, pathseg, path, td);
3377 if ((error = namei(&nd)) != 0)
3379 vfslocked = NDHASGIANT(&nd);
3380 NDFREE(&nd, NDF_ONLY_PNBUF);
3381 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3383 VFS_UNLOCK_GIANT(vfslocked);
3388 * Set the access and modification times of a file.
3390 #ifndef _SYS_SYSPROTO_H_
3391 struct futimes_args {
3393 struct timeval *tptr;
3397 sys_futimes(td, uap)
3399 register struct futimes_args /* {
3401 struct timeval *tptr;
3405 return (kern_futimes(td, uap->fd, uap->tptr, UIO_USERSPACE));
3409 kern_futimes(struct thread *td, int fd, struct timeval *tptr,
3410 enum uio_seg tptrseg)
3412 struct timespec ts[2];
3418 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3420 if ((error = getvnode(td->td_proc->p_fd, fd, CAP_FUTIMES, &fp))
3423 vfslocked = VFS_LOCK_GIANT(fp->f_vnode->v_mount);
3425 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
3426 AUDIT_ARG_VNODE1(fp->f_vnode);
3427 VOP_UNLOCK(fp->f_vnode, 0);
3429 error = setutimes(td, fp->f_vnode, ts, 2, tptr == NULL);
3430 VFS_UNLOCK_GIANT(vfslocked);
3436 * Truncate a file given its path name.
3438 #ifndef _SYS_SYSPROTO_H_
3439 struct truncate_args {
3446 sys_truncate(td, uap)
3448 register struct truncate_args /* {
3455 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3459 kern_truncate(struct thread *td, char *path, enum uio_seg pathseg, off_t length)
3465 struct nameidata nd;
3470 NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE | AUDITVNODE1, pathseg, path, td);
3471 if ((error = namei(&nd)) != 0)
3473 vfslocked = NDHASGIANT(&nd);
3475 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0) {
3477 VFS_UNLOCK_GIANT(vfslocked);
3480 NDFREE(&nd, NDF_ONLY_PNBUF);
3481 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3482 if (vp->v_type == VDIR)
3485 else if ((error = mac_vnode_check_write(td->td_ucred, NOCRED, vp))) {
3488 else if ((error = vn_writechk(vp)) == 0 &&
3489 (error = VOP_ACCESS(vp, VWRITE, td->td_ucred, td)) == 0) {
3491 vattr.va_size = length;
3492 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3495 vn_finished_write(mp);
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 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
3948 vn_finished_write(mp);
3950 NDFREE(&nd, NDF_ONLY_PNBUF);
3952 if (nd.ni_dvp == vp)
3956 VFS_UNLOCK_GIANT(vfslocked);
3962 * Read a block of directory entries in a filesystem independent format.
3964 #ifndef _SYS_SYSPROTO_H_
3965 struct ogetdirentries_args {
3973 ogetdirentries(struct thread *td, struct ogetdirentries_args *uap)
3978 error = kern_ogetdirentries(td, uap, &loff);
3980 error = copyout(&loff, uap->basep, sizeof(long));
3985 kern_ogetdirentries(struct thread *td, struct ogetdirentries_args *uap,
3990 struct uio auio, kuio;
3991 struct iovec aiov, kiov;
3992 struct dirent *dp, *edp;
3994 int error, eofflag, readcnt, vfslocked;
3997 /* XXX arbitrary sanity limit on `count'. */
3998 if (uap->count > 64 * 1024)
4000 if ((error = getvnode(td->td_proc->p_fd, uap->fd, CAP_READ,
4003 if ((fp->f_flag & FREAD) == 0) {
4009 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
4010 if (vp->v_type != VDIR) {
4011 VFS_UNLOCK_GIANT(vfslocked);
4015 aiov.iov_base = uap->buf;
4016 aiov.iov_len = uap->count;
4017 auio.uio_iov = &aiov;
4018 auio.uio_iovcnt = 1;
4019 auio.uio_rw = UIO_READ;
4020 auio.uio_segflg = UIO_USERSPACE;
4022 auio.uio_resid = uap->count;
4023 vn_lock(vp, LK_SHARED | LK_RETRY);
4024 loff = auio.uio_offset = fp->f_offset;
4026 error = mac_vnode_check_readdir(td->td_ucred, vp);
4029 VFS_UNLOCK_GIANT(vfslocked);
4034 # if (BYTE_ORDER != LITTLE_ENDIAN)
4035 if (vp->v_mount->mnt_maxsymlinklen <= 0) {
4036 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag,
4038 fp->f_offset = auio.uio_offset;
4043 kuio.uio_iov = &kiov;
4044 kuio.uio_segflg = UIO_SYSSPACE;
4045 kiov.iov_len = uap->count;
4046 dirbuf = malloc(uap->count, M_TEMP, M_WAITOK);
4047 kiov.iov_base = dirbuf;
4048 error = VOP_READDIR(vp, &kuio, fp->f_cred, &eofflag,
4050 fp->f_offset = kuio.uio_offset;
4052 readcnt = uap->count - kuio.uio_resid;
4053 edp = (struct dirent *)&dirbuf[readcnt];
4054 for (dp = (struct dirent *)dirbuf; dp < edp; ) {
4055 # if (BYTE_ORDER == LITTLE_ENDIAN)
4057 * The expected low byte of
4058 * dp->d_namlen is our dp->d_type.
4059 * The high MBZ byte of dp->d_namlen
4060 * is our dp->d_namlen.
4062 dp->d_type = dp->d_namlen;
4066 * The dp->d_type is the high byte
4067 * of the expected dp->d_namlen,
4068 * so must be zero'ed.
4072 if (dp->d_reclen > 0) {
4073 dp = (struct dirent *)
4074 ((char *)dp + dp->d_reclen);
4081 error = uiomove(dirbuf, readcnt, &auio);
4083 free(dirbuf, M_TEMP);
4087 VFS_UNLOCK_GIANT(vfslocked);
4091 if (uap->count == auio.uio_resid &&
4092 (vp->v_vflag & VV_ROOT) &&
4093 (vp->v_mount->mnt_flag & MNT_UNION)) {
4094 struct vnode *tvp = vp;
4095 vp = vp->v_mount->mnt_vnodecovered;
4101 VFS_UNLOCK_GIANT(vfslocked);
4105 VFS_UNLOCK_GIANT(vfslocked);
4107 td->td_retval[0] = uap->count - auio.uio_resid;
4112 #endif /* COMPAT_43 */
4115 * Read a block of directory entries in a filesystem independent format.
4117 #ifndef _SYS_SYSPROTO_H_
4118 struct getdirentries_args {
4126 sys_getdirentries(td, uap)
4128 register struct getdirentries_args /* {
4138 error = kern_getdirentries(td, uap->fd, uap->buf, uap->count, &base);
4141 if (uap->basep != NULL)
4142 error = copyout(&base, uap->basep, sizeof(long));
4147 kern_getdirentries(struct thread *td, int fd, char *buf, u_int count,
4159 auio.uio_resid = count;
4160 if (auio.uio_resid > IOSIZE_MAX)
4162 if ((error = getvnode(td->td_proc->p_fd, fd, CAP_READ | CAP_SEEK,
4165 if ((fp->f_flag & FREAD) == 0) {
4171 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
4172 if (vp->v_type != VDIR) {
4173 VFS_UNLOCK_GIANT(vfslocked);
4177 aiov.iov_base = buf;
4178 aiov.iov_len = count;
4179 auio.uio_iov = &aiov;
4180 auio.uio_iovcnt = 1;
4181 auio.uio_rw = UIO_READ;
4182 auio.uio_segflg = UIO_USERSPACE;
4184 vn_lock(vp, LK_SHARED | LK_RETRY);
4185 AUDIT_ARG_VNODE1(vp);
4186 loff = auio.uio_offset = fp->f_offset;
4188 error = mac_vnode_check_readdir(td->td_ucred, vp);
4191 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL,
4193 fp->f_offset = auio.uio_offset;
4196 VFS_UNLOCK_GIANT(vfslocked);
4199 if (count == auio.uio_resid &&
4200 (vp->v_vflag & VV_ROOT) &&
4201 (vp->v_mount->mnt_flag & MNT_UNION)) {
4202 struct vnode *tvp = vp;
4203 vp = vp->v_mount->mnt_vnodecovered;
4209 VFS_UNLOCK_GIANT(vfslocked);
4213 VFS_UNLOCK_GIANT(vfslocked);
4215 td->td_retval[0] = count - auio.uio_resid;
4221 #ifndef _SYS_SYSPROTO_H_
4222 struct getdents_args {
4229 sys_getdents(td, uap)
4231 register struct getdents_args /* {
4237 struct getdirentries_args ap;
4240 ap.count = uap->count;
4242 return (sys_getdirentries(td, &ap));
4246 * Set the mode mask for creation of filesystem nodes.
4248 #ifndef _SYS_SYSPROTO_H_
4256 struct umask_args /* {
4260 register struct filedesc *fdp;
4262 FILEDESC_XLOCK(td->td_proc->p_fd);
4263 fdp = td->td_proc->p_fd;
4264 td->td_retval[0] = fdp->fd_cmask;
4265 fdp->fd_cmask = uap->newmask & ALLPERMS;
4266 FILEDESC_XUNLOCK(td->td_proc->p_fd);
4271 * Void all references to file by ripping underlying filesystem away from
4274 #ifndef _SYS_SYSPROTO_H_
4275 struct revoke_args {
4282 register struct revoke_args /* {
4289 struct nameidata nd;
4292 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | MPSAFE | AUDITVNODE1,
4293 UIO_USERSPACE, uap->path, td);
4294 if ((error = namei(&nd)) != 0)
4296 vfslocked = NDHASGIANT(&nd);
4298 NDFREE(&nd, NDF_ONLY_PNBUF);
4299 if (vp->v_type != VCHR || vp->v_rdev == NULL) {
4304 error = mac_vnode_check_revoke(td->td_ucred, vp);
4308 error = VOP_GETATTR(vp, &vattr, td->td_ucred);
4311 if (td->td_ucred->cr_uid != vattr.va_uid) {
4312 error = priv_check(td, PRIV_VFS_ADMIN);
4317 VOP_REVOKE(vp, REVOKEALL);
4320 VFS_UNLOCK_GIANT(vfslocked);
4325 * Convert a user file descriptor to a kernel file entry and check that, if it
4326 * is a capability, the correct rights are present. A reference on the file
4327 * entry is held upon returning.
4330 getvnode(struct filedesc *fdp, int fd, cap_rights_t rights,
4335 struct file *fp_fromcap;
4341 if ((fdp == NULL) || (fp = fget_unlocked(fdp, fd)) == NULL)
4345 * If the file descriptor is for a capability, test rights and use the
4346 * file descriptor referenced by the capability.
4348 error = cap_funwrap(fp, rights, &fp_fromcap);
4350 fdrop(fp, curthread);
4353 if (fp != fp_fromcap) {
4355 fdrop(fp, curthread);
4358 #endif /* CAPABILITIES */
4361 * The file could be not of the vnode type, or it may be not
4362 * yet fully initialized, in which case the f_vnode pointer
4363 * may be set, but f_ops is still badfileops. E.g.,
4364 * devfs_open() transiently create such situation to
4365 * facilitate csw d_fdopen().
4367 * Dupfdopen() handling in kern_openat() installs the
4368 * half-baked file into the process descriptor table, allowing
4369 * other thread to dereference it. Guard against the race by
4372 if (fp->f_vnode == NULL || fp->f_ops == &badfileops) {
4373 fdrop(fp, curthread);
4382 * Get an (NFS) file handle.
4384 #ifndef _SYS_SYSPROTO_H_
4385 struct lgetfh_args {
4393 register struct lgetfh_args *uap;
4395 struct nameidata nd;
4397 register struct vnode *vp;
4401 error = priv_check(td, PRIV_VFS_GETFH);
4404 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | MPSAFE | AUDITVNODE1,
4405 UIO_USERSPACE, uap->fname, td);
4409 vfslocked = NDHASGIANT(&nd);
4410 NDFREE(&nd, NDF_ONLY_PNBUF);
4412 bzero(&fh, sizeof(fh));
4413 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
4414 error = VOP_VPTOFH(vp, &fh.fh_fid);
4416 VFS_UNLOCK_GIANT(vfslocked);
4419 error = copyout(&fh, uap->fhp, sizeof (fh));
4423 #ifndef _SYS_SYSPROTO_H_
4432 register struct getfh_args *uap;
4434 struct nameidata nd;
4436 register struct vnode *vp;
4440 error = priv_check(td, PRIV_VFS_GETFH);
4443 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | MPSAFE | AUDITVNODE1,
4444 UIO_USERSPACE, uap->fname, td);
4448 vfslocked = NDHASGIANT(&nd);
4449 NDFREE(&nd, NDF_ONLY_PNBUF);
4451 bzero(&fh, sizeof(fh));
4452 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
4453 error = VOP_VPTOFH(vp, &fh.fh_fid);
4455 VFS_UNLOCK_GIANT(vfslocked);
4458 error = copyout(&fh, uap->fhp, sizeof (fh));
4463 * syscall for the rpc.lockd to use to translate a NFS file handle into an
4466 * warning: do not remove the priv_check() call or this becomes one giant
4469 #ifndef _SYS_SYSPROTO_H_
4470 struct fhopen_args {
4471 const struct fhandle *u_fhp;
4478 struct fhopen_args /* {
4479 const struct fhandle *u_fhp;
4483 struct proc *p = td->td_proc;
4488 struct vattr *vap = &vat;
4491 register struct filedesc *fdp = p->p_fd;
4492 int fmode, error, type;
4498 error = priv_check(td, PRIV_VFS_FHOPEN);
4501 fmode = FFLAGS(uap->flags);
4502 /* why not allow a non-read/write open for our lockd? */
4503 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
4505 error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
4508 /* find the mount point */
4509 mp = vfs_busyfs(&fhp.fh_fsid);
4512 vfslocked = VFS_LOCK_GIANT(mp);
4513 /* now give me my vnode, it gets returned to me locked */
4514 error = VFS_FHTOVP(mp, &fhp.fh_fid, LK_EXCLUSIVE, &vp);
4519 * from now on we have to make sure not
4520 * to forget about the vnode
4521 * any error that causes an abort must vput(vp)
4522 * just set error = err and 'goto bad;'.
4528 if (vp->v_type == VLNK) {
4532 if (vp->v_type == VSOCK) {
4536 if (vp->v_type != VDIR && fmode & O_DIRECTORY) {
4541 if (fmode & (FWRITE | O_TRUNC)) {
4542 if (vp->v_type == VDIR) {
4546 error = vn_writechk(vp);
4553 if ((fmode & O_APPEND) && (fmode & FWRITE))
4556 error = mac_vnode_check_open(td->td_ucred, vp, accmode);
4561 error = VOP_ACCESS(vp, accmode, td->td_ucred, td);
4565 if (fmode & O_TRUNC) {
4567 VOP_UNLOCK(vp, 0); /* XXX */
4568 if ((error = vn_start_write(NULL, &mp, V_WAIT | PCATCH)) != 0) {
4573 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); /* XXX */
4577 * We don't yet have fp->f_cred, so use td->td_ucred, which
4580 error = mac_vnode_check_write(td->td_ucred, td->td_ucred, vp);
4585 error = VOP_SETATTR(vp, vap, td->td_ucred);
4589 vn_finished_write(mp);
4593 error = VOP_OPEN(vp, fmode, td->td_ucred, td, NULL);
4601 * end of vn_open code
4604 if ((error = falloc(td, &nfp, &indx, fmode)) != 0) {
4609 /* An extra reference on `nfp' has been held for us by falloc(). */
4612 finit(nfp, fmode & FMASK, DTYPE_VNODE, vp, &vnops);
4613 if (fmode & (O_EXLOCK | O_SHLOCK)) {
4614 lf.l_whence = SEEK_SET;
4617 if (fmode & O_EXLOCK)
4618 lf.l_type = F_WRLCK;
4620 lf.l_type = F_RDLCK;
4622 if ((fmode & FNONBLOCK) == 0)
4625 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf,
4628 * The lock request failed. Normally close the
4629 * descriptor but handle the case where someone might
4630 * have dup()d or close()d it when we weren't looking.
4632 fdclose(fdp, fp, indx, td);
4635 * release our private reference
4640 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
4641 atomic_set_int(&fp->f_flag, FHASLOCK);
4646 VFS_UNLOCK_GIANT(vfslocked);
4647 td->td_retval[0] = indx;
4653 VFS_UNLOCK_GIANT(vfslocked);
4658 * Stat an (NFS) file handle.
4660 #ifndef _SYS_SYSPROTO_H_
4661 struct fhstat_args {
4662 struct fhandle *u_fhp;
4669 register struct fhstat_args /* {
4670 struct fhandle *u_fhp;
4681 error = priv_check(td, PRIV_VFS_FHSTAT);
4684 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4687 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4689 vfslocked = VFS_LOCK_GIANT(mp);
4690 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_EXCLUSIVE, &vp);
4693 VFS_UNLOCK_GIANT(vfslocked);
4696 error = vn_stat(vp, &sb, td->td_ucred, NOCRED, td);
4698 VFS_UNLOCK_GIANT(vfslocked);
4701 error = copyout(&sb, uap->sb, sizeof(sb));
4706 * Implement fstatfs() for (NFS) file handles.
4708 #ifndef _SYS_SYSPROTO_H_
4709 struct fhstatfs_args {
4710 struct fhandle *u_fhp;
4715 sys_fhstatfs(td, uap)
4717 struct fhstatfs_args /* {
4718 struct fhandle *u_fhp;
4726 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4729 error = kern_fhstatfs(td, fh, &sf);
4732 return (copyout(&sf, uap->buf, sizeof(sf)));
4736 kern_fhstatfs(struct thread *td, fhandle_t fh, struct statfs *buf)
4744 error = priv_check(td, PRIV_VFS_FHSTATFS);
4747 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4749 vfslocked = VFS_LOCK_GIANT(mp);
4750 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_EXCLUSIVE, &vp);
4753 VFS_UNLOCK_GIANT(vfslocked);
4757 error = prison_canseemount(td->td_ucred, mp);
4761 error = mac_mount_check_stat(td->td_ucred, mp);
4766 * Set these in case the underlying filesystem fails to do so.
4769 sp->f_version = STATFS_VERSION;
4770 sp->f_namemax = NAME_MAX;
4771 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
4772 error = VFS_STATFS(mp, sp);
4777 VFS_UNLOCK_GIANT(vfslocked);
4782 kern_posix_fallocate(struct thread *td, int fd, off_t offset, off_t len)
4787 off_t olen, ooffset;
4788 int error, vfslocked;
4792 error = fget(td, fd, CAP_WRITE, &fp);
4796 switch (fp->f_type) {
4807 if ((fp->f_flag & FWRITE) == 0) {
4812 if (vp->v_type != VREG) {
4816 if (offset < 0 || len <= 0) {
4820 /* Check for wrap. */
4821 if (offset > OFF_MAX - len) {
4826 /* Allocating blocks may take a long time, so iterate. */
4832 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
4834 error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
4836 VFS_UNLOCK_GIANT(vfslocked);
4839 error = vn_lock(vp, LK_EXCLUSIVE);
4841 vn_finished_write(mp);
4842 VFS_UNLOCK_GIANT(vfslocked);
4846 error = mac_vnode_check_write(td->td_ucred, fp->f_cred, vp);
4849 error = VOP_ALLOCATE(vp, &offset, &len);
4851 vn_finished_write(mp);
4852 VFS_UNLOCK_GIANT(vfslocked);
4854 if (olen + ooffset != offset + len) {
4855 panic("offset + len changed from %jx/%jx to %jx/%jx",
4856 ooffset, olen, offset, len);
4858 if (error != 0 || len == 0)
4860 KASSERT(olen > len, ("Iteration did not make progress?"));
4870 sys_posix_fallocate(struct thread *td, struct posix_fallocate_args *uap)
4873 return (kern_posix_fallocate(td, uap->fd, uap->offset, uap->len));
4877 * Unlike madvise(2), we do not make a best effort to remember every
4878 * possible caching hint. Instead, we remember the last setting with
4879 * the exception that we will allow POSIX_FADV_NORMAL to adjust the
4880 * region of any current setting.
4883 kern_posix_fadvise(struct thread *td, int fd, off_t offset, off_t len,
4886 struct fadvise_info *fa, *new;
4892 if (offset < 0 || len < 0 || offset > OFF_MAX - len)
4895 case POSIX_FADV_SEQUENTIAL:
4896 case POSIX_FADV_RANDOM:
4897 case POSIX_FADV_NOREUSE:
4898 new = malloc(sizeof(*fa), M_FADVISE, M_WAITOK);
4900 case POSIX_FADV_NORMAL:
4901 case POSIX_FADV_WILLNEED:
4902 case POSIX_FADV_DONTNEED:
4908 /* XXX: CAP_POSIX_FADVISE? */
4909 error = fget(td, fd, 0, &fp);
4913 switch (fp->f_type) {
4925 if (vp->v_type != VREG) {
4932 end = offset + len - 1;
4934 case POSIX_FADV_SEQUENTIAL:
4935 case POSIX_FADV_RANDOM:
4936 case POSIX_FADV_NOREUSE:
4938 * Try to merge any existing non-standard region with
4939 * this new region if possible, otherwise create a new
4940 * non-standard region for this request.
4942 mtx_pool_lock(mtxpool_sleep, fp);
4944 if (fa != NULL && fa->fa_advice == advice &&
4945 ((fa->fa_start <= end && fa->fa_end >= offset) ||
4946 (end != OFF_MAX && fa->fa_start == end + 1) ||
4947 (fa->fa_end != OFF_MAX && fa->fa_end + 1 == offset))) {
4948 if (offset < fa->fa_start)
4949 fa->fa_start = offset;
4950 if (end > fa->fa_end)
4953 new->fa_advice = advice;
4954 new->fa_start = offset;
4959 mtx_pool_unlock(mtxpool_sleep, fp);
4961 case POSIX_FADV_NORMAL:
4963 * If a the "normal" region overlaps with an existing
4964 * non-standard region, trim or remove the
4965 * non-standard region.
4967 mtx_pool_lock(mtxpool_sleep, fp);
4970 if (offset <= fa->fa_start && end >= fa->fa_end) {
4972 fp->f_advice = NULL;
4973 } else if (offset <= fa->fa_start &&
4974 end >= fa->fa_start)
4975 fa->fa_start = end + 1;
4976 else if (offset <= fa->fa_end && end >= fa->fa_end)
4977 fa->fa_end = offset - 1;
4978 else if (offset >= fa->fa_start && end <= fa->fa_end) {
4980 * If the "normal" region is a middle
4981 * portion of the existing
4982 * non-standard region, just remove
4983 * the whole thing rather than picking
4984 * one side or the other to
4988 fp->f_advice = NULL;
4991 mtx_pool_unlock(mtxpool_sleep, fp);
4993 case POSIX_FADV_WILLNEED:
4994 case POSIX_FADV_DONTNEED:
4995 error = VOP_ADVISE(vp, offset, end, advice);
5001 free(new, M_FADVISE);
5006 sys_posix_fadvise(struct thread *td, struct posix_fadvise_args *uap)
5009 return (kern_posix_fadvise(td, uap->fd, uap->offset, uap->len,