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