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