blob: 70ce40914c8a88d157ed5211b8cef2d6ca738245 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scott7b718762005-11-02 14:58:39 +11002 * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <xfs.h>
19
20static mutex_t uuid_monitor;
21static int uuid_table_size;
22static uuid_t *uuid_table;
23
24void
25uuid_init(void)
26{
27 mutex_init(&uuid_monitor, MUTEX_DEFAULT, "uuid_monitor");
28}
29
30/*
31 * uuid_getnodeuniq - obtain the node unique fields of a UUID.
32 *
33 * This is not in any way a standard or condoned UUID function;
34 * it just something that's needed for user-level file handles.
35 */
36void
37uuid_getnodeuniq(uuid_t *uuid, int fsid [2])
38{
39 char *uu = (char *)uuid;
40
41 /* on IRIX, this function assumes big-endian fields within
42 * the uuid, so we use INT_GET to get the same result on
43 * little-endian systems
44 */
45
46 fsid[0] = (INT_GET(*(u_int16_t*)(uu+8), ARCH_CONVERT) << 16) +
47 INT_GET(*(u_int16_t*)(uu+4), ARCH_CONVERT);
48 fsid[1] = INT_GET(*(u_int32_t*)(uu ), ARCH_CONVERT);
49}
50
51void
52uuid_create_nil(uuid_t *uuid)
53{
54 memset(uuid, 0, sizeof(*uuid));
55}
56
57int
58uuid_is_nil(uuid_t *uuid)
59{
60 int i;
61 char *cp = (char *)uuid;
62
63 if (uuid == NULL)
64 return 0;
65 /* implied check of version number here... */
66 for (i = 0; i < sizeof *uuid; i++)
67 if (*cp++) return 0; /* not nil */
68 return 1; /* is nil */
69}
70
71int
72uuid_equal(uuid_t *uuid1, uuid_t *uuid2)
73{
74 return memcmp(uuid1, uuid2, sizeof(uuid_t)) ? 0 : 1;
75}
76
77/*
78 * Given a 128-bit uuid, return a 64-bit value by adding the top and bottom
79 * 64-bit words. NOTE: This function can not be changed EVER. Although
80 * brain-dead, some applications depend on this 64-bit value remaining
81 * persistent. Specifically, DMI vendors store the value as a persistent
82 * filehandle.
83 */
84__uint64_t
85uuid_hash64(uuid_t *uuid)
86{
87 __uint64_t *sp = (__uint64_t *)uuid;
88
89 return sp[0] + sp[1];
90}
91
92int
93uuid_table_insert(uuid_t *uuid)
94{
95 int i, hole;
96
97 mutex_lock(&uuid_monitor, PVFS);
98 for (i = 0, hole = -1; i < uuid_table_size; i++) {
99 if (uuid_is_nil(&uuid_table[i])) {
100 hole = i;
101 continue;
102 }
103 if (uuid_equal(uuid, &uuid_table[i])) {
104 mutex_unlock(&uuid_monitor);
105 return 0;
106 }
107 }
108 if (hole < 0) {
109 uuid_table = kmem_realloc(uuid_table,
110 (uuid_table_size + 1) * sizeof(*uuid_table),
111 uuid_table_size * sizeof(*uuid_table),
112 KM_SLEEP);
113 hole = uuid_table_size++;
114 }
115 uuid_table[hole] = *uuid;
116 mutex_unlock(&uuid_monitor);
117 return 1;
118}
119
120void
121uuid_table_remove(uuid_t *uuid)
122{
123 int i;
124
125 mutex_lock(&uuid_monitor, PVFS);
126 for (i = 0; i < uuid_table_size; i++) {
127 if (uuid_is_nil(&uuid_table[i]))
128 continue;
129 if (!uuid_equal(uuid, &uuid_table[i]))
130 continue;
131 uuid_create_nil(&uuid_table[i]);
132 break;
133 }
134 ASSERT(i < uuid_table_size);
135 mutex_unlock(&uuid_monitor);
136}