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