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