Home | History | Annotate | Line # | Download | only in tmpfs
tmpfs_vnops.c revision 1.102
      1 /*	$NetBSD: tmpfs_vnops.c,v 1.102 2013/10/01 23:10:25 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.102 2013/10/01 23:10:25 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 	{ &vop_whiteout_desc,		tmpfs_whiteout },
    107 	{ NULL, NULL }
    108 };
    109 
    110 const struct vnodeopv_desc tmpfs_vnodeop_opv_desc = {
    111 	&tmpfs_vnodeop_p, tmpfs_vnodeop_entries
    112 };
    113 
    114 /*
    115  * tmpfs_lookup: path name traversal routine.
    116  *
    117  * Arguments: dvp (directory being searched), vpp (result),
    118  * cnp (component name - path).
    119  *
    120  * => Caller holds a reference and lock on dvp.
    121  * => We return looked-up vnode (vpp) locked, with a reference held.
    122  */
    123 int
    124 tmpfs_lookup(void *v)
    125 {
    126 	struct vop_lookup_args /* {
    127 		struct vnode *a_dvp;
    128 		struct vnode **a_vpp;
    129 		struct componentname *a_cnp;
    130 	} */ *ap = v;
    131 	vnode_t *dvp = ap->a_dvp, **vpp = ap->a_vpp;
    132 	struct componentname *cnp = ap->a_cnp;
    133 	const bool lastcn = (cnp->cn_flags & ISLASTCN) != 0;
    134 	tmpfs_node_t *dnode, *tnode;
    135 	tmpfs_dirent_t *de;
    136 	int cachefound, iswhiteout;
    137 	int error;
    138 
    139 	KASSERT(VOP_ISLOCKED(dvp));
    140 
    141 	dnode = VP_TO_TMPFS_DIR(dvp);
    142 	*vpp = NULL;
    143 
    144 	/* Check accessibility of directory. */
    145 	error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred);
    146 	if (error) {
    147 		goto out;
    148 	}
    149 
    150 	/*
    151 	 * If requesting the last path component on a read-only file system
    152 	 * with a write operation, deny it.
    153 	 */
    154 	if (lastcn && (dvp->v_mount->mnt_flag & MNT_RDONLY) != 0 &&
    155 	    (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
    156 		error = EROFS;
    157 		goto out;
    158 	}
    159 
    160 	/*
    161 	 * Avoid doing a linear scan of the directory if the requested
    162 	 * directory/name couple is already in the cache.
    163 	 */
    164 	cachefound = cache_lookup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
    165 				  cnp->cn_nameiop, cnp->cn_flags,
    166 				  &iswhiteout, vpp);
    167 	if (iswhiteout) {
    168 		cnp->cn_flags |= ISWHITEOUT;
    169 	}
    170 	if (cachefound && *vpp == NULLVP) {
    171 		/* Negative cache hit. */
    172 		error = ENOENT;
    173 		goto out;
    174 	} else if (cachefound) {
    175 		error = 0;
    176 		goto out;
    177 	}
    178 
    179 	if (cnp->cn_flags & ISDOTDOT) {
    180 		tmpfs_node_t *pnode;
    181 
    182 		/*
    183 		 * Lookup of ".." case.
    184 		 */
    185 		if (lastcn && cnp->cn_nameiop == RENAME) {
    186 			error = EINVAL;
    187 			goto out;
    188 		}
    189 		KASSERT(dnode->tn_type == VDIR);
    190 		pnode = dnode->tn_spec.tn_dir.tn_parent;
    191 		if (pnode == NULL) {
    192 			error = ENOENT;
    193 			goto out;
    194 		}
    195 
    196 		/*
    197 		 * Lock the parent tn_vlock before releasing the vnode lock,
    198 		 * and thus prevents parent from disappearing.
    199 		 */
    200 		mutex_enter(&pnode->tn_vlock);
    201 		VOP_UNLOCK(dvp);
    202 
    203 		/*
    204 		 * Get a vnode of the '..' entry and re-acquire the lock.
    205 		 * Release the tn_vlock.
    206 		 */
    207 		error = tmpfs_vnode_get(dvp->v_mount, pnode, vpp);
    208 		vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
    209 		goto out;
    210 
    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(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 	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_nameiop != CREATE) {
    291 		cache_enter(dvp, *vpp, cnp->cn_nameptr, cnp->cn_namelen,
    292 			    cnp->cn_flags);
    293 	}
    294 out:
    295 	KASSERT((*vpp && VOP_ISLOCKED(*vpp)) || error);
    296 	KASSERT(VOP_ISLOCKED(dvp));
    297 
    298 	return error;
    299 }
    300 
    301 int
    302 tmpfs_create(void *v)
    303 {
    304 	struct vop_create_args /* {
    305 		struct vnode		*a_dvp;
    306 		struct vnode		**a_vpp;
    307 		struct componentname	*a_cnp;
    308 		struct vattr		*a_vap;
    309 	} */ *ap = v;
    310 	vnode_t *dvp = ap->a_dvp, **vpp = ap->a_vpp;
    311 	struct componentname *cnp = ap->a_cnp;
    312 	struct vattr *vap = ap->a_vap;
    313 
    314 	KASSERT(VOP_ISLOCKED(dvp));
    315 	KASSERT(vap->va_type == VREG || vap->va_type == VSOCK);
    316 	return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
    317 }
    318 
    319 int
    320 tmpfs_mknod(void *v)
    321 {
    322 	struct vop_mknod_args /* {
    323 		struct vnode		*a_dvp;
    324 		struct vnode		**a_vpp;
    325 		struct componentname	*a_cnp;
    326 		struct vattr		*a_vap;
    327 	} */ *ap = v;
    328 	vnode_t *dvp = ap->a_dvp, **vpp = ap->a_vpp;
    329 	struct componentname *cnp = ap->a_cnp;
    330 	struct vattr *vap = ap->a_vap;
    331 	enum vtype vt = vap->va_type;
    332 
    333 	if (vt != VBLK && vt != VCHR && vt != VFIFO) {
    334 		vput(dvp);
    335 		return EINVAL;
    336 	}
    337 	return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
    338 }
    339 
    340 int
    341 tmpfs_open(void *v)
    342 {
    343 	struct vop_open_args /* {
    344 		struct vnode	*a_vp;
    345 		int		a_mode;
    346 		kauth_cred_t	a_cred;
    347 	} */ *ap = v;
    348 	vnode_t *vp = ap->a_vp;
    349 	mode_t mode = ap->a_mode;
    350 	tmpfs_node_t *node;
    351 
    352 	KASSERT(VOP_ISLOCKED(vp));
    353 
    354 	node = VP_TO_TMPFS_NODE(vp);
    355 	if (node->tn_links < 1) {
    356 		/*
    357 		 * The file is still active, but all its names have been
    358 		 * removed (e.g. by a "rmdir $(pwd)").  It cannot be opened
    359 		 * any more, as it is about to be destroyed.
    360 		 */
    361 		return ENOENT;
    362 	}
    363 
    364 	/* If the file is marked append-only, deny write requests. */
    365 	if ((node->tn_flags & APPEND) != 0 &&
    366 	    (mode & (FWRITE | O_APPEND)) == FWRITE) {
    367 		return EPERM;
    368 	}
    369 	return 0;
    370 }
    371 
    372 int
    373 tmpfs_close(void *v)
    374 {
    375 	struct vop_close_args /* {
    376 		struct vnode	*a_vp;
    377 		int		a_fflag;
    378 		kauth_cred_t	a_cred;
    379 	} */ *ap = v;
    380 	vnode_t *vp = ap->a_vp;
    381 
    382 	KASSERT(VOP_ISLOCKED(vp));
    383 
    384 	tmpfs_update(vp, NULL, NULL, NULL, UPDATE_CLOSE);
    385 	return 0;
    386 }
    387 
    388 int
    389 tmpfs_access(void *v)
    390 {
    391 	struct vop_access_args /* {
    392 		struct vnode	*a_vp;
    393 		int		a_mode;
    394 		kauth_cred_t	a_cred;
    395 	} */ *ap = v;
    396 	vnode_t *vp = ap->a_vp;
    397 	mode_t mode = ap->a_mode;
    398 	kauth_cred_t cred = ap->a_cred;
    399 	tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp);
    400 	const bool writing = (mode & VWRITE) != 0;
    401 
    402 	KASSERT(VOP_ISLOCKED(vp));
    403 
    404 	/* Possible? */
    405 	switch (vp->v_type) {
    406 	case VDIR:
    407 	case VLNK:
    408 	case VREG:
    409 		if (writing && (vp->v_mount->mnt_flag & MNT_RDONLY) != 0) {
    410 			return EROFS;
    411 		}
    412 		break;
    413 	case VBLK:
    414 	case VCHR:
    415 	case VSOCK:
    416 	case VFIFO:
    417 		break;
    418 	default:
    419 		return EINVAL;
    420 	}
    421 	if (writing && (node->tn_flags & IMMUTABLE) != 0) {
    422 		return EPERM;
    423 	}
    424 
    425 	return kauth_authorize_vnode(cred, KAUTH_ACCESS_ACTION(mode,
    426 	    vp->v_type, node->tn_mode), vp, NULL, genfs_can_access(vp->v_type,
    427 	    node->tn_mode, node->tn_uid, node->tn_gid, mode, cred));
    428 }
    429 
    430 int
    431 tmpfs_getattr(void *v)
    432 {
    433 	struct vop_getattr_args /* {
    434 		struct vnode	*a_vp;
    435 		struct vattr	*a_vap;
    436 		kauth_cred_t	a_cred;
    437 	} */ *ap = v;
    438 	vnode_t *vp = ap->a_vp;
    439 	struct vattr *vap = ap->a_vap;
    440 	tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp);
    441 
    442 	vattr_null(vap);
    443 
    444 	tmpfs_update(vp, NULL, NULL, NULL, 0);
    445 
    446 	vap->va_type = vp->v_type;
    447 	vap->va_mode = node->tn_mode;
    448 	vap->va_nlink = node->tn_links;
    449 	vap->va_uid = node->tn_uid;
    450 	vap->va_gid = node->tn_gid;
    451 	vap->va_fsid = vp->v_mount->mnt_stat.f_fsidx.__fsid_val[0];
    452 	vap->va_fileid = node->tn_id;
    453 	vap->va_size = node->tn_size;
    454 	vap->va_blocksize = PAGE_SIZE;
    455 	vap->va_atime = node->tn_atime;
    456 	vap->va_mtime = node->tn_mtime;
    457 	vap->va_ctime = node->tn_ctime;
    458 	vap->va_birthtime = node->tn_birthtime;
    459 	vap->va_gen = TMPFS_NODE_GEN(node);
    460 	vap->va_flags = node->tn_flags;
    461 	vap->va_rdev = (vp->v_type == VBLK || vp->v_type == VCHR) ?
    462 	    node->tn_spec.tn_dev.tn_rdev : VNOVAL;
    463 	vap->va_bytes = round_page(node->tn_size);
    464 	vap->va_filerev = VNOVAL;
    465 	vap->va_vaflags = 0;
    466 	vap->va_spare = VNOVAL; /* XXX */
    467 
    468 	return 0;
    469 }
    470 
    471 #define GOODTIME(tv)	((tv)->tv_sec != VNOVAL || (tv)->tv_nsec != VNOVAL)
    472 /* XXX Should this operation be atomic?  I think it should, but code in
    473  * XXX other places (e.g., ufs) doesn't seem to be... */
    474 int
    475 tmpfs_setattr(void *v)
    476 {
    477 	struct vop_setattr_args /* {
    478 		struct vnode	*a_vp;
    479 		struct vattr	*a_vap;
    480 		kauth_cred_t	a_cred;
    481 	} */ *ap = v;
    482 	vnode_t *vp = ap->a_vp;
    483 	struct vattr *vap = ap->a_vap;
    484 	kauth_cred_t cred = ap->a_cred;
    485 	lwp_t *l = curlwp;
    486 	int error = 0;
    487 
    488 	KASSERT(VOP_ISLOCKED(vp));
    489 
    490 	/* Abort if any unsettable attribute is given. */
    491 	if (vap->va_type != VNON || vap->va_nlink != VNOVAL ||
    492 	    vap->va_fsid != VNOVAL || vap->va_fileid != VNOVAL ||
    493 	    vap->va_blocksize != VNOVAL || GOODTIME(&vap->va_ctime) ||
    494 	    vap->va_gen != VNOVAL || vap->va_rdev != VNOVAL ||
    495 	    vap->va_bytes != VNOVAL) {
    496 		return EINVAL;
    497 	}
    498 	if (error == 0 && (vap->va_flags != VNOVAL))
    499 		error = tmpfs_chflags(vp, vap->va_flags, cred, l);
    500 
    501 	if (error == 0 && (vap->va_size != VNOVAL))
    502 		error = tmpfs_chsize(vp, vap->va_size, cred, l);
    503 
    504 	if (error == 0 && (vap->va_uid != VNOVAL || vap->va_gid != VNOVAL))
    505 		error = tmpfs_chown(vp, vap->va_uid, vap->va_gid, cred, l);
    506 
    507 	if (error == 0 && (vap->va_mode != VNOVAL))
    508 		error = tmpfs_chmod(vp, vap->va_mode, cred, l);
    509 
    510 	if (error == 0 && (GOODTIME(&vap->va_atime) || GOODTIME(&vap->va_mtime)
    511 	    || GOODTIME(&vap->va_birthtime))) {
    512 		error = tmpfs_chtimes(vp, &vap->va_atime, &vap->va_mtime,
    513 		    &vap->va_birthtime, vap->va_vaflags, cred, l);
    514 		if (error == 0)
    515 			return 0;
    516 	}
    517 	tmpfs_update(vp, NULL, NULL, NULL, 0);
    518 	return error;
    519 }
    520 
    521 int
    522 tmpfs_read(void *v)
    523 {
    524 	struct vop_read_args /* {
    525 		struct vnode *a_vp;
    526 		struct uio *a_uio;
    527 		int a_ioflag;
    528 		kauth_cred_t a_cred;
    529 	} */ *ap = v;
    530 	vnode_t *vp = ap->a_vp;
    531 	struct uio *uio = ap->a_uio;
    532 	const int ioflag = ap->a_ioflag;
    533 	tmpfs_node_t *node;
    534 	struct uvm_object *uobj;
    535 	int error;
    536 
    537 	KASSERT(VOP_ISLOCKED(vp));
    538 
    539 	if (vp->v_type != VREG) {
    540 		return EISDIR;
    541 	}
    542 	if (uio->uio_offset < 0) {
    543 		return EINVAL;
    544 	}
    545 
    546 	node = VP_TO_TMPFS_NODE(vp);
    547 	node->tn_status |= TMPFS_NODE_ACCESSED;
    548 	uobj = node->tn_spec.tn_reg.tn_aobj;
    549 	error = 0;
    550 
    551 	while (error == 0 && uio->uio_resid > 0) {
    552 		vsize_t len;
    553 
    554 		if (node->tn_size <= uio->uio_offset) {
    555 			break;
    556 		}
    557 		len = MIN(node->tn_size - uio->uio_offset, uio->uio_resid);
    558 		if (len == 0) {
    559 			break;
    560 		}
    561 		error = ubc_uiomove(uobj, uio, len, IO_ADV_DECODE(ioflag),
    562 		    UBC_READ | UBC_PARTIALOK | UBC_UNMAP_FLAG(vp));
    563 	}
    564 	return error;
    565 }
    566 
    567 int
    568 tmpfs_write(void *v)
    569 {
    570 	struct vop_write_args /* {
    571 		struct vnode	*a_vp;
    572 		struct uio	*a_uio;
    573 		int		a_ioflag;
    574 		kauth_cred_t	a_cred;
    575 	} */ *ap = v;
    576 	vnode_t *vp = ap->a_vp;
    577 	struct uio *uio = ap->a_uio;
    578 	const int ioflag = ap->a_ioflag;
    579 	tmpfs_node_t *node;
    580 	struct uvm_object *uobj;
    581 	off_t oldsize;
    582 	bool extended;
    583 	int error;
    584 
    585 	KASSERT(VOP_ISLOCKED(vp));
    586 
    587 	node = VP_TO_TMPFS_NODE(vp);
    588 	oldsize = node->tn_size;
    589 
    590 	if (uio->uio_offset < 0 || vp->v_type != VREG) {
    591 		error = EINVAL;
    592 		goto out;
    593 	}
    594 	if (uio->uio_resid == 0) {
    595 		error = 0;
    596 		goto out;
    597 	}
    598 	if (ioflag & IO_APPEND) {
    599 		uio->uio_offset = node->tn_size;
    600 	}
    601 
    602 	extended = uio->uio_offset + uio->uio_resid > node->tn_size;
    603 	if (extended) {
    604 		error = tmpfs_reg_resize(vp, uio->uio_offset + uio->uio_resid);
    605 		if (error)
    606 			goto out;
    607 	}
    608 
    609 	uobj = node->tn_spec.tn_reg.tn_aobj;
    610 	error = 0;
    611 	while (error == 0 && uio->uio_resid > 0) {
    612 		vsize_t len;
    613 
    614 		len = MIN(node->tn_size - uio->uio_offset, uio->uio_resid);
    615 		if (len == 0) {
    616 			break;
    617 		}
    618 		error = ubc_uiomove(uobj, uio, len, IO_ADV_DECODE(ioflag),
    619 		    UBC_WRITE | UBC_UNMAP_FLAG(vp));
    620 	}
    621 	if (error) {
    622 		(void)tmpfs_reg_resize(vp, oldsize);
    623 	}
    624 
    625 	node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED |
    626 	    (extended ? TMPFS_NODE_CHANGED : 0);
    627 	VN_KNOTE(vp, NOTE_WRITE);
    628 out:
    629 	if (error) {
    630 		KASSERT(oldsize == node->tn_size);
    631 	} else {
    632 		KASSERT(uio->uio_resid == 0);
    633 	}
    634 	return error;
    635 }
    636 
    637 int
    638 tmpfs_fsync(void *v)
    639 {
    640 	struct vop_fsync_args /* {
    641 		struct vnode *a_vp;
    642 		kauth_cred_t a_cred;
    643 		int a_flags;
    644 		off_t a_offlo;
    645 		off_t a_offhi;
    646 		struct lwp *a_l;
    647 	} */ *ap = v;
    648 	vnode_t *vp = ap->a_vp;
    649 
    650 	/* Nothing to do.  Just update. */
    651 	KASSERT(VOP_ISLOCKED(vp));
    652 	tmpfs_update(vp, NULL, NULL, NULL, 0);
    653 	return 0;
    654 }
    655 
    656 /*
    657  * tmpfs_remove: unlink a file.
    658  *
    659  * => Both directory (dvp) and file (vp) are locked.
    660  * => We unlock and drop the reference on both.
    661  */
    662 int
    663 tmpfs_remove(void *v)
    664 {
    665 	struct vop_remove_args /* {
    666 		struct vnode *a_dvp;
    667 		struct vnode *a_vp;
    668 		struct componentname *a_cnp;
    669 	} */ *ap = v;
    670 	vnode_t *dvp = ap->a_dvp, *vp = ap->a_vp;
    671 	tmpfs_node_t *node;
    672 	tmpfs_dirent_t *de;
    673 	int error;
    674 
    675 	KASSERT(VOP_ISLOCKED(dvp));
    676 	KASSERT(VOP_ISLOCKED(vp));
    677 
    678 	if (vp->v_type == VDIR) {
    679 		error = EPERM;
    680 		goto out;
    681 	}
    682 	node = VP_TO_TMPFS_NODE(vp);
    683 
    684 	/* Files marked as immutable or append-only cannot be deleted. */
    685 	if (node->tn_flags & (IMMUTABLE | APPEND)) {
    686 		error = EPERM;
    687 		goto out;
    688 	}
    689 
    690 	/* Lookup the directory entry (check the cached hint first). */
    691 	de = tmpfs_dir_cached(node);
    692 	if (de == NULL) {
    693 		tmpfs_node_t *dnode = VP_TO_TMPFS_DIR(dvp);
    694 		struct componentname *cnp = ap->a_cnp;
    695 		de = tmpfs_dir_lookup(dnode, cnp);
    696 	}
    697 	KASSERT(de && de->td_node == node);
    698 
    699 	/*
    700 	 * Remove the entry from the directory (drops the link count) and
    701 	 * destroy it or replace it with a whiteout.
    702 	 * Note: the inode referred by it will not be destroyed
    703 	 * until the vnode is reclaimed/recycled.
    704 	 */
    705 	tmpfs_dir_detach(dvp, de);
    706 	if (ap->a_cnp->cn_flags & DOWHITEOUT)
    707 		tmpfs_dir_attach(dvp, de, TMPFS_NODE_WHITEOUT);
    708 	else
    709 		tmpfs_free_dirent(VFS_TO_TMPFS(vp->v_mount), de);
    710 	if (node->tn_links > 0) {
    711 		/* We removed a hard link. */
    712 		node->tn_status |= TMPFS_NODE_CHANGED;
    713 		tmpfs_update(vp, NULL, NULL, NULL, 0);
    714 	}
    715 	error = 0;
    716 out:
    717 	/* Drop the references and unlock the vnodes. */
    718 	vput(vp);
    719 	if (dvp == vp) {
    720 		vrele(dvp);
    721 	} else {
    722 		vput(dvp);
    723 	}
    724 	return error;
    725 }
    726 
    727 /*
    728  * tmpfs_link: create a hard link.
    729  */
    730 int
    731 tmpfs_link(void *v)
    732 {
    733 	struct vop_link_args /* {
    734 		struct vnode *a_dvp;
    735 		struct vnode *a_vp;
    736 		struct componentname *a_cnp;
    737 	} */ *ap = v;
    738 	vnode_t *dvp = ap->a_dvp;
    739 	vnode_t *vp = ap->a_vp;
    740 	struct componentname *cnp = ap->a_cnp;
    741 	tmpfs_node_t *dnode, *node;
    742 	tmpfs_dirent_t *de;
    743 	int error;
    744 
    745 	KASSERT(dvp != vp);
    746 	KASSERT(VOP_ISLOCKED(dvp));
    747 	KASSERT(vp->v_type != VDIR);
    748 	KASSERT(dvp->v_mount == vp->v_mount);
    749 
    750 	dnode = VP_TO_TMPFS_DIR(dvp);
    751 	node = VP_TO_TMPFS_NODE(vp);
    752 
    753 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    754 
    755 	/* Check for maximum number of links limit. */
    756 	if (node->tn_links == LINK_MAX) {
    757 		error = EMLINK;
    758 		goto out;
    759 	}
    760 	KASSERT(node->tn_links < LINK_MAX);
    761 
    762 	/* We cannot create links of files marked immutable or append-only. */
    763 	if (node->tn_flags & (IMMUTABLE | APPEND)) {
    764 		error = EPERM;
    765 		goto out;
    766 	}
    767 
    768 	/* Allocate a new directory entry to represent the inode. */
    769 	error = tmpfs_alloc_dirent(VFS_TO_TMPFS(vp->v_mount),
    770 	    cnp->cn_nameptr, cnp->cn_namelen, &de);
    771 	if (error) {
    772 		goto out;
    773 	}
    774 
    775 	/*
    776 	 * Insert the entry into the directory.
    777 	 * It will increase the inode link count.
    778 	 */
    779 	tmpfs_dir_attach(dvp, de, node);
    780 
    781 	/* Update the timestamps and trigger the event. */
    782 	if (node->tn_vnode) {
    783 		VN_KNOTE(node->tn_vnode, NOTE_LINK);
    784 	}
    785 	node->tn_status |= TMPFS_NODE_CHANGED;
    786 	tmpfs_update(vp, NULL, NULL, NULL, 0);
    787 	error = 0;
    788 out:
    789 	VOP_UNLOCK(vp);
    790 	vput(dvp);
    791 	return error;
    792 }
    793 
    794 int
    795 tmpfs_mkdir(void *v)
    796 {
    797 	struct vop_mkdir_args /* {
    798 		struct vnode		*a_dvp;
    799 		struct vnode		**a_vpp;
    800 		struct componentname	*a_cnp;
    801 		struct vattr		*a_vap;
    802 	} */ *ap = v;
    803 	vnode_t *dvp = ap->a_dvp;
    804 	vnode_t **vpp = ap->a_vpp;
    805 	struct componentname *cnp = ap->a_cnp;
    806 	struct vattr *vap = ap->a_vap;
    807 
    808 	KASSERT(vap->va_type == VDIR);
    809 	return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
    810 }
    811 
    812 int
    813 tmpfs_rmdir(void *v)
    814 {
    815 	struct vop_rmdir_args /* {
    816 		struct vnode		*a_dvp;
    817 		struct vnode		*a_vp;
    818 		struct componentname	*a_cnp;
    819 	} */ *ap = v;
    820 	vnode_t *dvp = ap->a_dvp;
    821 	vnode_t *vp = ap->a_vp;
    822 	tmpfs_mount_t *tmp = VFS_TO_TMPFS(dvp->v_mount);
    823 	tmpfs_node_t *dnode = VP_TO_TMPFS_DIR(dvp);
    824 	tmpfs_node_t *node = VP_TO_TMPFS_DIR(vp);
    825 	tmpfs_dirent_t *de;
    826 	int error = 0;
    827 
    828 	KASSERT(VOP_ISLOCKED(dvp));
    829 	KASSERT(VOP_ISLOCKED(vp));
    830 	KASSERT(node->tn_spec.tn_dir.tn_parent == dnode);
    831 
    832 	/*
    833 	 * Directories with more than two non-whiteout
    834 	 * entries ('.' and '..') cannot be removed.
    835 	 */
    836 	if (node->tn_size > 0) {
    837 		KASSERT(error == 0);
    838 		TAILQ_FOREACH(de, &node->tn_spec.tn_dir.tn_dir, td_entries) {
    839 			if (de->td_node != TMPFS_NODE_WHITEOUT) {
    840 				error = ENOTEMPTY;
    841 				break;
    842 			}
    843 		}
    844 		if (error)
    845 			goto out;
    846 	}
    847 
    848 	/* Lookup the directory entry (check the cached hint first). */
    849 	de = tmpfs_dir_cached(node);
    850 	if (de == NULL) {
    851 		struct componentname *cnp = ap->a_cnp;
    852 		de = tmpfs_dir_lookup(dnode, cnp);
    853 	}
    854 	KASSERT(de && de->td_node == node);
    855 
    856 	/* Check flags to see if we are allowed to remove the directory. */
    857 	if (dnode->tn_flags & APPEND || node->tn_flags & (IMMUTABLE | APPEND)) {
    858 		error = EPERM;
    859 		goto out;
    860 	}
    861 
    862 	/* Decrement the link count for the virtual '.' entry. */
    863 	node->tn_links--;
    864 	node->tn_status |= TMPFS_NODE_STATUSALL;
    865 
    866 	/* Detach the directory entry from the directory. */
    867 	tmpfs_dir_detach(dvp, de);
    868 
    869 	/* Purge the cache for parent. */
    870 	cache_purge(dvp);
    871 
    872 	/*
    873 	 * Destroy the directory entry or replace it with a whiteout.
    874 	 * Note: the inode referred by it will not be destroyed
    875 	 * until the vnode is reclaimed.
    876 	 */
    877 	if (ap->a_cnp->cn_flags & DOWHITEOUT)
    878 		tmpfs_dir_attach(dvp, de, TMPFS_NODE_WHITEOUT);
    879 	else
    880 		tmpfs_free_dirent(tmp, de);
    881 
    882 	/* Destroy the whiteout entries from the node. */
    883 	while ((de = TAILQ_FIRST(&node->tn_spec.tn_dir.tn_dir)) != NULL) {
    884 		KASSERT(de->td_node == TMPFS_NODE_WHITEOUT);
    885 		tmpfs_dir_detach(vp, de);
    886 		tmpfs_free_dirent(tmp, de);
    887 	}
    888 
    889 	KASSERT(node->tn_links == 0);
    890 out:
    891 	/* Release the nodes. */
    892 	vput(dvp);
    893 	vput(vp);
    894 	return error;
    895 }
    896 
    897 int
    898 tmpfs_symlink(void *v)
    899 {
    900 	struct vop_symlink_args /* {
    901 		struct vnode		*a_dvp;
    902 		struct vnode		**a_vpp;
    903 		struct componentname	*a_cnp;
    904 		struct vattr		*a_vap;
    905 		char			*a_target;
    906 	} */ *ap = v;
    907 	vnode_t *dvp = ap->a_dvp;
    908 	vnode_t **vpp = ap->a_vpp;
    909 	struct componentname *cnp = ap->a_cnp;
    910 	struct vattr *vap = ap->a_vap;
    911 	char *target = ap->a_target;
    912 
    913 	KASSERT(vap->va_type == VLNK);
    914 	return tmpfs_alloc_file(dvp, vpp, vap, cnp, target);
    915 }
    916 
    917 int
    918 tmpfs_readdir(void *v)
    919 {
    920 	struct vop_readdir_args /* {
    921 		struct vnode	*a_vp;
    922 		struct uio	*a_uio;
    923 		kauth_cred_t	a_cred;
    924 		int		*a_eofflag;
    925 		off_t		**a_cookies;
    926 		int		*ncookies;
    927 	} */ *ap = v;
    928 	vnode_t *vp = ap->a_vp;
    929 	struct uio *uio = ap->a_uio;
    930 	int *eofflag = ap->a_eofflag;
    931 	off_t **cookies = ap->a_cookies;
    932 	int *ncookies = ap->a_ncookies;
    933 	off_t startoff, cnt;
    934 	tmpfs_node_t *node;
    935 	int error;
    936 
    937 	KASSERT(VOP_ISLOCKED(vp));
    938 
    939 	/* This operation only makes sense on directory nodes. */
    940 	if (vp->v_type != VDIR) {
    941 		return ENOTDIR;
    942 	}
    943 	node = VP_TO_TMPFS_DIR(vp);
    944 	startoff = uio->uio_offset;
    945 	cnt = 0;
    946 	if (node->tn_links == 0) {
    947 		error = 0;
    948 		goto out;
    949 	}
    950 
    951 	if (uio->uio_offset == TMPFS_DIRCOOKIE_DOT) {
    952 		error = tmpfs_dir_getdotdent(node, uio);
    953 		if (error != 0) {
    954 			if (error == -1)
    955 				error = 0;
    956 			goto out;
    957 		}
    958 		cnt++;
    959 	}
    960 	if (uio->uio_offset == TMPFS_DIRCOOKIE_DOTDOT) {
    961 		error = tmpfs_dir_getdotdotdent(node, uio);
    962 		if (error != 0) {
    963 			if (error == -1)
    964 				error = 0;
    965 			goto out;
    966 		}
    967 		cnt++;
    968 	}
    969 	error = tmpfs_dir_getdents(node, uio, &cnt);
    970 	if (error == -1) {
    971 		error = 0;
    972 	}
    973 	KASSERT(error >= 0);
    974 out:
    975 	if (eofflag != NULL) {
    976 		*eofflag = (!error && uio->uio_offset == TMPFS_DIRCOOKIE_EOF);
    977 	}
    978 	if (error || cookies == NULL || ncookies == NULL) {
    979 		return error;
    980 	}
    981 
    982 	/* Update NFS-related variables, if any. */
    983 	off_t i, off = startoff;
    984 	tmpfs_dirent_t *de = NULL;
    985 
    986 	*cookies = malloc(cnt * sizeof(off_t), M_TEMP, M_WAITOK);
    987 	*ncookies = cnt;
    988 
    989 	for (i = 0; i < cnt; i++) {
    990 		KASSERT(off != TMPFS_DIRCOOKIE_EOF);
    991 		if (off != TMPFS_DIRCOOKIE_DOT) {
    992 			if (off == TMPFS_DIRCOOKIE_DOTDOT) {
    993 				de = TAILQ_FIRST(&node->tn_spec.tn_dir.tn_dir);
    994 			} else if (de != NULL) {
    995 				de = TAILQ_NEXT(de, td_entries);
    996 			} else {
    997 				de = tmpfs_dir_lookupbycookie(node, off);
    998 				KASSERT(de != NULL);
    999 				de = TAILQ_NEXT(de, td_entries);
   1000 			}
   1001 			if (de == NULL) {
   1002 				off = TMPFS_DIRCOOKIE_EOF;
   1003 			} else {
   1004 				off = tmpfs_dircookie(de);
   1005 			}
   1006 		} else {
   1007 			off = TMPFS_DIRCOOKIE_DOTDOT;
   1008 		}
   1009 		(*cookies)[i] = off;
   1010 	}
   1011 	KASSERT(uio->uio_offset == off);
   1012 	return error;
   1013 }
   1014 
   1015 int
   1016 tmpfs_readlink(void *v)
   1017 {
   1018 	struct vop_readlink_args /* {
   1019 		struct vnode	*a_vp;
   1020 		struct uio	*a_uio;
   1021 		kauth_cred_t	a_cred;
   1022 	} */ *ap = v;
   1023 	vnode_t *vp = ap->a_vp;
   1024 	struct uio *uio = ap->a_uio;
   1025 	tmpfs_node_t *node;
   1026 	int error;
   1027 
   1028 	KASSERT(VOP_ISLOCKED(vp));
   1029 	KASSERT(uio->uio_offset == 0);
   1030 	KASSERT(vp->v_type == VLNK);
   1031 
   1032 	node = VP_TO_TMPFS_NODE(vp);
   1033 	error = uiomove(node->tn_spec.tn_lnk.tn_link,
   1034 	    MIN(node->tn_size, uio->uio_resid), uio);
   1035 	node->tn_status |= TMPFS_NODE_ACCESSED;
   1036 
   1037 	return error;
   1038 }
   1039 
   1040 int
   1041 tmpfs_inactive(void *v)
   1042 {
   1043 	struct vop_inactive_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 
   1050 	KASSERT(VOP_ISLOCKED(vp));
   1051 
   1052 	node = VP_TO_TMPFS_NODE(vp);
   1053 	*ap->a_recycle = (node->tn_links == 0);
   1054 	VOP_UNLOCK(vp);
   1055 
   1056 	return 0;
   1057 }
   1058 
   1059 int
   1060 tmpfs_reclaim(void *v)
   1061 {
   1062 	struct vop_reclaim_args /* {
   1063 		struct vnode *a_vp;
   1064 	} */ *ap = v;
   1065 	vnode_t *vp = ap->a_vp;
   1066 	tmpfs_mount_t *tmp = VFS_TO_TMPFS(vp->v_mount);
   1067 	tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp);
   1068 	bool racing;
   1069 
   1070 	/* Disassociate inode from vnode. */
   1071 	mutex_enter(&node->tn_vlock);
   1072 	node->tn_vnode = NULL;
   1073 	vp->v_data = NULL;
   1074 	/* Check if tmpfs_vnode_get() is racing with us. */
   1075 	racing = TMPFS_NODE_RECLAIMING(node);
   1076 	mutex_exit(&node->tn_vlock);
   1077 
   1078 	/*
   1079 	 * If inode is not referenced, i.e. no links, then destroy it.
   1080 	 * Note: if racing - inode is about to get a new vnode, leave it.
   1081 	 */
   1082 	if (node->tn_links == 0 && !racing) {
   1083 		tmpfs_free_node(tmp, node);
   1084 	}
   1085 	return 0;
   1086 }
   1087 
   1088 int
   1089 tmpfs_pathconf(void *v)
   1090 {
   1091 	struct vop_pathconf_args /* {
   1092 		struct vnode	*a_vp;
   1093 		int		a_name;
   1094 		register_t	*a_retval;
   1095 	} */ *ap = v;
   1096 	const int name = ap->a_name;
   1097 	register_t *retval = ap->a_retval;
   1098 	int error = 0;
   1099 
   1100 	switch (name) {
   1101 	case _PC_LINK_MAX:
   1102 		*retval = LINK_MAX;
   1103 		break;
   1104 	case _PC_NAME_MAX:
   1105 		*retval = TMPFS_MAXNAMLEN;
   1106 		break;
   1107 	case _PC_PATH_MAX:
   1108 		*retval = PATH_MAX;
   1109 		break;
   1110 	case _PC_PIPE_BUF:
   1111 		*retval = PIPE_BUF;
   1112 		break;
   1113 	case _PC_CHOWN_RESTRICTED:
   1114 		*retval = 1;
   1115 		break;
   1116 	case _PC_NO_TRUNC:
   1117 		*retval = 1;
   1118 		break;
   1119 	case _PC_SYNC_IO:
   1120 		*retval = 1;
   1121 		break;
   1122 	case _PC_FILESIZEBITS:
   1123 		*retval = sizeof(off_t) * CHAR_BIT;
   1124 		break;
   1125 	default:
   1126 		error = EINVAL;
   1127 	}
   1128 	return error;
   1129 }
   1130 
   1131 int
   1132 tmpfs_advlock(void *v)
   1133 {
   1134 	struct vop_advlock_args /* {
   1135 		struct vnode	*a_vp;
   1136 		void *		a_id;
   1137 		int		a_op;
   1138 		struct flock	*a_fl;
   1139 		int		a_flags;
   1140 	} */ *ap = v;
   1141 	vnode_t *vp = ap->a_vp;
   1142 	tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp);
   1143 
   1144 	return lf_advlock(v, &node->tn_lockf, node->tn_size);
   1145 }
   1146 
   1147 int
   1148 tmpfs_getpages(void *v)
   1149 {
   1150 	struct vop_getpages_args /* {
   1151 		struct vnode *a_vp;
   1152 		voff_t a_offset;
   1153 		struct vm_page **a_m;
   1154 		int *a_count;
   1155 		int a_centeridx;
   1156 		vm_prot_t a_access_type;
   1157 		int a_advice;
   1158 		int a_flags;
   1159 	} */ * const ap = v;
   1160 	vnode_t *vp = ap->a_vp;
   1161 	const voff_t offset = ap->a_offset;
   1162 	struct vm_page **pgs = ap->a_m;
   1163 	const int centeridx = ap->a_centeridx;
   1164 	const vm_prot_t access_type = ap->a_access_type;
   1165 	const int advice = ap->a_advice;
   1166 	const int flags = ap->a_flags;
   1167 	int error, npages = *ap->a_count;
   1168 	tmpfs_node_t *node;
   1169 	struct uvm_object *uobj;
   1170 
   1171 	KASSERT(vp->v_type == VREG);
   1172 	KASSERT(mutex_owned(vp->v_interlock));
   1173 
   1174 	node = VP_TO_TMPFS_NODE(vp);
   1175 	uobj = node->tn_spec.tn_reg.tn_aobj;
   1176 
   1177 	/*
   1178 	 * Currently, PGO_PASTEOF is not supported.
   1179 	 */
   1180 	if (vp->v_size <= offset + (centeridx << PAGE_SHIFT)) {
   1181 		if ((flags & PGO_LOCKED) == 0)
   1182 			mutex_exit(vp->v_interlock);
   1183 		return EINVAL;
   1184 	}
   1185 
   1186 	if (vp->v_size < offset + (npages << PAGE_SHIFT)) {
   1187 		npages = (round_page(vp->v_size) - offset) >> PAGE_SHIFT;
   1188 	}
   1189 
   1190 	if ((flags & PGO_LOCKED) != 0)
   1191 		return EBUSY;
   1192 
   1193 	if ((flags & PGO_NOTIMESTAMP) == 0) {
   1194 		if ((vp->v_mount->mnt_flag & MNT_NOATIME) == 0)
   1195 			node->tn_status |= TMPFS_NODE_ACCESSED;
   1196 
   1197 		if ((access_type & VM_PROT_WRITE) != 0) {
   1198 			node->tn_status |= TMPFS_NODE_MODIFIED;
   1199 			if (vp->v_mount->mnt_flag & MNT_RELATIME)
   1200 				node->tn_status |= TMPFS_NODE_ACCESSED;
   1201 		}
   1202 	}
   1203 
   1204 	/*
   1205 	 * Invoke the pager.
   1206 	 *
   1207 	 * Clean the array of pages before.  XXX: PR/32166
   1208 	 * Note that vnode lock is shared with underlying UVM object.
   1209 	 */
   1210 	if (pgs) {
   1211 		memset(pgs, 0, sizeof(struct vm_pages *) * npages);
   1212 	}
   1213 	KASSERT(vp->v_interlock == uobj->vmobjlock);
   1214 
   1215 	error = (*uobj->pgops->pgo_get)(uobj, offset, pgs, &npages, centeridx,
   1216 	    access_type, advice, flags | PGO_ALLPAGES);
   1217 
   1218 #if defined(DEBUG)
   1219 	if (!error && pgs) {
   1220 		for (int i = 0; i < npages; i++) {
   1221 			KASSERT(pgs[i] != NULL);
   1222 		}
   1223 	}
   1224 #endif
   1225 	return error;
   1226 }
   1227 
   1228 int
   1229 tmpfs_putpages(void *v)
   1230 {
   1231 	struct vop_putpages_args /* {
   1232 		struct vnode *a_vp;
   1233 		voff_t a_offlo;
   1234 		voff_t a_offhi;
   1235 		int a_flags;
   1236 	} */ * const ap = v;
   1237 	vnode_t *vp = ap->a_vp;
   1238 	const voff_t offlo = ap->a_offlo;
   1239 	const voff_t offhi = ap->a_offhi;
   1240 	const int flags = ap->a_flags;
   1241 	tmpfs_node_t *node;
   1242 	struct uvm_object *uobj;
   1243 	int error;
   1244 
   1245 	KASSERT(mutex_owned(vp->v_interlock));
   1246 
   1247 	if (vp->v_type != VREG) {
   1248 		mutex_exit(vp->v_interlock);
   1249 		return 0;
   1250 	}
   1251 
   1252 	node = VP_TO_TMPFS_NODE(vp);
   1253 	uobj = node->tn_spec.tn_reg.tn_aobj;
   1254 
   1255 	KASSERT(vp->v_interlock == uobj->vmobjlock);
   1256 	error = (*uobj->pgops->pgo_put)(uobj, offlo, offhi, flags);
   1257 
   1258 	/* XXX mtime */
   1259 
   1260 	return error;
   1261 }
   1262 
   1263 int
   1264 tmpfs_whiteout(void *v)
   1265 {
   1266 	struct vop_whiteout_args /* {
   1267 		struct vnode		*a_dvp;
   1268 		struct componentname	*a_cnp;
   1269 		int			a_flags;
   1270 	} */ *ap = v;
   1271 	vnode_t *dvp = ap->a_dvp;
   1272 	struct componentname *cnp = ap->a_cnp;
   1273 	const int flags = ap->a_flags;
   1274 	tmpfs_mount_t *tmp = VFS_TO_TMPFS(dvp->v_mount);
   1275 	tmpfs_dirent_t *de;
   1276 	int error;
   1277 
   1278 	switch (flags) {
   1279 	case LOOKUP:
   1280 		break;
   1281 	case CREATE:
   1282 		error = tmpfs_alloc_dirent(tmp, cnp->cn_nameptr,
   1283 		    cnp->cn_namelen, &de);
   1284 		if (error)
   1285 			return error;
   1286 		tmpfs_dir_attach(dvp, de, TMPFS_NODE_WHITEOUT);
   1287 		break;
   1288 	case DELETE:
   1289 		cnp->cn_flags &= ~DOWHITEOUT; /* when in doubt, cargo cult */
   1290 		de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(dvp), cnp);
   1291 		if (de == NULL)
   1292 			return ENOENT;
   1293 		tmpfs_dir_detach(dvp, de);
   1294 		tmpfs_free_dirent(tmp, de);
   1295 		break;
   1296 	}
   1297 	return 0;
   1298 }
   1299 
   1300 int
   1301 tmpfs_print(void *v)
   1302 {
   1303 	struct vop_print_args /* {
   1304 		struct vnode	*a_vp;
   1305 	} */ *ap = v;
   1306 	vnode_t *vp = ap->a_vp;
   1307 	tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp);
   1308 
   1309 	printf("tag VT_TMPFS, tmpfs_node %p, flags 0x%x, links %d\n"
   1310 	    "\tmode 0%o, owner %d, group %d, size %" PRIdMAX ", status 0x%x",
   1311 	    node, node->tn_flags, node->tn_links, node->tn_mode, node->tn_uid,
   1312 	    node->tn_gid, (uintmax_t)node->tn_size, node->tn_status);
   1313 	if (vp->v_type == VFIFO) {
   1314 		VOCALL(fifo_vnodeop_p, VOFFSET(vop_print), v);
   1315 	}
   1316 	printf("\n");
   1317 	return 0;
   1318 }
   1319