xref: /onnv-gate/usr/src/uts/common/fs/zfs/unique.c (revision 2856:6f4d5ee1906a)
1789Sahrens /*
2789Sahrens  * CDDL HEADER START
3789Sahrens  *
4789Sahrens  * The contents of this file are subject to the terms of the
5*2856Snd150628  * Common Development and Distribution License (the "License").
6*2856Snd150628  * You may not use this file except in compliance with the License.
7789Sahrens  *
8789Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9789Sahrens  * or http://www.opensolaris.org/os/licensing.
10789Sahrens  * See the License for the specific language governing permissions
11789Sahrens  * and limitations under the License.
12789Sahrens  *
13789Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14789Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15789Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16789Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17789Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18789Sahrens  *
19789Sahrens  * CDDL HEADER END
20789Sahrens  */
21789Sahrens /*
22*2856Snd150628  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23789Sahrens  * Use is subject to license terms.
24789Sahrens  */
25789Sahrens 
26789Sahrens #pragma ident	"%Z%%M%	%I%	%E% SMI"
27789Sahrens 
28789Sahrens #include <sys/zfs_context.h>
29789Sahrens #include <sys/avl.h>
30789Sahrens #include <sys/unique.h>
31789Sahrens 
32789Sahrens static avl_tree_t unique_avl;
33*2856Snd150628 static kmutex_t unique_mtx;	/* Lock never initialized. */
34789Sahrens 
35789Sahrens typedef struct unique {
36789Sahrens 	avl_node_t un_link;
37789Sahrens 	uint64_t un_value;
38789Sahrens } unique_t;
39789Sahrens 
40789Sahrens #define	UNIQUE_MASK ((1ULL << UNIQUE_BITS) - 1)
41789Sahrens 
42789Sahrens static int
43789Sahrens unique_compare(const void *a, const void *b)
44789Sahrens {
45789Sahrens 	const unique_t *una = a;
46789Sahrens 	const unique_t *unb = b;
47789Sahrens 
48789Sahrens 	if (una->un_value < unb->un_value)
49789Sahrens 		return (-1);
50789Sahrens 	if (una->un_value > unb->un_value)
51789Sahrens 		return (+1);
52789Sahrens 	return (0);
53789Sahrens }
54789Sahrens 
55789Sahrens void
56789Sahrens unique_init(void)
57789Sahrens {
58789Sahrens 	avl_create(&unique_avl, unique_compare,
59789Sahrens 	    sizeof (unique_t), offsetof(unique_t, un_link));
60789Sahrens }
61789Sahrens 
62789Sahrens uint64_t
63789Sahrens unique_create(void)
64789Sahrens {
65789Sahrens 	return (unique_insert(0));
66789Sahrens }
67789Sahrens 
68789Sahrens uint64_t
69789Sahrens unique_insert(uint64_t value)
70789Sahrens {
71789Sahrens 	avl_index_t idx;
72789Sahrens 	unique_t *un = kmem_alloc(sizeof (unique_t), KM_SLEEP);
73789Sahrens 
74789Sahrens 	un->un_value = value;
75789Sahrens 
76789Sahrens 	mutex_enter(&unique_mtx);
77789Sahrens 	while (un->un_value == 0 || un->un_value & ~UNIQUE_MASK ||
78789Sahrens 	    avl_find(&unique_avl, un, &idx)) {
79789Sahrens 		mutex_exit(&unique_mtx);
80789Sahrens 		(void) random_get_pseudo_bytes((void*)&un->un_value,
81789Sahrens 		    sizeof (un->un_value));
82789Sahrens 		un->un_value &= UNIQUE_MASK;
83789Sahrens 		mutex_enter(&unique_mtx);
84789Sahrens 	}
85789Sahrens 
86789Sahrens 	avl_insert(&unique_avl, un, idx);
87789Sahrens 	mutex_exit(&unique_mtx);
88789Sahrens 
89789Sahrens 	return (un->un_value);
90789Sahrens }
91789Sahrens 
92789Sahrens void
93789Sahrens unique_remove(uint64_t value)
94789Sahrens {
95789Sahrens 	unique_t un_tofind;
96789Sahrens 	unique_t *un;
97789Sahrens 
98789Sahrens 	un_tofind.un_value = value;
99789Sahrens 	mutex_enter(&unique_mtx);
100789Sahrens 	un = avl_find(&unique_avl, &un_tofind, NULL);
101789Sahrens 	if (un != NULL) {
102789Sahrens 		avl_remove(&unique_avl, un);
103789Sahrens 		kmem_free(un, sizeof (unique_t));
104789Sahrens 	}
105789Sahrens 	mutex_exit(&unique_mtx);
106789Sahrens }
107