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