source: trunk/server/lib/tdb/common/tdb_private.h

Last change on this file was 987, checked in by Silvan Scherrer, 9 years ago

samba server: fix crlf in tdb trunk code

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