Home | History | Annotate | Line # | Download | only in tmpfs
tmpfs_vnops.c revision 1.144.2.1
      1 /*	$NetBSD: tmpfs_vnops.c,v 1.144.2.1 2020/12/14 14:38:13 thorpej Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2005, 2006, 2007, 2020 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.144.2.1 2020/12/14 14:38:13 thorpej 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 #include <sys/atomic.h>
     53 
     54 #include <uvm/uvm_object.h>
     55 
     56 #include <miscfs/fifofs/fifo.h>
     57 #include <miscfs/genfs/genfs.h>
     58 #include <fs/tmpfs/tmpfs_vnops.h>
     59 #include <fs/tmpfs/tmpfs.h>
     60 
     61 /*
     62  * vnode operations vector used for files stored in a tmpfs file system.
     63  */
     64 int (**tmpfs_vnodeop_p)(void *);
     65 const struct vnodeopv_entry_desc tmpfs_vnodeop_entries[] = {
     66 	{ &vop_default_desc,		vn_default_error },
     67 	{ &vop_lookup_desc,		tmpfs_lookup },
     68 	{ &vop_create_desc,		tmpfs_create },
     69 	{ &vop_mknod_desc,		tmpfs_mknod },
     70 	{ &vop_open_desc,		tmpfs_open },
     71 	{ &vop_close_desc,		tmpfs_close },
     72 	{ &vop_access_desc,		tmpfs_access },
     73 	{ &vop_accessx_desc,		genfs_accessx },
     74 	{ &vop_getattr_desc,		tmpfs_getattr },
     75 	{ &vop_setattr_desc,		tmpfs_setattr },
     76 	{ &vop_read_desc,		tmpfs_read },
     77 	{ &vop_write_desc,		tmpfs_write },
     78 	{ &vop_fallocate_desc,		genfs_eopnotsupp },
     79 	{ &vop_fdiscard_desc,		genfs_eopnotsupp },
     80 	{ &vop_ioctl_desc,		tmpfs_ioctl },
     81 	{ &vop_fcntl_desc,		tmpfs_fcntl },
     82 	{ &vop_poll_desc,		tmpfs_poll },
     83 	{ &vop_kqfilter_desc,		tmpfs_kqfilter },
     84 	{ &vop_revoke_desc,		tmpfs_revoke },
     85 	{ &vop_mmap_desc,		tmpfs_mmap },
     86 	{ &vop_fsync_desc,		tmpfs_fsync },
     87 	{ &vop_seek_desc,		tmpfs_seek },
     88 	{ &vop_remove_desc,		tmpfs_remove },
     89 	{ &vop_link_desc,		tmpfs_link },
     90 	{ &vop_rename_desc,		tmpfs_rename },
     91 	{ &vop_mkdir_desc,		tmpfs_mkdir },
     92 	{ &vop_rmdir_desc,		tmpfs_rmdir },
     93 	{ &vop_symlink_desc,		tmpfs_symlink },
     94 	{ &vop_readdir_desc,		tmpfs_readdir },
     95 	{ &vop_readlink_desc,		tmpfs_readlink },
     96 	{ &vop_abortop_desc,		tmpfs_abortop },
     97 	{ &vop_inactive_desc,		tmpfs_inactive },
     98 	{ &vop_reclaim_desc,		tmpfs_reclaim },
     99 	{ &vop_lock_desc,		tmpfs_lock },
    100 	{ &vop_unlock_desc,		tmpfs_unlock },
    101 	{ &vop_bmap_desc,		tmpfs_bmap },
    102 	{ &vop_strategy_desc,		tmpfs_strategy },
    103 	{ &vop_print_desc,		tmpfs_print },
    104 	{ &vop_pathconf_desc,		tmpfs_pathconf },
    105 	{ &vop_islocked_desc,		tmpfs_islocked },
    106 	{ &vop_advlock_desc,		tmpfs_advlock },
    107 	{ &vop_bwrite_desc,		tmpfs_bwrite },
    108 	{ &vop_getpages_desc,		tmpfs_getpages },
    109 	{ &vop_putpages_desc,		tmpfs_putpages },
    110 	{ &vop_whiteout_desc,		tmpfs_whiteout },
    111 	{ NULL, NULL }
    112 };
    113 
    114 const struct vnodeopv_desc tmpfs_vnodeop_opv_desc = {
    115 	&tmpfs_vnodeop_p, tmpfs_vnodeop_entries
    116 };
    117 
    118 /*
    119  * tmpfs_lookup: path name traversal routine.
    120  *
    121  * Arguments: dvp (directory being searched), vpp (result),
    122  * cnp (component name - path).
    123  *
    124  * => Caller holds a reference and lock on dvp.
    125  * => We return looked-up vnode (vpp) locked, with a reference held.
    126  */
    127 int
    128 tmpfs_lookup(void *v)
    129 {
    130 	struct vop_lookup_v2_args /* {
    131 		struct vnode *a_dvp;
    132 		struct vnode **a_vpp;
    133 		struct componentname *a_cnp;
    134 	} */ *ap = v;
    135 	vnode_t *dvp = ap->a_dvp, **vpp = ap->a_vpp;
    136 	struct componentname *cnp = ap->a_cnp;
    137 	const bool lastcn = (cnp->cn_flags & ISLASTCN) != 0;
    138 	tmpfs_node_t *dnode, *tnode;
    139 	tmpfs_dirent_t *de;
    140 	int cachefound, iswhiteout;
    141 	int error;
    142 
    143 	KASSERT(VOP_ISLOCKED(dvp));
    144 
    145 	dnode = VP_TO_TMPFS_DIR(dvp);
    146 	*vpp = NULL;
    147 
    148 	/* Check accessibility of directory. */
    149 	error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred);
    150 	if (error) {
    151 		goto out;
    152 	}
    153 
    154 	/*
    155 	 * If requesting the last path component on a read-only file system
    156 	 * with a write operation, deny it.
    157 	 */
    158 	if (lastcn && (dvp->v_mount->mnt_flag & MNT_RDONLY) != 0 &&
    159 	    (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
    160 		error = EROFS;
    161 		goto out;
    162 	}
    163 
    164 	/*
    165 	 * Avoid doing a linear scan of the directory if the requested
    166 	 * directory/name couple is already in the cache.
    167 	 */
    168 	cachefound = cache_lookup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
    169 				  cnp->cn_nameiop, cnp->cn_flags,
    170 				  &iswhiteout, vpp);
    171 	if (iswhiteout) {
    172 		cnp->cn_flags |= ISWHITEOUT;
    173 	}
    174 	if (cachefound && *vpp == NULLVP) {
    175 		/* Negative cache hit. */
    176 		error = ENOENT;
    177 		goto out;
    178 	} else if (cachefound) {
    179 		error = 0;
    180 		goto out;
    181 	}
    182 
    183 	/*
    184 	 * Treat an unlinked directory as empty (no "." or "..")
    185 	 */
    186 	if (dnode->tn_links == 0) {
    187 		KASSERT(dnode->tn_size == 0);
    188 		error = ENOENT;
    189 		goto out;
    190 	}
    191 
    192 	if (cnp->cn_flags & ISDOTDOT) {
    193 		tmpfs_node_t *pnode;
    194 
    195 		/*
    196 		 * Lookup of ".." case.
    197 		 */
    198 		if (lastcn && cnp->cn_nameiop == RENAME) {
    199 			error = EINVAL;
    200 			goto out;
    201 		}
    202 		KASSERT(dnode->tn_type == VDIR);
    203 		pnode = dnode->tn_spec.tn_dir.tn_parent;
    204 		if (pnode == NULL) {
    205 			error = ENOENT;
    206 			goto done;
    207 		}
    208 
    209 		error = vcache_get(dvp->v_mount, &pnode, sizeof(pnode), vpp);
    210 		goto done;
    211 	} else if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') {
    212 		/*
    213 		 * Lookup of "." case.
    214 		 */
    215 		if (lastcn && cnp->cn_nameiop == RENAME) {
    216 			error = EISDIR;
    217 			goto out;
    218 		}
    219 		vref(dvp);
    220 		*vpp = dvp;
    221 		error = 0;
    222 		goto done;
    223 	}
    224 
    225 	/*
    226 	 * Other lookup cases: perform directory scan.
    227 	 */
    228 	de = tmpfs_dir_lookup(dnode, cnp);
    229 	if (de == NULL || de->td_node == TMPFS_NODE_WHITEOUT) {
    230 		/*
    231 		 * The entry was not found in the directory.  This is valid
    232 		 * if we are creating or renaming an entry and are working
    233 		 * on the last component of the path name.
    234 		 */
    235 		if (lastcn && (cnp->cn_nameiop == CREATE ||
    236 		    cnp->cn_nameiop == RENAME)) {
    237 			error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred);
    238 			if (error) {
    239 				goto out;
    240 			}
    241 			error = EJUSTRETURN;
    242 		} else {
    243 			error = ENOENT;
    244 		}
    245 		if (de) {
    246 			KASSERT(de->td_node == TMPFS_NODE_WHITEOUT);
    247 			cnp->cn_flags |= ISWHITEOUT;
    248 		}
    249 		goto done;
    250 	}
    251 
    252 	tnode = de->td_node;
    253 
    254 	/*
    255 	 * If it is not the last path component and found a non-directory
    256 	 * or non-link entry (which may itself be pointing to a directory),
    257 	 * raise an error.
    258 	 */
    259 	if (!lastcn && tnode->tn_type != VDIR && tnode->tn_type != VLNK) {
    260 		error = ENOTDIR;
    261 		goto out;
    262 	}
    263 
    264 	/* Check the permissions. */
    265 	if (lastcn && (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
    266 		error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred);
    267 		if (error)
    268 			goto out;
    269 
    270 		if ((dnode->tn_mode & S_ISTXT) != 0) {
    271 			error = kauth_authorize_vnode(cnp->cn_cred,
    272 			    KAUTH_VNODE_DELETE, tnode->tn_vnode,
    273 			    dnode->tn_vnode, genfs_can_sticky(dvp, cnp->cn_cred,
    274 			    dnode->tn_uid, tnode->tn_uid));
    275 			if (error) {
    276 				error = EPERM;
    277 				goto out;
    278 			}
    279 		}
    280 	}
    281 
    282 	/* Get a vnode for the matching entry. */
    283 	error = vcache_get(dvp->v_mount, &tnode, sizeof(tnode), vpp);
    284 done:
    285 	/*
    286 	 * Cache the result, unless request was for creation (as it does
    287 	 * not improve the performance).
    288 	 */
    289 	if (cnp->cn_nameiop != CREATE) {
    290 		cache_enter(dvp, *vpp, cnp->cn_nameptr, cnp->cn_namelen,
    291 			    cnp->cn_flags);
    292 	}
    293 out:
    294 	KASSERT(VOP_ISLOCKED(dvp));
    295 
    296 	return error;
    297 }
    298 
    299 int
    300 tmpfs_create(void *v)
    301 {
    302 	struct vop_create_v3_args /* {
    303 		struct vnode		*a_dvp;
    304 		struct vnode		**a_vpp;
    305 		struct componentname	*a_cnp;
    306 		struct vattr		*a_vap;
    307 	} */ *ap = v;
    308 	vnode_t *dvp = ap->a_dvp, **vpp = ap->a_vpp;
    309 	struct componentname *cnp = ap->a_cnp;
    310 	struct vattr *vap = ap->a_vap;
    311 
    312 	KASSERT(VOP_ISLOCKED(dvp));
    313 	KASSERT(vap->va_type == VREG || vap->va_type == VSOCK);
    314 	return tmpfs_construct_node(dvp, vpp, vap, cnp, NULL);
    315 }
    316 
    317 int
    318 tmpfs_mknod(void *v)
    319 {
    320 	struct vop_mknod_v3_args /* {
    321 		struct vnode		*a_dvp;
    322 		struct vnode		**a_vpp;
    323 		struct componentname	*a_cnp;
    324 		struct vattr		*a_vap;
    325 	} */ *ap = v;
    326 	vnode_t *dvp = ap->a_dvp, **vpp = ap->a_vpp;
    327 	struct componentname *cnp = ap->a_cnp;
    328 	struct vattr *vap = ap->a_vap;
    329 	enum vtype vt = vap->va_type;
    330 
    331 	if (vt != VBLK && vt != VCHR && vt != VFIFO) {
    332 		*vpp = NULL;
    333 		return EINVAL;
    334 	}
    335 	return tmpfs_construct_node(dvp, vpp, vap, cnp, NULL);
    336 }
    337 
    338 int
    339 tmpfs_open(void *v)
    340 {
    341 	struct vop_open_args /* {
    342 		struct vnode	*a_vp;
    343 		int		a_mode;
    344 		kauth_cred_t	a_cred;
    345 	} */ *ap = v;
    346 	vnode_t *vp = ap->a_vp;
    347 	mode_t mode = ap->a_mode;
    348 	tmpfs_node_t *node;
    349 
    350 	KASSERT(VOP_ISLOCKED(vp));
    351 
    352 	node = VP_TO_TMPFS_NODE(vp);
    353 
    354 	/* If the file is marked append-only, deny write requests. */
    355 	if ((node->tn_flags & APPEND) != 0 &&
    356 	    (mode & (FWRITE | O_APPEND)) == FWRITE) {
    357 		return EPERM;
    358 	}
    359 	return 0;
    360 }
    361 
    362 int
    363 tmpfs_close(void *v)
    364 {
    365 	struct vop_close_args /* {
    366 		struct vnode	*a_vp;
    367 		int		a_fflag;
    368 		kauth_cred_t	a_cred;
    369 	} */ *ap = v;
    370 	vnode_t *vp __diagused = ap->a_vp;
    371 
    372 	KASSERT(VOP_ISLOCKED(vp));
    373 	return 0;
    374 }
    375 
    376 int
    377 tmpfs_access(void *v)
    378 {
    379 	struct vop_access_args /* {
    380 		struct vnode	*a_vp;
    381 		accmode_t	a_accmode;
    382 		kauth_cred_t	a_cred;
    383 	} */ *ap = v;
    384 	vnode_t *vp = ap->a_vp;
    385 	accmode_t accmode = ap->a_accmode;
    386 	kauth_cred_t cred = ap->a_cred;
    387 	tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp);
    388 	const bool writing = (accmode & VWRITE) != 0;
    389 
    390 	KASSERT(VOP_ISLOCKED(vp));
    391 
    392 	/* Possible? */
    393 	switch (vp->v_type) {
    394 	case VDIR:
    395 	case VLNK:
    396 	case VREG:
    397 		if (writing && (vp->v_mount->mnt_flag & MNT_RDONLY) != 0) {
    398 			return EROFS;
    399 		}
    400 		break;
    401 	case VBLK:
    402 	case VCHR:
    403 	case VSOCK:
    404 	case VFIFO:
    405 		break;
    406 	default:
    407 		return EINVAL;
    408 	}
    409 	if (writing && (node->tn_flags & IMMUTABLE) != 0) {
    410 		return EPERM;
    411 	}
    412 
    413 	return kauth_authorize_vnode(cred, KAUTH_ACCESS_ACTION(accmode,
    414 	    vp->v_type, node->tn_mode), vp, NULL, genfs_can_access(vp, cred,
    415 	    node->tn_uid, node->tn_gid, node->tn_mode, NULL, accmode));
    416 }
    417 
    418 int
    419 tmpfs_getattr(void *v)
    420 {
    421 	struct vop_getattr_args /* {
    422 		struct vnode	*a_vp;
    423 		struct vattr	*a_vap;
    424 		kauth_cred_t	a_cred;
    425 	} */ *ap = v;
    426 	vnode_t *vp = ap->a_vp;
    427 	struct vattr *vap = ap->a_vap;
    428 	tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp);
    429 
    430 	vattr_null(vap);
    431 
    432 	vap->va_type = vp->v_type;
    433 	vap->va_mode = node->tn_mode;
    434 	vap->va_nlink = node->tn_links;
    435 	vap->va_uid = node->tn_uid;
    436 	vap->va_gid = node->tn_gid;
    437 	vap->va_fsid = vp->v_mount->mnt_stat.f_fsidx.__fsid_val[0];
    438 	vap->va_fileid = node->tn_id;
    439 	vap->va_size = node->tn_size;
    440 	vap->va_blocksize = PAGE_SIZE;
    441 	vap->va_gen = TMPFS_NODE_GEN(node);
    442 	vap->va_flags = node->tn_flags;
    443 	vap->va_rdev = (vp->v_type == VBLK || vp->v_type == VCHR) ?
    444 	    node->tn_spec.tn_dev.tn_rdev : VNOVAL;
    445 	vap->va_bytes = round_page(node->tn_size);
    446 	vap->va_filerev = VNOVAL;
    447 	vap->va_vaflags = 0;
    448 	vap->va_spare = VNOVAL; /* XXX */
    449 
    450 	mutex_enter(&node->tn_timelock);
    451 	tmpfs_update_locked(vp, 0);
    452 	vap->va_atime = node->tn_atime;
    453 	vap->va_mtime = node->tn_mtime;
    454 	vap->va_ctime = node->tn_ctime;
    455 	vap->va_birthtime = node->tn_birthtime;
    456 	mutex_exit(&node->tn_timelock);
    457 
    458 	return 0;
    459 }
    460 
    461 int
    462 tmpfs_setattr(void *v)
    463 {
    464 	struct vop_setattr_args /* {
    465 		struct vnode	*a_vp;
    466 		struct vattr	*a_vap;
    467 		kauth_cred_t	a_cred;
    468 	} */ *ap = v;
    469 	vnode_t *vp = ap->a_vp;
    470 	struct vattr *vap = ap->a_vap;
    471 	kauth_cred_t cred = ap->a_cred;
    472 	lwp_t *l = curlwp;
    473 	int error = 0;
    474 
    475 	KASSERT(VOP_ISLOCKED(vp));
    476 
    477 	/* Abort if any unsettable attribute is given. */
    478 	if (vap->va_type != VNON || vap->va_nlink != VNOVAL ||
    479 	    vap->va_fsid != VNOVAL || vap->va_fileid != VNOVAL ||
    480 	    vap->va_blocksize != VNOVAL || vap->va_ctime.tv_sec != VNOVAL ||
    481 	    vap->va_gen != VNOVAL || vap->va_rdev != VNOVAL ||
    482 	    vap->va_bytes != VNOVAL) {
    483 		return EINVAL;
    484 	}
    485 
    486 	if (error == 0 && vap->va_flags != VNOVAL)
    487 		error = tmpfs_chflags(vp, vap->va_flags, cred, l);
    488 
    489 	if (error == 0 && vap->va_size != VNOVAL)
    490 		error = tmpfs_chsize(vp, vap->va_size, cred, l);
    491 
    492 	if (error == 0 && (vap->va_uid != VNOVAL || vap->va_gid != VNOVAL))
    493 		error = tmpfs_chown(vp, vap->va_uid, vap->va_gid, cred, l);
    494 
    495 	if (error == 0 && vap->va_mode != VNOVAL)
    496 		error = tmpfs_chmod(vp, vap->va_mode, cred, l);
    497 
    498 	const bool chsometime =
    499 	    vap->va_atime.tv_sec != VNOVAL ||
    500 	    vap->va_mtime.tv_sec != VNOVAL ||
    501 	    vap->va_birthtime.tv_sec != VNOVAL;
    502 	if (error == 0 && chsometime) {
    503 		error = tmpfs_chtimes(vp, &vap->va_atime, &vap->va_mtime,
    504 		    &vap->va_birthtime, vap->va_vaflags, cred, l);
    505 	}
    506 	return error;
    507 }
    508 
    509 int
    510 tmpfs_read(void *v)
    511 {
    512 	struct vop_read_args /* {
    513 		struct vnode *a_vp;
    514 		struct uio *a_uio;
    515 		int a_ioflag;
    516 		kauth_cred_t a_cred;
    517 	} */ *ap = v;
    518 	vnode_t *vp = ap->a_vp;
    519 	struct uio *uio = ap->a_uio;
    520 	const int ioflag = ap->a_ioflag;
    521 	tmpfs_node_t *node;
    522 	struct uvm_object *uobj;
    523 	int error;
    524 
    525 	KASSERT(VOP_ISLOCKED(vp));
    526 
    527 	if (vp->v_type == VDIR) {
    528 		return EISDIR;
    529 	}
    530 	if (uio->uio_offset < 0 || vp->v_type != VREG) {
    531 		return EINVAL;
    532 	}
    533 
    534 	/* Note: reading zero bytes should not update atime. */
    535 	if (uio->uio_resid == 0) {
    536 		return 0;
    537 	}
    538 
    539 	node = VP_TO_TMPFS_NODE(vp);
    540 	uobj = node->tn_spec.tn_reg.tn_aobj;
    541 	error = 0;
    542 
    543 	while (error == 0 && uio->uio_resid > 0) {
    544 		vsize_t len;
    545 
    546 		if (node->tn_size <= uio->uio_offset) {
    547 			break;
    548 		}
    549 		len = MIN(node->tn_size - uio->uio_offset, uio->uio_resid);
    550 		if (len == 0) {
    551 			break;
    552 		}
    553 		error = ubc_uiomove(uobj, uio, len, IO_ADV_DECODE(ioflag),
    554 		    UBC_READ | UBC_PARTIALOK | UBC_VNODE_FLAGS(vp));
    555 	}
    556 
    557 	tmpfs_update(vp, TMPFS_UPDATE_ATIME);
    558 	return error;
    559 }
    560 
    561 int
    562 tmpfs_write(void *v)
    563 {
    564 	struct vop_write_args /* {
    565 		struct vnode	*a_vp;
    566 		struct uio	*a_uio;
    567 		int		a_ioflag;
    568 		kauth_cred_t	a_cred;
    569 	} */ *ap = v;
    570 	vnode_t *vp = ap->a_vp;
    571 	struct uio *uio = ap->a_uio;
    572 	const int ioflag = ap->a_ioflag;
    573 	tmpfs_node_t *node;
    574 	struct uvm_object *uobj;
    575 	off_t oldsize;
    576 	int error, ubc_flags;
    577 
    578 	KASSERT(VOP_ISLOCKED(vp));
    579 
    580 	node = VP_TO_TMPFS_NODE(vp);
    581 	oldsize = node->tn_size;
    582 
    583 	if ((vp->v_mount->mnt_flag & MNT_RDONLY) != 0) {
    584 		error = EROFS;
    585 		goto out;
    586 	}
    587 
    588 	if (uio->uio_offset < 0 || vp->v_type != VREG) {
    589 		error = EINVAL;
    590 		goto out;
    591 	}
    592 	if (uio->uio_resid == 0) {
    593 		error = 0;
    594 		goto out;
    595 	}
    596 	if (ioflag & IO_APPEND) {
    597 		uio->uio_offset = node->tn_size;
    598 	}
    599 
    600 	if (uio->uio_offset + uio->uio_resid > node->tn_size) {
    601 		error = tmpfs_reg_resize(vp, uio->uio_offset + uio->uio_resid);
    602 		if (error)
    603 			goto out;
    604 	}
    605 
    606 	/*
    607 	 * If we're extending the file and have data to write that would
    608 	 * not leave an un-zeroed hole, we can avoid fault processing and
    609 	 * zeroing of pages on allocation.
    610 	 *
    611 	 * Don't do this if the file is mapped and we need to touch an
    612 	 * existing page, because writing a mapping of the file into itself
    613 	 * could cause a deadlock on PG_BUSY.
    614 	 *
    615 	 * New pages will not become visible until finished here (because
    616 	 * of PG_BUSY and the vnode lock).
    617 	 */
    618 	ubc_flags = UBC_WRITE | UBC_VNODE_FLAGS(vp);
    619 #if 0
    620 	/*
    621 	 * XXX disable use of UBC_FAULTBUSY for now, this check is insufficient
    622 	 * because it does not zero uninitialized parts of pages in all of
    623 	 * the cases where zeroing is needed.
    624 	 */
    625 	if (uio->uio_offset >= oldsize &&
    626 	    ((uio->uio_offset & (PAGE_SIZE - 1)) == 0 ||
    627 	    ((vp->v_vflag & VV_MAPPED) == 0 &&
    628 	    trunc_page(uio->uio_offset) == trunc_page(oldsize)))) {
    629 		ubc_flags |= UBC_FAULTBUSY;
    630 	}
    631 #endif
    632 
    633 	uobj = node->tn_spec.tn_reg.tn_aobj;
    634 	error = 0;
    635 	while (error == 0 && uio->uio_resid > 0) {
    636 		vsize_t len;
    637 
    638 		len = MIN(node->tn_size - uio->uio_offset, uio->uio_resid);
    639 		if (len == 0) {
    640 			break;
    641 		}
    642 		error = ubc_uiomove(uobj, uio, len, IO_ADV_DECODE(ioflag),
    643 		    ubc_flags);
    644 	}
    645 	if (error) {
    646 		(void)tmpfs_reg_resize(vp, oldsize);
    647 	}
    648 
    649 	tmpfs_update(vp, TMPFS_UPDATE_MTIME | TMPFS_UPDATE_CTIME);
    650 	VN_KNOTE(vp, NOTE_WRITE);
    651 out:
    652 	if (error) {
    653 		KASSERT(oldsize == node->tn_size);
    654 	} else {
    655 		KASSERT(uio->uio_resid == 0);
    656 	}
    657 	return error;
    658 }
    659 
    660 int
    661 tmpfs_fsync(void *v)
    662 {
    663 	struct vop_fsync_args /* {
    664 		struct vnode *a_vp;
    665 		kauth_cred_t a_cred;
    666 		int a_flags;
    667 		off_t a_offlo;
    668 		off_t a_offhi;
    669 		struct lwp *a_l;
    670 	} */ *ap = v;
    671 	vnode_t *vp __diagused = ap->a_vp;
    672 
    673 	/* Nothing to do.  Should be up to date. */
    674 	KASSERT(VOP_ISLOCKED(vp));
    675 	return 0;
    676 }
    677 
    678 /*
    679  * tmpfs_remove: unlink a file.
    680  *
    681  * => Both directory (dvp) and file (vp) are locked.
    682  * => We unlock and drop the reference on both.
    683  */
    684 int
    685 tmpfs_remove(void *v)
    686 {
    687 	struct vop_remove_v2_args /* {
    688 		struct vnode *a_dvp;
    689 		struct vnode *a_vp;
    690 		struct componentname *a_cnp;
    691 	} */ *ap = v;
    692 	vnode_t *dvp = ap->a_dvp, *vp = ap->a_vp;
    693 	tmpfs_node_t *dnode, *node;
    694 	tmpfs_dirent_t *de;
    695 	int error, tflags;
    696 
    697 	KASSERT(VOP_ISLOCKED(dvp));
    698 	KASSERT(VOP_ISLOCKED(vp));
    699 
    700 	if (vp->v_type == VDIR) {
    701 		error = EPERM;
    702 		goto out;
    703 	}
    704 	dnode = VP_TO_TMPFS_DIR(dvp);
    705 	node = VP_TO_TMPFS_NODE(vp);
    706 
    707 	/*
    708 	 * Files marked as immutable or append-only cannot be deleted.
    709 	 * Likewise, files residing on directories marked as append-only
    710 	 * cannot be deleted.
    711 	 */
    712 	if (node->tn_flags & (IMMUTABLE | APPEND)) {
    713 		error = EPERM;
    714 		goto out;
    715 	}
    716 	if (dnode->tn_flags & APPEND) {
    717 		error = EPERM;
    718 		goto out;
    719 	}
    720 
    721 	/* Lookup the directory entry (check the cached hint first). */
    722 	de = tmpfs_dir_cached(node);
    723 	if (de == NULL) {
    724 		struct componentname *cnp = ap->a_cnp;
    725 		de = tmpfs_dir_lookup(dnode, cnp);
    726 	}
    727 	KASSERT(de && de->td_node == node);
    728 
    729 	/*
    730 	 * Remove the entry from the directory (drops the link count) and
    731 	 * destroy it or replace with a whiteout.
    732 	 *
    733 	 * Note: the inode referred by it will not be destroyed until the
    734 	 * vnode is reclaimed/recycled.
    735 	 */
    736 
    737 	tmpfs_dir_detach(dnode, de);
    738 
    739 	if (ap->a_cnp->cn_flags & DOWHITEOUT)
    740 		tmpfs_dir_attach(dnode, de, TMPFS_NODE_WHITEOUT);
    741 	else
    742 		tmpfs_free_dirent(VFS_TO_TMPFS(vp->v_mount), de);
    743 
    744 	tflags = TMPFS_UPDATE_MTIME | TMPFS_UPDATE_CTIME;
    745 	if (node->tn_links > 0) {
    746 		/* We removed a hard link. */
    747 		tflags |= TMPFS_UPDATE_CTIME;
    748 	}
    749 	tmpfs_update(dvp, tflags);
    750 	error = 0;
    751 out:
    752 	/* Drop the reference and unlock the node. */
    753 	if (dvp == vp) {
    754 		vrele(vp);
    755 	} else {
    756 		vput(vp);
    757 	}
    758 	return error;
    759 }
    760 
    761 /*
    762  * tmpfs_link: create a hard link.
    763  */
    764 int
    765 tmpfs_link(void *v)
    766 {
    767 	struct vop_link_v2_args /* {
    768 		struct vnode *a_dvp;
    769 		struct vnode *a_vp;
    770 		struct componentname *a_cnp;
    771 	} */ *ap = v;
    772 	vnode_t *dvp = ap->a_dvp;
    773 	vnode_t *vp = ap->a_vp;
    774 	struct componentname *cnp = ap->a_cnp;
    775 	tmpfs_node_t *dnode, *node;
    776 	tmpfs_dirent_t *de;
    777 	int error;
    778 
    779 	KASSERT(dvp != vp);
    780 	KASSERT(VOP_ISLOCKED(dvp));
    781 	KASSERT(vp->v_type != VDIR);
    782 	KASSERT(dvp->v_mount == vp->v_mount);
    783 
    784 	dnode = VP_TO_TMPFS_DIR(dvp);
    785 	node = VP_TO_TMPFS_NODE(vp);
    786 
    787 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    788 
    789 	/* Check for maximum number of links limit. */
    790 	if (node->tn_links == LINK_MAX) {
    791 		error = EMLINK;
    792 		goto out;
    793 	}
    794 	KASSERT(node->tn_links < LINK_MAX);
    795 
    796 	/* We cannot create links of files marked immutable or append-only. */
    797 	if (node->tn_flags & (IMMUTABLE | APPEND)) {
    798 		error = EPERM;
    799 		goto out;
    800 	}
    801 
    802 	/* Allocate a new directory entry to represent the inode. */
    803 	error = tmpfs_alloc_dirent(VFS_TO_TMPFS(vp->v_mount),
    804 	    cnp->cn_nameptr, cnp->cn_namelen, &de);
    805 	if (error) {
    806 		goto out;
    807 	}
    808 
    809 	/*
    810 	 * Insert the entry into the directory.
    811 	 * It will increase the inode link count.
    812 	 */
    813 	tmpfs_dir_attach(dnode, de, node);
    814 	tmpfs_update(dvp, TMPFS_UPDATE_MTIME | TMPFS_UPDATE_CTIME);
    815 
    816 	/* Update the timestamps and trigger the event. */
    817 	if (node->tn_vnode) {
    818 		VN_KNOTE(node->tn_vnode, NOTE_LINK);
    819 	}
    820 	tmpfs_update(vp, TMPFS_UPDATE_CTIME);
    821 	error = 0;
    822 out:
    823 	VOP_UNLOCK(vp);
    824 	return error;
    825 }
    826 
    827 int
    828 tmpfs_mkdir(void *v)
    829 {
    830 	struct vop_mkdir_v3_args /* {
    831 		struct vnode		*a_dvp;
    832 		struct vnode		**a_vpp;
    833 		struct componentname	*a_cnp;
    834 		struct vattr		*a_vap;
    835 	} */ *ap = v;
    836 	vnode_t *dvp = ap->a_dvp;
    837 	vnode_t **vpp = ap->a_vpp;
    838 	struct componentname *cnp = ap->a_cnp;
    839 	struct vattr *vap = ap->a_vap;
    840 
    841 	KASSERT(vap->va_type == VDIR);
    842 	return tmpfs_construct_node(dvp, vpp, vap, cnp, NULL);
    843 }
    844 
    845 int
    846 tmpfs_rmdir(void *v)
    847 {
    848 	struct vop_rmdir_v2_args /* {
    849 		struct vnode		*a_dvp;
    850 		struct vnode		*a_vp;
    851 		struct componentname	*a_cnp;
    852 	} */ *ap = v;
    853 	vnode_t *dvp = ap->a_dvp;
    854 	vnode_t *vp = ap->a_vp;
    855 	tmpfs_mount_t *tmp = VFS_TO_TMPFS(dvp->v_mount);
    856 	tmpfs_node_t *dnode = VP_TO_TMPFS_DIR(dvp);
    857 	tmpfs_node_t *node = VP_TO_TMPFS_DIR(vp);
    858 	tmpfs_dirent_t *de;
    859 	int error = 0;
    860 
    861 	KASSERT(VOP_ISLOCKED(dvp));
    862 	KASSERT(VOP_ISLOCKED(vp));
    863 
    864 	/*
    865 	 * Directories with more than two entries ('.' and '..') cannot be
    866 	 * removed.  There may be whiteout entries, which we will destroy.
    867 	 */
    868 	if (node->tn_size > 0) {
    869 		/*
    870 		 * If never had whiteout entries, the directory is certainly
    871 		 * not empty.  Otherwise, scan for any non-whiteout entry.
    872 		 */
    873 		if ((node->tn_gen & TMPFS_WHITEOUT_BIT) == 0) {
    874 			error = ENOTEMPTY;
    875 			goto out;
    876 		}
    877 		TAILQ_FOREACH(de, &node->tn_spec.tn_dir.tn_dir, td_entries) {
    878 			if (de->td_node != TMPFS_NODE_WHITEOUT) {
    879 				error = ENOTEMPTY;
    880 				goto out;
    881 			}
    882 		}
    883 		KASSERT(error == 0);
    884 	}
    885 
    886 	KASSERT(node->tn_spec.tn_dir.tn_parent == dnode);
    887 
    888 	/* Lookup the directory entry (check the cached hint first). */
    889 	de = tmpfs_dir_cached(node);
    890 	if (de == NULL) {
    891 		struct componentname *cnp = ap->a_cnp;
    892 		de = tmpfs_dir_lookup(dnode, cnp);
    893 	}
    894 	KASSERT(de && de->td_node == node);
    895 
    896 	/* Check flags to see if we are allowed to remove the directory. */
    897 	if (dnode->tn_flags & APPEND || node->tn_flags & (IMMUTABLE | APPEND)) {
    898 		error = EPERM;
    899 		goto out;
    900 	}
    901 
    902 	/* Decrement the link count for the virtual '.' entry. */
    903 	node->tn_links--;
    904 
    905 	/* Detach the directory entry from the directory. */
    906 	tmpfs_dir_detach(dnode, de);
    907 
    908 	/* Purge the cache for parent. */
    909 	cache_purge(dvp);
    910 
    911 	/*
    912 	 * Destroy the directory entry or replace it with a whiteout.
    913 	 *
    914 	 * Note: the inode referred by it will not be destroyed until the
    915 	 * vnode is reclaimed.
    916 	 */
    917 	if (ap->a_cnp->cn_flags & DOWHITEOUT)
    918 		tmpfs_dir_attach(dnode, de, TMPFS_NODE_WHITEOUT);
    919 	else
    920 		tmpfs_free_dirent(tmp, de);
    921 
    922 	/* Destroy the whiteout entries from the node. */
    923 	while ((de = TAILQ_FIRST(&node->tn_spec.tn_dir.tn_dir)) != NULL) {
    924 		KASSERT(de->td_node == TMPFS_NODE_WHITEOUT);
    925 		tmpfs_dir_detach(node, de);
    926 		tmpfs_free_dirent(tmp, de);
    927 	}
    928 	tmpfs_update(dvp, TMPFS_UPDATE_MTIME | TMPFS_UPDATE_CTIME);
    929 
    930 	KASSERT(node->tn_size == 0);
    931 	KASSERT(node->tn_links == 0);
    932 out:
    933 	/* Release the node. */
    934 	KASSERT(dvp != vp);
    935 	vput(vp);
    936 	return error;
    937 }
    938 
    939 int
    940 tmpfs_symlink(void *v)
    941 {
    942 	struct vop_symlink_v3_args /* {
    943 		struct vnode		*a_dvp;
    944 		struct vnode		**a_vpp;
    945 		struct componentname	*a_cnp;
    946 		struct vattr		*a_vap;
    947 		char			*a_target;
    948 	} */ *ap = v;
    949 	vnode_t *dvp = ap->a_dvp;
    950 	vnode_t **vpp = ap->a_vpp;
    951 	struct componentname *cnp = ap->a_cnp;
    952 	struct vattr *vap = ap->a_vap;
    953 	char *target = ap->a_target;
    954 
    955 	KASSERT(vap->va_type == VLNK);
    956 	return tmpfs_construct_node(dvp, vpp, vap, cnp, target);
    957 }
    958 
    959 int
    960 tmpfs_readdir(void *v)
    961 {
    962 	struct vop_readdir_args /* {
    963 		struct vnode	*a_vp;
    964 		struct uio	*a_uio;
    965 		kauth_cred_t	a_cred;
    966 		int		*a_eofflag;
    967 		off_t		**a_cookies;
    968 		int		*ncookies;
    969 	} */ *ap = v;
    970 	vnode_t *vp = ap->a_vp;
    971 	struct uio *uio = ap->a_uio;
    972 	int *eofflag = ap->a_eofflag;
    973 	off_t **cookies = ap->a_cookies;
    974 	int *ncookies = ap->a_ncookies;
    975 	off_t startoff, cnt;
    976 	tmpfs_node_t *node;
    977 	int error;
    978 
    979 	KASSERT(VOP_ISLOCKED(vp));
    980 
    981 	/* This operation only makes sense on directory nodes. */
    982 	if (vp->v_type != VDIR) {
    983 		return ENOTDIR;
    984 	}
    985 	node = VP_TO_TMPFS_DIR(vp);
    986 	startoff = uio->uio_offset;
    987 	cnt = 0;
    988 
    989 	/*
    990 	 * Retrieve the directory entries, unless it is being destroyed.
    991 	 */
    992 	if (node->tn_links) {
    993 		error = tmpfs_dir_getdents(node, uio, &cnt);
    994 	} else {
    995 		error = 0;
    996 	}
    997 
    998 	if (eofflag != NULL) {
    999 		*eofflag = !error && uio->uio_offset == TMPFS_DIRSEQ_EOF;
   1000 	}
   1001 	if (error || cookies == NULL || ncookies == NULL) {
   1002 		return error;
   1003 	}
   1004 
   1005 	/* Update NFS-related variables, if any. */
   1006 	tmpfs_dirent_t *de = NULL;
   1007 	off_t i, off = startoff;
   1008 
   1009 	*cookies = malloc(cnt * sizeof(off_t), M_TEMP, M_WAITOK);
   1010 	*ncookies = cnt;
   1011 
   1012 	for (i = 0; i < cnt; i++) {
   1013 		KASSERT(off != TMPFS_DIRSEQ_EOF);
   1014 		if (off != TMPFS_DIRSEQ_DOT) {
   1015 			if (off == TMPFS_DIRSEQ_DOTDOT) {
   1016 				de = TAILQ_FIRST(&node->tn_spec.tn_dir.tn_dir);
   1017 			} else if (de != NULL) {
   1018 				de = TAILQ_NEXT(de, td_entries);
   1019 			} else {
   1020 				de = tmpfs_dir_lookupbyseq(node, off);
   1021 				KASSERT(de != NULL);
   1022 				de = TAILQ_NEXT(de, td_entries);
   1023 			}
   1024 			if (de == NULL) {
   1025 				off = TMPFS_DIRSEQ_EOF;
   1026 			} else {
   1027 				off = tmpfs_dir_getseq(node, de);
   1028 			}
   1029 		} else {
   1030 			off = TMPFS_DIRSEQ_DOTDOT;
   1031 		}
   1032 		(*cookies)[i] = off;
   1033 	}
   1034 	KASSERT(uio->uio_offset == off);
   1035 	return error;
   1036 }
   1037 
   1038 int
   1039 tmpfs_readlink(void *v)
   1040 {
   1041 	struct vop_readlink_args /* {
   1042 		struct vnode	*a_vp;
   1043 		struct uio	*a_uio;
   1044 		kauth_cred_t	a_cred;
   1045 	} */ *ap = v;
   1046 	vnode_t *vp = ap->a_vp;
   1047 	struct uio *uio = ap->a_uio;
   1048 	tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp);
   1049 	int error;
   1050 
   1051 	KASSERT(VOP_ISLOCKED(vp));
   1052 	KASSERT(uio->uio_offset == 0);
   1053 	KASSERT(vp->v_type == VLNK);
   1054 
   1055 	/* Note: readlink(2) returns the path without NUL terminator. */
   1056 	if (node->tn_size > 0) {
   1057 		error = uiomove(node->tn_spec.tn_lnk.tn_link,
   1058 		    MIN(node->tn_size, uio->uio_resid), uio);
   1059 	} else {
   1060 		error = 0;
   1061 	}
   1062 	tmpfs_update(vp, TMPFS_UPDATE_ATIME);
   1063 
   1064 	return error;
   1065 }
   1066 
   1067 int
   1068 tmpfs_inactive(void *v)
   1069 {
   1070 	struct vop_inactive_v2_args /* {
   1071 		struct vnode *a_vp;
   1072 		bool *a_recycle;
   1073 	} */ *ap = v;
   1074 	vnode_t *vp = ap->a_vp;
   1075 	tmpfs_node_t *node;
   1076 	int error = 0;
   1077 
   1078 	KASSERT(VOP_ISLOCKED(vp));
   1079 
   1080 	node = VP_TO_TMPFS_NODE(vp);
   1081 	if (node->tn_links == 0) {
   1082 		/*
   1083 		 * Mark node as dead by setting its generation to zero.
   1084 		 */
   1085 		atomic_and_32(&node->tn_gen, ~TMPFS_NODE_GEN_MASK);
   1086 
   1087 		/*
   1088 		 * If the file has been deleted, truncate it, otherwise VFS
   1089 		 * will quite rightly try to write back dirty data, which in
   1090 		 * the case of tmpfs/UAO means needless page deactivations.
   1091 		 */
   1092 		if (vp->v_type == VREG) {
   1093 			error = tmpfs_reg_resize(vp, 0);
   1094 		}
   1095 		*ap->a_recycle = true;
   1096 	} else {
   1097 		tmpfs_update(vp, 0);
   1098 		*ap->a_recycle = false;
   1099 	}
   1100 
   1101 	return error;
   1102 }
   1103 
   1104 int
   1105 tmpfs_reclaim(void *v)
   1106 {
   1107 	struct vop_reclaim_v2_args /* {
   1108 		struct vnode *a_vp;
   1109 	} */ *ap = v;
   1110 	vnode_t *vp = ap->a_vp;
   1111 	tmpfs_mount_t *tmp = VFS_TO_TMPFS(vp->v_mount);
   1112 	tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp);
   1113 
   1114 	/* Unlock vnode.  We still have exclusive access to it. */
   1115 	VOP_UNLOCK(vp);
   1116 
   1117 	/* Disassociate inode from vnode. */
   1118 	node->tn_vnode = NULL;
   1119 	vp->v_data = NULL;
   1120 
   1121 	/* If inode is not referenced, i.e. no links, then destroy it. */
   1122 	if (node->tn_links == 0)
   1123 		tmpfs_free_node(tmp, node);
   1124 	return 0;
   1125 }
   1126 
   1127 int
   1128 tmpfs_pathconf(void *v)
   1129 {
   1130 	struct vop_pathconf_args /* {
   1131 		struct vnode	*a_vp;
   1132 		int		a_name;
   1133 		register_t	*a_retval;
   1134 	} */ *ap = v;
   1135 	register_t *retval = ap->a_retval;
   1136 
   1137 	switch (ap->a_name) {
   1138 	case _PC_LINK_MAX:
   1139 		*retval = LINK_MAX;
   1140 		return 0;
   1141 	case _PC_NAME_MAX:
   1142 		*retval = TMPFS_MAXNAMLEN;
   1143 		return 0;
   1144 	case _PC_PATH_MAX:
   1145 		*retval = PATH_MAX;
   1146 		return 0;
   1147 	case _PC_PIPE_BUF:
   1148 		*retval = PIPE_BUF;
   1149 		return 0;
   1150 	case _PC_CHOWN_RESTRICTED:
   1151 		*retval = 1;
   1152 		return 0;
   1153 	case _PC_NO_TRUNC:
   1154 		*retval = 1;
   1155 		return 0;
   1156 	case _PC_SYNC_IO:
   1157 		*retval = 1;
   1158 		return 0;
   1159 	case _PC_FILESIZEBITS:
   1160 		*retval = sizeof(off_t) * CHAR_BIT;
   1161 		return 0;
   1162 	default:
   1163 		return genfs_pathconf(ap);
   1164 	}
   1165 }
   1166 
   1167 int
   1168 tmpfs_advlock(void *v)
   1169 {
   1170 	struct vop_advlock_args /* {
   1171 		struct vnode	*a_vp;
   1172 		void *		a_id;
   1173 		int		a_op;
   1174 		struct flock	*a_fl;
   1175 		int		a_flags;
   1176 	} */ *ap = v;
   1177 	vnode_t *vp = ap->a_vp;
   1178 	tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp);
   1179 
   1180 	return lf_advlock(v, &node->tn_lockf, node->tn_size);
   1181 }
   1182 
   1183 int
   1184 tmpfs_getpages(void *v)
   1185 {
   1186 	struct vop_getpages_args /* {
   1187 		struct vnode *a_vp;
   1188 		voff_t a_offset;
   1189 		struct vm_page **a_m;
   1190 		int *a_count;
   1191 		int a_centeridx;
   1192 		vm_prot_t a_access_type;
   1193 		int a_advice;
   1194 		int a_flags;
   1195 	} */ * const ap = v;
   1196 	vnode_t *vp = ap->a_vp;
   1197 	const voff_t offset = ap->a_offset;
   1198 	struct vm_page **pgs = ap->a_m;
   1199 	const int centeridx = ap->a_centeridx;
   1200 	const vm_prot_t access_type = ap->a_access_type;
   1201 	const int advice = ap->a_advice;
   1202 	const int flags = ap->a_flags;
   1203 	int error, iflag, npages = *ap->a_count;
   1204 	tmpfs_node_t *node;
   1205 	struct uvm_object *uobj;
   1206 
   1207 	KASSERT(vp->v_type == VREG);
   1208 	KASSERT(rw_lock_held(vp->v_uobj.vmobjlock));
   1209 
   1210 	/*
   1211 	 * Currently, PGO_PASTEOF is not supported.
   1212 	 */
   1213 	if (vp->v_size <= offset + (centeridx << PAGE_SHIFT)) {
   1214 		if ((flags & PGO_LOCKED) == 0)
   1215 			rw_exit(vp->v_uobj.vmobjlock);
   1216 		return EINVAL;
   1217 	}
   1218 
   1219 	if (vp->v_size < offset + (npages << PAGE_SHIFT)) {
   1220 		npages = (round_page(vp->v_size) - offset) >> PAGE_SHIFT;
   1221 	}
   1222 
   1223 	/*
   1224 	 * Check for reclaimed vnode.  v_interlock is not held here, but
   1225 	 * VI_DEADCHECK is set with vmobjlock held.
   1226 	 */
   1227 	iflag = atomic_load_relaxed(&vp->v_iflag);
   1228 	if (__predict_false((iflag & VI_DEADCHECK) != 0)) {
   1229 		mutex_enter(vp->v_interlock);
   1230 		error = vdead_check(vp, VDEAD_NOWAIT);
   1231 		mutex_exit(vp->v_interlock);
   1232 		if (error) {
   1233 			if ((flags & PGO_LOCKED) == 0)
   1234 				rw_exit(vp->v_uobj.vmobjlock);
   1235 			return error;
   1236 		}
   1237 	}
   1238 
   1239 	node = VP_TO_TMPFS_NODE(vp);
   1240 	uobj = node->tn_spec.tn_reg.tn_aobj;
   1241 
   1242 	/*
   1243 	 * Update timestamp lazily.  The update will be made real when
   1244 	 * a synchronous update is next made -- or by tmpfs_getattr,
   1245 	 * tmpfs_putpages, and tmpfs_inactive.
   1246 	 */
   1247 	if ((flags & PGO_NOTIMESTAMP) == 0) {
   1248 		u_int tflags = 0;
   1249 
   1250 		if ((vp->v_mount->mnt_flag & MNT_NOATIME) == 0)
   1251 			tflags |= TMPFS_UPDATE_ATIME;
   1252 
   1253 		if ((access_type & VM_PROT_WRITE) != 0) {
   1254 			tflags |= TMPFS_UPDATE_MTIME;
   1255 			if (vp->v_mount->mnt_flag & MNT_RELATIME)
   1256 				tflags |= TMPFS_UPDATE_ATIME;
   1257 		}
   1258 		tmpfs_update_lazily(vp, tflags);
   1259 	}
   1260 
   1261 	/* Invoke the pager.  The vnode vmobjlock is shared with the UAO. */
   1262 	KASSERT(vp->v_uobj.vmobjlock == uobj->vmobjlock);
   1263 	error = (*uobj->pgops->pgo_get)(uobj, offset, pgs, &npages, centeridx,
   1264 	    access_type, advice, flags);
   1265 #if defined(DEBUG)
   1266 	if (!error && pgs) {
   1267 		KASSERT(pgs[centeridx] != NULL);
   1268 	}
   1269 #endif
   1270 	return error;
   1271 }
   1272 
   1273 int
   1274 tmpfs_putpages(void *v)
   1275 {
   1276 	struct vop_putpages_args /* {
   1277 		struct vnode *a_vp;
   1278 		voff_t a_offlo;
   1279 		voff_t a_offhi;
   1280 		int a_flags;
   1281 	} */ * const ap = v;
   1282 	vnode_t *vp = ap->a_vp;
   1283 	const voff_t offlo = ap->a_offlo;
   1284 	const voff_t offhi = ap->a_offhi;
   1285 	const int flags = ap->a_flags;
   1286 	tmpfs_node_t *node;
   1287 	struct uvm_object *uobj;
   1288 	int error;
   1289 
   1290 	KASSERT(rw_write_held(vp->v_uobj.vmobjlock));
   1291 
   1292 	if (vp->v_type != VREG) {
   1293 		rw_exit(vp->v_uobj.vmobjlock);
   1294 		return 0;
   1295 	}
   1296 
   1297 	node = VP_TO_TMPFS_NODE(vp);
   1298 	uobj = node->tn_spec.tn_reg.tn_aobj;
   1299 
   1300 	KASSERT(vp->v_uobj.vmobjlock == uobj->vmobjlock);
   1301 	error = (*uobj->pgops->pgo_put)(uobj, offlo, offhi, flags);
   1302 
   1303 	/* XXX mtime */
   1304 
   1305 	/* Process deferred updates. */
   1306 	tmpfs_update(vp, 0);
   1307 	return error;
   1308 }
   1309 
   1310 int
   1311 tmpfs_whiteout(void *v)
   1312 {
   1313 	struct vop_whiteout_args /* {
   1314 		struct vnode		*a_dvp;
   1315 		struct componentname	*a_cnp;
   1316 		int			a_flags;
   1317 	} */ *ap = v;
   1318 	vnode_t *dvp = ap->a_dvp;
   1319 	struct componentname *cnp = ap->a_cnp;
   1320 	const int flags = ap->a_flags;
   1321 	tmpfs_mount_t *tmp = VFS_TO_TMPFS(dvp->v_mount);
   1322 	tmpfs_node_t *dnode = VP_TO_TMPFS_DIR(dvp);
   1323 	tmpfs_dirent_t *de;
   1324 	int error;
   1325 
   1326 	switch (flags) {
   1327 	case LOOKUP:
   1328 		break;
   1329 	case CREATE:
   1330 		error = tmpfs_alloc_dirent(tmp, cnp->cn_nameptr,
   1331 		    cnp->cn_namelen, &de);
   1332 		if (error)
   1333 			return error;
   1334 		tmpfs_dir_attach(dnode, de, TMPFS_NODE_WHITEOUT);
   1335 		break;
   1336 	case DELETE:
   1337 		cnp->cn_flags &= ~DOWHITEOUT; /* when in doubt, cargo cult */
   1338 		de = tmpfs_dir_lookup(dnode, cnp);
   1339 		if (de == NULL)
   1340 			return ENOENT;
   1341 		tmpfs_dir_detach(dnode, de);
   1342 		tmpfs_free_dirent(tmp, de);
   1343 		break;
   1344 	}
   1345 	tmpfs_update(dvp, TMPFS_UPDATE_MTIME | TMPFS_UPDATE_CTIME);
   1346 	return 0;
   1347 }
   1348 
   1349 int
   1350 tmpfs_print(void *v)
   1351 {
   1352 	struct vop_print_args /* {
   1353 		struct vnode	*a_vp;
   1354 	} */ *ap = v;
   1355 	vnode_t *vp = ap->a_vp;
   1356 	tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp);
   1357 
   1358 	printf("tag VT_TMPFS, tmpfs_node %p, flags 0x%x, links %d\n"
   1359 	    "\tmode 0%o, owner %d, group %d, size %" PRIdMAX,
   1360 	    node, node->tn_flags, node->tn_links, node->tn_mode, node->tn_uid,
   1361 	    node->tn_gid, (uintmax_t)node->tn_size);
   1362 	if (vp->v_type == VFIFO) {
   1363 		VOCALL(fifo_vnodeop_p, VOFFSET(vop_print), v);
   1364 	}
   1365 	printf("\n");
   1366 	return 0;
   1367 }
   1368