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