]> git.ozlabs.org Git - ccan/blob - ccan/tdb2/tdb1_private.h
22a6f7972f2c89219f4a7eb99af00087f0634d98
[ccan] / ccan / tdb2 / tdb1_private.h
1 #ifndef CCAN_TDB2_TDB1_PRIVATE_H
2 #define CCAN_TDB2_TDB1_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 "private.h"
29 #include "tdb1.h"
30
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__)
34
35 #define tdb_error(tdb) \
36         tdb_error((struct tdb_context *)(tdb))
37
38 #define tdb_brlock(tdb1, rw_type, offset, len, flags)   \
39         tdb_brlock((struct tdb_context *)(tdb1),        \
40                    (rw_type), (offset), (len), (flags))
41
42 #define tdb_brunlock(tdb1, rw_type, offset, len) \
43         tdb_brunlock((struct tdb_context *)(tdb1), (rw_type), (offset), (len))
44
45 #define tdb_nest_lock(tdb1, offset, ltype, flags)       \
46         tdb_nest_lock((struct tdb_context *)(tdb1), (offset), (ltype), (flags))
47
48 #define tdb_nest_unlock(tdb1, offset, ltype)    \
49         tdb_nest_unlock((struct tdb_context *)(tdb1), (offset), (ltype))
50
51 #define tdb_allrecord_lock(tdb1, offset, flags, upgradable)             \
52         tdb_allrecord_lock((struct tdb_context *)(tdb1),                \
53                            (offset), (flags), (upgradable))
54
55 #define tdb_allrecord_unlock(tdb1, ltype)                               \
56         tdb_allrecord_unlock((struct tdb_context *)(tdb1), (ltype))
57
58 #define tdb_allrecord_upgrade(tdb1, start)                              \
59         tdb_allrecord_upgrade((struct tdb_context *)(tdb1), (start))
60
61 #define tdb_hash(tdb1, ptr, len) \
62         tdb_hash((struct tdb_context *)(tdb1), (ptr), (len))
63
64 #define tdb_lock_gradual(tdb1, ltype, flags, off, len)  \
65         tdb_lock_gradual((struct tdb_context *)(tdb1),  \
66                          (ltype), (flags), (off), (len))
67 /***** END FIXME ***/
68
69 #include <limits.h>
70
71 /* #define TDB_TRACE 1 */
72 #ifndef HAVE_GETPAGESIZE
73 #define getpagesize() 0x2000
74 #endif
75
76 #ifndef __STRING
77 #define __STRING(x)    #x
78 #endif
79
80 #ifndef __STRINGSTRING
81 #define __STRINGSTRING(x) __STRING(x)
82 #endif
83
84 #ifndef __location__
85 #define __location__ __FILE__ ":" __STRINGSTRING(__LINE__)
86 #endif
87
88 typedef uint32_t tdb1_len_t;
89 typedef uint32_t tdb1_off_t;
90
91 #ifndef offsetof
92 #define offsetof(t,f) ((unsigned int)&((t *)0)->f)
93 #endif
94
95 #define TDB1_VERSION (0x26011967 + 6)
96 #define TDB1_MAGIC (0x26011999U)
97 #define TDB1_FREE_MAGIC (~TDB1_MAGIC)
98 #define TDB1_DEAD_MAGIC (0xFEE1DEAD)
99 #define TDB1_RECOVERY_MAGIC (0xf53bc0e7U)
100 #define TDB1_RECOVERY_INVALID_MAGIC (0x0)
101 #define TDB1_HASH_RWLOCK_MAGIC (0xbad1a51U)
102 #define TDB1_ALIGNMENT 4
103 #define TDB1_DEFAULT_HASH_SIZE 131
104 #define TDB1_FREELIST_TOP (sizeof(struct tdb1_header))
105 #define TDB1_ALIGN(x,a) (((x) + (a)-1) & ~((a)-1))
106 #define TDB1_BYTEREV(x) (((((x)&0xff)<<24)|((x)&0xFF00)<<8)|(((x)>>8)&0xFF00)|((x)>>24))
107 #define TDB1_DEAD(r) ((r)->magic == TDB1_DEAD_MAGIC)
108 #define TDB1_BAD_MAGIC(r) ((r)->magic != TDB1_MAGIC && !TDB1_DEAD(r))
109 #define TDB1_HASH_TOP(hash) (TDB1_FREELIST_TOP + (TDB1_BUCKET(hash)+1)*sizeof(tdb1_off_t))
110 #define TDB1_HASHTABLE_SIZE(tdb) ((tdb->header.hash_size+1)*sizeof(tdb1_off_t))
111 #define TDB1_DATA_START(hash_size) (TDB1_HASH_TOP(hash_size-1) + sizeof(tdb1_off_t))
112 #define TDB1_RECOVERY_HEAD offsetof(struct tdb1_header, recovery_start)
113 #define TDB1_SEQNUM_OFS    offsetof(struct tdb1_header, sequence_number)
114 #define TDB1_PAD_BYTE 0x42
115 #define TDB1_PAD_U32  0x42424242
116
117 /* lock offsets */
118 #define TDB1_OPEN_LOCK        0
119 #define TDB1_ACTIVE_LOCK      4
120 #define TDB1_TRANSACTION_LOCK 8
121
122 /* free memory if the pointer is valid and zero the pointer */
123 #ifndef SAFE_FREE
124 #define SAFE_FREE(x) do { if ((x) != NULL) {free(x); (x)=NULL;} } while(0)
125 #endif
126
127 #define TDB1_BUCKET(hash) ((hash) % tdb->header.hash_size)
128
129 #define TDB1_DOCONV() (tdb->flags & TDB_CONVERT)
130 #define TDB1_CONV(x) (TDB1_DOCONV() ? tdb1_convert(&x, sizeof(x)) : &x)
131
132 /* the body of the database is made of one tdb1_record for the free space
133    plus a separate data list for each hash value */
134 struct tdb1_record {
135         tdb1_off_t next; /* offset of the next record in the list */
136         tdb1_len_t rec_len; /* total byte length of record */
137         tdb1_len_t key_len; /* byte length of key */
138         tdb1_len_t data_len; /* byte length of data */
139         uint32_t full_hash; /* the full 32 bit hash of the key */
140         uint32_t magic;   /* try to catch errors */
141         /* the following union is implied:
142                 union {
143                         char record[rec_len];
144                         struct {
145                                 char key[key_len];
146                                 char data[data_len];
147                         }
148                         uint32_t totalsize; (tailer)
149                 }
150         */
151 };
152
153
154 /* this is stored at the front of every database */
155 struct tdb1_header {
156         char magic_food[32]; /* for /etc/magic */
157         uint32_t version; /* version of the code */
158         uint32_t hash_size; /* number of hash entries */
159         tdb1_off_t rwlocks; /* obsolete - kept to detect old formats */
160         tdb1_off_t recovery_start; /* offset of transaction recovery region */
161         tdb1_off_t sequence_number; /* used when TDB1_SEQNUM is set */
162         uint32_t magic1_hash; /* hash of TDB_MAGIC_FOOD. */
163         uint32_t magic2_hash; /* hash of TDB1_MAGIC. */
164         tdb1_off_t reserved[27];
165 };
166
167 struct tdb1_traverse_lock {
168         struct tdb1_traverse_lock *next;
169         uint32_t off;
170         uint32_t hash;
171         int lock_rw;
172 };
173
174 struct tdb1_context;
175
176 struct tdb1_methods {
177         int (*tdb1_read)(struct tdb1_context *, tdb1_off_t , void *, tdb1_len_t , int );
178         int (*tdb1_write)(struct tdb1_context *, tdb1_off_t, const void *, tdb1_len_t);
179         void (*next_hash_chain)(struct tdb1_context *, uint32_t *);
180         int (*tdb1_oob)(struct tdb1_context *, tdb1_off_t , int );
181         int (*tdb1_expand_file)(struct tdb1_context *, tdb1_off_t , tdb1_off_t );
182 };
183
184 struct tdb1_context {
185         struct tdb1_context *next;
186
187         char *name; /* the name of the database */
188
189         /* Logging function */
190         void (*log_fn)(struct tdb1_context *tdb,
191                        enum tdb_log_level level,
192                        enum TDB_ERROR ecode,
193                        const char *message,
194                        void *data);
195         void *log_data;
196
197         /* Last error we returned. */
198         enum TDB_ERROR last_error; /* error code for last tdb error */
199
200         /* The actual file information */
201         struct tdb_file *file;
202
203         int open_flags; /* flags used in the open - needed by reopen */
204
205         /* low level (fnctl) lock functions. */
206         int (*lock_fn)(int fd, int rw, off_t off, off_t len, bool w, void *);
207         int (*unlock_fn)(int fd, int rw, off_t off, off_t len, void *);
208         void *lock_data;
209
210         uint32_t flags; /* the flags passed to tdb1_open */
211
212         /* Our statistics. */
213         struct tdb_attribute_stats stats;
214
215         /* Hash function. */
216         uint64_t (*hash_fn)(const void *key, size_t len, uint64_t seed, void *);
217         void *hash_data;
218         uint64_t hash_seed;
219
220         bool read_only; /* opened read-only */
221         int traverse_read; /* read-only traversal */
222         int traverse_write; /* read-write traversal */
223         struct tdb1_header header; /* a cached copy of the header */
224         struct tdb1_traverse_lock travlocks; /* current traversal locks */
225         const struct tdb1_methods *methods;
226         struct tdb1_transaction *transaction;
227         int page_size;
228         int max_dead_records;
229 };
230
231 /*
232   internal prototypes
233 */
234 int tdb1_munmap(struct tdb1_context *tdb);
235 void tdb1_mmap(struct tdb1_context *tdb);
236 int tdb1_lock(struct tdb1_context *tdb, int list, int ltype);
237 int tdb1_nest_lock(struct tdb1_context *tdb, uint32_t offset, int ltype,
238                   enum tdb_lock_flags flags);
239 int tdb1_nest_unlock(struct tdb1_context *tdb, uint32_t offset, int ltype);
240 int tdb1_unlock(struct tdb1_context *tdb, int list, int ltype);
241 int tdb1_brlock(struct tdb1_context *tdb,
242                int rw_type, tdb1_off_t offset, size_t len,
243                enum tdb_lock_flags flags);
244 int tdb1_brunlock(struct tdb1_context *tdb,
245                  int rw_type, tdb1_off_t offset, size_t len);
246 bool tdb1_have_extra_locks(struct tdb1_context *tdb);
247 void tdb1_release_transaction_locks(struct tdb1_context *tdb);
248 int tdb1_transaction_lock(struct tdb1_context *tdb, int ltype,
249                          enum tdb_lock_flags lockflags);
250 int tdb1_transaction_unlock(struct tdb1_context *tdb, int ltype);
251 int tdb1_recovery_area(struct tdb1_context *tdb,
252                       const struct tdb1_methods *methods,
253                       tdb1_off_t *recovery_offset,
254                       struct tdb1_record *rec);
255 int tdb1_allrecord_lock(struct tdb1_context *tdb, int ltype,
256                        enum tdb_lock_flags flags, bool upgradable);
257 int tdb1_allrecord_unlock(struct tdb1_context *tdb, int ltype);
258 int tdb1_allrecord_upgrade(struct tdb1_context *tdb);
259 int tdb1_write_lock_record(struct tdb1_context *tdb, tdb1_off_t off);
260 int tdb1_write_unlock_record(struct tdb1_context *tdb, tdb1_off_t off);
261 int tdb1_ofs_read(struct tdb1_context *tdb, tdb1_off_t offset, tdb1_off_t *d);
262 int tdb1_ofs_write(struct tdb1_context *tdb, tdb1_off_t offset, tdb1_off_t *d);
263 void *tdb1_convert(void *buf, uint32_t size);
264 int tdb1_free(struct tdb1_context *tdb, tdb1_off_t offset, struct tdb1_record *rec);
265 tdb1_off_t tdb1_allocate(struct tdb1_context *tdb, tdb1_len_t length, struct tdb1_record *rec);
266 int tdb1_ofs_read(struct tdb1_context *tdb, tdb1_off_t offset, tdb1_off_t *d);
267 int tdb1_ofs_write(struct tdb1_context *tdb, tdb1_off_t offset, tdb1_off_t *d);
268 int tdb1_lock_record(struct tdb1_context *tdb, tdb1_off_t off);
269 int tdb1_unlock_record(struct tdb1_context *tdb, tdb1_off_t off);
270 bool tdb1_needs_recovery(struct tdb1_context *tdb);
271 int tdb1_rec_read(struct tdb1_context *tdb, tdb1_off_t offset, struct tdb1_record *rec);
272 int tdb1_rec_write(struct tdb1_context *tdb, tdb1_off_t offset, struct tdb1_record *rec);
273 int tdb1_do_delete(struct tdb1_context *tdb, tdb1_off_t rec_ptr, struct tdb1_record *rec);
274 unsigned char *tdb1_alloc_read(struct tdb1_context *tdb, tdb1_off_t offset, tdb1_len_t len);
275 int tdb1_parse_data(struct tdb1_context *tdb, TDB_DATA key,
276                    tdb1_off_t offset, tdb1_len_t len,
277                    int (*parser)(TDB_DATA key, TDB_DATA data,
278                                  void *private_data),
279                    void *private_data);
280 tdb1_off_t tdb1_find_lock_hash(struct tdb1_context *tdb, TDB_DATA key, uint32_t hash, int locktype,
281                            struct tdb1_record *rec);
282 void tdb1_io_init(struct tdb1_context *tdb);
283 int tdb1_expand(struct tdb1_context *tdb, tdb1_off_t size);
284 int tdb1_rec_free_read(struct tdb1_context *tdb, tdb1_off_t off,
285                       struct tdb1_record *rec);
286 bool tdb1_write_all(int fd, const void *buf, size_t count);
287 int tdb1_transaction_recover(struct tdb1_context *tdb);
288 void tdb1_header_hash(struct tdb1_context *tdb,
289                      uint32_t *magic1_hash, uint32_t *magic2_hash);
290 uint64_t tdb1_old_hash(const void *key, size_t len, uint64_t seed, void *);
291 size_t tdb1_dead_space(struct tdb1_context *tdb, tdb1_off_t off);
292 #endif /* CCAN_TDB2_TDB1_PRIVATE_H */