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