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