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