blob: e157015c70ffcad4ed7ee0e989e4714fb55db526 [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
Christoph Hellwig794fe2d2006-01-11 15:37:17 +110024/* IRIX interpretation of an uuid_t */
25typedef struct {
26 __be32 uu_timelow;
27 __be16 uu_timemid;
28 __be16 uu_timehi;
29 __be16 uu_clockseq;
30 __be16 uu_node[3];
31} xfs_uu_t;
32
Linus Torvalds1da177e2005-04-16 15:20:36 -070033/*
34 * uuid_getnodeuniq - obtain the node unique fields of a UUID.
35 *
36 * This is not in any way a standard or condoned UUID function;
37 * it just something that's needed for user-level file handles.
38 */
39void
40uuid_getnodeuniq(uuid_t *uuid, int fsid [2])
41{
Christoph Hellwig794fe2d2006-01-11 15:37:17 +110042 xfs_uu_t *uup = (xfs_uu_t *)uuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Christoph Hellwig794fe2d2006-01-11 15:37:17 +110044 fsid[0] = (be16_to_cpu(uup->uu_clockseq) << 16) |
45 be16_to_cpu(uup->uu_timemid);
Nathan Scotta7801432006-03-14 13:24:46 +110046 fsid[1] = be32_to_cpu(uup->uu_timelow);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047}
48
49void
50uuid_create_nil(uuid_t *uuid)
51{
52 memset(uuid, 0, sizeof(*uuid));
53}
54
55int
56uuid_is_nil(uuid_t *uuid)
57{
58 int i;
59 char *cp = (char *)uuid;
60
61 if (uuid == NULL)
62 return 0;
63 /* implied check of version number here... */
64 for (i = 0; i < sizeof *uuid; i++)
65 if (*cp++) return 0; /* not nil */
66 return 1; /* is nil */
67}
68
69int
70uuid_equal(uuid_t *uuid1, uuid_t *uuid2)
71{
72 return memcmp(uuid1, uuid2, sizeof(uuid_t)) ? 0 : 1;
73}
74
75/*
76 * Given a 128-bit uuid, return a 64-bit value by adding the top and bottom
77 * 64-bit words. NOTE: This function can not be changed EVER. Although
78 * brain-dead, some applications depend on this 64-bit value remaining
79 * persistent. Specifically, DMI vendors store the value as a persistent
80 * filehandle.
81 */
82__uint64_t
83uuid_hash64(uuid_t *uuid)
84{
85 __uint64_t *sp = (__uint64_t *)uuid;
86
87 return sp[0] + sp[1];
88}
89
90int
91uuid_table_insert(uuid_t *uuid)
92{
93 int i, hole;
94
Jes Sorensen794ee1b2006-01-09 15:59:21 -080095 mutex_lock(&uuid_monitor);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 for (i = 0, hole = -1; i < uuid_table_size; i++) {
97 if (uuid_is_nil(&uuid_table[i])) {
98 hole = i;
99 continue;
100 }
101 if (uuid_equal(uuid, &uuid_table[i])) {
102 mutex_unlock(&uuid_monitor);
103 return 0;
104 }
105 }
106 if (hole < 0) {
107 uuid_table = kmem_realloc(uuid_table,
108 (uuid_table_size + 1) * sizeof(*uuid_table),
109 uuid_table_size * sizeof(*uuid_table),
110 KM_SLEEP);
111 hole = uuid_table_size++;
112 }
113 uuid_table[hole] = *uuid;
114 mutex_unlock(&uuid_monitor);
115 return 1;
116}
117
118void
119uuid_table_remove(uuid_t *uuid)
120{
121 int i;
122
Jes Sorensen794ee1b2006-01-09 15:59:21 -0800123 mutex_lock(&uuid_monitor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 for (i = 0; i < uuid_table_size; i++) {
125 if (uuid_is_nil(&uuid_table[i]))
126 continue;
127 if (!uuid_equal(uuid, &uuid_table[i]))
128 continue;
129 uuid_create_nil(&uuid_table[i]);
130 break;
131 }
132 ASSERT(i < uuid_table_size);
133 mutex_unlock(&uuid_monitor);
134}
Nathan Scotta7801432006-03-14 13:24:46 +1100135
136void
137uuid_init(void)
138{
139 mutex_init(&uuid_monitor);
140}