]> git.ozlabs.org Git - ccan/blob - ccan/tdb2/tdb1_private.h
0d40d7a9190cc88dad9cc48334e3787a7527c369
[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_VERSION (0x26011967 + 6)
67 #define TDB1_MAGIC (0x26011999U)
68 #define TDB1_FREE_MAGIC (~TDB1_MAGIC)
69 #define TDB1_DEAD_MAGIC (0xFEE1DEAD)
70 #define TDB1_RECOVERY_MAGIC (0xf53bc0e7U)
71 #define TDB1_RECOVERY_INVALID_MAGIC (0x0)
72 #define TDB1_HASH_RWLOCK_MAGIC (0xbad1a51U)
73 #define TDB1_ALIGNMENT 4
74 #define TDB1_DEFAULT_HASH_SIZE 131
75 #define TDB1_FREELIST_TOP (sizeof(struct tdb1_header))
76 #define TDB1_ALIGN(x,a) (((x) + (a)-1) & ~((a)-1))
77 #define TDB1_BYTEREV(x) (((((x)&0xff)<<24)|((x)&0xFF00)<<8)|(((x)>>8)&0xFF00)|((x)>>24))
78 #define TDB1_DEAD(r) ((r)->magic == TDB1_DEAD_MAGIC)
79 #define TDB1_BAD_MAGIC(r) ((r)->magic != TDB1_MAGIC && !TDB1_DEAD(r))
80 #define TDB1_HASH_TOP(hash) (TDB1_FREELIST_TOP + (TDB1_BUCKET(hash)+1)*sizeof(tdb1_off_t))
81 #define TDB1_HASHTABLE_SIZE(tdb) ((tdb->header.hash_size+1)*sizeof(tdb1_off_t))
82 #define TDB1_DATA_START(hash_size) (TDB1_HASH_TOP(hash_size-1) + sizeof(tdb1_off_t))
83 #define TDB1_RECOVERY_HEAD offsetof(struct tdb1_header, recovery_start)
84 #define TDB1_SEQNUM_OFS    offsetof(struct tdb1_header, sequence_number)
85 #define TDB1_PAD_BYTE 0x42
86 #define TDB1_PAD_U32  0x42424242
87
88 /* lock offsets */
89 #define TDB1_OPEN_LOCK        0
90 #define TDB1_ACTIVE_LOCK      4
91 #define TDB1_TRANSACTION_LOCK 8
92
93 /* free memory if the pointer is valid and zero the pointer */
94 #ifndef SAFE_FREE
95 #define SAFE_FREE(x) do { if ((x) != NULL) {free(x); (x)=NULL;} } while(0)
96 #endif
97
98 #define TDB1_BUCKET(hash) ((hash) % tdb->header.hash_size)
99
100 #define TDB1_DOCONV() (tdb->flags & TDB_CONVERT)
101 #define TDB1_CONV(x) (TDB1_DOCONV() ? tdb1_convert(&x, sizeof(x)) : &x)
102
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 */
105 struct tdb1_record {
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:
113                 union {
114                         char record[rec_len];
115                         struct {
116                                 char key[key_len];
117                                 char data[data_len];
118                         }
119                         uint32_t totalsize; (tailer)
120                 }
121         */
122 };
123
124
125 /* this is stored at the front of every database */
126 struct tdb1_header {
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 TDB_MAGIC_FOOD. */
134         uint32_t magic2_hash; /* hash of TDB1_MAGIC. */
135         tdb1_off_t reserved[27];
136 };
137
138 struct tdb1_lock_type {
139         uint32_t off;
140         uint32_t count;
141         uint32_t ltype;
142 };
143
144 struct tdb1_traverse_lock {
145         struct tdb1_traverse_lock *next;
146         uint32_t off;
147         uint32_t hash;
148         int lock_rw;
149 };
150
151 struct tdb1_context;
152
153 struct tdb1_methods {
154         int (*tdb1_read)(struct tdb1_context *, tdb1_off_t , void *, tdb1_len_t , int );
155         int (*tdb1_write)(struct tdb1_context *, tdb1_off_t, const void *, tdb1_len_t);
156         void (*next_hash_chain)(struct tdb1_context *, uint32_t *);
157         int (*tdb1_oob)(struct tdb1_context *, tdb1_off_t , int );
158         int (*tdb1_expand_file)(struct tdb1_context *, tdb1_off_t , tdb1_off_t );
159 };
160
161 struct tdb1_context {
162         struct tdb1_context *next;
163
164         char *name; /* the name of the database */
165
166         /* Logging function */
167         void (*log_fn)(struct tdb1_context *tdb,
168                        enum tdb_log_level level,
169                        enum TDB_ERROR ecode,
170                        const char *message,
171                        void *data);
172         void *log_data;
173
174         /* Last error we returned. */
175         enum TDB_ERROR last_error; /* error code for last tdb error */
176
177         void *map_ptr; /* where it is currently mapped */
178         int fd; /* open file descriptor for the database */
179         tdb1_len_t map_size; /* how much space has been mapped */
180         int read_only; /* opened read-only */
181         int traverse_read; /* read-only traversal */
182         int traverse_write; /* read-write traversal */
183         struct tdb1_lock_type allrecord_lock; /* .offset == upgradable */
184         int num_lockrecs;
185         struct tdb1_lock_type *lockrecs; /* only real locks, all with count>0 */
186         struct tdb1_header header; /* a cached copy of the header */
187         uint32_t flags; /* the flags passed to tdb1_open */
188         struct tdb1_traverse_lock travlocks; /* current traversal locks */
189         dev_t device;   /* uniquely identifies this tdb */
190         ino_t inode;    /* uniquely identifies this tdb */
191         unsigned int (*hash_fn)(TDB_DATA *key);
192         int open_flags; /* flags used in the open - needed by reopen */
193         const struct tdb1_methods *methods;
194         struct tdb1_transaction *transaction;
195         int page_size;
196         int max_dead_records;
197 };
198
199 /*
200   internal prototypes
201 */
202 int tdb1_munmap(struct tdb1_context *tdb);
203 void tdb1_mmap(struct tdb1_context *tdb);
204 int tdb1_lock(struct tdb1_context *tdb, int list, int ltype);
205 int tdb1_nest_lock(struct tdb1_context *tdb, uint32_t offset, int ltype,
206                   enum tdb_lock_flags flags);
207 int tdb1_nest_unlock(struct tdb1_context *tdb, uint32_t offset, int ltype);
208 int tdb1_unlock(struct tdb1_context *tdb, int list, int ltype);
209 int tdb1_brlock(struct tdb1_context *tdb,
210                int rw_type, tdb1_off_t offset, size_t len,
211                enum tdb_lock_flags flags);
212 int tdb1_brunlock(struct tdb1_context *tdb,
213                  int rw_type, tdb1_off_t offset, size_t len);
214 bool tdb1_have_extra_locks(struct tdb1_context *tdb);
215 void tdb1_release_transaction_locks(struct tdb1_context *tdb);
216 int tdb1_transaction_lock(struct tdb1_context *tdb, int ltype,
217                          enum tdb_lock_flags lockflags);
218 int tdb1_transaction_unlock(struct tdb1_context *tdb, int ltype);
219 int tdb1_recovery_area(struct tdb1_context *tdb,
220                       const struct tdb1_methods *methods,
221                       tdb1_off_t *recovery_offset,
222                       struct tdb1_record *rec);
223 int tdb1_allrecord_lock(struct tdb1_context *tdb, int ltype,
224                        enum tdb_lock_flags flags, bool upgradable);
225 int tdb1_allrecord_unlock(struct tdb1_context *tdb, int ltype);
226 int tdb1_allrecord_upgrade(struct tdb1_context *tdb);
227 int tdb1_write_lock_record(struct tdb1_context *tdb, tdb1_off_t off);
228 int tdb1_write_unlock_record(struct tdb1_context *tdb, tdb1_off_t off);
229 int tdb1_ofs_read(struct tdb1_context *tdb, tdb1_off_t offset, tdb1_off_t *d);
230 int tdb1_ofs_write(struct tdb1_context *tdb, tdb1_off_t offset, tdb1_off_t *d);
231 void *tdb1_convert(void *buf, uint32_t size);
232 int tdb1_free(struct tdb1_context *tdb, tdb1_off_t offset, struct tdb1_record *rec);
233 tdb1_off_t tdb1_allocate(struct tdb1_context *tdb, tdb1_len_t length, struct tdb1_record *rec);
234 int tdb1_ofs_read(struct tdb1_context *tdb, tdb1_off_t offset, tdb1_off_t *d);
235 int tdb1_ofs_write(struct tdb1_context *tdb, tdb1_off_t offset, tdb1_off_t *d);
236 int tdb1_lock_record(struct tdb1_context *tdb, tdb1_off_t off);
237 int tdb1_unlock_record(struct tdb1_context *tdb, tdb1_off_t off);
238 bool tdb1_needs_recovery(struct tdb1_context *tdb);
239 int tdb1_rec_read(struct tdb1_context *tdb, tdb1_off_t offset, struct tdb1_record *rec);
240 int tdb1_rec_write(struct tdb1_context *tdb, tdb1_off_t offset, struct tdb1_record *rec);
241 int tdb1_do_delete(struct tdb1_context *tdb, tdb1_off_t rec_ptr, struct tdb1_record *rec);
242 unsigned char *tdb1_alloc_read(struct tdb1_context *tdb, tdb1_off_t offset, tdb1_len_t len);
243 int tdb1_parse_data(struct tdb1_context *tdb, TDB_DATA key,
244                    tdb1_off_t offset, tdb1_len_t len,
245                    int (*parser)(TDB_DATA key, TDB_DATA data,
246                                  void *private_data),
247                    void *private_data);
248 tdb1_off_t tdb1_find_lock_hash(struct tdb1_context *tdb, TDB_DATA key, uint32_t hash, int locktype,
249                            struct tdb1_record *rec);
250 void tdb1_io_init(struct tdb1_context *tdb);
251 int tdb1_expand(struct tdb1_context *tdb, tdb1_off_t size);
252 int tdb1_rec_free_read(struct tdb1_context *tdb, tdb1_off_t off,
253                       struct tdb1_record *rec);
254 bool tdb1_write_all(int fd, const void *buf, size_t count);
255 int tdb1_transaction_recover(struct tdb1_context *tdb);
256 void tdb1_header_hash(struct tdb1_context *tdb,
257                      uint32_t *magic1_hash, uint32_t *magic2_hash);
258 unsigned int tdb1_old_hash(TDB_DATA *key);
259 size_t tdb1_dead_space(struct tdb1_context *tdb, tdb1_off_t off);
260 #endif /* CCAN_TDB2_TDB1_PRIVATE_H */