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