Home | History | Annotate | Line # | Download | only in tmpfs
tmpfs_subr.c revision 1.89
      1 /*	$NetBSD: tmpfs_subr.c,v 1.89 2013/11/21 14:39:09 rmind Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2005-2013 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, and by Mindaugas Rasiukevicius.
     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: interfaces for inode and directory entry
     35  * construction, destruction and manipulation.
     36  *
     37  * Reference counting
     38  *
     39  *	The link count of inode (tmpfs_node_t::tn_links) is used as a
     40  *	reference counter.  However, it has slightly different semantics.
     41  *
     42  *	For directories - link count represents directory entries, which
     43  *	refer to the directories.  In other words, it represents the count
     44  *	of sub-directories.  It also takes into account the virtual '.'
     45  *	entry (which has no real entry in the list).  For files - link count
     46  *	represents the hard links.  Since only empty directories can be
     47  *	removed - link count aligns the reference counting requirements
     48  *	enough.  Note: to check whether directory is not empty, the inode
     49  *	size (tmpfs_node_t::tn_size) can be used.
     50  *
     51  *	The inode itself, as an object, gathers its first reference when
     52  *	directory entry is attached via tmpfs_dir_attach(9).  For instance,
     53  *	after regular tmpfs_create(), a file would have a link count of 1,
     54  *	while directory after tmpfs_mkdir() would have 2 (due to '.').
     55  *
     56  * Reclamation
     57  *
     58  *	It should be noted that tmpfs inodes rely on a combination of vnode
     59  *	reference counting and link counting.  That is, an inode can only be
     60  *	destroyed if its associated vnode is inactive.  The destruction is
     61  *	done on vnode reclamation i.e. tmpfs_reclaim().  It should be noted
     62  *	that tmpfs_node_t::tn_links being 0 is a destruction criterion.
     63  *
     64  *	If an inode has references within the file system (tn_links > 0) and
     65  *	its inactive vnode gets reclaimed/recycled - then the association is
     66  *	broken in tmpfs_reclaim().  In such case, an inode will always pass
     67  *	tmpfs_lookup() and thus tmpfs_vnode_get() to associate a new vnode.
     68  *
     69  * Lock order
     70  *
     71  *	tmpfs_node_t::tn_vlock ->
     72  *		vnode_t::v_vlock ->
     73  *			vnode_t::v_interlock
     74  */
     75 
     76 #include <sys/cdefs.h>
     77 __KERNEL_RCSID(0, "$NetBSD: tmpfs_subr.c,v 1.89 2013/11/21 14:39:09 rmind Exp $");
     78 
     79 #include <sys/param.h>
     80 #include <sys/cprng.h>
     81 #include <sys/dirent.h>
     82 #include <sys/event.h>
     83 #include <sys/kmem.h>
     84 #include <sys/mount.h>
     85 #include <sys/namei.h>
     86 #include <sys/time.h>
     87 #include <sys/stat.h>
     88 #include <sys/systm.h>
     89 #include <sys/vnode.h>
     90 #include <sys/kauth.h>
     91 #include <sys/atomic.h>
     92 
     93 #include <uvm/uvm.h>
     94 
     95 #include <miscfs/specfs/specdev.h>
     96 #include <miscfs/genfs/genfs.h>
     97 #include <fs/tmpfs/tmpfs.h>
     98 #include <fs/tmpfs/tmpfs_fifoops.h>
     99 #include <fs/tmpfs/tmpfs_specops.h>
    100 #include <fs/tmpfs/tmpfs_vnops.h>
    101 
    102 static void	tmpfs_dir_putseq(tmpfs_node_t *, tmpfs_dirent_t *);
    103 
    104 /*
    105  * tmpfs_alloc_node: allocate a new inode of a specified type and
    106  * insert it into the list of specified mount point.
    107  */
    108 int
    109 tmpfs_alloc_node(tmpfs_mount_t *tmp, enum vtype type, uid_t uid, gid_t gid,
    110     mode_t mode, char *target, dev_t rdev, tmpfs_node_t **node)
    111 {
    112 	tmpfs_node_t *nnode;
    113 
    114 	nnode = tmpfs_node_get(tmp);
    115 	if (nnode == NULL) {
    116 		return ENOSPC;
    117 	}
    118 
    119 	/* Initially, no references and no associations. */
    120 	nnode->tn_links = 0;
    121 	nnode->tn_vnode = NULL;
    122 	nnode->tn_dirent_hint = NULL;
    123 
    124 	/*
    125 	 * XXX Where the pool is backed by a map larger than (4GB *
    126 	 * sizeof(*nnode)), this may produce duplicate inode numbers
    127 	 * for applications that do not understand 64-bit ino_t.
    128 	 */
    129 	nnode->tn_id = (ino_t)((uintptr_t)nnode / sizeof(*nnode));
    130 	nnode->tn_gen = TMPFS_NODE_GEN_MASK & cprng_fast32();
    131 
    132 	/* Generic initialization. */
    133 	nnode->tn_type = type;
    134 	nnode->tn_size = 0;
    135 	nnode->tn_status = 0;
    136 	nnode->tn_flags = 0;
    137 	nnode->tn_lockf = NULL;
    138 
    139 	vfs_timestamp(&nnode->tn_atime);
    140 	nnode->tn_birthtime = nnode->tn_atime;
    141 	nnode->tn_ctime = nnode->tn_atime;
    142 	nnode->tn_mtime = nnode->tn_atime;
    143 
    144 	KASSERT(uid != VNOVAL && gid != VNOVAL && mode != VNOVAL);
    145 	nnode->tn_uid = uid;
    146 	nnode->tn_gid = gid;
    147 	nnode->tn_mode = mode;
    148 
    149 	/* Type-specific initialization. */
    150 	switch (nnode->tn_type) {
    151 	case VBLK:
    152 	case VCHR:
    153 		/* Character/block special device. */
    154 		KASSERT(rdev != VNOVAL);
    155 		nnode->tn_spec.tn_dev.tn_rdev = rdev;
    156 		break;
    157 	case VDIR:
    158 		/* Directory. */
    159 		TAILQ_INIT(&nnode->tn_spec.tn_dir.tn_dir);
    160 		nnode->tn_spec.tn_dir.tn_parent = NULL;
    161 		nnode->tn_spec.tn_dir.tn_seq_arena = NULL;
    162 		nnode->tn_spec.tn_dir.tn_next_seq = TMPFS_DIRSEQ_START;
    163 		nnode->tn_spec.tn_dir.tn_readdir_lastp = NULL;
    164 
    165 		/* Extra link count for the virtual '.' entry. */
    166 		nnode->tn_links++;
    167 		break;
    168 	case VFIFO:
    169 	case VSOCK:
    170 		break;
    171 	case VLNK:
    172 		/* Symbolic link.  Target specifies the file name. */
    173 		KASSERT(target != NULL);
    174 		nnode->tn_size = strlen(target);
    175 
    176 		if (nnode->tn_size == 0) {
    177 			/* Zero-length targets are supported. */
    178 			nnode->tn_spec.tn_lnk.tn_link = NULL;
    179 			break;
    180 		}
    181 
    182 		KASSERT(nnode->tn_size < MAXPATHLEN);
    183 		nnode->tn_size++; /* include the NUL terminator */
    184 
    185 		nnode->tn_spec.tn_lnk.tn_link =
    186 		    tmpfs_strname_alloc(tmp, nnode->tn_size);
    187 		if (nnode->tn_spec.tn_lnk.tn_link == NULL) {
    188 			tmpfs_node_put(tmp, nnode);
    189 			return ENOSPC;
    190 		}
    191 		memcpy(nnode->tn_spec.tn_lnk.tn_link, target, nnode->tn_size);
    192 		break;
    193 	case VREG:
    194 		/* Regular file.  Create an underlying UVM object. */
    195 		nnode->tn_spec.tn_reg.tn_aobj =
    196 		    uao_create(INT32_MAX - PAGE_SIZE, 0);
    197 		nnode->tn_spec.tn_reg.tn_aobj_pages = 0;
    198 		break;
    199 	default:
    200 		KASSERT(false);
    201 	}
    202 
    203 	mutex_init(&nnode->tn_vlock, MUTEX_DEFAULT, IPL_NONE);
    204 
    205 	mutex_enter(&tmp->tm_lock);
    206 	LIST_INSERT_HEAD(&tmp->tm_nodes, nnode, tn_entries);
    207 	mutex_exit(&tmp->tm_lock);
    208 
    209 	*node = nnode;
    210 	return 0;
    211 }
    212 
    213 /*
    214  * tmpfs_free_node: remove the inode from a list in the mount point and
    215  * destroy the inode structures.
    216  */
    217 void
    218 tmpfs_free_node(tmpfs_mount_t *tmp, tmpfs_node_t *node)
    219 {
    220 	size_t objsz;
    221 
    222 	mutex_enter(&tmp->tm_lock);
    223 	LIST_REMOVE(node, tn_entries);
    224 	mutex_exit(&tmp->tm_lock);
    225 
    226 	switch (node->tn_type) {
    227 	case VLNK:
    228 		if (node->tn_size > 0) {
    229 			tmpfs_strname_free(tmp, node->tn_spec.tn_lnk.tn_link,
    230 			    node->tn_size);
    231 		}
    232 		break;
    233 	case VREG:
    234 		/*
    235 		 * Calculate the size of inode data, decrease the used-memory
    236 		 * counter, and destroy the unerlying UVM object (if any).
    237 		 */
    238 		objsz = PAGE_SIZE * node->tn_spec.tn_reg.tn_aobj_pages;
    239 		if (objsz != 0) {
    240 			tmpfs_mem_decr(tmp, objsz);
    241 		}
    242 		if (node->tn_spec.tn_reg.tn_aobj != NULL) {
    243 			uao_detach(node->tn_spec.tn_reg.tn_aobj);
    244 		}
    245 		break;
    246 	case VDIR:
    247 		KASSERT(node->tn_spec.tn_dir.tn_seq_arena == NULL);
    248 		KASSERT(TAILQ_EMPTY(&node->tn_spec.tn_dir.tn_dir));
    249 		KASSERT(node->tn_spec.tn_dir.tn_parent == NULL ||
    250 		    node == tmp->tm_root);
    251 		break;
    252 	default:
    253 		break;
    254 	}
    255 
    256 	mutex_destroy(&node->tn_vlock);
    257 	tmpfs_node_put(tmp, node);
    258 }
    259 
    260 /*
    261  * tmpfs_vnode_get: allocate or reclaim a vnode for a specified inode.
    262  *
    263  * => Must be called with tmpfs_node_t::tn_vlock held.
    264  * => Returns vnode (*vpp) locked.
    265  */
    266 int
    267 tmpfs_vnode_get(struct mount *mp, tmpfs_node_t *node, vnode_t **vpp)
    268 {
    269 	vnode_t *vp;
    270 	kmutex_t *slock;
    271 	int error;
    272 again:
    273 	/* If there is already a vnode, try to reclaim it. */
    274 	if ((vp = node->tn_vnode) != NULL) {
    275 		atomic_or_32(&node->tn_gen, TMPFS_RECLAIMING_BIT);
    276 		mutex_enter(vp->v_interlock);
    277 		mutex_exit(&node->tn_vlock);
    278 		error = vget(vp, LK_EXCLUSIVE);
    279 		if (error == ENOENT) {
    280 			mutex_enter(&node->tn_vlock);
    281 			goto again;
    282 		}
    283 		atomic_and_32(&node->tn_gen, ~TMPFS_RECLAIMING_BIT);
    284 		*vpp = vp;
    285 		return error;
    286 	}
    287 	if (TMPFS_NODE_RECLAIMING(node)) {
    288 		atomic_and_32(&node->tn_gen, ~TMPFS_RECLAIMING_BIT);
    289 	}
    290 
    291 	/*
    292 	 * Get a new vnode and associate it with our inode.  Share the
    293 	 * lock with underlying UVM object, if there is one (VREG case).
    294 	 */
    295 	if (node->tn_type == VREG) {
    296 		struct uvm_object *uobj = node->tn_spec.tn_reg.tn_aobj;
    297 		slock = uobj->vmobjlock;
    298 	} else {
    299 		slock = NULL;
    300 	}
    301 	error = getnewvnode(VT_TMPFS, mp, tmpfs_vnodeop_p, slock, &vp);
    302 	if (error) {
    303 		mutex_exit(&node->tn_vlock);
    304 		return error;
    305 	}
    306 
    307 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    308 	vp->v_type = node->tn_type;
    309 
    310 	/* Type-specific initialization. */
    311 	switch (node->tn_type) {
    312 	case VBLK:
    313 	case VCHR:
    314 		vp->v_op = tmpfs_specop_p;
    315 		spec_node_init(vp, node->tn_spec.tn_dev.tn_rdev);
    316 		break;
    317 	case VDIR:
    318 		vp->v_vflag |= node->tn_spec.tn_dir.tn_parent == node ?
    319 		    VV_ROOT : 0;
    320 		break;
    321 	case VFIFO:
    322 		vp->v_op = tmpfs_fifoop_p;
    323 		break;
    324 	case VLNK:
    325 	case VREG:
    326 	case VSOCK:
    327 		break;
    328 	default:
    329 		KASSERT(false);
    330 	}
    331 
    332 	uvm_vnp_setsize(vp, node->tn_size);
    333 	vp->v_data = node;
    334 	node->tn_vnode = vp;
    335 	mutex_exit(&node->tn_vlock);
    336 
    337 	KASSERT(VOP_ISLOCKED(vp));
    338 	*vpp = vp;
    339 	return 0;
    340 }
    341 
    342 /*
    343  * tmpfs_alloc_file: allocate a new file of specified type and adds it
    344  * into the parent directory.
    345  *
    346  * => Credentials of the caller are used.
    347  */
    348 int
    349 tmpfs_alloc_file(vnode_t *dvp, vnode_t **vpp, struct vattr *vap,
    350     struct componentname *cnp, char *target)
    351 {
    352 	tmpfs_mount_t *tmp = VFS_TO_TMPFS(dvp->v_mount);
    353 	tmpfs_node_t *dnode = VP_TO_TMPFS_DIR(dvp), *node;
    354 	tmpfs_dirent_t *de, *wde;
    355 	int error;
    356 
    357 	KASSERT(VOP_ISLOCKED(dvp));
    358 	*vpp = NULL;
    359 
    360 	/* Check for the maximum number of links limit. */
    361 	if (vap->va_type == VDIR) {
    362 		/* Check for maximum links limit. */
    363 		if (dnode->tn_links == LINK_MAX) {
    364 			error = EMLINK;
    365 			goto out;
    366 		}
    367 		KASSERT(dnode->tn_links < LINK_MAX);
    368 	}
    369 
    370 	/* Allocate a node that represents the new file. */
    371 	error = tmpfs_alloc_node(tmp, vap->va_type, kauth_cred_geteuid(cnp->cn_cred),
    372 	    dnode->tn_gid, vap->va_mode, target, vap->va_rdev, &node);
    373 	if (error)
    374 		goto out;
    375 
    376 	/* Allocate a directory entry that points to the new file. */
    377 	error = tmpfs_alloc_dirent(tmp, cnp->cn_nameptr, cnp->cn_namelen, &de);
    378 	if (error) {
    379 		tmpfs_free_node(tmp, node);
    380 		goto out;
    381 	}
    382 
    383 	/* Get a vnode for the new file. */
    384 	mutex_enter(&node->tn_vlock);
    385 	error = tmpfs_vnode_get(dvp->v_mount, node, vpp);
    386 	if (error) {
    387 		tmpfs_free_dirent(tmp, de);
    388 		tmpfs_free_node(tmp, node);
    389 		goto out;
    390 	}
    391 
    392 	/* Remove whiteout before adding the new entry. */
    393 	if (cnp->cn_flags & ISWHITEOUT) {
    394 		wde = tmpfs_dir_lookup(dnode, cnp);
    395 		KASSERT(wde != NULL && wde->td_node == TMPFS_NODE_WHITEOUT);
    396 		tmpfs_dir_detach(dnode, wde);
    397 		tmpfs_free_dirent(tmp, wde);
    398 	}
    399 
    400 	/* Associate inode and attach the entry into the directory. */
    401 	tmpfs_dir_attach(dnode, de, node);
    402 
    403 	/* Make node opaque if requested. */
    404 	if (cnp->cn_flags & ISWHITEOUT)
    405 		node->tn_flags |= UF_OPAQUE;
    406 out:
    407 	vput(dvp);
    408 	return error;
    409 }
    410 
    411 /*
    412  * tmpfs_alloc_dirent: allocates a new directory entry for the inode.
    413  * The directory entry contains a path name component.
    414  */
    415 int
    416 tmpfs_alloc_dirent(tmpfs_mount_t *tmp, const char *name, uint16_t len,
    417     tmpfs_dirent_t **de)
    418 {
    419 	tmpfs_dirent_t *nde;
    420 
    421 	nde = tmpfs_dirent_get(tmp);
    422 	if (nde == NULL)
    423 		return ENOSPC;
    424 
    425 	nde->td_name = tmpfs_strname_alloc(tmp, len);
    426 	if (nde->td_name == NULL) {
    427 		tmpfs_dirent_put(tmp, nde);
    428 		return ENOSPC;
    429 	}
    430 	nde->td_namelen = len;
    431 	memcpy(nde->td_name, name, len);
    432 	nde->td_seq = TMPFS_DIRSEQ_NONE;
    433 
    434 	*de = nde;
    435 	return 0;
    436 }
    437 
    438 /*
    439  * tmpfs_free_dirent: free a directory entry.
    440  */
    441 void
    442 tmpfs_free_dirent(tmpfs_mount_t *tmp, tmpfs_dirent_t *de)
    443 {
    444 	KASSERT(de->td_node == NULL);
    445 	KASSERT(de->td_seq == TMPFS_DIRSEQ_NONE);
    446 	tmpfs_strname_free(tmp, de->td_name, de->td_namelen);
    447 	tmpfs_dirent_put(tmp, de);
    448 }
    449 
    450 /*
    451  * tmpfs_dir_attach: associate directory entry with a specified inode,
    452  * and attach the entry into the directory, specified by vnode.
    453  *
    454  * => Increases link count on the associated node.
    455  * => Increases link count on directory node, if our node is VDIR.
    456  *    It is caller's responsibility to check for the LINK_MAX limit.
    457  * => Triggers kqueue events here.
    458  */
    459 void
    460 tmpfs_dir_attach(tmpfs_node_t *dnode, tmpfs_dirent_t *de, tmpfs_node_t *node)
    461 {
    462 	vnode_t *dvp = dnode->tn_vnode;
    463 	int events = NOTE_WRITE;
    464 
    465 	KASSERT(dvp != NULL);
    466 	KASSERT(VOP_ISLOCKED(dvp));
    467 
    468 	/* Get a new sequence number. */
    469 	KASSERT(de->td_seq == TMPFS_DIRSEQ_NONE);
    470 	de->td_seq = tmpfs_dir_getseq(dnode, de);
    471 
    472 	/* Associate directory entry and the inode. */
    473 	de->td_node = node;
    474 	if (node != TMPFS_NODE_WHITEOUT) {
    475 		KASSERT(node->tn_links < LINK_MAX);
    476 		node->tn_links++;
    477 
    478 		/* Save the hint (might overwrite). */
    479 		node->tn_dirent_hint = de;
    480 	}
    481 
    482 	/* Insert the entry to the directory (parent of inode). */
    483 	TAILQ_INSERT_TAIL(&dnode->tn_spec.tn_dir.tn_dir, de, td_entries);
    484 	dnode->tn_size += sizeof(tmpfs_dirent_t);
    485 	dnode->tn_status |= TMPFS_NODE_STATUSALL;
    486 	uvm_vnp_setsize(dvp, dnode->tn_size);
    487 
    488 	if (node != TMPFS_NODE_WHITEOUT && node->tn_type == VDIR) {
    489 		/* Set parent. */
    490 		KASSERT(node->tn_spec.tn_dir.tn_parent == NULL);
    491 		node->tn_spec.tn_dir.tn_parent = dnode;
    492 
    493 		/* Increase the link count of parent. */
    494 		KASSERT(dnode->tn_links < LINK_MAX);
    495 		dnode->tn_links++;
    496 		events |= NOTE_LINK;
    497 
    498 		TMPFS_VALIDATE_DIR(node);
    499 	}
    500 	VN_KNOTE(dvp, events);
    501 }
    502 
    503 /*
    504  * tmpfs_dir_detach: disassociate directory entry and its inode,
    505  * and detach the entry from the directory, specified by vnode.
    506  *
    507  * => Decreases link count on the associated node.
    508  * => Decreases the link count on directory node, if our node is VDIR.
    509  * => Triggers kqueue events here.
    510  *
    511  * => Note: dvp and vp may be NULL only if called by tmpfs_unmount().
    512  */
    513 void
    514 tmpfs_dir_detach(tmpfs_node_t *dnode, tmpfs_dirent_t *de)
    515 {
    516 	tmpfs_node_t *node = de->td_node;
    517 	vnode_t *vp, *dvp = dnode->tn_vnode;
    518 	int events = NOTE_WRITE;
    519 
    520 	KASSERT(dvp == NULL || VOP_ISLOCKED(dvp));
    521 
    522 	if (__predict_true(node != TMPFS_NODE_WHITEOUT)) {
    523 		/* Deassociate the inode and entry. */
    524 		node->tn_dirent_hint = NULL;
    525 
    526 		KASSERT(node->tn_links > 0);
    527 		node->tn_links--;
    528 
    529 		if ((vp = node->tn_vnode) != NULL) {
    530 			KASSERT(VOP_ISLOCKED(vp));
    531 			VN_KNOTE(vp, node->tn_links ? NOTE_LINK : NOTE_DELETE);
    532 		}
    533 
    534 		/* If directory - decrease the link count of parent. */
    535 		if (node->tn_type == VDIR) {
    536 			KASSERT(node->tn_spec.tn_dir.tn_parent == dnode);
    537 			node->tn_spec.tn_dir.tn_parent = NULL;
    538 
    539 			KASSERT(dnode->tn_links > 0);
    540 			dnode->tn_links--;
    541 			events |= NOTE_LINK;
    542 		}
    543 	}
    544 	de->td_node = NULL;
    545 
    546 	/* Remove the entry from the directory. */
    547 	if (dnode->tn_spec.tn_dir.tn_readdir_lastp == de) {
    548 		dnode->tn_spec.tn_dir.tn_readdir_lastp = NULL;
    549 	}
    550 	TAILQ_REMOVE(&dnode->tn_spec.tn_dir.tn_dir, de, td_entries);
    551 
    552 	dnode->tn_size -= sizeof(tmpfs_dirent_t);
    553 	dnode->tn_status |= TMPFS_NODE_STATUSALL;
    554 	tmpfs_dir_putseq(dnode, de);
    555 
    556 	if (dvp) {
    557 		uvm_vnp_setsize(dvp, dnode->tn_size);
    558 		VN_KNOTE(dvp, events);
    559 	}
    560 }
    561 
    562 /*
    563  * tmpfs_dir_lookup: find a directory entry in the specified inode.
    564  *
    565  * Note that the . and .. components are not allowed as they do not
    566  * physically exist within directories.
    567  */
    568 tmpfs_dirent_t *
    569 tmpfs_dir_lookup(tmpfs_node_t *node, struct componentname *cnp)
    570 {
    571 	const char *name = cnp->cn_nameptr;
    572 	const uint16_t nlen = cnp->cn_namelen;
    573 	tmpfs_dirent_t *de;
    574 
    575 	KASSERT(VOP_ISLOCKED(node->tn_vnode));
    576 	KASSERT(nlen != 1 || !(name[0] == '.'));
    577 	KASSERT(nlen != 2 || !(name[0] == '.' && name[1] == '.'));
    578 	TMPFS_VALIDATE_DIR(node);
    579 
    580 	TAILQ_FOREACH(de, &node->tn_spec.tn_dir.tn_dir, td_entries) {
    581 		if (de->td_namelen != nlen)
    582 			continue;
    583 		if (memcmp(de->td_name, name, nlen) != 0)
    584 			continue;
    585 		break;
    586 	}
    587 	node->tn_status |= TMPFS_NODE_ACCESSED;
    588 	return de;
    589 }
    590 
    591 /*
    592  * tmpfs_dir_cached: get a cached directory entry if it is valid.  Used to
    593  * avoid unnecessary tmpfs_dir_lookup().
    594  *
    595  * => The vnode must be locked.
    596  */
    597 tmpfs_dirent_t *
    598 tmpfs_dir_cached(tmpfs_node_t *node)
    599 {
    600 	tmpfs_dirent_t *de = node->tn_dirent_hint;
    601 
    602 	KASSERT(VOP_ISLOCKED(node->tn_vnode));
    603 
    604 	if (de == NULL) {
    605 		return NULL;
    606 	}
    607 	KASSERT(de->td_node == node);
    608 
    609 	/*
    610 	 * Directories always have a valid hint.  For files, check if there
    611 	 * are any hard links.  If there are - hint might be invalid.
    612 	 */
    613 	return (node->tn_type != VDIR && node->tn_links > 1) ? NULL : de;
    614 }
    615 
    616 /*
    617  * tmpfs_dir_getseq: get a per-directory sequence number for the entry.
    618  *
    619  * => Shall not be larger than 2^31 for linux32 compatibility.
    620  */
    621 uint32_t
    622 tmpfs_dir_getseq(tmpfs_node_t *dnode, tmpfs_dirent_t *de)
    623 {
    624 	uint32_t seq = de->td_seq;
    625 	vmem_t *seq_arena;
    626 	vmem_addr_t off;
    627 	int error __diagused;
    628 
    629 	TMPFS_VALIDATE_DIR(dnode);
    630 
    631 	if (__predict_true(seq != TMPFS_DIRSEQ_NONE)) {
    632 		/* Already set. */
    633 		KASSERT(seq >= TMPFS_DIRSEQ_START);
    634 		return seq;
    635 	}
    636 
    637 	/*
    638 	 * The "." and ".." and the end-of-directory have reserved numbers.
    639 	 * The other sequence numbers are allocated as following:
    640 	 *
    641 	 * - The first half of the 2^31 is assigned incrementally.
    642 	 *
    643 	 * - If that range is exceeded, then the second half of 2^31
    644 	 * is used, but managed by vmem(9).
    645 	 */
    646 
    647 	seq = dnode->tn_spec.tn_dir.tn_next_seq;
    648 	KASSERT(seq >= TMPFS_DIRSEQ_START);
    649 
    650 	if (__predict_true(seq < TMPFS_DIRSEQ_END)) {
    651 		/* First half: just increment and return. */
    652 		dnode->tn_spec.tn_dir.tn_next_seq++;
    653 		return seq;
    654 	}
    655 
    656 	/*
    657 	 * First half exceeded, use the second half.  May need to create
    658 	 * vmem(9) arena for the directory first.
    659 	 */
    660 	if ((seq_arena = dnode->tn_spec.tn_dir.tn_seq_arena) == NULL) {
    661 		seq_arena = vmem_create("tmpfscoo", 0,
    662 		    TMPFS_DIRSEQ_END - 1, 1, NULL, NULL, NULL, 0,
    663 		    VM_SLEEP, IPL_NONE);
    664 		dnode->tn_spec.tn_dir.tn_seq_arena = seq_arena;
    665 		KASSERT(seq_arena != NULL);
    666 	}
    667 	error = vmem_alloc(seq_arena, 1, VM_SLEEP | VM_BESTFIT, &off);
    668 	KASSERT(error == 0);
    669 
    670 	KASSERT(off < TMPFS_DIRSEQ_END);
    671 	seq = off | TMPFS_DIRSEQ_END;
    672 	return seq;
    673 }
    674 
    675 static void
    676 tmpfs_dir_putseq(tmpfs_node_t *dnode, tmpfs_dirent_t *de)
    677 {
    678 	vmem_t *seq_arena = dnode->tn_spec.tn_dir.tn_seq_arena;
    679 	uint32_t seq = de->td_seq;
    680 
    681 	TMPFS_VALIDATE_DIR(dnode);
    682 
    683 	if (seq == TMPFS_DIRSEQ_NONE || seq < TMPFS_DIRSEQ_END) {
    684 		/* First half (or no sequence number set yet). */
    685 		KASSERT(de->td_seq >= TMPFS_DIRSEQ_START);
    686 	} else {
    687 		/* Second half. */
    688 		KASSERT(seq_arena != NULL);
    689 		KASSERT(seq >= TMPFS_DIRSEQ_END);
    690 		seq &= ~TMPFS_DIRSEQ_END;
    691 		vmem_free(seq_arena, seq, 1);
    692 	}
    693 	de->td_seq = TMPFS_DIRSEQ_NONE;
    694 
    695 	/* Empty?  We can reset. */
    696 	if (seq_arena && dnode->tn_size == 0) {
    697 		dnode->tn_spec.tn_dir.tn_seq_arena = NULL;
    698 		dnode->tn_spec.tn_dir.tn_next_seq = TMPFS_DIRSEQ_START;
    699 		vmem_destroy(seq_arena);
    700 	}
    701 }
    702 
    703 /*
    704  * tmpfs_dir_lookupbyseq: lookup a directory entry by the sequence number.
    705  */
    706 tmpfs_dirent_t *
    707 tmpfs_dir_lookupbyseq(tmpfs_node_t *node, off_t seq)
    708 {
    709 	tmpfs_dirent_t *de = node->tn_spec.tn_dir.tn_readdir_lastp;
    710 
    711 	TMPFS_VALIDATE_DIR(node);
    712 
    713 	/*
    714 	 * First, check the cache.  If does not match - perform a lookup.
    715 	 */
    716 	if (de && de->td_seq == seq) {
    717 		KASSERT(de->td_seq >= TMPFS_DIRSEQ_START);
    718 		KASSERT(de->td_seq != TMPFS_DIRSEQ_NONE);
    719 		return de;
    720 	}
    721 	TAILQ_FOREACH(de, &node->tn_spec.tn_dir.tn_dir, td_entries) {
    722 		KASSERT(de->td_seq >= TMPFS_DIRSEQ_START);
    723 		KASSERT(de->td_seq != TMPFS_DIRSEQ_NONE);
    724 		if (de->td_seq == seq)
    725 			return de;
    726 	}
    727 	return NULL;
    728 }
    729 
    730 /*
    731  * tmpfs_dir_getdotents: helper function for tmpfs_readdir() to get the
    732  * dot meta entries, that is, "." or "..".  Copy it to the UIO space.
    733  */
    734 static int
    735 tmpfs_dir_getdotents(tmpfs_node_t *node, struct dirent *dp, struct uio *uio)
    736 {
    737 	tmpfs_dirent_t *de;
    738 	off_t next = 0;
    739 	int error;
    740 
    741 	switch (uio->uio_offset) {
    742 	case TMPFS_DIRSEQ_DOT:
    743 		dp->d_fileno = node->tn_id;
    744 		strlcpy(dp->d_name, ".", sizeof(dp->d_name));
    745 		next = TMPFS_DIRSEQ_DOTDOT;
    746 		break;
    747 	case TMPFS_DIRSEQ_DOTDOT:
    748 		dp->d_fileno = node->tn_spec.tn_dir.tn_parent->tn_id;
    749 		strlcpy(dp->d_name, "..", sizeof(dp->d_name));
    750 		de = TAILQ_FIRST(&node->tn_spec.tn_dir.tn_dir);
    751 		next = de ? tmpfs_dir_getseq(node, de) : TMPFS_DIRSEQ_EOF;
    752 		break;
    753 	default:
    754 		KASSERT(false);
    755 	}
    756 	dp->d_type = DT_DIR;
    757 	dp->d_namlen = strlen(dp->d_name);
    758 	dp->d_reclen = _DIRENT_SIZE(dp);
    759 
    760 	if (dp->d_reclen > uio->uio_resid) {
    761 		return EJUSTRETURN;
    762 	}
    763 	if ((error = uiomove(dp, dp->d_reclen, uio)) != 0) {
    764 		return error;
    765 	}
    766 
    767 	uio->uio_offset = next;
    768 	return error;
    769 }
    770 
    771 /*
    772  * tmpfs_dir_getdents: helper function for tmpfs_readdir.
    773  *
    774  * => Returns as much directory entries as can fit in the uio space.
    775  * => The read starts at uio->uio_offset.
    776  */
    777 int
    778 tmpfs_dir_getdents(tmpfs_node_t *node, struct uio *uio, off_t *cntp)
    779 {
    780 	tmpfs_dirent_t *de;
    781 	struct dirent *dentp;
    782 	int error = 0;
    783 
    784 	KASSERT(VOP_ISLOCKED(node->tn_vnode));
    785 	TMPFS_VALIDATE_DIR(node);
    786 
    787 	/*
    788 	 * Allocate struct dirent and first check for the "." and "..".
    789 	 * Note: tmpfs_dir_getdotents() will "seek" for us.
    790 	 */
    791 	dentp = kmem_zalloc(sizeof(struct dirent), KM_SLEEP);
    792 
    793 	if (uio->uio_offset == TMPFS_DIRSEQ_DOT) {
    794 		if ((error = tmpfs_dir_getdotents(node, dentp, uio)) != 0) {
    795 			goto done;
    796 		}
    797 		(*cntp)++;
    798 	}
    799 	if (uio->uio_offset == TMPFS_DIRSEQ_DOTDOT) {
    800 		if ((error = tmpfs_dir_getdotents(node, dentp, uio)) != 0) {
    801 			goto done;
    802 		}
    803 		(*cntp)++;
    804 	}
    805 
    806 	/* Done if we reached the end. */
    807 	if (uio->uio_offset == TMPFS_DIRSEQ_EOF) {
    808 		goto done;
    809 	}
    810 
    811 	/* Locate the directory entry given by the given sequence number. */
    812 	de = tmpfs_dir_lookupbyseq(node, uio->uio_offset);
    813 	if (de == NULL) {
    814 		error = EINVAL;
    815 		goto done;
    816 	}
    817 
    818 	/*
    819 	 * Read as many entries as possible; i.e., until we reach the end
    820 	 * of the directory or we exhaust UIO space.
    821 	 */
    822 	do {
    823 		if (de->td_node == TMPFS_NODE_WHITEOUT) {
    824 			dentp->d_fileno = 1;
    825 			dentp->d_type = DT_WHT;
    826 		} else {
    827 			dentp->d_fileno = de->td_node->tn_id;
    828 			dentp->d_type = vtype2dt(de->td_node->tn_type);
    829 		}
    830 		dentp->d_namlen = de->td_namelen;
    831 		KASSERT(de->td_namelen < sizeof(dentp->d_name));
    832 		memcpy(dentp->d_name, de->td_name, de->td_namelen);
    833 		dentp->d_name[de->td_namelen] = '\0';
    834 		dentp->d_reclen = _DIRENT_SIZE(dentp);
    835 
    836 		if (dentp->d_reclen > uio->uio_resid) {
    837 			/* Exhausted UIO space. */
    838 			error = EJUSTRETURN;
    839 			break;
    840 		}
    841 
    842 		/* Copy out the directory entry and continue. */
    843 		error = uiomove(dentp, dentp->d_reclen, uio);
    844 		if (error) {
    845 			break;
    846 		}
    847 		(*cntp)++;
    848 		de = TAILQ_NEXT(de, td_entries);
    849 
    850 	} while (uio->uio_resid > 0 && de);
    851 
    852 	/* Cache the last entry or clear and mark EOF. */
    853 	uio->uio_offset = de ? tmpfs_dir_getseq(node, de) : TMPFS_DIRSEQ_EOF;
    854 	node->tn_spec.tn_dir.tn_readdir_lastp = de;
    855 done:
    856 	node->tn_status |= TMPFS_NODE_ACCESSED;
    857 	kmem_free(dentp, sizeof(struct dirent));
    858 
    859 	if (error == EJUSTRETURN) {
    860 		/* Exhausted UIO space - just return. */
    861 		error = 0;
    862 	}
    863 	KASSERT(error >= 0);
    864 	return error;
    865 }
    866 
    867 /*
    868  * tmpfs_reg_resize: resize the underlying UVM object associated with the
    869  * specified regular file.
    870  */
    871 int
    872 tmpfs_reg_resize(struct vnode *vp, off_t newsize)
    873 {
    874 	tmpfs_mount_t *tmp = VFS_TO_TMPFS(vp->v_mount);
    875 	tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp);
    876 	struct uvm_object *uobj = node->tn_spec.tn_reg.tn_aobj;
    877 	size_t newpages, oldpages;
    878 	off_t oldsize;
    879 
    880 	KASSERT(vp->v_type == VREG);
    881 	KASSERT(newsize >= 0);
    882 
    883 	oldsize = node->tn_size;
    884 	oldpages = round_page(oldsize) >> PAGE_SHIFT;
    885 	newpages = round_page(newsize) >> PAGE_SHIFT;
    886 	KASSERT(oldpages == node->tn_spec.tn_reg.tn_aobj_pages);
    887 
    888 	if (newpages > oldpages) {
    889 		/* Increase the used-memory counter if getting extra pages. */
    890 		if (!tmpfs_mem_incr(tmp, (newpages - oldpages) << PAGE_SHIFT)) {
    891 			return ENOSPC;
    892 		}
    893 	} else if (newsize < oldsize) {
    894 		int zerolen = MIN(round_page(newsize), node->tn_size) - newsize;
    895 
    896 		ubc_zerorange(uobj, newsize, zerolen, UBC_UNMAP_FLAG(vp));
    897 	}
    898 
    899 	node->tn_spec.tn_reg.tn_aobj_pages = newpages;
    900 	node->tn_size = newsize;
    901 	uvm_vnp_setsize(vp, newsize);
    902 
    903 	/*
    904 	 * Free "backing store".
    905 	 */
    906 	if (newpages < oldpages) {
    907 		KASSERT(uobj->vmobjlock == vp->v_interlock);
    908 
    909 		mutex_enter(uobj->vmobjlock);
    910 		uao_dropswap_range(uobj, newpages, oldpages);
    911 		mutex_exit(uobj->vmobjlock);
    912 
    913 		/* Decrease the used-memory counter. */
    914 		tmpfs_mem_decr(tmp, (oldpages - newpages) << PAGE_SHIFT);
    915 	}
    916 	if (newsize > oldsize) {
    917 		VN_KNOTE(vp, NOTE_EXTEND);
    918 	}
    919 	return 0;
    920 }
    921 
    922 /*
    923  * tmpfs_chflags: change flags of the given vnode.
    924  *
    925  * => Caller should perform tmpfs_update().
    926  */
    927 int
    928 tmpfs_chflags(vnode_t *vp, int flags, kauth_cred_t cred, lwp_t *l)
    929 {
    930 	tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp);
    931 	kauth_action_t action = KAUTH_VNODE_WRITE_FLAGS;
    932 	int error;
    933 	bool changing_sysflags = false;
    934 
    935 	KASSERT(VOP_ISLOCKED(vp));
    936 
    937 	/* Disallow this operation if the file system is mounted read-only. */
    938 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
    939 		return EROFS;
    940 
    941 	/*
    942 	 * If the new flags have non-user flags that are different than
    943 	 * those on the node, we need special permission to change them.
    944 	 */
    945 	if ((flags & SF_SETTABLE) != (node->tn_flags & SF_SETTABLE)) {
    946 		action |= KAUTH_VNODE_WRITE_SYSFLAGS;
    947 		changing_sysflags = true;
    948 	}
    949 
    950 	/*
    951 	 * Indicate that this node's flags have system attributes in them if
    952 	 * that's the case.
    953 	 */
    954 	if (node->tn_flags & (SF_IMMUTABLE | SF_APPEND)) {
    955 		action |= KAUTH_VNODE_HAS_SYSFLAGS;
    956 	}
    957 
    958 	error = kauth_authorize_vnode(cred, action, vp, NULL,
    959 	    genfs_can_chflags(cred, vp->v_type, node->tn_uid,
    960 	    changing_sysflags));
    961 	if (error)
    962 		return error;
    963 
    964 	/*
    965 	 * Set the flags. If we're not setting non-user flags, be careful not
    966 	 * to overwrite them.
    967 	 *
    968 	 * XXX: Can't we always assign here? if the system flags are different,
    969 	 *      the code above should catch attempts to change them without
    970 	 *      proper permissions, and if we're here it means it's okay to
    971 	 *      change them...
    972 	 */
    973 	if (!changing_sysflags) {
    974 		/* Clear all user-settable flags and re-set them. */
    975 		node->tn_flags &= SF_SETTABLE;
    976 		node->tn_flags |= (flags & UF_SETTABLE);
    977 	} else {
    978 		node->tn_flags = flags;
    979 	}
    980 	node->tn_status |= TMPFS_NODE_CHANGED;
    981 	VN_KNOTE(vp, NOTE_ATTRIB);
    982 	return 0;
    983 }
    984 
    985 /*
    986  * tmpfs_chmod: change access mode on the given vnode.
    987  *
    988  * => Caller should perform tmpfs_update().
    989  */
    990 int
    991 tmpfs_chmod(vnode_t *vp, mode_t mode, kauth_cred_t cred, lwp_t *l)
    992 {
    993 	tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp);
    994 	int error;
    995 
    996 	KASSERT(VOP_ISLOCKED(vp));
    997 
    998 	/* Disallow this operation if the file system is mounted read-only. */
    999 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
   1000 		return EROFS;
   1001 
   1002 	/* Immutable or append-only files cannot be modified, either. */
   1003 	if (node->tn_flags & (IMMUTABLE | APPEND))
   1004 		return EPERM;
   1005 
   1006 	error = kauth_authorize_vnode(cred, KAUTH_VNODE_WRITE_SECURITY, vp,
   1007 	    NULL, genfs_can_chmod(vp->v_type, cred, node->tn_uid, node->tn_gid, mode));
   1008 	if (error) {
   1009 		return error;
   1010 	}
   1011 	node->tn_mode = (mode & ALLPERMS);
   1012 	node->tn_status |= TMPFS_NODE_CHANGED;
   1013 	VN_KNOTE(vp, NOTE_ATTRIB);
   1014 	return 0;
   1015 }
   1016 
   1017 /*
   1018  * tmpfs_chown: change ownership of the given vnode.
   1019  *
   1020  * => At least one of uid or gid must be different than VNOVAL.
   1021  * => Attribute is unchanged for VNOVAL case.
   1022  * => Caller should perform tmpfs_update().
   1023  */
   1024 int
   1025 tmpfs_chown(vnode_t *vp, uid_t uid, gid_t gid, kauth_cred_t cred, lwp_t *l)
   1026 {
   1027 	tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp);
   1028 	int error;
   1029 
   1030 	KASSERT(VOP_ISLOCKED(vp));
   1031 
   1032 	/* Assign default values if they are unknown. */
   1033 	KASSERT(uid != VNOVAL || gid != VNOVAL);
   1034 	if (uid == VNOVAL) {
   1035 		uid = node->tn_uid;
   1036 	}
   1037 	if (gid == VNOVAL) {
   1038 		gid = node->tn_gid;
   1039 	}
   1040 
   1041 	/* Disallow this operation if the file system is mounted read-only. */
   1042 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
   1043 		return EROFS;
   1044 
   1045 	/* Immutable or append-only files cannot be modified, either. */
   1046 	if (node->tn_flags & (IMMUTABLE | APPEND))
   1047 		return EPERM;
   1048 
   1049 	error = kauth_authorize_vnode(cred, KAUTH_VNODE_CHANGE_OWNERSHIP, vp,
   1050 	    NULL, genfs_can_chown(cred, node->tn_uid, node->tn_gid, uid,
   1051 	    gid));
   1052 	if (error) {
   1053 		return error;
   1054 	}
   1055 	node->tn_uid = uid;
   1056 	node->tn_gid = gid;
   1057 	node->tn_status |= TMPFS_NODE_CHANGED;
   1058 	VN_KNOTE(vp, NOTE_ATTRIB);
   1059 	return 0;
   1060 }
   1061 
   1062 /*
   1063  * tmpfs_chsize: change size of the given vnode.
   1064  */
   1065 int
   1066 tmpfs_chsize(vnode_t *vp, u_quad_t size, kauth_cred_t cred, lwp_t *l)
   1067 {
   1068 	tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp);
   1069 
   1070 	KASSERT(VOP_ISLOCKED(vp));
   1071 
   1072 	/* Decide whether this is a valid operation based on the file type. */
   1073 	switch (vp->v_type) {
   1074 	case VDIR:
   1075 		return EISDIR;
   1076 	case VREG:
   1077 		if (vp->v_mount->mnt_flag & MNT_RDONLY) {
   1078 			return EROFS;
   1079 		}
   1080 		break;
   1081 	case VBLK:
   1082 	case VCHR:
   1083 	case VFIFO:
   1084 		/*
   1085 		 * Allow modifications of special files even if in the file
   1086 		 * system is mounted read-only (we are not modifying the
   1087 		 * files themselves, but the objects they represent).
   1088 		 */
   1089 		return 0;
   1090 	default:
   1091 		return EOPNOTSUPP;
   1092 	}
   1093 
   1094 	/* Immutable or append-only files cannot be modified, either. */
   1095 	if (node->tn_flags & (IMMUTABLE | APPEND)) {
   1096 		return EPERM;
   1097 	}
   1098 
   1099 	/* Note: tmpfs_truncate() will raise NOTE_EXTEND and NOTE_ATTRIB. */
   1100 	return tmpfs_truncate(vp, size);
   1101 }
   1102 
   1103 /*
   1104  * tmpfs_chtimes: change access and modification times for vnode.
   1105  */
   1106 int
   1107 tmpfs_chtimes(vnode_t *vp, const struct timespec *atime,
   1108     const struct timespec *mtime, const struct timespec *btime,
   1109     int vaflags, kauth_cred_t cred, lwp_t *l)
   1110 {
   1111 	tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp);
   1112 	int error;
   1113 
   1114 	KASSERT(VOP_ISLOCKED(vp));
   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 	error = kauth_authorize_vnode(cred, KAUTH_VNODE_WRITE_TIMES, vp, NULL,
   1125 	    genfs_can_chtimes(vp, vaflags, node->tn_uid, cred));
   1126 	if (error)
   1127 		return error;
   1128 
   1129 	if (atime->tv_sec != VNOVAL && atime->tv_nsec != VNOVAL)
   1130 		node->tn_status |= TMPFS_NODE_ACCESSED;
   1131 
   1132 	if (mtime->tv_sec != VNOVAL && mtime->tv_nsec != VNOVAL)
   1133 		node->tn_status |= TMPFS_NODE_MODIFIED;
   1134 
   1135 	if (btime->tv_sec == VNOVAL && btime->tv_nsec == VNOVAL)
   1136 		btime = NULL;
   1137 
   1138 	tmpfs_update(vp, atime, mtime, btime, 0);
   1139 	VN_KNOTE(vp, NOTE_ATTRIB);
   1140 	return 0;
   1141 }
   1142 
   1143 /*
   1144  * tmpfs_update: update timestamps, et al.
   1145  */
   1146 void
   1147 tmpfs_update(vnode_t *vp, const struct timespec *acc,
   1148     const struct timespec *mod, const struct timespec *birth, int flags)
   1149 {
   1150 	tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp);
   1151 	struct timespec nowtm;
   1152 
   1153 	/* KASSERT(VOP_ISLOCKED(vp)); */
   1154 
   1155 	if (flags & UPDATE_CLOSE) {
   1156 		/* XXX Need to do anything special? */
   1157 	}
   1158 	if ((node->tn_status & TMPFS_NODE_STATUSALL) == 0) {
   1159 		return;
   1160 	}
   1161 	if (birth != NULL) {
   1162 		node->tn_birthtime = *birth;
   1163 	}
   1164 	vfs_timestamp(&nowtm);
   1165 
   1166 	if (node->tn_status & TMPFS_NODE_ACCESSED) {
   1167 		node->tn_atime = acc ? *acc : nowtm;
   1168 	}
   1169 	if (node->tn_status & TMPFS_NODE_MODIFIED) {
   1170 		node->tn_mtime = mod ? *mod : nowtm;
   1171 	}
   1172 	if (node->tn_status & TMPFS_NODE_CHANGED) {
   1173 		node->tn_ctime = nowtm;
   1174 	}
   1175 
   1176 	node->tn_status &= ~TMPFS_NODE_STATUSALL;
   1177 }
   1178 
   1179 int
   1180 tmpfs_truncate(vnode_t *vp, off_t length)
   1181 {
   1182 	tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp);
   1183 	int error;
   1184 
   1185 	if (length < 0) {
   1186 		error = EINVAL;
   1187 		goto out;
   1188 	}
   1189 	if (node->tn_size == length) {
   1190 		error = 0;
   1191 		goto out;
   1192 	}
   1193 	error = tmpfs_reg_resize(vp, length);
   1194 	if (error == 0) {
   1195 		node->tn_status |= TMPFS_NODE_CHANGED | TMPFS_NODE_MODIFIED;
   1196 	}
   1197 out:
   1198 	tmpfs_update(vp, NULL, NULL, NULL, 0);
   1199 	return error;
   1200 }
   1201