tdb1_private.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. #ifndef CCAN_TDB2_TDB1_PRIVATE_H
  2. #define CCAN_TDB2_TDB1_PRIVATE_H
  3. /*
  4. Unix SMB/CIFS implementation.
  5. trivial database library - private includes
  6. Copyright (C) Andrew Tridgell 2005
  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 "private.h"
  22. #include <limits.h>
  23. /* #define TDB_TRACE 1 */
  24. #ifndef HAVE_GETPAGESIZE
  25. #define getpagesize() 0x2000
  26. #endif
  27. #ifndef __STRING
  28. #define __STRING(x) #x
  29. #endif
  30. #ifndef __STRINGSTRING
  31. #define __STRINGSTRING(x) __STRING(x)
  32. #endif
  33. #ifndef __location__
  34. #define __location__ __FILE__ ":" __STRINGSTRING(__LINE__)
  35. #endif
  36. #ifndef offsetof
  37. #define offsetof(t,f) ((unsigned int)&((t *)0)->f)
  38. #endif
  39. #define TDB1_VERSION (0x26011967 + 6)
  40. #define TDB1_MAGIC (0x26011999U)
  41. #define TDB1_FREE_MAGIC (~TDB1_MAGIC)
  42. #define TDB1_DEAD_MAGIC (0xFEE1DEAD)
  43. #define TDB1_RECOVERY_MAGIC (0xf53bc0e7U)
  44. #define TDB1_RECOVERY_INVALID_MAGIC (0x0)
  45. #define TDB1_HASH_RWLOCK_MAGIC (0xbad1a51U)
  46. #define TDB1_ALIGNMENT 4
  47. #define TDB1_DEFAULT_HASH_SIZE 131
  48. #define TDB1_FREELIST_TOP (sizeof(struct tdb1_header))
  49. #define TDB1_ALIGN(x,a) (((x) + (a)-1) & ~((a)-1))
  50. #define TDB1_DEAD(r) ((r)->magic == TDB1_DEAD_MAGIC)
  51. #define TDB1_BAD_MAGIC(r) ((r)->magic != TDB1_MAGIC && !TDB1_DEAD(r))
  52. #define TDB1_HASH_TOP(hash) (TDB1_FREELIST_TOP + (TDB1_BUCKET(hash)+1)*sizeof(tdb1_off_t))
  53. #define TDB1_HASHTABLE_SIZE(tdb) ((tdb->tdb1.header.hash_size+1)*sizeof(tdb1_off_t))
  54. #define TDB1_DATA_START(hash_size) (TDB1_HASH_TOP(hash_size-1) + sizeof(tdb1_off_t))
  55. #define TDB1_RECOVERY_HEAD offsetof(struct tdb1_header, recovery_start)
  56. #define TDB1_SEQNUM_OFS offsetof(struct tdb1_header, sequence_number)
  57. #define TDB1_PAD_BYTE 0x42
  58. #define TDB1_PAD_U32 0x42424242
  59. /* lock offsets */
  60. #define TDB1_OPEN_LOCK 0
  61. #define TDB1_ACTIVE_LOCK 4
  62. #define TDB1_TRANSACTION_LOCK 8
  63. /* free memory if the pointer is valid and zero the pointer */
  64. #ifndef SAFE_FREE
  65. #define SAFE_FREE(x) do { if ((x) != NULL) {free((void *)x); (x)=NULL;} } while(0)
  66. #endif
  67. #define TDB1_BUCKET(hash) ((hash) % tdb->tdb1.header.hash_size)
  68. #define TDB1_DOCONV() (tdb->flags & TDB_CONVERT)
  69. #define TDB1_CONV(x) (TDB1_DOCONV() ? tdb1_convert(&x, sizeof(x)) : &x)
  70. /* the body of the database is made of one tdb1_record for the free space
  71. plus a separate data list for each hash value */
  72. struct tdb1_record {
  73. tdb1_off_t next; /* offset of the next record in the list */
  74. tdb1_len_t rec_len; /* total byte length of record */
  75. tdb1_len_t key_len; /* byte length of key */
  76. tdb1_len_t data_len; /* byte length of data */
  77. uint32_t full_hash; /* the full 32 bit hash of the key */
  78. uint32_t magic; /* try to catch errors */
  79. /* the following union is implied:
  80. union {
  81. char record[rec_len];
  82. struct {
  83. char key[key_len];
  84. char data[data_len];
  85. }
  86. uint32_t totalsize; (tailer)
  87. }
  88. */
  89. };
  90. struct tdb1_methods {
  91. int (*tdb1_read)(struct tdb_context *, tdb1_off_t , void *, tdb1_len_t , int );
  92. int (*tdb1_write)(struct tdb_context *, tdb1_off_t, const void *, tdb1_len_t);
  93. void (*next_hash_chain)(struct tdb_context *, uint32_t *);
  94. int (*tdb1_oob)(struct tdb_context *, tdb1_off_t , int );
  95. int (*tdb1_expand_file)(struct tdb_context *, tdb1_off_t , tdb1_off_t );
  96. };
  97. /*
  98. internal prototypes
  99. */
  100. int tdb1_munmap(struct tdb_context *tdb);
  101. void tdb1_mmap(struct tdb_context *tdb);
  102. int tdb1_lock(struct tdb_context *tdb, int list, int ltype);
  103. int tdb1_nest_lock(struct tdb_context *tdb, uint32_t offset, int ltype,
  104. enum tdb_lock_flags flags);
  105. int tdb1_nest_unlock(struct tdb_context *tdb, uint32_t offset, int ltype);
  106. int tdb1_unlock(struct tdb_context *tdb, int list, int ltype);
  107. int tdb1_brlock(struct tdb_context *tdb,
  108. int rw_type, tdb1_off_t offset, size_t len,
  109. enum tdb_lock_flags flags);
  110. int tdb1_brunlock(struct tdb_context *tdb,
  111. int rw_type, tdb1_off_t offset, size_t len);
  112. bool tdb1_have_extra_locks(struct tdb_context *tdb);
  113. void tdb1_release_transaction_locks(struct tdb_context *tdb);
  114. int tdb1_transaction_lock(struct tdb_context *tdb, int ltype,
  115. enum tdb_lock_flags lockflags);
  116. int tdb1_transaction_unlock(struct tdb_context *tdb, int ltype);
  117. int tdb1_recovery_area(struct tdb_context *tdb,
  118. const struct tdb1_methods *methods,
  119. tdb1_off_t *recovery_offset,
  120. struct tdb1_record *rec);
  121. int tdb1_allrecord_upgrade(struct tdb_context *tdb);
  122. int tdb1_write_lock_record(struct tdb_context *tdb, tdb1_off_t off);
  123. int tdb1_write_unlock_record(struct tdb_context *tdb, tdb1_off_t off);
  124. int tdb1_ofs_read(struct tdb_context *tdb, tdb1_off_t offset, tdb1_off_t *d);
  125. int tdb1_ofs_write(struct tdb_context *tdb, tdb1_off_t offset, tdb1_off_t *d);
  126. void *tdb1_convert(void *buf, uint32_t size);
  127. int tdb1_free(struct tdb_context *tdb, tdb1_off_t offset, struct tdb1_record *rec);
  128. tdb1_off_t tdb1_allocate(struct tdb_context *tdb, tdb1_len_t length, struct tdb1_record *rec);
  129. int tdb1_ofs_read(struct tdb_context *tdb, tdb1_off_t offset, tdb1_off_t *d);
  130. int tdb1_ofs_write(struct tdb_context *tdb, tdb1_off_t offset, tdb1_off_t *d);
  131. int tdb1_lock_record(struct tdb_context *tdb, tdb1_off_t off);
  132. int tdb1_unlock_record(struct tdb_context *tdb, tdb1_off_t off);
  133. tdb_bool_err tdb1_needs_recovery(struct tdb_context *tdb);
  134. int tdb1_rec_read(struct tdb_context *tdb, tdb1_off_t offset, struct tdb1_record *rec);
  135. int tdb1_rec_write(struct tdb_context *tdb, tdb1_off_t offset, struct tdb1_record *rec);
  136. int tdb1_do_delete(struct tdb_context *tdb, tdb1_off_t rec_ptr, struct tdb1_record *rec);
  137. unsigned char *tdb1_alloc_read(struct tdb_context *tdb, tdb1_off_t offset, tdb1_len_t len);
  138. enum TDB_ERROR tdb1_parse_data(struct tdb_context *tdb, TDB_DATA key,
  139. tdb1_off_t offset, tdb1_len_t len,
  140. enum TDB_ERROR (*parser)(TDB_DATA key,
  141. TDB_DATA data,
  142. void *private_data),
  143. void *private_data);
  144. tdb1_off_t tdb1_find_lock_hash(struct tdb_context *tdb, TDB_DATA key, uint32_t hash, int locktype,
  145. struct tdb1_record *rec);
  146. void tdb1_io_init(struct tdb_context *tdb);
  147. int tdb1_expand(struct tdb_context *tdb, tdb1_off_t size);
  148. int tdb1_rec_free_read(struct tdb_context *tdb, tdb1_off_t off,
  149. struct tdb1_record *rec);
  150. bool tdb1_write_all(int fd, const void *buf, size_t count);
  151. void tdb1_header_hash(struct tdb_context *tdb,
  152. uint32_t *magic1_hash, uint32_t *magic2_hash);
  153. uint64_t tdb1_old_hash(const void *key, size_t len, uint64_t seed, void *);
  154. size_t tdb1_dead_space(struct tdb_context *tdb, tdb1_off_t off);
  155. #endif /* CCAN_TDB2_TDB1_PRIVATE_H */