Home | History | Annotate | Line # | Download | only in tmpfs
tmpfs_vnops.c revision 1.139
      1 /*	$NetBSD: tmpfs_vnops.c,v 1.139 2020/05/17 19:39:15 ad 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.139 2020/05/17 19:39:15 ad 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.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;
    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 	uobj = node->tn_spec.tn_reg.tn_aobj;
    607 	error = 0;
    608 	while (error == 0 && uio->uio_resid > 0) {
    609 		vsize_t len;
    610 
    611 		len = MIN(node->tn_size - uio->uio_offset, uio->uio_resid);
    612 		if (len == 0) {
    613 			break;
    614 		}
    615 		error = ubc_uiomove(uobj, uio, len, IO_ADV_DECODE(ioflag),
    616 		    UBC_WRITE | UBC_VNODE_FLAGS(vp));
    617 	}
    618 	if (error) {
    619 		(void)tmpfs_reg_resize(vp, oldsize);
    620 	}
    621 
    622 	tmpfs_update(vp, TMPFS_UPDATE_MTIME | TMPFS_UPDATE_CTIME);
    623 	VN_KNOTE(vp, NOTE_WRITE);
    624 out:
    625 	if (error) {
    626 		KASSERT(oldsize == node->tn_size);
    627 	} else {
    628 		KASSERT(uio->uio_resid == 0);
    629 	}
    630 	return error;
    631 }
    632 
    633 int
    634 tmpfs_fsync(void *v)
    635 {
    636 	struct vop_fsync_args /* {
    637 		struct vnode *a_vp;
    638 		kauth_cred_t a_cred;
    639 		int a_flags;
    640 		off_t a_offlo;
    641 		off_t a_offhi;
    642 		struct lwp *a_l;
    643 	} */ *ap = v;
    644 	vnode_t *vp __diagused = ap->a_vp;
    645 
    646 	/* Nothing to do.  Should be up to date. */
    647 	KASSERT(VOP_ISLOCKED(vp));
    648 	return 0;
    649 }
    650 
    651 /*
    652  * tmpfs_remove: unlink a file.
    653  *
    654  * => Both directory (dvp) and file (vp) are locked.
    655  * => We unlock and drop the reference on both.
    656  */
    657 int
    658 tmpfs_remove(void *v)
    659 {
    660 	struct vop_remove_v2_args /* {
    661 		struct vnode *a_dvp;
    662 		struct vnode *a_vp;
    663 		struct componentname *a_cnp;
    664 	} */ *ap = v;
    665 	vnode_t *dvp = ap->a_dvp, *vp = ap->a_vp;
    666 	tmpfs_node_t *dnode, *node;
    667 	tmpfs_dirent_t *de;
    668 	int error, tflags;
    669 
    670 	KASSERT(VOP_ISLOCKED(dvp));
    671 	KASSERT(VOP_ISLOCKED(vp));
    672 
    673 	if (vp->v_type == VDIR) {
    674 		error = EPERM;
    675 		goto out;
    676 	}
    677 	dnode = VP_TO_TMPFS_DIR(dvp);
    678 	node = VP_TO_TMPFS_NODE(vp);
    679 
    680 	/*
    681 	 * Files marked as immutable or append-only cannot be deleted.
    682 	 * Likewise, files residing on directories marked as append-only
    683 	 * cannot be deleted.
    684 	 */
    685 	if (node->tn_flags & (IMMUTABLE | APPEND)) {
    686 		error = EPERM;
    687 		goto out;
    688 	}
    689 	if (dnode->tn_flags & APPEND) {
    690 		error = EPERM;
    691 		goto out;
    692 	}
    693 
    694 	/* Lookup the directory entry (check the cached hint first). */
    695 	de = tmpfs_dir_cached(node);
    696 	if (de == NULL) {
    697 		struct componentname *cnp = ap->a_cnp;
    698 		de = tmpfs_dir_lookup(dnode, cnp);
    699 	}
    700 	KASSERT(de && de->td_node == node);
    701 
    702 	/*
    703 	 * Remove the entry from the directory (drops the link count) and
    704 	 * destroy it or replace with a whiteout.
    705 	 *
    706 	 * Note: the inode referred by it will not be destroyed until the
    707 	 * vnode is reclaimed/recycled.
    708 	 */
    709 
    710 	tmpfs_dir_detach(dnode, de);
    711 
    712 	if (ap->a_cnp->cn_flags & DOWHITEOUT)
    713 		tmpfs_dir_attach(dnode, de, TMPFS_NODE_WHITEOUT);
    714 	else
    715 		tmpfs_free_dirent(VFS_TO_TMPFS(vp->v_mount), de);
    716 
    717 	tflags = TMPFS_UPDATE_MTIME | TMPFS_UPDATE_CTIME;
    718 	if (node->tn_links > 0) {
    719 		/* We removed a hard link. */
    720 		tflags |= TMPFS_UPDATE_CTIME;
    721 	}
    722 	tmpfs_update(dvp, tflags);
    723 	error = 0;
    724 out:
    725 	/* Drop the reference and unlock the node. */
    726 	if (dvp == vp) {
    727 		vrele(vp);
    728 	} else {
    729 		vput(vp);
    730 	}
    731 	return error;
    732 }
    733 
    734 /*
    735  * tmpfs_link: create a hard link.
    736  */
    737 int
    738 tmpfs_link(void *v)
    739 {
    740 	struct vop_link_v2_args /* {
    741 		struct vnode *a_dvp;
    742 		struct vnode *a_vp;
    743 		struct componentname *a_cnp;
    744 	} */ *ap = v;
    745 	vnode_t *dvp = ap->a_dvp;
    746 	vnode_t *vp = ap->a_vp;
    747 	struct componentname *cnp = ap->a_cnp;
    748 	tmpfs_node_t *dnode, *node;
    749 	tmpfs_dirent_t *de;
    750 	int error;
    751 
    752 	KASSERT(dvp != vp);
    753 	KASSERT(VOP_ISLOCKED(dvp));
    754 	KASSERT(vp->v_type != VDIR);
    755 	KASSERT(dvp->v_mount == vp->v_mount);
    756 
    757 	dnode = VP_TO_TMPFS_DIR(dvp);
    758 	node = VP_TO_TMPFS_NODE(vp);
    759 
    760 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    761 
    762 	/* Check for maximum number of links limit. */
    763 	if (node->tn_links == LINK_MAX) {
    764 		error = EMLINK;
    765 		goto out;
    766 	}
    767 	KASSERT(node->tn_links < LINK_MAX);
    768 
    769 	/* We cannot create links of files marked immutable or append-only. */
    770 	if (node->tn_flags & (IMMUTABLE | APPEND)) {
    771 		error = EPERM;
    772 		goto out;
    773 	}
    774 
    775 	/* Allocate a new directory entry to represent the inode. */
    776 	error = tmpfs_alloc_dirent(VFS_TO_TMPFS(vp->v_mount),
    777 	    cnp->cn_nameptr, cnp->cn_namelen, &de);
    778 	if (error) {
    779 		goto out;
    780 	}
    781 
    782 	/*
    783 	 * Insert the entry into the directory.
    784 	 * It will increase the inode link count.
    785 	 */
    786 	tmpfs_dir_attach(dnode, de, node);
    787 	tmpfs_update(dvp, TMPFS_UPDATE_MTIME | TMPFS_UPDATE_CTIME);
    788 
    789 	/* Update the timestamps and trigger the event. */
    790 	if (node->tn_vnode) {
    791 		VN_KNOTE(node->tn_vnode, NOTE_LINK);
    792 	}
    793 	tmpfs_update(vp, TMPFS_UPDATE_CTIME);
    794 	error = 0;
    795 out:
    796 	VOP_UNLOCK(vp);
    797 	return error;
    798 }
    799 
    800 int
    801 tmpfs_mkdir(void *v)
    802 {
    803 	struct vop_mkdir_v3_args /* {
    804 		struct vnode		*a_dvp;
    805 		struct vnode		**a_vpp;
    806 		struct componentname	*a_cnp;
    807 		struct vattr		*a_vap;
    808 	} */ *ap = v;
    809 	vnode_t *dvp = ap->a_dvp;
    810 	vnode_t **vpp = ap->a_vpp;
    811 	struct componentname *cnp = ap->a_cnp;
    812 	struct vattr *vap = ap->a_vap;
    813 
    814 	KASSERT(vap->va_type == VDIR);
    815 	return tmpfs_construct_node(dvp, vpp, vap, cnp, NULL);
    816 }
    817 
    818 int
    819 tmpfs_rmdir(void *v)
    820 {
    821 	struct vop_rmdir_v2_args /* {
    822 		struct vnode		*a_dvp;
    823 		struct vnode		*a_vp;
    824 		struct componentname	*a_cnp;
    825 	} */ *ap = v;
    826 	vnode_t *dvp = ap->a_dvp;
    827 	vnode_t *vp = ap->a_vp;
    828 	tmpfs_mount_t *tmp = VFS_TO_TMPFS(dvp->v_mount);
    829 	tmpfs_node_t *dnode = VP_TO_TMPFS_DIR(dvp);
    830 	tmpfs_node_t *node = VP_TO_TMPFS_DIR(vp);
    831 	tmpfs_dirent_t *de;
    832 	int error = 0;
    833 
    834 	KASSERT(VOP_ISLOCKED(dvp));
    835 	KASSERT(VOP_ISLOCKED(vp));
    836 
    837 	/*
    838 	 * Directories with more than two entries ('.' and '..') cannot be
    839 	 * removed.  There may be whiteout entries, which we will destroy.
    840 	 */
    841 	if (node->tn_size > 0) {
    842 		/*
    843 		 * If never had whiteout entries, the directory is certainly
    844 		 * not empty.  Otherwise, scan for any non-whiteout entry.
    845 		 */
    846 		if ((node->tn_gen & TMPFS_WHITEOUT_BIT) == 0) {
    847 			error = ENOTEMPTY;
    848 			goto out;
    849 		}
    850 		TAILQ_FOREACH(de, &node->tn_spec.tn_dir.tn_dir, td_entries) {
    851 			if (de->td_node != TMPFS_NODE_WHITEOUT) {
    852 				error = ENOTEMPTY;
    853 				goto out;
    854 			}
    855 		}
    856 		KASSERT(error == 0);
    857 	}
    858 
    859 	KASSERT(node->tn_spec.tn_dir.tn_parent == dnode);
    860 
    861 	/* Lookup the directory entry (check the cached hint first). */
    862 	de = tmpfs_dir_cached(node);
    863 	if (de == NULL) {
    864 		struct componentname *cnp = ap->a_cnp;
    865 		de = tmpfs_dir_lookup(dnode, cnp);
    866 	}
    867 	KASSERT(de && de->td_node == node);
    868 
    869 	/* Check flags to see if we are allowed to remove the directory. */
    870 	if (dnode->tn_flags & APPEND || node->tn_flags & (IMMUTABLE | APPEND)) {
    871 		error = EPERM;
    872 		goto out;
    873 	}
    874 
    875 	/* Decrement the link count for the virtual '.' entry. */
    876 	node->tn_links--;
    877 
    878 	/* Detach the directory entry from the directory. */
    879 	tmpfs_dir_detach(dnode, de);
    880 
    881 	/* Purge the cache for parent. */
    882 	cache_purge(dvp);
    883 
    884 	/*
    885 	 * Destroy the directory entry or replace it with a whiteout.
    886 	 *
    887 	 * Note: the inode referred by it will not be destroyed until the
    888 	 * vnode is reclaimed.
    889 	 */
    890 	if (ap->a_cnp->cn_flags & DOWHITEOUT)
    891 		tmpfs_dir_attach(dnode, de, TMPFS_NODE_WHITEOUT);
    892 	else
    893 		tmpfs_free_dirent(tmp, de);
    894 
    895 	/* Destroy the whiteout entries from the node. */
    896 	while ((de = TAILQ_FIRST(&node->tn_spec.tn_dir.tn_dir)) != NULL) {
    897 		KASSERT(de->td_node == TMPFS_NODE_WHITEOUT);
    898 		tmpfs_dir_detach(node, de);
    899 		tmpfs_free_dirent(tmp, de);
    900 	}
    901 	tmpfs_update(dvp, TMPFS_UPDATE_MTIME | TMPFS_UPDATE_CTIME);
    902 
    903 	KASSERT(node->tn_size == 0);
    904 	KASSERT(node->tn_links == 0);
    905 out:
    906 	/* Release the node. */
    907 	KASSERT(dvp != vp);
    908 	vput(vp);
    909 	return error;
    910 }
    911 
    912 int
    913 tmpfs_symlink(void *v)
    914 {
    915 	struct vop_symlink_v3_args /* {
    916 		struct vnode		*a_dvp;
    917 		struct vnode		**a_vpp;
    918 		struct componentname	*a_cnp;
    919 		struct vattr		*a_vap;
    920 		char			*a_target;
    921 	} */ *ap = v;
    922 	vnode_t *dvp = ap->a_dvp;
    923 	vnode_t **vpp = ap->a_vpp;
    924 	struct componentname *cnp = ap->a_cnp;
    925 	struct vattr *vap = ap->a_vap;
    926 	char *target = ap->a_target;
    927 
    928 	KASSERT(vap->va_type == VLNK);
    929 	return tmpfs_construct_node(dvp, vpp, vap, cnp, target);
    930 }
    931 
    932 int
    933 tmpfs_readdir(void *v)
    934 {
    935 	struct vop_readdir_args /* {
    936 		struct vnode	*a_vp;
    937 		struct uio	*a_uio;
    938 		kauth_cred_t	a_cred;
    939 		int		*a_eofflag;
    940 		off_t		**a_cookies;
    941 		int		*ncookies;
    942 	} */ *ap = v;
    943 	vnode_t *vp = ap->a_vp;
    944 	struct uio *uio = ap->a_uio;
    945 	int *eofflag = ap->a_eofflag;
    946 	off_t **cookies = ap->a_cookies;
    947 	int *ncookies = ap->a_ncookies;
    948 	off_t startoff, cnt;
    949 	tmpfs_node_t *node;
    950 	int error;
    951 
    952 	KASSERT(VOP_ISLOCKED(vp));
    953 
    954 	/* This operation only makes sense on directory nodes. */
    955 	if (vp->v_type != VDIR) {
    956 		return ENOTDIR;
    957 	}
    958 	node = VP_TO_TMPFS_DIR(vp);
    959 	startoff = uio->uio_offset;
    960 	cnt = 0;
    961 
    962 	/*
    963 	 * Retrieve the directory entries, unless it is being destroyed.
    964 	 */
    965 	if (node->tn_links) {
    966 		error = tmpfs_dir_getdents(node, uio, &cnt);
    967 	} else {
    968 		error = 0;
    969 	}
    970 
    971 	if (eofflag != NULL) {
    972 		*eofflag = !error && uio->uio_offset == TMPFS_DIRSEQ_EOF;
    973 	}
    974 	if (error || cookies == NULL || ncookies == NULL) {
    975 		return error;
    976 	}
    977 
    978 	/* Update NFS-related variables, if any. */
    979 	tmpfs_dirent_t *de = NULL;
    980 	off_t i, off = startoff;
    981 
    982 	*cookies = malloc(cnt * sizeof(off_t), M_TEMP, M_WAITOK);
    983 	*ncookies = cnt;
    984 
    985 	for (i = 0; i < cnt; i++) {
    986 		KASSERT(off != TMPFS_DIRSEQ_EOF);
    987 		if (off != TMPFS_DIRSEQ_DOT) {
    988 			if (off == TMPFS_DIRSEQ_DOTDOT) {
    989 				de = TAILQ_FIRST(&node->tn_spec.tn_dir.tn_dir);
    990 			} else if (de != NULL) {
    991 				de = TAILQ_NEXT(de, td_entries);
    992 			} else {
    993 				de = tmpfs_dir_lookupbyseq(node, off);
    994 				KASSERT(de != NULL);
    995 				de = TAILQ_NEXT(de, td_entries);
    996 			}
    997 			if (de == NULL) {
    998 				off = TMPFS_DIRSEQ_EOF;
    999 			} else {
   1000 				off = tmpfs_dir_getseq(node, de);
   1001 			}
   1002 		} else {
   1003 			off = TMPFS_DIRSEQ_DOTDOT;
   1004 		}
   1005 		(*cookies)[i] = off;
   1006 	}
   1007 	KASSERT(uio->uio_offset == off);
   1008 	return error;
   1009 }
   1010 
   1011 int
   1012 tmpfs_readlink(void *v)
   1013 {
   1014 	struct vop_readlink_args /* {
   1015 		struct vnode	*a_vp;
   1016 		struct uio	*a_uio;
   1017 		kauth_cred_t	a_cred;
   1018 	} */ *ap = v;
   1019 	vnode_t *vp = ap->a_vp;
   1020 	struct uio *uio = ap->a_uio;
   1021 	tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp);
   1022 	int error;
   1023 
   1024 	KASSERT(VOP_ISLOCKED(vp));
   1025 	KASSERT(uio->uio_offset == 0);
   1026 	KASSERT(vp->v_type == VLNK);
   1027 
   1028 	/* Note: readlink(2) returns the path without NUL terminator. */
   1029 	if (node->tn_size > 0) {
   1030 		error = uiomove(node->tn_spec.tn_lnk.tn_link,
   1031 		    MIN(node->tn_size, uio->uio_resid), uio);
   1032 	} else {
   1033 		error = 0;
   1034 	}
   1035 	tmpfs_update(vp, TMPFS_UPDATE_ATIME);
   1036 
   1037 	return error;
   1038 }
   1039 
   1040 int
   1041 tmpfs_inactive(void *v)
   1042 {
   1043 	struct vop_inactive_v2_args /* {
   1044 		struct vnode *a_vp;
   1045 		bool *a_recycle;
   1046 	} */ *ap = v;
   1047 	vnode_t *vp = ap->a_vp;
   1048 	tmpfs_node_t *node;
   1049 	int error = 0;
   1050 
   1051 	KASSERT(VOP_ISLOCKED(vp));
   1052 
   1053 	node = VP_TO_TMPFS_NODE(vp);
   1054 	if (node->tn_links == 0) {
   1055 		/*
   1056 		 * Mark node as dead by setting its generation to zero.
   1057 		 */
   1058 		atomic_and_32(&node->tn_gen, ~TMPFS_NODE_GEN_MASK);
   1059 
   1060 		/*
   1061 		 * If the file has been deleted, truncate it, otherwise VFS
   1062 		 * will quite rightly try to write back dirty data, which in
   1063 		 * the case of tmpfs/UAO means needless page deactivations.
   1064 		 */
   1065 		if (vp->v_type == VREG) {
   1066 			error = tmpfs_reg_resize(vp, 0);
   1067 		}
   1068 		*ap->a_recycle = true;
   1069 	} else {
   1070 		tmpfs_update(vp, 0);
   1071 		*ap->a_recycle = false;
   1072 	}
   1073 
   1074 	return error;
   1075 }
   1076 
   1077 int
   1078 tmpfs_reclaim(void *v)
   1079 {
   1080 	struct vop_reclaim_v2_args /* {
   1081 		struct vnode *a_vp;
   1082 	} */ *ap = v;
   1083 	vnode_t *vp = ap->a_vp;
   1084 	tmpfs_mount_t *tmp = VFS_TO_TMPFS(vp->v_mount);
   1085 	tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp);
   1086 
   1087 	/* Unlock vnode.  We still have exclusive access to it. */
   1088 	VOP_UNLOCK(vp);
   1089 
   1090 	/* Disassociate inode from vnode. */
   1091 	node->tn_vnode = NULL;
   1092 	vp->v_data = NULL;
   1093 
   1094 	/* If inode is not referenced, i.e. no links, then destroy it. */
   1095 	if (node->tn_links == 0)
   1096 		tmpfs_free_node(tmp, node);
   1097 	return 0;
   1098 }
   1099 
   1100 int
   1101 tmpfs_pathconf(void *v)
   1102 {
   1103 	struct vop_pathconf_args /* {
   1104 		struct vnode	*a_vp;
   1105 		int		a_name;
   1106 		register_t	*a_retval;
   1107 	} */ *ap = v;
   1108 	const int name = ap->a_name;
   1109 	register_t *retval = ap->a_retval;
   1110 	int error = 0;
   1111 
   1112 	switch (name) {
   1113 	case _PC_LINK_MAX:
   1114 		*retval = LINK_MAX;
   1115 		break;
   1116 	case _PC_NAME_MAX:
   1117 		*retval = TMPFS_MAXNAMLEN;
   1118 		break;
   1119 	case _PC_PATH_MAX:
   1120 		*retval = PATH_MAX;
   1121 		break;
   1122 	case _PC_PIPE_BUF:
   1123 		*retval = PIPE_BUF;
   1124 		break;
   1125 	case _PC_CHOWN_RESTRICTED:
   1126 		*retval = 1;
   1127 		break;
   1128 	case _PC_NO_TRUNC:
   1129 		*retval = 1;
   1130 		break;
   1131 	case _PC_SYNC_IO:
   1132 		*retval = 1;
   1133 		break;
   1134 	case _PC_FILESIZEBITS:
   1135 		*retval = sizeof(off_t) * CHAR_BIT;
   1136 		break;
   1137 	default:
   1138 		error = EINVAL;
   1139 	}
   1140 	return error;
   1141 }
   1142 
   1143 int
   1144 tmpfs_advlock(void *v)
   1145 {
   1146 	struct vop_advlock_args /* {
   1147 		struct vnode	*a_vp;
   1148 		void *		a_id;
   1149 		int		a_op;
   1150 		struct flock	*a_fl;
   1151 		int		a_flags;
   1152 	} */ *ap = v;
   1153 	vnode_t *vp = ap->a_vp;
   1154 	tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp);
   1155 
   1156 	return lf_advlock(v, &node->tn_lockf, node->tn_size);
   1157 }
   1158 
   1159 int
   1160 tmpfs_getpages(void *v)
   1161 {
   1162 	struct vop_getpages_args /* {
   1163 		struct vnode *a_vp;
   1164 		voff_t a_offset;
   1165 		struct vm_page **a_m;
   1166 		int *a_count;
   1167 		int a_centeridx;
   1168 		vm_prot_t a_access_type;
   1169 		int a_advice;
   1170 		int a_flags;
   1171 	} */ * const ap = v;
   1172 	vnode_t *vp = ap->a_vp;
   1173 	const voff_t offset = ap->a_offset;
   1174 	struct vm_page **pgs = ap->a_m;
   1175 	const int centeridx = ap->a_centeridx;
   1176 	const vm_prot_t access_type = ap->a_access_type;
   1177 	const int advice = ap->a_advice;
   1178 	const int flags = ap->a_flags;
   1179 	int error, iflag, npages = *ap->a_count;
   1180 	tmpfs_node_t *node;
   1181 	struct uvm_object *uobj;
   1182 
   1183 	KASSERT(vp->v_type == VREG);
   1184 	KASSERT(rw_lock_held(vp->v_uobj.vmobjlock));
   1185 
   1186 	/*
   1187 	 * Currently, PGO_PASTEOF is not supported.
   1188 	 */
   1189 	if (vp->v_size <= offset + (centeridx << PAGE_SHIFT)) {
   1190 		if ((flags & PGO_LOCKED) == 0)
   1191 			rw_exit(vp->v_uobj.vmobjlock);
   1192 		return EINVAL;
   1193 	}
   1194 
   1195 	if (vp->v_size < offset + (npages << PAGE_SHIFT)) {
   1196 		npages = (round_page(vp->v_size) - offset) >> PAGE_SHIFT;
   1197 	}
   1198 
   1199 	/*
   1200 	 * Check for reclaimed vnode.  v_interlock is not held here, but
   1201 	 * VI_DEADCHECK is set with vmobjlock held.
   1202 	 */
   1203 	iflag = atomic_load_relaxed(&vp->v_iflag);
   1204 	if (__predict_false((iflag & VI_DEADCHECK) != 0) {
   1205 		mutex_enter(vp->v_interlock);
   1206 		error = vdead_check(vp, VDEAD_NOWAIT);
   1207 		mutex_exit(vp->v_interlock);
   1208 		if (error) {
   1209 			if ((flags & PGO_LOCKED) == 0)
   1210 				rw_exit(vp->v_uobj.vmobjlock);
   1211 			return error;
   1212 		}
   1213 	}
   1214 
   1215 	node = VP_TO_TMPFS_NODE(vp);
   1216 	uobj = node->tn_spec.tn_reg.tn_aobj;
   1217 
   1218 	/*
   1219 	 * Update timestamp lazily.  The update will be made real when
   1220 	 * a synchronous update is next made -- or by tmpfs_getattr,
   1221 	 * tmpfs_putpages, and tmpfs_inactive.
   1222 	 */
   1223 	if ((flags & PGO_NOTIMESTAMP) == 0) {
   1224 		u_int tflags = 0;
   1225 
   1226 		if ((vp->v_mount->mnt_flag & MNT_NOATIME) == 0)
   1227 			tflags |= TMPFS_UPDATE_ATIME;
   1228 
   1229 		if ((access_type & VM_PROT_WRITE) != 0) {
   1230 			tflags |= TMPFS_UPDATE_MTIME;
   1231 			if (vp->v_mount->mnt_flag & MNT_RELATIME)
   1232 				tflags |= TMPFS_UPDATE_ATIME;
   1233 		}
   1234 		tmpfs_update_lazily(vp, tflags);
   1235 	}
   1236 
   1237 	/*
   1238 	 * Invoke the pager.
   1239 	 *
   1240 	 * Clean the array of pages before.  XXX: PR/32166
   1241 	 * Note that vnode lock is shared with underlying UVM object.
   1242 	 */
   1243 	if ((flags & PGO_LOCKED) == 0 && pgs) {
   1244 		memset(pgs, 0, sizeof(struct vm_pages *) * npages);
   1245 	}
   1246 	KASSERT(vp->v_uobj.vmobjlock == uobj->vmobjlock);
   1247 
   1248 	error = (*uobj->pgops->pgo_get)(uobj, offset, pgs, &npages, centeridx,
   1249 	    access_type, advice, flags);
   1250 
   1251 #if defined(DEBUG)
   1252 	if (!error && pgs) {
   1253 		KASSERT(pgs[centeridx] != NULL);
   1254 	}
   1255 #endif
   1256 	return error;
   1257 }
   1258 
   1259 int
   1260 tmpfs_putpages(void *v)
   1261 {
   1262 	struct vop_putpages_args /* {
   1263 		struct vnode *a_vp;
   1264 		voff_t a_offlo;
   1265 		voff_t a_offhi;
   1266 		int a_flags;
   1267 	} */ * const ap = v;
   1268 	vnode_t *vp = ap->a_vp;
   1269 	const voff_t offlo = ap->a_offlo;
   1270 	const voff_t offhi = ap->a_offhi;
   1271 	const int flags = ap->a_flags;
   1272 	tmpfs_node_t *node;
   1273 	struct uvm_object *uobj;
   1274 	int error;
   1275 
   1276 	KASSERT(rw_write_held(vp->v_uobj.vmobjlock));
   1277 
   1278 	if (vp->v_type != VREG) {
   1279 		rw_exit(vp->v_uobj.vmobjlock);
   1280 		return 0;
   1281 	}
   1282 
   1283 	node = VP_TO_TMPFS_NODE(vp);
   1284 	uobj = node->tn_spec.tn_reg.tn_aobj;
   1285 
   1286 	KASSERT(vp->v_uobj.vmobjlock == uobj->vmobjlock);
   1287 	error = (*uobj->pgops->pgo_put)(uobj, offlo, offhi, flags);
   1288 
   1289 	/* XXX mtime */
   1290 
   1291 	/* Process deferred updates. */
   1292 	tmpfs_update(vp, 0);
   1293 	return error;
   1294 }
   1295 
   1296 int
   1297 tmpfs_whiteout(void *v)
   1298 {
   1299 	struct vop_whiteout_args /* {
   1300 		struct vnode		*a_dvp;
   1301 		struct componentname	*a_cnp;
   1302 		int			a_flags;
   1303 	} */ *ap = v;
   1304 	vnode_t *dvp = ap->a_dvp;
   1305 	struct componentname *cnp = ap->a_cnp;
   1306 	const int flags = ap->a_flags;
   1307 	tmpfs_mount_t *tmp = VFS_TO_TMPFS(dvp->v_mount);
   1308 	tmpfs_node_t *dnode = VP_TO_TMPFS_DIR(dvp);
   1309 	tmpfs_dirent_t *de;
   1310 	int error;
   1311 
   1312 	switch (flags) {
   1313 	case LOOKUP:
   1314 		break;
   1315 	case CREATE:
   1316 		error = tmpfs_alloc_dirent(tmp, cnp->cn_nameptr,
   1317 		    cnp->cn_namelen, &de);
   1318 		if (error)
   1319 			return error;
   1320 		tmpfs_dir_attach(dnode, de, TMPFS_NODE_WHITEOUT);
   1321 		break;
   1322 	case DELETE:
   1323 		cnp->cn_flags &= ~DOWHITEOUT; /* when in doubt, cargo cult */
   1324 		de = tmpfs_dir_lookup(dnode, cnp);
   1325 		if (de == NULL)
   1326 			return ENOENT;
   1327 		tmpfs_dir_detach(dnode, de);
   1328 		tmpfs_free_dirent(tmp, de);
   1329 		break;
   1330 	}
   1331 	tmpfs_update(dvp, TMPFS_UPDATE_MTIME | TMPFS_UPDATE_CTIME);
   1332 	return 0;
   1333 }
   1334 
   1335 int
   1336 tmpfs_print(void *v)
   1337 {
   1338 	struct vop_print_args /* {
   1339 		struct vnode	*a_vp;
   1340 	} */ *ap = v;
   1341 	vnode_t *vp = ap->a_vp;
   1342 	tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp);
   1343 
   1344 	printf("tag VT_TMPFS, tmpfs_node %p, flags 0x%x, links %d\n"
   1345 	    "\tmode 0%o, owner %d, group %d, size %" PRIdMAX,
   1346 	    node, node->tn_flags, node->tn_links, node->tn_mode, node->tn_uid,
   1347 	    node->tn_gid, (uintmax_t)node->tn_size);
   1348 	if (vp->v_type == VFIFO) {
   1349 		VOCALL(fifo_vnodeop_p, VOFFSET(vop_print), v);
   1350 	}
   1351 	printf("\n");
   1352 	return 0;
   1353 }
   1354