1 /*
2 * Copyright (c) 2000-2003 Silicon Graphics, Inc. All Rights Reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11 *
12 * Further, this software is distributed without any warranty that it is
13 * free of the rightful claim of any third person regarding infringement
14 * or the like. Any license provided herein, whether implied or
15 * otherwise, applies only to this software file. Patent licenses, if
16 * any, provided herein do not apply to combinations of this program with
17 * other software, or any other product whatsoever.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write the Free Software Foundation, Inc., 59
21 * Temple Place - Suite 330, Boston MA 02111-1307, USA.
22 *
23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
25 *
26 * http://www.sgi.com
27 *
28 * For further information regarding this notice, see:
29 *
30 * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
31 */
32
33 #include <xfs_config.h>
34 #include <xfs_compat.h>
35 #include <xfs_types.h>
36 #include <xfs_arch.h>
37
38 #include <support/uuid.h>
39 #include <support/kmem.h>
40 #include <support/debug.h>
41 #include <support/mutex.h>
42
43 static mutex_t uuid_monitor;
44 static int uuid_table_size;
45 static uuid_t *uuid_table;
46
47 void
uuid_init(void)48 uuid_init(void)
49 {
50 mutex_init(&uuid_monitor, MUTEX_DEFAULT, "uuid_monitor");
51 }
52
53 void
uuid_cleanup(void)54 uuid_cleanup(void)
55 {
56 mutex_destroy(&uuid_monitor);
57 }
58
59 /*
60 * uuid_getnodeuniq - obtain the node unique fields of a UUID.
61 *
62 * This is not in any way a standard or condoned UUID function;
63 * it just something that's needed for user-level file handles.
64 */
65 void
uuid_getnodeuniq(uuid_t * uuid,int fsid[2])66 uuid_getnodeuniq(uuid_t *uuid, int fsid [2])
67 {
68 char *uu = (char *)uuid;
69
70 /* on IRIX, this function assumes big-endian fields within
71 * the uuid, so we use INT_GET to get the same result on
72 * little-endian systems
73 */
74
75 fsid[0] = (INT_GET(*(u_int16_t*)(uu+8), ARCH_CONVERT) << 16) +
76 INT_GET(*(u_int16_t*)(uu+4), ARCH_CONVERT);
77 fsid[1] = INT_GET(*(u_int32_t*)(uu ), ARCH_CONVERT);
78 }
79
80 void
uuid_create_nil(uuid_t * uuid)81 uuid_create_nil(uuid_t *uuid)
82 {
83 memset(uuid, 0, sizeof(*uuid));
84 }
85
86 int
uuid_is_nil(uuid_t * uuid)87 uuid_is_nil(uuid_t *uuid)
88 {
89 int i;
90 char *cp = (char *)uuid;
91
92 if (uuid == NULL)
93 return 0;
94 /* implied check of version number here... */
95 for (i = 0; i < sizeof *uuid; i++)
96 if (*cp++) return 0; /* not nil */
97 return 1; /* is nil */
98 }
99
100 int
uuid_equal(uuid_t * uuid1,uuid_t * uuid2)101 uuid_equal(uuid_t *uuid1, uuid_t *uuid2)
102 {
103 return memcmp(uuid1, uuid2, sizeof(uuid_t)) ? 0 : 1;
104 }
105
106 /*
107 * Given a 128-bit uuid, return a 64-bit value by adding the top and bottom
108 * 64-bit words. NOTE: This function can not be changed EVER. Although
109 * brain-dead, some applications depend on this 64-bit value remaining
110 * persistent. Specifically, DMI vendors store the value as a persistent
111 * filehandle.
112 */
113 __uint64_t
uuid_hash64(uuid_t * uuid)114 uuid_hash64(uuid_t *uuid)
115 {
116 __uint64_t *sp = (__uint64_t *)uuid;
117
118 return sp[0] + sp[1];
119 }
120
121 int
uuid_table_insert(uuid_t * uuid)122 uuid_table_insert(uuid_t *uuid)
123 {
124 int i, hole;
125
126 mutex_lock(&uuid_monitor, PVFS);
127 for (i = 0, hole = -1; i < uuid_table_size; i++) {
128 if (uuid_is_nil(&uuid_table[i])) {
129 hole = i;
130 continue;
131 }
132 if (uuid_equal(uuid, &uuid_table[i])) {
133 mutex_unlock(&uuid_monitor);
134 return 0;
135 }
136 }
137 if (hole < 0) {
138 uuid_table = kmem_realloc(uuid_table,
139 (uuid_table_size + 1) * sizeof(*uuid_table),
140 uuid_table_size * sizeof(*uuid_table),
141 KM_SLEEP);
142 hole = uuid_table_size++;
143 }
144 uuid_table[hole] = *uuid;
145 mutex_unlock(&uuid_monitor);
146 return 1;
147 }
148
149 void
uuid_table_remove(uuid_t * uuid)150 uuid_table_remove(uuid_t *uuid)
151 {
152 int i;
153
154 mutex_lock(&uuid_monitor, PVFS);
155 for (i = 0; i < uuid_table_size; i++) {
156 if (uuid_is_nil(&uuid_table[i]))
157 continue;
158 if (!uuid_equal(uuid, &uuid_table[i]))
159 continue;
160 uuid_create_nil(&uuid_table[i]);
161 break;
162 }
163 ASSERT(i < uuid_table_size);
164 mutex_unlock(&uuid_monitor);
165 }
166