2 Unix SMB/CIFS implementation.
4 trivial database library
6 Copyright (C) Andrew Tridgell 1999-2005
7 Copyright (C) Paul `Rusty' Russell 2000
8 Copyright (C) Jeremy Allison 2000-2003
10 ** NOTE! The following LGPL license applies to the tdb
11 ** library. This does NOT imply that all of Samba is released
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.
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.
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/>.
28 #include "tdb1_private.h"
30 /* all contexts, to ensure no double-opens (fcntl locks don't nest!) */
31 static struct tdb1_context *tdb1s = NULL;
33 /* We use two hashes to double-check they're using the right hash function. */
34 void tdb1_header_hash(struct tdb1_context *tdb,
35 uint32_t *magic1_hash, uint32_t *magic2_hash)
38 uint32_t tdb1_magic = TDB1_MAGIC;
40 hash_key.dptr = (unsigned char *)TDB_MAGIC_FOOD;
41 hash_key.dsize = sizeof(TDB_MAGIC_FOOD);
42 *magic1_hash = tdb->hash_fn(&hash_key);
44 hash_key.dptr = (unsigned char *)TDB1_CONV(tdb1_magic);
45 hash_key.dsize = sizeof(tdb1_magic);
46 *magic2_hash = tdb->hash_fn(&hash_key);
48 /* Make sure at least one hash is non-zero! */
49 if (*magic1_hash == 0 && *magic2_hash == 0)
53 /* initialise a new database with a specified hash size */
54 static int tdb1_new_database(struct tdb1_context *tdb, int hash_size)
56 struct tdb1_header *newdb;
60 /* We make it up in memory, then write it out if not internal */
61 size = sizeof(struct tdb1_header) + (hash_size+1)*sizeof(tdb1_off_t);
62 if (!(newdb = (struct tdb1_header *)calloc(size, 1))) {
63 tdb->last_error = TDB_ERR_OOM;
67 /* Fill in the header */
68 newdb->version = TDB1_VERSION;
69 newdb->hash_size = hash_size;
71 tdb1_header_hash(tdb, &newdb->magic1_hash, &newdb->magic2_hash);
73 /* Make sure older tdbs (which don't check the magic hash fields)
74 * will refuse to open this TDB. */
75 if (tdb->hash_fn == tdb1_incompatible_hash)
76 newdb->rwlocks = TDB1_HASH_RWLOCK_MAGIC;
78 if (tdb->flags & TDB1_INTERNAL) {
80 tdb->map_ptr = (char *)newdb;
81 memcpy(&tdb->header, newdb, sizeof(tdb->header));
82 /* Convert the `ondisk' version if asked. */
86 if (lseek(tdb->fd, 0, SEEK_SET) == -1)
89 if (ftruncate(tdb->fd, 0) == -1)
92 /* This creates an endian-converted header, as if read from disk */
94 memcpy(&tdb->header, newdb, sizeof(tdb->header));
95 /* Don't endian-convert the magic food! */
96 memcpy(newdb->magic_food, TDB_MAGIC_FOOD, strlen(TDB_MAGIC_FOOD)+1);
97 /* we still have "ret == -1" here */
98 if (tdb1_write_all(tdb->fd, newdb, size))
108 static int tdb1_already_open(dev_t device,
111 struct tdb1_context *i;
113 for (i = tdb1s; i; i = i->next) {
114 if (i->device == device && i->inode == ino) {
122 /* open the database, creating it if necessary
124 The open_flags and mode are passed straight to the open call on the
125 database file. A flags value of O_WRONLY is invalid. The hash size
126 is advisory, use zero for a default value.
128 Return is NULL on error, in which case errno is also set. Don't
129 try to call tdb1_error or tdb1_errname, just do strerror(errno).
131 @param name may be NULL for internal databases. */
132 struct tdb1_context *tdb1_open(const char *name, int hash_size, int tdb1_flags,
133 int open_flags, mode_t mode)
135 return tdb1_open_ex(name, hash_size, tdb1_flags, open_flags, mode, NULL, NULL);
138 static bool hash_correct(struct tdb1_context *tdb,
139 uint32_t *m1, uint32_t *m2)
141 tdb1_header_hash(tdb, m1, m2);
142 return (tdb->header.magic1_hash == *m1 &&
143 tdb->header.magic2_hash == *m2);
146 static bool check_header_hash(struct tdb1_context *tdb,
147 uint32_t *m1, uint32_t *m2)
149 if (hash_correct(tdb, m1, m2))
152 /* If they use one inbuilt, try the other inbuilt hash. */
153 if (tdb->hash_fn == tdb1_old_hash)
154 tdb->hash_fn = tdb1_incompatible_hash;
155 else if (tdb->hash_fn == tdb1_incompatible_hash)
156 tdb->hash_fn = tdb1_old_hash;
159 return hash_correct(tdb, m1, m2);
162 struct tdb1_context *tdb1_open_ex(const char *name, int hash_size, int tdb1_flags,
163 int open_flags, mode_t mode,
164 const struct tdb1_logging_context *log_ctx,
165 tdb1_hash_func hash_fn)
167 struct tdb1_context *tdb;
169 int rev = 0, locked = 0;
173 const char *hash_alg;
174 uint32_t magic1, magic2;
176 if (!(tdb = (struct tdb1_context *)calloc(1, sizeof *tdb))) {
185 tdb->flags = tdb1_flags;
186 tdb->open_flags = open_flags;
188 tdb->log_fn = log_ctx->log_fn;
189 tdb->log_data = log_ctx->log_private;
193 if (name == NULL && (tdb1_flags & TDB1_INTERNAL)) {
194 name = "__TDB1_INTERNAL__";
198 tdb->name = (char *)"__NULL__";
199 tdb_logerr(tdb, TDB_ERR_EINVAL, TDB_LOG_USE_ERROR,
200 "tdb1_open_ex: called with name == NULL");
206 /* now make a copy of the name, as the caller memory might went away */
207 if (!(tdb->name = (char *)strdup(name))) {
209 * set the name as the given string, so that tdb1_name() will
210 * work in case of an error.
212 tdb->name = (char *)name;
213 tdb_logerr(tdb, TDB_ERR_OOM, TDB_LOG_ERROR,
214 "tdb1_open_ex: can't strdup(%s)", name);
221 tdb->hash_fn = hash_fn;
222 if (hash_fn == tdb1_incompatible_hash)
223 hash_alg = "tdb1_incompatible_hash";
225 hash_alg = "the user defined";
227 tdb->hash_fn = tdb1_old_hash;
228 hash_alg = "default";
231 /* cache the page size */
232 tdb->page_size = getpagesize();
233 if (tdb->page_size <= 0) {
234 tdb->page_size = 0x2000;
237 tdb->max_dead_records = (tdb1_flags & TDB1_VOLATILE) ? 5 : 0;
239 if ((open_flags & O_ACCMODE) == O_WRONLY) {
240 tdb_logerr(tdb, TDB_ERR_EINVAL, TDB_LOG_USE_ERROR,
241 "tdb1_open_ex: can't open tdb %s write-only",
248 hash_size = TDB1_DEFAULT_HASH_SIZE;
249 if ((open_flags & O_ACCMODE) == O_RDONLY) {
251 /* read only databases don't do locking or clear if first */
252 tdb->flags |= TDB1_NOLOCK;
253 tdb->flags &= ~TDB1_CLEAR_IF_FIRST;
256 if ((tdb->flags & TDB1_ALLOW_NESTING) &&
257 (tdb->flags & TDB1_DISALLOW_NESTING)) {
258 tdb_logerr(tdb, TDB_ERR_EINVAL, TDB_LOG_USE_ERROR,
260 "allow_nesting and disallow_nesting are not allowed together!");
266 * TDB1_ALLOW_NESTING is the default behavior.
267 * Note: this may change in future versions!
269 if (!(tdb->flags & TDB1_DISALLOW_NESTING)) {
270 tdb->flags |= TDB1_ALLOW_NESTING;
273 /* internal databases don't mmap or lock, and start off cleared */
274 if (tdb->flags & TDB1_INTERNAL) {
275 tdb->flags |= (TDB1_NOLOCK | TDB1_NOMMAP);
276 tdb->flags &= ~TDB1_CLEAR_IF_FIRST;
277 if (tdb1_new_database(tdb, hash_size) != 0) {
278 tdb_logerr(tdb, tdb->last_error, TDB_LOG_ERROR,
279 "tdb1_open_ex: tdb1_new_database failed!");
285 if ((tdb->fd = open(name, open_flags, mode)) == -1) {
286 tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
287 "tdb1_open_ex: could not open file %s: %s",
288 name, strerror(errno));
289 goto fail; /* errno set by open(2) */
292 /* on exec, don't inherit the fd */
293 v = fcntl(tdb->fd, F_GETFD, 0);
294 fcntl(tdb->fd, F_SETFD, v | FD_CLOEXEC);
296 /* ensure there is only one process initialising at once */
297 if (tdb1_nest_lock(tdb, TDB1_OPEN_LOCK, F_WRLCK, TDB_LOCK_WAIT) == -1) {
298 tdb_logerr(tdb, tdb->last_error, TDB_LOG_ERROR,
299 "tdb1_open_ex: failed to get open lock on %s: %s",
300 name, strerror(errno));
301 goto fail; /* errno set by tdb1_brlock */
304 /* we need to zero database if we are the only one with it open */
305 if ((tdb1_flags & TDB1_CLEAR_IF_FIRST) &&
307 (locked = (tdb1_nest_lock(tdb, TDB1_ACTIVE_LOCK, F_WRLCK, TDB_LOCK_NOWAIT|TDB_LOCK_PROBE) == 0))) {
308 open_flags |= O_CREAT;
309 if (ftruncate(tdb->fd, 0) == -1) {
310 tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
312 "failed to truncate %s: %s",
313 name, strerror(errno));
314 goto fail; /* errno set by ftruncate */
319 if (read(tdb->fd, &tdb->header, sizeof(tdb->header)) != sizeof(tdb->header)
320 || strcmp(tdb->header.magic_food, TDB_MAGIC_FOOD) != 0) {
321 if (!(open_flags & O_CREAT) || tdb1_new_database(tdb, hash_size) == -1) {
323 errno = EIO; /* ie bad format or something */
327 rev = (tdb->flags & TDB1_CONVERT);
328 } else if (tdb->header.version != TDB1_VERSION
329 && !(rev = (tdb->header.version==TDB1_BYTEREV(TDB1_VERSION)))) {
334 vp = (unsigned char *)&tdb->header.version;
335 vertest = (((uint32_t)vp[0]) << 24) | (((uint32_t)vp[1]) << 16) |
336 (((uint32_t)vp[2]) << 8) | (uint32_t)vp[3];
337 tdb->flags |= (vertest==TDB1_VERSION) ? TDB1_BIGENDIAN : 0;
339 tdb->flags &= ~TDB1_CONVERT;
341 tdb->flags |= TDB1_CONVERT;
342 tdb1_convert(&tdb->header, sizeof(tdb->header));
344 if (fstat(tdb->fd, &st) == -1)
347 if (tdb->header.rwlocks != 0 &&
348 tdb->header.rwlocks != TDB1_HASH_RWLOCK_MAGIC) {
349 tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_LOG_ERROR,
350 "tdb1_open_ex: spinlocks no longer supported");
354 if ((tdb->header.magic1_hash == 0) && (tdb->header.magic2_hash == 0)) {
355 /* older TDB without magic hash references */
356 tdb->hash_fn = tdb1_old_hash;
357 } else if (!check_header_hash(tdb, &magic1, &magic2)) {
358 tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_LOG_USE_ERROR,
360 "%s was not created with %s hash function we are using\n"
361 "magic1_hash[0x%08X %s 0x%08X] "
362 "magic2_hash[0x%08X %s 0x%08X]",
364 tdb->header.magic1_hash,
365 (tdb->header.magic1_hash == magic1) ? "==" : "!=",
367 tdb->header.magic2_hash,
368 (tdb->header.magic2_hash == magic2) ? "==" : "!=",
374 /* Is it already in the open list? If so, fail. */
375 if (tdb1_already_open(st.st_dev, st.st_ino)) {
376 tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_USE_ERROR,
378 "%s (%d,%d) is already open in this process",
379 name, (int)st.st_dev, (int)st.st_ino);
384 tdb->map_size = st.st_size;
385 tdb->device = st.st_dev;
386 tdb->inode = st.st_ino;
389 if (tdb1_nest_unlock(tdb, TDB1_ACTIVE_LOCK, F_WRLCK) == -1) {
390 tdb_logerr(tdb, tdb->last_error, TDB_LOG_ERROR,
392 "failed to release ACTIVE_LOCK on %s: %s",
393 name, strerror(errno));
399 /* We always need to do this if the CLEAR_IF_FIRST flag is set, even if
400 we didn't get the initial exclusive lock as we need to let all other
401 users know we're using it. */
403 if (tdb1_flags & TDB1_CLEAR_IF_FIRST) {
404 /* leave this lock in place to indicate it's in use */
405 if (tdb1_nest_lock(tdb, TDB1_ACTIVE_LOCK, F_RDLCK, TDB_LOCK_WAIT) == -1) {
410 /* if needed, run recovery */
411 if (tdb1_transaction_recover(tdb) == -1) {
416 /* Internal (memory-only) databases skip all the code above to
417 * do with disk files, and resume here by releasing their
418 * open lock and hooking into the active list. */
419 if (tdb1_nest_unlock(tdb, TDB1_OPEN_LOCK, F_WRLCK) == -1) {
427 { int save_errno = errno;
433 if (tdb->flags & TDB1_INTERNAL)
434 SAFE_FREE(tdb->map_ptr);
439 if (close(tdb->fd) != 0)
440 tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
441 "tdb1_open_ex: failed to close tdb->fd on error!");
442 SAFE_FREE(tdb->lockrecs);
443 SAFE_FREE(tdb->name);
451 * Set the maximum number of dead records per hash chain
454 void tdb1_set_max_dead(struct tdb1_context *tdb, int max_dead)
456 tdb->max_dead_records = max_dead;
462 * @returns -1 for error; 0 for success.
464 int tdb1_close(struct tdb1_context *tdb)
466 struct tdb1_context **i;
469 if (tdb->transaction) {
470 tdb1_transaction_cancel(tdb);
474 if (tdb->flags & TDB1_INTERNAL)
475 SAFE_FREE(tdb->map_ptr);
479 SAFE_FREE(tdb->name);
481 ret = close(tdb->fd);
484 SAFE_FREE(tdb->lockrecs);
486 /* Remove from contexts list */
487 for (i = &tdb1s; *i; i = &(*i)->next) {
494 memset(tdb, 0, sizeof(*tdb));