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