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