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.h>
34
35 static mutex_t uuid_monitor;
36 static int uuid_table_size;
37 static uuid_t *uuid_table;
38
39 void
uuid_init(void)40 uuid_init(void)
41 {
42 mutex_init(&uuid_monitor, MUTEX_DEFAULT, "uuid_monitor");
43 }
44
45 /*
46 * uuid_getnodeuniq - obtain the node unique fields of a UUID.
47 *
48 * This is not in any way a standard or condoned UUID function;
49 * it just something that's needed for user-level file handles.
50 */
51 void
uuid_getnodeuniq(uuid_t * uuid,int fsid[2])52 uuid_getnodeuniq(uuid_t *uuid, int fsid [2])
53 {
54 char *uu = (char *)uuid;
55
56 /* on IRIX, this function assumes big-endian fields within
57 * the uuid, so we use INT_GET to get the same result on
58 * little-endian systems
59 */
60
61 fsid[0] = (INT_GET(*(u_int16_t*)(uu+8), ARCH_CONVERT) << 16) +
62 INT_GET(*(u_int16_t*)(uu+4), ARCH_CONVERT);
63 fsid[1] = INT_GET(*(u_int32_t*)(uu ), ARCH_CONVERT);
64 }
65
66 void
uuid_create_nil(uuid_t * uuid)67 uuid_create_nil(uuid_t *uuid)
68 {
69 memset(uuid, 0, sizeof(*uuid));
70 }
71
72 int
uuid_is_nil(uuid_t * uuid)73 uuid_is_nil(uuid_t *uuid)
74 {
75 int i;
76 char *cp = (char *)uuid;
77
78 if (uuid == NULL)
79 return 0;
80 /* implied check of version number here... */
81 for (i = 0; i < sizeof *uuid; i++)
82 if (*cp++) return 0; /* not nil */
83 return 1; /* is nil */
84 }
85
86 int
uuid_equal(uuid_t * uuid1,uuid_t * uuid2)87 uuid_equal(uuid_t *uuid1, uuid_t *uuid2)
88 {
89 return memcmp(uuid1, uuid2, sizeof(uuid_t)) ? 0 : 1;
90 }
91
92 /*
93 * Given a 128-bit uuid, return a 64-bit value by adding the top and bottom
94 * 64-bit words. NOTE: This function can not be changed EVER. Although
95 * brain-dead, some applications depend on this 64-bit value remaining
96 * persistent. Specifically, DMI vendors store the value as a persistent
97 * filehandle.
98 */
99 __uint64_t
uuid_hash64(uuid_t * uuid)100 uuid_hash64(uuid_t *uuid)
101 {
102 __uint64_t *sp = (__uint64_t *)uuid;
103
104 return sp[0] + sp[1];
105 }
106
107 int
uuid_table_insert(uuid_t * uuid)108 uuid_table_insert(uuid_t *uuid)
109 {
110 int i, hole;
111
112 mutex_lock(&uuid_monitor, PVFS);
113 for (i = 0, hole = -1; i < uuid_table_size; i++) {
114 if (uuid_is_nil(&uuid_table[i])) {
115 hole = i;
116 continue;
117 }
118 if (uuid_equal(uuid, &uuid_table[i])) {
119 mutex_unlock(&uuid_monitor);
120 return 0;
121 }
122 }
123 if (hole < 0) {
124 uuid_table = kmem_realloc(uuid_table,
125 (uuid_table_size + 1) * sizeof(*uuid_table),
126 uuid_table_size * sizeof(*uuid_table),
127 KM_SLEEP);
128 hole = uuid_table_size++;
129 }
130 uuid_table[hole] = *uuid;
131 mutex_unlock(&uuid_monitor);
132 return 1;
133 }
134
135 void
uuid_table_remove(uuid_t * uuid)136 uuid_table_remove(uuid_t *uuid)
137 {
138 int i;
139
140 mutex_lock(&uuid_monitor, PVFS);
141 for (i = 0; i < uuid_table_size; i++) {
142 if (uuid_is_nil(&uuid_table[i]))
143 continue;
144 if (!uuid_equal(uuid, &uuid_table[i]))
145 continue;
146 uuid_create_nil(&uuid_table[i]);
147 break;
148 }
149 ASSERT(i < uuid_table_size);
150 mutex_unlock(&uuid_monitor);
151 }
152