xref: /freebsd/sys/contrib/openzfs/module/zfs/objlist.c (revision 61145dc2b94f12f6a47344fb9aac702321880e43)
1*61145dc2SMartin Matuska // SPDX-License-Identifier: CDDL-1.0
2eda14cbcSMatt Macy /*
3eda14cbcSMatt Macy  * CDDL HEADER START
4eda14cbcSMatt Macy  *
5eda14cbcSMatt Macy  * This file and its contents are supplied under the terms of the
6eda14cbcSMatt Macy  * Common Development and Distribution License ("CDDL"), version 1.0.
7eda14cbcSMatt Macy  * You may only use this file in accordance with the terms of version
8eda14cbcSMatt Macy  * 1.0 of the CDDL.
9eda14cbcSMatt Macy  *
10eda14cbcSMatt Macy  * A full copy of the text of the CDDL should have accompanied this
11eda14cbcSMatt Macy  * source.  A copy of the CDDL is also available via the Internet at
12eda14cbcSMatt Macy  * http://www.illumos.org/license/CDDL.
13eda14cbcSMatt Macy  *
14eda14cbcSMatt Macy  * CDDL HEADER END
15eda14cbcSMatt Macy  */
16eda14cbcSMatt Macy /*
17eda14cbcSMatt Macy  * Copyright (c) 2018 by Delphix. All rights reserved.
18eda14cbcSMatt Macy  */
19eda14cbcSMatt Macy 
20eda14cbcSMatt Macy #include	<sys/objlist.h>
21eda14cbcSMatt Macy #include	<sys/zfs_context.h>
22eda14cbcSMatt Macy 
23eda14cbcSMatt Macy objlist_t *
objlist_create(void)24eda14cbcSMatt Macy objlist_create(void)
25eda14cbcSMatt Macy {
26eda14cbcSMatt Macy 	objlist_t *list = kmem_alloc(sizeof (*list), KM_SLEEP);
27eda14cbcSMatt Macy 	list_create(&list->ol_list, sizeof (objlist_node_t),
28eda14cbcSMatt Macy 	    offsetof(objlist_node_t, on_node));
29eda14cbcSMatt Macy 	list->ol_last_lookup = 0;
30eda14cbcSMatt Macy 	return (list);
31eda14cbcSMatt Macy }
32eda14cbcSMatt Macy 
33eda14cbcSMatt Macy void
objlist_destroy(objlist_t * list)34eda14cbcSMatt Macy objlist_destroy(objlist_t *list)
35eda14cbcSMatt Macy {
36eda14cbcSMatt Macy 	for (objlist_node_t *n = list_remove_head(&list->ol_list);
37eda14cbcSMatt Macy 	    n != NULL; n = list_remove_head(&list->ol_list)) {
38eda14cbcSMatt Macy 		kmem_free(n, sizeof (*n));
39eda14cbcSMatt Macy 	}
40eda14cbcSMatt Macy 	list_destroy(&list->ol_list);
41eda14cbcSMatt Macy 	kmem_free(list, sizeof (*list));
42eda14cbcSMatt Macy }
43eda14cbcSMatt Macy 
44eda14cbcSMatt Macy /*
45eda14cbcSMatt Macy  * This function looks through the objlist to see if the specified object number
46eda14cbcSMatt Macy  * is contained in the objlist.  In the process, it will remove all object
47eda14cbcSMatt Macy  * numbers in the list that are smaller than the specified object number.  Thus,
48eda14cbcSMatt Macy  * any lookup of an object number smaller than a previously looked up object
49eda14cbcSMatt Macy  * number will always return false; therefore, all lookups should be done in
50eda14cbcSMatt Macy  * ascending order.
51eda14cbcSMatt Macy  */
52eda14cbcSMatt Macy boolean_t
objlist_exists(objlist_t * list,uint64_t object)53eda14cbcSMatt Macy objlist_exists(objlist_t *list, uint64_t object)
54eda14cbcSMatt Macy {
55eda14cbcSMatt Macy 	objlist_node_t *node = list_head(&list->ol_list);
56eda14cbcSMatt Macy 	ASSERT3U(object, >=, list->ol_last_lookup);
57eda14cbcSMatt Macy 	list->ol_last_lookup = object;
58eda14cbcSMatt Macy 	while (node != NULL && node->on_object < object) {
59eda14cbcSMatt Macy 		VERIFY3P(node, ==, list_remove_head(&list->ol_list));
60eda14cbcSMatt Macy 		kmem_free(node, sizeof (*node));
61eda14cbcSMatt Macy 		node = list_head(&list->ol_list);
62eda14cbcSMatt Macy 	}
63eda14cbcSMatt Macy 	return (node != NULL && node->on_object == object);
64eda14cbcSMatt Macy }
65eda14cbcSMatt Macy 
66eda14cbcSMatt Macy /*
67eda14cbcSMatt Macy  * The objlist is a list of object numbers stored in ascending order.  However,
68eda14cbcSMatt Macy  * the insertion of new object numbers does not seek out the correct location to
69eda14cbcSMatt Macy  * store a new object number; instead, it appends it to the list for simplicity.
70eda14cbcSMatt Macy  * Thus, any users must take care to only insert new object numbers in ascending
71eda14cbcSMatt Macy  * order.
72eda14cbcSMatt Macy  */
73eda14cbcSMatt Macy void
objlist_insert(objlist_t * list,uint64_t object)74eda14cbcSMatt Macy objlist_insert(objlist_t *list, uint64_t object)
75eda14cbcSMatt Macy {
76eda14cbcSMatt Macy 	objlist_node_t *node = kmem_zalloc(sizeof (*node), KM_SLEEP);
77eda14cbcSMatt Macy 	node->on_object = object;
78eda14cbcSMatt Macy #ifdef ZFS_DEBUG
79eda14cbcSMatt Macy 	objlist_node_t *last_object = list_tail(&list->ol_list);
80eda14cbcSMatt Macy 	uint64_t last_objnum = (last_object != NULL ? last_object->on_object :
81eda14cbcSMatt Macy 	    0);
82eda14cbcSMatt Macy 	ASSERT3U(node->on_object, >, last_objnum);
83eda14cbcSMatt Macy #endif
84eda14cbcSMatt Macy 	list_insert_tail(&list->ol_list, node);
85eda14cbcSMatt Macy }
86