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"
30 _PUBLIC_ TDB_DATA tdb_null;
33 non-blocking increment of the tdb sequence number if the tdb has been opened using
36 _PUBLIC_ 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->transaction != NULL) {
63 tdb_increment_seqnum_nonblock(tdb);
67 if (tdb_nest_lock(tdb, TDB_SEQNUM_OFS, F_WRLCK,
68 TDB_LOCK_WAIT|TDB_LOCK_PROBE) != 0) {
72 tdb_increment_seqnum_nonblock(tdb);
74 tdb_nest_unlock(tdb, TDB_SEQNUM_OFS, F_WRLCK, false);
77 static int tdb_key_compare(TDB_DATA key, TDB_DATA data, void *private_data)
79 return memcmp(data.dptr, key.dptr, data.dsize);
82 /* Returns 0 on fail. On success, return offset of record, and fills
84 static tdb_off_t tdb_find(struct tdb_context *tdb, TDB_DATA key, uint32_t hash,
89 /* read in the hash top */
90 if (tdb_ofs_read(tdb, TDB_HASH_TOP(hash), &rec_ptr) == -1)
93 /* keep looking until we find the right record */
95 if (tdb_rec_read(tdb, rec_ptr, r) == -1)
98 if (!TDB_DEAD(r) && hash==r->full_hash
99 && key.dsize==r->key_len
100 && tdb_parse_data(tdb, key, rec_ptr + sizeof(*r),
101 r->key_len, tdb_key_compare,
105 /* detect tight infinite loop */
106 if (rec_ptr == r->next) {
107 tdb->ecode = TDB_ERR_CORRUPT;
108 TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_find: loop detected.\n"));
113 tdb->ecode = TDB_ERR_NOEXIST;
117 /* As tdb_find, but if you succeed, keep the lock */
118 tdb_off_t tdb_find_lock_hash(struct tdb_context *tdb, TDB_DATA key, uint32_t hash, int locktype,
119 struct tdb_record *rec)
123 if (tdb_lock(tdb, BUCKET(hash), locktype) == -1)
125 if (!(rec_ptr = tdb_find(tdb, key, hash, rec)))
126 tdb_unlock(tdb, BUCKET(hash), locktype);
130 static TDB_DATA _tdb_fetch(struct tdb_context *tdb, TDB_DATA key);
132 struct tdb_update_hash_state {
133 const TDB_DATA *dbufs;
138 static int tdb_update_hash_cmp(TDB_DATA key, TDB_DATA data, void *private_data)
140 struct tdb_update_hash_state *state = private_data;
141 unsigned char *dptr = data.dptr;
144 if (state->dbufs_len != data.dsize) {
148 for (i=0; i<state->num_dbufs; i++) {
149 TDB_DATA dbuf = state->dbufs[i];
151 ret = memcmp(dptr, dbuf.dptr, dbuf.dsize);
161 /* update an entry in place - this only works if the new data size
162 is <= the old data size and the key exists.
163 on failure return -1.
165 static int tdb_update_hash(struct tdb_context *tdb, TDB_DATA key,
167 const TDB_DATA *dbufs, int num_dbufs,
170 struct tdb_record rec;
171 tdb_off_t rec_ptr, ofs;
175 if (!(rec_ptr = tdb_find(tdb, key, hash, &rec)))
178 /* it could be an exact duplicate of what is there - this is
179 * surprisingly common (eg. with a ldb re-index). */
180 if (rec.data_len == dbufs_len) {
181 struct tdb_update_hash_state state = {
182 .dbufs = dbufs, .num_dbufs = num_dbufs,
183 .dbufs_len = dbufs_len
187 ret = tdb_parse_record(tdb, key, tdb_update_hash_cmp, &state);
193 /* must be long enough key, data and tailer */
194 if (rec.rec_len < key.dsize + dbufs_len + sizeof(tdb_off_t)) {
195 tdb->ecode = TDB_SUCCESS; /* Not really an error */
199 ofs = rec_ptr + sizeof(rec) + rec.key_len;
201 for (i=0; i<num_dbufs; i++) {
202 TDB_DATA dbuf = dbufs[i];
205 ret = tdb->methods->tdb_write(tdb, ofs, dbuf.dptr, dbuf.dsize);
212 if (dbufs_len != rec.data_len) {
214 rec.data_len = dbufs_len;
215 return tdb_rec_write(tdb, rec_ptr, &rec);
221 /* find an entry in the database given a key */
222 /* If an entry doesn't exist tdb_err will be set to
223 * TDB_ERR_NOEXIST. If a key has no data attached
224 * then the TDB_DATA will have zero length but
227 static TDB_DATA _tdb_fetch(struct tdb_context *tdb, TDB_DATA key)
230 struct tdb_record rec;
234 /* find which hash bucket it is in */
235 hash = tdb->hash_fn(&key);
236 if (!(rec_ptr = tdb_find_lock_hash(tdb,key,hash,F_RDLCK,&rec)))
239 ret.dptr = tdb_alloc_read(tdb, rec_ptr + sizeof(rec) + rec.key_len,
241 ret.dsize = rec.data_len;
242 tdb_unlock(tdb, BUCKET(rec.full_hash), F_RDLCK);
246 _PUBLIC_ TDB_DATA tdb_fetch(struct tdb_context *tdb, TDB_DATA key)
248 TDB_DATA ret = _tdb_fetch(tdb, key);
250 tdb_trace_1rec_retrec(tdb, "tdb_fetch", key, ret);
255 * Find an entry in the database and hand the record's data to a parsing
256 * function. The parsing function is executed under the chain read lock, so it
257 * should be fast and should not block on other syscalls.
259 * DON'T CALL OTHER TDB CALLS FROM THE PARSER, THIS MIGHT LEAD TO SEGFAULTS.
261 * For mmapped tdb's that do not have a transaction open it points the parsing
262 * function directly at the mmap area, it avoids the malloc/memcpy in this
263 * case. If a transaction is open or no mmap is available, it has to do
264 * malloc/read/parse/free.
266 * This is interesting for all readers of potentially large data structures in
267 * the tdb records, ldb indexes being one example.
269 * Return -1 if the record was not found.
272 _PUBLIC_ int tdb_parse_record(struct tdb_context *tdb, TDB_DATA key,
273 int (*parser)(TDB_DATA key, TDB_DATA data,
278 struct tdb_record rec;
282 /* find which hash bucket it is in */
283 hash = tdb->hash_fn(&key);
285 if (!(rec_ptr = tdb_find_lock_hash(tdb,key,hash,F_RDLCK,&rec))) {
286 /* record not found */
287 tdb_trace_1rec_ret(tdb, "tdb_parse_record", key, -1);
288 tdb->ecode = TDB_ERR_NOEXIST;
291 tdb_trace_1rec_ret(tdb, "tdb_parse_record", key, 0);
293 ret = tdb_parse_data(tdb, key, rec_ptr + sizeof(rec) + rec.key_len,
294 rec.data_len, parser, private_data);
296 tdb_unlock(tdb, BUCKET(rec.full_hash), F_RDLCK);
301 /* check if an entry in the database exists
303 note that 1 is returned if the key is found and 0 is returned if not found
304 this doesn't match the conventions in the rest of this module, but is
307 static int tdb_exists_hash(struct tdb_context *tdb, TDB_DATA key, uint32_t hash)
309 struct tdb_record rec;
311 if (tdb_find_lock_hash(tdb, key, hash, F_RDLCK, &rec) == 0)
313 tdb_unlock(tdb, BUCKET(rec.full_hash), F_RDLCK);
317 _PUBLIC_ int tdb_exists(struct tdb_context *tdb, TDB_DATA key)
319 uint32_t hash = tdb->hash_fn(&key);
322 ret = tdb_exists_hash(tdb, key, hash);
323 tdb_trace_1rec_ret(tdb, "tdb_exists", key, ret);
327 /* actually delete an entry in the database given the offset */
328 int tdb_do_delete(struct tdb_context *tdb, tdb_off_t rec_ptr, struct tdb_record *rec)
330 tdb_off_t last_ptr, i;
331 struct tdb_record lastrec;
333 if (tdb->read_only || tdb->traverse_read) return -1;
335 if (((tdb->traverse_write != 0) && (!TDB_DEAD(rec))) ||
336 tdb_write_lock_record(tdb, rec_ptr) == -1) {
337 /* Someone traversing here: mark it as dead */
338 rec->magic = TDB_DEAD_MAGIC;
339 return tdb_rec_write(tdb, rec_ptr, rec);
341 if (tdb_write_unlock_record(tdb, rec_ptr) != 0)
344 /* find previous record in hash chain */
345 if (tdb_ofs_read(tdb, TDB_HASH_TOP(rec->full_hash), &i) == -1)
347 for (last_ptr = 0; i != rec_ptr; last_ptr = i, i = lastrec.next)
348 if (tdb_rec_read(tdb, i, &lastrec) == -1)
351 /* unlink it: next ptr is at start of record. */
353 last_ptr = TDB_HASH_TOP(rec->full_hash);
354 if (tdb_ofs_write(tdb, last_ptr, &rec->next) == -1)
357 /* recover the space */
358 if (tdb_free(tdb, rec_ptr, rec) == -1)
363 static int tdb_count_dead(struct tdb_context *tdb, uint32_t hash)
367 struct tdb_record rec;
369 /* read in the hash top */
370 if (tdb_ofs_read(tdb, TDB_HASH_TOP(hash), &rec_ptr) == -1)
374 if (tdb_rec_read(tdb, rec_ptr, &rec) == -1)
377 if (rec.magic == TDB_DEAD_MAGIC) {
386 * Purge all DEAD records from a hash chain
388 int tdb_purge_dead(struct tdb_context *tdb, uint32_t hash)
391 struct tdb_record rec;
394 if (tdb_lock_nonblock(tdb, -1, F_WRLCK) == -1) {
396 * Don't block the freelist if not strictly necessary
401 /* read in the hash top */
402 if (tdb_ofs_read(tdb, TDB_HASH_TOP(hash), &rec_ptr) == -1)
408 if (tdb_rec_read(tdb, rec_ptr, &rec) == -1) {
414 if (rec.magic == TDB_DEAD_MAGIC
415 && tdb_do_delete(tdb, rec_ptr, &rec) == -1) {
422 tdb_unlock(tdb, -1, F_WRLCK);
426 /* delete an entry in the database given a key */
427 static int tdb_delete_hash(struct tdb_context *tdb, TDB_DATA key, uint32_t hash)
430 struct tdb_record rec;
433 rec_ptr = tdb_find_lock_hash(tdb, key, hash, F_WRLCK, &rec);
438 if (tdb->max_dead_records != 0) {
440 uint32_t magic = TDB_DEAD_MAGIC;
443 * Allow for some dead records per hash chain, mainly for
444 * tdb's with a very high create/delete rate like locking.tdb.
447 if (tdb_count_dead(tdb, hash) >= tdb->max_dead_records) {
449 * Don't let the per-chain freelist grow too large,
450 * delete all existing dead records
452 tdb_purge_dead(tdb, hash);
456 * Just mark the record as dead.
459 tdb, rec_ptr + offsetof(struct tdb_record, magic),
463 ret = tdb_do_delete(tdb, rec_ptr, &rec);
467 tdb_increment_seqnum(tdb);
470 if (tdb_unlock(tdb, BUCKET(hash), F_WRLCK) != 0)
471 TDB_LOG((tdb, TDB_DEBUG_WARNING, "tdb_delete: WARNING tdb_unlock failed!\n"));
475 _PUBLIC_ int tdb_delete(struct tdb_context *tdb, TDB_DATA key)
477 uint32_t hash = tdb->hash_fn(&key);
480 ret = tdb_delete_hash(tdb, key, hash);
481 tdb_trace_1rec_ret(tdb, "tdb_delete", key, ret);
486 * See if we have a dead record around with enough space
488 tdb_off_t tdb_find_dead(struct tdb_context *tdb, uint32_t hash,
489 struct tdb_record *r, tdb_len_t length,
490 tdb_off_t *p_last_ptr)
492 tdb_off_t rec_ptr, last_ptr;
493 tdb_off_t best_rec_ptr = 0;
494 tdb_off_t best_last_ptr = 0;
495 struct tdb_record best = { .rec_len = UINT32_MAX };
497 length += sizeof(tdb_off_t); /* tailer */
499 last_ptr = TDB_HASH_TOP(hash);
501 /* read in the hash top */
502 if (tdb_ofs_read(tdb, last_ptr, &rec_ptr) == -1)
505 /* keep looking until we find the right record */
507 if (tdb_rec_read(tdb, rec_ptr, r) == -1)
510 if (TDB_DEAD(r) && (r->rec_len >= length) &&
511 (r->rec_len < best.rec_len)) {
512 best_rec_ptr = rec_ptr;
513 best_last_ptr = last_ptr;
520 if (best.rec_len == UINT32_MAX) {
525 *p_last_ptr = best_last_ptr;
529 static int _tdb_storev(struct tdb_context *tdb, TDB_DATA key,
530 const TDB_DATA *dbufs, int num_dbufs,
531 int flag, uint32_t hash)
533 struct tdb_record rec;
534 tdb_off_t rec_ptr, ofs;
535 tdb_len_t rec_len, dbufs_len;
541 for (i=0; i<num_dbufs; i++) {
542 size_t dsize = dbufs[i].dsize;
545 if (dbufs_len < dsize) {
546 tdb->ecode = TDB_ERR_OOM;
551 rec_len = key.dsize + dbufs_len;
552 if ((rec_len < key.dsize) || (rec_len < dbufs_len)) {
553 tdb->ecode = TDB_ERR_OOM;
557 /* check for it existing, on insert. */
558 if (flag == TDB_INSERT) {
559 if (tdb_exists_hash(tdb, key, hash)) {
560 tdb->ecode = TDB_ERR_EXISTS;
564 /* first try in-place update, on modify or replace. */
565 if (tdb_update_hash(tdb, key, hash, dbufs, num_dbufs,
569 if (tdb->ecode == TDB_ERR_NOEXIST &&
570 flag == TDB_MODIFY) {
571 /* if the record doesn't exist and we are in TDB_MODIFY mode then
572 we should fail the store */
576 /* reset the error code potentially set by the tdb_update_hash() */
577 tdb->ecode = TDB_SUCCESS;
579 /* delete any existing record - if it doesn't exist we don't
580 care. Doing this first reduces fragmentation, and avoids
581 coalescing with `allocated' block before it's updated. */
582 if (flag != TDB_INSERT)
583 tdb_delete_hash(tdb, key, hash);
585 /* we have to allocate some space */
586 rec_ptr = tdb_allocate(tdb, hash, rec_len, &rec);
592 /* Read hash top into next ptr */
593 if (tdb_ofs_read(tdb, TDB_HASH_TOP(hash), &rec.next) == -1)
596 rec.key_len = key.dsize;
597 rec.data_len = dbufs_len;
598 rec.full_hash = hash;
599 rec.magic = TDB_MAGIC;
603 /* write out and point the top of the hash chain at it */
604 ret = tdb_rec_write(tdb, ofs, &rec);
610 ret = tdb->methods->tdb_write(tdb, ofs, key.dptr, key.dsize);
616 for (i=0; i<num_dbufs; i++) {
617 ret = tdb->methods->tdb_write(tdb, ofs, dbufs[i].dptr,
622 ofs += dbufs[i].dsize;
625 ret = tdb_ofs_write(tdb, TDB_HASH_TOP(hash), &rec_ptr);
627 /* Need to tdb_unallocate() here */
635 tdb_increment_seqnum(tdb);
640 static int _tdb_store(struct tdb_context *tdb, TDB_DATA key,
641 TDB_DATA dbuf, int flag, uint32_t hash)
643 return _tdb_storev(tdb, key, &dbuf, 1, flag, hash);
646 /* store an element in the database, replacing any existing element
649 return 0 on success, -1 on failure
651 _PUBLIC_ int tdb_store(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf, int flag)
656 if (tdb->read_only || tdb->traverse_read) {
657 tdb->ecode = TDB_ERR_RDONLY;
658 tdb_trace_2rec_flag_ret(tdb, "tdb_store", key, dbuf, flag, -1);
662 /* find which hash bucket it is in */
663 hash = tdb->hash_fn(&key);
664 if (tdb_lock(tdb, BUCKET(hash), F_WRLCK) == -1)
667 ret = _tdb_store(tdb, key, dbuf, flag, hash);
668 tdb_trace_2rec_flag_ret(tdb, "tdb_store", key, dbuf, flag, ret);
669 tdb_unlock(tdb, BUCKET(hash), F_WRLCK);
673 /* Append to an entry. Create if not exist. */
674 _PUBLIC_ int tdb_append(struct tdb_context *tdb, TDB_DATA key, TDB_DATA new_dbuf)
680 /* find which hash bucket it is in */
681 hash = tdb->hash_fn(&key);
682 if (tdb_lock(tdb, BUCKET(hash), F_WRLCK) == -1)
685 dbuf = _tdb_fetch(tdb, key);
687 if (dbuf.dptr == NULL) {
688 dbuf.dptr = (unsigned char *)malloc(new_dbuf.dsize);
690 unsigned int new_len = dbuf.dsize + new_dbuf.dsize;
691 unsigned char *new_dptr;
693 /* realloc '0' is special: don't do that. */
696 new_dptr = (unsigned char *)realloc(dbuf.dptr, new_len);
697 if (new_dptr == NULL) {
700 dbuf.dptr = new_dptr;
703 if (dbuf.dptr == NULL) {
704 tdb->ecode = TDB_ERR_OOM;
708 memcpy(dbuf.dptr + dbuf.dsize, new_dbuf.dptr, new_dbuf.dsize);
709 dbuf.dsize += new_dbuf.dsize;
711 ret = _tdb_store(tdb, key, dbuf, 0, hash);
712 tdb_trace_2rec_retrec(tdb, "tdb_append", key, new_dbuf, dbuf);
715 tdb_unlock(tdb, BUCKET(hash), F_WRLCK);
716 SAFE_FREE(dbuf.dptr);
722 return the name of the current tdb file
723 useful for external logging functions
725 _PUBLIC_ const char *tdb_name(struct tdb_context *tdb)
731 return the underlying file descriptor being used by tdb, or -1
732 useful for external routines that want to check the device/inode
735 _PUBLIC_ int tdb_fd(struct tdb_context *tdb)
741 return the current logging function
742 useful for external tdb routines that wish to log tdb errors
744 _PUBLIC_ tdb_log_func tdb_log_fn(struct tdb_context *tdb)
746 return tdb->log.log_fn;
751 get the tdb sequence number. Only makes sense if the writers opened
752 with TDB_SEQNUM set. Note that this sequence number will wrap quite
753 quickly, so it should only be used for a 'has something changed'
754 test, not for code that relies on the count of the number of changes
755 made. If you want a counter then use a tdb record.
757 The aim of this sequence number is to allow for a very lightweight
758 test of a possible tdb change.
760 _PUBLIC_ int tdb_get_seqnum(struct tdb_context *tdb)
764 tdb_ofs_read(tdb, TDB_SEQNUM_OFS, &seqnum);
768 _PUBLIC_ int tdb_hash_size(struct tdb_context *tdb)
770 return tdb->hash_size;
773 _PUBLIC_ size_t tdb_map_size(struct tdb_context *tdb)
775 return tdb->map_size;
778 _PUBLIC_ int tdb_get_flags(struct tdb_context *tdb)
783 _PUBLIC_ void tdb_add_flags(struct tdb_context *tdb, unsigned flags)
785 if ((flags & TDB_ALLOW_NESTING) &&
786 (flags & TDB_DISALLOW_NESTING)) {
787 tdb->ecode = TDB_ERR_NESTING;
788 TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_add_flags: "
789 "allow_nesting and disallow_nesting are not allowed together!"));
793 if (flags & TDB_ALLOW_NESTING) {
794 tdb->flags &= ~TDB_DISALLOW_NESTING;
796 if (flags & TDB_DISALLOW_NESTING) {
797 tdb->flags &= ~TDB_ALLOW_NESTING;
803 _PUBLIC_ void tdb_remove_flags(struct tdb_context *tdb, unsigned flags)
805 if ((flags & TDB_ALLOW_NESTING) &&
806 (flags & TDB_DISALLOW_NESTING)) {
807 tdb->ecode = TDB_ERR_NESTING;
808 TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_remove_flags: "
809 "allow_nesting and disallow_nesting are not allowed together!"));
813 if ((flags & TDB_NOLOCK) &&
814 (tdb->feature_flags & TDB_FEATURE_FLAG_MUTEX) &&
815 (tdb->mutexes == NULL)) {
816 tdb->ecode = TDB_ERR_LOCK;
817 TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_remove_flags: "
818 "Can not remove NOLOCK flag on mutexed databases"));
822 if (flags & TDB_ALLOW_NESTING) {
823 tdb->flags |= TDB_DISALLOW_NESTING;
825 if (flags & TDB_DISALLOW_NESTING) {
826 tdb->flags |= TDB_ALLOW_NESTING;
829 tdb->flags &= ~flags;
834 enable sequence number handling on an open tdb
836 _PUBLIC_ void tdb_enable_seqnum(struct tdb_context *tdb)
838 tdb->flags |= TDB_SEQNUM;
843 add a region of the file to the freelist. Length is the size of the region in bytes,
844 which includes the free list header that needs to be added
846 static int tdb_free_region(struct tdb_context *tdb, tdb_off_t offset, ssize_t length)
848 struct tdb_record rec;
849 if (length <= sizeof(rec)) {
850 /* the region is not worth adding */
853 if (length + offset > tdb->map_size) {
854 TDB_LOG((tdb, TDB_DEBUG_FATAL,"tdb_free_region: adding region beyond end of file\n"));
857 memset(&rec,'\0',sizeof(rec));
858 rec.rec_len = length - sizeof(rec);
859 if (tdb_free(tdb, offset, &rec) == -1) {
860 TDB_LOG((tdb, TDB_DEBUG_FATAL,"tdb_free_region: failed to add free record\n"));
867 wipe the entire database, deleting all records. This can be done
868 very fast by using a allrecord lock. The entire data portion of the
869 file becomes a single entry in the freelist.
871 This code carefully steps around the recovery area, leaving it alone
873 _PUBLIC_ int tdb_wipe_all(struct tdb_context *tdb)
876 tdb_off_t offset = 0;
878 tdb_off_t recovery_head;
879 tdb_len_t recovery_size = 0;
881 if (tdb_lockall(tdb) != 0) {
885 tdb_trace(tdb, "tdb_wipe_all");
887 /* see if the tdb has a recovery area, and remember its size
888 if so. We don't want to lose this as otherwise each
889 tdb_wipe_all() in a transaction will increase the size of
890 the tdb by the size of the recovery area */
891 if (tdb_ofs_read(tdb, TDB_RECOVERY_HEAD, &recovery_head) == -1) {
892 TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_wipe_all: failed to read recovery head\n"));
896 if (recovery_head != 0) {
897 struct tdb_record rec;
898 if (tdb->methods->tdb_read(tdb, recovery_head, &rec, sizeof(rec), DOCONV()) == -1) {
899 TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_wipe_all: failed to read recovery record\n"));
902 recovery_size = rec.rec_len + sizeof(rec);
905 /* wipe the hashes */
906 for (i=0;i<tdb->hash_size;i++) {
907 if (tdb_ofs_write(tdb, TDB_HASH_TOP(i), &offset) == -1) {
908 TDB_LOG((tdb, TDB_DEBUG_FATAL,"tdb_wipe_all: failed to write hash %d\n", i));
913 /* wipe the freelist */
914 if (tdb_ofs_write(tdb, FREELIST_TOP, &offset) == -1) {
915 TDB_LOG((tdb, TDB_DEBUG_FATAL,"tdb_wipe_all: failed to write freelist\n"));
919 /* add all the rest of the file to the freelist, possibly leaving a gap
920 for the recovery area */
921 if (recovery_size == 0) {
922 /* the simple case - the whole file can be used as a freelist */
923 data_len = (tdb->map_size - TDB_DATA_START(tdb->hash_size));
924 if (tdb_free_region(tdb, TDB_DATA_START(tdb->hash_size), data_len) != 0) {
928 /* we need to add two freelist entries - one on either
929 side of the recovery area
931 Note that we cannot shift the recovery area during
932 this operation. Only the transaction.c code may
933 move the recovery area or we risk subtle data
936 data_len = (recovery_head - TDB_DATA_START(tdb->hash_size));
937 if (tdb_free_region(tdb, TDB_DATA_START(tdb->hash_size), data_len) != 0) {
940 /* and the 2nd free list entry after the recovery area - if any */
941 data_len = tdb->map_size - (recovery_head+recovery_size);
942 if (tdb_free_region(tdb, recovery_head+recovery_size, data_len) != 0) {
947 tdb_increment_seqnum_nonblock(tdb);
949 if (tdb_unlockall(tdb) != 0) {
950 TDB_LOG((tdb, TDB_DEBUG_FATAL,"tdb_wipe_all: failed to unlock\n"));
961 struct traverse_state {
963 struct tdb_context *dest_db;
967 traverse function for repacking
969 static int repack_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *private_data)
971 struct traverse_state *state = (struct traverse_state *)private_data;
972 if (tdb_store(state->dest_db, key, data, TDB_INSERT) != 0) {
982 _PUBLIC_ int tdb_repack(struct tdb_context *tdb)
984 struct tdb_context *tmp_db;
985 struct traverse_state state;
987 tdb_trace(tdb, "tdb_repack");
989 if (tdb_transaction_start(tdb) != 0) {
990 TDB_LOG((tdb, TDB_DEBUG_FATAL, __location__ " Failed to start transaction\n"));
994 tmp_db = tdb_open("tmpdb", tdb_hash_size(tdb), TDB_INTERNAL, O_RDWR|O_CREAT, 0);
995 if (tmp_db == NULL) {
996 TDB_LOG((tdb, TDB_DEBUG_FATAL, __location__ " Failed to create tmp_db\n"));
997 tdb_transaction_cancel(tdb);
1001 state.error = false;
1002 state.dest_db = tmp_db;
1004 if (tdb_traverse_read(tdb, repack_traverse, &state) == -1) {
1005 TDB_LOG((tdb, TDB_DEBUG_FATAL, __location__ " Failed to traverse copying out\n"));
1006 tdb_transaction_cancel(tdb);
1012 TDB_LOG((tdb, TDB_DEBUG_FATAL, __location__ " Error during traversal\n"));
1013 tdb_transaction_cancel(tdb);
1018 if (tdb_wipe_all(tdb) != 0) {
1019 TDB_LOG((tdb, TDB_DEBUG_FATAL, __location__ " Failed to wipe database\n"));
1020 tdb_transaction_cancel(tdb);
1025 state.error = false;
1026 state.dest_db = tdb;
1028 if (tdb_traverse_read(tmp_db, repack_traverse, &state) == -1) {
1029 TDB_LOG((tdb, TDB_DEBUG_FATAL, __location__ " Failed to traverse copying back\n"));
1030 tdb_transaction_cancel(tdb);
1036 TDB_LOG((tdb, TDB_DEBUG_FATAL, __location__ " Error during second traversal\n"));
1037 tdb_transaction_cancel(tdb);
1044 if (tdb_transaction_commit(tdb) != 0) {
1045 TDB_LOG((tdb, TDB_DEBUG_FATAL, __location__ " Failed to commit\n"));
1052 /* Even on files, we can get partial writes due to signals. */
1053 bool tdb_write_all(int fd, const void *buf, size_t count)
1057 ret = write(fd, buf, count);
1060 buf = (const char *)buf + ret;
1066 bool tdb_add_off_t(tdb_off_t a, tdb_off_t b, tdb_off_t *pret)
1068 tdb_off_t ret = a + b;
1070 if ((ret < a) || (ret < b)) {
1078 static void tdb_trace_write(struct tdb_context *tdb, const char *str)
1080 if (!tdb_write_all(tdb->tracefd, str, strlen(str))) {
1081 close(tdb->tracefd);
1086 static void tdb_trace_start(struct tdb_context *tdb)
1089 char msg[sizeof(tdb_off_t) * 4 + 1];
1091 tdb_ofs_read(tdb, TDB_SEQNUM_OFS, &seqnum);
1092 snprintf(msg, sizeof(msg), "%u ", seqnum);
1093 tdb_trace_write(tdb, msg);
1096 static void tdb_trace_end(struct tdb_context *tdb)
1098 tdb_trace_write(tdb, "\n");
1101 static void tdb_trace_end_ret(struct tdb_context *tdb, int ret)
1103 char msg[sizeof(ret) * 4 + 4];
1104 snprintf(msg, sizeof(msg), " = %i\n", ret);
1105 tdb_trace_write(tdb, msg);
1108 static void tdb_trace_record(struct tdb_context *tdb, TDB_DATA rec)
1110 char msg[20 + rec.dsize*2], *p;
1113 /* We differentiate zero-length records from non-existent ones. */
1114 if (rec.dptr == NULL) {
1115 tdb_trace_write(tdb, " NULL");
1119 /* snprintf here is purely cargo-cult programming. */
1121 p += snprintf(p, sizeof(msg), " %zu:", rec.dsize);
1122 for (i = 0; i < rec.dsize; i++)
1123 p += snprintf(p, 2, "%02x", rec.dptr[i]);
1125 tdb_trace_write(tdb, msg);
1128 void tdb_trace(struct tdb_context *tdb, const char *op)
1130 tdb_trace_start(tdb);
1131 tdb_trace_write(tdb, op);
1135 void tdb_trace_seqnum(struct tdb_context *tdb, uint32_t seqnum, const char *op)
1137 char msg[sizeof(tdb_off_t) * 4 + 1];
1139 snprintf(msg, sizeof(msg), "%u ", seqnum);
1140 tdb_trace_write(tdb, msg);
1141 tdb_trace_write(tdb, op);
1145 void tdb_trace_open(struct tdb_context *tdb, const char *op,
1146 unsigned hash_size, unsigned tdb_flags, unsigned open_flags)
1150 snprintf(msg, sizeof(msg),
1151 "%s %u 0x%x 0x%x", op, hash_size, tdb_flags, open_flags);
1152 tdb_trace_start(tdb);
1153 tdb_trace_write(tdb, msg);
1157 void tdb_trace_ret(struct tdb_context *tdb, const char *op, int ret)
1159 tdb_trace_start(tdb);
1160 tdb_trace_write(tdb, op);
1161 tdb_trace_end_ret(tdb, ret);
1164 void tdb_trace_retrec(struct tdb_context *tdb, const char *op, TDB_DATA ret)
1166 tdb_trace_start(tdb);
1167 tdb_trace_write(tdb, op);
1168 tdb_trace_write(tdb, " =");
1169 tdb_trace_record(tdb, ret);
1173 void tdb_trace_1rec(struct tdb_context *tdb, const char *op,
1176 tdb_trace_start(tdb);
1177 tdb_trace_write(tdb, op);
1178 tdb_trace_record(tdb, rec);
1182 void tdb_trace_1rec_ret(struct tdb_context *tdb, const char *op,
1183 TDB_DATA rec, int ret)
1185 tdb_trace_start(tdb);
1186 tdb_trace_write(tdb, op);
1187 tdb_trace_record(tdb, rec);
1188 tdb_trace_end_ret(tdb, ret);
1191 void tdb_trace_1rec_retrec(struct tdb_context *tdb, const char *op,
1192 TDB_DATA rec, TDB_DATA ret)
1194 tdb_trace_start(tdb);
1195 tdb_trace_write(tdb, op);
1196 tdb_trace_record(tdb, rec);
1197 tdb_trace_write(tdb, " =");
1198 tdb_trace_record(tdb, ret);
1202 void tdb_trace_2rec_flag_ret(struct tdb_context *tdb, const char *op,
1203 TDB_DATA rec1, TDB_DATA rec2, unsigned flag,
1206 char msg[1 + sizeof(ret) * 4];
1208 snprintf(msg, sizeof(msg), " %#x", flag);
1209 tdb_trace_start(tdb);
1210 tdb_trace_write(tdb, op);
1211 tdb_trace_record(tdb, rec1);
1212 tdb_trace_record(tdb, rec2);
1213 tdb_trace_write(tdb, msg);
1214 tdb_trace_end_ret(tdb, ret);
1217 void tdb_trace_2rec_retrec(struct tdb_context *tdb, const char *op,
1218 TDB_DATA rec1, TDB_DATA rec2, TDB_DATA ret)
1220 tdb_trace_start(tdb);
1221 tdb_trace_write(tdb, op);
1222 tdb_trace_record(tdb, rec1);
1223 tdb_trace_record(tdb, rec2);
1224 tdb_trace_write(tdb, " =");
1225 tdb_trace_record(tdb, ret);