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