Home | History | Annotate | Line # | Download | only in tmpfs
tmpfs_vnops.c revision 1.92.2.1
      1 /*	$NetBSD: tmpfs_vnops.c,v 1.92.2.1 2012/01/04 16:43:37 yamt Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2005, 2006, 2007 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Julio M. Merino Vidal, developed as part of Google's Summer of Code
      9  * 2005 program.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30  * POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 /*
     34  * tmpfs vnode interface.
     35  */
     36 
     37 #include <sys/cdefs.h>
     38 __KERNEL_RCSID(0, "$NetBSD: tmpfs_vnops.c,v 1.92.2.1 2012/01/04 16:43:37 yamt Exp $");
     39 
     40 #include <sys/param.h>
     41 #include <sys/dirent.h>
     42 #include <sys/fcntl.h>
     43 #include <sys/event.h>
     44 #include <sys/malloc.h>
     45 #include <sys/namei.h>
     46 #include <sys/stat.h>
     47 #include <sys/uio.h>
     48 #include <sys/unistd.h>
     49 #include <sys/vnode.h>
     50 #include <sys/lockf.h>
     51 #include <sys/kauth.h>
     52 
     53 #include <uvm/uvm.h>
     54 
     55 #include <miscfs/fifofs/fifo.h>
     56 #include <miscfs/genfs/genfs.h>
     57 #include <fs/tmpfs/tmpfs_vnops.h>
     58 #include <fs/tmpfs/tmpfs.h>
     59 
     60 /*
     61  * vnode operations vector used for files stored in a tmpfs file system.
     62  */
     63 int (**tmpfs_vnodeop_p)(void *);
     64 const struct vnodeopv_entry_desc tmpfs_vnodeop_entries[] = {
     65 	{ &vop_default_desc,		vn_default_error },
     66 	{ &vop_lookup_desc,		tmpfs_lookup },
     67 	{ &vop_create_desc,		tmpfs_create },
     68 	{ &vop_mknod_desc,		tmpfs_mknod },
     69 	{ &vop_open_desc,		tmpfs_open },
     70 	{ &vop_close_desc,		tmpfs_close },
     71 	{ &vop_access_desc,		tmpfs_access },
     72 	{ &vop_getattr_desc,		tmpfs_getattr },
     73 	{ &vop_setattr_desc,		tmpfs_setattr },
     74 	{ &vop_read_desc,		tmpfs_read },
     75 	{ &vop_write_desc,		tmpfs_write },
     76 	{ &vop_ioctl_desc,		tmpfs_ioctl },
     77 	{ &vop_fcntl_desc,		tmpfs_fcntl },
     78 	{ &vop_poll_desc,		tmpfs_poll },
     79 	{ &vop_kqfilter_desc,		tmpfs_kqfilter },
     80 	{ &vop_revoke_desc,		tmpfs_revoke },
     81 	{ &vop_mmap_desc,		tmpfs_mmap },
     82 	{ &vop_fsync_desc,		tmpfs_fsync },
     83 	{ &vop_seek_desc,		tmpfs_seek },
     84 	{ &vop_remove_desc,		tmpfs_remove },
     85 	{ &vop_link_desc,		tmpfs_link },
     86 	{ &vop_rename_desc,		tmpfs_rename },
     87 	{ &vop_mkdir_desc,		tmpfs_mkdir },
     88 	{ &vop_rmdir_desc,		tmpfs_rmdir },
     89 	{ &vop_symlink_desc,		tmpfs_symlink },
     90 	{ &vop_readdir_desc,		tmpfs_readdir },
     91 	{ &vop_readlink_desc,		tmpfs_readlink },
     92 	{ &vop_abortop_desc,		tmpfs_abortop },
     93 	{ &vop_inactive_desc,		tmpfs_inactive },
     94 	{ &vop_reclaim_desc,		tmpfs_reclaim },
     95 	{ &vop_lock_desc,		tmpfs_lock },
     96 	{ &vop_unlock_desc,		tmpfs_unlock },
     97 	{ &vop_bmap_desc,		tmpfs_bmap },
     98 	{ &vop_strategy_desc,		tmpfs_strategy },
     99 	{ &vop_print_desc,		tmpfs_print },
    100 	{ &vop_pathconf_desc,		tmpfs_pathconf },
    101 	{ &vop_islocked_desc,		tmpfs_islocked },
    102 	{ &vop_advlock_desc,		tmpfs_advlock },
    103 	{ &vop_bwrite_desc,		tmpfs_bwrite },
    104 	{ &vop_getpages_desc,		tmpfs_getpages },
    105 	{ &vop_putpages_desc,		tmpfs_putpages },
    106 	{ &vop_whiteout_desc,		tmpfs_whiteout },
    107 	{ NULL, NULL }
    108 };
    109 
    110 const struct vnodeopv_desc tmpfs_vnodeop_opv_desc = {
    111 	&tmpfs_vnodeop_p, tmpfs_vnodeop_entries
    112 };
    113 
    114 /*
    115  * tmpfs_lookup: path name traversal routine.
    116  *
    117  * Arguments: dvp (directory being searched), vpp (result),
    118  * cnp (component name - path).
    119  *
    120  * => Caller holds a reference and lock on dvp.
    121  * => We return looked-up vnode (vpp) locked, with a reference held.
    122  */
    123 int
    124 tmpfs_lookup(void *v)
    125 {
    126 	struct vop_lookup_args /* {
    127 		struct vnode *a_dvp;
    128 		struct vnode **a_vpp;
    129 		struct componentname *a_cnp;
    130 	} */ *ap = v;
    131 	vnode_t *dvp = ap->a_dvp, **vpp = ap->a_vpp;
    132 	struct componentname *cnp = ap->a_cnp;
    133 	const bool lastcn = (cnp->cn_flags & ISLASTCN) != 0;
    134 	tmpfs_node_t *dnode, *tnode;
    135 	tmpfs_dirent_t *de;
    136 	int error;
    137 
    138 	KASSERT(VOP_ISLOCKED(dvp));
    139 
    140 	dnode = VP_TO_TMPFS_DIR(dvp);
    141 	*vpp = NULL;
    142 
    143 	/* Check accessibility of directory. */
    144 	error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred);
    145 	if (error) {
    146 		goto out;
    147 	}
    148 
    149 	/*
    150 	 * If requesting the last path component on a read-only file system
    151 	 * with a write operation, deny it.
    152 	 */
    153 	if (lastcn && (dvp->v_mount->mnt_flag & MNT_RDONLY) != 0 &&
    154 	    (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
    155 		error = EROFS;
    156 		goto out;
    157 	}
    158 
    159 	/*
    160 	 * Avoid doing a linear scan of the directory if the requested
    161 	 * directory/name couple is already in the cache.
    162 	 */
    163 	error = cache_lookup(dvp, vpp, cnp);
    164 	if (error >= 0) {
    165 		/* Both cache-hit or an error case. */
    166 		goto out;
    167 	}
    168 
    169 	if (cnp->cn_flags & ISDOTDOT) {
    170 		tmpfs_node_t *pnode;
    171 
    172 		/*
    173 		 * Lookup of ".." case.
    174 		 */
    175 		if (lastcn && cnp->cn_nameiop == RENAME) {
    176 			error = EINVAL;
    177 			goto out;
    178 		}
    179 		KASSERT(dnode->tn_type == VDIR);
    180 		pnode = dnode->tn_spec.tn_dir.tn_parent;
    181 		if (pnode == NULL) {
    182 			error = ENOENT;
    183 			goto out;
    184 		}
    185 
    186 		/*
    187 		 * Lock the parent tn_vlock before releasing the vnode lock,
    188 		 * and thus prevents parent from disappearing.
    189 		 */
    190 		mutex_enter(&pnode->tn_vlock);
    191 		VOP_UNLOCK(dvp);
    192 
    193 		/*
    194 		 * Get a vnode of the '..' entry and re-acquire the lock.
    195 		 * Release the tn_vlock.
    196 		 */
    197 		error = tmpfs_vnode_get(dvp->v_mount, pnode, vpp);
    198 		vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
    199 		goto out;
    200 
    201 	} else if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') {
    202 		/*
    203 		 * Lookup of "." case.
    204 		 */
    205 		if (lastcn && cnp->cn_nameiop == RENAME) {
    206 			error = EISDIR;
    207 			goto out;
    208 		}
    209 		vref(dvp);
    210 		*vpp = dvp;
    211 		error = 0;
    212 		goto done;
    213 	}
    214 
    215 	/*
    216 	 * Other lookup cases: perform directory scan.
    217 	 */
    218 	de = tmpfs_dir_lookup(dnode, cnp);
    219 	if (de == NULL || de->td_node == TMPFS_NODE_WHITEOUT) {
    220 		/*
    221 		 * The entry was not found in the directory.  This is valid
    222 		 * if we are creating or renaming an entry and are working
    223 		 * on the last component of the path name.
    224 		 */
    225 		if (lastcn && (cnp->cn_nameiop == CREATE ||
    226 		    cnp->cn_nameiop == RENAME)) {
    227 			error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred);
    228 			if (error) {
    229 				goto out;
    230 			}
    231 			error = EJUSTRETURN;
    232 		} else {
    233 			error = ENOENT;
    234 		}
    235 		if (de) {
    236 			KASSERT(de->td_node == TMPFS_NODE_WHITEOUT);
    237 			cnp->cn_flags |= ISWHITEOUT;
    238 		}
    239 		goto done;
    240 	}
    241 
    242 	tnode = de->td_node;
    243 
    244 	/*
    245 	 * If it is not the last path component and found a non-directory
    246 	 * or non-link entry (which may itself be pointing to a directory),
    247 	 * raise an error.
    248 	 */
    249 	if (!lastcn && tnode->tn_type != VDIR && tnode->tn_type != VLNK) {
    250 		error = ENOTDIR;
    251 		goto out;
    252 	}
    253 
    254 	/* Check the permissions. */
    255 	if (lastcn && (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
    256 		kauth_action_t action = 0;
    257 
    258 		/* This is the file-system's decision. */
    259 		if ((dnode->tn_mode & S_ISTXT) != 0 &&
    260 		    kauth_cred_geteuid(cnp->cn_cred) != dnode->tn_uid &&
    261 		    kauth_cred_geteuid(cnp->cn_cred) != tnode->tn_uid) {
    262 			error = EPERM;
    263 		} else {
    264 			error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred);
    265 		}
    266 
    267 		if (cnp->cn_nameiop == DELETE) {
    268 			action |= KAUTH_VNODE_DELETE;
    269 		} else {
    270 			KASSERT(cnp->cn_nameiop == RENAME);
    271 			action |= KAUTH_VNODE_RENAME;
    272 		}
    273 		error = kauth_authorize_vnode(cnp->cn_cred,
    274 		    action, *vpp, dvp, error);
    275 		if (error) {
    276 			goto out;
    277 		}
    278 	}
    279 
    280 	/* Get a vnode for the matching entry. */
    281 	mutex_enter(&tnode->tn_vlock);
    282 	error = tmpfs_vnode_get(dvp->v_mount, tnode, vpp);
    283 done:
    284 	/*
    285 	 * Cache the result, unless request was for creation (as it does
    286 	 * not improve the performance).
    287 	 */
    288 	if ((cnp->cn_flags & MAKEENTRY) != 0 && cnp->cn_nameiop != CREATE) {
    289 		cache_enter(dvp, *vpp, cnp);
    290 	}
    291 out:
    292 	KASSERT((*vpp && VOP_ISLOCKED(*vpp)) || error);
    293 	KASSERT(VOP_ISLOCKED(dvp));
    294 
    295 	return error;
    296 }
    297 
    298 int
    299 tmpfs_create(void *v)
    300 {
    301 	struct vop_create_args /* {
    302 		struct vnode		*a_dvp;
    303 		struct vnode		**a_vpp;
    304 		struct componentname	*a_cnp;
    305 		struct vattr		*a_vap;
    306 	} */ *ap = v;
    307 	vnode_t *dvp = ap->a_dvp, **vpp = ap->a_vpp;
    308 	struct componentname *cnp = ap->a_cnp;
    309 	struct vattr *vap = ap->a_vap;
    310 
    311 	KASSERT(VOP_ISLOCKED(dvp));
    312 	KASSERT(vap->va_type == VREG || vap->va_type == VSOCK);
    313 	return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
    314 }
    315 
    316 int
    317 tmpfs_mknod(void *v)
    318 {
    319 	struct vop_mknod_args /* {
    320 		struct vnode		*a_dvp;
    321 		struct vnode		**a_vpp;
    322 		struct componentname	*a_cnp;
    323 		struct vattr		*a_vap;
    324 	} */ *ap = v;
    325 	vnode_t *dvp = ap->a_dvp, **vpp = ap->a_vpp;
    326 	struct componentname *cnp = ap->a_cnp;
    327 	struct vattr *vap = ap->a_vap;
    328 	enum vtype vt = vap->va_type;
    329 
    330 	if (vt != VBLK && vt != VCHR && vt != VFIFO) {
    331 		vput(dvp);
    332 		return EINVAL;
    333 	}
    334 	return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
    335 }
    336 
    337 int
    338 tmpfs_open(void *v)
    339 {
    340 	struct vop_open_args /* {
    341 		struct vnode	*a_vp;
    342 		int		a_mode;
    343 		kauth_cred_t	a_cred;
    344 	} */ *ap = v;
    345 	vnode_t *vp = ap->a_vp;
    346 	mode_t mode = ap->a_mode;
    347 	tmpfs_node_t *node;
    348 
    349 	KASSERT(VOP_ISLOCKED(vp));
    350 
    351 	node = VP_TO_TMPFS_NODE(vp);
    352 	if (node->tn_links < 1) {
    353 		/*
    354 		 * The file is still active, but all its names have been
    355 		 * removed (e.g. by a "rmdir $(pwd)").  It cannot be opened
    356 		 * any more, as it is about to be destroyed.
    357 		 */
    358 		return ENOENT;
    359 	}
    360 
    361 	/* If the file is marked append-only, deny write requests. */
    362 	if ((node->tn_flags & APPEND) != 0 &&
    363 	    (mode & (FWRITE | O_APPEND)) == FWRITE) {
    364 		return EPERM;
    365 	}
    366 	return 0;
    367 }
    368 
    369 int
    370 tmpfs_close(void *v)
    371 {
    372 	struct vop_close_args /* {
    373 		struct vnode	*a_vp;
    374 		int		a_fflag;
    375 		kauth_cred_t	a_cred;
    376 	} */ *ap = v;
    377 	vnode_t *vp = ap->a_vp;
    378 
    379 	KASSERT(VOP_ISLOCKED(vp));
    380 
    381 	tmpfs_update(vp, NULL, NULL, NULL, UPDATE_CLOSE);
    382 	return 0;
    383 }
    384 
    385 static int
    386 tmpfs_check_possible(vnode_t *vp, tmpfs_node_t *node, mode_t mode)
    387 {
    388 	const bool writing = (mode & VWRITE) != 0;
    389 
    390 	switch (vp->v_type) {
    391 	case VDIR:
    392 	case VLNK:
    393 	case VREG:
    394 		if (writing && (vp->v_mount->mnt_flag & MNT_RDONLY) != 0) {
    395 			return EROFS;
    396 		}
    397 		break;
    398 	case VBLK:
    399 	case VCHR:
    400 	case VSOCK:
    401 	case VFIFO:
    402 		break;
    403 	default:
    404 		return EINVAL;
    405 	}
    406 	return (writing && (node->tn_flags & IMMUTABLE) != 0) ? EPERM : 0;
    407 }
    408 
    409 static int
    410 tmpfs_check_permitted(vnode_t *vp, tmpfs_node_t *node, mode_t mode,
    411     kauth_cred_t cred)
    412 {
    413 
    414 	return genfs_can_access(vp->v_type, node->tn_mode, node->tn_uid,
    415 	    node->tn_gid, mode, cred);
    416 }
    417 
    418 int
    419 tmpfs_access(void *v)
    420 {
    421 	struct vop_access_args /* {
    422 		struct vnode	*a_vp;
    423 		int		a_mode;
    424 		kauth_cred_t	a_cred;
    425 	} */ *ap = v;
    426 	vnode_t *vp = ap->a_vp;
    427 	mode_t mode = ap->a_mode;
    428 	kauth_cred_t cred = ap->a_cred;
    429 	tmpfs_node_t *node;
    430 	int error;
    431 
    432 	KASSERT(VOP_ISLOCKED(vp));
    433 
    434 	node = VP_TO_TMPFS_NODE(vp);
    435 	error = tmpfs_check_possible(vp, node, mode);
    436 	if (error) {
    437 		return error;
    438 	}
    439 	return kauth_authorize_vnode(cred, kauth_mode_to_action(mode), vp,
    440 	    NULL, tmpfs_check_permitted(vp, node, mode, cred));
    441 }
    442 
    443 int
    444 tmpfs_getattr(void *v)
    445 {
    446 	struct vop_getattr_args /* {
    447 		struct vnode	*a_vp;
    448 		struct vattr	*a_vap;
    449 		kauth_cred_t	a_cred;
    450 	} */ *ap = v;
    451 	vnode_t *vp = ap->a_vp;
    452 	struct vattr *vap = ap->a_vap;
    453 	tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp);
    454 
    455 	vattr_null(vap);
    456 
    457 	tmpfs_update(vp, NULL, NULL, NULL, 0);
    458 
    459 	vap->va_type = vp->v_type;
    460 	vap->va_mode = node->tn_mode;
    461 	vap->va_nlink = node->tn_links;
    462 	vap->va_uid = node->tn_uid;
    463 	vap->va_gid = node->tn_gid;
    464 	vap->va_fsid = vp->v_mount->mnt_stat.f_fsidx.__fsid_val[0];
    465 	vap->va_fileid = node->tn_id;
    466 	vap->va_size = node->tn_size;
    467 	vap->va_blocksize = PAGE_SIZE;
    468 	vap->va_atime = node->tn_atime;
    469 	vap->va_mtime = node->tn_mtime;
    470 	vap->va_ctime = node->tn_ctime;
    471 	vap->va_birthtime = node->tn_birthtime;
    472 	vap->va_gen = TMPFS_NODE_GEN(node);
    473 	vap->va_flags = node->tn_flags;
    474 	vap->va_rdev = (vp->v_type == VBLK || vp->v_type == VCHR) ?
    475 	    node->tn_spec.tn_dev.tn_rdev : VNOVAL;
    476 	vap->va_bytes = round_page(node->tn_size);
    477 	vap->va_filerev = VNOVAL;
    478 	vap->va_vaflags = 0;
    479 	vap->va_spare = VNOVAL; /* XXX */
    480 
    481 	return 0;
    482 }
    483 
    484 #define GOODTIME(tv)	((tv)->tv_sec != VNOVAL || (tv)->tv_nsec != VNOVAL)
    485 /* XXX Should this operation be atomic?  I think it should, but code in
    486  * XXX other places (e.g., ufs) doesn't seem to be... */
    487 int
    488 tmpfs_setattr(void *v)
    489 {
    490 	struct vop_setattr_args /* {
    491 		struct vnode	*a_vp;
    492 		struct vattr	*a_vap;
    493 		kauth_cred_t	a_cred;
    494 	} */ *ap = v;
    495 	vnode_t *vp = ap->a_vp;
    496 	struct vattr *vap = ap->a_vap;
    497 	kauth_cred_t cred = ap->a_cred;
    498 	lwp_t *l = curlwp;
    499 	int error = 0;
    500 
    501 	KASSERT(VOP_ISLOCKED(vp));
    502 
    503 	/* Abort if any unsettable attribute is given. */
    504 	if (vap->va_type != VNON || vap->va_nlink != VNOVAL ||
    505 	    vap->va_fsid != VNOVAL || vap->va_fileid != VNOVAL ||
    506 	    vap->va_blocksize != VNOVAL || GOODTIME(&vap->va_ctime) ||
    507 	    vap->va_gen != VNOVAL || vap->va_rdev != VNOVAL ||
    508 	    vap->va_bytes != VNOVAL) {
    509 		return EINVAL;
    510 	}
    511 	if (error == 0 && (vap->va_flags != VNOVAL))
    512 		error = tmpfs_chflags(vp, vap->va_flags, cred, l);
    513 
    514 	if (error == 0 && (vap->va_size != VNOVAL))
    515 		error = tmpfs_chsize(vp, vap->va_size, cred, l);
    516 
    517 	if (error == 0 && (vap->va_uid != VNOVAL || vap->va_gid != VNOVAL))
    518 		error = tmpfs_chown(vp, vap->va_uid, vap->va_gid, cred, l);
    519 
    520 	if (error == 0 && (vap->va_mode != VNOVAL))
    521 		error = tmpfs_chmod(vp, vap->va_mode, cred, l);
    522 
    523 	if (error == 0 && (GOODTIME(&vap->va_atime) || GOODTIME(&vap->va_mtime)
    524 	    || GOODTIME(&vap->va_birthtime))) {
    525 		error = tmpfs_chtimes(vp, &vap->va_atime, &vap->va_mtime,
    526 		    &vap->va_birthtime, vap->va_vaflags, cred, l);
    527 		if (error == 0)
    528 			return 0;
    529 	}
    530 	tmpfs_update(vp, NULL, NULL, NULL, 0);
    531 	return error;
    532 }
    533 
    534 int
    535 tmpfs_read(void *v)
    536 {
    537 	struct vop_read_args /* {
    538 		struct vnode *a_vp;
    539 		struct uio *a_uio;
    540 		int a_ioflag;
    541 		kauth_cred_t a_cred;
    542 	} */ *ap = v;
    543 	vnode_t *vp = ap->a_vp;
    544 	struct uio *uio = ap->a_uio;
    545 	const int ioflag = ap->a_ioflag;
    546 	tmpfs_node_t *node;
    547 	struct uvm_object *uobj;
    548 	int error;
    549 
    550 	KASSERT(VOP_ISLOCKED(vp));
    551 
    552 	if (vp->v_type != VREG) {
    553 		return EISDIR;
    554 	}
    555 	if (uio->uio_offset < 0) {
    556 		return EINVAL;
    557 	}
    558 
    559 	node = VP_TO_TMPFS_NODE(vp);
    560 	node->tn_status |= TMPFS_NODE_ACCESSED;
    561 	uobj = node->tn_spec.tn_reg.tn_aobj;
    562 	error = 0;
    563 
    564 	if (uio->uio_offset + uio->uio_resid <= node->tn_size) {
    565 		uvm_loanobj(&vp->v_uobj, uio);
    566 	}
    567 	while (error == 0 && uio->uio_resid > 0) {
    568 		vsize_t len;
    569 
    570 		if (node->tn_size <= uio->uio_offset) {
    571 			break;
    572 		}
    573 		len = MIN(node->tn_size - uio->uio_offset, uio->uio_resid);
    574 		if (len == 0) {
    575 			break;
    576 		}
    577 		error = ubc_uiomove(uobj, uio, len, IO_ADV_DECODE(ioflag),
    578 		    UBC_READ | UBC_PARTIALOK | UBC_UNMAP_FLAG(vp));
    579 	}
    580 	return error;
    581 }
    582 
    583 int
    584 tmpfs_write(void *v)
    585 {
    586 	struct vop_write_args /* {
    587 		struct vnode	*a_vp;
    588 		struct uio	*a_uio;
    589 		int		a_ioflag;
    590 		kauth_cred_t	a_cred;
    591 	} */ *ap = v;
    592 	vnode_t *vp = ap->a_vp;
    593 	struct uio *uio = ap->a_uio;
    594 	const int ioflag = ap->a_ioflag;
    595 	tmpfs_node_t *node;
    596 	struct uvm_object *uobj;
    597 	off_t oldsize;
    598 	bool extended;
    599 	int error;
    600 
    601 	KASSERT(VOP_ISLOCKED(vp));
    602 
    603 	node = VP_TO_TMPFS_NODE(vp);
    604 	oldsize = node->tn_size;
    605 
    606 	if (uio->uio_offset < 0 || vp->v_type != VREG) {
    607 		error = EINVAL;
    608 		goto out;
    609 	}
    610 	if (uio->uio_resid == 0) {
    611 		error = 0;
    612 		goto out;
    613 	}
    614 	if (ioflag & IO_APPEND) {
    615 		uio->uio_offset = node->tn_size;
    616 	}
    617 
    618 	extended = uio->uio_offset + uio->uio_resid > node->tn_size;
    619 	if (extended) {
    620 		error = tmpfs_reg_resize(vp, uio->uio_offset + uio->uio_resid);
    621 		if (error)
    622 			goto out;
    623 	}
    624 
    625 	uobj = node->tn_spec.tn_reg.tn_aobj;
    626 	error = 0;
    627 	while (error == 0 && uio->uio_resid > 0) {
    628 		vsize_t len;
    629 
    630 		len = MIN(node->tn_size - uio->uio_offset, uio->uio_resid);
    631 		if (len == 0) {
    632 			break;
    633 		}
    634 		error = ubc_uiomove(uobj, uio, len, IO_ADV_DECODE(ioflag),
    635 		    UBC_WRITE | UBC_UNMAP_FLAG(vp));
    636 	}
    637 	if (error) {
    638 		(void)tmpfs_reg_resize(vp, oldsize);
    639 	}
    640 
    641 	node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED |
    642 	    (extended ? TMPFS_NODE_CHANGED : 0);
    643 	VN_KNOTE(vp, NOTE_WRITE);
    644 out:
    645 	if (error) {
    646 		KASSERT(oldsize == node->tn_size);
    647 	} else {
    648 		KASSERT(uio->uio_resid == 0);
    649 	}
    650 	return error;
    651 }
    652 
    653 int
    654 tmpfs_fsync(void *v)
    655 {
    656 	struct vop_fsync_args /* {
    657 		struct vnode *a_vp;
    658 		kauth_cred_t a_cred;
    659 		int a_flags;
    660 		off_t a_offlo;
    661 		off_t a_offhi;
    662 		struct lwp *a_l;
    663 	} */ *ap = v;
    664 	vnode_t *vp = ap->a_vp;
    665 
    666 	/* Nothing to do.  Just update. */
    667 	KASSERT(VOP_ISLOCKED(vp));
    668 	tmpfs_update(vp, NULL, NULL, NULL, 0);
    669 	return 0;
    670 }
    671 
    672 /*
    673  * tmpfs_remove: unlink a file.
    674  *
    675  * => Both directory (dvp) and file (vp) are locked.
    676  * => We unlock and drop the reference on both.
    677  */
    678 int
    679 tmpfs_remove(void *v)
    680 {
    681 	struct vop_remove_args /* {
    682 		struct vnode *a_dvp;
    683 		struct vnode *a_vp;
    684 		struct componentname *a_cnp;
    685 	} */ *ap = v;
    686 	vnode_t *dvp = ap->a_dvp, *vp = ap->a_vp;
    687 	tmpfs_node_t *node;
    688 	tmpfs_dirent_t *de;
    689 	int error;
    690 
    691 	KASSERT(VOP_ISLOCKED(dvp));
    692 	KASSERT(VOP_ISLOCKED(vp));
    693 
    694 	if (vp->v_type == VDIR) {
    695 		error = EPERM;
    696 		goto out;
    697 	}
    698 	node = VP_TO_TMPFS_NODE(vp);
    699 
    700 	/* Files marked as immutable or append-only cannot be deleted. */
    701 	if (node->tn_flags & (IMMUTABLE | APPEND)) {
    702 		error = EPERM;
    703 		goto out;
    704 	}
    705 
    706 	/* Lookup the directory entry (check the cached hint first). */
    707 	de = tmpfs_dir_cached(node);
    708 	if (de == NULL) {
    709 		tmpfs_node_t *dnode = VP_TO_TMPFS_DIR(dvp);
    710 		struct componentname *cnp = ap->a_cnp;
    711 		de = tmpfs_dir_lookup(dnode, cnp);
    712 	}
    713 	KASSERT(de && de->td_node == node);
    714 
    715 	/*
    716 	 * Remove the entry from the directory (drops the link count) and
    717 	 * destroy it or replace it with a whiteout.
    718 	 * Note: the inode referred by it will not be destroyed
    719 	 * until the vnode is reclaimed/recycled.
    720 	 */
    721 	tmpfs_dir_detach(dvp, de);
    722 	if (ap->a_cnp->cn_flags & DOWHITEOUT)
    723 		tmpfs_dir_attach(dvp, de, TMPFS_NODE_WHITEOUT);
    724 	else
    725 		tmpfs_free_dirent(VFS_TO_TMPFS(vp->v_mount), de);
    726 	error = 0;
    727 out:
    728 	/* Drop the references and unlock the vnodes. */
    729 	vput(vp);
    730 	if (dvp == vp) {
    731 		vrele(dvp);
    732 	} else {
    733 		vput(dvp);
    734 	}
    735 	return error;
    736 }
    737 
    738 /*
    739  * tmpfs_link: create a hard link.
    740  */
    741 int
    742 tmpfs_link(void *v)
    743 {
    744 	struct vop_link_args /* {
    745 		struct vnode *a_dvp;
    746 		struct vnode *a_vp;
    747 		struct componentname *a_cnp;
    748 	} */ *ap = v;
    749 	vnode_t *dvp = ap->a_dvp;
    750 	vnode_t *vp = ap->a_vp;
    751 	struct componentname *cnp = ap->a_cnp;
    752 	tmpfs_node_t *dnode, *node;
    753 	tmpfs_dirent_t *de;
    754 	int error;
    755 
    756 	KASSERT(dvp != vp);
    757 	KASSERT(VOP_ISLOCKED(dvp));
    758 	KASSERT(vp->v_type != VDIR);
    759 	KASSERT(dvp->v_mount == vp->v_mount);
    760 
    761 	dnode = VP_TO_TMPFS_DIR(dvp);
    762 	node = VP_TO_TMPFS_NODE(vp);
    763 
    764 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    765 
    766 	/* Check for maximum number of links limit. */
    767 	if (node->tn_links == LINK_MAX) {
    768 		error = EMLINK;
    769 		goto out;
    770 	}
    771 	KASSERT(node->tn_links < LINK_MAX);
    772 
    773 	/* We cannot create links of files marked immutable or append-only. */
    774 	if (node->tn_flags & (IMMUTABLE | APPEND)) {
    775 		error = EPERM;
    776 		goto out;
    777 	}
    778 
    779 	/* Allocate a new directory entry to represent the inode. */
    780 	error = tmpfs_alloc_dirent(VFS_TO_TMPFS(vp->v_mount),
    781 	    cnp->cn_nameptr, cnp->cn_namelen, &de);
    782 	if (error) {
    783 		goto out;
    784 	}
    785 
    786 	/*
    787 	 * Insert the entry into the directory.
    788 	 * It will increase the inode link count.
    789 	 */
    790 	tmpfs_dir_attach(dvp, de, node);
    791 
    792 	/* Update the timestamps and trigger the event. */
    793 	if (node->tn_vnode) {
    794 		VN_KNOTE(node->tn_vnode, NOTE_LINK);
    795 	}
    796 	node->tn_status |= TMPFS_NODE_CHANGED;
    797 	tmpfs_update(vp, NULL, NULL, NULL, 0);
    798 	error = 0;
    799 out:
    800 	VOP_UNLOCK(vp);
    801 	vput(dvp);
    802 	return error;
    803 }
    804 
    805 /*
    806  * tmpfs_rename: rename routine, the hairiest system call, with the
    807  * insane API.
    808  *
    809  * Arguments: fdvp (from-parent vnode), fvp (from-leaf), tdvp (to-parent)
    810  * and tvp (to-leaf), if exists (NULL if not).
    811  *
    812  * => Caller holds a reference on fdvp and fvp, they are unlocked.
    813  *    Note: fdvp and fvp can refer to the same object (i.e. when it is root).
    814  *
    815  * => Both tdvp and tvp are referenced and locked.  It is our responsibility
    816  *    to release the references and unlock them (or destroy).
    817  */
    818 
    819 /*
    820  * First, some forward declarations of subroutines.
    821  */
    822 
    823 static int tmpfs_sane_rename(struct vnode *, struct componentname *,
    824     struct vnode *, struct componentname *, kauth_cred_t, bool);
    825 static int tmpfs_rename_enter(struct mount *, struct tmpfs_mount *,
    826     kauth_cred_t,
    827     struct vnode *, struct tmpfs_node *, struct componentname *,
    828     struct tmpfs_dirent **, struct vnode **,
    829     struct vnode *, struct tmpfs_node *, struct componentname *,
    830     struct tmpfs_dirent **, struct vnode **);
    831 static int tmpfs_rename_enter_common(struct mount *, struct tmpfs_mount *,
    832     kauth_cred_t,
    833     struct vnode *, struct tmpfs_node *,
    834     struct componentname *, struct tmpfs_dirent **, struct vnode **,
    835     struct componentname *, struct tmpfs_dirent **, struct vnode **);
    836 static int tmpfs_rename_enter_separate(struct mount *, struct tmpfs_mount *,
    837     kauth_cred_t,
    838     struct vnode *, struct tmpfs_node *, struct componentname *,
    839     struct tmpfs_dirent **, struct vnode **,
    840     struct vnode *, struct tmpfs_node *, struct componentname *,
    841     struct tmpfs_dirent **, struct vnode **);
    842 static void tmpfs_rename_exit(struct tmpfs_mount *,
    843     struct vnode *, struct vnode *, struct vnode *, struct vnode *);
    844 static int tmpfs_rename_lock_directory(struct vnode *, struct tmpfs_node *);
    845 static int tmpfs_rename_genealogy(struct tmpfs_node *, struct tmpfs_node *,
    846     struct tmpfs_node **);
    847 static int tmpfs_rename_lock(struct mount *, kauth_cred_t, int,
    848     struct vnode *, struct tmpfs_node *, struct componentname *, bool,
    849     struct tmpfs_dirent **, struct vnode **,
    850     struct vnode *, struct tmpfs_node *, struct componentname *, bool,
    851     struct tmpfs_dirent **, struct vnode **);
    852 static void tmpfs_rename_attachdetach(struct tmpfs_mount *,
    853     struct vnode *, struct tmpfs_dirent *, struct vnode *,
    854     struct vnode *, struct tmpfs_dirent *, struct vnode *);
    855 static int tmpfs_do_remove(struct tmpfs_mount *, struct vnode *,
    856     struct tmpfs_node *, struct tmpfs_dirent *, struct vnode *, kauth_cred_t);
    857 static int tmpfs_rename_check_possible(struct tmpfs_node *,
    858     struct tmpfs_node *, struct tmpfs_node *, struct tmpfs_node *);
    859 static int tmpfs_rename_check_permitted(kauth_cred_t,
    860     struct tmpfs_node *, struct tmpfs_node *,
    861     struct tmpfs_node *, struct tmpfs_node *);
    862 static int tmpfs_remove_check_possible(struct tmpfs_node *,
    863     struct tmpfs_node *);
    864 static int tmpfs_remove_check_permitted(kauth_cred_t,
    865     struct tmpfs_node *, struct tmpfs_node *);
    866 static int tmpfs_check_sticky(kauth_cred_t,
    867     struct tmpfs_node *, struct tmpfs_node *);
    868 
    869 int
    870 tmpfs_rename(void *v)
    871 {
    872 	struct vop_rename_args  /* {
    873 		struct vnode		*a_fdvp;
    874 		struct vnode		*a_fvp;
    875 		struct componentname	*a_fcnp;
    876 		struct vnode		*a_tdvp;
    877 		struct vnode		*a_tvp;
    878 		struct componentname	*a_tcnp;
    879 	} */ *ap = v;
    880 	struct vnode *fdvp = ap->a_fdvp;
    881 	struct vnode *fvp = ap->a_fvp;
    882 	struct componentname *fcnp = ap->a_fcnp;
    883 	struct vnode *tdvp = ap->a_tdvp;
    884 	struct vnode *tvp = ap->a_tvp;
    885 	struct componentname *tcnp = ap->a_tcnp;
    886 	kauth_cred_t cred;
    887 	int error;
    888 
    889 	KASSERT(fdvp != NULL);
    890 	KASSERT(fvp != NULL);
    891 	KASSERT(fcnp != NULL);
    892 	KASSERT(fcnp->cn_nameptr != NULL);
    893 	KASSERT(tdvp != NULL);
    894 	KASSERT(tcnp != NULL);
    895 	KASSERT(fcnp->cn_nameptr != NULL);
    896 	/* KASSERT(VOP_ISLOCKED(fdvp) != LK_EXCLUSIVE); */
    897 	/* KASSERT(VOP_ISLOCKED(fvp) != LK_EXCLUSIVE); */
    898 	KASSERT(VOP_ISLOCKED(tdvp) == LK_EXCLUSIVE);
    899 	KASSERT((tvp == NULL) || (VOP_ISLOCKED(tvp) == LK_EXCLUSIVE));
    900 	KASSERT(fdvp->v_type == VDIR);
    901 	KASSERT(tdvp->v_type == VDIR);
    902 
    903 	cred = fcnp->cn_cred;
    904 	KASSERT(tcnp->cn_cred == cred);
    905 
    906 	/*
    907 	 * Sanitize our world from the VFS insanity.  Unlock the target
    908 	 * directory and node, which are locked.  Release the children,
    909 	 * which are referenced.  Check for rename("x", "y/."), which
    910 	 * it is our responsibility to reject, not the caller's.  (But
    911 	 * the caller does reject rename("x/.", "y").  Go figure.)
    912 	 */
    913 
    914 	VOP_UNLOCK(tdvp);
    915 	if ((tvp != NULL) && (tvp != tdvp))
    916 		VOP_UNLOCK(tvp);
    917 
    918 	vrele(fvp);
    919 	if (tvp != NULL)
    920 		vrele(tvp);
    921 
    922 	if (tvp == tdvp) {
    923 		error = EINVAL;
    924 		goto out;
    925 	}
    926 
    927 	error = tmpfs_sane_rename(fdvp, fcnp, tdvp, tcnp, cred, false);
    928 
    929 out:	/*
    930 	 * All done, whether with success or failure.  Release the
    931 	 * directory nodes now, as the caller expects from the VFS
    932 	 * protocol.
    933 	 */
    934 	vrele(fdvp);
    935 	vrele(tdvp);
    936 
    937 	return error;
    938 }
    939 
    940 /*
    941  * tmpfs_sane_rename: rename routine, the hairiest system call, with
    942  * the sane API.
    943  *
    944  * Arguments:
    945  *
    946  * . fdvp (from directory vnode),
    947  * . fcnp (from component name),
    948  * . tdvp (to directory vnode), and
    949  * . tcnp (to component name).
    950  *
    951  * fdvp and tdvp must be referenced and unlocked.
    952  */
    953 static int
    954 tmpfs_sane_rename(struct vnode *fdvp, struct componentname *fcnp,
    955     struct vnode *tdvp, struct componentname *tcnp, kauth_cred_t cred,
    956     bool posixly_correct)
    957 {
    958 	struct mount *mount;
    959 	struct tmpfs_mount *tmpfs;
    960 	struct tmpfs_node *fdnode, *tdnode;
    961 	struct tmpfs_dirent *fde, *tde;
    962 	struct vnode *fvp, *tvp;
    963 	char *newname;
    964 	int error;
    965 
    966 	KASSERT(fdvp != NULL);
    967 	KASSERT(fcnp != NULL);
    968 	KASSERT(tdvp != NULL);
    969 	KASSERT(tcnp != NULL);
    970 	/* KASSERT(VOP_ISLOCKED(fdvp) != LK_EXCLUSIVE); */
    971 	/* KASSERT(VOP_ISLOCKED(tdvp) != LK_EXCLUSIVE); */
    972 	KASSERT(fdvp->v_type == VDIR);
    973 	KASSERT(tdvp->v_type == VDIR);
    974 	KASSERT(fdvp->v_mount == tdvp->v_mount);
    975 	KASSERT((fcnp->cn_flags & ISDOTDOT) == 0);
    976 	KASSERT((tcnp->cn_flags & ISDOTDOT) == 0);
    977 	KASSERT((fcnp->cn_namelen != 1) || (fcnp->cn_nameptr[0] != '.'));
    978 	KASSERT((tcnp->cn_namelen != 1) || (tcnp->cn_nameptr[0] != '.'));
    979 	KASSERT((fcnp->cn_namelen != 2) || (fcnp->cn_nameptr[0] != '.') ||
    980 	    (fcnp->cn_nameptr[1] != '.'));
    981 	KASSERT((tcnp->cn_namelen != 2) || (tcnp->cn_nameptr[0] != '.') ||
    982 	    (tcnp->cn_nameptr[1] != '.'));
    983 
    984 	/*
    985 	 * Pull out the tmpfs data structures.
    986 	 */
    987 	fdnode = VP_TO_TMPFS_NODE(fdvp);
    988 	tdnode = VP_TO_TMPFS_NODE(tdvp);
    989 	KASSERT(fdnode != NULL);
    990 	KASSERT(tdnode != NULL);
    991 	KASSERT(fdnode->tn_vnode == fdvp);
    992 	KASSERT(tdnode->tn_vnode == tdvp);
    993 	KASSERT(fdnode->tn_type == VDIR);
    994 	KASSERT(tdnode->tn_type == VDIR);
    995 
    996 	mount = fdvp->v_mount;
    997 	KASSERT(mount != NULL);
    998 	KASSERT(mount == tdvp->v_mount);
    999 	/* XXX How can we be sure this stays true?  (Not that you're
   1000 	 * likely to mount a tmpfs read-only...)  */
   1001 	KASSERT((mount->mnt_flag & MNT_RDONLY) == 0);
   1002 	tmpfs = VFS_TO_TMPFS(mount);
   1003 	KASSERT(tmpfs != NULL);
   1004 
   1005 	/*
   1006 	 * Decide whether we need a new name, and allocate memory for
   1007 	 * it if so.  Do this before locking anything or taking
   1008 	 * destructive actions so that we can back out safely and sleep
   1009 	 * safely.  XXX Is sleeping an issue here?  Can this just be
   1010 	 * moved into tmpfs_rename_attachdetach?
   1011 	 */
   1012 	if (tmpfs_strname_neqlen(fcnp, tcnp)) {
   1013 		newname = tmpfs_strname_alloc(tmpfs, tcnp->cn_namelen);
   1014 		if (newname == NULL) {
   1015 			error = ENOSPC;
   1016 			goto out_unlocked;
   1017 		}
   1018 	} else {
   1019 		newname = NULL;
   1020 	}
   1021 
   1022 	/*
   1023 	 * Lock and look up everything.  GCC is not very clever.
   1024 	 */
   1025 	fde = tde = NULL;
   1026 	fvp = tvp = NULL;
   1027 	error = tmpfs_rename_enter(mount, tmpfs, cred,
   1028 	    fdvp, fdnode, fcnp, &fde, &fvp,
   1029 	    tdvp, tdnode, tcnp, &tde, &tvp);
   1030 	if (error)
   1031 		goto out_unlocked;
   1032 
   1033 	/*
   1034 	 * Check that everything is locked and looks right.
   1035 	 */
   1036 	KASSERT(fde != NULL);
   1037 	KASSERT(fvp != NULL);
   1038 	KASSERT(fde->td_node != NULL);
   1039 	KASSERT(fde->td_node->tn_vnode == fvp);
   1040 	KASSERT(fde->td_node->tn_type == fvp->v_type);
   1041 	KASSERT((tde == NULL) == (tvp == NULL));
   1042 	KASSERT((tde == NULL) || (tde->td_node != NULL));
   1043 	KASSERT((tde == NULL) || (tde->td_node->tn_vnode == tvp));
   1044 	KASSERT((tde == NULL) || (tde->td_node->tn_type == tvp->v_type));
   1045 	KASSERT(VOP_ISLOCKED(fdvp) == LK_EXCLUSIVE);
   1046 	KASSERT(VOP_ISLOCKED(tdvp) == LK_EXCLUSIVE);
   1047 	KASSERT(VOP_ISLOCKED(fvp) == LK_EXCLUSIVE);
   1048 	KASSERT((tvp == NULL) || (VOP_ISLOCKED(tvp) == LK_EXCLUSIVE));
   1049 
   1050 	/*
   1051 	 * If the source and destination are the same object, we need
   1052 	 * only at most delete the source entry.
   1053 	 */
   1054 	if (fvp == tvp) {
   1055 		KASSERT(tvp != NULL);
   1056 		if (fde->td_node->tn_type == VDIR) {
   1057 			/* XXX How can this possibly happen?  */
   1058 			error = EINVAL;
   1059 			goto out_locked;
   1060 		}
   1061 		if (!posixly_correct && (fde != tde)) {
   1062 			/* XXX Doesn't work because of locking.
   1063 			 * error = VOP_REMOVE(fdvp, fvp);
   1064 			 */
   1065 			error = tmpfs_do_remove(tmpfs, fdvp, fdnode, fde, fvp,
   1066 			    cred);
   1067 			if (error)
   1068 				goto out_locked;
   1069 		}
   1070 		goto success;
   1071 	}
   1072 	KASSERT(fde != tde);
   1073 	KASSERT(fvp != tvp);
   1074 
   1075 	/*
   1076 	 * If the target exists, refuse to rename a directory over a
   1077 	 * non-directory or vice versa, or to clobber a non-empty
   1078 	 * directory.
   1079 	 */
   1080 	if (tvp != NULL) {
   1081 		KASSERT(tde != NULL);
   1082 		KASSERT(tde->td_node != NULL);
   1083 		if (fvp->v_type == VDIR && tvp->v_type == VDIR)
   1084 			error = ((tde->td_node->tn_size > 0)? ENOTEMPTY : 0);
   1085 		else if (fvp->v_type == VDIR && tvp->v_type != VDIR)
   1086 			error = ENOTDIR;
   1087 		else if (fvp->v_type != VDIR && tvp->v_type == VDIR)
   1088 			error = EISDIR;
   1089 		else
   1090 			error = 0;
   1091 		if (error)
   1092 			goto out_locked;
   1093 		KASSERT((fvp->v_type == VDIR) == (tvp->v_type == VDIR));
   1094 	}
   1095 
   1096 	/*
   1097 	 * Authorize the rename.
   1098 	 */
   1099 	error = tmpfs_rename_check_possible(fdnode, fde->td_node,
   1100 	    tdnode, (tde? tde->td_node : NULL));
   1101 	if (error)
   1102 		goto out_locked;
   1103 	error = tmpfs_rename_check_permitted(cred, fdnode, fde->td_node,
   1104 	    tdnode, (tde? tde->td_node : NULL));
   1105 	error = kauth_authorize_vnode(cred, KAUTH_VNODE_DELETE, fvp, fdvp,
   1106 	    error);
   1107 	error = kauth_authorize_vnode(cred, KAUTH_VNODE_RENAME, tvp, tdvp,
   1108 	    error);
   1109 	if (error)
   1110 		goto out_locked;
   1111 
   1112 	/*
   1113 	 * Everything is hunky-dory.  Shuffle the directory entries.
   1114 	 */
   1115 	tmpfs_rename_attachdetach(tmpfs, fdvp, fde, fvp, tdvp, tde, tvp);
   1116 
   1117 	/*
   1118 	 * Update the directory entry's name necessary, and flag
   1119 	 * metadata updates.  A memory allocation failure here is not
   1120 	 * OK because we've already committed some changes that we
   1121 	 * can't back out at this point, and we have things locked so
   1122 	 * we can't sleep, hence the early allocation above.
   1123 	 */
   1124 	if (newname != NULL) {
   1125 		KASSERT(tcnp->cn_namelen <= TMPFS_MAXNAMLEN);
   1126 
   1127 		tmpfs_strname_free(tmpfs, fde->td_name, fde->td_namelen);
   1128 		fde->td_namelen = (uint16_t)tcnp->cn_namelen;
   1129 		(void)memcpy(newname, tcnp->cn_nameptr, tcnp->cn_namelen);
   1130 		/* Commit newname and don't free it on the way out.  */
   1131 		fde->td_name = newname;
   1132 		newname = NULL;
   1133 
   1134 		fde->td_node->tn_status |= TMPFS_NODE_CHANGED;
   1135 		tdnode->tn_status |= TMPFS_NODE_MODIFIED;
   1136 	}
   1137 
   1138 success:
   1139 	VN_KNOTE(fvp, NOTE_RENAME);
   1140 	error = 0;
   1141 
   1142 out_locked:
   1143 	tmpfs_rename_exit(tmpfs, fdvp, fvp, tdvp, tvp);
   1144 
   1145 out_unlocked:
   1146 	/* KASSERT(VOP_ISLOCKED(fdvp) != LK_EXCLUSIVE); */
   1147 	/* KASSERT(VOP_ISLOCKED(tdvp) != LK_EXCLUSIVE); */
   1148 	/* KASSERT((fvp == NULL) || (VOP_ISLOCKED(fvp) != LK_EXCLUSIVE)); */
   1149 	/* KASSERT((tvp == NULL) || (VOP_ISLOCKED(tvp) != LK_EXCLUSIVE)); */
   1150 
   1151 	if (newname != NULL)
   1152 		tmpfs_strname_free(tmpfs, newname, tcnp->cn_namelen);
   1153 
   1154 	return error;
   1155 }
   1156 
   1157 /*
   1158  * Look up fcnp in fdnode/fdvp and store its directory entry in fde_ret
   1159  * and the associated vnode in fvp_ret; fail if not found.  Look up
   1160  * tcnp in tdnode/tdvp and store its directory entry in tde_ret and the
   1161  * associated vnode in tvp_ret; store null instead if not found.  Fail
   1162  * if anything has been mounted on any of the nodes involved.
   1163  *
   1164  * fdvp and tdvp must be referenced.
   1165  *
   1166  * On entry, nothing is locked.
   1167  *
   1168  * On success, everything is locked, and *fvp_ret, and *tvp_ret if
   1169  * nonnull, are referenced.  The only pairs of vnodes that may be
   1170  * identical are {fdvp, tdvp} and {fvp, tvp}.
   1171  *
   1172  * On failure, everything remains as was.
   1173  *
   1174  * Locking everything including the source and target nodes is
   1175  * necessary to make sure that, e.g., link count updates are OK.  The
   1176  * locking order is, in general, ancestor-first, matching the order you
   1177  * need to use to look up a descendant anyway.
   1178  */
   1179 static int
   1180 tmpfs_rename_enter(struct mount *mount, struct tmpfs_mount *tmpfs,
   1181     kauth_cred_t cred,
   1182     struct vnode *fdvp, struct tmpfs_node *fdnode, struct componentname *fcnp,
   1183     struct tmpfs_dirent **fde_ret, struct vnode **fvp_ret,
   1184     struct vnode *tdvp, struct tmpfs_node *tdnode, struct componentname *tcnp,
   1185     struct tmpfs_dirent **tde_ret, struct vnode **tvp_ret)
   1186 {
   1187 	int error;
   1188 
   1189 	KASSERT(mount != NULL);
   1190 	KASSERT(tmpfs != NULL);
   1191 	KASSERT(fdvp != NULL);
   1192 	KASSERT(fdnode != NULL);
   1193 	KASSERT(fcnp != NULL);
   1194 	KASSERT(fde_ret != NULL);
   1195 	KASSERT(fvp_ret != NULL);
   1196 	KASSERT(tdvp != NULL);
   1197 	KASSERT(tdnode != NULL);
   1198 	KASSERT(tcnp != NULL);
   1199 	KASSERT(tde_ret != NULL);
   1200 	KASSERT(tvp_ret != NULL);
   1201 	KASSERT(fdnode->tn_vnode == fdvp);
   1202 	KASSERT(tdnode->tn_vnode == tdvp);
   1203 	KASSERT(fdnode->tn_type == VDIR);
   1204 	KASSERT(tdnode->tn_type == VDIR);
   1205 
   1206 	if (fdvp == tdvp) {
   1207 		KASSERT(fdnode == tdnode);
   1208 		error = tmpfs_rename_enter_common(mount, tmpfs, cred, fdvp,
   1209 		    fdnode, fcnp, fde_ret, fvp_ret, tcnp, tde_ret, tvp_ret);
   1210 	} else {
   1211 		KASSERT(fdnode != tdnode);
   1212 		error = tmpfs_rename_enter_separate(mount, tmpfs, cred,
   1213 		    fdvp, fdnode, fcnp, fde_ret, fvp_ret,
   1214 		    tdvp, tdnode, tcnp, tde_ret, tvp_ret);
   1215 	}
   1216 
   1217 	if (error)
   1218 		return error;
   1219 
   1220 	KASSERT(*fde_ret != NULL);
   1221 	KASSERT(*fvp_ret != NULL);
   1222 	KASSERT((*tde_ret == NULL) == (*tvp_ret == NULL));
   1223 	KASSERT((*tde_ret == NULL) || ((*tde_ret)->td_node != NULL));
   1224 	KASSERT((*tde_ret == NULL) ||
   1225 	    ((*tde_ret)->td_node->tn_vnode == *tvp_ret));
   1226 	KASSERT(VOP_ISLOCKED(fdvp) == LK_EXCLUSIVE);
   1227 	KASSERT(VOP_ISLOCKED(*fvp_ret) == LK_EXCLUSIVE);
   1228 	KASSERT(VOP_ISLOCKED(tdvp) == LK_EXCLUSIVE);
   1229 	KASSERT((*tvp_ret == NULL) ||
   1230 	    (VOP_ISLOCKED(*tvp_ret) == LK_EXCLUSIVE));
   1231 	KASSERT(*fvp_ret != fdvp);
   1232 	KASSERT(*fvp_ret != tdvp);
   1233 	KASSERT(*tvp_ret != fdvp);
   1234 	KASSERT(*tvp_ret != tdvp);
   1235 	return 0;
   1236 }
   1237 
   1238 /*
   1239  * Lock and look up with a common source/target directory.
   1240  */
   1241 static int
   1242 tmpfs_rename_enter_common(struct mount *mount, struct tmpfs_mount *tmpfs,
   1243     kauth_cred_t cred,
   1244     struct vnode *dvp, struct tmpfs_node *dnode,
   1245     struct componentname *fcnp,
   1246     struct tmpfs_dirent **fde_ret, struct vnode **fvp_ret,
   1247     struct componentname *tcnp,
   1248     struct tmpfs_dirent **tde_ret, struct vnode **tvp_ret)
   1249 {
   1250 	struct tmpfs_dirent *fde, *tde;
   1251 	struct vnode *fvp, *tvp;
   1252 	int error;
   1253 
   1254 	error = tmpfs_rename_lock_directory(dvp, dnode);
   1255 	if (error)
   1256 		goto fail0;
   1257 
   1258 	/* Did we lose a race with mount?  */
   1259 	if (dvp->v_mountedhere != NULL) {
   1260 		error = EBUSY;
   1261 		goto fail1;
   1262 	}
   1263 
   1264 	/* Make sure the caller may read the directory.  */
   1265 	error = VOP_ACCESS(dvp, VEXEC, cred);
   1266 	if (error)
   1267 		goto fail1;
   1268 
   1269 	/*
   1270 	 * The order in which we lock the source and target nodes is
   1271 	 * irrelevant because there can only be one rename on this
   1272 	 * directory in flight at a time, and we have it locked.
   1273 	 */
   1274 
   1275 	fde = tmpfs_dir_lookup(dnode, fcnp);
   1276 	if (fde == NULL) {
   1277 		error = ENOENT;
   1278 		goto fail1;
   1279 	}
   1280 
   1281 	KASSERT(fde->td_node != NULL);
   1282 	/* We ruled out `.' earlier.  */
   1283 	KASSERT(fde->td_node != dnode);
   1284 	/* We ruled out `..' earlier.  */
   1285 	KASSERT(fde->td_node != dnode->tn_spec.tn_dir.tn_parent);
   1286 	mutex_enter(&fde->td_node->tn_vlock);
   1287 	error = tmpfs_vnode_get(mount, fde->td_node, &fvp);
   1288 	if (error)
   1289 		goto fail1;
   1290 	KASSERT(fvp != NULL);
   1291 	KASSERT(VOP_ISLOCKED(fvp) == LK_EXCLUSIVE);
   1292 	KASSERT(fvp != dvp);
   1293 	KASSERT(fvp->v_mount == mount);
   1294 
   1295 	/* Refuse to rename a mount point.  */
   1296 	if ((fvp->v_type == VDIR) && (fvp->v_mountedhere != NULL)) {
   1297 		error = EBUSY;
   1298 		goto fail2;
   1299 	}
   1300 
   1301 	tde = tmpfs_dir_lookup(dnode, tcnp);
   1302 	if (tde == NULL) {
   1303 		tvp = NULL;
   1304 	} else {
   1305 		KASSERT(tde->td_node != NULL);
   1306 		/* We ruled out `.' earlier.  */
   1307 		KASSERT(tde->td_node != dnode);
   1308 		/* We ruled out `..' earlier.  */
   1309 		KASSERT(tde->td_node != dnode->tn_spec.tn_dir.tn_parent);
   1310 		if (tde->td_node != fde->td_node) {
   1311 			mutex_enter(&tde->td_node->tn_vlock);
   1312 			error = tmpfs_vnode_get(mount, tde->td_node, &tvp);
   1313 			if (error)
   1314 				goto fail2;
   1315 			KASSERT(tvp->v_mount == mount);
   1316 			/* Refuse to rename over a mount point.  */
   1317 			if ((tvp->v_type == VDIR) &&
   1318 			    (tvp->v_mountedhere != NULL)) {
   1319 				error = EBUSY;
   1320 				goto fail3;
   1321 			}
   1322 		} else {
   1323 			tvp = fvp;
   1324 			vref(tvp);
   1325 		}
   1326 		KASSERT(tvp != NULL);
   1327 		KASSERT(VOP_ISLOCKED(tvp) == LK_EXCLUSIVE);
   1328 	}
   1329 	KASSERT(tvp != dvp);
   1330 
   1331 	*fde_ret = fde;
   1332 	*fvp_ret = fvp;
   1333 	*tde_ret = tde;
   1334 	*tvp_ret = tvp;
   1335 	return 0;
   1336 
   1337 fail3:	if (tvp != NULL) {
   1338 		if (tvp != fvp)
   1339 			vput(tvp);
   1340 		else
   1341 			vrele(tvp);
   1342 	}
   1343 
   1344 fail2:	vput(fvp);
   1345 fail1:	VOP_UNLOCK(dvp);
   1346 fail0:	return error;
   1347 }
   1348 
   1349 /*
   1350  * Lock and look up with separate source and target directories.
   1351  */
   1352 static int
   1353 tmpfs_rename_enter_separate(struct mount *mount, struct tmpfs_mount *tmpfs,
   1354     kauth_cred_t cred,
   1355     struct vnode *fdvp, struct tmpfs_node *fdnode, struct componentname *fcnp,
   1356     struct tmpfs_dirent **fde_ret, struct vnode **fvp_ret,
   1357     struct vnode *tdvp, struct tmpfs_node *tdnode, struct componentname *tcnp,
   1358     struct tmpfs_dirent **tde_ret, struct vnode **tvp_ret)
   1359 {
   1360 	struct tmpfs_node *intermediate_node;
   1361 	struct tmpfs_dirent *fde, *tde;
   1362 	struct vnode *fvp, *tvp;
   1363 	int error;
   1364 
   1365 	KASSERT(fdvp != tdvp);
   1366 	KASSERT(fdnode != tdnode);
   1367 
   1368 #if 0				/* XXX */
   1369 	mutex_enter(&tmpfs->tm_rename_lock);
   1370 #endif
   1371 
   1372 	error = tmpfs_rename_genealogy(fdnode, tdnode, &intermediate_node);
   1373 	if (error)
   1374 		goto fail;
   1375 
   1376 	/*
   1377 	 * intermediate_node == NULL means fdnode is not an ancestor of
   1378 	 * tdnode.
   1379 	 */
   1380 	if (intermediate_node == NULL)
   1381 		error = tmpfs_rename_lock(mount, cred, ENOTEMPTY,
   1382 		    tdvp, tdnode, tcnp, true, &tde, &tvp,
   1383 		    fdvp, fdnode, fcnp, false, &fde, &fvp);
   1384 	else
   1385 		error = tmpfs_rename_lock(mount, cred, EINVAL,
   1386 		    fdvp, fdnode, fcnp, false, &fde, &fvp,
   1387 		    tdvp, tdnode, tcnp, true, &tde, &tvp);
   1388 	if (error)
   1389 		goto fail;
   1390 
   1391 	KASSERT(fde != NULL);
   1392 	KASSERT(fde->td_node != NULL);
   1393 
   1394 	/*
   1395 	 * Reject rename("foo/bar", "foo/bar/baz/quux/zot").
   1396 	 */
   1397 	if (fde->td_node == intermediate_node) {
   1398 		tmpfs_rename_exit(tmpfs, fdvp, fvp, tdvp, tvp);
   1399 		return EINVAL;
   1400 	}
   1401 
   1402 	*fde_ret = fde;
   1403 	*fvp_ret = fvp;
   1404 	*tde_ret = tde;
   1405 	*tvp_ret = tvp;
   1406 	return 0;
   1407 
   1408 fail:
   1409 #if 0				/* XXX */
   1410 	mutex_exit(&tmpfs->tm_rename_lock);
   1411 #endif
   1412 	return error;
   1413 }
   1414 
   1415 /*
   1416  * Unlock everything we locked for rename.
   1417  *
   1418  * fdvp and tdvp must be referenced.
   1419  *
   1420  * On entry, everything is locked, and fvp and tvp referenced.
   1421  *
   1422  * On exit, everything is unlocked, and fvp and tvp are released.
   1423  */
   1424 static void
   1425 tmpfs_rename_exit(struct tmpfs_mount *tmpfs,
   1426     struct vnode *fdvp, struct vnode *fvp,
   1427     struct vnode *tdvp, struct vnode *tvp)
   1428 {
   1429 
   1430 	KASSERT(tmpfs != NULL);
   1431 	KASSERT(fdvp != NULL);
   1432 	KASSERT(fvp != NULL);
   1433 	KASSERT(fdvp != fvp);
   1434 	KASSERT(fdvp != tvp);
   1435 	KASSERT(tdvp != tvp);
   1436 	KASSERT(tdvp != fvp);
   1437 	KASSERT(VOP_ISLOCKED(fdvp) == LK_EXCLUSIVE);
   1438 	KASSERT(VOP_ISLOCKED(tdvp) == LK_EXCLUSIVE);
   1439 	KASSERT(VOP_ISLOCKED(fvp) == LK_EXCLUSIVE);
   1440 	KASSERT((tvp == NULL) || (VOP_ISLOCKED(tvp) == LK_EXCLUSIVE));
   1441 
   1442 	if (tvp != NULL) {
   1443 		if (tvp != fvp)
   1444 			vput(tvp);
   1445 		else
   1446 			vrele(tvp);
   1447 	}
   1448 	VOP_UNLOCK(tdvp);
   1449 	vput(fvp);
   1450 	if (fdvp != tdvp)
   1451 		VOP_UNLOCK(fdvp);
   1452 
   1453 #if 0				/* XXX */
   1454 	if (fdvp != tdvp)
   1455 		mutex_exit(&tmpfs->tm_rename_lock);
   1456 #endif
   1457 }
   1458 
   1459 /*
   1460  * Lock a directory, but fail if it has been rmdir'd.
   1461  *
   1462  * vp must be referenced.
   1463  */
   1464 static int
   1465 tmpfs_rename_lock_directory(struct vnode *vp, struct tmpfs_node *node)
   1466 {
   1467 
   1468 	KASSERT(vp != NULL);
   1469 	KASSERT(node != NULL);
   1470 	KASSERT(node->tn_vnode == vp);
   1471 	KASSERT(node->tn_type == VDIR);
   1472 
   1473 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   1474 	if (node->tn_spec.tn_dir.tn_parent == NULL) {
   1475 		VOP_UNLOCK(vp);
   1476 		return ENOENT;
   1477 	}
   1478 
   1479 	return 0;
   1480 }
   1481 
   1482 /*
   1483  * Analyze the genealogy of the source and target nodes.
   1484  *
   1485  * On success, stores in *intermediate_node_ret either the child of
   1486  * fdnode of which tdnode is a descendant, or null if tdnode is not a
   1487  * descendant of fdnode at all.
   1488  *
   1489  * fdnode and tdnode must be unlocked and referenced.  The file
   1490  * system's rename lock must also be held, to exclude concurrent
   1491  * changes to the file system's genealogy other than rmdir.
   1492  *
   1493  * XXX This causes an extra lock/unlock of tdnode in the case when
   1494  * we're just about to lock it again before locking anything else.
   1495  * However, changing that requires reorganizing the code to make it
   1496  * even more horrifically obscure.
   1497  */
   1498 static int
   1499 tmpfs_rename_genealogy(struct tmpfs_node *fdnode, struct tmpfs_node *tdnode,
   1500     struct tmpfs_node **intermediate_node_ret)
   1501 {
   1502 	struct tmpfs_node *node = tdnode, *parent;
   1503 	int error;
   1504 
   1505 	KASSERT(fdnode != NULL);
   1506 	KASSERT(tdnode != NULL);
   1507 	KASSERT(fdnode != tdnode);
   1508 	KASSERT(intermediate_node_ret != NULL);
   1509 
   1510 	KASSERT(fdnode->tn_vnode != NULL);
   1511 	KASSERT(tdnode->tn_vnode != NULL);
   1512 	KASSERT(fdnode->tn_type == VDIR);
   1513 	KASSERT(tdnode->tn_type == VDIR);
   1514 
   1515 	/*
   1516 	 * We need to provisionally lock tdnode->tn_vnode to keep rmdir
   1517 	 * from deleting it -- or any ancestor -- at an inopportune
   1518 	 * moment.
   1519 	 */
   1520 	error = tmpfs_rename_lock_directory(tdnode->tn_vnode, tdnode);
   1521 	if (error)
   1522 		return error;
   1523 
   1524 	for (;;) {
   1525 		parent = node->tn_spec.tn_dir.tn_parent;
   1526 		KASSERT(parent != NULL);
   1527 		KASSERT(parent->tn_type == VDIR);
   1528 
   1529 		/* Did we hit the root without finding fdnode?  */
   1530 		if (parent == node) {
   1531 			*intermediate_node_ret = NULL;
   1532 			break;
   1533 		}
   1534 
   1535 		/* Did we find that fdnode is an ancestor?  */
   1536 		if (parent == fdnode) {
   1537 			*intermediate_node_ret = node;
   1538 			break;
   1539 		}
   1540 
   1541 		/* Neither -- keep ascending the family tree.  */
   1542 		node = parent;
   1543 	}
   1544 
   1545 	VOP_UNLOCK(tdnode->tn_vnode);
   1546 	return 0;
   1547 }
   1548 
   1549 /*
   1550  * Lock directories a and b, which must be distinct, and look up and
   1551  * lock nodes a and b.  Do a first and then b.  Directory b may not be
   1552  * an ancestor of directory a, although directory a may be an ancestor
   1553  * of directory b.  Fail with overlap_error if node a is directory b.
   1554  * Neither componentname may be `.' or `..'.
   1555  *
   1556  * a_dvp and b_dvp must be referenced.
   1557  *
   1558  * On entry, a_dvp and b_dvp are unlocked.
   1559  *
   1560  * On success,
   1561  * . a_dvp and b_dvp are locked,
   1562  * . *a_dirent_ret is filled with a directory entry whose node is
   1563  *     locked and referenced,
   1564  * . *b_vp_ret is filled with the corresponding vnode,
   1565  * . *b_dirent_ret is filled either with null or with a directory entry
   1566  *     whose node is locked and referenced,
   1567  * . *b_vp is filled either with null or with the corresponding vnode,
   1568  *     and
   1569  * . the only pair of vnodes that may be identical is a_vp and b_vp.
   1570  *
   1571  * On failure, a_dvp and b_dvp are left unlocked, and *a_dirent_ret,
   1572  * *a_vp, *b_dirent_ret, and *b_vp are left alone.
   1573  */
   1574 static int
   1575 tmpfs_rename_lock(struct mount *mount, kauth_cred_t cred, int overlap_error,
   1576     struct vnode *a_dvp, struct tmpfs_node *a_dnode,
   1577     struct componentname *a_cnp, bool a_missing_ok,
   1578     struct tmpfs_dirent **a_dirent_ret, struct vnode **a_vp_ret,
   1579     struct vnode *b_dvp, struct tmpfs_node *b_dnode,
   1580     struct componentname *b_cnp, bool b_missing_ok,
   1581     struct tmpfs_dirent **b_dirent_ret, struct vnode **b_vp_ret)
   1582 {
   1583 	struct tmpfs_dirent *a_dirent, *b_dirent;
   1584 	struct vnode *a_vp, *b_vp;
   1585 	int error;
   1586 
   1587 	KASSERT(a_dvp != NULL);
   1588 	KASSERT(a_dnode != NULL);
   1589 	KASSERT(a_cnp != NULL);
   1590 	KASSERT(a_dirent_ret != NULL);
   1591 	KASSERT(a_vp_ret != NULL);
   1592 	KASSERT(b_dvp != NULL);
   1593 	KASSERT(b_dnode != NULL);
   1594 	KASSERT(b_cnp != NULL);
   1595 	KASSERT(b_dirent_ret != NULL);
   1596 	KASSERT(b_vp_ret != NULL);
   1597 	KASSERT(a_dvp != b_dvp);
   1598 	KASSERT(a_dnode != b_dnode);
   1599 	KASSERT(a_dnode->tn_vnode == a_dvp);
   1600 	KASSERT(b_dnode->tn_vnode == b_dvp);
   1601 	KASSERT(a_dnode->tn_type == VDIR);
   1602 	KASSERT(b_dnode->tn_type == VDIR);
   1603 	KASSERT(a_missing_ok != b_missing_ok);
   1604 
   1605 	error = tmpfs_rename_lock_directory(a_dvp, a_dnode);
   1606 	if (error)
   1607 		goto fail0;
   1608 
   1609 	/* Did we lose a race with mount?  */
   1610 	if (a_dvp->v_mountedhere != NULL) {
   1611 		error = EBUSY;
   1612 		goto fail1;
   1613 	}
   1614 
   1615 	/* Make sure the caller may read the directory.  */
   1616 	error = VOP_ACCESS(a_dvp, VEXEC, cred);
   1617 	if (error)
   1618 		goto fail1;
   1619 
   1620 	a_dirent = tmpfs_dir_lookup(a_dnode, a_cnp);
   1621 	if (a_dirent != NULL) {
   1622 		KASSERT(a_dirent->td_node != NULL);
   1623 		/* We ruled out `.' earlier.  */
   1624 		KASSERT(a_dirent->td_node != a_dnode);
   1625 		/* We ruled out `..' earlier.  */
   1626 		KASSERT(a_dirent->td_node !=
   1627 		    a_dnode->tn_spec.tn_dir.tn_parent);
   1628 		if (a_dirent->td_node == b_dnode) {
   1629 			error = overlap_error;
   1630 			goto fail1;
   1631 		}
   1632 		mutex_enter(&a_dirent->td_node->tn_vlock);
   1633 		error = tmpfs_vnode_get(mount, a_dirent->td_node, &a_vp);
   1634 		if (error)
   1635 			goto fail1;
   1636 		KASSERT(a_vp->v_mount == mount);
   1637 		/* Refuse to rename (over) a mount point.  */
   1638 		if ((a_vp->v_type == VDIR) && (a_vp->v_mountedhere != NULL)) {
   1639 			error = EBUSY;
   1640 			goto fail2;
   1641 		}
   1642 	} else if (!a_missing_ok) {
   1643 		error = ENOENT;
   1644 		goto fail1;
   1645 	} else {
   1646 		a_vp = NULL;
   1647 	}
   1648 	KASSERT(a_vp != a_dvp);
   1649 	KASSERT(a_vp != b_dvp);
   1650 
   1651 	error = tmpfs_rename_lock_directory(b_dvp, b_dnode);
   1652 	if (error)
   1653 		goto fail2;
   1654 
   1655 	/* Did we lose a race with mount?  */
   1656 	if (b_dvp->v_mountedhere != NULL) {
   1657 		error = EBUSY;
   1658 		goto fail3;
   1659 	}
   1660 
   1661 	/* Make sure the caller may read the directory.  */
   1662 	error = VOP_ACCESS(b_dvp, VEXEC, cred);
   1663 	if (error)
   1664 		goto fail3;
   1665 
   1666 	b_dirent = tmpfs_dir_lookup(b_dnode, b_cnp);
   1667 	if (b_dirent != NULL) {
   1668 		KASSERT(b_dirent->td_node != NULL);
   1669 		/* We ruled out `.' earlier.  */
   1670 		KASSERT(b_dirent->td_node != b_dnode);
   1671 		/* We ruled out `..' earlier.  */
   1672 		KASSERT(b_dirent->td_node !=
   1673 		    b_dnode->tn_spec.tn_dir.tn_parent);
   1674 		/* b is not an ancestor of a.  */
   1675 		KASSERT(b_dirent->td_node != a_dnode);
   1676 		/* But the source and target nodes might be the same.  */
   1677 		if ((a_dirent == NULL) ||
   1678 		    (a_dirent->td_node != b_dirent->td_node)) {
   1679 			mutex_enter(&b_dirent->td_node->tn_vlock);
   1680 			error = tmpfs_vnode_get(mount, b_dirent->td_node,
   1681 			    &b_vp);
   1682 			if (error)
   1683 				goto fail3;
   1684 			KASSERT(b_vp->v_mount == mount);
   1685 			KASSERT(a_vp != b_vp);
   1686 			/* Refuse to rename (over) a mount point.  */
   1687 			if ((b_vp->v_type == VDIR) &&
   1688 			    (b_vp->v_mountedhere != NULL)) {
   1689 				error = EBUSY;
   1690 				goto fail4;
   1691 			}
   1692 		} else {
   1693 			b_vp = a_vp;
   1694 			vref(b_vp);
   1695 		}
   1696 	} else if (!b_missing_ok) {
   1697 		error = ENOENT;
   1698 		goto fail3;
   1699 	} else {
   1700 		b_vp = NULL;
   1701 	}
   1702 	KASSERT(b_vp != a_dvp);
   1703 	KASSERT(b_vp != b_dvp);
   1704 
   1705 	KASSERT(VOP_ISLOCKED(a_dvp) == LK_EXCLUSIVE);
   1706 	KASSERT(VOP_ISLOCKED(b_dvp) == LK_EXCLUSIVE);
   1707 	KASSERT(a_missing_ok || (a_dirent != NULL));
   1708 	KASSERT(a_missing_ok || (a_dirent->td_node != NULL));
   1709 	KASSERT(b_missing_ok || (b_dirent != NULL));
   1710 	KASSERT(b_missing_ok || (b_dirent->td_node != NULL));
   1711 	KASSERT((a_dirent == NULL) || (a_dirent->td_node != NULL));
   1712 	KASSERT((a_dirent == NULL) || (a_dirent->td_node->tn_vnode == a_vp));
   1713 	KASSERT((b_dirent == NULL) || (b_dirent->td_node != NULL));
   1714 	KASSERT((b_dirent == NULL) || (b_dirent->td_node->tn_vnode == b_vp));
   1715 	KASSERT((a_vp == NULL) || (VOP_ISLOCKED(a_vp) == LK_EXCLUSIVE));
   1716 	KASSERT((b_vp == NULL) || (VOP_ISLOCKED(b_vp) == LK_EXCLUSIVE));
   1717 
   1718 	*a_dirent_ret = a_dirent;
   1719 	*b_dirent_ret = b_dirent;
   1720 	*a_vp_ret = a_vp;
   1721 	*b_vp_ret = b_vp;
   1722 	return 0;
   1723 
   1724 fail4:	if (b_vp != NULL) {
   1725 		KASSERT(VOP_ISLOCKED(b_vp) == LK_EXCLUSIVE);
   1726 		if (b_vp != a_vp)
   1727 			vput(b_vp);
   1728 		else
   1729 			vrele(a_vp);
   1730 	}
   1731 
   1732 fail3:	KASSERT(VOP_ISLOCKED(b_dvp) == LK_EXCLUSIVE);
   1733 	VOP_UNLOCK(b_dvp);
   1734 
   1735 fail2:	if (a_vp != NULL) {
   1736 		KASSERT(VOP_ISLOCKED(a_vp) == LK_EXCLUSIVE);
   1737 		vput(a_vp);
   1738 	}
   1739 
   1740 fail1:	KASSERT(VOP_ISLOCKED(a_dvp) == LK_EXCLUSIVE);
   1741 	VOP_UNLOCK(a_dvp);
   1742 
   1743 fail0:	/* KASSERT(VOP_ISLOCKED(a_dvp) != LK_EXCLUSIVE); */
   1744 	/* KASSERT(VOP_ISLOCKED(b_dvp) != LK_EXCLUSIVE); */
   1745 	/* KASSERT((a_vp == NULL) || (VOP_ISLOCKED(a_vp) != LK_EXCLUSIVE)); */
   1746 	/* KASSERT((b_vp == NULL) || (VOP_ISLOCKED(b_vp) != LK_EXCLUSIVE)); */
   1747 	return error;
   1748 }
   1749 
   1750 /*
   1751  * Shuffle the directory entries to move fvp from the directory fdvp
   1752  * into the directory tdvp.  fde is fvp's directory entry in fdvp.  If
   1753  * we are overwriting a target node, it is tvp, and tde is its
   1754  * directory entry in tdvp.
   1755  *
   1756  * fdvp, fvp, tdvp, and tvp must all be locked and referenced.
   1757  */
   1758 static void
   1759 tmpfs_rename_attachdetach(struct tmpfs_mount *tmpfs,
   1760     struct vnode *fdvp, struct tmpfs_dirent *fde, struct vnode *fvp,
   1761     struct vnode *tdvp, struct tmpfs_dirent *tde, struct vnode *tvp)
   1762 {
   1763 
   1764 	KASSERT(tmpfs != NULL);
   1765 	KASSERT(fdvp != NULL);
   1766 	KASSERT(fde != NULL);
   1767 	KASSERT(fvp != NULL);
   1768 	KASSERT(tdvp != NULL);
   1769 	KASSERT(fde->td_node != NULL);
   1770 	KASSERT(fde->td_node->tn_vnode == fvp);
   1771 	KASSERT((tde == NULL) == (tvp == NULL));
   1772 	KASSERT((tde == NULL) || (tde->td_node != NULL));
   1773 	KASSERT((tde == NULL) || (tde->td_node->tn_vnode == tvp));
   1774 	KASSERT(VOP_ISLOCKED(fdvp) == LK_EXCLUSIVE);
   1775 	KASSERT(VOP_ISLOCKED(tdvp) == LK_EXCLUSIVE);
   1776 	KASSERT(VOP_ISLOCKED(fvp) == LK_EXCLUSIVE);
   1777 	KASSERT((tvp == NULL) || (VOP_ISLOCKED(tvp) == LK_EXCLUSIVE));
   1778 
   1779 	/*
   1780 	 * If we are moving from one directory to another, detach the
   1781 	 * source entry and reattach it to the target directory.
   1782 	 */
   1783 	if (fdvp != tdvp) {
   1784 		/* tmpfs_dir_detach clobbers fde->td_node, so save it.  */
   1785 		struct tmpfs_node *fnode = fde->td_node;
   1786 		tmpfs_dir_detach(fdvp, fde);
   1787 		tmpfs_dir_attach(tdvp, fde, fnode);
   1788 	} else if (tvp == NULL) {
   1789 		/*
   1790 		 * We are changing the directory.  tmpfs_dir_attach and
   1791 		 * tmpfs_dir_detach note the events for us, but for
   1792 		 * this case we don't call them, so we must note the
   1793 		 * event explicitly.
   1794 		 */
   1795 		VN_KNOTE(fdvp, NOTE_WRITE);
   1796 	}
   1797 
   1798 	/*
   1799 	 * If we are replacing an existing target entry, delete it.
   1800 	 */
   1801 	if (tde != NULL) {
   1802 		KASSERT(tvp != NULL);
   1803 		KASSERT(tde->td_node != NULL);
   1804 		KASSERT((fvp->v_type == VDIR) == (tvp->v_type == VDIR));
   1805 		if (tde->td_node->tn_type == VDIR) {
   1806 			KASSERT(tde->td_node->tn_size == 0);
   1807 			KASSERT(tde->td_node->tn_links == 2);
   1808 			/* Decrement the extra link count for `.' so
   1809 			 * the vnode will be recycled when released.  */
   1810 			tde->td_node->tn_links--;
   1811 		}
   1812 		tmpfs_dir_detach(tdvp, tde);
   1813 		tmpfs_free_dirent(tmpfs, tde);
   1814 	}
   1815 }
   1816 
   1817 /*
   1818  * Remove the entry de for the non-directory vp from the directory dvp.
   1819  *
   1820  * Everything must be locked and referenced.
   1821  */
   1822 static int
   1823 tmpfs_do_remove(struct tmpfs_mount *tmpfs, struct vnode *dvp,
   1824     struct tmpfs_node *dnode, struct tmpfs_dirent *de, struct vnode *vp,
   1825     kauth_cred_t cred)
   1826 {
   1827 	int error;
   1828 
   1829 	KASSERT(tmpfs != NULL);
   1830 	KASSERT(dvp != NULL);
   1831 	KASSERT(dnode != NULL);
   1832 	KASSERT(de != NULL);
   1833 	KASSERT(vp != NULL);
   1834 	KASSERT(dnode->tn_vnode == dvp);
   1835 	KASSERT(de->td_node != NULL);
   1836 	KASSERT(de->td_node->tn_vnode == vp);
   1837 	KASSERT(VOP_ISLOCKED(dvp) == LK_EXCLUSIVE);
   1838 	KASSERT(VOP_ISLOCKED(vp) == LK_EXCLUSIVE);
   1839 
   1840 	error = tmpfs_remove_check_possible(dnode, de->td_node);
   1841 	if (error)
   1842 		return error;
   1843 
   1844 	error = tmpfs_remove_check_permitted(cred, dnode, de->td_node);
   1845 	error = kauth_authorize_vnode(cred, KAUTH_VNODE_DELETE, vp, dvp,
   1846 	    error);
   1847 	if (error)
   1848 		return error;
   1849 
   1850 	tmpfs_dir_detach(dvp, de);
   1851 	tmpfs_free_dirent(tmpfs, de);
   1852 
   1853 	return 0;
   1854 }
   1855 
   1856 /*
   1857  * Check whether a rename is possible independent of credentials.
   1858  *
   1859  * Everything must be locked and referenced.
   1860  */
   1861 static int
   1862 tmpfs_rename_check_possible(
   1863     struct tmpfs_node *fdnode, struct tmpfs_node *fnode,
   1864     struct tmpfs_node *tdnode, struct tmpfs_node *tnode)
   1865 {
   1866 
   1867 	KASSERT(fdnode != NULL);
   1868 	KASSERT(fnode != NULL);
   1869 	KASSERT(tdnode != NULL);
   1870 	KASSERT(fdnode != fnode);
   1871 	KASSERT(tdnode != tnode);
   1872 	KASSERT(fnode != tnode);
   1873 	KASSERT(fdnode->tn_vnode != NULL);
   1874 	KASSERT(fnode->tn_vnode != NULL);
   1875 	KASSERT(tdnode->tn_vnode != NULL);
   1876 	KASSERT((tnode == NULL) || (tnode->tn_vnode != NULL));
   1877 	KASSERT(VOP_ISLOCKED(fdnode->tn_vnode) == LK_EXCLUSIVE);
   1878 	KASSERT(VOP_ISLOCKED(fnode->tn_vnode) == LK_EXCLUSIVE);
   1879 	KASSERT(VOP_ISLOCKED(tdnode->tn_vnode) == LK_EXCLUSIVE);
   1880 	KASSERT((tnode == NULL) ||
   1881 	    (VOP_ISLOCKED(tnode->tn_vnode) == LK_EXCLUSIVE));
   1882 
   1883 	/*
   1884 	 * If fdnode is immutable, we can't write to it.  If fdnode is
   1885 	 * append-only, the only change we can make is to add entries
   1886 	 * to it.  If fnode is immutable, we can't change the links to
   1887 	 * it.  If fnode is append-only...well, this is what UFS does.
   1888 	 */
   1889 	if ((fdnode->tn_flags | fnode->tn_flags) & (IMMUTABLE | APPEND))
   1890 		return EPERM;
   1891 
   1892 	/*
   1893 	 * If tdnode is immutable, we can't write to it.  If tdnode is
   1894 	 * append-only, we can add entries, but we can't change
   1895 	 * existing entries.
   1896 	 */
   1897 	if (tdnode->tn_flags & (IMMUTABLE | (tnode? APPEND : 0)))
   1898 		return EPERM;
   1899 
   1900 	/*
   1901 	 * If tnode is immutable, we can't replace links to it.  If
   1902 	 * tnode is append-only...well, this is what UFS does.
   1903 	 */
   1904 	if (tnode != NULL) {
   1905 		KASSERT(tnode != NULL);
   1906 		if ((tnode->tn_flags & (IMMUTABLE | APPEND)) != 0)
   1907 			return EPERM;
   1908 	}
   1909 
   1910 	return 0;
   1911 }
   1912 
   1913 /*
   1914  * Check whether a rename is permitted given our credentials.
   1915  *
   1916  * Everything must be locked and referenced.
   1917  */
   1918 static int
   1919 tmpfs_rename_check_permitted(kauth_cred_t cred,
   1920     struct tmpfs_node *fdnode, struct tmpfs_node *fnode,
   1921     struct tmpfs_node *tdnode, struct tmpfs_node *tnode)
   1922 {
   1923 	int error;
   1924 
   1925 	KASSERT(fdnode != NULL);
   1926 	KASSERT(fnode != NULL);
   1927 	KASSERT(tdnode != NULL);
   1928 	KASSERT(fdnode != fnode);
   1929 	KASSERT(tdnode != tnode);
   1930 	KASSERT(fnode != tnode);
   1931 	KASSERT(fdnode->tn_vnode != NULL);
   1932 	KASSERT(fnode->tn_vnode != NULL);
   1933 	KASSERT(tdnode->tn_vnode != NULL);
   1934 	KASSERT((tnode == NULL) || (tnode->tn_vnode != NULL));
   1935 	KASSERT(VOP_ISLOCKED(fdnode->tn_vnode) == LK_EXCLUSIVE);
   1936 	KASSERT(VOP_ISLOCKED(fnode->tn_vnode) == LK_EXCLUSIVE);
   1937 	KASSERT(VOP_ISLOCKED(tdnode->tn_vnode) == LK_EXCLUSIVE);
   1938 	KASSERT((tnode == NULL) ||
   1939 	    (VOP_ISLOCKED(tnode->tn_vnode) == LK_EXCLUSIVE));
   1940 
   1941 	/*
   1942 	 * We need to remove or change an entry in the source directory.
   1943 	 */
   1944 	error = VOP_ACCESS(fdnode->tn_vnode, VWRITE, cred);
   1945 	if (error)
   1946 		return error;
   1947 
   1948 	/*
   1949 	 * If we are changing directories, then we need to write to the
   1950 	 * target directory to add or change an entry.  Also, if fnode
   1951 	 * is a directory, we need to write to it to change its `..'
   1952 	 * entry.
   1953 	 */
   1954 	if (fdnode != tdnode) {
   1955 		error = VOP_ACCESS(tdnode->tn_vnode, VWRITE, cred);
   1956 		if (error)
   1957 			return error;
   1958 		if (fnode->tn_type == VDIR) {
   1959 			error = VOP_ACCESS(fnode->tn_vnode, VWRITE, cred);
   1960 			if (error)
   1961 				return error;
   1962 		}
   1963 	}
   1964 
   1965 	error = tmpfs_check_sticky(cred, fdnode, fnode);
   1966 	if (error)
   1967 		return error;
   1968 
   1969 	error = tmpfs_check_sticky(cred, tdnode, tnode);
   1970 	if (error)
   1971 		return error;
   1972 
   1973 	return 0;
   1974 }
   1975 
   1976 /*
   1977  * Check whether removing node's entry in dnode is possible independent
   1978  * of credentials.
   1979  *
   1980  * Everything must be locked and referenced.
   1981  */
   1982 static int
   1983 tmpfs_remove_check_possible(struct tmpfs_node *dnode, struct tmpfs_node *node)
   1984 {
   1985 
   1986 	KASSERT(dnode != NULL);
   1987 	KASSERT(dnode->tn_vnode != NULL);
   1988 	KASSERT(node != NULL);
   1989 	KASSERT(dnode != node);
   1990 	KASSERT(VOP_ISLOCKED(dnode->tn_vnode) == LK_EXCLUSIVE);
   1991 	KASSERT(VOP_ISLOCKED(node->tn_vnode) == LK_EXCLUSIVE);
   1992 
   1993 	/*
   1994 	 * We want to delete the entry.  If dnode is immutable, we
   1995 	 * can't write to it to delete the entry.  If dnode is
   1996 	 * append-only, the only change we can make is to add entries,
   1997 	 * so we can't delete entries.  If node is immutable, we can't
   1998 	 * change the links to it, so we can't delete the entry.  If
   1999 	 * node is append-only...well, this is what UFS does.
   2000 	 */
   2001 	if ((dnode->tn_flags | node->tn_flags) & (IMMUTABLE | APPEND))
   2002 		return EPERM;
   2003 
   2004 	return 0;
   2005 }
   2006 
   2007 /*
   2008  * Check whether removing node's entry in dnode is permitted given our
   2009  * credentials.
   2010  *
   2011  * Everything must be locked and referenced.
   2012  */
   2013 static int
   2014 tmpfs_remove_check_permitted(kauth_cred_t cred,
   2015     struct tmpfs_node *dnode, struct tmpfs_node *node)
   2016 {
   2017 	int error;
   2018 
   2019 	KASSERT(dnode != NULL);
   2020 	KASSERT(dnode->tn_vnode != NULL);
   2021 	KASSERT(node != NULL);
   2022 	KASSERT(dnode != node);
   2023 	KASSERT(VOP_ISLOCKED(dnode->tn_vnode) == LK_EXCLUSIVE);
   2024 	KASSERT(VOP_ISLOCKED(node->tn_vnode) == LK_EXCLUSIVE);
   2025 
   2026 	/*
   2027 	 * Check whether we are permitted to write to the source
   2028 	 * directory in order to delete an entry from it.
   2029 	 */
   2030 	error = VOP_ACCESS(dnode->tn_vnode, VWRITE, cred);
   2031 	if (error)
   2032 		return error;
   2033 
   2034 	error = tmpfs_check_sticky(cred, dnode, node);
   2035 	if (error)
   2036 		return error;
   2037 
   2038 	return 0;
   2039 }
   2040 
   2041 /*
   2042  * Check whether we may change an entry in a sticky directory.  If the
   2043  * directory is sticky, the user must own either the directory or, if
   2044  * it exists, the node, in order to change the entry.
   2045  *
   2046  * Everything must be locked and referenced.
   2047  */
   2048 static int
   2049 tmpfs_check_sticky(kauth_cred_t cred,
   2050     struct tmpfs_node *dnode, struct tmpfs_node *node)
   2051 {
   2052 
   2053 	KASSERT(dnode != NULL);
   2054 	KASSERT(dnode->tn_vnode != NULL);
   2055 	KASSERT(VOP_ISLOCKED(dnode->tn_vnode) == LK_EXCLUSIVE);
   2056 	KASSERT((node == NULL) || (node->tn_vnode != NULL));
   2057 	KASSERT((node == NULL) ||
   2058 	    (VOP_ISLOCKED(dnode->tn_vnode) == LK_EXCLUSIVE));
   2059 
   2060 	if (dnode->tn_mode & S_ISTXT) {
   2061 		uid_t euid = kauth_cred_geteuid(cred);
   2062 		if (euid == dnode->tn_uid)
   2063 			return 0;
   2064 		if ((node == NULL) || (euid == node->tn_uid))
   2065 			return 0;
   2066 		return EPERM;
   2067 	}
   2068 
   2069 	return 0;
   2070 }
   2071 
   2072 int
   2073 tmpfs_mkdir(void *v)
   2074 {
   2075 	struct vop_mkdir_args /* {
   2076 		struct vnode		*a_dvp;
   2077 		struct vnode		**a_vpp;
   2078 		struct componentname	*a_cnp;
   2079 		struct vattr		*a_vap;
   2080 	} */ *ap = v;
   2081 	vnode_t *dvp = ap->a_dvp;
   2082 	vnode_t **vpp = ap->a_vpp;
   2083 	struct componentname *cnp = ap->a_cnp;
   2084 	struct vattr *vap = ap->a_vap;
   2085 
   2086 	KASSERT(vap->va_type == VDIR);
   2087 	return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
   2088 }
   2089 
   2090 int
   2091 tmpfs_rmdir(void *v)
   2092 {
   2093 	struct vop_rmdir_args /* {
   2094 		struct vnode		*a_dvp;
   2095 		struct vnode		*a_vp;
   2096 		struct componentname	*a_cnp;
   2097 	} */ *ap = v;
   2098 	vnode_t *dvp = ap->a_dvp;
   2099 	vnode_t *vp = ap->a_vp;
   2100 	tmpfs_mount_t *tmp = VFS_TO_TMPFS(dvp->v_mount);
   2101 	tmpfs_node_t *dnode = VP_TO_TMPFS_DIR(dvp);
   2102 	tmpfs_node_t *node = VP_TO_TMPFS_DIR(vp);
   2103 	tmpfs_dirent_t *de;
   2104 	int error = 0;
   2105 
   2106 	KASSERT(VOP_ISLOCKED(dvp));
   2107 	KASSERT(VOP_ISLOCKED(vp));
   2108 	KASSERT(node->tn_spec.tn_dir.tn_parent == dnode);
   2109 
   2110 	/*
   2111 	 * Directories with more than two non-whiteout
   2112 	 * entries ('.' and '..') cannot be removed.
   2113 	 */
   2114 	if (node->tn_size > 0) {
   2115 		KASSERT(error == 0);
   2116 		TAILQ_FOREACH(de, &node->tn_spec.tn_dir.tn_dir, td_entries) {
   2117 			if (de->td_node != TMPFS_NODE_WHITEOUT) {
   2118 				error = ENOTEMPTY;
   2119 				break;
   2120 			}
   2121 		}
   2122 		if (error)
   2123 			goto out;
   2124 	}
   2125 
   2126 	/* Lookup the directory entry (check the cached hint first). */
   2127 	de = tmpfs_dir_cached(node);
   2128 	if (de == NULL) {
   2129 		struct componentname *cnp = ap->a_cnp;
   2130 		de = tmpfs_dir_lookup(dnode, cnp);
   2131 	}
   2132 	KASSERT(de && de->td_node == node);
   2133 
   2134 	/* Check flags to see if we are allowed to remove the directory. */
   2135 	if (dnode->tn_flags & APPEND || node->tn_flags & (IMMUTABLE | APPEND)) {
   2136 		error = EPERM;
   2137 		goto out;
   2138 	}
   2139 
   2140 	/* Decrement the link count for the virtual '.' entry. */
   2141 	node->tn_links--;
   2142 	node->tn_status |= TMPFS_NODE_STATUSALL;
   2143 
   2144 	/* Detach the directory entry from the directory. */
   2145 	tmpfs_dir_detach(dvp, de);
   2146 
   2147 	/* Purge the cache for parent. */
   2148 	cache_purge(dvp);
   2149 
   2150 	/*
   2151 	 * Destroy the directory entry or replace it with a whiteout.
   2152 	 * Note: the inode referred by it will not be destroyed
   2153 	 * until the vnode is reclaimed.
   2154 	 */
   2155 	if (ap->a_cnp->cn_flags & DOWHITEOUT)
   2156 		tmpfs_dir_attach(dvp, de, TMPFS_NODE_WHITEOUT);
   2157 	else
   2158 		tmpfs_free_dirent(tmp, de);
   2159 
   2160 	/* Destroy the whiteout entries from the node. */
   2161 	while ((de = TAILQ_FIRST(&node->tn_spec.tn_dir.tn_dir)) != NULL) {
   2162 		KASSERT(de->td_node == TMPFS_NODE_WHITEOUT);
   2163 		tmpfs_dir_detach(vp, de);
   2164 		tmpfs_free_dirent(tmp, de);
   2165 	}
   2166 
   2167 	KASSERT(node->tn_links == 0);
   2168 out:
   2169 	/* Release the nodes. */
   2170 	vput(dvp);
   2171 	vput(vp);
   2172 	return error;
   2173 }
   2174 
   2175 int
   2176 tmpfs_symlink(void *v)
   2177 {
   2178 	struct vop_symlink_args /* {
   2179 		struct vnode		*a_dvp;
   2180 		struct vnode		**a_vpp;
   2181 		struct componentname	*a_cnp;
   2182 		struct vattr		*a_vap;
   2183 		char			*a_target;
   2184 	} */ *ap = v;
   2185 	vnode_t *dvp = ap->a_dvp;
   2186 	vnode_t **vpp = ap->a_vpp;
   2187 	struct componentname *cnp = ap->a_cnp;
   2188 	struct vattr *vap = ap->a_vap;
   2189 	char *target = ap->a_target;
   2190 
   2191 	KASSERT(vap->va_type == VLNK);
   2192 	return tmpfs_alloc_file(dvp, vpp, vap, cnp, target);
   2193 }
   2194 
   2195 int
   2196 tmpfs_readdir(void *v)
   2197 {
   2198 	struct vop_readdir_args /* {
   2199 		struct vnode	*a_vp;
   2200 		struct uio	*a_uio;
   2201 		kauth_cred_t	a_cred;
   2202 		int		*a_eofflag;
   2203 		off_t		**a_cookies;
   2204 		int		*ncookies;
   2205 	} */ *ap = v;
   2206 	vnode_t *vp = ap->a_vp;
   2207 	struct uio *uio = ap->a_uio;
   2208 	int *eofflag = ap->a_eofflag;
   2209 	off_t **cookies = ap->a_cookies;
   2210 	int *ncookies = ap->a_ncookies;
   2211 	off_t startoff, cnt;
   2212 	tmpfs_node_t *node;
   2213 	int error;
   2214 
   2215 	KASSERT(VOP_ISLOCKED(vp));
   2216 
   2217 	/* This operation only makes sense on directory nodes. */
   2218 	if (vp->v_type != VDIR) {
   2219 		return ENOTDIR;
   2220 	}
   2221 	node = VP_TO_TMPFS_DIR(vp);
   2222 	startoff = uio->uio_offset;
   2223 	cnt = 0;
   2224 
   2225 	if (uio->uio_offset == TMPFS_DIRCOOKIE_DOT) {
   2226 		error = tmpfs_dir_getdotdent(node, uio);
   2227 		if (error != 0) {
   2228 			if (error == -1)
   2229 				error = 0;
   2230 			goto out;
   2231 		}
   2232 		cnt++;
   2233 	}
   2234 	if (uio->uio_offset == TMPFS_DIRCOOKIE_DOTDOT) {
   2235 		error = tmpfs_dir_getdotdotdent(node, uio);
   2236 		if (error != 0) {
   2237 			if (error == -1)
   2238 				error = 0;
   2239 			goto out;
   2240 		}
   2241 		cnt++;
   2242 	}
   2243 	error = tmpfs_dir_getdents(node, uio, &cnt);
   2244 	if (error == -1) {
   2245 		error = 0;
   2246 	}
   2247 	KASSERT(error >= 0);
   2248 out:
   2249 	if (eofflag != NULL) {
   2250 		*eofflag = (!error && uio->uio_offset == TMPFS_DIRCOOKIE_EOF);
   2251 	}
   2252 	if (error || cookies == NULL || ncookies == NULL) {
   2253 		return error;
   2254 	}
   2255 
   2256 	/* Update NFS-related variables, if any. */
   2257 	off_t i, off = startoff;
   2258 	tmpfs_dirent_t *de = NULL;
   2259 
   2260 	*cookies = malloc(cnt * sizeof(off_t), M_TEMP, M_WAITOK);
   2261 	*ncookies = cnt;
   2262 
   2263 	for (i = 0; i < cnt; i++) {
   2264 		KASSERT(off != TMPFS_DIRCOOKIE_EOF);
   2265 		if (off != TMPFS_DIRCOOKIE_DOT) {
   2266 			if (off == TMPFS_DIRCOOKIE_DOTDOT) {
   2267 				de = TAILQ_FIRST(&node->tn_spec.tn_dir.tn_dir);
   2268 			} else if (de != NULL) {
   2269 				de = TAILQ_NEXT(de, td_entries);
   2270 			} else {
   2271 				de = tmpfs_dir_lookupbycookie(node, off);
   2272 				KASSERT(de != NULL);
   2273 				de = TAILQ_NEXT(de, td_entries);
   2274 			}
   2275 			if (de == NULL) {
   2276 				off = TMPFS_DIRCOOKIE_EOF;
   2277 			} else {
   2278 				off = tmpfs_dircookie(de);
   2279 			}
   2280 		} else {
   2281 			off = TMPFS_DIRCOOKIE_DOTDOT;
   2282 		}
   2283 		(*cookies)[i] = off;
   2284 	}
   2285 	KASSERT(uio->uio_offset == off);
   2286 	return error;
   2287 }
   2288 
   2289 int
   2290 tmpfs_readlink(void *v)
   2291 {
   2292 	struct vop_readlink_args /* {
   2293 		struct vnode	*a_vp;
   2294 		struct uio	*a_uio;
   2295 		kauth_cred_t	a_cred;
   2296 	} */ *ap = v;
   2297 	vnode_t *vp = ap->a_vp;
   2298 	struct uio *uio = ap->a_uio;
   2299 	tmpfs_node_t *node;
   2300 	int error;
   2301 
   2302 	KASSERT(VOP_ISLOCKED(vp));
   2303 	KASSERT(uio->uio_offset == 0);
   2304 	KASSERT(vp->v_type == VLNK);
   2305 
   2306 	node = VP_TO_TMPFS_NODE(vp);
   2307 	error = uiomove(node->tn_spec.tn_lnk.tn_link,
   2308 	    MIN(node->tn_size, uio->uio_resid), uio);
   2309 	node->tn_status |= TMPFS_NODE_ACCESSED;
   2310 
   2311 	return error;
   2312 }
   2313 
   2314 int
   2315 tmpfs_inactive(void *v)
   2316 {
   2317 	struct vop_inactive_args /* {
   2318 		struct vnode *a_vp;
   2319 		bool *a_recycle;
   2320 	} */ *ap = v;
   2321 	vnode_t *vp = ap->a_vp;
   2322 	tmpfs_node_t *node;
   2323 
   2324 	KASSERT(VOP_ISLOCKED(vp));
   2325 
   2326 	node = VP_TO_TMPFS_NODE(vp);
   2327 	*ap->a_recycle = (node->tn_links == 0);
   2328 	VOP_UNLOCK(vp);
   2329 
   2330 	return 0;
   2331 }
   2332 
   2333 int
   2334 tmpfs_reclaim(void *v)
   2335 {
   2336 	struct vop_reclaim_args /* {
   2337 		struct vnode *a_vp;
   2338 	} */ *ap = v;
   2339 	vnode_t *vp = ap->a_vp;
   2340 	tmpfs_mount_t *tmp = VFS_TO_TMPFS(vp->v_mount);
   2341 	tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp);
   2342 	bool racing;
   2343 
   2344 	/* Disassociate inode from vnode. */
   2345 	mutex_enter(&node->tn_vlock);
   2346 	node->tn_vnode = NULL;
   2347 	vp->v_data = NULL;
   2348 	/* Check if tmpfs_vnode_get() is racing with us. */
   2349 	racing = TMPFS_NODE_RECLAIMING(node);
   2350 	mutex_exit(&node->tn_vlock);
   2351 
   2352 	/*
   2353 	 * If inode is not referenced, i.e. no links, then destroy it.
   2354 	 * Note: if racing - inode is about to get a new vnode, leave it.
   2355 	 */
   2356 	if (node->tn_links == 0 && !racing) {
   2357 		tmpfs_free_node(tmp, node);
   2358 	}
   2359 	return 0;
   2360 }
   2361 
   2362 int
   2363 tmpfs_pathconf(void *v)
   2364 {
   2365 	struct vop_pathconf_args /* {
   2366 		struct vnode	*a_vp;
   2367 		int		a_name;
   2368 		register_t	*a_retval;
   2369 	} */ *ap = v;
   2370 	const int name = ap->a_name;
   2371 	register_t *retval = ap->a_retval;
   2372 	int error = 0;
   2373 
   2374 	switch (name) {
   2375 	case _PC_LINK_MAX:
   2376 		*retval = LINK_MAX;
   2377 		break;
   2378 	case _PC_NAME_MAX:
   2379 		*retval = TMPFS_MAXNAMLEN;
   2380 		break;
   2381 	case _PC_PATH_MAX:
   2382 		*retval = PATH_MAX;
   2383 		break;
   2384 	case _PC_PIPE_BUF:
   2385 		*retval = PIPE_BUF;
   2386 		break;
   2387 	case _PC_CHOWN_RESTRICTED:
   2388 		*retval = 1;
   2389 		break;
   2390 	case _PC_NO_TRUNC:
   2391 		*retval = 1;
   2392 		break;
   2393 	case _PC_SYNC_IO:
   2394 		*retval = 1;
   2395 		break;
   2396 	case _PC_FILESIZEBITS:
   2397 		*retval = sizeof(off_t) * CHAR_BIT;
   2398 		break;
   2399 	default:
   2400 		error = EINVAL;
   2401 	}
   2402 	return error;
   2403 }
   2404 
   2405 int
   2406 tmpfs_advlock(void *v)
   2407 {
   2408 	struct vop_advlock_args /* {
   2409 		struct vnode	*a_vp;
   2410 		void *		a_id;
   2411 		int		a_op;
   2412 		struct flock	*a_fl;
   2413 		int		a_flags;
   2414 	} */ *ap = v;
   2415 	vnode_t *vp = ap->a_vp;
   2416 	tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp);
   2417 
   2418 	return lf_advlock(v, &node->tn_lockf, node->tn_size);
   2419 }
   2420 
   2421 int
   2422 tmpfs_getpages(void *v)
   2423 {
   2424 	struct vop_getpages_args /* {
   2425 		struct vnode *a_vp;
   2426 		voff_t a_offset;
   2427 		struct vm_page **a_m;
   2428 		int *a_count;
   2429 		int a_centeridx;
   2430 		vm_prot_t a_access_type;
   2431 		int a_advice;
   2432 		int a_flags;
   2433 	} */ * const ap = v;
   2434 	vnode_t *vp = ap->a_vp;
   2435 	const voff_t offset = ap->a_offset;
   2436 	struct vm_page **pgs = ap->a_m;
   2437 	const int centeridx = ap->a_centeridx;
   2438 	const vm_prot_t access_type = ap->a_access_type;
   2439 	const int advice = ap->a_advice;
   2440 	const int flags = ap->a_flags;
   2441 	int error, npages = *ap->a_count;
   2442 	tmpfs_node_t *node;
   2443 	struct uvm_object *uobj;
   2444 
   2445 	KASSERT(vp->v_type == VREG);
   2446 	KASSERT(mutex_owned(vp->v_interlock));
   2447 
   2448 	node = VP_TO_TMPFS_NODE(vp);
   2449 	uobj = node->tn_spec.tn_reg.tn_aobj;
   2450 
   2451 	/*
   2452 	 * Currently, PGO_PASTEOF is not supported.
   2453 	 */
   2454 	if (vp->v_size <= offset + (centeridx << PAGE_SHIFT)) {
   2455 		if ((flags & PGO_LOCKED) == 0)
   2456 			mutex_exit(vp->v_interlock);
   2457 		return EINVAL;
   2458 	}
   2459 
   2460 	if (vp->v_size < offset + (npages << PAGE_SHIFT)) {
   2461 		npages = (round_page(vp->v_size) - offset) >> PAGE_SHIFT;
   2462 	}
   2463 
   2464 	if ((flags & PGO_LOCKED) != 0)
   2465 		return EBUSY;
   2466 
   2467 	if ((flags & PGO_NOTIMESTAMP) == 0) {
   2468 		if ((vp->v_mount->mnt_flag & MNT_NOATIME) == 0)
   2469 			node->tn_status |= TMPFS_NODE_ACCESSED;
   2470 
   2471 		if ((access_type & VM_PROT_WRITE) != 0)
   2472 			node->tn_status |= TMPFS_NODE_MODIFIED;
   2473 	}
   2474 
   2475 	/*
   2476 	 * Invoke the pager.
   2477 	 *
   2478 	 * Clean the array of pages before.  XXX: PR/32166
   2479 	 * Note that vnode lock is shared with underlying UVM object.
   2480 	 */
   2481 	if (pgs) {
   2482 		memset(pgs, 0, sizeof(struct vm_pages *) * npages);
   2483 	}
   2484 	KASSERT(vp->v_interlock == uobj->vmobjlock);
   2485 
   2486 	error = (*uobj->pgops->pgo_get)(uobj, offset, pgs, &npages, centeridx,
   2487 	    access_type, advice, flags | PGO_ALLPAGES);
   2488 
   2489 #if defined(DEBUG)
   2490 	if (!error && pgs) {
   2491 		for (int i = 0; i < npages; i++) {
   2492 			KASSERT(pgs[i] != NULL);
   2493 		}
   2494 	}
   2495 #endif
   2496 	return error;
   2497 }
   2498 
   2499 int
   2500 tmpfs_putpages(void *v)
   2501 {
   2502 	struct vop_putpages_args /* {
   2503 		struct vnode *a_vp;
   2504 		voff_t a_offlo;
   2505 		voff_t a_offhi;
   2506 		int a_flags;
   2507 	} */ * const ap = v;
   2508 	vnode_t *vp = ap->a_vp;
   2509 	const voff_t offlo = ap->a_offlo;
   2510 	const voff_t offhi = ap->a_offhi;
   2511 	const int flags = ap->a_flags;
   2512 	tmpfs_node_t *node;
   2513 	struct uvm_object *uobj;
   2514 	int error;
   2515 
   2516 	KASSERT(mutex_owned(vp->v_interlock));
   2517 
   2518 	if (vp->v_type != VREG) {
   2519 		mutex_exit(vp->v_interlock);
   2520 		return 0;
   2521 	}
   2522 
   2523 	node = VP_TO_TMPFS_NODE(vp);
   2524 	uobj = node->tn_spec.tn_reg.tn_aobj;
   2525 
   2526 	KASSERT(vp->v_interlock == uobj->vmobjlock);
   2527 	error = (*uobj->pgops->pgo_put)(uobj, offlo, offhi, flags);
   2528 
   2529 	/* XXX mtime */
   2530 
   2531 	return error;
   2532 }
   2533 
   2534 int
   2535 tmpfs_whiteout(void *v)
   2536 {
   2537 	struct vop_whiteout_args /* {
   2538 		struct vnode		*a_dvp;
   2539 		struct componentname	*a_cnp;
   2540 		int			a_flags;
   2541 	} */ *ap = v;
   2542 	vnode_t *dvp = ap->a_dvp;
   2543 	struct componentname *cnp = ap->a_cnp;
   2544 	const int flags = ap->a_flags;
   2545 	tmpfs_mount_t *tmp = VFS_TO_TMPFS(dvp->v_mount);
   2546 	tmpfs_dirent_t *de;
   2547 	int error;
   2548 
   2549 	switch (flags) {
   2550 	case LOOKUP:
   2551 		break;
   2552 	case CREATE:
   2553 		error = tmpfs_alloc_dirent(tmp, cnp->cn_nameptr,
   2554 		    cnp->cn_namelen, &de);
   2555 		if (error)
   2556 			return error;
   2557 		tmpfs_dir_attach(dvp, de, TMPFS_NODE_WHITEOUT);
   2558 		break;
   2559 	case DELETE:
   2560 		cnp->cn_flags &= ~DOWHITEOUT; /* when in doubt, cargo cult */
   2561 		de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(dvp), cnp);
   2562 		if (de == NULL)
   2563 			return ENOENT;
   2564 		tmpfs_dir_detach(dvp, de);
   2565 		tmpfs_free_dirent(tmp, de);
   2566 		break;
   2567 	}
   2568 	return 0;
   2569 }
   2570 
   2571 int
   2572 tmpfs_print(void *v)
   2573 {
   2574 	struct vop_print_args /* {
   2575 		struct vnode	*a_vp;
   2576 	} */ *ap = v;
   2577 	vnode_t *vp = ap->a_vp;
   2578 	tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp);
   2579 
   2580 	printf("tag VT_TMPFS, tmpfs_node %p, flags 0x%x, links %d\n"
   2581 	    "\tmode 0%o, owner %d, group %d, size %" PRIdMAX ", status 0x%x",
   2582 	    node, node->tn_flags, node->tn_links, node->tn_mode, node->tn_uid,
   2583 	    node->tn_gid, (uintmax_t)node->tn_size, node->tn_status);
   2584 	if (vp->v_type == VFIFO) {
   2585 		VOCALL(fifo_vnodeop_p, VOFFSET(vop_print), v);
   2586 	}
   2587 	printf("\n");
   2588 	return 0;
   2589 }
   2590