]> git.ozlabs.org Git - ccan/blob - ccan/tdb2/tdb1_private.h
932e76110bbfb61747e47b27d65c2e4e73ec36fc
[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 /***** END FIXME ***/
39
40 #include <limits.h>
41
42 /* #define TDB_TRACE 1 */
43 #ifndef HAVE_GETPAGESIZE
44 #define getpagesize() 0x2000
45 #endif
46
47 #ifndef __STRING
48 #define __STRING(x)    #x
49 #endif
50
51 #ifndef __STRINGSTRING
52 #define __STRINGSTRING(x) __STRING(x)
53 #endif
54
55 #ifndef __location__
56 #define __location__ __FILE__ ":" __STRINGSTRING(__LINE__)
57 #endif
58
59 typedef uint32_t tdb1_len_t;
60 typedef uint32_t tdb1_off_t;
61
62 #ifndef offsetof
63 #define offsetof(t,f) ((unsigned int)&((t *)0)->f)
64 #endif
65
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
88
89 /* lock offsets */
90 #define TDB1_OPEN_LOCK        0
91 #define TDB1_ACTIVE_LOCK      4
92 #define TDB1_TRANSACTION_LOCK 8
93
94 /* free memory if the pointer is valid and zero the pointer */
95 #ifndef SAFE_FREE
96 #define SAFE_FREE(x) do { if ((x) != NULL) {free(x); (x)=NULL;} } while(0)
97 #endif
98
99 #define TDB1_BUCKET(hash) ((hash) % tdb->header.hash_size)
100
101 #define TDB1_DOCONV() (tdb->flags & TDB1_CONVERT)
102 #define TDB1_CONV(x) (TDB1_DOCONV() ? tdb1_convert(&x, sizeof(x)) : &x)
103
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 */
106 struct tdb1_record {
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:
114                 union {
115                         char record[rec_len];
116                         struct {
117                                 char key[key_len];
118                                 char data[data_len];
119                         }
120                         uint32_t totalsize; (tailer)
121                 }
122         */
123 };
124
125
126 /* this is stored at the front of every database */
127 struct tdb1_header {
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];
137 };
138
139 struct tdb1_lock_type {
140         uint32_t off;
141         uint32_t count;
142         uint32_t ltype;
143 };
144
145 struct tdb1_traverse_lock {
146         struct tdb1_traverse_lock *next;
147         uint32_t off;
148         uint32_t hash;
149         int lock_rw;
150 };
151
152 enum tdb1_lock_flags {
153         /* WAIT == F_SETLKW, NOWAIT == F_SETLK */
154         TDB1_LOCK_NOWAIT = 0,
155         TDB1_LOCK_WAIT = 1,
156         /* If set, don't log an error on failure. */
157         TDB1_LOCK_PROBE = 2,
158 };
159
160 struct tdb1_context;
161
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 );
168 };
169
170 struct tdb1_context {
171         struct tdb1_context *next;
172
173         char *name; /* the name of the database */
174
175         /* Logging function */
176         void (*log_fn)(struct tdb1_context *tdb,
177                        enum tdb_log_level level,
178                        enum TDB_ERROR ecode,
179                        const char *message,
180                        void *data);
181         void *log_data;
182
183         /* Last error we returned. */
184         enum TDB_ERROR last_error; /* error code for last tdb error */
185
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 */
193         int num_lockrecs;
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;
204         int page_size;
205         int max_dead_records;
206 };
207
208 /*
209   internal prototypes
210 */
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,
255                                  void *private_data),
256                    void *private_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 */