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 */
987 error = vn_open(&nd, &flags, cmode, fp);
990 * If the vn_open replaced the method vector, something
991 * wonderous happened deep below and we just pass it up
992 * pretending we know what we do.
994 if (error == ENXIO && fp->f_ops != &badfileops) {
996 td->td_retval[0] = indx;
1001 * handle special fdopen() case. bleh. dupfdopen() is
1002 * responsible for dropping the old contents of ofiles[indx]
1005 if ((error == ENODEV || error == ENXIO) &&
1006 td->td_dupfd >= 0 && /* XXX from fdopen */
1008 dupfdopen(td, fdp, indx, td->td_dupfd, flags, error)) == 0) {
1009 td->td_retval[0] = indx;
1014 * Clean up the descriptor, but only if another thread hadn't
1015 * replaced or closed it.
1017 fdclose(fdp, fp, indx, td);
1020 if (error == ERESTART)
1025 vfslocked = NDHASGIANT(&nd);
1026 NDFREE(&nd, NDF_ONLY_PNBUF);
1031 if (fp->f_data == NULL)
1033 fp->f_flag = flags & FMASK;
1035 fp->f_type = (vp->v_type == VFIFO ? DTYPE_FIFO : DTYPE_VNODE);
1036 if (fp->f_ops == &badfileops)
1040 VOP_UNLOCK(vp, 0, td);
1041 if (flags & (O_EXLOCK | O_SHLOCK)) {
1042 lf.l_whence = SEEK_SET;
1045 if (flags & O_EXLOCK)
1046 lf.l_type = F_WRLCK;
1048 lf.l_type = F_RDLCK;
1050 if ((flags & FNONBLOCK) == 0)
1052 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf,
1055 fp->f_flag |= FHASLOCK;
1057 if (flags & O_TRUNC) {
1058 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
1060 VOP_LEASE(vp, td, td->td_ucred, LEASE_WRITE);
1063 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
1065 error = mac_check_vnode_write(td->td_ucred, fp->f_cred, vp);
1068 error = VOP_SETATTR(vp, &vat, td->td_ucred, td);
1069 VOP_UNLOCK(vp, 0, td);
1070 vn_finished_write(mp);
1074 VFS_UNLOCK_GIANT(vfslocked);
1076 * Release our private reference, leaving the one associated with
1077 * the descriptor table intact.
1080 td->td_retval[0] = indx;
1083 VFS_UNLOCK_GIANT(vfslocked);
1084 fdclose(fdp, fp, indx, td);
1093 #ifndef _SYS_SYSPROTO_H_
1094 struct ocreat_args {
1102 register struct ocreat_args /* {
1108 return (kern_open(td, uap->path, UIO_USERSPACE,
1109 O_WRONLY | O_CREAT | O_TRUNC, uap->mode));
1111 #endif /* COMPAT_43 */
1114 * Create a special file.
1116 #ifndef _SYS_SYSPROTO_H_
1126 register struct mknod_args /* {
1133 return (kern_mknod(td, uap->path, UIO_USERSPACE, uap->mode, uap->dev));
1137 kern_mknod(struct thread *td, char *path, enum uio_seg pathseg, int mode,
1145 struct nameidata nd;
1148 AUDIT_ARG(mode, mode);
1149 AUDIT_ARG(dev, dev);
1150 switch (mode & S_IFMT) {
1153 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
1156 error = priv_check(td, PRIV_VFS_MKNOD_BAD);
1159 error = priv_check(td, PRIV_VFS_MKNOD_WHT);
1169 NDINIT(&nd, CREATE, LOCKPARENT | SAVENAME | MPSAFE | AUDITVNODE1,
1171 if ((error = namei(&nd)) != 0)
1173 vfslocked = NDHASGIANT(&nd);
1176 NDFREE(&nd, NDF_ONLY_PNBUF);
1177 if (vp == nd.ni_dvp)
1182 VFS_UNLOCK_GIANT(vfslocked);
1186 FILEDESC_SLOCK(td->td_proc->p_fd);
1187 vattr.va_mode = (mode & ALLPERMS) &
1188 ~td->td_proc->p_fd->fd_cmask;
1189 FILEDESC_SUNLOCK(td->td_proc->p_fd);
1190 vattr.va_rdev = dev;
1193 switch (mode & S_IFMT) {
1194 case S_IFMT: /* used by badsect to flag bad sectors */
1195 vattr.va_type = VBAD;
1198 vattr.va_type = VCHR;
1201 vattr.va_type = VBLK;
1207 panic("kern_mknod: invalid mode");
1210 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1211 NDFREE(&nd, NDF_ONLY_PNBUF);
1213 VFS_UNLOCK_GIANT(vfslocked);
1214 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1219 if (error == 0 && !whiteout)
1220 error = mac_check_vnode_create(td->td_ucred, nd.ni_dvp,
1221 &nd.ni_cnd, &vattr);
1224 VOP_LEASE(nd.ni_dvp, td, td->td_ucred, LEASE_WRITE);
1226 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, CREATE);
1228 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp,
1229 &nd.ni_cnd, &vattr);
1234 NDFREE(&nd, NDF_ONLY_PNBUF);
1236 vn_finished_write(mp);
1237 VFS_UNLOCK_GIANT(vfslocked);
1242 * Create a named pipe.
1244 #ifndef _SYS_SYSPROTO_H_
1245 struct mkfifo_args {
1253 register struct mkfifo_args /* {
1259 return (kern_mkfifo(td, uap->path, UIO_USERSPACE, uap->mode));
1263 kern_mkfifo(struct thread *td, char *path, enum uio_seg pathseg, int mode)
1268 struct nameidata nd;
1271 AUDIT_ARG(mode, mode);
1274 NDINIT(&nd, CREATE, LOCKPARENT | SAVENAME | MPSAFE | AUDITVNODE1,
1276 if ((error = namei(&nd)) != 0)
1278 vfslocked = NDHASGIANT(&nd);
1279 if (nd.ni_vp != NULL) {
1280 NDFREE(&nd, NDF_ONLY_PNBUF);
1281 if (nd.ni_vp == nd.ni_dvp)
1286 VFS_UNLOCK_GIANT(vfslocked);
1289 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1290 NDFREE(&nd, NDF_ONLY_PNBUF);
1292 VFS_UNLOCK_GIANT(vfslocked);
1293 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1298 vattr.va_type = VFIFO;
1299 FILEDESC_SLOCK(td->td_proc->p_fd);
1300 vattr.va_mode = (mode & ALLPERMS) & ~td->td_proc->p_fd->fd_cmask;
1301 FILEDESC_SUNLOCK(td->td_proc->p_fd);
1303 error = mac_check_vnode_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1308 VOP_LEASE(nd.ni_dvp, td, td->td_ucred, LEASE_WRITE);
1309 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
1316 vn_finished_write(mp);
1317 VFS_UNLOCK_GIANT(vfslocked);
1318 NDFREE(&nd, NDF_ONLY_PNBUF);
1323 * Make a hard file link.
1325 #ifndef _SYS_SYSPROTO_H_
1334 register struct link_args /* {
1341 error = kern_link(td, uap->path, uap->link, UIO_USERSPACE);
1345 static int hardlink_check_uid = 0;
1346 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
1347 &hardlink_check_uid, 0,
1348 "Unprivileged processes cannot create hard links to files owned by other "
1350 static int hardlink_check_gid = 0;
1351 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
1352 &hardlink_check_gid, 0,
1353 "Unprivileged processes cannot create hard links to files owned by other "
1357 can_hardlink(struct vnode *vp, struct thread *td, struct ucred *cred)
1362 if (!hardlink_check_uid && !hardlink_check_gid)
1365 error = VOP_GETATTR(vp, &va, cred, td);
1369 if (hardlink_check_uid && cred->cr_uid != va.va_uid) {
1370 error = priv_check_cred(cred, PRIV_VFS_LINK,
1376 if (hardlink_check_gid && !groupmember(va.va_gid, cred)) {
1377 error = priv_check_cred(cred, PRIV_VFS_LINK,
1387 kern_link(struct thread *td, char *path, char *link, enum uio_seg segflg)
1391 struct nameidata nd;
1397 NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE | AUDITVNODE1, segflg, path, td);
1398 if ((error = namei(&nd)) != 0)
1400 vfslocked = NDHASGIANT(&nd);
1401 NDFREE(&nd, NDF_ONLY_PNBUF);
1403 if (vp->v_type == VDIR) {
1405 VFS_UNLOCK_GIANT(vfslocked);
1406 return (EPERM); /* POSIX */
1408 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0) {
1410 VFS_UNLOCK_GIANT(vfslocked);
1413 NDINIT(&nd, CREATE, LOCKPARENT | SAVENAME | MPSAFE | AUDITVNODE2,
1415 if ((error = namei(&nd)) == 0) {
1416 lvfslocked = NDHASGIANT(&nd);
1417 if (nd.ni_vp != NULL) {
1418 if (nd.ni_dvp == nd.ni_vp)
1424 } else if ((error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td))
1426 VOP_LEASE(nd.ni_dvp, td, td->td_ucred, LEASE_WRITE);
1427 VOP_LEASE(vp, td, td->td_ucred, LEASE_WRITE);
1428 error = can_hardlink(vp, td, td->td_ucred);
1431 error = mac_check_vnode_link(td->td_ucred,
1432 nd.ni_dvp, vp, &nd.ni_cnd);
1435 error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
1436 VOP_UNLOCK(vp, 0, td);
1439 NDFREE(&nd, NDF_ONLY_PNBUF);
1440 VFS_UNLOCK_GIANT(lvfslocked);
1443 vn_finished_write(mp);
1444 VFS_UNLOCK_GIANT(vfslocked);
1449 * Make a symbolic link.
1451 #ifndef _SYS_SYSPROTO_H_
1452 struct symlink_args {
1460 register struct symlink_args /* {
1466 return (kern_symlink(td, uap->path, uap->link, UIO_USERSPACE));
1470 kern_symlink(struct thread *td, char *path, char *link, enum uio_seg segflg)
1476 struct nameidata nd;
1479 if (segflg == UIO_SYSSPACE) {
1482 syspath = uma_zalloc(namei_zone, M_WAITOK);
1483 if ((error = copyinstr(path, syspath, MAXPATHLEN, NULL)) != 0)
1486 AUDIT_ARG(text, syspath);
1489 NDINIT(&nd, CREATE, LOCKPARENT | SAVENAME | MPSAFE | AUDITVNODE1,
1491 if ((error = namei(&nd)) != 0)
1493 vfslocked = NDHASGIANT(&nd);
1495 NDFREE(&nd, NDF_ONLY_PNBUF);
1496 if (nd.ni_vp == nd.ni_dvp)
1501 VFS_UNLOCK_GIANT(vfslocked);
1505 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1506 NDFREE(&nd, NDF_ONLY_PNBUF);
1508 VFS_UNLOCK_GIANT(vfslocked);
1509 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1514 FILEDESC_SLOCK(td->td_proc->p_fd);
1515 vattr.va_mode = ACCESSPERMS &~ td->td_proc->p_fd->fd_cmask;
1516 FILEDESC_SUNLOCK(td->td_proc->p_fd);
1518 vattr.va_type = VLNK;
1519 error = mac_check_vnode_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1524 VOP_LEASE(nd.ni_dvp, td, td->td_ucred, LEASE_WRITE);
1525 error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr, syspath);
1531 NDFREE(&nd, NDF_ONLY_PNBUF);
1533 vn_finished_write(mp);
1534 VFS_UNLOCK_GIANT(vfslocked);
1536 if (segflg != UIO_SYSSPACE)
1537 uma_zfree(namei_zone, syspath);
1542 * Delete a whiteout from the filesystem.
1547 register struct undelete_args /* {
1553 struct nameidata nd;
1558 NDINIT(&nd, DELETE, LOCKPARENT | DOWHITEOUT | MPSAFE | AUDITVNODE1,
1559 UIO_USERSPACE, uap->path, td);
1563 vfslocked = NDHASGIANT(&nd);
1565 if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & ISWHITEOUT)) {
1566 NDFREE(&nd, NDF_ONLY_PNBUF);
1567 if (nd.ni_vp == nd.ni_dvp)
1573 VFS_UNLOCK_GIANT(vfslocked);
1576 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1577 NDFREE(&nd, NDF_ONLY_PNBUF);
1579 VFS_UNLOCK_GIANT(vfslocked);
1580 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1584 VOP_LEASE(nd.ni_dvp, td, td->td_ucred, LEASE_WRITE);
1585 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, DELETE);
1586 NDFREE(&nd, NDF_ONLY_PNBUF);
1588 vn_finished_write(mp);
1589 VFS_UNLOCK_GIANT(vfslocked);
1594 * Delete a name from the filesystem.
1596 #ifndef _SYS_SYSPROTO_H_
1597 struct unlink_args {
1604 struct unlink_args /* {
1610 error = kern_unlink(td, uap->path, UIO_USERSPACE);
1615 kern_unlink(struct thread *td, char *path, enum uio_seg pathseg)
1620 struct nameidata nd;
1625 NDINIT(&nd, DELETE, LOCKPARENT | LOCKLEAF | MPSAFE | AUDITVNODE1,
1627 if ((error = namei(&nd)) != 0)
1628 return (error == EINVAL ? EPERM : error);
1629 vfslocked = NDHASGIANT(&nd);
1631 if (vp->v_type == VDIR)
1632 error = EPERM; /* POSIX */
1635 * The root of a mounted filesystem cannot be deleted.
1637 * XXX: can this only be a VDIR case?
1639 if (vp->v_vflag & VV_ROOT)
1643 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1644 NDFREE(&nd, NDF_ONLY_PNBUF);
1646 if (vp == nd.ni_dvp)
1650 VFS_UNLOCK_GIANT(vfslocked);
1651 if ((error = vn_start_write(NULL, &mp,
1652 V_XSLEEP | PCATCH)) != 0)
1657 error = mac_check_vnode_delete(td->td_ucred, nd.ni_dvp, vp,
1662 VOP_LEASE(nd.ni_dvp, td, td->td_ucred, LEASE_WRITE);
1663 error = VOP_REMOVE(nd.ni_dvp, vp, &nd.ni_cnd);
1667 vn_finished_write(mp);
1669 NDFREE(&nd, NDF_ONLY_PNBUF);
1671 if (vp == nd.ni_dvp)
1675 VFS_UNLOCK_GIANT(vfslocked);
1680 * Reposition read/write file offset.
1682 #ifndef _SYS_SYSPROTO_H_
1693 register struct lseek_args /* {
1700 struct ucred *cred = td->td_ucred;
1708 if ((error = fget(td, uap->fd, &fp)) != 0)
1710 if (!(fp->f_ops->fo_flags & DFLAG_SEEKABLE)) {
1715 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
1716 noneg = (vp->v_type != VCHR);
1717 offset = uap->offset;
1718 switch (uap->whence) {
1721 (fp->f_offset < 0 ||
1722 (offset > 0 && fp->f_offset > OFF_MAX - offset))) {
1726 offset += fp->f_offset;
1729 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
1730 error = VOP_GETATTR(vp, &vattr, cred, td);
1731 VOP_UNLOCK(vp, 0, td);
1735 (vattr.va_size > OFF_MAX ||
1736 (offset > 0 && vattr.va_size > OFF_MAX - offset))) {
1740 offset += vattr.va_size;
1745 error = fo_ioctl(fp, FIOSEEKDATA, &offset, cred, td);
1748 error = fo_ioctl(fp, FIOSEEKHOLE, &offset, cred, td);
1753 if (error == 0 && noneg && offset < 0)
1757 fp->f_offset = offset;
1758 *(off_t *)(td->td_retval) = fp->f_offset;
1761 VFS_UNLOCK_GIANT(vfslocked);
1765 #if defined(COMPAT_43)
1767 * Reposition read/write file offset.
1769 #ifndef _SYS_SYSPROTO_H_
1770 struct olseek_args {
1779 register struct olseek_args /* {
1785 struct lseek_args /* {
1794 nuap.offset = uap->offset;
1795 nuap.whence = uap->whence;
1796 error = lseek(td, &nuap);
1799 #endif /* COMPAT_43 */
1802 * Check access permissions using passed credentials.
1805 vn_access(vp, user_flags, cred, td)
1813 /* Flags == 0 means only check for existence. */
1817 if (user_flags & R_OK)
1819 if (user_flags & W_OK)
1821 if (user_flags & X_OK)
1824 error = mac_check_vnode_access(cred, vp, flags);
1828 if ((flags & VWRITE) == 0 || (error = vn_writechk(vp)) == 0)
1829 error = VOP_ACCESS(vp, flags, cred, td);
1835 * Check access permissions using "real" credentials.
1837 #ifndef _SYS_SYSPROTO_H_
1838 struct access_args {
1846 register struct access_args /* {
1852 return (kern_access(td, uap->path, UIO_USERSPACE, uap->flags));
1856 kern_access(struct thread *td, char *path, enum uio_seg pathseg, int flags)
1858 struct ucred *cred, *tmpcred;
1859 register struct vnode *vp;
1860 struct nameidata nd;
1865 * Create and modify a temporary credential instead of one that
1866 * is potentially shared. This could also mess up socket
1867 * buffer accounting which can run in an interrupt context.
1869 cred = td->td_ucred;
1870 tmpcred = crdup(cred);
1871 tmpcred->cr_uid = cred->cr_ruid;
1872 tmpcred->cr_groups[0] = cred->cr_rgid;
1873 td->td_ucred = tmpcred;
1874 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | MPSAFE | AUDITVNODE1,
1876 if ((error = namei(&nd)) != 0)
1878 vfslocked = NDHASGIANT(&nd);
1881 error = vn_access(vp, flags, tmpcred, td);
1882 NDFREE(&nd, NDF_ONLY_PNBUF);
1884 VFS_UNLOCK_GIANT(vfslocked);
1886 td->td_ucred = cred;
1892 * Check access permissions using "effective" credentials.
1894 #ifndef _SYS_SYSPROTO_H_
1895 struct eaccess_args {
1903 register struct eaccess_args /* {
1909 return (kern_eaccess(td, uap->path, UIO_USERSPACE, uap->flags));
1913 kern_eaccess(struct thread *td, char *path, enum uio_seg pathseg, int flags)
1915 struct nameidata nd;
1920 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | MPSAFE | AUDITVNODE1,
1922 if ((error = namei(&nd)) != 0)
1925 vfslocked = NDHASGIANT(&nd);
1926 error = vn_access(vp, flags, td->td_ucred, td);
1927 NDFREE(&nd, NDF_ONLY_PNBUF);
1929 VFS_UNLOCK_GIANT(vfslocked);
1933 #if defined(COMPAT_43)
1935 * Get file status; this version follows links.
1937 #ifndef _SYS_SYSPROTO_H_
1946 register struct ostat_args /* {
1955 error = kern_stat(td, uap->path, UIO_USERSPACE, &sb);
1959 error = copyout(&osb, uap->ub, sizeof (osb));
1964 * Get file status; this version does not follow links.
1966 #ifndef _SYS_SYSPROTO_H_
1967 struct olstat_args {
1975 register struct olstat_args /* {
1984 error = kern_lstat(td, uap->path, UIO_USERSPACE, &sb);
1988 error = copyout(&osb, uap->ub, sizeof (osb));
1993 * Convert from an old to a new stat structure.
2001 ost->st_dev = st->st_dev;
2002 ost->st_ino = st->st_ino;
2003 ost->st_mode = st->st_mode;
2004 ost->st_nlink = st->st_nlink;
2005 ost->st_uid = st->st_uid;
2006 ost->st_gid = st->st_gid;
2007 ost->st_rdev = st->st_rdev;
2008 if (st->st_size < (quad_t)1 << 32)
2009 ost->st_size = st->st_size;
2012 ost->st_atime = st->st_atime;
2013 ost->st_mtime = st->st_mtime;
2014 ost->st_ctime = st->st_ctime;
2015 ost->st_blksize = st->st_blksize;
2016 ost->st_blocks = st->st_blocks;
2017 ost->st_flags = st->st_flags;
2018 ost->st_gen = st->st_gen;
2020 #endif /* COMPAT_43 */
2023 * Get file status; this version follows links.
2025 #ifndef _SYS_SYSPROTO_H_
2034 register struct stat_args /* {
2042 error = kern_stat(td, uap->path, UIO_USERSPACE, &sb);
2044 error = copyout(&sb, uap->ub, sizeof (sb));
2049 kern_stat(struct thread *td, char *path, enum uio_seg pathseg, struct stat *sbp)
2051 struct nameidata nd;
2053 int error, vfslocked;
2056 FOLLOW | LOCKSHARED | LOCKLEAF | MPSAFE | AUDITVNODE1,
2058 if ((error = namei(&nd)) != 0)
2060 vfslocked = NDHASGIANT(&nd);
2061 error = vn_stat(nd.ni_vp, &sb, td->td_ucred, NOCRED, td);
2062 NDFREE(&nd, NDF_ONLY_PNBUF);
2064 VFS_UNLOCK_GIANT(vfslocked);
2065 if (mtx_owned(&Giant))
2066 printf("stat(%d): %s\n", vfslocked, path);
2074 * Get file status; this version does not follow links.
2076 #ifndef _SYS_SYSPROTO_H_
2085 register struct lstat_args /* {
2093 error = kern_lstat(td, uap->path, UIO_USERSPACE, &sb);
2095 error = copyout(&sb, uap->ub, sizeof (sb));
2100 kern_lstat(struct thread *td, char *path, enum uio_seg pathseg, struct stat *sbp)
2104 struct nameidata nd;
2105 int error, vfslocked;
2108 NOFOLLOW | LOCKLEAF | LOCKSHARED | MPSAFE | AUDITVNODE1,
2110 if ((error = namei(&nd)) != 0)
2112 vfslocked = NDHASGIANT(&nd);
2114 error = vn_stat(vp, &sb, td->td_ucred, NOCRED, td);
2115 NDFREE(&nd, NDF_ONLY_PNBUF);
2117 VFS_UNLOCK_GIANT(vfslocked);
2125 * Implementation of the NetBSD [l]stat() functions.
2132 bzero(nsb, sizeof *nsb);
2133 nsb->st_dev = sb->st_dev;
2134 nsb->st_ino = sb->st_ino;
2135 nsb->st_mode = sb->st_mode;
2136 nsb->st_nlink = sb->st_nlink;
2137 nsb->st_uid = sb->st_uid;
2138 nsb->st_gid = sb->st_gid;
2139 nsb->st_rdev = sb->st_rdev;
2140 nsb->st_atimespec = sb->st_atimespec;
2141 nsb->st_mtimespec = sb->st_mtimespec;
2142 nsb->st_ctimespec = sb->st_ctimespec;
2143 nsb->st_size = sb->st_size;
2144 nsb->st_blocks = sb->st_blocks;
2145 nsb->st_blksize = sb->st_blksize;
2146 nsb->st_flags = sb->st_flags;
2147 nsb->st_gen = sb->st_gen;
2148 nsb->st_birthtimespec = sb->st_birthtimespec;
2151 #ifndef _SYS_SYSPROTO_H_
2160 register struct nstat_args /* {
2169 error = kern_stat(td, uap->path, UIO_USERSPACE, &sb);
2172 cvtnstat(&sb, &nsb);
2173 error = copyout(&nsb, uap->ub, sizeof (nsb));
2178 * NetBSD lstat. Get file status; this version does not follow links.
2180 #ifndef _SYS_SYSPROTO_H_
2189 register struct nlstat_args /* {
2198 error = kern_lstat(td, uap->path, UIO_USERSPACE, &sb);
2201 cvtnstat(&sb, &nsb);
2202 error = copyout(&nsb, uap->ub, sizeof (nsb));
2207 * Get configurable pathname variables.
2209 #ifndef _SYS_SYSPROTO_H_
2210 struct pathconf_args {
2218 register struct pathconf_args /* {
2224 return (kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name));
2228 kern_pathconf(struct thread *td, char *path, enum uio_seg pathseg, int name)
2230 struct nameidata nd;
2231 int error, vfslocked;
2233 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | MPSAFE | AUDITVNODE1,
2235 if ((error = namei(&nd)) != 0)
2237 vfslocked = NDHASGIANT(&nd);
2238 NDFREE(&nd, NDF_ONLY_PNBUF);
2240 /* If asynchronous I/O is available, it works for all files. */
2241 if (name == _PC_ASYNC_IO)
2242 td->td_retval[0] = async_io_version;
2244 error = VOP_PATHCONF(nd.ni_vp, name, td->td_retval);
2246 VFS_UNLOCK_GIANT(vfslocked);
2251 * Return target name of a symbolic link.
2253 #ifndef _SYS_SYSPROTO_H_
2254 struct readlink_args {
2263 register struct readlink_args /* {
2270 return (kern_readlink(td, uap->path, UIO_USERSPACE, uap->buf,
2271 UIO_USERSPACE, uap->count));
2275 kern_readlink(struct thread *td, char *path, enum uio_seg pathseg, char *buf,
2276 enum uio_seg bufseg, int count)
2278 register struct vnode *vp;
2282 struct nameidata nd;
2285 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | MPSAFE | AUDITVNODE1,
2287 if ((error = namei(&nd)) != 0)
2289 NDFREE(&nd, NDF_ONLY_PNBUF);
2290 vfslocked = NDHASGIANT(&nd);
2293 error = mac_check_vnode_readlink(td->td_ucred, vp);
2296 VFS_UNLOCK_GIANT(vfslocked);
2300 if (vp->v_type != VLNK)
2303 aiov.iov_base = buf;
2304 aiov.iov_len = count;
2305 auio.uio_iov = &aiov;
2306 auio.uio_iovcnt = 1;
2307 auio.uio_offset = 0;
2308 auio.uio_rw = UIO_READ;
2309 auio.uio_segflg = bufseg;
2311 auio.uio_resid = count;
2312 error = VOP_READLINK(vp, &auio, td->td_ucred);
2315 VFS_UNLOCK_GIANT(vfslocked);
2316 td->td_retval[0] = count - auio.uio_resid;
2321 * Common implementation code for chflags() and fchflags().
2324 setfflags(td, vp, flags)
2334 * Prevent non-root users from setting flags on devices. When
2335 * a device is reused, users can retain ownership of the device
2336 * if they are allowed to set flags and programs assume that
2337 * chown can't fail when done as root.
2339 if (vp->v_type == VCHR || vp->v_type == VBLK) {
2340 error = priv_check_cred(td->td_ucred, PRIV_VFS_CHFLAGS_DEV,
2346 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2348 VOP_LEASE(vp, td, td->td_ucred, LEASE_WRITE);
2349 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2351 vattr.va_flags = flags;
2353 error = mac_check_vnode_setflags(td->td_ucred, vp, vattr.va_flags);
2356 error = VOP_SETATTR(vp, &vattr, td->td_ucred, td);
2357 VOP_UNLOCK(vp, 0, td);
2358 vn_finished_write(mp);
2363 * Change flags of a file given a path name.
2365 #ifndef _SYS_SYSPROTO_H_
2366 struct chflags_args {
2374 register struct chflags_args /* {
2380 struct nameidata nd;
2383 AUDIT_ARG(fflags, uap->flags);
2384 NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE | AUDITVNODE1, UIO_USERSPACE,
2386 if ((error = namei(&nd)) != 0)
2388 NDFREE(&nd, NDF_ONLY_PNBUF);
2389 vfslocked = NDHASGIANT(&nd);
2390 error = setfflags(td, nd.ni_vp, uap->flags);
2392 VFS_UNLOCK_GIANT(vfslocked);
2397 * Same as chflags() but doesn't follow symlinks.
2402 register struct lchflags_args /* {
2408 struct nameidata nd;
2411 AUDIT_ARG(fflags, uap->flags);
2412 NDINIT(&nd, LOOKUP, NOFOLLOW | MPSAFE | AUDITVNODE1, UIO_USERSPACE,
2414 if ((error = namei(&nd)) != 0)
2416 vfslocked = NDHASGIANT(&nd);
2417 NDFREE(&nd, NDF_ONLY_PNBUF);
2418 error = setfflags(td, nd.ni_vp, uap->flags);
2420 VFS_UNLOCK_GIANT(vfslocked);
2425 * Change flags of a file given a file descriptor.
2427 #ifndef _SYS_SYSPROTO_H_
2428 struct fchflags_args {
2436 register struct fchflags_args /* {
2445 AUDIT_ARG(fd, uap->fd);
2446 AUDIT_ARG(fflags, uap->flags);
2447 if ((error = getvnode(td->td_proc->p_fd, uap->fd, &fp)) != 0)
2449 vfslocked = VFS_LOCK_GIANT(fp->f_vnode->v_mount);
2451 vn_lock(fp->f_vnode, LK_EXCLUSIVE | LK_RETRY, td);
2452 AUDIT_ARG(vnode, fp->f_vnode, ARG_VNODE1);
2453 VOP_UNLOCK(fp->f_vnode, 0, td);
2455 error = setfflags(td, fp->f_vnode, uap->flags);
2456 VFS_UNLOCK_GIANT(vfslocked);
2462 * Common implementation code for chmod(), lchmod() and fchmod().
2465 setfmode(td, vp, mode)
2474 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2476 VOP_LEASE(vp, td, td->td_ucred, LEASE_WRITE);
2477 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2479 vattr.va_mode = mode & ALLPERMS;
2481 error = mac_check_vnode_setmode(td->td_ucred, vp, vattr.va_mode);
2484 error = VOP_SETATTR(vp, &vattr, td->td_ucred, td);
2485 VOP_UNLOCK(vp, 0, td);
2486 vn_finished_write(mp);
2491 * Change mode of a file given path name.
2493 #ifndef _SYS_SYSPROTO_H_
2502 register struct chmod_args /* {
2508 return (kern_chmod(td, uap->path, UIO_USERSPACE, uap->mode));
2512 kern_chmod(struct thread *td, char *path, enum uio_seg pathseg, int mode)
2515 struct nameidata nd;
2518 AUDIT_ARG(mode, mode);
2519 NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE | AUDITVNODE1, pathseg, path, td);
2520 if ((error = namei(&nd)) != 0)
2522 vfslocked = NDHASGIANT(&nd);
2523 NDFREE(&nd, NDF_ONLY_PNBUF);
2524 error = setfmode(td, nd.ni_vp, mode);
2526 VFS_UNLOCK_GIANT(vfslocked);
2531 * Change mode of a file given path name (don't follow links.)
2533 #ifndef _SYS_SYSPROTO_H_
2534 struct lchmod_args {
2542 register struct lchmod_args /* {
2548 struct nameidata nd;
2551 AUDIT_ARG(mode, (mode_t)uap->mode);
2552 NDINIT(&nd, LOOKUP, NOFOLLOW | MPSAFE | AUDITVNODE1, UIO_USERSPACE,
2554 if ((error = namei(&nd)) != 0)
2556 vfslocked = NDHASGIANT(&nd);
2557 NDFREE(&nd, NDF_ONLY_PNBUF);
2558 error = setfmode(td, nd.ni_vp, uap->mode);
2560 VFS_UNLOCK_GIANT(vfslocked);
2565 * Change mode of a file given a file descriptor.
2567 #ifndef _SYS_SYSPROTO_H_
2568 struct fchmod_args {
2576 register struct fchmod_args /* {
2585 AUDIT_ARG(fd, uap->fd);
2586 AUDIT_ARG(mode, uap->mode);
2587 if ((error = getvnode(td->td_proc->p_fd, uap->fd, &fp)) != 0)
2589 vfslocked = VFS_LOCK_GIANT(fp->f_vnode->v_mount);
2591 vn_lock(fp->f_vnode, LK_EXCLUSIVE | LK_RETRY, td);
2592 AUDIT_ARG(vnode, fp->f_vnode, ARG_VNODE1);
2593 VOP_UNLOCK(fp->f_vnode, 0, td);
2595 error = setfmode(td, fp->f_vnode, uap->mode);
2596 VFS_UNLOCK_GIANT(vfslocked);
2602 * Common implementation for chown(), lchown(), and fchown()
2605 setfown(td, vp, uid, gid)
2615 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2617 VOP_LEASE(vp, td, td->td_ucred, LEASE_WRITE);
2618 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2623 error = mac_check_vnode_setowner(td->td_ucred, vp, vattr.va_uid,
2627 error = VOP_SETATTR(vp, &vattr, td->td_ucred, td);
2628 VOP_UNLOCK(vp, 0, td);
2629 vn_finished_write(mp);
2634 * Set ownership given a path name.
2636 #ifndef _SYS_SYSPROTO_H_
2646 register struct chown_args /* {
2653 return (kern_chown(td, uap->path, UIO_USERSPACE, uap->uid, uap->gid));
2657 kern_chown(struct thread *td, char *path, enum uio_seg pathseg, int uid,
2661 struct nameidata nd;
2664 AUDIT_ARG(owner, uid, gid);
2665 NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE | AUDITVNODE1, pathseg, path, td);
2666 if ((error = namei(&nd)) != 0)
2668 vfslocked = NDHASGIANT(&nd);
2669 NDFREE(&nd, NDF_ONLY_PNBUF);
2670 error = setfown(td, nd.ni_vp, uid, gid);
2672 VFS_UNLOCK_GIANT(vfslocked);
2677 * Set ownership given a path name, do not cross symlinks.
2679 #ifndef _SYS_SYSPROTO_H_
2680 struct lchown_args {
2689 register struct lchown_args /* {
2696 return (kern_lchown(td, uap->path, UIO_USERSPACE, uap->uid, uap->gid));
2700 kern_lchown(struct thread *td, char *path, enum uio_seg pathseg, int uid,
2704 struct nameidata nd;
2707 AUDIT_ARG(owner, uid, gid);
2708 NDINIT(&nd, LOOKUP, NOFOLLOW | MPSAFE | AUDITVNODE1, pathseg, path, td);
2709 if ((error = namei(&nd)) != 0)
2711 vfslocked = NDHASGIANT(&nd);
2712 NDFREE(&nd, NDF_ONLY_PNBUF);
2713 error = setfown(td, nd.ni_vp, uid, gid);
2715 VFS_UNLOCK_GIANT(vfslocked);
2720 * Set ownership given a file descriptor.
2722 #ifndef _SYS_SYSPROTO_H_
2723 struct fchown_args {
2732 register struct fchown_args /* {
2742 AUDIT_ARG(fd, uap->fd);
2743 AUDIT_ARG(owner, uap->uid, uap->gid);
2744 if ((error = getvnode(td->td_proc->p_fd, uap->fd, &fp)) != 0)
2746 vfslocked = VFS_LOCK_GIANT(fp->f_vnode->v_mount);
2748 vn_lock(fp->f_vnode, LK_EXCLUSIVE | LK_RETRY, td);
2749 AUDIT_ARG(vnode, fp->f_vnode, ARG_VNODE1);
2750 VOP_UNLOCK(fp->f_vnode, 0, td);
2752 error = setfown(td, fp->f_vnode, uap->uid, uap->gid);
2753 VFS_UNLOCK_GIANT(vfslocked);
2759 * Common implementation code for utimes(), lutimes(), and futimes().
2762 getutimes(usrtvp, tvpseg, tsp)
2763 const struct timeval *usrtvp;
2764 enum uio_seg tvpseg;
2765 struct timespec *tsp;
2767 struct timeval tv[2];
2768 const struct timeval *tvp;
2771 if (usrtvp == NULL) {
2773 TIMEVAL_TO_TIMESPEC(&tv[0], &tsp[0]);
2776 if (tvpseg == UIO_SYSSPACE) {
2779 if ((error = copyin(usrtvp, tv, sizeof(tv))) != 0)
2784 if (tvp[0].tv_usec < 0 || tvp[0].tv_usec >= 1000000 ||
2785 tvp[1].tv_usec < 0 || tvp[1].tv_usec >= 1000000)
2787 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
2788 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
2794 * Common implementation code for utimes(), lutimes(), and futimes().
2797 setutimes(td, vp, ts, numtimes, nullflag)
2800 const struct timespec *ts;
2804 int error, setbirthtime;
2808 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2810 VOP_LEASE(vp, td, td->td_ucred, LEASE_WRITE);
2811 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2813 if (numtimes < 3 && VOP_GETATTR(vp, &vattr, td->td_ucred, td) == 0 &&
2814 timespeccmp(&ts[1], &vattr.va_birthtime, < ))
2817 vattr.va_atime = ts[0];
2818 vattr.va_mtime = ts[1];
2820 vattr.va_birthtime = ts[1];
2822 vattr.va_birthtime = ts[2];
2824 vattr.va_vaflags |= VA_UTIMES_NULL;
2826 error = mac_check_vnode_setutimes(td->td_ucred, vp, vattr.va_atime,
2830 error = VOP_SETATTR(vp, &vattr, td->td_ucred, td);
2831 VOP_UNLOCK(vp, 0, td);
2832 vn_finished_write(mp);
2837 * Set the access and modification times of a file.
2839 #ifndef _SYS_SYSPROTO_H_
2840 struct utimes_args {
2842 struct timeval *tptr;
2848 register struct utimes_args /* {
2850 struct timeval *tptr;
2854 return (kern_utimes(td, uap->path, UIO_USERSPACE, uap->tptr,
2859 kern_utimes(struct thread *td, char *path, enum uio_seg pathseg,
2860 struct timeval *tptr, enum uio_seg tptrseg)
2862 struct timespec ts[2];
2864 struct nameidata nd;
2867 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
2869 NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE | AUDITVNODE1, pathseg, path, td);
2870 if ((error = namei(&nd)) != 0)
2872 vfslocked = NDHASGIANT(&nd);
2873 NDFREE(&nd, NDF_ONLY_PNBUF);
2874 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
2876 VFS_UNLOCK_GIANT(vfslocked);
2881 * Set the access and modification times of a file.
2883 #ifndef _SYS_SYSPROTO_H_
2884 struct lutimes_args {
2886 struct timeval *tptr;
2892 register struct lutimes_args /* {
2894 struct timeval *tptr;
2898 return (kern_lutimes(td, uap->path, UIO_USERSPACE, uap->tptr,
2903 kern_lutimes(struct thread *td, char *path, enum uio_seg pathseg,
2904 struct timeval *tptr, enum uio_seg tptrseg)
2906 struct timespec ts[2];
2908 struct nameidata nd;
2911 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
2913 NDINIT(&nd, LOOKUP, NOFOLLOW | MPSAFE | AUDITVNODE1, pathseg, path, td);
2914 if ((error = namei(&nd)) != 0)
2916 vfslocked = NDHASGIANT(&nd);
2917 NDFREE(&nd, NDF_ONLY_PNBUF);
2918 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
2920 VFS_UNLOCK_GIANT(vfslocked);
2925 * Set the access and modification times of a file.
2927 #ifndef _SYS_SYSPROTO_H_
2928 struct futimes_args {
2930 struct timeval *tptr;
2936 register struct futimes_args /* {
2938 struct timeval *tptr;
2942 return (kern_futimes(td, uap->fd, uap->tptr, UIO_USERSPACE));
2946 kern_futimes(struct thread *td, int fd, struct timeval *tptr,
2947 enum uio_seg tptrseg)
2949 struct timespec ts[2];
2955 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
2957 if ((error = getvnode(td->td_proc->p_fd, fd, &fp)) != 0)
2959 vfslocked = VFS_LOCK_GIANT(fp->f_vnode->v_mount);
2961 vn_lock(fp->f_vnode, LK_EXCLUSIVE | LK_RETRY, td);
2962 AUDIT_ARG(vnode, fp->f_vnode, ARG_VNODE1);
2963 VOP_UNLOCK(fp->f_vnode, 0, td);
2965 error = setutimes(td, fp->f_vnode, ts, 2, tptr == NULL);
2966 VFS_UNLOCK_GIANT(vfslocked);
2972 * Truncate a file given its path name.
2974 #ifndef _SYS_SYSPROTO_H_
2975 struct truncate_args {
2984 register struct truncate_args /* {
2991 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
2995 kern_truncate(struct thread *td, char *path, enum uio_seg pathseg, off_t length)
3001 struct nameidata nd;
3006 NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE | AUDITVNODE1, pathseg, path, td);
3007 if ((error = namei(&nd)) != 0)
3009 vfslocked = NDHASGIANT(&nd);
3011 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0) {
3013 VFS_UNLOCK_GIANT(vfslocked);
3016 NDFREE(&nd, NDF_ONLY_PNBUF);
3017 VOP_LEASE(vp, td, td->td_ucred, LEASE_WRITE);
3018 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
3019 if (vp->v_type == VDIR)
3022 else if ((error = mac_check_vnode_write(td->td_ucred, NOCRED, vp))) {
3025 else if ((error = vn_writechk(vp)) == 0 &&
3026 (error = VOP_ACCESS(vp, VWRITE, td->td_ucred, td)) == 0) {
3028 vattr.va_size = length;
3029 error = VOP_SETATTR(vp, &vattr, td->td_ucred, td);
3032 vn_finished_write(mp);
3033 VFS_UNLOCK_GIANT(vfslocked);
3038 * Truncate a file given a file descriptor.
3040 #ifndef _SYS_SYSPROTO_H_
3041 struct ftruncate_args {
3050 register struct ftruncate_args /* {
3063 AUDIT_ARG(fd, uap->fd);
3064 if (uap->length < 0)
3066 if ((error = getvnode(td->td_proc->p_fd, uap->fd, &fp)) != 0)
3068 if ((fp->f_flag & FWRITE) == 0) {
3073 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
3074 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
3076 VOP_LEASE(vp, td, td->td_ucred, LEASE_WRITE);
3077 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
3078 AUDIT_ARG(vnode, vp, ARG_VNODE1);
3079 if (vp->v_type == VDIR)
3082 else if ((error = mac_check_vnode_write(td->td_ucred, fp->f_cred,
3086 else if ((error = vn_writechk(vp)) == 0) {
3088 vattr.va_size = uap->length;
3089 error = VOP_SETATTR(vp, &vattr, fp->f_cred, td);
3091 VOP_UNLOCK(vp, 0, td);
3092 vn_finished_write(mp);
3094 VFS_UNLOCK_GIANT(vfslocked);
3099 #if defined(COMPAT_43)
3101 * Truncate a file given its path name.
3103 #ifndef _SYS_SYSPROTO_H_
3104 struct otruncate_args {
3112 register struct otruncate_args /* {
3117 struct truncate_args /* {
3123 nuap.path = uap->path;
3124 nuap.length = uap->length;
3125 return (truncate(td, &nuap));
3129 * Truncate a file given a file descriptor.
3131 #ifndef _SYS_SYSPROTO_H_
3132 struct oftruncate_args {
3140 register struct oftruncate_args /* {
3145 struct ftruncate_args /* {
3152 nuap.length = uap->length;
3153 return (ftruncate(td, &nuap));
3155 #endif /* COMPAT_43 */
3158 * Sync an open file.
3160 #ifndef _SYS_SYSPROTO_H_
3168 struct fsync_args /* {
3178 AUDIT_ARG(fd, uap->fd);
3179 if ((error = getvnode(td->td_proc->p_fd, uap->fd, &fp)) != 0)
3182 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
3183 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
3185 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
3186 AUDIT_ARG(vnode, vp, ARG_VNODE1);
3187 if (vp->v_object != NULL) {
3188 VM_OBJECT_LOCK(vp->v_object);
3189 vm_object_page_clean(vp->v_object, 0, 0, 0);
3190 VM_OBJECT_UNLOCK(vp->v_object);
3192 error = VOP_FSYNC(vp, MNT_WAIT, td);
3194 VOP_UNLOCK(vp, 0, td);
3195 vn_finished_write(mp);
3197 VFS_UNLOCK_GIANT(vfslocked);
3203 * Rename files. Source and destination must either both be directories, or
3204 * both not be directories. If target is a directory, it must be empty.
3206 #ifndef _SYS_SYSPROTO_H_
3207 struct rename_args {
3215 register struct rename_args /* {
3221 return (kern_rename(td, uap->from, uap->to, UIO_USERSPACE));
3225 kern_rename(struct thread *td, char *from, char *to, enum uio_seg pathseg)
3227 struct mount *mp = NULL;
3228 struct vnode *tvp, *fvp, *tdvp;
3229 struct nameidata fromnd, tond;
3236 NDINIT(&fromnd, DELETE, LOCKPARENT | LOCKLEAF | SAVESTART | MPSAFE |
3237 AUDITVNODE1, pathseg, from, td);
3239 NDINIT(&fromnd, DELETE, WANTPARENT | SAVESTART | MPSAFE |
3240 AUDITVNODE1, pathseg, from, td);
3242 if ((error = namei(&fromnd)) != 0)
3244 fvfslocked = NDHASGIANT(&fromnd);
3247 error = mac_check_vnode_rename_from(td->td_ucred, fromnd.ni_dvp,
3248 fromnd.ni_vp, &fromnd.ni_cnd);
3249 VOP_UNLOCK(fromnd.ni_dvp, 0, td);
3250 if (fromnd.ni_dvp != fromnd.ni_vp)
3251 VOP_UNLOCK(fromnd.ni_vp, 0, td);
3255 error = vn_start_write(fvp, &mp, V_WAIT | PCATCH);
3257 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3258 vrele(fromnd.ni_dvp);
3262 NDINIT(&tond, RENAME, LOCKPARENT | LOCKLEAF | NOCACHE | SAVESTART |
3263 MPSAFE | AUDITVNODE2, pathseg, to, td);
3264 if (fromnd.ni_vp->v_type == VDIR)
3265 tond.ni_cnd.cn_flags |= WILLBEDIR;
3266 if ((error = namei(&tond)) != 0) {
3267 /* Translate error code for rename("dir1", "dir2/."). */
3268 if (error == EISDIR && fvp->v_type == VDIR)
3270 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3271 vrele(fromnd.ni_dvp);
3273 vn_finished_write(mp);
3276 tvfslocked = NDHASGIANT(&tond);
3280 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
3283 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
3291 * If the source is the same as the destination (that is, if they
3292 * are links to the same vnode), then there is nothing to do.
3298 error = mac_check_vnode_rename_to(td->td_ucred, tdvp,
3299 tond.ni_vp, fromnd.ni_dvp == tdvp, &tond.ni_cnd);
3303 VOP_LEASE(tdvp, td, td->td_ucred, LEASE_WRITE);
3304 if (fromnd.ni_dvp != tdvp) {
3305 VOP_LEASE(fromnd.ni_dvp, td, td->td_ucred, LEASE_WRITE);
3308 VOP_LEASE(tvp, td, td->td_ucred, LEASE_WRITE);
3310 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
3311 tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
3312 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3313 NDFREE(&tond, NDF_ONLY_PNBUF);
3315 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3316 NDFREE(&tond, NDF_ONLY_PNBUF);
3323 vrele(fromnd.ni_dvp);
3326 vrele(tond.ni_startdir);
3327 vn_finished_write(mp);
3329 if (fromnd.ni_startdir)
3330 vrele(fromnd.ni_startdir);
3331 VFS_UNLOCK_GIANT(fvfslocked);
3332 VFS_UNLOCK_GIANT(tvfslocked);
3339 * Make a directory file.
3341 #ifndef _SYS_SYSPROTO_H_
3350 register struct mkdir_args /* {
3356 return (kern_mkdir(td, uap->path, UIO_USERSPACE, uap->mode));
3360 kern_mkdir(struct thread *td, char *path, enum uio_seg segflg, int mode)
3366 struct nameidata nd;
3369 AUDIT_ARG(mode, mode);
3372 NDINIT(&nd, CREATE, LOCKPARENT | SAVENAME | MPSAFE | AUDITVNODE1,
3374 nd.ni_cnd.cn_flags |= WILLBEDIR;
3375 if ((error = namei(&nd)) != 0)
3377 vfslocked = NDHASGIANT(&nd);
3380 NDFREE(&nd, NDF_ONLY_PNBUF);
3382 * XXX namei called with LOCKPARENT but not LOCKLEAF has
3383 * the strange behaviour of leaving the vnode unlocked
3384 * if the target is the same vnode as the parent.
3386 if (vp == nd.ni_dvp)
3391 VFS_UNLOCK_GIANT(vfslocked);
3394 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3395 NDFREE(&nd, NDF_ONLY_PNBUF);
3397 VFS_UNLOCK_GIANT(vfslocked);
3398 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3403 vattr.va_type = VDIR;
3404 FILEDESC_SLOCK(td->td_proc->p_fd);
3405 vattr.va_mode = (mode & ACCESSPERMS) &~ td->td_proc->p_fd->fd_cmask;
3406 FILEDESC_SUNLOCK(td->td_proc->p_fd);
3408 error = mac_check_vnode_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
3413 VOP_LEASE(nd.ni_dvp, td, td->td_ucred, LEASE_WRITE);
3414 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
3418 NDFREE(&nd, NDF_ONLY_PNBUF);
3422 vn_finished_write(mp);
3423 VFS_UNLOCK_GIANT(vfslocked);
3428 * Remove a directory file.
3430 #ifndef _SYS_SYSPROTO_H_
3438 struct rmdir_args /* {
3443 return (kern_rmdir(td, uap->path, UIO_USERSPACE));
3447 kern_rmdir(struct thread *td, char *path, enum uio_seg pathseg)
3452 struct nameidata nd;
3457 NDINIT(&nd, DELETE, LOCKPARENT | LOCKLEAF | MPSAFE | AUDITVNODE1,
3459 if ((error = namei(&nd)) != 0)
3461 vfslocked = NDHASGIANT(&nd);
3463 if (vp->v_type != VDIR) {
3468 * No rmdir "." please.
3470 if (nd.ni_dvp == vp) {
3475 * The root of a mounted filesystem cannot be deleted.
3477 if (vp->v_vflag & VV_ROOT) {
3482 error = mac_check_vnode_delete(td->td_ucred, nd.ni_dvp, vp,
3487 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3488 NDFREE(&nd, NDF_ONLY_PNBUF);
3490 if (nd.ni_dvp == vp)
3494 VFS_UNLOCK_GIANT(vfslocked);
3495 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3499 VOP_LEASE(nd.ni_dvp, td, td->td_ucred, LEASE_WRITE);
3500 VOP_LEASE(vp, td, td->td_ucred, LEASE_WRITE);
3501 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
3502 vn_finished_write(mp);
3504 NDFREE(&nd, NDF_ONLY_PNBUF);
3506 if (nd.ni_dvp == vp)
3510 VFS_UNLOCK_GIANT(vfslocked);
3516 * Read a block of directory entries in a filesystem independent format.
3518 #ifndef _SYS_SYSPROTO_H_
3519 struct ogetdirentries_args {
3527 ogetdirentries(td, uap)
3529 register struct ogetdirentries_args /* {
3538 struct uio auio, kuio;
3539 struct iovec aiov, kiov;
3540 struct dirent *dp, *edp;
3542 int error, eofflag, readcnt, vfslocked;
3545 /* XXX arbitrary sanity limit on `count'. */
3546 if (uap->count > 64 * 1024)
3548 if ((error = getvnode(td->td_proc->p_fd, uap->fd, &fp)) != 0)
3550 if ((fp->f_flag & FREAD) == 0) {
3556 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
3557 if (vp->v_type != VDIR) {
3558 VFS_UNLOCK_GIANT(vfslocked);
3562 aiov.iov_base = uap->buf;
3563 aiov.iov_len = uap->count;
3564 auio.uio_iov = &aiov;
3565 auio.uio_iovcnt = 1;
3566 auio.uio_rw = UIO_READ;
3567 auio.uio_segflg = UIO_USERSPACE;
3569 auio.uio_resid = uap->count;
3570 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
3571 loff = auio.uio_offset = fp->f_offset;
3573 error = mac_check_vnode_readdir(td->td_ucred, vp);
3575 VOP_UNLOCK(vp, 0, td);
3576 VFS_UNLOCK_GIANT(vfslocked);
3581 # if (BYTE_ORDER != LITTLE_ENDIAN)
3582 if (vp->v_mount->mnt_maxsymlinklen <= 0) {
3583 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag,
3585 fp->f_offset = auio.uio_offset;
3590 kuio.uio_iov = &kiov;
3591 kuio.uio_segflg = UIO_SYSSPACE;
3592 kiov.iov_len = uap->count;
3593 MALLOC(dirbuf, caddr_t, uap->count, M_TEMP, M_WAITOK);
3594 kiov.iov_base = dirbuf;
3595 error = VOP_READDIR(vp, &kuio, fp->f_cred, &eofflag,
3597 fp->f_offset = kuio.uio_offset;
3599 readcnt = uap->count - kuio.uio_resid;
3600 edp = (struct dirent *)&dirbuf[readcnt];
3601 for (dp = (struct dirent *)dirbuf; dp < edp; ) {
3602 # if (BYTE_ORDER == LITTLE_ENDIAN)
3604 * The expected low byte of
3605 * dp->d_namlen is our dp->d_type.
3606 * The high MBZ byte of dp->d_namlen
3607 * is our dp->d_namlen.
3609 dp->d_type = dp->d_namlen;
3613 * The dp->d_type is the high byte
3614 * of the expected dp->d_namlen,
3615 * so must be zero'ed.
3619 if (dp->d_reclen > 0) {
3620 dp = (struct dirent *)
3621 ((char *)dp + dp->d_reclen);
3628 error = uiomove(dirbuf, readcnt, &auio);
3630 FREE(dirbuf, M_TEMP);
3633 VOP_UNLOCK(vp, 0, td);
3634 VFS_UNLOCK_GIANT(vfslocked);
3638 if (uap->count == auio.uio_resid &&
3639 (vp->v_vflag & VV_ROOT) &&
3640 (vp->v_mount->mnt_flag & MNT_UNION)) {
3641 struct vnode *tvp = vp;
3642 vp = vp->v_mount->mnt_vnodecovered;
3648 VFS_UNLOCK_GIANT(vfslocked);
3651 VOP_UNLOCK(vp, 0, td);
3652 VFS_UNLOCK_GIANT(vfslocked);
3653 error = copyout(&loff, uap->basep, sizeof(long));
3655 td->td_retval[0] = uap->count - auio.uio_resid;
3658 #endif /* COMPAT_43 */
3661 * Read a block of directory entries in a filesystem independent format.
3663 #ifndef _SYS_SYSPROTO_H_
3664 struct getdirentries_args {
3672 getdirentries(td, uap)
3674 register struct getdirentries_args /* {
3689 AUDIT_ARG(fd, uap->fd);
3690 if ((error = getvnode(td->td_proc->p_fd, uap->fd, &fp)) != 0)
3692 if ((fp->f_flag & FREAD) == 0) {
3698 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
3699 if (vp->v_type != VDIR) {
3700 VFS_UNLOCK_GIANT(vfslocked);
3704 aiov.iov_base = uap->buf;
3705 aiov.iov_len = uap->count;
3706 auio.uio_iov = &aiov;
3707 auio.uio_iovcnt = 1;
3708 auio.uio_rw = UIO_READ;
3709 auio.uio_segflg = UIO_USERSPACE;
3711 auio.uio_resid = uap->count;
3712 /* vn_lock(vp, LK_SHARED | LK_RETRY, td); */
3713 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
3714 AUDIT_ARG(vnode, vp, ARG_VNODE1);
3715 loff = auio.uio_offset = fp->f_offset;
3717 error = mac_check_vnode_readdir(td->td_ucred, vp);
3720 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL,
3722 fp->f_offset = auio.uio_offset;
3724 VOP_UNLOCK(vp, 0, td);
3725 VFS_UNLOCK_GIANT(vfslocked);
3728 if (uap->count == auio.uio_resid &&
3729 (vp->v_vflag & VV_ROOT) &&
3730 (vp->v_mount->mnt_flag & MNT_UNION)) {
3731 struct vnode *tvp = vp;
3732 vp = vp->v_mount->mnt_vnodecovered;
3738 VFS_UNLOCK_GIANT(vfslocked);
3741 VOP_UNLOCK(vp, 0, td);
3742 VFS_UNLOCK_GIANT(vfslocked);
3743 if (uap->basep != NULL) {
3744 error = copyout(&loff, uap->basep, sizeof(long));
3746 td->td_retval[0] = uap->count - auio.uio_resid;
3752 #ifndef _SYS_SYSPROTO_H_
3753 struct getdents_args {
3762 register struct getdents_args /* {
3768 struct getdirentries_args ap;
3771 ap.count = uap->count;
3773 return (getdirentries(td, &ap));
3777 * Set the mode mask for creation of filesystem nodes.
3779 #ifndef _SYS_SYSPROTO_H_
3787 struct umask_args /* {
3791 register struct filedesc *fdp;
3793 FILEDESC_XLOCK(td->td_proc->p_fd);
3794 fdp = td->td_proc->p_fd;
3795 td->td_retval[0] = fdp->fd_cmask;
3796 fdp->fd_cmask = uap->newmask & ALLPERMS;
3797 FILEDESC_XUNLOCK(td->td_proc->p_fd);
3802 * Void all references to file by ripping underlying filesystem away from
3805 #ifndef _SYS_SYSPROTO_H_
3806 struct revoke_args {
3813 register struct revoke_args /* {
3820 struct nameidata nd;
3823 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | MPSAFE | AUDITVNODE1,
3824 UIO_USERSPACE, uap->path, td);
3825 if ((error = namei(&nd)) != 0)
3827 vfslocked = NDHASGIANT(&nd);
3829 NDFREE(&nd, NDF_ONLY_PNBUF);
3830 if (vp->v_type != VCHR) {
3835 error = mac_check_vnode_revoke(td->td_ucred, vp);
3839 error = VOP_GETATTR(vp, &vattr, td->td_ucred, td);
3842 if (td->td_ucred->cr_uid != vattr.va_uid) {
3843 error = priv_check_cred(td->td_ucred, PRIV_VFS_ADMIN,
3849 VOP_REVOKE(vp, REVOKEALL);
3852 VFS_UNLOCK_GIANT(vfslocked);
3857 * Convert a user file descriptor to a kernel file entry.
3858 * A reference on the file entry is held upon returning.
3861 getvnode(fdp, fd, fpp)
3862 struct filedesc *fdp;
3873 FILEDESC_SLOCK(fdp);
3874 if ((u_int)fd >= fdp->fd_nfiles ||
3875 (fp = fdp->fd_ofiles[fd]) == NULL)
3877 else if (fp->f_vnode == NULL) {
3884 FILEDESC_SUNLOCK(fdp);
3891 * Get an (NFS) file handle.
3893 #ifndef _SYS_SYSPROTO_H_
3894 struct lgetfh_args {
3902 register struct lgetfh_args *uap;
3904 struct nameidata nd;
3906 register struct vnode *vp;
3910 error = priv_check(td, PRIV_VFS_GETFH);
3913 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | MPSAFE | AUDITVNODE1,
3914 UIO_USERSPACE, uap->fname, td);
3918 vfslocked = NDHASGIANT(&nd);
3919 NDFREE(&nd, NDF_ONLY_PNBUF);
3921 bzero(&fh, sizeof(fh));
3922 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
3923 error = VOP_VPTOFH(vp, &fh.fh_fid);
3925 VFS_UNLOCK_GIANT(vfslocked);
3928 error = copyout(&fh, uap->fhp, sizeof (fh));
3932 #ifndef _SYS_SYSPROTO_H_
3941 register struct getfh_args *uap;
3943 struct nameidata nd;
3945 register struct vnode *vp;
3949 error = priv_check(td, PRIV_VFS_GETFH);
3952 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | MPSAFE | AUDITVNODE1,
3953 UIO_USERSPACE, uap->fname, td);
3957 vfslocked = NDHASGIANT(&nd);
3958 NDFREE(&nd, NDF_ONLY_PNBUF);
3960 bzero(&fh, sizeof(fh));
3961 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
3962 error = VOP_VPTOFH(vp, &fh.fh_fid);
3964 VFS_UNLOCK_GIANT(vfslocked);
3967 error = copyout(&fh, uap->fhp, sizeof (fh));
3972 * syscall for the rpc.lockd to use to translate a NFS file handle into an
3975 * warning: do not remove the priv_check() call or this becomes one giant
3978 #ifndef _SYS_SYSPROTO_H_
3979 struct fhopen_args {
3980 const struct fhandle *u_fhp;
3987 struct fhopen_args /* {
3988 const struct fhandle *u_fhp;
3992 struct proc *p = td->td_proc;
3997 struct vattr *vap = &vat;
4000 register struct filedesc *fdp = p->p_fd;
4001 int fmode, mode, error, type;
4006 error = priv_check(td, PRIV_VFS_FHOPEN);
4009 fmode = FFLAGS(uap->flags);
4010 /* why not allow a non-read/write open for our lockd? */
4011 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
4013 error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
4016 /* find the mount point */
4017 mp = vfs_getvfs(&fhp.fh_fsid);
4020 vfslocked = VFS_LOCK_GIANT(mp);
4021 /* now give me my vnode, it gets returned to me locked */
4022 error = VFS_FHTOVP(mp, &fhp.fh_fid, &vp);
4026 * from now on we have to make sure not
4027 * to forget about the vnode
4028 * any error that causes an abort must vput(vp)
4029 * just set error = err and 'goto bad;'.
4035 if (vp->v_type == VLNK) {
4039 if (vp->v_type == VSOCK) {
4044 if (fmode & (FWRITE | O_TRUNC)) {
4045 if (vp->v_type == VDIR) {
4049 error = vn_writechk(vp);
4056 if (fmode & O_APPEND)
4059 error = mac_check_vnode_open(td->td_ucred, vp, mode);
4064 error = VOP_ACCESS(vp, mode, td->td_ucred, td);
4068 if (fmode & O_TRUNC) {
4069 VOP_UNLOCK(vp, 0, td); /* XXX */
4070 if ((error = vn_start_write(NULL, &mp, V_WAIT | PCATCH)) != 0) {
4074 VOP_LEASE(vp, td, td->td_ucred, LEASE_WRITE);
4075 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td); /* XXX */
4078 * We don't yet have fp->f_cred, so use td->td_ucred, which
4081 error = mac_check_vnode_write(td->td_ucred, td->td_ucred, vp);
4086 error = VOP_SETATTR(vp, vap, td->td_ucred, td);
4090 vn_finished_write(mp);
4094 error = VOP_OPEN(vp, fmode, td->td_ucred, td, NULL);
4102 * end of vn_open code
4105 if ((error = falloc(td, &nfp, &indx)) != 0) {
4110 /* An extra reference on `nfp' has been held for us by falloc(). */
4116 nfp->f_flag = fmode & FMASK;
4117 nfp->f_type = DTYPE_VNODE;
4118 nfp->f_ops = &vnops;
4120 if (fmode & (O_EXLOCK | O_SHLOCK)) {
4121 lf.l_whence = SEEK_SET;
4124 if (fmode & O_EXLOCK)
4125 lf.l_type = F_WRLCK;
4127 lf.l_type = F_RDLCK;
4129 if ((fmode & FNONBLOCK) == 0)
4131 VOP_UNLOCK(vp, 0, td);
4132 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf,
4135 * The lock request failed. Normally close the
4136 * descriptor but handle the case where someone might
4137 * have dup()d or close()d it when we weren't looking.
4139 fdclose(fdp, fp, indx, td);
4142 * release our private reference
4147 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
4148 fp->f_flag |= FHASLOCK;
4151 VOP_UNLOCK(vp, 0, td);
4154 VFS_UNLOCK_GIANT(vfslocked);
4155 td->td_retval[0] = indx;
4162 VFS_UNLOCK_GIANT(vfslocked);
4167 * Stat an (NFS) file handle.
4169 #ifndef _SYS_SYSPROTO_H_
4170 struct fhstat_args {
4171 struct fhandle *u_fhp;
4178 register struct fhstat_args /* {
4179 struct fhandle *u_fhp;
4190 error = priv_check(td, PRIV_VFS_FHSTAT);
4193 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4196 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
4198 vfslocked = VFS_LOCK_GIANT(mp);
4199 if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp))) {
4201 VFS_UNLOCK_GIANT(vfslocked);
4204 error = vn_stat(vp, &sb, td->td_ucred, NOCRED, td);
4207 VFS_UNLOCK_GIANT(vfslocked);
4210 error = copyout(&sb, uap->sb, sizeof(sb));
4215 * Implement fstatfs() for (NFS) file handles.
4217 #ifndef _SYS_SYSPROTO_H_
4218 struct fhstatfs_args {
4219 struct fhandle *u_fhp;
4226 struct fhstatfs_args /* {
4227 struct fhandle *u_fhp;
4235 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4238 error = kern_fhstatfs(td, fh, &sf);
4241 return (copyout(&sf, uap->buf, sizeof(sf)));
4245 kern_fhstatfs(struct thread *td, fhandle_t fh, struct statfs *buf)
4253 error = priv_check(td, PRIV_VFS_FHSTATFS);
4256 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
4258 vfslocked = VFS_LOCK_GIANT(mp);
4259 error = VFS_FHTOVP(mp, &fh.fh_fid, &vp);
4261 VFS_UNLOCK_GIANT(vfslocked);
4266 error = prison_canseemount(td->td_ucred, mp);
4270 error = mac_check_mount_stat(td->td_ucred, mp);
4275 * Set these in case the underlying filesystem fails to do so.
4278 sp->f_version = STATFS_VERSION;
4279 sp->f_namemax = NAME_MAX;
4280 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
4281 error = VFS_STATFS(mp, sp, td);
4286 VFS_UNLOCK_GIANT(vfslocked);