tdb: introduce tdb->hdr_ofs
[samba.git] / lib / tdb / common / tdb_private.h
1 #ifndef TDB_PRIVATE_H
2 #define TDB_PRIVATE_H
3  /*
4    Unix SMB/CIFS implementation.
5
6    trivial database library - private includes
7
8    Copyright (C) Andrew Tridgell              2005
9
10      ** NOTE! The following LGPL license applies to the tdb
11      ** library. This does NOT imply that all of Samba is released
12      ** under the LGPL
13
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.
18
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.
23
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/>.
26 */
27
28 #include "replace.h"
29 #include "system/filesys.h"
30 #include "system/time.h"
31 #include "system/shmem.h"
32 #include "system/select.h"
33 #include "system/wait.h"
34 #include "tdb.h"
35
36 /* #define TDB_TRACE 1 */
37 #ifndef HAVE_GETPAGESIZE
38 #define getpagesize() 0x2000
39 #endif
40
41 typedef uint32_t tdb_len_t;
42 typedef uint32_t tdb_off_t;
43
44 #ifndef offsetof
45 #define offsetof(t,f) ((unsigned int)&((t *)0)->f)
46 #endif
47
48 #define TDB_MAGIC_FOOD "TDB file\n"
49 #define TDB_VERSION (0x26011967 + 6)
50 #define TDB_MAGIC (0x26011999U)
51 #define TDB_FREE_MAGIC (~TDB_MAGIC)
52 #define TDB_DEAD_MAGIC (0xFEE1DEAD)
53 #define TDB_RECOVERY_MAGIC (0xf53bc0e7U)
54 #define TDB_RECOVERY_INVALID_MAGIC (0x0)
55 #define TDB_HASH_RWLOCK_MAGIC (0xbad1a51U)
56 #define TDB_FEATURE_FLAG_MAGIC (0xbad1a52U)
57 #define TDB_ALIGNMENT 4
58 #define DEFAULT_HASH_SIZE 131
59 #define FREELIST_TOP (sizeof(struct tdb_header))
60 #define TDB_ALIGN(x,a) (((x) + (a)-1) & ~((a)-1))
61 #define TDB_BYTEREV(x) (((((x)&0xff)<<24)|((x)&0xFF00)<<8)|(((x)>>8)&0xFF00)|((x)>>24))
62 #define TDB_DEAD(r) ((r)->magic == TDB_DEAD_MAGIC)
63 #define TDB_BAD_MAGIC(r) ((r)->magic != TDB_MAGIC && !TDB_DEAD(r))
64 #define TDB_HASH_TOP(hash) (FREELIST_TOP + (BUCKET(hash)+1)*sizeof(tdb_off_t))
65 #define TDB_HASHTABLE_SIZE(tdb) ((tdb->hash_size+1)*sizeof(tdb_off_t))
66 #define TDB_DATA_START(hash_size) (TDB_HASH_TOP(hash_size-1) + sizeof(tdb_off_t))
67 #define TDB_RECOVERY_HEAD offsetof(struct tdb_header, recovery_start)
68 #define TDB_SEQNUM_OFS    offsetof(struct tdb_header, sequence_number)
69 #define TDB_PAD_BYTE 0x42
70 #define TDB_PAD_U32  0x42424242
71
72 #define TDB_SUPPORTED_FEATURE_FLAGS 0
73
74 /* NB assumes there is a local variable called "tdb" that is the
75  * current context, also takes doubly-parenthesized print-style
76  * argument. */
77 #define TDB_LOG(x) tdb->log.log_fn x
78
79 #ifdef TDB_TRACE
80 void tdb_trace(struct tdb_context *tdb, const char *op);
81 void tdb_trace_seqnum(struct tdb_context *tdb, uint32_t seqnum, const char *op);
82 void tdb_trace_open(struct tdb_context *tdb, const char *op,
83                     unsigned hash_size, unsigned tdb_flags, unsigned open_flags);
84 void tdb_trace_ret(struct tdb_context *tdb, const char *op, int ret);
85 void tdb_trace_retrec(struct tdb_context *tdb, const char *op, TDB_DATA ret);
86 void tdb_trace_1rec(struct tdb_context *tdb, const char *op,
87                     TDB_DATA rec);
88 void tdb_trace_1rec_ret(struct tdb_context *tdb, const char *op,
89                         TDB_DATA rec, int ret);
90 void tdb_trace_1rec_retrec(struct tdb_context *tdb, const char *op,
91                            TDB_DATA rec, TDB_DATA ret);
92 void tdb_trace_2rec_flag_ret(struct tdb_context *tdb, const char *op,
93                              TDB_DATA rec1, TDB_DATA rec2, unsigned flag,
94                              int ret);
95 void tdb_trace_2rec_retrec(struct tdb_context *tdb, const char *op,
96                            TDB_DATA rec1, TDB_DATA rec2, TDB_DATA ret);
97 #else
98 #define tdb_trace(tdb, op)
99 #define tdb_trace_seqnum(tdb, seqnum, op)
100 #define tdb_trace_open(tdb, op, hash_size, tdb_flags, open_flags)
101 #define tdb_trace_ret(tdb, op, ret)
102 #define tdb_trace_retrec(tdb, op, ret)
103 #define tdb_trace_1rec(tdb, op, rec)
104 #define tdb_trace_1rec_ret(tdb, op, rec, ret)
105 #define tdb_trace_1rec_retrec(tdb, op, rec, ret)
106 #define tdb_trace_2rec_flag_ret(tdb, op, rec1, rec2, flag, ret)
107 #define tdb_trace_2rec_retrec(tdb, op, rec1, rec2, ret)
108 #endif /* !TDB_TRACE */
109
110 /* lock offsets */
111 #define OPEN_LOCK        0
112 #define ACTIVE_LOCK      4
113 #define TRANSACTION_LOCK 8
114
115 /* free memory if the pointer is valid and zero the pointer */
116 #ifndef SAFE_FREE
117 #define SAFE_FREE(x) do { if ((x) != NULL) {free(x); (x)=NULL;} } while(0)
118 #endif
119
120 #define BUCKET(hash) ((hash) % tdb->hash_size)
121
122 #define DOCONV() (tdb->flags & TDB_CONVERT)
123 #define CONVERT(x) (DOCONV() ? tdb_convert(&x, sizeof(x)) : &x)
124
125
126 /* the body of the database is made of one tdb_record for the free space
127    plus a separate data list for each hash value */
128 struct tdb_record {
129         tdb_off_t next; /* offset of the next record in the list */
130         tdb_len_t rec_len; /* total byte length of record */
131         tdb_len_t key_len; /* byte length of key */
132         tdb_len_t data_len; /* byte length of data */
133         uint32_t full_hash; /* the full 32 bit hash of the key */
134         uint32_t magic;   /* try to catch errors */
135         /* the following union is implied:
136                 union {
137                         char record[rec_len];
138                         struct {
139                                 char key[key_len];
140                                 char data[data_len];
141                         }
142                         uint32_t totalsize; (tailer)
143                 }
144         */
145 };
146
147
148 /* this is stored at the front of every database */
149 struct tdb_header {
150         char magic_food[32]; /* for /etc/magic */
151         uint32_t version; /* version of the code */
152         uint32_t hash_size; /* number of hash entries */
153         tdb_off_t rwlocks; /* obsolete - kept to detect old formats */
154         tdb_off_t recovery_start; /* offset of transaction recovery region */
155         tdb_off_t sequence_number; /* used when TDB_SEQNUM is set */
156         uint32_t magic1_hash; /* hash of TDB_MAGIC_FOOD. */
157         uint32_t magic2_hash; /* hash of TDB_MAGIC. */
158         uint32_t feature_flags;
159         tdb_off_t reserved[26];
160 };
161
162 struct tdb_lock_type {
163         uint32_t off;
164         uint32_t count;
165         uint32_t ltype;
166 };
167
168 struct tdb_traverse_lock {
169         struct tdb_traverse_lock *next;
170         uint32_t off;
171         uint32_t hash;
172         int lock_rw;
173 };
174
175 enum tdb_lock_flags {
176         /* WAIT == F_SETLKW, NOWAIT == F_SETLK */
177         TDB_LOCK_NOWAIT = 0,
178         TDB_LOCK_WAIT = 1,
179         /* If set, don't log an error on failure. */
180         TDB_LOCK_PROBE = 2,
181         /* If set, don't actually lock at all. */
182         TDB_LOCK_MARK_ONLY = 4,
183 };
184
185 struct tdb_methods {
186         int (*tdb_read)(struct tdb_context *, tdb_off_t , void *, tdb_len_t , int );
187         int (*tdb_write)(struct tdb_context *, tdb_off_t, const void *, tdb_len_t);
188         void (*next_hash_chain)(struct tdb_context *, uint32_t *);
189         int (*tdb_oob)(struct tdb_context *, tdb_off_t , tdb_len_t, int );
190         int (*tdb_expand_file)(struct tdb_context *, tdb_off_t , tdb_off_t );
191 };
192
193 struct tdb_context {
194         char *name; /* the name of the database */
195         void *map_ptr; /* where it is currently mapped */
196         int fd; /* open file descriptor for the database */
197         tdb_len_t map_size; /* how much space has been mapped */
198         int read_only; /* opened read-only */
199         int traverse_read; /* read-only traversal */
200         int traverse_write; /* read-write traversal */
201         struct tdb_lock_type allrecord_lock; /* .offset == upgradable */
202         int num_lockrecs;
203         struct tdb_lock_type *lockrecs; /* only real locks, all with count>0 */
204         int lockrecs_array_length;
205
206         tdb_off_t hdr_ofs; /* this is 0 for now */
207
208         enum TDB_ERROR ecode; /* error code for last tdb error */
209         uint32_t hash_size;
210         uint32_t feature_flags;
211         uint32_t flags; /* the flags passed to tdb_open */
212         struct tdb_traverse_lock travlocks; /* current traversal locks */
213         struct tdb_context *next; /* all tdbs to avoid multiple opens */
214         dev_t device;   /* uniquely identifies this tdb */
215         ino_t inode;    /* uniquely identifies this tdb */
216         struct tdb_logging_context log;
217         unsigned int (*hash_fn)(TDB_DATA *key);
218         int open_flags; /* flags used in the open - needed by reopen */
219         const struct tdb_methods *methods;
220         struct tdb_transaction *transaction;
221         int page_size;
222         int max_dead_records;
223 #ifdef TDB_TRACE
224         int tracefd;
225 #endif
226         volatile sig_atomic_t *interrupt_sig_ptr;
227 };
228
229
230 /*
231   internal prototypes
232 */
233 int tdb_munmap(struct tdb_context *tdb);
234 int tdb_mmap(struct tdb_context *tdb);
235 int tdb_lock(struct tdb_context *tdb, int list, int ltype);
236 int tdb_lock_nonblock(struct tdb_context *tdb, int list, int ltype);
237 int tdb_nest_lock(struct tdb_context *tdb, uint32_t offset, int ltype,
238                   enum tdb_lock_flags flags);
239 int tdb_nest_unlock(struct tdb_context *tdb, uint32_t offset, int ltype,
240                     bool mark_lock);
241 int tdb_unlock(struct tdb_context *tdb, int list, int ltype);
242 int tdb_brlock(struct tdb_context *tdb,
243                int rw_type, tdb_off_t offset, size_t len,
244                enum tdb_lock_flags flags);
245 int tdb_brunlock(struct tdb_context *tdb,
246                  int rw_type, tdb_off_t offset, size_t len);
247 bool tdb_have_extra_locks(struct tdb_context *tdb);
248 void tdb_release_transaction_locks(struct tdb_context *tdb);
249 int tdb_transaction_lock(struct tdb_context *tdb, int ltype,
250                          enum tdb_lock_flags lockflags);
251 int tdb_transaction_unlock(struct tdb_context *tdb, int ltype);
252 int tdb_recovery_area(struct tdb_context *tdb,
253                       const struct tdb_methods *methods,
254                       tdb_off_t *recovery_offset,
255                       struct tdb_record *rec);
256 int tdb_allrecord_lock(struct tdb_context *tdb, int ltype,
257                        enum tdb_lock_flags flags, bool upgradable);
258 int tdb_allrecord_unlock(struct tdb_context *tdb, int ltype, bool mark_lock);
259 int tdb_allrecord_upgrade(struct tdb_context *tdb);
260 int tdb_write_lock_record(struct tdb_context *tdb, tdb_off_t off);
261 int tdb_write_unlock_record(struct tdb_context *tdb, tdb_off_t off);
262 int tdb_ofs_read(struct tdb_context *tdb, tdb_off_t offset, tdb_off_t *d);
263 int tdb_ofs_write(struct tdb_context *tdb, tdb_off_t offset, tdb_off_t *d);
264 void *tdb_convert(void *buf, uint32_t size);
265 int tdb_free(struct tdb_context *tdb, tdb_off_t offset, struct tdb_record *rec);
266 tdb_off_t tdb_allocate(struct tdb_context *tdb, int hash, tdb_len_t length,
267                        struct tdb_record *rec);
268 int tdb_ofs_read(struct tdb_context *tdb, tdb_off_t offset, tdb_off_t *d);
269 int tdb_ofs_write(struct tdb_context *tdb, tdb_off_t offset, tdb_off_t *d);
270 int tdb_lock_record(struct tdb_context *tdb, tdb_off_t off);
271 int tdb_unlock_record(struct tdb_context *tdb, tdb_off_t off);
272 bool tdb_needs_recovery(struct tdb_context *tdb);
273 int tdb_rec_read(struct tdb_context *tdb, tdb_off_t offset, struct tdb_record *rec);
274 int tdb_rec_write(struct tdb_context *tdb, tdb_off_t offset, struct tdb_record *rec);
275 int tdb_do_delete(struct tdb_context *tdb, tdb_off_t rec_ptr, struct tdb_record *rec);
276 unsigned char *tdb_alloc_read(struct tdb_context *tdb, tdb_off_t offset, tdb_len_t len);
277 int tdb_parse_data(struct tdb_context *tdb, TDB_DATA key,
278                    tdb_off_t offset, tdb_len_t len,
279                    int (*parser)(TDB_DATA key, TDB_DATA data,
280                                  void *private_data),
281                    void *private_data);
282 tdb_off_t tdb_find_lock_hash(struct tdb_context *tdb, TDB_DATA key, uint32_t hash, int locktype,
283                            struct tdb_record *rec);
284 tdb_off_t tdb_find_dead(struct tdb_context *tdb, uint32_t hash,
285                         struct tdb_record *r, tdb_len_t length,
286                         tdb_off_t *p_last_ptr);
287 int tdb_purge_dead(struct tdb_context *tdb, uint32_t hash);
288 void tdb_io_init(struct tdb_context *tdb);
289 int tdb_expand(struct tdb_context *tdb, tdb_off_t size);
290 tdb_off_t tdb_expand_adjust(tdb_off_t map_size, tdb_off_t size, int page_size);
291 int tdb_rec_free_read(struct tdb_context *tdb, tdb_off_t off,
292                       struct tdb_record *rec);
293 bool tdb_write_all(int fd, const void *buf, size_t count);
294 int tdb_transaction_recover(struct tdb_context *tdb);
295 void tdb_header_hash(struct tdb_context *tdb,
296                      uint32_t *magic1_hash, uint32_t *magic2_hash);
297 unsigned int tdb_old_hash(TDB_DATA *key);
298 size_t tdb_dead_space(struct tdb_context *tdb, tdb_off_t off);
299 bool tdb_add_off_t(tdb_off_t a, tdb_off_t b, tdb_off_t *pret);
300
301 /* tdb_off_t and tdb_len_t right now are both uint32_t */
302 #define tdb_add_len_t tdb_add_off_t
303 #endif /* TDB_PRIVATE_H */