Home | History | Annotate | Line # | Download | only in tmpfs
tmpfs_vnops.c revision 1.25
      1 /*	$NetBSD: tmpfs_vnops.c,v 1.25 2006/07/23 22:06:10 ad 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.25 2006/07/23 22:06:10 ad 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 	KASSERT(node->tn_links > 0);
    322 
    323 	/* If the file is marked append-only, deny write requests. */
    324 	if (node->tn_flags & APPEND && (mode & (FWRITE | O_APPEND)) == FWRITE)
    325 		error = EPERM;
    326 	else
    327 		error = 0;
    328 
    329 	KASSERT(VOP_ISLOCKED(vp));
    330 
    331 	return error;
    332 }
    333 
    334 /* --------------------------------------------------------------------- */
    335 
    336 int
    337 tmpfs_close(void *v)
    338 {
    339 	struct vnode *vp = ((struct vop_close_args *)v)->a_vp;
    340 
    341 	struct tmpfs_node *node;
    342 
    343 	KASSERT(VOP_ISLOCKED(vp));
    344 
    345 	node = VP_TO_TMPFS_NODE(vp);
    346 
    347 	if (node->tn_links > 0) {
    348 		/* Update node times.  No need to do it if the node has
    349 		 * been deleted, because it will vanish after we return. */
    350 		tmpfs_update(vp, NULL, NULL, UPDATE_CLOSE);
    351 	}
    352 
    353 	return 0;
    354 }
    355 
    356 /* --------------------------------------------------------------------- */
    357 
    358 int
    359 tmpfs_access(void *v)
    360 {
    361 	struct vnode *vp = ((struct vop_access_args *)v)->a_vp;
    362 	int mode = ((struct vop_access_args *)v)->a_mode;
    363 	kauth_cred_t cred = ((struct vop_access_args *)v)->a_cred;
    364 
    365 	int error;
    366 	struct tmpfs_node *node;
    367 
    368 	KASSERT(VOP_ISLOCKED(vp));
    369 
    370 	node = VP_TO_TMPFS_NODE(vp);
    371 
    372 	switch (vp->v_type) {
    373 	case VDIR:
    374 		/* FALLTHROUGH */
    375 	case VLNK:
    376 		/* FALLTHROUGH */
    377 	case VREG:
    378 		if (mode & VWRITE && vp->v_mount->mnt_flag & MNT_RDONLY) {
    379 			error = EROFS;
    380 			goto out;
    381 		}
    382 		break;
    383 
    384 	case VBLK:
    385 		/* FALLTHROUGH */
    386 	case VCHR:
    387 		/* FALLTHROUGH */
    388 	case VSOCK:
    389 		/* FALLTHROUGH */
    390 	case VFIFO:
    391 		break;
    392 
    393 	default:
    394 		error = EINVAL;
    395 		goto out;
    396 	}
    397 
    398 	if (mode & VWRITE && node->tn_flags & IMMUTABLE) {
    399 		error = EPERM;
    400 		goto out;
    401 	}
    402 
    403 	error = vaccess(vp->v_type, node->tn_mode, node->tn_uid,
    404 	    node->tn_gid, mode, cred);
    405 
    406 out:
    407 	KASSERT(VOP_ISLOCKED(vp));
    408 
    409 	return error;
    410 }
    411 
    412 /* --------------------------------------------------------------------- */
    413 
    414 int
    415 tmpfs_getattr(void *v)
    416 {
    417 	struct vnode *vp = ((struct vop_getattr_args *)v)->a_vp;
    418 	struct vattr *vap = ((struct vop_getattr_args *)v)->a_vap;
    419 
    420 	struct tmpfs_node *node;
    421 
    422 	node = VP_TO_TMPFS_NODE(vp);
    423 
    424 	VATTR_NULL(vap);
    425 
    426 	tmpfs_itimes(vp, NULL, NULL);
    427 
    428 	vap->va_type = vp->v_type;
    429 	vap->va_mode = node->tn_mode;
    430 	vap->va_nlink = node->tn_links;
    431 	vap->va_uid = node->tn_uid;
    432 	vap->va_gid = node->tn_gid;
    433 	vap->va_fsid = vp->v_mount->mnt_stat.f_fsidx.__fsid_val[0];
    434 	vap->va_fileid = node->tn_id;
    435 	vap->va_size = node->tn_size;
    436 	vap->va_blocksize = PAGE_SIZE;
    437 	vap->va_atime = node->tn_atime;
    438 	vap->va_mtime = node->tn_mtime;
    439 	vap->va_ctime = node->tn_ctime;
    440 	vap->va_birthtime = node->tn_birthtime;
    441 	vap->va_gen = node->tn_gen;
    442 	vap->va_flags = node->tn_flags;
    443 	vap->va_rdev = (vp->v_type == VBLK || vp->v_type == VCHR) ?
    444 		node->tn_spec.tn_dev.tn_rdev : VNOVAL;
    445 	vap->va_bytes = round_page(node->tn_size);
    446 	vap->va_filerev = VNOVAL;
    447 	vap->va_vaflags = 0;
    448 	vap->va_spare = VNOVAL; /* XXX */
    449 
    450 	return 0;
    451 }
    452 
    453 /* --------------------------------------------------------------------- */
    454 
    455 /* XXX Should this operation be atomic?  I think it should, but code in
    456  * XXX other places (e.g., ufs) doesn't seem to be... */
    457 int
    458 tmpfs_setattr(void *v)
    459 {
    460 	struct vnode *vp = ((struct vop_setattr_args *)v)->a_vp;
    461 	struct vattr *vap = ((struct vop_setattr_args *)v)->a_vap;
    462 	kauth_cred_t cred = ((struct vop_setattr_args *)v)->a_cred;
    463 	struct lwp *l = ((struct vop_setattr_args *)v)->a_l;
    464 
    465 	int error;
    466 
    467 	KASSERT(VOP_ISLOCKED(vp));
    468 
    469 	error = 0;
    470 
    471 	/* Abort if any unsettable attribute is given. */
    472 	if (vap->va_type != VNON ||
    473 	    vap->va_nlink != VNOVAL ||
    474 	    vap->va_fsid != VNOVAL ||
    475 	    vap->va_fileid != VNOVAL ||
    476 	    vap->va_blocksize != VNOVAL ||
    477 	    vap->va_ctime.tv_sec != VNOVAL ||
    478 	    vap->va_ctime.tv_nsec != VNOVAL ||
    479 	    vap->va_birthtime.tv_sec != VNOVAL ||
    480 	    vap->va_birthtime.tv_nsec != VNOVAL ||
    481 	    vap->va_gen != VNOVAL ||
    482 	    vap->va_rdev != VNOVAL ||
    483 	    vap->va_bytes != VNOVAL)
    484 		error = EINVAL;
    485 
    486 	if (error == 0 && (vap->va_flags != VNOVAL))
    487 		error = tmpfs_chflags(vp, vap->va_flags, cred, l);
    488 
    489 	if (error == 0 && (vap->va_size != VNOVAL))
    490 		error = tmpfs_chsize(vp, vap->va_size, cred, l);
    491 
    492 	if (error == 0 && (vap->va_uid != VNOVAL || vap->va_gid != VNOVAL))
    493 		error = tmpfs_chown(vp, vap->va_uid, vap->va_gid, cred, l);
    494 
    495 	if (error == 0 && (vap->va_mode != VNOVAL))
    496 		error = tmpfs_chmod(vp, vap->va_mode, cred, l);
    497 
    498 	if (error == 0 && ((vap->va_atime.tv_sec != VNOVAL &&
    499 	    vap->va_atime.tv_nsec != VNOVAL) ||
    500 	    (vap->va_mtime.tv_sec != VNOVAL &&
    501 	    vap->va_mtime.tv_nsec != VNOVAL)))
    502 		error = tmpfs_chtimes(vp, &vap->va_atime, &vap->va_mtime,
    503 		    vap->va_vaflags, cred, l);
    504 
    505 	/* Update the node times.  We give preference to the error codes
    506 	 * generated by this function rather than the ones that may arise
    507 	 * from tmpfs_update. */
    508 	tmpfs_update(vp, NULL, NULL, 0);
    509 
    510 	KASSERT(VOP_ISLOCKED(vp));
    511 
    512 	return error;
    513 }
    514 
    515 /* --------------------------------------------------------------------- */
    516 
    517 int
    518 tmpfs_read(void *v)
    519 {
    520 	struct vnode *vp = ((struct vop_read_args *)v)->a_vp;
    521 	struct uio *uio = ((struct vop_read_args *)v)->a_uio;
    522 
    523 	int error;
    524 	int flags;
    525 	struct tmpfs_node *node;
    526 	struct uvm_object *uobj;
    527 
    528 	KASSERT(VOP_ISLOCKED(vp));
    529 
    530 	node = VP_TO_TMPFS_NODE(vp);
    531 
    532 	if (vp->v_type != VREG) {
    533 		error = EISDIR;
    534 		goto out;
    535 	}
    536 
    537 	if (uio->uio_offset < 0) {
    538 		error = EINVAL;
    539 		goto out;
    540 	}
    541 
    542 	node->tn_status |= TMPFS_NODE_ACCESSED;
    543 
    544 	uobj = node->tn_spec.tn_reg.tn_aobj;
    545 	flags = UBC_WANT_UNMAP(vp) ? UBC_UNMAP : 0;
    546 	error = 0;
    547 	while (error == 0 && uio->uio_resid > 0) {
    548 		vsize_t len;
    549 		void *win;
    550 
    551 		if (node->tn_size <= uio->uio_offset)
    552 			break;
    553 
    554 		len = MIN(node->tn_size - uio->uio_offset, uio->uio_resid);
    555 		if (len == 0)
    556 			break;
    557 
    558 		win = ubc_alloc(uobj, uio->uio_offset, &len, UVM_ADV_NORMAL,
    559 		    UBC_READ);
    560 		error = uiomove(win, len, uio);
    561 		ubc_release(win, flags);
    562 	}
    563 
    564 out:
    565 	KASSERT(VOP_ISLOCKED(vp));
    566 
    567 	return error;
    568 }
    569 
    570 /* --------------------------------------------------------------------- */
    571 
    572 int
    573 tmpfs_write(void *v)
    574 {
    575 	struct vnode *vp = ((struct vop_write_args *)v)->a_vp;
    576 	struct uio *uio = ((struct vop_write_args *)v)->a_uio;
    577 	int ioflag = ((struct vop_write_args *)v)->a_ioflag;
    578 
    579 	boolean_t extended;
    580 	int error;
    581 	int flags;
    582 	off_t oldsize;
    583 	struct tmpfs_node *node;
    584 	struct uvm_object *uobj;
    585 
    586 	KASSERT(VOP_ISLOCKED(vp));
    587 
    588 	node = VP_TO_TMPFS_NODE(vp);
    589 	oldsize = node->tn_size;
    590 
    591 	if (uio->uio_offset < 0 || vp->v_type != VREG) {
    592 		error = EINVAL;
    593 		goto out;
    594 	}
    595 
    596 	if (uio->uio_resid == 0) {
    597 		error = 0;
    598 		goto out;
    599 	}
    600 
    601 	if (ioflag & IO_APPEND)
    602 		uio->uio_offset = node->tn_size;
    603 
    604 	extended = uio->uio_offset + uio->uio_resid > node->tn_size;
    605 	if (extended) {
    606 		error = tmpfs_reg_resize(vp, uio->uio_offset + uio->uio_resid);
    607 		if (error != 0)
    608 			goto out;
    609 	}
    610 
    611 	uobj = node->tn_spec.tn_reg.tn_aobj;
    612 	flags = UBC_WANT_UNMAP(vp) ? UBC_UNMAP : 0;
    613 	error = 0;
    614 	while (error == 0 && uio->uio_resid > 0) {
    615 		vsize_t len;
    616 		void *win;
    617 
    618 		len = MIN(node->tn_size - uio->uio_offset, uio->uio_resid);
    619 		if (len == 0)
    620 			break;
    621 
    622 		win = ubc_alloc(uobj, uio->uio_offset, &len, UVM_ADV_NORMAL,
    623 		    UBC_WRITE);
    624 		error = uiomove(win, len, uio);
    625 		ubc_release(win, flags);
    626 	}
    627 
    628 	node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED |
    629 	    (extended ? TMPFS_NODE_CHANGED : 0);
    630 
    631 	if (error != 0)
    632 		(void)tmpfs_reg_resize(vp, oldsize);
    633 
    634 out:
    635 	KASSERT(VOP_ISLOCKED(vp));
    636 	KASSERT(IMPLIES(error == 0, uio->uio_resid == 0));
    637 	KASSERT(IMPLIES(error != 0, oldsize == node->tn_size));
    638 
    639 	return error;
    640 }
    641 
    642 /* --------------------------------------------------------------------- */
    643 
    644 int
    645 tmpfs_fsync(void *v)
    646 {
    647 	struct vnode *vp = ((struct vop_fsync_args *)v)->a_vp;
    648 
    649 	KASSERT(VOP_ISLOCKED(vp));
    650 
    651 	tmpfs_update(vp, NULL, NULL, 0);
    652 
    653 	return 0;
    654 }
    655 
    656 /* --------------------------------------------------------------------- */
    657 
    658 int
    659 tmpfs_remove(void *v)
    660 {
    661 	struct vnode *dvp = ((struct vop_remove_args *)v)->a_dvp;
    662 	struct vnode *vp = ((struct vop_remove_args *)v)->a_vp;
    663 
    664 	int error;
    665 	struct tmpfs_dirent *de;
    666 	struct tmpfs_mount *tmp;
    667 	struct tmpfs_node *dnode;
    668 	struct tmpfs_node *node;
    669 
    670 	KASSERT(VOP_ISLOCKED(dvp));
    671 	KASSERT(VOP_ISLOCKED(vp));
    672 
    673 	dnode = VP_TO_TMPFS_DIR(dvp);
    674 	node = VP_TO_TMPFS_NODE(vp);
    675 	tmp = VFS_TO_TMPFS(vp->v_mount);
    676 	de = node->tn_lookup_dirent;
    677 	KASSERT(de != NULL);
    678 
    679 	/* XXX: Why isn't this done by the caller? */
    680 	if (vp->v_type == VDIR) {
    681 		error = EISDIR;
    682 		goto out;
    683 	}
    684 
    685 	/* Files marked as immutable or append-only cannot be deleted. */
    686 	if (node->tn_flags & (IMMUTABLE | APPEND)) {
    687 		error = EPERM;
    688 		goto out;
    689 	}
    690 
    691 	/* Remove the entry from the directory; as it is a file, we do not
    692 	 * have to change the number of hard links of the directory. */
    693 	tmpfs_dir_detach(dvp, de);
    694 
    695 	/* Notify interested parties about the modification of dvp.
    696 	 * The removal of vp is notified when it is reclaimed. */
    697 	VN_KNOTE(dvp, NOTE_WRITE);
    698 
    699 	/* Free the directory entry we just deleted.  Note that the node
    700 	 * referred by it will not be removed until the vnode is really
    701 	 * reclaimed. */
    702 	tmpfs_free_dirent(tmp, de, TRUE);
    703 
    704 	error = 0;
    705 
    706 out:
    707 	vput(dvp);
    708 	vput(vp);
    709 
    710 	KASSERT(!VOP_ISLOCKED(dvp));
    711 
    712 	return error;
    713 }
    714 
    715 /* --------------------------------------------------------------------- */
    716 
    717 int
    718 tmpfs_link(void *v)
    719 {
    720 	struct vnode *dvp = ((struct vop_link_args *)v)->a_dvp;
    721 	struct vnode *vp = ((struct vop_link_args *)v)->a_vp;
    722 	struct componentname *cnp = ((struct vop_link_args *)v)->a_cnp;
    723 
    724 	int error;
    725 	struct tmpfs_dirent *de;
    726 	struct tmpfs_node *dnode;
    727 	struct tmpfs_node *node;
    728 
    729 	KASSERT(VOP_ISLOCKED(dvp));
    730 	KASSERT(!VOP_ISLOCKED(vp));
    731 	KASSERT(cnp->cn_flags & HASBUF);
    732 	KASSERT(dvp != vp); /* XXX When can this be false? */
    733 
    734 	dnode = VP_TO_TMPFS_DIR(dvp);
    735 	node = VP_TO_TMPFS_NODE(vp);
    736 
    737 	/* Lock vp because we will need to run tmpfs_update over it, which
    738 	 * needs the vnode to be locked. */
    739 	error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    740 	if (error != 0)
    741 		goto out;
    742 
    743 	/* XXX: Why aren't the following two tests done by the caller? */
    744 
    745 	/* Hard links of directories are forbidden. */
    746 	if (vp->v_type == VDIR) {
    747 		error = EPERM;
    748 		goto out;
    749 	}
    750 
    751 	/* Cannot create cross-device links. */
    752 	if (dvp->v_mount != vp->v_mount) {
    753 		error = EXDEV;
    754 		goto out;
    755 	}
    756 
    757 	/* Ensure that we do not overflow the maximum number of links imposed
    758 	 * by the system. */
    759 	KASSERT(node->tn_links <= LINK_MAX);
    760 	if (node->tn_links == LINK_MAX) {
    761 		error = EMLINK;
    762 		goto out;
    763 	}
    764 
    765 	/* We cannot create links of files marked immutable or append-only. */
    766 	if (node->tn_flags & (IMMUTABLE | APPEND)) {
    767 		error = EPERM;
    768 		goto out;
    769 	}
    770 
    771 	/* Allocate a new directory entry to represent the node. */
    772 	error = tmpfs_alloc_dirent(VFS_TO_TMPFS(vp->v_mount), node,
    773 	    cnp->cn_nameptr, cnp->cn_namelen, &de);
    774 	if (error != 0)
    775 		goto out;
    776 
    777 	/* Insert the new directory entry into the appropriate directory. */
    778 	tmpfs_dir_attach(dvp, de);
    779 	VN_KNOTE(dvp, NOTE_WRITE);
    780 
    781 	/* vp link count has changed, so update node times. */
    782 	node->tn_status |= TMPFS_NODE_CHANGED;
    783 	tmpfs_update(vp, NULL, NULL, 0);
    784 
    785 	error = 0;
    786 
    787 out:
    788 	if (VOP_ISLOCKED(vp))
    789 		VOP_UNLOCK(vp, 0);
    790 
    791 	PNBUF_PUT(cnp->cn_pnbuf);
    792 
    793 	vput(dvp);
    794 
    795 	/* XXX Locking status of dvp does not match manual page. */
    796 	KASSERT(!VOP_ISLOCKED(dvp));
    797 	KASSERT(!VOP_ISLOCKED(vp));
    798 
    799 	return error;
    800 }
    801 
    802 /* --------------------------------------------------------------------- */
    803 
    804 int
    805 tmpfs_rename(void *v)
    806 {
    807 	struct vnode *fdvp = ((struct vop_rename_args *)v)->a_fdvp;
    808 	struct vnode *fvp = ((struct vop_rename_args *)v)->a_fvp;
    809 	struct componentname *fcnp = ((struct vop_rename_args *)v)->a_fcnp;
    810 	struct vnode *tdvp = ((struct vop_rename_args *)v)->a_tdvp;
    811 	struct vnode *tvp = ((struct vop_rename_args *)v)->a_tvp;
    812 	struct componentname *tcnp = ((struct vop_rename_args *)v)->a_tcnp;
    813 
    814 	char *newname;
    815 	int error;
    816 	struct tmpfs_dirent *de;
    817 	struct tmpfs_mount *tmp;
    818 	struct tmpfs_node *fdnode;
    819 	struct tmpfs_node *fnode;
    820 	struct tmpfs_node *tdnode;
    821 
    822 	KASSERT(VOP_ISLOCKED(tdvp));
    823 	KASSERT(IMPLIES(tvp != NULL, VOP_ISLOCKED(tvp)));
    824 	KASSERT(fcnp->cn_flags & HASBUF);
    825 	KASSERT(tcnp->cn_flags & HASBUF);
    826 
    827 	fdnode = VP_TO_TMPFS_DIR(fdvp);
    828 	fnode = VP_TO_TMPFS_NODE(fvp);
    829 	de = fnode->tn_lookup_dirent;
    830 
    831 	/* Disallow cross-device renames.
    832 	 * XXX Why isn't this done by the caller? */
    833 	if (fvp->v_mount != tdvp->v_mount ||
    834 	    (tvp != NULL && fvp->v_mount != tvp->v_mount)) {
    835 		error = EXDEV;
    836 		goto out;
    837 	}
    838 
    839 	tmp = VFS_TO_TMPFS(tdvp->v_mount);
    840 	tdnode = VP_TO_TMPFS_DIR(tdvp);
    841 
    842 	/* If source and target are the same file, there is nothing to do. */
    843 	if (fvp == tvp) {
    844 		error = 0;
    845 		goto out;
    846 	}
    847 
    848 	/* Avoid manipulating '.' and '..' entries. */
    849 	if (de == NULL) {
    850 		KASSERT(fvp->v_type == VDIR);
    851 		error = EINVAL;
    852 		goto out;
    853 	}
    854 	KASSERT(de->td_node == fnode);
    855 
    856 	/* If we need to move the directory between entries, lock the
    857 	 * source so that we can safely operate on it. */
    858 	if (fdnode != tdnode) {
    859 		error = vn_lock(fdvp, LK_EXCLUSIVE | LK_RETRY);
    860 		if (error != 0)
    861 			goto out;
    862 	}
    863 
    864 	/* Ensure that we have enough memory to hold the new name, if it
    865 	 * has to be changed. */
    866 	if (fcnp->cn_namelen != tcnp->cn_namelen ||
    867 	    memcmp(fcnp->cn_nameptr, tcnp->cn_nameptr, fcnp->cn_namelen) != 0) {
    868 		newname = tmpfs_str_pool_get(&tmp->tm_str_pool,
    869 		    tcnp->cn_namelen, 0);
    870 		if (newname == NULL) {
    871 			error = ENOSPC;
    872 			goto out_locked;
    873 		}
    874 	} else
    875 		newname = NULL;
    876 
    877 	/* If the node is being moved to another directory, we have to do
    878 	 * the move. */
    879 	if (fdnode != tdnode) {
    880 		/* In case we are moving a directory, we have to adjust its
    881 		 * parent to point to the new parent. */
    882 		if (de->td_node->tn_type == VDIR) {
    883 			struct tmpfs_node *n;
    884 
    885 			/* Ensure the target directory is not a child of the
    886 			 * directory being moved.  Otherwise, we'd end up
    887 			 * with stale nodes. */
    888 			n = tdnode;
    889 			while (n != n->tn_spec.tn_dir.tn_parent) {
    890 				if (n == fnode) {
    891 					error = EINVAL;
    892 					goto out_locked;
    893 				}
    894 				n = n->tn_spec.tn_dir.tn_parent;
    895 			}
    896 
    897 			/* Adjust the parent pointer. */
    898 			TMPFS_VALIDATE_DIR(fnode);
    899 			de->td_node->tn_spec.tn_dir.tn_parent = tdnode;
    900 
    901 			/* As a result of changing the target of the '..'
    902 			 * entry, the link count of the source and target
    903 			 * directories has to be adjusted. */
    904 			fdnode->tn_links--;
    905 			tdnode->tn_links++;
    906 		}
    907 
    908 		/* Do the move: just remove the entry from the source directory
    909 		 * and insert it into the target one. */
    910 		tmpfs_dir_detach(fdvp, de);
    911 		tmpfs_dir_attach(tdvp, de);
    912 
    913 		/* Notify listeners of fdvp about the change in the directory.
    914 		 * We can do it at this point because we aren't touching fdvp
    915 		 * any more below. */
    916 		VN_KNOTE(fdvp, NOTE_WRITE);
    917 	}
    918 
    919 	/* If the name has changed, we need to make it effective by changing
    920 	 * it in the directory entry. */
    921 	if (newname != NULL) {
    922 		KASSERT(tcnp->cn_namelen < MAXNAMLEN);
    923 		KASSERT(tcnp->cn_namelen < 0xffff);
    924 
    925 		tmpfs_str_pool_put(&tmp->tm_str_pool, de->td_name,
    926 		    de->td_namelen);
    927 		de->td_namelen = (uint16_t)tcnp->cn_namelen;
    928 		memcpy(newname, tcnp->cn_nameptr, tcnp->cn_namelen);
    929 		de->td_name = newname;
    930 
    931 		fnode->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 	struct tmpfs_node *node;
   1386 	struct uvm_object *uobj;
   1387 	int npages = *count;
   1388 
   1389 	KASSERT(vp->v_type == VREG);
   1390 	LOCK_ASSERT(simple_lock_held(&vp->v_interlock));
   1391 
   1392 	node = VP_TO_TMPFS_NODE(vp);
   1393 	uobj = node->tn_spec.tn_reg.tn_aobj;
   1394 
   1395 	/* We currently don't rely on PGO_PASTEOF. */
   1396 
   1397 	if (vp->v_size <= offset + (centeridx << PAGE_SHIFT)) {
   1398 		if ((flags & PGO_LOCKED) == 0)
   1399 			simple_unlock(&vp->v_interlock);
   1400 		return EINVAL;
   1401 	}
   1402 
   1403 	if (vp->v_size < offset + (npages << PAGE_SHIFT)) {
   1404 		npages = (round_page(vp->v_size) - offset) >> PAGE_SHIFT;
   1405 	}
   1406 
   1407 	if ((flags & PGO_LOCKED) != 0)
   1408 		return EBUSY;
   1409 
   1410 	if ((flags & PGO_NOTIMESTAMP) == 0) {
   1411 		if ((vp->v_mount->mnt_flag & MNT_NOATIME) == 0)
   1412 			node->tn_status |= TMPFS_NODE_ACCESSED;
   1413 
   1414 		if ((access_type & VM_PROT_WRITE) != 0)
   1415 			node->tn_status |= TMPFS_NODE_MODIFIED;
   1416 	}
   1417 
   1418 	simple_unlock(&vp->v_interlock);
   1419 
   1420 	simple_lock(&uobj->vmobjlock);
   1421 	error = (*uobj->pgops->pgo_get)(uobj, offset, m, &npages, centeridx,
   1422 	    access_type, advice, flags);
   1423 
   1424 	return error;
   1425 }
   1426 
   1427 /* --------------------------------------------------------------------- */
   1428 
   1429 int
   1430 tmpfs_putpages(void *v)
   1431 {
   1432 	struct vnode *vp = ((struct vop_putpages_args *)v)->a_vp;
   1433 	voff_t offlo = ((struct vop_putpages_args *)v)->a_offlo;
   1434 	voff_t offhi = ((struct vop_putpages_args *)v)->a_offhi;
   1435 	int flags = ((struct vop_putpages_args *)v)->a_flags;
   1436 
   1437 	int error;
   1438 	struct tmpfs_node *node;
   1439 	struct uvm_object *uobj;
   1440 
   1441 	LOCK_ASSERT(simple_lock_held(&vp->v_interlock));
   1442 
   1443 	node = VP_TO_TMPFS_NODE(vp);
   1444 
   1445 	if (vp->v_type != VREG) {
   1446 		simple_unlock(&vp->v_interlock);
   1447 		return 0;
   1448 	}
   1449 
   1450 	uobj = node->tn_spec.tn_reg.tn_aobj;
   1451 	simple_unlock(&vp->v_interlock);
   1452 
   1453 	simple_lock(&uobj->vmobjlock);
   1454 	error = (*uobj->pgops->pgo_put)(uobj, offlo, offhi, flags);
   1455 
   1456 	/* XXX mtime */
   1457 
   1458 	return error;
   1459 }
   1460