uuid.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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);
  26. }
  27. /* IRIX interpretation of an uuid_t */
  28. typedef struct {
  29. __be32 uu_timelow;
  30. __be16 uu_timemid;
  31. __be16 uu_timehi;
  32. __be16 uu_clockseq;
  33. __be16 uu_node[3];
  34. } xfs_uu_t;
  35. /*
  36. * uuid_getnodeuniq - obtain the node unique fields of a UUID.
  37. *
  38. * This is not in any way a standard or condoned UUID function;
  39. * it just something that's needed for user-level file handles.
  40. */
  41. void
  42. uuid_getnodeuniq(uuid_t *uuid, int fsid [2])
  43. {
  44. xfs_uu_t *uup = (xfs_uu_t *)uuid;
  45. fsid[0] = (be16_to_cpu(uup->uu_clockseq) << 16) |
  46. be16_to_cpu(uup->uu_timemid);
  47. fsid[1] = be16_to_cpu(uup->uu_timelow);
  48. }
  49. void
  50. uuid_create_nil(uuid_t *uuid)
  51. {
  52. memset(uuid, 0, sizeof(*uuid));
  53. }
  54. int
  55. uuid_is_nil(uuid_t *uuid)
  56. {
  57. int i;
  58. char *cp = (char *)uuid;
  59. if (uuid == NULL)
  60. return 0;
  61. /* implied check of version number here... */
  62. for (i = 0; i < sizeof *uuid; i++)
  63. if (*cp++) return 0; /* not nil */
  64. return 1; /* is nil */
  65. }
  66. int
  67. uuid_equal(uuid_t *uuid1, uuid_t *uuid2)
  68. {
  69. return memcmp(uuid1, uuid2, sizeof(uuid_t)) ? 0 : 1;
  70. }
  71. /*
  72. * Given a 128-bit uuid, return a 64-bit value by adding the top and bottom
  73. * 64-bit words. NOTE: This function can not be changed EVER. Although
  74. * brain-dead, some applications depend on this 64-bit value remaining
  75. * persistent. Specifically, DMI vendors store the value as a persistent
  76. * filehandle.
  77. */
  78. __uint64_t
  79. uuid_hash64(uuid_t *uuid)
  80. {
  81. __uint64_t *sp = (__uint64_t *)uuid;
  82. return sp[0] + sp[1];
  83. }
  84. int
  85. uuid_table_insert(uuid_t *uuid)
  86. {
  87. int i, hole;
  88. mutex_lock(&uuid_monitor);
  89. for (i = 0, hole = -1; i < uuid_table_size; i++) {
  90. if (uuid_is_nil(&uuid_table[i])) {
  91. hole = i;
  92. continue;
  93. }
  94. if (uuid_equal(uuid, &uuid_table[i])) {
  95. mutex_unlock(&uuid_monitor);
  96. return 0;
  97. }
  98. }
  99. if (hole < 0) {
  100. uuid_table = kmem_realloc(uuid_table,
  101. (uuid_table_size + 1) * sizeof(*uuid_table),
  102. uuid_table_size * sizeof(*uuid_table),
  103. KM_SLEEP);
  104. hole = uuid_table_size++;
  105. }
  106. uuid_table[hole] = *uuid;
  107. mutex_unlock(&uuid_monitor);
  108. return 1;
  109. }
  110. void
  111. uuid_table_remove(uuid_t *uuid)
  112. {
  113. int i;
  114. mutex_lock(&uuid_monitor);
  115. for (i = 0; i < uuid_table_size; i++) {
  116. if (uuid_is_nil(&uuid_table[i]))
  117. continue;
  118. if (!uuid_equal(uuid, &uuid_table[i]))
  119. continue;
  120. uuid_create_nil(&uuid_table[i]);
  121. break;
  122. }
  123. ASSERT(i < uuid_table_size);
  124. mutex_unlock(&uuid_monitor);
  125. }