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