xref: /illumos-gate/usr/src/uts/common/fs/zfs/unique.c (revision 1a2d662a91cee3bf82f41cd47c7ae6f3825d9db2)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #include <sys/zfs_context.h>
27 #include <sys/avl.h>
28 #include <sys/unique.h>
29 
30 static avl_tree_t unique_avl;
31 static kmutex_t unique_mtx;
32 
33 typedef struct unique {
34 	avl_node_t un_link;
35 	uint64_t un_value;
36 } unique_t;
37 
38 #define	UNIQUE_MASK ((1ULL << UNIQUE_BITS) - 1)
39 
40 static int
41 unique_compare(const void *a, const void *b)
42 {
43 	const unique_t *una = (const unique_t *)a;
44 	const unique_t *unb = (const unique_t *)b;
45 
46 	return (TREE_CMP(una->un_value, unb->un_value));
47 }
48 
49 void
50 unique_init(void)
51 {
52 	avl_create(&unique_avl, unique_compare,
53 	    sizeof (unique_t), offsetof(unique_t, un_link));
54 	mutex_init(&unique_mtx, NULL, MUTEX_DEFAULT, NULL);
55 }
56 
57 void
58 unique_fini(void)
59 {
60 	avl_destroy(&unique_avl);
61 	mutex_destroy(&unique_mtx);
62 }
63 
64 uint64_t
65 unique_create(void)
66 {
67 	uint64_t value = unique_insert(0);
68 	unique_remove(value);
69 	return (value);
70 }
71 
72 uint64_t
73 unique_insert(uint64_t value)
74 {
75 	avl_index_t idx;
76 	unique_t *un = kmem_alloc(sizeof (unique_t), KM_SLEEP);
77 
78 	un->un_value = value;
79 
80 	mutex_enter(&unique_mtx);
81 	while (un->un_value == 0 || un->un_value & ~UNIQUE_MASK ||
82 	    avl_find(&unique_avl, un, &idx)) {
83 		mutex_exit(&unique_mtx);
84 		(void) random_get_pseudo_bytes((void*)&un->un_value,
85 		    sizeof (un->un_value));
86 		un->un_value &= UNIQUE_MASK;
87 		mutex_enter(&unique_mtx);
88 	}
89 
90 	avl_insert(&unique_avl, un, idx);
91 	mutex_exit(&unique_mtx);
92 
93 	return (un->un_value);
94 }
95 
96 void
97 unique_remove(uint64_t value)
98 {
99 	unique_t un_tofind;
100 	unique_t *un;
101 
102 	un_tofind.un_value = value;
103 	mutex_enter(&unique_mtx);
104 	un = avl_find(&unique_avl, &un_tofind, NULL);
105 	if (un != NULL) {
106 		avl_remove(&unique_avl, un);
107 		kmem_free(un, sizeof (unique_t));
108 	}
109 	mutex_exit(&unique_mtx);
110 }
111