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_compat.h"
41 #include "opt_kdtrace.h"
42 #include "opt_ktrace.h"
44 #include <sys/param.h>
45 #include <sys/systm.h>
49 #include <sys/sysent.h>
50 #include <sys/malloc.h>
51 #include <sys/mount.h>
52 #include <sys/mutex.h>
53 #include <sys/sysproto.h>
54 #include <sys/namei.h>
55 #include <sys/filedesc.h>
56 #include <sys/kernel.h>
57 #include <sys/fcntl.h>
59 #include <sys/filio.h>
60 #include <sys/limits.h>
61 #include <sys/linker.h>
65 #include <sys/unistd.h>
66 #include <sys/vnode.h>
69 #include <sys/dirent.h>
71 #include <sys/syscallsubr.h>
72 #include <sys/sysctl.h>
74 #include <sys/ktrace.h>
77 #include <machine/stdarg.h>
79 #include <security/audit/audit.h>
80 #include <security/mac/mac_framework.h>
83 #include <vm/vm_object.h>
84 #include <vm/vm_page.h>
87 MALLOC_DEFINE(M_FADVISE, "fadvise", "posix_fadvise(2) information");
89 SDT_PROVIDER_DEFINE(vfs);
90 SDT_PROBE_DEFINE(vfs, , stat, mode, mode);
91 SDT_PROBE_ARGTYPE(vfs, , stat, mode, 0, "char *");
92 SDT_PROBE_ARGTYPE(vfs, , stat, mode, 1, "int");
93 SDT_PROBE_DEFINE(vfs, , stat, reg, reg);
94 SDT_PROBE_ARGTYPE(vfs, , stat, reg, 0, "char *");
95 SDT_PROBE_ARGTYPE(vfs, , stat, reg, 1, "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 setfown(struct thread *td, struct vnode *, uid_t, gid_t);
100 static int setfmode(struct thread *td, struct vnode *, int);
101 static int setfflags(struct thread *td, struct vnode *, int);
102 static int setutimes(struct thread *td, struct vnode *,
103 const struct timespec *, int, int);
104 static int vn_access(struct vnode *vp, int user_flags, struct ucred *cred,
108 * The module initialization routine for POSIX asynchronous I/O will
109 * set this to the version of AIO that it implements. (Zero means
110 * that it is not implemented.) This value is used here by pathconf()
111 * and in kern_descrip.c by fpathconf().
113 int async_io_version;
116 static int syncprt = 0;
117 SYSCTL_INT(_debug, OID_AUTO, syncprt, CTLFLAG_RW, &syncprt, 0, "");
121 * Sync each mounted filesystem.
123 #ifndef _SYS_SYSPROTO_H_
132 struct sync_args *uap;
134 struct mount *mp, *nmp;
137 mtx_lock(&mountlist_mtx);
138 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
139 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK)) {
140 nmp = TAILQ_NEXT(mp, mnt_list);
143 vfslocked = VFS_LOCK_GIANT(mp);
144 if ((mp->mnt_flag & MNT_RDONLY) == 0 &&
145 vn_start_write(NULL, &mp, V_NOWAIT) == 0) {
146 save = curthread_pflags_set(TDP_SYNCIO);
147 vfs_msync(mp, MNT_NOWAIT);
148 VFS_SYNC(mp, MNT_NOWAIT);
149 curthread_pflags_restore(save);
150 vn_finished_write(mp);
152 VFS_UNLOCK_GIANT(vfslocked);
153 mtx_lock(&mountlist_mtx);
154 nmp = TAILQ_NEXT(mp, mnt_list);
157 mtx_unlock(&mountlist_mtx);
162 * Change filesystem quotas.
164 #ifndef _SYS_SYSPROTO_H_
165 struct quotactl_args {
175 register struct quotactl_args /* {
187 AUDIT_ARG_CMD(uap->cmd);
188 AUDIT_ARG_UID(uap->uid);
189 if (!prison_allow(td->td_ucred, PR_ALLOW_QUOTAS))
191 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | MPSAFE | AUDITVNODE1,
192 UIO_USERSPACE, uap->path, td);
193 if ((error = namei(&nd)) != 0)
195 vfslocked = NDHASGIANT(&nd);
196 NDFREE(&nd, NDF_ONLY_PNBUF);
197 mp = nd.ni_vp->v_mount;
200 error = vfs_busy(mp, 0);
203 VFS_UNLOCK_GIANT(vfslocked);
206 error = VFS_QUOTACTL(mp, uap->cmd, uap->uid, uap->arg);
208 VFS_UNLOCK_GIANT(vfslocked);
213 * Used by statfs conversion routines to scale the block size up if
214 * necessary so that all of the block counts are <= 'max_size'. Note
215 * that 'max_size' should be a bitmask, i.e. 2^n - 1 for some non-zero
219 statfs_scale_blocks(struct statfs *sf, long max_size)
224 KASSERT(powerof2(max_size + 1), ("%s: invalid max_size", __func__));
227 * Attempt to scale the block counts to give a more accurate
228 * overview to userland of the ratio of free space to used
229 * space. To do this, find the largest block count and compute
230 * a divisor that lets it fit into a signed integer <= max_size.
232 if (sf->f_bavail < 0)
233 count = -sf->f_bavail;
235 count = sf->f_bavail;
236 count = MAX(sf->f_blocks, MAX(sf->f_bfree, count));
237 if (count <= max_size)
240 count >>= flsl(max_size);
247 sf->f_bsize <<= shift;
248 sf->f_blocks >>= shift;
249 sf->f_bfree >>= shift;
250 sf->f_bavail >>= shift;
254 * Get filesystem statistics.
256 #ifndef _SYS_SYSPROTO_H_
265 register struct statfs_args /* {
273 error = kern_statfs(td, uap->path, UIO_USERSPACE, &sf);
275 error = copyout(&sf, uap->buf, sizeof(sf));
280 kern_statfs(struct thread *td, char *path, enum uio_seg pathseg,
284 struct statfs *sp, sb;
289 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | MPSAFE |
290 AUDITVNODE1, pathseg, path, td);
294 vfslocked = NDHASGIANT(&nd);
295 mp = nd.ni_vp->v_mount;
297 NDFREE(&nd, NDF_ONLY_PNBUF);
299 error = vfs_busy(mp, 0);
302 VFS_UNLOCK_GIANT(vfslocked);
306 error = mac_mount_check_stat(td->td_ucred, mp);
311 * Set these in case the underlying filesystem fails to do so.
314 sp->f_version = STATFS_VERSION;
315 sp->f_namemax = NAME_MAX;
316 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
317 error = VFS_STATFS(mp, sp);
320 if (priv_check(td, PRIV_VFS_GENERATION)) {
321 bcopy(sp, &sb, sizeof(sb));
322 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
323 prison_enforce_statfs(td->td_ucred, mp, &sb);
329 VFS_UNLOCK_GIANT(vfslocked);
334 * Get filesystem statistics.
336 #ifndef _SYS_SYSPROTO_H_
337 struct fstatfs_args {
345 register struct fstatfs_args /* {
353 error = kern_fstatfs(td, uap->fd, &sf);
355 error = copyout(&sf, uap->buf, sizeof(sf));
360 kern_fstatfs(struct thread *td, int fd, struct statfs *buf)
364 struct statfs *sp, sb;
370 error = getvnode(td->td_proc->p_fd, fd, &fp);
374 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
375 vn_lock(vp, LK_SHARED | LK_RETRY);
377 AUDIT_ARG_VNODE1(vp);
388 error = vfs_busy(mp, 0);
391 VFS_UNLOCK_GIANT(vfslocked);
395 error = mac_mount_check_stat(td->td_ucred, mp);
400 * Set these in case the underlying filesystem fails to do so.
403 sp->f_version = STATFS_VERSION;
404 sp->f_namemax = NAME_MAX;
405 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
406 error = VFS_STATFS(mp, sp);
409 if (priv_check(td, PRIV_VFS_GENERATION)) {
410 bcopy(sp, &sb, sizeof(sb));
411 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
412 prison_enforce_statfs(td->td_ucred, mp, &sb);
419 VFS_UNLOCK_GIANT(vfslocked);
424 * Get statistics on all filesystems.
426 #ifndef _SYS_SYSPROTO_H_
427 struct getfsstat_args {
436 register struct getfsstat_args /* {
443 return (kern_getfsstat(td, &uap->buf, uap->bufsize, UIO_USERSPACE,
448 * If (bufsize > 0 && bufseg == UIO_SYSSPACE)
449 * The caller is responsible for freeing memory which will be allocated
453 kern_getfsstat(struct thread *td, struct statfs **buf, size_t bufsize,
454 enum uio_seg bufseg, int flags)
456 struct mount *mp, *nmp;
457 struct statfs *sfsp, *sp, sb;
458 size_t count, maxcount;
462 maxcount = bufsize / sizeof(struct statfs);
465 else if (bufseg == UIO_USERSPACE)
467 else /* if (bufseg == UIO_SYSSPACE) */ {
469 mtx_lock(&mountlist_mtx);
470 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
473 mtx_unlock(&mountlist_mtx);
474 if (maxcount > count)
476 sfsp = *buf = malloc(maxcount * sizeof(struct statfs), M_TEMP,
480 mtx_lock(&mountlist_mtx);
481 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
482 if (prison_canseemount(td->td_ucred, mp) != 0) {
483 nmp = TAILQ_NEXT(mp, mnt_list);
487 if (mac_mount_check_stat(td->td_ucred, mp) != 0) {
488 nmp = TAILQ_NEXT(mp, mnt_list);
492 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK)) {
493 nmp = TAILQ_NEXT(mp, mnt_list);
496 vfslocked = VFS_LOCK_GIANT(mp);
497 if (sfsp && count < maxcount) {
500 * Set these in case the underlying filesystem
503 sp->f_version = STATFS_VERSION;
504 sp->f_namemax = NAME_MAX;
505 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
507 * If MNT_NOWAIT or MNT_LAZY is specified, do not
508 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
509 * overrides MNT_WAIT.
511 if (((flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
512 (flags & MNT_WAIT)) &&
513 (error = VFS_STATFS(mp, sp))) {
514 VFS_UNLOCK_GIANT(vfslocked);
515 mtx_lock(&mountlist_mtx);
516 nmp = TAILQ_NEXT(mp, mnt_list);
520 if (priv_check(td, PRIV_VFS_GENERATION)) {
521 bcopy(sp, &sb, sizeof(sb));
522 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
523 prison_enforce_statfs(td->td_ucred, mp, &sb);
526 if (bufseg == UIO_SYSSPACE)
527 bcopy(sp, sfsp, sizeof(*sp));
528 else /* if (bufseg == UIO_USERSPACE) */ {
529 error = copyout(sp, sfsp, sizeof(*sp));
532 VFS_UNLOCK_GIANT(vfslocked);
538 VFS_UNLOCK_GIANT(vfslocked);
540 mtx_lock(&mountlist_mtx);
541 nmp = TAILQ_NEXT(mp, mnt_list);
544 mtx_unlock(&mountlist_mtx);
545 if (sfsp && count > maxcount)
546 td->td_retval[0] = maxcount;
548 td->td_retval[0] = count;
552 #ifdef COMPAT_FREEBSD4
554 * Get old format filesystem statistics.
556 static void cvtstatfs(struct statfs *, struct ostatfs *);
558 #ifndef _SYS_SYSPROTO_H_
559 struct freebsd4_statfs_args {
565 freebsd4_statfs(td, uap)
567 struct freebsd4_statfs_args /* {
576 error = kern_statfs(td, uap->path, UIO_USERSPACE, &sf);
579 cvtstatfs(&sf, &osb);
580 return (copyout(&osb, uap->buf, sizeof(osb)));
584 * Get filesystem statistics.
586 #ifndef _SYS_SYSPROTO_H_
587 struct freebsd4_fstatfs_args {
593 freebsd4_fstatfs(td, uap)
595 struct freebsd4_fstatfs_args /* {
604 error = kern_fstatfs(td, uap->fd, &sf);
607 cvtstatfs(&sf, &osb);
608 return (copyout(&osb, uap->buf, sizeof(osb)));
612 * Get statistics on all filesystems.
614 #ifndef _SYS_SYSPROTO_H_
615 struct freebsd4_getfsstat_args {
622 freebsd4_getfsstat(td, uap)
624 register struct freebsd4_getfsstat_args /* {
630 struct statfs *buf, *sp;
635 count = uap->bufsize / sizeof(struct ostatfs);
636 size = count * sizeof(struct statfs);
637 error = kern_getfsstat(td, &buf, size, UIO_SYSSPACE, uap->flags);
639 count = td->td_retval[0];
641 while (count > 0 && error == 0) {
643 error = copyout(&osb, uap->buf, sizeof(osb));
654 * Implement fstatfs() for (NFS) file handles.
656 #ifndef _SYS_SYSPROTO_H_
657 struct freebsd4_fhstatfs_args {
658 struct fhandle *u_fhp;
663 freebsd4_fhstatfs(td, uap)
665 struct freebsd4_fhstatfs_args /* {
666 struct fhandle *u_fhp;
675 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
678 error = kern_fhstatfs(td, fh, &sf);
681 cvtstatfs(&sf, &osb);
682 return (copyout(&osb, uap->buf, sizeof(osb)));
686 * Convert a new format statfs structure to an old format statfs structure.
694 statfs_scale_blocks(nsp, LONG_MAX);
695 bzero(osp, sizeof(*osp));
696 osp->f_bsize = nsp->f_bsize;
697 osp->f_iosize = MIN(nsp->f_iosize, LONG_MAX);
698 osp->f_blocks = nsp->f_blocks;
699 osp->f_bfree = nsp->f_bfree;
700 osp->f_bavail = nsp->f_bavail;
701 osp->f_files = MIN(nsp->f_files, LONG_MAX);
702 osp->f_ffree = MIN(nsp->f_ffree, LONG_MAX);
703 osp->f_owner = nsp->f_owner;
704 osp->f_type = nsp->f_type;
705 osp->f_flags = nsp->f_flags;
706 osp->f_syncwrites = MIN(nsp->f_syncwrites, LONG_MAX);
707 osp->f_asyncwrites = MIN(nsp->f_asyncwrites, LONG_MAX);
708 osp->f_syncreads = MIN(nsp->f_syncreads, LONG_MAX);
709 osp->f_asyncreads = MIN(nsp->f_asyncreads, LONG_MAX);
710 strlcpy(osp->f_fstypename, nsp->f_fstypename,
711 MIN(MFSNAMELEN, OMFSNAMELEN));
712 strlcpy(osp->f_mntonname, nsp->f_mntonname,
713 MIN(MNAMELEN, OMNAMELEN));
714 strlcpy(osp->f_mntfromname, nsp->f_mntfromname,
715 MIN(MNAMELEN, OMNAMELEN));
716 osp->f_fsid = nsp->f_fsid;
718 #endif /* COMPAT_FREEBSD4 */
721 * Change current working directory to a given file descriptor.
723 #ifndef _SYS_SYSPROTO_H_
731 struct fchdir_args /* {
735 register struct filedesc *fdp = td->td_proc->p_fd;
736 struct vnode *vp, *tdp, *vpold;
742 AUDIT_ARG_FD(uap->fd);
743 if ((error = getvnode(fdp, uap->fd, &fp)) != 0)
748 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
749 vn_lock(vp, LK_SHARED | LK_RETRY);
750 AUDIT_ARG_VNODE1(vp);
751 error = change_dir(vp, td);
752 while (!error && (mp = vp->v_mountedhere) != NULL) {
756 tvfslocked = VFS_LOCK_GIANT(mp);
757 error = VFS_ROOT(mp, LK_SHARED, &tdp);
760 VFS_UNLOCK_GIANT(tvfslocked);
764 VFS_UNLOCK_GIANT(vfslocked);
766 vfslocked = tvfslocked;
770 VFS_UNLOCK_GIANT(vfslocked);
774 VFS_UNLOCK_GIANT(vfslocked);
776 vpold = fdp->fd_cdir;
778 FILEDESC_XUNLOCK(fdp);
779 vfslocked = VFS_LOCK_GIANT(vpold->v_mount);
781 VFS_UNLOCK_GIANT(vfslocked);
786 * Change current working directory (``.'').
788 #ifndef _SYS_SYSPROTO_H_
796 struct chdir_args /* {
801 return (kern_chdir(td, uap->path, UIO_USERSPACE));
805 kern_chdir(struct thread *td, char *path, enum uio_seg pathseg)
807 register struct filedesc *fdp = td->td_proc->p_fd;
813 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1 |
814 MPSAFE, pathseg, path, td);
815 if ((error = namei(&nd)) != 0)
817 vfslocked = NDHASGIANT(&nd);
818 if ((error = change_dir(nd.ni_vp, td)) != 0) {
820 VFS_UNLOCK_GIANT(vfslocked);
821 NDFREE(&nd, NDF_ONLY_PNBUF);
824 VOP_UNLOCK(nd.ni_vp, 0);
825 VFS_UNLOCK_GIANT(vfslocked);
826 NDFREE(&nd, NDF_ONLY_PNBUF);
829 fdp->fd_cdir = nd.ni_vp;
830 FILEDESC_XUNLOCK(fdp);
831 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
833 VFS_UNLOCK_GIANT(vfslocked);
838 * Helper function for raised chroot(2) security function: Refuse if
839 * any filedescriptors are open directories.
842 chroot_refuse_vdir_fds(fdp)
843 struct filedesc *fdp;
849 FILEDESC_LOCK_ASSERT(fdp);
851 for (fd = 0; fd < fdp->fd_nfiles ; fd++) {
852 fp = fget_locked(fdp, fd);
855 if (fp->f_type == DTYPE_VNODE) {
857 if (vp->v_type == VDIR)
865 * This sysctl determines if we will allow a process to chroot(2) if it
866 * has a directory open:
867 * 0: disallowed for all processes.
868 * 1: allowed for processes that were not already chroot(2)'ed.
869 * 2: allowed for all processes.
872 static int chroot_allow_open_directories = 1;
874 SYSCTL_INT(_kern, OID_AUTO, chroot_allow_open_directories, CTLFLAG_RW,
875 &chroot_allow_open_directories, 0, "");
878 * Change notion of root (``/'') directory.
880 #ifndef _SYS_SYSPROTO_H_
888 struct chroot_args /* {
896 error = priv_check(td, PRIV_VFS_CHROOT);
899 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | MPSAFE |
900 AUDITVNODE1, UIO_USERSPACE, uap->path, td);
904 vfslocked = NDHASGIANT(&nd);
905 if ((error = change_dir(nd.ni_vp, td)) != 0)
908 if ((error = mac_vnode_check_chroot(td->td_ucred, nd.ni_vp)))
911 VOP_UNLOCK(nd.ni_vp, 0);
912 error = change_root(nd.ni_vp, td);
914 VFS_UNLOCK_GIANT(vfslocked);
915 NDFREE(&nd, NDF_ONLY_PNBUF);
919 VFS_UNLOCK_GIANT(vfslocked);
921 NDFREE(&nd, NDF_ONLY_PNBUF);
926 * Common routine for chroot and chdir. Callers must provide a locked vnode
936 ASSERT_VOP_LOCKED(vp, "change_dir(): vp not locked");
937 if (vp->v_type != VDIR)
940 error = mac_vnode_check_chdir(td->td_ucred, vp);
944 error = VOP_ACCESS(vp, VEXEC, td->td_ucred, td);
949 * Common routine for kern_chroot() and jail_attach(). The caller is
950 * responsible for invoking priv_check() and mac_vnode_check_chroot() to
951 * authorize this operation.
958 struct filedesc *fdp;
963 VFS_ASSERT_GIANT(vp->v_mount);
964 fdp = td->td_proc->p_fd;
966 if (chroot_allow_open_directories == 0 ||
967 (chroot_allow_open_directories == 1 && fdp->fd_rdir != rootvnode)) {
968 error = chroot_refuse_vdir_fds(fdp);
970 FILEDESC_XUNLOCK(fdp);
974 oldvp = fdp->fd_rdir;
981 FILEDESC_XUNLOCK(fdp);
982 vfslocked = VFS_LOCK_GIANT(oldvp->v_mount);
984 VFS_UNLOCK_GIANT(vfslocked);
989 * Check permissions, allocate an open file structure, and call the device
990 * open routine if any.
992 #ifndef _SYS_SYSPROTO_H_
1002 register struct open_args /* {
1009 return (kern_open(td, uap->path, UIO_USERSPACE, uap->flags, uap->mode));
1012 #ifndef _SYS_SYSPROTO_H_
1013 struct openat_args {
1021 openat(struct thread *td, struct openat_args *uap)
1024 return (kern_openat(td, uap->fd, uap->path, UIO_USERSPACE, uap->flag,
1029 kern_open(struct thread *td, char *path, enum uio_seg pathseg, int flags,
1033 return (kern_openat(td, AT_FDCWD, path, pathseg, flags, mode));
1037 kern_openat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1038 int flags, int mode)
1040 struct proc *p = td->td_proc;
1041 struct filedesc *fdp = p->p_fd;
1046 int type, indx, error;
1048 struct nameidata nd;
1051 AUDIT_ARG_FFLAGS(flags);
1052 AUDIT_ARG_MODE(mode);
1053 /* XXX: audit dirfd */
1055 * Only one of the O_EXEC, O_RDONLY, O_WRONLY and O_RDWR may
1058 if (flags & O_EXEC) {
1059 if (flags & O_ACCMODE)
1061 } else if ((flags & O_ACCMODE) == O_ACCMODE)
1064 flags = FFLAGS(flags);
1066 error = fallocf(td, &nfp, &indx, flags);
1069 /* An extra reference on `nfp' has been held for us by falloc(). */
1071 /* Set the flags early so the finit in devfs can pick them up. */
1072 fp->f_flag = flags & FMASK;
1073 cmode = ((mode &~ fdp->fd_cmask) & ALLPERMS) &~ S_ISTXT;
1074 NDINIT_AT(&nd, LOOKUP, FOLLOW | AUDITVNODE1 | MPSAFE, pathseg, path, fd,
1076 td->td_dupfd = -1; /* XXX check for fdopen */
1077 error = vn_open(&nd, &flags, cmode, fp);
1080 * If the vn_open replaced the method vector, something
1081 * wonderous happened deep below and we just pass it up
1082 * pretending we know what we do.
1084 if (error == ENXIO && fp->f_ops != &badfileops) {
1086 td->td_retval[0] = indx;
1091 * handle special fdopen() case. bleh. dupfdopen() is
1092 * responsible for dropping the old contents of ofiles[indx]
1095 if ((error == ENODEV || error == ENXIO) &&
1096 td->td_dupfd >= 0 && /* XXX from fdopen */
1098 dupfdopen(td, fdp, indx, td->td_dupfd, flags, error)) == 0) {
1099 td->td_retval[0] = indx;
1104 * Clean up the descriptor, but only if another thread hadn't
1105 * replaced or closed it.
1107 fdclose(fdp, fp, indx, td);
1113 vfslocked = NDHASGIANT(&nd);
1114 NDFREE(&nd, NDF_ONLY_PNBUF);
1118 * Store the vnode, for any f_type. Typically, the vnode use
1119 * count is decremented by direct call to vn_closefile() for
1120 * files that switched type in the cdevsw fdopen() method.
1124 * If the file wasn't claimed by devfs bind it to the normal
1125 * vnode operations here.
1127 if (fp->f_ops == &badfileops) {
1128 KASSERT(vp->v_type != VFIFO, ("Unexpected fifo."));
1130 finit(fp, flags & FMASK, DTYPE_VNODE, vp, &vnops);
1134 if (fp->f_type == DTYPE_VNODE && (flags & (O_EXLOCK | O_SHLOCK)) != 0) {
1135 lf.l_whence = SEEK_SET;
1138 if (flags & O_EXLOCK)
1139 lf.l_type = F_WRLCK;
1141 lf.l_type = F_RDLCK;
1143 if ((flags & FNONBLOCK) == 0)
1145 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf,
1148 atomic_set_int(&fp->f_flag, FHASLOCK);
1150 if (flags & O_TRUNC) {
1151 error = fo_truncate(fp, 0, td->td_ucred, td);
1155 VFS_UNLOCK_GIANT(vfslocked);
1157 * Release our private reference, leaving the one associated with
1158 * the descriptor table intact.
1161 td->td_retval[0] = indx;
1164 VFS_UNLOCK_GIANT(vfslocked);
1165 fdclose(fdp, fp, indx, td);
1174 #ifndef _SYS_SYSPROTO_H_
1175 struct ocreat_args {
1183 register struct ocreat_args /* {
1189 return (kern_open(td, uap->path, UIO_USERSPACE,
1190 O_WRONLY | O_CREAT | O_TRUNC, uap->mode));
1192 #endif /* COMPAT_43 */
1195 * Create a special file.
1197 #ifndef _SYS_SYSPROTO_H_
1207 register struct mknod_args /* {
1214 return (kern_mknod(td, uap->path, UIO_USERSPACE, uap->mode, uap->dev));
1217 #ifndef _SYS_SYSPROTO_H_
1218 struct mknodat_args {
1226 mknodat(struct thread *td, struct mknodat_args *uap)
1229 return (kern_mknodat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode,
1234 kern_mknod(struct thread *td, char *path, enum uio_seg pathseg, int mode,
1238 return (kern_mknodat(td, AT_FDCWD, path, pathseg, mode, dev));
1242 kern_mknodat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1250 struct nameidata nd;
1253 AUDIT_ARG_MODE(mode);
1255 switch (mode & S_IFMT) {
1258 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
1261 error = priv_check(td, PRIV_VFS_MKNOD_BAD);
1264 error = priv_check(td, PRIV_VFS_MKNOD_WHT);
1268 return (kern_mkfifoat(td, fd, path, pathseg, mode));
1278 NDINIT_AT(&nd, CREATE, LOCKPARENT | SAVENAME | MPSAFE | AUDITVNODE1,
1279 pathseg, path, fd, td);
1280 if ((error = namei(&nd)) != 0)
1282 vfslocked = NDHASGIANT(&nd);
1285 NDFREE(&nd, NDF_ONLY_PNBUF);
1286 if (vp == nd.ni_dvp)
1291 VFS_UNLOCK_GIANT(vfslocked);
1295 vattr.va_mode = (mode & ALLPERMS) &
1296 ~td->td_proc->p_fd->fd_cmask;
1297 vattr.va_rdev = dev;
1300 switch (mode & S_IFMT) {
1301 case S_IFMT: /* used by badsect to flag bad sectors */
1302 vattr.va_type = VBAD;
1305 vattr.va_type = VCHR;
1308 vattr.va_type = VBLK;
1314 panic("kern_mknod: invalid mode");
1317 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1318 NDFREE(&nd, NDF_ONLY_PNBUF);
1320 VFS_UNLOCK_GIANT(vfslocked);
1321 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1326 if (error == 0 && !whiteout)
1327 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp,
1328 &nd.ni_cnd, &vattr);
1332 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, CREATE);
1334 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp,
1335 &nd.ni_cnd, &vattr);
1340 NDFREE(&nd, NDF_ONLY_PNBUF);
1342 vn_finished_write(mp);
1343 VFS_UNLOCK_GIANT(vfslocked);
1348 * Create a named pipe.
1350 #ifndef _SYS_SYSPROTO_H_
1351 struct mkfifo_args {
1359 register struct mkfifo_args /* {
1365 return (kern_mkfifo(td, uap->path, UIO_USERSPACE, uap->mode));
1368 #ifndef _SYS_SYSPROTO_H_
1369 struct mkfifoat_args {
1376 mkfifoat(struct thread *td, struct mkfifoat_args *uap)
1379 return (kern_mkfifoat(td, uap->fd, uap->path, UIO_USERSPACE,
1384 kern_mkfifo(struct thread *td, char *path, enum uio_seg pathseg, int mode)
1387 return (kern_mkfifoat(td, AT_FDCWD, path, pathseg, mode));
1391 kern_mkfifoat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1397 struct nameidata nd;
1400 AUDIT_ARG_MODE(mode);
1403 NDINIT_AT(&nd, CREATE, LOCKPARENT | SAVENAME | MPSAFE | AUDITVNODE1,
1404 pathseg, path, fd, td);
1405 if ((error = namei(&nd)) != 0)
1407 vfslocked = NDHASGIANT(&nd);
1408 if (nd.ni_vp != NULL) {
1409 NDFREE(&nd, NDF_ONLY_PNBUF);
1410 if (nd.ni_vp == nd.ni_dvp)
1415 VFS_UNLOCK_GIANT(vfslocked);
1418 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1419 NDFREE(&nd, NDF_ONLY_PNBUF);
1421 VFS_UNLOCK_GIANT(vfslocked);
1422 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1427 vattr.va_type = VFIFO;
1428 vattr.va_mode = (mode & ALLPERMS) & ~td->td_proc->p_fd->fd_cmask;
1430 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1435 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
1442 vn_finished_write(mp);
1443 VFS_UNLOCK_GIANT(vfslocked);
1444 NDFREE(&nd, NDF_ONLY_PNBUF);
1449 * Make a hard file link.
1451 #ifndef _SYS_SYSPROTO_H_
1460 register struct link_args /* {
1466 return (kern_link(td, uap->path, uap->link, UIO_USERSPACE));
1469 #ifndef _SYS_SYSPROTO_H_
1470 struct linkat_args {
1479 linkat(struct thread *td, struct linkat_args *uap)
1484 if (flag & ~AT_SYMLINK_FOLLOW)
1487 return (kern_linkat(td, uap->fd1, uap->fd2, uap->path1, uap->path2,
1488 UIO_USERSPACE, (flag & AT_SYMLINK_FOLLOW) ? FOLLOW : NOFOLLOW));
1491 int hardlink_check_uid = 0;
1492 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
1493 &hardlink_check_uid, 0,
1494 "Unprivileged processes cannot create hard links to files owned by other "
1496 static int hardlink_check_gid = 0;
1497 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
1498 &hardlink_check_gid, 0,
1499 "Unprivileged processes cannot create hard links to files owned by other "
1503 can_hardlink(struct vnode *vp, struct ucred *cred)
1508 if (!hardlink_check_uid && !hardlink_check_gid)
1511 error = VOP_GETATTR(vp, &va, cred);
1515 if (hardlink_check_uid && cred->cr_uid != va.va_uid) {
1516 error = priv_check_cred(cred, PRIV_VFS_LINK, 0);
1521 if (hardlink_check_gid && !groupmember(va.va_gid, cred)) {
1522 error = priv_check_cred(cred, PRIV_VFS_LINK, 0);
1531 kern_link(struct thread *td, char *path, char *link, enum uio_seg segflg)
1534 return (kern_linkat(td, AT_FDCWD, AT_FDCWD, path,link, segflg, FOLLOW));
1538 kern_linkat(struct thread *td, int fd1, int fd2, char *path1, char *path2,
1539 enum uio_seg segflg, int follow)
1543 struct nameidata nd;
1549 NDINIT_AT(&nd, LOOKUP, follow | MPSAFE | AUDITVNODE1, segflg, path1,
1552 if ((error = namei(&nd)) != 0)
1554 vfslocked = NDHASGIANT(&nd);
1555 NDFREE(&nd, NDF_ONLY_PNBUF);
1557 if (vp->v_type == VDIR) {
1559 VFS_UNLOCK_GIANT(vfslocked);
1560 return (EPERM); /* POSIX */
1562 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0) {
1564 VFS_UNLOCK_GIANT(vfslocked);
1567 NDINIT_AT(&nd, CREATE, LOCKPARENT | SAVENAME | MPSAFE | AUDITVNODE2,
1568 segflg, path2, fd2, td);
1569 if ((error = namei(&nd)) == 0) {
1570 lvfslocked = NDHASGIANT(&nd);
1571 if (nd.ni_vp != NULL) {
1572 if (nd.ni_dvp == nd.ni_vp)
1578 } else if ((error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY))
1580 error = can_hardlink(vp, td->td_ucred);
1583 error = mac_vnode_check_link(td->td_ucred,
1584 nd.ni_dvp, vp, &nd.ni_cnd);
1587 error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
1591 NDFREE(&nd, NDF_ONLY_PNBUF);
1592 VFS_UNLOCK_GIANT(lvfslocked);
1595 vn_finished_write(mp);
1596 VFS_UNLOCK_GIANT(vfslocked);
1601 * Make a symbolic link.
1603 #ifndef _SYS_SYSPROTO_H_
1604 struct symlink_args {
1612 register struct symlink_args /* {
1618 return (kern_symlink(td, uap->path, uap->link, UIO_USERSPACE));
1621 #ifndef _SYS_SYSPROTO_H_
1622 struct symlinkat_args {
1629 symlinkat(struct thread *td, struct symlinkat_args *uap)
1632 return (kern_symlinkat(td, uap->path1, uap->fd, uap->path2,
1637 kern_symlink(struct thread *td, char *path, char *link, enum uio_seg segflg)
1640 return (kern_symlinkat(td, path, AT_FDCWD, link, segflg));
1644 kern_symlinkat(struct thread *td, char *path1, int fd, char *path2,
1645 enum uio_seg segflg)
1651 struct nameidata nd;
1654 if (segflg == UIO_SYSSPACE) {
1657 syspath = uma_zalloc(namei_zone, M_WAITOK);
1658 if ((error = copyinstr(path1, syspath, MAXPATHLEN, NULL)) != 0)
1661 AUDIT_ARG_TEXT(syspath);
1664 NDINIT_AT(&nd, CREATE, LOCKPARENT | SAVENAME | MPSAFE | AUDITVNODE1,
1665 segflg, path2, fd, td);
1666 if ((error = namei(&nd)) != 0)
1668 vfslocked = NDHASGIANT(&nd);
1670 NDFREE(&nd, NDF_ONLY_PNBUF);
1671 if (nd.ni_vp == nd.ni_dvp)
1676 VFS_UNLOCK_GIANT(vfslocked);
1680 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1681 NDFREE(&nd, NDF_ONLY_PNBUF);
1683 VFS_UNLOCK_GIANT(vfslocked);
1684 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1689 vattr.va_mode = ACCESSPERMS &~ td->td_proc->p_fd->fd_cmask;
1691 vattr.va_type = VLNK;
1692 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1697 error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr, syspath);
1703 NDFREE(&nd, NDF_ONLY_PNBUF);
1705 vn_finished_write(mp);
1706 VFS_UNLOCK_GIANT(vfslocked);
1708 if (segflg != UIO_SYSSPACE)
1709 uma_zfree(namei_zone, syspath);
1714 * Delete a whiteout from the filesystem.
1719 register struct undelete_args /* {
1725 struct nameidata nd;
1730 NDINIT(&nd, DELETE, LOCKPARENT | DOWHITEOUT | MPSAFE | AUDITVNODE1,
1731 UIO_USERSPACE, uap->path, td);
1735 vfslocked = NDHASGIANT(&nd);
1737 if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & ISWHITEOUT)) {
1738 NDFREE(&nd, NDF_ONLY_PNBUF);
1739 if (nd.ni_vp == nd.ni_dvp)
1745 VFS_UNLOCK_GIANT(vfslocked);
1748 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1749 NDFREE(&nd, NDF_ONLY_PNBUF);
1751 VFS_UNLOCK_GIANT(vfslocked);
1752 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1756 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, DELETE);
1757 NDFREE(&nd, NDF_ONLY_PNBUF);
1759 vn_finished_write(mp);
1760 VFS_UNLOCK_GIANT(vfslocked);
1765 * Delete a name from the filesystem.
1767 #ifndef _SYS_SYSPROTO_H_
1768 struct unlink_args {
1775 struct unlink_args /* {
1780 return (kern_unlink(td, uap->path, UIO_USERSPACE));
1783 #ifndef _SYS_SYSPROTO_H_
1784 struct unlinkat_args {
1791 unlinkat(struct thread *td, struct unlinkat_args *uap)
1793 int flag = uap->flag;
1795 char *path = uap->path;
1797 if (flag & ~AT_REMOVEDIR)
1800 if (flag & AT_REMOVEDIR)
1801 return (kern_rmdirat(td, fd, path, UIO_USERSPACE));
1803 return (kern_unlinkat(td, fd, path, UIO_USERSPACE));
1807 kern_unlink(struct thread *td, char *path, enum uio_seg pathseg)
1810 return (kern_unlinkat(td, AT_FDCWD, path, pathseg));
1814 kern_unlinkat(struct thread *td, int fd, char *path, enum uio_seg pathseg)
1819 struct nameidata nd;
1824 NDINIT_AT(&nd, DELETE, LOCKPARENT | LOCKLEAF | MPSAFE | AUDITVNODE1,
1825 pathseg, path, fd, td);
1826 if ((error = namei(&nd)) != 0)
1827 return (error == EINVAL ? EPERM : error);
1828 vfslocked = NDHASGIANT(&nd);
1830 if (vp->v_type == VDIR)
1831 error = EPERM; /* POSIX */
1834 * The root of a mounted filesystem cannot be deleted.
1836 * XXX: can this only be a VDIR case?
1838 if (vp->v_vflag & VV_ROOT)
1842 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1843 NDFREE(&nd, NDF_ONLY_PNBUF);
1845 if (vp == nd.ni_dvp)
1849 VFS_UNLOCK_GIANT(vfslocked);
1850 if ((error = vn_start_write(NULL, &mp,
1851 V_XSLEEP | PCATCH)) != 0)
1856 error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp,
1861 error = VOP_REMOVE(nd.ni_dvp, vp, &nd.ni_cnd);
1865 vn_finished_write(mp);
1867 NDFREE(&nd, NDF_ONLY_PNBUF);
1869 if (vp == nd.ni_dvp)
1873 VFS_UNLOCK_GIANT(vfslocked);
1878 * Reposition read/write file offset.
1880 #ifndef _SYS_SYSPROTO_H_
1891 register struct lseek_args /* {
1898 struct ucred *cred = td->td_ucred;
1906 AUDIT_ARG_FD(uap->fd);
1907 if ((error = fget(td, uap->fd, &fp)) != 0)
1909 if (!(fp->f_ops->fo_flags & DFLAG_SEEKABLE)) {
1914 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
1915 noneg = (vp->v_type != VCHR);
1916 offset = uap->offset;
1917 switch (uap->whence) {
1920 (fp->f_offset < 0 ||
1921 (offset > 0 && fp->f_offset > OFF_MAX - offset))) {
1925 offset += fp->f_offset;
1928 vn_lock(vp, LK_SHARED | LK_RETRY);
1929 error = VOP_GETATTR(vp, &vattr, cred);
1935 * If the file references a disk device, then fetch
1936 * the media size and use that to determine the ending
1939 if (vattr.va_size == 0 && vp->v_type == VCHR &&
1940 fo_ioctl(fp, DIOCGMEDIASIZE, &size, cred, td) == 0)
1941 vattr.va_size = size;
1943 (vattr.va_size > OFF_MAX ||
1944 (offset > 0 && vattr.va_size > OFF_MAX - offset))) {
1948 offset += vattr.va_size;
1953 error = fo_ioctl(fp, FIOSEEKDATA, &offset, cred, td);
1956 error = fo_ioctl(fp, FIOSEEKHOLE, &offset, cred, td);
1961 if (error == 0 && noneg && offset < 0)
1965 fp->f_offset = offset;
1966 VFS_KNOTE_UNLOCKED(vp, 0);
1967 *(off_t *)(td->td_retval) = fp->f_offset;
1970 VFS_UNLOCK_GIANT(vfslocked);
1974 #if defined(COMPAT_43)
1976 * Reposition read/write file offset.
1978 #ifndef _SYS_SYSPROTO_H_
1979 struct olseek_args {
1988 register struct olseek_args /* {
1994 struct lseek_args /* {
2002 nuap.offset = uap->offset;
2003 nuap.whence = uap->whence;
2004 return (lseek(td, &nuap));
2006 #endif /* COMPAT_43 */
2008 /* Version with the 'pad' argument */
2010 freebsd6_lseek(td, uap)
2012 register struct freebsd6_lseek_args *uap;
2014 struct lseek_args ouap;
2017 ouap.offset = uap->offset;
2018 ouap.whence = uap->whence;
2019 return (lseek(td, &ouap));
2023 * Check access permissions using passed credentials.
2026 vn_access(vp, user_flags, cred, td)
2035 /* Flags == 0 means only check for existence. */
2039 if (user_flags & R_OK)
2041 if (user_flags & W_OK)
2043 if (user_flags & X_OK)
2046 error = mac_vnode_check_access(cred, vp, accmode);
2050 if ((accmode & VWRITE) == 0 || (error = vn_writechk(vp)) == 0)
2051 error = VOP_ACCESS(vp, accmode, cred, td);
2057 * Check access permissions using "real" credentials.
2059 #ifndef _SYS_SYSPROTO_H_
2060 struct access_args {
2068 register struct access_args /* {
2074 return (kern_access(td, uap->path, UIO_USERSPACE, uap->flags));
2077 #ifndef _SYS_SYSPROTO_H_
2078 struct faccessat_args {
2086 faccessat(struct thread *td, struct faccessat_args *uap)
2089 if (uap->flag & ~AT_EACCESS)
2091 return (kern_accessat(td, uap->fd, uap->path, UIO_USERSPACE, uap->flag,
2096 kern_access(struct thread *td, char *path, enum uio_seg pathseg, int mode)
2099 return (kern_accessat(td, AT_FDCWD, path, pathseg, 0, mode));
2103 kern_accessat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2104 int flags, int mode)
2106 struct ucred *cred, *tmpcred;
2108 struct nameidata nd;
2113 * Create and modify a temporary credential instead of one that
2114 * is potentially shared.
2116 if (!(flags & AT_EACCESS)) {
2117 cred = td->td_ucred;
2118 tmpcred = crdup(cred);
2119 tmpcred->cr_uid = cred->cr_ruid;
2120 tmpcred->cr_groups[0] = cred->cr_rgid;
2121 td->td_ucred = tmpcred;
2123 cred = tmpcred = td->td_ucred;
2124 AUDIT_ARG_VALUE(mode);
2125 NDINIT_AT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | MPSAFE |
2126 AUDITVNODE1, pathseg, path, fd, td);
2127 if ((error = namei(&nd)) != 0)
2129 vfslocked = NDHASGIANT(&nd);
2132 error = vn_access(vp, mode, tmpcred, td);
2133 NDFREE(&nd, NDF_ONLY_PNBUF);
2135 VFS_UNLOCK_GIANT(vfslocked);
2137 if (!(flags & AT_EACCESS)) {
2138 td->td_ucred = cred;
2145 * Check access permissions using "effective" credentials.
2147 #ifndef _SYS_SYSPROTO_H_
2148 struct eaccess_args {
2156 register struct eaccess_args /* {
2162 return (kern_eaccess(td, uap->path, UIO_USERSPACE, uap->flags));
2166 kern_eaccess(struct thread *td, char *path, enum uio_seg pathseg, int flags)
2169 return (kern_accessat(td, AT_FDCWD, path, pathseg, AT_EACCESS, flags));
2172 #if defined(COMPAT_43)
2174 * Get file status; this version follows links.
2176 #ifndef _SYS_SYSPROTO_H_
2185 register struct ostat_args /* {
2194 error = kern_stat(td, uap->path, UIO_USERSPACE, &sb);
2198 error = copyout(&osb, uap->ub, sizeof (osb));
2203 * Get file status; this version does not follow links.
2205 #ifndef _SYS_SYSPROTO_H_
2206 struct olstat_args {
2214 register struct olstat_args /* {
2223 error = kern_lstat(td, uap->path, UIO_USERSPACE, &sb);
2227 error = copyout(&osb, uap->ub, sizeof (osb));
2232 * Convert from an old to a new stat structure.
2240 ost->st_dev = st->st_dev;
2241 ost->st_ino = st->st_ino;
2242 ost->st_mode = st->st_mode;
2243 ost->st_nlink = st->st_nlink;
2244 ost->st_uid = st->st_uid;
2245 ost->st_gid = st->st_gid;
2246 ost->st_rdev = st->st_rdev;
2247 if (st->st_size < (quad_t)1 << 32)
2248 ost->st_size = st->st_size;
2251 ost->st_atime = st->st_atime;
2252 ost->st_mtime = st->st_mtime;
2253 ost->st_ctime = st->st_ctime;
2254 ost->st_blksize = st->st_blksize;
2255 ost->st_blocks = st->st_blocks;
2256 ost->st_flags = st->st_flags;
2257 ost->st_gen = st->st_gen;
2259 #endif /* COMPAT_43 */
2262 * Get file status; this version follows links.
2264 #ifndef _SYS_SYSPROTO_H_
2273 register struct stat_args /* {
2281 error = kern_stat(td, uap->path, UIO_USERSPACE, &sb);
2283 error = copyout(&sb, uap->ub, sizeof (sb));
2287 #ifndef _SYS_SYSPROTO_H_
2288 struct fstatat_args {
2296 fstatat(struct thread *td, struct fstatat_args *uap)
2301 error = kern_statat(td, uap->flag, uap->fd, uap->path,
2302 UIO_USERSPACE, &sb);
2304 error = copyout(&sb, uap->buf, sizeof (sb));
2309 kern_stat(struct thread *td, char *path, enum uio_seg pathseg, struct stat *sbp)
2312 return (kern_statat(td, 0, AT_FDCWD, path, pathseg, sbp));
2316 kern_statat(struct thread *td, int flag, int fd, char *path,
2317 enum uio_seg pathseg, struct stat *sbp)
2320 return (kern_statat_vnhook(td, flag, fd, path, pathseg, sbp, NULL));
2324 kern_statat_vnhook(struct thread *td, int flag, int fd, char *path,
2325 enum uio_seg pathseg, struct stat *sbp,
2326 void (*hook)(struct vnode *vp, struct stat *sbp))
2328 struct nameidata nd;
2330 int error, vfslocked;
2332 if (flag & ~AT_SYMLINK_NOFOLLOW)
2335 NDINIT_AT(&nd, LOOKUP, ((flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW :
2336 FOLLOW) | LOCKSHARED | LOCKLEAF | AUDITVNODE1 | MPSAFE, pathseg,
2339 if ((error = namei(&nd)) != 0)
2341 vfslocked = NDHASGIANT(&nd);
2342 error = vn_stat(nd.ni_vp, &sb, td->td_ucred, NOCRED, td);
2344 SDT_PROBE(vfs, , stat, mode, path, sb.st_mode, 0, 0, 0);
2345 if (S_ISREG(sb.st_mode))
2346 SDT_PROBE(vfs, , stat, reg, path, pathseg, 0, 0, 0);
2347 if (__predict_false(hook != NULL))
2348 hook(nd.ni_vp, &sb);
2350 NDFREE(&nd, NDF_ONLY_PNBUF);
2352 VFS_UNLOCK_GIANT(vfslocked);
2357 if (KTRPOINT(td, KTR_STRUCT))
2364 * Get file status; this version does not follow links.
2366 #ifndef _SYS_SYSPROTO_H_
2375 register struct lstat_args /* {
2383 error = kern_lstat(td, uap->path, UIO_USERSPACE, &sb);
2385 error = copyout(&sb, uap->ub, sizeof (sb));
2390 kern_lstat(struct thread *td, char *path, enum uio_seg pathseg, struct stat *sbp)
2393 return (kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, path, pathseg,
2398 * Implementation of the NetBSD [l]stat() functions.
2405 bzero(nsb, sizeof *nsb);
2406 nsb->st_dev = sb->st_dev;
2407 nsb->st_ino = sb->st_ino;
2408 nsb->st_mode = sb->st_mode;
2409 nsb->st_nlink = sb->st_nlink;
2410 nsb->st_uid = sb->st_uid;
2411 nsb->st_gid = sb->st_gid;
2412 nsb->st_rdev = sb->st_rdev;
2413 nsb->st_atimespec = sb->st_atimespec;
2414 nsb->st_mtimespec = sb->st_mtimespec;
2415 nsb->st_ctimespec = sb->st_ctimespec;
2416 nsb->st_size = sb->st_size;
2417 nsb->st_blocks = sb->st_blocks;
2418 nsb->st_blksize = sb->st_blksize;
2419 nsb->st_flags = sb->st_flags;
2420 nsb->st_gen = sb->st_gen;
2421 nsb->st_birthtimespec = sb->st_birthtimespec;
2424 #ifndef _SYS_SYSPROTO_H_
2433 register struct nstat_args /* {
2442 error = kern_stat(td, uap->path, UIO_USERSPACE, &sb);
2445 cvtnstat(&sb, &nsb);
2446 error = copyout(&nsb, uap->ub, sizeof (nsb));
2451 * NetBSD lstat. Get file status; this version does not follow links.
2453 #ifndef _SYS_SYSPROTO_H_
2462 register struct nlstat_args /* {
2471 error = kern_lstat(td, uap->path, UIO_USERSPACE, &sb);
2474 cvtnstat(&sb, &nsb);
2475 error = copyout(&nsb, uap->ub, sizeof (nsb));
2480 * Get configurable pathname variables.
2482 #ifndef _SYS_SYSPROTO_H_
2483 struct pathconf_args {
2491 register struct pathconf_args /* {
2497 return (kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name, FOLLOW));
2500 #ifndef _SYS_SYSPROTO_H_
2501 struct lpathconf_args {
2509 register struct lpathconf_args /* {
2515 return (kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name, NOFOLLOW));
2519 kern_pathconf(struct thread *td, char *path, enum uio_seg pathseg, int name,
2522 struct nameidata nd;
2523 int error, vfslocked;
2525 NDINIT(&nd, LOOKUP, LOCKSHARED | LOCKLEAF | MPSAFE | AUDITVNODE1 |
2526 flags, pathseg, path, td);
2527 if ((error = namei(&nd)) != 0)
2529 vfslocked = NDHASGIANT(&nd);
2530 NDFREE(&nd, NDF_ONLY_PNBUF);
2532 /* If asynchronous I/O is available, it works for all files. */
2533 if (name == _PC_ASYNC_IO)
2534 td->td_retval[0] = async_io_version;
2536 error = VOP_PATHCONF(nd.ni_vp, name, td->td_retval);
2538 VFS_UNLOCK_GIANT(vfslocked);
2543 * Return target name of a symbolic link.
2545 #ifndef _SYS_SYSPROTO_H_
2546 struct readlink_args {
2555 register struct readlink_args /* {
2562 return (kern_readlink(td, uap->path, UIO_USERSPACE, uap->buf,
2563 UIO_USERSPACE, uap->count));
2565 #ifndef _SYS_SYSPROTO_H_
2566 struct readlinkat_args {
2574 readlinkat(struct thread *td, struct readlinkat_args *uap)
2577 return (kern_readlinkat(td, uap->fd, uap->path, UIO_USERSPACE,
2578 uap->buf, UIO_USERSPACE, uap->bufsize));
2582 kern_readlink(struct thread *td, char *path, enum uio_seg pathseg, char *buf,
2583 enum uio_seg bufseg, size_t count)
2586 return (kern_readlinkat(td, AT_FDCWD, path, pathseg, buf, bufseg,
2591 kern_readlinkat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2592 char *buf, enum uio_seg bufseg, size_t count)
2598 struct nameidata nd;
2601 if (count > INT_MAX)
2604 NDINIT_AT(&nd, LOOKUP, NOFOLLOW | LOCKSHARED | LOCKLEAF | MPSAFE |
2605 AUDITVNODE1, pathseg, path, fd, td);
2607 if ((error = namei(&nd)) != 0)
2609 NDFREE(&nd, NDF_ONLY_PNBUF);
2610 vfslocked = NDHASGIANT(&nd);
2613 error = mac_vnode_check_readlink(td->td_ucred, vp);
2616 VFS_UNLOCK_GIANT(vfslocked);
2620 if (vp->v_type != VLNK)
2623 aiov.iov_base = buf;
2624 aiov.iov_len = count;
2625 auio.uio_iov = &aiov;
2626 auio.uio_iovcnt = 1;
2627 auio.uio_offset = 0;
2628 auio.uio_rw = UIO_READ;
2629 auio.uio_segflg = bufseg;
2631 auio.uio_resid = count;
2632 error = VOP_READLINK(vp, &auio, td->td_ucred);
2635 VFS_UNLOCK_GIANT(vfslocked);
2636 td->td_retval[0] = count - auio.uio_resid;
2641 * Common implementation code for chflags() and fchflags().
2644 setfflags(td, vp, flags)
2654 * Prevent non-root users from setting flags on devices. When
2655 * a device is reused, users can retain ownership of the device
2656 * if they are allowed to set flags and programs assume that
2657 * chown can't fail when done as root.
2659 if (vp->v_type == VCHR || vp->v_type == VBLK) {
2660 error = priv_check(td, PRIV_VFS_CHFLAGS_DEV);
2665 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2667 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2669 vattr.va_flags = flags;
2671 error = mac_vnode_check_setflags(td->td_ucred, vp, vattr.va_flags);
2674 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
2676 vn_finished_write(mp);
2681 * Change flags of a file given a path name.
2683 #ifndef _SYS_SYSPROTO_H_
2684 struct chflags_args {
2692 register struct chflags_args /* {
2698 struct nameidata nd;
2701 AUDIT_ARG_FFLAGS(uap->flags);
2702 NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE | AUDITVNODE1, UIO_USERSPACE,
2704 if ((error = namei(&nd)) != 0)
2706 NDFREE(&nd, NDF_ONLY_PNBUF);
2707 vfslocked = NDHASGIANT(&nd);
2708 error = setfflags(td, nd.ni_vp, uap->flags);
2710 VFS_UNLOCK_GIANT(vfslocked);
2715 * Same as chflags() but doesn't follow symlinks.
2720 register struct lchflags_args /* {
2726 struct nameidata nd;
2729 AUDIT_ARG_FFLAGS(uap->flags);
2730 NDINIT(&nd, LOOKUP, NOFOLLOW | MPSAFE | AUDITVNODE1, UIO_USERSPACE,
2732 if ((error = namei(&nd)) != 0)
2734 vfslocked = NDHASGIANT(&nd);
2735 NDFREE(&nd, NDF_ONLY_PNBUF);
2736 error = setfflags(td, nd.ni_vp, uap->flags);
2738 VFS_UNLOCK_GIANT(vfslocked);
2743 * Change flags of a file given a file descriptor.
2745 #ifndef _SYS_SYSPROTO_H_
2746 struct fchflags_args {
2754 register struct fchflags_args /* {
2763 AUDIT_ARG_FD(uap->fd);
2764 AUDIT_ARG_FFLAGS(uap->flags);
2765 if ((error = getvnode(td->td_proc->p_fd, uap->fd, &fp)) != 0)
2767 vfslocked = VFS_LOCK_GIANT(fp->f_vnode->v_mount);
2769 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
2770 AUDIT_ARG_VNODE1(fp->f_vnode);
2771 VOP_UNLOCK(fp->f_vnode, 0);
2773 error = setfflags(td, fp->f_vnode, uap->flags);
2774 VFS_UNLOCK_GIANT(vfslocked);
2780 * Common implementation code for chmod(), lchmod() and fchmod().
2783 setfmode(td, vp, mode)
2792 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2794 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2796 vattr.va_mode = mode & ALLPERMS;
2798 error = mac_vnode_check_setmode(td->td_ucred, vp, vattr.va_mode);
2801 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
2803 vn_finished_write(mp);
2808 * Change mode of a file given path name.
2810 #ifndef _SYS_SYSPROTO_H_
2819 register struct chmod_args /* {
2825 return (kern_chmod(td, uap->path, UIO_USERSPACE, uap->mode));
2828 #ifndef _SYS_SYSPROTO_H_
2829 struct fchmodat_args {
2837 fchmodat(struct thread *td, struct fchmodat_args *uap)
2839 int flag = uap->flag;
2841 char *path = uap->path;
2842 mode_t mode = uap->mode;
2844 if (flag & ~AT_SYMLINK_NOFOLLOW)
2847 return (kern_fchmodat(td, fd, path, UIO_USERSPACE, mode, flag));
2851 kern_chmod(struct thread *td, char *path, enum uio_seg pathseg, int mode)
2854 return (kern_fchmodat(td, AT_FDCWD, path, pathseg, mode, 0));
2858 * Change mode of a file given path name (don't follow links.)
2860 #ifndef _SYS_SYSPROTO_H_
2861 struct lchmod_args {
2869 register struct lchmod_args /* {
2875 return (kern_fchmodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2876 uap->mode, AT_SYMLINK_NOFOLLOW));
2881 kern_fchmodat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2882 mode_t mode, int flag)
2885 struct nameidata nd;
2889 AUDIT_ARG_MODE(mode);
2890 follow = (flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
2891 NDINIT_AT(&nd, LOOKUP, follow | MPSAFE | AUDITVNODE1, pathseg, path,
2893 if ((error = namei(&nd)) != 0)
2895 vfslocked = NDHASGIANT(&nd);
2896 NDFREE(&nd, NDF_ONLY_PNBUF);
2897 error = setfmode(td, nd.ni_vp, mode);
2899 VFS_UNLOCK_GIANT(vfslocked);
2904 * Change mode of a file given a file descriptor.
2906 #ifndef _SYS_SYSPROTO_H_
2907 struct fchmod_args {
2915 register struct fchmod_args /* {
2924 AUDIT_ARG_FD(uap->fd);
2925 AUDIT_ARG_MODE(uap->mode);
2926 if ((error = getvnode(td->td_proc->p_fd, uap->fd, &fp)) != 0)
2928 vfslocked = VFS_LOCK_GIANT(fp->f_vnode->v_mount);
2930 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
2931 AUDIT_ARG_VNODE1(fp->f_vnode);
2932 VOP_UNLOCK(fp->f_vnode, 0);
2934 error = setfmode(td, fp->f_vnode, uap->mode);
2935 VFS_UNLOCK_GIANT(vfslocked);
2941 * Common implementation for chown(), lchown(), and fchown()
2944 setfown(td, vp, uid, gid)
2954 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2956 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2961 error = mac_vnode_check_setowner(td->td_ucred, vp, vattr.va_uid,
2965 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
2967 vn_finished_write(mp);
2972 * Set ownership given a path name.
2974 #ifndef _SYS_SYSPROTO_H_
2984 register struct chown_args /* {
2991 return (kern_chown(td, uap->path, UIO_USERSPACE, uap->uid, uap->gid));
2994 #ifndef _SYS_SYSPROTO_H_
2995 struct fchownat_args {
3004 fchownat(struct thread *td, struct fchownat_args *uap)
3009 if (flag & ~AT_SYMLINK_NOFOLLOW)
3012 return (kern_fchownat(td, uap->fd, uap->path, UIO_USERSPACE, uap->uid,
3013 uap->gid, uap->flag));
3017 kern_chown(struct thread *td, char *path, enum uio_seg pathseg, int uid,
3021 return (kern_fchownat(td, AT_FDCWD, path, pathseg, uid, gid, 0));
3025 kern_fchownat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
3026 int uid, int gid, int flag)
3028 struct nameidata nd;
3029 int error, vfslocked, follow;
3031 AUDIT_ARG_OWNER(uid, gid);
3032 follow = (flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
3033 NDINIT_AT(&nd, LOOKUP, follow | MPSAFE | AUDITVNODE1, pathseg, path,
3036 if ((error = namei(&nd)) != 0)
3038 vfslocked = NDHASGIANT(&nd);
3039 NDFREE(&nd, NDF_ONLY_PNBUF);
3040 error = setfown(td, nd.ni_vp, uid, gid);
3042 VFS_UNLOCK_GIANT(vfslocked);
3047 * Set ownership given a path name, do not cross symlinks.
3049 #ifndef _SYS_SYSPROTO_H_
3050 struct lchown_args {
3059 register struct lchown_args /* {
3066 return (kern_lchown(td, uap->path, UIO_USERSPACE, uap->uid, uap->gid));
3070 kern_lchown(struct thread *td, char *path, enum uio_seg pathseg, int uid,
3074 return (kern_fchownat(td, AT_FDCWD, path, pathseg, uid, gid,
3075 AT_SYMLINK_NOFOLLOW));
3079 * Set ownership given a file descriptor.
3081 #ifndef _SYS_SYSPROTO_H_
3082 struct fchown_args {
3091 register struct fchown_args /* {
3101 AUDIT_ARG_FD(uap->fd);
3102 AUDIT_ARG_OWNER(uap->uid, uap->gid);
3103 if ((error = getvnode(td->td_proc->p_fd, uap->fd, &fp)) != 0)
3105 vfslocked = VFS_LOCK_GIANT(fp->f_vnode->v_mount);
3107 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
3108 AUDIT_ARG_VNODE1(fp->f_vnode);
3109 VOP_UNLOCK(fp->f_vnode, 0);
3111 error = setfown(td, fp->f_vnode, uap->uid, uap->gid);
3112 VFS_UNLOCK_GIANT(vfslocked);
3118 * Common implementation code for utimes(), lutimes(), and futimes().
3121 getutimes(usrtvp, tvpseg, tsp)
3122 const struct timeval *usrtvp;
3123 enum uio_seg tvpseg;
3124 struct timespec *tsp;
3126 struct timeval tv[2];
3127 const struct timeval *tvp;
3130 if (usrtvp == NULL) {
3131 vfs_timestamp(&tsp[0]);
3134 if (tvpseg == UIO_SYSSPACE) {
3137 if ((error = copyin(usrtvp, tv, sizeof(tv))) != 0)
3142 if (tvp[0].tv_usec < 0 || tvp[0].tv_usec >= 1000000 ||
3143 tvp[1].tv_usec < 0 || tvp[1].tv_usec >= 1000000)
3145 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
3146 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
3152 * Common implementation code for utimes(), lutimes(), and futimes().
3155 setutimes(td, vp, ts, numtimes, nullflag)
3158 const struct timespec *ts;
3162 int error, setbirthtime;
3166 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
3168 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3170 if (numtimes < 3 && !VOP_GETATTR(vp, &vattr, td->td_ucred) &&
3171 timespeccmp(&ts[1], &vattr.va_birthtime, < ))
3174 vattr.va_atime = ts[0];
3175 vattr.va_mtime = ts[1];
3177 vattr.va_birthtime = ts[1];
3179 vattr.va_birthtime = ts[2];
3181 vattr.va_vaflags |= VA_UTIMES_NULL;
3183 error = mac_vnode_check_setutimes(td->td_ucred, vp, vattr.va_atime,
3187 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3189 vn_finished_write(mp);
3194 * Set the access and modification times of a file.
3196 #ifndef _SYS_SYSPROTO_H_
3197 struct utimes_args {
3199 struct timeval *tptr;
3205 register struct utimes_args /* {
3207 struct timeval *tptr;
3211 return (kern_utimes(td, uap->path, UIO_USERSPACE, uap->tptr,
3215 #ifndef _SYS_SYSPROTO_H_
3216 struct futimesat_args {
3219 const struct timeval * times;
3223 futimesat(struct thread *td, struct futimesat_args *uap)
3226 return (kern_utimesat(td, uap->fd, uap->path, UIO_USERSPACE,
3227 uap->times, UIO_USERSPACE));
3231 kern_utimes(struct thread *td, char *path, enum uio_seg pathseg,
3232 struct timeval *tptr, enum uio_seg tptrseg)
3235 return (kern_utimesat(td, AT_FDCWD, path, pathseg, tptr, tptrseg));
3239 kern_utimesat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
3240 struct timeval *tptr, enum uio_seg tptrseg)
3242 struct nameidata nd;
3243 struct timespec ts[2];
3244 int error, vfslocked;
3246 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3248 NDINIT_AT(&nd, LOOKUP, FOLLOW | MPSAFE | AUDITVNODE1, pathseg, path,
3251 if ((error = namei(&nd)) != 0)
3253 vfslocked = NDHASGIANT(&nd);
3254 NDFREE(&nd, NDF_ONLY_PNBUF);
3255 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3257 VFS_UNLOCK_GIANT(vfslocked);
3262 * Set the access and modification times of a file.
3264 #ifndef _SYS_SYSPROTO_H_
3265 struct lutimes_args {
3267 struct timeval *tptr;
3273 register struct lutimes_args /* {
3275 struct timeval *tptr;
3279 return (kern_lutimes(td, uap->path, UIO_USERSPACE, uap->tptr,
3284 kern_lutimes(struct thread *td, char *path, enum uio_seg pathseg,
3285 struct timeval *tptr, enum uio_seg tptrseg)
3287 struct timespec ts[2];
3289 struct nameidata nd;
3292 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3294 NDINIT(&nd, LOOKUP, NOFOLLOW | MPSAFE | AUDITVNODE1, pathseg, path, td);
3295 if ((error = namei(&nd)) != 0)
3297 vfslocked = NDHASGIANT(&nd);
3298 NDFREE(&nd, NDF_ONLY_PNBUF);
3299 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3301 VFS_UNLOCK_GIANT(vfslocked);
3306 * Set the access and modification times of a file.
3308 #ifndef _SYS_SYSPROTO_H_
3309 struct futimes_args {
3311 struct timeval *tptr;
3317 register struct futimes_args /* {
3319 struct timeval *tptr;
3323 return (kern_futimes(td, uap->fd, uap->tptr, UIO_USERSPACE));
3327 kern_futimes(struct thread *td, int fd, struct timeval *tptr,
3328 enum uio_seg tptrseg)
3330 struct timespec ts[2];
3336 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3338 if ((error = getvnode(td->td_proc->p_fd, fd, &fp)) != 0)
3340 vfslocked = VFS_LOCK_GIANT(fp->f_vnode->v_mount);
3342 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
3343 AUDIT_ARG_VNODE1(fp->f_vnode);
3344 VOP_UNLOCK(fp->f_vnode, 0);
3346 error = setutimes(td, fp->f_vnode, ts, 2, tptr == NULL);
3347 VFS_UNLOCK_GIANT(vfslocked);
3353 * Truncate a file given its path name.
3355 #ifndef _SYS_SYSPROTO_H_
3356 struct truncate_args {
3365 register struct truncate_args /* {
3372 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3376 kern_truncate(struct thread *td, char *path, enum uio_seg pathseg, off_t length)
3382 struct nameidata nd;
3387 NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE | AUDITVNODE1, pathseg, path, td);
3388 if ((error = namei(&nd)) != 0)
3390 vfslocked = NDHASGIANT(&nd);
3392 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0) {
3394 VFS_UNLOCK_GIANT(vfslocked);
3397 NDFREE(&nd, NDF_ONLY_PNBUF);
3398 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3399 if (vp->v_type == VDIR)
3402 else if ((error = mac_vnode_check_write(td->td_ucred, NOCRED, vp))) {
3405 else if ((error = vn_writechk(vp)) == 0 &&
3406 (error = VOP_ACCESS(vp, VWRITE, td->td_ucred, td)) == 0) {
3408 vattr.va_size = length;
3409 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3412 vn_finished_write(mp);
3413 VFS_UNLOCK_GIANT(vfslocked);
3417 #if defined(COMPAT_43)
3419 * Truncate a file given its path name.
3421 #ifndef _SYS_SYSPROTO_H_
3422 struct otruncate_args {
3430 register struct otruncate_args /* {
3435 struct truncate_args /* {
3441 nuap.path = uap->path;
3442 nuap.length = uap->length;
3443 return (truncate(td, &nuap));
3445 #endif /* COMPAT_43 */
3447 /* Versions with the pad argument */
3449 freebsd6_truncate(struct thread *td, struct freebsd6_truncate_args *uap)
3451 struct truncate_args ouap;
3453 ouap.path = uap->path;
3454 ouap.length = uap->length;
3455 return (truncate(td, &ouap));
3459 freebsd6_ftruncate(struct thread *td, struct freebsd6_ftruncate_args *uap)
3461 struct ftruncate_args ouap;
3464 ouap.length = uap->length;
3465 return (ftruncate(td, &ouap));
3469 * Sync an open file.
3471 #ifndef _SYS_SYSPROTO_H_
3479 struct fsync_args /* {
3487 int error, lock_flags;
3489 AUDIT_ARG_FD(uap->fd);
3490 if ((error = getvnode(td->td_proc->p_fd, uap->fd, &fp)) != 0)
3493 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
3494 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
3496 if (MNT_SHARED_WRITES(mp) ||
3497 ((mp == NULL) && MNT_SHARED_WRITES(vp->v_mount))) {
3498 lock_flags = LK_SHARED;
3500 lock_flags = LK_EXCLUSIVE;
3502 vn_lock(vp, lock_flags | LK_RETRY);
3503 AUDIT_ARG_VNODE1(vp);
3504 if (vp->v_object != NULL) {
3505 VM_OBJECT_LOCK(vp->v_object);
3506 vm_object_page_clean(vp->v_object, 0, 0, 0);
3507 VM_OBJECT_UNLOCK(vp->v_object);
3509 error = VOP_FSYNC(vp, MNT_WAIT, td);
3512 vn_finished_write(mp);
3514 VFS_UNLOCK_GIANT(vfslocked);
3520 * Rename files. Source and destination must either both be directories, or
3521 * both not be directories. If target is a directory, it must be empty.
3523 #ifndef _SYS_SYSPROTO_H_
3524 struct rename_args {
3532 register struct rename_args /* {
3538 return (kern_rename(td, uap->from, uap->to, UIO_USERSPACE));
3541 #ifndef _SYS_SYSPROTO_H_
3542 struct renameat_args {
3550 renameat(struct thread *td, struct renameat_args *uap)
3553 return (kern_renameat(td, uap->oldfd, uap->old, uap->newfd, uap->new,
3558 kern_rename(struct thread *td, char *from, char *to, enum uio_seg pathseg)
3561 return (kern_renameat(td, AT_FDCWD, from, AT_FDCWD, to, pathseg));
3565 kern_renameat(struct thread *td, int oldfd, char *old, int newfd, char *new,
3566 enum uio_seg pathseg)
3568 struct mount *mp = NULL;
3569 struct vnode *tvp, *fvp, *tdvp;
3570 struct nameidata fromnd, tond;
3577 NDINIT_AT(&fromnd, DELETE, LOCKPARENT | LOCKLEAF | SAVESTART | MPSAFE |
3578 AUDITVNODE1, pathseg, old, oldfd, td);
3580 NDINIT_AT(&fromnd, DELETE, WANTPARENT | SAVESTART | MPSAFE |
3581 AUDITVNODE1, pathseg, old, oldfd, td);
3584 if ((error = namei(&fromnd)) != 0)
3586 fvfslocked = NDHASGIANT(&fromnd);
3589 error = mac_vnode_check_rename_from(td->td_ucred, fromnd.ni_dvp,
3590 fromnd.ni_vp, &fromnd.ni_cnd);
3591 VOP_UNLOCK(fromnd.ni_dvp, 0);
3592 if (fromnd.ni_dvp != fromnd.ni_vp)
3593 VOP_UNLOCK(fromnd.ni_vp, 0);
3597 error = vn_start_write(fvp, &mp, V_WAIT | PCATCH);
3599 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3600 vrele(fromnd.ni_dvp);
3604 NDINIT_AT(&tond, RENAME, LOCKPARENT | LOCKLEAF | NOCACHE | SAVESTART |
3605 MPSAFE | AUDITVNODE2, pathseg, new, newfd, td);
3606 if (fromnd.ni_vp->v_type == VDIR)
3607 tond.ni_cnd.cn_flags |= WILLBEDIR;
3608 if ((error = namei(&tond)) != 0) {
3609 /* Translate error code for rename("dir1", "dir2/."). */
3610 if (error == EISDIR && fvp->v_type == VDIR)
3612 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3613 vrele(fromnd.ni_dvp);
3615 vn_finished_write(mp);
3618 tvfslocked = NDHASGIANT(&tond);
3622 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
3625 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
3635 * If the source is the same as the destination (that is, if they
3636 * are links to the same vnode), then there is nothing to do.
3642 error = mac_vnode_check_rename_to(td->td_ucred, tdvp,
3643 tond.ni_vp, fromnd.ni_dvp == tdvp, &tond.ni_cnd);
3647 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
3648 tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
3649 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3650 NDFREE(&tond, NDF_ONLY_PNBUF);
3652 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3653 NDFREE(&tond, NDF_ONLY_PNBUF);
3660 vrele(fromnd.ni_dvp);
3663 vrele(tond.ni_startdir);
3664 vn_finished_write(mp);
3666 if (fromnd.ni_startdir)
3667 vrele(fromnd.ni_startdir);
3668 VFS_UNLOCK_GIANT(fvfslocked);
3669 VFS_UNLOCK_GIANT(tvfslocked);
3676 * Make a directory file.
3678 #ifndef _SYS_SYSPROTO_H_
3687 register struct mkdir_args /* {
3693 return (kern_mkdir(td, uap->path, UIO_USERSPACE, uap->mode));
3696 #ifndef _SYS_SYSPROTO_H_
3697 struct mkdirat_args {
3704 mkdirat(struct thread *td, struct mkdirat_args *uap)
3707 return (kern_mkdirat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode));
3711 kern_mkdir(struct thread *td, char *path, enum uio_seg segflg, int mode)
3714 return (kern_mkdirat(td, AT_FDCWD, path, segflg, mode));
3718 kern_mkdirat(struct thread *td, int fd, char *path, enum uio_seg segflg,
3725 struct nameidata nd;
3728 AUDIT_ARG_MODE(mode);
3731 NDINIT_AT(&nd, CREATE, LOCKPARENT | SAVENAME | MPSAFE | AUDITVNODE1,
3732 segflg, path, fd, td);
3733 nd.ni_cnd.cn_flags |= WILLBEDIR;
3734 if ((error = namei(&nd)) != 0)
3736 vfslocked = NDHASGIANT(&nd);
3739 NDFREE(&nd, NDF_ONLY_PNBUF);
3741 * XXX namei called with LOCKPARENT but not LOCKLEAF has
3742 * the strange behaviour of leaving the vnode unlocked
3743 * if the target is the same vnode as the parent.
3745 if (vp == nd.ni_dvp)
3750 VFS_UNLOCK_GIANT(vfslocked);
3753 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3754 NDFREE(&nd, NDF_ONLY_PNBUF);
3756 VFS_UNLOCK_GIANT(vfslocked);
3757 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3762 vattr.va_type = VDIR;
3763 vattr.va_mode = (mode & ACCESSPERMS) &~ td->td_proc->p_fd->fd_cmask;
3765 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
3770 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
3774 NDFREE(&nd, NDF_ONLY_PNBUF);
3778 vn_finished_write(mp);
3779 VFS_UNLOCK_GIANT(vfslocked);
3784 * Remove a directory file.
3786 #ifndef _SYS_SYSPROTO_H_
3794 struct rmdir_args /* {
3799 return (kern_rmdir(td, uap->path, UIO_USERSPACE));
3803 kern_rmdir(struct thread *td, char *path, enum uio_seg pathseg)
3806 return (kern_rmdirat(td, AT_FDCWD, path, pathseg));
3810 kern_rmdirat(struct thread *td, int fd, char *path, enum uio_seg pathseg)
3815 struct nameidata nd;
3820 NDINIT_AT(&nd, DELETE, LOCKPARENT | LOCKLEAF | MPSAFE | AUDITVNODE1,
3821 pathseg, path, fd, td);
3822 if ((error = namei(&nd)) != 0)
3824 vfslocked = NDHASGIANT(&nd);
3826 if (vp->v_type != VDIR) {
3831 * No rmdir "." please.
3833 if (nd.ni_dvp == vp) {
3838 * The root of a mounted filesystem cannot be deleted.
3840 if (vp->v_vflag & VV_ROOT) {
3845 error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp,
3850 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3851 NDFREE(&nd, NDF_ONLY_PNBUF);
3853 if (nd.ni_dvp == vp)
3857 VFS_UNLOCK_GIANT(vfslocked);
3858 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3862 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
3863 vn_finished_write(mp);
3865 NDFREE(&nd, NDF_ONLY_PNBUF);
3867 if (nd.ni_dvp == vp)
3871 VFS_UNLOCK_GIANT(vfslocked);
3877 * Read a block of directory entries in a filesystem independent format.
3879 #ifndef _SYS_SYSPROTO_H_
3880 struct ogetdirentries_args {
3888 ogetdirentries(td, uap)
3890 register struct ogetdirentries_args /* {
3899 struct uio auio, kuio;
3900 struct iovec aiov, kiov;
3901 struct dirent *dp, *edp;
3903 int error, eofflag, readcnt, vfslocked;
3906 /* XXX arbitrary sanity limit on `count'. */
3907 if (uap->count > 64 * 1024)
3909 if ((error = getvnode(td->td_proc->p_fd, uap->fd, &fp)) != 0)
3911 if ((fp->f_flag & FREAD) == 0) {
3917 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
3918 if (vp->v_type != VDIR) {
3919 VFS_UNLOCK_GIANT(vfslocked);
3923 aiov.iov_base = uap->buf;
3924 aiov.iov_len = uap->count;
3925 auio.uio_iov = &aiov;
3926 auio.uio_iovcnt = 1;
3927 auio.uio_rw = UIO_READ;
3928 auio.uio_segflg = UIO_USERSPACE;
3930 auio.uio_resid = uap->count;
3931 vn_lock(vp, LK_SHARED | LK_RETRY);
3932 loff = auio.uio_offset = fp->f_offset;
3934 error = mac_vnode_check_readdir(td->td_ucred, vp);
3937 VFS_UNLOCK_GIANT(vfslocked);
3942 # if (BYTE_ORDER != LITTLE_ENDIAN)
3943 if (vp->v_mount->mnt_maxsymlinklen <= 0) {
3944 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag,
3946 fp->f_offset = auio.uio_offset;
3951 kuio.uio_iov = &kiov;
3952 kuio.uio_segflg = UIO_SYSSPACE;
3953 kiov.iov_len = uap->count;
3954 dirbuf = malloc(uap->count, M_TEMP, M_WAITOK);
3955 kiov.iov_base = dirbuf;
3956 error = VOP_READDIR(vp, &kuio, fp->f_cred, &eofflag,
3958 fp->f_offset = kuio.uio_offset;
3960 readcnt = uap->count - kuio.uio_resid;
3961 edp = (struct dirent *)&dirbuf[readcnt];
3962 for (dp = (struct dirent *)dirbuf; dp < edp; ) {
3963 # if (BYTE_ORDER == LITTLE_ENDIAN)
3965 * The expected low byte of
3966 * dp->d_namlen is our dp->d_type.
3967 * The high MBZ byte of dp->d_namlen
3968 * is our dp->d_namlen.
3970 dp->d_type = dp->d_namlen;
3974 * The dp->d_type is the high byte
3975 * of the expected dp->d_namlen,
3976 * so must be zero'ed.
3980 if (dp->d_reclen > 0) {
3981 dp = (struct dirent *)
3982 ((char *)dp + dp->d_reclen);
3989 error = uiomove(dirbuf, readcnt, &auio);
3991 free(dirbuf, M_TEMP);
3995 VFS_UNLOCK_GIANT(vfslocked);
3999 if (uap->count == auio.uio_resid &&
4000 (vp->v_vflag & VV_ROOT) &&
4001 (vp->v_mount->mnt_flag & MNT_UNION)) {
4002 struct vnode *tvp = vp;
4003 vp = vp->v_mount->mnt_vnodecovered;
4009 VFS_UNLOCK_GIANT(vfslocked);
4013 VFS_UNLOCK_GIANT(vfslocked);
4014 error = copyout(&loff, uap->basep, sizeof(long));
4016 td->td_retval[0] = uap->count - auio.uio_resid;
4019 #endif /* COMPAT_43 */
4022 * Read a block of directory entries in a filesystem independent format.
4024 #ifndef _SYS_SYSPROTO_H_
4025 struct getdirentries_args {
4033 getdirentries(td, uap)
4035 register struct getdirentries_args /* {
4045 error = kern_getdirentries(td, uap->fd, uap->buf, uap->count, &base);
4048 if (uap->basep != NULL)
4049 error = copyout(&base, uap->basep, sizeof(long));
4054 kern_getdirentries(struct thread *td, int fd, char *buf, u_int count,
4066 if (count > INT_MAX)
4068 if ((error = getvnode(td->td_proc->p_fd, fd, &fp)) != 0)
4070 if ((fp->f_flag & FREAD) == 0) {
4076 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
4077 if (vp->v_type != VDIR) {
4078 VFS_UNLOCK_GIANT(vfslocked);
4082 aiov.iov_base = buf;
4083 aiov.iov_len = count;
4084 auio.uio_iov = &aiov;
4085 auio.uio_iovcnt = 1;
4086 auio.uio_rw = UIO_READ;
4087 auio.uio_segflg = UIO_USERSPACE;
4089 auio.uio_resid = count;
4090 vn_lock(vp, LK_SHARED | LK_RETRY);
4091 AUDIT_ARG_VNODE1(vp);
4092 loff = auio.uio_offset = fp->f_offset;
4094 error = mac_vnode_check_readdir(td->td_ucred, vp);
4097 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL,
4099 fp->f_offset = auio.uio_offset;
4102 VFS_UNLOCK_GIANT(vfslocked);
4105 if (count == auio.uio_resid &&
4106 (vp->v_vflag & VV_ROOT) &&
4107 (vp->v_mount->mnt_flag & MNT_UNION)) {
4108 struct vnode *tvp = vp;
4109 vp = vp->v_mount->mnt_vnodecovered;
4115 VFS_UNLOCK_GIANT(vfslocked);
4119 VFS_UNLOCK_GIANT(vfslocked);
4121 td->td_retval[0] = count - auio.uio_resid;
4127 #ifndef _SYS_SYSPROTO_H_
4128 struct getdents_args {
4137 register struct getdents_args /* {
4143 struct getdirentries_args ap;
4146 ap.count = uap->count;
4148 return (getdirentries(td, &ap));
4152 * Set the mode mask for creation of filesystem nodes.
4154 #ifndef _SYS_SYSPROTO_H_
4162 struct umask_args /* {
4166 register struct filedesc *fdp;
4168 FILEDESC_XLOCK(td->td_proc->p_fd);
4169 fdp = td->td_proc->p_fd;
4170 td->td_retval[0] = fdp->fd_cmask;
4171 fdp->fd_cmask = uap->newmask & ALLPERMS;
4172 FILEDESC_XUNLOCK(td->td_proc->p_fd);
4177 * Void all references to file by ripping underlying filesystem away from
4180 #ifndef _SYS_SYSPROTO_H_
4181 struct revoke_args {
4188 register struct revoke_args /* {
4195 struct nameidata nd;
4198 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | MPSAFE | AUDITVNODE1,
4199 UIO_USERSPACE, uap->path, td);
4200 if ((error = namei(&nd)) != 0)
4202 vfslocked = NDHASGIANT(&nd);
4204 NDFREE(&nd, NDF_ONLY_PNBUF);
4205 if (vp->v_type != VCHR || vp->v_rdev == NULL) {
4210 error = mac_vnode_check_revoke(td->td_ucred, vp);
4214 error = VOP_GETATTR(vp, &vattr, td->td_ucred);
4217 if (td->td_ucred->cr_uid != vattr.va_uid) {
4218 error = priv_check(td, PRIV_VFS_ADMIN);
4223 VOP_REVOKE(vp, REVOKEALL);
4226 VFS_UNLOCK_GIANT(vfslocked);
4231 * Convert a user file descriptor to a kernel file entry.
4232 * A reference on the file entry is held upon returning.
4235 getvnode(fdp, fd, fpp)
4236 struct filedesc *fdp;
4245 if (fdp == NULL || (fp = fget_unlocked(fdp, fd)) == NULL)
4247 else if (fp->f_vnode == NULL) {
4249 fdrop(fp, curthread);
4256 * Get an (NFS) file handle.
4258 #ifndef _SYS_SYSPROTO_H_
4259 struct lgetfh_args {
4267 register struct lgetfh_args *uap;
4269 struct nameidata nd;
4271 register struct vnode *vp;
4275 error = priv_check(td, PRIV_VFS_GETFH);
4278 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | MPSAFE | AUDITVNODE1,
4279 UIO_USERSPACE, uap->fname, td);
4283 vfslocked = NDHASGIANT(&nd);
4284 NDFREE(&nd, NDF_ONLY_PNBUF);
4286 bzero(&fh, sizeof(fh));
4287 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
4288 error = VOP_VPTOFH(vp, &fh.fh_fid);
4290 VFS_UNLOCK_GIANT(vfslocked);
4293 error = copyout(&fh, uap->fhp, sizeof (fh));
4297 #ifndef _SYS_SYSPROTO_H_
4306 register struct getfh_args *uap;
4308 struct nameidata nd;
4310 register struct vnode *vp;
4314 error = priv_check(td, PRIV_VFS_GETFH);
4317 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | MPSAFE | AUDITVNODE1,
4318 UIO_USERSPACE, uap->fname, td);
4322 vfslocked = NDHASGIANT(&nd);
4323 NDFREE(&nd, NDF_ONLY_PNBUF);
4325 bzero(&fh, sizeof(fh));
4326 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
4327 error = VOP_VPTOFH(vp, &fh.fh_fid);
4329 VFS_UNLOCK_GIANT(vfslocked);
4332 error = copyout(&fh, uap->fhp, sizeof (fh));
4337 * syscall for the rpc.lockd to use to translate a NFS file handle into an
4340 * warning: do not remove the priv_check() call or this becomes one giant
4343 #ifndef _SYS_SYSPROTO_H_
4344 struct fhopen_args {
4345 const struct fhandle *u_fhp;
4352 struct fhopen_args /* {
4353 const struct fhandle *u_fhp;
4357 struct proc *p = td->td_proc;
4362 struct vattr *vap = &vat;
4365 register struct filedesc *fdp = p->p_fd;
4366 int fmode, error, type;
4372 error = priv_check(td, PRIV_VFS_FHOPEN);
4375 fmode = FFLAGS(uap->flags);
4376 /* why not allow a non-read/write open for our lockd? */
4377 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
4379 error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
4382 /* find the mount point */
4383 mp = vfs_busyfs(&fhp.fh_fsid);
4386 vfslocked = VFS_LOCK_GIANT(mp);
4387 /* now give me my vnode, it gets returned to me locked */
4388 error = VFS_FHTOVP(mp, &fhp.fh_fid, &vp);
4393 * from now on we have to make sure not
4394 * to forget about the vnode
4395 * any error that causes an abort must vput(vp)
4396 * just set error = err and 'goto bad;'.
4402 if (vp->v_type == VLNK) {
4406 if (vp->v_type == VSOCK) {
4411 if (fmode & (FWRITE | O_TRUNC)) {
4412 if (vp->v_type == VDIR) {
4416 error = vn_writechk(vp);
4423 if ((fmode & O_APPEND) && (fmode & FWRITE))
4426 error = mac_vnode_check_open(td->td_ucred, vp, accmode);
4431 error = VOP_ACCESS(vp, accmode, td->td_ucred, td);
4435 if (fmode & O_TRUNC) {
4437 VOP_UNLOCK(vp, 0); /* XXX */
4438 if ((error = vn_start_write(NULL, &mp, V_WAIT | PCATCH)) != 0) {
4443 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); /* XXX */
4447 * We don't yet have fp->f_cred, so use td->td_ucred, which
4450 error = mac_vnode_check_write(td->td_ucred, td->td_ucred, vp);
4455 error = VOP_SETATTR(vp, vap, td->td_ucred);
4459 vn_finished_write(mp);
4463 error = VOP_OPEN(vp, fmode, td->td_ucred, td, NULL);
4471 * end of vn_open code
4474 if ((error = fallocf(td, &nfp, &indx, fmode)) != 0) {
4479 /* An extra reference on `nfp' has been held for us by falloc(). */
4482 finit(nfp, fmode & FMASK, DTYPE_VNODE, vp, &vnops);
4483 if (fmode & (O_EXLOCK | O_SHLOCK)) {
4484 lf.l_whence = SEEK_SET;
4487 if (fmode & O_EXLOCK)
4488 lf.l_type = F_WRLCK;
4490 lf.l_type = F_RDLCK;
4492 if ((fmode & FNONBLOCK) == 0)
4495 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf,
4498 * The lock request failed. Normally close the
4499 * descriptor but handle the case where someone might
4500 * have dup()d or close()d it when we weren't looking.
4502 fdclose(fdp, fp, indx, td);
4505 * release our private reference
4510 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
4511 atomic_set_int(&fp->f_flag, FHASLOCK);
4516 VFS_UNLOCK_GIANT(vfslocked);
4517 td->td_retval[0] = indx;
4523 VFS_UNLOCK_GIANT(vfslocked);
4528 * Stat an (NFS) file handle.
4530 #ifndef _SYS_SYSPROTO_H_
4531 struct fhstat_args {
4532 struct fhandle *u_fhp;
4539 register struct fhstat_args /* {
4540 struct fhandle *u_fhp;
4551 error = priv_check(td, PRIV_VFS_FHSTAT);
4554 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4557 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4559 vfslocked = VFS_LOCK_GIANT(mp);
4560 error = VFS_FHTOVP(mp, &fh.fh_fid, &vp);
4563 VFS_UNLOCK_GIANT(vfslocked);
4566 error = vn_stat(vp, &sb, td->td_ucred, NOCRED, td);
4568 VFS_UNLOCK_GIANT(vfslocked);
4571 error = copyout(&sb, uap->sb, sizeof(sb));
4576 * Implement fstatfs() for (NFS) file handles.
4578 #ifndef _SYS_SYSPROTO_H_
4579 struct fhstatfs_args {
4580 struct fhandle *u_fhp;
4587 struct fhstatfs_args /* {
4588 struct fhandle *u_fhp;
4596 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4599 error = kern_fhstatfs(td, fh, &sf);
4602 return (copyout(&sf, uap->buf, sizeof(sf)));
4606 kern_fhstatfs(struct thread *td, fhandle_t fh, struct statfs *buf)
4614 error = priv_check(td, PRIV_VFS_FHSTATFS);
4617 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4619 vfslocked = VFS_LOCK_GIANT(mp);
4620 error = VFS_FHTOVP(mp, &fh.fh_fid, &vp);
4623 VFS_UNLOCK_GIANT(vfslocked);
4627 error = prison_canseemount(td->td_ucred, mp);
4631 error = mac_mount_check_stat(td->td_ucred, mp);
4636 * Set these in case the underlying filesystem fails to do so.
4639 sp->f_version = STATFS_VERSION;
4640 sp->f_namemax = NAME_MAX;
4641 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
4642 error = VFS_STATFS(mp, sp);
4647 VFS_UNLOCK_GIANT(vfslocked);
4652 kern_posix_fallocate(struct thread *td, int fd, off_t offset, off_t len)
4657 off_t olen, ooffset;
4658 int error, vfslocked;
4662 error = fget(td, fd, &fp);
4666 switch (fp->f_type) {
4677 if ((fp->f_flag & FWRITE) == 0) {
4682 if (vp->v_type != VREG) {
4686 if (offset < 0 || len <= 0) {
4690 /* Check for wrap. */
4691 if (offset > OFF_MAX - len) {
4696 /* Allocating blocks may take a long time, so iterate. */
4702 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
4704 error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
4706 VFS_UNLOCK_GIANT(vfslocked);
4709 error = vn_lock(vp, LK_EXCLUSIVE);
4711 vn_finished_write(mp);
4712 VFS_UNLOCK_GIANT(vfslocked);
4716 error = mac_vnode_check_write(td->td_ucred, fp->f_cred, vp);
4719 error = VOP_ALLOCATE(vp, &offset, &len);
4721 vn_finished_write(mp);
4722 VFS_UNLOCK_GIANT(vfslocked);
4724 if (olen + ooffset != offset + len) {
4725 panic("offset + len changed from %jx/%jx to %jx/%jx",
4726 ooffset, olen, offset, len);
4728 if (error != 0 || len == 0)
4730 KASSERT(olen > len, ("Iteration did not make progress?"));
4740 posix_fallocate(struct thread *td, struct posix_fallocate_args *uap)
4743 return (kern_posix_fallocate(td, uap->fd, uap->offset, uap->len));
4747 * Unlike madvise(2), we do not make a best effort to remember every
4748 * possible caching hint. Instead, we remember the last setting with
4749 * the exception that we will allow POSIX_FADV_NORMAL to adjust the
4750 * region of any current setting.
4753 kern_posix_fadvise(struct thread *td, int fd, off_t offset, off_t len,
4756 struct fadvise_info *fa, *new;
4762 if (offset < 0 || len < 0 || offset > OFF_MAX - len)
4765 case POSIX_FADV_SEQUENTIAL:
4766 case POSIX_FADV_RANDOM:
4767 case POSIX_FADV_NOREUSE:
4768 new = malloc(sizeof(*fa), M_FADVISE, M_WAITOK);
4770 case POSIX_FADV_NORMAL:
4771 case POSIX_FADV_WILLNEED:
4772 case POSIX_FADV_DONTNEED:
4778 error = fget(td, fd, &fp);
4782 switch (fp->f_type) {
4794 if (vp->v_type != VREG) {
4801 end = offset + len - 1;
4803 case POSIX_FADV_SEQUENTIAL:
4804 case POSIX_FADV_RANDOM:
4805 case POSIX_FADV_NOREUSE:
4807 * Try to merge any existing non-standard region with
4808 * this new region if possible, otherwise create a new
4809 * non-standard region for this request.
4811 mtx_pool_lock(mtxpool_sleep, fp);
4813 if (fa != NULL && fa->fa_advice == advice &&
4814 ((fa->fa_start <= end && fa->fa_end >= offset) ||
4815 (end != OFF_MAX && fa->fa_start == end + 1) ||
4816 (fa->fa_end != OFF_MAX && fa->fa_end + 1 == offset))) {
4817 if (offset < fa->fa_start)
4818 fa->fa_start = offset;
4819 if (end > fa->fa_end)
4822 new->fa_advice = advice;
4823 new->fa_start = offset;
4825 new->fa_prevstart = 0;
4826 new->fa_prevend = 0;
4830 mtx_pool_unlock(mtxpool_sleep, fp);
4832 case POSIX_FADV_NORMAL:
4834 * If a the "normal" region overlaps with an existing
4835 * non-standard region, trim or remove the
4836 * non-standard region.
4838 mtx_pool_lock(mtxpool_sleep, fp);
4841 if (offset <= fa->fa_start && end >= fa->fa_end) {
4843 fp->f_advice = NULL;
4844 } else if (offset <= fa->fa_start &&
4845 end >= fa->fa_start)
4846 fa->fa_start = end + 1;
4847 else if (offset <= fa->fa_end && end >= fa->fa_end)
4848 fa->fa_end = offset - 1;
4849 else if (offset >= fa->fa_start && end <= fa->fa_end) {
4851 * If the "normal" region is a middle
4852 * portion of the existing
4853 * non-standard region, just remove
4854 * the whole thing rather than picking
4855 * one side or the other to
4859 fp->f_advice = NULL;
4862 mtx_pool_unlock(mtxpool_sleep, fp);
4864 case POSIX_FADV_WILLNEED:
4865 case POSIX_FADV_DONTNEED:
4866 error = VOP_ADVISE(vp, offset, end, advice);
4872 free(new, M_FADVISE);
4877 posix_fadvise(struct thread *td, struct posix_fadvise_args *uap)
4880 return (kern_posix_fadvise(td, uap->fd, uap->offset, uap->len,