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