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