xref: /freebsd/sys/fs/tmpfs/tmpfs_subr.c (revision b9f654b163bce26de79705e77b872427c9f2afa1)
1 /*	$NetBSD: tmpfs_subr.c,v 1.35 2007/07/09 21:10:50 ad Exp $	*/
2 
3 /*-
4  * SPDX-License-Identifier: BSD-2-Clause-NetBSD
5  *
6  * Copyright (c) 2005 The NetBSD Foundation, Inc.
7  * All rights reserved.
8  *
9  * This code is derived from software contributed to The NetBSD Foundation
10  * by Julio M. Merino Vidal, developed as part of Google's Summer of Code
11  * 2005 program.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
23  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
26  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 /*
36  * Efficient memory file system supporting functions.
37  */
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
40 
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/dirent.h>
44 #include <sys/fnv_hash.h>
45 #include <sys/lock.h>
46 #include <sys/limits.h>
47 #include <sys/mount.h>
48 #include <sys/namei.h>
49 #include <sys/priv.h>
50 #include <sys/proc.h>
51 #include <sys/random.h>
52 #include <sys/rwlock.h>
53 #include <sys/stat.h>
54 #include <sys/sysctl.h>
55 #include <sys/vnode.h>
56 #include <sys/vmmeter.h>
57 
58 #include <vm/vm.h>
59 #include <vm/vm_param.h>
60 #include <vm/vm_object.h>
61 #include <vm/vm_page.h>
62 #include <vm/vm_pageout.h>
63 #include <vm/vm_pager.h>
64 #include <vm/vm_extern.h>
65 #include <vm/swap_pager.h>
66 
67 #include <fs/tmpfs/tmpfs.h>
68 #include <fs/tmpfs/tmpfs_fifoops.h>
69 #include <fs/tmpfs/tmpfs_vnops.h>
70 
71 SYSCTL_NODE(_vfs, OID_AUTO, tmpfs, CTLFLAG_RW, 0, "tmpfs file system");
72 
73 static long tmpfs_pages_reserved = TMPFS_PAGES_MINRESERVED;
74 
75 static int
76 sysctl_mem_reserved(SYSCTL_HANDLER_ARGS)
77 {
78 	int error;
79 	long pages, bytes;
80 
81 	pages = *(long *)arg1;
82 	bytes = pages * PAGE_SIZE;
83 
84 	error = sysctl_handle_long(oidp, &bytes, 0, req);
85 	if (error || !req->newptr)
86 		return (error);
87 
88 	pages = bytes / PAGE_SIZE;
89 	if (pages < TMPFS_PAGES_MINRESERVED)
90 		return (EINVAL);
91 
92 	*(long *)arg1 = pages;
93 	return (0);
94 }
95 
96 SYSCTL_PROC(_vfs_tmpfs, OID_AUTO, memory_reserved, CTLTYPE_LONG|CTLFLAG_RW,
97     &tmpfs_pages_reserved, 0, sysctl_mem_reserved, "L",
98     "Amount of available memory and swap below which tmpfs growth stops");
99 
100 static __inline int tmpfs_dirtree_cmp(struct tmpfs_dirent *a,
101     struct tmpfs_dirent *b);
102 RB_PROTOTYPE_STATIC(tmpfs_dir, tmpfs_dirent, uh.td_entries, tmpfs_dirtree_cmp);
103 
104 size_t
105 tmpfs_mem_avail(void)
106 {
107 	vm_ooffset_t avail;
108 
109 	avail = swap_pager_avail + vm_free_count() - tmpfs_pages_reserved;
110 	if (__predict_false(avail < 0))
111 		avail = 0;
112 	return (avail);
113 }
114 
115 size_t
116 tmpfs_pages_used(struct tmpfs_mount *tmp)
117 {
118 	const size_t node_size = sizeof(struct tmpfs_node) +
119 	    sizeof(struct tmpfs_dirent);
120 	size_t meta_pages;
121 
122 	meta_pages = howmany((uintmax_t)tmp->tm_nodes_inuse * node_size,
123 	    PAGE_SIZE);
124 	return (meta_pages + tmp->tm_pages_used);
125 }
126 
127 static size_t
128 tmpfs_pages_check_avail(struct tmpfs_mount *tmp, size_t req_pages)
129 {
130 	if (tmpfs_mem_avail() < req_pages)
131 		return (0);
132 
133 	if (tmp->tm_pages_max != ULONG_MAX &&
134 	    tmp->tm_pages_max < req_pages + tmpfs_pages_used(tmp))
135 			return (0);
136 
137 	return (1);
138 }
139 
140 void
141 tmpfs_ref_node(struct tmpfs_node *node)
142 {
143 
144 	TMPFS_NODE_LOCK(node);
145 	tmpfs_ref_node_locked(node);
146 	TMPFS_NODE_UNLOCK(node);
147 }
148 
149 void
150 tmpfs_ref_node_locked(struct tmpfs_node *node)
151 {
152 
153 	TMPFS_NODE_ASSERT_LOCKED(node);
154 	KASSERT(node->tn_refcount > 0, ("node %p zero refcount", node));
155 	KASSERT(node->tn_refcount < UINT_MAX, ("node %p refcount %u", node,
156 	    node->tn_refcount));
157 	node->tn_refcount++;
158 }
159 
160 /*
161  * Allocates a new node of type 'type' inside the 'tmp' mount point, with
162  * its owner set to 'uid', its group to 'gid' and its mode set to 'mode',
163  * using the credentials of the process 'p'.
164  *
165  * If the node type is set to 'VDIR', then the parent parameter must point
166  * to the parent directory of the node being created.  It may only be NULL
167  * while allocating the root node.
168  *
169  * If the node type is set to 'VBLK' or 'VCHR', then the rdev parameter
170  * specifies the device the node represents.
171  *
172  * If the node type is set to 'VLNK', then the parameter target specifies
173  * the file name of the target file for the symbolic link that is being
174  * created.
175  *
176  * Note that new nodes are retrieved from the available list if it has
177  * items or, if it is empty, from the node pool as long as there is enough
178  * space to create them.
179  *
180  * Returns zero on success or an appropriate error code on failure.
181  */
182 int
183 tmpfs_alloc_node(struct mount *mp, struct tmpfs_mount *tmp, enum vtype type,
184     uid_t uid, gid_t gid, mode_t mode, struct tmpfs_node *parent,
185     const char *target, dev_t rdev, struct tmpfs_node **node)
186 {
187 	struct tmpfs_node *nnode;
188 	vm_object_t obj;
189 
190 	/* If the root directory of the 'tmp' file system is not yet
191 	 * allocated, this must be the request to do it. */
192 	MPASS(IMPLIES(tmp->tm_root == NULL, parent == NULL && type == VDIR));
193 	KASSERT(tmp->tm_root == NULL || mp->mnt_writeopcount > 0,
194 	    ("creating node not under vn_start_write"));
195 
196 	MPASS(IFF(type == VLNK, target != NULL));
197 	MPASS(IFF(type == VBLK || type == VCHR, rdev != VNOVAL));
198 
199 	if (tmp->tm_nodes_inuse >= tmp->tm_nodes_max)
200 		return (ENOSPC);
201 	if (tmpfs_pages_check_avail(tmp, 1) == 0)
202 		return (ENOSPC);
203 
204 	if ((mp->mnt_kern_flag & MNTK_UNMOUNT) != 0) {
205 		/*
206 		 * When a new tmpfs node is created for fully
207 		 * constructed mount point, there must be a parent
208 		 * node, which vnode is locked exclusively.  As
209 		 * consequence, if the unmount is executing in
210 		 * parallel, vflush() cannot reclaim the parent vnode.
211 		 * Due to this, the check for MNTK_UNMOUNT flag is not
212 		 * racy: if we did not see MNTK_UNMOUNT flag, then tmp
213 		 * cannot be destroyed until node construction is
214 		 * finished and the parent vnode unlocked.
215 		 *
216 		 * Tmpfs does not need to instantiate new nodes during
217 		 * unmount.
218 		 */
219 		return (EBUSY);
220 	}
221 	if ((mp->mnt_kern_flag & MNT_RDONLY) != 0)
222 		return (EROFS);
223 
224 	nnode = (struct tmpfs_node *)uma_zalloc_arg(tmp->tm_node_pool, tmp,
225 	    M_WAITOK);
226 
227 	/* Generic initialization. */
228 	nnode->tn_type = type;
229 	vfs_timestamp(&nnode->tn_atime);
230 	nnode->tn_birthtime = nnode->tn_ctime = nnode->tn_mtime =
231 	    nnode->tn_atime;
232 	nnode->tn_uid = uid;
233 	nnode->tn_gid = gid;
234 	nnode->tn_mode = mode;
235 	nnode->tn_id = alloc_unr64(&tmp->tm_ino_unr);
236 	nnode->tn_refcount = 1;
237 
238 	/* Type-specific initialization. */
239 	switch (nnode->tn_type) {
240 	case VBLK:
241 	case VCHR:
242 		nnode->tn_rdev = rdev;
243 		break;
244 
245 	case VDIR:
246 		RB_INIT(&nnode->tn_dir.tn_dirhead);
247 		LIST_INIT(&nnode->tn_dir.tn_dupindex);
248 		MPASS(parent != nnode);
249 		MPASS(IMPLIES(parent == NULL, tmp->tm_root == NULL));
250 		nnode->tn_dir.tn_parent = (parent == NULL) ? nnode : parent;
251 		nnode->tn_dir.tn_readdir_lastn = 0;
252 		nnode->tn_dir.tn_readdir_lastp = NULL;
253 		nnode->tn_links++;
254 		TMPFS_NODE_LOCK(nnode->tn_dir.tn_parent);
255 		nnode->tn_dir.tn_parent->tn_links++;
256 		TMPFS_NODE_UNLOCK(nnode->tn_dir.tn_parent);
257 		break;
258 
259 	case VFIFO:
260 		/* FALLTHROUGH */
261 	case VSOCK:
262 		break;
263 
264 	case VLNK:
265 		MPASS(strlen(target) < MAXPATHLEN);
266 		nnode->tn_size = strlen(target);
267 		nnode->tn_link = malloc(nnode->tn_size, M_TMPFSNAME,
268 		    M_WAITOK);
269 		memcpy(nnode->tn_link, target, nnode->tn_size);
270 		break;
271 
272 	case VREG:
273 		obj = nnode->tn_reg.tn_aobj =
274 		    vm_pager_allocate(OBJT_SWAP, NULL, 0, VM_PROT_DEFAULT, 0,
275 			NULL /* XXXKIB - tmpfs needs swap reservation */);
276 		VM_OBJECT_WLOCK(obj);
277 		/* OBJ_TMPFS is set together with the setting of vp->v_object */
278 		vm_object_set_flag(obj, OBJ_NOSPLIT | OBJ_TMPFS_NODE);
279 		vm_object_clear_flag(obj, OBJ_ONEMAPPING);
280 		VM_OBJECT_WUNLOCK(obj);
281 		break;
282 
283 	default:
284 		panic("tmpfs_alloc_node: type %p %d", nnode,
285 		    (int)nnode->tn_type);
286 	}
287 
288 	TMPFS_LOCK(tmp);
289 	LIST_INSERT_HEAD(&tmp->tm_nodes_used, nnode, tn_entries);
290 	nnode->tn_attached = true;
291 	tmp->tm_nodes_inuse++;
292 	tmp->tm_refcount++;
293 	TMPFS_UNLOCK(tmp);
294 
295 	*node = nnode;
296 	return (0);
297 }
298 
299 /*
300  * Destroys the node pointed to by node from the file system 'tmp'.
301  * If the node references a directory, no entries are allowed.
302  */
303 void
304 tmpfs_free_node(struct tmpfs_mount *tmp, struct tmpfs_node *node)
305 {
306 
307 	TMPFS_LOCK(tmp);
308 	TMPFS_NODE_LOCK(node);
309 	if (!tmpfs_free_node_locked(tmp, node, false)) {
310 		TMPFS_NODE_UNLOCK(node);
311 		TMPFS_UNLOCK(tmp);
312 	}
313 }
314 
315 bool
316 tmpfs_free_node_locked(struct tmpfs_mount *tmp, struct tmpfs_node *node,
317     bool detach)
318 {
319 	vm_object_t uobj;
320 
321 	TMPFS_MP_ASSERT_LOCKED(tmp);
322 	TMPFS_NODE_ASSERT_LOCKED(node);
323 	KASSERT(node->tn_refcount > 0, ("node %p refcount zero", node));
324 
325 	node->tn_refcount--;
326 	if (node->tn_attached && (detach || node->tn_refcount == 0)) {
327 		MPASS(tmp->tm_nodes_inuse > 0);
328 		tmp->tm_nodes_inuse--;
329 		LIST_REMOVE(node, tn_entries);
330 		node->tn_attached = false;
331 	}
332 	if (node->tn_refcount > 0)
333 		return (false);
334 
335 #ifdef INVARIANTS
336 	MPASS(node->tn_vnode == NULL);
337 	MPASS((node->tn_vpstate & TMPFS_VNODE_ALLOCATING) == 0);
338 #endif
339 	TMPFS_NODE_UNLOCK(node);
340 	TMPFS_UNLOCK(tmp);
341 
342 	switch (node->tn_type) {
343 	case VBLK:
344 		/* FALLTHROUGH */
345 	case VCHR:
346 		/* FALLTHROUGH */
347 	case VDIR:
348 		/* FALLTHROUGH */
349 	case VFIFO:
350 		/* FALLTHROUGH */
351 	case VSOCK:
352 		break;
353 
354 	case VLNK:
355 		free(node->tn_link, M_TMPFSNAME);
356 		break;
357 
358 	case VREG:
359 		uobj = node->tn_reg.tn_aobj;
360 		if (uobj != NULL) {
361 			if (uobj->size != 0)
362 				atomic_subtract_long(&tmp->tm_pages_used, uobj->size);
363 			KASSERT((uobj->flags & OBJ_TMPFS) == 0,
364 			    ("leaked OBJ_TMPFS node %p vm_obj %p", node, uobj));
365 			vm_object_deallocate(uobj);
366 		}
367 		break;
368 
369 	default:
370 		panic("tmpfs_free_node: type %p %d", node, (int)node->tn_type);
371 	}
372 
373 	uma_zfree(tmp->tm_node_pool, node);
374 	TMPFS_LOCK(tmp);
375 	tmpfs_free_tmp(tmp);
376 	return (true);
377 }
378 
379 static __inline uint32_t
380 tmpfs_dirent_hash(const char *name, u_int len)
381 {
382 	uint32_t hash;
383 
384 	hash = fnv_32_buf(name, len, FNV1_32_INIT + len) & TMPFS_DIRCOOKIE_MASK;
385 #ifdef TMPFS_DEBUG_DIRCOOKIE_DUP
386 	hash &= 0xf;
387 #endif
388 	if (hash < TMPFS_DIRCOOKIE_MIN)
389 		hash += TMPFS_DIRCOOKIE_MIN;
390 
391 	return (hash);
392 }
393 
394 static __inline off_t
395 tmpfs_dirent_cookie(struct tmpfs_dirent *de)
396 {
397 	if (de == NULL)
398 		return (TMPFS_DIRCOOKIE_EOF);
399 
400 	MPASS(de->td_cookie >= TMPFS_DIRCOOKIE_MIN);
401 
402 	return (de->td_cookie);
403 }
404 
405 static __inline boolean_t
406 tmpfs_dirent_dup(struct tmpfs_dirent *de)
407 {
408 	return ((de->td_cookie & TMPFS_DIRCOOKIE_DUP) != 0);
409 }
410 
411 static __inline boolean_t
412 tmpfs_dirent_duphead(struct tmpfs_dirent *de)
413 {
414 	return ((de->td_cookie & TMPFS_DIRCOOKIE_DUPHEAD) != 0);
415 }
416 
417 void
418 tmpfs_dirent_init(struct tmpfs_dirent *de, const char *name, u_int namelen)
419 {
420 	de->td_hash = de->td_cookie = tmpfs_dirent_hash(name, namelen);
421 	memcpy(de->ud.td_name, name, namelen);
422 	de->td_namelen = namelen;
423 }
424 
425 /*
426  * Allocates a new directory entry for the node node with a name of name.
427  * The new directory entry is returned in *de.
428  *
429  * The link count of node is increased by one to reflect the new object
430  * referencing it.
431  *
432  * Returns zero on success or an appropriate error code on failure.
433  */
434 int
435 tmpfs_alloc_dirent(struct tmpfs_mount *tmp, struct tmpfs_node *node,
436     const char *name, u_int len, struct tmpfs_dirent **de)
437 {
438 	struct tmpfs_dirent *nde;
439 
440 	nde = uma_zalloc(tmp->tm_dirent_pool, M_WAITOK);
441 	nde->td_node = node;
442 	if (name != NULL) {
443 		nde->ud.td_name = malloc(len, M_TMPFSNAME, M_WAITOK);
444 		tmpfs_dirent_init(nde, name, len);
445 	} else
446 		nde->td_namelen = 0;
447 	if (node != NULL)
448 		node->tn_links++;
449 
450 	*de = nde;
451 
452 	return 0;
453 }
454 
455 /*
456  * Frees a directory entry.  It is the caller's responsibility to destroy
457  * the node referenced by it if needed.
458  *
459  * The link count of node is decreased by one to reflect the removal of an
460  * object that referenced it.  This only happens if 'node_exists' is true;
461  * otherwise the function will not access the node referred to by the
462  * directory entry, as it may already have been released from the outside.
463  */
464 void
465 tmpfs_free_dirent(struct tmpfs_mount *tmp, struct tmpfs_dirent *de)
466 {
467 	struct tmpfs_node *node;
468 
469 	node = de->td_node;
470 	if (node != NULL) {
471 		MPASS(node->tn_links > 0);
472 		node->tn_links--;
473 	}
474 	if (!tmpfs_dirent_duphead(de) && de->ud.td_name != NULL)
475 		free(de->ud.td_name, M_TMPFSNAME);
476 	uma_zfree(tmp->tm_dirent_pool, de);
477 }
478 
479 void
480 tmpfs_destroy_vobject(struct vnode *vp, vm_object_t obj)
481 {
482 
483 	ASSERT_VOP_ELOCKED(vp, "tmpfs_destroy_vobject");
484 	if (vp->v_type != VREG || obj == NULL)
485 		return;
486 
487 	VM_OBJECT_WLOCK(obj);
488 	VI_LOCK(vp);
489 	vm_object_clear_flag(obj, OBJ_TMPFS);
490 	obj->un_pager.swp.swp_tmpfs = NULL;
491 	if (vp->v_writecount < 0)
492 		vp->v_writecount = 0;
493 	VI_UNLOCK(vp);
494 	VM_OBJECT_WUNLOCK(obj);
495 }
496 
497 /*
498  * Need to clear v_object for insmntque failure.
499  */
500 static void
501 tmpfs_insmntque_dtr(struct vnode *vp, void *dtr_arg)
502 {
503 
504 	tmpfs_destroy_vobject(vp, vp->v_object);
505 	vp->v_object = NULL;
506 	vp->v_data = NULL;
507 	vp->v_op = &dead_vnodeops;
508 	vgone(vp);
509 	vput(vp);
510 }
511 
512 /*
513  * Allocates a new vnode for the node node or returns a new reference to
514  * an existing one if the node had already a vnode referencing it.  The
515  * resulting locked vnode is returned in *vpp.
516  *
517  * Returns zero on success or an appropriate error code on failure.
518  */
519 int
520 tmpfs_alloc_vp(struct mount *mp, struct tmpfs_node *node, int lkflag,
521     struct vnode **vpp)
522 {
523 	struct vnode *vp;
524 	struct tmpfs_mount *tm;
525 	vm_object_t object;
526 	int error;
527 
528 	error = 0;
529 	tm = VFS_TO_TMPFS(mp);
530 	TMPFS_NODE_LOCK(node);
531 	tmpfs_ref_node_locked(node);
532 loop:
533 	TMPFS_NODE_ASSERT_LOCKED(node);
534 	if ((vp = node->tn_vnode) != NULL) {
535 		MPASS((node->tn_vpstate & TMPFS_VNODE_DOOMED) == 0);
536 		VI_LOCK(vp);
537 		if ((node->tn_type == VDIR && node->tn_dir.tn_parent == NULL) ||
538 		    ((vp->v_iflag & VI_DOOMED) != 0 &&
539 		    (lkflag & LK_NOWAIT) != 0)) {
540 			VI_UNLOCK(vp);
541 			TMPFS_NODE_UNLOCK(node);
542 			error = ENOENT;
543 			vp = NULL;
544 			goto out;
545 		}
546 		if ((vp->v_iflag & VI_DOOMED) != 0) {
547 			VI_UNLOCK(vp);
548 			node->tn_vpstate |= TMPFS_VNODE_WRECLAIM;
549 			while ((node->tn_vpstate & TMPFS_VNODE_WRECLAIM) != 0) {
550 				msleep(&node->tn_vnode, TMPFS_NODE_MTX(node),
551 				    0, "tmpfsE", 0);
552 			}
553 			goto loop;
554 		}
555 		TMPFS_NODE_UNLOCK(node);
556 		error = vget(vp, lkflag | LK_INTERLOCK, curthread);
557 		if (error == ENOENT) {
558 			TMPFS_NODE_LOCK(node);
559 			goto loop;
560 		}
561 		if (error != 0) {
562 			vp = NULL;
563 			goto out;
564 		}
565 
566 		/*
567 		 * Make sure the vnode is still there after
568 		 * getting the interlock to avoid racing a free.
569 		 */
570 		if (node->tn_vnode == NULL || node->tn_vnode != vp) {
571 			vput(vp);
572 			TMPFS_NODE_LOCK(node);
573 			goto loop;
574 		}
575 
576 		goto out;
577 	}
578 
579 	if ((node->tn_vpstate & TMPFS_VNODE_DOOMED) ||
580 	    (node->tn_type == VDIR && node->tn_dir.tn_parent == NULL)) {
581 		TMPFS_NODE_UNLOCK(node);
582 		error = ENOENT;
583 		vp = NULL;
584 		goto out;
585 	}
586 
587 	/*
588 	 * otherwise lock the vp list while we call getnewvnode
589 	 * since that can block.
590 	 */
591 	if (node->tn_vpstate & TMPFS_VNODE_ALLOCATING) {
592 		node->tn_vpstate |= TMPFS_VNODE_WANT;
593 		error = msleep((caddr_t) &node->tn_vpstate,
594 		    TMPFS_NODE_MTX(node), 0, "tmpfs_alloc_vp", 0);
595 		if (error != 0)
596 			goto out;
597 		goto loop;
598 	} else
599 		node->tn_vpstate |= TMPFS_VNODE_ALLOCATING;
600 
601 	TMPFS_NODE_UNLOCK(node);
602 
603 	/* Get a new vnode and associate it with our node. */
604 	error = getnewvnode("tmpfs", mp, VFS_TO_TMPFS(mp)->tm_nonc ?
605 	    &tmpfs_vnodeop_nonc_entries : &tmpfs_vnodeop_entries, &vp);
606 	if (error != 0)
607 		goto unlock;
608 	MPASS(vp != NULL);
609 
610 	/* lkflag is ignored, the lock is exclusive */
611 	(void) vn_lock(vp, lkflag | LK_RETRY);
612 
613 	vp->v_data = node;
614 	vp->v_type = node->tn_type;
615 
616 	/* Type-specific initialization. */
617 	switch (node->tn_type) {
618 	case VBLK:
619 		/* FALLTHROUGH */
620 	case VCHR:
621 		/* FALLTHROUGH */
622 	case VLNK:
623 		/* FALLTHROUGH */
624 	case VSOCK:
625 		break;
626 	case VFIFO:
627 		vp->v_op = &tmpfs_fifoop_entries;
628 		break;
629 	case VREG:
630 		object = node->tn_reg.tn_aobj;
631 		VM_OBJECT_WLOCK(object);
632 		VI_LOCK(vp);
633 		KASSERT(vp->v_object == NULL, ("Not NULL v_object in tmpfs"));
634 		vp->v_object = object;
635 		object->un_pager.swp.swp_tmpfs = vp;
636 		vm_object_set_flag(object, OBJ_TMPFS);
637 		VI_UNLOCK(vp);
638 		VM_OBJECT_WUNLOCK(object);
639 		break;
640 	case VDIR:
641 		MPASS(node->tn_dir.tn_parent != NULL);
642 		if (node->tn_dir.tn_parent == node)
643 			vp->v_vflag |= VV_ROOT;
644 		break;
645 
646 	default:
647 		panic("tmpfs_alloc_vp: type %p %d", node, (int)node->tn_type);
648 	}
649 	if (vp->v_type != VFIFO)
650 		VN_LOCK_ASHARE(vp);
651 
652 	error = insmntque1(vp, mp, tmpfs_insmntque_dtr, NULL);
653 	if (error != 0)
654 		vp = NULL;
655 
656 unlock:
657 	TMPFS_NODE_LOCK(node);
658 
659 	MPASS(node->tn_vpstate & TMPFS_VNODE_ALLOCATING);
660 	node->tn_vpstate &= ~TMPFS_VNODE_ALLOCATING;
661 	node->tn_vnode = vp;
662 
663 	if (node->tn_vpstate & TMPFS_VNODE_WANT) {
664 		node->tn_vpstate &= ~TMPFS_VNODE_WANT;
665 		TMPFS_NODE_UNLOCK(node);
666 		wakeup((caddr_t) &node->tn_vpstate);
667 	} else
668 		TMPFS_NODE_UNLOCK(node);
669 
670 out:
671 	if (error == 0) {
672 		*vpp = vp;
673 
674 #ifdef INVARIANTS
675 		MPASS(*vpp != NULL && VOP_ISLOCKED(*vpp));
676 		TMPFS_NODE_LOCK(node);
677 		MPASS(*vpp == node->tn_vnode);
678 		TMPFS_NODE_UNLOCK(node);
679 #endif
680 	}
681 	tmpfs_free_node(tm, node);
682 
683 	return (error);
684 }
685 
686 /*
687  * Destroys the association between the vnode vp and the node it
688  * references.
689  */
690 void
691 tmpfs_free_vp(struct vnode *vp)
692 {
693 	struct tmpfs_node *node;
694 
695 	node = VP_TO_TMPFS_NODE(vp);
696 
697 	TMPFS_NODE_ASSERT_LOCKED(node);
698 	node->tn_vnode = NULL;
699 	if ((node->tn_vpstate & TMPFS_VNODE_WRECLAIM) != 0)
700 		wakeup(&node->tn_vnode);
701 	node->tn_vpstate &= ~TMPFS_VNODE_WRECLAIM;
702 	vp->v_data = NULL;
703 }
704 
705 /*
706  * Allocates a new file of type 'type' and adds it to the parent directory
707  * 'dvp'; this addition is done using the component name given in 'cnp'.
708  * The ownership of the new file is automatically assigned based on the
709  * credentials of the caller (through 'cnp'), the group is set based on
710  * the parent directory and the mode is determined from the 'vap' argument.
711  * If successful, *vpp holds a vnode to the newly created file and zero
712  * is returned.  Otherwise *vpp is NULL and the function returns an
713  * appropriate error code.
714  */
715 int
716 tmpfs_alloc_file(struct vnode *dvp, struct vnode **vpp, struct vattr *vap,
717     struct componentname *cnp, const char *target)
718 {
719 	int error;
720 	struct tmpfs_dirent *de;
721 	struct tmpfs_mount *tmp;
722 	struct tmpfs_node *dnode;
723 	struct tmpfs_node *node;
724 	struct tmpfs_node *parent;
725 
726 	ASSERT_VOP_ELOCKED(dvp, "tmpfs_alloc_file");
727 	MPASS(cnp->cn_flags & HASBUF);
728 
729 	tmp = VFS_TO_TMPFS(dvp->v_mount);
730 	dnode = VP_TO_TMPFS_DIR(dvp);
731 	*vpp = NULL;
732 
733 	/* If the entry we are creating is a directory, we cannot overflow
734 	 * the number of links of its parent, because it will get a new
735 	 * link. */
736 	if (vap->va_type == VDIR) {
737 		/* Ensure that we do not overflow the maximum number of links
738 		 * imposed by the system. */
739 		MPASS(dnode->tn_links <= TMPFS_LINK_MAX);
740 		if (dnode->tn_links == TMPFS_LINK_MAX) {
741 			return (EMLINK);
742 		}
743 
744 		parent = dnode;
745 		MPASS(parent != NULL);
746 	} else
747 		parent = NULL;
748 
749 	/* Allocate a node that represents the new file. */
750 	error = tmpfs_alloc_node(dvp->v_mount, tmp, vap->va_type,
751 	    cnp->cn_cred->cr_uid, dnode->tn_gid, vap->va_mode, parent,
752 	    target, vap->va_rdev, &node);
753 	if (error != 0)
754 		return (error);
755 
756 	/* Allocate a directory entry that points to the new file. */
757 	error = tmpfs_alloc_dirent(tmp, node, cnp->cn_nameptr, cnp->cn_namelen,
758 	    &de);
759 	if (error != 0) {
760 		tmpfs_free_node(tmp, node);
761 		return (error);
762 	}
763 
764 	/* Allocate a vnode for the new file. */
765 	error = tmpfs_alloc_vp(dvp->v_mount, node, LK_EXCLUSIVE, vpp);
766 	if (error != 0) {
767 		tmpfs_free_dirent(tmp, de);
768 		tmpfs_free_node(tmp, node);
769 		return (error);
770 	}
771 
772 	/* Now that all required items are allocated, we can proceed to
773 	 * insert the new node into the directory, an operation that
774 	 * cannot fail. */
775 	if (cnp->cn_flags & ISWHITEOUT)
776 		tmpfs_dir_whiteout_remove(dvp, cnp);
777 	tmpfs_dir_attach(dvp, de);
778 	return (0);
779 }
780 
781 struct tmpfs_dirent *
782 tmpfs_dir_first(struct tmpfs_node *dnode, struct tmpfs_dir_cursor *dc)
783 {
784 	struct tmpfs_dirent *de;
785 
786 	de = RB_MIN(tmpfs_dir, &dnode->tn_dir.tn_dirhead);
787 	dc->tdc_tree = de;
788 	if (de != NULL && tmpfs_dirent_duphead(de))
789 		de = LIST_FIRST(&de->ud.td_duphead);
790 	dc->tdc_current = de;
791 
792 	return (dc->tdc_current);
793 }
794 
795 struct tmpfs_dirent *
796 tmpfs_dir_next(struct tmpfs_node *dnode, struct tmpfs_dir_cursor *dc)
797 {
798 	struct tmpfs_dirent *de;
799 
800 	MPASS(dc->tdc_tree != NULL);
801 	if (tmpfs_dirent_dup(dc->tdc_current)) {
802 		dc->tdc_current = LIST_NEXT(dc->tdc_current, uh.td_dup.entries);
803 		if (dc->tdc_current != NULL)
804 			return (dc->tdc_current);
805 	}
806 	dc->tdc_tree = dc->tdc_current = RB_NEXT(tmpfs_dir,
807 	    &dnode->tn_dir.tn_dirhead, dc->tdc_tree);
808 	if ((de = dc->tdc_current) != NULL && tmpfs_dirent_duphead(de)) {
809 		dc->tdc_current = LIST_FIRST(&de->ud.td_duphead);
810 		MPASS(dc->tdc_current != NULL);
811 	}
812 
813 	return (dc->tdc_current);
814 }
815 
816 /* Lookup directory entry in RB-Tree. Function may return duphead entry. */
817 static struct tmpfs_dirent *
818 tmpfs_dir_xlookup_hash(struct tmpfs_node *dnode, uint32_t hash)
819 {
820 	struct tmpfs_dirent *de, dekey;
821 
822 	dekey.td_hash = hash;
823 	de = RB_FIND(tmpfs_dir, &dnode->tn_dir.tn_dirhead, &dekey);
824 	return (de);
825 }
826 
827 /* Lookup directory entry by cookie, initialize directory cursor accordingly. */
828 static struct tmpfs_dirent *
829 tmpfs_dir_lookup_cookie(struct tmpfs_node *node, off_t cookie,
830     struct tmpfs_dir_cursor *dc)
831 {
832 	struct tmpfs_dir *dirhead = &node->tn_dir.tn_dirhead;
833 	struct tmpfs_dirent *de, dekey;
834 
835 	MPASS(cookie >= TMPFS_DIRCOOKIE_MIN);
836 
837 	if (cookie == node->tn_dir.tn_readdir_lastn &&
838 	    (de = node->tn_dir.tn_readdir_lastp) != NULL) {
839 		/* Protect against possible race, tn_readdir_last[pn]
840 		 * may be updated with only shared vnode lock held. */
841 		if (cookie == tmpfs_dirent_cookie(de))
842 			goto out;
843 	}
844 
845 	if ((cookie & TMPFS_DIRCOOKIE_DUP) != 0) {
846 		LIST_FOREACH(de, &node->tn_dir.tn_dupindex,
847 		    uh.td_dup.index_entries) {
848 			MPASS(tmpfs_dirent_dup(de));
849 			if (de->td_cookie == cookie)
850 				goto out;
851 			/* dupindex list is sorted. */
852 			if (de->td_cookie < cookie) {
853 				de = NULL;
854 				goto out;
855 			}
856 		}
857 		MPASS(de == NULL);
858 		goto out;
859 	}
860 
861 	if ((cookie & TMPFS_DIRCOOKIE_MASK) != cookie) {
862 		de = NULL;
863 	} else {
864 		dekey.td_hash = cookie;
865 		/* Recover if direntry for cookie was removed */
866 		de = RB_NFIND(tmpfs_dir, dirhead, &dekey);
867 	}
868 	dc->tdc_tree = de;
869 	dc->tdc_current = de;
870 	if (de != NULL && tmpfs_dirent_duphead(de)) {
871 		dc->tdc_current = LIST_FIRST(&de->ud.td_duphead);
872 		MPASS(dc->tdc_current != NULL);
873 	}
874 	return (dc->tdc_current);
875 
876 out:
877 	dc->tdc_tree = de;
878 	dc->tdc_current = de;
879 	if (de != NULL && tmpfs_dirent_dup(de))
880 		dc->tdc_tree = tmpfs_dir_xlookup_hash(node,
881 		    de->td_hash);
882 	return (dc->tdc_current);
883 }
884 
885 /*
886  * Looks for a directory entry in the directory represented by node.
887  * 'cnp' describes the name of the entry to look for.  Note that the .
888  * and .. components are not allowed as they do not physically exist
889  * within directories.
890  *
891  * Returns a pointer to the entry when found, otherwise NULL.
892  */
893 struct tmpfs_dirent *
894 tmpfs_dir_lookup(struct tmpfs_node *node, struct tmpfs_node *f,
895     struct componentname *cnp)
896 {
897 	struct tmpfs_dir_duphead *duphead;
898 	struct tmpfs_dirent *de;
899 	uint32_t hash;
900 
901 	MPASS(IMPLIES(cnp->cn_namelen == 1, cnp->cn_nameptr[0] != '.'));
902 	MPASS(IMPLIES(cnp->cn_namelen == 2, !(cnp->cn_nameptr[0] == '.' &&
903 	    cnp->cn_nameptr[1] == '.')));
904 	TMPFS_VALIDATE_DIR(node);
905 
906 	hash = tmpfs_dirent_hash(cnp->cn_nameptr, cnp->cn_namelen);
907 	de = tmpfs_dir_xlookup_hash(node, hash);
908 	if (de != NULL && tmpfs_dirent_duphead(de)) {
909 		duphead = &de->ud.td_duphead;
910 		LIST_FOREACH(de, duphead, uh.td_dup.entries) {
911 			if (TMPFS_DIRENT_MATCHES(de, cnp->cn_nameptr,
912 			    cnp->cn_namelen))
913 				break;
914 		}
915 	} else if (de != NULL) {
916 		if (!TMPFS_DIRENT_MATCHES(de, cnp->cn_nameptr,
917 		    cnp->cn_namelen))
918 			de = NULL;
919 	}
920 	if (de != NULL && f != NULL && de->td_node != f)
921 		de = NULL;
922 
923 	return (de);
924 }
925 
926 /*
927  * Attach duplicate-cookie directory entry nde to dnode and insert to dupindex
928  * list, allocate new cookie value.
929  */
930 static void
931 tmpfs_dir_attach_dup(struct tmpfs_node *dnode,
932     struct tmpfs_dir_duphead *duphead, struct tmpfs_dirent *nde)
933 {
934 	struct tmpfs_dir_duphead *dupindex;
935 	struct tmpfs_dirent *de, *pde;
936 
937 	dupindex = &dnode->tn_dir.tn_dupindex;
938 	de = LIST_FIRST(dupindex);
939 	if (de == NULL || de->td_cookie < TMPFS_DIRCOOKIE_DUP_MAX) {
940 		if (de == NULL)
941 			nde->td_cookie = TMPFS_DIRCOOKIE_DUP_MIN;
942 		else
943 			nde->td_cookie = de->td_cookie + 1;
944 		MPASS(tmpfs_dirent_dup(nde));
945 		LIST_INSERT_HEAD(dupindex, nde, uh.td_dup.index_entries);
946 		LIST_INSERT_HEAD(duphead, nde, uh.td_dup.entries);
947 		return;
948 	}
949 
950 	/*
951 	 * Cookie numbers are near exhaustion. Scan dupindex list for unused
952 	 * numbers. dupindex list is sorted in descending order. Keep it so
953 	 * after inserting nde.
954 	 */
955 	while (1) {
956 		pde = de;
957 		de = LIST_NEXT(de, uh.td_dup.index_entries);
958 		if (de == NULL && pde->td_cookie != TMPFS_DIRCOOKIE_DUP_MIN) {
959 			/*
960 			 * Last element of the index doesn't have minimal cookie
961 			 * value, use it.
962 			 */
963 			nde->td_cookie = TMPFS_DIRCOOKIE_DUP_MIN;
964 			LIST_INSERT_AFTER(pde, nde, uh.td_dup.index_entries);
965 			LIST_INSERT_HEAD(duphead, nde, uh.td_dup.entries);
966 			return;
967 		} else if (de == NULL) {
968 			/*
969 			 * We are so lucky have 2^30 hash duplicates in single
970 			 * directory :) Return largest possible cookie value.
971 			 * It should be fine except possible issues with
972 			 * VOP_READDIR restart.
973 			 */
974 			nde->td_cookie = TMPFS_DIRCOOKIE_DUP_MAX;
975 			LIST_INSERT_HEAD(dupindex, nde,
976 			    uh.td_dup.index_entries);
977 			LIST_INSERT_HEAD(duphead, nde, uh.td_dup.entries);
978 			return;
979 		}
980 		if (de->td_cookie + 1 == pde->td_cookie ||
981 		    de->td_cookie >= TMPFS_DIRCOOKIE_DUP_MAX)
982 			continue;	/* No hole or invalid cookie. */
983 		nde->td_cookie = de->td_cookie + 1;
984 		MPASS(tmpfs_dirent_dup(nde));
985 		MPASS(pde->td_cookie > nde->td_cookie);
986 		MPASS(nde->td_cookie > de->td_cookie);
987 		LIST_INSERT_BEFORE(de, nde, uh.td_dup.index_entries);
988 		LIST_INSERT_HEAD(duphead, nde, uh.td_dup.entries);
989 		return;
990 	}
991 }
992 
993 /*
994  * Attaches the directory entry de to the directory represented by vp.
995  * Note that this does not change the link count of the node pointed by
996  * the directory entry, as this is done by tmpfs_alloc_dirent.
997  */
998 void
999 tmpfs_dir_attach(struct vnode *vp, struct tmpfs_dirent *de)
1000 {
1001 	struct tmpfs_node *dnode;
1002 	struct tmpfs_dirent *xde, *nde;
1003 
1004 	ASSERT_VOP_ELOCKED(vp, __func__);
1005 	MPASS(de->td_namelen > 0);
1006 	MPASS(de->td_hash >= TMPFS_DIRCOOKIE_MIN);
1007 	MPASS(de->td_cookie == de->td_hash);
1008 
1009 	dnode = VP_TO_TMPFS_DIR(vp);
1010 	dnode->tn_dir.tn_readdir_lastn = 0;
1011 	dnode->tn_dir.tn_readdir_lastp = NULL;
1012 
1013 	MPASS(!tmpfs_dirent_dup(de));
1014 	xde = RB_INSERT(tmpfs_dir, &dnode->tn_dir.tn_dirhead, de);
1015 	if (xde != NULL && tmpfs_dirent_duphead(xde))
1016 		tmpfs_dir_attach_dup(dnode, &xde->ud.td_duphead, de);
1017 	else if (xde != NULL) {
1018 		/*
1019 		 * Allocate new duphead. Swap xde with duphead to avoid
1020 		 * adding/removing elements with the same hash.
1021 		 */
1022 		MPASS(!tmpfs_dirent_dup(xde));
1023 		tmpfs_alloc_dirent(VFS_TO_TMPFS(vp->v_mount), NULL, NULL, 0,
1024 		    &nde);
1025 		/* *nde = *xde; XXX gcc 4.2.1 may generate invalid code. */
1026 		memcpy(nde, xde, sizeof(*xde));
1027 		xde->td_cookie |= TMPFS_DIRCOOKIE_DUPHEAD;
1028 		LIST_INIT(&xde->ud.td_duphead);
1029 		xde->td_namelen = 0;
1030 		xde->td_node = NULL;
1031 		tmpfs_dir_attach_dup(dnode, &xde->ud.td_duphead, nde);
1032 		tmpfs_dir_attach_dup(dnode, &xde->ud.td_duphead, de);
1033 	}
1034 	dnode->tn_size += sizeof(struct tmpfs_dirent);
1035 	dnode->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \
1036 	    TMPFS_NODE_MODIFIED;
1037 	tmpfs_update(vp);
1038 }
1039 
1040 /*
1041  * Detaches the directory entry de from the directory represented by vp.
1042  * Note that this does not change the link count of the node pointed by
1043  * the directory entry, as this is done by tmpfs_free_dirent.
1044  */
1045 void
1046 tmpfs_dir_detach(struct vnode *vp, struct tmpfs_dirent *de)
1047 {
1048 	struct tmpfs_mount *tmp;
1049 	struct tmpfs_dir *head;
1050 	struct tmpfs_node *dnode;
1051 	struct tmpfs_dirent *xde;
1052 
1053 	ASSERT_VOP_ELOCKED(vp, __func__);
1054 
1055 	dnode = VP_TO_TMPFS_DIR(vp);
1056 	head = &dnode->tn_dir.tn_dirhead;
1057 	dnode->tn_dir.tn_readdir_lastn = 0;
1058 	dnode->tn_dir.tn_readdir_lastp = NULL;
1059 
1060 	if (tmpfs_dirent_dup(de)) {
1061 		/* Remove duphead if de was last entry. */
1062 		if (LIST_NEXT(de, uh.td_dup.entries) == NULL) {
1063 			xde = tmpfs_dir_xlookup_hash(dnode, de->td_hash);
1064 			MPASS(tmpfs_dirent_duphead(xde));
1065 		} else
1066 			xde = NULL;
1067 		LIST_REMOVE(de, uh.td_dup.entries);
1068 		LIST_REMOVE(de, uh.td_dup.index_entries);
1069 		if (xde != NULL) {
1070 			if (LIST_EMPTY(&xde->ud.td_duphead)) {
1071 				RB_REMOVE(tmpfs_dir, head, xde);
1072 				tmp = VFS_TO_TMPFS(vp->v_mount);
1073 				MPASS(xde->td_node == NULL);
1074 				tmpfs_free_dirent(tmp, xde);
1075 			}
1076 		}
1077 		de->td_cookie = de->td_hash;
1078 	} else
1079 		RB_REMOVE(tmpfs_dir, head, de);
1080 
1081 	dnode->tn_size -= sizeof(struct tmpfs_dirent);
1082 	dnode->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \
1083 	    TMPFS_NODE_MODIFIED;
1084 	tmpfs_update(vp);
1085 }
1086 
1087 void
1088 tmpfs_dir_destroy(struct tmpfs_mount *tmp, struct tmpfs_node *dnode)
1089 {
1090 	struct tmpfs_dirent *de, *dde, *nde;
1091 
1092 	RB_FOREACH_SAFE(de, tmpfs_dir, &dnode->tn_dir.tn_dirhead, nde) {
1093 		RB_REMOVE(tmpfs_dir, &dnode->tn_dir.tn_dirhead, de);
1094 		/* Node may already be destroyed. */
1095 		de->td_node = NULL;
1096 		if (tmpfs_dirent_duphead(de)) {
1097 			while ((dde = LIST_FIRST(&de->ud.td_duphead)) != NULL) {
1098 				LIST_REMOVE(dde, uh.td_dup.entries);
1099 				dde->td_node = NULL;
1100 				tmpfs_free_dirent(tmp, dde);
1101 			}
1102 		}
1103 		tmpfs_free_dirent(tmp, de);
1104 	}
1105 }
1106 
1107 /*
1108  * Helper function for tmpfs_readdir.  Creates a '.' entry for the given
1109  * directory and returns it in the uio space.  The function returns 0
1110  * on success, -1 if there was not enough space in the uio structure to
1111  * hold the directory entry or an appropriate error code if another
1112  * error happens.
1113  */
1114 static int
1115 tmpfs_dir_getdotdent(struct tmpfs_mount *tm, struct tmpfs_node *node,
1116     struct uio *uio)
1117 {
1118 	int error;
1119 	struct dirent dent;
1120 
1121 	TMPFS_VALIDATE_DIR(node);
1122 	MPASS(uio->uio_offset == TMPFS_DIRCOOKIE_DOT);
1123 
1124 	dent.d_fileno = node->tn_id;
1125 	dent.d_type = DT_DIR;
1126 	dent.d_namlen = 1;
1127 	dent.d_name[0] = '.';
1128 	dent.d_reclen = GENERIC_DIRSIZ(&dent);
1129 	dirent_terminate(&dent);
1130 
1131 	if (dent.d_reclen > uio->uio_resid)
1132 		error = EJUSTRETURN;
1133 	else
1134 		error = uiomove(&dent, dent.d_reclen, uio);
1135 
1136 	tmpfs_set_status(tm, node, TMPFS_NODE_ACCESSED);
1137 
1138 	return (error);
1139 }
1140 
1141 /*
1142  * Helper function for tmpfs_readdir.  Creates a '..' entry for the given
1143  * directory and returns it in the uio space.  The function returns 0
1144  * on success, -1 if there was not enough space in the uio structure to
1145  * hold the directory entry or an appropriate error code if another
1146  * error happens.
1147  */
1148 static int
1149 tmpfs_dir_getdotdotdent(struct tmpfs_mount *tm, struct tmpfs_node *node,
1150     struct uio *uio)
1151 {
1152 	int error;
1153 	struct dirent dent;
1154 
1155 	TMPFS_VALIDATE_DIR(node);
1156 	MPASS(uio->uio_offset == TMPFS_DIRCOOKIE_DOTDOT);
1157 
1158 	/*
1159 	 * Return ENOENT if the current node is already removed.
1160 	 */
1161 	TMPFS_ASSERT_LOCKED(node);
1162 	if (node->tn_dir.tn_parent == NULL)
1163 		return (ENOENT);
1164 
1165 	TMPFS_NODE_LOCK(node->tn_dir.tn_parent);
1166 	dent.d_fileno = node->tn_dir.tn_parent->tn_id;
1167 	TMPFS_NODE_UNLOCK(node->tn_dir.tn_parent);
1168 
1169 	dent.d_type = DT_DIR;
1170 	dent.d_namlen = 2;
1171 	dent.d_name[0] = '.';
1172 	dent.d_name[1] = '.';
1173 	dent.d_reclen = GENERIC_DIRSIZ(&dent);
1174 	dirent_terminate(&dent);
1175 
1176 	if (dent.d_reclen > uio->uio_resid)
1177 		error = EJUSTRETURN;
1178 	else
1179 		error = uiomove(&dent, dent.d_reclen, uio);
1180 
1181 	tmpfs_set_status(tm, node, TMPFS_NODE_ACCESSED);
1182 
1183 	return (error);
1184 }
1185 
1186 /*
1187  * Helper function for tmpfs_readdir.  Returns as much directory entries
1188  * as can fit in the uio space.  The read starts at uio->uio_offset.
1189  * The function returns 0 on success, -1 if there was not enough space
1190  * in the uio structure to hold the directory entry or an appropriate
1191  * error code if another error happens.
1192  */
1193 int
1194 tmpfs_dir_getdents(struct tmpfs_mount *tm, struct tmpfs_node *node,
1195     struct uio *uio, int maxcookies, u_long *cookies, int *ncookies)
1196 {
1197 	struct tmpfs_dir_cursor dc;
1198 	struct tmpfs_dirent *de;
1199 	off_t off;
1200 	int error;
1201 
1202 	TMPFS_VALIDATE_DIR(node);
1203 
1204 	off = 0;
1205 
1206 	/*
1207 	 * Lookup the node from the current offset.  The starting offset of
1208 	 * 0 will lookup both '.' and '..', and then the first real entry,
1209 	 * or EOF if there are none.  Then find all entries for the dir that
1210 	 * fit into the buffer.  Once no more entries are found (de == NULL),
1211 	 * the offset is set to TMPFS_DIRCOOKIE_EOF, which will cause the next
1212 	 * call to return 0.
1213 	 */
1214 	switch (uio->uio_offset) {
1215 	case TMPFS_DIRCOOKIE_DOT:
1216 		error = tmpfs_dir_getdotdent(tm, node, uio);
1217 		if (error != 0)
1218 			return (error);
1219 		uio->uio_offset = TMPFS_DIRCOOKIE_DOTDOT;
1220 		if (cookies != NULL)
1221 			cookies[(*ncookies)++] = off = uio->uio_offset;
1222 		/* FALLTHROUGH */
1223 	case TMPFS_DIRCOOKIE_DOTDOT:
1224 		error = tmpfs_dir_getdotdotdent(tm, node, uio);
1225 		if (error != 0)
1226 			return (error);
1227 		de = tmpfs_dir_first(node, &dc);
1228 		uio->uio_offset = tmpfs_dirent_cookie(de);
1229 		if (cookies != NULL)
1230 			cookies[(*ncookies)++] = off = uio->uio_offset;
1231 		/* EOF. */
1232 		if (de == NULL)
1233 			return (0);
1234 		break;
1235 	case TMPFS_DIRCOOKIE_EOF:
1236 		return (0);
1237 	default:
1238 		de = tmpfs_dir_lookup_cookie(node, uio->uio_offset, &dc);
1239 		if (de == NULL)
1240 			return (EINVAL);
1241 		if (cookies != NULL)
1242 			off = tmpfs_dirent_cookie(de);
1243 	}
1244 
1245 	/* Read as much entries as possible; i.e., until we reach the end of
1246 	 * the directory or we exhaust uio space. */
1247 	do {
1248 		struct dirent d;
1249 
1250 		/* Create a dirent structure representing the current
1251 		 * tmpfs_node and fill it. */
1252 		if (de->td_node == NULL) {
1253 			d.d_fileno = 1;
1254 			d.d_type = DT_WHT;
1255 		} else {
1256 			d.d_fileno = de->td_node->tn_id;
1257 			switch (de->td_node->tn_type) {
1258 			case VBLK:
1259 				d.d_type = DT_BLK;
1260 				break;
1261 
1262 			case VCHR:
1263 				d.d_type = DT_CHR;
1264 				break;
1265 
1266 			case VDIR:
1267 				d.d_type = DT_DIR;
1268 				break;
1269 
1270 			case VFIFO:
1271 				d.d_type = DT_FIFO;
1272 				break;
1273 
1274 			case VLNK:
1275 				d.d_type = DT_LNK;
1276 				break;
1277 
1278 			case VREG:
1279 				d.d_type = DT_REG;
1280 				break;
1281 
1282 			case VSOCK:
1283 				d.d_type = DT_SOCK;
1284 				break;
1285 
1286 			default:
1287 				panic("tmpfs_dir_getdents: type %p %d",
1288 				    de->td_node, (int)de->td_node->tn_type);
1289 			}
1290 		}
1291 		d.d_namlen = de->td_namelen;
1292 		MPASS(de->td_namelen < sizeof(d.d_name));
1293 		(void)memcpy(d.d_name, de->ud.td_name, de->td_namelen);
1294 		d.d_reclen = GENERIC_DIRSIZ(&d);
1295 		dirent_terminate(&d);
1296 
1297 		/* Stop reading if the directory entry we are treating is
1298 		 * bigger than the amount of data that can be returned. */
1299 		if (d.d_reclen > uio->uio_resid) {
1300 			error = EJUSTRETURN;
1301 			break;
1302 		}
1303 
1304 		/* Copy the new dirent structure into the output buffer and
1305 		 * advance pointers. */
1306 		error = uiomove(&d, d.d_reclen, uio);
1307 		if (error == 0) {
1308 			de = tmpfs_dir_next(node, &dc);
1309 			if (cookies != NULL) {
1310 				off = tmpfs_dirent_cookie(de);
1311 				MPASS(*ncookies < maxcookies);
1312 				cookies[(*ncookies)++] = off;
1313 			}
1314 		}
1315 	} while (error == 0 && uio->uio_resid > 0 && de != NULL);
1316 
1317 	/* Skip setting off when using cookies as it is already done above. */
1318 	if (cookies == NULL)
1319 		off = tmpfs_dirent_cookie(de);
1320 
1321 	/* Update the offset and cache. */
1322 	uio->uio_offset = off;
1323 	node->tn_dir.tn_readdir_lastn = off;
1324 	node->tn_dir.tn_readdir_lastp = de;
1325 
1326 	tmpfs_set_status(tm, node, TMPFS_NODE_ACCESSED);
1327 	return error;
1328 }
1329 
1330 int
1331 tmpfs_dir_whiteout_add(struct vnode *dvp, struct componentname *cnp)
1332 {
1333 	struct tmpfs_dirent *de;
1334 	int error;
1335 
1336 	error = tmpfs_alloc_dirent(VFS_TO_TMPFS(dvp->v_mount), NULL,
1337 	    cnp->cn_nameptr, cnp->cn_namelen, &de);
1338 	if (error != 0)
1339 		return (error);
1340 	tmpfs_dir_attach(dvp, de);
1341 	return (0);
1342 }
1343 
1344 void
1345 tmpfs_dir_whiteout_remove(struct vnode *dvp, struct componentname *cnp)
1346 {
1347 	struct tmpfs_dirent *de;
1348 
1349 	de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(dvp), NULL, cnp);
1350 	MPASS(de != NULL && de->td_node == NULL);
1351 	tmpfs_dir_detach(dvp, de);
1352 	tmpfs_free_dirent(VFS_TO_TMPFS(dvp->v_mount), de);
1353 }
1354 
1355 /*
1356  * Resizes the aobj associated with the regular file pointed to by 'vp' to the
1357  * size 'newsize'.  'vp' must point to a vnode that represents a regular file.
1358  * 'newsize' must be positive.
1359  *
1360  * Returns zero on success or an appropriate error code on failure.
1361  */
1362 int
1363 tmpfs_reg_resize(struct vnode *vp, off_t newsize, boolean_t ignerr)
1364 {
1365 	struct tmpfs_mount *tmp;
1366 	struct tmpfs_node *node;
1367 	vm_object_t uobj;
1368 	vm_page_t m;
1369 	vm_pindex_t idx, newpages, oldpages;
1370 	off_t oldsize;
1371 	int base, rv;
1372 
1373 	MPASS(vp->v_type == VREG);
1374 	MPASS(newsize >= 0);
1375 
1376 	node = VP_TO_TMPFS_NODE(vp);
1377 	uobj = node->tn_reg.tn_aobj;
1378 	tmp = VFS_TO_TMPFS(vp->v_mount);
1379 
1380 	/*
1381 	 * Convert the old and new sizes to the number of pages needed to
1382 	 * store them.  It may happen that we do not need to do anything
1383 	 * because the last allocated page can accommodate the change on
1384 	 * its own.
1385 	 */
1386 	oldsize = node->tn_size;
1387 	oldpages = OFF_TO_IDX(oldsize + PAGE_MASK);
1388 	MPASS(oldpages == uobj->size);
1389 	newpages = OFF_TO_IDX(newsize + PAGE_MASK);
1390 
1391 	if (__predict_true(newpages == oldpages && newsize >= oldsize)) {
1392 		node->tn_size = newsize;
1393 		return (0);
1394 	}
1395 
1396 	if (newpages > oldpages &&
1397 	    tmpfs_pages_check_avail(tmp, newpages - oldpages) == 0)
1398 		return (ENOSPC);
1399 
1400 	VM_OBJECT_WLOCK(uobj);
1401 	if (newsize < oldsize) {
1402 		/*
1403 		 * Zero the truncated part of the last page.
1404 		 */
1405 		base = newsize & PAGE_MASK;
1406 		if (base != 0) {
1407 			idx = OFF_TO_IDX(newsize);
1408 retry:
1409 			m = vm_page_lookup(uobj, idx);
1410 			if (m != NULL) {
1411 				if (vm_page_sleep_if_busy(m, "tmfssz"))
1412 					goto retry;
1413 				MPASS(m->valid == VM_PAGE_BITS_ALL);
1414 			} else if (vm_pager_has_page(uobj, idx, NULL, NULL)) {
1415 				m = vm_page_alloc(uobj, idx, VM_ALLOC_NORMAL |
1416 				    VM_ALLOC_WAITFAIL);
1417 				if (m == NULL)
1418 					goto retry;
1419 				rv = vm_pager_get_pages(uobj, &m, 1, NULL,
1420 				    NULL);
1421 				vm_page_lock(m);
1422 				if (rv == VM_PAGER_OK) {
1423 					/*
1424 					 * Since the page was not resident,
1425 					 * and therefore not recently
1426 					 * accessed, immediately enqueue it
1427 					 * for asynchronous laundering.  The
1428 					 * current operation is not regarded
1429 					 * as an access.
1430 					 */
1431 					vm_page_launder(m);
1432 					vm_page_unlock(m);
1433 					vm_page_xunbusy(m);
1434 				} else {
1435 					vm_page_free(m);
1436 					vm_page_unlock(m);
1437 					if (ignerr)
1438 						m = NULL;
1439 					else {
1440 						VM_OBJECT_WUNLOCK(uobj);
1441 						return (EIO);
1442 					}
1443 				}
1444 			}
1445 			if (m != NULL) {
1446 				pmap_zero_page_area(m, base, PAGE_SIZE - base);
1447 				vm_page_dirty(m);
1448 				vm_pager_page_unswapped(m);
1449 			}
1450 		}
1451 
1452 		/*
1453 		 * Release any swap space and free any whole pages.
1454 		 */
1455 		if (newpages < oldpages) {
1456 			swap_pager_freespace(uobj, newpages, oldpages -
1457 			    newpages);
1458 			vm_object_page_remove(uobj, newpages, 0, 0);
1459 		}
1460 	}
1461 	uobj->size = newpages;
1462 	VM_OBJECT_WUNLOCK(uobj);
1463 
1464 	atomic_add_long(&tmp->tm_pages_used, newpages - oldpages);
1465 
1466 	node->tn_size = newsize;
1467 	return (0);
1468 }
1469 
1470 void
1471 tmpfs_check_mtime(struct vnode *vp)
1472 {
1473 	struct tmpfs_node *node;
1474 	struct vm_object *obj;
1475 
1476 	ASSERT_VOP_ELOCKED(vp, "check_mtime");
1477 	if (vp->v_type != VREG)
1478 		return;
1479 	obj = vp->v_object;
1480 	KASSERT((obj->flags & (OBJ_TMPFS_NODE | OBJ_TMPFS)) ==
1481 	    (OBJ_TMPFS_NODE | OBJ_TMPFS), ("non-tmpfs obj"));
1482 	/* unlocked read */
1483 	if ((obj->flags & OBJ_TMPFS_DIRTY) != 0) {
1484 		VM_OBJECT_WLOCK(obj);
1485 		if ((obj->flags & OBJ_TMPFS_DIRTY) != 0) {
1486 			obj->flags &= ~OBJ_TMPFS_DIRTY;
1487 			node = VP_TO_TMPFS_NODE(vp);
1488 			node->tn_status |= TMPFS_NODE_MODIFIED |
1489 			    TMPFS_NODE_CHANGED;
1490 		}
1491 		VM_OBJECT_WUNLOCK(obj);
1492 	}
1493 }
1494 
1495 /*
1496  * Change flags of the given vnode.
1497  * Caller should execute tmpfs_update on vp after a successful execution.
1498  * The vnode must be locked on entry and remain locked on exit.
1499  */
1500 int
1501 tmpfs_chflags(struct vnode *vp, u_long flags, struct ucred *cred,
1502     struct thread *p)
1503 {
1504 	int error;
1505 	struct tmpfs_node *node;
1506 
1507 	ASSERT_VOP_ELOCKED(vp, "chflags");
1508 
1509 	node = VP_TO_TMPFS_NODE(vp);
1510 
1511 	if ((flags & ~(SF_APPEND | SF_ARCHIVED | SF_IMMUTABLE | SF_NOUNLINK |
1512 	    UF_APPEND | UF_ARCHIVE | UF_HIDDEN | UF_IMMUTABLE | UF_NODUMP |
1513 	    UF_NOUNLINK | UF_OFFLINE | UF_OPAQUE | UF_READONLY | UF_REPARSE |
1514 	    UF_SPARSE | UF_SYSTEM)) != 0)
1515 		return (EOPNOTSUPP);
1516 
1517 	/* Disallow this operation if the file system is mounted read-only. */
1518 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
1519 		return EROFS;
1520 
1521 	/*
1522 	 * Callers may only modify the file flags on objects they
1523 	 * have VADMIN rights for.
1524 	 */
1525 	if ((error = VOP_ACCESS(vp, VADMIN, cred, p)))
1526 		return (error);
1527 	/*
1528 	 * Unprivileged processes are not permitted to unset system
1529 	 * flags, or modify flags if any system flags are set.
1530 	 */
1531 	if (!priv_check_cred(cred, PRIV_VFS_SYSFLAGS)) {
1532 		if (node->tn_flags &
1533 		    (SF_NOUNLINK | SF_IMMUTABLE | SF_APPEND)) {
1534 			error = securelevel_gt(cred, 0);
1535 			if (error)
1536 				return (error);
1537 		}
1538 	} else {
1539 		if (node->tn_flags &
1540 		    (SF_NOUNLINK | SF_IMMUTABLE | SF_APPEND) ||
1541 		    ((flags ^ node->tn_flags) & SF_SETTABLE))
1542 			return (EPERM);
1543 	}
1544 	node->tn_flags = flags;
1545 	node->tn_status |= TMPFS_NODE_CHANGED;
1546 
1547 	ASSERT_VOP_ELOCKED(vp, "chflags2");
1548 
1549 	return (0);
1550 }
1551 
1552 /*
1553  * Change access mode on the given vnode.
1554  * Caller should execute tmpfs_update on vp after a successful execution.
1555  * The vnode must be locked on entry and remain locked on exit.
1556  */
1557 int
1558 tmpfs_chmod(struct vnode *vp, mode_t mode, struct ucred *cred, struct thread *p)
1559 {
1560 	int error;
1561 	struct tmpfs_node *node;
1562 
1563 	ASSERT_VOP_ELOCKED(vp, "chmod");
1564 
1565 	node = VP_TO_TMPFS_NODE(vp);
1566 
1567 	/* Disallow this operation if the file system is mounted read-only. */
1568 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
1569 		return EROFS;
1570 
1571 	/* Immutable or append-only files cannot be modified, either. */
1572 	if (node->tn_flags & (IMMUTABLE | APPEND))
1573 		return EPERM;
1574 
1575 	/*
1576 	 * To modify the permissions on a file, must possess VADMIN
1577 	 * for that file.
1578 	 */
1579 	if ((error = VOP_ACCESS(vp, VADMIN, cred, p)))
1580 		return (error);
1581 
1582 	/*
1583 	 * Privileged processes may set the sticky bit on non-directories,
1584 	 * as well as set the setgid bit on a file with a group that the
1585 	 * process is not a member of.
1586 	 */
1587 	if (vp->v_type != VDIR && (mode & S_ISTXT)) {
1588 		if (priv_check_cred(cred, PRIV_VFS_STICKYFILE))
1589 			return (EFTYPE);
1590 	}
1591 	if (!groupmember(node->tn_gid, cred) && (mode & S_ISGID)) {
1592 		error = priv_check_cred(cred, PRIV_VFS_SETGID);
1593 		if (error)
1594 			return (error);
1595 	}
1596 
1597 
1598 	node->tn_mode &= ~ALLPERMS;
1599 	node->tn_mode |= mode & ALLPERMS;
1600 
1601 	node->tn_status |= TMPFS_NODE_CHANGED;
1602 
1603 	ASSERT_VOP_ELOCKED(vp, "chmod2");
1604 
1605 	return (0);
1606 }
1607 
1608 /*
1609  * Change ownership of the given vnode.  At least one of uid or gid must
1610  * be different than VNOVAL.  If one is set to that value, the attribute
1611  * is unchanged.
1612  * Caller should execute tmpfs_update on vp after a successful execution.
1613  * The vnode must be locked on entry and remain locked on exit.
1614  */
1615 int
1616 tmpfs_chown(struct vnode *vp, uid_t uid, gid_t gid, struct ucred *cred,
1617     struct thread *p)
1618 {
1619 	int error;
1620 	struct tmpfs_node *node;
1621 	uid_t ouid;
1622 	gid_t ogid;
1623 
1624 	ASSERT_VOP_ELOCKED(vp, "chown");
1625 
1626 	node = VP_TO_TMPFS_NODE(vp);
1627 
1628 	/* Assign default values if they are unknown. */
1629 	MPASS(uid != VNOVAL || gid != VNOVAL);
1630 	if (uid == VNOVAL)
1631 		uid = node->tn_uid;
1632 	if (gid == VNOVAL)
1633 		gid = node->tn_gid;
1634 	MPASS(uid != VNOVAL && gid != VNOVAL);
1635 
1636 	/* Disallow this operation if the file system is mounted read-only. */
1637 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
1638 		return EROFS;
1639 
1640 	/* Immutable or append-only files cannot be modified, either. */
1641 	if (node->tn_flags & (IMMUTABLE | APPEND))
1642 		return EPERM;
1643 
1644 	/*
1645 	 * To modify the ownership of a file, must possess VADMIN for that
1646 	 * file.
1647 	 */
1648 	if ((error = VOP_ACCESS(vp, VADMIN, cred, p)))
1649 		return (error);
1650 
1651 	/*
1652 	 * To change the owner of a file, or change the group of a file to a
1653 	 * group of which we are not a member, the caller must have
1654 	 * privilege.
1655 	 */
1656 	if ((uid != node->tn_uid ||
1657 	    (gid != node->tn_gid && !groupmember(gid, cred))) &&
1658 	    (error = priv_check_cred(cred, PRIV_VFS_CHOWN)))
1659 		return (error);
1660 
1661 	ogid = node->tn_gid;
1662 	ouid = node->tn_uid;
1663 
1664 	node->tn_uid = uid;
1665 	node->tn_gid = gid;
1666 
1667 	node->tn_status |= TMPFS_NODE_CHANGED;
1668 
1669 	if ((node->tn_mode & (S_ISUID | S_ISGID)) && (ouid != uid || ogid != gid)) {
1670 		if (priv_check_cred(cred, PRIV_VFS_RETAINSUGID))
1671 			node->tn_mode &= ~(S_ISUID | S_ISGID);
1672 	}
1673 
1674 	ASSERT_VOP_ELOCKED(vp, "chown2");
1675 
1676 	return (0);
1677 }
1678 
1679 /*
1680  * Change size of the given vnode.
1681  * Caller should execute tmpfs_update on vp after a successful execution.
1682  * The vnode must be locked on entry and remain locked on exit.
1683  */
1684 int
1685 tmpfs_chsize(struct vnode *vp, u_quad_t size, struct ucred *cred,
1686     struct thread *p)
1687 {
1688 	int error;
1689 	struct tmpfs_node *node;
1690 
1691 	ASSERT_VOP_ELOCKED(vp, "chsize");
1692 
1693 	node = VP_TO_TMPFS_NODE(vp);
1694 
1695 	/* Decide whether this is a valid operation based on the file type. */
1696 	error = 0;
1697 	switch (vp->v_type) {
1698 	case VDIR:
1699 		return EISDIR;
1700 
1701 	case VREG:
1702 		if (vp->v_mount->mnt_flag & MNT_RDONLY)
1703 			return EROFS;
1704 		break;
1705 
1706 	case VBLK:
1707 		/* FALLTHROUGH */
1708 	case VCHR:
1709 		/* FALLTHROUGH */
1710 	case VFIFO:
1711 		/* Allow modifications of special files even if in the file
1712 		 * system is mounted read-only (we are not modifying the
1713 		 * files themselves, but the objects they represent). */
1714 		return 0;
1715 
1716 	default:
1717 		/* Anything else is unsupported. */
1718 		return EOPNOTSUPP;
1719 	}
1720 
1721 	/* Immutable or append-only files cannot be modified, either. */
1722 	if (node->tn_flags & (IMMUTABLE | APPEND))
1723 		return EPERM;
1724 
1725 	error = tmpfs_truncate(vp, size);
1726 	/* tmpfs_truncate will raise the NOTE_EXTEND and NOTE_ATTRIB kevents
1727 	 * for us, as will update tn_status; no need to do that here. */
1728 
1729 	ASSERT_VOP_ELOCKED(vp, "chsize2");
1730 
1731 	return (error);
1732 }
1733 
1734 /*
1735  * Change access and modification times of the given vnode.
1736  * Caller should execute tmpfs_update on vp after a successful execution.
1737  * The vnode must be locked on entry and remain locked on exit.
1738  */
1739 int
1740 tmpfs_chtimes(struct vnode *vp, struct vattr *vap,
1741     struct ucred *cred, struct thread *l)
1742 {
1743 	int error;
1744 	struct tmpfs_node *node;
1745 
1746 	ASSERT_VOP_ELOCKED(vp, "chtimes");
1747 
1748 	node = VP_TO_TMPFS_NODE(vp);
1749 
1750 	/* Disallow this operation if the file system is mounted read-only. */
1751 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
1752 		return EROFS;
1753 
1754 	/* Immutable or append-only files cannot be modified, either. */
1755 	if (node->tn_flags & (IMMUTABLE | APPEND))
1756 		return EPERM;
1757 
1758 	error = vn_utimes_perm(vp, vap, cred, l);
1759 	if (error != 0)
1760 		return (error);
1761 
1762 	if (vap->va_atime.tv_sec != VNOVAL)
1763 		node->tn_status |= TMPFS_NODE_ACCESSED;
1764 
1765 	if (vap->va_mtime.tv_sec != VNOVAL)
1766 		node->tn_status |= TMPFS_NODE_MODIFIED;
1767 
1768 	if (vap->va_birthtime.tv_sec != VNOVAL)
1769 		node->tn_status |= TMPFS_NODE_MODIFIED;
1770 
1771 	tmpfs_itimes(vp, &vap->va_atime, &vap->va_mtime);
1772 
1773 	if (vap->va_birthtime.tv_sec != VNOVAL)
1774 		node->tn_birthtime = vap->va_birthtime;
1775 	ASSERT_VOP_ELOCKED(vp, "chtimes2");
1776 
1777 	return (0);
1778 }
1779 
1780 void
1781 tmpfs_set_status(struct tmpfs_mount *tm, struct tmpfs_node *node, int status)
1782 {
1783 
1784 	if ((node->tn_status & status) == status || tm->tm_ronly)
1785 		return;
1786 	TMPFS_NODE_LOCK(node);
1787 	node->tn_status |= status;
1788 	TMPFS_NODE_UNLOCK(node);
1789 }
1790 
1791 /* Sync timestamps */
1792 void
1793 tmpfs_itimes(struct vnode *vp, const struct timespec *acc,
1794     const struct timespec *mod)
1795 {
1796 	struct tmpfs_node *node;
1797 	struct timespec now;
1798 
1799 	ASSERT_VOP_LOCKED(vp, "tmpfs_itimes");
1800 	node = VP_TO_TMPFS_NODE(vp);
1801 
1802 	if ((node->tn_status & (TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED |
1803 	    TMPFS_NODE_CHANGED)) == 0)
1804 		return;
1805 
1806 	vfs_timestamp(&now);
1807 	TMPFS_NODE_LOCK(node);
1808 	if (node->tn_status & TMPFS_NODE_ACCESSED) {
1809 		if (acc == NULL)
1810 			 acc = &now;
1811 		node->tn_atime = *acc;
1812 	}
1813 	if (node->tn_status & TMPFS_NODE_MODIFIED) {
1814 		if (mod == NULL)
1815 			mod = &now;
1816 		node->tn_mtime = *mod;
1817 	}
1818 	if (node->tn_status & TMPFS_NODE_CHANGED)
1819 		node->tn_ctime = now;
1820 	node->tn_status &= ~(TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED |
1821 	    TMPFS_NODE_CHANGED);
1822 	TMPFS_NODE_UNLOCK(node);
1823 
1824 	/* XXX: FIX? The entropy here is desirable, but the harvesting may be expensive */
1825 	random_harvest_queue(node, sizeof(*node), RANDOM_FS_ATIME);
1826 }
1827 
1828 void
1829 tmpfs_update(struct vnode *vp)
1830 {
1831 
1832 	tmpfs_itimes(vp, NULL, NULL);
1833 }
1834 
1835 int
1836 tmpfs_truncate(struct vnode *vp, off_t length)
1837 {
1838 	int error;
1839 	struct tmpfs_node *node;
1840 
1841 	node = VP_TO_TMPFS_NODE(vp);
1842 
1843 	if (length < 0) {
1844 		error = EINVAL;
1845 		goto out;
1846 	}
1847 
1848 	if (node->tn_size == length) {
1849 		error = 0;
1850 		goto out;
1851 	}
1852 
1853 	if (length > VFS_TO_TMPFS(vp->v_mount)->tm_maxfilesize)
1854 		return (EFBIG);
1855 
1856 	error = tmpfs_reg_resize(vp, length, FALSE);
1857 	if (error == 0)
1858 		node->tn_status |= TMPFS_NODE_CHANGED | TMPFS_NODE_MODIFIED;
1859 
1860 out:
1861 	tmpfs_update(vp);
1862 
1863 	return (error);
1864 }
1865 
1866 static __inline int
1867 tmpfs_dirtree_cmp(struct tmpfs_dirent *a, struct tmpfs_dirent *b)
1868 {
1869 	if (a->td_hash > b->td_hash)
1870 		return (1);
1871 	else if (a->td_hash < b->td_hash)
1872 		return (-1);
1873 	return (0);
1874 }
1875 
1876 RB_GENERATE_STATIC(tmpfs_dir, tmpfs_dirent, uh.td_entries, tmpfs_dirtree_cmp);
1877