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