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