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/>.
33 /* Temporary wrapper to avoid undue churn in test/ */
34 #define tdb1_error(tdb) ((tdb)->ecode)
36 /* #define TDB_TRACE 1 */
37 #ifndef HAVE_GETPAGESIZE
38 #define getpagesize() 0x2000
42 #define __STRING(x) #x
45 #ifndef __STRINGSTRING
46 #define __STRINGSTRING(x) __STRING(x)
50 #define __location__ __FILE__ ":" __STRINGSTRING(__LINE__)
53 typedef uint32_t tdb1_len_t;
54 typedef uint32_t tdb1_off_t;
57 #define offsetof(t,f) ((unsigned int)&((t *)0)->f)
60 #define TDB1_MAGIC_FOOD "TDB file\n"
61 #define TDB1_VERSION (0x26011967 + 6)
62 #define TDB1_MAGIC (0x26011999U)
63 #define TDB1_FREE_MAGIC (~TDB1_MAGIC)
64 #define TDB1_DEAD_MAGIC (0xFEE1DEAD)
65 #define TDB1_RECOVERY_MAGIC (0xf53bc0e7U)
66 #define TDB1_RECOVERY_INVALID_MAGIC (0x0)
67 #define TDB1_HASH_RWLOCK_MAGIC (0xbad1a51U)
68 #define TDB1_ALIGNMENT 4
69 #define TDB1_DEFAULT_HASH_SIZE 131
70 #define TDB1_FREELIST_TOP (sizeof(struct tdb1_header))
71 #define TDB1_ALIGN(x,a) (((x) + (a)-1) & ~((a)-1))
72 #define TDB1_BYTEREV(x) (((((x)&0xff)<<24)|((x)&0xFF00)<<8)|(((x)>>8)&0xFF00)|((x)>>24))
73 #define TDB1_DEAD(r) ((r)->magic == TDB1_DEAD_MAGIC)
74 #define TDB1_BAD_MAGIC(r) ((r)->magic != TDB1_MAGIC && !TDB1_DEAD(r))
75 #define TDB1_HASH_TOP(hash) (TDB1_FREELIST_TOP + (TDB1_BUCKET(hash)+1)*sizeof(tdb1_off_t))
76 #define TDB1_HASHTABLE_SIZE(tdb) ((tdb->header.hash_size+1)*sizeof(tdb1_off_t))
77 #define TDB1_DATA_START(hash_size) (TDB1_HASH_TOP(hash_size-1) + sizeof(tdb1_off_t))
78 #define TDB1_RECOVERY_HEAD offsetof(struct tdb1_header, recovery_start)
79 #define TDB1_SEQNUM_OFS offsetof(struct tdb1_header, sequence_number)
80 #define TDB1_PAD_BYTE 0x42
81 #define TDB1_PAD_U32 0x42424242
83 /* NB assumes there is a local variable called "tdb" that is the
84 * current context, also takes doubly-parenthesized print-style
86 #define TDB1_LOG(x) tdb->log.log_fn x
89 #define TDB1_OPEN_LOCK 0
90 #define TDB1_ACTIVE_LOCK 4
91 #define TDB1_TRANSACTION_LOCK 8
93 /* free memory if the pointer is valid and zero the pointer */
95 #define SAFE_FREE(x) do { if ((x) != NULL) {free(x); (x)=NULL;} } while(0)
98 #define TDB1_BUCKET(hash) ((hash) % tdb->header.hash_size)
100 #define TDB1_DOCONV() (tdb->flags & TDB1_CONVERT)
101 #define TDB1_CONV(x) (TDB1_DOCONV() ? tdb1_convert(&x, sizeof(x)) : &x)
103 /* the body of the database is made of one tdb1_record for the free space
104 plus a separate data list for each hash value */
106 tdb1_off_t next; /* offset of the next record in the list */
107 tdb1_len_t rec_len; /* total byte length of record */
108 tdb1_len_t key_len; /* byte length of key */
109 tdb1_len_t data_len; /* byte length of data */
110 uint32_t full_hash; /* the full 32 bit hash of the key */
111 uint32_t magic; /* try to catch errors */
112 /* the following union is implied:
114 char record[rec_len];
119 uint32_t totalsize; (tailer)
125 /* this is stored at the front of every database */
127 char magic_food[32]; /* for /etc/magic */
128 uint32_t version; /* version of the code */
129 uint32_t hash_size; /* number of hash entries */
130 tdb1_off_t rwlocks; /* obsolete - kept to detect old formats */
131 tdb1_off_t recovery_start; /* offset of transaction recovery region */
132 tdb1_off_t sequence_number; /* used when TDB1_SEQNUM is set */
133 uint32_t magic1_hash; /* hash of TDB1_MAGIC_FOOD. */
134 uint32_t magic2_hash; /* hash of TDB1_MAGIC. */
135 tdb1_off_t reserved[27];
138 struct tdb1_lock_type {
144 struct tdb1_traverse_lock {
145 struct tdb1_traverse_lock *next;
151 enum tdb1_lock_flags {
152 /* WAIT == F_SETLKW, NOWAIT == F_SETLK */
153 TDB1_LOCK_NOWAIT = 0,
155 /* If set, don't log an error on failure. */
161 struct tdb1_methods {
162 int (*tdb1_read)(struct tdb1_context *, tdb1_off_t , void *, tdb1_len_t , int );
163 int (*tdb1_write)(struct tdb1_context *, tdb1_off_t, const void *, tdb1_len_t);
164 void (*next_hash_chain)(struct tdb1_context *, uint32_t *);
165 int (*tdb1_oob)(struct tdb1_context *, tdb1_off_t , int );
166 int (*tdb1_expand_file)(struct tdb1_context *, tdb1_off_t , tdb1_off_t );
169 struct tdb1_context {
170 char *name; /* the name of the database */
171 void *map_ptr; /* where it is currently mapped */
172 int fd; /* open file descriptor for the database */
173 tdb1_len_t map_size; /* how much space has been mapped */
174 int read_only; /* opened read-only */
175 int traverse_read; /* read-only traversal */
176 int traverse_write; /* read-write traversal */
177 struct tdb1_lock_type allrecord_lock; /* .offset == upgradable */
179 struct tdb1_lock_type *lockrecs; /* only real locks, all with count>0 */
180 enum TDB1_ERROR ecode; /* error code for last tdb error */
181 struct tdb1_header header; /* a cached copy of the header */
182 uint32_t flags; /* the flags passed to tdb1_open */
183 struct tdb1_traverse_lock travlocks; /* current traversal locks */
184 struct tdb1_context *next; /* all tdbs to avoid multiple opens */
185 dev_t device; /* uniquely identifies this tdb */
186 ino_t inode; /* uniquely identifies this tdb */
187 struct tdb1_logging_context log;
188 unsigned int (*hash_fn)(TDB1_DATA *key);
189 int open_flags; /* flags used in the open - needed by reopen */
190 const struct tdb1_methods *methods;
191 struct tdb1_transaction *transaction;
193 int max_dead_records;
199 int tdb1_munmap(struct tdb1_context *tdb);
200 void tdb1_mmap(struct tdb1_context *tdb);
201 int tdb1_lock(struct tdb1_context *tdb, int list, int ltype);
202 int tdb1_nest_lock(struct tdb1_context *tdb, uint32_t offset, int ltype,
203 enum tdb1_lock_flags flags);
204 int tdb1_nest_unlock(struct tdb1_context *tdb, uint32_t offset, int ltype);
205 int tdb1_unlock(struct tdb1_context *tdb, int list, int ltype);
206 int tdb1_brlock(struct tdb1_context *tdb,
207 int rw_type, tdb1_off_t offset, size_t len,
208 enum tdb1_lock_flags flags);
209 int tdb1_brunlock(struct tdb1_context *tdb,
210 int rw_type, tdb1_off_t offset, size_t len);
211 bool tdb1_have_extra_locks(struct tdb1_context *tdb);
212 void tdb1_release_transaction_locks(struct tdb1_context *tdb);
213 int tdb1_transaction_lock(struct tdb1_context *tdb, int ltype,
214 enum tdb1_lock_flags lockflags);
215 int tdb1_transaction_unlock(struct tdb1_context *tdb, int ltype);
216 int tdb1_recovery_area(struct tdb1_context *tdb,
217 const struct tdb1_methods *methods,
218 tdb1_off_t *recovery_offset,
219 struct tdb1_record *rec);
220 int tdb1_allrecord_lock(struct tdb1_context *tdb, int ltype,
221 enum tdb1_lock_flags flags, bool upgradable);
222 int tdb1_allrecord_unlock(struct tdb1_context *tdb, int ltype);
223 int tdb1_allrecord_upgrade(struct tdb1_context *tdb);
224 int tdb1_write_lock_record(struct tdb1_context *tdb, tdb1_off_t off);
225 int tdb1_write_unlock_record(struct tdb1_context *tdb, tdb1_off_t off);
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 void *tdb1_convert(void *buf, uint32_t size);
229 int tdb1_free(struct tdb1_context *tdb, tdb1_off_t offset, struct tdb1_record *rec);
230 tdb1_off_t tdb1_allocate(struct tdb1_context *tdb, tdb1_len_t length, struct tdb1_record *rec);
231 int tdb1_ofs_read(struct tdb1_context *tdb, tdb1_off_t offset, tdb1_off_t *d);
232 int tdb1_ofs_write(struct tdb1_context *tdb, tdb1_off_t offset, tdb1_off_t *d);
233 int tdb1_lock_record(struct tdb1_context *tdb, tdb1_off_t off);
234 int tdb1_unlock_record(struct tdb1_context *tdb, tdb1_off_t off);
235 bool tdb1_needs_recovery(struct tdb1_context *tdb);
236 int tdb1_rec_read(struct tdb1_context *tdb, tdb1_off_t offset, struct tdb1_record *rec);
237 int tdb1_rec_write(struct tdb1_context *tdb, tdb1_off_t offset, struct tdb1_record *rec);
238 int tdb1_do_delete(struct tdb1_context *tdb, tdb1_off_t rec_ptr, struct tdb1_record *rec);
239 unsigned char *tdb1_alloc_read(struct tdb1_context *tdb, tdb1_off_t offset, tdb1_len_t len);
240 int tdb1_parse_data(struct tdb1_context *tdb, TDB1_DATA key,
241 tdb1_off_t offset, tdb1_len_t len,
242 int (*parser)(TDB1_DATA key, TDB1_DATA data,
245 tdb1_off_t tdb1_find_lock_hash(struct tdb1_context *tdb, TDB1_DATA key, uint32_t hash, int locktype,
246 struct tdb1_record *rec);
247 void tdb1_io_init(struct tdb1_context *tdb);
248 int tdb1_expand(struct tdb1_context *tdb, tdb1_off_t size);
249 int tdb1_rec_free_read(struct tdb1_context *tdb, tdb1_off_t off,
250 struct tdb1_record *rec);
251 bool tdb1_write_all(int fd, const void *buf, size_t count);
252 int tdb1_transaction_recover(struct tdb1_context *tdb);
253 void tdb1_header_hash(struct tdb1_context *tdb,
254 uint32_t *magic1_hash, uint32_t *magic2_hash);
255 unsigned int tdb1_old_hash(TDB1_DATA *key);
256 size_t tdb1_dead_space(struct tdb1_context *tdb, tdb1_off_t off);
257 #endif /* CCAN_TDB2_TDB1_PRIVATE_H */