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 tdb_trace(tdb, "tdb_increment_seqnum_nonblock");
46 /* we ignore errors from this, as we have no sane way of
49 tdb_ofs_read(tdb, TDB_SEQNUM_OFS, &seqnum);
51 tdb_ofs_write(tdb, TDB_SEQNUM_OFS, &seqnum);
55 increment the tdb sequence number if the tdb has been opened using
58 static void tdb_increment_seqnum(struct tdb_context *tdb)
60 if (!(tdb->flags & TDB_SEQNUM)) {
64 if (tdb_brlock(tdb, TDB_SEQNUM_OFS, F_WRLCK, F_SETLKW, 1, 1) != 0) {
68 tdb_increment_seqnum_nonblock(tdb);
70 tdb_brlock(tdb, TDB_SEQNUM_OFS, F_UNLCK, F_SETLKW, 1, 1);
73 static int tdb_key_compare(TDB_DATA key, TDB_DATA data, void *private_data)
75 return memcmp(data.dptr, key.dptr, data.dsize);
78 /* Returns 0 on fail. On success, return offset of record, and fills
80 static tdb_off_t tdb_find(struct tdb_context *tdb, TDB_DATA key, uint32_t hash,
81 struct list_struct *r)
85 /* read in the hash top */
86 if (tdb_ofs_read(tdb, TDB_HASH_TOP(hash), &rec_ptr) == -1)
89 /* keep looking until we find the right record */
91 if (tdb_rec_read(tdb, rec_ptr, r) == -1)
94 if (!TDB_DEAD(r) && hash==r->full_hash
95 && key.dsize==r->key_len
96 && tdb_parse_data(tdb, key, rec_ptr + sizeof(*r),
97 r->key_len, tdb_key_compare,
103 return TDB_ERRCODE(TDB_ERR_NOEXIST, 0);
106 /* As tdb_find, but if you succeed, keep the lock */
107 tdb_off_t tdb_find_lock_hash(struct tdb_context *tdb, TDB_DATA key, uint32_t hash, int locktype,
108 struct list_struct *rec)
112 if (tdb_lock(tdb, BUCKET(hash), locktype) == -1)
114 if (!(rec_ptr = tdb_find(tdb, key, hash, rec)))
115 tdb_unlock(tdb, BUCKET(hash), locktype);
120 /* update an entry in place - this only works if the new data size
121 is <= the old data size and the key exists.
122 on failure return -1.
124 static int tdb_update_hash(struct tdb_context *tdb, TDB_DATA key, uint32_t hash, TDB_DATA dbuf)
126 struct list_struct rec;
130 if (!(rec_ptr = tdb_find(tdb, key, hash, &rec)))
133 /* must be long enough key, data and tailer */
134 if (rec.rec_len < key.dsize + dbuf.dsize + sizeof(tdb_off_t)) {
135 tdb->ecode = TDB_SUCCESS; /* Not really an error */
139 if (tdb->methods->tdb_write(tdb, rec_ptr + sizeof(rec) + rec.key_len,
140 dbuf.dptr, dbuf.dsize) == -1)
143 if (dbuf.dsize != rec.data_len) {
145 rec.data_len = dbuf.dsize;
146 return tdb_rec_write(tdb, rec_ptr, &rec);
152 /* find an entry in the database given a key */
153 /* If an entry doesn't exist tdb_err will be set to
154 * TDB_ERR_NOEXIST. If a key has no data attached
155 * then the TDB_DATA will have zero length but
158 static TDB_DATA do_tdb_fetch(struct tdb_context *tdb, TDB_DATA key)
161 struct list_struct rec;
165 /* find which hash bucket it is in */
166 hash = tdb->hash_fn(&key);
167 if (!(rec_ptr = tdb_find_lock_hash(tdb,key,hash,F_RDLCK,&rec))) {
170 ret.dptr = tdb_alloc_read(tdb, rec_ptr + sizeof(rec) + rec.key_len,
172 ret.dsize = rec.data_len;
173 tdb_unlock(tdb, BUCKET(rec.full_hash), F_RDLCK);
177 TDB_DATA tdb_fetch(struct tdb_context *tdb, TDB_DATA key)
179 TDB_DATA ret = do_tdb_fetch(tdb, key);
181 tdb_trace_1rec_retrec(tdb, "tdb_fetch", key, ret);
186 * Find an entry in the database and hand the record's data to a parsing
187 * function. The parsing function is executed under the chain read lock, so it
188 * should be fast and should not block on other syscalls.
190 * DONT CALL OTHER TDB CALLS FROM THE PARSER, THIS MIGHT LEAD TO SEGFAULTS.
192 * For mmapped tdb's that do not have a transaction open it points the parsing
193 * function directly at the mmap area, it avoids the malloc/memcpy in this
194 * case. If a transaction is open or no mmap is available, it has to do
195 * malloc/read/parse/free.
197 * This is interesting for all readers of potentially large data structures in
198 * the tdb records, ldb indexes being one example.
201 int tdb_parse_record(struct tdb_context *tdb, TDB_DATA key,
202 int (*parser)(TDB_DATA key, TDB_DATA data,
207 struct list_struct rec;
211 /* find which hash bucket it is in */
212 hash = tdb->hash_fn(&key);
214 if (!(rec_ptr = tdb_find_lock_hash(tdb,key,hash,F_RDLCK,&rec))) {
215 tdb_trace_1rec_ret(tdb, "tdb_parse_record", key,
217 return TDB_ERRCODE(TDB_ERR_NOEXIST, 0);
219 tdb_trace_1rec_ret(tdb, "tdb_parse_record", key, 0);
221 ret = tdb_parse_data(tdb, key, rec_ptr + sizeof(rec) + rec.key_len,
222 rec.data_len, parser, private_data);
224 tdb_unlock(tdb, BUCKET(rec.full_hash), F_RDLCK);
229 /* check if an entry in the database exists
231 note that 1 is returned if the key is found and 0 is returned if not found
232 this doesn't match the conventions in the rest of this module, but is
235 static int tdb_exists_hash(struct tdb_context *tdb, TDB_DATA key, uint32_t hash)
237 struct list_struct rec;
239 if (tdb_find_lock_hash(tdb, key, hash, F_RDLCK, &rec) == 0)
241 tdb_unlock(tdb, BUCKET(rec.full_hash), F_RDLCK);
245 int tdb_exists(struct tdb_context *tdb, TDB_DATA key)
247 uint32_t hash = tdb->hash_fn(&key);
250 ret = tdb_exists_hash(tdb, key, hash);
251 tdb_trace_1rec_ret(tdb, "tdb_exists", key, ret);
255 /* actually delete an entry in the database given the offset */
256 int tdb_do_delete(struct tdb_context *tdb, tdb_off_t rec_ptr, struct list_struct *rec)
258 tdb_off_t last_ptr, i;
259 struct list_struct lastrec;
261 if (tdb->read_only || tdb->traverse_read) return -1;
263 if (tdb->traverse_write != 0 ||
264 tdb_write_lock_record(tdb, rec_ptr) == -1) {
265 /* Someone traversing here: mark it as dead */
266 rec->magic = TDB_DEAD_MAGIC;
267 return tdb_rec_write(tdb, rec_ptr, rec);
269 if (tdb_write_unlock_record(tdb, rec_ptr) != 0)
272 /* find previous record in hash chain */
273 if (tdb_ofs_read(tdb, TDB_HASH_TOP(rec->full_hash), &i) == -1)
275 for (last_ptr = 0; i != rec_ptr; last_ptr = i, i = lastrec.next)
276 if (tdb_rec_read(tdb, i, &lastrec) == -1)
279 /* unlink it: next ptr is at start of record. */
281 last_ptr = TDB_HASH_TOP(rec->full_hash);
282 if (tdb_ofs_write(tdb, last_ptr, &rec->next) == -1)
285 /* recover the space */
286 if (tdb_free(tdb, rec_ptr, rec) == -1)
291 static int tdb_count_dead(struct tdb_context *tdb, uint32_t hash)
295 struct list_struct rec;
297 /* read in the hash top */
298 if (tdb_ofs_read(tdb, TDB_HASH_TOP(hash), &rec_ptr) == -1)
302 if (tdb_rec_read(tdb, rec_ptr, &rec) == -1)
305 if (rec.magic == TDB_DEAD_MAGIC) {
314 * Purge all DEAD records from a hash chain
316 static int tdb_purge_dead(struct tdb_context *tdb, uint32_t hash)
319 struct list_struct rec;
322 if (tdb_lock(tdb, -1, F_WRLCK) == -1) {
326 /* read in the hash top */
327 if (tdb_ofs_read(tdb, TDB_HASH_TOP(hash), &rec_ptr) == -1)
333 if (tdb_rec_read(tdb, rec_ptr, &rec) == -1) {
339 if (rec.magic == TDB_DEAD_MAGIC
340 && tdb_do_delete(tdb, rec_ptr, &rec) == -1) {
347 tdb_unlock(tdb, -1, F_WRLCK);
351 /* delete an entry in the database given a key */
352 static int tdb_delete_hash(struct tdb_context *tdb, TDB_DATA key, uint32_t hash)
355 struct list_struct rec;
358 if (tdb->max_dead_records != 0) {
361 * Allow for some dead records per hash chain, mainly for
362 * tdb's with a very high create/delete rate like locking.tdb.
365 if (tdb_lock(tdb, BUCKET(hash), F_WRLCK) == -1)
368 if (tdb_count_dead(tdb, hash) >= tdb->max_dead_records) {
370 * Don't let the per-chain freelist grow too large,
371 * delete all existing dead records
373 tdb_purge_dead(tdb, hash);
376 if (!(rec_ptr = tdb_find(tdb, key, hash, &rec))) {
377 tdb_unlock(tdb, BUCKET(hash), F_WRLCK);
382 * Just mark the record as dead.
384 rec.magic = TDB_DEAD_MAGIC;
385 ret = tdb_rec_write(tdb, rec_ptr, &rec);
388 if (!(rec_ptr = tdb_find_lock_hash(tdb, key, hash, F_WRLCK,
392 ret = tdb_do_delete(tdb, rec_ptr, &rec);
396 tdb_increment_seqnum(tdb);
399 if (tdb_unlock(tdb, BUCKET(rec.full_hash), F_WRLCK) != 0)
400 TDB_LOG((tdb, TDB_DEBUG_WARNING, "tdb_delete: WARNING tdb_unlock failed!\n"));
404 int tdb_delete(struct tdb_context *tdb, TDB_DATA key)
406 uint32_t hash = tdb->hash_fn(&key);
409 ret = tdb_delete_hash(tdb, key, hash);
410 tdb_trace_1rec_ret(tdb, "tdb_delete", key, ret);
415 * See if we have a dead record around with enough space
417 static tdb_off_t tdb_find_dead(struct tdb_context *tdb, uint32_t hash,
418 struct list_struct *r, tdb_len_t length)
422 /* read in the hash top */
423 if (tdb_ofs_read(tdb, TDB_HASH_TOP(hash), &rec_ptr) == -1)
426 /* keep looking until we find the right record */
428 if (tdb_rec_read(tdb, rec_ptr, r) == -1)
431 if (TDB_DEAD(r) && r->rec_len >= length) {
433 * First fit for simple coding, TODO: change to best
443 static int _tdb_store(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf,
444 int flag, uint32_t hash)
446 struct list_struct rec;
451 /* check for it existing, on insert. */
452 if (flag == TDB_INSERT) {
453 if (tdb_exists_hash(tdb, key, hash)) {
454 tdb->ecode = TDB_ERR_EXISTS;
458 /* first try in-place update, on modify or replace. */
459 if (tdb_update_hash(tdb, key, hash, dbuf) == 0) {
462 if (tdb->ecode == TDB_ERR_NOEXIST &&
463 flag == TDB_MODIFY) {
464 /* if the record doesn't exist and we are in TDB_MODIFY mode then
465 we should fail the store */
469 /* reset the error code potentially set by the tdb_update() */
470 tdb->ecode = TDB_SUCCESS;
472 /* delete any existing record - if it doesn't exist we don't
473 care. Doing this first reduces fragmentation, and avoids
474 coalescing with `allocated' block before it's updated. */
475 if (flag != TDB_INSERT)
476 tdb_delete_hash(tdb, key, hash);
478 /* Copy key+value *before* allocating free space in case malloc
479 fails and we are left with a dead spot in the tdb. */
481 if (!(p = (char *)malloc(key.dsize + dbuf.dsize))) {
482 tdb->ecode = TDB_ERR_OOM;
486 memcpy(p, key.dptr, key.dsize);
488 memcpy(p+key.dsize, dbuf.dptr, dbuf.dsize);
490 if (tdb->max_dead_records != 0) {
492 * Allow for some dead records per hash chain, look if we can
493 * find one that can hold the new record. We need enough space
494 * for key, data and tailer. If we find one, we don't have to
495 * consult the central freelist.
497 rec_ptr = tdb_find_dead(
499 key.dsize + dbuf.dsize + sizeof(tdb_off_t));
502 rec.key_len = key.dsize;
503 rec.data_len = dbuf.dsize;
504 rec.full_hash = hash;
505 rec.magic = TDB_MAGIC;
506 if (tdb_rec_write(tdb, rec_ptr, &rec) == -1
507 || tdb->methods->tdb_write(
508 tdb, rec_ptr + sizeof(rec),
509 p, key.dsize + dbuf.dsize) == -1) {
517 * We have to allocate some space from the freelist, so this means we
518 * have to lock it. Use the chance to purge all the DEAD records from
519 * the hash chain under the freelist lock.
522 if (tdb_lock(tdb, -1, F_WRLCK) == -1) {
526 if ((tdb->max_dead_records != 0)
527 && (tdb_purge_dead(tdb, hash) == -1)) {
528 tdb_unlock(tdb, -1, F_WRLCK);
532 /* we have to allocate some space */
533 rec_ptr = tdb_allocate(tdb, key.dsize + dbuf.dsize, &rec);
535 tdb_unlock(tdb, -1, F_WRLCK);
541 /* Read hash top into next ptr */
542 if (tdb_ofs_read(tdb, TDB_HASH_TOP(hash), &rec.next) == -1)
545 rec.key_len = key.dsize;
546 rec.data_len = dbuf.dsize;
547 rec.full_hash = hash;
548 rec.magic = TDB_MAGIC;
550 /* write out and point the top of the hash chain at it */
551 if (tdb_rec_write(tdb, rec_ptr, &rec) == -1
552 || tdb->methods->tdb_write(tdb, rec_ptr+sizeof(rec), p, key.dsize+dbuf.dsize)==-1
553 || tdb_ofs_write(tdb, TDB_HASH_TOP(hash), &rec_ptr) == -1) {
554 /* Need to tdb_unallocate() here */
562 tdb_increment_seqnum(tdb);
569 /* store an element in the database, replacing any existing element
572 return 0 on success, -1 on failure
574 int tdb_store(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf, int flag)
579 if (tdb->read_only || tdb->traverse_read) {
580 tdb->ecode = TDB_ERR_RDONLY;
581 tdb_trace_2rec_flag_ret(tdb, "tdb_store", key, dbuf, flag,
586 /* find which hash bucket it is in */
587 hash = tdb->hash_fn(&key);
588 if (tdb_lock(tdb, BUCKET(hash), F_WRLCK) == -1)
591 ret = _tdb_store(tdb, key, dbuf, flag, hash);
592 tdb_trace_2rec_flag_ret(tdb, "tdb_store", key, dbuf, flag, ret);
593 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 = do_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);
690 tdb_trace_ret(tdb, "tdb_get_seqnum", seqnum);
694 int tdb_hash_size(struct tdb_context *tdb)
696 return tdb->header.hash_size;
699 size_t tdb_map_size(struct tdb_context *tdb)
701 return tdb->map_size;
704 int tdb_get_flags(struct tdb_context *tdb)
709 void tdb_add_flags(struct tdb_context *tdb, unsigned flags)
714 void tdb_remove_flags(struct tdb_context *tdb, unsigned flags)
716 tdb->flags &= ~flags;
721 enable sequence number handling on an open tdb
723 void tdb_enable_seqnum(struct tdb_context *tdb)
725 tdb->flags |= TDB_SEQNUM;
730 add a region of the file to the freelist. Length is the size of the region in bytes,
731 which includes the free list header that needs to be added
733 static int tdb_free_region(struct tdb_context *tdb, tdb_off_t offset, ssize_t length)
735 struct list_struct rec;
736 if (length <= sizeof(rec)) {
737 /* the region is not worth adding */
740 if (length + offset > tdb->map_size) {
741 TDB_LOG((tdb, TDB_DEBUG_FATAL,"tdb_free_region: adding region beyond end of file\n"));
744 memset(&rec,'\0',sizeof(rec));
745 rec.rec_len = length - sizeof(rec);
746 if (tdb_free(tdb, offset, &rec) == -1) {
747 TDB_LOG((tdb, TDB_DEBUG_FATAL,"tdb_free_region: failed to add free record\n"));
754 wipe the entire database, deleting all records. This can be done
755 very fast by using a global lock. The entire data portion of the
756 file becomes a single entry in the freelist.
758 This code carefully steps around the recovery area, leaving it alone
760 int tdb_wipe_all(struct tdb_context *tdb)
763 tdb_off_t offset = 0;
765 tdb_off_t recovery_head;
766 tdb_len_t recovery_size = 0;
768 if (tdb_lockall(tdb) != 0) {
772 tdb_trace(tdb, "tdb_wipe_all");
774 /* see if the tdb has a recovery area, and remember its size
775 if so. We don't want to lose this as otherwise each
776 tdb_wipe_all() in a transaction will increase the size of
777 the tdb by the size of the recovery area */
778 if (tdb_ofs_read(tdb, TDB_RECOVERY_HEAD, &recovery_head) == -1) {
779 TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_wipe_all: failed to read recovery head\n"));
783 if (recovery_head != 0) {
784 struct list_struct rec;
785 if (tdb->methods->tdb_read(tdb, recovery_head, &rec, sizeof(rec), DOCONV()) == -1) {
786 TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_wipe_all: failed to read recovery record\n"));
789 recovery_size = rec.rec_len + sizeof(rec);
792 /* wipe the hashes */
793 for (i=0;i<tdb->header.hash_size;i++) {
794 if (tdb_ofs_write(tdb, TDB_HASH_TOP(i), &offset) == -1) {
795 TDB_LOG((tdb, TDB_DEBUG_FATAL,"tdb_wipe_all: failed to write hash %d\n", i));
800 /* wipe the freelist */
801 if (tdb_ofs_write(tdb, FREELIST_TOP, &offset) == -1) {
802 TDB_LOG((tdb, TDB_DEBUG_FATAL,"tdb_wipe_all: failed to write freelist\n"));
806 /* add all the rest of the file to the freelist, possibly leaving a gap
807 for the recovery area */
808 if (recovery_size == 0) {
809 /* the simple case - the whole file can be used as a freelist */
810 data_len = (tdb->map_size - TDB_DATA_START(tdb->header.hash_size));
811 if (tdb_free_region(tdb, TDB_DATA_START(tdb->header.hash_size), data_len) != 0) {
815 /* we need to add two freelist entries - one on either
816 side of the recovery area
818 Note that we cannot shift the recovery area during
819 this operation. Only the transaction.c code may
820 move the recovery area or we risk subtle data
823 data_len = (recovery_head - TDB_DATA_START(tdb->header.hash_size));
824 if (tdb_free_region(tdb, TDB_DATA_START(tdb->header.hash_size), data_len) != 0) {
827 /* and the 2nd free list entry after the recovery area - if any */
828 data_len = tdb->map_size - (recovery_head+recovery_size);
829 if (tdb_free_region(tdb, recovery_head+recovery_size, data_len) != 0) {
834 if (tdb_unlockall(tdb) != 0) {
835 TDB_LOG((tdb, TDB_DEBUG_FATAL,"tdb_wipe_all: failed to unlock\n"));
847 static void tdb_trace_write(struct tdb_context *tdb, const char *str)
849 if (write(tdb->tracefd, str, strlen(str)) != strlen(str)) {
855 static void tdb_trace_start(struct tdb_context *tdb)
858 char msg[sizeof(tdb_off_t) * 4];
860 tdb_ofs_read(tdb, TDB_SEQNUM_OFS, &seqnum);
861 sprintf(msg, "%u ", seqnum);
862 tdb_trace_write(tdb, msg);
865 static void tdb_trace_end(struct tdb_context *tdb)
867 tdb_trace_write(tdb, "\n");
870 static void tdb_trace_end_ret(struct tdb_context *tdb, int ret)
872 char msg[sizeof(ret) * 4];
873 sprintf(msg, " = %i\n", ret);
874 tdb_trace_write(tdb, msg);
877 static void tdb_trace_record(struct tdb_context *tdb, TDB_DATA rec)
882 /* We differentiate zero-length records from non-existent ones. */
883 if (rec.dptr == NULL) {
884 tdb_trace_write(tdb, " NULL");
887 sprintf(msg, " %zu:", rec.dsize);
888 tdb_trace_write(tdb, msg);
889 for (i = 0; i < rec.dsize; i++) {
890 sprintf(msg, "%02x", rec.dptr[i]);
891 tdb_trace_write(tdb, msg);
895 void tdb_trace(struct tdb_context *tdb, const char *op)
897 tdb_trace_start(tdb);
898 tdb_trace_write(tdb, op);
902 void tdb_trace_open(struct tdb_context *tdb, const char *op,
903 unsigned hash_size, unsigned tdb_flags, unsigned open_flags)
907 sprintf(msg, "%s %u %#x %#x", op, hash_size, tdb_flags, open_flags);
908 tdb_trace_start(tdb);
909 tdb_trace_write(tdb, msg);
913 void tdb_trace_ret(struct tdb_context *tdb, const char *op, int ret)
915 tdb_trace_start(tdb);
916 tdb_trace_write(tdb, op);
917 tdb_trace_end_ret(tdb, ret);
920 void tdb_trace_retrec(struct tdb_context *tdb, const char *op, TDB_DATA ret)
922 tdb_trace_start(tdb);
923 tdb_trace_write(tdb, op);
924 tdb_trace_write(tdb, " =");
925 tdb_trace_record(tdb, ret);
929 void tdb_trace_1rec(struct tdb_context *tdb, const char *op,
932 tdb_trace_start(tdb);
933 tdb_trace_write(tdb, op);
934 tdb_trace_record(tdb, rec);
938 void tdb_trace_1rec_ret(struct tdb_context *tdb, const char *op,
939 TDB_DATA rec, int ret)
941 tdb_trace_start(tdb);
942 tdb_trace_write(tdb, op);
943 tdb_trace_record(tdb, rec);
944 tdb_trace_end_ret(tdb, ret);
947 void tdb_trace_1rec_retrec(struct tdb_context *tdb, const char *op,
948 TDB_DATA rec, TDB_DATA ret)
950 tdb_trace_start(tdb);
951 tdb_trace_write(tdb, op);
952 tdb_trace_record(tdb, rec);
953 tdb_trace_write(tdb, " =");
954 tdb_trace_record(tdb, ret);
958 void tdb_trace_2rec_flag_ret(struct tdb_context *tdb, const char *op,
959 TDB_DATA rec1, TDB_DATA rec2, unsigned flag,
962 char msg[sizeof(ret) * 4];
964 sprintf(msg, " %#x", flag);
965 tdb_trace_start(tdb);
966 tdb_trace_write(tdb, op);
967 tdb_trace_record(tdb, rec1);
968 tdb_trace_record(tdb, rec2);
969 tdb_trace_write(tdb, msg);
970 tdb_trace_end_ret(tdb, ret);
973 void tdb_trace_2rec_retrec(struct tdb_context *tdb, const char *op,
974 TDB_DATA rec1, TDB_DATA rec2, TDB_DATA ret)
976 tdb_trace_start(tdb);
977 tdb_trace_write(tdb, op);
978 tdb_trace_record(tdb, rec1);
979 tdb_trace_record(tdb, rec2);
980 tdb_trace_write(tdb, " =");
981 tdb_trace_record(tdb, ret);