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"
43 #include <sys/param.h>
44 #include <sys/systm.h>
47 #include <sys/sysent.h>
48 #include <sys/malloc.h>
49 #include <sys/mount.h>
50 #include <sys/mutex.h>
51 #include <sys/sysproto.h>
52 #include <sys/namei.h>
53 #include <sys/filedesc.h>
54 #include <sys/kernel.h>
55 #include <sys/fcntl.h>
57 #include <sys/filio.h>
58 #include <sys/limits.h>
59 #include <sys/linker.h>
62 #include <sys/unistd.h>
63 #include <sys/vnode.h>
66 #include <sys/dirent.h>
68 #include <sys/syscallsubr.h>
69 #include <sys/sysctl.h>
71 #include <machine/stdarg.h>
73 #include <security/audit/audit.h>
74 #include <security/mac/mac_framework.h>
77 #include <vm/vm_object.h>
78 #include <vm/vm_page.h>
81 static int chroot_refuse_vdir_fds(struct filedesc *fdp);
82 static int getutimes(const struct timeval *, enum uio_seg, struct timespec *);
83 static int setfown(struct thread *td, struct vnode *, uid_t, gid_t);
84 static int setfmode(struct thread *td, struct vnode *, int);
85 static int setfflags(struct thread *td, struct vnode *, int);
86 static int setutimes(struct thread *td, struct vnode *,
87 const struct timespec *, int, int);
88 static int vn_access(struct vnode *vp, int user_flags, struct ucred *cred,
92 * The module initialization routine for POSIX asynchronous I/O will
93 * set this to the version of AIO that it implements. (Zero means
94 * that it is not implemented.) This value is used here by pathconf()
95 * and in kern_descrip.c by fpathconf().
100 static int syncprt = 0;
101 SYSCTL_INT(_debug, OID_AUTO, syncprt, CTLFLAG_RW, &syncprt, 0, "");
105 * Sync each mounted filesystem.
107 #ifndef _SYS_SYSPROTO_H_
116 struct sync_args *uap;
118 struct mount *mp, *nmp;
121 mtx_lock(&mountlist_mtx);
122 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
123 if (vfs_busy(mp, LK_NOWAIT, &mountlist_mtx, td)) {
124 nmp = TAILQ_NEXT(mp, mnt_list);
127 vfslocked = VFS_LOCK_GIANT(mp);
128 if ((mp->mnt_flag & MNT_RDONLY) == 0 &&
129 vn_start_write(NULL, &mp, V_NOWAIT) == 0) {
132 mp->mnt_kern_flag &= ~MNTK_ASYNC;
134 vfs_msync(mp, MNT_NOWAIT);
135 VFS_SYNC(mp, MNT_NOWAIT, td);
138 if ((mp->mnt_flag & MNT_ASYNC) != 0 &&
139 mp->mnt_noasync == 0)
140 mp->mnt_kern_flag |= MNTK_ASYNC;
142 vn_finished_write(mp);
144 VFS_UNLOCK_GIANT(vfslocked);
145 mtx_lock(&mountlist_mtx);
146 nmp = TAILQ_NEXT(mp, mnt_list);
149 mtx_unlock(&mountlist_mtx);
153 /* XXX PRISON: could be per prison flag */
154 static int prison_quotas;
156 SYSCTL_INT(_kern_prison, OID_AUTO, quotas, CTLFLAG_RW, &prison_quotas, 0, "");
160 * Change filesystem quotas.
162 #ifndef _SYS_SYSPROTO_H_
163 struct quotactl_args {
173 register struct quotactl_args /* {
185 AUDIT_ARG(cmd, uap->cmd);
186 AUDIT_ARG(uid, uap->uid);
187 if (jailed(td->td_ucred) && !prison_quotas)
189 NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE | AUDITVNODE1,
190 UIO_USERSPACE, uap->path, td);
191 if ((error = namei(&nd)) != 0)
193 vfslocked = NDHASGIANT(&nd);
194 NDFREE(&nd, NDF_ONLY_PNBUF);
195 mp = nd.ni_vp->v_mount;
196 if ((error = vfs_busy(mp, 0, NULL, td))) {
198 VFS_UNLOCK_GIANT(vfslocked);
202 error = VFS_QUOTACTL(mp, uap->cmd, uap->uid, uap->arg, td);
204 VFS_UNLOCK_GIANT(vfslocked);
209 * Get filesystem statistics.
211 #ifndef _SYS_SYSPROTO_H_
220 register struct statfs_args /* {
228 error = kern_statfs(td, uap->path, UIO_USERSPACE, &sf);
230 error = copyout(&sf, uap->buf, sizeof(sf));
235 kern_statfs(struct thread *td, char *path, enum uio_seg pathseg,
239 struct statfs *sp, sb;
244 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | MPSAFE | AUDITVNODE1,
249 vfslocked = NDHASGIANT(&nd);
250 mp = nd.ni_vp->v_mount;
252 NDFREE(&nd, NDF_ONLY_PNBUF);
255 error = mac_check_mount_stat(td->td_ucred, mp);
260 * Set these in case the underlying filesystem fails to do so.
263 sp->f_version = STATFS_VERSION;
264 sp->f_namemax = NAME_MAX;
265 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
266 error = VFS_STATFS(mp, sp, td);
269 if (priv_check(td, PRIV_VFS_GENERATION)) {
270 bcopy(sp, &sb, sizeof(sb));
271 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
272 prison_enforce_statfs(td->td_ucred, mp, &sb);
278 VFS_UNLOCK_GIANT(vfslocked);
279 if (mtx_owned(&Giant))
280 printf("statfs(%d): %s: %d\n", vfslocked, path, error);
285 * Get filesystem statistics.
287 #ifndef _SYS_SYSPROTO_H_
288 struct fstatfs_args {
296 register struct fstatfs_args /* {
304 error = kern_fstatfs(td, uap->fd, &sf);
306 error = copyout(&sf, uap->buf, sizeof(sf));
311 kern_fstatfs(struct thread *td, int fd, struct statfs *buf)
315 struct statfs *sp, sb;
321 error = getvnode(td->td_proc->p_fd, fd, &fp);
325 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
326 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
328 AUDIT_ARG(vnode, vp, ARG_VNODE1);
333 VOP_UNLOCK(vp, 0, td);
335 if (vp->v_iflag & VI_DOOMED) {
340 error = mac_check_mount_stat(td->td_ucred, mp);
345 * Set these in case the underlying filesystem fails to do so.
348 sp->f_version = STATFS_VERSION;
349 sp->f_namemax = NAME_MAX;
350 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
351 error = VFS_STATFS(mp, sp, td);
354 if (priv_check(td, PRIV_VFS_GENERATION)) {
355 bcopy(sp, &sb, sizeof(sb));
356 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
357 prison_enforce_statfs(td->td_ucred, mp, &sb);
364 VFS_UNLOCK_GIANT(vfslocked);
369 * Get statistics on all filesystems.
371 #ifndef _SYS_SYSPROTO_H_
372 struct getfsstat_args {
381 register struct getfsstat_args /* {
388 return (kern_getfsstat(td, &uap->buf, uap->bufsize, UIO_USERSPACE,
393 * If (bufsize > 0 && bufseg == UIO_SYSSPACE)
394 * The caller is responsible for freeing memory which will be allocated
398 kern_getfsstat(struct thread *td, struct statfs **buf, size_t bufsize,
399 enum uio_seg bufseg, int flags)
401 struct mount *mp, *nmp;
402 struct statfs *sfsp, *sp, sb;
403 size_t count, maxcount;
407 maxcount = bufsize / sizeof(struct statfs);
410 else if (bufseg == UIO_USERSPACE)
412 else /* if (bufseg == UIO_SYSSPACE) */ {
414 mtx_lock(&mountlist_mtx);
415 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
418 mtx_unlock(&mountlist_mtx);
419 if (maxcount > count)
421 sfsp = *buf = malloc(maxcount * sizeof(struct statfs), M_TEMP,
425 mtx_lock(&mountlist_mtx);
426 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
427 if (prison_canseemount(td->td_ucred, mp) != 0) {
428 nmp = TAILQ_NEXT(mp, mnt_list);
432 if (mac_check_mount_stat(td->td_ucred, mp) != 0) {
433 nmp = TAILQ_NEXT(mp, mnt_list);
437 if (vfs_busy(mp, LK_NOWAIT, &mountlist_mtx, td)) {
438 nmp = TAILQ_NEXT(mp, mnt_list);
441 vfslocked = VFS_LOCK_GIANT(mp);
442 if (sfsp && count < maxcount) {
445 * Set these in case the underlying filesystem
448 sp->f_version = STATFS_VERSION;
449 sp->f_namemax = NAME_MAX;
450 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
452 * If MNT_NOWAIT or MNT_LAZY is specified, do not
453 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
454 * overrides MNT_WAIT.
456 if (((flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
457 (flags & MNT_WAIT)) &&
458 (error = VFS_STATFS(mp, sp, td))) {
459 VFS_UNLOCK_GIANT(vfslocked);
460 mtx_lock(&mountlist_mtx);
461 nmp = TAILQ_NEXT(mp, mnt_list);
465 if (priv_check(td, PRIV_VFS_GENERATION)) {
466 bcopy(sp, &sb, sizeof(sb));
467 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
468 prison_enforce_statfs(td->td_ucred, mp, &sb);
471 if (bufseg == UIO_SYSSPACE)
472 bcopy(sp, sfsp, sizeof(*sp));
473 else /* if (bufseg == UIO_USERSPACE) */ {
474 error = copyout(sp, sfsp, sizeof(*sp));
477 VFS_UNLOCK_GIANT(vfslocked);
483 VFS_UNLOCK_GIANT(vfslocked);
485 mtx_lock(&mountlist_mtx);
486 nmp = TAILQ_NEXT(mp, mnt_list);
489 mtx_unlock(&mountlist_mtx);
490 if (sfsp && count > maxcount)
491 td->td_retval[0] = maxcount;
493 td->td_retval[0] = count;
497 #ifdef COMPAT_FREEBSD4
499 * Get old format filesystem statistics.
501 static void cvtstatfs(struct statfs *, struct ostatfs *);
503 #ifndef _SYS_SYSPROTO_H_
504 struct freebsd4_statfs_args {
510 freebsd4_statfs(td, uap)
512 struct freebsd4_statfs_args /* {
521 error = kern_statfs(td, uap->path, UIO_USERSPACE, &sf);
524 cvtstatfs(&sf, &osb);
525 return (copyout(&osb, uap->buf, sizeof(osb)));
529 * Get filesystem statistics.
531 #ifndef _SYS_SYSPROTO_H_
532 struct freebsd4_fstatfs_args {
538 freebsd4_fstatfs(td, uap)
540 struct freebsd4_fstatfs_args /* {
549 error = kern_fstatfs(td, uap->fd, &sf);
552 cvtstatfs(&sf, &osb);
553 return (copyout(&osb, uap->buf, sizeof(osb)));
557 * Get statistics on all filesystems.
559 #ifndef _SYS_SYSPROTO_H_
560 struct freebsd4_getfsstat_args {
567 freebsd4_getfsstat(td, uap)
569 register struct freebsd4_getfsstat_args /* {
575 struct statfs *buf, *sp;
580 count = uap->bufsize / sizeof(struct ostatfs);
581 size = count * sizeof(struct statfs);
582 error = kern_getfsstat(td, &buf, size, UIO_SYSSPACE, uap->flags);
584 count = td->td_retval[0];
586 while (count > 0 && error == 0) {
588 error = copyout(&osb, uap->buf, sizeof(osb));
599 * Implement fstatfs() for (NFS) file handles.
601 #ifndef _SYS_SYSPROTO_H_
602 struct freebsd4_fhstatfs_args {
603 struct fhandle *u_fhp;
608 freebsd4_fhstatfs(td, uap)
610 struct freebsd4_fhstatfs_args /* {
611 struct fhandle *u_fhp;
620 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
623 error = kern_fhstatfs(td, fh, &sf);
626 cvtstatfs(&sf, &osb);
627 return (copyout(&osb, uap->buf, sizeof(osb)));
631 * Convert a new format statfs structure to an old format statfs structure.
639 bzero(osp, sizeof(*osp));
640 osp->f_bsize = MIN(nsp->f_bsize, LONG_MAX);
641 osp->f_iosize = MIN(nsp->f_iosize, LONG_MAX);
642 osp->f_blocks = MIN(nsp->f_blocks, LONG_MAX);
643 osp->f_bfree = MIN(nsp->f_bfree, LONG_MAX);
644 osp->f_bavail = MIN(nsp->f_bavail, LONG_MAX);
645 osp->f_files = MIN(nsp->f_files, LONG_MAX);
646 osp->f_ffree = MIN(nsp->f_ffree, LONG_MAX);
647 osp->f_owner = nsp->f_owner;
648 osp->f_type = nsp->f_type;
649 osp->f_flags = nsp->f_flags;
650 osp->f_syncwrites = MIN(nsp->f_syncwrites, LONG_MAX);
651 osp->f_asyncwrites = MIN(nsp->f_asyncwrites, LONG_MAX);
652 osp->f_syncreads = MIN(nsp->f_syncreads, LONG_MAX);
653 osp->f_asyncreads = MIN(nsp->f_asyncreads, LONG_MAX);
654 strlcpy(osp->f_fstypename, nsp->f_fstypename,
655 MIN(MFSNAMELEN, OMFSNAMELEN));
656 strlcpy(osp->f_mntonname, nsp->f_mntonname,
657 MIN(MNAMELEN, OMNAMELEN));
658 strlcpy(osp->f_mntfromname, nsp->f_mntfromname,
659 MIN(MNAMELEN, OMNAMELEN));
660 osp->f_fsid = nsp->f_fsid;
662 #endif /* COMPAT_FREEBSD4 */
665 * Change current working directory to a given file descriptor.
667 #ifndef _SYS_SYSPROTO_H_
675 struct fchdir_args /* {
679 register struct filedesc *fdp = td->td_proc->p_fd;
680 struct vnode *vp, *tdp, *vpold;
686 AUDIT_ARG(fd, uap->fd);
687 if ((error = getvnode(fdp, uap->fd, &fp)) != 0)
692 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
693 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
694 AUDIT_ARG(vnode, vp, ARG_VNODE1);
695 error = change_dir(vp, td);
696 while (!error && (mp = vp->v_mountedhere) != NULL) {
698 if (vfs_busy(mp, 0, 0, td))
700 tvfslocked = VFS_LOCK_GIANT(mp);
701 error = VFS_ROOT(mp, LK_EXCLUSIVE, &tdp, td);
704 VFS_UNLOCK_GIANT(tvfslocked);
708 VFS_UNLOCK_GIANT(vfslocked);
710 vfslocked = tvfslocked;
714 VFS_UNLOCK_GIANT(vfslocked);
717 VOP_UNLOCK(vp, 0, td);
718 VFS_UNLOCK_GIANT(vfslocked);
720 vpold = fdp->fd_cdir;
722 FILEDESC_XUNLOCK(fdp);
723 vfslocked = VFS_LOCK_GIANT(vpold->v_mount);
725 VFS_UNLOCK_GIANT(vfslocked);
730 * Change current working directory (``.'').
732 #ifndef _SYS_SYSPROTO_H_
740 struct chdir_args /* {
745 return (kern_chdir(td, uap->path, UIO_USERSPACE));
749 kern_chdir(struct thread *td, char *path, enum uio_seg pathseg)
751 register struct filedesc *fdp = td->td_proc->p_fd;
757 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1 | MPSAFE,
759 if ((error = namei(&nd)) != 0)
761 vfslocked = NDHASGIANT(&nd);
762 if ((error = change_dir(nd.ni_vp, td)) != 0) {
764 VFS_UNLOCK_GIANT(vfslocked);
765 NDFREE(&nd, NDF_ONLY_PNBUF);
768 VOP_UNLOCK(nd.ni_vp, 0, td);
769 VFS_UNLOCK_GIANT(vfslocked);
770 NDFREE(&nd, NDF_ONLY_PNBUF);
773 fdp->fd_cdir = nd.ni_vp;
774 FILEDESC_XUNLOCK(fdp);
775 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
777 VFS_UNLOCK_GIANT(vfslocked);
782 * Helper function for raised chroot(2) security function: Refuse if
783 * any filedescriptors are open directories.
786 chroot_refuse_vdir_fds(fdp)
787 struct filedesc *fdp;
793 FILEDESC_LOCK_ASSERT(fdp);
795 for (fd = 0; fd < fdp->fd_nfiles ; fd++) {
796 fp = fget_locked(fdp, fd);
799 if (fp->f_type == DTYPE_VNODE) {
801 if (vp->v_type == VDIR)
809 * This sysctl determines if we will allow a process to chroot(2) if it
810 * has a directory open:
811 * 0: disallowed for all processes.
812 * 1: allowed for processes that were not already chroot(2)'ed.
813 * 2: allowed for all processes.
816 static int chroot_allow_open_directories = 1;
818 SYSCTL_INT(_kern, OID_AUTO, chroot_allow_open_directories, CTLFLAG_RW,
819 &chroot_allow_open_directories, 0, "");
822 * Change notion of root (``/'') directory.
824 #ifndef _SYS_SYSPROTO_H_
832 struct chroot_args /* {
840 error = priv_check_cred(td->td_ucred, PRIV_VFS_CHROOT,
844 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | MPSAFE | AUDITVNODE1,
845 UIO_USERSPACE, uap->path, td);
849 vfslocked = NDHASGIANT(&nd);
850 if ((error = change_dir(nd.ni_vp, td)) != 0)
853 if ((error = mac_check_vnode_chroot(td->td_ucred, nd.ni_vp)))
856 VOP_UNLOCK(nd.ni_vp, 0, td);
857 error = change_root(nd.ni_vp, td);
859 VFS_UNLOCK_GIANT(vfslocked);
860 NDFREE(&nd, NDF_ONLY_PNBUF);
864 VFS_UNLOCK_GIANT(vfslocked);
866 NDFREE(&nd, NDF_ONLY_PNBUF);
871 * Common routine for chroot and chdir. Callers must provide a locked vnode
881 ASSERT_VOP_LOCKED(vp, "change_dir(): vp not locked");
882 if (vp->v_type != VDIR)
885 error = mac_check_vnode_chdir(td->td_ucred, vp);
889 error = VOP_ACCESS(vp, VEXEC, td->td_ucred, td);
894 * Common routine for kern_chroot() and jail_attach(). The caller is
895 * responsible for invoking priv_check() and mac_check_chroot() to authorize
903 struct filedesc *fdp;
908 VFS_ASSERT_GIANT(vp->v_mount);
909 fdp = td->td_proc->p_fd;
911 if (chroot_allow_open_directories == 0 ||
912 (chroot_allow_open_directories == 1 && fdp->fd_rdir != rootvnode)) {
913 error = chroot_refuse_vdir_fds(fdp);
915 FILEDESC_XUNLOCK(fdp);
919 oldvp = fdp->fd_rdir;
926 FILEDESC_XUNLOCK(fdp);
927 vfslocked = VFS_LOCK_GIANT(oldvp->v_mount);
929 VFS_UNLOCK_GIANT(vfslocked);
934 * Check permissions, allocate an open file structure, and call the device
935 * open routine if any.
937 #ifndef _SYS_SYSPROTO_H_
947 register struct open_args /* {
954 return kern_open(td, uap->path, UIO_USERSPACE, uap->flags, uap->mode);
958 kern_open(struct thread *td, char *path, enum uio_seg pathseg, int flags,
961 struct proc *p = td->td_proc;
962 struct filedesc *fdp = p->p_fd;
969 int type, indx, error;
974 AUDIT_ARG(fflags, flags);
975 AUDIT_ARG(mode, mode);
976 if ((flags & O_ACCMODE) == O_ACCMODE)
978 flags = FFLAGS(flags);
979 error = falloc(td, &nfp, &indx);
982 /* An extra reference on `nfp' has been held for us by falloc(). */
984 cmode = ((mode &~ fdp->fd_cmask) & ALLPERMS) &~ S_ISTXT;
985 NDINIT(&nd, LOOKUP, FOLLOW | AUDITVNODE1 | MPSAFE, pathseg, path, td);
986 td->td_dupfd = -1; /* XXX check for fdopen */
988 if (fp != fdp->fd_ofiles[indx]) {
989 FILEDESC_XUNLOCK(fdp);
991 td->td_retval[0] = indx;
994 fdp->fd_ofileflags[indx] |= UF_OPENING;
995 FILEDESC_XUNLOCK(fdp);
996 error = vn_open(&nd, &flags, cmode, indx);
998 fdp->fd_ofileflags[indx] &= ~UF_OPENING;
999 FILEDESC_XUNLOCK(fdp);
1002 * If the vn_open replaced the method vector, something
1003 * wonderous happened deep below and we just pass it up
1004 * pretending we know what we do.
1006 if (error == ENXIO && fp->f_ops != &badfileops) {
1008 td->td_retval[0] = indx;
1013 * handle special fdopen() case. bleh. dupfdopen() is
1014 * responsible for dropping the old contents of ofiles[indx]
1017 if ((error == ENODEV || error == ENXIO) &&
1018 td->td_dupfd >= 0 && /* XXX from fdopen */
1020 dupfdopen(td, fdp, indx, td->td_dupfd, flags, error)) == 0) {
1021 td->td_retval[0] = indx;
1026 * Clean up the descriptor, but only if another thread hadn't
1027 * replaced or closed it.
1029 fdclose(fdp, fp, indx, td);
1032 if (error == ERESTART)
1037 vfslocked = NDHASGIANT(&nd);
1038 NDFREE(&nd, NDF_ONLY_PNBUF);
1043 if (fp->f_data == NULL)
1045 fp->f_flag = flags & FMASK;
1047 fp->f_type = (vp->v_type == VFIFO ? DTYPE_FIFO : DTYPE_VNODE);
1048 if (fp->f_ops == &badfileops)
1052 VOP_UNLOCK(vp, 0, td);
1053 if (flags & (O_EXLOCK | O_SHLOCK)) {
1054 lf.l_whence = SEEK_SET;
1057 if (flags & O_EXLOCK)
1058 lf.l_type = F_WRLCK;
1060 lf.l_type = F_RDLCK;
1062 if ((flags & FNONBLOCK) == 0)
1064 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf,
1067 fp->f_flag |= FHASLOCK;
1069 if (flags & O_TRUNC) {
1070 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
1072 VOP_LEASE(vp, td, td->td_ucred, LEASE_WRITE);
1075 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
1077 error = mac_check_vnode_write(td->td_ucred, fp->f_cred, vp);
1080 error = VOP_SETATTR(vp, &vat, td->td_ucred, td);
1081 VOP_UNLOCK(vp, 0, td);
1082 vn_finished_write(mp);
1086 VFS_UNLOCK_GIANT(vfslocked);
1088 * Release our private reference, leaving the one associated with
1089 * the descriptor table intact.
1092 td->td_retval[0] = indx;
1095 VFS_UNLOCK_GIANT(vfslocked);
1096 fdclose(fdp, fp, indx, td);
1105 #ifndef _SYS_SYSPROTO_H_
1106 struct ocreat_args {
1114 register struct ocreat_args /* {
1120 return (kern_open(td, uap->path, UIO_USERSPACE,
1121 O_WRONLY | O_CREAT | O_TRUNC, uap->mode));
1123 #endif /* COMPAT_43 */
1126 * Create a special file.
1128 #ifndef _SYS_SYSPROTO_H_
1138 register struct mknod_args /* {
1145 return (kern_mknod(td, uap->path, UIO_USERSPACE, uap->mode, uap->dev));
1149 kern_mknod(struct thread *td, char *path, enum uio_seg pathseg, int mode,
1157 struct nameidata nd;
1160 AUDIT_ARG(mode, mode);
1161 AUDIT_ARG(dev, dev);
1162 switch (mode & S_IFMT) {
1165 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
1168 error = priv_check(td, PRIV_VFS_MKNOD_BAD);
1171 error = priv_check(td, PRIV_VFS_MKNOD_WHT);
1181 NDINIT(&nd, CREATE, LOCKPARENT | SAVENAME | MPSAFE | AUDITVNODE1,
1183 if ((error = namei(&nd)) != 0)
1185 vfslocked = NDHASGIANT(&nd);
1188 NDFREE(&nd, NDF_ONLY_PNBUF);
1189 if (vp == nd.ni_dvp)
1194 VFS_UNLOCK_GIANT(vfslocked);
1198 FILEDESC_SLOCK(td->td_proc->p_fd);
1199 vattr.va_mode = (mode & ALLPERMS) &
1200 ~td->td_proc->p_fd->fd_cmask;
1201 FILEDESC_SUNLOCK(td->td_proc->p_fd);
1202 vattr.va_rdev = dev;
1205 switch (mode & S_IFMT) {
1206 case S_IFMT: /* used by badsect to flag bad sectors */
1207 vattr.va_type = VBAD;
1210 vattr.va_type = VCHR;
1213 vattr.va_type = VBLK;
1219 panic("kern_mknod: invalid mode");
1222 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1223 NDFREE(&nd, NDF_ONLY_PNBUF);
1225 VFS_UNLOCK_GIANT(vfslocked);
1226 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1231 if (error == 0 && !whiteout)
1232 error = mac_check_vnode_create(td->td_ucred, nd.ni_dvp,
1233 &nd.ni_cnd, &vattr);
1236 VOP_LEASE(nd.ni_dvp, td, td->td_ucred, LEASE_WRITE);
1238 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, CREATE);
1240 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp,
1241 &nd.ni_cnd, &vattr);
1246 NDFREE(&nd, NDF_ONLY_PNBUF);
1248 vn_finished_write(mp);
1249 VFS_UNLOCK_GIANT(vfslocked);
1254 * Create a named pipe.
1256 #ifndef _SYS_SYSPROTO_H_
1257 struct mkfifo_args {
1265 register struct mkfifo_args /* {
1271 return (kern_mkfifo(td, uap->path, UIO_USERSPACE, uap->mode));
1275 kern_mkfifo(struct thread *td, char *path, enum uio_seg pathseg, int mode)
1280 struct nameidata nd;
1283 AUDIT_ARG(mode, mode);
1286 NDINIT(&nd, CREATE, LOCKPARENT | SAVENAME | MPSAFE | AUDITVNODE1,
1288 if ((error = namei(&nd)) != 0)
1290 vfslocked = NDHASGIANT(&nd);
1291 if (nd.ni_vp != NULL) {
1292 NDFREE(&nd, NDF_ONLY_PNBUF);
1293 if (nd.ni_vp == nd.ni_dvp)
1298 VFS_UNLOCK_GIANT(vfslocked);
1301 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1302 NDFREE(&nd, NDF_ONLY_PNBUF);
1304 VFS_UNLOCK_GIANT(vfslocked);
1305 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1310 vattr.va_type = VFIFO;
1311 FILEDESC_SLOCK(td->td_proc->p_fd);
1312 vattr.va_mode = (mode & ALLPERMS) & ~td->td_proc->p_fd->fd_cmask;
1313 FILEDESC_SUNLOCK(td->td_proc->p_fd);
1315 error = mac_check_vnode_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1320 VOP_LEASE(nd.ni_dvp, td, td->td_ucred, LEASE_WRITE);
1321 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
1328 vn_finished_write(mp);
1329 VFS_UNLOCK_GIANT(vfslocked);
1330 NDFREE(&nd, NDF_ONLY_PNBUF);
1335 * Make a hard file link.
1337 #ifndef _SYS_SYSPROTO_H_
1346 register struct link_args /* {
1353 error = kern_link(td, uap->path, uap->link, UIO_USERSPACE);
1357 static int hardlink_check_uid = 0;
1358 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
1359 &hardlink_check_uid, 0,
1360 "Unprivileged processes cannot create hard links to files owned by other "
1362 static int hardlink_check_gid = 0;
1363 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
1364 &hardlink_check_gid, 0,
1365 "Unprivileged processes cannot create hard links to files owned by other "
1369 can_hardlink(struct vnode *vp, struct thread *td, struct ucred *cred)
1374 if (!hardlink_check_uid && !hardlink_check_gid)
1377 error = VOP_GETATTR(vp, &va, cred, td);
1381 if (hardlink_check_uid && cred->cr_uid != va.va_uid) {
1382 error = priv_check_cred(cred, PRIV_VFS_LINK,
1388 if (hardlink_check_gid && !groupmember(va.va_gid, cred)) {
1389 error = priv_check_cred(cred, PRIV_VFS_LINK,
1399 kern_link(struct thread *td, char *path, char *link, enum uio_seg segflg)
1403 struct nameidata nd;
1409 NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE | AUDITVNODE1, segflg, path, td);
1410 if ((error = namei(&nd)) != 0)
1412 vfslocked = NDHASGIANT(&nd);
1413 NDFREE(&nd, NDF_ONLY_PNBUF);
1415 if (vp->v_type == VDIR) {
1417 VFS_UNLOCK_GIANT(vfslocked);
1418 return (EPERM); /* POSIX */
1420 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0) {
1422 VFS_UNLOCK_GIANT(vfslocked);
1425 NDINIT(&nd, CREATE, LOCKPARENT | SAVENAME | MPSAFE | AUDITVNODE2,
1427 if ((error = namei(&nd)) == 0) {
1428 lvfslocked = NDHASGIANT(&nd);
1429 if (nd.ni_vp != NULL) {
1430 if (nd.ni_dvp == nd.ni_vp)
1436 } else if ((error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td))
1438 VOP_LEASE(nd.ni_dvp, td, td->td_ucred, LEASE_WRITE);
1439 VOP_LEASE(vp, td, td->td_ucred, LEASE_WRITE);
1440 error = can_hardlink(vp, td, td->td_ucred);
1443 error = mac_check_vnode_link(td->td_ucred,
1444 nd.ni_dvp, vp, &nd.ni_cnd);
1447 error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
1448 VOP_UNLOCK(vp, 0, td);
1451 NDFREE(&nd, NDF_ONLY_PNBUF);
1452 VFS_UNLOCK_GIANT(lvfslocked);
1455 vn_finished_write(mp);
1456 VFS_UNLOCK_GIANT(vfslocked);
1461 * Make a symbolic link.
1463 #ifndef _SYS_SYSPROTO_H_
1464 struct symlink_args {
1472 register struct symlink_args /* {
1478 return (kern_symlink(td, uap->path, uap->link, UIO_USERSPACE));
1482 kern_symlink(struct thread *td, char *path, char *link, enum uio_seg segflg)
1488 struct nameidata nd;
1491 if (segflg == UIO_SYSSPACE) {
1494 syspath = uma_zalloc(namei_zone, M_WAITOK);
1495 if ((error = copyinstr(path, syspath, MAXPATHLEN, NULL)) != 0)
1498 AUDIT_ARG(text, syspath);
1501 NDINIT(&nd, CREATE, LOCKPARENT | SAVENAME | MPSAFE | AUDITVNODE1,
1503 if ((error = namei(&nd)) != 0)
1505 vfslocked = NDHASGIANT(&nd);
1507 NDFREE(&nd, NDF_ONLY_PNBUF);
1508 if (nd.ni_vp == nd.ni_dvp)
1513 VFS_UNLOCK_GIANT(vfslocked);
1517 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1518 NDFREE(&nd, NDF_ONLY_PNBUF);
1520 VFS_UNLOCK_GIANT(vfslocked);
1521 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1526 FILEDESC_SLOCK(td->td_proc->p_fd);
1527 vattr.va_mode = ACCESSPERMS &~ td->td_proc->p_fd->fd_cmask;
1528 FILEDESC_SUNLOCK(td->td_proc->p_fd);
1530 vattr.va_type = VLNK;
1531 error = mac_check_vnode_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1536 VOP_LEASE(nd.ni_dvp, td, td->td_ucred, LEASE_WRITE);
1537 error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr, syspath);
1543 NDFREE(&nd, NDF_ONLY_PNBUF);
1545 vn_finished_write(mp);
1546 VFS_UNLOCK_GIANT(vfslocked);
1548 if (segflg != UIO_SYSSPACE)
1549 uma_zfree(namei_zone, syspath);
1554 * Delete a whiteout from the filesystem.
1559 register struct undelete_args /* {
1565 struct nameidata nd;
1570 NDINIT(&nd, DELETE, LOCKPARENT | DOWHITEOUT | MPSAFE | AUDITVNODE1,
1571 UIO_USERSPACE, uap->path, td);
1575 vfslocked = NDHASGIANT(&nd);
1577 if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & ISWHITEOUT)) {
1578 NDFREE(&nd, NDF_ONLY_PNBUF);
1579 if (nd.ni_vp == nd.ni_dvp)
1585 VFS_UNLOCK_GIANT(vfslocked);
1588 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1589 NDFREE(&nd, NDF_ONLY_PNBUF);
1591 VFS_UNLOCK_GIANT(vfslocked);
1592 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1596 VOP_LEASE(nd.ni_dvp, td, td->td_ucred, LEASE_WRITE);
1597 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, DELETE);
1598 NDFREE(&nd, NDF_ONLY_PNBUF);
1600 vn_finished_write(mp);
1601 VFS_UNLOCK_GIANT(vfslocked);
1606 * Delete a name from the filesystem.
1608 #ifndef _SYS_SYSPROTO_H_
1609 struct unlink_args {
1616 struct unlink_args /* {
1622 error = kern_unlink(td, uap->path, UIO_USERSPACE);
1627 kern_unlink(struct thread *td, char *path, enum uio_seg pathseg)
1632 struct nameidata nd;
1637 NDINIT(&nd, DELETE, LOCKPARENT | LOCKLEAF | MPSAFE | AUDITVNODE1,
1639 if ((error = namei(&nd)) != 0)
1640 return (error == EINVAL ? EPERM : error);
1641 vfslocked = NDHASGIANT(&nd);
1643 if (vp->v_type == VDIR)
1644 error = EPERM; /* POSIX */
1647 * The root of a mounted filesystem cannot be deleted.
1649 * XXX: can this only be a VDIR case?
1651 if (vp->v_vflag & VV_ROOT)
1655 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1656 NDFREE(&nd, NDF_ONLY_PNBUF);
1658 if (vp == nd.ni_dvp)
1662 VFS_UNLOCK_GIANT(vfslocked);
1663 if ((error = vn_start_write(NULL, &mp,
1664 V_XSLEEP | PCATCH)) != 0)
1669 error = mac_check_vnode_delete(td->td_ucred, nd.ni_dvp, vp,
1674 VOP_LEASE(nd.ni_dvp, td, td->td_ucred, LEASE_WRITE);
1675 error = VOP_REMOVE(nd.ni_dvp, vp, &nd.ni_cnd);
1679 vn_finished_write(mp);
1681 NDFREE(&nd, NDF_ONLY_PNBUF);
1683 if (vp == nd.ni_dvp)
1687 VFS_UNLOCK_GIANT(vfslocked);
1692 * Reposition read/write file offset.
1694 #ifndef _SYS_SYSPROTO_H_
1705 register struct lseek_args /* {
1712 struct ucred *cred = td->td_ucred;
1720 if ((error = fget(td, uap->fd, &fp)) != 0)
1722 if (!(fp->f_ops->fo_flags & DFLAG_SEEKABLE)) {
1727 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
1728 noneg = (vp->v_type != VCHR);
1729 offset = uap->offset;
1730 switch (uap->whence) {
1733 (fp->f_offset < 0 ||
1734 (offset > 0 && fp->f_offset > OFF_MAX - offset))) {
1738 offset += fp->f_offset;
1741 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
1742 error = VOP_GETATTR(vp, &vattr, cred, td);
1743 VOP_UNLOCK(vp, 0, td);
1747 (vattr.va_size > OFF_MAX ||
1748 (offset > 0 && vattr.va_size > OFF_MAX - offset))) {
1752 offset += vattr.va_size;
1757 error = fo_ioctl(fp, FIOSEEKDATA, &offset, cred, td);
1760 error = fo_ioctl(fp, FIOSEEKHOLE, &offset, cred, td);
1765 if (error == 0 && noneg && offset < 0)
1769 fp->f_offset = offset;
1770 *(off_t *)(td->td_retval) = fp->f_offset;
1773 VFS_UNLOCK_GIANT(vfslocked);
1777 #if defined(COMPAT_43)
1779 * Reposition read/write file offset.
1781 #ifndef _SYS_SYSPROTO_H_
1782 struct olseek_args {
1791 register struct olseek_args /* {
1797 struct lseek_args /* {
1806 nuap.offset = uap->offset;
1807 nuap.whence = uap->whence;
1808 error = lseek(td, &nuap);
1811 #endif /* COMPAT_43 */
1814 * Check access permissions using passed credentials.
1817 vn_access(vp, user_flags, cred, td)
1825 /* Flags == 0 means only check for existence. */
1829 if (user_flags & R_OK)
1831 if (user_flags & W_OK)
1833 if (user_flags & X_OK)
1836 error = mac_check_vnode_access(cred, vp, flags);
1840 if ((flags & VWRITE) == 0 || (error = vn_writechk(vp)) == 0)
1841 error = VOP_ACCESS(vp, flags, cred, td);
1847 * Check access permissions using "real" credentials.
1849 #ifndef _SYS_SYSPROTO_H_
1850 struct access_args {
1858 register struct access_args /* {
1864 return (kern_access(td, uap->path, UIO_USERSPACE, uap->flags));
1868 kern_access(struct thread *td, char *path, enum uio_seg pathseg, int flags)
1870 struct ucred *cred, *tmpcred;
1871 register struct vnode *vp;
1872 struct nameidata nd;
1877 * Create and modify a temporary credential instead of one that
1878 * is potentially shared. This could also mess up socket
1879 * buffer accounting which can run in an interrupt context.
1881 cred = td->td_ucred;
1882 tmpcred = crdup(cred);
1883 tmpcred->cr_uid = cred->cr_ruid;
1884 tmpcred->cr_groups[0] = cred->cr_rgid;
1885 td->td_ucred = tmpcred;
1886 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | MPSAFE | AUDITVNODE1,
1888 if ((error = namei(&nd)) != 0)
1890 vfslocked = NDHASGIANT(&nd);
1893 error = vn_access(vp, flags, tmpcred, td);
1894 NDFREE(&nd, NDF_ONLY_PNBUF);
1896 VFS_UNLOCK_GIANT(vfslocked);
1898 td->td_ucred = cred;
1904 * Check access permissions using "effective" credentials.
1906 #ifndef _SYS_SYSPROTO_H_
1907 struct eaccess_args {
1915 register struct eaccess_args /* {
1921 return (kern_eaccess(td, uap->path, UIO_USERSPACE, uap->flags));
1925 kern_eaccess(struct thread *td, char *path, enum uio_seg pathseg, int flags)
1927 struct nameidata nd;
1932 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | MPSAFE | AUDITVNODE1,
1934 if ((error = namei(&nd)) != 0)
1937 vfslocked = NDHASGIANT(&nd);
1938 error = vn_access(vp, flags, td->td_ucred, td);
1939 NDFREE(&nd, NDF_ONLY_PNBUF);
1941 VFS_UNLOCK_GIANT(vfslocked);
1945 #if defined(COMPAT_43)
1947 * Get file status; this version follows links.
1949 #ifndef _SYS_SYSPROTO_H_
1958 register struct ostat_args /* {
1967 error = kern_stat(td, uap->path, UIO_USERSPACE, &sb);
1971 error = copyout(&osb, uap->ub, sizeof (osb));
1976 * Get file status; this version does not follow links.
1978 #ifndef _SYS_SYSPROTO_H_
1979 struct olstat_args {
1987 register struct olstat_args /* {
1996 error = kern_lstat(td, uap->path, UIO_USERSPACE, &sb);
2000 error = copyout(&osb, uap->ub, sizeof (osb));
2005 * Convert from an old to a new stat structure.
2013 ost->st_dev = st->st_dev;
2014 ost->st_ino = st->st_ino;
2015 ost->st_mode = st->st_mode;
2016 ost->st_nlink = st->st_nlink;
2017 ost->st_uid = st->st_uid;
2018 ost->st_gid = st->st_gid;
2019 ost->st_rdev = st->st_rdev;
2020 if (st->st_size < (quad_t)1 << 32)
2021 ost->st_size = st->st_size;
2024 ost->st_atime = st->st_atime;
2025 ost->st_mtime = st->st_mtime;
2026 ost->st_ctime = st->st_ctime;
2027 ost->st_blksize = st->st_blksize;
2028 ost->st_blocks = st->st_blocks;
2029 ost->st_flags = st->st_flags;
2030 ost->st_gen = st->st_gen;
2032 #endif /* COMPAT_43 */
2035 * Get file status; this version follows links.
2037 #ifndef _SYS_SYSPROTO_H_
2046 register struct stat_args /* {
2054 error = kern_stat(td, uap->path, UIO_USERSPACE, &sb);
2056 error = copyout(&sb, uap->ub, sizeof (sb));
2061 kern_stat(struct thread *td, char *path, enum uio_seg pathseg, struct stat *sbp)
2063 struct nameidata nd;
2065 int error, vfslocked;
2068 FOLLOW | LOCKSHARED | LOCKLEAF | MPSAFE | AUDITVNODE1,
2070 if ((error = namei(&nd)) != 0)
2072 vfslocked = NDHASGIANT(&nd);
2073 error = vn_stat(nd.ni_vp, &sb, td->td_ucred, NOCRED, td);
2074 NDFREE(&nd, NDF_ONLY_PNBUF);
2076 VFS_UNLOCK_GIANT(vfslocked);
2077 if (mtx_owned(&Giant))
2078 printf("stat(%d): %s\n", vfslocked, path);
2086 * Get file status; this version does not follow links.
2088 #ifndef _SYS_SYSPROTO_H_
2097 register struct lstat_args /* {
2105 error = kern_lstat(td, uap->path, UIO_USERSPACE, &sb);
2107 error = copyout(&sb, uap->ub, sizeof (sb));
2112 kern_lstat(struct thread *td, char *path, enum uio_seg pathseg, struct stat *sbp)
2116 struct nameidata nd;
2117 int error, vfslocked;
2120 NOFOLLOW | LOCKLEAF | LOCKSHARED | MPSAFE | AUDITVNODE1,
2122 if ((error = namei(&nd)) != 0)
2124 vfslocked = NDHASGIANT(&nd);
2126 error = vn_stat(vp, &sb, td->td_ucred, NOCRED, td);
2127 NDFREE(&nd, NDF_ONLY_PNBUF);
2129 VFS_UNLOCK_GIANT(vfslocked);
2137 * Implementation of the NetBSD [l]stat() functions.
2144 bzero(nsb, sizeof *nsb);
2145 nsb->st_dev = sb->st_dev;
2146 nsb->st_ino = sb->st_ino;
2147 nsb->st_mode = sb->st_mode;
2148 nsb->st_nlink = sb->st_nlink;
2149 nsb->st_uid = sb->st_uid;
2150 nsb->st_gid = sb->st_gid;
2151 nsb->st_rdev = sb->st_rdev;
2152 nsb->st_atimespec = sb->st_atimespec;
2153 nsb->st_mtimespec = sb->st_mtimespec;
2154 nsb->st_ctimespec = sb->st_ctimespec;
2155 nsb->st_size = sb->st_size;
2156 nsb->st_blocks = sb->st_blocks;
2157 nsb->st_blksize = sb->st_blksize;
2158 nsb->st_flags = sb->st_flags;
2159 nsb->st_gen = sb->st_gen;
2160 nsb->st_birthtimespec = sb->st_birthtimespec;
2163 #ifndef _SYS_SYSPROTO_H_
2172 register struct nstat_args /* {
2181 error = kern_stat(td, uap->path, UIO_USERSPACE, &sb);
2184 cvtnstat(&sb, &nsb);
2185 error = copyout(&nsb, uap->ub, sizeof (nsb));
2190 * NetBSD lstat. Get file status; this version does not follow links.
2192 #ifndef _SYS_SYSPROTO_H_
2201 register struct nlstat_args /* {
2210 error = kern_lstat(td, uap->path, UIO_USERSPACE, &sb);
2213 cvtnstat(&sb, &nsb);
2214 error = copyout(&nsb, uap->ub, sizeof (nsb));
2219 * Get configurable pathname variables.
2221 #ifndef _SYS_SYSPROTO_H_
2222 struct pathconf_args {
2230 register struct pathconf_args /* {
2236 return (kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name));
2240 kern_pathconf(struct thread *td, char *path, enum uio_seg pathseg, int name)
2242 struct nameidata nd;
2243 int error, vfslocked;
2245 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | MPSAFE | AUDITVNODE1,
2247 if ((error = namei(&nd)) != 0)
2249 vfslocked = NDHASGIANT(&nd);
2250 NDFREE(&nd, NDF_ONLY_PNBUF);
2252 /* If asynchronous I/O is available, it works for all files. */
2253 if (name == _PC_ASYNC_IO)
2254 td->td_retval[0] = async_io_version;
2256 error = VOP_PATHCONF(nd.ni_vp, name, td->td_retval);
2258 VFS_UNLOCK_GIANT(vfslocked);
2263 * Return target name of a symbolic link.
2265 #ifndef _SYS_SYSPROTO_H_
2266 struct readlink_args {
2275 register struct readlink_args /* {
2282 return (kern_readlink(td, uap->path, UIO_USERSPACE, uap->buf,
2283 UIO_USERSPACE, uap->count));
2287 kern_readlink(struct thread *td, char *path, enum uio_seg pathseg, char *buf,
2288 enum uio_seg bufseg, int count)
2290 register struct vnode *vp;
2294 struct nameidata nd;
2297 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | MPSAFE | AUDITVNODE1,
2299 if ((error = namei(&nd)) != 0)
2301 NDFREE(&nd, NDF_ONLY_PNBUF);
2302 vfslocked = NDHASGIANT(&nd);
2305 error = mac_check_vnode_readlink(td->td_ucred, vp);
2308 VFS_UNLOCK_GIANT(vfslocked);
2312 if (vp->v_type != VLNK)
2315 aiov.iov_base = buf;
2316 aiov.iov_len = count;
2317 auio.uio_iov = &aiov;
2318 auio.uio_iovcnt = 1;
2319 auio.uio_offset = 0;
2320 auio.uio_rw = UIO_READ;
2321 auio.uio_segflg = bufseg;
2323 auio.uio_resid = count;
2324 error = VOP_READLINK(vp, &auio, td->td_ucred);
2327 VFS_UNLOCK_GIANT(vfslocked);
2328 td->td_retval[0] = count - auio.uio_resid;
2333 * Common implementation code for chflags() and fchflags().
2336 setfflags(td, vp, flags)
2346 * Prevent non-root users from setting flags on devices. When
2347 * a device is reused, users can retain ownership of the device
2348 * if they are allowed to set flags and programs assume that
2349 * chown can't fail when done as root.
2351 if (vp->v_type == VCHR || vp->v_type == VBLK) {
2352 error = priv_check_cred(td->td_ucred, PRIV_VFS_CHFLAGS_DEV,
2358 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2360 VOP_LEASE(vp, td, td->td_ucred, LEASE_WRITE);
2361 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2363 vattr.va_flags = flags;
2365 error = mac_check_vnode_setflags(td->td_ucred, vp, vattr.va_flags);
2368 error = VOP_SETATTR(vp, &vattr, td->td_ucred, td);
2369 VOP_UNLOCK(vp, 0, td);
2370 vn_finished_write(mp);
2375 * Change flags of a file given a path name.
2377 #ifndef _SYS_SYSPROTO_H_
2378 struct chflags_args {
2386 register struct chflags_args /* {
2392 struct nameidata nd;
2395 AUDIT_ARG(fflags, uap->flags);
2396 NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE | AUDITVNODE1, UIO_USERSPACE,
2398 if ((error = namei(&nd)) != 0)
2400 NDFREE(&nd, NDF_ONLY_PNBUF);
2401 vfslocked = NDHASGIANT(&nd);
2402 error = setfflags(td, nd.ni_vp, uap->flags);
2404 VFS_UNLOCK_GIANT(vfslocked);
2409 * Same as chflags() but doesn't follow symlinks.
2414 register struct lchflags_args /* {
2420 struct nameidata nd;
2423 AUDIT_ARG(fflags, uap->flags);
2424 NDINIT(&nd, LOOKUP, NOFOLLOW | MPSAFE | AUDITVNODE1, UIO_USERSPACE,
2426 if ((error = namei(&nd)) != 0)
2428 vfslocked = NDHASGIANT(&nd);
2429 NDFREE(&nd, NDF_ONLY_PNBUF);
2430 error = setfflags(td, nd.ni_vp, uap->flags);
2432 VFS_UNLOCK_GIANT(vfslocked);
2437 * Change flags of a file given a file descriptor.
2439 #ifndef _SYS_SYSPROTO_H_
2440 struct fchflags_args {
2448 register struct fchflags_args /* {
2457 AUDIT_ARG(fd, uap->fd);
2458 AUDIT_ARG(fflags, uap->flags);
2459 if ((error = getvnode(td->td_proc->p_fd, uap->fd, &fp)) != 0)
2461 vfslocked = VFS_LOCK_GIANT(fp->f_vnode->v_mount);
2463 vn_lock(fp->f_vnode, LK_EXCLUSIVE | LK_RETRY, td);
2464 AUDIT_ARG(vnode, fp->f_vnode, ARG_VNODE1);
2465 VOP_UNLOCK(fp->f_vnode, 0, td);
2467 error = setfflags(td, fp->f_vnode, uap->flags);
2468 VFS_UNLOCK_GIANT(vfslocked);
2474 * Common implementation code for chmod(), lchmod() and fchmod().
2477 setfmode(td, vp, mode)
2486 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2488 VOP_LEASE(vp, td, td->td_ucred, LEASE_WRITE);
2489 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2491 vattr.va_mode = mode & ALLPERMS;
2493 error = mac_check_vnode_setmode(td->td_ucred, vp, vattr.va_mode);
2496 error = VOP_SETATTR(vp, &vattr, td->td_ucred, td);
2497 VOP_UNLOCK(vp, 0, td);
2498 vn_finished_write(mp);
2503 * Change mode of a file given path name.
2505 #ifndef _SYS_SYSPROTO_H_
2514 register struct chmod_args /* {
2520 return (kern_chmod(td, uap->path, UIO_USERSPACE, uap->mode));
2524 kern_chmod(struct thread *td, char *path, enum uio_seg pathseg, int mode)
2527 struct nameidata nd;
2530 AUDIT_ARG(mode, mode);
2531 NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE | AUDITVNODE1, pathseg, path, td);
2532 if ((error = namei(&nd)) != 0)
2534 vfslocked = NDHASGIANT(&nd);
2535 NDFREE(&nd, NDF_ONLY_PNBUF);
2536 error = setfmode(td, nd.ni_vp, mode);
2538 VFS_UNLOCK_GIANT(vfslocked);
2543 * Change mode of a file given path name (don't follow links.)
2545 #ifndef _SYS_SYSPROTO_H_
2546 struct lchmod_args {
2554 register struct lchmod_args /* {
2560 struct nameidata nd;
2563 AUDIT_ARG(mode, (mode_t)uap->mode);
2564 NDINIT(&nd, LOOKUP, NOFOLLOW | MPSAFE | AUDITVNODE1, UIO_USERSPACE,
2566 if ((error = namei(&nd)) != 0)
2568 vfslocked = NDHASGIANT(&nd);
2569 NDFREE(&nd, NDF_ONLY_PNBUF);
2570 error = setfmode(td, nd.ni_vp, uap->mode);
2572 VFS_UNLOCK_GIANT(vfslocked);
2577 * Change mode of a file given a file descriptor.
2579 #ifndef _SYS_SYSPROTO_H_
2580 struct fchmod_args {
2588 register struct fchmod_args /* {
2597 AUDIT_ARG(fd, uap->fd);
2598 AUDIT_ARG(mode, uap->mode);
2599 if ((error = getvnode(td->td_proc->p_fd, uap->fd, &fp)) != 0)
2601 vfslocked = VFS_LOCK_GIANT(fp->f_vnode->v_mount);
2603 vn_lock(fp->f_vnode, LK_EXCLUSIVE | LK_RETRY, td);
2604 AUDIT_ARG(vnode, fp->f_vnode, ARG_VNODE1);
2605 VOP_UNLOCK(fp->f_vnode, 0, td);
2607 error = setfmode(td, fp->f_vnode, uap->mode);
2608 VFS_UNLOCK_GIANT(vfslocked);
2614 * Common implementation for chown(), lchown(), and fchown()
2617 setfown(td, vp, uid, gid)
2627 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2629 VOP_LEASE(vp, td, td->td_ucred, LEASE_WRITE);
2630 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2635 error = mac_check_vnode_setowner(td->td_ucred, vp, vattr.va_uid,
2639 error = VOP_SETATTR(vp, &vattr, td->td_ucred, td);
2640 VOP_UNLOCK(vp, 0, td);
2641 vn_finished_write(mp);
2646 * Set ownership given a path name.
2648 #ifndef _SYS_SYSPROTO_H_
2658 register struct chown_args /* {
2665 return (kern_chown(td, uap->path, UIO_USERSPACE, uap->uid, uap->gid));
2669 kern_chown(struct thread *td, char *path, enum uio_seg pathseg, int uid,
2673 struct nameidata nd;
2676 AUDIT_ARG(owner, uid, gid);
2677 NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE | AUDITVNODE1, pathseg, path, td);
2678 if ((error = namei(&nd)) != 0)
2680 vfslocked = NDHASGIANT(&nd);
2681 NDFREE(&nd, NDF_ONLY_PNBUF);
2682 error = setfown(td, nd.ni_vp, uid, gid);
2684 VFS_UNLOCK_GIANT(vfslocked);
2689 * Set ownership given a path name, do not cross symlinks.
2691 #ifndef _SYS_SYSPROTO_H_
2692 struct lchown_args {
2701 register struct lchown_args /* {
2708 return (kern_lchown(td, uap->path, UIO_USERSPACE, uap->uid, uap->gid));
2712 kern_lchown(struct thread *td, char *path, enum uio_seg pathseg, int uid,
2716 struct nameidata nd;
2719 AUDIT_ARG(owner, uid, gid);
2720 NDINIT(&nd, LOOKUP, NOFOLLOW | MPSAFE | AUDITVNODE1, pathseg, path, td);
2721 if ((error = namei(&nd)) != 0)
2723 vfslocked = NDHASGIANT(&nd);
2724 NDFREE(&nd, NDF_ONLY_PNBUF);
2725 error = setfown(td, nd.ni_vp, uid, gid);
2727 VFS_UNLOCK_GIANT(vfslocked);
2732 * Set ownership given a file descriptor.
2734 #ifndef _SYS_SYSPROTO_H_
2735 struct fchown_args {
2744 register struct fchown_args /* {
2754 AUDIT_ARG(fd, uap->fd);
2755 AUDIT_ARG(owner, uap->uid, uap->gid);
2756 if ((error = getvnode(td->td_proc->p_fd, uap->fd, &fp)) != 0)
2758 vfslocked = VFS_LOCK_GIANT(fp->f_vnode->v_mount);
2760 vn_lock(fp->f_vnode, LK_EXCLUSIVE | LK_RETRY, td);
2761 AUDIT_ARG(vnode, fp->f_vnode, ARG_VNODE1);
2762 VOP_UNLOCK(fp->f_vnode, 0, td);
2764 error = setfown(td, fp->f_vnode, uap->uid, uap->gid);
2765 VFS_UNLOCK_GIANT(vfslocked);
2771 * Common implementation code for utimes(), lutimes(), and futimes().
2774 getutimes(usrtvp, tvpseg, tsp)
2775 const struct timeval *usrtvp;
2776 enum uio_seg tvpseg;
2777 struct timespec *tsp;
2779 struct timeval tv[2];
2780 const struct timeval *tvp;
2783 if (usrtvp == NULL) {
2785 TIMEVAL_TO_TIMESPEC(&tv[0], &tsp[0]);
2788 if (tvpseg == UIO_SYSSPACE) {
2791 if ((error = copyin(usrtvp, tv, sizeof(tv))) != 0)
2796 if (tvp[0].tv_usec < 0 || tvp[0].tv_usec >= 1000000 ||
2797 tvp[1].tv_usec < 0 || tvp[1].tv_usec >= 1000000)
2799 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
2800 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
2806 * Common implementation code for utimes(), lutimes(), and futimes().
2809 setutimes(td, vp, ts, numtimes, nullflag)
2812 const struct timespec *ts;
2816 int error, setbirthtime;
2820 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2822 VOP_LEASE(vp, td, td->td_ucred, LEASE_WRITE);
2823 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2825 if (numtimes < 3 && VOP_GETATTR(vp, &vattr, td->td_ucred, td) == 0 &&
2826 timespeccmp(&ts[1], &vattr.va_birthtime, < ))
2829 vattr.va_atime = ts[0];
2830 vattr.va_mtime = ts[1];
2832 vattr.va_birthtime = ts[1];
2834 vattr.va_birthtime = ts[2];
2836 vattr.va_vaflags |= VA_UTIMES_NULL;
2838 error = mac_check_vnode_setutimes(td->td_ucred, vp, vattr.va_atime,
2842 error = VOP_SETATTR(vp, &vattr, td->td_ucred, td);
2843 VOP_UNLOCK(vp, 0, td);
2844 vn_finished_write(mp);
2849 * Set the access and modification times of a file.
2851 #ifndef _SYS_SYSPROTO_H_
2852 struct utimes_args {
2854 struct timeval *tptr;
2860 register struct utimes_args /* {
2862 struct timeval *tptr;
2866 return (kern_utimes(td, uap->path, UIO_USERSPACE, uap->tptr,
2871 kern_utimes(struct thread *td, char *path, enum uio_seg pathseg,
2872 struct timeval *tptr, enum uio_seg tptrseg)
2874 struct timespec ts[2];
2876 struct nameidata nd;
2879 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
2881 NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE | AUDITVNODE1, pathseg, path, td);
2882 if ((error = namei(&nd)) != 0)
2884 vfslocked = NDHASGIANT(&nd);
2885 NDFREE(&nd, NDF_ONLY_PNBUF);
2886 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
2888 VFS_UNLOCK_GIANT(vfslocked);
2893 * Set the access and modification times of a file.
2895 #ifndef _SYS_SYSPROTO_H_
2896 struct lutimes_args {
2898 struct timeval *tptr;
2904 register struct lutimes_args /* {
2906 struct timeval *tptr;
2910 return (kern_lutimes(td, uap->path, UIO_USERSPACE, uap->tptr,
2915 kern_lutimes(struct thread *td, char *path, enum uio_seg pathseg,
2916 struct timeval *tptr, enum uio_seg tptrseg)
2918 struct timespec ts[2];
2920 struct nameidata nd;
2923 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
2925 NDINIT(&nd, LOOKUP, NOFOLLOW | MPSAFE | AUDITVNODE1, pathseg, path, td);
2926 if ((error = namei(&nd)) != 0)
2928 vfslocked = NDHASGIANT(&nd);
2929 NDFREE(&nd, NDF_ONLY_PNBUF);
2930 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
2932 VFS_UNLOCK_GIANT(vfslocked);
2937 * Set the access and modification times of a file.
2939 #ifndef _SYS_SYSPROTO_H_
2940 struct futimes_args {
2942 struct timeval *tptr;
2948 register struct futimes_args /* {
2950 struct timeval *tptr;
2954 return (kern_futimes(td, uap->fd, uap->tptr, UIO_USERSPACE));
2958 kern_futimes(struct thread *td, int fd, struct timeval *tptr,
2959 enum uio_seg tptrseg)
2961 struct timespec ts[2];
2967 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
2969 if ((error = getvnode(td->td_proc->p_fd, fd, &fp)) != 0)
2971 vfslocked = VFS_LOCK_GIANT(fp->f_vnode->v_mount);
2973 vn_lock(fp->f_vnode, LK_EXCLUSIVE | LK_RETRY, td);
2974 AUDIT_ARG(vnode, fp->f_vnode, ARG_VNODE1);
2975 VOP_UNLOCK(fp->f_vnode, 0, td);
2977 error = setutimes(td, fp->f_vnode, ts, 2, tptr == NULL);
2978 VFS_UNLOCK_GIANT(vfslocked);
2984 * Truncate a file given its path name.
2986 #ifndef _SYS_SYSPROTO_H_
2987 struct truncate_args {
2996 register struct truncate_args /* {
3003 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3007 kern_truncate(struct thread *td, char *path, enum uio_seg pathseg, off_t length)
3013 struct nameidata nd;
3018 NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE | AUDITVNODE1, pathseg, path, td);
3019 if ((error = namei(&nd)) != 0)
3021 vfslocked = NDHASGIANT(&nd);
3023 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0) {
3025 VFS_UNLOCK_GIANT(vfslocked);
3028 NDFREE(&nd, NDF_ONLY_PNBUF);
3029 VOP_LEASE(vp, td, td->td_ucred, LEASE_WRITE);
3030 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
3031 if (vp->v_type == VDIR)
3034 else if ((error = mac_check_vnode_write(td->td_ucred, NOCRED, vp))) {
3037 else if ((error = vn_writechk(vp)) == 0 &&
3038 (error = VOP_ACCESS(vp, VWRITE, td->td_ucred, td)) == 0) {
3040 vattr.va_size = length;
3041 error = VOP_SETATTR(vp, &vattr, td->td_ucred, td);
3044 vn_finished_write(mp);
3045 VFS_UNLOCK_GIANT(vfslocked);
3050 * Truncate a file given a file descriptor.
3052 #ifndef _SYS_SYSPROTO_H_
3053 struct ftruncate_args {
3062 register struct ftruncate_args /* {
3075 AUDIT_ARG(fd, uap->fd);
3076 if (uap->length < 0)
3078 if ((error = getvnode(td->td_proc->p_fd, uap->fd, &fp)) != 0)
3080 if ((fp->f_flag & FWRITE) == 0) {
3085 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
3086 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
3088 VOP_LEASE(vp, td, td->td_ucred, LEASE_WRITE);
3089 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
3090 AUDIT_ARG(vnode, vp, ARG_VNODE1);
3091 if (vp->v_type == VDIR)
3094 else if ((error = mac_check_vnode_write(td->td_ucred, fp->f_cred,
3098 else if ((error = vn_writechk(vp)) == 0) {
3100 vattr.va_size = uap->length;
3101 error = VOP_SETATTR(vp, &vattr, fp->f_cred, td);
3103 VOP_UNLOCK(vp, 0, td);
3104 vn_finished_write(mp);
3106 VFS_UNLOCK_GIANT(vfslocked);
3111 #if defined(COMPAT_43)
3113 * Truncate a file given its path name.
3115 #ifndef _SYS_SYSPROTO_H_
3116 struct otruncate_args {
3124 register struct otruncate_args /* {
3129 struct truncate_args /* {
3135 nuap.path = uap->path;
3136 nuap.length = uap->length;
3137 return (truncate(td, &nuap));
3141 * Truncate a file given a file descriptor.
3143 #ifndef _SYS_SYSPROTO_H_
3144 struct oftruncate_args {
3152 register struct oftruncate_args /* {
3157 struct ftruncate_args /* {
3164 nuap.length = uap->length;
3165 return (ftruncate(td, &nuap));
3167 #endif /* COMPAT_43 */
3170 * Sync an open file.
3172 #ifndef _SYS_SYSPROTO_H_
3180 struct fsync_args /* {
3190 AUDIT_ARG(fd, uap->fd);
3191 if ((error = getvnode(td->td_proc->p_fd, uap->fd, &fp)) != 0)
3194 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
3195 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
3197 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
3198 AUDIT_ARG(vnode, vp, ARG_VNODE1);
3199 if (vp->v_object != NULL) {
3200 VM_OBJECT_LOCK(vp->v_object);
3201 vm_object_page_clean(vp->v_object, 0, 0, 0);
3202 VM_OBJECT_UNLOCK(vp->v_object);
3204 error = VOP_FSYNC(vp, MNT_WAIT, td);
3206 VOP_UNLOCK(vp, 0, td);
3207 vn_finished_write(mp);
3209 VFS_UNLOCK_GIANT(vfslocked);
3215 * Rename files. Source and destination must either both be directories, or
3216 * both not be directories. If target is a directory, it must be empty.
3218 #ifndef _SYS_SYSPROTO_H_
3219 struct rename_args {
3227 register struct rename_args /* {
3233 return (kern_rename(td, uap->from, uap->to, UIO_USERSPACE));
3237 kern_rename(struct thread *td, char *from, char *to, enum uio_seg pathseg)
3239 struct mount *mp = NULL;
3240 struct vnode *tvp, *fvp, *tdvp;
3241 struct nameidata fromnd, tond;
3248 NDINIT(&fromnd, DELETE, LOCKPARENT | LOCKLEAF | SAVESTART | MPSAFE |
3249 AUDITVNODE1, pathseg, from, td);
3251 NDINIT(&fromnd, DELETE, WANTPARENT | SAVESTART | MPSAFE |
3252 AUDITVNODE1, pathseg, from, td);
3254 if ((error = namei(&fromnd)) != 0)
3256 fvfslocked = NDHASGIANT(&fromnd);
3259 error = mac_check_vnode_rename_from(td->td_ucred, fromnd.ni_dvp,
3260 fromnd.ni_vp, &fromnd.ni_cnd);
3261 VOP_UNLOCK(fromnd.ni_dvp, 0, td);
3262 if (fromnd.ni_dvp != fromnd.ni_vp)
3263 VOP_UNLOCK(fromnd.ni_vp, 0, td);
3267 error = vn_start_write(fvp, &mp, V_WAIT | PCATCH);
3269 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3270 vrele(fromnd.ni_dvp);
3274 NDINIT(&tond, RENAME, LOCKPARENT | LOCKLEAF | NOCACHE | SAVESTART |
3275 MPSAFE | AUDITVNODE2, pathseg, to, td);
3276 if (fromnd.ni_vp->v_type == VDIR)
3277 tond.ni_cnd.cn_flags |= WILLBEDIR;
3278 if ((error = namei(&tond)) != 0) {
3279 /* Translate error code for rename("dir1", "dir2/."). */
3280 if (error == EISDIR && fvp->v_type == VDIR)
3282 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3283 vrele(fromnd.ni_dvp);
3285 vn_finished_write(mp);
3288 tvfslocked = NDHASGIANT(&tond);
3292 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
3295 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
3303 * If the source is the same as the destination (that is, if they
3304 * are links to the same vnode), then there is nothing to do.
3310 error = mac_check_vnode_rename_to(td->td_ucred, tdvp,
3311 tond.ni_vp, fromnd.ni_dvp == tdvp, &tond.ni_cnd);
3315 VOP_LEASE(tdvp, td, td->td_ucred, LEASE_WRITE);
3316 if (fromnd.ni_dvp != tdvp) {
3317 VOP_LEASE(fromnd.ni_dvp, td, td->td_ucred, LEASE_WRITE);
3320 VOP_LEASE(tvp, td, td->td_ucred, LEASE_WRITE);
3322 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
3323 tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
3324 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3325 NDFREE(&tond, NDF_ONLY_PNBUF);
3327 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3328 NDFREE(&tond, NDF_ONLY_PNBUF);
3335 vrele(fromnd.ni_dvp);
3338 vrele(tond.ni_startdir);
3339 vn_finished_write(mp);
3341 if (fromnd.ni_startdir)
3342 vrele(fromnd.ni_startdir);
3343 VFS_UNLOCK_GIANT(fvfslocked);
3344 VFS_UNLOCK_GIANT(tvfslocked);
3351 * Make a directory file.
3353 #ifndef _SYS_SYSPROTO_H_
3362 register struct mkdir_args /* {
3368 return (kern_mkdir(td, uap->path, UIO_USERSPACE, uap->mode));
3372 kern_mkdir(struct thread *td, char *path, enum uio_seg segflg, int mode)
3378 struct nameidata nd;
3381 AUDIT_ARG(mode, mode);
3384 NDINIT(&nd, CREATE, LOCKPARENT | SAVENAME | MPSAFE | AUDITVNODE1,
3386 nd.ni_cnd.cn_flags |= WILLBEDIR;
3387 if ((error = namei(&nd)) != 0)
3389 vfslocked = NDHASGIANT(&nd);
3392 NDFREE(&nd, NDF_ONLY_PNBUF);
3394 * XXX namei called with LOCKPARENT but not LOCKLEAF has
3395 * the strange behaviour of leaving the vnode unlocked
3396 * if the target is the same vnode as the parent.
3398 if (vp == nd.ni_dvp)
3403 VFS_UNLOCK_GIANT(vfslocked);
3406 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3407 NDFREE(&nd, NDF_ONLY_PNBUF);
3409 VFS_UNLOCK_GIANT(vfslocked);
3410 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3415 vattr.va_type = VDIR;
3416 FILEDESC_SLOCK(td->td_proc->p_fd);
3417 vattr.va_mode = (mode & ACCESSPERMS) &~ td->td_proc->p_fd->fd_cmask;
3418 FILEDESC_SUNLOCK(td->td_proc->p_fd);
3420 error = mac_check_vnode_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
3425 VOP_LEASE(nd.ni_dvp, td, td->td_ucred, LEASE_WRITE);
3426 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
3430 NDFREE(&nd, NDF_ONLY_PNBUF);
3434 vn_finished_write(mp);
3435 VFS_UNLOCK_GIANT(vfslocked);
3440 * Remove a directory file.
3442 #ifndef _SYS_SYSPROTO_H_
3450 struct rmdir_args /* {
3455 return (kern_rmdir(td, uap->path, UIO_USERSPACE));
3459 kern_rmdir(struct thread *td, char *path, enum uio_seg pathseg)
3464 struct nameidata nd;
3469 NDINIT(&nd, DELETE, LOCKPARENT | LOCKLEAF | MPSAFE | AUDITVNODE1,
3471 if ((error = namei(&nd)) != 0)
3473 vfslocked = NDHASGIANT(&nd);
3475 if (vp->v_type != VDIR) {
3480 * No rmdir "." please.
3482 if (nd.ni_dvp == vp) {
3487 * The root of a mounted filesystem cannot be deleted.
3489 if (vp->v_vflag & VV_ROOT) {
3494 error = mac_check_vnode_delete(td->td_ucred, nd.ni_dvp, vp,
3499 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3500 NDFREE(&nd, NDF_ONLY_PNBUF);
3502 if (nd.ni_dvp == vp)
3506 VFS_UNLOCK_GIANT(vfslocked);
3507 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3511 VOP_LEASE(nd.ni_dvp, td, td->td_ucred, LEASE_WRITE);
3512 VOP_LEASE(vp, td, td->td_ucred, LEASE_WRITE);
3513 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
3514 vn_finished_write(mp);
3516 NDFREE(&nd, NDF_ONLY_PNBUF);
3518 if (nd.ni_dvp == vp)
3522 VFS_UNLOCK_GIANT(vfslocked);
3528 * Read a block of directory entries in a filesystem independent format.
3530 #ifndef _SYS_SYSPROTO_H_
3531 struct ogetdirentries_args {
3539 ogetdirentries(td, uap)
3541 register struct ogetdirentries_args /* {
3550 struct uio auio, kuio;
3551 struct iovec aiov, kiov;
3552 struct dirent *dp, *edp;
3554 int error, eofflag, readcnt, vfslocked;
3557 /* XXX arbitrary sanity limit on `count'. */
3558 if (uap->count > 64 * 1024)
3560 if ((error = getvnode(td->td_proc->p_fd, uap->fd, &fp)) != 0)
3562 if ((fp->f_flag & FREAD) == 0) {
3568 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
3569 if (vp->v_type != VDIR) {
3570 VFS_UNLOCK_GIANT(vfslocked);
3574 aiov.iov_base = uap->buf;
3575 aiov.iov_len = uap->count;
3576 auio.uio_iov = &aiov;
3577 auio.uio_iovcnt = 1;
3578 auio.uio_rw = UIO_READ;
3579 auio.uio_segflg = UIO_USERSPACE;
3581 auio.uio_resid = uap->count;
3582 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
3583 loff = auio.uio_offset = fp->f_offset;
3585 error = mac_check_vnode_readdir(td->td_ucred, vp);
3587 VOP_UNLOCK(vp, 0, td);
3588 VFS_UNLOCK_GIANT(vfslocked);
3593 # if (BYTE_ORDER != LITTLE_ENDIAN)
3594 if (vp->v_mount->mnt_maxsymlinklen <= 0) {
3595 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag,
3597 fp->f_offset = auio.uio_offset;
3602 kuio.uio_iov = &kiov;
3603 kuio.uio_segflg = UIO_SYSSPACE;
3604 kiov.iov_len = uap->count;
3605 MALLOC(dirbuf, caddr_t, uap->count, M_TEMP, M_WAITOK);
3606 kiov.iov_base = dirbuf;
3607 error = VOP_READDIR(vp, &kuio, fp->f_cred, &eofflag,
3609 fp->f_offset = kuio.uio_offset;
3611 readcnt = uap->count - kuio.uio_resid;
3612 edp = (struct dirent *)&dirbuf[readcnt];
3613 for (dp = (struct dirent *)dirbuf; dp < edp; ) {
3614 # if (BYTE_ORDER == LITTLE_ENDIAN)
3616 * The expected low byte of
3617 * dp->d_namlen is our dp->d_type.
3618 * The high MBZ byte of dp->d_namlen
3619 * is our dp->d_namlen.
3621 dp->d_type = dp->d_namlen;
3625 * The dp->d_type is the high byte
3626 * of the expected dp->d_namlen,
3627 * so must be zero'ed.
3631 if (dp->d_reclen > 0) {
3632 dp = (struct dirent *)
3633 ((char *)dp + dp->d_reclen);
3640 error = uiomove(dirbuf, readcnt, &auio);
3642 FREE(dirbuf, M_TEMP);
3645 VOP_UNLOCK(vp, 0, td);
3646 VFS_UNLOCK_GIANT(vfslocked);
3650 if (uap->count == auio.uio_resid &&
3651 (vp->v_vflag & VV_ROOT) &&
3652 (vp->v_mount->mnt_flag & MNT_UNION)) {
3653 struct vnode *tvp = vp;
3654 vp = vp->v_mount->mnt_vnodecovered;
3660 VFS_UNLOCK_GIANT(vfslocked);
3663 VOP_UNLOCK(vp, 0, td);
3664 VFS_UNLOCK_GIANT(vfslocked);
3665 error = copyout(&loff, uap->basep, sizeof(long));
3667 td->td_retval[0] = uap->count - auio.uio_resid;
3670 #endif /* COMPAT_43 */
3673 * Read a block of directory entries in a filesystem independent format.
3675 #ifndef _SYS_SYSPROTO_H_
3676 struct getdirentries_args {
3684 getdirentries(td, uap)
3686 register struct getdirentries_args /* {
3701 AUDIT_ARG(fd, uap->fd);
3702 if ((error = getvnode(td->td_proc->p_fd, uap->fd, &fp)) != 0)
3704 if ((fp->f_flag & FREAD) == 0) {
3710 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
3711 if (vp->v_type != VDIR) {
3712 VFS_UNLOCK_GIANT(vfslocked);
3716 aiov.iov_base = uap->buf;
3717 aiov.iov_len = uap->count;
3718 auio.uio_iov = &aiov;
3719 auio.uio_iovcnt = 1;
3720 auio.uio_rw = UIO_READ;
3721 auio.uio_segflg = UIO_USERSPACE;
3723 auio.uio_resid = uap->count;
3724 /* vn_lock(vp, LK_SHARED | LK_RETRY, td); */
3725 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
3726 AUDIT_ARG(vnode, vp, ARG_VNODE1);
3727 loff = auio.uio_offset = fp->f_offset;
3729 error = mac_check_vnode_readdir(td->td_ucred, vp);
3732 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL,
3734 fp->f_offset = auio.uio_offset;
3736 VOP_UNLOCK(vp, 0, td);
3737 VFS_UNLOCK_GIANT(vfslocked);
3740 if (uap->count == auio.uio_resid &&
3741 (vp->v_vflag & VV_ROOT) &&
3742 (vp->v_mount->mnt_flag & MNT_UNION)) {
3743 struct vnode *tvp = vp;
3744 vp = vp->v_mount->mnt_vnodecovered;
3750 VFS_UNLOCK_GIANT(vfslocked);
3753 VOP_UNLOCK(vp, 0, td);
3754 VFS_UNLOCK_GIANT(vfslocked);
3755 if (uap->basep != NULL) {
3756 error = copyout(&loff, uap->basep, sizeof(long));
3758 td->td_retval[0] = uap->count - auio.uio_resid;
3764 #ifndef _SYS_SYSPROTO_H_
3765 struct getdents_args {
3774 register struct getdents_args /* {
3780 struct getdirentries_args ap;
3783 ap.count = uap->count;
3785 return (getdirentries(td, &ap));
3789 * Set the mode mask for creation of filesystem nodes.
3791 #ifndef _SYS_SYSPROTO_H_
3799 struct umask_args /* {
3803 register struct filedesc *fdp;
3805 FILEDESC_XLOCK(td->td_proc->p_fd);
3806 fdp = td->td_proc->p_fd;
3807 td->td_retval[0] = fdp->fd_cmask;
3808 fdp->fd_cmask = uap->newmask & ALLPERMS;
3809 FILEDESC_XUNLOCK(td->td_proc->p_fd);
3814 * Void all references to file by ripping underlying filesystem away from
3817 #ifndef _SYS_SYSPROTO_H_
3818 struct revoke_args {
3825 register struct revoke_args /* {
3832 struct nameidata nd;
3835 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | MPSAFE | AUDITVNODE1,
3836 UIO_USERSPACE, uap->path, td);
3837 if ((error = namei(&nd)) != 0)
3839 vfslocked = NDHASGIANT(&nd);
3841 NDFREE(&nd, NDF_ONLY_PNBUF);
3842 if (vp->v_type != VCHR) {
3847 error = mac_check_vnode_revoke(td->td_ucred, vp);
3851 error = VOP_GETATTR(vp, &vattr, td->td_ucred, td);
3854 if (td->td_ucred->cr_uid != vattr.va_uid) {
3855 error = priv_check_cred(td->td_ucred, PRIV_VFS_ADMIN,
3861 VOP_REVOKE(vp, REVOKEALL);
3864 VFS_UNLOCK_GIANT(vfslocked);
3869 * Convert a user file descriptor to a kernel file entry.
3870 * A reference on the file entry is held upon returning.
3873 getvnode(fdp, fd, fpp)
3874 struct filedesc *fdp;
3885 FILEDESC_SLOCK(fdp);
3886 if ((u_int)fd >= fdp->fd_nfiles ||
3887 (fp = fdp->fd_ofiles[fd]) == NULL)
3889 else if (fp->f_vnode == NULL) {
3896 FILEDESC_SUNLOCK(fdp);
3903 * Get an (NFS) file handle.
3905 #ifndef _SYS_SYSPROTO_H_
3906 struct lgetfh_args {
3914 register struct lgetfh_args *uap;
3916 struct nameidata nd;
3918 register struct vnode *vp;
3922 error = priv_check(td, PRIV_VFS_GETFH);
3925 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | MPSAFE | AUDITVNODE1,
3926 UIO_USERSPACE, uap->fname, td);
3930 vfslocked = NDHASGIANT(&nd);
3931 NDFREE(&nd, NDF_ONLY_PNBUF);
3933 bzero(&fh, sizeof(fh));
3934 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
3935 error = VOP_VPTOFH(vp, &fh.fh_fid);
3937 VFS_UNLOCK_GIANT(vfslocked);
3940 error = copyout(&fh, uap->fhp, sizeof (fh));
3944 #ifndef _SYS_SYSPROTO_H_
3953 register struct getfh_args *uap;
3955 struct nameidata nd;
3957 register struct vnode *vp;
3961 error = priv_check(td, PRIV_VFS_GETFH);
3964 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | MPSAFE | AUDITVNODE1,
3965 UIO_USERSPACE, uap->fname, td);
3969 vfslocked = NDHASGIANT(&nd);
3970 NDFREE(&nd, NDF_ONLY_PNBUF);
3972 bzero(&fh, sizeof(fh));
3973 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
3974 error = VOP_VPTOFH(vp, &fh.fh_fid);
3976 VFS_UNLOCK_GIANT(vfslocked);
3979 error = copyout(&fh, uap->fhp, sizeof (fh));
3984 * syscall for the rpc.lockd to use to translate a NFS file handle into an
3987 * warning: do not remove the priv_check() call or this becomes one giant
3990 #ifndef _SYS_SYSPROTO_H_
3991 struct fhopen_args {
3992 const struct fhandle *u_fhp;
3999 struct fhopen_args /* {
4000 const struct fhandle *u_fhp;
4004 struct proc *p = td->td_proc;
4009 struct vattr *vap = &vat;
4012 register struct filedesc *fdp = p->p_fd;
4013 int fmode, mode, error, type;
4018 error = priv_check(td, PRIV_VFS_FHOPEN);
4021 fmode = FFLAGS(uap->flags);
4022 /* why not allow a non-read/write open for our lockd? */
4023 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
4025 error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
4028 /* find the mount point */
4029 mp = vfs_getvfs(&fhp.fh_fsid);
4032 vfslocked = VFS_LOCK_GIANT(mp);
4033 /* now give me my vnode, it gets returned to me locked */
4034 error = VFS_FHTOVP(mp, &fhp.fh_fid, &vp);
4038 * from now on we have to make sure not
4039 * to forget about the vnode
4040 * any error that causes an abort must vput(vp)
4041 * just set error = err and 'goto bad;'.
4047 if (vp->v_type == VLNK) {
4051 if (vp->v_type == VSOCK) {
4056 if (fmode & (FWRITE | O_TRUNC)) {
4057 if (vp->v_type == VDIR) {
4061 error = vn_writechk(vp);
4068 if (fmode & O_APPEND)
4071 error = mac_check_vnode_open(td->td_ucred, vp, mode);
4076 error = VOP_ACCESS(vp, mode, td->td_ucred, td);
4080 if (fmode & O_TRUNC) {
4081 VOP_UNLOCK(vp, 0, td); /* XXX */
4082 if ((error = vn_start_write(NULL, &mp, V_WAIT | PCATCH)) != 0) {
4086 VOP_LEASE(vp, td, td->td_ucred, LEASE_WRITE);
4087 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td); /* XXX */
4090 * We don't yet have fp->f_cred, so use td->td_ucred, which
4093 error = mac_check_vnode_write(td->td_ucred, td->td_ucred, vp);
4098 error = VOP_SETATTR(vp, vap, td->td_ucred, td);
4102 vn_finished_write(mp);
4106 error = VOP_OPEN(vp, fmode, td->td_ucred, td, -1);
4114 * end of vn_open code
4117 if ((error = falloc(td, &nfp, &indx)) != 0) {
4122 /* An extra reference on `nfp' has been held for us by falloc(). */
4128 nfp->f_flag = fmode & FMASK;
4129 nfp->f_type = DTYPE_VNODE;
4130 nfp->f_ops = &vnops;
4132 if (fmode & (O_EXLOCK | O_SHLOCK)) {
4133 lf.l_whence = SEEK_SET;
4136 if (fmode & O_EXLOCK)
4137 lf.l_type = F_WRLCK;
4139 lf.l_type = F_RDLCK;
4141 if ((fmode & FNONBLOCK) == 0)
4143 VOP_UNLOCK(vp, 0, td);
4144 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf,
4147 * The lock request failed. Normally close the
4148 * descriptor but handle the case where someone might
4149 * have dup()d or close()d it when we weren't looking.
4151 fdclose(fdp, fp, indx, td);
4154 * release our private reference
4159 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
4160 fp->f_flag |= FHASLOCK;
4163 VOP_UNLOCK(vp, 0, td);
4166 VFS_UNLOCK_GIANT(vfslocked);
4167 td->td_retval[0] = indx;
4174 VFS_UNLOCK_GIANT(vfslocked);
4179 * Stat an (NFS) file handle.
4181 #ifndef _SYS_SYSPROTO_H_
4182 struct fhstat_args {
4183 struct fhandle *u_fhp;
4190 register struct fhstat_args /* {
4191 struct fhandle *u_fhp;
4202 error = priv_check(td, PRIV_VFS_FHSTAT);
4205 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4208 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
4210 vfslocked = VFS_LOCK_GIANT(mp);
4211 if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp))) {
4213 VFS_UNLOCK_GIANT(vfslocked);
4216 error = vn_stat(vp, &sb, td->td_ucred, NOCRED, td);
4219 VFS_UNLOCK_GIANT(vfslocked);
4222 error = copyout(&sb, uap->sb, sizeof(sb));
4227 * Implement fstatfs() for (NFS) file handles.
4229 #ifndef _SYS_SYSPROTO_H_
4230 struct fhstatfs_args {
4231 struct fhandle *u_fhp;
4238 struct fhstatfs_args /* {
4239 struct fhandle *u_fhp;
4247 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4250 error = kern_fhstatfs(td, fh, &sf);
4253 return (copyout(&sf, uap->buf, sizeof(sf)));
4257 kern_fhstatfs(struct thread *td, fhandle_t fh, struct statfs *buf)
4265 error = priv_check(td, PRIV_VFS_FHSTATFS);
4268 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
4270 vfslocked = VFS_LOCK_GIANT(mp);
4271 error = VFS_FHTOVP(mp, &fh.fh_fid, &vp);
4273 VFS_UNLOCK_GIANT(vfslocked);
4278 error = prison_canseemount(td->td_ucred, mp);
4282 error = mac_check_mount_stat(td->td_ucred, mp);
4287 * Set these in case the underlying filesystem fails to do so.
4290 sp->f_version = STATFS_VERSION;
4291 sp->f_namemax = NAME_MAX;
4292 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
4293 error = VFS_STATFS(mp, sp, td);
4298 VFS_UNLOCK_GIANT(vfslocked);