Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 2 | * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc. |
| 3 | * All Rights Reserved. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 5 | * 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * published by the Free Software Foundation. |
| 8 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 9 | * 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 14 | * 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <xfs.h> |
| 19 | |
| 20 | static mutex_t uuid_monitor; |
| 21 | static int uuid_table_size; |
| 22 | static uuid_t *uuid_table; |
| 23 | |
| 24 | void |
| 25 | uuid_init(void) |
| 26 | { |
Jes Sorensen | 794ee1b | 2006-01-09 15:59:21 -0800 | [diff] [blame] | 27 | mutex_init(&uuid_monitor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | } |
| 29 | |
Christoph Hellwig | 794fe2d | 2006-01-11 15:37:17 +1100 | [diff] [blame] | 30 | |
| 31 | /* IRIX interpretation of an uuid_t */ |
| 32 | typedef struct { |
| 33 | __be32 uu_timelow; |
| 34 | __be16 uu_timemid; |
| 35 | __be16 uu_timehi; |
| 36 | __be16 uu_clockseq; |
| 37 | __be16 uu_node[3]; |
| 38 | } xfs_uu_t; |
| 39 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | /* |
| 41 | * uuid_getnodeuniq - obtain the node unique fields of a UUID. |
| 42 | * |
| 43 | * This is not in any way a standard or condoned UUID function; |
| 44 | * it just something that's needed for user-level file handles. |
| 45 | */ |
| 46 | void |
| 47 | uuid_getnodeuniq(uuid_t *uuid, int fsid [2]) |
| 48 | { |
Christoph Hellwig | 794fe2d | 2006-01-11 15:37:17 +1100 | [diff] [blame] | 49 | xfs_uu_t *uup = (xfs_uu_t *)uuid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | |
Christoph Hellwig | 794fe2d | 2006-01-11 15:37:17 +1100 | [diff] [blame] | 51 | fsid[0] = (be16_to_cpu(uup->uu_clockseq) << 16) | |
| 52 | be16_to_cpu(uup->uu_timemid); |
| 53 | fsid[1] = be16_to_cpu(uup->uu_timelow); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | void |
| 57 | uuid_create_nil(uuid_t *uuid) |
| 58 | { |
| 59 | memset(uuid, 0, sizeof(*uuid)); |
| 60 | } |
| 61 | |
| 62 | int |
| 63 | uuid_is_nil(uuid_t *uuid) |
| 64 | { |
| 65 | int i; |
| 66 | char *cp = (char *)uuid; |
| 67 | |
| 68 | if (uuid == NULL) |
| 69 | return 0; |
| 70 | /* implied check of version number here... */ |
| 71 | for (i = 0; i < sizeof *uuid; i++) |
| 72 | if (*cp++) return 0; /* not nil */ |
| 73 | return 1; /* is nil */ |
| 74 | } |
| 75 | |
| 76 | int |
| 77 | uuid_equal(uuid_t *uuid1, uuid_t *uuid2) |
| 78 | { |
| 79 | return memcmp(uuid1, uuid2, sizeof(uuid_t)) ? 0 : 1; |
| 80 | } |
| 81 | |
| 82 | /* |
| 83 | * Given a 128-bit uuid, return a 64-bit value by adding the top and bottom |
| 84 | * 64-bit words. NOTE: This function can not be changed EVER. Although |
| 85 | * brain-dead, some applications depend on this 64-bit value remaining |
| 86 | * persistent. Specifically, DMI vendors store the value as a persistent |
| 87 | * filehandle. |
| 88 | */ |
| 89 | __uint64_t |
| 90 | uuid_hash64(uuid_t *uuid) |
| 91 | { |
| 92 | __uint64_t *sp = (__uint64_t *)uuid; |
| 93 | |
| 94 | return sp[0] + sp[1]; |
| 95 | } |
| 96 | |
| 97 | int |
| 98 | uuid_table_insert(uuid_t *uuid) |
| 99 | { |
| 100 | int i, hole; |
| 101 | |
Jes Sorensen | 794ee1b | 2006-01-09 15:59:21 -0800 | [diff] [blame] | 102 | mutex_lock(&uuid_monitor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | for (i = 0, hole = -1; i < uuid_table_size; i++) { |
| 104 | if (uuid_is_nil(&uuid_table[i])) { |
| 105 | hole = i; |
| 106 | continue; |
| 107 | } |
| 108 | if (uuid_equal(uuid, &uuid_table[i])) { |
| 109 | mutex_unlock(&uuid_monitor); |
| 110 | return 0; |
| 111 | } |
| 112 | } |
| 113 | if (hole < 0) { |
| 114 | uuid_table = kmem_realloc(uuid_table, |
| 115 | (uuid_table_size + 1) * sizeof(*uuid_table), |
| 116 | uuid_table_size * sizeof(*uuid_table), |
| 117 | KM_SLEEP); |
| 118 | hole = uuid_table_size++; |
| 119 | } |
| 120 | uuid_table[hole] = *uuid; |
| 121 | mutex_unlock(&uuid_monitor); |
| 122 | return 1; |
| 123 | } |
| 124 | |
| 125 | void |
| 126 | uuid_table_remove(uuid_t *uuid) |
| 127 | { |
| 128 | int i; |
| 129 | |
Jes Sorensen | 794ee1b | 2006-01-09 15:59:21 -0800 | [diff] [blame] | 130 | mutex_lock(&uuid_monitor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | for (i = 0; i < uuid_table_size; i++) { |
| 132 | if (uuid_is_nil(&uuid_table[i])) |
| 133 | continue; |
| 134 | if (!uuid_equal(uuid, &uuid_table[i])) |
| 135 | continue; |
| 136 | uuid_create_nil(&uuid_table[i]); |
| 137 | break; |
| 138 | } |
| 139 | ASSERT(i < uuid_table_size); |
| 140 | mutex_unlock(&uuid_monitor); |
| 141 | } |