tdb1_private.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. #ifndef _SAMBA_BUILD_
  22. #include "config.h"
  23. /* This keeps us consistent with TDB2 code. */
  24. #if HAVE_FILE_OFFSET_BITS
  25. #define _FILE_OFFSET_BITS 64
  26. #endif
  27. #include <stdint.h>
  28. #include <stdbool.h>
  29. #include <string.h>
  30. #include <sys/types.h>
  31. #include <sys/stat.h>
  32. #include <sys/mman.h>
  33. #include <sys/time.h>
  34. #include <fcntl.h>
  35. #include <unistd.h>
  36. #include <errno.h>
  37. #include <stdlib.h>
  38. #include <limits.h>
  39. #include <stdio.h>
  40. #include <utime.h>
  41. #else
  42. #include "replace.h"
  43. #include "system/filesys.h"
  44. #include "system/time.h"
  45. #include "system/shmem.h"
  46. #include "system/select.h"
  47. #include "system/wait.h"
  48. #endif
  49. #include "tdb1.h"
  50. /* Temporary wrapper to avoid undue churn in test/ */
  51. #define tdb1_error(tdb) ((tdb)->ecode)
  52. /* #define TDB_TRACE 1 */
  53. #ifndef HAVE_GETPAGESIZE
  54. #define getpagesize() 0x2000
  55. #endif
  56. #ifndef __STRING
  57. #define __STRING(x) #x
  58. #endif
  59. #ifndef __STRINGSTRING
  60. #define __STRINGSTRING(x) __STRING(x)
  61. #endif
  62. #ifndef __location__
  63. #define __location__ __FILE__ ":" __STRINGSTRING(__LINE__)
  64. #endif
  65. typedef uint32_t tdb1_len_t;
  66. typedef uint32_t tdb1_off_t;
  67. #ifndef offsetof
  68. #define offsetof(t,f) ((unsigned int)&((t *)0)->f)
  69. #endif
  70. #define TDB1_MAGIC_FOOD "TDB file\n"
  71. #define TDB1_VERSION (0x26011967 + 6)
  72. #define TDB1_MAGIC (0x26011999U)
  73. #define TDB1_FREE_MAGIC (~TDB1_MAGIC)
  74. #define TDB1_DEAD_MAGIC (0xFEE1DEAD)
  75. #define TDB1_RECOVERY_MAGIC (0xf53bc0e7U)
  76. #define TDB1_RECOVERY_INVALID_MAGIC (0x0)
  77. #define TDB1_HASH_RWLOCK_MAGIC (0xbad1a51U)
  78. #define TDB1_ALIGNMENT 4
  79. #define TDB1_DEFAULT_HASH_SIZE 131
  80. #define TDB1_FREELIST_TOP (sizeof(struct tdb1_header))
  81. #define TDB1_ALIGN(x,a) (((x) + (a)-1) & ~((a)-1))
  82. #define TDB1_BYTEREV(x) (((((x)&0xff)<<24)|((x)&0xFF00)<<8)|(((x)>>8)&0xFF00)|((x)>>24))
  83. #define TDB1_DEAD(r) ((r)->magic == TDB1_DEAD_MAGIC)
  84. #define TDB1_BAD_MAGIC(r) ((r)->magic != TDB1_MAGIC && !TDB1_DEAD(r))
  85. #define TDB1_HASH_TOP(hash) (TDB1_FREELIST_TOP + (TDB1_BUCKET(hash)+1)*sizeof(tdb1_off_t))
  86. #define TDB1_HASHTABLE_SIZE(tdb) ((tdb->header.hash_size+1)*sizeof(tdb1_off_t))
  87. #define TDB1_DATA_START(hash_size) (TDB1_HASH_TOP(hash_size-1) + sizeof(tdb1_off_t))
  88. #define TDB1_RECOVERY_HEAD offsetof(struct tdb1_header, recovery_start)
  89. #define TDB1_SEQNUM_OFS offsetof(struct tdb1_header, sequence_number)
  90. #define TDB1_PAD_BYTE 0x42
  91. #define TDB1_PAD_U32 0x42424242
  92. /* NB assumes there is a local variable called "tdb" that is the
  93. * current context, also takes doubly-parenthesized print-style
  94. * argument. */
  95. #define TDB1_LOG(x) tdb->log.log_fn x
  96. /* lock offsets */
  97. #define TDB1_OPEN_LOCK 0
  98. #define TDB1_ACTIVE_LOCK 4
  99. #define TDB1_TRANSACTION_LOCK 8
  100. /* free memory if the pointer is valid and zero the pointer */
  101. #ifndef SAFE_FREE
  102. #define SAFE_FREE(x) do { if ((x) != NULL) {free(x); (x)=NULL;} } while(0)
  103. #endif
  104. #define TDB1_BUCKET(hash) ((hash) % tdb->header.hash_size)
  105. #define TDB1_DOCONV() (tdb->flags & TDB1_CONVERT)
  106. #define TDB1_CONV(x) (TDB1_DOCONV() ? tdb1_convert(&x, sizeof(x)) : &x)
  107. /* the body of the database is made of one tdb1_record for the free space
  108. plus a separate data list for each hash value */
  109. struct tdb1_record {
  110. tdb1_off_t next; /* offset of the next record in the list */
  111. tdb1_len_t rec_len; /* total byte length of record */
  112. tdb1_len_t key_len; /* byte length of key */
  113. tdb1_len_t data_len; /* byte length of data */
  114. uint32_t full_hash; /* the full 32 bit hash of the key */
  115. uint32_t magic; /* try to catch errors */
  116. /* the following union is implied:
  117. union {
  118. char record[rec_len];
  119. struct {
  120. char key[key_len];
  121. char data[data_len];
  122. }
  123. uint32_t totalsize; (tailer)
  124. }
  125. */
  126. };
  127. /* this is stored at the front of every database */
  128. struct tdb1_header {
  129. char magic_food[32]; /* for /etc/magic */
  130. uint32_t version; /* version of the code */
  131. uint32_t hash_size; /* number of hash entries */
  132. tdb1_off_t rwlocks; /* obsolete - kept to detect old formats */
  133. tdb1_off_t recovery_start; /* offset of transaction recovery region */
  134. tdb1_off_t sequence_number; /* used when TDB1_SEQNUM is set */
  135. uint32_t magic1_hash; /* hash of TDB1_MAGIC_FOOD. */
  136. uint32_t magic2_hash; /* hash of TDB1_MAGIC. */
  137. tdb1_off_t reserved[27];
  138. };
  139. struct tdb1_lock_type {
  140. uint32_t off;
  141. uint32_t count;
  142. uint32_t ltype;
  143. };
  144. struct tdb1_traverse_lock {
  145. struct tdb1_traverse_lock *next;
  146. uint32_t off;
  147. uint32_t hash;
  148. int lock_rw;
  149. };
  150. enum tdb1_lock_flags {
  151. /* WAIT == F_SETLKW, NOWAIT == F_SETLK */
  152. TDB1_LOCK_NOWAIT = 0,
  153. TDB1_LOCK_WAIT = 1,
  154. /* If set, don't log an error on failure. */
  155. TDB1_LOCK_PROBE = 2,
  156. };
  157. struct tdb1_context;
  158. struct tdb1_methods {
  159. int (*tdb1_read)(struct tdb1_context *, tdb1_off_t , void *, tdb1_len_t , int );
  160. int (*tdb1_write)(struct tdb1_context *, tdb1_off_t, const void *, tdb1_len_t);
  161. void (*next_hash_chain)(struct tdb1_context *, uint32_t *);
  162. int (*tdb1_oob)(struct tdb1_context *, tdb1_off_t , int );
  163. int (*tdb1_expand_file)(struct tdb1_context *, tdb1_off_t , tdb1_off_t );
  164. };
  165. struct tdb1_context {
  166. char *name; /* the name of the database */
  167. void *map_ptr; /* where it is currently mapped */
  168. int fd; /* open file descriptor for the database */
  169. tdb1_len_t map_size; /* how much space has been mapped */
  170. int read_only; /* opened read-only */
  171. int traverse_read; /* read-only traversal */
  172. int traverse_write; /* read-write traversal */
  173. struct tdb1_lock_type allrecord_lock; /* .offset == upgradable */
  174. int num_lockrecs;
  175. struct tdb1_lock_type *lockrecs; /* only real locks, all with count>0 */
  176. enum TDB1_ERROR ecode; /* error code for last tdb error */
  177. struct tdb1_header header; /* a cached copy of the header */
  178. uint32_t flags; /* the flags passed to tdb1_open */
  179. struct tdb1_traverse_lock travlocks; /* current traversal locks */
  180. struct tdb1_context *next; /* all tdbs to avoid multiple opens */
  181. dev_t device; /* uniquely identifies this tdb */
  182. ino_t inode; /* uniquely identifies this tdb */
  183. struct tdb1_logging_context log;
  184. unsigned int (*hash_fn)(TDB1_DATA *key);
  185. int open_flags; /* flags used in the open - needed by reopen */
  186. const struct tdb1_methods *methods;
  187. struct tdb1_transaction *transaction;
  188. int page_size;
  189. int max_dead_records;
  190. };
  191. /*
  192. internal prototypes
  193. */
  194. int tdb1_munmap(struct tdb1_context *tdb);
  195. void tdb1_mmap(struct tdb1_context *tdb);
  196. int tdb1_lock(struct tdb1_context *tdb, int list, int ltype);
  197. int tdb1_nest_lock(struct tdb1_context *tdb, uint32_t offset, int ltype,
  198. enum tdb1_lock_flags flags);
  199. int tdb1_nest_unlock(struct tdb1_context *tdb, uint32_t offset, int ltype);
  200. int tdb1_unlock(struct tdb1_context *tdb, int list, int ltype);
  201. int tdb1_brlock(struct tdb1_context *tdb,
  202. int rw_type, tdb1_off_t offset, size_t len,
  203. enum tdb1_lock_flags flags);
  204. int tdb1_brunlock(struct tdb1_context *tdb,
  205. int rw_type, tdb1_off_t offset, size_t len);
  206. bool tdb1_have_extra_locks(struct tdb1_context *tdb);
  207. void tdb1_release_transaction_locks(struct tdb1_context *tdb);
  208. int tdb1_transaction_lock(struct tdb1_context *tdb, int ltype,
  209. enum tdb1_lock_flags lockflags);
  210. int tdb1_transaction_unlock(struct tdb1_context *tdb, int ltype);
  211. int tdb1_recovery_area(struct tdb1_context *tdb,
  212. const struct tdb1_methods *methods,
  213. tdb1_off_t *recovery_offset,
  214. struct tdb1_record *rec);
  215. int tdb1_allrecord_lock(struct tdb1_context *tdb, int ltype,
  216. enum tdb1_lock_flags flags, bool upgradable);
  217. int tdb1_allrecord_unlock(struct tdb1_context *tdb, int ltype);
  218. int tdb1_allrecord_upgrade(struct tdb1_context *tdb);
  219. int tdb1_write_lock_record(struct tdb1_context *tdb, tdb1_off_t off);
  220. int tdb1_write_unlock_record(struct tdb1_context *tdb, tdb1_off_t off);
  221. int tdb1_ofs_read(struct tdb1_context *tdb, tdb1_off_t offset, tdb1_off_t *d);
  222. int tdb1_ofs_write(struct tdb1_context *tdb, tdb1_off_t offset, tdb1_off_t *d);
  223. void *tdb1_convert(void *buf, uint32_t size);
  224. int tdb1_free(struct tdb1_context *tdb, tdb1_off_t offset, struct tdb1_record *rec);
  225. tdb1_off_t tdb1_allocate(struct tdb1_context *tdb, tdb1_len_t length, struct tdb1_record *rec);
  226. int tdb1_ofs_read(struct tdb1_context *tdb, tdb1_off_t offset, tdb1_off_t *d);
  227. int tdb1_ofs_write(struct tdb1_context *tdb, tdb1_off_t offset, tdb1_off_t *d);
  228. int tdb1_lock_record(struct tdb1_context *tdb, tdb1_off_t off);
  229. int tdb1_unlock_record(struct tdb1_context *tdb, tdb1_off_t off);
  230. bool tdb1_needs_recovery(struct tdb1_context *tdb);
  231. int tdb1_rec_read(struct tdb1_context *tdb, tdb1_off_t offset, struct tdb1_record *rec);
  232. int tdb1_rec_write(struct tdb1_context *tdb, tdb1_off_t offset, struct tdb1_record *rec);
  233. int tdb1_do_delete(struct tdb1_context *tdb, tdb1_off_t rec_ptr, struct tdb1_record *rec);
  234. unsigned char *tdb1_alloc_read(struct tdb1_context *tdb, tdb1_off_t offset, tdb1_len_t len);
  235. int tdb1_parse_data(struct tdb1_context *tdb, TDB1_DATA key,
  236. tdb1_off_t offset, tdb1_len_t len,
  237. int (*parser)(TDB1_DATA key, TDB1_DATA data,
  238. void *private_data),
  239. void *private_data);
  240. tdb1_off_t tdb1_find_lock_hash(struct tdb1_context *tdb, TDB1_DATA key, uint32_t hash, int locktype,
  241. struct tdb1_record *rec);
  242. void tdb1_io_init(struct tdb1_context *tdb);
  243. int tdb1_expand(struct tdb1_context *tdb, tdb1_off_t size);
  244. int tdb1_rec_free_read(struct tdb1_context *tdb, tdb1_off_t off,
  245. struct tdb1_record *rec);
  246. bool tdb1_write_all(int fd, const void *buf, size_t count);
  247. int tdb1_transaction_recover(struct tdb1_context *tdb);
  248. void tdb1_header_hash(struct tdb1_context *tdb,
  249. uint32_t *magic1_hash, uint32_t *magic2_hash);
  250. unsigned int tdb1_old_hash(TDB1_DATA *key);
  251. size_t tdb1_dead_space(struct tdb1_context *tdb, tdb1_off_t off);
  252. #endif /* CCAN_TDB2_TDB1_PRIVATE_H */