Home | History | Annotate | Line # | Download | only in kern
vfs_syscalls.c revision 1.196
      1 /*	$NetBSD: vfs_syscalls.c,v 1.196 2003/10/14 14:02:56 dbj Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1989, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  * (c) UNIX System Laboratories, Inc.
      7  * All or some portions of this file are derived from material licensed
      8  * to the University of California by American Telephone and Telegraph
      9  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
     10  * the permission of UNIX System Laboratories, Inc.
     11  *
     12  * Redistribution and use in source and binary forms, with or without
     13  * modification, are permitted provided that the following conditions
     14  * are met:
     15  * 1. Redistributions of source code must retain the above copyright
     16  *    notice, this list of conditions and the following disclaimer.
     17  * 2. Redistributions in binary form must reproduce the above copyright
     18  *    notice, this list of conditions and the following disclaimer in the
     19  *    documentation and/or other materials provided with the distribution.
     20  * 3. Neither the name of the University nor the names of its contributors
     21  *    may be used to endorse or promote products derived from this software
     22  *    without specific prior written permission.
     23  *
     24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  * SUCH DAMAGE.
     35  *
     36  *	@(#)vfs_syscalls.c	8.42 (Berkeley) 7/31/95
     37  */
     38 
     39 #include <sys/cdefs.h>
     40 __KERNEL_RCSID(0, "$NetBSD: vfs_syscalls.c,v 1.196 2003/10/14 14:02:56 dbj Exp $");
     41 
     42 #include "opt_compat_netbsd.h"
     43 #include "opt_compat_43.h"
     44 #include "opt_ktrace.h"
     45 
     46 #include <sys/param.h>
     47 #include <sys/systm.h>
     48 #include <sys/namei.h>
     49 #include <sys/filedesc.h>
     50 #include <sys/kernel.h>
     51 #include <sys/file.h>
     52 #include <sys/stat.h>
     53 #include <sys/vnode.h>
     54 #include <sys/mount.h>
     55 #include <sys/proc.h>
     56 #include <sys/uio.h>
     57 #include <sys/malloc.h>
     58 #include <sys/dirent.h>
     59 #include <sys/sysctl.h>
     60 #include <sys/sa.h>
     61 #include <sys/syscallargs.h>
     62 #ifdef KTRACE
     63 #include <sys/ktrace.h>
     64 #endif
     65 
     66 #include <miscfs/genfs/genfs.h>
     67 #include <miscfs/syncfs/syncfs.h>
     68 
     69 MALLOC_DEFINE(M_MOUNT, "mount", "vfs mount struct");
     70 
     71 static int change_dir __P((struct nameidata *, struct proc *));
     72 static int change_flags __P((struct vnode *, u_long, struct proc *));
     73 static int change_mode __P((struct vnode *, int, struct proc *p));
     74 static int change_owner __P((struct vnode *, uid_t, gid_t, struct proc *,
     75     int));
     76 static int change_utimes __P((struct vnode *vp, const struct timeval *,
     77 	       struct proc *p));
     78 static int rename_files __P((const char *, const char *, struct proc *, int));
     79 static int dostatfs __P((struct mount *, struct statfs *, struct proc *, int,
     80     int));
     81 
     82 void checkdirs __P((struct vnode *));
     83 
     84 int dovfsusermount = 0;
     85 
     86 /*
     87  * Virtual File System System Calls
     88  */
     89 
     90 /*
     91  * Mount a file system.
     92  */
     93 
     94 #if defined(COMPAT_09) || defined(COMPAT_43)
     95 /*
     96  * This table is used to maintain compatibility with 4.3BSD
     97  * and NetBSD 0.9 mount syscalls.  Note, the order is important!
     98  *
     99  * Do not modify this table. It should only contain filesystems
    100  * supported by NetBSD 0.9 and 4.3BSD.
    101  */
    102 const char * const mountcompatnames[] = {
    103 	NULL,		/* 0 = MOUNT_NONE */
    104 	MOUNT_FFS,	/* 1 = MOUNT_UFS */
    105 	MOUNT_NFS,	/* 2 */
    106 	MOUNT_MFS,	/* 3 */
    107 	MOUNT_MSDOS,	/* 4 */
    108 	MOUNT_CD9660,	/* 5 = MOUNT_ISOFS */
    109 	MOUNT_FDESC,	/* 6 */
    110 	MOUNT_KERNFS,	/* 7 */
    111 	NULL,		/* 8 = MOUNT_DEVFS */
    112 	MOUNT_AFS,	/* 9 */
    113 };
    114 const int nmountcompatnames = sizeof(mountcompatnames) /
    115     sizeof(mountcompatnames[0]);
    116 #endif /* COMPAT_09 || COMPAT_43 */
    117 
    118 /* ARGSUSED */
    119 int
    120 sys_mount(l, v, retval)
    121 	struct lwp *l;
    122 	void *v;
    123 	register_t *retval;
    124 {
    125 	struct sys_mount_args /* {
    126 		syscallarg(const char *) type;
    127 		syscallarg(const char *) path;
    128 		syscallarg(int) flags;
    129 		syscallarg(void *) data;
    130 	} */ *uap = v;
    131 	struct proc *p = l->l_proc;
    132 	struct vnode *vp;
    133 	struct mount *mp;
    134 	int error, flag = 0;
    135 	char fstypename[MFSNAMELEN];
    136 	struct vattr va;
    137 	struct nameidata nd;
    138 	struct vfsops *vfs;
    139 
    140 	if (dovfsusermount == 0 && (SCARG(uap, flags) & MNT_GETARGS) == 0 &&
    141 	    (error = suser(p->p_ucred, &p->p_acflag)))
    142 		return (error);
    143 	/*
    144 	 * Get vnode to be covered
    145 	 */
    146 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE,
    147 	    SCARG(uap, path), p);
    148 	if ((error = namei(&nd)) != 0)
    149 		return (error);
    150 	vp = nd.ni_vp;
    151 	/*
    152 	 * A lookup in VFS_MOUNT might result in an attempt to
    153 	 * lock this vnode again, so make the lock recursive.
    154 	 */
    155 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY | LK_SETRECURSE);
    156 	if (SCARG(uap, flags) & (MNT_UPDATE | MNT_GETARGS)) {
    157 		if ((vp->v_flag & VROOT) == 0) {
    158 			vput(vp);
    159 			return (EINVAL);
    160 		}
    161 		mp = vp->v_mount;
    162 		flag = mp->mnt_flag;
    163 		vfs = mp->mnt_op;
    164 		/*
    165 		 * We only allow the filesystem to be reloaded if it
    166 		 * is currently mounted read-only.
    167 		 */
    168 		if ((SCARG(uap, flags) & MNT_RELOAD) &&
    169 		    ((mp->mnt_flag & MNT_RDONLY) == 0)) {
    170 			vput(vp);
    171 			return (EOPNOTSUPP);	/* Needs translation */
    172 		}
    173 		/*
    174 		 * In "highly secure" mode, don't let the caller do anything
    175 		 * but downgrade a filesystem from read-write to read-only.
    176 		 * (see also below; MNT_UPDATE or MNT_GETARGS is required.)
    177 		 */
    178 		if (securelevel >= 2 &&
    179 		    SCARG(uap, flags) != MNT_GETARGS &&
    180 		    SCARG(uap, flags) !=
    181 		    (mp->mnt_flag | MNT_RDONLY |
    182 		     MNT_RELOAD | MNT_FORCE | MNT_UPDATE)) {
    183 			vput(vp);
    184 			return (EPERM);
    185 		}
    186 		mp->mnt_flag |= SCARG(uap, flags) &
    187 		    (MNT_RELOAD | MNT_FORCE | MNT_UPDATE | MNT_GETARGS);
    188 		/*
    189 		 * Only root, or the user that did the original mount is
    190 		 * permitted to update it.
    191 		 */
    192 		if ((mp->mnt_flag & MNT_GETARGS) == 0 &&
    193 		    mp->mnt_stat.f_owner != p->p_ucred->cr_uid &&
    194 		    (error = suser(p->p_ucred, &p->p_acflag)) != 0) {
    195 			vput(vp);
    196 			return (error);
    197 		}
    198 		/*
    199 		 * Do not allow NFS export by non-root users. For non-root
    200 		 * users, silently enforce MNT_NOSUID and MNT_NODEV, and
    201 		 * MNT_NOEXEC if mount point is already MNT_NOEXEC.
    202 		 */
    203 		if (p->p_ucred->cr_uid != 0) {
    204 			if (SCARG(uap, flags) & MNT_EXPORTED) {
    205 				vput(vp);
    206 				return (EPERM);
    207 			}
    208 			SCARG(uap, flags) |= MNT_NOSUID | MNT_NODEV;
    209 			if (flag & MNT_NOEXEC)
    210 				SCARG(uap, flags) |= MNT_NOEXEC;
    211 		}
    212 		if (vfs_busy(mp, LK_NOWAIT, 0)) {
    213 			vput(vp);
    214 			return (EPERM);
    215 		}
    216 		goto update;
    217 	} else {
    218 		if (securelevel >= 2) {
    219 			vput(vp);
    220 			return (EPERM);
    221 		}
    222 	}
    223 	/*
    224 	 * If the user is not root, ensure that they own the directory
    225 	 * onto which we are attempting to mount.
    226 	 */
    227 	if ((error = VOP_GETATTR(vp, &va, p->p_ucred, p)) != 0 ||
    228 	    (va.va_uid != p->p_ucred->cr_uid &&
    229 		(error = suser(p->p_ucred, &p->p_acflag)) != 0)) {
    230 		vput(vp);
    231 		return (error);
    232 	}
    233 	/*
    234 	 * Do not allow NFS export by non-root users. For non-root users,
    235 	 * silently enforce MNT_NOSUID and MNT_NODEV, and MNT_NOEXEC if the
    236 	 * mount point is already MNT_NOEXEC.
    237 	 */
    238 	if (p->p_ucred->cr_uid != 0) {
    239 		if (SCARG(uap, flags) & MNT_EXPORTED) {
    240 			vput(vp);
    241 			return (EPERM);
    242 		}
    243 		SCARG(uap, flags) |= MNT_NOSUID | MNT_NODEV;
    244 		if (vp->v_mount->mnt_flag & MNT_NOEXEC)
    245 			SCARG(uap, flags) |= MNT_NOEXEC;
    246 	}
    247 	if ((error = vinvalbuf(vp, V_SAVE, p->p_ucred, p, 0, 0)) != 0)
    248 		return (error);
    249 	if (vp->v_type != VDIR) {
    250 		vput(vp);
    251 		return (ENOTDIR);
    252 	}
    253 	error = copyinstr(SCARG(uap, type), fstypename, MFSNAMELEN, NULL);
    254 	if (error) {
    255 #if defined(COMPAT_09) || defined(COMPAT_43)
    256 		/*
    257 		 * Historically filesystem types were identified by number.
    258 		 * If we get an integer for the filesystem type instead of a
    259 		 * string, we check to see if it matches one of the historic
    260 		 * filesystem types.
    261 		 */
    262 		u_long fsindex = (u_long)SCARG(uap, type);
    263 		if (fsindex >= nmountcompatnames ||
    264 		    mountcompatnames[fsindex] == NULL) {
    265 			vput(vp);
    266 			return (ENODEV);
    267 		}
    268 		strncpy(fstypename, mountcompatnames[fsindex], MFSNAMELEN);
    269 #else
    270 		vput(vp);
    271 		return (error);
    272 #endif
    273 	}
    274 #ifdef	COMPAT_10
    275 	/* Accept `ufs' as an alias for `ffs'. */
    276 	if (!strncmp(fstypename, "ufs", MFSNAMELEN))
    277 		strncpy(fstypename, "ffs", MFSNAMELEN);
    278 #endif
    279 	if ((vfs = vfs_getopsbyname(fstypename)) == NULL) {
    280 		vput(vp);
    281 		return (ENODEV);
    282 	}
    283 	if (vp->v_mountedhere != NULL) {
    284 		vput(vp);
    285 		return (EBUSY);
    286 	}
    287 
    288 	/*
    289 	 * Allocate and initialize the file system.
    290 	 */
    291 	mp = (struct mount *)malloc((u_long)sizeof(struct mount),
    292 		M_MOUNT, M_WAITOK);
    293 	memset((char *)mp, 0, (u_long)sizeof(struct mount));
    294 	lockinit(&mp->mnt_lock, PVFS, "vfslock", 0, 0);
    295 	(void)vfs_busy(mp, LK_NOWAIT, 0);
    296 	mp->mnt_op = vfs;
    297 	vfs->vfs_refcount++;
    298 	mp->mnt_vnodecovered = vp;
    299 	mp->mnt_stat.f_owner = p->p_ucred->cr_uid;
    300 	mp->mnt_unmounter = NULL;
    301 
    302 	/*
    303 	 * The underlying file system may refuse the mount for
    304 	 * various reasons.  Allow root to force it to happen.
    305 	 */
    306 	if (p->p_ucred->cr_uid == 0)
    307 		mp->mnt_flag |= SCARG(uap, flags) & MNT_FORCE;
    308  update:
    309 	/*
    310 	 * Set the mount level flags.
    311 	 */
    312 	if (SCARG(uap, flags) & MNT_RDONLY)
    313 		mp->mnt_flag |= MNT_RDONLY;
    314 	else if (mp->mnt_flag & MNT_RDONLY)
    315 		mp->mnt_iflag |= IMNT_WANTRDWR;
    316 	mp->mnt_flag &=
    317 	  ~(MNT_NOSUID | MNT_NOEXEC | MNT_NODEV |
    318 	    MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_NOCOREDUMP |
    319 	    MNT_NOATIME | MNT_NODEVMTIME | MNT_SYMPERM | MNT_SOFTDEP);
    320 	mp->mnt_flag |= SCARG(uap, flags) &
    321 	   (MNT_NOSUID | MNT_NOEXEC | MNT_NODEV |
    322 	    MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_NOCOREDUMP |
    323 	    MNT_NOATIME | MNT_NODEVMTIME | MNT_SYMPERM | MNT_SOFTDEP |
    324 	    MNT_IGNORE);
    325 	/*
    326 	 * Mount the filesystem.
    327 	 */
    328 	error = VFS_MOUNT(mp, SCARG(uap, path), SCARG(uap, data), &nd, p);
    329 	if (mp->mnt_flag & (MNT_UPDATE | MNT_GETARGS)) {
    330 		if (mp->mnt_iflag & IMNT_WANTRDWR)
    331 			mp->mnt_flag &= ~MNT_RDONLY;
    332 		if (error || (mp->mnt_flag & MNT_GETARGS))
    333 			mp->mnt_flag = flag;
    334 		mp->mnt_flag &=~
    335 		    (MNT_RELOAD | MNT_FORCE | MNT_UPDATE | MNT_GETARGS);
    336 		mp->mnt_iflag &=~ IMNT_WANTRDWR;
    337 		if ((mp->mnt_flag & (MNT_RDONLY | MNT_ASYNC)) == 0) {
    338 			if (mp->mnt_syncer == NULL)
    339 				error = vfs_allocate_syncvnode(mp);
    340 		} else {
    341 			if (mp->mnt_syncer != NULL)
    342 				vfs_deallocate_syncvnode(mp);
    343 		}
    344 		vfs_unbusy(mp);
    345 		VOP_UNLOCK(vp, 0);
    346 		vrele(vp);
    347 		return (error);
    348 	}
    349 	/*
    350 	 * Put the new filesystem on the mount list after root.
    351 	 */
    352 	cache_purge(vp);
    353 	if (!error) {
    354 		mp->mnt_flag &=~
    355 		    (MNT_RELOAD | MNT_FORCE | MNT_UPDATE | MNT_GETARGS);
    356 		mp->mnt_iflag &=~ IMNT_WANTRDWR;
    357 		vp->v_mountedhere = mp;
    358 		simple_lock(&mountlist_slock);
    359 		CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
    360 		simple_unlock(&mountlist_slock);
    361 		checkdirs(vp);
    362 		VOP_UNLOCK(vp, 0);
    363 		if ((mp->mnt_flag & (MNT_RDONLY | MNT_ASYNC)) == 0)
    364 			error = vfs_allocate_syncvnode(mp);
    365 		vfs_unbusy(mp);
    366 		(void) VFS_STATFS(mp, &mp->mnt_stat, p);
    367 		if ((error = VFS_START(mp, 0, p)))
    368 			vrele(vp);
    369 	} else {
    370 		vp->v_mountedhere = (struct mount *)0;
    371 		vfs->vfs_refcount--;
    372 		vfs_unbusy(mp);
    373 		free(mp, M_MOUNT);
    374 		vput(vp);
    375 	}
    376 	return (error);
    377 }
    378 
    379 /*
    380  * Scan all active processes to see if any of them have a current
    381  * or root directory onto which the new filesystem has just been
    382  * mounted. If so, replace them with the new mount point.
    383  */
    384 void
    385 checkdirs(olddp)
    386 	struct vnode *olddp;
    387 {
    388 	struct cwdinfo *cwdi;
    389 	struct vnode *newdp;
    390 	struct proc *p;
    391 
    392 	if (olddp->v_usecount == 1)
    393 		return;
    394 	if (VFS_ROOT(olddp->v_mountedhere, &newdp))
    395 		panic("mount: lost mount");
    396 	proclist_lock_read();
    397 	LIST_FOREACH(p, &allproc, p_list) {
    398 		cwdi = p->p_cwdi;
    399 		if (cwdi->cwdi_cdir == olddp) {
    400 			vrele(cwdi->cwdi_cdir);
    401 			VREF(newdp);
    402 			cwdi->cwdi_cdir = newdp;
    403 		}
    404 		if (cwdi->cwdi_rdir == olddp) {
    405 			vrele(cwdi->cwdi_rdir);
    406 			VREF(newdp);
    407 			cwdi->cwdi_rdir = newdp;
    408 		}
    409 	}
    410 	proclist_unlock_read();
    411 	if (rootvnode == olddp) {
    412 		vrele(rootvnode);
    413 		VREF(newdp);
    414 		rootvnode = newdp;
    415 	}
    416 	vput(newdp);
    417 }
    418 
    419 /*
    420  * Unmount a file system.
    421  *
    422  * Note: unmount takes a path to the vnode mounted on as argument,
    423  * not special file (as before).
    424  */
    425 /* ARGSUSED */
    426 int
    427 sys_unmount(l, v, retval)
    428 	struct lwp *l;
    429 	void *v;
    430 	register_t *retval;
    431 {
    432 	struct sys_unmount_args /* {
    433 		syscallarg(const char *) path;
    434 		syscallarg(int) flags;
    435 	} */ *uap = v;
    436 	struct proc *p = l->l_proc;
    437 	struct vnode *vp;
    438 	struct mount *mp;
    439 	int error;
    440 	struct nameidata nd;
    441 
    442 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
    443 	    SCARG(uap, path), p);
    444 	if ((error = namei(&nd)) != 0)
    445 		return (error);
    446 	vp = nd.ni_vp;
    447 	mp = vp->v_mount;
    448 
    449 	/*
    450 	 * Only root, or the user that did the original mount is
    451 	 * permitted to unmount this filesystem.
    452 	 */
    453 	if ((mp->mnt_stat.f_owner != p->p_ucred->cr_uid) &&
    454 	    (error = suser(p->p_ucred, &p->p_acflag)) != 0) {
    455 		vput(vp);
    456 		return (error);
    457 	}
    458 
    459 	/*
    460 	 * Don't allow unmounting the root file system.
    461 	 */
    462 	if (mp->mnt_flag & MNT_ROOTFS) {
    463 		vput(vp);
    464 		return (EINVAL);
    465 	}
    466 
    467 	/*
    468 	 * Must be the root of the filesystem
    469 	 */
    470 	if ((vp->v_flag & VROOT) == 0) {
    471 		vput(vp);
    472 		return (EINVAL);
    473 	}
    474 	vput(vp);
    475 
    476 	/*
    477 	 * XXX Freeze syncer.  Must do this before locking the
    478 	 * mount point.  See dounmount() for details.
    479 	 */
    480 	lockmgr(&syncer_lock, LK_EXCLUSIVE, NULL);
    481 
    482 	if (vfs_busy(mp, 0, 0)) {
    483 		lockmgr(&syncer_lock, LK_RELEASE, NULL);
    484 		return (EBUSY);
    485 	}
    486 
    487 	return (dounmount(mp, SCARG(uap, flags), p));
    488 }
    489 
    490 /*
    491  * Do the actual file system unmount. File system is assumed to have been
    492  * marked busy by the caller.
    493  */
    494 int
    495 dounmount(mp, flags, p)
    496 	struct mount *mp;
    497 	int flags;
    498 	struct proc *p;
    499 {
    500 	struct vnode *coveredvp;
    501 	int error;
    502 	int async;
    503 	int used_syncer;
    504 
    505 	simple_lock(&mountlist_slock);
    506 	vfs_unbusy(mp);
    507 	used_syncer = (mp->mnt_syncer != NULL);
    508 
    509 	/*
    510 	 * XXX Syncer must be frozen when we get here.  This should really
    511 	 * be done on a per-mountpoint basis, but especially the softdep
    512 	 * code possibly called from the syncer doens't exactly work on a
    513 	 * per-mountpoint basis, so the softdep code would become a maze
    514 	 * of vfs_busy() calls.
    515 	 *
    516 	 * The caller of dounmount() must acquire syncer_lock because
    517 	 * the syncer itself acquires locks in syncer_lock -> vfs_busy
    518 	 * order, and we must preserve that order to avoid deadlock.
    519 	 *
    520 	 * So, if the file system did not use the syncer, now is
    521 	 * the time to release the syncer_lock.
    522 	 */
    523 	if (used_syncer == 0)
    524 		lockmgr(&syncer_lock, LK_RELEASE, NULL);
    525 
    526 	mp->mnt_iflag |= IMNT_UNMOUNT;
    527 	mp->mnt_unmounter = p;
    528 	lockmgr(&mp->mnt_lock, LK_DRAIN | LK_INTERLOCK, &mountlist_slock);
    529 	if (mp->mnt_flag & MNT_EXPUBLIC)
    530 		vfs_setpublicfs(NULL, NULL, NULL);
    531 	async = mp->mnt_flag & MNT_ASYNC;
    532 	mp->mnt_flag &= ~MNT_ASYNC;
    533 	cache_purgevfs(mp);	/* remove cache entries for this file sys */
    534 	if (mp->mnt_syncer != NULL)
    535 		vfs_deallocate_syncvnode(mp);
    536 	if (((mp->mnt_flag & MNT_RDONLY) ||
    537 	    (error = VFS_SYNC(mp, MNT_WAIT, p->p_ucred, p)) == 0) ||
    538 	    (flags & MNT_FORCE))
    539 		error = VFS_UNMOUNT(mp, flags, p);
    540 	simple_lock(&mountlist_slock);
    541 	if (error) {
    542 		if ((mp->mnt_flag & (MNT_RDONLY | MNT_ASYNC)) == 0)
    543 			(void) vfs_allocate_syncvnode(mp);
    544 		mp->mnt_iflag &= ~IMNT_UNMOUNT;
    545 		mp->mnt_unmounter = NULL;
    546 		mp->mnt_flag |= async;
    547 		lockmgr(&mp->mnt_lock, LK_RELEASE | LK_INTERLOCK | LK_REENABLE,
    548 		    &mountlist_slock);
    549 		if (used_syncer)
    550 			lockmgr(&syncer_lock, LK_RELEASE, NULL);
    551 		while (mp->mnt_wcnt > 0) {
    552 			wakeup(mp);
    553 			tsleep(&mp->mnt_wcnt, PVFS, "mntwcnt1", 0);
    554 		}
    555 		return (error);
    556 	}
    557 	CIRCLEQ_REMOVE(&mountlist, mp, mnt_list);
    558 	if ((coveredvp = mp->mnt_vnodecovered) != NULLVP) {
    559 		coveredvp->v_mountedhere = NULL;
    560 		vrele(coveredvp);
    561 	}
    562 	mp->mnt_op->vfs_refcount--;
    563 	if (LIST_FIRST(&mp->mnt_vnodelist) != NULL)
    564 		panic("unmount: dangling vnode");
    565 	mp->mnt_iflag |= IMNT_GONE;
    566 	lockmgr(&mp->mnt_lock, LK_RELEASE | LK_INTERLOCK, &mountlist_slock);
    567 	if (used_syncer)
    568 		lockmgr(&syncer_lock, LK_RELEASE, NULL);
    569 	while(mp->mnt_wcnt > 0) {
    570 		wakeup(mp);
    571 		tsleep(&mp->mnt_wcnt, PVFS, "mntwcnt2", 0);
    572 	}
    573 	free(mp, M_MOUNT);
    574 	return (0);
    575 }
    576 
    577 /*
    578  * Sync each mounted filesystem.
    579  */
    580 #ifdef DEBUG
    581 int syncprt = 0;
    582 struct ctldebug debug0 = { "syncprt", &syncprt };
    583 #endif
    584 
    585 /* ARGSUSED */
    586 int
    587 sys_sync(l, v, retval)
    588 	struct lwp *l;
    589 	void *v;
    590 	register_t *retval;
    591 {
    592 	struct mount *mp, *nmp;
    593 	int asyncflag;
    594 	struct proc *p = l == NULL ? &proc0 : l->l_proc;
    595 
    596 	simple_lock(&mountlist_slock);
    597 	for (mp = mountlist.cqh_last; mp != (void *)&mountlist; mp = nmp) {
    598 		if (vfs_busy(mp, LK_NOWAIT, &mountlist_slock)) {
    599 			nmp = mp->mnt_list.cqe_prev;
    600 			continue;
    601 		}
    602 		if ((mp->mnt_flag & MNT_RDONLY) == 0) {
    603 			asyncflag = mp->mnt_flag & MNT_ASYNC;
    604 			mp->mnt_flag &= ~MNT_ASYNC;
    605 			VFS_SYNC(mp, MNT_NOWAIT, p->p_ucred, p);
    606 			if (asyncflag)
    607 				 mp->mnt_flag |= MNT_ASYNC;
    608 		}
    609 		simple_lock(&mountlist_slock);
    610 		nmp = mp->mnt_list.cqe_prev;
    611 		vfs_unbusy(mp);
    612 
    613 	}
    614 	simple_unlock(&mountlist_slock);
    615 #ifdef DEBUG
    616 	if (syncprt)
    617 		vfs_bufstats();
    618 #endif /* DEBUG */
    619 	return (0);
    620 }
    621 
    622 /*
    623  * Change filesystem quotas.
    624  */
    625 /* ARGSUSED */
    626 int
    627 sys_quotactl(l, v, retval)
    628 	struct lwp *l;
    629 	void *v;
    630 	register_t *retval;
    631 {
    632 	struct sys_quotactl_args /* {
    633 		syscallarg(const char *) path;
    634 		syscallarg(int) cmd;
    635 		syscallarg(int) uid;
    636 		syscallarg(caddr_t) arg;
    637 	} */ *uap = v;
    638 	struct proc *p = l->l_proc;
    639 	struct mount *mp;
    640 	int error;
    641 	struct nameidata nd;
    642 
    643 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
    644 	if ((error = namei(&nd)) != 0)
    645 		return (error);
    646 	mp = nd.ni_vp->v_mount;
    647 	vrele(nd.ni_vp);
    648 	return (VFS_QUOTACTL(mp, SCARG(uap, cmd), SCARG(uap, uid),
    649 	    SCARG(uap, arg), p));
    650 }
    651 
    652 static int
    653 dostatfs(struct mount *mp, struct statfs *sp, struct proc *p, int flags,
    654     int root)
    655 {
    656 	struct cwdinfo *cwdi = p->p_cwdi;
    657 	int error = 0;
    658 
    659 	/*
    660 	 * If MNT_NOWAIT or MNT_LAZY is specified, do not
    661 	 * refresh the fsstat cache. MNT_WAIT or MNT_LAXY
    662 	 * overrides MNT_NOWAIT.
    663 	 */
    664 	if (flags == MNT_NOWAIT	|| flags == MNT_LAZY ||
    665 	    (flags != MNT_WAIT && flags != 0)) {
    666 		memcpy(sp, &mp->mnt_stat, sizeof(*sp));
    667 		goto done;
    668 	}
    669 
    670 	if ((error = VFS_STATFS(mp, sp, p)) != 0) {
    671 		return error;
    672 	}
    673 
    674 	if (cwdi->cwdi_rdir == NULL)
    675 		(void)memcpy(&mp->mnt_stat, sp, sizeof(mp->mnt_stat));
    676 done:
    677 	if (cwdi->cwdi_rdir != NULL) {
    678 		size_t len;
    679 		char *bp;
    680 		char *path = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
    681 		if (!path)
    682 			return ENOMEM;
    683 
    684 		bp = path + MAXPATHLEN;
    685 		*--bp = '\0';
    686 		error = getcwd_common(cwdi->cwdi_rdir, rootvnode, &bp, path,
    687 		    MAXPATHLEN / 2, 0, p);
    688 		if (error) {
    689 			free(path, M_TEMP);
    690 			return error;
    691 		}
    692 		len = strlen(bp);
    693 		/*
    694 		 * for mount points that are below our root, we can see
    695 		 * them, so we fix up the pathname and return them. The
    696 		 * rest we cannot see, so we don't allow viewing the
    697 		 * data.
    698 		 */
    699 		if (strncmp(bp, sp->f_mntonname, len) == 0) {
    700 			strlcpy(sp->f_mntonname, &sp->f_mntonname[len],
    701 			    sizeof(sp->f_mntonname));
    702 			if (sp->f_mntonname[0] == '\0')
    703 				(void)strlcpy(sp->f_mntonname, "/",
    704 				    sizeof(sp->f_mntonname));
    705 		} else {
    706 			if (root)
    707 				(void)strlcpy(sp->f_mntonname, "/",
    708 				    sizeof(sp->f_mntonname));
    709 			else
    710 				error = EPERM;
    711 		}
    712 		free(path, M_TEMP);
    713 	}
    714 	sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
    715 	sp->f_oflags = sp->f_flags & 0xffff;
    716 	return error;
    717 }
    718 
    719 /*
    720  * Get filesystem statistics.
    721  */
    722 /* ARGSUSED */
    723 int
    724 sys_statfs(l, v, retval)
    725 	struct lwp *l;
    726 	void *v;
    727 	register_t *retval;
    728 {
    729 	struct sys_statfs_args /* {
    730 		syscallarg(const char *) path;
    731 		syscallarg(struct statfs *) buf;
    732 	} */ *uap = v;
    733 	struct proc *p = l->l_proc;
    734 	struct mount *mp;
    735 	struct statfs sbuf;
    736 	int error;
    737 	struct nameidata nd;
    738 
    739 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
    740 	if ((error = namei(&nd)) != 0)
    741 		return error;
    742 	mp = nd.ni_vp->v_mount;
    743 	vrele(nd.ni_vp);
    744 	if ((error = dostatfs(mp, &sbuf, p, 0, 1)) != 0)
    745 		return error;
    746 	return copyout(&sbuf, SCARG(uap, buf), sizeof(sbuf));
    747 }
    748 
    749 /*
    750  * Get filesystem statistics.
    751  */
    752 /* ARGSUSED */
    753 int
    754 sys_fstatfs(l, v, retval)
    755 	struct lwp *l;
    756 	void *v;
    757 	register_t *retval;
    758 {
    759 	struct sys_fstatfs_args /* {
    760 		syscallarg(int) fd;
    761 		syscallarg(struct statfs *) buf;
    762 	} */ *uap = v;
    763 	struct proc *p = l->l_proc;
    764 	struct file *fp;
    765 	struct mount *mp;
    766 	struct statfs sbuf;
    767 	int error;
    768 
    769 	/* getvnode() will use the descriptor for us */
    770 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
    771 		return (error);
    772 	mp = ((struct vnode *)fp->f_data)->v_mount;
    773 	if ((error = dostatfs(mp, &sbuf, p, 0, 1)) != 0)
    774 		goto out;
    775 	error = copyout(&sbuf, SCARG(uap, buf), sizeof(sbuf));
    776  out:
    777 	FILE_UNUSE(fp, p);
    778 	return error;
    779 }
    780 
    781 
    782 /*
    783  * Get statistics on all filesystems.
    784  */
    785 int
    786 sys_getfsstat(l, v, retval)
    787 	struct lwp *l;
    788 	void *v;
    789 	register_t *retval;
    790 {
    791 	struct sys_getfsstat_args /* {
    792 		syscallarg(struct statfs *) buf;
    793 		syscallarg(long) bufsize;
    794 		syscallarg(int) flags;
    795 	} */ *uap = v;
    796 	int root = 0;
    797 	struct proc *p = l->l_proc;
    798 	struct mount *mp, *nmp;
    799 	struct statfs sbuf;
    800 	caddr_t sfsp;
    801 	long count, maxcount, error = 0;
    802 
    803 	maxcount = SCARG(uap, bufsize) / sizeof(struct statfs);
    804 	sfsp = (caddr_t)SCARG(uap, buf);
    805 	simple_lock(&mountlist_slock);
    806 	count = 0;
    807 	for (mp = CIRCLEQ_FIRST(&mountlist); mp != (void *)&mountlist;
    808 	     mp = nmp) {
    809 		if (vfs_busy(mp, LK_NOWAIT, &mountlist_slock)) {
    810 			nmp = CIRCLEQ_NEXT(mp, mnt_list);
    811 			continue;
    812 		}
    813 		if (sfsp && count < maxcount) {
    814 			error = dostatfs(mp, &sbuf, p, SCARG(uap, flags), 0);
    815 			if (error) {
    816 				simple_lock(&mountlist_slock);
    817 				nmp = CIRCLEQ_NEXT(mp, mnt_list);
    818 				vfs_unbusy(mp);
    819 				continue;
    820 			}
    821 			error = copyout(&sbuf, sfsp, sizeof(sbuf));
    822 			if (error) {
    823 				vfs_unbusy(mp);
    824 				return (error);
    825 			}
    826 			sfsp += sizeof(sbuf);
    827 			root |= strcmp(sbuf.f_mntonname, "/") == 0;
    828 		}
    829 		count++;
    830 		simple_lock(&mountlist_slock);
    831 		nmp = CIRCLEQ_NEXT(mp, mnt_list);
    832 		vfs_unbusy(mp);
    833 	}
    834 	simple_unlock(&mountlist_slock);
    835 	if (root == 0 && p->p_cwdi->cwdi_rdir) {
    836 		/*
    837 		 * fake a root entry
    838 		 */
    839 		if ((error = dostatfs(p->p_cwdi->cwdi_rdir->v_mount, &sbuf, p,
    840 		    SCARG(uap, flags), 1)) != 0)
    841 			return error;
    842 		if (sfsp)
    843 			error = copyout(&sbuf, sfsp, sizeof(sbuf));
    844 		count++;
    845 	}
    846 	if (sfsp && count > maxcount)
    847 		*retval = maxcount;
    848 	else
    849 		*retval = count;
    850 	return error;
    851 }
    852 
    853 /*
    854  * Change current working directory to a given file descriptor.
    855  */
    856 /* ARGSUSED */
    857 int
    858 sys_fchdir(l, v, retval)
    859 	struct lwp *l;
    860 	void *v;
    861 	register_t *retval;
    862 {
    863 	struct sys_fchdir_args /* {
    864 		syscallarg(int) fd;
    865 	} */ *uap = v;
    866 	struct proc *p = l->l_proc;
    867 	struct filedesc *fdp = p->p_fd;
    868 	struct cwdinfo *cwdi = p->p_cwdi;
    869 	struct vnode *vp, *tdp;
    870 	struct mount *mp;
    871 	struct file *fp;
    872 	int error;
    873 
    874 	/* getvnode() will use the descriptor for us */
    875 	if ((error = getvnode(fdp, SCARG(uap, fd), &fp)) != 0)
    876 		return (error);
    877 	vp = (struct vnode *)fp->f_data;
    878 
    879 	VREF(vp);
    880 	vn_lock(vp,  LK_EXCLUSIVE | LK_RETRY);
    881 	if (vp->v_type != VDIR)
    882 		error = ENOTDIR;
    883 	else
    884 		error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p);
    885 	while (!error && (mp = vp->v_mountedhere) != NULL) {
    886 		if (vfs_busy(mp, 0, 0))
    887 			continue;
    888 		error = VFS_ROOT(mp, &tdp);
    889 		vfs_unbusy(mp);
    890 		if (error)
    891 			break;
    892 		vput(vp);
    893 		vp = tdp;
    894 	}
    895 	if (error) {
    896 		vput(vp);
    897 		goto out;
    898 	}
    899 	VOP_UNLOCK(vp, 0);
    900 
    901 	/*
    902 	 * Disallow changing to a directory not under the process's
    903 	 * current root directory (if there is one).
    904 	 */
    905 	if (cwdi->cwdi_rdir && !vn_isunder(vp, NULL, p)) {
    906 		vrele(vp);
    907 		error = EPERM;	/* operation not permitted */
    908 		goto out;
    909 	}
    910 
    911 	vrele(cwdi->cwdi_cdir);
    912 	cwdi->cwdi_cdir = vp;
    913  out:
    914 	FILE_UNUSE(fp, p);
    915 	return (error);
    916 }
    917 
    918 /*
    919  * Change this process's notion of the root directory to a given file descriptor.
    920  */
    921 
    922 int
    923 sys_fchroot(l, v, retval)
    924 	struct lwp *l;
    925 	void *v;
    926 	register_t *retval;
    927 {
    928 	struct sys_fchroot_args *uap = v;
    929 	struct proc *p = l->l_proc;
    930 	struct filedesc *fdp = p->p_fd;
    931 	struct cwdinfo *cwdi = p->p_cwdi;
    932 	struct vnode	*vp;
    933 	struct file	*fp;
    934 	int		 error;
    935 
    936 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    937 		return error;
    938 	/* getvnode() will use the descriptor for us */
    939 	if ((error = getvnode(fdp, SCARG(uap, fd), &fp)) != 0)
    940 		return error;
    941 	vp = (struct vnode *) fp->f_data;
    942 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    943 	if (vp->v_type != VDIR)
    944 		error = ENOTDIR;
    945 	else
    946 		error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p);
    947 	VOP_UNLOCK(vp, 0);
    948 	if (error)
    949 		goto out;
    950 	VREF(vp);
    951 
    952 	/*
    953 	 * Prevent escaping from chroot by putting the root under
    954 	 * the working directory.  Silently chdir to / if we aren't
    955 	 * already there.
    956 	 */
    957 	if (!vn_isunder(cwdi->cwdi_cdir, vp, p)) {
    958 		/*
    959 		 * XXX would be more failsafe to change directory to a
    960 		 * deadfs node here instead
    961 		 */
    962 		vrele(cwdi->cwdi_cdir);
    963 		VREF(vp);
    964 		cwdi->cwdi_cdir = vp;
    965 	}
    966 
    967 	if (cwdi->cwdi_rdir != NULL)
    968 		vrele(cwdi->cwdi_rdir);
    969 	cwdi->cwdi_rdir = vp;
    970  out:
    971 	FILE_UNUSE(fp, p);
    972 	return (error);
    973 }
    974 
    975 
    976 
    977 /*
    978  * Change current working directory (``.'').
    979  */
    980 /* ARGSUSED */
    981 int
    982 sys_chdir(l, v, retval)
    983 	struct lwp *l;
    984 	void *v;
    985 	register_t *retval;
    986 {
    987 	struct sys_chdir_args /* {
    988 		syscallarg(const char *) path;
    989 	} */ *uap = v;
    990 	struct proc *p = l->l_proc;
    991 	struct cwdinfo *cwdi = p->p_cwdi;
    992 	int error;
    993 	struct nameidata nd;
    994 
    995 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
    996 	    SCARG(uap, path), p);
    997 	if ((error = change_dir(&nd, p)) != 0)
    998 		return (error);
    999 	vrele(cwdi->cwdi_cdir);
   1000 	cwdi->cwdi_cdir = nd.ni_vp;
   1001 	return (0);
   1002 }
   1003 
   1004 /*
   1005  * Change notion of root (``/'') directory.
   1006  */
   1007 /* ARGSUSED */
   1008 int
   1009 sys_chroot(l, v, retval)
   1010 	struct lwp *l;
   1011 	void *v;
   1012 	register_t *retval;
   1013 {
   1014 	struct sys_chroot_args /* {
   1015 		syscallarg(const char *) path;
   1016 	} */ *uap = v;
   1017 	struct proc *p = l->l_proc;
   1018 	struct cwdinfo *cwdi = p->p_cwdi;
   1019 	struct vnode *vp;
   1020 	int error;
   1021 	struct nameidata nd;
   1022 
   1023 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
   1024 		return (error);
   1025 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
   1026 	    SCARG(uap, path), p);
   1027 	if ((error = change_dir(&nd, p)) != 0)
   1028 		return (error);
   1029 	if (cwdi->cwdi_rdir != NULL)
   1030 		vrele(cwdi->cwdi_rdir);
   1031 	vp = nd.ni_vp;
   1032 	cwdi->cwdi_rdir = vp;
   1033 
   1034 	/*
   1035 	 * Prevent escaping from chroot by putting the root under
   1036 	 * the working directory.  Silently chdir to / if we aren't
   1037 	 * already there.
   1038 	 */
   1039 	if (!vn_isunder(cwdi->cwdi_cdir, vp, p)) {
   1040 		/*
   1041 		 * XXX would be more failsafe to change directory to a
   1042 		 * deadfs node here instead
   1043 		 */
   1044 		vrele(cwdi->cwdi_cdir);
   1045 		VREF(vp);
   1046 		cwdi->cwdi_cdir = vp;
   1047 	}
   1048 
   1049 	return (0);
   1050 }
   1051 
   1052 /*
   1053  * Common routine for chroot and chdir.
   1054  */
   1055 static int
   1056 change_dir(ndp, p)
   1057 	struct nameidata *ndp;
   1058 	struct proc *p;
   1059 {
   1060 	struct vnode *vp;
   1061 	int error;
   1062 
   1063 	if ((error = namei(ndp)) != 0)
   1064 		return (error);
   1065 	vp = ndp->ni_vp;
   1066 	if (vp->v_type != VDIR)
   1067 		error = ENOTDIR;
   1068 	else
   1069 		error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p);
   1070 
   1071 	if (error)
   1072 		vput(vp);
   1073 	else
   1074 		VOP_UNLOCK(vp, 0);
   1075 	return (error);
   1076 }
   1077 
   1078 /*
   1079  * Check permissions, allocate an open file structure,
   1080  * and call the device open routine if any.
   1081  */
   1082 int
   1083 sys_open(l, v, retval)
   1084 	struct lwp *l;
   1085 	void *v;
   1086 	register_t *retval;
   1087 {
   1088 	struct sys_open_args /* {
   1089 		syscallarg(const char *) path;
   1090 		syscallarg(int) flags;
   1091 		syscallarg(int) mode;
   1092 	} */ *uap = v;
   1093 	struct proc *p = l->l_proc;
   1094 	struct cwdinfo *cwdi = p->p_cwdi;
   1095 	struct filedesc *fdp = p->p_fd;
   1096 	struct file *fp;
   1097 	struct vnode *vp;
   1098 	int flags, cmode;
   1099 	int type, indx, error;
   1100 	struct flock lf;
   1101 	struct nameidata nd;
   1102 
   1103 	flags = FFLAGS(SCARG(uap, flags));
   1104 	if ((flags & (FREAD | FWRITE)) == 0)
   1105 		return (EINVAL);
   1106 	/* falloc() will use the file descriptor for us */
   1107 	if ((error = falloc(p, &fp, &indx)) != 0)
   1108 		return (error);
   1109 	cmode = ((SCARG(uap, mode) &~ cwdi->cwdi_cmask) & ALLPERMS) &~ S_ISTXT;
   1110 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
   1111 	l->l_dupfd = -indx - 1;			/* XXX check for fdopen */
   1112 	if ((error = vn_open(&nd, flags, cmode)) != 0) {
   1113 		FILE_UNUSE(fp, p);
   1114 		ffree(fp);
   1115 		if ((error == ENODEV || error == ENXIO) &&
   1116 		    l->l_dupfd >= 0 &&			/* XXX from fdopen */
   1117 		    (error =
   1118 			dupfdopen(p, indx, l->l_dupfd, flags, error)) == 0) {
   1119 			*retval = indx;
   1120 			return (0);
   1121 		}
   1122 		if (error == ERESTART)
   1123 			error = EINTR;
   1124 		fdremove(fdp, indx);
   1125 		return (error);
   1126 	}
   1127 	l->l_dupfd = 0;
   1128 	vp = nd.ni_vp;
   1129 	fp->f_flag = flags & FMASK;
   1130 	fp->f_type = DTYPE_VNODE;
   1131 	fp->f_ops = &vnops;
   1132 	fp->f_data = vp;
   1133 	if (flags & (O_EXLOCK | O_SHLOCK)) {
   1134 		lf.l_whence = SEEK_SET;
   1135 		lf.l_start = 0;
   1136 		lf.l_len = 0;
   1137 		if (flags & O_EXLOCK)
   1138 			lf.l_type = F_WRLCK;
   1139 		else
   1140 			lf.l_type = F_RDLCK;
   1141 		type = F_FLOCK;
   1142 		if ((flags & FNONBLOCK) == 0)
   1143 			type |= F_WAIT;
   1144 		VOP_UNLOCK(vp, 0);
   1145 		error = VOP_ADVLOCK(vp, fp, F_SETLK, &lf, type);
   1146 		if (error) {
   1147 			(void) vn_close(vp, fp->f_flag, fp->f_cred, p);
   1148 			FILE_UNUSE(fp, p);
   1149 			ffree(fp);
   1150 			fdremove(fdp, indx);
   1151 			return (error);
   1152 		}
   1153 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   1154 		fp->f_flag |= FHASLOCK;
   1155 	}
   1156 	VOP_UNLOCK(vp, 0);
   1157 	*retval = indx;
   1158 	FILE_SET_MATURE(fp);
   1159 	FILE_UNUSE(fp, p);
   1160 	return (0);
   1161 }
   1162 
   1163 /*
   1164  * Get file handle system call
   1165  */
   1166 int
   1167 sys_getfh(l, v, retval)
   1168 	struct lwp *l;
   1169 	void *v;
   1170 	register_t *retval;
   1171 {
   1172 	struct sys_getfh_args /* {
   1173 		syscallarg(char *) fname;
   1174 		syscallarg(fhandle_t *) fhp;
   1175 	} */ *uap = v;
   1176 	struct proc *p = l->l_proc;
   1177 	struct vnode *vp;
   1178 	fhandle_t fh;
   1179 	int error;
   1180 	struct nameidata nd;
   1181 
   1182 	/*
   1183 	 * Must be super user
   1184 	 */
   1185 	error = suser(p->p_ucred, &p->p_acflag);
   1186 	if (error)
   1187 		return (error);
   1188 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
   1189 	    SCARG(uap, fname), p);
   1190 	error = namei(&nd);
   1191 	if (error)
   1192 		return (error);
   1193 	vp = nd.ni_vp;
   1194 	memset(&fh, 0, sizeof(fh));
   1195 	fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
   1196 	error = VFS_VPTOFH(vp, &fh.fh_fid);
   1197 	vput(vp);
   1198 	if (error)
   1199 		return (error);
   1200 	error = copyout(&fh, (caddr_t)SCARG(uap, fhp), sizeof (fh));
   1201 	return (error);
   1202 }
   1203 
   1204 /*
   1205  * Open a file given a file handle.
   1206  *
   1207  * Check permissions, allocate an open file structure,
   1208  * and call the device open routine if any.
   1209  */
   1210 int
   1211 sys_fhopen(l, v, retval)
   1212 	struct lwp *l;
   1213 	void *v;
   1214 	register_t *retval;
   1215 {
   1216 	struct sys_fhopen_args /* {
   1217 		syscallarg(const fhandle_t *) fhp;
   1218 		syscallarg(int) flags;
   1219 	} */ *uap = v;
   1220 	struct proc *p = l->l_proc;
   1221 	struct filedesc *fdp = p->p_fd;
   1222 	struct file *fp;
   1223 	struct vnode *vp = NULL;
   1224 	struct mount *mp;
   1225 	struct ucred *cred = p->p_ucred;
   1226 	int flags;
   1227 	struct file *nfp;
   1228 	int type, indx, error=0;
   1229 	struct flock lf;
   1230 	struct vattr va;
   1231 	fhandle_t fh;
   1232 
   1233 	/*
   1234 	 * Must be super user
   1235 	 */
   1236 	if ((error = suser(p->p_ucred, &p->p_acflag)))
   1237 		return (error);
   1238 
   1239 	flags = FFLAGS(SCARG(uap, flags));
   1240 	if ((flags & (FREAD | FWRITE)) == 0)
   1241 		return (EINVAL);
   1242 	if ((flags & O_CREAT))
   1243 		return (EINVAL);
   1244 	/* falloc() will use the file descriptor for us */
   1245 	if ((error = falloc(p, &nfp, &indx)) != 0)
   1246 		return (error);
   1247 	fp = nfp;
   1248 	if ((error = copyin(SCARG(uap, fhp), &fh, sizeof(fhandle_t))) != 0)
   1249 		goto bad;
   1250 
   1251 	if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL) {
   1252 		error = ESTALE;
   1253 		goto bad;
   1254 	}
   1255 
   1256 	if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)) != 0) {
   1257 		vp = NULL;	/* most likely unnecessary sanity for bad: */
   1258 		goto bad;
   1259 	}
   1260 
   1261 	/* Now do an effective vn_open */
   1262 
   1263 	if (vp->v_type == VSOCK) {
   1264 		error = EOPNOTSUPP;
   1265 		goto bad;
   1266 	}
   1267 	if (flags & FREAD) {
   1268 		if ((error = VOP_ACCESS(vp, VREAD, cred, p)) != 0)
   1269 			goto bad;
   1270 	}
   1271 	if (flags & (FWRITE | O_TRUNC)) {
   1272 		if (vp->v_type == VDIR) {
   1273 			error = EISDIR;
   1274 			goto bad;
   1275 		}
   1276 		if ((error = vn_writechk(vp)) != 0 ||
   1277 		    (error = VOP_ACCESS(vp, VWRITE, cred, p)) != 0)
   1278 			goto bad;
   1279 	}
   1280 	if (flags & O_TRUNC) {
   1281 		VOP_UNLOCK(vp, 0);			/* XXX */
   1282 		VOP_LEASE(vp, p, cred, LEASE_WRITE);
   1283 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);   /* XXX */
   1284 		VATTR_NULL(&va);
   1285 		va.va_size = 0;
   1286 		if ((error = VOP_SETATTR(vp, &va, cred, p)) != 0)
   1287 			goto bad;
   1288 	}
   1289 	if ((error = VOP_OPEN(vp, flags, cred, p)) != 0)
   1290 		goto bad;
   1291 	if (vp->v_type == VREG &&
   1292 	    uvn_attach(vp, flags & FWRITE ? VM_PROT_WRITE : 0) == NULL) {
   1293 		error = EIO;
   1294 		goto bad;
   1295 	}
   1296 	if (flags & FWRITE)
   1297 		vp->v_writecount++;
   1298 
   1299 	/* done with modified vn_open, now finish what sys_open does. */
   1300 
   1301 	fp->f_flag = flags & FMASK;
   1302 	fp->f_type = DTYPE_VNODE;
   1303 	fp->f_ops = &vnops;
   1304 	fp->f_data = vp;
   1305 	if (flags & (O_EXLOCK | O_SHLOCK)) {
   1306 		lf.l_whence = SEEK_SET;
   1307 		lf.l_start = 0;
   1308 		lf.l_len = 0;
   1309 		if (flags & O_EXLOCK)
   1310 			lf.l_type = F_WRLCK;
   1311 		else
   1312 			lf.l_type = F_RDLCK;
   1313 		type = F_FLOCK;
   1314 		if ((flags & FNONBLOCK) == 0)
   1315 			type |= F_WAIT;
   1316 		VOP_UNLOCK(vp, 0);
   1317 		error = VOP_ADVLOCK(vp, fp, F_SETLK, &lf, type);
   1318 		if (error) {
   1319 			(void) vn_close(vp, fp->f_flag, fp->f_cred, p);
   1320 			FILE_UNUSE(fp, p);
   1321 			ffree(fp);
   1322 			fdremove(fdp, indx);
   1323 			return (error);
   1324 		}
   1325 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   1326 		fp->f_flag |= FHASLOCK;
   1327 	}
   1328 	VOP_UNLOCK(vp, 0);
   1329 	*retval = indx;
   1330 	FILE_SET_MATURE(fp);
   1331 	FILE_UNUSE(fp, p);
   1332 	return (0);
   1333 
   1334 bad:
   1335 	FILE_UNUSE(fp, p);
   1336 	ffree(fp);
   1337 	fdremove(fdp, indx);
   1338 	if (vp != NULL)
   1339 		vput(vp);
   1340 	return (error);
   1341 }
   1342 
   1343 /* ARGSUSED */
   1344 int
   1345 sys_fhstat(l, v, retval)
   1346 	struct lwp *l;
   1347 	void *v;
   1348 	register_t *retval;
   1349 {
   1350 	struct sys_fhstat_args /* {
   1351 		syscallarg(const fhandle_t *) fhp;
   1352 		syscallarg(struct stat *) sb;
   1353 	} */ *uap = v;
   1354 	struct proc *p = l->l_proc;
   1355 	struct stat sb;
   1356 	int error;
   1357 	fhandle_t fh;
   1358 	struct mount *mp;
   1359 	struct vnode *vp;
   1360 
   1361 	/*
   1362 	 * Must be super user
   1363 	 */
   1364 	if ((error = suser(p->p_ucred, &p->p_acflag)))
   1365 		return (error);
   1366 
   1367 	if ((error = copyin(SCARG(uap, fhp), &fh, sizeof(fhandle_t))) != 0)
   1368 		return (error);
   1369 
   1370 	if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
   1371 		return (ESTALE);
   1372 	if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)))
   1373 		return (error);
   1374 	error = vn_stat(vp, &sb, p);
   1375 	vput(vp);
   1376 	if (error)
   1377 		return (error);
   1378 	error = copyout(&sb, SCARG(uap, sb), sizeof(sb));
   1379 	return (error);
   1380 }
   1381 
   1382 /* ARGSUSED */
   1383 int
   1384 sys_fhstatfs(l, v, retval)
   1385 	struct lwp *l;
   1386 	void *v;
   1387 	register_t *retval;
   1388 {
   1389 	struct sys_fhstatfs_args /*
   1390 		syscallarg(const fhandle_t *) fhp;
   1391 		syscallarg(struct statfs *) buf;
   1392 	} */ *uap = v;
   1393 	struct proc *p = l->l_proc;
   1394 	struct statfs sbuf;
   1395 	fhandle_t fh;
   1396 	struct mount *mp;
   1397 	struct vnode *vp;
   1398 	int error;
   1399 
   1400 	/*
   1401 	 * Must be super user
   1402 	 */
   1403 	if ((error = suser(p->p_ucred, &p->p_acflag)))
   1404 		return (error);
   1405 
   1406 	if ((error = copyin(SCARG(uap, fhp), &fh, sizeof(fhandle_t))) != 0)
   1407 		return (error);
   1408 
   1409 	if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
   1410 		return (ESTALE);
   1411 	if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)))
   1412 		return (error);
   1413 	mp = vp->v_mount;
   1414 	vput(vp);
   1415 	if ((error = VFS_STATFS(mp, &sbuf, p)) != 0)
   1416 		return (error);
   1417 	return (copyout(&sbuf, SCARG(uap, buf), sizeof(sbuf)));
   1418 }
   1419 
   1420 /*
   1421  * Create a special file.
   1422  */
   1423 /* ARGSUSED */
   1424 int
   1425 sys_mknod(l, v, retval)
   1426 	struct lwp *l;
   1427 	void *v;
   1428 	register_t *retval;
   1429 {
   1430 	struct sys_mknod_args /* {
   1431 		syscallarg(const char *) path;
   1432 		syscallarg(int) mode;
   1433 		syscallarg(int) dev;
   1434 	} */ *uap = v;
   1435 	struct proc *p = l->l_proc;
   1436 	struct vnode *vp;
   1437 	struct vattr vattr;
   1438 	int error;
   1439 	int whiteout = 0;
   1440 	struct nameidata nd;
   1441 
   1442 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
   1443 		return (error);
   1444 	NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, path), p);
   1445 	if ((error = namei(&nd)) != 0)
   1446 		return (error);
   1447 	vp = nd.ni_vp;
   1448 	if (vp != NULL)
   1449 		error = EEXIST;
   1450 	else {
   1451 		VATTR_NULL(&vattr);
   1452 		vattr.va_mode =
   1453 		    (SCARG(uap, mode) & ALLPERMS) &~ p->p_cwdi->cwdi_cmask;
   1454 		vattr.va_rdev = SCARG(uap, dev);
   1455 		whiteout = 0;
   1456 
   1457 		switch (SCARG(uap, mode) & S_IFMT) {
   1458 		case S_IFMT:	/* used by badsect to flag bad sectors */
   1459 			vattr.va_type = VBAD;
   1460 			break;
   1461 		case S_IFCHR:
   1462 			vattr.va_type = VCHR;
   1463 			break;
   1464 		case S_IFBLK:
   1465 			vattr.va_type = VBLK;
   1466 			break;
   1467 		case S_IFWHT:
   1468 			whiteout = 1;
   1469 			break;
   1470 		default:
   1471 			error = EINVAL;
   1472 			break;
   1473 		}
   1474 	}
   1475 	if (!error) {
   1476 		VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
   1477 		if (whiteout) {
   1478 			error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, CREATE);
   1479 			if (error)
   1480 				VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   1481 			vput(nd.ni_dvp);
   1482 		} else {
   1483 			error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp,
   1484 						&nd.ni_cnd, &vattr);
   1485 			if (error == 0)
   1486 				vput(nd.ni_vp);
   1487 		}
   1488 	} else {
   1489 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   1490 		if (nd.ni_dvp == vp)
   1491 			vrele(nd.ni_dvp);
   1492 		else
   1493 			vput(nd.ni_dvp);
   1494 		if (vp)
   1495 			vrele(vp);
   1496 	}
   1497 	return (error);
   1498 }
   1499 
   1500 /*
   1501  * Create a named pipe.
   1502  */
   1503 /* ARGSUSED */
   1504 int
   1505 sys_mkfifo(l, v, retval)
   1506 	struct lwp *l;
   1507 	void *v;
   1508 	register_t *retval;
   1509 {
   1510 	struct sys_mkfifo_args /* {
   1511 		syscallarg(const char *) path;
   1512 		syscallarg(int) mode;
   1513 	} */ *uap = v;
   1514 	struct proc *p = l->l_proc;
   1515 	struct vattr vattr;
   1516 	int error;
   1517 	struct nameidata nd;
   1518 
   1519 	NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, path), p);
   1520 	if ((error = namei(&nd)) != 0)
   1521 		return (error);
   1522 	if (nd.ni_vp != NULL) {
   1523 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   1524 		if (nd.ni_dvp == nd.ni_vp)
   1525 			vrele(nd.ni_dvp);
   1526 		else
   1527 			vput(nd.ni_dvp);
   1528 		vrele(nd.ni_vp);
   1529 		return (EEXIST);
   1530 	}
   1531 	VATTR_NULL(&vattr);
   1532 	vattr.va_type = VFIFO;
   1533 	vattr.va_mode = (SCARG(uap, mode) & ALLPERMS) &~ p->p_cwdi->cwdi_cmask;
   1534 	VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
   1535 	error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
   1536 	if (error == 0)
   1537 		vput(nd.ni_vp);
   1538 	return (error);
   1539 }
   1540 
   1541 /*
   1542  * Make a hard file link.
   1543  */
   1544 /* ARGSUSED */
   1545 int
   1546 sys_link(l, v, retval)
   1547 	struct lwp *l;
   1548 	void *v;
   1549 	register_t *retval;
   1550 {
   1551 	struct sys_link_args /* {
   1552 		syscallarg(const char *) path;
   1553 		syscallarg(const char *) link;
   1554 	} */ *uap = v;
   1555 	struct proc *p = l->l_proc;
   1556 	struct vnode *vp;
   1557 	struct nameidata nd;
   1558 	int error;
   1559 
   1560 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
   1561 	if ((error = namei(&nd)) != 0)
   1562 		return (error);
   1563 	vp = nd.ni_vp;
   1564 	NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, link), p);
   1565 	if ((error = namei(&nd)) != 0)
   1566 		goto out;
   1567 	if (nd.ni_vp) {
   1568 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   1569 		if (nd.ni_dvp == nd.ni_vp)
   1570 			vrele(nd.ni_dvp);
   1571 		else
   1572 			vput(nd.ni_dvp);
   1573 		vrele(nd.ni_vp);
   1574 		error = EEXIST;
   1575 		goto out;
   1576 	}
   1577 	VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
   1578 	VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
   1579 	error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
   1580 out:
   1581 	vrele(vp);
   1582 	return (error);
   1583 }
   1584 
   1585 /*
   1586  * Make a symbolic link.
   1587  */
   1588 /* ARGSUSED */
   1589 int
   1590 sys_symlink(l, v, retval)
   1591 	struct lwp *l;
   1592 	void *v;
   1593 	register_t *retval;
   1594 {
   1595 	struct sys_symlink_args /* {
   1596 		syscallarg(const char *) path;
   1597 		syscallarg(const char *) link;
   1598 	} */ *uap = v;
   1599 	struct proc *p = l->l_proc;
   1600 	struct vattr vattr;
   1601 	char *path;
   1602 	int error;
   1603 	struct nameidata nd;
   1604 
   1605 	path = PNBUF_GET();
   1606 	error = copyinstr(SCARG(uap, path), path, MAXPATHLEN, NULL);
   1607 	if (error)
   1608 		goto out;
   1609 	NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, link), p);
   1610 	if ((error = namei(&nd)) != 0)
   1611 		goto out;
   1612 	if (nd.ni_vp) {
   1613 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   1614 		if (nd.ni_dvp == nd.ni_vp)
   1615 			vrele(nd.ni_dvp);
   1616 		else
   1617 			vput(nd.ni_dvp);
   1618 		vrele(nd.ni_vp);
   1619 		error = EEXIST;
   1620 		goto out;
   1621 	}
   1622 	VATTR_NULL(&vattr);
   1623 	vattr.va_mode = ACCESSPERMS &~ p->p_cwdi->cwdi_cmask;
   1624 	VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
   1625 	error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr, path);
   1626 	if (error == 0)
   1627 		vput(nd.ni_vp);
   1628 out:
   1629 	PNBUF_PUT(path);
   1630 	return (error);
   1631 }
   1632 
   1633 /*
   1634  * Delete a whiteout from the filesystem.
   1635  */
   1636 /* ARGSUSED */
   1637 int
   1638 sys_undelete(l, v, retval)
   1639 	struct lwp *l;
   1640 	void *v;
   1641 	register_t *retval;
   1642 {
   1643 	struct sys_undelete_args /* {
   1644 		syscallarg(const char *) path;
   1645 	} */ *uap = v;
   1646 	struct proc *p = l->l_proc;
   1647 	int error;
   1648 	struct nameidata nd;
   1649 
   1650 	NDINIT(&nd, DELETE, LOCKPARENT|DOWHITEOUT, UIO_USERSPACE,
   1651 	    SCARG(uap, path), p);
   1652 	error = namei(&nd);
   1653 	if (error)
   1654 		return (error);
   1655 
   1656 	if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & ISWHITEOUT)) {
   1657 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   1658 		if (nd.ni_dvp == nd.ni_vp)
   1659 			vrele(nd.ni_dvp);
   1660 		else
   1661 			vput(nd.ni_dvp);
   1662 		if (nd.ni_vp)
   1663 			vrele(nd.ni_vp);
   1664 		return (EEXIST);
   1665 	}
   1666 
   1667 	VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
   1668 	if ((error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, DELETE)) != 0)
   1669 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   1670 	vput(nd.ni_dvp);
   1671 	return (error);
   1672 }
   1673 
   1674 /*
   1675  * Delete a name from the filesystem.
   1676  */
   1677 /* ARGSUSED */
   1678 int
   1679 sys_unlink(l, v, retval)
   1680 	struct lwp *l;
   1681 	void *v;
   1682 	register_t *retval;
   1683 {
   1684 	struct sys_unlink_args /* {
   1685 		syscallarg(const char *) path;
   1686 	} */ *uap = v;
   1687 	struct proc *p = l->l_proc;
   1688 	struct vnode *vp;
   1689 	int error;
   1690 	struct nameidata nd;
   1691 
   1692 	NDINIT(&nd, DELETE, LOCKPARENT | LOCKLEAF, UIO_USERSPACE,
   1693 	    SCARG(uap, path), p);
   1694 	if ((error = namei(&nd)) != 0)
   1695 		return (error);
   1696 	vp = nd.ni_vp;
   1697 
   1698 	/*
   1699 	 * The root of a mounted filesystem cannot be deleted.
   1700 	 */
   1701 	if (vp->v_flag & VROOT) {
   1702 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   1703 		if (nd.ni_dvp == vp)
   1704 			vrele(nd.ni_dvp);
   1705 		else
   1706 			vput(nd.ni_dvp);
   1707 		vput(vp);
   1708 		error = EBUSY;
   1709 		goto out;
   1710 	}
   1711 
   1712 	VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
   1713 	VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
   1714 	error = VOP_REMOVE(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
   1715 out:
   1716 	return (error);
   1717 }
   1718 
   1719 /*
   1720  * Reposition read/write file offset.
   1721  */
   1722 int
   1723 sys_lseek(l, v, retval)
   1724 	struct lwp *l;
   1725 	void *v;
   1726 	register_t *retval;
   1727 {
   1728 	struct sys_lseek_args /* {
   1729 		syscallarg(int) fd;
   1730 		syscallarg(int) pad;
   1731 		syscallarg(off_t) offset;
   1732 		syscallarg(int) whence;
   1733 	} */ *uap = v;
   1734 	struct proc *p = l->l_proc;
   1735 	struct ucred *cred = p->p_ucred;
   1736 	struct filedesc *fdp = p->p_fd;
   1737 	struct file *fp;
   1738 	struct vnode *vp;
   1739 	struct vattr vattr;
   1740 	off_t newoff;
   1741 	int error;
   1742 
   1743 	if ((fp = fd_getfile(fdp, SCARG(uap, fd))) == NULL)
   1744 		return (EBADF);
   1745 
   1746 	FILE_USE(fp);
   1747 
   1748 	vp = (struct vnode *)fp->f_data;
   1749 	if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) {
   1750 		error = ESPIPE;
   1751 		goto out;
   1752 	}
   1753 
   1754 	switch (SCARG(uap, whence)) {
   1755 	case SEEK_CUR:
   1756 		newoff = fp->f_offset + SCARG(uap, offset);
   1757 		break;
   1758 	case SEEK_END:
   1759 		error = VOP_GETATTR(vp, &vattr, cred, p);
   1760 		if (error)
   1761 			goto out;
   1762 		newoff = SCARG(uap, offset) + vattr.va_size;
   1763 		break;
   1764 	case SEEK_SET:
   1765 		newoff = SCARG(uap, offset);
   1766 		break;
   1767 	default:
   1768 		error = EINVAL;
   1769 		goto out;
   1770 	}
   1771 	if ((error = VOP_SEEK(vp, fp->f_offset, newoff, cred)) != 0)
   1772 		goto out;
   1773 
   1774 	*(off_t *)retval = fp->f_offset = newoff;
   1775  out:
   1776 	FILE_UNUSE(fp, p);
   1777 	return (error);
   1778 }
   1779 
   1780 /*
   1781  * Positional read system call.
   1782  */
   1783 int
   1784 sys_pread(l, v, retval)
   1785 	struct lwp *l;
   1786 	void *v;
   1787 	register_t *retval;
   1788 {
   1789 	struct sys_pread_args /* {
   1790 		syscallarg(int) fd;
   1791 		syscallarg(void *) buf;
   1792 		syscallarg(size_t) nbyte;
   1793 		syscallarg(off_t) offset;
   1794 	} */ *uap = v;
   1795 	struct proc *p = l->l_proc;
   1796 	struct filedesc *fdp = p->p_fd;
   1797 	struct file *fp;
   1798 	struct vnode *vp;
   1799 	off_t offset;
   1800 	int error, fd = SCARG(uap, fd);
   1801 
   1802 	if ((fp = fd_getfile(fdp, fd)) == NULL)
   1803 		return (EBADF);
   1804 
   1805 	if ((fp->f_flag & FREAD) == 0) {
   1806 		simple_unlock(&fp->f_slock);
   1807 		return (EBADF);
   1808 	}
   1809 
   1810 	FILE_USE(fp);
   1811 
   1812 	vp = (struct vnode *)fp->f_data;
   1813 	if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) {
   1814 		error = ESPIPE;
   1815 		goto out;
   1816 	}
   1817 
   1818 	offset = SCARG(uap, offset);
   1819 
   1820 	/*
   1821 	 * XXX This works because no file systems actually
   1822 	 * XXX take any action on the seek operation.
   1823 	 */
   1824 	if ((error = VOP_SEEK(vp, fp->f_offset, offset, fp->f_cred)) != 0)
   1825 		goto out;
   1826 
   1827 	/* dofileread() will unuse the descriptor for us */
   1828 	return (dofileread(p, fd, fp, SCARG(uap, buf), SCARG(uap, nbyte),
   1829 	    &offset, 0, retval));
   1830 
   1831  out:
   1832 	FILE_UNUSE(fp, p);
   1833 	return (error);
   1834 }
   1835 
   1836 /*
   1837  * Positional scatter read system call.
   1838  */
   1839 int
   1840 sys_preadv(l, v, retval)
   1841 	struct lwp *l;
   1842 	void *v;
   1843 	register_t *retval;
   1844 {
   1845 	struct sys_preadv_args /* {
   1846 		syscallarg(int) fd;
   1847 		syscallarg(const struct iovec *) iovp;
   1848 		syscallarg(int) iovcnt;
   1849 		syscallarg(off_t) offset;
   1850 	} */ *uap = v;
   1851 	struct proc *p = l->l_proc;
   1852 	struct filedesc *fdp = p->p_fd;
   1853 	struct file *fp;
   1854 	struct vnode *vp;
   1855 	off_t offset;
   1856 	int error, fd = SCARG(uap, fd);
   1857 
   1858 	if ((fp = fd_getfile(fdp, fd)) == NULL)
   1859 		return (EBADF);
   1860 
   1861 	if ((fp->f_flag & FREAD) == 0) {
   1862 		simple_unlock(&fp->f_slock);
   1863 		return (EBADF);
   1864 	}
   1865 
   1866 	FILE_USE(fp);
   1867 
   1868 	vp = (struct vnode *)fp->f_data;
   1869 	if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) {
   1870 		error = ESPIPE;
   1871 		goto out;
   1872 	}
   1873 
   1874 	offset = SCARG(uap, offset);
   1875 
   1876 	/*
   1877 	 * XXX This works because no file systems actually
   1878 	 * XXX take any action on the seek operation.
   1879 	 */
   1880 	if ((error = VOP_SEEK(vp, fp->f_offset, offset, fp->f_cred)) != 0)
   1881 		goto out;
   1882 
   1883 	/* dofilereadv() will unuse the descriptor for us */
   1884 	return (dofilereadv(p, fd, fp, SCARG(uap, iovp), SCARG(uap, iovcnt),
   1885 	    &offset, 0, retval));
   1886 
   1887  out:
   1888 	FILE_UNUSE(fp, p);
   1889 	return (error);
   1890 }
   1891 
   1892 /*
   1893  * Positional write system call.
   1894  */
   1895 int
   1896 sys_pwrite(l, v, retval)
   1897 	struct lwp *l;
   1898 	void *v;
   1899 	register_t *retval;
   1900 {
   1901 	struct sys_pwrite_args /* {
   1902 		syscallarg(int) fd;
   1903 		syscallarg(const void *) buf;
   1904 		syscallarg(size_t) nbyte;
   1905 		syscallarg(off_t) offset;
   1906 	} */ *uap = v;
   1907 	struct proc *p = l->l_proc;
   1908 	struct filedesc *fdp = p->p_fd;
   1909 	struct file *fp;
   1910 	struct vnode *vp;
   1911 	off_t offset;
   1912 	int error, fd = SCARG(uap, fd);
   1913 
   1914 	if ((fp = fd_getfile(fdp, fd)) == NULL)
   1915 		return (EBADF);
   1916 
   1917 	if ((fp->f_flag & FWRITE) == 0) {
   1918 		simple_unlock(&fp->f_slock);
   1919 		return (EBADF);
   1920 	}
   1921 
   1922 	FILE_USE(fp);
   1923 
   1924 	vp = (struct vnode *)fp->f_data;
   1925 	if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) {
   1926 		error = ESPIPE;
   1927 		goto out;
   1928 	}
   1929 
   1930 	offset = SCARG(uap, offset);
   1931 
   1932 	/*
   1933 	 * XXX This works because no file systems actually
   1934 	 * XXX take any action on the seek operation.
   1935 	 */
   1936 	if ((error = VOP_SEEK(vp, fp->f_offset, offset, fp->f_cred)) != 0)
   1937 		goto out;
   1938 
   1939 	/* dofilewrite() will unuse the descriptor for us */
   1940 	return (dofilewrite(p, fd, fp, SCARG(uap, buf), SCARG(uap, nbyte),
   1941 	    &offset, 0, retval));
   1942 
   1943  out:
   1944 	FILE_UNUSE(fp, p);
   1945 	return (error);
   1946 }
   1947 
   1948 /*
   1949  * Positional gather write system call.
   1950  */
   1951 int
   1952 sys_pwritev(l, v, retval)
   1953 	struct lwp *l;
   1954 	void *v;
   1955 	register_t *retval;
   1956 {
   1957 	struct sys_pwritev_args /* {
   1958 		syscallarg(int) fd;
   1959 		syscallarg(const struct iovec *) iovp;
   1960 		syscallarg(int) iovcnt;
   1961 		syscallarg(off_t) offset;
   1962 	} */ *uap = v;
   1963 	struct proc *p = l->l_proc;
   1964 	struct filedesc *fdp = p->p_fd;
   1965 	struct file *fp;
   1966 	struct vnode *vp;
   1967 	off_t offset;
   1968 	int error, fd = SCARG(uap, fd);
   1969 
   1970 	if ((fp = fd_getfile(fdp, fd)) == NULL)
   1971 		return (EBADF);
   1972 
   1973 	if ((fp->f_flag & FWRITE) == 0) {
   1974 		simple_unlock(&fp->f_slock);
   1975 		return (EBADF);
   1976 	}
   1977 
   1978 	FILE_USE(fp);
   1979 
   1980 	vp = (struct vnode *)fp->f_data;
   1981 	if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) {
   1982 		error = ESPIPE;
   1983 		goto out;
   1984 	}
   1985 
   1986 	offset = SCARG(uap, offset);
   1987 
   1988 	/*
   1989 	 * XXX This works because no file systems actually
   1990 	 * XXX take any action on the seek operation.
   1991 	 */
   1992 	if ((error = VOP_SEEK(vp, fp->f_offset, offset, fp->f_cred)) != 0)
   1993 		goto out;
   1994 
   1995 	/* dofilewritev() will unuse the descriptor for us */
   1996 	return (dofilewritev(p, fd, fp, SCARG(uap, iovp), SCARG(uap, iovcnt),
   1997 	    &offset, 0, retval));
   1998 
   1999  out:
   2000 	FILE_UNUSE(fp, p);
   2001 	return (error);
   2002 }
   2003 
   2004 /*
   2005  * Check access permissions.
   2006  */
   2007 int
   2008 sys_access(l, v, retval)
   2009 	struct lwp *l;
   2010 	void *v;
   2011 	register_t *retval;
   2012 {
   2013 	struct sys_access_args /* {
   2014 		syscallarg(const char *) path;
   2015 		syscallarg(int) flags;
   2016 	} */ *uap = v;
   2017 	struct proc *p = l->l_proc;
   2018 	struct ucred *cred = crget();
   2019 	struct vnode *vp;
   2020 	int error, flags;
   2021 	struct nameidata nd;
   2022 
   2023 	(void)memcpy(cred, p->p_ucred, sizeof(*cred));
   2024 	cred->cr_ref = 1;
   2025 	cred->cr_uid = p->p_cred->p_ruid;
   2026 	cred->cr_gid = p->p_cred->p_rgid;
   2027 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
   2028 	    SCARG(uap, path), p);
   2029 	/* Override default credentials */
   2030 	nd.ni_cnd.cn_cred = cred;
   2031 	if ((error = namei(&nd)) != 0)
   2032 		goto out;
   2033 	vp = nd.ni_vp;
   2034 
   2035 	/* Flags == 0 means only check for existence. */
   2036 	if (SCARG(uap, flags)) {
   2037 		flags = 0;
   2038 		if (SCARG(uap, flags) & R_OK)
   2039 			flags |= VREAD;
   2040 		if (SCARG(uap, flags) & W_OK)
   2041 			flags |= VWRITE;
   2042 		if (SCARG(uap, flags) & X_OK)
   2043 			flags |= VEXEC;
   2044 
   2045 		error = VOP_ACCESS(vp, flags, cred, p);
   2046 		if (!error && (flags & VWRITE))
   2047 			error = vn_writechk(vp);
   2048 	}
   2049 	vput(vp);
   2050 out:
   2051 	crfree(cred);
   2052 	return (error);
   2053 }
   2054 
   2055 /*
   2056  * Get file status; this version follows links.
   2057  */
   2058 /* ARGSUSED */
   2059 int
   2060 sys___stat13(l, v, retval)
   2061 	struct lwp *l;
   2062 	void *v;
   2063 	register_t *retval;
   2064 {
   2065 	struct sys___stat13_args /* {
   2066 		syscallarg(const char *) path;
   2067 		syscallarg(struct stat *) ub;
   2068 	} */ *uap = v;
   2069 	struct proc *p = l->l_proc;
   2070 	struct stat sb;
   2071 	int error;
   2072 	struct nameidata nd;
   2073 
   2074 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
   2075 	    SCARG(uap, path), p);
   2076 	if ((error = namei(&nd)) != 0)
   2077 		return (error);
   2078 	error = vn_stat(nd.ni_vp, &sb, p);
   2079 	vput(nd.ni_vp);
   2080 	if (error)
   2081 		return (error);
   2082 	error = copyout(&sb, SCARG(uap, ub), sizeof(sb));
   2083 	return (error);
   2084 }
   2085 
   2086 /*
   2087  * Get file status; this version does not follow links.
   2088  */
   2089 /* ARGSUSED */
   2090 int
   2091 sys___lstat13(l, v, retval)
   2092 	struct lwp *l;
   2093 	void *v;
   2094 	register_t *retval;
   2095 {
   2096 	struct sys___lstat13_args /* {
   2097 		syscallarg(const char *) path;
   2098 		syscallarg(struct stat *) ub;
   2099 	} */ *uap = v;
   2100 	struct proc *p = l->l_proc;
   2101 	struct stat sb;
   2102 	int error;
   2103 	struct nameidata nd;
   2104 
   2105 	NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF, UIO_USERSPACE,
   2106 	    SCARG(uap, path), p);
   2107 	if ((error = namei(&nd)) != 0)
   2108 		return (error);
   2109 	error = vn_stat(nd.ni_vp, &sb, p);
   2110 	vput(nd.ni_vp);
   2111 	if (error)
   2112 		return (error);
   2113 	error = copyout(&sb, SCARG(uap, ub), sizeof(sb));
   2114 	return (error);
   2115 }
   2116 
   2117 /*
   2118  * Get configurable pathname variables.
   2119  */
   2120 /* ARGSUSED */
   2121 int
   2122 sys_pathconf(l, v, retval)
   2123 	struct lwp *l;
   2124 	void *v;
   2125 	register_t *retval;
   2126 {
   2127 	struct sys_pathconf_args /* {
   2128 		syscallarg(const char *) path;
   2129 		syscallarg(int) name;
   2130 	} */ *uap = v;
   2131 	struct proc *p = l->l_proc;
   2132 	int error;
   2133 	struct nameidata nd;
   2134 
   2135 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
   2136 	    SCARG(uap, path), p);
   2137 	if ((error = namei(&nd)) != 0)
   2138 		return (error);
   2139 	error = VOP_PATHCONF(nd.ni_vp, SCARG(uap, name), retval);
   2140 	vput(nd.ni_vp);
   2141 	return (error);
   2142 }
   2143 
   2144 /*
   2145  * Return target name of a symbolic link.
   2146  */
   2147 /* ARGSUSED */
   2148 int
   2149 sys_readlink(l, v, retval)
   2150 	struct lwp *l;
   2151 	void *v;
   2152 	register_t *retval;
   2153 {
   2154 	struct sys_readlink_args /* {
   2155 		syscallarg(const char *) path;
   2156 		syscallarg(char *) buf;
   2157 		syscallarg(size_t) count;
   2158 	} */ *uap = v;
   2159 	struct proc *p = l->l_proc;
   2160 	struct vnode *vp;
   2161 	struct iovec aiov;
   2162 	struct uio auio;
   2163 	int error;
   2164 	struct nameidata nd;
   2165 
   2166 	NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF, UIO_USERSPACE,
   2167 	    SCARG(uap, path), p);
   2168 	if ((error = namei(&nd)) != 0)
   2169 		return (error);
   2170 	vp = nd.ni_vp;
   2171 	if (vp->v_type != VLNK)
   2172 		error = EINVAL;
   2173 	else if (!(vp->v_mount->mnt_flag & MNT_SYMPERM) ||
   2174 	    (error = VOP_ACCESS(vp, VREAD, p->p_ucred, p)) == 0) {
   2175 		aiov.iov_base = SCARG(uap, buf);
   2176 		aiov.iov_len = SCARG(uap, count);
   2177 		auio.uio_iov = &aiov;
   2178 		auio.uio_iovcnt = 1;
   2179 		auio.uio_offset = 0;
   2180 		auio.uio_rw = UIO_READ;
   2181 		auio.uio_segflg = UIO_USERSPACE;
   2182 		auio.uio_procp = p;
   2183 		auio.uio_resid = SCARG(uap, count);
   2184 		error = VOP_READLINK(vp, &auio, p->p_ucred);
   2185 	}
   2186 	vput(vp);
   2187 	*retval = SCARG(uap, count) - auio.uio_resid;
   2188 	return (error);
   2189 }
   2190 
   2191 /*
   2192  * Change flags of a file given a path name.
   2193  */
   2194 /* ARGSUSED */
   2195 int
   2196 sys_chflags(l, v, retval)
   2197 	struct lwp *l;
   2198 	void *v;
   2199 	register_t *retval;
   2200 {
   2201 	struct sys_chflags_args /* {
   2202 		syscallarg(const char *) path;
   2203 		syscallarg(u_long) flags;
   2204 	} */ *uap = v;
   2205 	struct proc *p = l->l_proc;
   2206 	struct vnode *vp;
   2207 	int error;
   2208 	struct nameidata nd;
   2209 
   2210 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
   2211 	if ((error = namei(&nd)) != 0)
   2212 		return (error);
   2213 	vp = nd.ni_vp;
   2214 	error = change_flags(vp, SCARG(uap, flags), p);
   2215 	vput(vp);
   2216 	return (error);
   2217 }
   2218 
   2219 /*
   2220  * Change flags of a file given a file descriptor.
   2221  */
   2222 /* ARGSUSED */
   2223 int
   2224 sys_fchflags(l, v, retval)
   2225 	struct lwp *l;
   2226 	void *v;
   2227 	register_t *retval;
   2228 {
   2229 	struct sys_fchflags_args /* {
   2230 		syscallarg(int) fd;
   2231 		syscallarg(u_long) flags;
   2232 	} */ *uap = v;
   2233 	struct proc *p = l->l_proc;
   2234 	struct vnode *vp;
   2235 	struct file *fp;
   2236 	int error;
   2237 
   2238 	/* getvnode() will use the descriptor for us */
   2239 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
   2240 		return (error);
   2241 	vp = (struct vnode *)fp->f_data;
   2242 	error = change_flags(vp, SCARG(uap, flags), p);
   2243 	VOP_UNLOCK(vp, 0);
   2244 	FILE_UNUSE(fp, p);
   2245 	return (error);
   2246 }
   2247 
   2248 /*
   2249  * Change flags of a file given a path name; this version does
   2250  * not follow links.
   2251  */
   2252 int
   2253 sys_lchflags(l, v, retval)
   2254 	struct lwp *l;
   2255 	void *v;
   2256 	register_t *retval;
   2257 {
   2258 	struct sys_lchflags_args /* {
   2259 		syscallarg(const char *) path;
   2260 		syscallarg(u_long) flags;
   2261 	} */ *uap = v;
   2262 	struct proc *p = l->l_proc;
   2263 	struct vnode *vp;
   2264 	int error;
   2265 	struct nameidata nd;
   2266 
   2267 	NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
   2268 	if ((error = namei(&nd)) != 0)
   2269 		return (error);
   2270 	vp = nd.ni_vp;
   2271 	error = change_flags(vp, SCARG(uap, flags), p);
   2272 	vput(vp);
   2273 	return (error);
   2274 }
   2275 
   2276 /*
   2277  * Common routine to change flags of a file.
   2278  */
   2279 int
   2280 change_flags(vp, flags, p)
   2281 	struct vnode *vp;
   2282 	u_long flags;
   2283 	struct proc *p;
   2284 {
   2285 	struct vattr vattr;
   2286 	int error;
   2287 
   2288 	VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
   2289 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   2290 	/*
   2291 	 * Non-superusers cannot change the flags on devices, even if they
   2292 	 * own them.
   2293 	 */
   2294 	if (suser(p->p_ucred, &p->p_acflag) != 0) {
   2295 		if ((error = VOP_GETATTR(vp, &vattr, p->p_ucred, p)) != 0)
   2296 			goto out;
   2297 		if (vattr.va_type == VCHR || vattr.va_type == VBLK) {
   2298 			error = EINVAL;
   2299 			goto out;
   2300 		}
   2301 	}
   2302 	VATTR_NULL(&vattr);
   2303 	vattr.va_flags = flags;
   2304 	error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
   2305 out:
   2306 	return (error);
   2307 }
   2308 
   2309 /*
   2310  * Change mode of a file given path name; this version follows links.
   2311  */
   2312 /* ARGSUSED */
   2313 int
   2314 sys_chmod(l, v, retval)
   2315 	struct lwp *l;
   2316 	void *v;
   2317 	register_t *retval;
   2318 {
   2319 	struct sys_chmod_args /* {
   2320 		syscallarg(const char *) path;
   2321 		syscallarg(int) mode;
   2322 	} */ *uap = v;
   2323 	struct proc *p = l->l_proc;
   2324 	int error;
   2325 	struct nameidata nd;
   2326 
   2327 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
   2328 	if ((error = namei(&nd)) != 0)
   2329 		return (error);
   2330 
   2331 	error = change_mode(nd.ni_vp, SCARG(uap, mode), p);
   2332 
   2333 	vrele(nd.ni_vp);
   2334 	return (error);
   2335 }
   2336 
   2337 /*
   2338  * Change mode of a file given a file descriptor.
   2339  */
   2340 /* ARGSUSED */
   2341 int
   2342 sys_fchmod(l, v, retval)
   2343 	struct lwp *l;
   2344 	void *v;
   2345 	register_t *retval;
   2346 {
   2347 	struct sys_fchmod_args /* {
   2348 		syscallarg(int) fd;
   2349 		syscallarg(int) mode;
   2350 	} */ *uap = v;
   2351 	struct proc *p = l->l_proc;
   2352 	struct file *fp;
   2353 	int error;
   2354 
   2355 	/* getvnode() will use the descriptor for us */
   2356 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
   2357 		return (error);
   2358 
   2359 	error = change_mode((struct vnode *)fp->f_data, SCARG(uap, mode), p);
   2360 	FILE_UNUSE(fp, p);
   2361 	return (error);
   2362 }
   2363 
   2364 /*
   2365  * Change mode of a file given path name; this version does not follow links.
   2366  */
   2367 /* ARGSUSED */
   2368 int
   2369 sys_lchmod(l, v, retval)
   2370 	struct lwp *l;
   2371 	void *v;
   2372 	register_t *retval;
   2373 {
   2374 	struct sys_lchmod_args /* {
   2375 		syscallarg(const char *) path;
   2376 		syscallarg(int) mode;
   2377 	} */ *uap = v;
   2378 	struct proc *p = l->l_proc;
   2379 	int error;
   2380 	struct nameidata nd;
   2381 
   2382 	NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
   2383 	if ((error = namei(&nd)) != 0)
   2384 		return (error);
   2385 
   2386 	error = change_mode(nd.ni_vp, SCARG(uap, mode), p);
   2387 
   2388 	vrele(nd.ni_vp);
   2389 	return (error);
   2390 }
   2391 
   2392 /*
   2393  * Common routine to set mode given a vnode.
   2394  */
   2395 static int
   2396 change_mode(vp, mode, p)
   2397 	struct vnode *vp;
   2398 	int mode;
   2399 	struct proc *p;
   2400 {
   2401 	struct vattr vattr;
   2402 	int error;
   2403 
   2404 	VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
   2405 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   2406 	VATTR_NULL(&vattr);
   2407 	vattr.va_mode = mode & ALLPERMS;
   2408 	error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
   2409 	VOP_UNLOCK(vp, 0);
   2410 	return (error);
   2411 }
   2412 
   2413 /*
   2414  * Set ownership given a path name; this version follows links.
   2415  */
   2416 /* ARGSUSED */
   2417 int
   2418 sys_chown(l, v, retval)
   2419 	struct lwp *l;
   2420 	void *v;
   2421 	register_t *retval;
   2422 {
   2423 	struct sys_chown_args /* {
   2424 		syscallarg(const char *) path;
   2425 		syscallarg(uid_t) uid;
   2426 		syscallarg(gid_t) gid;
   2427 	} */ *uap = v;
   2428 	struct proc *p = l->l_proc;
   2429 	int error;
   2430 	struct nameidata nd;
   2431 
   2432 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
   2433 	if ((error = namei(&nd)) != 0)
   2434 		return (error);
   2435 
   2436 	error = change_owner(nd.ni_vp, SCARG(uap, uid), SCARG(uap, gid), p, 0);
   2437 
   2438 	vrele(nd.ni_vp);
   2439 	return (error);
   2440 }
   2441 
   2442 /*
   2443  * Set ownership given a path name; this version follows links.
   2444  * Provides POSIX semantics.
   2445  */
   2446 /* ARGSUSED */
   2447 int
   2448 sys___posix_chown(l, v, retval)
   2449 	struct lwp *l;
   2450 	void *v;
   2451 	register_t *retval;
   2452 {
   2453 	struct sys_chown_args /* {
   2454 		syscallarg(const char *) path;
   2455 		syscallarg(uid_t) uid;
   2456 		syscallarg(gid_t) gid;
   2457 	} */ *uap = v;
   2458 	struct proc *p = l->l_proc;
   2459 	int error;
   2460 	struct nameidata nd;
   2461 
   2462 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
   2463 	if ((error = namei(&nd)) != 0)
   2464 		return (error);
   2465 
   2466 	error = change_owner(nd.ni_vp, SCARG(uap, uid), SCARG(uap, gid), p, 1);
   2467 
   2468 	vrele(nd.ni_vp);
   2469 	return (error);
   2470 }
   2471 
   2472 /*
   2473  * Set ownership given a file descriptor.
   2474  */
   2475 /* ARGSUSED */
   2476 int
   2477 sys_fchown(l, v, retval)
   2478 	struct lwp *l;
   2479 	void *v;
   2480 	register_t *retval;
   2481 {
   2482 	struct sys_fchown_args /* {
   2483 		syscallarg(int) fd;
   2484 		syscallarg(uid_t) uid;
   2485 		syscallarg(gid_t) gid;
   2486 	} */ *uap = v;
   2487 	struct proc *p = l->l_proc;
   2488 	int error;
   2489 	struct file *fp;
   2490 
   2491 	/* getvnode() will use the descriptor for us */
   2492 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
   2493 		return (error);
   2494 
   2495 	error = change_owner((struct vnode *)fp->f_data, SCARG(uap, uid),
   2496 	    SCARG(uap, gid), p, 0);
   2497 	FILE_UNUSE(fp, p);
   2498 	return (error);
   2499 }
   2500 
   2501 /*
   2502  * Set ownership given a file descriptor, providing POSIX/XPG semantics.
   2503  */
   2504 /* ARGSUSED */
   2505 int
   2506 sys___posix_fchown(l, v, retval)
   2507 	struct lwp *l;
   2508 	void *v;
   2509 	register_t *retval;
   2510 {
   2511 	struct sys_fchown_args /* {
   2512 		syscallarg(int) fd;
   2513 		syscallarg(uid_t) uid;
   2514 		syscallarg(gid_t) gid;
   2515 	} */ *uap = v;
   2516 	struct proc *p = l->l_proc;
   2517 	int error;
   2518 	struct file *fp;
   2519 
   2520 	/* getvnode() will use the descriptor for us */
   2521 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
   2522 		return (error);
   2523 
   2524 	error = change_owner((struct vnode *)fp->f_data, SCARG(uap, uid),
   2525 	    SCARG(uap, gid), p, 1);
   2526 	FILE_UNUSE(fp, p);
   2527 	return (error);
   2528 }
   2529 
   2530 /*
   2531  * Set ownership given a path name; this version does not follow links.
   2532  */
   2533 /* ARGSUSED */
   2534 int
   2535 sys_lchown(l, v, retval)
   2536 	struct lwp *l;
   2537 	void *v;
   2538 	register_t *retval;
   2539 {
   2540 	struct sys_lchown_args /* {
   2541 		syscallarg(const char *) path;
   2542 		syscallarg(uid_t) uid;
   2543 		syscallarg(gid_t) gid;
   2544 	} */ *uap = v;
   2545 	struct proc *p = l->l_proc;
   2546 	int error;
   2547 	struct nameidata nd;
   2548 
   2549 	NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
   2550 	if ((error = namei(&nd)) != 0)
   2551 		return (error);
   2552 
   2553 	error = change_owner(nd.ni_vp, SCARG(uap, uid), SCARG(uap, gid), p, 0);
   2554 
   2555 	vrele(nd.ni_vp);
   2556 	return (error);
   2557 }
   2558 
   2559 /*
   2560  * Set ownership given a path name; this version does not follow links.
   2561  * Provides POSIX/XPG semantics.
   2562  */
   2563 /* ARGSUSED */
   2564 int
   2565 sys___posix_lchown(l, v, retval)
   2566 	struct lwp *l;
   2567 	void *v;
   2568 	register_t *retval;
   2569 {
   2570 	struct sys_lchown_args /* {
   2571 		syscallarg(const char *) path;
   2572 		syscallarg(uid_t) uid;
   2573 		syscallarg(gid_t) gid;
   2574 	} */ *uap = v;
   2575 	struct proc *p = l->l_proc;
   2576 	int error;
   2577 	struct nameidata nd;
   2578 
   2579 	NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
   2580 	if ((error = namei(&nd)) != 0)
   2581 		return (error);
   2582 
   2583 	error = change_owner(nd.ni_vp, SCARG(uap, uid), SCARG(uap, gid), p, 1);
   2584 
   2585 	vrele(nd.ni_vp);
   2586 	return (error);
   2587 }
   2588 
   2589 /*
   2590  * Common routine to set ownership given a vnode.
   2591  */
   2592 static int
   2593 change_owner(vp, uid, gid, p, posix_semantics)
   2594 	struct vnode *vp;
   2595 	uid_t uid;
   2596 	gid_t gid;
   2597 	struct proc *p;
   2598 	int posix_semantics;
   2599 {
   2600 	struct vattr vattr;
   2601 	mode_t newmode;
   2602 	int error;
   2603 
   2604 	VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
   2605 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   2606 	if ((error = VOP_GETATTR(vp, &vattr, p->p_ucred, p)) != 0)
   2607 		goto out;
   2608 
   2609 #define CHANGED(x) ((int)(x) != -1)
   2610 	newmode = vattr.va_mode;
   2611 	if (posix_semantics) {
   2612 		/*
   2613 		 * POSIX/XPG semantics: if the caller is not the super-user,
   2614 		 * clear set-user-id and set-group-id bits.  Both POSIX and
   2615 		 * the XPG consider the behaviour for calls by the super-user
   2616 		 * implementation-defined; we leave the set-user-id and set-
   2617 		 * group-id settings intact in that case.
   2618 		 */
   2619 		if (suser(p->p_ucred, NULL) != 0)
   2620 			newmode &= ~(S_ISUID | S_ISGID);
   2621 	} else {
   2622 		/*
   2623 		 * NetBSD semantics: when changing owner and/or group,
   2624 		 * clear the respective bit(s).
   2625 		 */
   2626 		if (CHANGED(uid))
   2627 			newmode &= ~S_ISUID;
   2628 		if (CHANGED(gid))
   2629 			newmode &= ~S_ISGID;
   2630 	}
   2631 	/* Update va_mode iff altered. */
   2632 	if (vattr.va_mode == newmode)
   2633 		newmode = VNOVAL;
   2634 
   2635 	VATTR_NULL(&vattr);
   2636 	vattr.va_uid = CHANGED(uid) ? uid : (uid_t)VNOVAL;
   2637 	vattr.va_gid = CHANGED(gid) ? gid : (gid_t)VNOVAL;
   2638 	vattr.va_mode = newmode;
   2639 	error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
   2640 #undef CHANGED
   2641 
   2642 out:
   2643 	VOP_UNLOCK(vp, 0);
   2644 	return (error);
   2645 }
   2646 
   2647 /*
   2648  * Set the access and modification times given a path name; this
   2649  * version follows links.
   2650  */
   2651 /* ARGSUSED */
   2652 int
   2653 sys_utimes(l, v, retval)
   2654 	struct lwp *l;
   2655 	void *v;
   2656 	register_t *retval;
   2657 {
   2658 	struct sys_utimes_args /* {
   2659 		syscallarg(const char *) path;
   2660 		syscallarg(const struct timeval *) tptr;
   2661 	} */ *uap = v;
   2662 	struct proc *p = l->l_proc;
   2663 	int error;
   2664 	struct nameidata nd;
   2665 
   2666 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
   2667 	if ((error = namei(&nd)) != 0)
   2668 		return (error);
   2669 
   2670 	error = change_utimes(nd.ni_vp, SCARG(uap, tptr), p);
   2671 
   2672 	vrele(nd.ni_vp);
   2673 	return (error);
   2674 }
   2675 
   2676 /*
   2677  * Set the access and modification times given a file descriptor.
   2678  */
   2679 /* ARGSUSED */
   2680 int
   2681 sys_futimes(l, v, retval)
   2682 	struct lwp *l;
   2683 	void *v;
   2684 	register_t *retval;
   2685 {
   2686 	struct sys_futimes_args /* {
   2687 		syscallarg(int) fd;
   2688 		syscallarg(const struct timeval *) tptr;
   2689 	} */ *uap = v;
   2690 	struct proc *p = l->l_proc;
   2691 	int error;
   2692 	struct file *fp;
   2693 
   2694 	/* getvnode() will use the descriptor for us */
   2695 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
   2696 		return (error);
   2697 
   2698 	error = change_utimes((struct vnode *)fp->f_data, SCARG(uap, tptr), p);
   2699 	FILE_UNUSE(fp, p);
   2700 	return (error);
   2701 }
   2702 
   2703 /*
   2704  * Set the access and modification times given a path name; this
   2705  * version does not follow links.
   2706  */
   2707 /* ARGSUSED */
   2708 int
   2709 sys_lutimes(l, v, retval)
   2710 	struct lwp *l;
   2711 	void *v;
   2712 	register_t *retval;
   2713 {
   2714 	struct sys_lutimes_args /* {
   2715 		syscallarg(const char *) path;
   2716 		syscallarg(const struct timeval *) tptr;
   2717 	} */ *uap = v;
   2718 	struct proc *p = l->l_proc;
   2719 	int error;
   2720 	struct nameidata nd;
   2721 
   2722 	NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
   2723 	if ((error = namei(&nd)) != 0)
   2724 		return (error);
   2725 
   2726 	error = change_utimes(nd.ni_vp, SCARG(uap, tptr), p);
   2727 
   2728 	vrele(nd.ni_vp);
   2729 	return (error);
   2730 }
   2731 
   2732 /*
   2733  * Common routine to set access and modification times given a vnode.
   2734  */
   2735 static int
   2736 change_utimes(vp, tptr, p)
   2737 	struct vnode *vp;
   2738 	const struct timeval *tptr;
   2739 	struct proc *p;
   2740 {
   2741 	struct timeval tv[2];
   2742 	struct vattr vattr;
   2743 	int error;
   2744 
   2745 	VATTR_NULL(&vattr);
   2746 	if (tptr == NULL) {
   2747 		microtime(&tv[0]);
   2748 		tv[1] = tv[0];
   2749 		vattr.va_vaflags |= VA_UTIMES_NULL;
   2750 	} else {
   2751 		error = copyin(tptr, tv, sizeof(tv));
   2752 		if (error)
   2753 			return (error);
   2754 	}
   2755 	VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
   2756 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   2757 	vattr.va_atime.tv_sec = tv[0].tv_sec;
   2758 	vattr.va_atime.tv_nsec = tv[0].tv_usec * 1000;
   2759 	vattr.va_mtime.tv_sec = tv[1].tv_sec;
   2760 	vattr.va_mtime.tv_nsec = tv[1].tv_usec * 1000;
   2761 	error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
   2762 	VOP_UNLOCK(vp, 0);
   2763 	return (error);
   2764 }
   2765 
   2766 /*
   2767  * Truncate a file given its path name.
   2768  */
   2769 /* ARGSUSED */
   2770 int
   2771 sys_truncate(l, v, retval)
   2772 	struct lwp *l;
   2773 	void *v;
   2774 	register_t *retval;
   2775 {
   2776 	struct sys_truncate_args /* {
   2777 		syscallarg(const char *) path;
   2778 		syscallarg(int) pad;
   2779 		syscallarg(off_t) length;
   2780 	} */ *uap = v;
   2781 	struct proc *p = l->l_proc;
   2782 	struct vnode *vp;
   2783 	struct vattr vattr;
   2784 	int error;
   2785 	struct nameidata nd;
   2786 
   2787 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
   2788 	if ((error = namei(&nd)) != 0)
   2789 		return (error);
   2790 	vp = nd.ni_vp;
   2791 	VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
   2792 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   2793 	if (vp->v_type == VDIR)
   2794 		error = EISDIR;
   2795 	else if ((error = vn_writechk(vp)) == 0 &&
   2796 	    (error = VOP_ACCESS(vp, VWRITE, p->p_ucred, p)) == 0) {
   2797 		VATTR_NULL(&vattr);
   2798 		vattr.va_size = SCARG(uap, length);
   2799 		error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
   2800 	}
   2801 	vput(vp);
   2802 	return (error);
   2803 }
   2804 
   2805 /*
   2806  * Truncate a file given a file descriptor.
   2807  */
   2808 /* ARGSUSED */
   2809 int
   2810 sys_ftruncate(l, v, retval)
   2811 	struct lwp *l;
   2812 	void *v;
   2813 	register_t *retval;
   2814 {
   2815 	struct sys_ftruncate_args /* {
   2816 		syscallarg(int) fd;
   2817 		syscallarg(int) pad;
   2818 		syscallarg(off_t) length;
   2819 	} */ *uap = v;
   2820 	struct proc *p = l->l_proc;
   2821 	struct vattr vattr;
   2822 	struct vnode *vp;
   2823 	struct file *fp;
   2824 	int error;
   2825 
   2826 	/* getvnode() will use the descriptor for us */
   2827 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
   2828 		return (error);
   2829 	if ((fp->f_flag & FWRITE) == 0) {
   2830 		error = EINVAL;
   2831 		goto out;
   2832 	}
   2833 	vp = (struct vnode *)fp->f_data;
   2834 	VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
   2835 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   2836 	if (vp->v_type == VDIR)
   2837 		error = EISDIR;
   2838 	else if ((error = vn_writechk(vp)) == 0) {
   2839 		VATTR_NULL(&vattr);
   2840 		vattr.va_size = SCARG(uap, length);
   2841 		error = VOP_SETATTR(vp, &vattr, fp->f_cred, p);
   2842 	}
   2843 	VOP_UNLOCK(vp, 0);
   2844  out:
   2845 	FILE_UNUSE(fp, p);
   2846 	return (error);
   2847 }
   2848 
   2849 /*
   2850  * Sync an open file.
   2851  */
   2852 /* ARGSUSED */
   2853 int
   2854 sys_fsync(l, v, retval)
   2855 	struct lwp *l;
   2856 	void *v;
   2857 	register_t *retval;
   2858 {
   2859 	struct sys_fsync_args /* {
   2860 		syscallarg(int) fd;
   2861 	} */ *uap = v;
   2862 	struct proc *p = l->l_proc;
   2863 	struct vnode *vp;
   2864 	struct file *fp;
   2865 	int error;
   2866 
   2867 	/* getvnode() will use the descriptor for us */
   2868 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
   2869 		return (error);
   2870 	vp = (struct vnode *)fp->f_data;
   2871 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   2872 	error = VOP_FSYNC(vp, fp->f_cred, FSYNC_WAIT, 0, 0, p);
   2873 	if (error == 0 && bioops.io_fsync != NULL &&
   2874 	    vp->v_mount && (vp->v_mount->mnt_flag & MNT_SOFTDEP))
   2875 		(*bioops.io_fsync)(vp);
   2876 	VOP_UNLOCK(vp, 0);
   2877 	FILE_UNUSE(fp, p);
   2878 	return (error);
   2879 }
   2880 
   2881 /*
   2882  * Sync the data of an open file.
   2883  */
   2884 /* ARGSUSED */
   2885 int
   2886 sys_fdatasync(l, v, retval)
   2887 	struct lwp *l;
   2888 	void *v;
   2889 	register_t *retval;
   2890 {
   2891 	struct sys_fdatasync_args /* {
   2892 		syscallarg(int) fd;
   2893 	} */ *uap = v;
   2894 	struct proc *p = l->l_proc;
   2895 	struct vnode *vp;
   2896 	struct file *fp;
   2897 	int error;
   2898 
   2899 	/* getvnode() will use the descriptor for us */
   2900 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
   2901 		return (error);
   2902 	vp = (struct vnode *)fp->f_data;
   2903 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   2904 	error = VOP_FSYNC(vp, fp->f_cred, FSYNC_WAIT|FSYNC_DATAONLY, 0, 0, p);
   2905 	VOP_UNLOCK(vp, 0);
   2906 	FILE_UNUSE(fp, p);
   2907 	return (error);
   2908 }
   2909 
   2910 /*
   2911  * Rename files, (standard) BSD semantics frontend.
   2912  */
   2913 /* ARGSUSED */
   2914 int
   2915 sys_rename(l, v, retval)
   2916 	struct lwp *l;
   2917 	void *v;
   2918 	register_t *retval;
   2919 {
   2920 	struct sys_rename_args /* {
   2921 		syscallarg(const char *) from;
   2922 		syscallarg(const char *) to;
   2923 	} */ *uap = v;
   2924 	struct proc *p = l->l_proc;
   2925 
   2926 	return (rename_files(SCARG(uap, from), SCARG(uap, to), p, 0));
   2927 }
   2928 
   2929 /*
   2930  * Rename files, POSIX semantics frontend.
   2931  */
   2932 /* ARGSUSED */
   2933 int
   2934 sys___posix_rename(l, v, retval)
   2935 	struct lwp *l;
   2936 	void *v;
   2937 	register_t *retval;
   2938 {
   2939 	struct sys___posix_rename_args /* {
   2940 		syscallarg(const char *) from;
   2941 		syscallarg(const char *) to;
   2942 	} */ *uap = v;
   2943 	struct proc *p = l->l_proc;
   2944 
   2945 	return (rename_files(SCARG(uap, from), SCARG(uap, to), p, 1));
   2946 }
   2947 
   2948 /*
   2949  * Rename files.  Source and destination must either both be directories,
   2950  * or both not be directories.  If target is a directory, it must be empty.
   2951  * If `from' and `to' refer to the same object, the value of the `retain'
   2952  * argument is used to determine whether `from' will be
   2953  *
   2954  * (retain == 0)	deleted unless `from' and `to' refer to the same
   2955  *			object in the file system's name space (BSD).
   2956  * (retain == 1)	always retained (POSIX).
   2957  */
   2958 static int
   2959 rename_files(from, to, p, retain)
   2960 	const char *from, *to;
   2961 	struct proc *p;
   2962 	int retain;
   2963 {
   2964 	struct vnode *tvp, *fvp, *tdvp;
   2965 	struct nameidata fromnd, tond;
   2966 	int error;
   2967 
   2968 	NDINIT(&fromnd, DELETE, WANTPARENT | SAVESTART, UIO_USERSPACE,
   2969 	    from, p);
   2970 	if ((error = namei(&fromnd)) != 0)
   2971 		return (error);
   2972 	fvp = fromnd.ni_vp;
   2973 	NDINIT(&tond, RENAME, LOCKPARENT | LOCKLEAF | NOCACHE | SAVESTART |
   2974 	    (fvp->v_type == VDIR ? CREATEDIR : 0), UIO_USERSPACE, to, p);
   2975 	if ((error = namei(&tond)) != 0) {
   2976 		VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
   2977 		vrele(fromnd.ni_dvp);
   2978 		vrele(fvp);
   2979 		goto out1;
   2980 	}
   2981 	tdvp = tond.ni_dvp;
   2982 	tvp = tond.ni_vp;
   2983 
   2984 	if (tvp != NULL) {
   2985 		if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
   2986 			error = ENOTDIR;
   2987 			goto out;
   2988 		} else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
   2989 			error = EISDIR;
   2990 			goto out;
   2991 		}
   2992 	}
   2993 
   2994 	if (fvp == tdvp)
   2995 		error = EINVAL;
   2996 
   2997 	/*
   2998 	 * Source and destination refer to the same object.
   2999 	 */
   3000 	if (fvp == tvp) {
   3001 		if (retain)
   3002 			error = -1;
   3003 		else if (fromnd.ni_dvp == tdvp &&
   3004 		    fromnd.ni_cnd.cn_namelen == tond.ni_cnd.cn_namelen &&
   3005 		    !memcmp(fromnd.ni_cnd.cn_nameptr,
   3006 		          tond.ni_cnd.cn_nameptr,
   3007 		          fromnd.ni_cnd.cn_namelen))
   3008 		error = -1;
   3009 	}
   3010 
   3011 out:
   3012 	if (!error) {
   3013 		VOP_LEASE(tdvp, p, p->p_ucred, LEASE_WRITE);
   3014 		if (fromnd.ni_dvp != tdvp)
   3015 			VOP_LEASE(fromnd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
   3016 		if (tvp) {
   3017 			VOP_LEASE(tvp, p, p->p_ucred, LEASE_WRITE);
   3018 		}
   3019 		error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
   3020 				   tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
   3021 	} else {
   3022 		VOP_ABORTOP(tond.ni_dvp, &tond.ni_cnd);
   3023 		if (tdvp == tvp)
   3024 			vrele(tdvp);
   3025 		else
   3026 			vput(tdvp);
   3027 		if (tvp)
   3028 			vput(tvp);
   3029 		VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
   3030 		vrele(fromnd.ni_dvp);
   3031 		vrele(fvp);
   3032 	}
   3033 	vrele(tond.ni_startdir);
   3034 	PNBUF_PUT(tond.ni_cnd.cn_pnbuf);
   3035 out1:
   3036 	if (fromnd.ni_startdir)
   3037 		vrele(fromnd.ni_startdir);
   3038 	PNBUF_PUT(fromnd.ni_cnd.cn_pnbuf);
   3039 	return (error == -1 ? 0 : error);
   3040 }
   3041 
   3042 /*
   3043  * Make a directory file.
   3044  */
   3045 /* ARGSUSED */
   3046 int
   3047 sys_mkdir(l, v, retval)
   3048 	struct lwp *l;
   3049 	void *v;
   3050 	register_t *retval;
   3051 {
   3052 	struct sys_mkdir_args /* {
   3053 		syscallarg(const char *) path;
   3054 		syscallarg(int) mode;
   3055 	} */ *uap = v;
   3056 	struct proc *p = l->l_proc;
   3057 	struct vnode *vp;
   3058 	struct vattr vattr;
   3059 	int error;
   3060 	struct nameidata nd;
   3061 
   3062 	NDINIT(&nd, CREATE, LOCKPARENT | CREATEDIR, UIO_USERSPACE,
   3063 	    SCARG(uap, path), p);
   3064 	if ((error = namei(&nd)) != 0)
   3065 		return (error);
   3066 	vp = nd.ni_vp;
   3067 	if (vp != NULL) {
   3068 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   3069 		if (nd.ni_dvp == vp)
   3070 			vrele(nd.ni_dvp);
   3071 		else
   3072 			vput(nd.ni_dvp);
   3073 		vrele(vp);
   3074 		return (EEXIST);
   3075 	}
   3076 	VATTR_NULL(&vattr);
   3077 	vattr.va_type = VDIR;
   3078 	vattr.va_mode =
   3079 	    (SCARG(uap, mode) & ACCESSPERMS) &~ p->p_cwdi->cwdi_cmask;
   3080 	VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
   3081 	error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
   3082 	if (!error)
   3083 		vput(nd.ni_vp);
   3084 	return (error);
   3085 }
   3086 
   3087 /*
   3088  * Remove a directory file.
   3089  */
   3090 /* ARGSUSED */
   3091 int
   3092 sys_rmdir(l, v, retval)
   3093 	struct lwp *l;
   3094 	void *v;
   3095 	register_t *retval;
   3096 {
   3097 	struct sys_rmdir_args /* {
   3098 		syscallarg(const char *) path;
   3099 	} */ *uap = v;
   3100 	struct proc *p = l->l_proc;
   3101 	struct vnode *vp;
   3102 	int error;
   3103 	struct nameidata nd;
   3104 
   3105 	NDINIT(&nd, DELETE, LOCKPARENT | LOCKLEAF, UIO_USERSPACE,
   3106 	    SCARG(uap, path), p);
   3107 	if ((error = namei(&nd)) != 0)
   3108 		return (error);
   3109 	vp = nd.ni_vp;
   3110 	if (vp->v_type != VDIR) {
   3111 		error = ENOTDIR;
   3112 		goto out;
   3113 	}
   3114 	/*
   3115 	 * No rmdir "." please.
   3116 	 */
   3117 	if (nd.ni_dvp == vp) {
   3118 		error = EINVAL;
   3119 		goto out;
   3120 	}
   3121 	/*
   3122 	 * The root of a mounted filesystem cannot be deleted.
   3123 	 */
   3124 	if (vp->v_flag & VROOT)
   3125 		error = EBUSY;
   3126 out:
   3127 	if (!error) {
   3128 		VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
   3129 		VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
   3130 		error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
   3131 	} else {
   3132 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   3133 		if (nd.ni_dvp == vp)
   3134 			vrele(nd.ni_dvp);
   3135 		else
   3136 			vput(nd.ni_dvp);
   3137 		vput(vp);
   3138 	}
   3139 	return (error);
   3140 }
   3141 
   3142 /*
   3143  * Read a block of directory entries in a file system independent format.
   3144  */
   3145 int
   3146 sys_getdents(l, v, retval)
   3147 	struct lwp *l;
   3148 	void *v;
   3149 	register_t *retval;
   3150 {
   3151 	struct sys_getdents_args /* {
   3152 		syscallarg(int) fd;
   3153 		syscallarg(char *) buf;
   3154 		syscallarg(size_t) count;
   3155 	} */ *uap = v;
   3156 	struct proc *p = l->l_proc;
   3157 	struct file *fp;
   3158 	int error, done;
   3159 
   3160 	/* getvnode() will use the descriptor for us */
   3161 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
   3162 		return (error);
   3163 	if ((fp->f_flag & FREAD) == 0) {
   3164 		error = EBADF;
   3165 		goto out;
   3166 	}
   3167 	error = vn_readdir(fp, SCARG(uap, buf), UIO_USERSPACE,
   3168 			SCARG(uap, count), &done, p, 0, 0);
   3169 #ifdef KTRACE
   3170 	if (!error && KTRPOINT(p, KTR_GENIO)) {
   3171 		struct iovec iov;
   3172 		iov.iov_base = SCARG(uap, buf);
   3173 		iov.iov_len = done;
   3174 		ktrgenio(p, SCARG(uap, fd), UIO_READ, &iov, done, 0);
   3175 	}
   3176 #endif
   3177 	*retval = done;
   3178  out:
   3179 	FILE_UNUSE(fp, p);
   3180 	return (error);
   3181 }
   3182 
   3183 /*
   3184  * Set the mode mask for creation of filesystem nodes.
   3185  */
   3186 int
   3187 sys_umask(l, v, retval)
   3188 	struct lwp *l;
   3189 	void *v;
   3190 	register_t *retval;
   3191 {
   3192 	struct sys_umask_args /* {
   3193 		syscallarg(mode_t) newmask;
   3194 	} */ *uap = v;
   3195 	struct proc *p = l->l_proc;
   3196 	struct cwdinfo *cwdi;
   3197 
   3198 	cwdi = p->p_cwdi;
   3199 	*retval = cwdi->cwdi_cmask;
   3200 	cwdi->cwdi_cmask = SCARG(uap, newmask) & ALLPERMS;
   3201 	return (0);
   3202 }
   3203 
   3204 /*
   3205  * Void all references to file by ripping underlying filesystem
   3206  * away from vnode.
   3207  */
   3208 /* ARGSUSED */
   3209 int
   3210 sys_revoke(l, v, retval)
   3211 	struct lwp *l;
   3212 	void *v;
   3213 	register_t *retval;
   3214 {
   3215 	struct sys_revoke_args /* {
   3216 		syscallarg(const char *) path;
   3217 	} */ *uap = v;
   3218 	struct proc *p = l->l_proc;
   3219 	struct vnode *vp;
   3220 	struct vattr vattr;
   3221 	int error;
   3222 	struct nameidata nd;
   3223 
   3224 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
   3225 	if ((error = namei(&nd)) != 0)
   3226 		return (error);
   3227 	vp = nd.ni_vp;
   3228 	if ((error = VOP_GETATTR(vp, &vattr, p->p_ucred, p)) != 0)
   3229 		goto out;
   3230 	if (p->p_ucred->cr_uid != vattr.va_uid &&
   3231 	    (error = suser(p->p_ucred, &p->p_acflag)) != 0)
   3232 		goto out;
   3233 	if (vp->v_usecount > 1 || (vp->v_flag & (VALIASED | VLAYER)))
   3234 		VOP_REVOKE(vp, REVOKEALL);
   3235 out:
   3236 	vrele(vp);
   3237 	return (error);
   3238 }
   3239 
   3240 /*
   3241  * Convert a user file descriptor to a kernel file entry.
   3242  */
   3243 int
   3244 getvnode(fdp, fd, fpp)
   3245 	struct filedesc *fdp;
   3246 	int fd;
   3247 	struct file **fpp;
   3248 {
   3249 	struct vnode *vp;
   3250 	struct file *fp;
   3251 
   3252 	if ((fp = fd_getfile(fdp, fd)) == NULL)
   3253 		return (EBADF);
   3254 
   3255 	FILE_USE(fp);
   3256 
   3257 	if (fp->f_type != DTYPE_VNODE) {
   3258 		FILE_UNUSE(fp, NULL);
   3259 		return (EINVAL);
   3260 	}
   3261 
   3262 	vp = (struct vnode *)fp->f_data;
   3263 	if (vp->v_type == VBAD) {
   3264 		FILE_UNUSE(fp, NULL);
   3265 		return (EBADF);
   3266 	}
   3267 
   3268 	*fpp = fp;
   3269 	return (0);
   3270 }
   3271