Home | History | Annotate | Line # | Download | only in tmpfs
tmpfs_subr.c revision 1.91
      1 /*	$NetBSD: tmpfs_subr.c,v 1.91 2013/11/23 21:53:27 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.91 2013/11/23 21:53:27 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_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_size == 0);
    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 	KASSERT(node->tn_links == 0);
    256 
    257 	mutex_destroy(&node->tn_vlock);
    258 	tmpfs_node_put(tmp, node);
    259 }
    260 
    261 /*
    262  * tmpfs_vnode_get: allocate or reclaim a vnode for a specified inode.
    263  *
    264  * => Must be called with tmpfs_node_t::tn_vlock held.
    265  * => Returns vnode (*vpp) locked.
    266  */
    267 int
    268 tmpfs_vnode_get(struct mount *mp, tmpfs_node_t *node, vnode_t **vpp)
    269 {
    270 	vnode_t *vp;
    271 	kmutex_t *slock;
    272 	int error;
    273 again:
    274 	/* If there is already a vnode, try to reclaim it. */
    275 	if ((vp = node->tn_vnode) != NULL) {
    276 		atomic_or_32(&node->tn_gen, TMPFS_RECLAIMING_BIT);
    277 		mutex_enter(vp->v_interlock);
    278 		mutex_exit(&node->tn_vlock);
    279 		error = vget(vp, LK_EXCLUSIVE);
    280 		if (error == ENOENT) {
    281 			mutex_enter(&node->tn_vlock);
    282 			goto again;
    283 		}
    284 		atomic_and_32(&node->tn_gen, ~TMPFS_RECLAIMING_BIT);
    285 		*vpp = vp;
    286 		return error;
    287 	}
    288 	if (TMPFS_NODE_RECLAIMING(node)) {
    289 		atomic_and_32(&node->tn_gen, ~TMPFS_RECLAIMING_BIT);
    290 	}
    291 
    292 	/*
    293 	 * Get a new vnode and associate it with our inode.  Share the
    294 	 * lock with underlying UVM object, if there is one (VREG case).
    295 	 */
    296 	if (node->tn_type == VREG) {
    297 		struct uvm_object *uobj = node->tn_spec.tn_reg.tn_aobj;
    298 		slock = uobj->vmobjlock;
    299 	} else {
    300 		slock = NULL;
    301 	}
    302 	error = getnewvnode(VT_TMPFS, mp, tmpfs_vnodeop_p, slock, &vp);
    303 	if (error) {
    304 		mutex_exit(&node->tn_vlock);
    305 		return error;
    306 	}
    307 
    308 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    309 	vp->v_type = node->tn_type;
    310 
    311 	/* Type-specific initialization. */
    312 	switch (node->tn_type) {
    313 	case VBLK:
    314 	case VCHR:
    315 		vp->v_op = tmpfs_specop_p;
    316 		spec_node_init(vp, node->tn_spec.tn_dev.tn_rdev);
    317 		break;
    318 	case VDIR:
    319 		vp->v_vflag |= node->tn_spec.tn_dir.tn_parent == node ?
    320 		    VV_ROOT : 0;
    321 		break;
    322 	case VFIFO:
    323 		vp->v_op = tmpfs_fifoop_p;
    324 		break;
    325 	case VLNK:
    326 	case VREG:
    327 	case VSOCK:
    328 		break;
    329 	default:
    330 		KASSERT(false);
    331 	}
    332 
    333 	uvm_vnp_setsize(vp, node->tn_size);
    334 	vp->v_data = node;
    335 	node->tn_vnode = vp;
    336 	mutex_exit(&node->tn_vlock);
    337 
    338 	KASSERT(VOP_ISLOCKED(vp));
    339 	*vpp = vp;
    340 	return 0;
    341 }
    342 
    343 /*
    344  * tmpfs_construct_node: allocate a new file of specified type and adds it
    345  * into the parent directory.
    346  *
    347  * => Credentials of the caller are used.
    348  */
    349 int
    350 tmpfs_construct_node(vnode_t *dvp, vnode_t **vpp, struct vattr *vap,
    351     struct componentname *cnp, char *target)
    352 {
    353 	tmpfs_mount_t *tmp = VFS_TO_TMPFS(dvp->v_mount);
    354 	tmpfs_node_t *dnode = VP_TO_TMPFS_DIR(dvp), *node;
    355 	tmpfs_dirent_t *de, *wde;
    356 	int error;
    357 
    358 	KASSERT(VOP_ISLOCKED(dvp));
    359 	*vpp = NULL;
    360 
    361 	/* Check for the maximum number of links limit. */
    362 	if (vap->va_type == VDIR) {
    363 		/* Check for maximum links limit. */
    364 		if (dnode->tn_links == LINK_MAX) {
    365 			error = EMLINK;
    366 			goto out;
    367 		}
    368 		KASSERT(dnode->tn_links < LINK_MAX);
    369 	}
    370 
    371 	/* Allocate a node that represents the new file. */
    372 	error = tmpfs_alloc_node(tmp, vap->va_type, kauth_cred_geteuid(cnp->cn_cred),
    373 	    dnode->tn_gid, vap->va_mode, target, vap->va_rdev, &node);
    374 	if (error)
    375 		goto out;
    376 
    377 	/* Allocate a directory entry that points to the new file. */
    378 	error = tmpfs_alloc_dirent(tmp, cnp->cn_nameptr, cnp->cn_namelen, &de);
    379 	if (error) {
    380 		tmpfs_free_node(tmp, node);
    381 		goto out;
    382 	}
    383 
    384 	/* Get a vnode for the new file. */
    385 	mutex_enter(&node->tn_vlock);
    386 	error = tmpfs_vnode_get(dvp->v_mount, node, vpp);
    387 	if (error) {
    388 		tmpfs_free_dirent(tmp, de);
    389 		tmpfs_free_node(tmp, node);
    390 		goto out;
    391 	}
    392 
    393 	/* Remove whiteout before adding the new entry. */
    394 	if (cnp->cn_flags & ISWHITEOUT) {
    395 		wde = tmpfs_dir_lookup(dnode, cnp);
    396 		KASSERT(wde != NULL && wde->td_node == TMPFS_NODE_WHITEOUT);
    397 		tmpfs_dir_detach(dnode, wde);
    398 		tmpfs_free_dirent(tmp, wde);
    399 	}
    400 
    401 	/* Associate inode and attach the entry into the directory. */
    402 	tmpfs_dir_attach(dnode, de, node);
    403 
    404 	/* Make node opaque if requested. */
    405 	if (cnp->cn_flags & ISWHITEOUT)
    406 		node->tn_flags |= UF_OPAQUE;
    407 
    408 	/* Update the parent's timestamps. */
    409 	tmpfs_update(dvp, TMPFS_UPDATE_MTIME | TMPFS_UPDATE_CTIME);
    410 out:
    411 	vput(dvp);
    412 	return error;
    413 }
    414 
    415 /*
    416  * tmpfs_alloc_dirent: allocates a new directory entry for the inode.
    417  * The directory entry contains a path name component.
    418  */
    419 int
    420 tmpfs_alloc_dirent(tmpfs_mount_t *tmp, const char *name, uint16_t len,
    421     tmpfs_dirent_t **de)
    422 {
    423 	tmpfs_dirent_t *nde;
    424 
    425 	nde = tmpfs_dirent_get(tmp);
    426 	if (nde == NULL)
    427 		return ENOSPC;
    428 
    429 	nde->td_name = tmpfs_strname_alloc(tmp, len);
    430 	if (nde->td_name == NULL) {
    431 		tmpfs_dirent_put(tmp, nde);
    432 		return ENOSPC;
    433 	}
    434 	nde->td_namelen = len;
    435 	memcpy(nde->td_name, name, len);
    436 	nde->td_seq = TMPFS_DIRSEQ_NONE;
    437 
    438 	*de = nde;
    439 	return 0;
    440 }
    441 
    442 /*
    443  * tmpfs_free_dirent: free a directory entry.
    444  */
    445 void
    446 tmpfs_free_dirent(tmpfs_mount_t *tmp, tmpfs_dirent_t *de)
    447 {
    448 	KASSERT(de->td_node == NULL);
    449 	KASSERT(de->td_seq == TMPFS_DIRSEQ_NONE);
    450 	tmpfs_strname_free(tmp, de->td_name, de->td_namelen);
    451 	tmpfs_dirent_put(tmp, de);
    452 }
    453 
    454 /*
    455  * tmpfs_dir_attach: associate directory entry with a specified inode,
    456  * and attach the entry into the directory, specified by vnode.
    457  *
    458  * => Increases link count on the associated node.
    459  * => Increases link count on directory node if our node is VDIR.
    460  * => It is caller's responsibility to check for the LINK_MAX limit.
    461  * => Triggers kqueue events here.
    462  */
    463 void
    464 tmpfs_dir_attach(tmpfs_node_t *dnode, tmpfs_dirent_t *de, tmpfs_node_t *node)
    465 {
    466 	vnode_t *dvp = dnode->tn_vnode;
    467 	int events = NOTE_WRITE;
    468 
    469 	KASSERT(dvp != NULL);
    470 	KASSERT(VOP_ISLOCKED(dvp));
    471 
    472 	/* Get a new sequence number. */
    473 	KASSERT(de->td_seq == TMPFS_DIRSEQ_NONE);
    474 	de->td_seq = tmpfs_dir_getseq(dnode, de);
    475 
    476 	/* Associate directory entry and the inode. */
    477 	de->td_node = node;
    478 	if (node != TMPFS_NODE_WHITEOUT) {
    479 		KASSERT(node->tn_links < LINK_MAX);
    480 		node->tn_links++;
    481 
    482 		/* Save the hint (might overwrite). */
    483 		node->tn_dirent_hint = de;
    484 	} else if ((dnode->tn_gen & TMPFS_WHITEOUT_BIT) == 0) {
    485 		/* Flag that there are whiteout entries. */
    486 		atomic_or_32(&dnode->tn_gen, TMPFS_WHITEOUT_BIT);
    487 	}
    488 
    489 	/* Insert the entry to the directory (parent of inode). */
    490 	TAILQ_INSERT_TAIL(&dnode->tn_spec.tn_dir.tn_dir, de, td_entries);
    491 	dnode->tn_size += sizeof(tmpfs_dirent_t);
    492 	uvm_vnp_setsize(dvp, dnode->tn_size);
    493 
    494 	if (node != TMPFS_NODE_WHITEOUT && node->tn_type == VDIR) {
    495 		/* Set parent. */
    496 		KASSERT(node->tn_spec.tn_dir.tn_parent == NULL);
    497 		node->tn_spec.tn_dir.tn_parent = dnode;
    498 
    499 		/* Increase the link count of parent. */
    500 		KASSERT(dnode->tn_links < LINK_MAX);
    501 		dnode->tn_links++;
    502 		events |= NOTE_LINK;
    503 
    504 		TMPFS_VALIDATE_DIR(node);
    505 	}
    506 	VN_KNOTE(dvp, events);
    507 }
    508 
    509 /*
    510  * tmpfs_dir_detach: disassociate directory entry and its inode,
    511  * and detach the entry from the directory, specified by vnode.
    512  *
    513  * => Decreases link count on the associated node.
    514  * => Decreases the link count on directory node, if our node is VDIR.
    515  * => Triggers kqueue events here.
    516  *
    517  * => Note: dvp and vp may be NULL only if called by tmpfs_unmount().
    518  */
    519 void
    520 tmpfs_dir_detach(tmpfs_node_t *dnode, tmpfs_dirent_t *de)
    521 {
    522 	tmpfs_node_t *node = de->td_node;
    523 	vnode_t *vp, *dvp = dnode->tn_vnode;
    524 	int events = NOTE_WRITE;
    525 
    526 	KASSERT(dvp == NULL || VOP_ISLOCKED(dvp));
    527 
    528 	if (__predict_true(node != TMPFS_NODE_WHITEOUT)) {
    529 		/* Deassociate the inode and entry. */
    530 		node->tn_dirent_hint = NULL;
    531 
    532 		KASSERT(node->tn_links > 0);
    533 		node->tn_links--;
    534 
    535 		if ((vp = node->tn_vnode) != NULL) {
    536 			KASSERT(VOP_ISLOCKED(vp));
    537 			VN_KNOTE(vp, node->tn_links ? NOTE_LINK : NOTE_DELETE);
    538 		}
    539 
    540 		/* If directory - decrease the link count of parent. */
    541 		if (node->tn_type == VDIR) {
    542 			KASSERT(node->tn_spec.tn_dir.tn_parent == dnode);
    543 			node->tn_spec.tn_dir.tn_parent = NULL;
    544 
    545 			KASSERT(dnode->tn_links > 0);
    546 			dnode->tn_links--;
    547 			events |= NOTE_LINK;
    548 		}
    549 	}
    550 	de->td_node = NULL;
    551 
    552 	/* Remove the entry from the directory. */
    553 	if (dnode->tn_spec.tn_dir.tn_readdir_lastp == de) {
    554 		dnode->tn_spec.tn_dir.tn_readdir_lastp = NULL;
    555 	}
    556 	TAILQ_REMOVE(&dnode->tn_spec.tn_dir.tn_dir, de, td_entries);
    557 	dnode->tn_size -= sizeof(tmpfs_dirent_t);
    558 	tmpfs_dir_putseq(dnode, de);
    559 
    560 	if (dvp) {
    561 		uvm_vnp_setsize(dvp, dnode->tn_size);
    562 		VN_KNOTE(dvp, events);
    563 	}
    564 }
    565 
    566 /*
    567  * tmpfs_dir_lookup: find a directory entry in the specified inode.
    568  *
    569  * Note that the . and .. components are not allowed as they do not
    570  * physically exist within directories.
    571  */
    572 tmpfs_dirent_t *
    573 tmpfs_dir_lookup(tmpfs_node_t *node, struct componentname *cnp)
    574 {
    575 	const char *name = cnp->cn_nameptr;
    576 	const uint16_t nlen = cnp->cn_namelen;
    577 	tmpfs_dirent_t *de;
    578 
    579 	KASSERT(VOP_ISLOCKED(node->tn_vnode));
    580 	KASSERT(nlen != 1 || !(name[0] == '.'));
    581 	KASSERT(nlen != 2 || !(name[0] == '.' && name[1] == '.'));
    582 	TMPFS_VALIDATE_DIR(node);
    583 
    584 	TAILQ_FOREACH(de, &node->tn_spec.tn_dir.tn_dir, td_entries) {
    585 		if (de->td_namelen != nlen)
    586 			continue;
    587 		if (memcmp(de->td_name, name, nlen) != 0)
    588 			continue;
    589 		break;
    590 	}
    591 	return de;
    592 }
    593 
    594 /*
    595  * tmpfs_dir_cached: get a cached directory entry if it is valid.  Used to
    596  * avoid unnecessary tmpfs_dir_lookup().
    597  *
    598  * => The vnode must be locked.
    599  */
    600 tmpfs_dirent_t *
    601 tmpfs_dir_cached(tmpfs_node_t *node)
    602 {
    603 	tmpfs_dirent_t *de = node->tn_dirent_hint;
    604 
    605 	KASSERT(VOP_ISLOCKED(node->tn_vnode));
    606 
    607 	if (de == NULL) {
    608 		return NULL;
    609 	}
    610 	KASSERT(de->td_node == node);
    611 
    612 	/*
    613 	 * Directories always have a valid hint.  For files, check if there
    614 	 * are any hard links.  If there are - hint might be invalid.
    615 	 */
    616 	return (node->tn_type != VDIR && node->tn_links > 1) ? NULL : de;
    617 }
    618 
    619 /*
    620  * tmpfs_dir_getseq: get a per-directory sequence number for the entry.
    621  *
    622  * => Shall not be larger than 2^31 for linux32 compatibility.
    623  */
    624 uint32_t
    625 tmpfs_dir_getseq(tmpfs_node_t *dnode, tmpfs_dirent_t *de)
    626 {
    627 	uint32_t seq = de->td_seq;
    628 	vmem_t *seq_arena;
    629 	vmem_addr_t off;
    630 	int error __diagused;
    631 
    632 	TMPFS_VALIDATE_DIR(dnode);
    633 
    634 	if (__predict_true(seq != TMPFS_DIRSEQ_NONE)) {
    635 		/* Already set. */
    636 		KASSERT(seq >= TMPFS_DIRSEQ_START);
    637 		return seq;
    638 	}
    639 
    640 	/*
    641 	 * The "." and ".." and the end-of-directory have reserved numbers.
    642 	 * The other sequence numbers are allocated as following:
    643 	 *
    644 	 * - The first half of the 2^31 is assigned incrementally.
    645 	 *
    646 	 * - If that range is exceeded, then the second half of 2^31
    647 	 * is used, but managed by vmem(9).
    648 	 */
    649 
    650 	seq = dnode->tn_spec.tn_dir.tn_next_seq;
    651 	KASSERT(seq >= TMPFS_DIRSEQ_START);
    652 
    653 	if (__predict_true(seq < TMPFS_DIRSEQ_END)) {
    654 		/* First half: just increment and return. */
    655 		dnode->tn_spec.tn_dir.tn_next_seq++;
    656 		return seq;
    657 	}
    658 
    659 	/*
    660 	 * First half exceeded, use the second half.  May need to create
    661 	 * vmem(9) arena for the directory first.
    662 	 */
    663 	if ((seq_arena = dnode->tn_spec.tn_dir.tn_seq_arena) == NULL) {
    664 		seq_arena = vmem_create("tmpfscoo", 0,
    665 		    TMPFS_DIRSEQ_END - 1, 1, NULL, NULL, NULL, 0,
    666 		    VM_SLEEP, IPL_NONE);
    667 		dnode->tn_spec.tn_dir.tn_seq_arena = seq_arena;
    668 		KASSERT(seq_arena != NULL);
    669 	}
    670 	error = vmem_alloc(seq_arena, 1, VM_SLEEP | VM_BESTFIT, &off);
    671 	KASSERT(error == 0);
    672 
    673 	KASSERT(off < TMPFS_DIRSEQ_END);
    674 	seq = off | TMPFS_DIRSEQ_END;
    675 	return seq;
    676 }
    677 
    678 static void
    679 tmpfs_dir_putseq(tmpfs_node_t *dnode, tmpfs_dirent_t *de)
    680 {
    681 	vmem_t *seq_arena = dnode->tn_spec.tn_dir.tn_seq_arena;
    682 	uint32_t seq = de->td_seq;
    683 
    684 	TMPFS_VALIDATE_DIR(dnode);
    685 
    686 	if (seq == TMPFS_DIRSEQ_NONE || seq < TMPFS_DIRSEQ_END) {
    687 		/* First half (or no sequence number set yet). */
    688 		KASSERT(de->td_seq >= TMPFS_DIRSEQ_START);
    689 	} else {
    690 		/* Second half. */
    691 		KASSERT(seq_arena != NULL);
    692 		KASSERT(seq >= TMPFS_DIRSEQ_END);
    693 		seq &= ~TMPFS_DIRSEQ_END;
    694 		vmem_free(seq_arena, seq, 1);
    695 	}
    696 	de->td_seq = TMPFS_DIRSEQ_NONE;
    697 
    698 	/* Empty?  We can reset. */
    699 	if (seq_arena && dnode->tn_size == 0) {
    700 		dnode->tn_spec.tn_dir.tn_seq_arena = NULL;
    701 		dnode->tn_spec.tn_dir.tn_next_seq = TMPFS_DIRSEQ_START;
    702 		vmem_destroy(seq_arena);
    703 	}
    704 }
    705 
    706 /*
    707  * tmpfs_dir_lookupbyseq: lookup a directory entry by the sequence number.
    708  */
    709 tmpfs_dirent_t *
    710 tmpfs_dir_lookupbyseq(tmpfs_node_t *node, off_t seq)
    711 {
    712 	tmpfs_dirent_t *de = node->tn_spec.tn_dir.tn_readdir_lastp;
    713 
    714 	TMPFS_VALIDATE_DIR(node);
    715 
    716 	/*
    717 	 * First, check the cache.  If does not match - perform a lookup.
    718 	 */
    719 	if (de && de->td_seq == seq) {
    720 		KASSERT(de->td_seq >= TMPFS_DIRSEQ_START);
    721 		KASSERT(de->td_seq != TMPFS_DIRSEQ_NONE);
    722 		return de;
    723 	}
    724 	TAILQ_FOREACH(de, &node->tn_spec.tn_dir.tn_dir, td_entries) {
    725 		KASSERT(de->td_seq >= TMPFS_DIRSEQ_START);
    726 		KASSERT(de->td_seq != TMPFS_DIRSEQ_NONE);
    727 		if (de->td_seq == seq)
    728 			return de;
    729 	}
    730 	return NULL;
    731 }
    732 
    733 /*
    734  * tmpfs_dir_getdotents: helper function for tmpfs_readdir() to get the
    735  * dot meta entries, that is, "." or "..".  Copy it to the UIO space.
    736  */
    737 static int
    738 tmpfs_dir_getdotents(tmpfs_node_t *node, struct dirent *dp, struct uio *uio)
    739 {
    740 	tmpfs_dirent_t *de;
    741 	off_t next = 0;
    742 	int error;
    743 
    744 	switch (uio->uio_offset) {
    745 	case TMPFS_DIRSEQ_DOT:
    746 		dp->d_fileno = node->tn_id;
    747 		strlcpy(dp->d_name, ".", sizeof(dp->d_name));
    748 		next = TMPFS_DIRSEQ_DOTDOT;
    749 		break;
    750 	case TMPFS_DIRSEQ_DOTDOT:
    751 		dp->d_fileno = node->tn_spec.tn_dir.tn_parent->tn_id;
    752 		strlcpy(dp->d_name, "..", sizeof(dp->d_name));
    753 		de = TAILQ_FIRST(&node->tn_spec.tn_dir.tn_dir);
    754 		next = de ? tmpfs_dir_getseq(node, de) : TMPFS_DIRSEQ_EOF;
    755 		break;
    756 	default:
    757 		KASSERT(false);
    758 	}
    759 	dp->d_type = DT_DIR;
    760 	dp->d_namlen = strlen(dp->d_name);
    761 	dp->d_reclen = _DIRENT_SIZE(dp);
    762 
    763 	if (dp->d_reclen > uio->uio_resid) {
    764 		return EJUSTRETURN;
    765 	}
    766 	if ((error = uiomove(dp, dp->d_reclen, uio)) != 0) {
    767 		return error;
    768 	}
    769 
    770 	uio->uio_offset = next;
    771 	return error;
    772 }
    773 
    774 /*
    775  * tmpfs_dir_getdents: helper function for tmpfs_readdir.
    776  *
    777  * => Returns as much directory entries as can fit in the uio space.
    778  * => The read starts at uio->uio_offset.
    779  */
    780 int
    781 tmpfs_dir_getdents(tmpfs_node_t *node, struct uio *uio, off_t *cntp)
    782 {
    783 	tmpfs_dirent_t *de;
    784 	struct dirent *dentp;
    785 	int error = 0;
    786 
    787 	KASSERT(VOP_ISLOCKED(node->tn_vnode));
    788 	TMPFS_VALIDATE_DIR(node);
    789 
    790 	/*
    791 	 * Allocate struct dirent and first check for the "." and "..".
    792 	 * Note: tmpfs_dir_getdotents() will "seek" for us.
    793 	 */
    794 	dentp = kmem_zalloc(sizeof(struct dirent), KM_SLEEP);
    795 
    796 	if (uio->uio_offset == TMPFS_DIRSEQ_DOT) {
    797 		if ((error = tmpfs_dir_getdotents(node, dentp, uio)) != 0) {
    798 			goto done;
    799 		}
    800 		(*cntp)++;
    801 	}
    802 	if (uio->uio_offset == TMPFS_DIRSEQ_DOTDOT) {
    803 		if ((error = tmpfs_dir_getdotents(node, dentp, uio)) != 0) {
    804 			goto done;
    805 		}
    806 		(*cntp)++;
    807 	}
    808 
    809 	/* Done if we reached the end. */
    810 	if (uio->uio_offset == TMPFS_DIRSEQ_EOF) {
    811 		goto done;
    812 	}
    813 
    814 	/* Locate the directory entry given by the given sequence number. */
    815 	de = tmpfs_dir_lookupbyseq(node, uio->uio_offset);
    816 	if (de == NULL) {
    817 		error = EINVAL;
    818 		goto done;
    819 	}
    820 
    821 	/*
    822 	 * Read as many entries as possible; i.e., until we reach the end
    823 	 * of the directory or we exhaust UIO space.
    824 	 */
    825 	do {
    826 		if (de->td_node == TMPFS_NODE_WHITEOUT) {
    827 			dentp->d_fileno = 1;
    828 			dentp->d_type = DT_WHT;
    829 		} else {
    830 			dentp->d_fileno = de->td_node->tn_id;
    831 			dentp->d_type = vtype2dt(de->td_node->tn_type);
    832 		}
    833 		dentp->d_namlen = de->td_namelen;
    834 		KASSERT(de->td_namelen < sizeof(dentp->d_name));
    835 		memcpy(dentp->d_name, de->td_name, de->td_namelen);
    836 		dentp->d_name[de->td_namelen] = '\0';
    837 		dentp->d_reclen = _DIRENT_SIZE(dentp);
    838 
    839 		if (dentp->d_reclen > uio->uio_resid) {
    840 			/* Exhausted UIO space. */
    841 			error = EJUSTRETURN;
    842 			break;
    843 		}
    844 
    845 		/* Copy out the directory entry and continue. */
    846 		error = uiomove(dentp, dentp->d_reclen, uio);
    847 		if (error) {
    848 			break;
    849 		}
    850 		(*cntp)++;
    851 		de = TAILQ_NEXT(de, td_entries);
    852 
    853 	} while (uio->uio_resid > 0 && de);
    854 
    855 	/* Cache the last entry or clear and mark EOF. */
    856 	uio->uio_offset = de ? tmpfs_dir_getseq(node, de) : TMPFS_DIRSEQ_EOF;
    857 	node->tn_spec.tn_dir.tn_readdir_lastp = de;
    858 done:
    859 	tmpfs_update(node->tn_vnode, TMPFS_UPDATE_ATIME);
    860 	kmem_free(dentp, sizeof(struct dirent));
    861 
    862 	if (error == EJUSTRETURN) {
    863 		/* Exhausted UIO space - just return. */
    864 		error = 0;
    865 	}
    866 	KASSERT(error >= 0);
    867 	return error;
    868 }
    869 
    870 /*
    871  * tmpfs_reg_resize: resize the underlying UVM object associated with the
    872  * specified regular file.
    873  */
    874 int
    875 tmpfs_reg_resize(struct vnode *vp, off_t newsize)
    876 {
    877 	tmpfs_mount_t *tmp = VFS_TO_TMPFS(vp->v_mount);
    878 	tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp);
    879 	struct uvm_object *uobj = node->tn_spec.tn_reg.tn_aobj;
    880 	size_t newpages, oldpages;
    881 	off_t oldsize;
    882 
    883 	KASSERT(vp->v_type == VREG);
    884 	KASSERT(newsize >= 0);
    885 
    886 	oldsize = node->tn_size;
    887 	oldpages = round_page(oldsize) >> PAGE_SHIFT;
    888 	newpages = round_page(newsize) >> PAGE_SHIFT;
    889 	KASSERT(oldpages == node->tn_spec.tn_reg.tn_aobj_pages);
    890 
    891 	if (newpages > oldpages) {
    892 		/* Increase the used-memory counter if getting extra pages. */
    893 		if (!tmpfs_mem_incr(tmp, (newpages - oldpages) << PAGE_SHIFT)) {
    894 			return ENOSPC;
    895 		}
    896 	} else if (newsize < oldsize) {
    897 		size_t zerolen;
    898 
    899 		zerolen = MIN(round_page(newsize), node->tn_size) - newsize;
    900 		ubc_zerorange(uobj, newsize, zerolen, UBC_UNMAP_FLAG(vp));
    901 	}
    902 
    903 	node->tn_spec.tn_reg.tn_aobj_pages = newpages;
    904 	node->tn_size = newsize;
    905 	uvm_vnp_setsize(vp, newsize);
    906 
    907 	/*
    908 	 * Free "backing store".
    909 	 */
    910 	if (newpages < oldpages) {
    911 		KASSERT(uobj->vmobjlock == vp->v_interlock);
    912 
    913 		mutex_enter(uobj->vmobjlock);
    914 		uao_dropswap_range(uobj, newpages, oldpages);
    915 		mutex_exit(uobj->vmobjlock);
    916 
    917 		/* Decrease the used-memory counter. */
    918 		tmpfs_mem_decr(tmp, (oldpages - newpages) << PAGE_SHIFT);
    919 	}
    920 	if (newsize > oldsize) {
    921 		VN_KNOTE(vp, NOTE_EXTEND);
    922 	}
    923 	return 0;
    924 }
    925 
    926 /*
    927  * tmpfs_chflags: change flags of the given vnode.
    928  */
    929 int
    930 tmpfs_chflags(vnode_t *vp, int flags, kauth_cred_t cred, lwp_t *l)
    931 {
    932 	tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp);
    933 	kauth_action_t action = KAUTH_VNODE_WRITE_FLAGS;
    934 	int error;
    935 	bool changing_sysflags = false;
    936 
    937 	KASSERT(VOP_ISLOCKED(vp));
    938 
    939 	/* Disallow this operation if the file system is mounted read-only. */
    940 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
    941 		return EROFS;
    942 
    943 	/*
    944 	 * If the new flags have non-user flags that are different than
    945 	 * those on the node, we need special permission to change them.
    946 	 */
    947 	if ((flags & SF_SETTABLE) != (node->tn_flags & SF_SETTABLE)) {
    948 		action |= KAUTH_VNODE_WRITE_SYSFLAGS;
    949 		changing_sysflags = true;
    950 	}
    951 
    952 	/*
    953 	 * Indicate that this node's flags have system attributes in them if
    954 	 * that's the case.
    955 	 */
    956 	if (node->tn_flags & (SF_IMMUTABLE | SF_APPEND)) {
    957 		action |= KAUTH_VNODE_HAS_SYSFLAGS;
    958 	}
    959 
    960 	error = kauth_authorize_vnode(cred, action, vp, NULL,
    961 	    genfs_can_chflags(cred, vp->v_type, node->tn_uid,
    962 	    changing_sysflags));
    963 	if (error)
    964 		return error;
    965 
    966 	/*
    967 	 * Set the flags. If we're not setting non-user flags, be careful not
    968 	 * to overwrite them.
    969 	 *
    970 	 * XXX: Can't we always assign here? if the system flags are different,
    971 	 *      the code above should catch attempts to change them without
    972 	 *      proper permissions, and if we're here it means it's okay to
    973 	 *      change them...
    974 	 */
    975 	if (!changing_sysflags) {
    976 		/* Clear all user-settable flags and re-set them. */
    977 		node->tn_flags &= SF_SETTABLE;
    978 		node->tn_flags |= (flags & UF_SETTABLE);
    979 	} else {
    980 		node->tn_flags = flags;
    981 	}
    982 	tmpfs_update(vp, TMPFS_UPDATE_CTIME);
    983 	VN_KNOTE(vp, NOTE_ATTRIB);
    984 	return 0;
    985 }
    986 
    987 /*
    988  * tmpfs_chmod: change access mode on the given vnode.
    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 	tmpfs_update(vp, TMPFS_UPDATE_CTIME);
   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  */
   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 	tmpfs_update(vp, TMPFS_UPDATE_CTIME);
   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 	const off_t length = size;
   1069 	int error;
   1070 
   1071 	KASSERT(VOP_ISLOCKED(vp));
   1072 
   1073 	/* Decide whether this is a valid operation based on the file type. */
   1074 	switch (vp->v_type) {
   1075 	case VDIR:
   1076 		return EISDIR;
   1077 	case VREG:
   1078 		if (vp->v_mount->mnt_flag & MNT_RDONLY) {
   1079 			return EROFS;
   1080 		}
   1081 		break;
   1082 	case VBLK:
   1083 	case VCHR:
   1084 	case VFIFO:
   1085 		/*
   1086 		 * Allow modifications of special files even if in the file
   1087 		 * system is mounted read-only (we are not modifying the
   1088 		 * files themselves, but the objects they represent).
   1089 		 */
   1090 		return 0;
   1091 	default:
   1092 		return EOPNOTSUPP;
   1093 	}
   1094 
   1095 	/* Immutable or append-only files cannot be modified, either. */
   1096 	if (node->tn_flags & (IMMUTABLE | APPEND)) {
   1097 		return EPERM;
   1098 	}
   1099 
   1100 	if (length < 0) {
   1101 		return EINVAL;
   1102 	}
   1103 	if (node->tn_size == length) {
   1104 		return 0;
   1105 	}
   1106 
   1107 	/* Note: tmpfs_reg_resize() will raise NOTE_EXTEND and NOTE_ATTRIB. */
   1108 	if ((error = tmpfs_reg_resize(vp, length)) != 0) {
   1109 		return error;
   1110 	}
   1111 	tmpfs_update(vp, TMPFS_UPDATE_CTIME | TMPFS_UPDATE_MTIME);
   1112 	return 0;
   1113 }
   1114 
   1115 /*
   1116  * tmpfs_chtimes: change access and modification times for vnode.
   1117  */
   1118 int
   1119 tmpfs_chtimes(vnode_t *vp, const struct timespec *atime,
   1120     const struct timespec *mtime, const struct timespec *btime,
   1121     int vaflags, kauth_cred_t cred, lwp_t *l)
   1122 {
   1123 	tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp);
   1124 	int error;
   1125 
   1126 	KASSERT(VOP_ISLOCKED(vp));
   1127 
   1128 	/* Disallow this operation if the file system is mounted read-only. */
   1129 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
   1130 		return EROFS;
   1131 
   1132 	/* Immutable or append-only files cannot be modified, either. */
   1133 	if (node->tn_flags & (IMMUTABLE | APPEND))
   1134 		return EPERM;
   1135 
   1136 	error = kauth_authorize_vnode(cred, KAUTH_VNODE_WRITE_TIMES, vp, NULL,
   1137 	    genfs_can_chtimes(vp, vaflags, node->tn_uid, cred));
   1138 	if (error)
   1139 		return error;
   1140 
   1141 	if (atime->tv_sec != VNOVAL) {
   1142 		node->tn_atime = *atime;
   1143 	}
   1144 	if (mtime->tv_sec != VNOVAL) {
   1145 		node->tn_mtime = *mtime;
   1146 	}
   1147 	if (btime->tv_sec != VNOVAL) {
   1148 		node->tn_birthtime = *btime;
   1149 	}
   1150 	VN_KNOTE(vp, NOTE_ATTRIB);
   1151 	return 0;
   1152 }
   1153 
   1154 /*
   1155  * tmpfs_update: update the timestamps as indicated by the flags.
   1156  */
   1157 void
   1158 tmpfs_update(vnode_t *vp, unsigned tflags)
   1159 {
   1160 	tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp);
   1161 	struct timespec nowtm;
   1162 
   1163 	if (tflags == 0) {
   1164 		return;
   1165 	}
   1166 	vfs_timestamp(&nowtm);
   1167 
   1168 	if (tflags & TMPFS_UPDATE_ATIME) {
   1169 		node->tn_atime = nowtm;
   1170 	}
   1171 	if (tflags & TMPFS_UPDATE_MTIME) {
   1172 		node->tn_mtime = nowtm;
   1173 	}
   1174 	if (tflags & TMPFS_UPDATE_CTIME) {
   1175 		node->tn_ctime = nowtm;
   1176 	}
   1177 }
   1178