1 #ifndef CCAN_TDB2_TDB1_PRIVATE_H
2 #define CCAN_TDB2_TDB1_PRIVATE_H
4 Unix SMB/CIFS implementation.
6 trivial database library - private includes
8 Copyright (C) Andrew Tridgell 2005
10 ** NOTE! The following LGPL license applies to the tdb
11 ** library. This does NOT imply that all of Samba is released
14 This library is free software; you can redistribute it and/or
15 modify it under the terms of the GNU Lesser General Public
16 License as published by the Free Software Foundation; either
17 version 3 of the License, or (at your option) any later version.
19 This library is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 Lesser General Public License for more details.
24 You should have received a copy of the GNU Lesser General Public
25 License along with this library; if not, see <http://www.gnu.org/licenses/>.
31 /**** FIXME: Type overrides for tdb2, for transition! */
32 #define tdb_logerr(tdb, ecode, level, ...) \
33 tdb_logerr((struct tdb_context *)(tdb), (ecode), (level), __VA_ARGS__)
35 #define tdb_error(tdb) \
36 tdb_error((struct tdb_context *)(tdb))
42 /* #define TDB_TRACE 1 */
43 #ifndef HAVE_GETPAGESIZE
44 #define getpagesize() 0x2000
48 #define __STRING(x) #x
51 #ifndef __STRINGSTRING
52 #define __STRINGSTRING(x) __STRING(x)
56 #define __location__ __FILE__ ":" __STRINGSTRING(__LINE__)
59 typedef uint32_t tdb1_len_t;
60 typedef uint32_t tdb1_off_t;
63 #define offsetof(t,f) ((unsigned int)&((t *)0)->f)
66 #define TDB1_MAGIC_FOOD "TDB file\n"
67 #define TDB1_VERSION (0x26011967 + 6)
68 #define TDB1_MAGIC (0x26011999U)
69 #define TDB1_FREE_MAGIC (~TDB1_MAGIC)
70 #define TDB1_DEAD_MAGIC (0xFEE1DEAD)
71 #define TDB1_RECOVERY_MAGIC (0xf53bc0e7U)
72 #define TDB1_RECOVERY_INVALID_MAGIC (0x0)
73 #define TDB1_HASH_RWLOCK_MAGIC (0xbad1a51U)
74 #define TDB1_ALIGNMENT 4
75 #define TDB1_DEFAULT_HASH_SIZE 131
76 #define TDB1_FREELIST_TOP (sizeof(struct tdb1_header))
77 #define TDB1_ALIGN(x,a) (((x) + (a)-1) & ~((a)-1))
78 #define TDB1_BYTEREV(x) (((((x)&0xff)<<24)|((x)&0xFF00)<<8)|(((x)>>8)&0xFF00)|((x)>>24))
79 #define TDB1_DEAD(r) ((r)->magic == TDB1_DEAD_MAGIC)
80 #define TDB1_BAD_MAGIC(r) ((r)->magic != TDB1_MAGIC && !TDB1_DEAD(r))
81 #define TDB1_HASH_TOP(hash) (TDB1_FREELIST_TOP + (TDB1_BUCKET(hash)+1)*sizeof(tdb1_off_t))
82 #define TDB1_HASHTABLE_SIZE(tdb) ((tdb->header.hash_size+1)*sizeof(tdb1_off_t))
83 #define TDB1_DATA_START(hash_size) (TDB1_HASH_TOP(hash_size-1) + sizeof(tdb1_off_t))
84 #define TDB1_RECOVERY_HEAD offsetof(struct tdb1_header, recovery_start)
85 #define TDB1_SEQNUM_OFS offsetof(struct tdb1_header, sequence_number)
86 #define TDB1_PAD_BYTE 0x42
87 #define TDB1_PAD_U32 0x42424242
90 #define TDB1_OPEN_LOCK 0
91 #define TDB1_ACTIVE_LOCK 4
92 #define TDB1_TRANSACTION_LOCK 8
94 /* free memory if the pointer is valid and zero the pointer */
96 #define SAFE_FREE(x) do { if ((x) != NULL) {free(x); (x)=NULL;} } while(0)
99 #define TDB1_BUCKET(hash) ((hash) % tdb->header.hash_size)
101 #define TDB1_DOCONV() (tdb->flags & TDB1_CONVERT)
102 #define TDB1_CONV(x) (TDB1_DOCONV() ? tdb1_convert(&x, sizeof(x)) : &x)
104 /* the body of the database is made of one tdb1_record for the free space
105 plus a separate data list for each hash value */
107 tdb1_off_t next; /* offset of the next record in the list */
108 tdb1_len_t rec_len; /* total byte length of record */
109 tdb1_len_t key_len; /* byte length of key */
110 tdb1_len_t data_len; /* byte length of data */
111 uint32_t full_hash; /* the full 32 bit hash of the key */
112 uint32_t magic; /* try to catch errors */
113 /* the following union is implied:
115 char record[rec_len];
120 uint32_t totalsize; (tailer)
126 /* this is stored at the front of every database */
128 char magic_food[32]; /* for /etc/magic */
129 uint32_t version; /* version of the code */
130 uint32_t hash_size; /* number of hash entries */
131 tdb1_off_t rwlocks; /* obsolete - kept to detect old formats */
132 tdb1_off_t recovery_start; /* offset of transaction recovery region */
133 tdb1_off_t sequence_number; /* used when TDB1_SEQNUM is set */
134 uint32_t magic1_hash; /* hash of TDB1_MAGIC_FOOD. */
135 uint32_t magic2_hash; /* hash of TDB1_MAGIC. */
136 tdb1_off_t reserved[27];
139 struct tdb1_lock_type {
145 struct tdb1_traverse_lock {
146 struct tdb1_traverse_lock *next;
152 enum tdb1_lock_flags {
153 /* WAIT == F_SETLKW, NOWAIT == F_SETLK */
154 TDB1_LOCK_NOWAIT = 0,
156 /* If set, don't log an error on failure. */
162 struct tdb1_methods {
163 int (*tdb1_read)(struct tdb1_context *, tdb1_off_t , void *, tdb1_len_t , int );
164 int (*tdb1_write)(struct tdb1_context *, tdb1_off_t, const void *, tdb1_len_t);
165 void (*next_hash_chain)(struct tdb1_context *, uint32_t *);
166 int (*tdb1_oob)(struct tdb1_context *, tdb1_off_t , int );
167 int (*tdb1_expand_file)(struct tdb1_context *, tdb1_off_t , tdb1_off_t );
170 struct tdb1_context {
171 struct tdb1_context *next;
173 char *name; /* the name of the database */
175 /* Logging function */
176 void (*log_fn)(struct tdb1_context *tdb,
177 enum tdb_log_level level,
178 enum TDB_ERROR ecode,
183 /* Last error we returned. */
184 enum TDB_ERROR last_error; /* error code for last tdb error */
186 void *map_ptr; /* where it is currently mapped */
187 int fd; /* open file descriptor for the database */
188 tdb1_len_t map_size; /* how much space has been mapped */
189 int read_only; /* opened read-only */
190 int traverse_read; /* read-only traversal */
191 int traverse_write; /* read-write traversal */
192 struct tdb1_lock_type allrecord_lock; /* .offset == upgradable */
194 struct tdb1_lock_type *lockrecs; /* only real locks, all with count>0 */
195 struct tdb1_header header; /* a cached copy of the header */
196 uint32_t flags; /* the flags passed to tdb1_open */
197 struct tdb1_traverse_lock travlocks; /* current traversal locks */
198 dev_t device; /* uniquely identifies this tdb */
199 ino_t inode; /* uniquely identifies this tdb */
200 unsigned int (*hash_fn)(TDB1_DATA *key);
201 int open_flags; /* flags used in the open - needed by reopen */
202 const struct tdb1_methods *methods;
203 struct tdb1_transaction *transaction;
205 int max_dead_records;
211 int tdb1_munmap(struct tdb1_context *tdb);
212 void tdb1_mmap(struct tdb1_context *tdb);
213 int tdb1_lock(struct tdb1_context *tdb, int list, int ltype);
214 int tdb1_nest_lock(struct tdb1_context *tdb, uint32_t offset, int ltype,
215 enum tdb1_lock_flags flags);
216 int tdb1_nest_unlock(struct tdb1_context *tdb, uint32_t offset, int ltype);
217 int tdb1_unlock(struct tdb1_context *tdb, int list, int ltype);
218 int tdb1_brlock(struct tdb1_context *tdb,
219 int rw_type, tdb1_off_t offset, size_t len,
220 enum tdb1_lock_flags flags);
221 int tdb1_brunlock(struct tdb1_context *tdb,
222 int rw_type, tdb1_off_t offset, size_t len);
223 bool tdb1_have_extra_locks(struct tdb1_context *tdb);
224 void tdb1_release_transaction_locks(struct tdb1_context *tdb);
225 int tdb1_transaction_lock(struct tdb1_context *tdb, int ltype,
226 enum tdb1_lock_flags lockflags);
227 int tdb1_transaction_unlock(struct tdb1_context *tdb, int ltype);
228 int tdb1_recovery_area(struct tdb1_context *tdb,
229 const struct tdb1_methods *methods,
230 tdb1_off_t *recovery_offset,
231 struct tdb1_record *rec);
232 int tdb1_allrecord_lock(struct tdb1_context *tdb, int ltype,
233 enum tdb1_lock_flags flags, bool upgradable);
234 int tdb1_allrecord_unlock(struct tdb1_context *tdb, int ltype);
235 int tdb1_allrecord_upgrade(struct tdb1_context *tdb);
236 int tdb1_write_lock_record(struct tdb1_context *tdb, tdb1_off_t off);
237 int tdb1_write_unlock_record(struct tdb1_context *tdb, tdb1_off_t off);
238 int tdb1_ofs_read(struct tdb1_context *tdb, tdb1_off_t offset, tdb1_off_t *d);
239 int tdb1_ofs_write(struct tdb1_context *tdb, tdb1_off_t offset, tdb1_off_t *d);
240 void *tdb1_convert(void *buf, uint32_t size);
241 int tdb1_free(struct tdb1_context *tdb, tdb1_off_t offset, struct tdb1_record *rec);
242 tdb1_off_t tdb1_allocate(struct tdb1_context *tdb, tdb1_len_t length, struct tdb1_record *rec);
243 int tdb1_ofs_read(struct tdb1_context *tdb, tdb1_off_t offset, tdb1_off_t *d);
244 int tdb1_ofs_write(struct tdb1_context *tdb, tdb1_off_t offset, tdb1_off_t *d);
245 int tdb1_lock_record(struct tdb1_context *tdb, tdb1_off_t off);
246 int tdb1_unlock_record(struct tdb1_context *tdb, tdb1_off_t off);
247 bool tdb1_needs_recovery(struct tdb1_context *tdb);
248 int tdb1_rec_read(struct tdb1_context *tdb, tdb1_off_t offset, struct tdb1_record *rec);
249 int tdb1_rec_write(struct tdb1_context *tdb, tdb1_off_t offset, struct tdb1_record *rec);
250 int tdb1_do_delete(struct tdb1_context *tdb, tdb1_off_t rec_ptr, struct tdb1_record *rec);
251 unsigned char *tdb1_alloc_read(struct tdb1_context *tdb, tdb1_off_t offset, tdb1_len_t len);
252 int tdb1_parse_data(struct tdb1_context *tdb, TDB1_DATA key,
253 tdb1_off_t offset, tdb1_len_t len,
254 int (*parser)(TDB1_DATA key, TDB1_DATA data,
257 tdb1_off_t tdb1_find_lock_hash(struct tdb1_context *tdb, TDB1_DATA key, uint32_t hash, int locktype,
258 struct tdb1_record *rec);
259 void tdb1_io_init(struct tdb1_context *tdb);
260 int tdb1_expand(struct tdb1_context *tdb, tdb1_off_t size);
261 int tdb1_rec_free_read(struct tdb1_context *tdb, tdb1_off_t off,
262 struct tdb1_record *rec);
263 bool tdb1_write_all(int fd, const void *buf, size_t count);
264 int tdb1_transaction_recover(struct tdb1_context *tdb);
265 void tdb1_header_hash(struct tdb1_context *tdb,
266 uint32_t *magic1_hash, uint32_t *magic2_hash);
267 unsigned int tdb1_old_hash(TDB1_DATA *key);
268 size_t tdb1_dead_space(struct tdb1_context *tdb, tdb1_off_t off);
269 #endif /* CCAN_TDB2_TDB1_PRIVATE_H */