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);
1110 if (error == ERESTART)
1115 vfslocked = NDHASGIANT(&nd);
1116 NDFREE(&nd, NDF_ONLY_PNBUF);
1120 * Store the vnode, for any f_type. Typically, the vnode use
1121 * count is decremented by direct call to vn_closefile() for
1122 * files that switched type in the cdevsw fdopen() method.
1126 * If the file wasn't claimed by devfs bind it to the normal
1127 * vnode operations here.
1129 if (fp->f_ops == &badfileops) {
1130 KASSERT(vp->v_type != VFIFO, ("Unexpected fifo."));
1132 finit(fp, flags & FMASK, DTYPE_VNODE, vp, &vnops);
1136 if (fp->f_type == DTYPE_VNODE && (flags & (O_EXLOCK | O_SHLOCK)) != 0) {
1137 lf.l_whence = SEEK_SET;
1140 if (flags & O_EXLOCK)
1141 lf.l_type = F_WRLCK;
1143 lf.l_type = F_RDLCK;
1145 if ((flags & FNONBLOCK) == 0)
1147 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf,
1150 atomic_set_int(&fp->f_flag, FHASLOCK);
1152 if (flags & O_TRUNC) {
1153 error = fo_truncate(fp, 0, td->td_ucred, td);
1157 VFS_UNLOCK_GIANT(vfslocked);
1159 * Release our private reference, leaving the one associated with
1160 * the descriptor table intact.
1163 td->td_retval[0] = indx;
1166 VFS_UNLOCK_GIANT(vfslocked);
1167 fdclose(fdp, fp, indx, td);
1176 #ifndef _SYS_SYSPROTO_H_
1177 struct ocreat_args {
1185 register struct ocreat_args /* {
1191 return (kern_open(td, uap->path, UIO_USERSPACE,
1192 O_WRONLY | O_CREAT | O_TRUNC, uap->mode));
1194 #endif /* COMPAT_43 */
1197 * Create a special file.
1199 #ifndef _SYS_SYSPROTO_H_
1209 register struct mknod_args /* {
1216 return (kern_mknod(td, uap->path, UIO_USERSPACE, uap->mode, uap->dev));
1219 #ifndef _SYS_SYSPROTO_H_
1220 struct mknodat_args {
1228 mknodat(struct thread *td, struct mknodat_args *uap)
1231 return (kern_mknodat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode,
1236 kern_mknod(struct thread *td, char *path, enum uio_seg pathseg, int mode,
1240 return (kern_mknodat(td, AT_FDCWD, path, pathseg, mode, dev));
1244 kern_mknodat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1252 struct nameidata nd;
1255 AUDIT_ARG_MODE(mode);
1257 switch (mode & S_IFMT) {
1260 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
1263 error = priv_check(td, PRIV_VFS_MKNOD_BAD);
1266 error = priv_check(td, PRIV_VFS_MKNOD_WHT);
1270 return (kern_mkfifoat(td, fd, path, pathseg, mode));
1280 NDINIT_AT(&nd, CREATE, LOCKPARENT | SAVENAME | MPSAFE | AUDITVNODE1,
1281 pathseg, path, fd, td);
1282 if ((error = namei(&nd)) != 0)
1284 vfslocked = NDHASGIANT(&nd);
1287 NDFREE(&nd, NDF_ONLY_PNBUF);
1288 if (vp == nd.ni_dvp)
1293 VFS_UNLOCK_GIANT(vfslocked);
1297 vattr.va_mode = (mode & ALLPERMS) &
1298 ~td->td_proc->p_fd->fd_cmask;
1299 vattr.va_rdev = dev;
1302 switch (mode & S_IFMT) {
1303 case S_IFMT: /* used by badsect to flag bad sectors */
1304 vattr.va_type = VBAD;
1307 vattr.va_type = VCHR;
1310 vattr.va_type = VBLK;
1316 panic("kern_mknod: invalid mode");
1319 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1320 NDFREE(&nd, NDF_ONLY_PNBUF);
1322 VFS_UNLOCK_GIANT(vfslocked);
1323 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1328 if (error == 0 && !whiteout)
1329 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp,
1330 &nd.ni_cnd, &vattr);
1334 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, CREATE);
1336 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp,
1337 &nd.ni_cnd, &vattr);
1342 NDFREE(&nd, NDF_ONLY_PNBUF);
1344 vn_finished_write(mp);
1345 VFS_UNLOCK_GIANT(vfslocked);
1350 * Create a named pipe.
1352 #ifndef _SYS_SYSPROTO_H_
1353 struct mkfifo_args {
1361 register struct mkfifo_args /* {
1367 return (kern_mkfifo(td, uap->path, UIO_USERSPACE, uap->mode));
1370 #ifndef _SYS_SYSPROTO_H_
1371 struct mkfifoat_args {
1378 mkfifoat(struct thread *td, struct mkfifoat_args *uap)
1381 return (kern_mkfifoat(td, uap->fd, uap->path, UIO_USERSPACE,
1386 kern_mkfifo(struct thread *td, char *path, enum uio_seg pathseg, int mode)
1389 return (kern_mkfifoat(td, AT_FDCWD, path, pathseg, mode));
1393 kern_mkfifoat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1399 struct nameidata nd;
1402 AUDIT_ARG_MODE(mode);
1405 NDINIT_AT(&nd, CREATE, LOCKPARENT | SAVENAME | MPSAFE | AUDITVNODE1,
1406 pathseg, path, fd, td);
1407 if ((error = namei(&nd)) != 0)
1409 vfslocked = NDHASGIANT(&nd);
1410 if (nd.ni_vp != NULL) {
1411 NDFREE(&nd, NDF_ONLY_PNBUF);
1412 if (nd.ni_vp == nd.ni_dvp)
1417 VFS_UNLOCK_GIANT(vfslocked);
1420 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1421 NDFREE(&nd, NDF_ONLY_PNBUF);
1423 VFS_UNLOCK_GIANT(vfslocked);
1424 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1429 vattr.va_type = VFIFO;
1430 vattr.va_mode = (mode & ALLPERMS) & ~td->td_proc->p_fd->fd_cmask;
1432 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1437 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
1444 vn_finished_write(mp);
1445 VFS_UNLOCK_GIANT(vfslocked);
1446 NDFREE(&nd, NDF_ONLY_PNBUF);
1451 * Make a hard file link.
1453 #ifndef _SYS_SYSPROTO_H_
1462 register struct link_args /* {
1468 return (kern_link(td, uap->path, uap->link, UIO_USERSPACE));
1471 #ifndef _SYS_SYSPROTO_H_
1472 struct linkat_args {
1481 linkat(struct thread *td, struct linkat_args *uap)
1486 if (flag & ~AT_SYMLINK_FOLLOW)
1489 return (kern_linkat(td, uap->fd1, uap->fd2, uap->path1, uap->path2,
1490 UIO_USERSPACE, (flag & AT_SYMLINK_FOLLOW) ? FOLLOW : NOFOLLOW));
1493 int hardlink_check_uid = 0;
1494 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
1495 &hardlink_check_uid, 0,
1496 "Unprivileged processes cannot create hard links to files owned by other "
1498 static int hardlink_check_gid = 0;
1499 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
1500 &hardlink_check_gid, 0,
1501 "Unprivileged processes cannot create hard links to files owned by other "
1505 can_hardlink(struct vnode *vp, struct ucred *cred)
1510 if (!hardlink_check_uid && !hardlink_check_gid)
1513 error = VOP_GETATTR(vp, &va, cred);
1517 if (hardlink_check_uid && cred->cr_uid != va.va_uid) {
1518 error = priv_check_cred(cred, PRIV_VFS_LINK, 0);
1523 if (hardlink_check_gid && !groupmember(va.va_gid, cred)) {
1524 error = priv_check_cred(cred, PRIV_VFS_LINK, 0);
1533 kern_link(struct thread *td, char *path, char *link, enum uio_seg segflg)
1536 return (kern_linkat(td, AT_FDCWD, AT_FDCWD, path,link, segflg, FOLLOW));
1540 kern_linkat(struct thread *td, int fd1, int fd2, char *path1, char *path2,
1541 enum uio_seg segflg, int follow)
1545 struct nameidata nd;
1551 NDINIT_AT(&nd, LOOKUP, follow | MPSAFE | AUDITVNODE1, segflg, path1,
1554 if ((error = namei(&nd)) != 0)
1556 vfslocked = NDHASGIANT(&nd);
1557 NDFREE(&nd, NDF_ONLY_PNBUF);
1559 if (vp->v_type == VDIR) {
1561 VFS_UNLOCK_GIANT(vfslocked);
1562 return (EPERM); /* POSIX */
1564 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0) {
1566 VFS_UNLOCK_GIANT(vfslocked);
1569 NDINIT_AT(&nd, CREATE, LOCKPARENT | SAVENAME | MPSAFE | AUDITVNODE2,
1570 segflg, path2, fd2, td);
1571 if ((error = namei(&nd)) == 0) {
1572 lvfslocked = NDHASGIANT(&nd);
1573 if (nd.ni_vp != NULL) {
1574 if (nd.ni_dvp == nd.ni_vp)
1580 } else if ((error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY))
1582 error = can_hardlink(vp, td->td_ucred);
1585 error = mac_vnode_check_link(td->td_ucred,
1586 nd.ni_dvp, vp, &nd.ni_cnd);
1589 error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
1593 NDFREE(&nd, NDF_ONLY_PNBUF);
1594 VFS_UNLOCK_GIANT(lvfslocked);
1597 vn_finished_write(mp);
1598 VFS_UNLOCK_GIANT(vfslocked);
1603 * Make a symbolic link.
1605 #ifndef _SYS_SYSPROTO_H_
1606 struct symlink_args {
1614 register struct symlink_args /* {
1620 return (kern_symlink(td, uap->path, uap->link, UIO_USERSPACE));
1623 #ifndef _SYS_SYSPROTO_H_
1624 struct symlinkat_args {
1631 symlinkat(struct thread *td, struct symlinkat_args *uap)
1634 return (kern_symlinkat(td, uap->path1, uap->fd, uap->path2,
1639 kern_symlink(struct thread *td, char *path, char *link, enum uio_seg segflg)
1642 return (kern_symlinkat(td, path, AT_FDCWD, link, segflg));
1646 kern_symlinkat(struct thread *td, char *path1, int fd, char *path2,
1647 enum uio_seg segflg)
1653 struct nameidata nd;
1656 if (segflg == UIO_SYSSPACE) {
1659 syspath = uma_zalloc(namei_zone, M_WAITOK);
1660 if ((error = copyinstr(path1, syspath, MAXPATHLEN, NULL)) != 0)
1663 AUDIT_ARG_TEXT(syspath);
1666 NDINIT_AT(&nd, CREATE, LOCKPARENT | SAVENAME | MPSAFE | AUDITVNODE1,
1667 segflg, path2, fd, td);
1668 if ((error = namei(&nd)) != 0)
1670 vfslocked = NDHASGIANT(&nd);
1672 NDFREE(&nd, NDF_ONLY_PNBUF);
1673 if (nd.ni_vp == nd.ni_dvp)
1678 VFS_UNLOCK_GIANT(vfslocked);
1682 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1683 NDFREE(&nd, NDF_ONLY_PNBUF);
1685 VFS_UNLOCK_GIANT(vfslocked);
1686 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1691 vattr.va_mode = ACCESSPERMS &~ td->td_proc->p_fd->fd_cmask;
1693 vattr.va_type = VLNK;
1694 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1699 error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr, syspath);
1705 NDFREE(&nd, NDF_ONLY_PNBUF);
1707 vn_finished_write(mp);
1708 VFS_UNLOCK_GIANT(vfslocked);
1710 if (segflg != UIO_SYSSPACE)
1711 uma_zfree(namei_zone, syspath);
1716 * Delete a whiteout from the filesystem.
1721 register struct undelete_args /* {
1727 struct nameidata nd;
1732 NDINIT(&nd, DELETE, LOCKPARENT | DOWHITEOUT | MPSAFE | AUDITVNODE1,
1733 UIO_USERSPACE, uap->path, td);
1737 vfslocked = NDHASGIANT(&nd);
1739 if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & ISWHITEOUT)) {
1740 NDFREE(&nd, NDF_ONLY_PNBUF);
1741 if (nd.ni_vp == nd.ni_dvp)
1747 VFS_UNLOCK_GIANT(vfslocked);
1750 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1751 NDFREE(&nd, NDF_ONLY_PNBUF);
1753 VFS_UNLOCK_GIANT(vfslocked);
1754 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1758 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, DELETE);
1759 NDFREE(&nd, NDF_ONLY_PNBUF);
1761 vn_finished_write(mp);
1762 VFS_UNLOCK_GIANT(vfslocked);
1767 * Delete a name from the filesystem.
1769 #ifndef _SYS_SYSPROTO_H_
1770 struct unlink_args {
1777 struct unlink_args /* {
1782 return (kern_unlink(td, uap->path, UIO_USERSPACE));
1785 #ifndef _SYS_SYSPROTO_H_
1786 struct unlinkat_args {
1793 unlinkat(struct thread *td, struct unlinkat_args *uap)
1795 int flag = uap->flag;
1797 char *path = uap->path;
1799 if (flag & ~AT_REMOVEDIR)
1802 if (flag & AT_REMOVEDIR)
1803 return (kern_rmdirat(td, fd, path, UIO_USERSPACE));
1805 return (kern_unlinkat(td, fd, path, UIO_USERSPACE));
1809 kern_unlink(struct thread *td, char *path, enum uio_seg pathseg)
1812 return (kern_unlinkat(td, AT_FDCWD, path, pathseg));
1816 kern_unlinkat(struct thread *td, int fd, char *path, enum uio_seg pathseg)
1821 struct nameidata nd;
1826 NDINIT_AT(&nd, DELETE, LOCKPARENT | LOCKLEAF | MPSAFE | AUDITVNODE1,
1827 pathseg, path, fd, td);
1828 if ((error = namei(&nd)) != 0)
1829 return (error == EINVAL ? EPERM : error);
1830 vfslocked = NDHASGIANT(&nd);
1832 if (vp->v_type == VDIR)
1833 error = EPERM; /* POSIX */
1836 * The root of a mounted filesystem cannot be deleted.
1838 * XXX: can this only be a VDIR case?
1840 if (vp->v_vflag & VV_ROOT)
1844 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1845 NDFREE(&nd, NDF_ONLY_PNBUF);
1847 if (vp == nd.ni_dvp)
1851 VFS_UNLOCK_GIANT(vfslocked);
1852 if ((error = vn_start_write(NULL, &mp,
1853 V_XSLEEP | PCATCH)) != 0)
1858 error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp,
1863 error = VOP_REMOVE(nd.ni_dvp, vp, &nd.ni_cnd);
1867 vn_finished_write(mp);
1869 NDFREE(&nd, NDF_ONLY_PNBUF);
1871 if (vp == nd.ni_dvp)
1875 VFS_UNLOCK_GIANT(vfslocked);
1880 * Reposition read/write file offset.
1882 #ifndef _SYS_SYSPROTO_H_
1893 register struct lseek_args /* {
1900 struct ucred *cred = td->td_ucred;
1908 AUDIT_ARG_FD(uap->fd);
1909 if ((error = fget(td, uap->fd, &fp)) != 0)
1911 if (!(fp->f_ops->fo_flags & DFLAG_SEEKABLE)) {
1916 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
1917 noneg = (vp->v_type != VCHR);
1918 offset = uap->offset;
1919 switch (uap->whence) {
1922 (fp->f_offset < 0 ||
1923 (offset > 0 && fp->f_offset > OFF_MAX - offset))) {
1927 offset += fp->f_offset;
1930 vn_lock(vp, LK_SHARED | LK_RETRY);
1931 error = VOP_GETATTR(vp, &vattr, cred);
1937 * If the file references a disk device, then fetch
1938 * the media size and use that to determine the ending
1941 if (vattr.va_size == 0 && vp->v_type == VCHR &&
1942 fo_ioctl(fp, DIOCGMEDIASIZE, &size, cred, td) == 0)
1943 vattr.va_size = size;
1945 (vattr.va_size > OFF_MAX ||
1946 (offset > 0 && vattr.va_size > OFF_MAX - offset))) {
1950 offset += vattr.va_size;
1955 error = fo_ioctl(fp, FIOSEEKDATA, &offset, cred, td);
1958 error = fo_ioctl(fp, FIOSEEKHOLE, &offset, cred, td);
1963 if (error == 0 && noneg && offset < 0)
1967 fp->f_offset = offset;
1968 VFS_KNOTE_UNLOCKED(vp, 0);
1969 *(off_t *)(td->td_retval) = fp->f_offset;
1972 VFS_UNLOCK_GIANT(vfslocked);
1976 #if defined(COMPAT_43)
1978 * Reposition read/write file offset.
1980 #ifndef _SYS_SYSPROTO_H_
1981 struct olseek_args {
1990 register struct olseek_args /* {
1996 struct lseek_args /* {
2004 nuap.offset = uap->offset;
2005 nuap.whence = uap->whence;
2006 return (lseek(td, &nuap));
2008 #endif /* COMPAT_43 */
2010 /* Version with the 'pad' argument */
2012 freebsd6_lseek(td, uap)
2014 register struct freebsd6_lseek_args *uap;
2016 struct lseek_args ouap;
2019 ouap.offset = uap->offset;
2020 ouap.whence = uap->whence;
2021 return (lseek(td, &ouap));
2025 * Check access permissions using passed credentials.
2028 vn_access(vp, user_flags, cred, td)
2037 /* Flags == 0 means only check for existence. */
2041 if (user_flags & R_OK)
2043 if (user_flags & W_OK)
2045 if (user_flags & X_OK)
2048 error = mac_vnode_check_access(cred, vp, accmode);
2052 if ((accmode & VWRITE) == 0 || (error = vn_writechk(vp)) == 0)
2053 error = VOP_ACCESS(vp, accmode, cred, td);
2059 * Check access permissions using "real" credentials.
2061 #ifndef _SYS_SYSPROTO_H_
2062 struct access_args {
2070 register struct access_args /* {
2076 return (kern_access(td, uap->path, UIO_USERSPACE, uap->flags));
2079 #ifndef _SYS_SYSPROTO_H_
2080 struct faccessat_args {
2088 faccessat(struct thread *td, struct faccessat_args *uap)
2091 if (uap->flag & ~AT_EACCESS)
2093 return (kern_accessat(td, uap->fd, uap->path, UIO_USERSPACE, uap->flag,
2098 kern_access(struct thread *td, char *path, enum uio_seg pathseg, int mode)
2101 return (kern_accessat(td, AT_FDCWD, path, pathseg, 0, mode));
2105 kern_accessat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2106 int flags, int mode)
2108 struct ucred *cred, *tmpcred;
2110 struct nameidata nd;
2115 * Create and modify a temporary credential instead of one that
2116 * is potentially shared.
2118 if (!(flags & AT_EACCESS)) {
2119 cred = td->td_ucred;
2120 tmpcred = crdup(cred);
2121 tmpcred->cr_uid = cred->cr_ruid;
2122 tmpcred->cr_groups[0] = cred->cr_rgid;
2123 td->td_ucred = tmpcred;
2125 cred = tmpcred = td->td_ucred;
2126 AUDIT_ARG_VALUE(mode);
2127 NDINIT_AT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | MPSAFE |
2128 AUDITVNODE1, pathseg, path, fd, td);
2129 if ((error = namei(&nd)) != 0)
2131 vfslocked = NDHASGIANT(&nd);
2134 error = vn_access(vp, mode, tmpcred, td);
2135 NDFREE(&nd, NDF_ONLY_PNBUF);
2137 VFS_UNLOCK_GIANT(vfslocked);
2139 if (!(flags & AT_EACCESS)) {
2140 td->td_ucred = cred;
2147 * Check access permissions using "effective" credentials.
2149 #ifndef _SYS_SYSPROTO_H_
2150 struct eaccess_args {
2158 register struct eaccess_args /* {
2164 return (kern_eaccess(td, uap->path, UIO_USERSPACE, uap->flags));
2168 kern_eaccess(struct thread *td, char *path, enum uio_seg pathseg, int flags)
2171 return (kern_accessat(td, AT_FDCWD, path, pathseg, AT_EACCESS, flags));
2174 #if defined(COMPAT_43)
2176 * Get file status; this version follows links.
2178 #ifndef _SYS_SYSPROTO_H_
2187 register struct ostat_args /* {
2196 error = kern_stat(td, uap->path, UIO_USERSPACE, &sb);
2200 error = copyout(&osb, uap->ub, sizeof (osb));
2205 * Get file status; this version does not follow links.
2207 #ifndef _SYS_SYSPROTO_H_
2208 struct olstat_args {
2216 register struct olstat_args /* {
2225 error = kern_lstat(td, uap->path, UIO_USERSPACE, &sb);
2229 error = copyout(&osb, uap->ub, sizeof (osb));
2234 * Convert from an old to a new stat structure.
2242 ost->st_dev = st->st_dev;
2243 ost->st_ino = st->st_ino;
2244 ost->st_mode = st->st_mode;
2245 ost->st_nlink = st->st_nlink;
2246 ost->st_uid = st->st_uid;
2247 ost->st_gid = st->st_gid;
2248 ost->st_rdev = st->st_rdev;
2249 if (st->st_size < (quad_t)1 << 32)
2250 ost->st_size = st->st_size;
2253 ost->st_atime = st->st_atime;
2254 ost->st_mtime = st->st_mtime;
2255 ost->st_ctime = st->st_ctime;
2256 ost->st_blksize = st->st_blksize;
2257 ost->st_blocks = st->st_blocks;
2258 ost->st_flags = st->st_flags;
2259 ost->st_gen = st->st_gen;
2261 #endif /* COMPAT_43 */
2264 * Get file status; this version follows links.
2266 #ifndef _SYS_SYSPROTO_H_
2275 register struct stat_args /* {
2283 error = kern_stat(td, uap->path, UIO_USERSPACE, &sb);
2285 error = copyout(&sb, uap->ub, sizeof (sb));
2289 #ifndef _SYS_SYSPROTO_H_
2290 struct fstatat_args {
2298 fstatat(struct thread *td, struct fstatat_args *uap)
2303 error = kern_statat(td, uap->flag, uap->fd, uap->path,
2304 UIO_USERSPACE, &sb);
2306 error = copyout(&sb, uap->buf, sizeof (sb));
2311 kern_stat(struct thread *td, char *path, enum uio_seg pathseg, struct stat *sbp)
2314 return (kern_statat(td, 0, AT_FDCWD, path, pathseg, sbp));
2318 kern_statat(struct thread *td, int flag, int fd, char *path,
2319 enum uio_seg pathseg, struct stat *sbp)
2322 return (kern_statat_vnhook(td, flag, fd, path, pathseg, sbp, NULL));
2326 kern_statat_vnhook(struct thread *td, int flag, int fd, char *path,
2327 enum uio_seg pathseg, struct stat *sbp,
2328 void (*hook)(struct vnode *vp, struct stat *sbp))
2330 struct nameidata nd;
2332 int error, vfslocked;
2334 if (flag & ~AT_SYMLINK_NOFOLLOW)
2337 NDINIT_AT(&nd, LOOKUP, ((flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW :
2338 FOLLOW) | LOCKSHARED | LOCKLEAF | AUDITVNODE1 | MPSAFE, pathseg,
2341 if ((error = namei(&nd)) != 0)
2343 vfslocked = NDHASGIANT(&nd);
2344 error = vn_stat(nd.ni_vp, &sb, td->td_ucred, NOCRED, td);
2346 SDT_PROBE(vfs, , stat, mode, path, sb.st_mode, 0, 0, 0);
2347 if (S_ISREG(sb.st_mode))
2348 SDT_PROBE(vfs, , stat, reg, path, pathseg, 0, 0, 0);
2349 if (__predict_false(hook != NULL))
2350 hook(nd.ni_vp, &sb);
2352 NDFREE(&nd, NDF_ONLY_PNBUF);
2354 VFS_UNLOCK_GIANT(vfslocked);
2359 if (KTRPOINT(td, KTR_STRUCT))
2366 * Get file status; this version does not follow links.
2368 #ifndef _SYS_SYSPROTO_H_
2377 register struct lstat_args /* {
2385 error = kern_lstat(td, uap->path, UIO_USERSPACE, &sb);
2387 error = copyout(&sb, uap->ub, sizeof (sb));
2392 kern_lstat(struct thread *td, char *path, enum uio_seg pathseg, struct stat *sbp)
2395 return (kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, path, pathseg,
2400 * Implementation of the NetBSD [l]stat() functions.
2407 bzero(nsb, sizeof *nsb);
2408 nsb->st_dev = sb->st_dev;
2409 nsb->st_ino = sb->st_ino;
2410 nsb->st_mode = sb->st_mode;
2411 nsb->st_nlink = sb->st_nlink;
2412 nsb->st_uid = sb->st_uid;
2413 nsb->st_gid = sb->st_gid;
2414 nsb->st_rdev = sb->st_rdev;
2415 nsb->st_atimespec = sb->st_atimespec;
2416 nsb->st_mtimespec = sb->st_mtimespec;
2417 nsb->st_ctimespec = sb->st_ctimespec;
2418 nsb->st_size = sb->st_size;
2419 nsb->st_blocks = sb->st_blocks;
2420 nsb->st_blksize = sb->st_blksize;
2421 nsb->st_flags = sb->st_flags;
2422 nsb->st_gen = sb->st_gen;
2423 nsb->st_birthtimespec = sb->st_birthtimespec;
2426 #ifndef _SYS_SYSPROTO_H_
2435 register struct nstat_args /* {
2444 error = kern_stat(td, uap->path, UIO_USERSPACE, &sb);
2447 cvtnstat(&sb, &nsb);
2448 error = copyout(&nsb, uap->ub, sizeof (nsb));
2453 * NetBSD lstat. Get file status; this version does not follow links.
2455 #ifndef _SYS_SYSPROTO_H_
2464 register struct nlstat_args /* {
2473 error = kern_lstat(td, uap->path, UIO_USERSPACE, &sb);
2476 cvtnstat(&sb, &nsb);
2477 error = copyout(&nsb, uap->ub, sizeof (nsb));
2482 * Get configurable pathname variables.
2484 #ifndef _SYS_SYSPROTO_H_
2485 struct pathconf_args {
2493 register struct pathconf_args /* {
2499 return (kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name, FOLLOW));
2502 #ifndef _SYS_SYSPROTO_H_
2503 struct lpathconf_args {
2511 register struct lpathconf_args /* {
2517 return (kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name, NOFOLLOW));
2521 kern_pathconf(struct thread *td, char *path, enum uio_seg pathseg, int name,
2524 struct nameidata nd;
2525 int error, vfslocked;
2527 NDINIT(&nd, LOOKUP, LOCKSHARED | LOCKLEAF | MPSAFE | AUDITVNODE1 |
2528 flags, pathseg, path, td);
2529 if ((error = namei(&nd)) != 0)
2531 vfslocked = NDHASGIANT(&nd);
2532 NDFREE(&nd, NDF_ONLY_PNBUF);
2534 /* If asynchronous I/O is available, it works for all files. */
2535 if (name == _PC_ASYNC_IO)
2536 td->td_retval[0] = async_io_version;
2538 error = VOP_PATHCONF(nd.ni_vp, name, td->td_retval);
2540 VFS_UNLOCK_GIANT(vfslocked);
2545 * Return target name of a symbolic link.
2547 #ifndef _SYS_SYSPROTO_H_
2548 struct readlink_args {
2557 register struct readlink_args /* {
2564 return (kern_readlink(td, uap->path, UIO_USERSPACE, uap->buf,
2565 UIO_USERSPACE, uap->count));
2567 #ifndef _SYS_SYSPROTO_H_
2568 struct readlinkat_args {
2576 readlinkat(struct thread *td, struct readlinkat_args *uap)
2579 return (kern_readlinkat(td, uap->fd, uap->path, UIO_USERSPACE,
2580 uap->buf, UIO_USERSPACE, uap->bufsize));
2584 kern_readlink(struct thread *td, char *path, enum uio_seg pathseg, char *buf,
2585 enum uio_seg bufseg, size_t count)
2588 return (kern_readlinkat(td, AT_FDCWD, path, pathseg, buf, bufseg,
2593 kern_readlinkat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2594 char *buf, enum uio_seg bufseg, size_t count)
2600 struct nameidata nd;
2603 if (count > INT_MAX)
2606 NDINIT_AT(&nd, LOOKUP, NOFOLLOW | LOCKSHARED | LOCKLEAF | MPSAFE |
2607 AUDITVNODE1, pathseg, path, fd, td);
2609 if ((error = namei(&nd)) != 0)
2611 NDFREE(&nd, NDF_ONLY_PNBUF);
2612 vfslocked = NDHASGIANT(&nd);
2615 error = mac_vnode_check_readlink(td->td_ucred, vp);
2618 VFS_UNLOCK_GIANT(vfslocked);
2622 if (vp->v_type != VLNK)
2625 aiov.iov_base = buf;
2626 aiov.iov_len = count;
2627 auio.uio_iov = &aiov;
2628 auio.uio_iovcnt = 1;
2629 auio.uio_offset = 0;
2630 auio.uio_rw = UIO_READ;
2631 auio.uio_segflg = bufseg;
2633 auio.uio_resid = count;
2634 error = VOP_READLINK(vp, &auio, td->td_ucred);
2637 VFS_UNLOCK_GIANT(vfslocked);
2638 td->td_retval[0] = count - auio.uio_resid;
2643 * Common implementation code for chflags() and fchflags().
2646 setfflags(td, vp, flags)
2656 * Prevent non-root users from setting flags on devices. When
2657 * a device is reused, users can retain ownership of the device
2658 * if they are allowed to set flags and programs assume that
2659 * chown can't fail when done as root.
2661 if (vp->v_type == VCHR || vp->v_type == VBLK) {
2662 error = priv_check(td, PRIV_VFS_CHFLAGS_DEV);
2667 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2669 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2671 vattr.va_flags = flags;
2673 error = mac_vnode_check_setflags(td->td_ucred, vp, vattr.va_flags);
2676 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
2678 vn_finished_write(mp);
2683 * Change flags of a file given a path name.
2685 #ifndef _SYS_SYSPROTO_H_
2686 struct chflags_args {
2694 register struct chflags_args /* {
2700 struct nameidata nd;
2703 AUDIT_ARG_FFLAGS(uap->flags);
2704 NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE | AUDITVNODE1, UIO_USERSPACE,
2706 if ((error = namei(&nd)) != 0)
2708 NDFREE(&nd, NDF_ONLY_PNBUF);
2709 vfslocked = NDHASGIANT(&nd);
2710 error = setfflags(td, nd.ni_vp, uap->flags);
2712 VFS_UNLOCK_GIANT(vfslocked);
2717 * Same as chflags() but doesn't follow symlinks.
2722 register struct lchflags_args /* {
2728 struct nameidata nd;
2731 AUDIT_ARG_FFLAGS(uap->flags);
2732 NDINIT(&nd, LOOKUP, NOFOLLOW | MPSAFE | AUDITVNODE1, UIO_USERSPACE,
2734 if ((error = namei(&nd)) != 0)
2736 vfslocked = NDHASGIANT(&nd);
2737 NDFREE(&nd, NDF_ONLY_PNBUF);
2738 error = setfflags(td, nd.ni_vp, uap->flags);
2740 VFS_UNLOCK_GIANT(vfslocked);
2745 * Change flags of a file given a file descriptor.
2747 #ifndef _SYS_SYSPROTO_H_
2748 struct fchflags_args {
2756 register struct fchflags_args /* {
2765 AUDIT_ARG_FD(uap->fd);
2766 AUDIT_ARG_FFLAGS(uap->flags);
2767 if ((error = getvnode(td->td_proc->p_fd, uap->fd, &fp)) != 0)
2769 vfslocked = VFS_LOCK_GIANT(fp->f_vnode->v_mount);
2771 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
2772 AUDIT_ARG_VNODE1(fp->f_vnode);
2773 VOP_UNLOCK(fp->f_vnode, 0);
2775 error = setfflags(td, fp->f_vnode, uap->flags);
2776 VFS_UNLOCK_GIANT(vfslocked);
2782 * Common implementation code for chmod(), lchmod() and fchmod().
2785 setfmode(td, vp, mode)
2794 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2796 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2798 vattr.va_mode = mode & ALLPERMS;
2800 error = mac_vnode_check_setmode(td->td_ucred, vp, vattr.va_mode);
2803 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
2805 vn_finished_write(mp);
2810 * Change mode of a file given path name.
2812 #ifndef _SYS_SYSPROTO_H_
2821 register struct chmod_args /* {
2827 return (kern_chmod(td, uap->path, UIO_USERSPACE, uap->mode));
2830 #ifndef _SYS_SYSPROTO_H_
2831 struct fchmodat_args {
2839 fchmodat(struct thread *td, struct fchmodat_args *uap)
2841 int flag = uap->flag;
2843 char *path = uap->path;
2844 mode_t mode = uap->mode;
2846 if (flag & ~AT_SYMLINK_NOFOLLOW)
2849 return (kern_fchmodat(td, fd, path, UIO_USERSPACE, mode, flag));
2853 kern_chmod(struct thread *td, char *path, enum uio_seg pathseg, int mode)
2856 return (kern_fchmodat(td, AT_FDCWD, path, pathseg, mode, 0));
2860 * Change mode of a file given path name (don't follow links.)
2862 #ifndef _SYS_SYSPROTO_H_
2863 struct lchmod_args {
2871 register struct lchmod_args /* {
2877 return (kern_fchmodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2878 uap->mode, AT_SYMLINK_NOFOLLOW));
2883 kern_fchmodat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2884 mode_t mode, int flag)
2887 struct nameidata nd;
2891 AUDIT_ARG_MODE(mode);
2892 follow = (flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
2893 NDINIT_AT(&nd, LOOKUP, follow | MPSAFE | AUDITVNODE1, pathseg, path,
2895 if ((error = namei(&nd)) != 0)
2897 vfslocked = NDHASGIANT(&nd);
2898 NDFREE(&nd, NDF_ONLY_PNBUF);
2899 error = setfmode(td, nd.ni_vp, mode);
2901 VFS_UNLOCK_GIANT(vfslocked);
2906 * Change mode of a file given a file descriptor.
2908 #ifndef _SYS_SYSPROTO_H_
2909 struct fchmod_args {
2917 register struct fchmod_args /* {
2926 AUDIT_ARG_FD(uap->fd);
2927 AUDIT_ARG_MODE(uap->mode);
2928 if ((error = getvnode(td->td_proc->p_fd, uap->fd, &fp)) != 0)
2930 vfslocked = VFS_LOCK_GIANT(fp->f_vnode->v_mount);
2932 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
2933 AUDIT_ARG_VNODE1(fp->f_vnode);
2934 VOP_UNLOCK(fp->f_vnode, 0);
2936 error = setfmode(td, fp->f_vnode, uap->mode);
2937 VFS_UNLOCK_GIANT(vfslocked);
2943 * Common implementation for chown(), lchown(), and fchown()
2946 setfown(td, vp, uid, gid)
2956 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2958 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2963 error = mac_vnode_check_setowner(td->td_ucred, vp, vattr.va_uid,
2967 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
2969 vn_finished_write(mp);
2974 * Set ownership given a path name.
2976 #ifndef _SYS_SYSPROTO_H_
2986 register struct chown_args /* {
2993 return (kern_chown(td, uap->path, UIO_USERSPACE, uap->uid, uap->gid));
2996 #ifndef _SYS_SYSPROTO_H_
2997 struct fchownat_args {
3006 fchownat(struct thread *td, struct fchownat_args *uap)
3011 if (flag & ~AT_SYMLINK_NOFOLLOW)
3014 return (kern_fchownat(td, uap->fd, uap->path, UIO_USERSPACE, uap->uid,
3015 uap->gid, uap->flag));
3019 kern_chown(struct thread *td, char *path, enum uio_seg pathseg, int uid,
3023 return (kern_fchownat(td, AT_FDCWD, path, pathseg, uid, gid, 0));
3027 kern_fchownat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
3028 int uid, int gid, int flag)
3030 struct nameidata nd;
3031 int error, vfslocked, follow;
3033 AUDIT_ARG_OWNER(uid, gid);
3034 follow = (flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
3035 NDINIT_AT(&nd, LOOKUP, follow | MPSAFE | AUDITVNODE1, pathseg, path,
3038 if ((error = namei(&nd)) != 0)
3040 vfslocked = NDHASGIANT(&nd);
3041 NDFREE(&nd, NDF_ONLY_PNBUF);
3042 error = setfown(td, nd.ni_vp, uid, gid);
3044 VFS_UNLOCK_GIANT(vfslocked);
3049 * Set ownership given a path name, do not cross symlinks.
3051 #ifndef _SYS_SYSPROTO_H_
3052 struct lchown_args {
3061 register struct lchown_args /* {
3068 return (kern_lchown(td, uap->path, UIO_USERSPACE, uap->uid, uap->gid));
3072 kern_lchown(struct thread *td, char *path, enum uio_seg pathseg, int uid,
3076 return (kern_fchownat(td, AT_FDCWD, path, pathseg, uid, gid,
3077 AT_SYMLINK_NOFOLLOW));
3081 * Set ownership given a file descriptor.
3083 #ifndef _SYS_SYSPROTO_H_
3084 struct fchown_args {
3093 register struct fchown_args /* {
3103 AUDIT_ARG_FD(uap->fd);
3104 AUDIT_ARG_OWNER(uap->uid, uap->gid);
3105 if ((error = getvnode(td->td_proc->p_fd, uap->fd, &fp)) != 0)
3107 vfslocked = VFS_LOCK_GIANT(fp->f_vnode->v_mount);
3109 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
3110 AUDIT_ARG_VNODE1(fp->f_vnode);
3111 VOP_UNLOCK(fp->f_vnode, 0);
3113 error = setfown(td, fp->f_vnode, uap->uid, uap->gid);
3114 VFS_UNLOCK_GIANT(vfslocked);
3120 * Common implementation code for utimes(), lutimes(), and futimes().
3123 getutimes(usrtvp, tvpseg, tsp)
3124 const struct timeval *usrtvp;
3125 enum uio_seg tvpseg;
3126 struct timespec *tsp;
3128 struct timeval tv[2];
3129 const struct timeval *tvp;
3132 if (usrtvp == NULL) {
3133 vfs_timestamp(&tsp[0]);
3136 if (tvpseg == UIO_SYSSPACE) {
3139 if ((error = copyin(usrtvp, tv, sizeof(tv))) != 0)
3144 if (tvp[0].tv_usec < 0 || tvp[0].tv_usec >= 1000000 ||
3145 tvp[1].tv_usec < 0 || tvp[1].tv_usec >= 1000000)
3147 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
3148 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
3154 * Common implementation code for utimes(), lutimes(), and futimes().
3157 setutimes(td, vp, ts, numtimes, nullflag)
3160 const struct timespec *ts;
3164 int error, setbirthtime;
3168 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
3170 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3172 if (numtimes < 3 && !VOP_GETATTR(vp, &vattr, td->td_ucred) &&
3173 timespeccmp(&ts[1], &vattr.va_birthtime, < ))
3176 vattr.va_atime = ts[0];
3177 vattr.va_mtime = ts[1];
3179 vattr.va_birthtime = ts[1];
3181 vattr.va_birthtime = ts[2];
3183 vattr.va_vaflags |= VA_UTIMES_NULL;
3185 error = mac_vnode_check_setutimes(td->td_ucred, vp, vattr.va_atime,
3189 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3191 vn_finished_write(mp);
3196 * Set the access and modification times of a file.
3198 #ifndef _SYS_SYSPROTO_H_
3199 struct utimes_args {
3201 struct timeval *tptr;
3207 register struct utimes_args /* {
3209 struct timeval *tptr;
3213 return (kern_utimes(td, uap->path, UIO_USERSPACE, uap->tptr,
3217 #ifndef _SYS_SYSPROTO_H_
3218 struct futimesat_args {
3221 const struct timeval * times;
3225 futimesat(struct thread *td, struct futimesat_args *uap)
3228 return (kern_utimesat(td, uap->fd, uap->path, UIO_USERSPACE,
3229 uap->times, UIO_USERSPACE));
3233 kern_utimes(struct thread *td, char *path, enum uio_seg pathseg,
3234 struct timeval *tptr, enum uio_seg tptrseg)
3237 return (kern_utimesat(td, AT_FDCWD, path, pathseg, tptr, tptrseg));
3241 kern_utimesat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
3242 struct timeval *tptr, enum uio_seg tptrseg)
3244 struct nameidata nd;
3245 struct timespec ts[2];
3246 int error, vfslocked;
3248 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3250 NDINIT_AT(&nd, LOOKUP, FOLLOW | MPSAFE | AUDITVNODE1, pathseg, path,
3253 if ((error = namei(&nd)) != 0)
3255 vfslocked = NDHASGIANT(&nd);
3256 NDFREE(&nd, NDF_ONLY_PNBUF);
3257 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3259 VFS_UNLOCK_GIANT(vfslocked);
3264 * Set the access and modification times of a file.
3266 #ifndef _SYS_SYSPROTO_H_
3267 struct lutimes_args {
3269 struct timeval *tptr;
3275 register struct lutimes_args /* {
3277 struct timeval *tptr;
3281 return (kern_lutimes(td, uap->path, UIO_USERSPACE, uap->tptr,
3286 kern_lutimes(struct thread *td, char *path, enum uio_seg pathseg,
3287 struct timeval *tptr, enum uio_seg tptrseg)
3289 struct timespec ts[2];
3291 struct nameidata nd;
3294 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3296 NDINIT(&nd, LOOKUP, NOFOLLOW | MPSAFE | AUDITVNODE1, pathseg, path, td);
3297 if ((error = namei(&nd)) != 0)
3299 vfslocked = NDHASGIANT(&nd);
3300 NDFREE(&nd, NDF_ONLY_PNBUF);
3301 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3303 VFS_UNLOCK_GIANT(vfslocked);
3308 * Set the access and modification times of a file.
3310 #ifndef _SYS_SYSPROTO_H_
3311 struct futimes_args {
3313 struct timeval *tptr;
3319 register struct futimes_args /* {
3321 struct timeval *tptr;
3325 return (kern_futimes(td, uap->fd, uap->tptr, UIO_USERSPACE));
3329 kern_futimes(struct thread *td, int fd, struct timeval *tptr,
3330 enum uio_seg tptrseg)
3332 struct timespec ts[2];
3338 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3340 if ((error = getvnode(td->td_proc->p_fd, fd, &fp)) != 0)
3342 vfslocked = VFS_LOCK_GIANT(fp->f_vnode->v_mount);
3344 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
3345 AUDIT_ARG_VNODE1(fp->f_vnode);
3346 VOP_UNLOCK(fp->f_vnode, 0);
3348 error = setutimes(td, fp->f_vnode, ts, 2, tptr == NULL);
3349 VFS_UNLOCK_GIANT(vfslocked);
3355 * Truncate a file given its path name.
3357 #ifndef _SYS_SYSPROTO_H_
3358 struct truncate_args {
3367 register struct truncate_args /* {
3374 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3378 kern_truncate(struct thread *td, char *path, enum uio_seg pathseg, off_t length)
3384 struct nameidata nd;
3389 NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE | AUDITVNODE1, pathseg, path, td);
3390 if ((error = namei(&nd)) != 0)
3392 vfslocked = NDHASGIANT(&nd);
3394 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0) {
3396 VFS_UNLOCK_GIANT(vfslocked);
3399 NDFREE(&nd, NDF_ONLY_PNBUF);
3400 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3401 if (vp->v_type == VDIR)
3404 else if ((error = mac_vnode_check_write(td->td_ucred, NOCRED, vp))) {
3407 else if ((error = vn_writechk(vp)) == 0 &&
3408 (error = VOP_ACCESS(vp, VWRITE, td->td_ucred, td)) == 0) {
3410 vattr.va_size = length;
3411 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3414 vn_finished_write(mp);
3415 VFS_UNLOCK_GIANT(vfslocked);
3419 #if defined(COMPAT_43)
3421 * Truncate a file given its path name.
3423 #ifndef _SYS_SYSPROTO_H_
3424 struct otruncate_args {
3432 register struct otruncate_args /* {
3437 struct truncate_args /* {
3443 nuap.path = uap->path;
3444 nuap.length = uap->length;
3445 return (truncate(td, &nuap));
3447 #endif /* COMPAT_43 */
3449 /* Versions with the pad argument */
3451 freebsd6_truncate(struct thread *td, struct freebsd6_truncate_args *uap)
3453 struct truncate_args ouap;
3455 ouap.path = uap->path;
3456 ouap.length = uap->length;
3457 return (truncate(td, &ouap));
3461 freebsd6_ftruncate(struct thread *td, struct freebsd6_ftruncate_args *uap)
3463 struct ftruncate_args ouap;
3466 ouap.length = uap->length;
3467 return (ftruncate(td, &ouap));
3471 * Sync an open file.
3473 #ifndef _SYS_SYSPROTO_H_
3481 struct fsync_args /* {
3489 int error, lock_flags;
3491 AUDIT_ARG_FD(uap->fd);
3492 if ((error = getvnode(td->td_proc->p_fd, uap->fd, &fp)) != 0)
3495 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
3496 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
3498 if (MNT_SHARED_WRITES(mp) ||
3499 ((mp == NULL) && MNT_SHARED_WRITES(vp->v_mount))) {
3500 lock_flags = LK_SHARED;
3502 lock_flags = LK_EXCLUSIVE;
3504 vn_lock(vp, lock_flags | LK_RETRY);
3505 AUDIT_ARG_VNODE1(vp);
3506 if (vp->v_object != NULL) {
3507 VM_OBJECT_LOCK(vp->v_object);
3508 vm_object_page_clean(vp->v_object, 0, 0, 0);
3509 VM_OBJECT_UNLOCK(vp->v_object);
3511 error = VOP_FSYNC(vp, MNT_WAIT, td);
3514 vn_finished_write(mp);
3516 VFS_UNLOCK_GIANT(vfslocked);
3522 * Rename files. Source and destination must either both be directories, or
3523 * both not be directories. If target is a directory, it must be empty.
3525 #ifndef _SYS_SYSPROTO_H_
3526 struct rename_args {
3534 register struct rename_args /* {
3540 return (kern_rename(td, uap->from, uap->to, UIO_USERSPACE));
3543 #ifndef _SYS_SYSPROTO_H_
3544 struct renameat_args {
3552 renameat(struct thread *td, struct renameat_args *uap)
3555 return (kern_renameat(td, uap->oldfd, uap->old, uap->newfd, uap->new,
3560 kern_rename(struct thread *td, char *from, char *to, enum uio_seg pathseg)
3563 return (kern_renameat(td, AT_FDCWD, from, AT_FDCWD, to, pathseg));
3567 kern_renameat(struct thread *td, int oldfd, char *old, int newfd, char *new,
3568 enum uio_seg pathseg)
3570 struct mount *mp = NULL;
3571 struct vnode *tvp, *fvp, *tdvp;
3572 struct nameidata fromnd, tond;
3579 NDINIT_AT(&fromnd, DELETE, LOCKPARENT | LOCKLEAF | SAVESTART | MPSAFE |
3580 AUDITVNODE1, pathseg, old, oldfd, td);
3582 NDINIT_AT(&fromnd, DELETE, WANTPARENT | SAVESTART | MPSAFE |
3583 AUDITVNODE1, pathseg, old, oldfd, td);
3586 if ((error = namei(&fromnd)) != 0)
3588 fvfslocked = NDHASGIANT(&fromnd);
3591 error = mac_vnode_check_rename_from(td->td_ucred, fromnd.ni_dvp,
3592 fromnd.ni_vp, &fromnd.ni_cnd);
3593 VOP_UNLOCK(fromnd.ni_dvp, 0);
3594 if (fromnd.ni_dvp != fromnd.ni_vp)
3595 VOP_UNLOCK(fromnd.ni_vp, 0);
3599 error = vn_start_write(fvp, &mp, V_WAIT | PCATCH);
3601 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3602 vrele(fromnd.ni_dvp);
3606 NDINIT_AT(&tond, RENAME, LOCKPARENT | LOCKLEAF | NOCACHE | SAVESTART |
3607 MPSAFE | AUDITVNODE2, pathseg, new, newfd, td);
3608 if (fromnd.ni_vp->v_type == VDIR)
3609 tond.ni_cnd.cn_flags |= WILLBEDIR;
3610 if ((error = namei(&tond)) != 0) {
3611 /* Translate error code for rename("dir1", "dir2/."). */
3612 if (error == EISDIR && fvp->v_type == VDIR)
3614 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3615 vrele(fromnd.ni_dvp);
3617 vn_finished_write(mp);
3620 tvfslocked = NDHASGIANT(&tond);
3624 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
3627 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
3637 * If the source is the same as the destination (that is, if they
3638 * are links to the same vnode), then there is nothing to do.
3644 error = mac_vnode_check_rename_to(td->td_ucred, tdvp,
3645 tond.ni_vp, fromnd.ni_dvp == tdvp, &tond.ni_cnd);
3649 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
3650 tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
3651 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3652 NDFREE(&tond, NDF_ONLY_PNBUF);
3654 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3655 NDFREE(&tond, NDF_ONLY_PNBUF);
3662 vrele(fromnd.ni_dvp);
3665 vrele(tond.ni_startdir);
3666 vn_finished_write(mp);
3668 if (fromnd.ni_startdir)
3669 vrele(fromnd.ni_startdir);
3670 VFS_UNLOCK_GIANT(fvfslocked);
3671 VFS_UNLOCK_GIANT(tvfslocked);
3678 * Make a directory file.
3680 #ifndef _SYS_SYSPROTO_H_
3689 register struct mkdir_args /* {
3695 return (kern_mkdir(td, uap->path, UIO_USERSPACE, uap->mode));
3698 #ifndef _SYS_SYSPROTO_H_
3699 struct mkdirat_args {
3706 mkdirat(struct thread *td, struct mkdirat_args *uap)
3709 return (kern_mkdirat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode));
3713 kern_mkdir(struct thread *td, char *path, enum uio_seg segflg, int mode)
3716 return (kern_mkdirat(td, AT_FDCWD, path, segflg, mode));
3720 kern_mkdirat(struct thread *td, int fd, char *path, enum uio_seg segflg,
3727 struct nameidata nd;
3730 AUDIT_ARG_MODE(mode);
3733 NDINIT_AT(&nd, CREATE, LOCKPARENT | SAVENAME | MPSAFE | AUDITVNODE1,
3734 segflg, path, fd, td);
3735 nd.ni_cnd.cn_flags |= WILLBEDIR;
3736 if ((error = namei(&nd)) != 0)
3738 vfslocked = NDHASGIANT(&nd);
3741 NDFREE(&nd, NDF_ONLY_PNBUF);
3743 * XXX namei called with LOCKPARENT but not LOCKLEAF has
3744 * the strange behaviour of leaving the vnode unlocked
3745 * if the target is the same vnode as the parent.
3747 if (vp == nd.ni_dvp)
3752 VFS_UNLOCK_GIANT(vfslocked);
3755 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3756 NDFREE(&nd, NDF_ONLY_PNBUF);
3758 VFS_UNLOCK_GIANT(vfslocked);
3759 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3764 vattr.va_type = VDIR;
3765 vattr.va_mode = (mode & ACCESSPERMS) &~ td->td_proc->p_fd->fd_cmask;
3767 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
3772 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
3776 NDFREE(&nd, NDF_ONLY_PNBUF);
3780 vn_finished_write(mp);
3781 VFS_UNLOCK_GIANT(vfslocked);
3786 * Remove a directory file.
3788 #ifndef _SYS_SYSPROTO_H_
3796 struct rmdir_args /* {
3801 return (kern_rmdir(td, uap->path, UIO_USERSPACE));
3805 kern_rmdir(struct thread *td, char *path, enum uio_seg pathseg)
3808 return (kern_rmdirat(td, AT_FDCWD, path, pathseg));
3812 kern_rmdirat(struct thread *td, int fd, char *path, enum uio_seg pathseg)
3817 struct nameidata nd;
3822 NDINIT_AT(&nd, DELETE, LOCKPARENT | LOCKLEAF | MPSAFE | AUDITVNODE1,
3823 pathseg, path, fd, td);
3824 if ((error = namei(&nd)) != 0)
3826 vfslocked = NDHASGIANT(&nd);
3828 if (vp->v_type != VDIR) {
3833 * No rmdir "." please.
3835 if (nd.ni_dvp == vp) {
3840 * The root of a mounted filesystem cannot be deleted.
3842 if (vp->v_vflag & VV_ROOT) {
3847 error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp,
3852 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3853 NDFREE(&nd, NDF_ONLY_PNBUF);
3855 if (nd.ni_dvp == vp)
3859 VFS_UNLOCK_GIANT(vfslocked);
3860 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3864 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
3865 vn_finished_write(mp);
3867 NDFREE(&nd, NDF_ONLY_PNBUF);
3869 if (nd.ni_dvp == vp)
3873 VFS_UNLOCK_GIANT(vfslocked);
3879 * Read a block of directory entries in a filesystem independent format.
3881 #ifndef _SYS_SYSPROTO_H_
3882 struct ogetdirentries_args {
3890 ogetdirentries(td, uap)
3892 register struct ogetdirentries_args /* {
3901 struct uio auio, kuio;
3902 struct iovec aiov, kiov;
3903 struct dirent *dp, *edp;
3905 int error, eofflag, readcnt, vfslocked;
3908 /* XXX arbitrary sanity limit on `count'. */
3909 if (uap->count > 64 * 1024)
3911 if ((error = getvnode(td->td_proc->p_fd, uap->fd, &fp)) != 0)
3913 if ((fp->f_flag & FREAD) == 0) {
3919 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
3920 if (vp->v_type != VDIR) {
3921 VFS_UNLOCK_GIANT(vfslocked);
3925 aiov.iov_base = uap->buf;
3926 aiov.iov_len = uap->count;
3927 auio.uio_iov = &aiov;
3928 auio.uio_iovcnt = 1;
3929 auio.uio_rw = UIO_READ;
3930 auio.uio_segflg = UIO_USERSPACE;
3932 auio.uio_resid = uap->count;
3933 vn_lock(vp, LK_SHARED | LK_RETRY);
3934 loff = auio.uio_offset = fp->f_offset;
3936 error = mac_vnode_check_readdir(td->td_ucred, vp);
3939 VFS_UNLOCK_GIANT(vfslocked);
3944 # if (BYTE_ORDER != LITTLE_ENDIAN)
3945 if (vp->v_mount->mnt_maxsymlinklen <= 0) {
3946 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag,
3948 fp->f_offset = auio.uio_offset;
3953 kuio.uio_iov = &kiov;
3954 kuio.uio_segflg = UIO_SYSSPACE;
3955 kiov.iov_len = uap->count;
3956 dirbuf = malloc(uap->count, M_TEMP, M_WAITOK);
3957 kiov.iov_base = dirbuf;
3958 error = VOP_READDIR(vp, &kuio, fp->f_cred, &eofflag,
3960 fp->f_offset = kuio.uio_offset;
3962 readcnt = uap->count - kuio.uio_resid;
3963 edp = (struct dirent *)&dirbuf[readcnt];
3964 for (dp = (struct dirent *)dirbuf; dp < edp; ) {
3965 # if (BYTE_ORDER == LITTLE_ENDIAN)
3967 * The expected low byte of
3968 * dp->d_namlen is our dp->d_type.
3969 * The high MBZ byte of dp->d_namlen
3970 * is our dp->d_namlen.
3972 dp->d_type = dp->d_namlen;
3976 * The dp->d_type is the high byte
3977 * of the expected dp->d_namlen,
3978 * so must be zero'ed.
3982 if (dp->d_reclen > 0) {
3983 dp = (struct dirent *)
3984 ((char *)dp + dp->d_reclen);
3991 error = uiomove(dirbuf, readcnt, &auio);
3993 free(dirbuf, M_TEMP);
3997 VFS_UNLOCK_GIANT(vfslocked);
4001 if (uap->count == auio.uio_resid &&
4002 (vp->v_vflag & VV_ROOT) &&
4003 (vp->v_mount->mnt_flag & MNT_UNION)) {
4004 struct vnode *tvp = vp;
4005 vp = vp->v_mount->mnt_vnodecovered;
4011 VFS_UNLOCK_GIANT(vfslocked);
4015 VFS_UNLOCK_GIANT(vfslocked);
4016 error = copyout(&loff, uap->basep, sizeof(long));
4018 td->td_retval[0] = uap->count - auio.uio_resid;
4021 #endif /* COMPAT_43 */
4024 * Read a block of directory entries in a filesystem independent format.
4026 #ifndef _SYS_SYSPROTO_H_
4027 struct getdirentries_args {
4035 getdirentries(td, uap)
4037 register struct getdirentries_args /* {
4047 error = kern_getdirentries(td, uap->fd, uap->buf, uap->count, &base);
4050 if (uap->basep != NULL)
4051 error = copyout(&base, uap->basep, sizeof(long));
4056 kern_getdirentries(struct thread *td, int fd, char *buf, u_int count,
4068 if (count > INT_MAX)
4070 if ((error = getvnode(td->td_proc->p_fd, fd, &fp)) != 0)
4072 if ((fp->f_flag & FREAD) == 0) {
4078 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
4079 if (vp->v_type != VDIR) {
4080 VFS_UNLOCK_GIANT(vfslocked);
4084 aiov.iov_base = buf;
4085 aiov.iov_len = count;
4086 auio.uio_iov = &aiov;
4087 auio.uio_iovcnt = 1;
4088 auio.uio_rw = UIO_READ;
4089 auio.uio_segflg = UIO_USERSPACE;
4091 auio.uio_resid = count;
4092 vn_lock(vp, LK_SHARED | LK_RETRY);
4093 AUDIT_ARG_VNODE1(vp);
4094 loff = auio.uio_offset = fp->f_offset;
4096 error = mac_vnode_check_readdir(td->td_ucred, vp);
4099 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL,
4101 fp->f_offset = auio.uio_offset;
4104 VFS_UNLOCK_GIANT(vfslocked);
4107 if (count == auio.uio_resid &&
4108 (vp->v_vflag & VV_ROOT) &&
4109 (vp->v_mount->mnt_flag & MNT_UNION)) {
4110 struct vnode *tvp = vp;
4111 vp = vp->v_mount->mnt_vnodecovered;
4117 VFS_UNLOCK_GIANT(vfslocked);
4121 VFS_UNLOCK_GIANT(vfslocked);
4123 td->td_retval[0] = count - auio.uio_resid;
4129 #ifndef _SYS_SYSPROTO_H_
4130 struct getdents_args {
4139 register struct getdents_args /* {
4145 struct getdirentries_args ap;
4148 ap.count = uap->count;
4150 return (getdirentries(td, &ap));
4154 * Set the mode mask for creation of filesystem nodes.
4156 #ifndef _SYS_SYSPROTO_H_
4164 struct umask_args /* {
4168 register struct filedesc *fdp;
4170 FILEDESC_XLOCK(td->td_proc->p_fd);
4171 fdp = td->td_proc->p_fd;
4172 td->td_retval[0] = fdp->fd_cmask;
4173 fdp->fd_cmask = uap->newmask & ALLPERMS;
4174 FILEDESC_XUNLOCK(td->td_proc->p_fd);
4179 * Void all references to file by ripping underlying filesystem away from
4182 #ifndef _SYS_SYSPROTO_H_
4183 struct revoke_args {
4190 register struct revoke_args /* {
4197 struct nameidata nd;
4200 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | MPSAFE | AUDITVNODE1,
4201 UIO_USERSPACE, uap->path, td);
4202 if ((error = namei(&nd)) != 0)
4204 vfslocked = NDHASGIANT(&nd);
4206 NDFREE(&nd, NDF_ONLY_PNBUF);
4207 if (vp->v_type != VCHR || vp->v_rdev == NULL) {
4212 error = mac_vnode_check_revoke(td->td_ucred, vp);
4216 error = VOP_GETATTR(vp, &vattr, td->td_ucred);
4219 if (td->td_ucred->cr_uid != vattr.va_uid) {
4220 error = priv_check(td, PRIV_VFS_ADMIN);
4225 VOP_REVOKE(vp, REVOKEALL);
4228 VFS_UNLOCK_GIANT(vfslocked);
4233 * Convert a user file descriptor to a kernel file entry.
4234 * A reference on the file entry is held upon returning.
4237 getvnode(fdp, fd, fpp)
4238 struct filedesc *fdp;
4247 if (fdp == NULL || (fp = fget_unlocked(fdp, fd)) == NULL)
4249 else if (fp->f_vnode == NULL) {
4251 fdrop(fp, curthread);
4258 * Get an (NFS) file handle.
4260 #ifndef _SYS_SYSPROTO_H_
4261 struct lgetfh_args {
4269 register struct lgetfh_args *uap;
4271 struct nameidata nd;
4273 register struct vnode *vp;
4277 error = priv_check(td, PRIV_VFS_GETFH);
4280 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | MPSAFE | AUDITVNODE1,
4281 UIO_USERSPACE, uap->fname, td);
4285 vfslocked = NDHASGIANT(&nd);
4286 NDFREE(&nd, NDF_ONLY_PNBUF);
4288 bzero(&fh, sizeof(fh));
4289 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
4290 error = VOP_VPTOFH(vp, &fh.fh_fid);
4292 VFS_UNLOCK_GIANT(vfslocked);
4295 error = copyout(&fh, uap->fhp, sizeof (fh));
4299 #ifndef _SYS_SYSPROTO_H_
4308 register struct getfh_args *uap;
4310 struct nameidata nd;
4312 register struct vnode *vp;
4316 error = priv_check(td, PRIV_VFS_GETFH);
4319 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | MPSAFE | AUDITVNODE1,
4320 UIO_USERSPACE, uap->fname, td);
4324 vfslocked = NDHASGIANT(&nd);
4325 NDFREE(&nd, NDF_ONLY_PNBUF);
4327 bzero(&fh, sizeof(fh));
4328 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
4329 error = VOP_VPTOFH(vp, &fh.fh_fid);
4331 VFS_UNLOCK_GIANT(vfslocked);
4334 error = copyout(&fh, uap->fhp, sizeof (fh));
4339 * syscall for the rpc.lockd to use to translate a NFS file handle into an
4342 * warning: do not remove the priv_check() call or this becomes one giant
4345 #ifndef _SYS_SYSPROTO_H_
4346 struct fhopen_args {
4347 const struct fhandle *u_fhp;
4354 struct fhopen_args /* {
4355 const struct fhandle *u_fhp;
4359 struct proc *p = td->td_proc;
4364 struct vattr *vap = &vat;
4367 register struct filedesc *fdp = p->p_fd;
4368 int fmode, error, type;
4374 error = priv_check(td, PRIV_VFS_FHOPEN);
4377 fmode = FFLAGS(uap->flags);
4378 /* why not allow a non-read/write open for our lockd? */
4379 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
4381 error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
4384 /* find the mount point */
4385 mp = vfs_busyfs(&fhp.fh_fsid);
4388 vfslocked = VFS_LOCK_GIANT(mp);
4389 /* now give me my vnode, it gets returned to me locked */
4390 error = VFS_FHTOVP(mp, &fhp.fh_fid, &vp);
4395 * from now on we have to make sure not
4396 * to forget about the vnode
4397 * any error that causes an abort must vput(vp)
4398 * just set error = err and 'goto bad;'.
4404 if (vp->v_type == VLNK) {
4408 if (vp->v_type == VSOCK) {
4413 if (fmode & (FWRITE | O_TRUNC)) {
4414 if (vp->v_type == VDIR) {
4418 error = vn_writechk(vp);
4425 if ((fmode & O_APPEND) && (fmode & FWRITE))
4428 error = mac_vnode_check_open(td->td_ucred, vp, accmode);
4433 error = VOP_ACCESS(vp, accmode, td->td_ucred, td);
4437 if (fmode & O_TRUNC) {
4439 VOP_UNLOCK(vp, 0); /* XXX */
4440 if ((error = vn_start_write(NULL, &mp, V_WAIT | PCATCH)) != 0) {
4445 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); /* XXX */
4449 * We don't yet have fp->f_cred, so use td->td_ucred, which
4452 error = mac_vnode_check_write(td->td_ucred, td->td_ucred, vp);
4457 error = VOP_SETATTR(vp, vap, td->td_ucred);
4461 vn_finished_write(mp);
4465 error = VOP_OPEN(vp, fmode, td->td_ucred, td, NULL);
4473 * end of vn_open code
4476 if ((error = fallocf(td, &nfp, &indx, fmode)) != 0) {
4481 /* An extra reference on `nfp' has been held for us by falloc(). */
4484 finit(nfp, fmode & FMASK, DTYPE_VNODE, vp, &vnops);
4485 if (fmode & (O_EXLOCK | O_SHLOCK)) {
4486 lf.l_whence = SEEK_SET;
4489 if (fmode & O_EXLOCK)
4490 lf.l_type = F_WRLCK;
4492 lf.l_type = F_RDLCK;
4494 if ((fmode & FNONBLOCK) == 0)
4497 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf,
4500 * The lock request failed. Normally close the
4501 * descriptor but handle the case where someone might
4502 * have dup()d or close()d it when we weren't looking.
4504 fdclose(fdp, fp, indx, td);
4507 * release our private reference
4512 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
4513 atomic_set_int(&fp->f_flag, FHASLOCK);
4518 VFS_UNLOCK_GIANT(vfslocked);
4519 td->td_retval[0] = indx;
4525 VFS_UNLOCK_GIANT(vfslocked);
4530 * Stat an (NFS) file handle.
4532 #ifndef _SYS_SYSPROTO_H_
4533 struct fhstat_args {
4534 struct fhandle *u_fhp;
4541 register struct fhstat_args /* {
4542 struct fhandle *u_fhp;
4553 error = priv_check(td, PRIV_VFS_FHSTAT);
4556 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4559 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4561 vfslocked = VFS_LOCK_GIANT(mp);
4562 error = VFS_FHTOVP(mp, &fh.fh_fid, &vp);
4565 VFS_UNLOCK_GIANT(vfslocked);
4568 error = vn_stat(vp, &sb, td->td_ucred, NOCRED, td);
4570 VFS_UNLOCK_GIANT(vfslocked);
4573 error = copyout(&sb, uap->sb, sizeof(sb));
4578 * Implement fstatfs() for (NFS) file handles.
4580 #ifndef _SYS_SYSPROTO_H_
4581 struct fhstatfs_args {
4582 struct fhandle *u_fhp;
4589 struct fhstatfs_args /* {
4590 struct fhandle *u_fhp;
4598 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4601 error = kern_fhstatfs(td, fh, &sf);
4604 return (copyout(&sf, uap->buf, sizeof(sf)));
4608 kern_fhstatfs(struct thread *td, fhandle_t fh, struct statfs *buf)
4616 error = priv_check(td, PRIV_VFS_FHSTATFS);
4619 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4621 vfslocked = VFS_LOCK_GIANT(mp);
4622 error = VFS_FHTOVP(mp, &fh.fh_fid, &vp);
4625 VFS_UNLOCK_GIANT(vfslocked);
4629 error = prison_canseemount(td->td_ucred, mp);
4633 error = mac_mount_check_stat(td->td_ucred, mp);
4638 * Set these in case the underlying filesystem fails to do so.
4641 sp->f_version = STATFS_VERSION;
4642 sp->f_namemax = NAME_MAX;
4643 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
4644 error = VFS_STATFS(mp, sp);
4649 VFS_UNLOCK_GIANT(vfslocked);
4654 kern_posix_fallocate(struct thread *td, int fd, off_t offset, off_t len)
4659 off_t olen, ooffset;
4660 int error, vfslocked;
4664 error = fget(td, fd, &fp);
4668 switch (fp->f_type) {
4679 if ((fp->f_flag & FWRITE) == 0) {
4684 if (vp->v_type != VREG) {
4688 if (offset < 0 || len <= 0) {
4692 /* Check for wrap. */
4693 if (offset > OFF_MAX - len) {
4698 /* Allocating blocks may take a long time, so iterate. */
4704 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
4706 error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
4708 VFS_UNLOCK_GIANT(vfslocked);
4711 error = vn_lock(vp, LK_EXCLUSIVE);
4713 vn_finished_write(mp);
4714 VFS_UNLOCK_GIANT(vfslocked);
4718 error = mac_vnode_check_write(td->td_ucred, fp->f_cred, vp);
4721 error = VOP_ALLOCATE(vp, &offset, &len);
4723 vn_finished_write(mp);
4724 VFS_UNLOCK_GIANT(vfslocked);
4726 if (olen + ooffset != offset + len) {
4727 panic("offset + len changed from %jx/%jx to %jx/%jx",
4728 ooffset, olen, offset, len);
4730 if (error != 0 || len == 0)
4732 KASSERT(olen > len, ("Iteration did not make progress?"));
4742 posix_fallocate(struct thread *td, struct posix_fallocate_args *uap)
4745 return (kern_posix_fallocate(td, uap->fd, uap->offset, uap->len));
4749 * Unlike madvise(2), we do not make a best effort to remember every
4750 * possible caching hint. Instead, we remember the last setting with
4751 * the exception that we will allow POSIX_FADV_NORMAL to adjust the
4752 * region of any current setting.
4755 kern_posix_fadvise(struct thread *td, int fd, off_t offset, off_t len,
4758 struct fadvise_info *fa, *new;
4764 if (offset < 0 || len < 0 || offset > OFF_MAX - len)
4767 case POSIX_FADV_SEQUENTIAL:
4768 case POSIX_FADV_RANDOM:
4769 case POSIX_FADV_NOREUSE:
4770 new = malloc(sizeof(*fa), M_FADVISE, M_WAITOK);
4772 case POSIX_FADV_NORMAL:
4773 case POSIX_FADV_WILLNEED:
4774 case POSIX_FADV_DONTNEED:
4780 error = fget(td, fd, &fp);
4784 switch (fp->f_type) {
4796 if (vp->v_type != VREG) {
4803 end = offset + len - 1;
4805 case POSIX_FADV_SEQUENTIAL:
4806 case POSIX_FADV_RANDOM:
4807 case POSIX_FADV_NOREUSE:
4809 * Try to merge any existing non-standard region with
4810 * this new region if possible, otherwise create a new
4811 * non-standard region for this request.
4813 mtx_pool_lock(mtxpool_sleep, fp);
4815 if (fa != NULL && fa->fa_advice == advice &&
4816 ((fa->fa_start <= end && fa->fa_end >= offset) ||
4817 (end != OFF_MAX && fa->fa_start == end + 1) ||
4818 (fa->fa_end != OFF_MAX && fa->fa_end + 1 == offset))) {
4819 if (offset < fa->fa_start)
4820 fa->fa_start = offset;
4821 if (end > fa->fa_end)
4824 new->fa_advice = advice;
4825 new->fa_start = offset;
4827 new->fa_prevstart = 0;
4828 new->fa_prevend = 0;
4832 mtx_pool_unlock(mtxpool_sleep, fp);
4834 case POSIX_FADV_NORMAL:
4836 * If a the "normal" region overlaps with an existing
4837 * non-standard region, trim or remove the
4838 * non-standard region.
4840 mtx_pool_lock(mtxpool_sleep, fp);
4843 if (offset <= fa->fa_start && end >= fa->fa_end) {
4845 fp->f_advice = NULL;
4846 } else if (offset <= fa->fa_start &&
4847 end >= fa->fa_start)
4848 fa->fa_start = end + 1;
4849 else if (offset <= fa->fa_end && end >= fa->fa_end)
4850 fa->fa_end = offset - 1;
4851 else if (offset >= fa->fa_start && end <= fa->fa_end) {
4853 * If the "normal" region is a middle
4854 * portion of the existing
4855 * non-standard region, just remove
4856 * the whole thing rather than picking
4857 * one side or the other to
4861 fp->f_advice = NULL;
4864 mtx_pool_unlock(mtxpool_sleep, fp);
4866 case POSIX_FADV_WILLNEED:
4867 case POSIX_FADV_DONTNEED:
4868 error = VOP_ADVISE(vp, offset, end, advice);
4874 free(new, M_FADVISE);
4879 posix_fadvise(struct thread *td, struct posix_fadvise_args *uap)
4882 return (kern_posix_fadvise(td, uap->fd, uap->offset, uap->len,