File:  [DragonFly] / src / sys / kern / vfs_syscalls.c
Revision 1.30: download - view: text, annotated - select for diffs
Tue Mar 16 17:53:53 2004 UTC (10 years, 8 months ago) by dillon
Branches: MAIN
CVS tags: HEAD
Separate chroot() into kern_chroot().  Rename change_dir() to checkvp_chdir()
and reorganize the code to avoid doing weird things to the passed vnode's
lock and ref count in deep subroutines (which lead to buggy code).

Fix a bug in chdir()/kern_chdir() (the namei data was not being freed in all
cases), and also fix a bug in symlink() (missing zfree in error case).

Submitted-by: Paul Herman <pherman@frenchfries.net>
Additional-work-by: dillon

    1: /*
    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.
    9:  *
   10:  * Redistribution and use in source and binary forms, with or without
   11:  * modification, are permitted provided that the following conditions
   12:  * are met:
   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:  * 3. All advertising materials mentioning features or use of this software
   19:  *    must display the following acknowledgement:
   20:  *	This product includes software developed by the University of
   21:  *	California, Berkeley and its contributors.
   22:  * 4. Neither the name of the University nor the names of its contributors
   23:  *    may be used to endorse or promote products derived from this software
   24:  *    without specific prior written permission.
   25:  *
   26:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   27:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   28:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   29:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   30:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   31:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   32:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   33:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   34:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   35:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   36:  * SUCH DAMAGE.
   37:  *
   38:  *	@(#)vfs_syscalls.c	8.13 (Berkeley) 4/15/94
   39:  * $FreeBSD: src/sys/kern/vfs_syscalls.c,v 1.151.2.18 2003/04/04 20:35:58 tegge Exp $
   40:  * $DragonFly: src/sys/kern/vfs_syscalls.c,v 1.30 2004/03/16 17:53:53 dillon Exp $
   41:  */
   42: 
   43: #include <sys/param.h>
   44: #include <sys/systm.h>
   45: #include <sys/buf.h>
   46: #include <sys/sysent.h>
   47: #include <sys/malloc.h>
   48: #include <sys/mount.h>
   49: #include <sys/sysproto.h>
   50: #include <sys/filedesc.h>
   51: #include <sys/kernel.h>
   52: #include <sys/fcntl.h>
   53: #include <sys/file.h>
   54: #include <sys/linker.h>
   55: #include <sys/stat.h>
   56: #include <sys/unistd.h>
   57: #include <sys/vnode.h>
   58: #include <sys/proc.h>
   59: #include <sys/namei.h>
   60: #include <sys/dirent.h>
   61: #include <sys/extattr.h>
   62: #include <sys/kern_syscall.h>
   63: 
   64: #include <machine/limits.h>
   65: #include <vfs/union/union.h>
   66: #include <sys/sysctl.h>
   67: #include <vm/vm.h>
   68: #include <vm/vm_object.h>
   69: #include <vm/vm_zone.h>
   70: #include <vm/vm_page.h>
   71: 
   72: #include <sys/file2.h>
   73: 
   74: static int checkvp_chdir (struct vnode *vn, struct thread *td);
   75: static void checkdirs (struct vnode *olddp);
   76: static int chroot_refuse_vdir_fds (struct filedesc *fdp);
   77: static int getutimes (const struct timeval *, struct timespec *);
   78: static int setfown (struct vnode *, uid_t, gid_t);
   79: static int setfmode (struct vnode *, int);
   80: static int setfflags (struct vnode *, int);
   81: static int setutimes (struct vnode *, const struct timespec *, int);
   82: static int	usermount = 0;	/* if 1, non-root can mount fs. */
   83: 
   84: int (*union_dircheckp) (struct thread *, struct vnode **, struct file *);
   85: 
   86: SYSCTL_INT(_vfs, OID_AUTO, usermount, CTLFLAG_RW, &usermount, 0, "");
   87: 
   88: /*
   89:  * Virtual File System System Calls
   90:  */
   91: 
   92: /*
   93:  * Mount a file system.
   94:  */
   95: /*
   96:  * mount_args(char *type, char *path, int flags, caddr_t data)
   97:  */
   98: /* ARGSUSED */
   99: int
  100: mount(struct mount_args *uap)
  101: {
  102: 	struct thread *td = curthread;
  103: 	struct proc *p = td->td_proc;
  104: 	struct vnode *vp;
  105: 	struct mount *mp;
  106: 	struct vfsconf *vfsp;
  107: 	int error, flag = 0, flag2 = 0;
  108: 	struct vattr va;
  109: 	struct nameidata nd;
  110: 	char fstypename[MFSNAMELEN];
  111: 	lwkt_tokref vlock;
  112: 	lwkt_tokref ilock;
  113: 
  114: 	if (usermount == 0 && (error = suser(td)))
  115: 		return (error);
  116: 	/*
  117: 	 * Do not allow NFS export by non-root users.
  118: 	 */
  119: 	if (SCARG(uap, flags) & MNT_EXPORTED) {
  120: 		error = suser(td);
  121: 		if (error)
  122: 			return (error);
  123: 	}
  124: 	/*
  125: 	 * Silently enforce MNT_NOSUID and MNT_NODEV for non-root users
  126: 	 */
  127: 	if (suser(td)) 
  128: 		SCARG(uap, flags) |= MNT_NOSUID | MNT_NODEV;
  129: 	/*
  130: 	 * Get vnode to be covered
  131: 	 */
  132: 	NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF, UIO_USERSPACE,
  133: 	    SCARG(uap, path), td);
  134: 	if ((error = namei(&nd)) != 0)
  135: 		return (error);
  136: 	NDFREE(&nd, NDF_ONLY_PNBUF);
  137: 	vp = nd.ni_vp;
  138: 	if (SCARG(uap, flags) & MNT_UPDATE) {
  139: 		if ((vp->v_flag & VROOT) == 0) {
  140: 			vput(vp);
  141: 			return (EINVAL);
  142: 		}
  143: 		mp = vp->v_mount;
  144: 		flag = mp->mnt_flag;
  145: 		flag2 = mp->mnt_kern_flag;
  146: 		/*
  147: 		 * We only allow the filesystem to be reloaded if it
  148: 		 * is currently mounted read-only.
  149: 		 */
  150: 		if ((SCARG(uap, flags) & MNT_RELOAD) &&
  151: 		    ((mp->mnt_flag & MNT_RDONLY) == 0)) {
  152: 			vput(vp);
  153: 			return (EOPNOTSUPP);	/* Needs translation */
  154: 		}
  155: 		/*
  156: 		 * Only root, or the user that did the original mount is
  157: 		 * permitted to update it.
  158: 		 */
  159: 		if (mp->mnt_stat.f_owner != p->p_ucred->cr_uid &&
  160: 		    (error = suser(td))) {
  161: 			vput(vp);
  162: 			return (error);
  163: 		}
  164: 		if (vfs_busy(mp, LK_NOWAIT, NULL, td)) {
  165: 			vput(vp);
  166: 			return (EBUSY);
  167: 		}
  168: 		lwkt_gettoken(&vlock, vp->v_interlock);
  169: 		if ((vp->v_flag & VMOUNT) != 0 ||
  170: 		    vp->v_mountedhere != NULL) {
  171: 			lwkt_reltoken(&vlock);
  172: 			vfs_unbusy(mp, td);
  173: 			vput(vp);
  174: 			return (EBUSY);
  175: 		}
  176: 		vp->v_flag |= VMOUNT;
  177: 		lwkt_reltoken(&vlock);
  178: 		mp->mnt_flag |=
  179: 		    SCARG(uap, flags) & (MNT_RELOAD | MNT_FORCE | MNT_UPDATE);
  180: 		VOP_UNLOCK(vp, NULL, 0, td);
  181: 		goto update;
  182: 	}
  183: 	/*
  184: 	 * If the user is not root, ensure that they own the directory
  185: 	 * onto which we are attempting to mount.
  186: 	 */
  187: 	if ((error = VOP_GETATTR(vp, &va, td)) ||
  188: 	    (va.va_uid != p->p_ucred->cr_uid &&
  189: 	     (error = suser(td)))) {
  190: 		vput(vp);
  191: 		return (error);
  192: 	}
  193: 	if ((error = vinvalbuf(vp, V_SAVE, td, 0, 0)) != 0) {
  194: 		vput(vp);
  195: 		return (error);
  196: 	}
  197: 	if (vp->v_type != VDIR) {
  198: 		vput(vp);
  199: 		return (ENOTDIR);
  200: 	}
  201: 	if ((error = copyinstr(SCARG(uap, type), fstypename, MFSNAMELEN, NULL)) != 0) {
  202: 		vput(vp);
  203: 		return (error);
  204: 	}
  205: 	for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
  206: 		if (!strcmp(vfsp->vfc_name, fstypename))
  207: 			break;
  208: 	if (vfsp == NULL) {
  209: 		linker_file_t lf;
  210: 
  211: 		/* Only load modules for root (very important!) */
  212: 		if ((error = suser(td)) != 0) {
  213: 			vput(vp);
  214: 			return error;
  215: 		}
  216: 		error = linker_load_file(fstypename, &lf);
  217: 		if (error || lf == NULL) {
  218: 			vput(vp);
  219: 			if (lf == NULL)
  220: 				error = ENODEV;
  221: 			return error;
  222: 		}
  223: 		lf->userrefs++;
  224: 		/* lookup again, see if the VFS was loaded */
  225: 		for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
  226: 			if (!strcmp(vfsp->vfc_name, fstypename))
  227: 				break;
  228: 		if (vfsp == NULL) {
  229: 			lf->userrefs--;
  230: 			linker_file_unload(lf);
  231: 			vput(vp);
  232: 			return (ENODEV);
  233: 		}
  234: 	}
  235: 	lwkt_gettoken(&vlock, vp->v_interlock);
  236: 	if ((vp->v_flag & VMOUNT) != 0 ||
  237: 	    vp->v_mountedhere != NULL) {
  238: 		lwkt_reltoken(&vlock);
  239: 		vput(vp);
  240: 		return (EBUSY);
  241: 	}
  242: 	vp->v_flag |= VMOUNT;
  243: 	lwkt_reltoken(&vlock);
  244: 
  245: 	/*
  246: 	 * Allocate and initialize the filesystem.
  247: 	 */
  248: 	mp = malloc(sizeof(struct mount), M_MOUNT, M_WAITOK);
  249: 	bzero((char *)mp, (u_long)sizeof(struct mount));
  250: 	TAILQ_INIT(&mp->mnt_nvnodelist);
  251: 	TAILQ_INIT(&mp->mnt_reservedvnlist);
  252: 	mp->mnt_nvnodelistsize = 0;
  253: 	lockinit(&mp->mnt_lock, 0, "vfslock", 0, LK_NOPAUSE);
  254: 	vfs_busy(mp, LK_NOWAIT, NULL, td);
  255: 	mp->mnt_op = vfsp->vfc_vfsops;
  256: 	mp->mnt_vfc = vfsp;
  257: 	vfsp->vfc_refcount++;
  258: 	mp->mnt_stat.f_type = vfsp->vfc_typenum;
  259: 	mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK;
  260: 	strncpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN);
  261: 	mp->mnt_vnodecovered = vp;
  262: 	mp->mnt_stat.f_owner = p->p_ucred->cr_uid;
  263: 	mp->mnt_iosize_max = DFLTPHYS;
  264: 	VOP_UNLOCK(vp, NULL, 0, td);
  265: update:
  266: 	/*
  267: 	 * Set the mount level flags.
  268: 	 */
  269: 	if (SCARG(uap, flags) & MNT_RDONLY)
  270: 		mp->mnt_flag |= MNT_RDONLY;
  271: 	else if (mp->mnt_flag & MNT_RDONLY)
  272: 		mp->mnt_kern_flag |= MNTK_WANTRDWR;
  273: 	mp->mnt_flag &=~ (MNT_NOSUID | MNT_NOEXEC | MNT_NODEV |
  274: 	    MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_NOATIME |
  275: 	    MNT_NOSYMFOLLOW | MNT_IGNORE |
  276: 	    MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR);
  277: 	mp->mnt_flag |= SCARG(uap, flags) & (MNT_NOSUID | MNT_NOEXEC |
  278: 	    MNT_NODEV | MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_FORCE |
  279: 	    MNT_NOSYMFOLLOW | MNT_IGNORE |
  280: 	    MNT_NOATIME | MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR);
  281: 	/*
  282: 	 * Mount the filesystem.
  283: 	 * XXX The final recipients of VFS_MOUNT just overwrite the ndp they
  284: 	 * get.  No freeing of cn_pnbuf.
  285: 	 */
  286: 	error = VFS_MOUNT(mp, SCARG(uap, path), SCARG(uap, data), &nd, td);
  287: 	if (mp->mnt_flag & MNT_UPDATE) {
  288: 		if (mp->mnt_kern_flag & MNTK_WANTRDWR)
  289: 			mp->mnt_flag &= ~MNT_RDONLY;
  290: 		mp->mnt_flag &=~ (MNT_UPDATE | MNT_RELOAD | MNT_FORCE);
  291: 		mp->mnt_kern_flag &=~ MNTK_WANTRDWR;
  292: 		if (error) {
  293: 			mp->mnt_flag = flag;
  294: 			mp->mnt_kern_flag = flag2;
  295: 		}
  296: 		vfs_unbusy(mp, td);
  297: 		lwkt_gettoken(&vlock, vp->v_interlock);
  298: 		vp->v_flag &= ~VMOUNT;
  299: 		lwkt_reltoken(&vlock);
  300: 		vrele(vp);
  301: 		return (error);
  302: 	}
  303: 	vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
  304: 	/*
  305: 	 * Put the new filesystem on the mount list after root.
  306: 	 */
  307: 	cache_purge(vp);
  308: 	if (!error) {
  309: 		lwkt_gettoken(&vlock, vp->v_interlock);
  310: 		vp->v_flag &= ~VMOUNT;
  311: 		vp->v_mountedhere = mp;
  312: 		lwkt_reltoken(&vlock);
  313: 		lwkt_gettoken(&ilock, &mountlist_token);
  314: 		TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list);
  315: 		lwkt_reltoken(&ilock);
  316: 		checkdirs(vp);
  317: 		VOP_UNLOCK(vp, NULL, 0, td);
  318: 		error = vfs_allocate_syncvnode(mp);
  319: 		vfs_unbusy(mp, td);
  320: 		if ((error = VFS_START(mp, 0, td)) != 0)
  321: 			vrele(vp);
  322: 	} else {
  323: 		lwkt_gettoken(&vlock, vp->v_interlock);
  324: 		vp->v_flag &= ~VMOUNT;
  325: 		lwkt_reltoken(&vlock);
  326: 		mp->mnt_vfc->vfc_refcount--;
  327: 		vfs_unbusy(mp, td);
  328: 		free((caddr_t)mp, M_MOUNT);
  329: 		vput(vp);
  330: 	}
  331: 	return (error);
  332: }
  333: 
  334: /*
  335:  * Scan all active processes to see if any of them have a current
  336:  * or root directory onto which the new filesystem has just been
  337:  * mounted. If so, replace them with the new mount point.
  338:  */
  339: static void
  340: checkdirs(struct vnode *olddp)
  341: {
  342: 	struct filedesc *fdp;
  343: 	struct vnode *newdp;
  344: 	struct proc *p;
  345: 
  346: 	if (olddp->v_usecount == 1)
  347: 		return;
  348: 	if (VFS_ROOT(olddp->v_mountedhere, &newdp))
  349: 		panic("mount: lost mount");
  350: 	FOREACH_PROC_IN_SYSTEM(p) {
  351: 		fdp = p->p_fd;
  352: 		if (fdp->fd_cdir == olddp) {
  353: 			vrele(fdp->fd_cdir);
  354: 			VREF(newdp);
  355: 			fdp->fd_cdir = newdp;
  356: 		}
  357: 		if (fdp->fd_rdir == olddp) {
  358: 			vrele(fdp->fd_rdir);
  359: 			VREF(newdp);
  360: 			fdp->fd_rdir = newdp;
  361: 		}
  362: 	}
  363: 	if (rootvnode == olddp) {
  364: 		vrele(rootvnode);
  365: 		VREF(newdp);
  366: 		rootvnode = newdp;
  367: 		vfs_cache_setroot(rootvnode);
  368: 	}
  369: 	vput(newdp);
  370: }
  371: 
  372: /*
  373:  * Unmount a file system.
  374:  *
  375:  * Note: unmount takes a path to the vnode mounted on as argument,
  376:  * not special file (as before).
  377:  */
  378: /*
  379:  * umount_args(char *path, int flags)
  380:  */
  381: /* ARGSUSED */
  382: int
  383: unmount(struct unmount_args *uap)
  384: {
  385: 	struct thread *td = curthread;
  386: 	struct proc *p = td->td_proc;
  387: 	struct vnode *vp;
  388: 	struct mount *mp;
  389: 	int error;
  390: 	struct nameidata nd;
  391: 
  392: 	KKASSERT(p);
  393: 	NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF, UIO_USERSPACE,
  394: 	    SCARG(uap, path), td);
  395: 	if ((error = namei(&nd)) != 0)
  396: 		return (error);
  397: 	vp = nd.ni_vp;
  398: 	NDFREE(&nd, NDF_ONLY_PNBUF);
  399: 	mp = vp->v_mount;
  400: 
  401: 	/*
  402: 	 * Only root, or the user that did the original mount is
  403: 	 * permitted to unmount this filesystem.
  404: 	 */
  405: 	if ((mp->mnt_stat.f_owner != p->p_ucred->cr_uid) &&
  406: 	    (error = suser(td))) {
  407: 		vput(vp);
  408: 		return (error);
  409: 	}
  410: 
  411: 	/*
  412: 	 * Don't allow unmounting the root file system.
  413: 	 */
  414: 	if (mp->mnt_flag & MNT_ROOTFS) {
  415: 		vput(vp);
  416: 		return (EINVAL);
  417: 	}
  418: 
  419: 	/*
  420: 	 * Must be the root of the filesystem
  421: 	 */
  422: 	if ((vp->v_flag & VROOT) == 0) {
  423: 		vput(vp);
  424: 		return (EINVAL);
  425: 	}
  426: 	vput(vp);
  427: 	return (dounmount(mp, SCARG(uap, flags), td));
  428: }
  429: 
  430: /*
  431:  * Do the actual file system unmount.
  432:  */
  433: int
  434: dounmount(struct mount *mp, int flags, struct thread *td)
  435: {
  436: 	struct vnode *coveredvp;
  437: 	int error;
  438: 	int async_flag;
  439: 	lwkt_tokref ilock;
  440: 
  441: 	lwkt_gettoken(&ilock, &mountlist_token);
  442: 	if (mp->mnt_kern_flag & MNTK_UNMOUNT) {
  443: 		lwkt_reltoken(&ilock);
  444: 		return (EBUSY);
  445: 	}
  446: 	mp->mnt_kern_flag |= MNTK_UNMOUNT;
  447: 	/* Allow filesystems to detect that a forced unmount is in progress. */
  448: 	if (flags & MNT_FORCE)
  449: 		mp->mnt_kern_flag |= MNTK_UNMOUNTF;
  450: 	error = lockmgr(&mp->mnt_lock, LK_DRAIN | LK_INTERLOCK |
  451: 	    ((flags & MNT_FORCE) ? 0 : LK_NOWAIT), &ilock, td);
  452: 	if (error) {
  453: 		mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
  454: 		if (mp->mnt_kern_flag & MNTK_MWAIT)
  455: 			wakeup((caddr_t)mp);
  456: 		return (error);
  457: 	}
  458: 
  459: 	if (mp->mnt_flag & MNT_EXPUBLIC)
  460: 		vfs_setpublicfs(NULL, NULL, NULL);
  461: 
  462: 	vfs_msync(mp, MNT_WAIT);
  463: 	async_flag = mp->mnt_flag & MNT_ASYNC;
  464: 	mp->mnt_flag &=~ MNT_ASYNC;
  465: 	cache_purgevfs(mp);	/* remove cache entries for this file sys */
  466: 	if (mp->mnt_syncer != NULL)
  467: 		vrele(mp->mnt_syncer);
  468: 	if (((mp->mnt_flag & MNT_RDONLY) ||
  469: 	     (error = VFS_SYNC(mp, MNT_WAIT, td)) == 0) ||
  470: 	    (flags & MNT_FORCE))
  471: 		error = VFS_UNMOUNT(mp, flags, td);
  472: 	lwkt_gettokref(&ilock);
  473: 	if (error) {
  474: 		if (mp->mnt_syncer == NULL)
  475: 			vfs_allocate_syncvnode(mp);
  476: 		mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
  477: 		mp->mnt_flag |= async_flag;
  478: 		lockmgr(&mp->mnt_lock, LK_RELEASE | LK_INTERLOCK | LK_REENABLE,
  479: 		    &ilock, td);
  480: 		if (mp->mnt_kern_flag & MNTK_MWAIT)
  481: 			wakeup((caddr_t)mp);
  482: 		return (error);
  483: 	}
  484: 	TAILQ_REMOVE(&mountlist, mp, mnt_list);
  485: 	if ((coveredvp = mp->mnt_vnodecovered) != NULLVP) {
  486: 		coveredvp->v_mountedhere = NULL;
  487: 		vrele(coveredvp);
  488: 	}
  489: 	mp->mnt_vfc->vfc_refcount--;
  490: 	if (!TAILQ_EMPTY(&mp->mnt_nvnodelist))
  491: 		panic("unmount: dangling vnode");
  492: 	lockmgr(&mp->mnt_lock, LK_RELEASE | LK_INTERLOCK, &ilock, td);
  493: 	if (mp->mnt_kern_flag & MNTK_MWAIT)
  494: 		wakeup((caddr_t)mp);
  495: 	free((caddr_t)mp, M_MOUNT);
  496: 	return (0);
  497: }
  498: 
  499: /*
  500:  * Sync each mounted filesystem.
  501:  */
  502: 
  503: #ifdef DEBUG
  504: static int syncprt = 0;
  505: SYSCTL_INT(_debug, OID_AUTO, syncprt, CTLFLAG_RW, &syncprt, 0, "");
  506: #endif
  507: 
  508: /* ARGSUSED */
  509: int
  510: sync(struct sync_args *uap)
  511: {
  512: 	struct thread *td = curthread;
  513: 	struct mount *mp, *nmp;
  514: 	lwkt_tokref ilock;
  515: 	int asyncflag;
  516: 
  517: 	lwkt_gettoken(&ilock, &mountlist_token);
  518: 	for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
  519: 		if (vfs_busy(mp, LK_NOWAIT, &ilock, td)) {
  520: 			nmp = TAILQ_NEXT(mp, mnt_list);
  521: 			continue;
  522: 		}
  523: 		if ((mp->mnt_flag & MNT_RDONLY) == 0) {
  524: 			asyncflag = mp->mnt_flag & MNT_ASYNC;
  525: 			mp->mnt_flag &= ~MNT_ASYNC;
  526: 			vfs_msync(mp, MNT_NOWAIT);
  527: 			VFS_SYNC(mp, MNT_NOWAIT, td);
  528: 			mp->mnt_flag |= asyncflag;
  529: 		}
  530: 		lwkt_gettokref(&ilock);
  531: 		nmp = TAILQ_NEXT(mp, mnt_list);
  532: 		vfs_unbusy(mp, td);
  533: 	}
  534: 	lwkt_reltoken(&ilock);
  535: #if 0
  536: /*
  537:  * XXX don't call vfs_bufstats() yet because that routine
  538:  * was not imported in the Lite2 merge.
  539:  */
  540: #ifdef DIAGNOSTIC
  541: 	if (syncprt)
  542: 		vfs_bufstats();
  543: #endif /* DIAGNOSTIC */
  544: #endif
  545: 	return (0);
  546: }
  547: 
  548: /* XXX PRISON: could be per prison flag */
  549: static int prison_quotas;
  550: #if 0
  551: SYSCTL_INT(_kern_prison, OID_AUTO, quotas, CTLFLAG_RW, &prison_quotas, 0, "");
  552: #endif
  553: 
  554: /*
  555:  *  quotactl_args(char *path, int fcmd, int uid, caddr_t arg)
  556:  *
  557:  * Change filesystem quotas.
  558:  */
  559: /* ARGSUSED */
  560: int
  561: quotactl(struct quotactl_args *uap)
  562: {
  563: 	struct thread *td = curthread;
  564: 	struct proc *p = td->td_proc;
  565: 	struct mount *mp;
  566: 	int error;
  567: 	struct nameidata nd;
  568: 
  569: 	KKASSERT(p);
  570: 	if (p->p_ucred->cr_prison && !prison_quotas)
  571: 		return (EPERM);
  572: 	NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE,
  573: 	    SCARG(uap, path), td);
  574: 	if ((error = namei(&nd)) != 0)
  575: 		return (error);
  576: 	mp = nd.ni_vp->v_mount;
  577: 	NDFREE(&nd, NDF_ONLY_PNBUF);
  578: 	vrele(nd.ni_vp);
  579: 	return (VFS_QUOTACTL(mp, SCARG(uap, cmd), SCARG(uap, uid),
  580: 	    SCARG(uap, arg), td));
  581: }
  582: 
  583: int
  584: kern_statfs(struct nameidata *nd, struct statfs *buf)
  585: {
  586: 	struct thread *td = curthread;
  587: 	struct mount *mp;
  588: 	struct statfs *sp;
  589: 	int error;
  590: 
  591: 	error = namei(nd);
  592: 	if (error)
  593: 		return (error);
  594: 	mp = nd->ni_vp->v_mount;
  595: 	sp = &mp->mnt_stat;
  596: 	NDFREE(nd, NDF_ONLY_PNBUF);
  597: 	vrele(nd->ni_vp);
  598: 	error = VFS_STATFS(mp, sp, td);
  599: 	if (error)
  600: 		return (error);
  601: 	sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
  602: 	bcopy(sp, buf, sizeof(*buf));
  603: 	/* Only root should have access to the fsid's. */
  604: 	if (suser(td))
  605: 		buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
  606: 	return (0);
  607: }
  608: 
  609: /*
  610:  * statfs_args(char *path, struct statfs *buf)
  611:  *
  612:  * Get filesystem statistics.
  613:  */
  614: int
  615: statfs(struct statfs_args *uap)
  616: {
  617: 	struct thread *td = curthread;
  618: 	struct nameidata nd;
  619: 	struct statfs buf;
  620: 	int error;
  621: 
  622: 	NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE, uap->path, td);
  623: 
  624: 	error = kern_statfs(&nd, &buf);
  625: 
  626: 	if (error == 0)
  627: 		error = copyout(&buf, uap->buf, sizeof(*uap->buf));
  628: 	return (error);
  629: }
  630: 
  631: int
  632: kern_fstatfs(int fd, struct statfs *buf)
  633: {
  634: 	struct thread *td = curthread;
  635: 	struct proc *p = td->td_proc;
  636: 	struct file *fp;
  637: 	struct mount *mp;
  638: 	struct statfs *sp;
  639: 	int error;
  640: 
  641: 	KKASSERT(p);
  642: 	error = getvnode(p->p_fd, fd, &fp);
  643: 	if (error)
  644: 		return (error);
  645: 	mp = ((struct vnode *)fp->f_data)->v_mount;
  646: 	if (mp == NULL)
  647: 		return (EBADF);
  648: 	sp = &mp->mnt_stat;
  649: 	error = VFS_STATFS(mp, sp, td);
  650: 	if (error)
  651: 		return (error);
  652: 	sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
  653: 	bcopy(sp, buf, sizeof(*buf));
  654: 	/* Only root should have access to the fsid's. */
  655: 	if (suser(td))
  656: 		buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
  657: 	return (0);
  658: }
  659: 
  660: /*
  661:  * fstatfs_args(int fd, struct statfs *buf)
  662:  *
  663:  * Get filesystem statistics.
  664:  */
  665: int
  666: fstatfs(struct fstatfs_args *uap)
  667: {
  668: 	struct statfs buf;
  669: 	int error;
  670: 
  671: 	error = kern_fstatfs(uap->fd, &buf);
  672: 
  673: 	if (error == 0)
  674: 		error = copyout(&buf, uap->buf, sizeof(*uap->buf));
  675: 	return (error);
  676: }
  677: 
  678: /*
  679:  * getfsstat_args(struct statfs *buf, long bufsize, int flags)
  680:  *
  681:  * Get statistics on all filesystems.
  682:  */
  683: /* ARGSUSED */
  684: int
  685: getfsstat(struct getfsstat_args *uap)
  686: {
  687: 	struct thread *td = curthread;
  688: 	struct mount *mp, *nmp;
  689: 	struct statfs *sp;
  690: 	caddr_t sfsp;
  691: 	lwkt_tokref ilock;
  692: 	long count, maxcount, error;
  693: 
  694: 	maxcount = SCARG(uap, bufsize) / sizeof(struct statfs);
  695: 	sfsp = (caddr_t)SCARG(uap, buf);
  696: 	count = 0;
  697: 	lwkt_gettoken(&ilock, &mountlist_token);
  698: 	for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
  699: 		if (vfs_busy(mp, LK_NOWAIT, &ilock, td)) {
  700: 			nmp = TAILQ_NEXT(mp, mnt_list);
  701: 			continue;
  702: 		}
  703: 		if (sfsp && count < maxcount) {
  704: 			sp = &mp->mnt_stat;
  705: 			/*
  706: 			 * If MNT_NOWAIT or MNT_LAZY is specified, do not
  707: 			 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
  708: 			 * overrides MNT_WAIT.
  709: 			 */
  710: 			if (((SCARG(uap, flags) & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
  711: 			    (SCARG(uap, flags) & MNT_WAIT)) &&
  712: 			    (error = VFS_STATFS(mp, sp, td))) {
  713: 				lwkt_gettokref(&ilock);
  714: 				nmp = TAILQ_NEXT(mp, mnt_list);
  715: 				vfs_unbusy(mp, td);
  716: 				continue;
  717: 			}
  718: 			sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
  719: 			error = copyout((caddr_t)sp, sfsp, sizeof(*sp));
  720: 			if (error) {
  721: 				vfs_unbusy(mp, td);
  722: 				return (error);
  723: 			}
  724: 			sfsp += sizeof(*sp);
  725: 		}
  726: 		count++;
  727: 		lwkt_gettokref(&ilock);
  728: 		nmp = TAILQ_NEXT(mp, mnt_list);
  729: 		vfs_unbusy(mp, td);
  730: 	}
  731: 	lwkt_reltoken(&ilock);
  732: 	if (sfsp && count > maxcount)
  733: 		uap->sysmsg_result = maxcount;
  734: 	else
  735: 		uap->sysmsg_result = count;
  736: 	return (0);
  737: }
  738: 
  739: /*
  740:  * fchdir_args(int fd)
  741:  *
  742:  * Change current working directory to a given file descriptor.
  743:  */
  744: /* ARGSUSED */
  745: int
  746: fchdir(struct fchdir_args *uap)
  747: {
  748: 	struct thread *td = curthread;
  749: 	struct proc *p = td->td_proc;
  750: 	struct filedesc *fdp = p->p_fd;
  751: 	struct vnode *vp, *tdp;
  752: 	struct mount *mp;
  753: 	struct file *fp;
  754: 	int error;
  755: 
  756: 	if ((error = getvnode(fdp, SCARG(uap, fd), &fp)) != 0)
  757: 		return (error);
  758: 	vp = (struct vnode *)fp->f_data;
  759: 	VREF(vp);
  760: 	vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
  761: 	if (vp->v_type != VDIR)
  762: 		error = ENOTDIR;
  763: 	else
  764: 		error = VOP_ACCESS(vp, VEXEC, p->p_ucred, td);
  765: 	while (!error && (mp = vp->v_mountedhere) != NULL) {
  766: 		if (vfs_busy(mp, 0, NULL, td))
  767: 			continue;
  768: 		error = VFS_ROOT(mp, &tdp);
  769: 		vfs_unbusy(mp, td);
  770: 		if (error)
  771: 			break;
  772: 		vput(vp);
  773: 		vp = tdp;
  774: 	}
  775: 	if (error) {
  776: 		vput(vp);
  777: 		return (error);
  778: 	}
  779: 	VOP_UNLOCK(vp, NULL, 0, td);
  780: 	vrele(fdp->fd_cdir);
  781: 	fdp->fd_cdir = vp;
  782: 	return (0);
  783: }
  784: 
  785: int
  786: kern_chdir(struct nameidata *nd)
  787: {
  788: 	struct thread *td = curthread;
  789: 	struct proc *p = td->td_proc;
  790: 	struct filedesc *fdp = p->p_fd;
  791: 	int error;
  792: 
  793: 	if ((error = namei(nd)) != 0)
  794: 		return (error);
  795: 	if ((error = checkvp_chdir(nd->ni_vp, td)) == 0) {
  796: 		vrele(fdp->fd_cdir);
  797: 		fdp->fd_cdir = nd->ni_vp;
  798: 		VREF(fdp->fd_cdir);
  799: 	}
  800: 	NDFREE(nd, ~(NDF_NO_FREE_PNBUF | NDF_NO_VP_PUT));
  801: 	return (error);
  802: }
  803: 
  804: /*
  805:  * chdir_args(char *path)
  806:  *
  807:  * Change current working directory (``.'').
  808:  */
  809: int
  810: chdir(struct chdir_args *uap)
  811: {
  812: 	struct thread *td = curthread;
  813: 	struct nameidata nd;
  814: 	int error;
  815: 
  816: 	NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF, UIO_USERSPACE,
  817: 	    uap->path, td);
  818: 
  819: 	error = kern_chdir(&nd);
  820: 
  821: 	return (error);
  822: }
  823: 
  824: /*
  825:  * Helper function for raised chroot(2) security function:  Refuse if
  826:  * any filedescriptors are open directories.
  827:  */
  828: static int
  829: chroot_refuse_vdir_fds(fdp)
  830: 	struct filedesc *fdp;
  831: {
  832: 	struct vnode *vp;
  833: 	struct file *fp;
  834: 	int error;
  835: 	int fd;
  836: 
  837: 	for (fd = 0; fd < fdp->fd_nfiles ; fd++) {
  838: 		error = getvnode(fdp, fd, &fp);
  839: 		if (error)
  840: 			continue;
  841: 		vp = (struct vnode *)fp->f_data;
  842: 		if (vp->v_type != VDIR)
  843: 			continue;
  844: 		return(EPERM);
  845: 	}
  846: 	return (0);
  847: }
  848: 
  849: /*
  850:  * This sysctl determines if we will allow a process to chroot(2) if it
  851:  * has a directory open:
  852:  *	0: disallowed for all processes.
  853:  *	1: allowed for processes that were not already chroot(2)'ed.
  854:  *	2: allowed for all processes.
  855:  */
  856: 
  857: static int chroot_allow_open_directories = 1;
  858: 
  859: SYSCTL_INT(_kern, OID_AUTO, chroot_allow_open_directories, CTLFLAG_RW,
  860:      &chroot_allow_open_directories, 0, "");
  861: 
  862: /*
  863:  * Chroot to the specified vnode.  vp must be locked and referenced on
  864:  * call, and will be left locked and referenced on return.  This routine
  865:  * may acquire additional refs on the vnode when associating it with
  866:  * the process's root and/or jail dirs.
  867:  */
  868: int
  869: kern_chroot(struct vnode *vp)
  870: {
  871: 	struct thread *td = curthread;
  872: 	struct proc *p = td->td_proc;
  873: 	struct filedesc *fdp = p->p_fd;
  874: 	int error;
  875: 
  876: 	/*
  877: 	 * Only root can chroot
  878: 	 */
  879: 	if ((error = suser_cred(p->p_ucred, PRISON_ROOT)) != 0)
  880: 		return (error);
  881: 
  882: 	/*
  883: 	 * Disallow open directory descriptors (fchdir() breakouts).
  884: 	 */
  885: 	if (chroot_allow_open_directories == 0 ||
  886: 	   (chroot_allow_open_directories == 1 && fdp->fd_rdir != rootvnode)) {
  887: 		if ((error = chroot_refuse_vdir_fds(fdp)) != 0)
  888: 			return (error);
  889: 	}
  890: 
  891: 	/*
  892: 	 * Check the validity of vp as a directory to change to and 
  893: 	 * associate it with rdir/jdir.
  894: 	 */
  895: 	if ((error = checkvp_chdir(vp, td)) == 0) {
  896: 		vrele(fdp->fd_rdir);
  897: 		fdp->fd_rdir = vp;
  898: 		VREF(fdp->fd_rdir);
  899: 		if (fdp->fd_jdir == NULL) {
  900: 			fdp->fd_jdir = vp;
  901: 			VREF(fdp->fd_jdir);
  902: 		}
  903: 	}
  904: 	return (error);
  905: }
  906: 
  907: /*
  908:  * chroot_args(char *path)
  909:  *
  910:  * Change notion of root (``/'') directory.
  911:  */
  912: /* ARGSUSED */
  913: int
  914: chroot(struct chroot_args *uap)
  915: {
  916: 	struct thread *td = curthread;
  917: 	struct nameidata nd;
  918: 	int error;
  919: 
  920: 	KKASSERT(td->td_proc);
  921: 	NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF, UIO_USERSPACE,
  922: 		SCARG(uap, path), td);
  923: 	if ((error = namei(&nd)) == 0) {
  924: 		error = kern_chroot(nd.ni_vp);
  925: 		NDFREE(&nd, ~(NDF_NO_FREE_PNBUF | NDF_NO_VP_PUT));
  926: 	}
  927: 	return (error);
  928: }
  929: 
  930: /*
  931:  * Common routine for chroot and chdir.  Given a locked, referenced vnode,
  932:  * determine whether it is legal to chdir to the vnode.  The vnode's state
  933:  * is not changed by this call.
  934:  */
  935: int
  936: checkvp_chdir(struct vnode *vp, struct thread *td)
  937: {
  938: 	int error;
  939: 
  940: 	if (vp->v_type != VDIR)
  941: 		error = ENOTDIR;
  942: 	else
  943: 		error = VOP_ACCESS(vp, VEXEC, td->td_proc->p_ucred, td);
  944: 	return (error);
  945: }
  946: 
  947: int
  948: kern_open(struct nameidata *nd, int oflags, int mode, int *res)
  949: {
  950: 	struct thread *td = curthread;
  951: 	struct proc *p = td->td_proc;
  952: 	struct filedesc *fdp = p->p_fd;
  953: 	struct file *fp;
  954: 	struct vnode *vp;
  955: 	int cmode, flags;
  956: 	struct file *nfp;
  957: 	int type, indx, error;
  958: 	struct flock lf;
  959: 
  960: 	if ((oflags & O_ACCMODE) == O_ACCMODE)
  961: 		return (EINVAL);
  962: 	flags = FFLAGS(oflags);
  963: 	error = falloc(p, &nfp, &indx);
  964: 	if (error)
  965: 		return (error);
  966: 	fp = nfp;
  967: 	cmode = ((mode &~ fdp->fd_cmask) & ALLPERMS) &~ S_ISTXT;
  968: 	p->p_dupfd = -indx - 1;			/* XXX check for fdopen */
  969: 	/*
  970: 	 * Bump the ref count to prevent another process from closing
  971: 	 * the descriptor while we are blocked in vn_open()
  972: 	 */
  973: 	fhold(fp);
  974: 	error = vn_open(nd, flags, cmode);
  975: 	if (error) {
  976: 		/*
  977: 		 * release our own reference
  978: 		 */
  979: 		fdrop(fp, td);
  980: 
  981: 		/*
  982: 		 * handle special fdopen() case.  bleh.  dupfdopen() is
  983: 		 * responsible for dropping the old contents of ofiles[indx]
  984: 		 * if it succeeds.
  985: 		 */
  986: 		if ((error == ENODEV || error == ENXIO) &&
  987: 		    p->p_dupfd >= 0 &&			/* XXX from fdopen */
  988: 		    (error =
  989: 			dupfdopen(fdp, indx, p->p_dupfd, flags, error)) == 0) {
  990: 			*res = indx;
  991: 			return (0);
  992: 		}
  993: 		/*
  994: 		 * Clean up the descriptor, but only if another thread hadn't
  995: 		 * replaced or closed it.
  996: 		 */
  997: 		if (fdp->fd_ofiles[indx] == fp) {
  998: 			fdp->fd_ofiles[indx] = NULL;
  999: 			fdrop(fp, td);
 1000: 		}
 1001: 
 1002: 		if (error == ERESTART)
 1003: 			error = EINTR;
 1004: 		return (error);
 1005: 	}
 1006: 	p->p_dupfd = 0;
 1007: 	NDFREE(nd, NDF_ONLY_PNBUF);
 1008: 	vp = nd->ni_vp;
 1009: 
 1010: 	/*
 1011: 	 * There should be 2 references on the file, one from the descriptor
 1012: 	 * table, and one for us.
 1013: 	 *
 1014: 	 * Handle the case where someone closed the file (via its file
 1015: 	 * descriptor) while we were blocked.  The end result should look
 1016: 	 * like opening the file succeeded but it was immediately closed.
 1017: 	 */
 1018: 	if (fp->f_count == 1) {
 1019: 		KASSERT(fdp->fd_ofiles[indx] != fp,
 1020: 		    ("Open file descriptor lost all refs"));
 1021: 		VOP_UNLOCK(vp, NULL, 0, td);
 1022: 		vn_close(vp, flags & FMASK, td);
 1023: 		fdrop(fp, td);
 1024: 		*res = indx;
 1025: 		return 0;
 1026: 	}
 1027: 
 1028: 	fp->f_data = (caddr_t)vp;
 1029: 	fp->f_flag = flags & FMASK;
 1030: 	fp->f_ops = &vnops;
 1031: 	fp->f_type = (vp->v_type == VFIFO ? DTYPE_FIFO : DTYPE_VNODE);
 1032: 	if (flags & (O_EXLOCK | O_SHLOCK)) {
 1033: 		lf.l_whence = SEEK_SET;
 1034: 		lf.l_start = 0;
 1035: 		lf.l_len = 0;
 1036: 		if (flags & O_EXLOCK)
 1037: 			lf.l_type = F_WRLCK;
 1038: 		else
 1039: 			lf.l_type = F_RDLCK;
 1040: 		type = F_FLOCK;
 1041: 		if ((flags & FNONBLOCK) == 0)
 1042: 			type |= F_WAIT;
 1043: 		VOP_UNLOCK(vp, NULL, 0, td);
 1044: 		if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
 1045: 			/*
 1046: 			 * lock request failed.  Normally close the descriptor
 1047: 			 * but handle the case where someone might have dup()d
 1048: 			 * it when we weren't looking.  One reference is
 1049: 			 * owned by the descriptor array, the other by us.
 1050: 			 */
 1051: 			if (fdp->fd_ofiles[indx] == fp) {
 1052: 				fdp->fd_ofiles[indx] = NULL;
 1053: 				fdrop(fp, td);
 1054: 			}
 1055: 			fdrop(fp, td);
 1056: 			return (error);
 1057: 		}
 1058: 		vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
 1059: 		fp->f_flag |= FHASLOCK;
 1060: 	}
 1061: 	/* assert that vn_open created a backing object if one is needed */
 1062: 	KASSERT(!vn_canvmio(vp) || VOP_GETVOBJECT(vp, NULL) == 0,
 1063: 		("open: vmio vnode has no backing object after vn_open"));
 1064: 	VOP_UNLOCK(vp, NULL, 0, td);
 1065: 
 1066: 	/*
 1067: 	 * release our private reference, leaving the one associated with the
 1068: 	 * descriptor table intact.
 1069: 	 */
 1070: 	fdrop(fp, td);
 1071: 	*res = indx;
 1072: 	return (0);
 1073: }
 1074: 
 1075: /*
 1076:  * open_args(char *path, int flags, int mode)
 1077:  *
 1078:  * Check permissions, allocate an open file structure,
 1079:  * and call the device open routine if any.
 1080:  */
 1081: int
 1082: open(struct open_args *uap)
 1083: {
 1084: 	struct thread *td = curthread;
 1085: 	struct nameidata nd;
 1086: 	int error;
 1087: 
 1088: 	NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE, uap->path, td);
 1089: 
 1090: 	error = kern_open(&nd, uap->flags, uap->mode, &uap->sysmsg_result);
 1091: 
 1092: 	return (error);
 1093: }
 1094: 
 1095: int
 1096: kern_mknod(struct nameidata *nd, int mode, int dev)
 1097: {
 1098: 	struct thread *td = curthread;
 1099: 	struct proc *p = td->td_proc;
 1100: 	struct vnode *vp;
 1101: 	struct vattr vattr;
 1102: 	int error;
 1103: 	int whiteout = 0;
 1104: 
 1105: 	KKASSERT(p);
 1106: 
 1107: 	switch (mode & S_IFMT) {
 1108: 	case S_IFCHR:
 1109: 	case S_IFBLK:
 1110: 		error = suser(td);
 1111: 		break;
 1112: 	default:
 1113: 		error = suser_cred(p->p_ucred, PRISON_ROOT);
 1114: 		break;
 1115: 	}
 1116: 	if (error)
 1117: 		return (error);
 1118: 	bwillwrite();
 1119: 	error = namei(nd);
 1120: 	if (error)
 1121: 		return (error);
 1122: 	vp = nd->ni_vp;
 1123: 	if (vp != NULL)
 1124: 		error = EEXIST;
 1125: 	else {
 1126: 		VATTR_NULL(&vattr);
 1127: 		vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
 1128: 		vattr.va_rdev = dev;
 1129: 		whiteout = 0;
 1130: 
 1131: 		switch (mode & S_IFMT) {
 1132: 		case S_IFMT:	/* used by badsect to flag bad sectors */
 1133: 			vattr.va_type = VBAD;
 1134: 			break;
 1135: 		case S_IFCHR:
 1136: 			vattr.va_type = VCHR;
 1137: 			break;
 1138: 		case S_IFBLK:
 1139: 			vattr.va_type = VBLK;
 1140: 			break;
 1141: 		case S_IFWHT:
 1142: 			whiteout = 1;
 1143: 			break;
 1144: 		default:
 1145: 			error = EINVAL;
 1146: 			break;
 1147: 		}
 1148: 	}
 1149: 	if (error == 0) {
 1150: 		VOP_LEASE(nd->ni_dvp, td, p->p_ucred, LEASE_WRITE);
 1151: 		if (whiteout)
 1152: 			error = VOP_WHITEOUT(nd->ni_dvp, NCPNULL,
 1153: 			    &nd->ni_cnd, NAMEI_CREATE);
 1154: 		else {
 1155: 			error = VOP_MKNOD(nd->ni_dvp, NCPNULL, &nd->ni_vp,
 1156: 			    &nd->ni_cnd, &vattr);
 1157: 			if (error == 0)
 1158: 				vput(nd->ni_vp);
 1159: 		}
 1160: 		NDFREE(nd, NDF_ONLY_PNBUF);
 1161: 		vput(nd->ni_dvp);
 1162: 	} else {
 1163: 		NDFREE(nd, NDF_ONLY_PNBUF);
 1164: 		if (nd->ni_dvp == vp)
 1165: 			vrele(nd->ni_dvp);
 1166: 		else
 1167: 			vput(nd->ni_dvp);
 1168: 		if (vp)
 1169: 			vrele(vp);
 1170: 	}
 1171: 	ASSERT_VOP_UNLOCKED(nd->ni_dvp, "mknod");
 1172: 	ASSERT_VOP_UNLOCKED(nd->ni_vp, "mknod");
 1173: 	return (error);
 1174: }
 1175: 
 1176: /*
 1177:  * mknod_args(char *path, int mode, int dev)
 1178:  *
 1179:  * Create a special file.
 1180:  */
 1181: int
 1182: mknod(struct mknod_args *uap)
 1183: {
 1184: 	struct thread *td = curthread;
 1185: 	struct nameidata nd;
 1186: 	int error;
 1187: 
 1188: 	NDINIT(&nd, NAMEI_CREATE, CNP_LOCKPARENT, UIO_USERSPACE, uap->path,
 1189: 	    td);
 1190: 
 1191: 	error = kern_mknod(&nd, uap->mode, uap->dev);
 1192: 
 1193: 	return (error);
 1194: }
 1195: 
 1196: int
 1197: kern_mkfifo(struct nameidata *nd, int mode)
 1198: {
 1199: 	struct thread *td = curthread;
 1200: 	struct proc *p = td->td_proc;
 1201: 	struct vattr vattr;
 1202: 	int error;
 1203: 
 1204: 	bwillwrite();
 1205: 	error = namei(nd);
 1206: 	if (error)
 1207: 		return (error);
 1208: 	if (nd->ni_vp != NULL) {
 1209: 		NDFREE(nd, NDF_ONLY_PNBUF);
 1210: 		if (nd->ni_dvp == nd->ni_vp)
 1211: 			vrele(nd->ni_dvp);
 1212: 		else
 1213: 			vput(nd->ni_dvp);
 1214: 		vrele(nd->ni_vp);
 1215: 		return (EEXIST);
 1216: 	}
 1217: 	VATTR_NULL(&vattr);
 1218: 	vattr.va_type = VFIFO;
 1219: 	vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
 1220: 	VOP_LEASE(nd->ni_dvp, td, p->p_ucred, LEASE_WRITE);
 1221: 	error = VOP_MKNOD(nd->ni_dvp, NCPNULL, &nd->ni_vp, &nd->ni_cnd, &vattr);
 1222: 	if (error == 0)
 1223: 		vput(nd->ni_vp);
 1224: 	NDFREE(nd, NDF_ONLY_PNBUF);
 1225: 	vput(nd->ni_dvp);
 1226: 	return (error);
 1227: }
 1228: 
 1229: /*
 1230:  * mkfifo_args(char *path, int mode)
 1231:  *
 1232:  * Create a named pipe.
 1233:  */
 1234: int
 1235: mkfifo(struct mkfifo_args *uap)
 1236: {
 1237: 	struct thread *td = curthread;
 1238: 	struct nameidata nd;
 1239: 	int error;
 1240: 
 1241: 	NDINIT(&nd, NAMEI_CREATE, CNP_LOCKPARENT, UIO_USERSPACE, uap->path,
 1242: 	    td);
 1243: 
 1244: 	error = kern_mkfifo(&nd, uap->mode);
 1245: 
 1246: 	return (error);
 1247: }
 1248: 
 1249: int
 1250: kern_link(struct nameidata *nd, struct nameidata *linknd)
 1251: {
 1252: 	struct thread *td = curthread;
 1253: 	struct proc *p = td->td_proc;
 1254: 	struct vnode *vp;
 1255: 	int error;
 1256: 
 1257: 	bwillwrite();
 1258: 	error = namei(nd);
 1259: 	if (error)
 1260: 		return (error);
 1261: 	NDFREE(nd, NDF_ONLY_PNBUF);
 1262: 	vp = nd->ni_vp;
 1263: 	if (vp->v_type == VDIR)
 1264: 		error = EPERM;		/* POSIX */
 1265: 	else {
 1266: 		error = namei(linknd);
 1267: 		if (error == 0) {
 1268: 			if (linknd->ni_vp != NULL) {
 1269: 				if (linknd->ni_vp)
 1270: 					vrele(linknd->ni_vp);
 1271: 				error = EEXIST;
 1272: 			} else {
 1273: 				VOP_LEASE(linknd->ni_dvp, td, p->p_ucred,
 1274: 				    LEASE_WRITE);
 1275: 				VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
 1276: 				error = VOP_LINK(linknd->ni_dvp, NCPNULL, vp,
 1277: 				    &linknd->ni_cnd);
 1278: 			}
 1279: 			NDFREE(linknd, NDF_ONLY_PNBUF);
 1280: 			if (linknd->ni_dvp == linknd->ni_vp)
 1281: 				vrele(linknd->ni_dvp);
 1282: 			else
 1283: 				vput(linknd->ni_dvp);
 1284: 			ASSERT_VOP_UNLOCKED(linknd->ni_dvp, "link");
 1285: 			ASSERT_VOP_UNLOCKED(linknd->ni_vp, "link");
 1286: 		}
 1287: 	}
 1288: 	vrele(vp);
 1289: 	return (error);
 1290: }
 1291: 
 1292: /*
 1293:  * link_args(char *path, char *link)
 1294:  *
 1295:  * Make a hard file link.
 1296:  */
 1297: int
 1298: link(struct link_args *uap)
 1299: {
 1300: 	struct thread *td = curthread;
 1301: 	struct nameidata nd, linknd;
 1302: 	int error;
 1303: 
 1304: 	NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_NOOBJ, UIO_USERSPACE,
 1305: 	    uap->path, td);
 1306: 	NDINIT(&linknd, NAMEI_CREATE, CNP_LOCKPARENT | CNP_NOOBJ,
 1307: 	    UIO_USERSPACE, uap->link, td);
 1308: 
 1309: 	error = kern_link(&nd, &linknd);
 1310: 
 1311: 	return (error);
 1312: }
 1313: 
 1314: int
 1315: kern_symlink(char *path, struct nameidata *nd)
 1316: {
 1317: 	struct thread *td = curthread;
 1318: 	struct proc *p = td->td_proc;
 1319: 	struct vattr vattr;
 1320: 	int error;
 1321: 
 1322: 	bwillwrite();
 1323: 	error = namei(nd);
 1324: 	if (error)
 1325: 		return (error);
 1326: 	if (nd->ni_vp) {
 1327: 		NDFREE(nd, NDF_ONLY_PNBUF);
 1328: 		if (nd->ni_dvp == nd->ni_vp)
 1329: 			vrele(nd->ni_dvp);
 1330: 		else
 1331: 			vput(nd->ni_dvp);
 1332: 		vrele(nd->ni_vp);
 1333: 		return (EEXIST);
 1334: 	}
 1335: 	VATTR_NULL(&vattr);
 1336: 	vattr.va_mode = ACCESSPERMS &~ p->p_fd->fd_cmask;
 1337: 	VOP_LEASE(nd->ni_dvp, td, p->p_ucred, LEASE_WRITE);
 1338: 	error = VOP_SYMLINK(nd->ni_dvp, NCPNULL, &nd->ni_vp, &nd->ni_cnd,
 1339: 	    &vattr, path);
 1340: 	NDFREE(nd, NDF_ONLY_PNBUF);
 1341: 	if (error == 0)
 1342: 		vput(nd->ni_vp);
 1343: 	vput(nd->ni_dvp);
 1344: 	ASSERT_VOP_UNLOCKED(nd->ni_dvp, "symlink");
 1345: 	ASSERT_VOP_UNLOCKED(nd->ni_vp, "symlink");
 1346: 
 1347: 	return (error);
 1348: }
 1349: 
 1350: /*
 1351:  * symlink(char *path, char *link)
 1352:  *
 1353:  * Make a symbolic link.
 1354:  */
 1355: int
 1356: symlink(struct symlink_args *uap)
 1357: {
 1358: 	struct thread *td = curthread;
 1359: 	struct nameidata nd;
 1360: 	char *path;
 1361: 	int error;
 1362: 
 1363: 	path = zalloc(namei_zone);
 1364: 	error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
 1365: 	if (error == 0) {
 1366: 		NDINIT(&nd, NAMEI_CREATE, CNP_LOCKPARENT | CNP_NOOBJ, 
 1367: 			UIO_USERSPACE, uap->link, td);
 1368: 		error = kern_symlink(path, &nd);
 1369: 	}
 1370: 	zfree(namei_zone, path);
 1371: 	return (error);
 1372: }
 1373: 
 1374: /*
 1375:  * undelete_args(char *path)
 1376:  *
 1377:  * Delete a whiteout from the filesystem.
 1378:  */
 1379: /* ARGSUSED */
 1380: int
 1381: undelete(struct undelete_args *uap)
 1382: {
 1383: 	struct thread *td = curthread;
 1384: 	struct proc *p = td->td_proc;
 1385: 	int error;
 1386: 	struct nameidata nd;
 1387: 
 1388: 	bwillwrite();
 1389: 	NDINIT(&nd, NAMEI_DELETE, CNP_LOCKPARENT | CNP_DOWHITEOUT, UIO_USERSPACE,
 1390: 	    SCARG(uap, path), td);
 1391: 	error = namei(&nd);
 1392: 	if (error)
 1393: 		return (error);
 1394: 
 1395: 	if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & CNP_ISWHITEOUT)) {
 1396: 		NDFREE(&nd, NDF_ONLY_PNBUF);
 1397: 		if (nd.ni_dvp == nd.ni_vp)
 1398: 			vrele(nd.ni_dvp);
 1399: 		else
 1400: 			vput(nd.ni_dvp);
 1401: 		if (nd.ni_vp)
 1402: 			vrele(nd.ni_vp);
 1403: 		return (EEXIST);
 1404: 	}
 1405: 
 1406: 	VOP_LEASE(nd.ni_dvp, td, p->p_ucred, LEASE_WRITE);
 1407: 	error = VOP_WHITEOUT(nd.ni_dvp, NCPNULL, &nd.ni_cnd, NAMEI_DELETE);
 1408: 	NDFREE(&nd, NDF_ONLY_PNBUF);
 1409: 	vput(nd.ni_dvp);
 1410: 	ASSERT_VOP_UNLOCKED(nd.ni_dvp, "undelete");
 1411: 	ASSERT_VOP_UNLOCKED(nd.ni_vp, "undelete");
 1412: 	return (error);
 1413: }
 1414: 
 1415: int
 1416: kern_unlink(struct nameidata *nd)
 1417: {
 1418: 	struct thread *td = curthread;
 1419: 	struct proc *p = td->td_proc;
 1420: 	struct vnode *vp;
 1421: 	int error;
 1422: 
 1423: 	bwillwrite();
 1424: 	error = namei(nd);
 1425: 	if (error)
 1426: 		return (error);
 1427: 	vp = nd->ni_vp;
 1428: 	VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
 1429: 	vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
 1430: 
 1431: 	if (vp->v_type == VDIR)
 1432: 		error = EPERM;		/* POSIX */
 1433: 	else {
 1434: 		/*
 1435: 		 * The root of a mounted filesystem cannot be deleted.
 1436: 		 *
 1437: 		 * XXX: can this only be a VDIR case?
 1438: 		 */
 1439: 		if (vp->v_flag & VROOT)
 1440: 			error = EBUSY;
 1441: 	}
 1442: 
 1443: 	if (error == 0) {
 1444: 		VOP_LEASE(nd->ni_dvp, td, p->p_ucred, LEASE_WRITE);
 1445: 		error = VOP_REMOVE(nd->ni_dvp, NCPNULL, vp, &nd->ni_cnd);
 1446: 	}
 1447: 	NDFREE(nd, NDF_ONLY_PNBUF);
 1448: 	if (nd->ni_dvp == vp)
 1449: 		vrele(nd->ni_dvp);
 1450: 	else
 1451: 		vput(nd->ni_dvp);
 1452: 	if (vp != NULLVP)
 1453: 		vput(vp);
 1454: 	ASSERT_VOP_UNLOCKED(nd->ni_dvp, "unlink");
 1455: 	ASSERT_VOP_UNLOCKED(nd->ni_vp, "unlink");
 1456: 	return (error);
 1457: }
 1458: 
 1459: /*
 1460:  * unlink_args(char *path)
 1461:  *
 1462:  * Delete a name from the filesystem.
 1463:  */
 1464: int
 1465: unlink(struct unlink_args *uap)
 1466: {
 1467: 	struct thread *td = curthread;
 1468: 	struct nameidata nd;
 1469: 	int error;
 1470: 
 1471: 	NDINIT(&nd, NAMEI_DELETE, CNP_LOCKPARENT, UIO_USERSPACE, uap->path,
 1472: 	    td);
 1473: 
 1474: 	error = kern_unlink(&nd);
 1475: 
 1476: 	return (error);
 1477: }
 1478: 
 1479: int
 1480: kern_lseek(int fd, off_t offset, int whence, off_t *res)
 1481: {
 1482: 	struct thread *td = curthread;
 1483: 	struct proc *p = td->td_proc;
 1484: 	struct filedesc *fdp = p->p_fd;
 1485: 	struct file *fp;
 1486: 	struct vattr vattr;
 1487: 	int error;
 1488: 
 1489: 	if (fd >= fdp->fd_nfiles ||
 1490: 	    (fp = fdp->fd_ofiles[fd]) == NULL)
 1491: 		return (EBADF);
 1492: 	if (fp->f_type != DTYPE_VNODE)
 1493: 		return (ESPIPE);
 1494: 	switch (whence) {
 1495: 	case L_INCR:
 1496: 		fp->f_offset += offset;
 1497: 		break;
 1498: 	case L_XTND:
 1499: 		error=VOP_GETATTR((struct vnode *)fp->f_data, &vattr, td);
 1500: 		if (error)
 1501: 			return (error);
 1502: 		fp->f_offset = offset + vattr.va_size;
 1503: 		break;
 1504: 	case L_SET:
 1505: 		fp->f_offset = offset;
 1506: 		break;
 1507: 	default:
 1508: 		return (EINVAL);
 1509: 	}
 1510: 	*res = fp->f_offset;
 1511: 	return (0);
 1512: }
 1513: 
 1514: /*
 1515:  * lseek_args(int fd, int pad, off_t offset, int whence)
 1516:  *
 1517:  * Reposition read/write file offset.
 1518:  */
 1519: int
 1520: lseek(struct lseek_args *uap)
 1521: {
 1522: 	int error;
 1523: 
 1524: 	error = kern_lseek(uap->fd, uap->offset, uap->whence,
 1525: 	    &uap->sysmsg_offset);
 1526: 
 1527: 	return (error);
 1528: }
 1529: 
 1530: int
 1531: kern_access(struct nameidata *nd, int aflags)
 1532: {
 1533: 	struct thread *td = curthread;
 1534: 	struct proc *p = td->td_proc;
 1535: 	struct ucred *cred, *tmpcred;
 1536: 	struct vnode *vp;
 1537: 	int error, flags;
 1538: 
 1539: 	cred = p->p_ucred;
 1540: 	/*
 1541: 	 * Create and modify a temporary credential instead of one that
 1542: 	 * is potentially shared.  This could also mess up socket
 1543: 	 * buffer accounting which can run in an interrupt context.
 1544: 	 */
 1545: 	tmpcred = crdup(cred);
 1546: 	tmpcred->cr_uid = p->p_ucred->cr_ruid;
 1547: 	tmpcred->cr_groups[0] = p->p_ucred->cr_rgid;
 1548: 	p->p_ucred = tmpcred;
 1549: 	nd->ni_cnd.cn_cred = tmpcred;
 1550: 	error = namei(nd);
 1551: 	if (error)
 1552: 		goto out1;
 1553: 	vp = nd->ni_vp;
 1554: 
 1555: 	/* Flags == 0 means only check for existence. */
 1556: 	if (aflags) {
 1557: 		flags = 0;
 1558: 		if (aflags & R_OK)
 1559: 			flags |= VREAD;
 1560: 		if (aflags & W_OK)
 1561: 			flags |= VWRITE;
 1562: 		if (aflags & X_OK)
 1563: 			flags |= VEXEC;
 1564: 		if ((flags & VWRITE) == 0 || (error = vn_writechk(vp)) == 0)
 1565: 			error = VOP_ACCESS(vp, flags, tmpcred, td);
 1566: 	}
 1567: 	NDFREE(nd, NDF_ONLY_PNBUF);
 1568: 	vput(vp);
 1569: out1:
 1570: 	p->p_ucred = cred;
 1571: 	crfree(tmpcred);
 1572: 	return (error);
 1573: }
 1574: 
 1575: /*
 1576:  * access_args(char *path, int flags)
 1577:  *
 1578:  * Check access permissions.
 1579:  */
 1580: int
 1581: access(struct access_args *uap)
 1582: {
 1583: 	struct thread *td = curthread;
 1584: 	struct nameidata nd;
 1585: 	int error;
 1586: 
 1587: 	NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF | CNP_NOOBJ,
 1588: 	    UIO_USERSPACE, uap->path, td);
 1589: 
 1590: 	error = kern_access(&nd, uap->flags);
 1591: 
 1592: 	return (error);
 1593: }
 1594: 
 1595: int
 1596: kern_stat(struct nameidata *nd, struct stat *st)
 1597: {
 1598: 	struct thread *td = curthread;
 1599: 	int error;
 1600: 
 1601: 	error = namei(nd);
 1602: 	if (error)
 1603: 		return (error);
 1604: 	error = vn_stat(nd->ni_vp, st, td);
 1605: 	NDFREE(nd, NDF_ONLY_PNBUF);
 1606: 	vput(nd->ni_vp);
 1607: 	return (error);
 1608: }
 1609: 
 1610: /*
 1611:  * stat_args(char *path, struct stat *ub)
 1612:  *
 1613:  * Get file status; this version follows links.
 1614:  */
 1615: int
 1616: stat(struct stat_args *uap)
 1617: {
 1618: 	struct thread *td = curthread;
 1619: 	struct nameidata nd;
 1620: 	struct stat st;
 1621: 	int error;
 1622: 
 1623: 	NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF | CNP_NOOBJ,
 1624: 	    UIO_USERSPACE, uap->path, td);
 1625: 
 1626: 	error = kern_stat(&nd, &st);
 1627: 
 1628: 	if (error == 0)
 1629: 		error = copyout(&st, uap->ub, sizeof(*uap->ub));
 1630: 	return (error);
 1631: }
 1632: 
 1633: /*
 1634:  * lstat_args(char *path, struct stat *ub)
 1635:  *
 1636:  * Get file status; this version does not follow links.
 1637:  */
 1638: int
 1639: lstat(struct lstat_args *uap)
 1640: {
 1641: 	struct thread *td = curthread;
 1642: 	struct nameidata nd;
 1643: 	struct stat st;
 1644: 	int error;
 1645: 
 1646: 	NDINIT(&nd, NAMEI_LOOKUP, CNP_LOCKLEAF | CNP_NOOBJ,
 1647: 	    UIO_USERSPACE, SCARG(uap, path), td);
 1648: 
 1649: 	error = kern_stat(&nd, &st);
 1650: 
 1651: 	if (error == 0)
 1652: 		error = copyout(&st, uap->ub, sizeof(*uap->ub));
 1653: 	return (error);
 1654: }
 1655: 
 1656: void
 1657: cvtnstat(sb, nsb)
 1658: 	struct stat *sb;
 1659: 	struct nstat *nsb;
 1660: {
 1661: 	nsb->st_dev = sb->st_dev;
 1662: 	nsb->st_ino = sb->st_ino;
 1663: 	nsb->st_mode = sb->st_mode;
 1664: 	nsb->st_nlink = sb->st_nlink;
 1665: 	nsb->st_uid = sb->st_uid;
 1666: 	nsb->st_gid = sb->st_gid;
 1667: 	nsb->st_rdev = sb->st_rdev;
 1668: 	nsb->st_atimespec = sb->st_atimespec;
 1669: 	nsb->st_mtimespec = sb->st_mtimespec;
 1670: 	nsb->st_ctimespec = sb->st_ctimespec;
 1671: 	nsb->st_size = sb->st_size;
 1672: 	nsb->st_blocks = sb->st_blocks;
 1673: 	nsb->st_blksize = sb->st_blksize;
 1674: 	nsb->st_flags = sb->st_flags;
 1675: 	nsb->st_gen = sb->st_gen;
 1676: 	nsb->st_qspare[0] = sb->st_qspare[0];
 1677: 	nsb->st_qspare[1] = sb->st_qspare[1];
 1678: }
 1679: 
 1680: /*
 1681:  * nstat_args(char *path, struct nstat *ub)
 1682:  */
 1683: /* ARGSUSED */
 1684: int
 1685: nstat(struct nstat_args *uap)
 1686: {
 1687: 	struct thread *td = curthread;
 1688: 	struct stat sb;
 1689: 	struct nstat nsb;
 1690: 	int error;
 1691: 	struct nameidata nd;
 1692: 
 1693: 	NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF | CNP_NOOBJ,
 1694: 	    UIO_USERSPACE, SCARG(uap, path), td);
 1695: 	if ((error = namei(&nd)) != 0)
 1696: 		return (error);
 1697: 	NDFREE(&nd, NDF_ONLY_PNBUF);
 1698: 	error = vn_stat(nd.ni_vp, &sb, td);
 1699: 	vput(nd.ni_vp);
 1700: 	if (error)
 1701: 		return (error);
 1702: 	cvtnstat(&sb, &nsb);
 1703: 	error = copyout((caddr_t)&nsb, (caddr_t)SCARG(uap, ub), sizeof (nsb));
 1704: 	return (error);
 1705: }
 1706: 
 1707: /*
 1708:  * lstat_args(char *path, struct stat *ub)
 1709:  *
 1710:  * Get file status; this version does not follow links.
 1711:  */
 1712: /* ARGSUSED */
 1713: int
 1714: nlstat(struct nlstat_args *uap)
 1715: {
 1716: 	struct thread *td = curthread;
 1717: 	int error;
 1718: 	struct vnode *vp;
 1719: 	struct stat sb;
 1720: 	struct nstat nsb;
 1721: 	struct nameidata nd;
 1722: 
 1723: 	NDINIT(&nd, NAMEI_LOOKUP, CNP_LOCKLEAF | CNP_NOOBJ,
 1724: 	    UIO_USERSPACE, SCARG(uap, path), td);
 1725: 	if ((error = namei(&nd)) != 0)
 1726: 		return (error);
 1727: 	vp = nd.ni_vp;
 1728: 	NDFREE(&nd, NDF_ONLY_PNBUF);
 1729: 	error = vn_stat(vp, &sb, td);
 1730: 	vput(vp);
 1731: 	if (error)
 1732: 		return (error);
 1733: 	cvtnstat(&sb, &nsb);
 1734: 	error = copyout((caddr_t)&nsb, (caddr_t)SCARG(uap, ub), sizeof (nsb));
 1735: 	return (error);
 1736: }
 1737: 
 1738: /*
 1739:  * pathconf_Args(char *path, int name)
 1740:  *
 1741:  * Get configurable pathname variables.
 1742:  */
 1743: /* ARGSUSED */
 1744: int
 1745: pathconf(struct pathconf_args *uap)
 1746: {
 1747: 	struct thread *td = curthread;
 1748: 	int error;
 1749: 	struct nameidata nd;
 1750: 
 1751: 	NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF | CNP_NOOBJ,
 1752: 	    UIO_USERSPACE, SCARG(uap, path), td);
 1753: 	if ((error = namei(&nd)) != 0)
 1754: 		return (error);
 1755: 	NDFREE(&nd, NDF_ONLY_PNBUF);
 1756: 	error = VOP_PATHCONF(nd.ni_vp, SCARG(uap, name), uap->sysmsg_fds);
 1757: 	vput(nd.ni_vp);
 1758: 	return (error);
 1759: }
 1760: 
 1761: /*
 1762:  * XXX: daver
 1763:  * kern_readlink isn't properly split yet.  There is a copyin burried
 1764:  * in VOP_READLINK().
 1765:  */
 1766: int
 1767: kern_readlink(struct nameidata *nd, char *buf, int count, int *res)
 1768: {
 1769: 	struct thread *td = curthread;
 1770: 	struct proc *p = td->td_proc;
 1771: 	struct vnode *vp;
 1772: 	struct iovec aiov;
 1773: 	struct uio auio;
 1774: 	int error;
 1775: 
 1776: 	error = namei(nd);
 1777: 	if (error)
 1778: 		return (error);
 1779: 	NDFREE(nd, NDF_ONLY_PNBUF);
 1780: 	vp = nd->ni_vp;
 1781: 	if (vp->v_type != VLNK)
 1782: 		error = EINVAL;
 1783: 	else {
 1784: 		aiov.iov_base = buf;
 1785: 		aiov.iov_len = count;
 1786: 		auio.uio_iov = &aiov;
 1787: 		auio.uio_iovcnt = 1;
 1788: 		auio.uio_offset = 0;
 1789: 		auio.uio_rw = UIO_READ;
 1790: 		auio.uio_segflg = UIO_USERSPACE;
 1791: 		auio.uio_td = td;
 1792: 		auio.uio_resid = count;
 1793: 		error = VOP_READLINK(vp, &auio, p->p_ucred);
 1794: 	}
 1795: 	vput(vp);
 1796: 	*res = count - auio.uio_resid;
 1797: 	return (error);
 1798: }
 1799: 
 1800: /*
 1801:  * readlink_args(char *path, char *buf, int count)
 1802:  *
 1803:  * Return target name of a symbolic link.
 1804:  */
 1805: int
 1806: readlink(struct readlink_args *uap)
 1807: {
 1808: 	struct thread *td = curthread;
 1809: 	struct nameidata nd;
 1810: 	int error;
 1811: 
 1812: 	NDINIT(&nd, NAMEI_LOOKUP, CNP_LOCKLEAF | CNP_NOOBJ, UIO_USERSPACE,
 1813: 	    uap->path, td);
 1814: 
 1815: 	error = kern_readlink(&nd, uap->buf, uap->count,
 1816: 	    &uap->sysmsg_result);
 1817: 
 1818: 	return (error);
 1819: }
 1820: 
 1821: static int
 1822: setfflags(struct vnode *vp, int flags)
 1823: {
 1824: 	struct thread *td = curthread;
 1825: 	struct proc *p = td->td_proc;
 1826: 	int error;
 1827: 	struct vattr vattr;
 1828: 
 1829: 	/*
 1830: 	 * Prevent non-root users from setting flags on devices.  When
 1831: 	 * a device is reused, users can retain ownership of the device
 1832: 	 * if they are allowed to set flags and programs assume that
 1833: 	 * chown can't fail when done as root.
 1834: 	 */
 1835: 	if ((vp->v_type == VCHR || vp->v_type == VBLK) && 
 1836: 	    ((error = suser_cred(p->p_ucred, PRISON_ROOT)) != 0))
 1837: 		return (error);
 1838: 
 1839: 	VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
 1840: 	vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
 1841: 	VATTR_NULL(&vattr);
 1842: 	vattr.va_flags = flags;
 1843: 	error = VOP_SETATTR(vp, &vattr, p->p_ucred, td);
 1844: 	VOP_UNLOCK(vp, NULL, 0, td);
 1845: 	return (error);
 1846: }
 1847: 
 1848: /*
 1849:  * chflags(char *path, int flags)
 1850:  *
 1851:  * Change flags of a file given a path name.
 1852:  */
 1853: /* ARGSUSED */
 1854: int
 1855: chflags(struct chflags_args *uap)
 1856: {
 1857: 	struct thread *td = curthread;
 1858: 	int error;
 1859: 	struct nameidata nd;
 1860: 
 1861: 	NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE,
 1862: 	    SCARG(uap, path), td);
 1863: 	if ((error = namei(&nd)) != 0)
 1864: 		return (error);
 1865: 	NDFREE(&nd, NDF_ONLY_PNBUF);
 1866: 	error = setfflags(nd.ni_vp, SCARG(uap, flags));
 1867: 	vrele(nd.ni_vp);
 1868: 	return error;
 1869: }
 1870: 
 1871: /*
 1872:  * fchflags_args(int fd, int flags)
 1873:  *
 1874:  * Change flags of a file given a file descriptor.
 1875:  */
 1876: /* ARGSUSED */
 1877: int
 1878: fchflags(struct fchflags_args *uap)
 1879: {
 1880: 	struct thread *td = curthread;
 1881: 	struct proc *p = td->td_proc;
 1882: 	struct file *fp;
 1883: 	int error;
 1884: 
 1885: 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
 1886: 		return (error);
 1887: 	return setfflags((struct vnode *) fp->f_data, SCARG(uap, flags));
 1888: }
 1889: 
 1890: static int
 1891: setfmode(struct vnode *vp, int mode)
 1892: {
 1893: 	struct thread *td = curthread;
 1894: 	struct proc *p = td->td_proc;
 1895: 	int error;
 1896: 	struct vattr vattr;
 1897: 
 1898: 	VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
 1899: 	vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
 1900: 	VATTR_NULL(&vattr);
 1901: 	vattr.va_mode = mode & ALLPERMS;
 1902: 	error = VOP_SETATTR(vp, &vattr, p->p_ucred, td);
 1903: 	VOP_UNLOCK(vp, NULL, 0, td);
 1904: 	return error;
 1905: }
 1906: 
 1907: int
 1908: kern_chmod(struct nameidata *nd, int mode)
 1909: {
 1910: 	int error;
 1911: 
 1912: 	error = namei(nd);
 1913: 	if (error)
 1914: 		return (error);
 1915: 	NDFREE(nd, NDF_ONLY_PNBUF);
 1916: 	error = setfmode(nd->ni_vp, mode);
 1917: 	vrele(nd->ni_vp);
 1918: 	return error;
 1919: }
 1920: 
 1921: /*
 1922:  * chmod_args(char *path, int mode)
 1923:  *
 1924:  * Change mode of a file given path name.
 1925:  */
 1926: /* ARGSUSED */
 1927: int
 1928: chmod(struct chmod_args *uap)
 1929: {
 1930: 	struct thread *td = curthread;
 1931: 	struct nameidata nd;
 1932: 	int error;
 1933: 
 1934: 	NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE, uap->path, td);
 1935: 
 1936: 	error = kern_chmod(&nd, uap->mode);
 1937: 
 1938: 	return (error);
 1939: }
 1940: 
 1941: /*
 1942:  * lchmod_args(char *path, int mode)
 1943:  *
 1944:  * Change mode of a file given path name (don't follow links.)
 1945:  */
 1946: /* ARGSUSED */
 1947: int
 1948: lchmod(struct lchmod_args *uap)
 1949: {
 1950: 	struct thread *td = curthread;
 1951: 	int error;
 1952: 	struct nameidata nd;
 1953: 
 1954: 	NDINIT(&nd, NAMEI_LOOKUP, 0, UIO_USERSPACE, SCARG(uap, path), td);
 1955: 	if ((error = namei(&nd)) != 0)
 1956: 		return (error);
 1957: 	NDFREE(&nd, NDF_ONLY_PNBUF);
 1958: 	error = setfmode(nd.ni_vp, SCARG(uap, mode));
 1959: 	vrele(nd.ni_vp);
 1960: 	return error;
 1961: }
 1962: 
 1963: /*
 1964:  * fchmod_args(int fd, int mode)
 1965:  *
 1966:  * Change mode of a file given a file descriptor.
 1967:  */
 1968: /* ARGSUSED */
 1969: int
 1970: fchmod(struct fchmod_args *uap)
 1971: {
 1972: 	struct thread *td = curthread;
 1973: 	struct proc *p = td->td_proc;
 1974: 	struct file *fp;
 1975: 	int error;
 1976: 
 1977: 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
 1978: 		return (error);
 1979: 	return setfmode((struct vnode *)fp->f_data, SCARG(uap, mode));
 1980: }
 1981: 
 1982: static int
 1983: setfown(struct vnode *vp, uid_t uid, gid_t gid)
 1984: {
 1985: 	struct thread *td = curthread;
 1986: 	struct proc *p = td->td_proc;
 1987: 	int error;
 1988: 	struct vattr vattr;
 1989: 
 1990: 	VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
 1991: 	vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
 1992: 	VATTR_NULL(&vattr);
 1993: 	vattr.va_uid = uid;
 1994: 	vattr.va_gid = gid;
 1995: 	error = VOP_SETATTR(vp, &vattr, p->p_ucred, td);
 1996: 	VOP_UNLOCK(vp, NULL, 0, td);
 1997: 	return error;
 1998: }
 1999: 
 2000: int
 2001: kern_chown(struct nameidata *nd, int uid, int gid)
 2002: {
 2003: 	int error;
 2004: 
 2005: 	error = namei(nd);
 2006: 	if (error)
 2007: 		return (error);
 2008: 	NDFREE(nd, NDF_ONLY_PNBUF);
 2009: 	error = setfown(nd->ni_vp, uid, gid);
 2010: 	vrele(nd->ni_vp);
 2011: 	return (error);
 2012: }
 2013: 
 2014: /*
 2015:  * chown(char *path, int uid, int gid)
 2016:  *
 2017:  * Set ownership given a path name.
 2018:  */
 2019: int
 2020: chown(struct chown_args *uap)
 2021: {
 2022: 	struct thread *td = curthread;
 2023: 	struct nameidata nd;
 2024: 	int error;
 2025: 
 2026: 	NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE, uap->path, td);
 2027: 
 2028: 	error = kern_chown(&nd, uap->uid, uap->gid);
 2029: 
 2030: 	return (error);
 2031: }
 2032: 
 2033: /*
 2034:  * lchown_args(char *path, int uid, int gid)
 2035:  *
 2036:  * Set ownership given a path name, do not cross symlinks.
 2037:  */
 2038: int
 2039: lchown(struct lchown_args *uap)
 2040: {
 2041: 	struct thread *td = curthread;
 2042: 	int error;
 2043: 	struct nameidata nd;
 2044: 
 2045: 	NDINIT(&nd, NAMEI_LOOKUP, 0, UIO_USERSPACE, uap->path, td);
 2046: 
 2047: 	error = kern_chown(&nd, uap->uid, uap->gid);
 2048: 
 2049: 	return (error);
 2050: }
 2051: 
 2052: /*
 2053:  * fchown_args(int fd, int uid, int gid)
 2054:  *
 2055:  * Set ownership given a file descriptor.
 2056:  */
 2057: /* ARGSUSED */
 2058: int
 2059: fchown(struct fchown_args *uap)
 2060: {
 2061: 	struct thread *td = curthread;
 2062: 	struct proc *p = td->td_proc;
 2063: 	struct file *fp;
 2064: 	int error;
 2065: 
 2066: 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
 2067: 		return (error);
 2068: 	return setfown((struct vnode *)fp->f_data,
 2069: 		SCARG(uap, uid), SCARG(uap, gid));
 2070: }
 2071: 
 2072: static int
 2073: getutimes(const struct timeval *tvp, struct timespec *tsp)
 2074: {
 2075: 	struct timeval tv[2];
 2076: 
 2077: 	if (tvp == NULL) {
 2078: 		microtime(&tv[0]);
 2079: 		TIMEVAL_TO_TIMESPEC(&tv[0], &tsp[0]);
 2080: 		tsp[1] = tsp[0];
 2081: 	} else {
 2082: 		TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
 2083: 		TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
 2084: 	}
 2085: 	return 0;
 2086: }
 2087: 
 2088: static int
 2089: setutimes(struct vnode *vp, const struct timespec *ts, int nullflag)
 2090: {
 2091: 	struct thread *td = curthread;
 2092: 	struct proc *p = td->td_proc;
 2093: 	int error;
 2094: 	struct vattr vattr;
 2095: 
 2096: 	VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
 2097: 	vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
 2098: 	VATTR_NULL(&vattr);
 2099: 	vattr.va_atime = ts[0];
 2100: 	vattr.va_mtime = ts[1];
 2101: 	if (nullflag)
 2102: 		vattr.va_vaflags |= VA_UTIMES_NULL;
 2103: 	error = VOP_SETATTR(vp, &vattr, p->p_ucred, td);
 2104: 	VOP_UNLOCK(vp, NULL, 0, td);
 2105: 	return error;
 2106: }
 2107: 
 2108: int
 2109: kern_utimes(struct nameidata *nd, struct timeval *tptr)
 2110: {
 2111: 	struct timespec ts[2];
 2112: 	int error;
 2113: 
 2114: 	error = getutimes(tptr, ts);
 2115: 	if (error)
 2116: 		return (error);
 2117: 	error = namei(nd);
 2118: 	if (error)
 2119: 		return (error);
 2120: 	NDFREE(nd, NDF_ONLY_PNBUF);
 2121: 	error = setutimes(nd->ni_vp, ts, tptr == NULL);
 2122: 	vrele(nd->ni_vp);
 2123: 	return (error);
 2124: }
 2125: 
 2126: /*
 2127:  * utimes_args(char *path, struct timeval *tptr)
 2128:  *
 2129:  * Set the access and modification times of a file.
 2130:  */
 2131: int
 2132: utimes(struct utimes_args *uap)
 2133: {
 2134: 	struct thread *td = curthread;
 2135: 	struct timeval tv[2];
 2136: 	struct nameidata nd;
 2137: 	int error;
 2138: 
 2139: 	if (uap->tptr) {
 2140:  		error = copyin(uap->tptr, tv, sizeof(tv));
 2141: 		if (error)
 2142: 			return (error);
 2143: 	}
 2144: 	NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE, uap->path, td);
 2145: 
 2146: 	error = kern_utimes(&nd, uap->tptr ? tv : NULL);
 2147: 
 2148: 	return (error);
 2149: }
 2150: 
 2151: /*
 2152:  * lutimes_args(char *path, struct timeval *tptr)
 2153:  *
 2154:  * Set the access and modification times of a file.
 2155:  */
 2156: int
 2157: lutimes(struct lutimes_args *uap)
 2158: {
 2159: 	struct thread *td = curthread;
 2160: 	struct timeval tv[2];
 2161: 	struct nameidata nd;
 2162: 	int error;
 2163: 
 2164: 	if (uap->tptr) {
 2165: 		error = copyin(uap->tptr, tv, sizeof(tv));
 2166: 		if (error)
 2167: 			return (error);
 2168: 	}
 2169: 	NDINIT(&nd, NAMEI_LOOKUP, 0, UIO_USERSPACE, uap->path, td);
 2170: 
 2171: 	error = kern_utimes(&nd, uap->tptr ? tv : NULL);
 2172: 
 2173: 	return (error);
 2174: }
 2175: 
 2176: int
 2177: kern_futimes(int fd, struct timeval *tptr)
 2178: {
 2179: 	struct thread *td = curthread;
 2180: 	struct proc *p = td->td_proc;
 2181: 	struct timespec ts[2];
 2182: 	struct file *fp;
 2183: 	int error;
 2184: 
 2185: 	error = getutimes(tptr, ts);
 2186: 	if (error)
 2187: 		return (error);
 2188: 	error = getvnode(p->p_fd, fd, &fp);
 2189: 	if (error)
 2190: 		return (error);
 2191: 	error =  setutimes((struct vnode *)fp->f_data, ts, tptr == NULL);
 2192: 	return (error);
 2193: }
 2194: 
 2195: /*
 2196:  * futimes_args(int fd, struct timeval *tptr)
 2197:  *
 2198:  * Set the access and modification times of a file.
 2199:  */
 2200: int
 2201: futimes(struct futimes_args *uap)
 2202: {
 2203: 	struct timeval tv[2];
 2204: 	int error;
 2205: 
 2206: 	if (uap->tptr) {
 2207: 		error = copyin(uap->tptr, tv, sizeof(tv));
 2208: 		if (error)
 2209: 			return (error);
 2210: 	}
 2211: 
 2212: 	error = kern_futimes(uap->fd, uap->tptr ? tv : NULL);
 2213: 
 2214: 	return (error);
 2215: }
 2216: 
 2217: int
 2218: kern_truncate(struct nameidata* nd, off_t length)
 2219: {
 2220: 	struct thread *td = curthread;
 2221: 	struct proc *p = td->td_proc;
 2222: 	struct vnode *vp;
 2223: 	struct vattr vattr;
 2224: 	int error;
 2225: 
 2226: 	if (length < 0)
 2227: 		return(EINVAL);
 2228: 	if ((error = namei(nd)) != 0)
 2229: 		return (error);
 2230: 	vp = nd->ni_vp;
 2231: 	NDFREE(nd, NDF_ONLY_PNBUF);
 2232: 	VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
 2233: 	vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
 2234: 	if (vp->v_type == VDIR)
 2235: 		error = EISDIR;
 2236: 	else if ((error = vn_writechk(vp)) == 0 &&
 2237: 	    (error = VOP_ACCESS(vp, VWRITE, p->p_ucred, td)) == 0) {
 2238: 		VATTR_NULL(&vattr);
 2239: 		vattr.va_size = length;
 2240: 		error = VOP_SETATTR(vp, &vattr, p->p_ucred, td);
 2241: 	}
 2242: 	vput(vp);
 2243: 	return (error);
 2244: }
 2245: 
 2246: /*
 2247:  * truncate(char *path, int pad, off_t length)
 2248:  *
 2249:  * Truncate a file given its path name.
 2250:  */
 2251: int
 2252: truncate(struct truncate_args *uap)
 2253: {
 2254: 	struct thread *td = curthread;
 2255: 	struct nameidata nd;
 2256: 	int error;
 2257: 
 2258: 	NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE, uap->path, td);
 2259: 
 2260: 	error = kern_truncate(&nd, uap->length);
 2261: 
 2262: 	return error;
 2263: }
 2264: 
 2265: int
 2266: kern_ftruncate(int fd, off_t length)
 2267: {
 2268: 	struct thread *td = curthread;
 2269: 	struct proc *p = td->td_proc;
 2270: 	struct vattr vattr;
 2271: 	struct vnode *vp;
 2272: 	struct file *fp;
 2273: 	int error;
 2274: 
 2275: 	if (length < 0)
 2276: 		return(EINVAL);
 2277: 	if ((error = getvnode(p->p_fd, fd, &fp)) != 0)
 2278: 		return (error);
 2279: 	if ((fp->f_flag & FWRITE) == 0)
 2280: 		return (EINVAL);
 2281: 	vp = (struct vnode *)fp->f_data;
 2282: 	VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
 2283: 	vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
 2284: 	if (vp->v_type == VDIR)
 2285: 		error = EISDIR;
 2286: 	else if ((error = vn_writechk(vp)) == 0) {
 2287: 		VATTR_NULL(&vattr);
 2288: 		vattr.va_size = length;
 2289: 		error = VOP_SETATTR(vp, &vattr, fp->f_cred, td);
 2290: 	}
 2291: 	VOP_UNLOCK(vp, NULL, 0, td);
 2292: 	return (error);
 2293: }
 2294: 
 2295: /*
 2296:  * ftruncate_args(int fd, int pad, off_t length)
 2297:  *
 2298:  * Truncate a file given a file descriptor.
 2299:  */
 2300: int
 2301: ftruncate(struct ftruncate_args *uap)
 2302: {
 2303: 	int error;
 2304: 
 2305: 	error = kern_ftruncate(uap->fd, uap->length);
 2306: 
 2307: 	return (error);
 2308: }
 2309: 
 2310: /*
 2311:  * fsync(int fd)
 2312:  *
 2313:  * Sync an open file.
 2314:  */
 2315: /* ARGSUSED */
 2316: int
 2317: fsync(struct fsync_args *uap)
 2318: {
 2319: 	struct thread *td = curthread;
 2320: 	struct proc *p = td->td_proc;
 2321: 	struct vnode *vp;
 2322: 	struct file *fp;
 2323: 	vm_object_t obj;
 2324: 	int error;
 2325: 
 2326: 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
 2327: 		return (error);
 2328: 	vp = (struct vnode *)fp->f_data;
 2329: 	vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
 2330: 	if (VOP_GETVOBJECT(vp, &obj) == 0)
 2331: 		vm_object_page_clean(obj, 0, 0, 0);
 2332: 	if ((error = VOP_FSYNC(vp, MNT_WAIT, td)) == 0 &&
 2333: 	    vp->v_mount && (vp->v_mount->mnt_flag & MNT_SOFTDEP) &&
 2334: 	    bioops.io_fsync)
 2335: 		error = (*bioops.io_fsync)(vp);
 2336: 	VOP_UNLOCK(vp, NULL, 0, td);
 2337: 	return (error);
 2338: }
 2339: 
 2340: int
 2341: kern_rename(struct nameidata *fromnd, struct nameidata *tond)
 2342: {
 2343: 	struct thread *td = curthread;
 2344: 	struct proc *p = td->td_proc;
 2345: 	struct vnode *tvp, *fvp, *tdvp;
 2346: 	int error;
 2347: 
 2348: 	bwillwrite();
 2349: 	error = namei(fromnd);
 2350: 	if (error)
 2351: 		return (error);
 2352: 	fvp = fromnd->ni_vp;
 2353: 	if (fromnd->ni_vp->v_type == VDIR)
 2354: 		tond->ni_cnd.cn_flags |= CNP_WILLBEDIR;
 2355: 	error = namei(tond);
 2356: 	if (error) {
 2357: 		/* Translate error code for rename("dir1", "dir2/."). */
 2358: 		if (error == EISDIR && fvp->v_type == VDIR)
 2359: 			error = EINVAL;
 2360: 		NDFREE(fromnd, NDF_ONLY_PNBUF);
 2361: 		vrele(fromnd->ni_dvp);
 2362: 		vrele(fvp);
 2363: 		goto out1;
 2364: 	}
 2365: 	tdvp = tond->ni_dvp;
 2366: 	tvp = tond->ni_vp;
 2367: 	if (tvp != NULL) {
 2368: 		if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
 2369: 			error = ENOTDIR;
 2370: 			goto out;
 2371: 		} else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
 2372: 			error = EISDIR;
 2373: 			goto out;
 2374: 		}
 2375: 	}
 2376: 	if (fvp == tdvp)
 2377: 		error = EINVAL;
 2378: 	/*
 2379: 	 * If the source is the same as the destination (that is, if they
 2380: 	 * are links to the same vnode), then there is nothing to do.
 2381: 	 */
 2382: 	if (fvp == tvp)
 2383: 		error = -1;
 2384: out:
 2385: 	if (!error) {
 2386: 		VOP_LEASE(tdvp, td, p->p_ucred, LEASE_WRITE);
 2387: 		if (fromnd->ni_dvp != tdvp) {
 2388: 			VOP_LEASE(fromnd->ni_dvp, td, p->p_ucred, LEASE_WRITE);
 2389: 		}
 2390: 		if (tvp) {
 2391: 			VOP_LEASE(tvp, td, p->p_ucred, LEASE_WRITE);
 2392: 		}
 2393: 		error = VOP_RENAME(fromnd->ni_dvp, NCPNULL, fromnd->ni_vp,
 2394: 		    &fromnd->ni_cnd, tond->ni_dvp, NCPNULL, tond->ni_vp,
 2395: 		    &tond->ni_cnd);
 2396: 		NDFREE(fromnd, NDF_ONLY_PNBUF);
 2397: 		NDFREE(tond, NDF_ONLY_PNBUF);
 2398: 	} else {
 2399: 		NDFREE(fromnd, NDF_ONLY_PNBUF);
 2400: 		NDFREE(tond, NDF_ONLY_PNBUF);
 2401: 		if (tdvp == tvp)
 2402: 			vrele(tdvp);
 2403: 		else
 2404: 			vput(tdvp);
 2405: 		if (tvp)
 2406: 			vput(tvp);
 2407: 		vrele(fromnd->ni_dvp);
 2408: 		vrele(fvp);
 2409: 	}
 2410: 	vrele(tond->ni_startdir);
 2411: 	ASSERT_VOP_UNLOCKED(fromnd->ni_dvp, "rename");
 2412: 	ASSERT_VOP_UNLOCKED(fromnd->ni_vp, "rename");
 2413: 	ASSERT_VOP_UNLOCKED(tond->ni_dvp, "rename");
 2414: 	ASSERT_VOP_UNLOCKED(tond->ni_vp, "rename");
 2415: out1:
 2416: 	if (fromnd->ni_startdir)
 2417: 		vrele(fromnd->ni_startdir);
 2418: 	if (error == -1)
 2419: 		return (0);
 2420: 	return (error);
 2421: }
 2422: 
 2423: /*
 2424:  * rename_args(char *from, char *to)
 2425:  *
 2426:  * Rename files.  Source and destination must either both be directories,
 2427:  * or both not be directories.  If target is a directory, it must be empty.
 2428:  */
 2429: int
 2430: rename(struct rename_args *uap)
 2431: {
 2432: 	struct thread *td = curthread;
 2433: 	struct nameidata fromnd, tond;
 2434: 	int error;
 2435: 
 2436: 	NDINIT(&fromnd, NAMEI_DELETE, CNP_WANTPARENT | CNP_SAVESTART,
 2437: 		UIO_USERSPACE, uap->from, td);
 2438: 	NDINIT(&tond, NAMEI_RENAME, 
 2439: 	    CNP_LOCKPARENT | CNP_LOCKLEAF | CNP_NOCACHE |
 2440: 	     CNP_SAVESTART | CNP_NOOBJ,
 2441: 	    UIO_USERSPACE, uap->to, td);
 2442: 
 2443: 	error = kern_rename(&fromnd, &tond);
 2444: 
 2445: 	return (error);
 2446: }
 2447: 
 2448: int
 2449: kern_mkdir(struct nameidata *nd, int mode)
 2450: {
 2451: 	struct thread *td = curthread;
 2452: 	struct proc *p = td->td_proc;
 2453: 	struct vnode *vp;
 2454: 	struct vattr vattr;
 2455: 	int error;
 2456: 
 2457: 	bwillwrite();
 2458: 	nd->ni_cnd.cn_flags |= CNP_WILLBEDIR;
 2459: 	error = namei(nd);
 2460: 	if (error)
 2461: 		return (error);
 2462: 	vp = nd->ni_vp;
 2463: 	if (vp) {
 2464: 		NDFREE(nd, NDF_ONLY_PNBUF);
 2465: 		if (nd->ni_dvp == vp)
 2466: 			vrele(nd->ni_dvp);
 2467: 		else
 2468: 			vput(nd->ni_dvp);
 2469: 		vrele(vp);
 2470: 		return (EEXIST);
 2471: 	}
 2472: 	VATTR_NULL(&vattr);
 2473: 	vattr.va_type = VDIR;
 2474: 	vattr.va_mode = (mode & ACCESSPERMS) &~ p->p_fd->fd_cmask;
 2475: 	VOP_LEASE(nd->ni_dvp, td, p->p_ucred, LEASE_WRITE);
 2476: 	error = VOP_MKDIR(nd->ni_dvp, NCPNULL, &nd->ni_vp, &nd->ni_cnd,
 2477: 	    &vattr);
 2478: 	NDFREE(nd, NDF_ONLY_PNBUF);
 2479: 	vput(nd->ni_dvp);
 2480: 	if (error == 0)
 2481: 		vput(nd->ni_vp);
 2482: 	ASSERT_VOP_UNLOCKED(nd->ni_dvp, "mkdir");
 2483: 	ASSERT_VOP_UNLOCKED(nd->ni_vp, "mkdir");
 2484: 	return (error);
 2485: }
 2486: 
 2487: /*
 2488:  * mkdir_args(char *path, int mode)
 2489:  *
 2490:  * Make a directory file.
 2491:  */
 2492: /* ARGSUSED */
 2493: int
 2494: mkdir(struct mkdir_args *uap)
 2495: {
 2496: 	struct thread *td = curthread;
 2497: 	struct nameidata nd;
 2498: 	int error;
 2499: 
 2500: 	NDINIT(&nd, NAMEI_CREATE, CNP_LOCKPARENT, UIO_USERSPACE, uap->path,
 2501: 	    td);
 2502: 
 2503: 	error = kern_mkdir(&nd, uap->mode);
 2504: 
 2505: 	return (error);
 2506: }
 2507: 
 2508: int
 2509: kern_rmdir(struct nameidata *nd)
 2510: {
 2511: 	struct thread *td = curthread;
 2512: 	struct proc *p = td->td_proc;
 2513: 	struct vnode *vp;
 2514: 	int error;
 2515: 
 2516: 	bwillwrite();
 2517: 	error = namei(nd);
 2518: 	if (error)
 2519: 		return (error);
 2520: 	vp = nd->ni_vp;
 2521: 	if (vp->v_type != VDIR) {
 2522: 		error = ENOTDIR;
 2523: 		goto out;
 2524: 	}
 2525: 	/*
 2526: 	 * No rmdir "." please.
 2527: 	 */
 2528: 	if (nd->ni_dvp == vp) {
 2529: 		error = EINVAL;
 2530: 		goto out;
 2531: 	}
 2532: 	/*
 2533: 	 * The root of a mounted filesystem cannot be deleted.
 2534: 	 */
 2535: 	if (vp->v_flag & VROOT)
 2536: 		error = EBUSY;
 2537: 	else {
 2538: 		VOP_LEASE(nd->ni_dvp, td, p->p_ucred, LEASE_WRITE);
 2539: 		VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
 2540: 		error = VOP_RMDIR(nd->ni_dvp, NCPNULL, nd->ni_vp,
 2541: 		    &nd->ni_cnd);
 2542: 	}
 2543: out:
 2544: 	NDFREE(nd, NDF_ONLY_PNBUF);
 2545: 	if (nd->ni_dvp == vp)
 2546: 		vrele(nd->ni_dvp);
 2547: 	else
 2548: 		vput(nd->ni_dvp);
 2549: 	if (vp != NULLVP)
 2550: 		vput(vp);
 2551: 	ASSERT_VOP_UNLOCKED(nd->ni_dvp, "rmdir");
 2552: 	ASSERT_VOP_UNLOCKED(nd->ni_vp, "rmdir");
 2553: 	return (error);
 2554: }
 2555: 
 2556: /*
 2557:  * rmdir_args(char *path)
 2558:  *
 2559:  * Remove a directory file.
 2560:  */
 2561: /* ARGSUSED */
 2562: int
 2563: rmdir(struct rmdir_args *uap)
 2564: {
 2565: 	struct thread *td = curthread;
 2566: 	struct nameidata nd;
 2567: 	int error;
 2568: 
 2569: 	NDINIT(&nd, NAMEI_DELETE, CNP_LOCKPARENT | CNP_LOCKLEAF,
 2570: 	    UIO_USERSPACE, uap->path, td);
 2571: 
 2572: 	error = kern_rmdir(&nd);
 2573: 
 2574: 	return (error);
 2575: }
 2576: 
 2577: int
 2578: kern_getdirentries(int fd, char *buf, u_int count, long *basep, int *res)
 2579: {
 2580: 	struct thread *td = curthread;
 2581: 	struct proc *p = td->td_proc;
 2582: 	struct vnode *vp;
 2583: 	struct file *fp;
 2584: 	struct uio auio;
 2585: 	struct iovec aiov;
 2586: 	long loff;
 2587: 	int error, eofflag;
 2588: 
 2589: 	if ((error = getvnode(p->p_fd, fd, &fp)) != 0)
 2590: 		return (error);
 2591: 	if ((fp->f_flag & FREAD) == 0)
 2592: 		return (EBADF);
 2593: 	vp = (struct vnode *)fp->f_data;
 2594: unionread:
 2595: 	if (vp->v_type != VDIR)
 2596: 		return (EINVAL);
 2597: 	aiov.iov_base = buf;
 2598: 	aiov.iov_len = count;
 2599: 	auio.uio_iov = &aiov;
 2600: 	auio.uio_iovcnt = 1;
 2601: 	auio.uio_rw = UIO_READ;
 2602: 	auio.uio_segflg = UIO_USERSPACE;
 2603: 	auio.uio_td = td;
 2604: 	auio.uio_resid = count;
 2605: 	/* vn_lock(vp, NULL, LK_SHARED | LK_RETRY, td); */
 2606: 	vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
 2607: 	loff = auio.uio_offset = fp->f_offset;
 2608: 	error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL, NULL);
 2609: 	fp->f_offset = auio.uio_offset;
 2610: 	VOP_UNLOCK(vp, NULL, 0, td);
 2611: 	if (error)
 2612: 		return (error);
 2613: 	if (count == auio.uio_resid) {
 2614: 		if (union_dircheckp) {
 2615: 			error = union_dircheckp(td, &vp, fp);
 2616: 			if (error == -1)
 2617: 				goto unionread;
 2618: 			if (error)
 2619: 				return (error);
 2620: 		}
 2621: 		if ((vp->v_flag & VROOT) &&
 2622: 		    (vp->v_mount->mnt_flag & MNT_UNION)) {
 2623: 			struct vnode *tvp = vp;
 2624: 			vp = vp->v_mount->mnt_vnodecovered;
 2625: 			VREF(vp);
 2626: 			fp->f_data = (caddr_t) vp;
 2627: 			fp->f_offset = 0;
 2628: 			vrele(tvp);
 2629: 			goto unionread;
 2630: 		}
 2631: 	}
 2632: 	if (basep) {
 2633: 		*basep = loff;
 2634: 	}
 2635: 	*res = count - auio.uio_resid;
 2636: 	return (error);
 2637: }
 2638: 
 2639: /*
 2640:  * getdirentries_args(int fd, char *buf, u_int conut, long *basep)
 2641:  *
 2642:  * Read a block of directory entries in a file system independent format.
 2643:  */
 2644: int
 2645: getdirentries(struct getdirentries_args *uap)
 2646: {
 2647: 	long base;
 2648: 	int error;
 2649: 
 2650: 	error = kern_getdirentries(uap->fd, uap->buf, uap->count, &base,
 2651: 	    &uap->sysmsg_result);
 2652: 
 2653: 	if (error == 0)
 2654: 		error = copyout(&base, uap->basep, sizeof(*uap->basep));
 2655: 	return (error);
 2656: }
 2657: 
 2658: /*
 2659:  * getdents_args(int fd, char *buf, size_t count)
 2660:  */
 2661: int
 2662: getdents(struct getdents_args *uap)
 2663: {
 2664: 	int error;
 2665: 
 2666: 	error = kern_getdirentries(uap->fd, uap->buf, uap->count, NULL,
 2667: 	    &uap->sysmsg_result);
 2668: 
 2669: 	return (error);
 2670: }
 2671: 
 2672: /*
 2673:  * umask(int newmask)
 2674:  *
 2675:  * Set the mode mask for creation of filesystem nodes.
 2676:  *
 2677:  * MP SAFE
 2678:  */
 2679: int
 2680: umask(struct umask_args *uap)
 2681: {
 2682: 	struct thread *td = curthread;
 2683: 	struct proc *p = td->td_proc;
 2684: 	struct filedesc *fdp;
 2685: 
 2686: 	fdp = p->p_fd;
 2687: 	uap->sysmsg_result = fdp->fd_cmask;
 2688: 	fdp->fd_cmask = SCARG(uap, newmask) & ALLPERMS;
 2689: 	return (0);
 2690: }
 2691: 
 2692: /*
 2693:  * revoke(char *path)
 2694:  *
 2695:  * Void all references to file by ripping underlying filesystem
 2696:  * away from vnode.
 2697:  */
 2698: /* ARGSUSED */
 2699: int
 2700: revoke(struct revoke_args *uap)
 2701: {
 2702: 	struct thread *td = curthread;
 2703: 	struct proc *p = td->td_proc;
 2704: 	struct vnode *vp;
 2705: 	struct vattr vattr;
 2706: 	int error;
 2707: 	struct nameidata nd;
 2708: 
 2709: 	NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE, SCARG(uap, path), td);
 2710: 	if ((error = namei(&nd)) != 0)
 2711: 		return (error);
 2712: 	vp = nd.ni_vp;
 2713: 	NDFREE(&nd, NDF_ONLY_PNBUF);
 2714: 	if (vp->v_type != VCHR && vp->v_type != VBLK) {
 2715: 		error = EINVAL;
 2716: 		goto out;
 2717: 	}
 2718: 	if ((error = VOP_GETATTR(vp, &vattr, td)) != 0)
 2719: 		goto out;
 2720: 	if (p->p_ucred->cr_uid != vattr.va_uid &&
 2721: 	    (error = suser_cred(p->p_ucred, PRISON_ROOT)))
 2722: 		goto out;
 2723: 	if (vcount(vp) > 1)
 2724: 		VOP_REVOKE(vp, REVOKEALL);
 2725: out:
 2726: 	vrele(vp);
 2727: 	return (error);
 2728: }
 2729: 
 2730: /*
 2731:  * Convert a user file descriptor to a kernel file entry.
 2732:  */
 2733: int
 2734: getvnode(struct filedesc *fdp, int fd, struct file **fpp)
 2735: {
 2736: 	struct file *fp;
 2737: 
 2738: 	if ((u_int)fd >= fdp->fd_nfiles ||
 2739: 	    (fp = fdp->fd_ofiles[fd]) == NULL)
 2740: 		return (EBADF);
 2741: 	if (fp->f_type != DTYPE_VNODE && fp->f_type != DTYPE_FIFO)
 2742: 		return (EINVAL);
 2743: 	*fpp = fp;
 2744: 	return (0);
 2745: }
 2746: /*
 2747:  * getfh_args(char *fname, fhandle_t *fhp)
 2748:  *
 2749:  * Get (NFS) file handle
 2750:  */
 2751: int
 2752: getfh(struct getfh_args *uap)
 2753: {
 2754: 	struct thread *td = curthread;
 2755: 	struct nameidata nd;
 2756: 	fhandle_t fh;
 2757: 	struct vnode *vp;
 2758: 	int error;
 2759: 
 2760: 	/*
 2761: 	 * Must be super user
 2762: 	 */
 2763: 	error = suser(td);
 2764: 	if (error)
 2765: 		return (error);
 2766: 	NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF, UIO_USERSPACE, uap->fname, td);
 2767: 	error = namei(&nd);
 2768: 	if (error)
 2769: 		return (error);
 2770: 	NDFREE(&nd, NDF_ONLY_PNBUF);
 2771: 	vp = nd.ni_vp;
 2772: 	bzero(&fh, sizeof(fh));
 2773: 	fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
 2774: 	error = VFS_VPTOFH(vp, &fh.fh_fid);
 2775: 	vput(vp);
 2776: 	if (error)
 2777: 		return (error);
 2778: 	error = copyout(&fh, uap->fhp, sizeof (fh));
 2779: 	return (error);
 2780: }
 2781: 
 2782: /*
 2783:  * fhopen_args(const struct fhandle *u_fhp, int flags)
 2784:  *
 2785:  * syscall for the rpc.lockd to use to translate a NFS file handle into
 2786:  * an open descriptor.
 2787:  *
 2788:  * warning: do not remove the suser() call or this becomes one giant
 2789:  * security hole.
 2790:  */
 2791: int
 2792: fhopen(struct fhopen_args *uap)
 2793: {
 2794: 	struct thread *td = curthread;
 2795: 	struct proc *p = td->td_proc;
 2796: 	struct mount *mp;
 2797: 	struct vnode *vp;
 2798: 	struct fhandle fhp;
 2799: 	struct vattr vat;
 2800: 	struct vattr *vap = &vat;
 2801: 	struct flock lf;
 2802: 	struct file *fp;
 2803: 	struct filedesc *fdp = p->p_fd;
 2804: 	int fmode, mode, error, type;
 2805: 	struct file *nfp; 
 2806: 	int indx;
 2807: 
 2808: 	/*
 2809: 	 * Must be super user
 2810: 	 */
 2811: 	error = suser(td);
 2812: 	if (error)
 2813: 		return (error);
 2814: 
 2815: 	fmode = FFLAGS(SCARG(uap, flags));
 2816: 	/* why not allow a non-read/write open for our lockd? */
 2817: 	if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
 2818: 		return (EINVAL);
 2819: 	error = copyin(SCARG(uap,u_fhp), &fhp, sizeof(fhp));
 2820: 	if (error)
 2821: 		return(error);
 2822: 	/* find the mount point */
 2823: 	mp = vfs_getvfs(&fhp.fh_fsid);
 2824: 	if (mp == NULL)
 2825: 		return (ESTALE);
 2826: 	/* now give me my vnode, it gets returned to me locked */
 2827: 	error = VFS_FHTOVP(mp, &fhp.fh_fid, &vp);
 2828: 	if (error)
 2829: 		return (error);
 2830:  	/*
 2831: 	 * from now on we have to make sure not
 2832: 	 * to forget about the vnode
 2833: 	 * any error that causes an abort must vput(vp) 
 2834: 	 * just set error = err and 'goto bad;'.
 2835: 	 */
 2836: 
 2837: 	/* 
 2838: 	 * from vn_open 
 2839: 	 */
 2840: 	if (vp->v_type == VLNK) {
 2841: 		error = EMLINK;
 2842: 		goto bad;
 2843: 	}
 2844: 	if (vp->v_type == VSOCK) {
 2845: 		error = EOPNOTSUPP;
 2846: 		goto bad;
 2847: 	}
 2848: 	mode = 0;
 2849: 	if (fmode & (FWRITE | O_TRUNC)) {
 2850: 		if (vp->v_type == VDIR) {
 2851: 			error = EISDIR;
 2852: 			goto bad;
 2853: 		}
 2854: 		error = vn_writechk(vp);
 2855: 		if (error)
 2856: 			goto bad;
 2857: 		mode |= VWRITE;
 2858: 	}
 2859: 	if (fmode & FREAD)
 2860: 		mode |= VREAD;
 2861: 	if (mode) {
 2862: 		error = VOP_ACCESS(vp, mode, p->p_ucred, td);
 2863: 		if (error)
 2864: 			goto bad;
 2865: 	}
 2866: 	if (fmode & O_TRUNC) {
 2867: 		VOP_UNLOCK(vp, NULL, 0, td);			/* XXX */
 2868: 		VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
 2869: 		vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);	/* XXX */
 2870: 		VATTR_NULL(vap);
 2871: 		vap->va_size = 0;
 2872: 		error = VOP_SETATTR(vp, vap, p->p_ucred, td);
 2873: 		if (error)
 2874: 			goto bad;
 2875: 	}
 2876: 	error = VOP_OPEN(vp, fmode, p->p_ucred, td);
 2877: 	if (error)
 2878: 		goto bad;
 2879: 	/*
 2880: 	 * Make sure that a VM object is created for VMIO support.
 2881: 	 */
 2882: 	if (vn_canvmio(vp) == TRUE) {
 2883: 		if ((error = vfs_object_create(vp, td)) != 0)
 2884: 			goto bad;
 2885: 	}
 2886: 	if (fmode & FWRITE)
 2887: 		vp->v_writecount++;
 2888: 
 2889: 	/*
 2890: 	 * end of vn_open code 
 2891: 	 */
 2892: 
 2893: 	if ((error = falloc(p, &nfp, &indx)) != 0) {
 2894: 		if (fmode & FWRITE)
 2895: 			vp->v_writecount--;
 2896: 		goto bad;
 2897: 	}
 2898: 	fp = nfp;	
 2899: 
 2900: 	/*
 2901: 	 * hold an extra reference to avoid having fp ripped out
 2902: 	 * from under us while we block in the lock op.
 2903: 	 */
 2904: 	fhold(fp);
 2905: 	nfp->f_data = (caddr_t)vp;
 2906: 	nfp->f_flag = fmode & FMASK;
 2907: 	nfp->f_ops = &vnops;
 2908: 	nfp->f_type = DTYPE_VNODE;
 2909: 	if (fmode & (O_EXLOCK | O_SHLOCK)) {
 2910: 		lf.l_whence = SEEK_SET;
 2911: 		lf.l_start = 0;
 2912: 		lf.l_len = 0;
 2913: 		if (fmode & O_EXLOCK)
 2914: 			lf.l_type = F_WRLCK;
 2915: 		else
 2916: 			lf.l_type = F_RDLCK;
 2917: 		type = F_FLOCK;
 2918: 		if ((fmode & FNONBLOCK) == 0)
 2919: 			type |= F_WAIT;
 2920: 		VOP_UNLOCK(vp, NULL, 0, td);
 2921: 		if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
 2922: 			/*
 2923: 			 * lock request failed.  Normally close the descriptor
 2924: 			 * but handle the case where someone might have dup()d
 2925: 			 * or close()d it when we weren't looking.
 2926: 			 */
 2927: 			if (fdp->fd_ofiles[indx] == fp) {
 2928: 				fdp->fd_ofiles[indx] = NULL;
 2929: 				fdrop(fp, td);
 2930: 			}
 2931: 
 2932: 			/*
 2933: 			 * release our private reference.
 2934: 			 */
 2935: 			fdrop(fp, td);
 2936: 			return (error);
 2937: 		}
 2938: 		vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
 2939: 		fp->f_flag |= FHASLOCK;
 2940: 	}
 2941: 	if ((vp->v_type == VREG) && (VOP_GETVOBJECT(vp, NULL) != 0))
 2942: 		vfs_object_create(vp, td);
 2943: 
 2944: 	VOP_UNLOCK(vp, NULL, 0, td);
 2945: 	fdrop(fp, td);
 2946: 	uap->sysmsg_result = indx;
 2947: 	return (0);
 2948: 
 2949: bad:
 2950: 	vput(vp);
 2951: 	return (error);
 2952: }
 2953: 
 2954: /*
 2955:  * fhstat_args(struct fhandle *u_fhp, struct stat *sb)
 2956:  */
 2957: int
 2958: fhstat(struct fhstat_args *uap)
 2959: {
 2960: 	struct thread *td = curthread;
 2961: 	struct stat sb;
 2962: 	fhandle_t fh;
 2963: 	struct mount *mp;
 2964: 	struct vnode *vp;
 2965: 	int error;
 2966: 
 2967: 	/*
 2968: 	 * Must be super user
 2969: 	 */
 2970: 	error = suser(td);
 2971: 	if (error)
 2972: 		return (error);
 2973: 	
 2974: 	error = copyin(SCARG(uap, u_fhp), &fh, sizeof(fhandle_t));
 2975: 	if (error)
 2976: 		return (error);
 2977: 
 2978: 	if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
 2979: 		return (ESTALE);
 2980: 	if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)))
 2981: 		return (error);
 2982: 	error = vn_stat(vp, &sb, td);
 2983: 	vput(vp);
 2984: 	if (error)
 2985: 		return (error);
 2986: 	error = copyout(&sb, SCARG(uap, sb), sizeof(sb));
 2987: 	return (error);
 2988: }
 2989: 
 2990: /*
 2991:  * fhstatfs_args(struct fhandle *u_fhp, struct statfs *buf)
 2992:  */
 2993: int
 2994: fhstatfs(struct fhstatfs_args *uap)
 2995: {
 2996: 	struct thread *td = curthread;
 2997: 	struct statfs *sp;
 2998: 	struct mount *mp;
 2999: 	struct vnode *vp;
 3000: 	struct statfs sb;
 3001: 	fhandle_t fh;
 3002: 	int error;
 3003: 
 3004: 	/*
 3005: 	 * Must be super user
 3006: 	 */
 3007: 	if ((error = suser(td)))
 3008: 		return (error);
 3009: 
 3010: 	if ((error = copyin(SCARG(uap, u_fhp), &fh, sizeof(fhandle_t))) != 0)
 3011: 		return (error);
 3012: 
 3013: 	if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
 3014: 		return (ESTALE);
 3015: 	if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)))
 3016: 		return (error);
 3017: 	mp = vp->v_mount;
 3018: 	sp = &mp->mnt_stat;
 3019: 	vput(vp);
 3020: 	if ((error = VFS_STATFS(mp, sp, td)) != 0)
 3021: 		return (error);
 3022: 	sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
 3023: 	if (suser(td)) {
 3024: 		bcopy((caddr_t)sp, (caddr_t)&sb, sizeof(sb));
 3025: 		sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
 3026: 		sp = &sb;
 3027: 	}
 3028: 	return (copyout(sp, SCARG(uap, buf), sizeof(*sp)));
 3029: }
 3030: 
 3031: /*
 3032:  * Syscall to push extended attribute configuration information into the
 3033:  * VFS.  Accepts a path, which it converts to a mountpoint, as well as
 3034:  * a command (int cmd), and attribute name and misc data.  For now, the
 3035:  * attribute name is left in userspace for consumption by the VFS_op.
 3036:  * It will probably be changed to be copied into sysspace by the
 3037:  * syscall in the future, once issues with various consumers of the
 3038:  * attribute code have raised their hands.
 3039:  *
 3040:  * Currently this is used only by UFS Extended Attributes.
 3041:  */
 3042: int
 3043: extattrctl(struct extattrctl_args *uap)
 3044: {
 3045: 	struct thread *td = curthread;
 3046: 	struct nameidata nd;
 3047: 	struct mount *mp;
 3048: 	int error;
 3049: 
 3050: 	NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE, SCARG(uap, path), td);
 3051: 	if ((error = namei(&nd)) != 0)
 3052: 		return (error);
 3053: 	mp = nd.ni_vp->v_mount;
 3054: 	NDFREE(&nd, 0);
 3055: 	return (VFS_EXTATTRCTL(mp, SCARG(uap, cmd), SCARG(uap, attrname),
 3056: 	    SCARG(uap, arg), td));
 3057: }
 3058: 
 3059: /*
 3060:  * Syscall to set a named extended attribute on a file or directory.
 3061:  * Accepts attribute name, and a uio structure pointing to the data to set.
 3062:  * The uio is consumed in the style of writev().  The real work happens
 3063:  * in VOP_SETEXTATTR().
 3064:  */
 3065: int
 3066: extattr_set_file(struct extattr_set_file_args *uap)
 3067: {
 3068: 	struct thread *td = curthread;
 3069: 	struct proc *p = td->td_proc;
 3070: 	struct nameidata nd;
 3071: 	struct uio auio;
 3072: 	struct iovec *iov, *needfree = NULL, aiov[UIO_SMALLIOV];
 3073: 	char attrname[EXTATTR_MAXNAMELEN];
 3074: 	u_int iovlen, cnt;
 3075: 	int error, i;
 3076: 
 3077: 	error = copyin(SCARG(uap, attrname), attrname, EXTATTR_MAXNAMELEN);
 3078: 	if (error)
 3079: 		return (error);
 3080: 	NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF, UIO_USERSPACE,
 3081: 	    SCARG(uap, path), td);
 3082: 	if ((error = namei(&nd)) != 0)
 3083: 		return(error);
 3084: 	iovlen = uap->iovcnt * sizeof(struct iovec);
 3085: 	if (uap->iovcnt > UIO_SMALLIOV) {
 3086: 		if (uap->iovcnt > UIO_MAXIOV) {
 3087: 			error = EINVAL;
 3088: 			goto done;
 3089: 		}
 3090: 		MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
 3091: 		needfree = iov;
 3092: 	} else
 3093: 		iov = aiov;
 3094: 	auio.uio_iov = iov;
 3095: 	auio.uio_iovcnt = uap->iovcnt;
 3096: 	auio.uio_rw = UIO_WRITE;
 3097: 	auio.uio_segflg = UIO_USERSPACE;
 3098: 	auio.uio_td = td;
 3099: 	auio.uio_offset = 0;
 3100: 	if ((error = copyin((caddr_t)uap->iovp, (caddr_t)iov, iovlen)))
 3101: 		goto done;
 3102: 	auio.uio_resid = 0;
 3103: 	for (i = 0; i < uap->iovcnt; i++) {
 3104: 		if (iov->iov_len > INT_MAX - auio.uio_resid) {
 3105: 			error = EINVAL;
 3106: 			goto done;
 3107: 		}
 3108: 		auio.uio_resid += iov->iov_len;
 3109: 		iov++;
 3110: 	}
 3111: 	cnt = auio.uio_resid;
 3112: 	error = VOP_SETEXTATTR(nd.ni_vp, attrname, &auio, p->p_ucred, td);
 3113: 	cnt -= auio.uio_resid;
 3114: 	uap->sysmsg_result = cnt;
 3115: done:
 3116: 	if (needfree)
 3117: 		FREE(needfree, M_IOV);
 3118: 	NDFREE(&nd, 0);
 3119: 	return (error);
 3120: }
 3121: 
 3122: /*
 3123:  * Syscall to get a named extended attribute on a file or directory.
 3124:  * Accepts attribute name, and a uio structure pointing to a buffer for the
 3125:  * data.  The uio is consumed in the style of readv().  The real work
 3126:  * happens in VOP_GETEXTATTR();
 3127:  */
 3128: int
 3129: extattr_get_file(struct extattr_get_file_args *uap)
 3130: {
 3131: 	struct thread *td = curthread;
 3132: 	struct proc *p = td->td_proc;
 3133: 	struct nameidata nd;
 3134: 	struct uio auio;
 3135: 	struct iovec *iov, *needfree, aiov[UIO_SMALLIOV];
 3136: 	char attrname[EXTATTR_MAXNAMELEN];
 3137: 	u_int iovlen, cnt;
 3138: 	int error, i;
 3139: 
 3140: 	error = copyin(SCARG(uap, attrname), attrname, EXTATTR_MAXNAMELEN);
 3141: 	if (error)
 3142: 		return (error);
 3143: 	NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF, UIO_USERSPACE,
 3144: 	    SCARG(uap, path), td);
 3145: 	if ((error = namei(&nd)) != 0)
 3146: 		return (error);
 3147: 	iovlen = uap->iovcnt * sizeof (struct iovec);
 3148: 	if (uap->iovcnt > UIO_SMALLIOV) {
 3149: 		if (uap->iovcnt > UIO_MAXIOV) {
 3150: 			NDFREE(&nd, 0);
 3151: 			return (EINVAL);
 3152: 		}
 3153: 		MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
 3154: 		needfree = iov;
 3155: 	} else {
 3156: 		iov = aiov;
 3157: 		needfree = NULL;
 3158: 	}
 3159: 	auio.uio_iov = iov;
 3160: 	auio.uio_iovcnt = uap->iovcnt;
 3161: 	auio.uio_rw = UIO_READ;
 3162: 	auio.uio_segflg = UIO_USERSPACE;
 3163: 	auio.uio_td = td;
 3164: 	auio.uio_offset = 0;
 3165: 	if ((error = copyin((caddr_t)uap->iovp, (caddr_t)iov, iovlen)))
 3166: 		goto done;
 3167: 	auio.uio_resid = 0;
 3168: 	for (i = 0; i < uap->iovcnt; i++) {
 3169: 		if (iov->iov_len > INT_MAX - auio.uio_resid) {
 3170: 			error = EINVAL;
 3171: 			goto done;
 3172: 		}
 3173: 		auio.uio_resid += iov->iov_len;
 3174: 		iov++;
 3175: 	}
 3176: 	cnt = auio.uio_resid;
 3177: 	error = VOP_GETEXTATTR(nd.ni_vp, attrname, &auio, p->p_ucred, td);
 3178: 	cnt -= auio.uio_resid;
 3179: 	uap->sysmsg_result = cnt;
 3180: done:
 3181: 	if (needfree)
 3182: 		FREE(needfree, M_IOV);
 3183: 	NDFREE(&nd, 0);
 3184: 	return(error);
 3185: }
 3186: 
 3187: /*
 3188:  * Syscall to delete a named extended attribute from a file or directory.
 3189:  * Accepts attribute name.  The real work happens in VOP_SETEXTATTR().
 3190:  */
 3191: int
 3192: extattr_delete_file(struct extattr_delete_file_args *uap)
 3193: {
 3194: 	struct thread *td = curthread;
 3195: 	struct proc *p = td->td_proc;
 3196: 	struct nameidata nd;
 3197: 	char attrname[EXTATTR_MAXNAMELEN];
 3198: 	int	error;
 3199: 
 3200: 	error = copyin(SCARG(uap, attrname), attrname, EXTATTR_MAXNAMELEN);
 3201: 	if (error)
 3202: 		return(error);
 3203: 	NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF, UIO_USERSPACE,
 3204: 	    SCARG(uap, path), td);
 3205: 	if ((error = namei(&nd)) != 0)
 3206: 		return(error);
 3207: 	error = VOP_SETEXTATTR(nd.ni_vp, attrname, NULL, p->p_ucred, td);
 3208: 	NDFREE(&nd, 0);
 3209: 	return(error);
 3210: }