Allow NULL queue to writev_send
[Samba.git] / lib / tdb / common / tdb.c
blobb59bb1571c3af92f4f75a1f19e3448ed706694db
1 /*
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
12 ** under the LGPL
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 TDB_DATA tdb_null;
33 non-blocking increment of the tdb sequence number if the tdb has been opened using
34 the TDB_SEQNUM flag
36 void tdb_increment_seqnum_nonblock(struct tdb_context *tdb)
38 tdb_off_t seqnum=0;
40 if (!(tdb->flags & TDB_SEQNUM)) {
41 return;
44 /* we ignore errors from this, as we have no sane way of
45 dealing with them.
47 tdb_ofs_read(tdb, TDB_SEQNUM_OFS, &seqnum);
48 seqnum++;
49 tdb_ofs_write(tdb, TDB_SEQNUM_OFS, &seqnum);
53 increment the tdb sequence number if the tdb has been opened using
54 the TDB_SEQNUM flag
56 static void tdb_increment_seqnum(struct tdb_context *tdb)
58 if (!(tdb->flags & TDB_SEQNUM)) {
59 return;
62 if (tdb_brlock(tdb, TDB_SEQNUM_OFS, F_WRLCK, F_SETLKW, 1, 1) != 0) {
63 return;
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
77 in rec */
78 static tdb_off_t tdb_find(struct tdb_context *tdb, TDB_DATA key, uint32_t hash,
79 struct list_struct *r)
81 tdb_off_t rec_ptr;
83 /* read in the hash top */
84 if (tdb_ofs_read(tdb, TDB_HASH_TOP(hash), &rec_ptr) == -1)
85 return 0;
87 /* keep looking until we find the right record */
88 while (rec_ptr) {
89 if (tdb_rec_read(tdb, rec_ptr, r) == -1)
90 return 0;
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,
96 NULL) == 0) {
97 return rec_ptr;
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);
104 rec_ptr = r->next;
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)
113 uint32_t rec_ptr;
115 if (tdb_lock(tdb, BUCKET(hash), locktype) == -1)
116 return 0;
117 if (!(rec_ptr = tdb_find(tdb, key, hash, rec)))
118 tdb_unlock(tdb, BUCKET(hash), locktype);
119 return rec_ptr;
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;
130 tdb_off_t rec_ptr;
132 /* find entry */
133 if (!(rec_ptr = tdb_find(tdb, key, hash, &rec)))
134 return -1;
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 */
139 return -1;
142 if (tdb->methods->tdb_write(tdb, rec_ptr + sizeof(rec) + rec.key_len,
143 dbuf.dptr, dbuf.dsize) == -1)
144 return -1;
146 if (dbuf.dsize != rec.data_len) {
147 /* update size */
148 rec.data_len = dbuf.dsize;
149 return tdb_rec_write(tdb, rec_ptr, &rec);
152 return 0;
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
159 * a non-zero pointer
161 TDB_DATA tdb_fetch(struct tdb_context *tdb, TDB_DATA key)
163 tdb_off_t rec_ptr;
164 struct list_struct rec;
165 TDB_DATA ret;
166 uint32_t hash;
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)))
171 return tdb_null;
173 ret.dptr = tdb_alloc_read(tdb, rec_ptr + sizeof(rec) + rec.key_len,
174 rec.data_len);
175 ret.dsize = rec.data_len;
176 tdb_unlock(tdb, BUCKET(rec.full_hash), F_RDLCK);
177 return ret;
181 * Find an entry in the database and hand the record's data to a parsing
182 * function. The parsing function is executed under the chain read lock, so it
183 * should be fast and should not block on other syscalls.
185 * DONT CALL OTHER TDB CALLS FROM THE PARSER, THIS MIGHT LEAD TO SEGFAULTS.
187 * For mmapped tdb's that do not have a transaction open it points the parsing
188 * function directly at the mmap area, it avoids the malloc/memcpy in this
189 * case. If a transaction is open or no mmap is available, it has to do
190 * malloc/read/parse/free.
192 * This is interesting for all readers of potentially large data structures in
193 * the tdb records, ldb indexes being one example.
196 int tdb_parse_record(struct tdb_context *tdb, TDB_DATA key,
197 int (*parser)(TDB_DATA key, TDB_DATA data,
198 void *private_data),
199 void *private_data)
201 tdb_off_t rec_ptr;
202 struct list_struct rec;
203 int ret;
204 uint32_t hash;
206 /* find which hash bucket it is in */
207 hash = tdb->hash_fn(&key);
209 if (!(rec_ptr = tdb_find_lock_hash(tdb,key,hash,F_RDLCK,&rec))) {
210 return TDB_ERRCODE(TDB_ERR_NOEXIST, 0);
213 ret = tdb_parse_data(tdb, key, rec_ptr + sizeof(rec) + rec.key_len,
214 rec.data_len, parser, private_data);
216 tdb_unlock(tdb, BUCKET(rec.full_hash), F_RDLCK);
218 return ret;
221 /* check if an entry in the database exists
223 note that 1 is returned if the key is found and 0 is returned if not found
224 this doesn't match the conventions in the rest of this module, but is
225 compatible with gdbm
227 static int tdb_exists_hash(struct tdb_context *tdb, TDB_DATA key, uint32_t hash)
229 struct list_struct rec;
231 if (tdb_find_lock_hash(tdb, key, hash, F_RDLCK, &rec) == 0)
232 return 0;
233 tdb_unlock(tdb, BUCKET(rec.full_hash), F_RDLCK);
234 return 1;
237 int tdb_exists(struct tdb_context *tdb, TDB_DATA key)
239 uint32_t hash = tdb->hash_fn(&key);
240 return tdb_exists_hash(tdb, key, hash);
243 /* actually delete an entry in the database given the offset */
244 int tdb_do_delete(struct tdb_context *tdb, tdb_off_t rec_ptr, struct list_struct *rec)
246 tdb_off_t last_ptr, i;
247 struct list_struct lastrec;
249 if (tdb->read_only || tdb->traverse_read) return -1;
251 if (((tdb->traverse_write != 0) && (!TDB_DEAD(rec))) ||
252 tdb_write_lock_record(tdb, rec_ptr) == -1) {
253 /* Someone traversing here: mark it as dead */
254 rec->magic = TDB_DEAD_MAGIC;
255 return tdb_rec_write(tdb, rec_ptr, rec);
257 if (tdb_write_unlock_record(tdb, rec_ptr) != 0)
258 return -1;
260 /* find previous record in hash chain */
261 if (tdb_ofs_read(tdb, TDB_HASH_TOP(rec->full_hash), &i) == -1)
262 return -1;
263 for (last_ptr = 0; i != rec_ptr; last_ptr = i, i = lastrec.next)
264 if (tdb_rec_read(tdb, i, &lastrec) == -1)
265 return -1;
267 /* unlink it: next ptr is at start of record. */
268 if (last_ptr == 0)
269 last_ptr = TDB_HASH_TOP(rec->full_hash);
270 if (tdb_ofs_write(tdb, last_ptr, &rec->next) == -1)
271 return -1;
273 /* recover the space */
274 if (tdb_free(tdb, rec_ptr, rec) == -1)
275 return -1;
276 return 0;
279 static int tdb_count_dead(struct tdb_context *tdb, uint32_t hash)
281 int res = 0;
282 tdb_off_t rec_ptr;
283 struct list_struct rec;
285 /* read in the hash top */
286 if (tdb_ofs_read(tdb, TDB_HASH_TOP(hash), &rec_ptr) == -1)
287 return 0;
289 while (rec_ptr) {
290 if (tdb_rec_read(tdb, rec_ptr, &rec) == -1)
291 return 0;
293 if (rec.magic == TDB_DEAD_MAGIC) {
294 res += 1;
296 rec_ptr = rec.next;
298 return res;
302 * Purge all DEAD records from a hash chain
304 static int tdb_purge_dead(struct tdb_context *tdb, uint32_t hash)
306 int res = -1;
307 struct list_struct rec;
308 tdb_off_t rec_ptr;
310 if (tdb_lock(tdb, -1, F_WRLCK) == -1) {
311 return -1;
314 /* read in the hash top */
315 if (tdb_ofs_read(tdb, TDB_HASH_TOP(hash), &rec_ptr) == -1)
316 goto fail;
318 while (rec_ptr) {
319 tdb_off_t next;
321 if (tdb_rec_read(tdb, rec_ptr, &rec) == -1) {
322 goto fail;
325 next = rec.next;
327 if (rec.magic == TDB_DEAD_MAGIC
328 && tdb_do_delete(tdb, rec_ptr, &rec) == -1) {
329 goto fail;
331 rec_ptr = next;
333 res = 0;
334 fail:
335 tdb_unlock(tdb, -1, F_WRLCK);
336 return res;
339 /* delete an entry in the database given a key */
340 static int tdb_delete_hash(struct tdb_context *tdb, TDB_DATA key, uint32_t hash)
342 tdb_off_t rec_ptr;
343 struct list_struct rec;
344 int ret;
346 if (tdb->max_dead_records != 0) {
349 * Allow for some dead records per hash chain, mainly for
350 * tdb's with a very high create/delete rate like locking.tdb.
353 if (tdb_lock(tdb, BUCKET(hash), F_WRLCK) == -1)
354 return -1;
356 if (tdb_count_dead(tdb, hash) >= tdb->max_dead_records) {
358 * Don't let the per-chain freelist grow too large,
359 * delete all existing dead records
361 tdb_purge_dead(tdb, hash);
364 if (!(rec_ptr = tdb_find(tdb, key, hash, &rec))) {
365 tdb_unlock(tdb, BUCKET(hash), F_WRLCK);
366 return -1;
370 * Just mark the record as dead.
372 rec.magic = TDB_DEAD_MAGIC;
373 ret = tdb_rec_write(tdb, rec_ptr, &rec);
375 else {
376 if (!(rec_ptr = tdb_find_lock_hash(tdb, key, hash, F_WRLCK,
377 &rec)))
378 return -1;
380 ret = tdb_do_delete(tdb, rec_ptr, &rec);
383 if (ret == 0) {
384 tdb_increment_seqnum(tdb);
387 if (tdb_unlock(tdb, BUCKET(rec.full_hash), F_WRLCK) != 0)
388 TDB_LOG((tdb, TDB_DEBUG_WARNING, "tdb_delete: WARNING tdb_unlock failed!\n"));
389 return ret;
392 int tdb_delete(struct tdb_context *tdb, TDB_DATA key)
394 uint32_t hash = tdb->hash_fn(&key);
395 return tdb_delete_hash(tdb, key, hash);
399 * See if we have a dead record around with enough space
401 static tdb_off_t tdb_find_dead(struct tdb_context *tdb, uint32_t hash,
402 struct list_struct *r, tdb_len_t length)
404 tdb_off_t rec_ptr;
406 /* read in the hash top */
407 if (tdb_ofs_read(tdb, TDB_HASH_TOP(hash), &rec_ptr) == -1)
408 return 0;
410 /* keep looking until we find the right record */
411 while (rec_ptr) {
412 if (tdb_rec_read(tdb, rec_ptr, r) == -1)
413 return 0;
415 if (TDB_DEAD(r) && r->rec_len >= length) {
417 * First fit for simple coding, TODO: change to best
418 * fit
420 return rec_ptr;
422 rec_ptr = r->next;
424 return 0;
427 /* store an element in the database, replacing any existing element
428 with the same key
430 return 0 on success, -1 on failure
432 int tdb_store(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf, int flag)
434 struct list_struct rec;
435 uint32_t hash;
436 tdb_off_t rec_ptr;
437 char *p = NULL;
438 int ret = -1;
440 if (tdb->read_only || tdb->traverse_read) {
441 tdb->ecode = TDB_ERR_RDONLY;
442 return -1;
445 /* find which hash bucket it is in */
446 hash = tdb->hash_fn(&key);
447 if (tdb_lock(tdb, BUCKET(hash), F_WRLCK) == -1)
448 return -1;
450 /* check for it existing, on insert. */
451 if (flag == TDB_INSERT) {
452 if (tdb_exists_hash(tdb, key, hash)) {
453 tdb->ecode = TDB_ERR_EXISTS;
454 goto fail;
456 } else {
457 /* first try in-place update, on modify or replace. */
458 if (tdb_update_hash(tdb, key, hash, dbuf) == 0) {
459 goto done;
461 if (tdb->ecode == TDB_ERR_NOEXIST &&
462 flag == TDB_MODIFY) {
463 /* if the record doesn't exist and we are in TDB_MODIFY mode then
464 we should fail the store */
465 goto fail;
468 /* reset the error code potentially set by the tdb_update() */
469 tdb->ecode = TDB_SUCCESS;
471 /* delete any existing record - if it doesn't exist we don't
472 care. Doing this first reduces fragmentation, and avoids
473 coalescing with `allocated' block before it's updated. */
474 if (flag != TDB_INSERT)
475 tdb_delete_hash(tdb, key, hash);
477 /* Copy key+value *before* allocating free space in case malloc
478 fails and we are left with a dead spot in the tdb. */
480 if (!(p = (char *)malloc(key.dsize + dbuf.dsize))) {
481 tdb->ecode = TDB_ERR_OOM;
482 goto fail;
485 memcpy(p, key.dptr, key.dsize);
486 if (dbuf.dsize)
487 memcpy(p+key.dsize, dbuf.dptr, dbuf.dsize);
489 if (tdb->max_dead_records != 0) {
491 * Allow for some dead records per hash chain, look if we can
492 * find one that can hold the new record. We need enough space
493 * for key, data and tailer. If we find one, we don't have to
494 * consult the central freelist.
496 rec_ptr = tdb_find_dead(
497 tdb, hash, &rec,
498 key.dsize + dbuf.dsize + sizeof(tdb_off_t));
500 if (rec_ptr != 0) {
501 rec.key_len = key.dsize;
502 rec.data_len = dbuf.dsize;
503 rec.full_hash = hash;
504 rec.magic = TDB_MAGIC;
505 if (tdb_rec_write(tdb, rec_ptr, &rec) == -1
506 || tdb->methods->tdb_write(
507 tdb, rec_ptr + sizeof(rec),
508 p, key.dsize + dbuf.dsize) == -1) {
509 goto fail;
511 goto done;
516 * We have to allocate some space from the freelist, so this means we
517 * have to lock it. Use the chance to purge all the DEAD records from
518 * the hash chain under the freelist lock.
521 if (tdb_lock(tdb, -1, F_WRLCK) == -1) {
522 goto fail;
525 if ((tdb->max_dead_records != 0)
526 && (tdb_purge_dead(tdb, hash) == -1)) {
527 tdb_unlock(tdb, -1, F_WRLCK);
528 goto fail;
531 /* we have to allocate some space */
532 rec_ptr = tdb_allocate(tdb, key.dsize + dbuf.dsize, &rec);
534 tdb_unlock(tdb, -1, F_WRLCK);
536 if (rec_ptr == 0) {
537 goto fail;
540 /* Read hash top into next ptr */
541 if (tdb_ofs_read(tdb, TDB_HASH_TOP(hash), &rec.next) == -1)
542 goto fail;
544 rec.key_len = key.dsize;
545 rec.data_len = dbuf.dsize;
546 rec.full_hash = hash;
547 rec.magic = TDB_MAGIC;
549 /* write out and point the top of the hash chain at it */
550 if (tdb_rec_write(tdb, rec_ptr, &rec) == -1
551 || tdb->methods->tdb_write(tdb, rec_ptr+sizeof(rec), p, key.dsize+dbuf.dsize)==-1
552 || tdb_ofs_write(tdb, TDB_HASH_TOP(hash), &rec_ptr) == -1) {
553 /* Need to tdb_unallocate() here */
554 goto fail;
557 done:
558 ret = 0;
559 fail:
560 if (ret == 0) {
561 tdb_increment_seqnum(tdb);
564 SAFE_FREE(p);
565 tdb_unlock(tdb, BUCKET(hash), F_WRLCK);
566 return ret;
570 /* Append to an entry. Create if not exist. */
571 int tdb_append(struct tdb_context *tdb, TDB_DATA key, TDB_DATA new_dbuf)
573 uint32_t hash;
574 TDB_DATA dbuf;
575 int ret = -1;
577 /* find which hash bucket it is in */
578 hash = tdb->hash_fn(&key);
579 if (tdb_lock(tdb, BUCKET(hash), F_WRLCK) == -1)
580 return -1;
582 dbuf = tdb_fetch(tdb, key);
584 if (dbuf.dptr == NULL) {
585 dbuf.dptr = (unsigned char *)malloc(new_dbuf.dsize);
586 } else {
587 unsigned char *new_dptr = (unsigned char *)realloc(dbuf.dptr,
588 dbuf.dsize + new_dbuf.dsize);
589 if (new_dptr == NULL) {
590 free(dbuf.dptr);
592 dbuf.dptr = new_dptr;
595 if (dbuf.dptr == NULL) {
596 tdb->ecode = TDB_ERR_OOM;
597 goto failed;
600 memcpy(dbuf.dptr + dbuf.dsize, new_dbuf.dptr, new_dbuf.dsize);
601 dbuf.dsize += new_dbuf.dsize;
603 ret = tdb_store(tdb, key, dbuf, 0);
605 failed:
606 tdb_unlock(tdb, BUCKET(hash), F_WRLCK);
607 SAFE_FREE(dbuf.dptr);
608 return ret;
613 return the name of the current tdb file
614 useful for external logging functions
616 const char *tdb_name(struct tdb_context *tdb)
618 return tdb->name;
622 return the underlying file descriptor being used by tdb, or -1
623 useful for external routines that want to check the device/inode
624 of the fd
626 int tdb_fd(struct tdb_context *tdb)
628 return tdb->fd;
632 return the current logging function
633 useful for external tdb routines that wish to log tdb errors
635 tdb_log_func tdb_log_fn(struct tdb_context *tdb)
637 return tdb->log.log_fn;
642 get the tdb sequence number. Only makes sense if the writers opened
643 with TDB_SEQNUM set. Note that this sequence number will wrap quite
644 quickly, so it should only be used for a 'has something changed'
645 test, not for code that relies on the count of the number of changes
646 made. If you want a counter then use a tdb record.
648 The aim of this sequence number is to allow for a very lightweight
649 test of a possible tdb change.
651 int tdb_get_seqnum(struct tdb_context *tdb)
653 tdb_off_t seqnum=0;
655 tdb_ofs_read(tdb, TDB_SEQNUM_OFS, &seqnum);
656 return seqnum;
659 int tdb_hash_size(struct tdb_context *tdb)
661 return tdb->header.hash_size;
664 size_t tdb_map_size(struct tdb_context *tdb)
666 return tdb->map_size;
669 int tdb_get_flags(struct tdb_context *tdb)
671 return tdb->flags;
674 void tdb_add_flags(struct tdb_context *tdb, unsigned flags)
676 tdb->flags |= flags;
679 void tdb_remove_flags(struct tdb_context *tdb, unsigned flags)
681 tdb->flags &= ~flags;
686 enable sequence number handling on an open tdb
688 void tdb_enable_seqnum(struct tdb_context *tdb)
690 tdb->flags |= TDB_SEQNUM;
695 add a region of the file to the freelist. Length is the size of the region in bytes,
696 which includes the free list header that needs to be added
698 static int tdb_free_region(struct tdb_context *tdb, tdb_off_t offset, ssize_t length)
700 struct list_struct rec;
701 if (length <= sizeof(rec)) {
702 /* the region is not worth adding */
703 return 0;
705 if (length + offset > tdb->map_size) {
706 TDB_LOG((tdb, TDB_DEBUG_FATAL,"tdb_free_region: adding region beyond end of file\n"));
707 return -1;
709 memset(&rec,'\0',sizeof(rec));
710 rec.rec_len = length - sizeof(rec);
711 if (tdb_free(tdb, offset, &rec) == -1) {
712 TDB_LOG((tdb, TDB_DEBUG_FATAL,"tdb_free_region: failed to add free record\n"));
713 return -1;
715 return 0;
719 wipe the entire database, deleting all records. This can be done
720 very fast by using a global lock. The entire data portion of the
721 file becomes a single entry in the freelist.
723 This code carefully steps around the recovery area, leaving it alone
725 int tdb_wipe_all(struct tdb_context *tdb)
727 int i;
728 tdb_off_t offset = 0;
729 ssize_t data_len;
730 tdb_off_t recovery_head;
731 tdb_len_t recovery_size = 0;
733 if (tdb_lockall(tdb) != 0) {
734 return -1;
737 /* see if the tdb has a recovery area, and remember its size
738 if so. We don't want to lose this as otherwise each
739 tdb_wipe_all() in a transaction will increase the size of
740 the tdb by the size of the recovery area */
741 if (tdb_ofs_read(tdb, TDB_RECOVERY_HEAD, &recovery_head) == -1) {
742 TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_wipe_all: failed to read recovery head\n"));
743 goto failed;
746 if (recovery_head != 0) {
747 struct list_struct rec;
748 if (tdb->methods->tdb_read(tdb, recovery_head, &rec, sizeof(rec), DOCONV()) == -1) {
749 TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_wipe_all: failed to read recovery record\n"));
750 return -1;
752 recovery_size = rec.rec_len + sizeof(rec);
755 /* wipe the hashes */
756 for (i=0;i<tdb->header.hash_size;i++) {
757 if (tdb_ofs_write(tdb, TDB_HASH_TOP(i), &offset) == -1) {
758 TDB_LOG((tdb, TDB_DEBUG_FATAL,"tdb_wipe_all: failed to write hash %d\n", i));
759 goto failed;
763 /* wipe the freelist */
764 if (tdb_ofs_write(tdb, FREELIST_TOP, &offset) == -1) {
765 TDB_LOG((tdb, TDB_DEBUG_FATAL,"tdb_wipe_all: failed to write freelist\n"));
766 goto failed;
769 /* add all the rest of the file to the freelist, possibly leaving a gap
770 for the recovery area */
771 if (recovery_size == 0) {
772 /* the simple case - the whole file can be used as a freelist */
773 data_len = (tdb->map_size - TDB_DATA_START(tdb->header.hash_size));
774 if (tdb_free_region(tdb, TDB_DATA_START(tdb->header.hash_size), data_len) != 0) {
775 goto failed;
777 } else {
778 /* we need to add two freelist entries - one on either
779 side of the recovery area
781 Note that we cannot shift the recovery area during
782 this operation. Only the transaction.c code may
783 move the recovery area or we risk subtle data
784 corruption
786 data_len = (recovery_head - TDB_DATA_START(tdb->header.hash_size));
787 if (tdb_free_region(tdb, TDB_DATA_START(tdb->header.hash_size), data_len) != 0) {
788 goto failed;
790 /* and the 2nd free list entry after the recovery area - if any */
791 data_len = tdb->map_size - (recovery_head+recovery_size);
792 if (tdb_free_region(tdb, recovery_head+recovery_size, data_len) != 0) {
793 goto failed;
797 if (tdb_unlockall(tdb) != 0) {
798 TDB_LOG((tdb, TDB_DEBUG_FATAL,"tdb_wipe_all: failed to unlock\n"));
799 goto failed;
802 return 0;
804 failed:
805 tdb_unlockall(tdb);
806 return -1;
809 struct traverse_state {
810 bool error;
811 struct tdb_context *dest_db;
815 traverse function for repacking
817 static int repack_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *private)
819 struct traverse_state *state = (struct traverse_state *)private;
820 if (tdb_store(state->dest_db, key, data, TDB_INSERT) != 0) {
821 state->error = true;
822 return -1;
824 return 0;
828 repack a tdb
830 int tdb_repack(struct tdb_context *tdb)
832 struct tdb_context *tmp_db;
833 struct traverse_state state;
835 if (tdb_transaction_start(tdb) != 0) {
836 TDB_LOG((tdb, TDB_DEBUG_FATAL, __location__ " Failed to start transaction\n"));
837 return -1;
840 tmp_db = tdb_open("tmpdb", tdb_hash_size(tdb), TDB_INTERNAL, O_RDWR|O_CREAT, 0);
841 if (tmp_db == NULL) {
842 TDB_LOG((tdb, TDB_DEBUG_FATAL, __location__ " Failed to create tmp_db\n"));
843 tdb_transaction_cancel(tdb);
844 return -1;
847 state.error = false;
848 state.dest_db = tmp_db;
850 if (tdb_traverse_read(tdb, repack_traverse, &state) == -1) {
851 TDB_LOG((tdb, TDB_DEBUG_FATAL, __location__ " Failed to traverse copying out\n"));
852 tdb_transaction_cancel(tdb);
853 tdb_close(tmp_db);
854 return -1;
857 if (state.error) {
858 TDB_LOG((tdb, TDB_DEBUG_FATAL, __location__ " Error during traversal\n"));
859 tdb_transaction_cancel(tdb);
860 tdb_close(tmp_db);
861 return -1;
864 if (tdb_wipe_all(tdb) != 0) {
865 TDB_LOG((tdb, TDB_DEBUG_FATAL, __location__ " Failed to wipe database\n"));
866 tdb_transaction_cancel(tdb);
867 tdb_close(tmp_db);
868 return -1;
871 state.error = false;
872 state.dest_db = tdb;
874 if (tdb_traverse_read(tmp_db, repack_traverse, &state) == -1) {
875 TDB_LOG((tdb, TDB_DEBUG_FATAL, __location__ " Failed to traverse copying back\n"));
876 tdb_transaction_cancel(tdb);
877 tdb_close(tmp_db);
878 return -1;
881 if (state.error) {
882 TDB_LOG((tdb, TDB_DEBUG_FATAL, __location__ " Error during second traversal\n"));
883 tdb_transaction_cancel(tdb);
884 tdb_close(tmp_db);
885 return -1;
888 tdb_close(tmp_db);
890 if (tdb_transaction_commit(tdb) != 0) {
891 TDB_LOG((tdb, TDB_DEBUG_FATAL, __location__ " Failed to commit\n"));
892 return -1;
895 return 0;