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