tdb1_open.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /*
  2. Unix SMB/CIFS implementation.
  3. trivial database library
  4. Copyright (C) Andrew Tridgell 1999-2005
  5. Copyright (C) Paul `Rusty' Russell 2000
  6. Copyright (C) Jeremy Allison 2000-2003
  7. ** NOTE! The following LGPL license applies to the tdb
  8. ** library. This does NOT imply that all of Samba is released
  9. ** under the LGPL
  10. This library is free software; you can redistribute it and/or
  11. modify it under the terms of the GNU Lesser General Public
  12. License as published by the Free Software Foundation; either
  13. version 3 of the License, or (at your option) any later version.
  14. This library is distributed in the hope that it will be useful,
  15. but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. Lesser General Public License for more details.
  18. You should have received a copy of the GNU Lesser General Public
  19. License along with this library; if not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #include <assert.h>
  22. #include "tdb1_private.h"
  23. #include <assert.h>
  24. /* We use two hashes to double-check they're using the right hash function. */
  25. void tdb1_header_hash(struct tdb_context *tdb,
  26. uint32_t *magic1_hash, uint32_t *magic2_hash)
  27. {
  28. uint32_t tdb1_magic = TDB1_MAGIC;
  29. *magic1_hash = tdb_hash(tdb, TDB_MAGIC_FOOD, sizeof(TDB_MAGIC_FOOD));
  30. *magic2_hash = tdb_hash(tdb, TDB1_CONV(tdb1_magic), sizeof(tdb1_magic));
  31. /* Make sure at least one hash is non-zero! */
  32. if (*magic1_hash == 0 && *magic2_hash == 0)
  33. *magic1_hash = 1;
  34. }
  35. static void tdb_context_init(struct tdb_context *tdb,
  36. struct tdb_attribute_tdb1_max_dead *max_dead)
  37. {
  38. assert(tdb->flags & TDB_VERSION1);
  39. tdb1_io_init(tdb);
  40. tdb->tdb1.traverse_read = tdb->tdb1.traverse_write = 0;
  41. memset(&tdb->tdb1.travlocks, 0, sizeof(tdb->tdb1.travlocks));
  42. tdb->tdb1.transaction = NULL;
  43. /* cache the page size */
  44. tdb->tdb1.page_size = getpagesize();
  45. if (tdb->tdb1.page_size <= 0) {
  46. tdb->tdb1.page_size = 0x2000;
  47. }
  48. if (max_dead) {
  49. tdb->tdb1.max_dead_records = max_dead->max_dead;
  50. } else {
  51. tdb->tdb1.max_dead_records = 0;
  52. }
  53. }
  54. /* initialise a new database */
  55. enum TDB_ERROR tdb1_new_database(struct tdb_context *tdb,
  56. struct tdb_attribute_tdb1_hashsize *hashsize,
  57. struct tdb_attribute_tdb1_max_dead *max_dead)
  58. {
  59. struct tdb1_header *newdb;
  60. size_t size;
  61. int hash_size = TDB1_DEFAULT_HASH_SIZE;
  62. enum TDB_ERROR ret;
  63. tdb_context_init(tdb, max_dead);
  64. /* Default TDB2 hash becomes default TDB1 hash. */
  65. if (tdb->hash_fn == tdb_jenkins_hash)
  66. tdb->hash_fn = tdb1_old_hash;
  67. if (hashsize)
  68. hash_size = hashsize->hsize;
  69. /* We make it up in memory, then write it out if not internal */
  70. size = sizeof(struct tdb1_header) + (hash_size+1)*sizeof(tdb1_off_t);
  71. if (!(newdb = (struct tdb1_header *)calloc(size, 1))) {
  72. return tdb_logerr(tdb, TDB_ERR_OOM, TDB_LOG_ERROR,
  73. "Could not allocate new database header");
  74. }
  75. /* Fill in the header */
  76. newdb->version = TDB1_VERSION;
  77. newdb->hash_size = hash_size;
  78. tdb1_header_hash(tdb, &newdb->magic1_hash, &newdb->magic2_hash);
  79. /* Make sure older tdbs (which don't check the magic hash fields)
  80. * will refuse to open this TDB. */
  81. if (tdb->hash_fn == tdb1_incompatible_hash)
  82. newdb->rwlocks = TDB1_HASH_RWLOCK_MAGIC;
  83. memcpy(&tdb->tdb1.header, newdb, sizeof(tdb->tdb1.header));
  84. /* This creates an endian-converted db. */
  85. TDB1_CONV(*newdb);
  86. /* Don't endian-convert the magic food! */
  87. memcpy(newdb->magic_food, TDB_MAGIC_FOOD, strlen(TDB_MAGIC_FOOD)+1);
  88. if (tdb->flags & TDB_INTERNAL) {
  89. tdb->file->map_size = size;
  90. tdb->file->map_ptr = (char *)newdb;
  91. return TDB_SUCCESS;
  92. }
  93. if (lseek(tdb->file->fd, 0, SEEK_SET) == -1) {
  94. ret = tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
  95. "tdb1_new_database: lseek failed");
  96. goto fail;
  97. }
  98. if (ftruncate(tdb->file->fd, 0) == -1) {
  99. ret = tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
  100. "tdb1_new_database: ftruncate failed");
  101. goto fail;
  102. }
  103. if (!tdb1_write_all(tdb->file->fd, newdb, size)) {
  104. ret = tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
  105. "tdb1_new_database: write failed");
  106. goto fail;
  107. }
  108. ret = TDB_SUCCESS;
  109. fail:
  110. SAFE_FREE(newdb);
  111. return ret;
  112. }
  113. typedef void (*tdb1_log_func)(struct tdb_context *, enum tdb_log_level, enum TDB_ERROR,
  114. const char *, void *);
  115. typedef uint64_t (*tdb1_hash_func)(const void *key, size_t len, uint64_t seed,
  116. void *data);
  117. struct tdb1_logging_context {
  118. tdb1_log_func log_fn;
  119. void *log_private;
  120. };
  121. static bool hash_correct(struct tdb_context *tdb,
  122. uint32_t *m1, uint32_t *m2)
  123. {
  124. /* older TDB without magic hash references */
  125. if (tdb->tdb1.header.magic1_hash == 0
  126. && tdb->tdb1.header.magic2_hash == 0) {
  127. return true;
  128. }
  129. tdb1_header_hash(tdb, m1, m2);
  130. return (tdb->tdb1.header.magic1_hash == *m1 &&
  131. tdb->tdb1.header.magic2_hash == *m2);
  132. }
  133. static bool check_header_hash(struct tdb_context *tdb,
  134. uint32_t *m1, uint32_t *m2)
  135. {
  136. if (hash_correct(tdb, m1, m2))
  137. return true;
  138. /* If they use one inbuilt, try the other inbuilt hash. */
  139. if (tdb->hash_fn == tdb1_old_hash)
  140. tdb->hash_fn = tdb1_incompatible_hash;
  141. else if (tdb->hash_fn == tdb1_incompatible_hash)
  142. tdb->hash_fn = tdb1_old_hash;
  143. else
  144. return false;
  145. return hash_correct(tdb, m1, m2);
  146. }
  147. /* We are hold the TDB open lock on tdb->fd. */
  148. enum TDB_ERROR tdb1_open(struct tdb_context *tdb,
  149. struct tdb_attribute_tdb1_max_dead *max_dead)
  150. {
  151. const char *hash_alg;
  152. uint32_t magic1, magic2;
  153. tdb->flags |= TDB_VERSION1;
  154. tdb_context_init(tdb, max_dead);
  155. /* Default TDB2 hash becomes default TDB1 hash. */
  156. if (tdb->hash_fn == tdb_jenkins_hash) {
  157. tdb->hash_fn = tdb1_old_hash;
  158. hash_alg = "default";
  159. } else if (tdb->hash_fn == tdb1_incompatible_hash)
  160. hash_alg = "tdb1_incompatible_hash";
  161. else
  162. hash_alg = "the user defined";
  163. if (tdb->tdb1.header.version != TDB1_BYTEREV(TDB1_VERSION)) {
  164. if (tdb->flags & TDB_CONVERT) {
  165. return tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
  166. "tdb1_open:"
  167. " %s does not need TDB_CONVERT",
  168. tdb->name);
  169. }
  170. } else {
  171. tdb->flags |= TDB_CONVERT;
  172. tdb1_convert(&tdb->tdb1.header, sizeof(tdb->tdb1.header));
  173. }
  174. if (tdb->tdb1.header.rwlocks != 0 &&
  175. tdb->tdb1.header.rwlocks != TDB1_HASH_RWLOCK_MAGIC) {
  176. return tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_LOG_ERROR,
  177. "tdb1_open: spinlocks no longer supported");
  178. }
  179. if (!check_header_hash(tdb, &magic1, &magic2)) {
  180. return tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_LOG_USE_ERROR,
  181. "tdb1_open: "
  182. "%s was not created with %s hash function we are using\n"
  183. "magic1_hash[0x%08X %s 0x%08X] "
  184. "magic2_hash[0x%08X %s 0x%08X]",
  185. tdb->name, hash_alg,
  186. tdb->tdb1.header.magic1_hash,
  187. (tdb->tdb1.header.magic1_hash == magic1) ? "==" : "!=",
  188. magic1,
  189. tdb->tdb1.header.magic2_hash,
  190. (tdb->tdb1.header.magic2_hash == magic2) ? "==" : "!=",
  191. magic2);
  192. }
  193. return TDB_SUCCESS;
  194. }