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