Home | History | Annotate | Line # | Download | only in tmpfs
tmpfs_subr.c revision 1.36.2.3
      1 /*	$NetBSD: tmpfs_subr.c,v 1.36.2.3 2008/01/09 01:55:52 matt 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  * 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  * Efficient memory file system supporting functions.
     42  */
     43 
     44 #include <sys/cdefs.h>
     45 __KERNEL_RCSID(0, "$NetBSD: tmpfs_subr.c,v 1.36.2.3 2008/01/09 01:55:52 matt Exp $");
     46 
     47 #include <sys/param.h>
     48 #include <sys/dirent.h>
     49 #include <sys/event.h>
     50 #include <sys/kmem.h>
     51 #include <sys/mount.h>
     52 #include <sys/namei.h>
     53 #include <sys/time.h>
     54 #include <sys/stat.h>
     55 #include <sys/systm.h>
     56 #include <sys/swap.h>
     57 #include <sys/vnode.h>
     58 #include <sys/kauth.h>
     59 #include <sys/proc.h>
     60 #include <sys/atomic.h>
     61 
     62 #include <uvm/uvm.h>
     63 
     64 #include <miscfs/specfs/specdev.h>
     65 #include <fs/tmpfs/tmpfs.h>
     66 #include <fs/tmpfs/tmpfs_fifoops.h>
     67 #include <fs/tmpfs/tmpfs_specops.h>
     68 #include <fs/tmpfs/tmpfs_vnops.h>
     69 
     70 /* --------------------------------------------------------------------- */
     71 
     72 /*
     73  * Allocates a new node of type 'type' inside the 'tmp' mount point, with
     74  * its owner set to 'uid', its group to 'gid' and its mode set to 'mode',
     75  * using the credentials of the process 'p'.
     76  *
     77  * If the node type is set to 'VDIR', then the parent parameter must point
     78  * to the parent directory of the node being created.  It may only be NULL
     79  * while allocating the root node.
     80  *
     81  * If the node type is set to 'VBLK' or 'VCHR', then the rdev parameter
     82  * specifies the device the node represents.
     83  *
     84  * If the node type is set to 'VLNK', then the parameter target specifies
     85  * the file name of the target file for the symbolic link that is being
     86  * created.
     87  *
     88  * Note that new nodes are retrieved from the available list if it has
     89  * items or, if it is empty, from the node pool as long as there is enough
     90  * space to create them.
     91  *
     92  * Returns zero on success or an appropriate error code on failure.
     93  */
     94 int
     95 tmpfs_alloc_node(struct tmpfs_mount *tmp, enum vtype type,
     96     uid_t uid, gid_t gid, mode_t mode, struct tmpfs_node *parent,
     97     char *target, dev_t rdev, struct tmpfs_node **node)
     98 {
     99 	struct tmpfs_node *nnode;
    100 
    101 	/* If the root directory of the 'tmp' file system is not yet
    102 	 * allocated, this must be the request to do it. */
    103 	KASSERT(IMPLIES(tmp->tm_root == NULL, parent == NULL && type == VDIR));
    104 
    105 	KASSERT(IFF(type == VLNK, target != NULL));
    106 	KASSERT(IFF(type == VBLK || type == VCHR, rdev != VNOVAL));
    107 
    108 	KASSERT(uid != VNOVAL && gid != VNOVAL && mode != VNOVAL);
    109 
    110 	nnode = NULL;
    111 	if (atomic_inc_uint_nv(&tmp->tm_nodes_cnt) >= tmp->tm_nodes_max) {
    112 		atomic_dec_uint(&tmp->tm_nodes_cnt);
    113 		return ENOSPC;
    114 	}
    115 
    116 	nnode = (struct tmpfs_node *)TMPFS_POOL_GET(&tmp->tm_node_pool, 0);
    117 	if (nnode == NULL) {
    118 		atomic_dec_uint(&tmp->tm_nodes_cnt);
    119 		return ENOSPC;
    120 	}
    121 
    122 	/*
    123 	 * XXX Where the pool is backed by a map larger than (4GB *
    124 	 * sizeof(*nnode)), this may produce duplicate inode numbers
    125 	 * for applications that do not understand 64-bit ino_t.
    126 	 */
    127 	nnode->tn_id = (ino_t)((uintptr_t)nnode / sizeof(*nnode));
    128 	nnode->tn_gen = arc4random();
    129 
    130 	/* Generic initialization. */
    131 	nnode->tn_type = type;
    132 	nnode->tn_size = 0;
    133 	nnode->tn_status = 0;
    134 	nnode->tn_flags = 0;
    135 	nnode->tn_links = 0;
    136 	getnanotime(&nnode->tn_atime);
    137 	nnode->tn_birthtime = nnode->tn_ctime = nnode->tn_mtime =
    138 	    nnode->tn_atime;
    139 	nnode->tn_uid = uid;
    140 	nnode->tn_gid = gid;
    141 	nnode->tn_mode = mode;
    142 	nnode->tn_lockf = NULL;
    143 	nnode->tn_vnode = NULL;
    144 
    145 	/* Type-specific initialization. */
    146 	switch (nnode->tn_type) {
    147 	case VBLK:
    148 	case VCHR:
    149 		nnode->tn_spec.tn_dev.tn_rdev = rdev;
    150 		break;
    151 
    152 	case VDIR:
    153 		TAILQ_INIT(&nnode->tn_spec.tn_dir.tn_dir);
    154 		nnode->tn_spec.tn_dir.tn_parent =
    155 		    (parent == NULL) ? nnode : parent;
    156 		nnode->tn_spec.tn_dir.tn_readdir_lastn = 0;
    157 		nnode->tn_spec.tn_dir.tn_readdir_lastp = NULL;
    158 		nnode->tn_links++;
    159 		break;
    160 
    161 	case VFIFO:
    162 		/* FALLTHROUGH */
    163 	case VSOCK:
    164 		break;
    165 
    166 	case VLNK:
    167 		KASSERT(strlen(target) < MAXPATHLEN);
    168 		nnode->tn_size = strlen(target);
    169 		nnode->tn_spec.tn_lnk.tn_link =
    170 		    tmpfs_str_pool_get(&tmp->tm_str_pool, nnode->tn_size, 0);
    171 		if (nnode->tn_spec.tn_lnk.tn_link == NULL) {
    172 			atomic_dec_uint(&tmp->tm_nodes_cnt);
    173 			TMPFS_POOL_PUT(&tmp->tm_node_pool, nnode);
    174 			return ENOSPC;
    175 		}
    176 		memcpy(nnode->tn_spec.tn_lnk.tn_link, target, nnode->tn_size);
    177 		break;
    178 
    179 	case VREG:
    180 		nnode->tn_spec.tn_reg.tn_aobj =
    181 		    uao_create(INT32_MAX - PAGE_SIZE, 0);
    182 		nnode->tn_spec.tn_reg.tn_aobj_pages = 0;
    183 		break;
    184 
    185 	default:
    186 		KASSERT(0);
    187 	}
    188 
    189 	mutex_init(&nnode->tn_vlock, MUTEX_DEFAULT, IPL_NONE);
    190 
    191 	mutex_enter(&tmp->tm_lock);
    192 	LIST_INSERT_HEAD(&tmp->tm_nodes, nnode, tn_entries);
    193 	mutex_exit(&tmp->tm_lock);
    194 
    195 	*node = nnode;
    196 	return 0;
    197 }
    198 
    199 /* --------------------------------------------------------------------- */
    200 
    201 /*
    202  * Destroys the node pointed to by node from the file system 'tmp'.
    203  * If the node does not belong to the given mount point, the results are
    204  * unpredicted.
    205  *
    206  * If the node references a directory; no entries are allowed because
    207  * their removal could need a recursive algorithm, something forbidden in
    208  * kernel space.  Furthermore, there is not need to provide such
    209  * functionality (recursive removal) because the only primitives offered
    210  * to the user are the removal of empty directories and the deletion of
    211  * individual files.
    212  *
    213  * Note that nodes are not really deleted; in fact, when a node has been
    214  * allocated, it cannot be deleted during the whole life of the file
    215  * system.  Instead, they are moved to the available list and remain there
    216  * until reused.
    217  */
    218 void
    219 tmpfs_free_node(struct tmpfs_mount *tmp, struct tmpfs_node *node)
    220 {
    221 
    222 	if (node->tn_type == VREG) {
    223 		atomic_add_int(&tmp->tm_pages_used,
    224 		    -node->tn_spec.tn_reg.tn_aobj_pages);
    225 	}
    226 	atomic_dec_uint(&tmp->tm_nodes_cnt);
    227 	mutex_enter(&tmp->tm_lock);
    228 	LIST_REMOVE(node, tn_entries);
    229 	mutex_exit(&tmp->tm_lock);
    230 
    231 	switch (node->tn_type) {
    232 	case VLNK:
    233 		tmpfs_str_pool_put(&tmp->tm_str_pool,
    234 		    node->tn_spec.tn_lnk.tn_link, node->tn_size);
    235 		break;
    236 
    237 	case VREG:
    238 		if (node->tn_spec.tn_reg.tn_aobj != NULL)
    239 			uao_detach(node->tn_spec.tn_reg.tn_aobj);
    240 		break;
    241 
    242 	default:
    243 		break;
    244 	}
    245 
    246 	mutex_destroy(&node->tn_vlock);
    247 	TMPFS_POOL_PUT(&tmp->tm_node_pool, node);
    248 }
    249 
    250 /* --------------------------------------------------------------------- */
    251 
    252 /*
    253  * Allocates a new directory entry for the node node with a name of name.
    254  * The new directory entry is returned in *de.
    255  *
    256  * The link count of node is increased by one to reflect the new object
    257  * referencing it.  This takes care of notifying kqueue listeners about
    258  * this change.
    259  *
    260  * Returns zero on success or an appropriate error code on failure.
    261  */
    262 int
    263 tmpfs_alloc_dirent(struct tmpfs_mount *tmp, struct tmpfs_node *node,
    264     const char *name, uint16_t len, struct tmpfs_dirent **de)
    265 {
    266 	struct tmpfs_dirent *nde;
    267 
    268 	nde = (struct tmpfs_dirent *)TMPFS_POOL_GET(&tmp->tm_dirent_pool, 0);
    269 	if (nde == NULL)
    270 		return ENOSPC;
    271 
    272 	nde->td_name = tmpfs_str_pool_get(&tmp->tm_str_pool, len, 0);
    273 	if (nde->td_name == NULL) {
    274 		TMPFS_POOL_PUT(&tmp->tm_dirent_pool, nde);
    275 		return ENOSPC;
    276 	}
    277 	nde->td_namelen = len;
    278 	memcpy(nde->td_name, name, len);
    279 	nde->td_node = node;
    280 
    281 	node->tn_links++;
    282 	if (node->tn_links > 1 && node->tn_vnode != NULL)
    283 		VN_KNOTE(node->tn_vnode, NOTE_LINK);
    284 	*de = nde;
    285 
    286 	return 0;
    287 }
    288 
    289 /* --------------------------------------------------------------------- */
    290 
    291 /*
    292  * Frees a directory entry.  It is the caller's responsibility to destroy
    293  * the node referenced by it if needed.
    294  *
    295  * The link count of node is decreased by one to reflect the removal of an
    296  * object that referenced it.  This only happens if 'node_exists' is true;
    297  * otherwise the function will not access the node referred to by the
    298  * directory entry, as it may already have been released from the outside.
    299  *
    300  * Interested parties (kqueue) are notified of the link count change; note
    301  * that this can include both the node pointed to by the directory entry
    302  * as well as its parent.
    303  */
    304 void
    305 tmpfs_free_dirent(struct tmpfs_mount *tmp, struct tmpfs_dirent *de,
    306     bool node_exists)
    307 {
    308 	if (node_exists) {
    309 		struct tmpfs_node *node;
    310 
    311 		node = de->td_node;
    312 
    313 		KASSERT(node->tn_links > 0);
    314 		node->tn_links--;
    315 		if (node->tn_vnode != NULL)
    316 			VN_KNOTE(node->tn_vnode, node->tn_links == 0 ?
    317 			    NOTE_DELETE : NOTE_LINK);
    318 		if (node->tn_type == VDIR)
    319 			VN_KNOTE(node->tn_spec.tn_dir.tn_parent->tn_vnode,
    320 			    NOTE_LINK);
    321 	}
    322 
    323 	tmpfs_str_pool_put(&tmp->tm_str_pool, de->td_name, de->td_namelen);
    324 	TMPFS_POOL_PUT(&tmp->tm_dirent_pool, de);
    325 }
    326 
    327 /* --------------------------------------------------------------------- */
    328 
    329 /*
    330  * Allocates a new vnode for the node node or returns a new reference to
    331  * an existing one if the node had already a vnode referencing it.  The
    332  * resulting locked vnode is returned in *vpp.
    333  *
    334  * Returns zero on success or an appropriate error code on failure.
    335  */
    336 int
    337 tmpfs_alloc_vp(struct mount *mp, struct tmpfs_node *node, struct vnode **vpp)
    338 {
    339 	int error;
    340 	struct vnode *nvp;
    341 	struct vnode *vp;
    342 
    343 	/* If there is already a vnode, then lock it. */
    344 	for (;;) {
    345 		mutex_enter(&node->tn_vlock);
    346 		if ((vp = node->tn_vnode) != NULL) {
    347 			mutex_enter(&vp->v_interlock);
    348 			mutex_exit(&node->tn_vlock);
    349 			error = vget(vp, LK_EXCLUSIVE | LK_INTERLOCK);
    350 			if (error == ENOENT) {
    351 				/* vnode was reclaimed. */
    352 				continue;
    353 			}
    354 			*vpp = vp;
    355 			return error;
    356 		}
    357 		break;
    358 	}
    359 
    360 	/* Get a new vnode and associate it with our node. */
    361 	error = getnewvnode(VT_TMPFS, mp, tmpfs_vnodeop_p, &vp);
    362 	if (error != 0) {
    363 		mutex_exit(&node->tn_vlock);
    364 		return error;
    365 	}
    366 
    367 	error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    368 	if (error != 0) {
    369 		mutex_exit(&node->tn_vlock);
    370 		ungetnewvnode(vp);
    371 		return error;
    372 	}
    373 
    374 	vp->v_type = node->tn_type;
    375 
    376 	/* Type-specific initialization. */
    377 	switch (node->tn_type) {
    378 	case VBLK:
    379 		/* FALLTHROUGH */
    380 	case VCHR:
    381 		vp->v_op = tmpfs_specop_p;
    382 		nvp = checkalias(vp, node->tn_spec.tn_dev.tn_rdev, mp);
    383 		if (nvp != NULL) {
    384 			/* Discard unneeded vnode, but save its inode. */
    385 			nvp->v_data = node;
    386 
    387 			/* XXX spec_vnodeops has no locking, so we have to
    388 			 * do it explicitly. */
    389 			vp->v_vflag &= ~VV_LOCKSWORK;
    390 			VOP_UNLOCK(vp, 0);
    391 			vp->v_op = spec_vnodeop_p;
    392 			vgone(vp);
    393 
    394 			/* Reinitialize aliased node. */
    395 			vp = nvp;
    396 			error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    397 			if (error != 0) {
    398 				mutex_exit(&node->tn_vlock);
    399 				return error;
    400 			}
    401 		}
    402 		break;
    403 
    404 	case VDIR:
    405 		vp->v_vflag |= node->tn_spec.tn_dir.tn_parent == node ?
    406 		    VV_ROOT : 0;
    407 		break;
    408 
    409 	case VFIFO:
    410 		vp->v_op = tmpfs_fifoop_p;
    411 		break;
    412 
    413 	case VLNK:
    414 		/* FALLTHROUGH */
    415 	case VREG:
    416 		/* FALLTHROUGH */
    417 	case VSOCK:
    418 		break;
    419 
    420 	default:
    421 		KASSERT(0);
    422 	}
    423 
    424 	uvm_vnp_setsize(vp, node->tn_size);
    425 	vp->v_data = node;
    426 	node->tn_vnode = vp;
    427 	mutex_exit(&node->tn_vlock);
    428 	*vpp = vp;
    429 
    430 	KASSERT(IFF(error == 0, *vpp != NULL && VOP_ISLOCKED(*vpp)));
    431 	KASSERT(*vpp == node->tn_vnode);
    432 
    433 	return error;
    434 }
    435 
    436 /* --------------------------------------------------------------------- */
    437 
    438 /*
    439  * Destroys the association between the vnode vp and the node it
    440  * references.
    441  */
    442 void
    443 tmpfs_free_vp(struct vnode *vp)
    444 {
    445 	struct tmpfs_node *node;
    446 
    447 	node = VP_TO_TMPFS_NODE(vp);
    448 
    449 	mutex_enter(&node->tn_vlock);
    450 	node->tn_vnode = NULL;
    451 	mutex_exit(&node->tn_vlock);
    452 	vp->v_data = NULL;
    453 }
    454 
    455 /* --------------------------------------------------------------------- */
    456 
    457 /*
    458  * Allocates a new file of type 'type' and adds it to the parent directory
    459  * 'dvp'; this addition is done using the component name given in 'cnp'.
    460  * The ownership of the new file is automatically assigned based on the
    461  * credentials of the caller (through 'cnp'), the group is set based on
    462  * the parent directory and the mode is determined from the 'vap' argument.
    463  * If successful, *vpp holds a vnode to the newly created file and zero
    464  * is returned.  Otherwise *vpp is NULL and the function returns an
    465  * appropriate error code.
    466  */
    467 int
    468 tmpfs_alloc_file(struct vnode *dvp, struct vnode **vpp, struct vattr *vap,
    469     struct componentname *cnp, char *target)
    470 {
    471 	int error;
    472 	struct tmpfs_dirent *de;
    473 	struct tmpfs_mount *tmp;
    474 	struct tmpfs_node *dnode;
    475 	struct tmpfs_node *node;
    476 	struct tmpfs_node *parent;
    477 
    478 	KASSERT(VOP_ISLOCKED(dvp));
    479 	KASSERT(cnp->cn_flags & HASBUF);
    480 
    481 	tmp = VFS_TO_TMPFS(dvp->v_mount);
    482 	dnode = VP_TO_TMPFS_DIR(dvp);
    483 	*vpp = NULL;
    484 
    485 	/* If the entry we are creating is a directory, we cannot overflow
    486 	 * the number of links of its parent, because it will get a new
    487 	 * link. */
    488 	if (vap->va_type == VDIR) {
    489 		/* Ensure that we do not overflow the maximum number of links
    490 		 * imposed by the system. */
    491 		KASSERT(dnode->tn_links <= LINK_MAX);
    492 		if (dnode->tn_links == LINK_MAX) {
    493 			error = EMLINK;
    494 			goto out;
    495 		}
    496 
    497 		parent = dnode;
    498 	} else
    499 		parent = NULL;
    500 
    501 	/* Allocate a node that represents the new file. */
    502 	error = tmpfs_alloc_node(tmp, vap->va_type, kauth_cred_geteuid(cnp->cn_cred),
    503 	    dnode->tn_gid, vap->va_mode, parent, target, vap->va_rdev, &node);
    504 	if (error != 0)
    505 		goto out;
    506 
    507 	/* Allocate a directory entry that points to the new file. */
    508 	error = tmpfs_alloc_dirent(tmp, node, cnp->cn_nameptr, cnp->cn_namelen,
    509 	    &de);
    510 	if (error != 0) {
    511 		tmpfs_free_node(tmp, node);
    512 		goto out;
    513 	}
    514 
    515 	/* Allocate a vnode for the new file. */
    516 	error = tmpfs_alloc_vp(dvp->v_mount, node, vpp);
    517 	if (error != 0) {
    518 		tmpfs_free_dirent(tmp, de, true);
    519 		tmpfs_free_node(tmp, node);
    520 		goto out;
    521 	}
    522 
    523 	/* Now that all required items are allocated, we can proceed to
    524 	 * insert the new node into the directory, an operation that
    525 	 * cannot fail. */
    526 	tmpfs_dir_attach(dvp, de);
    527 	if (vap->va_type == VDIR) {
    528 		VN_KNOTE(dvp, NOTE_LINK);
    529 		dnode->tn_links++;
    530 		KASSERT(dnode->tn_links <= LINK_MAX);
    531 	}
    532 
    533 out:
    534 	if (error != 0 || !(cnp->cn_flags & SAVESTART))
    535 		PNBUF_PUT(cnp->cn_pnbuf);
    536 	vput(dvp);
    537 
    538 	KASSERT(IFF(error == 0, *vpp != NULL));
    539 
    540 	return error;
    541 }
    542 
    543 /* --------------------------------------------------------------------- */
    544 
    545 /*
    546  * Attaches the directory entry de to the directory represented by vp.
    547  * Note that this does not change the link count of the node pointed by
    548  * the directory entry, as this is done by tmpfs_alloc_dirent.
    549  *
    550  * As the "parent" directory changes, interested parties are notified of
    551  * a write to it.
    552  */
    553 void
    554 tmpfs_dir_attach(struct vnode *vp, struct tmpfs_dirent *de)
    555 {
    556 	struct tmpfs_node *dnode;
    557 
    558 	dnode = VP_TO_TMPFS_DIR(vp);
    559 
    560 	TAILQ_INSERT_TAIL(&dnode->tn_spec.tn_dir.tn_dir, de, td_entries);
    561 	dnode->tn_size += sizeof(struct tmpfs_dirent);
    562 	dnode->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \
    563 	    TMPFS_NODE_MODIFIED;
    564 	uvm_vnp_setsize(vp, dnode->tn_size);
    565 
    566 	VN_KNOTE(vp, NOTE_WRITE);
    567 }
    568 
    569 /* --------------------------------------------------------------------- */
    570 
    571 /*
    572  * Detaches the directory entry de from the directory represented by vp.
    573  * Note that this does not change the link count of the node pointed by
    574  * the directory entry, as this is done by tmpfs_free_dirent.
    575  *
    576  * As the "parent" directory changes, interested parties are notified of
    577  * a write to it.
    578  */
    579 void
    580 tmpfs_dir_detach(struct vnode *vp, struct tmpfs_dirent *de)
    581 {
    582 	struct tmpfs_node *dnode;
    583 
    584 	KASSERT(VOP_ISLOCKED(vp));
    585 
    586 	dnode = VP_TO_TMPFS_DIR(vp);
    587 
    588 	if (dnode->tn_spec.tn_dir.tn_readdir_lastp == de) {
    589 		dnode->tn_spec.tn_dir.tn_readdir_lastn = 0;
    590 		dnode->tn_spec.tn_dir.tn_readdir_lastp = NULL;
    591 	}
    592 
    593 	TAILQ_REMOVE(&dnode->tn_spec.tn_dir.tn_dir, de, td_entries);
    594 	dnode->tn_size -= sizeof(struct tmpfs_dirent);
    595 	dnode->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \
    596 	    TMPFS_NODE_MODIFIED;
    597 	uvm_vnp_setsize(vp, dnode->tn_size);
    598 
    599 	VN_KNOTE(vp, NOTE_WRITE);
    600 }
    601 
    602 /* --------------------------------------------------------------------- */
    603 
    604 /*
    605  * Looks for a directory entry in the directory represented by node.
    606  * 'cnp' describes the name of the entry to look for.  Note that the .
    607  * and .. components are not allowed as they do not physically exist
    608  * within directories.
    609  *
    610  * Returns a pointer to the entry when found, otherwise NULL.
    611  */
    612 struct tmpfs_dirent *
    613 tmpfs_dir_lookup(struct tmpfs_node *node, struct componentname *cnp)
    614 {
    615 	bool found;
    616 	struct tmpfs_dirent *de;
    617 
    618 	KASSERT(IMPLIES(cnp->cn_namelen == 1, cnp->cn_nameptr[0] != '.'));
    619 	KASSERT(IMPLIES(cnp->cn_namelen == 2, !(cnp->cn_nameptr[0] == '.' &&
    620 	    cnp->cn_nameptr[1] == '.')));
    621 	TMPFS_VALIDATE_DIR(node);
    622 
    623 	node->tn_status |= TMPFS_NODE_ACCESSED;
    624 
    625 	found = 0;
    626 	TAILQ_FOREACH(de, &node->tn_spec.tn_dir.tn_dir, td_entries) {
    627 		KASSERT(cnp->cn_namelen < 0xffff);
    628 		if (de->td_namelen == (uint16_t)cnp->cn_namelen &&
    629 		    memcmp(de->td_name, cnp->cn_nameptr, de->td_namelen) == 0) {
    630 			found = 1;
    631 			break;
    632 		}
    633 	}
    634 
    635 	return found ? de : NULL;
    636 }
    637 
    638 /* --------------------------------------------------------------------- */
    639 
    640 /*
    641  * Helper function for tmpfs_readdir.  Creates a '.' entry for the given
    642  * directory and returns it in the uio space.  The function returns 0
    643  * on success, -1 if there was not enough space in the uio structure to
    644  * hold the directory entry or an appropriate error code if another
    645  * error happens.
    646  */
    647 int
    648 tmpfs_dir_getdotdent(struct tmpfs_node *node, struct uio *uio)
    649 {
    650 	int error;
    651 	struct dirent *dentp;
    652 
    653 	TMPFS_VALIDATE_DIR(node);
    654 	KASSERT(uio->uio_offset == TMPFS_DIRCOOKIE_DOT);
    655 
    656 	dentp = kmem_zalloc(sizeof(struct dirent), KM_SLEEP);
    657 
    658 	dentp->d_fileno = node->tn_id;
    659 	dentp->d_type = DT_DIR;
    660 	dentp->d_namlen = 1;
    661 	dentp->d_name[0] = '.';
    662 	dentp->d_name[1] = '\0';
    663 	dentp->d_reclen = _DIRENT_SIZE(dentp);
    664 
    665 	if (dentp->d_reclen > uio->uio_resid)
    666 		error = -1;
    667 	else {
    668 		error = uiomove(dentp, dentp->d_reclen, uio);
    669 		if (error == 0)
    670 			uio->uio_offset = TMPFS_DIRCOOKIE_DOTDOT;
    671 	}
    672 
    673 	node->tn_status |= TMPFS_NODE_ACCESSED;
    674 
    675 	kmem_free(dentp, sizeof(struct dirent));
    676 	return error;
    677 }
    678 
    679 /* --------------------------------------------------------------------- */
    680 
    681 /*
    682  * Helper function for tmpfs_readdir.  Creates a '..' entry for the given
    683  * directory and returns it in the uio space.  The function returns 0
    684  * on success, -1 if there was not enough space in the uio structure to
    685  * hold the directory entry or an appropriate error code if another
    686  * error happens.
    687  */
    688 int
    689 tmpfs_dir_getdotdotdent(struct tmpfs_node *node, struct uio *uio)
    690 {
    691 	int error;
    692 	struct dirent *dentp;
    693 
    694 	TMPFS_VALIDATE_DIR(node);
    695 	KASSERT(uio->uio_offset == TMPFS_DIRCOOKIE_DOTDOT);
    696 
    697 	dentp = kmem_zalloc(sizeof(struct dirent), KM_SLEEP);
    698 
    699 	dentp->d_fileno = node->tn_spec.tn_dir.tn_parent->tn_id;
    700 	dentp->d_type = DT_DIR;
    701 	dentp->d_namlen = 2;
    702 	dentp->d_name[0] = '.';
    703 	dentp->d_name[1] = '.';
    704 	dentp->d_name[2] = '\0';
    705 	dentp->d_reclen = _DIRENT_SIZE(dentp);
    706 
    707 	if (dentp->d_reclen > uio->uio_resid)
    708 		error = -1;
    709 	else {
    710 		error = uiomove(dentp, dentp->d_reclen, uio);
    711 		if (error == 0) {
    712 			struct tmpfs_dirent *de;
    713 
    714 			de = TAILQ_FIRST(&node->tn_spec.tn_dir.tn_dir);
    715 			if (de == NULL)
    716 				uio->uio_offset = TMPFS_DIRCOOKIE_EOF;
    717 			else
    718 				uio->uio_offset = tmpfs_dircookie(de);
    719 		}
    720 	}
    721 
    722 	node->tn_status |= TMPFS_NODE_ACCESSED;
    723 
    724 	kmem_free(dentp, sizeof(struct dirent));
    725 	return error;
    726 }
    727 
    728 /* --------------------------------------------------------------------- */
    729 
    730 /*
    731  * Lookup a directory entry by its associated cookie.
    732  */
    733 struct tmpfs_dirent *
    734 tmpfs_dir_lookupbycookie(struct tmpfs_node *node, off_t cookie)
    735 {
    736 	struct tmpfs_dirent *de;
    737 
    738 	if (cookie == node->tn_spec.tn_dir.tn_readdir_lastn &&
    739 	    node->tn_spec.tn_dir.tn_readdir_lastp != NULL) {
    740 		return node->tn_spec.tn_dir.tn_readdir_lastp;
    741 	}
    742 
    743 	TAILQ_FOREACH(de, &node->tn_spec.tn_dir.tn_dir, td_entries) {
    744 		if (tmpfs_dircookie(de) == cookie) {
    745 			break;
    746 		}
    747 	}
    748 
    749 	return de;
    750 }
    751 
    752 /* --------------------------------------------------------------------- */
    753 
    754 /*
    755  * Helper function for tmpfs_readdir.  Returns as much directory entries
    756  * as can fit in the uio space.  The read starts at uio->uio_offset.
    757  * The function returns 0 on success, -1 if there was not enough space
    758  * in the uio structure to hold the directory entry or an appropriate
    759  * error code if another error happens.
    760  */
    761 int
    762 tmpfs_dir_getdents(struct tmpfs_node *node, struct uio *uio, off_t *cntp)
    763 {
    764 	int error;
    765 	off_t startcookie;
    766 	struct dirent *dentp;
    767 	struct tmpfs_dirent *de;
    768 
    769 	TMPFS_VALIDATE_DIR(node);
    770 
    771 	/* Locate the first directory entry we have to return.  We have cached
    772 	 * the last readdir in the node, so use those values if appropriate.
    773 	 * Otherwise do a linear scan to find the requested entry. */
    774 	startcookie = uio->uio_offset;
    775 	KASSERT(startcookie != TMPFS_DIRCOOKIE_DOT);
    776 	KASSERT(startcookie != TMPFS_DIRCOOKIE_DOTDOT);
    777 	if (startcookie == TMPFS_DIRCOOKIE_EOF) {
    778 		return 0;
    779 	} else {
    780 		de = tmpfs_dir_lookupbycookie(node, startcookie);
    781 	}
    782 	if (de == NULL) {
    783 		return EINVAL;
    784 	}
    785 
    786 	dentp = kmem_zalloc(sizeof(struct dirent), KM_SLEEP);
    787 
    788 	/* Read as much entries as possible; i.e., until we reach the end of
    789 	 * the directory or we exhaust uio space. */
    790 	do {
    791 		/* Create a dirent structure representing the current
    792 		 * tmpfs_node and fill it. */
    793 		dentp->d_fileno = de->td_node->tn_id;
    794 		switch (de->td_node->tn_type) {
    795 		case VBLK:
    796 			dentp->d_type = DT_BLK;
    797 			break;
    798 
    799 		case VCHR:
    800 			dentp->d_type = DT_CHR;
    801 			break;
    802 
    803 		case VDIR:
    804 			dentp->d_type = DT_DIR;
    805 			break;
    806 
    807 		case VFIFO:
    808 			dentp->d_type = DT_FIFO;
    809 			break;
    810 
    811 		case VLNK:
    812 			dentp->d_type = DT_LNK;
    813 			break;
    814 
    815 		case VREG:
    816 			dentp->d_type = DT_REG;
    817 			break;
    818 
    819 		case VSOCK:
    820 			dentp->d_type = DT_SOCK;
    821 			break;
    822 
    823 		default:
    824 			KASSERT(0);
    825 		}
    826 		dentp->d_namlen = de->td_namelen;
    827 		KASSERT(de->td_namelen < sizeof(dentp->d_name));
    828 		(void)memcpy(dentp->d_name, de->td_name, de->td_namelen);
    829 		dentp->d_name[de->td_namelen] = '\0';
    830 		dentp->d_reclen = _DIRENT_SIZE(dentp);
    831 
    832 		/* Stop reading if the directory entry we are treating is
    833 		 * bigger than the amount of data that can be returned. */
    834 		if (dentp->d_reclen > uio->uio_resid) {
    835 			error = -1;
    836 			break;
    837 		}
    838 
    839 		/* Copy the new dirent structure into the output buffer and
    840 		 * advance pointers. */
    841 		error = uiomove(dentp, dentp->d_reclen, uio);
    842 
    843 		(*cntp)++;
    844 		de = TAILQ_NEXT(de, td_entries);
    845 	} while (error == 0 && uio->uio_resid > 0 && de != NULL);
    846 
    847 	/* Update the offset and cache. */
    848 	if (de == NULL) {
    849 		uio->uio_offset = TMPFS_DIRCOOKIE_EOF;
    850 		node->tn_spec.tn_dir.tn_readdir_lastn = 0;
    851 		node->tn_spec.tn_dir.tn_readdir_lastp = NULL;
    852 	} else {
    853 		node->tn_spec.tn_dir.tn_readdir_lastn = uio->uio_offset =
    854 		    tmpfs_dircookie(de);
    855 		node->tn_spec.tn_dir.tn_readdir_lastp = de;
    856 	}
    857 
    858 	node->tn_status |= TMPFS_NODE_ACCESSED;
    859 
    860 	kmem_free(dentp, sizeof(struct dirent));
    861 	return error;
    862 }
    863 
    864 /* --------------------------------------------------------------------- */
    865 
    866 /*
    867  * Resizes the aobj associated to the regular file pointed to by vp to
    868  * the size newsize.  'vp' must point to a vnode that represents a regular
    869  * file.  'newsize' must be positive.
    870  *
    871  * If the file is extended, the appropriate kevent is raised.  This does
    872  * not rise a write event though because resizing is not the same as
    873  * writing.
    874  *
    875  * Returns zero on success or an appropriate error code on failure.
    876  */
    877 int
    878 tmpfs_reg_resize(struct vnode *vp, off_t newsize)
    879 {
    880 	int error;
    881 	u_int newpages, oldpages;
    882 	struct tmpfs_mount *tmp;
    883 	struct tmpfs_node *node;
    884 	off_t oldsize;
    885 
    886 	KASSERT(vp->v_type == VREG);
    887 	KASSERT(newsize >= 0);
    888 
    889 	node = VP_TO_TMPFS_NODE(vp);
    890 	tmp = VFS_TO_TMPFS(vp->v_mount);
    891 
    892 	/* Convert the old and new sizes to the number of pages needed to
    893 	 * store them.  It may happen that we do not need to do anything
    894 	 * because the last allocated page can accommodate the change on
    895 	 * its own. */
    896 	oldsize = node->tn_size;
    897 	oldpages = round_page(oldsize) / PAGE_SIZE;
    898 	KASSERT(oldpages == node->tn_spec.tn_reg.tn_aobj_pages);
    899 	newpages = round_page(newsize) / PAGE_SIZE;
    900 
    901 	if (newpages > oldpages &&
    902 	    (ssize_t)(newpages - oldpages) > TMPFS_PAGES_AVAIL(tmp)) {
    903 		error = ENOSPC;
    904 		goto out;
    905 	}
    906 	atomic_add_int(&tmp->tm_pages_used, newpages - oldpages);
    907 
    908 	if (newsize < oldsize) {
    909 		int zerolen = MIN(round_page(newsize), node->tn_size) - newsize;
    910 
    911 		/*
    912 		 * zero out the truncated part of the last page.
    913 		 */
    914 
    915 		uvm_vnp_zerorange(vp, newsize, zerolen);
    916 	}
    917 
    918 	node->tn_spec.tn_reg.tn_aobj_pages = newpages;
    919 	node->tn_size = newsize;
    920 	uvm_vnp_setsize(vp, newsize);
    921 
    922 	/*
    923 	 * free "backing store"
    924 	 */
    925 
    926 	if (newpages < oldpages) {
    927 		struct uvm_object *uobj;
    928 
    929 		uobj = node->tn_spec.tn_reg.tn_aobj;
    930 
    931 		mutex_enter(&uobj->vmobjlock);
    932 		uao_dropswap_range(uobj, newpages, oldpages);
    933 		mutex_exit(&uobj->vmobjlock);
    934 	}
    935 
    936 	error = 0;
    937 
    938 	if (newsize > oldsize)
    939 		VN_KNOTE(vp, NOTE_EXTEND);
    940 
    941 out:
    942 	return error;
    943 }
    944 
    945 /* --------------------------------------------------------------------- */
    946 
    947 /*
    948  * Returns information about the number of available memory pages,
    949  * including physical and virtual ones.
    950  *
    951  * If 'total' is true, the value returned is the total amount of memory
    952  * pages configured for the system (either in use or free).
    953  * If it is FALSE, the value returned is the amount of free memory pages.
    954  *
    955  * Remember to remove TMPFS_PAGES_RESERVED from the returned value to avoid
    956  * excessive memory usage.
    957  *
    958  */
    959 size_t
    960 tmpfs_mem_info(bool total)
    961 {
    962 	size_t size;
    963 
    964 	size = 0;
    965 	size += uvmexp.swpgavail;
    966 	if (!total) {
    967 		size -= uvmexp.swpgonly;
    968 	}
    969 	size += uvmexp.free;
    970 	size += uvmexp.filepages;
    971 	if (size > uvmexp.wired) {
    972 		size -= uvmexp.wired;
    973 	} else {
    974 		size = 0;
    975 	}
    976 
    977 	return size;
    978 }
    979 
    980 /* --------------------------------------------------------------------- */
    981 
    982 /*
    983  * Change flags of the given vnode.
    984  * Caller should execute tmpfs_update on vp after a successful execution.
    985  * The vnode must be locked on entry and remain locked on exit.
    986  */
    987 int
    988 tmpfs_chflags(struct vnode *vp, int flags, kauth_cred_t cred, struct lwp *l)
    989 {
    990 	int error;
    991 	struct tmpfs_node *node;
    992 
    993 	KASSERT(VOP_ISLOCKED(vp));
    994 
    995 	node = VP_TO_TMPFS_NODE(vp);
    996 
    997 	/* Disallow this operation if the file system is mounted read-only. */
    998 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
    999 		return EROFS;
   1000 
   1001 	/* XXX: The following comes from UFS code, and can be found in
   1002 	 * several other file systems.  Shouldn't this be centralized
   1003 	 * somewhere? */
   1004 	if (kauth_cred_geteuid(cred) != node->tn_uid &&
   1005 	    (error = kauth_authorize_generic(cred, KAUTH_GENERIC_ISSUSER,
   1006 	    NULL)))
   1007 		return error;
   1008 	if (kauth_authorize_generic(cred, KAUTH_GENERIC_ISSUSER, NULL) == 0) {
   1009 		/* The super-user is only allowed to change flags if the file
   1010 		 * wasn't protected before and the securelevel is zero. */
   1011 		if ((node->tn_flags & (SF_IMMUTABLE | SF_APPEND)) &&
   1012 		    kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_CHSYSFLAGS,
   1013 		     0, NULL, NULL, NULL))
   1014 			return EPERM;
   1015 		node->tn_flags = flags;
   1016 	} else {
   1017 		/* Regular users can change flags provided they only want to
   1018 		 * change user-specific ones, not those reserved for the
   1019 		 * super-user. */
   1020 		if ((node->tn_flags & (SF_IMMUTABLE | SF_APPEND)) ||
   1021 		    (flags & UF_SETTABLE) != flags)
   1022 			return EPERM;
   1023 		if ((node->tn_flags & SF_SETTABLE) != (flags & SF_SETTABLE))
   1024 			return EPERM;
   1025 		node->tn_flags &= SF_SETTABLE;
   1026 		node->tn_flags |= (flags & UF_SETTABLE);
   1027 	}
   1028 
   1029 	node->tn_status |= TMPFS_NODE_CHANGED;
   1030 	VN_KNOTE(vp, NOTE_ATTRIB);
   1031 
   1032 	KASSERT(VOP_ISLOCKED(vp));
   1033 
   1034 	return 0;
   1035 }
   1036 
   1037 /* --------------------------------------------------------------------- */
   1038 
   1039 /*
   1040  * Change access mode on the given vnode.
   1041  * Caller should execute tmpfs_update on vp after a successful execution.
   1042  * The vnode must be locked on entry and remain locked on exit.
   1043  */
   1044 int
   1045 tmpfs_chmod(struct vnode *vp, mode_t mode, kauth_cred_t cred, struct lwp *l)
   1046 {
   1047 	int error, ismember = 0;
   1048 	struct tmpfs_node *node;
   1049 
   1050 	KASSERT(VOP_ISLOCKED(vp));
   1051 
   1052 	node = VP_TO_TMPFS_NODE(vp);
   1053 
   1054 	/* Disallow this operation if the file system is mounted read-only. */
   1055 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
   1056 		return EROFS;
   1057 
   1058 	/* Immutable or append-only files cannot be modified, either. */
   1059 	if (node->tn_flags & (IMMUTABLE | APPEND))
   1060 		return EPERM;
   1061 
   1062 	/* XXX: The following comes from UFS code, and can be found in
   1063 	 * several other file systems.  Shouldn't this be centralized
   1064 	 * somewhere? */
   1065 	if (kauth_cred_geteuid(cred) != node->tn_uid &&
   1066 	    (error = kauth_authorize_generic(cred, KAUTH_GENERIC_ISSUSER,
   1067 	    NULL)))
   1068 		return error;
   1069 	if (kauth_authorize_generic(cred, KAUTH_GENERIC_ISSUSER, NULL) != 0) {
   1070 		if (vp->v_type != VDIR && (mode & S_ISTXT))
   1071 			return EFTYPE;
   1072 
   1073 		if ((kauth_cred_ismember_gid(cred, node->tn_gid,
   1074 		    &ismember) != 0 || !ismember) && (mode & S_ISGID))
   1075 			return EPERM;
   1076 	}
   1077 
   1078 	node->tn_mode = (mode & ALLPERMS);
   1079 
   1080 	node->tn_status |= TMPFS_NODE_CHANGED;
   1081 	VN_KNOTE(vp, NOTE_ATTRIB);
   1082 
   1083 	KASSERT(VOP_ISLOCKED(vp));
   1084 
   1085 	return 0;
   1086 }
   1087 
   1088 /* --------------------------------------------------------------------- */
   1089 
   1090 /*
   1091  * Change ownership of the given vnode.  At least one of uid or gid must
   1092  * be different than VNOVAL.  If one is set to that value, the attribute
   1093  * is unchanged.
   1094  * Caller should execute tmpfs_update on vp after a successful execution.
   1095  * The vnode must be locked on entry and remain locked on exit.
   1096  */
   1097 int
   1098 tmpfs_chown(struct vnode *vp, uid_t uid, gid_t gid, kauth_cred_t cred,
   1099     struct lwp *l)
   1100 {
   1101 	int error, ismember = 0;
   1102 	struct tmpfs_node *node;
   1103 
   1104 	KASSERT(VOP_ISLOCKED(vp));
   1105 
   1106 	node = VP_TO_TMPFS_NODE(vp);
   1107 
   1108 	/* Assign default values if they are unknown. */
   1109 	KASSERT(uid != VNOVAL || gid != VNOVAL);
   1110 	if (uid == VNOVAL)
   1111 		uid = node->tn_uid;
   1112 	if (gid == VNOVAL)
   1113 		gid = node->tn_gid;
   1114 	KASSERT(uid != VNOVAL && gid != VNOVAL);
   1115 
   1116 	/* Disallow this operation if the file system is mounted read-only. */
   1117 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
   1118 		return EROFS;
   1119 
   1120 	/* Immutable or append-only files cannot be modified, either. */
   1121 	if (node->tn_flags & (IMMUTABLE | APPEND))
   1122 		return EPERM;
   1123 
   1124 	/* XXX: The following comes from UFS code, and can be found in
   1125 	 * several other file systems.  Shouldn't this be centralized
   1126 	 * somewhere? */
   1127 	if ((kauth_cred_geteuid(cred) != node->tn_uid || uid != node->tn_uid ||
   1128 	    (gid != node->tn_gid && !(kauth_cred_getegid(cred) == node->tn_gid ||
   1129 	    (kauth_cred_ismember_gid(cred, gid, &ismember) == 0 && ismember)))) &&
   1130 	    ((error = kauth_authorize_generic(cred, KAUTH_GENERIC_ISSUSER,
   1131 	    NULL)) != 0))
   1132 		return error;
   1133 
   1134 	node->tn_uid = uid;
   1135 	node->tn_gid = gid;
   1136 
   1137 	node->tn_status |= TMPFS_NODE_CHANGED;
   1138 	VN_KNOTE(vp, NOTE_ATTRIB);
   1139 
   1140 	KASSERT(VOP_ISLOCKED(vp));
   1141 
   1142 	return 0;
   1143 }
   1144 
   1145 /* --------------------------------------------------------------------- */
   1146 
   1147 /*
   1148  * Change size of the given vnode.
   1149  * Caller should execute tmpfs_update on vp after a successful execution.
   1150  * The vnode must be locked on entry and remain locked on exit.
   1151  */
   1152 int
   1153 tmpfs_chsize(struct vnode *vp, u_quad_t size, kauth_cred_t cred,
   1154     struct lwp *l)
   1155 {
   1156 	int error;
   1157 	struct tmpfs_node *node;
   1158 
   1159 	KASSERT(VOP_ISLOCKED(vp));
   1160 
   1161 	node = VP_TO_TMPFS_NODE(vp);
   1162 
   1163 	/* Decide whether this is a valid operation based on the file type. */
   1164 	error = 0;
   1165 	switch (vp->v_type) {
   1166 	case VDIR:
   1167 		return EISDIR;
   1168 
   1169 	case VREG:
   1170 		if (vp->v_mount->mnt_flag & MNT_RDONLY)
   1171 			return EROFS;
   1172 		break;
   1173 
   1174 	case VBLK:
   1175 		/* FALLTHROUGH */
   1176 	case VCHR:
   1177 		/* FALLTHROUGH */
   1178 	case VFIFO:
   1179 		/* Allow modifications of special files even if in the file
   1180 		 * system is mounted read-only (we are not modifying the
   1181 		 * files themselves, but the objects they represent). */
   1182 		return 0;
   1183 
   1184 	default:
   1185 		/* Anything else is unsupported. */
   1186 		return EOPNOTSUPP;
   1187 	}
   1188 
   1189 	/* Immutable or append-only files cannot be modified, either. */
   1190 	if (node->tn_flags & (IMMUTABLE | APPEND))
   1191 		return EPERM;
   1192 
   1193 	error = tmpfs_truncate(vp, size);
   1194 	/* tmpfs_truncate will raise the NOTE_EXTEND and NOTE_ATTRIB kevents
   1195 	 * for us, as will update tn_status; no need to do that here. */
   1196 
   1197 	KASSERT(VOP_ISLOCKED(vp));
   1198 
   1199 	return error;
   1200 }
   1201 
   1202 /* --------------------------------------------------------------------- */
   1203 
   1204 /*
   1205  * Change access and modification times of the given vnode.
   1206  * Caller should execute tmpfs_update on vp after a successful execution.
   1207  * The vnode must be locked on entry and remain locked on exit.
   1208  */
   1209 int
   1210 tmpfs_chtimes(struct vnode *vp, struct timespec *atime, struct timespec *mtime,
   1211     int vaflags, kauth_cred_t cred, struct lwp *l)
   1212 {
   1213 	int error;
   1214 	struct tmpfs_node *node;
   1215 
   1216 	KASSERT(VOP_ISLOCKED(vp));
   1217 
   1218 	node = VP_TO_TMPFS_NODE(vp);
   1219 
   1220 	/* Disallow this operation if the file system is mounted read-only. */
   1221 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
   1222 		return EROFS;
   1223 
   1224 	/* Immutable or append-only files cannot be modified, either. */
   1225 	if (node->tn_flags & (IMMUTABLE | APPEND))
   1226 		return EPERM;
   1227 
   1228 	/* XXX: The following comes from UFS code, and can be found in
   1229 	 * several other file systems.  Shouldn't this be centralized
   1230 	 * somewhere? */
   1231 	if (kauth_cred_geteuid(cred) != node->tn_uid &&
   1232 	    (error = kauth_authorize_generic(cred, KAUTH_GENERIC_ISSUSER,
   1233 	    NULL)) && ((vaflags & VA_UTIMES_NULL) == 0 ||
   1234 	    (error = VOP_ACCESS(vp, VWRITE, cred))))
   1235 		return error;
   1236 
   1237 	if (atime->tv_sec != VNOVAL && atime->tv_nsec != VNOVAL)
   1238 		node->tn_status |= TMPFS_NODE_ACCESSED;
   1239 
   1240 	if (mtime->tv_sec != VNOVAL && mtime->tv_nsec != VNOVAL)
   1241 		node->tn_status |= TMPFS_NODE_MODIFIED;
   1242 
   1243 	tmpfs_update(vp, atime, mtime, 0);
   1244 	VN_KNOTE(vp, NOTE_ATTRIB);
   1245 
   1246 	KASSERT(VOP_ISLOCKED(vp));
   1247 
   1248 	return 0;
   1249 }
   1250 
   1251 /* --------------------------------------------------------------------- */
   1252 
   1253 /* Sync timestamps */
   1254 void
   1255 tmpfs_itimes(struct vnode *vp, const struct timespec *acc,
   1256     const struct timespec *mod)
   1257 {
   1258 	struct timespec now;
   1259 	struct tmpfs_node *node;
   1260 
   1261 	node = VP_TO_TMPFS_NODE(vp);
   1262 
   1263 	if ((node->tn_status & (TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED |
   1264 	    TMPFS_NODE_CHANGED)) == 0)
   1265 		return;
   1266 
   1267 	getnanotime(&now);
   1268 	if (node->tn_status & TMPFS_NODE_ACCESSED) {
   1269 		if (acc == NULL)
   1270 			acc = &now;
   1271 		node->tn_atime = *acc;
   1272 	}
   1273 	if (node->tn_status & TMPFS_NODE_MODIFIED) {
   1274 		if (mod == NULL)
   1275 			mod = &now;
   1276 		node->tn_mtime = *mod;
   1277 	}
   1278 	if (node->tn_status & TMPFS_NODE_CHANGED)
   1279 		node->tn_ctime = now;
   1280 
   1281 	node->tn_status &=
   1282 	    ~(TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED | TMPFS_NODE_CHANGED);
   1283 }
   1284 
   1285 /* --------------------------------------------------------------------- */
   1286 
   1287 void
   1288 tmpfs_update(struct vnode *vp, const struct timespec *acc,
   1289     const struct timespec *mod, int flags)
   1290 {
   1291 
   1292 	struct tmpfs_node *node;
   1293 
   1294 	KASSERT(VOP_ISLOCKED(vp));
   1295 
   1296 	node = VP_TO_TMPFS_NODE(vp);
   1297 
   1298 #if 0
   1299 	if (flags & UPDATE_CLOSE)
   1300 		; /* XXX Need to do anything special? */
   1301 #endif
   1302 
   1303 	tmpfs_itimes(vp, acc, mod);
   1304 
   1305 	KASSERT(VOP_ISLOCKED(vp));
   1306 }
   1307 
   1308 /* --------------------------------------------------------------------- */
   1309 
   1310 int
   1311 tmpfs_truncate(struct vnode *vp, off_t length)
   1312 {
   1313 	bool extended;
   1314 	int error;
   1315 	struct tmpfs_node *node;
   1316 
   1317 	node = VP_TO_TMPFS_NODE(vp);
   1318 	extended = length > node->tn_size;
   1319 
   1320 	if (length < 0) {
   1321 		error = EINVAL;
   1322 		goto out;
   1323 	}
   1324 
   1325 	if (node->tn_size == length) {
   1326 		error = 0;
   1327 		goto out;
   1328 	}
   1329 
   1330 	error = tmpfs_reg_resize(vp, length);
   1331 	if (error == 0)
   1332 		node->tn_status |= TMPFS_NODE_CHANGED | TMPFS_NODE_MODIFIED;
   1333 
   1334 out:
   1335 	tmpfs_update(vp, NULL, NULL, 0);
   1336 
   1337 	return error;
   1338 }
   1339