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