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 SDT_PROVIDER_DEFINE(vfs);
88 SDT_PROBE_DEFINE(vfs, , stat, mode, mode);
89 SDT_PROBE_ARGTYPE(vfs, , stat, mode, 0, "char *");
90 SDT_PROBE_ARGTYPE(vfs, , stat, mode, 1, "int");
91 SDT_PROBE_DEFINE(vfs, , stat, reg, reg);
92 SDT_PROBE_ARGTYPE(vfs, , stat, reg, 0, "char *");
93 SDT_PROBE_ARGTYPE(vfs, , stat, reg, 1, "int");
95 static int chroot_refuse_vdir_fds(struct filedesc *fdp);
96 static int getutimes(const struct timeval *, enum uio_seg, struct timespec *);
97 static int setfown(struct thread *td, struct vnode *, uid_t, gid_t);
98 static int setfmode(struct thread *td, struct vnode *, int);
99 static int setfflags(struct thread *td, struct vnode *, int);
100 static int setutimes(struct thread *td, struct vnode *,
101 const struct timespec *, int, int);
102 static int vn_access(struct vnode *vp, int user_flags, struct ucred *cred,
106 * The module initialization routine for POSIX asynchronous I/O will
107 * set this to the version of AIO that it implements. (Zero means
108 * that it is not implemented.) This value is used here by pathconf()
109 * and in kern_descrip.c by fpathconf().
111 int async_io_version;
114 static int syncprt = 0;
115 SYSCTL_INT(_debug, OID_AUTO, syncprt, CTLFLAG_RW, &syncprt, 0, "");
119 * Sync each mounted filesystem.
121 #ifndef _SYS_SYSPROTO_H_
130 struct sync_args *uap;
132 struct mount *mp, *nmp;
135 mtx_lock(&mountlist_mtx);
136 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
137 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK)) {
138 nmp = TAILQ_NEXT(mp, mnt_list);
141 vfslocked = VFS_LOCK_GIANT(mp);
142 if ((mp->mnt_flag & MNT_RDONLY) == 0 &&
143 vn_start_write(NULL, &mp, V_NOWAIT) == 0) {
146 mp->mnt_kern_flag &= ~MNTK_ASYNC;
148 vfs_msync(mp, MNT_NOWAIT);
149 VFS_SYNC(mp, MNT_NOWAIT);
152 if ((mp->mnt_flag & MNT_ASYNC) != 0 &&
153 mp->mnt_noasync == 0)
154 mp->mnt_kern_flag |= MNTK_ASYNC;
156 vn_finished_write(mp);
158 VFS_UNLOCK_GIANT(vfslocked);
159 mtx_lock(&mountlist_mtx);
160 nmp = TAILQ_NEXT(mp, mnt_list);
163 mtx_unlock(&mountlist_mtx);
168 * Change filesystem quotas.
170 #ifndef _SYS_SYSPROTO_H_
171 struct quotactl_args {
181 register struct quotactl_args /* {
193 AUDIT_ARG_CMD(uap->cmd);
194 AUDIT_ARG_UID(uap->uid);
195 if (!prison_allow(td->td_ucred, PR_ALLOW_QUOTAS))
197 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | MPSAFE | AUDITVNODE1,
198 UIO_USERSPACE, uap->path, td);
199 if ((error = namei(&nd)) != 0)
201 vfslocked = NDHASGIANT(&nd);
202 NDFREE(&nd, NDF_ONLY_PNBUF);
203 mp = nd.ni_vp->v_mount;
206 error = vfs_busy(mp, 0);
209 VFS_UNLOCK_GIANT(vfslocked);
212 error = VFS_QUOTACTL(mp, uap->cmd, uap->uid, uap->arg);
214 VFS_UNLOCK_GIANT(vfslocked);
219 * Used by statfs conversion routines to scale the block size up if
220 * necessary so that all of the block counts are <= 'max_size'. Note
221 * that 'max_size' should be a bitmask, i.e. 2^n - 1 for some non-zero
225 statfs_scale_blocks(struct statfs *sf, long max_size)
230 KASSERT(powerof2(max_size + 1), ("%s: invalid max_size", __func__));
233 * Attempt to scale the block counts to give a more accurate
234 * overview to userland of the ratio of free space to used
235 * space. To do this, find the largest block count and compute
236 * a divisor that lets it fit into a signed integer <= max_size.
238 if (sf->f_bavail < 0)
239 count = -sf->f_bavail;
241 count = sf->f_bavail;
242 count = MAX(sf->f_blocks, MAX(sf->f_bfree, count));
243 if (count <= max_size)
246 count >>= flsl(max_size);
253 sf->f_bsize <<= shift;
254 sf->f_blocks >>= shift;
255 sf->f_bfree >>= shift;
256 sf->f_bavail >>= shift;
260 * Get filesystem statistics.
262 #ifndef _SYS_SYSPROTO_H_
271 register struct statfs_args /* {
279 error = kern_statfs(td, uap->path, UIO_USERSPACE, &sf);
281 error = copyout(&sf, uap->buf, sizeof(sf));
286 kern_statfs(struct thread *td, char *path, enum uio_seg pathseg,
290 struct statfs *sp, sb;
295 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | MPSAFE |
296 AUDITVNODE1, pathseg, path, td);
300 vfslocked = NDHASGIANT(&nd);
301 mp = nd.ni_vp->v_mount;
303 NDFREE(&nd, NDF_ONLY_PNBUF);
305 error = vfs_busy(mp, 0);
308 VFS_UNLOCK_GIANT(vfslocked);
312 error = mac_mount_check_stat(td->td_ucred, mp);
317 * Set these in case the underlying filesystem fails to do so.
320 sp->f_version = STATFS_VERSION;
321 sp->f_namemax = NAME_MAX;
322 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
323 error = VFS_STATFS(mp, sp);
326 if (priv_check(td, PRIV_VFS_GENERATION)) {
327 bcopy(sp, &sb, sizeof(sb));
328 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
329 prison_enforce_statfs(td->td_ucred, mp, &sb);
335 VFS_UNLOCK_GIANT(vfslocked);
340 * Get filesystem statistics.
342 #ifndef _SYS_SYSPROTO_H_
343 struct fstatfs_args {
351 register struct fstatfs_args /* {
359 error = kern_fstatfs(td, uap->fd, &sf);
361 error = copyout(&sf, uap->buf, sizeof(sf));
366 kern_fstatfs(struct thread *td, int fd, struct statfs *buf)
370 struct statfs *sp, sb;
376 error = getvnode(td->td_proc->p_fd, fd, &fp);
380 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
381 vn_lock(vp, LK_SHARED | LK_RETRY);
383 AUDIT_ARG_VNODE1(vp);
394 error = vfs_busy(mp, 0);
397 VFS_UNLOCK_GIANT(vfslocked);
401 error = mac_mount_check_stat(td->td_ucred, mp);
406 * Set these in case the underlying filesystem fails to do so.
409 sp->f_version = STATFS_VERSION;
410 sp->f_namemax = NAME_MAX;
411 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
412 error = VFS_STATFS(mp, sp);
415 if (priv_check(td, PRIV_VFS_GENERATION)) {
416 bcopy(sp, &sb, sizeof(sb));
417 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
418 prison_enforce_statfs(td->td_ucred, mp, &sb);
425 VFS_UNLOCK_GIANT(vfslocked);
430 * Get statistics on all filesystems.
432 #ifndef _SYS_SYSPROTO_H_
433 struct getfsstat_args {
442 register struct getfsstat_args /* {
449 return (kern_getfsstat(td, &uap->buf, uap->bufsize, UIO_USERSPACE,
454 * If (bufsize > 0 && bufseg == UIO_SYSSPACE)
455 * The caller is responsible for freeing memory which will be allocated
459 kern_getfsstat(struct thread *td, struct statfs **buf, size_t bufsize,
460 enum uio_seg bufseg, int flags)
462 struct mount *mp, *nmp;
463 struct statfs *sfsp, *sp, sb;
464 size_t count, maxcount;
468 maxcount = bufsize / sizeof(struct statfs);
471 else if (bufseg == UIO_USERSPACE)
473 else /* if (bufseg == UIO_SYSSPACE) */ {
475 mtx_lock(&mountlist_mtx);
476 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
479 mtx_unlock(&mountlist_mtx);
480 if (maxcount > count)
482 sfsp = *buf = malloc(maxcount * sizeof(struct statfs), M_TEMP,
486 mtx_lock(&mountlist_mtx);
487 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
488 if (prison_canseemount(td->td_ucred, mp) != 0) {
489 nmp = TAILQ_NEXT(mp, mnt_list);
493 if (mac_mount_check_stat(td->td_ucred, mp) != 0) {
494 nmp = TAILQ_NEXT(mp, mnt_list);
498 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK)) {
499 nmp = TAILQ_NEXT(mp, mnt_list);
502 vfslocked = VFS_LOCK_GIANT(mp);
503 if (sfsp && count < maxcount) {
506 * Set these in case the underlying filesystem
509 sp->f_version = STATFS_VERSION;
510 sp->f_namemax = NAME_MAX;
511 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
513 * If MNT_NOWAIT or MNT_LAZY is specified, do not
514 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
515 * overrides MNT_WAIT.
517 if (((flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
518 (flags & MNT_WAIT)) &&
519 (error = VFS_STATFS(mp, sp))) {
520 VFS_UNLOCK_GIANT(vfslocked);
521 mtx_lock(&mountlist_mtx);
522 nmp = TAILQ_NEXT(mp, mnt_list);
526 if (priv_check(td, PRIV_VFS_GENERATION)) {
527 bcopy(sp, &sb, sizeof(sb));
528 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
529 prison_enforce_statfs(td->td_ucred, mp, &sb);
532 if (bufseg == UIO_SYSSPACE)
533 bcopy(sp, sfsp, sizeof(*sp));
534 else /* if (bufseg == UIO_USERSPACE) */ {
535 error = copyout(sp, sfsp, sizeof(*sp));
538 VFS_UNLOCK_GIANT(vfslocked);
544 VFS_UNLOCK_GIANT(vfslocked);
546 mtx_lock(&mountlist_mtx);
547 nmp = TAILQ_NEXT(mp, mnt_list);
550 mtx_unlock(&mountlist_mtx);
551 if (sfsp && count > maxcount)
552 td->td_retval[0] = maxcount;
554 td->td_retval[0] = count;
558 #ifdef COMPAT_FREEBSD4
560 * Get old format filesystem statistics.
562 static void cvtstatfs(struct statfs *, struct ostatfs *);
564 #ifndef _SYS_SYSPROTO_H_
565 struct freebsd4_statfs_args {
571 freebsd4_statfs(td, uap)
573 struct freebsd4_statfs_args /* {
582 error = kern_statfs(td, uap->path, UIO_USERSPACE, &sf);
585 cvtstatfs(&sf, &osb);
586 return (copyout(&osb, uap->buf, sizeof(osb)));
590 * Get filesystem statistics.
592 #ifndef _SYS_SYSPROTO_H_
593 struct freebsd4_fstatfs_args {
599 freebsd4_fstatfs(td, uap)
601 struct freebsd4_fstatfs_args /* {
610 error = kern_fstatfs(td, uap->fd, &sf);
613 cvtstatfs(&sf, &osb);
614 return (copyout(&osb, uap->buf, sizeof(osb)));
618 * Get statistics on all filesystems.
620 #ifndef _SYS_SYSPROTO_H_
621 struct freebsd4_getfsstat_args {
628 freebsd4_getfsstat(td, uap)
630 register struct freebsd4_getfsstat_args /* {
636 struct statfs *buf, *sp;
641 count = uap->bufsize / sizeof(struct ostatfs);
642 size = count * sizeof(struct statfs);
643 error = kern_getfsstat(td, &buf, size, UIO_SYSSPACE, uap->flags);
645 count = td->td_retval[0];
647 while (count > 0 && error == 0) {
649 error = copyout(&osb, uap->buf, sizeof(osb));
660 * Implement fstatfs() for (NFS) file handles.
662 #ifndef _SYS_SYSPROTO_H_
663 struct freebsd4_fhstatfs_args {
664 struct fhandle *u_fhp;
669 freebsd4_fhstatfs(td, uap)
671 struct freebsd4_fhstatfs_args /* {
672 struct fhandle *u_fhp;
681 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
684 error = kern_fhstatfs(td, fh, &sf);
687 cvtstatfs(&sf, &osb);
688 return (copyout(&osb, uap->buf, sizeof(osb)));
692 * Convert a new format statfs structure to an old format statfs structure.
700 statfs_scale_blocks(nsp, LONG_MAX);
701 bzero(osp, sizeof(*osp));
702 osp->f_bsize = nsp->f_bsize;
703 osp->f_iosize = MIN(nsp->f_iosize, LONG_MAX);
704 osp->f_blocks = nsp->f_blocks;
705 osp->f_bfree = nsp->f_bfree;
706 osp->f_bavail = nsp->f_bavail;
707 osp->f_files = MIN(nsp->f_files, LONG_MAX);
708 osp->f_ffree = MIN(nsp->f_ffree, LONG_MAX);
709 osp->f_owner = nsp->f_owner;
710 osp->f_type = nsp->f_type;
711 osp->f_flags = nsp->f_flags;
712 osp->f_syncwrites = MIN(nsp->f_syncwrites, LONG_MAX);
713 osp->f_asyncwrites = MIN(nsp->f_asyncwrites, LONG_MAX);
714 osp->f_syncreads = MIN(nsp->f_syncreads, LONG_MAX);
715 osp->f_asyncreads = MIN(nsp->f_asyncreads, LONG_MAX);
716 strlcpy(osp->f_fstypename, nsp->f_fstypename,
717 MIN(MFSNAMELEN, OMFSNAMELEN));
718 strlcpy(osp->f_mntonname, nsp->f_mntonname,
719 MIN(MNAMELEN, OMNAMELEN));
720 strlcpy(osp->f_mntfromname, nsp->f_mntfromname,
721 MIN(MNAMELEN, OMNAMELEN));
722 osp->f_fsid = nsp->f_fsid;
724 #endif /* COMPAT_FREEBSD4 */
727 * Change current working directory to a given file descriptor.
729 #ifndef _SYS_SYSPROTO_H_
737 struct fchdir_args /* {
741 register struct filedesc *fdp = td->td_proc->p_fd;
742 struct vnode *vp, *tdp, *vpold;
748 AUDIT_ARG_FD(uap->fd);
749 if ((error = getvnode(fdp, uap->fd, &fp)) != 0)
754 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
755 vn_lock(vp, LK_SHARED | LK_RETRY);
756 AUDIT_ARG_VNODE1(vp);
757 error = change_dir(vp, td);
758 while (!error && (mp = vp->v_mountedhere) != NULL) {
762 tvfslocked = VFS_LOCK_GIANT(mp);
763 error = VFS_ROOT(mp, LK_SHARED, &tdp);
766 VFS_UNLOCK_GIANT(tvfslocked);
770 VFS_UNLOCK_GIANT(vfslocked);
772 vfslocked = tvfslocked;
776 VFS_UNLOCK_GIANT(vfslocked);
780 VFS_UNLOCK_GIANT(vfslocked);
782 vpold = fdp->fd_cdir;
784 FILEDESC_XUNLOCK(fdp);
785 vfslocked = VFS_LOCK_GIANT(vpold->v_mount);
787 VFS_UNLOCK_GIANT(vfslocked);
792 * Change current working directory (``.'').
794 #ifndef _SYS_SYSPROTO_H_
802 struct chdir_args /* {
807 return (kern_chdir(td, uap->path, UIO_USERSPACE));
811 kern_chdir(struct thread *td, char *path, enum uio_seg pathseg)
813 register struct filedesc *fdp = td->td_proc->p_fd;
819 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1 |
820 MPSAFE, pathseg, path, td);
821 if ((error = namei(&nd)) != 0)
823 vfslocked = NDHASGIANT(&nd);
824 if ((error = change_dir(nd.ni_vp, td)) != 0) {
826 VFS_UNLOCK_GIANT(vfslocked);
827 NDFREE(&nd, NDF_ONLY_PNBUF);
830 VOP_UNLOCK(nd.ni_vp, 0);
831 VFS_UNLOCK_GIANT(vfslocked);
832 NDFREE(&nd, NDF_ONLY_PNBUF);
835 fdp->fd_cdir = nd.ni_vp;
836 FILEDESC_XUNLOCK(fdp);
837 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
839 VFS_UNLOCK_GIANT(vfslocked);
844 * Helper function for raised chroot(2) security function: Refuse if
845 * any filedescriptors are open directories.
848 chroot_refuse_vdir_fds(fdp)
849 struct filedesc *fdp;
855 FILEDESC_LOCK_ASSERT(fdp);
857 for (fd = 0; fd < fdp->fd_nfiles ; fd++) {
858 fp = fget_locked(fdp, fd);
861 if (fp->f_type == DTYPE_VNODE) {
863 if (vp->v_type == VDIR)
871 * This sysctl determines if we will allow a process to chroot(2) if it
872 * has a directory open:
873 * 0: disallowed for all processes.
874 * 1: allowed for processes that were not already chroot(2)'ed.
875 * 2: allowed for all processes.
878 static int chroot_allow_open_directories = 1;
880 SYSCTL_INT(_kern, OID_AUTO, chroot_allow_open_directories, CTLFLAG_RW,
881 &chroot_allow_open_directories, 0, "");
884 * Change notion of root (``/'') directory.
886 #ifndef _SYS_SYSPROTO_H_
894 struct chroot_args /* {
902 error = priv_check(td, PRIV_VFS_CHROOT);
905 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | MPSAFE |
906 AUDITVNODE1, UIO_USERSPACE, uap->path, td);
910 vfslocked = NDHASGIANT(&nd);
911 if ((error = change_dir(nd.ni_vp, td)) != 0)
914 if ((error = mac_vnode_check_chroot(td->td_ucred, nd.ni_vp)))
917 VOP_UNLOCK(nd.ni_vp, 0);
918 error = change_root(nd.ni_vp, td);
920 VFS_UNLOCK_GIANT(vfslocked);
921 NDFREE(&nd, NDF_ONLY_PNBUF);
925 VFS_UNLOCK_GIANT(vfslocked);
927 NDFREE(&nd, NDF_ONLY_PNBUF);
932 * Common routine for chroot and chdir. Callers must provide a locked vnode
942 ASSERT_VOP_LOCKED(vp, "change_dir(): vp not locked");
943 if (vp->v_type != VDIR)
946 error = mac_vnode_check_chdir(td->td_ucred, vp);
950 error = VOP_ACCESS(vp, VEXEC, td->td_ucred, td);
955 * Common routine for kern_chroot() and jail_attach(). The caller is
956 * responsible for invoking priv_check() and mac_vnode_check_chroot() to
957 * authorize this operation.
964 struct filedesc *fdp;
969 VFS_ASSERT_GIANT(vp->v_mount);
970 fdp = td->td_proc->p_fd;
972 if (chroot_allow_open_directories == 0 ||
973 (chroot_allow_open_directories == 1 && fdp->fd_rdir != rootvnode)) {
974 error = chroot_refuse_vdir_fds(fdp);
976 FILEDESC_XUNLOCK(fdp);
980 oldvp = fdp->fd_rdir;
987 FILEDESC_XUNLOCK(fdp);
988 vfslocked = VFS_LOCK_GIANT(oldvp->v_mount);
990 VFS_UNLOCK_GIANT(vfslocked);
995 * Check permissions, allocate an open file structure, and call the device
996 * open routine if any.
998 #ifndef _SYS_SYSPROTO_H_
1008 register struct open_args /* {
1015 return (kern_open(td, uap->path, UIO_USERSPACE, uap->flags, uap->mode));
1018 #ifndef _SYS_SYSPROTO_H_
1019 struct openat_args {
1027 openat(struct thread *td, struct openat_args *uap)
1030 return (kern_openat(td, uap->fd, uap->path, UIO_USERSPACE, uap->flag,
1035 kern_open(struct thread *td, char *path, enum uio_seg pathseg, int flags,
1039 return (kern_openat(td, AT_FDCWD, path, pathseg, flags, mode));
1043 kern_openat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1044 int flags, int mode)
1046 struct proc *p = td->td_proc;
1047 struct filedesc *fdp = p->p_fd;
1052 int type, indx, error;
1054 struct nameidata nd;
1057 AUDIT_ARG_FFLAGS(flags);
1058 AUDIT_ARG_MODE(mode);
1059 /* XXX: audit dirfd */
1061 * Only one of the O_EXEC, O_RDONLY, O_WRONLY and O_RDWR may
1064 if (flags & O_EXEC) {
1065 if (flags & O_ACCMODE)
1067 } else if ((flags & O_ACCMODE) == O_ACCMODE)
1070 flags = FFLAGS(flags);
1072 error = fallocf(td, &nfp, &indx, flags);
1075 /* An extra reference on `nfp' has been held for us by falloc(). */
1077 /* Set the flags early so the finit in devfs can pick them up. */
1078 fp->f_flag = flags & FMASK;
1079 cmode = ((mode &~ fdp->fd_cmask) & ALLPERMS) &~ S_ISTXT;
1080 NDINIT_AT(&nd, LOOKUP, FOLLOW | AUDITVNODE1 | MPSAFE, pathseg, path, fd,
1082 td->td_dupfd = -1; /* XXX check for fdopen */
1083 error = vn_open(&nd, &flags, cmode, fp);
1086 * If the vn_open replaced the method vector, something
1087 * wonderous happened deep below and we just pass it up
1088 * pretending we know what we do.
1090 if (error == ENXIO && fp->f_ops != &badfileops) {
1092 td->td_retval[0] = indx;
1097 * handle special fdopen() case. bleh. dupfdopen() is
1098 * responsible for dropping the old contents of ofiles[indx]
1101 if ((error == ENODEV || error == ENXIO) &&
1102 td->td_dupfd >= 0 && /* XXX from fdopen */
1104 dupfdopen(td, fdp, indx, td->td_dupfd, flags, error)) == 0) {
1105 td->td_retval[0] = indx;
1110 * Clean up the descriptor, but only if another thread hadn't
1111 * replaced or closed it.
1113 fdclose(fdp, fp, indx, td);
1116 if (error == ERESTART)
1121 vfslocked = NDHASGIANT(&nd);
1122 NDFREE(&nd, NDF_ONLY_PNBUF);
1126 * Store the vnode, for any f_type. Typically, the vnode use
1127 * count is decremented by direct call to vn_closefile() for
1128 * files that switched type in the cdevsw fdopen() method.
1132 * If the file wasn't claimed by devfs bind it to the normal
1133 * vnode operations here.
1135 if (fp->f_ops == &badfileops) {
1136 KASSERT(vp->v_type != VFIFO, ("Unexpected fifo."));
1138 finit(fp, flags & FMASK, DTYPE_VNODE, vp, &vnops);
1142 if (fp->f_type == DTYPE_VNODE && (flags & (O_EXLOCK | O_SHLOCK)) != 0) {
1143 lf.l_whence = SEEK_SET;
1146 if (flags & O_EXLOCK)
1147 lf.l_type = F_WRLCK;
1149 lf.l_type = F_RDLCK;
1151 if ((flags & FNONBLOCK) == 0)
1153 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf,
1156 atomic_set_int(&fp->f_flag, FHASLOCK);
1158 if (flags & O_TRUNC) {
1159 error = fo_truncate(fp, 0, td->td_ucred, td);
1163 VFS_UNLOCK_GIANT(vfslocked);
1165 * Release our private reference, leaving the one associated with
1166 * the descriptor table intact.
1169 td->td_retval[0] = indx;
1172 VFS_UNLOCK_GIANT(vfslocked);
1173 fdclose(fdp, fp, indx, td);
1182 #ifndef _SYS_SYSPROTO_H_
1183 struct ocreat_args {
1191 register struct ocreat_args /* {
1197 return (kern_open(td, uap->path, UIO_USERSPACE,
1198 O_WRONLY | O_CREAT | O_TRUNC, uap->mode));
1200 #endif /* COMPAT_43 */
1203 * Create a special file.
1205 #ifndef _SYS_SYSPROTO_H_
1215 register struct mknod_args /* {
1222 return (kern_mknod(td, uap->path, UIO_USERSPACE, uap->mode, uap->dev));
1225 #ifndef _SYS_SYSPROTO_H_
1226 struct mknodat_args {
1234 mknodat(struct thread *td, struct mknodat_args *uap)
1237 return (kern_mknodat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode,
1242 kern_mknod(struct thread *td, char *path, enum uio_seg pathseg, int mode,
1246 return (kern_mknodat(td, AT_FDCWD, path, pathseg, mode, dev));
1250 kern_mknodat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1258 struct nameidata nd;
1261 AUDIT_ARG_MODE(mode);
1263 switch (mode & S_IFMT) {
1266 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
1269 error = priv_check(td, PRIV_VFS_MKNOD_BAD);
1272 error = priv_check(td, PRIV_VFS_MKNOD_WHT);
1276 return (kern_mkfifoat(td, fd, path, pathseg, mode));
1286 NDINIT_AT(&nd, CREATE, LOCKPARENT | SAVENAME | MPSAFE | AUDITVNODE1,
1287 pathseg, path, fd, td);
1288 if ((error = namei(&nd)) != 0)
1290 vfslocked = NDHASGIANT(&nd);
1293 NDFREE(&nd, NDF_ONLY_PNBUF);
1294 if (vp == nd.ni_dvp)
1299 VFS_UNLOCK_GIANT(vfslocked);
1303 vattr.va_mode = (mode & ALLPERMS) &
1304 ~td->td_proc->p_fd->fd_cmask;
1305 vattr.va_rdev = dev;
1308 switch (mode & S_IFMT) {
1309 case S_IFMT: /* used by badsect to flag bad sectors */
1310 vattr.va_type = VBAD;
1313 vattr.va_type = VCHR;
1316 vattr.va_type = VBLK;
1322 panic("kern_mknod: invalid mode");
1325 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1326 NDFREE(&nd, NDF_ONLY_PNBUF);
1328 VFS_UNLOCK_GIANT(vfslocked);
1329 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1334 if (error == 0 && !whiteout)
1335 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp,
1336 &nd.ni_cnd, &vattr);
1340 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, CREATE);
1342 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp,
1343 &nd.ni_cnd, &vattr);
1348 NDFREE(&nd, NDF_ONLY_PNBUF);
1350 vn_finished_write(mp);
1351 VFS_UNLOCK_GIANT(vfslocked);
1356 * Create a named pipe.
1358 #ifndef _SYS_SYSPROTO_H_
1359 struct mkfifo_args {
1367 register struct mkfifo_args /* {
1373 return (kern_mkfifo(td, uap->path, UIO_USERSPACE, uap->mode));
1376 #ifndef _SYS_SYSPROTO_H_
1377 struct mkfifoat_args {
1384 mkfifoat(struct thread *td, struct mkfifoat_args *uap)
1387 return (kern_mkfifoat(td, uap->fd, uap->path, UIO_USERSPACE,
1392 kern_mkfifo(struct thread *td, char *path, enum uio_seg pathseg, int mode)
1395 return (kern_mkfifoat(td, AT_FDCWD, path, pathseg, mode));
1399 kern_mkfifoat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1405 struct nameidata nd;
1408 AUDIT_ARG_MODE(mode);
1411 NDINIT_AT(&nd, CREATE, LOCKPARENT | SAVENAME | MPSAFE | AUDITVNODE1,
1412 pathseg, path, fd, td);
1413 if ((error = namei(&nd)) != 0)
1415 vfslocked = NDHASGIANT(&nd);
1416 if (nd.ni_vp != NULL) {
1417 NDFREE(&nd, NDF_ONLY_PNBUF);
1418 if (nd.ni_vp == nd.ni_dvp)
1423 VFS_UNLOCK_GIANT(vfslocked);
1426 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1427 NDFREE(&nd, NDF_ONLY_PNBUF);
1429 VFS_UNLOCK_GIANT(vfslocked);
1430 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1435 vattr.va_type = VFIFO;
1436 vattr.va_mode = (mode & ALLPERMS) & ~td->td_proc->p_fd->fd_cmask;
1438 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1443 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
1450 vn_finished_write(mp);
1451 VFS_UNLOCK_GIANT(vfslocked);
1452 NDFREE(&nd, NDF_ONLY_PNBUF);
1457 * Make a hard file link.
1459 #ifndef _SYS_SYSPROTO_H_
1468 register struct link_args /* {
1474 return (kern_link(td, uap->path, uap->link, UIO_USERSPACE));
1477 #ifndef _SYS_SYSPROTO_H_
1478 struct linkat_args {
1487 linkat(struct thread *td, struct linkat_args *uap)
1492 if (flag & ~AT_SYMLINK_FOLLOW)
1495 return (kern_linkat(td, uap->fd1, uap->fd2, uap->path1, uap->path2,
1496 UIO_USERSPACE, (flag & AT_SYMLINK_FOLLOW) ? FOLLOW : NOFOLLOW));
1499 int hardlink_check_uid = 0;
1500 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
1501 &hardlink_check_uid, 0,
1502 "Unprivileged processes cannot create hard links to files owned by other "
1504 static int hardlink_check_gid = 0;
1505 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
1506 &hardlink_check_gid, 0,
1507 "Unprivileged processes cannot create hard links to files owned by other "
1511 can_hardlink(struct vnode *vp, struct ucred *cred)
1516 if (!hardlink_check_uid && !hardlink_check_gid)
1519 error = VOP_GETATTR(vp, &va, cred);
1523 if (hardlink_check_uid && cred->cr_uid != va.va_uid) {
1524 error = priv_check_cred(cred, PRIV_VFS_LINK, 0);
1529 if (hardlink_check_gid && !groupmember(va.va_gid, cred)) {
1530 error = priv_check_cred(cred, PRIV_VFS_LINK, 0);
1539 kern_link(struct thread *td, char *path, char *link, enum uio_seg segflg)
1542 return (kern_linkat(td, AT_FDCWD, AT_FDCWD, path,link, segflg, FOLLOW));
1546 kern_linkat(struct thread *td, int fd1, int fd2, char *path1, char *path2,
1547 enum uio_seg segflg, int follow)
1551 struct nameidata nd;
1557 NDINIT_AT(&nd, LOOKUP, follow | MPSAFE | AUDITVNODE1, segflg, path1,
1560 if ((error = namei(&nd)) != 0)
1562 vfslocked = NDHASGIANT(&nd);
1563 NDFREE(&nd, NDF_ONLY_PNBUF);
1565 if (vp->v_type == VDIR) {
1567 VFS_UNLOCK_GIANT(vfslocked);
1568 return (EPERM); /* POSIX */
1570 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0) {
1572 VFS_UNLOCK_GIANT(vfslocked);
1575 NDINIT_AT(&nd, CREATE, LOCKPARENT | SAVENAME | MPSAFE | AUDITVNODE2,
1576 segflg, path2, fd2, td);
1577 if ((error = namei(&nd)) == 0) {
1578 lvfslocked = NDHASGIANT(&nd);
1579 if (nd.ni_vp != NULL) {
1580 if (nd.ni_dvp == nd.ni_vp)
1586 } else if ((error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY))
1588 error = can_hardlink(vp, td->td_ucred);
1591 error = mac_vnode_check_link(td->td_ucred,
1592 nd.ni_dvp, vp, &nd.ni_cnd);
1595 error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
1599 NDFREE(&nd, NDF_ONLY_PNBUF);
1600 VFS_UNLOCK_GIANT(lvfslocked);
1603 vn_finished_write(mp);
1604 VFS_UNLOCK_GIANT(vfslocked);
1609 * Make a symbolic link.
1611 #ifndef _SYS_SYSPROTO_H_
1612 struct symlink_args {
1620 register struct symlink_args /* {
1626 return (kern_symlink(td, uap->path, uap->link, UIO_USERSPACE));
1629 #ifndef _SYS_SYSPROTO_H_
1630 struct symlinkat_args {
1637 symlinkat(struct thread *td, struct symlinkat_args *uap)
1640 return (kern_symlinkat(td, uap->path1, uap->fd, uap->path2,
1645 kern_symlink(struct thread *td, char *path, char *link, enum uio_seg segflg)
1648 return (kern_symlinkat(td, path, AT_FDCWD, link, segflg));
1652 kern_symlinkat(struct thread *td, char *path1, int fd, char *path2,
1653 enum uio_seg segflg)
1659 struct nameidata nd;
1662 if (segflg == UIO_SYSSPACE) {
1665 syspath = uma_zalloc(namei_zone, M_WAITOK);
1666 if ((error = copyinstr(path1, syspath, MAXPATHLEN, NULL)) != 0)
1669 AUDIT_ARG_TEXT(syspath);
1672 NDINIT_AT(&nd, CREATE, LOCKPARENT | SAVENAME | MPSAFE | AUDITVNODE1,
1673 segflg, path2, fd, td);
1674 if ((error = namei(&nd)) != 0)
1676 vfslocked = NDHASGIANT(&nd);
1678 NDFREE(&nd, NDF_ONLY_PNBUF);
1679 if (nd.ni_vp == nd.ni_dvp)
1684 VFS_UNLOCK_GIANT(vfslocked);
1688 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1689 NDFREE(&nd, NDF_ONLY_PNBUF);
1691 VFS_UNLOCK_GIANT(vfslocked);
1692 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1697 vattr.va_mode = ACCESSPERMS &~ td->td_proc->p_fd->fd_cmask;
1699 vattr.va_type = VLNK;
1700 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1705 error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr, syspath);
1711 NDFREE(&nd, NDF_ONLY_PNBUF);
1713 vn_finished_write(mp);
1714 VFS_UNLOCK_GIANT(vfslocked);
1716 if (segflg != UIO_SYSSPACE)
1717 uma_zfree(namei_zone, syspath);
1722 * Delete a whiteout from the filesystem.
1727 register struct undelete_args /* {
1733 struct nameidata nd;
1738 NDINIT(&nd, DELETE, LOCKPARENT | DOWHITEOUT | MPSAFE | AUDITVNODE1,
1739 UIO_USERSPACE, uap->path, td);
1743 vfslocked = NDHASGIANT(&nd);
1745 if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & ISWHITEOUT)) {
1746 NDFREE(&nd, NDF_ONLY_PNBUF);
1747 if (nd.ni_vp == nd.ni_dvp)
1753 VFS_UNLOCK_GIANT(vfslocked);
1756 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1757 NDFREE(&nd, NDF_ONLY_PNBUF);
1759 VFS_UNLOCK_GIANT(vfslocked);
1760 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1764 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, DELETE);
1765 NDFREE(&nd, NDF_ONLY_PNBUF);
1767 vn_finished_write(mp);
1768 VFS_UNLOCK_GIANT(vfslocked);
1773 * Delete a name from the filesystem.
1775 #ifndef _SYS_SYSPROTO_H_
1776 struct unlink_args {
1783 struct unlink_args /* {
1788 return (kern_unlink(td, uap->path, UIO_USERSPACE));
1791 #ifndef _SYS_SYSPROTO_H_
1792 struct unlinkat_args {
1799 unlinkat(struct thread *td, struct unlinkat_args *uap)
1801 int flag = uap->flag;
1803 char *path = uap->path;
1805 if (flag & ~AT_REMOVEDIR)
1808 if (flag & AT_REMOVEDIR)
1809 return (kern_rmdirat(td, fd, path, UIO_USERSPACE));
1811 return (kern_unlinkat(td, fd, path, UIO_USERSPACE));
1815 kern_unlink(struct thread *td, char *path, enum uio_seg pathseg)
1818 return (kern_unlinkat(td, AT_FDCWD, path, pathseg));
1822 kern_unlinkat(struct thread *td, int fd, char *path, enum uio_seg pathseg)
1827 struct nameidata nd;
1832 NDINIT_AT(&nd, DELETE, LOCKPARENT | LOCKLEAF | MPSAFE | AUDITVNODE1,
1833 pathseg, path, fd, td);
1834 if ((error = namei(&nd)) != 0)
1835 return (error == EINVAL ? EPERM : error);
1836 vfslocked = NDHASGIANT(&nd);
1838 if (vp->v_type == VDIR)
1839 error = EPERM; /* POSIX */
1842 * The root of a mounted filesystem cannot be deleted.
1844 * XXX: can this only be a VDIR case?
1846 if (vp->v_vflag & VV_ROOT)
1850 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1851 NDFREE(&nd, NDF_ONLY_PNBUF);
1853 if (vp == nd.ni_dvp)
1857 VFS_UNLOCK_GIANT(vfslocked);
1858 if ((error = vn_start_write(NULL, &mp,
1859 V_XSLEEP | PCATCH)) != 0)
1864 error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp,
1869 error = VOP_REMOVE(nd.ni_dvp, vp, &nd.ni_cnd);
1873 vn_finished_write(mp);
1875 NDFREE(&nd, NDF_ONLY_PNBUF);
1877 if (vp == nd.ni_dvp)
1881 VFS_UNLOCK_GIANT(vfslocked);
1886 * Reposition read/write file offset.
1888 #ifndef _SYS_SYSPROTO_H_
1899 register struct lseek_args /* {
1906 struct ucred *cred = td->td_ucred;
1914 AUDIT_ARG_FD(uap->fd);
1915 if ((error = fget(td, uap->fd, &fp)) != 0)
1917 if (!(fp->f_ops->fo_flags & DFLAG_SEEKABLE)) {
1922 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
1923 noneg = (vp->v_type != VCHR);
1924 offset = uap->offset;
1925 switch (uap->whence) {
1928 (fp->f_offset < 0 ||
1929 (offset > 0 && fp->f_offset > OFF_MAX - offset))) {
1933 offset += fp->f_offset;
1936 vn_lock(vp, LK_SHARED | LK_RETRY);
1937 error = VOP_GETATTR(vp, &vattr, cred);
1943 * If the file references a disk device, then fetch
1944 * the media size and use that to determine the ending
1947 if (vattr.va_size == 0 && vp->v_type == VCHR &&
1948 fo_ioctl(fp, DIOCGMEDIASIZE, &size, cred, td) == 0)
1949 vattr.va_size = size;
1951 (vattr.va_size > OFF_MAX ||
1952 (offset > 0 && vattr.va_size > OFF_MAX - offset))) {
1956 offset += vattr.va_size;
1961 error = fo_ioctl(fp, FIOSEEKDATA, &offset, cred, td);
1964 error = fo_ioctl(fp, FIOSEEKHOLE, &offset, cred, td);
1969 if (error == 0 && noneg && offset < 0)
1973 fp->f_offset = offset;
1974 *(off_t *)(td->td_retval) = fp->f_offset;
1977 VFS_UNLOCK_GIANT(vfslocked);
1981 #if defined(COMPAT_43)
1983 * Reposition read/write file offset.
1985 #ifndef _SYS_SYSPROTO_H_
1986 struct olseek_args {
1995 register struct olseek_args /* {
2001 struct lseek_args /* {
2009 nuap.offset = uap->offset;
2010 nuap.whence = uap->whence;
2011 return (lseek(td, &nuap));
2013 #endif /* COMPAT_43 */
2015 /* Version with the 'pad' argument */
2017 freebsd6_lseek(td, uap)
2019 register struct freebsd6_lseek_args *uap;
2021 struct lseek_args ouap;
2024 ouap.offset = uap->offset;
2025 ouap.whence = uap->whence;
2026 return (lseek(td, &ouap));
2030 * Check access permissions using passed credentials.
2033 vn_access(vp, user_flags, cred, td)
2042 /* Flags == 0 means only check for existence. */
2046 if (user_flags & R_OK)
2048 if (user_flags & W_OK)
2050 if (user_flags & X_OK)
2053 error = mac_vnode_check_access(cred, vp, accmode);
2057 if ((accmode & VWRITE) == 0 || (error = vn_writechk(vp)) == 0)
2058 error = VOP_ACCESS(vp, accmode, cred, td);
2064 * Check access permissions using "real" credentials.
2066 #ifndef _SYS_SYSPROTO_H_
2067 struct access_args {
2075 register struct access_args /* {
2081 return (kern_access(td, uap->path, UIO_USERSPACE, uap->flags));
2084 #ifndef _SYS_SYSPROTO_H_
2085 struct faccessat_args {
2093 faccessat(struct thread *td, struct faccessat_args *uap)
2096 if (uap->flag & ~AT_EACCESS)
2098 return (kern_accessat(td, uap->fd, uap->path, UIO_USERSPACE, uap->flag,
2103 kern_access(struct thread *td, char *path, enum uio_seg pathseg, int mode)
2106 return (kern_accessat(td, AT_FDCWD, path, pathseg, 0, mode));
2110 kern_accessat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2111 int flags, int mode)
2113 struct ucred *cred, *tmpcred;
2115 struct nameidata nd;
2120 * Create and modify a temporary credential instead of one that
2121 * is potentially shared.
2123 if (!(flags & AT_EACCESS)) {
2124 cred = td->td_ucred;
2125 tmpcred = crdup(cred);
2126 tmpcred->cr_uid = cred->cr_ruid;
2127 tmpcred->cr_groups[0] = cred->cr_rgid;
2128 td->td_ucred = tmpcred;
2130 cred = tmpcred = td->td_ucred;
2131 AUDIT_ARG_VALUE(mode);
2132 NDINIT_AT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | MPSAFE |
2133 AUDITVNODE1, pathseg, path, fd, td);
2134 if ((error = namei(&nd)) != 0)
2136 vfslocked = NDHASGIANT(&nd);
2139 error = vn_access(vp, mode, tmpcred, td);
2140 NDFREE(&nd, NDF_ONLY_PNBUF);
2142 VFS_UNLOCK_GIANT(vfslocked);
2144 if (!(flags & AT_EACCESS)) {
2145 td->td_ucred = cred;
2152 * Check access permissions using "effective" credentials.
2154 #ifndef _SYS_SYSPROTO_H_
2155 struct eaccess_args {
2163 register struct eaccess_args /* {
2169 return (kern_eaccess(td, uap->path, UIO_USERSPACE, uap->flags));
2173 kern_eaccess(struct thread *td, char *path, enum uio_seg pathseg, int flags)
2176 return (kern_accessat(td, AT_FDCWD, path, pathseg, AT_EACCESS, flags));
2179 #if defined(COMPAT_43)
2181 * Get file status; this version follows links.
2183 #ifndef _SYS_SYSPROTO_H_
2192 register struct ostat_args /* {
2201 error = kern_stat(td, uap->path, UIO_USERSPACE, &sb);
2205 error = copyout(&osb, uap->ub, sizeof (osb));
2210 * Get file status; this version does not follow links.
2212 #ifndef _SYS_SYSPROTO_H_
2213 struct olstat_args {
2221 register struct olstat_args /* {
2230 error = kern_lstat(td, uap->path, UIO_USERSPACE, &sb);
2234 error = copyout(&osb, uap->ub, sizeof (osb));
2239 * Convert from an old to a new stat structure.
2247 ost->st_dev = st->st_dev;
2248 ost->st_ino = st->st_ino;
2249 ost->st_mode = st->st_mode;
2250 ost->st_nlink = st->st_nlink;
2251 ost->st_uid = st->st_uid;
2252 ost->st_gid = st->st_gid;
2253 ost->st_rdev = st->st_rdev;
2254 if (st->st_size < (quad_t)1 << 32)
2255 ost->st_size = st->st_size;
2258 ost->st_atime = st->st_atime;
2259 ost->st_mtime = st->st_mtime;
2260 ost->st_ctime = st->st_ctime;
2261 ost->st_blksize = st->st_blksize;
2262 ost->st_blocks = st->st_blocks;
2263 ost->st_flags = st->st_flags;
2264 ost->st_gen = st->st_gen;
2266 #endif /* COMPAT_43 */
2269 * Get file status; this version follows links.
2271 #ifndef _SYS_SYSPROTO_H_
2280 register struct stat_args /* {
2288 error = kern_stat(td, uap->path, UIO_USERSPACE, &sb);
2290 error = copyout(&sb, uap->ub, sizeof (sb));
2294 #ifndef _SYS_SYSPROTO_H_
2295 struct fstatat_args {
2303 fstatat(struct thread *td, struct fstatat_args *uap)
2308 error = kern_statat(td, uap->flag, uap->fd, uap->path,
2309 UIO_USERSPACE, &sb);
2311 error = copyout(&sb, uap->buf, sizeof (sb));
2316 kern_stat(struct thread *td, char *path, enum uio_seg pathseg, struct stat *sbp)
2319 return (kern_statat(td, 0, AT_FDCWD, path, pathseg, sbp));
2323 kern_statat(struct thread *td, int flag, int fd, char *path,
2324 enum uio_seg pathseg, struct stat *sbp)
2327 return (kern_statat_vnhook(td, flag, fd, path, pathseg, sbp, NULL));
2331 kern_statat_vnhook(struct thread *td, int flag, int fd, char *path,
2332 enum uio_seg pathseg, struct stat *sbp,
2333 void (*hook)(struct vnode *vp, struct stat *sbp))
2335 struct nameidata nd;
2337 int error, vfslocked;
2339 if (flag & ~AT_SYMLINK_NOFOLLOW)
2342 NDINIT_AT(&nd, LOOKUP, ((flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW :
2343 FOLLOW) | LOCKSHARED | LOCKLEAF | AUDITVNODE1 | MPSAFE, pathseg,
2346 if ((error = namei(&nd)) != 0)
2348 vfslocked = NDHASGIANT(&nd);
2349 error = vn_stat(nd.ni_vp, &sb, td->td_ucred, NOCRED, td);
2351 SDT_PROBE(vfs, , stat, mode, path, sb.st_mode, 0, 0, 0);
2352 if (S_ISREG(sb.st_mode))
2353 SDT_PROBE(vfs, , stat, reg, path, pathseg, 0, 0, 0);
2354 if (__predict_false(hook != NULL))
2355 hook(nd.ni_vp, &sb);
2357 NDFREE(&nd, NDF_ONLY_PNBUF);
2359 VFS_UNLOCK_GIANT(vfslocked);
2364 if (KTRPOINT(td, KTR_STRUCT))
2371 * Get file status; this version does not follow links.
2373 #ifndef _SYS_SYSPROTO_H_
2382 register struct lstat_args /* {
2390 error = kern_lstat(td, uap->path, UIO_USERSPACE, &sb);
2392 error = copyout(&sb, uap->ub, sizeof (sb));
2397 kern_lstat(struct thread *td, char *path, enum uio_seg pathseg, struct stat *sbp)
2400 return (kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, path, pathseg,
2405 * Implementation of the NetBSD [l]stat() functions.
2412 bzero(nsb, sizeof *nsb);
2413 nsb->st_dev = sb->st_dev;
2414 nsb->st_ino = sb->st_ino;
2415 nsb->st_mode = sb->st_mode;
2416 nsb->st_nlink = sb->st_nlink;
2417 nsb->st_uid = sb->st_uid;
2418 nsb->st_gid = sb->st_gid;
2419 nsb->st_rdev = sb->st_rdev;
2420 nsb->st_atimespec = sb->st_atimespec;
2421 nsb->st_mtimespec = sb->st_mtimespec;
2422 nsb->st_ctimespec = sb->st_ctimespec;
2423 nsb->st_size = sb->st_size;
2424 nsb->st_blocks = sb->st_blocks;
2425 nsb->st_blksize = sb->st_blksize;
2426 nsb->st_flags = sb->st_flags;
2427 nsb->st_gen = sb->st_gen;
2428 nsb->st_birthtimespec = sb->st_birthtimespec;
2431 #ifndef _SYS_SYSPROTO_H_
2440 register struct nstat_args /* {
2449 error = kern_stat(td, uap->path, UIO_USERSPACE, &sb);
2452 cvtnstat(&sb, &nsb);
2453 error = copyout(&nsb, uap->ub, sizeof (nsb));
2458 * NetBSD lstat. Get file status; this version does not follow links.
2460 #ifndef _SYS_SYSPROTO_H_
2469 register struct nlstat_args /* {
2478 error = kern_lstat(td, uap->path, UIO_USERSPACE, &sb);
2481 cvtnstat(&sb, &nsb);
2482 error = copyout(&nsb, uap->ub, sizeof (nsb));
2487 * Get configurable pathname variables.
2489 #ifndef _SYS_SYSPROTO_H_
2490 struct pathconf_args {
2498 register struct pathconf_args /* {
2504 return (kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name, FOLLOW));
2507 #ifndef _SYS_SYSPROTO_H_
2508 struct lpathconf_args {
2516 register struct lpathconf_args /* {
2522 return (kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name, NOFOLLOW));
2526 kern_pathconf(struct thread *td, char *path, enum uio_seg pathseg, int name,
2529 struct nameidata nd;
2530 int error, vfslocked;
2532 NDINIT(&nd, LOOKUP, LOCKSHARED | LOCKLEAF | MPSAFE | AUDITVNODE1 |
2533 flags, pathseg, path, td);
2534 if ((error = namei(&nd)) != 0)
2536 vfslocked = NDHASGIANT(&nd);
2537 NDFREE(&nd, NDF_ONLY_PNBUF);
2539 /* If asynchronous I/O is available, it works for all files. */
2540 if (name == _PC_ASYNC_IO)
2541 td->td_retval[0] = async_io_version;
2543 error = VOP_PATHCONF(nd.ni_vp, name, td->td_retval);
2545 VFS_UNLOCK_GIANT(vfslocked);
2550 * Return target name of a symbolic link.
2552 #ifndef _SYS_SYSPROTO_H_
2553 struct readlink_args {
2562 register struct readlink_args /* {
2569 return (kern_readlink(td, uap->path, UIO_USERSPACE, uap->buf,
2570 UIO_USERSPACE, uap->count));
2572 #ifndef _SYS_SYSPROTO_H_
2573 struct readlinkat_args {
2581 readlinkat(struct thread *td, struct readlinkat_args *uap)
2584 return (kern_readlinkat(td, uap->fd, uap->path, UIO_USERSPACE,
2585 uap->buf, UIO_USERSPACE, uap->bufsize));
2589 kern_readlink(struct thread *td, char *path, enum uio_seg pathseg, char *buf,
2590 enum uio_seg bufseg, size_t count)
2593 return (kern_readlinkat(td, AT_FDCWD, path, pathseg, buf, bufseg,
2598 kern_readlinkat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2599 char *buf, enum uio_seg bufseg, size_t count)
2605 struct nameidata nd;
2608 if (count > INT_MAX)
2611 NDINIT_AT(&nd, LOOKUP, NOFOLLOW | LOCKSHARED | LOCKLEAF | MPSAFE |
2612 AUDITVNODE1, pathseg, path, fd, td);
2614 if ((error = namei(&nd)) != 0)
2616 NDFREE(&nd, NDF_ONLY_PNBUF);
2617 vfslocked = NDHASGIANT(&nd);
2620 error = mac_vnode_check_readlink(td->td_ucred, vp);
2623 VFS_UNLOCK_GIANT(vfslocked);
2627 if (vp->v_type != VLNK)
2630 aiov.iov_base = buf;
2631 aiov.iov_len = count;
2632 auio.uio_iov = &aiov;
2633 auio.uio_iovcnt = 1;
2634 auio.uio_offset = 0;
2635 auio.uio_rw = UIO_READ;
2636 auio.uio_segflg = bufseg;
2638 auio.uio_resid = count;
2639 error = VOP_READLINK(vp, &auio, td->td_ucred);
2642 VFS_UNLOCK_GIANT(vfslocked);
2643 td->td_retval[0] = count - auio.uio_resid;
2648 * Common implementation code for chflags() and fchflags().
2651 setfflags(td, vp, flags)
2661 * Prevent non-root users from setting flags on devices. When
2662 * a device is reused, users can retain ownership of the device
2663 * if they are allowed to set flags and programs assume that
2664 * chown can't fail when done as root.
2666 if (vp->v_type == VCHR || vp->v_type == VBLK) {
2667 error = priv_check(td, PRIV_VFS_CHFLAGS_DEV);
2672 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2674 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2676 vattr.va_flags = flags;
2678 error = mac_vnode_check_setflags(td->td_ucred, vp, vattr.va_flags);
2681 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
2683 vn_finished_write(mp);
2688 * Change flags of a file given a path name.
2690 #ifndef _SYS_SYSPROTO_H_
2691 struct chflags_args {
2699 register struct chflags_args /* {
2705 struct nameidata nd;
2708 AUDIT_ARG_FFLAGS(uap->flags);
2709 NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE | AUDITVNODE1, UIO_USERSPACE,
2711 if ((error = namei(&nd)) != 0)
2713 NDFREE(&nd, NDF_ONLY_PNBUF);
2714 vfslocked = NDHASGIANT(&nd);
2715 error = setfflags(td, nd.ni_vp, uap->flags);
2717 VFS_UNLOCK_GIANT(vfslocked);
2722 * Same as chflags() but doesn't follow symlinks.
2727 register struct lchflags_args /* {
2733 struct nameidata nd;
2736 AUDIT_ARG_FFLAGS(uap->flags);
2737 NDINIT(&nd, LOOKUP, NOFOLLOW | MPSAFE | AUDITVNODE1, UIO_USERSPACE,
2739 if ((error = namei(&nd)) != 0)
2741 vfslocked = NDHASGIANT(&nd);
2742 NDFREE(&nd, NDF_ONLY_PNBUF);
2743 error = setfflags(td, nd.ni_vp, uap->flags);
2745 VFS_UNLOCK_GIANT(vfslocked);
2750 * Change flags of a file given a file descriptor.
2752 #ifndef _SYS_SYSPROTO_H_
2753 struct fchflags_args {
2761 register struct fchflags_args /* {
2770 AUDIT_ARG_FD(uap->fd);
2771 AUDIT_ARG_FFLAGS(uap->flags);
2772 if ((error = getvnode(td->td_proc->p_fd, uap->fd, &fp)) != 0)
2774 vfslocked = VFS_LOCK_GIANT(fp->f_vnode->v_mount);
2776 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
2777 AUDIT_ARG_VNODE1(fp->f_vnode);
2778 VOP_UNLOCK(fp->f_vnode, 0);
2780 error = setfflags(td, fp->f_vnode, uap->flags);
2781 VFS_UNLOCK_GIANT(vfslocked);
2787 * Common implementation code for chmod(), lchmod() and fchmod().
2790 setfmode(td, vp, mode)
2799 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2801 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2803 vattr.va_mode = mode & ALLPERMS;
2805 error = mac_vnode_check_setmode(td->td_ucred, vp, vattr.va_mode);
2808 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
2810 vn_finished_write(mp);
2815 * Change mode of a file given path name.
2817 #ifndef _SYS_SYSPROTO_H_
2826 register struct chmod_args /* {
2832 return (kern_chmod(td, uap->path, UIO_USERSPACE, uap->mode));
2835 #ifndef _SYS_SYSPROTO_H_
2836 struct fchmodat_args {
2844 fchmodat(struct thread *td, struct fchmodat_args *uap)
2846 int flag = uap->flag;
2848 char *path = uap->path;
2849 mode_t mode = uap->mode;
2851 if (flag & ~AT_SYMLINK_NOFOLLOW)
2854 return (kern_fchmodat(td, fd, path, UIO_USERSPACE, mode, flag));
2858 kern_chmod(struct thread *td, char *path, enum uio_seg pathseg, int mode)
2861 return (kern_fchmodat(td, AT_FDCWD, path, pathseg, mode, 0));
2865 * Change mode of a file given path name (don't follow links.)
2867 #ifndef _SYS_SYSPROTO_H_
2868 struct lchmod_args {
2876 register struct lchmod_args /* {
2882 return (kern_fchmodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2883 uap->mode, AT_SYMLINK_NOFOLLOW));
2888 kern_fchmodat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2889 mode_t mode, int flag)
2892 struct nameidata nd;
2896 AUDIT_ARG_MODE(mode);
2897 follow = (flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
2898 NDINIT_AT(&nd, LOOKUP, follow | MPSAFE | AUDITVNODE1, pathseg, path,
2900 if ((error = namei(&nd)) != 0)
2902 vfslocked = NDHASGIANT(&nd);
2903 NDFREE(&nd, NDF_ONLY_PNBUF);
2904 error = setfmode(td, nd.ni_vp, mode);
2906 VFS_UNLOCK_GIANT(vfslocked);
2911 * Change mode of a file given a file descriptor.
2913 #ifndef _SYS_SYSPROTO_H_
2914 struct fchmod_args {
2922 register struct fchmod_args /* {
2931 AUDIT_ARG_FD(uap->fd);
2932 AUDIT_ARG_MODE(uap->mode);
2933 if ((error = getvnode(td->td_proc->p_fd, uap->fd, &fp)) != 0)
2935 vfslocked = VFS_LOCK_GIANT(fp->f_vnode->v_mount);
2937 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
2938 AUDIT_ARG_VNODE1(fp->f_vnode);
2939 VOP_UNLOCK(fp->f_vnode, 0);
2941 error = setfmode(td, fp->f_vnode, uap->mode);
2942 VFS_UNLOCK_GIANT(vfslocked);
2948 * Common implementation for chown(), lchown(), and fchown()
2951 setfown(td, vp, uid, gid)
2961 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2963 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2968 error = mac_vnode_check_setowner(td->td_ucred, vp, vattr.va_uid,
2972 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
2974 vn_finished_write(mp);
2979 * Set ownership given a path name.
2981 #ifndef _SYS_SYSPROTO_H_
2991 register struct chown_args /* {
2998 return (kern_chown(td, uap->path, UIO_USERSPACE, uap->uid, uap->gid));
3001 #ifndef _SYS_SYSPROTO_H_
3002 struct fchownat_args {
3011 fchownat(struct thread *td, struct fchownat_args *uap)
3016 if (flag & ~AT_SYMLINK_NOFOLLOW)
3019 return (kern_fchownat(td, uap->fd, uap->path, UIO_USERSPACE, uap->uid,
3020 uap->gid, uap->flag));
3024 kern_chown(struct thread *td, char *path, enum uio_seg pathseg, int uid,
3028 return (kern_fchownat(td, AT_FDCWD, path, pathseg, uid, gid, 0));
3032 kern_fchownat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
3033 int uid, int gid, int flag)
3035 struct nameidata nd;
3036 int error, vfslocked, follow;
3038 AUDIT_ARG_OWNER(uid, gid);
3039 follow = (flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
3040 NDINIT_AT(&nd, LOOKUP, follow | MPSAFE | AUDITVNODE1, pathseg, path,
3043 if ((error = namei(&nd)) != 0)
3045 vfslocked = NDHASGIANT(&nd);
3046 NDFREE(&nd, NDF_ONLY_PNBUF);
3047 error = setfown(td, nd.ni_vp, uid, gid);
3049 VFS_UNLOCK_GIANT(vfslocked);
3054 * Set ownership given a path name, do not cross symlinks.
3056 #ifndef _SYS_SYSPROTO_H_
3057 struct lchown_args {
3066 register struct lchown_args /* {
3073 return (kern_lchown(td, uap->path, UIO_USERSPACE, uap->uid, uap->gid));
3077 kern_lchown(struct thread *td, char *path, enum uio_seg pathseg, int uid,
3081 return (kern_fchownat(td, AT_FDCWD, path, pathseg, uid, gid,
3082 AT_SYMLINK_NOFOLLOW));
3086 * Set ownership given a file descriptor.
3088 #ifndef _SYS_SYSPROTO_H_
3089 struct fchown_args {
3098 register struct fchown_args /* {
3108 AUDIT_ARG_FD(uap->fd);
3109 AUDIT_ARG_OWNER(uap->uid, uap->gid);
3110 if ((error = getvnode(td->td_proc->p_fd, uap->fd, &fp)) != 0)
3112 vfslocked = VFS_LOCK_GIANT(fp->f_vnode->v_mount);
3114 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
3115 AUDIT_ARG_VNODE1(fp->f_vnode);
3116 VOP_UNLOCK(fp->f_vnode, 0);
3118 error = setfown(td, fp->f_vnode, uap->uid, uap->gid);
3119 VFS_UNLOCK_GIANT(vfslocked);
3125 * Common implementation code for utimes(), lutimes(), and futimes().
3128 getutimes(usrtvp, tvpseg, tsp)
3129 const struct timeval *usrtvp;
3130 enum uio_seg tvpseg;
3131 struct timespec *tsp;
3133 struct timeval tv[2];
3134 const struct timeval *tvp;
3137 if (usrtvp == NULL) {
3138 vfs_timestamp(&tsp[0]);
3141 if (tvpseg == UIO_SYSSPACE) {
3144 if ((error = copyin(usrtvp, tv, sizeof(tv))) != 0)
3149 if (tvp[0].tv_usec < 0 || tvp[0].tv_usec >= 1000000 ||
3150 tvp[1].tv_usec < 0 || tvp[1].tv_usec >= 1000000)
3152 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
3153 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
3159 * Common implementation code for utimes(), lutimes(), and futimes().
3162 setutimes(td, vp, ts, numtimes, nullflag)
3165 const struct timespec *ts;
3169 int error, setbirthtime;
3173 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
3175 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3177 if (numtimes < 3 && !VOP_GETATTR(vp, &vattr, td->td_ucred) &&
3178 timespeccmp(&ts[1], &vattr.va_birthtime, < ))
3181 vattr.va_atime = ts[0];
3182 vattr.va_mtime = ts[1];
3184 vattr.va_birthtime = ts[1];
3186 vattr.va_birthtime = ts[2];
3188 vattr.va_vaflags |= VA_UTIMES_NULL;
3190 error = mac_vnode_check_setutimes(td->td_ucred, vp, vattr.va_atime,
3194 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3196 vn_finished_write(mp);
3201 * Set the access and modification times of a file.
3203 #ifndef _SYS_SYSPROTO_H_
3204 struct utimes_args {
3206 struct timeval *tptr;
3212 register struct utimes_args /* {
3214 struct timeval *tptr;
3218 return (kern_utimes(td, uap->path, UIO_USERSPACE, uap->tptr,
3222 #ifndef _SYS_SYSPROTO_H_
3223 struct futimesat_args {
3226 const struct timeval * times;
3230 futimesat(struct thread *td, struct futimesat_args *uap)
3233 return (kern_utimesat(td, uap->fd, uap->path, UIO_USERSPACE,
3234 uap->times, UIO_USERSPACE));
3238 kern_utimes(struct thread *td, char *path, enum uio_seg pathseg,
3239 struct timeval *tptr, enum uio_seg tptrseg)
3242 return (kern_utimesat(td, AT_FDCWD, path, pathseg, tptr, tptrseg));
3246 kern_utimesat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
3247 struct timeval *tptr, enum uio_seg tptrseg)
3249 struct nameidata nd;
3250 struct timespec ts[2];
3251 int error, vfslocked;
3253 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3255 NDINIT_AT(&nd, LOOKUP, FOLLOW | MPSAFE | AUDITVNODE1, pathseg, path,
3258 if ((error = namei(&nd)) != 0)
3260 vfslocked = NDHASGIANT(&nd);
3261 NDFREE(&nd, NDF_ONLY_PNBUF);
3262 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3264 VFS_UNLOCK_GIANT(vfslocked);
3269 * Set the access and modification times of a file.
3271 #ifndef _SYS_SYSPROTO_H_
3272 struct lutimes_args {
3274 struct timeval *tptr;
3280 register struct lutimes_args /* {
3282 struct timeval *tptr;
3286 return (kern_lutimes(td, uap->path, UIO_USERSPACE, uap->tptr,
3291 kern_lutimes(struct thread *td, char *path, enum uio_seg pathseg,
3292 struct timeval *tptr, enum uio_seg tptrseg)
3294 struct timespec ts[2];
3296 struct nameidata nd;
3299 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3301 NDINIT(&nd, LOOKUP, NOFOLLOW | MPSAFE | AUDITVNODE1, pathseg, path, td);
3302 if ((error = namei(&nd)) != 0)
3304 vfslocked = NDHASGIANT(&nd);
3305 NDFREE(&nd, NDF_ONLY_PNBUF);
3306 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3308 VFS_UNLOCK_GIANT(vfslocked);
3313 * Set the access and modification times of a file.
3315 #ifndef _SYS_SYSPROTO_H_
3316 struct futimes_args {
3318 struct timeval *tptr;
3324 register struct futimes_args /* {
3326 struct timeval *tptr;
3330 return (kern_futimes(td, uap->fd, uap->tptr, UIO_USERSPACE));
3334 kern_futimes(struct thread *td, int fd, struct timeval *tptr,
3335 enum uio_seg tptrseg)
3337 struct timespec ts[2];
3343 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3345 if ((error = getvnode(td->td_proc->p_fd, fd, &fp)) != 0)
3347 vfslocked = VFS_LOCK_GIANT(fp->f_vnode->v_mount);
3349 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
3350 AUDIT_ARG_VNODE1(fp->f_vnode);
3351 VOP_UNLOCK(fp->f_vnode, 0);
3353 error = setutimes(td, fp->f_vnode, ts, 2, tptr == NULL);
3354 VFS_UNLOCK_GIANT(vfslocked);
3360 * Truncate a file given its path name.
3362 #ifndef _SYS_SYSPROTO_H_
3363 struct truncate_args {
3372 register struct truncate_args /* {
3379 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3383 kern_truncate(struct thread *td, char *path, enum uio_seg pathseg, off_t length)
3389 struct nameidata nd;
3394 NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE | AUDITVNODE1, pathseg, path, td);
3395 if ((error = namei(&nd)) != 0)
3397 vfslocked = NDHASGIANT(&nd);
3399 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0) {
3401 VFS_UNLOCK_GIANT(vfslocked);
3404 NDFREE(&nd, NDF_ONLY_PNBUF);
3405 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3406 if (vp->v_type == VDIR)
3409 else if ((error = mac_vnode_check_write(td->td_ucred, NOCRED, vp))) {
3412 else if ((error = vn_writechk(vp)) == 0 &&
3413 (error = VOP_ACCESS(vp, VWRITE, td->td_ucred, td)) == 0) {
3415 vattr.va_size = length;
3416 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3419 vn_finished_write(mp);
3420 VFS_UNLOCK_GIANT(vfslocked);
3424 #if defined(COMPAT_43)
3426 * Truncate a file given its path name.
3428 #ifndef _SYS_SYSPROTO_H_
3429 struct otruncate_args {
3437 register struct otruncate_args /* {
3442 struct truncate_args /* {
3448 nuap.path = uap->path;
3449 nuap.length = uap->length;
3450 return (truncate(td, &nuap));
3452 #endif /* COMPAT_43 */
3454 /* Versions with the pad argument */
3456 freebsd6_truncate(struct thread *td, struct freebsd6_truncate_args *uap)
3458 struct truncate_args ouap;
3460 ouap.path = uap->path;
3461 ouap.length = uap->length;
3462 return (truncate(td, &ouap));
3466 freebsd6_ftruncate(struct thread *td, struct freebsd6_ftruncate_args *uap)
3468 struct ftruncate_args ouap;
3471 ouap.length = uap->length;
3472 return (ftruncate(td, &ouap));
3476 * Sync an open file.
3478 #ifndef _SYS_SYSPROTO_H_
3486 struct fsync_args /* {
3494 int error, lock_flags;
3496 AUDIT_ARG_FD(uap->fd);
3497 if ((error = getvnode(td->td_proc->p_fd, uap->fd, &fp)) != 0)
3500 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
3501 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
3503 if (MNT_SHARED_WRITES(mp) ||
3504 ((mp == NULL) && MNT_SHARED_WRITES(vp->v_mount))) {
3505 lock_flags = LK_SHARED;
3507 lock_flags = LK_EXCLUSIVE;
3509 vn_lock(vp, lock_flags | LK_RETRY);
3510 AUDIT_ARG_VNODE1(vp);
3511 if (vp->v_object != NULL) {
3512 VM_OBJECT_LOCK(vp->v_object);
3513 vm_object_page_clean(vp->v_object, 0, 0, 0);
3514 VM_OBJECT_UNLOCK(vp->v_object);
3516 error = VOP_FSYNC(vp, MNT_WAIT, td);
3519 vn_finished_write(mp);
3521 VFS_UNLOCK_GIANT(vfslocked);
3527 * Rename files. Source and destination must either both be directories, or
3528 * both not be directories. If target is a directory, it must be empty.
3530 #ifndef _SYS_SYSPROTO_H_
3531 struct rename_args {
3539 register struct rename_args /* {
3545 return (kern_rename(td, uap->from, uap->to, UIO_USERSPACE));
3548 #ifndef _SYS_SYSPROTO_H_
3549 struct renameat_args {
3557 renameat(struct thread *td, struct renameat_args *uap)
3560 return (kern_renameat(td, uap->oldfd, uap->old, uap->newfd, uap->new,
3565 kern_rename(struct thread *td, char *from, char *to, enum uio_seg pathseg)
3568 return (kern_renameat(td, AT_FDCWD, from, AT_FDCWD, to, pathseg));
3572 kern_renameat(struct thread *td, int oldfd, char *old, int newfd, char *new,
3573 enum uio_seg pathseg)
3575 struct mount *mp = NULL;
3576 struct vnode *tvp, *fvp, *tdvp;
3577 struct nameidata fromnd, tond;
3584 NDINIT_AT(&fromnd, DELETE, LOCKPARENT | LOCKLEAF | SAVESTART | MPSAFE |
3585 AUDITVNODE1, pathseg, old, oldfd, td);
3587 NDINIT_AT(&fromnd, DELETE, WANTPARENT | SAVESTART | MPSAFE |
3588 AUDITVNODE1, pathseg, old, oldfd, td);
3591 if ((error = namei(&fromnd)) != 0)
3593 fvfslocked = NDHASGIANT(&fromnd);
3596 error = mac_vnode_check_rename_from(td->td_ucred, fromnd.ni_dvp,
3597 fromnd.ni_vp, &fromnd.ni_cnd);
3598 VOP_UNLOCK(fromnd.ni_dvp, 0);
3599 if (fromnd.ni_dvp != fromnd.ni_vp)
3600 VOP_UNLOCK(fromnd.ni_vp, 0);
3604 error = vn_start_write(fvp, &mp, V_WAIT | PCATCH);
3606 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3607 vrele(fromnd.ni_dvp);
3611 NDINIT_AT(&tond, RENAME, LOCKPARENT | LOCKLEAF | NOCACHE | SAVESTART |
3612 MPSAFE | AUDITVNODE2, pathseg, new, newfd, td);
3613 if (fromnd.ni_vp->v_type == VDIR)
3614 tond.ni_cnd.cn_flags |= WILLBEDIR;
3615 if ((error = namei(&tond)) != 0) {
3616 /* Translate error code for rename("dir1", "dir2/."). */
3617 if (error == EISDIR && fvp->v_type == VDIR)
3619 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3620 vrele(fromnd.ni_dvp);
3622 vn_finished_write(mp);
3625 tvfslocked = NDHASGIANT(&tond);
3629 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
3632 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
3642 * If the source is the same as the destination (that is, if they
3643 * are links to the same vnode), then there is nothing to do.
3649 error = mac_vnode_check_rename_to(td->td_ucred, tdvp,
3650 tond.ni_vp, fromnd.ni_dvp == tdvp, &tond.ni_cnd);
3654 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
3655 tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
3656 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3657 NDFREE(&tond, NDF_ONLY_PNBUF);
3659 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3660 NDFREE(&tond, NDF_ONLY_PNBUF);
3667 vrele(fromnd.ni_dvp);
3670 vrele(tond.ni_startdir);
3671 vn_finished_write(mp);
3673 if (fromnd.ni_startdir)
3674 vrele(fromnd.ni_startdir);
3675 VFS_UNLOCK_GIANT(fvfslocked);
3676 VFS_UNLOCK_GIANT(tvfslocked);
3683 * Make a directory file.
3685 #ifndef _SYS_SYSPROTO_H_
3694 register struct mkdir_args /* {
3700 return (kern_mkdir(td, uap->path, UIO_USERSPACE, uap->mode));
3703 #ifndef _SYS_SYSPROTO_H_
3704 struct mkdirat_args {
3711 mkdirat(struct thread *td, struct mkdirat_args *uap)
3714 return (kern_mkdirat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode));
3718 kern_mkdir(struct thread *td, char *path, enum uio_seg segflg, int mode)
3721 return (kern_mkdirat(td, AT_FDCWD, path, segflg, mode));
3725 kern_mkdirat(struct thread *td, int fd, char *path, enum uio_seg segflg,
3732 struct nameidata nd;
3735 AUDIT_ARG_MODE(mode);
3738 NDINIT_AT(&nd, CREATE, LOCKPARENT | SAVENAME | MPSAFE | AUDITVNODE1,
3739 segflg, path, fd, td);
3740 nd.ni_cnd.cn_flags |= WILLBEDIR;
3741 if ((error = namei(&nd)) != 0)
3743 vfslocked = NDHASGIANT(&nd);
3746 NDFREE(&nd, NDF_ONLY_PNBUF);
3748 * XXX namei called with LOCKPARENT but not LOCKLEAF has
3749 * the strange behaviour of leaving the vnode unlocked
3750 * if the target is the same vnode as the parent.
3752 if (vp == nd.ni_dvp)
3757 VFS_UNLOCK_GIANT(vfslocked);
3760 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3761 NDFREE(&nd, NDF_ONLY_PNBUF);
3763 VFS_UNLOCK_GIANT(vfslocked);
3764 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3769 vattr.va_type = VDIR;
3770 vattr.va_mode = (mode & ACCESSPERMS) &~ td->td_proc->p_fd->fd_cmask;
3772 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
3777 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
3781 NDFREE(&nd, NDF_ONLY_PNBUF);
3785 vn_finished_write(mp);
3786 VFS_UNLOCK_GIANT(vfslocked);
3791 * Remove a directory file.
3793 #ifndef _SYS_SYSPROTO_H_
3801 struct rmdir_args /* {
3806 return (kern_rmdir(td, uap->path, UIO_USERSPACE));
3810 kern_rmdir(struct thread *td, char *path, enum uio_seg pathseg)
3813 return (kern_rmdirat(td, AT_FDCWD, path, pathseg));
3817 kern_rmdirat(struct thread *td, int fd, char *path, enum uio_seg pathseg)
3822 struct nameidata nd;
3827 NDINIT_AT(&nd, DELETE, LOCKPARENT | LOCKLEAF | MPSAFE | AUDITVNODE1,
3828 pathseg, path, fd, td);
3829 if ((error = namei(&nd)) != 0)
3831 vfslocked = NDHASGIANT(&nd);
3833 if (vp->v_type != VDIR) {
3838 * No rmdir "." please.
3840 if (nd.ni_dvp == vp) {
3845 * The root of a mounted filesystem cannot be deleted.
3847 if (vp->v_vflag & VV_ROOT) {
3852 error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp,
3857 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3858 NDFREE(&nd, NDF_ONLY_PNBUF);
3860 if (nd.ni_dvp == vp)
3864 VFS_UNLOCK_GIANT(vfslocked);
3865 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3869 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
3870 vn_finished_write(mp);
3872 NDFREE(&nd, NDF_ONLY_PNBUF);
3874 if (nd.ni_dvp == vp)
3878 VFS_UNLOCK_GIANT(vfslocked);
3884 * Read a block of directory entries in a filesystem independent format.
3886 #ifndef _SYS_SYSPROTO_H_
3887 struct ogetdirentries_args {
3895 ogetdirentries(td, uap)
3897 register struct ogetdirentries_args /* {
3906 struct uio auio, kuio;
3907 struct iovec aiov, kiov;
3908 struct dirent *dp, *edp;
3910 int error, eofflag, readcnt, vfslocked;
3913 /* XXX arbitrary sanity limit on `count'. */
3914 if (uap->count > 64 * 1024)
3916 if ((error = getvnode(td->td_proc->p_fd, uap->fd, &fp)) != 0)
3918 if ((fp->f_flag & FREAD) == 0) {
3924 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
3925 if (vp->v_type != VDIR) {
3926 VFS_UNLOCK_GIANT(vfslocked);
3930 aiov.iov_base = uap->buf;
3931 aiov.iov_len = uap->count;
3932 auio.uio_iov = &aiov;
3933 auio.uio_iovcnt = 1;
3934 auio.uio_rw = UIO_READ;
3935 auio.uio_segflg = UIO_USERSPACE;
3937 auio.uio_resid = uap->count;
3938 vn_lock(vp, LK_SHARED | LK_RETRY);
3939 loff = auio.uio_offset = fp->f_offset;
3941 error = mac_vnode_check_readdir(td->td_ucred, vp);
3944 VFS_UNLOCK_GIANT(vfslocked);
3949 # if (BYTE_ORDER != LITTLE_ENDIAN)
3950 if (vp->v_mount->mnt_maxsymlinklen <= 0) {
3951 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag,
3953 fp->f_offset = auio.uio_offset;
3958 kuio.uio_iov = &kiov;
3959 kuio.uio_segflg = UIO_SYSSPACE;
3960 kiov.iov_len = uap->count;
3961 dirbuf = malloc(uap->count, M_TEMP, M_WAITOK);
3962 kiov.iov_base = dirbuf;
3963 error = VOP_READDIR(vp, &kuio, fp->f_cred, &eofflag,
3965 fp->f_offset = kuio.uio_offset;
3967 readcnt = uap->count - kuio.uio_resid;
3968 edp = (struct dirent *)&dirbuf[readcnt];
3969 for (dp = (struct dirent *)dirbuf; dp < edp; ) {
3970 # if (BYTE_ORDER == LITTLE_ENDIAN)
3972 * The expected low byte of
3973 * dp->d_namlen is our dp->d_type.
3974 * The high MBZ byte of dp->d_namlen
3975 * is our dp->d_namlen.
3977 dp->d_type = dp->d_namlen;
3981 * The dp->d_type is the high byte
3982 * of the expected dp->d_namlen,
3983 * so must be zero'ed.
3987 if (dp->d_reclen > 0) {
3988 dp = (struct dirent *)
3989 ((char *)dp + dp->d_reclen);
3996 error = uiomove(dirbuf, readcnt, &auio);
3998 free(dirbuf, M_TEMP);
4002 VFS_UNLOCK_GIANT(vfslocked);
4006 if (uap->count == auio.uio_resid &&
4007 (vp->v_vflag & VV_ROOT) &&
4008 (vp->v_mount->mnt_flag & MNT_UNION)) {
4009 struct vnode *tvp = vp;
4010 vp = vp->v_mount->mnt_vnodecovered;
4016 VFS_UNLOCK_GIANT(vfslocked);
4020 VFS_UNLOCK_GIANT(vfslocked);
4021 error = copyout(&loff, uap->basep, sizeof(long));
4023 td->td_retval[0] = uap->count - auio.uio_resid;
4026 #endif /* COMPAT_43 */
4029 * Read a block of directory entries in a filesystem independent format.
4031 #ifndef _SYS_SYSPROTO_H_
4032 struct getdirentries_args {
4040 getdirentries(td, uap)
4042 register struct getdirentries_args /* {
4052 error = kern_getdirentries(td, uap->fd, uap->buf, uap->count, &base);
4055 if (uap->basep != NULL)
4056 error = copyout(&base, uap->basep, sizeof(long));
4061 kern_getdirentries(struct thread *td, int fd, char *buf, u_int count,
4073 if (count > INT_MAX)
4075 if ((error = getvnode(td->td_proc->p_fd, fd, &fp)) != 0)
4077 if ((fp->f_flag & FREAD) == 0) {
4083 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
4084 if (vp->v_type != VDIR) {
4085 VFS_UNLOCK_GIANT(vfslocked);
4089 aiov.iov_base = buf;
4090 aiov.iov_len = count;
4091 auio.uio_iov = &aiov;
4092 auio.uio_iovcnt = 1;
4093 auio.uio_rw = UIO_READ;
4094 auio.uio_segflg = UIO_USERSPACE;
4096 auio.uio_resid = count;
4097 vn_lock(vp, LK_SHARED | LK_RETRY);
4098 AUDIT_ARG_VNODE1(vp);
4099 loff = auio.uio_offset = fp->f_offset;
4101 error = mac_vnode_check_readdir(td->td_ucred, vp);
4104 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL,
4106 fp->f_offset = auio.uio_offset;
4109 VFS_UNLOCK_GIANT(vfslocked);
4112 if (count == auio.uio_resid &&
4113 (vp->v_vflag & VV_ROOT) &&
4114 (vp->v_mount->mnt_flag & MNT_UNION)) {
4115 struct vnode *tvp = vp;
4116 vp = vp->v_mount->mnt_vnodecovered;
4122 VFS_UNLOCK_GIANT(vfslocked);
4126 VFS_UNLOCK_GIANT(vfslocked);
4128 td->td_retval[0] = count - auio.uio_resid;
4134 #ifndef _SYS_SYSPROTO_H_
4135 struct getdents_args {
4144 register struct getdents_args /* {
4150 struct getdirentries_args ap;
4153 ap.count = uap->count;
4155 return (getdirentries(td, &ap));
4159 * Set the mode mask for creation of filesystem nodes.
4161 #ifndef _SYS_SYSPROTO_H_
4169 struct umask_args /* {
4173 register struct filedesc *fdp;
4175 FILEDESC_XLOCK(td->td_proc->p_fd);
4176 fdp = td->td_proc->p_fd;
4177 td->td_retval[0] = fdp->fd_cmask;
4178 fdp->fd_cmask = uap->newmask & ALLPERMS;
4179 FILEDESC_XUNLOCK(td->td_proc->p_fd);
4184 * Void all references to file by ripping underlying filesystem away from
4187 #ifndef _SYS_SYSPROTO_H_
4188 struct revoke_args {
4195 register struct revoke_args /* {
4202 struct nameidata nd;
4205 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | MPSAFE | AUDITVNODE1,
4206 UIO_USERSPACE, uap->path, td);
4207 if ((error = namei(&nd)) != 0)
4209 vfslocked = NDHASGIANT(&nd);
4211 NDFREE(&nd, NDF_ONLY_PNBUF);
4212 if (vp->v_type != VCHR || vp->v_rdev == NULL) {
4217 error = mac_vnode_check_revoke(td->td_ucred, vp);
4221 error = VOP_GETATTR(vp, &vattr, td->td_ucred);
4224 if (td->td_ucred->cr_uid != vattr.va_uid) {
4225 error = priv_check(td, PRIV_VFS_ADMIN);
4230 VOP_REVOKE(vp, REVOKEALL);
4233 VFS_UNLOCK_GIANT(vfslocked);
4238 * Convert a user file descriptor to a kernel file entry.
4239 * A reference on the file entry is held upon returning.
4242 getvnode(fdp, fd, fpp)
4243 struct filedesc *fdp;
4252 if (fdp == NULL || (fp = fget_unlocked(fdp, fd)) == NULL)
4254 else if (fp->f_vnode == NULL) {
4256 fdrop(fp, curthread);
4263 * Get an (NFS) file handle.
4265 #ifndef _SYS_SYSPROTO_H_
4266 struct lgetfh_args {
4274 register struct lgetfh_args *uap;
4276 struct nameidata nd;
4278 register struct vnode *vp;
4282 error = priv_check(td, PRIV_VFS_GETFH);
4285 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | MPSAFE | AUDITVNODE1,
4286 UIO_USERSPACE, uap->fname, td);
4290 vfslocked = NDHASGIANT(&nd);
4291 NDFREE(&nd, NDF_ONLY_PNBUF);
4293 bzero(&fh, sizeof(fh));
4294 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
4295 error = VOP_VPTOFH(vp, &fh.fh_fid);
4297 VFS_UNLOCK_GIANT(vfslocked);
4300 error = copyout(&fh, uap->fhp, sizeof (fh));
4304 #ifndef _SYS_SYSPROTO_H_
4313 register struct getfh_args *uap;
4315 struct nameidata nd;
4317 register struct vnode *vp;
4321 error = priv_check(td, PRIV_VFS_GETFH);
4324 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | MPSAFE | AUDITVNODE1,
4325 UIO_USERSPACE, uap->fname, td);
4329 vfslocked = NDHASGIANT(&nd);
4330 NDFREE(&nd, NDF_ONLY_PNBUF);
4332 bzero(&fh, sizeof(fh));
4333 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
4334 error = VOP_VPTOFH(vp, &fh.fh_fid);
4336 VFS_UNLOCK_GIANT(vfslocked);
4339 error = copyout(&fh, uap->fhp, sizeof (fh));
4344 * syscall for the rpc.lockd to use to translate a NFS file handle into an
4347 * warning: do not remove the priv_check() call or this becomes one giant
4350 #ifndef _SYS_SYSPROTO_H_
4351 struct fhopen_args {
4352 const struct fhandle *u_fhp;
4359 struct fhopen_args /* {
4360 const struct fhandle *u_fhp;
4364 struct proc *p = td->td_proc;
4369 struct vattr *vap = &vat;
4372 register struct filedesc *fdp = p->p_fd;
4373 int fmode, error, type;
4379 error = priv_check(td, PRIV_VFS_FHOPEN);
4382 fmode = FFLAGS(uap->flags);
4383 /* why not allow a non-read/write open for our lockd? */
4384 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
4386 error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
4389 /* find the mount point */
4390 mp = vfs_busyfs(&fhp.fh_fsid);
4393 vfslocked = VFS_LOCK_GIANT(mp);
4394 /* now give me my vnode, it gets returned to me locked */
4395 error = VFS_FHTOVP(mp, &fhp.fh_fid, &vp);
4400 * from now on we have to make sure not
4401 * to forget about the vnode
4402 * any error that causes an abort must vput(vp)
4403 * just set error = err and 'goto bad;'.
4409 if (vp->v_type == VLNK) {
4413 if (vp->v_type == VSOCK) {
4418 if (fmode & (FWRITE | O_TRUNC)) {
4419 if (vp->v_type == VDIR) {
4423 error = vn_writechk(vp);
4430 if ((fmode & O_APPEND) && (fmode & FWRITE))
4433 error = mac_vnode_check_open(td->td_ucred, vp, accmode);
4438 error = VOP_ACCESS(vp, accmode, td->td_ucred, td);
4442 if (fmode & O_TRUNC) {
4444 VOP_UNLOCK(vp, 0); /* XXX */
4445 if ((error = vn_start_write(NULL, &mp, V_WAIT | PCATCH)) != 0) {
4450 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); /* XXX */
4454 * We don't yet have fp->f_cred, so use td->td_ucred, which
4457 error = mac_vnode_check_write(td->td_ucred, td->td_ucred, vp);
4462 error = VOP_SETATTR(vp, vap, td->td_ucred);
4466 vn_finished_write(mp);
4470 error = VOP_OPEN(vp, fmode, td->td_ucred, td, NULL);
4478 * end of vn_open code
4481 if ((error = fallocf(td, &nfp, &indx, fmode)) != 0) {
4486 /* An extra reference on `nfp' has been held for us by falloc(). */
4489 finit(nfp, fmode & FMASK, DTYPE_VNODE, vp, &vnops);
4490 if (fmode & (O_EXLOCK | O_SHLOCK)) {
4491 lf.l_whence = SEEK_SET;
4494 if (fmode & O_EXLOCK)
4495 lf.l_type = F_WRLCK;
4497 lf.l_type = F_RDLCK;
4499 if ((fmode & FNONBLOCK) == 0)
4502 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf,
4505 * The lock request failed. Normally close the
4506 * descriptor but handle the case where someone might
4507 * have dup()d or close()d it when we weren't looking.
4509 fdclose(fdp, fp, indx, td);
4512 * release our private reference
4517 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
4518 atomic_set_int(&fp->f_flag, FHASLOCK);
4523 VFS_UNLOCK_GIANT(vfslocked);
4524 td->td_retval[0] = indx;
4530 VFS_UNLOCK_GIANT(vfslocked);
4535 * Stat an (NFS) file handle.
4537 #ifndef _SYS_SYSPROTO_H_
4538 struct fhstat_args {
4539 struct fhandle *u_fhp;
4546 register struct fhstat_args /* {
4547 struct fhandle *u_fhp;
4558 error = priv_check(td, PRIV_VFS_FHSTAT);
4561 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4564 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4566 vfslocked = VFS_LOCK_GIANT(mp);
4567 error = VFS_FHTOVP(mp, &fh.fh_fid, &vp);
4570 VFS_UNLOCK_GIANT(vfslocked);
4573 error = vn_stat(vp, &sb, td->td_ucred, NOCRED, td);
4575 VFS_UNLOCK_GIANT(vfslocked);
4578 error = copyout(&sb, uap->sb, sizeof(sb));
4583 * Implement fstatfs() for (NFS) file handles.
4585 #ifndef _SYS_SYSPROTO_H_
4586 struct fhstatfs_args {
4587 struct fhandle *u_fhp;
4594 struct fhstatfs_args /* {
4595 struct fhandle *u_fhp;
4603 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4606 error = kern_fhstatfs(td, fh, &sf);
4609 return (copyout(&sf, uap->buf, sizeof(sf)));
4613 kern_fhstatfs(struct thread *td, fhandle_t fh, struct statfs *buf)
4621 error = priv_check(td, PRIV_VFS_FHSTATFS);
4624 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4626 vfslocked = VFS_LOCK_GIANT(mp);
4627 error = VFS_FHTOVP(mp, &fh.fh_fid, &vp);
4630 VFS_UNLOCK_GIANT(vfslocked);
4634 error = prison_canseemount(td->td_ucred, mp);
4638 error = mac_mount_check_stat(td->td_ucred, mp);
4643 * Set these in case the underlying filesystem fails to do so.
4646 sp->f_version = STATFS_VERSION;
4647 sp->f_namemax = NAME_MAX;
4648 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
4649 error = VFS_STATFS(mp, sp);
4654 VFS_UNLOCK_GIANT(vfslocked);
4659 kern_posix_fallocate(struct thread *td, int fd, off_t offset, off_t len)
4664 off_t olen, ooffset;
4665 int error, vfslocked;
4669 error = fget(td, fd, &fp);
4673 switch (fp->f_type) {
4684 if ((fp->f_flag & FWRITE) == 0) {
4689 if (vp->v_type != VREG) {
4693 if (offset < 0 || len <= 0) {
4697 /* Check for wrap. */
4698 if (offset > OFF_MAX - len) {
4703 /* Allocating blocks may take a long time, so iterate. */
4709 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
4711 error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
4713 VFS_UNLOCK_GIANT(vfslocked);
4716 error = vn_lock(vp, LK_EXCLUSIVE);
4718 vn_finished_write(mp);
4719 VFS_UNLOCK_GIANT(vfslocked);
4723 error = mac_vnode_check_write(td->td_ucred, fp->f_cred, vp);
4726 error = VOP_ALLOCATE(vp, &offset, &len);
4728 vn_finished_write(mp);
4729 VFS_UNLOCK_GIANT(vfslocked);
4731 if (olen + ooffset != offset + len) {
4732 panic("offset + len changed from %jx/%jx to %jx/%jx",
4733 ooffset, olen, offset, len);
4735 if (error != 0 || len == 0)
4737 KASSERT(olen > len, ("Iteration did not make progress?"));
4747 posix_fallocate(struct thread *td, struct posix_fallocate_args *uap)
4750 return (kern_posix_fallocate(td, uap->fd, uap->offset, uap->len));