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