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