Print this page
cstyle fixes
dsl_dataset_set_fsid_guid should use ZFS_SPACE_CHECK_RESERVED
dsl_dataset_set_fsid_guid _check and _sync func declared static,
removed from dsl_dataset.h
rewrite unique_valid
6333 ZFS should let the user specify or modify the fsid_guid of a dataset
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/uts/common/fs/zfs/unique.c
+++ new/usr/src/uts/common/fs/zfs/unique.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21 /*
22 22 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
23 23 * Use is subject to license terms.
24 24 */
25 25
26 26 #pragma ident "%Z%%M% %I% %E% SMI"
27 27
28 28 #include <sys/zfs_context.h>
29 29 #include <sys/avl.h>
30 30 #include <sys/unique.h>
31 31
32 32 static avl_tree_t unique_avl;
33 33 static kmutex_t unique_mtx;
34 34
35 35 typedef struct unique {
36 36 avl_node_t un_link;
37 37 uint64_t un_value;
38 38 } unique_t;
39 39
40 40 #define UNIQUE_MASK ((1ULL << UNIQUE_BITS) - 1)
41 41
42 42 static int
43 43 unique_compare(const void *a, const void *b)
44 44 {
45 45 const unique_t *una = a;
46 46 const unique_t *unb = b;
47 47
48 48 if (una->un_value < unb->un_value)
49 49 return (-1);
50 50 if (una->un_value > unb->un_value)
51 51 return (+1);
52 52 return (0);
53 53 }
54 54
55 55 void
56 56 unique_init(void)
57 57 {
58 58 avl_create(&unique_avl, unique_compare,
59 59 sizeof (unique_t), offsetof(unique_t, un_link));
60 60 mutex_init(&unique_mtx, NULL, MUTEX_DEFAULT, NULL);
61 61 }
62 62
63 63 void
64 64 unique_fini(void)
65 65 {
66 66 avl_destroy(&unique_avl);
67 67 mutex_destroy(&unique_mtx);
68 68 }
69 69
70 70 uint64_t
71 71 unique_create(void)
72 72 {
73 73 uint64_t value = unique_insert(0);
74 74 unique_remove(value);
75 75 return (value);
76 76 }
77 77
78 78 uint64_t
79 79 unique_insert(uint64_t value)
80 80 {
81 81 avl_index_t idx;
82 82 unique_t *un = kmem_alloc(sizeof (unique_t), KM_SLEEP);
83 83
84 84 un->un_value = value;
85 85
86 86 mutex_enter(&unique_mtx);
87 87 while (un->un_value == 0 || un->un_value & ~UNIQUE_MASK ||
88 88 avl_find(&unique_avl, un, &idx)) {
89 89 mutex_exit(&unique_mtx);
90 90 (void) random_get_pseudo_bytes((void*)&un->un_value,
91 91 sizeof (un->un_value));
↓ open down ↓ |
91 lines elided |
↑ open up ↑ |
92 92 un->un_value &= UNIQUE_MASK;
93 93 mutex_enter(&unique_mtx);
94 94 }
95 95
96 96 avl_insert(&unique_avl, un, idx);
97 97 mutex_exit(&unique_mtx);
98 98
99 99 return (un->un_value);
100 100 }
101 101
102 +boolean_t
103 +unique_valid(uint64_t value)
104 +{
105 + unique_t un_tofind;
106 +
107 + un_tofind.un_value = value;
108 +
109 + mutex_enter(&unique_mtx);
110 + boolean_t rv = ((value & ~UNIQUE_MASK) == 0 &&
111 + avl_find(&unique_avl, &un_tofind, NULL) == NULL);
112 + mutex_exit(&unique_mtx);
113 +
114 + return (rv);
115 +}
116 +
102 117 void
103 118 unique_remove(uint64_t value)
104 119 {
105 120 unique_t un_tofind;
106 121 unique_t *un;
107 122
108 123 un_tofind.un_value = value;
109 124 mutex_enter(&unique_mtx);
110 125 un = avl_find(&unique_avl, &un_tofind, NULL);
111 126 if (un != NULL) {
112 127 avl_remove(&unique_avl, un);
113 128 kmem_free(un, sizeof (unique_t));
114 129 }
115 130 mutex_exit(&unique_mtx);
116 131 }
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX