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,
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->ecode = TDB_ERR_CORRUPT;
102 TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_find: loop detected.\n"));
107 tdb->ecode = TDB_ERR_NOEXIST;
111 /* As tdb_find, but if you succeed, keep the lock */
112 tdb_off_t tdb_find_lock_hash(struct tdb_context *tdb, TDB_DATA key, uint32_t hash, int locktype,
113 struct tdb_record *rec)
117 if (tdb_lock(tdb, BUCKET(hash), locktype) == -1)
119 if (!(rec_ptr = tdb_find(tdb, key, hash, rec)))
120 tdb_unlock(tdb, BUCKET(hash), locktype);
124 static TDB_DATA _tdb_fetch(struct tdb_context *tdb, TDB_DATA key);
126 /* update an entry in place - this only works if the new data size
127 is <= the old data size and the key exists.
128 on failure return -1.
130 static int tdb_update_hash(struct tdb_context *tdb, TDB_DATA key, uint32_t hash, TDB_DATA dbuf)
132 struct tdb_record rec;
136 if (!(rec_ptr = tdb_find(tdb, key, hash, &rec)))
139 /* it could be an exact duplicate of what is there - this is
140 * surprisingly common (eg. with a ldb re-index). */
141 if (rec.key_len == key.dsize &&
142 rec.data_len == dbuf.dsize &&
143 rec.full_hash == hash) {
144 TDB_DATA data = _tdb_fetch(tdb, key);
145 if (data.dsize == dbuf.dsize &&
146 memcmp(data.dptr, dbuf.dptr, data.dsize) == 0) {
158 /* must be long enough key, data and tailer */
159 if (rec.rec_len < key.dsize + dbuf.dsize + sizeof(tdb_off_t)) {
160 tdb->ecode = TDB_SUCCESS; /* Not really an error */
164 if (tdb->methods->tdb_write(tdb, rec_ptr + sizeof(rec) + rec.key_len,
165 dbuf.dptr, dbuf.dsize) == -1)
168 if (dbuf.dsize != rec.data_len) {
170 rec.data_len = dbuf.dsize;
171 return tdb_rec_write(tdb, rec_ptr, &rec);
177 /* find an entry in the database given a key */
178 /* If an entry doesn't exist tdb_err will be set to
179 * TDB_ERR_NOEXIST. If a key has no data attached
180 * then the TDB_DATA will have zero length but
183 static TDB_DATA _tdb_fetch(struct tdb_context *tdb, TDB_DATA key)
186 struct tdb_record rec;
190 /* find which hash bucket it is in */
191 hash = tdb->hash_fn(&key);
192 if (!(rec_ptr = tdb_find_lock_hash(tdb,key,hash,F_RDLCK,&rec)))
195 ret.dptr = tdb_alloc_read(tdb, rec_ptr + sizeof(rec) + rec.key_len,
197 ret.dsize = rec.data_len;
198 tdb_unlock(tdb, BUCKET(rec.full_hash), F_RDLCK);
202 TDB_DATA tdb_fetch(struct tdb_context *tdb, TDB_DATA key)
204 TDB_DATA ret = _tdb_fetch(tdb, key);
206 tdb_trace_1rec_retrec(tdb, "tdb_fetch", key, ret);
211 * Find an entry in the database and hand the record's data to a parsing
212 * function. The parsing function is executed under the chain read lock, so it
213 * should be fast and should not block on other syscalls.
215 * DONT CALL OTHER TDB CALLS FROM THE PARSER, THIS MIGHT LEAD TO SEGFAULTS.
217 * For mmapped tdb's that do not have a transaction open it points the parsing
218 * function directly at the mmap area, it avoids the malloc/memcpy in this
219 * case. If a transaction is open or no mmap is available, it has to do
220 * malloc/read/parse/free.
222 * This is interesting for all readers of potentially large data structures in
223 * the tdb records, ldb indexes being one example.
226 int tdb_parse_record(struct tdb_context *tdb, TDB_DATA key,
227 int (*parser)(TDB_DATA key, TDB_DATA data,
232 struct tdb_record rec;
236 /* find which hash bucket it is in */
237 hash = tdb->hash_fn(&key);
239 if (!(rec_ptr = tdb_find_lock_hash(tdb,key,hash,F_RDLCK,&rec))) {
240 tdb_trace_1rec_ret(tdb, "tdb_parse_record", key, -1);
241 tdb->ecode = TDB_ERR_NOEXIST;
244 tdb_trace_1rec_ret(tdb, "tdb_parse_record", key, 0);
246 ret = tdb_parse_data(tdb, key, rec_ptr + sizeof(rec) + rec.key_len,
247 rec.data_len, parser, private_data);
249 tdb_unlock(tdb, BUCKET(rec.full_hash), F_RDLCK);
254 /* check if an entry in the database exists
256 note that 1 is returned if the key is found and 0 is returned if not found
257 this doesn't match the conventions in the rest of this module, but is
260 static int tdb_exists_hash(struct tdb_context *tdb, TDB_DATA key, uint32_t hash)
262 struct tdb_record rec;
264 if (tdb_find_lock_hash(tdb, key, hash, F_RDLCK, &rec) == 0)
266 tdb_unlock(tdb, BUCKET(rec.full_hash), F_RDLCK);
270 int tdb_exists(struct tdb_context *tdb, TDB_DATA key)
272 uint32_t hash = tdb->hash_fn(&key);
275 ret = tdb_exists_hash(tdb, key, hash);
276 tdb_trace_1rec_ret(tdb, "tdb_exists", key, ret);
280 /* actually delete an entry in the database given the offset */
281 int tdb_do_delete(struct tdb_context *tdb, tdb_off_t rec_ptr, struct tdb_record *rec)
283 tdb_off_t last_ptr, i;
284 struct tdb_record lastrec;
286 if (tdb->read_only || tdb->traverse_read) return -1;
288 if (((tdb->traverse_write != 0) && (!TDB_DEAD(rec))) ||
289 tdb_write_lock_record(tdb, rec_ptr) == -1) {
290 /* Someone traversing here: mark it as dead */
291 rec->magic = TDB_DEAD_MAGIC;
292 return tdb_rec_write(tdb, rec_ptr, rec);
294 if (tdb_write_unlock_record(tdb, rec_ptr) != 0)
297 /* find previous record in hash chain */
298 if (tdb_ofs_read(tdb, TDB_HASH_TOP(rec->full_hash), &i) == -1)
300 for (last_ptr = 0; i != rec_ptr; last_ptr = i, i = lastrec.next)
301 if (tdb_rec_read(tdb, i, &lastrec) == -1)
304 /* unlink it: next ptr is at start of record. */
306 last_ptr = TDB_HASH_TOP(rec->full_hash);
307 if (tdb_ofs_write(tdb, last_ptr, &rec->next) == -1)
310 /* recover the space */
311 if (tdb_free(tdb, rec_ptr, rec) == -1)
316 static int tdb_count_dead(struct tdb_context *tdb, uint32_t hash)
320 struct tdb_record rec;
322 /* read in the hash top */
323 if (tdb_ofs_read(tdb, TDB_HASH_TOP(hash), &rec_ptr) == -1)
327 if (tdb_rec_read(tdb, rec_ptr, &rec) == -1)
330 if (rec.magic == TDB_DEAD_MAGIC) {
339 * Purge all DEAD records from a hash chain
341 static int tdb_purge_dead(struct tdb_context *tdb, uint32_t hash)
344 struct tdb_record rec;
347 if (tdb_lock(tdb, -1, F_WRLCK) == -1) {
351 /* read in the hash top */
352 if (tdb_ofs_read(tdb, TDB_HASH_TOP(hash), &rec_ptr) == -1)
358 if (tdb_rec_read(tdb, rec_ptr, &rec) == -1) {
364 if (rec.magic == TDB_DEAD_MAGIC
365 && tdb_do_delete(tdb, rec_ptr, &rec) == -1) {
372 tdb_unlock(tdb, -1, F_WRLCK);
376 /* delete an entry in the database given a key */
377 static int tdb_delete_hash(struct tdb_context *tdb, TDB_DATA key, uint32_t hash)
380 struct tdb_record rec;
383 if (tdb->max_dead_records != 0) {
386 * Allow for some dead records per hash chain, mainly for
387 * tdb's with a very high create/delete rate like locking.tdb.
390 if (tdb_lock(tdb, BUCKET(hash), F_WRLCK) == -1)
393 if (tdb_count_dead(tdb, hash) >= tdb->max_dead_records) {
395 * Don't let the per-chain freelist grow too large,
396 * delete all existing dead records
398 tdb_purge_dead(tdb, hash);
401 if (!(rec_ptr = tdb_find(tdb, key, hash, &rec))) {
402 tdb_unlock(tdb, BUCKET(hash), F_WRLCK);
407 * Just mark the record as dead.
409 rec.magic = TDB_DEAD_MAGIC;
410 ret = tdb_rec_write(tdb, rec_ptr, &rec);
413 if (!(rec_ptr = tdb_find_lock_hash(tdb, key, hash, F_WRLCK,
417 ret = tdb_do_delete(tdb, rec_ptr, &rec);
421 tdb_increment_seqnum(tdb);
424 if (tdb_unlock(tdb, BUCKET(rec.full_hash), F_WRLCK) != 0)
425 TDB_LOG((tdb, TDB_DEBUG_WARNING, "tdb_delete: WARNING tdb_unlock failed!\n"));
429 int tdb_delete(struct tdb_context *tdb, TDB_DATA key)
431 uint32_t hash = tdb->hash_fn(&key);
434 ret = tdb_delete_hash(tdb, key, hash);
435 tdb_trace_1rec_ret(tdb, "tdb_delete", key, ret);
440 * See if we have a dead record around with enough space
442 static tdb_off_t tdb_find_dead(struct tdb_context *tdb, uint32_t hash,
443 struct tdb_record *r, tdb_len_t length)
447 /* read in the hash top */
448 if (tdb_ofs_read(tdb, TDB_HASH_TOP(hash), &rec_ptr) == -1)
451 /* keep looking until we find the right record */
453 if (tdb_rec_read(tdb, rec_ptr, r) == -1)
456 if (TDB_DEAD(r) && r->rec_len >= length) {
458 * First fit for simple coding, TODO: change to best
468 static int _tdb_store(struct tdb_context *tdb, TDB_DATA key,
469 TDB_DATA dbuf, int flag, uint32_t hash)
471 struct tdb_record rec;
476 /* check for it existing, on insert. */
477 if (flag == TDB_INSERT) {
478 if (tdb_exists_hash(tdb, key, hash)) {
479 tdb->ecode = TDB_ERR_EXISTS;
483 /* first try in-place update, on modify or replace. */
484 if (tdb_update_hash(tdb, key, hash, dbuf) == 0) {
487 if (tdb->ecode == TDB_ERR_NOEXIST &&
488 flag == TDB_MODIFY) {
489 /* if the record doesn't exist and we are in TDB_MODIFY mode then
490 we should fail the store */
494 /* reset the error code potentially set by the tdb_update() */
495 tdb->ecode = TDB_SUCCESS;
497 /* delete any existing record - if it doesn't exist we don't
498 care. Doing this first reduces fragmentation, and avoids
499 coalescing with `allocated' block before it's updated. */
500 if (flag != TDB_INSERT)
501 tdb_delete_hash(tdb, key, hash);
503 /* Copy key+value *before* allocating free space in case malloc
504 fails and we are left with a dead spot in the tdb. */
506 if (!(p = (char *)malloc(key.dsize + dbuf.dsize))) {
507 tdb->ecode = TDB_ERR_OOM;
511 memcpy(p, key.dptr, key.dsize);
513 memcpy(p+key.dsize, dbuf.dptr, dbuf.dsize);
515 if (tdb->max_dead_records != 0) {
517 * Allow for some dead records per hash chain, look if we can
518 * find one that can hold the new record. We need enough space
519 * for key, data and tailer. If we find one, we don't have to
520 * consult the central freelist.
522 rec_ptr = tdb_find_dead(
524 key.dsize + dbuf.dsize + sizeof(tdb_off_t));
527 rec.key_len = key.dsize;
528 rec.data_len = dbuf.dsize;
529 rec.full_hash = hash;
530 rec.magic = TDB_MAGIC;
531 if (tdb_rec_write(tdb, rec_ptr, &rec) == -1
532 || tdb->methods->tdb_write(
533 tdb, rec_ptr + sizeof(rec),
534 p, key.dsize + dbuf.dsize) == -1) {
542 * We have to allocate some space from the freelist, so this means we
543 * have to lock it. Use the chance to purge all the DEAD records from
544 * the hash chain under the freelist lock.
547 if (tdb_lock(tdb, -1, F_WRLCK) == -1) {
551 if ((tdb->max_dead_records != 0)
552 && (tdb_purge_dead(tdb, hash) == -1)) {
553 tdb_unlock(tdb, -1, F_WRLCK);
557 /* we have to allocate some space */
558 rec_ptr = tdb_allocate(tdb, key.dsize + dbuf.dsize, &rec);
560 tdb_unlock(tdb, -1, F_WRLCK);
566 /* Read hash top into next ptr */
567 if (tdb_ofs_read(tdb, TDB_HASH_TOP(hash), &rec.next) == -1)
570 rec.key_len = key.dsize;
571 rec.data_len = dbuf.dsize;
572 rec.full_hash = hash;
573 rec.magic = TDB_MAGIC;
575 /* write out and point the top of the hash chain at it */
576 if (tdb_rec_write(tdb, rec_ptr, &rec) == -1
577 || tdb->methods->tdb_write(tdb, rec_ptr+sizeof(rec), p, key.dsize+dbuf.dsize)==-1
578 || tdb_ofs_write(tdb, TDB_HASH_TOP(hash), &rec_ptr) == -1) {
579 /* Need to tdb_unallocate() here */
587 tdb_increment_seqnum(tdb);
594 /* store an element in the database, replacing any existing element
597 return 0 on success, -1 on failure
599 int tdb_store(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf, int flag)
604 if (tdb->read_only || tdb->traverse_read) {
605 tdb->ecode = TDB_ERR_RDONLY;
606 tdb_trace_2rec_flag_ret(tdb, "tdb_store", key, dbuf, flag, -1);
610 /* find which hash bucket it is in */
611 hash = tdb->hash_fn(&key);
612 if (tdb_lock(tdb, BUCKET(hash), F_WRLCK) == -1)
615 ret = _tdb_store(tdb, key, dbuf, flag, hash);
616 tdb_trace_2rec_flag_ret(tdb, "tdb_store", key, dbuf, flag, ret);
617 tdb_unlock(tdb, BUCKET(hash), F_WRLCK);
621 /* Append to an entry. Create if not exist. */
622 int tdb_append(struct tdb_context *tdb, TDB_DATA key, TDB_DATA new_dbuf)
628 /* find which hash bucket it is in */
629 hash = tdb->hash_fn(&key);
630 if (tdb_lock(tdb, BUCKET(hash), F_WRLCK) == -1)
633 dbuf = _tdb_fetch(tdb, key);
635 if (dbuf.dptr == NULL) {
636 dbuf.dptr = (unsigned char *)malloc(new_dbuf.dsize);
638 unsigned int new_len = dbuf.dsize + new_dbuf.dsize;
639 unsigned char *new_dptr;
641 /* realloc '0' is special: don't do that. */
644 new_dptr = (unsigned char *)realloc(dbuf.dptr, new_len);
645 if (new_dptr == NULL) {
648 dbuf.dptr = new_dptr;
651 if (dbuf.dptr == NULL) {
652 tdb->ecode = TDB_ERR_OOM;
656 memcpy(dbuf.dptr + dbuf.dsize, new_dbuf.dptr, new_dbuf.dsize);
657 dbuf.dsize += new_dbuf.dsize;
659 ret = _tdb_store(tdb, key, dbuf, 0, hash);
660 tdb_trace_2rec_retrec(tdb, "tdb_append", key, new_dbuf, dbuf);
663 tdb_unlock(tdb, BUCKET(hash), F_WRLCK);
664 SAFE_FREE(dbuf.dptr);
670 return the name of the current tdb file
671 useful for external logging functions
673 const char *tdb_name(struct tdb_context *tdb)
679 return the underlying file descriptor being used by tdb, or -1
680 useful for external routines that want to check the device/inode
683 int tdb_fd(struct tdb_context *tdb)
689 return the current logging function
690 useful for external tdb routines that wish to log tdb errors
692 tdb_log_func tdb_log_fn(struct tdb_context *tdb)
694 return tdb->log.log_fn;
699 get the tdb sequence number. Only makes sense if the writers opened
700 with TDB_SEQNUM set. Note that this sequence number will wrap quite
701 quickly, so it should only be used for a 'has something changed'
702 test, not for code that relies on the count of the number of changes
703 made. If you want a counter then use a tdb record.
705 The aim of this sequence number is to allow for a very lightweight
706 test of a possible tdb change.
708 int tdb_get_seqnum(struct tdb_context *tdb)
712 tdb_ofs_read(tdb, TDB_SEQNUM_OFS, &seqnum);
716 int tdb_hash_size(struct tdb_context *tdb)
718 return tdb->header.hash_size;
721 size_t tdb_map_size(struct tdb_context *tdb)
723 return tdb->map_size;
726 int tdb_get_flags(struct tdb_context *tdb)
731 void tdb_add_flags(struct tdb_context *tdb, unsigned flags)
736 void tdb_remove_flags(struct tdb_context *tdb, unsigned flags)
738 tdb->flags &= ~flags;
743 enable sequence number handling on an open tdb
745 void tdb_enable_seqnum(struct tdb_context *tdb)
747 tdb->flags |= TDB_SEQNUM;
752 add a region of the file to the freelist. Length is the size of the region in bytes,
753 which includes the free list header that needs to be added
755 static int tdb_free_region(struct tdb_context *tdb, tdb_off_t offset, ssize_t length)
757 struct tdb_record rec;
758 if (length <= sizeof(rec)) {
759 /* the region is not worth adding */
762 if (length + offset > tdb->map_size) {
763 TDB_LOG((tdb, TDB_DEBUG_FATAL,"tdb_free_region: adding region beyond end of file\n"));
766 memset(&rec,'\0',sizeof(rec));
767 rec.rec_len = length - sizeof(rec);
768 if (tdb_free(tdb, offset, &rec) == -1) {
769 TDB_LOG((tdb, TDB_DEBUG_FATAL,"tdb_free_region: failed to add free record\n"));
776 wipe the entire database, deleting all records. This can be done
777 very fast by using a global lock. The entire data portion of the
778 file becomes a single entry in the freelist.
780 This code carefully steps around the recovery area, leaving it alone
782 int tdb_wipe_all(struct tdb_context *tdb)
785 tdb_off_t offset = 0;
787 tdb_off_t recovery_head;
788 tdb_len_t recovery_size = 0;
790 if (tdb_lockall(tdb) != 0) {
794 tdb_trace(tdb, "tdb_wipe_all");
796 /* see if the tdb has a recovery area, and remember its size
797 if so. We don't want to lose this as otherwise each
798 tdb_wipe_all() in a transaction will increase the size of
799 the tdb by the size of the recovery area */
800 if (tdb_ofs_read(tdb, TDB_RECOVERY_HEAD, &recovery_head) == -1) {
801 TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_wipe_all: failed to read recovery head\n"));
805 if (recovery_head != 0) {
806 struct tdb_record rec;
807 if (tdb->methods->tdb_read(tdb, recovery_head, &rec, sizeof(rec), DOCONV()) == -1) {
808 TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_wipe_all: failed to read recovery record\n"));
811 recovery_size = rec.rec_len + sizeof(rec);
814 /* wipe the hashes */
815 for (i=0;i<tdb->header.hash_size;i++) {
816 if (tdb_ofs_write(tdb, TDB_HASH_TOP(i), &offset) == -1) {
817 TDB_LOG((tdb, TDB_DEBUG_FATAL,"tdb_wipe_all: failed to write hash %d\n", i));
822 /* wipe the freelist */
823 if (tdb_ofs_write(tdb, FREELIST_TOP, &offset) == -1) {
824 TDB_LOG((tdb, TDB_DEBUG_FATAL,"tdb_wipe_all: failed to write freelist\n"));
828 /* add all the rest of the file to the freelist, possibly leaving a gap
829 for the recovery area */
830 if (recovery_size == 0) {
831 /* the simple case - the whole file can be used as a freelist */
832 data_len = (tdb->map_size - TDB_DATA_START(tdb->header.hash_size));
833 if (tdb_free_region(tdb, TDB_DATA_START(tdb->header.hash_size), data_len) != 0) {
837 /* we need to add two freelist entries - one on either
838 side of the recovery area
840 Note that we cannot shift the recovery area during
841 this operation. Only the transaction.c code may
842 move the recovery area or we risk subtle data
845 data_len = (recovery_head - TDB_DATA_START(tdb->header.hash_size));
846 if (tdb_free_region(tdb, TDB_DATA_START(tdb->header.hash_size), data_len) != 0) {
849 /* and the 2nd free list entry after the recovery area - if any */
850 data_len = tdb->map_size - (recovery_head+recovery_size);
851 if (tdb_free_region(tdb, recovery_head+recovery_size, data_len) != 0) {
856 if (tdb_unlockall(tdb) != 0) {
857 TDB_LOG((tdb, TDB_DEBUG_FATAL,"tdb_wipe_all: failed to unlock\n"));
868 struct traverse_state {
870 struct tdb_context *dest_db;
874 traverse function for repacking
876 static int repack_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *private_data)
878 struct traverse_state *state = (struct traverse_state *)private_data;
879 if (tdb_store(state->dest_db, key, data, TDB_INSERT) != 0) {
889 int tdb_repack(struct tdb_context *tdb)
891 struct tdb_context *tmp_db;
892 struct traverse_state state;
894 tdb_trace(tdb, "tdb_repack");
896 if (tdb_transaction_start(tdb) != 0) {
897 TDB_LOG((tdb, TDB_DEBUG_FATAL, __location__ " Failed to start transaction\n"));
901 tmp_db = tdb_open("tmpdb", tdb_hash_size(tdb), TDB_INTERNAL, O_RDWR|O_CREAT, 0);
902 if (tmp_db == NULL) {
903 TDB_LOG((tdb, TDB_DEBUG_FATAL, __location__ " Failed to create tmp_db\n"));
904 tdb_transaction_cancel(tdb);
909 state.dest_db = tmp_db;
911 if (tdb_traverse_read(tdb, repack_traverse, &state) == -1) {
912 TDB_LOG((tdb, TDB_DEBUG_FATAL, __location__ " Failed to traverse copying out\n"));
913 tdb_transaction_cancel(tdb);
919 TDB_LOG((tdb, TDB_DEBUG_FATAL, __location__ " Error during traversal\n"));
920 tdb_transaction_cancel(tdb);
925 if (tdb_wipe_all(tdb) != 0) {
926 TDB_LOG((tdb, TDB_DEBUG_FATAL, __location__ " Failed to wipe database\n"));
927 tdb_transaction_cancel(tdb);
935 if (tdb_traverse_read(tmp_db, repack_traverse, &state) == -1) {
936 TDB_LOG((tdb, TDB_DEBUG_FATAL, __location__ " Failed to traverse copying back\n"));
937 tdb_transaction_cancel(tdb);
943 TDB_LOG((tdb, TDB_DEBUG_FATAL, __location__ " Error during second traversal\n"));
944 tdb_transaction_cancel(tdb);
951 if (tdb_transaction_commit(tdb) != 0) {
952 TDB_LOG((tdb, TDB_DEBUG_FATAL, __location__ " Failed to commit\n"));
960 static void tdb_trace_write(struct tdb_context *tdb, const char *str)
962 if (write(tdb->tracefd, str, strlen(str)) != strlen(str)) {
968 static void tdb_trace_start(struct tdb_context *tdb)
971 char msg[sizeof(tdb_off_t) * 4 + 1];
973 tdb_ofs_read(tdb, TDB_SEQNUM_OFS, &seqnum);
974 snprintf(msg, sizeof(msg), "%u ", seqnum);
975 tdb_trace_write(tdb, msg);
978 static void tdb_trace_end(struct tdb_context *tdb)
980 tdb_trace_write(tdb, "\n");
983 static void tdb_trace_end_ret(struct tdb_context *tdb, int ret)
985 char msg[sizeof(ret) * 4 + 4];
986 snprintf(msg, sizeof(msg), " = %i\n", ret);
987 tdb_trace_write(tdb, msg);
990 static void tdb_trace_record(struct tdb_context *tdb, TDB_DATA rec)
992 char msg[20 + rec.dsize*2], *p;
995 /* We differentiate zero-length records from non-existent ones. */
996 if (rec.dptr == NULL) {
997 tdb_trace_write(tdb, " NULL");
1001 /* snprintf here is purely cargo-cult programming. */
1003 p += snprintf(p, sizeof(msg), " %zu:", rec.dsize);
1004 for (i = 0; i < rec.dsize; i++)
1005 p += snprintf(p, 2, "%02x", rec.dptr[i]);
1007 tdb_trace_write(tdb, msg);
1010 void tdb_trace(struct tdb_context *tdb, const char *op)
1012 tdb_trace_start(tdb);
1013 tdb_trace_write(tdb, op);
1017 void tdb_trace_seqnum(struct tdb_context *tdb, uint32_t seqnum, const char *op)
1019 char msg[sizeof(tdb_off_t) * 4 + 1];
1021 snprintf(msg, sizeof(msg), "%u ", seqnum);
1022 tdb_trace_write(tdb, msg);
1023 tdb_trace_write(tdb, op);
1027 void tdb_trace_open(struct tdb_context *tdb, const char *op,
1028 unsigned hash_size, unsigned tdb_flags, unsigned open_flags)
1032 snprintf(msg, sizeof(msg),
1033 "%s %u 0x%x 0x%x", op, hash_size, tdb_flags, open_flags);
1034 tdb_trace_start(tdb);
1035 tdb_trace_write(tdb, msg);
1039 void tdb_trace_ret(struct tdb_context *tdb, const char *op, int ret)
1041 tdb_trace_start(tdb);
1042 tdb_trace_write(tdb, op);
1043 tdb_trace_end_ret(tdb, ret);
1046 void tdb_trace_retrec(struct tdb_context *tdb, const char *op, TDB_DATA ret)
1048 tdb_trace_start(tdb);
1049 tdb_trace_write(tdb, op);
1050 tdb_trace_write(tdb, " =");
1051 tdb_trace_record(tdb, ret);
1055 void tdb_trace_1rec(struct tdb_context *tdb, const char *op,
1058 tdb_trace_start(tdb);
1059 tdb_trace_write(tdb, op);
1060 tdb_trace_record(tdb, rec);
1064 void tdb_trace_1rec_ret(struct tdb_context *tdb, const char *op,
1065 TDB_DATA rec, int ret)
1067 tdb_trace_start(tdb);
1068 tdb_trace_write(tdb, op);
1069 tdb_trace_record(tdb, rec);
1070 tdb_trace_end_ret(tdb, ret);
1073 void tdb_trace_1rec_retrec(struct tdb_context *tdb, const char *op,
1074 TDB_DATA rec, TDB_DATA ret)
1076 tdb_trace_start(tdb);
1077 tdb_trace_write(tdb, op);
1078 tdb_trace_record(tdb, rec);
1079 tdb_trace_write(tdb, " =");
1080 tdb_trace_record(tdb, ret);
1084 void tdb_trace_2rec_flag_ret(struct tdb_context *tdb, const char *op,
1085 TDB_DATA rec1, TDB_DATA rec2, unsigned flag,
1088 char msg[1 + sizeof(ret) * 4];
1090 snprintf(msg, sizeof(msg), " %#x", flag);
1091 tdb_trace_start(tdb);
1092 tdb_trace_write(tdb, op);
1093 tdb_trace_record(tdb, rec1);
1094 tdb_trace_record(tdb, rec2);
1095 tdb_trace_write(tdb, msg);
1096 tdb_trace_end_ret(tdb, ret);
1099 void tdb_trace_2rec_retrec(struct tdb_context *tdb, const char *op,
1100 TDB_DATA rec1, TDB_DATA rec2, TDB_DATA ret)
1102 tdb_trace_start(tdb);
1103 tdb_trace_write(tdb, op);
1104 tdb_trace_record(tdb, rec1);
1105 tdb_trace_record(tdb, rec2);
1106 tdb_trace_write(tdb, " =");
1107 tdb_trace_record(tdb, ret);