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