uuid.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * Copyright (c) 2000-2003 Silicon Graphics, Inc. All Rights Reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of version 2 of the GNU General Public License as
  6. * published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it would be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. *
  12. * Further, this software is distributed without any warranty that it is
  13. * free of the rightful claim of any third person regarding infringement
  14. * or the like. Any license provided herein, whether implied or
  15. * otherwise, applies only to this software file. Patent licenses, if
  16. * any, provided herein do not apply to combinations of this program with
  17. * other software, or any other product whatsoever.
  18. *
  19. * You should have received a copy of the GNU General Public License along
  20. * with this program; if not, write the Free Software Foundation, Inc., 59
  21. * Temple Place - Suite 330, Boston MA 02111-1307, USA.
  22. *
  23. * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
  24. * Mountain View, CA 94043, or:
  25. *
  26. * http://www.sgi.com
  27. *
  28. * For further information regarding this notice, see:
  29. *
  30. * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
  31. */
  32. #include <xfs.h>
  33. static mutex_t uuid_monitor;
  34. static int uuid_table_size;
  35. static uuid_t *uuid_table;
  36. void
  37. uuid_init(void)
  38. {
  39. mutex_init(&uuid_monitor, MUTEX_DEFAULT, "uuid_monitor");
  40. }
  41. /*
  42. * uuid_getnodeuniq - obtain the node unique fields of a UUID.
  43. *
  44. * This is not in any way a standard or condoned UUID function;
  45. * it just something that's needed for user-level file handles.
  46. */
  47. void
  48. uuid_getnodeuniq(uuid_t *uuid, int fsid [2])
  49. {
  50. char *uu = (char *)uuid;
  51. /* on IRIX, this function assumes big-endian fields within
  52. * the uuid, so we use INT_GET to get the same result on
  53. * little-endian systems
  54. */
  55. fsid[0] = (INT_GET(*(u_int16_t*)(uu+8), ARCH_CONVERT) << 16) +
  56. INT_GET(*(u_int16_t*)(uu+4), ARCH_CONVERT);
  57. fsid[1] = INT_GET(*(u_int32_t*)(uu ), ARCH_CONVERT);
  58. }
  59. void
  60. uuid_create_nil(uuid_t *uuid)
  61. {
  62. memset(uuid, 0, sizeof(*uuid));
  63. }
  64. int
  65. uuid_is_nil(uuid_t *uuid)
  66. {
  67. int i;
  68. char *cp = (char *)uuid;
  69. if (uuid == NULL)
  70. return 0;
  71. /* implied check of version number here... */
  72. for (i = 0; i < sizeof *uuid; i++)
  73. if (*cp++) return 0; /* not nil */
  74. return 1; /* is nil */
  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. * Given a 128-bit uuid, return a 64-bit value by adding the top and bottom
  83. * 64-bit words. NOTE: This function can not be changed EVER. Although
  84. * brain-dead, some applications depend on this 64-bit value remaining
  85. * persistent. Specifically, DMI vendors store the value as a persistent
  86. * filehandle.
  87. */
  88. __uint64_t
  89. uuid_hash64(uuid_t *uuid)
  90. {
  91. __uint64_t *sp = (__uint64_t *)uuid;
  92. return sp[0] + sp[1];
  93. }
  94. int
  95. uuid_table_insert(uuid_t *uuid)
  96. {
  97. int i, hole;
  98. mutex_lock(&uuid_monitor, PVFS);
  99. for (i = 0, hole = -1; i < uuid_table_size; i++) {
  100. if (uuid_is_nil(&uuid_table[i])) {
  101. hole = i;
  102. continue;
  103. }
  104. if (uuid_equal(uuid, &uuid_table[i])) {
  105. mutex_unlock(&uuid_monitor);
  106. return 0;
  107. }
  108. }
  109. if (hole < 0) {
  110. uuid_table = kmem_realloc(uuid_table,
  111. (uuid_table_size + 1) * sizeof(*uuid_table),
  112. uuid_table_size * sizeof(*uuid_table),
  113. KM_SLEEP);
  114. hole = uuid_table_size++;
  115. }
  116. uuid_table[hole] = *uuid;
  117. mutex_unlock(&uuid_monitor);
  118. return 1;
  119. }
  120. void
  121. uuid_table_remove(uuid_t *uuid)
  122. {
  123. int i;
  124. mutex_lock(&uuid_monitor, PVFS);
  125. for (i = 0; i < uuid_table_size; i++) {
  126. if (uuid_is_nil(&uuid_table[i]))
  127. continue;
  128. if (!uuid_equal(uuid, &uuid_table[i]))
  129. continue;
  130. uuid_create_nil(&uuid_table[i]);
  131. break;
  132. }
  133. ASSERT(i < uuid_table_size);
  134. mutex_unlock(&uuid_monitor);
  135. }