Home | History | Annotate | Line # | Download | only in tmpfs
tmpfs_vnops.c revision 1.44.2.9
      1 /*	$NetBSD: tmpfs_vnops.c,v 1.44.2.9 2007/12/27 17:54:34 ad 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  * 3. All advertising materials mentioning features or use of this software
     20  *    must display the following acknowledgement:
     21  *        This product includes software developed by the NetBSD
     22  *        Foundation, Inc. and its contributors.
     23  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24  *    contributors may be used to endorse or promote products derived
     25  *    from this software without specific prior written permission.
     26  *
     27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  * POSSIBILITY OF SUCH DAMAGE.
     38  */
     39 
     40 /*
     41  * tmpfs vnode interface.
     42  */
     43 
     44 #include <sys/cdefs.h>
     45 __KERNEL_RCSID(0, "$NetBSD: tmpfs_vnops.c,v 1.44.2.9 2007/12/27 17:54:34 ad Exp $");
     46 
     47 #include <sys/param.h>
     48 #include <sys/dirent.h>
     49 #include <sys/fcntl.h>
     50 #include <sys/event.h>
     51 #include <sys/malloc.h>
     52 #include <sys/namei.h>
     53 #include <sys/proc.h>
     54 #include <sys/stat.h>
     55 #include <sys/uio.h>
     56 #include <sys/unistd.h>
     57 #include <sys/vnode.h>
     58 #include <sys/lockf.h>
     59 #include <sys/kauth.h>
     60 
     61 #include <uvm/uvm.h>
     62 
     63 #include <miscfs/fifofs/fifo.h>
     64 #include <fs/tmpfs/tmpfs_vnops.h>
     65 #include <fs/tmpfs/tmpfs.h>
     66 
     67 /* --------------------------------------------------------------------- */
     68 
     69 /*
     70  * vnode operations vector used for files stored in a tmpfs file system.
     71  */
     72 int (**tmpfs_vnodeop_p)(void *);
     73 const struct vnodeopv_entry_desc tmpfs_vnodeop_entries[] = {
     74 	{ &vop_default_desc,		vn_default_error },
     75 	{ &vop_lookup_desc,		tmpfs_lookup },
     76 	{ &vop_create_desc,		tmpfs_create },
     77 	{ &vop_mknod_desc,		tmpfs_mknod },
     78 	{ &vop_open_desc,		tmpfs_open },
     79 	{ &vop_close_desc,		tmpfs_close },
     80 	{ &vop_access_desc,		tmpfs_access },
     81 	{ &vop_getattr_desc,		tmpfs_getattr },
     82 	{ &vop_setattr_desc,		tmpfs_setattr },
     83 	{ &vop_read_desc,		tmpfs_read },
     84 	{ &vop_write_desc,		tmpfs_write },
     85 	{ &vop_ioctl_desc,		tmpfs_ioctl },
     86 	{ &vop_fcntl_desc,		tmpfs_fcntl },
     87 	{ &vop_poll_desc,		tmpfs_poll },
     88 	{ &vop_kqfilter_desc,		tmpfs_kqfilter },
     89 	{ &vop_revoke_desc,		tmpfs_revoke },
     90 	{ &vop_mmap_desc,		tmpfs_mmap },
     91 	{ &vop_fsync_desc,		tmpfs_fsync },
     92 	{ &vop_seek_desc,		tmpfs_seek },
     93 	{ &vop_remove_desc,		tmpfs_remove },
     94 	{ &vop_link_desc,		tmpfs_link },
     95 	{ &vop_rename_desc,		tmpfs_rename },
     96 	{ &vop_mkdir_desc,		tmpfs_mkdir },
     97 	{ &vop_rmdir_desc,		tmpfs_rmdir },
     98 	{ &vop_symlink_desc,		tmpfs_symlink },
     99 	{ &vop_readdir_desc,		tmpfs_readdir },
    100 	{ &vop_readlink_desc,		tmpfs_readlink },
    101 	{ &vop_abortop_desc,		tmpfs_abortop },
    102 	{ &vop_inactive_desc,		tmpfs_inactive },
    103 	{ &vop_reclaim_desc,		tmpfs_reclaim },
    104 	{ &vop_lock_desc,		tmpfs_lock },
    105 	{ &vop_unlock_desc,		tmpfs_unlock },
    106 	{ &vop_bmap_desc,		tmpfs_bmap },
    107 	{ &vop_strategy_desc,		tmpfs_strategy },
    108 	{ &vop_print_desc,		tmpfs_print },
    109 	{ &vop_pathconf_desc,		tmpfs_pathconf },
    110 	{ &vop_islocked_desc,		tmpfs_islocked },
    111 	{ &vop_advlock_desc,		tmpfs_advlock },
    112 	{ &vop_lease_desc,		tmpfs_lease },
    113 	{ &vop_bwrite_desc,		tmpfs_bwrite },
    114 	{ &vop_getpages_desc,		tmpfs_getpages },
    115 	{ &vop_putpages_desc,		tmpfs_putpages },
    116 	{ NULL, NULL }
    117 };
    118 const struct vnodeopv_desc tmpfs_vnodeop_opv_desc =
    119 	{ &tmpfs_vnodeop_p, tmpfs_vnodeop_entries };
    120 
    121 /* --------------------------------------------------------------------- */
    122 
    123 int
    124 tmpfs_lookup(void *v)
    125 {
    126 	struct vnode *dvp = ((struct vop_lookup_args *)v)->a_dvp;
    127 	struct vnode **vpp = ((struct vop_lookup_args *)v)->a_vpp;
    128 	struct componentname *cnp = ((struct vop_lookup_args *)v)->a_cnp;
    129 
    130 	int error;
    131 	struct tmpfs_dirent *de;
    132 	struct tmpfs_node *dnode;
    133 
    134 	KASSERT(VOP_ISLOCKED(dvp));
    135 
    136 	dnode = VP_TO_TMPFS_DIR(dvp);
    137 	*vpp = NULL;
    138 
    139 	/* Check accessibility of requested node as a first step. */
    140 	error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred);
    141 	if (error != 0)
    142 		goto out;
    143 
    144 	/* If requesting the last path component on a read-only file system
    145 	 * with a write operation, deny it. */
    146 	if ((cnp->cn_flags & ISLASTCN) &&
    147 	    (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
    148 	    (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
    149 		error = EROFS;
    150 		goto out;
    151 	}
    152 
    153 	/* Avoid doing a linear scan of the directory if the requested
    154 	 * directory/name couple is already in the cache. */
    155 	error = cache_lookup(dvp, vpp, cnp);
    156 	if (error >= 0)
    157 		goto out;
    158 
    159 	/* We cannot be requesting the parent directory of the root node. */
    160 	KASSERT(IMPLIES(dnode->tn_type == VDIR &&
    161 	    dnode->tn_spec.tn_dir.tn_parent == dnode,
    162 	    !(cnp->cn_flags & ISDOTDOT)));
    163 
    164 	if (cnp->cn_flags & ISDOTDOT) {
    165 		VOP_UNLOCK(dvp, 0);
    166 
    167 		/* Allocate a new vnode on the matching entry. */
    168 		error = tmpfs_alloc_vp(dvp->v_mount,
    169 		    dnode->tn_spec.tn_dir.tn_parent, vpp);
    170 
    171 		vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
    172 	} else if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') {
    173 		VREF(dvp);
    174 		*vpp = dvp;
    175 		error = 0;
    176 	} else {
    177 		de = tmpfs_dir_lookup(dnode, cnp);
    178 		if (de == NULL) {
    179 			/* The entry was not found in the directory.
    180 			 * This is OK iff we are creating or renaming an
    181 			 * entry and are working on the last component of
    182 			 * the path name. */
    183 			if ((cnp->cn_flags & ISLASTCN) &&
    184 			    (cnp->cn_nameiop == CREATE || \
    185 			    cnp->cn_nameiop == RENAME)) {
    186 				error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred);
    187 				if (error != 0)
    188 					goto out;
    189 
    190 				/* Keep the component name in the buffer for
    191 				 * future uses. */
    192 				cnp->cn_flags |= SAVENAME;
    193 
    194 				error = EJUSTRETURN;
    195 			} else
    196 				error = ENOENT;
    197 		} else {
    198 			struct tmpfs_node *tnode;
    199 
    200 			/* The entry was found, so get its associated
    201 			 * tmpfs_node. */
    202 			tnode = de->td_node;
    203 
    204 			/* If we are not at the last path component and
    205 			 * found a non-directory or non-link entry (which
    206 			 * may itself be pointing to a directory), raise
    207 			 * an error. */
    208 			if ((tnode->tn_type != VDIR &&
    209 			    tnode->tn_type != VLNK) &&
    210 			    !(cnp->cn_flags & ISLASTCN)) {
    211 				error = ENOTDIR;
    212 				goto out;
    213 			}
    214 
    215 			/* If we are deleting or renaming the entry, keep
    216 			 * track of its tmpfs_dirent so that it can be
    217 			 * easily deleted later. */
    218 			if ((cnp->cn_flags & ISLASTCN) &&
    219 			    (cnp->cn_nameiop == DELETE ||
    220 			    cnp->cn_nameiop == RENAME)) {
    221 				if ((dnode->tn_mode & S_ISTXT) != 0 &&
    222 				    kauth_authorize_generic(cnp->cn_cred,
    223 				     KAUTH_GENERIC_ISSUSER, NULL) != 0 &&
    224 				    kauth_cred_geteuid(cnp->cn_cred) != dnode->tn_uid &&
    225 				    kauth_cred_geteuid(cnp->cn_cred) != tnode->tn_uid)
    226 					return EPERM;
    227 				error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred);
    228 				if (error != 0)
    229 					goto out;
    230 			} else
    231 				de = NULL;
    232 
    233 			/* Allocate a new vnode on the matching entry. */
    234 			error = tmpfs_alloc_vp(dvp->v_mount, tnode, vpp);
    235 		}
    236 	}
    237 
    238 	/* Store the result of this lookup in the cache.  Avoid this if the
    239 	 * request was for creation, as it does not improve timings on
    240 	 * emprical tests. */
    241 	if ((cnp->cn_flags & MAKEENTRY) && cnp->cn_nameiop != CREATE &&
    242 	    (cnp->cn_flags & ISDOTDOT) == 0)
    243 		cache_enter(dvp, *vpp, cnp);
    244 
    245 out:
    246 	/* If there were no errors, *vpp cannot be null and it must be
    247 	 * locked. */
    248 	KASSERT(IFF(error == 0, *vpp != NULL && VOP_ISLOCKED(*vpp)));
    249 
    250 	/* dvp must always be locked. */
    251 	KASSERT(VOP_ISLOCKED(dvp));
    252 
    253 	return error;
    254 }
    255 
    256 /* --------------------------------------------------------------------- */
    257 
    258 int
    259 tmpfs_create(void *v)
    260 {
    261 	struct vnode *dvp = ((struct vop_create_args *)v)->a_dvp;
    262 	struct vnode **vpp = ((struct vop_create_args *)v)->a_vpp;
    263 	struct componentname *cnp = ((struct vop_create_args *)v)->a_cnp;
    264 	struct vattr *vap = ((struct vop_create_args *)v)->a_vap;
    265 
    266 	KASSERT(vap->va_type == VREG || vap->va_type == VSOCK);
    267 
    268 	return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
    269 }
    270 /* --------------------------------------------------------------------- */
    271 
    272 int
    273 tmpfs_mknod(void *v)
    274 {
    275 	struct vnode *dvp = ((struct vop_mknod_args *)v)->a_dvp;
    276 	struct vnode **vpp = ((struct vop_mknod_args *)v)->a_vpp;
    277 	struct componentname *cnp = ((struct vop_mknod_args *)v)->a_cnp;
    278 	struct vattr *vap = ((struct vop_mknod_args *)v)->a_vap;
    279 
    280 	if (vap->va_type != VBLK && vap->va_type != VCHR &&
    281 	    vap->va_type != VFIFO)
    282 		return EINVAL;
    283 
    284 	return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
    285 }
    286 
    287 /* --------------------------------------------------------------------- */
    288 
    289 int
    290 tmpfs_open(void *v)
    291 {
    292 	struct vnode *vp = ((struct vop_open_args *)v)->a_vp;
    293 	int mode = ((struct vop_open_args *)v)->a_mode;
    294 
    295 	int error;
    296 	struct tmpfs_node *node;
    297 
    298 	KASSERT(VOP_ISLOCKED(vp));
    299 
    300 	node = VP_TO_TMPFS_NODE(vp);
    301 
    302 	/* The file is still active but all its names have been removed
    303 	 * (e.g. by a "rmdir $(pwd)").  It cannot be opened any more as
    304 	 * it is about to die. */
    305 	if (node->tn_links < 1) {
    306 		error = ENOENT;
    307 		goto out;
    308 	}
    309 
    310 	/* If the file is marked append-only, deny write requests. */
    311 	if (node->tn_flags & APPEND && (mode & (FWRITE | O_APPEND)) == FWRITE)
    312 		error = EPERM;
    313 	else
    314 		error = 0;
    315 
    316 out:
    317 	KASSERT(VOP_ISLOCKED(vp));
    318 
    319 	return error;
    320 }
    321 
    322 /* --------------------------------------------------------------------- */
    323 
    324 int
    325 tmpfs_close(void *v)
    326 {
    327 	struct vnode *vp = ((struct vop_close_args *)v)->a_vp;
    328 
    329 	struct tmpfs_node *node;
    330 
    331 	KASSERT(VOP_ISLOCKED(vp));
    332 
    333 	node = VP_TO_TMPFS_NODE(vp);
    334 
    335 	if (node->tn_links > 0) {
    336 		/* Update node times.  No need to do it if the node has
    337 		 * been deleted, because it will vanish after we return. */
    338 		tmpfs_update(vp, NULL, NULL, UPDATE_CLOSE);
    339 	}
    340 
    341 	return 0;
    342 }
    343 
    344 /* --------------------------------------------------------------------- */
    345 
    346 int
    347 tmpfs_access(void *v)
    348 {
    349 	struct vnode *vp = ((struct vop_access_args *)v)->a_vp;
    350 	int mode = ((struct vop_access_args *)v)->a_mode;
    351 	kauth_cred_t cred = ((struct vop_access_args *)v)->a_cred;
    352 
    353 	int error;
    354 	struct tmpfs_node *node;
    355 
    356 	KASSERT(VOP_ISLOCKED(vp));
    357 
    358 	node = VP_TO_TMPFS_NODE(vp);
    359 
    360 	switch (vp->v_type) {
    361 	case VDIR:
    362 		/* FALLTHROUGH */
    363 	case VLNK:
    364 		/* FALLTHROUGH */
    365 	case VREG:
    366 		if (mode & VWRITE && vp->v_mount->mnt_flag & MNT_RDONLY) {
    367 			error = EROFS;
    368 			goto out;
    369 		}
    370 		break;
    371 
    372 	case VBLK:
    373 		/* FALLTHROUGH */
    374 	case VCHR:
    375 		/* FALLTHROUGH */
    376 	case VSOCK:
    377 		/* FALLTHROUGH */
    378 	case VFIFO:
    379 		break;
    380 
    381 	default:
    382 		error = EINVAL;
    383 		goto out;
    384 	}
    385 
    386 	if (mode & VWRITE && node->tn_flags & IMMUTABLE) {
    387 		error = EPERM;
    388 		goto out;
    389 	}
    390 
    391 	error = vaccess(vp->v_type, node->tn_mode, node->tn_uid,
    392 	    node->tn_gid, mode, cred);
    393 
    394 out:
    395 	KASSERT(VOP_ISLOCKED(vp));
    396 
    397 	return error;
    398 }
    399 
    400 /* --------------------------------------------------------------------- */
    401 
    402 int
    403 tmpfs_getattr(void *v)
    404 {
    405 	struct vnode *vp = ((struct vop_getattr_args *)v)->a_vp;
    406 	struct vattr *vap = ((struct vop_getattr_args *)v)->a_vap;
    407 
    408 	struct tmpfs_node *node;
    409 
    410 	node = VP_TO_TMPFS_NODE(vp);
    411 
    412 	VATTR_NULL(vap);
    413 
    414 	tmpfs_itimes(vp, NULL, NULL);
    415 
    416 	vap->va_type = vp->v_type;
    417 	vap->va_mode = node->tn_mode;
    418 	vap->va_nlink = node->tn_links;
    419 	vap->va_uid = node->tn_uid;
    420 	vap->va_gid = node->tn_gid;
    421 	vap->va_fsid = vp->v_mount->mnt_stat.f_fsidx.__fsid_val[0];
    422 	vap->va_fileid = node->tn_id;
    423 	vap->va_size = node->tn_size;
    424 	vap->va_blocksize = PAGE_SIZE;
    425 	vap->va_atime = node->tn_atime;
    426 	vap->va_mtime = node->tn_mtime;
    427 	vap->va_ctime = node->tn_ctime;
    428 	vap->va_birthtime = node->tn_birthtime;
    429 	vap->va_gen = node->tn_gen;
    430 	vap->va_flags = node->tn_flags;
    431 	vap->va_rdev = (vp->v_type == VBLK || vp->v_type == VCHR) ?
    432 		node->tn_spec.tn_dev.tn_rdev : VNOVAL;
    433 	vap->va_bytes = round_page(node->tn_size);
    434 	vap->va_filerev = VNOVAL;
    435 	vap->va_vaflags = 0;
    436 	vap->va_spare = VNOVAL; /* XXX */
    437 
    438 	return 0;
    439 }
    440 
    441 /* --------------------------------------------------------------------- */
    442 
    443 /* XXX Should this operation be atomic?  I think it should, but code in
    444  * XXX other places (e.g., ufs) doesn't seem to be... */
    445 int
    446 tmpfs_setattr(void *v)
    447 {
    448 	struct vnode *vp = ((struct vop_setattr_args *)v)->a_vp;
    449 	struct vattr *vap = ((struct vop_setattr_args *)v)->a_vap;
    450 	kauth_cred_t cred = ((struct vop_setattr_args *)v)->a_cred;
    451 	struct lwp *l = curlwp;
    452 
    453 	int error;
    454 
    455 	KASSERT(VOP_ISLOCKED(vp));
    456 
    457 	error = 0;
    458 
    459 	/* Abort if any unsettable attribute is given. */
    460 	if (vap->va_type != VNON ||
    461 	    vap->va_nlink != VNOVAL ||
    462 	    vap->va_fsid != VNOVAL ||
    463 	    vap->va_fileid != VNOVAL ||
    464 	    vap->va_blocksize != VNOVAL ||
    465 	    vap->va_ctime.tv_sec != VNOVAL ||
    466 	    vap->va_ctime.tv_nsec != VNOVAL ||
    467 	    vap->va_birthtime.tv_sec != VNOVAL ||
    468 	    vap->va_birthtime.tv_nsec != VNOVAL ||
    469 	    vap->va_gen != VNOVAL ||
    470 	    vap->va_rdev != VNOVAL ||
    471 	    vap->va_bytes != VNOVAL)
    472 		error = EINVAL;
    473 
    474 	if (error == 0 && (vap->va_flags != VNOVAL))
    475 		error = tmpfs_chflags(vp, vap->va_flags, cred, l);
    476 
    477 	if (error == 0 && (vap->va_size != VNOVAL))
    478 		error = tmpfs_chsize(vp, vap->va_size, cred, l);
    479 
    480 	if (error == 0 && (vap->va_uid != VNOVAL || vap->va_gid != VNOVAL))
    481 		error = tmpfs_chown(vp, vap->va_uid, vap->va_gid, cred, l);
    482 
    483 	if (error == 0 && (vap->va_mode != VNOVAL))
    484 		error = tmpfs_chmod(vp, vap->va_mode, cred, l);
    485 
    486 	if (error == 0 && ((vap->va_atime.tv_sec != VNOVAL &&
    487 	    vap->va_atime.tv_nsec != VNOVAL) ||
    488 	    (vap->va_mtime.tv_sec != VNOVAL &&
    489 	    vap->va_mtime.tv_nsec != VNOVAL)))
    490 		error = tmpfs_chtimes(vp, &vap->va_atime, &vap->va_mtime,
    491 		    vap->va_vaflags, cred, l);
    492 
    493 	/* Update the node times.  We give preference to the error codes
    494 	 * generated by this function rather than the ones that may arise
    495 	 * from tmpfs_update. */
    496 	tmpfs_update(vp, NULL, NULL, 0);
    497 
    498 	KASSERT(VOP_ISLOCKED(vp));
    499 
    500 	return error;
    501 }
    502 
    503 /* --------------------------------------------------------------------- */
    504 
    505 int
    506 tmpfs_read(void *v)
    507 {
    508 	struct vnode *vp = ((struct vop_read_args *)v)->a_vp;
    509 	struct uio *uio = ((struct vop_read_args *)v)->a_uio;
    510 
    511 	int error;
    512 	int flags;
    513 	struct tmpfs_node *node;
    514 	struct uvm_object *uobj;
    515 
    516 	KASSERT(VOP_ISLOCKED(vp));
    517 
    518 	node = VP_TO_TMPFS_NODE(vp);
    519 
    520 	if (vp->v_type != VREG) {
    521 		error = EISDIR;
    522 		goto out;
    523 	}
    524 
    525 	if (uio->uio_offset < 0) {
    526 		error = EINVAL;
    527 		goto out;
    528 	}
    529 
    530 	node->tn_status |= TMPFS_NODE_ACCESSED;
    531 
    532 	uobj = node->tn_spec.tn_reg.tn_aobj;
    533 	flags = UBC_WANT_UNMAP(vp) ? UBC_UNMAP : 0;
    534 	error = 0;
    535 	while (error == 0 && uio->uio_resid > 0) {
    536 		vsize_t len;
    537 		void *win;
    538 
    539 		if (node->tn_size <= uio->uio_offset)
    540 			break;
    541 
    542 		len = MIN(node->tn_size - uio->uio_offset, uio->uio_resid);
    543 		if (len == 0)
    544 			break;
    545 
    546 		win = ubc_alloc(uobj, uio->uio_offset, &len, UVM_ADV_NORMAL,
    547 		    UBC_READ);
    548 		error = uiomove(win, len, uio);
    549 		ubc_release(win, flags);
    550 	}
    551 
    552 out:
    553 	KASSERT(VOP_ISLOCKED(vp));
    554 
    555 	return error;
    556 }
    557 
    558 /* --------------------------------------------------------------------- */
    559 
    560 int
    561 tmpfs_write(void *v)
    562 {
    563 	struct vnode *vp = ((struct vop_write_args *)v)->a_vp;
    564 	struct uio *uio = ((struct vop_write_args *)v)->a_uio;
    565 	int ioflag = ((struct vop_write_args *)v)->a_ioflag;
    566 
    567 	bool extended;
    568 	int error;
    569 	int flags;
    570 	off_t oldsize;
    571 	struct tmpfs_node *node;
    572 	struct uvm_object *uobj;
    573 
    574 	KASSERT(VOP_ISLOCKED(vp));
    575 
    576 	node = VP_TO_TMPFS_NODE(vp);
    577 	oldsize = node->tn_size;
    578 
    579 	if (uio->uio_offset < 0 || vp->v_type != VREG) {
    580 		error = EINVAL;
    581 		goto out;
    582 	}
    583 
    584 	if (uio->uio_resid == 0) {
    585 		error = 0;
    586 		goto out;
    587 	}
    588 
    589 	if (ioflag & IO_APPEND)
    590 		uio->uio_offset = node->tn_size;
    591 
    592 	extended = uio->uio_offset + uio->uio_resid > node->tn_size;
    593 	if (extended) {
    594 		error = tmpfs_reg_resize(vp, uio->uio_offset + uio->uio_resid);
    595 		if (error != 0)
    596 			goto out;
    597 	}
    598 
    599 	uobj = node->tn_spec.tn_reg.tn_aobj;
    600 	flags = UBC_WANT_UNMAP(vp) ? UBC_UNMAP : 0;
    601 	error = 0;
    602 	while (error == 0 && uio->uio_resid > 0) {
    603 		vsize_t len;
    604 		void *win;
    605 
    606 		len = MIN(node->tn_size - uio->uio_offset, uio->uio_resid);
    607 		if (len == 0)
    608 			break;
    609 
    610 		win = ubc_alloc(uobj, uio->uio_offset, &len, UVM_ADV_NORMAL,
    611 		    UBC_WRITE);
    612 		error = uiomove(win, len, uio);
    613 		ubc_release(win, flags);
    614 	}
    615 
    616 	node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED |
    617 	    (extended ? TMPFS_NODE_CHANGED : 0);
    618 
    619 	if (error != 0)
    620 		(void)tmpfs_reg_resize(vp, oldsize);
    621 
    622 	VN_KNOTE(vp, NOTE_WRITE);
    623 
    624 out:
    625 	KASSERT(VOP_ISLOCKED(vp));
    626 	KASSERT(IMPLIES(error == 0, uio->uio_resid == 0));
    627 	KASSERT(IMPLIES(error != 0, oldsize == node->tn_size));
    628 
    629 	return error;
    630 }
    631 
    632 /* --------------------------------------------------------------------- */
    633 
    634 int
    635 tmpfs_fsync(void *v)
    636 {
    637 	struct vnode *vp = ((struct vop_fsync_args *)v)->a_vp;
    638 
    639 	KASSERT(VOP_ISLOCKED(vp));
    640 
    641 	tmpfs_update(vp, NULL, NULL, 0);
    642 
    643 	return 0;
    644 }
    645 
    646 /* --------------------------------------------------------------------- */
    647 
    648 int
    649 tmpfs_remove(void *v)
    650 {
    651 	struct vnode *dvp = ((struct vop_remove_args *)v)->a_dvp;
    652 	struct vnode *vp = ((struct vop_remove_args *)v)->a_vp;
    653 	struct componentname *cnp = (((struct vop_remove_args *)v)->a_cnp);
    654 
    655 	int error;
    656 	struct tmpfs_dirent *de;
    657 	struct tmpfs_mount *tmp;
    658 	struct tmpfs_node *dnode;
    659 	struct tmpfs_node *node;
    660 
    661 	KASSERT(VOP_ISLOCKED(dvp));
    662 	KASSERT(VOP_ISLOCKED(vp));
    663 
    664 	if (vp->v_type == VDIR) {
    665 		error = EPERM;
    666 		goto out;
    667 	}
    668 
    669 	dnode = VP_TO_TMPFS_DIR(dvp);
    670 	node = VP_TO_TMPFS_NODE(vp);
    671 	tmp = VFS_TO_TMPFS(vp->v_mount);
    672 	de = tmpfs_dir_lookup(dnode, cnp);
    673 	if (de == NULL) {
    674 		error = ENOENT;
    675 		goto out;
    676 	}
    677 	KASSERT(de->td_node == node);
    678 
    679 	/* Files marked as immutable or append-only cannot be deleted. */
    680 	if (node->tn_flags & (IMMUTABLE | APPEND)) {
    681 		error = EPERM;
    682 		goto out;
    683 	}
    684 
    685 	/* Remove the entry from the directory; as it is a file, we do not
    686 	 * have to change the number of hard links of the directory. */
    687 	tmpfs_dir_detach(dvp, de);
    688 
    689 	/* Free the directory entry we just deleted.  Note that the node
    690 	 * referred by it will not be removed until the vnode is really
    691 	 * reclaimed. */
    692 	tmpfs_free_dirent(tmp, de, true);
    693 
    694 	error = 0;
    695 
    696 out:
    697 	vput(vp);
    698 	if (dvp == vp)
    699 		vrele(dvp);
    700 	else
    701 		vput(dvp);
    702 
    703 	return error;
    704 }
    705 
    706 /* --------------------------------------------------------------------- */
    707 
    708 int
    709 tmpfs_link(void *v)
    710 {
    711 	struct vnode *dvp = ((struct vop_link_args *)v)->a_dvp;
    712 	struct vnode *vp = ((struct vop_link_args *)v)->a_vp;
    713 	struct componentname *cnp = ((struct vop_link_args *)v)->a_cnp;
    714 
    715 	int error;
    716 	struct tmpfs_dirent *de;
    717 	struct tmpfs_node *dnode;
    718 	struct tmpfs_node *node;
    719 
    720 	KASSERT(VOP_ISLOCKED(dvp));
    721 	KASSERT(cnp->cn_flags & HASBUF);
    722 	KASSERT(dvp != vp); /* XXX When can this be false? */
    723 
    724 	dnode = VP_TO_TMPFS_DIR(dvp);
    725 	node = VP_TO_TMPFS_NODE(vp);
    726 
    727 	/* Lock vp because we will need to run tmpfs_update over it, which
    728 	 * needs the vnode to be locked. */
    729 	error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    730 	if (error != 0)
    731 		goto out1;
    732 
    733 	/* XXX: Why aren't the following two tests done by the caller? */
    734 
    735 	/* Hard links of directories are forbidden. */
    736 	if (vp->v_type == VDIR) {
    737 		error = EPERM;
    738 		goto out;
    739 	}
    740 
    741 	/* Cannot create cross-device links. */
    742 	if (dvp->v_mount != vp->v_mount) {
    743 		error = EXDEV;
    744 		goto out;
    745 	}
    746 
    747 	/* Ensure that we do not overflow the maximum number of links imposed
    748 	 * by the system. */
    749 	KASSERT(node->tn_links <= LINK_MAX);
    750 	if (node->tn_links == LINK_MAX) {
    751 		error = EMLINK;
    752 		goto out;
    753 	}
    754 
    755 	/* We cannot create links of files marked immutable or append-only. */
    756 	if (node->tn_flags & (IMMUTABLE | APPEND)) {
    757 		error = EPERM;
    758 		goto out;
    759 	}
    760 
    761 	/* Allocate a new directory entry to represent the node. */
    762 	error = tmpfs_alloc_dirent(VFS_TO_TMPFS(vp->v_mount), node,
    763 	    cnp->cn_nameptr, cnp->cn_namelen, &de);
    764 	if (error != 0)
    765 		goto out;
    766 
    767 	/* Insert the new directory entry into the appropriate directory. */
    768 	tmpfs_dir_attach(dvp, de);
    769 
    770 	/* vp link count has changed, so update node times. */
    771 	node->tn_status |= TMPFS_NODE_CHANGED;
    772 	tmpfs_update(vp, NULL, NULL, 0);
    773 
    774 	error = 0;
    775 
    776 out:
    777 	VOP_UNLOCK(vp, 0);
    778 out1:
    779 	PNBUF_PUT(cnp->cn_pnbuf);
    780 
    781 	vput(dvp);
    782 
    783 	return error;
    784 }
    785 
    786 /* --------------------------------------------------------------------- */
    787 
    788 int
    789 tmpfs_rename(void *v)
    790 {
    791 	struct vnode *fdvp = ((struct vop_rename_args *)v)->a_fdvp;
    792 	struct vnode *fvp = ((struct vop_rename_args *)v)->a_fvp;
    793 	struct componentname *fcnp = ((struct vop_rename_args *)v)->a_fcnp;
    794 	struct vnode *tdvp = ((struct vop_rename_args *)v)->a_tdvp;
    795 	struct vnode *tvp = ((struct vop_rename_args *)v)->a_tvp;
    796 	struct componentname *tcnp = ((struct vop_rename_args *)v)->a_tcnp;
    797 
    798 	char *newname;
    799 	int error;
    800 	struct tmpfs_dirent *de, *de2;
    801 	struct tmpfs_mount *tmp;
    802 	struct tmpfs_node *fdnode;
    803 	struct tmpfs_node *fnode;
    804 	struct tmpfs_node *tnode;
    805 	struct tmpfs_node *tdnode;
    806 	size_t namelen;
    807 
    808 	KASSERT(VOP_ISLOCKED(tdvp));
    809 	KASSERT(IMPLIES(tvp != NULL, VOP_ISLOCKED(tvp) == LK_EXCLUSIVE));
    810 	KASSERT(fcnp->cn_flags & HASBUF);
    811 	KASSERT(tcnp->cn_flags & HASBUF);
    812 
    813 	/* Disallow cross-device renames.
    814 	 * XXX Why isn't this done by the caller? */
    815 	if (fvp->v_mount != tdvp->v_mount ||
    816 	    (tvp != NULL && fvp->v_mount != tvp->v_mount)) {
    817 		error = EXDEV;
    818 		goto out_unlocked;
    819 	}
    820 
    821 	fnode = VP_TO_TMPFS_NODE(fvp);
    822 	fdnode = VP_TO_TMPFS_DIR(fdvp);
    823 	tnode = (tvp == NULL) ? NULL : VP_TO_TMPFS_NODE(tvp);
    824 	tdnode = VP_TO_TMPFS_DIR(tdvp);
    825 	tmp = VFS_TO_TMPFS(tdvp->v_mount);
    826 	newname = NULL;
    827 	namelen = 0;
    828 
    829 	/* If we need to move the directory between entries, lock the
    830 	 * source so that we can safely operate on it. */
    831 
    832 	/* XXX: this is a potential locking order violation! */
    833 	if (fdnode != tdnode) {
    834 		error = vn_lock(fdvp, LK_EXCLUSIVE | LK_RETRY);
    835 		if (error != 0)
    836 			goto out_unlocked;
    837 	}
    838 
    839 	de = tmpfs_dir_lookup(fdnode, fcnp);
    840 	if (de == NULL) {
    841 		error = ENOENT;
    842 		goto out;
    843 	}
    844 	KASSERT(de->td_node == fnode);
    845 
    846 	/* If source and target are the same file, there is nothing to do. */
    847 	if (fvp == tvp) {
    848 		error = 0;
    849 		goto out;
    850 	}
    851 
    852 	/* Avoid manipulating '.' and '..' entries. */
    853 	if (de == NULL) {
    854 		KASSERT(fvp->v_type == VDIR);
    855 		error = EINVAL;
    856 		goto out;
    857 	}
    858 	KASSERT(de->td_node == fnode);
    859 
    860 	/* If replacing an existing entry, ensure we can do the operation. */
    861 	if (tvp != NULL) {
    862 		KASSERT(tnode != NULL);
    863 		if (fnode->tn_type == VDIR && tnode->tn_type == VDIR) {
    864 			if (tnode->tn_size > 0) {
    865 				error = ENOTEMPTY;
    866 				goto out;
    867 			}
    868 		} else if (fnode->tn_type == VDIR && tnode->tn_type != VDIR) {
    869 			error = ENOTDIR;
    870 			goto out;
    871 		} else if (fnode->tn_type != VDIR && tnode->tn_type == VDIR) {
    872 			error = EISDIR;
    873 			goto out;
    874 		} else {
    875 			KASSERT(fnode->tn_type != VDIR &&
    876 			        tnode->tn_type != VDIR);
    877 		}
    878 	}
    879 
    880 	/* Ensure that we have enough memory to hold the new name, if it
    881 	 * has to be changed. */
    882 	namelen = tcnp->cn_namelen;
    883 	if (fcnp->cn_namelen != tcnp->cn_namelen ||
    884 	    memcmp(fcnp->cn_nameptr, tcnp->cn_nameptr, fcnp->cn_namelen) != 0) {
    885 		newname = tmpfs_str_pool_get(&tmp->tm_str_pool, namelen, 0);
    886 		if (newname == NULL) {
    887 			error = ENOSPC;
    888 			goto out;
    889 		}
    890 	}
    891 
    892 	/* If the node is being moved to another directory, we have to do
    893 	 * the move. */
    894 	if (fdnode != tdnode) {
    895 		/* In case we are moving a directory, we have to adjust its
    896 		 * parent to point to the new parent. */
    897 		if (de->td_node->tn_type == VDIR) {
    898 			struct tmpfs_node *n;
    899 
    900 			/* Ensure the target directory is not a child of the
    901 			 * directory being moved.  Otherwise, we'd end up
    902 			 * with stale nodes. */
    903 			n = tdnode;
    904 			while (n != n->tn_spec.tn_dir.tn_parent) {
    905 				if (n == fnode) {
    906 					error = EINVAL;
    907 					goto out;
    908 				}
    909 				n = n->tn_spec.tn_dir.tn_parent;
    910 			}
    911 
    912 			/* Adjust the parent pointer. */
    913 			TMPFS_VALIDATE_DIR(fnode);
    914 			de->td_node->tn_spec.tn_dir.tn_parent = tdnode;
    915 
    916 			/* As a result of changing the target of the '..'
    917 			 * entry, the link count of the source and target
    918 			 * directories has to be adjusted. */
    919 			fdnode->tn_links--;
    920 			tdnode->tn_links++;
    921 		}
    922 
    923 		/* Do the move: just remove the entry from the source directory
    924 		 * and insert it into the target one. */
    925 		tmpfs_dir_detach(fdvp, de);
    926 		tmpfs_dir_attach(tdvp, de);
    927 
    928 		/* Notify listeners of fdvp about the change in the directory.
    929 		 * We can do it at this point because we aren't touching fdvp
    930 		 * any more below. */
    931 		VN_KNOTE(fdvp, NOTE_WRITE);
    932 	}
    933 
    934 	/* If we are overwriting an entry, we have to remove the old one
    935 	 * from the target directory. */
    936 	if (tvp != NULL) {
    937 		KASSERT(tnode != NULL);
    938 
    939 		/* Remove the old entry from the target directory.
    940 		 * Note! This relies on tmpfs_dir_attach() putting the new
    941 		 * node on the end of the target's node list. */
    942 		de2 = tmpfs_dir_lookup(tdnode, tcnp);
    943 		KASSERT(de2 != NULL);
    944 /* XXXREMOVEME */
    945 		if (de2 == de) {
    946 			panic("tmpfs_rename: to self 1");
    947 		}
    948 		if (de2->td_node == de->td_node) {
    949 			panic("tmpfs_rename: to self 2");
    950 		}
    951 		if (de2->td_node != tnode) {
    952 			panic("tmpfs_rename: found wrong entry [%s]",
    953 			    tcnp->cn_nameptr);
    954 		}
    955 /* XXXREMOVEME */
    956 		KASSERT(de2->td_node == tnode);
    957 		tmpfs_dir_detach(tdvp, de2);
    958 
    959 		/* Free the directory entry we just deleted.  Note that the
    960 		 * node referred by it will not be removed until the vnode is
    961 		 * really reclaimed. */
    962 		tmpfs_free_dirent(VFS_TO_TMPFS(tvp->v_mount), de2, true);
    963 	}
    964 
    965 	/* If the name has changed, we need to make it effective by changing
    966 	 * it in the directory entry. */
    967 	if (newname != NULL) {
    968 		KASSERT(tcnp->cn_namelen < MAXNAMLEN);
    969 		KASSERT(tcnp->cn_namelen < 0xffff);
    970 
    971 		tmpfs_str_pool_put(&tmp->tm_str_pool, de->td_name,
    972 		    de->td_namelen);
    973 		de->td_namelen = (uint16_t)namelen;
    974 		memcpy(newname, tcnp->cn_nameptr, namelen);
    975 		de->td_name = newname;
    976 		newname = NULL;
    977 
    978 		fnode->tn_status |= TMPFS_NODE_CHANGED;
    979 		tdnode->tn_status |= TMPFS_NODE_MODIFIED;
    980 	}
    981 
    982 	/* Notify listeners of tdvp about the change in the directory (either
    983 	 * because a new entry was added or because one was removed) and
    984 	 * listeners of fvp about the rename. */
    985 	VN_KNOTE(tdvp, NOTE_WRITE);
    986 	VN_KNOTE(fvp, NOTE_RENAME);
    987 
    988 	error = 0;
    989 
    990  out:
    991 	if (fdnode != tdnode)
    992 		VOP_UNLOCK(fdvp, 0);
    993 
    994  out_unlocked:
    995 	/* Release target nodes. */
    996 	if (tdvp == tvp)
    997 		vrele(tdvp);
    998 	else
    999 		vput(tdvp);
   1000 	if (tvp != NULL)
   1001 		vput(tvp);
   1002 
   1003 	/* Release source nodes. */
   1004 	vrele(fdvp);
   1005 	vrele(fvp);
   1006 
   1007 	if (newname != NULL)
   1008 		tmpfs_str_pool_put(&tmp->tm_str_pool, newname, namelen);
   1009 
   1010 	return error;
   1011 }
   1012 
   1013 /* --------------------------------------------------------------------- */
   1014 
   1015 int
   1016 tmpfs_mkdir(void *v)
   1017 {
   1018 	struct vnode *dvp = ((struct vop_mkdir_args *)v)->a_dvp;
   1019 	struct vnode **vpp = ((struct vop_mkdir_args *)v)->a_vpp;
   1020 	struct componentname *cnp = ((struct vop_mkdir_args *)v)->a_cnp;
   1021 	struct vattr *vap = ((struct vop_mkdir_args *)v)->a_vap;
   1022 
   1023 	KASSERT(vap->va_type == VDIR);
   1024 
   1025 	return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
   1026 }
   1027 
   1028 /* --------------------------------------------------------------------- */
   1029 
   1030 int
   1031 tmpfs_rmdir(void *v)
   1032 {
   1033 	struct vnode *dvp = ((struct vop_rmdir_args *)v)->a_dvp;
   1034 	struct vnode *vp = ((struct vop_rmdir_args *)v)->a_vp;
   1035 	struct componentname *cnp = ((struct vop_rmdir_args *)v)->a_cnp;
   1036 
   1037 	int error;
   1038 	struct tmpfs_dirent *de;
   1039 	struct tmpfs_mount *tmp;
   1040 	struct tmpfs_node *dnode;
   1041 	struct tmpfs_node *node;
   1042 
   1043 	KASSERT(VOP_ISLOCKED(dvp));
   1044 	KASSERT(VOP_ISLOCKED(vp));
   1045 
   1046 	tmp = VFS_TO_TMPFS(dvp->v_mount);
   1047 	dnode = VP_TO_TMPFS_DIR(dvp);
   1048 	node = VP_TO_TMPFS_DIR(vp);
   1049 	error = 0;
   1050 
   1051 	/* Directories with more than two entries ('.' and '..') cannot be
   1052 	 * removed. */
   1053 	if (node->tn_size > 0) {
   1054 		error = ENOTEMPTY;
   1055 		goto out;
   1056 	}
   1057 
   1058 	/* This invariant holds only if we are not trying to remove "..".
   1059 	 * We checked for that above so this is safe now. */
   1060 	KASSERT(node->tn_spec.tn_dir.tn_parent == dnode);
   1061 
   1062 	/* Get the directory entry associated with node (vp). */
   1063 	de = tmpfs_dir_lookup(dnode, cnp);
   1064 	if (de == NULL) {
   1065 		error = ENOENT;
   1066 		goto out;
   1067 	}
   1068 	KASSERT(de->td_node == node);
   1069 
   1070 	/* Check flags to see if we are allowed to remove the directory. */
   1071 	if (dnode->tn_flags & APPEND || node->tn_flags & (IMMUTABLE | APPEND)) {
   1072 		error = EPERM;
   1073 		goto out;
   1074 	}
   1075 
   1076 	/* Detach the directory entry from the directory (dnode). */
   1077 	tmpfs_dir_detach(dvp, de);
   1078 
   1079 	node->tn_links--;
   1080 	node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \
   1081 	    TMPFS_NODE_MODIFIED;
   1082 	node->tn_spec.tn_dir.tn_parent->tn_links--;
   1083 	node->tn_spec.tn_dir.tn_parent->tn_status |= TMPFS_NODE_ACCESSED | \
   1084 	    TMPFS_NODE_CHANGED | TMPFS_NODE_MODIFIED;
   1085 
   1086 	/* Release the parent. */
   1087 	cache_purge(dvp); /* XXX Is this needed? */
   1088 
   1089 	/* Free the directory entry we just deleted.  Note that the node
   1090 	 * referred by it will not be removed until the vnode is really
   1091 	 * reclaimed. */
   1092 	tmpfs_free_dirent(tmp, de, true);
   1093 
   1094 	KASSERT(node->tn_links == 0);
   1095  out:
   1096 	/* Release the nodes. */
   1097 	vput(dvp);
   1098 	vput(vp);
   1099 
   1100 	return error;
   1101 }
   1102 
   1103 /* --------------------------------------------------------------------- */
   1104 
   1105 int
   1106 tmpfs_symlink(void *v)
   1107 {
   1108 	struct vnode *dvp = ((struct vop_symlink_args *)v)->a_dvp;
   1109 	struct vnode **vpp = ((struct vop_symlink_args *)v)->a_vpp;
   1110 	struct componentname *cnp = ((struct vop_symlink_args *)v)->a_cnp;
   1111 	struct vattr *vap = ((struct vop_symlink_args *)v)->a_vap;
   1112 	char *target = ((struct vop_symlink_args *)v)->a_target;
   1113 
   1114 	KASSERT(vap->va_type == VLNK);
   1115 
   1116 	return tmpfs_alloc_file(dvp, vpp, vap, cnp, target);
   1117 }
   1118 
   1119 /* --------------------------------------------------------------------- */
   1120 
   1121 int
   1122 tmpfs_readdir(void *v)
   1123 {
   1124 	struct vnode *vp = ((struct vop_readdir_args *)v)->a_vp;
   1125 	struct uio *uio = ((struct vop_readdir_args *)v)->a_uio;
   1126 	int *eofflag = ((struct vop_readdir_args *)v)->a_eofflag;
   1127 	off_t **cookies = ((struct vop_readdir_args *)v)->a_cookies;
   1128 	int *ncookies = ((struct vop_readdir_args *)v)->a_ncookies;
   1129 
   1130 	int error;
   1131 	off_t startoff;
   1132 	off_t cnt;
   1133 	struct tmpfs_node *node;
   1134 
   1135 	KASSERT(VOP_ISLOCKED(vp));
   1136 
   1137 	/* This operation only makes sense on directory nodes. */
   1138 	if (vp->v_type != VDIR) {
   1139 		error = ENOTDIR;
   1140 		goto out;
   1141 	}
   1142 
   1143 	node = VP_TO_TMPFS_DIR(vp);
   1144 
   1145 	startoff = uio->uio_offset;
   1146 
   1147 	cnt = 0;
   1148 	if (uio->uio_offset == TMPFS_DIRCOOKIE_DOT) {
   1149 		error = tmpfs_dir_getdotdent(node, uio);
   1150 		if (error == -1) {
   1151 			error = 0;
   1152 			goto outok;
   1153 		} else if (error != 0)
   1154 			goto outok;
   1155 		cnt++;
   1156 	}
   1157 
   1158 	if (uio->uio_offset == TMPFS_DIRCOOKIE_DOTDOT) {
   1159 		error = tmpfs_dir_getdotdotdent(node, uio);
   1160 		if (error == -1) {
   1161 			error = 0;
   1162 			goto outok;
   1163 		} else if (error != 0)
   1164 			goto outok;
   1165 		cnt++;
   1166 	}
   1167 
   1168 	error = tmpfs_dir_getdents(node, uio, &cnt);
   1169 	if (error == -1)
   1170 		error = 0;
   1171 	KASSERT(error >= 0);
   1172 
   1173 outok:
   1174 	/* This label assumes that startoff has been
   1175 	 * initialized.  If the compiler didn't spit out warnings, we'd
   1176 	 * simply make this one be 'out' and drop 'outok'. */
   1177 
   1178 	if (eofflag != NULL)
   1179 		*eofflag =
   1180 		    (error == 0 && uio->uio_offset == TMPFS_DIRCOOKIE_EOF);
   1181 
   1182 	/* Update NFS-related variables. */
   1183 	if (error == 0 && cookies != NULL && ncookies != NULL) {
   1184 		off_t i;
   1185 		off_t off = startoff;
   1186 		struct tmpfs_dirent *de = NULL;
   1187 
   1188 		*ncookies = cnt;
   1189 		*cookies = malloc(cnt * sizeof(off_t), M_TEMP, M_WAITOK);
   1190 
   1191 		for (i = 0; i < cnt; i++) {
   1192 			KASSERT(off != TMPFS_DIRCOOKIE_EOF);
   1193 			if (off == TMPFS_DIRCOOKIE_DOT) {
   1194 				off = TMPFS_DIRCOOKIE_DOTDOT;
   1195 			} else {
   1196 				if (off == TMPFS_DIRCOOKIE_DOTDOT) {
   1197 					de = TAILQ_FIRST(&node->tn_spec.
   1198 					    tn_dir.tn_dir);
   1199 				} else if (de != NULL) {
   1200 					de = TAILQ_NEXT(de, td_entries);
   1201 				} else {
   1202 					de = tmpfs_dir_lookupbycookie(node,
   1203 					    off);
   1204 					KASSERT(de != NULL);
   1205 					de = TAILQ_NEXT(de, td_entries);
   1206 				}
   1207 				if (de == NULL) {
   1208 					off = TMPFS_DIRCOOKIE_EOF;
   1209 				} else {
   1210 					off = tmpfs_dircookie(de);
   1211 				}
   1212 			}
   1213 
   1214 			(*cookies)[i] = off;
   1215 		}
   1216 		KASSERT(uio->uio_offset == off);
   1217 	}
   1218 
   1219 out:
   1220 	KASSERT(VOP_ISLOCKED(vp));
   1221 
   1222 	return error;
   1223 }
   1224 
   1225 /* --------------------------------------------------------------------- */
   1226 
   1227 int
   1228 tmpfs_readlink(void *v)
   1229 {
   1230 	struct vnode *vp = ((struct vop_readlink_args *)v)->a_vp;
   1231 	struct uio *uio = ((struct vop_readlink_args *)v)->a_uio;
   1232 
   1233 	int error;
   1234 	struct tmpfs_node *node;
   1235 
   1236 	KASSERT(VOP_ISLOCKED(vp));
   1237 	KASSERT(uio->uio_offset == 0);
   1238 	KASSERT(vp->v_type == VLNK);
   1239 
   1240 	node = VP_TO_TMPFS_NODE(vp);
   1241 
   1242 	error = uiomove(node->tn_spec.tn_lnk.tn_link,
   1243 	    MIN(node->tn_size, uio->uio_resid), uio);
   1244 	node->tn_status |= TMPFS_NODE_ACCESSED;
   1245 
   1246 	KASSERT(VOP_ISLOCKED(vp));
   1247 
   1248 	return error;
   1249 }
   1250 
   1251 /* --------------------------------------------------------------------- */
   1252 
   1253 int
   1254 tmpfs_inactive(void *v)
   1255 {
   1256 	struct vnode *vp = ((struct vop_inactive_args *)v)->a_vp;
   1257 
   1258 	struct tmpfs_node *node;
   1259 
   1260 	KASSERT(VOP_ISLOCKED(vp));
   1261 
   1262 	node = VP_TO_TMPFS_NODE(vp);
   1263 	*((struct vop_inactive_args *)v)->a_recycle = (node->tn_links == 0);
   1264 	VOP_UNLOCK(vp, 0);
   1265 
   1266 	return 0;
   1267 }
   1268 
   1269 /* --------------------------------------------------------------------- */
   1270 
   1271 int
   1272 tmpfs_reclaim(void *v)
   1273 {
   1274 	struct vnode *vp = ((struct vop_reclaim_args *)v)->a_vp;
   1275 
   1276 	struct tmpfs_mount *tmp;
   1277 	struct tmpfs_node *node;
   1278 
   1279 	node = VP_TO_TMPFS_NODE(vp);
   1280 	tmp = VFS_TO_TMPFS(vp->v_mount);
   1281 
   1282 	cache_purge(vp);
   1283 	tmpfs_free_vp(vp);
   1284 
   1285 	/* If the node referenced by this vnode was deleted by the user,
   1286 	 * we must free its associated data structures (now that the vnode
   1287 	 * is being reclaimed). */
   1288 	if (node->tn_links == 0)
   1289 		tmpfs_free_node(tmp, node);
   1290 
   1291 	KASSERT(vp->v_data == NULL);
   1292 
   1293 	return 0;
   1294 }
   1295 
   1296 /* --------------------------------------------------------------------- */
   1297 
   1298 int
   1299 tmpfs_print(void *v)
   1300 {
   1301 	struct vnode *vp = ((struct vop_print_args *)v)->a_vp;
   1302 
   1303 	struct tmpfs_node *node;
   1304 
   1305 	node = VP_TO_TMPFS_NODE(vp);
   1306 
   1307 	printf("tag VT_TMPFS, tmpfs_node %p, flags 0x%x, links %d\n",
   1308 	    node, node->tn_flags, node->tn_links);
   1309 	printf("\tmode 0%o, owner %d, group %d, size %" PRIdMAX
   1310 	    ", status 0x%x\n",
   1311 	    node->tn_mode, node->tn_uid, node->tn_gid,
   1312 	    (uintmax_t)node->tn_size, node->tn_status);
   1313 
   1314 	if (vp->v_type == VFIFO)
   1315 		fifo_printinfo(vp);
   1316 	lockmgr_printinfo(&vp->v_lock);
   1317 
   1318 	printf("\n");
   1319 
   1320 	return 0;
   1321 }
   1322 
   1323 /* --------------------------------------------------------------------- */
   1324 
   1325 int
   1326 tmpfs_pathconf(void *v)
   1327 {
   1328 	int name = ((struct vop_pathconf_args *)v)->a_name;
   1329 	register_t *retval = ((struct vop_pathconf_args *)v)->a_retval;
   1330 
   1331 	int error;
   1332 
   1333 	error = 0;
   1334 
   1335 	switch (name) {
   1336 	case _PC_LINK_MAX:
   1337 		*retval = LINK_MAX;
   1338 		break;
   1339 
   1340 	case _PC_NAME_MAX:
   1341 		*retval = NAME_MAX;
   1342 		break;
   1343 
   1344 	case _PC_PATH_MAX:
   1345 		*retval = PATH_MAX;
   1346 		break;
   1347 
   1348 	case _PC_PIPE_BUF:
   1349 		*retval = PIPE_BUF;
   1350 		break;
   1351 
   1352 	case _PC_CHOWN_RESTRICTED:
   1353 		*retval = 1;
   1354 		break;
   1355 
   1356 	case _PC_NO_TRUNC:
   1357 		*retval = 1;
   1358 		break;
   1359 
   1360 	case _PC_SYNC_IO:
   1361 		*retval = 1;
   1362 		break;
   1363 
   1364 	case _PC_FILESIZEBITS:
   1365 		*retval = 0; /* XXX Don't know which value should I return. */
   1366 		break;
   1367 
   1368 	default:
   1369 		error = EINVAL;
   1370 	}
   1371 
   1372 	return error;
   1373 }
   1374 
   1375 /* --------------------------------------------------------------------- */
   1376 
   1377 int
   1378 tmpfs_advlock(void *v)
   1379 {
   1380 	struct vnode *vp = ((struct vop_advlock_args *)v)->a_vp;
   1381 
   1382 	struct tmpfs_node *node;
   1383 
   1384 	node = VP_TO_TMPFS_NODE(vp);
   1385 
   1386 	return lf_advlock(v, &node->tn_lockf, node->tn_size);
   1387 }
   1388 
   1389 /* --------------------------------------------------------------------- */
   1390 
   1391 int
   1392 tmpfs_getpages(void *v)
   1393 {
   1394 	struct vnode *vp = ((struct vop_getpages_args *)v)->a_vp;
   1395 	voff_t offset = ((struct vop_getpages_args *)v)->a_offset;
   1396 	struct vm_page **m = ((struct vop_getpages_args *)v)->a_m;
   1397 	int *count = ((struct vop_getpages_args *)v)->a_count;
   1398 	int centeridx = ((struct vop_getpages_args *)v)->a_centeridx;
   1399 	vm_prot_t access_type = ((struct vop_getpages_args *)v)->a_access_type;
   1400 	int advice = ((struct vop_getpages_args *)v)->a_advice;
   1401 	int flags = ((struct vop_getpages_args *)v)->a_flags;
   1402 
   1403 	int error;
   1404 	int i;
   1405 	struct tmpfs_node *node;
   1406 	struct uvm_object *uobj;
   1407 	int npages = *count;
   1408 
   1409 	KASSERT(vp->v_type == VREG);
   1410 	KASSERT(mutex_owned(&vp->v_interlock));
   1411 
   1412 	node = VP_TO_TMPFS_NODE(vp);
   1413 	uobj = node->tn_spec.tn_reg.tn_aobj;
   1414 
   1415 	/* We currently don't rely on PGO_PASTEOF. */
   1416 
   1417 	if (vp->v_size <= offset + (centeridx << PAGE_SHIFT)) {
   1418 		if ((flags & PGO_LOCKED) == 0)
   1419 			mutex_exit(&vp->v_interlock);
   1420 		return EINVAL;
   1421 	}
   1422 
   1423 	if (vp->v_size < offset + (npages << PAGE_SHIFT)) {
   1424 		npages = (round_page(vp->v_size) - offset) >> PAGE_SHIFT;
   1425 	}
   1426 
   1427 	if ((flags & PGO_LOCKED) != 0)
   1428 		return EBUSY;
   1429 
   1430 	if ((flags & PGO_NOTIMESTAMP) == 0) {
   1431 		if ((vp->v_mount->mnt_flag & MNT_NOATIME) == 0)
   1432 			node->tn_status |= TMPFS_NODE_ACCESSED;
   1433 
   1434 		if ((access_type & VM_PROT_WRITE) != 0)
   1435 			node->tn_status |= TMPFS_NODE_MODIFIED;
   1436 	}
   1437 
   1438 	mutex_exit(&vp->v_interlock);
   1439 
   1440 	/*
   1441 	 * Make sure that the array on which we will store the
   1442 	 * gotten pages is clean.  Otherwise uao_get (pointed to by
   1443 	 * the pgo_get below) gets confused and does not return the
   1444 	 * appropriate pages.
   1445 	 *
   1446 	 * XXX This shall be revisited when kern/32166 is addressed
   1447 	 * because the loop to clean m[i] will most likely be redundant
   1448 	 * as well as the PGO_ALLPAGES flag.
   1449 	 */
   1450 	if (m != NULL)
   1451 		for (i = 0; i < npages; i++)
   1452 			m[i] = NULL;
   1453 	mutex_enter(&uobj->vmobjlock);
   1454 	error = (*uobj->pgops->pgo_get)(uobj, offset, m, &npages, centeridx,
   1455 	    access_type, advice, flags | PGO_ALLPAGES);
   1456 #if defined(DEBUG)
   1457 	{
   1458 		/* Make sure that all the pages we return are valid. */
   1459 		int dbgi;
   1460 		if (error == 0 && m != NULL)
   1461 			for (dbgi = 0; dbgi < npages; dbgi++)
   1462 				KASSERT(m[dbgi] != NULL);
   1463 	}
   1464 #endif
   1465 
   1466 	return error;
   1467 }
   1468 
   1469 /* --------------------------------------------------------------------- */
   1470 
   1471 int
   1472 tmpfs_putpages(void *v)
   1473 {
   1474 	struct vnode *vp = ((struct vop_putpages_args *)v)->a_vp;
   1475 	voff_t offlo = ((struct vop_putpages_args *)v)->a_offlo;
   1476 	voff_t offhi = ((struct vop_putpages_args *)v)->a_offhi;
   1477 	int flags = ((struct vop_putpages_args *)v)->a_flags;
   1478 
   1479 	int error;
   1480 	struct tmpfs_node *node;
   1481 	struct uvm_object *uobj;
   1482 
   1483 	KASSERT(mutex_owned(&vp->v_interlock));
   1484 
   1485 	node = VP_TO_TMPFS_NODE(vp);
   1486 
   1487 	if (vp->v_type != VREG) {
   1488 		mutex_exit(&vp->v_interlock);
   1489 		return 0;
   1490 	}
   1491 
   1492 	uobj = node->tn_spec.tn_reg.tn_aobj;
   1493 	mutex_exit(&vp->v_interlock);
   1494 
   1495 	mutex_enter(&uobj->vmobjlock);
   1496 	error = (*uobj->pgops->pgo_put)(uobj, offlo, offhi, flags);
   1497 
   1498 	/* XXX mtime */
   1499 
   1500 	return error;
   1501 }
   1502