uuid.c 3.1 KB

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