uuid.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc.
  3. * All Rights Reserved.
  4. *
  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
  7. * published by the Free Software Foundation.
  8. *
  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.
  13. *
  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
  17. */
  18. #include <xfs.h>
  19. static mutex_t uuid_monitor;
  20. static int uuid_table_size;
  21. static uuid_t *uuid_table;
  22. void
  23. uuid_init(void)
  24. {
  25. mutex_init(&uuid_monitor, MUTEX_DEFAULT, "uuid_monitor");
  26. }
  27. /*
  28. * uuid_getnodeuniq - obtain the node unique fields of a UUID.
  29. *
  30. * This is not in any way a standard or condoned UUID function;
  31. * it just something that's needed for user-level file handles.
  32. */
  33. void
  34. uuid_getnodeuniq(uuid_t *uuid, int fsid [2])
  35. {
  36. char *uu = (char *)uuid;
  37. /* on IRIX, this function assumes big-endian fields within
  38. * the uuid, so we use INT_GET to get the same result on
  39. * little-endian systems
  40. */
  41. fsid[0] = (INT_GET(*(u_int16_t*)(uu+8), ARCH_CONVERT) << 16) +
  42. INT_GET(*(u_int16_t*)(uu+4), ARCH_CONVERT);
  43. fsid[1] = INT_GET(*(u_int32_t*)(uu ), ARCH_CONVERT);
  44. }
  45. void
  46. uuid_create_nil(uuid_t *uuid)
  47. {
  48. memset(uuid, 0, sizeof(*uuid));
  49. }
  50. int
  51. uuid_is_nil(uuid_t *uuid)
  52. {
  53. int i;
  54. char *cp = (char *)uuid;
  55. if (uuid == NULL)
  56. return 0;
  57. /* implied check of version number here... */
  58. for (i = 0; i < sizeof *uuid; i++)
  59. if (*cp++) return 0; /* not nil */
  60. return 1; /* is nil */
  61. }
  62. int
  63. uuid_equal(uuid_t *uuid1, uuid_t *uuid2)
  64. {
  65. return memcmp(uuid1, uuid2, sizeof(uuid_t)) ? 0 : 1;
  66. }
  67. /*
  68. * Given a 128-bit uuid, return a 64-bit value by adding the top and bottom
  69. * 64-bit words. NOTE: This function can not be changed EVER. Although
  70. * brain-dead, some applications depend on this 64-bit value remaining
  71. * persistent. Specifically, DMI vendors store the value as a persistent
  72. * filehandle.
  73. */
  74. __uint64_t
  75. uuid_hash64(uuid_t *uuid)
  76. {
  77. __uint64_t *sp = (__uint64_t *)uuid;
  78. return sp[0] + sp[1];
  79. }
  80. int
  81. uuid_table_insert(uuid_t *uuid)
  82. {
  83. int i, hole;
  84. mutex_lock(&uuid_monitor, PVFS);
  85. for (i = 0, hole = -1; i < uuid_table_size; i++) {
  86. if (uuid_is_nil(&uuid_table[i])) {
  87. hole = i;
  88. continue;
  89. }
  90. if (uuid_equal(uuid, &uuid_table[i])) {
  91. mutex_unlock(&uuid_monitor);
  92. return 0;
  93. }
  94. }
  95. if (hole < 0) {
  96. uuid_table = kmem_realloc(uuid_table,
  97. (uuid_table_size + 1) * sizeof(*uuid_table),
  98. uuid_table_size * sizeof(*uuid_table),
  99. KM_SLEEP);
  100. hole = uuid_table_size++;
  101. }
  102. uuid_table[hole] = *uuid;
  103. mutex_unlock(&uuid_monitor);
  104. return 1;
  105. }
  106. void
  107. uuid_table_remove(uuid_t *uuid)
  108. {
  109. int i;
  110. mutex_lock(&uuid_monitor, PVFS);
  111. for (i = 0; i < uuid_table_size; i++) {
  112. if (uuid_is_nil(&uuid_table[i]))
  113. continue;
  114. if (!uuid_equal(uuid, &uuid_table[i]))
  115. continue;
  116. uuid_create_nil(&uuid_table[i]);
  117. break;
  118. }
  119. ASSERT(i < uuid_table_size);
  120. mutex_unlock(&uuid_monitor);
  121. }