]> git.ozlabs.org Git - ccan/blob - ccan/tdb2/tdb1_private.h
tdb2: import TDB1 code.
[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 #ifndef _SAMBA_BUILD_
29 #include "config.h"
30 /* This keeps us consistent with TDB2 code. */
31 #if HAVE_FILE_OFFSET_BITS
32 #define _FILE_OFFSET_BITS 64
33 #endif
34 #include <stdint.h>
35 #include <stdbool.h>
36 #include <string.h>
37 #include <sys/types.h>
38 #include <sys/stat.h>
39 #include <sys/mman.h>
40 #include <sys/time.h>
41 #include <fcntl.h>
42 #include <unistd.h>
43 #include <errno.h>
44 #include <stdlib.h>
45 #include <limits.h>
46 #include <stdio.h>
47 #include <utime.h>
48
49 #ifndef _PUBLIC_
50 #define _PUBLIC_
51 #endif
52
53 #else
54 #include "replace.h"
55 #include "system/filesys.h"
56 #include "system/time.h"
57 #include "system/shmem.h"
58 #include "system/select.h"
59 #include "system/wait.h"
60 #endif
61
62 #include "tdb1.h"
63
64 /* #define TDB_TRACE 1 */
65 #ifndef HAVE_GETPAGESIZE
66 #define getpagesize() 0x2000
67 #endif
68
69 #ifndef __STRING
70 #define __STRING(x)    #x
71 #endif
72
73 #ifndef __STRINGSTRING
74 #define __STRINGSTRING(x) __STRING(x)
75 #endif
76
77 #ifndef __location__
78 #define __location__ __FILE__ ":" __STRINGSTRING(__LINE__)
79 #endif
80
81 typedef uint32_t tdb1_len_t;
82 typedef uint32_t tdb1_off_t;
83
84 #ifndef offsetof
85 #define offsetof(t,f) ((unsigned int)&((t *)0)->f)
86 #endif
87
88 #define TDB1_MAGIC_FOOD "TDB file\n"
89 #define TDB1_VERSION (0x26011967 + 6)
90 #define TDB1_MAGIC (0x26011999U)
91 #define TDB1_FREE_MAGIC (~TDB1_MAGIC)
92 #define TDB1_DEAD_MAGIC (0xFEE1DEAD)
93 #define TDB1_RECOVERY_MAGIC (0xf53bc0e7U)
94 #define TDB1_RECOVERY_INVALID_MAGIC (0x0)
95 #define TDB1_HASH_RWLOCK_MAGIC (0xbad1a51U)
96 #define TDB1_ALIGNMENT 4
97 #define TDB1_DEFAULT_HASH_SIZE 131
98 #define TDB1_FREELIST_TOP (sizeof(struct tdb1_header))
99 #define TDB1_ALIGN(x,a) (((x) + (a)-1) & ~((a)-1))
100 #define TDB1_BYTEREV(x) (((((x)&0xff)<<24)|((x)&0xFF00)<<8)|(((x)>>8)&0xFF00)|((x)>>24))
101 #define TDB1_DEAD(r) ((r)->magic == TDB1_DEAD_MAGIC)
102 #define TDB1_BAD_MAGIC(r) ((r)->magic != TDB1_MAGIC && !TDB1_DEAD(r))
103 #define TDB1_HASH_TOP(hash) (TDB1_FREELIST_TOP + (TDB1_BUCKET(hash)+1)*sizeof(tdb1_off_t))
104 #define TDB1_HASHTABLE_SIZE(tdb) ((tdb->header.hash_size+1)*sizeof(tdb1_off_t))
105 #define TDB1_DATA_START(hash_size) (TDB1_HASH_TOP(hash_size-1) + sizeof(tdb1_off_t))
106 #define TDB1_RECOVERY_HEAD offsetof(struct tdb1_header, recovery_start)
107 #define TDB1_SEQNUM_OFS    offsetof(struct tdb1_header, sequence_number)
108 #define TDB1_PAD_BYTE 0x42
109 #define TDB1_PAD_U32  0x42424242
110
111 /* NB assumes there is a local variable called "tdb" that is the
112  * current context, also takes doubly-parenthesized print-style
113  * argument. */
114 #define TDB1_LOG(x) tdb->log.log_fn x
115
116 /* lock offsets */
117 #define TDB1_OPEN_LOCK        0
118 #define TDB1_ACTIVE_LOCK      4
119 #define TDB1_TRANSACTION_LOCK 8
120
121 /* free memory if the pointer is valid and zero the pointer */
122 #ifndef SAFE_FREE
123 #define SAFE_FREE(x) do { if ((x) != NULL) {free(x); (x)=NULL;} } while(0)
124 #endif
125
126 #define TDB1_BUCKET(hash) ((hash) % tdb->header.hash_size)
127
128 #define TDB1_DOCONV() (tdb->flags & TDB1_CONVERT)
129 #define TDB1_CONV(x) (TDB1_DOCONV() ? tdb1_convert(&x, sizeof(x)) : &x)
130
131 /* the body of the database is made of one tdb1_record for the free space
132    plus a separate data list for each hash value */
133 struct tdb1_record {
134         tdb1_off_t next; /* offset of the next record in the list */
135         tdb1_len_t rec_len; /* total byte length of record */
136         tdb1_len_t key_len; /* byte length of key */
137         tdb1_len_t data_len; /* byte length of data */
138         uint32_t full_hash; /* the full 32 bit hash of the key */
139         uint32_t magic;   /* try to catch errors */
140         /* the following union is implied:
141                 union {
142                         char record[rec_len];
143                         struct {
144                                 char key[key_len];
145                                 char data[data_len];
146                         }
147                         uint32_t totalsize; (tailer)
148                 }
149         */
150 };
151
152
153 /* this is stored at the front of every database */
154 struct tdb1_header {
155         char magic_food[32]; /* for /etc/magic */
156         uint32_t version; /* version of the code */
157         uint32_t hash_size; /* number of hash entries */
158         tdb1_off_t rwlocks; /* obsolete - kept to detect old formats */
159         tdb1_off_t recovery_start; /* offset of transaction recovery region */
160         tdb1_off_t sequence_number; /* used when TDB1_SEQNUM is set */
161         uint32_t magic1_hash; /* hash of TDB1_MAGIC_FOOD. */
162         uint32_t magic2_hash; /* hash of TDB1_MAGIC. */
163         tdb1_off_t reserved[27];
164 };
165
166 struct tdb1_lock_type {
167         uint32_t off;
168         uint32_t count;
169         uint32_t ltype;
170 };
171
172 struct tdb1_traverse_lock {
173         struct tdb1_traverse_lock *next;
174         uint32_t off;
175         uint32_t hash;
176         int lock_rw;
177 };
178
179 enum tdb1_lock_flags {
180         /* WAIT == F_SETLKW, NOWAIT == F_SETLK */
181         TDB1_LOCK_NOWAIT = 0,
182         TDB1_LOCK_WAIT = 1,
183         /* If set, don't log an error on failure. */
184         TDB1_LOCK_PROBE = 2,
185         /* If set, don't actually lock at all. */
186         TDB1_LOCK_MARK_ONLY = 4,
187 };
188
189 struct tdb1_context;
190
191 struct tdb1_methods {
192         int (*tdb1_read)(struct tdb1_context *, tdb1_off_t , void *, tdb1_len_t , int );
193         int (*tdb1_write)(struct tdb1_context *, tdb1_off_t, const void *, tdb1_len_t);
194         void (*next_hash_chain)(struct tdb1_context *, uint32_t *);
195         int (*tdb1_oob)(struct tdb1_context *, tdb1_off_t , int );
196         int (*tdb1_expand_file)(struct tdb1_context *, tdb1_off_t , tdb1_off_t );
197 };
198
199 struct tdb1_context {
200         char *name; /* the name of the database */
201         void *map_ptr; /* where it is currently mapped */
202         int fd; /* open file descriptor for the database */
203         tdb1_len_t map_size; /* how much space has been mapped */
204         int read_only; /* opened read-only */
205         int traverse_read; /* read-only traversal */
206         int traverse_write; /* read-write traversal */
207         struct tdb1_lock_type allrecord_lock; /* .offset == upgradable */
208         int num_lockrecs;
209         struct tdb1_lock_type *lockrecs; /* only real locks, all with count>0 */
210         enum TDB1_ERROR ecode; /* error code for last tdb error */
211         struct tdb1_header header; /* a cached copy of the header */
212         uint32_t flags; /* the flags passed to tdb1_open */
213         struct tdb1_traverse_lock travlocks; /* current traversal locks */
214         struct tdb1_context *next; /* all tdbs to avoid multiple opens */
215         dev_t device;   /* uniquely identifies this tdb */
216         ino_t inode;    /* uniquely identifies this tdb */
217         struct tdb1_logging_context log;
218         unsigned int (*hash_fn)(TDB1_DATA *key);
219         int open_flags; /* flags used in the open - needed by reopen */
220         const struct tdb1_methods *methods;
221         struct tdb1_transaction *transaction;
222         int page_size;
223         int max_dead_records;
224 #ifdef TDB1_TRACE
225         int tracefd;
226 #endif
227         volatile sig_atomic_t *interrupt_sig_ptr;
228 };
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_lock_nonblock(struct tdb1_context *tdb, int list, int ltype);
238 int tdb1_nest_lock(struct tdb1_context *tdb, uint32_t offset, int ltype,
239                   enum tdb1_lock_flags flags);
240 int tdb1_nest_unlock(struct tdb1_context *tdb, uint32_t offset, int ltype,
241                     bool mark_lock);
242 int tdb1_unlock(struct tdb1_context *tdb, int list, int ltype);
243 int tdb1_brlock(struct tdb1_context *tdb,
244                int rw_type, tdb1_off_t offset, size_t len,
245                enum tdb1_lock_flags flags);
246 int tdb1_brunlock(struct tdb1_context *tdb,
247                  int rw_type, tdb1_off_t offset, size_t len);
248 bool tdb1_have_extra_locks(struct tdb1_context *tdb);
249 void tdb1_release_transaction_locks(struct tdb1_context *tdb);
250 int tdb1_transaction_lock(struct tdb1_context *tdb, int ltype,
251                          enum tdb1_lock_flags lockflags);
252 int tdb1_transaction_unlock(struct tdb1_context *tdb, int ltype);
253 int tdb1_recovery_area(struct tdb1_context *tdb,
254                       const struct tdb1_methods *methods,
255                       tdb1_off_t *recovery_offset,
256                       struct tdb1_record *rec);
257 int tdb1_allrecord_lock(struct tdb1_context *tdb, int ltype,
258                        enum tdb1_lock_flags flags, bool upgradable);
259 int tdb1_allrecord_unlock(struct tdb1_context *tdb, int ltype, bool mark_lock);
260 int tdb1_allrecord_upgrade(struct tdb1_context *tdb);
261 int tdb1_write_lock_record(struct tdb1_context *tdb, tdb1_off_t off);
262 int tdb1_write_unlock_record(struct tdb1_context *tdb, tdb1_off_t off);
263 int tdb1_ofs_read(struct tdb1_context *tdb, tdb1_off_t offset, tdb1_off_t *d);
264 int tdb1_ofs_write(struct tdb1_context *tdb, tdb1_off_t offset, tdb1_off_t *d);
265 void *tdb1_convert(void *buf, uint32_t size);
266 int tdb1_free(struct tdb1_context *tdb, tdb1_off_t offset, struct tdb1_record *rec);
267 tdb1_off_t tdb1_allocate(struct tdb1_context *tdb, tdb1_len_t length, struct tdb1_record *rec);
268 int tdb1_ofs_read(struct tdb1_context *tdb, tdb1_off_t offset, tdb1_off_t *d);
269 int tdb1_ofs_write(struct tdb1_context *tdb, tdb1_off_t offset, tdb1_off_t *d);
270 int tdb1_lock_record(struct tdb1_context *tdb, tdb1_off_t off);
271 int tdb1_unlock_record(struct tdb1_context *tdb, tdb1_off_t off);
272 bool tdb1_needs_recovery(struct tdb1_context *tdb);
273 int tdb1_rec_read(struct tdb1_context *tdb, tdb1_off_t offset, struct tdb1_record *rec);
274 int tdb1_rec_write(struct tdb1_context *tdb, tdb1_off_t offset, struct tdb1_record *rec);
275 int tdb1_do_delete(struct tdb1_context *tdb, tdb1_off_t rec_ptr, struct tdb1_record *rec);
276 unsigned char *tdb1_alloc_read(struct tdb1_context *tdb, tdb1_off_t offset, tdb1_len_t len);
277 int tdb1_parse_data(struct tdb1_context *tdb, TDB1_DATA key,
278                    tdb1_off_t offset, tdb1_len_t len,
279                    int (*parser)(TDB1_DATA key, TDB1_DATA data,
280                                  void *private_data),
281                    void *private_data);
282 tdb1_off_t tdb1_find_lock_hash(struct tdb1_context *tdb, TDB1_DATA key, uint32_t hash, int locktype,
283                            struct tdb1_record *rec);
284 void tdb1_io_init(struct tdb1_context *tdb);
285 int tdb1_expand(struct tdb1_context *tdb, tdb1_off_t size);
286 int tdb1_rec_free_read(struct tdb1_context *tdb, tdb1_off_t off,
287                       struct tdb1_record *rec);
288 bool tdb1_write_all(int fd, const void *buf, size_t count);
289 int tdb1_transaction_recover(struct tdb1_context *tdb);
290 void tdb1_header_hash(struct tdb1_context *tdb,
291                      uint32_t *magic1_hash, uint32_t *magic2_hash);
292 unsigned int tdb1_old_hash(TDB1_DATA *key);
293 size_t tdb1_dead_space(struct tdb1_context *tdb, tdb1_off_t off);
294 #endif /* CCAN_TDB2_TDB1_PRIVATE_H */