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