Home | History | Annotate | Line # | Download | only in tmpfs
tmpfs_vnops.c revision 1.32.2.2.2.1
      1 /*	$NetBSD: tmpfs_vnops.c,v 1.32.2.2.2.1 2007/09/03 07:05:02 wrstuden 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.32.2.2.2.1 2007/09/03 07:05:02 wrstuden 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 *tnode;
    810 	struct tmpfs_node *tdnode;
    811 
    812 	KASSERT(VOP_ISLOCKED(tdvp));
    813 	KASSERT(IMPLIES(tvp != NULL, VOP_ISLOCKED(tvp)));
    814 	KASSERT(fcnp->cn_flags & HASBUF);
    815 	KASSERT(tcnp->cn_flags & HASBUF);
    816 
    817 	fdnode = VP_TO_TMPFS_DIR(fdvp);
    818 	fnode = VP_TO_TMPFS_NODE(fvp);
    819 	tnode = (tvp == NULL) ? NULL : VP_TO_TMPFS_NODE(tvp);
    820 	de = fnode->tn_lookup_dirent;
    821 
    822 	/* Disallow cross-device renames.
    823 	 * XXX Why isn't this done by the caller? */
    824 	if (fvp->v_mount != tdvp->v_mount ||
    825 	    (tvp != NULL && fvp->v_mount != tvp->v_mount)) {
    826 		error = EXDEV;
    827 		goto out;
    828 	}
    829 
    830 	tmp = VFS_TO_TMPFS(tdvp->v_mount);
    831 	tdnode = VP_TO_TMPFS_DIR(tdvp);
    832 
    833 	/* If source and target are the same file, there is nothing to do. */
    834 	if (fvp == tvp) {
    835 		error = 0;
    836 		goto out;
    837 	}
    838 
    839 	/* Avoid manipulating '.' and '..' entries. */
    840 	if (de == NULL) {
    841 		KASSERT(fvp->v_type == VDIR);
    842 		error = EINVAL;
    843 		goto out;
    844 	}
    845 	KASSERT(de->td_node == fnode);
    846 
    847 	/* If replacing an existing entry, ensure we can do the operation. */
    848 	if (tvp != NULL) {
    849 		KASSERT(tnode != NULL);
    850 		if (fnode->tn_type == VDIR && tnode->tn_type == VDIR) {
    851 			if (tnode->tn_size > 0) {
    852 				error = ENOTEMPTY;
    853 				goto out;
    854 			}
    855 		} else if (fnode->tn_type == VDIR && tnode->tn_type != VDIR) {
    856 			error = ENOTDIR;
    857 			goto out;
    858 		} else if (fnode->tn_type != VDIR && tnode->tn_type == VDIR) {
    859 			error = EISDIR;
    860 			goto out;
    861 		} else {
    862 			KASSERT(fnode->tn_type != VDIR &&
    863 			        tnode->tn_type != VDIR);
    864 		}
    865 	}
    866 
    867 	/* If we need to move the directory between entries, lock the
    868 	 * source so that we can safely operate on it. */
    869 	if (fdnode != tdnode) {
    870 		error = vn_lock(fdvp, LK_EXCLUSIVE | LK_RETRY);
    871 		if (error != 0)
    872 			goto out;
    873 	}
    874 
    875 	/* Ensure that we have enough memory to hold the new name, if it
    876 	 * has to be changed. */
    877 	if (fcnp->cn_namelen != tcnp->cn_namelen ||
    878 	    memcmp(fcnp->cn_nameptr, tcnp->cn_nameptr, fcnp->cn_namelen) != 0) {
    879 		newname = tmpfs_str_pool_get(&tmp->tm_str_pool,
    880 		    tcnp->cn_namelen, 0);
    881 		if (newname == NULL) {
    882 			error = ENOSPC;
    883 			goto out_locked;
    884 		}
    885 	} else
    886 		newname = NULL;
    887 
    888 	/* If the node is being moved to another directory, we have to do
    889 	 * the move. */
    890 	if (fdnode != tdnode) {
    891 		/* In case we are moving a directory, we have to adjust its
    892 		 * parent to point to the new parent. */
    893 		if (de->td_node->tn_type == VDIR) {
    894 			struct tmpfs_node *n;
    895 
    896 			/* Ensure the target directory is not a child of the
    897 			 * directory being moved.  Otherwise, we'd end up
    898 			 * with stale nodes. */
    899 			n = tdnode;
    900 			while (n != n->tn_spec.tn_dir.tn_parent) {
    901 				if (n == fnode) {
    902 					error = EINVAL;
    903 					goto out_locked;
    904 				}
    905 				n = n->tn_spec.tn_dir.tn_parent;
    906 			}
    907 
    908 			/* Adjust the parent pointer. */
    909 			TMPFS_VALIDATE_DIR(fnode);
    910 			de->td_node->tn_spec.tn_dir.tn_parent = tdnode;
    911 
    912 			/* As a result of changing the target of the '..'
    913 			 * entry, the link count of the source and target
    914 			 * directories has to be adjusted. */
    915 			fdnode->tn_links--;
    916 			tdnode->tn_links++;
    917 		}
    918 
    919 		/* Do the move: just remove the entry from the source directory
    920 		 * and insert it into the target one. */
    921 		tmpfs_dir_detach(fdvp, de);
    922 		tmpfs_dir_attach(tdvp, de);
    923 
    924 		/* Notify listeners of fdvp about the change in the directory.
    925 		 * We can do it at this point because we aren't touching fdvp
    926 		 * any more below. */
    927 		VN_KNOTE(fdvp, NOTE_WRITE);
    928 	}
    929 
    930 	/* If the name has changed, we need to make it effective by changing
    931 	 * it in the directory entry. */
    932 	if (newname != NULL) {
    933 		KASSERT(tcnp->cn_namelen < MAXNAMLEN);
    934 		KASSERT(tcnp->cn_namelen < 0xffff);
    935 
    936 		tmpfs_str_pool_put(&tmp->tm_str_pool, de->td_name,
    937 		    de->td_namelen);
    938 		de->td_namelen = (uint16_t)tcnp->cn_namelen;
    939 		memcpy(newname, tcnp->cn_nameptr, tcnp->cn_namelen);
    940 		de->td_name = newname;
    941 
    942 		fnode->tn_status |= TMPFS_NODE_CHANGED;
    943 		tdnode->tn_status |= TMPFS_NODE_MODIFIED;
    944 	}
    945 
    946 	/* If we are overwriting an entry, we have to remove the old one
    947 	 * from the target directory. */
    948 	if (tvp != NULL) {
    949 		KASSERT(tnode != NULL);
    950 
    951 		/* Remove the old entry from the target directory. */
    952 		de = tnode->tn_lookup_dirent;
    953 		tmpfs_dir_detach(tdvp, de);
    954 
    955 		/* Free the directory entry we just deleted.  Note that the
    956 		 * node referred by it will not be removed until the vnode is
    957 		 * really reclaimed. */
    958 		tmpfs_free_dirent(VFS_TO_TMPFS(tvp->v_mount), de, TRUE);
    959 	}
    960 
    961 	/* Notify listeners of tdvp about the change in the directory (either
    962 	 * because a new entry was added or because one was removed) and
    963 	 * listeners of fvp about the rename. */
    964 	VN_KNOTE(tdvp, NOTE_WRITE);
    965 	VN_KNOTE(fvp, NOTE_RENAME);
    966 
    967 	error = 0;
    968 
    969 out_locked:
    970 	if (fdnode != tdnode)
    971 		VOP_UNLOCK(fdvp, 0);
    972 
    973 out:
    974 	/* Release target nodes. */
    975 	/* XXX: I don't understand when tdvp can be the same as tvp, but
    976 	 * other code takes care of this... */
    977 	if (tdvp == tvp)
    978 		vrele(tdvp);
    979 	else
    980 		vput(tdvp);
    981 	if (tvp != NULL)
    982 		vput(tvp);
    983 
    984 	/* Release source nodes. */
    985 	vrele(fdvp);
    986 	vrele(fvp);
    987 
    988 	return error;
    989 }
    990 
    991 /* --------------------------------------------------------------------- */
    992 
    993 int
    994 tmpfs_mkdir(void *v)
    995 {
    996 	struct vnode *dvp = ((struct vop_mkdir_args *)v)->a_dvp;
    997 	struct vnode **vpp = ((struct vop_mkdir_args *)v)->a_vpp;
    998 	struct componentname *cnp = ((struct vop_mkdir_args *)v)->a_cnp;
    999 	struct vattr *vap = ((struct vop_mkdir_args *)v)->a_vap;
   1000 
   1001 	KASSERT(vap->va_type == VDIR);
   1002 
   1003 	return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
   1004 }
   1005 
   1006 /* --------------------------------------------------------------------- */
   1007 
   1008 int
   1009 tmpfs_rmdir(void *v)
   1010 {
   1011 	struct vnode *dvp = ((struct vop_rmdir_args *)v)->a_dvp;
   1012 	struct vnode *vp = ((struct vop_rmdir_args *)v)->a_vp;
   1013 
   1014 	int error;
   1015 	struct tmpfs_dirent *de;
   1016 	struct tmpfs_mount *tmp;
   1017 	struct tmpfs_node *dnode;
   1018 	struct tmpfs_node *node;
   1019 
   1020 	KASSERT(VOP_ISLOCKED(dvp));
   1021 	KASSERT(VOP_ISLOCKED(vp));
   1022 
   1023 	tmp = VFS_TO_TMPFS(dvp->v_mount);
   1024 	dnode = VP_TO_TMPFS_DIR(dvp);
   1025 	node = VP_TO_TMPFS_DIR(vp);
   1026 
   1027 	/* Directories with more than two entries ('.' and '..') cannot be
   1028 	 * removed. */
   1029 	if (node->tn_size > 0) {
   1030 		error = ENOTEMPTY;
   1031 		goto out;
   1032 	}
   1033 
   1034 	/* This invariant holds only if we are not trying to remove "..".
   1035 	 * We checked for that above so this is safe now. */
   1036 	KASSERT(node->tn_spec.tn_dir.tn_parent == dnode);
   1037 
   1038 	/* Get the directory entry associated with node (vp).  This was
   1039 	 * filled by tmpfs_lookup while looking up the entry. */
   1040 	de = node->tn_lookup_dirent;
   1041 	KASSERT(TMPFS_DIRENT_MATCHES(de,
   1042 	    ((struct vop_rmdir_args *)v)->a_cnp->cn_nameptr,
   1043 	    ((struct vop_rmdir_args *)v)->a_cnp->cn_namelen));
   1044 
   1045 	/* Check flags to see if we are allowed to remove the directory. */
   1046 	if (dnode->tn_flags & APPEND || node->tn_flags & (IMMUTABLE | APPEND)) {
   1047 		error = EPERM;
   1048 		goto out;
   1049 	}
   1050 
   1051 	/* Detach the directory entry from the directory (dnode). */
   1052 	tmpfs_dir_detach(dvp, de);
   1053 
   1054 	node->tn_links--;
   1055 	node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \
   1056 	    TMPFS_NODE_MODIFIED;
   1057 	node->tn_spec.tn_dir.tn_parent->tn_links--;
   1058 	node->tn_spec.tn_dir.tn_parent->tn_status |= TMPFS_NODE_ACCESSED | \
   1059 	    TMPFS_NODE_CHANGED | TMPFS_NODE_MODIFIED;
   1060 
   1061 	/* Release the parent. */
   1062 	cache_purge(dvp); /* XXX Is this needed? */
   1063 	vput(dvp);
   1064 
   1065 	/* Free the directory entry we just deleted.  Note that the node
   1066 	 * referred by it will not be removed until the vnode is really
   1067 	 * reclaimed. */
   1068 	tmpfs_free_dirent(tmp, de, TRUE);
   1069 
   1070 	/* Release the deleted vnode (will destroy the node, notify
   1071 	 * interested parties and clean it from the cache). */
   1072 	vput(vp);
   1073 
   1074 	error = 0;
   1075 
   1076 out:
   1077 	if (error != 0) {
   1078 		vput(dvp);
   1079 		vput(vp);
   1080 	}
   1081 
   1082 	return error;
   1083 }
   1084 
   1085 /* --------------------------------------------------------------------- */
   1086 
   1087 int
   1088 tmpfs_symlink(void *v)
   1089 {
   1090 	struct vnode *dvp = ((struct vop_symlink_args *)v)->a_dvp;
   1091 	struct vnode **vpp = ((struct vop_symlink_args *)v)->a_vpp;
   1092 	struct componentname *cnp = ((struct vop_symlink_args *)v)->a_cnp;
   1093 	struct vattr *vap = ((struct vop_symlink_args *)v)->a_vap;
   1094 	char *target = ((struct vop_symlink_args *)v)->a_target;
   1095 
   1096 	KASSERT(vap->va_type == VLNK);
   1097 
   1098 	return tmpfs_alloc_file(dvp, vpp, vap, cnp, target);
   1099 }
   1100 
   1101 /* --------------------------------------------------------------------- */
   1102 
   1103 int
   1104 tmpfs_readdir(void *v)
   1105 {
   1106 	struct vnode *vp = ((struct vop_readdir_args *)v)->a_vp;
   1107 	struct uio *uio = ((struct vop_readdir_args *)v)->a_uio;
   1108 	int *eofflag = ((struct vop_readdir_args *)v)->a_eofflag;
   1109 	off_t **cookies = ((struct vop_readdir_args *)v)->a_cookies;
   1110 	int *ncookies = ((struct vop_readdir_args *)v)->a_ncookies;
   1111 
   1112 	int error;
   1113 	off_t startoff;
   1114 	off_t cnt;
   1115 	struct tmpfs_node *node;
   1116 
   1117 	KASSERT(VOP_ISLOCKED(vp));
   1118 
   1119 	/* This operation only makes sense on directory nodes. */
   1120 	if (vp->v_type != VDIR) {
   1121 		error = ENOTDIR;
   1122 		goto out;
   1123 	}
   1124 
   1125 	node = VP_TO_TMPFS_DIR(vp);
   1126 
   1127 	startoff = uio->uio_offset;
   1128 
   1129 	cnt = 0;
   1130 	if (uio->uio_offset == TMPFS_DIRCOOKIE_DOT) {
   1131 		error = tmpfs_dir_getdotdent(node, uio);
   1132 		if (error == -1) {
   1133 			error = 0;
   1134 			goto outok;
   1135 		} else if (error != 0)
   1136 			goto outok;
   1137 		cnt++;
   1138 	}
   1139 
   1140 	if (uio->uio_offset == TMPFS_DIRCOOKIE_DOTDOT) {
   1141 		error = tmpfs_dir_getdotdotdent(node, uio);
   1142 		if (error == -1) {
   1143 			error = 0;
   1144 			goto outok;
   1145 		} else if (error != 0)
   1146 			goto outok;
   1147 		cnt++;
   1148 	}
   1149 
   1150 	error = tmpfs_dir_getdents(node, uio, &cnt);
   1151 	if (error == -1)
   1152 		error = 0;
   1153 	KASSERT(error >= 0);
   1154 
   1155 outok:
   1156 	/* This label assumes that startoff has been
   1157 	 * initialized.  If the compiler didn't spit out warnings, we'd
   1158 	 * simply make this one be 'out' and drop 'outok'. */
   1159 
   1160 	if (eofflag != NULL)
   1161 		*eofflag =
   1162 		    (error == 0 && uio->uio_offset == TMPFS_DIRCOOKIE_EOF);
   1163 
   1164 	/* Update NFS-related variables. */
   1165 	if (error == 0 && cookies != NULL && ncookies != NULL) {
   1166 		off_t i;
   1167 		off_t off = startoff;
   1168 		struct tmpfs_dirent *de = NULL;
   1169 
   1170 		*ncookies = cnt;
   1171 		*cookies = malloc(cnt * sizeof(off_t), M_TEMP, M_WAITOK);
   1172 
   1173 		for (i = 0; i < cnt; i++) {
   1174 			KASSERT(off != TMPFS_DIRCOOKIE_EOF);
   1175 			if (off == TMPFS_DIRCOOKIE_DOT) {
   1176 				off = TMPFS_DIRCOOKIE_DOTDOT;
   1177 			} else {
   1178 				if (off == TMPFS_DIRCOOKIE_DOTDOT) {
   1179 					de = TAILQ_FIRST(&node->tn_spec.
   1180 					    tn_dir.tn_dir);
   1181 				} else if (de != NULL) {
   1182 					de = TAILQ_NEXT(de, td_entries);
   1183 				} else {
   1184 					de = tmpfs_dir_lookupbycookie(node,
   1185 					    off);
   1186 					KASSERT(de != NULL);
   1187 					de = TAILQ_NEXT(de, td_entries);
   1188 				}
   1189 				if (de == NULL) {
   1190 					off = TMPFS_DIRCOOKIE_EOF;
   1191 				} else {
   1192 					off = tmpfs_dircookie(de);
   1193 				}
   1194 			}
   1195 
   1196 			(*cookies)[i] = off;
   1197 		}
   1198 		KASSERT(uio->uio_offset == off);
   1199 	}
   1200 
   1201 out:
   1202 	KASSERT(VOP_ISLOCKED(vp));
   1203 
   1204 	return error;
   1205 }
   1206 
   1207 /* --------------------------------------------------------------------- */
   1208 
   1209 int
   1210 tmpfs_readlink(void *v)
   1211 {
   1212 	struct vnode *vp = ((struct vop_readlink_args *)v)->a_vp;
   1213 	struct uio *uio = ((struct vop_readlink_args *)v)->a_uio;
   1214 
   1215 	int error;
   1216 	struct tmpfs_node *node;
   1217 
   1218 	KASSERT(VOP_ISLOCKED(vp));
   1219 	KASSERT(uio->uio_offset == 0);
   1220 	KASSERT(vp->v_type == VLNK);
   1221 
   1222 	node = VP_TO_TMPFS_NODE(vp);
   1223 
   1224 	error = uiomove(node->tn_spec.tn_lnk.tn_link,
   1225 	    MIN(node->tn_size, uio->uio_resid), uio);
   1226 	node->tn_status |= TMPFS_NODE_ACCESSED;
   1227 
   1228 	KASSERT(VOP_ISLOCKED(vp));
   1229 
   1230 	return error;
   1231 }
   1232 
   1233 /* --------------------------------------------------------------------- */
   1234 
   1235 int
   1236 tmpfs_inactive(void *v)
   1237 {
   1238 	struct vnode *vp = ((struct vop_inactive_args *)v)->a_vp;
   1239 	struct lwp *l = ((struct vop_inactive_args *)v)->a_l;
   1240 
   1241 	struct tmpfs_node *node;
   1242 
   1243 	KASSERT(VOP_ISLOCKED(vp));
   1244 
   1245 	node = VP_TO_TMPFS_NODE(vp);
   1246 
   1247 	VOP_UNLOCK(vp, 0);
   1248 
   1249 	if (node->tn_links == 0)
   1250 		vrecycle(vp, NULL, l);
   1251 
   1252 	return 0;
   1253 }
   1254 
   1255 /* --------------------------------------------------------------------- */
   1256 
   1257 int
   1258 tmpfs_reclaim(void *v)
   1259 {
   1260 	struct vnode *vp = ((struct vop_reclaim_args *)v)->a_vp;
   1261 
   1262 	struct tmpfs_mount *tmp;
   1263 	struct tmpfs_node *node;
   1264 
   1265 	KASSERT(!VOP_ISLOCKED(vp));
   1266 
   1267 	node = VP_TO_TMPFS_NODE(vp);
   1268 	tmp = VFS_TO_TMPFS(vp->v_mount);
   1269 
   1270 	cache_purge(vp);
   1271 	tmpfs_free_vp(vp);
   1272 
   1273 	/* If the node referenced by this vnode was deleted by the user,
   1274 	 * we must free its associated data structures (now that the vnode
   1275 	 * is being reclaimed). */
   1276 	if (node->tn_links == 0)
   1277 		tmpfs_free_node(tmp, node);
   1278 
   1279 	KASSERT(!VOP_ISLOCKED(vp));
   1280 	KASSERT(vp->v_data == NULL);
   1281 
   1282 	return 0;
   1283 }
   1284 
   1285 /* --------------------------------------------------------------------- */
   1286 
   1287 int
   1288 tmpfs_print(void *v)
   1289 {
   1290 	struct vnode *vp = ((struct vop_print_args *)v)->a_vp;
   1291 
   1292 	struct tmpfs_node *node;
   1293 
   1294 	node = VP_TO_TMPFS_NODE(vp);
   1295 
   1296 	printf("tag VT_TMPFS, tmpfs_node %p, flags 0x%x, links %d\n",
   1297 	    node, node->tn_flags, node->tn_links);
   1298 	printf("\tmode 0%o, owner %d, group %d, size %" PRIdMAX
   1299 	    ", status 0x%x\n",
   1300 	    node->tn_mode, node->tn_uid, node->tn_gid,
   1301 	    (uintmax_t)node->tn_size, node->tn_status);
   1302 
   1303 	if (vp->v_type == VFIFO)
   1304 		fifo_printinfo(vp);
   1305 	lockmgr_printinfo(&vp->v_lock);
   1306 
   1307 	printf("\n");
   1308 
   1309 	return 0;
   1310 }
   1311 
   1312 /* --------------------------------------------------------------------- */
   1313 
   1314 int
   1315 tmpfs_pathconf(void *v)
   1316 {
   1317 	int name = ((struct vop_pathconf_args *)v)->a_name;
   1318 	register_t *retval = ((struct vop_pathconf_args *)v)->a_retval;
   1319 
   1320 	int error;
   1321 
   1322 	error = 0;
   1323 
   1324 	switch (name) {
   1325 	case _PC_LINK_MAX:
   1326 		*retval = LINK_MAX;
   1327 		break;
   1328 
   1329 	case _PC_NAME_MAX:
   1330 		*retval = NAME_MAX;
   1331 		break;
   1332 
   1333 	case _PC_PATH_MAX:
   1334 		*retval = PATH_MAX;
   1335 		break;
   1336 
   1337 	case _PC_PIPE_BUF:
   1338 		*retval = PIPE_BUF;
   1339 		break;
   1340 
   1341 	case _PC_CHOWN_RESTRICTED:
   1342 		*retval = 1;
   1343 		break;
   1344 
   1345 	case _PC_NO_TRUNC:
   1346 		*retval = 1;
   1347 		break;
   1348 
   1349 	case _PC_SYNC_IO:
   1350 		*retval = 1;
   1351 		break;
   1352 
   1353 	case _PC_FILESIZEBITS:
   1354 		*retval = 0; /* XXX Don't know which value should I return. */
   1355 		break;
   1356 
   1357 	default:
   1358 		error = EINVAL;
   1359 	}
   1360 
   1361 	return error;
   1362 }
   1363 
   1364 /* --------------------------------------------------------------------- */
   1365 
   1366 int
   1367 tmpfs_advlock(void *v)
   1368 {
   1369 	struct vnode *vp = ((struct vop_advlock_args *)v)->a_vp;
   1370 
   1371 	struct tmpfs_node *node;
   1372 
   1373 	node = VP_TO_TMPFS_NODE(vp);
   1374 
   1375 	return lf_advlock(v, &node->tn_lockf, node->tn_size);
   1376 }
   1377 
   1378 /* --------------------------------------------------------------------- */
   1379 
   1380 int
   1381 tmpfs_getpages(void *v)
   1382 {
   1383 	struct vnode *vp = ((struct vop_getpages_args *)v)->a_vp;
   1384 	voff_t offset = ((struct vop_getpages_args *)v)->a_offset;
   1385 	struct vm_page **m = ((struct vop_getpages_args *)v)->a_m;
   1386 	int *count = ((struct vop_getpages_args *)v)->a_count;
   1387 	int centeridx = ((struct vop_getpages_args *)v)->a_centeridx;
   1388 	vm_prot_t access_type = ((struct vop_getpages_args *)v)->a_access_type;
   1389 	int advice = ((struct vop_getpages_args *)v)->a_advice;
   1390 	int flags = ((struct vop_getpages_args *)v)->a_flags;
   1391 
   1392 	int error;
   1393 	int i;
   1394 	struct tmpfs_node *node;
   1395 	struct uvm_object *uobj;
   1396 	int npages = *count;
   1397 
   1398 	KASSERT(vp->v_type == VREG);
   1399 	LOCK_ASSERT(simple_lock_held(&vp->v_interlock));
   1400 
   1401 	node = VP_TO_TMPFS_NODE(vp);
   1402 	uobj = node->tn_spec.tn_reg.tn_aobj;
   1403 
   1404 	/* We currently don't rely on PGO_PASTEOF. */
   1405 
   1406 	if (vp->v_size <= offset + (centeridx << PAGE_SHIFT)) {
   1407 		if ((flags & PGO_LOCKED) == 0)
   1408 			simple_unlock(&vp->v_interlock);
   1409 		return EINVAL;
   1410 	}
   1411 
   1412 	if (vp->v_size < offset + (npages << PAGE_SHIFT)) {
   1413 		npages = (round_page(vp->v_size) - offset) >> PAGE_SHIFT;
   1414 	}
   1415 
   1416 	if ((flags & PGO_LOCKED) != 0)
   1417 		return EBUSY;
   1418 
   1419 	if ((flags & PGO_NOTIMESTAMP) == 0) {
   1420 		if ((vp->v_mount->mnt_flag & MNT_NOATIME) == 0)
   1421 			node->tn_status |= TMPFS_NODE_ACCESSED;
   1422 
   1423 		if ((access_type & VM_PROT_WRITE) != 0)
   1424 			node->tn_status |= TMPFS_NODE_MODIFIED;
   1425 	}
   1426 
   1427 	simple_unlock(&vp->v_interlock);
   1428 
   1429 	/*
   1430 	 * Make sure that the array on which we will store the
   1431 	 * gotten pages is clean.  Otherwise uao_get (pointed to by
   1432 	 * the pgo_get below) gets confused and does not return the
   1433 	 * appropriate pages.
   1434 	 *
   1435 	 * XXX This shall be revisited when kern/32166 is addressed
   1436 	 * because the loop to clean m[i] will most likely be redundant
   1437 	 * as well as the PGO_ALLPAGES flag.
   1438 	 */
   1439 	if (m != NULL)
   1440 		for (i = 0; i < npages; i++)
   1441 			m[i] = NULL;
   1442 	simple_lock(&uobj->vmobjlock);
   1443 	error = (*uobj->pgops->pgo_get)(uobj, offset, m, &npages, centeridx,
   1444 	    access_type, advice, flags | PGO_ALLPAGES);
   1445 #if defined(DEBUG)
   1446 	{
   1447 		/* Make sure that all the pages we return are valid. */
   1448 		int dbgi;
   1449 		if (error == 0 && m != NULL)
   1450 			for (dbgi = 0; dbgi < npages; dbgi++)
   1451 				KASSERT(m[dbgi] != NULL);
   1452 	}
   1453 #endif
   1454 
   1455 	return error;
   1456 }
   1457 
   1458 /* --------------------------------------------------------------------- */
   1459 
   1460 int
   1461 tmpfs_putpages(void *v)
   1462 {
   1463 	struct vnode *vp = ((struct vop_putpages_args *)v)->a_vp;
   1464 	voff_t offlo = ((struct vop_putpages_args *)v)->a_offlo;
   1465 	voff_t offhi = ((struct vop_putpages_args *)v)->a_offhi;
   1466 	int flags = ((struct vop_putpages_args *)v)->a_flags;
   1467 
   1468 	int error;
   1469 	struct tmpfs_node *node;
   1470 	struct uvm_object *uobj;
   1471 
   1472 	LOCK_ASSERT(simple_lock_held(&vp->v_interlock));
   1473 
   1474 	node = VP_TO_TMPFS_NODE(vp);
   1475 
   1476 	if (vp->v_type != VREG) {
   1477 		simple_unlock(&vp->v_interlock);
   1478 		return 0;
   1479 	}
   1480 
   1481 	uobj = node->tn_spec.tn_reg.tn_aobj;
   1482 	simple_unlock(&vp->v_interlock);
   1483 
   1484 	simple_lock(&uobj->vmobjlock);
   1485 	error = (*uobj->pgops->pgo_put)(uobj, offlo, offhi, flags);
   1486 
   1487 	/* XXX mtime */
   1488 
   1489 	return error;
   1490 }
   1491