xref: /freebsd-13-stable/sys/fs/tmpfs/tmpfs_subr.c (revision e45c358ec6639209c748f6ad0dd2f89ebedfa91e)
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
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 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/dirent.h>
42 #include <sys/fnv_hash.h>
43 #include <sys/lock.h>
44 #include <sys/limits.h>
45 #include <sys/mount.h>
46 #include <sys/namei.h>
47 #include <sys/priv.h>
48 #include <sys/proc.h>
49 #include <sys/random.h>
50 #include <sys/refcount.h>
51 #include <sys/rwlock.h>
52 #include <sys/smr.h>
53 #include <sys/stat.h>
54 #include <sys/sysctl.h>
55 #include <sys/user.h>
56 #include <sys/vnode.h>
57 #include <sys/vmmeter.h>
58 
59 #include <vm/vm.h>
60 #include <vm/vm_param.h>
61 #include <vm/vm_object.h>
62 #include <vm/vm_page.h>
63 #include <vm/vm_pageout.h>
64 #include <vm/vm_pager.h>
65 #include <vm/vm_extern.h>
66 #include <vm/swap_pager.h>
67 
68 #include <fs/tmpfs/tmpfs.h>
69 #include <fs/tmpfs/tmpfs_fifoops.h>
70 #include <fs/tmpfs/tmpfs_vnops.h>
71 
72 SYSCTL_NODE(_vfs, OID_AUTO, tmpfs, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
73     "tmpfs file system");
74 
75 static long tmpfs_pages_reserved = TMPFS_PAGES_MINRESERVED;
76 
77 MALLOC_DEFINE(M_TMPFSDIR, "tmpfs dir", "tmpfs dirent structure");
78 static uma_zone_t tmpfs_node_pool;
79 VFS_SMR_DECLARE;
80 
81 int tmpfs_pager_type = -1;
82 
83 static vm_object_t
tmpfs_pager_alloc(void * handle,vm_ooffset_t size,vm_prot_t prot,vm_ooffset_t offset,struct ucred * cred)84 tmpfs_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot,
85     vm_ooffset_t offset, struct ucred *cred)
86 {
87 	vm_object_t object;
88 
89 	MPASS(handle == NULL);
90 	MPASS(offset == 0);
91 	object = vm_object_allocate_dyn(tmpfs_pager_type, size,
92 	    OBJ_COLORED | OBJ_SWAP);
93 	if (!swap_pager_init_object(object, NULL, NULL, size, 0)) {
94 		vm_object_deallocate(object);
95 		object = NULL;
96 	}
97 	return (object);
98 }
99 
100 /*
101  * Make sure tmpfs vnodes with writable mappings can be found on the lazy list.
102  *
103  * This allows for periodic mtime updates while only scanning vnodes which are
104  * plausibly dirty, see tmpfs_update_mtime_lazy.
105  */
106 static void
tmpfs_pager_writecount_recalc(vm_object_t object,vm_offset_t old,vm_offset_t new)107 tmpfs_pager_writecount_recalc(vm_object_t object, vm_offset_t old,
108     vm_offset_t new)
109 {
110 	struct vnode *vp;
111 
112 	VM_OBJECT_ASSERT_WLOCKED(object);
113 
114 	vp = VM_TO_TMPFS_VP(object);
115 
116 	/*
117 	 * Forced unmount?
118 	 */
119 	if (vp == NULL || vp->v_object == NULL) {
120 		KASSERT((object->flags & OBJ_TMPFS_VREF) == 0,
121 		    ("object %p with OBJ_TMPFS_VREF but without vnode",
122 		    object));
123 		VM_OBJECT_WUNLOCK(object);
124 		return;
125 	}
126 
127 	if (old == 0) {
128 		VNASSERT((object->flags & OBJ_TMPFS_VREF) == 0, vp,
129 		    ("object without writable mappings has a reference"));
130 		VNPASS(vp->v_usecount > 0, vp);
131 	} else {
132 		VNASSERT((object->flags & OBJ_TMPFS_VREF) != 0, vp,
133 		    ("object with writable mappings does not "
134 		    "have a reference"));
135 	}
136 
137 	if (old == new) {
138 		VM_OBJECT_WUNLOCK(object);
139 		return;
140 	}
141 
142 	if (new == 0) {
143 		vm_object_clear_flag(object, OBJ_TMPFS_VREF);
144 		VM_OBJECT_WUNLOCK(object);
145 		vrele(vp);
146 	} else {
147 		if ((object->flags & OBJ_TMPFS_VREF) == 0) {
148 			vref(vp);
149 			vlazy(vp);
150 			vm_object_set_flag(object, OBJ_TMPFS_VREF);
151 		}
152 		VM_OBJECT_WUNLOCK(object);
153 	}
154 }
155 
156 static void
tmpfs_pager_update_writecount(vm_object_t object,vm_offset_t start,vm_offset_t end)157 tmpfs_pager_update_writecount(vm_object_t object, vm_offset_t start,
158     vm_offset_t end)
159 {
160 	vm_offset_t new, old;
161 
162 	VM_OBJECT_WLOCK(object);
163 	KASSERT((object->flags & OBJ_ANON) == 0,
164 	    ("%s: object %p with OBJ_ANON", __func__, object));
165 	old = object->un_pager.swp.writemappings;
166 	object->un_pager.swp.writemappings += (vm_ooffset_t)end - start;
167 	new = object->un_pager.swp.writemappings;
168 	tmpfs_pager_writecount_recalc(object, old, new);
169 	VM_OBJECT_ASSERT_UNLOCKED(object);
170 }
171 
172 static void
tmpfs_pager_release_writecount(vm_object_t object,vm_offset_t start,vm_offset_t end)173 tmpfs_pager_release_writecount(vm_object_t object, vm_offset_t start,
174     vm_offset_t end)
175 {
176 	vm_offset_t new, old;
177 
178 	VM_OBJECT_WLOCK(object);
179 	KASSERT((object->flags & OBJ_ANON) == 0,
180 	    ("%s: object %p with OBJ_ANON", __func__, object));
181 	old = object->un_pager.swp.writemappings;
182 	KASSERT(old >= (vm_ooffset_t)end - start,
183 	    ("tmpfs obj %p writecount %jx dec %jx", object, (uintmax_t)old,
184 	    (uintmax_t)((vm_ooffset_t)end - start)));
185 	object->un_pager.swp.writemappings -= (vm_ooffset_t)end - start;
186 	new = object->un_pager.swp.writemappings;
187 	tmpfs_pager_writecount_recalc(object, old, new);
188 	VM_OBJECT_ASSERT_UNLOCKED(object);
189 }
190 
191 static void
tmpfs_pager_getvp(vm_object_t object,struct vnode ** vpp,bool * vp_heldp)192 tmpfs_pager_getvp(vm_object_t object, struct vnode **vpp, bool *vp_heldp)
193 {
194 	struct vnode *vp;
195 
196 	/*
197 	 * Tmpfs VREG node, which was reclaimed, has tmpfs_pager_type
198 	 * type.  In this case there is no v_writecount to adjust.
199 	 */
200 	if (vp_heldp != NULL)
201 		VM_OBJECT_RLOCK(object);
202 	else
203 		VM_OBJECT_ASSERT_LOCKED(object);
204 	if ((object->flags & OBJ_TMPFS) != 0) {
205 		vp = VM_TO_TMPFS_VP(object);
206 		if (vp != NULL) {
207 			*vpp = vp;
208 			if (vp_heldp != NULL) {
209 				vhold(vp);
210 				*vp_heldp = true;
211 			}
212 		}
213 	}
214 	if (vp_heldp != NULL)
215 		VM_OBJECT_RUNLOCK(object);
216 }
217 
218 static void
tmpfs_pager_freespace(vm_object_t obj,vm_pindex_t start,vm_size_t size)219 tmpfs_pager_freespace(vm_object_t obj, vm_pindex_t start, vm_size_t size)
220 {
221 	struct tmpfs_node *node;
222 	struct tmpfs_mount *tm;
223 	vm_size_t c;
224 
225 	swap_pager_freespace(obj, start, size, &c);
226 	if ((obj->flags & OBJ_TMPFS) == 0 || c == 0)
227 		return;
228 
229 	node = obj->un_pager.swp.swp_priv;
230 	MPASS(node->tn_type == VREG);
231 	tm = node->tn_reg.tn_tmp;
232 
233 	KASSERT(tm->tm_pages_used >= c,
234 	    ("tmpfs tm %p pages %jd free %jd", tm,
235 	    (uintmax_t)tm->tm_pages_used, (uintmax_t)c));
236 	atomic_add_long(&tm->tm_pages_used, -c);
237 	KASSERT(node->tn_reg.tn_pages >= c,
238 	    ("tmpfs node %p pages %jd free %jd", node,
239 	    (uintmax_t)node->tn_reg.tn_pages, (uintmax_t)c));
240 	node->tn_reg.tn_pages -= c;
241 }
242 
243 static void
tmpfs_page_inserted(vm_object_t obj,vm_page_t m)244 tmpfs_page_inserted(vm_object_t obj, vm_page_t m)
245 {
246 	struct tmpfs_node *node;
247 	struct tmpfs_mount *tm;
248 
249 	if ((obj->flags & OBJ_TMPFS) == 0)
250 		return;
251 
252 	node = obj->un_pager.swp.swp_priv;
253 	MPASS(node->tn_type == VREG);
254 	tm = node->tn_reg.tn_tmp;
255 
256 	if (!vm_pager_has_page(obj, m->pindex, NULL, NULL)) {
257 		atomic_add_long(&tm->tm_pages_used, 1);
258 		node->tn_reg.tn_pages += 1;
259 	}
260 }
261 
262 static void
tmpfs_page_removed(vm_object_t obj,vm_page_t m)263 tmpfs_page_removed(vm_object_t obj, vm_page_t m)
264 {
265 	struct tmpfs_node *node;
266 	struct tmpfs_mount *tm;
267 
268 	if ((obj->flags & OBJ_TMPFS) == 0)
269 		return;
270 
271 	node = obj->un_pager.swp.swp_priv;
272 	MPASS(node->tn_type == VREG);
273 	tm = node->tn_reg.tn_tmp;
274 
275 	if (!vm_pager_has_page(obj, m->pindex, NULL, NULL)) {
276 		KASSERT(tm->tm_pages_used >= 1,
277 		    ("tmpfs tm %p pages %jd free 1", tm,
278 		    (uintmax_t)tm->tm_pages_used));
279 		atomic_add_long(&tm->tm_pages_used, -1);
280 		KASSERT(node->tn_reg.tn_pages >= 1,
281 		    ("tmpfs node %p pages %jd free 1", node,
282 		    (uintmax_t)node->tn_reg.tn_pages));
283 		node->tn_reg.tn_pages -= 1;
284 	}
285 }
286 
287 static boolean_t
tmpfs_can_alloc_page(vm_object_t obj,vm_pindex_t pindex)288 tmpfs_can_alloc_page(vm_object_t obj, vm_pindex_t pindex)
289 {
290 	struct tmpfs_mount *tm;
291 
292 	tm = VM_TO_TMPFS_MP(obj);
293 	if (tm == NULL || vm_pager_has_page(obj, pindex, NULL, NULL) ||
294 	    tm->tm_pages_max == 0)
295 		return (true);
296 	return (tm->tm_pages_max > atomic_load_long(&tm->tm_pages_used));
297 }
298 
299 struct pagerops tmpfs_pager_ops = {
300 	.pgo_kvme_type = KVME_TYPE_VNODE,
301 	.pgo_alloc = tmpfs_pager_alloc,
302 	.pgo_set_writeable_dirty = vm_object_set_writeable_dirty_,
303 	.pgo_update_writecount = tmpfs_pager_update_writecount,
304 	.pgo_release_writecount = tmpfs_pager_release_writecount,
305 	.pgo_mightbedirty = vm_object_mightbedirty_,
306 	.pgo_getvp = tmpfs_pager_getvp,
307 	.pgo_freespace = tmpfs_pager_freespace,
308 	.pgo_page_inserted = tmpfs_page_inserted,
309 	.pgo_page_removed = tmpfs_page_removed,
310 	.pgo_can_alloc_page = tmpfs_can_alloc_page,
311 };
312 
313 static int
tmpfs_node_ctor(void * mem,int size,void * arg,int flags)314 tmpfs_node_ctor(void *mem, int size, void *arg, int flags)
315 {
316 	struct tmpfs_node *node;
317 
318 	node = mem;
319 	node->tn_gen++;
320 	node->tn_size = 0;
321 	node->tn_status = 0;
322 	node->tn_accessed = false;
323 	node->tn_flags = 0;
324 	node->tn_links = 0;
325 	node->tn_vnode = NULL;
326 	node->tn_vpstate = 0;
327 	return (0);
328 }
329 
330 static void
tmpfs_node_dtor(void * mem,int size,void * arg)331 tmpfs_node_dtor(void *mem, int size, void *arg)
332 {
333 	struct tmpfs_node *node;
334 
335 	node = mem;
336 	node->tn_type = VNON;
337 }
338 
339 static int
tmpfs_node_init(void * mem,int size,int flags)340 tmpfs_node_init(void *mem, int size, int flags)
341 {
342 	struct tmpfs_node *node;
343 
344 	node = mem;
345 	node->tn_id = 0;
346 	mtx_init(&node->tn_interlock, "tmpfsni", NULL, MTX_DEF);
347 	node->tn_gen = arc4random();
348 	return (0);
349 }
350 
351 static void
tmpfs_node_fini(void * mem,int size)352 tmpfs_node_fini(void *mem, int size)
353 {
354 	struct tmpfs_node *node;
355 
356 	node = mem;
357 	mtx_destroy(&node->tn_interlock);
358 }
359 
360 int
tmpfs_subr_init(void)361 tmpfs_subr_init(void)
362 {
363 	tmpfs_pager_type = vm_pager_alloc_dyn_type(&tmpfs_pager_ops,
364 	    OBJT_SWAP);
365 	if (tmpfs_pager_type == -1)
366 		return (EINVAL);
367 	tmpfs_node_pool = uma_zcreate("TMPFS node",
368 	    sizeof(struct tmpfs_node), tmpfs_node_ctor, tmpfs_node_dtor,
369 	    tmpfs_node_init, tmpfs_node_fini, UMA_ALIGN_PTR, 0);
370 	VFS_SMR_ZONE_SET(tmpfs_node_pool);
371 	return (0);
372 }
373 
374 void
tmpfs_subr_uninit(void)375 tmpfs_subr_uninit(void)
376 {
377 	if (tmpfs_pager_type != -1)
378 		vm_pager_free_dyn_type(tmpfs_pager_type);
379 	tmpfs_pager_type = -1;
380 	uma_zdestroy(tmpfs_node_pool);
381 }
382 
383 static int
sysctl_mem_reserved(SYSCTL_HANDLER_ARGS)384 sysctl_mem_reserved(SYSCTL_HANDLER_ARGS)
385 {
386 	int error;
387 	long pages, bytes;
388 
389 	pages = *(long *)arg1;
390 	bytes = pages * PAGE_SIZE;
391 
392 	error = sysctl_handle_long(oidp, &bytes, 0, req);
393 	if (error || !req->newptr)
394 		return (error);
395 
396 	pages = bytes / PAGE_SIZE;
397 	if (pages < TMPFS_PAGES_MINRESERVED)
398 		return (EINVAL);
399 
400 	*(long *)arg1 = pages;
401 	return (0);
402 }
403 
404 SYSCTL_PROC(_vfs_tmpfs, OID_AUTO, memory_reserved,
405     CTLTYPE_LONG|CTLFLAG_MPSAFE|CTLFLAG_RW, &tmpfs_pages_reserved, 0,
406     sysctl_mem_reserved, "L",
407     "Amount of available memory and swap below which tmpfs growth stops");
408 
409 static __inline int tmpfs_dirtree_cmp(struct tmpfs_dirent *a,
410     struct tmpfs_dirent *b);
411 RB_PROTOTYPE_STATIC(tmpfs_dir, tmpfs_dirent, uh.td_entries, tmpfs_dirtree_cmp);
412 
413 size_t
tmpfs_mem_avail(void)414 tmpfs_mem_avail(void)
415 {
416 	size_t avail;
417 	long reserved;
418 
419 	avail = swap_pager_avail + vm_free_count();
420 	reserved = atomic_load_long(&tmpfs_pages_reserved);
421 	if (__predict_false(avail < reserved))
422 		return (0);
423 	return (avail - reserved);
424 }
425 
426 size_t
tmpfs_pages_used(struct tmpfs_mount * tmp)427 tmpfs_pages_used(struct tmpfs_mount *tmp)
428 {
429 	const size_t node_size = sizeof(struct tmpfs_node) +
430 	    sizeof(struct tmpfs_dirent);
431 	size_t meta_pages;
432 
433 	meta_pages = howmany((uintmax_t)tmp->tm_nodes_inuse * node_size,
434 	    PAGE_SIZE);
435 	return (meta_pages + tmp->tm_pages_used);
436 }
437 
438 static bool
tmpfs_pages_check_avail(struct tmpfs_mount * tmp,size_t req_pages)439 tmpfs_pages_check_avail(struct tmpfs_mount *tmp, size_t req_pages)
440 {
441 	if (tmpfs_mem_avail() < req_pages)
442 		return (false);
443 
444 	if (tmp->tm_pages_max != ULONG_MAX &&
445 	    tmp->tm_pages_max < req_pages + tmpfs_pages_used(tmp))
446 		return (false);
447 
448 	return (true);
449 }
450 
451 static int
tmpfs_partial_page_invalidate(vm_object_t object,vm_pindex_t idx,int base,int end,boolean_t ignerr)452 tmpfs_partial_page_invalidate(vm_object_t object, vm_pindex_t idx, int base,
453     int end, boolean_t ignerr)
454 {
455 	vm_page_t m;
456 	int rv, error;
457 
458 	VM_OBJECT_ASSERT_WLOCKED(object);
459 	KASSERT(base >= 0, ("%s: base %d", __func__, base));
460 	KASSERT(end - base <= PAGE_SIZE, ("%s: base %d end %d", __func__, base,
461 	    end));
462 	error = 0;
463 
464 retry:
465 	m = vm_page_grab(object, idx, VM_ALLOC_NOCREAT);
466 	if (m != NULL) {
467 		MPASS(vm_page_all_valid(m));
468 	} else if (vm_pager_has_page(object, idx, NULL, NULL)) {
469 		m = vm_page_alloc(object, idx, VM_ALLOC_NORMAL |
470 		    VM_ALLOC_WAITFAIL);
471 		if (m == NULL)
472 			goto retry;
473 		vm_object_pip_add(object, 1);
474 		VM_OBJECT_WUNLOCK(object);
475 		rv = vm_pager_get_pages(object, &m, 1, NULL, NULL);
476 		VM_OBJECT_WLOCK(object);
477 		vm_object_pip_wakeup(object);
478 		if (rv == VM_PAGER_OK) {
479 			/*
480 			 * Since the page was not resident, and therefore not
481 			 * recently accessed, immediately enqueue it for
482 			 * asynchronous laundering.  The current operation is
483 			 * not regarded as an access.
484 			 */
485 			vm_page_launder(m);
486 		} else {
487 			vm_page_free(m);
488 			m = NULL;
489 			if (!ignerr)
490 				error = EIO;
491 		}
492 	}
493 	if (m != NULL) {
494 		pmap_zero_page_area(m, base, end - base);
495 		vm_page_set_dirty(m);
496 		vm_page_xunbusy(m);
497 	}
498 
499 	return (error);
500 }
501 
502 void
tmpfs_ref_node(struct tmpfs_node * node)503 tmpfs_ref_node(struct tmpfs_node *node)
504 {
505 #ifdef INVARIANTS
506 	u_int old;
507 
508 	old =
509 #endif
510 	refcount_acquire(&node->tn_refcount);
511 #ifdef INVARIANTS
512 	KASSERT(old > 0, ("node %p zero refcount", node));
513 #endif
514 }
515 
516 /*
517  * Allocates a new node of type 'type' inside the 'tmp' mount point, with
518  * its owner set to 'uid', its group to 'gid' and its mode set to 'mode',
519  * using the credentials of the process 'p'.
520  *
521  * If the node type is set to 'VDIR', then the parent parameter must point
522  * to the parent directory of the node being created.  It may only be NULL
523  * while allocating the root node.
524  *
525  * If the node type is set to 'VBLK' or 'VCHR', then the rdev parameter
526  * specifies the device the node represents.
527  *
528  * If the node type is set to 'VLNK', then the parameter target specifies
529  * the file name of the target file for the symbolic link that is being
530  * created.
531  *
532  * Note that new nodes are retrieved from the available list if it has
533  * items or, if it is empty, from the node pool as long as there is enough
534  * space to create them.
535  *
536  * Returns zero on success or an appropriate error code on failure.
537  */
538 int
tmpfs_alloc_node(struct mount * mp,struct tmpfs_mount * tmp,enum vtype type,uid_t uid,gid_t gid,mode_t mode,struct tmpfs_node * parent,const char * target,dev_t rdev,struct tmpfs_node ** node)539 tmpfs_alloc_node(struct mount *mp, struct tmpfs_mount *tmp, enum vtype type,
540     uid_t uid, gid_t gid, mode_t mode, struct tmpfs_node *parent,
541     const char *target, dev_t rdev, struct tmpfs_node **node)
542 {
543 	struct tmpfs_node *nnode;
544 	char *symlink;
545 	char symlink_smr;
546 
547 	/* If the root directory of the 'tmp' file system is not yet
548 	 * allocated, this must be the request to do it. */
549 	MPASS(IMPLIES(tmp->tm_root == NULL, parent == NULL && type == VDIR));
550 
551 	MPASS((type == VLNK) ^ (target == NULL));
552 	MPASS((type == VBLK || type == VCHR) ^ (rdev == VNOVAL));
553 
554 	if (tmp->tm_nodes_inuse >= tmp->tm_nodes_max)
555 		return (ENOSPC);
556 	if (!tmpfs_pages_check_avail(tmp, 1))
557 		return (ENOSPC);
558 
559 	if ((mp->mnt_kern_flag & MNTK_UNMOUNT) != 0) {
560 		/*
561 		 * When a new tmpfs node is created for fully
562 		 * constructed mount point, there must be a parent
563 		 * node, which vnode is locked exclusively.  As
564 		 * consequence, if the unmount is executing in
565 		 * parallel, vflush() cannot reclaim the parent vnode.
566 		 * Due to this, the check for MNTK_UNMOUNT flag is not
567 		 * racy: if we did not see MNTK_UNMOUNT flag, then tmp
568 		 * cannot be destroyed until node construction is
569 		 * finished and the parent vnode unlocked.
570 		 *
571 		 * Tmpfs does not need to instantiate new nodes during
572 		 * unmount.
573 		 */
574 		return (EBUSY);
575 	}
576 	if ((mp->mnt_kern_flag & MNT_RDONLY) != 0)
577 		return (EROFS);
578 
579 	nnode = uma_zalloc_smr(tmpfs_node_pool, M_WAITOK);
580 
581 	/* Generic initialization. */
582 	nnode->tn_type = type;
583 	vfs_timestamp(&nnode->tn_atime);
584 	nnode->tn_birthtime = nnode->tn_ctime = nnode->tn_mtime =
585 	    nnode->tn_atime;
586 	nnode->tn_uid = uid;
587 	nnode->tn_gid = gid;
588 	nnode->tn_mode = mode;
589 	nnode->tn_id = alloc_unr64(&tmp->tm_ino_unr);
590 	nnode->tn_refcount = 1;
591 
592 	/* Type-specific initialization. */
593 	switch (nnode->tn_type) {
594 	case VBLK:
595 	case VCHR:
596 		nnode->tn_rdev = rdev;
597 		break;
598 
599 	case VDIR:
600 		RB_INIT(&nnode->tn_dir.tn_dirhead);
601 		LIST_INIT(&nnode->tn_dir.tn_dupindex);
602 		MPASS(parent != nnode);
603 		MPASS(IMPLIES(parent == NULL, tmp->tm_root == NULL));
604 		nnode->tn_dir.tn_parent = (parent == NULL) ? nnode : parent;
605 		nnode->tn_dir.tn_readdir_lastn = 0;
606 		nnode->tn_dir.tn_readdir_lastp = NULL;
607 		nnode->tn_links++;
608 		TMPFS_NODE_LOCK(nnode->tn_dir.tn_parent);
609 		nnode->tn_dir.tn_parent->tn_links++;
610 		TMPFS_NODE_UNLOCK(nnode->tn_dir.tn_parent);
611 		break;
612 
613 	case VFIFO:
614 		/* FALLTHROUGH */
615 	case VSOCK:
616 		break;
617 
618 	case VLNK:
619 		MPASS(strlen(target) < MAXPATHLEN);
620 		nnode->tn_size = strlen(target);
621 
622 		symlink = NULL;
623 		if (!tmp->tm_nonc) {
624 			symlink = cache_symlink_alloc(nnode->tn_size + 1,
625 			    M_WAITOK);
626 			symlink_smr = true;
627 		}
628 		if (symlink == NULL) {
629 			symlink = malloc(nnode->tn_size + 1, M_TMPFSNAME,
630 			    M_WAITOK);
631 			symlink_smr = false;
632 		}
633 		memcpy(symlink, target, nnode->tn_size + 1);
634 
635 		/*
636 		 * Allow safe symlink resolving for lockless lookup.
637 		 * tmpfs_fplookup_symlink references this comment.
638 		 *
639 		 * 1. nnode is not yet visible to the world
640 		 * 2. both tn_link_target and tn_link_smr get populated
641 		 * 3. release fence publishes their content
642 		 * 4. tn_link_target content is immutable until node
643 		 *    destruction, where the pointer gets set to NULL
644 		 * 5. tn_link_smr is never changed once set
645 		 *
646 		 * As a result it is sufficient to issue load consume
647 		 * on the node pointer to also get the above content
648 		 * in a stable manner.  Worst case tn_link_smr flag
649 		 * may be set to true despite being stale, while the
650 		 * target buffer is already cleared out.
651 		 */
652 		atomic_store_ptr(&nnode->tn_link_target, symlink);
653 		atomic_store_char((char *)&nnode->tn_link_smr, symlink_smr);
654 		atomic_thread_fence_rel();
655 		break;
656 
657 	case VREG:
658 		nnode->tn_reg.tn_aobj =
659 		    vm_pager_allocate(tmpfs_pager_type, NULL, 0,
660 		    VM_PROT_DEFAULT, 0,
661 		    NULL /* XXXKIB - tmpfs needs swap reservation */);
662 		nnode->tn_reg.tn_aobj->un_pager.swp.swp_priv = nnode;
663 		vm_object_set_flag(nnode->tn_reg.tn_aobj, OBJ_TMPFS);
664 		nnode->tn_reg.tn_tmp = tmp;
665 		nnode->tn_reg.tn_pages = 0;
666 		break;
667 
668 	default:
669 		panic("tmpfs_alloc_node: type %p %d", nnode,
670 		    (int)nnode->tn_type);
671 	}
672 
673 	TMPFS_LOCK(tmp);
674 	LIST_INSERT_HEAD(&tmp->tm_nodes_used, nnode, tn_entries);
675 	nnode->tn_attached = true;
676 	tmp->tm_nodes_inuse++;
677 	tmp->tm_refcount++;
678 	TMPFS_UNLOCK(tmp);
679 
680 	*node = nnode;
681 	return (0);
682 }
683 
684 /*
685  * Destroys the node pointed to by node from the file system 'tmp'.
686  * If the node references a directory, no entries are allowed.
687  */
688 void
tmpfs_free_node(struct tmpfs_mount * tmp,struct tmpfs_node * node)689 tmpfs_free_node(struct tmpfs_mount *tmp, struct tmpfs_node *node)
690 {
691 	if (refcount_release_if_not_last(&node->tn_refcount))
692 		return;
693 
694 	TMPFS_LOCK(tmp);
695 	TMPFS_NODE_LOCK(node);
696 	if (!tmpfs_free_node_locked(tmp, node, false)) {
697 		TMPFS_NODE_UNLOCK(node);
698 		TMPFS_UNLOCK(tmp);
699 	}
700 }
701 
702 bool
tmpfs_free_node_locked(struct tmpfs_mount * tmp,struct tmpfs_node * node,bool detach)703 tmpfs_free_node_locked(struct tmpfs_mount *tmp, struct tmpfs_node *node,
704     bool detach)
705 {
706 	vm_object_t uobj;
707 	char *symlink;
708 	bool last;
709 
710 	TMPFS_MP_ASSERT_LOCKED(tmp);
711 	TMPFS_NODE_ASSERT_LOCKED(node);
712 
713 	last = refcount_release(&node->tn_refcount);
714 	if (node->tn_attached && (detach || last)) {
715 		MPASS(tmp->tm_nodes_inuse > 0);
716 		tmp->tm_nodes_inuse--;
717 		LIST_REMOVE(node, tn_entries);
718 		node->tn_attached = false;
719 	}
720 	if (!last)
721 		return (false);
722 
723 	TMPFS_NODE_UNLOCK(node);
724 
725 #ifdef INVARIANTS
726 	MPASS(node->tn_vnode == NULL);
727 	MPASS((node->tn_vpstate & TMPFS_VNODE_ALLOCATING) == 0);
728 
729 	/*
730 	 * Make sure this is a node type we can deal with. Everything
731 	 * is explicitly enumerated without the 'default' clause so
732 	 * the compiler can throw an error in case a new type is
733 	 * added.
734 	 */
735 	switch (node->tn_type) {
736 	case VBLK:
737 	case VCHR:
738 	case VDIR:
739 	case VFIFO:
740 	case VSOCK:
741 	case VLNK:
742 	case VREG:
743 		break;
744 	case VNON:
745 	case VBAD:
746 	case VMARKER:
747 		panic("%s: bad type %d for node %p", __func__,
748 		    (int)node->tn_type, node);
749 	}
750 #endif
751 
752 	switch (node->tn_type) {
753 	case VREG:
754 		uobj = node->tn_reg.tn_aobj;
755 		node->tn_reg.tn_aobj = NULL;
756 		if (uobj != NULL) {
757 			VM_OBJECT_WLOCK(uobj);
758 			KASSERT((uobj->flags & OBJ_TMPFS) != 0,
759 			    ("tmpfs node %p uobj %p not tmpfs", node, uobj));
760 			vm_object_clear_flag(uobj, OBJ_TMPFS);
761 			KASSERT(tmp->tm_pages_used >= node->tn_reg.tn_pages,
762 			    ("tmpfs tmp %p node %p pages %jd free %jd", tmp,
763 			    node, (uintmax_t)tmp->tm_pages_used,
764 			    (uintmax_t)node->tn_reg.tn_pages));
765 			atomic_add_long(&tmp->tm_pages_used,
766 			    -node->tn_reg.tn_pages);
767 			VM_OBJECT_WUNLOCK(uobj);
768 		}
769 		tmpfs_free_tmp(tmp);
770 
771 		/*
772 		 * vm_object_deallocate() must not be called while
773 		 * owning tm_allnode_lock, because deallocate might
774 		 * sleep.  Call it after tmpfs_free_tmp() does the
775 		 * unlock.
776 		 */
777 		if (uobj != NULL)
778 			vm_object_deallocate(uobj);
779 
780 		break;
781 	case VLNK:
782 		tmpfs_free_tmp(tmp);
783 
784 		symlink = node->tn_link_target;
785 		atomic_store_ptr(&node->tn_link_target, NULL);
786 		if (atomic_load_char(&node->tn_link_smr)) {
787 			cache_symlink_free(symlink, node->tn_size + 1);
788 		} else {
789 			free(symlink, M_TMPFSNAME);
790 		}
791 		break;
792 	default:
793 		tmpfs_free_tmp(tmp);
794 		break;
795 	}
796 
797 	uma_zfree_smr(tmpfs_node_pool, node);
798 	return (true);
799 }
800 
801 static __inline uint32_t
tmpfs_dirent_hash(const char * name,u_int len)802 tmpfs_dirent_hash(const char *name, u_int len)
803 {
804 	uint32_t hash;
805 
806 	hash = fnv_32_buf(name, len, FNV1_32_INIT + len) & TMPFS_DIRCOOKIE_MASK;
807 #ifdef TMPFS_DEBUG_DIRCOOKIE_DUP
808 	hash &= 0xf;
809 #endif
810 	if (hash < TMPFS_DIRCOOKIE_MIN)
811 		hash += TMPFS_DIRCOOKIE_MIN;
812 
813 	return (hash);
814 }
815 
816 static __inline off_t
tmpfs_dirent_cookie(struct tmpfs_dirent * de)817 tmpfs_dirent_cookie(struct tmpfs_dirent *de)
818 {
819 	if (de == NULL)
820 		return (TMPFS_DIRCOOKIE_EOF);
821 
822 	MPASS(de->td_cookie >= TMPFS_DIRCOOKIE_MIN);
823 
824 	return (de->td_cookie);
825 }
826 
827 static __inline boolean_t
tmpfs_dirent_dup(struct tmpfs_dirent * de)828 tmpfs_dirent_dup(struct tmpfs_dirent *de)
829 {
830 	return ((de->td_cookie & TMPFS_DIRCOOKIE_DUP) != 0);
831 }
832 
833 static __inline boolean_t
tmpfs_dirent_duphead(struct tmpfs_dirent * de)834 tmpfs_dirent_duphead(struct tmpfs_dirent *de)
835 {
836 	return ((de->td_cookie & TMPFS_DIRCOOKIE_DUPHEAD) != 0);
837 }
838 
839 void
tmpfs_dirent_init(struct tmpfs_dirent * de,const char * name,u_int namelen)840 tmpfs_dirent_init(struct tmpfs_dirent *de, const char *name, u_int namelen)
841 {
842 	de->td_hash = de->td_cookie = tmpfs_dirent_hash(name, namelen);
843 	memcpy(de->ud.td_name, name, namelen);
844 	de->td_namelen = namelen;
845 }
846 
847 /*
848  * Allocates a new directory entry for the node node with a name of name.
849  * The new directory entry is returned in *de.
850  *
851  * The link count of node is increased by one to reflect the new object
852  * referencing it.
853  *
854  * Returns zero on success or an appropriate error code on failure.
855  */
856 int
tmpfs_alloc_dirent(struct tmpfs_mount * tmp,struct tmpfs_node * node,const char * name,u_int len,struct tmpfs_dirent ** de)857 tmpfs_alloc_dirent(struct tmpfs_mount *tmp, struct tmpfs_node *node,
858     const char *name, u_int len, struct tmpfs_dirent **de)
859 {
860 	struct tmpfs_dirent *nde;
861 
862 	nde = malloc(sizeof(*nde), M_TMPFSDIR, M_WAITOK);
863 	nde->td_node = node;
864 	if (name != NULL) {
865 		nde->ud.td_name = malloc(len, M_TMPFSNAME, M_WAITOK);
866 		tmpfs_dirent_init(nde, name, len);
867 	} else
868 		nde->td_namelen = 0;
869 	if (node != NULL)
870 		node->tn_links++;
871 
872 	*de = nde;
873 
874 	return (0);
875 }
876 
877 /*
878  * Frees a directory entry.  It is the caller's responsibility to destroy
879  * the node referenced by it if needed.
880  *
881  * The link count of node is decreased by one to reflect the removal of an
882  * object that referenced it.  This only happens if 'node_exists' is true;
883  * otherwise the function will not access the node referred to by the
884  * directory entry, as it may already have been released from the outside.
885  */
886 void
tmpfs_free_dirent(struct tmpfs_mount * tmp,struct tmpfs_dirent * de)887 tmpfs_free_dirent(struct tmpfs_mount *tmp, struct tmpfs_dirent *de)
888 {
889 	struct tmpfs_node *node;
890 
891 	node = de->td_node;
892 	if (node != NULL) {
893 		MPASS(node->tn_links > 0);
894 		node->tn_links--;
895 	}
896 	if (!tmpfs_dirent_duphead(de) && de->ud.td_name != NULL)
897 		free(de->ud.td_name, M_TMPFSNAME);
898 	free(de, M_TMPFSDIR);
899 }
900 
901 void
tmpfs_destroy_vobject(struct vnode * vp,vm_object_t obj)902 tmpfs_destroy_vobject(struct vnode *vp, vm_object_t obj)
903 {
904 	bool want_vrele;
905 
906 	ASSERT_VOP_ELOCKED(vp, "tmpfs_destroy_vobject");
907 	if (vp->v_type != VREG || obj == NULL)
908 		return;
909 
910 	VM_OBJECT_WLOCK(obj);
911 	VI_LOCK(vp);
912 	vp->v_object = NULL;
913 
914 	/*
915 	 * May be going through forced unmount.
916 	 */
917 	want_vrele = false;
918 	if ((obj->flags & OBJ_TMPFS_VREF) != 0) {
919 		vm_object_clear_flag(obj, OBJ_TMPFS_VREF);
920 		want_vrele = true;
921 	}
922 
923 	if (vp->v_writecount < 0)
924 		vp->v_writecount = 0;
925 	VI_UNLOCK(vp);
926 	VM_OBJECT_WUNLOCK(obj);
927 	if (want_vrele) {
928 		vrele(vp);
929 	}
930 }
931 
932 /*
933  * Need to clear v_object for insmntque failure.
934  */
935 static void
tmpfs_insmntque_dtr(struct vnode * vp,void * dtr_arg)936 tmpfs_insmntque_dtr(struct vnode *vp, void *dtr_arg)
937 {
938 
939 	tmpfs_destroy_vobject(vp, vp->v_object);
940 	vp->v_object = NULL;
941 	vp->v_data = NULL;
942 	vp->v_op = &dead_vnodeops;
943 	vgone(vp);
944 	vput(vp);
945 }
946 
947 /*
948  * Allocates a new vnode for the node node or returns a new reference to
949  * an existing one if the node had already a vnode referencing it.  The
950  * resulting locked vnode is returned in *vpp.
951  *
952  * Returns zero on success or an appropriate error code on failure.
953  */
954 int
tmpfs_alloc_vp(struct mount * mp,struct tmpfs_node * node,int lkflag,struct vnode ** vpp)955 tmpfs_alloc_vp(struct mount *mp, struct tmpfs_node *node, int lkflag,
956     struct vnode **vpp)
957 {
958 	struct vnode *vp;
959 	enum vgetstate vs;
960 	struct tmpfs_mount *tm;
961 	vm_object_t object;
962 	int error;
963 
964 	error = 0;
965 	tm = VFS_TO_TMPFS(mp);
966 	TMPFS_NODE_LOCK(node);
967 	tmpfs_ref_node(node);
968 loop:
969 	TMPFS_NODE_ASSERT_LOCKED(node);
970 	if ((vp = node->tn_vnode) != NULL) {
971 		MPASS((node->tn_vpstate & TMPFS_VNODE_DOOMED) == 0);
972 		if ((node->tn_type == VDIR && node->tn_dir.tn_parent == NULL) ||
973 		    (VN_IS_DOOMED(vp) &&
974 		     (lkflag & LK_NOWAIT) != 0)) {
975 			TMPFS_NODE_UNLOCK(node);
976 			error = ENOENT;
977 			vp = NULL;
978 			goto out;
979 		}
980 		if (VN_IS_DOOMED(vp)) {
981 			node->tn_vpstate |= TMPFS_VNODE_WRECLAIM;
982 			while ((node->tn_vpstate & TMPFS_VNODE_WRECLAIM) != 0) {
983 				msleep(&node->tn_vnode, TMPFS_NODE_MTX(node),
984 				    0, "tmpfsE", 0);
985 			}
986 			goto loop;
987 		}
988 		vs = vget_prep(vp);
989 		TMPFS_NODE_UNLOCK(node);
990 		error = vget_finish(vp, lkflag, vs);
991 		if (error == ENOENT) {
992 			TMPFS_NODE_LOCK(node);
993 			goto loop;
994 		}
995 		if (error != 0) {
996 			vp = NULL;
997 			goto out;
998 		}
999 
1000 		/*
1001 		 * Make sure the vnode is still there after
1002 		 * getting the interlock to avoid racing a free.
1003 		 */
1004 		if (node->tn_vnode != vp) {
1005 			vput(vp);
1006 			TMPFS_NODE_LOCK(node);
1007 			goto loop;
1008 		}
1009 
1010 		goto out;
1011 	}
1012 
1013 	if ((node->tn_vpstate & TMPFS_VNODE_DOOMED) ||
1014 	    (node->tn_type == VDIR && node->tn_dir.tn_parent == NULL)) {
1015 		TMPFS_NODE_UNLOCK(node);
1016 		error = ENOENT;
1017 		vp = NULL;
1018 		goto out;
1019 	}
1020 
1021 	/*
1022 	 * otherwise lock the vp list while we call getnewvnode
1023 	 * since that can block.
1024 	 */
1025 	if (node->tn_vpstate & TMPFS_VNODE_ALLOCATING) {
1026 		node->tn_vpstate |= TMPFS_VNODE_WANT;
1027 		error = msleep((caddr_t) &node->tn_vpstate,
1028 		    TMPFS_NODE_MTX(node), 0, "tmpfs_alloc_vp", 0);
1029 		if (error != 0)
1030 			goto out;
1031 		goto loop;
1032 	} else
1033 		node->tn_vpstate |= TMPFS_VNODE_ALLOCATING;
1034 
1035 	TMPFS_NODE_UNLOCK(node);
1036 
1037 	/* Get a new vnode and associate it with our node. */
1038 	error = getnewvnode("tmpfs", mp, VFS_TO_TMPFS(mp)->tm_nonc ?
1039 	    &tmpfs_vnodeop_nonc_entries : &tmpfs_vnodeop_entries, &vp);
1040 	if (error != 0)
1041 		goto unlock;
1042 	MPASS(vp != NULL);
1043 
1044 	/* lkflag is ignored, the lock is exclusive */
1045 	(void) vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1046 
1047 	vp->v_data = node;
1048 	vp->v_type = node->tn_type;
1049 
1050 	/* Type-specific initialization. */
1051 	switch (node->tn_type) {
1052 	case VBLK:
1053 		/* FALLTHROUGH */
1054 	case VCHR:
1055 		/* FALLTHROUGH */
1056 	case VLNK:
1057 		/* FALLTHROUGH */
1058 	case VSOCK:
1059 		break;
1060 	case VFIFO:
1061 		vp->v_op = &tmpfs_fifoop_entries;
1062 		break;
1063 	case VREG:
1064 		object = node->tn_reg.tn_aobj;
1065 		VM_OBJECT_WLOCK(object);
1066 		KASSERT((object->flags & OBJ_TMPFS_VREF) == 0,
1067 		    ("%s: object %p with OBJ_TMPFS_VREF but without vnode",
1068 		    __func__, object));
1069 		VI_LOCK(vp);
1070 		KASSERT(vp->v_object == NULL, ("Not NULL v_object in tmpfs"));
1071 		vp->v_object = object;
1072 		vn_irflag_set_locked(vp, (tm->tm_pgread ? VIRF_PGREAD : 0));
1073 		VI_UNLOCK(vp);
1074 		VNASSERT((object->flags & OBJ_TMPFS_VREF) == 0, vp,
1075 		    ("leaked OBJ_TMPFS_VREF"));
1076 		if (object->un_pager.swp.writemappings > 0) {
1077 			vrefact(vp);
1078 			vlazy(vp);
1079 			vm_object_set_flag(object, OBJ_TMPFS_VREF);
1080 		}
1081 		VM_OBJECT_WUNLOCK(object);
1082 		break;
1083 	case VDIR:
1084 		MPASS(node->tn_dir.tn_parent != NULL);
1085 		if (node->tn_dir.tn_parent == node)
1086 			vp->v_vflag |= VV_ROOT;
1087 		break;
1088 
1089 	default:
1090 		panic("tmpfs_alloc_vp: type %p %d", node, (int)node->tn_type);
1091 	}
1092 	if (vp->v_type != VFIFO)
1093 		VN_LOCK_ASHARE(vp);
1094 
1095 	error = insmntque1(vp, mp, tmpfs_insmntque_dtr, NULL);
1096 	if (error != 0)
1097 		vp = NULL;
1098 
1099 unlock:
1100 	TMPFS_NODE_LOCK(node);
1101 
1102 	MPASS(node->tn_vpstate & TMPFS_VNODE_ALLOCATING);
1103 	node->tn_vpstate &= ~TMPFS_VNODE_ALLOCATING;
1104 	node->tn_vnode = vp;
1105 
1106 	if (node->tn_vpstate & TMPFS_VNODE_WANT) {
1107 		node->tn_vpstate &= ~TMPFS_VNODE_WANT;
1108 		TMPFS_NODE_UNLOCK(node);
1109 		wakeup((caddr_t) &node->tn_vpstate);
1110 	} else
1111 		TMPFS_NODE_UNLOCK(node);
1112 
1113 out:
1114 	if (error == 0) {
1115 		*vpp = vp;
1116 
1117 #ifdef INVARIANTS
1118 		MPASS(*vpp != NULL);
1119 		ASSERT_VOP_LOCKED(*vpp, __func__);
1120 		TMPFS_NODE_LOCK(node);
1121 		MPASS(*vpp == node->tn_vnode);
1122 		TMPFS_NODE_UNLOCK(node);
1123 #endif
1124 	}
1125 	tmpfs_free_node(tm, node);
1126 
1127 	return (error);
1128 }
1129 
1130 /*
1131  * Destroys the association between the vnode vp and the node it
1132  * references.
1133  */
1134 void
tmpfs_free_vp(struct vnode * vp)1135 tmpfs_free_vp(struct vnode *vp)
1136 {
1137 	struct tmpfs_node *node;
1138 
1139 	node = VP_TO_TMPFS_NODE(vp);
1140 
1141 	TMPFS_NODE_ASSERT_LOCKED(node);
1142 	node->tn_vnode = NULL;
1143 	if ((node->tn_vpstate & TMPFS_VNODE_WRECLAIM) != 0)
1144 		wakeup(&node->tn_vnode);
1145 	node->tn_vpstate &= ~TMPFS_VNODE_WRECLAIM;
1146 	vp->v_data = NULL;
1147 }
1148 
1149 /*
1150  * Allocates a new file of type 'type' and adds it to the parent directory
1151  * 'dvp'; this addition is done using the component name given in 'cnp'.
1152  * The ownership of the new file is automatically assigned based on the
1153  * credentials of the caller (through 'cnp'), the group is set based on
1154  * the parent directory and the mode is determined from the 'vap' argument.
1155  * If successful, *vpp holds a vnode to the newly created file and zero
1156  * is returned.  Otherwise *vpp is NULL and the function returns an
1157  * appropriate error code.
1158  */
1159 int
tmpfs_alloc_file(struct vnode * dvp,struct vnode ** vpp,struct vattr * vap,struct componentname * cnp,const char * target)1160 tmpfs_alloc_file(struct vnode *dvp, struct vnode **vpp, struct vattr *vap,
1161     struct componentname *cnp, const char *target)
1162 {
1163 	int error;
1164 	struct tmpfs_dirent *de;
1165 	struct tmpfs_mount *tmp;
1166 	struct tmpfs_node *dnode;
1167 	struct tmpfs_node *node;
1168 	struct tmpfs_node *parent;
1169 
1170 	ASSERT_VOP_ELOCKED(dvp, "tmpfs_alloc_file");
1171 	MPASS(cnp->cn_flags & HASBUF);
1172 
1173 	tmp = VFS_TO_TMPFS(dvp->v_mount);
1174 	dnode = VP_TO_TMPFS_DIR(dvp);
1175 	*vpp = NULL;
1176 
1177 	/* If the entry we are creating is a directory, we cannot overflow
1178 	 * the number of links of its parent, because it will get a new
1179 	 * link. */
1180 	if (vap->va_type == VDIR) {
1181 		/* Ensure that we do not overflow the maximum number of links
1182 		 * imposed by the system. */
1183 		MPASS(dnode->tn_links <= TMPFS_LINK_MAX);
1184 		if (dnode->tn_links == TMPFS_LINK_MAX) {
1185 			return (EMLINK);
1186 		}
1187 
1188 		parent = dnode;
1189 		MPASS(parent != NULL);
1190 	} else
1191 		parent = NULL;
1192 
1193 	/* Allocate a node that represents the new file. */
1194 	error = tmpfs_alloc_node(dvp->v_mount, tmp, vap->va_type,
1195 	    cnp->cn_cred->cr_uid, dnode->tn_gid, vap->va_mode, parent,
1196 	    target, vap->va_rdev, &node);
1197 	if (error != 0)
1198 		return (error);
1199 
1200 	/* Allocate a directory entry that points to the new file. */
1201 	error = tmpfs_alloc_dirent(tmp, node, cnp->cn_nameptr, cnp->cn_namelen,
1202 	    &de);
1203 	if (error != 0) {
1204 		tmpfs_free_node(tmp, node);
1205 		return (error);
1206 	}
1207 
1208 	/* Allocate a vnode for the new file. */
1209 	error = tmpfs_alloc_vp(dvp->v_mount, node, LK_EXCLUSIVE, vpp);
1210 	if (error != 0) {
1211 		tmpfs_free_dirent(tmp, de);
1212 		tmpfs_free_node(tmp, node);
1213 		return (error);
1214 	}
1215 
1216 	/* Now that all required items are allocated, we can proceed to
1217 	 * insert the new node into the directory, an operation that
1218 	 * cannot fail. */
1219 	if (cnp->cn_flags & ISWHITEOUT)
1220 		tmpfs_dir_whiteout_remove(dvp, cnp);
1221 	tmpfs_dir_attach(dvp, de);
1222 	return (0);
1223 }
1224 
1225 struct tmpfs_dirent *
tmpfs_dir_first(struct tmpfs_node * dnode,struct tmpfs_dir_cursor * dc)1226 tmpfs_dir_first(struct tmpfs_node *dnode, struct tmpfs_dir_cursor *dc)
1227 {
1228 	struct tmpfs_dirent *de;
1229 
1230 	de = RB_MIN(tmpfs_dir, &dnode->tn_dir.tn_dirhead);
1231 	dc->tdc_tree = de;
1232 	if (de != NULL && tmpfs_dirent_duphead(de))
1233 		de = LIST_FIRST(&de->ud.td_duphead);
1234 	dc->tdc_current = de;
1235 
1236 	return (dc->tdc_current);
1237 }
1238 
1239 struct tmpfs_dirent *
tmpfs_dir_next(struct tmpfs_node * dnode,struct tmpfs_dir_cursor * dc)1240 tmpfs_dir_next(struct tmpfs_node *dnode, struct tmpfs_dir_cursor *dc)
1241 {
1242 	struct tmpfs_dirent *de;
1243 
1244 	MPASS(dc->tdc_tree != NULL);
1245 	if (tmpfs_dirent_dup(dc->tdc_current)) {
1246 		dc->tdc_current = LIST_NEXT(dc->tdc_current, uh.td_dup.entries);
1247 		if (dc->tdc_current != NULL)
1248 			return (dc->tdc_current);
1249 	}
1250 	dc->tdc_tree = dc->tdc_current = RB_NEXT(tmpfs_dir,
1251 	    &dnode->tn_dir.tn_dirhead, dc->tdc_tree);
1252 	if ((de = dc->tdc_current) != NULL && tmpfs_dirent_duphead(de)) {
1253 		dc->tdc_current = LIST_FIRST(&de->ud.td_duphead);
1254 		MPASS(dc->tdc_current != NULL);
1255 	}
1256 
1257 	return (dc->tdc_current);
1258 }
1259 
1260 /* Lookup directory entry in RB-Tree. Function may return duphead entry. */
1261 static struct tmpfs_dirent *
tmpfs_dir_xlookup_hash(struct tmpfs_node * dnode,uint32_t hash)1262 tmpfs_dir_xlookup_hash(struct tmpfs_node *dnode, uint32_t hash)
1263 {
1264 	struct tmpfs_dirent *de, dekey;
1265 
1266 	dekey.td_hash = hash;
1267 	de = RB_FIND(tmpfs_dir, &dnode->tn_dir.tn_dirhead, &dekey);
1268 	return (de);
1269 }
1270 
1271 /* Lookup directory entry by cookie, initialize directory cursor accordingly. */
1272 static struct tmpfs_dirent *
tmpfs_dir_lookup_cookie(struct tmpfs_node * node,off_t cookie,struct tmpfs_dir_cursor * dc)1273 tmpfs_dir_lookup_cookie(struct tmpfs_node *node, off_t cookie,
1274     struct tmpfs_dir_cursor *dc)
1275 {
1276 	struct tmpfs_dir *dirhead = &node->tn_dir.tn_dirhead;
1277 	struct tmpfs_dirent *de, dekey;
1278 
1279 	MPASS(cookie >= TMPFS_DIRCOOKIE_MIN);
1280 
1281 	if (cookie == node->tn_dir.tn_readdir_lastn &&
1282 	    (de = node->tn_dir.tn_readdir_lastp) != NULL) {
1283 		/* Protect against possible race, tn_readdir_last[pn]
1284 		 * may be updated with only shared vnode lock held. */
1285 		if (cookie == tmpfs_dirent_cookie(de))
1286 			goto out;
1287 	}
1288 
1289 	if ((cookie & TMPFS_DIRCOOKIE_DUP) != 0) {
1290 		LIST_FOREACH(de, &node->tn_dir.tn_dupindex,
1291 		    uh.td_dup.index_entries) {
1292 			MPASS(tmpfs_dirent_dup(de));
1293 			if (de->td_cookie == cookie)
1294 				goto out;
1295 			/* dupindex list is sorted. */
1296 			if (de->td_cookie < cookie) {
1297 				de = NULL;
1298 				goto out;
1299 			}
1300 		}
1301 		MPASS(de == NULL);
1302 		goto out;
1303 	}
1304 
1305 	if ((cookie & TMPFS_DIRCOOKIE_MASK) != cookie) {
1306 		de = NULL;
1307 	} else {
1308 		dekey.td_hash = cookie;
1309 		/* Recover if direntry for cookie was removed */
1310 		de = RB_NFIND(tmpfs_dir, dirhead, &dekey);
1311 	}
1312 	dc->tdc_tree = de;
1313 	dc->tdc_current = de;
1314 	if (de != NULL && tmpfs_dirent_duphead(de)) {
1315 		dc->tdc_current = LIST_FIRST(&de->ud.td_duphead);
1316 		MPASS(dc->tdc_current != NULL);
1317 	}
1318 	return (dc->tdc_current);
1319 
1320 out:
1321 	dc->tdc_tree = de;
1322 	dc->tdc_current = de;
1323 	if (de != NULL && tmpfs_dirent_dup(de))
1324 		dc->tdc_tree = tmpfs_dir_xlookup_hash(node,
1325 		    de->td_hash);
1326 	return (dc->tdc_current);
1327 }
1328 
1329 /*
1330  * Looks for a directory entry in the directory represented by node.
1331  * 'cnp' describes the name of the entry to look for.  Note that the .
1332  * and .. components are not allowed as they do not physically exist
1333  * within directories.
1334  *
1335  * Returns a pointer to the entry when found, otherwise NULL.
1336  */
1337 struct tmpfs_dirent *
tmpfs_dir_lookup(struct tmpfs_node * node,struct tmpfs_node * f,struct componentname * cnp)1338 tmpfs_dir_lookup(struct tmpfs_node *node, struct tmpfs_node *f,
1339     struct componentname *cnp)
1340 {
1341 	struct tmpfs_dir_duphead *duphead;
1342 	struct tmpfs_dirent *de;
1343 	uint32_t hash;
1344 
1345 	MPASS(IMPLIES(cnp->cn_namelen == 1, cnp->cn_nameptr[0] != '.'));
1346 	MPASS(IMPLIES(cnp->cn_namelen == 2, !(cnp->cn_nameptr[0] == '.' &&
1347 	    cnp->cn_nameptr[1] == '.')));
1348 	TMPFS_VALIDATE_DIR(node);
1349 
1350 	hash = tmpfs_dirent_hash(cnp->cn_nameptr, cnp->cn_namelen);
1351 	de = tmpfs_dir_xlookup_hash(node, hash);
1352 	if (de != NULL && tmpfs_dirent_duphead(de)) {
1353 		duphead = &de->ud.td_duphead;
1354 		LIST_FOREACH(de, duphead, uh.td_dup.entries) {
1355 			if (TMPFS_DIRENT_MATCHES(de, cnp->cn_nameptr,
1356 			    cnp->cn_namelen))
1357 				break;
1358 		}
1359 	} else if (de != NULL) {
1360 		if (!TMPFS_DIRENT_MATCHES(de, cnp->cn_nameptr,
1361 		    cnp->cn_namelen))
1362 			de = NULL;
1363 	}
1364 	if (de != NULL && f != NULL && de->td_node != f)
1365 		de = NULL;
1366 
1367 	return (de);
1368 }
1369 
1370 /*
1371  * Attach duplicate-cookie directory entry nde to dnode and insert to dupindex
1372  * list, allocate new cookie value.
1373  */
1374 static void
tmpfs_dir_attach_dup(struct tmpfs_node * dnode,struct tmpfs_dir_duphead * duphead,struct tmpfs_dirent * nde)1375 tmpfs_dir_attach_dup(struct tmpfs_node *dnode,
1376     struct tmpfs_dir_duphead *duphead, struct tmpfs_dirent *nde)
1377 {
1378 	struct tmpfs_dir_duphead *dupindex;
1379 	struct tmpfs_dirent *de, *pde;
1380 
1381 	dupindex = &dnode->tn_dir.tn_dupindex;
1382 	de = LIST_FIRST(dupindex);
1383 	if (de == NULL || de->td_cookie < TMPFS_DIRCOOKIE_DUP_MAX) {
1384 		if (de == NULL)
1385 			nde->td_cookie = TMPFS_DIRCOOKIE_DUP_MIN;
1386 		else
1387 			nde->td_cookie = de->td_cookie + 1;
1388 		MPASS(tmpfs_dirent_dup(nde));
1389 		LIST_INSERT_HEAD(dupindex, nde, uh.td_dup.index_entries);
1390 		LIST_INSERT_HEAD(duphead, nde, uh.td_dup.entries);
1391 		return;
1392 	}
1393 
1394 	/*
1395 	 * Cookie numbers are near exhaustion. Scan dupindex list for unused
1396 	 * numbers. dupindex list is sorted in descending order. Keep it so
1397 	 * after inserting nde.
1398 	 */
1399 	while (1) {
1400 		pde = de;
1401 		de = LIST_NEXT(de, uh.td_dup.index_entries);
1402 		if (de == NULL && pde->td_cookie != TMPFS_DIRCOOKIE_DUP_MIN) {
1403 			/*
1404 			 * Last element of the index doesn't have minimal cookie
1405 			 * value, use it.
1406 			 */
1407 			nde->td_cookie = TMPFS_DIRCOOKIE_DUP_MIN;
1408 			LIST_INSERT_AFTER(pde, nde, uh.td_dup.index_entries);
1409 			LIST_INSERT_HEAD(duphead, nde, uh.td_dup.entries);
1410 			return;
1411 		} else if (de == NULL) {
1412 			/*
1413 			 * We are so lucky have 2^30 hash duplicates in single
1414 			 * directory :) Return largest possible cookie value.
1415 			 * It should be fine except possible issues with
1416 			 * VOP_READDIR restart.
1417 			 */
1418 			nde->td_cookie = TMPFS_DIRCOOKIE_DUP_MAX;
1419 			LIST_INSERT_HEAD(dupindex, nde,
1420 			    uh.td_dup.index_entries);
1421 			LIST_INSERT_HEAD(duphead, nde, uh.td_dup.entries);
1422 			return;
1423 		}
1424 		if (de->td_cookie + 1 == pde->td_cookie ||
1425 		    de->td_cookie >= TMPFS_DIRCOOKIE_DUP_MAX)
1426 			continue;	/* No hole or invalid cookie. */
1427 		nde->td_cookie = de->td_cookie + 1;
1428 		MPASS(tmpfs_dirent_dup(nde));
1429 		MPASS(pde->td_cookie > nde->td_cookie);
1430 		MPASS(nde->td_cookie > de->td_cookie);
1431 		LIST_INSERT_BEFORE(de, nde, uh.td_dup.index_entries);
1432 		LIST_INSERT_HEAD(duphead, nde, uh.td_dup.entries);
1433 		return;
1434 	}
1435 }
1436 
1437 /*
1438  * Attaches the directory entry de to the directory represented by vp.
1439  * Note that this does not change the link count of the node pointed by
1440  * the directory entry, as this is done by tmpfs_alloc_dirent.
1441  */
1442 void
tmpfs_dir_attach(struct vnode * vp,struct tmpfs_dirent * de)1443 tmpfs_dir_attach(struct vnode *vp, struct tmpfs_dirent *de)
1444 {
1445 	struct tmpfs_node *dnode;
1446 	struct tmpfs_dirent *xde, *nde;
1447 
1448 	ASSERT_VOP_ELOCKED(vp, __func__);
1449 	MPASS(de->td_namelen > 0);
1450 	MPASS(de->td_hash >= TMPFS_DIRCOOKIE_MIN);
1451 	MPASS(de->td_cookie == de->td_hash);
1452 
1453 	dnode = VP_TO_TMPFS_DIR(vp);
1454 	dnode->tn_dir.tn_readdir_lastn = 0;
1455 	dnode->tn_dir.tn_readdir_lastp = NULL;
1456 
1457 	MPASS(!tmpfs_dirent_dup(de));
1458 	xde = RB_INSERT(tmpfs_dir, &dnode->tn_dir.tn_dirhead, de);
1459 	if (xde != NULL && tmpfs_dirent_duphead(xde))
1460 		tmpfs_dir_attach_dup(dnode, &xde->ud.td_duphead, de);
1461 	else if (xde != NULL) {
1462 		/*
1463 		 * Allocate new duphead. Swap xde with duphead to avoid
1464 		 * adding/removing elements with the same hash.
1465 		 */
1466 		MPASS(!tmpfs_dirent_dup(xde));
1467 		tmpfs_alloc_dirent(VFS_TO_TMPFS(vp->v_mount), NULL, NULL, 0,
1468 		    &nde);
1469 		/* *nde = *xde; XXX gcc 4.2.1 may generate invalid code. */
1470 		memcpy(nde, xde, sizeof(*xde));
1471 		xde->td_cookie |= TMPFS_DIRCOOKIE_DUPHEAD;
1472 		LIST_INIT(&xde->ud.td_duphead);
1473 		xde->td_namelen = 0;
1474 		xde->td_node = NULL;
1475 		tmpfs_dir_attach_dup(dnode, &xde->ud.td_duphead, nde);
1476 		tmpfs_dir_attach_dup(dnode, &xde->ud.td_duphead, de);
1477 	}
1478 	dnode->tn_size += sizeof(struct tmpfs_dirent);
1479 	dnode->tn_status |= TMPFS_NODE_CHANGED | TMPFS_NODE_MODIFIED;
1480 	dnode->tn_accessed = true;
1481 	tmpfs_update(vp);
1482 }
1483 
1484 /*
1485  * Detaches the directory entry de from the directory represented by vp.
1486  * Note that this does not change the link count of the node pointed by
1487  * the directory entry, as this is done by tmpfs_free_dirent.
1488  */
1489 void
tmpfs_dir_detach(struct vnode * vp,struct tmpfs_dirent * de)1490 tmpfs_dir_detach(struct vnode *vp, struct tmpfs_dirent *de)
1491 {
1492 	struct tmpfs_mount *tmp;
1493 	struct tmpfs_dir *head;
1494 	struct tmpfs_node *dnode;
1495 	struct tmpfs_dirent *xde;
1496 
1497 	ASSERT_VOP_ELOCKED(vp, __func__);
1498 
1499 	dnode = VP_TO_TMPFS_DIR(vp);
1500 	head = &dnode->tn_dir.tn_dirhead;
1501 	dnode->tn_dir.tn_readdir_lastn = 0;
1502 	dnode->tn_dir.tn_readdir_lastp = NULL;
1503 
1504 	if (tmpfs_dirent_dup(de)) {
1505 		/* Remove duphead if de was last entry. */
1506 		if (LIST_NEXT(de, uh.td_dup.entries) == NULL) {
1507 			xde = tmpfs_dir_xlookup_hash(dnode, de->td_hash);
1508 			MPASS(tmpfs_dirent_duphead(xde));
1509 		} else
1510 			xde = NULL;
1511 		LIST_REMOVE(de, uh.td_dup.entries);
1512 		LIST_REMOVE(de, uh.td_dup.index_entries);
1513 		if (xde != NULL) {
1514 			if (LIST_EMPTY(&xde->ud.td_duphead)) {
1515 				RB_REMOVE(tmpfs_dir, head, xde);
1516 				tmp = VFS_TO_TMPFS(vp->v_mount);
1517 				MPASS(xde->td_node == NULL);
1518 				tmpfs_free_dirent(tmp, xde);
1519 			}
1520 		}
1521 		de->td_cookie = de->td_hash;
1522 	} else
1523 		RB_REMOVE(tmpfs_dir, head, de);
1524 
1525 	dnode->tn_size -= sizeof(struct tmpfs_dirent);
1526 	dnode->tn_status |= TMPFS_NODE_CHANGED | TMPFS_NODE_MODIFIED;
1527 	dnode->tn_accessed = true;
1528 	tmpfs_update(vp);
1529 }
1530 
1531 void
tmpfs_dir_destroy(struct tmpfs_mount * tmp,struct tmpfs_node * dnode)1532 tmpfs_dir_destroy(struct tmpfs_mount *tmp, struct tmpfs_node *dnode)
1533 {
1534 	struct tmpfs_dirent *de, *dde, *nde;
1535 
1536 	RB_FOREACH_SAFE(de, tmpfs_dir, &dnode->tn_dir.tn_dirhead, nde) {
1537 		RB_REMOVE(tmpfs_dir, &dnode->tn_dir.tn_dirhead, de);
1538 		/* Node may already be destroyed. */
1539 		de->td_node = NULL;
1540 		if (tmpfs_dirent_duphead(de)) {
1541 			while ((dde = LIST_FIRST(&de->ud.td_duphead)) != NULL) {
1542 				LIST_REMOVE(dde, uh.td_dup.entries);
1543 				dde->td_node = NULL;
1544 				tmpfs_free_dirent(tmp, dde);
1545 			}
1546 		}
1547 		tmpfs_free_dirent(tmp, de);
1548 	}
1549 }
1550 
1551 /*
1552  * Helper function for tmpfs_readdir.  Creates a '.' entry for the given
1553  * directory and returns it in the uio space.  The function returns 0
1554  * on success, -1 if there was not enough space in the uio structure to
1555  * hold the directory entry or an appropriate error code if another
1556  * error happens.
1557  */
1558 static int
tmpfs_dir_getdotdent(struct tmpfs_mount * tm,struct tmpfs_node * node,struct uio * uio)1559 tmpfs_dir_getdotdent(struct tmpfs_mount *tm, struct tmpfs_node *node,
1560     struct uio *uio)
1561 {
1562 	int error;
1563 	struct dirent dent;
1564 
1565 	TMPFS_VALIDATE_DIR(node);
1566 	MPASS(uio->uio_offset == TMPFS_DIRCOOKIE_DOT);
1567 
1568 	dent.d_fileno = node->tn_id;
1569 	dent.d_off = TMPFS_DIRCOOKIE_DOTDOT;
1570 	dent.d_type = DT_DIR;
1571 	dent.d_namlen = 1;
1572 	dent.d_name[0] = '.';
1573 	dent.d_reclen = GENERIC_DIRSIZ(&dent);
1574 	dirent_terminate(&dent);
1575 
1576 	if (dent.d_reclen > uio->uio_resid)
1577 		error = EJUSTRETURN;
1578 	else
1579 		error = uiomove(&dent, dent.d_reclen, uio);
1580 
1581 	tmpfs_set_accessed(tm, node);
1582 
1583 	return (error);
1584 }
1585 
1586 /*
1587  * Helper function for tmpfs_readdir.  Creates a '..' entry for the given
1588  * directory and returns it in the uio space.  The function returns 0
1589  * on success, -1 if there was not enough space in the uio structure to
1590  * hold the directory entry or an appropriate error code if another
1591  * error happens.
1592  */
1593 static int
tmpfs_dir_getdotdotdent(struct tmpfs_mount * tm,struct tmpfs_node * node,struct uio * uio,off_t next)1594 tmpfs_dir_getdotdotdent(struct tmpfs_mount *tm, struct tmpfs_node *node,
1595     struct uio *uio, off_t next)
1596 {
1597 	struct tmpfs_node *parent;
1598 	struct dirent dent;
1599 	int error;
1600 
1601 	TMPFS_VALIDATE_DIR(node);
1602 	MPASS(uio->uio_offset == TMPFS_DIRCOOKIE_DOTDOT);
1603 
1604 	/*
1605 	 * Return ENOENT if the current node is already removed.
1606 	 */
1607 	TMPFS_ASSERT_LOCKED(node);
1608 	parent = node->tn_dir.tn_parent;
1609 	if (parent == NULL)
1610 		return (ENOENT);
1611 
1612 	dent.d_fileno = parent->tn_id;
1613 	dent.d_off = next;
1614 	dent.d_type = DT_DIR;
1615 	dent.d_namlen = 2;
1616 	dent.d_name[0] = '.';
1617 	dent.d_name[1] = '.';
1618 	dent.d_reclen = GENERIC_DIRSIZ(&dent);
1619 	dirent_terminate(&dent);
1620 
1621 	if (dent.d_reclen > uio->uio_resid)
1622 		error = EJUSTRETURN;
1623 	else
1624 		error = uiomove(&dent, dent.d_reclen, uio);
1625 
1626 	tmpfs_set_accessed(tm, node);
1627 
1628 	return (error);
1629 }
1630 
1631 /*
1632  * Helper function for tmpfs_readdir.  Returns as much directory entries
1633  * as can fit in the uio space.  The read starts at uio->uio_offset.
1634  * The function returns 0 on success, -1 if there was not enough space
1635  * in the uio structure to hold the directory entry or an appropriate
1636  * error code if another error happens.
1637  */
1638 int
tmpfs_dir_getdents(struct tmpfs_mount * tm,struct tmpfs_node * node,struct uio * uio,int maxcookies,u_long * cookies,int * ncookies)1639 tmpfs_dir_getdents(struct tmpfs_mount *tm, struct tmpfs_node *node,
1640     struct uio *uio, int maxcookies, u_long *cookies, int *ncookies)
1641 {
1642 	struct tmpfs_dir_cursor dc;
1643 	struct tmpfs_dirent *de, *nde;
1644 	off_t off;
1645 	int error;
1646 
1647 	TMPFS_VALIDATE_DIR(node);
1648 
1649 	off = 0;
1650 
1651 	/*
1652 	 * Lookup the node from the current offset.  The starting offset of
1653 	 * 0 will lookup both '.' and '..', and then the first real entry,
1654 	 * or EOF if there are none.  Then find all entries for the dir that
1655 	 * fit into the buffer.  Once no more entries are found (de == NULL),
1656 	 * the offset is set to TMPFS_DIRCOOKIE_EOF, which will cause the next
1657 	 * call to return 0.
1658 	 */
1659 	switch (uio->uio_offset) {
1660 	case TMPFS_DIRCOOKIE_DOT:
1661 		error = tmpfs_dir_getdotdent(tm, node, uio);
1662 		if (error != 0)
1663 			return (error);
1664 		uio->uio_offset = off = TMPFS_DIRCOOKIE_DOTDOT;
1665 		if (cookies != NULL)
1666 			cookies[(*ncookies)++] = off;
1667 		/* FALLTHROUGH */
1668 	case TMPFS_DIRCOOKIE_DOTDOT:
1669 		de = tmpfs_dir_first(node, &dc);
1670 		off = tmpfs_dirent_cookie(de);
1671 		error = tmpfs_dir_getdotdotdent(tm, node, uio, off);
1672 		if (error != 0)
1673 			return (error);
1674 		uio->uio_offset = off;
1675 		if (cookies != NULL)
1676 			cookies[(*ncookies)++] = off;
1677 		/* EOF. */
1678 		if (de == NULL)
1679 			return (0);
1680 		break;
1681 	case TMPFS_DIRCOOKIE_EOF:
1682 		return (0);
1683 	default:
1684 		de = tmpfs_dir_lookup_cookie(node, uio->uio_offset, &dc);
1685 		if (de == NULL)
1686 			return (EINVAL);
1687 		if (cookies != NULL)
1688 			off = tmpfs_dirent_cookie(de);
1689 	}
1690 
1691 	/*
1692 	 * Read as much entries as possible; i.e., until we reach the end of the
1693 	 * directory or we exhaust uio space.
1694 	 */
1695 	do {
1696 		struct dirent d;
1697 
1698 		/*
1699 		 * Create a dirent structure representing the current tmpfs_node
1700 		 * and fill it.
1701 		 */
1702 		if (de->td_node == NULL) {
1703 			d.d_fileno = 1;
1704 			d.d_type = DT_WHT;
1705 		} else {
1706 			d.d_fileno = de->td_node->tn_id;
1707 			switch (de->td_node->tn_type) {
1708 			case VBLK:
1709 				d.d_type = DT_BLK;
1710 				break;
1711 
1712 			case VCHR:
1713 				d.d_type = DT_CHR;
1714 				break;
1715 
1716 			case VDIR:
1717 				d.d_type = DT_DIR;
1718 				break;
1719 
1720 			case VFIFO:
1721 				d.d_type = DT_FIFO;
1722 				break;
1723 
1724 			case VLNK:
1725 				d.d_type = DT_LNK;
1726 				break;
1727 
1728 			case VREG:
1729 				d.d_type = DT_REG;
1730 				break;
1731 
1732 			case VSOCK:
1733 				d.d_type = DT_SOCK;
1734 				break;
1735 
1736 			default:
1737 				panic("tmpfs_dir_getdents: type %p %d",
1738 				    de->td_node, (int)de->td_node->tn_type);
1739 			}
1740 		}
1741 		d.d_namlen = de->td_namelen;
1742 		MPASS(de->td_namelen < sizeof(d.d_name));
1743 		(void)memcpy(d.d_name, de->ud.td_name, de->td_namelen);
1744 		d.d_reclen = GENERIC_DIRSIZ(&d);
1745 
1746 		/*
1747 		 * Stop reading if the directory entry we are treating is bigger
1748 		 * than the amount of data that can be returned.
1749 		 */
1750 		if (d.d_reclen > uio->uio_resid) {
1751 			error = EJUSTRETURN;
1752 			break;
1753 		}
1754 
1755 		nde = tmpfs_dir_next(node, &dc);
1756 		d.d_off = tmpfs_dirent_cookie(nde);
1757 		dirent_terminate(&d);
1758 
1759 		/*
1760 		 * Copy the new dirent structure into the output buffer and
1761 		 * advance pointers.
1762 		 */
1763 		error = uiomove(&d, d.d_reclen, uio);
1764 		if (error == 0) {
1765 			de = nde;
1766 			if (cookies != NULL) {
1767 				off = tmpfs_dirent_cookie(de);
1768 				MPASS(*ncookies < maxcookies);
1769 				cookies[(*ncookies)++] = off;
1770 			}
1771 		}
1772 	} while (error == 0 && uio->uio_resid > 0 && de != NULL);
1773 
1774 	/* Skip setting off when using cookies as it is already done above. */
1775 	if (cookies == NULL)
1776 		off = tmpfs_dirent_cookie(de);
1777 
1778 	/* Update the offset and cache. */
1779 	uio->uio_offset = off;
1780 	node->tn_dir.tn_readdir_lastn = off;
1781 	node->tn_dir.tn_readdir_lastp = de;
1782 
1783 	tmpfs_set_accessed(tm, node);
1784 	return (error);
1785 }
1786 
1787 int
tmpfs_dir_whiteout_add(struct vnode * dvp,struct componentname * cnp)1788 tmpfs_dir_whiteout_add(struct vnode *dvp, struct componentname *cnp)
1789 {
1790 	struct tmpfs_dirent *de;
1791 	int error;
1792 
1793 	error = tmpfs_alloc_dirent(VFS_TO_TMPFS(dvp->v_mount), NULL,
1794 	    cnp->cn_nameptr, cnp->cn_namelen, &de);
1795 	if (error != 0)
1796 		return (error);
1797 	tmpfs_dir_attach(dvp, de);
1798 	return (0);
1799 }
1800 
1801 void
tmpfs_dir_whiteout_remove(struct vnode * dvp,struct componentname * cnp)1802 tmpfs_dir_whiteout_remove(struct vnode *dvp, struct componentname *cnp)
1803 {
1804 	struct tmpfs_dirent *de;
1805 
1806 	de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(dvp), NULL, cnp);
1807 	MPASS(de != NULL && de->td_node == NULL);
1808 	tmpfs_dir_detach(dvp, de);
1809 	tmpfs_free_dirent(VFS_TO_TMPFS(dvp->v_mount), de);
1810 }
1811 
1812 /*
1813  * Resizes the aobj associated with the regular file pointed to by 'vp' to the
1814  * size 'newsize'.  'vp' must point to a vnode that represents a regular file.
1815  * 'newsize' must be positive.
1816  *
1817  * Returns zero on success or an appropriate error code on failure.
1818  */
1819 int
tmpfs_reg_resize(struct vnode * vp,off_t newsize,boolean_t ignerr)1820 tmpfs_reg_resize(struct vnode *vp, off_t newsize, boolean_t ignerr)
1821 {
1822 	struct tmpfs_node *node;
1823 	vm_object_t uobj;
1824 	vm_pindex_t idx, newpages, oldpages;
1825 	off_t oldsize;
1826 	int base, error;
1827 
1828 	MPASS(vp->v_type == VREG);
1829 	MPASS(newsize >= 0);
1830 
1831 	node = VP_TO_TMPFS_NODE(vp);
1832 	uobj = node->tn_reg.tn_aobj;
1833 
1834 	/*
1835 	 * Convert the old and new sizes to the number of pages needed to
1836 	 * store them.  It may happen that we do not need to do anything
1837 	 * because the last allocated page can accommodate the change on
1838 	 * its own.
1839 	 */
1840 	oldsize = node->tn_size;
1841 	oldpages = OFF_TO_IDX(oldsize + PAGE_MASK);
1842 	MPASS(oldpages == uobj->size);
1843 	newpages = OFF_TO_IDX(newsize + PAGE_MASK);
1844 
1845 	if (__predict_true(newpages == oldpages && newsize >= oldsize)) {
1846 		node->tn_size = newsize;
1847 		return (0);
1848 	}
1849 
1850 	VM_OBJECT_WLOCK(uobj);
1851 	if (newsize < oldsize) {
1852 		/*
1853 		 * Zero the truncated part of the last page.
1854 		 */
1855 		base = newsize & PAGE_MASK;
1856 		if (base != 0) {
1857 			idx = OFF_TO_IDX(newsize);
1858 			error = tmpfs_partial_page_invalidate(uobj, idx, base,
1859 			    PAGE_SIZE, ignerr);
1860 			if (error != 0) {
1861 				VM_OBJECT_WUNLOCK(uobj);
1862 				return (error);
1863 			}
1864 		}
1865 
1866 		/*
1867 		 * Release any swap space and free any whole pages.
1868 		 */
1869 		if (newpages < oldpages)
1870 			vm_object_page_remove(uobj, newpages, 0, 0);
1871 	}
1872 	uobj->size = newpages;
1873 	VM_OBJECT_WUNLOCK(uobj);
1874 
1875 	node->tn_size = newsize;
1876 	return (0);
1877 }
1878 
1879 void
tmpfs_check_mtime(struct vnode * vp)1880 tmpfs_check_mtime(struct vnode *vp)
1881 {
1882 	struct tmpfs_node *node;
1883 	struct vm_object *obj;
1884 
1885 	ASSERT_VOP_ELOCKED(vp, "check_mtime");
1886 	if (vp->v_type != VREG)
1887 		return;
1888 	obj = vp->v_object;
1889 	KASSERT(obj->type == tmpfs_pager_type &&
1890 	    (obj->flags & (OBJ_SWAP | OBJ_TMPFS)) ==
1891 	    (OBJ_SWAP | OBJ_TMPFS), ("non-tmpfs obj"));
1892 	/* unlocked read */
1893 	if (obj->generation != obj->cleangeneration) {
1894 		VM_OBJECT_WLOCK(obj);
1895 		if (obj->generation != obj->cleangeneration) {
1896 			obj->cleangeneration = obj->generation;
1897 			node = VP_TO_TMPFS_NODE(vp);
1898 			node->tn_status |= TMPFS_NODE_MODIFIED |
1899 			    TMPFS_NODE_CHANGED;
1900 		}
1901 		VM_OBJECT_WUNLOCK(obj);
1902 	}
1903 }
1904 
1905 /*
1906  * Change flags of the given vnode.
1907  * Caller should execute tmpfs_update on vp after a successful execution.
1908  * The vnode must be locked on entry and remain locked on exit.
1909  */
1910 int
tmpfs_chflags(struct vnode * vp,u_long flags,struct ucred * cred,struct thread * td)1911 tmpfs_chflags(struct vnode *vp, u_long flags, struct ucred *cred,
1912     struct thread *td)
1913 {
1914 	int error;
1915 	struct tmpfs_node *node;
1916 
1917 	ASSERT_VOP_ELOCKED(vp, "chflags");
1918 
1919 	node = VP_TO_TMPFS_NODE(vp);
1920 
1921 	if ((flags & ~(SF_APPEND | SF_ARCHIVED | SF_IMMUTABLE | SF_NOUNLINK |
1922 	    UF_APPEND | UF_ARCHIVE | UF_HIDDEN | UF_IMMUTABLE | UF_NODUMP |
1923 	    UF_NOUNLINK | UF_OFFLINE | UF_OPAQUE | UF_READONLY | UF_REPARSE |
1924 	    UF_SPARSE | UF_SYSTEM)) != 0)
1925 		return (EOPNOTSUPP);
1926 
1927 	/* Disallow this operation if the file system is mounted read-only. */
1928 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
1929 		return (EROFS);
1930 
1931 	/*
1932 	 * Callers may only modify the file flags on objects they
1933 	 * have VADMIN rights for.
1934 	 */
1935 	if ((error = VOP_ACCESS(vp, VADMIN, cred, td)))
1936 		return (error);
1937 	/*
1938 	 * Unprivileged processes are not permitted to unset system
1939 	 * flags, or modify flags if any system flags are set.
1940 	 */
1941 	if (!priv_check_cred(cred, PRIV_VFS_SYSFLAGS)) {
1942 		if (node->tn_flags &
1943 		    (SF_NOUNLINK | SF_IMMUTABLE | SF_APPEND)) {
1944 			error = securelevel_gt(cred, 0);
1945 			if (error)
1946 				return (error);
1947 		}
1948 	} else {
1949 		if (node->tn_flags &
1950 		    (SF_NOUNLINK | SF_IMMUTABLE | SF_APPEND) ||
1951 		    ((flags ^ node->tn_flags) & SF_SETTABLE))
1952 			return (EPERM);
1953 	}
1954 	node->tn_flags = flags;
1955 	node->tn_status |= TMPFS_NODE_CHANGED;
1956 
1957 	ASSERT_VOP_ELOCKED(vp, "chflags2");
1958 
1959 	return (0);
1960 }
1961 
1962 /*
1963  * Change access mode on the given vnode.
1964  * Caller should execute tmpfs_update on vp after a successful execution.
1965  * The vnode must be locked on entry and remain locked on exit.
1966  */
1967 int
tmpfs_chmod(struct vnode * vp,mode_t mode,struct ucred * cred,struct thread * td)1968 tmpfs_chmod(struct vnode *vp, mode_t mode, struct ucred *cred,
1969     struct thread *td)
1970 {
1971 	int error;
1972 	struct tmpfs_node *node;
1973 	mode_t newmode;
1974 
1975 	ASSERT_VOP_ELOCKED(vp, "chmod");
1976 	ASSERT_VOP_IN_SEQC(vp);
1977 
1978 	node = VP_TO_TMPFS_NODE(vp);
1979 
1980 	/* Disallow this operation if the file system is mounted read-only. */
1981 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
1982 		return (EROFS);
1983 
1984 	/* Immutable or append-only files cannot be modified, either. */
1985 	if (node->tn_flags & (IMMUTABLE | APPEND))
1986 		return (EPERM);
1987 
1988 	/*
1989 	 * To modify the permissions on a file, must possess VADMIN
1990 	 * for that file.
1991 	 */
1992 	if ((error = VOP_ACCESS(vp, VADMIN, cred, td)))
1993 		return (error);
1994 
1995 	/*
1996 	 * Privileged processes may set the sticky bit on non-directories,
1997 	 * as well as set the setgid bit on a file with a group that the
1998 	 * process is not a member of.
1999 	 */
2000 	if (vp->v_type != VDIR && (mode & S_ISTXT)) {
2001 		if (priv_check_cred(cred, PRIV_VFS_STICKYFILE))
2002 			return (EFTYPE);
2003 	}
2004 	if (!groupmember(node->tn_gid, cred) && (mode & S_ISGID)) {
2005 		error = priv_check_cred(cred, PRIV_VFS_SETGID);
2006 		if (error)
2007 			return (error);
2008 	}
2009 
2010 	newmode = node->tn_mode & ~ALLPERMS;
2011 	newmode |= mode & ALLPERMS;
2012 	atomic_store_short(&node->tn_mode, newmode);
2013 
2014 	node->tn_status |= TMPFS_NODE_CHANGED;
2015 
2016 	ASSERT_VOP_ELOCKED(vp, "chmod2");
2017 
2018 	return (0);
2019 }
2020 
2021 /*
2022  * Change ownership of the given vnode.  At least one of uid or gid must
2023  * be different than VNOVAL.  If one is set to that value, the attribute
2024  * is unchanged.
2025  * Caller should execute tmpfs_update on vp after a successful execution.
2026  * The vnode must be locked on entry and remain locked on exit.
2027  */
2028 int
tmpfs_chown(struct vnode * vp,uid_t uid,gid_t gid,struct ucred * cred,struct thread * td)2029 tmpfs_chown(struct vnode *vp, uid_t uid, gid_t gid, struct ucred *cred,
2030     struct thread *td)
2031 {
2032 	int error;
2033 	struct tmpfs_node *node;
2034 	uid_t ouid;
2035 	gid_t ogid;
2036 	mode_t newmode;
2037 
2038 	ASSERT_VOP_ELOCKED(vp, "chown");
2039 	ASSERT_VOP_IN_SEQC(vp);
2040 
2041 	node = VP_TO_TMPFS_NODE(vp);
2042 
2043 	/* Assign default values if they are unknown. */
2044 	MPASS(uid != VNOVAL || gid != VNOVAL);
2045 	if (uid == VNOVAL)
2046 		uid = node->tn_uid;
2047 	if (gid == VNOVAL)
2048 		gid = node->tn_gid;
2049 	MPASS(uid != VNOVAL && gid != VNOVAL);
2050 
2051 	/* Disallow this operation if the file system is mounted read-only. */
2052 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
2053 		return (EROFS);
2054 
2055 	/* Immutable or append-only files cannot be modified, either. */
2056 	if (node->tn_flags & (IMMUTABLE | APPEND))
2057 		return (EPERM);
2058 
2059 	/*
2060 	 * To modify the ownership of a file, must possess VADMIN for that
2061 	 * file.
2062 	 */
2063 	if ((error = VOP_ACCESS(vp, VADMIN, cred, td)))
2064 		return (error);
2065 
2066 	/*
2067 	 * To change the owner of a file, or change the group of a file to a
2068 	 * group of which we are not a member, the caller must have
2069 	 * privilege.
2070 	 */
2071 	if ((uid != node->tn_uid ||
2072 	    (gid != node->tn_gid && !groupmember(gid, cred))) &&
2073 	    (error = priv_check_cred(cred, PRIV_VFS_CHOWN)))
2074 		return (error);
2075 
2076 	ogid = node->tn_gid;
2077 	ouid = node->tn_uid;
2078 
2079 	node->tn_uid = uid;
2080 	node->tn_gid = gid;
2081 
2082 	node->tn_status |= TMPFS_NODE_CHANGED;
2083 
2084 	if ((node->tn_mode & (S_ISUID | S_ISGID)) != 0 &&
2085 	    (ouid != uid || ogid != gid)) {
2086 		if (priv_check_cred(cred, PRIV_VFS_RETAINSUGID)) {
2087 			newmode = node->tn_mode & ~(S_ISUID | S_ISGID);
2088 			atomic_store_short(&node->tn_mode, newmode);
2089 		}
2090 	}
2091 
2092 	ASSERT_VOP_ELOCKED(vp, "chown2");
2093 
2094 	return (0);
2095 }
2096 
2097 /*
2098  * Change size of the given vnode.
2099  * Caller should execute tmpfs_update on vp after a successful execution.
2100  * The vnode must be locked on entry and remain locked on exit.
2101  */
2102 int
tmpfs_chsize(struct vnode * vp,u_quad_t size,struct ucred * cred,struct thread * td)2103 tmpfs_chsize(struct vnode *vp, u_quad_t size, struct ucred *cred,
2104     struct thread *td)
2105 {
2106 	int error;
2107 	struct tmpfs_node *node;
2108 
2109 	ASSERT_VOP_ELOCKED(vp, "chsize");
2110 
2111 	node = VP_TO_TMPFS_NODE(vp);
2112 
2113 	/* Decide whether this is a valid operation based on the file type. */
2114 	error = 0;
2115 	switch (vp->v_type) {
2116 	case VDIR:
2117 		return (EISDIR);
2118 
2119 	case VREG:
2120 		if (vp->v_mount->mnt_flag & MNT_RDONLY)
2121 			return (EROFS);
2122 		break;
2123 
2124 	case VBLK:
2125 		/* FALLTHROUGH */
2126 	case VCHR:
2127 		/* FALLTHROUGH */
2128 	case VFIFO:
2129 		/*
2130 		 * Allow modifications of special files even if in the file
2131 		 * system is mounted read-only (we are not modifying the
2132 		 * files themselves, but the objects they represent).
2133 		 */
2134 		return (0);
2135 
2136 	default:
2137 		/* Anything else is unsupported. */
2138 		return (EOPNOTSUPP);
2139 	}
2140 
2141 	/* Immutable or append-only files cannot be modified, either. */
2142 	if (node->tn_flags & (IMMUTABLE | APPEND))
2143 		return (EPERM);
2144 
2145 	error = vn_rlimit_trunc(size, td);
2146 	if (error != 0)
2147 		return (error);
2148 
2149 	error = tmpfs_truncate(vp, size);
2150 	/*
2151 	 * tmpfs_truncate will raise the NOTE_EXTEND and NOTE_ATTRIB kevents
2152 	 * for us, as will update tn_status; no need to do that here.
2153 	 */
2154 
2155 	ASSERT_VOP_ELOCKED(vp, "chsize2");
2156 
2157 	return (error);
2158 }
2159 
2160 /*
2161  * Change access and modification times of the given vnode.
2162  * Caller should execute tmpfs_update on vp after a successful execution.
2163  * The vnode must be locked on entry and remain locked on exit.
2164  */
2165 int
tmpfs_chtimes(struct vnode * vp,struct vattr * vap,struct ucred * cred,struct thread * td)2166 tmpfs_chtimes(struct vnode *vp, struct vattr *vap,
2167     struct ucred *cred, struct thread *td)
2168 {
2169 	int error;
2170 	struct tmpfs_node *node;
2171 
2172 	ASSERT_VOP_ELOCKED(vp, "chtimes");
2173 
2174 	node = VP_TO_TMPFS_NODE(vp);
2175 
2176 	/* Disallow this operation if the file system is mounted read-only. */
2177 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
2178 		return (EROFS);
2179 
2180 	/* Immutable or append-only files cannot be modified, either. */
2181 	if (node->tn_flags & (IMMUTABLE | APPEND))
2182 		return (EPERM);
2183 
2184 	error = vn_utimes_perm(vp, vap, cred, td);
2185 	if (error != 0)
2186 		return (error);
2187 
2188 	if (vap->va_atime.tv_sec != VNOVAL)
2189 		node->tn_accessed = true;
2190 	if (vap->va_mtime.tv_sec != VNOVAL)
2191 		node->tn_status |= TMPFS_NODE_MODIFIED;
2192 	if (vap->va_birthtime.tv_sec != VNOVAL)
2193 		node->tn_status |= TMPFS_NODE_MODIFIED;
2194 	tmpfs_itimes(vp, &vap->va_atime, &vap->va_mtime);
2195 	if (vap->va_birthtime.tv_sec != VNOVAL)
2196 		node->tn_birthtime = vap->va_birthtime;
2197 	ASSERT_VOP_ELOCKED(vp, "chtimes2");
2198 
2199 	return (0);
2200 }
2201 
2202 void
tmpfs_set_status(struct tmpfs_mount * tm,struct tmpfs_node * node,int status)2203 tmpfs_set_status(struct tmpfs_mount *tm, struct tmpfs_node *node, int status)
2204 {
2205 
2206 	if ((node->tn_status & status) == status || tm->tm_ronly)
2207 		return;
2208 	TMPFS_NODE_LOCK(node);
2209 	node->tn_status |= status;
2210 	TMPFS_NODE_UNLOCK(node);
2211 }
2212 
2213 void
tmpfs_set_accessed(struct tmpfs_mount * tm,struct tmpfs_node * node)2214 tmpfs_set_accessed(struct tmpfs_mount *tm, struct tmpfs_node *node)
2215 {
2216 	if (node->tn_accessed || tm->tm_ronly)
2217 		return;
2218 	atomic_store_8(&node->tn_accessed, true);
2219 }
2220 
2221 /* Sync timestamps */
2222 void
tmpfs_itimes(struct vnode * vp,const struct timespec * acc,const struct timespec * mod)2223 tmpfs_itimes(struct vnode *vp, const struct timespec *acc,
2224     const struct timespec *mod)
2225 {
2226 	struct tmpfs_node *node;
2227 	struct timespec now;
2228 
2229 	ASSERT_VOP_LOCKED(vp, "tmpfs_itimes");
2230 	node = VP_TO_TMPFS_NODE(vp);
2231 
2232 	if (!node->tn_accessed &&
2233 	    (node->tn_status & (TMPFS_NODE_MODIFIED | TMPFS_NODE_CHANGED)) == 0)
2234 		return;
2235 
2236 	vfs_timestamp(&now);
2237 	TMPFS_NODE_LOCK(node);
2238 	if (node->tn_accessed) {
2239 		if (acc == NULL)
2240 			 acc = &now;
2241 		node->tn_atime = *acc;
2242 	}
2243 	if (node->tn_status & TMPFS_NODE_MODIFIED) {
2244 		if (mod == NULL)
2245 			mod = &now;
2246 		node->tn_mtime = *mod;
2247 	}
2248 	if (node->tn_status & TMPFS_NODE_CHANGED)
2249 		node->tn_ctime = now;
2250 	node->tn_status &= ~(TMPFS_NODE_MODIFIED | TMPFS_NODE_CHANGED);
2251 	node->tn_accessed = false;
2252 	TMPFS_NODE_UNLOCK(node);
2253 
2254 	/* XXX: FIX? The entropy here is desirable, but the harvesting may be expensive */
2255 	random_harvest_queue(node, sizeof(*node), RANDOM_FS_ATIME);
2256 }
2257 
2258 int
tmpfs_truncate(struct vnode * vp,off_t length)2259 tmpfs_truncate(struct vnode *vp, off_t length)
2260 {
2261 	struct tmpfs_node *node;
2262 	int error;
2263 
2264 	if (length < 0)
2265 		return (EINVAL);
2266 	if (length > VFS_TO_TMPFS(vp->v_mount)->tm_maxfilesize)
2267 		return (EFBIG);
2268 
2269 	node = VP_TO_TMPFS_NODE(vp);
2270 	error = node->tn_size == length ? 0 : tmpfs_reg_resize(vp, length,
2271 	    FALSE);
2272 	if (error == 0)
2273 		node->tn_status |= TMPFS_NODE_CHANGED | TMPFS_NODE_MODIFIED;
2274 	tmpfs_update(vp);
2275 
2276 	return (error);
2277 }
2278 
2279 static __inline int
tmpfs_dirtree_cmp(struct tmpfs_dirent * a,struct tmpfs_dirent * b)2280 tmpfs_dirtree_cmp(struct tmpfs_dirent *a, struct tmpfs_dirent *b)
2281 {
2282 	if (a->td_hash > b->td_hash)
2283 		return (1);
2284 	else if (a->td_hash < b->td_hash)
2285 		return (-1);
2286 	return (0);
2287 }
2288 
2289 RB_GENERATE_STATIC(tmpfs_dir, tmpfs_dirent, uh.td_entries, tmpfs_dirtree_cmp);
2290