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 "tdb_private.h"
33 non-blocking increment of the tdb sequence number if the tdb has been opened using
36 void tdb_increment_seqnum_nonblock(struct tdb_context *tdb)
40 if (!(tdb->flags & TDB_SEQNUM)) {
44 /* we ignore errors from this, as we have no sane way of
47 tdb_ofs_read(tdb, TDB_SEQNUM_OFS, &seqnum);
49 tdb_ofs_write(tdb, TDB_SEQNUM_OFS, &seqnum);
53 increment the tdb sequence number if the tdb has been opened using
56 static void tdb_increment_seqnum(struct tdb_context *tdb)
58 if (!(tdb->flags & TDB_SEQNUM)) {
62 if (tdb_brlock(tdb, TDB_SEQNUM_OFS, F_WRLCK, F_SETLKW, 1, 1) != 0) {
66 tdb_increment_seqnum_nonblock(tdb);
68 tdb_brlock(tdb, TDB_SEQNUM_OFS, F_UNLCK, F_SETLKW, 1, 1);
71 static int tdb_key_compare(TDB_DATA key, TDB_DATA data, void *private_data)
73 return memcmp(data.dptr, key.dptr, data.dsize);
76 /* Returns 0 on fail. On success, return offset of record, and fills
78 static tdb_off_t tdb_find(struct tdb_context *tdb, TDB_DATA key, uint32_t hash,
79 struct list_struct *r)
83 /* read in the hash top */
84 if (tdb_ofs_read(tdb, TDB_HASH_TOP(hash), &rec_ptr) == -1)
87 /* keep looking until we find the right record */
89 if (tdb_rec_read(tdb, rec_ptr, r) == -1)
92 if (!TDB_DEAD(r) && hash==r->full_hash
93 && key.dsize==r->key_len
94 && tdb_parse_data(tdb, key, rec_ptr + sizeof(*r),
95 r->key_len, tdb_key_compare,
99 /* detect tight infinite loop */
100 if (rec_ptr == r->next) {
101 TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_find: loop detected.\n"));
102 return TDB_ERRCODE(TDB_ERR_CORRUPT, 0);
106 return TDB_ERRCODE(TDB_ERR_NOEXIST, 0);
109 /* As tdb_find, but if you succeed, keep the lock */
110 tdb_off_t tdb_find_lock_hash(struct tdb_context *tdb, TDB_DATA key, uint32_t hash, int locktype,
111 struct list_struct *rec)
115 if (tdb_lock(tdb, BUCKET(hash), locktype) == -1)
117 if (!(rec_ptr = tdb_find(tdb, key, hash, rec)))
118 tdb_unlock(tdb, BUCKET(hash), locktype);
123 /* update an entry in place - this only works if the new data size
124 is <= the old data size and the key exists.
125 on failure return -1.
127 static int tdb_update_hash(struct tdb_context *tdb, TDB_DATA key, uint32_t hash, TDB_DATA dbuf)
129 struct list_struct rec;
133 if (!(rec_ptr = tdb_find(tdb, key, hash, &rec)))
136 /* must be long enough key, data and tailer */
137 if (rec.rec_len < key.dsize + dbuf.dsize + sizeof(tdb_off_t)) {
138 tdb->ecode = TDB_SUCCESS; /* Not really an error */
142 if (tdb->methods->tdb_write(tdb, rec_ptr + sizeof(rec) + rec.key_len,
143 dbuf.dptr, dbuf.dsize) == -1)
146 if (dbuf.dsize != rec.data_len) {
148 rec.data_len = dbuf.dsize;
149 return tdb_rec_write(tdb, rec_ptr, &rec);
155 /* find an entry in the database given a key */
156 /* If an entry doesn't exist tdb_err will be set to
157 * TDB_ERR_NOEXIST. If a key has no data attached
158 * then the TDB_DATA will have zero length but
161 static TDB_DATA _tdb_fetch(struct tdb_context *tdb, TDB_DATA key)
164 struct list_struct rec;
168 /* find which hash bucket it is in */
169 hash = tdb->hash_fn(&key);
170 if (!(rec_ptr = tdb_find_lock_hash(tdb,key,hash,F_RDLCK,&rec)))
173 ret.dptr = tdb_alloc_read(tdb, rec_ptr + sizeof(rec) + rec.key_len,
175 ret.dsize = rec.data_len;
176 tdb_unlock(tdb, BUCKET(rec.full_hash), F_RDLCK);
180 TDB_DATA tdb_fetch(struct tdb_context *tdb, TDB_DATA key)
182 TDB_DATA ret = _tdb_fetch(tdb, key);
184 tdb_trace_1rec_retrec(tdb, "tdb_fetch", key, ret);
189 * Find an entry in the database and hand the record's data to a parsing
190 * function. The parsing function is executed under the chain read lock, so it
191 * should be fast and should not block on other syscalls.
193 * DONT CALL OTHER TDB CALLS FROM THE PARSER, THIS MIGHT LEAD TO SEGFAULTS.
195 * For mmapped tdb's that do not have a transaction open it points the parsing
196 * function directly at the mmap area, it avoids the malloc/memcpy in this
197 * case. If a transaction is open or no mmap is available, it has to do
198 * malloc/read/parse/free.
200 * This is interesting for all readers of potentially large data structures in
201 * the tdb records, ldb indexes being one example.
204 int tdb_parse_record(struct tdb_context *tdb, TDB_DATA key,
205 int (*parser)(TDB_DATA key, TDB_DATA data,
210 struct list_struct rec;
214 /* find which hash bucket it is in */
215 hash = tdb->hash_fn(&key);
217 if (!(rec_ptr = tdb_find_lock_hash(tdb,key,hash,F_RDLCK,&rec))) {
218 tdb_trace_1rec_ret(tdb, "tdb_parse_record", key, -1);
219 return TDB_ERRCODE(TDB_ERR_NOEXIST, 0);
221 tdb_trace_1rec_ret(tdb, "tdb_parse_record", key, 0);
223 ret = tdb_parse_data(tdb, key, rec_ptr + sizeof(rec) + rec.key_len,
224 rec.data_len, parser, private_data);
226 tdb_unlock(tdb, BUCKET(rec.full_hash), F_RDLCK);
231 /* check if an entry in the database exists
233 note that 1 is returned if the key is found and 0 is returned if not found
234 this doesn't match the conventions in the rest of this module, but is
237 static int tdb_exists_hash(struct tdb_context *tdb, TDB_DATA key, uint32_t hash)
239 struct list_struct rec;
241 if (tdb_find_lock_hash(tdb, key, hash, F_RDLCK, &rec) == 0)
243 tdb_unlock(tdb, BUCKET(rec.full_hash), F_RDLCK);
247 int tdb_exists(struct tdb_context *tdb, TDB_DATA key)
249 uint32_t hash = tdb->hash_fn(&key);
252 ret = tdb_exists_hash(tdb, key, hash);
253 tdb_trace_1rec_ret(tdb, "tdb_exists", key, ret);
257 /* actually delete an entry in the database given the offset */
258 int tdb_do_delete(struct tdb_context *tdb, tdb_off_t rec_ptr, struct list_struct *rec)
260 tdb_off_t last_ptr, i;
261 struct list_struct lastrec;
263 if (tdb->read_only || tdb->traverse_read) return -1;
265 if (((tdb->traverse_write != 0) && (!TDB_DEAD(rec))) ||
266 tdb_write_lock_record(tdb, rec_ptr) == -1) {
267 /* Someone traversing here: mark it as dead */
268 rec->magic = TDB_DEAD_MAGIC;
269 return tdb_rec_write(tdb, rec_ptr, rec);
271 if (tdb_write_unlock_record(tdb, rec_ptr) != 0)
274 /* find previous record in hash chain */
275 if (tdb_ofs_read(tdb, TDB_HASH_TOP(rec->full_hash), &i) == -1)
277 for (last_ptr = 0; i != rec_ptr; last_ptr = i, i = lastrec.next)
278 if (tdb_rec_read(tdb, i, &lastrec) == -1)
281 /* unlink it: next ptr is at start of record. */
283 last_ptr = TDB_HASH_TOP(rec->full_hash);
284 if (tdb_ofs_write(tdb, last_ptr, &rec->next) == -1)
287 /* recover the space */
288 if (tdb_free(tdb, rec_ptr, rec) == -1)
293 static int tdb_count_dead(struct tdb_context *tdb, uint32_t hash)
297 struct list_struct rec;
299 /* read in the hash top */
300 if (tdb_ofs_read(tdb, TDB_HASH_TOP(hash), &rec_ptr) == -1)
304 if (tdb_rec_read(tdb, rec_ptr, &rec) == -1)
307 if (rec.magic == TDB_DEAD_MAGIC) {
316 * Purge all DEAD records from a hash chain
318 static int tdb_purge_dead(struct tdb_context *tdb, uint32_t hash)
321 struct list_struct rec;
324 if (tdb_lock(tdb, -1, F_WRLCK) == -1) {
328 /* read in the hash top */
329 if (tdb_ofs_read(tdb, TDB_HASH_TOP(hash), &rec_ptr) == -1)
335 if (tdb_rec_read(tdb, rec_ptr, &rec) == -1) {
341 if (rec.magic == TDB_DEAD_MAGIC
342 && tdb_do_delete(tdb, rec_ptr, &rec) == -1) {
349 tdb_unlock(tdb, -1, F_WRLCK);
353 /* delete an entry in the database given a key */
354 static int tdb_delete_hash(struct tdb_context *tdb, TDB_DATA key, uint32_t hash)
357 struct list_struct rec;
360 if (tdb->max_dead_records != 0) {
363 * Allow for some dead records per hash chain, mainly for
364 * tdb's with a very high create/delete rate like locking.tdb.
367 if (tdb_lock(tdb, BUCKET(hash), F_WRLCK) == -1)
370 if (tdb_count_dead(tdb, hash) >= tdb->max_dead_records) {
372 * Don't let the per-chain freelist grow too large,
373 * delete all existing dead records
375 tdb_purge_dead(tdb, hash);
378 if (!(rec_ptr = tdb_find(tdb, key, hash, &rec))) {
379 tdb_unlock(tdb, BUCKET(hash), F_WRLCK);
384 * Just mark the record as dead.
386 rec.magic = TDB_DEAD_MAGIC;
387 ret = tdb_rec_write(tdb, rec_ptr, &rec);
390 if (!(rec_ptr = tdb_find_lock_hash(tdb, key, hash, F_WRLCK,
394 ret = tdb_do_delete(tdb, rec_ptr, &rec);
398 tdb_increment_seqnum(tdb);
401 if (tdb_unlock(tdb, BUCKET(rec.full_hash), F_WRLCK) != 0)
402 TDB_LOG((tdb, TDB_DEBUG_WARNING, "tdb_delete: WARNING tdb_unlock failed!\n"));
406 int tdb_delete(struct tdb_context *tdb, TDB_DATA key)
408 uint32_t hash = tdb->hash_fn(&key);
411 ret = tdb_delete_hash(tdb, key, hash);
412 tdb_trace_1rec_ret(tdb, "tdb_delete", key, ret);
417 * See if we have a dead record around with enough space
419 static tdb_off_t tdb_find_dead(struct tdb_context *tdb, uint32_t hash,
420 struct list_struct *r, tdb_len_t length)
424 /* read in the hash top */
425 if (tdb_ofs_read(tdb, TDB_HASH_TOP(hash), &rec_ptr) == -1)
428 /* keep looking until we find the right record */
430 if (tdb_rec_read(tdb, rec_ptr, r) == -1)
433 if (TDB_DEAD(r) && r->rec_len >= length) {
435 * First fit for simple coding, TODO: change to best
445 static int _tdb_store(struct tdb_context *tdb, TDB_DATA key,
446 TDB_DATA dbuf, int flag, uint32_t hash)
448 struct list_struct rec;
453 /* check for it existing, on insert. */
454 if (flag == TDB_INSERT) {
455 if (tdb_exists_hash(tdb, key, hash)) {
456 tdb->ecode = TDB_ERR_EXISTS;
460 /* first try in-place update, on modify or replace. */
461 if (tdb_update_hash(tdb, key, hash, dbuf) == 0) {
464 if (tdb->ecode == TDB_ERR_NOEXIST &&
465 flag == TDB_MODIFY) {
466 /* if the record doesn't exist and we are in TDB_MODIFY mode then
467 we should fail the store */
471 /* reset the error code potentially set by the tdb_update() */
472 tdb->ecode = TDB_SUCCESS;
474 /* delete any existing record - if it doesn't exist we don't
475 care. Doing this first reduces fragmentation, and avoids
476 coalescing with `allocated' block before it's updated. */
477 if (flag != TDB_INSERT)
478 tdb_delete_hash(tdb, key, hash);
480 /* Copy key+value *before* allocating free space in case malloc
481 fails and we are left with a dead spot in the tdb. */
483 if (!(p = (char *)malloc(key.dsize + dbuf.dsize))) {
484 tdb->ecode = TDB_ERR_OOM;
488 memcpy(p, key.dptr, key.dsize);
490 memcpy(p+key.dsize, dbuf.dptr, dbuf.dsize);
492 if (tdb->max_dead_records != 0) {
494 * Allow for some dead records per hash chain, look if we can
495 * find one that can hold the new record. We need enough space
496 * for key, data and tailer. If we find one, we don't have to
497 * consult the central freelist.
499 rec_ptr = tdb_find_dead(
501 key.dsize + dbuf.dsize + sizeof(tdb_off_t));
504 rec.key_len = key.dsize;
505 rec.data_len = dbuf.dsize;
506 rec.full_hash = hash;
507 rec.magic = TDB_MAGIC;
508 if (tdb_rec_write(tdb, rec_ptr, &rec) == -1
509 || tdb->methods->tdb_write(
510 tdb, rec_ptr + sizeof(rec),
511 p, key.dsize + dbuf.dsize) == -1) {
519 * We have to allocate some space from the freelist, so this means we
520 * have to lock it. Use the chance to purge all the DEAD records from
521 * the hash chain under the freelist lock.
524 if (tdb_lock(tdb, -1, F_WRLCK) == -1) {
528 if ((tdb->max_dead_records != 0)
529 && (tdb_purge_dead(tdb, hash) == -1)) {
530 tdb_unlock(tdb, -1, F_WRLCK);
534 /* we have to allocate some space */
535 rec_ptr = tdb_allocate(tdb, key.dsize + dbuf.dsize, &rec);
537 tdb_unlock(tdb, -1, F_WRLCK);
543 /* Read hash top into next ptr */
544 if (tdb_ofs_read(tdb, TDB_HASH_TOP(hash), &rec.next) == -1)
547 rec.key_len = key.dsize;
548 rec.data_len = dbuf.dsize;
549 rec.full_hash = hash;
550 rec.magic = TDB_MAGIC;
552 /* write out and point the top of the hash chain at it */
553 if (tdb_rec_write(tdb, rec_ptr, &rec) == -1
554 || tdb->methods->tdb_write(tdb, rec_ptr+sizeof(rec), p, key.dsize+dbuf.dsize)==-1
555 || tdb_ofs_write(tdb, TDB_HASH_TOP(hash), &rec_ptr) == -1) {
556 /* Need to tdb_unallocate() here */
564 tdb_increment_seqnum(tdb);
571 /* store an element in the database, replacing any existing element
574 return 0 on success, -1 on failure
576 int tdb_store(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf, int flag)
581 if (tdb->read_only || tdb->traverse_read) {
582 tdb->ecode = TDB_ERR_RDONLY;
583 tdb_trace_2rec_flag_ret(tdb, "tdb_store", key, dbuf, flag, -1);
587 /* find which hash bucket it is in */
588 hash = tdb->hash_fn(&key);
589 if (tdb_lock(tdb, BUCKET(hash), F_WRLCK) == -1)
592 ret = _tdb_store(tdb, key, dbuf, flag, hash);
593 tdb_trace_2rec_flag_ret(tdb, "tdb_store", key, dbuf, flag, ret);
594 tdb_unlock(tdb, BUCKET(hash), F_WRLCK);
598 /* Append to an entry. Create if not exist. */
599 int tdb_append(struct tdb_context *tdb, TDB_DATA key, TDB_DATA new_dbuf)
605 /* find which hash bucket it is in */
606 hash = tdb->hash_fn(&key);
607 if (tdb_lock(tdb, BUCKET(hash), F_WRLCK) == -1)
610 dbuf = _tdb_fetch(tdb, key);
612 if (dbuf.dptr == NULL) {
613 dbuf.dptr = (unsigned char *)malloc(new_dbuf.dsize);
615 unsigned int new_len = dbuf.dsize + new_dbuf.dsize;
616 unsigned char *new_dptr;
618 /* realloc '0' is special: don't do that. */
621 new_dptr = (unsigned char *)realloc(dbuf.dptr, new_len);
622 if (new_dptr == NULL) {
625 dbuf.dptr = new_dptr;
628 if (dbuf.dptr == NULL) {
629 tdb->ecode = TDB_ERR_OOM;
633 memcpy(dbuf.dptr + dbuf.dsize, new_dbuf.dptr, new_dbuf.dsize);
634 dbuf.dsize += new_dbuf.dsize;
636 ret = _tdb_store(tdb, key, dbuf, 0, hash);
637 tdb_trace_2rec_retrec(tdb, "tdb_append", key, new_dbuf, dbuf);
640 tdb_unlock(tdb, BUCKET(hash), F_WRLCK);
641 SAFE_FREE(dbuf.dptr);
647 return the name of the current tdb file
648 useful for external logging functions
650 const char *tdb_name(struct tdb_context *tdb)
656 return the underlying file descriptor being used by tdb, or -1
657 useful for external routines that want to check the device/inode
660 int tdb_fd(struct tdb_context *tdb)
666 return the current logging function
667 useful for external tdb routines that wish to log tdb errors
669 tdb_log_func tdb_log_fn(struct tdb_context *tdb)
671 return tdb->log.log_fn;
676 get the tdb sequence number. Only makes sense if the writers opened
677 with TDB_SEQNUM set. Note that this sequence number will wrap quite
678 quickly, so it should only be used for a 'has something changed'
679 test, not for code that relies on the count of the number of changes
680 made. If you want a counter then use a tdb record.
682 The aim of this sequence number is to allow for a very lightweight
683 test of a possible tdb change.
685 int tdb_get_seqnum(struct tdb_context *tdb)
689 tdb_ofs_read(tdb, TDB_SEQNUM_OFS, &seqnum);
693 int tdb_hash_size(struct tdb_context *tdb)
695 return tdb->header.hash_size;
698 size_t tdb_map_size(struct tdb_context *tdb)
700 return tdb->map_size;
703 int tdb_get_flags(struct tdb_context *tdb)
708 void tdb_add_flags(struct tdb_context *tdb, unsigned flags)
713 void tdb_remove_flags(struct tdb_context *tdb, unsigned flags)
715 tdb->flags &= ~flags;
720 enable sequence number handling on an open tdb
722 void tdb_enable_seqnum(struct tdb_context *tdb)
724 tdb->flags |= TDB_SEQNUM;
729 add a region of the file to the freelist. Length is the size of the region in bytes,
730 which includes the free list header that needs to be added
732 static int tdb_free_region(struct tdb_context *tdb, tdb_off_t offset, ssize_t length)
734 struct list_struct rec;
735 if (length <= sizeof(rec)) {
736 /* the region is not worth adding */
739 if (length + offset > tdb->map_size) {
740 TDB_LOG((tdb, TDB_DEBUG_FATAL,"tdb_free_region: adding region beyond end of file\n"));
743 memset(&rec,'\0',sizeof(rec));
744 rec.rec_len = length - sizeof(rec);
745 if (tdb_free(tdb, offset, &rec) == -1) {
746 TDB_LOG((tdb, TDB_DEBUG_FATAL,"tdb_free_region: failed to add free record\n"));
753 wipe the entire database, deleting all records. This can be done
754 very fast by using a global lock. The entire data portion of the
755 file becomes a single entry in the freelist.
757 This code carefully steps around the recovery area, leaving it alone
759 int tdb_wipe_all(struct tdb_context *tdb)
762 tdb_off_t offset = 0;
764 tdb_off_t recovery_head;
765 tdb_len_t recovery_size = 0;
767 if (tdb_lockall(tdb) != 0) {
771 tdb_trace(tdb, "tdb_wipe_all");
773 /* see if the tdb has a recovery area, and remember its size
774 if so. We don't want to lose this as otherwise each
775 tdb_wipe_all() in a transaction will increase the size of
776 the tdb by the size of the recovery area */
777 if (tdb_ofs_read(tdb, TDB_RECOVERY_HEAD, &recovery_head) == -1) {
778 TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_wipe_all: failed to read recovery head\n"));
782 if (recovery_head != 0) {
783 struct list_struct rec;
784 if (tdb->methods->tdb_read(tdb, recovery_head, &rec, sizeof(rec), DOCONV()) == -1) {
785 TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_wipe_all: failed to read recovery record\n"));
788 recovery_size = rec.rec_len + sizeof(rec);
791 /* wipe the hashes */
792 for (i=0;i<tdb->header.hash_size;i++) {
793 if (tdb_ofs_write(tdb, TDB_HASH_TOP(i), &offset) == -1) {
794 TDB_LOG((tdb, TDB_DEBUG_FATAL,"tdb_wipe_all: failed to write hash %d\n", i));
799 /* wipe the freelist */
800 if (tdb_ofs_write(tdb, FREELIST_TOP, &offset) == -1) {
801 TDB_LOG((tdb, TDB_DEBUG_FATAL,"tdb_wipe_all: failed to write freelist\n"));
805 /* add all the rest of the file to the freelist, possibly leaving a gap
806 for the recovery area */
807 if (recovery_size == 0) {
808 /* the simple case - the whole file can be used as a freelist */
809 data_len = (tdb->map_size - TDB_DATA_START(tdb->header.hash_size));
810 if (tdb_free_region(tdb, TDB_DATA_START(tdb->header.hash_size), data_len) != 0) {
814 /* we need to add two freelist entries - one on either
815 side of the recovery area
817 Note that we cannot shift the recovery area during
818 this operation. Only the transaction.c code may
819 move the recovery area or we risk subtle data
822 data_len = (recovery_head - TDB_DATA_START(tdb->header.hash_size));
823 if (tdb_free_region(tdb, TDB_DATA_START(tdb->header.hash_size), data_len) != 0) {
826 /* and the 2nd free list entry after the recovery area - if any */
827 data_len = tdb->map_size - (recovery_head+recovery_size);
828 if (tdb_free_region(tdb, recovery_head+recovery_size, data_len) != 0) {
833 if (tdb_unlockall(tdb) != 0) {
834 TDB_LOG((tdb, TDB_DEBUG_FATAL,"tdb_wipe_all: failed to unlock\n"));
845 struct traverse_state {
847 struct tdb_context *dest_db;
851 traverse function for repacking
853 static int repack_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *private_data)
855 struct traverse_state *state = (struct traverse_state *)private_data;
856 if (tdb_store(state->dest_db, key, data, TDB_INSERT) != 0) {
866 int tdb_repack(struct tdb_context *tdb)
868 struct tdb_context *tmp_db;
869 struct traverse_state state;
871 tdb_trace(tdb, "tdb_repack");
873 if (tdb_transaction_start(tdb) != 0) {
874 TDB_LOG((tdb, TDB_DEBUG_FATAL, __location__ " Failed to start transaction\n"));
878 tmp_db = tdb_open("tmpdb", tdb_hash_size(tdb), TDB_INTERNAL, O_RDWR|O_CREAT, 0);
879 if (tmp_db == NULL) {
880 TDB_LOG((tdb, TDB_DEBUG_FATAL, __location__ " Failed to create tmp_db\n"));
881 tdb_transaction_cancel(tdb);
886 state.dest_db = tmp_db;
888 if (tdb_traverse_read(tdb, repack_traverse, &state) == -1) {
889 TDB_LOG((tdb, TDB_DEBUG_FATAL, __location__ " Failed to traverse copying out\n"));
890 tdb_transaction_cancel(tdb);
896 TDB_LOG((tdb, TDB_DEBUG_FATAL, __location__ " Error during traversal\n"));
897 tdb_transaction_cancel(tdb);
902 if (tdb_wipe_all(tdb) != 0) {
903 TDB_LOG((tdb, TDB_DEBUG_FATAL, __location__ " Failed to wipe database\n"));
904 tdb_transaction_cancel(tdb);
912 if (tdb_traverse_read(tmp_db, repack_traverse, &state) == -1) {
913 TDB_LOG((tdb, TDB_DEBUG_FATAL, __location__ " Failed to traverse copying back\n"));
914 tdb_transaction_cancel(tdb);
920 TDB_LOG((tdb, TDB_DEBUG_FATAL, __location__ " Error during second traversal\n"));
921 tdb_transaction_cancel(tdb);
928 if (tdb_transaction_commit(tdb) != 0) {
929 TDB_LOG((tdb, TDB_DEBUG_FATAL, __location__ " Failed to commit\n"));
937 static void tdb_trace_write(struct tdb_context *tdb, const char *str)
939 if (write(tdb->tracefd, str, strlen(str)) != strlen(str)) {
945 static void tdb_trace_start(struct tdb_context *tdb)
948 char msg[sizeof(tdb_off_t) * 4 + 1];
950 tdb_ofs_read(tdb, TDB_SEQNUM_OFS, &seqnum);
951 snprintf(msg, sizeof(msg), "%u ", seqnum);
952 tdb_trace_write(tdb, msg);
955 static void tdb_trace_end(struct tdb_context *tdb)
957 tdb_trace_write(tdb, "\n");
960 static void tdb_trace_end_ret(struct tdb_context *tdb, int ret)
962 char msg[sizeof(ret) * 4 + 4];
963 snprintf(msg, sizeof(msg), " = %i\n", ret);
964 tdb_trace_write(tdb, msg);
967 static void tdb_trace_record(struct tdb_context *tdb, TDB_DATA rec)
969 char msg[20 + rec.dsize*2], *p;
972 /* We differentiate zero-length records from non-existent ones. */
973 if (rec.dptr == NULL) {
974 tdb_trace_write(tdb, " NULL");
978 /* snprintf here is purely cargo-cult programming. */
980 p += snprintf(p, sizeof(msg), " %zu:", rec.dsize);
981 for (i = 0; i < rec.dsize; i++)
982 p += snprintf(p, 2, "%02x", rec.dptr[i]);
984 tdb_trace_write(tdb, msg);
987 void tdb_trace(struct tdb_context *tdb, const char *op)
989 tdb_trace_start(tdb);
990 tdb_trace_write(tdb, op);
994 void tdb_trace_seqnum(struct tdb_context *tdb, uint32_t seqnum, const char *op)
996 char msg[sizeof(tdb_off_t) * 4 + 1];
998 snprintf(msg, sizeof(msg), "%u ", seqnum);
999 tdb_trace_write(tdb, msg);
1000 tdb_trace_write(tdb, op);
1004 void tdb_trace_open(struct tdb_context *tdb, const char *op,
1005 unsigned hash_size, unsigned tdb_flags, unsigned open_flags)
1009 snprintf(msg, sizeof(msg),
1010 "%s %u 0x%x 0x%x", op, hash_size, tdb_flags, open_flags);
1011 tdb_trace_start(tdb);
1012 tdb_trace_write(tdb, msg);
1016 void tdb_trace_ret(struct tdb_context *tdb, const char *op, int ret)
1018 tdb_trace_start(tdb);
1019 tdb_trace_write(tdb, op);
1020 tdb_trace_end_ret(tdb, ret);
1023 void tdb_trace_retrec(struct tdb_context *tdb, const char *op, TDB_DATA ret)
1025 tdb_trace_start(tdb);
1026 tdb_trace_write(tdb, op);
1027 tdb_trace_write(tdb, " =");
1028 tdb_trace_record(tdb, ret);
1032 void tdb_trace_1rec(struct tdb_context *tdb, const char *op,
1035 tdb_trace_start(tdb);
1036 tdb_trace_write(tdb, op);
1037 tdb_trace_record(tdb, rec);
1041 void tdb_trace_1rec_ret(struct tdb_context *tdb, const char *op,
1042 TDB_DATA rec, int ret)
1044 tdb_trace_start(tdb);
1045 tdb_trace_write(tdb, op);
1046 tdb_trace_record(tdb, rec);
1047 tdb_trace_end_ret(tdb, ret);
1050 void tdb_trace_1rec_retrec(struct tdb_context *tdb, const char *op,
1051 TDB_DATA rec, TDB_DATA ret)
1053 tdb_trace_start(tdb);
1054 tdb_trace_write(tdb, op);
1055 tdb_trace_record(tdb, rec);
1056 tdb_trace_write(tdb, " =");
1057 tdb_trace_record(tdb, ret);
1061 void tdb_trace_2rec_flag_ret(struct tdb_context *tdb, const char *op,
1062 TDB_DATA rec1, TDB_DATA rec2, unsigned flag,
1065 char msg[1 + sizeof(ret) * 4];
1067 snprintf(msg, sizeof(msg), " %#x", flag);
1068 tdb_trace_start(tdb);
1069 tdb_trace_write(tdb, op);
1070 tdb_trace_record(tdb, rec1);
1071 tdb_trace_record(tdb, rec2);
1072 tdb_trace_write(tdb, msg);
1073 tdb_trace_end_ret(tdb, ret);
1076 void tdb_trace_2rec_retrec(struct tdb_context *tdb, const char *op,
1077 TDB_DATA rec1, TDB_DATA rec2, TDB_DATA ret)
1079 tdb_trace_start(tdb);
1080 tdb_trace_write(tdb, op);
1081 tdb_trace_record(tdb, rec1);
1082 tdb_trace_record(tdb, rec2);
1083 tdb_trace_write(tdb, " =");
1084 tdb_trace_record(tdb, ret);