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