Home | History | Annotate | Line # | Download | only in tmpfs
tmpfs_vnops.c revision 1.85
      1 /*	$NetBSD: tmpfs_vnops.c,v 1.85 2011/05/29 22:29:07 rmind 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.85 2011/05/29 22:29:07 rmind 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 #if TMPFS_WHITEOUT
    107 	{ &vop_whiteout_desc,		tmpfs_whiteout },
    108 #endif
    109 	{ NULL, NULL }
    110 };
    111 
    112 const struct vnodeopv_desc tmpfs_vnodeop_opv_desc = {
    113 	&tmpfs_vnodeop_p, tmpfs_vnodeop_entries
    114 };
    115 
    116 /*
    117  * tmpfs_lookup: path name traversal routine.
    118  *
    119  * Arguments: dvp (directory being searched), vpp (result),
    120  * cnp (component name - path).
    121  *
    122  * => Caller holds a reference and lock on dvp.
    123  * => We return looked-up vnode (vpp) locked, with a reference held.
    124  */
    125 int
    126 tmpfs_lookup(void *v)
    127 {
    128 	struct vop_lookup_args /* {
    129 		struct vnode *a_dvp;
    130 		struct vnode **a_vpp;
    131 		struct componentname *a_cnp;
    132 	} */ *ap = v;
    133 	vnode_t *dvp = ap->a_dvp, **vpp = ap->a_vpp;
    134 	struct componentname *cnp = ap->a_cnp;
    135 	const bool lastcn = (cnp->cn_flags & ISLASTCN) != 0;
    136 	tmpfs_node_t *dnode, *tnode;
    137 	tmpfs_dirent_t *de;
    138 	int error;
    139 
    140 	KASSERT(VOP_ISLOCKED(dvp));
    141 
    142 	dnode = VP_TO_TMPFS_DIR(dvp);
    143 	*vpp = NULL;
    144 
    145 	/* Check accessibility of directory. */
    146 	error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred);
    147 	if (error) {
    148 		goto out;
    149 	}
    150 
    151 	/*
    152 	 * If requesting the last path component on a read-only file system
    153 	 * with a write operation, deny it.
    154 	 */
    155 	if (lastcn && (dvp->v_mount->mnt_flag & MNT_RDONLY) != 0 &&
    156 	    (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
    157 		error = EROFS;
    158 		goto out;
    159 	}
    160 
    161 	/*
    162 	 * Avoid doing a linear scan of the directory if the requested
    163 	 * directory/name couple is already in the cache.
    164 	 */
    165 	error = cache_lookup(dvp, vpp, cnp);
    166 	if (error >= 0) {
    167 		/* Both cache-hit or an error case. */
    168 		goto out;
    169 	}
    170 
    171 	if (cnp->cn_flags & ISDOTDOT) {
    172 		tmpfs_node_t *pnode;
    173 
    174 		/*
    175 		 * Lookup of ".." case.
    176 		 */
    177 		if (lastcn && cnp->cn_nameiop == RENAME) {
    178 			error = EINVAL;
    179 			goto out;
    180 		}
    181 		KASSERT(dnode->tn_type == VDIR);
    182 		pnode = dnode->tn_spec.tn_dir.tn_parent;
    183 		if (pnode == NULL) {
    184 			error = ENOENT;
    185 			goto out;
    186 		}
    187 
    188 		/*
    189 		 * Lock the parent tn_vlock before releasing the vnode lock,
    190 		 * and thus prevents parent from disappearing.
    191 		 */
    192 		mutex_enter(&pnode->tn_vlock);
    193 		VOP_UNLOCK(dvp);
    194 
    195 		/*
    196 		 * Get a vnode of the '..' entry and re-acquire the lock.
    197 		 * Release the tn_vlock.
    198 		 */
    199 		error = tmpfs_vnode_get(dvp->v_mount, pnode, vpp);
    200 		vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
    201 		goto out;
    202 
    203 	} else if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') {
    204 		/*
    205 		 * Lookup of "." case.
    206 		 */
    207 		if (lastcn && cnp->cn_nameiop == RENAME) {
    208 			error = EISDIR;
    209 			goto out;
    210 		}
    211 		vref(dvp);
    212 		*vpp = dvp;
    213 		error = 0;
    214 		goto done;
    215 	}
    216 
    217 	/*
    218 	 * Other lookup cases: perform directory scan.
    219 	 */
    220 	de = tmpfs_dir_lookup(dnode, cnp);
    221 	if (de == NULL || de->td_node == TMPFS_NODE_WHITEOUT) {
    222 		/*
    223 		 * The entry was not found in the directory.  This is valid
    224 		 * if we are creating or renaming an entry and are working
    225 		 * on the last component of the path name.
    226 		 */
    227 		if (lastcn && (cnp->cn_nameiop == CREATE ||
    228 		    cnp->cn_nameiop == RENAME)) {
    229 			error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred);
    230 			if (error) {
    231 				goto out;
    232 			}
    233 			error = EJUSTRETURN;
    234 		} else {
    235 			error = ENOENT;
    236 		}
    237 		if (de) {
    238 			KASSERT(de->td_node == TMPFS_NODE_WHITEOUT);
    239 			cnp->cn_flags |= ISWHITEOUT;
    240 		}
    241 		goto done;
    242 	}
    243 
    244 	tnode = de->td_node;
    245 
    246 	/*
    247 	 * If it is not the last path component and found a non-directory
    248 	 * or non-link entry (which may itself be pointing to a directory),
    249 	 * raise an error.
    250 	 */
    251 	if (!lastcn && tnode->tn_type != VDIR && tnode->tn_type != VLNK) {
    252 		error = ENOTDIR;
    253 		goto out;
    254 	}
    255 
    256 	/* Check the permissions. */
    257 	if (lastcn && (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
    258 		kauth_action_t action = 0;
    259 
    260 		/* This is the file-system's decision. */
    261 		if ((dnode->tn_mode & S_ISTXT) != 0 &&
    262 		    kauth_cred_geteuid(cnp->cn_cred) != dnode->tn_uid &&
    263 		    kauth_cred_geteuid(cnp->cn_cred) != tnode->tn_uid) {
    264 			error = EPERM;
    265 		} else {
    266 			error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred);
    267 		}
    268 
    269 		if (cnp->cn_nameiop == DELETE) {
    270 			action |= KAUTH_VNODE_DELETE;
    271 		} else {
    272 			KASSERT(cnp->cn_nameiop == RENAME);
    273 			action |= KAUTH_VNODE_RENAME;
    274 		}
    275 		error = kauth_authorize_vnode(cnp->cn_cred,
    276 		    action, *vpp, dvp, error);
    277 		if (error) {
    278 			goto out;
    279 		}
    280 	}
    281 
    282 	/* Get a vnode for the matching entry. */
    283 	mutex_enter(&tnode->tn_vlock);
    284 	error = tmpfs_vnode_get(dvp->v_mount, tnode, vpp);
    285 done:
    286 	/*
    287 	 * Cache the result, unless request was for creation (as it does
    288 	 * not improve the performance).
    289 	 */
    290 	if ((cnp->cn_flags & MAKEENTRY) != 0 && cnp->cn_nameiop != CREATE) {
    291 		cache_enter(dvp, *vpp, cnp);
    292 	}
    293 out:
    294 	KASSERT((*vpp && VOP_ISLOCKED(*vpp)) || error);
    295 	KASSERT(VOP_ISLOCKED(dvp));
    296 
    297 	return error;
    298 }
    299 
    300 int
    301 tmpfs_create(void *v)
    302 {
    303 	struct vop_create_args /* {
    304 		struct vnode		*a_dvp;
    305 		struct vnode		**a_vpp;
    306 		struct componentname	*a_cnp;
    307 		struct vattr		*a_vap;
    308 	} */ *ap = v;
    309 	vnode_t *dvp = ap->a_dvp, **vpp = ap->a_vpp;
    310 	struct componentname *cnp = ap->a_cnp;
    311 	struct vattr *vap = ap->a_vap;
    312 
    313 	KASSERT(VOP_ISLOCKED(dvp));
    314 	KASSERT(vap->va_type == VREG || vap->va_type == VSOCK);
    315 	return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
    316 }
    317 
    318 int
    319 tmpfs_mknod(void *v)
    320 {
    321 	struct vop_mknod_args /* {
    322 		struct vnode		*a_dvp;
    323 		struct vnode		**a_vpp;
    324 		struct componentname	*a_cnp;
    325 		struct vattr		*a_vap;
    326 	} */ *ap = v;
    327 	vnode_t *dvp = ap->a_dvp, **vpp = ap->a_vpp;
    328 	struct componentname *cnp = ap->a_cnp;
    329 	struct vattr *vap = ap->a_vap;
    330 	enum vtype vt = vap->va_type;
    331 
    332 	if (vt != VBLK && vt != VCHR && vt != VFIFO) {
    333 		vput(dvp);
    334 		return EINVAL;
    335 	}
    336 	return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
    337 }
    338 
    339 int
    340 tmpfs_open(void *v)
    341 {
    342 	struct vop_open_args /* {
    343 		struct vnode	*a_vp;
    344 		int		a_mode;
    345 		kauth_cred_t	a_cred;
    346 	} */ *ap = v;
    347 	vnode_t *vp = ap->a_vp;
    348 	mode_t mode = ap->a_mode;
    349 	tmpfs_node_t *node;
    350 
    351 	KASSERT(VOP_ISLOCKED(vp));
    352 
    353 	node = VP_TO_TMPFS_NODE(vp);
    354 	if (node->tn_links < 1) {
    355 		/*
    356 		 * The file is still active, but all its names have been
    357 		 * removed (e.g. by a "rmdir $(pwd)").  It cannot be opened
    358 		 * any more, as it is about to be destroyed.
    359 		 */
    360 		return ENOENT;
    361 	}
    362 
    363 	/* If the file is marked append-only, deny write requests. */
    364 	if ((node->tn_flags & APPEND) != 0 &&
    365 	    (mode & (FWRITE | O_APPEND)) == FWRITE) {
    366 		return EPERM;
    367 	}
    368 	return 0;
    369 }
    370 
    371 int
    372 tmpfs_close(void *v)
    373 {
    374 	struct vop_close_args /* {
    375 		struct vnode	*a_vp;
    376 		int		a_fflag;
    377 		kauth_cred_t	a_cred;
    378 	} */ *ap = v;
    379 	vnode_t *vp = ap->a_vp;
    380 
    381 	KASSERT(VOP_ISLOCKED(vp));
    382 
    383 	tmpfs_update(vp, NULL, NULL, NULL, UPDATE_CLOSE);
    384 	return 0;
    385 }
    386 
    387 static int
    388 tmpfs_check_possible(vnode_t *vp, tmpfs_node_t *node, mode_t mode)
    389 {
    390 	const bool writing = (mode & VWRITE) != 0;
    391 
    392 	switch (vp->v_type) {
    393 	case VDIR:
    394 	case VLNK:
    395 	case VREG:
    396 		if (writing && (vp->v_mount->mnt_flag & MNT_RDONLY) != 0) {
    397 			return EROFS;
    398 		}
    399 		break;
    400 	case VBLK:
    401 	case VCHR:
    402 	case VSOCK:
    403 	case VFIFO:
    404 		break;
    405 	default:
    406 		return EINVAL;
    407 	}
    408 	return (writing && (node->tn_flags & IMMUTABLE) != 0) ? EPERM : 0;
    409 }
    410 
    411 static int
    412 tmpfs_check_permitted(vnode_t *vp, tmpfs_node_t *node, mode_t mode,
    413     kauth_cred_t cred)
    414 {
    415 
    416 	return genfs_can_access(vp->v_type, node->tn_mode, node->tn_uid,
    417 	    node->tn_gid, mode, cred);
    418 }
    419 
    420 int
    421 tmpfs_access(void *v)
    422 {
    423 	struct vop_access_args /* {
    424 		struct vnode	*a_vp;
    425 		int		a_mode;
    426 		kauth_cred_t	a_cred;
    427 	} */ *ap = v;
    428 	vnode_t *vp = ap->a_vp;
    429 	mode_t mode = ap->a_mode;
    430 	kauth_cred_t cred = ap->a_cred;
    431 	tmpfs_node_t *node;
    432 	int error;
    433 
    434 	KASSERT(VOP_ISLOCKED(vp));
    435 
    436 	node = VP_TO_TMPFS_NODE(vp);
    437 	error = tmpfs_check_possible(vp, node, mode);
    438 	if (error) {
    439 		return error;
    440 	}
    441 	return kauth_authorize_vnode(cred, kauth_mode_to_action(mode), vp,
    442 	    NULL, tmpfs_check_permitted(vp, node, mode, cred));
    443 }
    444 
    445 int
    446 tmpfs_getattr(void *v)
    447 {
    448 	struct vop_getattr_args /* {
    449 		struct vnode	*a_vp;
    450 		struct vattr	*a_vap;
    451 		kauth_cred_t	a_cred;
    452 	} */ *ap = v;
    453 	vnode_t *vp = ap->a_vp;
    454 	struct vattr *vap = ap->a_vap;
    455 	tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp);
    456 
    457 	vattr_null(vap);
    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 	tmpfs_update(vp, NULL, NULL, NULL, 0);
    482 	return 0;
    483 }
    484 
    485 #define GOODTIME(tv)	((tv)->tv_sec != VNOVAL || (tv)->tv_nsec != VNOVAL)
    486 /* XXX Should this operation be atomic?  I think it should, but code in
    487  * XXX other places (e.g., ufs) doesn't seem to be... */
    488 int
    489 tmpfs_setattr(void *v)
    490 {
    491 	struct vop_setattr_args /* {
    492 		struct vnode	*a_vp;
    493 		struct vattr	*a_vap;
    494 		kauth_cred_t	a_cred;
    495 	} */ *ap = v;
    496 	vnode_t *vp = ap->a_vp;
    497 	struct vattr *vap = ap->a_vap;
    498 	kauth_cred_t cred = ap->a_cred;
    499 	lwp_t *l = curlwp;
    500 	int error = 0;
    501 
    502 	KASSERT(VOP_ISLOCKED(vp));
    503 
    504 	/* Abort if any unsettable attribute is given. */
    505 	if (vap->va_type != VNON || vap->va_nlink != VNOVAL ||
    506 	    vap->va_fsid != VNOVAL || vap->va_fileid != VNOVAL ||
    507 	    vap->va_blocksize != VNOVAL || GOODTIME(&vap->va_ctime) ||
    508 	    vap->va_gen != VNOVAL || vap->va_rdev != VNOVAL ||
    509 	    vap->va_bytes != VNOVAL) {
    510 		return EINVAL;
    511 	}
    512 	if (error == 0 && (vap->va_flags != VNOVAL))
    513 		error = tmpfs_chflags(vp, vap->va_flags, cred, l);
    514 
    515 	if (error == 0 && (vap->va_size != VNOVAL))
    516 		error = tmpfs_chsize(vp, vap->va_size, cred, l);
    517 
    518 	if (error == 0 && (vap->va_uid != VNOVAL || vap->va_gid != VNOVAL))
    519 		error = tmpfs_chown(vp, vap->va_uid, vap->va_gid, cred, l);
    520 
    521 	if (error == 0 && (vap->va_mode != VNOVAL))
    522 		error = tmpfs_chmod(vp, vap->va_mode, cred, l);
    523 
    524 	if (error == 0 && (GOODTIME(&vap->va_atime) || GOODTIME(&vap->va_mtime)
    525 	    || GOODTIME(&vap->va_birthtime))) {
    526 		error = tmpfs_chtimes(vp, &vap->va_atime, &vap->va_mtime,
    527 		    &vap->va_birthtime, vap->va_vaflags, cred, l);
    528 		if (error == 0)
    529 			return 0;
    530 	}
    531 	tmpfs_update(vp, NULL, NULL, NULL, 0);
    532 	return error;
    533 }
    534 
    535 int
    536 tmpfs_read(void *v)
    537 {
    538 	struct vop_read_args /* {
    539 		struct vnode *a_vp;
    540 		struct uio *a_uio;
    541 		int a_ioflag;
    542 		kauth_cred_t a_cred;
    543 	} */ *ap = v;
    544 	vnode_t *vp = ap->a_vp;
    545 	struct uio *uio = ap->a_uio;
    546 	const int ioflag = ap->a_ioflag;
    547 	tmpfs_node_t *node;
    548 	struct uvm_object *uobj;
    549 	int error;
    550 
    551 	KASSERT(VOP_ISLOCKED(vp));
    552 
    553 	if (vp->v_type != VREG) {
    554 		return EISDIR;
    555 	}
    556 	if (uio->uio_offset < 0) {
    557 		return EINVAL;
    558 	}
    559 
    560 	node = VP_TO_TMPFS_NODE(vp);
    561 	node->tn_status |= TMPFS_NODE_ACCESSED;
    562 	uobj = node->tn_spec.tn_reg.tn_aobj;
    563 	error = 0;
    564 
    565 	while (error == 0 && uio->uio_resid > 0) {
    566 		vsize_t len;
    567 
    568 		if (node->tn_size <= uio->uio_offset) {
    569 			break;
    570 		}
    571 		len = MIN(node->tn_size - uio->uio_offset, uio->uio_resid);
    572 		if (len == 0) {
    573 			break;
    574 		}
    575 		error = ubc_uiomove(uobj, uio, len, IO_ADV_DECODE(ioflag),
    576 		    UBC_READ | UBC_PARTIALOK | UBC_UNMAP_FLAG(vp));
    577 	}
    578 	return error;
    579 }
    580 
    581 int
    582 tmpfs_write(void *v)
    583 {
    584 	struct vop_write_args /* {
    585 		struct vnode	*a_vp;
    586 		struct uio	*a_uio;
    587 		int		a_ioflag;
    588 		kauth_cred_t	a_cred;
    589 	} */ *ap = v;
    590 	vnode_t *vp = ap->a_vp;
    591 	struct uio *uio = ap->a_uio;
    592 	const int ioflag = ap->a_ioflag;
    593 	tmpfs_node_t *node;
    594 	struct uvm_object *uobj;
    595 	off_t oldsize;
    596 	bool extended;
    597 	int error;
    598 
    599 	KASSERT(VOP_ISLOCKED(vp));
    600 
    601 	node = VP_TO_TMPFS_NODE(vp);
    602 	oldsize = node->tn_size;
    603 
    604 	if (uio->uio_offset < 0 || vp->v_type != VREG) {
    605 		error = EINVAL;
    606 		goto out;
    607 	}
    608 	if (uio->uio_resid == 0) {
    609 		error = 0;
    610 		goto out;
    611 	}
    612 	if (ioflag & IO_APPEND) {
    613 		uio->uio_offset = node->tn_size;
    614 	}
    615 
    616 	extended = uio->uio_offset + uio->uio_resid > node->tn_size;
    617 	if (extended) {
    618 		error = tmpfs_reg_resize(vp, uio->uio_offset + uio->uio_resid);
    619 		if (error)
    620 			goto out;
    621 	}
    622 
    623 	uobj = node->tn_spec.tn_reg.tn_aobj;
    624 	error = 0;
    625 	while (error == 0 && uio->uio_resid > 0) {
    626 		vsize_t len;
    627 
    628 		len = MIN(node->tn_size - uio->uio_offset, uio->uio_resid);
    629 		if (len == 0) {
    630 			break;
    631 		}
    632 		error = ubc_uiomove(uobj, uio, len, IO_ADV_DECODE(ioflag),
    633 		    UBC_WRITE | UBC_UNMAP_FLAG(vp));
    634 	}
    635 	if (error) {
    636 		(void)tmpfs_reg_resize(vp, oldsize);
    637 	}
    638 
    639 	node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED |
    640 	    (extended ? TMPFS_NODE_CHANGED : 0);
    641 	VN_KNOTE(vp, NOTE_WRITE);
    642 out:
    643 	if (error) {
    644 		KASSERT(oldsize == node->tn_size);
    645 	} else {
    646 		KASSERT(uio->uio_resid == 0);
    647 	}
    648 	return error;
    649 }
    650 
    651 int
    652 tmpfs_fsync(void *v)
    653 {
    654 	struct vop_fsync_args /* {
    655 		struct vnode *a_vp;
    656 		kauth_cred_t a_cred;
    657 		int a_flags;
    658 		off_t a_offlo;
    659 		off_t a_offhi;
    660 		struct lwp *a_l;
    661 	} */ *ap = v;
    662 	vnode_t *vp = ap->a_vp;
    663 
    664 	/* Nothing to do.  Just update. */
    665 	KASSERT(VOP_ISLOCKED(vp));
    666 	tmpfs_update(vp, NULL, NULL, NULL, 0);
    667 	return 0;
    668 }
    669 
    670 /*
    671  * tmpfs_remove: unlink a file.
    672  *
    673  * => Both directory (dvp) and file (vp) are locked.
    674  * => We unlock and drop the reference on both.
    675  */
    676 int
    677 tmpfs_remove(void *v)
    678 {
    679 	struct vop_remove_args /* {
    680 		struct vnode *a_dvp;
    681 		struct vnode *a_vp;
    682 		struct componentname *a_cnp;
    683 	} */ *ap = v;
    684 	vnode_t *dvp = ap->a_dvp, *vp = ap->a_vp;
    685 	tmpfs_node_t *node;
    686 	tmpfs_dirent_t *de;
    687 	int error;
    688 
    689 	KASSERT(VOP_ISLOCKED(dvp));
    690 	KASSERT(VOP_ISLOCKED(vp));
    691 
    692 	if (vp->v_type == VDIR) {
    693 		error = EPERM;
    694 		goto out;
    695 	}
    696 	node = VP_TO_TMPFS_NODE(vp);
    697 
    698 	/* Files marked as immutable or append-only cannot be deleted. */
    699 	if (node->tn_flags & (IMMUTABLE | APPEND)) {
    700 		error = EPERM;
    701 		goto out;
    702 	}
    703 
    704 	/* Lookup the directory entry (check the cached hint first). */
    705 	de = tmpfs_dir_cached(node);
    706 	if (de == NULL) {
    707 		tmpfs_node_t *dnode = VP_TO_TMPFS_DIR(dvp);
    708 		struct componentname *cnp = ap->a_cnp;
    709 		de = tmpfs_dir_lookup(dnode, cnp);
    710 	}
    711 	KASSERT(de && de->td_node == node);
    712 
    713 	/*
    714 	 * Remove the entry from the directory (drops the link count) and
    715 	 * destroy it.  Note: the inode referred by it will not be destroyed
    716 	 * until the vnode is reclaimed/recycled.
    717 	 */
    718 	tmpfs_dir_detach(dvp, de);
    719 	tmpfs_free_dirent(VFS_TO_TMPFS(vp->v_mount), de);
    720 	error = 0;
    721 out:
    722 	/* Drop the references and unlock the vnodes. */
    723 	vput(vp);
    724 	if (dvp == vp) {
    725 		vrele(dvp);
    726 	} else {
    727 		vput(dvp);
    728 	}
    729 	return error;
    730 }
    731 
    732 /*
    733  * tmpfs_link: create a hard link.
    734  */
    735 int
    736 tmpfs_link(void *v)
    737 {
    738 	struct vop_link_args /* {
    739 		struct vnode *a_dvp;
    740 		struct vnode *a_vp;
    741 		struct componentname *a_cnp;
    742 	} */ *ap = v;
    743 	vnode_t *dvp = ap->a_dvp;
    744 	vnode_t *vp = ap->a_vp;
    745 	struct componentname *cnp = ap->a_cnp;
    746 	tmpfs_node_t *dnode, *node;
    747 	tmpfs_dirent_t *de;
    748 	int error;
    749 
    750 	KASSERT(dvp != vp);
    751 	KASSERT(VOP_ISLOCKED(dvp));
    752 	KASSERT(vp->v_type != VDIR);
    753 	KASSERT(dvp->v_mount == vp->v_mount);
    754 
    755 	dnode = VP_TO_TMPFS_DIR(dvp);
    756 	node = VP_TO_TMPFS_NODE(vp);
    757 
    758 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    759 
    760 	/* Check for maximum number of links limit. */
    761 	if (node->tn_links == LINK_MAX) {
    762 		error = EMLINK;
    763 		goto out;
    764 	}
    765 	KASSERT(node->tn_links < LINK_MAX);
    766 
    767 	/* We cannot create links of files marked immutable or append-only. */
    768 	if (node->tn_flags & (IMMUTABLE | APPEND)) {
    769 		error = EPERM;
    770 		goto out;
    771 	}
    772 
    773 	/* Allocate a new directory entry to represent the inode. */
    774 	error = tmpfs_alloc_dirent(VFS_TO_TMPFS(vp->v_mount),
    775 	    cnp->cn_nameptr, cnp->cn_namelen, &de);
    776 	if (error) {
    777 		goto out;
    778 	}
    779 
    780 	/*
    781 	 * Insert the entry into the directory.
    782 	 * It will increase the inode link count.
    783 	 */
    784 	tmpfs_dir_attach(dvp, de, node);
    785 
    786 	/* Update the timestamps and trigger the event. */
    787 	if (node->tn_vnode) {
    788 		VN_KNOTE(node->tn_vnode, NOTE_LINK);
    789 	}
    790 	node->tn_status |= TMPFS_NODE_CHANGED;
    791 	tmpfs_update(vp, NULL, NULL, NULL, 0);
    792 	error = 0;
    793 out:
    794 	VOP_UNLOCK(vp);
    795 	vput(dvp);
    796 	return error;
    797 }
    798 
    799 /*
    800  * tmpfs_parentcheck_p: check if 'lower' is a descendent of 'upper'.
    801  *
    802  * => Returns 'true' if parent, and 'false' otherwise.
    803  */
    804 static inline bool
    805 tmpfs_parentcheck_p(tmpfs_node_t *lower, tmpfs_node_t *upper)
    806 {
    807 	tmpfs_node_t *un = lower;
    808 
    809 	while (un != un->tn_spec.tn_dir.tn_parent) {
    810 		KASSERT(un->tn_type == VDIR);
    811 		if (un == upper) {
    812 			return true;
    813 		}
    814 		un = un->tn_spec.tn_dir.tn_parent;
    815 	}
    816 	return false;
    817 }
    818 
    819 /*
    820  * tmpfs_rename: rename routine.
    821  *
    822  * Arguments: fdvp (from-parent vnode), fvp (from-leaf), tdvp (to-parent)
    823  * and tvp (to-leaf), if exists (NULL if not).
    824  *
    825  * => Caller holds a reference on fdvp and fvp, they are unlocked.
    826  *    Note: fdvp and fvp can refer to the same object (i.e. when it is root).
    827  *
    828  * => Both tdvp and tvp are referenced and locked.  It is our responsibility
    829  *    to release the references and unlock them (or destroy).
    830  */
    831 int
    832 tmpfs_rename(void *v)
    833 {
    834 	struct vop_rename_args  /* {
    835 		struct vnode		*a_fdvp;
    836 		struct vnode		*a_fvp;
    837 		struct componentname	*a_fcnp;
    838 		struct vnode		*a_tdvp;
    839 		struct vnode		*a_tvp;
    840 		struct componentname	*a_tcnp;
    841 	} */ *ap = v;
    842 	vnode_t *fdvp = ap->a_fdvp;
    843 	vnode_t *fvp = ap->a_fvp;
    844 	struct componentname *fcnp = ap->a_fcnp;
    845 	vnode_t *tdvp = ap->a_tdvp;
    846 	vnode_t *tvp = ap->a_tvp;
    847 	struct componentname *tcnp = ap->a_tcnp;
    848 	tmpfs_node_t *fdnode, *fnode, *tnode, *tdnode;
    849 	tmpfs_dirent_t *de;
    850 	tmpfs_mount_t *tmp;
    851 	size_t namelen;
    852 	char *newname;
    853 	int error;
    854 
    855 	KASSERT(VOP_ISLOCKED(tdvp));
    856 	KASSERT(tvp == NULL || VOP_ISLOCKED(tvp) == LK_EXCLUSIVE);
    857 	KASSERT((fcnp->cn_flags & ISDOTDOT) == 0);
    858 	KASSERT((tcnp->cn_flags & ISDOTDOT) == 0);
    859 
    860 	newname = NULL;
    861 	namelen = 0;
    862 	tmp = NULL;
    863 
    864 	/* Disallow cross-device renames. */
    865 	if (fvp->v_mount != tdvp->v_mount ||
    866 	    (tvp != NULL && fvp->v_mount != tvp->v_mount)) {
    867 		error = EXDEV;
    868 		goto out_unlocked;
    869 	}
    870 
    871 	fnode = VP_TO_TMPFS_NODE(fvp);
    872 	fdnode = VP_TO_TMPFS_DIR(fdvp);
    873 	tnode = (tvp == NULL) ? NULL : VP_TO_TMPFS_NODE(tvp);
    874 	tdnode = VP_TO_TMPFS_DIR(tdvp);
    875 	tmp = VFS_TO_TMPFS(tdvp->v_mount);
    876 
    877 	if (fdvp == tvp) {
    878 		error = 0;
    879 		goto out_unlocked;
    880 	}
    881 
    882 	/* Allocate memory, if necessary, for a new name. */
    883 	namelen = tcnp->cn_namelen;
    884 	if (tmpfs_strname_neqlen(fcnp, tcnp)) {
    885 		newname = tmpfs_strname_alloc(tmp, namelen);
    886 		if (newname == NULL) {
    887 			error = ENOSPC;
    888 			goto out_unlocked;
    889 		}
    890 	}
    891 
    892 	/* XXX: Lock order violation! */
    893 	if (fdvp != tdvp) {
    894 		vn_lock(fdvp, LK_EXCLUSIVE | LK_RETRY);
    895 	}
    896 	if (fvp != tvp) {
    897 		vn_lock(fvp, LK_EXCLUSIVE | LK_RETRY);
    898 	}
    899 
    900 	/* If the inode we were renaming has scarpered, just give up. */
    901 	de = tmpfs_dir_lookup(fdnode, fcnp);
    902 	if (de == NULL || de->td_node != fnode) {
    903 		error = ENOENT;
    904 		goto out;
    905 	}
    906 
    907 	/*
    908 	 * If source and target is the same vnode - it is either invalid
    909 	 * rename of a directory, or a hard link.  Remove the source link,
    910 	 * if the later.
    911 	 */
    912 	if (fvp == tvp) {
    913 		if (fvp->v_type == VDIR) {
    914 			error = EINVAL;
    915 			goto out;
    916 		}
    917 		/*
    918 		 * Detach and free the directory entry.  Drops the link
    919 		 * count on the inode.
    920 		 */
    921 		KASSERT(fnode == tnode);
    922 		tmpfs_dir_detach(fdvp, de);
    923 		tmpfs_free_dirent(tmp, de);
    924 		goto out_ok;
    925 	}
    926 
    927 	/* If replacing an existing entry, ensure we can do the operation. */
    928 	if (tvp != NULL) {
    929 		KASSERT(tnode != NULL);
    930 		if (fnode->tn_type == VDIR && tnode->tn_type == VDIR) {
    931 			if (tnode->tn_size > 0) {
    932 				error = ENOTEMPTY;
    933 				goto out;
    934 			}
    935 		} else if (fnode->tn_type == VDIR && tnode->tn_type != VDIR) {
    936 			error = ENOTDIR;
    937 			goto out;
    938 		} else if (fnode->tn_type != VDIR && tnode->tn_type == VDIR) {
    939 			error = EISDIR;
    940 			goto out;
    941 		} else {
    942 			KASSERT(fnode->tn_type != VDIR);
    943 			KASSERT(tnode->tn_type != VDIR);
    944 		}
    945 	}
    946 
    947 	/* Are we moving the inode to a different directory? */
    948 	if (fdvp != tdvp) {
    949 		/*
    950 		 * If we are moving a directory - ensure that it is not
    951 		 * parent of a target directory.  Otherwise, it would
    952 		 * result in stale nodes.
    953 		 */
    954 		if (fnode->tn_type == VDIR &&
    955 		    tmpfs_parentcheck_p(tdnode, fnode)) {
    956 			error = EINVAL;
    957 			goto out;
    958 		}
    959 
    960 		/*
    961 		 * Perform the move: detach from the source directory and
    962 		 * attach into the target directory.
    963 		 */
    964 		tmpfs_dir_detach(fdvp, de);
    965 		tmpfs_dir_attach(tdvp, de, fnode);
    966 
    967 	} else if (tvp == NULL) {
    968 		/* Trigger the event, if not overwriting. */
    969 		VN_KNOTE(tdvp, NOTE_WRITE);
    970 	}
    971 
    972 	/* Are we overwriting the entry? */
    973 	if (tvp != NULL) {
    974 		tmpfs_dirent_t *tde;
    975 
    976 		tde = tmpfs_dir_cached(tnode);
    977 		if (tde == NULL) {
    978 			tde = tmpfs_dir_lookup(tdnode, tcnp);
    979 		}
    980 		KASSERT(tde && tde->td_node == tnode);
    981 		KASSERT(tnode->tn_type == fnode->tn_type);
    982 
    983 		/*
    984 		 * Remove and destroy the directory entry on the target
    985 		 * directory, since we overwrite it.
    986 		 */
    987 		tmpfs_dir_detach(tdvp, tde);
    988 		tmpfs_free_dirent(tmp, tde);
    989 	}
    990 
    991 	/* If the name has changed, update directory entry. */
    992 	if (newname != NULL) {
    993 		KASSERT(tcnp->cn_namelen < MAXNAMLEN);
    994 
    995 		tmpfs_strname_free(tmp, de->td_name, de->td_namelen);
    996 		de->td_namelen = (uint16_t)namelen;
    997 		memcpy(newname, tcnp->cn_nameptr, namelen);
    998 		de->td_name = newname;
    999 		newname = NULL;
   1000 
   1001 		fnode->tn_status |= TMPFS_NODE_CHANGED;
   1002 		tdnode->tn_status |= TMPFS_NODE_MODIFIED;
   1003 	}
   1004 out_ok:
   1005 	/* Trigger the rename event. */
   1006 	VN_KNOTE(fvp, NOTE_RENAME);
   1007 	error = 0;
   1008 out:
   1009 	if (fdvp != tdvp) {
   1010 		VOP_UNLOCK(fdvp);
   1011 	}
   1012 	if (fvp != tvp) {
   1013 		VOP_UNLOCK(fvp);
   1014 	}
   1015 out_unlocked:
   1016 	/* Release target nodes. */
   1017 	if (tdvp == tvp) {
   1018 		vrele(tdvp);
   1019 	} else {
   1020 		vput(tdvp);
   1021 	}
   1022 	if (tvp) {
   1023 		vput(tvp);
   1024 	}
   1025 
   1026 	/* Release source nodes. */
   1027 	vrele(fdvp);
   1028 	vrele(fvp);
   1029 
   1030 	if (newname != NULL) {
   1031 		tmpfs_strname_free(tmp, newname, namelen);
   1032 	}
   1033 	return error;
   1034 }
   1035 
   1036 int
   1037 tmpfs_mkdir(void *v)
   1038 {
   1039 	struct vop_mkdir_args /* {
   1040 		struct vnode		*a_dvp;
   1041 		struct vnode		**a_vpp;
   1042 		struct componentname	*a_cnp;
   1043 		struct vattr		*a_vap;
   1044 	} */ *ap = v;
   1045 	vnode_t *dvp = ap->a_dvp;
   1046 	vnode_t **vpp = ap->a_vpp;
   1047 	struct componentname *cnp = ap->a_cnp;
   1048 	struct vattr *vap = ap->a_vap;
   1049 
   1050 	KASSERT(vap->va_type == VDIR);
   1051 	return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
   1052 }
   1053 
   1054 int
   1055 tmpfs_rmdir(void *v)
   1056 {
   1057 	struct vop_rmdir_args /* {
   1058 		struct vnode		*a_dvp;
   1059 		struct vnode		*a_vp;
   1060 		struct componentname	*a_cnp;
   1061 	} */ *ap = v;
   1062 	vnode_t *dvp = ap->a_dvp;
   1063 	vnode_t *vp = ap->a_vp;
   1064 	tmpfs_mount_t *tmp = VFS_TO_TMPFS(dvp->v_mount);
   1065 	tmpfs_node_t *dnode = VP_TO_TMPFS_DIR(dvp);
   1066 	tmpfs_node_t *node = VP_TO_TMPFS_DIR(vp);
   1067 	tmpfs_dirent_t *de;
   1068 	int error = 0;
   1069 
   1070 	KASSERT(VOP_ISLOCKED(dvp));
   1071 	KASSERT(VOP_ISLOCKED(vp));
   1072 	KASSERT(node->tn_spec.tn_dir.tn_parent == dnode);
   1073 
   1074 	/*
   1075 	 * Directories with more than two entries ('.' and '..') cannot
   1076 	 * be removed.
   1077 	 */
   1078 	if (node->tn_size > 0) {
   1079 		error = ENOTEMPTY;
   1080 		goto out;
   1081 	}
   1082 
   1083 	/* Lookup the directory entry (check the cached hint first). */
   1084 	de = tmpfs_dir_cached(node);
   1085 	if (de == NULL) {
   1086 		struct componentname *cnp = ap->a_cnp;
   1087 		de = tmpfs_dir_lookup(dnode, cnp);
   1088 	}
   1089 	KASSERT(de && de->td_node == node);
   1090 
   1091 	/* Check flags to see if we are allowed to remove the directory. */
   1092 	if (dnode->tn_flags & APPEND || node->tn_flags & (IMMUTABLE | APPEND)) {
   1093 		error = EPERM;
   1094 		goto out;
   1095 	}
   1096 
   1097 	/* Detach the directory entry from the directory. */
   1098 	tmpfs_dir_detach(dvp, de);
   1099 
   1100 	/* Decrement the link count for the virtual '.' entry. */
   1101 	node->tn_links--;
   1102 	node->tn_status |= TMPFS_NODE_STATUSALL;
   1103 
   1104 	/* Purge the cache for parent. */
   1105 	cache_purge(dvp);
   1106 
   1107 	/*
   1108 	 * Destroy the directory entry.  Note: the inode referred by it
   1109 	 * will not be destroyed until the vnode is reclaimed.
   1110 	 */
   1111 	tmpfs_free_dirent(tmp, de);
   1112 	KASSERT(node->tn_links == 0);
   1113 out:
   1114 	/* Release the nodes. */
   1115 	vput(dvp);
   1116 	vput(vp);
   1117 	return error;
   1118 }
   1119 
   1120 int
   1121 tmpfs_symlink(void *v)
   1122 {
   1123 	struct vop_symlink_args /* {
   1124 		struct vnode		*a_dvp;
   1125 		struct vnode		**a_vpp;
   1126 		struct componentname	*a_cnp;
   1127 		struct vattr		*a_vap;
   1128 		char			*a_target;
   1129 	} */ *ap = v;
   1130 	vnode_t *dvp = ap->a_dvp;
   1131 	vnode_t **vpp = ap->a_vpp;
   1132 	struct componentname *cnp = ap->a_cnp;
   1133 	struct vattr *vap = ap->a_vap;
   1134 	char *target = ap->a_target;
   1135 
   1136 	KASSERT(vap->va_type == VLNK);
   1137 	return tmpfs_alloc_file(dvp, vpp, vap, cnp, target);
   1138 }
   1139 
   1140 int
   1141 tmpfs_readdir(void *v)
   1142 {
   1143 	struct vop_readdir_args /* {
   1144 		struct vnode	*a_vp;
   1145 		struct uio	*a_uio;
   1146 		kauth_cred_t	a_cred;
   1147 		int		*a_eofflag;
   1148 		off_t		**a_cookies;
   1149 		int		*ncookies;
   1150 	} */ *ap = v;
   1151 	vnode_t *vp = ap->a_vp;
   1152 	struct uio *uio = ap->a_uio;
   1153 	int *eofflag = ap->a_eofflag;
   1154 	off_t **cookies = ap->a_cookies;
   1155 	int *ncookies = ap->a_ncookies;
   1156 	off_t startoff, cnt;
   1157 	tmpfs_node_t *node;
   1158 	int error;
   1159 
   1160 	KASSERT(VOP_ISLOCKED(vp));
   1161 
   1162 	/* This operation only makes sense on directory nodes. */
   1163 	if (vp->v_type != VDIR) {
   1164 		return ENOTDIR;
   1165 	}
   1166 	node = VP_TO_TMPFS_DIR(vp);
   1167 	startoff = uio->uio_offset;
   1168 	cnt = 0;
   1169 
   1170 	if (uio->uio_offset == TMPFS_DIRCOOKIE_DOT) {
   1171 		error = tmpfs_dir_getdotdent(node, uio);
   1172 		if (error != 0) {
   1173 			if (error == -1)
   1174 				error = 0;
   1175 			goto out;
   1176 		}
   1177 		cnt++;
   1178 	}
   1179 	if (uio->uio_offset == TMPFS_DIRCOOKIE_DOTDOT) {
   1180 		error = tmpfs_dir_getdotdotdent(node, uio);
   1181 		if (error != 0) {
   1182 			if (error == -1)
   1183 				error = 0;
   1184 			goto out;
   1185 		}
   1186 		cnt++;
   1187 	}
   1188 	error = tmpfs_dir_getdents(node, uio, &cnt);
   1189 	if (error == -1) {
   1190 		error = 0;
   1191 	}
   1192 	KASSERT(error >= 0);
   1193 out:
   1194 	if (eofflag != NULL) {
   1195 		*eofflag = (!error && uio->uio_offset == TMPFS_DIRCOOKIE_EOF);
   1196 	}
   1197 	if (error || cookies == NULL || ncookies == NULL) {
   1198 		return error;
   1199 	}
   1200 
   1201 	/* Update NFS-related variables, if any. */
   1202 	off_t i, off = startoff;
   1203 	tmpfs_dirent_t *de = NULL;
   1204 
   1205 	*cookies = malloc(cnt * sizeof(off_t), M_TEMP, M_WAITOK);
   1206 	*ncookies = cnt;
   1207 
   1208 	for (i = 0; i < cnt; i++) {
   1209 		KASSERT(off != TMPFS_DIRCOOKIE_EOF);
   1210 		if (off != TMPFS_DIRCOOKIE_DOT) {
   1211 			if (off == TMPFS_DIRCOOKIE_DOTDOT) {
   1212 				de = TAILQ_FIRST(&node->tn_spec.tn_dir.tn_dir);
   1213 			} else if (de != NULL) {
   1214 				de = TAILQ_NEXT(de, td_entries);
   1215 			} else {
   1216 				de = tmpfs_dir_lookupbycookie(node, off);
   1217 				KASSERT(de != NULL);
   1218 				de = TAILQ_NEXT(de, td_entries);
   1219 			}
   1220 			if (de == NULL) {
   1221 				off = TMPFS_DIRCOOKIE_EOF;
   1222 			} else {
   1223 				off = tmpfs_dircookie(de);
   1224 			}
   1225 		} else {
   1226 			off = TMPFS_DIRCOOKIE_DOTDOT;
   1227 		}
   1228 		(*cookies)[i] = off;
   1229 	}
   1230 	KASSERT(uio->uio_offset == off);
   1231 	return error;
   1232 }
   1233 
   1234 int
   1235 tmpfs_readlink(void *v)
   1236 {
   1237 	struct vop_readlink_args /* {
   1238 		struct vnode	*a_vp;
   1239 		struct uio	*a_uio;
   1240 		kauth_cred_t	a_cred;
   1241 	} */ *ap = v;
   1242 	vnode_t *vp = ap->a_vp;
   1243 	struct uio *uio = ap->a_uio;
   1244 	tmpfs_node_t *node;
   1245 	int error;
   1246 
   1247 	KASSERT(VOP_ISLOCKED(vp));
   1248 	KASSERT(uio->uio_offset == 0);
   1249 	KASSERT(vp->v_type == VLNK);
   1250 
   1251 	node = VP_TO_TMPFS_NODE(vp);
   1252 	error = uiomove(node->tn_spec.tn_lnk.tn_link,
   1253 	    MIN(node->tn_size, uio->uio_resid), uio);
   1254 	node->tn_status |= TMPFS_NODE_ACCESSED;
   1255 
   1256 	return error;
   1257 }
   1258 
   1259 int
   1260 tmpfs_inactive(void *v)
   1261 {
   1262 	struct vop_inactive_args /* {
   1263 		struct vnode *a_vp;
   1264 		bool *a_recycle;
   1265 	} */ *ap = v;
   1266 	vnode_t *vp = ap->a_vp;
   1267 	tmpfs_node_t *node;
   1268 
   1269 	KASSERT(VOP_ISLOCKED(vp));
   1270 
   1271 	node = VP_TO_TMPFS_NODE(vp);
   1272 	*ap->a_recycle = (node->tn_links == 0);
   1273 	VOP_UNLOCK(vp);
   1274 
   1275 	return 0;
   1276 }
   1277 
   1278 int
   1279 tmpfs_reclaim(void *v)
   1280 {
   1281 	struct vop_reclaim_args /* {
   1282 		struct vnode *a_vp;
   1283 	} */ *ap = v;
   1284 	vnode_t *vp = ap->a_vp;
   1285 	tmpfs_mount_t *tmp = VFS_TO_TMPFS(vp->v_mount);
   1286 	tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp);
   1287 	bool racing;
   1288 
   1289 	/* Disassociate inode from vnode. */
   1290 	mutex_enter(&node->tn_vlock);
   1291 	node->tn_vnode = NULL;
   1292 	vp->v_data = NULL;
   1293 	/* Check if tmpfs_vnode_get() is racing with us. */
   1294 	racing = TMPFS_NODE_RECLAIMING(node);
   1295 	mutex_exit(&node->tn_vlock);
   1296 
   1297 	/*
   1298 	 * If inode is not referenced, i.e. no links, then destroy it.
   1299 	 * Note: if racing - inode is about to get a new vnode, leave it.
   1300 	 */
   1301 	if (node->tn_links == 0 && !racing) {
   1302 		tmpfs_free_node(tmp, node);
   1303 	}
   1304 	return 0;
   1305 }
   1306 
   1307 int
   1308 tmpfs_pathconf(void *v)
   1309 {
   1310 	struct vop_pathconf_args /* {
   1311 		struct vnode	*a_vp;
   1312 		int		a_name;
   1313 		register_t	*a_retval;
   1314 	} */ *ap = v;
   1315 	const int name = ap->a_name;
   1316 	register_t *retval = ap->a_retval;
   1317 	int error = 0;
   1318 
   1319 	switch (name) {
   1320 	case _PC_LINK_MAX:
   1321 		*retval = LINK_MAX;
   1322 		break;
   1323 	case _PC_NAME_MAX:
   1324 		*retval = NAME_MAX;
   1325 		break;
   1326 	case _PC_PATH_MAX:
   1327 		*retval = PATH_MAX;
   1328 		break;
   1329 	case _PC_PIPE_BUF:
   1330 		*retval = PIPE_BUF;
   1331 		break;
   1332 	case _PC_CHOWN_RESTRICTED:
   1333 		*retval = 1;
   1334 		break;
   1335 	case _PC_NO_TRUNC:
   1336 		*retval = 1;
   1337 		break;
   1338 	case _PC_SYNC_IO:
   1339 		*retval = 1;
   1340 		break;
   1341 	case _PC_FILESIZEBITS:
   1342 		*retval = sizeof(off_t) * CHAR_BIT;
   1343 		break;
   1344 	default:
   1345 		error = EINVAL;
   1346 	}
   1347 	return error;
   1348 }
   1349 
   1350 int
   1351 tmpfs_advlock(void *v)
   1352 {
   1353 	struct vop_advlock_args /* {
   1354 		struct vnode	*a_vp;
   1355 		void *		a_id;
   1356 		int		a_op;
   1357 		struct flock	*a_fl;
   1358 		int		a_flags;
   1359 	} */ *ap = v;
   1360 	vnode_t *vp = ap->a_vp;
   1361 	tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp);
   1362 
   1363 	return lf_advlock(v, &node->tn_lockf, node->tn_size);
   1364 }
   1365 
   1366 int
   1367 tmpfs_getpages(void *v)
   1368 {
   1369 	struct vop_getpages_args /* {
   1370 		struct vnode *a_vp;
   1371 		voff_t a_offset;
   1372 		struct vm_page **a_m;
   1373 		int *a_count;
   1374 		int a_centeridx;
   1375 		vm_prot_t a_access_type;
   1376 		int a_advice;
   1377 		int a_flags;
   1378 	} */ * const ap = v;
   1379 	vnode_t *vp = ap->a_vp;
   1380 	const voff_t offset = ap->a_offset;
   1381 	struct vm_page **pgs = ap->a_m;
   1382 	const int centeridx = ap->a_centeridx;
   1383 	const vm_prot_t access_type = ap->a_access_type;
   1384 	const int advice = ap->a_advice;
   1385 	const int flags = ap->a_flags;
   1386 	int error, npages = *ap->a_count;
   1387 	tmpfs_node_t *node;
   1388 	struct uvm_object *uobj;
   1389 
   1390 	KASSERT(vp->v_type == VREG);
   1391 	KASSERT(mutex_owned(&vp->v_interlock));
   1392 
   1393 	node = VP_TO_TMPFS_NODE(vp);
   1394 	uobj = node->tn_spec.tn_reg.tn_aobj;
   1395 
   1396 	/*
   1397 	 * Currently, PGO_PASTEOF is not supported.
   1398 	 */
   1399 	if (vp->v_size <= offset + (centeridx << PAGE_SHIFT)) {
   1400 		if ((flags & PGO_LOCKED) == 0)
   1401 			mutex_exit(&vp->v_interlock);
   1402 		return EINVAL;
   1403 	}
   1404 
   1405 	if (vp->v_size < offset + (npages << PAGE_SHIFT)) {
   1406 		npages = (round_page(vp->v_size) - offset) >> PAGE_SHIFT;
   1407 	}
   1408 
   1409 	if ((flags & PGO_LOCKED) != 0)
   1410 		return EBUSY;
   1411 
   1412 	if ((flags & PGO_NOTIMESTAMP) == 0) {
   1413 		if ((vp->v_mount->mnt_flag & MNT_NOATIME) == 0)
   1414 			node->tn_status |= TMPFS_NODE_ACCESSED;
   1415 
   1416 		if ((access_type & VM_PROT_WRITE) != 0)
   1417 			node->tn_status |= TMPFS_NODE_MODIFIED;
   1418 	}
   1419 
   1420 	mutex_exit(&vp->v_interlock);
   1421 
   1422 	/*
   1423 	 * Invoke the pager.
   1424 	 *
   1425 	 * Clean the array of pages before.  XXX: PR/32166
   1426 	 * Note that vnode lock is shared with underlying UVM object.
   1427 	 */
   1428 	if (pgs) {
   1429 		memset(pgs, 0, sizeof(struct vm_pages *) * npages);
   1430 	}
   1431 	mutex_enter(&uobj->vmobjlock);
   1432 	error = (*uobj->pgops->pgo_get)(uobj, offset, pgs, &npages, centeridx,
   1433 	    access_type, advice, flags | PGO_ALLPAGES);
   1434 
   1435 #if defined(DEBUG)
   1436 	if (!error && pgs) {
   1437 		for (int i = 0; i < npages; i++) {
   1438 			KASSERT(pgs[i] != NULL);
   1439 		}
   1440 	}
   1441 #endif
   1442 	return error;
   1443 }
   1444 
   1445 int
   1446 tmpfs_putpages(void *v)
   1447 {
   1448 	struct vop_putpages_args /* {
   1449 		struct vnode *a_vp;
   1450 		voff_t a_offlo;
   1451 		voff_t a_offhi;
   1452 		int a_flags;
   1453 	} */ * const ap = v;
   1454 	vnode_t *vp = ap->a_vp;
   1455 	const voff_t offlo = ap->a_offlo;
   1456 	const voff_t offhi = ap->a_offhi;
   1457 	const int flags = ap->a_flags;
   1458 	tmpfs_node_t *node;
   1459 	struct uvm_object *uobj;
   1460 	int error;
   1461 
   1462 	KASSERT(mutex_owned(&vp->v_interlock));
   1463 
   1464 	node = VP_TO_TMPFS_NODE(vp);
   1465 
   1466 	if (vp->v_type != VREG) {
   1467 		mutex_exit(&vp->v_interlock);
   1468 		return 0;
   1469 	}
   1470 
   1471 	uobj = node->tn_spec.tn_reg.tn_aobj;
   1472 	mutex_exit(&vp->v_interlock);
   1473 
   1474 	mutex_enter(&uobj->vmobjlock);
   1475 	error = (*uobj->pgops->pgo_put)(uobj, offlo, offhi, flags);
   1476 
   1477 	/* XXX mtime */
   1478 
   1479 	return error;
   1480 }
   1481 
   1482 #ifdef TMPFS_WHITEOUT
   1483 int
   1484 tmpfs_whiteout(void *v)
   1485 {
   1486 	struct vop_whiteout_args /* {
   1487 		struct vnode		*a_dvp;
   1488 		struct componentname	*a_cnp;
   1489 		int			a_flags;
   1490 	} */ *ap = v;
   1491 	vnode_t *dvp = ap->a_dvp;
   1492 	struct componentname *cnp = ap->a_cnp;
   1493 	const int flags = ap->a_flags;
   1494 	tmpfs_mount_t *tmp = VFS_TO_TMPFS(dvp->v_mount);
   1495 	tmpfs_dirent_t *de;
   1496 	int error;
   1497 
   1498 	switch (flags) {
   1499 	case LOOKUP:
   1500 		break;
   1501 	case CREATE:
   1502 		error = tmpfs_alloc_dirent(tmp, cnp->cn_nameptr,
   1503 		    cnp->cn_namelen, &de);
   1504 		if (error)
   1505 			return error;
   1506 		tmpfs_dir_attach(dvp, de, TMPFS_NODE_WHITEOUT);
   1507 		break;
   1508 	case DELETE:
   1509 		cnp->cn_flags &= ~DOWHITEOUT; /* when in doubt, cargo cult */
   1510 		de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(dvp), cnp);
   1511 		if (de == NULL)
   1512 			return ENOENT;
   1513 		tmpfs_dir_detach(dvp, de);
   1514 		tmpfs_free_dirent(tmp, de);
   1515 		break;
   1516 	}
   1517 	return 0;
   1518 }
   1519 #endif
   1520 
   1521 int
   1522 tmpfs_print(void *v)
   1523 {
   1524 	struct vop_print_args /* {
   1525 		struct vnode	*a_vp;
   1526 	} */ *ap = v;
   1527 	vnode_t *vp = ap->a_vp;
   1528 	tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp);
   1529 
   1530 	printf("tag VT_TMPFS, tmpfs_node %p, flags 0x%x, links %d\n"
   1531 	    "\tmode 0%o, owner %d, group %d, size %" PRIdMAX ", status 0x%x",
   1532 	    node, node->tn_flags, node->tn_links, node->tn_mode, node->tn_uid,
   1533 	    node->tn_gid, (uintmax_t)node->tn_size, node->tn_status);
   1534 	if (vp->v_type == VFIFO) {
   1535 		VOCALL(fifo_vnodeop_p, VOFFSET(vop_print), v);
   1536 	}
   1537 	printf("\n");
   1538 	return 0;
   1539 }
   1540