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