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