Home | History | Annotate | Line # | Download | only in tmpfs
tmpfs_vnops.c revision 1.21
      1 /*	$NetBSD: tmpfs_vnops.c,v 1.21 2006/02/16 14:57:50 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.21 2006/02/16 14:57:50 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 
     60 #include <uvm/uvm.h>
     61 
     62 #include <miscfs/fifofs/fifo.h>
     63 #include <fs/tmpfs/tmpfs_vnops.h>
     64 #include <fs/tmpfs/tmpfs.h>
     65 
     66 /* --------------------------------------------------------------------- */
     67 
     68 /*
     69  * vnode operations vector used for files stored in a tmpfs file system.
     70  */
     71 int (**tmpfs_vnodeop_p)(void *);
     72 const struct vnodeopv_entry_desc tmpfs_vnodeop_entries[] = {
     73 	{ &vop_default_desc,		vn_default_error },
     74 	{ &vop_lookup_desc,		tmpfs_lookup },
     75 	{ &vop_create_desc,		tmpfs_create },
     76 	{ &vop_mknod_desc,		tmpfs_mknod },
     77 	{ &vop_open_desc,		tmpfs_open },
     78 	{ &vop_close_desc,		tmpfs_close },
     79 	{ &vop_access_desc,		tmpfs_access },
     80 	{ &vop_getattr_desc,		tmpfs_getattr },
     81 	{ &vop_setattr_desc,		tmpfs_setattr },
     82 	{ &vop_read_desc,		tmpfs_read },
     83 	{ &vop_write_desc,		tmpfs_write },
     84 	{ &vop_ioctl_desc,		tmpfs_ioctl },
     85 	{ &vop_fcntl_desc,		tmpfs_fcntl },
     86 	{ &vop_poll_desc,		tmpfs_poll },
     87 	{ &vop_kqfilter_desc,		tmpfs_kqfilter },
     88 	{ &vop_revoke_desc,		tmpfs_revoke },
     89 	{ &vop_mmap_desc,		tmpfs_mmap },
     90 	{ &vop_fsync_desc,		tmpfs_fsync },
     91 	{ &vop_seek_desc,		tmpfs_seek },
     92 	{ &vop_remove_desc,		tmpfs_remove },
     93 	{ &vop_link_desc,		tmpfs_link },
     94 	{ &vop_rename_desc,		tmpfs_rename },
     95 	{ &vop_mkdir_desc,		tmpfs_mkdir },
     96 	{ &vop_rmdir_desc,		tmpfs_rmdir },
     97 	{ &vop_symlink_desc,		tmpfs_symlink },
     98 	{ &vop_readdir_desc,		tmpfs_readdir },
     99 	{ &vop_readlink_desc,		tmpfs_readlink },
    100 	{ &vop_abortop_desc,		tmpfs_abortop },
    101 	{ &vop_inactive_desc,		tmpfs_inactive },
    102 	{ &vop_reclaim_desc,		tmpfs_reclaim },
    103 	{ &vop_lock_desc,		tmpfs_lock },
    104 	{ &vop_unlock_desc,		tmpfs_unlock },
    105 	{ &vop_bmap_desc,		tmpfs_bmap },
    106 	{ &vop_strategy_desc,		tmpfs_strategy },
    107 	{ &vop_print_desc,		tmpfs_print },
    108 	{ &vop_pathconf_desc,		tmpfs_pathconf },
    109 	{ &vop_islocked_desc,		tmpfs_islocked },
    110 	{ &vop_advlock_desc,		tmpfs_advlock },
    111 	{ &vop_lease_desc,		tmpfs_lease },
    112 	{ &vop_bwrite_desc,		tmpfs_bwrite },
    113 	{ &vop_getpages_desc,		tmpfs_getpages },
    114 	{ &vop_putpages_desc,		tmpfs_putpages },
    115 	{ NULL, NULL }
    116 };
    117 const struct vnodeopv_desc tmpfs_vnodeop_opv_desc =
    118 	{ &tmpfs_vnodeop_p, tmpfs_vnodeop_entries };
    119 
    120 /* --------------------------------------------------------------------- */
    121 
    122 int
    123 tmpfs_lookup(void *v)
    124 {
    125 	struct vnode *dvp = ((struct vop_lookup_args *)v)->a_dvp;
    126 	struct vnode **vpp = ((struct vop_lookup_args *)v)->a_vpp;
    127 	struct componentname *cnp = ((struct vop_lookup_args *)v)->a_cnp;
    128 
    129 	int error;
    130 	struct tmpfs_dirent *de;
    131 	struct tmpfs_node *dnode;
    132 
    133 	KASSERT(VOP_ISLOCKED(dvp));
    134 
    135 	dnode = VP_TO_TMPFS_DIR(dvp);
    136 	*vpp = NULL;
    137 
    138 	/* Check accessibility of requested node as a first step. */
    139 	error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred, cnp->cn_lwp);
    140 	if (error != 0)
    141 		goto out;
    142 
    143 	/* If requesting the last path component on a read-only file system
    144 	 * with a write operation, deny it. */
    145 	if ((cnp->cn_flags & ISLASTCN) &&
    146 	    (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
    147 	    (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
    148 		error = EROFS;
    149 		goto out;
    150 	}
    151 
    152 	/* Avoid doing a linear scan of the directory if the requested
    153 	 * directory/name couple is already in the cache. */
    154 	error = cache_lookup(dvp, vpp, cnp);
    155 	if (error >= 0)
    156 		goto out;
    157 
    158 	/* We cannot be requesting the parent directory of the root node. */
    159 	KASSERT(IMPLIES(dnode->tn_type == VDIR &&
    160 	    dnode->tn_spec.tn_dir.tn_parent == dnode,
    161 	    !(cnp->cn_flags & ISDOTDOT)));
    162 
    163 	if (cnp->cn_flags & ISDOTDOT) {
    164 		VOP_UNLOCK(dvp, 0);
    165 
    166 		/* Allocate a new vnode on the matching entry. */
    167 		error = tmpfs_alloc_vp(dvp->v_mount,
    168 		    dnode->tn_spec.tn_dir.tn_parent, vpp);
    169 
    170 		if (cnp->cn_flags & LOCKPARENT &&
    171 		    cnp->cn_flags & ISLASTCN) {
    172 			if (vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY) != 0)
    173 				cnp->cn_flags |= PDIRUNLOCK;
    174 		}
    175 		dnode->tn_spec.tn_dir.tn_parent->tn_lookup_dirent = NULL;
    176 	} else if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') {
    177 		VREF(dvp);
    178 		*vpp = dvp;
    179 		dnode->tn_lookup_dirent = NULL;
    180 		error = 0;
    181 	} else {
    182 		de = tmpfs_dir_lookup(dnode, cnp);
    183 		if (de == NULL) {
    184 			/* The entry was not found in the directory.
    185 			 * This is OK iff we are creating or renaming an
    186 			 * entry and are working on the last component of
    187 			 * the path name. */
    188 			if ((cnp->cn_flags & ISLASTCN) &&
    189 			    (cnp->cn_nameiop == CREATE || \
    190 			    cnp->cn_nameiop == RENAME)) {
    191 				error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred,
    192 				    cnp->cn_lwp);
    193 				if (error != 0)
    194 					goto out;
    195 
    196 				/* Keep the component name in the buffer for
    197 				 * future uses. */
    198 				cnp->cn_flags |= SAVENAME;
    199 
    200 				error = EJUSTRETURN;
    201 			} else
    202 				error = ENOENT;
    203 		} else {
    204 			struct tmpfs_node *tnode;
    205 
    206 			/* The entry was found, so get its associated
    207 			 * tmpfs_node. */
    208 			tnode = de->td_node;
    209 
    210 			/* If we are not at the last path component and
    211 			 * found a non-directory or non-link entry (which
    212 			 * may itself be pointing to a directory), raise
    213 			 * an error. */
    214 			if ((tnode->tn_type != VDIR &&
    215 			    tnode->tn_type != VLNK) &&
    216 			    !(cnp->cn_flags & ISLASTCN)) {
    217 				error = ENOTDIR;
    218 				goto out;
    219 			}
    220 
    221 			/* If we are deleting or renaming the entry, keep
    222 			 * track of its tmpfs_dirent so that it can be
    223 			 * easily deleted later. */
    224 			if ((cnp->cn_flags & ISLASTCN) &&
    225 			    (cnp->cn_nameiop == DELETE ||
    226 			    cnp->cn_nameiop == RENAME)) {
    227 				error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred,
    228 				    cnp->cn_lwp);
    229 				if (error != 0)
    230 					goto out;
    231 				/* TODO: Check sticky bit. */
    232 				tnode->tn_lookup_dirent = de;
    233 			}
    234 
    235 			/* Allocate a new vnode on the matching entry. */
    236 			error = tmpfs_alloc_vp(dvp->v_mount, tnode, vpp);
    237 
    238 			if (error == 0 && (!(cnp->cn_flags & LOCKPARENT) ||
    239 			    !(cnp->cn_flags & ISLASTCN)))
    240 				VOP_UNLOCK(dvp, 0);
    241 		}
    242 	}
    243 
    244 	/* Store the result of this lookup in the cache.  Avoid this if the
    245 	 * request was for creation, as it does not improve timings on
    246 	 * emprical tests. */
    247 	if ((cnp->cn_flags & MAKEENTRY) && cnp->cn_nameiop != CREATE)
    248 		cache_enter(dvp, *vpp, cnp);
    249 
    250 out:
    251 	/* If there were no errors, *vpp cannot be null and it must be
    252 	 * locked. */
    253 	KASSERT(IFF(error == 0, *vpp != NULL && VOP_ISLOCKED(*vpp)));
    254 
    255 	/* dvp has to be locked if:
    256 	 * - There were errors and relocking of dvp did not fail.
    257 	 * - We are doing a '..' lookup, relocking of dvp did not fail
    258 	 *   (PDIRUNLOCK is unset) and LOCKPARENT or ISLASTCN are not set.
    259 	 * - LOCKPARENT and ISLASTCN are set. */
    260 	KASSERT(IMPLIES(
    261 	    (error != 0 && !(cnp->cn_flags & PDIRUNLOCK)) ||
    262 	    (cnp->cn_flags & ISDOTDOT && !(cnp->cn_flags & PDIRUNLOCK) &&
    263 	     ((cnp->cn_flags & LOCKPARENT) && (cnp->cn_flags & ISLASTCN))) ||
    264 	    (cnp->cn_flags & LOCKPARENT && cnp->cn_flags & ISLASTCN)
    265 	    ,
    266 	    VOP_ISLOCKED(dvp)));
    267 
    268 	return error;
    269 }
    270 
    271 /* --------------------------------------------------------------------- */
    272 
    273 int
    274 tmpfs_create(void *v)
    275 {
    276 	struct vnode *dvp = ((struct vop_create_args *)v)->a_dvp;
    277 	struct vnode **vpp = ((struct vop_create_args *)v)->a_vpp;
    278 	struct componentname *cnp = ((struct vop_create_args *)v)->a_cnp;
    279 	struct vattr *vap = ((struct vop_create_args *)v)->a_vap;
    280 
    281 	KASSERT(vap->va_type == VREG || vap->va_type == VSOCK);
    282 
    283 	return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
    284 }
    285 /* --------------------------------------------------------------------- */
    286 
    287 int
    288 tmpfs_mknod(void *v)
    289 {
    290 	struct vnode *dvp = ((struct vop_mknod_args *)v)->a_dvp;
    291 	struct vnode **vpp = ((struct vop_mknod_args *)v)->a_vpp;
    292 	struct componentname *cnp = ((struct vop_mknod_args *)v)->a_cnp;
    293 	struct vattr *vap = ((struct vop_mknod_args *)v)->a_vap;
    294 
    295 	if (vap->va_type != VBLK && vap->va_type != VCHR &&
    296 	    vap->va_type != VFIFO)
    297 		return EINVAL;
    298 
    299 	return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
    300 }
    301 
    302 /* --------------------------------------------------------------------- */
    303 
    304 int
    305 tmpfs_open(void *v)
    306 {
    307 	struct vnode *vp = ((struct vop_open_args *)v)->a_vp;
    308 	int mode = ((struct vop_open_args *)v)->a_mode;
    309 
    310 	int error;
    311 	struct tmpfs_node *node;
    312 
    313 	KASSERT(VOP_ISLOCKED(vp));
    314 
    315 	node = VP_TO_TMPFS_NODE(vp);
    316 	KASSERT(node->tn_links > 0);
    317 
    318 	/* If the file is marked append-only, deny write requests. */
    319 	if (node->tn_flags & APPEND && (mode & (FWRITE | O_APPEND)) == FWRITE)
    320 		error = EPERM;
    321 	else
    322 		error = 0;
    323 
    324 	KASSERT(VOP_ISLOCKED(vp));
    325 
    326 	return error;
    327 }
    328 
    329 /* --------------------------------------------------------------------- */
    330 
    331 int
    332 tmpfs_close(void *v)
    333 {
    334 	struct vnode *vp = ((struct vop_close_args *)v)->a_vp;
    335 
    336 	struct tmpfs_node *node;
    337 
    338 	KASSERT(VOP_ISLOCKED(vp));
    339 
    340 	node = VP_TO_TMPFS_NODE(vp);
    341 
    342 	if (node->tn_links > 0) {
    343 		/* Update node times.  No need to do it if the node has
    344 		 * been deleted, because it will vanish after we return. */
    345 		tmpfs_update(vp, NULL, NULL, UPDATE_CLOSE);
    346 	}
    347 
    348 	return 0;
    349 }
    350 
    351 /* --------------------------------------------------------------------- */
    352 
    353 int
    354 tmpfs_access(void *v)
    355 {
    356 	struct vnode *vp = ((struct vop_access_args *)v)->a_vp;
    357 	int mode = ((struct vop_access_args *)v)->a_mode;
    358 	struct ucred *cred = ((struct vop_access_args *)v)->a_cred;
    359 
    360 	int error;
    361 	struct tmpfs_node *node;
    362 
    363 	KASSERT(VOP_ISLOCKED(vp));
    364 
    365 	node = VP_TO_TMPFS_NODE(vp);
    366 
    367 	switch (vp->v_type) {
    368 	case VDIR:
    369 		/* FALLTHROUGH */
    370 	case VLNK:
    371 		/* FALLTHROUGH */
    372 	case VREG:
    373 		if (mode & VWRITE && vp->v_mount->mnt_flag & MNT_RDONLY) {
    374 			error = EROFS;
    375 			goto out;
    376 		}
    377 		break;
    378 
    379 	case VBLK:
    380 		/* FALLTHROUGH */
    381 	case VCHR:
    382 		/* FALLTHROUGH */
    383 	case VSOCK:
    384 		/* FALLTHROUGH */
    385 	case VFIFO:
    386 		break;
    387 
    388 	default:
    389 		error = EINVAL;
    390 		goto out;
    391 	}
    392 
    393 	if (mode & VWRITE && node->tn_flags & IMMUTABLE) {
    394 		error = EPERM;
    395 		goto out;
    396 	}
    397 
    398 	error = vaccess(vp->v_type, node->tn_mode, node->tn_uid,
    399 	    node->tn_gid, mode, cred);
    400 
    401 out:
    402 	KASSERT(VOP_ISLOCKED(vp));
    403 
    404 	return error;
    405 }
    406 
    407 /* --------------------------------------------------------------------- */
    408 
    409 int
    410 tmpfs_getattr(void *v)
    411 {
    412 	struct vnode *vp = ((struct vop_getattr_args *)v)->a_vp;
    413 	struct vattr *vap = ((struct vop_getattr_args *)v)->a_vap;
    414 
    415 	struct tmpfs_node *node;
    416 
    417 	node = VP_TO_TMPFS_NODE(vp);
    418 
    419 	VATTR_NULL(vap);
    420 
    421 	tmpfs_itimes(vp, NULL, NULL);
    422 
    423 	vap->va_type = vp->v_type;
    424 	vap->va_mode = node->tn_mode;
    425 	vap->va_nlink = node->tn_links;
    426 	vap->va_uid = node->tn_uid;
    427 	vap->va_gid = node->tn_gid;
    428 	vap->va_fsid = vp->v_mount->mnt_stat.f_fsidx.__fsid_val[0];
    429 	vap->va_fileid = node->tn_id;
    430 	vap->va_size = node->tn_size;
    431 	vap->va_blocksize = PAGE_SIZE;
    432 	vap->va_atime = node->tn_atime;
    433 	vap->va_mtime = node->tn_mtime;
    434 	vap->va_ctime = node->tn_ctime;
    435 	vap->va_birthtime = node->tn_birthtime;
    436 	vap->va_gen = node->tn_gen;
    437 	vap->va_flags = node->tn_flags;
    438 	vap->va_rdev = (vp->v_type == VBLK || vp->v_type == VCHR) ?
    439 		node->tn_spec.tn_dev.tn_rdev : VNOVAL;
    440 	vap->va_bytes = round_page(node->tn_size);
    441 	vap->va_filerev = VNOVAL;
    442 	vap->va_vaflags = 0;
    443 	vap->va_spare = VNOVAL; /* XXX */
    444 
    445 	return 0;
    446 }
    447 
    448 /* --------------------------------------------------------------------- */
    449 
    450 /* XXX Should this operation be atomic?  I think it should, but code in
    451  * XXX other places (e.g., ufs) doesn't seem to be... */
    452 int
    453 tmpfs_setattr(void *v)
    454 {
    455 	struct vnode *vp = ((struct vop_setattr_args *)v)->a_vp;
    456 	struct vattr *vap = ((struct vop_setattr_args *)v)->a_vap;
    457 	struct ucred *cred = ((struct vop_setattr_args *)v)->a_cred;
    458 	struct lwp *l = ((struct vop_setattr_args *)v)->a_l;
    459 
    460 	int error;
    461 
    462 	KASSERT(VOP_ISLOCKED(vp));
    463 
    464 	error = 0;
    465 
    466 	/* Abort if any unsettable attribute is given. */
    467 	if (vap->va_type != VNON ||
    468 	    vap->va_nlink != VNOVAL ||
    469 	    vap->va_fsid != VNOVAL ||
    470 	    vap->va_fileid != VNOVAL ||
    471 	    vap->va_blocksize != VNOVAL ||
    472 	    vap->va_ctime.tv_sec != VNOVAL ||
    473 	    vap->va_ctime.tv_nsec != VNOVAL ||
    474 	    vap->va_birthtime.tv_sec != VNOVAL ||
    475 	    vap->va_birthtime.tv_nsec != VNOVAL ||
    476 	    vap->va_gen != VNOVAL ||
    477 	    vap->va_rdev != VNOVAL ||
    478 	    vap->va_bytes != VNOVAL)
    479 		error = EINVAL;
    480 
    481 	if (error == 0 && (vap->va_flags != VNOVAL))
    482 		error = tmpfs_chflags(vp, vap->va_flags, cred, l->l_proc);
    483 
    484 	if (error == 0 && (vap->va_size != VNOVAL))
    485 		error = tmpfs_chsize(vp, vap->va_size, cred, l->l_proc);
    486 
    487 	if (error == 0 && (vap->va_uid != VNOVAL || vap->va_gid != VNOVAL))
    488 		error = tmpfs_chown(vp, vap->va_uid, vap->va_gid, cred,
    489 		    l->l_proc);
    490 
    491 	if (error == 0 && (vap->va_mode != VNOVAL))
    492 		error = tmpfs_chmod(vp, vap->va_mode, cred, l->l_proc);
    493 
    494 	if (error == 0 && ((vap->va_atime.tv_sec != VNOVAL &&
    495 	    vap->va_atime.tv_nsec != VNOVAL) ||
    496 	    (vap->va_mtime.tv_sec != VNOVAL &&
    497 	    vap->va_mtime.tv_nsec != VNOVAL)))
    498 		error = tmpfs_chtimes(vp, &vap->va_atime, &vap->va_mtime,
    499 		    vap->va_vaflags, cred, l);
    500 
    501 	/* Update the node times.  We give preference to the error codes
    502 	 * generated by this function rather than the ones that may arise
    503 	 * from tmpfs_update. */
    504 	tmpfs_update(vp, NULL, NULL, 0);
    505 
    506 	KASSERT(VOP_ISLOCKED(vp));
    507 
    508 	return error;
    509 }
    510 
    511 /* --------------------------------------------------------------------- */
    512 
    513 int
    514 tmpfs_read(void *v)
    515 {
    516 	struct vnode *vp = ((struct vop_read_args *)v)->a_vp;
    517 	struct uio *uio = ((struct vop_read_args *)v)->a_uio;
    518 
    519 	int error;
    520 	int flags;
    521 	struct tmpfs_node *node;
    522 	struct uvm_object *uobj;
    523 
    524 	KASSERT(VOP_ISLOCKED(vp));
    525 
    526 	node = VP_TO_TMPFS_NODE(vp);
    527 
    528 	if (vp->v_type != VREG) {
    529 		error = EISDIR;
    530 		goto out;
    531 	}
    532 
    533 	if (uio->uio_offset < 0) {
    534 		error = EINVAL;
    535 		goto out;
    536 	}
    537 
    538 	node->tn_status |= TMPFS_NODE_ACCESSED;
    539 
    540 	uobj = node->tn_spec.tn_reg.tn_aobj;
    541 	flags = UBC_WANT_UNMAP(vp) ? UBC_UNMAP : 0;
    542 	error = 0;
    543 	while (error == 0 && uio->uio_resid > 0) {
    544 		vsize_t len;
    545 		void *win;
    546 
    547 		if (node->tn_size <= uio->uio_offset)
    548 			break;
    549 
    550 		len = MIN(node->tn_size - uio->uio_offset, uio->uio_resid);
    551 		if (len == 0)
    552 			break;
    553 
    554 		win = ubc_alloc(uobj, uio->uio_offset, &len, UVM_ADV_NORMAL,
    555 		    UBC_READ);
    556 		error = uiomove(win, len, uio);
    557 		ubc_release(win, flags);
    558 	}
    559 
    560 out:
    561 	KASSERT(VOP_ISLOCKED(vp));
    562 
    563 	return error;
    564 }
    565 
    566 /* --------------------------------------------------------------------- */
    567 
    568 int
    569 tmpfs_write(void *v)
    570 {
    571 	struct vnode *vp = ((struct vop_write_args *)v)->a_vp;
    572 	struct uio *uio = ((struct vop_write_args *)v)->a_uio;
    573 	int ioflag = ((struct vop_write_args *)v)->a_ioflag;
    574 
    575 	boolean_t extended;
    576 	int error;
    577 	int flags;
    578 	off_t oldsize;
    579 	struct tmpfs_node *node;
    580 	struct uvm_object *uobj;
    581 
    582 	KASSERT(VOP_ISLOCKED(vp));
    583 
    584 	node = VP_TO_TMPFS_NODE(vp);
    585 	oldsize = node->tn_size;
    586 
    587 	if (uio->uio_offset < 0 || vp->v_type != VREG) {
    588 		error = EINVAL;
    589 		goto out;
    590 	}
    591 
    592 	if (uio->uio_resid == 0) {
    593 		error = 0;
    594 		goto out;
    595 	}
    596 
    597 	if (ioflag & IO_APPEND)
    598 		uio->uio_offset = node->tn_size;
    599 
    600 	extended = uio->uio_offset + uio->uio_resid > node->tn_size;
    601 	if (extended) {
    602 		error = tmpfs_reg_resize(vp, uio->uio_offset + uio->uio_resid);
    603 		if (error != 0)
    604 			goto out;
    605 	}
    606 
    607 	uobj = node->tn_spec.tn_reg.tn_aobj;
    608 	flags = UBC_WANT_UNMAP(vp) ? UBC_UNMAP : 0;
    609 	error = 0;
    610 	while (error == 0 && uio->uio_resid > 0) {
    611 		vsize_t len;
    612 		void *win;
    613 
    614 		len = MIN(node->tn_size - uio->uio_offset, uio->uio_resid);
    615 		if (len == 0)
    616 			break;
    617 
    618 		win = ubc_alloc(uobj, uio->uio_offset, &len, UVM_ADV_NORMAL,
    619 		    UBC_WRITE);
    620 		error = uiomove(win, len, uio);
    621 		ubc_release(win, flags);
    622 	}
    623 
    624 	node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED |
    625 	    (extended ? TMPFS_NODE_CHANGED : 0);
    626 
    627 	if (error != 0)
    628 		(void)tmpfs_reg_resize(vp, oldsize);
    629 
    630 out:
    631 	KASSERT(VOP_ISLOCKED(vp));
    632 	KASSERT(IMPLIES(error == 0, uio->uio_resid == 0));
    633 	KASSERT(IMPLIES(error != 0, oldsize == node->tn_size));
    634 
    635 	return error;
    636 }
    637 
    638 /* --------------------------------------------------------------------- */
    639 
    640 int
    641 tmpfs_fsync(void *v)
    642 {
    643 	struct vnode *vp = ((struct vop_fsync_args *)v)->a_vp;
    644 
    645 	KASSERT(VOP_ISLOCKED(vp));
    646 
    647 	tmpfs_update(vp, NULL, NULL, 0);
    648 
    649 	return 0;
    650 }
    651 
    652 /* --------------------------------------------------------------------- */
    653 
    654 int
    655 tmpfs_remove(void *v)
    656 {
    657 	struct vnode *dvp = ((struct vop_remove_args *)v)->a_dvp;
    658 	struct vnode *vp = ((struct vop_remove_args *)v)->a_vp;
    659 
    660 	int error;
    661 	struct tmpfs_dirent *de;
    662 	struct tmpfs_mount *tmp;
    663 	struct tmpfs_node *dnode;
    664 	struct tmpfs_node *node;
    665 
    666 	KASSERT(VOP_ISLOCKED(dvp));
    667 	KASSERT(VOP_ISLOCKED(vp));
    668 
    669 	dnode = VP_TO_TMPFS_DIR(dvp);
    670 	node = VP_TO_TMPFS_NODE(vp);
    671 	tmp = VFS_TO_TMPFS(vp->v_mount);
    672 	de = node->tn_lookup_dirent;
    673 	KASSERT(de != NULL);
    674 
    675 	/* XXX: Why isn't this done by the caller? */
    676 	if (vp->v_type == VDIR) {
    677 		error = EISDIR;
    678 		goto out;
    679 	}
    680 
    681 	/* Files marked as immutable or append-only cannot be deleted. */
    682 	if (node->tn_flags & (IMMUTABLE | APPEND)) {
    683 		error = EPERM;
    684 		goto out;
    685 	}
    686 
    687 	/* Remove the entry from the directory; as it is a file, we do not
    688 	 * have to change the number of hard links of the directory. */
    689 	tmpfs_dir_detach(dvp, de);
    690 
    691 	/* Notify interested parties about the modification of dvp.
    692 	 * The removal of vp is notified when it is reclaimed. */
    693 	VN_KNOTE(dvp, NOTE_WRITE);
    694 
    695 	/* Free the directory entry we just deleted.  Note that the node
    696 	 * referred by it will not be removed until the vnode is really
    697 	 * reclaimed. */
    698 	tmpfs_free_dirent(tmp, de, TRUE);
    699 
    700 	error = 0;
    701 
    702 out:
    703 	vput(dvp);
    704 	vput(vp);
    705 
    706 	KASSERT(!VOP_ISLOCKED(dvp));
    707 
    708 	return error;
    709 }
    710 
    711 /* --------------------------------------------------------------------- */
    712 
    713 int
    714 tmpfs_link(void *v)
    715 {
    716 	struct vnode *dvp = ((struct vop_link_args *)v)->a_dvp;
    717 	struct vnode *vp = ((struct vop_link_args *)v)->a_vp;
    718 	struct componentname *cnp = ((struct vop_link_args *)v)->a_cnp;
    719 
    720 	int error;
    721 	struct tmpfs_dirent *de;
    722 	struct tmpfs_node *dnode;
    723 	struct tmpfs_node *node;
    724 
    725 	KASSERT(VOP_ISLOCKED(dvp));
    726 	KASSERT(!VOP_ISLOCKED(vp));
    727 	KASSERT(cnp->cn_flags & HASBUF);
    728 	KASSERT(dvp != vp); /* XXX When can this be false? */
    729 
    730 	dnode = VP_TO_TMPFS_DIR(dvp);
    731 	node = VP_TO_TMPFS_NODE(vp);
    732 
    733 	/* Lock vp because we will need to run tmpfs_update over it, which
    734 	 * needs the vnode to be locked. */
    735 	error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    736 	if (error != 0)
    737 		goto out;
    738 
    739 	/* XXX: Why aren't the following two tests done by the caller? */
    740 
    741 	/* Hard links of directories are forbidden. */
    742 	if (vp->v_type == VDIR) {
    743 		error = EPERM;
    744 		goto out;
    745 	}
    746 
    747 	/* Cannot create cross-device links. */
    748 	if (dvp->v_mount != vp->v_mount) {
    749 		error = EXDEV;
    750 		goto out;
    751 	}
    752 
    753 	/* Ensure that we do not overflow the maximum number of links imposed
    754 	 * by the system. */
    755 	KASSERT(node->tn_links <= LINK_MAX);
    756 	if (node->tn_links == LINK_MAX) {
    757 		error = EMLINK;
    758 		goto out;
    759 	}
    760 
    761 	/* We cannot create links of files marked immutable or append-only. */
    762 	if (node->tn_flags & (IMMUTABLE | APPEND)) {
    763 		error = EPERM;
    764 		goto out;
    765 	}
    766 
    767 	/* Allocate a new directory entry to represent the node. */
    768 	error = tmpfs_alloc_dirent(VFS_TO_TMPFS(vp->v_mount), node,
    769 	    cnp->cn_nameptr, cnp->cn_namelen, &de);
    770 	if (error != 0)
    771 		goto out;
    772 
    773 	/* Insert the new directory entry into the appropriate directory. */
    774 	tmpfs_dir_attach(dvp, de);
    775 	VN_KNOTE(dvp, NOTE_WRITE);
    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_MODIFIED;
    928 	}
    929 
    930 	/* If we are overwriting an entry, we have to remove the old one
    931 	 * from the target directory. */
    932 	if (tvp != NULL) {
    933 		struct tmpfs_node *tnode;
    934 
    935 		tnode = VP_TO_TMPFS_NODE(tvp);
    936 
    937 		/* The source node cannot be a directory in this case. */
    938 		KASSERT(fnode->tn_type != VDIR);
    939 
    940 		/* Remove the old entry from the target directory. */
    941 		de = tnode->tn_lookup_dirent;
    942 		tmpfs_dir_detach(tdvp, de);
    943 
    944 		/* Free the directory entry we just deleted.  Note that the
    945 		 * node referred by it will not be removed until the vnode is
    946 		 * really reclaimed. */
    947 		tmpfs_free_dirent(VFS_TO_TMPFS(tvp->v_mount), de, TRUE);
    948 	}
    949 
    950 	/* Notify listeners of tdvp about the change in the directory (either
    951 	 * because a new entry was added or because one was removed). */
    952 	VN_KNOTE(tdvp, NOTE_WRITE);
    953 
    954 	error = 0;
    955 
    956 out_locked:
    957 	if (fdnode != tdnode)
    958 		VOP_UNLOCK(fdvp, 0);
    959 
    960 out:
    961 	/* Release target nodes. */
    962 	/* XXX: I don't understand when tdvp can be the same as tvp, but
    963 	 * other code takes care of this... */
    964 	if (tdvp == tvp)
    965 		vrele(tdvp);
    966 	else
    967 		vput(tdvp);
    968 	if (tvp != NULL)
    969 		vput(tvp);
    970 
    971 	/* Release source nodes. */
    972 	vrele(fdvp);
    973 	vrele(fvp);
    974 
    975 	return error;
    976 }
    977 
    978 /* --------------------------------------------------------------------- */
    979 
    980 int
    981 tmpfs_mkdir(void *v)
    982 {
    983 	struct vnode *dvp = ((struct vop_mkdir_args *)v)->a_dvp;
    984 	struct vnode **vpp = ((struct vop_mkdir_args *)v)->a_vpp;
    985 	struct componentname *cnp = ((struct vop_mkdir_args *)v)->a_cnp;
    986 	struct vattr *vap = ((struct vop_mkdir_args *)v)->a_vap;
    987 
    988 	KASSERT(vap->va_type == VDIR);
    989 
    990 	return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
    991 }
    992 
    993 /* --------------------------------------------------------------------- */
    994 
    995 int
    996 tmpfs_rmdir(void *v)
    997 {
    998 	struct vnode *dvp = ((struct vop_rmdir_args *)v)->a_dvp;
    999 	struct vnode *vp = ((struct vop_rmdir_args *)v)->a_vp;
   1000 
   1001 	int error;
   1002 	struct tmpfs_dirent *de;
   1003 	struct tmpfs_mount *tmp;
   1004 	struct tmpfs_node *dnode;
   1005 	struct tmpfs_node *node;
   1006 
   1007 	KASSERT(VOP_ISLOCKED(dvp));
   1008 	KASSERT(VOP_ISLOCKED(vp));
   1009 
   1010 	tmp = VFS_TO_TMPFS(dvp->v_mount);
   1011 	dnode = VP_TO_TMPFS_DIR(dvp);
   1012 	node = VP_TO_TMPFS_DIR(vp);
   1013 	KASSERT(node->tn_spec.tn_dir.tn_parent == dnode);
   1014 
   1015 	/* Get the directory entry associated with node (vp).  This was
   1016 	 * filled by tmpfs_lookup while looking up the entry. */
   1017 	de = node->tn_lookup_dirent;
   1018 	KASSERT(TMPFS_DIRENT_MATCHES(de,
   1019 	    ((struct vop_rmdir_args *)v)->a_cnp->cn_nameptr,
   1020 	    ((struct vop_rmdir_args *)v)->a_cnp->cn_namelen));
   1021 
   1022 	/* Directories with more than two entries ('.' and '..') cannot be
   1023 	 * removed. */
   1024 	if (node->tn_size > 0) {
   1025 		error = ENOTEMPTY;
   1026 		goto out;
   1027 	}
   1028 
   1029 	/* Check flags to see if we are allowed to remove the directory. */
   1030 	if (dnode->tn_flags & APPEND || node->tn_flags & (IMMUTABLE | APPEND)) {
   1031 		error = EPERM;
   1032 		goto out;
   1033 	}
   1034 
   1035 	/* Detach the directory entry from the directory (dnode). */
   1036 	tmpfs_dir_detach(dvp, de);
   1037 
   1038 	node->tn_links--;
   1039 	node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \
   1040 	    TMPFS_NODE_MODIFIED;
   1041 	node->tn_spec.tn_dir.tn_parent->tn_links--;
   1042 	node->tn_spec.tn_dir.tn_parent->tn_status |= TMPFS_NODE_ACCESSED | \
   1043 	    TMPFS_NODE_CHANGED | TMPFS_NODE_MODIFIED;
   1044 
   1045 	/* Notify modification of parent directory and release it. */
   1046 	VN_KNOTE(dvp, NOTE_WRITE | NOTE_LINK);
   1047 	cache_purge(dvp); /* XXX Is this needed? */
   1048 	vput(dvp);
   1049 
   1050 	/* Free the directory entry we just deleted.  Note that the node
   1051 	 * referred by it will not be removed until the vnode is really
   1052 	 * reclaimed. */
   1053 	tmpfs_free_dirent(tmp, de, TRUE);
   1054 
   1055 	/* Release the deleted vnode (will destroy the node, notify
   1056 	 * interested parties and clean it from the cache). */
   1057 	vput(vp);
   1058 
   1059 	error = 0;
   1060 
   1061 out:
   1062 	if (error != 0) {
   1063 		vput(dvp);
   1064 		vput(vp);
   1065 	}
   1066 
   1067 	return error;
   1068 }
   1069 
   1070 /* --------------------------------------------------------------------- */
   1071 
   1072 int
   1073 tmpfs_symlink(void *v)
   1074 {
   1075 	struct vnode *dvp = ((struct vop_symlink_args *)v)->a_dvp;
   1076 	struct vnode **vpp = ((struct vop_symlink_args *)v)->a_vpp;
   1077 	struct componentname *cnp = ((struct vop_symlink_args *)v)->a_cnp;
   1078 	struct vattr *vap = ((struct vop_symlink_args *)v)->a_vap;
   1079 	char *target = ((struct vop_symlink_args *)v)->a_target;
   1080 
   1081 	KASSERT(vap->va_type == VLNK);
   1082 
   1083 	return tmpfs_alloc_file(dvp, vpp, vap, cnp, target);
   1084 }
   1085 
   1086 /* --------------------------------------------------------------------- */
   1087 
   1088 int
   1089 tmpfs_readdir(void *v)
   1090 {
   1091 	struct vnode *vp = ((struct vop_readdir_args *)v)->a_vp;
   1092 	struct uio *uio = ((struct vop_readdir_args *)v)->a_uio;
   1093 	int *eofflag = ((struct vop_readdir_args *)v)->a_eofflag;
   1094 	off_t **cookies = ((struct vop_readdir_args *)v)->a_cookies;
   1095 	int *ncookies = ((struct vop_readdir_args *)v)->a_ncookies;
   1096 
   1097 	int error;
   1098 	off_t startoff;
   1099 	off_t cnt;
   1100 	struct tmpfs_node *node;
   1101 
   1102 	KASSERT(VOP_ISLOCKED(vp));
   1103 
   1104 	/* This operation only makes sense on directory nodes. */
   1105 	if (vp->v_type != VDIR) {
   1106 		error = ENOTDIR;
   1107 		goto out;
   1108 	}
   1109 
   1110 	node = VP_TO_TMPFS_DIR(vp);
   1111 
   1112 	startoff = uio->uio_offset;
   1113 
   1114 	cnt = 0;
   1115 	if (uio->uio_offset == TMPFS_DIRCOOKIE_DOT) {
   1116 		error = tmpfs_dir_getdotdent(node, uio);
   1117 		if (error == -1) {
   1118 			error = 0;
   1119 			goto outok;
   1120 		} else if (error != 0)
   1121 			goto outok;
   1122 		cnt++;
   1123 	}
   1124 
   1125 	if (uio->uio_offset == TMPFS_DIRCOOKIE_DOTDOT) {
   1126 		error = tmpfs_dir_getdotdotdent(node, uio);
   1127 		if (error == -1) {
   1128 			error = 0;
   1129 			goto outok;
   1130 		} else if (error != 0)
   1131 			goto outok;
   1132 		cnt++;
   1133 	}
   1134 
   1135 	error = tmpfs_dir_getdents(node, uio, &cnt);
   1136 	if (error == -1)
   1137 		error = 0;
   1138 	KASSERT(error >= 0);
   1139 
   1140 outok:
   1141 	/* This label assumes that startoff has been
   1142 	 * initialized.  If the compiler didn't spit out warnings, we'd
   1143 	 * simply make this one be 'out' and drop 'outok'. */
   1144 
   1145 	if (eofflag != NULL)
   1146 		*eofflag =
   1147 		    (error == 0 && uio->uio_offset == TMPFS_DIRCOOKIE_EOF);
   1148 
   1149 	/* Update NFS-related variables. */
   1150 	if (error == 0 && cookies != NULL && ncookies != NULL) {
   1151 		off_t i;
   1152 		off_t off = startoff;
   1153 		struct tmpfs_dirent *de = NULL;
   1154 
   1155 		*ncookies = cnt;
   1156 		*cookies = malloc(cnt * sizeof(off_t), M_TEMP, M_WAITOK);
   1157 
   1158 		for (i = 0; i < cnt; i++) {
   1159 			KASSERT(off != TMPFS_DIRCOOKIE_EOF);
   1160 			if (off == TMPFS_DIRCOOKIE_DOT) {
   1161 				off = TMPFS_DIRCOOKIE_DOTDOT;
   1162 			} else {
   1163 				if (off == TMPFS_DIRCOOKIE_DOTDOT) {
   1164 					de = TAILQ_FIRST(&node->tn_spec.
   1165 					    tn_dir.tn_dir);
   1166 				} else if (de != NULL) {
   1167 					de = TAILQ_NEXT(de, td_entries);
   1168 				} else {
   1169 					de = tmpfs_dir_lookupbycookie(node,
   1170 					    off);
   1171 					KASSERT(de != NULL);
   1172 					de = TAILQ_NEXT(de, td_entries);
   1173 				}
   1174 				if (de == NULL) {
   1175 					off = TMPFS_DIRCOOKIE_EOF;
   1176 				} else {
   1177 					off = TMPFS_DIRCOOKIE(de);
   1178 				}
   1179 			}
   1180 
   1181 			(*cookies)[i] = off;
   1182 		}
   1183 		KASSERT(uio->uio_offset == off);
   1184 	}
   1185 
   1186 out:
   1187 	KASSERT(VOP_ISLOCKED(vp));
   1188 
   1189 	return error;
   1190 }
   1191 
   1192 /* --------------------------------------------------------------------- */
   1193 
   1194 int
   1195 tmpfs_readlink(void *v)
   1196 {
   1197 	struct vnode *vp = ((struct vop_readlink_args *)v)->a_vp;
   1198 	struct uio *uio = ((struct vop_readlink_args *)v)->a_uio;
   1199 
   1200 	int error;
   1201 	struct tmpfs_node *node;
   1202 
   1203 	KASSERT(VOP_ISLOCKED(vp));
   1204 	KASSERT(uio->uio_offset == 0);
   1205 	KASSERT(vp->v_type == VLNK);
   1206 
   1207 	node = VP_TO_TMPFS_NODE(vp);
   1208 
   1209 	error = uiomove(node->tn_spec.tn_lnk.tn_link,
   1210 	    MIN(node->tn_size, uio->uio_resid), uio);
   1211 	node->tn_status |= TMPFS_NODE_ACCESSED;
   1212 
   1213 	KASSERT(VOP_ISLOCKED(vp));
   1214 
   1215 	return error;
   1216 }
   1217 
   1218 /* --------------------------------------------------------------------- */
   1219 
   1220 int
   1221 tmpfs_inactive(void *v)
   1222 {
   1223 	struct vnode *vp = ((struct vop_inactive_args *)v)->a_vp;
   1224 	struct lwp *l = ((struct vop_inactive_args *)v)->a_l;
   1225 
   1226 	struct tmpfs_node *node;
   1227 
   1228 	KASSERT(VOP_ISLOCKED(vp));
   1229 
   1230 	node = VP_TO_TMPFS_NODE(vp);
   1231 
   1232 	VOP_UNLOCK(vp, 0);
   1233 
   1234 	if (node->tn_links == 0)
   1235 		vrecycle(vp, NULL, l);
   1236 
   1237 	return 0;
   1238 }
   1239 
   1240 /* --------------------------------------------------------------------- */
   1241 
   1242 int
   1243 tmpfs_reclaim(void *v)
   1244 {
   1245 	struct vnode *vp = ((struct vop_reclaim_args *)v)->a_vp;
   1246 
   1247 	struct tmpfs_mount *tmp;
   1248 	struct tmpfs_node *node;
   1249 
   1250 	KASSERT(!VOP_ISLOCKED(vp));
   1251 
   1252 	node = VP_TO_TMPFS_NODE(vp);
   1253 	tmp = VFS_TO_TMPFS(vp->v_mount);
   1254 
   1255 	if (node->tn_links == 0)
   1256 		VN_KNOTE(vp, NOTE_DELETE);
   1257 
   1258 	cache_purge(vp);
   1259 	tmpfs_free_vp(vp);
   1260 
   1261 	/* If the node referenced by this vnode was deleted by the user,
   1262 	 * we must free its associated data structures (now that the vnode
   1263 	 * is being reclaimed). */
   1264 	if (node->tn_links == 0)
   1265 		tmpfs_free_node(tmp, node);
   1266 
   1267 	KASSERT(!VOP_ISLOCKED(vp));
   1268 	KASSERT(vp->v_data == NULL);
   1269 
   1270 	return 0;
   1271 }
   1272 
   1273 /* --------------------------------------------------------------------- */
   1274 
   1275 int
   1276 tmpfs_print(void *v)
   1277 {
   1278 	struct vnode *vp = ((struct vop_print_args *)v)->a_vp;
   1279 
   1280 	struct tmpfs_node *node;
   1281 
   1282 	node = VP_TO_TMPFS_NODE(vp);
   1283 
   1284 	printf("tag VT_TMPFS, tmpfs_node %p, flags 0x%x, links %d\n",
   1285 	    node, node->tn_flags, node->tn_links);
   1286 	printf("\tmode 0%o, owner %d, group %d, size %" PRIdMAX
   1287 	    ", status 0x%x\n",
   1288 	    node->tn_mode, node->tn_uid, node->tn_gid,
   1289 	    (uintmax_t)node->tn_size, node->tn_status);
   1290 
   1291 	if (vp->v_type == VFIFO)
   1292 		fifo_printinfo(vp);
   1293 	lockmgr_printinfo(&vp->v_lock);
   1294 
   1295 	printf("\n");
   1296 
   1297 	return 0;
   1298 }
   1299 
   1300 /* --------------------------------------------------------------------- */
   1301 
   1302 int
   1303 tmpfs_pathconf(void *v)
   1304 {
   1305 	int name = ((struct vop_pathconf_args *)v)->a_name;
   1306 	register_t *retval = ((struct vop_pathconf_args *)v)->a_retval;
   1307 
   1308 	int error;
   1309 
   1310 	error = 0;
   1311 
   1312 	switch (name) {
   1313 	case _PC_LINK_MAX:
   1314 		*retval = LINK_MAX;
   1315 		break;
   1316 
   1317 	case _PC_NAME_MAX:
   1318 		*retval = NAME_MAX;
   1319 		break;
   1320 
   1321 	case _PC_PATH_MAX:
   1322 		*retval = PATH_MAX;
   1323 		break;
   1324 
   1325 	case _PC_PIPE_BUF:
   1326 		*retval = PIPE_BUF;
   1327 		break;
   1328 
   1329 	case _PC_CHOWN_RESTRICTED:
   1330 		*retval = 1;
   1331 		break;
   1332 
   1333 	case _PC_NO_TRUNC:
   1334 		*retval = 1;
   1335 		break;
   1336 
   1337 	case _PC_SYNC_IO:
   1338 		*retval = 1;
   1339 		break;
   1340 
   1341 	case _PC_FILESIZEBITS:
   1342 		*retval = 0; /* XXX Don't know which value should I return. */
   1343 		break;
   1344 
   1345 	default:
   1346 		error = EINVAL;
   1347 	}
   1348 
   1349 	return error;
   1350 }
   1351 
   1352 /* --------------------------------------------------------------------- */
   1353 
   1354 int
   1355 tmpfs_advlock(void *v)
   1356 {
   1357 	struct vnode *vp = ((struct vop_advlock_args *)v)->a_vp;
   1358 
   1359 	struct tmpfs_node *node;
   1360 
   1361 	node = VP_TO_TMPFS_NODE(vp);
   1362 
   1363 	return lf_advlock(v, &node->tn_lockf, node->tn_size);
   1364 }
   1365 
   1366 /* --------------------------------------------------------------------- */
   1367 
   1368 int
   1369 tmpfs_getpages(void *v)
   1370 {
   1371 	struct vnode *vp = ((struct vop_getpages_args *)v)->a_vp;
   1372 	voff_t offset = ((struct vop_getpages_args *)v)->a_offset;
   1373 	struct vm_page **m = ((struct vop_getpages_args *)v)->a_m;
   1374 	int *count = ((struct vop_getpages_args *)v)->a_count;
   1375 	int centeridx = ((struct vop_getpages_args *)v)->a_centeridx;
   1376 	vm_prot_t access_type = ((struct vop_getpages_args *)v)->a_access_type;
   1377 	int advice = ((struct vop_getpages_args *)v)->a_advice;
   1378 	int flags = ((struct vop_getpages_args *)v)->a_flags;
   1379 
   1380 	int error;
   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 	simple_lock(&uobj->vmobjlock);
   1417 	error = (*uobj->pgops->pgo_get)(uobj, offset, m, &npages, centeridx,
   1418 	    access_type, advice, flags);
   1419 
   1420 	return error;
   1421 }
   1422 
   1423 /* --------------------------------------------------------------------- */
   1424 
   1425 int
   1426 tmpfs_putpages(void *v)
   1427 {
   1428 	struct vnode *vp = ((struct vop_putpages_args *)v)->a_vp;
   1429 	voff_t offlo = ((struct vop_putpages_args *)v)->a_offlo;
   1430 	voff_t offhi = ((struct vop_putpages_args *)v)->a_offhi;
   1431 	int flags = ((struct vop_putpages_args *)v)->a_flags;
   1432 
   1433 	int error;
   1434 	struct tmpfs_node *node;
   1435 	struct uvm_object *uobj;
   1436 
   1437 	LOCK_ASSERT(simple_lock_held(&vp->v_interlock));
   1438 
   1439 	node = VP_TO_TMPFS_NODE(vp);
   1440 
   1441 	if (vp->v_type != VREG) {
   1442 		simple_unlock(&vp->v_interlock);
   1443 		return 0;
   1444 	}
   1445 
   1446 	uobj = node->tn_spec.tn_reg.tn_aobj;
   1447 	simple_unlock(&vp->v_interlock);
   1448 
   1449 	simple_lock(&uobj->vmobjlock);
   1450 	error = (*uobj->pgops->pgo_put)(uobj, offlo, offhi, flags);
   1451 
   1452 	/* XXX mtime */
   1453 
   1454 	return error;
   1455 }
   1456